1. -- Basic Expressions --

    Think about what value each of the following expressions will return? Check your answers using the Python interpreter by typing each expression into the interpreter and pressing enter.

    1. 2 - 10
    2. 3 * 5
    3. 8 / 2
    4. 9 / 2
    5. 9.0 / 2
    6. 5 - 3 * 2
    7. (5 - 3) * 2
    8. 4 ** 2
    9. 8 / 2 ** 2

    Did any of the results surprise you? If so, then you’ve probably run into a common point of confusion in Python 2 - Integer Division.

    Now turn this set of expressions into a program that you can save by using the editor. For each expression add one line to the editor as part of a print statement to display the answer to the screen.

    To tell someone reading the code what this section of the code is about, add a comment line that says ‘Problem 1’ before the code that answers the problem. Comments in Python are added by adding the # sign. Anything after a # sign on the same line is ignored when the program is run. So, the start of your program should look something like:

    # Problem 1
    [click here for output]
  2. -- Basic Variables --

    Here is a small program that converts a mass in kilograms to a mass in grams and then prints out the resulting value.

    mass_kg = 2.62
    mass_g = mass_kg * 1000

    Modify this code to create a variable that stores a body mass in pounds and assign it a value of 3.5 (about the right size for a Desert Cottontail Rabbit – Sylvilagus audubonii). Convert this value to kilograms (we are serious scientists after all). There are approximately 2.2046 lbs in a kilogram, so divide the variable storing the weight in pounds by 2.2046 and store this value in a new variable for storing mass in kilograms. Print the value of the new variable to the screen.

    [click here for output]
  3. -- More Variables --

    Calculate a total biomass in grams for 3 White-throated Woodrats (Neotoma albigula) and then convert it to kilograms. The total biomass is simply the sum of the biomass of all individuals, but in this case we only know that the average size of a single individual is 250 grams.

    1. Add a new section to your Python file starting with a comment.
    2. Create a variable grams and assign it the mass of a single Neotoma albigula.
    3. Create a variable number and assign it the number of individuals
    4. Create a variable biomass and assign it a value by multiplying the two variables together.
    5. Convert the value of biomass into kilograms (there are 1000 grams in a kilogram so divide by 1000) and assign this value to a new variable.
    6. Print the final answer to the screen.

    Are the variable names grams, number, and biomass the best choice? If we came back to the code for this assignment in two weeks (without the assignment itself in hand) would we be able to remember what these variables were referring to and therefore what was going on in the code? The variable name biomass is also kind of long. If we had to type it several times it would be faster just to type b. We could also use really descriptive alternatives like individual_mass_in_grams. Or we would compromise and abbreviate this or leave out some of the words to make it shorter (e.g., indiv_mass_g).

    Think about this and then rename the variables in your program in whatever you think is most useful.

    [click here for output]
  4. -- Built-in Functions --

    Use the built-in functions abs(), round(), pow(), int(), float(), and str() to print out the answers to the following. A built-in function is one that you don’t need to import a module to use. Use another function, help(), to learn how to use any of the functions that you don’t know how to use appropriately. help() takes one parameter, the name of the function you want information about. E.g.,help(pow).

    1. The absolute value of -15.5.
    2. 3.8 rounded to the nearest integer using standard rounding.
    3. 4.483847 rounded to one decimal place.
    4. Convert 3.8 to an integer format using int(), assign the value to a variable, and print out it’s value.
    5. Convert the answer to the previous question to a string and assign it back to the same variable name, print out the value.
    6. Convert the answer to the previous question to a float and assign it back to the same variable name, print out the value.
    7. Take the square of the answer to the previous question, assign it the same variable name, print out the value.
    [click here for output]
  5. -- math Functions --

    Use the sqrt() and log() functions from the math module, along with the built-in round() function to print the answers to the following questions to the screen.

    1. How long is one side of a square plant census quadrat that has an area of 10 m^2, rounded to two decimal places?
    2. The number of species in a region can be estimated based on the area of that region using the equation:

      number of species = 3.5 + 0.25 * log(area)

      The logarithms in this equation are natural logarithms. What is the estimated number of species for an area of 8 km^2 rounded to the nearest integer.

    [click here for output]
  6. -- Modify the Code 1 --

    The following code calculates the total net primary productivity (NPP) per day for two sites based on the grams of carbon produced per square meter per day, and the total area of the sites, and prints them out.

        site1_g_carbon_m2_day = 5
        site2_g_carbon_m2_day = 2.3
        site1_area_m2 = 200
        site2_area_m2 = 450
        site1_npp_day = site1_g_carbon_m2_day * site1_area_m2 
        site2_npp_day = site2_g_carbon_m2_day * site2_area_m2

    Modify the code to produce the following items and print them out in order:

    1. The sum of the total daily NPP for the two sites combined.
    2. The difference between the total daily NPP for the two sites. We only want an absolute difference, so use abs() function to make sure the number is positive.
    3. The total NPP over a year for the two sites combined.
    [click here for output]
  7. -- Code Shuffle --

    We are interested in understanding the monthly variation in precipitation in Gainesville, FL. We’ll use some data from the NOAA National Climatic Data Center.

    Start by downloading the data and saving it in the same directory as your homework script. Each row of this data file is a year (from 1961-2013) and each column is a month (January-December).

    Rearrange the following program so that it:

    • Imports the necessary modules
    • Imports the data
    • Calculates the average precipitation in each month across years
    • Plots the monthly averages as simply line plot
    import numpy as np
    monthly_mean_ppt = ppt_data.mean(axis=0)
    ppt_data = np.loadtxt("gainesville-precip.csv", delimiter=',')
    import matplotlib.pyplot as plt