12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Here, the last two lines typed are expressions (lumberjack and 2**8), and theirresults are displayed automatically. To exit an interactive session like this one andreturn to your system shell prompt, type Ctrl-D on Unix-like machines; on MS-DOSand Windows systems, type Ctrl-Z to exit. In the IDLE GUI discussed later, eithertype Ctrl-D or simply close the window.Now, we didn’t do much in this session’s code—just typed some <strong>Python</strong> print andassignment statements, and a few expressions, which we’ll study in detail later. Themain thing to notice is that the interpreter executes the code entered on each lineimmediately, when the Enter key is pressed.For instance, when we typed the first print statement at the >>> prompt, the output(a <strong>Python</strong> string) was echoed back right away. There was no need to create a sourcecodefile, and no need to run the code through a compiler and linker first, as you’dnormally 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; such a statement runsimmediately after you’ve entered all of its lines.Besides typing python in a shell window, you can also begin similar interactive sessionsby starting IDLE’s main window or, on Windows, by selecting the “<strong>Python</strong>(command-line)” menu option from the Start button menu for <strong>Python</strong>, as shown inFigure 2-1. Both spawn a >>> prompt with equivalent functionality—code is run as itis typed.Testing Code at the Interactive PromptBecause code is executed immediately, the interactive prompt turns out to be a greatplace to experiment with the language. It will be used often in this book to demonstratesmaller examples. In fact, this is the first rule of thumb to remember: if you’re ever indoubt about how a piece of <strong>Python</strong> code works, fire up the interactive command lineand try it out to see what happens. Chances are good that you won’t break anything.(You need to know more about system interfaces before you will become dangerous.)Although you won’t do the bulk of your coding in interactive sessions (because thecode you type there is not saved), the interactive interpreter is also a great place to testcode you’ve put in files. You can import your module files interactively and run testson the tools they define by typing calls at the interactive prompt. More generally, theinteractive prompt is a place to test program components, regardless of their source—you can type calls to linked-in C functions, exercise Java classes under Jython, andmore. Partly because of this interactive nature, <strong>Python</strong> supports an experimental andexploratory programming style you’ll find convenient when getting started.36 | Chapter 3: How You Run Programs

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!