{"id":986302,"date":"2024-12-27T07:43:42","date_gmt":"2024-12-26T23:43:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986302.html"},"modified":"2024-12-27T07:43:46","modified_gmt":"2024-12-26T23:43:46","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e8%b4%9f%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986302.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u8868\u793a\u8d1f\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062932\/a23d742a-40d8-4762-b4e5-14e1b4578d03.webp\" alt=\"Python\u4e2d\u5982\u4f55\u8868\u793a\u8d1f\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8868\u793a\u8d1f\u6570\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c<strong>\u53ea\u9700\u5728\u6570\u5b57\u524d\u52a0\u4e0a\u8d1f\u53f7\u201c-\u201d\u5373\u53ef<\/strong>\u3002\u4f8b\u5982\uff0c-1\u3001-2.5\u548c-100\u90fd\u662f\u8d1f\u6570\u3002Python\u4e2d\u7684\u8d1f\u6570\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\uff0c\u6bd4\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u3002\u6b64\u5916\uff0c\u8d1f\u6570\u4e5f\u53ef\u4ee5\u4e0e\u5185\u7f6e\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u5982abs()\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6570\u7684\u7edd\u5bf9\u503c\u3002\u5728\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u8d1f\u6570\u7684\u8868\u793a\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON\u4e2d\u7684\u6570\u503c\u7c7b\u578b<\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u5728Python\u4e2d\uff0c\u53d8\u91cf\u4e0d\u9700\u8981\u63d0\u524d\u58f0\u660e\u5176\u7c7b\u578b\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u503c\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570(int)\u3001\u6d6e\u70b9\u6570(float)\u548c\u590d\u6570(complex)\uff0c\u6bcf\u79cd\u7c7b\u578b\u90fd\u53ef\u4ee5\u8868\u793a\u8d1f\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6574\u6570\u7c7b\u578b (int)<\/strong><\/li>\n<\/ol>\n<p><p>Python\u4e2d\u7684\u6574\u6570\u7c7b\u578b\u7528\u4e8e\u8868\u793a\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u503c\u3002\u8d1f\u6574\u6570\u7528\u8d1f\u53f7\u201c-\u201d\u8868\u793a\uff0c\u4f8b\u5982-1\u3001-100\u3002\u8fd9\u4e9b\u8d1f\u6574\u6570\u53ef\u4ee5\u53c2\u4e0e\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6d6e\u70b9\u6570\u7c7b\u578b (float)<\/strong><\/li>\n<\/ol>\n<p><p>\u6d6e\u70b9\u6570\u7c7b\u578b\u7528\u4e8e\u8868\u793a\u5e26\u5c0f\u6570\u90e8\u5206\u7684\u6570\u503c\u3002\u8d1f\u6d6e\u70b9\u6570\u540c\u6837\u7528\u8d1f\u53f7\u201c-\u201d\u8868\u793a\uff0c\u4f8b\u5982-2.5\u3001-0.3333\u3002Python\u7684\u6d6e\u70b9\u6570\u9075\u5faaIEEE 754\u6807\u51c6\uff0c\u56e0\u6b64\u5b83\u4eec\u5177\u6709\u4e00\u5b9a\u7684\u7cbe\u5ea6\u9650\u5236\u3002\u6d6e\u70b9\u6570\u5e38\u7528\u4e8e\u9700\u8981\u66f4\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u7b97\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u590d\u6570\u7c7b\u578b (complex)<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u590d\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6570\u503c\u7c7b\u578b\uff0c\u5305\u542b\u5b9e\u90e8\u548c\u865a\u90e8\u3002\u8d1f\u6570\u53ef\u4ee5\u4f5c\u4e3a\u590d\u6570\u7684\u5b9e\u90e8\u6216\u865a\u90e8\uff0c\u4f8b\u5982\uff0c-3+4j\u662f\u4e00\u4e2a\u590d\u6570\uff0c\u5176\u4e2d\u5b9e\u90e8\u4e3a-3\uff0c\u865a\u90e8\u4e3a4\u3002Python\u7684\u590d\u6570\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u590d\u6570\u7684\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8d1f\u6570\u7684\u6570\u5b66\u8fd0\u7b97<\/p>\n<\/p>\n<p><p>Python\u652f\u6301\u5bf9\u8d1f\u6570\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u8d1f\u6570\u6570\u5b66\u8fd0\u7b97\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u52a0\u6cd5\u548c\u51cf\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u8d1f\u6570\u53ef\u4ee5\u4e0e\u6b63\u6570\u6216\u5176\u4ed6\u8d1f\u6570\u8fdb\u884c\u52a0\u6cd5\u548c\u51cf\u6cd5\u8fd0\u7b97\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = -5<\/p>\n<p>b = 3<\/p>\n<p>c = a + b  # \u7ed3\u679c\u4e3a-2<\/p>\n<p>d = a - b  # \u7ed3\u679c\u4e3a-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4e58\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u8d1f\u6570\u4e0e\u6b63\u6570\u6216\u5176\u4ed6\u8d1f\u6570\u76f8\u4e58\u65f6\uff0c\u7ed3\u679c\u53d6\u51b3\u4e8e\u4e58\u6570\u7684\u7b26\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">e = -4<\/p>\n<p>f = -2<\/p>\n<p>g = e * f  # \u7ed3\u679c\u4e3a8\uff0c\u56e0\u4e3a\u4e24\u4e2a\u8d1f\u6570\u76f8\u4e58\u7ed3\u679c\u4e3a\u6b63\u6570<\/p>\n<p>h = e * b  # \u7ed3\u679c\u4e3a-12\uff0c\u56e0\u4e3a\u8d1f\u6570\u4e0e\u6b63\u6570\u76f8\u4e58\u7ed3\u679c\u4e3a\u8d1f\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9664\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>Python\u4e2d\u7684\u8d1f\u6570\u53ef\u4ee5\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\uff0c\u7ed3\u679c\u6839\u636e\u88ab\u9664\u6570\u548c\u9664\u6570\u7684\u7b26\u53f7\u51b3\u5b9a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = -10<\/p>\n<p>j = 2<\/p>\n<p>k = i \/ j  # \u7ed3\u679c\u4e3a-5.0<\/p>\n<p>l = i \/ f  # \u7ed3\u679c\u4e3a5.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u8d1f\u6570\u4e0ePython\u5185\u7f6e\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u8d1f\u6570\u8fdb\u884c\u64cd\u4f5c\u3002\u8fd9\u4e9b\u51fd\u6570\u5305\u62ecabs()\u3001round()\u3001int()\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>abs()\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>abs()\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u4e00\u4e2a\u6570\u7684\u7edd\u5bf9\u503c\uff0c\u5373\u53bb\u6389\u8d1f\u53f7\u540e\u7684\u6570\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">m = -15<\/p>\n<p>n = abs(m)  # \u7ed3\u679c\u4e3a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>round()\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>round()\u51fd\u6570\u7528\u4e8e\u5c06\u6570\u503c\u56db\u820d\u4e94\u5165\u4e3a\u6700\u8fd1\u7684\u6574\u6570\u3002\u5982\u679c\u8f93\u5165\u7684\u662f\u8d1f\u6570\uff0cround()\u51fd\u6570\u4f1a\u8fd4\u56de\u56db\u820d\u4e94\u5165\u540e\u7684\u8d1f\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">o = -7.6<\/p>\n<p>p = round(o)  # \u7ed3\u679c\u4e3a-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>int()\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>int()\u51fd\u6570\u7528\u4e8e\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u622a\u6389\u5c0f\u6570\u90e8\u5206\u3002\u5982\u679c\u8f93\u5165\u7684\u662f\u8d1f\u6d6e\u70b9\u6570\uff0cint()\u51fd\u6570\u4f1a\u8fd4\u56de\u5411\u4e0b\u53d6\u6574\u540e\u7684\u8d1f\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">q = -3.9<\/p>\n<p>r = int(q)  # \u7ed3\u679c\u4e3a-3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u8d1f\u6570\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\uff08\u5982if\u3001elif\u3001else\uff09\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6570\u8fdb\u884c\u903b\u8f91\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d1f\u6570\uff0c\u6216\u8005\u5728\u8d1f\u6570\u7684\u60c5\u51b5\u4e0b\u91c7\u53d6\u7279\u5b9a\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u8d1f\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528if\u8bed\u53e5\u53ef\u4ee5\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d1f\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = -10<\/p>\n<p>if s &lt; 0:<\/p>\n<p>    print(&quot;s\u662f\u4e00\u4e2a\u8d1f\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6761\u4ef6\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<p><p>\u8d1f\u6570\u5728\u6761\u4ef6\u64cd\u4f5c\u4e2d\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = -5<\/p>\n<p>if t &lt; 0:<\/p>\n<p>    print(&quot;t\u662f\u8d1f\u6570\uff0c\u56e0\u6b64\u5c06\u5176\u8f6c\u6362\u4e3a\u6b63\u6570&quot;)<\/p>\n<p>    t = abs(t)<\/p>\n<p>print(t)  # \u8f93\u51fa5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u8d1f\u6570\u5728\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\uff09\u53ef\u4ee5\u5305\u542b\u8d1f\u6570\uff0c\u5e76\u4e14\u8fd9\u4e9b\u8d1f\u6570\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u64cd\u4f5c\u548c\u7b97\u6cd5\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5217\u8868\u4e2d\u7684\u8d1f\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u53ef\u4ee5\u5305\u542b\u8d1f\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u8d1f\u6570\u8fdb\u884c\u6392\u5e8f\u3001\u67e5\u627e\u7b49\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [3, -1, -7, 5, -9]<\/p>\n<p>numbers.sort()  # \u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u7ed3\u679c\u4e3a[-9, -7, -1, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b57\u5178\u4e2d\u7684\u8d1f\u6570\u503c<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5b57\u5178\u4e2d\uff0c\u8d1f\u6570\u53ef\u4ee5\u7528\u4f5c\u503c\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u5bf9\u8d1f\u6570\u8fdb\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">score = {&#39;Alice&#39;: -10, &#39;Bob&#39;: 5, &#39;Carol&#39;: -3}<\/p>\n<p>min_score = min(score.values())  # \u67e5\u627e\u5b57\u5178\u4e2d\u6700\u4f4e\u7684\u5206\u6570\uff0c\u7ed3\u679c\u4e3a-10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5143\u7ec4\u4e2d\u7684\u8d1f\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u5143\u7ec4\u53ef\u4ee5\u5305\u542b\u8d1f\u6570\uff0c\u5e76\u4e14\u5143\u7ec4\u7684\u5143\u7d20\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">coordinates = (-5, 8)<\/p>\n<p>x, y = coordinates  # \u89e3\u5305\u5143\u7ec4<\/p>\n<p>if x &lt; 0:<\/p>\n<p>    print(&quot;x\u662f\u8d1f\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u8d1f\u6570\u4e0e\u6570\u5b66\u5e93\u7684\u7ed3\u5408<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u5e93\uff08\u5982math\u3001numpy\uff09\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5bf9\u8d1f\u6570\u8fdb\u884c\u66f4\u9ad8\u7ea7\u64cd\u4f5c\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>math\u5e93\u4e2d\u7684\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<p><p>Python\u7684math\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u5904\u7406\u8d1f\u6570\u7684\u51fd\u6570\uff0c\u5982sqrt()\u3001log()\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>negative_number = -16<\/p>\n<h2><strong>math.sqrt()\u65e0\u6cd5\u76f4\u63a5\u5904\u7406\u8d1f\u6570\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u590d\u6570<\/strong><\/h2>\n<p>complex_number = complex(negative_number)<\/p>\n<p>sqrt_value = math.sqrt(abs(negative_number)) * (1j if negative_number &lt; 0 else 1)<\/p>\n<h2><strong>\u8ba1\u7b97\u8d1f\u6570\u7684\u5bf9\u6570\uff08\u81ea\u7136\u5bf9\u6570\uff09<\/strong><\/h2>\n<p>log_value = math.log(abs(negative_number)) if negative_number != 0 else float(&#39;-inf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>numpy\u5e93\u4e2d\u7684\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<p><p>numpy\u5e93\u662f\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u5bf9\u8d1f\u6570\u8fdb\u884c\u9ad8\u6548\u8fd0\u7b97\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([-1, -2, -3, 4, 5])<\/p>\n<p>abs_array = np.abs(array)  # \u83b7\u53d6\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u7edd\u5bf9\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u8d1f\u6570\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u8d1f\u6570\u662f\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e38\u7528\u4e8e\u8868\u793a\u635f\u5931\u3001\u51cf\u5c11\u6216\u8d1f\u589e\u957f\u7b49\u6982\u5ff5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u5206\u6790\u4e2d\u7684\u8d1f\u589e\u957f<\/strong><\/li>\n<\/ol>\n<p><p>\u8d1f\u6570\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u6570\u636e\u4e2d\u7684\u8d1f\u589e\u957f\u6216\u51cf\u5c11\u3002\u4f8b\u5982\uff0c\u5728\u5206\u6790\u80a1\u7968\u5e02\u573a\u6570\u636e\u65f6\uff0c\u8d1f\u6570\u53ef\u4ee5\u8868\u793a\u80a1\u7968\u4ef7\u683c\u7684\u4e0b\u964d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u635f\u5931\u8ba1\u7b97<\/strong><\/li>\n<\/ol>\n<p><p>\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u7edf\u8ba1\u5206\u6790\u4e2d\uff0c\u8d1f\u6570\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u635f\u5931\u51fd\u6570\uff0c\u4ee5\u8861\u91cf\u6a21\u578b\u9884\u6d4b\u4e0e\u5b9e\u9645\u503c\u4e4b\u95f4\u7684\u5dee\u8ddd\u3002\u4f8b\u5982\uff0c\u8d1f\u6570\u53ef\u4ee5\u8868\u793a\u6a21\u578b\u7684\u9884\u6d4b\u503c\u4f4e\u4e8e\u5b9e\u9645\u503c\u7684\u7a0b\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u8d1f\u6570\u5728\u56fe\u5f62\u548c\u7ed8\u56fe\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u56fe\u5f62\u548c\u7ed8\u56fe\u4e2d\uff0c\u8d1f\u6570\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u5750\u6807\u8f74\u4e0a\u7684\u8d1f\u65b9\u5411\u6216\u56fe\u5f62\u7684\u53cd\u5411\u53d8\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5750\u6807\u7cfb\u4e2d\u7684\u8d1f\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4e8c\u7ef4\u6216\u4e09\u7ef4\u56fe\u5f62\u4e2d\uff0c\u8d1f\u6570\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u56fe\u5f62\u5728\u5750\u6807\u7cfb\u4e2d\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u8d1f\u6570\u53ef\u4ee5\u8868\u793a\u56fe\u5f62\u5411\u5de6\u6216\u5411\u4e0b\u79fb\u52a8\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u53cd\u5411\u53d8\u6362<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u56fe\u5f62\u53d8\u6362\u4e2d\uff0c\u8d1f\u6570\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u53cd\u5411\u53d8\u6362\u3002\u4f8b\u5982\uff0c\u5728\u65cb\u8f6c\u56fe\u5f62\u65f6\uff0c\u8d1f\u89d2\u5ea6\u53ef\u4ee5\u8868\u793a\u9006\u65f6\u9488\u65cb\u8f6c\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u4f7f\u7528\u8d1f\u6570\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u8d1f\u6570\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u5e38\u89c1\u7684\u9677\u9631\u548c\u95ee\u9898\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u548c\u4e0d\u51c6\u786e\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6574\u6570\u9664\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python 3\u4e2d\uff0c\u6574\u6570\u9664\u6cd5\u8fd4\u56de\u6d6e\u70b9\u6570\u7ed3\u679c\uff0c\u800c\u5728Python 2\u4e2d\uff0c\u6574\u6570\u9664\u6cd5\u8fd4\u56de\u6574\u6570\u7ed3\u679c\u3002\u5728\u8fdb\u884c\u8d1f\u6570\u9664\u6cd5\u65f6\uff0c\u9700\u6ce8\u610fPython\u7248\u672c\u7684\u5dee\u5f02\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8d1f\u6570\u7684\u820d\u5165<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u820d\u5165\u8d1f\u6570\u65f6\uff0c\u9700\u6ce8\u610fPython\u4e2dround()\u51fd\u6570\u7684\u884c\u4e3a\u3002Python\u7684\u820d\u5165\u89c4\u5219\u662f\u201c\u94f6\u884c\u5bb6\u820d\u5165\u201d\uff0c\u5373\u56db\u820d\u516d\u5165\u4e94\u7559\u53cc\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8d1f\u6570\u7684\u7edd\u5bf9\u503c<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8ba1\u7b97\u8d1f\u6570\u7684\u7edd\u5bf9\u503c\u65f6\uff0c\u9700\u786e\u4fdd\u4f7f\u7528abs()\u51fd\u6570\u4ee5\u907f\u514d\u9519\u8bef\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u4e86Python\u4e2d\u8d1f\u6570\u7684\u8868\u793a\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002\u8d1f\u6570\u5728Python\u4e2d\u662f\u4e00\u79cd\u91cd\u8981\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u3001\u6761\u4ef6\u5224\u65ad\u3001\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\u3001\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u3002\u5728\u4f7f\u7528\u8d1f\u6570\u65f6\uff0c\u9700\u6ce8\u610fPython\u7248\u672c\u7684\u5dee\u5f02\u548c\u5e38\u89c1\u7684\u9677\u9631\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002\u65e0\u8bba\u662f\u5728\u65e5\u5e38\u7f16\u7a0b\u8fd8\u662f\u5728\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6b63\u786e\u7406\u89e3\u548c\u4f7f\u7528\u8d1f\u6570\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u80fd\u591f\u66f4\u597d\u5730\u638c\u63e1Python\u4e2d\u8d1f\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u8d1f\u6570\u662f\u5982\u4f55\u8868\u793a\u7684\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8d1f\u6570\u901a\u8fc7\u5728\u6570\u5b57\u524d\u9762\u52a0\u4e0a\u8d1f\u53f7\uff08-\uff09\u6765\u8868\u793a\u3002\u4f8b\u5982\uff0c-5\u3001-10.5\u548c-3.14\u90fd\u662f\u8d1f\u6570\u3002Python\u652f\u6301\u6574\u6570\u548c\u6d6e\u70b9\u6570\u7684\u8d1f\u6570\u8868\u793a\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u8d1f\u6570\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u8d1f\u6570\u53ef\u4ee5\u8fdb\u884c\u54ea\u4e9b\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8d1f\u6570\u53ef\u4ee5\u53c2\u4e0e\u5404\u79cd\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u7b49\u3002\u8d1f\u6570\u5728\u8fd9\u4e9b\u8fd0\u7b97\u4e2d\u9075\u5faa\u5e38\u89c4\u7684\u6570\u5b66\u89c4\u5219\u3002\u4f8b\u5982\uff0c-3 + 2 = -1\uff0c-5 * -2 = 10\u3002\u4f7f\u7528\u8d1f\u6570\u65f6\uff0cPython\u4f1a\u6839\u636e\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u6027\u81ea\u52a8\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d1f\u6570\uff1f<\/strong><br \/>\u8981\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d1f\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u3002\u901a\u8fc7\u6bd4\u8f83\u8be5\u6570\u4e0e0\u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u53ef\u4ee5\u5f97\u51fa\u7ed3\u8bba\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>if num &lt; 0:<\/code>\u6765\u68c0\u67e5<code>num<\/code>\u662f\u5426\u4e3a\u8d1f\u6570\u3002\u5982\u679c\u6761\u4ef6\u6210\u7acb\uff0c\u5219<code>num<\/code>\u5c31\u662f\u8d1f\u6570\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8868\u793a\u8d1f\u6570\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u6570\u5b57\u524d\u52a0\u4e0a\u8d1f\u53f7\u201c-\u201d\u5373\u53ef\u3002\u4f8b\u5982\uff0c-1\u3001-2.5\u548c-100\u90fd\u662f\u8d1f [&hellip;]","protected":false},"author":3,"featured_media":986316,"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\/986302"}],"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=986302"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986302\/revisions"}],"predecessor-version":[{"id":986318,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986302\/revisions\/986318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/986316"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=986302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=986302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=986302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}