From 887f49fac73e92e824559cb9bff9d80adeafadf5 Mon Sep 17 00:00:00 2001 From: Dee-M123 Date: Wed, 5 Nov 2025 15:23:23 +0100 Subject: [PATCH] Created using Colab --- list_operations.ipynb | 1731 ++++++++++++++++++++++++----------------- 1 file changed, 1033 insertions(+), 698 deletions(-) diff --git a/list_operations.ipynb b/list_operations.ipynb index db2f739..221855d 100644 --- a/list_operations.ipynb +++ b/list_operations.ipynb @@ -1,702 +1,1037 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# LAB | Python List Operations\n", - "\n", - "This notebook provides an overview of various operations you can perform with Python lists, including accessing, modifying, and managing list items.\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Accessing List Items\n", - "To access an item in a list, use its index. Python uses zero-based indexing, meaning the first item is at index 0.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'banana', 'cherry']\n" - ] - } - ], - "source": [ - "my_list = ['apple', 'banana', 'cherry']\n", - "print(my_list) # Outputs: 'apple', 'banana', 'cherry'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 1\n", - "Given the following list, print the third item from the list." - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": {}, - "outputs": [], - "source": [ - "fruits = ['grape', 'kiwi', 'orange', 'mango', 'pear']\n", - "\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Changing List Items\n", - "You can change the value of a specific item by referring to its index." - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'orange', 'cherry']\n" - ] - } - ], - "source": [ - "my_list = ['apple', 'banana', 'cherry']\n", - "my_list[1] = 'orange'\n", - "print(my_list)# Outputs: ['apple', 'orange', 'cherry']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To change multiple items, specify a range:" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'kiwi', 'mango']\n" - ] - } - ], - "source": [ - "my_list[1:3] = ['kiwi', 'mango']\n", - "print(my_list) # Outputs: ['apple', 'kiwi', 'mango']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 2\n", - "Given the following list, change the second color to \"yellow\" and print the updated list." - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": {}, - "outputs": [], - "source": [ - "colors = ['red', 'blue', 'green']\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Replacing Values in a List\n", - "To replace values in a list, you can assign new values to specific indices or ranges." - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "grape\n" - ] - } - ], - "source": [ - "my_list = ['apple', 'banana', 'cherry']\n", - "my_list = 'grape'\n", - "print(my_list) # Outputs: ['grape', 'banana', 'cherry']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 3\n", - "Given the following list of numbers, replace the first number with 15 and print the updated list.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [], - "source": [ - "numbers = [10, 20, 30]\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "## Appending Items to a List\n", - "To add an item to the end of a list, use the `append()` method.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'banana', 'cherry']\n" - ] - } - ], - "source": [ - "my_list = ['apple', 'banana']\n", - "my_list.append('cherry')\n", - "print(my_list) # Outputs: ['apple', 'banana', 'cherry']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 4\n", - "Given the following list of vegetables, append \"spinach\" to your list and print the updated list.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": {}, - "outputs": [], - "source": [ - "vegetables = ['carrot', 'broccoli']\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "\n", - "## Inserting Items to a List\n", - "To insert an item at a specific index without replacing existing values, use the `insert()` method.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'orange', 'banana']\n" - ] - } - ], - "source": [ - "my_list = ['apple', 'banana']\n", - "my_list.insert(1, 'orange')\n", - "print(my_list) # Outputs: ['apple', 'orange', 'banana']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 5\n", - "Given the following list of animals, insert \"hamster\" at the second position and print the updated list." - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [], - "source": [ - "animals = ['dog', 'cat', 'rabbit']\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Extending Items to a List\n" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'banana', 'cherry', 'date']\n" - ] - } - ], - "source": [ - "my_list = ['apple', 'banana']\n", - "my_list.extend(['cherry', 'date'])\n", - "print(my_list) # Outputs: ['apple', 'banana', 'cherry', 'date']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "\n", - "### Exercise 6\n", - "Given two lists of numbers below, extend `list1` with `list2` and print the resulting list.\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": {}, - "outputs": [], - "source": [ - "list1 = [1, 2, 3]\n", - "list2 = [4, 5]\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Removing an Item from a List\n", - "To remove an item by value, use the `remove()` method. To remove an item by index, use `pop()`, which also returns the removed item." - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'cherry']\n", - "apple\n", - "['cherry']\n" - ] - } - ], - "source": [ - "my_list = ['apple', 'banana', 'cherry']\n", - "my_list.remove('banana')\n", - "print(my_list) # Outputs: ['apple', 'cherry']\n", - "\n", - "removed_item = my_list.pop(0)\n", - "print(removed_item) # Outputs: apple\n", - "print(my_list) # Outputs: ['cherry']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 7\n", - "Given the following list of colors, remove \"green\" by name and then remove another color by index. Print the updated list after each removal." - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [], - "source": [ - "colors = ['red', 'blue', 'green', 'yellow']\n", - "# Your code here for removing \"green\"\n", - "# Your code here for removing another color by index (e.g., index 0)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Clearing an Entire List\n", - "To remove all items from a list, use the `clear()` method." - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[]\n" - ] - } - ], - "source": [ - "my_list.clear()\n", - "print(my_list) # Outputs: []" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 8\n", - "Given the following list of numbers, clear your list and print it to confirm it's empty." - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": {}, - "outputs": [], - "source": [ - "numbers = [1, 2, 3, 4, 5]\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## List Length\n", - "To find out how many items are in a list, use the `len()` function." - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3\n" - ] - } - ], - "source": [ - "my_list = [1, 2, 3]\n", - "length_of_list = len(my_list)\n", - "print(length_of_list) # Outputs: 3" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 9\n", - "Given the following list of fruits, print its length." - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": {}, - "outputs": [], - "source": [ - "fruits = ['apple', 'banana', 'cherry', 'date']\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Sorting a List\n", - "To sort items in a list in ascending order (or descending), use the `sort()` method or `sorted()` function." - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3]\n" - ] - } - ], - "source": [ - "my_list = [3, 1, 2]\n", - "my_list.sort()\n", - "print(my_list) # Outputs: [1, 2, 3]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 10\n", - "Given the following unsorted list of numbers, sort it and print the result." - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": {}, - "outputs": [], - "source": [ - "numbers = [5, 3, 8, 6]\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## The range() Function \n", - "The `range()` function generates a sequence of numbers. It can be used to create lists of numbers easily." - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2, 3, 4]\n" - ] - } - ], - "source": [ - "number_range = list(range(5))\n", - "print(number_range) # Outputs: [0, 1, 2, 3, 4]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 11\n", - "Create a list of numbers from 0 to 9 using `range()`, then print it." - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "## Simple Statistics \n", - "You can calculate simple statistics like sum and average using built-in functions on lists." - ] - }, - { - "cell_type": "code", - "execution_count": 61, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "20.0\n" - ] - } - ], - "source": [ - "numbers = [10, 20, 30]\n", - "total_sum = sum(numbers)\n", - "average_value = total_sum / len(numbers)\n", - "print(average_value) # Outputs: 20.0" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 12\n", - "Given this list of scores below calculate and print their average:" - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": {}, - "outputs": [], - "source": [ - "scores = [85, 90, 78]\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Slicing a List \n", - "You can access parts of lists using slicing. " - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 3]\n" - ] + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "g2U5gqJtOKeN" + }, + "source": [ + "# LAB | Python List Operations\n", + "\n", + "This notebook provides an overview of various operations you can perform with Python lists, including accessing, modifying, and managing list items.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YqW8VjTVOKeP" + }, + "source": [ + "## Accessing List Items\n", + "To access an item in a list, use its index. Python uses zero-based indexing, meaning the first item is at index 0.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "cgSYSwVCOKeP", + "outputId": "3ae61f93-fb1f-4b82-b704-a2873b5f8f65" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['apple', 'banana', 'cherry']\n" + ] + } + ], + "source": [ + "my_list = ['apple', 'banana', 'cherry']\n", + "print(my_list) # Outputs: 'apple', 'banana', 'cherry'" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_a7juaV6OKeQ" + }, + "source": [ + "### Exercise 1\n", + "Given the following list, print the third item from the list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Zz8OGEXFOKeQ", + "outputId": "7289c6a4-c035-4483-de96-d193a41d42ef" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "orange\n" + ] + } + ], + "source": [ + "fruits = ['grape', 'kiwi', 'orange', 'mango', 'pear']\n", + "\n", + "print(fruits[2])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4RHJQNaBOKeR" + }, + "source": [ + "## Changing List Items\n", + "You can change the value of a specific item by referring to its index." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "X6jA01LmOKeR", + "outputId": "a3f10328-a804-48d0-f01e-7116594b8f77" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['apple', 'orange', 'cherry']\n" + ] + } + ], + "source": [ + "my_list = ['apple', 'banana', 'cherry']\n", + "my_list[1] = 'orange'\n", + "print(my_list)# Outputs: ['apple', 'orange', 'cherry']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "66zK2dK0OKeR" + }, + "source": [ + "To change multiple items, specify a range:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "lg8awZTYOKeS", + "outputId": "f68582bc-bdc8-41da-b13e-888240db03f2" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['apple', 'kiwi', 'mango']\n" + ] + } + ], + "source": [ + "my_list[1:3] = ['kiwi', 'mango']\n", + "print(my_list) # Outputs: ['apple', 'kiwi', 'mango']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0WKU_1oTOKeS" + }, + "source": [ + "### Exercise 2\n", + "Given the following list, change the second color to \"yellow\" and print the updated list." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KHVloI3VOKeS", + "outputId": "74e578cf-e3eb-4d61-f71c-7f4b78443321" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['red', 'yellow', 'green']\n" + ] + } + ], + "source": [ + "colors = ['red', 'blue', 'green']\n", + "colors[1] = 'yellow'\n", + "print(colors)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "W55fwIu0OKeS" + }, + "source": [ + "## Replacing Values in a List\n", + "To replace values in a list, you can assign new values to specific indices or ranges." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Xn2Ba-EnOKeT", + "outputId": "be2eef54-2df5-4623-f3d0-dd3a5589ad47" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "grape\n" + ] + } + ], + "source": [ + "my_list = ['apple', 'banana', 'cherry']\n", + "my_list = 'grape'\n", + "print(my_list) # Outputs: ['grape', 'banana', 'cherry']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "X1TxEYCjOKeT" + }, + "source": [ + "### Exercise 3\n", + "Given the following list of numbers, replace the first number with 15 and print the updated list.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ym7jZ2-YOKeT", + "outputId": "c49a6c4e-2db6-430f-d9a6-c794cbc4faed" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[15, 20, 30]\n" + ] + } + ], + "source": [ + "numbers = [10, 20, 30]\n", + "numbers[0] = 15\n", + "print(numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PDq4GjS-OKeT" + }, + "source": [ + "\n", + "## Appending Items to a List\n", + "To add an item to the end of a list, use the `append()` method.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Br7KAHFoOKeT", + "outputId": "58af8ccf-843f-4289-8b76-24ef3746d771" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['apple', 'banana', 'cherry']\n" + ] + } + ], + "source": [ + "my_list = ['apple', 'banana']\n", + "my_list.append('cherry')\n", + "print(my_list) # Outputs: ['apple', 'banana', 'cherry']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NgjhOxG7OKeT" + }, + "source": [ + "### Exercise 4\n", + "Given the following list of vegetables, append \"spinach\" to your list and print the updated list.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "m7qqwIJKOKeU", + "outputId": "b8ba2da5-7683-4909-aeb3-f1e5b1b66e36" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['carrot', 'broccoli', 'spinach']\n" + ] + } + ], + "source": [ + "vegetables = ['carrot', 'broccoli']\n", + "vegetables.append(\"spinach\")\n", + "print(vegetables)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4wIo2qm-OKeU" + }, + "source": [ + "\n", + "\n", + "## Inserting Items to a List\n", + "To insert an item at a specific index without replacing existing values, use the `insert()` method.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "GqUuFVrpOKeU", + "outputId": "dcf620af-87d1-471a-dd39-70d108a9c182" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['apple', 'orange', 'banana']\n" + ] + } + ], + "source": [ + "my_list = ['apple', 'banana']\n", + "my_list.insert(1, 'orange')\n", + "print(my_list) # Outputs: ['apple', 'orange', 'banana']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oTiVFIqKOKeU" + }, + "source": [ + "### Exercise 5\n", + "Given the following list of animals, insert \"hamster\" at the second position and print the updated list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ciHP33cqOKeU", + "outputId": "b3fbadc8-628b-419f-9d25-2d378b93ad95" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['dog', 'hamster', 'cat', 'rabbit']\n" + ] + } + ], + "source": [ + "animals = ['dog', 'cat', 'rabbit']\n", + "animals.insert(1, \"hamster\")\n", + "print(animals)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ux68VK6cOKeU" + }, + "source": [ + "## Extending Items to a List\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "YyhpHlPoOKeU", + "outputId": "3020dc93-1a1a-4f46-a868-00dc49917b90" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['apple', 'banana', 'cherry', 'date']\n" + ] + } + ], + "source": [ + "my_list = ['apple', 'banana']\n", + "my_list.extend(['cherry', 'date'])\n", + "print(my_list) # Outputs: ['apple', 'banana', 'cherry', 'date']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "P2llNAbvOKeU" + }, + "source": [ + "\n", + "\n", + "### Exercise 6\n", + "Given two lists of numbers below, extend `list1` with `list2` and print the resulting list.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "r4YYsu4yOKeU", + "outputId": "da127e11-d566-4a30-b14a-45be6b365933" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "list1 = [1, 2, 3]\n", + "list2 = [4, 5]\n", + "list1.extend(list2)\n", + "print(list1)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "t1fiF8YjOKeU" + }, + "source": [ + "## Removing an Item from a List\n", + "To remove an item by value, use the `remove()` method. To remove an item by index, use `pop()`, which also returns the removed item." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "M_4x1B8POKeU", + "outputId": "55c38d5e-781d-4fb0-83f9-9dca354194ae" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['apple', 'cherry']\n", + "apple\n", + "['cherry']\n" + ] + } + ], + "source": [ + "my_list = ['apple', 'banana', 'cherry']\n", + "my_list.remove('banana')\n", + "print(my_list) # Outputs: ['apple', 'cherry']\n", + "\n", + "removed_item = my_list.pop(0)\n", + "print(removed_item) # Outputs: apple\n", + "print(my_list) # Outputs: ['cherry']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_bL-tz55OKeV" + }, + "source": [ + "### Exercise 7\n", + "Given the following list of colors, remove \"green\" by name and then remove another color by index. Print the updated list after each removal." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KBhfE66zOKeV", + "outputId": "74f57001-10bb-427a-9a68-bbf0dae05844" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['red', 'blue', 'yellow']\n", + "yellow\n", + "['red', 'blue']\n" + ] + } + ], + "source": [ + "colors = ['red', 'blue', 'green', 'yellow']\n", + "colors.remove(\"green\")\n", + "print(colors)\n", + "\n", + "r_color = colors.pop(2)\n", + "print(r_color)\n", + "print(colors)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zthjS92BOKeV" + }, + "source": [ + "## Clearing an Entire List\n", + "To remove all items from a list, use the `clear()` method." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "3OcsBRB7OKeV", + "outputId": "4310fda0-7186-49f2-febc-307e0e4781b3" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], + "source": [ + "my_list.clear()\n", + "print(my_list) # Outputs: []" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "D5SIZZSWOKeV" + }, + "source": [ + "### Exercise 8\n", + "Given the following list of numbers, clear your list and print it to confirm it's empty." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "xQcs8fQFOKeV", + "outputId": "cc0de46f-f79e-4e6b-97e0-d2b698952cb4" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[]\n" + ] + } + ], + "source": [ + "numbers = [1, 2, 3, 4, 5]\n", + "numbers.clear()\n", + "print(numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nA99YPLvOKeV" + }, + "source": [ + "## List Length\n", + "To find out how many items are in a list, use the `len()` function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "iljQK8PTOKeV", + "outputId": "1dea7b56-dc41-4bce-9e57-00cb89916168" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "my_list = [1, 2, 3]\n", + "length_of_list = len(my_list)\n", + "print(length_of_list) # Outputs: 3" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VoqfHPIVOKeV" + }, + "source": [ + "### Exercise 9\n", + "Given the following list of fruits, print its length." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8Pt8gw6HOKeV", + "outputId": "ec32930a-cafe-4cc9-c5fc-153e5866432f" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "execution_count": 22 + } + ], + "source": [ + "fruits = ['apple', 'banana', 'cherry', 'date']\n", + "len(fruits)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JrRtMn9fOKec" + }, + "source": [ + "## Sorting a List\n", + "To sort items in a list in ascending order (or descending), use the `sort()` method or `sorted()` function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "pC6Ppq0SOKec", + "outputId": "4558c542-c462-477d-e9bf-2cef19404a8c" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n" + ] + } + ], + "source": [ + "my_list = [3, 1, 2]\n", + "my_list.sort()\n", + "print(my_list) # Outputs: [1, 2, 3]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YK2xzcePOKec" + }, + "source": [ + "### Exercise 10\n", + "Given the following unsorted list of numbers, sort it and print the result." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "1u5whOkCOKec", + "outputId": "8422c7d0-cd30-496e-8aa1-3cae028eeec6" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[3, 5, 6, 8]\n" + ] + } + ], + "source": [ + "numbers = [5, 3, 8, 6]\n", + "numbers.sort()\n", + "print(numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v0x_kveuOKec" + }, + "source": [ + "## The range() Function\n", + "The `range()` function generates a sequence of numbers. It can be used to create lists of numbers easily." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "hnNFLjTIOKec", + "outputId": "7365283d-8549-42fe-f923-91e439f8ab35" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4]\n" + ] + } + ], + "source": [ + "number_range = list(range(5))\n", + "print(number_range) # Outputs: [0, 1, 2, 3, 4]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eIs1qAm1OKec" + }, + "source": [ + "### Exercise 11\n", + "Create a list of numbers from 0 to 9 using `range()`, then print it." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Cgi_jGmXOKec", + "outputId": "b130ea22-7615-4a74-88dc-dc55d7e8e020" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], + "source": [ + "my_range = list(range(10))\n", + "print(my_range)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hlcgUWbROKec" + }, + "source": [ + "\n", + "## Simple Statistics\n", + "You can calculate simple statistics like sum and average using built-in functions on lists." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "IiXGZwtrOKed", + "outputId": "31069ca8-a00b-4801-c089-ca38d4fa17da" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20.0\n" + ] + } + ], + "source": [ + "numbers = [10, 20, 30]\n", + "total_sum = sum(numbers)\n", + "average_value = total_sum / len(numbers)\n", + "print(average_value) # Outputs: 20.0" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SR0G0Qb9OKed" + }, + "source": [ + "### Exercise 12\n", + "Given this list of scores below calculate and print their average:" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "iDRQrqedOKed", + "outputId": "69c5b04f-4425-4f58-b788-c92e1d4adbc4" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "84.33333333333333\n" + ] + } + ], + "source": [ + "scores = [85, 90, 78]\n", + "score_sum = sum(scores)\n", + "score_average = score_sum / len(scores)\n", + "print(score_average)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pMB_0hD8OKed" + }, + "source": [ + "## Slicing a List\n", + "You can access parts of lists using slicing." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Mth1kKfROKed", + "outputId": "12eeb91a-abf0-4be6-8a1c-b85b7ee470ce" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3]\n" + ] + } + ], + "source": [ + "my_list = [1, 2, 3, 4]\n", + "sliced_part = my_list[1:3]\n", + "print(sliced_part) # Outputs: [2, 3]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "muZEQ01WOKed" + }, + "source": [ + "### Exercise 13\n", + "Using slicing on this given list below extract and print elements from index position one to three:" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sS9ZQlJvOKed", + "outputId": "82662ef1-809c-4cdd-cda6-6b7af8e57505" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['b', 'c', 'd']\n" + ] + } + ], + "source": [ + "items = ['a', 'b', 'c', 'd']\n", + "print(items[1:99])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yhG2SaS8OKed" + }, + "source": [ + "## Copying a List\n", + "To create a copy of a list without referencing it directly (to avoid changes affecting both), use slicing or `copy()` method." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "g4vl9WulOKed", + "outputId": "43e8ef5c-af43-4aeb-d38e-4fcc236d993d" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n" + ] + } + ], + "source": [ + "original_list = [1 ,2 ,3]\n", + "copied_list = original_list[:]\n", + "print(copied_list) # Outputs: [1 ,2 ,3]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "x3rB9OVBOKed" + }, + "source": [ + "### Exercise 14\n", + "Copy this given list below into another variable and print both lists:" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "tYxwT1CHOKed", + "outputId": "16d190b0-97bb-4c23-8772-4ffc1bb514b6" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[10, 20, 30]\n" + ] + } + ], + "source": [ + "original_items = [10 ,20 ,30]\n", + "new_items = original_items[:]\n", + "print(new_items)" + ] } - ], - "source": [ - "my_list = [1, 2, 3, 4]\n", - "sliced_part = my_list[1:3]\n", - "print(sliced_part) # Outputs: [2, 3]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 13\n", - "Using slicing on this given list below extract and print elements from index position one to three:" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": {}, - "outputs": [], - "source": [ - "items = ['a', 'b', 'c', 'd']\n", - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Copying a List\n", - "To create a copy of a list without referencing it directly (to avoid changes affecting both), use slicing or `copy()` method." - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3]\n" - ] + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.0" + }, + "colab": { + "provenance": [], + "include_colab_link": true } - ], - "source": [ - "original_list = [1 ,2 ,3]\n", - "copied_list = original_list[:]\n", - "print(copied_list) # Outputs: [1 ,2 ,3]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 14\n", - "Copy this given list below into another variable and print both lists:" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "metadata": {}, - "outputs": [], - "source": [ - "original_items = [10 ,20 ,30]\n", - "# Your code here" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.0" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file