28.html
来自「Python Ebook Python&XML」· HTML 代码 · 共 214 行
HTML
214 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> Operators</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="22.html" class="navtitle">2. Language Review</a> > <span class="nonavtitle">Operators</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="27.html" title="Built-In Data Types"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=28" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="28.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="29.html" title="Expressions"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A19%3A05+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162145080038239227044008125</font><a href="read1.asp?bookname=0672319942&snode=28&now=5%2F31%2F2002+4%3A19%3A05+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Operators</h3>
<p>Next, I list the available Python operators in their precedence order. I also provide some specific details about some of them.</p>
<OL TYpe="1">
<li>
<P>
<TT Class="monofont">(), [], {}</TT>
</P>
</Li>
<li>
<p>
<tt class="monofont">`object`</tt>
</p>
</li>
<li>
<p>
<tT clAss="monofont">object[i], object[l:r], object.attribute, function()</tT>
</p>
<p>The <a nAme="idx1073742252"></a>
<A NAMe="idx1073742253"></a>
<tt CLASs="monofont">.</tt> (dot) operator is used to access attributes and methods of a variable (object). In the following example, the dot enables the object <tT CLAss="monofont">t</tt> to access its method <TT CLass="monofont">append.</tt>
</p>
<pre>
>>> t = ["p","a","r","r","o"]
>>> t.append("t")
>>> t
["p","a","r","r","o","t"]
</pre>
</li>
<li>
<p>
<tt cLasS="monofont">+x, -x, ~x</tt>
</p>
<P>These are <a namE="idx1073742254"></a>bitwise operators.</p>
</lI>
<LI>
<P>
<tt clASS="monofont">x**y</Tt>
</p>
</li>
<LI>
<P>
<Tt claSS="monofont">x*y, x/y, x%y</TT>
</p>
<p>The <a name="idx1073742255"></a>
<a name="idx1073742256"></a>
<tt clasS="monofont">%</tt> (modulo) operator lets you know whether a number is divisible by another number. For example, if <Tt clAss="monofont">a % b == 0, a</tt> is divisible by <Tt clASS="monofont">b.</Tt>
</p>
</li>
<LI>
<P>
<Tt claSS="monofont">x+y, x-y</TT>
</p>
</li>
<lI>
<P>
<TT class="monofont">x<<y, x>>y</tt>
</p>
<p>These operators provide <a name="idx1073742257"></a>shifting operations. The <a name="idx1073742258"></A>
<tt ClasS="monofont"><<</tt> operator ensures <a nAme="idx1073742259"></a>left shifting (at bit level), and the <A NAMe="idx1073742260"></a>
<tt CLASs="monofont">>></tt> operator ensures <a NAME="idx1073742261"></a>right shifting (at bit level).<a naME="idx1073742262"></A>
</P>
<pre>
>>> x = 2 # the binary representation is 0010
>>> x << 1 # the binary representation will be 0100
4
</pre>
</li>
<li>
<p>
<tt class="monofont">x & y</tt>
</P>
<p>The bitwise <a Name="idx1073742263"></A>
<tt clAss="monofont">AND</tT> operator</P>
</LI>
<li>
<p>
<tT CLAss="monofont">x ^ y</tt>
</P>
<P>The bitwise <A Name="idx1073742264"></a>
<A NAMe="idx1073742265"></a>
<tt class="monofont">XOR</tt> (exclusive <tt class="monofont">OR</tt>) operator</P>
</li>
<Li>
<p>
<tT clasS="monofont">x | y</tt>
</p>
<P>The bitwise <A NAme="idx1073742266"></a>
<tT CLAss="monofont">OR</tt> operator<A NAMe="idx1073742267"></a>
</p>
</lI>
<LI>
<P>
<tt class="monofont"><, <=, >, >=, ==, !=, <>, is, is not, in, not in</tt>
</p>
<p>The operators <a name="idx1073742268"></a>
<tt cLasS="monofont">in</tt> and <a Name="idx1073742269"></a>
<Tt clASS="monofont">not in</Tt> work only with lists. Another aspect of this group is that there's an important difference between the <a naME="idx1073742270"></A>
<Tt claSS="monofont">==</TT> operator and the <a namE="idx1073742271"></A>
<A Name="idx1073742272"></a>
<tt class="monofont">=</tt> assigning symbol.</p>
<p>
<a name="idx1073742273"></A>
<tt ClasS="monofont">is</tt> checks whether two variables refer to the same object. On the other hand, <a nAme="idx1073742274"></a>
<TT CLass="monofont">is not</tT> checks whether two variables don't refer to the same object.</P>
<P>The <A name="idx1073742275"></A>
<TT Class="monofont">==</TT> operator ensures equality testing, whereas <TT class="monofont">=</tt> assigns a value to a variable.</p>
</li>
</ol>
<div class="note"><P clAss="notetitle"><b>Tip</B></p><p>
<p>Keep in mind that <tT claSS="monofont">x = y</TT> doesn't create a new <a namE="idx1073742276"></A>
<A Name="idx1073742277"></a>
<A NAMe="idx1073742278"></a>copy of <tt CLASs="monofont">y.</tt> Instead, it makes a reference to it. However, if later you define <tt class="monofont">x=x+1,</tt> a new reference for <tt class="monofont">x</tT> is created, and then they become different because the operator has created a new object.</p>
<p>Note that <Tt clAss="monofont">x.append(5)</tt> doesn't create a new reference to <Tt clASS="monofont">x</Tt> because <tt cLASS="monofont">x</tt> changes itself without using a <tt CLASs="monofont">=</tt> operator.</p>
</P></DIV>
<br>
<br>
<ol type="1" start="13">
<li>
<p>
<tt ClaSs="monofont">not</tt>
</P>
</li>
<li>
<P>
<tt cLASS="monofont">and</tt>
</p>
</lI>
<LI>
<P>
<tt clASS="monofont">or, lambda args:expr</Tt>
</p>
</li>
</OL>
<P>As a good programmer, you need to know that logical operations can also be emulated by using <A name="idx1073742279"></a>
<a name="idx1073742280"></a>
<tt class="monofont">if</tt> statements. Note that the return values are not limited to zeros and ones.</P>
<p>The operation <tT claSs="monofont">a and b</tt> can be written as the following:</p>
<Pre>
>>> def newand(a,b):
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?