[{"data":1,"prerenderedAt":782},["ShallowReactive",2],{"doc-\u002Fglossary\u002Fwhat-is-a-loop-in-python":3},{"id":4,"title":5,"body":6,"description":774,"extension":775,"meta":776,"navigation":777,"path":778,"seo":779,"stem":780,"__hash__":781},"content\u002Fglossary\u002Fwhat-is-a-loop-in-python.md","What Is a Loop in Python?",{"type":7,"value":8,"toc":750},"minimark",[9,13,17,20,23,39,42,47,110,113,116,119,140,144,147,150,161,169,172,176,179,182,196,199,203,206,213,219,222,239,245,253,258,266,269,276,280,286,322,325,342,345,361,371,375,378,381,395,402,412,416,419,438,444,478,484,489,492,496,499,506,531,534,550,554,557,584,591,594,636,639,650,654,658,661,665,674,678,684,688,693,697,703,707,735,746],[10,11,5],"h1",{"id":12},"what-is-a-loop-in-python",[14,15,16],"p",{},"A loop in Python is a way to repeat code.",[14,18,19],{},"This is useful when you want to do the same task more than once. Instead of writing the same line again and again, you can write a loop once and let Python repeat it for you.",[14,21,22],{},"Python mainly has two loop types:",[24,25,26,34],"ul",{},[27,28,29,33],"li",{},[30,31,32],"code",{},"for"," loops",[27,35,36,33],{},[30,37,38],{},"while",[14,40,41],{},"This page gives the basic idea of loops. It does not fully teach loop syntax.",[43,44,46],"h2",{"id":45},"quick-example","Quick example",[48,49,54],"pre",{"className":50,"code":51,"language":52,"meta":53,"style":53},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","for number in [1, 2, 3]:\n    print(number)\n","python","",[30,55,56,93],{"__ignoreMap":53},[57,58,61,64,68,71,75,79,82,85,87,90],"span",{"class":59,"line":60},"line",1,[57,62,32],{"class":63},"sVHd0",[57,65,67],{"class":66},"su5hD"," number ",[57,69,70],{"class":63},"in",[57,72,74],{"class":73},"sP7_E"," [",[57,76,78],{"class":77},"srdBf","1",[57,80,81],{"class":73},",",[57,83,84],{"class":77}," 2",[57,86,81],{"class":73},[57,88,89],{"class":77}," 3",[57,91,92],{"class":73},"]:\n",[57,94,96,100,103,107],{"class":59,"line":95},2,[57,97,99],{"class":98},"sptTA","    print",[57,101,102],{"class":73},"(",[57,104,106],{"class":105},"slqww","number",[57,108,109],{"class":73},")\n",[14,111,112],{},"This loop prints each number in the list.",[14,114,115],{},"The indented line is the code that repeats. It runs once for each value.",[14,117,118],{},"Output:",[48,120,122],{"className":50,"code":121,"language":52,"meta":53,"style":53},"1\n2\n3\n",[30,123,124,129,134],{"__ignoreMap":53},[57,125,126],{"class":59,"line":60},[57,127,128],{"class":77},"1\n",[57,130,131],{"class":59,"line":95},[57,132,133],{"class":77},"2\n",[57,135,137],{"class":59,"line":136},3,[57,138,139],{"class":77},"3\n",[43,141,143],{"id":142},"what-a-loop-means","What a loop means",[14,145,146],{},"A loop is a way to repeat code.",[14,148,149],{},"Here is the main idea:",[24,151,152,155,158],{},[27,153,154],{},"You want to perform the same action multiple times",[27,156,157],{},"You write that action once inside a loop",[27,159,160],{},"Python runs it again for each repetition",[14,162,163,164,168],{},"Each time the loop runs is called an ",[165,166,167],"strong",{},"iteration",".",[14,170,171],{},"For example, if a loop prints three items, that loop has three iterations.",[43,173,175],{"id":174},"why-loops-are-useful","Why loops are useful",[14,177,178],{},"Loops are useful because they help you avoid repeated code.",[14,180,181],{},"They are commonly used for tasks like:",[24,183,184,187,190,193],{},[27,185,186],{},"working through items in a list",[27,188,189],{},"reading characters in a string",[27,191,192],{},"reading lines from a file",[27,194,195],{},"counting through a range of numbers",[14,197,198],{},"Loops also make code easier to update later. If the repeated step needs to change, you usually only change it in one place.",[43,200,202],{"id":201},"the-main-loop-types-in-python","The main loop types in Python",[14,204,205],{},"Python has two main loop types.",[207,208,210,212],"h3",{"id":209},"for-loop",[30,211,32],{}," loop",[14,214,215,216,218],{},"A ",[30,217,32],{}," loop repeats once for each item in a sequence.",[14,220,221],{},"That sequence might be:",[24,223,224,227,230,236],{},[27,225,226],{},"a list",[27,228,229],{},"a string",[27,231,232,233],{},"a ",[30,234,235],{},"range()",[27,237,238],{},"other iterable values",[14,240,241,242,244],{},"Beginners often start with ",[30,243,32],{}," loops because they are usually easier to read.",[14,246,247,248,168],{},"If you want a full lesson, see ",[249,250,252],"a",{"href":251},"\u002Flearn\u002Fpython-for-loops-explained\u002F","Python for loops explained",[207,254,256,212],{"id":255},"while-loop",[30,257,38],{},[14,259,215,260,262,263,168],{},[30,261,38],{}," loop repeats as long as a condition stays ",[30,264,265],{},"True",[14,267,268],{},"This is useful when you do not know exactly how many times the loop should run.",[14,270,271,272,168],{},"For a full beginner guide, see ",[249,273,275],{"href":274},"\u002Flearn\u002Fpython-while-loops-explained\u002F","Python while loops explained",[43,277,279],{"id":278},"simple-example-of-a-loop","Simple example of a loop",[14,281,282,283,285],{},"Here is a small ",[30,284,32],{}," loop:",[48,287,288],{"className":50,"code":51,"language":52,"meta":53,"style":53},[30,289,290,312],{"__ignoreMap":53},[57,291,292,294,296,298,300,302,304,306,308,310],{"class":59,"line":60},[57,293,32],{"class":63},[57,295,67],{"class":66},[57,297,70],{"class":63},[57,299,74],{"class":73},[57,301,78],{"class":77},[57,303,81],{"class":73},[57,305,84],{"class":77},[57,307,81],{"class":73},[57,309,89],{"class":77},[57,311,92],{"class":73},[57,313,314,316,318,320],{"class":59,"line":95},[57,315,99],{"class":98},[57,317,102],{"class":73},[57,319,106],{"class":105},[57,321,109],{"class":73},[14,323,324],{},"What happens here:",[24,326,327,333,339],{},[27,328,329,332],{},[30,330,331],{},"for number in [1, 2, 3]:"," tells Python to go through the list one item at a time",[27,334,335,338],{},[30,336,337],{},"print(number)"," is the repeated action",[27,340,341],{},"The indentation shows which line belongs to the loop",[14,343,344],{},"Expected output:",[48,346,347],{"className":50,"code":121,"language":52,"meta":53,"style":53},[30,348,349,353,357],{"__ignoreMap":53},[57,350,351],{"class":59,"line":60},[57,352,128],{"class":77},[57,354,355],{"class":59,"line":95},[57,356,133],{"class":77},[57,358,359],{"class":59,"line":136},[57,360,139],{"class":77},[14,362,363,364,366,367,168],{},"If the ",[30,365,337],{}," line was not indented, it would not be part of the loop. If indentation is confusing, read ",[249,368,370],{"href":369},"\u002Flearn\u002Fpython-indentation-rules-and-why-they-matter\u002F","Python indentation rules and why they matter",[43,372,374],{"id":373},"when-to-use-a-loop","When to use a loop",[14,376,377],{},"Use a loop when you need to repeat a step.",[14,379,380],{},"Common examples:",[24,382,383,386,389,392],{},[27,384,385],{},"Loop through items in a list",[27,387,388],{},"Repeat until the user enters valid input",[27,390,391],{},"Read lines from a file one by one",[27,393,394],{},"Count through a range of numbers",[14,396,397,398,168],{},"For example, if you want to process each item in a list, a loop is the natural tool. A good next step is ",[249,399,401],{"href":400},"\u002Fhow-to\u002Fhow-to-loop-through-a-list-in-python\u002F","how to loop through a list in Python",[14,403,404,405,411],{},"You will also often see loops used with the ",[249,406,408,410],{"href":407},"\u002Freference\u002Fpython-range-function-explained\u002F",[30,409,235],{}," function"," when counting numbers.",[43,413,415],{"id":414},"important-beginner-idea","Important beginner idea",[14,417,418],{},"There are a few important things to understand early.",[24,420,421,424,427,430],{},[27,422,423],{},"A loop can run many times",[27,425,426],{},"A loop can also run zero times",[27,428,429],{},"Indentation matters because it defines the repeated block",[27,431,215,432,434,435],{},[30,433,38],{}," loop can become infinite if its condition never becomes ",[30,436,437],{},"False",[14,439,440,441,443],{},"For example, this ",[30,442,38],{}," loop never stops:",[48,445,447],{"className":50,"code":446,"language":52,"meta":53,"style":53},"while True:\n    print(\"loop running\")\n",[30,448,449,460],{"__ignoreMap":53},[57,450,451,453,457],{"class":59,"line":60},[57,452,38],{"class":63},[57,454,456],{"class":455},"s39Yj"," True",[57,458,459],{"class":73},":\n",[57,461,462,464,466,470,474,476],{"class":59,"line":95},[57,463,99],{"class":98},[57,465,102],{"class":73},[57,467,469],{"class":468},"sjJ54","\"",[57,471,473],{"class":472},"s_sjI","loop running",[57,475,469],{"class":468},[57,477,109],{"class":73},[14,479,480,481,168],{},"This is called an ",[165,482,483],{},"infinite loop",[14,485,215,486,488],{},[30,487,38],{}," loop usually needs something to change so the condition eventually becomes false.",[14,490,491],{},"Also, be careful when changing loop variables or counting values. Small mistakes can cause bugs or make a loop behave in unexpected ways.",[43,493,495],{"id":494},"what-this-page-should-not-cover-in-depth","What this page should not cover in depth",[14,497,498],{},"This page is only a glossary-style definition.",[14,500,501,502,505],{},"It does ",[165,503,504],{},"not"," fully teach:",[24,507,508,514,518,521,526],{},[27,509,510,511,513],{},"full ",[30,512,32],{}," loop syntax",[27,515,510,516,513],{},[30,517,38],{},[27,519,520],{},"nested loops",[27,522,523],{},[30,524,525],{},"break",[27,527,528],{},[30,529,530],{},"continue",[14,532,533],{},"If you want to go further, read:",[24,535,536,540,544],{},[27,537,538],{},[249,539,252],{"href":251},[27,541,542],{},[249,543,275],{"href":274},[27,545,546],{},[249,547,549],{"href":548},"\u002Flearn\u002Fpython-break-and-continue-statements\u002F","Python break and continue statements",[43,551,553],{"id":552},"common-mistakes","Common mistakes",[14,555,556],{},"Beginners often run into these problems:",[24,558,559,566,569,572,578],{},[27,560,561,562,565],{},"Confusing a loop with an ",[30,563,564],{},"if"," statement",[27,567,568],{},"Thinking loops always run forever",[27,570,571],{},"Forgetting that indentation controls which lines repeat",[27,573,574,575,577],{},"Using a ",[30,576,38],{}," loop without changing the condition",[27,579,580,581,583],{},"Not realizing that ",[30,582,32],{}," loops usually go through a sequence",[14,585,586,587,590],{},"If you are not sure what a loop is doing, simple ",[30,588,589],{},"print()"," statements can help you debug it.",[14,592,593],{},"Examples:",[48,595,597],{"className":50,"code":596,"language":52,"meta":53,"style":53},"print(item)\nprint(count)\nprint(\"loop running\")\n",[30,598,599,611,622],{"__ignoreMap":53},[57,600,601,604,606,609],{"class":59,"line":60},[57,602,603],{"class":98},"print",[57,605,102],{"class":73},[57,607,608],{"class":105},"item",[57,610,109],{"class":73},[57,612,613,615,617,620],{"class":59,"line":95},[57,614,603],{"class":98},[57,616,102],{"class":73},[57,618,619],{"class":105},"count",[57,621,109],{"class":73},[57,623,624,626,628,630,632,634],{"class":59,"line":136},[57,625,603],{"class":98},[57,627,102],{"class":73},[57,629,469],{"class":468},[57,631,473],{"class":472},[57,633,469],{"class":468},[57,635,109],{"class":73},[14,637,638],{},"These help you see:",[24,640,641,644,647],{},[27,642,643],{},"whether the loop is running",[27,645,646],{},"which value is being processed",[27,648,649],{},"how a counter is changing",[43,651,653],{"id":652},"faq","FAQ",[207,655,657],{"id":656},"what-is-a-loop-in-simple-words","What is a loop in simple words?",[14,659,660],{},"A loop is code that repeats the same steps multiple times.",[207,662,664],{"id":663},"what-are-the-two-main-loops-in-python","What are the two main loops in Python?",[14,666,667,668,670,671,673],{},"Python mainly uses ",[30,669,32],{}," loops and ",[30,672,38],{}," loops.",[207,675,677],{"id":676},"when-should-i-use-a-for-loop","When should I use a for loop?",[14,679,680,681,683],{},"Use a ",[30,682,32],{}," loop when you want to go through items like a list, string, or range.",[207,685,687],{"id":686},"when-should-i-use-a-while-loop","When should I use a while loop?",[14,689,680,690,692],{},[30,691,38],{}," loop when code should keep running until a condition changes.",[207,694,696],{"id":695},"can-a-loop-run-forever","Can a loop run forever?",[14,698,699,700,702],{},"Yes. A ",[30,701,38],{}," loop can run forever if the condition never becomes false.",[43,704,706],{"id":705},"see-also","See also",[24,708,709,713,717,721,725,730],{},[27,710,711],{},[249,712,252],{"href":251},[27,714,715],{},[249,716,275],{"href":274},[27,718,719],{},[249,720,370],{"href":369},[27,722,723],{},[249,724,549],{"href":548},[27,726,727],{},[249,728,729],{"href":407},"Python range() function explained",[27,731,732],{},[249,733,734],{"href":400},"How to loop through a list in Python",[14,736,737,738,741,742,745],{},"If you are learning loops for the first time, the best next step is to start with ",[165,739,740],{},"for loops",", then learn ",[165,743,744],{},"while loops",", and then practice looping through lists.",[747,748,749],"style",{},"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 .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 .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 .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}",{"title":53,"searchDepth":95,"depth":95,"links":751},[752,753,754,755,761,762,763,764,765,766,773],{"id":45,"depth":95,"text":46},{"id":142,"depth":95,"text":143},{"id":174,"depth":95,"text":175},{"id":201,"depth":95,"text":202,"children":756},[757,759],{"id":209,"depth":136,"text":758},"for loop",{"id":255,"depth":136,"text":760},"while loop",{"id":278,"depth":95,"text":279},{"id":373,"depth":95,"text":374},{"id":414,"depth":95,"text":415},{"id":494,"depth":95,"text":495},{"id":552,"depth":95,"text":553},{"id":652,"depth":95,"text":653,"children":767},[768,769,770,771,772],{"id":656,"depth":136,"text":657},{"id":663,"depth":136,"text":664},{"id":676,"depth":136,"text":677},{"id":686,"depth":136,"text":687},{"id":695,"depth":136,"text":696},{"id":705,"depth":95,"text":706},"Master what is a loop in python in our comprehensive Python beginner guide.","md",{},true,"\u002Fglossary\u002Fwhat-is-a-loop-in-python",{"title":5,"description":774},"glossary\u002Fwhat-is-a-loop-in-python","Mnq-14CKOm5nEHDnr4M8JufK5zOPIfqTFlg3hikroEg",1777585468117]