[{"data":1,"prerenderedAt":1400},["ShallowReactive",2],{"doc-\u002Fglossary\u002Fwhat-is-recursion-in-python":3},{"id":4,"title":5,"body":6,"description":1393,"extension":1394,"meta":1395,"navigation":164,"path":1396,"seo":1397,"stem":1398,"__hash__":1399},"content\u002Fglossary\u002Fwhat-is-recursion-in-python.md","What Is Recursion in Python?",{"type":7,"value":8,"toc":1373},"minimark",[9,13,17,20,23,40,43,179,182,207,210,232,241,246,249,252,263,266,276,309,313,316,328,335,338,418,421,425,431,515,518,566,569,583,587,592,678,681,685,688,691,704,707,789,791,800,803,817,821,824,827,975,977,1009,1012,1016,1019,1022,1036,1039,1047,1051,1054,1065,1068,1080,1084,1087,1104,1107,1148,1151,1154,1226,1233,1236,1250,1253,1288,1291,1303,1307,1311,1320,1324,1327,1331,1334,1338,1341,1345,1369],[10,11,5],"h1",{"id":12},"what-is-recursion-in-python",[14,15,16],"p",{},"Recursion in Python means a function calls itself.",[14,18,19],{},"This may sound strange at first, but the idea is simple: the function keeps solving a smaller version of the same problem until it reaches a stopping point.",[14,21,22],{},"A recursive function has two main parts:",[24,25,26,34],"ul",{},[27,28,29,33],"li",{},[30,31,32],"strong",{},"Base case",": the condition that stops the function",[27,35,36,39],{},[30,37,38],{},"Recursive case",": the part where the function calls itself again",[14,41,42],{},"Here is the simplest working example:",[44,45,50],"pre",{"className":46,"code":47,"language":48,"meta":49,"style":49},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","def countdown(n):\n    if n == 0:\n        print(\"Done\")\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(3)\n","python","",[51,52,53,77,99,121,127,140,159,166],"code",{"__ignoreMap":49},[54,55,58,62,66,70,74],"span",{"class":56,"line":57},"line",1,[54,59,61],{"class":60},"sbsja","def",[54,63,65],{"class":64},"sGLFI"," countdown",[54,67,69],{"class":68},"sP7_E","(",[54,71,73],{"class":72},"sFwrP","n",[54,75,76],{"class":68},"):\n",[54,78,80,84,88,92,96],{"class":56,"line":79},2,[54,81,83],{"class":82},"sVHd0","    if",[54,85,87],{"class":86},"su5hD"," n ",[54,89,91],{"class":90},"smGrS","==",[54,93,95],{"class":94},"srdBf"," 0",[54,97,98],{"class":68},":\n",[54,100,102,106,108,112,116,118],{"class":56,"line":101},3,[54,103,105],{"class":104},"sptTA","        print",[54,107,69],{"class":68},[54,109,111],{"class":110},"sjJ54","\"",[54,113,115],{"class":114},"s_sjI","Done",[54,117,111],{"class":110},[54,119,120],{"class":68},")\n",[54,122,124],{"class":56,"line":123},4,[54,125,126],{"class":82},"        return\n",[54,128,130,133,135,138],{"class":56,"line":129},5,[54,131,132],{"class":104},"    print",[54,134,69],{"class":68},[54,136,73],{"class":137},"slqww",[54,139,120],{"class":68},[54,141,143,146,148,151,154,157],{"class":56,"line":142},6,[54,144,145],{"class":137},"    countdown",[54,147,69],{"class":68},[54,149,150],{"class":137},"n ",[54,152,153],{"class":90},"-",[54,155,156],{"class":94}," 1",[54,158,120],{"class":68},[54,160,162],{"class":56,"line":161},7,[54,163,165],{"emptyLinePlaceholder":164},true,"\n",[54,167,169,172,174,177],{"class":56,"line":168},8,[54,170,171],{"class":137},"countdown",[54,173,69],{"class":68},[54,175,176],{"class":94},"3",[54,178,120],{"class":68},[14,180,181],{},"Output:",[44,183,185],{"className":46,"code":184,"language":48,"meta":49,"style":49},"3\n2\n1\nDone\n",[51,186,187,192,197,202],{"__ignoreMap":49},[54,188,189],{"class":56,"line":57},[54,190,191],{"class":94},"3\n",[54,193,194],{"class":56,"line":79},[54,195,196],{"class":94},"2\n",[54,198,199],{"class":56,"line":101},[54,200,201],{"class":94},"1\n",[54,203,204],{"class":56,"line":123},[54,205,206],{"class":86},"Done\n",[14,208,209],{},"How this works:",[24,211,212,221,229],{},[27,213,214,217,218],{},[51,215,216],{},"if n == 0:"," is the ",[30,219,220],{},"base case",[27,222,223,217,226],{},[51,224,225],{},"countdown(n - 1)",[30,227,228],{},"recursive call",[27,230,231],{},"Each call uses a smaller value, so the function moves toward stopping",[14,233,234,235,240],{},"If you are new to functions, see ",[236,237,239],"a",{"href":238},"\u002Fglossary\u002Fwhat-is-a-function-in-python\u002F","what is a function in Python",".",[242,243,245],"h2",{"id":244},"what-recursion-means","What recursion means",[14,247,248],{},"Recursion is when a function calls itself.",[14,250,251],{},"A recursive function usually works like this:",[24,253,254,257,260],{},[27,255,256],{},"It handles one small part of the problem",[27,258,259],{},"Then it calls itself to handle the rest",[27,261,262],{},"Each call should get closer to stopping",[14,264,265],{},"This is why recursion is often used for problems that can be broken into smaller versions of the same task.",[14,267,268,269,271,272,275],{},"For example, counting down from ",[51,270,176],{}," to ",[51,273,274],{},"0"," can be seen as:",[24,277,278,283,289,293,298,302,306],{},[27,279,280,281],{},"print ",[51,282,176],{},[27,284,285,286],{},"count down from ",[51,287,288],{},"2",[27,290,280,291],{},[51,292,288],{},[27,294,285,295],{},[51,296,297],{},"1",[27,299,280,300],{},[51,301,297],{},[27,303,285,304],{},[51,305,274],{},[27,307,308],{},"stop",[242,310,312],{"id":311},"the-two-required-parts","The two required parts",[14,314,315],{},"A recursive function needs both of these parts:",[24,317,318,323],{},[27,319,320,322],{},[30,321,32],{},": the condition that stops the recursion",[27,324,325,327],{},[30,326,38],{},": the part where the function calls itself",[14,329,330,331,240],{},"Without a base case, the function will keep calling itself until Python raises an error such as ",[236,332,334],{"href":333},"\u002Ferrors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix\u002F","RecursionError: maximum recursion depth exceeded",[14,336,337],{},"Example:",[44,339,341],{"className":46,"code":340,"language":48,"meta":49,"style":49},"def countdown(n):\n    if n == 0:          # base case\n        print(\"Done\")\n        return\n    print(n)\n    countdown(n - 1)    # recursive case\n",[51,342,343,355,372,386,390,400],{"__ignoreMap":49},[54,344,345,347,349,351,353],{"class":56,"line":57},[54,346,61],{"class":60},[54,348,65],{"class":64},[54,350,69],{"class":68},[54,352,73],{"class":72},[54,354,76],{"class":68},[54,356,357,359,361,363,365,368],{"class":56,"line":79},[54,358,83],{"class":82},[54,360,87],{"class":86},[54,362,91],{"class":90},[54,364,95],{"class":94},[54,366,367],{"class":68},":",[54,369,371],{"class":370},"sutJx","          # base case\n",[54,373,374,376,378,380,382,384],{"class":56,"line":101},[54,375,105],{"class":104},[54,377,69],{"class":68},[54,379,111],{"class":110},[54,381,115],{"class":114},[54,383,111],{"class":110},[54,385,120],{"class":68},[54,387,388],{"class":56,"line":123},[54,389,126],{"class":82},[54,391,392,394,396,398],{"class":56,"line":129},[54,393,132],{"class":104},[54,395,69],{"class":68},[54,397,73],{"class":137},[54,399,120],{"class":68},[54,401,402,404,406,408,410,412,415],{"class":56,"line":142},[54,403,145],{"class":137},[54,405,69],{"class":68},[54,407,150],{"class":137},[54,409,153],{"class":90},[54,411,156],{"class":94},[54,413,414],{"class":68},")",[54,416,417],{"class":370},"    # recursive case\n",[14,419,420],{},"The key idea is that the recursive call must move toward the base case.",[242,422,424],{"id":423},"how-recursion-works-step-by-step","How recursion works step by step",[14,426,427,428,240],{},"Let’s walk through ",[51,429,430],{},"countdown(3)",[44,432,433],{"className":46,"code":47,"language":48,"meta":49,"style":49},[51,434,435,447,459,473,477,487,501,505],{"__ignoreMap":49},[54,436,437,439,441,443,445],{"class":56,"line":57},[54,438,61],{"class":60},[54,440,65],{"class":64},[54,442,69],{"class":68},[54,444,73],{"class":72},[54,446,76],{"class":68},[54,448,449,451,453,455,457],{"class":56,"line":79},[54,450,83],{"class":82},[54,452,87],{"class":86},[54,454,91],{"class":90},[54,456,95],{"class":94},[54,458,98],{"class":68},[54,460,461,463,465,467,469,471],{"class":56,"line":101},[54,462,105],{"class":104},[54,464,69],{"class":68},[54,466,111],{"class":110},[54,468,115],{"class":114},[54,470,111],{"class":110},[54,472,120],{"class":68},[54,474,475],{"class":56,"line":123},[54,476,126],{"class":82},[54,478,479,481,483,485],{"class":56,"line":129},[54,480,132],{"class":104},[54,482,69],{"class":68},[54,484,73],{"class":137},[54,486,120],{"class":68},[54,488,489,491,493,495,497,499],{"class":56,"line":142},[54,490,145],{"class":137},[54,492,69],{"class":68},[54,494,150],{"class":137},[54,496,153],{"class":90},[54,498,156],{"class":94},[54,500,120],{"class":68},[54,502,503],{"class":56,"line":161},[54,504,165],{"emptyLinePlaceholder":164},[54,506,507,509,511,513],{"class":56,"line":168},[54,508,171],{"class":137},[54,510,69],{"class":68},[54,512,176],{"class":94},[54,514,120],{"class":68},[14,516,517],{},"Step by step:",[519,520,521,528,534,540,545,551,556,563],"ol",{},[27,522,523,525,526],{},[51,524,430],{}," prints ",[51,527,176],{},[27,529,530,531],{},"It calls ",[51,532,533],{},"countdown(2)",[27,535,536,525,538],{},[51,537,533],{},[51,539,288],{},[27,541,530,542],{},[51,543,544],{},"countdown(1)",[27,546,547,525,549],{},[51,548,544],{},[51,550,297],{},[27,552,530,553],{},[51,554,555],{},"countdown(0)",[27,557,558,560,561],{},[51,559,555],{}," reaches the base case and prints ",[51,562,115],{},[27,564,565],{},"The function calls then finish one by one",[14,567,568],{},"In other words:",[24,570,571,574,577,580],{},[27,572,573],{},"The function starts with one input value",[27,575,576],{},"It calls itself with a smaller value",[27,578,579],{},"This repeats until the base case is reached",[27,581,582],{},"Then the calls return back up",[242,584,586],{"id":585},"simple-beginner-examples","Simple beginner examples",[588,589,591],"h3",{"id":590},"countdown-from-a-number-to-zero","Countdown from a number to zero",[44,593,595],{"className":46,"code":594,"language":48,"meta":49,"style":49},"def countdown(n):\n    if n == 0:\n        print(\"Done\")\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(5)\n",[51,596,597,609,621,635,639,649,663,667],{"__ignoreMap":49},[54,598,599,601,603,605,607],{"class":56,"line":57},[54,600,61],{"class":60},[54,602,65],{"class":64},[54,604,69],{"class":68},[54,606,73],{"class":72},[54,608,76],{"class":68},[54,610,611,613,615,617,619],{"class":56,"line":79},[54,612,83],{"class":82},[54,614,87],{"class":86},[54,616,91],{"class":90},[54,618,95],{"class":94},[54,620,98],{"class":68},[54,622,623,625,627,629,631,633],{"class":56,"line":101},[54,624,105],{"class":104},[54,626,69],{"class":68},[54,628,111],{"class":110},[54,630,115],{"class":114},[54,632,111],{"class":110},[54,634,120],{"class":68},[54,636,637],{"class":56,"line":123},[54,638,126],{"class":82},[54,640,641,643,645,647],{"class":56,"line":129},[54,642,132],{"class":104},[54,644,69],{"class":68},[54,646,73],{"class":137},[54,648,120],{"class":68},[54,650,651,653,655,657,659,661],{"class":56,"line":142},[54,652,145],{"class":137},[54,654,69],{"class":68},[54,656,150],{"class":137},[54,658,153],{"class":90},[54,660,156],{"class":94},[54,662,120],{"class":68},[54,664,665],{"class":56,"line":161},[54,666,165],{"emptyLinePlaceholder":164},[54,668,669,671,673,676],{"class":56,"line":168},[54,670,171],{"class":137},[54,672,69],{"class":68},[54,674,675],{"class":94},"5",[54,677,120],{"class":68},[14,679,680],{},"This is a good first recursion example because it is easy to follow.",[588,682,684],{"id":683},"factorial-of-a-number","Factorial of a number",[14,686,687],{},"A factorial multiplies a number by all positive integers below it.",[14,689,690],{},"For example:",[24,692,693,698],{},[27,694,695],{},[51,696,697],{},"4! = 4 × 3 × 2 × 1",[27,699,700,701],{},"so the result is ",[51,702,703],{},"24",[14,705,706],{},"Recursive version:",[44,708,710],{"className":46,"code":709,"language":48,"meta":49,"style":49},"def factorial(n):\n    if n == 1:\n        return 1\n    return n * factorial(n - 1)\n\nprint(factorial(4))\n",[51,711,712,725,737,745,767,771],{"__ignoreMap":49},[54,713,714,716,719,721,723],{"class":56,"line":57},[54,715,61],{"class":60},[54,717,718],{"class":64}," factorial",[54,720,69],{"class":68},[54,722,73],{"class":72},[54,724,76],{"class":68},[54,726,727,729,731,733,735],{"class":56,"line":79},[54,728,83],{"class":82},[54,730,87],{"class":86},[54,732,91],{"class":90},[54,734,156],{"class":94},[54,736,98],{"class":68},[54,738,739,742],{"class":56,"line":101},[54,740,741],{"class":82},"        return",[54,743,744],{"class":94}," 1\n",[54,746,747,750,752,755,757,759,761,763,765],{"class":56,"line":123},[54,748,749],{"class":82},"    return",[54,751,87],{"class":86},[54,753,754],{"class":90},"*",[54,756,718],{"class":137},[54,758,69],{"class":68},[54,760,150],{"class":137},[54,762,153],{"class":90},[54,764,156],{"class":94},[54,766,120],{"class":68},[54,768,769],{"class":56,"line":129},[54,770,165],{"emptyLinePlaceholder":164},[54,772,773,776,778,781,783,786],{"class":56,"line":142},[54,774,775],{"class":104},"print",[54,777,69],{"class":68},[54,779,780],{"class":137},"factorial",[54,782,69],{"class":68},[54,784,785],{"class":94},"4",[54,787,788],{"class":68},"))\n",[14,790,181],{},[44,792,794],{"className":46,"code":793,"language":48,"meta":49,"style":49},"24\n",[51,795,796],{"__ignoreMap":49},[54,797,798],{"class":56,"line":57},[54,799,793],{"class":94},[14,801,802],{},"What happens here:",[24,804,805,811],{},[27,806,807,808],{},"The base case is ",[51,809,810],{},"n == 1",[27,812,813,814],{},"The recursive case is ",[51,815,816],{},"n * factorial(n - 1)",[588,818,820],{"id":819},"walking-through-nested-data-structures","Walking through nested data structures",[14,822,823],{},"Recursion is often useful when data contains smaller versions of itself.",[14,825,826],{},"Example with a nested list:",[44,828,830],{"className":46,"code":829,"language":48,"meta":49,"style":49},"def print_items(items):\n    for item in items:\n        if isinstance(item, list):\n            print_items(item)\n        else:\n            print(item)\n\ndata = [1, [2, 3], [4, [5, 6]]]\nprint_items(data)\n",[51,831,832,846,862,884,895,902,913,917,962],{"__ignoreMap":49},[54,833,834,836,839,841,844],{"class":56,"line":57},[54,835,61],{"class":60},[54,837,838],{"class":64}," print_items",[54,840,69],{"class":68},[54,842,843],{"class":72},"items",[54,845,76],{"class":68},[54,847,848,851,854,857,860],{"class":56,"line":79},[54,849,850],{"class":82},"    for",[54,852,853],{"class":86}," item ",[54,855,856],{"class":82},"in",[54,858,859],{"class":86}," items",[54,861,98],{"class":68},[54,863,864,867,870,872,875,878,882],{"class":56,"line":101},[54,865,866],{"class":82},"        if",[54,868,869],{"class":104}," isinstance",[54,871,69],{"class":68},[54,873,874],{"class":137},"item",[54,876,877],{"class":68},",",[54,879,881],{"class":880},"sZMiF"," list",[54,883,76],{"class":68},[54,885,886,889,891,893],{"class":56,"line":123},[54,887,888],{"class":137},"            print_items",[54,890,69],{"class":68},[54,892,874],{"class":137},[54,894,120],{"class":68},[54,896,897,900],{"class":56,"line":129},[54,898,899],{"class":82},"        else",[54,901,98],{"class":68},[54,903,904,907,909,911],{"class":56,"line":142},[54,905,906],{"class":104},"            print",[54,908,69],{"class":68},[54,910,874],{"class":137},[54,912,120],{"class":68},[54,914,915],{"class":56,"line":161},[54,916,165],{"emptyLinePlaceholder":164},[54,918,919,922,925,928,930,932,934,936,938,941,944,946,948,950,952,954,956,959],{"class":56,"line":168},[54,920,921],{"class":86},"data ",[54,923,924],{"class":90},"=",[54,926,927],{"class":68}," [",[54,929,297],{"class":94},[54,931,877],{"class":68},[54,933,927],{"class":68},[54,935,288],{"class":94},[54,937,877],{"class":68},[54,939,940],{"class":94}," 3",[54,942,943],{"class":68},"],",[54,945,927],{"class":68},[54,947,785],{"class":94},[54,949,877],{"class":68},[54,951,927],{"class":68},[54,953,675],{"class":94},[54,955,877],{"class":68},[54,957,958],{"class":94}," 6",[54,960,961],{"class":68},"]]]\n",[54,963,965,968,970,973],{"class":56,"line":964},9,[54,966,967],{"class":137},"print_items",[54,969,69],{"class":68},[54,971,972],{"class":137},"data",[54,974,120],{"class":68},[14,976,181],{},[44,978,980],{"className":46,"code":979,"language":48,"meta":49,"style":49},"1\n2\n3\n4\n5\n6\n",[51,981,982,986,990,994,999,1004],{"__ignoreMap":49},[54,983,984],{"class":56,"line":57},[54,985,201],{"class":94},[54,987,988],{"class":56,"line":79},[54,989,196],{"class":94},[54,991,992],{"class":56,"line":101},[54,993,191],{"class":94},[54,995,996],{"class":56,"line":123},[54,997,998],{"class":94},"4\n",[54,1000,1001],{"class":56,"line":129},[54,1002,1003],{"class":94},"5\n",[54,1005,1006],{"class":56,"line":142},[54,1007,1008],{"class":94},"6\n",[14,1010,1011],{},"This works because when the function finds another list, it calls itself again to process that smaller list.",[242,1013,1015],{"id":1014},"when-recursion-is-useful","When recursion is useful",[14,1017,1018],{},"Recursion is useful when a problem naturally repeats in smaller versions.",[14,1020,1021],{},"Common examples include:",[24,1023,1024,1027,1030,1033],{},[27,1025,1026],{},"Tree-like structures",[27,1028,1029],{},"Nested lists",[27,1031,1032],{},"Folder and file structures",[27,1034,1035],{},"Problems where each step looks like the previous step",[14,1037,1038],{},"Recursion can also make some code easier to read than a loop, especially when the data is nested.",[14,1040,1041,1042,1046],{},"Still, recursion is not always the best choice. For many simple repeated tasks, ",[236,1043,1045],{"href":1044},"\u002Flearn\u002Fpython-while-loops-explained\u002F","Python while loops explained"," will be easier to understand.",[242,1048,1050],{"id":1049},"when-beginners-should-avoid-recursion","When beginners should avoid recursion",[14,1052,1053],{},"Beginners should be careful with recursion in these situations:",[24,1055,1056,1059,1062],{},[27,1057,1058],{},"Simple counting tasks are often easier with loops",[27,1060,1061],{},"Deep recursion can hit Python’s recursion limit",[27,1063,1064],{},"Recursive code can be harder to debug at first",[14,1066,1067],{},"For example, if you just need to repeat something 10 times, a loop is usually clearer than a recursive function.",[14,1069,1070,1071,1075,1076,240],{},"If you want to compare the two ideas, it helps to understand ",[236,1072,1074],{"href":1073},"\u002Fglossary\u002Fwhat-is-a-loop-in-python\u002F","what is a loop in Python"," and ",[236,1077,1079],{"href":1078},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions explained",[242,1081,1083],{"id":1082},"common-recursion-problems","Common recursion problems",[14,1085,1086],{},"These are the most common beginner mistakes with recursion:",[24,1088,1089,1092,1095,1098,1101],{},[27,1090,1091],{},"Forgetting the base case",[27,1093,1094],{},"Writing a base case that is never reached",[27,1096,1097],{},"Changing the value in the wrong direction",[27,1099,1100],{},"Calling the function with the same value again",[27,1102,1103],{},"Expecting recursion to be faster than loops",[14,1105,1106],{},"Here is an example of a broken recursive function:",[44,1108,1110],{"className":46,"code":1109,"language":48,"meta":49,"style":49},"def countdown(n):\n    print(n)\n    countdown(n - 1)\n",[51,1111,1112,1124,1134],{"__ignoreMap":49},[54,1113,1114,1116,1118,1120,1122],{"class":56,"line":57},[54,1115,61],{"class":60},[54,1117,65],{"class":64},[54,1119,69],{"class":68},[54,1121,73],{"class":72},[54,1123,76],{"class":68},[54,1125,1126,1128,1130,1132],{"class":56,"line":79},[54,1127,132],{"class":104},[54,1129,69],{"class":68},[54,1131,73],{"class":137},[54,1133,120],{"class":68},[54,1135,1136,1138,1140,1142,1144,1146],{"class":56,"line":101},[54,1137,145],{"class":137},[54,1139,69],{"class":68},[54,1141,150],{"class":137},[54,1143,153],{"class":90},[54,1145,156],{"class":94},[54,1147,120],{"class":68},[14,1149,1150],{},"This keeps calling itself forever because there is no base case.",[14,1152,1153],{},"Here is another common mistake:",[44,1155,1157],{"className":46,"code":1156,"language":48,"meta":49,"style":49},"def countdown(n):\n    if n == 0:\n        print(\"Done\")\n        return\n    print(n)\n    countdown(n + 1)\n",[51,1158,1159,1171,1183,1197,1201,1211],{"__ignoreMap":49},[54,1160,1161,1163,1165,1167,1169],{"class":56,"line":57},[54,1162,61],{"class":60},[54,1164,65],{"class":64},[54,1166,69],{"class":68},[54,1168,73],{"class":72},[54,1170,76],{"class":68},[54,1172,1173,1175,1177,1179,1181],{"class":56,"line":79},[54,1174,83],{"class":82},[54,1176,87],{"class":86},[54,1178,91],{"class":90},[54,1180,95],{"class":94},[54,1182,98],{"class":68},[54,1184,1185,1187,1189,1191,1193,1195],{"class":56,"line":101},[54,1186,105],{"class":104},[54,1188,69],{"class":68},[54,1190,111],{"class":110},[54,1192,115],{"class":114},[54,1194,111],{"class":110},[54,1196,120],{"class":68},[54,1198,1199],{"class":56,"line":123},[54,1200,126],{"class":82},[54,1202,1203,1205,1207,1209],{"class":56,"line":129},[54,1204,132],{"class":104},[54,1206,69],{"class":68},[54,1208,73],{"class":137},[54,1210,120],{"class":68},[54,1212,1213,1215,1217,1219,1222,1224],{"class":56,"line":142},[54,1214,145],{"class":137},[54,1216,69],{"class":68},[54,1218,150],{"class":137},[54,1220,1221],{"class":90},"+",[54,1223,156],{"class":94},[54,1225,120],{"class":68},[14,1227,1228,1229,1232],{},"This is wrong because ",[51,1230,1231],{},"n + 1"," moves away from the base case instead of toward it.",[14,1234,1235],{},"If your code is not working, these quick checks help:",[24,1237,1238,1241,1244,1247],{},[27,1239,1240],{},"Make sure there is a base case",[27,1242,1243],{},"Make sure the base case can actually happen",[27,1245,1246],{},"Make sure each recursive call reduces the problem",[27,1248,1249],{},"Ask whether a loop would be simpler",[14,1251,1252],{},"Useful debugging tools:",[44,1254,1256],{"className":46,"code":1255,"language":48,"meta":49,"style":49},"print(n)\nprint(\"calling with:\", n)\n",[51,1257,1258,1268],{"__ignoreMap":49},[54,1259,1260,1262,1264,1266],{"class":56,"line":57},[54,1261,775],{"class":104},[54,1263,69],{"class":68},[54,1265,73],{"class":137},[54,1267,120],{"class":68},[54,1269,1270,1272,1274,1276,1279,1281,1283,1286],{"class":56,"line":79},[54,1271,775],{"class":104},[54,1273,69],{"class":68},[54,1275,111],{"class":110},[54,1277,1278],{"class":114},"calling with:",[54,1280,111],{"class":110},[54,1282,877],{"class":68},[54,1284,1285],{"class":137}," n",[54,1287,120],{"class":68},[14,1289,1290],{},"You can also use:",[24,1292,1293,1298],{},[27,1294,1295],{},[51,1296,1297],{},"help()",[27,1299,1300],{},[51,1301,1302],{},"traceback.print_exc()",[242,1304,1306],{"id":1305},"faq","FAQ",[588,1308,1310],{"id":1309},"is-recursion-the-same-as-a-loop","Is recursion the same as a loop?",[14,1312,1313,1314,1075,1317,240],{},"No. Both repeat work, but recursion uses function calls while loops use statements like ",[51,1315,1316],{},"for",[51,1318,1319],{},"while",[588,1321,1323],{"id":1322},"what-is-a-base-case-in-recursion","What is a base case in recursion?",[14,1325,1326],{},"It is the stopping condition that prevents the function from calling itself forever.",[588,1328,1330],{"id":1329},"why-does-python-give-a-recursion-error","Why does Python give a recursion error?",[14,1332,1333],{},"Usually because the function keeps calling itself too many times or never reaches the base case.",[588,1335,1337],{"id":1336},"should-beginners-use-recursion","Should beginners use recursion?",[14,1339,1340],{},"Yes, to understand the idea. But for many simple tasks, loops are easier to write and debug.",[242,1342,1344],{"id":1343},"see-also","See also",[24,1346,1347,1351,1355,1359,1364],{},[27,1348,1349],{},[236,1350,1079],{"href":1078},[27,1352,1353],{},[236,1354,1045],{"href":1044},[27,1356,1357],{},[236,1358,334],{"href":333},[27,1360,1361],{},[236,1362,1363],{"href":238},"What is a function in Python",[27,1365,1366],{},[236,1367,1368],{"href":1073},"What is a loop in Python",[1370,1371,1372],"style",{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}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}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 .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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":49,"searchDepth":79,"depth":79,"links":1374},[1375,1376,1377,1378,1383,1384,1385,1386,1392],{"id":244,"depth":79,"text":245},{"id":311,"depth":79,"text":312},{"id":423,"depth":79,"text":424},{"id":585,"depth":79,"text":586,"children":1379},[1380,1381,1382],{"id":590,"depth":101,"text":591},{"id":683,"depth":101,"text":684},{"id":819,"depth":101,"text":820},{"id":1014,"depth":79,"text":1015},{"id":1049,"depth":79,"text":1050},{"id":1082,"depth":79,"text":1083},{"id":1305,"depth":79,"text":1306,"children":1387},[1388,1389,1390,1391],{"id":1309,"depth":101,"text":1310},{"id":1322,"depth":101,"text":1323},{"id":1329,"depth":101,"text":1330},{"id":1336,"depth":101,"text":1337},{"id":1343,"depth":79,"text":1344},"Master what is recursion in python in our comprehensive Python beginner guide.","md",{},"\u002Fglossary\u002Fwhat-is-recursion-in-python",{"title":5,"description":1393},"glossary\u002Fwhat-is-recursion-in-python","6OR4kuwdmFeYDvo7jNoADsYvZT95NwuT_tirq6xF0RM",1777585468130]