[ Team LiB ] |
3.1 Interactive CodingPerhaps the simplest way to run Python programs is to type them at Python's interactive command line. There are a variety of ways to start this command line—in an IDE, from a system console, and so on. Assuming the interpreter is installed as an executable program on your system, the most platform-neutral way to start an interactive interpreter session is usually to type just "python" at your operating system's prompt, without any arguments. For example: % 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. >>> Here the word "python" is typed at your system shell prompt, to begin an interactive Python session (the "%" character stands for your system's prompt, not your input). The notion of a system shell prompt is generic, but varies per platform:
If you have not set your shell's PATH environment variable to include Python, you may need to replace the word "python" with the full path to the Python executable on your machine. For instance, on Windows, try typing C:\Python22\python (or C:\Python23\python for Version 2.3); on Unix and Linux, /usr/local/bin/python (or /usr/bin/python) will often suffice. Once the Python interactive session starts, it begins by printing two lines of informational text (which we normally omit in our examples to save space), and prompts for input with >>> 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: % python >>> print 'Hello world!' Hello world! >>> print 2 ** 8 256 Again, don't worry about the details of the print statements shown here yet (we'll start digging into syntax in the next chapter). In short, they print a Python string and an integer, as shown by the output lines that appear after each >>> input line. When working interactively like this, we can type as many Python commands as we like; each is run immediately after entered. Moreover, because the interactive session automatically prints the results of expressions typed, we don't usually need to say "print" explicitly at this prompt: >>> lumberjack = 'okay' >>> lumberjack 'okay' >>> 2 ** 8 256 >>> use Ctrl-D or Ctrl-Z to exit % Here, the last two lines typed are expressions (lumberjack and 2 ** 8), and their results are displayed automatically. To exit an interactive session like this one and return to your system shell prompt, type Ctrl-D on Unix-like machines; on MS-DOS and Windows systems, type Ctrl-Z to exit. In the IDLE GUI discussed later, either type Ctrl-D, or simply close the window. Now, we're not doing much in this session's code: we type Python print and assignment statements, and a few expressions, which we'll study in detail later. The main thing to notice is that the code entered is executed immediately by the interpreter, when the Enter key is pressed at the end of the line. For instance, after typing the first print statement at the >>> prompt, the output (a Python string) is echoed back right away. There's no need to run the code through a compiler and linker first, as you'd normally do when using a language such as C or C++. As you'll see in later chapters, you can also run multiline statements at the interactive prompt; the statement runs immediately after you've entered all its lines. Besides typing python in a shell window, you can also begin similar interactive sessions by starting IDLE's main window, or on Windows via the Start button menus for Python and select the Python (command-line) menu option as shown in Figure 2-1. Both spawn a >>> prompt with equivalent functionality—code is run as it is typed.
3.1.1 Using the Interactive PromptAlthough simple to use, there are a few ways that the interactive prompt seems to trip up beginners:
|
[ Team LiB ] |