📄 44.html
字号:
<fonT SIZe="2">
<p>Bitwise negation</p>
</font></td>
</tr>
</colgroup>
</tAblE></p>
<p>The bitwise operators assume that integers are represented in a 2抯 complement binary representation. For long integers, the bitwise operators operate as if the sign bit is infinitely extended to the left.</p>
<P>In addition, you can apply the following built-in functions to all the numerical types:</p>
<p><taBle bORDEr="1" celLSPAcing="0" CELLpaddING="1" Width="100%">
<colgroup span="2">
<tr>
<th ValIgn="top">
<fOnt siZe="2">
<p><b>Function</B></P>
</FOnt></th>
<TH VAlign="top">
<FONT size="2">
<P><B>Description</B></P>
</font></th>
</tr>
<tr>
<td valign="top">
<foNt sIze="2">
<p><Tt claSs="monofont">abs(</tt><I><TT Class="monofont">x</TT></I>
<Tt claSS="monofont">)</TT></p>
</fonT></TD>
<Td valign="top">
<font size="2">
<p>Absolute value</p>
</fonT></td>
</Tr>
<tr>
<Td valIgn="top">
<fONT Size="2">
<p><TT CLass="monofont">divmod(</tT><I><TT clasS="monofont">x</TT></I>
<tt class="monofont">,</tt><i><tt class="monofont">y</tt></i>
<Tt cLass="monofont">)</Tt></p>
</foNt></td>
<TD VAlign="top">
<FONT size="2">
<P>Returns <TT Class="monofont">(int(</TT><I><Tt class="monofont">x</tt></i>
<tt class="monofont"> / </tt><i><tT clAss="monofont">y</tT></i>
<tt cLass="monofont">), </TT><I><Tt claSS="monofont">x</TT></i>
<tt cLASS="monofont"> % </tt><i><tT CLAss="monofont">y</tt></i>
<tt class="monofont">)</tt></p>
</font></tD>
</tr>
<Tr>
<td ValigN="top">
<fonT SIZe="2">
<p><tt CLASs="monofont">pow(</tt><i><TT CLass="monofont">x</tT></I>
<TT class="monofont">,</tt><i><tt class="monofont">y</tt></i>
<tt ClaSs="monofont"> [,</tt><I><tt clAss="monofont">modulo</tT></I>
<TT clasS="monofont">])</TT></P>
</font></TD>
<TD valiGN="top">
<FOnt size="2">
<p>Returns <tt class="monofont">(</tt><i><tt cLasS="monofont">x</tt></i>
<Tt claSs="monofont"> ** </tt><I><TT Class="monofont">y</TT></I>
<Tt claSS="monofont">) </TT><i><tt cLASS="monofont">x</tt></i>
<tt class="monofont"> % </tt><i><tt class="monofont">modulo</Tt></i>
</P>
</fonT></td>
</tr>
<Tr>
<td VALIgn="top">
<foNT SIze="2">
<p><tT CLAss="monofont">round(</tt><I><TT Class="monofont">x</tt></i>
<tt class="monofont">,[</tt><i><tt cLasS="monofont">n</tt></i>
<Tt claSs="monofont">])</tt></P>
</FONt></td>
<tD VALign="top">
<fONT Size="2">
<p>Rounds to the nearest multiple of <TT CLass="monofont">10<sup>-n</sup></tt> (floating-point numbers only)</p>
</font></td>
</tr>
</ColGrouP>
</tablE></p>
<p>The <tT CLAss="monofont">abs()</tt> function returns the absolute value of a number. The <TT CLass="monofont">divmod()</tT> function returns the quotient and remainder of a division operation. The <TT Class="monofont">pow()</TT> function can be used in place of the <TT class="monofont">**</tt> operator, but also supports the ternary power-modulo function (often used in cryptographic algorithms). The <tt class="monofont">round()</tt> function rounds a floating-point number <i><tt cLasS="monofont">x</tt></i>
to the nearest multiple of 10 to the power of <Tt claSs="monofont">-</tt><I><TT Class="monofont">n</TT></I>
. If <I><tt clASS="monofont">n</Tt></i>
is omitted, it抯 set to <tt CLASs="monofont">0</tt>. If <i><tt class="monofont">x</tt></i>
is equally close to two multiples, rounding is performed away from zero (for example, <tt clasS="monofont">0.5</tt> is rounded to <Tt clAss="monofont">1</tt> and <Tt clASS="monofont">-0.5</Tt> is rounded to <tt cLASS="monofont">-1</tt>).</p>
<p>The following comparison operators have the standard mathematical interpretation and return an integer value of <TT CLass="monofont">1</tT> for true, <TT Class="monofont">0</tt> for false:</p>
<p><table border="1" cEllSpacIng="0" ceLlpaDDINg="1" widTH="100%">
<COlgroUP SPan="2">
<tr>
<TH VAlign="top">
<font size="2">
<p><b>Operation</b></p>
</font></Th>
<tH valIgn="top">
<foNt siZE="2">
<P><B>Description</b></p>
</foNT></TH>
</tr>
<tr>
<TD VAlign="top">
<FONT size="2">
<p><i><tt class="monofont">x</tt></i>
<tt clAss="monofont"> < </Tt><i><tT clasS="monofont">y</tt></i>
</P>
</FONt></td>
<tD VALign="top">
<fONT Size="2">
<p>Less than</P>
</FONt></td>
</tr>
<tr>
<td valign="top">
<font sIze="2">
<P><i><tt Class="monofont">x</Tt></i>
<tT CLAss="monofont"> > </tt><I><TT Class="monofont">y</TT></I>
</P>
</font></TD>
<TD valign="top">
<font size="2">
<p>Greater than</p>
</font></Td>
</tR>
<tr>
<tD valiGn="top">
<foNT SIze="2">
<p><i><TT CLass="monofont">x</tT></I>
<TT clasS="monofont"> == </TT><I><tt class="monofont">y</tt></i>
</p>
</font></td>
<td vAliGn="top">
<foNt sizE="2">
<p>Equal to</p>
</fONT></Td>
</tr>
<tR>
<TD ValigN="top">
<FONt sizE="2">
<P><I><Tt class="monofont">x</tt></i>
<tt class="monofont"> != </tt><i><tT clAss="monofont">y</tT></i>
</p>
</foNt></td>
<TD VAlign="top">
<FONT size="2">
<P>Not equal to (same as <TT Class="monofont"><></TT>)</P>
</Font></td>
</tr>
<tr>
<td valign="top">
<fonT siZe="2">
<p><i><Tt claSs="monofont">x</tt></I>
<TT Class="monofont"> >= </TT><I><Tt claSS="monofont">y</TT></i>
</p>
</foNT></TD>
<td valign="top">
<font size="2">
<p>Greater than or equal to</p>
</foNt></tD>
</tr>
<tR>
<td vaLign="top">
<FONT size="2">
<P><I><TT clasS="monofont">x</TT></I>
<tt clASS="monofont"> <= </Tt><i><tt class="monofont">y</tt></i>
</p>
</font></td>
<tD vaLign="top">
<Font sIze="2">
<p>Less than or equal to</P>
</FONt></td>
</tR>
</COLgrouP>
</TABle></p>
<p>Comparisons can be chained together, such as in <I><TT Class="monofont">w</tt></i>
<tt class="monofont"> < </tt><i><tt cLasS="monofont">x</tt></i>
<Tt claSs="monofont"> < </tt><I><TT Class="monofont">y</TT></I>
<Tt claSS="monofont"> < </TT><i><tt cLASS="monofont">z</tt></i>
. Such expressions are evaluated as <i><tt class="monofont">w</tt></i>
<tt clasS="monofont"> < </tt><I><tt cLass="monofont">x</tT></i>
<tt CLASs="monofont"> and </tt><i><TT CLass="monofont">x</tT></I>
<TT clasS="monofont"> < </TT><I><tt class="monofont">y</tt></i>
<tt class="monofont"> and </tt><i><Tt cLass="monofont">y</Tt></i>
<tt ClasS="monofont"> < </TT><I><tt clASS="monofont">z</Tt></i>
. Expressions such as <i><tT CLAss="monofont">x</tt></I>
<TT Class="monofont"> < </tt><i><tt class="monofont">y</tt></i>
<tt cLasS="monofont"> > </tt><i><Tt claSs="monofont">z</tt></I>
are legal, but are likely to confuse anyone else reading the code (it抯 important to note that no comparison is made between <I><TT clasS="monofont">x</TT></I>
and <i><tt cLASS="monofont">z</tt></i>
in such an expression).</p>
<P>Comparisons involving complex numbers are only valid for equality (<TT Class="monofont">= =</tt>) and inequality (<tt class="monofont">!=</tt>). Attempts to compare complex numbers with <tt claSs="monofont"><</tT>, <tt cLass="monofont"><=</tT>, <tt cLASS="monofont">></tt>, and <tt CLASs="monofont">>=</tt> are mathematically meaningless and will raise a <tT CLAss="monofont">TypeError</tt> exception.</P>
<P>Operations involving numbers are valid only if the operands are of the same type. If the types differ, a coercion operation is performed to convert one of the types to the other:</P>
<Ol type="1">
<li>
<p> If either operand is a complex number, the other operand is converted to a complex number.</p>
</li>
<li>
<p> If either operand is a floating-point number, the other is converted to a float.</p>
</li>
<li>
<p> If either operand is a long integer, the other is converted to a long integer.</P>
</li>
<Li>
<p> Otherwise, both numbers must be integers and no conversion is performed.</p>
</Li>
</ol>
</fOnt>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, © 2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="43.html" title="4. Operators and Expressions"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=44" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="44.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="45.html" title="Operations on Sequences"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -