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.

of <strong>Python</strong> 2.5. Again, though, you should use even that sparingly, and only if itsparts are all fairly simple; otherwise, you’re better off coding the full if statementform to make changes easier in the future. Your coworkers will be happy you did.Still, you may see the and/or version in code written prior to 2.5 (and in code writtenby C programmers who haven’t quite let go of their coding pasts).Why You Will Care: BooleansOne common way to use the somewhat unusual behavior of <strong>Python</strong> Boolean operatorsis to select from a set of objects with an or. A statement such as this:X = A or B or C or Nonesets X to the first nonempty (that is, true) object among A, B, and C, ortoNone if all ofthem are empty. This works because the or operator returns one of its two objects, andit turns out to be a fairly common coding paradigm in <strong>Python</strong>: to select a nonemptyobject from among a fixed-size set, simply string them together in an or expression.It’s also important to understand short-circuit evaluation because expressions on theright of a Boolean operator might call functions that perform substantial or importantwork, or have side effects that won’t happen if the short-circuit rule takes effect:if f1( ) or f2( ): ...Here, if f1 returns a true (or nonempty) value, <strong>Python</strong> will never run f2. To guaranteethat both functions will be run, call them before the or:tmp1, tmp2 = f1( ), f2( )if tmp1 or tmp2: ...You’ve already seen another application of this behavior in this chapter: because of theway Booleans work, the expression ((A and B) or C) can be used to emulate an if/elsestatement—almost.Also, notice that because all objects are inherently true or false, it’s common and easierin <strong>Python</strong> to test an object directly (if X:) rather than comparing it to an empty value(if X != '':). For a string, the two tests are equivalent.Chapter SummaryIn this chapter, we studied the <strong>Python</strong> if statement. Because this was our first compoundand logical statement, we also reviewed <strong>Python</strong>’s general syntax rules, andexplored the operation of truth tests in more depth than we were able to previously.Along the way, we also looked at how to code multiway branching in <strong>Python</strong>, andlearned about the if/else expression introduced in <strong>Python</strong> 2.5.The next chapter continues our look at procedural statements by expanding on thewhile and for loops. There, we’ll learn about alternative ways to code loops in<strong>Python</strong>, some of which may be better than others. Before that, though, here is theusual chapter quiz.246 | Chapter 12: if Tests

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

Saved successfully!

Ooh no, something went wrong!