2016 September 14,

CS 111: Programs

In this section we learn how to write a Python program in a text editor and run that program in the Python interpreter. The instructions are specific to TextWrangler running on Mac OS X, but other text editors behave similarly. We also learn the most important commands for navigating among folders in bash.

Launch the TextWrangler program by clicking its icon in your Dock. (If it's not there, then ask for help.) You should be greeted with a big, white window.

A text editor is kind of like a word processor (such as Microsoft Word), in that it is a program where you type a bunch of text. However, a text editor lacks many features, such as spell checking, mail merging, and fancy formatting. Instead it offers powerful "structural" features, such as sophisticated search-and-replace queries and the ability to understand computer code and display it in a helpful manner. Loosely speaking, you use a word processor if the document you're typing is going to be read by a human; you use a text editor if the document you're typing is going to be read by a computer. So we edit Python programs in a text editor.

A computer program is nothing more or less than a sequence of commands. Type the following commands into the TextWrangler window. In case it's not clear, the code is trying to convert a time period measured in seconds into a time period measured in years.

secondsPerYear = 60 * 60 * 24 * 365.25
secondsElapsed = 27000.341
yearsElapsed = secondsElapsed / secondsPerYear

Now press Command-S and save the file to your Desktop, giving it a name such as "secondstoyears.py". The ".py" suffix is conventional for Python programs, although not strictly necessary.

Keeping the TextWrangler window open, also open the Terminal program. Do not launch the Python interpreter in the terminal window; just stay in bash. In bash, first enter the command pwd (followed by Return). pwd means "print working directory"; it tells you where you are in the computer's file system:

Now use the ls ("list") command to list all of the files and folders here:

Assuming that you saved your secondstoyears.py program to your desktop, now do cd Desktop ("change directory" to Desktop) followed by ls:

You should see your secondstoyears.py program there. To run the program, enter python3 secondstoyears.py:

Your program ran, but you didn't ever get to see the answer. This is because you never told the program to produce any output. To fix this, go back to TextWrangler, add the line print(yearsElapsed) to the end of the program, and save the file. Then return to bash and press the up-arrow key. This takes you back to the last command, which was python3 secondstoyears.py. Press Return to issue this command again; it should now show you the answer:

When you run Python commands in the Python interpreter, the interpreter shows you the result of every command automatically. On the other hand, when you type Python commands into a file and run the file, Python does not show you the result of every command; instead, it outputs only what you tell it to, using the print command.

So there are two ways to interact with Python. The first way is to type individual commands into the Python interpreter. This is useful for when you want to do little experiments, to figure out how a particular Python command works. The second way is to type a sequence of commands into a text file, and then to execute that program as a whole using the Python interpreter. This is useful when you are writing complicated programs consisting of many commands.

Question 07A: In that program, what is the type of the variable yearsElapsed? How would you figure this out, from earlier questions I've asked? How would you test your answer, using the Python interpreter?

Question 07B: Suppose that, instead of saving the program file to your Desktop, you instead saved it in a folder "cs111" inside your Documents folder? How would you execute that program?