Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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