## 八、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)
(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)
(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)
(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)