diff --git a/python-crashcourse.ipynb b/python-crashcourse.ipynb index 914dd29..f265c6a 100644 --- a/python-crashcourse.ipynb +++ b/python-crashcourse.ipynb @@ -167,7 +167,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "> We can write exponents with the \" __**__ \" operator." + "> __Question__: What do you think will happen when we perform the following operation?
\n", + "`a + c`" ] }, { @@ -176,33 +177,32 @@ "metadata": {}, "outputs": [], "source": [ - "####### Example #######\n", - "2 ** 3" + "####### YOUR CODE HERE #######\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "> __Question__: What do you think will happen when we perform the following operation?
\n", - "`a + c`" + "> __Highlight for Answer__: We get a TypeError when we try to perform arithmetic with an integer and a string. This is how Python lets us know that we're asking it to do something that doesn't make sense.
\n", + "Was your prediction correct?" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "####### YOUR CODE HERE #######\n" + "> We can write exponents with the \" __**__ \" operator." ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "> __Highlight for Answer__: We get a TypeError when we try to perform arithmetic with an integer and a string. This is how Python lets us know that we're asking it to do something that doesn't make sense.
\n", - "Was your prediction correct?" + "####### Example #######\n", + "3**2 # This is \"3 squared\"" ] }, { @@ -227,6 +227,31 @@ "hrs_in_week" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> __Question__: Chromosomes are duplicated once every hour. If we start with 1 chromosome, how many will have after 3 hours? 5 hours? 100 hours?
\n", + "Solve using variable assignments and arithmetic.
\n", + "__HINT__: What arithmetic operation(s) are useful in this situation?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "####### YOUR CODE HERE #######\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> __Highlight for answer__: Assign a variable for the number of hours (e.g. hours = 3), and then perform the calculation 2**hours." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -240,8 +265,8 @@ "source": [ "> Write code __without__ using variable assignments to show the results of the following:
\n", "1. $\\frac{3}{2}\\times 5$\n", - "2. $0.11\\times 3$\n", - "3. $4^2 - 3$" + "2. $0.2\\times 5$\n", + "3. $5^2 - 3$" ] }, { @@ -250,8 +275,7 @@ "metadata": {}, "outputs": [], "source": [ - "####### YOUR CODE HERE #######\n", - "# 1.\n" + "####### YOUR CODE HERE #######\n" ] }, { @@ -260,8 +284,7 @@ "metadata": {}, "outputs": [], "source": [ - "####### YOUR CODE HERE #######\n", - "# 2.\n" + "####### YOUR CODE HERE #######\n" ] }, { @@ -270,8 +293,7 @@ "metadata": {}, "outputs": [], "source": [ - "####### YOUR CODE HERE #######\n", - "# 3.\n" + "####### YOUR CODE HERE #######\n" ] }, { @@ -280,8 +302,8 @@ "source": [ "> __Answers__:\n", "1. 7.5\n", - "2. 3.0\n", - "3. 13" + "2. 1.0\n", + "3. 22 " ] }, { @@ -289,7 +311,7 @@ "metadata": {}, "source": [ "> __Question__: How could you use variable assignments to make performing these calculations easier?
\n", - "HINT: Variable assignments are most useful when we need to keep track of numbers that we use repeatedly. \n", + "__HINT__: Variable assignments are most useful when we need to keep track of numbers that we use repeatedly. \n", "\n", "Try the same calculations __with__ variable assignments in the cells below:" ] @@ -303,13 +325,6 @@ "####### YOUR CODE HERE #######\n" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, @@ -371,6 +386,29 @@ "How did this compare to your prediction?" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> __Question__: Something bio example with lists...." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "a = 3**3\n", + "b = 5/2\n", + "c = 0.3 \n", + "\n", + "d = [a,b,c]\n", + "print(d)\n", + "print(d+d)\n", + "sum(d)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -528,9 +566,9 @@ "metadata": {}, "outputs": [], "source": [ - "print('Before:', time)\n", + "print(\"Before:\", time)\n", "time[1] = 7\n", - "print('After:', time)" + "print(\"After:\", time)" ] }, { @@ -609,7 +647,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "> Looking back at the calculations we just made, it sure looks like we need an easier way to perform repetitive tasks. Lucky for us, we can write \" __for loops__ \" to repeat a section of code however many times we want. " + "> Looking back at the calculations we just made, it sure looks like we need an easier way to perform repetitive tasks. Lucky for us, we can write \"__for loops__\" to repeat a seqeuence of operations however many times we want. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> Let's look more closely at the skeleton of a for loop:" ] }, { @@ -619,32 +664,19 @@ "outputs": [], "source": [ "####### Example #######\n", - "# Using for loops with the yeast cell problem looks like this:\n", - "\n", - "# We still want to create the same blank array first\n", - "num_hours = 3 # from the yeast problem\n", - "\n", - "cells = np.zeros(num_hours+1) # np.zeros() makes an array of zeros with a length specified by the input.\n", - "# the +1 adds a zero for the initial value\n", + "num_reps = 5\n", "\n", - "# Next, we still assign the initial value to our array\n", - "cells[0] = 10\n", + "for i in range(num_reps):\n", + " print(i)\n", "\n", - "# BUT now we write a for loop to perform the calculation we want:\n", - "for i in range(num_hours): \n", - " #take the current value of cells[i], multiply by 2, and assign that to the next value\n", - " cells[i+1] = cells[i]*2 \n", - " \n", - "# We print our results:\n", - "print(cells)\n", - "print(\"After 3 hours, there will be \", cells[3], \" cells.\")" + "### What is happening in this cell?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "> Let's look more closely at the skeleton of a for loop:" + "> A more detailed explanation:" ] }, { @@ -658,12 +690,49 @@ "# i is a counting variable that increases by one and goes up to num_reps\n", "\n", "# range() tells the loop to start at i = 0 and go to i = (num_reps-1)\n", - "for i in range(num_reps): # colon is important\n", - " print(i) # repeats the code that is indented\n", + "for i in range(num_reps): # colon designates where loop begins\n", + " print(i) # repeats the indented segment of code\n", "\n", "## We use this counting \" i \" to index arrays and perform repetitive operations. " ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> How can loops help us with the yeast cell problem from before?\n", + "\n", + "1. A population of yeast cells undergoes mitosis every hour, __doubling__ in population size. If there are __10 cells initially__, how many cells will there be after __3 hours__?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "####### Example #######\n", + "# Using for loops with the yeast cell problem looks like this:\n", + "\n", + "# We still want to create the same blank array first\n", + "num_hours = 3 # from the yeast problem\n", + "\n", + "cells = np.zeros(num_hours+1) # np.zeros() makes an array of zeros with a length specified by the input.\n", + "# the +1 adds a zero for the initial value\n", + "\n", + "# Next, we still assign the initial value to our array\n", + "cells[0] = 10\n", + "\n", + "# BUT now we write a for loop to perform the calculation we want:\n", + "for i in range(num_hours): \n", + " #take the current value of cells[i], multiply by 2, and assign that to the next value\n", + " cells[i+1] = cells[i]*2 \n", + " \n", + "# We print our results:\n", + "print(cells)\n", + "print(\"After 3 hours, there will be \", cells[3], \" cells.\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -709,10 +778,17 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "> We find that in building models, we often want to consider many possible outcomes. One of the simpler ways to do that is to provide a logical, or \" ___boolean___ \" statement based on a condition. These statements go something like :\n", + "> We find that in building models, we often want to consider many possible outcomes. One of the simpler ways to do that is to provide a __logical__, or ___boolean___ statement based on a condition. These statements go something like :\n", "- If X is true, Y will happen. Else, if Z is true, W will happen. " ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> The cell below tests whether the value of x is even or odd and returns an outcome. Try different values of x to see it in action!" + ] + }, { "cell_type": "code", "execution_count": null, @@ -722,7 +798,10 @@ "outputs": [], "source": [ "####### Example #######\n", - "x = 10\n", + "###################### \n", + "x = 10 # Test some different values of x\n", + "######################\n", + "\n", "if x % 2 == 0: # \"if the remainder of x / 2 is equal to 0\"\n", " print(\"EVEN\")\n", "else: # \"or else\"\n", @@ -733,14 +812,39 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "> The cell above tests whether the value of x is even or odd and returns an outcome. Try different values of x to see it in action!" + "> Introducing the `LAZY_NICKNAME` generator!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "####### Example #######\n", + "my_name = \"YOUR_NAME\" # Make sure to assign your name as a string\n", + "\n", + "nickname_cutoff = 3\n", + "\n", + "if len(my_name) > nickname_cutoff: # \"if the length of the string is greater than the specified value\"\n", + " print(\"My nickname is\", my_name[:nickname_cutoff], \".\")\n", + " \n", + "else:\n", + " print(\"My name\", my_name, \"is\", len(my_name), \"letters long.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> How does the `len()` function work with strings? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "> __Question__: How would you use conditional statements to test if a number is positive or negative?
HINT: Use \" > \" and \" < \" for greater than and less than operators! " + "> __Question__: We have an array of blood test results for a particular disease. __1__ represents a positive diagnosis (the patient has the disease), and __-1__ means that the patient does not have the disease. How can we use conditional statements to report which patients have the disease?.
HINTS: Use \" > \" and \" < \" for greater than and less than operators!" ] }, { @@ -749,7 +853,14 @@ "metadata": {}, "outputs": [], "source": [ - "####### YOUR CODE HERE #######\n" + "# Replace the blanks '###' with the correct information\n", + "blood_test_data = np.array([1,-1,1,-1,1,1])\n", + "\n", + "num_samples = len(blood_test_data) # Use the number of each sample to identify patients\n", + "\n", + "for i in range(###):\n", + " if ###\n", + " print(###)" ] }, { @@ -791,7 +902,9 @@ "source": [ "#### Practice:\n", "Recall from our mouse model that for some initial population sizes, we'd get negative values over time. We can't have negative mice! Let's use conditional statements to fix this.

\n", - "Copy your mouse model code in the cell below and __add a conditional statement__ to check whether the population size is negative __at each time step__. If it is, then assign that index of the array a value of 0. " + "Copy your mouse model code in the cell below and __add a conditional statement__ to check whether the population size is negative __at each time step__. If it is, then assign that index of the array a value of 0. The problems from before are repeated here:\n", + "1. Every year, the population of mice in a certain environment doubles. Unfortunately, 10 mice will be eaten by snakes each year. There are currently 15 mice. What will happen to the mice population after 5 years? \n", + "2. What happens when we change the initial population size to 5?" ] }, { @@ -844,7 +957,7 @@ "\n", "__HINTS__:
\n", "Make sure to add the standard length of a song to each replicate.
\n", - "Use the `sum( )` function after the loop to add up the total amount of time." + "Use the `sum( )` function after the loop to add up the total amount of time." ] }, { @@ -862,7 +975,7 @@ " \n", "print(songs_length)\n", " \n", - "total_songs_length = ### #Add up all of songs_length\n", + "total_songs_length = ### #Add up all of songs_length using sum()\n", " \n", "print(\"Total time of beautiful serenade:\", total_songs_length, \"minutes\")" ] @@ -961,7 +1074,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.6.5" } }, "nbformat": 4,