Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / ref / comparisons.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="ref.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="ref.html" title='Python Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="Booleans.html" />
<link rel="prev" href="bitwise.html" />
<link rel="parent" href="expressions.html" />
<link rel="next" href="Booleans.html" />
<meta name='aesop' content='information' />
<title>5.9 Comparisons</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.8 Binary bit-wise operations"
href="bitwise.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Expressions"
href="expressions.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.10 Boolean operations"
href="Booleans.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="bitwise.html">5.8 Binary bit-wise operations</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="expressions.html">5. Expressions</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="Booleans.html">5.10 Boolean operations</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION007900000000000000000"></A><A NAME="comparisons"></A>
<a id='l2h-429' xml:id='l2h-429'></a>
<BR>
5.9 Comparisons
</H1>
<P>
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 <code>a &lt; b &lt; c</code> have the
interpretation that is conventional in mathematics:
<a id='l2h-430' xml:id='l2h-430'></a>
<P>
<dl><dd class="grammar">
<div class="productions">
<table>
<tr>
<td><a id='tok-comparison' xml:id='tok-comparison'>comparison</a></td>
<td>::=</td>
<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>
<tr>
<td><a id='tok-comp_operator' xml:id='tok-comp_operator'>comp_operator</a></td>
<td>::=</td>
<td>"&lt;" | "&gt;" | "==" | "&gt;=" | "&lt;=" | "&lt;&gt;" | "!="</td></tr>
<tr>
<td></td>
<td></td>
<td><code>| "is" ["not"] | ["not"] "in"</code></td></tr>
</table>
</div>
<a class="grammar-footer"
href="grammar.txt" type="text/plain"
>Download entire grammar as text.</a>
</dd></dl>
<P>
Comparisons yield boolean values: <code>True</code> or <code>False</code>.
<P>
Comparisons can be chained arbitrarily, e.g., <code>x &lt; y &lt;= z</code> is
equivalent to <code>x &lt; y and y &lt;= z</code>, except that <code>y</code> is
evaluated only once (but in both cases <code>z</code> is not evaluated at all
when <code>x &lt; y</code> is found to be false).
<a id='l2h-431' xml:id='l2h-431'></a>
<P>
Formally, if <var>a</var>, <var>b</var>, <var>c</var>, ..., <var>y</var>, <var>z</var> are
expressions and <var>opa</var>, <var>opb</var>, ..., <var>opy</var> are comparison
operators, then <var>a opa b opb c</var> ...<var>y opy z</var> is equivalent
to <var>a opa b</var> <tt class="keyword">and</tt> <var>b opb c</var> <tt class="keyword">and</tt> ...
<var>y opy z</var>, except that each expression is evaluated at most once.
<P>
Note that <var>a opa b opb c</var> doesn't imply any kind of comparison
between <var>a</var> and <var>c</var>, so that, e.g., <code>x &lt; y &gt; z</code> is
perfectly legal (though perhaps not pretty).
<P>
The forms <code>&lt;&gt;</code> and <code>!=</code> are equivalent; for consistency with
C, <code>!=</code> is preferred; where <code>!=</code> is mentioned below
<code>&lt;&gt;</code> is also accepted. The <code>&lt;&gt;</code> spelling is considered
obsolescent.
<P>
The operators <code>&lt;</code>, <code>&gt;</code>, <code>==</code>, <code>&gt;=</code>, <code>&lt;=</code>, and
<code>!=</code> 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.
<P>
(This unusual definition of comparison was used to simplify the
definition of operations like sorting and the <tt class="keyword">in</tt> and
<tt class="keyword">not in</tt> operators. In the future, the comparison rules for
objects of different types are likely to change.)
<P>
Comparison of objects of the same type depends on the type:
<P>
<UL>
<LI>Numbers are compared arithmetically.
<P>
</LI>
<LI>Strings are compared lexicographically using the numeric equivalents
(the result of the built-in function <tt class="function">ord()</tt>) of their
characters. Unicode and 8-bit strings are fully interoperable in this
behavior.
<P>
</LI>
<LI>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>
If not equal, the sequences are ordered the same as their first
differing elements. For example, <code>cmp([1,2,x], [1,2,y])</code> returns
the same as <code>cmp(x,y)</code>. If the corresponding element does not
exist, the shorter sequence is ordered first (for example,
<code>[1,2] &lt; [1,2,3]</code>).
<P>
</LI>
<LI>Mappings (dictionaries) compare equal if and only if their sorted
(key, value) lists compare equal.<A NAME="tex2html7"
HREF="#foot4261"><SUP>5.4</SUP></A>Outcomes other than equality are resolved consistently, but are not
otherwise defined.<A NAME="tex2html8"
HREF="#foot4583"><SUP>5.5</SUP></A>
<P>
</LI>
<LI>Most other 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>
The operators <tt class="keyword">in</tt> and <tt class="keyword">not in</tt> test for set
membership. <code><var>x</var> in <var>s</var></code> evaluates to true if <var>x</var>
is a member of the set <var>s</var>, and false otherwise. <code><var>x</var>
not in <var>s</var></code> returns the negation of <code><var>x</var> in <var>s</var></code>.
The set membership test has traditionally been bound to sequences; an
object is a member of a set if the set is a sequence and contains an
element equal to that object. However, it is possible for an object
to support membership tests without being a sequence. In particular,
dictionaries support membership testing as a nicer way of spelling
<code><var>key</var> in <var>dict</var></code>; other mapping types may follow suit.
<P>
For the list and tuple types, <code><var>x</var> in <var>y</var></code> is true if and
only if there exists an index <var>i</var> such that
<code><var>x</var> == <var>y</var>[<var>i</var>]</code> is true.
<P>
For the Unicode and string types, <code><var>x</var> in <var>y</var></code> is true if
and only if <var>x</var> is a substring of <var>y</var>. An equivalent test is
<code>y.find(x) != -1</code>. Note, <var>x</var> and <var>y</var> need not be the
same type; consequently, <code>u'ab' in 'abc'</code> will return <code>True</code>.
Empty strings are always considered to be a substring of any other string,
so <code>"" in "abc"</code> will return <code>True</code>.
<span class="versionnote">Changed in version 2.3:
Previously, <var>x</var> was required to be a string of
length <code>1</code>.</span>
<P>
For user-defined classes which define the <tt class="method">__contains__()</tt> method,
<code><var>x</var> in <var>y</var></code> is true if and only if
<code><var>y</var>.__contains__(<var>x</var>)</code> is true.
<P>
For user-defined classes which do not define <tt class="method">__contains__()</tt> and
do define <tt class="method">__getitem__()</tt>, <code><var>x</var> in <var>y</var></code> is true if
and only if there is a non-negative integer index <var>i</var> such that
<code><var>x</var> == <var>y</var>[<var>i</var>]</code>, and all lower integer indices
do not raise <tt class="exception">IndexError</tt> exception. (If any other exception
is raised, it is as if <tt class="keyword">in</tt> raised that exception).
<P>
The operator <tt class="keyword">not in</tt> is defined to have the inverse true value
of <tt class="keyword">in</tt>.
<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>
<P>
The operators <tt class="keyword">is</tt> and <tt class="keyword">is not</tt> test for object identity:
<code><var>x</var> is <var>y</var></code> is true if and only if <var>x</var> and <var>y</var>
are the same object. <code><var>x</var> is not <var>y</var></code> yields the inverse
truth value.
<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>
<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot4261">... equal.</A><A
href="comparisons.html#tex2html7"><SUP>5.4</SUP></A></DT>
<DD>The implementation computes
this efficiently, without constructing lists or sorting.
</DD>
<DT><A NAME="foot4583">... defined.</A><A
href="comparisons.html#tex2html8"><SUP>5.5</SUP></A></DT>
<DD>Earlier versions of Python used
lexicographic comparison of the sorted (key, value) lists, but this
was very expensive for the common case of comparing for equality. An
even earlier version of Python compared dictionaries by identity only,
but this caused surprises because people expected to be able to test
a dictionary for emptiness by comparing it to <code>{}</code>.
</DD>
</DL>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.8 Binary bit-wise operations"
href="bitwise.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Expressions"
href="expressions.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.10 Boolean operations"
href="Booleans.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="bitwise.html">5.8 Binary bit-wise operations</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="expressions.html">5. Expressions</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="Booleans.html">5.10 Boolean operations</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>