## 八、Python的函数编程(之二)
----From a high schoolstudent's view to learn Python
关键字:
python 高中生学编程 MIT公开课视频Python函数编程 递归 递归函数 递归调用
四、Python’s Built-inFunctions
Python包含了很多内置的函数:
int()、float()、len()、type()、range()、print()、input()、abs()、str()、tuple()
在Python.org上有详细的描述
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td colspan="5" valign="middle" style="width: 415.2px; height: 17.2px; background-color: #f2e4f1; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #d6bad6 #000000 #000000 #000000"><p style="margin: 0px; text-align: center; font-size: 12px; line-height: normal; font-family: Helvetica; color: rgb(4, 51, 255);"><span style="text-decoration: underline ; letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>Built-inFunctions</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>abs()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>divmod()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>input()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>open()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>staticmethod()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>all()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>enumerate()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>int()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>ord()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>str()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>any()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>eval_r()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>isinstance()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>pow()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>sum()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>basestring()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>execfile()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>issubclass()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>print()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>super()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>bin()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>file()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>iter()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>property()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>tuple()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>bool()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>filter()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>len()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>range()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>type()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>bytearray()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>float()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>list()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>raw_input()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>unichr()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>callable()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>format()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>locals()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>reduce()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>unicode()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>chr()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>frozenset()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>long()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>reload()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>vars()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>classmethod()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>getattr()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>map()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>repr()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>xrange()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>cmp()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>globals()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>max()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>reversed()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>zip()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>compile()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>hasattr()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>memoryview()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>round()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>__import__()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>complex()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>hash()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>min()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>set()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>apply()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>delattr()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>help()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>next()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>setattr()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>buffer()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>dict()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>hex()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>object()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>slice()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>coerce()</b></a></span></p></td></tr><tr><td valign="middle" style="width: 88.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>dir()</b></a></span></p></td><td valign="middle" style="width: 76.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>id()</b></a></span></p></td><td valign="middle" style="width: 88.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>oct()</b></a></span></p></td><td valign="middle" style="width: 72.0px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.2px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>sorted()</b></a></span></p></td><td valign="middle" style="width: 89.7px; height: 17.2px; background-color: #f1f2ff; border-style: solid; border-width: 0.8px 0.8px 0.8px 0.2px; border-color: #000000 #000000 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(53, 95, 124);"><span style="letter-spacing: 0.0px"><a href="http://docs.python.org/2/library/functions.html"><b>intern()</b></a></span></p></td></tr></tbody></table>
这些函数多数都不是经常用到,所以大致了解一下即可,有一些印象,在需要的时候再仔细看看说明。
但在开头列的那几个函数,使用度非常高,应该仔细掌握,主要从以下几个方面进行了解:
函数的作用是什么?
函数的参数是什么类型、缺省参数是什么?
函数的返回值是什么类型?
dir([*object*])
Without arguments, return thelist of names in the current local scope. With an argument, attemptto return a list of valid attributes for that object.
If the object has a methodnamed __dir__(),this method will be called and must return the list of attributes.This allows objects that implement acustom [__getattr__()](http://docs.python.org/2/reference/datamodel.html) or [__getattribute__()](http://docs.python.org/2/reference/datamodel.html) functionto customize the way [dir()](http://docs.python.org/2/library/functions.html) reportstheir attributes.
If the object does notprovide __dir__(),the function tries its best to gather information from theobject’s __dict__ attribute,if defined, and from its type object. The resulting list is notnecessarily complete, and may be inaccurate when the object has acustom [__getattr__()](http://docs.python.org/2/reference/datamodel.html).
Thedefault [dir()](http://docs.python.org/2/library/functions.html) mechanismbehaves differently with different types of objects, as it attemptsto produce the most relevant, rather than complete,information:
1. If the object is a moduleobject, the list contains the names of the module’sattributes.
1. If the object is a type orclass object, the list contains the names of its attributes, andrecursively of the attributes of its bases.
1. Otherwise, the list containsthe object’s attributes’ names, the names of its class’sattributes, and recursively of the attributes of its class’s baseclasses.
The resulting list is sortedalphabetically. For example:
importstruct
dir() *#show the names in the module namespace*
['__builtins__', '__doc__','__name__', 'struct']
dir(struct) *#show the names in the struct module*
['Struct', '__builtins__','__doc__', '__file__', '__name__', '__package__', '_clearcache','calcsize', 'error', 'pack', 'pack_into', 'unpack','unpack_from']
classShape(object):
def__dir__(self):
return ['area', 'perimeter','location']
s=Shape()
dir(s)
['area', 'perimeter','location']
Note
Because [dir()](http://docs.python.org/2/library/functions.html) issupplied primarily as a convenience for use at an interactiveprompt, it tries to supply an interesting set of names more than ittries to supply a rigorously or consistently defined set of names,and its detailed behavior may change across releases. For example,metaclass attributes are not in the result list when the argumentis a class.
这是在一本书上找的两个练习:
1. dir()内建函数
- 启动Python 交互式解释器, 通过直接键入 dir() 回车以执行 dir() 内建函数。你看到 什么?显示你看到的每一个列表元素的值,记下实际值和你想像的值
- 你会问, dir() 函数是干什么的?我们已经知道在 dir 后边加上一对括号可以执行 dir() 内建函数, 如果不加括号会如何?试一试。解释器返回给你什么信息? 你认为这个信息表示什么意思 ?
- type() 内建函数接收任意的 Python对象做为参数并返回他们的类型。 在解释器中键入 type(dir),看看你得到的是什
- 本练习的最后一部分,我们来瞧一瞧 Python 的文档字符串。 通过dir.__doc__可以访问dir() 内建函数的文档字符串。printdir.__doc__可以显示这个字符串的内容。许多内建 函数,方法,模块及模块属性都有相应的文档字符串。我们希望你在你的代码中也要书写文档 字符串, 它会对使用这些代码的人提供及时方便的帮助。
2. 利用 dir() 找出 sys模块中更多的东西。
1. 启动Python交互解释器,执行dir()函数,然后键入 import sys 以导入 sys 模块。 再次执行 dir() 函数以确认sys 模块被正确的导入。 然后执行 dir(sys) , 你就可以看到 sys模块的所有属性了。
1. 显示 sys 模块的版本号属性及平台变量。记住在属性名前一定要加 sys. ,这表示这个属性是 sys 模块的。其中 version变量保存着你使用的 Python 解释器版本, platform 属性则包含你运行 Python时使用的计算机平台信息。
1. 最后,调用 sys.exit() 函数。这是一种热键之外的另一种退出Python 解释器的方式。
五、常用内建函数的使用
我们先从一些实际的例子着手:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.9px; height: 476.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td><td valign="top" style="width: 401.0px; height: 476.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>s = 'Hello, world.'</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>str(s)</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">'Hello, world.'</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>repr(s)</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">"'Hello, world.'"</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>str(0.1)</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">'0.1'</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>repr(0.1)</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">'0.10000000000000001'</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x = 10 * 3.25</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>y = 200 * 200</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) +'...'</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>print s</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">The value of x is 32.5, and yis 40000...</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>># The repr() of a string adds string quotes andbackslashes:</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... hello = 'hello,world\n'</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>hellos = repr(hello)</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>print hellos</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">'hello, world\n'</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>># The argument to repr() may be any Python object:</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">... repr((x, y, ('spam','eggs')))</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">"(32.5, 40000, ('spam','eggs'))"</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
how do you convert values tostrings? Luckily, Python has ways to convert any value to a string:pass it to the [**repr()**](http://www.python.org/doc//current/library/functions.html) or [**str()**](http://www.python.org/doc//current/library/functions.html) functions.
The str() function is meant toreturn representations of values which are fairly human-readable,while repr() is meant to generate representations which can be readby the interpreter (or will force a SyntaxError if there is notequivalent syntax). For objects which don’t have a particularrepresentation for human consumption, str() will return the samevalue as repr(). Many values, such as numbers or structures likelists and dictionaries, have the same representation using eitherfunction. Strings and floating point numbers, in particular, havetwo distinct representations.
Here are two ways to write atable of squares and cubes:
for x in range(1, 11):
... print repr(x).rjust(2),repr(x*x).rjust(3),
... # Note trailing comma on previousline
... print repr(x*x*x).rjust(4)
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
for x in range(1,11):
... print '{0:2d} {1:3d} {2:4d}'.format(x, x*x,x*x*x)
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
使用print进行格式化的结果输出,注意print、range等函数的使用
常用函数:
**abs(x)**
Return the absolute value of anumber. The argument may be a plain or long integer or a floatingpoint number. If the argument is a complex number, its magnitude isreturned.
**all(iterable)**
Return True if all elements ofthe iterable are true (or if the iterable is empty). Equivalentto:
def all(iterable):
for element in iterable:
if notelement:
return False
return True
**any(iterable)**
Return True if any element ofthe iterable is true. If the iterable is empty, return False.Equivalent to:
def any(iterable):
for element in iterable:
ifelement:
return True
return False
New in version 2.5.
**bin(x)**
Convert an integer number to abinary string. The result is a valid Python expression. If x is nota Python int object, it has to define an __index__() method thatreturns an integer.
New in version 2.6.
**bool([x])**
Convert a value to a Boolean,using the standard truth testing procedure. If x is false oromitted, this returns False; otherwise it returns True. bool isalso a class, which is a subclass of int. Class bool cannot besubclassed further. Its only instances are False andTrue.
New in version2.2.1.
Changed in version 2.3: If noargument is given, this function returns False.
**cmp(x, y)**
Compare the two objects x and yand return an integer according to the outcome. The return value isnegative if x < y, zero if x == y and strictlypositive if x > y.
**complex([real[,imag]])**
Create a complex number withthe value real + imag*j or convert a string or number to a complexnumber. If the first parameter is a string, it will be interpretedas a complex number and the function must be called without asecond parameter. The second parameter can never be a string. Eachargument may be any numeric type (including complex). If imag isomitted, it defaults to zero and the function serves as a numericconversion function like int(), long() and float(). If botharguments are omitted, returns 0j.
The complex type is describedin Numeric Types — int, float, long, complex.
**dir([object])**
Without arguments, return thelist of names in the current local scope. With an argument, attemptto return a list of valid attributes for that object.
If the object has a methodnamed __dir__(), this method will be called and must return thelist of attributes. This allows objects that implement a custom__getattr__() or __getattribute__() function to customize the waydir() reports their attributes.
If the object does not provide__dir__(), the function tries its best to gather information fromthe object’s __dict__ attribute, if defined, and from its typeobject. The resulting list is not necessarily complete, and may beinaccurate when the object has a custom __getattr__().
The default dir() mechanismbehaves differently with different types of objects, as it attemptsto produce the most relevant, rather than complete,information:
If the object is a moduleobject, the list contains the names of the module’sattributes.
If the object is a type orclass object, the list contains the names of its attributes, andrecursively of the attributes of its bases.
Otherwise, the list containsthe object’s attributes’ names, the names of its class’sattributes, and recursively of the attributes of its class’s baseclasses.
The resulting list is sortedalphabetically. For example:
import struct
dir() # doctest: +SKIP
['__builtins__', '__doc__','__name__', 'struct']
dir(struct) # doctest:+NORMALIZE_WHITESPACE
['Struct', '__builtins__','__doc__', '__file__', '__name__',
'__package__','_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
'unpack','unpack_from']
class Foo(object):
... def __dir__(self):
... return["kan", "ga", "roo"]
...
f = Foo()
dir(f)
['ga', 'kan', 'roo']
Note Because dir() is suppliedprimarily as a convenience for use at an interactive prompt, ittries to supply an interesting set of names more than it tries tosupply a rigorously or consistently defined set of names, and itsdetailed behavior may change across releases. For example,metaclass attributes are not in the result list when the argumentis a class.
**divmod(a, b)**
Take two (non complex) numbersas arguments and return a pair of numbers consisting of theirquotient and remainder when using long division. With mixed operandtypes, the rules for binary arithmetic operators apply. For plainand long integers, the result is the same as (a // b, a % b). Forfloating point numbers the result is (q, a % b), where q is usuallymath.floor(a / b) but may be 1 less than that. In any case q * b +a % b is very close to a, if a % b is non-zero it has the same signas b, and 0 <= abs(a % b) <abs(b).
Changed in version 2.3: Usingdivmod() with complex numbers is deprecated.
**enumerate(sequence[,start=0])**
Return an enumerate object.sequence must be a sequence, an iterator, or some other objectwhich supports iteration. The next() method of the iteratorreturned by enumerate() returns a tuple containing a count (fromstart which defaults to 0) and the corresponding value obtainedfrom iterating over iterable. enumerate() is useful for obtainingan indexed series: (0, seq[0]), (1, seq[1]), (2, seq[2]), .... Forexample:
for i, season in enumerate(['Spring', 'Summer', 'Fall','Winter']):
... print i, season
0 Spring
1 Summer
2 Fall
3 Winter
New in version 2.3.
New in version 2.6: The startparameter.
**float([x])**
Convert a string or a number tofloating point. If the argument is a string, it must contain apossibly signed decimal or floating point number, possibly embeddedin whitespace. The argument may also be [+|-]nan or [+|-]inf.Otherwise, the argument may be a plain or long integer or afloating point number, and a floating point number with the samevalue (within Python’s floating point precision) is returned. If noargument is given, returns 0.0.
Note When passing in a string,values for NaN and Infinity may be returned, depending on theunderlying C library. Float accepts the strings nan, inf and -inffor NaN and positive or negative infinity. The case and a leading +are ignored as well as a leading - is ignored for NaN. Float alwaysrepresents NaN and infinity as nan, inf or -inf.
The float type is described inNumeric Types — int, float, long, complex.
**help([object])**
Invoke the built-in helpsystem. (This function is intended for interactive use.) If noargument is given, the interactive help system starts on theinterpreter console. If the argument is a string, then the stringis looked up as the name of a module, function, class, method,keyword, or documentation topic, and a help page is printed on theconsole. If the argument is any other kind of object, a help pageon the object is generated.
This function is added to thebuilt-in namespace by the site module.
New in version 2.2.
**hex(x)**
Convert an integer number (ofany size) to a hexadecimal string. The result is a valid Pythonexpression.
Note To obtain a hexadecimalstring representation for a float, use the float.hex()method.
Changed in version 2.4:Formerly only returned an unsigned literal.
**int([x[,base]])**
Convert a string or number to aplain integer. If the argument is a string, it must contain apossibly signed decimal number representable as a Python integer,possibly embedded in whitespace. The base parameter gives the basefor the conversion (which is 10 by default) and may be any integerin the range [2, 36], or zero. If base is zero, the proper radix isdetermined based on the contents of string; the interpretation isthe same as for integer literals. (See Numeric literals.) If baseis specified and x is not a string, TypeError is raised. Otherwise,the argument may be a plain or long integer or a floating pointnumber. Conversion of floating point numbers to integers truncates(towards zero). If the argument is outside the integer range a longobject will be returned instead. If no arguments are given, returns0.
The integer type is describedin Numeric Types — int, float, long, complex.
**len(s)**
Return the length (the numberof items) of an object. The argument may be a sequence (string,tuple or list) or a mapping (dictionary).
list([iterable])
Return a list whose items arethe same and in the same order as iterable‘s items. iterable may beeither a sequence, a container that supports iteration, or aniterator object. If iterable is already a list, a copy is made andreturned, similar to iterable[:]. For instance, list('abc') returns['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If noargument is given, returns a new empty list, [].
list is a mutable sequencetype, as documented in Sequence Types — str, unicode, list, tuple,buffer, xrange. For other containers see the built in dict, set,and tuple classes, and the collections module.
**max(iterable[, args...][,key])**
With a single argumentiterable, return the largest item of a non-empty iterable (such asa string, tuple or list). With more than one argument, return thelargest of the arguments.
The optional key argumentspecifies a one-argument ordering function like that used forlist.sort(). The key argument, if supplied, must be in keyword form(for example, max(a,b,c,key=func)).
Changed in version 2.5: Addedsupport for the optional key argument.
**min(iterable[, args...][,key])**
With a single argumentiterable, return the smallest item of a non-empty iterable (such asa string, tuple or list). With more than one argument, return thesmallest of the arguments.
The optional key argumentspecifies a one-argument ordering function like that used forlist.sort(). The key argument, if supplied, must be in keyword form(for example, min(a,b,c,key=func)).
Changed in version 2.5: Addedsupport for the optional key argument.
**oct(x)**
Convert an integer number (ofany size) to an octal string. The result is a valid Pythonexpression.
Changed in version 2.4:Formerly only returned an unsigned literal.
**print([object, ...][, sep=''][, end='\n'][, file=sys.stdout])**
Print object(s) to the streamfile, separated by sep and followed by end. sep, end and file, ifpresent, must be given as keyword arguments.
All non-keyword arguments areconverted to strings like str() does and written to the stream,separated by sep and followed by end. Both sep and end must bestrings; they can also be None, which means to use the defaultvalues. If no object is given, print() will just writeend.
The file argument must be anobject with a write(string) method; if it is not present or None,sys.stdout will be used.
Note This function is notnormally available as a built-in since the name print is recognizedas the print statement. To disable the statement and use theprint() function, use this future statement at the top of yourmodule:
from __future__ importprint_function
New in version 2.6.
**range([start], stop[,step])**
This is a versatile function tocreate lists containing arithmetic progressions. It is most oftenused in for loops. The arguments must be plain integers. If thestep argument is omitted, it defaults to 1. If the start argumentis omitted, it defaults to 0. The full form returns a list of plainintegers [start, start + step, start + 2 * step, ...]. If step ispositive, the last element is the largest start + i * step lessthan stop; if step is negative, the last element is the smalleststart + i * step greater than stop. step must not be zero (or elseValueError is raised). Example:
range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8,9]
>>>range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9,10]
range(0, 30, 5)
[0, 5, 10, 15, 20,25]
range(0, 10, 3)
[0, 3, 6, 9]
range(0, -10, -1)
[0, -1, -2, -3, -4, -5, -6, -7,-8, -9]
range(0)
[]
range(1, 0)
[]
**raw_input([prompt])**
If the prompt argument ispresent, it is written to standard output without a trailingnewline. The function then reads a line from input, converts it toa string (stripping a trailing newline), and returns that. When EOFis read, EOFError is raised. Example:
s = raw_input('--> ')
MontyPython's Flying Circus
s
"Monty Python's FlyingCircus"
If the readline module wasloaded, then raw_input() will use it to provide elaborate lineediting and history features.
**repr(object)**
Return a string containing aprintable representation of an object. This is the same valueyielded by conversions (reverse quotes). It is sometimes useful tobe able to access this operation as an ordinary function. For manytypes, this function makes an attempt to return a string that wouldyield an object with the same value when passed to eval_r(),otherwise the representation is a string enclosed in angle bracketsthat contains the name of the type of the object together withadditional information often including the name and address of theobject. A class can control what this function returns for itsinstances by defining a __repr__() method.
**reversed(seq)**
Return a reverse iterator. seqmust be an object which has a __reversed__() method or supports thesequence protocol (the __len__() method and the __getitem__()method with integer arguments starting at 0).
New in version 2.4.
Changed in version 2.6: Addedthe possibility to write a custom __reversed__() method.
**round(x[, n])**
Return the floating point valuex rounded to n digits after the decimal point. If n is omitted, itdefaults to zero. The result is a floating point number. Values arerounded to the closest multiple of 10 to the power minus n; if twomultiples are equally close, rounding is done away from 0 (so. forexample, round(0.5) is 1.0 and round(-0.5) is -1.0).
**sorted(iterable[, cmp[,key[, reverse]]])**
Return a new sorted list fromthe items in iterable.
The optional arguments cmp,key, and reverse have the same meaning as those for the list.sort()method (described in section Mutable Sequence Types).
cmp specifies a customcomparison function of two arguments (iterable elements) whichshould return a negative, zero or positive number depending onwhether the first argument is considered smaller than, equal to, orlarger than the second argument: cmp=lambda x,y: cmp(x.lower(),y.lower()). The default value is None.
key specifies a function of oneargument that is used to extract a comparison key from each listelement: key=str.lower. The default value is None.
reverse is a boolean value. Ifset to True, then the list elements are sorted as if eachcomparison were reversed.
In general, the key and reverseconversion processes are much faster than specifying an equivalentcmp function. This is because cmp is called multiple times for eachlist element while key and reverse touch each element only once. Toconvert an old-style cmp function to a key function, see theCmpToKey recipe in the ASPN cookbook.
New in version 2.4.
**str([object])**
Return a string containing anicely printable representation of an object. For strings, thisreturns the string itself. The difference with repr(object) is thatstr(object) does not always attempt to return a string that isacceptable to eval_r(); its goal is to return a printable string.If no argument is given, returns the empty string, ''.
For more information on stringssee Sequence Types — str, unicode, list, tuple, buffer, xrangewhich describes sequence functionality (strings are sequences), andalso the string-specific methods described in the String Methodssection. To output formatted strings use template strings or the %operator described in the String Formatting Operations section. Inaddition see the String Services section. See alsounicode().
**sum(iterable[,start])**
Sums start and the items of aniterable from left to right and returns the total. start defaultsto 0. The iterable‘s items are normally numbers, and are notallowed to be strings. The fast, correct way to concatenate asequence of strings is by calling ''.join(sequence). Note thatsum(range(n), m) is equivalent to reduce(operator.add, range(n), m)To add floating point values with extended precision, seemath.fsum().
New in version 2.3.
**tuple([iterable])**
Return a tuple whose items arethe same and in the same order as iterable‘s items. iterable may bea sequence, a container that supports iteration, or an iteratorobject. If iterable is already a tuple, it is returned unchanged.For instance, tuple('abc') returns ('a', 'b', 'c') and tuple([1, 2,3]) returns (1, 2, 3). If no argument is given, returns a new emptytuple, ().
tuple is an immutable sequencetype, as documented in Sequence Types — str, unicode, list, tuple,buffer, xrange. For other containers see the built in dict, list,and set classes, and the collections module.
**type(object)**
Return the type of an object.The return value is a type object. The isinstance() built-infunction is recommended for testing the type of anobject.
With three arguments, type()functions as a constructor as detailed below.
**xrange([start], stop[,step])**
This function is very similarto range(), but returns an “xrange object” instead of a list. Thisis an opaque sequence type which yields the same values as thecorresponding list, without actually storing them allsimultaneously. The advantage of xrange() over range() is minimal(since xrange() still has to create the values when asked for them)except when a very large range is used on a memory-starved machineor when all of the range’s elements are never used (such as whenthe loop is usually terminated with break).
CPython implementation detail:xrange() is intended to be simple and fast. Implementations mayimpose restrictions to achieve this. The C implementation of Pythonrestricts all arguments to native C longs (“short” Pythonintegers), and also requires that the number of elements fit in anative C long. If a larger range is needed, an alternate versioncan be crafted using the itertools module: islice(count(start,step), (stop-start+step-1)//step).
六、递归
------
如果函数包含了对其自身的调用,该函数就是递归的。如果一个新的调用能在相同过程中较早的调用结束之前开始,那么该过程就是递归的。
可以看一段MIT公开课视频:
递归广泛地应用于使用递归函数的数学应用中,类似数学归纳法。
拿阶乘函数的定义来说明:
N! == factorial(N) == 1 * 2 * 3... * N
我们可以用这种方式来看阶乘:
~~~
factorial(N) = N!
= N * (N-1)!
= N * (N-1) * (N-2)!
…
= N * (N-1) * (N-2) ... * 3 * 2 * 1
~~~
我们现在可以看到阶乘是递归的,因为
factorial(N) = N*factorial(N-1)
换句话说,为了获得 factorial(N)的值,需要计算factorial(N-1),而且,为了找到 factorial(N-1),需要计算factorial(N-2)等等。我们现在给出阶乘函数的递归版本。
~~~
def factorial(n):
... if n==0 or n==1:
... return 1
... else:
... returnn*factorial(n-1)
...
factorial(21)
51090942171709440000L
factorial(4)
24
factorial(1)
1
factorial(0)
1
~~~
特别强调:
1. 递归函数必须有一个终止执行的条件,如我们的例子中,当n=0或者n=1时,会终止;否则,程序将进入一个无限循环的状态,俗称“死循环”
1. 递归虽然使程序看起来非常的简单,但是递归程序如果递归的次数很大的话,将会严重的消耗计算机的内存,甚至可能导致系统的崩溃
下面给出一段视频,里面是以经典的递归问题:斐波那契数列作为例子进行讲解的,值得学习!
【插入视频】
我的更多文章:
- [Python程序调试的一些体会](http://blog.sina.com.cn/s/blog_d6cca93e0101ewc9.html)(2013-10-06 22:57:35)
- [十四、Python编程计算24点(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euxx.html)(2013-10-03 22:18:28)
- [十三、Python编程计算24点(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101eukc.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片")
(2013-10-02 22:15:46)
- [十二、Python简单数据结构应用(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euk8.html)(2013-10-02 22:10:41)
- [十一、Python简单数据结构应用(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ep9z.html)(2013-09-23 23:31:49)
- [十、Python编程解决组合问题(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101entc.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片")
(2013-09-21 23:37:27)
- [九、Python编程解决组合问题(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ent7.html)(2013-09-21 23:32:54)
- [七、Python的函数编程(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwg.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频")
(2013-09-20 23:09:10)
- [六、Python的程序流程](http://blog.sina.com.cn/s/blog_d6cca93e0101ejeg.html)(2013-09-19 16:53:58)
- [高中生如何学编程](http://blog.sina.com.cn/s/blog_d6cca93e0101e8fn.html)(2013-09-02 19:26:01)