New Mexico Tech
Earth and Environmental Science

ERTH 401 / GEOP 501 - Computational Methods & Tools

Lab 2: Python, Variables

Lab slides

Logic prep (pdf, last few slides)

Introduction

Today we want you to play with variables, relational, logical operators and strings in Python. This is a basis to understand flow control later on. In this lab, you will be using both interactive Python (where Python will execute any command that you give it instantly) and you will be writing scripts with a text editor. (Note: We will be using Python 2.7 for this class since the computers in MSEC 345 all use version 2.7. There are minor syntax differences with other versions, so if you are writing scripts in the future for your research, it is recommended to look up the differences.)

For this lab, I expect one file with all the commands you use copied into it with an explanation using comments similar to my template. I also expect two separate Python scripts (ends in '.py') for the last two exercises at the bottom of the page.

Resources

Operators
Variables
Strings

Exercise I: Assignments and Comparisons

Open up the terminal like you did from the first lab and type python to begin running the Python interpreter.
First we're going to learn how to create variables and make comparisons. Variables are reserved memory locations to store values which can be referenced and manipulated by a computer program. In our first exercise, we're going to reference test_var, compare it to several numeric values and assign it a new value. Run the following code and explain the outputs that you get.

test_var = 1
test_var == 1
test_var == 5
test_var is 5
test_var is not 5

You can also perform mathematical operations in Python and you can perform operations on variables. Try the following code.

2 + 3
5 * 7
5 ** 2
7 % 3
x = 3
x += 1
x
x = 5 + 5 * 2
x
x /= 3
x

Make sure to explain what the ** and % operators are doing.

Exercise II: Relational Operators

There are several different operators that we can use to compare values and variables. What is happening in this code?

a = 2
b = 3
a > b
b > a
a > b or b > a
a > b and b > a
a != b

Exercise III: Data types

Now that we understand operators, we're going to look at the different types of variables we can use in Python. The standard variable is called an integer, which is simply a whole number. In this example, we'll also utilize floating point numbers, which have decimal points. Run Python and type the following:

x = 25
x
float(x)
int(float(x))
int(455.2)
int(455.7)

What does the float() function do to 'x'? What happens with the last output?
Next, try this program:

y = 5
type(y)
y = 5 / 2
y
type(y)
y = 5 / 2.0
y
type(y)

What does the type() function do? What is significant about the sixth line of the code?

Exercise IV: Strings

Now we're going to learn about strings, which store characters instead of numbers. Strings can be manipulated in different ways.
In this example, we're going to use brackets after the string to splice out certain letters. Try this code and explain how string splicing works.

text = 'Hello'
text
text[0]
text[0:2]
text = text + ' Hi'
text

You can do a lot of different things with strings. Try the following code and explain what's happening.

number = 123
type(number)
text = str(number)
type(text)
print ("Mike's age: " + text[1:])
print ("Steve's age: " + text[1:] + " years")
print ("Grandpa's age: " + number)

Exercise V: Error codes

The last line of code from exercise IV produced an error code. You cannot combine integers or floats with a string in a print statement.
Try the following lines of code. If they produce an error code, explain the error code and how you can fix it. If you don't understand the error, ask for help or look it up online.

test_var2 == 1
print = 5
type()
float() = 10
text = "Cat"
text[6]

Exercise VI: Scripts and Deliverables

For our final exercise, we're going to introduce you to text editors and show you how to create an executable script. Then you will use the knowledge you've gained from this lab to create two scripts on your own.

First, exit the Python interpreter by typing quit() or just press Ctrl-D. Next, type touch helloworld.py to create a new file and then type vim helloworld.py to open up the Vim text editor.
There are 2 main modes within Vim, normal mode and insert mode. This takes a little getting used to, but one important thing to remember is that the escape key switches to normal mode and the i key switches to insert mode.

Normal mode: This mode allows you to navigate the text efficiently and run commands. Here are some example commands:

Insert mode: This is the mode used to actually write text.

Now, using Vim, create the following script:

#!/usr/bin/env python
print "Hello world"

Then, exit Vim by going to normal mode (escape) and typing :wq. The w saves the file and the q quits Vim and goes back to the terminal. The line #!/usr/bin/env python is essential to create an executable script, but to finish making your script executable, type chmod +x helloworld.py. You can now run your script by typing ./helloworld.py (enter).

Now that you can write your own scripts, For the next exercise, try writing a script which defines a single string consisting of a series of at least three words (ex. "cat, tree, dog") and then prints a statement with each word spliced into a full sentence (example output: The cat climbed up the tree to escape the dog).

Finally, create a script which defines three integers (not strings) greater than 99 and concatenates one or two digits from each number into a single string of numbers. Example: If you have the numbers 145, 277 and 921, a valid output would be "14779."

rg <at> nmt <dot> edu | Last modified: August 29 2017 18:57.