Learning Objectives

Following this assignment students should be able to:

  • execute simple math in the Python shell
  • assign and manipulate variables
  • use built-in functions for math and stats
  • understand the assignment and execution flow of a Python script
  • understand the vector and data frame object structures
  • assign, subset, and manipulate data in a vector
  • execute vector algebra
  • import data frames and interact with columns as vectors

Reading

Lecture Notes


Exercises

  1. Basic Expressions (10 pts)

    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
    print(2-10)
    
    [click here for output]
  2. Basic Variables (10 pts)

    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
    print(mass_g)
    

    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 (10 pts)

    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 (10 pts)

    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. Code Shuffle (10 pts)

    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
    plt.plot(monthly_mean_ppt)
    import numpy as np
    monthly_mean_ppt = ppt_data.mean(axis=0)
    ppt_data = np.loadtxt("gainesville-precip.csv", delimiter=',')
    plt.show()
    import matplotlib.pyplot as plt
    

Assignment submission & checklist