{"id":2980,"date":"2025-06-03T16:14:12","date_gmt":"2025-06-04T09:14:11","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2980"},"modified":"2025-06-04T16:14:12","modified_gmt":"2025-06-04T09:14:11","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84complex%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84complex%e5%87%bd%e6%95%b8\/","title":{"rendered":"2025 \u6700\u65b0\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 complex() \u51fd\u6578\u8207\u8907\u6578\u904b\u7b97"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, complex(), \u8907\u6578, Python \u51fd\u6578\"><\/p>\n<h1>2025 \u6700\u65b0\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 complex() \u51fd\u6578\u8207\u8907\u6578\u904b\u7b97<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`complex()` \u51fd\u6578\u7528\u65bc\u5efa\u7acb\u8907\u6578\u5c0d\u8c61\u3002\u8907\u6578\u7531\u5be6\u90e8\u548c\u865b\u90e8\u7d44\u6210\uff0c\u5728\u6578\u5b78\u548c\u79d1\u5b78\u8a08\u7b97\u4e2d\u5177\u6709\u91cd\u8981\u7684\u61c9\u7528\u3002\u6b64\u51fd\u6578\u63a5\u6536\u5169\u500b\u53c3\u6578\uff1a\u7b2c\u4e00\u500b\u70ba\u5be6\u90e8\uff0c\u7b2c\u4e8c\u500b\u70ba\u865b\u90e8\u3002\u5982\u679c\u53ea\u63d0\u4f9b\u4e00\u500b\u53c3\u6578\uff0c\u5247\u865b\u90e8\u9ed8\u8a8d\u70ba 0\u3002<\/p>\n<p>### \u5efa\u7acb\u8907\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>\u4ee5\u4e0b\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `complex()` \u51fd\u6578\u4f86\u5efa\u7acb\u8907\u6578\uff1a<\/p>\n<p>#### \u7bc4\u4f8b 1\uff1a\u5efa\u7acb\u8907\u6578<br \/>\n&#8220;`python<br \/>\nz1 = complex(3, 4)  # \u5be6\u90e8\u70ba 3\uff0c\u865b\u90e8\u70ba 4<br \/>\nprint(z1)  # \u8f38\u51fa\uff1a (3+4j)<br \/>\n&#8220;`<\/p>\n<p>#### \u7bc4\u4f8b 2\uff1a\u53ea\u63d0\u4f9b\u5be6\u90e8<br \/>\n&#8220;`python<br \/>\nz2 = complex(5)  # \u5be6\u90e8\u70ba 5\uff0c\u865b\u90e8\u70ba 0<br \/>\nprint(z2)  # \u8f38\u51fa\uff1a (5+0j)<br \/>\n&#8220;`<\/p>\n<p>### \u7372\u53d6\u8907\u6578\u7684\u5be6\u90e8\u8207\u865b\u90e8<\/p>\n<p>\u53ef\u4ee5\u901a\u904e\u5c6c\u6027 `real` \u548c `imag` \u4f86\u7372\u53d6\u8907\u6578\u7684\u5be6\u90e8\u548c\u865b\u90e8\uff1a<\/p>\n<p>&#8220;`python<br \/>\nreal_part = z1.real  # \u7372\u53d6\u5be6\u90e8<br \/>\nimaginary_part = z1.imag  # \u7372\u53d6\u865b\u90e8<br \/>\nprint(real_part, imaginary_part)  # \u8f38\u51fa\uff1a 3 4<br \/>\n&#8220;`<\/p>\n<p>### \u8a08\u7b97\u6975\u5750\u6a19<\/p>\n<p>\u8907\u6578\u7684\u6975\u5750\u6a19\u8868\u793a\u65b9\u6cd5\u540c\u6a23\u975e\u5e38\u91cd\u8981\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `abs()` \u51fd\u6578\u8a08\u7b97\u8907\u6578\u7684\u6a21\uff0c\u4ee5\u53ca\u4f7f\u7528 `cmath` \u6a21\u7d44\u7372\u53d6\u76f8\u4f4d\u89d2\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport cmath<\/p>\n<p>r = abs(z1)  # \u8a08\u7b97\u6a21<br \/>\nphi = cmath.phase(z1)  # \u8a08\u7b97\u76f8\u4f4d\u89d2<br \/>\nprint(r, phi)  # \u8f38\u51fa\u6a21\u548c\u76f8\u4f4d\u89d2<br \/>\n&#8220;`<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `complex()` \u51fd\u6578\u6642\uff0c\u8acb\u6ce8\u610f\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **\u932f\u8aa4\u7684\u6578\u64da\u985e\u578b**\uff1a\u78ba\u4fdd\u63d0\u4f9b\u7684\u53c3\u6578\u70ba\u6578\u5b57\u985e\u578b\u3002\u5982\u679c\u63d0\u4f9b\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u985e\u578b\uff0c\u5c07\u5f15\u767c `TypeError`\u3002<\/p>\n<p>&#8211; **\u8907\u6578\u5f62\u5f0f\u932f\u8aa4**\uff1a\u5982\u679c\u624b\u52d5\u5efa\u7acb\u8907\u6578\uff0c\u8acb\u4f7f\u7528 `j` \u4f5c\u70ba\u865b\u90e8\u6a19\u8b58\uff0c\u4f8b\u5982 `3 + 4j` \u800c\u975e `3 + 4i`\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u8907\u6578\u5728\u8a31\u591a\u61c9\u7528\u4e2d\u81f3\u95dc\u91cd\u8981\uff0c\u5305\u62ec\u4fe1\u865f\u8655\u7406\u3001\u5716\u5f62\u5b78\u4ee5\u53ca\u63a7\u5236\u7cfb\u7d71\u3002\u4e86\u89e3\u5982\u4f55\u5728\u9019\u4e9b\u9818\u57df\u4e2d\u4f7f\u7528\u8907\u6578\u5c07\u4f7f\u4f60\u5728\u79d1\u5b78\u548c\u5de5\u7a0b\u8a08\u7b97\u4e2d\u53d7\u76ca\u7121\u7aae\u3002<\/p>\n<p>\u6b32\u4e86\u89e3\u66f4\u591a Python \u7684\u9032\u968e\u61c9\u7528\uff0c\u6b61\u8fce\u53c3\u8003\u6211\u5011\u7684 [Python \u6559\u5b78\u8cc7\u6e90](https:\/\/vocus.cc)\u3002<\/p>\n<p>### \u7d50\u8a9e<\/p>\n<p>\u7e3d\u7d50\u800c\u8a00\uff0cPython \u7684 `complex()` \u51fd\u6578\u4f7f\u5f97\u8655\u7406\u8907\u6578\u8b8a\u5f97\u7c21\u55ae\u4e14\u9ad8\u6548\u3002\u638c\u63e1\u9019\u4e00\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u5f0f\u5c0d\u65bc\u9032\u884c\u6578\u5b78\u8a08\u7b97\u3001\u4fe1\u865f\u8655\u7406\u4ee5\u53ca\u6578\u64da\u5206\u6790\u5177\u6709\u91cd\u8981\u610f\u7fa9\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `complex()` \u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: `complex()` \u51fd\u6578\u8fd4\u56de\u4e00\u500b\u8907\u6578\u5c0d\u8c61\uff0c\u683c\u5f0f\u70ba `a + bj`\uff0c\u5176\u4e2d `a` \u662f\u5be6\u90e8\uff0c`b` \u662f\u865b\u90e8\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5c07\u8907\u6578\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `str()` \u51fd\u6578\u5c07\u8907\u6578\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u4f8b\u5982 `str(z1)`\u3002<\/p>\n<p>**Q3: \u8907\u6578\u7684\u904b\u7b97\u6709\u4ec0\u9ebc\u7279\u5225\u4e4b\u8655\uff1f**<br \/>\nA3: \u8907\u6578\u7684\u904b\u7b97\u9075\u5faa\u7279\u5b9a\u7684\u6578\u5b78\u898f\u5247\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u6e1b\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\uff0c\u9019\u4e9b\u904b\u7b97\u9075\u5faa\u5be6\u6578\u904b\u7b97\u7684\u57fa\u672c\u898f\u5247\uff0c\u4f46\u4e5f\u9700\u8981\u8003\u616e\u865b\u90e8\u7684\u5b58\u5728\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>complex()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u5275\u5efa\u8907\u6578\u5c0d\u8c61\uff0c\u4e26\u63d0\u4f9b\u4e00\u7cfb\u5217\u7684\u64cd\u4f5c\u4f86\u64cd\u4f5c\u8907\u6578\u5c0d\u8c61\u3002\u672c\u6587\u5c07\u4ecb\u7d39complex()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2980","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-M4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2980"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2980\/revisions"}],"predecessor-version":[{"id":2981,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2980\/revisions\/2981"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}