12.07.2015 Views

Is Python a

Is Python a

Is Python a

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using the Interactive PromptAlthough the interactive prompt is simple to use, there are a few tips that beginnerswill need to keep in mind:• Type <strong>Python</strong> commands only. First of all, remember that you can only type<strong>Python</strong> code at the <strong>Python</strong> prompt, not system commands. There are ways torun system commands from within <strong>Python</strong> code (e.g., with os.system), but theyare not as direct as simply typing the commands themselves.• print statements are required only in files. Because the interactive interpreterautomatically prints the results of expressions, you do not need to type completeprint statements interactively. This is a nice feature, but it tends to confuse userswhen they move on to writing code in files: within a code file, you must useprint statements to see your output, because expression results are not automaticallyechoed. Remember, you must say print in files, but not interactively.• Don’t indent at the interactive prompt (yet). When typing <strong>Python</strong> programs,either interactively or into a text file, be sure to start all your unnested statementsin column 1 (that is, all the way to the left). If you don’t, <strong>Python</strong> mayprint a “SyntaxError” message. Until Chapter 10, all statements you write will beunnested, so this includes everything for now. This seems to be a recurring confusionin introductory <strong>Python</strong> classes. A leading space generates an error message.• Watch out for prompt changes and compound statements. We won’t meet compound(multiline) statements until Chapter 10, but, as a preview, you should knowthat when typing lines 2 and beyond of a compound statement interactively, theprompt may change. In the simple shell window interface, the interactive promptchanges to ... instead of >>> for lines 2 and beyond; in the IDLE interface, linesafter the first are automatically indented. In either case, inserting a blank line(done by hitting the Enter key at the start of a line) is needed to tell interactive<strong>Python</strong> that you’re done typing the multiline statement; by contrast, blank linesare ignored in files.You’ll see why this matters in Chapter 10. For now, if you happen to comeacross a ... prompt or a blank line when entering your code, it probably meansthat you’ve somehow confused interactive <strong>Python</strong> into thinking you’re typing amultiline statement. Try hitting the Enter key or a Ctrl-C combination to getback to the main prompt. The >>> and ... prompts can also be changed (theyare available in the built-in module sys), but I’ll assume they have not been inthe book’s example listings.System Command Lines and FilesAlthough the interactive prompt is great for experimenting and testing, it has one bigdisadvantage: programs you type there go away as soon as the <strong>Python</strong> interpreterexecutes them. The code you type interactively is never stored in a file, so you can’tSystem Command Lines and Files | 37

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

Saved successfully!

Ooh no, something went wrong!