<div class="section" id="notin">
<span id="in"></span><span id="isnot"></span><span id="is"></span><span id="comparisons"></span><span id="id12"></span><h2>Comparisons</h2>
<span class="target" id="index-970"></span><p id="index-971">Unlike C, all comparison operations in Python have the same priority, which is
lower than that of any arithmetic, shifting or bitwise operation.  Also unlike
C, expressions like <tt class="docutils literal"><span class="pre">a</span> <span class="pre">&lt;</span> <span class="pre">b</span> <span class="pre">&lt;</span> <span class="pre">c</span></tt> have the interpretation that is conventional
in mathematics:</p>
<pre>
<strong id="grammar-token-comparison">comparison   </strong> ::=  <a class="reference internal" href="#grammar-token-or_expr"><tt class="xref docutils literal"><span class="pre">or_expr</span></tt></a> ( <a class="reference internal" href="#grammar-token-comp_operator"><tt class="xref docutils literal"><span class="pre">comp_operator</span></tt></a> <a class="reference internal" href="#grammar-token-or_expr"><tt class="xref docutils literal"><span class="pre">or_expr</span></tt></a> )*
<strong id="grammar-token-comp_operator">comp_operator</strong> ::=  &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;==&quot; | &quot;&gt;=&quot; | &quot;&lt;=&quot; | &quot;&lt;&gt;&quot; | &quot;!=&quot;
                   | &quot;is&quot; [&quot;not&quot;] | [&quot;not&quot;] &quot;in&quot;
</pre>
<p>Comparisons yield boolean values: <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt>.</p>
<p id="index-972">Comparisons can be chained arbitrarily, e.g., <tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">&lt;=</span> <span class="pre">z</span></tt> is equivalent to
<tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">and</span> <span class="pre">y</span> <span class="pre">&lt;=</span> <span class="pre">z</span></tt>, except that <tt class="docutils literal"><span class="pre">y</span></tt> is evaluated only once (but in both
cases <tt class="docutils literal"><span class="pre">z</span></tt> is not evaluated at all when <tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span></tt> is found to be false).</p>
<p>Formally, if <em>a</em>, <em>b</em>, <em>c</em>, ..., <em>y</em>, <em>z</em> are expressions and <em>op1</em>, <em>op2</em>, ...,
<em>opN</em> are comparison operators, then <tt class="docutils literal"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span> <span class="pre">...</span> <span class="pre">y</span> <span class="pre">opN</span> <span class="pre">z</span></tt> is equivalent
to <tt class="docutils literal"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">and</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span> <span class="pre">and</span> <span class="pre">...</span> <span class="pre">y</span> <span class="pre">opN</span> <span class="pre">z</span></tt>, except that each expression is
evaluated at most once.</p>
<p>Note that <tt class="docutils literal"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span></tt> doesn&#8217;t imply any kind of comparison between <em>a</em> and
<em>c</em>, so that, e.g., <tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">&gt;</span> <span class="pre">z</span></tt> is perfectly legal (though perhaps not
pretty).</p>
<p>The forms <tt class="docutils literal"><span class="pre">&lt;&gt;</span></tt> and <tt class="docutils literal"><span class="pre">!=</span></tt> are equivalent; for consistency with C, <tt class="docutils literal"><span class="pre">!=</span></tt> is
preferred; where <tt class="docutils literal"><span class="pre">!=</span></tt> is mentioned below <tt class="docutils literal"><span class="pre">&lt;&gt;</span></tt> is also accepted.  The <tt class="docutils literal"><span class="pre">&lt;&gt;</span></tt>
spelling is considered obsolescent.</p>
<p>The operators <tt class="docutils literal"><span class="pre">&lt;</span></tt>, <tt class="docutils literal"><span class="pre">&gt;</span></tt>, <tt class="docutils literal"><span class="pre">==</span></tt>, <tt class="docutils literal"><span class="pre">&gt;=</span></tt>, <tt class="docutils literal"><span class="pre">&lt;=</span></tt>, and <tt class="docutils literal"><span class="pre">!=</span></tt> compare the
values of two objects.  The objects need not have the same type. If both are
numbers, they are converted to a common type.  Otherwise, objects of different
types <em>always</em> compare unequal, and are ordered consistently but arbitrarily.
You can control comparison behavior of objects of non-built-in types by defining
a <tt class="docutils literal"><span class="pre">__cmp__</span></tt> method or rich comparison methods like <tt class="docutils literal"><span class="pre">__gt__</span></tt>, described in
section <a class="reference external" href="datamodel.html#specialnames"><em>Special method names</em></a>.</p>
<p>(This unusual definition of comparison was used to simplify the definition of
operations like sorting and the <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> and <a class="reference internal" href="#notin"><tt class="xref docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt></a> operators.
In the future, the comparison rules for objects of different types are likely to
change.)</p>
<p>Comparison of objects of the same type depends on the type:</p>
<ul>
<li><p class="first">Numbers are compared arithmetically.</p>
</li>
<li><p class="first">Strings are compared lexicographically using the numeric equivalents (the
result of the built-in function <a title="ord" class="reference external" href="../library/functions.html#ord"><tt class="xref docutils literal"><span class="pre">ord()</span></tt></a>) of their characters.  Unicode and
8-bit strings are fully interoperable in this behavior. <a class="footnote-reference" href="#id22" id="id13">[4]</a></p>
</li>
<li><p class="first">Tuples and lists are compared lexicographically using comparison of
corresponding elements.  This means that to compare equal, each element must
compare equal and the two sequences must be of the same type and have the same
length.</p>
<p>If not equal, the sequences are ordered the same as their first differing
elements.  For example, <tt class="docutils literal"><span class="pre">cmp([1,2,x],</span> <span class="pre">[1,2,y])</span></tt> returns the same as
<tt class="docutils literal"><span class="pre">cmp(x,y)</span></tt>.  If the corresponding element does not exist, the shorter sequence
is ordered first (for example, <tt class="docutils literal"><span class="pre">[1,2]</span> <span class="pre">&lt;</span> <span class="pre">[1,2,3]</span></tt>).</p>
</li>
<li><p class="first">Mappings (dictionaries) compare equal if and only if their sorted (key, value)
lists compare equal. <a class="footnote-reference" href="#id23" id="id14">[5]</a> Outcomes other than equality are resolved
consistently, but are not otherwise defined. <a class="footnote-reference" href="#id24" id="id15">[6]</a></p>
</li>
<li><p class="first">Most other objects of built-in types compare unequal unless they are the same
object; the choice whether one object is considered smaller or larger than
another one is made arbitrarily but consistently within one execution of a
program.</p>
</li>
</ul>
<p id="membership-test-details">The operators <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> and <a class="reference internal" href="#notin"><tt class="xref docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt></a> test for collection
membership.  <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">s</span></tt> evaluates to true if <em>x</em> is a member of the collection
<em>s</em>, and false otherwise.  <tt class="docutils literal"><span class="pre">x</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">s</span></tt> returns the negation of <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">s</span></tt>.
The collection membership test has traditionally been bound to sequences; an
object is a member of a collection if the collection is a sequence and contains
an element equal to that object.  However, it make sense for many other object
types to support membership tests without being a sequence.  In particular,
dictionaries (for keys) and sets support membership testing.</p>
<p>For the list and tuple types, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> is true if and only if there exists an
index <em>i</em> such that <tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y[i]</span></tt> is true.</p>
<p>For the Unicode and string types, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> is true if and only if <em>x</em> is a
substring of <em>y</em>.  An equivalent test is <tt class="docutils literal"><span class="pre">y.find(x)</span> <span class="pre">!=</span> <span class="pre">-1</span></tt>.  Note, <em>x</em> and <em>y</em>
need not be the same type; consequently, <tt class="docutils literal"><span class="pre">u'ab'</span> <span class="pre">in</span> <span class="pre">'abc'</span></tt> will return
<tt class="xref docutils literal"><span class="pre">True</span></tt>. Empty strings are always considered to be a substring of any other
string, so <tt class="docutils literal"><span class="pre">&quot;&quot;</span> <span class="pre">in</span> <span class="pre">&quot;abc&quot;</span></tt> will return <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.3: </span>Previously, <em>x</em> was required to be a string of length <tt class="docutils literal"><span class="pre">1</span></tt>.</p>
<p>For user-defined classes which define the <a title="object.__contains__" class="reference external" href="datamodel.html#object.__contains__"><tt class="xref docutils literal"><span class="pre">__contains__()</span></tt></a> method, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span>
<span class="pre">y</span></tt> is true if and only if <tt class="docutils literal"><span class="pre">y.__contains__(x)</span></tt> is true.</p>
<p>For user-defined classes which do not define <a title="object.__contains__" class="reference external" href="datamodel.html#object.__contains__"><tt class="xref docutils literal"><span class="pre">__contains__()</span></tt></a> but do define
<a title="object.__iter__" class="reference external" href="datamodel.html#object.__iter__"><tt class="xref docutils literal"><span class="pre">__iter__()</span></tt></a>, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> is true if some value <tt class="docutils literal"><span class="pre">z</span></tt> with <tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">z</span></tt> is
produced while iterating over <tt class="docutils literal"><span class="pre">y</span></tt>.  If an exception is raised during the
iteration, it is as if <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> raised that exception.</p>
<p>Lastly, the old-style iteration protocol is tried: if a class defines
<a title="object.__getitem__" class="reference external" href="datamodel.html#object.__getitem__"><tt class="xref docutils literal"><span class="pre">__getitem__()</span></tt></a>, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> is true if and only if there is a non-negative
integer index <em>i</em> such that <tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y[i]</span></tt>, and all lower integer indices do not
raise <a title="exceptions.IndexError" class="reference external" href="../library/exceptions.html#exceptions.IndexError"><tt class="xref docutils literal"><span class="pre">IndexError</span></tt></a> exception. (If any other exception is raised, it is as
if <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> raised that exception).</p>
<p id="index-973">The operator <a class="reference internal" href="#notin"><tt class="xref docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt></a> is defined to have the inverse true value of
<a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a>.</p>
<p id="index-974">The operators <a class="reference internal" href="#is"><tt class="xref docutils literal"><span class="pre">is</span></tt></a> and <a class="reference internal" href="#isnot"><tt class="xref docutils literal"><span class="pre">is</span> <span class="pre">not</span></tt></a> test for object identity: <tt class="docutils literal"><span class="pre">x</span>
<span class="pre">is</span> <span class="pre">y</span></tt> is true if and only if <em>x</em> and <em>y</em> are the same object.  <tt class="docutils literal"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">y</span></tt>
yields the inverse truth value. <a class="footnote-reference" href="#id25" id="id16">[7]</a></p>
</div>
