CS 170 Lab 1: Getting Started with Python

Due date: 22 August 2018

This is a lab exercise to help you get started with using Python and the Idle development environment. You will learn the process we will use all semester for editing and running Python programs.

Part 1: Just Practice

This part of the lab just has you trying out Python.

  1. Log into Windows.
  2. Launch Idle by clicking Start → All Programs → Python 3.4 → Idle (Python GUI)
  3. Type the following at the prompt:
    >>> def main():
            print ('Hello, world!')
    
    >>> main()
    

    and make sure the system responds with: Hello, world!
  4. Repeat the call to main a few times to make sure it still works.
  5. If you finish this before the class moves on to Part 2, try typing various arithmetic expressions to see if they work, for example, 3+4 (3+4)*5

Part 2: Editing Programs in Files

This portion of the lab will create a Python program in a file and then run it. This is the way we will do program development most of the time for the rest of the semester.

  1. Launch Windows Explorer (not Internet Explorer) by choosing Start → All Programs → Accessories → Windows Explorer. You should always have Explorer running when you program, to keep track of your files. I like to set the View Options so that known file extensions are NOT hidden. Do this by choosing View → Details and then Tools → Folder Options → View → Apply to All Folders.
  2. Make a directory for your work for this class on your Y: drive by navigating to your Y: drive and choosing File → New → Folder and naming it something like cs170. (Blank spaces in the names of directories and files are a really bad idea and don't work in all environments.)
  3. In Idle choose File → New Window to get an editor window. Note that your Python Shell window will remain open as well. In this editor window type the text of the program futval.py. Type it exactly as written, including the comments (anything after # on a line). Make one addition by adding a line near the top with your name. Save your work frequently (using File → Save As. . . and giving it the name futval.py the first time, to name the file, then File → Save after that).
  4. When you have finished typing in the program (and saved it) you can run the program by choosing Run → Run Module. If there are error messages, read them carefully and try to understand them and fix the problem. If the program is running correctly, an initial investment of 100.00 with an annualized interest rate of 10% (meaning 0.10) should yield a 10-year value of 259.37. To try this, type 100 for the initial principal and type 0.1 (aka 10%) for the annualized interest rate. The result you should see is 259.37424601000026. We will learn how to format numeric output later in the semester.
  5. When your program runs correctly (and has your name in a comment at the top) choose File → Print Window to print the file to the printer in the lab. Hand me this file before you leave.