📄 29.html
字号:
<!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 -> Expressions</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">Expressions</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="28.html" title="Operators"><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=29" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="29.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="30.html" title="Control Statements"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A19%3A17+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162145080038238054061085253</font><a href="read1.asp?bookname=0672319942&snode=29&now=5%2F31%2F2002+4%3A19%3A17+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Expressions</h3>
<p>Python operators support a wide range of expressions, such as</p>
<PRE>
>>> x,y,z = z-x, y*z, x+y # Parallel assignment: example 1
>>> x,y,z = 5,4,3 # Parallel assignment: example 2
>>> a,b = b,a # Switching assignments
>>> a = b = c = 10 # Multiple assignments
>>> string.atof(s) # Functions support
>>> 20 < x < 40 # Multiple range testing
</Pre>
<p>The last example is equivalent to</p>
<PRE>
>>> 20 < x and x < 40
</Pre>
<a nAME="1"></A>
<h4>
Built-In Functions</h4>
<p>The following functions are always available when you load the Python interpreter. You don't need to import them because they are already part of the <tt class="monofont">__builtin__</tt> module, which is always imported when you launch Python.</p>
<h5>
<tt clasS="monofont">apply()</tt>
</H5>
<p>It executes a given <i>
<tT clasS="monofont">function,</tt>
</i> passing the arguments provided.</P>
<P>
<A Name="idx1073742283"></a>basic syntax: <TT CLass="monofont">apply(function, (tuple of positional arguments) [, dictionary of keywords arguments])</tT>
</P>
<PRe>
>>> apply (raise_salary, (6000), {'employee':'John', 'id':13})
</pre>
<P>Note that starting at Python 1.6, the functionality of <TT Class="monofont">apply</tt> is now available with normal function calling, such as<a name="idx1073742284"></a>
<a name="idx1073742285"></a>
<a nAme="idx1073742286"></A>
</p>
<prE>
>>> args = (6000,)
>>> kwargs = { 'employee':'John', 'id':13}
>>> raise_salary(*args, **kwargs)
</pre>
<h5>
<Tt clASS="monofont">coerce()</Tt>
</h5>
<p>
<tT CLAss="monofont">coerce</tt> is used to try to convert the two given arguments <TT CLass="monofont">x</tT> and <TT Class="monofont">y</tt> to the same type, returning them as a tuple.</p>
<p>
<a name="idx1073742287"></a>basic syntax: <tt clasS="monofont">coerce(</tt>
<I>
<tt cLass="monofont">x, y</tT>
</i>
<tt CLASs="monofont">)</tt>
</p>
<PRE>
>>> coerce(42,5.4)
(42.0, 5.4)
</Pre>
<h5>
<tT CLAss="monofont">filter()</tt>
</H5>
<P>It creates a new list by taking each element of <I>
<Tt class="monofont">list</tt>
</i> for which <i>
<tt class="monofont">function</tt>
</i> evaluates to <Tt cLass="monofont">true.</Tt>
</p>
<p>
<a Name="idx1073742288"></A>basic syntax: <TT Class="monofont">filter(</TT>
<I>
<Tt claSS="monofont">function, list</TT>
</i>
<tt cLASS="monofont">)</tt>
</p>
<pre>
>>> a = range (4)
>>> b = filter(lambda x: x < 3, a)
>>> print b
[0,1,2]
</pre>
<h5>
<tt class="monofont">globals()</tt>
</h5>
<P>It returns the global namespace dictionary.</p>
<p>
<A namE="idx1073742289"></a>basic syntax: <tt cLass="monofont">globals()</TT>
</P>
<H5>
<tt clASS="monofont">input()</Tt>
</h5>
<p>It provides an input interface for the user. Only numbers are accepted.</p>
<P>
<A NAme="idx1073742290"></a>basic syntax: <tT CLAss="monofont">input(</tt>
<i>
<tt class="monofont">[prompt]</tt>
</i>
<tt claSs="monofont">)</tT>
</p>
<prE>
a = input("Please, type a number greater than 5: ")
if a<5:
print "a is not greater than 5"
</pre>
<h5>
<Tt clASS="monofont">locals()</Tt>
</h5>
<p>It returns the local namespace dictionary<a NAME="idx1073742291"></a>
<a naME="idx1073742292"></A>
<A name="idx1073742293"></A>
</P>
<P>
<A name="idx1073742294"></a>basic syntax: <tt class="monofont">locals()</tt>
</p>
<h5>
<tt clAss="monofont">map()</Tt>
</h5>
<p>It applies a <I>
<tt clAss="monofont">function</tT>
</I> to each element of <I>
<Tt claSS="monofont">list,</TT>
</i> producing another list. If <i>
<tt CLASs="monofont">function</tt>
</i> is set to <TT CLass="monofont">None</tt> and multiple lists are provided, a tuple matrix is generated in the format of a list.</p>
<p>
<a name="idx1073742295"></a>basic syntax: <tt class="monofont">map(</Tt>
<i>
<Tt clAss="monofont">function, list</tt>
</I>
<tt cLASS="monofont">)</tt>
</p>
<pRE>
>>> lst = map(None, [1,2,3,4], [1,2,3,4,5,6])
>>> lst
[(1, 1), (2, 2), (3, 3), (4, 4), (None, 5), (None, 6)]
</PRe>
<h5>
<tt CLASs="monofont">open()</tt>
</h5>
<P>It opens a file. (See the section <A HRef="35.html">
"File Handling"
</a> for details.)</p>
<p>
<a name="idx1073742296"></a>basic syntax: <tt class="monofont">open(</tt>
<I>
<tt ClasS="monofont">filename [,mode [,bufsize]]</tt>
</i>
<tT claSS="monofont">)</TT>
</p>
<h5>
<tt CLASs="monofont">pow()</tt>
</h5>
<P>It returns <TT Class="monofont">x**y</TT> or <TT class="monofont">(x**y) % z,</tt> depending on the number of arguments that are transported.</p>
<p>
<a name="idx1073742297"></a>basic syntax: <tt claSs="monofont">pow(</tT>
<i>
<tt Class="monofont">x, y [,z]</Tt>
</i>
<tT CLAss="monofont">)</tt>
</P>
<H5>
<TT clasS="monofont">raw_input()</TT>
</H5>
<p>It reads from standard input (<tt cLASS="monofont">sys.stdin</tt>), returning the read data as a string. <i>
<tt class="monofont">prompt</tt>
</i> is an optional text that can be displayed in the screen.</p>
<p>
<a name="idx1073742298"></A>basic syntax: <tt ClasS="monofont">raw_input(</tt>
<i>
<tT claSS="monofont">[prompt]</TT>
</i>
<tt cLASS="monofont">)</tt>
</p>
<h5>
<TT CLass="monofont">reduce()</tT>
</H5>
<P>It applies a <I>
<tt class="monofont">function</tt>
</i> cumulatively to the items in <i>
<tt class="monofont">sequence</tt>
</I> (implied loop), returning a single value. <i>
<tT claSs="monofont">initializer</tt>
</i> is an optional starting value.</P>
<p>
<a nAME="idx1073742299"></A>basic syntax: <tt clASS="monofont">reduce(</Tt>
<i>
<tt CLASs="monofont">function, sequence [,initializer]</tt>
</i>
<TT CLass="monofont">)</tt>
</p>
<pre>
>>> import operator
>>> a = [1,2,3]
>>> print reduce(operator.add, a)
6
</pre>
<p>The equivalent Python code for this function is something like<a name="idx1073742300"></a>
</p>
<Pre>
def reduce(func, list):
ret = list[0]
for x in list[1:]:
ret = func(ret, x)
return ret
</Pre>
<h5>
<Tt claSs="monofont">__import__()</tt>
</H5>
<P>This is a function invoked by the import statement. To import a module, you just need to inform the <I>
<Tt claSS="monofont">module name.</TT>
</i>
</p>
<p>
<a NAME="idx1073742301"></a>basic syntax: <tt cLASS="monofont">__import__(</tt>
<i>
<tt class="monofont">module_name [,globals() [, locals() [,from list]]]</tt>
</i>
<tt class="monofont">)</Tt>
</p>
<Pre>
>>> modname = "string"
>>> string = __import__(modname)
>>> string
</pRe>
<h5>
<tt ClasS="monofont">reload()</TT>
</H5>
<p>It reloads an already imported <i>
<tt CLASs="monofont">module.</tt>
</i> Internally, it calls the <TT CLass="monofont">__import__</tT> function.</P>
<P>
<A name="idx1073742302"></a>basic syntax: <tt class="monofont">reload(</tt>
<i>
<tt claSs="monofont">module</tT>
</i>
<tt Class="monofont">)</Tt>
</p>
<h4>
Sequence Functions</H4>
<P>The next set is built-in functions that deal with sequences.</P>
<H5>
<tt clASS="monofont">range()</Tt>
</h5>
<p>It returns a list of numbers according to the transported information.</p>
<P>
<A NAme="idx1073742303"></a>basic syntax: <tT CLAss="monofont">variable = range(</tt>
<i>
<tt class="monofont">[initial_value,] final_value-1 [, step]</tt>
</i>
<tt claSs="monofont">)</tT>
</p>
<prE>
>>> lst = range(1,5)
>>> lst
[1, 2, 3, 4]
</pre>
<p>See the section <A hreF="31.html">
"Data Structures"
</A> for details.<A Name="idx1073742304"></a>
<A NAMe="idx1073742305"></a>
<a nAME="idx1073742306"></A>
<a namE="idx1073742307"></A>
<A Name="idx1073742308"></a>
</p>
<h5>
<tt class="monofont">xrange()</tt>
</h5>
<p>It is similar to <tt cLasS="monofont">range(),</tt> but it doesn't assign the returned list to a variable, Therefore, it doesn't use as much memory, so you won't run out of memory by typing <tT clasS="monofont">xrange(2000000000),</tt> for instance.</p>
<P>
<A NAme="idx1073742309"></a>basic syntax: <tT CLAss="monofont">xrange(</tt>
<I>
<TT Class="monofont">[initial_value,] final_value-1 [, step]</TT>
</I>
<Tt class="monofont">)</tt>
</p>
<p>See the section <a href="31.html">
"Data Structures"
</a> for details.</p>
<h5>
<tt ClaSs="monofont">len()</tt>
</H5>
<p>It returns the length/number of elements of <i>
<tt ClasS="monofont">string.</TT>
</I>
</p>
<p>
<a nAME="idx1073742310"></A>basic syntax: <tt clASS="monofont">len(</Tt>
<i>
<tt CLASs="monofont">variablename</tt>
</i>
<tt class="monofont">)</tt>
</p>
<h5>
<tt claSs="monofont">max()</tT>
</h5>
<p>It returns the maximum/largest element of <i>
<Tt claSs="monofont">sequence.</tt>
</I>
</P>
<P>
<A name="idx1073742311"></A>basic syntax: <TT Class="monofont">max(sequence)</TT>
</P>
<Pre>
>>> max(1, 2, 3)
3
>>> max("MY BRAIN HURTS")
"Y"
</prE>
<H5>
<TT class="monofont">min()</tt>
</h5>
<p>It returns the minimum/smallest element of <i>
<tt class="monofont">sequence.</tt>
</i>
<A naMe="idx1073742312"></a>
</p>
<P>
<a namE="idx1073742313"></a>basic syntax: <tt CLASs="monofont">min(</tt>
<i>
<TT CLass="monofont">sequence</tT>
</I>
<TT clasS="monofont">)</TT>
</P>
<pre>
>>> min("MY BRAIN HURTS")
" "
</pre>
<h5>
<tt class="monofont">zip()</tt>
</h5>
<p>It returns a list <a nAme="idx1073742314"></A>
<a naMe="idx1073742315"></a>
<a nAme="idx1073742316"></a>of tuples where each tuple contains the i-th element from each of the given sequences. This function generates a resulting list whose length is exactly the same as of the shortest given sequence. Note that, on the other hand, the function <TT CLass="monofont">map(None, sequence1, sequence2,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -