{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Structures, Functions, and Files\n", "tuples, lists, dicts, and sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuple\n", "* A tuple is a 1-d fixed-lenght, immutable sequence of Python objects\n", "* use `()` brackets or 'tuple'" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((1, 2, 3, 4, 5, 6), (7, 8, 9))" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tup = ((1, 2, 3, 4, 5, 6), (7, 8, 9))\n", "tup" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list = [2, 4, 6]\n", "type(my_list)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "(2, 4, 6)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tup2 = tuple(my_list)\n", "tup2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_string = \"Hello, world!\"\n", "type(my_string)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tup3 = tuple(my_string)\n", "tup3" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('H', 'e', 'l', 'l', 'o')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tup3[0:5]" ] }, { "cell_type": "code", "execution_count": 9, "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)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# tuples are immutable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtup3\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"h\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "# tuples are immutable\n", "tup3[0] = \"h\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('foo', (2, 4, 6), range(0, 5))" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tup4 = tuple([\"foo\", (2, 4, 6), range(5)])\n", "tup4" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'append'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtup4\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtup4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } ], "source": [ "tup4[1].append(3)\n", "tup4" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tup4.count(\"foo\")" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 2, 3), (4, 5, 6), (7, 8, 9)]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]\n", "seq" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = 1, b = 2, c = 3\n", "a = 4, b = 5, c = 6\n", "a = 7, b = 8, c = 9\n" ] } ], "source": [ "for a, b, c in seq:\n", " print(\"a = {0}, b = {1}, c = {2}\".format(a, b, c))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((7, 8, 9), range(0, 20))" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values = ((a, b, c), (range(20)))\n", "values" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "y, *rest = values" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(7, 8, 9)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[range(0, 20)]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List\n", "* Defined using `[]` brackets" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "type(my_list)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, [10, 11, 12, 14, 15]]" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_list = [10, 11, 12, 14, 15]\n", "my_list.append(new_list)\n", "my_list" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, [10, 11, 12, 14, 15], 10, 11, 12, 14, 15]" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list + new_list" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list.pop(9)\n", "my_list" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 9]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list.remove(8)\n", "my_list" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 11, 12, 14, 15, 1, 2, 3, 4, 5, 6, 7, 9]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_list + my_list" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 5, 7]" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [7, 2, 5, 1, 3]\n", "a.sort()\n", "a" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NoneType" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sort_list)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sort_list is None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## dict\n", "* A Python dictionary called a hash map or an associated array\n", "* Made of key value pairs\n", "* Use curly braces `{}` and colons" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "my_info = {\"name\" : \"daniel\", \n", " \"age\" : 30, \n", " \"degree\" : \"phd\", \n", " \"skills\" : [\"R\", \"Python\", \"C++\"]}" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'age': 30,\n", " 'degree': 'phd',\n", " 'name': 'daniel',\n", " 'skills': ['R', 'Python', 'C++']}" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_info" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['R', 'Python', 'C++']" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_info[\"skills\"]" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"name\" in my_info" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'age': 30,\n", " 'colleges': ['UDSM', 'TSU', 'UNLV'],\n", " 'degree': 'phd',\n", " 'name': 'daniel',\n", " 'skills': ['R', 'Python', 'C++']}" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_info[\"colleges\"] = [\"UDSM\", \"TSU\", \"UNLV\"]\n", "my_info" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## set" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "set1 = {1, 2, 3, 4}\n", "set2 = {5, 6, 7, 8, 9, 4, 4}" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6, 7, 8, 9}" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set1.union(set2)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6, 7, 8, 9}" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set1 | set2" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{4}" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set1.intersection(set2)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{4}" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set1 & set2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "def my_func():\n", " a = 4\n", " b = 6\n", " c = 8\n", " return{\"a\" : a, \"b\" : b, \"c\" : c}" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 4, 'b': 6, 'c': 8}" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_func()" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda', 'south carolina##', 'West virginia?']" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "import re\n", "def clean_strings(strings): \n", " result = [] \n", " for value in strings: \n", " value = value.strip() \n", " value = re.sub('[!#?]', '', value) \n", " value = value.title() \n", " result.append(value) \n", " return result" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Alabama']" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clean_strings(states)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### lambda (anonymous) functions\n", "A way of writing functions consisting of a single statement, the result of which is a return value" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [], "source": [ "anon_func = lambda x: x * 2" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "anon_func(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Files and the operation system" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [], "source": [ "path = \"examples/segismundo.txt\"\n", "f = open(path)" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Sueña el rico en su riqueza,',\n", " 'que más cuidados le ofrece;',\n", " '',\n", " 'sueña el pobre que padece',\n", " 'su miseria y su pobreza;',\n", " '',\n", " 'sueña el que a medrar empieza,',\n", " 'sueña el que afana y pretende,',\n", " 'sueña el que agravia y ofende,',\n", " '',\n", " 'y en el mundo, en conclusión,',\n", " 'todos sueñan lo que son,',\n", " 'aunque ninguno lo entiende.',\n", " '']" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lines = [x.rstrip() for x in f]\n", "lines" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "with open(path) as f:\n", " lines = [x.rstrip() for x in f]" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Sueña el rico en su riqueza,',\n", " 'que más cuidados le ofrece;',\n", " '',\n", " 'sueña el pobre que padece',\n", " 'su miseria y su pobreza;',\n", " '',\n", " 'sueña el que a medrar empieza,',\n", " 'sueña el que afana y pretende,',\n", " 'sueña el que agravia y ofende,',\n", " '',\n", " 'y en el mundo, en conclusión,',\n", " 'todos sueñan lo que son,',\n", " 'aunque ninguno lo entiende.',\n", " '']" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lines" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Sueña el rico en su riqueza,\\n',\n", " 'que más cuidados le ofrece;\\n',\n", " '\\n',\n", " 'sueña el pobre que padece\\n',\n", " 'su miseria y su pobreza;\\n',\n", " '\\n',\n", " 'sueña el que a medrar empieza,\\n',\n", " 'sueña el que afana y pretende,\\n',\n", " 'sueña el que agravia y ofende,\\n',\n", " '\\n',\n", " 'y en el mundo, en conclusión,\\n',\n", " 'todos sueñan lo que son,\\n',\n", " 'aunque ninguno lo entiende.\\n',\n", " '\\n']" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open(path) as f:\n", " lines = f.readlines()\n", "\n", "lines " ] }, { "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }