home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / t / truth_valu < prev    next >
Text File  |  1996-11-14  |  1KB  |  30 lines

  1. <TITLE>Truth Value Testing -- Python library reference</TITLE>
  2. Next: <A HREF="../b/boolean_operations" TYPE="Next">Boolean Operations</A>  
  3. Prev: <A HREF="../t/types" TYPE="Prev">Types</A>  
  4. Up: <A HREF="../t/types" TYPE="Up">Types</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H2>2.1.1. Truth Value Testing</H2>
  7. Any object can be tested for truth value, for use in an <CODE>if</CODE> or
  8. <CODE>while</CODE> condition or as operand of the Boolean operations below.
  9. The following values are considered false:
  10. <UL>
  11. <LI>•  <CODE>None</CODE>
  12. <LI>•  zero of any numeric type, e.g., <CODE>0</CODE>, <CODE>0L</CODE>, <CODE>0.0</CODE>.
  13. <P>
  14. <LI>•  any empty sequence, e.g., <CODE>''</CODE>, <CODE>()</CODE>, <CODE>[]</CODE>.
  15. <P>
  16. <LI>•  any empty mapping, e.g., <CODE>{}</CODE>.
  17. <P>
  18. <LI>•  instances of user-defined classes, if the class defines a
  19. <CODE>__nonzero__()</CODE> or <CODE>__len__()</CODE> method, when that
  20. method returns zero.
  21. <P>
  22. </UL>
  23. All other values are considered true --- so objects of many types are
  24. always true.
  25. Operations and built-in functions that have a Boolean result always
  26. return <CODE>0</CODE> for false and <CODE>1</CODE> for true, unless otherwise
  27. stated.  (Important exception: the Boolean operations `<SAMP>or</SAMP>' and
  28. `<SAMP>and</SAMP>' always return one of their operands.)
  29. <P>
  30.