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.
- gedit - simple and straightforward editor, no frills. You can
get by this semester with this one, but should look into learning a
more advanced editor if you take more CSCI classes.
Oddly enough, this program shows up as TextEdit when using the Activity search bar. - vim - the "vi improved" editor was Professor Dale's favorite, but I'm not a fan (it's modal, which means keystrokes mean different things at different times). Can run in Terminal or its own window.
- subl - Sublime text editor is an intermediate-level editor. As easy to use as gedit for simple tasks but has some more advanced features that make life better for users willing to learn more.
- vscode - I see more and more students using this editor on their own computers, so I've had it installed on the Linux computers, though I haven't used it much.
- emacs - the most advanced (and hardest to become proficient at) of the bunch, it can be extended with new features using its own programming language! My go-to editor. Can run in Terminal or its own window.
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.
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:
- What command would you use to get the name of the current working
directory?
- Write out the (complete) name of your home directory.
- What directory does the single dot "." refer to?
- What directory does the double dot ".." refer to?
- What directory does the cd command take you to if you enter
it by itself?
- 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.
- 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?
- 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.
- 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)
- Exit the Python shell by typing either
exit()
orquit()
at the prompt. As a shortcut, you can also exit by typingCtrl-d
(while holding theCtrl
key down, type ad
).
- 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.
- 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: ")
- 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 likefloat
convert values from one type to another. See if you can enter different values forradius
each time you run the script.
- 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.
Be prepared to ask and answer questions about the material in lecture.