{"id":1174742,"date":"2025-01-15T17:22:34","date_gmt":"2025-01-15T09:22:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174742.html"},"modified":"2025-01-15T17:22:38","modified_gmt":"2025-01-15T09:22:38","slug":"python%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e5%88%a4%e6%96%ad%e5%a4%9a%e4%b8%aa%e6%9d%a1%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174742.html","title":{"rendered":"python\u5982\u4f55\u540c\u65f6\u5224\u65ad\u591a\u4e2a\u6761\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110352\/e6041077-3d85-490a-bf7b-3a6c14e01f8b.webp\" alt=\"python\u5982\u4f55\u540c\u65f6\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u540c\u65f6\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\u53ef\u4ee5\u4f7f\u7528&#39;and&#39;\u3001&#39;or&#39;\u548c&#39;not&#39;\u5173\u952e\u5b57\u3002<\/strong> \u8fd9\u4e9b\u5173\u952e\u5b57\u7528\u6765\u5c06\u591a\u4e2a\u6761\u4ef6\u7ed3\u5408\u5728\u4e00\u8d77\u8fdb\u884c\u5224\u65ad\u3002\u4f8b\u5982\uff0c&#39;and&#39;\u8868\u793a\u4e24\u4e2a\u6761\u4ef6\u90fd\u8981\u6ee1\u8db3\uff0c&#39;or&#39;\u8868\u793a\u4efb\u610f\u4e00\u4e2a\u6761\u4ef6\u6ee1\u8db3\u5373\u53ef\uff0c&#39;not&#39;\u8868\u793a\u6761\u4ef6\u53d6\u53cd\u3002<strong>\u4f7f\u7528\u8fd9\u4e9b\u5173\u952e\u5b57\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u903b\u8f91\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/strong> \u5177\u4f53\u6765\u8bf4\uff0c&#39;and&#39;\u5173\u952e\u5b57\u975e\u5e38\u5e38\u7528\uff0c\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u5197\u4f59\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>income = 50000<\/p>\n<p>if age &gt; 18 and income &gt; 30000:<\/p>\n<p>    print(&quot;Eligible for loan&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u6709\u5f53<code>age<\/code>\u5927\u4e8e18\u5e76\u4e14<code>income<\/code>\u5927\u4e8e30000\u65f6\uff0c\u624d\u4f1a\u8f93\u51fa\u201cEligible for loan\u201d\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u4e2a\u5173\u952e\u5b57\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001AND\u5173\u952e\u5b57\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>&#39;and&#39;\u5173\u952e\u5b57\u7528\u4e8e\u540c\u65f6\u6ee1\u8db3\u591a\u4e2a\u6761\u4ef6\u65f6\u3002\u6240\u6709\u6761\u4ef6\u90fd\u5fc5\u987b\u4e3a\u771f\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u624d\u4e3a\u771f\u3002\u5426\u5219\uff0c\u53ea\u8981\u6709\u4e00\u4e2a\u6761\u4ef6\u4e3a\u5047\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u5c31\u4e3a\u5047\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 20<\/p>\n<p>c = 30<\/p>\n<p>if a &lt; b and b &lt; c:<\/p>\n<p>    print(&quot;Both conditions are True&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;One or both conditions are False&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>a &lt; b<\/code>\u548c<code>b &lt; c<\/code>\u4e24\u4e2a\u6761\u4ef6\u90fd\u4e3a\u771f\uff0c\u6240\u4ee5\u4f1a\u8f93\u51fa\u201cBoth conditions are True\u201d\u3002<\/p>\n<\/p>\n<p><h4>2. \u590d\u6742\u6761\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5224\u65ad\u6761\u4ef6\u5f80\u5f80\u4f1a\u6bd4\u8f83\u590d\u6742\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u5224\u65ad\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528&#39;and&#39;\u5173\u952e\u5b57\u5c06\u591a\u4e2a\u6761\u4ef6\u7ec4\u5408\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 30<\/p>\n<p>income = 60000<\/p>\n<p>credit_score = 700<\/p>\n<p>if age &gt; 18 and income &gt; 40000 and credit_score &gt; 650:<\/p>\n<p>    print(&quot;Eligible for premium loan&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Not eligible for premium loan&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u6709\u5f53<code>age<\/code>\u5927\u4e8e18\uff0c<code>income<\/code>\u5927\u4e8e40000\uff0c\u5e76\u4e14<code>credit_score<\/code>\u5927\u4e8e650\u65f6\uff0c\u624d\u4f1a\u8f93\u51fa\u201cEligible for premium loan\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001OR\u5173\u952e\u5b57\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>&#39;or&#39;\u5173\u952e\u5b57\u7528\u4e8e\u6ee1\u8db3\u4efb\u4e00\u6761\u4ef6\u5373\u53ef\u3002\u53ea\u8981\u6709\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u5c31\u4e3a\u771f\u3002\u53ea\u6709\u5f53\u6240\u6709\u6761\u4ef6\u90fd\u4e3a\u5047\u65f6\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u624d\u4e3a\u5047\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 20<\/p>\n<p>c = 5<\/p>\n<p>if a &gt; b or c &lt; b:<\/p>\n<p>    print(&quot;At least one condition is True&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Both conditions are False&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>a &gt; b<\/code>\u4e3a\u5047\uff0c\u4f46<code>c &lt; b<\/code>\u4e3a\u771f\uff0c\u6240\u4ee5\u4f1a\u8f93\u51fa\u201cAt least one condition is True\u201d\u3002<\/p>\n<\/p>\n<p><h4>2. \u590d\u6742\u6761\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5224\u65ad\u6761\u4ef6\u53ef\u80fd\u4f1a\u6bd4\u8f83\u590d\u6742\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528&#39;or&#39;\u5173\u952e\u5b57\u5c06\u591a\u4e2a\u6761\u4ef6\u7ec4\u5408\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">temperature = 35<\/p>\n<p>humidity = 85<\/p>\n<p>weather = &quot;sunny&quot;<\/p>\n<p>if temperature &gt; 30 or humidity &gt; 80 or weather == &quot;sunny&quot;:<\/p>\n<p>    print(&quot;It might be a hot day&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The weather is moderate&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u8981<code>temperature<\/code>\u5927\u4e8e30\uff0c\u6216<code>humidity<\/code>\u5927\u4e8e80\uff0c\u6216<code>weather<\/code>\u7b49\u4e8e&quot;sunny&quot;\u4e2d\u7684\u4efb\u4e00\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u4f1a\u8f93\u51fa\u201cIt might be a hot day\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001NOT\u5173\u952e\u5b57\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>&#39;not&#39;\u5173\u952e\u5b57\u7528\u4e8e\u53d6\u53cd\u3002\u5373\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u6574\u4e2a\u8868\u8fbe\u5f0f\u4e3a\u5047\uff1b\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u6574\u4e2a\u8868\u8fbe\u5f0f\u4e3a\u771f\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 20<\/p>\n<p>if not a &gt; b:<\/p>\n<p>    print(&quot;a is not greater than b&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;a is greater than b&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>a &gt; b<\/code>\u4e3a\u5047\uff0c\u53d6\u53cd\u540e\u4e3a\u771f\uff0c\u6240\u4ee5\u4f1a\u8f93\u51fa\u201ca is not greater than b\u201d\u3002<\/p>\n<\/p>\n<p><h4>2. \u590d\u6742\u6761\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5224\u65ad\u6761\u4ef6\u53ef\u80fd\u4f1a\u6bd4\u8f83\u590d\u6742\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528&#39;not&#39;\u5173\u952e\u5b57\u5c06\u6761\u4ef6\u53d6\u53cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>is_student = False<\/p>\n<p>if not (age &lt; 18 or is_student):<\/p>\n<p>    print(&quot;Eligible for full membership&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Not eligible for full membership&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c<code>age<\/code>\u5c0f\u4e8e18\u6216<code>is_student<\/code>\u4e3a\u771f\uff0c\u5219\u6574\u4e2a\u6761\u4ef6\u4e3a\u5047\uff0c\u53d6\u53cd\u540e\u4e3a\u771f\uff0c\u6240\u4ee5\u4f1a\u8f93\u51fa\u201cEligible for full membership\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001AND\u3001OR\u3001NOT\u7ec4\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u540c\u65f6\u4f7f\u7528<code>and<\/code>\u3001<code>or<\/code>\u548c<code>not<\/code>\u5173\u952e\u5b57\u6765\u5904\u7406\u590d\u6742\u7684\u5224\u65ad\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h4>1. \u7ec4\u5408\u4f7f\u7528<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">age = 22<\/p>\n<p>income = 30000<\/p>\n<p>is_student = True<\/p>\n<p>if (age &gt; 18 and income &gt; 25000) or (is_student and income &gt; 20000):<\/p>\n<p>    print(&quot;Eligible for special loan&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Not eligible for special loan&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u8981\u6ee1\u8db3<code>age<\/code>\u5927\u4e8e18\u5e76\u4e14<code>income<\/code>\u5927\u4e8e25000\uff0c\u6216\u8005<code>is_student<\/code>\u4e3a\u771f\u5e76\u4e14<code>income<\/code>\u5927\u4e8e20000\u4e2d\u7684\u4efb\u4e00\u6761\u4ef6\uff0c\u5c31\u4f1a\u8f93\u51fa\u201cEligible for special loan\u201d\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u62ec\u53f7\u660e\u786e\u4f18\u5148\u7ea7<\/h4>\n<\/p>\n<p><p>\u5f53\u5224\u65ad\u6761\u4ef6\u975e\u5e38\u590d\u6742\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7\u6765\u660e\u786e\u4f18\u5148\u7ea7\uff0c\u907f\u514d\u903b\u8f91\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 20<\/p>\n<p>income = 15000<\/p>\n<p>credit_score = 650<\/p>\n<p>is_student = True<\/p>\n<p>if (age &gt; 18 and (income &gt; 20000 or credit_score &gt; 600)) and not is_student:<\/p>\n<p>    print(&quot;Eligible for standard loan&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Not eligible for standard loan&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u4e86\u62ec\u53f7\u6765\u660e\u786e\u6761\u4ef6\u7684\u4f18\u5148\u7ea7\uff0c\u5148\u5224\u65ad<code>age &gt; 18<\/code>\u548c<code>(income &gt; 20000 or credit_score &gt; 600)<\/code>\uff0c\u7136\u540e\u518d\u5224\u65ad\u662f\u5426<code>not is_student<\/code>\u3002\u53ea\u6709\u5728\u6240\u6709\u6761\u4ef6\u90fd\u4e3a\u771f\u65f6\uff0c\u624d\u4f1a\u8f93\u51fa\u201cEligible for standard loan\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u4f7f\u7528<code>and<\/code>\u3001<code>or<\/code>\u548c<code>not<\/code>\u5173\u952e\u5b57\u80fd\u591f\u7b80\u5316\u4ee3\u7801\u903b\u8f91\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. \u7528\u6237\u8ba4\u8bc1<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u8ba4\u8bc1\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u540c\u65f6\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\uff0c\u4f8b\u5982\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u5426\u6b63\u786e\uff0c\u7528\u6237\u662f\u5426\u88ab\u51bb\u7ed3\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">username = &quot;user1&quot;<\/p>\n<p>password = &quot;password123&quot;<\/p>\n<p>is_active = True<\/p>\n<p>if username == &quot;user1&quot; and password == &quot;password123&quot; and is_active:<\/p>\n<p>    print(&quot;Login successful&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Login f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u6709\u5f53\u7528\u6237\u540d\u3001\u5bc6\u7801\u6b63\u786e\uff0c\u5e76\u4e14\u7528\u6237\u672a\u88ab\u51bb\u7ed3\u65f6\uff0c\u624d\u4f1a\u8f93\u51fa\u201cLogin successful\u201d\u3002<\/p>\n<\/p>\n<p><h4>2. \u6570\u636e\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u9a8c\u8bc1\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\uff0c\u4f8b\u5982\u6570\u636e\u662f\u5426\u4e3a\u7a7a\uff0c\u6570\u636e\u7c7b\u578b\u662f\u5426\u6b63\u786e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = &quot;example&quot;<\/p>\n<p>is_valid = True<\/p>\n<p>is_not_empty = bool(data)<\/p>\n<p>if is_valid and is_not_empty:<\/p>\n<p>    print(&quot;Data is valid and not empty&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Data is invalid or empty&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u6709\u5f53\u6570\u636e\u6709\u6548\u5e76\u4e14\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u624d\u4f1a\u8f93\u51fa\u201cData is valid and not empty\u201d\u3002<\/p>\n<\/p>\n<p><h4>3. \u8bbf\u95ee\u6743\u9650\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u8bbf\u95ee\u6743\u9650\u63a7\u5236\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5224\u65ad\u7528\u6237\u7684\u89d2\u8272\u548c\u6743\u9650\uff0c\u4f8b\u5982\u7528\u6237\u662f\u5426\u4e3a\u7ba1\u7406\u5458\uff0c\u662f\u5426\u6709\u8bbf\u95ee\u6743\u9650\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">role = &quot;admin&quot;<\/p>\n<p>has_permission = True<\/p>\n<p>if role == &quot;admin&quot; or has_permission:<\/p>\n<p>    print(&quot;Access granted&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Access denied&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u8981\u7528\u6237\u662f\u7ba1\u7406\u5458\uff0c\u6216\u8005\u6709\u8bbf\u95ee\u6743\u9650\uff0c\u5c31\u4f1a\u8f93\u51fa\u201cAccess granted\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>and<\/code>\u3001<code>or<\/code>\u548c<code>not<\/code>\u5173\u952e\u5b57\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f18\u5148\u7ea7<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>not<\/code>\u7684\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u5176\u6b21\u662f<code>and<\/code>\uff0c\u6700\u540e\u662f<code>or<\/code>\u3002\u5728\u8fdb\u884c\u590d\u6742\u5224\u65ad\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7\u6765\u660e\u786e\u4f18\u5148\u7ea7\uff0c\u907f\u514d\u903b\u8f91\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">condition1 = True<\/p>\n<p>condition2 = False<\/p>\n<p>condition3 = True<\/p>\n<p>result = condition1 and not condition2 or condition3<\/p>\n<p>print(result)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e<code>not<\/code>\u7684\u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u6240\u4ee5\u5148\u5bf9<code>condition2<\/code>\u8fdb\u884c\u53d6\u53cd\uff0c\u7136\u540e\u518d\u8fdb\u884c<code>and<\/code>\u548c<code>or<\/code>\u7684\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><h4>2. \u77ed\u8def\u6c42\u503c<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>and<\/code>\u548c<code>or<\/code>\u90fd\u5177\u6709\u77ed\u8def\u6c42\u503c\u7684\u7279\u6027\u3002\u5373\u5728\u5224\u65ad\u6761\u4ef6\u65f6\uff0c\u5982\u679c\u524d\u9762\u7684\u6761\u4ef6\u5df2\u7ecf\u80fd\u591f\u786e\u5b9a\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\uff0c\u5219\u540e\u9762\u7684\u6761\u4ef6\u5c06\u4e0d\u4f1a\u88ab\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def check1():<\/p>\n<p>    print(&quot;check1 called&quot;)<\/p>\n<p>    return True<\/p>\n<p>def check2():<\/p>\n<p>    print(&quot;check2 called&quot;)<\/p>\n<p>    return False<\/p>\n<p>result = check1() and check2()  # \u53ea\u4f1a\u8f93\u51fa &quot;check1 called&quot;<\/p>\n<p>print(result)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e<code>check1()<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u6240\u4ee5\u4f1a\u7ee7\u7eed\u5224\u65ad<code>check2()<\/code>\u3002\u4f46\u5982\u679c<code>check1()<\/code>\u8fd4\u56de<code>False<\/code>\uff0c\u5219\u4e0d\u4f1a\u7ee7\u7eed\u5224\u65ad<code>check2()<\/code>\uff0c\u56e0\u4e3a\u6574\u4e2a\u8868\u8fbe\u5f0f\u5df2\u7ecf\u53ef\u4ee5\u786e\u5b9a\u4e3a<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3. \u53ef\u8bfb\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5c3d\u91cf\u4fdd\u6301\u4ee3\u7801\u7684\u7b80\u6d01\u548c\u53ef\u8bfb\u6027\u3002\u5982\u679c\u5224\u65ad\u6761\u4ef6\u8fc7\u4e8e\u590d\u6742\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5176\u62c6\u5206\u6210\u591a\u4e2a\u7b80\u5355\u7684\u6761\u4ef6\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>income = 40000<\/p>\n<p>credit_score = 700<\/p>\n<p>is_student = False<\/p>\n<h2><strong>\u62c6\u5206\u6210\u591a\u4e2a\u7b80\u5355\u7684\u6761\u4ef6<\/strong><\/h2>\n<p>is_adult = age &gt; 18<\/p>\n<p>has_good_income = income &gt; 30000<\/p>\n<p>has_good_credit = credit_score &gt; 650<\/p>\n<p>is_not_student = not is_student<\/p>\n<p>if is_adult and has_good_income and has_good_credit and is_not_student:<\/p>\n<p>    print(&quot;Eligible for special offer&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Not eligible for special offer&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c06\u590d\u6742\u7684\u5224\u65ad\u6761\u4ef6\u62c6\u5206\u6210\u591a\u4e2a\u7b80\u5355\u7684\u6761\u4ef6\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u793a\u4f8b\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u540c\u65f6\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\uff0c\u4e0b\u9762\u5217\u4e3e\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u8868\u5355\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5728\u8868\u5355\u9a8c\u8bc1\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u540c\u65f6\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\uff0c\u4f8b\u5982\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u90ae\u7bb1\u7b49\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">username = &quot;user123&quot;<\/p>\n<p>password = &quot;pass123&quot;<\/p>\n<p>email = &quot;user@example.com&quot;<\/p>\n<p>is_valid_username = len(username) &gt;= 5<\/p>\n<p>is_valid_password = len(password) &gt;= 6<\/p>\n<p>is_valid_email = &quot;@&quot; in email and &quot;.&quot; in email<\/p>\n<p>if is_valid_username and is_valid_password and is_valid_email:<\/p>\n<p>    print(&quot;Form is valid&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Form is invalid&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u6709\u5f53\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u90ae\u7bb1\u90fd\u7b26\u5408\u8981\u6c42\u65f6\uff0c\u624d\u4f1a\u8f93\u51fa\u201cForm is valid\u201d\u3002<\/p>\n<\/p>\n<p><h4>2. \u5546\u54c1\u7b5b\u9009<\/h4>\n<\/p>\n<p><p>\u5728\u7535\u5546\u5e73\u53f0\u4e0a\uff0c\u901a\u5e38\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u7b5b\u9009\u5546\u54c1\uff0c\u4f8b\u5982\u4ef7\u683c\u3001\u8bc4\u5206\u3001\u5e93\u5b58\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">price = 100<\/p>\n<p>rating = 4.5<\/p>\n<p>in_stock = True<\/p>\n<p>is_affordable = price &lt;= 150<\/p>\n<p>has_good_rating = rating &gt;= 4.0<\/p>\n<p>is_available = in_stock<\/p>\n<p>if is_affordable and has_good_rating and is_available:<\/p>\n<p>    print(&quot;Product meets the criteria&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Product does not meet the criteria&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u6709\u5f53\u5546\u54c1\u4ef7\u683c\u5408\u9002\u3001\u8bc4\u5206\u9ad8\u5e76\u4e14\u6709\u5e93\u5b58\u65f6\uff0c\u624d\u4f1a\u8f93\u51fa\u201cProduct meets the criteria\u201d\u3002<\/p>\n<\/p>\n<p><h4>3. \u52a8\u6001\u6743\u9650\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u52a8\u6001\u63a7\u5236\u7528\u6237\u7684\u6743\u9650\uff0c\u4f8b\u5982\u7528\u6237\u89d2\u8272\u3001\u6743\u9650\u7b49\u7ea7\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">role = &quot;editor&quot;<\/p>\n<p>permission_level = 3<\/p>\n<p>is_admin = role == &quot;admin&quot;<\/p>\n<p>is_editor_with_high_permission = role == &quot;editor&quot; and permission_level &gt; 2<\/p>\n<p>if is_admin or is_editor_with_high_permission:<\/p>\n<p>    print(&quot;Access granted&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Access denied&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ea\u8981\u7528\u6237\u662f\u7ba1\u7406\u5458\uff0c\u6216\u8005\u662f\u6743\u9650\u7b49\u7ea7\u5927\u4e8e2\u7684\u7f16\u8f91\uff0c\u5c31\u4f1a\u8f93\u51fa\u201cAccess granted\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\u3002<strong>\u4e3b\u8981\u4f7f\u7528\u7684\u5173\u952e\u5b57\u662f<code>and<\/code>\u3001<code>or<\/code>\u548c<code>not<\/code>\uff0c\u5b83\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u6765\u5904\u7406\u590d\u6742\u7684\u5224\u65ad\u903b\u8f91\u3002<\/strong> \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5173\u952e\u5b57\u80fd\u591f\u7b80\u5316\u4ee3\u7801\u903b\u8f91\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u90e8\u5206\u5185\u5bb9\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26<code>and<\/code>\u3001<code>or<\/code>\u548c<code>not<\/code>\u6765\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\u3002<code>and<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u5f53\u6240\u6709\u6761\u4ef6\u90fd\u4e3a\u771f\u65f6\u8fd4\u56de\u771f\uff0c<code>or<\/code>\u8fd0\u7b97\u7b26\u5219\u662f\u5728\u4efb\u4e00\u6761\u4ef6\u4e3a\u771f\u65f6\u8fd4\u56de\u771f\uff0c\u800c<code>not<\/code>\u8fd0\u7b97\u7b26\u5219\u7528\u4e8e\u53cd\u8f6c\u6761\u4ef6\u7684\u771f\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>if condition1 and condition2:<\/code> \u6765\u5224\u65ad\u4e24\u4e2a\u6761\u4ef6\u662f\u5426\u540c\u65f6\u4e3a\u771f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u6765\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\u6216\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u6765\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>any()<\/code>\u6216<code>all()<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u68c0\u67e5\u6761\u4ef6\u3002\u4f8b\u5982\uff0c<code>if all(x &gt; 0 for x in my_list):<\/code> \u53ef\u4ee5\u68c0\u67e5\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u5927\u4e8e0\uff0c\u800c<code>if any(x &gt; 0 for x in my_list):<\/code> \u5219\u68c0\u67e5\u662f\u5426\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\u5927\u4e8e0\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u591a\u6761\u4ef6\u5224\u65ad\u5b9e\u4f8b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u89c1\u7684\u591a\u6761\u4ef6\u5224\u65ad\u5b9e\u4f8b\u5305\u62ec\u7528\u6237\u8f93\u5165\u9a8c\u8bc1\u3001\u6570\u636e\u8fc7\u6ee4\u4ee5\u53ca\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u7ed3\u5408\u591a\u4e2a\u6761\u4ef6\u6765\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u7b26\u5408\u7279\u5b9a\u683c\u5f0f\u6216\u8981\u6c42\uff0c\u6216\u8005\u5728\u6570\u636e\u5206\u6790\u4e2d\u7b5b\u9009\u51fa\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e\u96c6\u3002\u8fd9\u4e9b\u5b9e\u4f8b\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u589e\u5f3a\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u540c\u65f6\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\u53ef\u4ee5\u4f7f\u7528&#39;and&#39;\u3001&#39;or&#39;\u548c&#39;not&#038; [&hellip;]","protected":false},"author":3,"featured_media":1174748,"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\/1174742"}],"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=1174742"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174742\/revisions"}],"predecessor-version":[{"id":1174749,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174742\/revisions\/1174749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174748"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}