| 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="typesnumeric.html" /> |
| 13 | <link rel="prev" href="boolean.html" /> |
| 14 | <link rel="parent" href="types.html" /> |
| 15 | <link rel="next" href="typesnumeric.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>2.3.3 Comparisons </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.2 Boolean Operations " |
| 25 | href="boolean.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.4 Numeric Types " |
| 31 | href="typesnumeric.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="boolean.html">2.3.2 Boolean Operations </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="typesnumeric.html">2.3.4 Numeric Types </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION004330000000000000000"></A><A NAME="comparisons"></A> |
| 56 | <BR> |
| 57 | 2.3.3 Comparisons |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | Comparison operations are supported by all objects. They all have the |
| 62 | same priority (which is higher than that of the Boolean operations). |
| 63 | Comparisons can be chained arbitrarily; for example, <code><var>x</var> < |
| 64 | <var>y</var> <= <var>z</var></code> is equivalent to <code><var>x</var> < <var>y</var> and |
| 65 | <var>y</var> <= <var>z</var></code>, except that <var>y</var> is evaluated only once (but |
| 66 | in both cases <var>z</var> is not evaluated at all when <code><var>x</var> < |
| 67 | <var>y</var></code> is found to be false). |
| 68 | <a id='l2h-108' xml:id='l2h-108'></a> |
| 69 | <P> |
| 70 | This table summarizes the comparison operations: |
| 71 | |
| 72 | <P> |
| 73 | <div class="center"><table class="realtable"> |
| 74 | <thead> |
| 75 | <tr> |
| 76 | <th class="center">Operation</th> |
| 77 | <th class="left" >Meaning</th> |
| 78 | <th class="center">Notes</th> |
| 79 | </tr> |
| 80 | </thead> |
| 81 | <tbody> |
| 82 | <tr><td class="center" valign="baseline"><code><</code></td> |
| 83 | <td class="left" >strictly less than</td> |
| 84 | <td class="center"></td></tr> |
| 85 | <tr><td class="center" valign="baseline"><code><=</code></td> |
| 86 | <td class="left" >less than or equal</td> |
| 87 | <td class="center"></td></tr> |
| 88 | <tr><td class="center" valign="baseline"><code>></code></td> |
| 89 | <td class="left" >strictly greater than</td> |
| 90 | <td class="center"></td></tr> |
| 91 | <tr><td class="center" valign="baseline"><code>>=</code></td> |
| 92 | <td class="left" >greater than or equal</td> |
| 93 | <td class="center"></td></tr> |
| 94 | <tr><td class="center" valign="baseline"><code>==</code></td> |
| 95 | <td class="left" >equal</td> |
| 96 | <td class="center"></td></tr> |
| 97 | <tr><td class="center" valign="baseline"><code>!=</code></td> |
| 98 | <td class="left" >not equal</td> |
| 99 | <td class="center">(1)</td></tr> |
| 100 | <tr><td class="center" valign="baseline"><code><></code></td> |
| 101 | <td class="left" >not equal</td> |
| 102 | <td class="center">(1)</td></tr> |
| 103 | <tr><td class="center" valign="baseline"><code>is</code></td> |
| 104 | <td class="left" >object identity</td> |
| 105 | <td class="center"></td></tr> |
| 106 | <tr><td class="center" valign="baseline"><code>is not</code></td> |
| 107 | <td class="left" >negated object identity</td> |
| 108 | <td class="center"></td></tr></tbody> |
| 109 | </table></div> |
| 110 | <a id='l2h-109' xml:id='l2h-109'></a><a id='l2h-110' xml:id='l2h-110'></a> <a id='l2h-111' xml:id='l2h-111'></a><a id='l2h-112' xml:id='l2h-112'></a> |
| 111 | <P> |
| 112 | Notes: |
| 113 | |
| 114 | <P> |
| 115 | <DL> |
| 116 | <DT><STRONG>(1)</STRONG></DT> |
| 117 | <DD><code><></code> and <code>!=</code> are alternate spellings for the same operator. |
| 118 | <code>!=</code> is the preferred spelling; <code><></code> is obsolescent. |
| 119 | |
| 120 | <P> |
| 121 | </DD> |
| 122 | </DL> |
| 123 | |
| 124 | <P> |
| 125 | Objects of different types, except different numeric types and different string types, never |
| 126 | compare equal; such objects are ordered consistently but arbitrarily |
| 127 | (so that sorting a heterogeneous array yields a consistent result). |
| 128 | Furthermore, some types (for example, file objects) support only a |
| 129 | degenerate notion of comparison where any two objects of that type are |
| 130 | unequal. Again, such objects are ordered arbitrarily but |
| 131 | consistently. The <code><</code>, <code><=</code>, <code>></code> and <code>>=</code> |
| 132 | operators will raise a <tt class="exception">TypeError</tt> exception when any operand |
| 133 | is a complex number. |
| 134 | <a id='l2h-113' xml:id='l2h-113'></a><a id='l2h-114' xml:id='l2h-114'></a> |
| 135 | <P> |
| 136 | Instances of a class normally compare as non-equal unless the class |
| 137 | <a id='l2h-116' xml:id='l2h-116'></a>defines the <tt class="method">__cmp__()</tt> method. Refer to the |
| 138 | <em class="citetitle"><a |
| 139 | href="../ref/customization.html" |
| 140 | title="Python Reference Manual" |
| 141 | >Python Reference Manual</a></em> for |
| 142 | information on the use of this method to effect object comparisons. |
| 143 | |
| 144 | <P> |
| 145 | <strong>Implementation note:</strong> Objects of different types except |
| 146 | numbers are ordered by their type names; objects of the same types |
| 147 | that don't support proper comparison are ordered by their address. |
| 148 | |
| 149 | <P> |
| 150 | Two more operations with the same syntactic priority, |
| 151 | "<tt class="samp">in</tt>"<a id='l2h-117' xml:id='l2h-117'></a> and "<tt class="samp">not in</tt>"<a id='l2h-118' xml:id='l2h-118'></a>, are supported |
| 152 | only by sequence types (below). |
| 153 | |
| 154 | <P> |
| 155 | |
| 156 | <DIV CLASS="navigation"> |
| 157 | <div class='online-navigation'> |
| 158 | <p></p><hr /> |
| 159 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 160 | <tr> |
| 161 | <td class='online-navigation'><a rel="prev" title="2.3.2 Boolean Operations " |
| 162 | href="boolean.html"><img src='../icons/previous.png' |
| 163 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 164 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" |
| 165 | href="types.html"><img src='../icons/up.png' |
| 166 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 167 | <td class='online-navigation'><a rel="next" title="2.3.4 Numeric Types " |
| 168 | href="typesnumeric.html"><img src='../icons/next.png' |
| 169 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 170 | <td align="center" width="100%">Python Library Reference</td> |
| 171 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 172 | href="contents.html"><img src='../icons/contents.png' |
| 173 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 174 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 175 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 176 | <td class='online-navigation'><a rel="index" title="Index" |
| 177 | href="genindex.html"><img src='../icons/index.png' |
| 178 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 179 | </tr></table> |
| 180 | <div class='online-navigation'> |
| 181 | <b class="navlabel">Previous:</b> |
| 182 | <a class="sectref" rel="prev" href="boolean.html">2.3.2 Boolean Operations </A> |
| 183 | <b class="navlabel">Up:</b> |
| 184 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> |
| 185 | <b class="navlabel">Next:</b> |
| 186 | <a class="sectref" rel="next" href="typesnumeric.html">2.3.4 Numeric Types </A> |
| 187 | </div> |
| 188 | </div> |
| 189 | <hr /> |
| 190 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 191 | </DIV> |
| 192 | <!--End of Navigation Panel--> |
| 193 | <ADDRESS> |
| 194 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 195 | </ADDRESS> |
| 196 | </BODY> |
| 197 | </HTML> |