Cover | Table of Contents | Colophon
http://www.python.org.www.python.org. It may also be found through
various other distribution channels. You may have Python already
available on your machine, especially on Linux and Unix. If
you're working on Windows, you'll
usually find Python in the Start menu, as captured in Figure 2-1 (we'll learn what these menu
items mean in a moment). On Unix and Linux, Python probably lives in
your /usr directory tree.
print 'hello world' print 2 ** 100
print statements,
which simply print a string (the text in quotes) and a numeric
expression result (2 to the power 100) to the output stream.
Don't worry about the syntax of this code
yet—for this chapter, we're interested only in
getting it to run. We'll explain the
print statement, and why you can raise 2 to the
power 100 in Python without overflowing, in later parts of this book.print statements show up
somewhere on your computer—by default, usually in the same
window you were in when you ran the program:hello world 1267650600228229401496703205376
D:\temp>python script1.py
hello world
1267650600228229401496703205376www.python.org, get with the ActivePython
distribution, and have automatically in most Linux machines. If
you've found a preinstalled version of Python on
your machine, it's probably CPython as well, unless
your company is using Python in very specialized ways.% python
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>% python
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>>>> when it's waiting
for you to type a new Python statement or expression. When working
interactively, the results of your code are displayed after the
>>> lines—here are the results of
two Python print statements:print 2 ** 8 # Raise to a power. print 'the bright side ' + 'of life' # + means concatenation.
print statements and
Python comments to the right. Text after a
# is simply ignored as a human-readable comment,
and is not part of the statement's syntax. Again,
ignore the syntax of code in this file for now. The point to notice
is that we've typed code into a file, rather than at
the interactive prompt. In the process, we've coded
a fully-functional Python script.#! trick of the prior section, or associate the
file MIME type with an application or command by editing files,
installing programs, or using other tools. See your file
explorer's documentation for more details, if clicks
do not work correctly right off the bat.# A comment import sys print sys.platform print 2 ** 100
import and two prints again
(sys.platform is just a string that identifies the
kind of computer you're working on; it lives in a
module called sys, which we must
import to load). In fact, we can run this file
from a system command line:D:\OldVaio\LP-2ndEd\Examples>c:\python22\python script4.py
win32
1267650600228229401496703205376
D:\LP-2ndEd\Examples>c:\python22\python >>> import script4 win32 1267650600228229401496703205376
>>> import script4 >>> import script4
>>> prompt). This works like all
interactive sessions—code you type here is run immediately
after you type it—and serves as a testing tool.
http://www.activestate.com.http://www.pythonware.com
for details.http://www.python.org
resources).
PythonWin
is a Windows-only IDE for Python; it is roughly like IDLE, with a
handful of useful Windows-specific extensions added in. For instance,
PythonWin has support for COM objects. It also adds basic user
interface features beyond IDLE, such as object attribute list popups.
Further, PythonWin serves as an example of using the Windows
extension package's GUI library. See http://www.activestate.com.#include <Python.h>
...
Py_Initialize( );
PyRun_SimpleString("x = brave + sir + robin");emacs text editor, you can do all your Python
editing and launching from inside the text editor itself. See the
text editor resources page at
http://www.python.org/editors for more details.execfile, os.popen,
os.system); however, these tools are beyond the
scope of the present chapter.>>> prompt), and type the expression:
"Hello World!" (including the quotes). The string
should be echoed back to you. The purpose of this exercise is to get
your environment configured to run Python. In some scenarios, you may
need to first run a cd shell command, type the
full path to the python executable, or add its
path to your PATH environment variable. If
desired, you can set it in your .cshrc or
.kshrc file to make Python permanently available
on Unix systems; on Windows use a setup.bat,
autoexec.bat, or the environment variable GUI.
See Appendix A for help with environment variable
settings.print 'Hello module world!'. Store this
statement in a file named module1.py. Now, run
this file by using any launch option you like: running it in IDLE,
clicking on its file icon, passing it to the Python interpreter
program on the system shell's command line, and so
on. In fact, experiment by running your file with as many of the
launch techniques seen in this chapter as you can. Which technique
seems easiest? (There is no right answer to this one.)>>> prompt) and import the
module you wrote in Exercise 2. Try moving the file to a different
directory and importing it again from its original directory (i.e.,
run Python in the original directory when you import); what happens?
(Hint: is there still a file named