{"id":44114,"date":"2025-06-17T01:43:28","date_gmt":"2025-06-16T17:43:28","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44114.html"},"modified":"2025-06-17T01:43:28","modified_gmt":"2025-06-16T17:43:28","slug":"python-100%e4%b8%aa%e5%b8%b8%e7%94%a8%e5%87%bd%e6%95%b0%e5%85%a8%e9%9d%a2%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/44114.html","title":{"rendered":"Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790"},"content":{"rendered":"<h2>Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790<\/h2>\n<h3>1. \u7c7b\u578b\u8f6c\u6362\u51fd\u6570<\/h3>\n<h4>1.1 int()<\/h4>\n<p>\u5c06\u5b57\u7b26\u4e32\u6216\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nint(&#039;123&#039;)  # 123<br \/>\nint(3.14)   # 3<\/p>\n<p># \u6307\u5b9a\u8fdb\u5236\u8f6c\u6362<br \/>\nint(&#039;1010&#039;, 2)  # 10 (\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236)<br \/>\nint(&#039;FF&#039;, 16)   # 255 (\u5341\u516d\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nint(&#039;&#039;)       # ValueError: invalid literal for int() with base 10: &#039;&#039;<br \/>\nint(None)     # TypeError: int() argument must be a string, a bytes-like object or a number, not &#039;NoneType&#039;<br \/>\nint(&#039;3.14&#039;)   # ValueError: invalid literal for int() with base 10: &#039;3.14&#039;<br \/>\nint(float(&#039;inf&#039;))  # OverflowError: cannot convert float infinity to integer<\/p>\n<h4>1.2 float()<\/h4>\n<p>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nfloat(&#039;3.14&#039;)  # 3.14<br \/>\nfloat(3)       # 3.0<\/p>\n<p># \u7279\u6b8a\u503c\u8f6c\u6362<br \/>\nfloat(&#039;inf&#039;)   # inf<br \/>\nfloat(&#039;-inf&#039;)  # -inf<br \/>\nfloat(&#039;nan&#039;)   # nan<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nfloat(&#039;&#039;)       # ValueError: could not convert string to float: &#039;&#039;<br \/>\nfloat(None)     # TypeError: float() argument must be a string or a number, not &#039;NoneType&#039;<br \/>\nfloat(&#039;3.14a&#039;)  # ValueError: could not convert string to float: &#039;3.14a&#039;<\/p>\n<h4>1.3 str()<\/h4>\n<p>\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nstr(123)     # &#039;123&#039;<br \/>\nstr(3.14)    # &#039;3.14&#039;<br \/>\nstr(True)    # &#039;True&#039;<\/p>\n<p># \u7279\u6b8a\u5bf9\u8c61\u8f6c\u6362<br \/>\nstr(None)    # &#039;None&#039;<br \/>\nstr([1,2,3]) # &#039;[1, 2, 3]&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nstr(float(&#039;inf&#039;))  # &#039;inf&#039;<br \/>\nstr(float(&#039;nan&#039;))  # &#039;nan&#039;<br \/>\nstr(object())      # &#039;&lt;object object at 0x&#8230;&gt;&#039;<\/p>\n<h4>1.4 bool()<\/h4>\n<p>\u5c06\u503c\u8f6c\u6362\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nbool(1)      # True<br \/>\nbool(0)      # False<br \/>\nbool(&#039;&#039;)     # False<br \/>\nbool(&#039;abc&#039;)  # True<\/p>\n<p># \u7279\u6b8a\u503c\u8f6c\u6362<br \/>\nbool(None)   # False<br \/>\nbool([])     # False<br \/>\nbool([0])    # True<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nbool(float(&#039;inf&#039;))  # True<br \/>\nbool(float(&#039;nan&#039;))  # True<\/p>\n<h4>1.5 list()<\/h4>\n<p>\u521b\u5efa\u5217\u8868\u6216\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlist(&#039;abc&#039;)       # [&#039;a&#039;, &#039;b&#039;, &#039;c&#039;]<br \/>\nlist((1,2,3))     # [1, 2, 3]<\/p>\n<p># \u7a7a\u5217\u8868\u521b\u5efa<br \/>\nlist()            # []<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlist(None)        # TypeError: &#039;NoneType&#039; object is not iterable<br \/>\nlist(123)         # TypeError: &#039;int&#039; object is not iterable<br \/>\nlist({&#039;a&#039;:1})     # [&#039;a&#039;] (\u5b57\u5178\u9ed8\u8ba4\u8fed\u4ee3\u952e)<\/p>\n<h4>1.6 tuple()<\/h4>\n<p>\u521b\u5efa\u5143\u7ec4\u6216\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u4e3a\u5143\u7ec4\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\ntuple([1,2,3])   # (1, 2, 3)<br \/>\ntuple(&#039;abc&#039;)     # (&#039;a&#039;, &#039;b&#039;, &#039;c&#039;)<\/p>\n<p># \u7a7a\u5143\u7ec4\u521b\u5efa<br \/>\ntuple()          # ()<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\ntuple(None)      # TypeError: &#039;NoneType&#039; object is not iterable<br \/>\ntuple(123)       # TypeError: &#039;int&#039; object is not iterable<\/p>\n<h4>1.7 set()<\/h4>\n<p>\u521b\u5efa\u96c6\u5408\u6216\u53bb\u9664\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nset([1,2,2,3])   # {1, 2, 3}<br \/>\nset(&#039;aabbcc&#039;)    # {&#039;a&#039;, &#039;b&#039;, &#039;c&#039;}<\/p>\n<p># \u7a7a\u96c6\u5408\u521b\u5efa<br \/>\nset()            # set()<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nset(None)        # TypeError: &#039;NoneType&#039; object is not iterable<br \/>\nset(123)         # TypeError: &#039;int&#039; object is not iterable<br \/>\nset([[]])        # TypeError: unhashable type: &#039;list&#039;<\/p>\n<h4>1.8 dict()<\/h4>\n<p>\u521b\u5efa\u5b57\u5178\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\ndict(a&#061;1, b&#061;2)        # {&#039;a&#039;: 1, &#039;b&#039;: 2}<br \/>\ndict([(&#039;a&#039;,1),(&#039;b&#039;,2)]) # {&#039;a&#039;: 1, &#039;b&#039;: 2}<\/p>\n<p># \u7a7a\u5b57\u5178\u521b\u5efa<br \/>\ndict()                # {}<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\ndict(None)            # TypeError: cannot convert dictionary update sequence element #0 to a sequence<br \/>\ndict(123)             # TypeError: cannot convert dictionary update sequence element #0 to a sequence<br \/>\ndict([(&#039;a&#039;,1), None]) # TypeError: cannot convert dictionary update sequence element #1 to a sequence<\/p>\n<h3>2. \u8f93\u5165\u8f93\u51fa\u51fd\u6570<\/h3>\n<h4>2.9 input()<\/h4>\n<p>\u4ece\u63a7\u5236\u53f0\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# name &#061; input(&#034;\u8bf7\u8f93\u5165\u4f60\u7684\u540d\u5b57: &#034;)  # \u7528\u6237\u8f93\u5165\u4f1a\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u8f93\u5165Ctrl&#043;D (Unix) \u6216 Ctrl&#043;Z (Windows) \u4f1a\u5f15\u53d1 EOFError<\/p>\n<h4>2.10 print()<\/h4>\n<p>\u5c06\u6307\u5b9a\u7684\u5bf9\u8c61\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nprint(&#039;Hello&#039;, &#039;World&#039;)  # Hello World<br \/>\nprint(1, 2, 3, sep&#061;&#039;-&#039;)  # 1-2-3<\/p>\n<p># \u53c2\u6570\u8bf4\u660e<br \/>\n# sep: \u5206\u9694\u7b26&#xff0c;\u9ed8\u8ba4\u4e3a\u7a7a\u683c<br \/>\n# end: \u7ed3\u675f\u5b57\u7b26&#xff0c;\u9ed8\u8ba4\u4e3a\u6362\u884c\u7b26<br \/>\n# file: \u8f93\u51fa\u6587\u4ef6\u5bf9\u8c61&#xff0c;\u9ed8\u8ba4\u4e3asys.stdout<br \/>\n# flush: \u662f\u5426\u7acb\u5373\u5237\u65b0\u7f13\u51b2\u533a&#xff0c;\u9ed8\u8ba4\u4e3aFalse<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nprint(None)      # None<br \/>\nprint(float(&#039;inf&#039;))  # inf<br \/>\nprint(float(&#039;nan&#039;))  # nan<\/p>\n<h3>3. \u6570\u5b66\u8fd0\u7b97\u51fd\u6570<\/h3>\n<h4>3.11 abs()<\/h4>\n<p>\u8fd4\u56de\u4e00\u4e2a\u6570\u7684\u7edd\u5bf9\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nabs(-5)      # 5<br \/>\nabs(3.14)    # 3.14<\/p>\n<p># \u590d\u6570\u7edd\u5bf9\u503c<br \/>\nabs(3&#043;4j)    # 5.0 (\u8fd4\u56de\u6a21)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nabs(float(&#039;inf&#039;))  # inf<br \/>\nabs(float(&#039;-inf&#039;)) # inf<br \/>\nabs(float(&#039;nan&#039;))  # nan<\/p>\n<h4>3.12 max()<\/h4>\n<p>\u8fd4\u56de\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6700\u5927\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nmax([1, 2, 3])     # 3<br \/>\nmax(&#039;a&#039;, &#039;b&#039;, &#039;c&#039;) # &#039;c&#039;<\/p>\n<p># \u6307\u5b9akey\u51fd\u6570<br \/>\nmax([&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;], key&#061;len)  # &#039;banana&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nmax([])            # ValueError: max() arg is an empty sequence<br \/>\nmax([float(&#039;nan&#039;), 1, 2])  # nan (\u4f46\u6bd4\u8f83nan\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u4e00\u81f4)<br \/>\nmax([None, 1, 2])  # TypeError: &#039;&gt;&#039; not supported between instances of &#039;int&#039; and &#039;NoneType&#039;<\/p>\n<h4>3.13 min()<\/h4>\n<p>\u8fd4\u56de\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6700\u5c0f\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nmin([1, 2, 3])     # 1<br \/>\nmin(&#039;a&#039;, &#039;b&#039;, &#039;c&#039;) # &#039;a&#039;<\/p>\n<p># \u6307\u5b9akey\u51fd\u6570<br \/>\nmin([&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;], key&#061;len)  # &#039;apple&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nmin([])            # ValueError: min() arg is an empty sequence<br \/>\nmin([float(&#039;nan&#039;), 1, 2])  # nan (\u4f46\u6bd4\u8f83nan\u7684\u884c\u4e3a\u53ef\u80fd\u4e0d\u4e00\u81f4)<br \/>\nmin([None, 1, 2])  # TypeError: &#039;&lt;&#039; not supported between instances of &#039;int&#039; and &#039;NoneType&#039;<\/p>\n<h4>3.14 sum()<\/h4>\n<p>\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u6c42\u548c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nsum([1, 2, 3])     # 6<br \/>\nsum([1.5, 2.5, 3]) # 7.0<\/p>\n<p># \u6307\u5b9a\u8d77\u59cb\u503c<br \/>\nsum([1, 2, 3], 10) # 16<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nsum([])            # 0<br \/>\nsum([&#039;a&#039;, &#039;b&#039;])    # TypeError: unsupported operand type(s) for &#043;: &#039;int&#039; and &#039;str&#039;<br \/>\nsum([float(&#039;inf&#039;), 1])  # inf<br \/>\nsum([float(&#039;nan&#039;), 1])  # nan<\/p>\n<h4>3.15 round()<\/h4>\n<p>\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nround(3.14159)     # 3<br \/>\nround(3.14159, 2)  # 3.14<\/p>\n<p># \u94f6\u884c\u5bb6\u820d\u5165\u6cd5(\u56db\u820d\u516d\u5165\u4e94\u6210\u53cc)<br \/>\nround(2.5)         # 2<br \/>\nround(3.5)         # 4<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nround(float(&#039;inf&#039;))  # inf<br \/>\nround(float(&#039;nan&#039;))  # nan<br \/>\nround(123.456, -2)   # 100.0 (\u8d1f\u7684ndigits\u53c2\u6570)<br \/>\nround(123.456, 300)  # 123.456 (\u8fc7\u5927ndigits\u53c2\u6570)<\/p>\n<h3>4. \u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6570<\/h3>\n<h4>4.16 len()<\/h4>\n<p>\u8fd4\u56de\u5bf9\u8c61\u7684\u957f\u5ea6\u6216\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlen(&#039;abc&#039;)      # 3<br \/>\nlen([1,2,3])    # 3<br \/>\nlen({&#039;a&#039;:1})    # 1<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlen(&#039;&#039;)         # 0<br \/>\nlen(None)       # TypeError: object of type &#039;NoneType&#039; has no len()<br \/>\nlen(123)        # TypeError: object of type &#039;int&#039; has no len()<\/p>\n<h4>4.17 str.split()<\/h4>\n<p>\u4ee5\u6307\u5b9a\u5b57\u7b26\u4e3a\u5206\u9694\u7b26\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;apple,banana,cherry&#039;.split(&#039;,&#039;)  # [&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;]<\/p>\n<p># \u6307\u5b9a\u6700\u5927\u5206\u5272\u6b21\u6570<br \/>\n&#039;apple,banana,cherry&#039;.split(&#039;,&#039;, 1)  # [&#039;apple&#039;, &#039;banana,cherry&#039;]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;&#039;.split(&#039;,&#039;)    # [&#039;&#039;]<br \/>\n&#039;   &#039;.split()    # [] (\u9ed8\u8ba4\u5206\u5272\u7a7a\u767d\u5b57\u7b26)<br \/>\nNone.split()     # AttributeError: &#039;NoneType&#039; object has no attribute &#039;split&#039;<\/p>\n<h4>4.18 str.join()<\/h4>\n<p>\u7528\u6307\u5b9a\u5b57\u7b26\u4e32\u8fde\u63a5\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;,&#039;.join([&#039;a&#039;, &#039;b&#039;, &#039;c&#039;])  # &#039;a,b,c&#039;<br \/>\n&#039;-&#039;.join(&#039;abc&#039;)            # &#039;a-b-c&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;&#039;.join([])                # &#039;&#039;<br \/>\n&#039;,&#039;.join([1, 2, 3])        # TypeError: sequence item 0: expected str instance, int found<br \/>\n&#039;,&#039;.join(None)             # TypeError: can only join an iterable<\/p>\n<h4>4.19 str.find()<\/h4>\n<p>\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u5b50\u4e32&#xff0c;\u8fd4\u56de\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;hello world&#039;.find(&#039;world&#039;)  # 6<br \/>\n&#039;hello world&#039;.find(&#039;o&#039;)      # 4<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;hello&#039;.find(&#039;x&#039;)            # -1 (\u672a\u627e\u5230)<br \/>\n&#039;&#039;.find(&#039;&#039;)                  # 0<br \/>\n&#039;hello&#039;.find(&#039;&#039;)             # 0<br \/>\n&#039;hello&#039;.find(None)           # TypeError: must be str, not NoneType<\/p>\n<h4>4.20 str.rfind()<\/h4>\n<p>\u4ece\u53f3\u4fa7\u5f00\u59cb\u67e5\u627e\u5b50\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;hello world&#039;.rfind(&#039;o&#039;)     # 7<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;hello&#039;.rfind(&#039;x&#039;)           # -1<br \/>\n&#039;&#039;.rfind(&#039;&#039;)                 # 0<br \/>\n&#039;hello&#039;.rfind(&#039;&#039;, 10)        # 5 (\u8d85\u8fc7\u5b57\u7b26\u4e32\u957f\u5ea6)<\/p>\n<h4>4.21 str.replace()<\/h4>\n<p>\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b50\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;hello world&#039;.replace(&#039;world&#039;, &#039;Python&#039;)  # &#039;hello Python&#039;<\/p>\n<p># \u6307\u5b9a\u66ff\u6362\u6b21\u6570<br \/>\n&#039;ababab&#039;.replace(&#039;a&#039;, &#039;c&#039;, 2)  # &#039;cbcbab&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;hello&#039;.replace(&#039;&#039;, &#039;-&#039;)       # &#039;-h-e-l-l-o-&#039;<br \/>\n&#039;hello&#039;.replace(&#039;x&#039;, &#039;y&#039;)      # &#039;hello&#039; (\u65e0\u5339\u914d)<br \/>\n&#039;hello&#039;.replace(None, &#039;y&#039;)     # TypeError: replace() argument 1 must be str, not NoneType<\/p>\n<h4>4.22 str.strip()<\/h4>\n<p>\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;  hello  &#039;.strip()       # &#039;hello&#039;<br \/>\n&#039;\\\\thello\\\\n&#039;.strip()       # &#039;hello&#039;<\/p>\n<p># \u6307\u5b9a\u53bb\u9664\u5b57\u7b26<br \/>\n&#039;xxhelloxx&#039;.strip(&#039;x&#039;)    # &#039;hello&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;&#039;.strip()                # &#039;&#039;<br \/>\n&#039;  &#039;.strip()              # &#039;&#039;<br \/>\nNone.strip()              # AttributeError<\/p>\n<h4>4.23 str.lstrip()<\/h4>\n<p>\u53bb\u9664\u5b57\u7b26\u4e32\u5de6\u4fa7\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;  hello  &#039;.lstrip()      # &#039;hello  &#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;&#039;.lstrip()               # &#039;&#039;<br \/>\nNone.lstrip()             # AttributeError<\/p>\n<h4>4.24 str.rstrip()<\/h4>\n<p>\u53bb\u9664\u5b57\u7b26\u4e32\u53f3\u4fa7\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;  hello  &#039;.rstrip()      # &#039;  hello&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;&#039;.rstrip()               # &#039;&#039;<br \/>\nNone.rstrip()             # AttributeError<\/p>\n<h4>4.25 str.upper()<\/h4>\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;Hello&#039;.upper()           # &#039;HELLO&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;&#039;.upper()                # &#039;&#039;<br \/>\n&#039;123&#039;.upper()             # &#039;123&#039;<br \/>\nNone.upper()              # AttributeError<\/p>\n<h4>4.26 str.lower()<\/h4>\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;Hello&#039;.lower()           # &#039;hello&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;&#039;.lower()                # &#039;&#039;<br \/>\n&#039;123&#039;.lower()             # &#039;123&#039;<br \/>\nNone.lower()              # AttributeError<\/p>\n<h4>4.27 str.title()<\/h4>\n<p>\u5c06\u6bcf\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\u5927\u5199\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n&#039;hello world&#039;.title()     # &#039;Hello World&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n&#039;&#039;.title()                # &#039;&#039;<br \/>\n&#034;they&#039;re bill&#039;s&#034;.title()  # &#034;They&#039;Re Bill&#039;S&#034; (\u6ce8\u610f\u6487\u53f7\u540e\u7684\u5b57\u6bcd)<br \/>\nNone.title()              # AttributeError<\/p>\n<h3>5. \u5217\u8868\u64cd\u4f5c\u51fd\u6570<\/h3>\n<h4>5.28 list.append()<\/h4>\n<p>\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [1, 2]<br \/>\nlst.append(3)    # lst\u53d8\u4e3a[1, 2, 3]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlst.append(None) # lst\u53d8\u4e3a[1, 2, 3, None]<br \/>\nlst.append(lst)  # \u53ef\u4ee5\u6dfb\u52a0\u81ea\u8eab(\u521b\u5efa\u5faa\u73af\u5f15\u7528)<\/p>\n<h4>5.29 list.extend()<\/h4>\n<p>\u7528\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6269\u5c55\u5217\u8868\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [1, 2]<br \/>\nlst.extend([3, 4])  # lst\u53d8\u4e3a[1, 2, 3, 4]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlst.extend(&#039;abc&#039;)   # lst\u53d8\u4e3a[1, 2, 3, 4, &#039;a&#039;, &#039;b&#039;, &#039;c&#039;]<br \/>\nlst.extend(None)    # TypeError: &#039;NoneType&#039; object is not iterable<\/p>\n<h4>5.30 list.insert()<\/h4>\n<p>\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [1, 3]<br \/>\nlst.insert(1, 2)    # lst\u53d8\u4e3a[1, 2, 3]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlst.insert(-10, 0)  # \u63d2\u5165\u5230\u6700\u524d\u9762<br \/>\nlst.insert(100, 4)  # \u63d2\u5165\u5230\u6700\u540e\u9762<br \/>\nlst.insert(1, None) # \u53ef\u4ee5\u63d2\u5165None<\/p>\n<h4>5.31 list.remove()<\/h4>\n<p>\u79fb\u9664\u5217\u8868\u4e2d\u6307\u5b9a\u503c\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [1, 2, 3, 2]<br \/>\nlst.remove(2)       # lst\u53d8\u4e3a[1, 3, 2]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlst.remove(4)       # ValueError: list.remove(x): x not in list<br \/>\nlst.remove(None)    # \u5982\u679c\u5217\u8868\u4e2d\u6709None\u53ef\u4ee5\u79fb\u9664<\/p>\n<h4>5.32 list.pop()<\/h4>\n<p>\u79fb\u9664\u5e76\u8fd4\u56de\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [1, 2, 3]<br \/>\nlst.pop()           # \u8fd4\u56de3, lst\u53d8\u4e3a[1, 2]<br \/>\nlst.pop(0)          # \u8fd4\u56de1, lst\u53d8\u4e3a[2]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlst.pop(100)        # IndexError: pop index out of range<br \/>\n[].pop()            # IndexError: pop from empty list<\/p>\n<h4>5.33 list.index()<\/h4>\n<p>\u8fd4\u56de\u6307\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [1, 2, 3, 2]<br \/>\nlst.index(2)        # 1<\/p>\n<p># \u6307\u5b9a\u641c\u7d22\u8303\u56f4<br \/>\nlst.index(2, 2)     # 3<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlst.index(4)        # ValueError: 4 is not in list<br \/>\n[].index(1)         # ValueError: 1 is not in list<\/p>\n<h4>5.34 list.count()<\/h4>\n<p>\u8fd4\u56de\u6307\u5b9a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [1, 2, 3, 2]<br \/>\nlst.count(2)        # 2<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlst.count(4)        # 0<br \/>\n[].count(1)         # 0<br \/>\nlst.count(None)     # 0 (\u9664\u975e\u5217\u8868\u4e2d\u6709None)<\/p>\n<h4>5.35 list.sort()<\/h4>\n<p>\u5bf9\u5217\u8868\u8fdb\u884c\u539f\u5730\u6392\u5e8f\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [3, 1, 2]<br \/>\nlst.sort()          # lst\u53d8\u4e3a[1, 2, 3]<\/p>\n<p># \u6307\u5b9akey\u548creverse<br \/>\nlst.sort(reverse&#061;True)  # \u964d\u5e8f\u6392\u5e8f<br \/>\nlst.sort(key&#061;lambda x: -x)  # \u6309\u8d1f\u503c\u6392\u5e8f<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlst &#061; [1, &#039;a&#039;, 2]   # TypeError: &#039;&lt;&#039; not supported between instances of &#039;str&#039; and &#039;int&#039;<br \/>\n[].sort()           # \u65e0\u64cd\u4f5c<\/p>\n<h4>5.36 list.reverse()<\/h4>\n<p>\u53cd\u8f6c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u987a\u5e8f\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlst &#061; [1, 2, 3]<br \/>\nlst.reverse()       # lst\u53d8\u4e3a[3, 2, 1]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n[].reverse()        # \u65e0\u64cd\u4f5c<\/p>\n<h3>6. \u5b57\u5178\u64cd\u4f5c\u51fd\u6570<\/h3>\n<h4>6.37 dict.keys()<\/h4>\n<p>\u8fd4\u56de\u5b57\u5178\u7684\u952e\u89c6\u56fe\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nd &#061; {&#039;a&#039;:1, &#039;b&#039;:2}<br \/>\nd.keys()            # dict_keys([&#039;a&#039;, &#039;b&#039;])<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n{}.keys()           # dict_keys([])<\/p>\n<h4>6.38 dict.values()<\/h4>\n<p>\u8fd4\u56de\u5b57\u5178\u7684\u503c\u89c6\u56fe\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nd &#061; {&#039;a&#039;:1, &#039;b&#039;:2}<br \/>\nd.values()          # dict_values([1, 2])<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n{}.values()         # dict_values([])<\/p>\n<h4>6.39 dict.items()<\/h4>\n<p>\u8fd4\u56de\u5b57\u5178\u7684\u952e\u503c\u5bf9\u89c6\u56fe\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nd &#061; {&#039;a&#039;:1, &#039;b&#039;:2}<br \/>\nd.items()           # dict_items([(&#039;a&#039;, 1), (&#039;b&#039;, 2)])<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n{}.items()          # dict_items([])<\/p>\n<h4>6.40 dict.get()<\/h4>\n<p>\u83b7\u53d6\u6307\u5b9a\u952e\u7684\u503c&#xff0c;\u4e0d\u5b58\u5728\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nd &#061; {&#039;a&#039;:1, &#039;b&#039;:2}<br \/>\nd.get(&#039;a&#039;)          # 1<br \/>\nd.get(&#039;c&#039;, 0)       # 0<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nd.get(&#039;c&#039;)          # None (\u4e0d\u6307\u5b9a\u9ed8\u8ba4\u503c\u65f6)<\/p>\n<h4>6.41 dict.pop()<\/h4>\n<p>\u79fb\u9664\u5e76\u8fd4\u56de\u6307\u5b9a\u952e\u7684\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nd &#061; {&#039;a&#039;:1, &#039;b&#039;:2}<br \/>\nd.pop(&#039;a&#039;)          # 1, d\u53d8\u4e3a{&#039;b&#039;:2}<\/p>\n<p># \u6307\u5b9a\u9ed8\u8ba4\u503c<br \/>\nd.pop(&#039;c&#039;, 0)       # 0<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nd.pop(&#039;c&#039;)          # KeyError: &#039;c&#039;<br \/>\n{}.pop(&#039;a&#039;)         # KeyError: &#039;a&#039;<\/p>\n<h4>6.42 dict.popitem()<\/h4>\n<p>\u968f\u673a\u79fb\u9664\u5e76\u8fd4\u56de\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nd &#061; {&#039;a&#039;:1, &#039;b&#039;:2}<br \/>\nd.popitem()         # \u53ef\u80fd\u662f (&#039;a&#039;, 1) \u6216 (&#039;b&#039;, 2)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n{}.popitem()        # KeyError: &#039;popitem(): dictionary is empty&#039;<\/p>\n<h4>6.43 dict.update()<\/h4>\n<p>\u7528\u53e6\u4e00\u4e2a\u5b57\u5178\u66f4\u65b0\u5f53\u524d\u5b57\u5178\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nd &#061; {&#039;a&#039;:1}<br \/>\nd.update({&#039;b&#039;:2})   # d\u53d8\u4e3a{&#039;a&#039;:1, &#039;b&#039;:2}<\/p>\n<p># \u591a\u79cd\u66f4\u65b0\u65b9\u5f0f<br \/>\nd.update(b&#061;3, c&#061;4)  # d\u53d8\u4e3a{&#039;a&#039;:1, &#039;b&#039;:3, &#039;c&#039;:4}<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nd.update(None)      # TypeError: &#039;NoneType&#039; object is not iterable<br \/>\nd.update(1)         # TypeError: cannot convert dictionary update sequence element #0 to a sequence<\/p>\n<h3>7. \u6587\u4ef6\u64cd\u4f5c\u51fd\u6570<\/h3>\n<h4>7.44 open()<\/h4>\n<p>\u6253\u5f00\u6587\u4ef6\u5e76\u8fd4\u56de\u6587\u4ef6\u5bf9\u8c61\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# f &#061; open(&#039;file.txt&#039;, &#039;r&#039;)  # \u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6<\/p>\n<p># \u5e38\u7528\u6a21\u5f0f<br \/>\n# &#039;r&#039; &#8211; \u8bfb\u53d6 (\u9ed8\u8ba4)<br \/>\n# &#039;w&#039; &#8211; \u5199\u5165 (\u4f1a\u622a\u65ad\u6587\u4ef6)<br \/>\n# &#039;a&#039; &#8211; \u8ffd\u52a0<br \/>\n# &#039;b&#039; &#8211; \u4e8c\u8fdb\u5236\u6a21\u5f0f<br \/>\n# &#039;&#043;&#039; &#8211; \u8bfb\u5199\u6a21\u5f0f<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# open(&#039;nonexistent.txt&#039;, &#039;r&#039;)  # FileNotFoundError<br \/>\n# open(&#039;\/invalid\/path&#039;, &#039;w&#039;)     # PermissionError \u6216\u5176\u4ed6OSError<\/p>\n<h4>7.45 file.read()<\/h4>\n<p>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# with open(&#039;file.txt&#039;) as f:<br \/>\n#     content &#061; f.read()  # \u8bfb\u53d6\u5168\u90e8\u5185\u5bb9<\/p>\n<p># \u6307\u5b9a\u8bfb\u53d6\u5927\u5c0f<br \/>\n# f.read(100)  # \u8bfb\u53d6100\u4e2a\u5b57\u7b26<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# f.read() \u5728\u6587\u4ef6\u5173\u95ed\u540e\u8c03\u7528\u4f1a\u5f15\u53d1 ValueError<\/p>\n<h4>7.46 file.readline()<\/h4>\n<p>\u8bfb\u53d6\u6587\u4ef6\u7684\u4e00\u884c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# with open(&#039;file.txt&#039;) as f:<br \/>\n#     line &#061; f.readline()  # \u8bfb\u53d6\u4e00\u884c<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u6587\u4ef6\u672b\u5c3e\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32 &#039;&#039;<\/p>\n<h4>7.47 file.readlines()<\/h4>\n<p>\u8bfb\u53d6\u6240\u6709\u884c\u5e76\u8fd4\u56de\u5217\u8868\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# with open(&#039;file.txt&#039;) as f:<br \/>\n#     lines &#061; f.readlines()  # \u8fd4\u56de\u884c\u5217\u8868<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u7a7a\u6587\u4ef6\u8fd4\u56de\u7a7a\u5217\u8868 []<\/p>\n<h4>7.48 file.write()<\/h4>\n<p>\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# with open(&#039;file.txt&#039;, &#039;w&#039;) as f:<br \/>\n#     f.write(&#039;hello\\\\n&#039;)  # \u5199\u5165\u5b57\u7b26\u4e32<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u53ea\u80fd\u5199\u5165\u5b57\u7b26\u4e32&#xff0c;\u5199\u5165\u5176\u4ed6\u7c7b\u578b\u4f1a\u5f15\u53d1 TypeError<br \/>\n# f.write(123)  # TypeError<\/p>\n<h4>7.49 file.close()<\/h4>\n<p>\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# f &#061; open(&#039;file.txt&#039;)<br \/>\n# f.close()<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u591a\u6b21\u8c03\u7528close()\u4e0d\u4f1a\u62a5\u9519<br \/>\n# \u4f7f\u7528with\u8bed\u53e5\u66f4\u5b89\u5168&#xff0c;\u4f1a\u81ea\u52a8\u5173\u95ed\u6587\u4ef6<\/p>\n<h3>8. \u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668\u51fd\u6570<\/h3>\n<h4>8.50 range()<\/h4>\n<p>\u751f\u6210\u6574\u6570\u5e8f\u5217\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlist(range(5))        # [0, 1, 2, 3, 4]<br \/>\nlist(range(1, 5))     # [1, 2, 3, 4]<br \/>\nlist(range(1, 10, 2)) # [1, 3, 5, 7, 9]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlist(range(0))        # []<br \/>\nlist(range(1, 1))     # []<br \/>\nlist(range(1, 5, -1)) # []<\/p>\n<h4>8.51 enumerate()<\/h4>\n<p>\u8fd4\u56de\u679a\u4e3e\u5bf9\u8c61(\u7d22\u5f15\u548c\u5143\u7d20)\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlist(enumerate([&#039;a&#039;, &#039;b&#039;, &#039;c&#039;]))  # [(0, &#039;a&#039;), (1, &#039;b&#039;), (2, &#039;c&#039;)]<\/p>\n<p># \u6307\u5b9a\u8d77\u59cb\u7d22\u5f15<br \/>\nlist(enumerate([&#039;a&#039;, &#039;b&#039;], 1))    # [(1, &#039;a&#039;), (2, &#039;b&#039;)]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlist(enumerate([]))               # []<br \/>\nlist(enumerate(None))             # TypeError: &#039;NoneType&#039; object is not iterable<\/p>\n<h4>8.52 zip()<\/h4>\n<p>\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6253\u5305\u6210\u5143\u7ec4\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlist(zip([1, 2], [&#039;a&#039;, &#039;b&#039;]))  # [(1, &#039;a&#039;), (2, &#039;b&#039;)]<\/p>\n<p># \u4e0d\u540c\u957f\u5ea6\u5904\u7406<br \/>\nlist(zip([1, 2, 3], [&#039;a&#039;, &#039;b&#039;]))  # [(1, &#039;a&#039;), (2, &#039;b&#039;)]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlist(zip())                      # []<br \/>\nlist(zip([], []))                # []<br \/>\nlist(zip([1], None))             # TypeError: zip argument #2 must support iteration<\/p>\n<h3>9. \u51fd\u6570\u5f0f\u7f16\u7a0b\u51fd\u6570<\/h3>\n<h4>9.53 map()<\/h4>\n<p>\u5c06\u51fd\u6570\u5e94\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlist(map(str, [1, 2, 3]))  # [&#039;1&#039;, &#039;2&#039;, &#039;3&#039;]<br \/>\nlist(map(lambda x: x*2, [1, 2, 3]))  # [2, 4, 6]<\/p>\n<p># \u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61<br \/>\nlist(map(lambda x,y: x&#043;y, [1,2], [3,4]))  # [4, 6]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlist(map(str, []))         # []<br \/>\nlist(map(None, [1,2]))     # TypeError: &#039;NoneType&#039; object is not callable<\/p>\n<h4>9.54 filter()<\/h4>\n<p>\u6839\u636e\u51fd\u6570\u6761\u4ef6\u8fc7\u6ee4\u5143\u7d20\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlist(filter(lambda x: x&gt;0, [-1, 0, 1, 2]))  # [1, 2]<\/p>\n<p># \u4f7f\u7528None\u8fc7\u6ee4\u5047\u503c<br \/>\nlist(filter(None, [0, 1, False, True]))     # [1, True]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlist(filter(lambda x: x&gt;0, []))             # []<\/p>\n<h4>9.55 reduce()<\/h4>\n<p>\u5bf9\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97(\u9700\u4ecefunctools\u5bfc\u5165)\u3002<\/p>\n<p>from functools import reduce<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nreduce(lambda x,y: x&#043;y, [1,2,3,4])  # 10 (((1&#043;2)&#043;3)&#043;4)<\/p>\n<p># \u6307\u5b9a\u521d\u59cb\u503c<br \/>\nreduce(lambda x,y: x&#043;y, [1,2,3], 10)  # 16<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nreduce(lambda x,y: x&#043;y, [])       # TypeError: reduce() of empty sequence with no initial value<br \/>\nreduce(lambda x,y: x&#043;y, [1])      # 1 (\u5355\u5143\u7d20\u76f4\u63a5\u8fd4\u56de)<\/p>\n<h3>10. \u6a21\u5757\u4e0e\u5305\u51fd\u6570<\/h3>\n<h4>10.56 import<\/h4>\n<p>\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# import math<br \/>\n# math.sqrt(4)<\/p>\n<p># \u522b\u540d<br \/>\n# import numpy as np<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# import nonexistent_module  # ModuleNotFoundError<\/p>\n<h4>10.57 from\u2026import<\/h4>\n<p>\u4ece\u6a21\u5757\u5bfc\u5165\u7279\u5b9a\u5bf9\u8c61\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# from math import sqrt<br \/>\n# sqrt(4)<\/p>\n<p># \u5bfc\u5165\u591a\u4e2a<br \/>\n# from math import sqrt, pi<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# from math import nonexistent  # ImportError: cannot import name &#039;nonexistent&#039;<\/p>\n<h3>11. \u65e5\u671f\u65f6\u95f4\u51fd\u6570<\/h3>\n<h4>11.58 datetime.date.today()<\/h4>\n<p>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u3002<\/p>\n<p>from datetime import date<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# today &#061; date.today()  # \u8fd4\u56dedate\u5bf9\u8c61<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u65e0\u53c2\u6570&#xff0c;\u603b\u662f\u8fd4\u56de\u5f53\u524d\u65e5\u671f<\/p>\n<h4>11.59 datetime.datetime.now()<\/h4>\n<p>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<p>from datetime import datetime<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# now &#061; datetime.now()  # \u8fd4\u56dedatetime\u5bf9\u8c61<\/p>\n<p># \u6307\u5b9a\u65f6\u533a<br \/>\n# from datetime import timezone<br \/>\n# datetime.now(timezone.utc)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u65e0\u53c2\u6570&#xff0c;\u603b\u662f\u8fd4\u56de\u5f53\u524d\u65f6\u95f4<\/p>\n<h4>11.60 datetime.datetime.strptime()<\/h4>\n<p>\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002<\/p>\n<p>from datetime import datetime<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# dt &#061; datetime.strptime(&#039;2023-01-01&#039;, &#039;%Y-%m-%d&#039;)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# datetime.strptime(&#039;invalid&#039;, &#039;%Y&#039;)  # ValueError: time data &#039;invalid&#039; does not match format &#039;%Y&#039;<br \/>\n# datetime.strptime(None, &#039;%Y&#039;)       # TypeError: strptime() argument 1 must be str, not None<\/p>\n<h4>11.61 datetime.datetime.strftime()<\/h4>\n<p>\u5c06\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>from datetime import datetime<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# now &#061; datetime.now()<br \/>\n# now.strftime(&#039;%Y-%m-%d %H:%M:%S&#039;)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# datetime.strftime(None, &#039;%Y&#039;)  # AttributeError: &#039;NoneType&#039; object has no attribute &#039;strftime&#039;<\/p>\n<h3>12. \u9519\u8bef\u5904\u7406\u51fd\u6570<\/h3>\n<h4>12.62 try-except<\/h4>\n<p>\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\ntry:<br \/>\n    1 \/ 0<br \/>\nexcept ZeroDivisionError:<br \/>\n    print(&#034;\u4e0d\u80fd\u9664\u4ee5\u96f6&#034;)<\/p>\n<p># \u591a\u4e2a\u5f02\u5e38<br \/>\ntry:<br \/>\n    # \u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801<br \/>\nexcept (TypeError, ValueError):<br \/>\n    # \u5904\u7406\u591a\u79cd\u5f02\u5e38<br \/>\nexcept Exception as e:<br \/>\n    # \u6355\u83b7\u6240\u6709\u5f02\u5e38<br \/>\n    print(f&#034;\u53d1\u751f\u9519\u8bef: {e}&#034;)<br \/>\nfinally:<br \/>\n    # \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u4f1a\u6267\u884c<br \/>\n    print(&#034;\u6e05\u7406\u4ee3\u7801&#034;)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u7a7a\u7684try-except\u5757\u662f\u5408\u6cd5\u4f46\u4e0d\u597d\u7684\u505a\u6cd5<\/p>\n<h4>12.63 raise<\/h4>\n<p>\u624b\u52a8\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nif x &lt; 0:<br \/>\n    raise ValueError(&#034;x\u4e0d\u80fd\u4e3a\u8d1f\u6570&#034;)<\/p>\n<p># \u91cd\u65b0\u629b\u51fa\u5f53\u524d\u5f02\u5e38<br \/>\ntry:<br \/>\n    1 \/ 0<br \/>\nexcept:<br \/>\n    print(&#034;\u53d1\u751f\u9519\u8bef&#034;)<br \/>\n    raise  # \u91cd\u65b0\u629b\u51fa<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nraise  # \u4e0d\u5728except\u5757\u4e2d\u4f7f\u7528\u4f1a\u5f15\u53d1RuntimeError<br \/>\nraise None  # TypeError: exceptions must derive from BaseException<\/p>\n<h3>13. \u5176\u4ed6\u5e38\u7528\u51fd\u6570<\/h3>\n<h4>13.64 id()<\/h4>\n<p>\u8fd4\u56de\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nx &#061; 1<br \/>\nid(x)  # \u8fd4\u56de\u5185\u5b58\u5730\u5740<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nid(None)  # \u8fd4\u56deNone\u7684id<\/p>\n<h4>13.65 type()<\/h4>\n<p>\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\ntype(1)        # &lt;class &#039;int&#039;&gt;<br \/>\ntype(&#039;a&#039;)      # &lt;class &#039;str&#039;&gt;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\ntype(None)     # &lt;class &#039;NoneType&#039;&gt;<br \/>\ntype(type)     # &lt;class &#039;type&#039;&gt;<\/p>\n<h4>13.66 isinstance()<\/h4>\n<p>\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nisinstance(1, int)      # True<br \/>\nisinstance(&#039;a&#039;, str)    # True<\/p>\n<p># \u68c0\u67e5\u591a\u4e2a\u7c7b\u578b<br \/>\nisinstance(1, (int, float))  # True<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nisinstance(1, type)     # False<br \/>\nisinstance(int, type)   # True<\/p>\n<h4>13.67 hasattr()<\/h4>\n<p>\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u6709\u6307\u5b9a\u5c5e\u6027\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nhasattr(&#039;abc&#039;, &#039;upper&#039;)  # True<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nhasattr(None, &#039;x&#039;)       # False<br \/>\nhasattr(1, &#039;imag&#039;)       # True (int\u6709imag\u5c5e\u6027)<\/p>\n<h4>13.68 setattr()<\/h4>\n<p>\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nclass MyClass: pass<br \/>\nobj &#061; MyClass()<br \/>\nsetattr(obj, &#039;x&#039;, 1)  # obj.x &#061; 1<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nsetattr(1, &#039;x&#039;, 1)    # TypeError: &#039;int&#039; object has no attribute &#039;x&#039;<br \/>\nsetattr(obj, 123, 1)  # \u5c5e\u6027\u540d\u5e94\u4e3a\u5b57\u7b26\u4e32<\/p>\n<h4>13.69 getattr()<\/h4>\n<p>\u83b7\u53d6\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\ngetattr(&#039;abc&#039;, &#039;upper&#039;)()  # &#039;ABC&#039;<\/p>\n<p># \u6307\u5b9a\u9ed8\u8ba4\u503c<br \/>\ngetattr(&#039;abc&#039;, &#039;x&#039;, None)  # None<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\ngetattr(&#039;abc&#039;, 123)        # TypeError: attribute name must be string<\/p>\n<h4>13.70 delattr()<\/h4>\n<p>\u5220\u9664\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nclass MyClass: x &#061; 1<br \/>\nobj &#061; MyClass()<br \/>\ndelattr(obj, &#039;x&#039;)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\ndelattr(obj, &#039;x&#039;)  # AttributeError: x<br \/>\ndelattr(1, &#039;real&#039;) # TypeError: &#039;int&#039; object has no attribute &#039;real&#039;<\/p>\n<h4>13.71 globals()<\/h4>\n<p>\u8fd4\u56de\u5168\u5c40\u53d8\u91cf\u7684\u5b57\u5178\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nglobals()  # \u8fd4\u56de\u5f53\u524d\u5168\u5c40\u7b26\u53f7\u8868<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u603b\u662f\u8fd4\u56de\u5b57\u5178&#xff0c;\u5373\u4f7f\u5728\u51fd\u6570\u5185\u90e8<\/p>\n<h4>13.72 locals()<\/h4>\n<p>\u8fd4\u56de\u5c40\u90e8\u53d8\u91cf\u7684\u5b57\u5178\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\ndef func():<br \/>\n    x &#061; 1<br \/>\n    return locals()<\/p>\n<p>func()  # {&#039;x&#039;: 1}<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u5728\u6a21\u5757\u7ea7\u522b&#xff0c;locals()\u548cglobals()\u76f8\u540c<\/p>\n<h4>13.73 help()<\/h4>\n<p>\u542f\u52a8\u5e2e\u52a9\u7cfb\u7edf\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\n# help(list)  # \u663e\u793alist\u7684\u5e2e\u52a9\u4fe1\u606f<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# help(None)  # \u663e\u793aNone\u7684\u5e2e\u52a9\u4fe1\u606f<\/p>\n<h4>13.74 dir()<\/h4>\n<p>\u8fd4\u56de\u5bf9\u8c61\u7684\u5c5e\u6027\u5217\u8868\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\ndir(list)  # \u8fd4\u56delist\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5217\u8868<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\ndir()      # \u8fd4\u56de\u5f53\u524d\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u540d<br \/>\ndir(None)  # \u8fd4\u56deNone\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5217\u8868<\/p>\n<h4>13.75 compile()<\/h4>\n<p>\u5c06\u5b57\u7b26\u4e32\u7f16\u8bd1\u4e3a\u4ee3\u7801\u5bf9\u8c61\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\ncode &#061; compile(&#039;print(&#034;hello&#034;)&#039;, &#039;test&#039;, &#039;exec&#039;)<br \/>\nexec(code)  # \u8f93\u51fahello<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\ncompile(&#039;invalid code&#039;, &#039;test&#039;, &#039;exec&#039;)  # SyntaxError<br \/>\ncompile(123, &#039;test&#039;, &#039;exec&#039;)            # TypeError: expected string without null bytes<\/p>\n<h4>13.76 eval()<\/h4>\n<p>\u8ba1\u7b97\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\neval(&#039;1 &#043; 1&#039;)  # 2<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\neval(&#039;import os&#039;)  # SyntaxError<br \/>\neval(None)         # TypeError: eval() arg 1 must be a string, bytes or code object<\/p>\n<h4>13.77 exec()<\/h4>\n<p>\u6267\u884c\u5b57\u7b26\u4e32\u6216\u4ee3\u7801\u5bf9\u8c61\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nexec(&#039;x &#061; 1\\\\nprint(x)&#039;)  # \u8f93\u51fa1<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nexec(None)               # TypeError: exec() arg 1 must be a string, bytes or code object<\/p>\n<h4>13.78 hash()<\/h4>\n<p>\u8fd4\u56de\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nhash(&#039;abc&#039;)  # \u8fd4\u56de\u54c8\u5e0c\u503c<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nhash([])     # TypeError: unhashable type: &#039;list&#039;<br \/>\nhash(None)   # \u8fd4\u56deNone\u7684\u54c8\u5e0c\u503c<\/p>\n<h4>13.79 iter()<\/h4>\n<p>\u8fd4\u56de\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nit &#061; iter([1, 2, 3])<br \/>\nnext(it)  # 1<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\niter(1)   # TypeError: &#039;int&#039; object is not iterable<br \/>\niter(None) # TypeError: &#039;NoneType&#039; object is not iterable<\/p>\n<h4>13.80 next()<\/h4>\n<p>\u83b7\u53d6\u8fed\u4ee3\u5668\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nit &#061; iter([1, 2])<br \/>\nnext(it)  # 1<\/p>\n<p># \u6307\u5b9a\u9ed8\u8ba4\u503c<br \/>\nnext(it, &#039;end&#039;)  # 2<br \/>\nnext(it, &#039;end&#039;)  # &#039;end&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nnext(it)         # StopIteration<\/p>\n<h4>13.81 all()<\/h4>\n<p>\u68c0\u67e5\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u4e3a\u771f\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nall([1, 2, 3])   # True<br \/>\nall([1, 0, 3])   # False<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nall([])          # True (\u7a7a\u53ef\u8fed\u4ee3\u5bf9\u8c61)<br \/>\nall([None])      # False<\/p>\n<h4>13.82 any()<\/h4>\n<p>\u68c0\u67e5\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u662f\u5426\u6709\u4efb\u4f55\u5143\u7d20\u4e3a\u771f\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nany([0, 1, 0])   # True<br \/>\nany([0, 0, 0])   # False<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nany([])          # False (\u7a7a\u53ef\u8fed\u4ee3\u5bf9\u8c61)<br \/>\nany([None])      # False<\/p>\n<h4>13.83 bytes()<\/h4>\n<p>\u521b\u5efa\u5b57\u8282\u5bf9\u8c61\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nbytes([1, 2, 3])  # b&#039;\\\\x01\\\\x02\\\\x03&#039;<br \/>\nbytes(&#039;abc&#039;, &#039;utf-8&#039;)  # b&#039;abc&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nbytes(-1)         # ValueError: negative count<br \/>\nbytes(&#039;abc&#039;, &#039;invalid&#039;)  # LookupError: unknown encoding: invalid<\/p>\n<h4>13.84 bytearray()<\/h4>\n<p>\u521b\u5efa\u53ef\u53d8\u7684\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nbytearray([1, 2, 3])  # bytearray(b&#039;\\\\x01\\\\x02\\\\x03&#039;)<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nbytearray(-1)         # ValueError: negative count<\/p>\n<h4>13.85 memoryview()<\/h4>\n<p>\u8fd4\u56de\u5bf9\u8c61\u7684\u5185\u5b58\u89c6\u56fe\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nmv &#061; memoryview(b&#039;abc&#039;)<br \/>\nmv[0]  # 97<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nmemoryview(&#039;abc&#039;)     # TypeError: memoryview: a bytes-like object is required<\/p>\n<h4>13.86 ord()<\/h4>\n<p>\u8fd4\u56de\u5b57\u7b26\u7684Unicode\u7801\u70b9\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nord(&#039;a&#039;)  # 97<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nord(&#039;&#039;)   # TypeError: ord() expected a character, but string of length 0 found<br \/>\nord(&#039;ab&#039;) # TypeError: ord() expected a character, but string of length 2 found<\/p>\n<h4>13.87 chr()<\/h4>\n<p>\u6839\u636eUnicode\u7801\u70b9\u8fd4\u56de\u5b57\u7b26\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nchr(97)  # &#039;a&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nchr(-1)  # ValueError: chr() arg not in range(0x110000)<br \/>\nchr(0x110000)  # ValueError<\/p>\n<h4>13.88 bin()<\/h4>\n<p>\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nbin(10)  # &#039;0b1010&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nbin(3.14)  # TypeError: &#039;float&#039; object cannot be interpreted as an integer<br \/>\nbin(-10)   # &#039;-0b1010&#039;<\/p>\n<h4>13.89 oct()<\/h4>\n<p>\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\noct(8)  # &#039;0o10&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\noct(3.14)  # TypeError<br \/>\noct(-8)    # &#039;-0o10&#039;<\/p>\n<h4>13.90 hex()<\/h4>\n<p>\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nhex(16)  # &#039;0x10&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nhex(3.14)  # TypeError<br \/>\nhex(-16)   # &#039;-0x10&#039;<\/p>\n<h4>13.91 frozenset()<\/h4>\n<p>\u521b\u5efa\u4e0d\u53ef\u53d8\u96c6\u5408\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nfs &#061; frozenset([1, 2, 3])<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nfrozenset(None)  # TypeError: &#039;NoneType&#039; object is not iterable<\/p>\n<h4>13.92 super()<\/h4>\n<p>\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nclass Parent:<br \/>\n    def method(self):<br \/>\n        print(&#034;Parent method&#034;)<\/p>\n<p>class Child(Parent):<br \/>\n    def method(self):<br \/>\n        super().method()<br \/>\n        print(&#034;Child method&#034;)<\/p>\n<p>Child().method()<br \/>\n# \u8f93\u51fa:<br \/>\n# Parent method<br \/>\n# Child method<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u4e0d\u6b63\u786e\u7684\u4f7f\u7528\u4f1a\u5bfc\u81f4RuntimeError<\/p>\n<h4>13.93 property()<\/h4>\n<p>\u521b\u5efa\u5c5e\u6027\u63cf\u8ff0\u7b26\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nclass MyClass:<br \/>\n    def __init__(self):<br \/>\n        self._x &#061; None<\/p>\n<p>    &#064;property<br \/>\n    def x(self):<br \/>\n        return self._x<\/p>\n<p>    &#064;x.setter<br \/>\n    def x(self, value):<br \/>\n        self._x &#061; value<\/p>\n<p>obj &#061; MyClass()<br \/>\nobj.x &#061; 1  # \u8c03\u7528setter<br \/>\nprint(obj.x)  # \u8c03\u7528getter<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u4e0d\u6b63\u786e\u7684\u5c5e\u6027\u8bbf\u95ee\u4f1a\u5f15\u53d1AttributeError<\/p>\n<h4>13.94 classmethod()<\/h4>\n<p>\u5b9a\u4e49\u7c7b\u65b9\u6cd5\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nclass MyClass:<br \/>\n    &#064;classmethod<br \/>\n    def class_method(cls):<br \/>\n        print(f&#034;Called from {cls}&#034;)<\/p>\n<p>MyClass.class_method()  # \u901a\u8fc7\u7c7b\u8c03\u7528<br \/>\nobj &#061; MyClass()<br \/>\nobj.class_method()      # \u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u4e0d\u6b63\u786e\u7684\u4f7f\u7528\u4f1a\u5bfc\u81f4TypeError<\/p>\n<h4>13.95 staticmethod()<\/h4>\n<p>\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nclass MyClass:<br \/>\n    &#064;staticmethod<br \/>\n    def static_method():<br \/>\n        print(&#034;Static method&#034;)<\/p>\n<p>MyClass.static_method()  # \u901a\u8fc7\u7c7b\u8c03\u7528<br \/>\nobj &#061; MyClass()<br \/>\nobj.static_method()      # \u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\n# \u4e0d\u6b63\u786e\u7684\u4f7f\u7528\u4f1a\u5bfc\u81f4TypeError<\/p>\n<h4>13.96 len()<\/h4>\n<p>\u8fd4\u56de\u5bf9\u8c61\u7684\u957f\u5ea6\u6216\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlen(&#039;abc&#039;)  # 3<br \/>\nlen([1,2,3]) # 3<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlen(123)    # TypeError<br \/>\nlen(None)   # TypeError<\/p>\n<h4>13.97 sorted()<\/h4>\n<p>\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u5e76\u8fd4\u56de\u65b0\u5217\u8868\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nsorted([3, 1, 2])  # [1, 2, 3]<\/p>\n<p># \u6307\u5b9akey\u548creverse<br \/>\nsorted([&#039;apple&#039;, &#039;banana&#039;], key&#061;len, reverse&#061;True)  # [&#039;banana&#039;, &#039;apple&#039;]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nsorted(None)  # TypeError<br \/>\nsorted([1, &#039;a&#039;])  # TypeError<\/p>\n<h4>13.98 reversed()<\/h4>\n<p>\u8fd4\u56de\u53cd\u8f6c\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nlist(reversed([1, 2, 3]))  # [3, 2, 1]<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nlist(reversed(None))  # TypeError<br \/>\nlist(reversed(123))   # TypeError<\/p>\n<h4>13.99 format()<\/h4>\n<p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nformat(3.14159, &#039;.2f&#039;)  # &#039;3.14&#039;<br \/>\n&#034;{:.2f}&#034;.format(3.14159)  # &#039;3.14&#039;<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nformat(&#039;abc&#039;, 123)  # TypeError<\/p>\n<h4>13.100 vars()<\/h4>\n<p>\u8fd4\u56de\u5bf9\u8c61\u7684\u5c5e\u6027\u5b57\u5178\u3002<\/p>\n<p># \u57fa\u672c\u7528\u6cd5<br \/>\nclass MyClass: pass<br \/>\nobj &#061; MyClass()<br \/>\nobj.x &#061; 1<br \/>\nvars(obj)  # {&#039;x&#039;: 1}<\/p>\n<p># \u4e34\u754c\u503c\u5904\u7406<br \/>\nvars(1)    # TypeError<br \/>\nvars(None) # TypeError<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.7k\u6b21\uff0c\u70b9\u8d5e48\u6b21\uff0c\u6536\u85cf123\u6b21\u3002Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,190],"topic":[],"class_list":["post-44114","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/44114.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.7k\u6b21\uff0c\u70b9\u8d5e48\u6b21\uff0c\u6536\u85cf123\u6b21\u3002Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44114.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-16T17:43:28+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44114.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44114.html\",\"name\":\"Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-16T17:43:28+00:00\",\"dateModified\":\"2025-06-16T17:43:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44114.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44114.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44114.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/44114.html","og_locale":"zh_CN","og_type":"article","og_title":"Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.7k\u6b21\uff0c\u70b9\u8d5e48\u6b21\uff0c\u6536\u85cf123\u6b21\u3002Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790","og_url":"https:\/\/www.wsisp.com\/helps\/44114.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-16T17:43:28+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44114.html","url":"https:\/\/www.wsisp.com\/helps\/44114.html","name":"Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-16T17:43:28+00:00","dateModified":"2025-06-16T17:43:28+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44114.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44114.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44114.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python 100\u4e2a\u5e38\u7528\u51fd\u6570\u5168\u9762\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=44114"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44114\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44114"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}