{"id":1002356,"date":"2024-12-27T10:07:59","date_gmt":"2024-12-27T02:07:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1002356.html"},"modified":"2024-12-27T10:08:02","modified_gmt":"2024-12-27T02:08:02","slug":"python%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e7%9c%9f%e5%81%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1002356.html","title":{"rendered":"python\u8bed\u8a00\u5982\u4f55\u5224\u65ad\u771f\u5047"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080143\/eec849af-28fe-4e5f-b41f-50f89d442209.webp\" alt=\"python\u8bed\u8a00\u5982\u4f55\u5224\u65ad\u771f\u5047\" \/><\/p>\n<p><p> <strong>Python\u8bed\u8a00\u4e2d\u5224\u65ad\u771f\u5047\u4e3b\u8981\u901a\u8fc7\u5e03\u5c14\u503c\u3001\u6761\u4ef6\u8bed\u53e5\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\u3002\u5e03\u5c14\u503c\u662fPython\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u53ea\u6709True\u548cFalse\u4e24\u79cd\u503c\u3001\u6761\u4ef6\u8bed\u53e5\u901a\u8fc7if\u3001elif\u3001else\u7ed3\u6784\u5224\u65ad\u6761\u4ef6\u7684\u771f\u5047\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\u5305\u62ecand\u3001or\u3001not\uff0c\u7528\u4e8e\u7ec4\u5408\u6761\u4ef6\u3002<\/strong>\u5728Python\u4e2d\uff0c\u4efb\u4f55\u5bf9\u8c61\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u5e03\u5c14\u503c\u8fdb\u884c\u8bc4\u4f30\uff0c\u5176\u4e2d\u975e\u96f6\u6570\u503c\u3001\u975e\u7a7a\u5e8f\u5217\u6216\u6620\u5c04\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u7b49\uff09\u90fd\u88ab\u89c6\u4e3aTrue\uff0c\u800c\u96f6\u503c\u3001None\u3001\u7a7a\u5e8f\u5217\u6216\u6620\u5c04\u5219\u88ab\u89c6\u4e3aFalse\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5b9e\u73b0\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u548c\u903b\u8f91\u63a7\u5236\uff0c\u786e\u4fdd\u7a0b\u5e8f\u6309\u7167\u9884\u671f\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5e03\u5c14\u503c\u4e0e\u57fa\u672c\u5224\u65ad<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5e03\u5c14\u503c\u662f\u5224\u65ad\u771f\u5047\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u3002\u5e03\u5c14\u7c7b\u578b\u53ea\u6709\u4e24\u4e2a\u503c\uff1aTrue\u548cFalse\u3002\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0cPython\u4f1a\u81ea\u52a8\u5c06\u5404\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u8fdb\u884c\u5224\u65ad\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570<code>bool()<\/code>\u6765\u67e5\u770b\u4e00\u4e2a\u5bf9\u8c61\u7684\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(bool(0))  # False<\/p>\n<p>print(bool(1))  # True<\/p>\n<p>print(bool([]))  # False<\/p>\n<p>print(bool([1, 2, 3]))  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u6570\u5b570\u548c\u7a7a\u5217\u8868\u88ab\u8bc4\u4f30\u4e3aFalse\uff0c\u800c\u975e\u96f6\u6570\u503c\u548c\u975e\u7a7a\u5217\u8868\u5219\u4e3aTrue\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6761\u4ef6\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u6761\u4ef6\u8bed\u53e5\u662f\u7a0b\u5e8f\u63a7\u5236\u6d41\u7684\u6838\u5fc3\uff0c\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002Python\u4e2d\u7684\u6761\u4ef6\u8bed\u53e5\u5305\u62ec<code>if<\/code>\u3001<code>elif<\/code>\u548c<code>else<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>if x &gt; 5:<\/p>\n<p>    print(&quot;x is greater than 5&quot;)<\/p>\n<p>elif x == 5:<\/p>\n<p>    print(&quot;x is 5&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;x is less than 5&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u53d8\u91cfx\u7684\u503c\u5927\u4e8e5\uff0c\u5219\u7a0b\u5e8f\u8f93\u51fa\u201cx is greater than 5\u201d\uff1b\u5982\u679cx\u7b49\u4e8e5\uff0c\u5219\u8f93\u51fa\u201cx is 5\u201d\uff1b\u5426\u5219\uff0c\u8f93\u51fa\u201cx is less than 5\u201d\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u903b\u8f91\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>Python\u4e2d\u6709\u4e09\u4e2a\u4e3b\u8981\u7684\u903b\u8f91\u8fd0\u7b97\u7b26\uff1a<code>and<\/code>\u3001<code>or<\/code>\u548c<code>not<\/code>\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7528\u4e8e\u8fde\u63a5\u591a\u4e2a\u6761\u4ef6\uff0c\u5f62\u6210\u66f4\u590d\u6742\u7684\u5224\u65ad\u903b\u8f91\u3002<\/p>\n<\/p>\n<ul>\n<li><strong><code>and<\/code><\/strong>\uff1a\u5f53\u4e14\u4ec5\u5f53\u6240\u6709\u6761\u4ef6\u90fd\u4e3aTrue\u65f6\uff0c\u7ed3\u679c\u624d\u4e3aTrue\u3002<\/li>\n<li><strong><code>or<\/code><\/strong>\uff1a\u53ea\u8981\u6709\u4e00\u4e2a\u6761\u4ef6\u4e3aTrue\uff0c\u7ed3\u679c\u5c31\u4e3aTrue\u3002<\/li>\n<li><strong><code>not<\/code><\/strong>\uff1a\u7528\u4e8e\u5bf9\u6761\u4ef6\u7ed3\u679c\u53d6\u53cd\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">a = True<\/p>\n<p>b = False<\/p>\n<p>print(a and b)  # False<\/p>\n<p>print(a or b)  # True<\/p>\n<p>print(not a)  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>has_license = True<\/p>\n<p>if age &gt;= 18 and has_license:<\/p>\n<p>    print(&quot;You can drive.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;You cannot drive.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\u7528\u4e8e\u5224\u65ad\u4e24\u4e2a\u503c\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<ul>\n<li><strong><code>==<\/code><\/strong>\uff1a\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\u3002<\/li>\n<li><strong><code>!=<\/code><\/strong>\uff1a\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u4e0d\u76f8\u7b49\u3002<\/li>\n<li><strong><code>&gt;<\/code><\/strong>\u3001<code>&lt;<\/code><strong>\u3001<code>&gt;=<\/code><\/strong>\u3001<code>&lt;=<\/code>\uff1a\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u503c\u7684\u5927\u5c0f\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = 20<\/p>\n<p>print(x == y)  # False<\/p>\n<p>print(x != y)  # True<\/p>\n<p>print(x &gt; y)  # False<\/p>\n<p>print(x &lt;= y)  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6210\u5458\u8fd0\u7b97\u7b26\u4e0e\u8eab\u4efd\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>Python\u4e2d\u8fd8\u6709\u6210\u5458\u8fd0\u7b97\u7b26\u548c\u8eab\u4efd\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u7279\u6b8a\u7684\u5224\u65ad\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6210\u5458\u8fd0\u7b97\u7b26<\/strong>\uff1a<code>in<\/code>\u548c<code>not in<\/code>\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u4e2d\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>print(&quot;banana&quot; in fruits)  # True<\/p>\n<p>print(&quot;grape&quot; not in fruits)  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u8eab\u4efd\u8fd0\u7b97\u7b26<\/strong>\uff1a<code>is<\/code>\u548c<code>is not<\/code>\u7528\u4e8e\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u4e3a\u540c\u4e00\u4e2a\u5bf9\u8c61\uff08\u5373\u662f\u5426\u6307\u5411\u540c\u4e00\u5185\u5b58\u5730\u5740\uff09\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">x = [1, 2, 3]<\/p>\n<p>y = x<\/p>\n<p>z = x[:]<\/p>\n<p>print(x is y)  # True<\/p>\n<p>print(x is z)  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u6765\u8fdb\u884c\u771f\u5047\u5224\u65ad\u548c\u903b\u8f91\u63a7\u5236\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u5458\u80fd\u591f\u7f16\u5199\u51fa\u529f\u80fd\u5f3a\u5927\u4e14\u903b\u8f91\u6e05\u6670\u7684\u4ee3\u7801\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u8fd8\u662f\u590d\u6742\u7684\u903b\u8f91\u63a7\u5236\uff0cPython\u90fd\u80fd\u80dc\u4efb\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u7406\u89e3\u8fd9\u4e9b\u57fa\u672c\u6982\u5ff5\u548c\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u7a0b\u5e8f\u5458\u66f4\u597d\u5730\u5b9e\u73b0\u7a0b\u5e8f\u7684\u903b\u8f91\u63a7\u5236\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u771f\u5047\u503c\u7684\u6bd4\u8f83\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5224\u65ad\u771f\u5047\u503c\u901a\u5e38\u4f9d\u8d56\u4e8e\u5e03\u5c14\u503cTrue\u548cFalse\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\uff08\u5982if\u8bed\u53e5\uff09\u6765\u8fdb\u884c\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u8868\u8fbe\u5f0f<code>x &gt; 10<\/code>\u6765\u5224\u65ad\u53d8\u91cfx\u662f\u5426\u5927\u4e8e10\uff0c\u5982\u679c\u6761\u4ef6\u6210\u7acb\uff0c\u5c06\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002\u6b64\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5185\u7f6e\u51fd\u6570\u5982<code>bool()<\/code>\uff0c\u53ef\u4ee5\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u975e\u7a7a\u5b57\u7b26\u4e32\u3001\u975e\u96f6\u6570\u5b57\u548c\u975e\u7a7a\u96c6\u5408\u90fd\u8fd4\u56deTrue\uff0c\u5176\u4ed6\u5219\u8fd4\u56deFalse\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u6570\u636e\u7c7b\u578b\u4f1a\u88ab\u89c6\u4e3a\u5047\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4ee5\u4e0b\u6570\u636e\u7c7b\u578b\u901a\u5e38\u88ab\u89c6\u4e3a\u5047\u503c\uff1a\u7a7a\u5b57\u7b26\u4e32\uff08&#39;&#39;\uff09\u3001\u96f6\uff080\uff09\u3001\u7a7a\u5217\u8868\uff08[]\uff09\u3001\u7a7a\u5143\u7ec4\uff08()\uff09\u3001\u7a7a\u5b57\u5178\uff08{}\uff09\u3001\u7a7a\u96c6\u5408\uff08set()\uff09\u548cNone\u5bf9\u8c61\u3002\u8fd9\u4e9b\u7c7b\u578b\u5728\u5e03\u5c14\u4e0a\u4e0b\u6587\u4e2d\u4f1a\u88ab\u89e3\u91ca\u4e3aFalse\uff0c\u4efb\u4f55\u975e\u7a7a\u6216\u975e\u96f6\u7684\u503c\u90fd\u4f1a\u88ab\u89c6\u4e3aTrue\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u771f\u5047\u5224\u65ad\u903b\u8f91\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u5e76\u5b9e\u73b0<code>__bool__()<\/code>\u6216<code>__len__()<\/code>\u65b9\u6cd5\u6765\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u771f\u5047\u5224\u65ad\u903b\u8f91\u3002\u5f53\u8c03\u7528<code>bool()<\/code>\u51fd\u6570\u6216\u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65f6\uff0cPython\u4f1a\u81ea\u52a8\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u51b3\u5b9a\u5bf9\u8c61\u7684\u5e03\u5c14\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>__len__()<\/code>\u8fd4\u56de0\uff0c\u5219\u8be5\u5bf9\u8c61\u4f1a\u88ab\u89c6\u4e3aFalse\uff1b\u5982\u679c\u8fd4\u56de\u975e\u96f6\u503c\uff0c\u5219\u88ab\u89c6\u4e3aTrue\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4e3a\u81ea\u5df1\u7684\u7c7b\u5b9a\u4e49\u771f\u5047\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bed\u8a00\u4e2d\u5224\u65ad\u771f\u5047\u4e3b\u8981\u901a\u8fc7\u5e03\u5c14\u503c\u3001\u6761\u4ef6\u8bed\u53e5\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\u3002\u5e03\u5c14\u503c\u662fPython\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u53ea [&hellip;]","protected":false},"author":3,"featured_media":1002359,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002356"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1002356"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002356\/revisions"}],"predecessor-version":[{"id":1002362,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002356\/revisions\/1002362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1002359"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1002356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1002356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1002356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}