{ "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 - Spring 1 - Lesson 1: Revisiting Lists and For Loops
\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\nfruit = (\"Apple\", \"Pear\", \"Orange\", \"Banana\", \"Kiwi\")\n\nprint(f\"{fruit [1]} comes from New Zealand\")\n\nfor item in fruits\n print(item)\n\n#====> Describe the three errors and what you did to fix them\n#(1)\n#\n#(2)\n#\n#(3)\n#", "metadata": { "trusted": true }, "outputs": [], "execution_count": null }, { "id": "8acd9fa7-63cc-476c-9223-7f269838b81b", "cell_type": "markdown", "source": "
\n
\n
\n
\n
\n

🐍 Coding Activity (A): Python Lists Revisited

\n\n
\n\n
\n\n

In this activity, you must:

\n\n
    \n
  1. Create a Python list containing your top 5 music artists
  2. \n
  3. Output the first artist in the list
  4. \n
  5. Output the last three artists in the list (using a range)
  6. \n
  7. Output a sentence in this format (using an item from the list):
    \n \"David Bowie is the author of Starman\"\n
  8. \n
  9. Remove (pop) one artist from the list and then append a different artist
  10. \n
  11. Output (append) the list
  12. \n
\n\n
\nExample Code - Click to show / hide\n\n
\n# Create a list of zoo animals\nanimals = [\"Lion\", \"Elephant\", \"Giraffe\", \"Zebra\", \"Penguin\"]\n\n# Adds a new item to the end of the list\nanimals.append(\"Tiger\") \n\n# Outputs the 4th item\nprint(animals[3]) \n\n# Outputs the full list\nprint(animals) \n\n# Outputs the 2nd to the 3rd item\nprint(animals[1:3]) \n\n# Removes the 4th item of the list\nanimals.pop(3) \n\n# Outputs a list item in a sentence\nprint(animals[1] + \" is one of the animals in the zoo\")\n\n
\n\n
\n
\n", "metadata": {} }, { "id": "afd1d001-3bab-4c8b-a708-8f3f6bdf168e", "cell_type": "code", "source": "# 🐍 Coding Activity A - Python Lists Revisited\n\nartists = []\n\n#====> Output the first artist in the list\n\n\n#====> Output the last three artists in the list (using a range)\n\n\n#====> Output a sentence in the format given above\n\n\n#====> Remove (pop) one artist from the list\n\n\n#====> Append a new artist to the list\n\n\n#====> Output the list\n\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": 1 }, { "id": "93fd2f05-f9b1-436f-af0f-b2507cb42b3f", "cell_type": "markdown", "source": "
\n
\n
\n
\n
\n

🎓  Modelling - Coding Activity (B): For Loops With Range\n\n
\n\n

In the code below, your teacher will model how to use a for loop with a range

\n

They will demonstrate how the loop counter variable is a number that increases by 1 with each iteration

\n

They will then demonstrate how this can be used in order to output each item of a list on a separate line

\n\n

", "metadata": {} }, { "id": "09d38f71-d6df-45e2-be5c-bc922241317a", "cell_type": "code", "source": "# 🐍 Coding Activity B - For loops With Range\n# 0 1 2 3 4 5\nCities = [\"London\",\"Paris\",\"New York\",\"Tokyo\",\"Delhi\",\"Dubai\"]\n\n#====> Code along with your teacher in the space below\n\n\n\n\n\n#====> Then.....\n#====> Answer these questions\n# (1) What is the name we have used for the loop counter variable?\n#\n# (2) What happens to the loop counter variable with each iteration?\n#\n# (3) Explain how we are able to use this to output each item of the list?\n#\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": null }, { "id": "8fa663f0-45ed-4dae-abff-c29e283d1d0d", "cell_type": "markdown", "source": "
\n
\n
\n
\n
\n

🐍  Coding Activity (C): Using Range to Output Personalities\n\n
\n\n
\n

In the code below there is a list of 6 personalities

\n

You must complete the code so:

\n\n
    \n
  1. The (currently empty) list called names contains 6 names (your choice)
  2. \n
  3. A for loop that uses range() will run 6 times
  4. \n
  5. In each iteration of the for loop an f string is output that includes:
    ** A name from the names list
    ** A personality type from the personalities list.
    Both of these will match the value of the loop counter variable.\n
\n

The output for each person should look something like this\n

Alice is always very cheerful

\n
\n\n
\nExample Code - Click to show / hide\n\n
\n# Loop through a list of animals and output each as an f string\nanimals = [\"lion\", \"tiger\", \"bear\", \"zebra\", \"elephant\"]\n\nfor i in range(0,5):\n    print(f\"There is a {animals[i]} at our zoo\")\n\n
\n
", "metadata": {} }, { "id": "aeb05287-7b7c-4796-bd75-26e34227dc54", "cell_type": "code", "source": "# Coding Activity C - Using Range to Output Personalities\npersonalities = [\"Cheerful\", \"Helpful\", \"Optimisitc\", \"Smart\", \"Creative\", \"Grumpy\"]\nnames = []\n\n#====> Write your for loop below\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

🐍  Extension: Combining items from three lists

\n\n
\n\n

Use what you have just learned to output the Name, marks and grade for each student in the class

\n

The output for each student should be something like this

\n
Alex : 28 Marks : Grade 4
\n\n\n
", "metadata": {} }, { "id": "d20376fd-4e2d-419e-b2fb-668252d979d0", "cell_type": "code", "source": "# Extension: Combining items from three lists\n# List of student names\nnames = [\"Alex\", \"Priya\", \"James\", \"Sophie\", \"Liam\", \"Aisha\", \"Daniel\", \"Emily\", \"Noah\", \"Hannah\"]\n\n# Corresponding marks out of 60\nmarks = [28, 34, 41, 47, 52, 30, 45, 56, 38, 49]\n\n# Corresponding grades (4–9)\ngrades = [4, 5, 6, 7, 8, 4, 7, 9, 5, 8]\n\n#-----------------------------------Your Code Goes below --------------------------------\n\n#====> \n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": 7 } ] }