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.

Common Coding GotchasBefore the programming exercises for this part of the book, let’s run through some ofthe most common mistakes beginners make when coding <strong>Python</strong> statements andprograms. Many of these are warnings I’ve thrown out earlier in this part of thebook, collected here for ease of reference. You’ll learn to avoid these pitfalls onceyou’ve gained a bit of <strong>Python</strong> coding experience, but a few words now might helpyou avoid falling into some of these traps initially:• Don’t forget the colons. Always remember to type a : at the end of compoundstatement headers (the first line of an if, while, for, etc.). You’ll probably forgetat first (I did, and so have most of my 3,000 <strong>Python</strong> students over the years), butyou can take some comfort from the fact that it will soon become an unconscioushabit.• Start in column 1. Be sure to start top-level (unnested) code in column 1. Thatincludes unnested code typed into module files, as well as unnested code typedat the interactive prompt.• Blank lines matter at the interactive prompt. Blank lines in compound statementsare always ignored in module files, but when you’re typing code at theinteractive prompt, they end the statement. In other words, blank lines tell theinteractive command line that you’ve finished a compound statement; if youwant to continue, don’t hit the Enter key at the ... prompt (or in IDLE) untilyou’re really done.• Indent consistently. Avoid mixing tabs and spaces in the indentation of a block,unless you know what your text editor does with tabs. Otherwise, what you seein your editor may not be what <strong>Python</strong> sees when it counts tabs as a number ofspaces. This is true in any block-structured language, not just <strong>Python</strong>—if thenext programmer has her tabs set differently, she will not understand the structureof your code. It’s safer to use all tabs or all spaces for each block.• Don’t code C in <strong>Python</strong>. A reminder for C/C++ programmers: you don’t need totype parentheses around tests in if and while headers (e.g., if (X==1):). Youcan, if you like (any expression can be enclosed in parentheses), but they arefully superfluous in this context. Also, do not terminate all your statements withsemicolons; it’s technically legal to do this in <strong>Python</strong> as well, but it’s totally uselessunless you’re placing more than one statement on a single line (the end of aline normally terminates a statement). And remember, don’t embed assignmentstatements in while loop tests, and don’t use {} around blocks (indent yournested code blocks consistently instead).Common Coding Gotchas | 291

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

Saved successfully!

Ooh no, something went wrong!