{"id":1081013,"date":"2025-01-08T12:35:34","date_gmt":"2025-01-08T04:35:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081013.html"},"modified":"2025-01-08T12:35:37","modified_gmt":"2025-01-08T04:35:37","slug":"python3%e5%a6%82%e4%bd%95%e5%86%99%e9%80%bb%e8%be%91%e4%b8%8e-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081013.html","title":{"rendered":"python3\u5982\u4f55\u5199\u903b\u8f91\u4e0e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183236\/1cdbe365-a441-4287-831e-bb6da89a7b31.webp\" alt=\"python3\u5982\u4f55\u5199\u903b\u8f91\u4e0e\" \/><\/p>\n<p><p> <strong>Python3\u5199\u903b\u8f91\u4e0e\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5e03\u5c14\u8fd0\u7b97\u7b26<code>and<\/code>\u3001\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\u3001\u4f7f\u7528<code>all()<\/code>\u51fd\u6570\u3002<\/strong> \u4f7f\u7528\u5e03\u5c14\u8fd0\u7b97\u7b26<code>and<\/code>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u662f\u5426\u540c\u65f6\u4e3a\u771f\u3002\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6761\u4ef6\u5224\u65ad\u548c\u903b\u8f91\u63a7\u5236\u3002<code>all()<\/code>\u51fd\u6570\u5219\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u4e3a\u771f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5e03\u5c14\u8fd0\u7b97\u7b26<code>and<\/code><\/h3>\n<\/p>\n<p><p>\u5e03\u5c14\u8fd0\u7b97\u7b26<code>and<\/code>\u662fPython\u4e2d\u7528\u4e8e\u5b9e\u73b0\u903b\u8f91\u4e0e\u64cd\u4f5c\u7684\u57fa\u672c\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u8fde\u63a5\u4e24\u4e2a\u6216\u591a\u4e2a\u6761\u4ef6\uff0c\u5e76\u5728\u6240\u6709\u6761\u4ef6\u90fd\u6ee1\u8db3\u7684\u60c5\u51b5\u4e0b\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002 <\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">x = 5<\/p>\n<p>y = 10<\/p>\n<p>if x &gt; 0 and y &gt; 0:<\/p>\n<p>    print(&quot;Both numbers are positive.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;At least one number is not positive.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6761\u4ef6<code>x &gt; 0<\/code>\u548c<code>y &gt; 0<\/code>\u90fd\u5fc5\u987b\u4e3a\u771f\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u624d\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u5e76\u6253\u5370\u51fa\u201cBoth numbers are positive.\u201d\u3002\u5426\u5219\uff0c\u5c06\u6253\u5370\u51fa\u201cAt least one number is not positive.\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7ed3\u5408<code>if<\/code>\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\u4f7f\u7528<code>and<\/code>\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u5728\u67d0\u4e2a\u8303\u56f4\u5185\uff0c\u5e76\u4e14\u662f\u5426\u4e3a\u5076\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">number = 8<\/p>\n<p>if number &gt;= 1 and number &lt;= 10 and number % 2 == 0:<\/p>\n<p>    print(&quot;The number is between 1 and 10 and is even.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The number does not meet the criteria.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6761\u4ef6<code>number &gt;= 1<\/code>\u3001<code>number &lt;= 10<\/code>\u548c<code>number % 2 == 0<\/code>\u90fd\u5fc5\u987b\u4e3a\u771f\uff0c\u624d\u4f1a\u6253\u5370\u51fa\u201c<code>The number is between 1 and 10 and is even.<\/code>\u201d\u3002\u5426\u5219\uff0c\u5c06\u6253\u5370\u51fa\u201c<code>The number does not meet the criteria.<\/code>\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>all()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>all()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u4e3a\u771f\u3002\u5982\u679c\u6240\u6709\u5143\u7d20\u90fd\u4e3a\u771f\uff0c<code>all()<\/code>\u51fd\u6570\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u8fd9\u5728\u9700\u8981\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">conditions = [x &gt; 0, y &gt; 0, x + y &gt; 10]<\/p>\n<p>if all(conditions):<\/p>\n<p>    print(&quot;All conditions are met.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Not all conditions are met.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>conditions<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\u7684\u5217\u8868\u3002<code>all(conditions)<\/code>\u68c0\u67e5\u5217\u8868\u4e2d\u7684\u6240\u6709\u6761\u4ef6\u662f\u5426\u90fd\u4e3a\u771f\u3002\u5982\u679c\u662f\uff0c\u5219\u6253\u5370\u51fa\u201c<code>All conditions are met.<\/code>\u201d\uff0c\u5426\u5219\u6253\u5370\u51fa\u201c<code>Not all conditions are met.<\/code>\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython3\u4e2d\u5b9e\u73b0\u903b\u8f91\u4e0e\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5e03\u5c14\u8fd0\u7b97\u7b26<code>and<\/code>\u3001\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\u3001\u4f7f\u7528<code>all()<\/code>\u51fd\u6570\u3002<strong>\u5e03\u5c14\u8fd0\u7b97\u7b26<code>and<\/code><\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fde\u63a5\u591a\u4e2a\u6761\u4ef6\u5e76\u8fdb\u884c\u5224\u65ad\u3002<strong>\u7ed3\u5408<code>if<\/code>\u8bed\u53e5<\/strong>\u5219\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u63a7\u5236\u3002<strong><code>all()<\/code>\u51fd\u6570<\/strong>\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u68c0\u67e5\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u6761\u4ef6\u662f\u5426\u90fd\u4e3a\u771f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u903b\u8f91\u4e0e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u9a8c\u8bc1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u662f\u5426\u540c\u65f6\u6ee1\u8db3\u3002\u4f8b\u5982\uff0c\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u8868\u5355\u6570\u636e\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def validate_form(data):<\/p>\n<p>    if data[&#39;age&#39;] &gt; 18 and data[&#39;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&#39;].endswith(&#39;@example.com&#39;) and data[&#39;name&#39;]:<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<p>form_data = {&#39;age&#39;: 25, &#39;email&#39;: &#39;user@example.com&#39;, &#39;name&#39;: &#39;John Doe&#39;}<\/p>\n<p>if validate_form(form_data):<\/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<code>validate_form<\/code>\u51fd\u6570\u68c0\u67e5\u7528\u6237\u8f93\u5165\u7684\u5e74\u9f84\u662f\u5426\u5927\u4e8e18\u3001\u90ae\u7bb1\u662f\u5426\u4ee5<code>@example.com<\/code>\u7ed3\u5c3e\u3001\u59d3\u540d\u662f\u5426\u975e\u7a7a\u3002\u5982\u679c\u6240\u6709\u6761\u4ef6\u90fd\u6ee1\u8db3\uff0c\u51fd\u6570\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u590d\u6742\u6761\u4ef6\u5224\u65ad<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u540c\u65f6\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u5224\u65ad\u4e00\u4e2a\u7528\u6237\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u67d0\u4e2a\u8d44\u6e90\uff0c\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u7528\u6237\u7684\u89d2\u8272\u3001\u72b6\u6001\u4ee5\u53ca\u5176\u4ed6\u76f8\u5173\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def has_access(user):<\/p>\n<p>    if user[&#39;role&#39;] == &#39;admin&#39; and user[&#39;active&#39;] and not user[&#39;banned&#39;]:<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<p>user_info = {&#39;role&#39;: &#39;admin&#39;, &#39;active&#39;: True, &#39;banned&#39;: False}<\/p>\n<p>if has_access(user_info):<\/p>\n<p>    print(&quot;User has access.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;User does not have access.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>has_access<\/code>\u51fd\u6570\u68c0\u67e5\u7528\u6237\u7684\u89d2\u8272\u662f\u5426\u4e3a<code>admin<\/code>\u3001\u7528\u6237\u662f\u5426\u6d3b\u8dc3\u4ee5\u53ca\u7528\u6237\u662f\u5426\u672a\u88ab\u7981\u6b62\u3002\u5982\u679c\u6240\u6709\u6761\u4ef6\u90fd\u6ee1\u8db3\uff0c\u51fd\u6570\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4e0e<code>all()<\/code>\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u903b\u8f91\u4e0e\u64cd\u4f5c\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5728\u9700\u8981\u65f6\u624d\u4f1a\u751f\u6210\u5143\u7d20\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [2, 4, 6, 8, 10]<\/p>\n<p>if all(n % 2 == 0 for n in numbers):<\/p>\n<p>    print(&quot;All numbers are even.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Not all numbers are even.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u751f\u6210\u5668\u8868\u8fbe\u5f0f<code>(n % 2 == 0 for n in numbers)<\/code>\u9010\u4e2a\u68c0\u67e5\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\u3002\u5982\u679c\u6240\u6709\u6570\u5b57\u90fd\u662f\u5076\u6570\uff0c<code>all()<\/code>\u51fd\u6570\u8fd4\u56de<code>True<\/code>\uff0c\u5e76\u6253\u5370\u51fa\u201c<code>All numbers are even.<\/code>\u201d\u3002\u5426\u5219\uff0c\u6253\u5370\u51fa\u201c<code>Not all numbers are even.<\/code>\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u903b\u8f91\u6216\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u7ed3\u5408\u903b\u8f91\u6216\u8fd0\u7b97\u7b26<code>or<\/code>\u548c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26<code>and<\/code>\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u5224\u65ad\u4e00\u4e2a\u7528\u6237\u662f\u5426\u4e3a\u7ba1\u7406\u5458\u6216\u8d85\u7ea7\u7528\u6237\uff0c\u5e76\u4e14\u7528\u6237\u662f\u5426\u6d3b\u8dc3\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def has_privileges(user):<\/p>\n<p>    if (user[&#39;role&#39;] == &#39;admin&#39; or user[&#39;role&#39;] == &#39;superuser&#39;) and user[&#39;active&#39;]:<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<p>user_info = {&#39;role&#39;: &#39;superuser&#39;, &#39;active&#39;: True}<\/p>\n<p>if has_privileges(user_info):<\/p>\n<p>    print(&quot;User has privileges.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;User does not have privileges.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>has_privileges<\/code>\u51fd\u6570\u68c0\u67e5\u7528\u6237\u7684\u89d2\u8272\u662f\u5426\u4e3a<code>admin<\/code>\u6216<code>superuser<\/code>\uff0c\u5e76\u4e14\u7528\u6237\u662f\u5426\u6d3b\u8dc3\u3002\u5982\u679c\u6761\u4ef6\u90fd\u6ee1\u8db3\uff0c\u51fd\u6570\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u4f7f\u7528\u903b\u8f91\u4e0e<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5217\u8868\u9879\u3002\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u4f7f\u7528\u903b\u8f91\u4e0e\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6761\u4ef6\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>filtered_numbers = [n for n in numbers if n &gt; 3 and n % 2 == 0]<\/p>\n<p>print(filtered_numbers)  # \u8f93\u51fa: [4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6240\u6709\u5927\u4e8e3\u4e14\u4e3a\u5076\u6570\u7684\u6570\u5b57\u3002\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26<code>and<\/code>\u7528\u4e8e\u540c\u65f6\u68c0\u67e5\u4e24\u4e2a\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u9519\u8bef\u5904\u7406\u4e2d\u7684\u903b\u8f91\u4e0e<\/h3>\n<\/p>\n<p><p>\u5728\u9519\u8bef\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u5e76\u4e14\u6587\u4ef6\u662f\u5426\u53ef\u8bfb\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if os.path.exists(file_path) and os.access(file_path, os.R_OK):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;File does not exist or is not readable.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>os.path.exists(file_path)<\/code>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c<code>os.access(file_path, os.R_OK)<\/code>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u53ef\u8bfb\u3002\u5982\u679c\u4e24\u4e2a\u6761\u4ef6\u90fd\u6ee1\u8db3\uff0c\u6587\u4ef6\u5c06\u88ab\u6253\u5f00\u5e76\u8bfb\u53d6\u5185\u5bb9\u3002\u5426\u5219\uff0c\u5c06\u6253\u5370\u51fa\u201c<code>File does not exist or is not readable.<\/code>\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u7ed3\u5408\u51fd\u6570\u5f0f\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e0e\u9ad8\u9636\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u7b80\u6d01\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u8fc7\u6ee4\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5217\u8868\u9879\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>def is_even_and_greater_than_three(n):<\/p>\n<p>    return n &gt; 3 and n % 2 == 0<\/p>\n<p>filtered_numbers = list(filter(is_even_and_greater_than_three, numbers))<\/p>\n<p>print(filtered_numbers)  # \u8f93\u51fa: [4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filter()<\/code>\u51fd\u6570\u7528\u4e8e\u8fc7\u6ee4\u6ee1\u8db3<code>is_even_and_greater_than_three<\/code>\u6761\u4ef6\u7684\u5217\u8868\u9879\u3002\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26<code>and<\/code>\u7528\u4e8e\u540c\u65f6\u68c0\u67e5\u4e24\u4e2a\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u7ed3\u5408\u903b\u8f91\u975e\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4e0e\u903b\u8f91\u975e\u8fd0\u7b97\u7b26<code>not<\/code>\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u5224\u65ad\u4e00\u4e2a\u7528\u6237\u662f\u5426\u65e2\u4e0d\u662f\u7ba1\u7406\u5458\u4e5f\u4e0d\u662f\u8d85\u7ea7\u7528\u6237\uff0c\u5e76\u4e14\u7528\u6237\u662f\u5426\u6d3b\u8dc3\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def lacks_privileges(user):<\/p>\n<p>    if (user[&#39;role&#39;] != &#39;admin&#39; and user[&#39;role&#39;] != &#39;superuser&#39;) and user[&#39;active&#39;]:<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<p>user_info = {&#39;role&#39;: &#39;guest&#39;, &#39;active&#39;: True}<\/p>\n<p>if lacks_privileges(user_info):<\/p>\n<p>    print(&quot;User lacks privileges but is active.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;User does not meet the criteria.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>lacks_privileges<\/code>\u51fd\u6570\u68c0\u67e5\u7528\u6237\u7684\u89d2\u8272\u662f\u5426\u65e2\u4e0d\u662f<code>admin<\/code>\u4e5f\u4e0d\u662f<code>superuser<\/code>\uff0c\u5e76\u4e14\u7528\u6237\u662f\u5426\u6d3b\u8dc3\u3002\u5982\u679c\u6761\u4ef6\u90fd\u6ee1\u8db3\uff0c\u51fd\u6570\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u8be6\u7ec6\u4ecb\u7ecd\uff0cPython3\u4e2d\u5b9e\u73b0\u903b\u8f91\u4e0e\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5e03\u5c14\u8fd0\u7b97\u7b26<code>and<\/code>\u3001\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\u3001\u4f7f\u7528<code>all()<\/code>\u51fd\u6570\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3001\u903b\u8f91\u6216\u8fd0\u7b97\u7b26\u7ed3\u5408\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u9519\u8bef\u5904\u7406\u3001\u51fd\u6570\u5f0f\u7f16\u7a0b\u4ee5\u53ca\u903b\u8f91\u975e\u8fd0\u7b97\u7b26\u7ed3\u5408\u7b49\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u903b\u8f91\u4e0e\u64cd\u4f5c\uff0c\u5e76\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002<strong>\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3001\u9ad8\u6548\u548c\u6613\u4e8e\u7ef4\u62a4<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u903b\u8f91\u4e0e\u5728Python3\u4e2d\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u903b\u8f91\u4e0e\u64cd\u4f5c\u4f7f\u7528\u5173\u952e\u5b57<code>and<\/code>\u8fdb\u884c\u5b9e\u73b0\u3002\u903b\u8f91\u4e0e\u8fd0\u7b97\u7b26\u4f1a\u68c0\u67e5\u4e24\u4e2a\u6761\u4ef6\u662f\u5426\u540c\u65f6\u4e3a\u771f\uff0c\u53ea\u6709\u5f53\u4e24\u4e2a\u6761\u4ef6\u90fd\u4e3a\u771f\u65f6\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u624d\u4e3a\u771f\u3002\u4f8b\u5982\uff0c<code>a = True<\/code>\u548c<code>b = False<\/code>\u65f6\uff0c\u8868\u8fbe\u5f0f<code>a and b<\/code>\u7684\u7ed3\u679c\u4e3a<code>False<\/code>\u3002<\/p>\n<p><strong>\u5728Python3\u4e2d\uff0c\u903b\u8f91\u4e0e\u8fd0\u7b97\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u903b\u8f91\u4e0e\u8fd0\u7b97\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u540c\u65f6\u6ee1\u8db3\u591a\u4e2a\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u4e0e\u6765\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u503c\u7b26\u5408\u7279\u5b9a\u7684\u8303\u56f4\uff0c\u6216\u8005\u5728\u63a7\u5236\u6d41\u4e2d\u5224\u65ad\u591a\u4e2a\u6761\u4ef6\u662f\u5426\u90fd\u6210\u7acb\uff0c\u4ece\u800c\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python3\u4e2d\u4f7f\u7528\u903b\u8f91\u4e0e\u8fdb\u884c\u590d\u6742\u6761\u4ef6\u5224\u65ad\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u903b\u8f91\u4e0e\u53ef\u4ee5\u4e0e\u5176\u4ed6\u903b\u8f91\u8fd0\u7b97\u7b26\uff08\u5982<code>or<\/code>\u548c<code>not<\/code>\uff09\u7ed3\u5408\u4f7f\u7528\uff0c\u4ece\u800c\u5f62\u6210\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u4e0e\u5c06\u591a\u4e2a\u6761\u4ef6\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5982<code>if (age &gt; 18 and age &lt; 65) and (income &gt; 30000):<\/code>\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u68c0\u67e5<code>age<\/code>\u662f\u5426\u572818\u523065\u4e4b\u95f4\uff0c\u5e76\u4e14<code>income<\/code>\u662f\u5426\u5927\u4e8e30000\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python3\u5199\u903b\u8f91\u4e0e\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5e03\u5c14\u8fd0\u7b97\u7b26and\u3001\u7ed3\u5408if\u8bed\u53e5\u3001\u4f7f\u7528all()\u51fd\u6570\u3002 \u4f7f\u7528\u5e03\u5c14\u8fd0\u7b97\u7b26and [&hellip;]","protected":false},"author":3,"featured_media":1081019,"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\/1081013"}],"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=1081013"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081013\/revisions"}],"predecessor-version":[{"id":1081020,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081013\/revisions\/1081020"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1081019"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}