[{"data":1,"prerenderedAt":1587},["ShallowReactive",2],{"content-\u002Fcompetitive-programming\u002Fmath-library\u002Fsum":3,"related-\u002Fcompetitive-programming\u002Fmath-library\u002Fsum":1458,"sidebar-content":1562},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"tags":10,"date":14,"weight":15,"body":16,"_type":1452,"_id":1453,"_source":1454,"_file":1455,"_stem":1456,"_extension":1457},"\u002Fcompetitive-programming\u002Fmath-library\u002Fsum","math-library",false,"","Sum Elements","Using for loops to sum elements in Python.",[11,12,13],"python","backend","arrays","2026-03-18",20,{"type":17,"children":18,"toc":1431},"root",[19,28,34,41,203,217,315,321,492,498,612,624,766,779,896,909,1020,1033,1146,1158,1273,1292,1420,1425],{"type":20,"tag":21,"props":22,"children":24},"element","h2",{"id":23},"summing-elements-in-python",[25],{"type":26,"value":27},"text","Summing Elements in Python",{"type":20,"tag":29,"props":30,"children":31},"p",{},[32],{"type":26,"value":33},"In Python, you can sum elements in a list using various methods. Here are some common approaches:",{"type":20,"tag":35,"props":36,"children":38},"h3",{"id":37},"using-a-for-loop",[39],{"type":26,"value":40},"Using a For Loop",{"type":20,"tag":42,"props":43,"children":46},"pre",{"className":44,"code":45,"language":11,"meta":7,"style":7},"language-python shiki shiki-themes github-light github-dark","numbers = [1, 2, 3, 4, 5]\ntotal = 0\nfor number in numbers:\n    total += number\nprint(total)  # Output: 15\n",[47],{"type":20,"tag":48,"props":49,"children":50},"code",{"__ignoreMap":7},[51,122,140,164,183],{"type":20,"tag":52,"props":53,"children":56},"span",{"class":54,"line":55},"line",1,[57,63,69,74,80,85,90,94,99,103,108,112,117],{"type":20,"tag":52,"props":58,"children":60},{"style":59},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[61],{"type":26,"value":62},"numbers ",{"type":20,"tag":52,"props":64,"children":66},{"style":65},"--shiki-default:#D73A49;--shiki-dark:#F97583",[67],{"type":26,"value":68},"=",{"type":20,"tag":52,"props":70,"children":71},{"style":59},[72],{"type":26,"value":73}," [",{"type":20,"tag":52,"props":75,"children":77},{"style":76},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[78],{"type":26,"value":79},"1",{"type":20,"tag":52,"props":81,"children":82},{"style":59},[83],{"type":26,"value":84},", ",{"type":20,"tag":52,"props":86,"children":87},{"style":76},[88],{"type":26,"value":89},"2",{"type":20,"tag":52,"props":91,"children":92},{"style":59},[93],{"type":26,"value":84},{"type":20,"tag":52,"props":95,"children":96},{"style":76},[97],{"type":26,"value":98},"3",{"type":20,"tag":52,"props":100,"children":101},{"style":59},[102],{"type":26,"value":84},{"type":20,"tag":52,"props":104,"children":105},{"style":76},[106],{"type":26,"value":107},"4",{"type":20,"tag":52,"props":109,"children":110},{"style":59},[111],{"type":26,"value":84},{"type":20,"tag":52,"props":113,"children":114},{"style":76},[115],{"type":26,"value":116},"5",{"type":20,"tag":52,"props":118,"children":119},{"style":59},[120],{"type":26,"value":121},"]\n",{"type":20,"tag":52,"props":123,"children":125},{"class":54,"line":124},2,[126,131,135],{"type":20,"tag":52,"props":127,"children":128},{"style":59},[129],{"type":26,"value":130},"total ",{"type":20,"tag":52,"props":132,"children":133},{"style":65},[134],{"type":26,"value":68},{"type":20,"tag":52,"props":136,"children":137},{"style":76},[138],{"type":26,"value":139}," 0\n",{"type":20,"tag":52,"props":141,"children":143},{"class":54,"line":142},3,[144,149,154,159],{"type":20,"tag":52,"props":145,"children":146},{"style":65},[147],{"type":26,"value":148},"for",{"type":20,"tag":52,"props":150,"children":151},{"style":59},[152],{"type":26,"value":153}," number ",{"type":20,"tag":52,"props":155,"children":156},{"style":65},[157],{"type":26,"value":158},"in",{"type":20,"tag":52,"props":160,"children":161},{"style":59},[162],{"type":26,"value":163}," numbers:\n",{"type":20,"tag":52,"props":165,"children":167},{"class":54,"line":166},4,[168,173,178],{"type":20,"tag":52,"props":169,"children":170},{"style":59},[171],{"type":26,"value":172},"    total ",{"type":20,"tag":52,"props":174,"children":175},{"style":65},[176],{"type":26,"value":177},"+=",{"type":20,"tag":52,"props":179,"children":180},{"style":59},[181],{"type":26,"value":182}," number\n",{"type":20,"tag":52,"props":184,"children":186},{"class":54,"line":185},5,[187,192,197],{"type":20,"tag":52,"props":188,"children":189},{"style":76},[190],{"type":26,"value":191},"print",{"type":20,"tag":52,"props":193,"children":194},{"style":59},[195],{"type":26,"value":196},"(total)  ",{"type":20,"tag":52,"props":198,"children":200},{"style":199},"--shiki-default:#6A737D;--shiki-dark:#6A737D",[201],{"type":26,"value":202},"# Output: 15\n",{"type":20,"tag":35,"props":204,"children":206},{"id":205},"using-the-built-in-sum-function",[207,209,215],{"type":26,"value":208},"Using the Built-in ",{"type":20,"tag":48,"props":210,"children":212},{"className":211},[],[213],{"type":26,"value":214},"sum()",{"type":26,"value":216}," Function",{"type":20,"tag":42,"props":218,"children":220},{"className":44,"code":219,"language":11,"meta":7,"style":7},"numbers = [1, 2, 3, 4, 5]\ntotal = sum(numbers)\nprint(total)  # Output: 15\n",[221],{"type":20,"tag":48,"props":222,"children":223},{"__ignoreMap":7},[224,279,300],{"type":20,"tag":52,"props":225,"children":226},{"class":54,"line":55},[227,231,235,239,243,247,251,255,259,263,267,271,275],{"type":20,"tag":52,"props":228,"children":229},{"style":59},[230],{"type":26,"value":62},{"type":20,"tag":52,"props":232,"children":233},{"style":65},[234],{"type":26,"value":68},{"type":20,"tag":52,"props":236,"children":237},{"style":59},[238],{"type":26,"value":73},{"type":20,"tag":52,"props":240,"children":241},{"style":76},[242],{"type":26,"value":79},{"type":20,"tag":52,"props":244,"children":245},{"style":59},[246],{"type":26,"value":84},{"type":20,"tag":52,"props":248,"children":249},{"style":76},[250],{"type":26,"value":89},{"type":20,"tag":52,"props":252,"children":253},{"style":59},[254],{"type":26,"value":84},{"type":20,"tag":52,"props":256,"children":257},{"style":76},[258],{"type":26,"value":98},{"type":20,"tag":52,"props":260,"children":261},{"style":59},[262],{"type":26,"value":84},{"type":20,"tag":52,"props":264,"children":265},{"style":76},[266],{"type":26,"value":107},{"type":20,"tag":52,"props":268,"children":269},{"style":59},[270],{"type":26,"value":84},{"type":20,"tag":52,"props":272,"children":273},{"style":76},[274],{"type":26,"value":116},{"type":20,"tag":52,"props":276,"children":277},{"style":59},[278],{"type":26,"value":121},{"type":20,"tag":52,"props":280,"children":281},{"class":54,"line":124},[282,286,290,295],{"type":20,"tag":52,"props":283,"children":284},{"style":59},[285],{"type":26,"value":130},{"type":20,"tag":52,"props":287,"children":288},{"style":65},[289],{"type":26,"value":68},{"type":20,"tag":52,"props":291,"children":292},{"style":76},[293],{"type":26,"value":294}," sum",{"type":20,"tag":52,"props":296,"children":297},{"style":59},[298],{"type":26,"value":299},"(numbers)\n",{"type":20,"tag":52,"props":301,"children":302},{"class":54,"line":142},[303,307,311],{"type":20,"tag":52,"props":304,"children":305},{"style":76},[306],{"type":26,"value":191},{"type":20,"tag":52,"props":308,"children":309},{"style":59},[310],{"type":26,"value":196},{"type":20,"tag":52,"props":312,"children":313},{"style":199},[314],{"type":26,"value":202},{"type":20,"tag":35,"props":316,"children":318},{"id":317},"using-a-while-loop",[319],{"type":26,"value":320},"Using a While Loop",{"type":20,"tag":42,"props":322,"children":324},{"className":44,"code":323,"language":11,"meta":7,"style":7},"numbers = [1, 2, 3, 4, 5]\ntotal = 0\nindex = 0\nwhile index \u003C len(numbers):\n    total += numbers[index]\n    index += 1\nprint(total)  # Output: 15\n",[325],{"type":20,"tag":48,"props":326,"children":327},{"__ignoreMap":7},[328,383,398,414,442,458,476],{"type":20,"tag":52,"props":329,"children":330},{"class":54,"line":55},[331,335,339,343,347,351,355,359,363,367,371,375,379],{"type":20,"tag":52,"props":332,"children":333},{"style":59},[334],{"type":26,"value":62},{"type":20,"tag":52,"props":336,"children":337},{"style":65},[338],{"type":26,"value":68},{"type":20,"tag":52,"props":340,"children":341},{"style":59},[342],{"type":26,"value":73},{"type":20,"tag":52,"props":344,"children":345},{"style":76},[346],{"type":26,"value":79},{"type":20,"tag":52,"props":348,"children":349},{"style":59},[350],{"type":26,"value":84},{"type":20,"tag":52,"props":352,"children":353},{"style":76},[354],{"type":26,"value":89},{"type":20,"tag":52,"props":356,"children":357},{"style":59},[358],{"type":26,"value":84},{"type":20,"tag":52,"props":360,"children":361},{"style":76},[362],{"type":26,"value":98},{"type":20,"tag":52,"props":364,"children":365},{"style":59},[366],{"type":26,"value":84},{"type":20,"tag":52,"props":368,"children":369},{"style":76},[370],{"type":26,"value":107},{"type":20,"tag":52,"props":372,"children":373},{"style":59},[374],{"type":26,"value":84},{"type":20,"tag":52,"props":376,"children":377},{"style":76},[378],{"type":26,"value":116},{"type":20,"tag":52,"props":380,"children":381},{"style":59},[382],{"type":26,"value":121},{"type":20,"tag":52,"props":384,"children":385},{"class":54,"line":124},[386,390,394],{"type":20,"tag":52,"props":387,"children":388},{"style":59},[389],{"type":26,"value":130},{"type":20,"tag":52,"props":391,"children":392},{"style":65},[393],{"type":26,"value":68},{"type":20,"tag":52,"props":395,"children":396},{"style":76},[397],{"type":26,"value":139},{"type":20,"tag":52,"props":399,"children":400},{"class":54,"line":142},[401,406,410],{"type":20,"tag":52,"props":402,"children":403},{"style":59},[404],{"type":26,"value":405},"index ",{"type":20,"tag":52,"props":407,"children":408},{"style":65},[409],{"type":26,"value":68},{"type":20,"tag":52,"props":411,"children":412},{"style":76},[413],{"type":26,"value":139},{"type":20,"tag":52,"props":415,"children":416},{"class":54,"line":166},[417,422,427,432,437],{"type":20,"tag":52,"props":418,"children":419},{"style":65},[420],{"type":26,"value":421},"while",{"type":20,"tag":52,"props":423,"children":424},{"style":59},[425],{"type":26,"value":426}," index ",{"type":20,"tag":52,"props":428,"children":429},{"style":65},[430],{"type":26,"value":431},"\u003C",{"type":20,"tag":52,"props":433,"children":434},{"style":76},[435],{"type":26,"value":436}," len",{"type":20,"tag":52,"props":438,"children":439},{"style":59},[440],{"type":26,"value":441},"(numbers):\n",{"type":20,"tag":52,"props":443,"children":444},{"class":54,"line":185},[445,449,453],{"type":20,"tag":52,"props":446,"children":447},{"style":59},[448],{"type":26,"value":172},{"type":20,"tag":52,"props":450,"children":451},{"style":65},[452],{"type":26,"value":177},{"type":20,"tag":52,"props":454,"children":455},{"style":59},[456],{"type":26,"value":457}," numbers[index]\n",{"type":20,"tag":52,"props":459,"children":461},{"class":54,"line":460},6,[462,467,471],{"type":20,"tag":52,"props":463,"children":464},{"style":59},[465],{"type":26,"value":466},"    index ",{"type":20,"tag":52,"props":468,"children":469},{"style":65},[470],{"type":26,"value":177},{"type":20,"tag":52,"props":472,"children":473},{"style":76},[474],{"type":26,"value":475}," 1\n",{"type":20,"tag":52,"props":477,"children":479},{"class":54,"line":478},7,[480,484,488],{"type":20,"tag":52,"props":481,"children":482},{"style":76},[483],{"type":26,"value":191},{"type":20,"tag":52,"props":485,"children":486},{"style":59},[487],{"type":26,"value":196},{"type":20,"tag":52,"props":489,"children":490},{"style":199},[491],{"type":26,"value":202},{"type":20,"tag":35,"props":493,"children":495},{"id":494},"using-list-comprehension",[496],{"type":26,"value":497},"Using List Comprehension",{"type":20,"tag":42,"props":499,"children":501},{"className":44,"code":500,"language":11,"meta":7,"style":7},"numbers = [1, 2, 3, 4, 5]\ntotal = sum([number for number in numbers])\nprint(total)  # Output: 15\n",[502],{"type":20,"tag":48,"props":503,"children":504},{"__ignoreMap":7},[505,560,597],{"type":20,"tag":52,"props":506,"children":507},{"class":54,"line":55},[508,512,516,520,524,528,532,536,540,544,548,552,556],{"type":20,"tag":52,"props":509,"children":510},{"style":59},[511],{"type":26,"value":62},{"type":20,"tag":52,"props":513,"children":514},{"style":65},[515],{"type":26,"value":68},{"type":20,"tag":52,"props":517,"children":518},{"style":59},[519],{"type":26,"value":73},{"type":20,"tag":52,"props":521,"children":522},{"style":76},[523],{"type":26,"value":79},{"type":20,"tag":52,"props":525,"children":526},{"style":59},[527],{"type":26,"value":84},{"type":20,"tag":52,"props":529,"children":530},{"style":76},[531],{"type":26,"value":89},{"type":20,"tag":52,"props":533,"children":534},{"style":59},[535],{"type":26,"value":84},{"type":20,"tag":52,"props":537,"children":538},{"style":76},[539],{"type":26,"value":98},{"type":20,"tag":52,"props":541,"children":542},{"style":59},[543],{"type":26,"value":84},{"type":20,"tag":52,"props":545,"children":546},{"style":76},[547],{"type":26,"value":107},{"type":20,"tag":52,"props":549,"children":550},{"style":59},[551],{"type":26,"value":84},{"type":20,"tag":52,"props":553,"children":554},{"style":76},[555],{"type":26,"value":116},{"type":20,"tag":52,"props":557,"children":558},{"style":59},[559],{"type":26,"value":121},{"type":20,"tag":52,"props":561,"children":562},{"class":54,"line":124},[563,567,571,575,580,584,588,592],{"type":20,"tag":52,"props":564,"children":565},{"style":59},[566],{"type":26,"value":130},{"type":20,"tag":52,"props":568,"children":569},{"style":65},[570],{"type":26,"value":68},{"type":20,"tag":52,"props":572,"children":573},{"style":76},[574],{"type":26,"value":294},{"type":20,"tag":52,"props":576,"children":577},{"style":59},[578],{"type":26,"value":579},"([number ",{"type":20,"tag":52,"props":581,"children":582},{"style":65},[583],{"type":26,"value":148},{"type":20,"tag":52,"props":585,"children":586},{"style":59},[587],{"type":26,"value":153},{"type":20,"tag":52,"props":589,"children":590},{"style":65},[591],{"type":26,"value":158},{"type":20,"tag":52,"props":593,"children":594},{"style":59},[595],{"type":26,"value":596}," numbers])\n",{"type":20,"tag":52,"props":598,"children":599},{"class":54,"line":142},[600,604,608],{"type":20,"tag":52,"props":601,"children":602},{"style":76},[603],{"type":26,"value":191},{"type":20,"tag":52,"props":605,"children":606},{"style":59},[607],{"type":26,"value":196},{"type":20,"tag":52,"props":609,"children":610},{"style":199},[611],{"type":26,"value":202},{"type":20,"tag":35,"props":613,"children":615},{"id":614},"using-functoolsreduce",[616,618],{"type":26,"value":617},"Using ",{"type":20,"tag":48,"props":619,"children":621},{"className":620},[],[622],{"type":26,"value":623},"functools.reduce",{"type":20,"tag":42,"props":625,"children":627},{"className":44,"code":626,"language":11,"meta":7,"style":7},"from functools import reduce\nnumbers = [1, 2, 3, 4, 5]\ntotal = reduce(lambda x, y: x + y, numbers)\nprint(total)  # Output: 15\n",[628],{"type":20,"tag":48,"props":629,"children":630},{"__ignoreMap":7},[631,655,710,751],{"type":20,"tag":52,"props":632,"children":633},{"class":54,"line":55},[634,639,644,649],{"type":20,"tag":52,"props":635,"children":636},{"style":65},[637],{"type":26,"value":638},"from",{"type":20,"tag":52,"props":640,"children":641},{"style":59},[642],{"type":26,"value":643}," functools ",{"type":20,"tag":52,"props":645,"children":646},{"style":65},[647],{"type":26,"value":648},"import",{"type":20,"tag":52,"props":650,"children":652},{"style":651},"--shiki-default:#E36209;--shiki-dark:#FFAB70",[653],{"type":26,"value":654}," reduce\n",{"type":20,"tag":52,"props":656,"children":657},{"class":54,"line":124},[658,662,666,670,674,678,682,686,690,694,698,702,706],{"type":20,"tag":52,"props":659,"children":660},{"style":59},[661],{"type":26,"value":62},{"type":20,"tag":52,"props":663,"children":664},{"style":65},[665],{"type":26,"value":68},{"type":20,"tag":52,"props":667,"children":668},{"style":59},[669],{"type":26,"value":73},{"type":20,"tag":52,"props":671,"children":672},{"style":76},[673],{"type":26,"value":79},{"type":20,"tag":52,"props":675,"children":676},{"style":59},[677],{"type":26,"value":84},{"type":20,"tag":52,"props":679,"children":680},{"style":76},[681],{"type":26,"value":89},{"type":20,"tag":52,"props":683,"children":684},{"style":59},[685],{"type":26,"value":84},{"type":20,"tag":52,"props":687,"children":688},{"style":76},[689],{"type":26,"value":98},{"type":20,"tag":52,"props":691,"children":692},{"style":59},[693],{"type":26,"value":84},{"type":20,"tag":52,"props":695,"children":696},{"style":76},[697],{"type":26,"value":107},{"type":20,"tag":52,"props":699,"children":700},{"style":59},[701],{"type":26,"value":84},{"type":20,"tag":52,"props":703,"children":704},{"style":76},[705],{"type":26,"value":116},{"type":20,"tag":52,"props":707,"children":708},{"style":59},[709],{"type":26,"value":121},{"type":20,"tag":52,"props":711,"children":712},{"class":54,"line":142},[713,717,721,726,731,736,741,746],{"type":20,"tag":52,"props":714,"children":715},{"style":59},[716],{"type":26,"value":130},{"type":20,"tag":52,"props":718,"children":719},{"style":65},[720],{"type":26,"value":68},{"type":20,"tag":52,"props":722,"children":723},{"style":651},[724],{"type":26,"value":725}," reduce",{"type":20,"tag":52,"props":727,"children":728},{"style":59},[729],{"type":26,"value":730},"(",{"type":20,"tag":52,"props":732,"children":733},{"style":65},[734],{"type":26,"value":735},"lambda",{"type":20,"tag":52,"props":737,"children":738},{"style":59},[739],{"type":26,"value":740}," x, y: x ",{"type":20,"tag":52,"props":742,"children":743},{"style":65},[744],{"type":26,"value":745},"+",{"type":20,"tag":52,"props":747,"children":748},{"style":59},[749],{"type":26,"value":750}," y, numbers)\n",{"type":20,"tag":52,"props":752,"children":753},{"class":54,"line":166},[754,758,762],{"type":20,"tag":52,"props":755,"children":756},{"style":76},[757],{"type":26,"value":191},{"type":20,"tag":52,"props":759,"children":760},{"style":59},[761],{"type":26,"value":196},{"type":20,"tag":52,"props":763,"children":764},{"style":199},[765],{"type":26,"value":202},{"type":20,"tag":35,"props":767,"children":769},{"id":768},"using-numpy-library",[770,771,777],{"type":26,"value":617},{"type":20,"tag":48,"props":772,"children":774},{"className":773},[],[775],{"type":26,"value":776},"numpy",{"type":26,"value":778}," Library",{"type":20,"tag":42,"props":780,"children":782},{"className":44,"code":781,"language":11,"meta":7,"style":7},"import numpy as np\nnumbers = np.array([1, 2, 3, 4, 5])\ntotal = np.sum(numbers)\nprint(total)  # Output: 15\n",[783],{"type":20,"tag":48,"props":784,"children":785},{"__ignoreMap":7},[786,808,865,881],{"type":20,"tag":52,"props":787,"children":788},{"class":54,"line":55},[789,793,798,803],{"type":20,"tag":52,"props":790,"children":791},{"style":65},[792],{"type":26,"value":648},{"type":20,"tag":52,"props":794,"children":795},{"style":59},[796],{"type":26,"value":797}," numpy ",{"type":20,"tag":52,"props":799,"children":800},{"style":65},[801],{"type":26,"value":802},"as",{"type":20,"tag":52,"props":804,"children":805},{"style":59},[806],{"type":26,"value":807}," np\n",{"type":20,"tag":52,"props":809,"children":810},{"class":54,"line":124},[811,815,819,824,828,832,836,840,844,848,852,856,860],{"type":20,"tag":52,"props":812,"children":813},{"style":59},[814],{"type":26,"value":62},{"type":20,"tag":52,"props":816,"children":817},{"style":65},[818],{"type":26,"value":68},{"type":20,"tag":52,"props":820,"children":821},{"style":59},[822],{"type":26,"value":823}," np.array([",{"type":20,"tag":52,"props":825,"children":826},{"style":76},[827],{"type":26,"value":79},{"type":20,"tag":52,"props":829,"children":830},{"style":59},[831],{"type":26,"value":84},{"type":20,"tag":52,"props":833,"children":834},{"style":76},[835],{"type":26,"value":89},{"type":20,"tag":52,"props":837,"children":838},{"style":59},[839],{"type":26,"value":84},{"type":20,"tag":52,"props":841,"children":842},{"style":76},[843],{"type":26,"value":98},{"type":20,"tag":52,"props":845,"children":846},{"style":59},[847],{"type":26,"value":84},{"type":20,"tag":52,"props":849,"children":850},{"style":76},[851],{"type":26,"value":107},{"type":20,"tag":52,"props":853,"children":854},{"style":59},[855],{"type":26,"value":84},{"type":20,"tag":52,"props":857,"children":858},{"style":76},[859],{"type":26,"value":116},{"type":20,"tag":52,"props":861,"children":862},{"style":59},[863],{"type":26,"value":864},"])\n",{"type":20,"tag":52,"props":866,"children":867},{"class":54,"line":142},[868,872,876],{"type":20,"tag":52,"props":869,"children":870},{"style":59},[871],{"type":26,"value":130},{"type":20,"tag":52,"props":873,"children":874},{"style":65},[875],{"type":26,"value":68},{"type":20,"tag":52,"props":877,"children":878},{"style":59},[879],{"type":26,"value":880}," np.sum(numbers)\n",{"type":20,"tag":52,"props":882,"children":883},{"class":54,"line":166},[884,888,892],{"type":20,"tag":52,"props":885,"children":886},{"style":76},[887],{"type":26,"value":191},{"type":20,"tag":52,"props":889,"children":890},{"style":59},[891],{"type":26,"value":196},{"type":20,"tag":52,"props":893,"children":894},{"style":199},[895],{"type":26,"value":202},{"type":20,"tag":35,"props":897,"children":899},{"id":898},"using-mathfsum-for-floating-point-precision",[900,901,907],{"type":26,"value":617},{"type":20,"tag":48,"props":902,"children":904},{"className":903},[],[905],{"type":26,"value":906},"math.fsum",{"type":26,"value":908}," for Floating Point Precision",{"type":20,"tag":42,"props":910,"children":912},{"className":44,"code":911,"language":11,"meta":7,"style":7},"import math\nnumbers = [1.0, 2.0, 3.0, 4.0, 5.0]\ntotal = math.fsum(numbers)\nprint(total)  # Output: 15.0\n",[913],{"type":20,"tag":48,"props":914,"children":915},{"__ignoreMap":7},[916,928,988,1004],{"type":20,"tag":52,"props":917,"children":918},{"class":54,"line":55},[919,923],{"type":20,"tag":52,"props":920,"children":921},{"style":65},[922],{"type":26,"value":648},{"type":20,"tag":52,"props":924,"children":925},{"style":59},[926],{"type":26,"value":927}," math\n",{"type":20,"tag":52,"props":929,"children":930},{"class":54,"line":124},[931,935,939,943,948,952,957,961,966,970,975,979,984],{"type":20,"tag":52,"props":932,"children":933},{"style":59},[934],{"type":26,"value":62},{"type":20,"tag":52,"props":936,"children":937},{"style":65},[938],{"type":26,"value":68},{"type":20,"tag":52,"props":940,"children":941},{"style":59},[942],{"type":26,"value":73},{"type":20,"tag":52,"props":944,"children":945},{"style":76},[946],{"type":26,"value":947},"1.0",{"type":20,"tag":52,"props":949,"children":950},{"style":59},[951],{"type":26,"value":84},{"type":20,"tag":52,"props":953,"children":954},{"style":76},[955],{"type":26,"value":956},"2.0",{"type":20,"tag":52,"props":958,"children":959},{"style":59},[960],{"type":26,"value":84},{"type":20,"tag":52,"props":962,"children":963},{"style":76},[964],{"type":26,"value":965},"3.0",{"type":20,"tag":52,"props":967,"children":968},{"style":59},[969],{"type":26,"value":84},{"type":20,"tag":52,"props":971,"children":972},{"style":76},[973],{"type":26,"value":974},"4.0",{"type":20,"tag":52,"props":976,"children":977},{"style":59},[978],{"type":26,"value":84},{"type":20,"tag":52,"props":980,"children":981},{"style":76},[982],{"type":26,"value":983},"5.0",{"type":20,"tag":52,"props":985,"children":986},{"style":59},[987],{"type":26,"value":121},{"type":20,"tag":52,"props":989,"children":990},{"class":54,"line":142},[991,995,999],{"type":20,"tag":52,"props":992,"children":993},{"style":59},[994],{"type":26,"value":130},{"type":20,"tag":52,"props":996,"children":997},{"style":65},[998],{"type":26,"value":68},{"type":20,"tag":52,"props":1000,"children":1001},{"style":59},[1002],{"type":26,"value":1003}," math.fsum(numbers)\n",{"type":20,"tag":52,"props":1005,"children":1006},{"class":54,"line":166},[1007,1011,1015],{"type":20,"tag":52,"props":1008,"children":1009},{"style":76},[1010],{"type":26,"value":191},{"type":20,"tag":52,"props":1012,"children":1013},{"style":59},[1014],{"type":26,"value":196},{"type":20,"tag":52,"props":1016,"children":1017},{"style":199},[1018],{"type":26,"value":1019},"# Output: 15.0\n",{"type":20,"tag":35,"props":1021,"children":1023},{"id":1022},"using-itertoolsaccumulate-for-cumulative-sums",[1024,1025,1031],{"type":26,"value":617},{"type":20,"tag":48,"props":1026,"children":1028},{"className":1027},[],[1029],{"type":26,"value":1030},"itertools.accumulate",{"type":26,"value":1032}," for Cumulative Sums",{"type":20,"tag":42,"props":1034,"children":1036},{"className":44,"code":1035,"language":11,"meta":7,"style":7},"import itertools\nnumbers = [1, 2, 3, 4, 5]\ncumulative_sums = list(itertools.accumulate(numbers))\nprint(cumulative_sums)  # Output: [1, 3, 6, 10, 15]\n",[1037],{"type":20,"tag":48,"props":1038,"children":1039},{"__ignoreMap":7},[1040,1052,1107,1129],{"type":20,"tag":52,"props":1041,"children":1042},{"class":54,"line":55},[1043,1047],{"type":20,"tag":52,"props":1044,"children":1045},{"style":65},[1046],{"type":26,"value":648},{"type":20,"tag":52,"props":1048,"children":1049},{"style":59},[1050],{"type":26,"value":1051}," itertools\n",{"type":20,"tag":52,"props":1053,"children":1054},{"class":54,"line":124},[1055,1059,1063,1067,1071,1075,1079,1083,1087,1091,1095,1099,1103],{"type":20,"tag":52,"props":1056,"children":1057},{"style":59},[1058],{"type":26,"value":62},{"type":20,"tag":52,"props":1060,"children":1061},{"style":65},[1062],{"type":26,"value":68},{"type":20,"tag":52,"props":1064,"children":1065},{"style":59},[1066],{"type":26,"value":73},{"type":20,"tag":52,"props":1068,"children":1069},{"style":76},[1070],{"type":26,"value":79},{"type":20,"tag":52,"props":1072,"children":1073},{"style":59},[1074],{"type":26,"value":84},{"type":20,"tag":52,"props":1076,"children":1077},{"style":76},[1078],{"type":26,"value":89},{"type":20,"tag":52,"props":1080,"children":1081},{"style":59},[1082],{"type":26,"value":84},{"type":20,"tag":52,"props":1084,"children":1085},{"style":76},[1086],{"type":26,"value":98},{"type":20,"tag":52,"props":1088,"children":1089},{"style":59},[1090],{"type":26,"value":84},{"type":20,"tag":52,"props":1092,"children":1093},{"style":76},[1094],{"type":26,"value":107},{"type":20,"tag":52,"props":1096,"children":1097},{"style":59},[1098],{"type":26,"value":84},{"type":20,"tag":52,"props":1100,"children":1101},{"style":76},[1102],{"type":26,"value":116},{"type":20,"tag":52,"props":1104,"children":1105},{"style":59},[1106],{"type":26,"value":121},{"type":20,"tag":52,"props":1108,"children":1109},{"class":54,"line":142},[1110,1115,1119,1124],{"type":20,"tag":52,"props":1111,"children":1112},{"style":59},[1113],{"type":26,"value":1114},"cumulative_sums ",{"type":20,"tag":52,"props":1116,"children":1117},{"style":65},[1118],{"type":26,"value":68},{"type":20,"tag":52,"props":1120,"children":1121},{"style":76},[1122],{"type":26,"value":1123}," list",{"type":20,"tag":52,"props":1125,"children":1126},{"style":59},[1127],{"type":26,"value":1128},"(itertools.accumulate(numbers))\n",{"type":20,"tag":52,"props":1130,"children":1131},{"class":54,"line":166},[1132,1136,1141],{"type":20,"tag":52,"props":1133,"children":1134},{"style":76},[1135],{"type":26,"value":191},{"type":20,"tag":52,"props":1137,"children":1138},{"style":59},[1139],{"type":26,"value":1140},"(cumulative_sums)  ",{"type":20,"tag":52,"props":1142,"children":1143},{"style":199},[1144],{"type":26,"value":1145},"# Output: [1, 3, 6, 10, 15]\n",{"type":20,"tag":35,"props":1147,"children":1149},{"id":1148},"using-pandas-library",[1150,1151,1157],{"type":26,"value":617},{"type":20,"tag":48,"props":1152,"children":1154},{"className":1153},[],[1155],{"type":26,"value":1156},"pandas",{"type":26,"value":778},{"type":20,"tag":42,"props":1159,"children":1161},{"className":44,"code":1160,"language":11,"meta":7,"style":7},"import pandas as pd\nnumbers = pd.Series([1, 2, 3, 4, 5])\ntotal = numbers.sum()\nprint(total)  # Output: 15\n",[1162],{"type":20,"tag":48,"props":1163,"children":1164},{"__ignoreMap":7},[1165,1186,1242,1258],{"type":20,"tag":52,"props":1166,"children":1167},{"class":54,"line":55},[1168,1172,1177,1181],{"type":20,"tag":52,"props":1169,"children":1170},{"style":65},[1171],{"type":26,"value":648},{"type":20,"tag":52,"props":1173,"children":1174},{"style":59},[1175],{"type":26,"value":1176}," pandas ",{"type":20,"tag":52,"props":1178,"children":1179},{"style":65},[1180],{"type":26,"value":802},{"type":20,"tag":52,"props":1182,"children":1183},{"style":59},[1184],{"type":26,"value":1185}," pd\n",{"type":20,"tag":52,"props":1187,"children":1188},{"class":54,"line":124},[1189,1193,1197,1202,1206,1210,1214,1218,1222,1226,1230,1234,1238],{"type":20,"tag":52,"props":1190,"children":1191},{"style":59},[1192],{"type":26,"value":62},{"type":20,"tag":52,"props":1194,"children":1195},{"style":65},[1196],{"type":26,"value":68},{"type":20,"tag":52,"props":1198,"children":1199},{"style":59},[1200],{"type":26,"value":1201}," pd.Series([",{"type":20,"tag":52,"props":1203,"children":1204},{"style":76},[1205],{"type":26,"value":79},{"type":20,"tag":52,"props":1207,"children":1208},{"style":59},[1209],{"type":26,"value":84},{"type":20,"tag":52,"props":1211,"children":1212},{"style":76},[1213],{"type":26,"value":89},{"type":20,"tag":52,"props":1215,"children":1216},{"style":59},[1217],{"type":26,"value":84},{"type":20,"tag":52,"props":1219,"children":1220},{"style":76},[1221],{"type":26,"value":98},{"type":20,"tag":52,"props":1223,"children":1224},{"style":59},[1225],{"type":26,"value":84},{"type":20,"tag":52,"props":1227,"children":1228},{"style":76},[1229],{"type":26,"value":107},{"type":20,"tag":52,"props":1231,"children":1232},{"style":59},[1233],{"type":26,"value":84},{"type":20,"tag":52,"props":1235,"children":1236},{"style":76},[1237],{"type":26,"value":116},{"type":20,"tag":52,"props":1239,"children":1240},{"style":59},[1241],{"type":26,"value":864},{"type":20,"tag":52,"props":1243,"children":1244},{"class":54,"line":142},[1245,1249,1253],{"type":20,"tag":52,"props":1246,"children":1247},{"style":59},[1248],{"type":26,"value":130},{"type":20,"tag":52,"props":1250,"children":1251},{"style":65},[1252],{"type":26,"value":68},{"type":20,"tag":52,"props":1254,"children":1255},{"style":59},[1256],{"type":26,"value":1257}," numbers.sum()\n",{"type":20,"tag":52,"props":1259,"children":1260},{"class":54,"line":166},[1261,1265,1269],{"type":20,"tag":52,"props":1262,"children":1263},{"style":76},[1264],{"type":26,"value":191},{"type":20,"tag":52,"props":1266,"children":1267},{"style":59},[1268],{"type":26,"value":196},{"type":20,"tag":52,"props":1270,"children":1271},{"style":199},[1272],{"type":26,"value":202},{"type":20,"tag":35,"props":1274,"children":1276},{"id":1275},"using-reduce-with-operatoradd",[1277,1278,1284,1286],{"type":26,"value":617},{"type":20,"tag":48,"props":1279,"children":1281},{"className":1280},[],[1282],{"type":26,"value":1283},"reduce",{"type":26,"value":1285}," with ",{"type":20,"tag":48,"props":1287,"children":1289},{"className":1288},[],[1290],{"type":26,"value":1291},"operator.add",{"type":20,"tag":42,"props":1293,"children":1295},{"className":44,"code":1294,"language":11,"meta":7,"style":7},"from functools import reduce\nimport operator\nnumbers = [1, 2, 3, 4, 5]\ntotal = reduce(operator.add, numbers)\nprint(total)  # Output: 15\n",[1296],{"type":20,"tag":48,"props":1297,"children":1298},{"__ignoreMap":7},[1299,1318,1330,1385,1405],{"type":20,"tag":52,"props":1300,"children":1301},{"class":54,"line":55},[1302,1306,1310,1314],{"type":20,"tag":52,"props":1303,"children":1304},{"style":65},[1305],{"type":26,"value":638},{"type":20,"tag":52,"props":1307,"children":1308},{"style":59},[1309],{"type":26,"value":643},{"type":20,"tag":52,"props":1311,"children":1312},{"style":65},[1313],{"type":26,"value":648},{"type":20,"tag":52,"props":1315,"children":1316},{"style":651},[1317],{"type":26,"value":654},{"type":20,"tag":52,"props":1319,"children":1320},{"class":54,"line":124},[1321,1325],{"type":20,"tag":52,"props":1322,"children":1323},{"style":65},[1324],{"type":26,"value":648},{"type":20,"tag":52,"props":1326,"children":1327},{"style":59},[1328],{"type":26,"value":1329}," operator\n",{"type":20,"tag":52,"props":1331,"children":1332},{"class":54,"line":142},[1333,1337,1341,1345,1349,1353,1357,1361,1365,1369,1373,1377,1381],{"type":20,"tag":52,"props":1334,"children":1335},{"style":59},[1336],{"type":26,"value":62},{"type":20,"tag":52,"props":1338,"children":1339},{"style":65},[1340],{"type":26,"value":68},{"type":20,"tag":52,"props":1342,"children":1343},{"style":59},[1344],{"type":26,"value":73},{"type":20,"tag":52,"props":1346,"children":1347},{"style":76},[1348],{"type":26,"value":79},{"type":20,"tag":52,"props":1350,"children":1351},{"style":59},[1352],{"type":26,"value":84},{"type":20,"tag":52,"props":1354,"children":1355},{"style":76},[1356],{"type":26,"value":89},{"type":20,"tag":52,"props":1358,"children":1359},{"style":59},[1360],{"type":26,"value":84},{"type":20,"tag":52,"props":1362,"children":1363},{"style":76},[1364],{"type":26,"value":98},{"type":20,"tag":52,"props":1366,"children":1367},{"style":59},[1368],{"type":26,"value":84},{"type":20,"tag":52,"props":1370,"children":1371},{"style":76},[1372],{"type":26,"value":107},{"type":20,"tag":52,"props":1374,"children":1375},{"style":59},[1376],{"type":26,"value":84},{"type":20,"tag":52,"props":1378,"children":1379},{"style":76},[1380],{"type":26,"value":116},{"type":20,"tag":52,"props":1382,"children":1383},{"style":59},[1384],{"type":26,"value":121},{"type":20,"tag":52,"props":1386,"children":1387},{"class":54,"line":166},[1388,1392,1396,1400],{"type":20,"tag":52,"props":1389,"children":1390},{"style":59},[1391],{"type":26,"value":130},{"type":20,"tag":52,"props":1393,"children":1394},{"style":65},[1395],{"type":26,"value":68},{"type":20,"tag":52,"props":1397,"children":1398},{"style":651},[1399],{"type":26,"value":725},{"type":20,"tag":52,"props":1401,"children":1402},{"style":59},[1403],{"type":26,"value":1404},"(operator.add, numbers)\n",{"type":20,"tag":52,"props":1406,"children":1407},{"class":54,"line":185},[1408,1412,1416],{"type":20,"tag":52,"props":1409,"children":1410},{"style":76},[1411],{"type":26,"value":191},{"type":20,"tag":52,"props":1413,"children":1414},{"style":59},[1415],{"type":26,"value":196},{"type":20,"tag":52,"props":1417,"children":1418},{"style":199},[1419],{"type":26,"value":202},{"type":20,"tag":29,"props":1421,"children":1422},{},[1423],{"type":26,"value":1424},"These methods provide various ways to sum elements in a list, depending on your specific needs and preferences.",{"type":20,"tag":1426,"props":1427,"children":1428},"style",{},[1429],{"type":26,"value":1430},"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);}",{"title":7,"searchDepth":142,"depth":142,"links":1432},[1433],{"id":23,"depth":124,"text":27,"children":1434},[1435,1436,1438,1439,1440,1442,1444,1446,1448,1450],{"id":37,"depth":142,"text":40},{"id":205,"depth":142,"text":1437},"Using the Built-in sum() Function",{"id":317,"depth":142,"text":320},{"id":494,"depth":142,"text":497},{"id":614,"depth":142,"text":1441},"Using functools.reduce",{"id":768,"depth":142,"text":1443},"Using numpy Library",{"id":898,"depth":142,"text":1445},"Using math.fsum for Floating Point Precision",{"id":1022,"depth":142,"text":1447},"Using itertools.accumulate for Cumulative Sums",{"id":1148,"depth":142,"text":1449},"Using pandas Library",{"id":1275,"depth":142,"text":1451},"Using reduce with operator.add","markdown","content:competitive-programming:math-library:sum.md","content","competitive-programming\u002Fmath-library\u002Fsum.md","competitive-programming\u002Fmath-library\u002Fsum","md",[1459,1464,1469,1475,1480,1485,1490,1495,1504,1510,1515,1520,1525,1530,1535,1543,1549,1556],{"_path":1460,"title":1461,"description":1462,"tags":1463,"date":14},"\u002Fcompetitive-programming\u002Farray-manipulation\u002Ffor-looping","For Looping in Python","Using for loops to manipulate arrays in Python.",[11,12,13],{"_path":1465,"title":1466,"description":1467,"tags":1468,"date":14},"\u002Fcompetitive-programming\u002Farray-manipulation\u002Fwhile-looping","While Looping in Python","Using while loops to manipulate arrays in Python.",[11,12,13],{"_path":1470,"title":1471,"description":1472,"tags":1473,"date":1474},"\u002Fcompetitive-programming\u002Fassignments","Assignments","Assignments in Python for competitive programming.",[11,12],"2026-04-10",{"_path":1476,"title":1477,"description":1478,"tags":1479,"date":1474},"\u002Fcompetitive-programming\u002Fdata-type","Data Types","Data types in Python for competitive programming.",[11,12],{"_path":1481,"title":1482,"description":1483,"tags":1484,"date":1474},"\u002Fcompetitive-programming\u002Fhash-map","Hash Map","Hash map implementation in Python for competitive programming.",[11,12],{"_path":1486,"title":1487,"description":1488,"tags":1489,"date":1474},"\u002Fcompetitive-programming\u002Finput-output","Input and Output","Handling input and output in Python for competitive programming.",[11,12],{"_path":1491,"title":1492,"description":1493,"tags":1494,"date":14},"\u002Fcompetitive-programming\u002Fmath-library\u002Fsummary","Summary","Summary of the math library in Python.",[11,12,13],{"_path":1496,"title":1497,"description":1498,"tags":1499,"date":1503},"\u002Fdocker\u002Fcheat-sheets","Docker Cheat Sheets","Cheat sheets for frequently used Docker commands and concepts",[1500,1501,1502],"docker","devops","infrastructure","2026-05-01",{"_path":1505,"title":1506,"description":1507,"tags":1508,"date":1509},"\u002Fdocker\u002Fdocker-dasar\u002Fdocker-container","Docker Container","Introduction to Docker containers and their usage",[1500,1501,1502],"2026-03-12",{"_path":1511,"title":1512,"description":1513,"tags":1514,"date":1509},"\u002Fdocker\u002Fdocker-dasar\u002Fdocker-execution","Docker Execution","Introduction to Docker execution and their usage",[1500,1501,1502],{"_path":1516,"title":1517,"description":1518,"tags":1519,"date":1509},"\u002Fdocker\u002Fdocker-dasar\u002Fdocker-image","Docker Image","Introduction to Docker images and their usage",[1500,1501,1502],{"_path":1521,"title":1522,"description":1523,"tags":1524,"date":1509},"\u002Fdocker\u002Fdocker-dasar\u002Fdocker-logging","Docker Logging","Introduction to Docker logging and their usage",[1500,1501,1502],{"_path":1526,"title":1527,"description":1528,"tags":1529,"date":1509},"\u002Fdocker\u002Fdocker-prerequisites\u002Fdocker-installation","Docker Installation","Introduction to Docker installation and their usage",[1500,1501,1502],{"_path":1531,"title":1532,"description":1533,"tags":1534,"date":1509},"\u002Fdocker\u002Fresources","Docker Resources","Collection of Docker resources for learning and reference",[1500,1501,1502],{"_path":1536,"title":1537,"description":1538,"tags":1539,"date":1509},"\u002Fjavascript\u002Fpromises","Promise Patterns","Common JavaScript promise patterns and async\u002Fawait usage",[1540,1541,1542],"javascript","frontend","async",{"_path":1544,"title":1545,"description":1546,"tags":1547,"date":14},"\u002Fphp\u002Farrays","PHP Array Functions","Essential PHP array functions and patterns",[1548,12,13],"php",{"_path":1550,"title":1551,"description":1552,"tags":1553,"date":1555},"\u002Fphp\u002Fpatterns\u002Fsingleton","Singleton Pattern","Implementing the singleton pattern in PHP",[1548,1554,12],"patterns","2026-03-13",{"_path":1557,"title":1558,"description":1559,"tags":1560,"date":1561},"\u002Fphp\u002Freferences","PHP References (&)","Useful PHP reference snippets and patterns",[1548,12],"2026-03-19",[1563,1565,1566,1567,1568,1569,1570,1571,1572,1574,1575,1577,1579,1581,1582,1583,1584,1585,1586],{"_path":1460,"title":1461,"weight":1564},10,{"_path":1465,"title":1466,"weight":1564},{"_path":1470,"title":1471,"weight":142},{"_path":1476,"title":1477,"weight":124},{"_path":1481,"title":1482,"weight":1564},{"_path":1486,"title":1487,"weight":55},{"_path":4,"title":8,"weight":15},{"_path":1491,"title":1492,"weight":1564},{"_path":1496,"title":1497,"weight":1573},100,{"_path":1505,"title":1506,"weight":15},{"_path":1511,"title":1512,"weight":1576},22,{"_path":1516,"title":1517,"weight":1578},21,{"_path":1521,"title":1522,"weight":1580},23,{"_path":1526,"title":1527,"weight":1564},{"_path":1531,"title":1532,"weight":55},{"_path":1536,"title":1537},{"_path":1544,"title":1545,"weight":55},{"_path":1550,"title":1551},{"_path":1557,"title":1558,"weight":124},1777665108402]