Sei sulla pagina 1di 4

CHAPTER 12.

DOCUMENTING YOUR CODE 96

modify how they are formatted (having a space between the docstring
and code, putting end quotes on a separate line, etc.) Be careful if
you desire to not follow the Style Guide.
Not only will it make your life easier when you finish your project,
but it also makes your code easier to read and follow. (Wish the people
at my work could learn how to document their code. Even just a few
comments explaining what a function does would help. :) )
Chapter 13

Making a Program

After having used it for many years now, I’ve come to find that Python
is an extremely capable language, equal in power to C++, Java, et al.
If you don’t need the "finesse" the major languages provide, I highly
recommend learning Python or another dynamic language like Ruby.
You’ll program faster with fewer errors (like memory management)
and can harness the power of a built-in GUI for rapid prototyping of
applications. You can also use these languages for quick scripts to
speed repetitive tasks. Plus, they are inherently cross-platform so you
can easily switch between operating systems or find a larger market for
your programs. Heck, Python is used extensively by Google, NASA,
and many game publishers, so it can’t be all that bad.
One of the biggest complaints people have is the forced use of white
space and indentation. But if you think about it, that’s considered a
"good coding practice"; it makes it easier to follow the flow of the
program and reduces the chance of errors. Plus, since brackets aren’t
required, you don’t have to worry about your program not working
because you forgot to close a nested if statement. After a few days of
using Python, you won’t even notice, though I imagine you’ll notice
how "sloppy" other languages look.
Now, on with the show...

97
CHAPTER 13. MAKING A PROGRAM 98

13.1 Making Python Do Something


So far I’ve talked about how Python is structured and how it differs
from other languages. Now it’s time to make some real programs.
To begin with, Python programs are comprised of functions, classes,
modules, and packages.
1. Functions are programmer created code blocks that do a specific
task.
2. Classes are object-oriented structures that I’ll talk about later;
suffice to say they are pretty powerful structures that can make
programming life easier, though they can be difficult to learn
and wield well.
3. Modules are generally considered normal program files, i.e. a file
comprised of functions/classes, loops, control statements, etc.
4. Packages are programs made up of many different modules.
In reality, I consider modules and packages to be "programs". It just
depends on how many separate files are required to make the program
run. Yes, it is possible to have a single, monolithic file that controls
the entire program but it’s usually better to have different parts in
different files. It’s actually easier to keep track of what’s going on and
you can cluster bits of code that have common goals, e.g. have a file
that holds library functions, one that handles the GUI, and one that
processes data entry.
An important module to know is the Python standard library.
There are two versions: Python 2.6 and Python 3.0. The library
is a collection of common code blocks that you can call when needed.
This means you don’t have to "rebuild the wheel" every time you want
to do something, such as calculate the tangent of a function. All you
have to do is import the portion of the standard library you need, e.g.
the math block, and then use it like regular Python code. Knowing
what’s in the standard library separates the good programmers from
the great ones, at least in my book.
That being said, let’s make a simple Python program. This pro-
gram can be made in IDLE (the standard Python programming envi-
ronment that comes with the Python install), a third-party program-
ming environment (such as SPE, Komodo, Eclipse, BoaConstructor,
CHAPTER 13. MAKING A PROGRAM 99

etc.), or a simple text editor like Window’s Notepad, vim, emacs, BBE-
dit, etc. (More programs can be found in the appendix on page 168).
Listing 13.1: First example program
def s q u a r e ( x ) : #d e f i n e t h e f u n c t i o n ; " x " i s t h e
argument
return x ∗ x #p a s s b a c k t o c a l l e r t h e
s q u a r e o f a number

f o r y in r a n g e ( 1 , 1 1 ) : #c y c l e t h r o u g h a l i s t o f
numbers
print s q u a r e ( y ) #p r i n t t h e s q u a r e o f a number
Listing 13.1 is about as simple as it gets. First we define the
function called square() (the parenthesis indicates that it’s a function
rather than a statement) and tell it that the argument called “x” will
be used for processing. Then we actually define what the function
will do; in this case, it will multiply “x” times itself to produce a
square. By using the keyword return, the square value will be given
back to whatever actually called the function (in this case, the print
statement).
Next we create a for loop that prints the squared value of each
number as it increases from 1 to 11. This should be fairly easy to
follow, especially with the comments off to the side. Realize that
many programs you’ll see aren’t commented this much; quite often,
the programs aren’t commented at all. I like to think that I have a
sufficient amount of documentation in my code (you’ll see later) so
that it’s pretty easy for even new programmers to figure out what’s
going on.
To run this program, simply save it with a filename, such as “first_program.py”.
Then, at the command prompt simply type “python first_program.py”.
The results should look like this:
Listing 13.2: First example program output
$python f i r s t _ p r o g r a m . py #your command prompt
by d i f f e r from "$"
1
4
9

Potrebbero piacerti anche