{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(lists)=\n", "# Lists\n", "``` {index} Lists (python)\n", "```\n", "A list is an object that contains multiple elements in defined order (called an **index** number). The syntax is:\n", "\n", "```\n", " l = [element0, element1, element2, element3, ...]\n", "```\n", " \n", "Note that indexing in python starts at 0! To extract a specific element or part of the list, we can use slicing syntax:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "[5, 9, 7, 11]\n", "[1, 5]\n", "\n" ] } ], "source": [ "l = [1, 3, 5, 9, 7, 11]\n", "\n", "# Print first element (index 0!)\n", "print(l[0])\n", "\n", "# Print elements three to six (not inclusive), returns a list\n", "print(l[2:6])\n", "\n", "# Print every second element but the last two excluded\n", "print(l[:-3:2])\n", "\n", "# Print what class l is\n", "print(type(l))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same operations can be done to lists of strings:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "potatoes\n", "['apples', 'bananas', 'milk', 'juice']\n", "['potatoes', 'apples']\n", "\n" ] } ], "source": [ "shopping_list = [\"potatoes\", \"cucumber\", \"apples\",\n", " \"bananas\", \"milk\", \"juice\"]\n", "\n", "# Print first element\n", "print(shopping_list[0])\n", "\n", "# Print elements three to six, returns a lis\n", "print(shopping_list[2:6])\n", "\n", "# Print every other elements but the last two excluded\n", "print(shopping_list[0:-3:2])\n", "\n", "# Returns class list\n", "print(type(shopping_list))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other operations on lists:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "[1, 3, 5, 9, 7, 11, 13]\n", "1\n", "13\n", "49\n", "[1, 3, 5, 9, 7, 11, 13, 17, 19]\n", "[1, 3, 305, 5, 9, 7, 11, 13, 17, 19]\n", "[1, 3, 5, 9, 7, 11, 13, 17, 19]\n", "5\n", "False\n" ] } ], "source": [ "l = [1, 3, 5, 9, 7, 11]\n", "\n", "print(len(l)) # Prints the number of elements in the list\n", "\n", "l.append(13) # Appends 13 at the end of the list\n", "print(l)\n", "\n", "print(min(l)) # Returns minimum value in the list\n", "print(max(l)) # Returns maximum value in the list\n", "\n", "print(sum(l)) # Sums up elements of the list\n", "\n", "l = l + [17, 19] # Adds a list [17, 19] at the end of l\n", "print(l)\n", "\n", "l.insert(2, 305) # Insert at index 2, number 305\n", "print(l)\n", "\n", "del l[2] # Delete the element we inserted at index 2\n", "print(l)\n", "\n", "print(l.index(11)) # Return what index value 11 has\n", "\n", "print(305 in l) # Check if 305 is in the list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(lists_exercises)=\n", "## Exercises\n", "-------\n", "\n", "* **Multi-type lists** compared to other programming languages Python is very flexible when it comes to types of objects inside them. For example, we can have the following list:\n", "\n", " l = [1,\"0\",True]\n", "\n", " Try to transform the above list into the following one using list operations such as `del` and `append`:\n", "\n", " l = [1,2,3]\n", "\n", " Print the largest element of the list.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{admonition} Answer\n", ":class: dropdown\n", "\n", "```python\n", "l = [1,\"0\",True]\n", "\n", "del l[2]\n", "del l[1]\n", "l.append(2)\n", "l.append(3)\n", "\n", "print(l)\n", "print(max(l))\n", "```\n", "\n", "````" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }