CSci 157 - Lab 2

Purpose: to become familiar with writing scripts in Python, then turning those scripts into programs.

Warm Up: Text Editors

We'll be writing simple programs using a text editor for this and many later lab assignments; last week I suggested using gedit, but there are many more editors installed. Here are a few that you might try out.  Just type one of these names at the command line to start; you can also find them using Activity search bar.  Just start typing "text" or "editor" and one or more of these will come up. Note that word processors don't work for writing programs, which must be plain text with no fancy font or formatting information in them.

When we look at Python Integrated Development Environments (IDEs) later this semester, we'll see that the editor is built-in to these programs. But you don't always have access to IDEs, so learning at least one editor is still a good investment.

Resources.

  1. Professor Dale's Basic UNIX/Linux command reference
  2. Learning the Shell tutorial

Part 1. Navigating the Linux Terminal

For this section, you'll write answers on a sheet of paper. You can use scratch paper if you need it, look on the shelf above the printer.

Start the "Learning the Shell" tutorial. To get a hang of using the basic Linux commands, you'll work through the first three sections: What is "the shell"?, Navigation, and Looking Around. As you work through these sections, find answers for the following questions:

  1. What command would you use to get the name of the current working directory?

  2. Write out the (complete) name of your home directory.

  3. What directory does the single dot "." refer to?
     
  4. What directory does the double dot ".." refer to?

  5. What directory does the cd command take you to if you enter it by itself?

  6. Show a use of the cd command that will always take you to your CS157 directory, no matter which directory you happen to be in.

  7. Make your home directory the current directory. List the directory contents using ls -l. Now list the directory contents again using ls -la. What new files do you see in the second directory listing? Why did they not show up the first time?
     
  8. Try the command ls -ltc; this lists the directory with the newest (or most recently modified) files first. You don't have enough new files for this to be useful now, but it can be helpful later if you need to see which files you were making changes to (or downloaded) most recently.

Part 2.  In this part we will create a Python script from code we ran last week, and then in a later class we'll see how to make it a Python program. Finally, we'll try to write a new program from scratch.

  1. In Lab 1, you tried out the following code fragment in the Python shell. Try it out again:
    pi = 3.14159
    radius = 10.0  # different value this time
    area = pi*(radius**2)
    print('The area is', area)
  2. Exit the Python shell by typing either exit() or quit() at the prompt. As a shortcut, you can also exit by typing Ctrl-d (while holding the Ctrl key down, type a d).
     
  3. Open a text editor and type the statements in Step 1 into a new file. Save it as area.py and run it (see Lab 1 Part 3 if you need a reminder on how to run scripts). Should have the same results as before.

  4. There's not much point to an area script that only calculates one possible radius! Replace line 2 with the following statement:
        radius = input("Enter a value for the radius: ")
  5. Run this program. Does it work? Probably not, for reasons explained in Zelle Chapter 2. A quick fix would be:
        radius = float(input("Enter a value for the radius: "))
    Recall that using functions like float convert values from one type to another. See if you can enter different values for radius each time you run the script.
     
  6. Now write a new program from scratch; start a new file in the editor and add code to compute the wind chill when given a temperature and wind speed. The structure of the file will be similar to area.py, though the calculations and variable names should be different. In particular, your program should prompt the user for the values of T and v in the math formula below:

    For a temperature value T, the wind chill is equal to w = 35.74 + 0.6215T + (0.4275T - 35.75) v0.16
     
    Just by the way, this formula is only valid for temperatures less than 45 degrees F and wind speeds up to 60 mph.

    Run the program a few times with reasonable values for T and v.

Part 3. Demonstration

When finished, demonstrate the two running scripts to the instructor. Turn in your answers to the Part 1 questions.

Read Zelle Chapter 2 completely by next week for a solid introduction to more building blocks of simple Python programs (in your text, also posted in BrightSpace).
Be prepared to ask and answer questions about the material in lecture.