Lab 1

Introduction:

The purpose of this lab is to familiarize yourself with many of the tools you will be using throughout the semester. There are two parts to the lab, one part should be done during recitation. The second part needs to be done outside of recitation.

Part 1: Learning IDLE and the Debugger

Opening the debugger:

  1. Open up IDLE, and click on "Debug" on the toolbar at the top. From the dropdown, select "Debugger" to open a new window; this is the Debugger window.
  2. If you return to Python shell window and click on the "Debug" toolbar dropdown, you should now see a checkmark next to "Debugger." Additionally, the Python shell window should now have written "[DEBUG ON]".

Using the debugger

  1. Open up a new python file window by using the CTRL+N shortcut, or by going to FILE > New Window. For the purpose of this lab, we will work with a simple python program dealing with arithmetic and simple variables. Using the debugger is very helpful, because it shows you the current value of any and every variable you have.
  2. Copy and paste the following code into your new python window, making sure to remove any tabs or spaces from the beginning of the lines: a = 5
    b = 2
    c = a + b
    c = c + a * 2
    c = c + 1
  3. Now that you have the debugger open, run the code that you just pasted in by hitting F5, or by going to Run > Run Module. Any changes you have made to a program will need to be saved before the program can be run, and so if you haven't saved already, IDLE will ask you to save your new file.
  4. Once you've run the file, the Python shell window will jump into focus, and the debugger will pop up. Keep clicking on "Step" - Python will run one line of code at a time, showing the values of the variables as your code is executed.
  5. Specifically, notice that, as your program executes, the variables a, b, and c will appear under the section named "Locals." The label "Locals" is a common shortening of "local variables" which is just a collection of every variable that exists at the current location in your code. If you also look at the middle select window, you can see that, as you click the step button, the line number will change: > '__main__'.(), line 3: c = a + b to > '__main__'.(), line 4: c = c + a * 2
  6. This will show you where you currently are within your program! Run through your program to make sure that you can follow how each variable is being computed and that each line of the Python code makes sense.
  7. NOTE: When you step to a line of code, that line of code has not yet been executed. For instance, when you are currently on line 3, you should notice that "Locals" does not contain the variable 'c' yet; only when you hit "step" again will the debugger execute that code and create the new variable. In the beginning, it might feel like you are looking at everything one step behind!

Finding errors

  1. Now that you know how to work the debugger, let's see how it responds to errors.
  2. Go to the python file you were previously working on and replace the code with the following: a = 1
    b = 0
    c = a / b
    d = c + "2"
  3. Make sure the debugger is open and once again run the program. Then go step by step through the program.
  4. As you will notice, a highlighted error message will pop up complaining about a "ZeroDivisionError" and the program will not finish running.
  5. Errors like a "ZeroDivisionError" will cause a program to crash, so you won't be able to look at what the rest of the program does until you fix it!
  6. Replace the code with the following and debug it again: a = 1
    b = 0
    c = a * b
    d = c + "2"
  7. Once again the program will crash due to an error, this time because strings (a word) can't be added to an integer (a whole number).
  8. Using the debugger in this way will let you find and correct errors as they appear.

Submitting to CourseWeb

Throughout the semester, you will be writing a lot of Python programs and submitting them to CourseWeb. To practice that, this section will go over how to submit your python program. For the instructions below, use the program you wrote in the previous section.

In addition to your program, you will also be submitting an assignment information sheet. Complete the information sheet.

  1. Run your program to ensure it is behaving the way you expect it to behave.
  2. Save the source code file(s) and exit the IDE or text editor.
  3. Zip your files and make sure the assignment information sheet is included in that zip file.
  4. Upload the file to CourseWeb.
  5. Download the file from CourseWeb and extract the contents to a different location than where the original source code files were.
  6. Does the zip file contain your assignment information sheet? Is that sheet complete?
  7. Does the zip file contain all of the .py files? Open those .py files to make sure they aren't blank. Run the source code file(s) in that .zip file. Does your program still behave the way it is supposed to?

Part 2: Access to Python 3

You will be writing a lot of Python programs. In preparation for this, part 2 of this lab has you finding computers you can use.

The computers in the classroom (5505) have Python 3 installed. In case you need to work on an assignment when the classroom is unavailable, this part of the lab asks that you find other computers that you can use. One location should be a computer lab on campus and the other can either be another computer lab or your own computer.

To find computer labs on campus, visit Pitt's Lab Locations, Hours, and Equipment page. Find a lab that is convenient for you. Visit this computer lab to ensure it has Python 3 installed. If it does not, find another computer lab.

If you would like to use your own computer, you will need to download and install Python 3. It can be downloaded from: https://www.python.org/downloads/.

Final Note:

For this lab, you do not need to write a driver program or use the two new classes in any way. However, it is a good idea to test your classes before submitting them.

Submission and Grading:

Complete the Assignment Information Sheet.

Submit your final program from part 1 and assignment information sheet (zipped into one file) to CourseWeb in the Lab 1 assignment.

The assignment is due Monday, May 18 by 9:00 am. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded before the deadline will be the one graded. If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well.

For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.