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.
- 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
- 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.
- Start the Python shell.
- At the prompt, import the math module:
import math
- 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
- Exit the Python shell, then restart it.
- Import the math module again, this time using this command:
from math import *
- Now try these examples:
pow(sqrt(2), 2)
sin(e)
cos(pi)
- 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:
- You should be in Terminal, at the $ prompt (not in Python)
- Run this command:
pip install --user graphics.py
- 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.
- To test your installation:
- Start the Python shell
- At the prompt, enter
import graphics
- If you get the shell prompt back, it worked! If there's an error message, let me know right away.
- Exit the Python shell (enter
quit()
or just hitCtrl-D
)
- Installing graphics.py on your own computer:
pip install graphics.py
- 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.
- 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
- Open area_m.py in an editor
and rewrite it to use
pi
from themath
module instead of the variable we used before. On the first line, import the math module like this:
from math import pi
- 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.
- Now add this line to the end of your script:
Run this and record the results and any errors. Why do you think an error might have occurred? Write down your idea.print(cos(pi))
- 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
- Now let's try out the
graphics
module. Start the Python shell and enterimport graphics
.
- If you get the shell prompt back, it worked! If there's an error
message, let me know right away.
- 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() - Now download and run these two programs: simple_graphics.py,
graphics_test.py.
- 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.