{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ Make a list of data objects, each of a different type you know. Print the elements of the list and their type to the screen by index." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Create a dictionary that represents a shopping list and store it in a variable: Use three things you need\n", "to buy as *keys* and add how much you want to buy of them as *values*. Than add another item to the existing dictionary. Print the dictionary keys to the screen." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 1__ Take the points `(0, 0)`, `(0, 1)`, `(1, 0)` and `(1, 1)`.\n", "\n", " - Store each point in a variable as a tuple. Make an empty list and append each point to the list. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Change the second point `(0, 1)` to `(0, -1)` by overwriting the variable in which it is stored. What happens to this point in the list?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Change the third point `(1, 0)` to `(-1, 0)` by indexing this point stored in the list. What happens to this point outside of the list?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now store each of the initial points (`(0, 0)`, `(0, 1)`, `(1, 0)` and `(1, 1)`) in variables as lists and append them to a list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Change the second point `[0, 1]` to `[0, -1]` by overwriting the *y*-component of the variable in which it is stored. What happens to this point in the list?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Change the third point `[1, 0]` to `[-1, 0]` by indexing this point stored in the list and overwriting the its *x*-component. What happens to this point outside of the list?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 2__ Take the string `\"High Performance Computing\"` and\n", " - convert this into a list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Use the tuple `tuple(\"abcdefghijklmnopqrstuvwxyz \")` to map the letters in the string to their index in the alphabet. Tipp: Use the method `tuple.index` and the built-in function `map`. Map takes a function as the first argument and a sequence (e.g. a string as the second. The function is applied to each element of the sequence. Convert the return value of `map()` to a list. Use `string.casefold()` to map also upper case characters. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Create a dictionary from the map and the list that represents this mapping (`d = {\"letter\": mapped_index}`). Tipp: Use `zip()`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 3__ Consider the following dictionary of random floating values associated with random string keys:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "dictionary = {\n", " 'afsgfabkpetv': 0.50996581835599997,\n", " 'ajpjggwdnbbv': 0.48394650530200001,\n", " 'bbsqfnzsaerh': 0.94967251027800004,\n", " 'bmkkubxijsgoi': 0.73021130082789998,\n", " 'bmmqvyintpoat': 0.47911570513349999,\n", " 'ccgasifxzngp': 0.32569065293400001,\n", " 'cebmdkidwwyt': 0.97307019405200001,\n", " 'cismykmadevcya': 0.20194133057088001,\n", " 'cjktphzhhbvrn': 0.27046062498669998,\n", " 'ckdgztgvksmid': 0.3741114083232,\n", " 'cwnukrruweshiuh': 0.53529824780703295,\n", " 'cwthembmixxmg': 0.089824943543700006,\n", " 'dasjwthqotarfgz': 0.48673599515272398,\n", " 'dgoxsygqjkkiv': 0.49586035923279997,\n", " 'dijwhwnubajohkf': 0.074657439369638007,\n", " 'dszizjhbozuuy': 0.19034104761039999,\n", " 'xcvhdthqotarfgk': 0.48673599515272398,\n", " 'ehuzgmflwhxcnmv': 0.027337878930186001,\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Use the built-in properties of sets to find out whether any two values are the same. How could you check if a set already contains a dictionary value? Could two keys ever be the same?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Compute the sum of all values in the dictionary." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Make a set of unique letters found in all dictionary keys. Tipp: Use the string method `str.join()`." ] } ], "metadata": { "anaconda-cloud": {}, "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.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }