You are here: Home > Dive Into Python > Your First Python Program | << >> | ||||
Dive Into PythonPython from novice to pro |
You know how other books go on and on about programming fundamentals and finally work up to building a complete, working program? Let's skip all that.
Here is a complete, working Python program.
It probably makes absolutely no sense to you. Don't worry about that, because you're going to dissect it line by line. But read through it first and see what, if anything, you can make of it.
Example 2.1. odbchelper.py
If you have not already done so, you can download this and other examples used in this book.
def buildConnectionString(params): """Build a connection string from a dictionary of parameters. Returns string.""" return ";".join(["%s=%s" % (k, v) for k, v in params.items()]) if __name__ == "__main__": myParams = {"server":"mpilgrim", \ "database":"master", \ "uid":"sa", \ "pwd":"secret" \ } print buildConnectionString(myParams)
Now run this program and see what happens.
In the ActivePython IDE on Windows, you can run the Python program you're editing by choosing -> (Ctrl-R). Output is displayed in the interactive window. |
In the Python IDE on Mac OS, you can run a Python program with -> (Cmd-R), but there is an important option you must set first. Open the .py file in the IDE, pop up the options menu by clicking the black triangle in the upper-right corner of the window, and make sure the option is checked. This is a per-file setting, but you'll only need to do it once per file. |
On UNIX-compatible systems (including Mac OS X), you can run a Python program from the command line: python odbchelper.py |
<< Summary |
| 1 | 2 | 3 | 4 | 5 | 6 | |
Declaring Functions >> |