mapping quake data

Lab 2
Programming for Data Analysis
Lehman College, City University of New York
Spring 2017

Today's lab will focus on loops and the random library in Python.
Software tools needed: web browser and Python IDLE programming environment.

More on Turtles

Last lab, we wrote a program using Turtle graphics:

Click on the ► to run the program.

The range() function generates a sequence of numbers. For example, range(4) gives the sequence 0,1,2,3. While range(10) gives 0,1,2,3,4,5,6,7,8,9. In general, range(n) will give the sequence 0,1,2,...,n-1. We will discuss this more over the next couple of sections.

More on Iteration

Let's explore a bit more about how for loops repeat, or iterate, a set of commands:
  1. Work through 4.6 Iteration Simplifies our Turtle Program.

  2. Let's use the programs with alex the turtle to draw a hexagon with different color sides. The program from the books drew a square:
    import turtle            # set up alex
    wn = turtle.Screen()
    alex = turtle.Turtle()
    for aColor in ["yellow", "red", "purple", "blue"]:
  3. To draw a colorful hexagon:
    1. Open up idle3 and start a new file ("File > New File" or "⌘N").
    2. Copy in the program above from the textbook.
    3. Add two more colors to the list in the for statement.
    4. Change the angle that alex turns left to be 60.
    5. Test your program to make sure it works.
    6. Add your name and a short description to the top and submit at Problem #6.

More on range()

Our previous use of the range() statement generated sequences that always start with 0, but there are many more sequences that can be generated.

First, work through the textbook's section on the 4.7: The range Function.

Next, let's write a program that will print out:

Blast off!

If we were asked to print the numbers in the other direction, we could do this with the simplest range:

for i in range(11):
followed by a print("Blast off!"). But we want to print out the numbers, 10, 9, 8, ... , 1. In the textbook section, they had a very useful command:
print(range(10, 0, -1))
which printed [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].

Let's use that with a for loop to print out our countdown:

for i in range(10,0,-1):

That gives all the pieces for our program! Now let's put them together and test it:

  1. Open up idle3 and start a new file ("File > New File" or "⌘N").
  2. Copy in the for loop above.
  3. Add in the print statement that prints "Blast off!"
  4. Test your program to make sure it works.
  5. Add your name and a short description to the top and submit at Problem #7.
Here's another example of the range function in action. Try to guess what it will do before clicking on the run (►) button:

Random Range

Python has a built-in library for generating random numbers. To use it, you include at the top of your file:
import random
The random library includes a function that's similar to range, called randrange. As with range, you can specify the starting, stopping, and step values, and the function randrange chooses a number at random in that range. Some examples: Let's use that last example to have our turtle make a random walk:

Notice that our turtle turns a degrees, where a is chosen at random between 0 and 359 degrees. What if your turtle was in a city and had to stay on a grid of streets (and not ramble through buildings)? How can you change the randrange() to choose only from the numbers: 0,90,180,270 (submit your answer as Problem #10).

In-class Quiz

During lab, there is a quiz on the for loops and turtles. The password to access the quiz will be given during lab.

What's Next?

If you finish the lab early, now is a great time to get a head start on the programming problems due early next week. There's instructors to help you and you already have Python up and running. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.