Cover | Table of Contents | Colophon
import this statement?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. I'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 1267650600228229401496703205376km
D:\temp> python script1.py hello world 1267650600228229401496703205376
% python Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>>
% python Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>>
PATH environment variable to include Python's install directory, you may need to replace the word "python" with the full path to the Python executable on your machine. On Windows, try typing C:\Python25\python (for version 2.5); on Unix and Linux, /usr/local/bin/python (or /usr/bin/python) will often suffice. Alternatively, you can run a change-directory command to go to Python's install directory before typing "python" (try a cd c:\python25 command on Windows, for instance).python command line entered at a system prompt. As a first example, start your favorite text editor (e.g., vi, Notepad, or the IDLE editor), and type two Python statements into a text file named spam.py:print 2 ** 8 # Raise to a power print 'the bright side ' + 'of life' # + means concatenation
print statements, and some Python #! trick discussed in 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 you must import to load). You can run this file from a system command line:D:\LP3E\Examples> c:\python25\python script4.py win32 1267650600228229401496703205376
import operations essentially load another file, and grant access to that file's contents. The contents of a module are made available to the outside world through its attributes (a term I'll define in the next section).import:D:\LP3E\Examples> c:\python25\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.
#include <Python.h> ... Py_Initialize( ); PyRun_SimpleString("x = brave + sir + robin");