{"id":320,"date":"2024-04-29T00:25:21","date_gmt":"2024-04-28T16:25:21","guid":{"rendered":"https:\/\/669082.xyz\/?p=320"},"modified":"2024-04-29T01:47:18","modified_gmt":"2024-04-28T17:47:18","slug":"20240428%e5%ad%a6%e4%b9%a0python_%e6%9d%82%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/669082.xyz\/index.php\/2024\/04\/29\/20240428%e5%ad%a6%e4%b9%a0python_%e6%9d%82%e9%a1%b9\/","title":{"rendered":"20240428\u5b66\u4e60Python_\u6742\u9879"},"content":{"rendered":"<pre><code class=\"language-python\">#Python \u8fed\u4ee3\u5668\n#\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u503c\u7684\u53ef\u8ba1\u6570\u6570\u5b57\u3002\n#\u5728 Python \u4e2d\uff0c\u8fed\u4ee3\u5668\u662f\u5b9e\u73b0\u8fed\u4ee3\u5668\u534f\u8bae\u7684\u5bf9\u8c61\uff0c\u5b83\u5305\u542b\u65b9\u6cd5 __iter__() \u548c __next__()\u3002\n\n#\u4ece\u5143\u7ec4\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5e76\u6253\u5370\u6bcf\u4e2a\u503c\uff1a\nmytuple = (&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;)\nmyit = iter(mytuple)\n\nprint(next(myit))\nprint(next(myit))\nprint(next(myit))\n\n#\u5b57\u7b26\u4e32\u4e5f\u662f\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\nmystr = &quot;banana&quot;\nmyit = iter(mystr)\n\nprint(next(myit))\nprint(next(myit))\nprint(next(myit))\nprint(next(myit))\nprint(next(myit))\nprint(next(myit))\n\n#\u4e5f\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff1a\nmytuple = (&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;)\n\nfor x in mytuple:\n  print(x)\n\n#\u7528for\u8fed\u4ee3\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff1a\nmystr = &quot;banana&quot;\n\nfor x in mystr:\n  print(x)\n\n#\u521b\u5efa\u8fed\u4ee3\u5668\nclass MyNumbers:\n  def __iter__(self):\n    self.a = 1\n    return self\n\n  def __next__(self):\n    x = self.a\n    self.a += 1\n    return x\n\nmyclass = MyNumbers()\nmyiter = iter(myclass)\n\nprint(next(myiter))\nprint(next(myiter))\nprint(next(myiter))\nprint(next(myiter))\nprint(next(myiter))\n\n#StopIteration\u8bed\u53e5\n#\u4e3a\u4e86\u9632\u6b62\u8fed\u4ee3\u6c38\u8fdc\u8fdb\u884c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 StopIteration \u8bed\u53e5\u3002\nclass MyNumbers:\n  def __iter__(self):\n    self.a = 1\n    return self\n\n  def __next__(self):\n    if self.a &lt;= 20:\n      x = self.a\n      self.a += 1\n      return x\n    else:\n      raise StopIteration\n\nmyclass = MyNumbers()\nmyiter = iter(myclass)\n\nfor x in myiter:\n  print(x)<\/code><\/pre>\n<pre><code class=\"language-python\">#Python \u4f5c\u7528\u57df\n#\u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u7684\u53d8\u91cf\u5728\u8be5\u51fd\u6570\u5185\u90e8\u53ef\u7528\uff1a\ndef myfunc():\n  x = 100\n  print(x)\n\nmyfunc()\n\n#\u80fd\u591f\u4ece\u51fd\u6570\u5185\u7684\u4e00\u4e2a\u51fd\u6570\u8bbf\u95ee\u5c40\u90e8\u53d8\u91cf\uff1a\ndef myfunc():\n  x = 100\n  def myinnerfunc():\n    print(x)\n  myinnerfunc()\n\nmyfunc()\n\n#\u51fd\u6570\u5916\u90e8\u521b\u5efa\u7684\u53d8\u91cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u4f7f\u7528\uff1a\nx = 100\n\ndef myfunc():\n  print(x)\n\nmyfunc()\n\nprint(x)\n\n#\u5982\u679c\u51fd\u6570\u5185\u90e8\u6709\u76f8\u540c\u540d\u5b57\u7684\u53d8\u91cf\nx = 100\n\ndef myfunc():\n  x = 200\n  print(x) #200 \u4f7f\u7528\u51fd\u6570\u5185\u90e8\u7684\n\nmyfunc()\n\nprint(x) #100\u5168\u5c40\u7684\n\n#Global \u5173\u952e\u5b57\n#\u5982\u679c\u4f7f\u7528 global \u5173\u952e\u5b57\uff0c\u5219\u8be5\u53d8\u91cf\u5c5e\u4e8e\u5168\u5c40\ndef myfunc():\n  global x\n  x = 100\n\nmyfunc()\n\nprint(x)\n\n#\u5728\u51fd\u6570\u5185\u90e8\u66f4\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u8bf7\u4f7f\u7528 global \u5173\u952e\u5b57\u5f15\u7528\u8be5\u53d8\u91cf\uff1a\nx = 100\n\ndef myfunc():\n  global x\n  x = 200\n\nmyfunc()\n\nprint(x)\n<\/code><\/pre>\n<pre><code class=\"language-python\">#Python \u6a21\u5757\n#\u6a21\u5757\u662f\u5305\u542b\u4e00\u7ec4\u51fd\u6570\u7684\u6587\u4ef6\uff0c\u5e0c\u671b\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5f15\u7528\u3002\n#\u5982\u9700\u521b\u5efa\u6a21\u5757\uff0c\u53ea\u9700\u5c06\u6240\u9700\u4ee3\u7801\u4fdd\u5b58\u5728\u6587\u4ef6\u6269\u5c55\u540d\u4e3a .py \u7684\u6587\u4ef6\u4e2d\uff1a\n\n#\u5728\u540d\u4e3a mymodule.py \u7684\u6587\u4ef6\u4e2d\u4fdd\u5b58\u4ee3\u7801\uff1a\ndef greeting(name):\n  print(&quot;Hello, &quot; + name)\n\n#\u5728\u53e6\u5916\u6587\u4ef6\u4e2d\u5bfc\u5165\u540d\u4e3a mymodule \u7684\u6a21\u5757\uff0c\u5e76\u8c03\u7528 greeting \u51fd\u6570\uff1a\nimport mymodule\n\nmymodule.greeting(&quot;Bill&quot;)\n\n#\u6ce8\u91ca\uff1a\u5982\u679c\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u65f6\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a\n#module_name.function_name\n\n#\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\n#\u6a21\u5757\u53ef\u4ee5\u5305\u542b\u5df2\u7ecf\u63cf\u8ff0\u7684\u51fd\u6570\uff0c\u4f46\u4e5f\u53ef\u4ee5\u5305\u542b\u5404\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\uff08\u6570\u7ec4\u3001\u5b57\u5178\u3001\u5bf9\u8c61\u7b49\uff09\uff1a\n\n#\u5728\u6587\u4ef6 mymodule.py \u4e2d\u4fdd\u5b58\u4ee3\u7801\uff1a\nperson1 = {\n  &quot;name&quot;: &quot;Bill&quot;,\n  &quot;age&quot;: 63,\n  &quot;country&quot;: &quot;USA&quot;\n}\n\n#\u5728\u53e6\u4e2a\u6587\u4ef6\u4e2d\u5bfc\u5165\u540d\u4e3a mymodule \u7684\u6a21\u5757\uff0c\u5e76\u8bbf\u95ee person1 \u5b57\u5178\uff1a\nimport mymodule\n\na = mymodule.person1[&quot;age&quot;]\nprint(a)\n\n#\u60a8\u53ef\u4ee5\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u4f7f\u7528 as \u5173\u952e\u5b57\u521b\u5efa\u522b\u540d\uff1a\nimport mymodule as mx\n\na = mx.person1[&quot;age&quot;]\nprint(a)\n\n#Python \u4e2d\u6709\u4e00\u4e9b\u5185\u5efa\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u968f\u65f6\u5bfc\u5165\u3002\nimport platform\n\nx = platform.system()\nprint(x)\n\n#dir() \u51fd\u6570\n#\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u5217\u51fa\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u540d\uff08\u6216\u53d8\u91cf\u540d\uff09\u3002dir() \u51fd\u6570\uff1a\n#\u6ce8\u91ca\uff1adir() \u51fd\u6570\u53ef\u7528\u4e8e\u6240\u6709\u6a21\u5757\uff0c\u4e5f\u53ef\u7528\u4e8e\u60a8\u81ea\u5df1\u521b\u5efa\u7684\u6a21\u5757\n#\u5217\u51fa\u5c5e\u4e8e platform \u6a21\u5757\u7684\u6240\u6709\u5df2\u5b9a\u4e49\u540d\u79f0\uff1a\nimport platform\n\nx = dir(platform)\nprint(x)\n\n#\u4ece\u6a21\u5757\u5bfc\u5165\n#\u60a8\u53ef\u4ee5\u4f7f\u7528 from \u5173\u952e\u5b57\u9009\u62e9\u4ec5\u4ece\u6a21\u5757\u5bfc\u5165\u90e8\u4ef6\u3002\n\n#\u540d\u4e3a mymodule \u7684\u6a21\u5757\u62e5\u6709\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u5b57\u5178\uff1a\ndef greeting(name):\n  print(&quot;Hello, &quot; + name)\n\nperson1 = {\n  &quot;name&quot;: &quot;Bill&quot;,\n  &quot;age&quot;: 63,\n  &quot;country&quot;: &quot;USA&quot;\n}\n\n#\u4ec5\u4ece\u6a21\u5757\u5bfc\u5165 person1 \u5b57\u5178\uff1a\nfrom mymodule import person1\n\nprint (person1[&quot;age&quot;])\n<\/code><\/pre>\n<pre><code class=\"language-python\">#Python \u65e5\u671f\nimport datetime\n\nx = datetime.datetime.now()\nprint(x) #2024-04-29 00:41:08.220933\n\n#\u8fd4\u56de weekday \u7684\u540d\u79f0\u548c\u5e74\u4efd\uff1a\nimport datetime\n\nx = datetime.datetime.now()\n\nprint(x.year)\nprint(x.strftime(&quot;%A&quot;))<\/code><\/pre>\n<h5>python\u91cc\u5173\u4e8e\u65e5\u671f \u5408\u6cd5\u683c\u5f0f\u4ee3\u7801\u7684\u53c2\u8003<\/h5>\n<p><img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/04\/image-1714322586596.png\" alt=\"file\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/04\/image-1714322622927.png\" alt=\"file\" \/><\/p>\n<h5>Python \u4e2d\u7684 JSON<\/h5>\n<pre><code class=\"language-python\">#json\u8f6c\u5b57\u5178\nimport json\n\nx =  &#039;{ &quot;name&quot;:&quot;Bill&quot;, &quot;age&quot;:63, &quot;city&quot;:&quot;Seatle&quot;}&#039; # \u4e00\u4e9b JSON:\n\ny = json.loads(x) # \u89e3\u6790 x:\n\nprint(type(y)) #&lt;class &#039;dict&#039;&gt;\n\nprint(y[&quot;age&quot;]) # \u7ed3\u679c\u662f Python \u5b57\u5178\uff1a\n\n#\u628a Python \u5b57\u5178 \u8f6c\u6362\u4e3a JSON\nimport json\n\n# Python \u5bf9\u8c61\uff08\u5b57\u5178\uff09\uff1a\nx = {\n  &quot;name&quot;: &quot;Bill&quot;,\n  &quot;age&quot;: 63,\n  &quot;city&quot;: &quot;Seatle&quot;\n}\n\n# \u8f6c\u6362\u4e3a JSON\uff1a\ny = json.dumps(x)\n\n# \u7ed3\u679c\u662f JSON \u5b57\u7b26\u4e32\uff1a\nprint(y)\n\n#\u53ef\u4ee5\u628a\u4ee5\u4e0b\u7c7b\u578b\u7684 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff1a\n#dict\n#list\n#tuple\n#string\n#int\n#float\n#True\n#False\n#None\n\n#\u5b9e\u4f8b \u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u5e76\u6253\u5370\u503c\uff1a\n#\u770b\u8d77\u6765\u53ea\u6709\u5b57\u5178 \u8f6c\u5316\u5f97\u624d\u662f\u771f\u6b63\u7684json\nimport json\n\nprint(json.dumps({&quot;name&quot;: &quot;Bill&quot;, &quot;age&quot;: 63}))\nprint(json.dumps([&quot;apple&quot;, &quot;bananas&quot;]))\nprint(json.dumps((&quot;apple&quot;, &quot;bananas&quot;)))\nprint(json.dumps(&quot;hello&quot;))\nprint(json.dumps(42))\nprint(json.dumps(31.76))\nprint(json.dumps(True))\nprint(json.dumps(False))\nprint(json.dumps(None))\n\n#\u4e00\u4e2a\u8f6c\u6362\u5b9e\u4f8b\nimport json\n\nx = {\n  &quot;name&quot;: &quot;Bill&quot;,\n  &quot;age&quot;: 63,\n  &quot;married&quot;: True,\n  &quot;divorced&quot;: False,\n  &quot;children&quot;: (&quot;Jennifer&quot;,&quot;Rory&quot;,&quot;Phoebe&quot;),\n  &quot;pets&quot;: None,\n  &quot;cars&quot;: [\n    {&quot;model&quot;: &quot;Porsche&quot;, &quot;mpg&quot;: 38.2},\n    {&quot;model&quot;: &quot;BMW M5&quot;, &quot;mpg&quot;: 26.9}\n  ]\n}\n\nprint(json.dumps(x))\n\n#\u4e0a\u9762\u7684\u5b9e\u4f8b\u6253\u5370\u4e00\u4e2a JSON \u5b57\u7b26\u4e32\uff0c\u4f46\u5b83\u4e0d\u662f\u5f88\u5bb9\u6613\u9605\u8bfb\uff0c\u6ca1\u6709\u7f29\u8fdb\u548c\u6362\u884c\u3002\n#json.dumps() \u65b9\u6cd5\u63d0\u4f9b\u4e86\u4ee4\u7ed3\u679c\u66f4\u6613\u8bfb\u7684\u53c2\u6570\uff1a\n#\u4f7f\u7528 indent \u53c2\u6570\u5b9a\u4e49\u7f29\u8fdb\u6570\uff1a\njson.dumps(x, indent=4)\n\n#\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u503c\u4e3a\uff08&quot;, &quot;, &quot;: &quot;\uff09\uff0c\u8fd9\u610f\u5473\u7740\u4f7f\u7528\u9017\u53f7\u548c\u7a7a\u683c\u5206\u9694\u6bcf\u4e2a\u5bf9\u8c61\uff0c\u4f7f\u7528\u5192\u53f7\u548c\u7a7a\u683c\u5c06\u952e\u4e0e\u503c\u5206\u5f00\uff1a\n#\u4f7f\u7528 separators \u53c2\u6570\u6765\u66f4\u6539\u9ed8\u8ba4\u5206\u9694\u7b26\uff1a\njson.dumps(x, indent=4, separators=(&quot;. &quot;, &quot; = &quot;))\n\n#\u4f7f\u7528 sort_keys \u53c2\u6570\u6765\u6307\u5b9a\u662f\u5426\u5e94\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff1a\njson.dumps(x, indent=4, sort_keys=True)\n<\/code><\/pre>\n<h5>Python RegEx \u6b63\u5219\u8868\u8fbe\u5f0f<\/h5>\n<pre><code class=\"language-python\">#RegEx \u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5f62\u6210\u641c\u7d22\u6a21\u5f0f\u7684\u5b57\u7b26\u5e8f\u5217\u3002\n#RegEx \u53ef\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u6307\u5b9a\u7684\u641c\u7d22\u6a21\u5f0f\n#Python \u63d0\u4f9b\u540d\u4e3a re \u7684\u5185\u7f6e\u5305\uff0c\u53ef\u7528\u4e8e\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\n\n#\u68c0\u7d22\u5b57\u7b26\u4e32\u4ee5\u67e5\u770b\u5b83\u662f\u5426\u4ee5 &quot;japan&quot; \u5f00\u5934\u5e76\u4ee5 &quot;country&quot; \u7ed3\u5c3e\uff1a\nimport re\n\ntxt = &quot;japan is a great country&quot;\nx = re.search(&quot;^japan.*country$&quot;, txt)\n\nif (x):\n  print(&quot;YES! We have a match!&quot;)\nelse:\n  print(&quot;No match&quot;)\n<\/code><\/pre>\n<h6>re \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7ec4\u51fd\u6570\uff0c\u5141\u8bb8\u6211\u4eec\u68c0\u7d22\u5b57\u7b26\u4e32\u4ee5\u8fdb\u884c\u5339\u914d\uff1a<\/h6>\n<p><img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/04\/image-1714323538350.png\" alt=\"file\" \/><\/p>\n<h6>\u5143\u5b57\u7b26 \u5143\u5b57\u7b26\u662f\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\u5b57\u7b26\uff1a<\/h6>\n<p><img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/04\/image-1714323592693.png\" alt=\"file\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/04\/image-1714323635155.png\" alt=\"file\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/04\/image-1714323667744.png\" alt=\"file\" \/><\/p>\n<pre><code class=\"language-python\">#findall() \u51fd\u6570\n#findall() \u51fd\u6570\u8fd4\u56de\u5305\u542b\u6240\u6709\u5339\u914d\u9879\u7684\u5217\u8868\u3002\n#\u6253\u5370\u6240\u6709\u5339\u914d\u7684\u5217\u8868\uff1a\nimport re\n\nstr = &quot;japan is a great country&quot;\nx = re.findall(&quot;a&quot;, str)\nprint(x)\n\n#\u5982\u679c\u672a\u627e\u5230\u5339\u914d\uff0c\u5219\u8fd4\u56de\u7a7a\u5217\u8868\uff1a\nimport re\n\nstr = &quot;japan is a great country&quot;\nx = re.findall(&quot;USA&quot;, str)\nprint(x)\n\n#search() \u51fd\u6570\n#search() \u51fd\u6570\u641c\u7d22\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\uff0c\u5982\u679c\u5b58\u5728\u5339\u914d\u5219\u8fd4\u56de Match \u5bf9\u8c61\u3002\n#\u5982\u679c\u6709\u591a\u4e2a\u5339\u914d\uff0c\u5219\u4ec5\u8fd4\u56de\u9996\u4e2a\u5339\u914d\u9879\uff1a\n\n#\u5728\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u7b2c\u4e00\u4e2a\u7a7a\u767d\u5b57\u7b26\uff1a\nimport re\n\nstr = &quot;japan is a great country&quot;\nx = re.search(&quot;\\s&quot;, str)\n\nprint(&quot;The first white-space character is located in position:&quot;, x.start())\n\n#\u5982\u679c\u672a\u627e\u5230\u5339\u914d\uff0c\u5219\u8fd4\u56de\u503c None\uff1a\nimport re\n\nstr = &quot;japan is a great country&quot;\nx = re.search(&quot;USA&quot;, str)\nprint(x)\n\n#split() \u51fd\u6570\n#split() \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5b57\u7b26\u4e32\u5728\u6bcf\u6b21\u5339\u914d\u65f6\u88ab\u62c6\u5206\uff1a\n\n#\u5728\u6bcf\u4e2a\u7a7a\u767d\u5b57\u7b26\u5904\u8fdb\u884c\u62c6\u5206\uff1a\nimport re\n\nstr = &quot;China is a great country&quot;\nx = re.split(&quot;\\s&quot;, str)\nprint(x)\n\n#\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a maxsplit \u53c2\u6570\u6765\u63a7\u5236\u51fa\u73b0\u6b21\u6570\uff1a\n#\u4ec5\u5728\u9996\u6b21\u51fa\u73b0\u65f6\u62c6\u5206\u5b57\u7b26\u4e32\uff1a\nimport re\n\nstr = &quot;japan is a great country&quot;\nx = re.split(&quot;\\s&quot;, str, 1)\nprint(x)\n\n#sub() \u51fd\u6570\n#sub() \u51fd\u6570\u628a\u5339\u914d\u66ff\u6362\u4e3a\u60a8\u9009\u62e9\u7684\u6587\u672c\uff1a\nimport re\n\nstr = &quot;japan is a great country&quot;\nx = re.sub(&quot;\\s&quot;, &quot;=&quot;, str)\nprint(x)\n\n#\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a count \u53c2\u6570\u6765\u63a7\u5236\u66ff\u6362\u6b21\u6570\uff1a\n#\u7b2c4\u4e2a\u53c2\u6570\u63a7\u5236\u66ff\u6362\u6b21\u6570\nimport re\n\nstr = &quot;japan is a great country&quot;\nx = re.sub(&quot;\\s&quot;, &quot;=&quot;, str, 2)\nprint(x)\n\n#Match \u5bf9\u8c61\n#Match \u5bf9\u8c61\u662f\u5305\u542b\u6709\u5173\u641c\u7d22\u548c\u7ed3\u679c\u4fe1\u606f\u7684\u5bf9\u8c61\u3002\n#\u6ce8\u91ca\uff1a\u5982\u679c\u6ca1\u6709\u5339\u914d\uff0c\u5219\u8fd4\u56de\u503c None\uff0c\u800c\u4e0d\u662f Match \u5bf9\u8c61\u3002\n\n#\u6267\u884c\u4f1a\u8fd4\u56de Match \u5bf9\u8c61\u7684\u641c\u7d22\uff1a\nimport re\n\nstr = &quot;japan is a great country&quot;\nx = re.search(&quot;a&quot;, str)\nprint(x) # \u5c06\u6253\u5370\u4e00\u4e2a\u5bf9\u8c61\n\n#Match \u5bf9\u8c61\u63d0\u4f9b\u4e86\u7528\u4e8e\u53d6\u56de\u6709\u5173\u641c\u7d22\u53ca\u7ed3\u679c\u4fe1\u606f\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a\n#span() \u8fd4\u56de\u7684\u5143\u7ec4\u5305\u542b\u4e86\u5339\u914d\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\n#.string \u8fd4\u56de\u4f20\u5165\u51fd\u6570\u7684\u5b57\u7b26\u4e32\n#group() \u8fd4\u56de\u5339\u914d\u7684\u5b57\u7b26\u4e32\u90e8\u5206#\n\n#\u67e5\u627e\u4efb\u4f55\u4ee5J\u5f00\u5934\u7684\u5355\u8bcd\nimport re\n\nstr = &quot;Japan is a great country&quot;\nx = re.search(r&quot;\\bJ\\w+&quot;, str)\nprint(x.span()) #(0, 5)\n\n#\u6253\u5370\u5339\u914d\u7684\u5b57\u7b26\u4e32\u90e8\u5206\u3002\nimport re\n\nstr = &quot;Japan is a great country&quot;\nx = re.search(r&quot;\\bJ\\w+&quot;, str)\nprint(x.group())\n<\/code><\/pre>\n<h5>Python Try Except \u9519\u8bef\u5904\u7406<\/h5>\n<pre><code class=\"language-python\">try:\n  print(x) #\u8fd9\u91cc\u6ca1\u5b9a\u4e49 \u4f1a\u6709\u5f02\u5e38 \u4f1a\u88abtry \u6355\u83b7\nexcept:\n  print(&quot;An exception occurred&quot;)\n\n#\u591a\u4e2a\u5f02\u5e38\ntry:\n  print(x)\nexcept NameError:\n  print(&quot;Variable x is not defined&quot;)\nexcept:\n  print(&quot;Something else went wrong&quot;)\n\n#else\u5173\u5065\u5b57\n#\u5982\u679c\u6ca1\u6709\u5f02\u5e38\u624d\u4f1a\u6267\u884celse\ntry:\n  print(&quot;Hello&quot;)\nexcept:\n  print(&quot;Something went wrong&quot;)\nelse:\n  print(&quot;Nothing went wrong&quot;)\n\n#Finally\u5173\u5065\u5b57\n#\u5982\u679c\u6709finally \u5219\u65e0\u8bba try \u5757\u662f\u5426\u5f15\u53d1\u9519\u8bef\uff0c\u90fd\u4f1a\u6267\u884c finally \u5757\u3002\ntry:\n  print(x)\nexcept:\n  print(&quot;Something went wrong&quot;)\nfinally:\n  print(&quot;The &#039;try except&#039; is finished&quot;)\n\n#finally\u7528\u4f8b \u65e0\u8bba\u5982\u4f55\u90fd\u8981\u5173\u95ed\u6587\u4ef6\ntry:\n  f = open(&quot;demofile.txt&quot;)\n  f.write(&quot;Lorum Ipsum&quot;)\nexcept:\n  print(&quot;Something went wrong when writing to the file&quot;)\nfinally:\n  f.close()\n\n#\u629b\u51fa\u5f02\u5e38 raise\nx = -1\n\nif x &lt; 0:\n  raise Exception(&quot;Sorry, no numbers below zero&quot;)\n\n#\u6307\u5b9a\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\nx = &quot;hello&quot;\n\nif not type(x) is int:\n  raise TypeError(&quot;Only integers are allowed&quot;)\n<\/code><\/pre>\n<h5>Python \u547d\u4ee4\u884c\u8f93\u5165 input<\/h5>\n<pre><code class=\"language-python\">print(&quot;Enter your name:&quot;)\nx = input()\nprint(&quot;Hello &quot;, x)\n<\/code><\/pre>\n<h5>Python \u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h5>\n<pre><code class=\"language-python\">#\u5b57\u7b26\u4e32 format()\n#format() \u65b9\u6cd5\u5141\u8bb8\u60a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u9009\u5b9a\u90e8\u5206\u3002\nprice = 52\ntxt = &quot;The price is {} dollars&quot;\nprint(txt.format(price))\n\n#\u5c06\u4ef7\u683c\u683c\u5f0f\u5316\u4e3a\u5e26\u6709\u4e24\u4f4d\u5c0f\u6570\u7684\u6570\u5b57\uff1a\ntxt = &quot;The price is {:.2f} dollars&quot;\n\n#\u9700\u4f7f\u7528\u66f4\u591a\u503c\uff0c\u53ea\u9700\u5411 format() \u65b9\u6cd5\u6dfb\u52a0\u66f4\u591a\u503c\uff1a\nquantity = 3\nitemno = 567\nprice = 52\nmyorder = &quot;I want {} pieces of item number {} for {:.2f} dollars.&quot;\nprint(myorder.format(quantity, itemno, price))\n\n#\u7d22\u5f15\u53f7\n#\u60a8\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u53f7\uff08\u82b1\u62ec\u53f7 {0} \u5185\u7684\u6570\u5b57\uff09\u6765\u786e\u4fdd\u5c06\u503c\u653e\u5728\u6b63\u786e\u7684\u5360\u4f4d\u7b26\u4e2d\nquantity = 3\nitemno = 567\nprice = 52\nmyorder = &quot;I want {0} pieces of item number {1} for {2:.2f} dollars.&quot;\nprint(myorder.format(quantity, itemno, price))\n\n#\u5982\u679c\u8981\u591a\u6b21\u5f15\u7528\u76f8\u540c\u7684\u503c\uff0c\u8bf7\u4f7f\u7528\u7d22\u5f15\u53f7\uff1a\nage = 63\nname = &quot;Bill&quot;\ntxt = &quot;His name is {1}. {1} is {0} years old.&quot;\nprint(txt.format(age, name))\n\n#\u547d\u540d\u7d22\u5f15\n#\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5728\u82b1\u62ec\u53f7 {carname} \u4e2d\u8f93\u5165\u540d\u79f0\u6765\u4f7f\u7528\u547d\u540d\u7d22\u5f15\uff0c\u4f46\u662f\u5728\u4f20\u9012\u53c2\u6570\u503c txt.format(carname = &quot;Ford&quot;) \u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u540d\u79f0\nmyorder = &quot;I have a {carname}, it is a {model}.&quot;\nprint(myorder.format(carname = &quot;Porsche&quot;, model = &quot;911&quot;))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#Python \u8fed\u4ee3\u5668 #\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u503c\u7684\u53ef\u8ba1\u6570\u6570\u5b57\u3002 #\u5728 Python \u4e2d\uff0c\u8fed\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-320","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=320"}],"version-history":[{"count":9,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/320\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}