See All Titles |
![]() ![]() The Shell EnvironmentThe Python language is wrapped within a shell development environment. The main component of this shell is a command line interpreter, which is perfect for practicing, learning, and testing your programs. Command Line InterpreterThe command line interpreter is the heart of Python's shell environment. To access the command line interpreter, you need to switch to the prompt of your operating system. The following examples presume that the python directory is in your system's path environment variable. On a UNIX system, you must type $ python If you are running MS Windows, just say c:\> python Note that in both cases, you just need to type the word python; the rest is part of the shell prompt. The Python for Windows installation also provides access to the command line interpreter by clicking its icon on the Start menu (see Figure 2.1). Figure 2.1. By clicking on the Python (command line) icon, you gain access to the shell environment.![]() After the command line interpreter is loaded (see Figure 2.2), you can start coding your own programs. Figure 2.2. Python's command line interface is now ready to use.![]() Instead of using the command line interpreter, you can also use a graphical user interface called IDLE (see Figure 2.3). Figure 2.3. IDLE is Python's GUI interpreter.![]() As you can see by looking at the coding area in both Figures 2.2 and 2.3, the interpreter's primary prompt is a >>>. Let's start interacting with Python by running a variation of the standard "hello world" program. >>> print "Hello Python World" Hello Python World The previous example demonstrates that the screen is the standard output device for commands that are typed in the interpreter's prompt. Next, another example is demonstrated. Note that the first command doesn't print anything because it is just an assignment operation. The result of the operation is passed to and stored at the informed variable. On the other hand, the second command has its output redirected to the standard output, which enables you to see the result of the operation. >>> alfa = 3 + 2 >>> alfa * 4 20 Python's syntax automatically indicates when a statement requires a subblock. The interpreter's secondary prompt … means that the next line is a continuation from the current line and not a new line. In some cases, when you finish entering a multiline statement, you need to type ENTER at the beginning of the first line located after the end of the code block. By doing so, you will return to the primary prompt. Four basic situations that use a secondary prompt are as follows:
Tip
If you need to quit the interpreter while working on UNIX or MS Windows systems, press CTRL+D or CTRL+Z, respectively.
|
Index terms contained in this sectionaccessingcommand line interpreter 2nd applications hello world command line interpreter 2nd 3rd hello world program interpreters command line 2nd 3rd launching command line interpreter 2nd opening command line interpreter 2nd programs hello world prompts secondary running command line interpreter 2nd secondary prompts shell environment 2nd 3rd software hello world statements requirements for subblock subblocks requirements in statements syntax statements requiring subblocks |
© 2002, O'Reilly & Associates, Inc. |