The Personal Web Pages of Chris X. Edwards
XED's Python Lessons for Beginners |
Lesson One - Messing Around Until Something Useful Happens
The first step to enriching your life with a fine programming language
like Python is having it available. If you are using Linux or a recent
Mac OS, then it's already there. If you are not, you should work on
that problem. (If you're really in a bind, this is a nice
workaround: http://www.cygwin.com/.)
At this point I'll assume that you have access to Python. How would
you know for sure? Type something like this at a terminal's command
prompt:
If you're lucky, this spits out something like:
"Programming in Python" sounds so intense, so let's start by "messing
around with Python" which is gratifying in a much more instantaneous
way. Simply type:
And you get some mess like this:
Python 2.4.2 (#1, Oct 27 2005, 10:21:56)
[GCC 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
|
Now you are not in your normal shell's interactive mode - you are in
the Python shell. It turns out that you could use this Python shell
instead of your normal shell (bash or tcsh), but that would be weird
and you'd better wait until you're really gung-ho about Python for
that.
The reason the Python shell is so great is that one can test little
bits of code to see how they work. This is a fantasticly helpful
ability when programming, debugging, and especially when learning
Python. This is why we're starting with it.
Since you may not know any Python now, you'll have nothing to test.
That's ok. The Python shell (or Python Interactive Interpreter) is
extremely handy in day to day life without much knowledge of Python at
all. I find that it makes a terrific desktop calculator.
Let's say you want to know how much time per year you waste driving to
work measured in days. Say 50 work weeks a year, 5 days per week,
.6 hour each way. Just type this:
>>> 50 * 5 * .6 * 2
300.0
|
That's pretty easy. Now here comes a little problem. Let's say you
wanted to put that in context of how many equivalent 8 hour workdays
are lost. One might assume that this would work:
The problem is, this answer is kind of wrong. Python does a great job
as a personal calculator, but you must be very careful about this
fact: Python prefers integer division. This means that if you give
Python 2 whole integers (no decimal), it will give you an answer that
is also a whole integer, and that will be inaccurate most of the time.
Fortunately, the answer to the problem is simple:
There you go. In this hypothetical situation, a person who traded a .6
hour commute for a productive bus/limo ride would get the equivalent
of 37.5 days at work to do whatever they wanted!
Python has other tricks that are helpful when using it as a
calculator. One of Python's strengths is a large and well-organized
collection of "modules" which can be imported into a Python
environment at any time on an as needed basis. Here is an example:
Nothing much seems to happen, but now you can do stuff like this:
>>> math.sin( math.pi / 2 )
1.0
|
Note that here you don't need to say 2.0 because pi is a decimal
number, and therefore so is the result. There are lots of things the
math module can do, but the most intersting thing to explore now is
this Python trick:
Don't worry if you don't think you'll ever need this kind of fancy
math. The point here is the built-in help facility that can often help
you out when you don't know what your options are or how to use them.
Play around with the Python interpreter and see if you can convince
yourself that it would sometimes make a nice calculator.
LWM Home |
Python Lessons Index |
Lesson 2 |
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
|
Chris X. Edwards ~ March 2006
|