{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Practice Problems\n", "### Lecture 11\n", "Answer each number in a separate cell\n", "\n", "Rename this notebook with your last name, first initial and the lecture \n", " \n", " ex. Cych_B_11\n", " \n", "Turn-in this notebook on Canvas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Lambda functions\n", " - Write a regular function **s** that takes one argument and returns the square of that argument\n", " - Write a **lambda** function called **square** that squares the input parameter\n", " - Call your function **s** with the value 4. Print the output\n", " - Call your function **square** with the value 4. Print the output\n", "\n", "2. map( )\n", " - create a list of values from 0 to 300 in intervals of 10 \n", " - create a list of values from 0 to 90 in intervals of 3\n", " - write an anonymous function that calculates the difference betwen two values\n", " - apply **map( )** and the anonymous function to the two lists you defined\n", " - print the results of the **map( )** function - remember to use **list( )** to make a list from the list generator made by **map( )** \n", "\n", "3. filter( )\n", " - Copy the following dictionary into a code cell:\n", "\n", " **lastEruption = {\"Mt.Etna\": 2017, \"Mt. St. Helens\" :1980, \"Mt. Erebus\": 2017, \"Mount Teide\" : 1909, \"Mt. Hood\": 1800}** \n", "\n", " - Define an anonymous function **active** that returns a boolean. It should return **True** if a volcano has erupted in the last 5 years \n", " - use the function **filter( )**, the function **active**, and the dictionary of volcanoes to determine which volcanoes have erupted in the last 5 years. \n", " - Print the names of the recently active volcanoes \n", "\n", "4. reduce( )\n", " - write an anonymous function that finds multiples of 7 \n", " - write a different anonymous function that returns the greater of two numbers \n", " - use your two anonymous functions with **filter( )** and **reduce( )** to find the greatest multiple of 7 in this list:\n", "\\[234, 55, 40, 100, 450, 335, 308, 693, 333, 405, 303, 109, 321, 565, 891\\]\n", " - print the final value\n", "\n", "5. List comprehensions\n", " - The following dictionary, **atomicNumbers**, has the atomic number of an element as the key and the element name as the value\n", "\n", " **atomicNumbers = {1:'H', 2:\"He\", 3: \"Li\", 4:\"Be\", 5:\"B\", 6:\"C\", 7: \"N\", 8:\"O\", 9:\"F\", 10:\"Ne\", 11:\"Na\", 12:\"Mg\", 13:\"Al\", 14:\"Si\", 15:\"P\", 16:\"S\", 17:\"Cl\", 18:\"Ar\"}**\n", " - The following list, **lifeElements**, contains the atomic numbers essential for life\n", "\n", " **lifeElements = \\[6,1, 8,7,15,16\\]**\n", " - use a list comprehension to print out the names of the elements that are essential for life.\n", "\n", "6. Dictionary comprehensions\n", " - The following list - elements- is a list of the first 18 elements in the periodic table\n", "\n", " **elements = \\[\"H\", \"He\", \"Li\", \"Be\", \"B\", \"C\", \"N\", \"O\", \"F\", \"N\", \"Na\", \"Mg\", \"Al\", \"Si\", \"P\", \"S\", \"Cl\", \"Ar\"\\]** \n", " - Create a dictionary comprehension of the elements and their atomic number. The key is the element name while the value is the atomic number\n", " - print the dictionary " ] }, { "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }