CSci 157 - Lab 3

Purpose: to continue becoming familiar with scripts, programs, using imported modules, and get a sneak peek at the graphics library module.

Resources: When we use the graphics package, it may help to have a look at this tutorial.

Part 1. Checking your setup

You'll be recording results to turn in today, so you'll need paper. If you need scratch paper, look on the shelf above the printer. Please use the blank side, and be sure to include your name!

Start the Terminal. You will be in your home directory.

  1. Enter the command printenv PATH and compare the output to the text below; make a note of any differences. If there are no differences, record "Identical PATH output"

    /usr/local/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/./:.:/usr/share/java/objectdraw.jar:/usr/share/java/stdlib.jar:/usr/lib/jvm/java-16-oracle/bin:/usr/lib/jvm/java-16-oracle/db/bin
     
  2. In a web browser,

Part 2. Using the math module

Let's start by trying out the "Python Math Bloopers" from this week's notes.

  1. Start the Python shell.
     
  2. At the prompt, import the math module: import math
     
  3. Evaluate each of the expressions from the page of "Using Numbers in Pythons" handout. Record the results you get, and give the type of the result as well. If you get an error message, record just the last line.

    I've repeated them here for convenience, plus added a few new ones to try:
     
    • 3 * ( 1 / 3 )
    • 3 * ( 1 // 3 )
    • math.pow(math.sqrt(2), 2)
    • 2 - math.pow(math.sqrt(2), 2)
    • (2 - math.pow(math.sqrt(2), 2)) * 10e20
    • '0' + '4'
    • '0' + 4
    • '0' + '4' - '3'
    • '00' + '7'
    • '0' * 2 + '7'
    • '0' * (2 + 7)  # what does this one say about order of operations?
    • '0' * 100
       
  4. Exit the Python shell, then restart it.
     
  5. Import the math module again, this time using this command:
    from math import *
  6. Now try these examples:
    1. pow(sqrt(2), 2)
    2. sin(e)
    3. cos(pi)
       
  7. Exit the Python shell before continuing to the next part.

Part 3. Installing a new module

For the next few explorations to work, you need to install a new module called graphics.py.  Modules like math come built-in with in the Python Standard Library, but anyone can develop a Python module and contribute it to the software development community. The graphics module was developed by Prof. Zelle and made freely available to anyone who wants to use it. The Python community includes thousands of modules for anyone to use in their software development projects, and we can use a tool called "pip" ("package installer for Python") to install this module and any others we need.

Here's how to do it:

  1. You should be in Terminal, at the $ prompt (not in Python)
  2. Run this command: pip install --user graphics.py
  3. It worked if you see something like this:
    Collecting graphics.py
      Downloading graphics.py
    Installing collected packages: graphics.py
      Running setup.py install for graphics ... done
    Successfully installed graphics-1.0
    You are using pip version 21.3.1, however version 22.3.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.

    If it doesn't seem to have worked, call me over and we'll fix it.

  4. To test your installation:
    1. Start the Python shell
    2. At the prompt, enter import graphics
    3. If you get the shell prompt back, it worked! If there's an error message, let me know right away.
    4. Exit the Python shell (enter quit() or just hit Ctrl-D)
Resources to look at later:
  1. Installing graphics.py on your own computer: pip install graphics.py
  2. Graphics Module documentation

Part 4. Scripts and Demonstration

In this part we will try writing more scripts and programs. Make sure to change the directory to your CS157 folder.

  1. Make a copy of your script area.py from Lab 2; call it area_m.py. Here's an easy way to do it in the Terminal:
    cp area.py area_m.py
  2. Open area_m.py in an editor and rewrite it to use pi from the math module instead of the variable we used before. On the first line, import the math module like this:
    from math import pi
  3. Run both the original script and your new one; give it the same radius value for both. Note the difference in the output, if any.
     
  4. Now add this line to the end of your script:
    print(cos(pi))
    Run this and record the results and any errors. Why do you think an error might have occurred? Write down your idea.
     
  5. If the command in Part 3 didn't work for you, download the graphics module by saving this file into your CS157 folder: https://mcsp.wartburg.edu/zelle/python/graphics.py
     
  6. Now let's try out the graphics module. Start the Python shell and enter import graphics.
     
  7. If you get the shell prompt back, it worked! If there's an error message, let me know right away.
     
  8. Enter each of these statements, one at a time:
    window = graphics.GraphWin('It worked!')
    window.getMouse() # you'll then click the mouse in the new window
    window.close()
  9. Now download and run these two programs: simple_graphics.py, graphics_test.py.
     
  10. Demonstrate running the script from #6 and graphics_test.py to the instructor. Don't forget to turn in your answer sheet.

    Keep the two graphics programs around, we'll be working with them more next week.