Exercises

Command-Line Exercises

  1. Simple Commands. Enter the following one-line commands to Python:

    • copyright
    • license
    • credits
    • help
  2. Simple Expressions. Enter one-line commands to Python to compute the following:

    • 12345 + 23456
    • 98765 - 12345
    • 128 * 256
    • 22 / 7
    • 355 / 113
    • (18-32)*5/9

    • -10*9/5+32

IDLE Exercises

  1. Create an Exercises Directory. Create a directory (or folder) for keeping your various exercise scripts. Be sure it is not in the same directory in which you installed Python.

  2. Use IDLE's Shell Window. Start IDLE. Refer back to the exercises in the section called “Command-Line Interaction”. Run these exercises using IDLE.

  3. Use IDLE's File WindowStart IDLE. Note the version number. Use New Window under the File menu to create a simple file. The file should have the following content.

    """ My First File """
    print __doc__

    Save this file in your exercises directory; be sure the name ends with .py. Run your file with the Run Module menu item in the Run menu, usually F5.

Script Exercises

  1. Print Script. Create and run Python file with commands like the following examples: print 12345 + 23456; print 98765 - 12345; print 128 * 256; print 22 / 7.

  2. Another Simple Print Script. Create and run a Python file with commands like the following examples: print "one red", 18.0/38.0; print "two reds in a row",(18.0/38.0)**2.

  3. Interactive Differences. First, run IDLE (or Python) interactively and enter the following "commands": copyright, license, credits. These are special global objects that print interesting things on the interactive Python console.

    Create a Python file with the three commands, each one on a separate line: copyright, license, credits. When you run this, it doesn't produce any output, nor does it produce an error.

    Now create a Python file with three commands, each on a separate line: print copyright, print license, print credits.

    Interestingly, these three global variables have different behavior when used in a script. This is rare. By default, there are just three more variables with this kind of behavior: quit, exit and help.

  4. Numeric Types. Compare the results of 22/7 and 22.0/7. Explain the differences in the output.