{"id":1104330,"date":"2025-01-08T16:19:43","date_gmt":"2025-01-08T08:19:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104330.html"},"modified":"2025-01-08T16:19:46","modified_gmt":"2025-01-08T08:19:46","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%a4%e4%b8%aa%e6%95%b0%e8%bd%ac%e4%b8%ba%e5%a4%8d%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104330.html","title":{"rendered":"Python\u5982\u4f55\u5c06\u4e24\u4e2a\u6570\u8f6c\u4e3a\u590d\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065747\/25f6f10a-2470-4758-a4ca-cbdcd10cbc37.webp\" alt=\"Python\u5982\u4f55\u5c06\u4e24\u4e2a\u6570\u8f6c\u4e3a\u590d\u6570\" \/><\/p>\n<p><p> <strong>Python\u5c06\u4e24\u4e2a\u6570\u8f6c\u4e3a\u590d\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>complex()<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u590d\u6570\u5b57\u9762\u91cf\u3001\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u6784\u9020\u590d\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>complex()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e24\u4e2a\u6570\u8f6c\u5316\u4e3a\u590d\u6570\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>complex()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5185\u7f6e\u7684<code>complex()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5b9e\u6570\u8f6c\u6362\u4e3a\u590d\u6570\u3002<code>complex(real, imag)<\/code>\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u5b9e\u90e8\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u865a\u90e8\u3002\u5982\u679c\u53ea\u4f20\u5165\u4e00\u4e2a\u53c2\u6570\uff0c\u5219\u9ed8\u8ba4\u865a\u90e8\u4e3a0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">real = 3<\/p>\n<p>imag = 4<\/p>\n<p>z = complex(real, imag)<\/p>\n<p>print(z)  # \u8f93\u51fa (3+4j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u6700\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u9700\u8981\u5c06\u4e24\u4e2a\u6570\u76f4\u63a5\u8f6c\u6362\u4e3a\u590d\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u590d\u6570\u5b57\u9762\u91cf<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u590d\u6570\u5b57\u9762\u91cf\u6765\u8868\u793a\u590d\u6570\u3002\u590d\u6570\u7531\u5b9e\u90e8\u548c\u865a\u90e8\u7ec4\u6210\uff0c\u865a\u90e8\u540e\u9762\u5e26\u6709\u4e00\u4e2a<code>j<\/code>\u6216\u8005<code>J<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">real = 5<\/p>\n<p>imag = 6<\/p>\n<p>z = real + imag * 1j<\/p>\n<p>print(z)  # \u8f93\u51fa (5+6j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u7b80\u5355\u7684\u590d\u6570\u8868\u793a\u975e\u5e38\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u5df2\u7ecf\u77e5\u9053\u5b9e\u90e8\u548c\u865a\u90e8\u7684\u503c\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u4e66\u5199\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u6784\u9020\u590d\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u6765\u6784\u9020\u590d\u6570\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u4e0e<code>1j<\/code>\u76f8\u4e58\u6765\u5f97\u5230\u590d\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">real = 7<\/p>\n<p>imag = 8<\/p>\n<p>z = real + imag * 1j<\/p>\n<p>print(z)  # \u8f93\u51fa (7+8j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u8ba1\u7b97\u590d\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u89e3\u6790\u7528\u6237\u8f93\u5165<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u7528\u6237\u8f93\u5165\u4e2d\u89e3\u6790\u51fa\u590d\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c<code>complex()<\/code>\u51fd\u6570\u7ed3\u5408\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = &quot;9,10&quot;<\/p>\n<p>real, imag = map(int, user_input.split(&#39;,&#39;))<\/p>\n<p>z = complex(real, imag)<\/p>\n<p>print(z)  # \u8f93\u51fa (9+10j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4ece\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e2d\u89e3\u6790\u51fa\u590d\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u5e76\u8f6c\u6362<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u5e38\u5e38\u9700\u8981\u4ece\u5217\u8868\u3001\u5143\u7ec4\u7b49\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u6570\u503c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u590d\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u89e3\u5305\u64cd\u4f5c\u548c<code>complex()<\/code>\u51fd\u6570\u7ed3\u5408\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [11, 12]<\/p>\n<p>real, imag = data<\/p>\n<p>z = complex(real, imag)<\/p>\n<p>print(z)  # \u8f93\u51fa (11+12j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5c06\u5b58\u50a8\u5728\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6570\u503c\u8f6c\u6362\u4e3a\u590d\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06\u4e24\u4e2a\u6570\u8f6c\u6362\u4e3a\u590d\u6570\u3002<strong>\u4f7f\u7528<code>complex()<\/code>\u51fd\u6570\u6700\u5e38\u89c1\u4e14\u6613\u4e8e\u7406\u89e3\u3001\u4f7f\u7528\u590d\u6570\u5b57\u9762\u91cf\u7b80\u5355\u76f4\u89c2\u3001\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u7075\u6d3b\u591a\u6837\u3001\u89e3\u6790\u7528\u6237\u8f93\u5165\u9002\u7528\u4e8e\u4ea4\u4e92\u5f0f\u573a\u666f\u3001\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u5e76\u8f6c\u6362\u9002\u7528\u4e8e\u590d\u6742\u6570\u636e\u5904\u7406\u3002<\/strong> \u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u590d\u6570\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u590d\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684complex()\u51fd\u6570\u6216\u76f4\u63a5\u4f7f\u7528\u201c\u5b9e\u90e8+\u865a\u90e8j\u201d\u7684\u683c\u5f0f\u6765\u521b\u5efa\u590d\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528complex()\u51fd\u6570\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>z = complex(3, 4)<\/code>\uff0c\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5b9e\u90e8\u4e3a3\uff0c\u865a\u90e8\u4e3a4\u7684\u590d\u6570z\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u76f4\u63a5\u5199\u4f5c<code>z = 3 + 4j<\/code>\uff0c\u4e24\u8005\u90fd\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u8f6c\u6362\u4e3a\u590d\u6570\uff1f<\/strong><br \/>\u51e0\u4e4e\u6240\u6709\u7684\u6570\u503c\u7c7b\u578b\u90fd\u53ef\u4ee5\u8f6c\u6362\u4e3a\u590d\u6570\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>complex(5)<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u590d\u65705+0j\uff0c\u800c<code>complex(2.5)<\/code>\u5219\u8fd4\u56de2.5+0j\u3002\u5982\u679c\u9700\u8981\u5c06\u4e24\u4e2a\u6570\u5206\u522b\u4f5c\u4e3a\u5b9e\u90e8\u548c\u865a\u90e8\uff0c\u53ef\u4ee5\u4f7f\u7528<code>complex(real, imag)<\/code>\u7684\u5f62\u5f0f\uff0c\u5c06\u6574\u6570\u6216\u6d6e\u70b9\u6570\u4f20\u5165\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8fdb\u884c\u590d\u6570\u8fd0\u7b97\uff1f<\/strong><br \/>Python\u652f\u6301\u590d\u6570\u7684\u57fa\u672c\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u3002\u8fd0\u7b97\u7684\u7ed3\u679c\u4e5f\u4f1a\u662f\u590d\u6570\u3002\u4f8b\u5982\uff0c\u4e24\u4e2a\u590d\u6570\u76f8\u52a0\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>result = (3 + 4j) + (1 + 2j)<\/code>\uff0c\u7ed3\u679c\u5c06\u662f<code>4 + 6j<\/code>\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u8fd0\u7b97\uff0cPython\u5185\u7f6e\u7684\u6570\u5b66\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5bf9\u590d\u6570\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u4f7f\u7528cmath\u6a21\u5757\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u590d\u6570\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u4e24\u4e2a\u6570\u8f6c\u4e3a\u590d\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528complex()\u51fd\u6570\u3001\u4f7f\u7528\u590d\u6570\u5b57\u9762\u91cf\u3001\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u6784\u9020\u590d\u6570\u3002 \u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1104337,"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\/1104330"}],"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=1104330"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104330\/revisions"}],"predecessor-version":[{"id":1104340,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104330\/revisions\/1104340"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104337"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}