{ "metadata": { "kernelspec": { "name": "python", "display_name": "Python (Pyodide)", "language": "python" }, "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8" } }, "nbformat_minor": 5, "nbformat": 4, "cells": [ { "id": "80c19b23-b438-47c9-a982-1b0ab88dae87", "cell_type": "markdown", "source": "
\n
Y9 - Autumn 2 - Lesson 5: Lists and iteration
\n
\n\n
", "metadata": {} }, { "id": "2c1b9716-5a5d-45b1-ae1a-7c8f5c548fb1", "cell_type": "markdown", "source": "
\n

🕛   Do Now: Fix The Code Below\n\n
\n\n
\n
    \n
  • There are three errors in the code below
  • \n
  • Identify what they are and fix them
  • \n
  • Expain the changes you made in the comments section
  • \n
\n
", "metadata": {} }, { "id": "b3185f4a-1f26-40a0-b0a5-d7c709a76de3", "cell_type": "code", "source": "# Do Now\nguess = input(\"Name one of the four nations of the UK\")\n\nif guess.lower() = \"england\":\n print(\"Correct, England is a nation of the UK\")\nif guess.lower() = \"wales\":\n print(\"Correct, wales is a nation of the UK\")\nif guess.lower() = \"scotland\":\n print(\"Correct, Scotland is a nation of the UK\")\nif guess.lower() = \"northern ireland\":\n print(\"Correct, Northern Ireland is a nation of the UK\")\nelse:\n print(\"Incorrect, {guess} is not a nation of the UK\")\n\n#----------------------------Comments-------------------------------\n# Identify the three changes you made.\n\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": null }, { "id": "8acd9fa7-63cc-476c-9223-7f269838b81b", "cell_type": "markdown", "source": "
\n
\n
\n
\n
\n

🎓  Modelling Activity (A): Python Lists\n\n
\n\n
\n

Your techer will demonstrate several key skills, you may code along with your teacher if you wish to do so

\n\n
    \n
  • Understanding what a list is and how list indexing works
  • \n
  • Accessing a list item via its index
  • \n
  • Accessing a range of items by giving the start and end index numbers
  • \n
  • Understanding that the item at the end index number isn't included in the output (up-to but not including)
  • \n \n
\n
", "metadata": {} }, { "id": "afd1d001-3bab-4c8b-a708-8f3f6bdf168e", "cell_type": "code", "source": "# Modelling Activity A - Python Lists\n\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": null }, { "id": "93fd2f05-f9b1-436f-af0f-b2507cb42b3f", "cell_type": "markdown", "source": "
\n
\n
\n
\n
\n

⌨️  Coding Activity (B): Python Lists\n\n
\n\n

Use what you have just learned to complete the activities below

\n

Here are some code snippets to help you with your code

\n\n\n
\nprint(desserts[3])\n
\n
\n
\n\n

Outputs from the first index number, up-to (but not including) the last index number

\n
\nprint(desserts[6:12])\n
\n
\n
\n\n

Outputs a list item within a string

\n
\nprint(f\"You have ordered {desserts[7]}\")\n
\n
\n
\n\n

To run the cell, press CTRL and Enter ⌨️

\n\n\n

", "metadata": {} }, { "id": "09d38f71-d6df-45e2-be5c-bc922241317a", "cell_type": "code", "source": "#Code Activity B\n# 0 1 2 3 4 5\nanimals = [\"Lion\",\"Tiger\",\"Bear\",\"Wolf\",\"Elephant\",\"Zebra\"]\n\n#====> Fix this code so it outputs Zebra\nprint(animals[?])\n\n#====> Write the correct code to output Lion\n\n\n#====> Write the correct code to output [\"Wolf\", \"Elephant\", \"Zebra\"]\n\n\n#====> Write the correct code to output [\"Tiger\", \"Bear\", \"Wolf\"]\n\n\n#====> Write code (using a list index) that outputs \"We have a Lion in our zoo\"\n\n\n#--------------------Questions (Start each comment with a hash tag---------------------\n#(1) What is the index number of the first item? How does the affect the others?\n\n\n#(2) What advantages can you think of for computer scientists structuring data in lists?\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": null }, { "id": "8fa663f0-45ed-4dae-abff-c29e283d1d0d", "cell_type": "markdown", "source": "
\n
\n
\n
\n
\n

🎓  (Modelling) Activity (C): Looping through a list\n\n
\n\n
\n

Your techer will demonstrate several key skills, you may code along with your teacher if you wish to do so

\n\n
    \n
  • Understanding what iteration means
  • \n
  • Understanding what controls iteration (e.g. a number or the number of items in a list)
  • \n
  • How to write a simple for loop
  • \n
  • Make the for loop iterate through each item of a list
  • \n \n
\n
", "metadata": {} }, { "id": "aeb05287-7b7c-4796-bd75-26e34227dc54", "cell_type": "code", "source": "# Modelling Activity C - Looping through a list \ndeserts = [\"Sahara\",\"Atacama\",\"Namib\",\"Gobi\",\"Kalahari\",\"antarctic\",\"arabian\"]\n\n\n\n\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": null }, { "id": "f505694b-f9e6-43bf-9b1c-73301e1fa6d8", "cell_type": "markdown", "source": "
\n
\n
\n
\n

⌨️  Activity (D): Iterating through a list\n\n
\n\n

Use what you have just learned to complete the activities below

\n

Here are some code snippets to help you with your code

\n\n

Creating a list

\n
\nlistylistlistlist = [\"item1\",\"item2\",\"item3\",\"item4\"]\n
\n
\n
\n\n

Iterating through a list

\n
\nfor item in listylistlistlist:\n    print(item)\n
\n
\n
\n\n\n

", "metadata": {} }, { "id": "d20376fd-4e2d-419e-b2fb-668252d979d0", "cell_type": "code", "source": "# Coding Activity D - Iterating through a list\n\n#====> Create a list that contains the planets of the solar system\n#====> Make sure they are in the correct order\n\n\n#====> Iterate through the list to output each planet\n\n\n#====> Ensure your code outputs each planet in a sentence \n#e.g. Earth is a planet in the solar system\n\n\n#---------------------------------Extension Activities--------------------------------------------\n# Complete them in the two code cells below\n# Remember to copy over your list into the cell\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": 7 }, { "id": "317e5a2e-fccd-4649-83d0-dc58df3009ac", "cell_type": "markdown", "source": "
\n
\n
\n
\n
\n

⌨️  Extension B.1: Inner Rocky / Outer Gassy

\n\n
\n\n

In this activity, you will ammend the code for activity B so that it outputs in two separate sections like so

\n\n
\n These are the inner, rocky planets:
\n -------------------------------------
\n Mercury
\n Venus
\n Earth
\n Mars
\n
\n These are the outer, gas giants:
\n -------------------------------------
\n Jupiter
\n Saturn
\n Uranus
\n Neptune
\n \n
\n
\n
\nHow to do it?\n
\n\n

With a for loop, you can also loop through a specific range of items

\n

As you have seen earlier, a range is up-to (but not including) the last item

\n

Even if the last item of a list is 5, you can put 6 in order to display item 5 (even though item 6 technically doesn't exist!)

\n

Here is a code snippet to help you with your code, this will print items 0, 1, 2 and 3 of the list

\n\n
\nfor each in range(0,4):\n    print(listitem[each])\n
\n
\n

Another massive hint!... You will need more than one for loop

\n\n
", "metadata": {} }, { "id": "d69520be-a0f1-4eeb-9e43-b7df752b9c2b", "cell_type": "code", "source": "#====> Extension B.1 Inner Rocky and Outer Gassy\n# Look at the Extension task I have put at the bottom of this notebook EXtension B.2\n\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": null }, { "id": "d490739c-c76f-4190-8d1d-fef1d689341e", "cell_type": "markdown", "source": "
\n
\n
\n\n
\n

⌨️  Extension B.2: Short Names/ Long Names

\n\n
\n\n

In this activity, you will ammend the code for activity B so that it outputs \"short name\" next to planets who's names are 5 characters or less and long name next to planets who's names are longer

\n\n
\n Short Name / Long Name:
\n -------------------------------------
\n Mercury has a long name
\n Venus has a short name
\n Earth has a short name
\n Mars has a short name
\n Jupiter has a long name
\n Saturn has a long name
\n Uranus has a long name
\n Neptune has a long name
\n \n
\n
\n
\nHow to do it?\n
\n\n

Using selection IF / ELSE and the len() function, you can check to see if a string is a certain length

\n

The code below checks to see if a string is smaller than 8 characters

\n

Obviously, as this is an extension task, you must adapt it to check a list item during each iteration!

\n\n
\nmystring = \"dodecahedron\"\n    \nif len(mystring) < 8:\n    print(f\"mystring has less than 8 characters!\")\nelse:\n    print(f\"mystring has 8 or more characters!\")\n
\n
\n
\n\n", "metadata": {} }, { "id": "1fccd7db-df66-49a8-acd4-7dc617abbf7f", "cell_type": "code", "source": "#====> Extension B.2 Short names / long names\n# Look at the Extension task I have put at the bottom of this notebook EXtension B.2\n\n\n\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": null } ] }