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.

What <strong>Python</strong> AddsThe one new syntax component in <strong>Python</strong> is the colon character (:). All <strong>Python</strong> compoundstatements (i.e., statements that have statements nested inside them) followthe same general pattern of a header line terminated in a colon followed by a nestedblock of code usually indented underneath the header line, like this:Header line:Nested statement blockThe colon is required, and omitting it is probably the most common coding mistakeamong new <strong>Python</strong> programmers—it’s certainly one I’ve witnessed thousands oftimes in <strong>Python</strong> training classes. In fact, if you are new to <strong>Python</strong>, you’ll almost certainlyforget the colon character very soon. Most <strong>Python</strong>-friendly editors make thismistake easy to spot, and including it eventually becomes an unconscious habit (somuch so that you may start typing colons into your C++ code, too, generating manyentertaining error messages from your C++ compiler!).What <strong>Python</strong> RemovesAlthough <strong>Python</strong> requires the extra colon character, there are three things programmersin C-like languages must include that you don’t generally have to in <strong>Python</strong>.Parentheses are optionalThe first of these is the set of parentheses around the tests at the top of the statement:if (x < y)The parentheses here are required by the syntax of many C-like languages. In<strong>Python</strong>, they are not—we simply omit the parentheses, and the statement works thesame way:if x < yTechnically speaking, because every expression can be enclosed in parentheses,including them will not hurt in this <strong>Python</strong> code, and they are not treated as an errorif present. But don’t do that: you’ll be wearing out your keyboard needlessly, andbroadcasting to the world that you’re an ex-C programmer still learning <strong>Python</strong> (Iwas once, too). The <strong>Python</strong> way is to simply omit the parentheses in these kinds ofstatements altogether.End of line is end of statementThe second and more significant syntax component you won’t find in <strong>Python</strong> code isthe semicolon. You don’t need to terminate statements with semicolons in <strong>Python</strong>the way you do in C-like languages:x = 1;204 | Chapter 10: Introducing <strong>Python</strong> Statements

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

Saved successfully!

Ooh no, something went wrong!