{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# In Class Activity - Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook we will try to put some of the Python knowledge you acquired into use. For each exercise, you will see some code that has some bugs or asks you to write a little program. Simply fix the code cell to make code that runs correctly, or write the code to specification. Then add a markdown cell below your answer that says what you changed and why, or explains what your code is doing in English." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Importing and using existing functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 0: Random number generation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following code imports the random number functionality in Python from the `numpy` library and uses to do generate random numbers. Why won't this code run?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'numpy' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\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;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnpr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"My random number is \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'numpy' is not defined" ] } ], "source": [ "import numpy.random as npr\n", "\n", "print(\"My random number is \", numpy.random.random())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1: Random sample" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next line chooses a random sample of 5 elements from a list. What is wrong here?" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 5)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m5\u001b[0m\n\u001b[0;31m npr.choice(my_list,size=10,replace=False)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "import numpy.random as npr\n", "\n", "my_list = list(range(100)\n", " \n", "npr.choice(my_list,size=10,replace=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 2: Looking things up" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above example when you get it to work what does the `choice()` function do? The documentation for this function is available by googling for \"numpy.random choice\". Add your answer in a cell below. Then look up the `random()` function used in Problem 0. What does it do?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing a function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the problem that remain you will need to write little function that accomplish various goals. Let's verify your understanding of functions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3: Defining a function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What does this function do? Write a new cell below the function definition that calls the function using five different inputs and verify the outputs match your expectation." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def my_function(x, y):\n", " return x+y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4: What is not great about this function?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function works but is not ideal, why?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def my_function_2(x, y, z):\n", " return x+y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 5: Write you own function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write your own custom function named `catchy_name()` to write a program that takes as input two arguments which are assumed to be strings. Return a new string which is the concatenations of the first two letters of each of the inputs. e.g. \n", "```\n", "catchy_name(\"South\",\"Houston\") -> \"SoHo\"\n", "catchy_name(\"Jennifer\", \"Lopez\") -> \"JeLo\"\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 6: Cat-Dog" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function called `catdog` that returns True if a input string given has the word cat and dog an equal number of times. For example, \n", "```\n", "cat_dog('catdog') # shoudl return True\n", "cat_dog('catcat') # should return false\n", "cat_dog('1cat1cadodog') # should return true\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Google 'python count occurrences in string' to find a existing function of method that counts the number of times a substring appears in a string to get started." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 7: Summing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function call `summing()` that takes a list of numbers as input and return the sum of all the numbers. Use a for loop to do this.\n", "\n", "```\n", "summing([1, 2, 3]) → 6\n", "summing([5, 11, 2]) → 18\n", "summming([7, 0, 0]) → 7\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 8: Biggest difference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in `min` and `max` functions.\n", "\n", "```\n", "big_diff([10, 3, 5, 6]) → 7\n", "big_diff([7, 2, 10, 9]) → 8\n", "big_diff([2, 10, 7, 2]) → 8\n", "```" ] }, { "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.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }