|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### Sondos Aabed 1190652\n", |
| 8 | + "### Week Two of parctical training \n", |
| 9 | + "### Functions in Python \n" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "#### Quiz: Population Density Function\n", |
| 17 | + "\n", |
| 18 | + "Write a function named population_density that takes two arguments, population and land_area, and returns a population density calculated from those values.\n" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 1, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [ |
| 26 | + { |
| 27 | + "name": "stdout", |
| 28 | + "output_type": "stream", |
| 29 | + "text": [ |
| 30 | + "0.5\n" |
| 31 | + ] |
| 32 | + } |
| 33 | + ], |
| 34 | + "source": [ |
| 35 | + "def population_density(population, land_area):\n", |
| 36 | + " return population/land_area\n", |
| 37 | + "print(population_density(10,20))" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "#### Quiz: readable_timedelta\n", |
| 45 | + "\n", |
| 46 | + "Write a function named readable_timedelta. The function should take one argument, an integer days, and return a string that says how many weeks and days that is. For example, calling the function and printing the result like this:\n", |
| 47 | + "\n", |
| 48 | + "print(readable_timedelta(10))\n", |
| 49 | + "\n", |
| 50 | + "should output the following:\n", |
| 51 | + "\n", |
| 52 | + "1 week(s) and 3 day(s).\n", |
| 53 | + "\n" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 5, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [ |
| 61 | + { |
| 62 | + "name": "stdout", |
| 63 | + "output_type": "stream", |
| 64 | + "text": [ |
| 65 | + "0 week(s) and 1 day(s)\n" |
| 66 | + ] |
| 67 | + } |
| 68 | + ], |
| 69 | + "source": [ |
| 70 | + "def readable_timedelta(days):\n", |
| 71 | + " return \"{} week(s) and {} day(s)\".format(int(days/7),days%7)\n", |
| 72 | + "\n", |
| 73 | + "print(readable_timedelta(1))" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "#### Quiz: Lambda with Map\n", |
| 81 | + "\n", |
| 82 | + "`map()` is a higher-order built-in function that takes a function and iterable as inputs, and returns an iterator that applies the function to each element of the iterable. The code below uses `map()` to find the mean of each list in `numbers` to create the list `averages`. Give it a test run to see what happens.\n", |
| 83 | + "\n", |
| 84 | + "Rewrite this code to be more concise by replacing the `mean` function with a lambda expression defined within the call to `map()`." |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "code", |
| 89 | + "execution_count": 8, |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [ |
| 92 | + { |
| 93 | + "name": "stdout", |
| 94 | + "output_type": "stream", |
| 95 | + "text": [ |
| 96 | + "[57.0, 58.2, 50.6, 27.2]\n" |
| 97 | + ] |
| 98 | + } |
| 99 | + ], |
| 100 | + "source": [ |
| 101 | + "numbers = [\n", |
| 102 | + " [34, 63, 88, 71, 29],\n", |
| 103 | + " [90, 78, 51, 27, 45],\n", |
| 104 | + " [63, 37, 85, 46, 22],\n", |
| 105 | + " [51, 22, 34, 11, 18]\n", |
| 106 | + " ]\n", |
| 107 | + "\n", |
| 108 | + "def mean(num_list):\n", |
| 109 | + " return sum(num_list) / len(num_list)\n", |
| 110 | + "\n", |
| 111 | + "averages = list(map(lambda num_list: sum(num_list)/len(num_list), numbers))\n", |
| 112 | + "print(averages)\n" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "markdown", |
| 117 | + "metadata": {}, |
| 118 | + "source": [ |
| 119 | + "#### Quiz: Lambda with Filter\n", |
| 120 | + "\n", |
| 121 | + "`filter()` is a higher-order built-in function that takes a function and iterable as inputs and returns an iterator with the elements from the iterable for which the function returns True. The code below uses `filter()` to get the names in `cities` that are fewer than 10 characters long to create the list `short_cities`. Give it a test run to see what happens.\n", |
| 122 | + "\n", |
| 123 | + "Rewrite this code to be more concise by replacing the `is_short` function with a lambda expression defined within the call to `filter()`." |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "cities = [\"New York City\", \"Los Angeles\", \"Chicago\", \"Mountain View\", \"Denver\", \"Boston\"]\n", |
| 133 | + "\n", |
| 134 | + "def is_short(name):\n", |
| 135 | + " return len(name) < 10\n", |
| 136 | + "\n", |
| 137 | + "short_cities = list(filter(is_short, cities))" |
| 138 | + ] |
| 139 | + } |
| 140 | + ], |
| 141 | + "metadata": { |
| 142 | + "kernelspec": { |
| 143 | + "display_name": "Python 3", |
| 144 | + "language": "python", |
| 145 | + "name": "python3" |
| 146 | + }, |
| 147 | + "language_info": { |
| 148 | + "codemirror_mode": { |
| 149 | + "name": "ipython", |
| 150 | + "version": 3 |
| 151 | + }, |
| 152 | + "file_extension": ".py", |
| 153 | + "mimetype": "text/x-python", |
| 154 | + "name": "python", |
| 155 | + "nbconvert_exporter": "python", |
| 156 | + "pygments_lexer": "ipython3", |
| 157 | + "version": "3.11.0" |
| 158 | + }, |
| 159 | + "orig_nbformat": 4 |
| 160 | + }, |
| 161 | + "nbformat": 4, |
| 162 | + "nbformat_minor": 2 |
| 163 | +} |
0 commit comments