| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="boolean.html" /> |
| 13 | <link rel="prev" href="types.html" /> |
| 14 | <link rel="parent" href="types.html" /> |
| 15 | <link rel="next" href="boolean.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>2.3.1 Truth Value Testing</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="2.3 Built-in Types" |
| 25 | href="types.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" |
| 28 | href="types.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="2.3.2 Boolean Operations " |
| 31 | href="boolean.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="types.html">2.3 Built-in Types</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="boolean.html">2.3.2 Boolean Operations </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION004310000000000000000"></A><A NAME="truth"></A> |
| 56 | <BR> |
| 57 | 2.3.1 Truth Value Testing |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | Any object can be tested for truth value, for use in an <tt class="keyword">if</tt> or |
| 62 | <tt class="keyword">while</tt> condition or as operand of the Boolean operations below. |
| 63 | The following values are considered false: |
| 64 | <a id='l2h-91' xml:id='l2h-91'></a><a id='l2h-92' xml:id='l2h-92'></a><a id='l2h-93' xml:id='l2h-93'></a><a id='l2h-94' xml:id='l2h-94'></a><a id='l2h-101' xml:id='l2h-101'></a> |
| 65 | |
| 66 | <P> |
| 67 | |
| 68 | <UL> |
| 69 | <LI><code>None</code> |
| 70 | <a id='l2h-96' xml:id='l2h-96'></a> |
| 71 | <P> |
| 72 | </LI> |
| 73 | <LI><code>False</code> |
| 74 | <a id='l2h-98' xml:id='l2h-98'></a> |
| 75 | <P> |
| 76 | </LI> |
| 77 | <LI>zero of any numeric type, for example, <code>0</code>, <code>0L</code>, |
| 78 | <code>0.0</code>, <code>0j</code>. |
| 79 | |
| 80 | <P> |
| 81 | </LI> |
| 82 | <LI>any empty sequence, for example, <code>''</code>, <code>()</code>, <code>[]</code>. |
| 83 | |
| 84 | <P> |
| 85 | </LI> |
| 86 | <LI>any empty mapping, for example, <code>{}</code>. |
| 87 | |
| 88 | <P> |
| 89 | </LI> |
| 90 | <LI>instances of user-defined classes, if the class defines a |
| 91 | <tt class="method">__nonzero__()</tt> or <tt class="method">__len__()</tt> method, when that |
| 92 | method returns the integer zero or <tt class="class">bool</tt> value |
| 93 | <code>False</code>.<A NAME="tex2html6" |
| 94 | HREF="#foot3425"><SUP>2.5</SUP></A> |
| 95 | <P> |
| 96 | </LI> |
| 97 | </UL> |
| 98 | |
| 99 | <P> |
| 100 | All other values are considered true -- so objects of many types are |
| 101 | always true. |
| 102 | <a id='l2h-102' xml:id='l2h-102'></a> |
| 103 | |
| 104 | <P> |
| 105 | Operations and built-in functions that have a Boolean result always |
| 106 | return <code>0</code> or <code>False</code> for false and <code>1</code> or <code>True</code> |
| 107 | for true, unless otherwise stated. (Important exception: the Boolean |
| 108 | operations "<tt class="samp">or</tt>"<a id='l2h-99' xml:id='l2h-99'></a> and "<tt class="samp">and</tt>"<a id='l2h-100' xml:id='l2h-100'></a> always |
| 109 | return one of their operands.) |
| 110 | <a id='l2h-103' xml:id='l2h-103'></a> |
| 111 | |
| 112 | <P> |
| 113 | <BR><HR><H4>Footnotes</H4> |
| 114 | <DL> |
| 115 | <DT><A NAME="foot3425">...False.</A><A |
| 116 | href="truth.html#tex2html6"><SUP>2.5</SUP></A></DT> |
| 117 | <DD>Additional |
| 118 | information on these special methods may be found in the |
| 119 | <em class="citetitle"><a |
| 120 | href="../ref/ref.html" |
| 121 | title="Python Reference Manual" |
| 122 | >Python Reference Manual</a></em>. |
| 123 | |
| 124 | </DD> |
| 125 | </DL> |
| 126 | <DIV CLASS="navigation"> |
| 127 | <div class='online-navigation'> |
| 128 | <p></p><hr /> |
| 129 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 130 | <tr> |
| 131 | <td class='online-navigation'><a rel="prev" title="2.3 Built-in Types" |
| 132 | href="types.html"><img src='../icons/previous.png' |
| 133 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 134 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" |
| 135 | href="types.html"><img src='../icons/up.png' |
| 136 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 137 | <td class='online-navigation'><a rel="next" title="2.3.2 Boolean Operations " |
| 138 | href="boolean.html"><img src='../icons/next.png' |
| 139 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 140 | <td align="center" width="100%">Python Library Reference</td> |
| 141 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 142 | href="contents.html"><img src='../icons/contents.png' |
| 143 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 144 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 145 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 146 | <td class='online-navigation'><a rel="index" title="Index" |
| 147 | href="genindex.html"><img src='../icons/index.png' |
| 148 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 149 | </tr></table> |
| 150 | <div class='online-navigation'> |
| 151 | <b class="navlabel">Previous:</b> |
| 152 | <a class="sectref" rel="prev" href="types.html">2.3 Built-in Types</A> |
| 153 | <b class="navlabel">Up:</b> |
| 154 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> |
| 155 | <b class="navlabel">Next:</b> |
| 156 | <a class="sectref" rel="next" href="boolean.html">2.3.2 Boolean Operations </A> |
| 157 | </div> |
| 158 | </div> |
| 159 | <hr /> |
| 160 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 161 | </DIV> |
| 162 | <!--End of Navigation Panel--> |
| 163 | <ADDRESS> |
| 164 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 165 | </ADDRESS> |
| 166 | </BODY> |
| 167 | </HTML> |