In this lesson, you will write
Python code to create variables (to store data values) and to run arithmetic calculations on variables.
After completing this lesson, you will be able to:
- Explain how
Pythonuses variables to store data
- create variables to store single data values (e.g. integers, decimals, text string)
- run simple arithmetic calculations on these variables (e.g. with the addition operator
What You Need
Be sure that you have followed the instructions on Setting up Git, Bash, and Anaconda on your computer to install the tools for your operating system (Windows, Mac, Linux).
Be sure that you have completed the previous lesson on The Jupyter Notebook Interface.
The code below is available in the ea-bootcamp-day-2 repository that you cloned to
earth-analytics-bootcamp under your home directory.
Within a programming workflow, variables store data (i.e. information) that you want to re-use in your code (e.g. a single value, list of values, a filename, or path to a directory).
Python, variables can be created without defining the type of data that it will hold (e.g. whole numbers, text string). This differs from many other programming languages that require the variable to be explicitly assigned a data type.
A variable can contain a single value - such as a whole number (i.e. integer), decimal, or text string - or it can contain a list of values. In this lesson, you will work with variables that store single data values.
You can create a variable to store a single numeric value by defining a name and assigning a value to that name using the equal (
=) operator (e.g.
variablename = 1.5).
If the value is a text string, you need to add quotations (
"") around the value, so that
Python knows it is a text string (e.g.
variablename = "text").
Just like you want to use good naming conventions for directories on your computer, you also want to assign clear and short names to variables, avoiding spaces or complicated wording, but still specific enough that you know what it is.
For example, a variable containing the average annual precipitation in millimeters (mm) for Boulder, Colorado could simply be called
precip = 525 precip
city = "Boulder" city
In this lesson, you will:
- create new variables for average monthly precipitation values for Boulder, CO, U.S.A.
- apply arithmetic calculations to these variables to convert the values from inches to millimeters.
You will use data on average monthly precipitation for Boulder, Colorado provided by the U.S. National Oceanic and Atmospheric Administration (NOAA).
Begin Writing Your Code
Add Comments to Your Python Code
You have previously learned that documentation is critical for open reproducible science.
Python provides a great way to easily document your code with comments.
Python comments before each code block can help you to document and explain what is being accomplished with that code.
Python comments are lines in the your code that will not execute a task and can be designated using
#. Typically, the comment is written before the lines of codes, or code bloack (e.g.
# the code below does this).
Begin by creating a variable for each month to store the single value of average precipitation in that month (e.g.
jan = 0.70).
# create new variables for monthly average precipitation values (inches) for January through June jan = 0.70 feb = 0.75 mar = 1.85 apr = 2.93 may = 3.05 june = 2.02
Running Arithmetic Calculations
Python, there are many arithmetic operators including operators for addition (
+) , subtraction (
-), multiplication (
*), and division (
Run the Cells below and notice that the output is automatically generated, without the need to call the
a = 4 b = 10 a + b
b - a
a * b
b / a
Assign Calculation Results to Variable
You can also assign new values to an existing variables as part of an arithmetic operation, though now you will need to use the
a = a + b print(a)
Notice that the word
14, without the quotations or parentheses.
You have now used your first
Python function! Functions in
Python are commands that can take inputs that are used to produce output.
We will learn more about functions as the course continues, and we will use the
Now that you have created individual variables to store the average precipitation (in inches) for each month, you can run arithmetic calculations on these variables to convert the values from inches to millimeters (mm). One inch is equal to 25.4 mm.
Use the multiplication (
*) operator to multiply each monthly variable by 25.4 to convert the value from inches to millimeters. Use the original variable name (e.g.
jan) to recreate it with the new value.
# convert the monthly variables for `jan` to `june` from inches to millimeters (1 inch = 25.4 mm) jan = jan * 25.4 feb = feb * 25.4 mar = mar * 25.4 apr = apr * 25.4 may = may * 25.4 june = june * 25.4 # print the value of `jan` to check that its value has changed print(jan)
Congratulations - you have created your first
Python variables of this course!
Python skills to:
Create new variables for monthly average precipitation values (inches) for July through December (hint:
jan = 0.70).
Use the appropriate arithmetic operator to convert the monthly variables for
decfrom inches to millimeters (1 inch = 25.4 mm) (hint:
jan = jan * 25.4).
Print the values of
decto check that the values have changed (hint: