{"id":2696,"date":"2025-05-28T17:05:57","date_gmt":"2025-06-04T09:52:56","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2696"},"modified":"2025-06-04T17:05:57","modified_gmt":"2025-06-04T09:52:56","slug":"%e4%bd%bf%e7%94%a8pythonsum%e5%87%bd%e6%95%b8%e8%a8%88%e7%ae%97%e4%b8%b2%e5%88%97%e4%b8%ad%e6%95%b8%e5%80%bc%e5%85%83%e7%b4%a0%e7%9a%84%e7%b8%bd%e5%92%8c","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8pythonsum%e5%87%bd%e6%95%b8%e8%a8%88%e7%ae%97%e4%b8%b2%e5%88%97%e4%b8%ad%e6%95%b8%e5%80%bc%e5%85%83%e7%b4%a0%e7%9a%84%e7%b8%bd%e5%92%8c\/","title":{"rendered":"2025 \u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u4f7f\u7528 sum() \u51fd\u6578\u8a08\u7b97\u4e32\u5217\u4e2d\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, \u4e32\u5217, sum() \u51fd\u6578, \u6578\u503c\u5143\u7d20, \u7e3d\u548c, Python \u6559\u5b78, Python 2025\"><\/p>\n<h2>2025 \u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u5229\u7528 Python \u4e32\u5217(List)\u4f7f\u7528 sum() \u51fd\u6578\u8a08\u7b97\u4e32\u5217\u4e2d\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c<\/h2>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u5730\u958b\u767c\u51fa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5f0f\u3002Python \u4e2d\u7684\u4e32\u5217(List)\u662f\u4e00\u7a2e\u5e38\u7528\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u53ef\u4ee5\u5132\u5b58\u591a\u500b\u5143\u7d20\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528 `sum()` \u51fd\u6578\u4f86\u8a08\u7b97\u4e32\u5217\u4e2d\u6240\u6709\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c\u3002<\/p>\n<p>### \u57fa\u672c\u7528\u6cd5<\/p>\n<p>`sum()` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u4e32\u5217\u4e2d\u6240\u6709\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c\u3002\u5b83\u63a5\u53d7\u4e00\u500b\u53c3\u6578\uff0c\u8a72\u53c3\u6578\u662f\u4e00\u500b\u4e32\u5217\uff0c\u4e26\u5c07\u4e32\u5217\u4e2d\u6240\u6709\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c\u50b3\u56de\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u4e32\u5217\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u6578\u5b57\uff1a<\/p>\n<p>&#8220;`python<br \/>\nnumbers = [1, 2, 3, 4, 5]\n&#8220;`<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `sum()` \u51fd\u6578\u4f86\u8a08\u7b97\u9019\u500b\u4e32\u5217\u4e2d\u6240\u6709\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntotal = sum(numbers)<br \/>\nprint(total)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u5c07\u6703\u5f97\u5230\u4ee5\u4e0b\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\n15<br \/>\n&#8220;`<\/p>\n<p>\u56e0\u6b64\uff0c\u6211\u5011\u53ef\u4ee5\u5f97\u77e5 `numbers` \u4e32\u5217\u4e2d\u6240\u6709\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c\u70ba 15\u3002<\/p>\n<p>### \u4f7f\u7528\u7b2c\u4e8c\u500b\u53c3\u6578<\/p>\n<p>`sum()` \u51fd\u6578\u9084\u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e8c\u500b\u53c3\u6578\uff0c\u8a72\u53c3\u6578\u662f\u4e00\u500b\u6578\u503c\uff0c\u7528\u4f86\u6307\u5b9a\u5f9e\u4e32\u5217\u4e2d\u8a08\u7b97\u7e3d\u548c\u7684\u958b\u59cb\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u60f3\u8981\u5f9e `numbers` \u4e32\u5217\u7684\u7b2c\u4e8c\u500b\u5143\u7d20\u958b\u59cb\u8a08\u7b97\u7e3d\u548c\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntotal = sum(numbers, 2)<br \/>\nprint(total)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u5c07\u6703\u5f97\u5230\u4ee5\u4e0b\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\n14<br \/>\n&#8220;`<\/p>\n<p>\u56e0\u6b64\uff0c\u6211\u5011\u53ef\u4ee5\u5f97\u77e5 `numbers` \u4e32\u5217\u4e2d\u5f9e\u7b2c\u4e8c\u500b\u5143\u7d20\u958b\u59cb\u7684\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c\u70ba 14\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `sum()` \u51fd\u6578\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e7e\u7a2e\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **TypeError\uff1aunsupported operand type(s)**<br \/>\n   \u7576\u4e32\u5217\u4e2d\u5305\u542b\u975e\u6578\u503c\u985e\u578b\u6642\uff0c\u6703\u5c0e\u81f4\u6b64\u932f\u8aa4\u3002\u8acb\u78ba\u4fdd\u4e32\u5217\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u6578\u5b57\u3002<\/p>\n<p>   &#8220;`python<br \/>\n   mixed_list = [1, 2, &#8216;three&#8217;, 4]\n   total = sum(mixed_list)  # \u9019\u6703\u5f15\u767c TypeError<br \/>\n   &#8220;`<\/p>\n<p>2. **\u7a7a\u4e32\u5217\u7684\u8655\u7406**<br \/>\n   \u5982\u679c\u50b3\u5165\u7684\u662f\u4e00\u500b\u7a7a\u4e32\u5217\uff0c`sum()` \u6703\u8fd4\u56de 0\uff0c\u9019\u662f\u5408\u4e4e\u9810\u671f\u7684\u884c\u70ba\u3002<\/p>\n<p>   &#8220;`python<br \/>\n   empty_list = []\n   total = sum(empty_list)  # \u8fd4\u56de 0<br \/>\n   &#8220;`<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`sum()` \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u4e32\u5217\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u5176\u4ed6\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff0c\u4f8b\u5982\u5143\u7d44\u548c\u96c6\u5408\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntuple_numbers = (1, 2, 3, 4, 5)<br \/>\ntotal = sum(tuple_numbers)<br \/>\nprint(total)  # \u8f38\u51fa: 15<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u7d50\u5408\u5217\u8868\u63a8\u5c0e\u5f0f\u4f86\u8a08\u7b97\u7279\u5b9a\u689d\u4ef6\u4e0b\u7684\u7e3d\u548c\uff0c\u4f8b\u5982\u8a08\u7b97\u6240\u6709\u5076\u6578\u7684\u7e3d\u548c\uff1a<\/p>\n<p>&#8220;`python<br \/>\neven_sum = sum(num for num in numbers if num % 2 == 0)<br \/>\nprint(even_sum)  # \u8f38\u51fa: 6<br \/>\n&#8220;`<\/p>\n<p>### \u7d50\u8ad6<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u4e2d\u7684 `sum()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8f15\u9b06\u8a08\u7b97\u4e32\u5217\u4e2d\u6240\u6709\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c\u3002\u5b83\u7684\u9748\u6d3b\u6027\u4f7f\u5176\u9069\u7528\u65bc\u5404\u7a2e\u60c5\u5883\uff0c\u4e26\u4e14\u7c21\u5316\u4e86\u6578\u64da\u8655\u7406\u7684\u904e\u7a0b\u3002\u82e5\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u4f7f\u7528\u6280\u5de7\uff0c\u6b61\u8fce\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78](https:\/\/vocus.cc)\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: sum() \u51fd\u6578\u53ef\u4ee5\u8655\u7406\u975e\u6578\u5b57\u985e\u578b\u55ce\uff1f**<br \/>\nA1: \u4e0d\u53ef\u4ee5\uff0c`sum()` \u51fd\u6578\u50c5\u652f\u6301\u6578\u5b57\u985e\u578b\u7684\u5143\u7d20\u3002\u5982\u679c\u4e32\u5217\u4e2d\u5305\u542b\u975e\u6578\u5b57\u985e\u578b\uff0c\u6703\u5f15\u767c TypeError\u3002<\/p>\n<p>**Q2: \u5982\u679c\u8981\u8a08\u7b97\u4e32\u5217\u4e2d\u6240\u6709\u8ca0\u6578\u7684\u7e3d\u548c\uff0c\u8a72\u600e\u9ebc\u505a\uff1f**<br \/>\nA2: \u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5c0e\u5f0f\u4f86\u7be9\u9078\u8ca0\u6578\uff0c\u7136\u5f8c\u518d\u4f7f\u7528 `sum()` \u51fd\u6578\u8a08\u7b97\uff0c\u4f8b\u5982\uff1a`total = sum(num for num in numbers if num < 0)`\u3002\n\n**Q3: sum() \u51fd\u6578\u7684\u6027\u80fd\u5982\u4f55\uff1f**  \nA3: `sum()` \u51fd\u6578\u5728\u8a08\u7b97\u5927\u91cf\u6578\u64da\u6642\u8868\u73fe\u826f\u597d\uff0c\u56e0\u70ba\u5b83\u662f\u5728 C \u8a9e\u8a00\u5c64\u9762\u5be6\u73fe\u7684\uff0c\u901a\u5e38\u6bd4\u624b\u52d5\u8fed\u4ee3\u66f4\u5feb\u3002\u4e0d\u904e\uff0c\u6578\u64da\u91cf\u975e\u5e38\u9f90\u5927\u7684\u60c5\u6cc1\u4e0b\uff0c\u9084\u662f\u5efa\u8b70\u8003\u616e\u4f7f\u7528 NumPy \u7b49\u5eab\u4f86\u9032\u884c\u66f4\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u3002\n---\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aPython\u63d0\u4f9b\u4e86sum()\u51fd\u6578\uff0c\u53ef\u4ee5\u8f15\u9b06\u8a08\u7b97\u4e32\u5217\u4e2d\u6578\u503c\u5143\u7d20\u7684\u7e3d\u548c\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u8a08\u7b97\u51fa\u7d50\u679c\uff0c\u6e1b\u5c11\u958b\u767c\u6642\u9593\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-2696","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-Hu","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2696","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=2696"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2696\/revisions"}],"predecessor-version":[{"id":2697,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2696\/revisions\/2697"}],"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=2696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}