{ "cells": [ { "cell_type": "markdown", "id": "a19e7d26", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# DS2500 Lesson 1\n", "\n", "Jan 13, 2022\n", "\n", "## Content\n", "- Installing packages: pip\n", "- Python types \n", " - simple:\n", " - boolean\n", " - ints \n", " - floats \n", " - strings\n", " - ordered sequences:\n", " - list\n", " - tuple\n", " - mapping:\n", " - dictionary\n", "- Operators (Arithmetic & Logical)\n", "- Flow Control (If-statments)\n", "\n", "++ denotes a nuance not necessary for DS2500, but helpful to point out as we (re)welcome everyone to python!\n" ] }, { "cell_type": "markdown", "id": "f0f0d689", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Installing python packages in Jupyter\n", "\n", "`pip` is the official python package installer. It installs python packages from [pypi.org](https://pypi.org/) with the terminal command:\n", "\n", " pip3 install \n", " \n", "(Note: use `pip3` on linux/mac and `pip` on windows immediately above)\n", "\n", "Jupyter notebooks allow you run type a terminal command by prefixing a code line with `!`:\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "bcc34875", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Requirement already satisfied: numpy in /home/matt/.local/lib/python3.10/site-packages (1.24.1)\n" ] } ], "source": [ "# Taken together, you can install a python package right in jupyter!\n", "!pip3 install numpy\n" ] }, { "cell_type": "markdown", "id": "0f19e119", "metadata": {}, "source": [ "### Not working for you?\n", "- try a more explicit way of calling pip:\n", " - `python3 -m pip` instead of `pip3` on mac/linux\n", " - `python -m pip` instead of `pip` on windows\n", " \n", "still running into trouble? Reach out on piazza please\n" ] }, { "cell_type": "markdown", "id": "efeafb73", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Python variable names\n", "\n", "- can have letters, digits and underscores but may not begin with a digit\n", "- python is case sensitive \n", " - example `a` and `A` are seperate variables\n", "\n", "\n", "\n", "- conventions:\n", " - variables should be all lower case\n", " - use underscores to seperate words\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "37cf3918", "metadata": {}, "outputs": [], "source": [ "# string (convention: use single quotes, double quotes work too though)\n", "some_string = 'a'\n", "some_other_string = \"a\"\n", "\n", "# integer\n", "some_int = 3\n", "\n", "# float\n", "some_float = 1.2345\n", "\n", "# bool\n", "sunny_day = True\n" ] }, { "cell_type": "markdown", "id": "c1773ad6", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Arithmetic Operators\n", "| Python operation | Arithmetic operator | Python expression\n", "| :-------- | :-------- | :-------- \n", "| Addition | `+` | `f + 7` \n", "| Subtraction | `–` | `p - c` \n", "| Multiplication | `*` | `b * m` \n", "| Exponentiation | `**` | `x ** y` \n", "| True division | `/` | `x / y` \n", "| Floor division | `//` | `x // y` \n", "| Remainder (modulo) | `%` | `r % s` \n", "\n", "* [All operators and their precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "4bd78ade", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 11\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "b06c2553", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 3\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "3d0cabac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# arithmetic operators are defined for some non-numbers too\n", "# like strings\n", "'a' + 'b'" ] }, { "cell_type": "markdown", "id": "bdf9323e", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Floor division & Modulo Operator\n", "\n", "- floor division `a // b`:\n", " - divide a by b, round down to the nearest integer\n", "- modulo operation `a % b`:\n", " - the \"remainder\" after dividing a by b (using only integers)\n", " - admittedly odd at first look, surpisingly useful!\n" ] }, { "cell_type": "markdown", "id": "5b8ad6a1", "metadata": {}, "source": [ "### example0:\n", "\n", "- 9 divided by 4 = 2 remainder 1 (since 9 = 4 * 2 + 1)\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "1ec9b399", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 // 4\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "de49780c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 % 2\n" ] }, { "cell_type": "markdown", "id": "b0a369a8", "metadata": {}, "source": [ "### example1: \n", "10 divide by 2 = 5 remainder 0 (since 10 = 5 * 2 + 0)\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "e96f0dfb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 // 2\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "8f87faf0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 % 2\n" ] }, { "cell_type": "markdown", "id": "e371e80c", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Logical Operators\n", "\n", "Logical operations operate on one (or more) inputs and return a boolean. \n", "\n", "Think of them like a yes/no question you can ask of your variables:\n", "\n", "Algebraic operator | Python operator | Sample condition | Meaning \n", ":---- | :---- | :---- | :----\n", "> | `>` | `x > y` | `x` is greater than `y`\n", "< | `<` | `x < y` | `x` is less than `y`\n", "≥ | `>=` | `x >= y` | `x` is greater than or equal to `y`\n", "≤ | `<=` | `x <= y` | `x` is less than or equal to `y`\n", "= | `==` | `x == y` | `x` is equal to `y`\n", "≠ | `!=` | `x != y` | `x` is not equal to `y`\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "f073a57b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3\n", "b = 10\n", "\n", "a != b\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "9a69f1de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# python \"syntactic sugar\": evaluating if a number is in a range\n", "x = 200\n", "\n", "100 <= x < 200\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "0fc5af70", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'<' not supported between instances of 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[15], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# # produces TypeError: objects must be compare-able\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;241;43m10\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m<\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\n", "\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'int' and 'str'" ] } ], "source": [ "# # produces TypeError: objects must be compare-able\n", "# 10 < 'a'\n" ] }, { "cell_type": "code", "execution_count": 17, "id": "8bb5e804", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# python bad habit, don't use \"is\" to compare objects \n", "# (it sometimes works, which makes it a subtle error to catch)\n", "\n", "# works for strings\n", "x = 'a'\n", "y = 'b'\n", "x is y\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "3a259edb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# doesn't work for floats\n", "x = 1.23\n", "y = 1.23\n", "x is y\n" ] }, { "cell_type": "code", "execution_count": 19, "id": "7f386a42", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x == y" ] }, { "cell_type": "markdown", "id": "9ef6f303", "metadata": {}, "source": [ "`is` tests if the variables point to the same object (more on this during Object Orient Programming in a few weeks) \n", "\n", "++[Strings](https://en.wikipedia.org/wiki/String_literal) behave a bit differently.\n" ] }, { "cell_type": "markdown", "id": "db6c8a88", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Type Casting (converting between variable types)\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "11e76fa9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# `type()` returns the object type\n", "a = 1\n", "type(a)\n" ] }, { "cell_type": "code", "execution_count": 21, "id": "aa774428", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# the line below ensures a = 1 and a is a float\n", "a = float(1)\n", "\n", "type(a)\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "49453022", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = float('3')\n", "type(a)\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "a324ec57", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 1.5" ] }, { "cell_type": "code", "execution_count": 27, "id": "2856bab5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(some_int)" ] }, { "cell_type": "code", "execution_count": 25, "id": "9674f686", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# notice: implicit type casting between ints and floats\n", "some_int = 3\n", "some_float = 3.141\n", "some_sum = some_int + some_float\n", "\n", "type(some_sum)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1bbfe677", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "83ea1189", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Type casting Booleans\n", "\n", "ints / floats which do not equal 0 are cast to True, otherwise False:\n" ] }, { "cell_type": "code", "execution_count": 28, "id": "36028575", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(123.123)\n" ] }, { "cell_type": "code", "execution_count": 29, "id": "a7714bd6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# integer 0\n", "bool(0)\n" ] }, { "cell_type": "code", "execution_count": 30, "id": "749a1baa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# float 0\n", "bool(0.0)\n" ] }, { "cell_type": "markdown", "id": "f7302525", "metadata": {}, "source": [ "strings which are not empty are cast to True, otherwise False:\n" ] }, { "cell_type": "code", "execution_count": 31, "id": "04786499", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# non empty string\n", "bool('asdf')\n" ] }, { "cell_type": "code", "execution_count": 32, "id": "a82dba23", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# empty string (string of length 0)\n", "bool('')\n" ] }, { "cell_type": "markdown", "id": "b816a7d2", "metadata": {}, "source": [ "though we haven't introduced them just yet, the \"non-empty -> True\" works elsewhere too\n", "\n", "dictionaries, tuples and sets which are not empty are cast to True, otherwise False:\n" ] }, { "cell_type": "code", "execution_count": 33, "id": "c9de2a35", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(['this', 'is', 'a', 'list', 'of', 'strings'])\n" ] }, { "cell_type": "code", "execution_count": 34, "id": "10f9bade", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# an empty list\n", "bool([])\n" ] }, { "cell_type": "markdown", "id": "f536f16d", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Control Flow (if blocks)\n", "\n", "\n", " if :\n", " \n", " elif :\n", " \n", " else:\n", " \n", "\n" ] }, { "cell_type": "code", "execution_count": 38, "id": "89305a20", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "youve earned an a\n" ] } ], "source": [ "grade = 94\n", "if grade >= 93:\n", " print('youve earned an a')\n", "elif grade >= 90:\n", " # elif -> \"else if\"\n", " print('youve earned an a-')\n", " print('here is another line of code in the elif block')\n", "else:\n", " print('some other grade')\n" ] }, { "cell_type": "markdown", "id": "1cc0dba2", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## booleans operators (and, or, not)\n" ] }, { "cell_type": "code", "execution_count": 41, "id": "7f3fcd33", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 44, "id": "c97512b7", "metadata": {}, "outputs": [], "source": [ "grade = 50\n", "name = 'aifdsuasidufh'\n", "if grade < 93 and name == 'matt':\n", " print('youd think the instructor could get an a in their own class ...')\n" ] }, { "cell_type": "markdown", "id": "1e53d90d", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# `input()`\n", "\n", "- `input()` asks for input and stores it as a string\n", "- input arguement to `input()` is a string which is displayed to user\n" ] }, { "cell_type": "code", "execution_count": 53, "id": "e8a3cade", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_variable" ] }, { "cell_type": "code", "execution_count": 52, "id": "c7697fd4", "metadata": {}, "outputs": [], "source": [ "some_variable = 100" ] }, { "cell_type": "code", "execution_count": 47, "id": "b00d1088", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(favorite_number)" ] }, { "cell_type": "code", "execution_count": 50, "id": "84e2efa6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "please input your favorite number:my favorite number is 5!\n" ] }, { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'my favorite number is 5!'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[50], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m## Getting input from user\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m favorite_number \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mplease input your favorite number:\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(favorite_number)\n", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'my favorite number is 5!'" ] } ], "source": [ "## Getting input from user\n", "favorite_number = input('please input your favorite number:')\n", "print(favorite_number)\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "02536274", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# In Class Activity A:\n", "- get users numerical grade (0 to 100)\n", " - `input()`\n", "- print a message which tells the user their letter grade in the course (not the +/-)\n", "- (++) include the +/- on the grade, make an effort to avoid repetitive code\n", " - hint0: there are a few different implementations, are sure yours is simplest?\n", " - hint1: modulo operator\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d5102304", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d120ba27", "metadata": {}, "outputs": [], "source": [ "x = 30" ] }, { "cell_type": "markdown", "id": "7cfff974", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Lists\n", "A python **list** is:\n", "- an ordered sequence of objects\n", "- **mutable** (mutable objects can be modified, immutable objects may not)\n", "- ++ implemented as a [dynamic array](https://en.wikipedia.org/wiki/Linked_list#Linked_lists_vs._dynamic_arrays)\n", "\n", "see [python doc](https://docs.python.org/3/tutorial/datastructures.html) for more detail\n", "\n", "### Indexing a python list (more to come on this next lesson)\n", "\n", "![](https://i.ibb.co/JK1VHpy/list1.png)\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "122aef9b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-45, 6, 0, 72, 1543]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# how to make a list\n", "some_list = [-45, 6, 0, 72, 1543]\n", "some_list" ] }, { "cell_type": "code", "execution_count": 3, "id": "671a4000", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# addressing an element (note: we start indexing at 0)\n", "some_list[1]" ] }, { "cell_type": "code", "execution_count": 4, "id": "b03a3e07", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# finding index of items equal to 72\n", "some_list.index(72)" ] }, { "cell_type": "code", "execution_count": 5, "id": "29f5837d", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "'are you in there?' is not in list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[5], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# what happens if the item isn't in the list?\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43msome_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mare you in there?\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mValueError\u001b[0m: 'are you in there?' is not in list" ] } ], "source": [ "# what happens if the item isn't in the list?\n", "some_list.index('are you in there?')" ] }, { "cell_type": "code", "execution_count": 9, "id": "f0b699fa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# how should we check if an item is in a list?\n", "1928392183791872 in some_list\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "cc89d63b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# note that we find index of the first occurance\n", "# (there are two `c` entries, but we only get index of first one)\n", "some_other_list = ['a', 'b', 'c', 'c', 'd']\n", "some_other_list.index('c')\n" ] }, { "cell_type": "markdown", "id": "d363f944", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Modifying a list\n", "\n", "Because a list is **mutable**, we may modify the list itself:\n", "- directly assign an item by its index\n", " - (e.g. swap the item at index 2 for value 20: `some_list[2] = 20`)\n", "- `append()` items at the end of the list\n", "- `insert()` items into arbitrary locations in the list\n", "- `pop()` an index position's item out of a list, removing it from the list\n", "\n", "++ there are other useful ones too, [see the official documentation on lists](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)\n", "\n", "![](https://i.ibb.co/JK1VHpy/list1.png)\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "7504648a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-45, 6, 0, 72, 1543]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# make the list in the example above\n", "some_list = [-45, 6, 0, 72, 1543]\n", "some_list\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "5689c9e3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-45, 'a new item at position 1!', 0, 72, 1543]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# directly swapping out the item at a particular index position for another\n", "some_list[1] = 'a new item at position 1!'\n", "some_list\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "bfa079d6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-45, 'a new item at position 1!', 0, 72, 1543, 'asdf', 'asdf', 'asdf']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# append a single item to the end of a list\n", "some_list.append('asdf')\n", "some_list\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "a1f81e58", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a new beginning',\n", " -45,\n", " 'a new item at position 1!',\n", " 0,\n", " 72,\n", " 1543,\n", " 'asdf',\n", " 'asdf',\n", " 'asdf']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# insert a single item to an arbitrary location of a list\n", "# note: we push list at, and including index, to the right to make room\n", "some_list.insert(0, 'a new beginning')\n", "some_list\n" ] }, { "cell_type": "code", "execution_count": 17, "id": "9b6c022a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a new beginning',\n", " -45,\n", " 'a new item at position 1!',\n", " 'a middle child',\n", " 0,\n", " 72,\n", " 1543,\n", " 'asdf',\n", " 'asdf',\n", " 'asdf']" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# insert a single item to an arbitrary location of a list\n", "some_list.insert(3, 'a middle child')\n", "some_list\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "bc591d3e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a middle child'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# popping a single item out of the list\n", "third_item = some_list.pop(3)\n", "third_item\n" ] }, { "cell_type": "code", "execution_count": 19, "id": "c2af8806", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a new beginning',\n", " -45,\n", " 'a new item at position 1!',\n", " 0,\n", " 72,\n", " 1543,\n", " 'asdf',\n", " 'asdf',\n", " 'asdf']" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# notice that after popping, item at that index (3 above) is removed from the list\n", "some_list\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9dce5551", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "id": "9114c9c9", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### \"Arithmetic\" on lists:\n", "- \"multiplication\" by int: repeat list some number of times\n", "- \"addition\" with another list: join two lists together\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "812b08ce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['e', 'c', 'h', 'o', ' ']" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# cast a string to a list\n", "my_list = list('echo ')\n", "my_list\n" ] }, { "cell_type": "code", "execution_count": 21, "id": "341cb0a5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['e', 'c', 'h', 'o', ' ', 'e', 'c', 'h', 'o', ' ', 'e', 'c', 'h', 'o', ' ']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiplying a list by an integer\n", "my_list * 3\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "aff1911e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['e', 'c', 'h', 'o', ' ', 'a', 'b', 'c']" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# adding two lists together (unlike typical addition, order matters!)\n", "another_list = list('abc')\n", "another_list\n", "my_list + another_list\n" ] }, { "cell_type": "markdown", "id": "a0799afa", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Helpful list functions (min, max, len, sorted)\n" ] }, { "cell_type": "code", "execution_count": 24, "id": "d5a104fb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 11, -10, 4, -100]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list_of_ints = [2, 11, -10, 4, -100]\n", "list_of_ints" ] }, { "cell_type": "code", "execution_count": 25, "id": "5074c094", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-100" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(list_of_ints)\n" ] }, { "cell_type": "code", "execution_count": 26, "id": "c3329947", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(list_of_ints)\n" ] }, { "cell_type": "code", "execution_count": 27, "id": "a13d4529", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# number of elements in the list\n", "len(list_of_ints)\n" ] }, { "cell_type": "code", "execution_count": 28, "id": "3961b517", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-100, -10, 2, 4, 11]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# return a sorted copy of the list (increasing order)\n", "sorted_list_of_ints = sorted(list_of_ints)\n", "sorted_list_of_ints\n" ] }, { "cell_type": "code", "execution_count": 29, "id": "c73dd457", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a' < 'b'" ] }, { "cell_type": "code", "execution_count": 30, "id": "5353a517", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc', 'abe', 'bert', 'cat']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sorted will compare all items in list to each other\n", "# (if items are strings, sorting is alpha ordering)\n", "sorted(['abe', 'abc', 'cat', 'bert'])\n" ] }, { "cell_type": "code", "execution_count": 31, "id": "f92a1f5d", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'<' not supported between instances of 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[31], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# all items in list must be able to be compared (this line raises error)\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;43msorted\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mc\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mb\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'int' and 'str'" ] } ], "source": [ "# all items in list must be able to be compared (this line raises error)\n", "sorted(['a', 'c', 'b', 3, 1, 2])\n" ] }, { "cell_type": "markdown", "id": "7f7ec8ff", "metadata": {}, "source": [ "You can also sort in [reverse](https://docs.python.org/3/library/functions.html#sorted) (decreasing order) by using the `reverse` parameter\n" ] }, { "cell_type": "code", "execution_count": 32, "id": "bfba9bee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[11, 4, 2, -10, -100]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted_list_of_ints = sorted(list_of_ints, reverse=True)\n", "sorted_list_of_ints\n" ] }, { "cell_type": "markdown", "id": "6d77b6dc", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# In class activity B (Lists & Strings):\n", "1. Replace the `# work here` comment cell with code which asks the user to `input()` a lowercase name and then `.append()`s it to all the relevant lists:\n", " - `list_short`\n", " - length of name is less than 5 chars\n", " - `list_alpha`\n", " - characters of name are in alpha order\n", " - e.g. `bert` since `b` comes before `e` comes before `r` comes before `t`\n", " - test if a name is alpha sorted via `list(name) == sorted(name)`\n", " - `list_all`\n", " - every name\n", " \n", " lmnopqrstuvwxyz\n", "2. Once complete, run this cell many times to test the following set of names ('abc', 'abcdefg', 'zy', 'zyxwvut'), run the `# observe each list...` cell and observe its output to confirm your code works properly.\n", " \n", "(++) Use a dictionary in place of the 3x list implementation. Note that we'll cover dictionaries shortly, we haven't introduced them just yet. Be sure to build the 3x list implementation above first, then copy paste your work and modify for this stretch goal.\n" ] }, { "cell_type": "code", "execution_count": 60, "id": "40fae897", "metadata": {}, "outputs": [], "source": [ "# initialize dict, keys are categories, values are lists\n", "name_dict = {'all': [],\n", " 'short': list(),\n", " 'alpha': list()}\n" ] }, { "cell_type": "code", "execution_count": 65, "id": "17500f7e", "metadata": {}, "outputs": [], "source": [ "# input name and append it to relevant lists\n", "# work here\n", "\n", "name = 'aa'\n", "\n", "# appends name to list of alpha names (if appropriate)\n", "if list(name) == sorted(name):\n", " name_dict['alpha'].append(name)\n", "\n", "# appends name to list of short names (if appropriate)\n", "if len(name) < 5:\n", " name_dict['short'].append(name)\n", "\n", "# appends name to list of all names\n", " name_dict['all'].append(name)\n" ] }, { "cell_type": "code", "execution_count": 66, "id": "085e271f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "short names:\n", "['abc', 'aa']\n", "alpha names:\n", "['abc', 'abcdefghi', 'aa']\n", "all names:\n", "['abc', 'abcdefghi', 'apodsfiuaspdofhusadpifuhsadpifuhasdpiufh', 'aa']\n" ] } ], "source": [ "# observe each list (there are cleaner ways to do this ... we'll get there!)\n", "name_dict\n" ] }, { "cell_type": "code", "execution_count": null, "id": "309c40d6", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d28730f3", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "id": "28c448e7", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Tuples\n", "A tuple is\n", "- an ordered, immutable sequence of objects\n", "\n", "(Remember, a list is an ordered, mutable sequence of objects)\n", "\n", "### Mutable\n", "- describes an object which can be changed\n", "\n", "### Immutable\n", "- describes an object which, one created, cannot be changed \n" ] }, { "cell_type": "code", "execution_count": 67, "id": "ec6c9642", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('a', 1, 'asdf')" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_tuple = ('a', 1, 'asdf')\n", "a_tuple\n" ] }, { "cell_type": "code", "execution_count": 68, "id": "72e748e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('a', 1, 'asdf')" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# careful with the tuple constructor (it takes a single input which is a tuple)\n", "a_tuple = tuple(('a', 1, 'asdf'))\n", "a_tuple\n" ] }, { "cell_type": "code", "execution_count": 69, "id": "f2d2d826", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('a', 1, 'asdf')" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# you can make a tuple without including the parenthases (this is most common in the code I've seen)\n", "a_tuple = 'a', 1, 'asdf'\n", "a_tuple\n" ] }, { "cell_type": "code", "execution_count": 70, "id": "d055929c", "metadata": {}, "outputs": [], "source": [ "beatles_tuple = 'john', 'ringo', 'paul', 'george'\n" ] }, { "cell_type": "code", "execution_count": 71, "id": "0a0506dd", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[71], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# # tuples are immutable, you can't change which items are inside them \u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;66;03m# # (all methods from \"modifying a list\" above won't work)\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[43mbeatles_tuple\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124myoko ono\u001b[39m\u001b[38;5;124m'\u001b[39m\n", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "# # tuples are immutable, you can't change which items are inside them \n", "# # (all methods from \"modifying a list\" above won't work)\n", "beatles_tuple[0] = 'yoko ono'\n" ] }, { "cell_type": "markdown", "id": "127271cd", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Dictionary\n", "python dictionary\n", "- unordered collection which stores key-value pairs \n", "- mutable\n", "\n", "#### A good first intuition of pyton dictionaries: a phone book\n", "\n", "\n", "\n", "- you can lookup phone number if you know a name\n", "- matches name-phone number pairs (key=name, value=phone number)\n", "- metaphor breakdown: phone books are sorted by name ... python dictionaries are not ordered!\n" ] }, { "cell_type": "code", "execution_count": 74, "id": "8cc965c6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'matt': 7, 'riva': 7, 'eli': 3}" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "favorite_number_dict = {'matt': 7, 'riva': 7, 'eli': 3}\n", "favorite_number_dict\n" ] }, { "cell_type": "code", "execution_count": 75, "id": "448996a8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# use square brackets to \"lookup\" (below we get the value associated with key 'matt')\n", "favorite_number_dict['matt']\n" ] }, { "cell_type": "code", "execution_count": 76, "id": "676e3fba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'matt': 7, 'riva': 7, 'eli': 3, 'zeke': 9999}" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# dictionaries are mutable, we can add key value pairs\n", "favorite_number_dict['zeke'] = 9999\n", "favorite_number_dict\n" ] }, { "cell_type": "code", "execution_count": 77, "id": "5151121b", "metadata": {}, "outputs": [], "source": [ "favorite_number_dict['matt'] = 'asdfhasdufh'\n" ] }, { "cell_type": "code", "execution_count": 78, "id": "b956b39e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'matt': 'asdfhasdufh', 'riva': 7, 'eli': 3, 'zeke': 9999}" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "favorite_number_dict\n" ] }, { "cell_type": "code", "execution_count": 79, "id": "0d4a4639", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'matt': 7, 'riva': 7, 'eli': 3}" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "favorite_number_dict = {'matt': 7, 'riva': 7, 'eli': 3}\n", "favorite_number_dict\n" ] }, { "cell_type": "code", "execution_count": 80, "id": "59fc1093", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'matt': 42, 'riva': 7, 'eli': 3, 'fleck': 3.14159}" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# you can update one dict into another\n", "favorite_number_dict2 = {'matt': 42, 'fleck': 3.14159}\n", "\n", "# add (overwrite if need be) key, value pairs from favorite_number_dict2 to favorite_number_dict\n", "favorite_number_dict.update(favorite_number_dict2)\n", "\n", "# notice that value associated with key 'matt' was overwritten\n", "favorite_number_dict\n" ] }, { "cell_type": "code", "execution_count": 81, "id": "1bdd4bb8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'riva': 7, 'eli': 3, 'fleck': 3.14159}" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# removing a key, value from a dictionary (by key)\n", "del favorite_number_dict['matt']\n", "favorite_number_dict\n" ] }, { "cell_type": "code", "execution_count": 82, "id": "6aed2e38", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['riva', 'eli', 'fleck'])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# keys() returns all the keys of the dict\n", "favorite_number_dict.keys()\n" ] }, { "cell_type": "code", "execution_count": 83, "id": "38a9c2ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values([7, 3, 3.14159])" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# values() returns all the values of the dict\n", "favorite_number_dict.values()\n" ] }, { "cell_type": "code", "execution_count": 84, "id": "6c9c824e", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "dict_items([('riva', 7), ('eli', 3), ('fleck', 3.14159)])" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# items() returns (key, value) tuples of all the pairs in the dict\n", "favorite_number_dict.items()\n" ] }, { "cell_type": "code", "execution_count": 85, "id": "8bd35768", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in favorite_number_dict.values()\n" ] }, { "cell_type": "code", "execution_count": 86, "id": "772f5c86", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# testing if key is in the dictionary\n", "'riva' in favorite_number_dict\n" ] }, { "cell_type": "markdown", "id": "13d76f13", "metadata": {}, "source": [ "# In Class Activity C\n", "\n", "Repeat ICA B, but use a dictionary in place of 3x lists. Your dictionary should have keys: \n", "- 'all'\n", "- 'short' \n", "- 'alpha'\n", "\n", "The values associated with each key are lists of all the names which meet the criteria. For example, if you were to run the given test case names: `'abc', 'abcdefg', 'zy', 'zyxwvut'` your code should build:\n", "\n", "```python\n", "name_dict = {'all': ['abc', 'abcdefg', 'zy', 'zyxwvut'],\n", " 'short': ['abc', 'zy'],\n", " 'alpha': ['abc', 'abcdefg']}\n", "```\n", "\n", "Please be sure to copy your work from above intead of replacing it so you can submit ICA B.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b2265609", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }