{"id":40,"date":"2026-06-13T16:15:42","date_gmt":"2026-06-13T08:15:42","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=40"},"modified":"2026-06-13T16:15:42","modified_gmt":"2026-06-13T08:15:42","slug":"python3-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/13\/python3-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/","title":{"rendered":"Python3 \u6570\u636e\u7c7b\u578b\u8f6c\u6362"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 Python 3 \u4e2d\uff0c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5206\u4e3a<strong>\u9690\u5f0f\u8f6c\u6362<\/strong>\uff08Python \u81ea\u52a8\u5b8c\u6210\uff09\u548c<strong>\u663e\u5f0f\u8f6c\u6362<\/strong>\uff08\u5f00\u53d1\u8005\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5f3a\u5236\u8f6c\u6362\uff09\u3002\u7531\u4e8e Python \u662f\u5f3a\u7c7b\u578b\u8bed\u8a00\uff0c\u5b83\u4e0d\u4f1a\u50cf JavaScript \u90a3\u6837\u968f\u610f\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\uff08\u4f8b\u5982\u4e0d\u4f1a\u628a\u5b57\u7b26\u4e32 <code>\"1\"<\/code> \u548c\u6570\u5b57 <code>1<\/code> \u81ea\u52a8\u76f8\u52a0\uff09\uff0c\u56e0\u6b64\u638c\u63e1\u663e\u5f0f\u8f6c\u6362\u975e\u5e38\u91cd\u8981\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f Python 3 \u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u5168\u9762\u6307\u5357\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6570\u5b57\u4e0e\u5b57\u7b26\u4e32\u7684\u8f6c\u6362<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u8f6c\u6362\u573a\u666f\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># 1. \u6570\u5b57 -&gt; \u5b57\u7b26\u4e32 (str)\nnum = 100\nprint(str(num) + &quot; apples&quot;)  # \u8f93\u51fa: &quot;100 apples&quot;\nprint(str(3.14))             # \u8f93\u51fa: &quot;3.14&quot;\n\n# 2. \u5b57\u7b26\u4e32 -&gt; \u6574\u6570 (int)\nprint(int(&quot;42&quot;))             # \u8f93\u51fa: 42\nprint(int(&quot;-10&quot;))            # \u8f93\u51fa: -10\n\n# 3. \u5b57\u7b26\u4e32 -&gt; \u6d6e\u70b9\u6570 (float)\nprint(float(&quot;3.14&quot;))         # \u8f93\u51fa: 3.14\nprint(float(&quot;1e3&quot;))          # \u8f93\u51fa: 1000.0 (\u652f\u6301\u79d1\u5b66\u8ba1\u6570\u6cd5)\n\n# \u26a0\ufe0f \u5e38\u89c1\u9677\u9631\uff1a\u4e0d\u80fd\u76f4\u63a5\u5c06\u5305\u542b\u5c0f\u6570\u70b9\u7684\u5b57\u7b26\u4e32\u8f6c\u4e3a int\n# int(&quot;3.14&quot;)  # \u4f1a\u62a5\u9519: ValueError: invalid literal for int() with base 10: &#039;3.14&#039;\n# \u6b63\u786e\u505a\u6cd5\uff1a\u5148\u8f6c float\uff0c\u518d\u8f6c int\nprint(int(float(&quot;3.14&quot;)))    # \u8f93\u51fa: 3 (\u6ce8\u610f\uff1a\u662f\u5411\u4e0b\u622a\u65ad\uff0c\u4e0d\u662f\u56db\u820d\u4e94\u5165)<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6570\u5b57\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># 1. \u6d6e\u70b9\u6570 -&gt; \u6574\u6570 (int)\n# \u6ce8\u610f\uff1aint() \u8f6c\u6362\u6d6e\u70b9\u6570\u65f6\u662f\u76f4\u63a5\u201c\u622a\u65ad\u201d\u5c0f\u6570\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u56db\u820d\u4e94\u5165\nprint(int(3.9))   # \u8f93\u51fa: 3\nprint(int(-3.9))  # \u8f93\u51fa: -3\n# \u5982\u679c\u9700\u8981\u56db\u820d\u4e94\u5165\uff0c\u8bf7\u4f7f\u7528 round()\nprint(round(3.9)) # \u8f93\u51fa: 4\n\n# 2. \u6574\u6570 -&gt; \u6d6e\u70b9\u6570 (float)\nprint(float(5))   # \u8f93\u51fa: 5.0\n\n# 3. \u6570\u5b57 -&gt; \u5e03\u5c14\u503c (bool)\n# 0 \u548c 0.0 \u8f6c\u6362\u4e3a False\uff0c\u5176\u4ed6\u6240\u6709\u6570\u5b57\u8f6c\u6362\u4e3a True\nprint(bool(0))    # False\nprint(bool(1))    # True\nprint(bool(-5))   # True<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5bb9\u5668\uff08\u5e8f\u5217\uff09\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5217\u8868 (<code>list<\/code>)\u3001\u5143\u7ec4 (<code>tuple<\/code>) \u548c\u96c6\u5408 (<code>set<\/code>) \u4e4b\u95f4\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u3002\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u88ab\u89c6\u4e3a\u5b57\u7b26\u7684\u5e8f\u5217\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><span>24<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># 1. \u5b57\u7b26\u4e32 -&gt; \u5217\u8868\/\u5143\u7ec4\/\u96c6\u5408\ntext = &quot;hello&quot;\nprint(list(text))   # [&#039;h&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;]\nprint(tuple(text))  # (&#039;h&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;)\nprint(set(text))    # {&#039;h&#039;, &#039;e&#039;, &#039;l&#039;, &#039;o&#039;} (\u81ea\u52a8\u53bb\u91cd\u4e14\u65e0\u5e8f)\n\n# 2. \u5217\u8868 &lt;-&gt; \u5143\u7ec4 &lt;-&gt; \u96c6\u5408\nmy_list = [1, 2, 2, 3]\nmy_tuple = tuple(my_list)  # (1, 2, 2, 3)\nmy_set = set(my_list)      # {1, 2, 3} (\u5e38\u7528\u4e8e\u5217\u8868\u5feb\u901f\u53bb\u91cd)\nback_to_list = list(my_set)# [1, 2, 3]\n\n# 3. \u8f6c\u6362\u4e3a\u5b57\u5178 (dict)\n# \u65b9\u6cd5 A\uff1a\u5305\u542b\u53cc\u5143\u7d20\u5143\u7ec4\/\u5217\u8868\u7684\u5e8f\u5217\npairs = [(&quot;a&quot;, 1), (&quot;b&quot;, 2)]\nprint(dict(pairs))  # {&#039;a&#039;: 1, &#039;b&#039;: 2}\n\n# \u65b9\u6cd5 B\uff1a\u4f7f\u7528 zip \u51fd\u6570\u5408\u5e76\u4e24\u4e2a\u5217\u8868\nkeys = [&quot;name&quot;, &quot;age&quot;]\nvalues = [&quot;Alice&quot;, 25]\nprint(dict(zip(keys, values)))  # {&#039;name&#039;: &#039;Alice&#039;, &#039;age&#039;: 25}\n\n# \u65b9\u6cd5 C\uff1a\u5173\u952e\u5b57\u53c2\u6570 (\u952e\u5fc5\u987b\u662f\u5408\u6cd5\u7684\u6807\u8bc6\u7b26\/\u5b57\u7b26\u4e32)\nprint(dict(name=&quot;Bob&quot;, age=30)) # {&#039;name&#039;: &#039;Bob&#039;, &#039;age&#039;: 30}<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5e03\u5c14\u503c\u8f6c\u6362 (Truth Value Testing) \ud83c\udf1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\uff0c\u4efb\u4f55\u5bf9\u8c61\u90fd\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002\u7406\u89e3\u54ea\u4e9b\u503c\u88ab\u89c6\u4e3a <code>False<\/code> \u662f\u5199\u51fa &#8220;Pythonic&#8221;\uff08\u5730\u9053\uff09\u4ee3\u7801\u7684\u5173\u952e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4ee5\u4e0b\u6240\u6709\u503c\u8f6c\u6362\u4e3a <code>bool<\/code> \u65f6\u5747\u4e3a <code>False<\/code>\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e38\u91cf\uff1a<code>None<\/code>, <code>False<\/code><\/li>\n\n\n\n<li>\u6570\u5b57\u96f6\uff1a<code>0<\/code>, <code>0.0<\/code>, <code>0j<\/code> (\u590d\u6570\u96f6)<\/li>\n\n\n\n<li>\u7a7a\u5e8f\u5217\/\u96c6\u5408\uff1a<code>\"\"<\/code> (\u7a7a\u5b57\u7b26\u4e32), <code>[]<\/code> (\u7a7a\u5217\u8868), <code>()<\/code> (\u7a7a\u5143\u7ec4), <code>{}<\/code> (\u7a7a\u5b57\u5178), <code>set()<\/code> (\u7a7a\u96c6\u5408)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9664\u6b64\u4e4b\u5916\u7684\u6240\u6709\u503c\uff0c\u8f6c\u6362\u4e3a <code>bool<\/code> \u5747\u4e3a <code>True<\/code>\u3002<\/strong><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u5b9e\u9645\u5e94\u7528\uff1a\u5728 if \u8bed\u53e5\u4e2d\u76f4\u63a5\u5224\u65ad\uff0c\u800c\u4e0d\u662f\u5199 if len(my_list) &gt; 0:\nmy_list = []\nif not my_list:  # \u7a7a\u5217\u8868\u88ab\u89c6\u4e3a False\uff0cnot False \u5373\u4e3a True\n    print(&quot;\u5217\u8868\u4e3a\u7a7a&quot;)\n\nname = &quot;&quot;\nif name:\n    print(&quot;\u540d\u5b57\u4e0d\u4e3a\u7a7a&quot;)\nelse:\n    print(&quot;\u540d\u5b57\u4e3a\u7a7a&quot;) # \u4f1a\u6267\u884c\u8fd9\u4e00\u884c<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u5b57\u7b26\u4e32\u4e0e\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u8f6c\u6362 (\u8fdb\u9636)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5f62\u5982 <code>\"[1, 2, 3]\"<\/code> \u6216 <code>\"{'a': 1}\"<\/code> \u7684<strong>\u5b57\u7b26\u4e32<\/strong>\u8f6c\u6362\u56de\u771f\u5b9e\u7684\u5217\u8868\u6216\u5b57\u5178\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">import json\nimport ast\n\njson_str = &#039;[&quot;apple&quot;, &quot;banana&quot;]&#039;\ndict_str = &quot;{&#039;name&#039;: &#039;Alice&#039;, &#039;age&#039;: 25}&quot; # \u6ce8\u610f\uff1a\u6807\u51c6 JSON \u8981\u6c42\u53cc\u5f15\u53f7\uff0c\u8fd9\u662f Python \u5b57\u5178\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\n\n# \u274c \u5371\u9669\u505a\u6cd5\uff1a\u4f7f\u7528 eval()\n# eval(dict_str)  # \u867d\u7136\u80fd\u5de5\u4f5c\uff0c\u4f46\u5982\u679c\u5b57\u7b26\u4e32\u6765\u81ea\u7528\u6237\u8f93\u5165\uff0c\u4f1a\u5bfc\u81f4\u4e25\u91cd\u7684\u5b89\u5168\u6f0f\u6d1e (\u4ee3\u7801\u6ce8\u5165)\n\n# \u2705 \u63a8\u8350\u505a\u6cd5 1\uff1a\u4f7f\u7528 ast.literal_eval (\u5b89\u5168\u5730\u8bc4\u4f30 Python \u5b57\u9762\u91cf)\nsafe_dict = ast.literal_eval(dict_str)\nprint(type(safe_dict))  # &lt;class &#039;dict&#039;&gt;\nprint(safe_dict[&#039;name&#039;]) # &#039;Alice&#039;\n\n# \u2705 \u63a8\u8350\u505a\u6cd5 2\uff1a\u4f7f\u7528 json \u6a21\u5757 (\u9002\u7528\u4e8e\u6807\u51c6 JSON \u683c\u5f0f\u5b57\u7b26\u4e32)\n# \u6ce8\u610f\uff1aJSON \u5b57\u7b26\u4e32\u4e2d\u7684\u952e\u548c\u5b57\u7b26\u4e32\u503c\u5fc5\u987b\u4f7f\u7528\u53cc\u5f15\u53f7 &quot; &quot;\nvalid_json_str = &#039;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}&#039;\ndata = json.loads(valid_json_str)\nprint(type(data))       # &lt;class &#039;dict&#039;&gt;<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 (\u81ea\u52a8\u8f6c\u6362)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u6570\u5b66\u8fd0\u7b97\u4e2d\uff0cPython \u4f1a\u81ea\u52a8\u5c06\u201c\u8f83\u4f4e\u201d\u7684\u7c7b\u578b\u63d0\u5347\u4e3a\u201c\u8f83\u9ad8\u201d\u7684\u7c7b\u578b\uff0c\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u3002<br>\u4f18\u5148\u7ea7\u5927\u81f4\u4e3a\uff1a<code>bool<\/code> &lt; <code>int<\/code> &lt; <code>float<\/code> &lt; <code>complex<\/code><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># int \u548c float \u8fd0\u7b97\uff0cint \u81ea\u52a8\u8f6c\u4e3a float\nresult = 5 + 2.5\nprint(result)      # 7.5\nprint(type(result))# &lt;class &#039;float&#039;&gt;\n\n# bool \u53c2\u4e0e\u6570\u5b66\u8fd0\u7b97\u65f6\uff0cTrue=1, False=0\nprint(True + 3)    # 4<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u6838\u5fc3\u907f\u5751\u603b\u7ed3<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>int(\"3.14\")<\/code> \u4f1a\u62a5\u9519<\/strong>\uff1a\u5fc5\u987b\u5148\u7528 <code>float(\"3.14\")<\/code> \u8f6c\u6362\uff0c\u518d\u7528 <code>int()<\/code> \u622a\u65ad\u3002<\/li>\n\n\n\n<li><strong><code>int()<\/code> \u662f\u622a\u65ad\u4e0d\u662f\u56db\u820d\u4e94\u5165<\/strong>\uff1a<code>int(3.99)<\/code> \u7ed3\u679c\u662f <code>3<\/code>\u3002\u9700\u8981\u56db\u820d\u4e94\u5165\u8bf7\u7528 <code>round(3.99)<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u7a7a\u5b57\u5178\u7684\u8868\u793a<\/strong>\uff1a<code>{}<\/code> \u521b\u5efa\u7684\u662f\u7a7a\u5b57\u5178\uff0c\u5982\u679c\u8981\u521b\u5efa\u7a7a\u96c6\u5408\uff0c\u5fc5\u987b\u4f7f\u7528 <code>set()<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>list()<\/code> \u8f6c\u6362\u5b57\u7b26\u4e32<\/strong>\uff1a<code>list(\"abc\")<\/code> \u4f1a\u53d8\u6210 <code>['a', 'b', 'c']<\/code>\u3002\u5982\u679c\u4f60\u60f3\u628a\u6574\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u5217\u8868\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u5e94\u8be5\u5199\u6210 <code>[\"abc\"]<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5bf9\u67d0\u79cd\u7279\u5b9a\u7684\u8f6c\u6362\u573a\u666f\uff08\u4f8b\u5982\uff1a\u65f6\u95f4\u5b57\u7b26\u4e32\u8f6c datetime \u5bf9\u8c61\uff0c\u6216\u8005\u5b57\u8282 bytes \u4e0e\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\uff09\u6709\u7591\u95ee\uff0c\u53ef\u4ee5\u968f\u65f6\u544a\u8bc9\u6211\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python 3 \u4e2d\uff0c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5206\u4e3a\u9690\u5f0f\u8f6c\u6362\uff08Pyth&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":41,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions\/41"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}