sep 8
Questions?
Next assignment, from chap 2 & 3, is posted; due Tuesday.
python on windows
Discuss installing and running Python 2.7 from python.org
under Windows.
The IDLE editor/python application will be fine for this term;
just understand that you can open, edit, and save files in
any folder, and that when you run them, the python interactive
prompt is also running in that folder. (That will matter when
we start reading/writing/loading other files from within python.)
It's also helpful for this sort of work to make sure that
you can see the file extensions. Sometimes a file that's
really named "my_paper.doc" shows up on the desktop as just
"my_paper", with Windows hiding the .doc extension. You
can set that as in the Folder View preferences. In XP Pro
use (Start >> Settings >> Folder Options ; View tab;
uncheck "Hide extensions for known file types").
By default (at least in my testing) python.org's python
for Windows download installs itself in C:\Python27\ and the
python program itself is at C:\Python27\python.exe .
At the command prompt then, you can get to by using its
full location (though you don't need the .exe).
On Windows XP Pro :
Start >> Programs >> Accessories >> Command Prompt
brings up the black terminal. From there, typing
/Python27/python
at the > prompt will run python.
The computer stores a list of directories where it
will look for program that you type at the command prompt;
this list is called the "PATH". It's possible to edit
the path so that it looks in /Python27/ by default, so
that you can just type "python" and have it work.
The PATH is one of the "environment variables".
On Windows XP Pro, those are changed from
Start >> Settings >> Control Panel ; choose "classic"
click System, then Advanced.
The Environment button is at the bottom of the window.
Choose PATH from the variables, and click "Edit".
Append ";/Python27" (no quotes) to the text string.
(It uses semicolons between the directories to search.)
Or as I said, just use IDLE, the python.org IDE
(integrated development environemnt) that gives
both an editor and a "run python" command.
Another option is to install cygwin.org's system,
from which you can choose many unix things to run
under windows, including bash (the shell), xinit,
ssh (for remote login), xinit (X11 pop-up windows
from the remote machines), xterm (a terminal),
python itself, emacs, and lots of other cool stuff.
chap 2
software development cycle
- understand problem (can be harder than you think)
- create specs : inputs? outputs? (be very specific)
- choose algorithm: what does it do?
- implement (write the code)
- test and debug (again, can be harder than you expect)
- maintain (if used over time, needs will usually change)
- aside: python code in wiki pages
- does this have any bugs? Hmmm.
first_name # underbar convention
FirstName # camelcase convention
3people # WRONG - can't start with number
site43_bldg2 # OK - can have embedded numbers
n="Jim Mahoney" # BAD - later will be hard to tell 'n' means.
- variables (those names) have values (numbers, strings, lists, ...).
- expressions let you manipulate the values
- example1: (2.0 + 0.1/20)**3
- example2: "Mr " + first_name + " " + last_name
- operators: + - / * ** ... and others
- types: int float (i.e. decimal) strings ... and others
$ python
>>> type(1)
<type 'int'>
>>> type(1.0)
<type 'float'>
- tuples
- fairly specific to the python language
$ python
>>> a = 1,2,3
(1, 2, 3)
- Try these to see what they do :
>>> a * 2
>>> 2 * a
>>> a + 2
>>> 2 + a
>>> type(a)
interest_rate = 3.0 # percent
start_amount = amount = 100.00
periods = 10
for i in range(periods):
amount = amount * (1 + interest_rate/100.0)
print amount, " at ", \
interest_rate, "% ", \
periods, " times is ", amount
# (The "\" character at the end of a line
# continues it on the next line.)
We will look at the pieces of this more
carefully soon; for now, the point is to
have a first exposure and to get the general
idea. We'll continue to fill in the details
and go over specific pieces, like range().
chapter 3
- data types
- internal representations
- type() function in python
- float numbers are approximate - finite number of bits for storage
>>> 0.3
0.29999999999999999
- but in python, integers grow as needed ... and become another type
>>> 2**10, 2**20, 2**30, 2**40
(1024, 1048576, 1073741824, 1099511627776L)
(Notice the "L" at the end of the last number: that's a new "long" type.)
Discussion: why is the switch near 2**30 ? why not always use "long" types for integers? why have different storage for floats and ints?
math library
First a bit about names, namespaces, and dir()
$ python
>>> dir() # what
['__builtins__', '__doc__', '__name__']
>>> dir(__builtins__)
... long list of built-in things ...
>>> from math import *
>>> dir()
['__builtins__', '__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
The math functions like sin(), cos(), sqrt(), aren't available in python by default. Instead, you must "import" them from a "module" called "math".
The book does this instead :
>>> import math
>>> math.sqrt(3.0)
which leaves things from the math module with "math." before their names. If you do things that way, sin(pi/2) is math.sin(math.pi/2).
accumulating results in a loop
sum = 0
numbers = [1, 10, 20, 18, 17, 34, 22]
for number in numbers:
sum = sum + number
print "The sum is ", sum
Look at this carefully to understand what's going on.
In class: run this. Then put in more print statements
to see exactly what is going on during the loop.
(The loop is the "for" statement and the indented part
after it.)