{"id":10020878,"date":"2025-07-04T10:31:43","date_gmt":"2025-07-04T02:31:43","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020878"},"modified":"2025-07-04T10:31:43","modified_gmt":"2025-07-04T02:31:43","slug":"ai-%e5%a4%a7%e6%a8%a1%e5%9e%8b%e5%ba%94%e7%94%a8%e8%bf%9b%e9%98%b6%e7%b3%bb%e5%88%97%ef%bc%88%e4%b8%80%ef%bc%89%ef%bc%9apython-%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020878","title":{"rendered":"AI \u5927\u6a21\u578b\u5e94\u7528\u8fdb\u9636\u7cfb\u5217\uff08\u4e00\uff09\uff1aPython \u57fa\u7840"},"content":{"rendered":"<p>\u672c\u6587\u5df2\u540c\u6b65\u5230github <a href=\"https:\/\/link.segmentfault.com\/?enc=XZ%2FPjSJEvJhThtbPep3VVQ%3D%3D.Ua2wO0fkI9e9S6Ycr%2FhgNr547%2BSvniaGWWElE39ON5UMB%2BWM%2F17NLqs85X0QGA7E\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">AI \u5927\u6a21\u578b\u5e94\u7528\u8fdb\u9636\u7cfb\u5217\uff08\u4e00\uff09\uff1aPython \u57fa\u7840<\/a>\uff0c\u6b22\u8fceStar\u3002<\/p>\n<h2>\u6570\u636e\u7c7b\u578b<\/h2>\n<h3>\u6570\u5b57<\/h3>\n<blockquote><p>\u5728 Python \u4e2d\uff0c\u6570\u5b57\u7c7b\u578b\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u4e00\uff0c\u7528\u4e8e\u8868\u793a\u6570\u503c<\/p><\/blockquote>\n<ul>\n<li>\n<p>\u6574\u6570<\/p>\n<pre><code>  a = 10        # \u5341\u8fdb\u5236\u6574\u6570\n  b = 0b1010    # \u4e8c\u8fdb\u5236\u6574\u6570\uff08\u7b49\u4e8e\u5341\u8fdb\u5236\u768410\uff09\n  c = 0o12      # \u516b\u8fdb\u5236\u6574\u6570\uff08\u7b49\u4e8e\u5341\u8fdb\u5236\u768410\uff09\n  d = 0xA       # \u5341\u516d\u8fdb\u5236\u6574\u6570\uff08\u7b49\u4e8e\u5341\u8fdb\u5236\u768410\uff09\n\n  print(a, b, c, d)  # \u8f93\u51fa: 10 10 10 10<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6d6e\u70b9\u6570<\/p>\n<pre><code>    x = 3.14\n    y = 1.23e-4  # \u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u8868\u793a1.23\u00d710\u207b\u2074\uff0c\u53730.000123\n\n    print(x, y)  # \u8f93\u51fa: 3.14 0.000123<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>\u5b57\u7b26\u4e32<\/h3>\n<blockquote><p>\u5728 Python \u4e2d\uff0c\u5b57\u7b26\u4e32\u5c5e\u4e8e\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u8868\u793a\u6587\u672c\u6570\u636e<\/p><\/blockquote>\n<ul>\n<li>\n<p>\u57fa\u672c\u5b57\u7b26\u4e32<\/p>\n<pre><code>name=&quot;\u5c0f\u660e&quot;\nage=&quot;18&quot;\nprint(f{{name}\u4eca\u5e74{age}\u5c81) \/\/ \u8f93\u51fa: \u5c0f\u660e\u4eca\u5e7418\u5c81<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8f6c\u4e49\u5b57\u7b26\u4e32<\/p>\n<pre><code>text=&quot;\u8f6c\u4e49\/&quot;python\/&quot;&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32<\/p>\n<ul>\n<li>\n<p>\u5305\u542b\u591a\u884c\u6587\u672c\uff0c\u4fdd\u7559\u6240\u6709\u6362\u884c\u548c\u7a7a\u683c<\/p>\n<pre><code>&quot;&quot;&quot;\n\u4f60\u597d\uff0c\nPython\n&quot;&quot;&quot;<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5b57\u7b26\u4e32\u62fc\u63a5<\/p>\n<ul>\n<li>\n<p>\u5f53\u548c\u6570\u5b57\u6570\u5b57\u62fc\u63a5\u65f6\uff0c\u9700\u8981\u5148\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32<\/p>\n<pre><code>name = &quot;\u5c0f\u660e&quot;\nage = 18\n\nmessage = &quot;name&quot; + str(age) + &quot;\u5c81&quot;\nprint(message)\nprint(f&quot;{name} \u4eca\u5e74 {str(age)} \u5c81&quot;)<\/code><\/pre>\n<ul>\n<li>\n<p>\u7d22\u5f15<\/p>\n<pre><code>name = &quot;python&quot;\nprint(name[0]) \u8f93\u51fa\u7b2c\u4e00\u4e2a\u5b57\u7b26 p\nprint(name[-1]) \u8f93\u51fa\u6700\u540e\u4e00\u4e2a\u5b57\u7b26 n<\/code><\/pre>\n<\/li>\n<li>\u5207\u7247<\/li>\n<\/ul>\n<pre><code>str = &quot;Hello, world!&quot; \nprint(str[0:6]) # \u8f93\u51fa\uff1aHello\uff08\u4ece\u7b2c0\u5230\u7b2c4\u4e2a\u5b57\u7b26\uff0c\u4e0d\u5305\u62ec\u7b2c6\u4e2a\uff09\nprint(str[6:]) # \u8f93\u51fa\uff1aworld!\uff08\u4ece\u7b2c6\u4e2a\u5b57\u7b26\u5230\u7ed3\u5c3e\uff09 \nprint(str[:6]) # \u8f93\u51fa\uff1aHello\uff08\u4ece\u5f00\u5934\u5230\u7b2c5\u4e2a\u5b57\u7b26\uff09<\/code><\/pre>\n<ul>\n<li>\u65b9\u6cd5<\/li>\n<li>\n<p>lower \u5168\u90e8\u8f6c\u6362\u4e3a\u5c0f\u5199<\/p>\n<pre><code>  str = &quot;Hello, World&quot;\n  print(str.lower()) \u8f93\u51fa &quot;hello, world&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p>upper \u5168\u90e8\u8f6c\u6362\u4e3a\u5927\u5199<\/p>\n<pre><code>  str = &quot;Hello, World&quot;\n  print(str.lower()) \u8f93\u51fa &quot;HELLO, WORLD&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p>find \u67e5\u627e\u5b57\u7b26\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e<\/p>\n<pre><code>  str = &quot;Hello, World&quot;\n  print(str.find(&quot;World&quot;)) \u8f93\u51fa 6<\/code><\/pre>\n<\/li>\n<li>\n<p>replace \u66ff\u6362<\/p>\n<pre><code>   str = &quot;Hello, World&quot;\n  print(str.replace(&quot;World&quot;, &quot;Python&quot;)) \u66ff\u6362\u6240\u6709&quot;World&quot;\u4e3a&quot;Python&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p>format \u7528{}\u5360\u4f4d\uff0c\u540e\u9762\u7528format\u586b\u5145<\/p>\n<pre><code>  name = &quot;\u5c0f\u660e&quot; \n  age = 18\n  print(&quot;{}\u4eca\u5e74{}\u5c81&quot;.format(name, age)) \u8f93\u51fa:\u5c0f\u660e\u4eca\u5e7418\u5c81 <\/code><\/pre>\n<\/li>\n<li>\n<p>f-string<\/p>\n<pre><code>  name = &quot;\u5c0f\u660e&quot; \n  age = 18\n  print(f&quot;{name}\u4eca\u5e74{age}\u5c81&quot;) \u8f93\u51fa:\u5c0f\u660e\u4eca\u5e7418\u5c81 <\/code><\/pre>\n<\/li>\n<li>encode<\/li>\n<li>decode<\/li>\n<li>startWith<\/li>\n<li>endWidth<\/li>\n<li>isdigit<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u5e03\u5c14\u503c<\/h3>\n<ul>\n<li>True<\/li>\n<li>False<\/li>\n<\/ul>\n<h3>\u7a7a\u503c<\/h3>\n<ul>\n<li>\n<p>\u8868\u793a\u4ec0\u4e48\u90fd\u6ca1\u6709<\/p>\n<pre><code>name = None<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>\u5217\u8868<\/h3>\n<blockquote><p>\u5217\u8868\uff08List\uff09\u5c5e\u4e8e\u5185\u7f6e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u80fd\u591f\u5b58\u653e\u5404\u79cd\u7c7b\u578b\u7684\u5143\u7d20<\/p><\/blockquote>\n<h4>\u521b\u5efa<\/h4>\n<pre><code>list = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]\nprint(list[0]) # \u8f93\u51fa\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1aa\nprint(list[1]) # \u8f93\u51fa\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff1ab\nprint(list[2]) # \u8f93\u51fa\u7b2c\u4e09\u4e2a\u5143\u7d20\uff1ac\nprint(list[-1]) # \u8f93\u51fa\u6700\u540e\u4e00\u4e2a\u5143\u7d20\nprint(list[-2]) # \u8f93\u51fa\u5012\u6570\u7b2c\u4e8c\u4e2a\u5143\u7d20\nprint(list[3]) # \u62a5\u9519\uff1aIndexError: list index out of range<\/code><\/pre>\n<h4>\u6dfb\u52a0<\/h4>\n<pre><code>list = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]\nlist.append('d')\nprint(list) # \u8f93\u51fa [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]<\/code><\/pre>\n<h4>\u63d2\u5165<\/h4>\n<pre><code>list = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]\n# \u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\nlist.insert(1, &quot;e&quot;)\nprint(list) # \u8f93\u51fa [&quot;a&quot;, &quot;e&quot;, &quot;b&quot;, &quot;c&quot;]<\/code><\/pre>\n<h4>\u4fee\u6539<\/h4>\n<pre><code>list = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]\nlist[1] = &quot;d&quot;\nprint(list) # \u8f93\u51fa [&quot;a&quot;, &quot;d&quot;, &quot;c&quot;]<\/code><\/pre>\n<h4>\u5220\u9664<\/h4>\n<pre><code>list = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]\nconst item = list.pop()\nprint(item) # \u8f93\u51fa a\nprint(list) # \u8f93\u51fa ['b', 'c']<\/code><\/pre>\n<h3>\u5143\u7956<\/h3>\n<blockquote><p><strong>\u5143\u7ec4\uff08tuple\uff09<\/strong> \u00a0\u662f\u4e00\u79cd\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\u7c7b\u578b\uff0c\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u5143\u7d20<\/p><\/blockquote>\n<h4>\u521b\u5efa<\/h4>\n<pre><code>tuple = ('a', 'b', 'c')\nprint(tuple) # \u8f93\u51fa ('a', 'b', 'c')<\/code><\/pre>\n<h4>\u8bbf\u95ee<\/h4>\n<pre><code>tuple = ('a', 'b', 'c')\nitem = tuple[1] # \u8f93\u51fa 'b'\nitem2 = tuple[-1] # \u8f93\u51fa 'c'<\/code><\/pre>\n<h4>\u4e0d\u53ef\u53d8\u6027<\/h4>\n<pre><code>tuple = ('a', 'b', 'c')\ntuple[1] = 'd' # TypeError: 'tuple' object does not support item assignment<\/code><\/pre>\n<h4>\u53ef\u4ee5\u5305\u542b\u53ef\u53d8\u5bf9\u8c61<\/h4>\n<pre><code>tuple = ['a', ['b', 'c']]\ntuple[1][0] = 'd'\nprint(tuple) # \u8f93\u51fa ['a', ['d', 'c']]<\/code><\/pre>\n<h3>\u5b57\u5178<\/h3>\n<blockquote><p><strong>\u5b57\u5178\uff08Dictionary\uff09<\/strong> \u00a0\u662f\u4e00\u79cd\u65e0\u5e8f\u3001\u53ef\u53d8\u4e14\u53ef\u8fed\u4ee3\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\uff08key-value pairs\uff09<\/p><\/blockquote>\n<h4>\u521b\u5efa<\/h4>\n<pre><code># \u65b9\u5f0f1\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7\nempty_dict = {}\nperson = {&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: 18}\n\n# \u65b9\u5f0f2\uff1a\u4f7f\u7528 dict() \u6784\u9020\u51fd\u6570\ncolors = dict(red=&quot;#FF0000&quot;, green=&quot;#00FF00&quot;, blue=&quot;#0000FF&quot;)\n<\/code><\/pre>\n<h4>\u8bbf\u95ee<\/h4>\n<pre><code>person = {&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: 18}\nprint(person[&quot;name&quot;])  # \u8f93\u51fa: zhangsan\nprint(person.get(&quot;age&quot;))  # \u8f93\u51fa: 18 (\u63a8\u8350\u4f7f\u7528 get \u65b9\u6cd5)<\/code><\/pre>\n<h4>\u6dfb\u52a0<\/h4>\n<pre><code>person = {&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: 18}\nperson[&quot;city&quot;] = &quot;Beijing&quot;  # \u6dfb\u52a0\u65b0\u952e &quot;city&quot;\nprint(person) # {'name': 'zhangsan', 'age': 18, 'city': 'Beijing'}<\/code><\/pre>\n<h4>\u4fee\u6539<\/h4>\n<pre><code>person = {&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: 18}\nperson[&quot;name&quot;] = &quot;lisi&quot;\nprint(person) # {'name': 'lisi', 'age': 18}<\/code><\/pre>\n<h4>\u83b7\u53d6<\/h4>\n<pre><code>person = {&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: 18}\n\nprint(person[&quot;name&quot;]) # \u8f93\u51fazhangsan\nprint(person[&quot;city&quot;]) # \u62a5\u9519 KeyError: 'city'\n\nprint(person.get(&quot;name&quot;)) # \u8f93\u51fa zhangsan\nprint(person.get(&quot;city&quot;)) # \u8f93\u51fa None \u67e5\u4e0d\u5230\u65f6\u8fd4\u56deNone\nprint(person.get(&quot;job&quot;, &quot;IT&quot;)) # \u8f93\u51fa IT \u9ed8\u8ba4\u503c\n<\/code><\/pre>\n<h4>\u5220\u9664<\/h4>\n<pre><code>person = {&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: 18 }\nname = person.pop(&quot;name&quot;)\nprint(name) # \u8f93\u51fa zhangsan\nprint(person) # \u8f93\u51fa {'age': 18 }<\/code><\/pre>\n<h3>\u96c6\u5408<\/h3>\n<blockquote><p>\u5728 Python \u4e2d\uff0c\u96c6\u5408\uff08Set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u5143\u7d20\u552f\u4e00\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5c5e\u4e8e\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u4e00<\/p><\/blockquote>\n<h4>\u521b\u5efa<\/h4>\n<pre><code># \u521b\u5efa\u5305\u542b\u5143\u7d20\u7684\u96c6\u5408\nset = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}\nprint(set)  # \u8f93\u51fa: {'a', 'b', 'c'}\n\n# \u96c6\u5408\u4f1a\u81ea\u52a8\u53bb\u91cd\nnumbers = {1, 2, 2, 3, 3, 3}\nprint(numbers)  # \u8f93\u51fa: {1, 2, 3}<\/code><\/pre>\n<pre><code># \u4ece\u5217\u8868\u521b\u5efa\u96c6\u5408\nset = set([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;])\nprint(colors)  # \u8f93\u51fa: {'a', 'b', 'c'}\n\n# \u521b\u5efa\u7a7a\u96c6\u5408\uff08\u5fc5\u987b\u7528 set()\uff0c\u56e0\u4e3a {} \u8868\u793a\u7a7a\u5b57\u5178\uff09\nempty_set = set()\nprint(type(empty_set))  # \u8f93\u51fa: &lt;class 'set'&gt;<\/code><\/pre>\n<h4>\u6dfb\u52a0<\/h4>\n<pre><code>set = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}\nset.add('d')\nprint(set) # \u8f93\u51fa {'d', 'a', 'c', 'b'}<\/code><\/pre>\n<h4>\u5220\u9664<\/h4>\n<pre><code>set = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}\nset.remove('a')\nprint(set) # \u8f93\u51fa {'b', 'c'}<\/code><\/pre>\n<h3>\u51fd\u6570<\/h3>\n<blockquote><p>\u5728 Python \u4e2d\uff0c\u51fd\u6570\u662f\u7ec4\u7ec7\u597d\u7684\u3001\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u5757\uff0c\u7528\u4e8e\u6267\u884c\u5355\u4e00\u6216\u76f8\u5173\u8054\u7684\u4efb\u52a1\u3002\u51fd\u6570\u4f7f\u4ee3\u7801\u66f4\u6a21\u5757\u5316\u3001\u66f4\u6613\u8bfb\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e Python \u51fd\u6570\u5b9a\u4e49\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p><\/blockquote>\n<h4>\u51fd\u6570\u5b9a\u4e49<\/h4>\n<pre><code>def function_name(parameters):\n    &quot;&quot;&quot;\u51fd\u6570\u6587\u6863\u5b57\u7b26\u4e32\uff08\u53ef\u9009\uff09&quot;&quot;&quot;\n    # \u51fd\u6570\u4f53\n    return [expression]  # \u8fd4\u56de\u8bed\u53e5\uff08\u53ef\u9009\uff09<\/code><\/pre>\n<h4>\u8fd4\u56de\u503c<\/h4>\n<pre><code>def calculate(a, b):\n    return a + b, a - b, a * b\n\nsum_result, diff_result, prod_result = calculate(10, 5)\nprint(sum_result)    # \u8f93\u51fa: 15\nprint(diff_result)   # \u8f93\u51fa: 5\nprint(prod_result)   # \u8f93\u51fa: 50<\/code><\/pre>\n<h4>\u533f\u540d\u51fd\u6570<\/h4>\n<pre><code>square = lambda x: x ** 2\nprint(square(4))  # \u8f93\u51fa: 16\n\n# \u7ed3\u5408\u5185\u7f6e\u51fd\u6570\u4f7f\u7528\nnumbers = [1, 2, 3, 4, 5]\nsquared_numbers = list(map(lambda x: x ** 2, numbers))\nprint(squared_numbers)  # \u8f93\u51fa: [1, 4, 9, 16, 25]<\/code><\/pre>\n<h4>\u9ad8\u9636\u51fd\u6570<\/h4>\n<blockquote><p>\u6709\u4e9b\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u5176\u4ed6\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u6216\u8005\u628a\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u88ab\u79f0\u4e3a\u9ad8\u9636\u51fd\u6570<\/p><\/blockquote>\n<pre><code>def apply_operation(func, a, b):\n    return func(a, b)\n\nresult = apply_operation(lambda x, y: x + y, 3, 4)\nprint(result)  # \u8f93\u51fa: 7<\/code><\/pre>\n<h4>\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<pre><code>def count_up_to(n):\n    i = 1\n    while i &lt;= n:\n        yield i\n        i += 1\n\ncounter = count_up_to(3)\nprint(next(counter))  # \u8f93\u51fa: 1\nprint(next(counter))  # \u8f93\u51fa: 2\nprint(next(counter))  # \u8f93\u51fa: 3<\/code><\/pre>\n<h4>\u5185\u7f6e\u51fd\u6570<\/h4>\n<h5><strong>1. \u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/strong><\/h5>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>int(x)<\/code><\/td>\n<td>\u5c06\u00a0<code>x<\/code>\u00a0\u8f6c\u6362\u4e3a\u6574\u6570<\/td>\n<td><code>int(&quot;10&quot;) \u2192 10<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>float(x)<\/code><\/td>\n<td>\u5c06\u00a0<code>x<\/code>\u00a0\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/td>\n<td><code>float(&quot;3.14&quot;) \u2192 3.14<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>str(x)<\/code><\/td>\n<td>\u5c06\u00a0<code>x<\/code>\u00a0\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/td>\n<td><code>str(42) \u2192 &quot;42&quot;<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>bool(x)<\/code><\/td>\n<td>\u5c06\u00a0<code>x<\/code>\u00a0\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff08<code>True<\/code>\/<code>False<\/code>\uff09<\/td>\n<td><code>bool(0) \u2192 False<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>list(x)<\/code><\/td>\n<td>\u5c06\u00a0<code>x<\/code>\u00a0\u8f6c\u6362\u4e3a\u5217\u8868<\/td>\n<td><code>list(&quot;abc&quot;) \u2192 ['a', 'b', 'c']<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>tuple(x)<\/code><\/td>\n<td>\u5c06\u00a0<code>x<\/code>\u00a0\u8f6c\u6362\u4e3a\u5143\u7ec4<\/td>\n<td><code>tuple([1,2]) \u2192 (1,2)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>dict(x)<\/code><\/td>\n<td>\u5c06\u00a0<code>x<\/code>\u00a0\u8f6c\u6362\u4e3a\u5b57\u5178<\/td>\n<td><code>dict([('a',1)]) \u2192 {'a':1}<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>set(x)<\/code><\/td>\n<td>\u5c06\u00a0<code>x<\/code>\u00a0\u8f6c\u6362\u4e3a\u96c6\u5408<\/td>\n<td><code>set([1,1,2]) \u2192 {1,2}<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>2. \u6570\u5b66\u8fd0\u7b97<\/strong><\/h5>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>abs(x)<\/code><\/td>\n<td>\u8fd4\u56de\u00a0<code>x<\/code>\u00a0\u7684\u7edd\u5bf9\u503c<\/td>\n<td><code>abs(-5) \u2192 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>round(x, n)<\/code><\/td>\n<td>\u5bf9\u00a0<code>x<\/code>\u00a0\u56db\u820d\u4e94\u5165\uff0c\u4fdd\u7559\u00a0<code>n<\/code>\u00a0\u4f4d\u5c0f\u6570<\/td>\n<td><code>round(3.1415, 2) \u2192 3.14<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>max(iterable)<\/code><\/td>\n<td>\u8fd4\u56de\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6700\u5927\u503c<\/td>\n<td><code>max([1,5,3]) \u2192 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>min(iterable)<\/code><\/td>\n<td>\u8fd4\u56de\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6700\u5c0f\u503c<\/td>\n<td><code>min([1,5,3]) \u2192 1<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>sum(iterable)<\/code><\/td>\n<td>\u8fd4\u56de\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u6240\u6709\u5143\u7d20\u7684\u548c<\/td>\n<td><code>sum([1,2,3]) \u2192 6<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>3. \u8fed\u4ee3\u4e0e\u5e8f\u5217\u64cd\u4f5c<\/strong><\/h5>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>len(x)<\/code><\/td>\n<td>\u8fd4\u56de\u5bf9\u8c61\u7684\u957f\u5ea6\u6216\u5143\u7d20\u4e2a\u6570<\/td>\n<td><code>len(&quot;abc&quot;) \u2192 3<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>sorted(x)<\/code><\/td>\n<td>\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u65b0\u5217\u8868\uff08\u539f\u5217\u8868\u4e0d\u53d8\uff09<\/td>\n<td><code>sorted([3,1,2]) \u2192 [1,2,3]<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>reversed(x)<\/code><\/td>\n<td>\u8fd4\u56de\u53cd\u5411\u8fed\u4ee3\u5668<\/td>\n<td><code>list(reversed([1,2])) \u2192 [2,1]<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>range(start, stop, step)<\/code><\/td>\n<td>\u751f\u6210\u6570\u5b57\u5e8f\u5217<\/td>\n<td><code>list(range(0,5,2)) \u2192 [0,2,4]<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>enumerate(iterable)<\/code><\/td>\n<td>\u8fd4\u56de\u5e26\u7d22\u5f15\u7684\u5143\u7ec4\u8fed\u4ee3\u5668<\/td>\n<td><code>list(enumerate(&quot;ab&quot;)) \u2192 [(0,'a'), (1,'b')]<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>zip(*iterables)<\/code><\/td>\n<td>\u5c06\u591a\u4e2a\u8fed\u4ee3\u5668\u7684\u5143\u7d20\u6253\u5305\u6210\u5143\u7ec4<\/td>\n<td><code>list(zip([1,2], ['a','b'])) \u2192 [(1,'a'), (2,'b')]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>4. \u8f93\u5165\u8f93\u51fa<\/strong><\/h5>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>print(*objects)<\/code><\/td>\n<td>\u6253\u5370\u5bf9\u8c61\u5230\u6807\u51c6\u8f93\u51fa<\/td>\n<td><code>print(&quot;Hello&quot;, 42)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>input(prompt)<\/code><\/td>\n<td>\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u5b57\u7b26\u4e32\uff08\u5e26\u63d0\u793a\uff09<\/td>\n<td><code>name = input(&quot;\u8bf7\u8f93\u5165\u540d\u5b57: &quot;)<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>5. \u6587\u4ef6\u64cd\u4f5c<\/strong><\/h5>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>open(file, mode)<\/code><\/td>\n<td>\u6253\u5f00\u6587\u4ef6\u5e76\u8fd4\u56de\u6587\u4ef6\u5bf9\u8c61<\/td>\n<td><code>f = open(&quot;test.txt&quot;, &quot;r&quot;)<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>6. \u7c7b\u578b\u4e0e\u5bf9\u8c61\u68c0\u67e5<\/strong><\/h5>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>type(x)<\/code><\/td>\n<td>\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b<\/td>\n<td><code>type(42) \u2192 &lt;class 'int'&gt;<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>isinstance(x, type)<\/code><\/td>\n<td>\u5224\u65ad\u00a0<code>x<\/code>\u00a0\u662f\u5426\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u5b9e\u4f8b<\/td>\n<td><code>isinstance(42, int) \u2192 True<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>dir(x)<\/code><\/td>\n<td>\u8fd4\u56de\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u540d\u79f0<\/td>\n<td><code>dir([])<\/code>\u00a0\u8fd4\u56de\u5217\u8868\u7684\u6240\u6709\u65b9\u6cd5<\/td>\n<\/tr>\n<tr>\n<td><code>id(x)<\/code><\/td>\n<td>\u8fd4\u56de\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff08\u5185\u5b58\u5730\u5740\uff09<\/td>\n<td><code>id(obj)<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>7. \u9ad8\u7ea7\u5185\u7f6e\u51fd\u6570<\/strong><\/h5>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>map(func, iterable)<\/code><\/td>\n<td>\u5bf9\u8fed\u4ee3\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u51fd\u6570<\/td>\n<td><code>list(map(lambda x:x*2, [1,2])) \u2192 [2,4]<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>filter(func, iterable)<\/code><\/td>\n<td>\u8fc7\u6ee4\u8fed\u4ee3\u5668\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20<\/td>\n<td><code>list(filter(lambda x:x&gt;0, [-1,2])) \u2192 [2]<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>reduce(func, iterable)<\/code><\/td>\n<td>\u7d2f\u79ef\u8ba1\u7b97\u8fed\u4ee3\u5668\u4e2d\u7684\u5143\u7d20\uff08\u9700\u5bfc\u5165\uff09<\/td>\n<td><code>from functools import reduce<\/code> <code>reduce(lambda x,y:x+y, [1,2,3]) \u2192 6<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>eval(expression)<\/code><\/td>\n<td>\u6267\u884c\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u5e76\u8fd4\u56de\u7ed3\u679c<\/td>\n<td><code>eval(&quot;1+2&quot;) \u2192 3<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5><strong>8. \u5176\u4ed6\u5e38\u7528\u51fd\u6570<\/strong><\/h5>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>pow(x, y)<\/code><\/td>\n<td>\u8ba1\u7b97\u00a0<code>x<\/code>\u00a0\u7684\u00a0<code>y<\/code>\u00a0\u6b21\u5e42<\/td>\n<td><code>pow(2, 3) \u2192 8<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>divmod(x, y)<\/code><\/td>\n<td>\u8fd4\u56de\u00a0<code>x<\/code>\u00a0\u9664\u4ee5\u00a0<code>y<\/code>\u00a0\u7684\u5546\u548c\u4f59\u6570<\/td>\n<td><code>divmod(10, 3) \u2192 (3, 1)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>help([obj])<\/code><\/td>\n<td>\u663e\u793a\u5bf9\u8c61\u7684\u5e2e\u52a9\u6587\u6863\uff08\u4ea4\u4e92\u5f0f\u73af\u5883\uff09<\/td>\n<td><code>help(list)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>globals()<\/code><\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u5168\u5c40\u7b26\u53f7\u8868\uff08\u5b57\u5178\u5f62\u5f0f\uff09<\/td>\n<td><code>globals()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>locals()<\/code><\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u5c40\u90e8\u7b26\u53f7\u8868\uff08\u5b57\u5178\u5f62\u5f0f\uff09<\/td>\n<td><code>locals()<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u9762\u5411\u5bf9\u8c61<\/h3>\n<blockquote><p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08Object-Oriented Programming\uff0cOOP\uff09\u662f\u4e00\u79cd<strong>\u7f16\u7a0b\u8303\u5f0f<\/strong>\uff0c\u5b83\u5c06\u6570\u636e\uff08\u5c5e\u6027\uff09\u548c\u64cd\u4f5c\u6570\u636e\u7684\u4ee3\u7801\uff08\u65b9\u6cd5\uff09\u5c01\u88c5\u4e3a\u76f8\u4e92\u5173\u8054\u7684 \u201c\u5bf9\u8c61\u201d\uff0c\u5e76\u901a\u8fc7\u5bf9\u8c61\u4e4b\u95f4\u7684\u4ea4\u4e92\u6765\u6784\u5efa\u8f6f\u4ef6\u7cfb\u7edf\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u4e8b\u7269\u62bd\u8c61\u4e3a\u7a0b\u5e8f\u4e2d\u7684\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u62e5\u6709\u81ea\u5df1\u7684\u72b6\u6001\u548c\u884c\u4e3a\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684<strong>\u53ef\u590d\u7528\u6027<\/strong>\u3001<strong>\u53ef\u7ef4\u62a4\u6027<\/strong>\u548c<strong>\u53ef\u6269\u5c55\u6027<\/strong><\/p><\/blockquote>\n<h4>\u6838\u5fc3\u6982\u5ff5<\/h4>\n<p><strong>\u7c7b\uff08Class\uff09<\/strong> \uff1a\u7c7b\u662f\u5bf9\u8c61\u7684\u84dd\u56fe\u6216\u539f\u578b\uff0c\u5b83\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/p>\n<p><strong>\u5bf9\u8c61\uff08Object\uff09<\/strong> \uff1a\u5bf9\u8c61\u662f\u7c7b\u7684\u5b9e\u4f8b<\/p>\n<p><strong>\u65b9\u6cd5\uff08Method\uff09<\/strong> \uff1a\u65b9\u6cd5\u662f\u5bf9\u8c61\u53ef\u4ee5\u6267\u884c\u7684\u64cd\u4f5c\uff0c\u901a\u5e38\u662f\u51fd\u6570<\/p>\n<h4>\u4e09\u5927\u7279\u6027<\/h4>\n<h5>\u5c01\u88c5<\/h5>\n<blockquote><p>\u5c06\u6570\u636e\uff08\u5c5e\u6027\uff09\u548c\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u7ed1\u5b9a\u5728\u4e00\u8d77\uff0c\u5e76\u901a\u8fc7\u8bbf\u95ee\u63a7\u5236\u9690\u85cf\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u53ea\u66b4\u9732\u5fc5\u8981\u7684\u63a5\u53e3\u3002<\/p><\/blockquote>\n<pre><code>class BankAccount:\n    def __init__(self, balance):\n        self.__balance = balance  # \u79c1\u6709\u5c5e\u6027\uff0c\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\n        \n    # \u5b58\u6b3e\u65b9\u6cd5\n    def deposit(self, amount):\n        self.__balance += amount  \n        \n    # \u83b7\u53d6\u4f59\u989d\u65b9\u6cd5\n    def get_balance(self):\n        return self.__balance  \n\nbankAccount = BankAccount(200)\nprint(bankAccount.get_balance())  # \u8f93\u51fa: 200\nbankAccount.deposit(600)\nprint(bankAccount.get_balance())  # \u8f93\u51fa: 800<\/code><\/pre>\n<h5>\u7ee7\u627f<\/h5>\n<blockquote><p>\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u91cd\u5199\u6216\u6269\u5c55\u7236\u7c7b\u529f\u80fd\uff0c\u5b9e\u73b0\u4ee3\u7801\u590d\u7528<\/p><\/blockquote>\n<pre><code># \u7236\u7c7b\nclass Animal:\n    def __init__(self, name):\n        self.name = name\n\n    def speak(self):\n        raise NotImplementedError(&quot;\u5b50\u7c7b\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e2a\u65b9\u6cd5&quot;)\n\n# \u5b50\u7c7b\nclass Dog(Animal):\n    def speak(self):\n        return f&quot;{self.name} \u6c6a\u6c6a\u53eb&quot;\n\n# \u5b50\u7c7b\nclass Cat(Animal):\n    def speak(self):\n        return f&quot;{self.name} \u55b5\u55b5\u53eb&quot;\n\n# \u4f7f\u7528\u793a\u4f8b\ndog = Dog(&quot;\u5927\u9ec4&quot;)\ncat = Cat(&quot;\u5c0f\u82b1&quot;)\nprint(dog.speak())  # \u8f93\u51fa: \u5927\u9ec4 \u6c6a\u6c6a\u53eb\nprint(cat.speak())  # \u8f93\u51fa: \u5c0f\u82b1 \u55b5\u55b5\u53eb<\/code><\/pre>\n<h5>\u591a\u6001<\/h5>\n<blockquote><p>\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\u53ef\u4ee5\u5bf9\u540c\u4e00\u6d88\u606f\u505a\u51fa\u4e0d\u540c\u54cd\u5e94\uff0c\u901a\u8fc7\u7ee7\u627f\u548c\u63a5\u53e3\u5b9e\u73b0<\/p><\/blockquote>\n<pre><code>class Animal:\n    def speak(self):\n        return &quot;\u52a8\u7269\u53d1\u51fa\u58f0\u97f3&quot;\n\nclass Dog(Animal):\n    def speak(self):\n        return &quot;\u6c6a\u6c6a\u53eb&quot;\n\nclass Cat(Animal):\n    def speak(self):\n        return &quot;\u55b5\u55b5\u53eb&quot;\n\n# \u591a\u6001\u793a\u4f8b\uff1a\u540c\u4e00\u65b9\u6cd5\u8c03\u7528\uff0c\u4e0d\u540c\u5b50\u7c7b\u6709\u4e0d\u540c\u884c\u4e3a\ndef make_animal_speak(animal):\n    print(animal.speak())\n\ndog = Dog()\ncat = Cat()\n\nmake_animal_speak(dog)  # \u8f93\u51fa\uff1a\u6c6a\u6c6a\u53eb\nmake_animal_speak(cat)  # \u8f93\u51fa\uff1a\u55b5\u55b5\u53eb<\/code><\/pre>\n<h3>\u5224\u65ad<\/h3>\n<h4><strong>\u57fa\u672c\u00a0<code>if-else<\/code>\u00a0\u7ed3\u6784<\/strong><\/h4>\n<pre><code>x = 10\n\nif x &gt; 5:\n    print(&quot;x \u5927\u4e8e 5&quot;)  # \u6761\u4ef6\u4e3a\u771f\u65f6\u6267\u884c\nelse:\n    print(&quot;x \u5c0f\u4e8e\u7b49\u4e8e 5&quot;)<\/code><\/pre>\n<h4><strong>\u591a\u6761\u4ef6\u5224\u65ad\uff1a<code>elif<\/code><\/strong><\/h4>\n<pre><code>score = 85\n\nif score &gt;= 90:\n    print(&quot;\u4f18\u79c0&quot;)\nelif score &gt;= 80:\n    print(&quot;\u826f\u597d&quot;)  # \u6267\u884c\u6b64\u5206\u652f\nelif score &gt;= 60:\n    print(&quot;\u53ca\u683c&quot;)\nelse:\n    print(&quot;\u4e0d\u53ca\u683c&quot;)<\/code><\/pre>\n<h4><strong>\u5d4c\u5957\u5224\u65ad<\/strong><\/h4>\n<pre><code>age = 20\nis_student = True\n\nif age &gt;= 18:\n    if is_student:\n        print(&quot;\u6210\u5e74\u4eba\u5b66\u751f&quot;)  # \u6267\u884c\u6b64\u5206\u652f\n    else:\n        print(&quot;\u6210\u5e74\u4eba\u975e\u5b66\u751f&quot;)\nelse:\n    print(&quot;\u672a\u6210\u5e74\u4eba&quot;)<\/code><\/pre>\n<h4><strong>\u903b\u8f91\u8fd0\u7b97\u7b26\uff08<code>and<\/code>,\u00a0<code>or<\/code>,\u00a0<code>not<\/code>\uff09<\/strong><\/h4>\n<pre><code>x = 5\ny = 10\n\nif x &gt; 0 and y &lt; 20:\n    print(&quot;\u4e24\u4e2a\u6761\u4ef6\u90fd\u6ee1\u8db3&quot;)  # \u6267\u884c\u6b64\u5206\u652f\n\nif x &gt; 10 or y &gt; 5:\n    print(&quot;\u81f3\u5c11\u4e00\u4e2a\u6761\u4ef6\u6ee1\u8db3&quot;)  # \u6267\u884c\u6b64\u5206\u652f\n\nif not x &gt; 10:\n    print(&quot;x \u4e0d\u5927\u4e8e 10&quot;)  # \u6267\u884c\u6b64\u5206\u652f<\/code><\/pre>\n<h4><strong>\u6210\u5458\u8fd0\u7b97\u7b26\uff08<code>in<\/code>,\u00a0<code>not in<\/code>\uff09<\/strong><\/h4>\n<pre><code>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]\n\nif &quot;apple&quot; in fruits:\n    print(&quot;\u5217\u8868\u5305\u542b apple&quot;)  # \u6267\u884c\u6b64\u5206\u652f\n\nname = &quot;Alice&quot;\nif &quot;li&quot; in name:\n    print(&quot;\u540d\u5b57\u5305\u542b 'li'&quot;)  # \u6267\u884c\u6b64\u5206\u652f<\/code><\/pre>\n<h4><strong>\u4e09\u5143\u8868\u8fbe\u5f0f<\/strong><\/h4>\n<pre><code>x = 10\nresult = &quot;\u5076\u6570&quot; if x % 2 == 0 else &quot;\u5947\u6570&quot;\nprint(result)  # \u8f93\u51fa\uff1a\u5076\u6570<\/code><\/pre>\n<h4><strong>\u771f\u503c\u5224\u65ad<\/strong><\/h4>\n<p>Python \u4e2d\u4ee5\u4e0b\u503c\u88ab\u89c6\u4e3a\u00a0<code>False<\/code>\uff0c\u5176\u4ed6\u503c\u5747\u4e3a\u00a0<code>True<\/code>\uff1a<\/p>\n<ul>\n<li><code>False<\/code><\/li>\n<li><code>None<\/code><\/li>\n<li>\u6570\u503c 0\uff08\u5982\u00a0<code>0<\/code>,\u00a0<code>0.0<\/code>\uff09<\/li>\n<li>\u7a7a\u5e8f\u5217\uff08\u5982\u00a0<code>[]<\/code>,\u00a0<code>()<\/code>,\u00a0<code>''<\/code>\uff09<\/li>\n<li>\u7a7a\u6620\u5c04\uff08\u5982\u00a0<code>{}<\/code>\uff09<\/li>\n<\/ul>\n<pre><code>name = &quot;&quot;\nif not name:  # \u7a7a\u5b57\u7b26\u4e32\u4e3a False\n    print(&quot;\u540d\u5b57\u4e3a\u7a7a&quot;)\n\nvalues = []\nif values:  # \u7a7a\u5217\u8868\u4e3a False\uff0c\u6b64\u6761\u4ef6\u4e0d\u6267\u884c\n    print(&quot;\u5217\u8868\u4e0d\u4e3a\u7a7a&quot;)<\/code><\/pre>\n<h3>\u6a21\u5757<\/h3>\n<blockquote><p>\u5728 Python \u4e2d\uff0c<strong>\u6a21\u5757\uff08Module\uff09<\/strong> \u00a0\u662f\u7ec4\u7ec7\u4ee3\u7801\u7684\u57fa\u672c\u5355\u5143\uff0c\u7528\u4e8e\u5c06\u76f8\u5173\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u653e\u5728\u4e00\u8d77\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e Python \u6a21\u5757\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p><\/blockquote>\n<h4>\u4e00\u3001\u6a21\u5757\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<h5>1. \u4ec0\u4e48\u662f\u6a21\u5757\uff1f<\/h5>\n<ul>\n<li><strong>\u6a21\u5757<\/strong>\uff1a\u4e00\u4e2a<code>.py<\/code>\u6587\u4ef6\u5c31\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u6587\u4ef6\u540d\u5373\u6a21\u5757\u540d\u3002<\/li>\n<li><strong>\u5305\uff08Package\uff09<\/strong> \uff1a\u4e00\u4e2a\u5305\u542b<code>__init__.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u7528\u4e8e\u7ec4\u7ec7\u591a\u4e2a\u6a21\u5757\u3002<\/li>\n<\/ul>\n<h5>2. \u6a21\u5757\u7684\u4f5c\u7528<\/h5>\n<ul>\n<li><strong>\u4ee3\u7801\u590d\u7528<\/strong>\uff1a\u907f\u514d\u91cd\u590d\u7f16\u5199\u76f8\u540c\u4ee3\u7801\u3002<\/li>\n<li><strong>\u547d\u540d\u7a7a\u95f4\u9694\u79bb<\/strong>\uff1a\u4e0d\u540c\u6a21\u5757\u7684\u540c\u540d\u53d8\u91cf \/ \u51fd\u6570\u4e92\u4e0d\u5e72\u6270\u3002<\/li>\n<li><strong>\u7ec4\u7ec7\u67b6\u6784<\/strong>\uff1a\u6309\u529f\u80fd\u5c06\u4ee3\u7801\u5206\u6a21\u5757\u7ba1\u7406\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n<h4>\u4e8c\u3001\u6a21\u5757\u7684\u5bfc\u5165\u65b9\u5f0f<\/h4>\n<h5>1.\u00a0<code>import<\/code>\u00a0\u8bed\u53e5<\/h5>\n<p>\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u4f7f\u7528\u65f6\u9700\u52a0\u6a21\u5757\u540d\u524d\u7f00\u3002<\/p>\n<pre><code>import math\n\nprint(math.pi)          # \u8f93\u51fa: 3.141592653589793\nprint(math.sqrt(16))    # \u8f93\u51fa: 4.0<\/code><\/pre>\n<h5>2.\u00a0<code>from ... import ...<\/code><\/h5>\n<p>\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u5bf9\u8c61\uff0c\u76f4\u63a5\u4f7f\u7528\u65e0\u9700\u524d\u7f00\u3002<\/p>\n<pre><code>from math import pi, sqrt\n\nprint(pi)               # \u8f93\u51fa: 3.141592653589793\nprint(sqrt(16))         # \u8f93\u51fa: 4.0<\/code><\/pre>\n<h5>3.\u00a0<code>from ... import *<\/code><\/h5>\n<p>\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\uff08\u4e0d\u63a8\u8350\uff0c\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff09\u3002<\/p>\n<pre><code>from math import *\n\nprint(sin(pi\/2))        # \u8f93\u51fa: 1.0<\/code><\/pre>\n<h5>4.\u00a0<code>as<\/code>\u00a0\u522b\u540d<\/h5>\n<p>\u4e3a\u6a21\u5757\u6216\u5bf9\u8c61\u6307\u5b9a\u522b\u540d\uff0c\u7b80\u5316\u540d\u79f0\u3002<\/p>\n<pre><code>import numpy as np\nfrom pandas import DataFrame as DF\n\narr = np.array([1, 2, 3])\ndf = DF({'col1': [1, 2], 'col2': [3, 4]})<\/code><\/pre>\n<h4>\u4e09\u3001\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/h4>\n<p>Python \u89e3\u91ca\u5668\u6309\u4ee5\u4e0b\u987a\u5e8f\u67e5\u627e\u6a21\u5757\uff1a<\/p>\n<ol>\n<li><strong>\u5185\u7f6e\u6a21\u5757<\/strong>\uff08\u5982<code>sys<\/code>\u3001<code>math<\/code>\uff09<\/li>\n<li><strong>\u5f53\u524d\u76ee\u5f55<\/strong><\/li>\n<li><strong>\u73af\u5883\u53d8\u91cf<code>PYTHONPATH<\/code>\u6307\u5b9a\u7684\u8def\u5f84<\/strong><\/li>\n<li><strong>Python \u5b89\u88c5\u8def\u5f84\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u76ee\u5f55<\/strong><\/li>\n<\/ol>\n<p>\u67e5\u770b\u641c\u7d22\u8def\u5f84\uff1a<\/p>\n<pre><code>import sys\nprint(sys.path)  # \u8f93\u51fa\u6a21\u5757\u641c\u7d22\u8def\u5f84\u5217\u8868<\/code><\/pre>\n<h4>\u56db\u3001\u81ea\u5b9a\u4e49\u6a21\u5757<\/h4>\n<p>\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u4ee3\u7801\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a\u521b\u5efa<code>my_module.py<\/code><\/p>\n<pre><code># my_module.py\ndef greet(name):\n    return f&quot;Hello, {name}!&quot;\n\ndef add(a, b):\n    return a + b\n\n# \u6a21\u5757\u5185\u6d4b\u8bd5\u4ee3\u7801\nif __name__ == &quot;__main__&quot;:\n    print(greet(&quot;Alice&quot;))  # \u4ec5\u5f53\u76f4\u63a5\u8fd0\u884c\u6b64\u6587\u4ef6\u65f6\u6267\u884c<\/code><\/pre>\n<p><strong>\u5bfc\u5165\u5e76\u4f7f\u7528<\/strong>\uff1a<\/p>\n<pre><code>import my_module\n\nprint(my_module.greet(&quot;Bob&quot;))  # \u8f93\u51fa: Hello, Bob!\nprint(my_module.add(3, 4))     # \u8f93\u51fa: 7<\/code><\/pre>\n<h4>\u4e94\u3001\u5305\uff08Package\uff09\u7684\u4f7f\u7528<\/h4>\n<p>\u5305\u662f\u4e00\u79cd\u5c42\u6b21\u5316\u7684\u6a21\u5757\u7ec4\u7ec7\u65b9\u5f0f\uff0c\u901a\u8fc7\u76ee\u5f55\u7ed3\u6784\u7ba1\u7406\u6a21\u5757\u3002<\/p>\n<p><strong>\u76ee\u5f55\u7ed3\u6784\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code>my_package\/\n    __init__.py      # \u5fc5\u987b\u5b58\u5728\uff0c\u53ef\u4e3a\u7a7a\u6587\u4ef6\n    module1.py\n    module2.py\n    subpackage\/      # \u5b50\u5305\n        __init__.py\n        submodule.py<\/code><\/pre>\n<p><strong>\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/strong>\uff1a<\/p>\n<pre><code># \u65b9\u5f0f1\uff1a\u5b8c\u6574\u8def\u5f84\u5bfc\u5165\nimport my_package.module1\nmy_package.module1.foo()\n\n# \u65b9\u5f0f2\uff1a\u90e8\u5206\u5bfc\u5165\nfrom my_package import module2\nmodule2.bar()\n\n# \u65b9\u5f0f3\uff1a\u4ece\u5b50\u5305\u5bfc\u5165\nfrom my_package.subpackage import submodule\nsubmodule.baz()<\/code><\/pre>\n<h4>\u516d\u3001\u5e38\u7528\u5185\u7f6e\u6a21\u5757<\/h4>\n<p>Python \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u5927\u91cf\u5185\u7f6e\u6a21\u5757\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>os<\/strong>\uff1a\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3\uff08\u6587\u4ef6\u64cd\u4f5c\u3001\u8def\u5f84\u7ba1\u7406\uff09<\/li>\n<li><strong>sys<\/strong>\uff1aPython \u89e3\u91ca\u5668\u76f8\u5173\u53c2\u6570<\/li>\n<li><strong>math<\/strong>\uff1a\u6570\u5b66\u51fd\u6570<\/li>\n<li><strong>random<\/strong>\uff1a\u968f\u673a\u6570\u751f\u6210<\/li>\n<li><strong>datetime<\/strong>\uff1a\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406<\/li>\n<li><strong>json<\/strong>\uff1aJSON \u6570\u636e\u5904\u7406<\/li>\n<li><strong>requests<\/strong>\uff1aHTTP \u8bf7\u6c42\uff08\u9700\u5355\u72ec\u5b89\u88c5\uff09<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u64cd\u4f5c\u6587\u4ef6<\/p>\n<pre><code>import os\n\n# \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\nprint(os.getcwd())\n\n# \u5217\u51fa\u76ee\u5f55\u5185\u5bb9\nprint(os.listdir('.'))\n\n# \u521b\u5efa\u76ee\u5f55\nos.mkdir('new_dir')<\/code><\/pre>\n<h4>\u4e03\u3001\u6a21\u5757\u7684\u7279\u6b8a\u5c5e\u6027<\/h4>\n<ul>\n<li><strong><code>__name__<\/code><\/strong> \uff1a\u6a21\u5757\u7684\u540d\u79f0\uff0c\u76f4\u63a5\u8fd0\u884c\u65f6\u4e3a<code>__main__<\/code>\uff0c\u88ab\u5bfc\u5165\u65f6\u4e3a\u6a21\u5757\u540d\u3002<\/li>\n<li><strong><code>__doc__<\/code><\/strong> \uff1a\u6a21\u5757\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff08\u7b2c\u4e00\u884c\u6ce8\u91ca\uff09\u3002<\/li>\n<li><strong><code>__file__<\/code><\/strong> \uff1a\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code># \u67e5\u770b\u6a21\u5757\u5c5e\u6027\nimport math\n\nprint(math.__name__)   # \u8f93\u51fa: math\nprint(math.__doc__)    # \u8f93\u51fa: \u6a21\u5757\u6587\u6863\u5b57\u7b26\u4e32\nprint(math.__file__)   # \u8f93\u51fa: \u6a21\u5757\u6587\u4ef6\u8def\u5f84<\/code><\/pre>\n<h4>\u516b\u3001\u7b2c\u4e09\u65b9\u6a21\u5757\u7684\u5b89\u88c5\u4e0e\u7ba1\u7406<\/h4>\n<p>\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757\uff1a<\/p>\n<pre><code># \u5b89\u88c5\u6a21\u5757\npip install requests\n\n# \u67e5\u770b\u5df2\u5b89\u88c5\u6a21\u5757\npip list\n\n# \u66f4\u65b0\u6a21\u5757\npip install --upgrade requests\n\n# \u5220\u9664\u6a21\u5757\npip uninstall requests<\/code><\/pre>\n<h3>\u5faa\u73af<\/h3>\n<blockquote><p>\u5728 Python \u4e2d\uff0c\u5faa\u73af\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002Python \u63d0\u4f9b\u4e86\u4e24\u79cd\u4e3b\u8981\u7684\u5faa\u73af\u7ed3\u6784\uff1a<code>for<\/code>\u00a0\u5faa\u73af\u548c\u00a0<code>while<\/code>\u00a0\u5faa\u73af\uff0c\u53e6\u5916\u8fd8\u6709\u4e00\u4e9b\u8f85\u52a9\u7684\u5173\u952e\u5b57\u5982\u00a0<code>break<\/code>\u3001<code>continue<\/code>\u00a0\u7b49\u3002\u4e0b\u9762\u4e3a\u4f60\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p><\/blockquote>\n<h4>1.\u00a0<code>for<\/code>\u00a0\u5faa\u73af<\/h4>\n<p><code>for<\/code>\u00a0\u5faa\u73af\u7528\u4e8e\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u7b49\uff09\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u57fa\u672c\u8bed\u6cd5<\/strong>\uff1a<\/p>\n<pre><code>for \u53d8\u91cf in \u53ef\u8fed\u4ee3\u5bf9\u8c61:\n    # \u5faa\u73af\u4f53\u4ee3\u7801\n    pass  # \u66ff\u6362\u4e3a\u5b9e\u9645\u6267\u884c\u7684\u4ee3\u7801<\/code><\/pre>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a\u904d\u5386\u5217\u8868\u5e76\u6253\u5370\u6bcf\u4e2a\u5143\u7d20<\/p>\n<pre><code>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]\nfor fruit in fruits:\n    print(fruit)<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u00a0<code>range()<\/code>\u00a0\u51fd\u6570<\/strong>\uff1a\u5982\u679c\u9700\u8981\u904d\u5386\u6307\u5b9a\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u00a0<code>range()<\/code>\u00a0\u51fd\u6570\u3002<\/p>\n<pre><code>for i in range(5):  # \u751f\u6210 0 \u5230 4 \u7684\u6574\u6570\n    print(i)<\/code><\/pre>\n<h4>2.\u00a0<code>while<\/code>\u00a0\u5faa\u73af<\/h4>\n<p><code>while<\/code>\u00a0\u5faa\u73af\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002<\/p>\n<p><strong>\u57fa\u672c\u8bed\u6cd5<\/strong>\uff1a<\/p>\n<pre><code>while \u6761\u4ef6:\n    # \u5faa\u73af\u4f53\u4ee3\u7801\n    pass  # \u66ff\u6362\u4e3a\u5b9e\u9645\u6267\u884c\u7684\u4ee3\u7801<\/code><\/pre>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a\u8ba1\u7b97 1 \u5230 10 \u7684\u7d2f\u52a0\u548c<\/p>\n<pre><code>sum = 0\ni = 1\nwhile i &lt;= 10:\n    sum += i\n    i += 1\nprint(sum)  # \u8f93\u51fa 55<\/code><\/pre>\n<h4>3. \u5faa\u73af\u63a7\u5236\u8bed\u53e5<\/h4>\n<ul>\n<li>\n<p><strong><code>break<\/code><\/strong>\uff1a\u7528\u4e8e\u8df3\u51fa\u6574\u4e2a\u5faa\u73af\uff0c\u4e0d\u518d\u6267\u884c\u540e\u7eed\u7684\u8fed\u4ee3\u3002<\/p>\n<pre><code>for i in range(5):\n    if i == 3:\n        break\n    print(i)  # \u8f93\u51fa 0, 1, 2<\/code><\/pre>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p><strong><code>continue<\/code><\/strong>\uff1a\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/p>\n<pre><code>for i in range(5):\n    if i == 3:\n        continue\n    print(i)  # \u8f93\u51fa 0, 1, 2, 4<\/code><\/pre>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p><strong><code>else<\/code>\u00a0\u5b50\u53e5<\/strong>\uff1a\u5728\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\uff08\u6ca1\u6709\u88ab\u00a0<code>break<\/code>\u00a0\u4e2d\u65ad\uff09\u65f6\u6267\u884c\u3002<\/p>\n<pre><code>for i in range(3):\n    print(i)\nelse:\n    print(&quot;\u5faa\u73af\u7ed3\u675f&quot;)  # \u4f1a\u88ab\u6267\u884c<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4>4. \u5d4c\u5957\u5faa\u73af<\/h4>\n<p>\u5faa\u73af\u5185\u90e8\u53ef\u4ee5\u5305\u542b\u53e6\u4e00\u4e2a\u5faa\u73af\uff0c\u5e38\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a\u6253\u5370\u4e58\u6cd5\u8868<\/p>\n<pre><code>for i in range(1, 10):\n    for j in range(1, i + 1):\n        print(f&quot;{j}\u00d7{i}={i*j}&quot;, end=&quot;\/t&quot;)\n    print()  # \u6362\u884c<\/code><\/pre>\n<h3>\u6b63\u5219<\/h3>\n<blockquote><p>\u5728 Python \u91cc\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff09\u662f\u7528\u4e8e\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5b83\u501f\u52a9\u7279\u5b9a\u6a21\u5f0f\u6765\u5339\u914d\u3001\u67e5\u627e\u3001\u66ff\u6362\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u4e3a\u4f60\u4ecb\u7ecd Python \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6838\u5fc3\u529f\u80fd\u548c\u7528\u6cd5\u3002<\/p><\/blockquote>\n<h4>1. \u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u7840\u5143\u5b57\u7b26<\/h4>\n<ul>\n<li><code>.<\/code>\uff1a\u80fd\u591f\u5339\u914d\u9664\u6362\u884c\u7b26\u4e4b\u5916\u7684\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\u3002<\/li>\n<li><code>^<\/code>\uff1a\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/li>\n<li><code>$<\/code>\uff1a\u7528\u6765\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002<\/li>\n<li><code>*<\/code>\uff1a\u8868\u793a\u524d\u9762\u7684\u5b57\u7b26\u53ef\u4ee5\u51fa\u73b0\u96f6\u6b21\u6216\u8005\u591a\u6b21\u3002<\/li>\n<li><code>+<\/code>\uff1a\u610f\u5473\u7740\u524d\u9762\u7684\u5b57\u7b26\u81f3\u5c11\u8981\u51fa\u73b0\u4e00\u6b21\u3002<\/li>\n<li><code>?<\/code>\uff1a\u8868\u793a\u524d\u9762\u7684\u5b57\u7b26\u53ef\u4ee5\u51fa\u73b0\u96f6\u6b21\u6216\u8005\u4e00\u6b21\u3002<\/li>\n<li><code>{n}<\/code>\uff1a\u8981\u6c42\u524d\u9762\u7684\u5b57\u7b26\u6070\u597d\u51fa\u73b0\u00a0<code>n<\/code>\u00a0\u6b21\u3002<\/li>\n<li><code>{n,}<\/code>\uff1a\u8981\u6c42\u524d\u9762\u7684\u5b57\u7b26\u81f3\u5c11\u51fa\u73b0\u00a0<code>n<\/code>\u00a0\u6b21\u3002<\/li>\n<li><code>{n,m}<\/code>\uff1a\u8981\u6c42\u524d\u9762\u7684\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u5728\u00a0<code>n<\/code>\u00a0\u5230\u00a0<code>m<\/code>\u00a0\u6b21\u4e4b\u95f4\u3002<\/li>\n<li><code>[]<\/code>\uff1a\u7528\u4e8e\u5339\u914d\u65b9\u62ec\u53f7\u4e2d\u6307\u5b9a\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\uff0c\u4f8b\u5982\u00a0<code>[a-z]<\/code>\u00a0\u53ef\u4ee5\u5339\u914d\u4efb\u610f\u5c0f\u5199\u5b57\u6bcd\u3002<\/li>\n<li><code>|<\/code>\uff1a\u8868\u793a\u6216\u8005\u7684\u5173\u7cfb\uff0c\u7528\u4e8e\u5339\u914d\u591a\u4e2a\u6a21\u5f0f\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u3002<\/li>\n<li><code>()<\/code>\uff1a\u7528\u4e8e\u5206\u7ec4\uff0c\u53ef\u4ee5\u63d0\u53d6\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<h4>2. Python \u4e2d\u7684 re \u6a21\u5757<\/h4>\n<p>Python \u901a\u8fc7\u00a0<code>re<\/code>\u00a0\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u51fd\u6570\uff1a<\/p>\n<h5>\uff081\uff09re.match()<\/h5>\n<p>\u6b64\u51fd\u6570\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\u6a21\u5f0f\uff0c\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff1b\u5426\u5219\u8fd4\u56de\u00a0<code>None<\/code>\u3002<\/p>\n<pre><code>import re\n\npattern = r'hello'\ntext = 'hello world'\nmatch = re.match(pattern, text)\nif match:\n    print('\u5339\u914d\u6210\u529f:', match.group())  # \u8f93\u51fa: \u5339\u914d\u6210\u529f: hello\nelse:\n    print('\u5339\u914d\u5931\u8d25')<\/code><\/pre>\n<h5>\uff082\uff09re.search()<\/h5>\n<p>\u8be5\u51fd\u6570\u4f1a\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u82e5\u627e\u5230\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff1b\u82e5\u672a\u627e\u5230\uff0c\u5219\u8fd4\u56de\u00a0<code>None<\/code>\u3002<\/p>\n<pre><code>pattern = r'world'\ntext = 'hello world'\nsearch = re.search(pattern, text)\nif search:\n    print('\u627e\u5230\u5339\u914d:', search.group())  # \u8f93\u51fa: \u627e\u5230\u5339\u914d: world\nelse:\n    print('\u672a\u627e\u5230\u5339\u914d')<\/code><\/pre>\n<h5>\uff083\uff09re.findall()<\/h5>\n<p>\u6b64\u51fd\u6570\u4f1a\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code>pattern = r'\/d+'  # \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\ntext = 'I have 3 apples and 5 oranges'\nnumbers = re.findall(pattern, text)\nprint('\u627e\u5230\u7684\u6570\u5b57:', numbers)  # \u8f93\u51fa: \u627e\u5230\u7684\u6570\u5b57: ['3', '5']<\/code><\/pre>\n<h5>\uff084\uff09re.sub()<\/h5>\n<p>\u8be5\u51fd\u6570\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u5339\u914d\u6a21\u5f0f\u7684\u90e8\u5206\uff0c\u53ef\u4ee5\u6307\u5b9a\u66ff\u6362\u7684\u6b21\u6570\u3002<\/p>\n<pre><code>pattern = r'apple'\ntext = 'I like apple'\nnew_text = re.sub(pattern, 'banana', text)\nprint('\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32:', new_text)  # \u8f93\u51fa: \u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32: I like banana<\/code><\/pre>\n<h5>\uff085\uff09re.split()<\/h5>\n<p>\u6b64\u51fd\u6570\u4f9d\u636e\u5339\u914d\u7684\u6a21\u5f0f\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272\uff0c\u5e76\u8fd4\u56de\u5206\u5272\u540e\u7684\u5217\u8868\u3002<\/p>\n<pre><code>pattern = r'\/s+'  # \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u767d\u5b57\u7b26\ntext = 'Hello   World'\nwords = re.split(pattern, text)\nprint('\u5206\u5272\u540e\u7684\u5355\u8bcd:', words)  # \u8f93\u51fa: \u5206\u5272\u540e\u7684\u5355\u8bcd: ['Hello', 'World']<\/code><\/pre>\n<h4>3. \u5339\u914d\u5bf9\u8c61\u7684\u65b9\u6cd5<\/h4>\n<p>\u5f53\u4f7f\u7528\u00a0<code>match()<\/code>\u3001<code>search()<\/code>\u00a0\u7b49\u51fd\u6570\u83b7\u5f97\u5339\u914d\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u5229\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li><code>group()<\/code>\uff1a\u8fd4\u56de\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>start()<\/code>\uff1a\u8fd4\u56de\u5339\u914d\u5f00\u59cb\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><code>end()<\/code>\uff1a\u8fd4\u56de\u5339\u914d\u7ed3\u675f\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><code>span()<\/code>\uff1a\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u5305\u542b\u5339\u914d\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<pre><code>pattern = r'(\/d{3})-(\/d{4})'\ntext = 'My phone number is 123-4567'\nmatch = re.search(pattern, text)\nif match:\n    print('\u5b8c\u6574\u5339\u914d:', match.group())  # \u8f93\u51fa: \u5b8c\u6574\u5339\u914d: 123-4567\n    print('\u7b2c\u4e00\u7ec4:', match.group(1))    # \u8f93\u51fa: \u7b2c\u4e00\u7ec4: 123\n    print('\u7b2c\u4e8c\u7ec4:', match.group(2))    # \u8f93\u51fa: \u7b2c\u4e8c\u7ec4: 4567\n    print('\u5f00\u59cb\u4f4d\u7f6e:', match.start())   # \u8f93\u51fa: \u5f00\u59cb\u4f4d\u7f6e: 19\n    print('\u7ed3\u675f\u4f4d\u7f6e:', match.end())     # \u8f93\u51fa: \u7ed3\u675f\u4f4d\u7f6e: 28<\/code><\/pre>\n<h4>4. \u6807\u5fd7\u4f4d<\/h4>\n<p>\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6807\u5fd7\u4f4d\u6765\u4fee\u6539\u5339\u914d\u7684\u884c\u4e3a\uff0c\u5e38\u89c1\u7684\u6807\u5fd7\u4f4d\u6709\uff1a<\/p>\n<ul>\n<li><code>re.I<\/code>\uff08\u6216\u00a0<code>re.IGNORECASE<\/code>\uff09\uff1a\u8fdb\u884c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5339\u914d\u3002<\/li>\n<li><code>re.M<\/code>\uff08\u6216\u00a0<code>re.MULTILINE<\/code>\uff09\uff1a\u4f7f\u00a0<code>^<\/code>\u00a0\u548c\u00a0<code>$<\/code>\u00a0\u80fd\u591f\u5339\u914d\u6bcf\u884c\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u3002<\/li>\n<li><code>re.S<\/code>\uff08\u6216\u00a0<code>re.DOTALL<\/code>\uff09\uff1a\u8ba9\u00a0<code>.<\/code>\u00a0\u53ef\u4ee5\u5339\u914d\u5305\u62ec\u6362\u884c\u7b26\u5728\u5185\u7684\u4efb\u610f\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<pre><code>pattern = r'hello'\ntext = 'HELLO world'\nmatch = re.search(pattern, text, re.I)  # \u4f7f\u7528 re.I \u6807\u5fd7\u4f4d\u5ffd\u7565\u5927\u5c0f\u5199\nif match:\n    print('\u5339\u914d\u6210\u529f:', match.group())  # \u8f93\u51fa: \u5339\u914d\u6210\u529f: HELLO<\/code><\/pre>\n<h4>5. \u8d2a\u5a6a\u5339\u914d\u4e0e\u975e\u8d2a\u5a6a\u5339\u914d<\/h4>\n<ul>\n<li>\u8d2a\u5a6a\u5339\u914d\uff1a\u4f1a\u5c3d\u53ef\u80fd\u591a\u5730\u5339\u914d\u5b57\u7b26\uff0c\u4f8b\u5982\u00a0<code>.*<\/code>\u00a0\u4f1a\u5339\u914d\u5c3d\u53ef\u80fd\u591a\u7684\u4efb\u610f\u5b57\u7b26\u3002<\/li>\n<li>\u975e\u8d2a\u5a6a\u5339\u914d\uff1a\u4f1a\u5c3d\u53ef\u80fd\u5c11\u5730\u5339\u914d\u5b57\u7b26\uff0c\u5728\u91cf\u8bcd\u540e\u9762\u52a0\u4e0a\u00a0<code>?<\/code>\u00a0\u5c31\u53ef\u4ee5\u5b9e\u73b0\u975e\u8d2a\u5a6a\u5339\u914d\uff0c\u4f8b\u5982\u00a0<code>.*?<\/code>\u3002<\/li>\n<\/ul>\n<pre><code>text = '&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;'\n\n# \u8d2a\u5a6a\u5339\u914d\ngreedy = re.search(r'&lt;.*&gt;', text)\nprint('\u8d2a\u5a6a\u5339\u914d:', greedy.group())  # \u8f93\u51fa: \u8d2a\u5a6a\u5339\u914d: &lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;\n\n# \u975e\u8d2a\u5a6a\u5339\u914d\nnon_greedy = re.search(r'&lt;.*?&gt;', text)\nprint('\u975e\u8d2a\u5a6a\u5339\u914d:', non_greedy.group())  # \u8f93\u51fa: \u975e\u8d2a\u5a6a\u5339\u914d: &lt;html&gt;<\/code><\/pre>\n<h4>6. \u5e38\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u793a\u4f8b<\/h4>\n<ul>\n<li>\u5339\u914d\u90ae\u7bb1\uff1a<code>r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+'<\/code><\/li>\n<li>\u5339\u914d URL\uff1a<code>r'https?:\/\/\/S+|www.\/S+'<\/code><\/li>\n<li>\u5339\u914d IP \u5730\u5740\uff1a<code>r'\/d{1,3}.\/d{1,3}.\/d{1,3}.\/d{1,3}'<\/code><\/li>\n<li>\u5339\u914d\u624b\u673a\u53f7\u7801\uff1a<code>r'1[3-9]\/d{9}'<\/code><\/li>\n<\/ul>\n<h4>7. \u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<p>\u5f53\u540c\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u9700\u8981\u5728\u591a\u4e2a\u5730\u65b9\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u5148\u5bf9\u5176\u8fdb\u884c\u7f16\u8bd1\uff0c\u8fd9\u6837\u80fd\u591f\u63d0\u9ad8\u5339\u914d\u6548\u7387\u3002<\/p>\n<pre><code>pattern = re.compile(r'\/d+')  # \u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\ntext = 'I have 3 apples and 5 oranges'\nnumbers = pattern.findall(text)\nprint('\u627e\u5230\u7684\u6570\u5b57:', numbers)  # \u8f93\u51fa: \u627e\u5230\u7684\u6570\u5b57: ['3', '5']<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046825325\">https:\/\/segmentfault.com\/a\/1190000046825325<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5df2\u540c\u6b65\u5230github AI \u5927\u6a21\u578b\u5e94\u7528\u8fdb\u9636\u7cfb\u5217\uff08\u4e00\uff09\uff1aPython \u57fa\u7840\uff0c\u6b22\u8fceStar\u3002 \u6570\u636e\u7c7b\u578b \u6570\u5b57  [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020878","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020878"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020878\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}