Exercises
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.
 2  10
 3 * 5
 8 / 2
 9 / 2
 9.0 / 2
 5  3 * 2
 (5  3) * 2
 4 ** 2
 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:Expected outputs for Basic Expressions: 1# Problem 1 print(210)
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 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.
Expected outputs for Basic Variables: 1More Variables
Calculate a total biomass in grams for 3 Whitethroated 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.
 Add a new section to your Python file starting with a comment.
 Create a variable
grams
and assign it the mass of a single Neotoma albigula.  Create a variable
number
and assign it the number of individuals  Create a variable
biomass
and assign it a value by multiplying the two variables together.  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.  Print the final answer to the screen.
Are the variable names
grams
,number
, andbiomass
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 namebiomass
is also kind of long. If we had to type it several times it would be faster just to typeb
. We could also use really descriptive alternatives likeindividual_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.
Expected outputs for More Variables: 1Builtin Functions
Use the builtin functions
abs()
,round()
,pow()
,int()
,float()
, andstr()
to print out the answers to the following. A builtin 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)
. The absolute value of 15.5.
 3.8 rounded to the nearest integer using standard rounding.
 4.483847 rounded to one decimal place.
 Convert 3.8 to an integer format using int(), assign the value to a variable, and print out it’s value.
 Convert the answer to the previous question to a string and assign it back to the same variable name, print out the value.
 Convert the answer to the previous question to a float and assign it back to the same variable name, print out the value.
 Take the square of the answer to the previous question, assign it the same variable name, print out the value.
math Functions
Use the
sqrt()
andlog()
functions from themath
module, along with the builtinround()
function to print the answers to the following questions to the screen. How long is one side of a square plant census quadrat that has an area of 10 m^2, rounded to two decimal places?

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.
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 print(site1_npp_day) print(site2_npp_day)
Modify the code to produce the following items and print them out in order:
 The sum of the total daily NPP for the two sites combined.
 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.
 The total NPP over a year for the two sites combined.
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 19612013) and each column is a month (JanuaryDecember).
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
Expected outputs for Code Shuffle:plt.plot(monthly_mean_ppt) import numpy as np monthly_mean_ppt = ppt_data.mean(axis=0) ppt_data = np.loadtxt("gainesvilleprecip.csv", delimiter=',') plt.show() import matplotlib.pyplot as plt