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.
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)
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.
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.
a. Add a new section to your Python file starting with a comment.
b. Create a variable grams
and assign it the mass of a single
Neotoma albigula.
c. Create a variable number
and assign it the number of individuals
d. Create a variable biomass
and assign it a value by multiplying
the two variables together.
e. 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.
f. 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.
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)
.
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.
number of species = 3.5 +
0.25 * log(area)
. The logarithms in this equation are natural
logarithms. For an area of 8 km^2 what is the estimated number of
species rounded to the nearest integer.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:
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:
a. Imports the necessary modules b. Imports the data c. Calculates the average precipitation in each month across years d. 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