Python IDLE Tutorial
Introduction to Programming and Algorithm Design (COP-1000)

Objectives

  • Become familiar with the Python Integrated Development Environment (IDLE)
  • Understand the steps in the software development process
  • Recognize and correct coding syntax errors
  • Understand Python syntax for variable declaration and console I/O
  • Save program source code
  • Execute a program
  • Use a Help facility

The Problem

We have several measurements taken in inches, and we would like to write a program to convert these measurements to their equivalent in centimeters. To do this, we will follow the steps in the program development cycle:


Formulate the Problem (Step 1)

In this step, figure out exactly what the problem is, and what will solve it.

This problem seems straightforward: both inches and centimeters are units of length so we should be able to convert one to another, given the proper conversion factor.

This program should let the user input a value in inches, and output the equivalent value in centimeters. Since both the input and output will be variable, we will assign them the descriptive identifiers inches and centimeters. To do this, the program will need to convert the input provided to the output necessary. We look up the conversion factor to find that 1 inch = 2.54 centimeters, so to obtain centimeters, we will multiply inches by 2.54.


Design a Solution (Step 2)

The overall structure of the program to solve the problem is put together here, explaining how the program will work.  Flowcharts and pseudocode are two design tools that can be used to define the specific sequence of steps – the algorithm – the program will follow.

Many programs follow the input-process-output (IPO) pattern. Our algorithm, in pseudocode, might look like this:

Input the length in inches
Calculate centimeters as 2.54 * inches
Display centimeters

Implement the Design (Step 3)

This step is where the design is translated into a computer programming language. For our purposes we will use Python. The program will look like this when we finish coding it:

# in2cm.py -- January 10, 2009 -- C. J. Craig
#     Converts inches to centimeters

def main():
    print "This program converts inches to centimeters."
    print "(1 inch = 2.54 centimeters)"
    print
    inches = input("Enter length in inches: ")
    centimeters = 2.54 * inches
    print "That is", centimeters, "centimeters."

main()
Compare the lines of Python code with the lines of pseudocode in the algorithm for similarities and differences. (Note that everything following a # on a line is a comment, and not part of the program itself.)
Coding. Use the Python IDLE to enter the above program code by following these steps:
  1. From the Start menu, open the Python IDLE program, which will open the Python shell, an interactive window in which you can type in Python instructions directly and have them immediately interpreted and executed. (Note: Instead of opening in the shell window, IDLE may open with an edit window, depending on how it was configured when it was installed. If this is the case, you can skip step 2, below.)
  2. From the File menu, select New Window which will open an edit window, in which you can code a complete program, edit and save it.
  3. From the File menu, select Save as...
    1. Type in2cm.py in the File name box
    2. Change the Save in: text box to the location where you want to save your source file. You can save it to the hard drive, a floppy diskette, zip disk, or a thumb drive.
    3. Click OK
  4. Now type the above program in the edit window, using the normal Windows editing keys (backspace, arrow keys, etc.) and mouse pointer. Use your name, not "C. J. Craig". Notice that as you enter the code, the colors indicate what type of code you are entering:
    1. Blue: a Python definition

    2. Red: a comment
    3. Green: a string constant
    4. Orange: a keyword in Python
    5. Black: everything else

  5. Save your work periodically by choosing Save from the File menu, or clicking on the diskette icon on the button bar
  6. When you have finished entering the program, save it one more time. You are now ready to invoke the interpreter, which will process your program one line at a time, converting your source code into machine language object code and executing it
  7. From the Run menu, choose Run module
  8. The program will begin to execute in the Python Shell, by displaying the prompt "Length in inches:"
  9. As the program executes, the interpreter may encounter some syntax errors. Depending on the type of error, it may identify them in by highlighting a line in the edit window, or by identifying the offending line during execution in the Python shell.
  10. In either case, compare that line with the program above and:
    1. Edit the line as necessary
    2. Resave your source code
    3. Run the program again
    4. Repeat until you have eliminated all syntax errors. If you have a syntax error you just can't find, ask your instructor.
  11. When you have corrected all your syntax errors, you have a clean compile, and are ready to start testing your program for logic errors.
Using Help. To correct syntax errors, you may need to refer to the Python's built-in Help facility. A hallmark of a good programmer is familiarity with Help – the ability to find what you are looking for quickly. The Python IDLE has extensive information on the Python language.
  1. From the Help menu, select Python Docs
  2. Help will open with the content window, with links to major topics, including the Python language reference and a tutorial. This is useful if you are just browsing, but if you are trying to find information on a specific item, it's better to use the index and/or the find feature:
  3. After finding and reviewing the information, you can close the help facility, or leave it open for future reference and return to the IDE.

Testing the Solution (Step 4)

Testing your program will require running it, inputting test values, observing the results, and comparing these results with what you expected. If the observed results are different than the expected results, your algorithm – and therefore the program on which it was based – has a semantic (logic) error.

  1. Write down several test values as input, and manually determine the correct conversions. For example:
    1. 1 inch = 2.54 centimeters

    2. 22.5 inches = 57.15 centimeters
    3. 72 inches = 182.88

  2. From the edit window, run your program (Run | Run Module)
  3. Enter test values and observe the results, comparing them to what you expected.
  4. Rerun the program to test other values. Did you get what you expected?
  5. If you obtain unexpected results, ask your instructor to take a look at your source code, since this example should contain no logic errors.

Maintaining the Program (Step 5)

This step is really a never-ending process, as you design and implement improvements to the program in response to changes in the needs of the user. How could this program be enhanced?

 Updated: 08.25.2009