[{"data":1,"prerenderedAt":2089},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-sum-function-explained":3},{"id":4,"title":5,"body":6,"description":2082,"extension":2083,"meta":2084,"navigation":296,"path":2085,"seo":2086,"stem":2087,"__hash__":2088},"content\u002Freference\u002Fpython-sum-function-explained.md","Python sum() Function Explained",{"type":7,"value":8,"toc":2032},"minimark",[9,19,30,37,42,131,137,144,149,152,175,178,182,210,228,243,247,250,309,312,321,324,342,349,352,409,411,420,423,446,449,455,460,465,511,513,522,526,570,572,581,588,594,623,625,634,640,644,658,706,708,717,722,728,733,737,740,791,794,843,845,854,864,868,871,921,926,930,933,976,979,990,994,999,1003,1006,1025,1028,1040,1043,1064,1066,1075,1079,1081,1132,1135,1218,1220,1229,1232,1236,1238,1285,1288,1359,1361,1369,1376,1378,1461,1467,1546,1548,1557,1564,1570,1575,1580,1632,1636,1718,1721,1727,1738,1741,1752,1763,1767,1773,1798,1804,1887,1890,1916,1920,1927,1930,1937,1948,1954,1957,1964,1970,1980,1983,1987,2028],[10,11,13,14,18],"h1",{"id":12},"python-sum-function-explained","Python ",[15,16,17],"code",{},"sum()"," Function Explained",[20,21,22,23,25,26,29],"p",{},"The built-in ",[15,24,17],{}," function adds numbers from an iterable such as a list, tuple, or ",[15,27,28],{},"range"," object.",[20,31,32,33,36],{},"It is one of the simplest ways to get a total in Python. Beginners often use it to add all numbers in a list, but it also has an optional ",[15,34,35],{},"start"," value and a few important limits.",[38,39,41],"h2",{"id":40},"quick-example","Quick example",[43,44,49],"pre",{"className":45,"code":46,"language":47,"meta":48,"style":48},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","numbers = [1, 2, 3, 4]\ntotal = sum(numbers)\nprint(total)  # 10\n","python","",[15,50,51,91,113],{"__ignoreMap":48},[52,53,56,60,64,68,72,75,78,80,83,85,88],"span",{"class":54,"line":55},"line",1,[52,57,59],{"class":58},"su5hD","numbers ",[52,61,63],{"class":62},"smGrS","=",[52,65,67],{"class":66},"sP7_E"," [",[52,69,71],{"class":70},"srdBf","1",[52,73,74],{"class":66},",",[52,76,77],{"class":70}," 2",[52,79,74],{"class":66},[52,81,82],{"class":70}," 3",[52,84,74],{"class":66},[52,86,87],{"class":70}," 4",[52,89,90],{"class":66},"]\n",[52,92,94,97,99,103,106,110],{"class":54,"line":93},2,[52,95,96],{"class":58},"total ",[52,98,63],{"class":62},[52,100,102],{"class":101},"sptTA"," sum",[52,104,105],{"class":66},"(",[52,107,109],{"class":108},"slqww","numbers",[52,111,112],{"class":66},")\n",[52,114,116,119,121,124,127],{"class":54,"line":115},3,[52,117,118],{"class":101},"print",[52,120,105],{"class":66},[52,122,123],{"class":108},"total",[52,125,126],{"class":66},")",[52,128,130],{"class":129},"sutJx","  # 10\n",[20,132,133,134,136],{},"Use ",[15,135,17],{}," to add numbers from an iterable like a list or tuple.",[38,138,140,141,143],{"id":139},"what-sum-does","What ",[15,142,17],{}," does",[20,145,146,148],{},[15,147,17],{}," adds numeric values from an iterable.",[20,150,151],{},"Common beginner uses include:",[153,154,155,159,162,168],"ul",{},[156,157,158],"li",{},"Adding numbers in a list",[156,160,161],{},"Adding values in a tuple",[156,163,164,165],{},"Adding numbers from ",[15,166,167],{},"range()",[156,169,170,171,174],{},"Counting ",[15,172,173],{},"True"," values in a list of booleans",[20,176,177],{},"It returns the final total as a number.",[38,179,181],{"id":180},"basic-syntax","Basic syntax",[43,183,185],{"className":45,"code":184,"language":47,"meta":48,"style":48},"sum(iterable, start=0)\n",[15,186,187],{"__ignoreMap":48},[52,188,189,192,194,197,199,203,205,208],{"class":54,"line":55},[52,190,191],{"class":101},"sum",[52,193,105],{"class":66},[52,195,196],{"class":108},"iterable",[52,198,74],{"class":66},[52,200,202],{"class":201},"s99_P"," start",[52,204,63],{"class":62},[52,206,207],{"class":70},"0",[52,209,112],{"class":66},[153,211,212,217,222],{},[156,213,214,216],{},[15,215,196],{}," is the group of values to add",[156,218,219,221],{},[15,220,35],{}," is an optional value added before the iterable values",[156,223,224,225],{},"Most of the time, beginners only need ",[15,226,227],{},"sum(iterable)",[20,229,230,231,234,235,242],{},"If you are new to the word ",[232,233,196],"em",{},", think of it as a group of values you can loop through, such as a list, tuple, or ",[236,237,239,241],"a",{"href":238},"\u002Freference\u002Fpython-range-function-explained\u002F",[15,240,167],{}," object",".",[38,244,246],{"id":245},"simple-example","Simple example",[20,248,249],{},"Here is a basic example with a list of integers:",[43,251,253],{"className":45,"code":252,"language":47,"meta":48,"style":48},"numbers = [5, 10, 15]\ntotal = sum(numbers)\n\nprint(total)\n",[15,254,255,278,292,298],{"__ignoreMap":48},[52,256,257,259,261,263,266,268,271,273,276],{"class":54,"line":55},[52,258,59],{"class":58},[52,260,63],{"class":62},[52,262,67],{"class":66},[52,264,265],{"class":70},"5",[52,267,74],{"class":66},[52,269,270],{"class":70}," 10",[52,272,74],{"class":66},[52,274,275],{"class":70}," 15",[52,277,90],{"class":66},[52,279,280,282,284,286,288,290],{"class":54,"line":93},[52,281,96],{"class":58},[52,283,63],{"class":62},[52,285,102],{"class":101},[52,287,105],{"class":66},[52,289,109],{"class":108},[52,291,112],{"class":66},[52,293,294],{"class":54,"line":115},[52,295,297],{"emptyLinePlaceholder":296},true,"\n",[52,299,301,303,305,307],{"class":54,"line":300},4,[52,302,118],{"class":101},[52,304,105],{"class":66},[52,306,123],{"class":108},[52,308,112],{"class":66},[20,310,311],{},"Output:",[43,313,315],{"className":45,"code":314,"language":47,"meta":48,"style":48},"30\n",[15,316,317],{"__ignoreMap":48},[52,318,319],{"class":54,"line":55},[52,320,314],{"class":70},[20,322,323],{},"What happens here:",[153,325,326,331,337],{},[156,327,328,330],{},[15,329,109],{}," contains three integers",[156,332,333,336],{},[15,334,335],{},"sum(numbers)"," adds them together",[156,338,339,340],{},"The result is stored in ",[15,341,123],{},[38,343,345,346,348],{"id":344},"using-the-start-value","Using the ",[15,347,35],{}," value",[20,350,351],{},"The second argument lets you begin with an extra value before Python adds the iterable items.",[43,353,355],{"className":45,"code":354,"language":47,"meta":48,"style":48},"numbers = [1, 2, 3]\ntotal = sum(numbers, 10)\n\nprint(total)\n",[15,356,357,377,395,399],{"__ignoreMap":48},[52,358,359,361,363,365,367,369,371,373,375],{"class":54,"line":55},[52,360,59],{"class":58},[52,362,63],{"class":62},[52,364,67],{"class":66},[52,366,71],{"class":70},[52,368,74],{"class":66},[52,370,77],{"class":70},[52,372,74],{"class":66},[52,374,82],{"class":70},[52,376,90],{"class":66},[52,378,379,381,383,385,387,389,391,393],{"class":54,"line":93},[52,380,96],{"class":58},[52,382,63],{"class":62},[52,384,102],{"class":101},[52,386,105],{"class":66},[52,388,109],{"class":108},[52,390,74],{"class":66},[52,392,270],{"class":70},[52,394,112],{"class":66},[52,396,397],{"class":54,"line":115},[52,398,297],{"emptyLinePlaceholder":296},[52,400,401,403,405,407],{"class":54,"line":300},[52,402,118],{"class":101},[52,404,105],{"class":66},[52,406,123],{"class":108},[52,408,112],{"class":66},[20,410,311],{},[43,412,414],{"className":45,"code":413,"language":47,"meta":48,"style":48},"16\n",[15,415,416],{"__ignoreMap":48},[52,417,418],{"class":54,"line":55},[52,419,413],{"class":70},[20,421,422],{},"This works like:",[153,424,425,431,436,441],{},[156,426,427,428],{},"Start at ",[15,429,430],{},"10",[156,432,433,434],{},"Add ",[15,435,71],{},[156,437,433,438],{},[15,439,440],{},"2",[156,442,433,443],{},[15,444,445],{},"3",[20,447,448],{},"This is useful when you already have a starting total.",[38,450,140,452,454],{"id":451},"what-sum-can-work-with",[15,453,17],{}," can work with",[20,456,457,459],{},[15,458,17],{}," works with numeric values.",[461,462,464],"h3",{"id":463},"integers","Integers",[43,466,468],{"className":45,"code":467,"language":47,"meta":48,"style":48},"values = [1, 2, 3, 4]\nprint(sum(values))\n",[15,469,470,495],{"__ignoreMap":48},[52,471,472,475,477,479,481,483,485,487,489,491,493],{"class":54,"line":55},[52,473,474],{"class":58},"values ",[52,476,63],{"class":62},[52,478,67],{"class":66},[52,480,71],{"class":70},[52,482,74],{"class":66},[52,484,77],{"class":70},[52,486,74],{"class":66},[52,488,82],{"class":70},[52,490,74],{"class":66},[52,492,87],{"class":70},[52,494,90],{"class":66},[52,496,497,499,501,503,505,508],{"class":54,"line":93},[52,498,118],{"class":101},[52,500,105],{"class":66},[52,502,191],{"class":101},[52,504,105],{"class":66},[52,506,507],{"class":108},"values",[52,509,510],{"class":66},"))\n",[20,512,311],{},[43,514,516],{"className":45,"code":515,"language":47,"meta":48,"style":48},"10\n",[15,517,518],{"__ignoreMap":48},[52,519,520],{"class":54,"line":55},[52,521,515],{"class":70},[461,523,525],{"id":524},"floats","Floats",[43,527,529],{"className":45,"code":528,"language":47,"meta":48,"style":48},"prices = [2.5, 3.75, 1.25]\nprint(sum(prices))\n",[15,530,531,555],{"__ignoreMap":48},[52,532,533,536,538,540,543,545,548,550,553],{"class":54,"line":55},[52,534,535],{"class":58},"prices ",[52,537,63],{"class":62},[52,539,67],{"class":66},[52,541,542],{"class":70},"2.5",[52,544,74],{"class":66},[52,546,547],{"class":70}," 3.75",[52,549,74],{"class":66},[52,551,552],{"class":70}," 1.25",[52,554,90],{"class":66},[52,556,557,559,561,563,565,568],{"class":54,"line":93},[52,558,118],{"class":101},[52,560,105],{"class":66},[52,562,191],{"class":101},[52,564,105],{"class":66},[52,566,567],{"class":108},"prices",[52,569,510],{"class":66},[20,571,311],{},[43,573,575],{"className":45,"code":574,"language":47,"meta":48,"style":48},"7.5\n",[15,576,577],{"__ignoreMap":48},[52,578,579],{"class":54,"line":55},[52,580,574],{"class":70},[20,582,583,584,242],{},"If you need a refresher on numeric types, see ",[236,585,587],{"href":586},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex\u002F","Python numbers explained: int and float",[461,589,591,592],{"id":590},"values-from-range","Values from ",[15,593,167],{},[43,595,597],{"className":45,"code":596,"language":47,"meta":48,"style":48},"print(sum(range(1, 6)))\n",[15,598,599],{"__ignoreMap":48},[52,600,601,603,605,607,609,611,613,615,617,620],{"class":54,"line":55},[52,602,118],{"class":101},[52,604,105],{"class":66},[52,606,191],{"class":101},[52,608,105],{"class":66},[52,610,28],{"class":101},[52,612,105],{"class":66},[52,614,71],{"class":70},[52,616,74],{"class":66},[52,618,619],{"class":70}," 6",[52,621,622],{"class":66},")))\n",[20,624,311],{},[43,626,628],{"className":45,"code":627,"language":47,"meta":48,"style":48},"15\n",[15,629,630],{"__ignoreMap":48},[52,631,632],{"class":54,"line":55},[52,633,627],{"class":70},[20,635,636,637,242],{},"This adds ",[15,638,639],{},"1 + 2 + 3 + 4 + 5",[461,641,643],{"id":642},"boolean-values","Boolean values",[20,645,646,647,649,650,652,653,649,656,242],{},"Booleans can also be summed because ",[15,648,173],{}," acts like ",[15,651,71],{}," and ",[15,654,655],{},"False",[15,657,207],{},[43,659,661],{"className":45,"code":660,"language":47,"meta":48,"style":48},"results = [True, False, True, True]\nprint(sum(results))\n",[15,662,663,691],{"__ignoreMap":48},[52,664,665,668,670,672,675,677,680,682,685,687,689],{"class":54,"line":55},[52,666,667],{"class":58},"results ",[52,669,63],{"class":62},[52,671,67],{"class":66},[52,673,173],{"class":674},"s39Yj",[52,676,74],{"class":66},[52,678,679],{"class":674}," False",[52,681,74],{"class":66},[52,683,684],{"class":674}," True",[52,686,74],{"class":66},[52,688,684],{"class":674},[52,690,90],{"class":66},[52,692,693,695,697,699,701,704],{"class":54,"line":93},[52,694,118],{"class":101},[52,696,105],{"class":66},[52,698,191],{"class":101},[52,700,105],{"class":66},[52,702,703],{"class":108},"results",[52,705,510],{"class":66},[20,707,311],{},[43,709,711],{"className":45,"code":710,"language":47,"meta":48,"style":48},"3\n",[15,712,713],{"__ignoreMap":48},[52,714,715],{"class":54,"line":55},[52,716,710],{"class":70},[20,718,719,720,242],{},"This is a simple way to count how many values are ",[15,721,173],{},[38,723,140,725,727],{"id":724},"what-sum-does-not-work-with",[15,726,17],{}," does not work with",[20,729,730,732],{},[15,731,17],{}," is for numeric addition. It is not for joining text or combining lists.",[461,734,736],{"id":735},"it-does-not-join-strings","It does not join strings",[20,738,739],{},"This causes an error:",[43,741,743],{"className":45,"code":742,"language":47,"meta":48,"style":48},"words = [\"hello\", \"world\"]\nprint(sum(words))\n",[15,744,745,776],{"__ignoreMap":48},[52,746,747,750,752,754,758,762,764,766,769,772,774],{"class":54,"line":55},[52,748,749],{"class":58},"words ",[52,751,63],{"class":62},[52,753,67],{"class":66},[52,755,757],{"class":756},"sjJ54","\"",[52,759,761],{"class":760},"s_sjI","hello",[52,763,757],{"class":756},[52,765,74],{"class":66},[52,767,768],{"class":756}," \"",[52,770,771],{"class":760},"world",[52,773,757],{"class":756},[52,775,90],{"class":66},[52,777,778,780,782,784,786,789],{"class":54,"line":93},[52,779,118],{"class":101},[52,781,105],{"class":66},[52,783,191],{"class":101},[52,785,105],{"class":66},[52,787,788],{"class":108},"words",[52,790,510],{"class":66},[20,792,793],{},"Use string joining instead:",[43,795,797],{"className":45,"code":796,"language":47,"meta":48,"style":48},"words = [\"hello\", \"world\"]\nprint(\"\".join(words))\n",[15,798,799,823],{"__ignoreMap":48},[52,800,801,803,805,807,809,811,813,815,817,819,821],{"class":54,"line":55},[52,802,749],{"class":58},[52,804,63],{"class":62},[52,806,67],{"class":66},[52,808,757],{"class":756},[52,810,761],{"class":760},[52,812,757],{"class":756},[52,814,74],{"class":66},[52,816,768],{"class":756},[52,818,771],{"class":760},[52,820,757],{"class":756},[52,822,90],{"class":66},[52,824,825,827,829,832,834,837,839,841],{"class":54,"line":93},[52,826,118],{"class":101},[52,828,105],{"class":66},[52,830,831],{"class":756},"\"\"",[52,833,242],{"class":66},[52,835,836],{"class":108},"join",[52,838,105],{"class":66},[52,840,788],{"class":108},[52,842,510],{"class":66},[20,844,311],{},[43,846,848],{"className":45,"code":847,"language":47,"meta":48,"style":48},"helloworld\n",[15,849,850],{"__ignoreMap":48},[52,851,852],{"class":54,"line":55},[52,853,847],{"class":58},[20,855,856,857,242],{},"If you need to turn values into text first, see ",[236,858,860,863],{"href":859},"\u002Freference\u002Fpython-str-function-explained\u002F",[15,861,862],{},"str()"," explained",[461,865,867],{"id":866},"it-does-not-combine-lists","It does not combine lists",[20,869,870],{},"This also causes an error:",[43,872,874],{"className":45,"code":873,"language":47,"meta":48,"style":48},"lists = [[1, 2], [3, 4]]\nprint(sum(lists))\n",[15,875,876,906],{"__ignoreMap":48},[52,877,878,881,883,886,888,890,892,895,897,899,901,903],{"class":54,"line":55},[52,879,880],{"class":58},"lists ",[52,882,63],{"class":62},[52,884,885],{"class":66}," [[",[52,887,71],{"class":70},[52,889,74],{"class":66},[52,891,77],{"class":70},[52,893,894],{"class":66},"],",[52,896,67],{"class":66},[52,898,445],{"class":70},[52,900,74],{"class":66},[52,902,87],{"class":70},[52,904,905],{"class":66},"]]\n",[52,907,908,910,912,914,916,919],{"class":54,"line":93},[52,909,118],{"class":101},[52,911,105],{"class":66},[52,913,191],{"class":101},[52,915,105],{"class":66},[52,917,918],{"class":108},"lists",[52,920,510],{"class":66},[20,922,923,925],{},[15,924,17],{}," is not the right tool for combining lists.",[461,927,929],{"id":928},"it-fails-with-mixed-incompatible-types","It fails with mixed incompatible types",[20,931,932],{},"For example:",[43,934,936],{"className":45,"code":935,"language":47,"meta":48,"style":48},"values = [1, 2, \"3\"]\nprint(sum(values))\n",[15,937,938,962],{"__ignoreMap":48},[52,939,940,942,944,946,948,950,952,954,956,958,960],{"class":54,"line":55},[52,941,474],{"class":58},[52,943,63],{"class":62},[52,945,67],{"class":66},[52,947,71],{"class":70},[52,949,74],{"class":66},[52,951,77],{"class":70},[52,953,74],{"class":66},[52,955,768],{"class":756},[52,957,445],{"class":760},[52,959,757],{"class":756},[52,961,90],{"class":66},[52,963,964,966,968,970,972,974],{"class":54,"line":93},[52,965,118],{"class":101},[52,967,105],{"class":66},[52,969,191],{"class":101},[52,971,105],{"class":66},[52,973,507],{"class":108},[52,975,510],{"class":66},[20,977,978],{},"This fails because Python cannot add integers and strings together.",[20,980,981,982,985,986,242],{},"If you have number strings such as ",[15,983,984],{},"\"3\"",", convert them first. See ",[236,987,989],{"href":988},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python\u002F","how to convert a string to an int in Python",[38,991,993],{"id":992},"common-errors-and-fixes","Common errors and fixes",[20,995,996,997,242],{},"Here are some of the most common beginner mistakes with ",[15,998,17],{},[461,1000,1002],{"id":1001},"passing-a-single-number-instead-of-an-iterable","Passing a single number instead of an iterable",[20,1004,1005],{},"This is wrong:",[43,1007,1009],{"className":45,"code":1008,"language":47,"meta":48,"style":48},"print(sum(5))\n",[15,1010,1011],{"__ignoreMap":48},[52,1012,1013,1015,1017,1019,1021,1023],{"class":54,"line":55},[52,1014,118],{"class":101},[52,1016,105],{"class":66},[52,1018,191],{"class":101},[52,1020,105],{"class":66},[52,1022,265],{"class":70},[52,1024,510],{"class":66},[20,1026,1027],{},"Why it fails:",[153,1029,1030,1035],{},[156,1031,1032,1034],{},[15,1033,265],{}," is a single integer",[156,1036,1037,1039],{},[15,1038,17],{}," expects an iterable, not one number",[20,1041,1042],{},"Use an iterable instead:",[43,1044,1046],{"className":45,"code":1045,"language":47,"meta":48,"style":48},"print(sum([5]))\n",[15,1047,1048],{"__ignoreMap":48},[52,1049,1050,1052,1054,1056,1059,1061],{"class":54,"line":55},[52,1051,118],{"class":101},[52,1053,105],{"class":66},[52,1055,191],{"class":101},[52,1057,1058],{"class":66},"([",[52,1060,265],{"class":70},[52,1062,1063],{"class":66},"]))\n",[20,1065,311],{},[43,1067,1069],{"className":45,"code":1068,"language":47,"meta":48,"style":48},"5\n",[15,1070,1071],{"__ignoreMap":48},[52,1072,1073],{"class":54,"line":55},[52,1074,1068],{"class":70},[461,1076,1078],{"id":1077},"trying-to-sum-strings","Trying to sum strings",[20,1080,1005],{},[43,1082,1084],{"className":45,"code":1083,"language":47,"meta":48,"style":48},"values = [\"1\", \"2\", \"3\"]\nprint(sum(values))\n",[15,1085,1086,1118],{"__ignoreMap":48},[52,1087,1088,1090,1092,1094,1096,1098,1100,1102,1104,1106,1108,1110,1112,1114,1116],{"class":54,"line":55},[52,1089,474],{"class":58},[52,1091,63],{"class":62},[52,1093,67],{"class":66},[52,1095,757],{"class":756},[52,1097,71],{"class":760},[52,1099,757],{"class":756},[52,1101,74],{"class":66},[52,1103,768],{"class":756},[52,1105,440],{"class":760},[52,1107,757],{"class":756},[52,1109,74],{"class":66},[52,1111,768],{"class":756},[52,1113,445],{"class":760},[52,1115,757],{"class":756},[52,1117,90],{"class":66},[52,1119,1120,1122,1124,1126,1128,1130],{"class":54,"line":93},[52,1121,118],{"class":101},[52,1123,105],{"class":66},[52,1125,191],{"class":101},[52,1127,105],{"class":66},[52,1129,507],{"class":108},[52,1131,510],{"class":66},[20,1133,1134],{},"Fix it by converting the strings to integers:",[43,1136,1138],{"className":45,"code":1137,"language":47,"meta":48,"style":48},"values = [\"1\", \"2\", \"3\"]\ntotal = sum(int(x) for x in values)\nprint(total)\n",[15,1139,1140,1172,1208],{"__ignoreMap":48},[52,1141,1142,1144,1146,1148,1150,1152,1154,1156,1158,1160,1162,1164,1166,1168,1170],{"class":54,"line":55},[52,1143,474],{"class":58},[52,1145,63],{"class":62},[52,1147,67],{"class":66},[52,1149,757],{"class":756},[52,1151,71],{"class":760},[52,1153,757],{"class":756},[52,1155,74],{"class":66},[52,1157,768],{"class":756},[52,1159,440],{"class":760},[52,1161,757],{"class":756},[52,1163,74],{"class":66},[52,1165,768],{"class":756},[52,1167,445],{"class":760},[52,1169,757],{"class":756},[52,1171,90],{"class":66},[52,1173,1174,1176,1178,1180,1182,1186,1188,1191,1193,1197,1200,1203,1206],{"class":54,"line":93},[52,1175,96],{"class":58},[52,1177,63],{"class":62},[52,1179,102],{"class":101},[52,1181,105],{"class":66},[52,1183,1185],{"class":1184},"sZMiF","int",[52,1187,105],{"class":66},[52,1189,1190],{"class":108},"x",[52,1192,126],{"class":66},[52,1194,1196],{"class":1195},"sVHd0"," for",[52,1198,1199],{"class":108}," x ",[52,1201,1202],{"class":1195},"in",[52,1204,1205],{"class":108}," values",[52,1207,112],{"class":66},[52,1209,1210,1212,1214,1216],{"class":54,"line":115},[52,1211,118],{"class":101},[52,1213,105],{"class":66},[52,1215,123],{"class":108},[52,1217,112],{"class":66},[20,1219,311],{},[43,1221,1223],{"className":45,"code":1222,"language":47,"meta":48,"style":48},"6\n",[15,1224,1225],{"__ignoreMap":48},[52,1226,1227],{"class":54,"line":55},[52,1228,1222],{"class":70},[20,1230,1231],{},"This kind of problem is related to TypeError with unsupported operand types.",[461,1233,1235],{"id":1234},"mixing-numbers-and-strings","Mixing numbers and strings",[20,1237,1005],{},[43,1239,1241],{"className":45,"code":1240,"language":47,"meta":48,"style":48},"values = [1, 2, \"3\", 4]\nprint(sum(values))\n",[15,1242,1243,1271],{"__ignoreMap":48},[52,1244,1245,1247,1249,1251,1253,1255,1257,1259,1261,1263,1265,1267,1269],{"class":54,"line":55},[52,1246,474],{"class":58},[52,1248,63],{"class":62},[52,1250,67],{"class":66},[52,1252,71],{"class":70},[52,1254,74],{"class":66},[52,1256,77],{"class":70},[52,1258,74],{"class":66},[52,1260,768],{"class":756},[52,1262,445],{"class":760},[52,1264,757],{"class":756},[52,1266,74],{"class":66},[52,1268,87],{"class":70},[52,1270,90],{"class":66},[52,1272,1273,1275,1277,1279,1281,1283],{"class":54,"line":93},[52,1274,118],{"class":101},[52,1276,105],{"class":66},[52,1278,191],{"class":101},[52,1280,105],{"class":66},[52,1282,507],{"class":108},[52,1284,510],{"class":66},[20,1286,1287],{},"Fix it by making all values numeric:",[43,1289,1291],{"className":45,"code":1290,"language":47,"meta":48,"style":48},"values = [1, 2, \"3\", 4]\ntotal = sum(int(x) for x in values)\nprint(total)\n",[15,1292,1293,1321,1349],{"__ignoreMap":48},[52,1294,1295,1297,1299,1301,1303,1305,1307,1309,1311,1313,1315,1317,1319],{"class":54,"line":55},[52,1296,474],{"class":58},[52,1298,63],{"class":62},[52,1300,67],{"class":66},[52,1302,71],{"class":70},[52,1304,74],{"class":66},[52,1306,77],{"class":70},[52,1308,74],{"class":66},[52,1310,768],{"class":756},[52,1312,445],{"class":760},[52,1314,757],{"class":756},[52,1316,74],{"class":66},[52,1318,87],{"class":70},[52,1320,90],{"class":66},[52,1322,1323,1325,1327,1329,1331,1333,1335,1337,1339,1341,1343,1345,1347],{"class":54,"line":93},[52,1324,96],{"class":58},[52,1326,63],{"class":62},[52,1328,102],{"class":101},[52,1330,105],{"class":66},[52,1332,1185],{"class":1184},[52,1334,105],{"class":66},[52,1336,1190],{"class":108},[52,1338,126],{"class":66},[52,1340,1196],{"class":1195},[52,1342,1199],{"class":108},[52,1344,1202],{"class":1195},[52,1346,1205],{"class":108},[52,1348,112],{"class":66},[52,1350,1351,1353,1355,1357],{"class":54,"line":115},[52,1352,118],{"class":101},[52,1354,105],{"class":66},[52,1356,123],{"class":108},[52,1358,112],{"class":66},[20,1360,311],{},[43,1362,1363],{"className":45,"code":515,"language":47,"meta":48,"style":48},[15,1364,1365],{"__ignoreMap":48},[52,1366,1367],{"class":54,"line":55},[52,1368,515],{"class":70},[461,1370,1372,1373,1375],{"id":1371},"using-sum-to-join-text","Using ",[15,1374,17],{}," to join text",[20,1377,1005],{},[43,1379,1381],{"className":45,"code":1380,"language":47,"meta":48,"style":48},"letters = [\"P\", \"y\", \"t\", \"h\", \"o\", \"n\"]\nprint(sum(letters))\n",[15,1382,1383,1446],{"__ignoreMap":48},[52,1384,1385,1388,1390,1392,1394,1397,1399,1401,1403,1406,1408,1410,1412,1415,1417,1419,1421,1424,1426,1428,1430,1433,1435,1437,1439,1442,1444],{"class":54,"line":55},[52,1386,1387],{"class":58},"letters ",[52,1389,63],{"class":62},[52,1391,67],{"class":66},[52,1393,757],{"class":756},[52,1395,1396],{"class":760},"P",[52,1398,757],{"class":756},[52,1400,74],{"class":66},[52,1402,768],{"class":756},[52,1404,1405],{"class":760},"y",[52,1407,757],{"class":756},[52,1409,74],{"class":66},[52,1411,768],{"class":756},[52,1413,1414],{"class":760},"t",[52,1416,757],{"class":756},[52,1418,74],{"class":66},[52,1420,768],{"class":756},[52,1422,1423],{"class":760},"h",[52,1425,757],{"class":756},[52,1427,74],{"class":66},[52,1429,768],{"class":756},[52,1431,1432],{"class":760},"o",[52,1434,757],{"class":756},[52,1436,74],{"class":66},[52,1438,768],{"class":756},[52,1440,1441],{"class":760},"n",[52,1443,757],{"class":756},[52,1445,90],{"class":66},[52,1447,1448,1450,1452,1454,1456,1459],{"class":54,"line":93},[52,1449,118],{"class":101},[52,1451,105],{"class":66},[52,1453,191],{"class":101},[52,1455,105],{"class":66},[52,1457,1458],{"class":108},"letters",[52,1460,510],{"class":66},[20,1462,133,1463,1466],{},[15,1464,1465],{},"join()"," instead:",[43,1468,1470],{"className":45,"code":1469,"language":47,"meta":48,"style":48},"letters = [\"P\", \"y\", \"t\", \"h\", \"o\", \"n\"]\nprint(\"\".join(letters))\n",[15,1471,1472,1528],{"__ignoreMap":48},[52,1473,1474,1476,1478,1480,1482,1484,1486,1488,1490,1492,1494,1496,1498,1500,1502,1504,1506,1508,1510,1512,1514,1516,1518,1520,1522,1524,1526],{"class":54,"line":55},[52,1475,1387],{"class":58},[52,1477,63],{"class":62},[52,1479,67],{"class":66},[52,1481,757],{"class":756},[52,1483,1396],{"class":760},[52,1485,757],{"class":756},[52,1487,74],{"class":66},[52,1489,768],{"class":756},[52,1491,1405],{"class":760},[52,1493,757],{"class":756},[52,1495,74],{"class":66},[52,1497,768],{"class":756},[52,1499,1414],{"class":760},[52,1501,757],{"class":756},[52,1503,74],{"class":66},[52,1505,768],{"class":756},[52,1507,1423],{"class":760},[52,1509,757],{"class":756},[52,1511,74],{"class":66},[52,1513,768],{"class":756},[52,1515,1432],{"class":760},[52,1517,757],{"class":756},[52,1519,74],{"class":66},[52,1521,768],{"class":756},[52,1523,1441],{"class":760},[52,1525,757],{"class":756},[52,1527,90],{"class":66},[52,1529,1530,1532,1534,1536,1538,1540,1542,1544],{"class":54,"line":93},[52,1531,118],{"class":101},[52,1533,105],{"class":66},[52,1535,831],{"class":756},[52,1537,242],{"class":66},[52,1539,836],{"class":108},[52,1541,105],{"class":66},[52,1543,1458],{"class":108},[52,1545,510],{"class":66},[20,1547,311],{},[43,1549,1551],{"className":45,"code":1550,"language":47,"meta":48,"style":48},"Python\n",[15,1552,1553],{"__ignoreMap":48},[52,1554,1555],{"class":54,"line":55},[52,1556,1550],{"class":58},[20,1558,1559,1560,242],{},"If you see string-related errors while building text, you may also want to read ",[236,1561,1563],{"href":1562},"\u002Ferrors\u002Ftypeerror-sequence-item-0-expected-str-instance-fix\u002F","TypeError: sequence item 0 expected str instance",[38,1565,1567,1569],{"id":1566},"sum-vs-manual-loop",[15,1568,17],{}," vs manual loop",[20,1571,1572,1574],{},[15,1573,17],{}," is often better when you only need the total.",[461,1576,1372,1578],{"id":1577},"using-sum",[15,1579,17],{},[43,1581,1583],{"className":45,"code":1582,"language":47,"meta":48,"style":48},"numbers = [4, 7, 9]\ntotal = sum(numbers)\nprint(total)\n",[15,1584,1585,1608,1622],{"__ignoreMap":48},[52,1586,1587,1589,1591,1593,1596,1598,1601,1603,1606],{"class":54,"line":55},[52,1588,59],{"class":58},[52,1590,63],{"class":62},[52,1592,67],{"class":66},[52,1594,1595],{"class":70},"4",[52,1597,74],{"class":66},[52,1599,1600],{"class":70}," 7",[52,1602,74],{"class":66},[52,1604,1605],{"class":70}," 9",[52,1607,90],{"class":66},[52,1609,1610,1612,1614,1616,1618,1620],{"class":54,"line":93},[52,1611,96],{"class":58},[52,1613,63],{"class":62},[52,1615,102],{"class":101},[52,1617,105],{"class":66},[52,1619,109],{"class":108},[52,1621,112],{"class":66},[52,1623,1624,1626,1628,1630],{"class":54,"line":115},[52,1625,118],{"class":101},[52,1627,105],{"class":66},[52,1629,123],{"class":108},[52,1631,112],{"class":66},[461,1633,1635],{"id":1634},"using-a-manual-loop","Using a manual loop",[43,1637,1639],{"className":45,"code":1638,"language":47,"meta":48,"style":48},"numbers = [4, 7, 9]\ntotal = 0\n\nfor number in numbers:\n    total += number\n\nprint(total)\n",[15,1640,1641,1661,1670,1674,1690,1702,1707],{"__ignoreMap":48},[52,1642,1643,1645,1647,1649,1651,1653,1655,1657,1659],{"class":54,"line":55},[52,1644,59],{"class":58},[52,1646,63],{"class":62},[52,1648,67],{"class":66},[52,1650,1595],{"class":70},[52,1652,74],{"class":66},[52,1654,1600],{"class":70},[52,1656,74],{"class":66},[52,1658,1605],{"class":70},[52,1660,90],{"class":66},[52,1662,1663,1665,1667],{"class":54,"line":93},[52,1664,96],{"class":58},[52,1666,63],{"class":62},[52,1668,1669],{"class":70}," 0\n",[52,1671,1672],{"class":54,"line":115},[52,1673,297],{"emptyLinePlaceholder":296},[52,1675,1676,1679,1682,1684,1687],{"class":54,"line":300},[52,1677,1678],{"class":1195},"for",[52,1680,1681],{"class":58}," number ",[52,1683,1202],{"class":1195},[52,1685,1686],{"class":58}," numbers",[52,1688,1689],{"class":66},":\n",[52,1691,1693,1696,1699],{"class":54,"line":1692},5,[52,1694,1695],{"class":58},"    total ",[52,1697,1698],{"class":62},"+=",[52,1700,1701],{"class":58}," number\n",[52,1703,1705],{"class":54,"line":1704},6,[52,1706,297],{"emptyLinePlaceholder":296},[52,1708,1710,1712,1714,1716],{"class":54,"line":1709},7,[52,1711,118],{"class":101},[52,1713,105],{"class":66},[52,1715,123],{"class":108},[52,1717,112],{"class":66},[20,1719,1720],{},"Both produce the same result.",[20,1722,1723,1724,1726],{},"Why ",[15,1725,17],{}," is often better:",[153,1728,1729,1732,1735],{},[156,1730,1731],{},"Shorter code",[156,1733,1734],{},"Easier to read",[156,1736,1737],{},"Clear intent: you want a total",[20,1739,1740],{},"Why a manual loop is sometimes better:",[153,1742,1743,1746,1749],{},[156,1744,1745],{},"You need to skip some values",[156,1747,1748],{},"You need extra checks",[156,1750,1751],{},"You want to change values before adding them",[20,1753,1754,1755,1759,1760,1762],{},"For example, if you only want certain items, you might first ",[236,1756,1758],{"href":1757},"\u002Fhow-to\u002Fhow-to-filter-a-list-in-python\u002F","filter a list in Python"," and then use ",[15,1761,17],{}," on the filtered result.",[38,1764,1766],{"id":1765},"common-mistakes","Common mistakes",[20,1768,1769,1770,1772],{},"These are the most common causes of problems with ",[15,1771,17],{},":",[153,1774,1775,1781,1788,1795],{},[156,1776,1777,1778],{},"Passing a number instead of an iterable, such as ",[15,1779,1780],{},"sum(5)",[156,1782,1783,1784,1787],{},"Trying to sum strings like ",[15,1785,1786],{},"['1', '2', '3']"," without converting them",[156,1789,1372,1790,1792,1793],{},[15,1791,17],{}," to join text instead of using string ",[15,1794,1465],{},[156,1796,1797],{},"Mixing numbers and strings in the same iterable",[20,1799,1800,1801,1803],{},"If ",[15,1802,17],{}," is failing, these quick checks can help:",[43,1805,1807],{"className":45,"code":1806,"language":47,"meta":48,"style":48},"print(values)\nprint(type(values))\nprint([type(x) for x in values])\nprint(sum([int(x) for x in values]))\n",[15,1808,1809,1819,1834,1859],{"__ignoreMap":48},[52,1810,1811,1813,1815,1817],{"class":54,"line":55},[52,1812,118],{"class":101},[52,1814,105],{"class":66},[52,1816,507],{"class":108},[52,1818,112],{"class":66},[52,1820,1821,1823,1825,1828,1830,1832],{"class":54,"line":93},[52,1822,118],{"class":101},[52,1824,105],{"class":66},[52,1826,1827],{"class":1184},"type",[52,1829,105],{"class":66},[52,1831,507],{"class":108},[52,1833,510],{"class":66},[52,1835,1836,1838,1840,1842,1844,1846,1848,1850,1852,1854,1856],{"class":54,"line":115},[52,1837,118],{"class":101},[52,1839,1058],{"class":66},[52,1841,1827],{"class":1184},[52,1843,105],{"class":66},[52,1845,1190],{"class":108},[52,1847,126],{"class":66},[52,1849,1196],{"class":1195},[52,1851,1199],{"class":108},[52,1853,1202],{"class":1195},[52,1855,1205],{"class":108},[52,1857,1858],{"class":66},"])\n",[52,1860,1861,1863,1865,1867,1869,1871,1873,1875,1877,1879,1881,1883,1885],{"class":54,"line":300},[52,1862,118],{"class":101},[52,1864,105],{"class":66},[52,1866,191],{"class":101},[52,1868,1058],{"class":66},[52,1870,1185],{"class":1184},[52,1872,105],{"class":66},[52,1874,1190],{"class":108},[52,1876,126],{"class":66},[52,1878,1196],{"class":1195},[52,1880,1199],{"class":108},[52,1882,1202],{"class":1195},[52,1884,1205],{"class":108},[52,1886,1063],{"class":66},[20,1888,1889],{},"What these checks do:",[153,1891,1892,1898,1904,1910],{},[156,1893,1894,1897],{},[15,1895,1896],{},"print(values)"," shows the actual data",[156,1899,1900,1903],{},[15,1901,1902],{},"print(type(values))"," shows whether the main object is a list, tuple, or something else",[156,1905,1906,1909],{},[15,1907,1908],{},"print([type(x) for x in values])"," shows the type of each item",[156,1911,1912,1915],{},[15,1913,1914],{},"print(sum([int(x) for x in values]))"," tests whether conversion to integers fixes the problem",[38,1917,1919],{"id":1918},"faq","FAQ",[461,1921,1923,1924,1926],{"id":1922},"what-does-sum-return-in-python","What does ",[15,1925,17],{}," return in Python?",[20,1928,1929],{},"It returns the total of the numeric values in an iterable.",[461,1931,1933,1934,1936],{"id":1932},"can-sum-add-strings-in-python","Can ",[15,1935,17],{}," add strings in Python?",[20,1938,1939,1940,1943,1944,1947],{},"No. Use ",[15,1941,1942],{},"''.join(...)"," or ",[15,1945,1946],{},"'separator'.join(...)"," for strings.",[461,1949,1933,1951,1953],{"id":1950},"can-sum-work-with-floats",[15,1952,17],{}," work with floats?",[20,1955,1956],{},"Yes. It can add both integers and floats.",[461,1958,1960,1961,1963],{"id":1959},"what-is-the-second-argument-in-sum","What is the second argument in ",[15,1962,17],{},"?",[20,1965,1966,1967,1969],{},"It is the optional ",[15,1968,35],{}," value added before the iterable values are summed.",[461,1971,1973,1974,1976,1977,1963],{"id":1972},"why-does-sum-give-a-typeerror","Why does ",[15,1975,17],{}," give a ",[15,1978,1979],{},"TypeError",[20,1981,1982],{},"Usually because one or more values are not numbers, or because the argument is not an iterable.",[38,1984,1986],{"id":1985},"see-also","See also",[153,1988,1989,1993,2000,2008,2014,2019,2024],{},[156,1990,1991],{},[236,1992,587],{"href":586},[156,1994,1995],{},[236,1996,13,1997,1999],{"href":238},[15,1998,167],{}," function explained",[156,2001,2002],{},[236,2003,13,2005,1999],{"href":2004},"\u002Freference\u002Fpython-len-function-explained\u002F",[15,2006,2007],{},"len()",[156,2009,2010],{},[236,2011,13,2012,1999],{"href":859},[15,2013,862],{},[156,2015,2016],{},[236,2017,2018],{"href":1757},"How to filter a list in Python",[156,2020,2021],{},[236,2022,2023],{"href":988},"How to convert a string to an int in Python",[156,2025,2026],{},[236,2027,1563],{"href":1562},[2029,2030,2031],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}",{"title":48,"searchDepth":93,"depth":93,"links":2033},[2034,2035,2037,2038,2039,2041,2049,2055,2062,2068,2069,2081],{"id":40,"depth":93,"text":41},{"id":139,"depth":93,"text":2036},"What sum() does",{"id":180,"depth":93,"text":181},{"id":245,"depth":93,"text":246},{"id":344,"depth":93,"text":2040},"Using the start value",{"id":451,"depth":93,"text":2042,"children":2043},"What sum() can work with",[2044,2045,2046,2048],{"id":463,"depth":115,"text":464},{"id":524,"depth":115,"text":525},{"id":590,"depth":115,"text":2047},"Values from range()",{"id":642,"depth":115,"text":643},{"id":724,"depth":93,"text":2050,"children":2051},"What sum() does not work with",[2052,2053,2054],{"id":735,"depth":115,"text":736},{"id":866,"depth":115,"text":867},{"id":928,"depth":115,"text":929},{"id":992,"depth":93,"text":993,"children":2056},[2057,2058,2059,2060],{"id":1001,"depth":115,"text":1002},{"id":1077,"depth":115,"text":1078},{"id":1234,"depth":115,"text":1235},{"id":1371,"depth":115,"text":2061},"Using sum() to join text",{"id":1566,"depth":93,"text":2063,"children":2064},"sum() vs manual loop",[2065,2067],{"id":1577,"depth":115,"text":2066},"Using sum()",{"id":1634,"depth":115,"text":1635},{"id":1765,"depth":93,"text":1766},{"id":1918,"depth":93,"text":1919,"children":2070},[2071,2073,2075,2077,2079],{"id":1922,"depth":115,"text":2072},"What does sum() return in Python?",{"id":1932,"depth":115,"text":2074},"Can sum() add strings in Python?",{"id":1950,"depth":115,"text":2076},"Can sum() work with floats?",{"id":1959,"depth":115,"text":2078},"What is the second argument in sum()?",{"id":1972,"depth":115,"text":2080},"Why does sum() give a TypeError?",{"id":1985,"depth":93,"text":1986},"Master python sum function explained in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-sum-function-explained",{"title":5,"description":2082},"reference\u002Fpython-sum-function-explained","pUBJSfP8YGcV7z25xWzkoWEl0bvqGBoLMBf338R4GWY",1777585492836]