| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.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="ref.html" title='Python Reference Manual' /> |
| 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="Booleans.html" /> |
| 13 | <link rel="prev" href="bitwise.html" /> |
| 14 | <link rel="parent" href="expressions.html" /> |
| 15 | <link rel="next" href="Booleans.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.9 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="5.8 Binary bit-wise operations" |
| 25 | href="bitwise.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="5. Expressions" |
| 28 | href="expressions.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="5.10 Boolean operations" |
| 31 | href="Booleans.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 Reference Manual</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'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></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="bitwise.html">5.8 Binary bit-wise operations</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="expressions.html">5. Expressions</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="Booleans.html">5.10 Boolean operations</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION007900000000000000000"></A><A NAME="comparisons"></A> |
| 56 | <a id='l2h-429' xml:id='l2h-429'></a> |
| 57 | <BR> |
| 58 | 5.9 Comparisons |
| 59 | </H1> |
| 60 | |
| 61 | <P> |
| 62 | Unlike C, all comparison operations in Python have the same priority, |
| 63 | which is lower than that of any arithmetic, shifting or bitwise |
| 64 | operation. Also unlike C, expressions like <code>a < b < c</code> have the |
| 65 | interpretation that is conventional in mathematics: |
| 66 | <a id='l2h-430' xml:id='l2h-430'></a> |
| 67 | <P> |
| 68 | <dl><dd class="grammar"> |
| 69 | <div class="productions"> |
| 70 | <table> |
| 71 | <tr> |
| 72 | <td><a id='tok-comparison' xml:id='tok-comparison'>comparison</a></td> |
| 73 | <td>::=</td> |
| 74 | <td><a class='grammartoken' href="bitwise.html#tok-or_expr">or_expr</a> ( <a class='grammartoken' href="comparisons.html#tok-comp_operator">comp_operator</a> <a class='grammartoken' href="bitwise.html#tok-or_expr">or_expr</a> )*</td></tr> |
| 75 | <tr> |
| 76 | <td><a id='tok-comp_operator' xml:id='tok-comp_operator'>comp_operator</a></td> |
| 77 | <td>::=</td> |
| 78 | <td>"<" | ">" | "==" | ">=" | "<=" | "<>" | "!="</td></tr> |
| 79 | <tr> |
| 80 | <td></td> |
| 81 | <td></td> |
| 82 | <td><code>| "is" ["not"] | ["not"] "in"</code></td></tr> |
| 83 | </table> |
| 84 | </div> |
| 85 | <a class="grammar-footer" |
| 86 | href="grammar.txt" type="text/plain" |
| 87 | >Download entire grammar as text.</a> |
| 88 | </dd></dl> |
| 89 | |
| 90 | <P> |
| 91 | Comparisons yield boolean values: <code>True</code> or <code>False</code>. |
| 92 | |
| 93 | <P> |
| 94 | Comparisons can be chained arbitrarily, e.g., <code>x < y <= z</code> is |
| 95 | equivalent to <code>x < y and y <= z</code>, except that <code>y</code> is |
| 96 | evaluated only once (but in both cases <code>z</code> is not evaluated at all |
| 97 | when <code>x < y</code> is found to be false). |
| 98 | <a id='l2h-431' xml:id='l2h-431'></a> |
| 99 | <P> |
| 100 | Formally, if <var>a</var>, <var>b</var>, <var>c</var>, ..., <var>y</var>, <var>z</var> are |
| 101 | expressions and <var>opa</var>, <var>opb</var>, ..., <var>opy</var> are comparison |
| 102 | operators, then <var>a opa b opb c</var> ...<var>y opy z</var> is equivalent |
| 103 | to <var>a opa b</var> <tt class="keyword">and</tt> <var>b opb c</var> <tt class="keyword">and</tt> ... |
| 104 | <var>y opy z</var>, except that each expression is evaluated at most once. |
| 105 | |
| 106 | <P> |
| 107 | Note that <var>a opa b opb c</var> doesn't imply any kind of comparison |
| 108 | between <var>a</var> and <var>c</var>, so that, e.g., <code>x < y > z</code> is |
| 109 | perfectly legal (though perhaps not pretty). |
| 110 | |
| 111 | <P> |
| 112 | The forms <code><></code> and <code>!=</code> are equivalent; for consistency with |
| 113 | C, <code>!=</code> is preferred; where <code>!=</code> is mentioned below |
| 114 | <code><></code> is also accepted. The <code><></code> spelling is considered |
| 115 | obsolescent. |
| 116 | |
| 117 | <P> |
| 118 | The operators <code><</code>, <code>></code>, <code>==</code>, <code>>=</code>, <code><=</code>, and |
| 119 | <code>!=</code> compare |
| 120 | the values of two objects. The objects need not have the same type. |
| 121 | If both are numbers, they are converted to a common type. Otherwise, |
| 122 | objects of different types <em>always</em> compare unequal, and are |
| 123 | ordered consistently but arbitrarily. |
| 124 | |
| 125 | <P> |
| 126 | (This unusual definition of comparison was used to simplify the |
| 127 | definition of operations like sorting and the <tt class="keyword">in</tt> and |
| 128 | <tt class="keyword">not in</tt> operators. In the future, the comparison rules for |
| 129 | objects of different types are likely to change.) |
| 130 | |
| 131 | <P> |
| 132 | Comparison of objects of the same type depends on the type: |
| 133 | |
| 134 | <P> |
| 135 | |
| 136 | <UL> |
| 137 | <LI>Numbers are compared arithmetically. |
| 138 | |
| 139 | <P> |
| 140 | </LI> |
| 141 | <LI>Strings are compared lexicographically using the numeric equivalents |
| 142 | (the result of the built-in function <tt class="function">ord()</tt>) of their |
| 143 | characters. Unicode and 8-bit strings are fully interoperable in this |
| 144 | behavior. |
| 145 | |
| 146 | <P> |
| 147 | </LI> |
| 148 | <LI>Tuples and lists are compared lexicographically using comparison of |
| 149 | corresponding elements. This means that to compare equal, each |
| 150 | element must compare equal and the two sequences must be of the same |
| 151 | type and have the same length. |
| 152 | |
| 153 | <P> |
| 154 | If not equal, the sequences are ordered the same as their first |
| 155 | differing elements. For example, <code>cmp([1,2,x], [1,2,y])</code> returns |
| 156 | the same as <code>cmp(x,y)</code>. If the corresponding element does not |
| 157 | exist, the shorter sequence is ordered first (for example, |
| 158 | <code>[1,2] < [1,2,3]</code>). |
| 159 | |
| 160 | <P> |
| 161 | </LI> |
| 162 | <LI>Mappings (dictionaries) compare equal if and only if their sorted |
| 163 | (key, value) lists compare equal.<A NAME="tex2html7" |
| 164 | HREF="#foot4261"><SUP>5.4</SUP></A>Outcomes other than equality are resolved consistently, but are not |
| 165 | otherwise defined.<A NAME="tex2html8" |
| 166 | HREF="#foot4583"><SUP>5.5</SUP></A> |
| 167 | <P> |
| 168 | </LI> |
| 169 | <LI>Most other types compare unequal unless they are the same object; |
| 170 | the choice whether one object is considered smaller or larger than |
| 171 | another one is made arbitrarily but consistently within one |
| 172 | execution of a program. |
| 173 | |
| 174 | <P> |
| 175 | </LI> |
| 176 | </UL> |
| 177 | |
| 178 | <P> |
| 179 | The operators <tt class="keyword">in</tt> and <tt class="keyword">not in</tt> test for set |
| 180 | membership. <code><var>x</var> in <var>s</var></code> evaluates to true if <var>x</var> |
| 181 | is a member of the set <var>s</var>, and false otherwise. <code><var>x</var> |
| 182 | not in <var>s</var></code> returns the negation of <code><var>x</var> in <var>s</var></code>. |
| 183 | The set membership test has traditionally been bound to sequences; an |
| 184 | object is a member of a set if the set is a sequence and contains an |
| 185 | element equal to that object. However, it is possible for an object |
| 186 | to support membership tests without being a sequence. In particular, |
| 187 | dictionaries support membership testing as a nicer way of spelling |
| 188 | <code><var>key</var> in <var>dict</var></code>; other mapping types may follow suit. |
| 189 | |
| 190 | <P> |
| 191 | For the list and tuple types, <code><var>x</var> in <var>y</var></code> is true if and |
| 192 | only if there exists an index <var>i</var> such that |
| 193 | <code><var>x</var> == <var>y</var>[<var>i</var>]</code> is true. |
| 194 | |
| 195 | <P> |
| 196 | For the Unicode and string types, <code><var>x</var> in <var>y</var></code> is true if |
| 197 | and only if <var>x</var> is a substring of <var>y</var>. An equivalent test is |
| 198 | <code>y.find(x) != -1</code>. Note, <var>x</var> and <var>y</var> need not be the |
| 199 | same type; consequently, <code>u'ab' in 'abc'</code> will return <code>True</code>. |
| 200 | Empty strings are always considered to be a substring of any other string, |
| 201 | so <code>"" in "abc"</code> will return <code>True</code>. |
| 202 | |
| 203 | <span class="versionnote">Changed in version 2.3: |
| 204 | Previously, <var>x</var> was required to be a string of |
| 205 | length <code>1</code>.</span> |
| 206 | |
| 207 | <P> |
| 208 | For user-defined classes which define the <tt class="method">__contains__()</tt> method, |
| 209 | <code><var>x</var> in <var>y</var></code> is true if and only if |
| 210 | <code><var>y</var>.__contains__(<var>x</var>)</code> is true. |
| 211 | |
| 212 | <P> |
| 213 | For user-defined classes which do not define <tt class="method">__contains__()</tt> and |
| 214 | do define <tt class="method">__getitem__()</tt>, <code><var>x</var> in <var>y</var></code> is true if |
| 215 | and only if there is a non-negative integer index <var>i</var> such that |
| 216 | <code><var>x</var> == <var>y</var>[<var>i</var>]</code>, and all lower integer indices |
| 217 | do not raise <tt class="exception">IndexError</tt> exception. (If any other exception |
| 218 | is raised, it is as if <tt class="keyword">in</tt> raised that exception). |
| 219 | |
| 220 | <P> |
| 221 | The operator <tt class="keyword">not in</tt> is defined to have the inverse true value |
| 222 | of <tt class="keyword">in</tt>. |
| 223 | <a id='l2h-432' xml:id='l2h-432'></a><a id='l2h-433' xml:id='l2h-433'></a><a id='l2h-434' xml:id='l2h-434'></a><a id='l2h-435' xml:id='l2h-435'></a> |
| 224 | <P> |
| 225 | The operators <tt class="keyword">is</tt> and <tt class="keyword">is not</tt> test for object identity: |
| 226 | <code><var>x</var> is <var>y</var></code> is true if and only if <var>x</var> and <var>y</var> |
| 227 | are the same object. <code><var>x</var> is not <var>y</var></code> yields the inverse |
| 228 | truth value. |
| 229 | <a id='l2h-436' xml:id='l2h-436'></a><a id='l2h-437' xml:id='l2h-437'></a><a id='l2h-438' xml:id='l2h-438'></a> |
| 230 | <P> |
| 231 | <BR><HR><H4>Footnotes</H4> |
| 232 | <DL> |
| 233 | <DT><A NAME="foot4261">... equal.</A><A |
| 234 | href="comparisons.html#tex2html7"><SUP>5.4</SUP></A></DT> |
| 235 | <DD>The implementation computes |
| 236 | this efficiently, without constructing lists or sorting. |
| 237 | |
| 238 | </DD> |
| 239 | <DT><A NAME="foot4583">... defined.</A><A |
| 240 | href="comparisons.html#tex2html8"><SUP>5.5</SUP></A></DT> |
| 241 | <DD>Earlier versions of Python used |
| 242 | lexicographic comparison of the sorted (key, value) lists, but this |
| 243 | was very expensive for the common case of comparing for equality. An |
| 244 | even earlier version of Python compared dictionaries by identity only, |
| 245 | but this caused surprises because people expected to be able to test |
| 246 | a dictionary for emptiness by comparing it to <code>{}</code>. |
| 247 | |
| 248 | </DD> |
| 249 | </DL> |
| 250 | <DIV CLASS="navigation"> |
| 251 | <div class='online-navigation'> |
| 252 | <p></p><hr /> |
| 253 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 254 | <tr> |
| 255 | <td class='online-navigation'><a rel="prev" title="5.8 Binary bit-wise operations" |
| 256 | href="bitwise.html"><img src='../icons/previous.png' |
| 257 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 258 | <td class='online-navigation'><a rel="parent" title="5. Expressions" |
| 259 | href="expressions.html"><img src='../icons/up.png' |
| 260 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 261 | <td class='online-navigation'><a rel="next" title="5.10 Boolean operations" |
| 262 | href="Booleans.html"><img src='../icons/next.png' |
| 263 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 264 | <td align="center" width="100%">Python Reference Manual</td> |
| 265 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 266 | href="contents.html"><img src='../icons/contents.png' |
| 267 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 268 | <td class='online-navigation'><img src='../icons/blank.png' |
| 269 | border='0' height='32' alt='' width='32' /></td> |
| 270 | <td class='online-navigation'><a rel="index" title="Index" |
| 271 | href="genindex.html"><img src='../icons/index.png' |
| 272 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 273 | </tr></table> |
| 274 | <div class='online-navigation'> |
| 275 | <b class="navlabel">Previous:</b> |
| 276 | <a class="sectref" rel="prev" href="bitwise.html">5.8 Binary bit-wise operations</A> |
| 277 | <b class="navlabel">Up:</b> |
| 278 | <a class="sectref" rel="parent" href="expressions.html">5. Expressions</A> |
| 279 | <b class="navlabel">Next:</b> |
| 280 | <a class="sectref" rel="next" href="Booleans.html">5.10 Boolean operations</A> |
| 281 | </div> |
| 282 | </div> |
| 283 | <hr /> |
| 284 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 285 | </DIV> |
| 286 | <!--End of Navigation Panel--> |
| 287 | <ADDRESS> |
| 288 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 289 | </ADDRESS> |
| 290 | </BODY> |
| 291 | </HTML> |