[{"data":1,"prerenderedAt":41829},["ShallowReactive",2],{"doc-\u002Fglossary\u002Fwhat-is-a-list-in-python":3,"nav-glossary":1165},{"id":4,"title":5,"body":6,"description":1158,"extension":1159,"meta":1160,"navigation":475,"path":1161,"seo":1162,"stem":1163,"__hash__":1164},"content\u002Fglossary\u002Fwhat-is-a-list-in-python.md","What Is a List in Python?",{"type":7,"value":8,"toc":1140},"minimark",[9,13,17,20,25,105,108,153,156,172,176,179,182,195,198,241,248,252,255,258,272,275,316,319,323,326,368,370,423,427,430,536,538,584,587,613,625,642,646,649,652,663,666,692,695,845,858,862,865,868,879,882,902,906,909,958,961,1020,1023,1049,1060,1064,1069,1072,1076,1079,1083,1086,1090,1093,1097,1102,1106,1136],[10,11,5],"h1",{"id":12},"what-is-a-list-in-python",[14,15,16],"p",{},"A list in Python is a beginner-friendly way to store multiple values in one variable. This page explains what a list is, what it is used for, and how to recognize one when you see it.",[14,18,19],{},"If you are just getting started, the main idea is simple: a list lets you keep related items together in order. For example, you might use a list for names, scores, or file paths.",[21,22,24],"h2",{"id":23},"quick-example","Quick example",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","numbers = [1, 2, 3]\nprint(numbers)\nprint(type(numbers))\n","python","",[33,34,35,70,87],"code",{"__ignoreMap":31},[36,37,40,44,48,52,56,59,62,64,67],"span",{"class":38,"line":39},"line",1,[36,41,43],{"class":42},"su5hD","numbers ",[36,45,47],{"class":46},"smGrS","=",[36,49,51],{"class":50},"sP7_E"," [",[36,53,55],{"class":54},"srdBf","1",[36,57,58],{"class":50},",",[36,60,61],{"class":54}," 2",[36,63,58],{"class":50},[36,65,66],{"class":54}," 3",[36,68,69],{"class":50},"]\n",[36,71,73,77,80,84],{"class":38,"line":72},2,[36,74,76],{"class":75},"sptTA","print",[36,78,79],{"class":50},"(",[36,81,83],{"class":82},"slqww","numbers",[36,85,86],{"class":50},")\n",[36,88,90,92,94,98,100,102],{"class":38,"line":89},3,[36,91,76],{"class":75},[36,93,79],{"class":50},[36,95,97],{"class":96},"sZMiF","type",[36,99,79],{"class":50},[36,101,83],{"class":82},[36,103,104],{"class":50},"))\n",[14,106,107],{},"Output:",[26,109,111],{"className":28,"code":110,"language":30,"meta":31,"style":31},"[1, 2, 3]\n\u003Cclass 'list'>\n",[33,112,113,130],{"__ignoreMap":31},[36,114,115,118,120,122,124,126,128],{"class":38,"line":39},[36,116,117],{"class":50},"[",[36,119,55],{"class":54},[36,121,58],{"class":50},[36,123,61],{"class":54},[36,125,58],{"class":50},[36,127,66],{"class":54},[36,129,69],{"class":50},[36,131,132,135,139,143,147,150],{"class":38,"line":72},[36,133,134],{"class":46},"\u003C",[36,136,138],{"class":137},"sbsja","class",[36,140,142],{"class":141},"sjJ54"," '",[36,144,146],{"class":145},"s_sjI","list",[36,148,149],{"class":141},"'",[36,151,152],{"class":46},">\n",[14,154,155],{},"This example shows two important things:",[157,158,159,166],"ul",{},[160,161,162,163],"li",{},"A list uses square brackets ",[33,164,165],{},"[]",[160,167,168,169,171],{},"Python recognizes it as a ",[33,170,146],{}," type",[21,173,175],{"id":174},"what-a-list-is","What a list is",[14,177,178],{},"A list is a Python data type used to store multiple values in one variable.",[14,180,181],{},"Lists have a few easy-to-spot rules:",[157,183,184,189,192],{},[160,185,186,187],{},"They use square brackets: ",[33,188,165],{},[160,190,191],{},"Items are separated by commas",[160,193,194],{},"Items stay in order",[14,196,197],{},"Example:",[26,199,201],{"className":28,"code":200,"language":30,"meta":31,"style":31},"colors = [\"red\", \"green\", \"blue\"]\n",[33,202,203],{"__ignoreMap":31},[36,204,205,208,210,212,215,218,220,222,225,228,230,232,234,237,239],{"class":38,"line":39},[36,206,207],{"class":42},"colors ",[36,209,47],{"class":46},[36,211,51],{"class":50},[36,213,214],{"class":141},"\"",[36,216,217],{"class":145},"red",[36,219,214],{"class":141},[36,221,58],{"class":50},[36,223,224],{"class":141}," \"",[36,226,227],{"class":145},"green",[36,229,214],{"class":141},[36,231,58],{"class":50},[36,233,224],{"class":141},[36,235,236],{"class":145},"blue",[36,238,214],{"class":141},[36,240,69],{"class":50},[14,242,243,244,247],{},"Here, ",[33,245,246],{},"colors"," is a list with three items.",[21,249,251],{"id":250},"what-lists-are-used-for","What lists are used for",[14,253,254],{},"Lists are useful when you need to work with a group of related values.",[14,256,257],{},"Common uses include:",[157,259,260,263,266,269],{},[160,261,262],{},"Storing names, numbers, scores, or file paths",[160,264,265],{},"Looping through items one by one",[160,267,268],{},"Keeping data in a specific order",[160,270,271],{},"Changing the collection later by adding or removing items",[14,273,274],{},"For example, you could store student names in a list:",[26,276,278],{"className":28,"code":277,"language":30,"meta":31,"style":31},"students = [\"Maya\", \"Leo\", \"Sam\"]\n",[33,279,280],{"__ignoreMap":31},[36,281,282,285,287,289,291,294,296,298,300,303,305,307,309,312,314],{"class":38,"line":39},[36,283,284],{"class":42},"students ",[36,286,47],{"class":46},[36,288,51],{"class":50},[36,290,214],{"class":141},[36,292,293],{"class":145},"Maya",[36,295,214],{"class":141},[36,297,58],{"class":50},[36,299,224],{"class":141},[36,301,302],{"class":145},"Leo",[36,304,214],{"class":141},[36,306,58],{"class":50},[36,308,224],{"class":141},[36,310,311],{"class":145},"Sam",[36,313,214],{"class":141},[36,315,69],{"class":50},[14,317,318],{},"Then later, you could add another name or go through each student in a loop.",[21,320,322],{"id":321},"main-features-of-a-python-list","Main features of a Python list",[14,324,325],{},"Python lists have a few important features:",[157,327,328,338,352,360],{},[160,329,330,334,337],{},[331,332,333],"strong",{},"Lists are ordered",[335,336],"br",{},"\nThe first item stays first, the second stays second, and so on.",[160,339,340,343,345,346,351],{},[331,341,342],{},"Lists are mutable",[335,344],{},"\nThis means you can change them after creating them. You can add, remove, or update items. If you want to learn more about this idea, see ",[347,348,350],"a",{"href":349},"\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types","mutability in Python",".",[160,353,354,357,359],{},[331,355,356],{},"A list can contain different data types",[335,358],{},"\nA single list can store strings, numbers, booleans, or even other lists.",[160,361,362,365,367],{},[331,363,364],{},"A list can contain duplicate values",[335,366],{},"\nThe same value can appear more than once.",[14,369,197],{},[26,371,373],{"className":28,"code":372,"language":30,"meta":31,"style":31},"mixed = [\"apple\", 10, True, \"apple\"]\nprint(mixed)\n",[33,374,375,412],{"__ignoreMap":31},[36,376,377,380,382,384,386,389,391,393,396,398,402,404,406,408,410],{"class":38,"line":39},[36,378,379],{"class":42},"mixed ",[36,381,47],{"class":46},[36,383,51],{"class":50},[36,385,214],{"class":141},[36,387,388],{"class":145},"apple",[36,390,214],{"class":141},[36,392,58],{"class":50},[36,394,395],{"class":54}," 10",[36,397,58],{"class":50},[36,399,401],{"class":400},"s39Yj"," True",[36,403,58],{"class":50},[36,405,224],{"class":141},[36,407,388],{"class":145},[36,409,214],{"class":141},[36,411,69],{"class":50},[36,413,414,416,418,421],{"class":38,"line":72},[36,415,76],{"class":75},[36,417,79],{"class":50},[36,419,420],{"class":82},"mixed",[36,422,86],{"class":50},[21,424,426],{"id":425},"simple-example","Simple example",[14,428,429],{},"Here is a small example that shows how to create a list, get one item, add a new item, and print the result.",[26,431,433],{"className":28,"code":432,"language":30,"meta":31,"style":31},"names = [\"Ana\", \"Ben\", \"Cara\"]\n\nprint(names[0])\n\nnames.append(\"Diego\")\n\nprint(names)\n",[33,434,435,471,477,494,499,520,525],{"__ignoreMap":31},[36,436,437,440,442,444,446,449,451,453,455,458,460,462,464,467,469],{"class":38,"line":39},[36,438,439],{"class":42},"names ",[36,441,47],{"class":46},[36,443,51],{"class":50},[36,445,214],{"class":141},[36,447,448],{"class":145},"Ana",[36,450,214],{"class":141},[36,452,58],{"class":50},[36,454,224],{"class":141},[36,456,457],{"class":145},"Ben",[36,459,214],{"class":141},[36,461,58],{"class":50},[36,463,224],{"class":141},[36,465,466],{"class":145},"Cara",[36,468,214],{"class":141},[36,470,69],{"class":50},[36,472,473],{"class":38,"line":72},[36,474,476],{"emptyLinePlaceholder":475},true,"\n",[36,478,479,481,483,486,488,491],{"class":38,"line":89},[36,480,76],{"class":75},[36,482,79],{"class":50},[36,484,485],{"class":82},"names",[36,487,117],{"class":50},[36,489,490],{"class":54},"0",[36,492,493],{"class":50},"])\n",[36,495,497],{"class":38,"line":496},4,[36,498,476],{"emptyLinePlaceholder":475},[36,500,502,504,506,509,511,513,516,518],{"class":38,"line":501},5,[36,503,485],{"class":42},[36,505,351],{"class":50},[36,507,508],{"class":82},"append",[36,510,79],{"class":50},[36,512,214],{"class":141},[36,514,515],{"class":145},"Diego",[36,517,214],{"class":141},[36,519,86],{"class":50},[36,521,523],{"class":38,"line":522},6,[36,524,476],{"emptyLinePlaceholder":475},[36,526,528,530,532,534],{"class":38,"line":527},7,[36,529,76],{"class":75},[36,531,79],{"class":50},[36,533,485],{"class":82},[36,535,86],{"class":50},[14,537,107],{},[26,539,541],{"className":28,"code":540,"language":30,"meta":31,"style":31},"Ana\n['Ana', 'Ben', 'Cara', 'Diego']\n",[33,542,543,548],{"__ignoreMap":31},[36,544,545],{"class":38,"line":39},[36,546,547],{"class":42},"Ana\n",[36,549,550,552,554,556,558,560,562,564,566,568,570,572,574,576,578,580,582],{"class":38,"line":72},[36,551,117],{"class":50},[36,553,149],{"class":141},[36,555,448],{"class":145},[36,557,149],{"class":141},[36,559,58],{"class":50},[36,561,142],{"class":141},[36,563,457],{"class":145},[36,565,149],{"class":141},[36,567,58],{"class":50},[36,569,142],{"class":141},[36,571,466],{"class":145},[36,573,149],{"class":141},[36,575,58],{"class":50},[36,577,142],{"class":141},[36,579,515],{"class":145},[36,581,149],{"class":141},[36,583,69],{"class":50},[14,585,586],{},"What this code does:",[157,588,589,595,601,607],{},[160,590,591,594],{},[33,592,593],{},"names = [\"Ana\", \"Ben\", \"Cara\"]"," creates a list",[160,596,597,600],{},[33,598,599],{},"names[0]"," gets the first item",[160,602,603,606],{},[33,604,605],{},"append(\"Diego\")"," adds a new item to the end",[160,608,609,612],{},[33,610,611],{},"print(names)"," shows the updated list",[14,614,615,616,618,619,621,622,624],{},"A useful detail for beginners: list indexes start at ",[33,617,490],{},", not ",[33,620,55],{},". So ",[33,623,599],{}," means the first item.",[14,626,627,628,632,633,637,638,351],{},"If you want a full guide, see ",[347,629,631],{"href":630},"\u002Flearn\u002Fpython-lists-explained-beginner-guide","Python lists explained for beginners",". If you specifically want to add items, see ",[347,634,636],{"href":635},"\u002Fhow-to\u002Fhow-to-add-an-item-to-a-list-in-python","how to add an item to a list in Python"," or the ",[347,639,641],{"href":640},"\u002Freference\u002Fpython-list-append-method","Python list append() method",[21,643,645],{"id":644},"list-vs-other-collection-types","List vs other collection types",[14,647,648],{},"Lists are one of several ways to store groups of data in Python.",[14,650,651],{},"Use a list when:",[157,653,654,657,660],{},[160,655,656],{},"Order matters",[160,658,659],{},"You may need to change the data later",[160,661,662],{},"You want to access items by position",[14,664,665],{},"Here is how lists compare to other common collection types:",[157,667,668,674,680,686],{},[160,669,670,673],{},[331,671,672],{},"List",": ordered and changeable",[160,675,676,679],{},[331,677,678],{},"Tuple",": similar to a list, but not meant to be changed",[160,681,682,685],{},[331,683,684],{},"Set",": does not allow duplicates and does not keep items in a fixed order the same way",[160,687,688,691],{},[331,689,690],{},"Dictionary",": stores key-value pairs instead of positions",[14,693,694],{},"Examples:",[26,696,698],{"className":28,"code":697,"language":30,"meta":31,"style":31},"my_list = [\"a\", \"b\", \"c\"]\nmy_tuple = (\"a\", \"b\", \"c\")\nmy_set = {\"a\", \"b\", \"c\"}\nmy_dict = {\"name\": \"Ana\", \"age\": 12}\n",[33,699,700,735,769,804],{"__ignoreMap":31},[36,701,702,705,707,709,711,713,715,717,719,722,724,726,728,731,733],{"class":38,"line":39},[36,703,704],{"class":42},"my_list ",[36,706,47],{"class":46},[36,708,51],{"class":50},[36,710,214],{"class":141},[36,712,347],{"class":145},[36,714,214],{"class":141},[36,716,58],{"class":50},[36,718,224],{"class":141},[36,720,721],{"class":145},"b",[36,723,214],{"class":141},[36,725,58],{"class":50},[36,727,224],{"class":141},[36,729,730],{"class":145},"c",[36,732,214],{"class":141},[36,734,69],{"class":50},[36,736,737,740,742,745,747,749,751,753,755,757,759,761,763,765,767],{"class":38,"line":72},[36,738,739],{"class":42},"my_tuple ",[36,741,47],{"class":46},[36,743,744],{"class":50}," (",[36,746,214],{"class":141},[36,748,347],{"class":145},[36,750,214],{"class":141},[36,752,58],{"class":50},[36,754,224],{"class":141},[36,756,721],{"class":145},[36,758,214],{"class":141},[36,760,58],{"class":50},[36,762,224],{"class":141},[36,764,730],{"class":145},[36,766,214],{"class":141},[36,768,86],{"class":50},[36,770,771,774,776,779,781,783,785,787,789,791,793,795,797,799,801],{"class":38,"line":89},[36,772,773],{"class":42},"my_set ",[36,775,47],{"class":46},[36,777,778],{"class":50}," {",[36,780,214],{"class":141},[36,782,347],{"class":145},[36,784,214],{"class":141},[36,786,58],{"class":50},[36,788,224],{"class":141},[36,790,721],{"class":145},[36,792,214],{"class":141},[36,794,58],{"class":50},[36,796,224],{"class":141},[36,798,730],{"class":145},[36,800,214],{"class":141},[36,802,803],{"class":50},"}\n",[36,805,806,809,811,813,815,818,820,823,825,827,829,831,833,836,838,840,843],{"class":38,"line":496},[36,807,808],{"class":42},"my_dict ",[36,810,47],{"class":46},[36,812,778],{"class":50},[36,814,214],{"class":141},[36,816,817],{"class":145},"name",[36,819,214],{"class":141},[36,821,822],{"class":50},":",[36,824,224],{"class":141},[36,826,448],{"class":145},[36,828,214],{"class":141},[36,830,58],{"class":50},[36,832,224],{"class":141},[36,834,835],{"class":145},"age",[36,837,214],{"class":141},[36,839,822],{"class":50},[36,841,842],{"class":54}," 12",[36,844,803],{"class":50},[14,846,847,848,852,853,857],{},"If you want a beginner-friendly comparison, read ",[347,849,851],{"href":850},"\u002Flearn\u002Fwhen-to-use-lists-vs-tuples-vs-sets-vs-dictionaries","when to use lists vs tuples vs sets vs dictionaries",". You can also see ",[347,854,856],{"href":855},"\u002Fglossary\u002Fwhat-is-a-tuple-in-python","what is a tuple in Python"," for a closer look at tuples.",[21,859,861],{"id":860},"what-this-page-does-not-cover","What this page does not cover",[14,863,864],{},"This page is a glossary-style definition, not a full tutorial.",[14,866,867],{},"It does not cover:",[157,869,870,873,876],{},[160,871,872],{},"Every list method",[160,874,875],{},"Step-by-step tasks for adding or removing items",[160,877,878],{},"Advanced list topics",[14,880,881],{},"For the next step, you can read:",[157,883,884,890,896],{},[160,885,886],{},[347,887,889],{"href":888},"\u002Freference\u002Fpython-list-creating-a-list","how to create a list in Python",[160,891,892],{},[347,893,895],{"href":894},"\u002Fhow-to\u002Fhow-to-remove-an-item-from-a-list-in-python","how to remove an item from a list in Python",[160,897,898],{},[347,899,901],{"href":900},"\u002Freference\u002Fpython-list-length-len","Python len() for list length",[21,903,905],{"id":904},"common-mistakes","Common mistakes",[14,907,908],{},"Beginners often run into the same list-related problems. Here are a few common ones.",[157,910,911,919,938,950],{},[160,912,913,916,918],{},[331,914,915],{},"Thinking a list can only store one type of value",[335,917],{},"\nThis is not true. A list can store mixed types.",[160,920,921,924,926,927,929,930,933,934,937],{},[331,922,923],{},"Confusing lists with tuples or dictionaries",[335,925],{},"\nLists use square brackets ",[33,928,165],{},". Tuples use parentheses ",[33,931,932],{},"()",". Dictionaries use curly braces ",[33,935,936],{},"{}"," with keys and values.",[160,939,940,943,945,946,618,948,351],{},[331,941,942],{},"Forgetting that list indexes start at 0",[335,944],{},"\nThe first item is at index ",[33,947,490],{},[33,949,55],{},[160,951,952,955,957],{},[331,953,954],{},"Using parentheses instead of square brackets",[335,956],{},"\nThis creates a tuple, not a list.",[14,959,960],{},"These quick checks can help when you are unsure what a variable contains:",[26,962,964],{"className":28,"code":963,"language":30,"meta":31,"style":31},"print(my_list)\nprint(type(my_list))\nprint(len(my_list))\nprint(my_list[0])\n",[33,965,966,977,991,1006],{"__ignoreMap":31},[36,967,968,970,972,975],{"class":38,"line":39},[36,969,76],{"class":75},[36,971,79],{"class":50},[36,973,974],{"class":82},"my_list",[36,976,86],{"class":50},[36,978,979,981,983,985,987,989],{"class":38,"line":72},[36,980,76],{"class":75},[36,982,79],{"class":50},[36,984,97],{"class":96},[36,986,79],{"class":50},[36,988,974],{"class":82},[36,990,104],{"class":50},[36,992,993,995,997,1000,1002,1004],{"class":38,"line":89},[36,994,76],{"class":75},[36,996,79],{"class":50},[36,998,999],{"class":75},"len",[36,1001,79],{"class":50},[36,1003,974],{"class":82},[36,1005,104],{"class":50},[36,1007,1008,1010,1012,1014,1016,1018],{"class":38,"line":496},[36,1009,76],{"class":75},[36,1011,79],{"class":50},[36,1013,974],{"class":82},[36,1015,117],{"class":50},[36,1017,490],{"class":54},[36,1019,493],{"class":50},[14,1021,1022],{},"What each line does:",[157,1024,1025,1031,1037,1043],{},[160,1026,1027,1030],{},[33,1028,1029],{},"print(my_list)"," shows the full value",[160,1032,1033,1036],{},[33,1034,1035],{},"print(type(my_list))"," confirms whether it is a list",[160,1038,1039,1042],{},[33,1040,1041],{},"print(len(my_list))"," shows how many items it contains",[160,1044,1045,1048],{},[33,1046,1047],{},"print(my_list[0])"," shows the first item",[14,1050,1051,1052,1055,1056,351],{},"Be careful with ",[33,1053,1054],{},"my_list[0]"," if the list might be empty. If you access an index that does not exist, you may get an error. For help with that, see ",[347,1057,1059],{"href":1058},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained","IndexError: list index out of range",[21,1061,1063],{"id":1062},"faq","FAQ",[1065,1066,1068],"h3",{"id":1067},"what-is-a-list-in-python-in-simple-words","What is a list in Python in simple words?",[14,1070,1071],{},"A list is a container that holds multiple values in a single variable.",[1065,1073,1075],{"id":1074},"can-a-python-list-store-different-types-of-values","Can a Python list store different types of values?",[14,1077,1078],{},"Yes. A list can contain strings, numbers, booleans, and even other lists.",[1065,1080,1082],{"id":1081},"are-python-lists-ordered","Are Python lists ordered?",[14,1084,1085],{},"Yes. Items stay in a specific order, and you can access them by index.",[1065,1087,1089],{"id":1088},"can-you-change-a-list-after-creating-it","Can you change a list after creating it?",[14,1091,1092],{},"Yes. Lists are mutable, so you can add, remove, or update items.",[1065,1094,1096],{"id":1095},"what-symbols-are-used-to-create-a-list","What symbols are used to create a list?",[14,1098,1099,1100],{},"Square brackets: ",[33,1101,165],{},[21,1103,1105],{"id":1104},"see-also","See also",[157,1107,1108,1112,1117,1121,1126,1131],{},[160,1109,1110],{},[347,1111,631],{"href":630},[160,1113,1114],{},[347,1115,1116],{"href":888},"How to create a list in Python",[160,1118,1119],{},[347,1120,641],{"href":640},[160,1122,1123],{},[347,1124,1125],{"href":635},"How to add an item to a list in Python",[160,1127,1128],{},[347,1129,1130],{"href":894},"How to remove an item from a list in Python",[160,1132,1133],{},[347,1134,1135],{"href":850},"When to use lists vs tuples vs sets vs dictionaries",[1137,1138,1139],"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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":1141},[1142,1143,1144,1145,1146,1147,1148,1149,1150,1157],{"id":23,"depth":72,"text":24},{"id":174,"depth":72,"text":175},{"id":250,"depth":72,"text":251},{"id":321,"depth":72,"text":322},{"id":425,"depth":72,"text":426},{"id":644,"depth":72,"text":645},{"id":860,"depth":72,"text":861},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":1151},[1152,1153,1154,1155,1156],{"id":1067,"depth":89,"text":1068},{"id":1074,"depth":89,"text":1075},{"id":1081,"depth":89,"text":1082},{"id":1088,"depth":89,"text":1089},{"id":1095,"depth":89,"text":1096},{"id":1104,"depth":72,"text":1105},"Master what is a list in python in our comprehensive Python beginner guide.","md",{},"\u002Fglossary\u002Fwhat-is-a-list-in-python",{"title":5,"description":1158},"glossary\u002Fwhat-is-a-list-in-python","dh7DeBT0q0jLOhGG99UV3YC86vCppkWBoDPlSDWoyPQ",[1166,2957,4677,5929,7401,8740,9811,11482,12801,14744,15627,16326,18140,18919,19679,20901,22325,24337,25701,27033,28323,29566,30534,31732,33415,35283,36883,38127,39130,40551],{"id":1167,"title":1168,"body":1169,"description":2951,"extension":1159,"meta":2952,"navigation":475,"path":2953,"seo":2954,"stem":2955,"__hash__":2956},"content\u002Fglossary\u002Fwhat-is-a-boolean-in-python.md","What Is a Boolean in Python?",{"type":7,"value":1170,"toc":2910},[1171,1174,1184,1187,1194,1198,1206,1216,1219,1230,1233,1236,1255,1262,1265,1286,1290,1293,1303,1306,1319,1322,1328,1361,1363,1397,1402,1405,1470,1472,1501,1505,1508,1515,1569,1571,1588,1591,1611,1614,1646,1648,1656,1659,1670,1677,1681,1684,1690,1696,1747,1749,1762,1775,1782,1785,1827,1837,1841,1844,1859,1861,1902,1904,1916,1920,1923,1934,1936,1996,1998,2011,2015,2018,2027,2076,2082,2124,2126,2134,2141,2183,2185,2194,2202,2221,2223,2242,2246,2249,2257,2260,2321,2323,2365,2368,2375,2384,2431,2433,2471,2474,2486,2495,2498,2510,2513,2558,2561,2577,2586,2590,2593,2603,2606,2616,2618,2621,2652,2655,2749,2751,2787,2790,2808,2817,2819,2823,2830,2834,2843,2847,2858,2862,2867,2871,2877,2879,2907],[10,1172,1168],{"id":1173},"what-is-a-boolean-in-python",[14,1175,1176,1177,1180,1181,351],{},"A Boolean in Python is a value that can only be ",[33,1178,1179],{},"True"," or ",[33,1182,1183],{},"False",[14,1185,1186],{},"Beginners see Booleans very early because Python uses them to make decisions. For example, a program might check whether a user is logged in, whether a number is greater than another number, or whether a list has any items.",[14,1188,1189,1190,1193],{},"If you are new to Python, this page will help you understand what Booleans are, where they come from, and how they are used in simple ",[33,1191,1192],{},"if"," statements.",[21,1195,1197],{"id":1196},"what-a-boolean-means","What a Boolean means",[14,1199,1200,1201,1205],{},"A Boolean is a ",[347,1202,1204],{"href":1203},"\u002Fglossary\u002Fwhat-is-a-data-type-in-python\u002F","data type in Python"," with only two possible values:",[157,1207,1208,1212],{},[160,1209,1210],{},[33,1211,1179],{},[160,1213,1214],{},[33,1215,1183],{},[14,1217,1218],{},"You can think of a Boolean as a yes\u002Fno answer:",[157,1220,1221,1224,1227],{},[160,1222,1223],{},"yes \u002F no",[160,1225,1226],{},"on \u002F off",[160,1228,1229],{},"true \u002F false",[14,1231,1232],{},"Booleans are used when Python needs to check a condition.",[14,1234,1235],{},"For example:",[157,1237,1238,1249,1252],{},[160,1239,1240,1241,1244,1245,1248],{},"Is ",[33,1242,1243],{},"5"," greater than ",[33,1246,1247],{},"3","?",[160,1250,1251],{},"Is a user logged in?",[160,1253,1254],{},"Does a list contain any items?",[14,1256,1257,1258,1180,1260,351],{},"The answer to each of these is either ",[33,1259,1179],{},[33,1261,1183],{},[14,1263,1264],{},"Also note that Python is case-sensitive:",[157,1266,1267,1272,1276],{},[160,1268,1269,1271],{},[33,1270,1179],{}," is correct",[160,1273,1274,1271],{},[33,1275,1183],{},[160,1277,1278,1281,1282,1285],{},[33,1279,1280],{},"true"," and ",[33,1283,1284],{},"false"," are not correct in Python",[21,1287,1289],{"id":1288},"boolean-values-in-python","Boolean values in Python",[14,1291,1292],{},"Python has two built-in Boolean values:",[157,1294,1295,1299],{},[160,1296,1297],{},[33,1298,1179],{},[160,1300,1301],{},[33,1302,1183],{},[14,1304,1305],{},"These are not strings. That means:",[157,1307,1308,1313],{},[160,1309,1310,1312],{},[33,1311,1179],{}," is a Boolean value",[160,1314,1315,1318],{},[33,1316,1317],{},"\"True\""," is a string",[14,1320,1321],{},"That difference is important.",[14,1323,1324,1325,822],{},"You can check the type of a Boolean with ",[33,1326,1327],{},"type()",[26,1329,1331],{"className":28,"code":1330,"language":30,"meta":31,"style":31},"print(type(True))\nprint(type(False))\n",[33,1332,1333,1347],{"__ignoreMap":31},[36,1334,1335,1337,1339,1341,1343,1345],{"class":38,"line":39},[36,1336,76],{"class":75},[36,1338,79],{"class":50},[36,1340,97],{"class":96},[36,1342,79],{"class":50},[36,1344,1179],{"class":400},[36,1346,104],{"class":50},[36,1348,1349,1351,1353,1355,1357,1359],{"class":38,"line":72},[36,1350,76],{"class":75},[36,1352,79],{"class":50},[36,1354,97],{"class":96},[36,1356,79],{"class":50},[36,1358,1183],{"class":400},[36,1360,104],{"class":50},[14,1362,107],{},[26,1364,1366],{"className":28,"code":1365,"language":30,"meta":31,"style":31},"\u003Cclass 'bool'>\n\u003Cclass 'bool'>\n",[33,1367,1368,1383],{"__ignoreMap":31},[36,1369,1370,1372,1374,1376,1379,1381],{"class":38,"line":39},[36,1371,134],{"class":46},[36,1373,138],{"class":137},[36,1375,142],{"class":141},[36,1377,1378],{"class":145},"bool",[36,1380,149],{"class":141},[36,1382,152],{"class":46},[36,1384,1385,1387,1389,1391,1393,1395],{"class":38,"line":72},[36,1386,134],{"class":46},[36,1388,138],{"class":137},[36,1390,142],{"class":141},[36,1392,1378],{"class":145},[36,1394,149],{"class":141},[36,1396,152],{"class":46},[14,1398,1399,1400,351],{},"The Boolean type in Python is called ",[33,1401,1378],{},[14,1403,1404],{},"Here is a simple example:",[26,1406,1408],{"className":28,"code":1407,"language":30,"meta":31,"style":31},"is_logged_in = True\nhas_items = False\n\nprint(is_logged_in)\nprint(has_items)\nprint(type(is_logged_in))\n",[33,1409,1410,1420,1430,1434,1445,1456],{"__ignoreMap":31},[36,1411,1412,1415,1417],{"class":38,"line":39},[36,1413,1414],{"class":42},"is_logged_in ",[36,1416,47],{"class":46},[36,1418,1419],{"class":400}," True\n",[36,1421,1422,1425,1427],{"class":38,"line":72},[36,1423,1424],{"class":42},"has_items ",[36,1426,47],{"class":46},[36,1428,1429],{"class":400}," False\n",[36,1431,1432],{"class":38,"line":89},[36,1433,476],{"emptyLinePlaceholder":475},[36,1435,1436,1438,1440,1443],{"class":38,"line":496},[36,1437,76],{"class":75},[36,1439,79],{"class":50},[36,1441,1442],{"class":82},"is_logged_in",[36,1444,86],{"class":50},[36,1446,1447,1449,1451,1454],{"class":38,"line":501},[36,1448,76],{"class":75},[36,1450,79],{"class":50},[36,1452,1453],{"class":82},"has_items",[36,1455,86],{"class":50},[36,1457,1458,1460,1462,1464,1466,1468],{"class":38,"line":522},[36,1459,76],{"class":75},[36,1461,79],{"class":50},[36,1463,97],{"class":96},[36,1465,79],{"class":50},[36,1467,1442],{"class":82},[36,1469,104],{"class":50},[14,1471,107],{},[26,1473,1475],{"className":28,"code":1474,"language":30,"meta":31,"style":31},"True\nFalse\n\u003Cclass 'bool'>\n",[33,1476,1477,1482,1487],{"__ignoreMap":31},[36,1478,1479],{"class":38,"line":39},[36,1480,1481],{"class":400},"True\n",[36,1483,1484],{"class":38,"line":72},[36,1485,1486],{"class":400},"False\n",[36,1488,1489,1491,1493,1495,1497,1499],{"class":38,"line":89},[36,1490,134],{"class":46},[36,1492,138],{"class":137},[36,1494,142],{"class":141},[36,1496,1378],{"class":145},[36,1498,149],{"class":141},[36,1500,152],{"class":46},[21,1502,1504],{"id":1503},"how-booleans-are-created","How Booleans are created",[14,1506,1507],{},"One common way to create a Boolean is with a comparison.",[14,1509,1510,1511,1180,1513,351],{},"Comparisons ask a question and return ",[33,1512,1179],{},[33,1514,1183],{},[26,1516,1518],{"className":28,"code":1517,"language":30,"meta":31,"style":31},"print(5 > 3)\nprint(2 == 4)\nprint(10 != 7)\n",[33,1519,1520,1535,1552],{"__ignoreMap":31},[36,1521,1522,1524,1526,1528,1531,1533],{"class":38,"line":39},[36,1523,76],{"class":75},[36,1525,79],{"class":50},[36,1527,1243],{"class":54},[36,1529,1530],{"class":46}," >",[36,1532,66],{"class":54},[36,1534,86],{"class":50},[36,1536,1537,1539,1541,1544,1547,1550],{"class":38,"line":72},[36,1538,76],{"class":75},[36,1540,79],{"class":50},[36,1542,1543],{"class":54},"2",[36,1545,1546],{"class":46}," ==",[36,1548,1549],{"class":54}," 4",[36,1551,86],{"class":50},[36,1553,1554,1556,1558,1561,1564,1567],{"class":38,"line":89},[36,1555,76],{"class":75},[36,1557,79],{"class":50},[36,1559,1560],{"class":54},"10",[36,1562,1563],{"class":46}," !=",[36,1565,1566],{"class":54}," 7",[36,1568,86],{"class":50},[14,1570,107],{},[26,1572,1574],{"className":28,"code":1573,"language":30,"meta":31,"style":31},"True\nFalse\nTrue\n",[33,1575,1576,1580,1584],{"__ignoreMap":31},[36,1577,1578],{"class":38,"line":39},[36,1579,1481],{"class":400},[36,1581,1582],{"class":38,"line":72},[36,1583,1486],{"class":400},[36,1585,1586],{"class":38,"line":89},[36,1587,1481],{"class":400},[14,1589,1590],{},"In these examples:",[157,1592,1593,1599,1605],{},[160,1594,1595,1598],{},[33,1596,1597],{},"5 > 3"," means “is 5 greater than 3?”",[160,1600,1601,1604],{},[33,1602,1603],{},"2 == 4"," means “is 2 equal to 4?”",[160,1606,1607,1610],{},[33,1608,1609],{},"10 != 7"," means “is 10 not equal to 7?”",[14,1612,1613],{},"You can also store comparison results in variables:",[26,1615,1617],{"className":28,"code":1616,"language":30,"meta":31,"style":31},"is_adult = 20 >= 18\nprint(is_adult)\n",[33,1618,1619,1635],{"__ignoreMap":31},[36,1620,1621,1624,1626,1629,1632],{"class":38,"line":39},[36,1622,1623],{"class":42},"is_adult ",[36,1625,47],{"class":46},[36,1627,1628],{"class":54}," 20",[36,1630,1631],{"class":46}," >=",[36,1633,1634],{"class":54}," 18\n",[36,1636,1637,1639,1641,1644],{"class":38,"line":72},[36,1638,76],{"class":75},[36,1640,79],{"class":50},[36,1642,1643],{"class":82},"is_adult",[36,1645,86],{"class":50},[14,1647,107],{},[26,1649,1650],{"className":28,"code":1481,"language":30,"meta":31,"style":31},[33,1651,1652],{"__ignoreMap":31},[36,1653,1654],{"class":38,"line":39},[36,1655,1481],{"class":400},[14,1657,1658],{},"Functions can return Booleans too. You will see this often as you learn more Python.",[14,1660,1661,1662,1669],{},"Expressions inside ",[347,1663,1665,1666,1668],{"href":1664},"\u002Flearn\u002Fpython-if-statements-explained\u002F","Python ",[33,1667,1192],{}," statements"," are also checked as true or false. That is how Python decides which block of code to run.",[14,1671,1672,1673,351],{},"If you want a fuller beginner lesson, see ",[347,1674,1676],{"href":1675},"\u002Flearn\u002Fpython-booleans-explained-true-and-false\u002F","Python Booleans explained: True and False",[21,1678,1680],{"id":1679},"where-beginners-use-booleans","Where beginners use Booleans",[14,1682,1683],{},"Beginners usually use Booleans in a few common places.",[1065,1685,1687,1688,1668],{"id":1686},"in-if-statements","In ",[33,1689,1192],{},[14,1691,1692,1693,1695],{},"Booleans are very common in ",[33,1694,1192],{}," statements because they control program flow.",[26,1697,1699],{"className":28,"code":1698,"language":30,"meta":31,"style":31},"age = 18\n\nif age >= 18:\n    print(\"You are an adult.\")\n",[33,1700,1701,1710,1714,1731],{"__ignoreMap":31},[36,1702,1703,1706,1708],{"class":38,"line":39},[36,1704,1705],{"class":42},"age ",[36,1707,47],{"class":46},[36,1709,1634],{"class":54},[36,1711,1712],{"class":38,"line":72},[36,1713,476],{"emptyLinePlaceholder":475},[36,1715,1716,1719,1722,1725,1728],{"class":38,"line":89},[36,1717,1192],{"class":1718},"sVHd0",[36,1720,1721],{"class":42}," age ",[36,1723,1724],{"class":46},">=",[36,1726,1727],{"class":54}," 18",[36,1729,1730],{"class":50},":\n",[36,1732,1733,1736,1738,1740,1743,1745],{"class":38,"line":496},[36,1734,1735],{"class":75},"    print",[36,1737,79],{"class":50},[36,1739,214],{"class":141},[36,1741,1742],{"class":145},"You are an adult.",[36,1744,214],{"class":141},[36,1746,86],{"class":50},[14,1748,107],{},[26,1750,1752],{"className":28,"code":1751,"language":30,"meta":31,"style":31},"You are an adult.\n",[33,1753,1754],{"__ignoreMap":31},[36,1755,1756,1759],{"class":38,"line":39},[36,1757,1758],{"class":42},"You are an adult",[36,1760,1761],{"class":50},".\n",[14,1763,1764,1765,1768,1769,1771,1772,1774],{},"The comparison ",[33,1766,1767],{},"age >= 18"," returns a Boolean. If it is ",[33,1770,1179],{},", the code inside the ",[33,1773,1192],{}," block runs.",[1065,1776,1687,1778,1781],{"id":1777},"in-while-loops",[33,1779,1780],{},"while"," loops",[14,1783,1784],{},"Booleans can also control whether a loop keeps running.",[26,1786,1788],{"className":28,"code":1787,"language":30,"meta":31,"style":31},"running = False\n\nwhile running:\n    print(\"This will not print.\")\n",[33,1789,1790,1799,1803,1812],{"__ignoreMap":31},[36,1791,1792,1795,1797],{"class":38,"line":39},[36,1793,1794],{"class":42},"running ",[36,1796,47],{"class":46},[36,1798,1429],{"class":400},[36,1800,1801],{"class":38,"line":72},[36,1802,476],{"emptyLinePlaceholder":475},[36,1804,1805,1807,1810],{"class":38,"line":89},[36,1806,1780],{"class":1718},[36,1808,1809],{"class":42}," running",[36,1811,1730],{"class":50},[36,1813,1814,1816,1818,1820,1823,1825],{"class":38,"line":496},[36,1815,1735],{"class":75},[36,1817,79],{"class":50},[36,1819,214],{"class":141},[36,1821,1822],{"class":145},"This will not print.",[36,1824,214],{"class":141},[36,1826,86],{"class":50},[14,1828,1829,1830,1833,1834,1836],{},"Because ",[33,1831,1832],{},"running"," is ",[33,1835,1183],{},", the loop does not run.",[1065,1838,1840],{"id":1839},"in-variables","In variables",[14,1842,1843],{},"Booleans are often stored in clearly named variables such as:",[157,1845,1846,1850,1854],{},[160,1847,1848],{},[33,1849,1442],{},[160,1851,1852],{},[33,1853,1453],{},[160,1855,1856],{},[33,1857,1858],{},"is_ready",[14,1860,197],{},[26,1862,1864],{"className":28,"code":1863,"language":30,"meta":31,"style":31},"has_items = True\n\nif has_items:\n    print(\"Your cart has items.\")\n",[33,1865,1866,1874,1878,1887],{"__ignoreMap":31},[36,1867,1868,1870,1872],{"class":38,"line":39},[36,1869,1424],{"class":42},[36,1871,47],{"class":46},[36,1873,1419],{"class":400},[36,1875,1876],{"class":38,"line":72},[36,1877,476],{"emptyLinePlaceholder":475},[36,1879,1880,1882,1885],{"class":38,"line":89},[36,1881,1192],{"class":1718},[36,1883,1884],{"class":42}," has_items",[36,1886,1730],{"class":50},[36,1888,1889,1891,1893,1895,1898,1900],{"class":38,"line":496},[36,1890,1735],{"class":75},[36,1892,79],{"class":50},[36,1894,214],{"class":141},[36,1896,1897],{"class":145},"Your cart has items.",[36,1899,214],{"class":141},[36,1901,86],{"class":50},[14,1903,107],{},[26,1905,1907],{"className":28,"code":1906,"language":30,"meta":31,"style":31},"Your cart has items.\n",[33,1908,1909],{"__ignoreMap":31},[36,1910,1911,1914],{"class":38,"line":39},[36,1912,1913],{"class":42},"Your cart has items",[36,1915,1761],{"class":50},[1065,1917,1919],{"id":1918},"when-checking-conditions","When checking conditions",[14,1921,1922],{},"Booleans are useful when checking things like:",[157,1924,1925,1928,1931],{},[160,1926,1927],{},"equality",[160,1929,1930],{},"size",[160,1932,1933],{},"existence",[14,1935,1235],{},[26,1937,1939],{"className":28,"code":1938,"language":30,"meta":31,"style":31},"name = \"Sam\"\nprint(name == \"Sam\")\nprint(len(name) > 0)\n",[33,1940,1941,1955,1974],{"__ignoreMap":31},[36,1942,1943,1946,1948,1950,1952],{"class":38,"line":39},[36,1944,1945],{"class":42},"name ",[36,1947,47],{"class":46},[36,1949,224],{"class":141},[36,1951,311],{"class":145},[36,1953,1954],{"class":141},"\"\n",[36,1956,1957,1959,1961,1963,1966,1968,1970,1972],{"class":38,"line":72},[36,1958,76],{"class":75},[36,1960,79],{"class":50},[36,1962,1945],{"class":82},[36,1964,1965],{"class":46},"==",[36,1967,224],{"class":141},[36,1969,311],{"class":145},[36,1971,214],{"class":141},[36,1973,86],{"class":50},[36,1975,1976,1978,1980,1982,1984,1986,1989,1991,1994],{"class":38,"line":89},[36,1977,76],{"class":75},[36,1979,79],{"class":50},[36,1981,999],{"class":75},[36,1983,79],{"class":50},[36,1985,817],{"class":82},[36,1987,1988],{"class":50},")",[36,1990,1530],{"class":46},[36,1992,1993],{"class":54}," 0",[36,1995,86],{"class":50},[14,1997,107],{},[26,1999,2001],{"className":28,"code":2000,"language":30,"meta":31,"style":31},"True\nTrue\n",[33,2002,2003,2007],{"__ignoreMap":31},[36,2004,2005],{"class":38,"line":39},[36,2006,1481],{"class":400},[36,2008,2009],{"class":38,"line":72},[36,2010,1481],{"class":400},[21,2012,2014],{"id":2013},"boolean-examples-to-include","Boolean examples to include",[14,2016,2017],{},"Here are a few simple Boolean examples in one place.",[1065,2019,2021,2022,1281,2024,2026],{"id":2020},"assign-true-and-false-to-variables","Assign ",[33,2023,1179],{},[33,2025,1183],{}," to variables",[26,2028,2030],{"className":28,"code":2029,"language":30,"meta":31,"style":31},"is_open = True\nis_closed = False\n\nprint(is_open)\nprint(is_closed)\n",[33,2031,2032,2041,2050,2054,2065],{"__ignoreMap":31},[36,2033,2034,2037,2039],{"class":38,"line":39},[36,2035,2036],{"class":42},"is_open ",[36,2038,47],{"class":46},[36,2040,1419],{"class":400},[36,2042,2043,2046,2048],{"class":38,"line":72},[36,2044,2045],{"class":42},"is_closed ",[36,2047,47],{"class":46},[36,2049,1429],{"class":400},[36,2051,2052],{"class":38,"line":89},[36,2053,476],{"emptyLinePlaceholder":475},[36,2055,2056,2058,2060,2063],{"class":38,"line":496},[36,2057,76],{"class":75},[36,2059,79],{"class":50},[36,2061,2062],{"class":82},"is_open",[36,2064,86],{"class":50},[36,2066,2067,2069,2071,2074],{"class":38,"line":501},[36,2068,76],{"class":75},[36,2070,79],{"class":50},[36,2072,2073],{"class":82},"is_closed",[36,2075,86],{"class":50},[1065,2077,2079,2080],{"id":2078},"use-a-comparison-like-age-18","Use a comparison like ",[33,2081,1767],{},[26,2083,2085],{"className":28,"code":2084,"language":30,"meta":31,"style":31},"age = 16\ncan_vote = age >= 18\n\nprint(can_vote)\n",[33,2086,2087,2096,2109,2113],{"__ignoreMap":31},[36,2088,2089,2091,2093],{"class":38,"line":39},[36,2090,1705],{"class":42},[36,2092,47],{"class":46},[36,2094,2095],{"class":54}," 16\n",[36,2097,2098,2101,2103,2105,2107],{"class":38,"line":72},[36,2099,2100],{"class":42},"can_vote ",[36,2102,47],{"class":46},[36,2104,1721],{"class":42},[36,2106,1724],{"class":46},[36,2108,1634],{"class":54},[36,2110,2111],{"class":38,"line":89},[36,2112,476],{"emptyLinePlaceholder":475},[36,2114,2115,2117,2119,2122],{"class":38,"line":496},[36,2116,76],{"class":75},[36,2118,79],{"class":50},[36,2120,2121],{"class":82},"can_vote",[36,2123,86],{"class":50},[14,2125,107],{},[26,2127,2128],{"className":28,"code":1486,"language":30,"meta":31,"style":31},[33,2129,2130],{"__ignoreMap":31},[36,2131,2132],{"class":38,"line":39},[36,2133,1486],{"class":400},[1065,2135,2137,2138,2140],{"id":2136},"use-a-boolean-in-an-if-statement","Use a Boolean in an ",[33,2139,1192],{}," statement",[26,2142,2144],{"className":28,"code":2143,"language":30,"meta":31,"style":31},"is_member = True\n\nif is_member:\n    print(\"Welcome back!\")\n",[33,2145,2146,2155,2159,2168],{"__ignoreMap":31},[36,2147,2148,2151,2153],{"class":38,"line":39},[36,2149,2150],{"class":42},"is_member ",[36,2152,47],{"class":46},[36,2154,1419],{"class":400},[36,2156,2157],{"class":38,"line":72},[36,2158,476],{"emptyLinePlaceholder":475},[36,2160,2161,2163,2166],{"class":38,"line":89},[36,2162,1192],{"class":1718},[36,2164,2165],{"class":42}," is_member",[36,2167,1730],{"class":50},[36,2169,2170,2172,2174,2176,2179,2181],{"class":38,"line":496},[36,2171,1735],{"class":75},[36,2173,79],{"class":50},[36,2175,214],{"class":141},[36,2177,2178],{"class":145},"Welcome back!",[36,2180,214],{"class":141},[36,2182,86],{"class":50},[14,2184,107],{},[26,2186,2188],{"className":28,"code":2187,"language":30,"meta":31,"style":31},"Welcome back!\n",[33,2189,2190],{"__ignoreMap":31},[36,2191,2192],{"class":38,"line":39},[36,2193,2187],{"class":42},[1065,2195,2197,2198,2201],{"id":2196},"show-typetrue-output","Show ",[33,2199,2200],{},"type(True)"," output",[26,2203,2205],{"className":28,"code":2204,"language":30,"meta":31,"style":31},"print(type(True))\n",[33,2206,2207],{"__ignoreMap":31},[36,2208,2209,2211,2213,2215,2217,2219],{"class":38,"line":39},[36,2210,76],{"class":75},[36,2212,79],{"class":50},[36,2214,97],{"class":96},[36,2216,79],{"class":50},[36,2218,1179],{"class":400},[36,2220,104],{"class":50},[14,2222,107],{},[26,2224,2226],{"className":28,"code":2225,"language":30,"meta":31,"style":31},"\u003Cclass 'bool'>\n",[33,2227,2228],{"__ignoreMap":31},[36,2229,2230,2232,2234,2236,2238,2240],{"class":38,"line":39},[36,2231,134],{"class":46},[36,2233,138],{"class":137},[36,2235,142],{"class":141},[36,2237,1378],{"class":145},[36,2239,149],{"class":141},[36,2241,152],{"class":46},[21,2243,2245],{"id":2244},"common-beginner-confusion","Common beginner confusion",[14,2247,2248],{},"A few Boolean mistakes are very common when you are starting out.",[1065,2250,2252,2254,2255],{"id":2251},"true-is-not-the-same-as-true",[33,2253,1179],{}," is not the same as ",[33,2256,1317],{},[14,2258,2259],{},"This is a very important difference:",[26,2261,2263],{"className":28,"code":2262,"language":30,"meta":31,"style":31},"print(True)\nprint(\"True\")\nprint(type(True))\nprint(type(\"True\"))\n",[33,2264,2265,2275,2289,2303],{"__ignoreMap":31},[36,2266,2267,2269,2271,2273],{"class":38,"line":39},[36,2268,76],{"class":75},[36,2270,79],{"class":50},[36,2272,1179],{"class":400},[36,2274,86],{"class":50},[36,2276,2277,2279,2281,2283,2285,2287],{"class":38,"line":72},[36,2278,76],{"class":75},[36,2280,79],{"class":50},[36,2282,214],{"class":141},[36,2284,1179],{"class":145},[36,2286,214],{"class":141},[36,2288,86],{"class":50},[36,2290,2291,2293,2295,2297,2299,2301],{"class":38,"line":89},[36,2292,76],{"class":75},[36,2294,79],{"class":50},[36,2296,97],{"class":96},[36,2298,79],{"class":50},[36,2300,1179],{"class":400},[36,2302,104],{"class":50},[36,2304,2305,2307,2309,2311,2313,2315,2317,2319],{"class":38,"line":496},[36,2306,76],{"class":75},[36,2308,79],{"class":50},[36,2310,97],{"class":96},[36,2312,79],{"class":50},[36,2314,214],{"class":141},[36,2316,1179],{"class":145},[36,2318,214],{"class":141},[36,2320,104],{"class":50},[14,2322,107],{},[26,2324,2326],{"className":28,"code":2325,"language":30,"meta":31,"style":31},"True\nTrue\n\u003Cclass 'bool'>\n\u003Cclass 'str'>\n",[33,2327,2328,2332,2336,2350],{"__ignoreMap":31},[36,2329,2330],{"class":38,"line":39},[36,2331,1481],{"class":400},[36,2333,2334],{"class":38,"line":72},[36,2335,1481],{"class":400},[36,2337,2338,2340,2342,2344,2346,2348],{"class":38,"line":89},[36,2339,134],{"class":46},[36,2341,138],{"class":137},[36,2343,142],{"class":141},[36,2345,1378],{"class":145},[36,2347,149],{"class":141},[36,2349,152],{"class":46},[36,2351,2352,2354,2356,2358,2361,2363],{"class":38,"line":496},[36,2353,134],{"class":46},[36,2355,138],{"class":137},[36,2357,142],{"class":141},[36,2359,2360],{"class":145},"str",[36,2362,149],{"class":141},[36,2364,152],{"class":46},[14,2366,2367],{},"They may look similar when printed, but they are different types.",[1065,2369,2371,2254,2373],{"id":2370},"false-is-not-the-same-as-0",[33,2372,1183],{},[33,2374,490],{},[14,2376,2377,2378,2380,2381,2383],{},"In some Python contexts, ",[33,2379,1183],{}," is related to ",[33,2382,490],{},", but they are not the same thing conceptually.",[26,2385,2387],{"className":28,"code":2386,"language":30,"meta":31,"style":31},"print(False == 0)\nprint(type(False))\nprint(type(0))\n",[33,2388,2389,2403,2417],{"__ignoreMap":31},[36,2390,2391,2393,2395,2397,2399,2401],{"class":38,"line":39},[36,2392,76],{"class":75},[36,2394,79],{"class":50},[36,2396,1183],{"class":400},[36,2398,1546],{"class":46},[36,2400,1993],{"class":54},[36,2402,86],{"class":50},[36,2404,2405,2407,2409,2411,2413,2415],{"class":38,"line":72},[36,2406,76],{"class":75},[36,2408,79],{"class":50},[36,2410,97],{"class":96},[36,2412,79],{"class":50},[36,2414,1183],{"class":400},[36,2416,104],{"class":50},[36,2418,2419,2421,2423,2425,2427,2429],{"class":38,"line":89},[36,2420,76],{"class":75},[36,2422,79],{"class":50},[36,2424,97],{"class":96},[36,2426,79],{"class":50},[36,2428,490],{"class":54},[36,2430,104],{"class":50},[14,2432,107],{},[26,2434,2436],{"className":28,"code":2435,"language":30,"meta":31,"style":31},"True\n\u003Cclass 'bool'>\n\u003Cclass 'int'>\n",[33,2437,2438,2442,2456],{"__ignoreMap":31},[36,2439,2440],{"class":38,"line":39},[36,2441,1481],{"class":400},[36,2443,2444,2446,2448,2450,2452,2454],{"class":38,"line":72},[36,2445,134],{"class":46},[36,2447,138],{"class":137},[36,2449,142],{"class":141},[36,2451,1378],{"class":145},[36,2453,149],{"class":141},[36,2455,152],{"class":46},[36,2457,2458,2460,2462,2464,2467,2469],{"class":38,"line":89},[36,2459,134],{"class":46},[36,2461,138],{"class":137},[36,2463,142],{"class":141},[36,2465,2466],{"class":145},"int",[36,2468,149],{"class":141},[36,2470,152],{"class":46},[14,2472,2473],{},"As a beginner, it is best to think of:",[157,2475,2476,2481],{},[160,2477,2478,2480],{},[33,2479,1183],{}," as a Boolean value",[160,2482,2483,2485],{},[33,2484,490],{}," as a number",[1065,2487,2489,2490,2492,2493],{"id":2488},"use-to-compare-not","Use ",[33,2491,1965],{}," to compare, not ",[33,2494,47],{},[14,2496,2497],{},"This is a very common mistake.",[157,2499,2500,2505],{},[160,2501,2502,2504],{},[33,2503,47],{}," assigns a value",[160,2506,2507,2509],{},[33,2508,1965],{}," compares two values",[14,2511,2512],{},"Correct:",[26,2514,2516],{"className":28,"code":2515,"language":30,"meta":31,"style":31},"age = 20\n\nif age == 20:\n    print(\"Age is 20\")\n",[33,2517,2518,2527,2531,2543],{"__ignoreMap":31},[36,2519,2520,2522,2524],{"class":38,"line":39},[36,2521,1705],{"class":42},[36,2523,47],{"class":46},[36,2525,2526],{"class":54}," 20\n",[36,2528,2529],{"class":38,"line":72},[36,2530,476],{"emptyLinePlaceholder":475},[36,2532,2533,2535,2537,2539,2541],{"class":38,"line":89},[36,2534,1192],{"class":1718},[36,2536,1721],{"class":42},[36,2538,1965],{"class":46},[36,2540,1628],{"class":54},[36,2542,1730],{"class":50},[36,2544,2545,2547,2549,2551,2554,2556],{"class":38,"line":496},[36,2546,1735],{"class":75},[36,2548,79],{"class":50},[36,2550,214],{"class":141},[36,2552,2553],{"class":145},"Age is 20",[36,2555,214],{"class":141},[36,2557,86],{"class":50},[14,2559,2560],{},"Wrong:",[26,2562,2564],{"className":28,"code":2563,"language":30,"meta":31,"style":31},"# if age = 20:\n#     print(\"Age is 20\")\n",[33,2565,2566,2572],{"__ignoreMap":31},[36,2567,2568],{"class":38,"line":39},[36,2569,2571],{"class":2570},"sutJx","# if age = 20:\n",[36,2573,2574],{"class":38,"line":72},[36,2575,2576],{"class":2570},"#     print(\"Age is 20\")\n",[14,2578,2579,2580,2582,2583,2585],{},"The wrong version causes an error because ",[33,2581,47],{}," cannot be used for comparison inside an ",[33,2584,1192],{}," condition.",[1065,2587,2589],{"id":2588},"python-keywords-are-case-sensitive","Python keywords are case-sensitive",[14,2591,2592],{},"These are correct:",[157,2594,2595,2599],{},[160,2596,2597],{},[33,2598,1179],{},[160,2600,2601],{},[33,2602,1183],{},[14,2604,2605],{},"These are wrong:",[157,2607,2608,2612],{},[160,2609,2610],{},[33,2611,1280],{},[160,2613,2614],{},[33,2615,1284],{},[21,2617,905],{"id":904},[14,2619,2620],{},"These problems often confuse beginners:",[157,2622,2623,2631,2637,2646],{},[160,2624,2625,2626,1180,2628,2630],{},"Writing ",[33,2627,1280],{},[33,2629,1284],{}," in lowercase",[160,2632,2633,2634,2636],{},"Using strings like ",[33,2635,1317],{}," when a real Boolean is needed",[160,2638,2639,2640,2642,2643,2645],{},"Using ",[33,2641,47],{}," instead of ",[33,2644,1965],{}," in a condition",[160,2647,2648,2649,2651],{},"Assuming every non-empty value is literally ",[33,2650,1179],{}," instead of just truthy",[14,2653,2654],{},"If something is not working, these quick checks can help:",[26,2656,2658],{"className":28,"code":2657,"language":30,"meta":31,"style":31},"value = \"hello\"\n\nprint(value)\nprint(type(value))\nprint(5 > 3)\nprint(bool(0))\nprint(bool(\"hello\"))\n",[33,2659,2660,2674,2678,2689,2703,2717,2731],{"__ignoreMap":31},[36,2661,2662,2665,2667,2669,2672],{"class":38,"line":39},[36,2663,2664],{"class":42},"value ",[36,2666,47],{"class":46},[36,2668,224],{"class":141},[36,2670,2671],{"class":145},"hello",[36,2673,1954],{"class":141},[36,2675,2676],{"class":38,"line":72},[36,2677,476],{"emptyLinePlaceholder":475},[36,2679,2680,2682,2684,2687],{"class":38,"line":89},[36,2681,76],{"class":75},[36,2683,79],{"class":50},[36,2685,2686],{"class":82},"value",[36,2688,86],{"class":50},[36,2690,2691,2693,2695,2697,2699,2701],{"class":38,"line":496},[36,2692,76],{"class":75},[36,2694,79],{"class":50},[36,2696,97],{"class":96},[36,2698,79],{"class":50},[36,2700,2686],{"class":82},[36,2702,104],{"class":50},[36,2704,2705,2707,2709,2711,2713,2715],{"class":38,"line":501},[36,2706,76],{"class":75},[36,2708,79],{"class":50},[36,2710,1243],{"class":54},[36,2712,1530],{"class":46},[36,2714,66],{"class":54},[36,2716,86],{"class":50},[36,2718,2719,2721,2723,2725,2727,2729],{"class":38,"line":522},[36,2720,76],{"class":75},[36,2722,79],{"class":50},[36,2724,1378],{"class":96},[36,2726,79],{"class":50},[36,2728,490],{"class":54},[36,2730,104],{"class":50},[36,2732,2733,2735,2737,2739,2741,2743,2745,2747],{"class":38,"line":527},[36,2734,76],{"class":75},[36,2736,79],{"class":50},[36,2738,1378],{"class":96},[36,2740,79],{"class":50},[36,2742,214],{"class":141},[36,2744,2671],{"class":145},[36,2746,214],{"class":141},[36,2748,104],{"class":50},[14,2750,107],{},[26,2752,2754],{"className":28,"code":2753,"language":30,"meta":31,"style":31},"hello\n\u003Cclass 'str'>\nTrue\nFalse\nTrue\n",[33,2755,2756,2761,2775,2779,2783],{"__ignoreMap":31},[36,2757,2758],{"class":38,"line":39},[36,2759,2760],{"class":42},"hello\n",[36,2762,2763,2765,2767,2769,2771,2773],{"class":38,"line":72},[36,2764,134],{"class":46},[36,2766,138],{"class":137},[36,2768,142],{"class":141},[36,2770,2360],{"class":145},[36,2772,149],{"class":141},[36,2774,152],{"class":46},[36,2776,2777],{"class":38,"line":89},[36,2778,1481],{"class":400},[36,2780,2781],{"class":38,"line":496},[36,2782,1486],{"class":400},[36,2784,2785],{"class":38,"line":501},[36,2786,1481],{"class":400},[14,2788,2789],{},"These lines help you check:",[157,2791,2792,2795,2798,2801],{},[160,2793,2794],{},"the actual value",[160,2796,2797],{},"the type of the value",[160,2799,2800],{},"whether an expression returns a Boolean",[160,2802,2803,2804,2807],{},"how ",[33,2805,2806],{},"bool()"," converts values",[14,2809,2810,2811,351],{},"To learn more about this conversion, see the ",[347,2812,2814,2816],{"href":2813},"\u002Freference\u002Fpython-bool-function-explained\u002F",[33,2815,2806],{}," function explained",[21,2818,1063],{"id":1062},[1065,2820,2822],{"id":2821},"what-is-a-boolean-in-python-in-simple-words","What is a Boolean in Python in simple words?",[14,2824,2825,2826,1180,2828,351],{},"A Boolean is a value that can only be ",[33,2827,1179],{},[33,2829,1183],{},[1065,2831,2833],{"id":2832},"is-true-a-string-in-python","Is True a string in Python?",[14,2835,2836,2837,2839,2840,2842],{},"No. ",[33,2838,1179],{}," is a Boolean value. ",[33,2841,1317],{}," is a string.",[1065,2844,2846],{"id":2845},"how-do-i-make-a-boolean-in-python","How do I make a Boolean in Python?",[14,2848,2849,2850,1180,2852,2854,2855,351],{},"You can assign ",[33,2851,1179],{},[33,2853,1183],{}," directly, or create one with a comparison like ",[33,2856,2857],{},"10 > 5",[1065,2859,2861],{"id":2860},"what-is-the-boolean-type-in-python","What is the Boolean type in Python?",[14,2863,2864,2865,351],{},"The Boolean type is ",[33,2866,1378],{},[1065,2868,2870],{"id":2869},"where-are-booleans-used","Where are Booleans used?",[14,2872,2873,2874,2876],{},"They are used in ",[33,2875,1192],{}," statements, loops, comparisons, and functions that return yes\u002Fno results.",[21,2878,1105],{"id":1104},[157,2880,2881,2885,2890,2896,2902],{},[160,2882,2883],{},[347,2884,1676],{"href":1675},[160,2886,2887],{},[347,2888,2889],{"href":1664},"Python if statements explained",[160,2891,2892],{},[347,2893,2895],{"href":2894},"\u002Flearn\u002Fpython-data-types-overview\u002F","Python data types overview",[160,2897,2898],{},[347,2899,1665,2900,2816],{"href":2813},[33,2901,2806],{},[160,2903,2904],{},[347,2905,2906],{"href":1203},"What is a data type in Python?",[1137,2908,2909],{},"html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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 .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 .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}",{"title":31,"searchDepth":72,"depth":72,"links":2911},[2912,2913,2914,2915,2923,2933,2942,2943,2950],{"id":1196,"depth":72,"text":1197},{"id":1288,"depth":72,"text":1289},{"id":1503,"depth":72,"text":1504},{"id":1679,"depth":72,"text":1680,"children":2916},[2917,2919,2921,2922],{"id":1686,"depth":89,"text":2918},"In if statements",{"id":1777,"depth":89,"text":2920},"In while loops",{"id":1839,"depth":89,"text":1840},{"id":1918,"depth":89,"text":1919},{"id":2013,"depth":72,"text":2014,"children":2924},[2925,2927,2929,2931],{"id":2020,"depth":89,"text":2926},"Assign True and False to variables",{"id":2078,"depth":89,"text":2928},"Use a comparison like age >= 18",{"id":2136,"depth":89,"text":2930},"Use a Boolean in an if statement",{"id":2196,"depth":89,"text":2932},"Show type(True) output",{"id":2244,"depth":72,"text":2245,"children":2934},[2935,2937,2939,2941],{"id":2251,"depth":89,"text":2936},"True is not the same as \"True\"",{"id":2370,"depth":89,"text":2938},"False is not the same as 0",{"id":2488,"depth":89,"text":2940},"Use == to compare, not =",{"id":2588,"depth":89,"text":2589},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":2944},[2945,2946,2947,2948,2949],{"id":2821,"depth":89,"text":2822},{"id":2832,"depth":89,"text":2833},{"id":2845,"depth":89,"text":2846},{"id":2860,"depth":89,"text":2861},{"id":2869,"depth":89,"text":2870},{"id":1104,"depth":72,"text":1105},"Master what is a boolean in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-boolean-in-python",{"title":1168,"description":2951},"glossary\u002Fwhat-is-a-boolean-in-python","znjmwMnrd5FUWAQj6jRn2M02NdyaDKjWQ6OJFUPIA34",{"id":2958,"title":2959,"body":2960,"description":4671,"extension":1159,"meta":4672,"navigation":475,"path":4673,"seo":4674,"stem":4675,"__hash__":4676},"content\u002Fglossary\u002Fwhat-is-a-class-in-python.md","What Is a Class in Python?",{"type":7,"value":2961,"toc":4639},[2962,2965,2968,2971,2982,2992,2996,2999,3171,3173,3188,3190,3227,3235,3239,3242,3245,3251,3268,3271,3283,3286,3290,3293,3296,3307,3310,3312,3334,3337,3344,3348,3351,3366,3368,3382,3385,3503,3505,3519,3527,3531,3534,3540,3546,3564,3569,3573,3578,3619,3628,3638,3642,3647,3650,3690,3698,3701,3704,3763,3771,3774,3777,3856,3864,3871,3875,3878,3881,3899,3902,3913,3916,3919,3926,3928,3931,3947,3950,3990,3993,4018,4020,4023,4027,4030,4061,4064,4075,4079,4082,4135,4140,4143,4206,4213,4218,4221,4223,4259,4261,4299,4303,4306,4309,4324,4327,4331,4334,4399,4402,4428,4430,4525,4528,4569,4571,4575,4578,4582,4585,4589,4592,4599,4604,4606,4633,4636],[10,2963,2959],{"id":2964},"what-is-a-class-in-python",[14,2966,2967],{},"A class in Python is a blueprint for creating objects.",[14,2969,2970],{},"If that sounds abstract, think of it this way:",[157,2972,2973,2976,2979],{},[160,2974,2975],{},"A class describes what something has",[160,2977,2978],{},"A class also describes what something can do",[160,2980,2981],{},"The actual thing you create from that description is called an object",[14,2983,2984,2985,2988,2989,351],{},"Classes help you group data and behavior in one place. In Python, the data is usually stored in ",[331,2986,2987],{},"attributes",", and the actions are defined with ",[331,2990,2991],{},"methods",[21,2993,2995],{"id":2994},"simple-example-of-a-class","Simple example of a class",[14,2997,2998],{},"Here is a small working example that shows a class, an object, an attribute, and a method:",[26,3000,3002],{"className":28,"code":3001,"language":30,"meta":31,"style":31},"class Dog:\n    species = \"animal\"\n\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(self.name + \" says woof\")\n\nmy_dog = Dog(\"Max\")\nprint(my_dog.name)\nmy_dog.bark()\n",[33,3003,3004,3014,3028,3032,3055,3072,3076,3090,3116,3121,3142,3158],{"__ignoreMap":31},[36,3005,3006,3008,3012],{"class":38,"line":39},[36,3007,138],{"class":137},[36,3009,3011],{"class":3010},"sbgvK"," Dog",[36,3013,1730],{"class":50},[36,3015,3016,3019,3021,3023,3026],{"class":38,"line":72},[36,3017,3018],{"class":42},"    species ",[36,3020,47],{"class":46},[36,3022,224],{"class":141},[36,3024,3025],{"class":145},"animal",[36,3027,1954],{"class":141},[36,3029,3030],{"class":38,"line":89},[36,3031,476],{"emptyLinePlaceholder":475},[36,3033,3034,3037,3040,3042,3046,3048,3052],{"class":38,"line":496},[36,3035,3036],{"class":137},"    def",[36,3038,3039],{"class":75}," __init__",[36,3041,79],{"class":50},[36,3043,3045],{"class":3044},"smCYv","self",[36,3047,58],{"class":50},[36,3049,3051],{"class":3050},"sFwrP"," name",[36,3053,3054],{"class":50},"):\n",[36,3056,3057,3061,3063,3066,3069],{"class":38,"line":501},[36,3058,3060],{"class":3059},"s_hVV","        self",[36,3062,351],{"class":50},[36,3064,817],{"class":3065},"skxfh",[36,3067,3068],{"class":46}," =",[36,3070,3071],{"class":42}," name\n",[36,3073,3074],{"class":38,"line":522},[36,3075,476],{"emptyLinePlaceholder":475},[36,3077,3078,3080,3084,3086,3088],{"class":38,"line":527},[36,3079,3036],{"class":137},[36,3081,3083],{"class":3082},"sGLFI"," bark",[36,3085,79],{"class":50},[36,3087,3045],{"class":3044},[36,3089,3054],{"class":50},[36,3091,3093,3096,3098,3100,3102,3104,3107,3109,3112,3114],{"class":38,"line":3092},8,[36,3094,3095],{"class":75},"        print",[36,3097,79],{"class":50},[36,3099,3045],{"class":3059},[36,3101,351],{"class":50},[36,3103,817],{"class":3065},[36,3105,3106],{"class":46}," +",[36,3108,224],{"class":141},[36,3110,3111],{"class":145}," says woof",[36,3113,214],{"class":141},[36,3115,86],{"class":50},[36,3117,3119],{"class":38,"line":3118},9,[36,3120,476],{"emptyLinePlaceholder":475},[36,3122,3124,3127,3129,3131,3133,3135,3138,3140],{"class":38,"line":3123},10,[36,3125,3126],{"class":42},"my_dog ",[36,3128,47],{"class":46},[36,3130,3011],{"class":82},[36,3132,79],{"class":50},[36,3134,214],{"class":141},[36,3136,3137],{"class":145},"Max",[36,3139,214],{"class":141},[36,3141,86],{"class":50},[36,3143,3145,3147,3149,3152,3154,3156],{"class":38,"line":3144},11,[36,3146,76],{"class":75},[36,3148,79],{"class":50},[36,3150,3151],{"class":82},"my_dog",[36,3153,351],{"class":50},[36,3155,817],{"class":3065},[36,3157,86],{"class":50},[36,3159,3161,3163,3165,3168],{"class":38,"line":3160},12,[36,3162,3151],{"class":42},[36,3164,351],{"class":50},[36,3166,3167],{"class":82},"bark",[36,3169,3170],{"class":50},"()\n",[14,3172,107],{},[26,3174,3176],{"className":28,"code":3175,"language":30,"meta":31,"style":31},"Max\nMax says woof\n",[33,3177,3178,3183],{"__ignoreMap":31},[36,3179,3180],{"class":38,"line":39},[36,3181,3182],{"class":42},"Max\n",[36,3184,3185],{"class":38,"line":72},[36,3186,3187],{"class":42},"Max says woof\n",[14,3189,586],{},[157,3191,3192,3201,3209,3215,3221],{},[160,3193,3194,3197,3198],{},[33,3195,3196],{},"class Dog:"," creates a class named ",[33,3199,3200],{},"Dog",[160,3202,3203,3206,3207],{},[33,3204,3205],{},"__init__"," sets up each new dog with a ",[33,3208,817],{},[160,3210,3211,3214],{},[33,3212,3213],{},"self.name"," is an attribute",[160,3216,3217,3220],{},[33,3218,3219],{},"bark()"," is a method",[160,3222,3223,3226],{},[33,3224,3225],{},"my_dog = Dog(\"Max\")"," creates an object from the class",[14,3228,3229,3230,3234],{},"If you are new to this topic, it helps to learn ",[347,3231,3233],{"href":3232},"\u002Fglossary\u002Fwhat-is-an-object-in-python\u002F","what an object is in Python"," next, because classes and objects are closely connected.",[21,3236,3238],{"id":3237},"what-a-class-means","What a class means",[14,3240,3241],{},"A class is a template for making objects.",[14,3243,3244],{},"It lets you put related data and related actions together. This makes code easier to read and organize.",[14,3246,3247,3248,3250],{},"For example, a ",[33,3249,3200],{}," class might contain:",[157,3252,3253,3260],{},[160,3254,3255,3256,1281,3258],{},"attributes like ",[33,3257,817],{},[33,3259,835],{},[160,3261,3262,3263,1281,3265],{},"methods like ",[33,3264,3219],{},[33,3266,3267],{},"run()",[14,3269,3270],{},"In Python:",[157,3272,3273,3278],{},[160,3274,3275,3276],{},"data inside a class is usually called ",[331,3277,2987],{},[160,3279,3280,3281],{},"functions inside a class are called ",[331,3282,2991],{},[14,3284,3285],{},"So instead of storing everything in separate variables and functions, a class keeps related pieces together.",[21,3287,3289],{"id":3288},"why-classes-are-useful","Why classes are useful",[14,3291,3292],{},"Classes are useful because they help you organize code.",[14,3294,3295],{},"They are especially helpful when:",[157,3297,3298,3301,3304],{},[160,3299,3300],{},"you want to create many similar objects",[160,3302,3303],{},"the same kind of data and actions belong together",[160,3305,3306],{},"your program is getting large enough that structure matters",[14,3308,3309],{},"For beginners, it is often easiest to think of a class as a way to model a real thing.",[14,3311,694],{},[157,3313,3314,3319,3324,3329],{},[160,3315,3316,3317],{},"a ",[33,3318,3200],{},[160,3320,3316,3321],{},[33,3322,3323],{},"Student",[160,3325,3316,3326],{},[33,3327,3328],{},"BankAccount",[160,3330,3316,3331],{},[33,3332,3333],{},"Car",[14,3335,3336],{},"Each of these has data and actions. A student might have a name and grade. A bank account might have a balance and a method to deposit money.",[14,3338,3339,3340,351],{},"If you want a larger beginner lesson, see ",[347,3341,3343],{"href":3342},"\u002Flearn\u002Fpython-classes-and-objects-explained\u002F","Python classes and objects explained",[21,3345,3347],{"id":3346},"class-vs-object","Class vs object",[14,3349,3350],{},"This is one of the most important ideas to understand.",[157,3352,3353,3359],{},[160,3354,3355,3356,3358],{},"A ",[331,3357,138],{}," is the blueprint",[160,3360,3361,3362,3365],{},"An ",[331,3363,3364],{},"object"," is one real item made from that blueprint",[14,3367,197],{},[157,3369,3370,3375],{},[160,3371,3372,3374],{},[33,3373,3200],{}," is the class",[160,3376,3377,3379,3380],{},[33,3378,3137],{}," is an object created from ",[33,3381,3200],{},[14,3383,3384],{},"You can create many objects from one class.",[26,3386,3388],{"className":28,"code":3387,"language":30,"meta":31,"style":31},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\ndog1 = Dog(\"Max\")\ndog2 = Dog(\"Bella\")\n\nprint(dog1.name)\nprint(dog2.name)\n",[33,3389,3390,3398,3414,3426,3430,3449,3469,3473,3488],{"__ignoreMap":31},[36,3391,3392,3394,3396],{"class":38,"line":39},[36,3393,138],{"class":137},[36,3395,3011],{"class":3010},[36,3397,1730],{"class":50},[36,3399,3400,3402,3404,3406,3408,3410,3412],{"class":38,"line":72},[36,3401,3036],{"class":137},[36,3403,3039],{"class":75},[36,3405,79],{"class":50},[36,3407,3045],{"class":3044},[36,3409,58],{"class":50},[36,3411,3051],{"class":3050},[36,3413,3054],{"class":50},[36,3415,3416,3418,3420,3422,3424],{"class":38,"line":89},[36,3417,3060],{"class":3059},[36,3419,351],{"class":50},[36,3421,817],{"class":3065},[36,3423,3068],{"class":46},[36,3425,3071],{"class":42},[36,3427,3428],{"class":38,"line":496},[36,3429,476],{"emptyLinePlaceholder":475},[36,3431,3432,3435,3437,3439,3441,3443,3445,3447],{"class":38,"line":501},[36,3433,3434],{"class":42},"dog1 ",[36,3436,47],{"class":46},[36,3438,3011],{"class":82},[36,3440,79],{"class":50},[36,3442,214],{"class":141},[36,3444,3137],{"class":145},[36,3446,214],{"class":141},[36,3448,86],{"class":50},[36,3450,3451,3454,3456,3458,3460,3462,3465,3467],{"class":38,"line":522},[36,3452,3453],{"class":42},"dog2 ",[36,3455,47],{"class":46},[36,3457,3011],{"class":82},[36,3459,79],{"class":50},[36,3461,214],{"class":141},[36,3463,3464],{"class":145},"Bella",[36,3466,214],{"class":141},[36,3468,86],{"class":50},[36,3470,3471],{"class":38,"line":527},[36,3472,476],{"emptyLinePlaceholder":475},[36,3474,3475,3477,3479,3482,3484,3486],{"class":38,"line":3092},[36,3476,76],{"class":75},[36,3478,79],{"class":50},[36,3480,3481],{"class":82},"dog1",[36,3483,351],{"class":50},[36,3485,817],{"class":3065},[36,3487,86],{"class":50},[36,3489,3490,3492,3494,3497,3499,3501],{"class":38,"line":3118},[36,3491,76],{"class":75},[36,3493,79],{"class":50},[36,3495,3496],{"class":82},"dog2",[36,3498,351],{"class":50},[36,3500,817],{"class":3065},[36,3502,86],{"class":50},[14,3504,107],{},[26,3506,3508],{"className":28,"code":3507,"language":30,"meta":31,"style":31},"Max\nBella\n",[33,3509,3510,3514],{"__ignoreMap":31},[36,3511,3512],{"class":38,"line":39},[36,3513,3182],{"class":42},[36,3515,3516],{"class":38,"line":72},[36,3517,3518],{"class":42},"Bella\n",[14,3520,3521,3522,1281,3524,3526],{},"Both ",[33,3523,3481],{},[33,3525,3496],{}," come from the same class, but they store different data.",[21,3528,3530],{"id":3529},"basic-parts-of-a-class","Basic parts of a class",[14,3532,3533],{},"Here are the main parts beginners should know.",[1065,3535,3537,3539],{"id":3536},"class-keyword",[33,3538,138],{}," keyword",[14,3541,3542,3543,3545],{},"The ",[33,3544,138],{}," keyword starts a class definition.",[26,3547,3549],{"className":28,"code":3548,"language":30,"meta":31,"style":31},"class Dog:\n    pass\n",[33,3550,3551,3559],{"__ignoreMap":31},[36,3552,3553,3555,3557],{"class":38,"line":39},[36,3554,138],{"class":137},[36,3556,3011],{"class":3010},[36,3558,1730],{"class":50},[36,3560,3561],{"class":38,"line":72},[36,3562,3563],{"class":1718},"    pass\n",[14,3565,3566,3567,351],{},"This creates a class named ",[33,3568,3200],{},[1065,3570,3571],{"id":3205},[33,3572,3205],{},[14,3574,3542,3575,3577],{},[33,3576,3205],{}," method sets up starting values for each object.",[26,3579,3581],{"className":28,"code":3580,"language":30,"meta":31,"style":31},"class Dog:\n    def __init__(self, name):\n        self.name = name\n",[33,3582,3583,3591,3607],{"__ignoreMap":31},[36,3584,3585,3587,3589],{"class":38,"line":39},[36,3586,138],{"class":137},[36,3588,3011],{"class":3010},[36,3590,1730],{"class":50},[36,3592,3593,3595,3597,3599,3601,3603,3605],{"class":38,"line":72},[36,3594,3036],{"class":137},[36,3596,3039],{"class":75},[36,3598,79],{"class":50},[36,3600,3045],{"class":3044},[36,3602,58],{"class":50},[36,3604,3051],{"class":3050},[36,3606,3054],{"class":50},[36,3608,3609,3611,3613,3615,3617],{"class":38,"line":89},[36,3610,3060],{"class":3059},[36,3612,351],{"class":50},[36,3614,817],{"class":3065},[36,3616,3068],{"class":46},[36,3618,3071],{"class":42},[14,3620,3621,3622,3624,3625,3627],{},"When you create a new ",[33,3623,3200],{},", Python runs ",[33,3626,3205],{}," automatically.",[14,3629,3630,3631,351],{},"If this part feels confusing, see ",[347,3632,3634,3635,3637],{"href":3633},"\u002Flearn\u002Fthe-__init__-method-in-python-explained\u002F","the ",[33,3636,3205],{}," method in Python explained",[1065,3639,3640],{"id":3045},[33,3641,3045],{},[14,3643,3644,3646],{},[33,3645,3045],{}," refers to the current object.",[14,3648,3649],{},"It tells Python which object's data you are working with.",[26,3651,3652],{"className":28,"code":3580,"language":30,"meta":31,"style":31},[33,3653,3654,3662,3678],{"__ignoreMap":31},[36,3655,3656,3658,3660],{"class":38,"line":39},[36,3657,138],{"class":137},[36,3659,3011],{"class":3010},[36,3661,1730],{"class":50},[36,3663,3664,3666,3668,3670,3672,3674,3676],{"class":38,"line":72},[36,3665,3036],{"class":137},[36,3667,3039],{"class":75},[36,3669,79],{"class":50},[36,3671,3045],{"class":3044},[36,3673,58],{"class":50},[36,3675,3051],{"class":3050},[36,3677,3054],{"class":50},[36,3679,3680,3682,3684,3686,3688],{"class":38,"line":89},[36,3681,3060],{"class":3059},[36,3683,351],{"class":50},[36,3685,817],{"class":3065},[36,3687,3068],{"class":46},[36,3689,3071],{"class":42},[14,3691,243,3692,3694,3695,3697],{},[33,3693,3213],{}," means “the ",[33,3696,817],{}," of this specific dog.”",[1065,3699,3700],{"id":2987},"Attributes",[14,3702,3703],{},"Attributes store values on an object.",[26,3705,3707],{"className":28,"code":3706,"language":30,"meta":31,"style":31},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n",[33,3708,3709,3717,3738,3750],{"__ignoreMap":31},[36,3710,3711,3713,3715],{"class":38,"line":39},[36,3712,138],{"class":137},[36,3714,3011],{"class":3010},[36,3716,1730],{"class":50},[36,3718,3719,3721,3723,3725,3727,3729,3731,3733,3736],{"class":38,"line":72},[36,3720,3036],{"class":137},[36,3722,3039],{"class":75},[36,3724,79],{"class":50},[36,3726,3045],{"class":3044},[36,3728,58],{"class":50},[36,3730,3051],{"class":3050},[36,3732,58],{"class":50},[36,3734,3735],{"class":3050}," age",[36,3737,3054],{"class":50},[36,3739,3740,3742,3744,3746,3748],{"class":38,"line":89},[36,3741,3060],{"class":3059},[36,3743,351],{"class":50},[36,3745,817],{"class":3065},[36,3747,3068],{"class":46},[36,3749,3071],{"class":42},[36,3751,3752,3754,3756,3758,3760],{"class":38,"line":496},[36,3753,3060],{"class":3059},[36,3755,351],{"class":50},[36,3757,835],{"class":3065},[36,3759,3068],{"class":46},[36,3761,3762],{"class":42}," age\n",[14,3764,3765,3766,1281,3768,3770],{},"In this example, ",[33,3767,817],{},[33,3769,835],{}," are attributes.",[1065,3772,3773],{"id":2991},"Methods",[14,3775,3776],{},"Methods are functions inside a class.",[26,3778,3780],{"className":28,"code":3779,"language":30,"meta":31,"style":31},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(self.name + \" says woof\")\n",[33,3781,3782,3790,3806,3818,3822,3834],{"__ignoreMap":31},[36,3783,3784,3786,3788],{"class":38,"line":39},[36,3785,138],{"class":137},[36,3787,3011],{"class":3010},[36,3789,1730],{"class":50},[36,3791,3792,3794,3796,3798,3800,3802,3804],{"class":38,"line":72},[36,3793,3036],{"class":137},[36,3795,3039],{"class":75},[36,3797,79],{"class":50},[36,3799,3045],{"class":3044},[36,3801,58],{"class":50},[36,3803,3051],{"class":3050},[36,3805,3054],{"class":50},[36,3807,3808,3810,3812,3814,3816],{"class":38,"line":89},[36,3809,3060],{"class":3059},[36,3811,351],{"class":50},[36,3813,817],{"class":3065},[36,3815,3068],{"class":46},[36,3817,3071],{"class":42},[36,3819,3820],{"class":38,"line":496},[36,3821,476],{"emptyLinePlaceholder":475},[36,3823,3824,3826,3828,3830,3832],{"class":38,"line":501},[36,3825,3036],{"class":137},[36,3827,3083],{"class":3082},[36,3829,79],{"class":50},[36,3831,3045],{"class":3044},[36,3833,3054],{"class":50},[36,3835,3836,3838,3840,3842,3844,3846,3848,3850,3852,3854],{"class":38,"line":522},[36,3837,3095],{"class":75},[36,3839,79],{"class":50},[36,3841,3045],{"class":3059},[36,3843,351],{"class":50},[36,3845,817],{"class":3065},[36,3847,3106],{"class":46},[36,3849,224],{"class":141},[36,3851,3111],{"class":145},[36,3853,214],{"class":141},[36,3855,86],{"class":50},[14,3857,243,3858,3860,3861,3863],{},[33,3859,3219],{}," is a method because it belongs to the ",[33,3862,3200],{}," class.",[14,3865,3866,3867,351],{},"You may also want to read ",[347,3868,3870],{"href":3869},"\u002Fglossary\u002Fwhat-is-a-method-in-python\u002F","what a method is in Python",[21,3872,3874],{"id":3873},"when-beginners-should-use-a-class","When beginners should use a class",[14,3876,3877],{},"Use a class when several values and actions belong together.",[14,3879,3880],{},"Good beginner examples include:",[157,3882,3883,3887,3891,3895],{},[160,3884,3885],{},[33,3886,3200],{},[160,3888,3889],{},[33,3890,3323],{},[160,3892,3893],{},[33,3894,3328],{},[160,3896,3897],{},[33,3898,3333],{},[14,3900,3901],{},"A class is often a good choice when you find yourself thinking:",[157,3903,3904,3907,3910],{},[160,3905,3906],{},"“These values belong to one thing”",[160,3908,3909],{},"“I want many similar items”",[160,3911,3912],{},"“Each item should have its own data”",[14,3914,3915],{},"Do not force classes into every program.",[14,3917,3918],{},"For very small scripts, simple variables and functions are often enough. You do not need classes just because Python supports them.",[14,3920,3921,3922,351],{},"If you want to understand the bigger idea behind classes, see ",[347,3923,3925],{"href":3924},"\u002Flearn\u002Fobject-oriented-programming-in-python-explained\u002F","object-oriented programming in Python explained",[21,3927,2245],{"id":2244},[14,3929,3930],{},"Here are some common points that confuse beginners.",[157,3932,3933,3936,3939,3944],{},[160,3934,3935],{},"A class is not the same as a function",[160,3937,3938],{},"A class creates objects, while a function usually performs one task",[160,3940,3941,3943],{},[33,3942,3045],{}," must appear in instance methods",[160,3945,3946],{},"Defining a class does nothing by itself until you create an object",[14,3948,3949],{},"For example, this defines a class but does not create any object:",[26,3951,3953],{"className":28,"code":3952,"language":30,"meta":31,"style":31},"class Dog:\n    def bark(self):\n        print(\"Woof\")\n",[33,3954,3955,3963,3975],{"__ignoreMap":31},[36,3956,3957,3959,3961],{"class":38,"line":39},[36,3958,138],{"class":137},[36,3960,3011],{"class":3010},[36,3962,1730],{"class":50},[36,3964,3965,3967,3969,3971,3973],{"class":38,"line":72},[36,3966,3036],{"class":137},[36,3968,3083],{"class":3082},[36,3970,79],{"class":50},[36,3972,3045],{"class":3044},[36,3974,3054],{"class":50},[36,3976,3977,3979,3981,3983,3986,3988],{"class":38,"line":89},[36,3978,3095],{"class":75},[36,3980,79],{"class":50},[36,3982,214],{"class":141},[36,3984,3985],{"class":145},"Woof",[36,3987,214],{"class":141},[36,3989,86],{"class":50},[14,3991,3992],{},"To use it, you still need to create an object:",[26,3994,3996],{"className":28,"code":3995,"language":30,"meta":31,"style":31},"my_dog = Dog()\nmy_dog.bark()\n",[33,3997,3998,4008],{"__ignoreMap":31},[36,3999,4000,4002,4004,4006],{"class":38,"line":39},[36,4001,3126],{"class":42},[36,4003,47],{"class":46},[36,4005,3011],{"class":82},[36,4007,3170],{"class":50},[36,4009,4010,4012,4014,4016],{"class":38,"line":72},[36,4011,3151],{"class":42},[36,4013,351],{"class":50},[36,4015,3167],{"class":82},[36,4017,3170],{"class":50},[21,4019,905],{"id":904},[14,4021,4022],{},"Beginners often run into the same problems when learning classes.",[1065,4024,4026],{"id":4025},"mixing-up-a-class-and-an-object","Mixing up a class and an object",[14,4028,4029],{},"A class is the definition. An object is the thing you create from it.",[26,4031,4033],{"className":28,"code":4032,"language":30,"meta":31,"style":31},"class Dog:\n    pass\n\nmy_dog = Dog()\n",[33,4034,4035,4043,4047,4051],{"__ignoreMap":31},[36,4036,4037,4039,4041],{"class":38,"line":39},[36,4038,138],{"class":137},[36,4040,3011],{"class":3010},[36,4042,1730],{"class":50},[36,4044,4045],{"class":38,"line":72},[36,4046,3563],{"class":1718},[36,4048,4049],{"class":38,"line":89},[36,4050,476],{"emptyLinePlaceholder":475},[36,4052,4053,4055,4057,4059],{"class":38,"line":496},[36,4054,3126],{"class":42},[36,4056,47],{"class":46},[36,4058,3011],{"class":82},[36,4060,3170],{"class":50},[14,4062,4063],{},"Here:",[157,4065,4066,4070],{},[160,4067,4068,3374],{},[33,4069,3200],{},[160,4071,4072,4074],{},[33,4073,3151],{}," is the object",[1065,4076,4078],{"id":4077},"forgetting-to-create-an-object-before-using-attributes-or-methods","Forgetting to create an object before using attributes or methods",[14,4080,4081],{},"This will not work:",[26,4083,4085],{"className":28,"code":4084,"language":30,"meta":31,"style":31},"class Dog:\n    def bark(self):\n        print(\"Woof\")\n\nDog.bark()\n",[33,4086,4087,4095,4107,4121,4125],{"__ignoreMap":31},[36,4088,4089,4091,4093],{"class":38,"line":39},[36,4090,138],{"class":137},[36,4092,3011],{"class":3010},[36,4094,1730],{"class":50},[36,4096,4097,4099,4101,4103,4105],{"class":38,"line":72},[36,4098,3036],{"class":137},[36,4100,3083],{"class":3082},[36,4102,79],{"class":50},[36,4104,3045],{"class":3044},[36,4106,3054],{"class":50},[36,4108,4109,4111,4113,4115,4117,4119],{"class":38,"line":89},[36,4110,3095],{"class":75},[36,4112,79],{"class":50},[36,4114,214],{"class":141},[36,4116,3985],{"class":145},[36,4118,214],{"class":141},[36,4120,86],{"class":50},[36,4122,4123],{"class":38,"line":496},[36,4124,476],{"emptyLinePlaceholder":475},[36,4126,4127,4129,4131,4133],{"class":38,"line":501},[36,4128,3200],{"class":42},[36,4130,351],{"class":50},[36,4132,3167],{"class":82},[36,4134,3170],{"class":50},[14,4136,4137,4139],{},[33,4138,3219],{}," is an instance method, so it needs an object.",[14,4141,4142],{},"Correct version:",[26,4144,4146],{"className":28,"code":4145,"language":30,"meta":31,"style":31},"class Dog:\n    def bark(self):\n        print(\"Woof\")\n\nmy_dog = Dog()\nmy_dog.bark()\n",[33,4147,4148,4156,4168,4182,4186,4196],{"__ignoreMap":31},[36,4149,4150,4152,4154],{"class":38,"line":39},[36,4151,138],{"class":137},[36,4153,3011],{"class":3010},[36,4155,1730],{"class":50},[36,4157,4158,4160,4162,4164,4166],{"class":38,"line":72},[36,4159,3036],{"class":137},[36,4161,3083],{"class":3082},[36,4163,79],{"class":50},[36,4165,3045],{"class":3044},[36,4167,3054],{"class":50},[36,4169,4170,4172,4174,4176,4178,4180],{"class":38,"line":89},[36,4171,3095],{"class":75},[36,4173,79],{"class":50},[36,4175,214],{"class":141},[36,4177,3985],{"class":145},[36,4179,214],{"class":141},[36,4181,86],{"class":50},[36,4183,4184],{"class":38,"line":496},[36,4185,476],{"emptyLinePlaceholder":475},[36,4187,4188,4190,4192,4194],{"class":38,"line":501},[36,4189,3126],{"class":42},[36,4191,47],{"class":46},[36,4193,3011],{"class":82},[36,4195,3170],{"class":50},[36,4197,4198,4200,4202,4204],{"class":38,"line":522},[36,4199,3151],{"class":42},[36,4201,351],{"class":50},[36,4203,3167],{"class":82},[36,4205,3170],{"class":50},[1065,4207,4209,4210,4212],{"id":4208},"not-understanding-what-self-refers-to","Not understanding what ",[33,4211,3045],{}," refers to",[14,4214,4215,4217],{},[33,4216,3045],{}," means the current object.",[14,4219,4220],{},"If you leave it out, your method definition will be wrong.",[14,4222,2560],{},[26,4224,4226],{"className":28,"code":4225,"language":30,"meta":31,"style":31},"class Dog:\n    def bark():\n        print(\"Woof\")\n",[33,4227,4228,4236,4245],{"__ignoreMap":31},[36,4229,4230,4232,4234],{"class":38,"line":39},[36,4231,138],{"class":137},[36,4233,3011],{"class":3010},[36,4235,1730],{"class":50},[36,4237,4238,4240,4242],{"class":38,"line":72},[36,4239,3036],{"class":137},[36,4241,3083],{"class":3082},[36,4243,4244],{"class":50},"():\n",[36,4246,4247,4249,4251,4253,4255,4257],{"class":38,"line":89},[36,4248,3095],{"class":75},[36,4250,79],{"class":50},[36,4252,214],{"class":141},[36,4254,3985],{"class":145},[36,4256,214],{"class":141},[36,4258,86],{"class":50},[14,4260,2512],{},[26,4262,4263],{"className":28,"code":3952,"language":30,"meta":31,"style":31},[33,4264,4265,4273,4285],{"__ignoreMap":31},[36,4266,4267,4269,4271],{"class":38,"line":39},[36,4268,138],{"class":137},[36,4270,3011],{"class":3010},[36,4272,1730],{"class":50},[36,4274,4275,4277,4279,4281,4283],{"class":38,"line":72},[36,4276,3036],{"class":137},[36,4278,3083],{"class":3082},[36,4280,79],{"class":50},[36,4282,3045],{"class":3044},[36,4284,3054],{"class":50},[36,4286,4287,4289,4291,4293,4295,4297],{"class":38,"line":89},[36,4288,3095],{"class":75},[36,4290,79],{"class":50},[36,4292,214],{"class":141},[36,4294,3985],{"class":145},[36,4296,214],{"class":141},[36,4298,86],{"class":50},[1065,4300,4302],{"id":4301},"thinking-classes-are-required-for-every-python-program","Thinking classes are required for every Python program",[14,4304,4305],{},"They are not.",[14,4307,4308],{},"Many beginner programs work perfectly well with:",[157,4310,4311,4314,4318,4321],{},[160,4312,4313],{},"variables",[160,4315,4316,1668],{},[33,4317,1192],{},[160,4319,4320],{},"loops",[160,4322,4323],{},"functions",[14,4325,4326],{},"Classes become useful when your code needs more structure.",[21,4328,4330],{"id":4329},"helpful-ways-to-inspect-a-class-or-object","Helpful ways to inspect a class or object",[14,4332,4333],{},"If you are exploring how classes work, these commands can help:",[26,4335,4337],{"className":28,"code":4336,"language":30,"meta":31,"style":31},"print(type(my_dog))\nprint(my_dog.__dict__)\nprint(Dog.__dict__.keys())\nhelp(Dog)\n",[33,4338,4339,4353,4368,4388],{"__ignoreMap":31},[36,4340,4341,4343,4345,4347,4349,4351],{"class":38,"line":39},[36,4342,76],{"class":75},[36,4344,79],{"class":50},[36,4346,97],{"class":96},[36,4348,79],{"class":50},[36,4350,3151],{"class":82},[36,4352,104],{"class":50},[36,4354,4355,4357,4359,4361,4363,4366],{"class":38,"line":72},[36,4356,76],{"class":75},[36,4358,79],{"class":50},[36,4360,3151],{"class":82},[36,4362,351],{"class":50},[36,4364,4365],{"class":3059},"__dict__",[36,4367,86],{"class":50},[36,4369,4370,4372,4374,4376,4378,4380,4382,4385],{"class":38,"line":89},[36,4371,76],{"class":75},[36,4373,79],{"class":50},[36,4375,3200],{"class":82},[36,4377,351],{"class":50},[36,4379,4365],{"class":3059},[36,4381,351],{"class":50},[36,4383,4384],{"class":82},"keys",[36,4386,4387],{"class":50},"())\n",[36,4389,4390,4393,4395,4397],{"class":38,"line":496},[36,4391,4392],{"class":75},"help",[36,4394,79],{"class":50},[36,4396,3200],{"class":82},[36,4398,86],{"class":50},[14,4400,4401],{},"What they show:",[157,4403,4404,4410,4416,4422],{},[160,4405,4406,4409],{},[33,4407,4408],{},"type(my_dog)"," shows the type of the object",[160,4411,4412,4415],{},[33,4413,4414],{},"my_dog.__dict__"," shows the object's attributes",[160,4417,4418,4421],{},[33,4419,4420],{},"Dog.__dict__.keys()"," shows names defined on the class",[160,4423,4424,4427],{},[33,4425,4426],{},"help(Dog)"," shows built-in help information",[14,4429,197],{},[26,4431,4433],{"className":28,"code":4432,"language":30,"meta":31,"style":31},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\nmy_dog = Dog(\"Max\")\n\nprint(type(my_dog))\nprint(my_dog.__dict__)\n",[33,4434,4435,4443,4459,4471,4475,4493,4497,4511],{"__ignoreMap":31},[36,4436,4437,4439,4441],{"class":38,"line":39},[36,4438,138],{"class":137},[36,4440,3011],{"class":3010},[36,4442,1730],{"class":50},[36,4444,4445,4447,4449,4451,4453,4455,4457],{"class":38,"line":72},[36,4446,3036],{"class":137},[36,4448,3039],{"class":75},[36,4450,79],{"class":50},[36,4452,3045],{"class":3044},[36,4454,58],{"class":50},[36,4456,3051],{"class":3050},[36,4458,3054],{"class":50},[36,4460,4461,4463,4465,4467,4469],{"class":38,"line":89},[36,4462,3060],{"class":3059},[36,4464,351],{"class":50},[36,4466,817],{"class":3065},[36,4468,3068],{"class":46},[36,4470,3071],{"class":42},[36,4472,4473],{"class":38,"line":496},[36,4474,476],{"emptyLinePlaceholder":475},[36,4476,4477,4479,4481,4483,4485,4487,4489,4491],{"class":38,"line":501},[36,4478,3126],{"class":42},[36,4480,47],{"class":46},[36,4482,3011],{"class":82},[36,4484,79],{"class":50},[36,4486,214],{"class":141},[36,4488,3137],{"class":145},[36,4490,214],{"class":141},[36,4492,86],{"class":50},[36,4494,4495],{"class":38,"line":522},[36,4496,476],{"emptyLinePlaceholder":475},[36,4498,4499,4501,4503,4505,4507,4509],{"class":38,"line":527},[36,4500,76],{"class":75},[36,4502,79],{"class":50},[36,4504,97],{"class":96},[36,4506,79],{"class":50},[36,4508,3151],{"class":82},[36,4510,104],{"class":50},[36,4512,4513,4515,4517,4519,4521,4523],{"class":38,"line":3092},[36,4514,76],{"class":75},[36,4516,79],{"class":50},[36,4518,3151],{"class":82},[36,4520,351],{"class":50},[36,4522,4365],{"class":3059},[36,4524,86],{"class":50},[14,4526,4527],{},"Possible output:",[26,4529,4531],{"className":28,"code":4530,"language":30,"meta":31,"style":31},"\u003Cclass '__main__.Dog'>\n{'name': 'Max'}\n",[33,4532,4533,4548],{"__ignoreMap":31},[36,4534,4535,4537,4539,4541,4544,4546],{"class":38,"line":39},[36,4536,134],{"class":46},[36,4538,138],{"class":137},[36,4540,142],{"class":141},[36,4542,4543],{"class":145},"__main__.Dog",[36,4545,149],{"class":141},[36,4547,152],{"class":46},[36,4549,4550,4553,4555,4557,4559,4561,4563,4565,4567],{"class":38,"line":72},[36,4551,4552],{"class":50},"{",[36,4554,149],{"class":141},[36,4556,817],{"class":145},[36,4558,149],{"class":141},[36,4560,822],{"class":50},[36,4562,142],{"class":141},[36,4564,3137],{"class":145},[36,4566,149],{"class":141},[36,4568,803],{"class":50},[21,4570,1063],{"id":1062},[1065,4572,4574],{"id":4573},"what-is-a-class-in-python-in-simple-words","What is a class in Python in simple words?",[14,4576,4577],{},"A class is a blueprint for creating objects that store data and perform actions.",[1065,4579,4581],{"id":4580},"what-is-the-difference-between-a-class-and-an-object","What is the difference between a class and an object?",[14,4583,4584],{},"A class is the definition. An object is one instance created from that definition.",[1065,4586,4588],{"id":4587},"do-beginners-need-classes-right-away","Do beginners need classes right away?",[14,4590,4591],{},"No. Beginners can learn variables, functions, and loops first. Classes become useful when code needs better structure.",[1065,4593,4595,4596,4598],{"id":4594},"what-is-self-in-a-python-class","What is ",[33,4597,3045],{}," in a Python class?",[14,4600,4601,4603],{},[33,4602,3045],{}," refers to the current object, so Python knows which object's data to use.",[21,4605,1105],{"id":1104},[157,4607,4608,4612,4617,4623,4628],{},[160,4609,4610],{},[347,4611,3343],{"href":3342},[160,4613,4614],{},[347,4615,4616],{"href":3924},"Object-oriented programming in Python explained",[160,4618,4619],{},[347,4620,3542,4621,3637],{"href":3633},[33,4622,3205],{},[160,4624,4625],{},[347,4626,4627],{"href":3232},"What is an object in Python?",[160,4629,4630],{},[347,4631,4632],{"href":3869},"What is a method in Python?",[14,4634,4635],{},"If you understand the basic definition of a class, the next step is to learn how to create your own classes and objects step by step.",[1137,4637,4638],{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smCYv, html code.shiki .smCYv{--shiki-light:#E53935;--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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":4640},[4641,4642,4643,4644,4645,4653,4654,4655,4662,4663,4670],{"id":2994,"depth":72,"text":2995},{"id":3237,"depth":72,"text":3238},{"id":3288,"depth":72,"text":3289},{"id":3346,"depth":72,"text":3347},{"id":3529,"depth":72,"text":3530,"children":4646},[4647,4649,4650,4651,4652],{"id":3536,"depth":89,"text":4648},"class keyword",{"id":3205,"depth":89,"text":3205},{"id":3045,"depth":89,"text":3045},{"id":2987,"depth":89,"text":3700},{"id":2991,"depth":89,"text":3773},{"id":3873,"depth":72,"text":3874},{"id":2244,"depth":72,"text":2245},{"id":904,"depth":72,"text":905,"children":4656},[4657,4658,4659,4661],{"id":4025,"depth":89,"text":4026},{"id":4077,"depth":89,"text":4078},{"id":4208,"depth":89,"text":4660},"Not understanding what self refers to",{"id":4301,"depth":89,"text":4302},{"id":4329,"depth":72,"text":4330},{"id":1062,"depth":72,"text":1063,"children":4664},[4665,4666,4667,4668],{"id":4573,"depth":89,"text":4574},{"id":4580,"depth":89,"text":4581},{"id":4587,"depth":89,"text":4588},{"id":4594,"depth":89,"text":4669},"What is self in a Python class?",{"id":1104,"depth":72,"text":1105},"Master what is a class in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-class-in-python",{"title":2959,"description":4671},"glossary\u002Fwhat-is-a-class-in-python","htJlPP_G1d5r2P9JNmoAGRRaTitwxX4s2PTwDqMdelg",{"id":4678,"title":4679,"body":4680,"description":5923,"extension":1159,"meta":5924,"navigation":475,"path":5925,"seo":5926,"stem":5927,"__hash__":5928},"content\u002Fglossary\u002Fwhat-is-a-data-type-in-python.md","What Is a Data Type in Python?",{"type":7,"value":4681,"toc":5904},[4682,4685,4688,4691,4708,4711,4715,4718,4721,4751,4754,4756,4772,4814,4816,4831,4837,4841,4844,4898,4900,4992,4995,5000,5004,5007,5009,5023,5026,5046,5049,5072,5082,5089,5093,5096,5099,5127,5132,5197,5199,5260,5267,5271,5274,5277,5280,5339,5341,5374,5377,5384,5386,5389,5413,5415,5528,5530,5620,5624,5627,5630,5662,5665,5667,5670,5684,5687,5761,5763,5790,5793,5815,5817,5821,5829,5833,5836,5845,5852,5856,5868,5872,5875,5877,5901],[10,4683,4679],{"id":4684},"what-is-a-data-type-in-python",[14,4686,4687],{},"A data type in Python tells Python what kind of value you are working with.",[14,4689,4690],{},"For example, a value might be:",[157,4692,4693,4696,4699,4702],{},[160,4694,4695],{},"text",[160,4697,4698],{},"a whole number",[160,4700,4701],{},"a decimal number",[160,4703,4704,1180,4706],{},[33,4705,1179],{},[33,4707,1183],{},[14,4709,4710],{},"Data types matter because they affect what you can do with a value. Python uses them to decide how values are stored and how operations should behave.",[21,4712,4714],{"id":4713},"what-a-data-type-means","What a data type means",[14,4716,4717],{},"A data type describes the kind of value you are using.",[14,4719,4720],{},"Here are a few simple examples:",[157,4722,4723,4729,4735,4741],{},[160,4724,4725,4726,171],{},"Text uses the ",[331,4727,4728],{},"string",[160,4730,4731,4732,171],{},"Whole numbers use the ",[331,4733,4734],{},"integer",[160,4736,4737,4738,171],{},"Decimal numbers use the ",[331,4739,4740],{},"float",[160,4742,4743,1281,4745,4747,4748,171],{},[33,4744,1179],{},[33,4746,1183],{}," use the ",[331,4749,4750],{},"boolean",[14,4752,4753],{},"Different data types support different operations.",[14,4755,1235],{},[157,4757,4758,4764,4769],{},[160,4759,4760,4761],{},"You can add numbers with ",[33,4762,4763],{},"+",[160,4765,4766,4767],{},"You can join strings with ",[33,4768,4763],{},[160,4770,4771],{},"Some operations work for one type but not another",[26,4773,4775],{"className":28,"code":4774,"language":30,"meta":31,"style":31},"print(2 + 3)\nprint(\"hello\" + \" world\")\n",[33,4776,4777,4791],{"__ignoreMap":31},[36,4778,4779,4781,4783,4785,4787,4789],{"class":38,"line":39},[36,4780,76],{"class":75},[36,4782,79],{"class":50},[36,4784,1543],{"class":54},[36,4786,3106],{"class":46},[36,4788,66],{"class":54},[36,4790,86],{"class":50},[36,4792,4793,4795,4797,4799,4801,4803,4805,4807,4810,4812],{"class":38,"line":72},[36,4794,76],{"class":75},[36,4796,79],{"class":50},[36,4798,214],{"class":141},[36,4800,2671],{"class":145},[36,4802,214],{"class":141},[36,4804,3106],{"class":46},[36,4806,224],{"class":141},[36,4808,4809],{"class":145}," world",[36,4811,214],{"class":141},[36,4813,86],{"class":50},[14,4815,107],{},[26,4817,4819],{"className":28,"code":4818,"language":30,"meta":31,"style":31},"5\nhello world\n",[33,4820,4821,4826],{"__ignoreMap":31},[36,4822,4823],{"class":38,"line":39},[36,4824,4825],{"class":54},"5\n",[36,4827,4828],{"class":38,"line":72},[36,4829,4830],{"class":42},"hello world\n",[14,4832,4833,4834,4836],{},"Even though both lines use ",[33,4835,4763],{},", Python handles them differently because the data types are different.",[21,4838,4840],{"id":4839},"simple-examples-of-data-types","Simple examples of data types",[14,4842,4843],{},"Some common built-in Python data types are:",[157,4845,4846,4855,4863,4872,4882,4890],{},[160,4847,4848,4851,4852],{},[331,4849,4850],{},"String",": text like ",[33,4853,4854],{},"\"hello\"",[160,4856,4857,4860,4861],{},[331,4858,4859],{},"Integer",": whole number like ",[33,4862,1243],{},[160,4864,4865,4868,4869],{},[331,4866,4867],{},"Float",": decimal number like ",[33,4870,4871],{},"3.14",[160,4873,4874,4877,4878,1180,4880],{},[331,4875,4876],{},"Boolean",": ",[33,4879,1179],{},[33,4881,1183],{},[160,4883,4884,4886,4887],{},[331,4885,672],{},": a collection like ",[33,4888,4889],{},"[1, 2, 3]",[160,4891,4892,4894,4895],{},[331,4893,690],{},": key-value data like ",[33,4896,4897],{},"{\"name\": \"Sam\"}",[14,4899,197],{},[26,4901,4903],{"className":28,"code":4902,"language":30,"meta":31,"style":31},"message = \"hello\"\ncount = 5\nprice = 3.14\nis_ready = True\nnumbers = [1, 2, 3]\nuser = {\"name\": \"Sam\"}\n",[33,4904,4905,4918,4928,4938,4947,4967],{"__ignoreMap":31},[36,4906,4907,4910,4912,4914,4916],{"class":38,"line":39},[36,4908,4909],{"class":42},"message ",[36,4911,47],{"class":46},[36,4913,224],{"class":141},[36,4915,2671],{"class":145},[36,4917,1954],{"class":141},[36,4919,4920,4923,4925],{"class":38,"line":72},[36,4921,4922],{"class":42},"count ",[36,4924,47],{"class":46},[36,4926,4927],{"class":54}," 5\n",[36,4929,4930,4933,4935],{"class":38,"line":89},[36,4931,4932],{"class":42},"price ",[36,4934,47],{"class":46},[36,4936,4937],{"class":54}," 3.14\n",[36,4939,4940,4943,4945],{"class":38,"line":496},[36,4941,4942],{"class":42},"is_ready ",[36,4944,47],{"class":46},[36,4946,1419],{"class":400},[36,4948,4949,4951,4953,4955,4957,4959,4961,4963,4965],{"class":38,"line":501},[36,4950,43],{"class":42},[36,4952,47],{"class":46},[36,4954,51],{"class":50},[36,4956,55],{"class":54},[36,4958,58],{"class":50},[36,4960,61],{"class":54},[36,4962,58],{"class":50},[36,4964,66],{"class":54},[36,4966,69],{"class":50},[36,4968,4969,4972,4974,4976,4978,4980,4982,4984,4986,4988,4990],{"class":38,"line":522},[36,4970,4971],{"class":42},"user ",[36,4973,47],{"class":46},[36,4975,778],{"class":50},[36,4977,214],{"class":141},[36,4979,817],{"class":145},[36,4981,214],{"class":141},[36,4983,822],{"class":50},[36,4985,224],{"class":141},[36,4987,311],{"class":145},[36,4989,214],{"class":141},[36,4991,803],{"class":50},[14,4993,4994],{},"Each value has its own type.",[14,4996,4997,4998,351],{},"If you want a broader introduction, see ",[347,4999,2895],{"href":2894},[21,5001,5003],{"id":5002},"why-data-types-matter","Why data types matter",[14,5005,5006],{},"Data types matter because they affect what your code can do.",[14,5008,1235],{},[157,5010,5011,5014,5017,5020],{},[160,5012,5013],{},"Numbers can be added",[160,5015,5016],{},"Strings behave like text",[160,5018,5019],{},"Some combinations of types cause errors",[160,5021,5022],{},"Choosing the right type makes your code easier to work with",[14,5024,5025],{},"This works:",[26,5027,5029],{"className":28,"code":5028,"language":30,"meta":31,"style":31},"print(10 + 5)\n",[33,5030,5031],{"__ignoreMap":31},[36,5032,5033,5035,5037,5039,5041,5044],{"class":38,"line":39},[36,5034,76],{"class":75},[36,5036,79],{"class":50},[36,5038,1560],{"class":54},[36,5040,3106],{"class":46},[36,5042,5043],{"class":54}," 5",[36,5045,86],{"class":50},[14,5047,5048],{},"But this causes a problem:",[26,5050,5052],{"className":28,"code":5051,"language":30,"meta":31,"style":31},"print(\"10\" + 5)\n",[33,5053,5054],{"__ignoreMap":31},[36,5055,5056,5058,5060,5062,5064,5066,5068,5070],{"class":38,"line":39},[36,5057,76],{"class":75},[36,5059,79],{"class":50},[36,5061,214],{"class":141},[36,5063,1560],{"class":145},[36,5065,214],{"class":141},[36,5067,3106],{"class":46},[36,5069,5043],{"class":54},[36,5071,86],{"class":50},[14,5073,5074,5075,5078,5079,5081],{},"Python raises an error because ",[33,5076,5077],{},"\"10\""," is a string and ",[33,5080,1243],{}," is an integer. These types do not work together in that operation.",[14,5083,5084,5085,351],{},"If you want to understand this more clearly, see ",[347,5086,5088],{"href":5087},"\u002Ferrors\u002Ftypeerror-vs-valueerror-in-python-explained\u002F","TypeError vs ValueError in Python explained",[21,5090,5092],{"id":5091},"how-python-knows-a-values-type","How Python knows a value's type",[14,5094,5095],{},"Python usually figures out the type from the value itself.",[14,5097,5098],{},"Common examples:",[157,5100,5101,5106,5113,5120],{},[160,5102,5103,5104],{},"Quotes usually mean a string: ",[33,5105,4854],{},[160,5107,5108,5109,4877,5111],{},"A whole number is usually an ",[33,5110,2466],{},[33,5112,1243],{},[160,5114,5115,5116,4877,5118],{},"A decimal number is usually a ",[33,5117,4740],{},[33,5119,4871],{},[160,5121,5122,1281,5124,5126],{},[33,5123,1179],{},[33,5125,1183],{}," are booleans",[14,5128,5129,5130,351],{},"You can check a value's type with ",[33,5131,1327],{},[26,5133,5135],{"className":28,"code":5134,"language":30,"meta":31,"style":31},"print(type(\"hello\"))\nprint(type(5))\nprint(type(3.14))\nprint(type(True))\n",[33,5136,5137,5155,5169,5183],{"__ignoreMap":31},[36,5138,5139,5141,5143,5145,5147,5149,5151,5153],{"class":38,"line":39},[36,5140,76],{"class":75},[36,5142,79],{"class":50},[36,5144,97],{"class":96},[36,5146,79],{"class":50},[36,5148,214],{"class":141},[36,5150,2671],{"class":145},[36,5152,214],{"class":141},[36,5154,104],{"class":50},[36,5156,5157,5159,5161,5163,5165,5167],{"class":38,"line":72},[36,5158,76],{"class":75},[36,5160,79],{"class":50},[36,5162,97],{"class":96},[36,5164,79],{"class":50},[36,5166,1243],{"class":54},[36,5168,104],{"class":50},[36,5170,5171,5173,5175,5177,5179,5181],{"class":38,"line":89},[36,5172,76],{"class":75},[36,5174,79],{"class":50},[36,5176,97],{"class":96},[36,5178,79],{"class":50},[36,5180,4871],{"class":54},[36,5182,104],{"class":50},[36,5184,5185,5187,5189,5191,5193,5195],{"class":38,"line":496},[36,5186,76],{"class":75},[36,5188,79],{"class":50},[36,5190,97],{"class":96},[36,5192,79],{"class":50},[36,5194,1179],{"class":400},[36,5196,104],{"class":50},[14,5198,107],{},[26,5200,5202],{"className":28,"code":5201,"language":30,"meta":31,"style":31},"\u003Cclass 'str'>\n\u003Cclass 'int'>\n\u003Cclass 'float'>\n\u003Cclass 'bool'>\n",[33,5203,5204,5218,5232,5246],{"__ignoreMap":31},[36,5205,5206,5208,5210,5212,5214,5216],{"class":38,"line":39},[36,5207,134],{"class":46},[36,5209,138],{"class":137},[36,5211,142],{"class":141},[36,5213,2360],{"class":145},[36,5215,149],{"class":141},[36,5217,152],{"class":46},[36,5219,5220,5222,5224,5226,5228,5230],{"class":38,"line":72},[36,5221,134],{"class":46},[36,5223,138],{"class":137},[36,5225,142],{"class":141},[36,5227,2466],{"class":145},[36,5229,149],{"class":141},[36,5231,152],{"class":46},[36,5233,5234,5236,5238,5240,5242,5244],{"class":38,"line":89},[36,5235,134],{"class":46},[36,5237,138],{"class":137},[36,5239,142],{"class":141},[36,5241,4740],{"class":145},[36,5243,149],{"class":141},[36,5245,152],{"class":46},[36,5247,5248,5250,5252,5254,5256,5258],{"class":38,"line":496},[36,5249,134],{"class":46},[36,5251,138],{"class":137},[36,5253,142],{"class":141},[36,5255,1378],{"class":145},[36,5257,149],{"class":141},[36,5259,152],{"class":46},[14,5261,5262,5263,351],{},"If you want to learn more about this function, see ",[347,5264,5266],{"href":5265},"\u002Freference\u002Fpython-type-function-explained\u002F","Python type() function explained",[21,5268,5270],{"id":5269},"variables-and-data-types","Variables and data types",[14,5272,5273],{},"A variable stores a value.",[14,5275,5276],{},"The value inside the variable has a data type.",[14,5278,5279],{},"In Python, you do not need to declare the type first. Python works it out when you assign the value.",[26,5281,5283],{"className":28,"code":5282,"language":30,"meta":31,"style":31},"x = 5\nprint(type(x))\n\nx = \"hello\"\nprint(type(x))\n",[33,5284,5285,5294,5309,5313,5325],{"__ignoreMap":31},[36,5286,5287,5290,5292],{"class":38,"line":39},[36,5288,5289],{"class":42},"x ",[36,5291,47],{"class":46},[36,5293,4927],{"class":54},[36,5295,5296,5298,5300,5302,5304,5307],{"class":38,"line":72},[36,5297,76],{"class":75},[36,5299,79],{"class":50},[36,5301,97],{"class":96},[36,5303,79],{"class":50},[36,5305,5306],{"class":82},"x",[36,5308,104],{"class":50},[36,5310,5311],{"class":38,"line":89},[36,5312,476],{"emptyLinePlaceholder":475},[36,5314,5315,5317,5319,5321,5323],{"class":38,"line":496},[36,5316,5289],{"class":42},[36,5318,47],{"class":46},[36,5320,224],{"class":141},[36,5322,2671],{"class":145},[36,5324,1954],{"class":141},[36,5326,5327,5329,5331,5333,5335,5337],{"class":38,"line":501},[36,5328,76],{"class":75},[36,5330,79],{"class":50},[36,5332,97],{"class":96},[36,5334,79],{"class":50},[36,5336,5306],{"class":82},[36,5338,104],{"class":50},[14,5340,107],{},[26,5342,5344],{"className":28,"code":5343,"language":30,"meta":31,"style":31},"\u003Cclass 'int'>\n\u003Cclass 'str'>\n",[33,5345,5346,5360],{"__ignoreMap":31},[36,5347,5348,5350,5352,5354,5356,5358],{"class":38,"line":39},[36,5349,134],{"class":46},[36,5351,138],{"class":137},[36,5353,142],{"class":141},[36,5355,2466],{"class":145},[36,5357,149],{"class":141},[36,5359,152],{"class":46},[36,5361,5362,5364,5366,5368,5370,5372],{"class":38,"line":72},[36,5363,134],{"class":46},[36,5365,138],{"class":137},[36,5367,142],{"class":141},[36,5369,2360],{"class":145},[36,5371,149],{"class":141},[36,5373,152],{"class":46},[14,5375,5376],{},"This means the same variable can later store a different type of value.",[14,5378,5379,5380,351],{},"If you are still learning variables, see ",[347,5381,5383],{"href":5382},"\u002Fglossary\u002Fwhat-is-a-variable-in-python\u002F","what is a variable in Python",[21,5385,2245],{"id":2244},[14,5387,5388],{},"These are very common mistakes when learning data types:",[157,5390,5391,5397,5405,5410],{},[160,5392,5393,5396],{},[33,5394,5395],{},"\"5\""," is a string, not an integer",[160,5398,5399,1281,5401,5404],{},[33,5400,1243],{},[33,5402,5403],{},"5.0"," are different types",[160,5406,5407,5409],{},[33,5408,1179],{}," is a boolean, not a string",[160,5411,5412],{},"A list and a tuple are different types, even if they look similar",[14,5414,197],{},[26,5416,5418],{"className":28,"code":5417,"language":30,"meta":31,"style":31},"print(type(\"5\"))\nprint(type(5))\nprint(type(5.0))\nprint(type(True))\nprint(type([1, 2, 3]))\nprint(type((1, 2, 3)))\n",[33,5419,5420,5438,5452,5466,5480,5504],{"__ignoreMap":31},[36,5421,5422,5424,5426,5428,5430,5432,5434,5436],{"class":38,"line":39},[36,5423,76],{"class":75},[36,5425,79],{"class":50},[36,5427,97],{"class":96},[36,5429,79],{"class":50},[36,5431,214],{"class":141},[36,5433,1243],{"class":145},[36,5435,214],{"class":141},[36,5437,104],{"class":50},[36,5439,5440,5442,5444,5446,5448,5450],{"class":38,"line":72},[36,5441,76],{"class":75},[36,5443,79],{"class":50},[36,5445,97],{"class":96},[36,5447,79],{"class":50},[36,5449,1243],{"class":54},[36,5451,104],{"class":50},[36,5453,5454,5456,5458,5460,5462,5464],{"class":38,"line":89},[36,5455,76],{"class":75},[36,5457,79],{"class":50},[36,5459,97],{"class":96},[36,5461,79],{"class":50},[36,5463,5403],{"class":54},[36,5465,104],{"class":50},[36,5467,5468,5470,5472,5474,5476,5478],{"class":38,"line":496},[36,5469,76],{"class":75},[36,5471,79],{"class":50},[36,5473,97],{"class":96},[36,5475,79],{"class":50},[36,5477,1179],{"class":400},[36,5479,104],{"class":50},[36,5481,5482,5484,5486,5488,5491,5493,5495,5497,5499,5501],{"class":38,"line":501},[36,5483,76],{"class":75},[36,5485,79],{"class":50},[36,5487,97],{"class":96},[36,5489,5490],{"class":50},"([",[36,5492,55],{"class":54},[36,5494,58],{"class":50},[36,5496,61],{"class":54},[36,5498,58],{"class":50},[36,5500,66],{"class":54},[36,5502,5503],{"class":50},"]))\n",[36,5505,5506,5508,5510,5512,5515,5517,5519,5521,5523,5525],{"class":38,"line":522},[36,5507,76],{"class":75},[36,5509,79],{"class":50},[36,5511,97],{"class":96},[36,5513,5514],{"class":50},"((",[36,5516,55],{"class":54},[36,5518,58],{"class":50},[36,5520,61],{"class":54},[36,5522,58],{"class":50},[36,5524,66],{"class":54},[36,5526,5527],{"class":50},")))\n",[14,5529,107],{},[26,5531,5533],{"className":28,"code":5532,"language":30,"meta":31,"style":31},"\u003Cclass 'str'>\n\u003Cclass 'int'>\n\u003Cclass 'float'>\n\u003Cclass 'bool'>\n\u003Cclass 'list'>\n\u003Cclass 'tuple'>\n",[33,5534,5535,5549,5563,5577,5591,5605],{"__ignoreMap":31},[36,5536,5537,5539,5541,5543,5545,5547],{"class":38,"line":39},[36,5538,134],{"class":46},[36,5540,138],{"class":137},[36,5542,142],{"class":141},[36,5544,2360],{"class":145},[36,5546,149],{"class":141},[36,5548,152],{"class":46},[36,5550,5551,5553,5555,5557,5559,5561],{"class":38,"line":72},[36,5552,134],{"class":46},[36,5554,138],{"class":137},[36,5556,142],{"class":141},[36,5558,2466],{"class":145},[36,5560,149],{"class":141},[36,5562,152],{"class":46},[36,5564,5565,5567,5569,5571,5573,5575],{"class":38,"line":89},[36,5566,134],{"class":46},[36,5568,138],{"class":137},[36,5570,142],{"class":141},[36,5572,4740],{"class":145},[36,5574,149],{"class":141},[36,5576,152],{"class":46},[36,5578,5579,5581,5583,5585,5587,5589],{"class":38,"line":496},[36,5580,134],{"class":46},[36,5582,138],{"class":137},[36,5584,142],{"class":141},[36,5586,1378],{"class":145},[36,5588,149],{"class":141},[36,5590,152],{"class":46},[36,5592,5593,5595,5597,5599,5601,5603],{"class":38,"line":501},[36,5594,134],{"class":46},[36,5596,138],{"class":137},[36,5598,142],{"class":141},[36,5600,146],{"class":145},[36,5602,149],{"class":141},[36,5604,152],{"class":46},[36,5606,5607,5609,5611,5613,5616,5618],{"class":38,"line":522},[36,5608,134],{"class":46},[36,5610,138],{"class":137},[36,5612,142],{"class":141},[36,5614,5615],{"class":145},"tuple",[36,5617,149],{"class":141},[36,5619,152],{"class":46},[21,5621,5623],{"id":5622},"when-to-learn-more-next","When to learn more next",[14,5625,5626],{},"Once you understand the basic idea of data types, the next useful step is to learn specific types in more detail.",[14,5628,5629],{},"Good next topics are:",[157,5631,5632,5638,5644,5650,5656],{},[160,5633,5634],{},[347,5635,5637],{"href":5636},"\u002Fglossary\u002Fwhat-is-a-string-in-python\u002F","what is a string in Python",[160,5639,5640],{},[347,5641,5643],{"href":5642},"\u002Fglossary\u002Fwhat-is-an-integer-in-python\u002F","what is an integer in Python",[160,5645,5646],{},[347,5647,5649],{"href":5648},"\u002Fglossary\u002Fwhat-is-a-float-in-python\u002F","what is a float in Python",[160,5651,5652],{},[347,5653,5655],{"href":5654},"\u002Fglossary\u002Fwhat-is-a-boolean-in-python\u002F","what is a boolean in Python",[160,5657,5658],{},[347,5659,5661],{"href":5660},"\u002Flearn\u002Ftype-conversion-in-python-casting-between-types\u002F","type conversion in Python",[14,5663,5664],{},"Type conversion is especially useful because it lets you change one type into another when needed.",[21,5666,905],{"id":904},[14,5668,5669],{},"Beginners often run into problems like these:",[157,5671,5672,5675,5678,5681],{},[160,5673,5674],{},"Treating a string like a number",[160,5676,5677],{},"Assuming all numbers are the same type",[160,5679,5680],{},"Confusing a variable name with the value's type",[160,5682,5683],{},"Mixing incompatible data types in one operation",[14,5685,5686],{},"These commands can help you check what is happening:",[26,5688,5690],{"className":28,"code":5689,"language":30,"meta":31,"style":31},"value = \"5\"\n\nprint(type(value))\nprint(isinstance(value, int))\nprint(isinstance(value, str))\n",[33,5691,5692,5704,5708,5722,5742],{"__ignoreMap":31},[36,5693,5694,5696,5698,5700,5702],{"class":38,"line":39},[36,5695,2664],{"class":42},[36,5697,47],{"class":46},[36,5699,224],{"class":141},[36,5701,1243],{"class":145},[36,5703,1954],{"class":141},[36,5705,5706],{"class":38,"line":72},[36,5707,476],{"emptyLinePlaceholder":475},[36,5709,5710,5712,5714,5716,5718,5720],{"class":38,"line":89},[36,5711,76],{"class":75},[36,5713,79],{"class":50},[36,5715,97],{"class":96},[36,5717,79],{"class":50},[36,5719,2686],{"class":82},[36,5721,104],{"class":50},[36,5723,5724,5726,5728,5731,5733,5735,5737,5740],{"class":38,"line":496},[36,5725,76],{"class":75},[36,5727,79],{"class":50},[36,5729,5730],{"class":75},"isinstance",[36,5732,79],{"class":50},[36,5734,2686],{"class":82},[36,5736,58],{"class":50},[36,5738,5739],{"class":96}," int",[36,5741,104],{"class":50},[36,5743,5744,5746,5748,5750,5752,5754,5756,5759],{"class":38,"line":501},[36,5745,76],{"class":75},[36,5747,79],{"class":50},[36,5749,5730],{"class":75},[36,5751,79],{"class":50},[36,5753,2686],{"class":82},[36,5755,58],{"class":50},[36,5757,5758],{"class":96}," str",[36,5760,104],{"class":50},[14,5762,107],{},[26,5764,5766],{"className":28,"code":5765,"language":30,"meta":31,"style":31},"\u003Cclass 'str'>\nFalse\nTrue\n",[33,5767,5768,5782,5786],{"__ignoreMap":31},[36,5769,5770,5772,5774,5776,5778,5780],{"class":38,"line":39},[36,5771,134],{"class":46},[36,5773,138],{"class":137},[36,5775,142],{"class":141},[36,5777,2360],{"class":145},[36,5779,149],{"class":141},[36,5781,152],{"class":46},[36,5783,5784],{"class":38,"line":72},[36,5785,1486],{"class":400},[36,5787,5788],{"class":38,"line":89},[36,5789,1481],{"class":400},[14,5791,5792],{},"Useful checks:",[157,5794,5795,5800,5805,5810],{},[160,5796,5797],{},[33,5798,5799],{},"type(value)",[160,5801,5802],{},[33,5803,5804],{},"print(type(value))",[160,5806,5807],{},[33,5808,5809],{},"isinstance(value, int)",[160,5811,5812],{},[33,5813,5814],{},"isinstance(value, str)",[21,5816,1063],{"id":1062},[1065,5818,5820],{"id":5819},"what-is-a-data-type-in-simple-words","What is a data type in simple words?",[14,5822,5823,5824,5826,5827,351],{},"It is the kind of value you are working with, such as text, a number, or ",[33,5825,1179],{},"\u002F",[33,5828,1183],{},[1065,5830,5832],{"id":5831},"does-python-require-me-to-declare-data-types","Does Python require me to declare data types?",[14,5834,5835],{},"No. Python figures out the type from the value you assign.",[1065,5837,1240,5839,5841,5842,5844],{"id":5838},"is-10-the-same-as-10-in-python",[33,5840,5077],{}," the same as ",[33,5843,1560],{}," in Python?",[14,5846,2836,5847,5078,5849,5851],{},[33,5848,5077],{},[33,5850,1560],{}," is an integer.",[1065,5853,5855],{"id":5854},"how-do-i-check-a-data-type-in-python","How do I check a data type in Python?",[14,5857,5858,5859,5861,5862,1180,5865,351],{},"Use the ",[33,5860,1327],{}," function, such as ",[33,5863,5864],{},"type(5)",[33,5866,5867],{},"type(\"hello\")",[1065,5869,5871],{"id":5870},"why-do-data-types-matter","Why do data types matter?",[14,5873,5874],{},"They affect what operations are allowed and help prevent errors in your code.",[21,5876,1105],{"id":1104},[157,5878,5879,5883,5887,5892,5897],{},[160,5880,5881],{},[347,5882,2895],{"href":2894},[160,5884,5885],{},[347,5886,5266],{"href":5265},[160,5888,5889],{},[347,5890,5891],{"href":5382},"What is a variable in Python?",[160,5893,5894],{},[347,5895,5896],{"href":5660},"Type conversion in Python",[160,5898,5899],{},[347,5900,5088],{"href":5087},[1137,5902,5903],{},"html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":31,"searchDepth":72,"depth":72,"links":5905},[5906,5907,5908,5909,5910,5911,5912,5913,5914,5922],{"id":4713,"depth":72,"text":4714},{"id":4839,"depth":72,"text":4840},{"id":5002,"depth":72,"text":5003},{"id":5091,"depth":72,"text":5092},{"id":5269,"depth":72,"text":5270},{"id":2244,"depth":72,"text":2245},{"id":5622,"depth":72,"text":5623},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":5915},[5916,5917,5918,5920,5921],{"id":5819,"depth":89,"text":5820},{"id":5831,"depth":89,"text":5832},{"id":5838,"depth":89,"text":5919},"Is \"10\" the same as 10 in Python?",{"id":5854,"depth":89,"text":5855},{"id":5870,"depth":89,"text":5871},{"id":1104,"depth":72,"text":1105},"Master what is a data type in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-data-type-in-python",{"title":4679,"description":5923},"glossary\u002Fwhat-is-a-data-type-in-python","GD-Lz07MYxbr6RXICo3Zauv5LlpreWcF96DKL-o8e4E",{"id":5930,"title":5931,"body":5932,"description":7395,"extension":1159,"meta":7396,"navigation":475,"path":7397,"seo":7398,"stem":7399,"__hash__":7400},"content\u002Fglossary\u002Fwhat-is-a-dictionary-in-python.md","What Is a Dictionary in Python?",{"type":7,"value":5933,"toc":7373},[5934,5937,5945,5958,5961,5963,6076,6078,6093,6098,6102,6108,6132,6135,6149,6153,6156,6185,6188,6205,6208,6211,6222,6225,6308,6310,6319,6323,6326,6337,6349,6352,6366,6369,6373,6382,6396,6398,6506,6508,6523,6525,6545,6552,6556,6559,6577,6579,6680,6682,6736,6739,6743,6746,6752,6766,6771,6784,6787,6838,6841,6900,6903,6909,6911,6914,6936,6939,6998,7004,7006,7020,7023,7074,7084,7091,7147,7149,7158,7171,7175,7178,7242,7245,7256,7260,7263,7265,7291,7293,7297,7300,7304,7313,7317,7320,7324,7327,7331,7334,7336,7370],[10,5935,5931],{"id":5936},"what-is-a-dictionary-in-python",[14,5938,5939,5940,1281,5942,351],{},"A dictionary in Python is a way to store related data using ",[331,5941,4384],{},[331,5943,5944],{},"values",[14,5946,5947,5948,5950,5951,5953,5954,5957],{},"This is useful when your data has labels. For example, a person can have a ",[331,5949,817],{},", ",[331,5952,835],{},", and ",[331,5955,5956],{},"city",". Instead of storing those values by position, a dictionary stores them by name.",[14,5959,5960],{},"This page explains the basic idea of a dictionary, what it stores, and when to use one.",[21,5962,24],{"id":23},[26,5964,5966],{"className":28,"code":5965,"language":30,"meta":31,"style":31},"person = {\n    \"name\": \"Alice\",\n    \"age\": 25,\n    \"city\": \"Paris\"\n}\n\nprint(person[\"name\"])\nprint(person[\"age\"])\n",[33,5967,5968,5978,5999,6014,6031,6035,6039,6058],{"__ignoreMap":31},[36,5969,5970,5973,5975],{"class":38,"line":39},[36,5971,5972],{"class":42},"person ",[36,5974,47],{"class":46},[36,5976,5977],{"class":50}," {\n",[36,5979,5980,5983,5985,5987,5989,5991,5994,5996],{"class":38,"line":72},[36,5981,5982],{"class":141},"    \"",[36,5984,817],{"class":145},[36,5986,214],{"class":141},[36,5988,822],{"class":50},[36,5990,224],{"class":141},[36,5992,5993],{"class":145},"Alice",[36,5995,214],{"class":141},[36,5997,5998],{"class":50},",\n",[36,6000,6001,6003,6005,6007,6009,6012],{"class":38,"line":89},[36,6002,5982],{"class":141},[36,6004,835],{"class":145},[36,6006,214],{"class":141},[36,6008,822],{"class":50},[36,6010,6011],{"class":54}," 25",[36,6013,5998],{"class":50},[36,6015,6016,6018,6020,6022,6024,6026,6029],{"class":38,"line":496},[36,6017,5982],{"class":141},[36,6019,5956],{"class":145},[36,6021,214],{"class":141},[36,6023,822],{"class":50},[36,6025,224],{"class":141},[36,6027,6028],{"class":145},"Paris",[36,6030,1954],{"class":141},[36,6032,6033],{"class":38,"line":501},[36,6034,803],{"class":50},[36,6036,6037],{"class":38,"line":522},[36,6038,476],{"emptyLinePlaceholder":475},[36,6040,6041,6043,6045,6048,6050,6052,6054,6056],{"class":38,"line":527},[36,6042,76],{"class":75},[36,6044,79],{"class":50},[36,6046,6047],{"class":82},"person",[36,6049,117],{"class":50},[36,6051,214],{"class":141},[36,6053,817],{"class":145},[36,6055,214],{"class":141},[36,6057,493],{"class":50},[36,6059,6060,6062,6064,6066,6068,6070,6072,6074],{"class":38,"line":3092},[36,6061,76],{"class":75},[36,6063,79],{"class":50},[36,6065,6047],{"class":82},[36,6067,117],{"class":50},[36,6069,214],{"class":141},[36,6071,835],{"class":145},[36,6073,214],{"class":141},[36,6075,493],{"class":50},[14,6077,107],{},[26,6079,6081],{"className":28,"code":6080,"language":30,"meta":31,"style":31},"Alice\n25\n",[33,6082,6083,6088],{"__ignoreMap":31},[36,6084,6085],{"class":38,"line":39},[36,6086,6087],{"class":42},"Alice\n",[36,6089,6090],{"class":38,"line":72},[36,6091,6092],{"class":54},"25\n",[14,6094,6095,6096,351],{},"Use a dictionary when you want to store related values using names called ",[331,6097,4384],{},[21,6099,6101],{"id":6100},"simple-definition","Simple definition",[14,6103,6104,6105,351],{},"A dictionary is a Python data type that stores data as ",[331,6106,6107],{},"key-value pairs",[157,6109,6110,6116,6121],{},[160,6111,3355,6112,6115],{},[331,6113,6114],{},"key"," is the name you use to find a value",[160,6117,3355,6118,6120],{},[331,6119,2686],{}," is the data stored for that key",[160,6122,6123,6124,5950,6126,6128,6129],{},"Dictionaries are useful when data has labels like ",[33,6125,817],{},[33,6127,835],{},", or ",[33,6130,6131],{},"price",[14,6133,6134],{},"You will often see dictionaries used for:",[157,6136,6137,6140,6143,6146],{},[160,6138,6139],{},"user information",[160,6141,6142],{},"settings",[160,6144,6145],{},"product details",[160,6147,6148],{},"lookup data",[21,6150,6152],{"id":6151},"what-key-value-pairs-look-like","What key-value pairs look like",[14,6154,6155],{},"Here is a simple dictionary:",[26,6157,6159],{"className":28,"code":6158,"language":30,"meta":31,"style":31},"person = {\"name\": \"Alice\"}\n",[33,6160,6161],{"__ignoreMap":31},[36,6162,6163,6165,6167,6169,6171,6173,6175,6177,6179,6181,6183],{"class":38,"line":39},[36,6164,5972],{"class":42},[36,6166,47],{"class":46},[36,6168,778],{"class":50},[36,6170,214],{"class":141},[36,6172,817],{"class":145},[36,6174,214],{"class":141},[36,6176,822],{"class":50},[36,6178,224],{"class":141},[36,6180,5993],{"class":145},[36,6182,214],{"class":141},[36,6184,803],{"class":50},[14,6186,6187],{},"In this example:",[157,6189,6190,6198],{},[160,6191,6192,6195,6196],{},[33,6193,6194],{},"\"name\""," is the ",[331,6197,6114],{},[160,6199,6200,6195,6203],{},[33,6201,6202],{},"\"Alice\"",[331,6204,2686],{},[14,6206,6207],{},"A dictionary connects the key to the value.",[14,6209,6210],{},"Some important points:",[157,6212,6213,6216,6219],{},[160,6214,6215],{},"Each key points to one value",[160,6217,6218],{},"Keys in the same dictionary should be unique",[160,6220,6221],{},"Keys are often strings, but they do not have to be",[14,6223,6224],{},"For example, number keys also work:",[26,6226,6228],{"className":28,"code":6227,"language":30,"meta":31,"style":31},"scores = {\n    1: \"low\",\n    2: \"medium\",\n    3: \"high\"\n}\n\nprint(scores[2])\n",[33,6229,6230,6239,6255,6271,6285,6289,6293],{"__ignoreMap":31},[36,6231,6232,6235,6237],{"class":38,"line":39},[36,6233,6234],{"class":42},"scores ",[36,6236,47],{"class":46},[36,6238,5977],{"class":50},[36,6240,6241,6244,6246,6248,6251,6253],{"class":38,"line":72},[36,6242,6243],{"class":54},"    1",[36,6245,822],{"class":50},[36,6247,224],{"class":141},[36,6249,6250],{"class":145},"low",[36,6252,214],{"class":141},[36,6254,5998],{"class":50},[36,6256,6257,6260,6262,6264,6267,6269],{"class":38,"line":89},[36,6258,6259],{"class":54},"    2",[36,6261,822],{"class":50},[36,6263,224],{"class":141},[36,6265,6266],{"class":145},"medium",[36,6268,214],{"class":141},[36,6270,5998],{"class":50},[36,6272,6273,6276,6278,6280,6283],{"class":38,"line":496},[36,6274,6275],{"class":54},"    3",[36,6277,822],{"class":50},[36,6279,224],{"class":141},[36,6281,6282],{"class":145},"high",[36,6284,1954],{"class":141},[36,6286,6287],{"class":38,"line":501},[36,6288,803],{"class":50},[36,6290,6291],{"class":38,"line":522},[36,6292,476],{"emptyLinePlaceholder":475},[36,6294,6295,6297,6299,6302,6304,6306],{"class":38,"line":527},[36,6296,76],{"class":75},[36,6298,79],{"class":50},[36,6300,6301],{"class":82},"scores",[36,6303,117],{"class":50},[36,6305,1543],{"class":54},[36,6307,493],{"class":50},[14,6309,107],{},[26,6311,6313],{"className":28,"code":6312,"language":30,"meta":31,"style":31},"medium\n",[33,6314,6315],{"__ignoreMap":31},[36,6316,6317],{"class":38,"line":39},[36,6318,6312],{"class":42},[21,6320,6322],{"id":6321},"why-dictionaries-are-useful","Why dictionaries are useful",[14,6324,6325],{},"Dictionaries are useful because they make data easier to read and understand.",[14,6327,6328,6329,6332,6333,6336],{},"Instead of remembering that ",[33,6330,6331],{},"person[0]"," is a name and ",[33,6334,6335],{},"person[1]"," is an age, you can use clear labels like:",[157,6338,6339,6344],{},[160,6340,6341],{},[33,6342,6343],{},"person[\"name\"]",[160,6345,6346],{},[33,6347,6348],{},"person[\"age\"]",[14,6350,6351],{},"This makes dictionaries a good choice for:",[157,6353,6354,6357,6360,6363],{},[160,6355,6356],{},"storing records",[160,6358,6359],{},"saving settings",[160,6361,6362],{},"mapping one value to another",[160,6364,6365],{},"looking up data by name or ID",[14,6367,6368],{},"They are often easier to read than long lists when the data is labeled.",[21,6370,6372],{"id":6371},"basic-dictionary-example","Basic dictionary example",[14,6374,6375,6376,6379,6380,351],{},"You create a dictionary with ",[331,6377,6378],{},"curly braces"," ",[33,6381,936],{},[157,6383,6384,6387,6393],{},[160,6385,6386],{},"Separate each key-value pair with a comma",[160,6388,6389,6390,6392],{},"Use a colon ",[33,6391,822],{}," between each key and value",[160,6394,6395],{},"Access a value with square brackets and the key",[14,6397,197],{},[26,6399,6401],{"className":28,"code":6400,"language":30,"meta":31,"style":31},"book = {\n    \"title\": \"Python Basics\",\n    \"pages\": 200,\n    \"price\": 19.99\n}\n\nprint(book[\"title\"])\nprint(book[\"price\"])\n",[33,6402,6403,6412,6432,6448,6461,6465,6469,6488],{"__ignoreMap":31},[36,6404,6405,6408,6410],{"class":38,"line":39},[36,6406,6407],{"class":42},"book ",[36,6409,47],{"class":46},[36,6411,5977],{"class":50},[36,6413,6414,6416,6419,6421,6423,6425,6428,6430],{"class":38,"line":72},[36,6415,5982],{"class":141},[36,6417,6418],{"class":145},"title",[36,6420,214],{"class":141},[36,6422,822],{"class":50},[36,6424,224],{"class":141},[36,6426,6427],{"class":145},"Python Basics",[36,6429,214],{"class":141},[36,6431,5998],{"class":50},[36,6433,6434,6436,6439,6441,6443,6446],{"class":38,"line":89},[36,6435,5982],{"class":141},[36,6437,6438],{"class":145},"pages",[36,6440,214],{"class":141},[36,6442,822],{"class":50},[36,6444,6445],{"class":54}," 200",[36,6447,5998],{"class":50},[36,6449,6450,6452,6454,6456,6458],{"class":38,"line":496},[36,6451,5982],{"class":141},[36,6453,6131],{"class":145},[36,6455,214],{"class":141},[36,6457,822],{"class":50},[36,6459,6460],{"class":54}," 19.99\n",[36,6462,6463],{"class":38,"line":501},[36,6464,803],{"class":50},[36,6466,6467],{"class":38,"line":522},[36,6468,476],{"emptyLinePlaceholder":475},[36,6470,6471,6473,6475,6478,6480,6482,6484,6486],{"class":38,"line":527},[36,6472,76],{"class":75},[36,6474,79],{"class":50},[36,6476,6477],{"class":82},"book",[36,6479,117],{"class":50},[36,6481,214],{"class":141},[36,6483,6418],{"class":145},[36,6485,214],{"class":141},[36,6487,493],{"class":50},[36,6489,6490,6492,6494,6496,6498,6500,6502,6504],{"class":38,"line":3092},[36,6491,76],{"class":75},[36,6493,79],{"class":50},[36,6495,6477],{"class":82},[36,6497,117],{"class":50},[36,6499,214],{"class":141},[36,6501,6131],{"class":145},[36,6503,214],{"class":141},[36,6505,493],{"class":50},[14,6507,107],{},[26,6509,6511],{"className":28,"code":6510,"language":30,"meta":31,"style":31},"Python Basics\n19.99\n",[33,6512,6513,6518],{"__ignoreMap":31},[36,6514,6515],{"class":38,"line":39},[36,6516,6517],{"class":42},"Python Basics\n",[36,6519,6520],{"class":38,"line":72},[36,6521,6522],{"class":54},"19.99\n",[14,6524,6187],{},[157,6526,6527,6533,6539],{},[160,6528,6529,6532],{},[33,6530,6531],{},"\"title\""," is a key",[160,6534,6535,6538],{},[33,6536,6537],{},"\"Python Basics\""," is its value",[160,6540,6541,6544],{},[33,6542,6543],{},"book[\"title\"]"," gets the value for that key",[14,6546,6547,6548,351],{},"If you want to learn the syntax in more detail, see ",[347,6549,6551],{"href":6550},"\u002Freference\u002Fpython-dictionary-creating-a-dictionary\u002F","creating a dictionary in Python",[21,6553,6555],{"id":6554},"important-beginner-facts","Important beginner facts",[14,6557,6558],{},"There are a few important things beginners should know about dictionaries.",[157,6560,6561,6568,6571,6574],{},[160,6562,6563,6564,6567],{},"Dictionaries are ",[331,6565,6566],{},"mutable",", which means you can change them after creating them",[160,6569,6570],{},"You can add new key-value pairs later",[160,6572,6573],{},"You can update existing values",[160,6575,6576],{},"Dictionary keys are not based on numeric positions like list indexes",[14,6578,197],{},[26,6580,6582],{"className":28,"code":6581,"language":30,"meta":31,"style":31},"person = {\"name\": \"Alice\", \"age\": 25}\n\nperson[\"age\"] = 26\nperson[\"city\"] = \"Paris\"\n\nprint(person)\n",[33,6583,6584,6620,6624,6644,6666,6670],{"__ignoreMap":31},[36,6585,6586,6588,6590,6592,6594,6596,6598,6600,6602,6604,6606,6608,6610,6612,6614,6616,6618],{"class":38,"line":39},[36,6587,5972],{"class":42},[36,6589,47],{"class":46},[36,6591,778],{"class":50},[36,6593,214],{"class":141},[36,6595,817],{"class":145},[36,6597,214],{"class":141},[36,6599,822],{"class":50},[36,6601,224],{"class":141},[36,6603,5993],{"class":145},[36,6605,214],{"class":141},[36,6607,58],{"class":50},[36,6609,224],{"class":141},[36,6611,835],{"class":145},[36,6613,214],{"class":141},[36,6615,822],{"class":50},[36,6617,6011],{"class":54},[36,6619,803],{"class":50},[36,6621,6622],{"class":38,"line":72},[36,6623,476],{"emptyLinePlaceholder":475},[36,6625,6626,6628,6630,6632,6634,6636,6639,6641],{"class":38,"line":89},[36,6627,6047],{"class":42},[36,6629,117],{"class":50},[36,6631,214],{"class":141},[36,6633,835],{"class":145},[36,6635,214],{"class":141},[36,6637,6638],{"class":50},"]",[36,6640,3068],{"class":46},[36,6642,6643],{"class":54}," 26\n",[36,6645,6646,6648,6650,6652,6654,6656,6658,6660,6662,6664],{"class":38,"line":496},[36,6647,6047],{"class":42},[36,6649,117],{"class":50},[36,6651,214],{"class":141},[36,6653,5956],{"class":145},[36,6655,214],{"class":141},[36,6657,6638],{"class":50},[36,6659,3068],{"class":46},[36,6661,224],{"class":141},[36,6663,6028],{"class":145},[36,6665,1954],{"class":141},[36,6667,6668],{"class":38,"line":501},[36,6669,476],{"emptyLinePlaceholder":475},[36,6671,6672,6674,6676,6678],{"class":38,"line":522},[36,6673,76],{"class":75},[36,6675,79],{"class":50},[36,6677,6047],{"class":82},[36,6679,86],{"class":50},[14,6681,107],{},[26,6683,6685],{"className":28,"code":6684,"language":30,"meta":31,"style":31},"{'name': 'Alice', 'age': 26, 'city': 'Paris'}\n",[33,6686,6687],{"__ignoreMap":31},[36,6688,6689,6691,6693,6695,6697,6699,6701,6703,6705,6707,6709,6711,6713,6715,6718,6720,6722,6724,6726,6728,6730,6732,6734],{"class":38,"line":39},[36,6690,4552],{"class":50},[36,6692,149],{"class":141},[36,6694,817],{"class":145},[36,6696,149],{"class":141},[36,6698,822],{"class":50},[36,6700,142],{"class":141},[36,6702,5993],{"class":145},[36,6704,149],{"class":141},[36,6706,58],{"class":50},[36,6708,142],{"class":141},[36,6710,835],{"class":145},[36,6712,149],{"class":141},[36,6714,822],{"class":50},[36,6716,6717],{"class":54}," 26",[36,6719,58],{"class":50},[36,6721,142],{"class":141},[36,6723,5956],{"class":145},[36,6725,149],{"class":141},[36,6727,822],{"class":50},[36,6729,142],{"class":141},[36,6731,6028],{"class":145},[36,6733,149],{"class":141},[36,6735,803],{"class":50},[14,6737,6738],{},"This means dictionaries are flexible. You can start with a small set of data and change it as needed.",[21,6740,6742],{"id":6741},"dictionary-vs-list","Dictionary vs list",[14,6744,6745],{},"A dictionary is not the same as a list.",[14,6747,6748,6749,6751],{},"Use a ",[331,6750,146],{}," when:",[157,6753,6754,6757,6760],{},[160,6755,6756],{},"order matters",[160,6758,6759],{},"positions matter",[160,6761,6762,6763],{},"you want to access items by index like ",[33,6764,6765],{},"items[0]",[14,6767,6748,6768,6751],{},[331,6769,6770],{},"dictionary",[157,6772,6773,6776,6779],{},[160,6774,6775],{},"labels matter",[160,6777,6778],{},"you want named values",[160,6780,6781,6782],{},"you want to access data by keys like ",[33,6783,6343],{},[14,6785,6786],{},"Example of a list:",[26,6788,6790],{"className":28,"code":6789,"language":30,"meta":31,"style":31},"colors = [\"red\", \"green\", \"blue\"]\nprint(colors[0])\n",[33,6791,6792,6824],{"__ignoreMap":31},[36,6793,6794,6796,6798,6800,6802,6804,6806,6808,6810,6812,6814,6816,6818,6820,6822],{"class":38,"line":39},[36,6795,207],{"class":42},[36,6797,47],{"class":46},[36,6799,51],{"class":50},[36,6801,214],{"class":141},[36,6803,217],{"class":145},[36,6805,214],{"class":141},[36,6807,58],{"class":50},[36,6809,224],{"class":141},[36,6811,227],{"class":145},[36,6813,214],{"class":141},[36,6815,58],{"class":50},[36,6817,224],{"class":141},[36,6819,236],{"class":145},[36,6821,214],{"class":141},[36,6823,69],{"class":50},[36,6825,6826,6828,6830,6832,6834,6836],{"class":38,"line":72},[36,6827,76],{"class":75},[36,6829,79],{"class":50},[36,6831,246],{"class":82},[36,6833,117],{"class":50},[36,6835,490],{"class":54},[36,6837,493],{"class":50},[14,6839,6840],{},"Example of a dictionary:",[26,6842,6844],{"className":28,"code":6843,"language":30,"meta":31,"style":31},"person = {\"name\": \"Alice\", \"age\": 25}\nprint(person[\"name\"])\n",[33,6845,6846,6882],{"__ignoreMap":31},[36,6847,6848,6850,6852,6854,6856,6858,6860,6862,6864,6866,6868,6870,6872,6874,6876,6878,6880],{"class":38,"line":39},[36,6849,5972],{"class":42},[36,6851,47],{"class":46},[36,6853,778],{"class":50},[36,6855,214],{"class":141},[36,6857,817],{"class":145},[36,6859,214],{"class":141},[36,6861,822],{"class":50},[36,6863,224],{"class":141},[36,6865,5993],{"class":145},[36,6867,214],{"class":141},[36,6869,58],{"class":50},[36,6871,224],{"class":141},[36,6873,835],{"class":145},[36,6875,214],{"class":141},[36,6877,822],{"class":50},[36,6879,6011],{"class":54},[36,6881,803],{"class":50},[36,6883,6884,6886,6888,6890,6892,6894,6896,6898],{"class":38,"line":72},[36,6885,76],{"class":75},[36,6887,79],{"class":50},[36,6889,6047],{"class":82},[36,6891,117],{"class":50},[36,6893,214],{"class":141},[36,6895,817],{"class":145},[36,6897,214],{"class":141},[36,6899,493],{"class":50},[14,6901,6902],{},"Lists use indexes like numbers. Dictionaries use keys like names or IDs.",[14,6904,6905,6906,351],{},"If you want a broader comparison, see ",[347,6907,851],{"href":6908},"\u002Flearn\u002Fwhen-to-use-lists-vs-tuples-vs-sets-vs-dictionaries\u002F",[21,6910,2245],{"id":2244},[14,6912,6913],{},"Here are some common mistakes beginners make with dictionaries.",[157,6915,6916,6919,6928,6934],{},[160,6917,6918],{},"A dictionary is not the same as a list",[160,6920,6921,6922,6924,6925,6927],{},"You do not get values with ",[33,6923,6331],{}," unless ",[33,6926,490],{}," is actually a key",[160,6929,6930,6931],{},"Missing keys can cause a ",[33,6932,6933],{},"KeyError",[160,6935,6221],{},[14,6937,6938],{},"Example of a mistake:",[26,6940,6942],{"className":28,"code":6941,"language":30,"meta":31,"style":31},"person = {\"name\": \"Alice\", \"age\": 25}\n\nprint(person[0])\n",[33,6943,6944,6980,6984],{"__ignoreMap":31},[36,6945,6946,6948,6950,6952,6954,6956,6958,6960,6962,6964,6966,6968,6970,6972,6974,6976,6978],{"class":38,"line":39},[36,6947,5972],{"class":42},[36,6949,47],{"class":46},[36,6951,778],{"class":50},[36,6953,214],{"class":141},[36,6955,817],{"class":145},[36,6957,214],{"class":141},[36,6959,822],{"class":50},[36,6961,224],{"class":141},[36,6963,5993],{"class":145},[36,6965,214],{"class":141},[36,6967,58],{"class":50},[36,6969,224],{"class":141},[36,6971,835],{"class":145},[36,6973,214],{"class":141},[36,6975,822],{"class":50},[36,6977,6011],{"class":54},[36,6979,803],{"class":50},[36,6981,6982],{"class":38,"line":72},[36,6983,476],{"emptyLinePlaceholder":475},[36,6985,6986,6988,6990,6992,6994,6996],{"class":38,"line":89},[36,6987,76],{"class":75},[36,6989,79],{"class":50},[36,6991,6047],{"class":82},[36,6993,117],{"class":50},[36,6995,490],{"class":54},[36,6997,493],{"class":50},[14,6999,7000,7001,7003],{},"This will cause an error because ",[33,7002,490],{}," is not a key in the dictionary.",[1065,7005,905],{"id":904},[157,7007,7008,7011,7014,7017],{},[160,7009,7010],{},"Trying to access a dictionary value with a list-style index",[160,7012,7013],{},"Using a key that does not exist",[160,7015,7016],{},"Confusing keys with values",[160,7018,7019],{},"Assuming dictionaries are only for strings",[14,7021,7022],{},"Example of a missing key:",[26,7024,7026],{"className":28,"code":7025,"language":30,"meta":31,"style":31},"person = {\"name\": \"Alice\"}\n\nprint(person[\"age\"])\n",[33,7027,7028,7052,7056],{"__ignoreMap":31},[36,7029,7030,7032,7034,7036,7038,7040,7042,7044,7046,7048,7050],{"class":38,"line":39},[36,7031,5972],{"class":42},[36,7033,47],{"class":46},[36,7035,778],{"class":50},[36,7037,214],{"class":141},[36,7039,817],{"class":145},[36,7041,214],{"class":141},[36,7043,822],{"class":50},[36,7045,224],{"class":141},[36,7047,5993],{"class":145},[36,7049,214],{"class":141},[36,7051,803],{"class":50},[36,7053,7054],{"class":38,"line":72},[36,7055,476],{"emptyLinePlaceholder":475},[36,7057,7058,7060,7062,7064,7066,7068,7070,7072],{"class":38,"line":89},[36,7059,76],{"class":75},[36,7061,79],{"class":50},[36,7063,6047],{"class":82},[36,7065,117],{"class":50},[36,7067,214],{"class":141},[36,7069,835],{"class":145},[36,7071,214],{"class":141},[36,7073,493],{"class":50},[14,7075,7076,7077,7079,7080,7083],{},"This raises a ",[33,7078,6933],{}," because ",[33,7081,7082],{},"\"age\""," is not in the dictionary.",[14,7085,7086,7087,7090],{},"A safer option is often the ",[33,7088,7089],{},"get()"," method:",[26,7092,7094],{"className":28,"code":7093,"language":30,"meta":31,"style":31},"person = {\"name\": \"Alice\"}\n\nprint(person.get(\"age\"))\n",[33,7095,7096,7120,7124],{"__ignoreMap":31},[36,7097,7098,7100,7102,7104,7106,7108,7110,7112,7114,7116,7118],{"class":38,"line":39},[36,7099,5972],{"class":42},[36,7101,47],{"class":46},[36,7103,778],{"class":50},[36,7105,214],{"class":141},[36,7107,817],{"class":145},[36,7109,214],{"class":141},[36,7111,822],{"class":50},[36,7113,224],{"class":141},[36,7115,5993],{"class":145},[36,7117,214],{"class":141},[36,7119,803],{"class":50},[36,7121,7122],{"class":38,"line":72},[36,7123,476],{"emptyLinePlaceholder":475},[36,7125,7126,7128,7130,7132,7134,7137,7139,7141,7143,7145],{"class":38,"line":89},[36,7127,76],{"class":75},[36,7129,79],{"class":50},[36,7131,6047],{"class":82},[36,7133,351],{"class":50},[36,7135,7136],{"class":82},"get",[36,7138,79],{"class":50},[36,7140,214],{"class":141},[36,7142,835],{"class":145},[36,7144,214],{"class":141},[36,7146,104],{"class":50},[14,7148,107],{},[26,7150,7152],{"className":28,"code":7151,"language":30,"meta":31,"style":31},"None\n",[33,7153,7154],{"__ignoreMap":31},[36,7155,7156],{"class":38,"line":39},[36,7157,7151],{"class":400},[14,7159,7160,7161,1281,7167,351],{},"To learn more, see ",[347,7162,7164,7165],{"href":7163},"\u002Freference\u002Fpython-dictionary-get-method\u002F","Python dictionary ",[33,7166,7089],{},[347,7168,7170],{"href":7169},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes\u002F","KeyError in Python: causes and fixes",[1065,7172,7174],{"id":7173},"useful-debugging-checks","Useful debugging checks",[14,7176,7177],{},"If you are not sure what your dictionary contains, these simple checks help:",[26,7179,7181],{"className":28,"code":7180,"language":30,"meta":31,"style":31},"print(my_dict)\nprint(my_dict.keys())\nprint(type(my_dict))\nprint(\"name\" in my_dict)\n",[33,7182,7183,7194,7208,7222],{"__ignoreMap":31},[36,7184,7185,7187,7189,7192],{"class":38,"line":39},[36,7186,76],{"class":75},[36,7188,79],{"class":50},[36,7190,7191],{"class":82},"my_dict",[36,7193,86],{"class":50},[36,7195,7196,7198,7200,7202,7204,7206],{"class":38,"line":72},[36,7197,76],{"class":75},[36,7199,79],{"class":50},[36,7201,7191],{"class":82},[36,7203,351],{"class":50},[36,7205,4384],{"class":82},[36,7207,4387],{"class":50},[36,7209,7210,7212,7214,7216,7218,7220],{"class":38,"line":89},[36,7211,76],{"class":75},[36,7213,79],{"class":50},[36,7215,97],{"class":96},[36,7217,79],{"class":50},[36,7219,7191],{"class":82},[36,7221,104],{"class":50},[36,7223,7224,7226,7228,7230,7232,7234,7237,7240],{"class":38,"line":496},[36,7225,76],{"class":75},[36,7227,79],{"class":50},[36,7229,214],{"class":141},[36,7231,817],{"class":145},[36,7233,214],{"class":141},[36,7235,7236],{"class":1718}," in",[36,7238,7239],{"class":82}," my_dict",[36,7241,86],{"class":50},[14,7243,7244],{},"These can help you answer questions like:",[157,7246,7247,7250,7253],{},[160,7248,7249],{},"Is this really a dictionary?",[160,7251,7252],{},"What keys does it have?",[160,7254,7255],{},"Does a specific key exist?",[21,7257,7259],{"id":7258},"next-steps","Next steps",[14,7261,7262],{},"Once you understand the basic idea, the next step is using dictionaries in simple tasks.",[14,7264,5629],{},[157,7266,7267,7273,7279,7285],{},[160,7268,7269],{},[347,7270,7272],{"href":7271},"\u002Flearn\u002Fpython-dictionaries-explained\u002F","Python dictionaries explained",[160,7274,7275],{},[347,7276,7278],{"href":7277},"\u002Fhow-to\u002Fhow-to-access-values-in-a-dictionary-in-python\u002F","How to access values in a dictionary in Python",[160,7280,7281],{},[347,7282,7284],{"href":7283},"\u002Fhow-to\u002Fhow-to-add-a-key-to-a-dictionary-in-python\u002F","How to add a key to a dictionary in Python",[160,7286,7287],{},[347,7288,7290],{"href":7289},"\u002Fhow-to\u002Fhow-to-check-if-a-key-exists-in-a-dictionary-in-python\u002F","How to check if a key exists in a dictionary in Python",[21,7292,1063],{"id":1062},[1065,7294,7296],{"id":7295},"what-is-a-dictionary-in-python-in-simple-words","What is a dictionary in Python in simple words?",[14,7298,7299],{},"It is a way to store data using names called keys and the data connected to them called values.",[1065,7301,7303],{"id":7302},"when-should-i-use-a-dictionary-instead-of-a-list","When should I use a dictionary instead of a list?",[14,7305,7306,7307,5950,7309,6128,7311,351],{},"Use a dictionary when your data has labels, such as ",[33,7308,817],{},[33,7310,835],{},[33,7312,6131],{},[1065,7314,7316],{"id":7315},"can-dictionary-keys-be-numbers","Can dictionary keys be numbers?",[14,7318,7319],{},"Yes. Keys can be numbers, strings, and some other immutable types.",[1065,7321,7323],{"id":7322},"can-a-dictionary-have-duplicate-keys","Can a dictionary have duplicate keys?",[14,7325,7326],{},"No. Each key should be unique in the same dictionary.",[1065,7328,7330],{"id":7329},"are-python-dictionaries-changeable","Are Python dictionaries changeable?",[14,7332,7333],{},"Yes. Dictionaries are mutable, so you can add, remove, or update items.",[21,7335,1105],{"id":1104},[157,7337,7338,7342,7347,7354,7358,7362,7366],{},[160,7339,7340],{},[347,7341,7272],{"href":7271},[160,7343,7344],{},[347,7345,7346],{"href":6550},"Creating a dictionary in Python",[160,7348,7349],{},[347,7350,7164,7351,7353],{"href":7163},[33,7352,7089],{}," method",[160,7355,7356],{},[347,7357,7278],{"href":7277},[160,7359,7360],{},[347,7361,7284],{"href":7283},[160,7363,7364],{},[347,7365,7170],{"href":7169},[160,7367,7368],{},[347,7369,1135],{"href":6908},[1137,7371,7372],{},"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 .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 .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 .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":31,"searchDepth":72,"depth":72,"links":7374},[7375,7376,7377,7378,7379,7380,7381,7382,7386,7387,7394],{"id":23,"depth":72,"text":24},{"id":6100,"depth":72,"text":6101},{"id":6151,"depth":72,"text":6152},{"id":6321,"depth":72,"text":6322},{"id":6371,"depth":72,"text":6372},{"id":6554,"depth":72,"text":6555},{"id":6741,"depth":72,"text":6742},{"id":2244,"depth":72,"text":2245,"children":7383},[7384,7385],{"id":904,"depth":89,"text":905},{"id":7173,"depth":89,"text":7174},{"id":7258,"depth":72,"text":7259},{"id":1062,"depth":72,"text":1063,"children":7388},[7389,7390,7391,7392,7393],{"id":7295,"depth":89,"text":7296},{"id":7302,"depth":89,"text":7303},{"id":7315,"depth":89,"text":7316},{"id":7322,"depth":89,"text":7323},{"id":7329,"depth":89,"text":7330},{"id":1104,"depth":72,"text":1105},"Master what is a dictionary in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-dictionary-in-python",{"title":5931,"description":7395},"glossary\u002Fwhat-is-a-dictionary-in-python","w7V5RnPyw6Rt9Auw1dQHepLQxRCSyMasKCA0lo-xXuU",{"id":7402,"title":7403,"body":7404,"description":8734,"extension":1159,"meta":8735,"navigation":475,"path":8736,"seo":8737,"stem":8738,"__hash__":8739},"content\u002Fglossary\u002Fwhat-is-a-float-in-python.md","What Is a Float in Python?",{"type":7,"value":7405,"toc":8716},[7406,7409,7422,7425,7431,7435,7440,7442,7460,7466,7471,7475,7478,7480,7497,7500,7503,7541,7543,7567,7573,7620,7622,7669,7673,7676,7693,7695,7770,7772,7792,7795,7802,7809,7813,7818,7834,7839,7856,7859,7874,7876,7924,7926,7963,7971,7974,8018,8020,8044,8046,8049,8093,8095,8119,8122,8144,8148,8151,8154,8156,8177,8179,8188,8195,8198,8226,8228,8237,8247,8251,8260,8262,8301,8303,8317,8320,8385,8387,8411,8417,8436,8439,8469,8480,8482,8485,8508,8510,8597,8600,8611,8613,8617,8630,8634,8641,8645,8653,8657,8660,8664,8673,8675,8713],[10,7407,7403],{"id":7408},"what-is-a-float-in-python",[14,7410,3355,7411,7413,7414,5950,7416,6128,7419,351],{},[331,7412,4740],{}," in Python is a number that can include a decimal point. Beginners often use floats for values like ",[33,7415,4871],{},[33,7417,7418],{},"2.5",[33,7420,7421],{},"-0.75",[14,7423,7424],{},"Floats are useful when you need numbers that are not just whole numbers. For example, you might use them for measurements, prices in simple examples, or division results.",[14,7426,7427,7428,7430],{},"This page explains what the term ",[331,7429,4740],{}," means, how it looks in Python, and how it differs from an integer.",[21,7432,7434],{"id":7433},"definition","Definition",[14,7436,3355,7437,7439],{},[331,7438,4740],{}," is a numeric type in Python used for numbers with decimal values.",[14,7441,694],{},[157,7443,7444,7448,7452,7456],{},[160,7445,7446],{},[33,7447,4871],{},[160,7449,7450],{},[33,7451,7418],{},[160,7453,7454],{},[33,7455,7421],{},[160,7457,7458],{},[33,7459,5403],{},[14,7461,7462,7463,7465],{},"Even though ",[33,7464,5403],{}," looks like a whole number, Python still treats it as a float because it has a decimal point.",[14,7467,7468,7469,351],{},"If you want to compare floats with whole numbers, see ",[347,7470,5643],{"href":5642},[21,7472,7474],{"id":7473},"how-floats-look-in-python","How floats look in Python",[14,7476,7477],{},"Floats usually appear with a decimal point.",[14,7479,5098],{},[157,7481,7482,7487,7492],{},[160,7483,7484],{},[33,7485,7486],{},"1.5",[160,7488,7489],{},[33,7490,7491],{},"0.0",[160,7493,7494],{},[33,7495,7496],{},"-3.2",[14,7498,7499],{},"A decimal point usually means the value is a float.",[14,7501,7502],{},"Python also supports scientific notation. This also creates a float:",[26,7504,7506],{"className":28,"code":7505,"language":30,"meta":31,"style":31},"x = 1e3\nprint(x)\nprint(type(x))\n",[33,7507,7508,7517,7527],{"__ignoreMap":31},[36,7509,7510,7512,7514],{"class":38,"line":39},[36,7511,5289],{"class":42},[36,7513,47],{"class":46},[36,7515,7516],{"class":54}," 1e3\n",[36,7518,7519,7521,7523,7525],{"class":38,"line":72},[36,7520,76],{"class":75},[36,7522,79],{"class":50},[36,7524,5306],{"class":82},[36,7526,86],{"class":50},[36,7528,7529,7531,7533,7535,7537,7539],{"class":38,"line":89},[36,7530,76],{"class":75},[36,7532,79],{"class":50},[36,7534,97],{"class":96},[36,7536,79],{"class":50},[36,7538,5306],{"class":82},[36,7540,104],{"class":50},[14,7542,107],{},[26,7544,7546],{"className":28,"code":7545,"language":30,"meta":31,"style":31},"1000.0\n\u003Cclass 'float'>\n",[33,7547,7548,7553],{"__ignoreMap":31},[36,7549,7550],{"class":38,"line":39},[36,7551,7552],{"class":54},"1000.0\n",[36,7554,7555,7557,7559,7561,7563,7565],{"class":38,"line":72},[36,7556,134],{"class":46},[36,7558,138],{"class":137},[36,7560,142],{"class":141},[36,7562,4740],{"class":145},[36,7564,149],{"class":141},[36,7566,152],{"class":46},[14,7568,5129,7569,822],{},[347,7570,7571],{"href":5265},[33,7572,1327],{},[26,7574,7576],{"className":28,"code":7575,"language":30,"meta":31,"style":31},"print(type(3.14))\nprint(type(5.0))\nprint(type(5))\n",[33,7577,7578,7592,7606],{"__ignoreMap":31},[36,7579,7580,7582,7584,7586,7588,7590],{"class":38,"line":39},[36,7581,76],{"class":75},[36,7583,79],{"class":50},[36,7585,97],{"class":96},[36,7587,79],{"class":50},[36,7589,4871],{"class":54},[36,7591,104],{"class":50},[36,7593,7594,7596,7598,7600,7602,7604],{"class":38,"line":72},[36,7595,76],{"class":75},[36,7597,79],{"class":50},[36,7599,97],{"class":96},[36,7601,79],{"class":50},[36,7603,5403],{"class":54},[36,7605,104],{"class":50},[36,7607,7608,7610,7612,7614,7616,7618],{"class":38,"line":89},[36,7609,76],{"class":75},[36,7611,79],{"class":50},[36,7613,97],{"class":96},[36,7615,79],{"class":50},[36,7617,1243],{"class":54},[36,7619,104],{"class":50},[14,7621,107],{},[26,7623,7625],{"className":28,"code":7624,"language":30,"meta":31,"style":31},"\u003Cclass 'float'>\n\u003Cclass 'float'>\n\u003Cclass 'int'>\n",[33,7626,7627,7641,7655],{"__ignoreMap":31},[36,7628,7629,7631,7633,7635,7637,7639],{"class":38,"line":39},[36,7630,134],{"class":46},[36,7632,138],{"class":137},[36,7634,142],{"class":141},[36,7636,4740],{"class":145},[36,7638,149],{"class":141},[36,7640,152],{"class":46},[36,7642,7643,7645,7647,7649,7651,7653],{"class":38,"line":72},[36,7644,134],{"class":46},[36,7646,138],{"class":137},[36,7648,142],{"class":141},[36,7650,4740],{"class":145},[36,7652,149],{"class":141},[36,7654,152],{"class":46},[36,7656,7657,7659,7661,7663,7665,7667],{"class":38,"line":89},[36,7658,134],{"class":46},[36,7660,138],{"class":137},[36,7662,142],{"class":141},[36,7664,2466],{"class":145},[36,7666,149],{"class":141},[36,7668,152],{"class":46},[21,7670,7672],{"id":7671},"when-beginners-use-floats","When beginners use floats",[14,7674,7675],{},"Beginners often use floats in situations like these:",[157,7677,7678,7681,7684,7687],{},[160,7679,7680],{},"Measurements such as height, weight, or distance",[160,7682,7683],{},"Money examples in simple lessons",[160,7685,7686],{},"Division results",[160,7688,7689,7690],{},"User input converted with ",[33,7691,7692],{},"float()",[14,7694,197],{},[26,7696,7698],{"className":28,"code":7697,"language":30,"meta":31,"style":31},"height = 1.75\nprice = 4.99\nresult = 7 \u002F 2\n\nprint(height)\nprint(price)\nprint(result)\n",[33,7699,7700,7710,7719,7734,7738,7749,7759],{"__ignoreMap":31},[36,7701,7702,7705,7707],{"class":38,"line":39},[36,7703,7704],{"class":42},"height ",[36,7706,47],{"class":46},[36,7708,7709],{"class":54}," 1.75\n",[36,7711,7712,7714,7716],{"class":38,"line":72},[36,7713,4932],{"class":42},[36,7715,47],{"class":46},[36,7717,7718],{"class":54}," 4.99\n",[36,7720,7721,7724,7726,7728,7731],{"class":38,"line":89},[36,7722,7723],{"class":42},"result ",[36,7725,47],{"class":46},[36,7727,1566],{"class":54},[36,7729,7730],{"class":46}," \u002F",[36,7732,7733],{"class":54}," 2\n",[36,7735,7736],{"class":38,"line":496},[36,7737,476],{"emptyLinePlaceholder":475},[36,7739,7740,7742,7744,7747],{"class":38,"line":501},[36,7741,76],{"class":75},[36,7743,79],{"class":50},[36,7745,7746],{"class":82},"height",[36,7748,86],{"class":50},[36,7750,7751,7753,7755,7757],{"class":38,"line":522},[36,7752,76],{"class":75},[36,7754,79],{"class":50},[36,7756,6131],{"class":82},[36,7758,86],{"class":50},[36,7760,7761,7763,7765,7768],{"class":38,"line":527},[36,7762,76],{"class":75},[36,7764,79],{"class":50},[36,7766,7767],{"class":82},"result",[36,7769,86],{"class":50},[14,7771,107],{},[26,7773,7775],{"className":28,"code":7774,"language":30,"meta":31,"style":31},"1.75\n4.99\n3.5\n",[33,7776,7777,7782,7787],{"__ignoreMap":31},[36,7778,7779],{"class":38,"line":39},[36,7780,7781],{"class":54},"1.75\n",[36,7783,7784],{"class":38,"line":72},[36,7785,7786],{"class":54},"4.99\n",[36,7788,7789],{"class":38,"line":89},[36,7790,7791],{"class":54},"3.5\n",[14,7793,7794],{},"A useful beginner rule is this:",[157,7796,7797],{},[160,7798,7799,7801],{},[33,7800,5826],{}," returns a float in Python",[14,7803,7804,7805,351],{},"If you want a wider overview of number types, see ",[347,7806,7808],{"href":7807},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex\u002F","Python numbers explained: int, float, complex",[21,7810,7812],{"id":7811},"float-vs-int","Float vs int",[14,7814,3361,7815,7817],{},[331,7816,2466],{}," stores whole numbers:",[157,7819,7820,7824,7829],{},[160,7821,7822],{},[33,7823,55],{},[160,7825,7826],{},[33,7827,7828],{},"25",[160,7830,7831],{},[33,7832,7833],{},"-7",[14,7835,3355,7836,7838],{},[331,7837,4740],{}," stores numbers with decimal values:",[157,7840,7841,7846,7851],{},[160,7842,7843],{},[33,7844,7845],{},"1.0",[160,7847,7848],{},[33,7849,7850],{},"25.5",[160,7852,7853],{},[33,7854,7855],{},"-7.25",[14,7857,7858],{},"These two values are not the same type:",[157,7860,7861,7868],{},[160,7862,7863,7865,7866],{},[33,7864,1243],{}," → ",[33,7867,2466],{},[160,7869,7870,7865,7872],{},[33,7871,5403],{},[33,7873,4740],{},[14,7875,197],{},[26,7877,7879],{"className":28,"code":7878,"language":30,"meta":31,"style":31},"print(type(5))\nprint(type(5.0))\nprint(5 == 5.0)\n",[33,7880,7881,7895,7909],{"__ignoreMap":31},[36,7882,7883,7885,7887,7889,7891,7893],{"class":38,"line":39},[36,7884,76],{"class":75},[36,7886,79],{"class":50},[36,7888,97],{"class":96},[36,7890,79],{"class":50},[36,7892,1243],{"class":54},[36,7894,104],{"class":50},[36,7896,7897,7899,7901,7903,7905,7907],{"class":38,"line":72},[36,7898,76],{"class":75},[36,7900,79],{"class":50},[36,7902,97],{"class":96},[36,7904,79],{"class":50},[36,7906,5403],{"class":54},[36,7908,104],{"class":50},[36,7910,7911,7913,7915,7917,7919,7922],{"class":38,"line":89},[36,7912,76],{"class":75},[36,7914,79],{"class":50},[36,7916,1243],{"class":54},[36,7918,1546],{"class":46},[36,7920,7921],{"class":54}," 5.0",[36,7923,86],{"class":50},[14,7925,107],{},[26,7927,7929],{"className":28,"code":7928,"language":30,"meta":31,"style":31},"\u003Cclass 'int'>\n\u003Cclass 'float'>\nTrue\n",[33,7930,7931,7945,7959],{"__ignoreMap":31},[36,7932,7933,7935,7937,7939,7941,7943],{"class":38,"line":39},[36,7934,134],{"class":46},[36,7936,138],{"class":137},[36,7938,142],{"class":141},[36,7940,2466],{"class":145},[36,7942,149],{"class":141},[36,7944,152],{"class":46},[36,7946,7947,7949,7951,7953,7955,7957],{"class":38,"line":72},[36,7948,134],{"class":46},[36,7950,138],{"class":137},[36,7952,142],{"class":141},[36,7954,4740],{"class":145},[36,7956,149],{"class":141},[36,7958,152],{"class":46},[36,7960,7961],{"class":38,"line":89},[36,7962,1481],{"class":400},[14,7964,7965,7966,1281,7968,7970],{},"So ",[33,7967,1243],{},[33,7969,5403],{}," can be equal in value, but they are different types.",[14,7972,7973],{},"Python can also do math with ints and floats together:",[26,7975,7977],{"className":28,"code":7976,"language":30,"meta":31,"style":31},"total = 2 + 3.5\nprint(total)\nprint(type(total))\n",[33,7978,7979,7993,8004],{"__ignoreMap":31},[36,7980,7981,7984,7986,7988,7990],{"class":38,"line":39},[36,7982,7983],{"class":42},"total ",[36,7985,47],{"class":46},[36,7987,61],{"class":54},[36,7989,3106],{"class":46},[36,7991,7992],{"class":54}," 3.5\n",[36,7994,7995,7997,7999,8002],{"class":38,"line":72},[36,7996,76],{"class":75},[36,7998,79],{"class":50},[36,8000,8001],{"class":82},"total",[36,8003,86],{"class":50},[36,8005,8006,8008,8010,8012,8014,8016],{"class":38,"line":89},[36,8007,76],{"class":75},[36,8009,79],{"class":50},[36,8011,97],{"class":96},[36,8013,79],{"class":50},[36,8015,8001],{"class":82},[36,8017,104],{"class":50},[14,8019,107],{},[26,8021,8023],{"className":28,"code":8022,"language":30,"meta":31,"style":31},"5.5\n\u003Cclass 'float'>\n",[33,8024,8025,8030],{"__ignoreMap":31},[36,8026,8027],{"class":38,"line":39},[36,8028,8029],{"class":54},"5.5\n",[36,8031,8032,8034,8036,8038,8040,8042],{"class":38,"line":72},[36,8033,134],{"class":46},[36,8035,138],{"class":137},[36,8037,142],{"class":141},[36,8039,4740],{"class":145},[36,8041,149],{"class":141},[36,8043,152],{"class":46},[21,8045,426],{"id":425},[14,8047,8048],{},"Here is a very small example of a float stored in a variable:",[26,8050,8052],{"className":28,"code":8051,"language":30,"meta":31,"style":31},"temperature = 21.5\n\nprint(temperature)\nprint(type(temperature))\n",[33,8053,8054,8064,8068,8079],{"__ignoreMap":31},[36,8055,8056,8059,8061],{"class":38,"line":39},[36,8057,8058],{"class":42},"temperature ",[36,8060,47],{"class":46},[36,8062,8063],{"class":54}," 21.5\n",[36,8065,8066],{"class":38,"line":72},[36,8067,476],{"emptyLinePlaceholder":475},[36,8069,8070,8072,8074,8077],{"class":38,"line":89},[36,8071,76],{"class":75},[36,8073,79],{"class":50},[36,8075,8076],{"class":82},"temperature",[36,8078,86],{"class":50},[36,8080,8081,8083,8085,8087,8089,8091],{"class":38,"line":496},[36,8082,76],{"class":75},[36,8084,79],{"class":50},[36,8086,97],{"class":96},[36,8088,79],{"class":50},[36,8090,8076],{"class":82},[36,8092,104],{"class":50},[14,8094,107],{},[26,8096,8098],{"className":28,"code":8097,"language":30,"meta":31,"style":31},"21.5\n\u003Cclass 'float'>\n",[33,8099,8100,8105],{"__ignoreMap":31},[36,8101,8102],{"class":38,"line":39},[36,8103,8104],{"class":54},"21.5\n",[36,8106,8107,8109,8111,8113,8115,8117],{"class":38,"line":72},[36,8108,134],{"class":46},[36,8110,138],{"class":137},[36,8112,142],{"class":141},[36,8114,4740],{"class":145},[36,8116,149],{"class":141},[36,8118,152],{"class":46},[14,8120,8121],{},"What this does:",[157,8123,8124,8130,8136],{},[160,8125,8126,8129],{},[33,8127,8128],{},"temperature = 21.5"," stores a float in a variable",[160,8131,8132,8135],{},[33,8133,8134],{},"print(temperature)"," shows the value",[160,8137,8138,8141,8142],{},[33,8139,8140],{},"print(type(temperature))"," shows that the type is ",[33,8143,4740],{},[21,8145,8147],{"id":8146},"important-beginner-note-about-precision","Important beginner note about precision",[14,8149,8150],{},"Floats are very useful, but they are not always perfectly exact.",[14,8152,8153],{},"Some decimal values cannot be stored exactly in binary form. Because of that, you may sometimes see small unexpected results.",[14,8155,197],{},[26,8157,8159],{"className":28,"code":8158,"language":30,"meta":31,"style":31},"print(0.1 + 0.2)\n",[33,8160,8161],{"__ignoreMap":31},[36,8162,8163,8165,8167,8170,8172,8175],{"class":38,"line":39},[36,8164,76],{"class":75},[36,8166,79],{"class":50},[36,8168,8169],{"class":54},"0.1",[36,8171,3106],{"class":46},[36,8173,8174],{"class":54}," 0.2",[36,8176,86],{"class":50},[14,8178,107],{},[26,8180,8182],{"className":28,"code":8181,"language":30,"meta":31,"style":31},"0.30000000000000004\n",[33,8183,8184],{"__ignoreMap":31},[36,8185,8186],{"class":38,"line":39},[36,8187,8181],{"class":54},[14,8189,8190,8191,8194],{},"This does ",[331,8192,8193],{},"not"," mean Python is broken. It means floats have small precision limits.",[14,8196,8197],{},"For beginner code, a practical fix is often to format or round the result:",[26,8199,8201],{"className":28,"code":8200,"language":30,"meta":31,"style":31},"print(round(0.1 + 0.2, 2))\n",[33,8202,8203],{"__ignoreMap":31},[36,8204,8205,8207,8209,8212,8214,8216,8218,8220,8222,8224],{"class":38,"line":39},[36,8206,76],{"class":75},[36,8208,79],{"class":50},[36,8210,8211],{"class":75},"round",[36,8213,79],{"class":50},[36,8215,8169],{"class":54},[36,8217,3106],{"class":46},[36,8219,8174],{"class":54},[36,8221,58],{"class":50},[36,8223,61],{"class":54},[36,8225,104],{"class":50},[14,8227,107],{},[26,8229,8231],{"className":28,"code":8230,"language":30,"meta":31,"style":31},"0.3\n",[33,8232,8233],{"__ignoreMap":31},[36,8234,8235],{"class":38,"line":39},[36,8236,8230],{"class":54},[14,8238,8239,8240,351],{},"You can learn more on the ",[347,8241,8243,8246],{"href":8242},"\u002Freference\u002Fpython-round-function-explained\u002F",[33,8244,8245],{},"round()"," function",[21,8248,8250],{"id":8249},"converting-values-to-float","Converting values to float",[14,8252,8253,8254,8259],{},"You can use ",[347,8255,8257],{"href":8256},"\u002Freference\u002Fpython-float-function-explained\u002F",[33,8258,7692],{}," to convert other values into floats.",[14,8261,694],{},[26,8263,8265],{"className":28,"code":8264,"language":30,"meta":31,"style":31},"print(float(\"3.5\"))\nprint(float(7))\n",[33,8266,8267,8286],{"__ignoreMap":31},[36,8268,8269,8271,8273,8275,8277,8279,8282,8284],{"class":38,"line":39},[36,8270,76],{"class":75},[36,8272,79],{"class":50},[36,8274,4740],{"class":96},[36,8276,79],{"class":50},[36,8278,214],{"class":141},[36,8280,8281],{"class":145},"3.5",[36,8283,214],{"class":141},[36,8285,104],{"class":50},[36,8287,8288,8290,8292,8294,8296,8299],{"class":38,"line":72},[36,8289,76],{"class":75},[36,8291,79],{"class":50},[36,8293,4740],{"class":96},[36,8295,79],{"class":50},[36,8297,8298],{"class":54},"7",[36,8300,104],{"class":50},[14,8302,107],{},[26,8304,8306],{"className":28,"code":8305,"language":30,"meta":31,"style":31},"3.5\n7.0\n",[33,8307,8308,8312],{"__ignoreMap":31},[36,8309,8310],{"class":38,"line":39},[36,8311,7791],{"class":54},[36,8313,8314],{"class":38,"line":72},[36,8315,8316],{"class":54},"7.0\n",[14,8318,8319],{},"This is common when working with user input:",[26,8321,8323],{"className":28,"code":8322,"language":30,"meta":31,"style":31},"user_text = \"8.25\"\nnumber = float(user_text)\n\nprint(number)\nprint(type(number))\n",[33,8324,8325,8339,8356,8360,8371],{"__ignoreMap":31},[36,8326,8327,8330,8332,8334,8337],{"class":38,"line":39},[36,8328,8329],{"class":42},"user_text ",[36,8331,47],{"class":46},[36,8333,224],{"class":141},[36,8335,8336],{"class":145},"8.25",[36,8338,1954],{"class":141},[36,8340,8341,8344,8346,8349,8351,8354],{"class":38,"line":72},[36,8342,8343],{"class":42},"number ",[36,8345,47],{"class":46},[36,8347,8348],{"class":96}," float",[36,8350,79],{"class":50},[36,8352,8353],{"class":82},"user_text",[36,8355,86],{"class":50},[36,8357,8358],{"class":38,"line":89},[36,8359,476],{"emptyLinePlaceholder":475},[36,8361,8362,8364,8366,8369],{"class":38,"line":496},[36,8363,76],{"class":75},[36,8365,79],{"class":50},[36,8367,8368],{"class":82},"number",[36,8370,86],{"class":50},[36,8372,8373,8375,8377,8379,8381,8383],{"class":38,"line":501},[36,8374,76],{"class":75},[36,8376,79],{"class":50},[36,8378,97],{"class":96},[36,8380,79],{"class":50},[36,8382,8368],{"class":82},[36,8384,104],{"class":50},[14,8386,107],{},[26,8388,8390],{"className":28,"code":8389,"language":30,"meta":31,"style":31},"8.25\n\u003Cclass 'float'>\n",[33,8391,8392,8397],{"__ignoreMap":31},[36,8393,8394],{"class":38,"line":39},[36,8395,8396],{"class":54},"8.25\n",[36,8398,8399,8401,8403,8405,8407,8409],{"class":38,"line":72},[36,8400,134],{"class":46},[36,8402,138],{"class":137},[36,8404,142],{"class":141},[36,8406,4740],{"class":145},[36,8408,149],{"class":141},[36,8410,152],{"class":46},[14,8412,8413,8414,822],{},"If the text is not a valid number, Python raises a ",[33,8415,8416],{},"ValueError",[26,8418,8420],{"className":28,"code":8419,"language":30,"meta":31,"style":31},"float(\"hello\")\n",[33,8421,8422],{"__ignoreMap":31},[36,8423,8424,8426,8428,8430,8432,8434],{"class":38,"line":39},[36,8425,4740],{"class":96},[36,8427,79],{"class":50},[36,8429,214],{"class":141},[36,8431,2671],{"class":145},[36,8433,214],{"class":141},[36,8435,86],{"class":50},[14,8437,8438],{},"Error:",[26,8440,8442],{"className":28,"code":8441,"language":30,"meta":31,"style":31},"ValueError: could not convert string to float: 'hello'\n",[33,8443,8444],{"__ignoreMap":31},[36,8445,8446,8448,8450,8453,8455,8458,8460,8462,8464,8466],{"class":38,"line":39},[36,8447,8416],{"class":96},[36,8449,822],{"class":50},[36,8451,8452],{"class":42}," could ",[36,8454,8193],{"class":46},[36,8456,8457],{"class":42}," convert string to ",[36,8459,4740],{"class":96},[36,8461,822],{"class":50},[36,8463,142],{"class":141},[36,8465,2671],{"class":145},[36,8467,8468],{"class":141},"'\n",[14,8470,8471,8472,1180,8476,351],{},"If you need help with that, see ",[347,8473,8475],{"href":8474},"\u002Ferrors\u002Fvalueerror-could-not-convert-string-to-float-fix\u002F","ValueError: could not convert string to float",[347,8477,8479],{"href":8478},"\u002Fhow-to\u002Fhow-to-convert-string-to-float-in-python\u002F","how to convert a string to float in Python",[21,8481,905],{"id":904},[14,8483,8484],{},"Beginners often run into these problems:",[157,8486,8487,8492,8500,8503],{},[160,8488,8489,8490],{},"Thinking every number is an ",[33,8491,2466],{},[160,8493,8494,8495,1281,8497,8499],{},"Assuming ",[33,8496,1243],{},[33,8498,5403],{}," are the same type",[160,8501,8502],{},"Expecting decimal math to always be perfectly exact",[160,8504,8505,8506],{},"Trying to convert non-number text with ",[33,8507,7692],{},[14,8509,5792],{},[26,8511,8513],{"className":28,"code":8512,"language":30,"meta":31,"style":31},"print(type(3.14))\nprint(type(5.0))\nprint(float(\"3.5\"))\nprint(0.1 + 0.2)\nprint(round(0.1 + 0.2, 2))\n",[33,8514,8515,8529,8543,8561,8575],{"__ignoreMap":31},[36,8516,8517,8519,8521,8523,8525,8527],{"class":38,"line":39},[36,8518,76],{"class":75},[36,8520,79],{"class":50},[36,8522,97],{"class":96},[36,8524,79],{"class":50},[36,8526,4871],{"class":54},[36,8528,104],{"class":50},[36,8530,8531,8533,8535,8537,8539,8541],{"class":38,"line":72},[36,8532,76],{"class":75},[36,8534,79],{"class":50},[36,8536,97],{"class":96},[36,8538,79],{"class":50},[36,8540,5403],{"class":54},[36,8542,104],{"class":50},[36,8544,8545,8547,8549,8551,8553,8555,8557,8559],{"class":38,"line":89},[36,8546,76],{"class":75},[36,8548,79],{"class":50},[36,8550,4740],{"class":96},[36,8552,79],{"class":50},[36,8554,214],{"class":141},[36,8556,8281],{"class":145},[36,8558,214],{"class":141},[36,8560,104],{"class":50},[36,8562,8563,8565,8567,8569,8571,8573],{"class":38,"line":496},[36,8564,76],{"class":75},[36,8566,79],{"class":50},[36,8568,8169],{"class":54},[36,8570,3106],{"class":46},[36,8572,8174],{"class":54},[36,8574,86],{"class":50},[36,8576,8577,8579,8581,8583,8585,8587,8589,8591,8593,8595],{"class":38,"line":501},[36,8578,76],{"class":75},[36,8580,79],{"class":50},[36,8582,8211],{"class":75},[36,8584,79],{"class":50},[36,8586,8169],{"class":54},[36,8588,3106],{"class":46},[36,8590,8174],{"class":54},[36,8592,58],{"class":50},[36,8594,61],{"class":54},[36,8596,104],{"class":50},[14,8598,8599],{},"These small tests help you quickly see:",[157,8601,8602,8605,8608],{},[160,8603,8604],{},"whether a value is a float",[160,8606,8607],{},"how conversion works",[160,8609,8610],{},"how precision can affect output",[21,8612,1063],{"id":1062},[1065,8614,8616],{"id":8615},"is-5-a-float-in-python","Is 5 a float in Python?",[14,8618,2836,8619,8621,8622,8624,8625,8627,8628,351],{},[33,8620,1243],{}," is an ",[33,8623,2466],{},". ",[33,8626,5403],{}," is a ",[33,8629,4740],{},[1065,8631,8633],{"id":8632},"is-50-the-same-as-5","Is 5.0 the same as 5?",[14,8635,8636,8637,1281,8639,351],{},"They can compare as equal in value, but they are different types: ",[33,8638,4740],{},[33,8640,2466],{},[1065,8642,8644],{"id":8643},"how-do-i-create-a-float-in-python","How do I create a float in Python?",[14,8646,8647,8648,8650,8651,351],{},"Write a number with a decimal point, like ",[33,8649,7418],{},", or convert a value with ",[33,8652,7692],{},[1065,8654,8656],{"id":8655},"why-does-01-02-give-a-strange-result","Why does 0.1 + 0.2 give a strange result?",[14,8658,8659],{},"Floats cannot store some decimal values exactly, so small precision differences can appear.",[1065,8661,8663],{"id":8662},"can-i-convert-a-string-to-a-float","Can I convert a string to a float?",[14,8665,8666,8667,8670,8671,351],{},"Yes. Use ",[33,8668,8669],{},"float(\"3.14\")",". If the text is not a valid number, Python raises a ",[33,8672,8416],{},[21,8674,1105],{"id":1104},[157,8676,8677,8682,8686,8692,8697,8703,8709],{},[160,8678,8679],{},[347,8680,8681],{"href":5642},"What is an integer in Python?",[160,8683,8684],{},[347,8685,7808],{"href":7807},[160,8687,8688],{},[347,8689,1665,8690,2816],{"href":8256},[33,8691,7692],{},[160,8693,8694],{},[347,8695,8696],{"href":8478},"How to convert string to float in Python",[160,8698,8699],{},[347,8700,1665,8701,2816],{"href":5265},[33,8702,1327],{},[160,8704,8705],{},[347,8706,1665,8707,2816],{"href":8242},[33,8708,8245],{},[160,8710,8711],{},[347,8712,8475],{"href":8474},[1137,8714,8715],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":8717},[8718,8719,8720,8721,8722,8723,8724,8725,8726,8733],{"id":7433,"depth":72,"text":7434},{"id":7473,"depth":72,"text":7474},{"id":7671,"depth":72,"text":7672},{"id":7811,"depth":72,"text":7812},{"id":425,"depth":72,"text":426},{"id":8146,"depth":72,"text":8147},{"id":8249,"depth":72,"text":8250},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":8727},[8728,8729,8730,8731,8732],{"id":8615,"depth":89,"text":8616},{"id":8632,"depth":89,"text":8633},{"id":8643,"depth":89,"text":8644},{"id":8655,"depth":89,"text":8656},{"id":8662,"depth":89,"text":8663},{"id":1104,"depth":72,"text":1105},"Master what is a float in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-float-in-python",{"title":7403,"description":8734},"glossary\u002Fwhat-is-a-float-in-python","dCyJxFDCAXd3R-MHnc95fAIXrH_sURhodDlK2vHQeKs",{"id":8741,"title":8742,"body":8743,"description":9805,"extension":1159,"meta":9806,"navigation":475,"path":9807,"seo":9808,"stem":9809,"__hash__":9810},"content\u002Fglossary\u002Fwhat-is-a-function-in-python.md","What Is a Function in Python?",{"type":7,"value":8744,"toc":9786},[8745,8748,8751,8754,8756,8759,8773,8776,8817,8819,8828,8831,8847,8857,8861,8864,8878,8881,8885,8888,8904,8906,8946,8948,8964,8967,8987,8994,8998,9001,9020,9022,9092,9094,9117,9119,9135,9138,9142,9148,9172,9174,9242,9244,9252,9254,9273,9276,9344,9346,9359,9366,9373,9377,9380,9396,9398,9431,9433,9442,9447,9450,9466,9474,9478,9481,9495,9502,9504,9507,9529,9532,9559,9562,9598,9601,9657,9660,9697,9704,9706,9710,9713,9717,9720,9724,9727,9733,9739,9743,9746,9748,9780,9783],[10,8746,8742],{"id":8747},"what-is-a-function-in-python",[14,8749,8750],{},"A function in Python is a reusable block of code that does a specific task. You can run it when needed, and it can optionally send a value back.",[14,8752,8753],{},"Functions are one of the main ways to organize code. They help you group related steps together and avoid writing the same code again and again.",[21,8755,6101],{"id":6100},[14,8757,8758],{},"A function is:",[157,8760,8761,8764,8767,8770],{},[160,8762,8763],{},"A named block of code",[160,8765,8766],{},"A way to group steps that belong together",[160,8768,8769],{},"Something you can run more than once",[160,8771,8772],{},"A tool for avoiding repeated code",[14,8774,8775],{},"Here is a very simple example:",[26,8777,8779],{"className":28,"code":8778,"language":30,"meta":31,"style":31},"def say_hello():\n    print(\"Hello\")\n\nsay_hello()\n",[33,8780,8781,8791,8806,8810],{"__ignoreMap":31},[36,8782,8783,8786,8789],{"class":38,"line":39},[36,8784,8785],{"class":137},"def",[36,8787,8788],{"class":3082}," say_hello",[36,8790,4244],{"class":50},[36,8792,8793,8795,8797,8799,8802,8804],{"class":38,"line":72},[36,8794,1735],{"class":75},[36,8796,79],{"class":50},[36,8798,214],{"class":141},[36,8800,8801],{"class":145},"Hello",[36,8803,214],{"class":141},[36,8805,86],{"class":50},[36,8807,8808],{"class":38,"line":89},[36,8809,476],{"emptyLinePlaceholder":475},[36,8811,8812,8815],{"class":38,"line":496},[36,8813,8814],{"class":82},"say_hello",[36,8816,3170],{"class":50},[14,8818,107],{},[26,8820,8822],{"className":28,"code":8821,"language":30,"meta":31,"style":31},"Hello\n",[33,8823,8824],{"__ignoreMap":31},[36,8825,8826],{"class":38,"line":39},[36,8827,8821],{"class":42},[14,8829,8830],{},"This example shows the two main parts:",[157,8832,8833,8841],{},[160,8834,8835,8838,8839],{},[331,8836,8837],{},"Defining"," a function with ",[33,8840,8785],{},[160,8842,8843,8846],{},[331,8844,8845],{},"Calling"," the function by writing its name with parentheses",[14,8848,8849,8850,8853,8854,351],{},"The function does not run when Python first sees ",[33,8851,8852],{},"def say_hello():",". It runs only when you call ",[33,8855,8856],{},"say_hello()",[21,8858,8860],{"id":8859},"why-functions-are-useful","Why functions are useful",[14,8862,8863],{},"Functions are useful because they help you:",[157,8865,8866,8869,8872,8875],{},[160,8867,8868],{},"Make code easier to read",[160,8870,8871],{},"Reduce repetition",[160,8873,8874],{},"Break large problems into smaller parts",[160,8876,8877],{},"Reuse the same logic in different places",[14,8879,8880],{},"For example, without a function, you might repeat the same lines several times. With a function, you write the code once and call it whenever you need it.",[21,8882,8884],{"id":8883},"how-a-function-works","How a function works",[14,8886,8887],{},"A basic function works like this:",[157,8889,8890,8895,8898,8901],{},[160,8891,8892,8893],{},"You define a function with ",[33,8894,8785],{},[160,8896,8897],{},"You give the function a name",[160,8899,8900],{},"You put the code for that task inside the function",[160,8902,8903],{},"The indented code runs when the function is called",[14,8905,197],{},[26,8907,8909],{"className":28,"code":8908,"language":30,"meta":31,"style":31},"def show_message():\n    print(\"This is inside the function\")\n\nshow_message()\n",[33,8910,8911,8920,8935,8939],{"__ignoreMap":31},[36,8912,8913,8915,8918],{"class":38,"line":39},[36,8914,8785],{"class":137},[36,8916,8917],{"class":3082}," show_message",[36,8919,4244],{"class":50},[36,8921,8922,8924,8926,8928,8931,8933],{"class":38,"line":72},[36,8923,1735],{"class":75},[36,8925,79],{"class":50},[36,8927,214],{"class":141},[36,8929,8930],{"class":145},"This is inside the function",[36,8932,214],{"class":141},[36,8934,86],{"class":50},[36,8936,8937],{"class":38,"line":89},[36,8938,476],{"emptyLinePlaceholder":475},[36,8940,8941,8944],{"class":38,"line":496},[36,8942,8943],{"class":82},"show_message",[36,8945,3170],{"class":50},[14,8947,107],{},[26,8949,8951],{"className":28,"code":8950,"language":30,"meta":31,"style":31},"This is inside the function\n",[33,8952,8953],{"__ignoreMap":31},[36,8954,8955,8958,8961],{"class":38,"line":39},[36,8956,8957],{"class":42},"This ",[36,8959,8960],{"class":46},"is",[36,8962,8963],{"class":42}," inside the function\n",[14,8965,8966],{},"Key parts:",[157,8968,8969,8974,8979,8984],{},[160,8970,8971,8973],{},[33,8972,8785],{}," starts the function definition",[160,8975,8976,8978],{},[33,8977,8943],{}," is the function name",[160,8980,8981,8983],{},[33,8982,932],{}," are used in both the definition and the call",[160,8985,8986],{},"The indented line is the function body",[14,8988,8989,8990,351],{},"If you want a fuller introduction, see ",[347,8991,8993],{"href":8992},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions explained",[21,8995,8997],{"id":8996},"functions-can-take-input","Functions can take input",[14,8999,9000],{},"Functions can work with different data by taking input.",[157,9002,9003,9010,9017],{},[160,9004,9005,9006],{},"Inputs in the function definition are called ",[347,9007,9009],{"href":9008},"\u002Fglossary\u002Fwhat-is-a-parameter-in-python\u002F","parameters",[160,9011,9012,9013],{},"Values you pass when calling the function are called ",[347,9014,9016],{"href":9015},"\u002Fglossary\u002Fwhat-is-an-argument-in-python\u002F","arguments",[160,9018,9019],{},"This lets one function work with different values",[14,9021,197],{},[26,9023,9025],{"className":28,"code":9024,"language":30,"meta":31,"style":31},"def greet(name):\n    print(\"Hello,\", name)\n\ngreet(\"Maya\")\ngreet(\"Leo\")\n",[33,9026,9027,9040,9059,9063,9078],{"__ignoreMap":31},[36,9028,9029,9031,9034,9036,9038],{"class":38,"line":39},[36,9030,8785],{"class":137},[36,9032,9033],{"class":3082}," greet",[36,9035,79],{"class":50},[36,9037,817],{"class":3050},[36,9039,3054],{"class":50},[36,9041,9042,9044,9046,9048,9051,9053,9055,9057],{"class":38,"line":72},[36,9043,1735],{"class":75},[36,9045,79],{"class":50},[36,9047,214],{"class":141},[36,9049,9050],{"class":145},"Hello,",[36,9052,214],{"class":141},[36,9054,58],{"class":50},[36,9056,3051],{"class":82},[36,9058,86],{"class":50},[36,9060,9061],{"class":38,"line":89},[36,9062,476],{"emptyLinePlaceholder":475},[36,9064,9065,9068,9070,9072,9074,9076],{"class":38,"line":496},[36,9066,9067],{"class":82},"greet",[36,9069,79],{"class":50},[36,9071,214],{"class":141},[36,9073,293],{"class":145},[36,9075,214],{"class":141},[36,9077,86],{"class":50},[36,9079,9080,9082,9084,9086,9088,9090],{"class":38,"line":501},[36,9081,9067],{"class":82},[36,9083,79],{"class":50},[36,9085,214],{"class":141},[36,9087,302],{"class":145},[36,9089,214],{"class":141},[36,9091,86],{"class":50},[14,9093,107],{},[26,9095,9097],{"className":28,"code":9096,"language":30,"meta":31,"style":31},"Hello, Maya\nHello, Leo\n",[33,9098,9099,9108],{"__ignoreMap":31},[36,9100,9101,9103,9105],{"class":38,"line":39},[36,9102,8801],{"class":42},[36,9104,58],{"class":50},[36,9106,9107],{"class":42}," Maya\n",[36,9109,9110,9112,9114],{"class":38,"line":72},[36,9111,8801],{"class":42},[36,9113,58],{"class":50},[36,9115,9116],{"class":42}," Leo\n",[14,9118,4063],{},[157,9120,9121,9126],{},[160,9122,9123,9125],{},[33,9124,817],{}," is a parameter",[160,9127,9128,1281,9131,9134],{},[33,9129,9130],{},"\"Maya\"",[33,9132,9133],{},"\"Leo\""," are arguments",[14,9136,9137],{},"This is useful because you do not need a separate function for every person or value.",[21,9139,9141],{"id":9140},"functions-can-return-output","Functions can return output",[14,9143,9144,9145,351],{},"A function can also send a value back using ",[33,9146,9147],{},"return",[157,9149,9150,9155,9158,9163],{},[160,9151,9152,9154],{},[33,9153,9147],{}," gives a value back to the program",[160,9156,9157],{},"The returned value can be stored in a variable",[160,9159,9160,9161],{},"Not every function needs ",[33,9162,9147],{},[160,9164,9165,9168,9169,9171],{},[33,9166,9167],{},"print()"," shows something on the screen, but ",[33,9170,9147],{}," gives a value back to your code",[14,9173,197],{},[26,9175,9177],{"className":28,"code":9176,"language":30,"meta":31,"style":31},"def add_numbers(a, b):\n    return a + b\n\nresult = add_numbers(2, 3)\nprint(result)\n",[33,9178,9179,9197,9210,9214,9232],{"__ignoreMap":31},[36,9180,9181,9183,9186,9188,9190,9192,9195],{"class":38,"line":39},[36,9182,8785],{"class":137},[36,9184,9185],{"class":3082}," add_numbers",[36,9187,79],{"class":50},[36,9189,347],{"class":3050},[36,9191,58],{"class":50},[36,9193,9194],{"class":3050}," b",[36,9196,3054],{"class":50},[36,9198,9199,9202,9205,9207],{"class":38,"line":72},[36,9200,9201],{"class":1718},"    return",[36,9203,9204],{"class":42}," a ",[36,9206,4763],{"class":46},[36,9208,9209],{"class":42}," b\n",[36,9211,9212],{"class":38,"line":89},[36,9213,476],{"emptyLinePlaceholder":475},[36,9215,9216,9218,9220,9222,9224,9226,9228,9230],{"class":38,"line":496},[36,9217,7723],{"class":42},[36,9219,47],{"class":46},[36,9221,9185],{"class":82},[36,9223,79],{"class":50},[36,9225,1543],{"class":54},[36,9227,58],{"class":50},[36,9229,66],{"class":54},[36,9231,86],{"class":50},[36,9233,9234,9236,9238,9240],{"class":38,"line":501},[36,9235,76],{"class":75},[36,9237,79],{"class":50},[36,9239,7767],{"class":82},[36,9241,86],{"class":50},[14,9243,107],{},[26,9245,9246],{"className":28,"code":4825,"language":30,"meta":31,"style":31},[33,9247,9248],{"__ignoreMap":31},[36,9249,9250],{"class":38,"line":39},[36,9251,4825],{"class":54},[14,9253,6187],{},[157,9255,9256,9262,9268],{},[160,9257,9258,9259],{},"The function calculates ",[33,9260,9261],{},"a + b",[160,9263,9264,9267],{},[33,9265,9266],{},"return a + b"," sends the result back",[160,9269,9270,9271],{},"That result is stored in ",[33,9272,7767],{},[14,9274,9275],{},"This is different from:",[26,9277,9279],{"className":28,"code":9278,"language":30,"meta":31,"style":31},"def add_and_print(a, b):\n    print(a + b)\n\nvalue = add_and_print(2, 3)\nprint(value)\n",[33,9280,9281,9298,9312,9316,9334],{"__ignoreMap":31},[36,9282,9283,9285,9288,9290,9292,9294,9296],{"class":38,"line":39},[36,9284,8785],{"class":137},[36,9286,9287],{"class":3082}," add_and_print",[36,9289,79],{"class":50},[36,9291,347],{"class":3050},[36,9293,58],{"class":50},[36,9295,9194],{"class":3050},[36,9297,3054],{"class":50},[36,9299,9300,9302,9304,9306,9308,9310],{"class":38,"line":72},[36,9301,1735],{"class":75},[36,9303,79],{"class":50},[36,9305,3316],{"class":82},[36,9307,4763],{"class":46},[36,9309,9194],{"class":82},[36,9311,86],{"class":50},[36,9313,9314],{"class":38,"line":89},[36,9315,476],{"emptyLinePlaceholder":475},[36,9317,9318,9320,9322,9324,9326,9328,9330,9332],{"class":38,"line":496},[36,9319,2664],{"class":42},[36,9321,47],{"class":46},[36,9323,9287],{"class":82},[36,9325,79],{"class":50},[36,9327,1543],{"class":54},[36,9329,58],{"class":50},[36,9331,66],{"class":54},[36,9333,86],{"class":50},[36,9335,9336,9338,9340,9342],{"class":38,"line":501},[36,9337,76],{"class":75},[36,9339,79],{"class":50},[36,9341,2686],{"class":82},[36,9343,86],{"class":50},[14,9345,107],{},[26,9347,9349],{"className":28,"code":9348,"language":30,"meta":31,"style":31},"5\nNone\n",[33,9350,9351,9355],{"__ignoreMap":31},[36,9352,9353],{"class":38,"line":39},[36,9354,4825],{"class":54},[36,9356,9357],{"class":38,"line":72},[36,9358,7151],{"class":400},[14,9360,9361,9362,9365],{},"Why does ",[33,9363,9364],{},"None"," appear? Because the function printed the result, but it did not return one.",[14,9367,9368,9369,351],{},"If you want to learn this idea in more detail, see ",[347,9370,9372],{"href":9371},"\u002Fglossary\u002Fwhat-is-a-return-value-in-python\u002F","what is a return value in Python",[21,9374,9376],{"id":9375},"built-in-functions-and-your-own-functions","Built-in functions and your own functions",[14,9378,9379],{},"Python already includes many built-in functions, such as:",[157,9381,9382,9386,9391],{},[160,9383,9384],{},[33,9385,9167],{},[160,9387,9388],{},[33,9389,9390],{},"len()",[160,9392,9393],{},[33,9394,9395],{},"input()",[14,9397,197],{},[26,9399,9401],{"className":28,"code":9400,"language":30,"meta":31,"style":31},"text = \"Python\"\nprint(len(text))\n",[33,9402,9403,9417],{"__ignoreMap":31},[36,9404,9405,9408,9410,9412,9415],{"class":38,"line":39},[36,9406,9407],{"class":42},"text ",[36,9409,47],{"class":46},[36,9411,224],{"class":141},[36,9413,9414],{"class":145},"Python",[36,9416,1954],{"class":141},[36,9418,9419,9421,9423,9425,9427,9429],{"class":38,"line":72},[36,9420,76],{"class":75},[36,9422,79],{"class":50},[36,9424,999],{"class":75},[36,9426,79],{"class":50},[36,9428,4695],{"class":82},[36,9430,104],{"class":50},[14,9432,107],{},[26,9434,9436],{"className":28,"code":9435,"language":30,"meta":31,"style":31},"6\n",[33,9437,9438],{"__ignoreMap":31},[36,9439,9440],{"class":38,"line":39},[36,9441,9435],{"class":54},[14,9443,9444,9445,351],{},"You can also create your own functions with ",[33,9446,8785],{},[14,9448,9449],{},"Both built-in functions and your own functions are usually called with parentheses. For example:",[157,9451,9452,9457,9462],{},[160,9453,9454],{},[33,9455,9456],{},"print(\"Hi\")",[160,9458,9459],{},[33,9460,9461],{},"len(\"cat\")",[160,9463,9464],{},[33,9465,8856],{},[14,9467,9468,9469,351],{},"If you want to understand one common built-in function better, see ",[347,9470,1665,9472,2816],{"href":9471},"\u002Freference\u002Fpython-print-function-explained\u002F",[33,9473,9167],{},[21,9475,9477],{"id":9476},"what-this-page-does-not-cover-in-depth","What this page does not cover in depth",[14,9479,9480],{},"This page explains the basic idea of a function. It does not go deeply into:",[157,9482,9483,9486,9489,9492],{},[160,9484,9485],{},"Detailed parameter rules",[160,9487,9488],{},"Default arguments and keyword arguments",[160,9490,9491],{},"Lambda functions",[160,9493,9494],{},"Advanced function concepts",[14,9496,9497,9498,351],{},"If your next goal is to make one yourself, see ",[347,9499,9501],{"href":9500},"\u002Fhow-to\u002Fhow-to-create-a-simple-function-in-python\u002F","how to create a simple function in Python",[21,9503,905],{"id":904},[14,9505,9506],{},"Beginners often run into these problems when learning functions:",[157,9508,9509,9512,9515,9523,9526],{},[160,9510,9511],{},"Thinking a function runs as soon as it is defined",[160,9513,9514],{},"Forgetting to call the function after defining it",[160,9516,9517,9518,9520,9521],{},"Confusing ",[33,9519,9167],{}," with ",[33,9522,9147],{},[160,9524,9525],{},"Forgetting the parentheses when calling a function",[160,9527,9528],{},"Using wrong indentation inside the function body",[14,9530,9531],{},"Example of a function that is defined but not called:",[26,9533,9535],{"className":28,"code":9534,"language":30,"meta":31,"style":31},"def say_hello():\n    print(\"Hello\")\n",[33,9536,9537,9545],{"__ignoreMap":31},[36,9538,9539,9541,9543],{"class":38,"line":39},[36,9540,8785],{"class":137},[36,9542,8788],{"class":3082},[36,9544,4244],{"class":50},[36,9546,9547,9549,9551,9553,9555,9557],{"class":38,"line":72},[36,9548,1735],{"class":75},[36,9550,79],{"class":50},[36,9552,214],{"class":141},[36,9554,8801],{"class":145},[36,9556,214],{"class":141},[36,9558,86],{"class":50},[14,9560,9561],{},"This code creates the function, but it does not produce output yet. To run it, you must call it:",[26,9563,9564],{"className":28,"code":8778,"language":30,"meta":31,"style":31},[33,9565,9566,9574,9588,9592],{"__ignoreMap":31},[36,9567,9568,9570,9572],{"class":38,"line":39},[36,9569,8785],{"class":137},[36,9571,8788],{"class":3082},[36,9573,4244],{"class":50},[36,9575,9576,9578,9580,9582,9584,9586],{"class":38,"line":72},[36,9577,1735],{"class":75},[36,9579,79],{"class":50},[36,9581,214],{"class":141},[36,9583,8801],{"class":145},[36,9585,214],{"class":141},[36,9587,86],{"class":50},[36,9589,9590],{"class":38,"line":89},[36,9591,476],{"emptyLinePlaceholder":475},[36,9593,9594,9596],{"class":38,"line":496},[36,9595,8814],{"class":82},[36,9597,3170],{"class":50},[14,9599,9600],{},"You can also use a few quick checks while learning:",[26,9602,9604],{"className":28,"code":9603,"language":30,"meta":31,"style":31},"print(my_function)\nprint(my_function())\nhelp(print)\ntype(print)\ntype(my_function)\n",[33,9605,9606,9617,9627,9637,9647],{"__ignoreMap":31},[36,9607,9608,9610,9612,9615],{"class":38,"line":39},[36,9609,76],{"class":75},[36,9611,79],{"class":50},[36,9613,9614],{"class":82},"my_function",[36,9616,86],{"class":50},[36,9618,9619,9621,9623,9625],{"class":38,"line":72},[36,9620,76],{"class":75},[36,9622,79],{"class":50},[36,9624,9614],{"class":82},[36,9626,4387],{"class":50},[36,9628,9629,9631,9633,9635],{"class":38,"line":89},[36,9630,4392],{"class":75},[36,9632,79],{"class":50},[36,9634,76],{"class":75},[36,9636,86],{"class":50},[36,9638,9639,9641,9643,9645],{"class":38,"line":496},[36,9640,97],{"class":96},[36,9642,79],{"class":50},[36,9644,76],{"class":75},[36,9646,86],{"class":50},[36,9648,9649,9651,9653,9655],{"class":38,"line":501},[36,9650,97],{"class":96},[36,9652,79],{"class":50},[36,9654,9614],{"class":82},[36,9656,86],{"class":50},[14,9658,9659],{},"What these show:",[157,9661,9662,9668,9674,9682,9691],{},[160,9663,9664,9667],{},[33,9665,9666],{},"print(my_function)"," shows that the function exists",[160,9669,9670,9673],{},[33,9671,9672],{},"print(my_function())"," calls the function and prints its return value",[160,9675,9676,9679,9680,8246],{},[33,9677,9678],{},"help(print)"," shows help for the built-in ",[33,9681,9167],{},[160,9683,9684,9687,9688,9690],{},[33,9685,9686],{},"type(print)"," shows that ",[33,9689,76],{}," is a built-in function",[160,9692,9693,9696],{},[33,9694,9695],{},"type(my_function)"," shows that your function is a function object",[14,9698,1051,9699,9701,9702,351],{},[33,9700,9672],{},": it runs the function. If the function only prints something and does not return a value, you may also see ",[33,9703,9364],{},[21,9705,1063],{"id":1062},[1065,9707,9709],{"id":9708},"what-is-a-function-in-simple-words","What is a function in simple words?",[14,9711,9712],{},"A function is a reusable set of instructions that performs a task.",[1065,9714,9716],{"id":9715},"what-is-the-difference-between-defining-and-calling-a-function","What is the difference between defining and calling a function?",[14,9718,9719],{},"Defining creates the function. Calling runs it.",[1065,9721,9723],{"id":9722},"does-every-function-need-a-return-statement","Does every function need a return statement?",[14,9725,9726],{},"No. Some functions only perform an action, such as printing output.",[1065,9728,1240,9730,9732],{"id":9729},"is-print-a-function",[33,9731,9167],{}," a function?",[14,9734,9735,9736,9738],{},"Yes. ",[33,9737,9167],{}," is a built-in Python function.",[1065,9740,9742],{"id":9741},"why-should-beginners-use-functions","Why should beginners use functions?",[14,9744,9745],{},"Functions make code easier to reuse, organize, and understand.",[21,9747,1105],{"id":1104},[157,9749,9750,9754,9759,9764,9769,9774],{},[160,9751,9752],{},[347,9753,8993],{"href":8992},[160,9755,9756],{},[347,9757,9758],{"href":9008},"What is a parameter in Python?",[160,9760,9761],{},[347,9762,9763],{"href":9015},"What is an argument in Python?",[160,9765,9766],{},[347,9767,9768],{"href":9371},"What is a return value in Python?",[160,9770,9771],{},[347,9772,9773],{"href":9500},"How to create a simple function in Python",[160,9775,9776],{},[347,9777,1665,9778,2816],{"href":9471},[33,9779,9167],{},[14,9781,9782],{},"Next step: learn how to create your own function, pass values into it, and get a result back.",[1137,9784,9785],{},"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 .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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":9787},[9788,9789,9790,9791,9792,9793,9794,9795,9796,9804],{"id":6100,"depth":72,"text":6101},{"id":8859,"depth":72,"text":8860},{"id":8883,"depth":72,"text":8884},{"id":8996,"depth":72,"text":8997},{"id":9140,"depth":72,"text":9141},{"id":9375,"depth":72,"text":9376},{"id":9476,"depth":72,"text":9477},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":9797},[9798,9799,9800,9801,9803],{"id":9708,"depth":89,"text":9709},{"id":9715,"depth":89,"text":9716},{"id":9722,"depth":89,"text":9723},{"id":9729,"depth":89,"text":9802},"Is print() a function?",{"id":9741,"depth":89,"text":9742},{"id":1104,"depth":72,"text":1105},"Master what is a function in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-function-in-python",{"title":8742,"description":9805},"glossary\u002Fwhat-is-a-function-in-python","ox27Rb0rBNXGTNmCFtfm2MEG3Gl_Soqb6b10lgkKaZ4",{"id":9812,"title":9813,"body":9814,"description":11476,"extension":1159,"meta":11477,"navigation":475,"path":11478,"seo":11479,"stem":11480,"__hash__":11481},"content\u002Fglossary\u002Fwhat-is-a-generator-in-python.md","What Is a Generator in Python?",{"type":7,"value":9815,"toc":11449},[9816,9819,9822,9825,9831,9833,9836,9839,9858,9860,9863,9927,9929,9949,9952,9973,9977,9980,9991,9994,9998,10003,10009,10071,10073,10087,10090,10106,10109,10113,10118,10232,10234,10279,10282,10298,10304,10308,10311,10313,10316,10369,10373,10376,10458,10461,10479,10488,10565,10567,10588,10592,10595,10598,10609,10612,10616,10626,10629,10640,10643,10704,10707,10710,10788,10790,10799,10803,10806,10871,10873,10902,10908,10915,10919,10922,10955,10958,10960,10962,10984,10988,11106,11108,11132,11135,11139,11142,11236,11238,11273,11276,11296,11299,11371,11373,11377,11380,11384,11389,11393,11396,11400,11403,11407,11413,11415,11443,11446],[10,9817,9813],{"id":9818},"what-is-a-generator-in-python",[14,9820,9821],{},"A generator in Python is an object that produces values one at a time, only when they are needed.",[14,9823,9824],{},"This is useful because Python does not have to create and store every value at once. For beginners, the main idea is simple: a generator lets you work through data step by step instead of building a full list immediately.",[14,9826,9827,9828,351],{},"Generators are commonly created with a function that uses ",[33,9829,9830],{},"yield",[21,9832,6101],{"id":6100},[14,9834,9835],{},"A generator is an object that gives values one at a time when Python asks for them.",[14,9837,9838],{},"Key points:",[157,9840,9841,9847,9853],{},[160,9842,9843,9844,9846],{},"It does ",[331,9845,8193],{}," build the whole result at once",[160,9848,9849,9850],{},"It can be looped over with ",[33,9851,9852],{},"for",[160,9854,9855,9856],{},"It is often created by a function that uses ",[33,9857,9830],{},[21,9859,24],{"id":23},[14,9861,9862],{},"This is the simplest generator example:",[26,9864,9866],{"className":28,"code":9865,"language":30,"meta":31,"style":31},"def numbers():\n    yield 1\n    yield 2\n    yield 3\n\nfor n in numbers():\n    print(n)\n",[33,9867,9868,9877,9885,9891,9898,9902,9916],{"__ignoreMap":31},[36,9869,9870,9872,9875],{"class":38,"line":39},[36,9871,8785],{"class":137},[36,9873,9874],{"class":3082}," numbers",[36,9876,4244],{"class":50},[36,9878,9879,9882],{"class":38,"line":72},[36,9880,9881],{"class":1718},"    yield",[36,9883,9884],{"class":54}," 1\n",[36,9886,9887,9889],{"class":38,"line":89},[36,9888,9881],{"class":1718},[36,9890,7733],{"class":54},[36,9892,9893,9895],{"class":38,"line":496},[36,9894,9881],{"class":1718},[36,9896,9897],{"class":54}," 3\n",[36,9899,9900],{"class":38,"line":501},[36,9901,476],{"emptyLinePlaceholder":475},[36,9903,9904,9906,9909,9912,9914],{"class":38,"line":522},[36,9905,9852],{"class":1718},[36,9907,9908],{"class":42}," n ",[36,9910,9911],{"class":1718},"in",[36,9913,9874],{"class":82},[36,9915,4244],{"class":50},[36,9917,9918,9920,9922,9925],{"class":38,"line":527},[36,9919,1735],{"class":75},[36,9921,79],{"class":50},[36,9923,9924],{"class":82},"n",[36,9926,86],{"class":50},[14,9928,107],{},[26,9930,9932],{"className":28,"code":9931,"language":30,"meta":31,"style":31},"1\n2\n3\n",[33,9933,9934,9939,9944],{"__ignoreMap":31},[36,9935,9936],{"class":38,"line":39},[36,9937,9938],{"class":54},"1\n",[36,9940,9941],{"class":38,"line":72},[36,9942,9943],{"class":54},"2\n",[36,9945,9946],{"class":38,"line":89},[36,9947,9948],{"class":54},"3\n",[14,9950,9951],{},"What happens here:",[157,9953,9954,9962,9968],{},[160,9955,9956,9959,9960],{},[33,9957,9958],{},"numbers()"," is a generator function because it uses ",[33,9961,9830],{},[160,9963,9964,9965,9967],{},"Each ",[33,9966,9830],{}," gives back one value",[160,9969,3542,9970,9972],{},[33,9971,9852],{}," loop asks for values one by one",[21,9974,9976],{"id":9975},"why-beginners-should-care","Why beginners should care",[14,9978,9979],{},"Generators are helpful because they:",[157,9981,9982,9985,9988],{},[160,9983,9984],{},"Work well when looping through many values",[160,9986,9987],{},"Can use less memory than a full list",[160,9989,9990],{},"Show how Python can process items step by step",[14,9992,9993],{},"You do not need generators for every task. But they are useful when you do not want to store everything at once.",[21,9995,9997],{"id":9996},"how-a-generator-works","How a generator works",[14,9999,10000,10001,351],{},"A normal function usually finishes with ",[33,10002,9147],{},[14,10004,10005,10006,10008],{},"A generator function is different. It uses ",[33,10007,9830],{}," to produce one value at a time.",[26,10010,10012],{"className":28,"code":10011,"language":30,"meta":31,"style":31},"def greet():\n    yield \"Hello\"\n    yield \"World\"\n\nfor word in greet():\n    print(word)\n",[33,10013,10014,10022,10032,10043,10047,10060],{"__ignoreMap":31},[36,10015,10016,10018,10020],{"class":38,"line":39},[36,10017,8785],{"class":137},[36,10019,9033],{"class":3082},[36,10021,4244],{"class":50},[36,10023,10024,10026,10028,10030],{"class":38,"line":72},[36,10025,9881],{"class":1718},[36,10027,224],{"class":141},[36,10029,8801],{"class":145},[36,10031,1954],{"class":141},[36,10033,10034,10036,10038,10041],{"class":38,"line":89},[36,10035,9881],{"class":1718},[36,10037,224],{"class":141},[36,10039,10040],{"class":145},"World",[36,10042,1954],{"class":141},[36,10044,10045],{"class":38,"line":496},[36,10046,476],{"emptyLinePlaceholder":475},[36,10048,10049,10051,10054,10056,10058],{"class":38,"line":501},[36,10050,9852],{"class":1718},[36,10052,10053],{"class":42}," word ",[36,10055,9911],{"class":1718},[36,10057,9033],{"class":82},[36,10059,4244],{"class":50},[36,10061,10062,10064,10066,10069],{"class":38,"line":522},[36,10063,1735],{"class":75},[36,10065,79],{"class":50},[36,10067,10068],{"class":82},"word",[36,10070,86],{"class":50},[14,10072,107],{},[26,10074,10076],{"className":28,"code":10075,"language":30,"meta":31,"style":31},"Hello\nWorld\n",[33,10077,10078,10082],{"__ignoreMap":31},[36,10079,10080],{"class":38,"line":39},[36,10081,8821],{"class":42},[36,10083,10084],{"class":38,"line":72},[36,10085,10086],{"class":42},"World\n",[14,10088,10089],{},"The important idea is this:",[157,10091,10092,10097,10100,10103],{},[160,10093,10094,10095],{},"Python runs the function until it reaches ",[33,10096,9830],{},[160,10098,10099],{},"It sends back that value",[160,10101,10102],{},"The function pauses",[160,10104,10105],{},"When the next value is needed, the function continues from where it paused",[14,10107,10108],{},"That pause-and-continue behavior is what makes generators special.",[21,10110,10112],{"id":10111},"generator-function-example","Generator function example",[14,10114,10115,10116,822],{},"Here is a small example that shows the function pausing after each ",[33,10117,9830],{},[26,10119,10121],{"className":28,"code":10120,"language":30,"meta":31,"style":31},"def count_up():\n    print(\"Start\")\n    yield 1\n    print(\"Middle\")\n    yield 2\n    print(\"End\")\n    yield 3\n\nfor number in count_up():\n    print(\"Got:\", number)\n",[33,10122,10123,10132,10147,10153,10168,10174,10189,10195,10199,10212],{"__ignoreMap":31},[36,10124,10125,10127,10130],{"class":38,"line":39},[36,10126,8785],{"class":137},[36,10128,10129],{"class":3082}," count_up",[36,10131,4244],{"class":50},[36,10133,10134,10136,10138,10140,10143,10145],{"class":38,"line":72},[36,10135,1735],{"class":75},[36,10137,79],{"class":50},[36,10139,214],{"class":141},[36,10141,10142],{"class":145},"Start",[36,10144,214],{"class":141},[36,10146,86],{"class":50},[36,10148,10149,10151],{"class":38,"line":89},[36,10150,9881],{"class":1718},[36,10152,9884],{"class":54},[36,10154,10155,10157,10159,10161,10164,10166],{"class":38,"line":496},[36,10156,1735],{"class":75},[36,10158,79],{"class":50},[36,10160,214],{"class":141},[36,10162,10163],{"class":145},"Middle",[36,10165,214],{"class":141},[36,10167,86],{"class":50},[36,10169,10170,10172],{"class":38,"line":501},[36,10171,9881],{"class":1718},[36,10173,7733],{"class":54},[36,10175,10176,10178,10180,10182,10185,10187],{"class":38,"line":522},[36,10177,1735],{"class":75},[36,10179,79],{"class":50},[36,10181,214],{"class":141},[36,10183,10184],{"class":145},"End",[36,10186,214],{"class":141},[36,10188,86],{"class":50},[36,10190,10191,10193],{"class":38,"line":527},[36,10192,9881],{"class":1718},[36,10194,9897],{"class":54},[36,10196,10197],{"class":38,"line":3092},[36,10198,476],{"emptyLinePlaceholder":475},[36,10200,10201,10203,10206,10208,10210],{"class":38,"line":3118},[36,10202,9852],{"class":1718},[36,10204,10205],{"class":42}," number ",[36,10207,9911],{"class":1718},[36,10209,10129],{"class":82},[36,10211,4244],{"class":50},[36,10213,10214,10216,10218,10220,10223,10225,10227,10230],{"class":38,"line":3123},[36,10215,1735],{"class":75},[36,10217,79],{"class":50},[36,10219,214],{"class":141},[36,10221,10222],{"class":145},"Got:",[36,10224,214],{"class":141},[36,10226,58],{"class":50},[36,10228,10229],{"class":82}," number",[36,10231,86],{"class":50},[14,10233,107],{},[26,10235,10237],{"className":28,"code":10236,"language":30,"meta":31,"style":31},"Start\nGot: 1\nMiddle\nGot: 2\nEnd\nGot: 3\n",[33,10238,10239,10244,10253,10258,10266,10271],{"__ignoreMap":31},[36,10240,10241],{"class":38,"line":39},[36,10242,10243],{"class":42},"Start\n",[36,10245,10246,10249,10251],{"class":38,"line":72},[36,10247,10248],{"class":42},"Got",[36,10250,822],{"class":50},[36,10252,9884],{"class":54},[36,10254,10255],{"class":38,"line":89},[36,10256,10257],{"class":42},"Middle\n",[36,10259,10260,10262,10264],{"class":38,"line":496},[36,10261,10248],{"class":42},[36,10263,822],{"class":50},[36,10265,7733],{"class":54},[36,10267,10268],{"class":38,"line":501},[36,10269,10270],{"class":42},"End\n",[36,10272,10273,10275,10277],{"class":38,"line":522},[36,10274,10248],{"class":42},[36,10276,822],{"class":50},[36,10278,9897],{"class":54},[14,10280,10281],{},"Notice what happens:",[157,10283,10284,10290,10295],{},[160,10285,10286,10287,10289],{},"The function does ",[331,10288,8193],{}," run all at once",[160,10291,10292,10293],{},"It stops at each ",[33,10294,9830],{},[160,10296,10297],{},"It continues only when the loop asks for the next value",[14,10299,1672,10300,351],{},[347,10301,10303],{"href":10302},"\u002Flearn\u002Fgenerators-in-python-explained\u002F","generators in Python explained",[21,10305,10307],{"id":10306},"generator-vs-list","Generator vs list",[14,10309,10310],{},"A generator and a list are not the same.",[1065,10312,672],{"id":146},[14,10314,10315],{},"A list stores all items in memory right away.",[26,10317,10319],{"className":28,"code":10318,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4]\nprint(numbers)\nprint(numbers[0])\n",[33,10320,10321,10345,10355],{"__ignoreMap":31},[36,10322,10323,10325,10327,10329,10331,10333,10335,10337,10339,10341,10343],{"class":38,"line":39},[36,10324,43],{"class":42},[36,10326,47],{"class":46},[36,10328,51],{"class":50},[36,10330,55],{"class":54},[36,10332,58],{"class":50},[36,10334,61],{"class":54},[36,10336,58],{"class":50},[36,10338,66],{"class":54},[36,10340,58],{"class":50},[36,10342,1549],{"class":54},[36,10344,69],{"class":50},[36,10346,10347,10349,10351,10353],{"class":38,"line":72},[36,10348,76],{"class":75},[36,10350,79],{"class":50},[36,10352,83],{"class":82},[36,10354,86],{"class":50},[36,10356,10357,10359,10361,10363,10365,10367],{"class":38,"line":89},[36,10358,76],{"class":75},[36,10360,79],{"class":50},[36,10362,83],{"class":82},[36,10364,117],{"class":50},[36,10366,490],{"class":54},[36,10368,493],{"class":50},[1065,10370,10372],{"id":10371},"generator","Generator",[14,10374,10375],{},"A generator produces items only when needed.",[26,10377,10379],{"className":28,"code":10378,"language":30,"meta":31,"style":31},"def numbers():\n    yield 1\n    yield 2\n    yield 3\n    yield 4\n\ngen = numbers()\n\nfor item in gen:\n    print(item)\n",[33,10380,10381,10389,10395,10401,10407,10414,10418,10429,10433,10447],{"__ignoreMap":31},[36,10382,10383,10385,10387],{"class":38,"line":39},[36,10384,8785],{"class":137},[36,10386,9874],{"class":3082},[36,10388,4244],{"class":50},[36,10390,10391,10393],{"class":38,"line":72},[36,10392,9881],{"class":1718},[36,10394,9884],{"class":54},[36,10396,10397,10399],{"class":38,"line":89},[36,10398,9881],{"class":1718},[36,10400,7733],{"class":54},[36,10402,10403,10405],{"class":38,"line":496},[36,10404,9881],{"class":1718},[36,10406,9897],{"class":54},[36,10408,10409,10411],{"class":38,"line":501},[36,10410,9881],{"class":1718},[36,10412,10413],{"class":54}," 4\n",[36,10415,10416],{"class":38,"line":522},[36,10417,476],{"emptyLinePlaceholder":475},[36,10419,10420,10423,10425,10427],{"class":38,"line":527},[36,10421,10422],{"class":42},"gen ",[36,10424,47],{"class":46},[36,10426,9874],{"class":82},[36,10428,3170],{"class":50},[36,10430,10431],{"class":38,"line":3092},[36,10432,476],{"emptyLinePlaceholder":475},[36,10434,10435,10437,10440,10442,10445],{"class":38,"line":3118},[36,10436,9852],{"class":1718},[36,10438,10439],{"class":42}," item ",[36,10441,9911],{"class":1718},[36,10443,10444],{"class":42}," gen",[36,10446,1730],{"class":50},[36,10448,10449,10451,10453,10456],{"class":38,"line":3123},[36,10450,1735],{"class":75},[36,10452,79],{"class":50},[36,10454,10455],{"class":82},"item",[36,10457,86],{"class":50},[14,10459,10460],{},"Main differences:",[157,10462,10463,10468,10473,10476],{},[160,10464,3355,10465,10467],{},[331,10466,146],{}," stores everything at once",[160,10469,3355,10470,10472],{},[331,10471,10371],{}," produces values one at a time",[160,10474,10475],{},"A list can be reused easily",[160,10477,10478],{},"A generator is usually used up as you iterate through it",[14,10480,10481,10482,822],{},"If you want all generator values at once, you can convert it with ",[347,10483,10485],{"href":10484},"\u002Freference\u002Fpython-list-function-explained\u002F",[33,10486,10487],{},"list()",[26,10489,10491],{"className":28,"code":10490,"language":30,"meta":31,"style":31},"def numbers():\n    yield 1\n    yield 2\n    yield 3\n\ngen = numbers()\nall_values = list(gen)\n\nprint(all_values)\n",[33,10492,10493,10501,10507,10513,10519,10523,10533,10550,10554],{"__ignoreMap":31},[36,10494,10495,10497,10499],{"class":38,"line":39},[36,10496,8785],{"class":137},[36,10498,9874],{"class":3082},[36,10500,4244],{"class":50},[36,10502,10503,10505],{"class":38,"line":72},[36,10504,9881],{"class":1718},[36,10506,9884],{"class":54},[36,10508,10509,10511],{"class":38,"line":89},[36,10510,9881],{"class":1718},[36,10512,7733],{"class":54},[36,10514,10515,10517],{"class":38,"line":496},[36,10516,9881],{"class":1718},[36,10518,9897],{"class":54},[36,10520,10521],{"class":38,"line":501},[36,10522,476],{"emptyLinePlaceholder":475},[36,10524,10525,10527,10529,10531],{"class":38,"line":522},[36,10526,10422],{"class":42},[36,10528,47],{"class":46},[36,10530,9874],{"class":82},[36,10532,3170],{"class":50},[36,10534,10535,10538,10540,10543,10545,10548],{"class":38,"line":527},[36,10536,10537],{"class":42},"all_values ",[36,10539,47],{"class":46},[36,10541,10542],{"class":96}," list",[36,10544,79],{"class":50},[36,10546,10547],{"class":82},"gen",[36,10549,86],{"class":50},[36,10551,10552],{"class":38,"line":3092},[36,10553,476],{"emptyLinePlaceholder":475},[36,10555,10556,10558,10560,10563],{"class":38,"line":3118},[36,10557,76],{"class":75},[36,10559,79],{"class":50},[36,10561,10562],{"class":82},"all_values",[36,10564,86],{"class":50},[14,10566,107],{},[26,10568,10570],{"className":28,"code":10569,"language":30,"meta":31,"style":31},"[1, 2, 3]\n",[33,10571,10572],{"__ignoreMap":31},[36,10573,10574,10576,10578,10580,10582,10584,10586],{"class":38,"line":39},[36,10575,117],{"class":50},[36,10577,55],{"class":54},[36,10579,58],{"class":50},[36,10581,61],{"class":54},[36,10583,58],{"class":50},[36,10585,66],{"class":54},[36,10587,69],{"class":50},[21,10589,10591],{"id":10590},"when-to-use-a-generator","When to use a generator",[14,10593,10594],{},"A generator is useful when you want to process values step by step.",[14,10596,10597],{},"Common cases:",[157,10599,10600,10603,10606],{},[160,10601,10602],{},"Reading large data one piece at a time",[160,10604,10605],{},"Looping through many values without storing them all",[160,10607,10608],{},"Creating very large sequences",[14,10610,10611],{},"For small data, a list is often simpler. For large or streaming data, a generator can be a better choice.",[21,10613,10615],{"id":10614},"important-beginner-note","Important beginner note",[14,10617,10618,10619,10622,10623,351],{},"A generator is ",[331,10620,10621],{},"iterable",", but it is ",[331,10624,10625],{},"not a list",[14,10627,10628],{},"That means:",[157,10630,10631,10634,10637],{},[160,10632,10633],{},"You can loop over it",[160,10635,10636],{},"You usually cannot access items by index",[160,10638,10639],{},"It may be exhausted after one full loop",[14,10641,10642],{},"This will fail:",[26,10644,10646],{"className":28,"code":10645,"language":30,"meta":31,"style":31},"def numbers():\n    yield 10\n    yield 20\n    yield 30\n\ngen = numbers()\nprint(gen[0])\n",[33,10647,10648,10656,10663,10669,10676,10680,10690],{"__ignoreMap":31},[36,10649,10650,10652,10654],{"class":38,"line":39},[36,10651,8785],{"class":137},[36,10653,9874],{"class":3082},[36,10655,4244],{"class":50},[36,10657,10658,10660],{"class":38,"line":72},[36,10659,9881],{"class":1718},[36,10661,10662],{"class":54}," 10\n",[36,10664,10665,10667],{"class":38,"line":89},[36,10666,9881],{"class":1718},[36,10668,2526],{"class":54},[36,10670,10671,10673],{"class":38,"line":496},[36,10672,9881],{"class":1718},[36,10674,10675],{"class":54}," 30\n",[36,10677,10678],{"class":38,"line":501},[36,10679,476],{"emptyLinePlaceholder":475},[36,10681,10682,10684,10686,10688],{"class":38,"line":522},[36,10683,10422],{"class":42},[36,10685,47],{"class":46},[36,10687,9874],{"class":82},[36,10689,3170],{"class":50},[36,10691,10692,10694,10696,10698,10700,10702],{"class":38,"line":527},[36,10693,76],{"class":75},[36,10695,79],{"class":50},[36,10697,10547],{"class":82},[36,10699,117],{"class":50},[36,10701,490],{"class":54},[36,10703,493],{"class":50},[14,10705,10706],{},"This causes an error because generators do not support indexing.",[14,10708,10709],{},"If you need indexing, convert the generator to a list first:",[26,10711,10713],{"className":28,"code":10712,"language":30,"meta":31,"style":31},"def numbers():\n    yield 10\n    yield 20\n    yield 30\n\ngen = numbers()\nvalues = list(gen)\n\nprint(values[0])\n",[33,10714,10715,10723,10729,10735,10741,10745,10755,10770,10774],{"__ignoreMap":31},[36,10716,10717,10719,10721],{"class":38,"line":39},[36,10718,8785],{"class":137},[36,10720,9874],{"class":3082},[36,10722,4244],{"class":50},[36,10724,10725,10727],{"class":38,"line":72},[36,10726,9881],{"class":1718},[36,10728,10662],{"class":54},[36,10730,10731,10733],{"class":38,"line":89},[36,10732,9881],{"class":1718},[36,10734,2526],{"class":54},[36,10736,10737,10739],{"class":38,"line":496},[36,10738,9881],{"class":1718},[36,10740,10675],{"class":54},[36,10742,10743],{"class":38,"line":501},[36,10744,476],{"emptyLinePlaceholder":475},[36,10746,10747,10749,10751,10753],{"class":38,"line":522},[36,10748,10422],{"class":42},[36,10750,47],{"class":46},[36,10752,9874],{"class":82},[36,10754,3170],{"class":50},[36,10756,10757,10760,10762,10764,10766,10768],{"class":38,"line":527},[36,10758,10759],{"class":42},"values ",[36,10761,47],{"class":46},[36,10763,10542],{"class":96},[36,10765,79],{"class":50},[36,10767,10547],{"class":82},[36,10769,86],{"class":50},[36,10771,10772],{"class":38,"line":3092},[36,10773,476],{"emptyLinePlaceholder":475},[36,10775,10776,10778,10780,10782,10784,10786],{"class":38,"line":3118},[36,10777,76],{"class":75},[36,10779,79],{"class":50},[36,10781,5944],{"class":82},[36,10783,117],{"class":50},[36,10785,490],{"class":54},[36,10787,493],{"class":50},[14,10789,107],{},[26,10791,10793],{"className":28,"code":10792,"language":30,"meta":31,"style":31},"10\n",[33,10794,10795],{"__ignoreMap":31},[36,10796,10797],{"class":38,"line":39},[36,10798,10792],{"class":54},[21,10800,10802],{"id":10801},"generator-expression","Generator expression",[14,10804,10805],{},"You can also create a generator with parentheses.",[26,10807,10809],{"className":28,"code":10808,"language":30,"meta":31,"style":31},"squares = (x * x for x in range(5))\n\nfor value in squares:\n    print(value)\n",[33,10810,10811,10843,10847,10861],{"__ignoreMap":31},[36,10812,10813,10816,10818,10820,10822,10825,10828,10830,10832,10834,10837,10839,10841],{"class":38,"line":39},[36,10814,10815],{"class":42},"squares ",[36,10817,47],{"class":46},[36,10819,744],{"class":50},[36,10821,5289],{"class":42},[36,10823,10824],{"class":46},"*",[36,10826,10827],{"class":42}," x ",[36,10829,9852],{"class":1718},[36,10831,10827],{"class":42},[36,10833,9911],{"class":1718},[36,10835,10836],{"class":75}," range",[36,10838,79],{"class":50},[36,10840,1243],{"class":54},[36,10842,104],{"class":50},[36,10844,10845],{"class":38,"line":72},[36,10846,476],{"emptyLinePlaceholder":475},[36,10848,10849,10851,10854,10856,10859],{"class":38,"line":89},[36,10850,9852],{"class":1718},[36,10852,10853],{"class":42}," value ",[36,10855,9911],{"class":1718},[36,10857,10858],{"class":42}," squares",[36,10860,1730],{"class":50},[36,10862,10863,10865,10867,10869],{"class":38,"line":496},[36,10864,1735],{"class":75},[36,10866,79],{"class":50},[36,10868,2686],{"class":82},[36,10870,86],{"class":50},[14,10872,107],{},[26,10874,10876],{"className":28,"code":10875,"language":30,"meta":31,"style":31},"0\n1\n4\n9\n16\n",[33,10877,10878,10883,10887,10892,10897],{"__ignoreMap":31},[36,10879,10880],{"class":38,"line":39},[36,10881,10882],{"class":54},"0\n",[36,10884,10885],{"class":38,"line":72},[36,10886,9938],{"class":54},[36,10888,10889],{"class":38,"line":89},[36,10890,10891],{"class":54},"4\n",[36,10893,10894],{"class":38,"line":496},[36,10895,10896],{"class":54},"9\n",[36,10898,10899],{"class":38,"line":501},[36,10900,10901],{"class":54},"16\n",[14,10903,10904,10905,351],{},"This is called a ",[331,10906,10907],{},"generator expression",[14,10909,10910,10911,351],{},"It looks similar to a list comprehension, but it does not build a full list right away. If you want to compare the two, see ",[347,10912,10914],{"href":10913},"\u002Flearn\u002Flist-comprehensions-in-python-explained\u002F","list comprehensions in Python explained",[21,10916,10918],{"id":10917},"related-concepts","Related concepts",[14,10920,10921],{},"These terms are closely connected:",[157,10923,10924,10937,10949],{},[160,10925,10926,10929,10930,10932,10933],{},[331,10927,10928],{},"Iterable",": something you can loop over",[335,10931],{},"\nSee ",[347,10934,10936],{"href":10935},"\u002Fglossary\u002Fwhat-is-an-iterable-in-python\u002F","what is an iterable in Python",[160,10938,10939,10942,10943,10932,10945],{},[331,10940,10941],{},"Iterator",": an object that returns the next item",[335,10944],{},[347,10946,10948],{"href":10947},"\u002Fglossary\u002Fwhat-is-an-iterator-in-python\u002F","what is an iterator in Python",[160,10950,10951,10954],{},[331,10952,10953],{},"List comprehension",": creates a list, unlike a generator expression",[14,10956,10957],{},"A generator is part of Python’s iteration system, but for beginners, the main thing to remember is that it gives values one by one.",[21,10959,905],{"id":904},[14,10961,8484],{},[157,10963,10964,10967,10973,10976],{},[160,10965,10966],{},"Thinking a generator is the same as a list",[160,10968,10969,10970],{},"Trying to access a generator with an index like ",[33,10971,10972],{},"my_gen[0]",[160,10974,10975],{},"Forgetting that a generator may be empty after one full loop",[160,10977,2639,10978,10980,10981,10983],{},[33,10979,9147],{}," when ",[33,10982,9830],{}," is needed",[1065,10985,10987],{"id":10986},"example-generator-gets-exhausted","Example: generator gets exhausted",[26,10989,10991],{"className":28,"code":10990,"language":30,"meta":31,"style":31},"def numbers():\n    yield 1\n    yield 2\n    yield 3\n\ngen = numbers()\n\nfor item in gen:\n    print(item)\n\nprint(\"Loop again:\")\n\nfor item in gen:\n    print(item)\n",[33,10992,10993,11001,11007,11013,11019,11023,11033,11037,11049,11059,11063,11078,11082,11095],{"__ignoreMap":31},[36,10994,10995,10997,10999],{"class":38,"line":39},[36,10996,8785],{"class":137},[36,10998,9874],{"class":3082},[36,11000,4244],{"class":50},[36,11002,11003,11005],{"class":38,"line":72},[36,11004,9881],{"class":1718},[36,11006,9884],{"class":54},[36,11008,11009,11011],{"class":38,"line":89},[36,11010,9881],{"class":1718},[36,11012,7733],{"class":54},[36,11014,11015,11017],{"class":38,"line":496},[36,11016,9881],{"class":1718},[36,11018,9897],{"class":54},[36,11020,11021],{"class":38,"line":501},[36,11022,476],{"emptyLinePlaceholder":475},[36,11024,11025,11027,11029,11031],{"class":38,"line":522},[36,11026,10422],{"class":42},[36,11028,47],{"class":46},[36,11030,9874],{"class":82},[36,11032,3170],{"class":50},[36,11034,11035],{"class":38,"line":527},[36,11036,476],{"emptyLinePlaceholder":475},[36,11038,11039,11041,11043,11045,11047],{"class":38,"line":3092},[36,11040,9852],{"class":1718},[36,11042,10439],{"class":42},[36,11044,9911],{"class":1718},[36,11046,10444],{"class":42},[36,11048,1730],{"class":50},[36,11050,11051,11053,11055,11057],{"class":38,"line":3118},[36,11052,1735],{"class":75},[36,11054,79],{"class":50},[36,11056,10455],{"class":82},[36,11058,86],{"class":50},[36,11060,11061],{"class":38,"line":3123},[36,11062,476],{"emptyLinePlaceholder":475},[36,11064,11065,11067,11069,11071,11074,11076],{"class":38,"line":3144},[36,11066,76],{"class":75},[36,11068,79],{"class":50},[36,11070,214],{"class":141},[36,11072,11073],{"class":145},"Loop again:",[36,11075,214],{"class":141},[36,11077,86],{"class":50},[36,11079,11080],{"class":38,"line":3160},[36,11081,476],{"emptyLinePlaceholder":475},[36,11083,11085,11087,11089,11091,11093],{"class":38,"line":11084},13,[36,11086,9852],{"class":1718},[36,11088,10439],{"class":42},[36,11090,9911],{"class":1718},[36,11092,10444],{"class":42},[36,11094,1730],{"class":50},[36,11096,11098,11100,11102,11104],{"class":38,"line":11097},14,[36,11099,1735],{"class":75},[36,11101,79],{"class":50},[36,11103,10455],{"class":82},[36,11105,86],{"class":50},[14,11107,107],{},[26,11109,11111],{"className":28,"code":11110,"language":30,"meta":31,"style":31},"1\n2\n3\nLoop again:\n",[33,11112,11113,11117,11121,11125],{"__ignoreMap":31},[36,11114,11115],{"class":38,"line":39},[36,11116,9938],{"class":54},[36,11118,11119],{"class":38,"line":72},[36,11120,9943],{"class":54},[36,11122,11123],{"class":38,"line":89},[36,11124,9948],{"class":54},[36,11126,11127,11130],{"class":38,"line":496},[36,11128,11129],{"class":42},"Loop again",[36,11131,1730],{"class":50},[14,11133,11134],{},"The second loop prints nothing because the generator has already been used up.",[21,11136,11138],{"id":11137},"useful-checks","Useful checks",[14,11140,11141],{},"These simple commands can help you understand what a generator is doing:",[26,11143,11145],{"className":28,"code":11144,"language":30,"meta":31,"style":31},"def numbers():\n    yield 1\n    yield 2\n    yield 3\n\nmy_generator = numbers()\n\nprint(type(my_generator))\nprint(next(my_generator))\nprint(list(my_generator))\n",[33,11146,11147,11155,11161,11167,11173,11177,11188,11192,11207,11222],{"__ignoreMap":31},[36,11148,11149,11151,11153],{"class":38,"line":39},[36,11150,8785],{"class":137},[36,11152,9874],{"class":3082},[36,11154,4244],{"class":50},[36,11156,11157,11159],{"class":38,"line":72},[36,11158,9881],{"class":1718},[36,11160,9884],{"class":54},[36,11162,11163,11165],{"class":38,"line":89},[36,11164,9881],{"class":1718},[36,11166,7733],{"class":54},[36,11168,11169,11171],{"class":38,"line":496},[36,11170,9881],{"class":1718},[36,11172,9897],{"class":54},[36,11174,11175],{"class":38,"line":501},[36,11176,476],{"emptyLinePlaceholder":475},[36,11178,11179,11182,11184,11186],{"class":38,"line":522},[36,11180,11181],{"class":42},"my_generator ",[36,11183,47],{"class":46},[36,11185,9874],{"class":82},[36,11187,3170],{"class":50},[36,11189,11190],{"class":38,"line":527},[36,11191,476],{"emptyLinePlaceholder":475},[36,11193,11194,11196,11198,11200,11202,11205],{"class":38,"line":3092},[36,11195,76],{"class":75},[36,11197,79],{"class":50},[36,11199,97],{"class":96},[36,11201,79],{"class":50},[36,11203,11204],{"class":82},"my_generator",[36,11206,104],{"class":50},[36,11208,11209,11211,11213,11216,11218,11220],{"class":38,"line":3118},[36,11210,76],{"class":75},[36,11212,79],{"class":50},[36,11214,11215],{"class":75},"next",[36,11217,79],{"class":50},[36,11219,11204],{"class":82},[36,11221,104],{"class":50},[36,11223,11224,11226,11228,11230,11232,11234],{"class":38,"line":3123},[36,11225,76],{"class":75},[36,11227,79],{"class":50},[36,11229,146],{"class":96},[36,11231,79],{"class":50},[36,11233,11204],{"class":82},[36,11235,104],{"class":50},[14,11237,4527],{},[26,11239,11241],{"className":28,"code":11240,"language":30,"meta":31,"style":31},"\u003Cclass 'generator'>\n1\n[2, 3]\n",[33,11242,11243,11257,11261],{"__ignoreMap":31},[36,11244,11245,11247,11249,11251,11253,11255],{"class":38,"line":39},[36,11246,134],{"class":46},[36,11248,138],{"class":137},[36,11250,142],{"class":141},[36,11252,10371],{"class":145},[36,11254,149],{"class":141},[36,11256,152],{"class":46},[36,11258,11259],{"class":38,"line":72},[36,11260,9938],{"class":54},[36,11262,11263,11265,11267,11269,11271],{"class":38,"line":89},[36,11264,117],{"class":50},[36,11266,1543],{"class":54},[36,11268,58],{"class":50},[36,11270,66],{"class":54},[36,11272,69],{"class":50},[14,11274,11275],{},"Why this output matters:",[157,11277,11278,11284,11290],{},[160,11279,11280,11283],{},[33,11281,11282],{},"type(my_generator)"," shows that the object is a generator",[160,11285,11286,11289],{},[33,11287,11288],{},"next(my_generator)"," gets the next value",[160,11291,11292,11295],{},[33,11293,11294],{},"list(my_generator)"," collects the remaining values into a list",[14,11297,11298],{},"You can also inspect it with a loop:",[26,11300,11302],{"className":28,"code":11301,"language":30,"meta":31,"style":31},"def numbers():\n    yield 1\n    yield 2\n    yield 3\n\nmy_generator = numbers()\n\nfor item in my_generator:\n    print(item)\n",[33,11303,11304,11312,11318,11324,11330,11334,11344,11348,11361],{"__ignoreMap":31},[36,11305,11306,11308,11310],{"class":38,"line":39},[36,11307,8785],{"class":137},[36,11309,9874],{"class":3082},[36,11311,4244],{"class":50},[36,11313,11314,11316],{"class":38,"line":72},[36,11315,9881],{"class":1718},[36,11317,9884],{"class":54},[36,11319,11320,11322],{"class":38,"line":89},[36,11321,9881],{"class":1718},[36,11323,7733],{"class":54},[36,11325,11326,11328],{"class":38,"line":496},[36,11327,9881],{"class":1718},[36,11329,9897],{"class":54},[36,11331,11332],{"class":38,"line":501},[36,11333,476],{"emptyLinePlaceholder":475},[36,11335,11336,11338,11340,11342],{"class":38,"line":522},[36,11337,11181],{"class":42},[36,11339,47],{"class":46},[36,11341,9874],{"class":82},[36,11343,3170],{"class":50},[36,11345,11346],{"class":38,"line":527},[36,11347,476],{"emptyLinePlaceholder":475},[36,11349,11350,11352,11354,11356,11359],{"class":38,"line":3092},[36,11351,9852],{"class":1718},[36,11353,10439],{"class":42},[36,11355,9911],{"class":1718},[36,11357,11358],{"class":42}," my_generator",[36,11360,1730],{"class":50},[36,11362,11363,11365,11367,11369],{"class":38,"line":3118},[36,11364,1735],{"class":75},[36,11366,79],{"class":50},[36,11368,10455],{"class":82},[36,11370,86],{"class":50},[21,11372,1063],{"id":1062},[1065,11374,11376],{"id":11375},"what-is-a-generator-in-python-in-simple-words","What is a generator in Python in simple words?",[14,11378,11379],{},"It is an object that gives values one at a time instead of storing them all at once.",[1065,11381,11383],{"id":11382},"what-does-yield-do-in-python","What does yield do in Python?",[14,11385,11386,11388],{},[33,11387,9830],{}," sends back one value and pauses the function until the next value is requested.",[1065,11390,11392],{"id":11391},"is-a-generator-the-same-as-a-list","Is a generator the same as a list?",[14,11394,11395],{},"No. A list stores all items immediately, while a generator produces items as needed.",[1065,11397,11399],{"id":11398},"when-should-i-use-a-generator","When should I use a generator?",[14,11401,11402],{},"Use one when you want to loop through many values without keeping all of them in memory at once.",[1065,11404,11406],{"id":11405},"can-i-convert-a-generator-to-a-list","Can I convert a generator to a list?",[14,11408,8666,11409,11412],{},[33,11410,11411],{},"list(generator_object)"," if you want all values at once.",[21,11414,1105],{"id":1104},[157,11416,11417,11422,11427,11432,11437],{},[160,11418,11419],{},[347,11420,11421],{"href":10302},"Generators in Python explained",[160,11423,11424],{},[347,11425,11426],{"href":10947},"What is an iterator in Python",[160,11428,11429],{},[347,11430,11431],{"href":10935},"What is an iterable in Python",[160,11433,11434],{},[347,11435,11436],{"href":10913},"List comprehensions in Python explained",[160,11438,11439],{},[347,11440,1665,11441,2816],{"href":10484},[33,11442,10487],{},[14,11444,11445],{},"If this topic makes sense now, the best next step is to read a beginner-friendly lesson that compares generators, iterables, and iterators side by side with simple examples.",[1137,11447,11448],{},"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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":11450},[11451,11452,11453,11454,11455,11456,11460,11461,11462,11463,11464,11467,11468,11475],{"id":6100,"depth":72,"text":6101},{"id":23,"depth":72,"text":24},{"id":9975,"depth":72,"text":9976},{"id":9996,"depth":72,"text":9997},{"id":10111,"depth":72,"text":10112},{"id":10306,"depth":72,"text":10307,"children":11457},[11458,11459],{"id":146,"depth":89,"text":672},{"id":10371,"depth":89,"text":10372},{"id":10590,"depth":72,"text":10591},{"id":10614,"depth":72,"text":10615},{"id":10801,"depth":72,"text":10802},{"id":10917,"depth":72,"text":10918},{"id":904,"depth":72,"text":905,"children":11465},[11466],{"id":10986,"depth":89,"text":10987},{"id":11137,"depth":72,"text":11138},{"id":1062,"depth":72,"text":1063,"children":11469},[11470,11471,11472,11473,11474],{"id":11375,"depth":89,"text":11376},{"id":11382,"depth":89,"text":11383},{"id":11391,"depth":89,"text":11392},{"id":11398,"depth":89,"text":11399},{"id":11405,"depth":89,"text":11406},{"id":1104,"depth":72,"text":1105},"Master what is a generator in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-generator-in-python",{"title":9813,"description":11476},"glossary\u002Fwhat-is-a-generator-in-python","ObJYC8wVdnyKARrx5IbUqzNrdbDHLkvuyTqgP64Gd_A",{"id":11483,"title":11484,"body":11485,"description":12795,"extension":1159,"meta":12796,"navigation":475,"path":12797,"seo":12798,"stem":12799,"__hash__":12800},"content\u002Fglossary\u002Fwhat-is-a-lambda-function-in-python.md","What Is a Lambda Function in Python?",{"type":7,"value":11486,"toc":12772},[11487,11490,11493,11503,11506,11548,11550,11558,11561,11563,11566,11583,11585,11625,11627,11636,11639,11645,11649,11652,11669,11672,11686,11688,11736,11738,11746,11748,11762,11773,11777,11783,11786,11832,11868,11871,11879,11883,11897,11901,11912,11922,11926,11929,11936,11939,12050,12052,12102,12111,12123,12128,12204,12206,12233,12236,12243,12248,12337,12339,12360,12365,12372,12376,12379,12393,12396,12410,12416,12418,12421,12449,12451,12508,12510,12534,12537,12539,12541,12555,12558,12622,12625,12643,12645,12649,12652,12656,12659,12661,12708,12712,12715,12719,12725,12727,12769],[10,11488,11484],{"id":11489},"what-is-a-lambda-function-in-python",[14,11491,11492],{},"A lambda function in Python is a small function written in one line.",[14,11494,11495,11496,6128,11499,11502],{},"Beginners usually see lambda functions in examples with sorting, ",[33,11497,11498],{},"map()",[33,11500,11501],{},"filter()",". They are useful for short tasks, but they are not a replacement for normal functions.",[14,11504,11505],{},"A lambda function works like a regular function. The main difference is that it is written in a shorter form.",[26,11507,11509],{"className":28,"code":11508,"language":30,"meta":31,"style":31},"square = lambda x: x * x\nprint(square(5))\n",[33,11510,11511,11533],{"__ignoreMap":31},[36,11512,11513,11516,11518,11521,11524,11526,11528,11530],{"class":38,"line":39},[36,11514,11515],{"class":42},"square ",[36,11517,47],{"class":46},[36,11519,11520],{"class":137}," lambda",[36,11522,11523],{"class":3050}," x",[36,11525,822],{"class":50},[36,11527,10827],{"class":42},[36,11529,10824],{"class":46},[36,11531,11532],{"class":42}," x\n",[36,11534,11535,11537,11539,11542,11544,11546],{"class":38,"line":72},[36,11536,76],{"class":75},[36,11538,79],{"class":50},[36,11540,11541],{"class":82},"square",[36,11543,79],{"class":50},[36,11545,1243],{"class":54},[36,11547,104],{"class":50},[14,11549,107],{},[26,11551,11552],{"className":28,"code":6092,"language":30,"meta":31,"style":31},[33,11553,11554],{"__ignoreMap":31},[36,11555,11556],{"class":38,"line":39},[36,11557,6092],{"class":54},[14,11559,11560],{},"A lambda function is a small anonymous function written in one line.",[21,11562,6101],{"id":6100},[14,11564,11565],{},"A lambda function:",[157,11567,11568,11571,11574,11577],{},[160,11569,11570],{},"is a short function written in one line",[160,11572,11573],{},"can take inputs and return a result",[160,11575,11576],{},"is often called an anonymous function because it does not need a normal function name",[160,11578,11579,11580,3539],{},"uses the ",[33,11581,11582],{},"lambda",[14,11584,1404],{},[26,11586,11588],{"className":28,"code":11587,"language":30,"meta":31,"style":31},"double = lambda x: x * 2\nprint(double(4))\n",[33,11589,11590,11609],{"__ignoreMap":31},[36,11591,11592,11595,11597,11599,11601,11603,11605,11607],{"class":38,"line":39},[36,11593,11594],{"class":42},"double ",[36,11596,47],{"class":46},[36,11598,11520],{"class":137},[36,11600,11523],{"class":3050},[36,11602,822],{"class":50},[36,11604,10827],{"class":42},[36,11606,10824],{"class":46},[36,11608,7733],{"class":54},[36,11610,11611,11613,11615,11618,11620,11623],{"class":38,"line":72},[36,11612,76],{"class":75},[36,11614,79],{"class":50},[36,11616,11617],{"class":82},"double",[36,11619,79],{"class":50},[36,11621,11622],{"class":54},"4",[36,11624,104],{"class":50},[14,11626,107],{},[26,11628,11630],{"className":28,"code":11629,"language":30,"meta":31,"style":31},"8\n",[33,11631,11632],{"__ignoreMap":31},[36,11633,11634],{"class":38,"line":39},[36,11635,11629],{"class":54},[14,11637,11638],{},"Even though lambda functions are often called anonymous, you can still store one in a variable, as shown above.",[14,11640,11641,11642,351],{},"If you are new to functions, see ",[347,11643,11644],{"href":9807},"what a function is in Python",[21,11646,11648],{"id":11647},"basic-syntax","Basic syntax",[14,11650,11651],{},"The basic pattern is:",[26,11653,11655],{"className":28,"code":11654,"language":30,"meta":31,"style":31},"lambda parameters: expression\n",[33,11656,11657],{"__ignoreMap":31},[36,11658,11659,11661,11664,11666],{"class":38,"line":39},[36,11660,11582],{"class":137},[36,11662,11663],{"class":3050}," parameters",[36,11665,822],{"class":50},[36,11667,11668],{"class":42}," expression\n",[14,11670,11671],{},"How to read this:",[157,11673,11674,11677,11680],{},[160,11675,11676],{},"the part before the colon is the input",[160,11678,11679],{},"the part after the colon is the value that gets returned",[160,11681,11682,11683,11685],{},"there is no ",[33,11684,9147],{}," keyword inside a lambda",[14,11687,197],{},[26,11689,11691],{"className":28,"code":11690,"language":30,"meta":31,"style":31},"add = lambda a, b: a + b\nprint(add(3, 7))\n",[33,11692,11693,11717],{"__ignoreMap":31},[36,11694,11695,11698,11700,11702,11705,11707,11709,11711,11713,11715],{"class":38,"line":39},[36,11696,11697],{"class":42},"add ",[36,11699,47],{"class":46},[36,11701,11520],{"class":137},[36,11703,11704],{"class":3050}," a",[36,11706,58],{"class":50},[36,11708,9194],{"class":3050},[36,11710,822],{"class":50},[36,11712,9204],{"class":42},[36,11714,4763],{"class":46},[36,11716,9209],{"class":42},[36,11718,11719,11721,11723,11726,11728,11730,11732,11734],{"class":38,"line":72},[36,11720,76],{"class":75},[36,11722,79],{"class":50},[36,11724,11725],{"class":82},"add",[36,11727,79],{"class":50},[36,11729,1247],{"class":54},[36,11731,58],{"class":50},[36,11733,1566],{"class":54},[36,11735,104],{"class":50},[14,11737,107],{},[26,11739,11740],{"className":28,"code":10792,"language":30,"meta":31,"style":31},[33,11741,11742],{"__ignoreMap":31},[36,11743,11744],{"class":38,"line":39},[36,11745,10792],{"class":54},[14,11747,6187],{},[157,11749,11750,11757],{},[160,11751,11752,1281,11754,11756],{},[33,11753,347],{},[33,11755,721],{}," are parameters",[160,11758,11759,11761],{},[33,11760,9261],{}," is the expression that Python returns",[14,11763,11764,11765,1281,11769,351],{},"If you want to understand the difference between inputs in a function definition and values passed into a function call, see ",[347,11766,11768],{"href":11767},"\u002Fglossary\u002Fwhat-is-a-parameter-in-python","what a parameter is in Python",[347,11770,11772],{"href":11771},"\u002Fglossary\u002Fwhat-is-an-argument-in-python","what an argument is in Python",[21,11774,11776],{"id":11775},"how-it-compares-to-def","How it compares to def",[14,11778,11779,11780,11782],{},"A regular function uses ",[33,11781,8785],{}," and can contain multiple lines. A lambda function is limited to a single expression.",[14,11784,11785],{},"These two examples do the same thing:",[26,11787,11789],{"className":28,"code":11788,"language":30,"meta":31,"style":31},"def square(x):\n    return x * x\n\nprint(square(5))\n",[33,11790,11791,11804,11814,11818],{"__ignoreMap":31},[36,11792,11793,11795,11798,11800,11802],{"class":38,"line":39},[36,11794,8785],{"class":137},[36,11796,11797],{"class":3082}," square",[36,11799,79],{"class":50},[36,11801,5306],{"class":3050},[36,11803,3054],{"class":50},[36,11805,11806,11808,11810,11812],{"class":38,"line":72},[36,11807,9201],{"class":1718},[36,11809,10827],{"class":42},[36,11811,10824],{"class":46},[36,11813,11532],{"class":42},[36,11815,11816],{"class":38,"line":89},[36,11817,476],{"emptyLinePlaceholder":475},[36,11819,11820,11822,11824,11826,11828,11830],{"class":38,"line":496},[36,11821,76],{"class":75},[36,11823,79],{"class":50},[36,11825,11541],{"class":82},[36,11827,79],{"class":50},[36,11829,1243],{"class":54},[36,11831,104],{"class":50},[26,11833,11834],{"className":28,"code":11508,"language":30,"meta":31,"style":31},[33,11835,11836,11854],{"__ignoreMap":31},[36,11837,11838,11840,11842,11844,11846,11848,11850,11852],{"class":38,"line":39},[36,11839,11515],{"class":42},[36,11841,47],{"class":46},[36,11843,11520],{"class":137},[36,11845,11523],{"class":3050},[36,11847,822],{"class":50},[36,11849,10827],{"class":42},[36,11851,10824],{"class":46},[36,11853,11532],{"class":42},[36,11855,11856,11858,11860,11862,11864,11866],{"class":38,"line":72},[36,11857,76],{"class":75},[36,11859,79],{"class":50},[36,11861,11541],{"class":82},[36,11863,79],{"class":50},[36,11865,1243],{"class":54},[36,11867,104],{"class":50},[14,11869,11870],{},"Output for both:",[26,11872,11873],{"className":28,"code":6092,"language":30,"meta":31,"style":31},[33,11874,11875],{"__ignoreMap":31},[36,11876,11877],{"class":38,"line":39},[36,11878,6092],{"class":54},[14,11880,2489,11881,6751],{},[33,11882,8785],{},[157,11884,11885,11888,11891,11894],{},[160,11886,11887],{},"the logic is longer",[160,11889,11890],{},"you need multiple steps",[160,11892,11893],{},"you want a clear function name",[160,11895,11896],{},"readability matters more than saving space",[14,11898,2489,11899,6751],{},[33,11900,11582],{},[157,11902,11903,11906,11909],{},[160,11904,11905],{},"the operation is short",[160,11907,11908],{},"you only need the function once",[160,11910,11911],{},"the code stays easy to read",[14,11913,11914,11915,1281,11918,351],{},"For a broader explanation, see ",[347,11916,8993],{"href":11917},"\u002Flearn\u002Fpython-functions-explained",[347,11919,11921],{"href":11920},"\u002Flearn\u002Flambda-functions-in-python-explained","lambda functions in Python explained",[21,11923,11925],{"id":11924},"where-beginners-may-see-lambda","Where beginners may see lambda",[14,11927,11928],{},"Beginners often see lambda used when a function is passed into another function.",[1065,11930,11932,11933],{"id":11931},"with-sorted","With ",[33,11934,11935],{},"sorted()",[14,11937,11938],{},"A common example is sorting data by a specific part of each item.",[26,11940,11942],{"className":28,"code":11941,"language":30,"meta":31,"style":31},"students = [(\"Maya\", 82), (\"Leo\", 91), (\"Ava\", 78)]\n\nresult = sorted(students, key=lambda student: student[1])\nprint(result)\n",[33,11943,11944,11999,12003,12040],{"__ignoreMap":31},[36,11945,11946,11948,11950,11953,11955,11957,11959,11961,11964,11967,11969,11971,11973,11975,11977,11980,11982,11984,11986,11989,11991,11993,11996],{"class":38,"line":39},[36,11947,284],{"class":42},[36,11949,47],{"class":46},[36,11951,11952],{"class":50}," [(",[36,11954,214],{"class":141},[36,11956,293],{"class":145},[36,11958,214],{"class":141},[36,11960,58],{"class":50},[36,11962,11963],{"class":54}," 82",[36,11965,11966],{"class":50},"),",[36,11968,744],{"class":50},[36,11970,214],{"class":141},[36,11972,302],{"class":145},[36,11974,214],{"class":141},[36,11976,58],{"class":50},[36,11978,11979],{"class":54}," 91",[36,11981,11966],{"class":50},[36,11983,744],{"class":50},[36,11985,214],{"class":141},[36,11987,11988],{"class":145},"Ava",[36,11990,214],{"class":141},[36,11992,58],{"class":50},[36,11994,11995],{"class":54}," 78",[36,11997,11998],{"class":50},")]\n",[36,12000,12001],{"class":38,"line":72},[36,12002,476],{"emptyLinePlaceholder":475},[36,12004,12005,12007,12009,12012,12014,12017,12019,12023,12025,12027,12030,12032,12034,12036,12038],{"class":38,"line":89},[36,12006,7723],{"class":42},[36,12008,47],{"class":46},[36,12010,12011],{"class":75}," sorted",[36,12013,79],{"class":50},[36,12015,12016],{"class":82},"students",[36,12018,58],{"class":50},[36,12020,12022],{"class":12021},"s99_P"," key",[36,12024,47],{"class":46},[36,12026,11582],{"class":137},[36,12028,12029],{"class":3050}," student",[36,12031,822],{"class":50},[36,12033,12029],{"class":82},[36,12035,117],{"class":50},[36,12037,55],{"class":54},[36,12039,493],{"class":50},[36,12041,12042,12044,12046,12048],{"class":38,"line":496},[36,12043,76],{"class":75},[36,12045,79],{"class":50},[36,12047,7767],{"class":82},[36,12049,86],{"class":50},[14,12051,107],{},[26,12053,12055],{"className":28,"code":12054,"language":30,"meta":31,"style":31},"[('Ava', 78), ('Maya', 82), ('Leo', 91)]\n",[33,12056,12057],{"__ignoreMap":31},[36,12058,12059,12062,12064,12066,12068,12070,12072,12074,12076,12078,12080,12082,12084,12086,12088,12090,12092,12094,12096,12098,12100],{"class":38,"line":39},[36,12060,12061],{"class":50},"[(",[36,12063,149],{"class":141},[36,12065,11988],{"class":145},[36,12067,149],{"class":141},[36,12069,58],{"class":50},[36,12071,11995],{"class":54},[36,12073,11966],{"class":50},[36,12075,744],{"class":50},[36,12077,149],{"class":141},[36,12079,293],{"class":145},[36,12081,149],{"class":141},[36,12083,58],{"class":50},[36,12085,11963],{"class":54},[36,12087,11966],{"class":50},[36,12089,744],{"class":50},[36,12091,149],{"class":141},[36,12093,302],{"class":145},[36,12095,149],{"class":141},[36,12097,58],{"class":50},[36,12099,11979],{"class":54},[36,12101,11998],{"class":50},[14,12103,243,12104,12107,12108,12110],{},[33,12105,12106],{},"lambda student: student[1]"," tells ",[33,12109,11935],{}," to use the second value in each tuple for sorting.",[14,12112,12113,12114,12119,12120,12122],{},"See ",[347,12115,1665,12117,2816],{"href":12116},"\u002Freference\u002Fpython-sorted-function-explained",[33,12118,11935],{}," if you want to learn more about the ",[33,12121,6114],{}," argument.",[1065,12124,11932,12126],{"id":12125},"with-map",[33,12127,11498],{},[26,12129,12131],{"className":28,"code":12130,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4]\n\nresult = list(map(lambda x: x * 2, numbers))\nprint(result)\n",[33,12132,12133,12157,12161,12194],{"__ignoreMap":31},[36,12134,12135,12137,12139,12141,12143,12145,12147,12149,12151,12153,12155],{"class":38,"line":39},[36,12136,43],{"class":42},[36,12138,47],{"class":46},[36,12140,51],{"class":50},[36,12142,55],{"class":54},[36,12144,58],{"class":50},[36,12146,61],{"class":54},[36,12148,58],{"class":50},[36,12150,66],{"class":54},[36,12152,58],{"class":50},[36,12154,1549],{"class":54},[36,12156,69],{"class":50},[36,12158,12159],{"class":38,"line":72},[36,12160,476],{"emptyLinePlaceholder":475},[36,12162,12163,12165,12167,12169,12171,12174,12176,12178,12180,12182,12184,12186,12188,12190,12192],{"class":38,"line":89},[36,12164,7723],{"class":42},[36,12166,47],{"class":46},[36,12168,10542],{"class":96},[36,12170,79],{"class":50},[36,12172,12173],{"class":75},"map",[36,12175,79],{"class":50},[36,12177,11582],{"class":137},[36,12179,11523],{"class":3050},[36,12181,822],{"class":50},[36,12183,10827],{"class":82},[36,12185,10824],{"class":46},[36,12187,61],{"class":54},[36,12189,58],{"class":50},[36,12191,9874],{"class":82},[36,12193,104],{"class":50},[36,12195,12196,12198,12200,12202],{"class":38,"line":496},[36,12197,76],{"class":75},[36,12199,79],{"class":50},[36,12201,7767],{"class":82},[36,12203,86],{"class":50},[14,12205,107],{},[26,12207,12209],{"className":28,"code":12208,"language":30,"meta":31,"style":31},"[2, 4, 6, 8]\n",[33,12210,12211],{"__ignoreMap":31},[36,12212,12213,12215,12217,12219,12221,12223,12226,12228,12231],{"class":38,"line":39},[36,12214,117],{"class":50},[36,12216,1543],{"class":54},[36,12218,58],{"class":50},[36,12220,1549],{"class":54},[36,12222,58],{"class":50},[36,12224,12225],{"class":54}," 6",[36,12227,58],{"class":50},[36,12229,12230],{"class":54}," 8",[36,12232,69],{"class":50},[14,12234,12235],{},"This applies the lambda function to each item in the list.",[14,12237,12113,12238,351],{},[347,12239,1665,12241,2816],{"href":12240},"\u002Freference\u002Fpython-map-function-explained",[33,12242,11498],{},[1065,12244,11932,12246],{"id":12245},"with-filter",[33,12247,11501],{},[26,12249,12251],{"className":28,"code":12250,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4, 5, 6]\n\nresult = list(filter(lambda x: x % 2 == 0, numbers))\nprint(result)\n",[33,12252,12253,12285,12289,12327],{"__ignoreMap":31},[36,12254,12255,12257,12259,12261,12263,12265,12267,12269,12271,12273,12275,12277,12279,12281,12283],{"class":38,"line":39},[36,12256,43],{"class":42},[36,12258,47],{"class":46},[36,12260,51],{"class":50},[36,12262,55],{"class":54},[36,12264,58],{"class":50},[36,12266,61],{"class":54},[36,12268,58],{"class":50},[36,12270,66],{"class":54},[36,12272,58],{"class":50},[36,12274,1549],{"class":54},[36,12276,58],{"class":50},[36,12278,5043],{"class":54},[36,12280,58],{"class":50},[36,12282,12225],{"class":54},[36,12284,69],{"class":50},[36,12286,12287],{"class":38,"line":72},[36,12288,476],{"emptyLinePlaceholder":475},[36,12290,12291,12293,12295,12297,12299,12302,12304,12306,12308,12310,12312,12315,12317,12319,12321,12323,12325],{"class":38,"line":89},[36,12292,7723],{"class":42},[36,12294,47],{"class":46},[36,12296,10542],{"class":96},[36,12298,79],{"class":50},[36,12300,12301],{"class":75},"filter",[36,12303,79],{"class":50},[36,12305,11582],{"class":137},[36,12307,11523],{"class":3050},[36,12309,822],{"class":50},[36,12311,10827],{"class":82},[36,12313,12314],{"class":46},"%",[36,12316,61],{"class":54},[36,12318,1546],{"class":46},[36,12320,1993],{"class":54},[36,12322,58],{"class":50},[36,12324,9874],{"class":82},[36,12326,104],{"class":50},[36,12328,12329,12331,12333,12335],{"class":38,"line":496},[36,12330,76],{"class":75},[36,12332,79],{"class":50},[36,12334,7767],{"class":82},[36,12336,86],{"class":50},[14,12338,107],{},[26,12340,12342],{"className":28,"code":12341,"language":30,"meta":31,"style":31},"[2, 4, 6]\n",[33,12343,12344],{"__ignoreMap":31},[36,12345,12346,12348,12350,12352,12354,12356,12358],{"class":38,"line":39},[36,12347,117],{"class":50},[36,12349,1543],{"class":54},[36,12351,58],{"class":50},[36,12353,1549],{"class":54},[36,12355,58],{"class":50},[36,12357,12225],{"class":54},[36,12359,69],{"class":50},[14,12361,12362,12363,351],{},"This keeps only the values where the lambda returns ",[33,12364,1179],{},[14,12366,12113,12367,351],{},[347,12368,1665,12370,2816],{"href":12369},"\u002Freference\u002Fpython-filter-function-explained",[33,12371,11501],{},[21,12373,12375],{"id":12374},"when-to-use-it","When to use it",[14,12377,12378],{},"Use a lambda function when:",[157,12380,12381,12384,12387],{},[160,12382,12383],{},"you need a small one-time function",[160,12385,12386],{},"the code is still easy to read",[160,12388,12389,12390,12392],{},"writing a full ",[33,12391,8785],{}," function would feel unnecessary",[14,12394,12395],{},"Avoid lambda when:",[157,12397,12398,12401,12404,12407],{},[160,12399,12400],{},"the expression becomes hard to understand",[160,12402,12403],{},"you need multiple statements",[160,12405,12406],{},"the function will be reused often",[160,12408,12409],{},"a named function would make the code clearer",[14,12411,12412,12413,12415],{},"A good rule for beginners is simple: if the lambda makes the code harder to read, use ",[33,12414,8785],{}," instead.",[21,12417,2245],{"id":2244},[14,12419,12420],{},"Some common points are easy to misunderstand:",[157,12422,12423,12429,12435,12441],{},[160,12424,12425,12428],{},[331,12426,12427],{},"Lambda is still a function."," It is not a special separate kind of thing.",[160,12430,12431,12434],{},[331,12432,12433],{},"It can be stored in a variable."," You can assign it just like other values.",[160,12436,12437,12440],{},[331,12438,12439],{},"It does not run automatically."," Creating a lambda only creates the function. You still need to call it.",[160,12442,12443,12448],{},[331,12444,12445,12446,351],{},"It is not always better than ",[33,12447,8785],{}," Shorter code is not always clearer code.",[14,12450,197],{},[26,12452,12454],{"className":28,"code":12453,"language":30,"meta":31,"style":31},"my_lambda = lambda x: x + 1\n\nprint(type(my_lambda))\nprint(my_lambda(3))\n",[33,12455,12456,12475,12479,12494],{"__ignoreMap":31},[36,12457,12458,12461,12463,12465,12467,12469,12471,12473],{"class":38,"line":39},[36,12459,12460],{"class":42},"my_lambda ",[36,12462,47],{"class":46},[36,12464,11520],{"class":137},[36,12466,11523],{"class":3050},[36,12468,822],{"class":50},[36,12470,10827],{"class":42},[36,12472,4763],{"class":46},[36,12474,9884],{"class":54},[36,12476,12477],{"class":38,"line":72},[36,12478,476],{"emptyLinePlaceholder":475},[36,12480,12481,12483,12485,12487,12489,12492],{"class":38,"line":89},[36,12482,76],{"class":75},[36,12484,79],{"class":50},[36,12486,97],{"class":96},[36,12488,79],{"class":50},[36,12490,12491],{"class":82},"my_lambda",[36,12493,104],{"class":50},[36,12495,12496,12498,12500,12502,12504,12506],{"class":38,"line":496},[36,12497,76],{"class":75},[36,12499,79],{"class":50},[36,12501,12491],{"class":82},[36,12503,79],{"class":50},[36,12505,1247],{"class":54},[36,12507,104],{"class":50},[14,12509,4527],{},[26,12511,12513],{"className":28,"code":12512,"language":30,"meta":31,"style":31},"\u003Cclass 'function'>\n4\n",[33,12514,12515,12530],{"__ignoreMap":31},[36,12516,12517,12519,12521,12523,12526,12528],{"class":38,"line":39},[36,12518,134],{"class":46},[36,12520,138],{"class":137},[36,12522,142],{"class":141},[36,12524,12525],{"class":145},"function",[36,12527,149],{"class":141},[36,12529,152],{"class":46},[36,12531,12532],{"class":38,"line":72},[36,12533,10891],{"class":54},[14,12535,12536],{},"This shows that a lambda is a normal function object.",[21,12538,905],{"id":904},[14,12540,8484],{},[157,12542,12543,12546,12549,12552],{},[160,12544,12545],{},"Thinking lambda is a different kind of object instead of a function",[160,12547,12548],{},"Trying to write multiple statements inside a lambda",[160,12550,12551],{},"Using lambda where a normal named function would be easier to read",[160,12553,12554],{},"Confusing parameters with arguments in lambda examples",[14,12556,12557],{},"For quick checking, these commands can help:",[26,12559,12561],{"className":28,"code":12560,"language":30,"meta":31,"style":31},"print(type(my_lambda))\nprint(my_lambda(3))\nhelp(sorted)\nhelp(map)\nhelp(filter)\n",[33,12562,12563,12577,12591,12602,12612],{"__ignoreMap":31},[36,12564,12565,12567,12569,12571,12573,12575],{"class":38,"line":39},[36,12566,76],{"class":75},[36,12568,79],{"class":50},[36,12570,97],{"class":96},[36,12572,79],{"class":50},[36,12574,12491],{"class":82},[36,12576,104],{"class":50},[36,12578,12579,12581,12583,12585,12587,12589],{"class":38,"line":72},[36,12580,76],{"class":75},[36,12582,79],{"class":50},[36,12584,12491],{"class":82},[36,12586,79],{"class":50},[36,12588,1247],{"class":54},[36,12590,104],{"class":50},[36,12592,12593,12595,12597,12600],{"class":38,"line":89},[36,12594,4392],{"class":75},[36,12596,79],{"class":50},[36,12598,12599],{"class":75},"sorted",[36,12601,86],{"class":50},[36,12603,12604,12606,12608,12610],{"class":38,"line":496},[36,12605,4392],{"class":75},[36,12607,79],{"class":50},[36,12609,12173],{"class":75},[36,12611,86],{"class":50},[36,12613,12614,12616,12618,12620],{"class":38,"line":501},[36,12615,4392],{"class":75},[36,12617,79],{"class":50},[36,12619,12301],{"class":75},[36,12621,86],{"class":50},[14,12623,12624],{},"These can help you confirm:",[157,12626,12627,12630,12633],{},[160,12628,12629],{},"whether your lambda is really a function",[160,12631,12632],{},"what result it returns",[160,12634,12635,12636,5950,12638,5953,12640,12642],{},"how functions like ",[33,12637,11935],{},[33,12639,11498],{},[33,12641,11501],{}," expect lambda to be used",[21,12644,1063],{"id":1062},[1065,12646,12648],{"id":12647},"does-a-lambda-function-return-a-value","Does a lambda function return a value?",[14,12650,12651],{},"Yes. The expression after the colon is returned automatically.",[1065,12653,12655],{"id":12654},"can-a-lambda-function-have-more-than-one-parameter","Can a lambda function have more than one parameter?",[14,12657,12658],{},"Yes. You can write multiple parameters separated by commas.",[14,12660,197],{},[26,12662,12664],{"className":28,"code":12663,"language":30,"meta":31,"style":31},"multiply = lambda a, b: a * b\nprint(multiply(2, 5))\n",[33,12665,12666,12689],{"__ignoreMap":31},[36,12667,12668,12671,12673,12675,12677,12679,12681,12683,12685,12687],{"class":38,"line":39},[36,12669,12670],{"class":42},"multiply ",[36,12672,47],{"class":46},[36,12674,11520],{"class":137},[36,12676,11704],{"class":3050},[36,12678,58],{"class":50},[36,12680,9194],{"class":3050},[36,12682,822],{"class":50},[36,12684,9204],{"class":42},[36,12686,10824],{"class":46},[36,12688,9209],{"class":42},[36,12690,12691,12693,12695,12698,12700,12702,12704,12706],{"class":38,"line":72},[36,12692,76],{"class":75},[36,12694,79],{"class":50},[36,12696,12697],{"class":82},"multiply",[36,12699,79],{"class":50},[36,12701,1543],{"class":54},[36,12703,58],{"class":50},[36,12705,5043],{"class":54},[36,12707,104],{"class":50},[1065,12709,12711],{"id":12710},"can-a-lambda-function-contain-multiple-lines","Can a lambda function contain multiple lines?",[14,12713,12714],{},"No. A lambda is limited to one expression.",[1065,12716,12718],{"id":12717},"should-i-always-use-lambda-instead-of-def","Should I always use lambda instead of def?",[14,12720,12721,12722,12724],{},"No. Use ",[33,12723,8785],{}," when the function is longer, reused, or needs to be easier to read.",[21,12726,1105],{"id":1104},[157,12728,12729,12734,12738,12743,12747,12751,12757,12763],{},[160,12730,12731],{},[347,12732,12733],{"href":11920},"Lambda functions in Python explained",[160,12735,12736],{},[347,12737,8993],{"href":11917},[160,12739,12740],{},[347,12741,12742],{"href":9807},"What is a function in Python?",[160,12744,12745],{},[347,12746,9758],{"href":11767},[160,12748,12749],{},[347,12750,9763],{"href":11771},[160,12752,12753],{},[347,12754,1665,12755,2816],{"href":12116},[33,12756,11935],{},[160,12758,12759],{},[347,12760,1665,12761,2816],{"href":12240},[33,12762,11498],{},[160,12764,12765],{},[347,12766,1665,12767,2816],{"href":12369},[33,12768,11501],{},[1137,12770,12771],{},"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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":12773},[12774,12775,12776,12777,12785,12786,12787,12788,12794],{"id":6100,"depth":72,"text":6101},{"id":11647,"depth":72,"text":11648},{"id":11775,"depth":72,"text":11776},{"id":11924,"depth":72,"text":11925,"children":12778},[12779,12781,12783],{"id":11931,"depth":89,"text":12780},"With sorted()",{"id":12125,"depth":89,"text":12782},"With map()",{"id":12245,"depth":89,"text":12784},"With filter()",{"id":12374,"depth":72,"text":12375},{"id":2244,"depth":72,"text":2245},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":12789},[12790,12791,12792,12793],{"id":12647,"depth":89,"text":12648},{"id":12654,"depth":89,"text":12655},{"id":12710,"depth":89,"text":12711},{"id":12717,"depth":89,"text":12718},{"id":1104,"depth":72,"text":1105},"Master what is a lambda function in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-lambda-function-in-python",{"title":11484,"description":12795},"glossary\u002Fwhat-is-a-lambda-function-in-python","1duFT-oZBMjE43zr-Wd_Oj61ufSUSXHoWaFpR-MM4hc",{"id":12802,"title":12803,"body":12804,"description":14738,"extension":1159,"meta":14739,"navigation":475,"path":14740,"seo":14741,"stem":14742,"__hash__":14743},"content\u002Fglossary\u002Fwhat-is-a-list-comprehension-in-python.md","What Is a List Comprehension in Python?",{"type":7,"value":12805,"toc":14713},[12806,12809,12812,12815,12817,12887,12890,12892,12903,12909,12913,12916,12939,12942,12963,12965,13032,13034,13052,13059,13063,13066,13093,13098,13100,13186,13193,13196,13200,13203,13217,13220,13223,13315,13318,13389,13392,13405,13407,13410,13413,13424,13427,13513,13519,13530,13533,13539,13543,13546,13549,13552,13555,13642,13645,13712,13715,13717,13720,13731,13733,13820,13826,13829,13901,13903,13906,13910,13913,13935,13938,14001,14008,14010,14034,14037,14099,14103,14106,14109,14217,14221,14224,14287,14290,14357,14363,14366,14369,14404,14407,14470,14473,14570,14572,14576,14579,14583,14589,14591,14665,14669,14672,14676,14682,14684,14710],[10,12807,12803],{"id":12808},"what-is-a-list-comprehension-in-python",[14,12810,12811],{},"A list comprehension is a short, readable way to create a new list in Python.",[14,12813,12814],{},"It lets you combine a loop and the value you want to build inside square brackets. Beginners often use it to transform data, such as squaring numbers, or to filter data, such as keeping only even numbers.",[14,12816,1404],{},[26,12818,12820],{"className":28,"code":12819,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4]\nsquares = [n * n for n in numbers]\nprint(squares)\n# [1, 4, 9, 16]\n",[33,12821,12822,12846,12871,12882],{"__ignoreMap":31},[36,12823,12824,12826,12828,12830,12832,12834,12836,12838,12840,12842,12844],{"class":38,"line":39},[36,12825,43],{"class":42},[36,12827,47],{"class":46},[36,12829,51],{"class":50},[36,12831,55],{"class":54},[36,12833,58],{"class":50},[36,12835,61],{"class":54},[36,12837,58],{"class":50},[36,12839,66],{"class":54},[36,12841,58],{"class":50},[36,12843,1549],{"class":54},[36,12845,69],{"class":50},[36,12847,12848,12850,12852,12854,12857,12859,12861,12863,12865,12867,12869],{"class":38,"line":72},[36,12849,10815],{"class":42},[36,12851,47],{"class":46},[36,12853,51],{"class":50},[36,12855,12856],{"class":42},"n ",[36,12858,10824],{"class":46},[36,12860,9908],{"class":42},[36,12862,9852],{"class":1718},[36,12864,9908],{"class":42},[36,12866,9911],{"class":1718},[36,12868,9874],{"class":42},[36,12870,69],{"class":50},[36,12872,12873,12875,12877,12880],{"class":38,"line":89},[36,12874,76],{"class":75},[36,12876,79],{"class":50},[36,12878,12879],{"class":82},"squares",[36,12881,86],{"class":50},[36,12883,12884],{"class":38,"line":496},[36,12885,12886],{"class":2570},"# [1, 4, 9, 16]\n",[14,12888,12889],{},"A list comprehension creates a new list in one line from another iterable.",[21,12891,6101],{"id":6100},[157,12893,12894,12897,12900],{},[160,12895,12896],{},"A list comprehension is a short way to create a new list.",[160,12898,12899],{},"It combines a loop and an expression inside square brackets.",[160,12901,12902],{},"It is often used when you want to transform or filter items.",[14,12904,12905,12906,351],{},"If you are new to Python lists, see ",[347,12907,12908],{"href":1161},"what a list is in Python",[21,12910,12912],{"id":12911},"basic-pattern","Basic pattern",[14,12914,12915],{},"The basic form is:",[26,12917,12919],{"className":28,"code":12918,"language":30,"meta":31,"style":31},"[expression for item in iterable]\n",[33,12920,12921],{"__ignoreMap":31},[36,12922,12923,12925,12928,12930,12932,12934,12937],{"class":38,"line":39},[36,12924,117],{"class":50},[36,12926,12927],{"class":42},"expression ",[36,12929,9852],{"class":1718},[36,12931,10439],{"class":42},[36,12933,9911],{"class":1718},[36,12935,12936],{"class":42}," iterable",[36,12938,69],{"class":50},[14,12940,12941],{},"What each part means:",[157,12943,12944,12950,12955],{},[160,12945,12946,12949],{},[33,12947,12948],{},"expression"," = what each new item should become",[160,12951,12952,12954],{},[33,12953,10455],{}," = each value from the input sequence",[160,12956,12957,12959,12960],{},[33,12958,10621],{}," = the thing you loop over, such as a list, string, tuple, or ",[33,12961,12962],{},"range",[14,12964,197],{},[26,12966,12968],{"className":28,"code":12967,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\ndoubled = [n * 2 for n in numbers]\nprint(doubled)\n# [2, 4, 6]\n",[33,12969,12970,12990,13016,13027],{"__ignoreMap":31},[36,12971,12972,12974,12976,12978,12980,12982,12984,12986,12988],{"class":38,"line":39},[36,12973,43],{"class":42},[36,12975,47],{"class":46},[36,12977,51],{"class":50},[36,12979,55],{"class":54},[36,12981,58],{"class":50},[36,12983,61],{"class":54},[36,12985,58],{"class":50},[36,12987,66],{"class":54},[36,12989,69],{"class":50},[36,12991,12992,12995,12997,12999,13001,13003,13005,13008,13010,13012,13014],{"class":38,"line":72},[36,12993,12994],{"class":42},"doubled ",[36,12996,47],{"class":46},[36,12998,51],{"class":50},[36,13000,12856],{"class":42},[36,13002,10824],{"class":46},[36,13004,61],{"class":54},[36,13006,13007],{"class":1718}," for",[36,13009,9908],{"class":42},[36,13011,9911],{"class":1718},[36,13013,9874],{"class":42},[36,13015,69],{"class":50},[36,13017,13018,13020,13022,13025],{"class":38,"line":89},[36,13019,76],{"class":75},[36,13021,79],{"class":50},[36,13023,13024],{"class":82},"doubled",[36,13026,86],{"class":50},[36,13028,13029],{"class":38,"line":496},[36,13030,13031],{"class":2570},"# [2, 4, 6]\n",[14,13033,6187],{},[157,13035,13036,13041,13046],{},[160,13037,13038,13040],{},[33,13039,83],{}," is the iterable",[160,13042,13043,13045],{},[33,13044,9924],{}," is each item",[160,13047,13048,13051],{},[33,13049,13050],{},"n * 2"," is the expression",[14,13053,13054,13055,351],{},"If the word iterable is unfamiliar, read ",[347,13056,13058],{"href":13057},"\u002Fglossary\u002Fwhat-is-an-iterable-in-python","what an iterable is in Python",[21,13060,13062],{"id":13061},"pattern-with-a-condition","Pattern with a condition",[14,13064,13065],{},"You can also add a condition:",[26,13067,13069],{"className":28,"code":13068,"language":30,"meta":31,"style":31},"[expression for item in iterable if condition]\n",[33,13070,13071],{"__ignoreMap":31},[36,13072,13073,13075,13077,13079,13081,13083,13086,13088,13091],{"class":38,"line":39},[36,13074,117],{"class":50},[36,13076,12927],{"class":42},[36,13078,9852],{"class":1718},[36,13080,10439],{"class":42},[36,13082,9911],{"class":1718},[36,13084,13085],{"class":42}," iterable ",[36,13087,1192],{"class":1718},[36,13089,13090],{"class":42}," condition",[36,13092,69],{"class":50},[14,13094,3542,13095,13097],{},[33,13096,1192],{}," part filters items before they are added to the new list.",[14,13099,197],{},[26,13101,13103],{"className":28,"code":13102,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4, 5, 6]\neven_numbers = [n for n in numbers if n % 2 == 0]\nprint(even_numbers)\n# [2, 4, 6]\n",[33,13104,13105,13137,13171,13182],{"__ignoreMap":31},[36,13106,13107,13109,13111,13113,13115,13117,13119,13121,13123,13125,13127,13129,13131,13133,13135],{"class":38,"line":39},[36,13108,43],{"class":42},[36,13110,47],{"class":46},[36,13112,51],{"class":50},[36,13114,55],{"class":54},[36,13116,58],{"class":50},[36,13118,61],{"class":54},[36,13120,58],{"class":50},[36,13122,66],{"class":54},[36,13124,58],{"class":50},[36,13126,1549],{"class":54},[36,13128,58],{"class":50},[36,13130,5043],{"class":54},[36,13132,58],{"class":50},[36,13134,12225],{"class":54},[36,13136,69],{"class":50},[36,13138,13139,13142,13144,13146,13148,13150,13152,13154,13157,13159,13161,13163,13165,13167,13169],{"class":38,"line":72},[36,13140,13141],{"class":42},"even_numbers ",[36,13143,47],{"class":46},[36,13145,51],{"class":50},[36,13147,12856],{"class":42},[36,13149,9852],{"class":1718},[36,13151,9908],{"class":42},[36,13153,9911],{"class":1718},[36,13155,13156],{"class":42}," numbers ",[36,13158,1192],{"class":1718},[36,13160,9908],{"class":42},[36,13162,12314],{"class":46},[36,13164,61],{"class":54},[36,13166,1546],{"class":46},[36,13168,1993],{"class":54},[36,13170,69],{"class":50},[36,13172,13173,13175,13177,13180],{"class":38,"line":89},[36,13174,76],{"class":75},[36,13176,79],{"class":50},[36,13178,13179],{"class":82},"even_numbers",[36,13181,86],{"class":50},[36,13183,13184],{"class":38,"line":496},[36,13185,13031],{"class":2570},[14,13187,13188,13189,13192],{},"This keeps only the numbers where ",[33,13190,13191],{},"n % 2 == 0"," is true.",[14,13194,13195],{},"This pattern is useful when you want to remove unwanted values while building a new list.",[21,13197,13199],{"id":13198},"why-beginners-use-it","Why beginners use it",[14,13201,13202],{},"Beginners use list comprehensions because they are:",[157,13204,13205,13211,13214],{},[160,13206,13207,13208],{},"Shorter than writing a loop plus ",[33,13209,13210],{},"append()",[160,13212,13213],{},"Good for simple list creation tasks",[160,13215,13216],{},"Common in Python code, so it helps to recognize them early",[14,13218,13219],{},"For example, these two pieces of code do the same thing.",[14,13221,13222],{},"Using a normal loop:",[26,13224,13226],{"className":28,"code":13225,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4]\nsquares = []\n\nfor n in numbers:\n    squares.append(n * n)\n\nprint(squares)\n# [1, 4, 9, 16]\n",[33,13227,13228,13252,13261,13265,13277,13297,13301,13311],{"__ignoreMap":31},[36,13229,13230,13232,13234,13236,13238,13240,13242,13244,13246,13248,13250],{"class":38,"line":39},[36,13231,43],{"class":42},[36,13233,47],{"class":46},[36,13235,51],{"class":50},[36,13237,55],{"class":54},[36,13239,58],{"class":50},[36,13241,61],{"class":54},[36,13243,58],{"class":50},[36,13245,66],{"class":54},[36,13247,58],{"class":50},[36,13249,1549],{"class":54},[36,13251,69],{"class":50},[36,13253,13254,13256,13258],{"class":38,"line":72},[36,13255,10815],{"class":42},[36,13257,47],{"class":46},[36,13259,13260],{"class":50}," []\n",[36,13262,13263],{"class":38,"line":89},[36,13264,476],{"emptyLinePlaceholder":475},[36,13266,13267,13269,13271,13273,13275],{"class":38,"line":496},[36,13268,9852],{"class":1718},[36,13270,9908],{"class":42},[36,13272,9911],{"class":1718},[36,13274,9874],{"class":42},[36,13276,1730],{"class":50},[36,13278,13279,13282,13284,13286,13288,13290,13292,13295],{"class":38,"line":501},[36,13280,13281],{"class":42},"    squares",[36,13283,351],{"class":50},[36,13285,508],{"class":82},[36,13287,79],{"class":50},[36,13289,12856],{"class":82},[36,13291,10824],{"class":46},[36,13293,13294],{"class":82}," n",[36,13296,86],{"class":50},[36,13298,13299],{"class":38,"line":522},[36,13300,476],{"emptyLinePlaceholder":475},[36,13302,13303,13305,13307,13309],{"class":38,"line":527},[36,13304,76],{"class":75},[36,13306,79],{"class":50},[36,13308,12879],{"class":82},[36,13310,86],{"class":50},[36,13312,13313],{"class":38,"line":3092},[36,13314,12886],{"class":2570},[14,13316,13317],{},"Using a list comprehension:",[26,13319,13321],{"className":28,"code":13320,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4]\nsquares = [n * n for n in numbers]\n\nprint(squares)\n# [1, 4, 9, 16]\n",[33,13322,13323,13347,13371,13375,13385],{"__ignoreMap":31},[36,13324,13325,13327,13329,13331,13333,13335,13337,13339,13341,13343,13345],{"class":38,"line":39},[36,13326,43],{"class":42},[36,13328,47],{"class":46},[36,13330,51],{"class":50},[36,13332,55],{"class":54},[36,13334,58],{"class":50},[36,13336,61],{"class":54},[36,13338,58],{"class":50},[36,13340,66],{"class":54},[36,13342,58],{"class":50},[36,13344,1549],{"class":54},[36,13346,69],{"class":50},[36,13348,13349,13351,13353,13355,13357,13359,13361,13363,13365,13367,13369],{"class":38,"line":72},[36,13350,10815],{"class":42},[36,13352,47],{"class":46},[36,13354,51],{"class":50},[36,13356,12856],{"class":42},[36,13358,10824],{"class":46},[36,13360,9908],{"class":42},[36,13362,9852],{"class":1718},[36,13364,9908],{"class":42},[36,13366,9911],{"class":1718},[36,13368,9874],{"class":42},[36,13370,69],{"class":50},[36,13372,13373],{"class":38,"line":89},[36,13374,476],{"emptyLinePlaceholder":475},[36,13376,13377,13379,13381,13383],{"class":38,"line":496},[36,13378,76],{"class":75},[36,13380,79],{"class":50},[36,13382,12879],{"class":82},[36,13384,86],{"class":50},[36,13386,13387],{"class":38,"line":501},[36,13388,12886],{"class":2570},[14,13390,13391],{},"The second version is shorter, but both are correct.",[14,13393,13394,13395,13399,13400,351],{},"If you want to understand the loop version first, read ",[347,13396,13398],{"href":13397},"\u002Flearn\u002Fpython-for-loops-explained","Python for loops explained",". If you want to learn more about adding items with a loop, see the ",[347,13401,1665,13402,7353],{"href":640},[33,13403,13404],{},"list.append()",[21,13406,12375],{"id":12374},[14,13408,13409],{},"Use a list comprehension when you want to do one clear job.",[14,13411,13412],{},"Good examples:",[157,13414,13415,13418,13421],{},[160,13416,13417],{},"Simple transformations, such as making strings uppercase",[160,13419,13420],{},"Simple transformations, such as squaring numbers",[160,13422,13423],{},"Simple filtering, such as keeping only even numbers",[14,13425,13426],{},"Example with strings:",[26,13428,13430],{"className":28,"code":13429,"language":30,"meta":31,"style":31},"words = [\"cat\", \"dog\", \"bird\"]\nupper_words = [word.upper() for word in words]\nprint(upper_words)\n# ['CAT', 'DOG', 'BIRD']\n",[33,13431,13432,13468,13497,13508],{"__ignoreMap":31},[36,13433,13434,13437,13439,13441,13443,13446,13448,13450,13452,13455,13457,13459,13461,13464,13466],{"class":38,"line":39},[36,13435,13436],{"class":42},"words ",[36,13438,47],{"class":46},[36,13440,51],{"class":50},[36,13442,214],{"class":141},[36,13444,13445],{"class":145},"cat",[36,13447,214],{"class":141},[36,13449,58],{"class":50},[36,13451,224],{"class":141},[36,13453,13454],{"class":145},"dog",[36,13456,214],{"class":141},[36,13458,58],{"class":50},[36,13460,224],{"class":141},[36,13462,13463],{"class":145},"bird",[36,13465,214],{"class":141},[36,13467,69],{"class":50},[36,13469,13470,13473,13475,13477,13479,13481,13484,13486,13488,13490,13492,13495],{"class":38,"line":72},[36,13471,13472],{"class":42},"upper_words ",[36,13474,47],{"class":46},[36,13476,51],{"class":50},[36,13478,10068],{"class":42},[36,13480,351],{"class":50},[36,13482,13483],{"class":82},"upper",[36,13485,932],{"class":50},[36,13487,13007],{"class":1718},[36,13489,10053],{"class":42},[36,13491,9911],{"class":1718},[36,13493,13494],{"class":42}," words",[36,13496,69],{"class":50},[36,13498,13499,13501,13503,13506],{"class":38,"line":89},[36,13500,76],{"class":75},[36,13502,79],{"class":50},[36,13504,13505],{"class":82},"upper_words",[36,13507,86],{"class":50},[36,13509,13510],{"class":38,"line":496},[36,13511,13512],{"class":2570},"# ['CAT', 'DOG', 'BIRD']\n",[14,13514,13515,13516,13518],{},"Use a normal ",[33,13517,9852],{}," loop if:",[157,13520,13521,13524,13527],{},[160,13522,13523],{},"The logic becomes hard to read",[160,13525,13526],{},"You need many steps",[160,13528,13529],{},"You need to print, test, and update several things inside the loop",[14,13531,13532],{},"A shorter line is not always better if it becomes confusing.",[14,13534,13535,13536,351],{},"For a full lesson with more examples, see ",[347,13537,10914],{"href":13538},"\u002Flearn\u002Flist-comprehensions-in-python-explained",[21,13540,13542],{"id":13541},"how-it-compares-to-a-for-loop","How it compares to a for loop",[14,13544,13545],{},"A normal loop is often easier to read when there are many steps.",[14,13547,13548],{},"A list comprehension is better when you want to do one simple action and create a new list.",[14,13550,13551],{},"Both can produce the same result.",[14,13553,13554],{},"Example with a normal loop:",[26,13556,13558],{"className":28,"code":13557,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nresult = []\n\nfor n in numbers:\n    result.append(n + 10)\n\nprint(result)\n# [11, 12, 13]\n",[33,13559,13560,13580,13588,13592,13604,13623,13627,13637],{"__ignoreMap":31},[36,13561,13562,13564,13566,13568,13570,13572,13574,13576,13578],{"class":38,"line":39},[36,13563,43],{"class":42},[36,13565,47],{"class":46},[36,13567,51],{"class":50},[36,13569,55],{"class":54},[36,13571,58],{"class":50},[36,13573,61],{"class":54},[36,13575,58],{"class":50},[36,13577,66],{"class":54},[36,13579,69],{"class":50},[36,13581,13582,13584,13586],{"class":38,"line":72},[36,13583,7723],{"class":42},[36,13585,47],{"class":46},[36,13587,13260],{"class":50},[36,13589,13590],{"class":38,"line":89},[36,13591,476],{"emptyLinePlaceholder":475},[36,13593,13594,13596,13598,13600,13602],{"class":38,"line":496},[36,13595,9852],{"class":1718},[36,13597,9908],{"class":42},[36,13599,9911],{"class":1718},[36,13601,9874],{"class":42},[36,13603,1730],{"class":50},[36,13605,13606,13609,13611,13613,13615,13617,13619,13621],{"class":38,"line":501},[36,13607,13608],{"class":42},"    result",[36,13610,351],{"class":50},[36,13612,508],{"class":82},[36,13614,79],{"class":50},[36,13616,12856],{"class":82},[36,13618,4763],{"class":46},[36,13620,395],{"class":54},[36,13622,86],{"class":50},[36,13624,13625],{"class":38,"line":522},[36,13626,476],{"emptyLinePlaceholder":475},[36,13628,13629,13631,13633,13635],{"class":38,"line":527},[36,13630,76],{"class":75},[36,13632,79],{"class":50},[36,13634,7767],{"class":82},[36,13636,86],{"class":50},[36,13638,13639],{"class":38,"line":3092},[36,13640,13641],{"class":2570},"# [11, 12, 13]\n",[14,13643,13644],{},"The same result with a list comprehension:",[26,13646,13648],{"className":28,"code":13647,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nresult = [n + 10 for n in numbers]\n\nprint(result)\n# [11, 12, 13]\n",[33,13649,13650,13670,13694,13698,13708],{"__ignoreMap":31},[36,13651,13652,13654,13656,13658,13660,13662,13664,13666,13668],{"class":38,"line":39},[36,13653,43],{"class":42},[36,13655,47],{"class":46},[36,13657,51],{"class":50},[36,13659,55],{"class":54},[36,13661,58],{"class":50},[36,13663,61],{"class":54},[36,13665,58],{"class":50},[36,13667,66],{"class":54},[36,13669,69],{"class":50},[36,13671,13672,13674,13676,13678,13680,13682,13684,13686,13688,13690,13692],{"class":38,"line":72},[36,13673,7723],{"class":42},[36,13675,47],{"class":46},[36,13677,51],{"class":50},[36,13679,12856],{"class":42},[36,13681,4763],{"class":46},[36,13683,395],{"class":54},[36,13685,13007],{"class":1718},[36,13687,9908],{"class":42},[36,13689,9911],{"class":1718},[36,13691,9874],{"class":42},[36,13693,69],{"class":50},[36,13695,13696],{"class":38,"line":89},[36,13697,476],{"emptyLinePlaceholder":475},[36,13699,13700,13702,13704,13706],{"class":38,"line":496},[36,13701,76],{"class":75},[36,13703,79],{"class":50},[36,13705,7767],{"class":82},[36,13707,86],{"class":50},[36,13709,13710],{"class":38,"line":501},[36,13711,13641],{"class":2570},[14,13713,13714],{},"Choose the version that is easiest to understand.",[21,13716,10615],{"id":10614},[14,13718,13719],{},"A few important points:",[157,13721,13722,13725,13728],{},[160,13723,13724],{},"A list comprehension always creates a list.",[160,13726,13727],{},"It does not change the original list unless you assign the result back.",[160,13729,13730],{},"Do not confuse it with generator expressions, which use parentheses instead of square brackets.",[14,13732,197],{},[26,13734,13736],{"className":28,"code":13735,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nsquares = [n * n for n in numbers]\n\nprint(numbers)\n# [1, 2, 3]\n\nprint(squares)\n# [1, 4, 9]\n",[33,13737,13738,13758,13782,13786,13796,13801,13805,13815],{"__ignoreMap":31},[36,13739,13740,13742,13744,13746,13748,13750,13752,13754,13756],{"class":38,"line":39},[36,13741,43],{"class":42},[36,13743,47],{"class":46},[36,13745,51],{"class":50},[36,13747,55],{"class":54},[36,13749,58],{"class":50},[36,13751,61],{"class":54},[36,13753,58],{"class":50},[36,13755,66],{"class":54},[36,13757,69],{"class":50},[36,13759,13760,13762,13764,13766,13768,13770,13772,13774,13776,13778,13780],{"class":38,"line":72},[36,13761,10815],{"class":42},[36,13763,47],{"class":46},[36,13765,51],{"class":50},[36,13767,12856],{"class":42},[36,13769,10824],{"class":46},[36,13771,9908],{"class":42},[36,13773,9852],{"class":1718},[36,13775,9908],{"class":42},[36,13777,9911],{"class":1718},[36,13779,9874],{"class":42},[36,13781,69],{"class":50},[36,13783,13784],{"class":38,"line":89},[36,13785,476],{"emptyLinePlaceholder":475},[36,13787,13788,13790,13792,13794],{"class":38,"line":496},[36,13789,76],{"class":75},[36,13791,79],{"class":50},[36,13793,83],{"class":82},[36,13795,86],{"class":50},[36,13797,13798],{"class":38,"line":501},[36,13799,13800],{"class":2570},"# [1, 2, 3]\n",[36,13802,13803],{"class":38,"line":522},[36,13804,476],{"emptyLinePlaceholder":475},[36,13806,13807,13809,13811,13813],{"class":38,"line":527},[36,13808,76],{"class":75},[36,13810,79],{"class":50},[36,13812,12879],{"class":82},[36,13814,86],{"class":50},[36,13816,13817],{"class":38,"line":3092},[36,13818,13819],{"class":2570},"# [1, 4, 9]\n",[14,13821,13822,13823,13825],{},"The original ",[33,13824,83],{}," list stays the same.",[14,13827,13828],{},"You can also check the type:",[26,13830,13832],{"className":28,"code":13831,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nresult = [n * n for n in numbers]\n\nprint(type(result))\n# \u003Cclass 'list'>\n",[33,13833,13834,13854,13878,13882,13896],{"__ignoreMap":31},[36,13835,13836,13838,13840,13842,13844,13846,13848,13850,13852],{"class":38,"line":39},[36,13837,43],{"class":42},[36,13839,47],{"class":46},[36,13841,51],{"class":50},[36,13843,55],{"class":54},[36,13845,58],{"class":50},[36,13847,61],{"class":54},[36,13849,58],{"class":50},[36,13851,66],{"class":54},[36,13853,69],{"class":50},[36,13855,13856,13858,13860,13862,13864,13866,13868,13870,13872,13874,13876],{"class":38,"line":72},[36,13857,7723],{"class":42},[36,13859,47],{"class":46},[36,13861,51],{"class":50},[36,13863,12856],{"class":42},[36,13865,10824],{"class":46},[36,13867,9908],{"class":42},[36,13869,9852],{"class":1718},[36,13871,9908],{"class":42},[36,13873,9911],{"class":1718},[36,13875,9874],{"class":42},[36,13877,69],{"class":50},[36,13879,13880],{"class":38,"line":89},[36,13881,476],{"emptyLinePlaceholder":475},[36,13883,13884,13886,13888,13890,13892,13894],{"class":38,"line":496},[36,13885,76],{"class":75},[36,13887,79],{"class":50},[36,13889,97],{"class":96},[36,13891,79],{"class":50},[36,13893,7767],{"class":82},[36,13895,104],{"class":50},[36,13897,13898],{"class":38,"line":501},[36,13899,13900],{"class":2570},"# \u003Cclass 'list'>\n",[21,13902,905],{"id":904},[14,13904,13905],{},"Here are some common beginner mistakes with list comprehensions.",[1065,13907,13909],{"id":13908},"forgetting-the-square-brackets","Forgetting the square brackets",[14,13911,13912],{},"This is wrong:",[26,13914,13916],{"className":28,"code":13915,"language":30,"meta":31,"style":31},"n * n for n in numbers\n",[33,13917,13918],{"__ignoreMap":31},[36,13919,13920,13922,13924,13926,13928,13930,13932],{"class":38,"line":39},[36,13921,12856],{"class":42},[36,13923,10824],{"class":46},[36,13925,9908],{"class":42},[36,13927,9852],{"class":1718},[36,13929,9908],{"class":42},[36,13931,9911],{"class":1718},[36,13933,13934],{"class":42}," numbers\n",[14,13936,13937],{},"A list comprehension must use square brackets:",[26,13939,13941],{"className":28,"code":13940,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nsquares = [n * n for n in numbers]\nprint(squares)\n# [1, 4, 9]\n",[33,13942,13943,13963,13987,13997],{"__ignoreMap":31},[36,13944,13945,13947,13949,13951,13953,13955,13957,13959,13961],{"class":38,"line":39},[36,13946,43],{"class":42},[36,13948,47],{"class":46},[36,13950,51],{"class":50},[36,13952,55],{"class":54},[36,13954,58],{"class":50},[36,13956,61],{"class":54},[36,13958,58],{"class":50},[36,13960,66],{"class":54},[36,13962,69],{"class":50},[36,13964,13965,13967,13969,13971,13973,13975,13977,13979,13981,13983,13985],{"class":38,"line":72},[36,13966,10815],{"class":42},[36,13968,47],{"class":46},[36,13970,51],{"class":50},[36,13972,12856],{"class":42},[36,13974,10824],{"class":46},[36,13976,9908],{"class":42},[36,13978,9852],{"class":1718},[36,13980,9908],{"class":42},[36,13982,9911],{"class":1718},[36,13984,9874],{"class":42},[36,13986,69],{"class":50},[36,13988,13989,13991,13993,13995],{"class":38,"line":89},[36,13990,76],{"class":75},[36,13992,79],{"class":50},[36,13994,12879],{"class":82},[36,13996,86],{"class":50},[36,13998,13999],{"class":38,"line":496},[36,14000,13819],{"class":2570},[1065,14002,14004,14005,14007],{"id":14003},"putting-the-for-part-before-the-expression","Putting the ",[33,14006,9852],{}," part before the expression",[14,14009,13912],{},[26,14011,14013],{"className":28,"code":14012,"language":30,"meta":31,"style":31},"[for n in numbers n * n]\n",[33,14014,14015],{"__ignoreMap":31},[36,14016,14017,14019,14021,14023,14025,14028,14030,14032],{"class":38,"line":39},[36,14018,117],{"class":50},[36,14020,9852],{"class":1718},[36,14022,9908],{"class":42},[36,14024,9911],{"class":1718},[36,14026,14027],{"class":42}," numbers n ",[36,14029,10824],{"class":46},[36,14031,13294],{"class":42},[36,14033,69],{"class":50},[14,14035,14036],{},"The correct order is:",[26,14038,14039],{"className":28,"code":13940,"language":30,"meta":31,"style":31},[33,14040,14041,14061,14085,14095],{"__ignoreMap":31},[36,14042,14043,14045,14047,14049,14051,14053,14055,14057,14059],{"class":38,"line":39},[36,14044,43],{"class":42},[36,14046,47],{"class":46},[36,14048,51],{"class":50},[36,14050,55],{"class":54},[36,14052,58],{"class":50},[36,14054,61],{"class":54},[36,14056,58],{"class":50},[36,14058,66],{"class":54},[36,14060,69],{"class":50},[36,14062,14063,14065,14067,14069,14071,14073,14075,14077,14079,14081,14083],{"class":38,"line":72},[36,14064,10815],{"class":42},[36,14066,47],{"class":46},[36,14068,51],{"class":50},[36,14070,12856],{"class":42},[36,14072,10824],{"class":46},[36,14074,9908],{"class":42},[36,14076,9852],{"class":1718},[36,14078,9908],{"class":42},[36,14080,9911],{"class":1718},[36,14082,9874],{"class":42},[36,14084,69],{"class":50},[36,14086,14087,14089,14091,14093],{"class":38,"line":89},[36,14088,76],{"class":75},[36,14090,79],{"class":50},[36,14092,12879],{"class":82},[36,14094,86],{"class":50},[36,14096,14097],{"class":38,"line":496},[36,14098,13819],{"class":2570},[1065,14100,14102],{"id":14101},"trying-to-do-too-much-logic-in-one-line","Trying to do too much logic in one line",[14,14104,14105],{},"List comprehensions are best for simple cases.",[14,14107,14108],{},"If your code becomes difficult to read, use a normal loop instead:",[26,14110,14112],{"className":28,"code":14111,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4]\nresult = []\n\nfor n in numbers:\n    if n % 2 == 0:\n        result.append(n * n)\n\nprint(result)\n# [4, 16]\n",[33,14113,14114,14138,14146,14150,14162,14179,14198,14202,14212],{"__ignoreMap":31},[36,14115,14116,14118,14120,14122,14124,14126,14128,14130,14132,14134,14136],{"class":38,"line":39},[36,14117,43],{"class":42},[36,14119,47],{"class":46},[36,14121,51],{"class":50},[36,14123,55],{"class":54},[36,14125,58],{"class":50},[36,14127,61],{"class":54},[36,14129,58],{"class":50},[36,14131,66],{"class":54},[36,14133,58],{"class":50},[36,14135,1549],{"class":54},[36,14137,69],{"class":50},[36,14139,14140,14142,14144],{"class":38,"line":72},[36,14141,7723],{"class":42},[36,14143,47],{"class":46},[36,14145,13260],{"class":50},[36,14147,14148],{"class":38,"line":89},[36,14149,476],{"emptyLinePlaceholder":475},[36,14151,14152,14154,14156,14158,14160],{"class":38,"line":496},[36,14153,9852],{"class":1718},[36,14155,9908],{"class":42},[36,14157,9911],{"class":1718},[36,14159,9874],{"class":42},[36,14161,1730],{"class":50},[36,14163,14164,14167,14169,14171,14173,14175,14177],{"class":38,"line":501},[36,14165,14166],{"class":1718},"    if",[36,14168,9908],{"class":42},[36,14170,12314],{"class":46},[36,14172,61],{"class":54},[36,14174,1546],{"class":46},[36,14176,1993],{"class":54},[36,14178,1730],{"class":50},[36,14180,14181,14184,14186,14188,14190,14192,14194,14196],{"class":38,"line":522},[36,14182,14183],{"class":42},"        result",[36,14185,351],{"class":50},[36,14187,508],{"class":82},[36,14189,79],{"class":50},[36,14191,12856],{"class":82},[36,14193,10824],{"class":46},[36,14195,13294],{"class":82},[36,14197,86],{"class":50},[36,14199,14200],{"class":38,"line":527},[36,14201,476],{"emptyLinePlaceholder":475},[36,14203,14204,14206,14208,14210],{"class":38,"line":3092},[36,14205,76],{"class":75},[36,14207,79],{"class":50},[36,14209,7767],{"class":82},[36,14211,86],{"class":50},[36,14213,14214],{"class":38,"line":3118},[36,14215,14216],{"class":2570},"# [4, 16]\n",[1065,14218,14220],{"id":14219},"expecting-the-original-list-to-change-automatically","Expecting the original list to change automatically",[14,14222,14223],{},"A list comprehension creates a new list. It does not update the old one unless you assign it back.",[26,14225,14227],{"className":28,"code":14226,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\n[n * 2 for n in numbers]\n\nprint(numbers)\n# [1, 2, 3]\n",[33,14228,14229,14249,14269,14273,14283],{"__ignoreMap":31},[36,14230,14231,14233,14235,14237,14239,14241,14243,14245,14247],{"class":38,"line":39},[36,14232,43],{"class":42},[36,14234,47],{"class":46},[36,14236,51],{"class":50},[36,14238,55],{"class":54},[36,14240,58],{"class":50},[36,14242,61],{"class":54},[36,14244,58],{"class":50},[36,14246,66],{"class":54},[36,14248,69],{"class":50},[36,14250,14251,14253,14255,14257,14259,14261,14263,14265,14267],{"class":38,"line":72},[36,14252,117],{"class":50},[36,14254,12856],{"class":42},[36,14256,10824],{"class":46},[36,14258,61],{"class":54},[36,14260,13007],{"class":1718},[36,14262,9908],{"class":42},[36,14264,9911],{"class":1718},[36,14266,9874],{"class":42},[36,14268,69],{"class":50},[36,14270,14271],{"class":38,"line":89},[36,14272,476],{"emptyLinePlaceholder":475},[36,14274,14275,14277,14279,14281],{"class":38,"line":496},[36,14276,76],{"class":75},[36,14278,79],{"class":50},[36,14280,83],{"class":82},[36,14282,86],{"class":50},[36,14284,14285],{"class":38,"line":501},[36,14286,13800],{"class":2570},[14,14288,14289],{},"To replace the old list, assign the result:",[26,14291,14293],{"className":28,"code":14292,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nnumbers = [n * 2 for n in numbers]\n\nprint(numbers)\n# [2, 4, 6]\n",[33,14294,14295,14315,14339,14343,14353],{"__ignoreMap":31},[36,14296,14297,14299,14301,14303,14305,14307,14309,14311,14313],{"class":38,"line":39},[36,14298,43],{"class":42},[36,14300,47],{"class":46},[36,14302,51],{"class":50},[36,14304,55],{"class":54},[36,14306,58],{"class":50},[36,14308,61],{"class":54},[36,14310,58],{"class":50},[36,14312,66],{"class":54},[36,14314,69],{"class":50},[36,14316,14317,14319,14321,14323,14325,14327,14329,14331,14333,14335,14337],{"class":38,"line":72},[36,14318,43],{"class":42},[36,14320,47],{"class":46},[36,14322,51],{"class":50},[36,14324,12856],{"class":42},[36,14326,10824],{"class":46},[36,14328,61],{"class":54},[36,14330,13007],{"class":1718},[36,14332,9908],{"class":42},[36,14334,9911],{"class":1718},[36,14336,9874],{"class":42},[36,14338,69],{"class":50},[36,14340,14341],{"class":38,"line":89},[36,14342,476],{"emptyLinePlaceholder":475},[36,14344,14345,14347,14349,14351],{"class":38,"line":496},[36,14346,76],{"class":75},[36,14348,79],{"class":50},[36,14350,83],{"class":82},[36,14352,86],{"class":50},[36,14354,14355],{"class":38,"line":501},[36,14356,13031],{"class":2570},[1065,14358,2639,14360,14362],{"id":14359},"using-append-inside-the-comprehension",[33,14361,13210],{}," inside the comprehension",[14,14364,14365],{},"This is not how list comprehensions work.",[14,14367,14368],{},"Wrong idea:",[26,14370,14372],{"className":28,"code":14371,"language":30,"meta":31,"style":31},"result = [result.append(n) for n in numbers]\n",[33,14373,14374],{"__ignoreMap":31},[36,14375,14376,14378,14380,14382,14384,14386,14388,14390,14392,14394,14396,14398,14400,14402],{"class":38,"line":39},[36,14377,7723],{"class":42},[36,14379,47],{"class":46},[36,14381,51],{"class":50},[36,14383,7767],{"class":42},[36,14385,351],{"class":50},[36,14387,508],{"class":82},[36,14389,79],{"class":50},[36,14391,9924],{"class":82},[36,14393,1988],{"class":50},[36,14395,13007],{"class":1718},[36,14397,9908],{"class":42},[36,14399,9911],{"class":1718},[36,14401,9874],{"class":42},[36,14403,69],{"class":50},[14,14405,14406],{},"Instead, put the value you want directly in the expression:",[26,14408,14410],{"className":28,"code":14409,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nresult = [n for n in numbers]\n\nprint(result)\n# [1, 2, 3]\n",[33,14411,14412,14432,14452,14456,14466],{"__ignoreMap":31},[36,14413,14414,14416,14418,14420,14422,14424,14426,14428,14430],{"class":38,"line":39},[36,14415,43],{"class":42},[36,14417,47],{"class":46},[36,14419,51],{"class":50},[36,14421,55],{"class":54},[36,14423,58],{"class":50},[36,14425,61],{"class":54},[36,14427,58],{"class":50},[36,14429,66],{"class":54},[36,14431,69],{"class":50},[36,14433,14434,14436,14438,14440,14442,14444,14446,14448,14450],{"class":38,"line":72},[36,14435,7723],{"class":42},[36,14437,47],{"class":46},[36,14439,51],{"class":50},[36,14441,12856],{"class":42},[36,14443,9852],{"class":1718},[36,14445,9908],{"class":42},[36,14447,9911],{"class":1718},[36,14449,9874],{"class":42},[36,14451,69],{"class":50},[36,14453,14454],{"class":38,"line":89},[36,14455,476],{"emptyLinePlaceholder":475},[36,14457,14458,14460,14462,14464],{"class":38,"line":496},[36,14459,76],{"class":75},[36,14461,79],{"class":50},[36,14463,7767],{"class":82},[36,14465,86],{"class":50},[36,14467,14468],{"class":38,"line":501},[36,14469,13800],{"class":2570},[14,14471,14472],{},"Useful checks while learning:",[26,14474,14476],{"className":28,"code":14475,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4]\nprint(numbers)\nprint([n * n for n in numbers])\nprint(type([n * n for n in numbers]))\nhelp(list)\n",[33,14477,14478,14502,14512,14534,14560],{"__ignoreMap":31},[36,14479,14480,14482,14484,14486,14488,14490,14492,14494,14496,14498,14500],{"class":38,"line":39},[36,14481,43],{"class":42},[36,14483,47],{"class":46},[36,14485,51],{"class":50},[36,14487,55],{"class":54},[36,14489,58],{"class":50},[36,14491,61],{"class":54},[36,14493,58],{"class":50},[36,14495,66],{"class":54},[36,14497,58],{"class":50},[36,14499,1549],{"class":54},[36,14501,69],{"class":50},[36,14503,14504,14506,14508,14510],{"class":38,"line":72},[36,14505,76],{"class":75},[36,14507,79],{"class":50},[36,14509,83],{"class":82},[36,14511,86],{"class":50},[36,14513,14514,14516,14518,14520,14522,14524,14526,14528,14530,14532],{"class":38,"line":89},[36,14515,76],{"class":75},[36,14517,5490],{"class":50},[36,14519,12856],{"class":82},[36,14521,10824],{"class":46},[36,14523,9908],{"class":82},[36,14525,9852],{"class":1718},[36,14527,9908],{"class":82},[36,14529,9911],{"class":1718},[36,14531,9874],{"class":82},[36,14533,493],{"class":50},[36,14535,14536,14538,14540,14542,14544,14546,14548,14550,14552,14554,14556,14558],{"class":38,"line":496},[36,14537,76],{"class":75},[36,14539,79],{"class":50},[36,14541,97],{"class":96},[36,14543,5490],{"class":50},[36,14545,12856],{"class":82},[36,14547,10824],{"class":46},[36,14549,9908],{"class":82},[36,14551,9852],{"class":1718},[36,14553,9908],{"class":82},[36,14555,9911],{"class":1718},[36,14557,9874],{"class":82},[36,14559,5503],{"class":50},[36,14561,14562,14564,14566,14568],{"class":38,"line":501},[36,14563,4392],{"class":75},[36,14565,79],{"class":50},[36,14567,146],{"class":96},[36,14569,86],{"class":50},[21,14571,1063],{"id":1062},[1065,14573,14575],{"id":14574},"is-a-list-comprehension-the-same-as-a-for-loop","Is a list comprehension the same as a for loop?",[14,14577,14578],{},"Not exactly. It is a shorter way to build a new list, but it still uses looping behind the scenes.",[1065,14580,14582],{"id":14581},"can-a-list-comprehension-include-an-if-statement","Can a list comprehension include an if statement?",[14,14584,14585,14586,14588],{},"Yes. You can add an ",[33,14587,1192],{}," condition at the end to keep only matching items.",[14,14590,197],{},[26,14592,14594],{"className":28,"code":14593,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3, 4]\nsmall_numbers = [n for n in numbers if n \u003C 3]\nprint(small_numbers)\n# [1, 2]\n",[33,14595,14596,14620,14649,14660],{"__ignoreMap":31},[36,14597,14598,14600,14602,14604,14606,14608,14610,14612,14614,14616,14618],{"class":38,"line":39},[36,14599,43],{"class":42},[36,14601,47],{"class":46},[36,14603,51],{"class":50},[36,14605,55],{"class":54},[36,14607,58],{"class":50},[36,14609,61],{"class":54},[36,14611,58],{"class":50},[36,14613,66],{"class":54},[36,14615,58],{"class":50},[36,14617,1549],{"class":54},[36,14619,69],{"class":50},[36,14621,14622,14625,14627,14629,14631,14633,14635,14637,14639,14641,14643,14645,14647],{"class":38,"line":72},[36,14623,14624],{"class":42},"small_numbers ",[36,14626,47],{"class":46},[36,14628,51],{"class":50},[36,14630,12856],{"class":42},[36,14632,9852],{"class":1718},[36,14634,9908],{"class":42},[36,14636,9911],{"class":1718},[36,14638,13156],{"class":42},[36,14640,1192],{"class":1718},[36,14642,9908],{"class":42},[36,14644,134],{"class":46},[36,14646,66],{"class":54},[36,14648,69],{"class":50},[36,14650,14651,14653,14655,14658],{"class":38,"line":89},[36,14652,76],{"class":75},[36,14654,79],{"class":50},[36,14656,14657],{"class":82},"small_numbers",[36,14659,86],{"class":50},[36,14661,14662],{"class":38,"line":496},[36,14663,14664],{"class":2570},"# [1, 2]\n",[1065,14666,14668],{"id":14667},"does-a-list-comprehension-change-the-original-list","Does a list comprehension change the original list?",[14,14670,14671],{},"No. It creates a new list unless you assign it back to the same variable.",[1065,14673,14675],{"id":14674},"should-beginners-use-list-comprehensions","Should beginners use list comprehensions?",[14,14677,14678,14679,14681],{},"Yes, for simple cases. If the line becomes confusing, use a normal ",[33,14680,9852],{}," loop instead.",[21,14683,1105],{"id":1104},[157,14685,14686,14690,14694,14700,14705],{},[160,14687,14688],{},[347,14689,11436],{"href":13538},[160,14691,14692],{},[347,14693,13398],{"href":13397},[160,14695,14696],{},[347,14697,1665,14698,7353],{"href":640},[33,14699,13404],{},[160,14701,14702],{},[347,14703,14704],{"href":13057},"What is an iterable in Python?",[160,14706,14707],{},[347,14708,14709],{"href":1161},"What is a list in Python?",[1137,14711,14712],{},"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 .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 .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 .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}",{"title":31,"searchDepth":72,"depth":72,"links":14714},[14715,14716,14717,14718,14719,14720,14721,14722,14731,14737],{"id":6100,"depth":72,"text":6101},{"id":12911,"depth":72,"text":12912},{"id":13061,"depth":72,"text":13062},{"id":13198,"depth":72,"text":13199},{"id":12374,"depth":72,"text":12375},{"id":13541,"depth":72,"text":13542},{"id":10614,"depth":72,"text":10615},{"id":904,"depth":72,"text":905,"children":14723},[14724,14725,14727,14728,14729],{"id":13908,"depth":89,"text":13909},{"id":14003,"depth":89,"text":14726},"Putting the for part before the expression",{"id":14101,"depth":89,"text":14102},{"id":14219,"depth":89,"text":14220},{"id":14359,"depth":89,"text":14730},"Using append() inside the comprehension",{"id":1062,"depth":72,"text":1063,"children":14732},[14733,14734,14735,14736],{"id":14574,"depth":89,"text":14575},{"id":14581,"depth":89,"text":14582},{"id":14667,"depth":89,"text":14668},{"id":14674,"depth":89,"text":14675},{"id":1104,"depth":72,"text":1105},"Master what is a list comprehension in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-list-comprehension-in-python",{"title":12803,"description":14738},"glossary\u002Fwhat-is-a-list-comprehension-in-python","4atNnOGInxvucFFJt9Pn2s55oMu-8Ugqn_FOjZZRKrk",{"id":4,"title":5,"body":14745,"description":1158,"extension":1159,"meta":15625,"navigation":475,"path":1161,"seo":15626,"stem":1163,"__hash__":1164},{"type":7,"value":14746,"toc":15607},[14747,14749,14751,14753,14755,14803,14805,14839,14841,14851,14853,14855,14857,14867,14869,14905,14909,14911,14913,14915,14925,14927,14963,14965,14967,14969,14997,14999,15045,15047,15049,15139,15141,15185,15187,15205,15213,15221,15223,15225,15227,15235,15237,15255,15257,15393,15399,15401,15403,15405,15413,15415,15429,15431,15433,15469,15471,15527,15529,15547,15553,15555,15557,15559,15561,15563,15565,15567,15569,15571,15573,15577,15579,15605],[10,14748,5],{"id":12},[14,14750,16],{},[14,14752,19],{},[21,14754,24],{"id":23},[26,14756,14757],{"className":28,"code":29,"language":30,"meta":31,"style":31},[33,14758,14759,14779,14789],{"__ignoreMap":31},[36,14760,14761,14763,14765,14767,14769,14771,14773,14775,14777],{"class":38,"line":39},[36,14762,43],{"class":42},[36,14764,47],{"class":46},[36,14766,51],{"class":50},[36,14768,55],{"class":54},[36,14770,58],{"class":50},[36,14772,61],{"class":54},[36,14774,58],{"class":50},[36,14776,66],{"class":54},[36,14778,69],{"class":50},[36,14780,14781,14783,14785,14787],{"class":38,"line":72},[36,14782,76],{"class":75},[36,14784,79],{"class":50},[36,14786,83],{"class":82},[36,14788,86],{"class":50},[36,14790,14791,14793,14795,14797,14799,14801],{"class":38,"line":89},[36,14792,76],{"class":75},[36,14794,79],{"class":50},[36,14796,97],{"class":96},[36,14798,79],{"class":50},[36,14800,83],{"class":82},[36,14802,104],{"class":50},[14,14804,107],{},[26,14806,14807],{"className":28,"code":110,"language":30,"meta":31,"style":31},[33,14808,14809,14825],{"__ignoreMap":31},[36,14810,14811,14813,14815,14817,14819,14821,14823],{"class":38,"line":39},[36,14812,117],{"class":50},[36,14814,55],{"class":54},[36,14816,58],{"class":50},[36,14818,61],{"class":54},[36,14820,58],{"class":50},[36,14822,66],{"class":54},[36,14824,69],{"class":50},[36,14826,14827,14829,14831,14833,14835,14837],{"class":38,"line":72},[36,14828,134],{"class":46},[36,14830,138],{"class":137},[36,14832,142],{"class":141},[36,14834,146],{"class":145},[36,14836,149],{"class":141},[36,14838,152],{"class":46},[14,14840,155],{},[157,14842,14843,14847],{},[160,14844,162,14845],{},[33,14846,165],{},[160,14848,168,14849,171],{},[33,14850,146],{},[21,14852,175],{"id":174},[14,14854,178],{},[14,14856,181],{},[157,14858,14859,14863,14865],{},[160,14860,186,14861],{},[33,14862,165],{},[160,14864,191],{},[160,14866,194],{},[14,14868,197],{},[26,14870,14871],{"className":28,"code":200,"language":30,"meta":31,"style":31},[33,14872,14873],{"__ignoreMap":31},[36,14874,14875,14877,14879,14881,14883,14885,14887,14889,14891,14893,14895,14897,14899,14901,14903],{"class":38,"line":39},[36,14876,207],{"class":42},[36,14878,47],{"class":46},[36,14880,51],{"class":50},[36,14882,214],{"class":141},[36,14884,217],{"class":145},[36,14886,214],{"class":141},[36,14888,58],{"class":50},[36,14890,224],{"class":141},[36,14892,227],{"class":145},[36,14894,214],{"class":141},[36,14896,58],{"class":50},[36,14898,224],{"class":141},[36,14900,236],{"class":145},[36,14902,214],{"class":141},[36,14904,69],{"class":50},[14,14906,243,14907,247],{},[33,14908,246],{},[21,14910,251],{"id":250},[14,14912,254],{},[14,14914,257],{},[157,14916,14917,14919,14921,14923],{},[160,14918,262],{},[160,14920,265],{},[160,14922,268],{},[160,14924,271],{},[14,14926,274],{},[26,14928,14929],{"className":28,"code":277,"language":30,"meta":31,"style":31},[33,14930,14931],{"__ignoreMap":31},[36,14932,14933,14935,14937,14939,14941,14943,14945,14947,14949,14951,14953,14955,14957,14959,14961],{"class":38,"line":39},[36,14934,284],{"class":42},[36,14936,47],{"class":46},[36,14938,51],{"class":50},[36,14940,214],{"class":141},[36,14942,293],{"class":145},[36,14944,214],{"class":141},[36,14946,58],{"class":50},[36,14948,224],{"class":141},[36,14950,302],{"class":145},[36,14952,214],{"class":141},[36,14954,58],{"class":50},[36,14956,224],{"class":141},[36,14958,311],{"class":145},[36,14960,214],{"class":141},[36,14962,69],{"class":50},[14,14964,318],{},[21,14966,322],{"id":321},[14,14968,325],{},[157,14970,14971,14977,14985,14991],{},[160,14972,14973,14975,337],{},[331,14974,333],{},[335,14976],{},[160,14978,14979,14981,345,14983,351],{},[331,14980,342],{},[335,14982],{},[347,14984,350],{"href":349},[160,14986,14987,14989,359],{},[331,14988,356],{},[335,14990],{},[160,14992,14993,14995,367],{},[331,14994,364],{},[335,14996],{},[14,14998,197],{},[26,15000,15001],{"className":28,"code":372,"language":30,"meta":31,"style":31},[33,15002,15003,15035],{"__ignoreMap":31},[36,15004,15005,15007,15009,15011,15013,15015,15017,15019,15021,15023,15025,15027,15029,15031,15033],{"class":38,"line":39},[36,15006,379],{"class":42},[36,15008,47],{"class":46},[36,15010,51],{"class":50},[36,15012,214],{"class":141},[36,15014,388],{"class":145},[36,15016,214],{"class":141},[36,15018,58],{"class":50},[36,15020,395],{"class":54},[36,15022,58],{"class":50},[36,15024,401],{"class":400},[36,15026,58],{"class":50},[36,15028,224],{"class":141},[36,15030,388],{"class":145},[36,15032,214],{"class":141},[36,15034,69],{"class":50},[36,15036,15037,15039,15041,15043],{"class":38,"line":72},[36,15038,76],{"class":75},[36,15040,79],{"class":50},[36,15042,420],{"class":82},[36,15044,86],{"class":50},[21,15046,426],{"id":425},[14,15048,429],{},[26,15050,15051],{"className":28,"code":432,"language":30,"meta":31,"style":31},[33,15052,15053,15085,15089,15103,15107,15125,15129],{"__ignoreMap":31},[36,15054,15055,15057,15059,15061,15063,15065,15067,15069,15071,15073,15075,15077,15079,15081,15083],{"class":38,"line":39},[36,15056,439],{"class":42},[36,15058,47],{"class":46},[36,15060,51],{"class":50},[36,15062,214],{"class":141},[36,15064,448],{"class":145},[36,15066,214],{"class":141},[36,15068,58],{"class":50},[36,15070,224],{"class":141},[36,15072,457],{"class":145},[36,15074,214],{"class":141},[36,15076,58],{"class":50},[36,15078,224],{"class":141},[36,15080,466],{"class":145},[36,15082,214],{"class":141},[36,15084,69],{"class":50},[36,15086,15087],{"class":38,"line":72},[36,15088,476],{"emptyLinePlaceholder":475},[36,15090,15091,15093,15095,15097,15099,15101],{"class":38,"line":89},[36,15092,76],{"class":75},[36,15094,79],{"class":50},[36,15096,485],{"class":82},[36,15098,117],{"class":50},[36,15100,490],{"class":54},[36,15102,493],{"class":50},[36,15104,15105],{"class":38,"line":496},[36,15106,476],{"emptyLinePlaceholder":475},[36,15108,15109,15111,15113,15115,15117,15119,15121,15123],{"class":38,"line":501},[36,15110,485],{"class":42},[36,15112,351],{"class":50},[36,15114,508],{"class":82},[36,15116,79],{"class":50},[36,15118,214],{"class":141},[36,15120,515],{"class":145},[36,15122,214],{"class":141},[36,15124,86],{"class":50},[36,15126,15127],{"class":38,"line":522},[36,15128,476],{"emptyLinePlaceholder":475},[36,15130,15131,15133,15135,15137],{"class":38,"line":527},[36,15132,76],{"class":75},[36,15134,79],{"class":50},[36,15136,485],{"class":82},[36,15138,86],{"class":50},[14,15140,107],{},[26,15142,15143],{"className":28,"code":540,"language":30,"meta":31,"style":31},[33,15144,15145,15149],{"__ignoreMap":31},[36,15146,15147],{"class":38,"line":39},[36,15148,547],{"class":42},[36,15150,15151,15153,15155,15157,15159,15161,15163,15165,15167,15169,15171,15173,15175,15177,15179,15181,15183],{"class":38,"line":72},[36,15152,117],{"class":50},[36,15154,149],{"class":141},[36,15156,448],{"class":145},[36,15158,149],{"class":141},[36,15160,58],{"class":50},[36,15162,142],{"class":141},[36,15164,457],{"class":145},[36,15166,149],{"class":141},[36,15168,58],{"class":50},[36,15170,142],{"class":141},[36,15172,466],{"class":145},[36,15174,149],{"class":141},[36,15176,58],{"class":50},[36,15178,142],{"class":141},[36,15180,515],{"class":145},[36,15182,149],{"class":141},[36,15184,69],{"class":50},[14,15186,586],{},[157,15188,15189,15193,15197,15201],{},[160,15190,15191,594],{},[33,15192,593],{},[160,15194,15195,600],{},[33,15196,599],{},[160,15198,15199,606],{},[33,15200,605],{},[160,15202,15203,612],{},[33,15204,611],{},[14,15206,615,15207,618,15209,621,15211,624],{},[33,15208,490],{},[33,15210,55],{},[33,15212,599],{},[14,15214,627,15215,632,15217,637,15219,351],{},[347,15216,631],{"href":630},[347,15218,636],{"href":635},[347,15220,641],{"href":640},[21,15222,645],{"id":644},[14,15224,648],{},[14,15226,651],{},[157,15228,15229,15231,15233],{},[160,15230,656],{},[160,15232,659],{},[160,15234,662],{},[14,15236,665],{},[157,15238,15239,15243,15247,15251],{},[160,15240,15241,673],{},[331,15242,672],{},[160,15244,15245,679],{},[331,15246,678],{},[160,15248,15249,685],{},[331,15250,684],{},[160,15252,15253,691],{},[331,15254,690],{},[14,15256,694],{},[26,15258,15259],{"className":28,"code":697,"language":30,"meta":31,"style":31},[33,15260,15261,15293,15325,15357],{"__ignoreMap":31},[36,15262,15263,15265,15267,15269,15271,15273,15275,15277,15279,15281,15283,15285,15287,15289,15291],{"class":38,"line":39},[36,15264,704],{"class":42},[36,15266,47],{"class":46},[36,15268,51],{"class":50},[36,15270,214],{"class":141},[36,15272,347],{"class":145},[36,15274,214],{"class":141},[36,15276,58],{"class":50},[36,15278,224],{"class":141},[36,15280,721],{"class":145},[36,15282,214],{"class":141},[36,15284,58],{"class":50},[36,15286,224],{"class":141},[36,15288,730],{"class":145},[36,15290,214],{"class":141},[36,15292,69],{"class":50},[36,15294,15295,15297,15299,15301,15303,15305,15307,15309,15311,15313,15315,15317,15319,15321,15323],{"class":38,"line":72},[36,15296,739],{"class":42},[36,15298,47],{"class":46},[36,15300,744],{"class":50},[36,15302,214],{"class":141},[36,15304,347],{"class":145},[36,15306,214],{"class":141},[36,15308,58],{"class":50},[36,15310,224],{"class":141},[36,15312,721],{"class":145},[36,15314,214],{"class":141},[36,15316,58],{"class":50},[36,15318,224],{"class":141},[36,15320,730],{"class":145},[36,15322,214],{"class":141},[36,15324,86],{"class":50},[36,15326,15327,15329,15331,15333,15335,15337,15339,15341,15343,15345,15347,15349,15351,15353,15355],{"class":38,"line":89},[36,15328,773],{"class":42},[36,15330,47],{"class":46},[36,15332,778],{"class":50},[36,15334,214],{"class":141},[36,15336,347],{"class":145},[36,15338,214],{"class":141},[36,15340,58],{"class":50},[36,15342,224],{"class":141},[36,15344,721],{"class":145},[36,15346,214],{"class":141},[36,15348,58],{"class":50},[36,15350,224],{"class":141},[36,15352,730],{"class":145},[36,15354,214],{"class":141},[36,15356,803],{"class":50},[36,15358,15359,15361,15363,15365,15367,15369,15371,15373,15375,15377,15379,15381,15383,15385,15387,15389,15391],{"class":38,"line":496},[36,15360,808],{"class":42},[36,15362,47],{"class":46},[36,15364,778],{"class":50},[36,15366,214],{"class":141},[36,15368,817],{"class":145},[36,15370,214],{"class":141},[36,15372,822],{"class":50},[36,15374,224],{"class":141},[36,15376,448],{"class":145},[36,15378,214],{"class":141},[36,15380,58],{"class":50},[36,15382,224],{"class":141},[36,15384,835],{"class":145},[36,15386,214],{"class":141},[36,15388,822],{"class":50},[36,15390,842],{"class":54},[36,15392,803],{"class":50},[14,15394,847,15395,852,15397,857],{},[347,15396,851],{"href":850},[347,15398,856],{"href":855},[21,15400,861],{"id":860},[14,15402,864],{},[14,15404,867],{},[157,15406,15407,15409,15411],{},[160,15408,872],{},[160,15410,875],{},[160,15412,878],{},[14,15414,881],{},[157,15416,15417,15421,15425],{},[160,15418,15419],{},[347,15420,889],{"href":888},[160,15422,15423],{},[347,15424,895],{"href":894},[160,15426,15427],{},[347,15428,901],{"href":900},[21,15430,905],{"id":904},[14,15432,908],{},[157,15434,15435,15441,15453,15463],{},[160,15436,15437,15439,918],{},[331,15438,915],{},[335,15440],{},[160,15442,15443,15445,926,15447,929,15449,933,15451,937],{},[331,15444,923],{},[335,15446],{},[33,15448,165],{},[33,15450,932],{},[33,15452,936],{},[160,15454,15455,15457,945,15459,618,15461,351],{},[331,15456,942],{},[335,15458],{},[33,15460,490],{},[33,15462,55],{},[160,15464,15465,15467,957],{},[331,15466,954],{},[335,15468],{},[14,15470,960],{},[26,15472,15473],{"className":28,"code":963,"language":30,"meta":31,"style":31},[33,15474,15475,15485,15499,15513],{"__ignoreMap":31},[36,15476,15477,15479,15481,15483],{"class":38,"line":39},[36,15478,76],{"class":75},[36,15480,79],{"class":50},[36,15482,974],{"class":82},[36,15484,86],{"class":50},[36,15486,15487,15489,15491,15493,15495,15497],{"class":38,"line":72},[36,15488,76],{"class":75},[36,15490,79],{"class":50},[36,15492,97],{"class":96},[36,15494,79],{"class":50},[36,15496,974],{"class":82},[36,15498,104],{"class":50},[36,15500,15501,15503,15505,15507,15509,15511],{"class":38,"line":89},[36,15502,76],{"class":75},[36,15504,79],{"class":50},[36,15506,999],{"class":75},[36,15508,79],{"class":50},[36,15510,974],{"class":82},[36,15512,104],{"class":50},[36,15514,15515,15517,15519,15521,15523,15525],{"class":38,"line":496},[36,15516,76],{"class":75},[36,15518,79],{"class":50},[36,15520,974],{"class":82},[36,15522,117],{"class":50},[36,15524,490],{"class":54},[36,15526,493],{"class":50},[14,15528,1022],{},[157,15530,15531,15535,15539,15543],{},[160,15532,15533,1030],{},[33,15534,1029],{},[160,15536,15537,1036],{},[33,15538,1035],{},[160,15540,15541,1042],{},[33,15542,1041],{},[160,15544,15545,1048],{},[33,15546,1047],{},[14,15548,1051,15549,1055,15551,351],{},[33,15550,1054],{},[347,15552,1059],{"href":1058},[21,15554,1063],{"id":1062},[1065,15556,1068],{"id":1067},[14,15558,1071],{},[1065,15560,1075],{"id":1074},[14,15562,1078],{},[1065,15564,1082],{"id":1081},[14,15566,1085],{},[1065,15568,1089],{"id":1088},[14,15570,1092],{},[1065,15572,1096],{"id":1095},[14,15574,1099,15575],{},[33,15576,165],{},[21,15578,1105],{"id":1104},[157,15580,15581,15585,15589,15593,15597,15601],{},[160,15582,15583],{},[347,15584,631],{"href":630},[160,15586,15587],{},[347,15588,1116],{"href":888},[160,15590,15591],{},[347,15592,641],{"href":640},[160,15594,15595],{},[347,15596,1125],{"href":635},[160,15598,15599],{},[347,15600,1130],{"href":894},[160,15602,15603],{},[347,15604,1135],{"href":850},[1137,15606,1139],{},{"title":31,"searchDepth":72,"depth":72,"links":15608},[15609,15610,15611,15612,15613,15614,15615,15616,15617,15624],{"id":23,"depth":72,"text":24},{"id":174,"depth":72,"text":175},{"id":250,"depth":72,"text":251},{"id":321,"depth":72,"text":322},{"id":425,"depth":72,"text":426},{"id":644,"depth":72,"text":645},{"id":860,"depth":72,"text":861},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":15618},[15619,15620,15621,15622,15623],{"id":1067,"depth":89,"text":1068},{"id":1074,"depth":89,"text":1075},{"id":1081,"depth":89,"text":1082},{"id":1088,"depth":89,"text":1089},{"id":1095,"depth":89,"text":1096},{"id":1104,"depth":72,"text":1105},{},{"title":5,"description":1158},{"id":15628,"title":15629,"body":15630,"description":16320,"extension":1159,"meta":16321,"navigation":475,"path":16322,"seo":16323,"stem":16324,"__hash__":16325},"content\u002Fglossary\u002Fwhat-is-a-loop-in-python.md","What Is a Loop in Python?",{"type":7,"value":15631,"toc":16296},[15632,15635,15638,15641,15644,15654,15657,15659,15697,15700,15703,15705,15721,15725,15728,15731,15742,15748,15751,15755,15758,15761,15775,15778,15782,15785,15791,15796,15799,15815,15821,15827,15832,15839,15842,15849,15853,15859,15895,15897,15914,15917,15933,15943,15947,15950,15952,15966,15973,15982,15986,15989,16007,16013,16041,16047,16052,16055,16059,16062,16067,16092,16095,16111,16113,16115,16140,16146,16148,16188,16191,16202,16204,16208,16211,16215,16224,16228,16233,16237,16242,16246,16252,16254,16282,16293],[10,15633,15629],{"id":15634},"what-is-a-loop-in-python",[14,15636,15637],{},"A loop in Python is a way to repeat code.",[14,15639,15640],{},"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,15642,15643],{},"Python mainly has two loop types:",[157,15645,15646,15650],{},[160,15647,15648,1781],{},[33,15649,9852],{},[160,15651,15652,1781],{},[33,15653,1780],{},[14,15655,15656],{},"This page gives the basic idea of loops. It does not fully teach loop syntax.",[21,15658,24],{"id":23},[26,15660,15662],{"className":28,"code":15661,"language":30,"meta":31,"style":31},"for number in [1, 2, 3]:\n    print(number)\n",[33,15663,15664,15687],{"__ignoreMap":31},[36,15665,15666,15668,15670,15672,15674,15676,15678,15680,15682,15684],{"class":38,"line":39},[36,15667,9852],{"class":1718},[36,15669,10205],{"class":42},[36,15671,9911],{"class":1718},[36,15673,51],{"class":50},[36,15675,55],{"class":54},[36,15677,58],{"class":50},[36,15679,61],{"class":54},[36,15681,58],{"class":50},[36,15683,66],{"class":54},[36,15685,15686],{"class":50},"]:\n",[36,15688,15689,15691,15693,15695],{"class":38,"line":72},[36,15690,1735],{"class":75},[36,15692,79],{"class":50},[36,15694,8368],{"class":82},[36,15696,86],{"class":50},[14,15698,15699],{},"This loop prints each number in the list.",[14,15701,15702],{},"The indented line is the code that repeats. It runs once for each value.",[14,15704,107],{},[26,15706,15707],{"className":28,"code":9931,"language":30,"meta":31,"style":31},[33,15708,15709,15713,15717],{"__ignoreMap":31},[36,15710,15711],{"class":38,"line":39},[36,15712,9938],{"class":54},[36,15714,15715],{"class":38,"line":72},[36,15716,9943],{"class":54},[36,15718,15719],{"class":38,"line":89},[36,15720,9948],{"class":54},[21,15722,15724],{"id":15723},"what-a-loop-means","What a loop means",[14,15726,15727],{},"A loop is a way to repeat code.",[14,15729,15730],{},"Here is the main idea:",[157,15732,15733,15736,15739],{},[160,15734,15735],{},"You want to perform the same action multiple times",[160,15737,15738],{},"You write that action once inside a loop",[160,15740,15741],{},"Python runs it again for each repetition",[14,15743,15744,15745,351],{},"Each time the loop runs is called an ",[331,15746,15747],{},"iteration",[14,15749,15750],{},"For example, if a loop prints three items, that loop has three iterations.",[21,15752,15754],{"id":15753},"why-loops-are-useful","Why loops are useful",[14,15756,15757],{},"Loops are useful because they help you avoid repeated code.",[14,15759,15760],{},"They are commonly used for tasks like:",[157,15762,15763,15766,15769,15772],{},[160,15764,15765],{},"working through items in a list",[160,15767,15768],{},"reading characters in a string",[160,15770,15771],{},"reading lines from a file",[160,15773,15774],{},"counting through a range of numbers",[14,15776,15777],{},"Loops also make code easier to update later. If the repeated step needs to change, you usually only change it in one place.",[21,15779,15781],{"id":15780},"the-main-loop-types-in-python","The main loop types in Python",[14,15783,15784],{},"Python has two main loop types.",[1065,15786,15788,15790],{"id":15787},"for-loop",[33,15789,9852],{}," loop",[14,15792,3355,15793,15795],{},[33,15794,9852],{}," loop repeats once for each item in a sequence.",[14,15797,15798],{},"That sequence might be:",[157,15800,15801,15804,15807,15812],{},[160,15802,15803],{},"a list",[160,15805,15806],{},"a string",[160,15808,3316,15809],{},[33,15810,15811],{},"range()",[160,15813,15814],{},"other iterable values",[14,15816,15817,15818,15820],{},"Beginners often start with ",[33,15819,9852],{}," loops because they are usually easier to read.",[14,15822,15823,15824,351],{},"If you want a full lesson, see ",[347,15825,13398],{"href":15826},"\u002Flearn\u002Fpython-for-loops-explained\u002F",[1065,15828,15830,15790],{"id":15829},"while-loop",[33,15831,1780],{},[14,15833,3355,15834,15836,15837,351],{},[33,15835,1780],{}," loop repeats as long as a condition stays ",[33,15838,1179],{},[14,15840,15841],{},"This is useful when you do not know exactly how many times the loop should run.",[14,15843,15844,15845,351],{},"For a full beginner guide, see ",[347,15846,15848],{"href":15847},"\u002Flearn\u002Fpython-while-loops-explained\u002F","Python while loops explained",[21,15850,15852],{"id":15851},"simple-example-of-a-loop","Simple example of a loop",[14,15854,15855,15856,15858],{},"Here is a small ",[33,15857,9852],{}," loop:",[26,15860,15861],{"className":28,"code":15661,"language":30,"meta":31,"style":31},[33,15862,15863,15885],{"__ignoreMap":31},[36,15864,15865,15867,15869,15871,15873,15875,15877,15879,15881,15883],{"class":38,"line":39},[36,15866,9852],{"class":1718},[36,15868,10205],{"class":42},[36,15870,9911],{"class":1718},[36,15872,51],{"class":50},[36,15874,55],{"class":54},[36,15876,58],{"class":50},[36,15878,61],{"class":54},[36,15880,58],{"class":50},[36,15882,66],{"class":54},[36,15884,15686],{"class":50},[36,15886,15887,15889,15891,15893],{"class":38,"line":72},[36,15888,1735],{"class":75},[36,15890,79],{"class":50},[36,15892,8368],{"class":82},[36,15894,86],{"class":50},[14,15896,9951],{},[157,15898,15899,15905,15911],{},[160,15900,15901,15904],{},[33,15902,15903],{},"for number in [1, 2, 3]:"," tells Python to go through the list one item at a time",[160,15906,15907,15910],{},[33,15908,15909],{},"print(number)"," is the repeated action",[160,15912,15913],{},"The indentation shows which line belongs to the loop",[14,15915,15916],{},"Expected output:",[26,15918,15919],{"className":28,"code":9931,"language":30,"meta":31,"style":31},[33,15920,15921,15925,15929],{"__ignoreMap":31},[36,15922,15923],{"class":38,"line":39},[36,15924,9938],{"class":54},[36,15926,15927],{"class":38,"line":72},[36,15928,9943],{"class":54},[36,15930,15931],{"class":38,"line":89},[36,15932,9948],{"class":54},[14,15934,15935,15936,15938,15939,351],{},"If the ",[33,15937,15909],{}," line was not indented, it would not be part of the loop. If indentation is confusing, read ",[347,15940,15942],{"href":15941},"\u002Flearn\u002Fpython-indentation-rules-and-why-they-matter\u002F","Python indentation rules and why they matter",[21,15944,15946],{"id":15945},"when-to-use-a-loop","When to use a loop",[14,15948,15949],{},"Use a loop when you need to repeat a step.",[14,15951,5098],{},[157,15953,15954,15957,15960,15963],{},[160,15955,15956],{},"Loop through items in a list",[160,15958,15959],{},"Repeat until the user enters valid input",[160,15961,15962],{},"Read lines from a file one by one",[160,15964,15965],{},"Count through a range of numbers",[14,15967,15968,15969,351],{},"For example, if you want to process each item in a list, a loop is the natural tool. A good next step is ",[347,15970,15972],{"href":15971},"\u002Fhow-to\u002Fhow-to-loop-through-a-list-in-python\u002F","how to loop through a list in Python",[14,15974,15975,15976,15981],{},"You will also often see loops used with the ",[347,15977,15979,8246],{"href":15978},"\u002Freference\u002Fpython-range-function-explained\u002F",[33,15980,15811],{}," when counting numbers.",[21,15983,15985],{"id":15984},"important-beginner-idea","Important beginner idea",[14,15987,15988],{},"There are a few important things to understand early.",[157,15990,15991,15994,15997,16000],{},[160,15992,15993],{},"A loop can run many times",[160,15995,15996],{},"A loop can also run zero times",[160,15998,15999],{},"Indentation matters because it defines the repeated block",[160,16001,3355,16002,16004,16005],{},[33,16003,1780],{}," loop can become infinite if its condition never becomes ",[33,16006,1183],{},[14,16008,16009,16010,16012],{},"For example, this ",[33,16011,1780],{}," loop never stops:",[26,16014,16016],{"className":28,"code":16015,"language":30,"meta":31,"style":31},"while True:\n    print(\"loop running\")\n",[33,16017,16018,16026],{"__ignoreMap":31},[36,16019,16020,16022,16024],{"class":38,"line":39},[36,16021,1780],{"class":1718},[36,16023,401],{"class":400},[36,16025,1730],{"class":50},[36,16027,16028,16030,16032,16034,16037,16039],{"class":38,"line":72},[36,16029,1735],{"class":75},[36,16031,79],{"class":50},[36,16033,214],{"class":141},[36,16035,16036],{"class":145},"loop running",[36,16038,214],{"class":141},[36,16040,86],{"class":50},[14,16042,16043,16044,351],{},"This is called an ",[331,16045,16046],{},"infinite loop",[14,16048,3355,16049,16051],{},[33,16050,1780],{}," loop usually needs something to change so the condition eventually becomes false.",[14,16053,16054],{},"Also, be careful when changing loop variables or counting values. Small mistakes can cause bugs or make a loop behave in unexpected ways.",[21,16056,16058],{"id":16057},"what-this-page-should-not-cover-in-depth","What this page should not cover in depth",[14,16060,16061],{},"This page is only a glossary-style definition.",[14,16063,9843,16064,16066],{},[331,16065,8193],{}," fully teach:",[157,16068,16069,16075,16079,16082,16087],{},[160,16070,16071,16072,16074],{},"full ",[33,16073,9852],{}," loop syntax",[160,16076,16071,16077,16074],{},[33,16078,1780],{},[160,16080,16081],{},"nested loops",[160,16083,16084],{},[33,16085,16086],{},"break",[160,16088,16089],{},[33,16090,16091],{},"continue",[14,16093,16094],{},"If you want to go further, read:",[157,16096,16097,16101,16105],{},[160,16098,16099],{},[347,16100,13398],{"href":15826},[160,16102,16103],{},[347,16104,15848],{"href":15847},[160,16106,16107],{},[347,16108,16110],{"href":16109},"\u002Flearn\u002Fpython-break-and-continue-statements\u002F","Python break and continue statements",[21,16112,905],{"id":904},[14,16114,8484],{},[157,16116,16117,16122,16125,16128,16134],{},[160,16118,16119,16120,2140],{},"Confusing a loop with an ",[33,16121,1192],{},[160,16123,16124],{},"Thinking loops always run forever",[160,16126,16127],{},"Forgetting that indentation controls which lines repeat",[160,16129,16130,16131,16133],{},"Using a ",[33,16132,1780],{}," loop without changing the condition",[160,16135,16136,16137,16139],{},"Not realizing that ",[33,16138,9852],{}," loops usually go through a sequence",[14,16141,16142,16143,16145],{},"If you are not sure what a loop is doing, simple ",[33,16144,9167],{}," statements can help you debug it.",[14,16147,694],{},[26,16149,16151],{"className":28,"code":16150,"language":30,"meta":31,"style":31},"print(item)\nprint(count)\nprint(\"loop running\")\n",[33,16152,16153,16163,16174],{"__ignoreMap":31},[36,16154,16155,16157,16159,16161],{"class":38,"line":39},[36,16156,76],{"class":75},[36,16158,79],{"class":50},[36,16160,10455],{"class":82},[36,16162,86],{"class":50},[36,16164,16165,16167,16169,16172],{"class":38,"line":72},[36,16166,76],{"class":75},[36,16168,79],{"class":50},[36,16170,16171],{"class":82},"count",[36,16173,86],{"class":50},[36,16175,16176,16178,16180,16182,16184,16186],{"class":38,"line":89},[36,16177,76],{"class":75},[36,16179,79],{"class":50},[36,16181,214],{"class":141},[36,16183,16036],{"class":145},[36,16185,214],{"class":141},[36,16187,86],{"class":50},[14,16189,16190],{},"These help you see:",[157,16192,16193,16196,16199],{},[160,16194,16195],{},"whether the loop is running",[160,16197,16198],{},"which value is being processed",[160,16200,16201],{},"how a counter is changing",[21,16203,1063],{"id":1062},[1065,16205,16207],{"id":16206},"what-is-a-loop-in-simple-words","What is a loop in simple words?",[14,16209,16210],{},"A loop is code that repeats the same steps multiple times.",[1065,16212,16214],{"id":16213},"what-are-the-two-main-loops-in-python","What are the two main loops in Python?",[14,16216,16217,16218,16220,16221,16223],{},"Python mainly uses ",[33,16219,9852],{}," loops and ",[33,16222,1780],{}," loops.",[1065,16225,16227],{"id":16226},"when-should-i-use-a-for-loop","When should I use a for loop?",[14,16229,6748,16230,16232],{},[33,16231,9852],{}," loop when you want to go through items like a list, string, or range.",[1065,16234,16236],{"id":16235},"when-should-i-use-a-while-loop","When should I use a while loop?",[14,16238,6748,16239,16241],{},[33,16240,1780],{}," loop when code should keep running until a condition changes.",[1065,16243,16245],{"id":16244},"can-a-loop-run-forever","Can a loop run forever?",[14,16247,16248,16249,16251],{},"Yes. A ",[33,16250,1780],{}," loop can run forever if the condition never becomes false.",[21,16253,1105],{"id":1104},[157,16255,16256,16260,16264,16268,16272,16277],{},[160,16257,16258],{},[347,16259,13398],{"href":15826},[160,16261,16262],{},[347,16263,15848],{"href":15847},[160,16265,16266],{},[347,16267,15942],{"href":15941},[160,16269,16270],{},[347,16271,16110],{"href":16109},[160,16273,16274],{},[347,16275,16276],{"href":15978},"Python range() function explained",[160,16278,16279],{},[347,16280,16281],{"href":15971},"How to loop through a list in Python",[14,16283,16284,16285,16288,16289,16292],{},"If you are learning loops for the first time, the best next step is to start with ",[331,16286,16287],{},"for loops",", then learn ",[331,16290,16291],{},"while loops",", and then practice looping through lists.",[1137,16294,16295],{},"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":31,"searchDepth":72,"depth":72,"links":16297},[16298,16299,16300,16301,16307,16308,16309,16310,16311,16312,16319],{"id":23,"depth":72,"text":24},{"id":15723,"depth":72,"text":15724},{"id":15753,"depth":72,"text":15754},{"id":15780,"depth":72,"text":15781,"children":16302},[16303,16305],{"id":15787,"depth":89,"text":16304},"for loop",{"id":15829,"depth":89,"text":16306},"while loop",{"id":15851,"depth":72,"text":15852},{"id":15945,"depth":72,"text":15946},{"id":15984,"depth":72,"text":15985},{"id":16057,"depth":72,"text":16058},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":16313},[16314,16315,16316,16317,16318],{"id":16206,"depth":89,"text":16207},{"id":16213,"depth":89,"text":16214},{"id":16226,"depth":89,"text":16227},{"id":16235,"depth":89,"text":16236},{"id":16244,"depth":89,"text":16245},{"id":1104,"depth":72,"text":1105},"Master what is a loop in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-loop-in-python",{"title":15629,"description":16320},"glossary\u002Fwhat-is-a-loop-in-python","Mnq-14CKOm5nEHDnr4M8JufK5zOPIfqTFlg3hikroEg",{"id":16327,"title":16328,"body":16329,"description":18134,"extension":1159,"meta":18135,"navigation":475,"path":18136,"seo":18137,"stem":18138,"__hash__":18139},"content\u002Fglossary\u002Fwhat-is-a-method-in-python.md","What Is a Method in Python?",{"type":7,"value":16330,"toc":18106},[16331,16334,16337,16340,16372,16378,16388,16390,16393,16395,16411,16413,16463,16465,16473,16475,16491,16497,16502,16506,16509,16529,16531,16609,16611,16640,16642,16660,16663,16669,16673,16676,16680,16683,16698,16700,16766,16768,16788,16795,16799,16802,16818,16820,16914,16916,16961,16965,16968,16984,16986,17083,17085,17153,17156,17160,17163,17195,17198,17213,17216,17218,17284,17286,17295,17297,17317,17320,17332,17336,17339,17348,17350,17415,17417,17426,17428,17448,17455,17461,17526,17528,17536,17540,17546,17549,17673,17675,17683,17689,17693,17696,17700,17703,17705,17761,17763,17799,17802,17806,17809,17811,17850,17856,17870,17874,17877,17932,17935,17939,17942,18006,18009,18028,18030,18034,18037,18041,18048,18052,18055,18059,18064,18068,18071,18073,18103],[10,16332,16328],{"id":16333},"what-is-a-method-in-python",[14,16335,16336],{},"A method in Python is a function that belongs to an object.",[14,16338,16339],{},"You use methods all the time in Python, even in simple code like:",[26,16341,16343],{"className":28,"code":16342,"language":30,"meta":31,"style":31},"name = \"Ada\"\nprint(name.upper())\n",[33,16344,16345,16358],{"__ignoreMap":31},[36,16346,16347,16349,16351,16353,16356],{"class":38,"line":39},[36,16348,1945],{"class":42},[36,16350,47],{"class":46},[36,16352,224],{"class":141},[36,16354,16355],{"class":145},"Ada",[36,16357,1954],{"class":141},[36,16359,16360,16362,16364,16366,16368,16370],{"class":38,"line":72},[36,16361,76],{"class":75},[36,16363,79],{"class":50},[36,16365,817],{"class":82},[36,16367,351],{"class":50},[36,16369,13483],{"class":82},[36,16371,4387],{"class":50},[14,16373,3765,16374,16377],{},[33,16375,16376],{},"upper()"," is a method of a string object.",[14,16379,16380,16381,16384,16385,351],{},"Understanding methods helps you read Python code more easily. It also helps you know why some actions are written as ",[33,16382,16383],{},"object.method()"," while others are written as regular functions like ",[33,16386,16387],{},"len(object)",[21,16389,7434],{"id":7433},[14,16391,16392],{},"A method is a function that belongs to an object.",[14,16394,9838],{},[157,16396,16397,16400,16403,16408],{},[160,16398,16399],{},"A method is called on an object",[160,16401,16402],{},"You use dot notation to call it",[160,16404,16405,16406],{},"The usual shape is ",[33,16407,16383],{},[160,16409,16410],{},"Methods can perform an action or return information",[14,16412,197],{},[26,16414,16416],{"className":28,"code":16415,"language":30,"meta":31,"style":31},"text = \" hello \"\ncleaned = text.strip()\n\nprint(cleaned)\n",[33,16417,16418,16431,16448,16452],{"__ignoreMap":31},[36,16419,16420,16422,16424,16426,16429],{"class":38,"line":39},[36,16421,9407],{"class":42},[36,16423,47],{"class":46},[36,16425,224],{"class":141},[36,16427,16428],{"class":145}," hello ",[36,16430,1954],{"class":141},[36,16432,16433,16436,16438,16441,16443,16446],{"class":38,"line":72},[36,16434,16435],{"class":42},"cleaned ",[36,16437,47],{"class":46},[36,16439,16440],{"class":42}," text",[36,16442,351],{"class":50},[36,16444,16445],{"class":82},"strip",[36,16447,3170],{"class":50},[36,16449,16450],{"class":38,"line":89},[36,16451,476],{"emptyLinePlaceholder":475},[36,16453,16454,16456,16458,16461],{"class":38,"line":496},[36,16455,76],{"class":75},[36,16457,79],{"class":50},[36,16459,16460],{"class":82},"cleaned",[36,16462,86],{"class":50},[14,16464,107],{},[26,16466,16467],{"className":28,"code":2760,"language":30,"meta":31,"style":31},[33,16468,16469],{"__ignoreMap":31},[36,16470,16471],{"class":38,"line":39},[36,16472,2760],{"class":42},[14,16474,4063],{},[157,16476,16477,16481,16486],{},[160,16478,16479,4074],{},[33,16480,4695],{},[160,16482,16483,16485],{},[33,16484,16445],{}," is the method name",[160,16487,16488,16490],{},[33,16489,932],{}," runs the method",[14,16492,3542,16493,16496],{},[33,16494,16495],{},"strip()"," method removes spaces from the beginning and end of the string.",[14,16498,16499,16500,351],{},"If you are new to objects, see ",[347,16501,3233],{"href":3232},[21,16503,16505],{"id":16504},"how-methods-are-different-from-functions","How methods are different from functions",[14,16507,16508],{},"A method is similar to a function, but they are not called in the same way.",[157,16510,16511,16517,16523,16526],{},[160,16512,16513,16514],{},"A function is called by name, like ",[33,16515,16516],{},"len(my_list)",[160,16518,16519,16520],{},"A method is called on an object, like ",[33,16521,16522],{},"my_list.append(5)",[160,16524,16525],{},"Methods belong to a specific type or class",[160,16527,16528],{},"Not every operation in Python uses a method",[14,16530,197],{},[26,16532,16534],{"className":28,"code":16533,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\n\nprint(len(numbers))   # function\nnumbers.append(4)     # method\n\nprint(numbers)\n",[33,16535,16536,16556,16560,16578,16595,16599],{"__ignoreMap":31},[36,16537,16538,16540,16542,16544,16546,16548,16550,16552,16554],{"class":38,"line":39},[36,16539,43],{"class":42},[36,16541,47],{"class":46},[36,16543,51],{"class":50},[36,16545,55],{"class":54},[36,16547,58],{"class":50},[36,16549,61],{"class":54},[36,16551,58],{"class":50},[36,16553,66],{"class":54},[36,16555,69],{"class":50},[36,16557,16558],{"class":38,"line":72},[36,16559,476],{"emptyLinePlaceholder":475},[36,16561,16562,16564,16566,16568,16570,16572,16575],{"class":38,"line":89},[36,16563,76],{"class":75},[36,16565,79],{"class":50},[36,16567,999],{"class":75},[36,16569,79],{"class":50},[36,16571,83],{"class":82},[36,16573,16574],{"class":50},"))",[36,16576,16577],{"class":2570},"   # function\n",[36,16579,16580,16582,16584,16586,16588,16590,16592],{"class":38,"line":496},[36,16581,83],{"class":42},[36,16583,351],{"class":50},[36,16585,508],{"class":82},[36,16587,79],{"class":50},[36,16589,11622],{"class":54},[36,16591,1988],{"class":50},[36,16593,16594],{"class":2570},"     # method\n",[36,16596,16597],{"class":38,"line":501},[36,16598,476],{"emptyLinePlaceholder":475},[36,16600,16601,16603,16605,16607],{"class":38,"line":522},[36,16602,76],{"class":75},[36,16604,79],{"class":50},[36,16606,83],{"class":82},[36,16608,86],{"class":50},[14,16610,107],{},[26,16612,16614],{"className":28,"code":16613,"language":30,"meta":31,"style":31},"3\n[1, 2, 3, 4]\n",[33,16615,16616,16620],{"__ignoreMap":31},[36,16617,16618],{"class":38,"line":39},[36,16619,9948],{"class":54},[36,16621,16622,16624,16626,16628,16630,16632,16634,16636,16638],{"class":38,"line":72},[36,16623,117],{"class":50},[36,16625,55],{"class":54},[36,16627,58],{"class":50},[36,16629,61],{"class":54},[36,16631,58],{"class":50},[36,16633,66],{"class":54},[36,16635,58],{"class":50},[36,16637,1549],{"class":54},[36,16639,69],{"class":50},[14,16641,6187],{},[157,16643,16644,16652],{},[160,16645,16646,16649,16650],{},[33,16647,16648],{},"len(numbers)"," uses the built-in function ",[33,16651,999],{},[160,16653,16654,16657,16658],{},[33,16655,16656],{},"numbers.append(4)"," uses the list method ",[33,16659,508],{},[14,16661,16662],{},"So even though both work with the list, one is a function and one is a method.",[14,16664,16665,16666,351],{},"For a practical example, see the ",[347,16667,641],{"href":16668},"\u002Freference\u002Fpython-list-append-method\u002F",[21,16670,16672],{"id":16671},"simple-examples-of-methods","Simple examples of methods",[14,16674,16675],{},"Different object types have different methods.",[1065,16677,16679],{"id":16678},"string-methods","String methods",[14,16681,16682],{},"Strings have methods such as:",[157,16684,16685,16690,16694],{},[160,16686,16687],{},[33,16688,16689],{},"lower()",[160,16691,16692],{},[33,16693,16376],{},[160,16695,16696],{},[33,16697,16495],{},[14,16699,197],{},[26,16701,16703],{"className":28,"code":16702,"language":30,"meta":31,"style":31},"word = \" PyThOn \"\n\nprint(word.lower())\nprint(word.upper())\nprint(word.strip())\n",[33,16704,16705,16719,16723,16738,16752],{"__ignoreMap":31},[36,16706,16707,16710,16712,16714,16717],{"class":38,"line":39},[36,16708,16709],{"class":42},"word ",[36,16711,47],{"class":46},[36,16713,224],{"class":141},[36,16715,16716],{"class":145}," PyThOn ",[36,16718,1954],{"class":141},[36,16720,16721],{"class":38,"line":72},[36,16722,476],{"emptyLinePlaceholder":475},[36,16724,16725,16727,16729,16731,16733,16736],{"class":38,"line":89},[36,16726,76],{"class":75},[36,16728,79],{"class":50},[36,16730,10068],{"class":82},[36,16732,351],{"class":50},[36,16734,16735],{"class":82},"lower",[36,16737,4387],{"class":50},[36,16739,16740,16742,16744,16746,16748,16750],{"class":38,"line":496},[36,16741,76],{"class":75},[36,16743,79],{"class":50},[36,16745,10068],{"class":82},[36,16747,351],{"class":50},[36,16749,13483],{"class":82},[36,16751,4387],{"class":50},[36,16753,16754,16756,16758,16760,16762,16764],{"class":38,"line":501},[36,16755,76],{"class":75},[36,16757,79],{"class":50},[36,16759,10068],{"class":82},[36,16761,351],{"class":50},[36,16763,16445],{"class":82},[36,16765,4387],{"class":50},[14,16767,107],{},[26,16769,16771],{"className":28,"code":16770,"language":30,"meta":31,"style":31}," python \n PYTHON\nPyThOn\n",[33,16772,16773,16778,16783],{"__ignoreMap":31},[36,16774,16775],{"class":38,"line":39},[36,16776,16777],{"class":42}," python \n",[36,16779,16780],{"class":38,"line":72},[36,16781,16782],{"class":3059}," PYTHON\n",[36,16784,16785],{"class":38,"line":89},[36,16786,16787],{"class":42},"PyThOn\n",[14,16789,16790,16791,351],{},"If you want to learn one of these in more detail, see the ",[347,16792,16794],{"href":16793},"\u002Freference\u002Fpython-string-lower-method\u002F","Python string lower() method",[1065,16796,16798],{"id":16797},"list-methods","List methods",[14,16800,16801],{},"Lists have methods such as:",[157,16803,16804,16808,16813],{},[160,16805,16806],{},[33,16807,13210],{},[160,16809,16810],{},[33,16811,16812],{},"pop()",[160,16814,16815],{},[33,16816,16817],{},"sort()",[14,16819,197],{},[26,16821,16823],{"className":28,"code":16822,"language":30,"meta":31,"style":31},"numbers = [3, 1, 2]\n\nnumbers.append(4)\nnumbers.sort()\n\nprint(numbers)\nprint(numbers.pop())\nprint(numbers)\n",[33,16824,16825,16846,16850,16864,16875,16879,16889,16904],{"__ignoreMap":31},[36,16826,16827,16829,16831,16833,16835,16837,16840,16842,16844],{"class":38,"line":39},[36,16828,43],{"class":42},[36,16830,47],{"class":46},[36,16832,51],{"class":50},[36,16834,1247],{"class":54},[36,16836,58],{"class":50},[36,16838,16839],{"class":54}," 1",[36,16841,58],{"class":50},[36,16843,61],{"class":54},[36,16845,69],{"class":50},[36,16847,16848],{"class":38,"line":72},[36,16849,476],{"emptyLinePlaceholder":475},[36,16851,16852,16854,16856,16858,16860,16862],{"class":38,"line":89},[36,16853,83],{"class":42},[36,16855,351],{"class":50},[36,16857,508],{"class":82},[36,16859,79],{"class":50},[36,16861,11622],{"class":54},[36,16863,86],{"class":50},[36,16865,16866,16868,16870,16873],{"class":38,"line":496},[36,16867,83],{"class":42},[36,16869,351],{"class":50},[36,16871,16872],{"class":82},"sort",[36,16874,3170],{"class":50},[36,16876,16877],{"class":38,"line":501},[36,16878,476],{"emptyLinePlaceholder":475},[36,16880,16881,16883,16885,16887],{"class":38,"line":522},[36,16882,76],{"class":75},[36,16884,79],{"class":50},[36,16886,83],{"class":82},[36,16888,86],{"class":50},[36,16890,16891,16893,16895,16897,16899,16902],{"class":38,"line":527},[36,16892,76],{"class":75},[36,16894,79],{"class":50},[36,16896,83],{"class":82},[36,16898,351],{"class":50},[36,16900,16901],{"class":82},"pop",[36,16903,4387],{"class":50},[36,16905,16906,16908,16910,16912],{"class":38,"line":3092},[36,16907,76],{"class":75},[36,16909,79],{"class":50},[36,16911,83],{"class":82},[36,16913,86],{"class":50},[14,16915,107],{},[26,16917,16919],{"className":28,"code":16918,"language":30,"meta":31,"style":31},"[1, 2, 3, 4]\n4\n[1, 2, 3]\n",[33,16920,16921,16941,16945],{"__ignoreMap":31},[36,16922,16923,16925,16927,16929,16931,16933,16935,16937,16939],{"class":38,"line":39},[36,16924,117],{"class":50},[36,16926,55],{"class":54},[36,16928,58],{"class":50},[36,16930,61],{"class":54},[36,16932,58],{"class":50},[36,16934,66],{"class":54},[36,16936,58],{"class":50},[36,16938,1549],{"class":54},[36,16940,69],{"class":50},[36,16942,16943],{"class":38,"line":72},[36,16944,10891],{"class":54},[36,16946,16947,16949,16951,16953,16955,16957,16959],{"class":38,"line":89},[36,16948,117],{"class":50},[36,16950,55],{"class":54},[36,16952,58],{"class":50},[36,16954,61],{"class":54},[36,16956,58],{"class":50},[36,16958,66],{"class":54},[36,16960,69],{"class":50},[1065,16962,16964],{"id":16963},"dictionary-methods","Dictionary methods",[14,16966,16967],{},"Dictionaries have methods such as:",[157,16969,16970,16974,16979],{},[160,16971,16972],{},[33,16973,7089],{},[160,16975,16976],{},[33,16977,16978],{},"keys()",[160,16980,16981],{},[33,16982,16983],{},"items()",[14,16985,197],{},[26,16987,16989],{"className":28,"code":16988,"language":30,"meta":31,"style":31},"user = {\"name\": \"Sam\", \"age\": 20}\n\nprint(user.get(\"name\"))\nprint(user.keys())\nprint(user.items())\n",[33,16990,16991,17027,17031,17054,17068],{"__ignoreMap":31},[36,16992,16993,16995,16997,16999,17001,17003,17005,17007,17009,17011,17013,17015,17017,17019,17021,17023,17025],{"class":38,"line":39},[36,16994,4971],{"class":42},[36,16996,47],{"class":46},[36,16998,778],{"class":50},[36,17000,214],{"class":141},[36,17002,817],{"class":145},[36,17004,214],{"class":141},[36,17006,822],{"class":50},[36,17008,224],{"class":141},[36,17010,311],{"class":145},[36,17012,214],{"class":141},[36,17014,58],{"class":50},[36,17016,224],{"class":141},[36,17018,835],{"class":145},[36,17020,214],{"class":141},[36,17022,822],{"class":50},[36,17024,1628],{"class":54},[36,17026,803],{"class":50},[36,17028,17029],{"class":38,"line":72},[36,17030,476],{"emptyLinePlaceholder":475},[36,17032,17033,17035,17037,17040,17042,17044,17046,17048,17050,17052],{"class":38,"line":89},[36,17034,76],{"class":75},[36,17036,79],{"class":50},[36,17038,17039],{"class":82},"user",[36,17041,351],{"class":50},[36,17043,7136],{"class":82},[36,17045,79],{"class":50},[36,17047,214],{"class":141},[36,17049,817],{"class":145},[36,17051,214],{"class":141},[36,17053,104],{"class":50},[36,17055,17056,17058,17060,17062,17064,17066],{"class":38,"line":496},[36,17057,76],{"class":75},[36,17059,79],{"class":50},[36,17061,17039],{"class":82},[36,17063,351],{"class":50},[36,17065,4384],{"class":82},[36,17067,4387],{"class":50},[36,17069,17070,17072,17074,17076,17078,17081],{"class":38,"line":501},[36,17071,76],{"class":75},[36,17073,79],{"class":50},[36,17075,17039],{"class":82},[36,17077,351],{"class":50},[36,17079,17080],{"class":82},"items",[36,17082,4387],{"class":50},[14,17084,107],{},[26,17086,17088],{"className":28,"code":17087,"language":30,"meta":31,"style":31},"Sam\ndict_keys(['name', 'age'])\ndict_items([('name', 20), ('age', 20)])\n",[33,17089,17090,17095,17118],{"__ignoreMap":31},[36,17091,17092],{"class":38,"line":39},[36,17093,17094],{"class":42},"Sam\n",[36,17096,17097,17100,17102,17104,17106,17108,17110,17112,17114,17116],{"class":38,"line":72},[36,17098,17099],{"class":82},"dict_keys",[36,17101,5490],{"class":50},[36,17103,149],{"class":141},[36,17105,817],{"class":145},[36,17107,149],{"class":141},[36,17109,58],{"class":50},[36,17111,142],{"class":141},[36,17113,835],{"class":145},[36,17115,149],{"class":141},[36,17117,493],{"class":50},[36,17119,17120,17123,17126,17128,17130,17132,17134,17136,17138,17140,17142,17144,17146,17148,17150],{"class":38,"line":89},[36,17121,17122],{"class":82},"dict_items",[36,17124,17125],{"class":50},"([(",[36,17127,149],{"class":141},[36,17129,817],{"class":145},[36,17131,149],{"class":141},[36,17133,58],{"class":50},[36,17135,1628],{"class":54},[36,17137,11966],{"class":50},[36,17139,744],{"class":50},[36,17141,149],{"class":141},[36,17143,835],{"class":145},[36,17145,149],{"class":141},[36,17147,58],{"class":50},[36,17149,1628],{"class":54},[36,17151,17152],{"class":50},")])\n",[14,17154,17155],{},"These methods work because each type provides its own set of methods.",[21,17157,17159],{"id":17158},"how-to-read-method-calls","How to read method calls",[14,17161,17162],{},"When you see code like this:",[26,17164,17166],{"className":28,"code":17165,"language":30,"meta":31,"style":31},"name = \"maria\"\nprint(name.upper())\n",[33,17167,17168,17181],{"__ignoreMap":31},[36,17169,17170,17172,17174,17176,17179],{"class":38,"line":39},[36,17171,1945],{"class":42},[36,17173,47],{"class":46},[36,17175,224],{"class":141},[36,17177,17178],{"class":145},"maria",[36,17180,1954],{"class":141},[36,17182,17183,17185,17187,17189,17191,17193],{"class":38,"line":72},[36,17184,76],{"class":75},[36,17186,79],{"class":50},[36,17188,817],{"class":82},[36,17190,351],{"class":50},[36,17192,13483],{"class":82},[36,17194,4387],{"class":50},[14,17196,17197],{},"You can read it like this:",[157,17199,17200,17204,17208],{},[160,17201,17202,4074],{},[33,17203,817],{},[160,17205,17206,16485],{},[33,17207,13483],{},[160,17209,17210,17212],{},[33,17211,932],{}," calls the method",[14,17214,17215],{},"Some methods also take arguments.",[14,17217,197],{},[26,17219,17221],{"className":28,"code":17220,"language":30,"meta":31,"style":31},"text = \"banana\"\nnew_text = text.replace(\"a\", \"o\")\n\nprint(new_text)\n",[33,17222,17223,17236,17269,17273],{"__ignoreMap":31},[36,17224,17225,17227,17229,17231,17234],{"class":38,"line":39},[36,17226,9407],{"class":42},[36,17228,47],{"class":46},[36,17230,224],{"class":141},[36,17232,17233],{"class":145},"banana",[36,17235,1954],{"class":141},[36,17237,17238,17241,17243,17245,17247,17250,17252,17254,17256,17258,17260,17262,17265,17267],{"class":38,"line":72},[36,17239,17240],{"class":42},"new_text ",[36,17242,47],{"class":46},[36,17244,16440],{"class":42},[36,17246,351],{"class":50},[36,17248,17249],{"class":82},"replace",[36,17251,79],{"class":50},[36,17253,214],{"class":141},[36,17255,347],{"class":145},[36,17257,214],{"class":141},[36,17259,58],{"class":50},[36,17261,224],{"class":141},[36,17263,17264],{"class":145},"o",[36,17266,214],{"class":141},[36,17268,86],{"class":50},[36,17270,17271],{"class":38,"line":89},[36,17272,476],{"emptyLinePlaceholder":475},[36,17274,17275,17277,17279,17282],{"class":38,"line":496},[36,17276,76],{"class":75},[36,17278,79],{"class":50},[36,17280,17281],{"class":82},"new_text",[36,17283,86],{"class":50},[14,17285,107],{},[26,17287,17289],{"className":28,"code":17288,"language":30,"meta":31,"style":31},"bonono\n",[33,17290,17291],{"__ignoreMap":31},[36,17292,17293],{"class":38,"line":39},[36,17294,17288],{"class":42},[14,17296,4063],{},[157,17298,17299,17303,17308],{},[160,17300,17301,4074],{},[33,17302,4695],{},[160,17304,17305,17307],{},[33,17306,17249],{}," is the method",[160,17309,17310,1281,17313,17316],{},[33,17311,17312],{},"\"a\"",[33,17314,17315],{},"\"o\""," are arguments passed into the method",[14,17318,17319],{},"A good way to read it is:",[17321,17322,17323],"blockquote",{},[14,17324,17325,17326,17328,17329,17331],{},"“Call the ",[33,17327,17249],{}," method on ",[33,17330,4695],{},".”",[21,17333,17335],{"id":17334},"methods-in-classes","Methods in classes",[14,17337,17338],{},"You can also create your own methods by defining them inside a class.",[14,17340,17341,17342,1281,17344,351],{},"If you have not learned this yet, see ",[347,17343,3343],{"href":3342},[347,17345,17347],{"href":17346},"\u002Fglossary\u002Fwhat-is-a-class-in-python\u002F","what a class is in Python",[14,17349,197],{},[26,17351,17353],{"className":28,"code":17352,"language":30,"meta":31,"style":31},"class Dog:\n    def bark(self):\n        return \"Woof!\"\n\nmy_dog = Dog()\nprint(my_dog.bark())\n",[33,17354,17355,17363,17375,17387,17391,17401],{"__ignoreMap":31},[36,17356,17357,17359,17361],{"class":38,"line":39},[36,17358,138],{"class":137},[36,17360,3011],{"class":3010},[36,17362,1730],{"class":50},[36,17364,17365,17367,17369,17371,17373],{"class":38,"line":72},[36,17366,3036],{"class":137},[36,17368,3083],{"class":3082},[36,17370,79],{"class":50},[36,17372,3045],{"class":3044},[36,17374,3054],{"class":50},[36,17376,17377,17380,17382,17385],{"class":38,"line":89},[36,17378,17379],{"class":1718},"        return",[36,17381,224],{"class":141},[36,17383,17384],{"class":145},"Woof!",[36,17386,1954],{"class":141},[36,17388,17389],{"class":38,"line":496},[36,17390,476],{"emptyLinePlaceholder":475},[36,17392,17393,17395,17397,17399],{"class":38,"line":501},[36,17394,3126],{"class":42},[36,17396,47],{"class":46},[36,17398,3011],{"class":82},[36,17400,3170],{"class":50},[36,17402,17403,17405,17407,17409,17411,17413],{"class":38,"line":522},[36,17404,76],{"class":75},[36,17406,79],{"class":50},[36,17408,3151],{"class":82},[36,17410,351],{"class":50},[36,17412,3167],{"class":82},[36,17414,4387],{"class":50},[14,17416,107],{},[26,17418,17420],{"className":28,"code":17419,"language":30,"meta":31,"style":31},"Woof!\n",[33,17421,17422],{"__ignoreMap":31},[36,17423,17424],{"class":38,"line":39},[36,17425,17419],{"class":42},[14,17427,6187],{},[157,17429,17430,17435,17440],{},[160,17431,17432,17434],{},[33,17433,3200],{}," is a class",[160,17436,17437,17439],{},[33,17438,3151],{}," is an object made from that class",[160,17441,17442,17444,17445,17447],{},[33,17443,3219],{}," is a method of the ",[33,17446,3200],{}," class",[1065,17449,17451,17452,17454],{"id":17450},"what-self-means","What ",[33,17453,3045],{}," means",[14,17456,17457,17458,17460],{},"A method in a class usually has ",[33,17459,3045],{}," as its first parameter:",[26,17462,17464],{"className":28,"code":17463,"language":30,"meta":31,"style":31},"class Person:\n    def greet(self):\n        return \"Hello\"\n\np = Person()\nprint(p.greet())\n",[33,17465,17466,17475,17487,17497,17501,17512],{"__ignoreMap":31},[36,17467,17468,17470,17473],{"class":38,"line":39},[36,17469,138],{"class":137},[36,17471,17472],{"class":3010}," Person",[36,17474,1730],{"class":50},[36,17476,17477,17479,17481,17483,17485],{"class":38,"line":72},[36,17478,3036],{"class":137},[36,17480,9033],{"class":3082},[36,17482,79],{"class":50},[36,17484,3045],{"class":3044},[36,17486,3054],{"class":50},[36,17488,17489,17491,17493,17495],{"class":38,"line":89},[36,17490,17379],{"class":1718},[36,17492,224],{"class":141},[36,17494,8801],{"class":145},[36,17496,1954],{"class":141},[36,17498,17499],{"class":38,"line":496},[36,17500,476],{"emptyLinePlaceholder":475},[36,17502,17503,17506,17508,17510],{"class":38,"line":501},[36,17504,17505],{"class":42},"p ",[36,17507,47],{"class":46},[36,17509,17472],{"class":82},[36,17511,3170],{"class":50},[36,17513,17514,17516,17518,17520,17522,17524],{"class":38,"line":522},[36,17515,76],{"class":75},[36,17517,79],{"class":50},[36,17519,14],{"class":82},[36,17521,351],{"class":50},[36,17523,9067],{"class":82},[36,17525,4387],{"class":50},[14,17527,107],{},[26,17529,17530],{"className":28,"code":8821,"language":30,"meta":31,"style":31},[33,17531,17532],{"__ignoreMap":31},[36,17533,17534],{"class":38,"line":39},[36,17535,8821],{"class":42},[14,17537,17538,3646],{},[33,17539,3045],{},[14,17541,17542,17543,17545],{},"You do not pass ",[33,17544,3045],{}," yourself when calling the method. Python does that for you.",[14,17547,17548],{},"Here is another example:",[26,17550,17552],{"className":28,"code":17551,"language":30,"meta":31,"style":31},"class Counter:\n    def __init__(self):\n        self.value = 0\n\n    def increase(self):\n        self.value += 1\n\ncounter = Counter()\ncounter.increase()\ncounter.increase()\n\nprint(counter.value)\n",[33,17553,17554,17563,17575,17588,17592,17605,17618,17622,17633,17645,17655,17659],{"__ignoreMap":31},[36,17555,17556,17558,17561],{"class":38,"line":39},[36,17557,138],{"class":137},[36,17559,17560],{"class":3010}," Counter",[36,17562,1730],{"class":50},[36,17564,17565,17567,17569,17571,17573],{"class":38,"line":72},[36,17566,3036],{"class":137},[36,17568,3039],{"class":75},[36,17570,79],{"class":50},[36,17572,3045],{"class":3044},[36,17574,3054],{"class":50},[36,17576,17577,17579,17581,17583,17585],{"class":38,"line":89},[36,17578,3060],{"class":3059},[36,17580,351],{"class":50},[36,17582,2686],{"class":3065},[36,17584,3068],{"class":46},[36,17586,17587],{"class":54}," 0\n",[36,17589,17590],{"class":38,"line":496},[36,17591,476],{"emptyLinePlaceholder":475},[36,17593,17594,17596,17599,17601,17603],{"class":38,"line":501},[36,17595,3036],{"class":137},[36,17597,17598],{"class":3082}," increase",[36,17600,79],{"class":50},[36,17602,3045],{"class":3044},[36,17604,3054],{"class":50},[36,17606,17607,17609,17611,17613,17616],{"class":38,"line":522},[36,17608,3060],{"class":3059},[36,17610,351],{"class":50},[36,17612,2686],{"class":3065},[36,17614,17615],{"class":46}," +=",[36,17617,9884],{"class":54},[36,17619,17620],{"class":38,"line":527},[36,17621,476],{"emptyLinePlaceholder":475},[36,17623,17624,17627,17629,17631],{"class":38,"line":3092},[36,17625,17626],{"class":42},"counter ",[36,17628,47],{"class":46},[36,17630,17560],{"class":82},[36,17632,3170],{"class":50},[36,17634,17635,17638,17640,17643],{"class":38,"line":3118},[36,17636,17637],{"class":42},"counter",[36,17639,351],{"class":50},[36,17641,17642],{"class":82},"increase",[36,17644,3170],{"class":50},[36,17646,17647,17649,17651,17653],{"class":38,"line":3123},[36,17648,17637],{"class":42},[36,17650,351],{"class":50},[36,17652,17642],{"class":82},[36,17654,3170],{"class":50},[36,17656,17657],{"class":38,"line":3144},[36,17658,476],{"emptyLinePlaceholder":475},[36,17660,17661,17663,17665,17667,17669,17671],{"class":38,"line":3160},[36,17662,76],{"class":75},[36,17664,79],{"class":50},[36,17666,17637],{"class":82},[36,17668,351],{"class":50},[36,17670,2686],{"class":3065},[36,17672,86],{"class":50},[14,17674,107],{},[26,17676,17677],{"className":28,"code":9943,"language":30,"meta":31,"style":31},[33,17678,17679],{"__ignoreMap":31},[36,17680,17681],{"class":38,"line":39},[36,17682,9943],{"class":54},[14,17684,3542,17685,17688],{},[33,17686,17687],{},"increase()"," method changes data stored in the object.",[21,17690,17692],{"id":17691},"what-beginners-often-confuse","What beginners often confuse",[14,17694,17695],{},"Beginners often run into the same problems when learning methods.",[1065,17697,17699],{"id":17698},"forgetting-parentheses","Forgetting parentheses",[14,17701,17702],{},"A method call usually needs parentheses.",[14,17704,197],{},[26,17706,17708],{"className":28,"code":17707,"language":30,"meta":31,"style":31},"text = \"hello\"\n\nprint(text.upper)    # no parentheses\nprint(text.upper())  # correct\n",[33,17709,17710,17722,17726,17743],{"__ignoreMap":31},[36,17711,17712,17714,17716,17718,17720],{"class":38,"line":39},[36,17713,9407],{"class":42},[36,17715,47],{"class":46},[36,17717,224],{"class":141},[36,17719,2671],{"class":145},[36,17721,1954],{"class":141},[36,17723,17724],{"class":38,"line":72},[36,17725,476],{"emptyLinePlaceholder":475},[36,17727,17728,17730,17732,17734,17736,17738,17740],{"class":38,"line":89},[36,17729,76],{"class":75},[36,17731,79],{"class":50},[36,17733,4695],{"class":82},[36,17735,351],{"class":50},[36,17737,13483],{"class":3065},[36,17739,1988],{"class":50},[36,17741,17742],{"class":2570},"    # no parentheses\n",[36,17744,17745,17747,17749,17751,17753,17755,17758],{"class":38,"line":496},[36,17746,76],{"class":75},[36,17748,79],{"class":50},[36,17750,4695],{"class":82},[36,17752,351],{"class":50},[36,17754,13483],{"class":82},[36,17756,17757],{"class":50},"())",[36,17759,17760],{"class":2570},"  # correct\n",[14,17762,107],{},[26,17764,17766],{"className":28,"code":17765,"language":30,"meta":31,"style":31},"\u003Cbuilt-in method upper of str object at ...>\nHELLO\n",[33,17767,17768,17794],{"__ignoreMap":31},[36,17769,17770,17772,17775,17778,17781,17783,17786,17789,17792],{"class":38,"line":39},[36,17771,134],{"class":46},[36,17773,17774],{"class":42},"built",[36,17776,17777],{"class":46},"-in",[36,17779,17780],{"class":42}," method upper of ",[36,17782,2360],{"class":96},[36,17784,17785],{"class":96}," object",[36,17787,17788],{"class":42}," at ",[36,17790,17791],{"class":3059},"...",[36,17793,152],{"class":46},[36,17795,17796],{"class":38,"line":72},[36,17797,17798],{"class":3059},"HELLO\n",[14,17800,17801],{},"Without parentheses, you are referring to the method itself, not calling it.",[1065,17803,17805],{"id":17804},"using-the-wrong-method-on-the-wrong-type","Using the wrong method on the wrong type",[14,17807,17808],{},"Not all types have the same methods.",[14,17810,197],{},[26,17812,17814],{"className":28,"code":17813,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nprint(numbers.lower())\n",[33,17815,17816,17836],{"__ignoreMap":31},[36,17817,17818,17820,17822,17824,17826,17828,17830,17832,17834],{"class":38,"line":39},[36,17819,43],{"class":42},[36,17821,47],{"class":46},[36,17823,51],{"class":50},[36,17825,55],{"class":54},[36,17827,58],{"class":50},[36,17829,61],{"class":54},[36,17831,58],{"class":50},[36,17833,66],{"class":54},[36,17835,69],{"class":50},[36,17837,17838,17840,17842,17844,17846,17848],{"class":38,"line":72},[36,17839,76],{"class":75},[36,17841,79],{"class":50},[36,17843,83],{"class":82},[36,17845,351],{"class":50},[36,17847,16735],{"class":82},[36,17849,4387],{"class":50},[14,17851,17852,17853,17855],{},"This causes an error because lists do not have a ",[33,17854,16689],{}," method.",[14,17857,17858,17859,17862,17863,351],{},"You would get an ",[33,17860,17861],{},"AttributeError",". If you see that problem, read ",[347,17864,17866,17867],{"href":17865},"\u002Ferrors\u002Fattributeerror-object-has-no-attribute-fix\u002F","how to fix ",[33,17868,17869],{},"AttributeError: object has no attribute",[1065,17871,17873],{"id":17872},"confusing-functions-with-methods","Confusing functions with methods",[14,17875,17876],{},"These are different:",[26,17878,17880],{"className":28,"code":17879,"language":30,"meta":31,"style":31},"text = \"hello\"\n\nprint(len(text))    # function\nprint(text.upper()) # method\n",[33,17881,17882,17894,17898,17915],{"__ignoreMap":31},[36,17883,17884,17886,17888,17890,17892],{"class":38,"line":39},[36,17885,9407],{"class":42},[36,17887,47],{"class":46},[36,17889,224],{"class":141},[36,17891,2671],{"class":145},[36,17893,1954],{"class":141},[36,17895,17896],{"class":38,"line":72},[36,17897,476],{"emptyLinePlaceholder":475},[36,17899,17900,17902,17904,17906,17908,17910,17912],{"class":38,"line":89},[36,17901,76],{"class":75},[36,17903,79],{"class":50},[36,17905,999],{"class":75},[36,17907,79],{"class":50},[36,17909,4695],{"class":82},[36,17911,16574],{"class":50},[36,17913,17914],{"class":2570},"    # function\n",[36,17916,17917,17919,17921,17923,17925,17927,17929],{"class":38,"line":496},[36,17918,76],{"class":75},[36,17920,79],{"class":50},[36,17922,4695],{"class":82},[36,17924,351],{"class":50},[36,17926,13483],{"class":82},[36,17928,17757],{"class":50},[36,17930,17931],{"class":2570}," # method\n",[14,17933,17934],{},"Both are valid, but they are called differently.",[1065,17936,17938],{"id":17937},"not-understanding-that-methods-belong-to-objects","Not understanding that methods belong to objects",[14,17940,17941],{},"If you are not sure what kind of object you have, these tools help:",[26,17943,17945],{"className":28,"code":17944,"language":30,"meta":31,"style":31},"value = \"hello\"\n\nprint(type(value))\nprint(dir(value))\nhelp(value.upper)\n",[33,17946,17947,17959,17963,17977,17992],{"__ignoreMap":31},[36,17948,17949,17951,17953,17955,17957],{"class":38,"line":39},[36,17950,2664],{"class":42},[36,17952,47],{"class":46},[36,17954,224],{"class":141},[36,17956,2671],{"class":145},[36,17958,1954],{"class":141},[36,17960,17961],{"class":38,"line":72},[36,17962,476],{"emptyLinePlaceholder":475},[36,17964,17965,17967,17969,17971,17973,17975],{"class":38,"line":89},[36,17966,76],{"class":75},[36,17968,79],{"class":50},[36,17970,97],{"class":96},[36,17972,79],{"class":50},[36,17974,2686],{"class":82},[36,17976,104],{"class":50},[36,17978,17979,17981,17983,17986,17988,17990],{"class":38,"line":496},[36,17980,76],{"class":75},[36,17982,79],{"class":50},[36,17984,17985],{"class":75},"dir",[36,17987,79],{"class":50},[36,17989,2686],{"class":82},[36,17991,104],{"class":50},[36,17993,17994,17996,17998,18000,18002,18004],{"class":38,"line":501},[36,17995,4392],{"class":75},[36,17997,79],{"class":50},[36,17999,2686],{"class":82},[36,18001,351],{"class":50},[36,18003,13483],{"class":3065},[36,18005,86],{"class":50},[14,18007,18008],{},"Useful debugging commands:",[157,18010,18011,18016,18022],{},[160,18012,18013,18015],{},[33,18014,5799],{}," shows the type",[160,18017,18018,18021],{},[33,18019,18020],{},"dir(value)"," shows available attributes and methods",[160,18023,18024,18027],{},[33,18025,18026],{},"help(value.method_name)"," shows documentation for a specific method",[21,18029,1063],{"id":1062},[1065,18031,18033],{"id":18032},"is-a-method-the-same-as-a-function-in-python","Is a method the same as a function in Python?",[14,18035,18036],{},"Not exactly. A method is a function attached to an object or class.",[1065,18038,18040],{"id":18039},"how-do-i-know-if-something-is-a-method","How do I know if something is a method?",[14,18042,18043,18044,18047],{},"If you call it on an object with dot notation, like ",[33,18045,18046],{},"text.lower()",", it is a method.",[1065,18049,18051],{"id":18050},"why-does-python-use-both-functions-and-methods","Why does Python use both functions and methods?",[14,18053,18054],{},"Some actions make more sense as object behavior, while others work as general-purpose functions.",[1065,18056,18058],{"id":18057},"what-does-self-mean-in-a-method","What does self mean in a method?",[14,18060,18061,18063],{},[33,18062,3045],{}," refers to the current object and is used inside class methods.",[1065,18065,18067],{"id":18066},"can-i-create-my-own-methods","Can I create my own methods?",[14,18069,18070],{},"Yes. When you define functions inside a class, they become methods of that class.",[21,18072,1105],{"id":1104},[157,18074,18075,18079,18083,18088,18092,18096],{},[160,18076,18077],{},[347,18078,3343],{"href":3342},[160,18080,18081],{},[347,18082,4627],{"href":3232},[160,18084,18085],{},[347,18086,18087],{"href":17346},"What is a class in Python?",[160,18089,18090],{},[347,18091,641],{"href":16668},[160,18093,18094],{},[347,18095,16794],{"href":16793},[160,18097,18098],{},[347,18099,18100,18101],{"href":17865},"How to fix ",[33,18102,17869],{},[1137,18104,18105],{},"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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .smCYv, html code.shiki .smCYv{--shiki-light:#E53935;--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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":18107},[18108,18109,18110,18115,18116,18120,18126,18133],{"id":7433,"depth":72,"text":7434},{"id":16504,"depth":72,"text":16505},{"id":16671,"depth":72,"text":16672,"children":18111},[18112,18113,18114],{"id":16678,"depth":89,"text":16679},{"id":16797,"depth":89,"text":16798},{"id":16963,"depth":89,"text":16964},{"id":17158,"depth":72,"text":17159},{"id":17334,"depth":72,"text":17335,"children":18117},[18118],{"id":17450,"depth":89,"text":18119},"What self means",{"id":17691,"depth":72,"text":17692,"children":18121},[18122,18123,18124,18125],{"id":17698,"depth":89,"text":17699},{"id":17804,"depth":89,"text":17805},{"id":17872,"depth":89,"text":17873},{"id":17937,"depth":89,"text":17938},{"id":1062,"depth":72,"text":1063,"children":18127},[18128,18129,18130,18131,18132],{"id":18032,"depth":89,"text":18033},{"id":18039,"depth":89,"text":18040},{"id":18050,"depth":89,"text":18051},{"id":18057,"depth":89,"text":18058},{"id":18066,"depth":89,"text":18067},{"id":1104,"depth":72,"text":1105},"Master what is a method in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-method-in-python",{"title":16328,"description":18134},"glossary\u002Fwhat-is-a-method-in-python","uFw1fQX0mgEuhu-MTxahLKIdwLt-kkHxY9WlorKjuqU",{"id":18141,"title":18142,"body":18143,"description":18913,"extension":1159,"meta":18914,"navigation":475,"path":18915,"seo":18916,"stem":18917,"__hash__":18918},"content\u002Fglossary\u002Fwhat-is-a-module-in-python.md","What Is a Module in Python?",{"type":7,"value":18144,"toc":18891},[18145,18148,18154,18157,18172,18179,18182,18190,18201,18203,18210,18217,18220,18243,18246,18251,18255,18258,18261,18287,18290,18294,18297,18300,18341,18344,18408,18411,18423,18429,18433,18438,18444,18447,18462,18473,18477,18487,18490,18507,18509,18545,18547,18556,18563,18568,18571,18575,18578,18584,18617,18623,18662,18664,18678,18681,18702,18705,18714,18716,18719,18740,18750,18753,18755,18757,18803,18812,18814,18818,18821,18827,18831,18834,18837,18841,18844,18848,18850,18860,18862,18888],[10,18146,18142],{"id":18147},"what-is-a-module-in-python",[14,18149,3355,18150,18153],{},[331,18151,18152],{},"module"," in Python is a file that contains code you can reuse.",[14,18155,18156],{},"That file can contain:",[157,18158,18159,18161,18163,18166,18169],{},[160,18160,4323],{},[160,18162,4313],{},[160,18164,18165],{},"classes",[160,18167,18168],{},"import statements",[160,18170,18171],{},"code that runs when the file is imported or executed",[14,18173,18174,18175,18178],{},"You use a module by ",[331,18176,18177],{},"importing"," it into another Python file.",[14,18180,18181],{},"A simple way to think about it is this:",[157,18183,18184,18187],{},[160,18185,18186],{},"one file stores useful code",[160,18188,18189],{},"another file uses that code",[14,18191,18192,18193,18196,18197,351],{},"This page explains what a module is and why it matters. If you want the full mechanics of ",[33,18194,18195],{},"import",", see ",[347,18198,18200],{"href":18199},"\u002Flearn\u002Fhow-import-works-in-python\u002F","how import works in Python",[21,18202,6101],{"id":6100},[14,18204,18205,18206,18209],{},"A Python module is usually a single ",[33,18207,18208],{},".py"," file.",[14,18211,18212,18213,18216],{},"For example, if you have a file named ",[33,18214,18215],{},"helpers.py",", that file can be a module. Another file can import it and use code from it.",[14,18218,18219],{},"Here is the basic idea:",[157,18221,18222,18227,18235],{},[160,18223,18224,18226],{},[33,18225,18215],{}," contains reusable code",[160,18228,18229,18232,18233],{},[33,18230,18231],{},"main.py"," imports ",[33,18234,18215],{},[160,18236,18237,18239,18240,18242],{},[33,18238,18231],{}," uses what ",[33,18241,18215],{}," provides",[14,18244,18245],{},"In beginner terms, this is the most important idea:",[17321,18247,18248],{},[14,18249,18250],{},"A module is a Python file that helps you organize and reuse code.",[21,18252,18254],{"id":18253},"why-modules-are-useful","Why modules are useful",[14,18256,18257],{},"Modules make Python programs easier to work with.",[14,18259,18260],{},"They help because:",[157,18262,18263,18269,18275,18281],{},[160,18264,18265,18268],{},[331,18266,18267],{},"They organize code"," into smaller files",[160,18270,18271,18274],{},[331,18272,18273],{},"They reduce repetition"," because you can reuse the same code",[160,18276,18277,18280],{},[331,18278,18279],{},"They improve readability"," by separating different parts of a program",[160,18282,18283,18286],{},[331,18284,18285],{},"They make projects easier to maintain"," as your code grows",[14,18288,18289],{},"Without modules, you may end up putting everything into one large file. That quickly becomes hard to read and update.",[21,18291,18293],{"id":18292},"what-can-be-inside-a-module","What can be inside a module",[14,18295,18296],{},"A module can contain many kinds of Python code.",[14,18298,18299],{},"Common examples include:",[157,18301,18302,18314,18325,18335],{},[160,18303,18304,18307,18308,1180,18311],{},[331,18305,18306],{},"Functions"," such as ",[33,18309,18310],{},"greet()",[33,18312,18313],{},"calculate_total()",[160,18315,18316,18307,18319,1180,18322],{},[331,18317,18318],{},"Variables",[33,18320,18321],{},"TAX_RATE",[33,18323,18324],{},"app_name",[160,18326,18327,18307,18330,1180,18333],{},[331,18328,18329],{},"Classes",[33,18331,18332],{},"User",[33,18334,3333],{},[160,18336,18337,18340],{},[331,18338,18339],{},"Imports and supporting code"," used by the module",[14,18342,18343],{},"Example module:",[26,18345,18347],{"className":28,"code":18346,"language":30,"meta":31,"style":31},"# helpers.py\n\napp_name = \"My Program\"\n\ndef greet(name):\n    return f\"Hello, {name}!\"\n",[33,18348,18349,18354,18358,18372,18376,18388],{"__ignoreMap":31},[36,18350,18351],{"class":38,"line":39},[36,18352,18353],{"class":2570},"# helpers.py\n",[36,18355,18356],{"class":38,"line":72},[36,18357,476],{"emptyLinePlaceholder":475},[36,18359,18360,18363,18365,18367,18370],{"class":38,"line":89},[36,18361,18362],{"class":42},"app_name ",[36,18364,47],{"class":46},[36,18366,224],{"class":141},[36,18368,18369],{"class":145},"My Program",[36,18371,1954],{"class":141},[36,18373,18374],{"class":38,"line":496},[36,18375,476],{"emptyLinePlaceholder":475},[36,18377,18378,18380,18382,18384,18386],{"class":38,"line":501},[36,18379,8785],{"class":137},[36,18381,9033],{"class":3082},[36,18383,79],{"class":50},[36,18385,817],{"class":3050},[36,18387,3054],{"class":50},[36,18389,18390,18392,18395,18398,18400,18402,18405],{"class":38,"line":522},[36,18391,9201],{"class":1718},[36,18393,18394],{"class":137}," f",[36,18396,18397],{"class":145},"\"Hello, ",[36,18399,4552],{"class":54},[36,18401,817],{"class":42},[36,18403,18404],{"class":54},"}",[36,18406,18407],{"class":145},"!\"\n",[14,18409,18410],{},"This module contains:",[157,18412,18413,18418],{},[160,18414,18415,18416],{},"one variable: ",[33,18417,18324],{},[160,18419,18420,18421],{},"one function: ",[33,18422,18310],{},[14,18424,18425,18426,18428],{},"A module does ",[331,18427,8193],{}," have to contain only functions. Beginners often assume that, but modules can store many kinds of code.",[21,18430,18432],{"id":18431},"module-vs-package","Module vs package",[14,18434,3355,18435,18437],{},[331,18436,18152],{}," is usually one Python file.",[14,18439,3355,18440,18443],{},[331,18441,18442],{},"package"," is a folder that groups related modules together.",[14,18445,18446],{},"So the difference is:",[157,18448,18449,18457],{},[160,18450,18451,18453,18454,18456],{},[331,18452,18152],{}," = one ",[33,18455,18208],{}," file",[160,18458,18459,18461],{},[331,18460,18442],{}," = one folder containing related Python code",[14,18463,18464,18465,18468,18469,351],{},"This page is only defining the term ",[18466,18467,18152],"em",{},". For the full distinction, see ",[347,18470,18472],{"href":18471},"\u002Fglossary\u002Fwhat-is-a-package-in-python\u002F","what is a package in Python",[21,18474,18476],{"id":18475},"built-in-modules-and-your-own-modules","Built-in modules and your own modules",[14,18478,18479,18480,1180,18483,18486],{},"Some modules come with Python already installed. These are often called ",[331,18481,18482],{},"built-in",[331,18484,18485],{},"standard library"," modules.",[14,18488,18489],{},"Examples include:",[157,18491,18492,18497,18502],{},[160,18493,18494],{},[33,18495,18496],{},"math",[160,18498,18499],{},[33,18500,18501],{},"random",[160,18503,18504],{},[33,18505,18506],{},"os",[14,18508,1235],{},[26,18510,18512],{"className":28,"code":18511,"language":30,"meta":31,"style":31},"import math\n\nprint(math.sqrt(9))\n",[33,18513,18514,18521,18525],{"__ignoreMap":31},[36,18515,18516,18518],{"class":38,"line":39},[36,18517,18195],{"class":1718},[36,18519,18520],{"class":42}," math\n",[36,18522,18523],{"class":38,"line":72},[36,18524,476],{"emptyLinePlaceholder":475},[36,18526,18527,18529,18531,18533,18535,18538,18540,18543],{"class":38,"line":89},[36,18528,76],{"class":75},[36,18530,79],{"class":50},[36,18532,18496],{"class":82},[36,18534,351],{"class":50},[36,18536,18537],{"class":82},"sqrt",[36,18539,79],{"class":50},[36,18541,18542],{"class":54},"9",[36,18544,104],{"class":50},[14,18546,15916],{},[26,18548,18550],{"className":28,"code":18549,"language":30,"meta":31,"style":31},"3.0\n",[33,18551,18552],{"__ignoreMap":31},[36,18553,18554],{"class":38,"line":39},[36,18555,18549],{"class":54},[14,18557,18558,18559,351],{},"You can learn more from this ",[347,18560,18562],{"href":18561},"\u002Fstandard-library\u002Fpython-math-module-overview\u002F","Python math module overview",[14,18564,18565,18566,18209],{},"You can also create your own module by making a ",[33,18567,18208],{},[14,18569,18570],{},"Beginners often start by importing built-in modules first. Later, when programs get larger, custom modules become very useful for organizing your own code.",[21,18572,18574],{"id":18573},"basic-example-idea","Basic example idea",[14,18576,18577],{},"Here is a minimal example showing one custom module and one file that uses it.",[1065,18579,18581,18582],{"id":18580},"file-1-helperspy","File 1: ",[33,18583,18215],{},[26,18585,18587],{"className":28,"code":18586,"language":30,"meta":31,"style":31},"def greet(name):\n    return f\"Hello, {name}!\"\n",[33,18588,18589,18601],{"__ignoreMap":31},[36,18590,18591,18593,18595,18597,18599],{"class":38,"line":39},[36,18592,8785],{"class":137},[36,18594,9033],{"class":3082},[36,18596,79],{"class":50},[36,18598,817],{"class":3050},[36,18600,3054],{"class":50},[36,18602,18603,18605,18607,18609,18611,18613,18615],{"class":38,"line":72},[36,18604,9201],{"class":1718},[36,18606,18394],{"class":137},[36,18608,18397],{"class":145},[36,18610,4552],{"class":54},[36,18612,817],{"class":42},[36,18614,18404],{"class":54},[36,18616,18407],{"class":145},[1065,18618,18620,18621],{"id":18619},"file-2-mainpy","File 2: ",[33,18622,18231],{},[26,18624,18626],{"className":28,"code":18625,"language":30,"meta":31,"style":31},"import helpers\n\nprint(helpers.greet(\"Ava\"))\n",[33,18627,18628,18635,18639],{"__ignoreMap":31},[36,18629,18630,18632],{"class":38,"line":39},[36,18631,18195],{"class":1718},[36,18633,18634],{"class":42}," helpers\n",[36,18636,18637],{"class":38,"line":72},[36,18638,476],{"emptyLinePlaceholder":475},[36,18640,18641,18643,18645,18648,18650,18652,18654,18656,18658,18660],{"class":38,"line":89},[36,18642,76],{"class":75},[36,18644,79],{"class":50},[36,18646,18647],{"class":82},"helpers",[36,18649,351],{"class":50},[36,18651,9067],{"class":82},[36,18653,79],{"class":50},[36,18655,214],{"class":141},[36,18657,11988],{"class":145},[36,18659,214],{"class":141},[36,18661,104],{"class":50},[14,18663,15916],{},[26,18665,18667],{"className":28,"code":18666,"language":30,"meta":31,"style":31},"Hello, Ava!\n",[33,18668,18669],{"__ignoreMap":31},[36,18670,18671,18673,18675],{"class":38,"line":39},[36,18672,8801],{"class":42},[36,18674,58],{"class":50},[36,18676,18677],{"class":42}," Ava!\n",[14,18679,18680],{},"What is happening here:",[157,18682,18683,18689,18694],{},[160,18684,18685,6195,18687],{},[33,18686,18215],{},[331,18688,18152],{},[160,18690,18691,18693],{},[33,18692,18231],{}," imports that module",[160,18695,18696,18698,18699,18701],{},[33,18697,18231],{}," uses the ",[33,18700,18310],{}," function from the module",[14,18703,18704],{},"This is one of the simplest examples of a Python module.",[14,18706,18707,18708,1180,18712,351],{},"If you want to go deeper into the import statement, read ",[347,18709,18711],{"href":18710},"\u002Flearn\u002Fpython-modules-explained\u002F","Python modules explained",[347,18713,18200],{"href":18199},[21,18715,10615],{"id":10614},[14,18717,18718],{},"A few details are easy to miss when you are new to modules.",[157,18720,18721,18727,18734,18737],{},[160,18722,18723,18724,18726],{},"A module is ",[331,18725,8193],{}," the same as a package",[160,18728,18729,18730,18733],{},"Importing a module runs ",[331,18731,18732],{},"top-level code"," in that file",[160,18735,18736],{},"The file name matters when importing",[160,18738,18739],{},"You should avoid naming your file the same as a standard library module",[14,18741,18742,18743,18746,18747,18749],{},"For example, if you create a file named ",[33,18744,18745],{},"random.py",", it can conflict with Python’s built-in ",[33,18748,18501],{}," module.",[14,18751,18752],{},"That can cause confusing import problems later.",[21,18754,905],{"id":904},[14,18756,8484],{},[157,18758,18759,18767,18778,18786],{},[160,18760,18761,18764,18766],{},[331,18762,18763],{},"Confusing a module with a package",[335,18765],{},"\nA module is usually one file. A package is a folder of related modules.",[160,18768,18769,18772,18774,18775,18777],{},[331,18770,18771],{},"Thinking a module must be built in",[335,18773],{},"\nBuilt-in modules are modules, but your own ",[33,18776,18208],{}," files can be modules too.",[160,18779,18780,18783,18785],{},[331,18781,18782],{},"Assuming a module can only contain functions",[335,18784],{},"\nModules can also contain variables, classes, imports, and other code.",[160,18787,18788,18791,18793,18794,5950,18796,6128,18799,18802],{},[331,18789,18790],{},"Naming a file after a standard library module",[335,18792],{},"\nAvoid names like ",[33,18795,18745],{},[33,18797,18798],{},"json.py",[33,18800,18801],{},"math.py",". These can shadow the real module and cause import errors.",[14,18804,18805,18806,351],{},"If Python cannot find the module you expect, see ",[347,18807,17866,18809],{"href":18808},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix\u002F",[33,18810,18811],{},"ModuleNotFoundError: No module named 'x'",[21,18813,1063],{"id":1062},[1065,18815,18817],{"id":18816},"is-a-module-the-same-as-a-python-file","Is a module the same as a Python file?",[14,18819,18820],{},"Usually, yes.",[14,18822,18823,18824,18826],{},"In beginner terms, a module is commonly a ",[33,18825,18208],{}," file that can be imported.",[1065,18828,18830],{"id":18829},"can-i-make-my-own-module","Can I make my own module?",[14,18832,18833],{},"Yes.",[14,18835,18836],{},"Create a Python file and import it from another file.",[1065,18838,18840],{"id":18839},"what-is-the-difference-between-a-module-and-a-package","What is the difference between a module and a package?",[14,18842,18843],{},"A module is one file. A package is a folder that groups modules together.",[1065,18845,18847],{"id":18846},"do-built-in-modules-count-as-modules","Do built-in modules count as modules?",[14,18849,18833],{},[14,18851,18852,18853,5950,18855,5953,18857,18859],{},"Modules like ",[33,18854,18496],{},[33,18856,18501],{},[33,18858,18506],{}," are Python modules.",[21,18861,1105],{"id":1104},[157,18863,18864,18868,18873,18878,18882],{},[160,18865,18866],{},[347,18867,18711],{"href":18710},[160,18869,18870],{},[347,18871,18872],{"href":18199},"How import works in Python",[160,18874,18875],{},[347,18876,18877],{"href":18471},"What is a package in Python?",[160,18879,18880],{},[347,18881,18562],{"href":18561},[160,18883,18884],{},[347,18885,18100,18886],{"href":18808},[33,18887,18811],{},[1137,18889,18890],{},"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 .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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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}",{"title":31,"searchDepth":72,"depth":72,"links":18892},[18893,18894,18895,18896,18897,18898,18904,18905,18906,18912],{"id":6100,"depth":72,"text":6101},{"id":18253,"depth":72,"text":18254},{"id":18292,"depth":72,"text":18293},{"id":18431,"depth":72,"text":18432},{"id":18475,"depth":72,"text":18476},{"id":18573,"depth":72,"text":18574,"children":18899},[18900,18902],{"id":18580,"depth":89,"text":18901},"File 1: helpers.py",{"id":18619,"depth":89,"text":18903},"File 2: main.py",{"id":10614,"depth":72,"text":10615},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":18907},[18908,18909,18910,18911],{"id":18816,"depth":89,"text":18817},{"id":18829,"depth":89,"text":18830},{"id":18839,"depth":89,"text":18840},{"id":18846,"depth":89,"text":18847},{"id":1104,"depth":72,"text":1105},"Master what is a module in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-module-in-python",{"title":18142,"description":18913},"glossary\u002Fwhat-is-a-module-in-python","Z48eM-EacvLNUUHwMGEDdwdzueQ7jHeHVQBZfJyRITA",{"id":18920,"title":18921,"body":18922,"description":19673,"extension":1159,"meta":19674,"navigation":475,"path":19675,"seo":19676,"stem":19677,"__hash__":19678},"content\u002Fglossary\u002Fwhat-is-a-package-in-python.md","What Is a Package in Python?",{"type":7,"value":18923,"toc":19652},[18924,18927,18939,18942,18948,19023,19027,19057,19060,19064,19067,19069,19097,19099,19112,19121,19125,19128,19152,19154,19182,19184,19197,19207,19211,19214,19217,19224,19226,19245,19248,19255,19257,19263,19266,19270,19273,19276,19293,19296,19300,19303,19305,19315,19322,19325,19341,19348,19355,19364,19367,19375,19389,19392,19396,19402,19405,19429,19432,19454,19456,19459,19483,19486,19541,19544,19567,19578,19580,19584,19587,19591,19594,19600,19603,19607,19610,19616,19622,19624,19649],[10,18925,18921],{"id":18926},"what-is-a-package-in-python",[14,18928,3355,18929,18931,18932,18934,18935,18938],{},[331,18930,18442],{}," in Python is a way to organize related code.",[335,18933],{},"\nIn simple terms, a package is usually a ",[331,18936,18937],{},"folder"," that contains Python modules and sometimes other package folders.",[14,18940,18941],{},"This helps keep code grouped in a clear structure instead of putting everything into one large file.",[14,18943,18944,18945,18947],{},"Beginners often use packages before they know the term. For example, when you write an ",[33,18946,18195],{}," statement, you may already be using a module that belongs to a package.",[26,18949,18951],{"className":28,"code":18950,"language":30,"meta":31,"style":31},"import math\nimport json\n\nprint(math.sqrt(16))\nprint(json.dumps({\"name\": \"Sam\"}))\n",[33,18952,18953,18959,18966,18970,18989],{"__ignoreMap":31},[36,18954,18955,18957],{"class":38,"line":39},[36,18956,18195],{"class":1718},[36,18958,18520],{"class":42},[36,18960,18961,18963],{"class":38,"line":72},[36,18962,18195],{"class":1718},[36,18964,18965],{"class":42}," json\n",[36,18967,18968],{"class":38,"line":89},[36,18969,476],{"emptyLinePlaceholder":475},[36,18971,18972,18974,18976,18978,18980,18982,18984,18987],{"class":38,"line":496},[36,18973,76],{"class":75},[36,18975,79],{"class":50},[36,18977,18496],{"class":82},[36,18979,351],{"class":50},[36,18981,18537],{"class":82},[36,18983,79],{"class":50},[36,18985,18986],{"class":54},"16",[36,18988,104],{"class":50},[36,18990,18991,18993,18995,18998,19000,19003,19006,19008,19010,19012,19014,19016,19018,19020],{"class":38,"line":501},[36,18992,76],{"class":75},[36,18994,79],{"class":50},[36,18996,18997],{"class":82},"json",[36,18999,351],{"class":50},[36,19001,19002],{"class":82},"dumps",[36,19004,19005],{"class":50},"({",[36,19007,214],{"class":141},[36,19009,817],{"class":145},[36,19011,214],{"class":141},[36,19013,822],{"class":50},[36,19015,224],{"class":141},[36,19017,311],{"class":145},[36,19019,214],{"class":141},[36,19021,19022],{"class":50},"}))\n",[14,19024,19025],{},[331,19026,107],{},[26,19028,19030],{"className":28,"code":19029,"language":30,"meta":31,"style":31},"4.0\n{\"name\": \"Sam\"}\n",[33,19031,19032,19037],{"__ignoreMap":31},[36,19033,19034],{"class":38,"line":39},[36,19035,19036],{"class":54},"4.0\n",[36,19038,19039,19041,19043,19045,19047,19049,19051,19053,19055],{"class":38,"line":72},[36,19040,4552],{"class":50},[36,19042,214],{"class":141},[36,19044,817],{"class":145},[36,19046,214],{"class":141},[36,19048,822],{"class":50},[36,19050,224],{"class":141},[36,19052,311],{"class":145},[36,19054,214],{"class":141},[36,19056,803],{"class":50},[14,19058,19059],{},"A package is a way to organize Python code. Beginners often use packages before they fully understand them.",[21,19061,19063],{"id":19062},"what-a-package-means-in-python","What a package means in Python",[14,19065,19066],{},"A package helps organize related Python code.",[14,19068,18219],{},[157,19070,19071,19076,19082,19089,19092],{},[160,19072,19073,19074],{},"A package is usually a ",[331,19075,18937],{},[160,19077,19078,19079],{},"That folder contains one or more ",[331,19080,19081],{},"modules",[160,19083,3355,19084,19086,19087,18456],{},[331,19085,18152],{}," is usually a single ",[33,19088,18208],{},[160,19090,19091],{},"Packages make larger programs easier to manage",[160,19093,19094,19095],{},"You often notice packages when using ",[33,19096,18195],{},[14,19098,1235],{},[157,19100,19101,19106],{},[160,19102,19103,19105],{},[33,19104,18496],{}," is a module",[160,19107,19108,19109,19111],{},"A folder containing several related ",[33,19110,18208],{}," files can be a package",[14,19113,19114,19115,1180,19118,351],{},"If you are not fully sure what a module is, see ",[347,19116,19117],{"href":18915},"what is a module in Python",[347,19119,18711],{"href":19120},"\u002Flearn\u002Fpython-modules-explained",[21,19122,19124],{"id":19123},"package-vs-module","Package vs module",[14,19126,19127],{},"A package and a module are not the same thing.",[157,19129,19130,19135,19140,19146],{},[160,19131,3355,19132,19134],{},[331,19133,18152],{}," is usually one Python file",[160,19136,3355,19137,19139],{},[331,19138,18442],{}," is a collection of modules in a folder",[160,19141,19142,19143],{},"You import a module with code like ",[33,19144,19145],{},"import module_name",[160,19147,19148,19149],{},"You can import from a package with code like ",[33,19150,19151],{},"import package.module",[14,19153,197],{},[26,19155,19157],{"className":28,"code":19156,"language":30,"meta":31,"style":31},"import json\nimport xml.etree.ElementTree\n",[33,19158,19159,19165],{"__ignoreMap":31},[36,19160,19161,19163],{"class":38,"line":39},[36,19162,18195],{"class":1718},[36,19164,18965],{"class":42},[36,19166,19167,19169,19172,19174,19177,19179],{"class":38,"line":72},[36,19168,18195],{"class":1718},[36,19170,19171],{"class":42}," xml",[36,19173,351],{"class":50},[36,19175,19176],{"class":3065},"etree",[36,19178,351],{"class":50},[36,19180,19181],{"class":3065},"ElementTree\n",[14,19183,6187],{},[157,19185,19186,19191],{},[160,19187,19188,19190],{},[33,19189,18997],{}," is imported directly",[160,19192,19193,19196],{},[33,19194,19195],{},"xml.etree.ElementTree"," shows a name inside a larger package structure",[14,19198,19199,19200,1180,19203,351],{},"This page is only defining the term. If you want the full beginner explanation of imports, read ",[347,19201,18200],{"href":19202},"\u002Flearn\u002Fhow-import-works-in-python",[347,19204,19206],{"href":19205},"\u002Fhow-to\u002Fhow-to-import-a-module-in-python","how to import a module in Python",[21,19208,19210],{"id":19209},"simple-package-structure","Simple package structure",[14,19212,19213],{},"A package often looks like a folder containing multiple Python files.",[14,19215,19216],{},"Example structure:",[26,19218,19222],{"className":19219,"code":19221,"language":4695,"meta":31},[19220],"language-text","my_project\u002F\n└── tools\u002F\n    ├── __init__.py\n    ├── math_helpers.py\n    └── string_helpers.py\n",[33,19223,19221],{"__ignoreMap":31},[14,19225,6187],{},[157,19227,19228,19234,19239],{},[160,19229,19230,19233],{},[33,19231,19232],{},"tools"," is the package folder",[160,19235,19236,19105],{},[33,19237,19238],{},"math_helpers.py",[160,19240,19241,19244],{},[33,19242,19243],{},"string_helpers.py"," is another module",[14,19246,19247],{},"Related code is grouped together inside one folder.",[14,19249,19250,19251,19254],{},"A package can also contain ",[331,19252,19253],{},"subpackages",", which are package folders inside other package folders.",[14,19256,197],{},[26,19258,19261],{"className":19259,"code":19260,"language":4695,"meta":31},[19220],"my_project\u002F\n└── tools\u002F\n    ├── __init__.py\n    ├── math_helpers.py\n    └── text\u002F\n        ├── __init__.py\n        └── string_helpers.py\n",[33,19262,19260],{"__ignoreMap":31},[14,19264,19265],{},"This makes it easier to organize larger projects.",[21,19267,19269],{"id":19268},"why-packages-are-useful","Why packages are useful",[14,19271,19272],{},"Packages are useful because they help structure code clearly.",[14,19274,19275],{},"They help by:",[157,19277,19278,19281,19284,19287,19290],{},[160,19279,19280],{},"Keeping code organized",[160,19282,19283],{},"Grouping related features together",[160,19285,19286],{},"Reducing confusion in larger programs",[160,19288,19289],{},"Making code easier to reuse",[160,19291,19292],{},"Making code easier to share",[14,19294,19295],{},"Without packages, a project can quickly become messy if every feature is placed in one file.",[21,19297,19299],{"id":19298},"common-examples-beginners-already-use","Common examples beginners already use",[14,19301,19302],{},"Beginners often use packages and modules from Python’s standard library.",[14,19304,18299],{},[157,19306,19307,19311],{},[160,19308,19309],{},[33,19310,18997],{},[160,19312,19313],{},[33,19314,18506],{},[14,19316,19317,19318,19321],{},"You may also install third-party tools with ",[33,19319,19320],{},"pip",". Many of those are distributed as packages.",[14,19323,19324],{},"Important beginner note:",[157,19326,19327,19333,19336],{},[160,19328,19329,19330,19332],{},"You do ",[331,19331,8193],{}," need to build your own package before you can use one",[160,19334,19335],{},"You can use packages just by importing them",[160,19337,19338,19339],{},"You can install many external packages with ",[33,19340,19320],{},[14,19342,19343,19344,351],{},"If you want to do that next, see ",[347,19345,19347],{"href":19346},"\u002Fhow-to\u002Fhow-to-install-a-python-package-with-pip","how to install a Python package with pip",[21,19349,19351,19352],{"id":19350},"important-beginner-note-about-__init__py","Important beginner note about ",[33,19353,19354],{},"__init__.py",[14,19356,19357,19358,19361,19362,351],{},"Older explanations often say that a package ",[331,19359,19360],{},"must"," contain a file named ",[33,19363,19354],{},[14,19365,19366],{},"That file is still very common and still useful.",[14,19368,19369,19370,351,19372,19374],{},"In modern Python, some packages can work without ",[33,19371,19354],{},[335,19373],{},"\nFor beginner learning, the important thing to know is:",[157,19376,19377,19383,19386],{},[160,19378,19379,19380,19382],{},"You will often see ",[33,19381,19354],{}," in package folders",[160,19384,19385],{},"It is normal for package examples to include it",[160,19387,19388],{},"Its presence often helps show that a folder is meant to be part of a package structure",[14,19390,19391],{},"So if you see this file, do not worry. It is a normal part of many Python package examples.",[21,19393,19395],{"id":19394},"when-this-term-is-usually-confusing","When this term is usually confusing",[14,19397,19398,19399,19401],{},"The word ",[331,19400,18442],{}," is often confusing because beginners mix it up with other Python terms.",[14,19403,19404],{},"Common confusion points:",[157,19406,19407,19417,19420,19426],{},[160,19408,19409,19410,5950,19412,5953,19414],{},"Mixing up ",[331,19411,18442],{},[331,19413,18152],{},[331,19415,19416],{},"library",[160,19418,19419],{},"Thinking every imported name is a package",[160,19421,19422,19423,19425],{},"Thinking ",[33,19424,19320],{}," only installs modules",[160,19427,19428],{},"Confusing an ordinary folder with an importable package",[14,19430,19431],{},"A useful way to remember it:",[157,19433,19434,19442,19448],{},[160,19435,19436,19439,19440,18456],{},[331,19437,19438],{},"Module"," = usually one ",[33,19441,18208],{},[160,19443,19444,19447],{},[331,19445,19446],{},"Package"," = a folder that groups related modules",[160,19449,19450,19453],{},[331,19451,19452],{},"Library"," = a broader term for reusable code",[21,19455,905],{"id":904},[14,19457,19458],{},"These are common mistakes beginners make when learning this term:",[157,19460,19461,19467,19474,19477],{},[160,19462,19463,19464,19466],{},"Confusing a single ",[33,19465,18208],{}," file with a package",[160,19468,19422,19469,1281,19471,19473],{},[331,19470,18442],{},[331,19472,19416],{}," mean exactly the same thing",[160,19475,19476],{},"Not realizing that packages help organize multiple modules",[160,19478,19479,19480,19482],{},"Seeing ",[33,19481,19151],{}," and not knowing that it often matches a folder structure",[14,19484,19485],{},"If you run into import problems while working with packages, these commands can help:",[26,19487,19491],{"className":19488,"code":19489,"language":19490,"meta":31,"style":31},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python --version\npython -c \"import json; print(json.__file__)\"\npython -c \"import os; print(os.__name__)\"\npython -c \"import sys; print(sys.path)\"\n","bash",[33,19492,19493,19501,19515,19528],{"__ignoreMap":31},[36,19494,19495,19497],{"class":38,"line":39},[36,19496,30],{"class":3010},[36,19498,19500],{"class":19499},"stzsN"," --version\n",[36,19502,19503,19505,19508,19510,19513],{"class":38,"line":72},[36,19504,30],{"class":3010},[36,19506,19507],{"class":19499}," -c",[36,19509,224],{"class":141},[36,19511,19512],{"class":145},"import json; print(json.__file__)",[36,19514,1954],{"class":141},[36,19516,19517,19519,19521,19523,19526],{"class":38,"line":89},[36,19518,30],{"class":3010},[36,19520,19507],{"class":19499},[36,19522,224],{"class":141},[36,19524,19525],{"class":145},"import os; print(os.__name__)",[36,19527,1954],{"class":141},[36,19529,19530,19532,19534,19536,19539],{"class":38,"line":496},[36,19531,30],{"class":3010},[36,19533,19507],{"class":19499},[36,19535,224],{"class":141},[36,19537,19538],{"class":145},"import sys; print(sys.path)",[36,19540,1954],{"class":141},[14,19542,19543],{},"What these do:",[157,19545,19546,19552,19557,19562],{},[160,19547,19548,19551],{},[33,19549,19550],{},"python --version"," shows which Python version you are using",[160,19553,19554,19556],{},[33,19555,19512],{}," shows where the imported code comes from",[160,19558,19559,19561],{},[33,19560,19525],{}," shows the imported module name",[160,19563,19564,19566],{},[33,19565,19538],{}," shows where Python looks for importable code",[14,19568,19569,19570,1180,19574,351],{},"If Python cannot find a package or module, see ",[347,19571,19573],{"href":19572},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix","ModuleNotFoundError: No module named x",[347,19575,19577],{"href":19576},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix","ImportError: No module named x",[21,19579,1063],{"id":1062},[1065,19581,19583],{"id":19582},"is-a-package-the-same-as-a-module-in-python","Is a package the same as a module in Python?",[14,19585,19586],{},"No. A module is usually one Python file. A package is a folder that groups related modules.",[1065,19588,19590],{"id":19589},"is-every-folder-a-python-package","Is every folder a Python package?",[14,19592,19593],{},"No. A folder becomes useful as a package when Python can import from it.",[1065,19595,19597,19598,1248],{"id":19596},"do-python-packages-always-need-__init__py","Do Python packages always need ",[33,19599,19354],{},[14,19601,19602],{},"Not always in modern Python, but beginners will often see that file in package examples.",[1065,19604,19606],{"id":19605},"what-is-the-difference-between-a-package-and-a-library","What is the difference between a package and a library?",[14,19608,19609],{},"A package is a Python code organization unit. A library is a broader term for reusable code and may contain one or more packages.",[1065,19611,19613,19614,1248],{"id":19612},"can-i-install-packages-with-pip","Can I install packages with ",[33,19615,19320],{},[14,19617,19618,19619,19621],{},"Yes. Many third-party tools installed with ",[33,19620,19320],{}," are distributed as packages.",[21,19623,1105],{"id":1104},[157,19625,19626,19631,19635,19639,19644],{},[160,19627,19628],{},[347,19629,19630],{"href":18915},"What is a module in Python?",[160,19632,19633],{},[347,19634,18711],{"href":19120},[160,19636,19637],{},[347,19638,18872],{"href":19202},[160,19640,19641],{},[347,19642,19643],{"href":19205},"How to import a module in Python",[160,19645,19646],{},[347,19647,19648],{"href":19346},"How to install a Python package with pip",[1137,19650,19651],{},"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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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 .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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":19653},[19654,19655,19656,19657,19658,19659,19661,19662,19663,19672],{"id":19062,"depth":72,"text":19063},{"id":19123,"depth":72,"text":19124},{"id":19209,"depth":72,"text":19210},{"id":19268,"depth":72,"text":19269},{"id":19298,"depth":72,"text":19299},{"id":19350,"depth":72,"text":19660},"Important beginner note about __init__.py",{"id":19394,"depth":72,"text":19395},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":19664},[19665,19666,19667,19669,19670],{"id":19582,"depth":89,"text":19583},{"id":19589,"depth":89,"text":19590},{"id":19596,"depth":89,"text":19668},"Do Python packages always need __init__.py?",{"id":19605,"depth":89,"text":19606},{"id":19612,"depth":89,"text":19671},"Can I install packages with pip?",{"id":1104,"depth":72,"text":1105},"Master what is a package in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-package-in-python",{"title":18921,"description":19673},"glossary\u002Fwhat-is-a-package-in-python","plJQM12dREgcyD_U59ahFyEs90e3GnTg_jOjcZizaIo",{"id":19680,"title":19681,"body":19682,"description":20896,"extension":1159,"meta":20897,"navigation":475,"path":11767,"seo":20898,"stem":20899,"__hash__":20900},"content\u002Fglossary\u002Fwhat-is-a-parameter-in-python.md","What Is a Parameter in Python?",{"type":7,"value":19683,"toc":20868},[19684,19687,19693,19696,19699,19710,19716,19718,19772,19785,19787,19796,19800,19806,19809,19823,19825,19828,19831,19834,19838,19841,19844,19852,19854,19886,19889,19899,19902,19906,19909,19921,19923,19975,19977,19989,19992,20004,20015,20019,20022,20025,20028,20109,20111,20130,20133,20137,20141,20199,20202,20213,20215,20230,20234,20287,20289,20303,20305,20313,20317,20400,20403,20419,20421,20446,20449,20453,20456,20470,20473,20509,20511,20519,20522,20526,20529,20533,20536,20539,20547,20551,20554,20556,20591,20596,20615,20619,20625,20670,20676,20683,20685,20688,20707,20710,20771,20779,20781,20789,20797,20799,20803,20806,20810,20813,20817,20820,20824,20827,20831,20837,20839,20862,20865],[10,19685,19681],{"id":19686},"what-is-a-parameter-in-python",[14,19688,3355,19689,19692],{},[331,19690,19691],{},"parameter"," in Python is a variable written inside the parentheses of a function definition.",[14,19694,19695],{},"It acts like a placeholder. When you call the function later, Python gives that parameter a value.",[14,19697,19698],{},"This page focuses on the basic idea:",[157,19700,19701,19704,19707],{},[160,19702,19703],{},"what a parameter is",[160,19705,19706],{},"where it appears",[160,19708,19709],{},"how it is different from an argument",[14,19711,19712,19713,351],{},"It is not a full guide to writing functions. If you want the bigger picture, see ",[347,19714,19715],{"href":8992},"Python Functions Explained",[21,19717,24],{"id":23},[26,19719,19721],{"className":28,"code":19720,"language":30,"meta":31,"style":31},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Mia\")\n",[33,19722,19723,19735,19753,19757],{"__ignoreMap":31},[36,19724,19725,19727,19729,19731,19733],{"class":38,"line":39},[36,19726,8785],{"class":137},[36,19728,9033],{"class":3082},[36,19730,79],{"class":50},[36,19732,817],{"class":3050},[36,19734,3054],{"class":50},[36,19736,19737,19739,19741,19743,19745,19747,19749,19751],{"class":38,"line":72},[36,19738,1735],{"class":75},[36,19740,79],{"class":50},[36,19742,214],{"class":141},[36,19744,8801],{"class":145},[36,19746,214],{"class":141},[36,19748,58],{"class":50},[36,19750,3051],{"class":82},[36,19752,86],{"class":50},[36,19754,19755],{"class":38,"line":89},[36,19756,476],{"emptyLinePlaceholder":475},[36,19758,19759,19761,19763,19765,19768,19770],{"class":38,"line":496},[36,19760,9067],{"class":82},[36,19762,79],{"class":50},[36,19764,214],{"class":141},[36,19766,19767],{"class":145},"Mia",[36,19769,214],{"class":141},[36,19771,86],{"class":50},[14,19773,3765,19774,6195,19776,8624,19778,6195,19781,19784],{},[33,19775,817],{},[331,19777,19691],{},[33,19779,19780],{},"\"Mia\"",[331,19782,19783],{},"argument"," passed to the function.",[14,19786,107],{},[26,19788,19790],{"className":28,"code":19789,"language":30,"meta":31,"style":31},"Hello Mia\n",[33,19791,19792],{"__ignoreMap":31},[36,19793,19794],{"class":38,"line":39},[36,19795,19789],{"class":42},[21,19797,19799],{"id":19798},"page-purpose","Page purpose",[14,19801,19802,19803],{},"This page answers a common beginner question: ",[331,19804,19805],{},"what is a parameter in Python?",[14,19807,19808],{},"It keeps the topic narrow and practical:",[157,19810,19811,19814,19817,19820],{},[160,19812,19813],{},"a clear definition",[160,19815,19816],{},"where parameters appear",[160,19818,19819],{},"simple examples",[160,19821,19822],{},"the difference between parameters and arguments",[21,19824,6101],{"id":6100},[14,19826,19827],{},"A parameter is a variable listed inside the parentheses when you define a function.",[14,19829,19830],{},"It is a placeholder for a value the function will receive later.",[14,19832,19833],{},"Parameters are useful because they let one function work with different input values instead of being hard-coded for just one case.",[21,19835,19837],{"id":19836},"where-parameters-appear","Where parameters appear",[14,19839,19840],{},"Parameters appear in the function definition line.",[14,19842,19843],{},"They come:",[157,19845,19846,19849],{},[160,19847,19848],{},"after the function name",[160,19850,19851],{},"inside the parentheses",[14,19853,197],{},[26,19855,19857],{"className":28,"code":19856,"language":30,"meta":31,"style":31},"def add(a, b):\n    return a + b\n",[33,19858,19859,19876],{"__ignoreMap":31},[36,19860,19861,19863,19866,19868,19870,19872,19874],{"class":38,"line":39},[36,19862,8785],{"class":137},[36,19864,19865],{"class":3082}," add",[36,19867,79],{"class":50},[36,19869,347],{"class":3050},[36,19871,58],{"class":50},[36,19873,9194],{"class":3050},[36,19875,3054],{"class":50},[36,19877,19878,19880,19882,19884],{"class":38,"line":72},[36,19879,9201],{"class":1718},[36,19881,9204],{"class":42},[36,19883,4763],{"class":46},[36,19885,9209],{"class":42},[14,19887,19888],{},"In this function:",[157,19890,19891,19895],{},[160,19892,19893,9125],{},[33,19894,347],{},[160,19896,19897,9125],{},[33,19898,721],{},[14,19900,19901],{},"Both names are available as variables inside the function.",[21,19903,19905],{"id":19904},"parameter-vs-argument","Parameter vs argument",[14,19907,19908],{},"This is the part that confuses many beginners.",[157,19910,19911,19916],{},[160,19912,3355,19913,19915],{},[331,19914,19691],{}," is the name in the function definition",[160,19917,3361,19918,19920],{},[331,19919,19783],{}," is the actual value passed when calling the function",[14,19922,197],{},[26,19924,19925],{"className":28,"code":19720,"language":30,"meta":31,"style":31},[33,19926,19927,19939,19957,19961],{"__ignoreMap":31},[36,19928,19929,19931,19933,19935,19937],{"class":38,"line":39},[36,19930,8785],{"class":137},[36,19932,9033],{"class":3082},[36,19934,79],{"class":50},[36,19936,817],{"class":3050},[36,19938,3054],{"class":50},[36,19940,19941,19943,19945,19947,19949,19951,19953,19955],{"class":38,"line":72},[36,19942,1735],{"class":75},[36,19944,79],{"class":50},[36,19946,214],{"class":141},[36,19948,8801],{"class":145},[36,19950,214],{"class":141},[36,19952,58],{"class":50},[36,19954,3051],{"class":82},[36,19956,86],{"class":50},[36,19958,19959],{"class":38,"line":89},[36,19960,476],{"emptyLinePlaceholder":475},[36,19962,19963,19965,19967,19969,19971,19973],{"class":38,"line":496},[36,19964,9067],{"class":82},[36,19966,79],{"class":50},[36,19968,214],{"class":141},[36,19970,19767],{"class":145},[36,19972,214],{"class":141},[36,19974,86],{"class":50},[14,19976,4063],{},[157,19978,19979,19984],{},[160,19980,19981,19983],{},[33,19982,817],{}," is the parameter",[160,19985,19986,19988],{},[33,19987,19780],{}," is the argument",[14,19990,19991],{},"A simple way to remember it:",[157,19993,19994,19999],{},[160,19995,19996,19998],{},[331,19997,19691],{}," = the variable in the function",[160,20000,20001,20003],{},[331,20002,19783],{}," = the real value you send into it",[14,20005,20006,20007,20011,20012,351],{},"If you want a fuller comparison, read ",[347,20008,20010],{"href":20009},"\u002Flearn\u002Ffunction-parameters-and-arguments-in-python\u002F","Function Parameters and Arguments in Python"," or the glossary page ",[347,20013,20014],{"href":9015},"What Is an Argument in Python?",[21,20016,20018],{"id":20017},"why-parameters-are-useful","Why parameters are useful",[14,20020,20021],{},"Parameters make functions more reusable.",[14,20023,20024],{},"Without parameters, you often have to repeat similar code. With parameters, the same function can work with different data.",[14,20026,20027],{},"For example, this function can greet many people:",[26,20029,20031],{"className":28,"code":20030,"language":30,"meta":31,"style":31},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Mia\")\ngreet(\"Leo\")\ngreet(\"Ava\")\n",[33,20032,20033,20045,20063,20067,20081,20095],{"__ignoreMap":31},[36,20034,20035,20037,20039,20041,20043],{"class":38,"line":39},[36,20036,8785],{"class":137},[36,20038,9033],{"class":3082},[36,20040,79],{"class":50},[36,20042,817],{"class":3050},[36,20044,3054],{"class":50},[36,20046,20047,20049,20051,20053,20055,20057,20059,20061],{"class":38,"line":72},[36,20048,1735],{"class":75},[36,20050,79],{"class":50},[36,20052,214],{"class":141},[36,20054,8801],{"class":145},[36,20056,214],{"class":141},[36,20058,58],{"class":50},[36,20060,3051],{"class":82},[36,20062,86],{"class":50},[36,20064,20065],{"class":38,"line":89},[36,20066,476],{"emptyLinePlaceholder":475},[36,20068,20069,20071,20073,20075,20077,20079],{"class":38,"line":496},[36,20070,9067],{"class":82},[36,20072,79],{"class":50},[36,20074,214],{"class":141},[36,20076,19767],{"class":145},[36,20078,214],{"class":141},[36,20080,86],{"class":50},[36,20082,20083,20085,20087,20089,20091,20093],{"class":38,"line":501},[36,20084,9067],{"class":82},[36,20086,79],{"class":50},[36,20088,214],{"class":141},[36,20090,302],{"class":145},[36,20092,214],{"class":141},[36,20094,86],{"class":50},[36,20096,20097,20099,20101,20103,20105,20107],{"class":38,"line":522},[36,20098,9067],{"class":82},[36,20100,79],{"class":50},[36,20102,214],{"class":141},[36,20104,11988],{"class":145},[36,20106,214],{"class":141},[36,20108,86],{"class":50},[14,20110,107],{},[26,20112,20114],{"className":28,"code":20113,"language":30,"meta":31,"style":31},"Hello Mia\nHello Leo\nHello Ava\n",[33,20115,20116,20120,20125],{"__ignoreMap":31},[36,20117,20118],{"class":38,"line":39},[36,20119,19789],{"class":42},[36,20121,20122],{"class":38,"line":72},[36,20123,20124],{"class":42},"Hello Leo\n",[36,20126,20127],{"class":38,"line":89},[36,20128,20129],{"class":42},"Hello Ava\n",[14,20131,20132],{},"The function code stays the same. Only the argument changes.",[21,20134,20136],{"id":20135},"basic-examples-to-include","Basic examples to include",[1065,20138,20140],{"id":20139},"one-parameter-example","One parameter example",[26,20142,20144],{"className":28,"code":20143,"language":30,"meta":31,"style":31},"def show_color(color):\n    print(\"Color:\", color)\n\nshow_color(\"blue\")\n",[33,20145,20146,20160,20180,20184],{"__ignoreMap":31},[36,20147,20148,20150,20153,20155,20158],{"class":38,"line":39},[36,20149,8785],{"class":137},[36,20151,20152],{"class":3082}," show_color",[36,20154,79],{"class":50},[36,20156,20157],{"class":3050},"color",[36,20159,3054],{"class":50},[36,20161,20162,20164,20166,20168,20171,20173,20175,20178],{"class":38,"line":72},[36,20163,1735],{"class":75},[36,20165,79],{"class":50},[36,20167,214],{"class":141},[36,20169,20170],{"class":145},"Color:",[36,20172,214],{"class":141},[36,20174,58],{"class":50},[36,20176,20177],{"class":82}," color",[36,20179,86],{"class":50},[36,20181,20182],{"class":38,"line":89},[36,20183,476],{"emptyLinePlaceholder":475},[36,20185,20186,20189,20191,20193,20195,20197],{"class":38,"line":496},[36,20187,20188],{"class":82},"show_color",[36,20190,79],{"class":50},[36,20192,214],{"class":141},[36,20194,236],{"class":145},[36,20196,214],{"class":141},[36,20198,86],{"class":50},[14,20200,20201],{},"In this code:",[157,20203,20204,20208],{},[160,20205,20206,19983],{},[33,20207,20157],{},[160,20209,20210,19988],{},[33,20211,20212],{},"\"blue\"",[14,20214,107],{},[26,20216,20218],{"className":28,"code":20217,"language":30,"meta":31,"style":31},"Color: blue\n",[33,20219,20220],{"__ignoreMap":31},[36,20221,20222,20225,20227],{"class":38,"line":39},[36,20223,20224],{"class":42},"Color",[36,20226,822],{"class":50},[36,20228,20229],{"class":42}," blue\n",[1065,20231,20233],{"id":20232},"two-parameter-example","Two parameter example",[26,20235,20237],{"className":28,"code":20236,"language":30,"meta":31,"style":31},"def add(a, b):\n    print(a + b)\n\nadd(3, 5)\n",[33,20238,20239,20255,20269,20273],{"__ignoreMap":31},[36,20240,20241,20243,20245,20247,20249,20251,20253],{"class":38,"line":39},[36,20242,8785],{"class":137},[36,20244,19865],{"class":3082},[36,20246,79],{"class":50},[36,20248,347],{"class":3050},[36,20250,58],{"class":50},[36,20252,9194],{"class":3050},[36,20254,3054],{"class":50},[36,20256,20257,20259,20261,20263,20265,20267],{"class":38,"line":72},[36,20258,1735],{"class":75},[36,20260,79],{"class":50},[36,20262,3316],{"class":82},[36,20264,4763],{"class":46},[36,20266,9194],{"class":82},[36,20268,86],{"class":50},[36,20270,20271],{"class":38,"line":89},[36,20272,476],{"emptyLinePlaceholder":475},[36,20274,20275,20277,20279,20281,20283,20285],{"class":38,"line":496},[36,20276,11725],{"class":82},[36,20278,79],{"class":50},[36,20280,1247],{"class":54},[36,20282,58],{"class":50},[36,20284,5043],{"class":54},[36,20286,86],{"class":50},[14,20288,20201],{},[157,20290,20291,20297],{},[160,20292,20293,1281,20295,11756],{},[33,20294,347],{},[33,20296,721],{},[160,20298,20299,1281,20301,9134],{},[33,20300,1247],{},[33,20302,1243],{},[14,20304,107],{},[26,20306,20307],{"className":28,"code":11629,"language":30,"meta":31,"style":31},[33,20308,20309],{"__ignoreMap":31},[36,20310,20311],{"class":38,"line":39},[36,20312,11629],{"class":54},[1065,20314,20316],{"id":20315},"example-showing-parameter-names-receive-passed-values","Example showing parameter names receive passed values",[26,20318,20320],{"className":28,"code":20319,"language":30,"meta":31,"style":31},"def introduce(name, age):\n    print(\"Name:\", name)\n    print(\"Age:\", age)\n\nintroduce(\"Mia\", 12)\n",[33,20321,20322,20339,20358,20377,20381],{"__ignoreMap":31},[36,20323,20324,20326,20329,20331,20333,20335,20337],{"class":38,"line":39},[36,20325,8785],{"class":137},[36,20327,20328],{"class":3082}," introduce",[36,20330,79],{"class":50},[36,20332,817],{"class":3050},[36,20334,58],{"class":50},[36,20336,3735],{"class":3050},[36,20338,3054],{"class":50},[36,20340,20341,20343,20345,20347,20350,20352,20354,20356],{"class":38,"line":72},[36,20342,1735],{"class":75},[36,20344,79],{"class":50},[36,20346,214],{"class":141},[36,20348,20349],{"class":145},"Name:",[36,20351,214],{"class":141},[36,20353,58],{"class":50},[36,20355,3051],{"class":82},[36,20357,86],{"class":50},[36,20359,20360,20362,20364,20366,20369,20371,20373,20375],{"class":38,"line":89},[36,20361,1735],{"class":75},[36,20363,79],{"class":50},[36,20365,214],{"class":141},[36,20367,20368],{"class":145},"Age:",[36,20370,214],{"class":141},[36,20372,58],{"class":50},[36,20374,3735],{"class":82},[36,20376,86],{"class":50},[36,20378,20379],{"class":38,"line":496},[36,20380,476],{"emptyLinePlaceholder":475},[36,20382,20383,20386,20388,20390,20392,20394,20396,20398],{"class":38,"line":501},[36,20384,20385],{"class":82},"introduce",[36,20387,79],{"class":50},[36,20389,214],{"class":141},[36,20391,19767],{"class":145},[36,20393,214],{"class":141},[36,20395,58],{"class":50},[36,20397,842],{"class":54},[36,20399,86],{"class":50},[14,20401,20402],{},"When the function is called:",[157,20404,20405,20412],{},[160,20406,20407,20409,20410],{},[33,20408,817],{}," receives ",[33,20411,19780],{},[160,20413,20414,20409,20416],{},[33,20415,835],{},[33,20417,20418],{},"12",[14,20420,107],{},[26,20422,20424],{"className":28,"code":20423,"language":30,"meta":31,"style":31},"Name: Mia\nAge: 12\n",[33,20425,20426,20436],{"__ignoreMap":31},[36,20427,20428,20431,20433],{"class":38,"line":39},[36,20429,20430],{"class":42},"Name",[36,20432,822],{"class":50},[36,20434,20435],{"class":42}," Mia\n",[36,20437,20438,20441,20443],{"class":38,"line":72},[36,20439,20440],{"class":42},"Age",[36,20442,822],{"class":50},[36,20444,20445],{"class":54}," 12\n",[14,20447,20448],{},"This shows that parameter names become variables inside the function after values are passed in.",[21,20450,20452],{"id":20451},"important-beginner-notes","Important beginner notes",[14,20454,20455],{},"Keep these points in mind:",[157,20457,20458,20464,20467],{},[160,20459,20460,20461],{},"A function can have ",[331,20462,20463],{},"zero, one, or many parameters",[160,20465,20466],{},"Parameter names are variables used inside the function",[160,20468,20469],{},"A parameter only gets a value when the function is called",[14,20471,20472],{},"Example of a function with no parameters:",[26,20474,20475],{"className":28,"code":8778,"language":30,"meta":31,"style":31},[33,20476,20477,20485,20499,20503],{"__ignoreMap":31},[36,20478,20479,20481,20483],{"class":38,"line":39},[36,20480,8785],{"class":137},[36,20482,8788],{"class":3082},[36,20484,4244],{"class":50},[36,20486,20487,20489,20491,20493,20495,20497],{"class":38,"line":72},[36,20488,1735],{"class":75},[36,20490,79],{"class":50},[36,20492,214],{"class":141},[36,20494,8801],{"class":145},[36,20496,214],{"class":141},[36,20498,86],{"class":50},[36,20500,20501],{"class":38,"line":89},[36,20502,476],{"emptyLinePlaceholder":475},[36,20504,20505,20507],{"class":38,"line":496},[36,20506,8814],{"class":82},[36,20508,3170],{"class":50},[14,20510,107],{},[26,20512,20513],{"className":28,"code":8821,"language":30,"meta":31,"style":31},[33,20514,20515],{"__ignoreMap":31},[36,20516,20517],{"class":38,"line":39},[36,20518,8821],{"class":42},[14,20520,20521],{},"So parameters are common, but they are not required for every function.",[21,20523,20525],{"id":20524},"common-confusion-to-address","Common confusion to address",[14,20527,20528],{},"Beginners often run into the same misunderstandings.",[1065,20530,20532],{"id":20531},"mixing-up-parameter-and-argument","Mixing up parameter and argument",[14,20534,20535],{},"People often use these words as if they mean exactly the same thing.",[14,20537,20538],{},"They are closely related, but not identical:",[157,20540,20541,20544],{},[160,20542,20543],{},"parameter = name in the definition",[160,20545,20546],{},"argument = value in the call",[1065,20548,20550],{"id":20549},"thinking-the-parameter-already-has-a-value","Thinking the parameter already has a value",[14,20552,20553],{},"A parameter does not automatically have a value before the function is called.",[14,20555,1235],{},[26,20557,20559],{"className":28,"code":20558,"language":30,"meta":31,"style":31},"def greet(name):\n    print(\"Hello\", name)\n",[33,20560,20561,20573],{"__ignoreMap":31},[36,20562,20563,20565,20567,20569,20571],{"class":38,"line":39},[36,20564,8785],{"class":137},[36,20566,9033],{"class":3082},[36,20568,79],{"class":50},[36,20570,817],{"class":3050},[36,20572,3054],{"class":50},[36,20574,20575,20577,20579,20581,20583,20585,20587,20589],{"class":38,"line":72},[36,20576,1735],{"class":75},[36,20578,79],{"class":50},[36,20580,214],{"class":141},[36,20582,8801],{"class":145},[36,20584,214],{"class":141},[36,20586,58],{"class":50},[36,20588,3051],{"class":82},[36,20590,86],{"class":50},[14,20592,243,20593,20595],{},[33,20594,817],{}," is just a placeholder until you call the function with something like:",[26,20597,20599],{"className":28,"code":20598,"language":30,"meta":31,"style":31},"greet(\"Mia\")\n",[33,20600,20601],{"__ignoreMap":31},[36,20602,20603,20605,20607,20609,20611,20613],{"class":38,"line":39},[36,20604,9067],{"class":82},[36,20606,79],{"class":50},[36,20608,214],{"class":141},[36,20610,19767],{"class":145},[36,20612,214],{"class":141},[36,20614,86],{"class":50},[1065,20616,20618],{"id":20617},"forgetting-to-pass-required-arguments","Forgetting to pass required arguments",[14,20620,20621,20622,351],{},"If a function expects a value and you do not pass one, Python usually raises a ",[33,20623,20624],{},"TypeError",[26,20626,20628],{"className":28,"code":20627,"language":30,"meta":31,"style":31},"def greet(name):\n    print(\"Hello\", name)\n\ngreet()\n",[33,20629,20630,20642,20660,20664],{"__ignoreMap":31},[36,20631,20632,20634,20636,20638,20640],{"class":38,"line":39},[36,20633,8785],{"class":137},[36,20635,9033],{"class":3082},[36,20637,79],{"class":50},[36,20639,817],{"class":3050},[36,20641,3054],{"class":50},[36,20643,20644,20646,20648,20650,20652,20654,20656,20658],{"class":38,"line":72},[36,20645,1735],{"class":75},[36,20647,79],{"class":50},[36,20649,214],{"class":141},[36,20651,8801],{"class":145},[36,20653,214],{"class":141},[36,20655,58],{"class":50},[36,20657,3051],{"class":82},[36,20659,86],{"class":50},[36,20661,20662],{"class":38,"line":89},[36,20663,476],{"emptyLinePlaceholder":475},[36,20665,20666,20668],{"class":38,"line":496},[36,20667,9067],{"class":82},[36,20669,3170],{"class":50},[14,20671,20672,20673,20675],{},"This causes an error because ",[33,20674,817],{}," needs an argument.",[14,20677,20678,20679,351],{},"If that happens, see ",[347,20680,20682],{"href":20681},"\u002Ferrors\u002Ftypeerror-missing-required-positional-argument-fix\u002F","TypeError: missing required positional argument (Fix)",[21,20684,905],{"id":904},[14,20686,20687],{},"These are common causes of confusion around parameters:",[157,20689,20690,20698,20701,20704],{},[160,20691,20692,20693,1281,20695,20697],{},"Using the words ",[331,20694,19691],{},[331,20696,19783],{}," as if they mean exactly the same thing",[160,20699,20700],{},"Calling a function without passing all required values",[160,20702,20703],{},"Assuming parameter names outside the function are automatically connected to inside names",[160,20705,20706],{},"Confusing parameters with return values",[14,20708,20709],{},"For example, these names do not need to match:",[26,20711,20713],{"className":28,"code":20712,"language":30,"meta":31,"style":31},"def greet(name):\n    print(\"Hello\", name)\n\nperson = \"Mia\"\ngreet(person)\n",[33,20714,20715,20727,20745,20749,20761],{"__ignoreMap":31},[36,20716,20717,20719,20721,20723,20725],{"class":38,"line":39},[36,20718,8785],{"class":137},[36,20720,9033],{"class":3082},[36,20722,79],{"class":50},[36,20724,817],{"class":3050},[36,20726,3054],{"class":50},[36,20728,20729,20731,20733,20735,20737,20739,20741,20743],{"class":38,"line":72},[36,20730,1735],{"class":75},[36,20732,79],{"class":50},[36,20734,214],{"class":141},[36,20736,8801],{"class":145},[36,20738,214],{"class":141},[36,20740,58],{"class":50},[36,20742,3051],{"class":82},[36,20744,86],{"class":50},[36,20746,20747],{"class":38,"line":89},[36,20748,476],{"emptyLinePlaceholder":475},[36,20750,20751,20753,20755,20757,20759],{"class":38,"line":496},[36,20752,5972],{"class":42},[36,20754,47],{"class":46},[36,20756,224],{"class":141},[36,20758,19767],{"class":145},[36,20760,1954],{"class":141},[36,20762,20763,20765,20767,20769],{"class":38,"line":501},[36,20764,9067],{"class":82},[36,20766,79],{"class":50},[36,20768,6047],{"class":82},[36,20770,86],{"class":50},[14,20772,20773,20774,20776,20777,351],{},"This works because the value of ",[33,20775,6047],{}," is passed into the parameter ",[33,20778,817],{},[14,20780,107],{},[26,20782,20783],{"className":28,"code":19789,"language":30,"meta":31,"style":31},[33,20784,20785],{"__ignoreMap":31},[36,20786,20787],{"class":38,"line":39},[36,20788,19789],{"class":42},[14,20790,20791,20792,20794,20795,351],{},"The outside variable is ",[33,20793,6047],{},", but the parameter inside the function is still ",[33,20796,817],{},[21,20798,1063],{"id":1062},[1065,20800,20802],{"id":20801},"what-is-a-parameter-in-python-in-simple-words","What is a parameter in Python in simple words?",[14,20804,20805],{},"It is a variable in a function definition that receives a value when the function is called.",[1065,20807,20809],{"id":20808},"is-a-parameter-the-same-as-an-argument","Is a parameter the same as an argument?",[14,20811,20812],{},"Not exactly. A parameter is in the function definition. An argument is the value passed to the function call.",[1065,20814,20816],{"id":20815},"can-a-python-function-have-no-parameters","Can a Python function have no parameters?",[14,20818,20819],{},"Yes. Some functions do not need any input values.",[1065,20821,20823],{"id":20822},"can-a-function-have-more-than-one-parameter","Can a function have more than one parameter?",[14,20825,20826],{},"Yes. You can define multiple parameters separated by commas.",[1065,20828,20830],{"id":20829},"what-error-happens-if-i-forget-an-argument","What error happens if I forget an argument?",[14,20832,20833,20834,20836],{},"You often get a ",[33,20835,20624],{}," saying a required positional argument is missing.",[21,20838,1105],{"id":1104},[157,20840,20841,20845,20849,20853,20858],{},[160,20842,20843],{},[347,20844,19715],{"href":8992},[160,20846,20847],{},[347,20848,20010],{"href":20009},[160,20850,20851],{},[347,20852,20014],{"href":9015},[160,20854,20855],{},[347,20856,8742],{"href":20857},"\u002Fglossary\u002Fwhat-is-a-function-in-python\u002F",[160,20859,20860],{},[347,20861,20682],{"href":20681},[14,20863,20864],{},"Next step: learn how parameters and arguments work together in real function calls.",[1137,20866,20867],{},"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 .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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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}",{"title":31,"searchDepth":72,"depth":72,"links":20869},[20870,20871,20872,20873,20874,20875,20876,20881,20882,20887,20888,20895],{"id":23,"depth":72,"text":24},{"id":19798,"depth":72,"text":19799},{"id":6100,"depth":72,"text":6101},{"id":19836,"depth":72,"text":19837},{"id":19904,"depth":72,"text":19905},{"id":20017,"depth":72,"text":20018},{"id":20135,"depth":72,"text":20136,"children":20877},[20878,20879,20880],{"id":20139,"depth":89,"text":20140},{"id":20232,"depth":89,"text":20233},{"id":20315,"depth":89,"text":20316},{"id":20451,"depth":72,"text":20452},{"id":20524,"depth":72,"text":20525,"children":20883},[20884,20885,20886],{"id":20531,"depth":89,"text":20532},{"id":20549,"depth":89,"text":20550},{"id":20617,"depth":89,"text":20618},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":20889},[20890,20891,20892,20893,20894],{"id":20801,"depth":89,"text":20802},{"id":20808,"depth":89,"text":20809},{"id":20815,"depth":89,"text":20816},{"id":20822,"depth":89,"text":20823},{"id":20829,"depth":89,"text":20830},{"id":1104,"depth":72,"text":1105},"Master what is a parameter in python in our comprehensive Python beginner guide.",{},{"title":19681,"description":20896},"glossary\u002Fwhat-is-a-parameter-in-python","tooWMWZCA2u9sl3S0ePRT9MAApRrP0INQ1Ud9SzGyYo",{"id":20902,"title":20903,"body":20904,"description":22319,"extension":1159,"meta":22320,"navigation":475,"path":22321,"seo":22322,"stem":22323,"__hash__":22324},"content\u002Fglossary\u002Fwhat-is-a-return-value-in-python.md","What Is a Return Value in Python?",{"type":7,"value":20905,"toc":22285},[20906,20909,20915,20923,20928,20991,20993,21001,21006,21010,21026,21030,21034,21037,21048,21051,21055,21058,21120,21122,21130,21132,21157,21164,21168,21171,21189,21195,21262,21264,21276,21281,21308,21313,21375,21377,21385,21394,21398,21406,21447,21449,21462,21469,21472,21490,21494,21497,21501,21549,21551,21560,21564,21631,21633,21642,21646,21736,21738,21754,21763,21767,21770,21777,21779,21838,21840,21853,21856,21911,21915,21918,21970,21972,22005,22012,22018,22053,22058,22062,22070,22074,22095,22099,22102,22125,22157,22176,22179,22194,22209,22211,22214,22217,22221,22229,22233,22238,22242,22245,22247,22282],[10,20907,20903],{"id":20908},"what-is-a-return-value-in-python",[14,20910,3355,20911,20914],{},[331,20912,20913],{},"return value"," is the result a function sends back after it finishes running.",[14,20916,20917,20918,1281,20920,20922],{},"This is an important Python idea because functions often do work and then give the answer back to the code that called them. A beginner mistake is thinking that ",[33,20919,9167],{},[33,20921,9147],{}," do the same thing. They do not.",[14,20924,2489,20925,20927],{},[33,20926,9147],{}," when a function should give a result back to your program:",[26,20929,20931],{"className":28,"code":20930,"language":30,"meta":31,"style":31},"def add(a, b):\n    return a + b\n\nresult = add(2, 3)\nprint(result)\n",[33,20932,20933,20949,20959,20963,20981],{"__ignoreMap":31},[36,20934,20935,20937,20939,20941,20943,20945,20947],{"class":38,"line":39},[36,20936,8785],{"class":137},[36,20938,19865],{"class":3082},[36,20940,79],{"class":50},[36,20942,347],{"class":3050},[36,20944,58],{"class":50},[36,20946,9194],{"class":3050},[36,20948,3054],{"class":50},[36,20950,20951,20953,20955,20957],{"class":38,"line":72},[36,20952,9201],{"class":1718},[36,20954,9204],{"class":42},[36,20956,4763],{"class":46},[36,20958,9209],{"class":42},[36,20960,20961],{"class":38,"line":89},[36,20962,476],{"emptyLinePlaceholder":475},[36,20964,20965,20967,20969,20971,20973,20975,20977,20979],{"class":38,"line":496},[36,20966,7723],{"class":42},[36,20968,47],{"class":46},[36,20970,19865],{"class":82},[36,20972,79],{"class":50},[36,20974,1543],{"class":54},[36,20976,58],{"class":50},[36,20978,66],{"class":54},[36,20980,86],{"class":50},[36,20982,20983,20985,20987,20989],{"class":38,"line":501},[36,20984,76],{"class":75},[36,20986,79],{"class":50},[36,20988,7767],{"class":82},[36,20990,86],{"class":50},[14,20992,107],{},[26,20994,20995],{"className":28,"code":4825,"language":30,"meta":31,"style":31},[33,20996,20997],{"__ignoreMap":31},[36,20998,20999],{"class":38,"line":39},[36,21000,4825],{"class":54},[14,21002,21003,21005],{},[33,21004,9147],{}," gives the result back to the caller. Then your code can store it, print it, or use it somewhere else.",[21,21007,21009],{"id":21008},"what-this-term-means","What this term means",[157,21011,21012,21017,21023],{},[160,21013,3355,21014,21016],{},[331,21015,20913],{}," is the result a function sends back after it runs.",[160,21018,21019,21020,21022],{},"The return value is created with the ",[33,21021,9147],{}," keyword.",[160,21024,21025],{},"The calling code can save that value in a variable, print it, or use it in another expression.",[14,21027,11641,21028,351],{},[347,21029,11644],{"href":20857},[21,21031,21033],{"id":21032},"why-return-values-matter","Why return values matter",[14,21035,21036],{},"Return values matter because they make functions useful.",[157,21038,21039,21042,21045],{},[160,21040,21041],{},"They let functions produce results.",[160,21043,21044],{},"They make code reusable because the result can be used in different places.",[160,21046,21047],{},"They help separate calculation from display.",[14,21049,21050],{},"For example, a function can calculate a total and return it. Then another part of your program can decide what to do with that total.",[21,21052,21054],{"id":21053},"basic-example","Basic example",[14,21056,21057],{},"Here is a simple function that adds two numbers and returns the answer:",[26,21059,21060],{"className":28,"code":20930,"language":30,"meta":31,"style":31},[33,21061,21062,21078,21088,21092,21110],{"__ignoreMap":31},[36,21063,21064,21066,21068,21070,21072,21074,21076],{"class":38,"line":39},[36,21065,8785],{"class":137},[36,21067,19865],{"class":3082},[36,21069,79],{"class":50},[36,21071,347],{"class":3050},[36,21073,58],{"class":50},[36,21075,9194],{"class":3050},[36,21077,3054],{"class":50},[36,21079,21080,21082,21084,21086],{"class":38,"line":72},[36,21081,9201],{"class":1718},[36,21083,9204],{"class":42},[36,21085,4763],{"class":46},[36,21087,9209],{"class":42},[36,21089,21090],{"class":38,"line":89},[36,21091,476],{"emptyLinePlaceholder":475},[36,21093,21094,21096,21098,21100,21102,21104,21106,21108],{"class":38,"line":496},[36,21095,7723],{"class":42},[36,21097,47],{"class":46},[36,21099,19865],{"class":82},[36,21101,79],{"class":50},[36,21103,1543],{"class":54},[36,21105,58],{"class":50},[36,21107,66],{"class":54},[36,21109,86],{"class":50},[36,21111,21112,21114,21116,21118],{"class":38,"line":501},[36,21113,76],{"class":75},[36,21115,79],{"class":50},[36,21117,7767],{"class":82},[36,21119,86],{"class":50},[14,21121,107],{},[26,21123,21124],{"className":28,"code":4825,"language":30,"meta":31,"style":31},[33,21125,21126],{"__ignoreMap":31},[36,21127,21128],{"class":38,"line":39},[36,21129,4825],{"class":54},[14,21131,9951],{},[157,21133,21134,21140,21145,21151],{},[160,21135,21136,21139],{},[33,21137,21138],{},"def add(a, b):"," creates a function with two inputs.",[160,21141,21142,21144],{},[33,21143,9266],{}," sends the sum back.",[160,21146,21147,21150],{},[33,21148,21149],{},"result = add(2, 3)"," stores the returned value in a variable.",[160,21152,21153,21156],{},[33,21154,21155],{},"print(result)"," shows that value on the screen.",[14,21158,21159,21160,1281,21162,351],{},"If you need a refresher on function inputs, see ",[347,21161,11772],{"href":9015},[347,21163,11768],{"href":9008},[21,21165,21167],{"id":21166},"return-vs-print","Return vs print",[14,21169,21170],{},"This is one of the most common beginner confusions.",[157,21172,21173,21178,21183,21186],{},[160,21174,21175,21177],{},[33,21176,9167],{}," shows text or values on the screen.",[160,21179,21180,21182],{},[33,21181,9147],{}," sends a value back from a function.",[160,21184,21185],{},"A function can return a value without printing anything.",[160,21187,21188],{},"Visible output is not the same as a returned result.",[1065,21190,21192,21193],{"id":21191},"example-using-print","Example using ",[33,21194,9167],{},[26,21196,21198],{"className":28,"code":21197,"language":30,"meta":31,"style":31},"def add(a, b):\n    print(a + b)\n\nresult = add(2, 3)\nprint(result)\n",[33,21199,21200,21216,21230,21234,21252],{"__ignoreMap":31},[36,21201,21202,21204,21206,21208,21210,21212,21214],{"class":38,"line":39},[36,21203,8785],{"class":137},[36,21205,19865],{"class":3082},[36,21207,79],{"class":50},[36,21209,347],{"class":3050},[36,21211,58],{"class":50},[36,21213,9194],{"class":3050},[36,21215,3054],{"class":50},[36,21217,21218,21220,21222,21224,21226,21228],{"class":38,"line":72},[36,21219,1735],{"class":75},[36,21221,79],{"class":50},[36,21223,3316],{"class":82},[36,21225,4763],{"class":46},[36,21227,9194],{"class":82},[36,21229,86],{"class":50},[36,21231,21232],{"class":38,"line":89},[36,21233,476],{"emptyLinePlaceholder":475},[36,21235,21236,21238,21240,21242,21244,21246,21248,21250],{"class":38,"line":496},[36,21237,7723],{"class":42},[36,21239,47],{"class":46},[36,21241,19865],{"class":82},[36,21243,79],{"class":50},[36,21245,1543],{"class":54},[36,21247,58],{"class":50},[36,21249,66],{"class":54},[36,21251,86],{"class":50},[36,21253,21254,21256,21258,21260],{"class":38,"line":501},[36,21255,76],{"class":75},[36,21257,79],{"class":50},[36,21259,7767],{"class":82},[36,21261,86],{"class":50},[14,21263,107],{},[26,21265,21266],{"className":28,"code":9348,"language":30,"meta":31,"style":31},[33,21267,21268,21272],{"__ignoreMap":31},[36,21269,21270],{"class":38,"line":39},[36,21271,4825],{"class":54},[36,21273,21274],{"class":38,"line":72},[36,21275,7151],{"class":400},[14,21277,9361,21278,21280],{},[33,21279,9364],{}," appear?",[157,21282,21283,21288,21294,21300],{},[160,21284,21285,21286],{},"The function prints ",[33,21287,1243],{},[160,21289,21290,21291,21293],{},"But it does ",[331,21292,8193],{}," return anything",[160,21295,21296,21297,21299],{},"So Python returns ",[33,21298,9364],{}," by default",[160,21301,21302,21303,21305,21306],{},"That ",[33,21304,9364],{}," gets stored in ",[33,21307,7767],{},[1065,21309,21192,21311],{"id":21310},"example-using-return",[33,21312,9147],{},[26,21314,21315],{"className":28,"code":20930,"language":30,"meta":31,"style":31},[33,21316,21317,21333,21343,21347,21365],{"__ignoreMap":31},[36,21318,21319,21321,21323,21325,21327,21329,21331],{"class":38,"line":39},[36,21320,8785],{"class":137},[36,21322,19865],{"class":3082},[36,21324,79],{"class":50},[36,21326,347],{"class":3050},[36,21328,58],{"class":50},[36,21330,9194],{"class":3050},[36,21332,3054],{"class":50},[36,21334,21335,21337,21339,21341],{"class":38,"line":72},[36,21336,9201],{"class":1718},[36,21338,9204],{"class":42},[36,21340,4763],{"class":46},[36,21342,9209],{"class":42},[36,21344,21345],{"class":38,"line":89},[36,21346,476],{"emptyLinePlaceholder":475},[36,21348,21349,21351,21353,21355,21357,21359,21361,21363],{"class":38,"line":496},[36,21350,7723],{"class":42},[36,21352,47],{"class":46},[36,21354,19865],{"class":82},[36,21356,79],{"class":50},[36,21358,1543],{"class":54},[36,21360,58],{"class":50},[36,21362,66],{"class":54},[36,21364,86],{"class":50},[36,21366,21367,21369,21371,21373],{"class":38,"line":501},[36,21368,76],{"class":75},[36,21370,79],{"class":50},[36,21372,7767],{"class":82},[36,21374,86],{"class":50},[14,21376,107],{},[26,21378,21379],{"className":28,"code":4825,"language":30,"meta":31,"style":31},[33,21380,21381],{"__ignoreMap":31},[36,21382,21383],{"class":38,"line":39},[36,21384,4825],{"class":54},[14,21386,21387,21388,21390,21391,351],{},"If you want to understand ",[33,21389,9167],{}," better, see ",[347,21392,21393],{"href":9471},"Python print() function explained",[21,21395,21397],{"id":21396},"what-happens-if-there-is-no-return","What happens if there is no return",[14,21399,21400,21401,21403,21404,351],{},"If a function does not use ",[33,21402,9147],{},", Python returns ",[33,21405,9364],{},[26,21407,21409],{"className":28,"code":21408,"language":30,"meta":31,"style":31},"def greet():\n    print(\"Hello\")\n\nprint(greet())\n",[33,21410,21411,21419,21433,21437],{"__ignoreMap":31},[36,21412,21413,21415,21417],{"class":38,"line":39},[36,21414,8785],{"class":137},[36,21416,9033],{"class":3082},[36,21418,4244],{"class":50},[36,21420,21421,21423,21425,21427,21429,21431],{"class":38,"line":72},[36,21422,1735],{"class":75},[36,21424,79],{"class":50},[36,21426,214],{"class":141},[36,21428,8801],{"class":145},[36,21430,214],{"class":141},[36,21432,86],{"class":50},[36,21434,21435],{"class":38,"line":89},[36,21436,476],{"emptyLinePlaceholder":475},[36,21438,21439,21441,21443,21445],{"class":38,"line":496},[36,21440,76],{"class":75},[36,21442,79],{"class":50},[36,21444,9067],{"class":82},[36,21446,4387],{"class":50},[14,21448,107],{},[26,21450,21452],{"className":28,"code":21451,"language":30,"meta":31,"style":31},"Hello\nNone\n",[33,21453,21454,21458],{"__ignoreMap":31},[36,21455,21456],{"class":38,"line":39},[36,21457,8821],{"class":42},[36,21459,21460],{"class":38,"line":72},[36,21461,7151],{"class":400},[14,21463,21464,21465,21468],{},"This surprises many beginners. The function printed ",[33,21466,21467],{},"\"Hello\"",", but it did not send back a useful value.",[14,21470,21471],{},"That is why functions that only print are different from functions that return data.",[14,21473,21474,21475,21478,21479,21481,21482,1180,21486,351],{},"This also explains some ",[33,21476,21477],{},"NoneType"," errors. If your code expects a real value but gets ",[33,21480,9364],{},", you may see errors like ",[347,21483,21485],{"href":21484},"\u002Ferrors\u002Fattributeerror-nonetype-object-has-no-attribute-fix\u002F","AttributeError: 'NoneType' object has no attribute ...",[347,21487,21489],{"href":21488},"\u002Ferrors\u002Ftypeerror-nonetype-object-is-not-iterable-fix\u002F","TypeError: 'NoneType' object is not iterable",[21,21491,21493],{"id":21492},"using-a-return-value","Using a return value",[14,21495,21496],{},"Once a function returns a value, you can use it in different ways.",[1065,21498,21500],{"id":21499},"save-it-in-a-variable","Save it in a variable",[26,21502,21504],{"className":28,"code":21503,"language":30,"meta":31,"style":31},"def get_name():\n    return \"Maya\"\n\nname = get_name()\nprint(name)\n",[33,21505,21506,21515,21525,21529,21539],{"__ignoreMap":31},[36,21507,21508,21510,21513],{"class":38,"line":39},[36,21509,8785],{"class":137},[36,21511,21512],{"class":3082}," get_name",[36,21514,4244],{"class":50},[36,21516,21517,21519,21521,21523],{"class":38,"line":72},[36,21518,9201],{"class":1718},[36,21520,224],{"class":141},[36,21522,293],{"class":145},[36,21524,1954],{"class":141},[36,21526,21527],{"class":38,"line":89},[36,21528,476],{"emptyLinePlaceholder":475},[36,21530,21531,21533,21535,21537],{"class":38,"line":496},[36,21532,1945],{"class":42},[36,21534,47],{"class":46},[36,21536,21512],{"class":82},[36,21538,3170],{"class":50},[36,21540,21541,21543,21545,21547],{"class":38,"line":501},[36,21542,76],{"class":75},[36,21544,79],{"class":50},[36,21546,817],{"class":82},[36,21548,86],{"class":50},[14,21550,107],{},[26,21552,21554],{"className":28,"code":21553,"language":30,"meta":31,"style":31},"Maya\n",[33,21555,21556],{"__ignoreMap":31},[36,21557,21558],{"class":38,"line":39},[36,21559,21553],{"class":42},[1065,21561,21563],{"id":21562},"use-it-in-math-or-string-operations","Use it in math or string operations",[26,21565,21567],{"className":28,"code":21566,"language":30,"meta":31,"style":31},"def add(a, b):\n    return a + b\n\ntotal = add(4, 6)\nprint(total * 2)\n",[33,21568,21569,21585,21595,21599,21617],{"__ignoreMap":31},[36,21570,21571,21573,21575,21577,21579,21581,21583],{"class":38,"line":39},[36,21572,8785],{"class":137},[36,21574,19865],{"class":3082},[36,21576,79],{"class":50},[36,21578,347],{"class":3050},[36,21580,58],{"class":50},[36,21582,9194],{"class":3050},[36,21584,3054],{"class":50},[36,21586,21587,21589,21591,21593],{"class":38,"line":72},[36,21588,9201],{"class":1718},[36,21590,9204],{"class":42},[36,21592,4763],{"class":46},[36,21594,9209],{"class":42},[36,21596,21597],{"class":38,"line":89},[36,21598,476],{"emptyLinePlaceholder":475},[36,21600,21601,21603,21605,21607,21609,21611,21613,21615],{"class":38,"line":496},[36,21602,7983],{"class":42},[36,21604,47],{"class":46},[36,21606,19865],{"class":82},[36,21608,79],{"class":50},[36,21610,11622],{"class":54},[36,21612,58],{"class":50},[36,21614,12225],{"class":54},[36,21616,86],{"class":50},[36,21618,21619,21621,21623,21625,21627,21629],{"class":38,"line":501},[36,21620,76],{"class":75},[36,21622,79],{"class":50},[36,21624,7983],{"class":82},[36,21626,10824],{"class":46},[36,21628,61],{"class":54},[36,21630,86],{"class":50},[14,21632,107],{},[26,21634,21636],{"className":28,"code":21635,"language":30,"meta":31,"style":31},"20\n",[33,21637,21638],{"__ignoreMap":31},[36,21639,21640],{"class":38,"line":39},[36,21641,21635],{"class":54},[1065,21643,21645],{"id":21644},"pass-it-into-another-function","Pass it into another function",[26,21647,21649],{"className":28,"code":21648,"language":30,"meta":31,"style":31},"def add(a, b):\n    return a + b\n\ndef show_number(number):\n    print(\"The number is:\", number)\n\nshow_number(add(2, 3))\n",[33,21650,21651,21667,21677,21681,21694,21713,21717],{"__ignoreMap":31},[36,21652,21653,21655,21657,21659,21661,21663,21665],{"class":38,"line":39},[36,21654,8785],{"class":137},[36,21656,19865],{"class":3082},[36,21658,79],{"class":50},[36,21660,347],{"class":3050},[36,21662,58],{"class":50},[36,21664,9194],{"class":3050},[36,21666,3054],{"class":50},[36,21668,21669,21671,21673,21675],{"class":38,"line":72},[36,21670,9201],{"class":1718},[36,21672,9204],{"class":42},[36,21674,4763],{"class":46},[36,21676,9209],{"class":42},[36,21678,21679],{"class":38,"line":89},[36,21680,476],{"emptyLinePlaceholder":475},[36,21682,21683,21685,21688,21690,21692],{"class":38,"line":496},[36,21684,8785],{"class":137},[36,21686,21687],{"class":3082}," show_number",[36,21689,79],{"class":50},[36,21691,8368],{"class":3050},[36,21693,3054],{"class":50},[36,21695,21696,21698,21700,21702,21705,21707,21709,21711],{"class":38,"line":501},[36,21697,1735],{"class":75},[36,21699,79],{"class":50},[36,21701,214],{"class":141},[36,21703,21704],{"class":145},"The number is:",[36,21706,214],{"class":141},[36,21708,58],{"class":50},[36,21710,10229],{"class":82},[36,21712,86],{"class":50},[36,21714,21715],{"class":38,"line":522},[36,21716,476],{"emptyLinePlaceholder":475},[36,21718,21719,21722,21724,21726,21728,21730,21732,21734],{"class":38,"line":527},[36,21720,21721],{"class":82},"show_number",[36,21723,79],{"class":50},[36,21725,11725],{"class":82},[36,21727,79],{"class":50},[36,21729,1543],{"class":54},[36,21731,58],{"class":50},[36,21733,66],{"class":54},[36,21735,104],{"class":50},[14,21737,107],{},[26,21739,21741],{"className":28,"code":21740,"language":30,"meta":31,"style":31},"The number is: 5\n",[33,21742,21743],{"__ignoreMap":31},[36,21744,21745,21748,21750,21752],{"class":38,"line":39},[36,21746,21747],{"class":42},"The number ",[36,21749,8960],{"class":1718},[36,21751,822],{"class":50},[36,21753,4927],{"class":54},[14,21755,21756,21757,1281,21761,351],{},"If you want more practice with this idea, see ",[347,21758,21760],{"href":21759},"\u002Flearn\u002Freturn-values-in-python-functions\u002F","return values in Python functions",[347,21762,8993],{"href":8992},[21,21764,21766],{"id":21765},"common-beginner-mistakes","Common beginner mistakes",[14,21768,21769],{},"Here are some common problems with return values.",[1065,21771,2639,21773,10980,21775,10983],{"id":21772},"using-print-when-return-is-needed",[33,21774,9167],{},[33,21776,9147],{},[14,21778,2560],{},[26,21780,21782],{"className":28,"code":21781,"language":30,"meta":31,"style":31},"def square(x):\n    print(x * x)\n\nresult = square(4)\nprint(result)\n",[33,21783,21784,21796,21810,21814,21828],{"__ignoreMap":31},[36,21785,21786,21788,21790,21792,21794],{"class":38,"line":39},[36,21787,8785],{"class":137},[36,21789,11797],{"class":3082},[36,21791,79],{"class":50},[36,21793,5306],{"class":3050},[36,21795,3054],{"class":50},[36,21797,21798,21800,21802,21804,21806,21808],{"class":38,"line":72},[36,21799,1735],{"class":75},[36,21801,79],{"class":50},[36,21803,5289],{"class":82},[36,21805,10824],{"class":46},[36,21807,11523],{"class":82},[36,21809,86],{"class":50},[36,21811,21812],{"class":38,"line":89},[36,21813,476],{"emptyLinePlaceholder":475},[36,21815,21816,21818,21820,21822,21824,21826],{"class":38,"line":496},[36,21817,7723],{"class":42},[36,21819,47],{"class":46},[36,21821,11797],{"class":82},[36,21823,79],{"class":50},[36,21825,11622],{"class":54},[36,21827,86],{"class":50},[36,21829,21830,21832,21834,21836],{"class":38,"line":501},[36,21831,76],{"class":75},[36,21833,79],{"class":50},[36,21835,7767],{"class":82},[36,21837,86],{"class":50},[14,21839,107],{},[26,21841,21843],{"className":28,"code":21842,"language":30,"meta":31,"style":31},"16\nNone\n",[33,21844,21845,21849],{"__ignoreMap":31},[36,21846,21847],{"class":38,"line":39},[36,21848,10901],{"class":54},[36,21850,21851],{"class":38,"line":72},[36,21852,7151],{"class":400},[14,21854,21855],{},"Better:",[26,21857,21859],{"className":28,"code":21858,"language":30,"meta":31,"style":31},"def square(x):\n    return x * x\n\nresult = square(4)\nprint(result)\n",[33,21860,21861,21873,21883,21887,21901],{"__ignoreMap":31},[36,21862,21863,21865,21867,21869,21871],{"class":38,"line":39},[36,21864,8785],{"class":137},[36,21866,11797],{"class":3082},[36,21868,79],{"class":50},[36,21870,5306],{"class":3050},[36,21872,3054],{"class":50},[36,21874,21875,21877,21879,21881],{"class":38,"line":72},[36,21876,9201],{"class":1718},[36,21878,10827],{"class":42},[36,21880,10824],{"class":46},[36,21882,11532],{"class":42},[36,21884,21885],{"class":38,"line":89},[36,21886,476],{"emptyLinePlaceholder":475},[36,21888,21889,21891,21893,21895,21897,21899],{"class":38,"line":496},[36,21890,7723],{"class":42},[36,21892,47],{"class":46},[36,21894,11797],{"class":82},[36,21896,79],{"class":50},[36,21898,11622],{"class":54},[36,21900,86],{"class":50},[36,21902,21903,21905,21907,21909],{"class":38,"line":501},[36,21904,76],{"class":75},[36,21906,79],{"class":50},[36,21908,7767],{"class":82},[36,21910,86],{"class":50},[1065,21912,21914],{"id":21913},"forgetting-to-capture-the-returned-value","Forgetting to capture the returned value",[14,21916,21917],{},"A function can return a value, but if you do not store or use it, that result may be lost.",[26,21919,21921],{"className":28,"code":21920,"language":30,"meta":31,"style":31},"def add(a, b):\n    return a + b\n\nadd(2, 3)  # returned value is ignored\n",[33,21922,21923,21939,21949,21953],{"__ignoreMap":31},[36,21924,21925,21927,21929,21931,21933,21935,21937],{"class":38,"line":39},[36,21926,8785],{"class":137},[36,21928,19865],{"class":3082},[36,21930,79],{"class":50},[36,21932,347],{"class":3050},[36,21934,58],{"class":50},[36,21936,9194],{"class":3050},[36,21938,3054],{"class":50},[36,21940,21941,21943,21945,21947],{"class":38,"line":72},[36,21942,9201],{"class":1718},[36,21944,9204],{"class":42},[36,21946,4763],{"class":46},[36,21948,9209],{"class":42},[36,21950,21951],{"class":38,"line":89},[36,21952,476],{"emptyLinePlaceholder":475},[36,21954,21955,21957,21959,21961,21963,21965,21967],{"class":38,"line":496},[36,21956,11725],{"class":82},[36,21958,79],{"class":50},[36,21960,1543],{"class":54},[36,21962,58],{"class":50},[36,21964,66],{"class":54},[36,21966,1988],{"class":50},[36,21968,21969],{"class":2570},"  # returned value is ignored\n",[14,21971,21855],{},[26,21973,21975],{"className":28,"code":21974,"language":30,"meta":31,"style":31},"result = add(2, 3)\nprint(result)\n",[33,21976,21977,21995],{"__ignoreMap":31},[36,21978,21979,21981,21983,21985,21987,21989,21991,21993],{"class":38,"line":39},[36,21980,7723],{"class":42},[36,21982,47],{"class":46},[36,21984,19865],{"class":82},[36,21986,79],{"class":50},[36,21988,1543],{"class":54},[36,21990,58],{"class":50},[36,21992,66],{"class":54},[36,21994,86],{"class":50},[36,21996,21997,21999,22001,22003],{"class":38,"line":72},[36,21998,76],{"class":75},[36,22000,79],{"class":50},[36,22002,7767],{"class":82},[36,22004,86],{"class":50},[1065,22006,22008,22009,22011],{"id":22007},"writing-code-after-return-and-expecting-it-to-run","Writing code after ",[33,22010,9147],{}," and expecting it to run",[14,22013,22014,22015,22017],{},"When Python reaches ",[33,22016,9147],{},", the function ends immediately.",[26,22019,22021],{"className":28,"code":22020,"language":30,"meta":31,"style":31},"def example():\n    return 10\n    print(\"This will not run\")\n",[33,22022,22023,22032,22038],{"__ignoreMap":31},[36,22024,22025,22027,22030],{"class":38,"line":39},[36,22026,8785],{"class":137},[36,22028,22029],{"class":3082}," example",[36,22031,4244],{"class":50},[36,22033,22034,22036],{"class":38,"line":72},[36,22035,9201],{"class":1718},[36,22037,10662],{"class":54},[36,22039,22040,22042,22044,22046,22049,22051],{"class":38,"line":89},[36,22041,1735],{"class":75},[36,22043,79],{"class":50},[36,22045,214],{"class":141},[36,22047,22048],{"class":145},"This will not run",[36,22050,214],{"class":141},[36,22052,86],{"class":50},[14,22054,3542,22055,22057],{},[33,22056,9167],{}," line never runs.",[1065,22059,22061],{"id":22060},"expecting-every-function-to-return-a-useful-value","Expecting every function to return a useful value",[14,22063,22064,22065,22067,22068,351],{},"Some functions return useful data, but some do not. If a function has no ",[33,22066,9147],{},", it gives back ",[33,22069,9364],{},[21,22071,22073],{"id":22072},"common-causes-of-confusion","Common causes of confusion",[157,22075,22076,22079,22087,22090],{},[160,22077,22078],{},"Confusing printed output with returned output",[160,22080,22081,22082,22084,22085],{},"Forgetting that functions without ",[33,22083,9147],{}," give back ",[33,22086,9364],{},[160,22088,22089],{},"Trying to use the result of a function that only prints",[160,22091,22092,22093,2140],{},"Placing important code after a ",[33,22094,9147],{},[21,22096,22098],{"id":22097},"helpful-checks","Helpful checks",[14,22100,22101],{},"If you are not sure what a function returns, these quick checks can help:",[26,22103,22105],{"className":28,"code":22104,"language":30,"meta":31,"style":31},"print(add(2, 3))\n",[33,22106,22107],{"__ignoreMap":31},[36,22108,22109,22111,22113,22115,22117,22119,22121,22123],{"class":38,"line":39},[36,22110,76],{"class":75},[36,22112,79],{"class":50},[36,22114,11725],{"class":82},[36,22116,79],{"class":50},[36,22118,1543],{"class":54},[36,22120,58],{"class":50},[36,22122,66],{"class":54},[36,22124,104],{"class":50},[26,22126,22127],{"className":28,"code":21974,"language":30,"meta":31,"style":31},[33,22128,22129,22147],{"__ignoreMap":31},[36,22130,22131,22133,22135,22137,22139,22141,22143,22145],{"class":38,"line":39},[36,22132,7723],{"class":42},[36,22134,47],{"class":46},[36,22136,19865],{"class":82},[36,22138,79],{"class":50},[36,22140,1543],{"class":54},[36,22142,58],{"class":50},[36,22144,66],{"class":54},[36,22146,86],{"class":50},[36,22148,22149,22151,22153,22155],{"class":38,"line":72},[36,22150,76],{"class":75},[36,22152,79],{"class":50},[36,22154,7767],{"class":82},[36,22156,86],{"class":50},[26,22158,22160],{"className":28,"code":22159,"language":30,"meta":31,"style":31},"print(type(result))\n",[33,22161,22162],{"__ignoreMap":31},[36,22163,22164,22166,22168,22170,22172,22174],{"class":38,"line":39},[36,22165,76],{"class":75},[36,22167,79],{"class":50},[36,22169,97],{"class":96},[36,22171,79],{"class":50},[36,22173,7767],{"class":82},[36,22175,104],{"class":50},[14,22177,22178],{},"You can also inspect built-in functions:",[26,22180,22182],{"className":28,"code":22181,"language":30,"meta":31,"style":31},"help(print)\n",[33,22183,22184],{"__ignoreMap":31},[36,22185,22186,22188,22190,22192],{"class":38,"line":39},[36,22187,4392],{"class":75},[36,22189,79],{"class":50},[36,22191,76],{"class":75},[36,22193,86],{"class":50},[26,22195,22197],{"className":28,"code":22196,"language":30,"meta":31,"style":31},"help(len)\n",[33,22198,22199],{"__ignoreMap":31},[36,22200,22201,22203,22205,22207],{"class":38,"line":39},[36,22202,4392],{"class":75},[36,22204,79],{"class":50},[36,22206,999],{"class":75},[36,22208,86],{"class":50},[21,22210,1063],{"id":1062},[1065,22212,9768],{"id":22213},"what-is-a-return-value-in-python-1",[14,22215,22216],{},"It is the value a function sends back to the place where the function was called.",[1065,22218,22220],{"id":22219},"is-return-the-same-as-print","Is return the same as print?",[14,22222,2836,22223,22225,22226,22228],{},[33,22224,9167],{}," displays something. ",[33,22227,9147],{}," gives a result back from the function.",[1065,22230,22232],{"id":22231},"what-does-a-function-return-if-i-do-not-write-return","What does a function return if I do not write return?",[14,22234,22235,22236,351],{},"It returns ",[33,22237,9364],{},[1065,22239,22241],{"id":22240},"can-a-function-return-more-than-one-value","Can a function return more than one value?",[14,22243,22244],{},"Yes. Python can return multiple values, usually as a tuple.",[21,22246,1105],{"id":1104},[157,22248,22249,22253,22258,22262,22266,22270,22274,22278],{},[160,22250,22251],{},[347,22252,8993],{"href":8992},[160,22254,22255],{},[347,22256,22257],{"href":21759},"Return values in Python functions",[160,22259,22260],{},[347,22261,21393],{"href":9471},[160,22263,22264],{},[347,22265,12742],{"href":20857},[160,22267,22268],{},[347,22269,9763],{"href":9015},[160,22271,22272],{},[347,22273,9758],{"href":9008},[160,22275,22276],{},[347,22277,21489],{"href":21488},[160,22279,22280],{},[347,22281,21485],{"href":21484},[1137,22283,22284],{},"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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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 .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}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":31,"searchDepth":72,"depth":72,"links":22286},[22287,22288,22289,22290,22296,22297,22302,22310,22311,22312,22318],{"id":21008,"depth":72,"text":21009},{"id":21032,"depth":72,"text":21033},{"id":21053,"depth":72,"text":21054},{"id":21166,"depth":72,"text":21167,"children":22291},[22292,22294],{"id":21191,"depth":89,"text":22293},"Example using print()",{"id":21310,"depth":89,"text":22295},"Example using return",{"id":21396,"depth":72,"text":21397},{"id":21492,"depth":72,"text":21493,"children":22298},[22299,22300,22301],{"id":21499,"depth":89,"text":21500},{"id":21562,"depth":89,"text":21563},{"id":21644,"depth":89,"text":21645},{"id":21765,"depth":72,"text":21766,"children":22303},[22304,22306,22307,22309],{"id":21772,"depth":89,"text":22305},"Using print() when return is needed",{"id":21913,"depth":89,"text":21914},{"id":22007,"depth":89,"text":22308},"Writing code after return and expecting it to run",{"id":22060,"depth":89,"text":22061},{"id":22072,"depth":72,"text":22073},{"id":22097,"depth":72,"text":22098},{"id":1062,"depth":72,"text":1063,"children":22313},[22314,22315,22316,22317],{"id":22213,"depth":89,"text":9768},{"id":22219,"depth":89,"text":22220},{"id":22231,"depth":89,"text":22232},{"id":22240,"depth":89,"text":22241},{"id":1104,"depth":72,"text":1105},"Master what is a return value in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-return-value-in-python",{"title":20903,"description":22319},"glossary\u002Fwhat-is-a-return-value-in-python","XIKGvGN2yeGT7cLiOluEhe-Muz_l-Imy0yJ1Gl1Si00",{"id":22326,"title":22327,"body":22328,"description":24331,"extension":1159,"meta":24332,"navigation":475,"path":24333,"seo":24334,"stem":24335,"__hash__":24336},"content\u002Fglossary\u002Fwhat-is-a-set-in-python.md","What Is a Set in Python?",{"type":7,"value":22329,"toc":24297},[22330,22333,22336,22424,22428,22446,22450,22491,22499,22503,22506,22509,22523,22525,22574,22576,22601,22607,22611,22621,22624,22652,22654,22767,22769,22830,22835,22839,22842,22846,22881,22886,22889,22940,22942,22967,22971,22976,23007,23016,23046,23048,23083,23093,23100,23104,23107,23110,23124,23127,23194,23196,23217,23224,23227,23306,23308,23321,23325,23328,23335,23396,23405,23412,23482,23492,23598,23603,23608,23615,23622,23704,23706,23735,23742,23749,23827,23829,23846,23853,23860,23938,23940,23953,23957,23960,23974,23977,24011,24014,24017,24060,24062,24064,24081,24084,24135,24138,24141,24203,24206,24220,24222,24226,24229,24233,24236,24240,24248,24252,24255,24257,24294],[10,22331,22327],{"id":22332},"what-is-a-set-in-python",[14,22334,22335],{},"A set in Python is a collection of unique values. This page helps beginners understand what a set is, when to use it, and how it is different from other collection types.",[26,22337,22339],{"className":28,"code":22338,"language":30,"meta":31,"style":31},"numbers = {1, 2, 3, 3}\nprint(numbers)\n\ncolors = set([\"red\", \"blue\", \"red\"])\nprint(colors)\n",[33,22340,22341,22365,22375,22379,22414],{"__ignoreMap":31},[36,22342,22343,22345,22347,22349,22351,22353,22355,22357,22359,22361,22363],{"class":38,"line":39},[36,22344,43],{"class":42},[36,22346,47],{"class":46},[36,22348,778],{"class":50},[36,22350,55],{"class":54},[36,22352,58],{"class":50},[36,22354,61],{"class":54},[36,22356,58],{"class":50},[36,22358,66],{"class":54},[36,22360,58],{"class":50},[36,22362,66],{"class":54},[36,22364,803],{"class":50},[36,22366,22367,22369,22371,22373],{"class":38,"line":72},[36,22368,76],{"class":75},[36,22370,79],{"class":50},[36,22372,83],{"class":82},[36,22374,86],{"class":50},[36,22376,22377],{"class":38,"line":89},[36,22378,476],{"emptyLinePlaceholder":475},[36,22380,22381,22383,22385,22388,22390,22392,22394,22396,22398,22400,22402,22404,22406,22408,22410,22412],{"class":38,"line":496},[36,22382,207],{"class":42},[36,22384,47],{"class":46},[36,22386,22387],{"class":96}," set",[36,22389,5490],{"class":50},[36,22391,214],{"class":141},[36,22393,217],{"class":145},[36,22395,214],{"class":141},[36,22397,58],{"class":50},[36,22399,224],{"class":141},[36,22401,236],{"class":145},[36,22403,214],{"class":141},[36,22405,58],{"class":50},[36,22407,224],{"class":141},[36,22409,217],{"class":145},[36,22411,214],{"class":141},[36,22413,493],{"class":50},[36,22415,22416,22418,22420,22422],{"class":38,"line":501},[36,22417,76],{"class":75},[36,22419,79],{"class":50},[36,22421,246],{"class":82},[36,22423,86],{"class":50},[14,22425,22426],{},[331,22427,8121],{},[157,22429,22430,22436,22443],{},[160,22431,22432,22433,22435],{},"The first line creates a set with duplicate ",[33,22434,1247],{},", but the duplicate is removed.",[160,22437,22438,22439,22442],{},"The second example uses ",[33,22440,22441],{},"set()"," to convert a list into a set.",[160,22444,22445],{},"In both cases, only unique values remain.",[14,22447,22448],{},[331,22449,4527],{},[26,22451,22453],{"className":28,"code":22452,"language":30,"meta":31,"style":31},"{1, 2, 3}\n{'red', 'blue'}\n",[33,22454,22455,22471],{"__ignoreMap":31},[36,22456,22457,22459,22461,22463,22465,22467,22469],{"class":38,"line":39},[36,22458,4552],{"class":50},[36,22460,55],{"class":54},[36,22462,58],{"class":50},[36,22464,61],{"class":54},[36,22466,58],{"class":50},[36,22468,66],{"class":54},[36,22470,803],{"class":50},[36,22472,22473,22475,22477,22479,22481,22483,22485,22487,22489],{"class":38,"line":72},[36,22474,4552],{"class":50},[36,22476,149],{"class":141},[36,22478,217],{"class":145},[36,22480,149],{"class":141},[36,22482,58],{"class":50},[36,22484,142],{"class":141},[36,22486,236],{"class":145},[36,22488,149],{"class":141},[36,22490,803],{"class":50},[17321,22492,22493],{},[14,22494,22495,22496,22498],{},"Use curly braces with values or the ",[33,22497,22441],{}," function to create a set. Duplicate values are removed automatically.",[21,22500,22502],{"id":22501},"set-definition","Set definition",[14,22504,22505],{},"A set is a built-in Python collection type.",[14,22507,22508],{},"A set:",[157,22510,22511,22514,22517,22520],{},[160,22512,22513],{},"stores multiple values in one variable",[160,22515,22516],{},"keeps only unique values",[160,22518,22519],{},"is unordered, so items do not have a fixed position",[160,22521,22522],{},"is useful when you care about membership or removing duplicates",[14,22524,197],{},[26,22526,22528],{"className":28,"code":22527,"language":30,"meta":31,"style":31},"fruits = {\"apple\", \"banana\", \"apple\"}\nprint(fruits)\n",[33,22529,22530,22563],{"__ignoreMap":31},[36,22531,22532,22535,22537,22539,22541,22543,22545,22547,22549,22551,22553,22555,22557,22559,22561],{"class":38,"line":39},[36,22533,22534],{"class":42},"fruits ",[36,22536,47],{"class":46},[36,22538,778],{"class":50},[36,22540,214],{"class":141},[36,22542,388],{"class":145},[36,22544,214],{"class":141},[36,22546,58],{"class":50},[36,22548,224],{"class":141},[36,22550,17233],{"class":145},[36,22552,214],{"class":141},[36,22554,58],{"class":50},[36,22556,224],{"class":141},[36,22558,388],{"class":145},[36,22560,214],{"class":141},[36,22562,803],{"class":50},[36,22564,22565,22567,22569,22572],{"class":38,"line":72},[36,22566,76],{"class":75},[36,22568,79],{"class":50},[36,22570,22571],{"class":82},"fruits",[36,22573,86],{"class":50},[14,22575,107],{},[26,22577,22579],{"className":28,"code":22578,"language":30,"meta":31,"style":31},"{'apple', 'banana'}\n",[33,22580,22581],{"__ignoreMap":31},[36,22582,22583,22585,22587,22589,22591,22593,22595,22597,22599],{"class":38,"line":39},[36,22584,4552],{"class":50},[36,22586,149],{"class":141},[36,22588,388],{"class":145},[36,22590,149],{"class":141},[36,22592,58],{"class":50},[36,22594,142],{"class":141},[36,22596,17233],{"class":145},[36,22598,149],{"class":141},[36,22600,803],{"class":50},[14,22602,7462,22603,22606],{},[33,22604,22605],{},"\"apple\""," appears twice, the set keeps it only once.",[21,22608,22610],{"id":22609},"how-a-set-is-different-from-a-list-or-tuple","How a set is different from a list or tuple",[14,22612,22613,22614,1281,22618,351],{},"Sets are different from ",[347,22615,22617],{"href":22616},"\u002Fglossary\u002Fwhat-is-a-list-in-python\u002F","what is a list in Python",[347,22619,856],{"href":22620},"\u002Fglossary\u002Fwhat-is-a-tuple-in-python\u002F",[14,22622,22623],{},"Here are the main differences:",[157,22625,22626,22632,22638,22644],{},[160,22627,22628,22631],{},[331,22629,22630],{},"Lists"," keep order and allow duplicates",[160,22633,22634,22637],{},[331,22635,22636],{},"Tuples"," keep order and are often used for fixed data",[160,22639,22640,22643],{},[331,22641,22642],{},"Sets"," do not keep duplicates",[160,22645,22646,22648,22649],{},[331,22647,22642],{}," do not support indexing like ",[33,22650,22651],{},"my_set[0]",[14,22653,197],{},[26,22655,22657],{"className":28,"code":22656,"language":30,"meta":31,"style":31},"my_list = [1, 2, 2, 3]\nmy_tuple = (1, 2, 2, 3)\nmy_set = {1, 2, 2, 3}\n\nprint(my_list)\nprint(my_tuple)\nprint(my_set)\n",[33,22658,22659,22683,22707,22731,22735,22745,22756],{"__ignoreMap":31},[36,22660,22661,22663,22665,22667,22669,22671,22673,22675,22677,22679,22681],{"class":38,"line":39},[36,22662,704],{"class":42},[36,22664,47],{"class":46},[36,22666,51],{"class":50},[36,22668,55],{"class":54},[36,22670,58],{"class":50},[36,22672,61],{"class":54},[36,22674,58],{"class":50},[36,22676,61],{"class":54},[36,22678,58],{"class":50},[36,22680,66],{"class":54},[36,22682,69],{"class":50},[36,22684,22685,22687,22689,22691,22693,22695,22697,22699,22701,22703,22705],{"class":38,"line":72},[36,22686,739],{"class":42},[36,22688,47],{"class":46},[36,22690,744],{"class":50},[36,22692,55],{"class":54},[36,22694,58],{"class":50},[36,22696,61],{"class":54},[36,22698,58],{"class":50},[36,22700,61],{"class":54},[36,22702,58],{"class":50},[36,22704,66],{"class":54},[36,22706,86],{"class":50},[36,22708,22709,22711,22713,22715,22717,22719,22721,22723,22725,22727,22729],{"class":38,"line":89},[36,22710,773],{"class":42},[36,22712,47],{"class":46},[36,22714,778],{"class":50},[36,22716,55],{"class":54},[36,22718,58],{"class":50},[36,22720,61],{"class":54},[36,22722,58],{"class":50},[36,22724,61],{"class":54},[36,22726,58],{"class":50},[36,22728,66],{"class":54},[36,22730,803],{"class":50},[36,22732,22733],{"class":38,"line":496},[36,22734,476],{"emptyLinePlaceholder":475},[36,22736,22737,22739,22741,22743],{"class":38,"line":501},[36,22738,76],{"class":75},[36,22740,79],{"class":50},[36,22742,974],{"class":82},[36,22744,86],{"class":50},[36,22746,22747,22749,22751,22754],{"class":38,"line":522},[36,22748,76],{"class":75},[36,22750,79],{"class":50},[36,22752,22753],{"class":82},"my_tuple",[36,22755,86],{"class":50},[36,22757,22758,22760,22762,22765],{"class":38,"line":527},[36,22759,76],{"class":75},[36,22761,79],{"class":50},[36,22763,22764],{"class":82},"my_set",[36,22766,86],{"class":50},[14,22768,4527],{},[26,22770,22772],{"className":28,"code":22771,"language":30,"meta":31,"style":31},"[1, 2, 2, 3]\n(1, 2, 2, 3)\n{1, 2, 3}\n",[33,22773,22774,22794,22814],{"__ignoreMap":31},[36,22775,22776,22778,22780,22782,22784,22786,22788,22790,22792],{"class":38,"line":39},[36,22777,117],{"class":50},[36,22779,55],{"class":54},[36,22781,58],{"class":50},[36,22783,61],{"class":54},[36,22785,58],{"class":50},[36,22787,61],{"class":54},[36,22789,58],{"class":50},[36,22791,66],{"class":54},[36,22793,69],{"class":50},[36,22795,22796,22798,22800,22802,22804,22806,22808,22810,22812],{"class":38,"line":72},[36,22797,79],{"class":50},[36,22799,55],{"class":54},[36,22801,58],{"class":50},[36,22803,61],{"class":54},[36,22805,58],{"class":50},[36,22807,61],{"class":54},[36,22809,58],{"class":50},[36,22811,66],{"class":54},[36,22813,86],{"class":50},[36,22815,22816,22818,22820,22822,22824,22826,22828],{"class":38,"line":89},[36,22817,4552],{"class":50},[36,22819,55],{"class":54},[36,22821,58],{"class":50},[36,22823,61],{"class":54},[36,22825,58],{"class":50},[36,22827,66],{"class":54},[36,22829,803],{"class":50},[14,22831,22832,22833,351],{},"The set removes the duplicate ",[33,22834,1543],{},[21,22836,22838],{"id":22837},"how-to-create-a-set","How to create a set",[14,22840,22841],{},"You can create a set in two common ways.",[1065,22843,22845],{"id":22844},"use-curly-braces","Use curly braces",[26,22847,22849],{"className":28,"code":22848,"language":30,"meta":31,"style":31},"numbers = {1, 2, 3}\nprint(numbers)\n",[33,22850,22851,22871],{"__ignoreMap":31},[36,22852,22853,22855,22857,22859,22861,22863,22865,22867,22869],{"class":38,"line":39},[36,22854,43],{"class":42},[36,22856,47],{"class":46},[36,22858,778],{"class":50},[36,22860,55],{"class":54},[36,22862,58],{"class":50},[36,22864,61],{"class":54},[36,22866,58],{"class":50},[36,22868,66],{"class":54},[36,22870,803],{"class":50},[36,22872,22873,22875,22877,22879],{"class":38,"line":72},[36,22874,76],{"class":75},[36,22876,79],{"class":50},[36,22878,83],{"class":82},[36,22880,86],{"class":50},[1065,22882,2489,22884],{"id":22883},"use-set",[33,22885,22441],{},[14,22887,22888],{},"This is useful when converting another iterable, such as a list or string, into a set.",[26,22890,22892],{"className":28,"code":22891,"language":30,"meta":31,"style":31},"letters = set([\"a\", \"b\", \"a\"])\nprint(letters)\n",[33,22893,22894,22929],{"__ignoreMap":31},[36,22895,22896,22899,22901,22903,22905,22907,22909,22911,22913,22915,22917,22919,22921,22923,22925,22927],{"class":38,"line":39},[36,22897,22898],{"class":42},"letters ",[36,22900,47],{"class":46},[36,22902,22387],{"class":96},[36,22904,5490],{"class":50},[36,22906,214],{"class":141},[36,22908,347],{"class":145},[36,22910,214],{"class":141},[36,22912,58],{"class":50},[36,22914,224],{"class":141},[36,22916,721],{"class":145},[36,22918,214],{"class":141},[36,22920,58],{"class":50},[36,22922,224],{"class":141},[36,22924,347],{"class":145},[36,22926,214],{"class":141},[36,22928,493],{"class":50},[36,22930,22931,22933,22935,22938],{"class":38,"line":72},[36,22932,76],{"class":75},[36,22934,79],{"class":50},[36,22936,22937],{"class":82},"letters",[36,22939,86],{"class":50},[14,22941,107],{},[26,22943,22945],{"className":28,"code":22944,"language":30,"meta":31,"style":31},"{'a', 'b'}\n",[33,22946,22947],{"__ignoreMap":31},[36,22948,22949,22951,22953,22955,22957,22959,22961,22963,22965],{"class":38,"line":39},[36,22950,4552],{"class":50},[36,22952,149],{"class":141},[36,22954,347],{"class":145},[36,22956,149],{"class":141},[36,22958,58],{"class":50},[36,22960,142],{"class":141},[36,22962,721],{"class":145},[36,22964,149],{"class":141},[36,22966,803],{"class":50},[1065,22968,22970],{"id":22969},"create-an-empty-set","Create an empty set",[14,22972,2489,22973,22975],{},[33,22974,22441],{}," for an empty set.",[26,22977,22979],{"className":28,"code":22978,"language":30,"meta":31,"style":31},"empty_set = set()\nprint(type(empty_set))\n",[33,22980,22981,22992],{"__ignoreMap":31},[36,22982,22983,22986,22988,22990],{"class":38,"line":39},[36,22984,22985],{"class":42},"empty_set ",[36,22987,47],{"class":46},[36,22989,22387],{"class":96},[36,22991,3170],{"class":50},[36,22993,22994,22996,22998,23000,23002,23005],{"class":38,"line":72},[36,22995,76],{"class":75},[36,22997,79],{"class":50},[36,22999,97],{"class":96},[36,23001,79],{"class":50},[36,23003,23004],{"class":82},"empty_set",[36,23006,104],{"class":50},[14,23008,23009,23010,23012,23013,23015],{},"Do ",[331,23011,8193],{}," use ",[33,23014,936],{}," if you want an empty set.",[26,23017,23019],{"className":28,"code":23018,"language":30,"meta":31,"style":31},"empty_value = {}\nprint(type(empty_value))\n",[33,23020,23021,23031],{"__ignoreMap":31},[36,23022,23023,23026,23028],{"class":38,"line":39},[36,23024,23025],{"class":42},"empty_value ",[36,23027,47],{"class":46},[36,23029,23030],{"class":50}," {}\n",[36,23032,23033,23035,23037,23039,23041,23044],{"class":38,"line":72},[36,23034,76],{"class":75},[36,23036,79],{"class":50},[36,23038,97],{"class":96},[36,23040,79],{"class":50},[36,23042,23043],{"class":82},"empty_value",[36,23045,104],{"class":50},[14,23047,107],{},[26,23049,23051],{"className":28,"code":23050,"language":30,"meta":31,"style":31},"\u003Cclass 'set'>\n\u003Cclass 'dict'>\n",[33,23052,23053,23068],{"__ignoreMap":31},[36,23054,23055,23057,23059,23061,23064,23066],{"class":38,"line":39},[36,23056,134],{"class":46},[36,23058,138],{"class":137},[36,23060,142],{"class":141},[36,23062,23063],{"class":145},"set",[36,23065,149],{"class":141},[36,23067,152],{"class":46},[36,23069,23070,23072,23074,23076,23079,23081],{"class":38,"line":72},[36,23071,134],{"class":46},[36,23073,138],{"class":137},[36,23075,142],{"class":141},[36,23077,23078],{"class":145},"dict",[36,23080,149],{"class":141},[36,23082,152],{"class":46},[14,23084,23085,23087,23088,23092],{},[33,23086,936],{}," creates an empty ",[347,23089,23091],{"href":23090},"\u002Fglossary\u002Fwhat-is-a-dictionary-in-python\u002F","what is a dictionary in Python",", not an empty set.",[14,23094,23095,23096,351],{},"If you want more detail, see ",[347,23097,23099],{"href":23098},"\u002Freference\u002Fpython-set-creating-a-set\u002F","creating a set in Python",[21,23101,23103],{"id":23102},"what-sets-are-good-for","What sets are good for",[14,23105,23106],{},"Sets are useful when uniqueness matters.",[14,23108,23109],{},"Common uses:",[157,23111,23112,23115,23118,23121],{},[160,23113,23114],{},"removing duplicate values from a list",[160,23116,23117],{},"checking if a value exists quickly",[160,23119,23120],{},"comparing groups of values",[160,23122,23123],{},"finding shared or different items between collections",[14,23125,23126],{},"Example: remove duplicates from a list",[26,23128,23130],{"className":28,"code":23129,"language":30,"meta":31,"style":31},"numbers = [1, 2, 2, 3, 3, 3]\nunique_numbers = set(numbers)\n\nprint(unique_numbers)\n",[33,23131,23132,23164,23179,23183],{"__ignoreMap":31},[36,23133,23134,23136,23138,23140,23142,23144,23146,23148,23150,23152,23154,23156,23158,23160,23162],{"class":38,"line":39},[36,23135,43],{"class":42},[36,23137,47],{"class":46},[36,23139,51],{"class":50},[36,23141,55],{"class":54},[36,23143,58],{"class":50},[36,23145,61],{"class":54},[36,23147,58],{"class":50},[36,23149,61],{"class":54},[36,23151,58],{"class":50},[36,23153,66],{"class":54},[36,23155,58],{"class":50},[36,23157,66],{"class":54},[36,23159,58],{"class":50},[36,23161,66],{"class":54},[36,23163,69],{"class":50},[36,23165,23166,23169,23171,23173,23175,23177],{"class":38,"line":72},[36,23167,23168],{"class":42},"unique_numbers ",[36,23170,47],{"class":46},[36,23172,22387],{"class":96},[36,23174,79],{"class":50},[36,23176,83],{"class":82},[36,23178,86],{"class":50},[36,23180,23181],{"class":38,"line":89},[36,23182,476],{"emptyLinePlaceholder":475},[36,23184,23185,23187,23189,23192],{"class":38,"line":496},[36,23186,76],{"class":75},[36,23188,79],{"class":50},[36,23190,23191],{"class":82},"unique_numbers",[36,23193,86],{"class":50},[14,23195,4527],{},[26,23197,23199],{"className":28,"code":23198,"language":30,"meta":31,"style":31},"{1, 2, 3}\n",[33,23200,23201],{"__ignoreMap":31},[36,23202,23203,23205,23207,23209,23211,23213,23215],{"class":38,"line":39},[36,23204,4552],{"class":50},[36,23206,55],{"class":54},[36,23208,58],{"class":50},[36,23210,61],{"class":54},[36,23212,58],{"class":50},[36,23214,66],{"class":54},[36,23216,803],{"class":50},[14,23218,23219,23220,351],{},"If you want a step-by-step version of this task, see ",[347,23221,23223],{"href":23222},"\u002Fhow-to\u002Fhow-to-remove-duplicates-from-a-list-in-python\u002F","how to remove duplicates from a list in Python",[14,23225,23226],{},"Example: check membership",[26,23228,23230],{"className":28,"code":23229,"language":30,"meta":31,"style":31},"colors = {\"red\", \"blue\", \"green\"}\n\nprint(\"red\" in colors)\nprint(\"yellow\" in colors)\n",[33,23231,23232,23264,23268,23287],{"__ignoreMap":31},[36,23233,23234,23236,23238,23240,23242,23244,23246,23248,23250,23252,23254,23256,23258,23260,23262],{"class":38,"line":39},[36,23235,207],{"class":42},[36,23237,47],{"class":46},[36,23239,778],{"class":50},[36,23241,214],{"class":141},[36,23243,217],{"class":145},[36,23245,214],{"class":141},[36,23247,58],{"class":50},[36,23249,224],{"class":141},[36,23251,236],{"class":145},[36,23253,214],{"class":141},[36,23255,58],{"class":50},[36,23257,224],{"class":141},[36,23259,227],{"class":145},[36,23261,214],{"class":141},[36,23263,803],{"class":50},[36,23265,23266],{"class":38,"line":72},[36,23267,476],{"emptyLinePlaceholder":475},[36,23269,23270,23272,23274,23276,23278,23280,23282,23285],{"class":38,"line":89},[36,23271,76],{"class":75},[36,23273,79],{"class":50},[36,23275,214],{"class":141},[36,23277,217],{"class":145},[36,23279,214],{"class":141},[36,23281,7236],{"class":1718},[36,23283,23284],{"class":82}," colors",[36,23286,86],{"class":50},[36,23288,23289,23291,23293,23295,23298,23300,23302,23304],{"class":38,"line":496},[36,23290,76],{"class":75},[36,23292,79],{"class":50},[36,23294,214],{"class":141},[36,23296,23297],{"class":145},"yellow",[36,23299,214],{"class":141},[36,23301,7236],{"class":1718},[36,23303,23284],{"class":82},[36,23305,86],{"class":50},[14,23307,107],{},[26,23309,23311],{"className":28,"code":23310,"language":30,"meta":31,"style":31},"True\nFalse\n",[33,23312,23313,23317],{"__ignoreMap":31},[36,23314,23315],{"class":38,"line":39},[36,23316,1481],{"class":400},[36,23318,23319],{"class":38,"line":72},[36,23320,1486],{"class":400},[21,23322,23324],{"id":23323},"basic-things-you-can-do-with-a-set","Basic things you can do with a set",[14,23326,23327],{},"Python sets come with useful methods.",[1065,23329,23331,23332],{"id":23330},"add-an-item-with-add","Add an item with ",[33,23333,23334],{},"add()",[26,23336,23338],{"className":28,"code":23337,"language":30,"meta":31,"style":31},"colors = {\"red\", \"blue\"}\ncolors.add(\"green\")\n\nprint(colors)\n",[33,23339,23340,23364,23382,23386],{"__ignoreMap":31},[36,23341,23342,23344,23346,23348,23350,23352,23354,23356,23358,23360,23362],{"class":38,"line":39},[36,23343,207],{"class":42},[36,23345,47],{"class":46},[36,23347,778],{"class":50},[36,23349,214],{"class":141},[36,23351,217],{"class":145},[36,23353,214],{"class":141},[36,23355,58],{"class":50},[36,23357,224],{"class":141},[36,23359,236],{"class":145},[36,23361,214],{"class":141},[36,23363,803],{"class":50},[36,23365,23366,23368,23370,23372,23374,23376,23378,23380],{"class":38,"line":72},[36,23367,246],{"class":42},[36,23369,351],{"class":50},[36,23371,11725],{"class":82},[36,23373,79],{"class":50},[36,23375,214],{"class":141},[36,23377,227],{"class":145},[36,23379,214],{"class":141},[36,23381,86],{"class":50},[36,23383,23384],{"class":38,"line":89},[36,23385,476],{"emptyLinePlaceholder":475},[36,23387,23388,23390,23392,23394],{"class":38,"line":496},[36,23389,76],{"class":75},[36,23391,79],{"class":50},[36,23393,246],{"class":82},[36,23395,86],{"class":50},[14,23397,23398,23399],{},"See also: ",[347,23400,23402,23403,7353],{"href":23401},"\u002Freference\u002Fpython-set-add-method\u002F","Python set ",[33,23404,23334],{},[1065,23406,23408,23409],{"id":23407},"add-many-items-with-update","Add many items with ",[33,23410,23411],{},"update()",[26,23413,23415],{"className":28,"code":23414,"language":30,"meta":31,"style":31},"colors = {\"red\", \"blue\"}\ncolors.update([\"green\", \"yellow\"])\n\nprint(colors)\n",[33,23416,23417,23441,23468,23472],{"__ignoreMap":31},[36,23418,23419,23421,23423,23425,23427,23429,23431,23433,23435,23437,23439],{"class":38,"line":39},[36,23420,207],{"class":42},[36,23422,47],{"class":46},[36,23424,778],{"class":50},[36,23426,214],{"class":141},[36,23428,217],{"class":145},[36,23430,214],{"class":141},[36,23432,58],{"class":50},[36,23434,224],{"class":141},[36,23436,236],{"class":145},[36,23438,214],{"class":141},[36,23440,803],{"class":50},[36,23442,23443,23445,23447,23450,23452,23454,23456,23458,23460,23462,23464,23466],{"class":38,"line":72},[36,23444,246],{"class":42},[36,23446,351],{"class":50},[36,23448,23449],{"class":82},"update",[36,23451,5490],{"class":50},[36,23453,214],{"class":141},[36,23455,227],{"class":145},[36,23457,214],{"class":141},[36,23459,58],{"class":50},[36,23461,224],{"class":141},[36,23463,23297],{"class":145},[36,23465,214],{"class":141},[36,23467,493],{"class":50},[36,23469,23470],{"class":38,"line":89},[36,23471,476],{"emptyLinePlaceholder":475},[36,23473,23474,23476,23478,23480],{"class":38,"line":496},[36,23475,76],{"class":75},[36,23477,79],{"class":50},[36,23479,246],{"class":82},[36,23481,86],{"class":50},[1065,23483,23485,23486,1180,23489],{"id":23484},"remove-items-with-remove-or-discard","Remove items with ",[33,23487,23488],{},"remove()",[33,23490,23491],{},"discard()",[26,23493,23495],{"className":28,"code":23494,"language":30,"meta":31,"style":31},"colors = {\"red\", \"blue\", \"green\"}\n\ncolors.remove(\"blue\")\nprint(colors)\n\ncolors.discard(\"yellow\")  # no error if item is missing\nprint(colors)\n",[33,23496,23497,23529,23533,23552,23562,23566,23588],{"__ignoreMap":31},[36,23498,23499,23501,23503,23505,23507,23509,23511,23513,23515,23517,23519,23521,23523,23525,23527],{"class":38,"line":39},[36,23500,207],{"class":42},[36,23502,47],{"class":46},[36,23504,778],{"class":50},[36,23506,214],{"class":141},[36,23508,217],{"class":145},[36,23510,214],{"class":141},[36,23512,58],{"class":50},[36,23514,224],{"class":141},[36,23516,236],{"class":145},[36,23518,214],{"class":141},[36,23520,58],{"class":50},[36,23522,224],{"class":141},[36,23524,227],{"class":145},[36,23526,214],{"class":141},[36,23528,803],{"class":50},[36,23530,23531],{"class":38,"line":72},[36,23532,476],{"emptyLinePlaceholder":475},[36,23534,23535,23537,23539,23542,23544,23546,23548,23550],{"class":38,"line":89},[36,23536,246],{"class":42},[36,23538,351],{"class":50},[36,23540,23541],{"class":82},"remove",[36,23543,79],{"class":50},[36,23545,214],{"class":141},[36,23547,236],{"class":145},[36,23549,214],{"class":141},[36,23551,86],{"class":50},[36,23553,23554,23556,23558,23560],{"class":38,"line":496},[36,23555,76],{"class":75},[36,23557,79],{"class":50},[36,23559,246],{"class":82},[36,23561,86],{"class":50},[36,23563,23564],{"class":38,"line":501},[36,23565,476],{"emptyLinePlaceholder":475},[36,23567,23568,23570,23572,23575,23577,23579,23581,23583,23585],{"class":38,"line":522},[36,23569,246],{"class":42},[36,23571,351],{"class":50},[36,23573,23574],{"class":82},"discard",[36,23576,79],{"class":50},[36,23578,214],{"class":141},[36,23580,23297],{"class":145},[36,23582,214],{"class":141},[36,23584,1988],{"class":50},[36,23586,23587],{"class":2570},"  # no error if item is missing\n",[36,23589,23590,23592,23594,23596],{"class":38,"line":527},[36,23591,76],{"class":75},[36,23593,79],{"class":50},[36,23595,246],{"class":82},[36,23597,86],{"class":50},[14,23599,23600,23602],{},[33,23601,23488],{}," causes an error if the item is not in the set.",[14,23604,23605,23607],{},[33,23606,23491],{}," does not.",[14,23609,23398,23610],{},[347,23611,23402,23613,7353],{"href":23612},"\u002Freference\u002Fpython-set-remove-method\u002F",[33,23614,23488],{},[1065,23616,23618,23619],{"id":23617},"combine-sets-with-union","Combine sets with ",[33,23620,23621],{},"union()",[26,23623,23625],{"className":28,"code":23624,"language":30,"meta":31,"style":31},"set1 = {1, 2, 3}\nset2 = {3, 4, 5}\n\nresult = set1.union(set2)\nprint(result)\n",[33,23626,23627,23648,23669,23673,23694],{"__ignoreMap":31},[36,23628,23629,23632,23634,23636,23638,23640,23642,23644,23646],{"class":38,"line":39},[36,23630,23631],{"class":42},"set1 ",[36,23633,47],{"class":46},[36,23635,778],{"class":50},[36,23637,55],{"class":54},[36,23639,58],{"class":50},[36,23641,61],{"class":54},[36,23643,58],{"class":50},[36,23645,66],{"class":54},[36,23647,803],{"class":50},[36,23649,23650,23653,23655,23657,23659,23661,23663,23665,23667],{"class":38,"line":72},[36,23651,23652],{"class":42},"set2 ",[36,23654,47],{"class":46},[36,23656,778],{"class":50},[36,23658,1247],{"class":54},[36,23660,58],{"class":50},[36,23662,1549],{"class":54},[36,23664,58],{"class":50},[36,23666,5043],{"class":54},[36,23668,803],{"class":50},[36,23670,23671],{"class":38,"line":89},[36,23672,476],{"emptyLinePlaceholder":475},[36,23674,23675,23677,23679,23682,23684,23687,23689,23692],{"class":38,"line":496},[36,23676,7723],{"class":42},[36,23678,47],{"class":46},[36,23680,23681],{"class":42}," set1",[36,23683,351],{"class":50},[36,23685,23686],{"class":82},"union",[36,23688,79],{"class":50},[36,23690,23691],{"class":82},"set2",[36,23693,86],{"class":50},[36,23695,23696,23698,23700,23702],{"class":38,"line":501},[36,23697,76],{"class":75},[36,23699,79],{"class":50},[36,23701,7767],{"class":82},[36,23703,86],{"class":50},[14,23705,4527],{},[26,23707,23709],{"className":28,"code":23708,"language":30,"meta":31,"style":31},"{1, 2, 3, 4, 5}\n",[33,23710,23711],{"__ignoreMap":31},[36,23712,23713,23715,23717,23719,23721,23723,23725,23727,23729,23731,23733],{"class":38,"line":39},[36,23714,4552],{"class":50},[36,23716,55],{"class":54},[36,23718,58],{"class":50},[36,23720,61],{"class":54},[36,23722,58],{"class":50},[36,23724,66],{"class":54},[36,23726,58],{"class":50},[36,23728,1549],{"class":54},[36,23730,58],{"class":50},[36,23732,5043],{"class":54},[36,23734,803],{"class":50},[14,23736,23398,23737],{},[347,23738,23402,23740,7353],{"href":23739},"\u002Freference\u002Fpython-set-union-method\u002F",[33,23741,23621],{},[1065,23743,23745,23746],{"id":23744},"find-shared-values-with-intersection","Find shared values with ",[33,23747,23748],{},"intersection()",[26,23750,23752],{"className":28,"code":23751,"language":30,"meta":31,"style":31},"set1 = {1, 2, 3}\nset2 = {2, 3, 4}\n\nresult = set1.intersection(set2)\nprint(result)\n",[33,23753,23754,23774,23794,23798,23817],{"__ignoreMap":31},[36,23755,23756,23758,23760,23762,23764,23766,23768,23770,23772],{"class":38,"line":39},[36,23757,23631],{"class":42},[36,23759,47],{"class":46},[36,23761,778],{"class":50},[36,23763,55],{"class":54},[36,23765,58],{"class":50},[36,23767,61],{"class":54},[36,23769,58],{"class":50},[36,23771,66],{"class":54},[36,23773,803],{"class":50},[36,23775,23776,23778,23780,23782,23784,23786,23788,23790,23792],{"class":38,"line":72},[36,23777,23652],{"class":42},[36,23779,47],{"class":46},[36,23781,778],{"class":50},[36,23783,1543],{"class":54},[36,23785,58],{"class":50},[36,23787,66],{"class":54},[36,23789,58],{"class":50},[36,23791,1549],{"class":54},[36,23793,803],{"class":50},[36,23795,23796],{"class":38,"line":89},[36,23797,476],{"emptyLinePlaceholder":475},[36,23799,23800,23802,23804,23806,23808,23811,23813,23815],{"class":38,"line":496},[36,23801,7723],{"class":42},[36,23803,47],{"class":46},[36,23805,23681],{"class":42},[36,23807,351],{"class":50},[36,23809,23810],{"class":82},"intersection",[36,23812,79],{"class":50},[36,23814,23691],{"class":82},[36,23816,86],{"class":50},[36,23818,23819,23821,23823,23825],{"class":38,"line":501},[36,23820,76],{"class":75},[36,23822,79],{"class":50},[36,23824,7767],{"class":82},[36,23826,86],{"class":50},[14,23828,107],{},[26,23830,23832],{"className":28,"code":23831,"language":30,"meta":31,"style":31},"{2, 3}\n",[33,23833,23834],{"__ignoreMap":31},[36,23835,23836,23838,23840,23842,23844],{"class":38,"line":39},[36,23837,4552],{"class":50},[36,23839,1543],{"class":54},[36,23841,58],{"class":50},[36,23843,66],{"class":54},[36,23845,803],{"class":50},[14,23847,23398,23848],{},[347,23849,23402,23851,7353],{"href":23850},"\u002Freference\u002Fpython-set-intersection-method\u002F",[33,23852,23748],{},[1065,23854,23856,23857],{"id":23855},"find-differences-with-difference","Find differences with ",[33,23858,23859],{},"difference()",[26,23861,23863],{"className":28,"code":23862,"language":30,"meta":31,"style":31},"set1 = {1, 2, 3}\nset2 = {2, 3, 4}\n\nresult = set1.difference(set2)\nprint(result)\n",[33,23864,23865,23885,23905,23909,23928],{"__ignoreMap":31},[36,23866,23867,23869,23871,23873,23875,23877,23879,23881,23883],{"class":38,"line":39},[36,23868,23631],{"class":42},[36,23870,47],{"class":46},[36,23872,778],{"class":50},[36,23874,55],{"class":54},[36,23876,58],{"class":50},[36,23878,61],{"class":54},[36,23880,58],{"class":50},[36,23882,66],{"class":54},[36,23884,803],{"class":50},[36,23886,23887,23889,23891,23893,23895,23897,23899,23901,23903],{"class":38,"line":72},[36,23888,23652],{"class":42},[36,23890,47],{"class":46},[36,23892,778],{"class":50},[36,23894,1543],{"class":54},[36,23896,58],{"class":50},[36,23898,66],{"class":54},[36,23900,58],{"class":50},[36,23902,1549],{"class":54},[36,23904,803],{"class":50},[36,23906,23907],{"class":38,"line":89},[36,23908,476],{"emptyLinePlaceholder":475},[36,23910,23911,23913,23915,23917,23919,23922,23924,23926],{"class":38,"line":496},[36,23912,7723],{"class":42},[36,23914,47],{"class":46},[36,23916,23681],{"class":42},[36,23918,351],{"class":50},[36,23920,23921],{"class":82},"difference",[36,23923,79],{"class":50},[36,23925,23691],{"class":82},[36,23927,86],{"class":50},[36,23929,23930,23932,23934,23936],{"class":38,"line":501},[36,23931,76],{"class":75},[36,23933,79],{"class":50},[36,23935,7767],{"class":82},[36,23937,86],{"class":50},[14,23939,107],{},[26,23941,23943],{"className":28,"code":23942,"language":30,"meta":31,"style":31},"{1}\n",[33,23944,23945],{"__ignoreMap":31},[36,23946,23947,23949,23951],{"class":38,"line":39},[36,23948,4552],{"class":50},[36,23950,55],{"class":54},[36,23952,803],{"class":50},[21,23954,23956],{"id":23955},"important-beginner-rules","Important beginner rules",[14,23958,23959],{},"When working with sets, remember these rules:",[157,23961,23962,23965,23968,23971],{},[160,23963,23964],{},"Set items must be hashable, so lists cannot be set items",[160,23966,23967],{},"The printed order of a set may look random",[160,23969,23970],{},"You cannot access set items by index",[160,23972,23973],{},"If you need ordered data, use a list or tuple instead",[14,23975,23976],{},"Example of invalid code:",[26,23978,23980],{"className":28,"code":23979,"language":30,"meta":31,"style":31},"my_set = {[1, 2], [3, 4]}\n",[33,23981,23982],{"__ignoreMap":31},[36,23983,23984,23986,23988,23991,23993,23995,23997,24000,24002,24004,24006,24008],{"class":38,"line":39},[36,23985,773],{"class":42},[36,23987,47],{"class":46},[36,23989,23990],{"class":50}," {[",[36,23992,55],{"class":54},[36,23994,58],{"class":50},[36,23996,61],{"class":54},[36,23998,23999],{"class":50},"],",[36,24001,51],{"class":50},[36,24003,1247],{"class":54},[36,24005,58],{"class":50},[36,24007,1549],{"class":54},[36,24009,24010],{"class":50},"]}\n",[14,24012,24013],{},"This causes an error because lists are mutable and cannot be stored inside a set.",[14,24015,24016],{},"A tuple can be used instead:",[26,24018,24020],{"className":28,"code":24019,"language":30,"meta":31,"style":31},"my_set = {(1, 2), (3, 4)}\nprint(my_set)\n",[33,24021,24022,24050],{"__ignoreMap":31},[36,24023,24024,24026,24028,24031,24033,24035,24037,24039,24041,24043,24045,24047],{"class":38,"line":39},[36,24025,773],{"class":42},[36,24027,47],{"class":46},[36,24029,24030],{"class":50}," {(",[36,24032,55],{"class":54},[36,24034,58],{"class":50},[36,24036,61],{"class":54},[36,24038,11966],{"class":50},[36,24040,744],{"class":50},[36,24042,1247],{"class":54},[36,24044,58],{"class":50},[36,24046,1549],{"class":54},[36,24048,24049],{"class":50},")}\n",[36,24051,24052,24054,24056,24058],{"class":38,"line":72},[36,24053,76],{"class":75},[36,24055,79],{"class":50},[36,24057,22764],{"class":82},[36,24059,86],{"class":50},[21,24061,905],{"id":904},[14,24063,8484],{},[157,24065,24066,24072,24075,24078],{},[160,24067,24068,24069,24071],{},"using ",[33,24070,936],{}," and expecting an empty set",[160,24073,24074],{},"trying to access a set item by index",[160,24076,24077],{},"expecting duplicates to stay in the set",[160,24079,24080],{},"trying to put a list inside a set",[14,24082,24083],{},"Example of indexing error:",[26,24085,24087],{"className":28,"code":24086,"language":30,"meta":31,"style":31},"colors = {\"red\", \"blue\", \"green\"}\nprint(colors[0])\n",[33,24088,24089,24121],{"__ignoreMap":31},[36,24090,24091,24093,24095,24097,24099,24101,24103,24105,24107,24109,24111,24113,24115,24117,24119],{"class":38,"line":39},[36,24092,207],{"class":42},[36,24094,47],{"class":46},[36,24096,778],{"class":50},[36,24098,214],{"class":141},[36,24100,217],{"class":145},[36,24102,214],{"class":141},[36,24104,58],{"class":50},[36,24106,224],{"class":141},[36,24108,236],{"class":145},[36,24110,214],{"class":141},[36,24112,58],{"class":50},[36,24114,224],{"class":141},[36,24116,227],{"class":145},[36,24118,214],{"class":141},[36,24120,803],{"class":50},[36,24122,24123,24125,24127,24129,24131,24133],{"class":38,"line":72},[36,24124,76],{"class":75},[36,24126,79],{"class":50},[36,24128,246],{"class":82},[36,24130,117],{"class":50},[36,24132,490],{"class":54},[36,24134,493],{"class":50},[14,24136,24137],{},"This fails because sets do not have positions like lists.",[14,24139,24140],{},"If you are not sure what a variable contains, these checks can help:",[26,24142,24144],{"className":28,"code":24143,"language":30,"meta":31,"style":31},"print(my_set)\nprint(type(my_set))\nprint(len(my_set))\nprint('red' in my_set)\n",[33,24145,24146,24156,24170,24184],{"__ignoreMap":31},[36,24147,24148,24150,24152,24154],{"class":38,"line":39},[36,24149,76],{"class":75},[36,24151,79],{"class":50},[36,24153,22764],{"class":82},[36,24155,86],{"class":50},[36,24157,24158,24160,24162,24164,24166,24168],{"class":38,"line":72},[36,24159,76],{"class":75},[36,24161,79],{"class":50},[36,24163,97],{"class":96},[36,24165,79],{"class":50},[36,24167,22764],{"class":82},[36,24169,104],{"class":50},[36,24171,24172,24174,24176,24178,24180,24182],{"class":38,"line":89},[36,24173,76],{"class":75},[36,24175,79],{"class":50},[36,24177,999],{"class":75},[36,24179,79],{"class":50},[36,24181,22764],{"class":82},[36,24183,104],{"class":50},[36,24185,24186,24188,24190,24192,24194,24196,24198,24201],{"class":38,"line":496},[36,24187,76],{"class":75},[36,24189,79],{"class":50},[36,24191,149],{"class":141},[36,24193,217],{"class":145},[36,24195,149],{"class":141},[36,24197,7236],{"class":1718},[36,24199,24200],{"class":82}," my_set",[36,24202,86],{"class":50},[14,24204,24205],{},"These are useful for debugging because they show:",[157,24207,24208,24211,24214,24217],{},[160,24209,24210],{},"the actual values in the set",[160,24212,24213],{},"whether the variable is really a set",[160,24215,24216],{},"how many unique items it contains",[160,24218,24219],{},"whether a specific value exists in it",[21,24221,1063],{"id":1062},[1065,24223,24225],{"id":24224},"does-a-set-keep-items-in-order","Does a set keep items in order?",[14,24227,24228],{},"No. A set is unordered, so you should not rely on item position.",[1065,24230,24232],{"id":24231},"can-a-set-contain-duplicate-values","Can a set contain duplicate values?",[14,24234,24235],{},"No. Duplicate values are removed automatically.",[1065,24237,24239],{"id":24238},"how-do-i-create-an-empty-set-in-python","How do I create an empty set in Python?",[14,24241,2489,24242,24244,24245,24247],{},[33,24243,22441],{},". If you use ",[33,24246,936],{},", Python creates an empty dictionary.",[1065,24249,24251],{"id":24250},"when-should-i-use-a-set-instead-of-a-list","When should I use a set instead of a list?",[14,24253,24254],{},"Use a set when you need unique values or fast membership checks.",[21,24256,1105],{"id":1104},[157,24258,24259,24265,24270,24276,24282,24288],{},[160,24260,24261],{},[347,24262,24264],{"href":24263},"\u002Flearn\u002Fpython-sets-explained\u002F","Python sets explained",[160,24266,24267],{},[347,24268,24269],{"href":23098},"Creating a set in Python",[160,24271,24272],{},[347,24273,23402,24274,7353],{"href":23401},[33,24275,23334],{},[160,24277,24278],{},[347,24279,23402,24280,7353],{"href":23612},[33,24281,23488],{},[160,24283,24284],{},[347,24285,23402,24286,7353],{"href":23739},[33,24287,23621],{},[160,24289,24290],{},[347,24291,23402,24292,7353],{"href":23850},[33,24293,23748],{},[1137,24295,24296],{},"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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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}",{"title":31,"searchDepth":72,"depth":72,"links":24298},[24299,24300,24301,24307,24308,24322,24323,24324,24330],{"id":22501,"depth":72,"text":22502},{"id":22609,"depth":72,"text":22610},{"id":22837,"depth":72,"text":22838,"children":24302},[24303,24304,24306],{"id":22844,"depth":89,"text":22845},{"id":22883,"depth":89,"text":24305},"Use set()",{"id":22969,"depth":89,"text":22970},{"id":23102,"depth":72,"text":23103},{"id":23323,"depth":72,"text":23324,"children":24309},[24310,24312,24314,24316,24318,24320],{"id":23330,"depth":89,"text":24311},"Add an item with add()",{"id":23407,"depth":89,"text":24313},"Add many items with update()",{"id":23484,"depth":89,"text":24315},"Remove items with remove() or discard()",{"id":23617,"depth":89,"text":24317},"Combine sets with union()",{"id":23744,"depth":89,"text":24319},"Find shared values with intersection()",{"id":23855,"depth":89,"text":24321},"Find differences with difference()",{"id":23955,"depth":72,"text":23956},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":24325},[24326,24327,24328,24329],{"id":24224,"depth":89,"text":24225},{"id":24231,"depth":89,"text":24232},{"id":24238,"depth":89,"text":24239},{"id":24250,"depth":89,"text":24251},{"id":1104,"depth":72,"text":1105},"Master what is a set in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-set-in-python",{"title":22327,"description":24331},"glossary\u002Fwhat-is-a-set-in-python","nB67-EcTPd_r6o9qujWcW_aERjxQUzT_e3DBtLMCuIA",{"id":24338,"title":24339,"body":24340,"description":25695,"extension":1159,"meta":25696,"navigation":475,"path":25697,"seo":25698,"stem":25699,"__hash__":25700},"content\u002Fglossary\u002Fwhat-is-a-string-in-python.md","What Is a String in Python?",{"type":7,"value":24341,"toc":25668},[24342,24345,24348,24351,24368,24371,24425,24427,24440,24443,24447,24450,24453,24477,24480,24493,24496,24554,24558,24561,24563,24577,24614,24616,24649,24657,24661,24664,24685,24687,24761,24764,24768,24771,24777,24833,24835,24844,24850,24875,24877,24886,24890,24893,24946,24948,24963,24967,25017,25019,25034,25038,25117,25119,25165,25185,25187,25190,25193,25195,25236,25238,25252,25255,25272,25275,25333,25335,25343,25356,25360,25366,25369,25380,25383,25385,25388,25407,25410,25449,25454,25457,25499,25502,25545,25548,25559,25561,25565,25570,25578,25586,25590,25598,25602,25605,25611,25614,25616,25665],[10,24343,24339],{"id":24344},"what-is-a-string-in-python",[14,24346,24347],{},"A string in Python is a value used to store text.",[14,24349,24350],{},"Beginners use strings all the time. A string can hold:",[157,24352,24353,24356,24359,24362],{},[160,24354,24355],{},"A word",[160,24357,24358],{},"A sentence",[160,24360,24361],{},"A number written as text",[160,24363,24364,24365],{},"Even empty text like ",[33,24366,24367],{},"\"\"",[14,24369,24370],{},"Python knows something is a string when it is written inside quotes. This page defines the term clearly and shows where strings are commonly used.",[26,24372,24374],{"className":28,"code":24373,"language":30,"meta":31,"style":31},"name = \"Alice\"\nmessage = 'Hello'\n\nprint(name)\nprint(message)\n",[33,24375,24376,24388,24400,24404,24414],{"__ignoreMap":31},[36,24377,24378,24380,24382,24384,24386],{"class":38,"line":39},[36,24379,1945],{"class":42},[36,24381,47],{"class":46},[36,24383,224],{"class":141},[36,24385,5993],{"class":145},[36,24387,1954],{"class":141},[36,24389,24390,24392,24394,24396,24398],{"class":38,"line":72},[36,24391,4909],{"class":42},[36,24393,47],{"class":46},[36,24395,142],{"class":141},[36,24397,8801],{"class":145},[36,24399,8468],{"class":141},[36,24401,24402],{"class":38,"line":89},[36,24403,476],{"emptyLinePlaceholder":475},[36,24405,24406,24408,24410,24412],{"class":38,"line":496},[36,24407,76],{"class":75},[36,24409,79],{"class":50},[36,24411,817],{"class":82},[36,24413,86],{"class":50},[36,24415,24416,24418,24420,24423],{"class":38,"line":501},[36,24417,76],{"class":75},[36,24419,79],{"class":50},[36,24421,24422],{"class":82},"message",[36,24424,86],{"class":50},[14,24426,107],{},[26,24428,24430],{"className":28,"code":24429,"language":30,"meta":31,"style":31},"Alice\nHello\n",[33,24431,24432,24436],{"__ignoreMap":31},[36,24433,24434],{"class":38,"line":39},[36,24435,6087],{"class":42},[36,24437,24438],{"class":38,"line":72},[36,24439,8821],{"class":42},[14,24441,24442],{},"A string is text inside quotes. Python treats quoted text as a string value.",[21,24444,24446],{"id":24445},"what-a-string-is","What a string is",[14,24448,24449],{},"A string is a value used to store text.",[14,24451,24452],{},"Strings can contain:",[157,24454,24455,24460,24466,24472],{},[160,24456,24457,24458],{},"Words like ",[33,24459,4854],{},[160,24461,24462,24463],{},"Names like ",[33,24464,24465],{},"'Python'",[160,24467,24468,24469],{},"Digits written as text like ",[33,24470,24471],{},"\"123\"",[160,24473,24474,24475],{},"Empty text like ",[33,24476,24367],{},[14,24478,24479],{},"In Python, strings are written inside:",[157,24481,24482,24488],{},[160,24483,24484,24485],{},"Single quotes: ",[33,24486,24487],{},"'hello'",[160,24489,24490,24491],{},"Double quotes: ",[33,24492,4854],{},[14,24494,24495],{},"These are all strings:",[26,24497,24499],{"className":28,"code":24498,"language":30,"meta":31,"style":31},"print(\"hello\")\nprint('Python')\nprint(\"123\")\nprint(\"\")\n",[33,24500,24501,24515,24529,24544],{"__ignoreMap":31},[36,24502,24503,24505,24507,24509,24511,24513],{"class":38,"line":39},[36,24504,76],{"class":75},[36,24506,79],{"class":50},[36,24508,214],{"class":141},[36,24510,2671],{"class":145},[36,24512,214],{"class":141},[36,24514,86],{"class":50},[36,24516,24517,24519,24521,24523,24525,24527],{"class":38,"line":72},[36,24518,76],{"class":75},[36,24520,79],{"class":50},[36,24522,149],{"class":141},[36,24524,9414],{"class":145},[36,24526,149],{"class":141},[36,24528,86],{"class":50},[36,24530,24531,24533,24535,24537,24540,24542],{"class":38,"line":89},[36,24532,76],{"class":75},[36,24534,79],{"class":50},[36,24536,214],{"class":141},[36,24538,24539],{"class":145},"123",[36,24541,214],{"class":141},[36,24543,86],{"class":50},[36,24545,24546,24548,24550,24552],{"class":38,"line":496},[36,24547,76],{"class":75},[36,24549,79],{"class":50},[36,24551,24367],{"class":141},[36,24553,86],{"class":50},[21,24555,24557],{"id":24556},"how-python-knows-something-is-a-string","How Python knows something is a string",[14,24559,24560],{},"Python checks for quotes around the value.",[14,24562,1235],{},[157,24564,24565,24571],{},[160,24566,24567,24570],{},[33,24568,24569],{},"\"42\""," is a string because it has quotes",[160,24572,24573,24576],{},[33,24574,24575],{},"42"," is an integer because it has no quotes",[26,24578,24580],{"className":28,"code":24579,"language":30,"meta":31,"style":31},"print(type(\"42\"))\nprint(type(42))\n",[33,24581,24582,24600],{"__ignoreMap":31},[36,24583,24584,24586,24588,24590,24592,24594,24596,24598],{"class":38,"line":39},[36,24585,76],{"class":75},[36,24587,79],{"class":50},[36,24589,97],{"class":96},[36,24591,79],{"class":50},[36,24593,214],{"class":141},[36,24595,24575],{"class":145},[36,24597,214],{"class":141},[36,24599,104],{"class":50},[36,24601,24602,24604,24606,24608,24610,24612],{"class":38,"line":72},[36,24603,76],{"class":75},[36,24605,79],{"class":50},[36,24607,97],{"class":96},[36,24609,79],{"class":50},[36,24611,24575],{"class":54},[36,24613,104],{"class":50},[14,24615,107],{},[26,24617,24619],{"className":28,"code":24618,"language":30,"meta":31,"style":31},"\u003Cclass 'str'>\n\u003Cclass 'int'>\n",[33,24620,24621,24635],{"__ignoreMap":31},[36,24622,24623,24625,24627,24629,24631,24633],{"class":38,"line":39},[36,24624,134],{"class":46},[36,24626,138],{"class":137},[36,24628,142],{"class":141},[36,24630,2360],{"class":145},[36,24632,149],{"class":141},[36,24634,152],{"class":46},[36,24636,24637,24639,24641,24643,24645,24647],{"class":38,"line":72},[36,24638,134],{"class":46},[36,24640,138],{"class":137},[36,24642,142],{"class":141},[36,24644,2466],{"class":145},[36,24646,149],{"class":141},[36,24648,152],{"class":46},[14,24650,24651,24652,24656],{},"Even when they look similar, strings and numbers are different types of values. If you want to inspect a value, the ",[347,24653,24654,8246],{"href":5265},[33,24655,1327],{}," is very useful.",[21,24658,24660],{"id":24659},"common-places-beginners-use-strings","Common places beginners use strings",[14,24662,24663],{},"Beginners often use strings for:",[157,24665,24666,24669,24674,24679,24682],{},[160,24667,24668],{},"Names",[160,24670,24671,24672],{},"Messages shown with ",[33,24673,9167],{},[160,24675,24676,24677],{},"User input from ",[33,24678,9395],{},[160,24680,24681],{},"File names and file paths",[160,24683,24684],{},"Data read from files or APIs",[14,24686,197],{},[26,24688,24690],{"className":28,"code":24689,"language":30,"meta":31,"style":31},"name = \"Maya\"\nfilename = \"notes.txt\"\n\nprint(\"Hello, \" + name)\nprint(\"Opening file:\", filename)\n",[33,24691,24692,24704,24718,24722,24741],{"__ignoreMap":31},[36,24693,24694,24696,24698,24700,24702],{"class":38,"line":39},[36,24695,1945],{"class":42},[36,24697,47],{"class":46},[36,24699,224],{"class":141},[36,24701,293],{"class":145},[36,24703,1954],{"class":141},[36,24705,24706,24709,24711,24713,24716],{"class":38,"line":72},[36,24707,24708],{"class":42},"filename ",[36,24710,47],{"class":46},[36,24712,224],{"class":141},[36,24714,24715],{"class":145},"notes.txt",[36,24717,1954],{"class":141},[36,24719,24720],{"class":38,"line":89},[36,24721,476],{"emptyLinePlaceholder":475},[36,24723,24724,24726,24728,24730,24733,24735,24737,24739],{"class":38,"line":496},[36,24725,76],{"class":75},[36,24727,79],{"class":50},[36,24729,214],{"class":141},[36,24731,24732],{"class":145},"Hello, ",[36,24734,214],{"class":141},[36,24736,3106],{"class":46},[36,24738,3051],{"class":82},[36,24740,86],{"class":50},[36,24742,24743,24745,24747,24749,24752,24754,24756,24759],{"class":38,"line":501},[36,24744,76],{"class":75},[36,24746,79],{"class":50},[36,24748,214],{"class":141},[36,24750,24751],{"class":145},"Opening file:",[36,24753,214],{"class":141},[36,24755,58],{"class":50},[36,24757,24758],{"class":82}," filename",[36,24760,86],{"class":50},[14,24762,24763],{},"In many beginner programs, most visible text is stored as strings.",[21,24765,24767],{"id":24766},"what-you-can-do-with-strings","What you can do with strings",[14,24769,24770],{},"Strings are not just for storing text. You can also work with them in useful ways.",[1065,24772,24774,24775],{"id":24773},"join-text-together-with","Join text together with ",[33,24776,4763],{},[26,24778,24780],{"className":28,"code":24779,"language":30,"meta":31,"style":31},"first = \"Hello\"\nsecond = \"World\"\n\nprint(first + \" \" + second)\n",[33,24781,24782,24795,24808,24812],{"__ignoreMap":31},[36,24783,24784,24787,24789,24791,24793],{"class":38,"line":39},[36,24785,24786],{"class":42},"first ",[36,24788,47],{"class":46},[36,24790,224],{"class":141},[36,24792,8801],{"class":145},[36,24794,1954],{"class":141},[36,24796,24797,24800,24802,24804,24806],{"class":38,"line":72},[36,24798,24799],{"class":42},"second ",[36,24801,47],{"class":46},[36,24803,224],{"class":141},[36,24805,10040],{"class":145},[36,24807,1954],{"class":141},[36,24809,24810],{"class":38,"line":89},[36,24811,476],{"emptyLinePlaceholder":475},[36,24813,24814,24816,24818,24820,24822,24824,24826,24828,24831],{"class":38,"line":496},[36,24815,76],{"class":75},[36,24817,79],{"class":50},[36,24819,24786],{"class":82},[36,24821,4763],{"class":46},[36,24823,224],{"class":141},[36,24825,224],{"class":141},[36,24827,3106],{"class":46},[36,24829,24830],{"class":82}," second",[36,24832,86],{"class":50},[14,24834,107],{},[26,24836,24838],{"className":28,"code":24837,"language":30,"meta":31,"style":31},"Hello World\n",[33,24839,24840],{"__ignoreMap":31},[36,24841,24842],{"class":38,"line":39},[36,24843,24837],{"class":42},[1065,24845,24847,24848],{"id":24846},"repeat-text-with","Repeat text with ",[33,24849,10824],{},[26,24851,24853],{"className":28,"code":24852,"language":30,"meta":31,"style":31},"print(\"ha\" * 3)\n",[33,24854,24855],{"__ignoreMap":31},[36,24856,24857,24859,24861,24863,24866,24868,24871,24873],{"class":38,"line":39},[36,24858,76],{"class":75},[36,24860,79],{"class":50},[36,24862,214],{"class":141},[36,24864,24865],{"class":145},"ha",[36,24867,214],{"class":141},[36,24869,24870],{"class":46}," *",[36,24872,66],{"class":54},[36,24874,86],{"class":50},[14,24876,107],{},[26,24878,24880],{"className":28,"code":24879,"language":30,"meta":31,"style":31},"hahaha\n",[33,24881,24882],{"__ignoreMap":31},[36,24883,24884],{"class":38,"line":39},[36,24885,24879],{"class":42},[1065,24887,24889],{"id":24888},"get-part-of-a-string","Get part of a string",[14,24891,24892],{},"You can access characters by position or take a slice of the string.",[26,24894,24896],{"className":28,"code":24895,"language":30,"meta":31,"style":31},"word = \"Python\"\n\nprint(word[0])\nprint(word[0:3])\n",[33,24897,24898,24910,24914,24928],{"__ignoreMap":31},[36,24899,24900,24902,24904,24906,24908],{"class":38,"line":39},[36,24901,16709],{"class":42},[36,24903,47],{"class":46},[36,24905,224],{"class":141},[36,24907,9414],{"class":145},[36,24909,1954],{"class":141},[36,24911,24912],{"class":38,"line":72},[36,24913,476],{"emptyLinePlaceholder":475},[36,24915,24916,24918,24920,24922,24924,24926],{"class":38,"line":89},[36,24917,76],{"class":75},[36,24919,79],{"class":50},[36,24921,10068],{"class":82},[36,24923,117],{"class":50},[36,24925,490],{"class":54},[36,24927,493],{"class":50},[36,24929,24930,24932,24934,24936,24938,24940,24942,24944],{"class":38,"line":496},[36,24931,76],{"class":75},[36,24933,79],{"class":50},[36,24935,10068],{"class":82},[36,24937,117],{"class":50},[36,24939,490],{"class":54},[36,24941,822],{"class":50},[36,24943,1247],{"class":54},[36,24945,493],{"class":50},[14,24947,107],{},[26,24949,24951],{"className":28,"code":24950,"language":30,"meta":31,"style":31},"P\nPyt\n",[33,24952,24953,24958],{"__ignoreMap":31},[36,24954,24955],{"class":38,"line":39},[36,24956,24957],{"class":42},"P\n",[36,24959,24960],{"class":38,"line":72},[36,24961,24962],{"class":42},"Pyt\n",[1065,24964,24966],{"id":24965},"change-letter-case","Change letter case",[26,24968,24970],{"className":28,"code":24969,"language":30,"meta":31,"style":31},"text = \"PyThOn\"\n\nprint(text.lower())\nprint(text.upper())\n",[33,24971,24972,24985,24989,25003],{"__ignoreMap":31},[36,24973,24974,24976,24978,24980,24983],{"class":38,"line":39},[36,24975,9407],{"class":42},[36,24977,47],{"class":46},[36,24979,224],{"class":141},[36,24981,24982],{"class":145},"PyThOn",[36,24984,1954],{"class":141},[36,24986,24987],{"class":38,"line":72},[36,24988,476],{"emptyLinePlaceholder":475},[36,24990,24991,24993,24995,24997,24999,25001],{"class":38,"line":89},[36,24992,76],{"class":75},[36,24994,79],{"class":50},[36,24996,4695],{"class":82},[36,24998,351],{"class":50},[36,25000,16735],{"class":82},[36,25002,4387],{"class":50},[36,25004,25005,25007,25009,25011,25013,25015],{"class":38,"line":496},[36,25006,76],{"class":75},[36,25008,79],{"class":50},[36,25010,4695],{"class":82},[36,25012,351],{"class":50},[36,25014,13483],{"class":82},[36,25016,4387],{"class":50},[14,25018,107],{},[26,25020,25022],{"className":28,"code":25021,"language":30,"meta":31,"style":31},"python\nPYTHON\n",[33,25023,25024,25029],{"__ignoreMap":31},[36,25025,25026],{"class":38,"line":39},[36,25027,25028],{"class":42},"python\n",[36,25030,25031],{"class":38,"line":72},[36,25032,25033],{"class":3059},"PYTHON\n",[1065,25035,25037],{"id":25036},"split-text-or-join-parts-together","Split text or join parts together",[26,25039,25041],{"className":28,"code":25040,"language":30,"meta":31,"style":31},"text = \"red,green,blue\"\ncolors = text.split(\",\")\n\nprint(colors)\nprint(\"-\".join(colors))\n",[33,25042,25043,25056,25079,25083,25093],{"__ignoreMap":31},[36,25044,25045,25047,25049,25051,25054],{"class":38,"line":39},[36,25046,9407],{"class":42},[36,25048,47],{"class":46},[36,25050,224],{"class":141},[36,25052,25053],{"class":145},"red,green,blue",[36,25055,1954],{"class":141},[36,25057,25058,25060,25062,25064,25066,25069,25071,25073,25075,25077],{"class":38,"line":72},[36,25059,207],{"class":42},[36,25061,47],{"class":46},[36,25063,16440],{"class":42},[36,25065,351],{"class":50},[36,25067,25068],{"class":82},"split",[36,25070,79],{"class":50},[36,25072,214],{"class":141},[36,25074,58],{"class":145},[36,25076,214],{"class":141},[36,25078,86],{"class":50},[36,25080,25081],{"class":38,"line":89},[36,25082,476],{"emptyLinePlaceholder":475},[36,25084,25085,25087,25089,25091],{"class":38,"line":496},[36,25086,76],{"class":75},[36,25088,79],{"class":50},[36,25090,246],{"class":82},[36,25092,86],{"class":50},[36,25094,25095,25097,25099,25101,25104,25106,25108,25111,25113,25115],{"class":38,"line":501},[36,25096,76],{"class":75},[36,25098,79],{"class":50},[36,25100,214],{"class":141},[36,25102,25103],{"class":145},"-",[36,25105,214],{"class":141},[36,25107,351],{"class":50},[36,25109,25110],{"class":82},"join",[36,25112,79],{"class":50},[36,25114,246],{"class":82},[36,25116,104],{"class":50},[14,25118,107],{},[26,25120,25122],{"className":28,"code":25121,"language":30,"meta":31,"style":31},"['red', 'green', 'blue']\nred-green-blue\n",[33,25123,25124,25152],{"__ignoreMap":31},[36,25125,25126,25128,25130,25132,25134,25136,25138,25140,25142,25144,25146,25148,25150],{"class":38,"line":39},[36,25127,117],{"class":50},[36,25129,149],{"class":141},[36,25131,217],{"class":145},[36,25133,149],{"class":141},[36,25135,58],{"class":50},[36,25137,142],{"class":141},[36,25139,227],{"class":145},[36,25141,149],{"class":141},[36,25143,58],{"class":50},[36,25145,142],{"class":141},[36,25147,236],{"class":145},[36,25149,149],{"class":141},[36,25151,69],{"class":50},[36,25153,25154,25156,25158,25160,25162],{"class":38,"line":72},[36,25155,217],{"class":42},[36,25157,25103],{"class":46},[36,25159,227],{"class":42},[36,25161,25103],{"class":46},[36,25163,25164],{"class":42},"blue\n",[14,25166,25167,25168,25172,25173,1281,25179,351],{},"If you want to go deeper, see the beginner lesson on ",[347,25169,25171],{"href":25170},"\u002Flearn\u002Fpython-strings-explained-basics-and-examples\u002F","Python strings explained with basics and examples",". You can also learn specific methods like ",[347,25174,25176],{"href":25175},"\u002Freference\u002Fpython-string-split-method\u002F",[33,25177,25178],{},"split()",[347,25180,25182],{"href":25181},"\u002Freference\u002Fpython-string-join-method\u002F",[33,25183,25184],{},"join()",[21,25186,15985],{"id":15984},[14,25188,25189],{},"Strings are text, not numbers.",[14,25191,25192],{},"This is one of the most important beginner ideas to understand.",[14,25194,1235],{},[26,25196,25198],{"className":28,"code":25197,"language":30,"meta":31,"style":31},"print(\"5\" + \"3\")\nprint(5 + 3)\n",[33,25199,25200,25222],{"__ignoreMap":31},[36,25201,25202,25204,25206,25208,25210,25212,25214,25216,25218,25220],{"class":38,"line":39},[36,25203,76],{"class":75},[36,25205,79],{"class":50},[36,25207,214],{"class":141},[36,25209,1243],{"class":145},[36,25211,214],{"class":141},[36,25213,3106],{"class":46},[36,25215,224],{"class":141},[36,25217,1247],{"class":145},[36,25219,214],{"class":141},[36,25221,86],{"class":50},[36,25223,25224,25226,25228,25230,25232,25234],{"class":38,"line":72},[36,25225,76],{"class":75},[36,25227,79],{"class":50},[36,25229,1243],{"class":54},[36,25231,3106],{"class":46},[36,25233,66],{"class":54},[36,25235,86],{"class":50},[14,25237,107],{},[26,25239,25241],{"className":28,"code":25240,"language":30,"meta":31,"style":31},"53\n8\n",[33,25242,25243,25248],{"__ignoreMap":31},[36,25244,25245],{"class":38,"line":39},[36,25246,25247],{"class":54},"53\n",[36,25249,25250],{"class":38,"line":72},[36,25251,11629],{"class":54},[14,25253,25254],{},"Why?",[157,25256,25257,25265],{},[160,25258,25259,1281,25261,25264],{},[33,25260,5395],{},[33,25262,25263],{},"\"3\""," are strings, so Python joins them",[160,25266,25267,1281,25269,25271],{},[33,25268,1243],{},[33,25270,1247],{}," are numbers, so Python adds them",[14,25273,25274],{},"If you need math, convert the string first:",[26,25276,25278],{"className":28,"code":25277,"language":30,"meta":31,"style":31},"a = \"5\"\nb = \"3\"\n\nprint(int(a) + int(b))\n",[33,25279,25280,25292,25305,25309],{"__ignoreMap":31},[36,25281,25282,25284,25286,25288,25290],{"class":38,"line":39},[36,25283,3316],{"class":42},[36,25285,47],{"class":46},[36,25287,224],{"class":141},[36,25289,1243],{"class":145},[36,25291,1954],{"class":141},[36,25293,25294,25297,25299,25301,25303],{"class":38,"line":72},[36,25295,25296],{"class":42},"b ",[36,25298,47],{"class":46},[36,25300,224],{"class":141},[36,25302,1247],{"class":145},[36,25304,1954],{"class":141},[36,25306,25307],{"class":38,"line":89},[36,25308,476],{"emptyLinePlaceholder":475},[36,25310,25311,25313,25315,25317,25319,25321,25323,25325,25327,25329,25331],{"class":38,"line":496},[36,25312,76],{"class":75},[36,25314,79],{"class":50},[36,25316,2466],{"class":96},[36,25318,79],{"class":50},[36,25320,347],{"class":82},[36,25322,1988],{"class":50},[36,25324,3106],{"class":46},[36,25326,5739],{"class":96},[36,25328,79],{"class":50},[36,25330,721],{"class":82},[36,25332,104],{"class":50},[14,25334,107],{},[26,25336,25337],{"className":28,"code":11629,"language":30,"meta":31,"style":31},[33,25338,25339],{"__ignoreMap":31},[36,25340,25341],{"class":38,"line":39},[36,25342,11629],{"class":54},[14,25344,2489,25345,25348,25349,25351,25352,351],{},[33,25346,25347],{},"int()"," for whole numbers and ",[33,25350,7692],{}," for decimal numbers. If you need help with this, see ",[347,25353,25355],{"href":25354},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python\u002F","how to convert a string to an integer in Python",[21,25357,25359],{"id":25358},"how-this-page-is-different-from-other-string-pages","How this page is different from other string pages",[14,25361,25362,25363,25365],{},"This page defines the term ",[331,25364,4728],{}," only.",[14,25367,25368],{},"It does not fully teach:",[157,25370,25371,25374,25377],{},[160,25372,25373],{},"Every string method",[160,25375,25376],{},"Advanced formatting",[160,25378,25379],{},"All ways to create and modify strings",[14,25381,25382],{},"Use this page to understand the basic meaning of a string. Then move on to deeper pages for creating strings, changing them, splitting them, joining them, and formatting them.",[21,25384,905],{"id":904},[14,25386,25387],{},"Beginners often run into these problems with strings:",[157,25389,25390,25393,25396,25401],{},[160,25391,25392],{},"Thinking quoted numbers are real numbers",[160,25394,25395],{},"Forgetting quotes around text",[160,25397,25398,25399],{},"Mixing strings and integers with ",[33,25400,4763],{},[160,25402,25403,25404,25406],{},"Expecting ",[33,25405,9395],{}," to return a number automatically",[14,25408,25409],{},"Example of a common mistake:",[26,25411,25413],{"className":28,"code":25412,"language":30,"meta":31,"style":31},"age = input(\"Enter your age: \")\nprint(age + 1)\n",[33,25414,25415,25435],{"__ignoreMap":31},[36,25416,25417,25419,25421,25424,25426,25428,25431,25433],{"class":38,"line":39},[36,25418,1705],{"class":42},[36,25420,47],{"class":46},[36,25422,25423],{"class":75}," input",[36,25425,79],{"class":50},[36,25427,214],{"class":141},[36,25429,25430],{"class":145},"Enter your age: ",[36,25432,214],{"class":141},[36,25434,86],{"class":50},[36,25436,25437,25439,25441,25443,25445,25447],{"class":38,"line":72},[36,25438,76],{"class":75},[36,25440,79],{"class":50},[36,25442,1705],{"class":82},[36,25444,4763],{"class":46},[36,25446,16839],{"class":54},[36,25448,86],{"class":50},[14,25450,20672,25451,25453],{},[33,25452,9395],{}," returns a string, and Python cannot add a string and an integer directly.",[14,25455,25456],{},"A correct version is:",[26,25458,25460],{"className":28,"code":25459,"language":30,"meta":31,"style":31},"age = int(input(\"Enter your age: \"))\nprint(age + 1)\n",[33,25461,25462,25485],{"__ignoreMap":31},[36,25463,25464,25466,25468,25470,25472,25475,25477,25479,25481,25483],{"class":38,"line":39},[36,25465,1705],{"class":42},[36,25467,47],{"class":46},[36,25469,5739],{"class":96},[36,25471,79],{"class":50},[36,25473,25474],{"class":75},"input",[36,25476,79],{"class":50},[36,25478,214],{"class":141},[36,25480,25430],{"class":145},[36,25482,214],{"class":141},[36,25484,104],{"class":50},[36,25486,25487,25489,25491,25493,25495,25497],{"class":38,"line":72},[36,25488,76],{"class":75},[36,25490,79],{"class":50},[36,25492,1705],{"class":82},[36,25494,4763],{"class":46},[36,25496,16839],{"class":54},[36,25498,86],{"class":50},[14,25500,25501],{},"When debugging string problems, these checks help a lot:",[26,25503,25505],{"className":28,"code":25504,"language":30,"meta":31,"style":31},"print(value)\nprint(type(value))\nprint(len(value))\n",[33,25506,25507,25517,25531],{"__ignoreMap":31},[36,25508,25509,25511,25513,25515],{"class":38,"line":39},[36,25510,76],{"class":75},[36,25512,79],{"class":50},[36,25514,2686],{"class":82},[36,25516,86],{"class":50},[36,25518,25519,25521,25523,25525,25527,25529],{"class":38,"line":72},[36,25520,76],{"class":75},[36,25522,79],{"class":50},[36,25524,97],{"class":96},[36,25526,79],{"class":50},[36,25528,2686],{"class":82},[36,25530,104],{"class":50},[36,25532,25533,25535,25537,25539,25541,25543],{"class":38,"line":89},[36,25534,76],{"class":75},[36,25536,79],{"class":50},[36,25538,999],{"class":75},[36,25540,79],{"class":50},[36,25542,2686],{"class":82},[36,25544,104],{"class":50},[14,25546,25547],{},"Use them to answer simple questions:",[157,25549,25550,25553,25556],{},[160,25551,25552],{},"What is the actual value?",[160,25554,25555],{},"What type is it?",[160,25557,25558],{},"How long is it?",[21,25560,1063],{"id":1062},[1065,25562,25564],{"id":25563},"is-a-string-always-text","Is a string always text?",[14,25566,25567,25568,351],{},"Yes. In Python, a string represents text data, even if the text contains digits like ",[33,25569,24471],{},[1065,25571,25573,25574,1281,25576,5844],{"id":25572},"what-is-the-difference-between-5-and-5-in-python","What is the difference between ",[33,25575,5395],{},[33,25577,1243],{},[14,25579,25580,25582,25583,25585],{},[33,25581,5395],{}," is a string. ",[33,25584,1243],{}," is an integer. The first is text, the second is a number.",[1065,25587,25589],{"id":25588},"can-a-string-be-empty","Can a string be empty?",[14,25591,25592,25593,1180,25595,351],{},"Yes. An empty string is written as ",[33,25594,24367],{},[33,25596,25597],{},"''",[1065,25599,25601],{"id":25600},"do-i-have-to-use-double-quotes-for-strings","Do I have to use double quotes for strings?",[14,25603,25604],{},"No. You can use single quotes or double quotes. Both create strings.",[1065,25606,9361,25608,25610],{"id":25607},"why-does-input-usually-give-me-a-string",[33,25609,9395],{}," usually give me a string?",[14,25612,25613],{},"Because user input is read as text by default. Convert it if you need a number.",[21,25615,1105],{"id":1104},[157,25617,25618,25623,25629,25637,25643,25648,25657],{},[160,25619,25620],{},[347,25621,25622],{"href":25170},"Python strings explained: basics and examples",[160,25624,25625],{},[347,25626,25628],{"href":25627},"\u002Freference\u002Fpython-string-creating-a-string\u002F","Creating a string in Python",[160,25630,25631],{},[347,25632,1665,25634,2816],{"href":25633},"\u002Freference\u002Fpython-str-function-explained\u002F",[33,25635,25636],{},"str()",[160,25638,25639],{},[347,25640,25642],{"href":25641},"\u002Fhow-to\u002Fhow-to-convert-int-to-string-in-python\u002F","How to convert int to string in Python",[160,25644,25645],{},[347,25646,25647],{"href":25354},"How to convert string to int in Python",[160,25649,25650],{},[347,25651,25653,25654,7353],{"href":25652},"\u002Freference\u002Fpython-string-replace-method\u002F","Python string ",[33,25655,25656],{},"replace()",[160,25658,25659],{},[347,25660,25653,25662,7353],{"href":25661},"\u002Freference\u002Fpython-string-format-method\u002F",[33,25663,25664],{},"format()",[1137,25666,25667],{},"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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":25669},[25670,25671,25672,25673,25682,25683,25684,25685,25694],{"id":24445,"depth":72,"text":24446},{"id":24556,"depth":72,"text":24557},{"id":24659,"depth":72,"text":24660},{"id":24766,"depth":72,"text":24767,"children":25674},[25675,25677,25679,25680,25681],{"id":24773,"depth":89,"text":25676},"Join text together with +",{"id":24846,"depth":89,"text":25678},"Repeat text with *",{"id":24888,"depth":89,"text":24889},{"id":24965,"depth":89,"text":24966},{"id":25036,"depth":89,"text":25037},{"id":15984,"depth":72,"text":15985},{"id":25358,"depth":72,"text":25359},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":25686},[25687,25688,25690,25691,25692],{"id":25563,"depth":89,"text":25564},{"id":25572,"depth":89,"text":25689},"What is the difference between \"5\" and 5 in Python?",{"id":25588,"depth":89,"text":25589},{"id":25600,"depth":89,"text":25601},{"id":25607,"depth":89,"text":25693},"Why does input() usually give me a string?",{"id":1104,"depth":72,"text":1105},"Master what is a string in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-string-in-python",{"title":24339,"description":25695},"glossary\u002Fwhat-is-a-string-in-python","Td1pXRnDFzUyxLtt-UvNN658iZV0k9uDXXrR28M2_04",{"id":25702,"title":25703,"body":25704,"description":27028,"extension":1159,"meta":27029,"navigation":475,"path":855,"seo":27030,"stem":27031,"__hash__":27032},"content\u002Fglossary\u002Fwhat-is-a-tuple-in-python.md","What Is a Tuple in Python?",{"type":7,"value":25705,"toc":27004},[25706,25709,25712,25719,25722,25747,25754,25756,25759,25762,25779,25781,25828,25830,25863,25868,25872,25875,25889,25891,25936,25938,25967,25970,25983,25990,25994,25997,26000,26004,26052,26056,26059,26106,26108,26139,26142,26177,26179,26198,26202,26228,26230,26238,26245,26249,26252,26270,26274,26339,26341,26356,26363,26367,26421,26423,26445,26452,26456,26526,26528,26548,26552,26604,26606,26614,26623,26627,26630,26633,26644,26646,26770,26772,26794,26799,26806,26808,26811,26825,26827,26893,26895,26927,26930,26933,26941,26943,26947,26950,26954,26957,26961,26964,26968,26975,26977,27001],[10,25707,25703],{"id":25708},"what-is-a-tuple-in-python",[14,25710,25711],{},"A tuple in Python is a way to store multiple values in a single variable.",[14,25713,25714,25715,25718],{},"Tuples are ",[331,25716,25717],{},"ordered collections",", which means the items stay in the same position. They are usually used for data that should not change after it is created.",[14,25720,25721],{},"You will often see tuples written with parentheses, like this:",[26,25723,25725],{"className":28,"code":25724,"language":30,"meta":31,"style":31},"numbers = (1, 2, 3)\n",[33,25726,25727],{"__ignoreMap":31},[36,25728,25729,25731,25733,25735,25737,25739,25741,25743,25745],{"class":38,"line":39},[36,25730,43],{"class":42},[36,25732,47],{"class":46},[36,25734,744],{"class":50},[36,25736,55],{"class":54},[36,25738,58],{"class":50},[36,25740,61],{"class":54},[36,25742,58],{"class":50},[36,25744,66],{"class":54},[36,25746,86],{"class":50},[14,25748,25749,25750,351],{},"If you want a deeper lesson, see ",[347,25751,25753],{"href":25752},"\u002Flearn\u002Fpython-tuples-explained\u002F","Python tuples explained",[21,25755,6101],{"id":6100},[14,25757,25758],{},"A tuple is a collection of values stored together.",[14,25760,25761],{},"Here are the main ideas:",[157,25763,25764,25767,25770,25773],{},[160,25765,25766],{},"A tuple is a collection of values in one variable",[160,25768,25769],{},"Tuple items keep their order",[160,25771,25772],{},"Tuples are usually used for data that should stay the same",[160,25774,25775,25776],{},"Tuples are written with parentheses, like ",[33,25777,25778],{},"(1, 2, 3)",[14,25780,197],{},[26,25782,25784],{"className":28,"code":25783,"language":30,"meta":31,"style":31},"colors = (\"red\", \"green\", \"blue\")\nprint(colors)\n",[33,25785,25786,25818],{"__ignoreMap":31},[36,25787,25788,25790,25792,25794,25796,25798,25800,25802,25804,25806,25808,25810,25812,25814,25816],{"class":38,"line":39},[36,25789,207],{"class":42},[36,25791,47],{"class":46},[36,25793,744],{"class":50},[36,25795,214],{"class":141},[36,25797,217],{"class":145},[36,25799,214],{"class":141},[36,25801,58],{"class":50},[36,25803,224],{"class":141},[36,25805,227],{"class":145},[36,25807,214],{"class":141},[36,25809,58],{"class":50},[36,25811,224],{"class":141},[36,25813,236],{"class":145},[36,25815,214],{"class":141},[36,25817,86],{"class":50},[36,25819,25820,25822,25824,25826],{"class":38,"line":72},[36,25821,76],{"class":75},[36,25823,79],{"class":50},[36,25825,246],{"class":82},[36,25827,86],{"class":50},[14,25829,107],{},[26,25831,25833],{"className":28,"code":25832,"language":30,"meta":31,"style":31},"('red', 'green', 'blue')\n",[33,25834,25835],{"__ignoreMap":31},[36,25836,25837,25839,25841,25843,25845,25847,25849,25851,25853,25855,25857,25859,25861],{"class":38,"line":39},[36,25838,79],{"class":50},[36,25840,149],{"class":141},[36,25842,217],{"class":145},[36,25844,149],{"class":141},[36,25846,58],{"class":50},[36,25848,142],{"class":141},[36,25850,227],{"class":145},[36,25852,149],{"class":141},[36,25854,58],{"class":50},[36,25856,142],{"class":141},[36,25858,236],{"class":145},[36,25860,149],{"class":141},[36,25862,86],{"class":50},[14,25864,3765,25865,25867],{},[33,25866,246],{}," is a tuple with three items.",[21,25869,25871],{"id":25870},"what-makes-a-tuple-different","What makes a tuple different",[14,25873,25874],{},"A tuple has a few important features:",[157,25876,25877,25880,25883,25886],{},[160,25878,25879],{},"Tuples are ordered",[160,25881,25882],{},"Tuples can store different data types",[160,25884,25885],{},"Tuples are immutable, which means you cannot change individual items after creation",[160,25887,25888],{},"Tuples can contain duplicate values",[14,25890,197],{},[26,25892,25894],{"className":28,"code":25893,"language":30,"meta":31,"style":31},"data = (\"Alice\", 25, True, 25)\nprint(data)\n",[33,25895,25896,25925],{"__ignoreMap":31},[36,25897,25898,25901,25903,25905,25907,25909,25911,25913,25915,25917,25919,25921,25923],{"class":38,"line":39},[36,25899,25900],{"class":42},"data ",[36,25902,47],{"class":46},[36,25904,744],{"class":50},[36,25906,214],{"class":141},[36,25908,5993],{"class":145},[36,25910,214],{"class":141},[36,25912,58],{"class":50},[36,25914,6011],{"class":54},[36,25916,58],{"class":50},[36,25918,401],{"class":400},[36,25920,58],{"class":50},[36,25922,6011],{"class":54},[36,25924,86],{"class":50},[36,25926,25927,25929,25931,25934],{"class":38,"line":72},[36,25928,76],{"class":75},[36,25930,79],{"class":50},[36,25932,25933],{"class":82},"data",[36,25935,86],{"class":50},[14,25937,107],{},[26,25939,25941],{"className":28,"code":25940,"language":30,"meta":31,"style":31},"('Alice', 25, True, 25)\n",[33,25942,25943],{"__ignoreMap":31},[36,25944,25945,25947,25949,25951,25953,25955,25957,25959,25961,25963,25965],{"class":38,"line":39},[36,25946,79],{"class":50},[36,25948,149],{"class":141},[36,25950,5993],{"class":145},[36,25952,149],{"class":141},[36,25954,58],{"class":50},[36,25956,6011],{"class":54},[36,25958,58],{"class":50},[36,25960,401],{"class":400},[36,25962,58],{"class":50},[36,25964,6011],{"class":54},[36,25966,86],{"class":50},[14,25968,25969],{},"This tuple contains:",[157,25971,25972,25974,25977,25980],{},[160,25973,15806],{},[160,25975,25976],{},"an integer",[160,25978,25979],{},"a boolean",[160,25981,25982],{},"a duplicate value",[14,25984,25985,25986,351],{},"If you are new to immutability, see ",[347,25987,25989],{"href":25988},"\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types\u002F","mutable vs immutable types in Python",[21,25991,25993],{"id":25992},"how-to-create-a-tuple","How to create a tuple",[14,25995,25996],{},"You create a tuple by separating values with commas.",[14,25998,25999],{},"Parentheses are common because they make the code easier to read.",[1065,26001,26003],{"id":26002},"regular-tuple","Regular tuple",[26,26005,26007],{"className":28,"code":26006,"language":30,"meta":31,"style":31},"fruits = (\"apple\", \"banana\", \"orange\")\nprint(fruits)\n",[33,26008,26009,26042],{"__ignoreMap":31},[36,26010,26011,26013,26015,26017,26019,26021,26023,26025,26027,26029,26031,26033,26035,26038,26040],{"class":38,"line":39},[36,26012,22534],{"class":42},[36,26014,47],{"class":46},[36,26016,744],{"class":50},[36,26018,214],{"class":141},[36,26020,388],{"class":145},[36,26022,214],{"class":141},[36,26024,58],{"class":50},[36,26026,224],{"class":141},[36,26028,17233],{"class":145},[36,26030,214],{"class":141},[36,26032,58],{"class":50},[36,26034,224],{"class":141},[36,26036,26037],{"class":145},"orange",[36,26039,214],{"class":141},[36,26041,86],{"class":50},[36,26043,26044,26046,26048,26050],{"class":38,"line":72},[36,26045,76],{"class":75},[36,26047,79],{"class":50},[36,26049,22571],{"class":82},[36,26051,86],{"class":50},[1065,26053,26055],{"id":26054},"single-item-tuple","Single-item tuple",[14,26057,26058],{},"A one-item tuple needs a trailing comma.",[26,26060,26062],{"className":28,"code":26061,"language":30,"meta":31,"style":31},"item = (\"apple\",)\nprint(item)\nprint(type(item))\n",[33,26063,26064,26082,26092],{"__ignoreMap":31},[36,26065,26066,26069,26071,26073,26075,26077,26079],{"class":38,"line":39},[36,26067,26068],{"class":42},"item ",[36,26070,47],{"class":46},[36,26072,744],{"class":50},[36,26074,214],{"class":141},[36,26076,388],{"class":145},[36,26078,214],{"class":141},[36,26080,26081],{"class":50},",)\n",[36,26083,26084,26086,26088,26090],{"class":38,"line":72},[36,26085,76],{"class":75},[36,26087,79],{"class":50},[36,26089,10455],{"class":82},[36,26091,86],{"class":50},[36,26093,26094,26096,26098,26100,26102,26104],{"class":38,"line":89},[36,26095,76],{"class":75},[36,26097,79],{"class":50},[36,26099,97],{"class":96},[36,26101,79],{"class":50},[36,26103,10455],{"class":82},[36,26105,104],{"class":50},[14,26107,107],{},[26,26109,26111],{"className":28,"code":26110,"language":30,"meta":31,"style":31},"('apple',)\n\u003Cclass 'tuple'>\n",[33,26112,26113,26125],{"__ignoreMap":31},[36,26114,26115,26117,26119,26121,26123],{"class":38,"line":39},[36,26116,79],{"class":50},[36,26118,149],{"class":141},[36,26120,388],{"class":145},[36,26122,149],{"class":141},[36,26124,26081],{"class":50},[36,26126,26127,26129,26131,26133,26135,26137],{"class":38,"line":72},[36,26128,134],{"class":46},[36,26130,138],{"class":137},[36,26132,142],{"class":141},[36,26134,5615],{"class":145},[36,26136,149],{"class":141},[36,26138,152],{"class":46},[14,26140,26141],{},"Without the comma, Python does not treat it as a tuple:",[26,26143,26145],{"className":28,"code":26144,"language":30,"meta":31,"style":31},"item = (\"apple\")\nprint(type(item))\n",[33,26146,26147,26163],{"__ignoreMap":31},[36,26148,26149,26151,26153,26155,26157,26159,26161],{"class":38,"line":39},[36,26150,26068],{"class":42},[36,26152,47],{"class":46},[36,26154,744],{"class":50},[36,26156,214],{"class":141},[36,26158,388],{"class":145},[36,26160,214],{"class":141},[36,26162,86],{"class":50},[36,26164,26165,26167,26169,26171,26173,26175],{"class":38,"line":72},[36,26166,76],{"class":75},[36,26168,79],{"class":50},[36,26170,97],{"class":96},[36,26172,79],{"class":50},[36,26174,10455],{"class":82},[36,26176,104],{"class":50},[14,26178,107],{},[26,26180,26182],{"className":28,"code":26181,"language":30,"meta":31,"style":31},"\u003Cclass 'str'>\n",[33,26183,26184],{"__ignoreMap":31},[36,26185,26186,26188,26190,26192,26194,26196],{"class":38,"line":39},[36,26187,134],{"class":46},[36,26189,138],{"class":137},[36,26191,142],{"class":141},[36,26193,2360],{"class":145},[36,26195,149],{"class":141},[36,26197,152],{"class":46},[1065,26199,26201],{"id":26200},"empty-tuple","Empty tuple",[26,26203,26205],{"className":28,"code":26204,"language":30,"meta":31,"style":31},"empty = ()\nprint(empty)\n",[33,26206,26207,26217],{"__ignoreMap":31},[36,26208,26209,26212,26214],{"class":38,"line":39},[36,26210,26211],{"class":42},"empty ",[36,26213,47],{"class":46},[36,26215,26216],{"class":50}," ()\n",[36,26218,26219,26221,26223,26226],{"class":38,"line":72},[36,26220,76],{"class":75},[36,26222,79],{"class":50},[36,26224,26225],{"class":82},"empty",[36,26227,86],{"class":50},[14,26229,107],{},[26,26231,26232],{"className":28,"code":3170,"language":30,"meta":31,"style":31},[33,26233,26234],{"__ignoreMap":31},[36,26235,26236],{"class":38,"line":39},[36,26237,3170],{"class":50},[14,26239,26240,26241,351],{},"For more detail, see ",[347,26242,26244],{"href":26243},"\u002Freference\u002Fpython-tuple-creating-a-tuple\u002F","creating a tuple in Python",[21,26246,26248],{"id":26247},"how-to-use-tuple-values","How to use tuple values",[14,26250,26251],{},"Even though tuples cannot be changed item by item, you can still read and use their values.",[157,26253,26254,26257,26260,26265],{},[160,26255,26256],{},"Access items with an index",[160,26258,26259],{},"Use slicing to get part of a tuple",[160,26261,26262,26263,15790],{},"Loop through a tuple with a ",[33,26264,9852],{},[160,26266,2489,26267,26269],{},[33,26268,9390],{}," to get the number of items",[1065,26271,26273],{"id":26272},"access-an-item-by-index","Access an item by index",[26,26275,26277],{"className":28,"code":26276,"language":30,"meta":31,"style":31},"letters = (\"a\", \"b\", \"c\")\nprint(letters[0])\nprint(letters[1])\n",[33,26278,26279,26311,26325],{"__ignoreMap":31},[36,26280,26281,26283,26285,26287,26289,26291,26293,26295,26297,26299,26301,26303,26305,26307,26309],{"class":38,"line":39},[36,26282,22898],{"class":42},[36,26284,47],{"class":46},[36,26286,744],{"class":50},[36,26288,214],{"class":141},[36,26290,347],{"class":145},[36,26292,214],{"class":141},[36,26294,58],{"class":50},[36,26296,224],{"class":141},[36,26298,721],{"class":145},[36,26300,214],{"class":141},[36,26302,58],{"class":50},[36,26304,224],{"class":141},[36,26306,730],{"class":145},[36,26308,214],{"class":141},[36,26310,86],{"class":50},[36,26312,26313,26315,26317,26319,26321,26323],{"class":38,"line":72},[36,26314,76],{"class":75},[36,26316,79],{"class":50},[36,26318,22937],{"class":82},[36,26320,117],{"class":50},[36,26322,490],{"class":54},[36,26324,493],{"class":50},[36,26326,26327,26329,26331,26333,26335,26337],{"class":38,"line":89},[36,26328,76],{"class":75},[36,26330,79],{"class":50},[36,26332,22937],{"class":82},[36,26334,117],{"class":50},[36,26336,55],{"class":54},[36,26338,493],{"class":50},[14,26340,107],{},[26,26342,26344],{"className":28,"code":26343,"language":30,"meta":31,"style":31},"a\nb\n",[33,26345,26346,26351],{"__ignoreMap":31},[36,26347,26348],{"class":38,"line":39},[36,26349,26350],{"class":42},"a\n",[36,26352,26353],{"class":38,"line":72},[36,26354,26355],{"class":42},"b\n",[14,26357,26358,26359,351],{},"If needed, learn more about ",[347,26360,26362],{"href":26361},"\u002Freference\u002Fpython-tuple-indexing-explained\u002F","tuple indexing in Python",[1065,26364,26366],{"id":26365},"get-part-of-a-tuple-with-slicing","Get part of a tuple with slicing",[26,26368,26370],{"className":28,"code":26369,"language":30,"meta":31,"style":31},"numbers = (10, 20, 30, 40, 50)\nprint(numbers[1:4])\n",[33,26371,26372,26403],{"__ignoreMap":31},[36,26373,26374,26376,26378,26380,26382,26384,26386,26388,26391,26393,26396,26398,26401],{"class":38,"line":39},[36,26375,43],{"class":42},[36,26377,47],{"class":46},[36,26379,744],{"class":50},[36,26381,1560],{"class":54},[36,26383,58],{"class":50},[36,26385,1628],{"class":54},[36,26387,58],{"class":50},[36,26389,26390],{"class":54}," 30",[36,26392,58],{"class":50},[36,26394,26395],{"class":54}," 40",[36,26397,58],{"class":50},[36,26399,26400],{"class":54}," 50",[36,26402,86],{"class":50},[36,26404,26405,26407,26409,26411,26413,26415,26417,26419],{"class":38,"line":72},[36,26406,76],{"class":75},[36,26408,79],{"class":50},[36,26410,83],{"class":82},[36,26412,117],{"class":50},[36,26414,55],{"class":54},[36,26416,822],{"class":50},[36,26418,11622],{"class":54},[36,26420,493],{"class":50},[14,26422,107],{},[26,26424,26426],{"className":28,"code":26425,"language":30,"meta":31,"style":31},"(20, 30, 40)\n",[33,26427,26428],{"__ignoreMap":31},[36,26429,26430,26432,26435,26437,26439,26441,26443],{"class":38,"line":39},[36,26431,79],{"class":50},[36,26433,26434],{"class":54},"20",[36,26436,58],{"class":50},[36,26438,26390],{"class":54},[36,26440,58],{"class":50},[36,26442,26395],{"class":54},[36,26444,86],{"class":50},[14,26446,26447,26448,351],{},"You can read more in ",[347,26449,26451],{"href":26450},"\u002Freference\u002Fpython-tuple-slicing-explained\u002F","tuple slicing explained",[1065,26453,26455],{"id":26454},"loop-through-a-tuple","Loop through a tuple",[26,26457,26459],{"className":28,"code":26458,"language":30,"meta":31,"style":31},"days = (\"Mon\", \"Tue\", \"Wed\")\n\nfor day in days:\n    print(day)\n",[33,26460,26461,26497,26501,26515],{"__ignoreMap":31},[36,26462,26463,26466,26468,26470,26472,26475,26477,26479,26481,26484,26486,26488,26490,26493,26495],{"class":38,"line":39},[36,26464,26465],{"class":42},"days ",[36,26467,47],{"class":46},[36,26469,744],{"class":50},[36,26471,214],{"class":141},[36,26473,26474],{"class":145},"Mon",[36,26476,214],{"class":141},[36,26478,58],{"class":50},[36,26480,224],{"class":141},[36,26482,26483],{"class":145},"Tue",[36,26485,214],{"class":141},[36,26487,58],{"class":50},[36,26489,224],{"class":141},[36,26491,26492],{"class":145},"Wed",[36,26494,214],{"class":141},[36,26496,86],{"class":50},[36,26498,26499],{"class":38,"line":72},[36,26500,476],{"emptyLinePlaceholder":475},[36,26502,26503,26505,26508,26510,26513],{"class":38,"line":89},[36,26504,9852],{"class":1718},[36,26506,26507],{"class":42}," day ",[36,26509,9911],{"class":1718},[36,26511,26512],{"class":42}," days",[36,26514,1730],{"class":50},[36,26516,26517,26519,26521,26524],{"class":38,"line":496},[36,26518,1735],{"class":75},[36,26520,79],{"class":50},[36,26522,26523],{"class":82},"day",[36,26525,86],{"class":50},[14,26527,107],{},[26,26529,26531],{"className":28,"code":26530,"language":30,"meta":31,"style":31},"Mon\nTue\nWed\n",[33,26532,26533,26538,26543],{"__ignoreMap":31},[36,26534,26535],{"class":38,"line":39},[36,26536,26537],{"class":42},"Mon\n",[36,26539,26540],{"class":38,"line":72},[36,26541,26542],{"class":42},"Tue\n",[36,26544,26545],{"class":38,"line":89},[36,26546,26547],{"class":42},"Wed\n",[1065,26549,26551],{"id":26550},"get-the-number-of-items","Get the number of items",[26,26553,26555],{"className":28,"code":26554,"language":30,"meta":31,"style":31},"days = (\"Mon\", \"Tue\", \"Wed\")\nprint(len(days))\n",[33,26556,26557,26589],{"__ignoreMap":31},[36,26558,26559,26561,26563,26565,26567,26569,26571,26573,26575,26577,26579,26581,26583,26585,26587],{"class":38,"line":39},[36,26560,26465],{"class":42},[36,26562,47],{"class":46},[36,26564,744],{"class":50},[36,26566,214],{"class":141},[36,26568,26474],{"class":145},[36,26570,214],{"class":141},[36,26572,58],{"class":50},[36,26574,224],{"class":141},[36,26576,26483],{"class":145},[36,26578,214],{"class":141},[36,26580,58],{"class":50},[36,26582,224],{"class":141},[36,26584,26492],{"class":145},[36,26586,214],{"class":141},[36,26588,86],{"class":50},[36,26590,26591,26593,26595,26597,26599,26602],{"class":38,"line":72},[36,26592,76],{"class":75},[36,26594,79],{"class":50},[36,26596,999],{"class":75},[36,26598,79],{"class":50},[36,26600,26601],{"class":82},"days",[36,26603,104],{"class":50},[14,26605,107],{},[26,26607,26608],{"className":28,"code":9948,"language":30,"meta":31,"style":31},[33,26609,26610],{"__ignoreMap":31},[36,26611,26612],{"class":38,"line":39},[36,26613,9948],{"class":54},[14,26615,26616,26617,351],{},"See also ",[347,26618,2803,26620,26622],{"href":26619},"\u002Freference\u002Fpython-tuple-length-len\u002F",[33,26621,9390],{}," works with tuples",[21,26624,26626],{"id":26625},"when-beginners-should-use-tuples","When beginners should use tuples",[14,26628,26629],{},"Tuples are a good choice when the data should stay fixed.",[14,26631,26632],{},"Use a tuple for:",[157,26634,26635,26638,26641],{},[160,26636,26637],{},"fixed data, like days of the week or coordinates",[160,26639,26640],{},"values that should not be changed by mistake",[160,26642,26643],{},"grouped values that belong together",[14,26645,694],{},[26,26647,26649],{"className":28,"code":26648,"language":30,"meta":31,"style":31},"days_of_week = (\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\")\npoint = (10, 20)\n\nprint(days_of_week[0])\nprint(point)\n",[33,26650,26651,26723,26740,26744,26759],{"__ignoreMap":31},[36,26652,26653,26656,26658,26660,26662,26665,26667,26669,26671,26674,26676,26678,26680,26683,26685,26687,26689,26692,26694,26696,26698,26701,26703,26705,26707,26710,26712,26714,26716,26719,26721],{"class":38,"line":39},[36,26654,26655],{"class":42},"days_of_week ",[36,26657,47],{"class":46},[36,26659,744],{"class":50},[36,26661,214],{"class":141},[36,26663,26664],{"class":145},"Monday",[36,26666,214],{"class":141},[36,26668,58],{"class":50},[36,26670,224],{"class":141},[36,26672,26673],{"class":145},"Tuesday",[36,26675,214],{"class":141},[36,26677,58],{"class":50},[36,26679,224],{"class":141},[36,26681,26682],{"class":145},"Wednesday",[36,26684,214],{"class":141},[36,26686,58],{"class":50},[36,26688,224],{"class":141},[36,26690,26691],{"class":145},"Thursday",[36,26693,214],{"class":141},[36,26695,58],{"class":50},[36,26697,224],{"class":141},[36,26699,26700],{"class":145},"Friday",[36,26702,214],{"class":141},[36,26704,58],{"class":50},[36,26706,224],{"class":141},[36,26708,26709],{"class":145},"Saturday",[36,26711,214],{"class":141},[36,26713,58],{"class":50},[36,26715,224],{"class":141},[36,26717,26718],{"class":145},"Sunday",[36,26720,214],{"class":141},[36,26722,86],{"class":50},[36,26724,26725,26728,26730,26732,26734,26736,26738],{"class":38,"line":72},[36,26726,26727],{"class":42},"point ",[36,26729,47],{"class":46},[36,26731,744],{"class":50},[36,26733,1560],{"class":54},[36,26735,58],{"class":50},[36,26737,1628],{"class":54},[36,26739,86],{"class":50},[36,26741,26742],{"class":38,"line":89},[36,26743,476],{"emptyLinePlaceholder":475},[36,26745,26746,26748,26750,26753,26755,26757],{"class":38,"line":496},[36,26747,76],{"class":75},[36,26749,79],{"class":50},[36,26751,26752],{"class":82},"days_of_week",[36,26754,117],{"class":50},[36,26756,490],{"class":54},[36,26758,493],{"class":50},[36,26760,26761,26763,26765,26768],{"class":38,"line":501},[36,26762,76],{"class":75},[36,26764,79],{"class":50},[36,26766,26767],{"class":82},"point",[36,26769,86],{"class":50},[14,26771,107],{},[26,26773,26775],{"className":28,"code":26774,"language":30,"meta":31,"style":31},"Monday\n(10, 20)\n",[33,26776,26777,26782],{"__ignoreMap":31},[36,26778,26779],{"class":38,"line":39},[36,26780,26781],{"class":42},"Monday\n",[36,26783,26784,26786,26788,26790,26792],{"class":38,"line":72},[36,26785,79],{"class":50},[36,26787,1560],{"class":54},[36,26789,58],{"class":50},[36,26791,1628],{"class":54},[36,26793,86],{"class":50},[14,26795,6748,26796,26798],{},[331,26797,146],{}," instead when you need to add, remove, or change items.",[14,26800,26801,26802,1281,26804,351],{},"If you are comparing the two, see ",[347,26803,22617],{"href":22616},[347,26805,851],{"href":6908},[21,26807,10615],{"id":10614},[14,26809,26810],{},"There is one detail that often confuses beginners.",[157,26812,26813,26816,26819,26822],{},[160,26814,26815],{},"A tuple itself cannot be changed after creation",[160,26817,26818],{},"But a tuple can contain mutable objects, such as lists",[160,26820,26821],{},"This page defines the term only",[160,26823,26824],{},"Use the main tuple learning and reference pages for indexing, slicing, and methods",[14,26826,197],{},[26,26828,26830],{"className":28,"code":26829,"language":30,"meta":31,"style":31},"data = ([1, 2], [3, 4])\ndata[0].append(99)\n\nprint(data)\n",[33,26831,26832,26859,26879,26883],{"__ignoreMap":31},[36,26833,26834,26836,26838,26841,26843,26845,26847,26849,26851,26853,26855,26857],{"class":38,"line":39},[36,26835,25900],{"class":42},[36,26837,47],{"class":46},[36,26839,26840],{"class":50}," ([",[36,26842,55],{"class":54},[36,26844,58],{"class":50},[36,26846,61],{"class":54},[36,26848,23999],{"class":50},[36,26850,51],{"class":50},[36,26852,1247],{"class":54},[36,26854,58],{"class":50},[36,26856,1549],{"class":54},[36,26858,493],{"class":50},[36,26860,26861,26863,26865,26867,26870,26872,26874,26877],{"class":38,"line":72},[36,26862,25933],{"class":42},[36,26864,117],{"class":50},[36,26866,490],{"class":54},[36,26868,26869],{"class":50},"].",[36,26871,508],{"class":82},[36,26873,79],{"class":50},[36,26875,26876],{"class":54},"99",[36,26878,86],{"class":50},[36,26880,26881],{"class":38,"line":89},[36,26882,476],{"emptyLinePlaceholder":475},[36,26884,26885,26887,26889,26891],{"class":38,"line":496},[36,26886,76],{"class":75},[36,26888,79],{"class":50},[36,26890,25933],{"class":82},[36,26892,86],{"class":50},[14,26894,107],{},[26,26896,26898],{"className":28,"code":26897,"language":30,"meta":31,"style":31},"([1, 2, 99], [3, 4])\n",[33,26899,26900],{"__ignoreMap":31},[36,26901,26902,26904,26906,26908,26910,26912,26915,26917,26919,26921,26923,26925],{"class":38,"line":39},[36,26903,5490],{"class":50},[36,26905,55],{"class":54},[36,26907,58],{"class":50},[36,26909,61],{"class":54},[36,26911,58],{"class":50},[36,26913,26914],{"class":54}," 99",[36,26916,23999],{"class":50},[36,26918,51],{"class":50},[36,26920,1247],{"class":54},[36,26922,58],{"class":50},[36,26924,1549],{"class":54},[36,26926,493],{"class":50},[14,26928,26929],{},"The tuple still exists in the same structure, but the list inside it was changed.",[14,26931,26932],{},"So when people say a tuple is immutable, they mean:",[157,26934,26935,26938],{},[160,26936,26937],{},"you cannot replace tuple items directly",[160,26939,26940],{},"but objects inside the tuple might still be mutable",[21,26942,1063],{"id":1062},[1065,26944,26946],{"id":26945},"is-a-tuple-the-same-as-a-list","Is a tuple the same as a list?",[14,26948,26949],{},"No. Both store multiple values in order, but a list can be changed and a tuple cannot be changed item by item.",[1065,26951,26953],{"id":26952},"why-would-i-use-a-tuple-instead-of-a-list","Why would I use a tuple instead of a list?",[14,26955,26956],{},"Use a tuple when the data should stay fixed, such as coordinates, settings, or known values.",[1065,26958,26960],{"id":26959},"can-a-tuple-contain-different-data-types","Can a tuple contain different data types?",[14,26962,26963],{},"Yes. A tuple can contain strings, numbers, booleans, and other objects.",[1065,26965,26967],{"id":26966},"how-do-i-make-a-tuple-with-one-item","How do I make a tuple with one item?",[14,26969,26970,26971,26974],{},"Add a comma after the value, like ",[33,26972,26973],{},"(\"apple\",)",". Without the comma, Python does not treat it as a tuple.",[21,26976,1105],{"id":1104},[157,26978,26979,26983,26988,26992,26997],{},[160,26980,26981],{},[347,26982,25753],{"href":25752},[160,26984,26985],{},[347,26986,26987],{"href":26243},"Creating a tuple in Python",[160,26989,26990],{},[347,26991,14709],{"href":22616},[160,26993,26994],{},[347,26995,26996],{"href":25988},"Mutable vs immutable types in Python",[160,26998,26999],{},[347,27000,1135],{"href":6908},[1137,27002,27003],{},"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 .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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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":31,"searchDepth":72,"depth":72,"links":27005},[27006,27007,27008,27013,27019,27020,27021,27027],{"id":6100,"depth":72,"text":6101},{"id":25870,"depth":72,"text":25871},{"id":25992,"depth":72,"text":25993,"children":27009},[27010,27011,27012],{"id":26002,"depth":89,"text":26003},{"id":26054,"depth":89,"text":26055},{"id":26200,"depth":89,"text":26201},{"id":26247,"depth":72,"text":26248,"children":27014},[27015,27016,27017,27018],{"id":26272,"depth":89,"text":26273},{"id":26365,"depth":89,"text":26366},{"id":26454,"depth":89,"text":26455},{"id":26550,"depth":89,"text":26551},{"id":26625,"depth":72,"text":26626},{"id":10614,"depth":72,"text":10615},{"id":1062,"depth":72,"text":1063,"children":27022},[27023,27024,27025,27026],{"id":26945,"depth":89,"text":26946},{"id":26952,"depth":89,"text":26953},{"id":26959,"depth":89,"text":26960},{"id":26966,"depth":89,"text":26967},{"id":1104,"depth":72,"text":1105},"Master what is a tuple in python in our comprehensive Python beginner guide.",{},{"title":25703,"description":27028},"glossary\u002Fwhat-is-a-tuple-in-python","3Z8k_yvfRGykbY91Irp2BGiPp_uejjixLohdnqJ_pJI",{"id":27034,"title":27035,"body":27036,"description":28317,"extension":1159,"meta":28318,"navigation":475,"path":28319,"seo":28320,"stem":28321,"__hash__":28322},"content\u002Fglossary\u002Fwhat-is-a-variable-in-python.md","What Is a Variable in Python?",{"type":7,"value":27037,"toc":28287},[27038,27041,27044,27052,27057,27106,27108,27122,27124,27141,27144,27148,27151,27153,27170,27172,27199,27201,27209,27217,27224,27228,27231,27233,27244,27247,27280,27283,27320,27323,27327,27330,27345,27347,27373,27375,27384,27394,27398,27401,27405,27434,27438,27461,27465,27489,27493,27516,27525,27529,27532,27538,27584,27586,27599,27602,27605,27609,27612,27632,27635,27671,27674,27694,27697,27716,27720,27723,27726,27729,27745,27748,27764,27771,27774,27778,27781,27789,27791,27832,27834,27848,27851,27870,27873,27890,27903,27906,27908,27911,27917,27925,27938,27946,27951,27955,28000,28002,28015,28018,28022,28025,28035,28038,28049,28053,28076,28081,28088,28092,28095,28098,28105,28108,28185,28187,28228,28230,28234,28237,28241,28244,28248,28251,28255,28258,28260,28284],[10,27039,27035],{"id":27040},"what-is-a-variable-in-python",[14,27042,27043],{},"A variable in Python is a name that refers to a value.",[14,27045,27046,27047,5826,27049,27051],{},"You use variables to store data and use it later in your code. For example, you can store a person's name, an age, a price, or a ",[33,27048,1179],{},[33,27050,1183],{}," value in a variable.",[14,27053,27054,27055,351],{},"In Python, you create a variable by assigning a value with ",[33,27056,47],{},[26,27058,27060],{"className":28,"code":27059,"language":30,"meta":31,"style":31},"name = \"Sam\"\nage = 12\n\nprint(name)\nprint(age)\n",[33,27061,27062,27074,27082,27086,27096],{"__ignoreMap":31},[36,27063,27064,27066,27068,27070,27072],{"class":38,"line":39},[36,27065,1945],{"class":42},[36,27067,47],{"class":46},[36,27069,224],{"class":141},[36,27071,311],{"class":145},[36,27073,1954],{"class":141},[36,27075,27076,27078,27080],{"class":38,"line":72},[36,27077,1705],{"class":42},[36,27079,47],{"class":46},[36,27081,20445],{"class":54},[36,27083,27084],{"class":38,"line":89},[36,27085,476],{"emptyLinePlaceholder":475},[36,27087,27088,27090,27092,27094],{"class":38,"line":496},[36,27089,76],{"class":75},[36,27091,79],{"class":50},[36,27093,817],{"class":82},[36,27095,86],{"class":50},[36,27097,27098,27100,27102,27104],{"class":38,"line":501},[36,27099,76],{"class":75},[36,27101,79],{"class":50},[36,27103,835],{"class":82},[36,27105,86],{"class":50},[14,27107,107],{},[26,27109,27111],{"className":28,"code":27110,"language":30,"meta":31,"style":31},"Sam\n12\n",[33,27112,27113,27117],{"__ignoreMap":31},[36,27114,27115],{"class":38,"line":39},[36,27116,17094],{"class":42},[36,27118,27119],{"class":38,"line":72},[36,27120,27121],{"class":54},"12\n",[14,27123,6187],{},[157,27125,27126,27134],{},[160,27127,27128,27130,27131],{},[33,27129,817],{}," is a variable that refers to the text ",[33,27132,27133],{},"\"Sam\"",[160,27135,27136,27138,27139],{},[33,27137,835],{}," is a variable that refers to the number ",[33,27140,20418],{},[14,27142,27143],{},"This is the basic idea of a variable: a named place for storing data.",[21,27145,27147],{"id":27146},"what-a-variable-means","What a variable means",[14,27149,27150],{},"A variable is a name that refers to a value.",[14,27152,10628],{},[157,27154,27155,27158,27166],{},[160,27156,27157],{},"It lets you store data and use it later",[160,27159,27160,27161,5826,27163,27165],{},"The value can be text, a number, ",[33,27162,1179],{},[33,27164,1183],{},", or other kinds of data",[160,27167,27054,27168],{},[33,27169,47],{},[14,27171,197],{},[26,27173,27175],{"className":28,"code":27174,"language":30,"meta":31,"style":31},"message = \"Hello\"\nprint(message)\n",[33,27176,27177,27189],{"__ignoreMap":31},[36,27178,27179,27181,27183,27185,27187],{"class":38,"line":39},[36,27180,4909],{"class":42},[36,27182,47],{"class":46},[36,27184,224],{"class":141},[36,27186,8801],{"class":145},[36,27188,1954],{"class":141},[36,27190,27191,27193,27195,27197],{"class":38,"line":72},[36,27192,76],{"class":75},[36,27194,79],{"class":50},[36,27196,24422],{"class":82},[36,27198,86],{"class":50},[14,27200,107],{},[26,27202,27203],{"className":28,"code":8821,"language":30,"meta":31,"style":31},[33,27204,27205],{"__ignoreMap":31},[36,27206,27207],{"class":38,"line":39},[36,27208,8821],{"class":42},[14,27210,243,27211,27213,27214,27216],{},[33,27212,24422],{}," is the variable name, and ",[33,27215,21467],{}," is the value.",[14,27218,27219,27220,351],{},"If you are new to Python, you may also want to read ",[347,27221,27223],{"href":27222},"\u002Flearn\u002Fpython-variables-explained-for-beginners\u002F","Python variables explained for beginners",[21,27225,27227],{"id":27226},"why-variables-are-useful","Why variables are useful",[14,27229,27230],{},"Variables are used in almost every Python program because they make code easier to work with.",[14,27232,18260],{},[157,27234,27235,27238,27241],{},[160,27236,27237],{},"They make code easier to read",[160,27239,27240],{},"They let you reuse values without typing them again",[160,27242,27243],{},"They make it easier to update data in one place",[14,27245,27246],{},"Example without a variable:",[26,27248,27250],{"className":28,"code":27249,"language":30,"meta":31,"style":31},"print(\"Sam\")\nprint(\"Sam\")\n",[33,27251,27252,27266],{"__ignoreMap":31},[36,27253,27254,27256,27258,27260,27262,27264],{"class":38,"line":39},[36,27255,76],{"class":75},[36,27257,79],{"class":50},[36,27259,214],{"class":141},[36,27261,311],{"class":145},[36,27263,214],{"class":141},[36,27265,86],{"class":50},[36,27267,27268,27270,27272,27274,27276,27278],{"class":38,"line":72},[36,27269,76],{"class":75},[36,27271,79],{"class":50},[36,27273,214],{"class":141},[36,27275,311],{"class":145},[36,27277,214],{"class":141},[36,27279,86],{"class":50},[14,27281,27282],{},"Example with a variable:",[26,27284,27286],{"className":28,"code":27285,"language":30,"meta":31,"style":31},"name = \"Sam\"\nprint(name)\nprint(name)\n",[33,27287,27288,27300,27310],{"__ignoreMap":31},[36,27289,27290,27292,27294,27296,27298],{"class":38,"line":39},[36,27291,1945],{"class":42},[36,27293,47],{"class":46},[36,27295,224],{"class":141},[36,27297,311],{"class":145},[36,27299,1954],{"class":141},[36,27301,27302,27304,27306,27308],{"class":38,"line":72},[36,27303,76],{"class":75},[36,27305,79],{"class":50},[36,27307,817],{"class":82},[36,27309,86],{"class":50},[36,27311,27312,27314,27316,27318],{"class":38,"line":89},[36,27313,76],{"class":75},[36,27315,79],{"class":50},[36,27317,817],{"class":82},[36,27319,86],{"class":50},[14,27321,27322],{},"The second version is easier to update. If the name changes, you only need to change it once.",[21,27324,27326],{"id":27325},"how-to-create-a-variable","How to create a variable",[14,27328,27329],{},"To create a variable in Python:",[27331,27332,27333,27336,27342],"ol",{},[160,27334,27335],{},"Write the variable name on the left",[160,27337,27338,27339,27341],{},"Write ",[33,27340,47],{}," in the middle",[160,27343,27344],{},"Write the value on the right",[14,27346,197],{},[26,27348,27350],{"className":28,"code":27349,"language":30,"meta":31,"style":31},"score = 100\nprint(score)\n",[33,27351,27352,27362],{"__ignoreMap":31},[36,27353,27354,27357,27359],{"class":38,"line":39},[36,27355,27356],{"class":42},"score ",[36,27358,47],{"class":46},[36,27360,27361],{"class":54}," 100\n",[36,27363,27364,27366,27368,27371],{"class":38,"line":72},[36,27365,76],{"class":75},[36,27367,79],{"class":50},[36,27369,27370],{"class":82},"score",[36,27372,86],{"class":50},[14,27374,107],{},[26,27376,27378],{"className":28,"code":27377,"language":30,"meta":31,"style":31},"100\n",[33,27379,27380],{"__ignoreMap":31},[36,27381,27382],{"class":38,"line":39},[36,27383,27377],{"class":54},[14,27385,27386,27387,27389,27390,27393],{},"It helps to remember that ",[33,27388,47],{}," here means ",[331,27391,27392],{},"assignment",", not mathematical equality. It means “put this value into this variable name.”",[21,27395,27397],{"id":27396},"simple-variable-examples","Simple variable examples",[14,27399,27400],{},"Variables can store different kinds of values.",[1065,27402,27404],{"id":27403},"store-text","Store text",[26,27406,27408],{"className":28,"code":27407,"language":30,"meta":31,"style":31},"city = \"London\"\nprint(city)\n",[33,27409,27410,27424],{"__ignoreMap":31},[36,27411,27412,27415,27417,27419,27422],{"class":38,"line":39},[36,27413,27414],{"class":42},"city ",[36,27416,47],{"class":46},[36,27418,224],{"class":141},[36,27420,27421],{"class":145},"London",[36,27423,1954],{"class":141},[36,27425,27426,27428,27430,27432],{"class":38,"line":72},[36,27427,76],{"class":75},[36,27429,79],{"class":50},[36,27431,5956],{"class":82},[36,27433,86],{"class":50},[1065,27435,27437],{"id":27436},"store-a-whole-number","Store a whole number",[26,27439,27441],{"className":28,"code":27440,"language":30,"meta":31,"style":31},"count = 5\nprint(count)\n",[33,27442,27443,27451],{"__ignoreMap":31},[36,27444,27445,27447,27449],{"class":38,"line":39},[36,27446,4922],{"class":42},[36,27448,47],{"class":46},[36,27450,4927],{"class":54},[36,27452,27453,27455,27457,27459],{"class":38,"line":72},[36,27454,76],{"class":75},[36,27456,79],{"class":50},[36,27458,16171],{"class":82},[36,27460,86],{"class":50},[1065,27462,27464],{"id":27463},"store-a-decimal-number","Store a decimal number",[26,27466,27468],{"className":28,"code":27467,"language":30,"meta":31,"style":31},"price = 9.99\nprint(price)\n",[33,27469,27470,27479],{"__ignoreMap":31},[36,27471,27472,27474,27476],{"class":38,"line":39},[36,27473,4932],{"class":42},[36,27475,47],{"class":46},[36,27477,27478],{"class":54}," 9.99\n",[36,27480,27481,27483,27485,27487],{"class":38,"line":72},[36,27482,76],{"class":75},[36,27484,79],{"class":50},[36,27486,6131],{"class":82},[36,27488,86],{"class":50},[1065,27490,27492],{"id":27491},"store-a-boolean","Store a boolean",[26,27494,27496],{"className":28,"code":27495,"language":30,"meta":31,"style":31},"is_ready = True\nprint(is_ready)\n",[33,27497,27498,27506],{"__ignoreMap":31},[36,27499,27500,27502,27504],{"class":38,"line":39},[36,27501,4942],{"class":42},[36,27503,47],{"class":46},[36,27505,1419],{"class":400},[36,27507,27508,27510,27512,27514],{"class":38,"line":72},[36,27509,76],{"class":75},[36,27511,79],{"class":50},[36,27513,1858],{"class":82},[36,27515,86],{"class":50},[14,27517,27518,27519,27522,27523,351],{},"These are different ",[347,27520,27521],{"href":1203},"data types in Python",". If you want a broader view, see ",[347,27524,2895],{"href":2894},[21,27526,27528],{"id":27527},"changing-a-variable-value","Changing a variable value",[14,27530,27531],{},"A variable can point to a new value later.",[14,27533,27534,27535,351],{},"This is called ",[331,27536,27537],{},"reassignment",[26,27539,27541],{"className":28,"code":27540,"language":30,"meta":31,"style":31},"count = 5\nprint(count)\n\ncount = 6\nprint(count)\n",[33,27542,27543,27551,27561,27565,27574],{"__ignoreMap":31},[36,27544,27545,27547,27549],{"class":38,"line":39},[36,27546,4922],{"class":42},[36,27548,47],{"class":46},[36,27550,4927],{"class":54},[36,27552,27553,27555,27557,27559],{"class":38,"line":72},[36,27554,76],{"class":75},[36,27556,79],{"class":50},[36,27558,16171],{"class":82},[36,27560,86],{"class":50},[36,27562,27563],{"class":38,"line":89},[36,27564,476],{"emptyLinePlaceholder":475},[36,27566,27567,27569,27571],{"class":38,"line":496},[36,27568,4922],{"class":42},[36,27570,47],{"class":46},[36,27572,27573],{"class":54}," 6\n",[36,27575,27576,27578,27580,27582],{"class":38,"line":501},[36,27577,76],{"class":75},[36,27579,79],{"class":50},[36,27581,16171],{"class":82},[36,27583,86],{"class":50},[14,27585,107],{},[26,27587,27589],{"className":28,"code":27588,"language":30,"meta":31,"style":31},"5\n6\n",[33,27590,27591,27595],{"__ignoreMap":31},[36,27592,27593],{"class":38,"line":39},[36,27594,4825],{"class":54},[36,27596,27597],{"class":38,"line":72},[36,27598,9435],{"class":54},[14,27600,27601],{},"The newest assigned value is the current one.",[14,27603,27604],{},"This is another reason variables are useful. You can update information as your program runs.",[21,27606,27608],{"id":27607},"basic-naming-rules","Basic naming rules",[14,27610,27611],{},"Python variable names must follow some simple rules:",[157,27613,27614,27617,27620,27623],{},[160,27615,27616],{},"Names can contain letters, numbers, and underscores",[160,27618,27619],{},"Names cannot start with a number",[160,27621,27622],{},"Names cannot contain spaces",[160,27624,27625,27626,5950,27628,6128,27630],{},"You cannot use Python keywords like ",[33,27627,1192],{},[33,27629,9852],{},[33,27631,138],{},[14,27633,27634],{},"Valid names:",[26,27636,27638],{"className":28,"code":27637,"language":30,"meta":31,"style":31},"user_name = \"Ana\"\nscore1 = 10\ntotal_price = 19.99\n",[33,27639,27640,27653,27662],{"__ignoreMap":31},[36,27641,27642,27645,27647,27649,27651],{"class":38,"line":39},[36,27643,27644],{"class":42},"user_name ",[36,27646,47],{"class":46},[36,27648,224],{"class":141},[36,27650,448],{"class":145},[36,27652,1954],{"class":141},[36,27654,27655,27658,27660],{"class":38,"line":72},[36,27656,27657],{"class":42},"score1 ",[36,27659,47],{"class":46},[36,27661,10662],{"class":54},[36,27663,27664,27667,27669],{"class":38,"line":89},[36,27665,27666],{"class":42},"total_price ",[36,27668,47],{"class":46},[36,27670,6460],{"class":54},[14,27672,27673],{},"Invalid names:",[26,27675,27677],{"className":28,"code":27676,"language":30,"meta":31,"style":31},"# 2name = \"Ana\"\n# user name = \"Ana\"\n# class = \"Math\"\n",[33,27678,27679,27684,27689],{"__ignoreMap":31},[36,27680,27681],{"class":38,"line":39},[36,27682,27683],{"class":2570},"# 2name = \"Ana\"\n",[36,27685,27686],{"class":38,"line":72},[36,27687,27688],{"class":2570},"# user name = \"Ana\"\n",[36,27690,27691],{"class":38,"line":89},[36,27692,27693],{"class":2570},"# class = \"Math\"\n",[14,27695,27696],{},"Those examples are invalid because:",[157,27698,27699,27705,27711],{},[160,27700,27701,27704],{},[33,27702,27703],{},"2name"," starts with a number",[160,27706,27707,27710],{},[33,27708,27709],{},"user name"," contains a space",[160,27712,27713,27715],{},[33,27714,138],{}," is a Python keyword",[21,27717,27719],{"id":27718},"good-variable-names","Good variable names",[14,27721,27722],{},"Choose names that describe the value clearly.",[14,27724,27725],{},"Good variable names make code easier to read and debug.",[14,27727,27728],{},"Prefer names like:",[157,27730,27731,27736,27741],{},[160,27732,27733],{},[33,27734,27735],{},"total_price",[160,27737,27738],{},[33,27739,27740],{},"user_name",[160,27742,27743],{},[33,27744,1442],{},[14,27746,27747],{},"Instead of unclear names like:",[157,27749,27750,27755,27759],{},[160,27751,27752],{},[33,27753,27754],{},"tp",[160,27756,27757],{},[33,27758,5306],{},[160,27760,27761],{},[33,27762,27763],{},"a1",[14,27765,27766,27767,27770],{},"A common Python style is to use ",[331,27768,27769],{},"lowercase_with_underscores"," for variable names.",[14,27772,27773],{},"Clear names are especially helpful for beginners.",[21,27775,27777],{"id":27776},"variables-are-not-the-same-as-values","Variables are not the same as values",[14,27779,27780],{},"A variable name and a value are not the same thing.",[157,27782,27783,27786],{},[160,27784,27785],{},"The variable name is the label",[160,27787,27788],{},"The value is the actual data",[14,27790,197],{},[26,27792,27794],{"className":28,"code":27793,"language":30,"meta":31,"style":31},"name = \"Ana\"\nprint(name)\nprint(\"name\")\n",[33,27795,27796,27808,27818],{"__ignoreMap":31},[36,27797,27798,27800,27802,27804,27806],{"class":38,"line":39},[36,27799,1945],{"class":42},[36,27801,47],{"class":46},[36,27803,224],{"class":141},[36,27805,448],{"class":145},[36,27807,1954],{"class":141},[36,27809,27810,27812,27814,27816],{"class":38,"line":72},[36,27811,76],{"class":75},[36,27813,79],{"class":50},[36,27815,817],{"class":82},[36,27817,86],{"class":50},[36,27819,27820,27822,27824,27826,27828,27830],{"class":38,"line":89},[36,27821,76],{"class":75},[36,27823,79],{"class":50},[36,27825,214],{"class":141},[36,27827,817],{"class":145},[36,27829,214],{"class":141},[36,27831,86],{"class":50},[14,27833,107],{},[26,27835,27837],{"className":28,"code":27836,"language":30,"meta":31,"style":31},"Ana\nname\n",[33,27838,27839,27843],{"__ignoreMap":31},[36,27840,27841],{"class":38,"line":39},[36,27842,547],{"class":42},[36,27844,27845],{"class":38,"line":72},[36,27846,27847],{"class":42},"name\n",[14,27849,27850],{},"Why is the output different?",[157,27852,27853,27861],{},[160,27854,27855,27858,27859],{},[33,27856,27857],{},"print(name)"," uses the variable called ",[33,27860,817],{},[160,27862,27863,27866,27867,27869],{},[33,27864,27865],{},"print(\"name\")"," prints the text ",[33,27868,6194],{}," exactly as written",[14,27871,27872],{},"So in:",[26,27874,27876],{"className":28,"code":27875,"language":30,"meta":31,"style":31},"name = \"Ana\"\n",[33,27877,27878],{"__ignoreMap":31},[36,27879,27880,27882,27884,27886,27888],{"class":38,"line":39},[36,27881,1945],{"class":42},[36,27883,47],{"class":46},[36,27885,224],{"class":141},[36,27887,448],{"class":145},[36,27889,1954],{"class":141},[157,27891,27892,27897],{},[160,27893,27894,27896],{},[33,27895,817],{}," is the variable",[160,27898,27899,27902],{},[33,27900,27901],{},"\"Ana\""," is the string value",[14,27904,27905],{},"This difference matters when printing, comparing, and updating data.",[21,27907,905],{"id":904},[14,27909,27910],{},"Beginners often run into the same problems when learning variables.",[1065,27912,19422,27914,27916],{"id":27913},"thinking-means-equals-in-the-math-sense",[33,27915,47],{}," means “equals” in the math sense",[14,27918,27919,27920,27922,27923,351],{},"In Python, ",[33,27921,47],{}," usually means ",[331,27924,27392],{},[26,27926,27928],{"className":28,"code":27927,"language":30,"meta":31,"style":31},"x = 5\n",[33,27929,27930],{"__ignoreMap":31},[36,27931,27932,27934,27936],{"class":38,"line":39},[36,27933,5289],{"class":42},[36,27935,47],{"class":46},[36,27937,4927],{"class":54},[14,27939,27940,27941,27943,27944,17331],{},"This means “assign the value ",[33,27942,1243],{}," to ",[33,27945,5306],{},[14,27947,9843,27948,27950],{},[331,27949,8193],{}," mean “x equals 5” in the same way as a math equation.",[1065,27952,27954],{"id":27953},"using-quotes-around-a-variable-name-by-mistake","Using quotes around a variable name by mistake",[26,27956,27958],{"className":28,"code":27957,"language":30,"meta":31,"style":31},"name = \"Sam\"\n\nprint(name)\nprint(\"name\")\n",[33,27959,27960,27972,27976,27986],{"__ignoreMap":31},[36,27961,27962,27964,27966,27968,27970],{"class":38,"line":39},[36,27963,1945],{"class":42},[36,27965,47],{"class":46},[36,27967,224],{"class":141},[36,27969,311],{"class":145},[36,27971,1954],{"class":141},[36,27973,27974],{"class":38,"line":72},[36,27975,476],{"emptyLinePlaceholder":475},[36,27977,27978,27980,27982,27984],{"class":38,"line":89},[36,27979,76],{"class":75},[36,27981,79],{"class":50},[36,27983,817],{"class":82},[36,27985,86],{"class":50},[36,27987,27988,27990,27992,27994,27996,27998],{"class":38,"line":496},[36,27989,76],{"class":75},[36,27991,79],{"class":50},[36,27993,214],{"class":141},[36,27995,817],{"class":145},[36,27997,214],{"class":141},[36,27999,86],{"class":50},[14,28001,107],{},[26,28003,28005],{"className":28,"code":28004,"language":30,"meta":31,"style":31},"Sam\nname\n",[33,28006,28007,28011],{"__ignoreMap":31},[36,28008,28009],{"class":38,"line":39},[36,28010,17094],{"class":42},[36,28012,28013],{"class":38,"line":72},[36,28014,27847],{"class":42},[14,28016,28017],{},"With quotes, Python treats it as text, not as a variable.",[1065,28019,28021],{"id":28020},"using-an-invalid-variable-name","Using an invalid variable name",[14,28023,28024],{},"These names are not valid:",[157,28026,28027,28031],{},[160,28028,28029],{},[33,28030,27703],{},[160,28032,28033],{},[33,28034,27709],{},[14,28036,28037],{},"Use names like:",[157,28039,28040,28045],{},[160,28041,28042],{},[33,28043,28044],{},"name2",[160,28046,28047],{},[33,28048,27740],{},[1065,28050,28052],{"id":28051},"trying-to-use-a-variable-before-assigning-it","Trying to use a variable before assigning it",[26,28054,28056],{"className":28,"code":28055,"language":30,"meta":31,"style":31},"print(age)\nage = 12\n",[33,28057,28058,28068],{"__ignoreMap":31},[36,28059,28060,28062,28064,28066],{"class":38,"line":39},[36,28061,76],{"class":75},[36,28063,79],{"class":50},[36,28065,835],{"class":82},[36,28067,86],{"class":50},[36,28069,28070,28072,28074],{"class":38,"line":72},[36,28071,1705],{"class":42},[36,28073,47],{"class":46},[36,28075,20445],{"class":54},[14,28077,20672,28078,28080],{},[33,28079,835],{}," does not have a value yet.",[14,28082,28083,28084,351],{},"Python often shows this as a ",[347,28085,28087],{"href":28086},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix\u002F","NameError: name is not defined",[1065,28089,28091],{"id":28090},"confusing-variables-with-data-types","Confusing variables with data types",[14,28093,28094],{},"A variable is the name you use.",[14,28096,28097],{},"A data type is the kind of value stored there, such as a string, integer, or boolean.",[14,28099,28100,28101,351],{},"You can inspect a value's type with the ",[347,28102,28103,8246],{"href":5265},[33,28104,1327],{},[14,28106,28107],{},"Useful debugging checks:",[26,28109,28111],{"className":28,"code":28110,"language":30,"meta":31,"style":31},"name = \"Sam\"\nage = 12\n\nprint(name)\nprint(type(name))\nprint(age)\nprint(type(age))\n",[33,28112,28113,28125,28133,28137,28147,28161,28171],{"__ignoreMap":31},[36,28114,28115,28117,28119,28121,28123],{"class":38,"line":39},[36,28116,1945],{"class":42},[36,28118,47],{"class":46},[36,28120,224],{"class":141},[36,28122,311],{"class":145},[36,28124,1954],{"class":141},[36,28126,28127,28129,28131],{"class":38,"line":72},[36,28128,1705],{"class":42},[36,28130,47],{"class":46},[36,28132,20445],{"class":54},[36,28134,28135],{"class":38,"line":89},[36,28136,476],{"emptyLinePlaceholder":475},[36,28138,28139,28141,28143,28145],{"class":38,"line":496},[36,28140,76],{"class":75},[36,28142,79],{"class":50},[36,28144,817],{"class":82},[36,28146,86],{"class":50},[36,28148,28149,28151,28153,28155,28157,28159],{"class":38,"line":501},[36,28150,76],{"class":75},[36,28152,79],{"class":50},[36,28154,97],{"class":96},[36,28156,79],{"class":50},[36,28158,817],{"class":82},[36,28160,104],{"class":50},[36,28162,28163,28165,28167,28169],{"class":38,"line":522},[36,28164,76],{"class":75},[36,28166,79],{"class":50},[36,28168,835],{"class":82},[36,28170,86],{"class":50},[36,28172,28173,28175,28177,28179,28181,28183],{"class":38,"line":527},[36,28174,76],{"class":75},[36,28176,79],{"class":50},[36,28178,97],{"class":96},[36,28180,79],{"class":50},[36,28182,835],{"class":82},[36,28184,104],{"class":50},[14,28186,4527],{},[26,28188,28190],{"className":28,"code":28189,"language":30,"meta":31,"style":31},"Sam\n\u003Cclass 'str'>\n12\n\u003Cclass 'int'>\n",[33,28191,28192,28196,28210,28214],{"__ignoreMap":31},[36,28193,28194],{"class":38,"line":39},[36,28195,17094],{"class":42},[36,28197,28198,28200,28202,28204,28206,28208],{"class":38,"line":72},[36,28199,134],{"class":46},[36,28201,138],{"class":137},[36,28203,142],{"class":141},[36,28205,2360],{"class":145},[36,28207,149],{"class":141},[36,28209,152],{"class":46},[36,28211,28212],{"class":38,"line":89},[36,28213,27121],{"class":54},[36,28215,28216,28218,28220,28222,28224,28226],{"class":38,"line":496},[36,28217,134],{"class":46},[36,28219,138],{"class":137},[36,28221,142],{"class":141},[36,28223,2466],{"class":145},[36,28225,149],{"class":141},[36,28227,152],{"class":46},[21,28229,1063],{"id":1062},[1065,28231,28233],{"id":28232},"do-i-need-to-declare-a-variable-type-in-python-first","Do I need to declare a variable type in Python first?",[14,28235,28236],{},"No. Python creates the variable when you assign a value to it.",[1065,28238,28240],{"id":28239},"can-a-variable-change-from-one-type-to-another","Can a variable change from one type to another?",[14,28242,28243],{},"Yes. A variable can refer to a string first and a number later, though clear code usually avoids unnecessary changes.",[1065,28245,28247],{"id":28246},"is-a-variable-the-same-as-a-value","Is a variable the same as a value?",[14,28249,28250],{},"No. The variable is the name. The value is the data stored under that name.",[1065,28252,28254],{"id":28253},"why-does-python-say-a-name-is-not-defined","Why does Python say a name is not defined?",[14,28256,28257],{},"Usually because the variable was used before it was assigned, or the name was misspelled.",[21,28259,1105],{"id":1104},[157,28261,28262,28266,28270,28276,28280],{},[160,28263,28264],{},[347,28265,27223],{"href":27222},[160,28267,28268],{},[347,28269,2895],{"href":2894},[160,28271,28272],{},[347,28273,1665,28274,2816],{"href":5265},[33,28275,1327],{},[160,28277,28278],{},[347,28279,28087],{"href":28086},[160,28281,28282],{},[347,28283,2906],{"href":1203},[1137,28285,28286],{},"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 .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 .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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":31,"searchDepth":72,"depth":72,"links":28288},[28289,28290,28291,28292,28298,28299,28300,28301,28302,28310,28316],{"id":27146,"depth":72,"text":27147},{"id":27226,"depth":72,"text":27227},{"id":27325,"depth":72,"text":27326},{"id":27396,"depth":72,"text":27397,"children":28293},[28294,28295,28296,28297],{"id":27403,"depth":89,"text":27404},{"id":27436,"depth":89,"text":27437},{"id":27463,"depth":89,"text":27464},{"id":27491,"depth":89,"text":27492},{"id":27527,"depth":72,"text":27528},{"id":27607,"depth":72,"text":27608},{"id":27718,"depth":72,"text":27719},{"id":27776,"depth":72,"text":27777},{"id":904,"depth":72,"text":905,"children":28303},[28304,28306,28307,28308,28309],{"id":27913,"depth":89,"text":28305},"Thinking = means “equals” in the math sense",{"id":27953,"depth":89,"text":27954},{"id":28020,"depth":89,"text":28021},{"id":28051,"depth":89,"text":28052},{"id":28090,"depth":89,"text":28091},{"id":1062,"depth":72,"text":1063,"children":28311},[28312,28313,28314,28315],{"id":28232,"depth":89,"text":28233},{"id":28239,"depth":89,"text":28240},{"id":28246,"depth":89,"text":28247},{"id":28253,"depth":89,"text":28254},{"id":1104,"depth":72,"text":1105},"Master what is a variable in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-a-variable-in-python",{"title":27035,"description":28317},"glossary\u002Fwhat-is-a-variable-in-python","g4BQJtVgmQzaryqL4kiimkkL36TFCEDzHf2tUz_GhZg",{"id":28324,"title":20014,"body":28325,"description":29561,"extension":1159,"meta":29562,"navigation":475,"path":11771,"seo":29563,"stem":29564,"__hash__":29565},"content\u002Fglossary\u002Fwhat-is-an-argument-in-python.md",{"type":7,"value":28326,"toc":29536},[28327,28330,28335,28338,28345,28359,28412,28419,28421,28432,28434,28453,28460,28464,28467,28495,28497,28549,28551,28561,28570,28574,28577,28588,28590,28671,28673,28691,28694,28698,28702,28708,28760,28762,28770,28772,28787,28790,28794,28800,28870,28872,28880,28887,28891,28894,28962,28964,28978,28982,28985,28999,29001,29054,29057,29080,29086,29090,29094,29114,29120,29124,29177,29179,29188,29195,29199,29266,29268,29280,29283,29287,29323,29326,29328,29330,29349,29352,29396,29401,29409,29411,29469,29472,29474,29478,29481,29485,29488,29492,29495,29499,29502,29504,29530,29533],[10,28328,20014],{"id":28329},"what-is-an-argument-in-python",[14,28331,3361,28332,28334],{},[331,28333,19783],{}," in Python is a value you pass to a function when you call it.",[14,28336,28337],{},"This helps the function work with real data. For example, a function can greet different people, add different numbers, or print different messages based on the arguments you give it.",[14,28339,28340,28341,1281,28343,822],{},"A common beginner confusion is the difference between ",[331,28342,9016],{},[331,28344,9009],{},[157,28346,28347,28353],{},[160,28348,28349,28352],{},[331,28350,28351],{},"Parameters"," are the names written in the function definition.",[160,28354,28355,28358],{},[331,28356,28357],{},"Arguments"," are the actual values passed into the function call.",[26,28360,28362],{"className":28,"code":28361,"language":30,"meta":31,"style":31},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Sam\")\n",[33,28363,28364,28376,28394,28398],{"__ignoreMap":31},[36,28365,28366,28368,28370,28372,28374],{"class":38,"line":39},[36,28367,8785],{"class":137},[36,28369,9033],{"class":3082},[36,28371,79],{"class":50},[36,28373,817],{"class":3050},[36,28375,3054],{"class":50},[36,28377,28378,28380,28382,28384,28386,28388,28390,28392],{"class":38,"line":72},[36,28379,1735],{"class":75},[36,28381,79],{"class":50},[36,28383,214],{"class":141},[36,28385,8801],{"class":145},[36,28387,214],{"class":141},[36,28389,58],{"class":50},[36,28391,3051],{"class":82},[36,28393,86],{"class":50},[36,28395,28396],{"class":38,"line":89},[36,28397,476],{"emptyLinePlaceholder":475},[36,28399,28400,28402,28404,28406,28408,28410],{"class":38,"line":496},[36,28401,9067],{"class":82},[36,28403,79],{"class":50},[36,28405,214],{"class":141},[36,28407,311],{"class":145},[36,28409,214],{"class":141},[36,28411,86],{"class":50},[14,28413,28414,6195,28416,28418],{},[33,28415,27133],{},[331,28417,19783],{},". It is the value passed to the function.",[21,28420,6101],{"id":6100},[157,28422,28423,28426,28429],{},[160,28424,28425],{},"An argument is a value you give to a function when you call it.",[160,28427,28428],{},"The function uses that value while it runs.",[160,28430,28431],{},"Arguments go inside the parentheses after the function name.",[14,28433,1235],{},[26,28435,28437],{"className":28,"code":28436,"language":30,"meta":31,"style":31},"print(\"Hello\")\n",[33,28438,28439],{"__ignoreMap":31},[36,28440,28441,28443,28445,28447,28449,28451],{"class":38,"line":39},[36,28442,76],{"class":75},[36,28444,79],{"class":50},[36,28446,214],{"class":141},[36,28448,8801],{"class":145},[36,28450,214],{"class":141},[36,28452,86],{"class":50},[14,28454,243,28455,28457,28458,351],{},[33,28456,21467],{}," is the argument passed to ",[33,28459,9167],{},[21,28461,28463],{"id":28462},"argument-vs-parameter","Argument vs parameter",[14,28465,28466],{},"This difference is important when you start writing your own functions.",[157,28468,28469,28474,28479,28487],{},[160,28470,3355,28471,28473],{},[331,28472,19691],{}," is the variable named in the function definition.",[160,28475,3361,28476,28478],{},[331,28477,19783],{}," is the actual value passed to that function.",[160,28480,1687,28481,5950,28484,28486],{},[33,28482,28483],{},"def greet(name)",[33,28485,817],{}," is a parameter.",[160,28488,1687,28489,5950,28492,28494],{},[33,28490,28491],{},"greet(\"Sam\")",[33,28493,27133],{}," is an argument.",[14,28496,197],{},[26,28498,28499],{"className":28,"code":28361,"language":30,"meta":31,"style":31},[33,28500,28501,28513,28531,28535],{"__ignoreMap":31},[36,28502,28503,28505,28507,28509,28511],{"class":38,"line":39},[36,28504,8785],{"class":137},[36,28506,9033],{"class":3082},[36,28508,79],{"class":50},[36,28510,817],{"class":3050},[36,28512,3054],{"class":50},[36,28514,28515,28517,28519,28521,28523,28525,28527,28529],{"class":38,"line":72},[36,28516,1735],{"class":75},[36,28518,79],{"class":50},[36,28520,214],{"class":141},[36,28522,8801],{"class":145},[36,28524,214],{"class":141},[36,28526,58],{"class":50},[36,28528,3051],{"class":82},[36,28530,86],{"class":50},[36,28532,28533],{"class":38,"line":89},[36,28534,476],{"emptyLinePlaceholder":475},[36,28536,28537,28539,28541,28543,28545,28547],{"class":38,"line":496},[36,28538,9067],{"class":82},[36,28540,79],{"class":50},[36,28542,214],{"class":141},[36,28544,311],{"class":145},[36,28546,214],{"class":141},[36,28548,86],{"class":50},[14,28550,12941],{},[157,28552,28553,28557],{},[160,28554,28555,19983],{},[33,28556,817],{},[160,28558,28559,19988],{},[33,28560,27133],{},[14,28562,28563,28564,1281,28567,351],{},"If you want a fuller explanation, see ",[347,28565,28566],{"href":20009},"function parameters and arguments in Python",[347,28568,28569],{"href":9008},"what is a parameter in Python",[21,28571,28573],{"id":28572},"why-arguments-are-useful","Why arguments are useful",[14,28575,28576],{},"Arguments make functions flexible.",[157,28578,28579,28582,28585],{},[160,28580,28581],{},"They let one function work with different values.",[160,28583,28584],{},"They make code more reusable.",[160,28586,28587],{},"They help avoid repeating similar code.",[14,28589,197],{},[26,28591,28593],{"className":28,"code":28592,"language":30,"meta":31,"style":31},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Sam\")\ngreet(\"Ava\")\ngreet(\"Leo\")\n",[33,28594,28595,28607,28625,28629,28643,28657],{"__ignoreMap":31},[36,28596,28597,28599,28601,28603,28605],{"class":38,"line":39},[36,28598,8785],{"class":137},[36,28600,9033],{"class":3082},[36,28602,79],{"class":50},[36,28604,817],{"class":3050},[36,28606,3054],{"class":50},[36,28608,28609,28611,28613,28615,28617,28619,28621,28623],{"class":38,"line":72},[36,28610,1735],{"class":75},[36,28612,79],{"class":50},[36,28614,214],{"class":141},[36,28616,8801],{"class":145},[36,28618,214],{"class":141},[36,28620,58],{"class":50},[36,28622,3051],{"class":82},[36,28624,86],{"class":50},[36,28626,28627],{"class":38,"line":89},[36,28628,476],{"emptyLinePlaceholder":475},[36,28630,28631,28633,28635,28637,28639,28641],{"class":38,"line":496},[36,28632,9067],{"class":82},[36,28634,79],{"class":50},[36,28636,214],{"class":141},[36,28638,311],{"class":145},[36,28640,214],{"class":141},[36,28642,86],{"class":50},[36,28644,28645,28647,28649,28651,28653,28655],{"class":38,"line":501},[36,28646,9067],{"class":82},[36,28648,79],{"class":50},[36,28650,214],{"class":141},[36,28652,11988],{"class":145},[36,28654,214],{"class":141},[36,28656,86],{"class":50},[36,28658,28659,28661,28663,28665,28667,28669],{"class":38,"line":522},[36,28660,9067],{"class":82},[36,28662,79],{"class":50},[36,28664,214],{"class":141},[36,28666,302],{"class":145},[36,28668,214],{"class":141},[36,28670,86],{"class":50},[14,28672,107],{},[26,28674,28676],{"className":28,"code":28675,"language":30,"meta":31,"style":31},"Hello Sam\nHello Ava\nHello Leo\n",[33,28677,28678,28683,28687],{"__ignoreMap":31},[36,28679,28680],{"class":38,"line":39},[36,28681,28682],{"class":42},"Hello Sam\n",[36,28684,28685],{"class":38,"line":72},[36,28686,20129],{"class":42},[36,28688,28689],{"class":38,"line":89},[36,28690,20124],{"class":42},[14,28692,28693],{},"The function stays the same. Only the argument changes.",[21,28695,28697],{"id":28696},"common-types-of-arguments-beginners-see","Common types of arguments beginners see",[1065,28699,28701],{"id":28700},"positional-arguments","Positional arguments",[14,28703,3355,28704,28707],{},[331,28705,28706],{},"positional argument"," is matched by order.",[26,28709,28710],{"className":28,"code":20236,"language":30,"meta":31,"style":31},[33,28711,28712,28728,28742,28746],{"__ignoreMap":31},[36,28713,28714,28716,28718,28720,28722,28724,28726],{"class":38,"line":39},[36,28715,8785],{"class":137},[36,28717,19865],{"class":3082},[36,28719,79],{"class":50},[36,28721,347],{"class":3050},[36,28723,58],{"class":50},[36,28725,9194],{"class":3050},[36,28727,3054],{"class":50},[36,28729,28730,28732,28734,28736,28738,28740],{"class":38,"line":72},[36,28731,1735],{"class":75},[36,28733,79],{"class":50},[36,28735,3316],{"class":82},[36,28737,4763],{"class":46},[36,28739,9194],{"class":82},[36,28741,86],{"class":50},[36,28743,28744],{"class":38,"line":89},[36,28745,476],{"emptyLinePlaceholder":475},[36,28747,28748,28750,28752,28754,28756,28758],{"class":38,"line":496},[36,28749,11725],{"class":82},[36,28751,79],{"class":50},[36,28753,1247],{"class":54},[36,28755,58],{"class":50},[36,28757,5043],{"class":54},[36,28759,86],{"class":50},[14,28761,107],{},[26,28763,28764],{"className":28,"code":11629,"language":30,"meta":31,"style":31},[33,28765,28766],{"__ignoreMap":31},[36,28767,28768],{"class":38,"line":39},[36,28769,11629],{"class":54},[14,28771,4063],{},[157,28773,28774,28781],{},[160,28775,28776,28778,28779],{},[33,28777,1247],{}," is passed to ",[33,28780,347],{},[160,28782,28783,28778,28785],{},[33,28784,1243],{},[33,28786,721],{},[14,28788,28789],{},"If you swap the order, the values go into different parameters.",[1065,28791,28793],{"id":28792},"keyword-arguments","Keyword arguments",[14,28795,3355,28796,28799],{},[331,28797,28798],{},"keyword argument"," is matched by parameter name.",[26,28801,28803],{"className":28,"code":28802,"language":30,"meta":31,"style":31},"def greet(name, message):\n    print(message, name)\n\ngreet(name=\"Sam\", message=\"Hello\")\n",[33,28804,28805,28822,28836,28840],{"__ignoreMap":31},[36,28806,28807,28809,28811,28813,28815,28817,28820],{"class":38,"line":39},[36,28808,8785],{"class":137},[36,28810,9033],{"class":3082},[36,28812,79],{"class":50},[36,28814,817],{"class":3050},[36,28816,58],{"class":50},[36,28818,28819],{"class":3050}," message",[36,28821,3054],{"class":50},[36,28823,28824,28826,28828,28830,28832,28834],{"class":38,"line":72},[36,28825,1735],{"class":75},[36,28827,79],{"class":50},[36,28829,24422],{"class":82},[36,28831,58],{"class":50},[36,28833,3051],{"class":82},[36,28835,86],{"class":50},[36,28837,28838],{"class":38,"line":89},[36,28839,476],{"emptyLinePlaceholder":475},[36,28841,28842,28844,28846,28848,28850,28852,28854,28856,28858,28860,28862,28864,28866,28868],{"class":38,"line":496},[36,28843,9067],{"class":82},[36,28845,79],{"class":50},[36,28847,817],{"class":12021},[36,28849,47],{"class":46},[36,28851,214],{"class":141},[36,28853,311],{"class":145},[36,28855,214],{"class":141},[36,28857,58],{"class":50},[36,28859,28819],{"class":12021},[36,28861,47],{"class":46},[36,28863,214],{"class":141},[36,28865,8801],{"class":145},[36,28867,214],{"class":141},[36,28869,86],{"class":50},[14,28871,107],{},[26,28873,28874],{"className":28,"code":28682,"language":30,"meta":31,"style":31},[33,28875,28876],{"__ignoreMap":31},[36,28877,28878],{"class":38,"line":39},[36,28879,28682],{"class":42},[14,28881,28882,28883,351],{},"Keyword arguments can make code easier to read. Learn more in ",[347,28884,28886],{"href":28885},"\u002Flearn\u002Fdefault-and-keyword-arguments-explained\u002F","default and keyword arguments explained",[1065,28888,28890],{"id":28889},"default-arguments","Default arguments",[14,28892,28893],{},"A parameter can have a default value. If you do not pass an argument for it, Python uses the default.",[26,28895,28897],{"className":28,"code":28896,"language":30,"meta":31,"style":31},"def greet(name=\"friend\"):\n    print(\"Hello\", name)\n\ngreet()\ngreet(\"Sam\")\n",[33,28898,28899,28920,28938,28942,28948],{"__ignoreMap":31},[36,28900,28901,28903,28905,28907,28909,28911,28913,28916,28918],{"class":38,"line":39},[36,28902,8785],{"class":137},[36,28904,9033],{"class":3082},[36,28906,79],{"class":50},[36,28908,817],{"class":3050},[36,28910,47],{"class":46},[36,28912,214],{"class":141},[36,28914,28915],{"class":145},"friend",[36,28917,214],{"class":141},[36,28919,3054],{"class":50},[36,28921,28922,28924,28926,28928,28930,28932,28934,28936],{"class":38,"line":72},[36,28923,1735],{"class":75},[36,28925,79],{"class":50},[36,28927,214],{"class":141},[36,28929,8801],{"class":145},[36,28931,214],{"class":141},[36,28933,58],{"class":50},[36,28935,3051],{"class":82},[36,28937,86],{"class":50},[36,28939,28940],{"class":38,"line":89},[36,28941,476],{"emptyLinePlaceholder":475},[36,28943,28944,28946],{"class":38,"line":496},[36,28945,9067],{"class":82},[36,28947,3170],{"class":50},[36,28949,28950,28952,28954,28956,28958,28960],{"class":38,"line":501},[36,28951,9067],{"class":82},[36,28953,79],{"class":50},[36,28955,214],{"class":141},[36,28957,311],{"class":145},[36,28959,214],{"class":141},[36,28961,86],{"class":50},[14,28963,107],{},[26,28965,28967],{"className":28,"code":28966,"language":30,"meta":31,"style":31},"Hello friend\nHello Sam\n",[33,28968,28969,28974],{"__ignoreMap":31},[36,28970,28971],{"class":38,"line":39},[36,28972,28973],{"class":42},"Hello friend\n",[36,28975,28976],{"class":38,"line":72},[36,28977,28682],{"class":42},[21,28979,28981],{"id":28980},"how-to-read-a-function-call","How to read a function call",[14,28983,28984],{},"When you see a function call, read it in this order:",[157,28986,28987,28990,28993,28996],{},[160,28988,28989],{},"Look at the function name first.",[160,28991,28992],{},"Then look inside the parentheses.",[160,28994,28995],{},"The values inside are the arguments.",[160,28997,28998],{},"A function can have zero, one, or many arguments.",[14,29000,694],{},[26,29002,29004],{"className":28,"code":29003,"language":30,"meta":31,"style":31},"print(\"Hi\")\nlen(\"apple\")\nsum([1, 2, 3])\n",[33,29005,29006,29021,29035],{"__ignoreMap":31},[36,29007,29008,29010,29012,29014,29017,29019],{"class":38,"line":39},[36,29009,76],{"class":75},[36,29011,79],{"class":50},[36,29013,214],{"class":141},[36,29015,29016],{"class":145},"Hi",[36,29018,214],{"class":141},[36,29020,86],{"class":50},[36,29022,29023,29025,29027,29029,29031,29033],{"class":38,"line":72},[36,29024,999],{"class":75},[36,29026,79],{"class":50},[36,29028,214],{"class":141},[36,29030,388],{"class":145},[36,29032,214],{"class":141},[36,29034,86],{"class":50},[36,29036,29037,29040,29042,29044,29046,29048,29050,29052],{"class":38,"line":89},[36,29038,29039],{"class":75},"sum",[36,29041,5490],{"class":50},[36,29043,55],{"class":54},[36,29045,58],{"class":50},[36,29047,61],{"class":54},[36,29049,58],{"class":50},[36,29051,66],{"class":54},[36,29053,493],{"class":50},[14,29055,29056],{},"In these calls:",[157,29058,29059,29067,29073],{},[160,29060,29061,29064,29065],{},[33,29062,29063],{},"\"Hi\""," is an argument to ",[33,29066,9167],{},[160,29068,29069,29064,29071],{},[33,29070,22605],{},[33,29072,9390],{},[160,29074,29075,29064,29077],{},[33,29076,4889],{},[33,29078,29079],{},"sum()",[14,29081,29082,29083,351],{},"If you are new to functions, it also helps to read ",[347,29084,29085],{"href":20857},"what is a function in Python",[21,29087,29089],{"id":29088},"beginner-examples-to-include","Beginner examples to include",[1065,29091,29093],{"id":29092},"one-argument-passed-to-a-print-like-function","One argument passed to a print-like function",[26,29095,29097],{"className":28,"code":29096,"language":30,"meta":31,"style":31},"print(\"Python is fun\")\n",[33,29098,29099],{"__ignoreMap":31},[36,29100,29101,29103,29105,29107,29110,29112],{"class":38,"line":39},[36,29102,76],{"class":75},[36,29104,79],{"class":50},[36,29106,214],{"class":141},[36,29108,29109],{"class":145},"Python is fun",[36,29111,214],{"class":141},[36,29113,86],{"class":50},[14,29115,29116,29119],{},[33,29117,29118],{},"\"Python is fun\""," is the argument.",[1065,29121,29123],{"id":29122},"two-arguments-passed-to-an-add-function","Two arguments passed to an add function",[26,29125,29127],{"className":28,"code":29126,"language":30,"meta":31,"style":31},"def add(a, b):\n    print(a + b)\n\nadd(10, 20)\n",[33,29128,29129,29145,29159,29163],{"__ignoreMap":31},[36,29130,29131,29133,29135,29137,29139,29141,29143],{"class":38,"line":39},[36,29132,8785],{"class":137},[36,29134,19865],{"class":3082},[36,29136,79],{"class":50},[36,29138,347],{"class":3050},[36,29140,58],{"class":50},[36,29142,9194],{"class":3050},[36,29144,3054],{"class":50},[36,29146,29147,29149,29151,29153,29155,29157],{"class":38,"line":72},[36,29148,1735],{"class":75},[36,29150,79],{"class":50},[36,29152,3316],{"class":82},[36,29154,4763],{"class":46},[36,29156,9194],{"class":82},[36,29158,86],{"class":50},[36,29160,29161],{"class":38,"line":89},[36,29162,476],{"emptyLinePlaceholder":475},[36,29164,29165,29167,29169,29171,29173,29175],{"class":38,"line":496},[36,29166,11725],{"class":82},[36,29168,79],{"class":50},[36,29170,1560],{"class":54},[36,29172,58],{"class":50},[36,29174,1628],{"class":54},[36,29176,86],{"class":50},[14,29178,107],{},[26,29180,29182],{"className":28,"code":29181,"language":30,"meta":31,"style":31},"30\n",[33,29183,29184],{"__ignoreMap":31},[36,29185,29186],{"class":38,"line":39},[36,29187,29181],{"class":54},[14,29189,29190,1281,29192,29194],{},[33,29191,1560],{},[33,29193,26434],{}," are the arguments.",[1065,29196,29198],{"id":29197},"keyword-argument-example","Keyword argument example",[26,29200,29202],{"className":28,"code":29201,"language":30,"meta":31,"style":31},"def show_user(name, age):\n    print(name, age)\n\nshow_user(age=25, name=\"Maya\")\n",[33,29203,29204,29221,29235,29239],{"__ignoreMap":31},[36,29205,29206,29208,29211,29213,29215,29217,29219],{"class":38,"line":39},[36,29207,8785],{"class":137},[36,29209,29210],{"class":3082}," show_user",[36,29212,79],{"class":50},[36,29214,817],{"class":3050},[36,29216,58],{"class":50},[36,29218,3735],{"class":3050},[36,29220,3054],{"class":50},[36,29222,29223,29225,29227,29229,29231,29233],{"class":38,"line":72},[36,29224,1735],{"class":75},[36,29226,79],{"class":50},[36,29228,817],{"class":82},[36,29230,58],{"class":50},[36,29232,3735],{"class":82},[36,29234,86],{"class":50},[36,29236,29237],{"class":38,"line":89},[36,29238,476],{"emptyLinePlaceholder":475},[36,29240,29241,29244,29246,29248,29250,29252,29254,29256,29258,29260,29262,29264],{"class":38,"line":496},[36,29242,29243],{"class":82},"show_user",[36,29245,79],{"class":50},[36,29247,835],{"class":12021},[36,29249,47],{"class":46},[36,29251,7828],{"class":54},[36,29253,58],{"class":50},[36,29255,3051],{"class":12021},[36,29257,47],{"class":46},[36,29259,214],{"class":141},[36,29261,293],{"class":145},[36,29263,214],{"class":141},[36,29265,86],{"class":50},[14,29267,107],{},[26,29269,29271],{"className":28,"code":29270,"language":30,"meta":31,"style":31},"Maya 25\n",[33,29272,29273],{"__ignoreMap":31},[36,29274,29275,29278],{"class":38,"line":39},[36,29276,29277],{"class":42},"Maya ",[36,29279,6092],{"class":54},[14,29281,29282],{},"The arguments are still values, but they are passed by parameter name.",[1065,29284,29286],{"id":29285},"function-call-with-no-arguments","Function call with no arguments",[26,29288,29289],{"className":28,"code":8778,"language":30,"meta":31,"style":31},[33,29290,29291,29299,29313,29317],{"__ignoreMap":31},[36,29292,29293,29295,29297],{"class":38,"line":39},[36,29294,8785],{"class":137},[36,29296,8788],{"class":3082},[36,29298,4244],{"class":50},[36,29300,29301,29303,29305,29307,29309,29311],{"class":38,"line":72},[36,29302,1735],{"class":75},[36,29304,79],{"class":50},[36,29306,214],{"class":141},[36,29308,8801],{"class":145},[36,29310,214],{"class":141},[36,29312,86],{"class":50},[36,29314,29315],{"class":38,"line":89},[36,29316,476],{"emptyLinePlaceholder":475},[36,29318,29319,29321],{"class":38,"line":496},[36,29320,8814],{"class":82},[36,29322,3170],{"class":50},[14,29324,29325],{},"This function call has no arguments because nothing is inside the parentheses.",[21,29327,905],{"id":904},[14,29329,8484],{},[157,29331,29332,29335,29340,29343,29346],{},[160,29333,29334],{},"Mixing up arguments and parameters",[160,29336,29337,29338,19988],{},"Thinking the variable in ",[33,29339,8785],{},[160,29341,29342],{},"Passing arguments in the wrong order",[160,29344,29345],{},"Forgetting required arguments when calling a function",[160,29347,29348],{},"Using a keyword name that does not exist in the function definition",[14,29350,29351],{},"Example of a missing argument:",[26,29353,29354],{"className":28,"code":20627,"language":30,"meta":31,"style":31},[33,29355,29356,29368,29386,29390],{"__ignoreMap":31},[36,29357,29358,29360,29362,29364,29366],{"class":38,"line":39},[36,29359,8785],{"class":137},[36,29361,9033],{"class":3082},[36,29363,79],{"class":50},[36,29365,817],{"class":3050},[36,29367,3054],{"class":50},[36,29369,29370,29372,29374,29376,29378,29380,29382,29384],{"class":38,"line":72},[36,29371,1735],{"class":75},[36,29373,79],{"class":50},[36,29375,214],{"class":141},[36,29377,8801],{"class":145},[36,29379,214],{"class":141},[36,29381,58],{"class":50},[36,29383,3051],{"class":82},[36,29385,86],{"class":50},[36,29387,29388],{"class":38,"line":89},[36,29389,476],{"emptyLinePlaceholder":475},[36,29391,29392,29394],{"class":38,"line":496},[36,29393,9067],{"class":82},[36,29395,3170],{"class":50},[14,29397,20672,29398,29400],{},[33,29399,817],{}," needs a value.",[14,29402,29403,29404,351],{},"If you see this problem, read ",[347,29405,17866,29406],{"href":20681},[33,29407,29408],{},"TypeError: missing required positional argument",[14,29410,14472],{},[26,29412,29414],{"className":28,"code":29413,"language":30,"meta":31,"style":31},"help(print)\nhelp(len)\nprint(type(\"Sam\"))\nprint(len.__defaults__)\n",[33,29415,29416,29426,29436,29454],{"__ignoreMap":31},[36,29417,29418,29420,29422,29424],{"class":38,"line":39},[36,29419,4392],{"class":75},[36,29421,79],{"class":50},[36,29423,76],{"class":75},[36,29425,86],{"class":50},[36,29427,29428,29430,29432,29434],{"class":38,"line":72},[36,29429,4392],{"class":75},[36,29431,79],{"class":50},[36,29433,999],{"class":75},[36,29435,86],{"class":50},[36,29437,29438,29440,29442,29444,29446,29448,29450,29452],{"class":38,"line":89},[36,29439,76],{"class":75},[36,29441,79],{"class":50},[36,29443,97],{"class":96},[36,29445,79],{"class":50},[36,29447,214],{"class":141},[36,29449,311],{"class":145},[36,29451,214],{"class":141},[36,29453,104],{"class":50},[36,29455,29456,29458,29460,29462,29464,29467],{"class":38,"line":496},[36,29457,76],{"class":75},[36,29459,79],{"class":50},[36,29461,999],{"class":75},[36,29463,351],{"class":50},[36,29465,29466],{"class":3059},"__defaults__",[36,29468,86],{"class":50},[14,29470,29471],{},"These can help you inspect built-in functions and understand what kind of values you are passing.",[21,29473,1063],{"id":1062},[1065,29475,29477],{"id":29476},"what-is-the-difference-between-an-argument-and-a-parameter-in-python","What is the difference between an argument and a parameter in Python?",[14,29479,29480],{},"A parameter is the name in the function definition. An argument is the value passed when calling the function.",[1065,29482,29484],{"id":29483},"can-a-function-have-more-than-one-argument","Can a function have more than one argument?",[14,29486,29487],{},"Yes. A function can accept multiple arguments if it is defined with multiple parameters.",[1065,29489,29491],{"id":29490},"can-a-function-have-no-arguments","Can a function have no arguments?",[14,29493,29494],{},"Yes. Some functions are called with empty parentheses.",[1065,29496,29498],{"id":29497},"are-keyword-arguments-and-positional-arguments-both-arguments","Are keyword arguments and positional arguments both arguments?",[14,29500,29501],{},"Yes. They are two common ways to pass values to a function.",[21,29503,1105],{"id":1104},[157,29505,29506,29511,29515,29519,29524],{},[160,29507,29508],{},[347,29509,29510],{"href":20009},"Function parameters and arguments in Python",[160,29512,29513],{},[347,29514,9758],{"href":9008},[160,29516,29517],{},[347,29518,12742],{"href":20857},[160,29520,29521],{},[347,29522,29523],{"href":28885},"Default and keyword arguments explained",[160,29525,29526],{},[347,29527,18100,29528],{"href":20681},[33,29529,29408],{},[14,29531,29532],{},"Next, learn how parameters and arguments work together in real function examples.",[1137,29534,29535],{},"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 .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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":29537},[29538,29539,29540,29541,29546,29547,29553,29554,29560],{"id":6100,"depth":72,"text":6101},{"id":28462,"depth":72,"text":28463},{"id":28572,"depth":72,"text":28573},{"id":28696,"depth":72,"text":28697,"children":29542},[29543,29544,29545],{"id":28700,"depth":89,"text":28701},{"id":28792,"depth":89,"text":28793},{"id":28889,"depth":89,"text":28890},{"id":28980,"depth":72,"text":28981},{"id":29088,"depth":72,"text":29089,"children":29548},[29549,29550,29551,29552],{"id":29092,"depth":89,"text":29093},{"id":29122,"depth":89,"text":29123},{"id":29197,"depth":89,"text":29198},{"id":29285,"depth":89,"text":29286},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":29555},[29556,29557,29558,29559],{"id":29476,"depth":89,"text":29477},{"id":29483,"depth":89,"text":29484},{"id":29490,"depth":89,"text":29491},{"id":29497,"depth":89,"text":29498},{"id":1104,"depth":72,"text":1105},"Master what is an argument in python in our comprehensive Python beginner guide.",{},{"title":20014,"description":29561},"glossary\u002Fwhat-is-an-argument-in-python","-8aV3VDQG4YbaW1kznCmTUHo2kl-lKbjK0HX9sT8itk",{"id":29567,"title":29568,"body":29569,"description":30528,"extension":1159,"meta":30529,"navigation":475,"path":30530,"seo":30531,"stem":30532,"__hash__":30533},"content\u002Fglossary\u002Fwhat-is-an-exception-in-python.md","What Is an Exception in Python?",{"type":7,"value":29570,"toc":30510},[29571,29574,29580,29587,29590,29610,29614,29617,29672,29674,29683,29685,29711,29715,29718,29735,29741,29755,29762,29766,29769,29788,29790,29833,29859,29881,29931,29933,29936,29969,29977,29980,30042,30044,30053,30055,30072,30086,30090,30093,30112,30115,30136,30139,30143,30146,30159,30161,30175,30185,30189,30192,30225,30228,30257,30260,30292,30296,30299,30316,30319,30323,30326,30338,30341,30360,30367,30382,30385,30400,30406,30421,30424,30426,30430,30433,30437,30445,30449,30452,30456,30469,30471,30499,30507],[10,29572,29568],{"id":29573},"what-is-an-exception-in-python",[14,29575,29576,29577,351],{},"An exception in Python is a problem that happens ",[331,29578,29579],{},"while your program is running",[14,29581,29582,29583,29586],{},"When an exception happens, Python usually stops the normal flow of the program and shows an error message. In many cases, you can also ",[331,29584,29585],{},"handle"," the exception so your program responds in a better way instead of crashing.",[14,29588,29589],{},"A simple way to think about it:",[157,29591,29592,29595,29598,29601],{},[160,29593,29594],{},"Your code starts running normally",[160,29596,29597],{},"Something unexpected happens",[160,29599,29600],{},"Python raises an exception",[160,29602,29603,29604,1281,29607],{},"You can either let it stop the program or handle it with ",[33,29605,29606],{},"try",[33,29608,29609],{},"except",[21,29611,29613],{"id":29612},"a-quick-example","A quick example",[14,29615,29616],{},"Here is a small example of an exception being handled:",[26,29618,29620],{"className":28,"code":29619,"language":30,"meta":31,"style":31},"try:\n    number = int(\"abc\")\nexcept ValueError:\n    print(\"That value cannot be converted to an integer\")\n",[33,29621,29622,29628,29648,29657],{"__ignoreMap":31},[36,29623,29624,29626],{"class":38,"line":39},[36,29625,29606],{"class":1718},[36,29627,1730],{"class":50},[36,29629,29630,29633,29635,29637,29639,29641,29644,29646],{"class":38,"line":72},[36,29631,29632],{"class":42},"    number ",[36,29634,47],{"class":46},[36,29636,5739],{"class":96},[36,29638,79],{"class":50},[36,29640,214],{"class":141},[36,29642,29643],{"class":145},"abc",[36,29645,214],{"class":141},[36,29647,86],{"class":50},[36,29649,29650,29652,29655],{"class":38,"line":89},[36,29651,29609],{"class":1718},[36,29653,29654],{"class":96}," ValueError",[36,29656,1730],{"class":50},[36,29658,29659,29661,29663,29665,29668,29670],{"class":38,"line":496},[36,29660,1735],{"class":75},[36,29662,79],{"class":50},[36,29664,214],{"class":141},[36,29666,29667],{"class":145},"That value cannot be converted to an integer",[36,29669,214],{"class":141},[36,29671,86],{"class":50},[14,29673,107],{},[26,29675,29677],{"className":28,"code":29676,"language":30,"meta":31,"style":31},"That value cannot be converted to an integer\n",[33,29678,29679],{"__ignoreMap":31},[36,29680,29681],{"class":38,"line":39},[36,29682,29676],{"class":42},[14,29684,8121],{},[157,29686,29687,29697,29702,29708],{},[160,29688,29689,29692,29693,29696],{},[33,29690,29691],{},"int(\"abc\")"," tries to turn the text ",[33,29694,29695],{},"\"abc\""," into an integer",[160,29698,29699,29700],{},"That is not possible, so Python raises a ",[33,29701,8416],{},[160,29703,3542,29704,29707],{},[33,29705,29706],{},"except ValueError:"," block catches that exception",[160,29709,29710],{},"Instead of crashing, the program prints a helpful message",[21,29712,29714],{"id":29713},"what-an-exception-means","What an exception means",[14,29716,29717],{},"An exception is:",[157,29719,29720,29726,29729,29732],{},[160,29721,29722,29723],{},"An error that happens ",[331,29724,29725],{},"while a program is running",[160,29727,29728],{},"Something that interrupts normal execution",[160,29730,29731],{},"A way for Python to report what went wrong",[160,29733,29734],{},"Usually shown with an exception name and a traceback",[14,29736,29737,29738,351],{},"Exceptions are different from ",[331,29739,29740],{},"syntax errors",[157,29742,29743,29749],{},[160,29744,3355,29745,29748],{},[331,29746,29747],{},"syntax error"," means the code itself is written incorrectly",[160,29750,3361,29751,29754],{},[331,29752,29753],{},"exception"," means the code starts running, but a problem happens during execution",[14,29756,29757,29758,351],{},"For a broader introduction, see ",[347,29759,29761],{"href":29760},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained",[21,29763,29765],{"id":29764},"when-exceptions-happen","When exceptions happen",[14,29767,29768],{},"Exceptions happen in very common situations, such as:",[157,29770,29771,29774,29779,29782,29785],{},[160,29772,29773],{},"Using a bad list index",[160,29775,29776,29777],{},"Converting invalid text with ",[33,29778,25347],{},[160,29780,29781],{},"Opening a file that does not exist",[160,29783,29784],{},"Dividing by zero",[160,29786,29787],{},"Looking up a missing dictionary key",[14,29789,694],{},[26,29791,29793],{"className":28,"code":29792,"language":30,"meta":31,"style":31},"numbers = [10, 20, 30]\nprint(numbers[5])  # IndexError\n",[33,29794,29795,29815],{"__ignoreMap":31},[36,29796,29797,29799,29801,29803,29805,29807,29809,29811,29813],{"class":38,"line":39},[36,29798,43],{"class":42},[36,29800,47],{"class":46},[36,29802,51],{"class":50},[36,29804,1560],{"class":54},[36,29806,58],{"class":50},[36,29808,1628],{"class":54},[36,29810,58],{"class":50},[36,29812,26390],{"class":54},[36,29814,69],{"class":50},[36,29816,29817,29819,29821,29823,29825,29827,29830],{"class":38,"line":72},[36,29818,76],{"class":75},[36,29820,79],{"class":50},[36,29822,83],{"class":82},[36,29824,117],{"class":50},[36,29826,1243],{"class":54},[36,29828,29829],{"class":50},"])",[36,29831,29832],{"class":2570},"  # IndexError\n",[26,29834,29836],{"className":28,"code":29835,"language":30,"meta":31,"style":31},"print(int(\"hello\"))  # ValueError\n",[33,29837,29838],{"__ignoreMap":31},[36,29839,29840,29842,29844,29846,29848,29850,29852,29854,29856],{"class":38,"line":39},[36,29841,76],{"class":75},[36,29843,79],{"class":50},[36,29845,2466],{"class":96},[36,29847,79],{"class":50},[36,29849,214],{"class":141},[36,29851,2671],{"class":145},[36,29853,214],{"class":141},[36,29855,16574],{"class":50},[36,29857,29858],{"class":2570},"  # ValueError\n",[26,29860,29862],{"className":28,"code":29861,"language":30,"meta":31,"style":31},"print(10 \u002F 0)  # ZeroDivisionError\n",[33,29863,29864],{"__ignoreMap":31},[36,29865,29866,29868,29870,29872,29874,29876,29878],{"class":38,"line":39},[36,29867,76],{"class":75},[36,29869,79],{"class":50},[36,29871,1560],{"class":54},[36,29873,7730],{"class":46},[36,29875,1993],{"class":54},[36,29877,1988],{"class":50},[36,29879,29880],{"class":2570},"  # ZeroDivisionError\n",[26,29882,29884],{"className":28,"code":29883,"language":30,"meta":31,"style":31},"data = {\"name\": \"Ana\"}\nprint(data[\"age\"])  # KeyError\n",[33,29885,29886,29910],{"__ignoreMap":31},[36,29887,29888,29890,29892,29894,29896,29898,29900,29902,29904,29906,29908],{"class":38,"line":39},[36,29889,25900],{"class":42},[36,29891,47],{"class":46},[36,29893,778],{"class":50},[36,29895,214],{"class":141},[36,29897,817],{"class":145},[36,29899,214],{"class":141},[36,29901,822],{"class":50},[36,29903,224],{"class":141},[36,29905,448],{"class":145},[36,29907,214],{"class":141},[36,29909,803],{"class":50},[36,29911,29912,29914,29916,29918,29920,29922,29924,29926,29928],{"class":38,"line":72},[36,29913,76],{"class":75},[36,29915,79],{"class":50},[36,29917,25933],{"class":82},[36,29919,117],{"class":50},[36,29921,214],{"class":141},[36,29923,835],{"class":145},[36,29925,214],{"class":141},[36,29927,29829],{"class":50},[36,29929,29930],{"class":2570},"  # KeyError\n",[21,29932,21054],{"id":21053},[14,29934,29935],{},"Here is code that raises an exception:",[26,29937,29939],{"className":28,"code":29938,"language":30,"meta":31,"style":31},"number = int(\"abc\")\nprint(number)\n",[33,29940,29941,29959],{"__ignoreMap":31},[36,29942,29943,29945,29947,29949,29951,29953,29955,29957],{"class":38,"line":39},[36,29944,8343],{"class":42},[36,29946,47],{"class":46},[36,29948,5739],{"class":96},[36,29950,79],{"class":50},[36,29952,214],{"class":141},[36,29954,29643],{"class":145},[36,29956,214],{"class":141},[36,29958,86],{"class":50},[36,29960,29961,29963,29965,29967],{"class":38,"line":72},[36,29962,76],{"class":75},[36,29964,79],{"class":50},[36,29966,8368],{"class":82},[36,29968,86],{"class":50},[14,29970,29971,29972,7079,29974,29976],{},"If you run it, Python raises a ",[33,29973,8416],{},[33,29975,29695],{}," is not a valid integer.",[14,29978,29979],{},"Now compare that with a handled version:",[26,29981,29983],{"className":28,"code":29982,"language":30,"meta":31,"style":31},"try:\n    number = int(\"abc\")\n    print(number)\nexcept ValueError:\n    print(\"Please enter a number using digits only\")\n",[33,29984,29985,29991,30009,30019,30027],{"__ignoreMap":31},[36,29986,29987,29989],{"class":38,"line":39},[36,29988,29606],{"class":1718},[36,29990,1730],{"class":50},[36,29992,29993,29995,29997,29999,30001,30003,30005,30007],{"class":38,"line":72},[36,29994,29632],{"class":42},[36,29996,47],{"class":46},[36,29998,5739],{"class":96},[36,30000,79],{"class":50},[36,30002,214],{"class":141},[36,30004,29643],{"class":145},[36,30006,214],{"class":141},[36,30008,86],{"class":50},[36,30010,30011,30013,30015,30017],{"class":38,"line":89},[36,30012,1735],{"class":75},[36,30014,79],{"class":50},[36,30016,8368],{"class":82},[36,30018,86],{"class":50},[36,30020,30021,30023,30025],{"class":38,"line":496},[36,30022,29609],{"class":1718},[36,30024,29654],{"class":96},[36,30026,1730],{"class":50},[36,30028,30029,30031,30033,30035,30038,30040],{"class":38,"line":501},[36,30030,1735],{"class":75},[36,30032,79],{"class":50},[36,30034,214],{"class":141},[36,30036,30037],{"class":145},"Please enter a number using digits only",[36,30039,214],{"class":141},[36,30041,86],{"class":50},[14,30043,107],{},[26,30045,30047],{"className":28,"code":30046,"language":30,"meta":31,"style":31},"Please enter a number using digits only\n",[33,30048,30049],{"__ignoreMap":31},[36,30050,30051],{"class":38,"line":39},[36,30052,30046],{"class":42},[14,30054,11671],{},[157,30056,30057,30065],{},[160,30058,30059,30061,30062],{},[33,30060,29606],{}," means: ",[331,30063,30064],{},"run this code",[160,30066,30067,30061,30069],{},[33,30068,29609],{},[331,30070,30071],{},"if this specific error happens, do this instead",[14,30073,30074,30075,1281,30078,30081,30082,351],{},"If you want to learn the full pattern, including ",[33,30076,30077],{},"else",[33,30079,30080],{},"finally",", read ",[347,30083,30085],{"href":30084},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python\u002F","using try, except, else, and finally in Python",[21,30087,30089],{"id":30088},"why-exceptions-are-useful","Why exceptions are useful",[14,30091,30092],{},"Exceptions are useful because:",[157,30094,30095,30098,30106,30109],{},[160,30096,30097],{},"They prevent silent failures",[160,30099,30100,30101,1180,30103],{},"They give clear names such as ",[33,30102,8416],{},[33,30104,30105],{},"IndexError",[160,30107,30108],{},"They let programs respond to problems",[160,30110,30111],{},"They help you debug by showing what caused the problem",[14,30113,30114],{},"Without exceptions, your program might fail in confusing ways. With exceptions, Python tells you:",[157,30116,30117,30123,30129],{},[160,30118,30119,30122],{},[331,30120,30121],{},"what"," went wrong",[160,30124,30125,30128],{},[331,30126,30127],{},"where"," it happened",[160,30130,30131,30132,30135],{},"often ",[331,30133,30134],{},"which type"," of problem it is",[14,30137,30138],{},"That makes fixing bugs much easier.",[21,30140,30142],{"id":30141},"exception-vs-error-handling","Exception vs error handling",[14,30144,30145],{},"These two ideas are related, but they are not the same:",[157,30147,30148,30153],{},[160,30149,3361,30150,30152],{},[331,30151,29753],{}," is the problem itself",[160,30154,30155,30158],{},[331,30156,30157],{},"Error handling"," is the code you write to deal with that problem",[14,30160,1235],{},[157,30162,30163,30168],{},[160,30164,30165,30167],{},[33,30166,8416],{}," is an exception",[160,30169,30170,1281,30172,30174],{},[33,30171,29606],{},[33,30173,29609],{}," are part of error handling",[14,30176,30177,30178,30180,30181,351],{},"This page focuses on the term ",[331,30179,29753],{},". For practical ways to catch and manage them, see ",[347,30182,30184],{"href":30183},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",[21,30186,30188],{"id":30187},"common-exception-types-beginners-see","Common exception types beginners see",[14,30190,30191],{},"These are some of the most common exceptions in Python:",[157,30193,30194,30198,30202,30206,30210,30215,30220],{},[160,30195,30196],{},[33,30197,8416],{},[160,30199,30200],{},[33,30201,20624],{},[160,30203,30204],{},[33,30205,30105],{},[160,30207,30208],{},[33,30209,6933],{},[160,30211,30212],{},[33,30213,30214],{},"NameError",[160,30216,30217],{},[33,30218,30219],{},"FileNotFoundError",[160,30221,30222],{},[33,30223,30224],{},"ZeroDivisionError",[14,30226,30227],{},"A few examples:",[157,30229,30230,30237,30242,30247,30252],{},[160,30231,30232,30234,30235],{},[33,30233,8416],{},": the value is the wrong form, such as ",[33,30236,29691],{},[160,30238,30239,30241],{},[33,30240,20624],{},": the type is wrong, such as adding a string to an integer",[160,30243,30244,30246],{},[33,30245,30105],{},": a list index does not exist",[160,30248,30249,30251],{},[33,30250,30214],{},": you use a variable that has not been defined",[160,30253,30254,30256],{},[33,30255,30219],{},": Python cannot find the file you asked for",[14,30258,30259],{},"You can read more about specific exceptions here:",[157,30261,30262,30268,30274,30280,30286],{},[160,30263,30264],{},[347,30265,30267],{"href":30266},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F","ValueError in Python: causes and fixes",[160,30269,30270],{},[347,30271,30273],{"href":30272},"\u002Ferrors\u002Ftypeerror-vs-valueerror-in-python-explained","TypeError in Python: causes and fixes",[160,30275,30276],{},[347,30277,30279],{"href":30278},"\u002Ferrors\u002Findexerror-in-python-causes-and-fixes\u002F","IndexError in Python: causes and fixes",[160,30281,30282],{},[347,30283,30285],{"href":30284},"\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes\u002F","NameError in Python: causes and fixes",[160,30287,30288],{},[347,30289,30291],{"href":30290},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes","FileNotFoundError in Python: causes and fixes",[21,30293,30295],{"id":30294},"common-causes","Common causes",[14,30297,30298],{},"Beginners often run into exceptions because of one of these problems:",[157,30300,30301,30304,30307,30310,30313],{},[160,30302,30303],{},"Using the wrong type of value in a function",[160,30305,30306],{},"Accessing data that does not exist",[160,30308,30309],{},"Working with missing files or invalid paths",[160,30311,30312],{},"Doing math that is not allowed, such as division by zero",[160,30314,30315],{},"Using variables before defining them",[14,30317,30318],{},"When you see an exception, read the exception name first. It usually gives the best clue about the problem.",[21,30320,30322],{"id":30321},"helpful-debugging-commands","Helpful debugging commands",[14,30324,30325],{},"These commands and tools can help you understand exceptions:",[26,30327,30329],{"className":19488,"code":30328,"language":19490,"meta":31,"style":31},"python your_file.py\n",[33,30330,30331],{"__ignoreMap":31},[36,30332,30333,30335],{"class":38,"line":39},[36,30334,30],{"class":3010},[36,30336,30337],{"class":145}," your_file.py\n",[14,30339,30340],{},"Runs your Python file so you can see the full error message.",[26,30342,30344],{"className":28,"code":30343,"language":30,"meta":31,"style":31},"print(type(value))\n",[33,30345,30346],{"__ignoreMap":31},[36,30347,30348,30350,30352,30354,30356,30358],{"class":38,"line":39},[36,30349,76],{"class":75},[36,30351,79],{"class":50},[36,30353,97],{"class":96},[36,30355,79],{"class":50},[36,30357,2686],{"class":82},[36,30359,104],{"class":50},[14,30361,30362,30363,1281,30365,351],{},"Shows the type of a value, which helps with ",[33,30364,20624],{},[33,30366,8416],{},[26,30368,30370],{"className":28,"code":30369,"language":30,"meta":31,"style":31},"print(value)\n",[33,30371,30372],{"__ignoreMap":31},[36,30373,30374,30376,30378,30380],{"class":38,"line":39},[36,30375,76],{"class":75},[36,30377,79],{"class":50},[36,30379,2686],{"class":82},[36,30381,86],{"class":50},[14,30383,30384],{},"Shows the actual value being used.",[26,30386,30388],{"className":28,"code":30387,"language":30,"meta":31,"style":31},"help(int)\n",[33,30389,30390],{"__ignoreMap":31},[36,30391,30392,30394,30396,30398],{"class":38,"line":39},[36,30393,4392],{"class":75},[36,30395,79],{"class":50},[36,30397,2466],{"class":96},[36,30399,86],{"class":50},[14,30401,30402,30403,30405],{},"Displays help for ",[33,30404,25347],{}," so you can see how it works.",[26,30407,30409],{"className":28,"code":30408,"language":30,"meta":31,"style":31},"dir(object)\n",[33,30410,30411],{"__ignoreMap":31},[36,30412,30413,30415,30417,30419],{"class":38,"line":39},[36,30414,17985],{"class":75},[36,30416,79],{"class":50},[36,30418,3364],{"class":96},[36,30420,86],{"class":50},[14,30422,30423],{},"Shows available attributes and methods on an object.",[21,30425,1063],{"id":1062},[1065,30427,30429],{"id":30428},"is-an-exception-the-same-as-a-syntax-error","Is an exception the same as a syntax error?",[14,30431,30432],{},"No. A syntax error means the code is written incorrectly. An exception happens while correct-looking code is running.",[1065,30434,30436],{"id":30435},"do-exceptions-always-crash-a-program","Do exceptions always crash a program?",[14,30438,30439,30440,1281,30442,30444],{},"Not always. If you handle the exception with ",[33,30441,29606],{},[33,30443,29609],{},", your program can continue or fail more clearly.",[1065,30446,30448],{"id":30447},"what-is-the-difference-between-an-exception-and-an-error","What is the difference between an exception and an error?",[14,30450,30451],{},"Beginners often use the words the same way. In Python, an exception usually means a runtime problem that Python can raise and you can handle.",[1065,30453,30455],{"id":30454},"what-is-the-most-common-exception-for-beginners","What is the most common exception for beginners?",[14,30457,30458,30459,5950,30461,5950,30463,5950,30465,5953,30467,351],{},"Common ones include ",[33,30460,8416],{},[33,30462,20624],{},[33,30464,30105],{},[33,30466,30214],{},[33,30468,30219],{},[21,30470,1105],{"id":1104},[157,30472,30473,30477,30482,30487,30491,30495],{},[160,30474,30475],{},[347,30476,29761],{"href":29760},[160,30478,30479],{},[347,30480,30481],{"href":30084},"Using try, except, else, and finally in Python",[160,30483,30484],{},[347,30485,30486],{"href":30183},"How to handle exceptions in Python",[160,30488,30489],{},[347,30490,30267],{"href":30266},[160,30492,30493],{},[347,30494,30273],{"href":30272},[160,30496,30497],{},[347,30498,30279],{"href":30278},[14,30500,30501,30502,1281,30504,30506],{},"The next useful step is to learn how to catch exceptions with ",[33,30503,29606],{},[33,30505,29609],{},", then look up the specific error type you are seeing.",[1137,30508,30509],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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 pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":31,"searchDepth":72,"depth":72,"links":30511},[30512,30513,30514,30515,30516,30517,30518,30519,30520,30521,30527],{"id":29612,"depth":72,"text":29613},{"id":29713,"depth":72,"text":29714},{"id":29764,"depth":72,"text":29765},{"id":21053,"depth":72,"text":21054},{"id":30088,"depth":72,"text":30089},{"id":30141,"depth":72,"text":30142},{"id":30187,"depth":72,"text":30188},{"id":30294,"depth":72,"text":30295},{"id":30321,"depth":72,"text":30322},{"id":1062,"depth":72,"text":1063,"children":30522},[30523,30524,30525,30526],{"id":30428,"depth":89,"text":30429},{"id":30435,"depth":89,"text":30436},{"id":30447,"depth":89,"text":30448},{"id":30454,"depth":89,"text":30455},{"id":1104,"depth":72,"text":1105},"Master what is an exception in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-an-exception-in-python",{"title":29568,"description":30528},"glossary\u002Fwhat-is-an-exception-in-python","Qu1hz4UKGJnQxVSOv2tWNI-Gjg2AOOwrfviTc8l5Q6E",{"id":30535,"title":30536,"body":30537,"description":31726,"extension":1159,"meta":31727,"navigation":475,"path":31728,"seo":31729,"stem":31730,"__hash__":31731},"content\u002Fglossary\u002Fwhat-is-an-if-statement-in-python.md","What Is an if Statement in Python?",{"type":7,"value":30538,"toc":31702},[30539,30542,30547,30559,30562,30564,30608,30610,30619,30624,30626,30631,30633,30654,30656,30702,30710,30714,30721,30727,30730,30764,30766,30798,30800,30808,30814,30859,30862,30871,30882,30886,30892,30912,30915,30937,30940,30993,30995,31008,31016,31020,31023,31067,31069,31083,31086,31132,31134,31143,31151,31157,31161,31167,31169,31183,31185,31242,31245,31252,31254,31260,31263,31280,31287,31289,31294,31298,31300,31341,31347,31354,31358,31361,31404,31410,31417,31424,31429,31431,31475,31483,31487,31494,31499,31503,31509,31558,31560,31583,31586,31613,31615,31623,31629,31631,31635,31640,31644,31650,31654,31661,31665,31671,31673,31699],[10,30540,30536],{"id":30541},"what-is-an-if-statement-in-python",[14,30543,3361,30544,30546],{},[33,30545,1192],{}," statement in Python lets your program make a decision.",[14,30548,30549,30550,30552,30553,30555,30556,30558],{},"It checks a condition. If that condition is ",[33,30551,1179],{},", Python runs the code inside the ",[33,30554,1192],{}," block. If the condition is ",[33,30557,1183],{},", Python skips that code.",[14,30560,30561],{},"This is one of the most important ideas in Python because it helps programs respond to different situations.",[21,30563,24],{"id":23},[26,30565,30567],{"className":28,"code":30566,"language":30,"meta":31,"style":31},"age = 18\n\nif age >= 18:\n    print(\"You can vote\")\n",[33,30568,30569,30577,30581,30593],{"__ignoreMap":31},[36,30570,30571,30573,30575],{"class":38,"line":39},[36,30572,1705],{"class":42},[36,30574,47],{"class":46},[36,30576,1634],{"class":54},[36,30578,30579],{"class":38,"line":72},[36,30580,476],{"emptyLinePlaceholder":475},[36,30582,30583,30585,30587,30589,30591],{"class":38,"line":89},[36,30584,1192],{"class":1718},[36,30586,1721],{"class":42},[36,30588,1724],{"class":46},[36,30590,1727],{"class":54},[36,30592,1730],{"class":50},[36,30594,30595,30597,30599,30601,30604,30606],{"class":38,"line":496},[36,30596,1735],{"class":75},[36,30598,79],{"class":50},[36,30600,214],{"class":141},[36,30602,30603],{"class":145},"You can vote",[36,30605,214],{"class":141},[36,30607,86],{"class":50},[14,30609,107],{},[26,30611,30613],{"className":28,"code":30612,"language":30,"meta":31,"style":31},"You can vote\n",[33,30614,30615],{"__ignoreMap":31},[36,30616,30617],{"class":38,"line":39},[36,30618,30612],{"class":42},[14,30620,2489,30621,30623],{},[33,30622,1192],{}," when you want Python to make a decision based on a condition.",[21,30625,6101],{"id":6100},[14,30627,3361,30628,30630],{},[33,30629,1192],{}," statement lets Python decide whether to run some code.",[14,30632,18219],{},[157,30634,30635,30640,30643,30649],{},[160,30636,3361,30637,30639],{},[33,30638,1192],{}," statement lets Python make a decision.",[160,30641,30642],{},"It checks a condition.",[160,30644,30645,30646,30648],{},"If the condition is ",[33,30647,1179],{},", the indented code runs.",[160,30650,30645,30651,30653],{},[33,30652,1183],{},", Python skips that block.",[14,30655,1235],{},[26,30657,30659],{"className":28,"code":30658,"language":30,"meta":31,"style":31},"temperature = 30\n\nif temperature > 25:\n    print(\"It is a warm day\")\n",[33,30660,30661,30669,30673,30687],{"__ignoreMap":31},[36,30662,30663,30665,30667],{"class":38,"line":39},[36,30664,8058],{"class":42},[36,30666,47],{"class":46},[36,30668,10675],{"class":54},[36,30670,30671],{"class":38,"line":72},[36,30672,476],{"emptyLinePlaceholder":475},[36,30674,30675,30677,30680,30683,30685],{"class":38,"line":89},[36,30676,1192],{"class":1718},[36,30678,30679],{"class":42}," temperature ",[36,30681,30682],{"class":46},">",[36,30684,6011],{"class":54},[36,30686,1730],{"class":50},[36,30688,30689,30691,30693,30695,30698,30700],{"class":38,"line":496},[36,30690,1735],{"class":75},[36,30692,79],{"class":50},[36,30694,214],{"class":141},[36,30696,30697],{"class":145},"It is a warm day",[36,30699,214],{"class":141},[36,30701,86],{"class":50},[14,30703,1829,30704,1833,30707,30709],{},[33,30705,30706],{},"temperature > 25",[33,30708,1179],{},", Python prints the message.",[21,30711,30713],{"id":30712},"what-a-condition-means","What a condition means",[14,30715,30716,30717,1180,30719,351],{},"A condition is an expression that becomes either ",[33,30718,1179],{},[33,30720,1183],{},[14,30722,30723,30724,30726],{},"This matters because an ",[33,30725,1192],{}," statement only works with something that can be tested in that way.",[14,30728,30729],{},"Common comparison operators are:",[157,30731,30732,30737,30743,30748,30753,30758],{},[160,30733,30734,30736],{},[33,30735,1965],{}," equal to",[160,30738,30739,30742],{},[33,30740,30741],{},"!="," not equal to",[160,30744,30745,30747],{},[33,30746,30682],{}," greater than",[160,30749,30750,30752],{},[33,30751,134],{}," less than",[160,30754,30755,30757],{},[33,30756,1724],{}," greater than or equal to",[160,30759,30760,30763],{},[33,30761,30762],{},"\u003C="," less than or equal to",[14,30765,197],{},[26,30767,30769],{"className":28,"code":30768,"language":30,"meta":31,"style":31},"score = 75\n\nprint(score >= 50)\n",[33,30770,30771,30780,30784],{"__ignoreMap":31},[36,30772,30773,30775,30777],{"class":38,"line":39},[36,30774,27356],{"class":42},[36,30776,47],{"class":46},[36,30778,30779],{"class":54}," 75\n",[36,30781,30782],{"class":38,"line":72},[36,30783,476],{"emptyLinePlaceholder":475},[36,30785,30786,30788,30790,30792,30794,30796],{"class":38,"line":89},[36,30787,76],{"class":75},[36,30789,79],{"class":50},[36,30791,27356],{"class":82},[36,30793,1724],{"class":46},[36,30795,26400],{"class":54},[36,30797,86],{"class":50},[14,30799,107],{},[26,30801,30802],{"className":28,"code":1481,"language":30,"meta":31,"style":31},[33,30803,30804],{"__ignoreMap":31},[36,30805,30806],{"class":38,"line":39},[36,30807,1481],{"class":400},[14,30809,30810,30811,30813],{},"That result can be used inside an ",[33,30812,1192],{}," statement:",[26,30815,30817],{"className":28,"code":30816,"language":30,"meta":31,"style":31},"score = 75\n\nif score >= 50:\n    print(\"You passed\")\n",[33,30818,30819,30827,30831,30844],{"__ignoreMap":31},[36,30820,30821,30823,30825],{"class":38,"line":39},[36,30822,27356],{"class":42},[36,30824,47],{"class":46},[36,30826,30779],{"class":54},[36,30828,30829],{"class":38,"line":72},[36,30830,476],{"emptyLinePlaceholder":475},[36,30832,30833,30835,30838,30840,30842],{"class":38,"line":89},[36,30834,1192],{"class":1718},[36,30836,30837],{"class":42}," score ",[36,30839,1724],{"class":46},[36,30841,26400],{"class":54},[36,30843,1730],{"class":50},[36,30845,30846,30848,30850,30852,30855,30857],{"class":38,"line":496},[36,30847,1735],{"class":75},[36,30849,79],{"class":50},[36,30851,214],{"class":141},[36,30853,30854],{"class":145},"You passed",[36,30856,214],{"class":141},[36,30858,86],{"class":50},[14,30860,30861],{},"Conditions often compare:",[157,30863,30864,30866,30869],{},[160,30865,83],{},[160,30867,30868],{},"strings",[160,30870,4313],{},[14,30872,30873,30874,1281,30876,30878,30879,351],{},"They also rely on the Boolean values ",[33,30875,1179],{},[33,30877,1183],{},". If you are new to those values, see ",[347,30880,30881],{"href":1675},"Python booleans explained: True and False",[21,30883,30885],{"id":30884},"basic-structure","Basic structure",[14,30887,30888,30889,30891],{},"A basic ",[33,30890,1192],{}," statement looks like this:",[26,30893,30895],{"className":28,"code":30894,"language":30,"meta":31,"style":31},"if condition:\n    do_something()\n",[33,30896,30897,30905],{"__ignoreMap":31},[36,30898,30899,30901,30903],{"class":38,"line":39},[36,30900,1192],{"class":1718},[36,30902,13090],{"class":42},[36,30904,1730],{"class":50},[36,30906,30907,30910],{"class":38,"line":72},[36,30908,30909],{"class":82},"    do_something",[36,30911,3170],{"class":50},[14,30913,30914],{},"The structure is:",[157,30916,30917,30922,30927,30932],{},[160,30918,30919,30920],{},"Start with the keyword ",[33,30921,1192],{},[160,30923,30924,30925],{},"Write a condition after ",[33,30926,1192],{},[160,30928,30929,30930],{},"End the line with a colon ",[33,30931,822],{},[160,30933,30934,30935],{},"Indent the code that should run when the condition is ",[33,30936,1179],{},[14,30938,30939],{},"Here is a real runnable example:",[26,30941,30943],{"className":28,"code":30942,"language":30,"meta":31,"style":31},"name = \"Maya\"\n\nif name == \"Maya\":\n    print(\"Hello, Maya\")\n",[33,30944,30945,30957,30961,30978],{"__ignoreMap":31},[36,30946,30947,30949,30951,30953,30955],{"class":38,"line":39},[36,30948,1945],{"class":42},[36,30950,47],{"class":46},[36,30952,224],{"class":141},[36,30954,293],{"class":145},[36,30956,1954],{"class":141},[36,30958,30959],{"class":38,"line":72},[36,30960,476],{"emptyLinePlaceholder":475},[36,30962,30963,30965,30968,30970,30972,30974,30976],{"class":38,"line":89},[36,30964,1192],{"class":1718},[36,30966,30967],{"class":42}," name ",[36,30969,1965],{"class":46},[36,30971,224],{"class":141},[36,30973,293],{"class":145},[36,30975,214],{"class":141},[36,30977,1730],{"class":50},[36,30979,30980,30982,30984,30986,30989,30991],{"class":38,"line":496},[36,30981,1735],{"class":75},[36,30983,79],{"class":50},[36,30985,214],{"class":141},[36,30987,30988],{"class":145},"Hello, Maya",[36,30990,214],{"class":141},[36,30992,86],{"class":50},[14,30994,107],{},[26,30996,30998],{"className":28,"code":30997,"language":30,"meta":31,"style":31},"Hello, Maya\n",[33,30999,31000],{"__ignoreMap":31},[36,31001,31002,31004,31006],{"class":38,"line":39},[36,31003,8801],{"class":42},[36,31005,58],{"class":50},[36,31007,9107],{"class":42},[14,31009,31010,31011,31013,31014,351],{},"Indentation is very important in Python. It tells Python which lines belong to the ",[33,31012,1192],{}," block. If you want a fuller explanation, read ",[347,31015,15942],{"href":15941},[21,31017,31019],{"id":31018},"small-real-example","Small real example",[14,31021,31022],{},"Here is a simple example that checks whether a number is positive:",[26,31024,31026],{"className":28,"code":31025,"language":30,"meta":31,"style":31},"number = 5\n\nif number > 0:\n    print(\"The number is positive\")\n",[33,31027,31028,31036,31040,31052],{"__ignoreMap":31},[36,31029,31030,31032,31034],{"class":38,"line":39},[36,31031,8343],{"class":42},[36,31033,47],{"class":46},[36,31035,4927],{"class":54},[36,31037,31038],{"class":38,"line":72},[36,31039,476],{"emptyLinePlaceholder":475},[36,31041,31042,31044,31046,31048,31050],{"class":38,"line":89},[36,31043,1192],{"class":1718},[36,31045,10205],{"class":42},[36,31047,30682],{"class":46},[36,31049,1993],{"class":54},[36,31051,1730],{"class":50},[36,31053,31054,31056,31058,31060,31063,31065],{"class":38,"line":496},[36,31055,1735],{"class":75},[36,31057,79],{"class":50},[36,31059,214],{"class":141},[36,31061,31062],{"class":145},"The number is positive",[36,31064,214],{"class":141},[36,31066,86],{"class":50},[14,31068,107],{},[26,31070,31072],{"className":28,"code":31071,"language":30,"meta":31,"style":31},"The number is positive\n",[33,31073,31074],{"__ignoreMap":31},[36,31075,31076,31078,31080],{"class":38,"line":39},[36,31077,21747],{"class":42},[36,31079,8960],{"class":46},[36,31081,31082],{"class":42}," positive\n",[14,31084,31085],{},"Now look at this version:",[26,31087,31089],{"className":28,"code":31088,"language":30,"meta":31,"style":31},"number = -2\n\nif number > 0:\n    print(\"The number is positive\")\n",[33,31090,31091,31102,31106,31118],{"__ignoreMap":31},[36,31092,31093,31095,31097,31100],{"class":38,"line":39},[36,31094,8343],{"class":42},[36,31096,47],{"class":46},[36,31098,31099],{"class":46}," -",[36,31101,9943],{"class":54},[36,31103,31104],{"class":38,"line":72},[36,31105,476],{"emptyLinePlaceholder":475},[36,31107,31108,31110,31112,31114,31116],{"class":38,"line":89},[36,31109,1192],{"class":1718},[36,31111,10205],{"class":42},[36,31113,30682],{"class":46},[36,31115,1993],{"class":54},[36,31117,1730],{"class":50},[36,31119,31120,31122,31124,31126,31128,31130],{"class":38,"line":496},[36,31121,1735],{"class":75},[36,31123,79],{"class":50},[36,31125,214],{"class":141},[36,31127,31062],{"class":145},[36,31129,214],{"class":141},[36,31131,86],{"class":50},[14,31133,107],{},[26,31135,31137],{"className":28,"code":31136,"language":30,"meta":31,"style":31},"# No output\n",[33,31138,31139],{"__ignoreMap":31},[36,31140,31141],{"class":38,"line":39},[36,31142,31136],{"class":2570},[14,31144,31145,31146,1833,31149,351],{},"Nothing is printed because the condition ",[33,31147,31148],{},"number > 0",[33,31150,1183],{},[14,31152,31153,31154,31156],{},"This shows the main job of an ",[33,31155,1192],{}," statement: run code only when a test passes.",[21,31158,31160],{"id":31159},"why-beginners-use-if-statements","Why beginners use if statements",[14,31162,31163,31164,31166],{},"Beginners use ",[33,31165,1192],{}," statements all the time because they make programs more useful.",[14,31168,257],{},[157,31170,31171,31174,31177,31180],{},[160,31172,31173],{},"checking user input",[160,31175,31176],{},"making a program react differently in different situations",[160,31178,31179],{},"validating data before using it",[160,31181,31182],{},"preventing errors by checking first",[14,31184,197],{},[26,31186,31188],{"className":28,"code":31187,"language":30,"meta":31,"style":31},"username = \"sam\"\n\nif username != \"\":\n    print(\"Welcome,\", username)\n",[33,31189,31190,31204,31208,31222],{"__ignoreMap":31},[36,31191,31192,31195,31197,31199,31202],{"class":38,"line":39},[36,31193,31194],{"class":42},"username ",[36,31196,47],{"class":46},[36,31198,224],{"class":141},[36,31200,31201],{"class":145},"sam",[36,31203,1954],{"class":141},[36,31205,31206],{"class":38,"line":72},[36,31207,476],{"emptyLinePlaceholder":475},[36,31209,31210,31212,31215,31217,31220],{"class":38,"line":89},[36,31211,1192],{"class":1718},[36,31213,31214],{"class":42}," username ",[36,31216,30741],{"class":46},[36,31218,31219],{"class":141}," \"\"",[36,31221,1730],{"class":50},[36,31223,31224,31226,31228,31230,31233,31235,31237,31240],{"class":38,"line":496},[36,31225,1735],{"class":75},[36,31227,79],{"class":50},[36,31229,214],{"class":141},[36,31231,31232],{"class":145},"Welcome,",[36,31234,214],{"class":141},[36,31236,58],{"class":50},[36,31238,31239],{"class":82}," username",[36,31241,86],{"class":50},[14,31243,31244],{},"This kind of check helps make sure a value is usable before the program continues.",[14,31246,31247,31248,351],{},"If you want to learn how Python handles more than one choice, the next step is ",[347,31249,31251],{"href":31250},"\u002Flearn\u002Fpython-if-else-and-elif-explained\u002F","Python if, else, and elif explained",[21,31253,861],{"id":860},[14,31255,31256,31257,31259],{},"This page explains only the basic meaning of an ",[33,31258,1192],{}," statement.",[14,31261,31262],{},"It does not fully cover:",[157,31264,31265,31271,31277],{},[160,31266,31267,9520,31269],{},[33,31268,1192],{},[33,31270,30077],{},[160,31272,31273,31276],{},[33,31274,31275],{},"elif"," for multiple conditions",[160,31278,31279],{},"complex Boolean logic",[14,31281,31282,31283,1281,31285,351],{},"For a full beginner lesson with more branching examples, see ",[347,31284,2889],{"href":1664},[347,31286,31251],{"href":31250},[21,31288,905],{"id":904},[14,31290,31291,31292,1193],{},"Here are some common beginner mistakes with ",[33,31293,1192],{},[1065,31295,31297],{"id":31296},"forgetting-the-colon","Forgetting the colon",[14,31299,13912],{},[26,31301,31303],{"className":28,"code":31302,"language":30,"meta":31,"style":31},"age = 18\n\nif age >= 18\n    print(\"You can vote\")\n",[33,31304,31305,31313,31317,31327],{"__ignoreMap":31},[36,31306,31307,31309,31311],{"class":38,"line":39},[36,31308,1705],{"class":42},[36,31310,47],{"class":46},[36,31312,1634],{"class":54},[36,31314,31315],{"class":38,"line":72},[36,31316,476],{"emptyLinePlaceholder":475},[36,31318,31319,31321,31323,31325],{"class":38,"line":89},[36,31320,1192],{"class":1718},[36,31322,1721],{"class":42},[36,31324,1724],{"class":46},[36,31326,1634],{"class":54},[36,31328,31329,31331,31333,31335,31337,31339],{"class":38,"line":496},[36,31330,1735],{"class":75},[36,31332,79],{"class":50},[36,31334,214],{"class":141},[36,31336,30603],{"class":145},[36,31338,214],{"class":141},[36,31340,86],{"class":50},[14,31342,31343,31344,31346],{},"Python needs a colon at the end of the ",[33,31345,1192],{}," line.",[14,31348,31349,31350,351],{},"If you get an error, see ",[347,31351,31353],{"href":31352},"\u002Ferrors\u002Fsyntaxerror-missing-colon-fix\u002F","SyntaxError: missing colon",[1065,31355,31357],{"id":31356},"not-indenting-the-code-block","Not indenting the code block",[14,31359,31360],{},"This is also wrong:",[26,31362,31364],{"className":28,"code":31363,"language":30,"meta":31,"style":31},"age = 18\n\nif age >= 18:\nprint(\"You can vote\")\n",[33,31365,31366,31374,31378,31390],{"__ignoreMap":31},[36,31367,31368,31370,31372],{"class":38,"line":39},[36,31369,1705],{"class":42},[36,31371,47],{"class":46},[36,31373,1634],{"class":54},[36,31375,31376],{"class":38,"line":72},[36,31377,476],{"emptyLinePlaceholder":475},[36,31379,31380,31382,31384,31386,31388],{"class":38,"line":89},[36,31381,1192],{"class":1718},[36,31383,1721],{"class":42},[36,31385,1724],{"class":46},[36,31387,1727],{"class":54},[36,31389,1730],{"class":50},[36,31391,31392,31394,31396,31398,31400,31402],{"class":38,"line":496},[36,31393,76],{"class":75},[36,31395,79],{"class":50},[36,31397,214],{"class":141},[36,31399,30603],{"class":145},[36,31401,214],{"class":141},[36,31403,86],{"class":50},[14,31405,31406,31407,31409],{},"The line inside the ",[33,31408,1192],{}," block must be indented.",[14,31411,31412,31413,351],{},"If needed, read ",[347,31414,31416],{"href":31415},"\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix\u002F","IndentationError: expected an indented block",[1065,31418,2639,31420,2642,31422],{"id":31419},"using-instead-of",[33,31421,47],{},[33,31423,1965],{},[14,31425,2489,31426,31428],{},[33,31427,1965],{}," when you want to compare values.",[14,31430,2512],{},[26,31432,31434],{"className":28,"code":31433,"language":30,"meta":31,"style":31},"age = 18\n\nif age == 18:\n    print(\"Age is 18\")\n",[33,31435,31436,31444,31448,31460],{"__ignoreMap":31},[36,31437,31438,31440,31442],{"class":38,"line":39},[36,31439,1705],{"class":42},[36,31441,47],{"class":46},[36,31443,1634],{"class":54},[36,31445,31446],{"class":38,"line":72},[36,31447,476],{"emptyLinePlaceholder":475},[36,31449,31450,31452,31454,31456,31458],{"class":38,"line":89},[36,31451,1192],{"class":1718},[36,31453,1721],{"class":42},[36,31455,1965],{"class":46},[36,31457,1727],{"class":54},[36,31459,1730],{"class":50},[36,31461,31462,31464,31466,31468,31471,31473],{"class":38,"line":496},[36,31463,1735],{"class":75},[36,31465,79],{"class":50},[36,31467,214],{"class":141},[36,31469,31470],{"class":145},"Age is 18",[36,31472,214],{"class":141},[36,31474,86],{"class":50},[14,31476,31477,31479,31480,31482],{},[33,31478,47],{}," is for assigning a value. ",[33,31481,1965],{}," is for checking whether two values are equal.",[1065,31484,31486],{"id":31485},"expecting-the-block-to-run-when-the-condition-is-false","Expecting the block to run when the condition is False",[14,31488,3361,31489,31491,31492,351],{},[33,31490,1192],{}," block runs only when the condition is ",[33,31493,1179],{},[14,31495,30645,31496,31498],{},[33,31497,1183],{},", Python skips the block and moves on.",[1065,31500,31502],{"id":31501},"simple-debugging-checks","Simple debugging checks",[14,31504,31505,31506,31508],{},"If your ",[33,31507,1192],{}," statement is not doing what you expect, print the condition result:",[26,31510,31512],{"className":28,"code":31511,"language":30,"meta":31,"style":31},"age = 16\n\nprint(age >= 18)\nprint(type(age >= 18))\n",[33,31513,31514,31522,31526,31540],{"__ignoreMap":31},[36,31515,31516,31518,31520],{"class":38,"line":39},[36,31517,1705],{"class":42},[36,31519,47],{"class":46},[36,31521,2095],{"class":54},[36,31523,31524],{"class":38,"line":72},[36,31525,476],{"emptyLinePlaceholder":475},[36,31527,31528,31530,31532,31534,31536,31538],{"class":38,"line":89},[36,31529,76],{"class":75},[36,31531,79],{"class":50},[36,31533,1705],{"class":82},[36,31535,1724],{"class":46},[36,31537,1727],{"class":54},[36,31539,86],{"class":50},[36,31541,31542,31544,31546,31548,31550,31552,31554,31556],{"class":38,"line":496},[36,31543,76],{"class":75},[36,31545,79],{"class":50},[36,31547,97],{"class":96},[36,31549,79],{"class":50},[36,31551,1705],{"class":82},[36,31553,1724],{"class":46},[36,31555,1727],{"class":54},[36,31557,104],{"class":50},[14,31559,107],{},[26,31561,31563],{"className":28,"code":31562,"language":30,"meta":31,"style":31},"False\n\u003Cclass 'bool'>\n",[33,31564,31565,31569],{"__ignoreMap":31},[36,31566,31567],{"class":38,"line":39},[36,31568,1486],{"class":400},[36,31570,31571,31573,31575,31577,31579,31581],{"class":38,"line":72},[36,31572,134],{"class":46},[36,31574,138],{"class":137},[36,31576,142],{"class":141},[36,31578,1378],{"class":145},[36,31580,149],{"class":141},[36,31582,152],{"class":46},[14,31584,31585],{},"You can also test a condition directly:",[26,31587,31589],{"className":28,"code":31588,"language":30,"meta":31,"style":31},"age = 20\nprint(age >= 18)\n",[33,31590,31591,31599],{"__ignoreMap":31},[36,31592,31593,31595,31597],{"class":38,"line":39},[36,31594,1705],{"class":42},[36,31596,47],{"class":46},[36,31598,2526],{"class":54},[36,31600,31601,31603,31605,31607,31609,31611],{"class":38,"line":72},[36,31602,76],{"class":75},[36,31604,79],{"class":50},[36,31606,1705],{"class":82},[36,31608,1724],{"class":46},[36,31610,1727],{"class":54},[36,31612,86],{"class":50},[14,31614,107],{},[26,31616,31617],{"className":28,"code":1481,"language":30,"meta":31,"style":31},[33,31618,31619],{"__ignoreMap":31},[36,31620,31621],{"class":38,"line":39},[36,31622,1481],{"class":400},[14,31624,31625,31626,31628],{},"These small ",[33,31627,9167],{}," checks can help you see what Python is actually evaluating.",[21,31630,1063],{"id":1062},[1065,31632,31634],{"id":31633},"what-does-an-if-statement-do-in-python","What does an if statement do in Python?",[14,31636,31637,31638,351],{},"It checks a condition and runs code only if that condition is ",[33,31639,1179],{},[1065,31641,31643],{"id":31642},"does-an-if-statement-always-need-else","Does an if statement always need else?",[14,31645,31646,31647,31649],{},"No. An ",[33,31648,1192],{}," statement can be used by itself.",[1065,31651,31653],{"id":31652},"what-kind-of-value-goes-inside-an-if-statement","What kind of value goes inside an if statement?",[14,31655,31656,31657,1180,31659,351],{},"A condition that becomes ",[33,31658,1179],{},[33,31660,1183],{},[1065,31662,31664],{"id":31663},"why-is-indentation-important-in-an-if-statement","Why is indentation important in an if statement?",[14,31666,31667,31668,31670],{},"Indentation tells Python which lines belong to the ",[33,31669,1192],{}," block.",[21,31672,1105],{"id":1104},[157,31674,31675,31679,31683,31687,31691,31695],{},[160,31676,31677],{},[347,31678,2889],{"href":1664},[160,31680,31681],{},[347,31682,31251],{"href":31250},[160,31684,31685],{},[347,31686,30881],{"href":1675},[160,31688,31689],{},[347,31690,15942],{"href":15941},[160,31692,31693],{},[347,31694,31353],{"href":31352},[160,31696,31697],{},[347,31698,31416],{"href":31415},[1137,31700,31701],{},"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 .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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":31,"searchDepth":72,"depth":72,"links":31703},[31704,31705,31706,31707,31708,31709,31710,31711,31719,31725],{"id":23,"depth":72,"text":24},{"id":6100,"depth":72,"text":6101},{"id":30712,"depth":72,"text":30713},{"id":30884,"depth":72,"text":30885},{"id":31018,"depth":72,"text":31019},{"id":31159,"depth":72,"text":31160},{"id":860,"depth":72,"text":861},{"id":904,"depth":72,"text":905,"children":31712},[31713,31714,31715,31717,31718],{"id":31296,"depth":89,"text":31297},{"id":31356,"depth":89,"text":31357},{"id":31419,"depth":89,"text":31716},"Using = instead of ==",{"id":31485,"depth":89,"text":31486},{"id":31501,"depth":89,"text":31502},{"id":1062,"depth":72,"text":1063,"children":31720},[31721,31722,31723,31724],{"id":31633,"depth":89,"text":31634},{"id":31642,"depth":89,"text":31643},{"id":31652,"depth":89,"text":31653},{"id":31663,"depth":89,"text":31664},{"id":1104,"depth":72,"text":1105},"Master what is an if statement in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-an-if-statement-in-python",{"title":30536,"description":31726},"glossary\u002Fwhat-is-an-if-statement-in-python","FpI9R3RxtCbVJQVWmrHz4Vw1WjJaSrE8yDVMW1-1qkk",{"id":31733,"title":31734,"body":31735,"description":33409,"extension":1159,"meta":33410,"navigation":475,"path":33411,"seo":33412,"stem":33413,"__hash__":33414},"content\u002Fglossary\u002Fwhat-is-an-integer-in-python.md","What Is an Integer in Python?",{"type":7,"value":31736,"toc":33385},[31737,31740,31743,31746,31765,31770,31776,31778,31781,31795,31797,31875,31877,31924,31928,31931,31957,31963,31978,31984,31988,31994,32044,32046,32092,32095,32097,32134,32136,32159,32166,32170,32173,32189,32191,32301,32303,32329,32331,32354,32358,32361,32365,32403,32405,32429,32433,32475,32477,32523,32527,32578,32580,32589,32596,32601,32666,32672,32694,32707,32711,32714,32716,32732,32779,32781,32828,32831,32843,32848,32852,32858,32910,32914,32937,32942,33007,33011,33033,33036,33067,33076,33078,33081,33106,33109,33156,33159,33178,33180,33236,33238,33266,33268,33272,33275,33279,33284,33288,33293,33326,33330,33335,33339,33347,33349,33382],[10,31738,31734],{"id":31739},"what-is-an-integer-in-python",[14,31741,31742],{},"An integer in Python is a whole number. It does not have a decimal point.",[14,31744,31745],{},"Examples of integers:",[157,31747,31748,31753,31757,31761],{},[160,31749,31750],{},[33,31751,31752],{},"-3",[160,31754,31755],{},[33,31756,490],{},[160,31758,31759],{},[33,31760,8298],{},[160,31762,31763],{},[33,31764,24575],{},[14,31766,31767,31768,351],{},"In Python, the type name for an integer is ",[33,31769,2466],{},[14,31771,31772,31773,31775],{},"If you are new to Python, integers are one of the most common ",[347,31774,27521],{"href":1203},". You will use them for counting, indexing, loops, and simple math.",[21,31777,7434],{"id":7433},[14,31779,31780],{},"An integer is:",[157,31782,31783,31786,31789],{},[160,31784,31785],{},"A whole number",[160,31787,31788],{},"A number with no decimal point",[160,31790,31791,31792,31794],{},"A value stored as the ",[33,31793,2466],{}," type in Python",[14,31796,694],{},[26,31798,31800],{"className":28,"code":31799,"language":30,"meta":31,"style":31},"a = 10\nb = 0\nc = -25\n\nprint(type(a))\nprint(type(b))\nprint(type(c))\n",[33,31801,31802,31810,31818,31829,31833,31847,31861],{"__ignoreMap":31},[36,31803,31804,31806,31808],{"class":38,"line":39},[36,31805,3316],{"class":42},[36,31807,47],{"class":46},[36,31809,10662],{"class":54},[36,31811,31812,31814,31816],{"class":38,"line":72},[36,31813,25296],{"class":42},[36,31815,47],{"class":46},[36,31817,17587],{"class":54},[36,31819,31820,31823,31825,31827],{"class":38,"line":89},[36,31821,31822],{"class":42},"c ",[36,31824,47],{"class":46},[36,31826,31099],{"class":46},[36,31828,6092],{"class":54},[36,31830,31831],{"class":38,"line":496},[36,31832,476],{"emptyLinePlaceholder":475},[36,31834,31835,31837,31839,31841,31843,31845],{"class":38,"line":501},[36,31836,76],{"class":75},[36,31838,79],{"class":50},[36,31840,97],{"class":96},[36,31842,79],{"class":50},[36,31844,347],{"class":82},[36,31846,104],{"class":50},[36,31848,31849,31851,31853,31855,31857,31859],{"class":38,"line":522},[36,31850,76],{"class":75},[36,31852,79],{"class":50},[36,31854,97],{"class":96},[36,31856,79],{"class":50},[36,31858,721],{"class":82},[36,31860,104],{"class":50},[36,31862,31863,31865,31867,31869,31871,31873],{"class":38,"line":527},[36,31864,76],{"class":75},[36,31866,79],{"class":50},[36,31868,97],{"class":96},[36,31870,79],{"class":50},[36,31872,730],{"class":82},[36,31874,104],{"class":50},[14,31876,107],{},[26,31878,31880],{"className":28,"code":31879,"language":30,"meta":31,"style":31},"\u003Cclass 'int'>\n\u003Cclass 'int'>\n\u003Cclass 'int'>\n",[33,31881,31882,31896,31910],{"__ignoreMap":31},[36,31883,31884,31886,31888,31890,31892,31894],{"class":38,"line":39},[36,31885,134],{"class":46},[36,31887,138],{"class":137},[36,31889,142],{"class":141},[36,31891,2466],{"class":145},[36,31893,149],{"class":141},[36,31895,152],{"class":46},[36,31897,31898,31900,31902,31904,31906,31908],{"class":38,"line":72},[36,31899,134],{"class":46},[36,31901,138],{"class":137},[36,31903,142],{"class":141},[36,31905,2466],{"class":145},[36,31907,149],{"class":141},[36,31909,152],{"class":46},[36,31911,31912,31914,31916,31918,31920,31922],{"class":38,"line":89},[36,31913,134],{"class":46},[36,31915,138],{"class":137},[36,31917,142],{"class":141},[36,31919,2466],{"class":145},[36,31921,149],{"class":141},[36,31923,152],{"class":46},[21,31925,31927],{"id":31926},"what-counts-as-an-integer","What counts as an integer",[14,31929,31930],{},"These are integers:",[157,31932,31933,31943,31952],{},[160,31934,31935,31936,5950,31938,5953,31940],{},"Positive whole numbers like ",[33,31937,55],{},[33,31939,1243],{},[33,31941,31942],{},"100",[160,31944,31945,31946,1281,31949],{},"Negative whole numbers like ",[33,31947,31948],{},"-1",[33,31950,31951],{},"-20",[160,31953,31954,31955],{},"Zero, which is ",[33,31956,490],{},[14,31958,31959,31960,31962],{},"These are ",[331,31961,8193],{}," integers:",[157,31964,31965,31969,31973],{},[160,31966,31967],{},[33,31968,8281],{},[160,31970,31971],{},[33,31972,7491],{},[160,31974,31975],{},[33,31976,31977],{},"-2.75",[14,31979,31980,31981,351],{},"Those values are decimal numbers, which Python usually stores as ",[347,31982,31983],{"href":5648},"floats",[21,31985,31987],{"id":31986},"how-python-shows-integer-type","How Python shows integer type",[14,31989,31990,31991,31993],{},"You can check a value's type with the ",[33,31992,1327],{}," function.",[26,31995,31997],{"className":28,"code":31996,"language":30,"meta":31,"style":31},"print(type(5))\nprint(type(-8))\nprint(type(0))\n",[33,31998,31999,32013,32030],{"__ignoreMap":31},[36,32000,32001,32003,32005,32007,32009,32011],{"class":38,"line":39},[36,32002,76],{"class":75},[36,32004,79],{"class":50},[36,32006,97],{"class":96},[36,32008,79],{"class":50},[36,32010,1243],{"class":54},[36,32012,104],{"class":50},[36,32014,32015,32017,32019,32021,32023,32025,32028],{"class":38,"line":72},[36,32016,76],{"class":75},[36,32018,79],{"class":50},[36,32020,97],{"class":96},[36,32022,79],{"class":50},[36,32024,25103],{"class":46},[36,32026,32027],{"class":54},"8",[36,32029,104],{"class":50},[36,32031,32032,32034,32036,32038,32040,32042],{"class":38,"line":89},[36,32033,76],{"class":75},[36,32035,79],{"class":50},[36,32037,97],{"class":96},[36,32039,79],{"class":50},[36,32041,490],{"class":54},[36,32043,104],{"class":50},[14,32045,107],{},[26,32047,32048],{"className":28,"code":31879,"language":30,"meta":31,"style":31},[33,32049,32050,32064,32078],{"__ignoreMap":31},[36,32051,32052,32054,32056,32058,32060,32062],{"class":38,"line":39},[36,32053,134],{"class":46},[36,32055,138],{"class":137},[36,32057,142],{"class":141},[36,32059,2466],{"class":145},[36,32061,149],{"class":141},[36,32063,152],{"class":46},[36,32065,32066,32068,32070,32072,32074,32076],{"class":38,"line":72},[36,32067,134],{"class":46},[36,32069,138],{"class":137},[36,32071,142],{"class":141},[36,32073,2466],{"class":145},[36,32075,149],{"class":141},[36,32077,152],{"class":46},[36,32079,32080,32082,32084,32086,32088,32090],{"class":38,"line":89},[36,32081,134],{"class":46},[36,32083,138],{"class":137},[36,32085,142],{"class":141},[36,32087,2466],{"class":145},[36,32089,149],{"class":141},[36,32091,152],{"class":46},[14,32093,32094],{},"This is useful when you are not sure what kind of value you have.",[14,32096,1235],{},[26,32098,32100],{"className":28,"code":32099,"language":30,"meta":31,"style":31},"value = 12\nprint(value)\nprint(type(value))\n",[33,32101,32102,32110,32120],{"__ignoreMap":31},[36,32103,32104,32106,32108],{"class":38,"line":39},[36,32105,2664],{"class":42},[36,32107,47],{"class":46},[36,32109,20445],{"class":54},[36,32111,32112,32114,32116,32118],{"class":38,"line":72},[36,32113,76],{"class":75},[36,32115,79],{"class":50},[36,32117,2686],{"class":82},[36,32119,86],{"class":50},[36,32121,32122,32124,32126,32128,32130,32132],{"class":38,"line":89},[36,32123,76],{"class":75},[36,32125,79],{"class":50},[36,32127,97],{"class":96},[36,32129,79],{"class":50},[36,32131,2686],{"class":82},[36,32133,104],{"class":50},[14,32135,107],{},[26,32137,32139],{"className":28,"code":32138,"language":30,"meta":31,"style":31},"12\n\u003Cclass 'int'>\n",[33,32140,32141,32145],{"__ignoreMap":31},[36,32142,32143],{"class":38,"line":39},[36,32144,27121],{"class":54},[36,32146,32147,32149,32151,32153,32155,32157],{"class":38,"line":72},[36,32148,134],{"class":46},[36,32150,138],{"class":137},[36,32152,142],{"class":141},[36,32154,2466],{"class":145},[36,32156,149],{"class":141},[36,32158,152],{"class":46},[14,32160,32161,32162,351],{},"If you want to learn more, see the ",[347,32163,1665,32164,2816],{"href":5265},[33,32165,1327],{},[21,32167,32169],{"id":32168},"common-places-integers-are-used","Common places integers are used",[14,32171,32172],{},"Beginners often use integers in these situations:",[157,32174,32175,32178,32183,32186],{},[160,32176,32177],{},"Counting items",[160,32179,32180,32181],{},"Looping with ",[33,32182,15811],{},[160,32184,32185],{},"Using list indexes",[160,32187,32188],{},"Doing simple math",[14,32190,197],{},[26,32192,32194],{"className":28,"code":32193,"language":30,"meta":31,"style":31},"apples = 4\nprint(apples + 2)\n\nfor i in range(3):\n    print(i)\n\ncolors = [\"red\", \"green\", \"blue\"]\nprint(colors[1])\n",[33,32195,32196,32205,32219,32223,32240,32251,32255,32287],{"__ignoreMap":31},[36,32197,32198,32201,32203],{"class":38,"line":39},[36,32199,32200],{"class":42},"apples ",[36,32202,47],{"class":46},[36,32204,10413],{"class":54},[36,32206,32207,32209,32211,32213,32215,32217],{"class":38,"line":72},[36,32208,76],{"class":75},[36,32210,79],{"class":50},[36,32212,32200],{"class":82},[36,32214,4763],{"class":46},[36,32216,61],{"class":54},[36,32218,86],{"class":50},[36,32220,32221],{"class":38,"line":89},[36,32222,476],{"emptyLinePlaceholder":475},[36,32224,32225,32227,32230,32232,32234,32236,32238],{"class":38,"line":496},[36,32226,9852],{"class":1718},[36,32228,32229],{"class":42}," i ",[36,32231,9911],{"class":1718},[36,32233,10836],{"class":75},[36,32235,79],{"class":50},[36,32237,1247],{"class":54},[36,32239,3054],{"class":50},[36,32241,32242,32244,32246,32249],{"class":38,"line":501},[36,32243,1735],{"class":75},[36,32245,79],{"class":50},[36,32247,32248],{"class":82},"i",[36,32250,86],{"class":50},[36,32252,32253],{"class":38,"line":522},[36,32254,476],{"emptyLinePlaceholder":475},[36,32256,32257,32259,32261,32263,32265,32267,32269,32271,32273,32275,32277,32279,32281,32283,32285],{"class":38,"line":527},[36,32258,207],{"class":42},[36,32260,47],{"class":46},[36,32262,51],{"class":50},[36,32264,214],{"class":141},[36,32266,217],{"class":145},[36,32268,214],{"class":141},[36,32270,58],{"class":50},[36,32272,224],{"class":141},[36,32274,227],{"class":145},[36,32276,214],{"class":141},[36,32278,58],{"class":50},[36,32280,224],{"class":141},[36,32282,236],{"class":145},[36,32284,214],{"class":141},[36,32286,69],{"class":50},[36,32288,32289,32291,32293,32295,32297,32299],{"class":38,"line":3092},[36,32290,76],{"class":75},[36,32292,79],{"class":50},[36,32294,246],{"class":82},[36,32296,117],{"class":50},[36,32298,55],{"class":54},[36,32300,493],{"class":50},[14,32302,107],{},[26,32304,32306],{"className":28,"code":32305,"language":30,"meta":31,"style":31},"6\n0\n1\n2\ngreen\n",[33,32307,32308,32312,32316,32320,32324],{"__ignoreMap":31},[36,32309,32310],{"class":38,"line":39},[36,32311,9435],{"class":54},[36,32313,32314],{"class":38,"line":72},[36,32315,10882],{"class":54},[36,32317,32318],{"class":38,"line":89},[36,32319,9938],{"class":54},[36,32321,32322],{"class":38,"line":496},[36,32323,9943],{"class":54},[36,32325,32326],{"class":38,"line":501},[36,32327,32328],{"class":42},"green\n",[14,32330,18680],{},[157,32332,32333,32339,32345],{},[160,32334,32335,32338],{},[33,32336,32337],{},"apples = 4"," stores an integer",[160,32340,32341,32344],{},[33,32342,32343],{},"range(3)"," uses integers to control the loop",[160,32346,32347,32350,32351,32353],{},[33,32348,32349],{},"colors[1]"," uses the integer ",[33,32352,55],{}," as a list index",[21,32355,32357],{"id":32356},"integer-example-ideas","Integer example ideas",[14,32359,32360],{},"Here are some simple beginner examples.",[1065,32362,32364],{"id":32363},"store-age-in-a-variable","Store age in a variable",[26,32366,32368],{"className":28,"code":32367,"language":30,"meta":31,"style":31},"age = 15\nprint(age)\nprint(type(age))\n",[33,32369,32370,32379,32389],{"__ignoreMap":31},[36,32371,32372,32374,32376],{"class":38,"line":39},[36,32373,1705],{"class":42},[36,32375,47],{"class":46},[36,32377,32378],{"class":54}," 15\n",[36,32380,32381,32383,32385,32387],{"class":38,"line":72},[36,32382,76],{"class":75},[36,32384,79],{"class":50},[36,32386,835],{"class":82},[36,32388,86],{"class":50},[36,32390,32391,32393,32395,32397,32399,32401],{"class":38,"line":89},[36,32392,76],{"class":75},[36,32394,79],{"class":50},[36,32396,97],{"class":96},[36,32398,79],{"class":50},[36,32400,835],{"class":82},[36,32402,104],{"class":50},[14,32404,107],{},[26,32406,32408],{"className":28,"code":32407,"language":30,"meta":31,"style":31},"15\n\u003Cclass 'int'>\n",[33,32409,32410,32415],{"__ignoreMap":31},[36,32411,32412],{"class":38,"line":39},[36,32413,32414],{"class":54},"15\n",[36,32416,32417,32419,32421,32423,32425,32427],{"class":38,"line":72},[36,32418,134],{"class":46},[36,32420,138],{"class":137},[36,32422,142],{"class":141},[36,32424,2466],{"class":145},[36,32426,149],{"class":141},[36,32428,152],{"class":46},[1065,32430,32432],{"id":32431},"count-how-many-times-a-loop-runs","Count how many times a loop runs",[26,32434,32436],{"className":28,"code":32435,"language":30,"meta":31,"style":31},"for count in range(5):\n    print(\"Loop number:\", count)\n",[33,32437,32438,32455],{"__ignoreMap":31},[36,32439,32440,32442,32445,32447,32449,32451,32453],{"class":38,"line":39},[36,32441,9852],{"class":1718},[36,32443,32444],{"class":42}," count ",[36,32446,9911],{"class":1718},[36,32448,10836],{"class":75},[36,32450,79],{"class":50},[36,32452,1243],{"class":54},[36,32454,3054],{"class":50},[36,32456,32457,32459,32461,32463,32466,32468,32470,32473],{"class":38,"line":72},[36,32458,1735],{"class":75},[36,32460,79],{"class":50},[36,32462,214],{"class":141},[36,32464,32465],{"class":145},"Loop number:",[36,32467,214],{"class":141},[36,32469,58],{"class":50},[36,32471,32472],{"class":82}," count",[36,32474,86],{"class":50},[14,32476,107],{},[26,32478,32480],{"className":28,"code":32479,"language":30,"meta":31,"style":31},"Loop number: 0\nLoop number: 1\nLoop number: 2\nLoop number: 3\nLoop number: 4\n",[33,32481,32482,32491,32499,32507,32515],{"__ignoreMap":31},[36,32483,32484,32487,32489],{"class":38,"line":39},[36,32485,32486],{"class":42},"Loop number",[36,32488,822],{"class":50},[36,32490,17587],{"class":54},[36,32492,32493,32495,32497],{"class":38,"line":72},[36,32494,32486],{"class":42},[36,32496,822],{"class":50},[36,32498,9884],{"class":54},[36,32500,32501,32503,32505],{"class":38,"line":89},[36,32502,32486],{"class":42},[36,32504,822],{"class":50},[36,32506,7733],{"class":54},[36,32508,32509,32511,32513],{"class":38,"line":496},[36,32510,32486],{"class":42},[36,32512,822],{"class":50},[36,32514,9897],{"class":54},[36,32516,32517,32519,32521],{"class":38,"line":501},[36,32518,32486],{"class":42},[36,32520,822],{"class":50},[36,32522,10413],{"class":54},[1065,32524,32526],{"id":32525},"access-a-list-item-by-index","Access a list item by index",[26,32528,32530],{"className":28,"code":32529,"language":30,"meta":31,"style":31},"letters = [\"a\", \"b\", \"c\"]\nprint(letters[2])\n",[33,32531,32532,32564],{"__ignoreMap":31},[36,32533,32534,32536,32538,32540,32542,32544,32546,32548,32550,32552,32554,32556,32558,32560,32562],{"class":38,"line":39},[36,32535,22898],{"class":42},[36,32537,47],{"class":46},[36,32539,51],{"class":50},[36,32541,214],{"class":141},[36,32543,347],{"class":145},[36,32545,214],{"class":141},[36,32547,58],{"class":50},[36,32549,224],{"class":141},[36,32551,721],{"class":145},[36,32553,214],{"class":141},[36,32555,58],{"class":50},[36,32557,224],{"class":141},[36,32559,730],{"class":145},[36,32561,214],{"class":141},[36,32563,69],{"class":50},[36,32565,32566,32568,32570,32572,32574,32576],{"class":38,"line":72},[36,32567,76],{"class":75},[36,32569,79],{"class":50},[36,32571,22937],{"class":82},[36,32573,117],{"class":50},[36,32575,1543],{"class":54},[36,32577,493],{"class":50},[14,32579,107],{},[26,32581,32583],{"className":28,"code":32582,"language":30,"meta":31,"style":31},"c\n",[33,32584,32585],{"__ignoreMap":31},[36,32586,32587],{"class":38,"line":39},[36,32588,32582],{"class":42},[1065,32590,32592,32593,32595],{"id":32591},"convert-user-input-with-int-when-needed","Convert user input with ",[33,32594,25347],{}," when needed",[14,32597,3542,32598,32600],{},[33,32599,9395],{}," function returns text, not a number.",[26,32602,32604],{"className":28,"code":32603,"language":30,"meta":31,"style":31},"text = input(\"Enter your age: \")\nage = int(text)\n\nprint(age)\nprint(type(age))\n",[33,32605,32606,32624,32638,32642,32652],{"__ignoreMap":31},[36,32607,32608,32610,32612,32614,32616,32618,32620,32622],{"class":38,"line":39},[36,32609,9407],{"class":42},[36,32611,47],{"class":46},[36,32613,25423],{"class":75},[36,32615,79],{"class":50},[36,32617,214],{"class":141},[36,32619,25430],{"class":145},[36,32621,214],{"class":141},[36,32623,86],{"class":50},[36,32625,32626,32628,32630,32632,32634,32636],{"class":38,"line":72},[36,32627,1705],{"class":42},[36,32629,47],{"class":46},[36,32631,5739],{"class":96},[36,32633,79],{"class":50},[36,32635,4695],{"class":82},[36,32637,86],{"class":50},[36,32639,32640],{"class":38,"line":89},[36,32641,476],{"emptyLinePlaceholder":475},[36,32643,32644,32646,32648,32650],{"class":38,"line":496},[36,32645,76],{"class":75},[36,32647,79],{"class":50},[36,32649,835],{"class":82},[36,32651,86],{"class":50},[36,32653,32654,32656,32658,32660,32662,32664],{"class":38,"line":501},[36,32655,76],{"class":75},[36,32657,79],{"class":50},[36,32659,97],{"class":96},[36,32661,79],{"class":50},[36,32663,835],{"class":82},[36,32665,104],{"class":50},[14,32667,32668,32669,32671],{},"If the user enters ",[33,32670,20418],{},", the output will be:",[26,32673,32674],{"className":28,"code":32138,"language":30,"meta":31,"style":31},[33,32675,32676,32680],{"__ignoreMap":31},[36,32677,32678],{"class":38,"line":39},[36,32679,27121],{"class":54},[36,32681,32682,32684,32686,32688,32690,32692],{"class":38,"line":72},[36,32683,134],{"class":46},[36,32685,138],{"class":137},[36,32687,142],{"class":141},[36,32689,2466],{"class":145},[36,32691,149],{"class":141},[36,32693,152],{"class":46},[14,32695,32696,32697,1281,32702,351],{},"To learn more, see the ",[347,32698,1665,32700,2816],{"href":32699},"\u002Freference\u002Fpython-input-function-explained\u002F",[33,32701,9395],{},[347,32703,1665,32705,2816],{"href":32704},"\u002Freference\u002Fpython-int-function-explained\u002F",[33,32706,25347],{},[21,32708,32710],{"id":32709},"integer-vs-float","Integer vs float",[14,32712,32713],{},"An integer has no decimal part. A float can store decimal numbers.",[14,32715,694],{},[157,32717,32718,32723,32728],{},[160,32719,32720,32722],{},[33,32721,1243],{}," is an integer",[160,32724,32725,32727],{},[33,32726,5403],{}," is a float",[160,32729,32730,32727],{},[33,32731,4871],{},[26,32733,32735],{"className":28,"code":32734,"language":30,"meta":31,"style":31},"print(type(5))\nprint(type(5.0))\nprint(type(3.14))\n",[33,32736,32737,32751,32765],{"__ignoreMap":31},[36,32738,32739,32741,32743,32745,32747,32749],{"class":38,"line":39},[36,32740,76],{"class":75},[36,32742,79],{"class":50},[36,32744,97],{"class":96},[36,32746,79],{"class":50},[36,32748,1243],{"class":54},[36,32750,104],{"class":50},[36,32752,32753,32755,32757,32759,32761,32763],{"class":38,"line":72},[36,32754,76],{"class":75},[36,32756,79],{"class":50},[36,32758,97],{"class":96},[36,32760,79],{"class":50},[36,32762,5403],{"class":54},[36,32764,104],{"class":50},[36,32766,32767,32769,32771,32773,32775,32777],{"class":38,"line":89},[36,32768,76],{"class":75},[36,32770,79],{"class":50},[36,32772,97],{"class":96},[36,32774,79],{"class":50},[36,32776,4871],{"class":54},[36,32778,104],{"class":50},[14,32780,107],{},[26,32782,32784],{"className":28,"code":32783,"language":30,"meta":31,"style":31},"\u003Cclass 'int'>\n\u003Cclass 'float'>\n\u003Cclass 'float'>\n",[33,32785,32786,32800,32814],{"__ignoreMap":31},[36,32787,32788,32790,32792,32794,32796,32798],{"class":38,"line":39},[36,32789,134],{"class":46},[36,32791,138],{"class":137},[36,32793,142],{"class":141},[36,32795,2466],{"class":145},[36,32797,149],{"class":141},[36,32799,152],{"class":46},[36,32801,32802,32804,32806,32808,32810,32812],{"class":38,"line":72},[36,32803,134],{"class":46},[36,32805,138],{"class":137},[36,32807,142],{"class":141},[36,32809,4740],{"class":145},[36,32811,149],{"class":141},[36,32813,152],{"class":46},[36,32815,32816,32818,32820,32822,32824,32826],{"class":38,"line":89},[36,32817,134],{"class":46},[36,32819,138],{"class":137},[36,32821,142],{"class":141},[36,32823,4740],{"class":145},[36,32825,149],{"class":141},[36,32827,152],{"class":46},[14,32829,32830],{},"Use the right type for the data you need:",[157,32832,32833,32838],{},[160,32834,2489,32835,32837],{},[33,32836,2466],{}," for counting whole things",[160,32839,2489,32840,32842],{},[33,32841,4740],{}," for values with decimals",[14,32844,32845,32846,351],{},"For a bigger overview, see ",[347,32847,7808],{"href":7807},[21,32849,32851],{"id":32850},"related-beginner-confusion","Related beginner confusion",[14,32853,32854,32855,32857],{},"A very common mistake is thinking that ",[33,32856,9395],{}," gives you an integer automatically. It does not. It gives you a string.",[26,32859,32861],{"className":28,"code":32860,"language":30,"meta":31,"style":31},"value = input(\"Enter a number: \")\n\nprint(value)\nprint(type(value))\n",[33,32862,32863,32882,32886,32896],{"__ignoreMap":31},[36,32864,32865,32867,32869,32871,32873,32875,32878,32880],{"class":38,"line":39},[36,32866,2664],{"class":42},[36,32868,47],{"class":46},[36,32870,25423],{"class":75},[36,32872,79],{"class":50},[36,32874,214],{"class":141},[36,32876,32877],{"class":145},"Enter a number: ",[36,32879,214],{"class":141},[36,32881,86],{"class":50},[36,32883,32884],{"class":38,"line":72},[36,32885,476],{"emptyLinePlaceholder":475},[36,32887,32888,32890,32892,32894],{"class":38,"line":89},[36,32889,76],{"class":75},[36,32891,79],{"class":50},[36,32893,2686],{"class":82},[36,32895,86],{"class":50},[36,32897,32898,32900,32902,32904,32906,32908],{"class":38,"line":496},[36,32899,76],{"class":75},[36,32901,79],{"class":50},[36,32903,97],{"class":96},[36,32905,79],{"class":50},[36,32907,2686],{"class":82},[36,32909,104],{"class":50},[14,32911,32668,32912,32671],{},[33,32913,20418],{},[26,32915,32917],{"className":28,"code":32916,"language":30,"meta":31,"style":31},"12\n\u003Cclass 'str'>\n",[33,32918,32919,32923],{"__ignoreMap":31},[36,32920,32921],{"class":38,"line":39},[36,32922,27121],{"class":54},[36,32924,32925,32927,32929,32931,32933,32935],{"class":38,"line":72},[36,32926,134],{"class":46},[36,32928,138],{"class":137},[36,32930,142],{"class":141},[36,32932,2360],{"class":145},[36,32934,149],{"class":141},[36,32936,152],{"class":46},[14,32938,32939,32940,822],{},"To turn numeric text into an integer, use ",[33,32941,25347],{},[26,32943,32945],{"className":28,"code":32944,"language":30,"meta":31,"style":31},"value = input(\"Enter a number: \")\nnumber = int(value)\n\nprint(number)\nprint(type(number))\n",[33,32946,32947,32965,32979,32983,32993],{"__ignoreMap":31},[36,32948,32949,32951,32953,32955,32957,32959,32961,32963],{"class":38,"line":39},[36,32950,2664],{"class":42},[36,32952,47],{"class":46},[36,32954,25423],{"class":75},[36,32956,79],{"class":50},[36,32958,214],{"class":141},[36,32960,32877],{"class":145},[36,32962,214],{"class":141},[36,32964,86],{"class":50},[36,32966,32967,32969,32971,32973,32975,32977],{"class":38,"line":72},[36,32968,8343],{"class":42},[36,32970,47],{"class":46},[36,32972,5739],{"class":96},[36,32974,79],{"class":50},[36,32976,2686],{"class":82},[36,32978,86],{"class":50},[36,32980,32981],{"class":38,"line":89},[36,32982,476],{"emptyLinePlaceholder":475},[36,32984,32985,32987,32989,32991],{"class":38,"line":496},[36,32986,76],{"class":75},[36,32988,79],{"class":50},[36,32990,8368],{"class":82},[36,32992,86],{"class":50},[36,32994,32995,32997,32999,33001,33003,33005],{"class":38,"line":501},[36,32996,76],{"class":75},[36,32998,79],{"class":50},[36,33000,97],{"class":96},[36,33002,79],{"class":50},[36,33004,8368],{"class":82},[36,33006,104],{"class":50},[14,33008,32668,33009,32671],{},[33,33010,20418],{},[26,33012,33013],{"className":28,"code":32138,"language":30,"meta":31,"style":31},[33,33014,33015,33019],{"__ignoreMap":31},[36,33016,33017],{"class":38,"line":39},[36,33018,27121],{"class":54},[36,33020,33021,33023,33025,33027,33029,33031],{"class":38,"line":72},[36,33022,134],{"class":46},[36,33024,138],{"class":137},[36,33026,142],{"class":141},[36,33028,2466],{"class":145},[36,33030,149],{"class":141},[36,33032,152],{"class":46},[14,33034,33035],{},"But invalid text cannot be converted:",[26,33037,33039],{"className":28,"code":33038,"language":30,"meta":31,"style":31},"value = \"hello\"\nnumber = int(value)\n",[33,33040,33041,33053],{"__ignoreMap":31},[36,33042,33043,33045,33047,33049,33051],{"class":38,"line":39},[36,33044,2664],{"class":42},[36,33046,47],{"class":46},[36,33048,224],{"class":141},[36,33050,2671],{"class":145},[36,33052,1954],{"class":141},[36,33054,33055,33057,33059,33061,33063,33065],{"class":38,"line":72},[36,33056,8343],{"class":42},[36,33058,47],{"class":46},[36,33060,5739],{"class":96},[36,33062,79],{"class":50},[36,33064,2686],{"class":82},[36,33066,86],{"class":50},[14,33068,20672,33069,33071,33072,351],{},[33,33070,4854],{}," is not a valid integer. If you run into that problem, see ",[347,33073,33075],{"href":33074},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","ValueError: invalid literal for int() with base 10",[21,33077,905],{"id":904},[14,33079,33080],{},"Beginners often get confused about integers in these ways:",[157,33082,33083,33086,33091,33094,33097],{},[160,33084,33085],{},"Thinking numbers with decimals are integers",[160,33087,8494,33088,33090],{},[33,33089,9395],{}," returns an integer automatically",[160,33092,33093],{},"Mixing strings and integers in calculations",[160,33095,33096],{},"Using a float where Python expects an integer",[160,33098,33099,33100,33102,33103,33105],{},"Confusing the ",[33,33101,2466],{}," type with the ",[33,33104,25347],{}," conversion function",[14,33107,33108],{},"These quick checks can help:",[26,33110,33112],{"className":28,"code":33111,"language":30,"meta":31,"style":31},"print(value)\nprint(type(value))\nprint(isinstance(value, int))\n",[33,33113,33114,33124,33138],{"__ignoreMap":31},[36,33115,33116,33118,33120,33122],{"class":38,"line":39},[36,33117,76],{"class":75},[36,33119,79],{"class":50},[36,33121,2686],{"class":82},[36,33123,86],{"class":50},[36,33125,33126,33128,33130,33132,33134,33136],{"class":38,"line":72},[36,33127,76],{"class":75},[36,33129,79],{"class":50},[36,33131,97],{"class":96},[36,33133,79],{"class":50},[36,33135,2686],{"class":82},[36,33137,104],{"class":50},[36,33139,33140,33142,33144,33146,33148,33150,33152,33154],{"class":38,"line":89},[36,33141,76],{"class":75},[36,33143,79],{"class":50},[36,33145,5730],{"class":75},[36,33147,79],{"class":50},[36,33149,2686],{"class":82},[36,33151,58],{"class":50},[36,33153,5739],{"class":96},[36,33155,104],{"class":50},[14,33157,33158],{},"What they do:",[157,33160,33161,33167,33172],{},[160,33162,33163,33166],{},[33,33164,33165],{},"print(value)"," shows the current value",[160,33168,33169,33171],{},[33,33170,5804],{}," shows its type",[160,33173,33174,33177],{},[33,33175,33176],{},"print(isinstance(value, int))"," tells you whether it is an integer",[14,33179,197],{},[26,33181,33183],{"className":28,"code":33182,"language":30,"meta":31,"style":31},"value = 7\nprint(value)\nprint(type(value))\nprint(isinstance(value, int))\n",[33,33184,33185,33194,33204,33218],{"__ignoreMap":31},[36,33186,33187,33189,33191],{"class":38,"line":39},[36,33188,2664],{"class":42},[36,33190,47],{"class":46},[36,33192,33193],{"class":54}," 7\n",[36,33195,33196,33198,33200,33202],{"class":38,"line":72},[36,33197,76],{"class":75},[36,33199,79],{"class":50},[36,33201,2686],{"class":82},[36,33203,86],{"class":50},[36,33205,33206,33208,33210,33212,33214,33216],{"class":38,"line":89},[36,33207,76],{"class":75},[36,33209,79],{"class":50},[36,33211,97],{"class":96},[36,33213,79],{"class":50},[36,33215,2686],{"class":82},[36,33217,104],{"class":50},[36,33219,33220,33222,33224,33226,33228,33230,33232,33234],{"class":38,"line":496},[36,33221,76],{"class":75},[36,33223,79],{"class":50},[36,33225,5730],{"class":75},[36,33227,79],{"class":50},[36,33229,2686],{"class":82},[36,33231,58],{"class":50},[36,33233,5739],{"class":96},[36,33235,104],{"class":50},[14,33237,107],{},[26,33239,33241],{"className":28,"code":33240,"language":30,"meta":31,"style":31},"7\n\u003Cclass 'int'>\nTrue\n",[33,33242,33243,33248,33262],{"__ignoreMap":31},[36,33244,33245],{"class":38,"line":39},[36,33246,33247],{"class":54},"7\n",[36,33249,33250,33252,33254,33256,33258,33260],{"class":38,"line":72},[36,33251,134],{"class":46},[36,33253,138],{"class":137},[36,33255,142],{"class":141},[36,33257,2466],{"class":145},[36,33259,149],{"class":141},[36,33261,152],{"class":46},[36,33263,33264],{"class":38,"line":89},[36,33265,1481],{"class":400},[21,33267,1063],{"id":1062},[1065,33269,33271],{"id":33270},"is-0-an-integer-in-python","Is 0 an integer in Python?",[14,33273,33274],{},"Yes. Zero is an integer.",[1065,33276,33278],{"id":33277},"is-50-an-integer","Is 5.0 an integer?",[14,33280,2836,33281,33283],{},[33,33282,5403],{}," is a float because it has a decimal form.",[1065,33285,33287],{"id":33286},"how-do-i-convert-text-to-an-integer-in-python","How do I convert text to an integer in Python?",[14,33289,2489,33290,33292],{},[33,33291,25347],{},", such as:",[26,33294,33296],{"className":28,"code":33295,"language":30,"meta":31,"style":31},"number = int(\"5\")\nprint(number)\n",[33,33297,33298,33316],{"__ignoreMap":31},[36,33299,33300,33302,33304,33306,33308,33310,33312,33314],{"class":38,"line":39},[36,33301,8343],{"class":42},[36,33303,47],{"class":46},[36,33305,5739],{"class":96},[36,33307,79],{"class":50},[36,33309,214],{"class":141},[36,33311,1243],{"class":145},[36,33313,214],{"class":141},[36,33315,86],{"class":50},[36,33317,33318,33320,33322,33324],{"class":38,"line":72},[36,33319,76],{"class":75},[36,33321,79],{"class":50},[36,33323,8368],{"class":82},[36,33325,86],{"class":50},[1065,33327,33329],{"id":33328},"what-is-the-python-type-for-integers","What is the Python type for integers?",[14,33331,33332,33333,351],{},"The type name is ",[33,33334,2466],{},[1065,33336,33338],{"id":33337},"can-integers-be-negative","Can integers be negative?",[14,33340,33341,33342,1281,33344,33346],{},"Yes. Values like ",[33,33343,31948],{},[33,33345,31951],{}," are integers.",[21,33348,1105],{"id":1104},[157,33350,33351,33355,33360,33366,33370,33376],{},[160,33352,33353],{},[347,33354,2906],{"href":1203},[160,33356,33357],{},[347,33358,33359],{"href":5648},"What is a float in Python?",[160,33361,33362],{},[347,33363,1665,33364,2816],{"href":32704},[33,33365,25347],{},[160,33367,33368],{},[347,33369,25647],{"href":25354},[160,33371,33372],{},[347,33373,1665,33374,2816],{"href":32699},[33,33375,9395],{},[160,33377,33378],{},[347,33379,1665,33380,2816],{"href":5265},[33,33381,1327],{},[1137,33383,33384],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":33386},[33387,33388,33389,33390,33391,33398,33399,33400,33401,33408],{"id":7433,"depth":72,"text":7434},{"id":31926,"depth":72,"text":31927},{"id":31986,"depth":72,"text":31987},{"id":32168,"depth":72,"text":32169},{"id":32356,"depth":72,"text":32357,"children":33392},[33393,33394,33395,33396],{"id":32363,"depth":89,"text":32364},{"id":32431,"depth":89,"text":32432},{"id":32525,"depth":89,"text":32526},{"id":32591,"depth":89,"text":33397},"Convert user input with int() when needed",{"id":32709,"depth":72,"text":32710},{"id":32850,"depth":72,"text":32851},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":33402},[33403,33404,33405,33406,33407],{"id":33270,"depth":89,"text":33271},{"id":33277,"depth":89,"text":33278},{"id":33286,"depth":89,"text":33287},{"id":33328,"depth":89,"text":33329},{"id":33337,"depth":89,"text":33338},{"id":1104,"depth":72,"text":1105},"Master what is an integer in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-an-integer-in-python",{"title":31734,"description":33409},"glossary\u002Fwhat-is-an-integer-in-python","IxH4rn1I4sIU1dwvvOfpB5EOb2f0e5DJCbBUjHsPCwQ",{"id":33416,"title":33417,"body":33418,"description":35278,"extension":1159,"meta":35279,"navigation":475,"path":13057,"seo":35280,"stem":35281,"__hash__":35282},"content\u002Fglossary\u002Fwhat-is-an-iterable-in-python.md","What Is an Iterable in Python?",{"type":7,"value":33419,"toc":35240},[33420,33423,33428,33439,33448,33450,33499,33503,33520,33523,33525,33528,33541,33544,33563,33565,33611,33615,33633,33640,33644,33647,33650,33683,33685,33742,33746,33783,33786,33802,33807,33811,33814,33816,33868,33870,33921,33923,33968,33970,34021,34023,34029,34098,34102,34116,34125,34129,34136,34167,34171,34196,34200,34203,34263,34267,34273,34275,34296,34299,34339,34345,34353,34357,34360,34372,34378,34465,34469,34485,34487,34499,34508,34512,34515,34520,34525,34589,34594,34598,34603,34654,34658,34670,34674,34677,34737,34741,34754,34758,34761,34851,34855,34884,34888,34891,34897,34940,34943,34949,35002,35009,35014,35016,35043,35046,35048,35050,35072,35075,35133,35144,35147,35163,35165,35169,35175,35179,35186,35190,35193,35197,35202,35206,35209,35211,35237],[10,33421,33417],{"id":33422},"what-is-an-iterable-in-python",[14,33424,3361,33425,33427],{},[331,33426,10621],{}," is an object that Python can go through one value at a time.",[14,33429,33430,33431,33433,33434,1281,33436,33438],{},"You will see iterables all the time in Python. They are used in ",[33,33432,9852],{}," loops, in built-in functions like ",[33,33435,29079],{},[33,33437,11935],{},", and in many common tasks such as reading files or unpacking values.",[14,33440,33441,33442,33444,33445,351],{},"A big reason beginners should learn this word is that many errors and examples make more sense once you know what an iterable is. It also helps you understand the difference between an ",[331,33443,10621],{}," and an ",[331,33446,33447],{},"iterator",[21,33449,24],{"id":23},[26,33451,33453],{"className":28,"code":33452,"language":30,"meta":31,"style":31},"items = [10, 20, 30]\nfor item in items:\n    print(item)\n",[33,33454,33455,33476,33489],{"__ignoreMap":31},[36,33456,33457,33460,33462,33464,33466,33468,33470,33472,33474],{"class":38,"line":39},[36,33458,33459],{"class":42},"items ",[36,33461,47],{"class":46},[36,33463,51],{"class":50},[36,33465,1560],{"class":54},[36,33467,58],{"class":50},[36,33469,1628],{"class":54},[36,33471,58],{"class":50},[36,33473,26390],{"class":54},[36,33475,69],{"class":50},[36,33477,33478,33480,33482,33484,33487],{"class":38,"line":72},[36,33479,9852],{"class":1718},[36,33481,10439],{"class":42},[36,33483,9911],{"class":1718},[36,33485,33486],{"class":42}," items",[36,33488,1730],{"class":50},[36,33490,33491,33493,33495,33497],{"class":38,"line":89},[36,33492,1735],{"class":75},[36,33494,79],{"class":50},[36,33496,10455],{"class":82},[36,33498,86],{"class":50},[14,33500,33501],{},[331,33502,107],{},[26,33504,33506],{"className":28,"code":33505,"language":30,"meta":31,"style":31},"10\n20\n30\n",[33,33507,33508,33512,33516],{"__ignoreMap":31},[36,33509,33510],{"class":38,"line":39},[36,33511,10792],{"class":54},[36,33513,33514],{"class":38,"line":72},[36,33515,21635],{"class":54},[36,33517,33518],{"class":38,"line":89},[36,33519,29181],{"class":54},[14,33521,33522],{},"A list is iterable because Python can go through its values one by one.",[21,33524,6101],{"id":6100},[14,33526,33527],{},"An iterable is:",[157,33529,33530,33533,33536],{},[160,33531,33532],{},"An object Python can loop through",[160,33534,33535],{},"Something that provides values one at a time",[160,33537,33538,33539,1781],{},"Often used with ",[33,33540,9852],{},[14,33542,33543],{},"Common iterable objects include:",[157,33545,33546,33549,33551,33553,33555,33558],{},[160,33547,33548],{},"Strings",[160,33550,22630],{},[160,33552,22636],{},[160,33554,22642],{},[160,33556,33557],{},"Dictionaries",[160,33559,33560,33562],{},[33,33561,12962],{}," objects",[14,33564,1235],{},[26,33566,33568],{"className":28,"code":33567,"language":30,"meta":31,"style":31},"word = \"cat\"\n\nfor letter in word:\n    print(letter)\n",[33,33569,33570,33582,33586,33600],{"__ignoreMap":31},[36,33571,33572,33574,33576,33578,33580],{"class":38,"line":39},[36,33573,16709],{"class":42},[36,33575,47],{"class":46},[36,33577,224],{"class":141},[36,33579,13445],{"class":145},[36,33581,1954],{"class":141},[36,33583,33584],{"class":38,"line":72},[36,33585,476],{"emptyLinePlaceholder":475},[36,33587,33588,33590,33593,33595,33598],{"class":38,"line":89},[36,33589,9852],{"class":1718},[36,33591,33592],{"class":42}," letter ",[36,33594,9911],{"class":1718},[36,33596,33597],{"class":42}," word",[36,33599,1730],{"class":50},[36,33601,33602,33604,33606,33609],{"class":38,"line":496},[36,33603,1735],{"class":75},[36,33605,79],{"class":50},[36,33607,33608],{"class":82},"letter",[36,33610,86],{"class":50},[14,33612,33613],{},[331,33614,107],{},[26,33616,33618],{"className":28,"code":33617,"language":30,"meta":31,"style":31},"c\na\nt\n",[33,33619,33620,33624,33628],{"__ignoreMap":31},[36,33621,33622],{"class":38,"line":39},[36,33623,32582],{"class":42},[36,33625,33626],{"class":38,"line":72},[36,33627,26350],{"class":42},[36,33629,33630],{"class":38,"line":89},[36,33631,33632],{"class":42},"t\n",[14,33634,33635,33636,33639],{},"Here, the string ",[33,33637,33638],{},"\"cat\""," is iterable because Python can loop through each character.",[21,33641,33643],{"id":33642},"why-beginners-need-to-know-this","Why beginners need to know this",[14,33645,33646],{},"Many parts of Python expect an iterable.",[14,33648,33649],{},"For example, these built-in functions work with iterables:",[157,33651,33652,33656,33660,33665,33669,33673,33678],{},[160,33653,33654],{},[33,33655,29079],{},[160,33657,33658],{},[33,33659,10487],{},[160,33661,33662],{},[33,33663,33664],{},"tuple()",[160,33666,33667],{},[33,33668,22441],{},[160,33670,33671],{},[33,33672,11935],{},[160,33674,33675],{},[33,33676,33677],{},"any()",[160,33679,33680],{},[33,33681,33682],{},"all()",[14,33684,197],{},[26,33686,33688],{"className":28,"code":33687,"language":30,"meta":31,"style":31},"numbers = [4, 5, 6]\nprint(sum(numbers))\nprint(list(\"abc\"))\n",[33,33689,33690,33710,33724],{"__ignoreMap":31},[36,33691,33692,33694,33696,33698,33700,33702,33704,33706,33708],{"class":38,"line":39},[36,33693,43],{"class":42},[36,33695,47],{"class":46},[36,33697,51],{"class":50},[36,33699,11622],{"class":54},[36,33701,58],{"class":50},[36,33703,5043],{"class":54},[36,33705,58],{"class":50},[36,33707,12225],{"class":54},[36,33709,69],{"class":50},[36,33711,33712,33714,33716,33718,33720,33722],{"class":38,"line":72},[36,33713,76],{"class":75},[36,33715,79],{"class":50},[36,33717,29039],{"class":75},[36,33719,79],{"class":50},[36,33721,83],{"class":82},[36,33723,104],{"class":50},[36,33725,33726,33728,33730,33732,33734,33736,33738,33740],{"class":38,"line":89},[36,33727,76],{"class":75},[36,33729,79],{"class":50},[36,33731,146],{"class":96},[36,33733,79],{"class":50},[36,33735,214],{"class":141},[36,33737,29643],{"class":145},[36,33739,214],{"class":141},[36,33741,104],{"class":50},[14,33743,33744],{},[331,33745,107],{},[26,33747,33749],{"className":28,"code":33748,"language":30,"meta":31,"style":31},"15\n['a', 'b', 'c']\n",[33,33750,33751,33755],{"__ignoreMap":31},[36,33752,33753],{"class":38,"line":39},[36,33754,32414],{"class":54},[36,33756,33757,33759,33761,33763,33765,33767,33769,33771,33773,33775,33777,33779,33781],{"class":38,"line":72},[36,33758,117],{"class":50},[36,33760,149],{"class":141},[36,33762,347],{"class":145},[36,33764,149],{"class":141},[36,33766,58],{"class":50},[36,33768,142],{"class":141},[36,33770,721],{"class":145},[36,33772,149],{"class":141},[36,33774,58],{"class":50},[36,33776,142],{"class":141},[36,33778,730],{"class":145},[36,33780,149],{"class":141},[36,33782,69],{"class":50},[14,33784,33785],{},"Understanding iterables also helps with error messages such as:",[157,33787,33788,33793,33798],{},[160,33789,33790],{},[33,33791,33792],{},"TypeError: 'int' object is not iterable",[160,33794,33795],{},[33,33796,33797],{},"TypeError: 'bool' object is not iterable",[160,33799,33800],{},[33,33801,21489],{},[14,33803,33804,33805,351],{},"If you are still getting comfortable with loops, see ",[347,33806,13398],{"href":15826},[21,33808,33810],{"id":33809},"common-examples-of-iterable-objects","Common examples of iterable objects",[14,33812,33813],{},"Here are some common iterable types in Python.",[1065,33815,672],{"id":146},[26,33817,33819],{"className":28,"code":33818,"language":30,"meta":31,"style":31},"values = [1, 2, 3]\n\nfor value in values:\n    print(value)\n",[33,33820,33821,33841,33845,33858],{"__ignoreMap":31},[36,33822,33823,33825,33827,33829,33831,33833,33835,33837,33839],{"class":38,"line":39},[36,33824,10759],{"class":42},[36,33826,47],{"class":46},[36,33828,51],{"class":50},[36,33830,55],{"class":54},[36,33832,58],{"class":50},[36,33834,61],{"class":54},[36,33836,58],{"class":50},[36,33838,66],{"class":54},[36,33840,69],{"class":50},[36,33842,33843],{"class":38,"line":72},[36,33844,476],{"emptyLinePlaceholder":475},[36,33846,33847,33849,33851,33853,33856],{"class":38,"line":89},[36,33848,9852],{"class":1718},[36,33850,10853],{"class":42},[36,33852,9911],{"class":1718},[36,33854,33855],{"class":42}," values",[36,33857,1730],{"class":50},[36,33859,33860,33862,33864,33866],{"class":38,"line":496},[36,33861,1735],{"class":75},[36,33863,79],{"class":50},[36,33865,2686],{"class":82},[36,33867,86],{"class":50},[1065,33869,678],{"id":5615},[26,33871,33873],{"className":28,"code":33872,"language":30,"meta":31,"style":31},"values = (1, 2, 3)\n\nfor value in values:\n    print(value)\n",[33,33874,33875,33895,33899,33911],{"__ignoreMap":31},[36,33876,33877,33879,33881,33883,33885,33887,33889,33891,33893],{"class":38,"line":39},[36,33878,10759],{"class":42},[36,33880,47],{"class":46},[36,33882,744],{"class":50},[36,33884,55],{"class":54},[36,33886,58],{"class":50},[36,33888,61],{"class":54},[36,33890,58],{"class":50},[36,33892,66],{"class":54},[36,33894,86],{"class":50},[36,33896,33897],{"class":38,"line":72},[36,33898,476],{"emptyLinePlaceholder":475},[36,33900,33901,33903,33905,33907,33909],{"class":38,"line":89},[36,33902,9852],{"class":1718},[36,33904,10853],{"class":42},[36,33906,9911],{"class":1718},[36,33908,33855],{"class":42},[36,33910,1730],{"class":50},[36,33912,33913,33915,33917,33919],{"class":38,"line":496},[36,33914,1735],{"class":75},[36,33916,79],{"class":50},[36,33918,2686],{"class":82},[36,33920,86],{"class":50},[1065,33922,4850],{"id":4728},[26,33924,33926],{"className":28,"code":33925,"language":30,"meta":31,"style":31},"text = \"abc\"\n\nfor char in text:\n    print(char)\n",[33,33927,33928,33940,33944,33957],{"__ignoreMap":31},[36,33929,33930,33932,33934,33936,33938],{"class":38,"line":39},[36,33931,9407],{"class":42},[36,33933,47],{"class":46},[36,33935,224],{"class":141},[36,33937,29643],{"class":145},[36,33939,1954],{"class":141},[36,33941,33942],{"class":38,"line":72},[36,33943,476],{"emptyLinePlaceholder":475},[36,33945,33946,33948,33951,33953,33955],{"class":38,"line":89},[36,33947,9852],{"class":1718},[36,33949,33950],{"class":42}," char ",[36,33952,9911],{"class":1718},[36,33954,16440],{"class":42},[36,33956,1730],{"class":50},[36,33958,33959,33961,33963,33966],{"class":38,"line":496},[36,33960,1735],{"class":75},[36,33962,79],{"class":50},[36,33964,33965],{"class":82},"char",[36,33967,86],{"class":50},[1065,33969,684],{"id":23063},[26,33971,33973],{"className":28,"code":33972,"language":30,"meta":31,"style":31},"values = {1, 2, 3}\n\nfor value in values:\n    print(value)\n",[33,33974,33975,33995,33999,34011],{"__ignoreMap":31},[36,33976,33977,33979,33981,33983,33985,33987,33989,33991,33993],{"class":38,"line":39},[36,33978,10759],{"class":42},[36,33980,47],{"class":46},[36,33982,778],{"class":50},[36,33984,55],{"class":54},[36,33986,58],{"class":50},[36,33988,61],{"class":54},[36,33990,58],{"class":50},[36,33992,66],{"class":54},[36,33994,803],{"class":50},[36,33996,33997],{"class":38,"line":72},[36,33998,476],{"emptyLinePlaceholder":475},[36,34000,34001,34003,34005,34007,34009],{"class":38,"line":89},[36,34002,9852],{"class":1718},[36,34004,10853],{"class":42},[36,34006,9911],{"class":1718},[36,34008,33855],{"class":42},[36,34010,1730],{"class":50},[36,34012,34013,34015,34017,34019],{"class":38,"line":496},[36,34014,1735],{"class":75},[36,34016,79],{"class":50},[36,34018,2686],{"class":82},[36,34020,86],{"class":50},[1065,34022,690],{"id":6770},[14,34024,34025,34026,351],{},"A dictionary is iterable, but it loops over ",[331,34027,34028],{},"keys by default",[26,34030,34032],{"className":28,"code":34031,"language":30,"meta":31,"style":31},"student = {\"name\": \"Ana\", \"age\": 12}\n\nfor key in student:\n    print(key)\n",[33,34033,34034,34071,34075,34088],{"__ignoreMap":31},[36,34035,34036,34039,34041,34043,34045,34047,34049,34051,34053,34055,34057,34059,34061,34063,34065,34067,34069],{"class":38,"line":39},[36,34037,34038],{"class":42},"student ",[36,34040,47],{"class":46},[36,34042,778],{"class":50},[36,34044,214],{"class":141},[36,34046,817],{"class":145},[36,34048,214],{"class":141},[36,34050,822],{"class":50},[36,34052,224],{"class":141},[36,34054,448],{"class":145},[36,34056,214],{"class":141},[36,34058,58],{"class":50},[36,34060,224],{"class":141},[36,34062,835],{"class":145},[36,34064,214],{"class":141},[36,34066,822],{"class":50},[36,34068,842],{"class":54},[36,34070,803],{"class":50},[36,34072,34073],{"class":38,"line":72},[36,34074,476],{"emptyLinePlaceholder":475},[36,34076,34077,34079,34082,34084,34086],{"class":38,"line":89},[36,34078,9852],{"class":1718},[36,34080,34081],{"class":42}," key ",[36,34083,9911],{"class":1718},[36,34085,12029],{"class":42},[36,34087,1730],{"class":50},[36,34089,34090,34092,34094,34096],{"class":38,"line":496},[36,34091,1735],{"class":75},[36,34093,79],{"class":50},[36,34095,6114],{"class":82},[36,34097,86],{"class":50},[14,34099,34100],{},[331,34101,107],{},[26,34103,34105],{"className":28,"code":34104,"language":30,"meta":31,"style":31},"name\nage\n",[33,34106,34107,34111],{"__ignoreMap":31},[36,34108,34109],{"class":38,"line":39},[36,34110,27847],{"class":42},[36,34112,34113],{"class":38,"line":72},[36,34114,34115],{"class":42},"age\n",[14,34117,34118,34119,1180,34122,351],{},"If you want the values or both keys and values, you would use ",[33,34120,34121],{},".values()",[33,34123,34124],{},".items()",[1065,34126,34128],{"id":34127},"range-object","Range object",[14,34130,3542,34131,34135],{},[347,34132,34133,8246],{"href":15978},[33,34134,15811],{}," creates an iterable object often used in loops.",[26,34137,34139],{"className":28,"code":34138,"language":30,"meta":31,"style":31},"for number in range(5):\n    print(number)\n",[33,34140,34141,34157],{"__ignoreMap":31},[36,34142,34143,34145,34147,34149,34151,34153,34155],{"class":38,"line":39},[36,34144,9852],{"class":1718},[36,34146,10205],{"class":42},[36,34148,9911],{"class":1718},[36,34150,10836],{"class":75},[36,34152,79],{"class":50},[36,34154,1243],{"class":54},[36,34156,3054],{"class":50},[36,34158,34159,34161,34163,34165],{"class":38,"line":72},[36,34160,1735],{"class":75},[36,34162,79],{"class":50},[36,34164,8368],{"class":82},[36,34166,86],{"class":50},[14,34168,34169],{},[331,34170,107],{},[26,34172,34174],{"className":28,"code":34173,"language":30,"meta":31,"style":31},"0\n1\n2\n3\n4\n",[33,34175,34176,34180,34184,34188,34192],{"__ignoreMap":31},[36,34177,34178],{"class":38,"line":39},[36,34179,10882],{"class":54},[36,34181,34182],{"class":38,"line":72},[36,34183,9938],{"class":54},[36,34185,34186],{"class":38,"line":89},[36,34187,9943],{"class":54},[36,34189,34190],{"class":38,"line":496},[36,34191,9948],{"class":54},[36,34193,34194],{"class":38,"line":501},[36,34195,10891],{"class":54},[1065,34197,34199],{"id":34198},"file-object","File object",[14,34201,34202],{},"A file object can be read line by line, which means it is iterable.",[26,34204,34206],{"className":28,"code":34205,"language":30,"meta":31,"style":31},"with open(\"example.txt\") as file:\n    for line in file:\n        print(line.strip())\n",[33,34207,34208,34235,34249],{"__ignoreMap":31},[36,34209,34210,34213,34216,34218,34220,34223,34225,34227,34230,34233],{"class":38,"line":39},[36,34211,34212],{"class":1718},"with",[36,34214,34215],{"class":75}," open",[36,34217,79],{"class":50},[36,34219,214],{"class":141},[36,34221,34222],{"class":145},"example.txt",[36,34224,214],{"class":141},[36,34226,1988],{"class":50},[36,34228,34229],{"class":1718}," as",[36,34231,18456],{"class":34232},"sMMDD",[36,34234,1730],{"class":50},[36,34236,34237,34240,34243,34245,34247],{"class":38,"line":72},[36,34238,34239],{"class":1718},"    for",[36,34241,34242],{"class":42}," line ",[36,34244,9911],{"class":1718},[36,34246,18456],{"class":34232},[36,34248,1730],{"class":50},[36,34250,34251,34253,34255,34257,34259,34261],{"class":38,"line":89},[36,34252,3095],{"class":75},[36,34254,79],{"class":50},[36,34256,38],{"class":82},[36,34258,351],{"class":50},[36,34260,16445],{"class":82},[36,34262,4387],{"class":50},[21,34264,34266],{"id":34265},"what-is-not-iterable","What is not iterable",[14,34268,34269,34270,34272],{},"Some values are ",[331,34271,8193],{}," iterable.",[14,34274,694],{},[157,34276,34277,34282,34287,34292],{},[160,34278,34279,34280],{},"A single number like ",[33,34281,1243],{},[160,34283,34284,34285],{},"A float like ",[33,34286,4871],{},[160,34288,34289,34290],{},"A boolean like ",[33,34291,1179],{},[160,34293,34294],{},[33,34295,9364],{},[14,34297,34298],{},"This code causes an error:",[26,34300,34302],{"className":28,"code":34301,"language":30,"meta":31,"style":31},"value = 5\n\nfor item in value:\n    print(item)\n",[33,34303,34304,34312,34316,34329],{"__ignoreMap":31},[36,34305,34306,34308,34310],{"class":38,"line":39},[36,34307,2664],{"class":42},[36,34309,47],{"class":46},[36,34311,4927],{"class":54},[36,34313,34314],{"class":38,"line":72},[36,34315,476],{"emptyLinePlaceholder":475},[36,34317,34318,34320,34322,34324,34327],{"class":38,"line":89},[36,34319,9852],{"class":1718},[36,34321,10439],{"class":42},[36,34323,9911],{"class":1718},[36,34325,34326],{"class":42}," value",[36,34328,1730],{"class":50},[36,34330,34331,34333,34335,34337],{"class":38,"line":496},[36,34332,1735],{"class":75},[36,34334,79],{"class":50},[36,34336,10455],{"class":82},[36,34338,86],{"class":50},[14,34340,34341,34342,34344],{},"Python raises a ",[33,34343,20624],{}," because an integer is not iterable.",[14,34346,34347,34348,351],{},"If you need help with that specific error, see ",[347,34349,17866,34351],{"href":34350},"\u002Ferrors\u002Ftypeerror-int-object-is-not-iterable-fix\u002F",[33,34352,33792],{},[21,34354,34356],{"id":34355},"iterable-vs-iterator","Iterable vs iterator",[14,34358,34359],{},"These two words are related, but they are not the same.",[157,34361,34362,34367],{},[160,34363,3361,34364,34366],{},[331,34365,10621],{}," is something you can loop over",[160,34368,3361,34369,34371],{},[331,34370,33447],{}," is the object that gives the next value each time",[14,34373,34374,34375,822],{},"You can turn an iterable into an iterator with ",[33,34376,34377],{},"iter()",[26,34379,34381],{"className":28,"code":34380,"language":30,"meta":31,"style":31},"numbers = [10, 20, 30]\niterator = iter(numbers)\n\nprint(next(iterator))\nprint(next(iterator))\nprint(next(iterator))\n",[33,34382,34383,34403,34419,34423,34437,34451],{"__ignoreMap":31},[36,34384,34385,34387,34389,34391,34393,34395,34397,34399,34401],{"class":38,"line":39},[36,34386,43],{"class":42},[36,34388,47],{"class":46},[36,34390,51],{"class":50},[36,34392,1560],{"class":54},[36,34394,58],{"class":50},[36,34396,1628],{"class":54},[36,34398,58],{"class":50},[36,34400,26390],{"class":54},[36,34402,69],{"class":50},[36,34404,34405,34408,34410,34413,34415,34417],{"class":38,"line":72},[36,34406,34407],{"class":42},"iterator ",[36,34409,47],{"class":46},[36,34411,34412],{"class":75}," iter",[36,34414,79],{"class":50},[36,34416,83],{"class":82},[36,34418,86],{"class":50},[36,34420,34421],{"class":38,"line":89},[36,34422,476],{"emptyLinePlaceholder":475},[36,34424,34425,34427,34429,34431,34433,34435],{"class":38,"line":496},[36,34426,76],{"class":75},[36,34428,79],{"class":50},[36,34430,11215],{"class":75},[36,34432,79],{"class":50},[36,34434,33447],{"class":82},[36,34436,104],{"class":50},[36,34438,34439,34441,34443,34445,34447,34449],{"class":38,"line":501},[36,34440,76],{"class":75},[36,34442,79],{"class":50},[36,34444,11215],{"class":75},[36,34446,79],{"class":50},[36,34448,33447],{"class":82},[36,34450,104],{"class":50},[36,34452,34453,34455,34457,34459,34461,34463],{"class":38,"line":522},[36,34454,76],{"class":75},[36,34456,79],{"class":50},[36,34458,11215],{"class":75},[36,34460,79],{"class":50},[36,34462,33447],{"class":82},[36,34464,104],{"class":50},[14,34466,34467],{},[331,34468,107],{},[26,34470,34471],{"className":28,"code":33505,"language":30,"meta":31,"style":31},[33,34472,34473,34477,34481],{"__ignoreMap":31},[36,34474,34475],{"class":38,"line":39},[36,34476,10792],{"class":54},[36,34478,34479],{"class":38,"line":72},[36,34480,21635],{"class":54},[36,34482,34483],{"class":38,"line":89},[36,34484,29181],{"class":54},[14,34486,6187],{},[157,34488,34489,34494],{},[160,34490,34491,34493],{},[33,34492,83],{}," is an iterable",[160,34495,34496,34498],{},[33,34497,33447],{}," is an iterator",[14,34500,34501,34502,1180,34504,351],{},"You do not need to memorize the full iterator protocol yet. For a full beginner explanation, see ",[347,34503,10948],{"href":10947},[347,34505,34507],{"href":34506},"\u002Flearn\u002Fiterators-and-iterable-objects-explained\u002F","iterators and iterable objects explained",[21,34509,34511],{"id":34510},"how-python-uses-iterables","How Python uses iterables",[14,34513,34514],{},"Python uses iterables in many common situations.",[1065,34516,1687,34518,1781],{"id":34517},"in-for-loops",[33,34519,9852],{},[14,34521,3355,34522,34524],{},[33,34523,9852],{}," loop asks for items one by one:",[26,34526,34528],{"className":28,"code":34527,"language":30,"meta":31,"style":31},"colors = [\"red\", \"green\", \"blue\"]\n\nfor color in colors:\n    print(color)\n",[33,34529,34530,34562,34566,34579],{"__ignoreMap":31},[36,34531,34532,34534,34536,34538,34540,34542,34544,34546,34548,34550,34552,34554,34556,34558,34560],{"class":38,"line":39},[36,34533,207],{"class":42},[36,34535,47],{"class":46},[36,34537,51],{"class":50},[36,34539,214],{"class":141},[36,34541,217],{"class":145},[36,34543,214],{"class":141},[36,34545,58],{"class":50},[36,34547,224],{"class":141},[36,34549,227],{"class":145},[36,34551,214],{"class":141},[36,34553,58],{"class":50},[36,34555,224],{"class":141},[36,34557,236],{"class":145},[36,34559,214],{"class":141},[36,34561,69],{"class":50},[36,34563,34564],{"class":38,"line":72},[36,34565,476],{"emptyLinePlaceholder":475},[36,34567,34568,34570,34573,34575,34577],{"class":38,"line":89},[36,34569,9852],{"class":1718},[36,34571,34572],{"class":42}," color ",[36,34574,9911],{"class":1718},[36,34576,23284],{"class":42},[36,34578,1730],{"class":50},[36,34580,34581,34583,34585,34587],{"class":38,"line":496},[36,34582,1735],{"class":75},[36,34584,79],{"class":50},[36,34586,20157],{"class":82},[36,34588,86],{"class":50},[14,34590,34591,34592,351],{},"If you want more loop examples, see ",[347,34593,15972],{"href":15971},[1065,34595,34597],{"id":34596},"in-membership-checks","In membership checks",[14,34599,3542,34600,34602],{},[33,34601,9911],{}," operator often works with iterables:",[26,34604,34606],{"className":28,"code":34605,"language":30,"meta":31,"style":31},"print(\"a\" in \"cat\")\nprint(2 in [1, 2, 3])\n",[33,34607,34608,34630],{"__ignoreMap":31},[36,34609,34610,34612,34614,34616,34618,34620,34622,34624,34626,34628],{"class":38,"line":39},[36,34611,76],{"class":75},[36,34613,79],{"class":50},[36,34615,214],{"class":141},[36,34617,347],{"class":145},[36,34619,214],{"class":141},[36,34621,7236],{"class":1718},[36,34623,224],{"class":141},[36,34625,13445],{"class":145},[36,34627,214],{"class":141},[36,34629,86],{"class":50},[36,34631,34632,34634,34636,34638,34640,34642,34644,34646,34648,34650,34652],{"class":38,"line":72},[36,34633,76],{"class":75},[36,34635,79],{"class":50},[36,34637,1543],{"class":54},[36,34639,7236],{"class":1718},[36,34641,51],{"class":50},[36,34643,55],{"class":54},[36,34645,58],{"class":50},[36,34647,61],{"class":54},[36,34649,58],{"class":50},[36,34651,66],{"class":54},[36,34653,493],{"class":50},[14,34655,34656],{},[331,34657,107],{},[26,34659,34660],{"className":28,"code":2000,"language":30,"meta":31,"style":31},[33,34661,34662,34666],{"__ignoreMap":31},[36,34663,34664],{"class":38,"line":39},[36,34665,1481],{"class":400},[36,34667,34668],{"class":38,"line":72},[36,34669,1481],{"class":400},[1065,34671,34673],{"id":34672},"in-unpacking","In unpacking",[14,34675,34676],{},"Python can unpack values from an iterable:",[26,34678,34680],{"className":28,"code":34679,"language":30,"meta":31,"style":31},"point = (4, 7)\nx, y = point\n\nprint(x)\nprint(y)\n",[33,34681,34682,34698,34712,34716,34726],{"__ignoreMap":31},[36,34683,34684,34686,34688,34690,34692,34694,34696],{"class":38,"line":39},[36,34685,26727],{"class":42},[36,34687,47],{"class":46},[36,34689,744],{"class":50},[36,34691,11622],{"class":54},[36,34693,58],{"class":50},[36,34695,1566],{"class":54},[36,34697,86],{"class":50},[36,34699,34700,34702,34704,34707,34709],{"class":38,"line":72},[36,34701,5306],{"class":42},[36,34703,58],{"class":50},[36,34705,34706],{"class":42}," y ",[36,34708,47],{"class":46},[36,34710,34711],{"class":42}," point\n",[36,34713,34714],{"class":38,"line":89},[36,34715,476],{"emptyLinePlaceholder":475},[36,34717,34718,34720,34722,34724],{"class":38,"line":496},[36,34719,76],{"class":75},[36,34721,79],{"class":50},[36,34723,5306],{"class":82},[36,34725,86],{"class":50},[36,34727,34728,34730,34732,34735],{"class":38,"line":501},[36,34729,76],{"class":75},[36,34731,79],{"class":50},[36,34733,34734],{"class":82},"y",[36,34736,86],{"class":50},[14,34738,34739],{},[331,34740,107],{},[26,34742,34744],{"className":28,"code":34743,"language":30,"meta":31,"style":31},"4\n7\n",[33,34745,34746,34750],{"__ignoreMap":31},[36,34747,34748],{"class":38,"line":39},[36,34749,10891],{"class":54},[36,34751,34752],{"class":38,"line":72},[36,34753,33247],{"class":54},[1065,34755,34757],{"id":34756},"in-built-in-functions","In built-in functions",[14,34759,34760],{},"Many built-in functions read values from an iterable:",[26,34762,34764],{"className":28,"code":34763,"language":30,"meta":31,"style":31},"numbers = [3, 1, 2]\n\nprint(sorted(numbers))\nprint(any([False, False, True]))\nprint(all([True, True, True]))\n",[33,34765,34766,34786,34790,34804,34828],{"__ignoreMap":31},[36,34767,34768,34770,34772,34774,34776,34778,34780,34782,34784],{"class":38,"line":39},[36,34769,43],{"class":42},[36,34771,47],{"class":46},[36,34773,51],{"class":50},[36,34775,1247],{"class":54},[36,34777,58],{"class":50},[36,34779,16839],{"class":54},[36,34781,58],{"class":50},[36,34783,61],{"class":54},[36,34785,69],{"class":50},[36,34787,34788],{"class":38,"line":72},[36,34789,476],{"emptyLinePlaceholder":475},[36,34791,34792,34794,34796,34798,34800,34802],{"class":38,"line":89},[36,34793,76],{"class":75},[36,34795,79],{"class":50},[36,34797,12599],{"class":75},[36,34799,79],{"class":50},[36,34801,83],{"class":82},[36,34803,104],{"class":50},[36,34805,34806,34808,34810,34813,34815,34817,34819,34822,34824,34826],{"class":38,"line":496},[36,34807,76],{"class":75},[36,34809,79],{"class":50},[36,34811,34812],{"class":75},"any",[36,34814,5490],{"class":50},[36,34816,1183],{"class":400},[36,34818,58],{"class":50},[36,34820,34821],{"class":400}," False",[36,34823,58],{"class":50},[36,34825,401],{"class":400},[36,34827,5503],{"class":50},[36,34829,34830,34832,34834,34837,34839,34841,34843,34845,34847,34849],{"class":38,"line":501},[36,34831,76],{"class":75},[36,34833,79],{"class":50},[36,34835,34836],{"class":75},"all",[36,34838,5490],{"class":50},[36,34840,1179],{"class":400},[36,34842,58],{"class":50},[36,34844,401],{"class":400},[36,34846,58],{"class":50},[36,34848,401],{"class":400},[36,34850,5503],{"class":50},[14,34852,34853],{},[331,34854,107],{},[26,34856,34858],{"className":28,"code":34857,"language":30,"meta":31,"style":31},"[1, 2, 3]\nTrue\nTrue\n",[33,34859,34860,34876,34880],{"__ignoreMap":31},[36,34861,34862,34864,34866,34868,34870,34872,34874],{"class":38,"line":39},[36,34863,117],{"class":50},[36,34865,55],{"class":54},[36,34867,58],{"class":50},[36,34869,61],{"class":54},[36,34871,58],{"class":50},[36,34873,66],{"class":54},[36,34875,69],{"class":50},[36,34877,34878],{"class":38,"line":72},[36,34879,1481],{"class":400},[36,34881,34882],{"class":38,"line":89},[36,34883,1481],{"class":400},[21,34885,34887],{"id":34886},"how-to-check-if-something-behaves-like-an-iterable","How to check if something behaves like an iterable",[14,34889,34890],{},"For beginners, the easiest way is to test it in a practical way.",[1065,34892,34894,34895,15790],{"id":34893},"try-a-for-loop","Try a ",[33,34896,9852],{},[26,34898,34900],{"className":28,"code":34899,"language":30,"meta":31,"style":31},"value = \"hello\"\n\nfor item in value:\n    print(item)\n",[33,34901,34902,34914,34918,34930],{"__ignoreMap":31},[36,34903,34904,34906,34908,34910,34912],{"class":38,"line":39},[36,34905,2664],{"class":42},[36,34907,47],{"class":46},[36,34909,224],{"class":141},[36,34911,2671],{"class":145},[36,34913,1954],{"class":141},[36,34915,34916],{"class":38,"line":72},[36,34917,476],{"emptyLinePlaceholder":475},[36,34919,34920,34922,34924,34926,34928],{"class":38,"line":89},[36,34921,9852],{"class":1718},[36,34923,10439],{"class":42},[36,34925,9911],{"class":1718},[36,34927,34326],{"class":42},[36,34929,1730],{"class":50},[36,34931,34932,34934,34936,34938],{"class":38,"line":496},[36,34933,1735],{"class":75},[36,34935,79],{"class":50},[36,34937,10455],{"class":82},[36,34939,86],{"class":50},[14,34941,34942],{},"If Python can loop through it, it behaves like an iterable.",[1065,34944,34946,34947],{"id":34945},"try-iter","Try ",[33,34948,34377],{},[26,34950,34952],{"className":28,"code":34951,"language":30,"meta":31,"style":31},"value = [1, 2, 3]\niterator = iter(value)\n\nprint(iterator)\n",[33,34953,34954,34974,34988,34992],{"__ignoreMap":31},[36,34955,34956,34958,34960,34962,34964,34966,34968,34970,34972],{"class":38,"line":39},[36,34957,2664],{"class":42},[36,34959,47],{"class":46},[36,34961,51],{"class":50},[36,34963,55],{"class":54},[36,34965,58],{"class":50},[36,34967,61],{"class":54},[36,34969,58],{"class":50},[36,34971,66],{"class":54},[36,34973,69],{"class":50},[36,34975,34976,34978,34980,34982,34984,34986],{"class":38,"line":72},[36,34977,34407],{"class":42},[36,34979,47],{"class":46},[36,34981,34412],{"class":75},[36,34983,79],{"class":50},[36,34985,2686],{"class":82},[36,34987,86],{"class":50},[36,34989,34990],{"class":38,"line":89},[36,34991,476],{"emptyLinePlaceholder":475},[36,34993,34994,34996,34998,35000],{"class":38,"line":496},[36,34995,76],{"class":75},[36,34997,79],{"class":50},[36,34999,33447],{"class":82},[36,35001,86],{"class":50},[14,35003,35004,35005,35008],{},"If ",[33,35006,35007],{},"iter(value)"," works, the object is iterable.",[14,35010,35011,35012,351],{},"If it fails, Python raises a ",[33,35013,20624],{},[14,35015,197],{},[26,35017,35019],{"className":28,"code":35018,"language":30,"meta":31,"style":31},"value = 3.14\niterator = iter(value)\n",[33,35020,35021,35029],{"__ignoreMap":31},[36,35022,35023,35025,35027],{"class":38,"line":39},[36,35024,2664],{"class":42},[36,35026,47],{"class":46},[36,35028,4937],{"class":54},[36,35030,35031,35033,35035,35037,35039,35041],{"class":38,"line":72},[36,35032,34407],{"class":42},[36,35034,47],{"class":46},[36,35036,34412],{"class":75},[36,35038,79],{"class":50},[36,35040,2686],{"class":82},[36,35042,86],{"class":50},[14,35044,35045],{},"This raises an error because a float is not iterable.",[21,35047,905],{"id":904},[14,35049,8484],{},[157,35051,35052,35055,35058,35069],{},[160,35053,35054],{},"Thinking iterable means list only",[160,35056,35057],{},"Confusing an iterable with an iterator",[160,35059,35060,35061,5950,35063,5950,35065,6128,35067],{},"Trying to loop over an ",[33,35062,2466],{},[33,35064,4740],{},[33,35066,1378],{},[33,35068,9364],{},[160,35070,35071],{},"Forgetting that dictionaries loop over keys by default",[14,35073,35074],{},"These quick checks can help when debugging:",[26,35076,35078],{"className":28,"code":35077,"language":30,"meta":31,"style":31},"type(value)\niter(value)\nlist(value)\nfor item in value:\n    print(item)\n",[33,35079,35080,35090,35101,35111,35123],{"__ignoreMap":31},[36,35081,35082,35084,35086,35088],{"class":38,"line":39},[36,35083,97],{"class":96},[36,35085,79],{"class":50},[36,35087,2686],{"class":82},[36,35089,86],{"class":50},[36,35091,35092,35095,35097,35099],{"class":38,"line":72},[36,35093,35094],{"class":75},"iter",[36,35096,79],{"class":50},[36,35098,2686],{"class":82},[36,35100,86],{"class":50},[36,35102,35103,35105,35107,35109],{"class":38,"line":89},[36,35104,146],{"class":96},[36,35106,79],{"class":50},[36,35108,2686],{"class":82},[36,35110,86],{"class":50},[36,35112,35113,35115,35117,35119,35121],{"class":38,"line":496},[36,35114,9852],{"class":1718},[36,35116,10439],{"class":42},[36,35118,9911],{"class":1718},[36,35120,34326],{"class":42},[36,35122,1730],{"class":50},[36,35124,35125,35127,35129,35131],{"class":38,"line":501},[36,35126,1735],{"class":75},[36,35128,79],{"class":50},[36,35130,10455],{"class":82},[36,35132,86],{"class":50},[14,35134,1051,35135,1281,35138,35141,35142,351],{},[33,35136,35137],{},"list(value)",[33,35139,35140],{},"for item in value"," if you are not sure the value is iterable. They can raise a ",[33,35143,20624],{},[14,35145,35146],{},"Related error pages:",[157,35148,35149,35157],{},[160,35150,35151],{},[347,35152,35154,35155],{"href":35153},"\u002Ferrors\u002Ftypeerror-bool-object-is-not-iterable-fix\u002F","Fix ",[33,35156,33797],{},[160,35158,35159],{},[347,35160,35154,35161],{"href":21488},[33,35162,21489],{},[21,35164,1063],{"id":1062},[1065,35166,35168],{"id":35167},"is-every-iterable-a-list","Is every iterable a list?",[14,35170,35171,35172,35174],{},"No. Lists are iterable, but strings, tuples, sets, dictionaries, ",[33,35173,12962],{}," objects, and file objects are iterable too.",[1065,35176,35178],{"id":35177},"is-a-dictionary-iterable","Is a dictionary iterable?",[14,35180,35181,35182,1180,35184,351],{},"Yes. A dictionary is iterable, and looping over it gives its keys unless you use ",[33,35183,34121],{},[33,35185,34124],{},[1065,35187,35189],{"id":35188},"is-a-string-iterable","Is a string iterable?",[14,35191,35192],{},"Yes. Python can loop through a string one character at a time.",[1065,35194,35196],{"id":35195},"what-happens-if-i-try-to-loop-over-a-number","What happens if I try to loop over a number?",[14,35198,34341,35199,35201],{},[33,35200,20624],{}," because numbers are not iterable.",[1065,35203,35205],{"id":35204},"what-is-the-difference-between-iterable-and-iterator","What is the difference between iterable and iterator?",[14,35207,35208],{},"An iterable is something you can start looping over. An iterator is the object that returns each next value during that loop.",[21,35210,1105],{"id":1104},[157,35212,35213,35218,35223,35227,35233],{},[160,35214,35215],{},[347,35216,35217],{"href":10947},"What is an iterator in Python?",[160,35219,35220],{},[347,35221,35222],{"href":34506},"Iterators and iterable objects explained",[160,35224,35225],{},[347,35226,13398],{"href":15826},[160,35228,35229],{},[347,35230,1665,35231,2816],{"href":15978},[33,35232,15811],{},[160,35234,35235],{},[347,35236,16281],{"href":15971},[1137,35238,35239],{},"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 .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 .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 .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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":35241},[35242,35243,35244,35245,35254,35255,35256,35263,35269,35270,35277],{"id":23,"depth":72,"text":24},{"id":6100,"depth":72,"text":6101},{"id":33642,"depth":72,"text":33643},{"id":33809,"depth":72,"text":33810,"children":35246},[35247,35248,35249,35250,35251,35252,35253],{"id":146,"depth":89,"text":672},{"id":5615,"depth":89,"text":678},{"id":4728,"depth":89,"text":4850},{"id":23063,"depth":89,"text":684},{"id":6770,"depth":89,"text":690},{"id":34127,"depth":89,"text":34128},{"id":34198,"depth":89,"text":34199},{"id":34265,"depth":72,"text":34266},{"id":34355,"depth":72,"text":34356},{"id":34510,"depth":72,"text":34511,"children":35257},[35258,35260,35261,35262],{"id":34517,"depth":89,"text":35259},"In for loops",{"id":34596,"depth":89,"text":34597},{"id":34672,"depth":89,"text":34673},{"id":34756,"depth":89,"text":34757},{"id":34886,"depth":72,"text":34887,"children":35264},[35265,35267],{"id":34893,"depth":89,"text":35266},"Try a for loop",{"id":34945,"depth":89,"text":35268},"Try iter()",{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":35271},[35272,35273,35274,35275,35276],{"id":35167,"depth":89,"text":35168},{"id":35177,"depth":89,"text":35178},{"id":35188,"depth":89,"text":35189},{"id":35195,"depth":89,"text":35196},{"id":35204,"depth":89,"text":35205},{"id":1104,"depth":72,"text":1105},"Master what is an iterable in python in our comprehensive Python beginner guide.",{},{"title":33417,"description":35278},"glossary\u002Fwhat-is-an-iterable-in-python","FuGRJvUrhskJKB0Y1oQEBFO_9mtnah4PaTX5kTWB3Ug",{"id":35284,"title":35285,"body":35286,"description":36877,"extension":1159,"meta":36878,"navigation":475,"path":36879,"seo":36880,"stem":36881,"__hash__":36882},"content\u002Fglossary\u002Fwhat-is-an-iterator-in-python.md","What Is an Iterator in Python?",{"type":7,"value":35287,"toc":36856},[35288,35291,35297,35306,35309,35320,35322,35409,35411,35427,35436,35440,35443,35463,35466,35470,35473,35485,35487,35497,35502,35504,35579,35582,35587,35596,35599,35613,35615,35712,35714,35731,35739,35850,35853,35862,35869,35873,35878,35881,35932,35935,35955,35961,35964,35968,35971,36087,36090,36106,36113,36209,36211,36235,36239,36242,36270,36280,36284,36287,36305,36307,36417,36419,36441,36444,36447,36540,36542,36555,36557,36559,36585,36591,36630,36633,36635,36688,36691,36766,36769,36781,36783,36787,36793,36797,36800,36808,36811,36815,36820,36824,36827,36829,36853],[10,35289,35285],{"id":35290},"what-is-an-iterator-in-python",[14,35292,35293,35294,351],{},"An iterator in Python is an object that gives you values ",[331,35295,35296],{},"one at a time",[14,35298,35299,35300,35302,35303,351],{},"It keeps track of where it is, so each time you ask for the next value, it moves forward. Iterators are used behind the scenes in ",[33,35301,9852],{}," loops, and they are closely related to ",[347,35304,35305],{"href":10935},"iterable objects",[14,35307,35308],{},"A beginner-friendly way to think about it is this:",[157,35310,35311,35315],{},[160,35312,3361,35313,34366],{},[331,35314,10621],{},[160,35316,3361,35317,35319],{},[331,35318,33447],{}," is the thing that actually returns each item one by one",[21,35321,24],{"id":23},[26,35323,35325],{"className":28,"code":35324,"language":30,"meta":31,"style":31},"numbers = [10, 20, 30]\nit = iter(numbers)\n\nprint(next(it))\nprint(next(it))\nprint(next(it))\n",[33,35326,35327,35347,35362,35366,35381,35395],{"__ignoreMap":31},[36,35328,35329,35331,35333,35335,35337,35339,35341,35343,35345],{"class":38,"line":39},[36,35330,43],{"class":42},[36,35332,47],{"class":46},[36,35334,51],{"class":50},[36,35336,1560],{"class":54},[36,35338,58],{"class":50},[36,35340,1628],{"class":54},[36,35342,58],{"class":50},[36,35344,26390],{"class":54},[36,35346,69],{"class":50},[36,35348,35349,35352,35354,35356,35358,35360],{"class":38,"line":72},[36,35350,35351],{"class":42},"it ",[36,35353,47],{"class":46},[36,35355,34412],{"class":75},[36,35357,79],{"class":50},[36,35359,83],{"class":82},[36,35361,86],{"class":50},[36,35363,35364],{"class":38,"line":89},[36,35365,476],{"emptyLinePlaceholder":475},[36,35367,35368,35370,35372,35374,35376,35379],{"class":38,"line":496},[36,35369,76],{"class":75},[36,35371,79],{"class":50},[36,35373,11215],{"class":75},[36,35375,79],{"class":50},[36,35377,35378],{"class":82},"it",[36,35380,104],{"class":50},[36,35382,35383,35385,35387,35389,35391,35393],{"class":38,"line":501},[36,35384,76],{"class":75},[36,35386,79],{"class":50},[36,35388,11215],{"class":75},[36,35390,79],{"class":50},[36,35392,35378],{"class":82},[36,35394,104],{"class":50},[36,35396,35397,35399,35401,35403,35405,35407],{"class":38,"line":522},[36,35398,76],{"class":75},[36,35400,79],{"class":50},[36,35402,11215],{"class":75},[36,35404,79],{"class":50},[36,35406,35378],{"class":82},[36,35408,104],{"class":50},[14,35410,107],{},[26,35412,35413],{"className":28,"code":33505,"language":30,"meta":31,"style":31},[33,35414,35415,35419,35423],{"__ignoreMap":31},[36,35416,35417],{"class":38,"line":39},[36,35418,10792],{"class":54},[36,35420,35421],{"class":38,"line":72},[36,35422,21635],{"class":54},[36,35424,35425],{"class":38,"line":89},[36,35426,29181],{"class":54},[14,35428,2489,35429,35431,35432,35435],{},[33,35430,34377],{}," to create an iterator from an iterable, then use ",[33,35433,35434],{},"next()"," to get one item at a time.",[21,35437,35439],{"id":35438},"what-an-iterator-means","What an iterator means",[14,35441,35442],{},"An iterator:",[157,35444,35445,35448,35451,35456],{},[160,35446,35447],{},"Gives values one at a time",[160,35449,35450],{},"Remembers its current position",[160,35452,35453,35454],{},"Moves forward each time you call ",[33,35455,35434],{},[160,35457,35458,35459,35462],{},"Raises ",[33,35460,35461],{},"StopIteration"," when there are no items left",[14,35464,35465],{},"This is why iterators are useful when Python needs to process data step by step.",[21,35467,35469],{"id":35468},"iterator-vs-iterable","Iterator vs iterable",[14,35471,35472],{},"These two terms are related, but they are not the same.",[157,35474,35475,35480],{},[160,35476,3361,35477,35479],{},[331,35478,10621],{}," is an object you can loop over",[160,35481,3361,35482,35484],{},[331,35483,33447],{}," is the object that does the step-by-step looping",[14,35486,33543],{},[157,35488,35489,35491,35493,35495],{},[160,35490,22630],{},[160,35492,22636],{},[160,35494,33548],{},[160,35496,33557],{},[14,35498,35499,35500,351],{},"You usually create an iterator from an iterable with ",[33,35501,34377],{},[14,35503,197],{},[26,35505,35507],{"className":28,"code":35506,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\n\nprint(type(numbers))\n\nit = iter(numbers)\nprint(type(it))\n",[33,35508,35509,35529,35533,35547,35551,35565],{"__ignoreMap":31},[36,35510,35511,35513,35515,35517,35519,35521,35523,35525,35527],{"class":38,"line":39},[36,35512,43],{"class":42},[36,35514,47],{"class":46},[36,35516,51],{"class":50},[36,35518,55],{"class":54},[36,35520,58],{"class":50},[36,35522,61],{"class":54},[36,35524,58],{"class":50},[36,35526,66],{"class":54},[36,35528,69],{"class":50},[36,35530,35531],{"class":38,"line":72},[36,35532,476],{"emptyLinePlaceholder":475},[36,35534,35535,35537,35539,35541,35543,35545],{"class":38,"line":89},[36,35536,76],{"class":75},[36,35538,79],{"class":50},[36,35540,97],{"class":96},[36,35542,79],{"class":50},[36,35544,83],{"class":82},[36,35546,104],{"class":50},[36,35548,35549],{"class":38,"line":496},[36,35550,476],{"emptyLinePlaceholder":475},[36,35552,35553,35555,35557,35559,35561,35563],{"class":38,"line":501},[36,35554,35351],{"class":42},[36,35556,47],{"class":46},[36,35558,34412],{"class":75},[36,35560,79],{"class":50},[36,35562,83],{"class":82},[36,35564,86],{"class":50},[36,35566,35567,35569,35571,35573,35575,35577],{"class":38,"line":522},[36,35568,76],{"class":75},[36,35570,79],{"class":50},[36,35572,97],{"class":96},[36,35574,79],{"class":50},[36,35576,35378],{"class":82},[36,35578,104],{"class":50},[14,35580,35581],{},"A list is iterable, but it is not itself an iterator.",[14,35583,35584,35585,351],{},"If you want a deeper explanation, see ",[347,35586,10936],{"href":10935},[21,35588,35590,35591,1281,35593,35595],{"id":35589},"how-iter-and-next-work","How ",[33,35592,34377],{},[33,35594,35434],{}," work",[14,35597,35598],{},"The two main functions are:",[157,35600,35601,35607],{},[160,35602,35603,35606],{},[33,35604,35605],{},"iter(obj)"," creates an iterator from an iterable object",[160,35608,35609,35612],{},[33,35610,35611],{},"next(iterator)"," gets the next value from that iterator",[14,35614,197],{},[26,35616,35618],{"className":28,"code":35617,"language":30,"meta":31,"style":31},"letters = [\"a\", \"b\", \"c\"]\nit = iter(letters)\n\nprint(next(it))\nprint(next(it))\nprint(next(it))\n",[33,35619,35620,35652,35666,35670,35684,35698],{"__ignoreMap":31},[36,35621,35622,35624,35626,35628,35630,35632,35634,35636,35638,35640,35642,35644,35646,35648,35650],{"class":38,"line":39},[36,35623,22898],{"class":42},[36,35625,47],{"class":46},[36,35627,51],{"class":50},[36,35629,214],{"class":141},[36,35631,347],{"class":145},[36,35633,214],{"class":141},[36,35635,58],{"class":50},[36,35637,224],{"class":141},[36,35639,721],{"class":145},[36,35641,214],{"class":141},[36,35643,58],{"class":50},[36,35645,224],{"class":141},[36,35647,730],{"class":145},[36,35649,214],{"class":141},[36,35651,69],{"class":50},[36,35653,35654,35656,35658,35660,35662,35664],{"class":38,"line":72},[36,35655,35351],{"class":42},[36,35657,47],{"class":46},[36,35659,34412],{"class":75},[36,35661,79],{"class":50},[36,35663,22937],{"class":82},[36,35665,86],{"class":50},[36,35667,35668],{"class":38,"line":89},[36,35669,476],{"emptyLinePlaceholder":475},[36,35671,35672,35674,35676,35678,35680,35682],{"class":38,"line":496},[36,35673,76],{"class":75},[36,35675,79],{"class":50},[36,35677,11215],{"class":75},[36,35679,79],{"class":50},[36,35681,35378],{"class":82},[36,35683,104],{"class":50},[36,35685,35686,35688,35690,35692,35694,35696],{"class":38,"line":501},[36,35687,76],{"class":75},[36,35689,79],{"class":50},[36,35691,11215],{"class":75},[36,35693,79],{"class":50},[36,35695,35378],{"class":82},[36,35697,104],{"class":50},[36,35699,35700,35702,35704,35706,35708,35710],{"class":38,"line":522},[36,35701,76],{"class":75},[36,35703,79],{"class":50},[36,35705,11215],{"class":75},[36,35707,79],{"class":50},[36,35709,35378],{"class":82},[36,35711,104],{"class":50},[14,35713,107],{},[26,35715,35717],{"className":28,"code":35716,"language":30,"meta":31,"style":31},"a\nb\nc\n",[33,35718,35719,35723,35727],{"__ignoreMap":31},[36,35720,35721],{"class":38,"line":39},[36,35722,26350],{"class":42},[36,35724,35725],{"class":38,"line":72},[36,35726,26355],{"class":42},[36,35728,35729],{"class":38,"line":89},[36,35730,32582],{"class":42},[14,35732,35733,35734,35736,35737,351],{},"If you call ",[33,35735,35434],{}," again after the iterator is finished, Python raises ",[33,35738,35461],{},[26,35740,35742],{"className":28,"code":35741,"language":30,"meta":31,"style":31},"letters = [\"a\", \"b\", \"c\"]\nit = iter(letters)\n\nprint(next(it))\nprint(next(it))\nprint(next(it))\nprint(next(it))\n",[33,35743,35744,35776,35790,35794,35808,35822,35836],{"__ignoreMap":31},[36,35745,35746,35748,35750,35752,35754,35756,35758,35760,35762,35764,35766,35768,35770,35772,35774],{"class":38,"line":39},[36,35747,22898],{"class":42},[36,35749,47],{"class":46},[36,35751,51],{"class":50},[36,35753,214],{"class":141},[36,35755,347],{"class":145},[36,35757,214],{"class":141},[36,35759,58],{"class":50},[36,35761,224],{"class":141},[36,35763,721],{"class":145},[36,35765,214],{"class":141},[36,35767,58],{"class":50},[36,35769,224],{"class":141},[36,35771,730],{"class":145},[36,35773,214],{"class":141},[36,35775,69],{"class":50},[36,35777,35778,35780,35782,35784,35786,35788],{"class":38,"line":72},[36,35779,35351],{"class":42},[36,35781,47],{"class":46},[36,35783,34412],{"class":75},[36,35785,79],{"class":50},[36,35787,22937],{"class":82},[36,35789,86],{"class":50},[36,35791,35792],{"class":38,"line":89},[36,35793,476],{"emptyLinePlaceholder":475},[36,35795,35796,35798,35800,35802,35804,35806],{"class":38,"line":496},[36,35797,76],{"class":75},[36,35799,79],{"class":50},[36,35801,11215],{"class":75},[36,35803,79],{"class":50},[36,35805,35378],{"class":82},[36,35807,104],{"class":50},[36,35809,35810,35812,35814,35816,35818,35820],{"class":38,"line":501},[36,35811,76],{"class":75},[36,35813,79],{"class":50},[36,35815,11215],{"class":75},[36,35817,79],{"class":50},[36,35819,35378],{"class":82},[36,35821,104],{"class":50},[36,35823,35824,35826,35828,35830,35832,35834],{"class":38,"line":522},[36,35825,76],{"class":75},[36,35827,79],{"class":50},[36,35829,11215],{"class":75},[36,35831,79],{"class":50},[36,35833,35378],{"class":82},[36,35835,104],{"class":50},[36,35837,35838,35840,35842,35844,35846,35848],{"class":38,"line":527},[36,35839,76],{"class":75},[36,35841,79],{"class":50},[36,35843,11215],{"class":75},[36,35845,79],{"class":50},[36,35847,35378],{"class":82},[36,35849,104],{"class":50},[14,35851,35852],{},"This causes:",[26,35854,35856],{"className":28,"code":35855,"language":30,"meta":31,"style":31},"StopIteration\n",[33,35857,35858],{"__ignoreMap":31},[36,35859,35860],{"class":38,"line":39},[36,35861,35855],{"class":96},[14,35863,35864,35865,351],{},"That exception simply means there are no more values left. If you want to understand this error better, see ",[347,35866,35868],{"href":35867},"\u002Ferrors\u002Fstopiteration-exception-in-python-explained\u002F","StopIteration exception in Python explained",[21,35870,35872],{"id":35871},"how-for-loops-use-iterators","How for loops use iterators",[14,35874,3355,35875,35877],{},[33,35876,9852],{}," loop uses iterators automatically.",[14,35879,35880],{},"When you write this:",[26,35882,35884],{"className":28,"code":35883,"language":30,"meta":31,"style":31},"numbers = [10, 20, 30]\n\nfor number in numbers:\n    print(number)\n",[33,35885,35886,35906,35910,35922],{"__ignoreMap":31},[36,35887,35888,35890,35892,35894,35896,35898,35900,35902,35904],{"class":38,"line":39},[36,35889,43],{"class":42},[36,35891,47],{"class":46},[36,35893,51],{"class":50},[36,35895,1560],{"class":54},[36,35897,58],{"class":50},[36,35899,1628],{"class":54},[36,35901,58],{"class":50},[36,35903,26390],{"class":54},[36,35905,69],{"class":50},[36,35907,35908],{"class":38,"line":72},[36,35909,476],{"emptyLinePlaceholder":475},[36,35911,35912,35914,35916,35918,35920],{"class":38,"line":89},[36,35913,9852],{"class":1718},[36,35915,10205],{"class":42},[36,35917,9911],{"class":1718},[36,35919,9874],{"class":42},[36,35921,1730],{"class":50},[36,35923,35924,35926,35928,35930],{"class":38,"line":496},[36,35925,1735],{"class":75},[36,35927,79],{"class":50},[36,35929,8368],{"class":82},[36,35931,86],{"class":50},[14,35933,35934],{},"Python does something like this behind the scenes:",[27331,35936,35937,35943,35949],{},[160,35938,35939,35940],{},"Calls ",[33,35941,35942],{},"iter(numbers)",[160,35944,35945,35946],{},"Keeps calling ",[33,35947,35948],{},"next(...)",[160,35950,35951,35952,35954],{},"Stops when ",[33,35953,35461],{}," happens",[14,35956,35957,35958,35960],{},"So a ",[33,35959,9852],{}," loop is really using the iterator protocol for you.",[14,35962,35963],{},"That is why you usually do not need to manage iterators directly. Still, understanding them helps explain how loops work and why some objects can only be used once.",[21,35965,35967],{"id":35966},"simple-example-to-include","Simple example to include",[14,35969,35970],{},"Here is a full example that shows how an iterator is created and consumed:",[26,35972,35974],{"className":28,"code":35973,"language":30,"meta":31,"style":31},"numbers = [100, 200, 300]\nit = iter(numbers)\n\nprint(next(it))  # first item\nprint(next(it))  # second item\nprint(next(it))  # third item\n\nprint(next(it))  # no items left\n",[33,35975,35976,35997,36011,36015,36032,36049,36066,36070],{"__ignoreMap":31},[36,35977,35978,35980,35982,35984,35986,35988,35990,35992,35995],{"class":38,"line":39},[36,35979,43],{"class":42},[36,35981,47],{"class":46},[36,35983,51],{"class":50},[36,35985,31942],{"class":54},[36,35987,58],{"class":50},[36,35989,6445],{"class":54},[36,35991,58],{"class":50},[36,35993,35994],{"class":54}," 300",[36,35996,69],{"class":50},[36,35998,35999,36001,36003,36005,36007,36009],{"class":38,"line":72},[36,36000,35351],{"class":42},[36,36002,47],{"class":46},[36,36004,34412],{"class":75},[36,36006,79],{"class":50},[36,36008,83],{"class":82},[36,36010,86],{"class":50},[36,36012,36013],{"class":38,"line":89},[36,36014,476],{"emptyLinePlaceholder":475},[36,36016,36017,36019,36021,36023,36025,36027,36029],{"class":38,"line":496},[36,36018,76],{"class":75},[36,36020,79],{"class":50},[36,36022,11215],{"class":75},[36,36024,79],{"class":50},[36,36026,35378],{"class":82},[36,36028,16574],{"class":50},[36,36030,36031],{"class":2570},"  # first item\n",[36,36033,36034,36036,36038,36040,36042,36044,36046],{"class":38,"line":501},[36,36035,76],{"class":75},[36,36037,79],{"class":50},[36,36039,11215],{"class":75},[36,36041,79],{"class":50},[36,36043,35378],{"class":82},[36,36045,16574],{"class":50},[36,36047,36048],{"class":2570},"  # second item\n",[36,36050,36051,36053,36055,36057,36059,36061,36063],{"class":38,"line":522},[36,36052,76],{"class":75},[36,36054,79],{"class":50},[36,36056,11215],{"class":75},[36,36058,79],{"class":50},[36,36060,35378],{"class":82},[36,36062,16574],{"class":50},[36,36064,36065],{"class":2570},"  # third item\n",[36,36067,36068],{"class":38,"line":527},[36,36069,476],{"emptyLinePlaceholder":475},[36,36071,36072,36074,36076,36078,36080,36082,36084],{"class":38,"line":3092},[36,36073,76],{"class":75},[36,36075,79],{"class":50},[36,36077,11215],{"class":75},[36,36079,79],{"class":50},[36,36081,35378],{"class":82},[36,36083,16574],{"class":50},[36,36085,36086],{"class":2570},"  # no items left\n",[14,36088,36089],{},"Expected behavior:",[157,36091,36092,36098],{},[160,36093,36094,36095,36097],{},"The first three ",[33,36096,35434],{}," calls print values",[160,36099,36100,36101,36103,36104],{},"The last ",[33,36102,35434],{}," call raises ",[33,36105,35461],{},[14,36107,36108,36109,1281,36111,822],{},"A safer version uses ",[33,36110,29606],{},[33,36112,29609],{},[26,36114,36116],{"className":28,"code":36115,"language":30,"meta":31,"style":31},"numbers = [100, 200, 300]\nit = iter(numbers)\n\ntry:\n    while True:\n        print(next(it))\nexcept StopIteration:\n    print(\"Iterator finished\")\n",[33,36117,36118,36138,36152,36156,36162,36171,36185,36194],{"__ignoreMap":31},[36,36119,36120,36122,36124,36126,36128,36130,36132,36134,36136],{"class":38,"line":39},[36,36121,43],{"class":42},[36,36123,47],{"class":46},[36,36125,51],{"class":50},[36,36127,31942],{"class":54},[36,36129,58],{"class":50},[36,36131,6445],{"class":54},[36,36133,58],{"class":50},[36,36135,35994],{"class":54},[36,36137,69],{"class":50},[36,36139,36140,36142,36144,36146,36148,36150],{"class":38,"line":72},[36,36141,35351],{"class":42},[36,36143,47],{"class":46},[36,36145,34412],{"class":75},[36,36147,79],{"class":50},[36,36149,83],{"class":82},[36,36151,86],{"class":50},[36,36153,36154],{"class":38,"line":89},[36,36155,476],{"emptyLinePlaceholder":475},[36,36157,36158,36160],{"class":38,"line":496},[36,36159,29606],{"class":1718},[36,36161,1730],{"class":50},[36,36163,36164,36167,36169],{"class":38,"line":501},[36,36165,36166],{"class":1718},"    while",[36,36168,401],{"class":400},[36,36170,1730],{"class":50},[36,36172,36173,36175,36177,36179,36181,36183],{"class":38,"line":522},[36,36174,3095],{"class":75},[36,36176,79],{"class":50},[36,36178,11215],{"class":75},[36,36180,79],{"class":50},[36,36182,35378],{"class":82},[36,36184,104],{"class":50},[36,36186,36187,36189,36192],{"class":38,"line":527},[36,36188,29609],{"class":1718},[36,36190,36191],{"class":96}," StopIteration",[36,36193,1730],{"class":50},[36,36195,36196,36198,36200,36202,36205,36207],{"class":38,"line":3092},[36,36197,1735],{"class":75},[36,36199,79],{"class":50},[36,36201,214],{"class":141},[36,36203,36204],{"class":145},"Iterator finished",[36,36206,214],{"class":141},[36,36208,86],{"class":50},[14,36210,107],{},[26,36212,36214],{"className":28,"code":36213,"language":30,"meta":31,"style":31},"100\n200\n300\nIterator finished\n",[33,36215,36216,36220,36225,36230],{"__ignoreMap":31},[36,36217,36218],{"class":38,"line":39},[36,36219,27377],{"class":54},[36,36221,36222],{"class":38,"line":72},[36,36223,36224],{"class":54},"200\n",[36,36226,36227],{"class":38,"line":89},[36,36228,36229],{"class":54},"300\n",[36,36231,36232],{"class":38,"line":496},[36,36233,36234],{"class":42},"Iterator finished\n",[21,36236,36238],{"id":36237},"when-beginners-should-care","When beginners should care",[14,36240,36241],{},"You do not need to use iterators directly all the time, but they matter in a few common situations:",[157,36243,36244,36250,36256,36262,36267],{},[160,36245,36246,36247,36249],{},"When learning how ",[33,36248,9852],{}," loops work",[160,36251,36252,36253,36255],{},"When using ",[33,36254,35434],{}," directly",[160,36257,36258,36259],{},"When working with ",[347,36260,36261],{"href":10302},"generator objects in Python",[160,36263,36264,36265],{},"When debugging ",[33,36266,35461],{},[160,36268,36269],{},"When dealing with one-time-use objects",[14,36271,36272,36273,36279],{},"Understanding iterators also helps when using tools like ",[347,36274,36276],{"href":36275},"\u002Freference\u002Fpython-enumerate-function-explained\u002F",[33,36277,36278],{},"enumerate()",", which work with iterable data.",[21,36281,36283],{"id":36282},"important-iterator-behavior","Important iterator behavior",[14,36285,36286],{},"There are a few behaviors beginners should know:",[157,36288,36289,36296,36299,36302],{},[160,36290,36291,36292,36295],{},"Most iterators are ",[331,36293,36294],{},"consumed"," as you use them",[160,36297,36298],{},"You usually cannot restart the same iterator from the beginning",[160,36300,36301],{},"If you need to loop again, create a new iterator",[160,36303,36304],{},"Not every iterable object is itself an iterator",[14,36306,197],{},[26,36308,36310],{"className":28,"code":36309,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\nit = iter(numbers)\n\nfor value in it:\n    print(value)\n\nprint(\"Loop again:\")\n\nfor value in it:\n    print(value)\n",[33,36311,36312,36332,36346,36350,36363,36373,36377,36391,36395,36407],{"__ignoreMap":31},[36,36313,36314,36316,36318,36320,36322,36324,36326,36328,36330],{"class":38,"line":39},[36,36315,43],{"class":42},[36,36317,47],{"class":46},[36,36319,51],{"class":50},[36,36321,55],{"class":54},[36,36323,58],{"class":50},[36,36325,61],{"class":54},[36,36327,58],{"class":50},[36,36329,66],{"class":54},[36,36331,69],{"class":50},[36,36333,36334,36336,36338,36340,36342,36344],{"class":38,"line":72},[36,36335,35351],{"class":42},[36,36337,47],{"class":46},[36,36339,34412],{"class":75},[36,36341,79],{"class":50},[36,36343,83],{"class":82},[36,36345,86],{"class":50},[36,36347,36348],{"class":38,"line":89},[36,36349,476],{"emptyLinePlaceholder":475},[36,36351,36352,36354,36356,36358,36361],{"class":38,"line":496},[36,36353,9852],{"class":1718},[36,36355,10853],{"class":42},[36,36357,9911],{"class":1718},[36,36359,36360],{"class":42}," it",[36,36362,1730],{"class":50},[36,36364,36365,36367,36369,36371],{"class":38,"line":501},[36,36366,1735],{"class":75},[36,36368,79],{"class":50},[36,36370,2686],{"class":82},[36,36372,86],{"class":50},[36,36374,36375],{"class":38,"line":522},[36,36376,476],{"emptyLinePlaceholder":475},[36,36378,36379,36381,36383,36385,36387,36389],{"class":38,"line":527},[36,36380,76],{"class":75},[36,36382,79],{"class":50},[36,36384,214],{"class":141},[36,36386,11073],{"class":145},[36,36388,214],{"class":141},[36,36390,86],{"class":50},[36,36392,36393],{"class":38,"line":3092},[36,36394,476],{"emptyLinePlaceholder":475},[36,36396,36397,36399,36401,36403,36405],{"class":38,"line":3118},[36,36398,9852],{"class":1718},[36,36400,10853],{"class":42},[36,36402,9911],{"class":1718},[36,36404,36360],{"class":42},[36,36406,1730],{"class":50},[36,36408,36409,36411,36413,36415],{"class":38,"line":3123},[36,36410,1735],{"class":75},[36,36412,79],{"class":50},[36,36414,2686],{"class":82},[36,36416,86],{"class":50},[14,36418,107],{},[26,36420,36421],{"className":28,"code":11110,"language":30,"meta":31,"style":31},[33,36422,36423,36427,36431,36435],{"__ignoreMap":31},[36,36424,36425],{"class":38,"line":39},[36,36426,9938],{"class":54},[36,36428,36429],{"class":38,"line":72},[36,36430,9943],{"class":54},[36,36432,36433],{"class":38,"line":89},[36,36434,9948],{"class":54},[36,36436,36437,36439],{"class":38,"line":496},[36,36438,11129],{"class":42},[36,36440,1730],{"class":50},[14,36442,36443],{},"The second loop prints nothing because the iterator is already exhausted.",[14,36445,36446],{},"To start over, create a new iterator:",[26,36448,36450],{"className":28,"code":36449,"language":30,"meta":31,"style":31},"numbers = [1, 2, 3]\n\nit1 = iter(numbers)\nit2 = iter(numbers)\n\nprint(next(it1))\nprint(next(it2))\n",[33,36451,36452,36472,36476,36491,36506,36510,36525],{"__ignoreMap":31},[36,36453,36454,36456,36458,36460,36462,36464,36466,36468,36470],{"class":38,"line":39},[36,36455,43],{"class":42},[36,36457,47],{"class":46},[36,36459,51],{"class":50},[36,36461,55],{"class":54},[36,36463,58],{"class":50},[36,36465,61],{"class":54},[36,36467,58],{"class":50},[36,36469,66],{"class":54},[36,36471,69],{"class":50},[36,36473,36474],{"class":38,"line":72},[36,36475,476],{"emptyLinePlaceholder":475},[36,36477,36478,36481,36483,36485,36487,36489],{"class":38,"line":89},[36,36479,36480],{"class":42},"it1 ",[36,36482,47],{"class":46},[36,36484,34412],{"class":75},[36,36486,79],{"class":50},[36,36488,83],{"class":82},[36,36490,86],{"class":50},[36,36492,36493,36496,36498,36500,36502,36504],{"class":38,"line":496},[36,36494,36495],{"class":42},"it2 ",[36,36497,47],{"class":46},[36,36499,34412],{"class":75},[36,36501,79],{"class":50},[36,36503,83],{"class":82},[36,36505,86],{"class":50},[36,36507,36508],{"class":38,"line":501},[36,36509,476],{"emptyLinePlaceholder":475},[36,36511,36512,36514,36516,36518,36520,36523],{"class":38,"line":522},[36,36513,76],{"class":75},[36,36515,79],{"class":50},[36,36517,11215],{"class":75},[36,36519,79],{"class":50},[36,36521,36522],{"class":82},"it1",[36,36524,104],{"class":50},[36,36526,36527,36529,36531,36533,36535,36538],{"class":38,"line":527},[36,36528,76],{"class":75},[36,36530,79],{"class":50},[36,36532,11215],{"class":75},[36,36534,79],{"class":50},[36,36536,36537],{"class":82},"it2",[36,36539,104],{"class":50},[14,36541,107],{},[26,36543,36545],{"className":28,"code":36544,"language":30,"meta":31,"style":31},"1\n1\n",[33,36546,36547,36551],{"__ignoreMap":31},[36,36548,36549],{"class":38,"line":39},[36,36550,9938],{"class":54},[36,36552,36553],{"class":38,"line":72},[36,36554,9938],{"class":54},[21,36556,905],{"id":904},[14,36558,8484],{},[157,36560,36561,36564,36573,36576,36579],{},[160,36562,36563],{},"Confusing iterable objects like lists with iterator objects",[160,36565,36566,36567,36569,36570],{},"Calling ",[33,36568,35434],{}," on a list instead of on ",[33,36571,36572],{},"iter(list)",[160,36574,36575],{},"Expecting an iterator to reset automatically",[160,36577,36578],{},"Using an iterator once and not realizing it is already exhausted",[160,36580,36581,36582,36584],{},"Not understanding why ",[33,36583,35461],{}," appears",[14,36586,36587,36588,36590],{},"For example, this does ",[331,36589,8193],{}," work:",[26,36592,36594],{"className":28,"code":36593,"language":30,"meta":31,"style":31},"numbers = [10, 20, 30]\nprint(next(numbers))\n",[33,36595,36596,36616],{"__ignoreMap":31},[36,36597,36598,36600,36602,36604,36606,36608,36610,36612,36614],{"class":38,"line":39},[36,36599,43],{"class":42},[36,36601,47],{"class":46},[36,36603,51],{"class":50},[36,36605,1560],{"class":54},[36,36607,58],{"class":50},[36,36609,1628],{"class":54},[36,36611,58],{"class":50},[36,36613,26390],{"class":54},[36,36615,69],{"class":50},[36,36617,36618,36620,36622,36624,36626,36628],{"class":38,"line":72},[36,36619,76],{"class":75},[36,36621,79],{"class":50},[36,36623,11215],{"class":75},[36,36625,79],{"class":50},[36,36627,83],{"class":82},[36,36629,104],{"class":50},[14,36631,36632],{},"It raises an error because a list is not an iterator.",[14,36634,4142],{},[26,36636,36638],{"className":28,"code":36637,"language":30,"meta":31,"style":31},"numbers = [10, 20, 30]\nit = iter(numbers)\nprint(next(it))\n",[33,36639,36640,36660,36674],{"__ignoreMap":31},[36,36641,36642,36644,36646,36648,36650,36652,36654,36656,36658],{"class":38,"line":39},[36,36643,43],{"class":42},[36,36645,47],{"class":46},[36,36647,51],{"class":50},[36,36649,1560],{"class":54},[36,36651,58],{"class":50},[36,36653,1628],{"class":54},[36,36655,58],{"class":50},[36,36657,26390],{"class":54},[36,36659,69],{"class":50},[36,36661,36662,36664,36666,36668,36670,36672],{"class":38,"line":72},[36,36663,35351],{"class":42},[36,36665,47],{"class":46},[36,36667,34412],{"class":75},[36,36669,79],{"class":50},[36,36671,83],{"class":82},[36,36673,86],{"class":50},[36,36675,36676,36678,36680,36682,36684,36686],{"class":38,"line":89},[36,36677,76],{"class":75},[36,36679,79],{"class":50},[36,36681,11215],{"class":75},[36,36683,79],{"class":50},[36,36685,35378],{"class":82},[36,36687,104],{"class":50},[14,36689,36690],{},"Useful things to check while debugging:",[26,36692,36694],{"className":28,"code":36693,"language":30,"meta":31,"style":31},"type(obj)\niter(obj)\nnext(it)\nhasattr(obj, '__iter__')\nhasattr(obj, '__next__')\n",[33,36695,36696,36707,36717,36727,36747],{"__ignoreMap":31},[36,36697,36698,36700,36702,36705],{"class":38,"line":39},[36,36699,97],{"class":96},[36,36701,79],{"class":50},[36,36703,36704],{"class":82},"obj",[36,36706,86],{"class":50},[36,36708,36709,36711,36713,36715],{"class":38,"line":72},[36,36710,35094],{"class":75},[36,36712,79],{"class":50},[36,36714,36704],{"class":82},[36,36716,86],{"class":50},[36,36718,36719,36721,36723,36725],{"class":38,"line":89},[36,36720,11215],{"class":75},[36,36722,79],{"class":50},[36,36724,35378],{"class":82},[36,36726,86],{"class":50},[36,36728,36729,36732,36734,36736,36738,36740,36743,36745],{"class":38,"line":496},[36,36730,36731],{"class":75},"hasattr",[36,36733,79],{"class":50},[36,36735,36704],{"class":82},[36,36737,58],{"class":50},[36,36739,142],{"class":141},[36,36741,36742],{"class":145},"__iter__",[36,36744,149],{"class":141},[36,36746,86],{"class":50},[36,36748,36749,36751,36753,36755,36757,36759,36762,36764],{"class":38,"line":501},[36,36750,36731],{"class":75},[36,36752,79],{"class":50},[36,36754,36704],{"class":82},[36,36756,58],{"class":50},[36,36758,142],{"class":141},[36,36760,36761],{"class":145},"__next__",[36,36763,149],{"class":141},[36,36765,86],{"class":50},[14,36767,36768],{},"In simple terms:",[157,36770,36771,36776],{},[160,36772,36773,36775],{},[33,36774,36742],{}," usually means an object can be iterated over",[160,36777,36778,36780],{},[33,36779,36761],{}," usually means it behaves like an iterator",[21,36782,1063],{"id":1062},[1065,36784,36786],{"id":36785},"is-a-list-an-iterator-in-python","Is a list an iterator in Python?",[14,36788,36789,36790,351],{},"No. A list is iterable, but it is not itself an iterator. You can create an iterator from it with ",[33,36791,36792],{},"iter(list_name)",[1065,36794,36796],{"id":36795},"what-is-the-difference-between-an-iterator-and-an-iterable","What is the difference between an iterator and an iterable?",[14,36798,36799],{},"An iterable can be looped over. An iterator returns items one at a time and keeps track of its current position.",[1065,36801,9361,36803,36805,36806,1248],{"id":36802},"why-does-next-give-stopiteration",[33,36804,35434],{}," give ",[33,36807,35461],{},[14,36809,36810],{},"It means the iterator has no more values left.",[1065,36812,36814],{"id":36813},"do-for-loops-use-iterators","Do for loops use iterators?",[14,36816,16248,36817,36819],{},[33,36818,9852],{}," loop uses the iterator protocol behind the scenes.",[1065,36821,36823],{"id":36822},"can-i-reuse-an-iterator","Can I reuse an iterator?",[14,36825,36826],{},"Usually no. Once it is exhausted, create a new iterator from the original iterable if possible.",[21,36828,1105],{"id":1104},[157,36830,36831,36835,36839,36843,36847],{},[160,36832,36833],{},[347,36834,14704],{"href":10935},[160,36836,36837],{},[347,36838,35222],{"href":34506},[160,36840,36841],{},[347,36842,11421],{"href":10302},[160,36844,36845],{},[347,36846,35868],{"href":35867},[160,36848,36849],{},[347,36850,1665,36851,2816],{"href":36275},[33,36852,36278],{},[1137,36854,36855],{},"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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":36857},[36858,36859,36860,36861,36863,36864,36865,36866,36867,36868,36876],{"id":23,"depth":72,"text":24},{"id":35438,"depth":72,"text":35439},{"id":35468,"depth":72,"text":35469},{"id":35589,"depth":72,"text":36862},"How iter() and next() work",{"id":35871,"depth":72,"text":35872},{"id":35966,"depth":72,"text":35967},{"id":36237,"depth":72,"text":36238},{"id":36282,"depth":72,"text":36283},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":36869},[36870,36871,36872,36874,36875],{"id":36785,"depth":89,"text":36786},{"id":36795,"depth":89,"text":36796},{"id":36802,"depth":89,"text":36873},"Why does next() give StopIteration?",{"id":36813,"depth":89,"text":36814},{"id":36822,"depth":89,"text":36823},{"id":1104,"depth":72,"text":1105},"Master what is an iterator in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-an-iterator-in-python",{"title":35285,"description":36877},"glossary\u002Fwhat-is-an-iterator-in-python","DfZz96A67zLDocRJuMtLCjoJc2LPigvmhye7LUCIaSk",{"id":36884,"title":36885,"body":36886,"description":38121,"extension":1159,"meta":38122,"navigation":475,"path":38123,"seo":38124,"stem":38125,"__hash__":38126},"content\u002Fglossary\u002Fwhat-is-an-object-in-python.md","What Is an Object in Python?",{"type":7,"value":36887,"toc":38103},[36888,36891,36897,36904,36921,36924,36926,36929,37024,37026,37073,37076,37097,37103,37105,37108,37110,37121,37124,37152,37156,37159,37191,37194,37212,37214,37278,37281,37299,37303,37306,37401,37403,37478,37481,37513,37518,37522,37528,37534,37536,37597,37599,37616,37618,37634,37637,37644,37648,37654,37657,37662,37693,37695,37703,37708,37753,37755,37775,37778,37789,37794,37796,37799,37802,37828,37831,37858,37871,37873,37877,37882,37892,37895,37898,37909,37911,37914,37948,37951,38012,38014,38025,38027,38031,38034,38038,38041,38045,38048,38052,38057,38059,38100],[10,36889,36885],{"id":36890},"what-is-an-object-in-python",[14,36892,36893,36894,36896],{},"In Python, an ",[331,36895,3364],{}," is a value that exists in memory.",[14,36898,36899,36900,36903],{},"This idea is important because ",[331,36901,36902],{},"everything you work with in Python is an object",". That includes:",[157,36905,36906,36908,36910,36913,36916,36919],{},[160,36907,83],{},[160,36909,30868],{},[160,36911,36912],{},"lists",[160,36914,36915],{},"dictionaries",[160,36917,36918],{},"booleans",[160,36920,4323],{},[14,36922,36923],{},"If you understand what an object is, it becomes easier to understand types, methods, and some common errors.",[21,36925,24],{"id":23},[14,36927,36928],{},"A quick way to see that different values in Python are objects with different types:",[26,36930,36932],{"className":28,"code":36931,"language":30,"meta":31,"style":31},"x = 10\nprint(type(x))\n\nname = \"Sam\"\nprint(type(name))\n\nitems = [1, 2, 3]\nprint(type(items))\n",[33,36933,36934,36942,36956,36960,36972,36986,36990,37010],{"__ignoreMap":31},[36,36935,36936,36938,36940],{"class":38,"line":39},[36,36937,5289],{"class":42},[36,36939,47],{"class":46},[36,36941,10662],{"class":54},[36,36943,36944,36946,36948,36950,36952,36954],{"class":38,"line":72},[36,36945,76],{"class":75},[36,36947,79],{"class":50},[36,36949,97],{"class":96},[36,36951,79],{"class":50},[36,36953,5306],{"class":82},[36,36955,104],{"class":50},[36,36957,36958],{"class":38,"line":89},[36,36959,476],{"emptyLinePlaceholder":475},[36,36961,36962,36964,36966,36968,36970],{"class":38,"line":496},[36,36963,1945],{"class":42},[36,36965,47],{"class":46},[36,36967,224],{"class":141},[36,36969,311],{"class":145},[36,36971,1954],{"class":141},[36,36973,36974,36976,36978,36980,36982,36984],{"class":38,"line":501},[36,36975,76],{"class":75},[36,36977,79],{"class":50},[36,36979,97],{"class":96},[36,36981,79],{"class":50},[36,36983,817],{"class":82},[36,36985,104],{"class":50},[36,36987,36988],{"class":38,"line":522},[36,36989,476],{"emptyLinePlaceholder":475},[36,36991,36992,36994,36996,36998,37000,37002,37004,37006,37008],{"class":38,"line":527},[36,36993,33459],{"class":42},[36,36995,47],{"class":46},[36,36997,51],{"class":50},[36,36999,55],{"class":54},[36,37001,58],{"class":50},[36,37003,61],{"class":54},[36,37005,58],{"class":50},[36,37007,66],{"class":54},[36,37009,69],{"class":50},[36,37011,37012,37014,37016,37018,37020,37022],{"class":38,"line":3092},[36,37013,76],{"class":75},[36,37015,79],{"class":50},[36,37017,97],{"class":96},[36,37019,79],{"class":50},[36,37021,17080],{"class":82},[36,37023,104],{"class":50},[14,37025,15916],{},[26,37027,37029],{"className":28,"code":37028,"language":30,"meta":31,"style":31},"\u003Cclass 'int'>\n\u003Cclass 'str'>\n\u003Cclass 'list'>\n",[33,37030,37031,37045,37059],{"__ignoreMap":31},[36,37032,37033,37035,37037,37039,37041,37043],{"class":38,"line":39},[36,37034,134],{"class":46},[36,37036,138],{"class":137},[36,37038,142],{"class":141},[36,37040,2466],{"class":145},[36,37042,149],{"class":141},[36,37044,152],{"class":46},[36,37046,37047,37049,37051,37053,37055,37057],{"class":38,"line":72},[36,37048,134],{"class":46},[36,37050,138],{"class":137},[36,37052,142],{"class":141},[36,37054,2360],{"class":145},[36,37056,149],{"class":141},[36,37058,152],{"class":46},[36,37060,37061,37063,37065,37067,37069,37071],{"class":38,"line":89},[36,37062,134],{"class":46},[36,37064,138],{"class":137},[36,37066,142],{"class":141},[36,37068,146],{"class":145},[36,37070,149],{"class":141},[36,37072,152],{"class":46},[14,37074,37075],{},"This shows that:",[157,37077,37078,37085,37091],{},[160,37079,37080,37082,37083],{},[33,37081,1560],{}," is an object of type ",[33,37084,2466],{},[160,37086,37087,37082,37089],{},[33,37088,27133],{},[33,37090,2360],{},[160,37092,37093,37082,37095],{},[33,37094,4889],{},[33,37096,146],{},[14,37098,32161,37099,351],{},[347,37100,1665,37101,2816],{"href":5265},[33,37102,1327],{},[21,37104,6101],{"id":6100},[14,37106,37107],{},"An object is a value stored in memory.",[14,37109,3270],{},[157,37111,37112,37115,37118],{},[160,37113,37114],{},"everything you work with is an object",[160,37116,37117],{},"objects have a type",[160,37119,37120],{},"different object types behave differently",[14,37122,37123],{},"Common examples of objects include:",[157,37125,37126,37131,37136,37141,37147],{},[160,37127,37128,37129],{},"numbers like ",[33,37130,1243],{},[160,37132,37133,37134],{},"strings like ",[33,37135,4854],{},[160,37137,37138,37139],{},"lists like ",[33,37140,4889],{},[160,37142,37143,37144],{},"dictionaries like ",[33,37145,37146],{},"{\"a\": 1}",[160,37148,37149,37150],{},"functions like ",[33,37151,76],{},[21,37153,37155],{"id":37154},"what-makes-something-an-object","What makes something an object",[14,37157,37158],{},"In Python, an object usually has a few basic parts:",[157,37160,37161,37175,37183],{},[160,37162,37163,37166,37168,37169,5950,37171,6128,37173,351],{},[331,37164,37165],{},"A type",[335,37167],{},"\nThe type tells Python what kind of object it is, such as ",[33,37170,2466],{},[33,37172,2360],{},[33,37174,146],{},[160,37176,37177,37180,37182],{},[331,37178,37179],{},"Data",[335,37181],{},"\nThe object stores some value or information.",[160,37184,37185,37188,37190],{},[331,37186,37187],{},"Behavior",[335,37189],{},"\nSome objects have methods you can use.",[14,37192,37193],{},"You can inspect an object with built-in functions such as:",[157,37195,37196,37201,37207],{},[160,37197,37198,37200],{},[33,37199,5799],{}," to see its type",[160,37202,37203,37206],{},[33,37204,37205],{},"id(value)"," to see its identity in memory",[160,37208,37209,37211],{},[33,37210,18020],{}," to see available attributes and methods",[14,37213,197],{},[26,37215,37217],{"className":28,"code":37216,"language":30,"meta":31,"style":31},"value = \"hello\"\n\nprint(type(value))\nprint(id(value))\nprint(dir(value))\n",[33,37218,37219,37231,37235,37249,37264],{"__ignoreMap":31},[36,37220,37221,37223,37225,37227,37229],{"class":38,"line":39},[36,37222,2664],{"class":42},[36,37224,47],{"class":46},[36,37226,224],{"class":141},[36,37228,2671],{"class":145},[36,37230,1954],{"class":141},[36,37232,37233],{"class":38,"line":72},[36,37234,476],{"emptyLinePlaceholder":475},[36,37236,37237,37239,37241,37243,37245,37247],{"class":38,"line":89},[36,37238,76],{"class":75},[36,37240,79],{"class":50},[36,37242,97],{"class":96},[36,37244,79],{"class":50},[36,37246,2686],{"class":82},[36,37248,104],{"class":50},[36,37250,37251,37253,37255,37258,37260,37262],{"class":38,"line":496},[36,37252,76],{"class":75},[36,37254,79],{"class":50},[36,37256,37257],{"class":75},"id",[36,37259,79],{"class":50},[36,37261,2686],{"class":82},[36,37263,104],{"class":50},[36,37265,37266,37268,37270,37272,37274,37276],{"class":38,"line":501},[36,37267,76],{"class":75},[36,37269,79],{"class":50},[36,37271,17985],{"class":75},[36,37273,79],{"class":50},[36,37275,2686],{"class":82},[36,37277,104],{"class":50},[14,37279,37280],{},"If you want to explore these tools, see:",[157,37282,37283,37291],{},[160,37284,37285],{},[347,37286,1665,37288,2816],{"href":37287},"\u002Freference\u002Fpython-dir-function-explained\u002F",[33,37289,37290],{},"dir()",[160,37292,37293],{},[347,37294,1665,37296,2816],{"href":37295},"\u002Freference\u002Fpython-id-function-explained\u002F",[33,37297,37298],{},"id()",[21,37300,37302],{"id":37301},"common-examples-of-objects","Common examples of objects",[14,37304,37305],{},"Here are some common Python objects:",[26,37307,37309],{"className":28,"code":37308,"language":30,"meta":31,"style":31},"print(type(5))\nprint(type(\"hello\"))\nprint(type([1, 2, 3]))\nprint(type({\"a\": 1}))\nprint(type(True))\n",[33,37310,37311,37325,37343,37365,37387],{"__ignoreMap":31},[36,37312,37313,37315,37317,37319,37321,37323],{"class":38,"line":39},[36,37314,76],{"class":75},[36,37316,79],{"class":50},[36,37318,97],{"class":96},[36,37320,79],{"class":50},[36,37322,1243],{"class":54},[36,37324,104],{"class":50},[36,37326,37327,37329,37331,37333,37335,37337,37339,37341],{"class":38,"line":72},[36,37328,76],{"class":75},[36,37330,79],{"class":50},[36,37332,97],{"class":96},[36,37334,79],{"class":50},[36,37336,214],{"class":141},[36,37338,2671],{"class":145},[36,37340,214],{"class":141},[36,37342,104],{"class":50},[36,37344,37345,37347,37349,37351,37353,37355,37357,37359,37361,37363],{"class":38,"line":89},[36,37346,76],{"class":75},[36,37348,79],{"class":50},[36,37350,97],{"class":96},[36,37352,5490],{"class":50},[36,37354,55],{"class":54},[36,37356,58],{"class":50},[36,37358,61],{"class":54},[36,37360,58],{"class":50},[36,37362,66],{"class":54},[36,37364,5503],{"class":50},[36,37366,37367,37369,37371,37373,37375,37377,37379,37381,37383,37385],{"class":38,"line":496},[36,37368,76],{"class":75},[36,37370,79],{"class":50},[36,37372,97],{"class":96},[36,37374,19005],{"class":50},[36,37376,214],{"class":141},[36,37378,347],{"class":145},[36,37380,214],{"class":141},[36,37382,822],{"class":50},[36,37384,16839],{"class":54},[36,37386,19022],{"class":50},[36,37388,37389,37391,37393,37395,37397,37399],{"class":38,"line":501},[36,37390,76],{"class":75},[36,37392,79],{"class":50},[36,37394,97],{"class":96},[36,37396,79],{"class":50},[36,37398,1179],{"class":400},[36,37400,104],{"class":50},[14,37402,15916],{},[26,37404,37406],{"className":28,"code":37405,"language":30,"meta":31,"style":31},"\u003Cclass 'int'>\n\u003Cclass 'str'>\n\u003Cclass 'list'>\n\u003Cclass 'dict'>\n\u003Cclass 'bool'>\n",[33,37407,37408,37422,37436,37450,37464],{"__ignoreMap":31},[36,37409,37410,37412,37414,37416,37418,37420],{"class":38,"line":39},[36,37411,134],{"class":46},[36,37413,138],{"class":137},[36,37415,142],{"class":141},[36,37417,2466],{"class":145},[36,37419,149],{"class":141},[36,37421,152],{"class":46},[36,37423,37424,37426,37428,37430,37432,37434],{"class":38,"line":72},[36,37425,134],{"class":46},[36,37427,138],{"class":137},[36,37429,142],{"class":141},[36,37431,2360],{"class":145},[36,37433,149],{"class":141},[36,37435,152],{"class":46},[36,37437,37438,37440,37442,37444,37446,37448],{"class":38,"line":89},[36,37439,134],{"class":46},[36,37441,138],{"class":137},[36,37443,142],{"class":141},[36,37445,146],{"class":145},[36,37447,149],{"class":141},[36,37449,152],{"class":46},[36,37451,37452,37454,37456,37458,37460,37462],{"class":38,"line":496},[36,37453,134],{"class":46},[36,37455,138],{"class":137},[36,37457,142],{"class":141},[36,37459,23078],{"class":145},[36,37461,149],{"class":141},[36,37463,152],{"class":46},[36,37465,37466,37468,37470,37472,37474,37476],{"class":38,"line":501},[36,37467,134],{"class":46},[36,37469,138],{"class":137},[36,37471,142],{"class":141},[36,37473,1378],{"class":145},[36,37475,149],{"class":141},[36,37477,152],{"class":46},[14,37479,37480],{},"So:",[157,37482,37483,37489,37495,37501,37507],{},[160,37484,37485,8621,37487,17785],{},[33,37486,1243],{},[33,37488,2466],{},[160,37490,37491,8627,37493,17785],{},[33,37492,4854],{},[33,37494,2360],{},[160,37496,37497,8627,37499,17785],{},[33,37498,4889],{},[33,37500,146],{},[160,37502,37503,8627,37505,17785],{},[33,37504,37146],{},[33,37506,23078],{},[160,37508,37509,8627,37511,17785],{},[33,37510,1179],{},[33,37512,1378],{},[14,37514,37515,37516,351],{},"For a broader overview, see ",[347,37517,2895],{"href":2894},[21,37519,37521],{"id":37520},"objects-and-variables","Objects and variables",[14,37523,37524,37525,37527],{},"A variable is ",[331,37526,8193],{}," the object itself.",[14,37529,37530,37531,351],{},"A variable is just a ",[331,37532,37533],{},"name that refers to an object",[14,37535,197],{},[26,37537,37539],{"className":28,"code":37538,"language":30,"meta":31,"style":31},"x = 10\ny = x\n\nprint(x)\nprint(y)\nprint(x is y)\n",[33,37540,37541,37549,37558,37562,37572,37582],{"__ignoreMap":31},[36,37542,37543,37545,37547],{"class":38,"line":39},[36,37544,5289],{"class":42},[36,37546,47],{"class":46},[36,37548,10662],{"class":54},[36,37550,37551,37554,37556],{"class":38,"line":72},[36,37552,37553],{"class":42},"y ",[36,37555,47],{"class":46},[36,37557,11532],{"class":42},[36,37559,37560],{"class":38,"line":89},[36,37561,476],{"emptyLinePlaceholder":475},[36,37563,37564,37566,37568,37570],{"class":38,"line":496},[36,37565,76],{"class":75},[36,37567,79],{"class":50},[36,37569,5306],{"class":82},[36,37571,86],{"class":50},[36,37573,37574,37576,37578,37580],{"class":38,"line":501},[36,37575,76],{"class":75},[36,37577,79],{"class":50},[36,37579,34734],{"class":82},[36,37581,86],{"class":50},[36,37583,37584,37586,37588,37590,37592,37595],{"class":38,"line":522},[36,37585,76],{"class":75},[36,37587,79],{"class":50},[36,37589,5289],{"class":82},[36,37591,8960],{"class":1718},[36,37593,37594],{"class":82}," y",[36,37596,86],{"class":50},[14,37598,15916],{},[26,37600,37602],{"className":28,"code":37601,"language":30,"meta":31,"style":31},"10\n10\nTrue\n",[33,37603,37604,37608,37612],{"__ignoreMap":31},[36,37605,37606],{"class":38,"line":39},[36,37607,10792],{"class":54},[36,37609,37610],{"class":38,"line":72},[36,37611,10792],{"class":54},[36,37613,37614],{"class":38,"line":89},[36,37615,1481],{"class":400},[14,37617,6187],{},[157,37619,37620,37624,37629],{},[160,37621,37622,4074],{},[33,37623,1560],{},[160,37625,37626,37628],{},[33,37627,5306],{}," refers to that object",[160,37630,37631,37633],{},[33,37632,34734],{}," also refers to that same object",[14,37635,37636],{},"So two variables can point to the same object.",[14,37638,37639,37640,37643],{},"This is one reason beginners sometimes get confused. They may think the variable ",[18466,37641,37642],{},"contains"," the value in a simple way, but in Python it is more accurate to say the variable refers to an object.",[21,37645,37647],{"id":37646},"objects-and-methods","Objects and methods",[14,37649,37650,37651,351],{},"Some objects have methods that you call with ",[331,37652,37653],{},"dot notation",[14,37655,37656],{},"A method is a function connected to an object.",[14,37658,37659,37660,822],{},"For example, a string object has methods like ",[33,37661,16376],{},[26,37663,37665],{"className":28,"code":37664,"language":30,"meta":31,"style":31},"text = \"hello\"\nprint(text.upper())\n",[33,37666,37667,37679],{"__ignoreMap":31},[36,37668,37669,37671,37673,37675,37677],{"class":38,"line":39},[36,37670,9407],{"class":42},[36,37672,47],{"class":46},[36,37674,224],{"class":141},[36,37676,2671],{"class":145},[36,37678,1954],{"class":141},[36,37680,37681,37683,37685,37687,37689,37691],{"class":38,"line":72},[36,37682,76],{"class":75},[36,37684,79],{"class":50},[36,37686,4695],{"class":82},[36,37688,351],{"class":50},[36,37690,13483],{"class":82},[36,37692,4387],{"class":50},[14,37694,107],{},[26,37696,37697],{"className":28,"code":17798,"language":30,"meta":31,"style":31},[33,37698,37699],{"__ignoreMap":31},[36,37700,37701],{"class":38,"line":39},[36,37702,17798],{"class":3059},[14,37704,37705,37706,822],{},"A list object has methods like ",[33,37707,13210],{},[26,37709,37711],{"className":28,"code":37710,"language":30,"meta":31,"style":31},"items = [1, 2]\nitems.append(3)\nprint(items)\n",[33,37712,37713,37729,37743],{"__ignoreMap":31},[36,37714,37715,37717,37719,37721,37723,37725,37727],{"class":38,"line":39},[36,37716,33459],{"class":42},[36,37718,47],{"class":46},[36,37720,51],{"class":50},[36,37722,55],{"class":54},[36,37724,58],{"class":50},[36,37726,61],{"class":54},[36,37728,69],{"class":50},[36,37730,37731,37733,37735,37737,37739,37741],{"class":38,"line":72},[36,37732,17080],{"class":42},[36,37734,351],{"class":50},[36,37736,508],{"class":82},[36,37738,79],{"class":50},[36,37740,1247],{"class":54},[36,37742,86],{"class":50},[36,37744,37745,37747,37749,37751],{"class":38,"line":89},[36,37746,76],{"class":75},[36,37748,79],{"class":50},[36,37750,17080],{"class":82},[36,37752,86],{"class":50},[14,37754,107],{},[26,37756,37757],{"className":28,"code":10569,"language":30,"meta":31,"style":31},[33,37758,37759],{"__ignoreMap":31},[36,37760,37761,37763,37765,37767,37769,37771,37773],{"class":38,"line":39},[36,37762,117],{"class":50},[36,37764,55],{"class":54},[36,37766,58],{"class":50},[36,37768,61],{"class":54},[36,37770,58],{"class":50},[36,37772,66],{"class":54},[36,37774,69],{"class":50},[14,37776,37777],{},"This helps explain why different objects behave differently:",[157,37779,37780,37783,37786],{},[160,37781,37782],{},"strings have string methods",[160,37784,37785],{},"lists have list methods",[160,37787,37788],{},"integers do not have the same methods as strings or lists",[14,37790,7160,37791,351],{},[347,37792,37793],{"href":3869},"what is a method in Python",[21,37795,9976],{"id":9975},[14,37797,37798],{},"Understanding objects helps with many basic Python ideas.",[14,37800,37801],{},"It helps you understand:",[157,37803,37804,37807,37814,37819,37822],{},[160,37805,37806],{},"why different values have different types",[160,37808,37809,37810,37813],{},"why ",[33,37811,37812],{},"\"hello\".upper()"," works",[160,37815,37809,37816,37813],{},[33,37817,37818],{},"[1, 2].append(3)",[160,37820,37821],{},"why some method calls fail on the wrong type",[160,37823,37824,37825,37827],{},"why errors like ",[347,37826,17869],{"href":17865}," happen",[14,37829,37830],{},"For example, this causes an error:",[26,37832,37834],{"className":28,"code":37833,"language":30,"meta":31,"style":31},"number = 10\nprint(number.upper())\n",[33,37835,37836,37844],{"__ignoreMap":31},[36,37837,37838,37840,37842],{"class":38,"line":39},[36,37839,8343],{"class":42},[36,37841,47],{"class":46},[36,37843,10662],{"class":54},[36,37845,37846,37848,37850,37852,37854,37856],{"class":38,"line":72},[36,37847,76],{"class":75},[36,37849,79],{"class":50},[36,37851,8368],{"class":82},[36,37853,351],{"class":50},[36,37855,13483],{"class":82},[36,37857,4387],{"class":50},[14,37859,37860,37861,8621,37863,37865,37866,37868,37869,17855],{},"Why? Because ",[33,37862,1560],{},[33,37864,2466],{}," object, and ",[33,37867,2466],{}," objects do not have an ",[33,37870,16376],{},[21,37872,861],{"id":860},[14,37874,25362,37875,25365],{},[331,37876,3364],{},[14,37878,9843,37879,37881],{},[331,37880,8193],{}," fully explain:",[157,37883,37884,37886,37889],{},[160,37885,18165],{},[160,37887,37888],{},"object-oriented programming",[160,37890,37891],{},"custom objects you create yourself",[14,37893,37894],{},"Those topics are separate and are better learned step by step.",[14,37896,37897],{},"If you want to continue, see:",[157,37899,37900,37905],{},[160,37901,37902],{},[347,37903,37904],{"href":17346},"what is a class in Python",[160,37906,37907],{},[347,37908,3343],{"href":3342},[21,37910,905],{"id":904},[14,37912,37913],{},"Beginners often run into these misunderstandings:",[157,37915,37916,37924,37932,37940],{},[160,37917,37918,37921,37923],{},[331,37919,37920],{},"Thinking only class instances are objects",[335,37922],{},"\nIn Python, basic values like integers and strings are objects too.",[160,37925,37926,37929,37931],{},[331,37927,37928],{},"Confusing a variable name with the object it refers to",[335,37930],{},"\nThe variable is just a name.",[160,37933,37934,37937,37939],{},[331,37935,37936],{},"Assuming all objects have the same methods",[335,37938],{},"\nDifferent object types have different methods.",[160,37941,37942,37945,37947],{},[331,37943,37944],{},"Trying to use a list method on a string or a string method on an int",[335,37946],{},"\nMethods depend on the type of object.",[14,37949,37950],{},"Useful commands for checking an object:",[26,37952,37954],{"className":28,"code":37953,"language":30,"meta":31,"style":31},"print(type(value))\nprint(id(value))\nprint(dir(value))\nhelp(type(value))\n",[33,37955,37956,37970,37984,37998],{"__ignoreMap":31},[36,37957,37958,37960,37962,37964,37966,37968],{"class":38,"line":39},[36,37959,76],{"class":75},[36,37961,79],{"class":50},[36,37963,97],{"class":96},[36,37965,79],{"class":50},[36,37967,2686],{"class":82},[36,37969,104],{"class":50},[36,37971,37972,37974,37976,37978,37980,37982],{"class":38,"line":72},[36,37973,76],{"class":75},[36,37975,79],{"class":50},[36,37977,37257],{"class":75},[36,37979,79],{"class":50},[36,37981,2686],{"class":82},[36,37983,104],{"class":50},[36,37985,37986,37988,37990,37992,37994,37996],{"class":38,"line":89},[36,37987,76],{"class":75},[36,37989,79],{"class":50},[36,37991,17985],{"class":75},[36,37993,79],{"class":50},[36,37995,2686],{"class":82},[36,37997,104],{"class":50},[36,37999,38000,38002,38004,38006,38008,38010],{"class":38,"line":496},[36,38001,4392],{"class":75},[36,38003,79],{"class":50},[36,38005,97],{"class":96},[36,38007,79],{"class":50},[36,38009,2686],{"class":82},[36,38011,104],{"class":50},[14,38013,7244],{},[157,38015,38016,38019,38022],{},[160,38017,38018],{},"What kind of object is this?",[160,38020,38021],{},"What methods does it have?",[160,38023,38024],{},"Am I using the right method for this type?",[21,38026,1063],{"id":1062},[1065,38028,38030],{"id":38029},"is-everything-in-python-really-an-object","Is everything in Python really an object?",[14,38032,38033],{},"Yes. Common values like integers, strings, lists, functions, and even classes are objects in Python.",[1065,38035,38037],{"id":38036},"is-a-variable-an-object","Is a variable an object?",[14,38039,38040],{},"No. A variable is a name that points to an object.",[1065,38042,38044],{"id":38043},"is-a-class-the-same-as-an-object","Is a class the same as an object?",[14,38046,38047],{},"A class is also an object in Python, but beginners usually learn it as a blueprint used to create other objects.",[1065,38049,38051],{"id":38050},"how-do-i-check-what-kind-of-object-i-have","How do I check what kind of object I have?",[14,38053,2489,38054,38056],{},[33,38055,5799],{}," to see the object's type.",[21,38058,1105],{"id":1104},[157,38060,38061,38065,38069,38073,38077,38083,38089,38095],{},[160,38062,38063],{},[347,38064,18087],{"href":17346},[160,38066,38067],{},[347,38068,4632],{"href":3869},[160,38070,38071],{},[347,38072,3343],{"href":3342},[160,38074,38075],{},[347,38076,2895],{"href":2894},[160,38078,38079],{},[347,38080,1665,38081,2816],{"href":5265},[33,38082,1327],{},[160,38084,38085],{},[347,38086,1665,38087,2816],{"href":37287},[33,38088,37290],{},[160,38090,38091],{},[347,38092,1665,38093,2816],{"href":37295},[33,38094,37298],{},[160,38096,38097],{},[347,38098,38099],{"href":17865},"AttributeError: object has no attribute fix",[1137,38101,38102],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":38104},[38105,38106,38107,38108,38109,38110,38111,38112,38113,38114,38120],{"id":23,"depth":72,"text":24},{"id":6100,"depth":72,"text":6101},{"id":37154,"depth":72,"text":37155},{"id":37301,"depth":72,"text":37302},{"id":37520,"depth":72,"text":37521},{"id":37646,"depth":72,"text":37647},{"id":9975,"depth":72,"text":9976},{"id":860,"depth":72,"text":861},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":38115},[38116,38117,38118,38119],{"id":38029,"depth":89,"text":38030},{"id":38036,"depth":89,"text":38037},{"id":38043,"depth":89,"text":38044},{"id":38050,"depth":89,"text":38051},{"id":1104,"depth":72,"text":1105},"Master what is an object in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-an-object-in-python",{"title":36885,"description":38121},"glossary\u002Fwhat-is-an-object-in-python","EkzZ71_RHsLR-HsjqAB81d3FIq9Zih76uayWfl5kYPo",{"id":38128,"title":38129,"body":38130,"description":39124,"extension":1159,"meta":39125,"navigation":475,"path":39126,"seo":39127,"stem":39128,"__hash__":39129},"content\u002Fglossary\u002Fwhat-is-error-handling-in-python.md","What Is Error Handling in Python?",{"type":7,"value":38131,"toc":39097},[38132,38135,38138,38141,38148,38150,38153,38247,38249,38274,38277,38281,38284,38287,38301,38310,38314,38317,38320,38334,38337,38354,38358,38361,38382,38384,38401,38405,38408,38412,38417,38421,38426,38430,38437,38441,38446,38451,38455,38458,38537,38541,38563,38565,38570,38576,38581,38587,38590,38593,38687,38694,38698,38701,38718,38721,38770,38773,38819,38822,38824,38827,38846,38851,38856,38858,38875,38882,38884,38887,38890,38905,38914,38923,38927,38930,38957,38960,38972,39021,39024,39026,39030,39033,39037,39040,39044,39047,39051,39054,39058,39061,39063,39094],[10,38133,38129],{"id":38134},"what-is-error-handling-in-python",[14,38136,38137],{},"Error handling is the way a Python program deals with problems while it is running.",[14,38139,38140],{},"Instead of crashing as soon as something goes wrong, your code can respond in a controlled way. This is useful when working with user input, files, calculations, and other data that may not always be valid.",[14,38142,38143,38144,1281,38146,351],{},"In Python, error handling is commonly done with ",[33,38145,29606],{},[33,38147,29609],{},[21,38149,29613],{"id":29612},[14,38151,38152],{},"Here is a simple example of error handling:",[26,38154,38156],{"className":28,"code":38155,"language":30,"meta":31,"style":31},"try:\n    number = int(input(\"Enter a number: \"))\n    print(10 \u002F number)\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n",[33,38157,38158,38164,38186,38200,38208,38223,38232],{"__ignoreMap":31},[36,38159,38160,38162],{"class":38,"line":39},[36,38161,29606],{"class":1718},[36,38163,1730],{"class":50},[36,38165,38166,38168,38170,38172,38174,38176,38178,38180,38182,38184],{"class":38,"line":72},[36,38167,29632],{"class":42},[36,38169,47],{"class":46},[36,38171,5739],{"class":96},[36,38173,79],{"class":50},[36,38175,25474],{"class":75},[36,38177,79],{"class":50},[36,38179,214],{"class":141},[36,38181,32877],{"class":145},[36,38183,214],{"class":141},[36,38185,104],{"class":50},[36,38187,38188,38190,38192,38194,38196,38198],{"class":38,"line":89},[36,38189,1735],{"class":75},[36,38191,79],{"class":50},[36,38193,1560],{"class":54},[36,38195,7730],{"class":46},[36,38197,10229],{"class":82},[36,38199,86],{"class":50},[36,38201,38202,38204,38206],{"class":38,"line":496},[36,38203,29609],{"class":1718},[36,38205,29654],{"class":96},[36,38207,1730],{"class":50},[36,38209,38210,38212,38214,38216,38219,38221],{"class":38,"line":501},[36,38211,1735],{"class":75},[36,38213,79],{"class":50},[36,38215,214],{"class":141},[36,38217,38218],{"class":145},"Please enter a valid whole number.",[36,38220,214],{"class":141},[36,38222,86],{"class":50},[36,38224,38225,38227,38230],{"class":38,"line":522},[36,38226,29609],{"class":1718},[36,38228,38229],{"class":96}," ZeroDivisionError",[36,38231,1730],{"class":50},[36,38233,38234,38236,38238,38240,38243,38245],{"class":38,"line":527},[36,38235,1735],{"class":75},[36,38237,79],{"class":50},[36,38239,214],{"class":141},[36,38241,38242],{"class":145},"You cannot divide by zero.",[36,38244,214],{"class":141},[36,38246,86],{"class":50},[14,38248,8121],{},[157,38250,38251,38257,38265,38271],{},[160,38252,38253,38254,38256],{},"The code in ",[33,38255,29606],{}," might fail.",[160,38258,38259,38260,38262,38263,351],{},"If the user enters text like ",[33,38261,4854],{},", Python raises a ",[33,38264,8416],{},[160,38266,32668,38267,38262,38269,351],{},[33,38268,490],{},[33,38270,30224],{},[160,38272,38273],{},"Instead of showing a long traceback, the program prints a clear message.",[14,38275,38276],{},"This is the basic idea of error handling: prepare for problems and respond safely.",[21,38278,38280],{"id":38279},"what-error-handling-means","What error handling means",[14,38282,38283],{},"Error handling is how a program deals with problems during runtime.",[14,38285,38286],{},"It helps your code:",[157,38288,38289,38292,38295,38298],{},[160,38290,38291],{},"respond to errors instead of stopping suddenly",[160,38293,38294],{},"show useful messages",[160,38296,38297],{},"recover when possible",[160,38299,38300],{},"stop safely when needed",[14,38302,38303,38304,38307,38308,351],{},"In Python, these problems are usually called ",[331,38305,38306],{},"exceptions",". If that term is new, see ",[347,38309,29761],{"href":29760},[21,38311,38313],{"id":38312},"why-error-handling-is-useful","Why error handling is useful",[14,38315,38316],{},"Error handling is useful because real programs often deal with unpredictable input and data.",[14,38318,38319],{},"It can help you:",[157,38321,38322,38325,38328,38331],{},[160,38323,38324],{},"prevent the whole program from crashing on common mistakes",[160,38326,38327],{},"show clear messages to the user",[160,38329,38330],{},"continue running when that makes sense",[160,38332,38333],{},"stop safely when continuing would cause bigger problems",[14,38335,38336],{},"This is especially important when working with:",[157,38338,38339,38342,38345,38348,38351],{},[160,38340,38341],{},"user input",[160,38343,38344],{},"files",[160,38346,38347],{},"web data",[160,38349,38350],{},"external APIs",[160,38352,38353],{},"calculations",[21,38355,38357],{"id":38356},"common-situations-where-errors-happen","Common situations where errors happen",[14,38359,38360],{},"Beginners often see errors in situations like these:",[157,38362,38363,38370,38373,38376,38379],{},[160,38364,38365,38366,1180,38368],{},"converting text to a number with ",[33,38367,25347],{},[33,38369,7692],{},[160,38371,38372],{},"opening a file that does not exist",[160,38374,38375],{},"using a list index that is too large",[160,38377,38378],{},"looking up a missing dictionary key",[160,38380,38381],{},"dividing by zero",[14,38383,694],{},[157,38385,38386,38389,38392,38395,38398],{},[160,38387,38388],{},"User enters text when a number is expected",[160,38390,38391],{},"Program tries to open a missing file",[160,38393,38394],{},"Code uses an invalid list index",[160,38396,38397],{},"Program divides by zero",[160,38399,38400],{},"Code accesses a missing dictionary key",[21,38402,38404],{"id":38403},"basic-parts-of-python-error-handling","Basic parts of Python error handling",[14,38406,38407],{},"Python gives you a few main tools for handling errors.",[1065,38409,38410],{"id":29606},[33,38411,29606],{},[14,38413,3542,38414,38416],{},[33,38415,29606],{}," block contains code that may cause an error.",[1065,38418,38419],{"id":29609},[33,38420,29609],{},[14,38422,3542,38423,38425],{},[33,38424,29609],{}," block runs if an error happens.",[1065,38427,38428],{"id":30077},[33,38429,30077],{},[14,38431,3542,38432,38434,38435,351],{},[33,38433,30077],{}," block runs only if no error happens in ",[33,38436,29606],{},[1065,38438,38439],{"id":30080},[33,38440,30080],{},[14,38442,3542,38443,38445],{},[33,38444,30080],{}," block runs whether there is an error or not.",[14,38447,38448,38449,351],{},"If you want the full syntax and examples, read ",[347,38450,30085],{"href":30084},[21,38452,38454],{"id":38453},"beginner-example-to-include","Beginner example to include",[14,38456,38457],{},"A common beginner example is asking the user for a number.",[26,38459,38461],{"className":28,"code":38460,"language":30,"meta":31,"style":31},"try:\n    number = int(input(\"Enter a number: \"))\n    print(\"Double:\", number * 2)\nexcept ValueError:\n    print(\"That was not a valid whole number.\")\n",[33,38462,38463,38469,38491,38514,38522],{"__ignoreMap":31},[36,38464,38465,38467],{"class":38,"line":39},[36,38466,29606],{"class":1718},[36,38468,1730],{"class":50},[36,38470,38471,38473,38475,38477,38479,38481,38483,38485,38487,38489],{"class":38,"line":72},[36,38472,29632],{"class":42},[36,38474,47],{"class":46},[36,38476,5739],{"class":96},[36,38478,79],{"class":50},[36,38480,25474],{"class":75},[36,38482,79],{"class":50},[36,38484,214],{"class":141},[36,38486,32877],{"class":145},[36,38488,214],{"class":141},[36,38490,104],{"class":50},[36,38492,38493,38495,38497,38499,38502,38504,38506,38508,38510,38512],{"class":38,"line":89},[36,38494,1735],{"class":75},[36,38496,79],{"class":50},[36,38498,214],{"class":141},[36,38500,38501],{"class":145},"Double:",[36,38503,214],{"class":141},[36,38505,58],{"class":50},[36,38507,10205],{"class":82},[36,38509,10824],{"class":46},[36,38511,61],{"class":54},[36,38513,86],{"class":50},[36,38515,38516,38518,38520],{"class":38,"line":496},[36,38517,29609],{"class":1718},[36,38519,29654],{"class":96},[36,38521,1730],{"class":50},[36,38523,38524,38526,38528,38530,38533,38535],{"class":38,"line":501},[36,38525,1735],{"class":75},[36,38527,79],{"class":50},[36,38529,214],{"class":141},[36,38531,38532],{"class":145},"That was not a valid whole number.",[36,38534,214],{"class":141},[36,38536,86],{"class":50},[1065,38538,38540],{"id":38539},"what-happens-here","What happens here",[157,38542,38543,38548,38553,38558],{},[160,38544,38545,38547],{},[33,38546,9395],{}," gives you text",[160,38549,38550,38552],{},[33,38551,25347],{}," tries to convert that text to a whole number",[160,38554,38555,38556],{},"if the text is not a valid number, Python raises ",[33,38557,8416],{},[160,38559,3634,38560,38562],{},[33,38561,29609],{}," block shows a friendly message",[14,38564,197],{},[14,38566,38567],{},[331,38568,38569],{},"Input",[26,38571,38574],{"className":38572,"code":38573,"language":4695,"meta":31},[19220],"abc\n",[33,38575,38573],{"__ignoreMap":31},[14,38577,38578],{},[331,38579,38580],{},"Output",[26,38582,38585],{"className":38583,"code":38584,"language":4695,"meta":31},[19220],"That was not a valid whole number.\n",[33,38586,38584],{"__ignoreMap":31},[14,38588,38589],{},"Without error handling, the program would stop and show a traceback.",[14,38591,38592],{},"Here is a second simple example with a file:",[26,38594,38596],{"className":28,"code":38595,"language":30,"meta":31,"style":31},"try:\n    with open(\"notes.txt\", \"r\") as file:\n        content = file.read()\n        print(content)\nexcept FileNotFoundError:\n    print(\"The file was not found.\")\n",[33,38597,38598,38604,38636,38652,38663,38672],{"__ignoreMap":31},[36,38599,38600,38602],{"class":38,"line":39},[36,38601,29606],{"class":1718},[36,38603,1730],{"class":50},[36,38605,38606,38609,38611,38613,38615,38617,38619,38621,38623,38626,38628,38630,38632,38634],{"class":38,"line":72},[36,38607,38608],{"class":1718},"    with",[36,38610,34215],{"class":75},[36,38612,79],{"class":50},[36,38614,214],{"class":141},[36,38616,24715],{"class":145},[36,38618,214],{"class":141},[36,38620,58],{"class":50},[36,38622,224],{"class":141},[36,38624,38625],{"class":145},"r",[36,38627,214],{"class":141},[36,38629,1988],{"class":50},[36,38631,34229],{"class":1718},[36,38633,18456],{"class":34232},[36,38635,1730],{"class":50},[36,38637,38638,38641,38643,38645,38647,38650],{"class":38,"line":89},[36,38639,38640],{"class":42},"        content ",[36,38642,47],{"class":46},[36,38644,18456],{"class":34232},[36,38646,351],{"class":50},[36,38648,38649],{"class":82},"read",[36,38651,3170],{"class":50},[36,38653,38654,38656,38658,38661],{"class":38,"line":496},[36,38655,3095],{"class":75},[36,38657,79],{"class":50},[36,38659,38660],{"class":82},"content",[36,38662,86],{"class":50},[36,38664,38665,38667,38670],{"class":38,"line":501},[36,38666,29609],{"class":1718},[36,38668,38669],{"class":96}," FileNotFoundError",[36,38671,1730],{"class":50},[36,38673,38674,38676,38678,38680,38683,38685],{"class":38,"line":522},[36,38675,1735],{"class":75},[36,38677,79],{"class":50},[36,38679,214],{"class":141},[36,38681,38682],{"class":145},"The file was not found.",[36,38684,214],{"class":141},[36,38686,86],{"class":50},[14,38688,35004,38689,38691,38692,351],{},[33,38690,24715],{}," does not exist, the program handles the problem instead of crashing. For more help with this specific problem, see ",[347,38693,30291],{"href":30290},[21,38695,38697],{"id":38696},"good-beginner-habits","Good beginner habits",[14,38699,38700],{},"When you start using error handling, these habits will help:",[157,38702,38703,38706,38712,38715],{},[160,38704,38705],{},"catch specific errors when possible",[160,38707,38708,38709,38711],{},"keep the ",[33,38710,29606],{}," block small",[160,38713,38714],{},"write messages that clearly explain what went wrong",[160,38716,38717],{},"only handle errors you understand",[14,38719,38720],{},"For example, this is better:",[26,38722,38724],{"className":28,"code":38723,"language":30,"meta":31,"style":31},"try:\n    number = int(user_input)\nexcept ValueError:\n    print(\"Please enter a valid number.\")\n",[33,38725,38726,38732,38747,38755],{"__ignoreMap":31},[36,38727,38728,38730],{"class":38,"line":39},[36,38729,29606],{"class":1718},[36,38731,1730],{"class":50},[36,38733,38734,38736,38738,38740,38742,38745],{"class":38,"line":72},[36,38735,29632],{"class":42},[36,38737,47],{"class":46},[36,38739,5739],{"class":96},[36,38741,79],{"class":50},[36,38743,38744],{"class":82},"user_input",[36,38746,86],{"class":50},[36,38748,38749,38751,38753],{"class":38,"line":89},[36,38750,29609],{"class":1718},[36,38752,29654],{"class":96},[36,38754,1730],{"class":50},[36,38756,38757,38759,38761,38763,38766,38768],{"class":38,"line":496},[36,38758,1735],{"class":75},[36,38760,79],{"class":50},[36,38762,214],{"class":141},[36,38764,38765],{"class":145},"Please enter a valid number.",[36,38767,214],{"class":141},[36,38769,86],{"class":50},[14,38771,38772],{},"Than this:",[26,38774,38776],{"className":28,"code":38775,"language":30,"meta":31,"style":31},"try:\n    number = int(user_input)\nexcept:\n    print(\"Something went wrong.\")\n",[33,38777,38778,38784,38798,38804],{"__ignoreMap":31},[36,38779,38780,38782],{"class":38,"line":39},[36,38781,29606],{"class":1718},[36,38783,1730],{"class":50},[36,38785,38786,38788,38790,38792,38794,38796],{"class":38,"line":72},[36,38787,29632],{"class":42},[36,38789,47],{"class":46},[36,38791,5739],{"class":96},[36,38793,79],{"class":50},[36,38795,38744],{"class":82},[36,38797,86],{"class":50},[36,38799,38800,38802],{"class":38,"line":89},[36,38801,29609],{"class":1718},[36,38803,1730],{"class":50},[36,38805,38806,38808,38810,38812,38815,38817],{"class":38,"line":496},[36,38807,1735],{"class":75},[36,38809,79],{"class":50},[36,38811,214],{"class":141},[36,38813,38814],{"class":145},"Something went wrong.",[36,38816,214],{"class":141},[36,38818,86],{"class":50},[14,38820,38821],{},"The first version is clearer and easier to debug.",[21,38823,21766],{"id":21765},[14,38825,38826],{},"Here are some common mistakes:",[157,38828,38829,38835,38840,38843],{},[160,38830,38831,38832,38834],{},"using a bare ",[33,38833,29609],{}," for everything",[160,38836,38837,38838],{},"putting too much code inside ",[33,38839,29606],{},[160,38841,38842],{},"catching an error but doing nothing",[160,38844,38845],{},"confusing syntax errors with runtime errors",[14,38847,3355,38848,38850],{},[331,38849,29747],{}," means your code is written incorrectly, so Python cannot run it.",[14,38852,3361,38853,38855],{},[331,38854,29753],{}," usually happens while the program is already running.",[14,38857,1235],{},[26,38859,38861],{"className":28,"code":38860,"language":30,"meta":31,"style":31},"print(\"Hello\"\n",[33,38862,38863],{"__ignoreMap":31},[36,38864,38865,38867,38869,38871,38873],{"class":38,"line":39},[36,38866,76],{"class":75},[36,38868,79],{"class":50},[36,38870,214],{"class":141},[36,38872,8801],{"class":145},[36,38874,1954],{"class":141},[14,38876,38877,38878,1281,38880,351],{},"This is a syntax error, not something you would normally handle with ",[33,38879,29606],{},[33,38881,29609],{},[21,38883,16058],{"id":16057},[14,38885,38886],{},"This page is only a definition and overview.",[14,38888,38889],{},"It does not go deep into:",[157,38891,38892,38899,38902],{},[160,38893,16071,38894,5826,38896,38898],{},[33,38895,29606],{},[33,38897,29609],{}," syntax",[160,38900,38901],{},"long lists of exception types",[160,38903,38904],{},"advanced exception handling patterns",[14,38906,38907,38908,1180,38912,351],{},"For practical usage, see ",[347,38909,38911],{"href":38910},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use try-except blocks in Python",[347,38913,30184],{"href":30183},[14,38915,38916,38917,1180,38919,351],{},"If you need help with a specific error, go to pages such as ",[347,38918,30267],{"href":30266},[347,38920,38922],{"href":38921},"\u002Ferrors\u002Fzerodivisionerror-in-python-causes-and-fixes","ZeroDivisionError in Python: causes and fixes",[21,38924,38926],{"id":38925},"next-steps-for-the-reader","Next steps for the reader",[14,38928,38929],{},"A good path forward is:",[157,38931,38932,38935,38946,38949],{},[160,38933,38934],{},"learn what Python errors and exceptions are",[160,38936,38937,38938,5950,38940,5950,38942,5953,38944,35595],{},"understand how ",[33,38939,29606],{},[33,38941,29609],{},[33,38943,30077],{},[33,38945,30080],{},[160,38947,38948],{},"visit specific error pages when solving a real problem",[160,38950,38951,38952,38956],{},"learn how to ",[347,38953,38955],{"href":38954},"\u002Flearn\u002Fraising-exceptions-in-python\u002F","raise exceptions in Python"," when you want your own code to signal a problem",[14,38958,38959],{},"Useful commands while learning:",[26,38961,38963],{"className":19488,"code":38962,"language":19490,"meta":31,"style":31},"python your_script.py\n",[33,38964,38965],{"__ignoreMap":31},[36,38966,38967,38969],{"class":38,"line":39},[36,38968,30],{"class":3010},[36,38970,38971],{"class":145}," your_script.py\n",[26,38973,38975],{"className":28,"code":38974,"language":30,"meta":31,"style":31},"print(type(value))\nprint(value)\nhelp(ValueError)\nhelp(ZeroDivisionError)\n",[33,38976,38977,38991,39001,39011],{"__ignoreMap":31},[36,38978,38979,38981,38983,38985,38987,38989],{"class":38,"line":39},[36,38980,76],{"class":75},[36,38982,79],{"class":50},[36,38984,97],{"class":96},[36,38986,79],{"class":50},[36,38988,2686],{"class":82},[36,38990,104],{"class":50},[36,38992,38993,38995,38997,38999],{"class":38,"line":72},[36,38994,76],{"class":75},[36,38996,79],{"class":50},[36,38998,2686],{"class":82},[36,39000,86],{"class":50},[36,39002,39003,39005,39007,39009],{"class":38,"line":89},[36,39004,4392],{"class":75},[36,39006,79],{"class":50},[36,39008,8416],{"class":96},[36,39010,86],{"class":50},[36,39012,39013,39015,39017,39019],{"class":38,"line":496},[36,39014,4392],{"class":75},[36,39016,79],{"class":50},[36,39018,30224],{"class":96},[36,39020,86],{"class":50},[14,39022,39023],{},"These can help you inspect values and understand the error type you are seeing.",[21,39025,1063],{"id":1062},[1065,39027,39029],{"id":39028},"is-error-handling-the-same-as-fixing-errors","Is error handling the same as fixing errors?",[14,39031,39032],{},"Not exactly. Error handling means your program is prepared for possible problems and responds safely when they happen.",[1065,39034,39036],{"id":39035},"what-is-the-difference-between-an-error-and-an-exception","What is the difference between an error and an exception?",[14,39038,39039],{},"Beginners often use the words loosely. In Python, an exception is the object raised when something goes wrong during runtime.",[1065,39041,39043],{"id":39042},"should-i-use-try-except-everywhere","Should I use try-except everywhere?",[14,39045,39046],{},"No. Use it where errors are likely and where you know how to respond.",[1065,39048,39050],{"id":39049},"why-not-use-bare-except","Why not use bare except?",[14,39052,39053],{},"It can hide the real problem and make debugging harder. Catch specific exception types instead.",[1065,39055,39057],{"id":39056},"can-a-program-continue-after-an-error","Can a program continue after an error?",[14,39059,39060],{},"Yes, if the error is handled properly and your code knows what to do next.",[21,39062,1105],{"id":1104},[157,39064,39065,39069,39073,39077,39082,39086,39090],{},[160,39066,39067],{},[347,39068,29761],{"href":29760},[160,39070,39071],{},[347,39072,30481],{"href":30084},[160,39074,39075],{},[347,39076,30486],{"href":30183},[160,39078,39079],{},[347,39080,39081],{"href":38910},"How to use try-except blocks in Python",[160,39083,39084],{},[347,39085,30267],{"href":30266},[160,39087,39088],{},[347,39089,30291],{"href":30290},[160,39091,39092],{},[347,39093,38922],{"href":38921},[1137,39095,39096],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":31,"searchDepth":72,"depth":72,"links":39098},[39099,39100,39101,39102,39103,39109,39112,39113,39114,39115,39116,39123],{"id":29612,"depth":72,"text":29613},{"id":38279,"depth":72,"text":38280},{"id":38312,"depth":72,"text":38313},{"id":38356,"depth":72,"text":38357},{"id":38403,"depth":72,"text":38404,"children":39104},[39105,39106,39107,39108],{"id":29606,"depth":89,"text":29606},{"id":29609,"depth":89,"text":29609},{"id":30077,"depth":89,"text":30077},{"id":30080,"depth":89,"text":30080},{"id":38453,"depth":72,"text":38454,"children":39110},[39111],{"id":38539,"depth":89,"text":38540},{"id":38696,"depth":72,"text":38697},{"id":21765,"depth":72,"text":21766},{"id":16057,"depth":72,"text":16058},{"id":38925,"depth":72,"text":38926},{"id":1062,"depth":72,"text":1063,"children":39117},[39118,39119,39120,39121,39122],{"id":39028,"depth":89,"text":39029},{"id":39035,"depth":89,"text":39036},{"id":39042,"depth":89,"text":39043},{"id":39049,"depth":89,"text":39050},{"id":39056,"depth":89,"text":39057},{"id":1104,"depth":72,"text":1105},"Master what is error handling in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-error-handling-in-python",{"title":38129,"description":39124},"glossary\u002Fwhat-is-error-handling-in-python","E7NUtlPZ1uqSXZqOLPDGAiIYvYOKsWkZXIHKceW49dg",{"id":39131,"title":39132,"body":39133,"description":40545,"extension":1159,"meta":40546,"navigation":475,"path":40547,"seo":40548,"stem":40549,"__hash__":40550},"content\u002Fglossary\u002Fwhat-is-inheritance-in-python.md","What Is Inheritance in Python?",{"type":7,"value":39134,"toc":40524},[39135,39138,39141,39148,39150,39240,39244,39253,39267,39271,39274,39297,39299,39318,39325,39329,39332,39335,39349,39367,39371,39374,39429,39432,39449,39452,39472,39479,39483,39486,39500,39502,39624,39628,39642,39656,39658,39661,39782,39786,39800,39802,39832,39835,39839,39845,39848,39978,39982,39996,40006,40013,40018,40030,40032,40203,40207,40221,40227,40249,40252,40256,40259,40261,40275,40278,40286,40289,40291,40294,40311,40321,40323,40326,40340,40343,40422,40424,40459,40461,40465,40468,40472,40475,40479,40482,40486,40489,40493,40496,40498,40518,40521],[10,39136,39132],{"id":39137},"what-is-inheritance-in-python",[14,39139,39140],{},"Inheritance in Python is a way for one class to use code from another class.",[14,39142,39143,39144,351],{},"This helps you reuse methods and attributes instead of writing the same code again. It is a common idea in object-oriented programming, so it makes more sense if you already understand ",[347,39145,39147],{"href":39146},"\u002Flearn\u002Fpython-classes-and-objects-explained","classes and objects in Python",[14,39149,18219],{},[26,39151,39153],{"className":28,"code":39152,"language":30,"meta":31,"style":31},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    pass\n\npet = Dog()\nprint(pet.speak())\n",[33,39154,39155,39164,39177,39188,39192,39205,39209,39213,39224],{"__ignoreMap":31},[36,39156,39157,39159,39162],{"class":38,"line":39},[36,39158,138],{"class":137},[36,39160,39161],{"class":3010}," Animal",[36,39163,1730],{"class":50},[36,39165,39166,39168,39171,39173,39175],{"class":38,"line":72},[36,39167,3036],{"class":137},[36,39169,39170],{"class":3082}," speak",[36,39172,79],{"class":50},[36,39174,3045],{"class":3044},[36,39176,3054],{"class":50},[36,39178,39179,39181,39183,39186],{"class":38,"line":89},[36,39180,17379],{"class":1718},[36,39182,224],{"class":141},[36,39184,39185],{"class":145},"Some sound",[36,39187,1954],{"class":141},[36,39189,39190],{"class":38,"line":496},[36,39191,476],{"emptyLinePlaceholder":475},[36,39193,39194,39196,39198,39200,39203],{"class":38,"line":501},[36,39195,138],{"class":137},[36,39197,3011],{"class":3010},[36,39199,79],{"class":50},[36,39201,39202],{"class":3010},"Animal",[36,39204,3054],{"class":50},[36,39206,39207],{"class":38,"line":522},[36,39208,3563],{"class":1718},[36,39210,39211],{"class":38,"line":527},[36,39212,476],{"emptyLinePlaceholder":475},[36,39214,39215,39218,39220,39222],{"class":38,"line":3092},[36,39216,39217],{"class":42},"pet ",[36,39219,47],{"class":46},[36,39221,3011],{"class":82},[36,39223,3170],{"class":50},[36,39225,39226,39228,39230,39233,39235,39238],{"class":38,"line":3118},[36,39227,76],{"class":75},[36,39229,79],{"class":50},[36,39231,39232],{"class":82},"pet",[36,39234,351],{"class":50},[36,39236,39237],{"class":82},"speak",[36,39239,4387],{"class":50},[14,39241,39242],{},[331,39243,107],{},[26,39245,39247],{"className":28,"code":39246,"language":30,"meta":31,"style":31},"Some sound\n",[33,39248,39249],{"__ignoreMap":31},[36,39250,39251],{"class":38,"line":39},[36,39252,39246],{"class":42},[14,39254,39255,39257,39258,39261,39262,39264,39265,351],{},[33,39256,3200],{}," does not define ",[33,39259,39260],{},"speak()",", but it can still use it because ",[33,39263,3200],{}," inherits from ",[33,39266,39202],{},[21,39268,39270],{"id":39269},"what-inheritance-means","What inheritance means",[14,39272,39273],{},"Inheritance lets one class use code from another class.",[157,39275,39276,39285,39294],{},[160,39277,39278,39279,1180,39282],{},"The new class is often called the ",[331,39280,39281],{},"child class",[331,39283,39284],{},"subclass",[160,39286,39287,39288,1180,39291],{},"The class it inherits from is often called the ",[331,39289,39290],{},"parent class",[331,39292,39293],{},"base class",[160,39295,39296],{},"It helps avoid repeating the same methods and attributes",[14,39298,36768],{},[157,39300,39301,39306,39311],{},[160,39302,39303,39305],{},[33,39304,39202],{}," is the parent class",[160,39307,39308,39310],{},[33,39309,3200],{}," is the child class",[160,39312,39313,39315,39316],{},[33,39314,3200],{}," can use behavior defined in ",[33,39317,39202],{},[14,39319,39320,39321,1281,39323,351],{},"If you are new to this topic, it also helps to know ",[347,39322,17347],{"href":4673},[347,39324,3233],{"href":38123},[21,39326,39328],{"id":39327},"why-beginners-use-inheritance","Why beginners use inheritance",[14,39330,39331],{},"Beginners usually use inheritance for simple code reuse.",[14,39333,39334],{},"It is useful when:",[157,39336,39337,39340,39343,39346],{},[160,39338,39339],{},"You want to reuse code instead of writing the same method again",[160,39341,39342],{},"You have related classes that share common behavior",[160,39344,39345],{},"You want to keep code organized when objects are similar",[160,39347,39348],{},"You want updates in shared code to affect multiple child classes",[14,39350,39351,39352,1281,39354,39357,39358,1180,39361,39364,39365,351],{},"For example, if both ",[33,39353,3200],{},[33,39355,39356],{},"Cat"," are animals, they may both need methods like ",[33,39359,39360],{},"eat()",[33,39362,39363],{},"sleep()",". Instead of writing those methods twice, you can put them in ",[33,39366,39202],{},[21,39368,39370],{"id":39369},"basic-inheritance-syntax","Basic inheritance syntax",[14,39372,39373],{},"To create a child class, put the parent class name in parentheses after the child class name.",[26,39375,39377],{"className":28,"code":39376,"language":30,"meta":31,"style":31},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    pass\n",[33,39378,39379,39387,39399,39409,39413,39425],{"__ignoreMap":31},[36,39380,39381,39383,39385],{"class":38,"line":39},[36,39382,138],{"class":137},[36,39384,39161],{"class":3010},[36,39386,1730],{"class":50},[36,39388,39389,39391,39393,39395,39397],{"class":38,"line":72},[36,39390,3036],{"class":137},[36,39392,39170],{"class":3082},[36,39394,79],{"class":50},[36,39396,3045],{"class":3044},[36,39398,3054],{"class":50},[36,39400,39401,39403,39405,39407],{"class":38,"line":89},[36,39402,17379],{"class":1718},[36,39404,224],{"class":141},[36,39406,39185],{"class":145},[36,39408,1954],{"class":141},[36,39410,39411],{"class":38,"line":496},[36,39412,476],{"emptyLinePlaceholder":475},[36,39414,39415,39417,39419,39421,39423],{"class":38,"line":501},[36,39416,138],{"class":137},[36,39418,3011],{"class":3010},[36,39420,79],{"class":50},[36,39422,39202],{"class":3010},[36,39424,3054],{"class":50},[36,39426,39427],{"class":38,"line":522},[36,39428,3563],{"class":1718},[14,39430,39431],{},"This line is the key part:",[26,39433,39435],{"className":28,"code":39434,"language":30,"meta":31,"style":31},"class Dog(Animal):\n",[33,39436,39437],{"__ignoreMap":31},[36,39438,39439,39441,39443,39445,39447],{"class":38,"line":39},[36,39440,138],{"class":137},[36,39442,3011],{"class":3010},[36,39444,79],{"class":50},[36,39446,39202],{"class":3010},[36,39448,3054],{"class":50},[14,39450,39451],{},"It means:",[157,39453,39454,39460,39467],{},[160,39455,39456,39264,39458],{},[33,39457,3200],{},[33,39459,39202],{},[160,39461,39462,39464,39465],{},[33,39463,3200],{}," gets access to methods and attributes from ",[33,39466,39202],{},[160,39468,39469,39471],{},[33,39470,3200],{}," can also define its own methods",[14,39473,39474,39475,351],{},"If you want a refresher on class methods, see ",[347,39476,39478],{"href":39477},"\u002Flearn\u002Fbasic-methods-in-python-classes-explained","basic methods in Python classes",[21,39480,39482],{"id":39481},"what-a-child-class-can-do","What a child class can do",[14,39484,39485],{},"A child class can do several useful things:",[157,39487,39488,39491,39494,39497],{},[160,39489,39490],{},"Use parent methods without rewriting them",[160,39492,39493],{},"Add new methods that only belong to the child class",[160,39495,39496],{},"Override a parent method with a new version",[160,39498,39499],{},"Call parent behavior when needed",[14,39501,1404],{},[26,39503,39505],{"className":28,"code":39504,"language":30,"meta":31,"style":31},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    def fetch(self):\n        return \"The dog runs after the ball\"\n\npet = Dog()\n\nprint(pet.speak())\nprint(pet.fetch())\n",[33,39506,39507,39515,39527,39537,39541,39553,39566,39577,39581,39591,39595,39609],{"__ignoreMap":31},[36,39508,39509,39511,39513],{"class":38,"line":39},[36,39510,138],{"class":137},[36,39512,39161],{"class":3010},[36,39514,1730],{"class":50},[36,39516,39517,39519,39521,39523,39525],{"class":38,"line":72},[36,39518,3036],{"class":137},[36,39520,39170],{"class":3082},[36,39522,79],{"class":50},[36,39524,3045],{"class":3044},[36,39526,3054],{"class":50},[36,39528,39529,39531,39533,39535],{"class":38,"line":89},[36,39530,17379],{"class":1718},[36,39532,224],{"class":141},[36,39534,39185],{"class":145},[36,39536,1954],{"class":141},[36,39538,39539],{"class":38,"line":496},[36,39540,476],{"emptyLinePlaceholder":475},[36,39542,39543,39545,39547,39549,39551],{"class":38,"line":501},[36,39544,138],{"class":137},[36,39546,3011],{"class":3010},[36,39548,79],{"class":50},[36,39550,39202],{"class":3010},[36,39552,3054],{"class":50},[36,39554,39555,39557,39560,39562,39564],{"class":38,"line":522},[36,39556,3036],{"class":137},[36,39558,39559],{"class":3082}," fetch",[36,39561,79],{"class":50},[36,39563,3045],{"class":3044},[36,39565,3054],{"class":50},[36,39567,39568,39570,39572,39575],{"class":38,"line":527},[36,39569,17379],{"class":1718},[36,39571,224],{"class":141},[36,39573,39574],{"class":145},"The dog runs after the ball",[36,39576,1954],{"class":141},[36,39578,39579],{"class":38,"line":3092},[36,39580,476],{"emptyLinePlaceholder":475},[36,39582,39583,39585,39587,39589],{"class":38,"line":3118},[36,39584,39217],{"class":42},[36,39586,47],{"class":46},[36,39588,3011],{"class":82},[36,39590,3170],{"class":50},[36,39592,39593],{"class":38,"line":3123},[36,39594,476],{"emptyLinePlaceholder":475},[36,39596,39597,39599,39601,39603,39605,39607],{"class":38,"line":3144},[36,39598,76],{"class":75},[36,39600,79],{"class":50},[36,39602,39232],{"class":82},[36,39604,351],{"class":50},[36,39606,39237],{"class":82},[36,39608,4387],{"class":50},[36,39610,39611,39613,39615,39617,39619,39622],{"class":38,"line":3160},[36,39612,76],{"class":75},[36,39614,79],{"class":50},[36,39616,39232],{"class":82},[36,39618,351],{"class":50},[36,39620,39621],{"class":82},"fetch",[36,39623,4387],{"class":50},[14,39625,39626],{},[331,39627,107],{},[26,39629,39631],{"className":28,"code":39630,"language":30,"meta":31,"style":31},"Some sound\nThe dog runs after the ball\n",[33,39632,39633,39637],{"__ignoreMap":31},[36,39634,39635],{"class":38,"line":39},[36,39636,39246],{"class":42},[36,39638,39639],{"class":38,"line":72},[36,39640,39641],{"class":42},"The dog runs after the ball\n",[14,39643,39644,39646,39647,39649,39650,39652,39653,351],{},[33,39645,3200],{}," inherited ",[33,39648,39260],{}," from ",[33,39651,39202],{}," and added its own method, ",[33,39654,39655],{},"fetch()",[21,39657,35967],{"id":35966},[14,39659,39660],{},"Here is a slightly fuller example that shows both inherited behavior and new child behavior:",[26,39662,39664],{"className":28,"code":39663,"language":30,"meta":31,"style":31},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    def fetch(self):\n        return \"Fetching the stick\"\n\ndog = Dog()\n\nprint(dog.speak())\nprint(dog.fetch())\n",[33,39665,39666,39674,39686,39696,39700,39712,39724,39735,39739,39750,39754,39768],{"__ignoreMap":31},[36,39667,39668,39670,39672],{"class":38,"line":39},[36,39669,138],{"class":137},[36,39671,39161],{"class":3010},[36,39673,1730],{"class":50},[36,39675,39676,39678,39680,39682,39684],{"class":38,"line":72},[36,39677,3036],{"class":137},[36,39679,39170],{"class":3082},[36,39681,79],{"class":50},[36,39683,3045],{"class":3044},[36,39685,3054],{"class":50},[36,39687,39688,39690,39692,39694],{"class":38,"line":89},[36,39689,17379],{"class":1718},[36,39691,224],{"class":141},[36,39693,39185],{"class":145},[36,39695,1954],{"class":141},[36,39697,39698],{"class":38,"line":496},[36,39699,476],{"emptyLinePlaceholder":475},[36,39701,39702,39704,39706,39708,39710],{"class":38,"line":501},[36,39703,138],{"class":137},[36,39705,3011],{"class":3010},[36,39707,79],{"class":50},[36,39709,39202],{"class":3010},[36,39711,3054],{"class":50},[36,39713,39714,39716,39718,39720,39722],{"class":38,"line":522},[36,39715,3036],{"class":137},[36,39717,39559],{"class":3082},[36,39719,79],{"class":50},[36,39721,3045],{"class":3044},[36,39723,3054],{"class":50},[36,39725,39726,39728,39730,39733],{"class":38,"line":527},[36,39727,17379],{"class":1718},[36,39729,224],{"class":141},[36,39731,39732],{"class":145},"Fetching the stick",[36,39734,1954],{"class":141},[36,39736,39737],{"class":38,"line":3092},[36,39738,476],{"emptyLinePlaceholder":475},[36,39740,39741,39744,39746,39748],{"class":38,"line":3118},[36,39742,39743],{"class":42},"dog ",[36,39745,47],{"class":46},[36,39747,3011],{"class":82},[36,39749,3170],{"class":50},[36,39751,39752],{"class":38,"line":3123},[36,39753,476],{"emptyLinePlaceholder":475},[36,39755,39756,39758,39760,39762,39764,39766],{"class":38,"line":3144},[36,39757,76],{"class":75},[36,39759,79],{"class":50},[36,39761,13454],{"class":82},[36,39763,351],{"class":50},[36,39765,39237],{"class":82},[36,39767,4387],{"class":50},[36,39769,39770,39772,39774,39776,39778,39780],{"class":38,"line":3160},[36,39771,76],{"class":75},[36,39773,79],{"class":50},[36,39775,13454],{"class":82},[36,39777,351],{"class":50},[36,39779,39621],{"class":82},[36,39781,4387],{"class":50},[14,39783,39784],{},[331,39785,107],{},[26,39787,39789],{"className":28,"code":39788,"language":30,"meta":31,"style":31},"Some sound\nFetching the stick\n",[33,39790,39791,39795],{"__ignoreMap":31},[36,39792,39793],{"class":38,"line":39},[36,39794,39246],{"class":42},[36,39796,39797],{"class":38,"line":72},[36,39798,39799],{"class":42},"Fetching the stick\n",[14,39801,9951],{},[157,39803,39804,39811,39817,39825],{},[160,39805,39806,39808,39809],{},[33,39807,39202],{}," defines ",[33,39810,39260],{},[160,39812,39813,39264,39815],{},[33,39814,3200],{},[33,39816,39202],{},[160,39818,39819,39821,39822,39824],{},[33,39820,3200],{}," does not need to write ",[33,39823,39260],{}," again",[160,39826,39827,39829,39830],{},[33,39828,3200],{}," adds a new method called ",[33,39831,39655],{},[14,39833,39834],{},"This is a good example of how inheritance reduces repetition.",[21,39836,39838],{"id":39837},"method-overriding","Method overriding",[14,39840,39841,39842,351],{},"A child class can replace a parent method with its own version. This is called ",[331,39843,39844],{},"overriding",[14,39846,39847],{},"Use overriding when classes share a general idea but need different behavior.",[26,39849,39851],{"className":28,"code":39850,"language":30,"meta":31,"style":31},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    def speak(self):\n        return \"Woof\"\n\nanimal = Animal()\ndog = Dog()\n\nprint(animal.speak())\nprint(dog.speak())\n",[33,39852,39853,39861,39873,39883,39887,39899,39911,39921,39925,39936,39946,39950,39964],{"__ignoreMap":31},[36,39854,39855,39857,39859],{"class":38,"line":39},[36,39856,138],{"class":137},[36,39858,39161],{"class":3010},[36,39860,1730],{"class":50},[36,39862,39863,39865,39867,39869,39871],{"class":38,"line":72},[36,39864,3036],{"class":137},[36,39866,39170],{"class":3082},[36,39868,79],{"class":50},[36,39870,3045],{"class":3044},[36,39872,3054],{"class":50},[36,39874,39875,39877,39879,39881],{"class":38,"line":89},[36,39876,17379],{"class":1718},[36,39878,224],{"class":141},[36,39880,39185],{"class":145},[36,39882,1954],{"class":141},[36,39884,39885],{"class":38,"line":496},[36,39886,476],{"emptyLinePlaceholder":475},[36,39888,39889,39891,39893,39895,39897],{"class":38,"line":501},[36,39890,138],{"class":137},[36,39892,3011],{"class":3010},[36,39894,79],{"class":50},[36,39896,39202],{"class":3010},[36,39898,3054],{"class":50},[36,39900,39901,39903,39905,39907,39909],{"class":38,"line":522},[36,39902,3036],{"class":137},[36,39904,39170],{"class":3082},[36,39906,79],{"class":50},[36,39908,3045],{"class":3044},[36,39910,3054],{"class":50},[36,39912,39913,39915,39917,39919],{"class":38,"line":527},[36,39914,17379],{"class":1718},[36,39916,224],{"class":141},[36,39918,3985],{"class":145},[36,39920,1954],{"class":141},[36,39922,39923],{"class":38,"line":3092},[36,39924,476],{"emptyLinePlaceholder":475},[36,39926,39927,39930,39932,39934],{"class":38,"line":3118},[36,39928,39929],{"class":42},"animal ",[36,39931,47],{"class":46},[36,39933,39161],{"class":82},[36,39935,3170],{"class":50},[36,39937,39938,39940,39942,39944],{"class":38,"line":3123},[36,39939,39743],{"class":42},[36,39941,47],{"class":46},[36,39943,3011],{"class":82},[36,39945,3170],{"class":50},[36,39947,39948],{"class":38,"line":3144},[36,39949,476],{"emptyLinePlaceholder":475},[36,39951,39952,39954,39956,39958,39960,39962],{"class":38,"line":3160},[36,39953,76],{"class":75},[36,39955,79],{"class":50},[36,39957,3025],{"class":82},[36,39959,351],{"class":50},[36,39961,39237],{"class":82},[36,39963,4387],{"class":50},[36,39965,39966,39968,39970,39972,39974,39976],{"class":38,"line":11084},[36,39967,76],{"class":75},[36,39969,79],{"class":50},[36,39971,13454],{"class":82},[36,39973,351],{"class":50},[36,39975,39237],{"class":82},[36,39977,4387],{"class":50},[14,39979,39980],{},[331,39981,107],{},[26,39983,39985],{"className":28,"code":39984,"language":30,"meta":31,"style":31},"Some sound\nWoof\n",[33,39986,39987,39991],{"__ignoreMap":31},[36,39988,39989],{"class":38,"line":39},[36,39990,39246],{"class":42},[36,39992,39993],{"class":38,"line":72},[36,39994,39995],{"class":42},"Woof\n",[14,39997,7462,39998,39264,40000,40002,40003,40005],{},[33,39999,3200],{},[33,40001,39202],{},", it uses its own ",[33,40004,39260],{}," method because the child version overrides the parent version.",[21,40007,2639,40009,40012],{"id":40008},"using-super-at-a-beginner-level",[33,40010,40011],{},"super()"," at a beginner level",[14,40014,40015,40017],{},[33,40016,40011],{}," lets a child class call a method from the parent class.",[14,40019,40020,40021,40023,40024,351],{},"Beginners often see this in the ",[33,40022,3205],{}," method, which is explained in ",[347,40025,3634,40027,40029],{"href":40026},"\u002Flearn\u002Fthe-__init__-method-in-python-explained",[33,40028,3205],{}," method in Python",[14,40031,197],{},[26,40033,40035],{"className":28,"code":40034,"language":30,"meta":31,"style":31},"class Animal:\n    def __init__(self, name):\n        self.name = name\n\nclass Dog(Animal):\n    def __init__(self, name, breed):\n        super().__init__(name)\n        self.breed = breed\n\npet = Dog(\"Max\", \"Labrador\")\n\nprint(pet.name)\nprint(pet.breed)\n",[33,40036,40037,40045,40061,40073,40077,40089,40110,40126,40140,40144,40171,40175,40189],{"__ignoreMap":31},[36,40038,40039,40041,40043],{"class":38,"line":39},[36,40040,138],{"class":137},[36,40042,39161],{"class":3010},[36,40044,1730],{"class":50},[36,40046,40047,40049,40051,40053,40055,40057,40059],{"class":38,"line":72},[36,40048,3036],{"class":137},[36,40050,3039],{"class":75},[36,40052,79],{"class":50},[36,40054,3045],{"class":3044},[36,40056,58],{"class":50},[36,40058,3051],{"class":3050},[36,40060,3054],{"class":50},[36,40062,40063,40065,40067,40069,40071],{"class":38,"line":89},[36,40064,3060],{"class":3059},[36,40066,351],{"class":50},[36,40068,817],{"class":3065},[36,40070,3068],{"class":46},[36,40072,3071],{"class":42},[36,40074,40075],{"class":38,"line":496},[36,40076,476],{"emptyLinePlaceholder":475},[36,40078,40079,40081,40083,40085,40087],{"class":38,"line":501},[36,40080,138],{"class":137},[36,40082,3011],{"class":3010},[36,40084,79],{"class":50},[36,40086,39202],{"class":3010},[36,40088,3054],{"class":50},[36,40090,40091,40093,40095,40097,40099,40101,40103,40105,40108],{"class":38,"line":522},[36,40092,3036],{"class":137},[36,40094,3039],{"class":75},[36,40096,79],{"class":50},[36,40098,3045],{"class":3044},[36,40100,58],{"class":50},[36,40102,3051],{"class":3050},[36,40104,58],{"class":50},[36,40106,40107],{"class":3050}," breed",[36,40109,3054],{"class":50},[36,40111,40112,40115,40118,40120,40122,40124],{"class":38,"line":527},[36,40113,40114],{"class":96},"        super",[36,40116,40117],{"class":50},"().",[36,40119,3205],{"class":75},[36,40121,79],{"class":50},[36,40123,817],{"class":82},[36,40125,86],{"class":50},[36,40127,40128,40130,40132,40135,40137],{"class":38,"line":3092},[36,40129,3060],{"class":3059},[36,40131,351],{"class":50},[36,40133,40134],{"class":3065},"breed",[36,40136,3068],{"class":46},[36,40138,40139],{"class":42}," breed\n",[36,40141,40142],{"class":38,"line":3118},[36,40143,476],{"emptyLinePlaceholder":475},[36,40145,40146,40148,40150,40152,40154,40156,40158,40160,40162,40164,40167,40169],{"class":38,"line":3123},[36,40147,39217],{"class":42},[36,40149,47],{"class":46},[36,40151,3011],{"class":82},[36,40153,79],{"class":50},[36,40155,214],{"class":141},[36,40157,3137],{"class":145},[36,40159,214],{"class":141},[36,40161,58],{"class":50},[36,40163,224],{"class":141},[36,40165,40166],{"class":145},"Labrador",[36,40168,214],{"class":141},[36,40170,86],{"class":50},[36,40172,40173],{"class":38,"line":3144},[36,40174,476],{"emptyLinePlaceholder":475},[36,40176,40177,40179,40181,40183,40185,40187],{"class":38,"line":3160},[36,40178,76],{"class":75},[36,40180,79],{"class":50},[36,40182,39232],{"class":82},[36,40184,351],{"class":50},[36,40186,817],{"class":3065},[36,40188,86],{"class":50},[36,40190,40191,40193,40195,40197,40199,40201],{"class":38,"line":11084},[36,40192,76],{"class":75},[36,40194,79],{"class":50},[36,40196,39232],{"class":82},[36,40198,351],{"class":50},[36,40200,40134],{"class":3065},[36,40202,86],{"class":50},[14,40204,40205],{},[331,40206,107],{},[26,40208,40210],{"className":28,"code":40209,"language":30,"meta":31,"style":31},"Max\nLabrador\n",[33,40211,40212,40216],{"__ignoreMap":31},[36,40213,40214],{"class":38,"line":39},[36,40215,3182],{"class":42},[36,40217,40218],{"class":38,"line":72},[36,40219,40220],{"class":42},"Labrador\n",[14,40222,40223,40224,40226],{},"Why use ",[33,40225,40011],{}," here?",[157,40228,40229,40237,40243],{},[160,40230,40231,40234,40235],{},[33,40232,40233],{},"Animal.__init__()"," sets ",[33,40236,3213],{},[160,40238,40239,40242],{},[33,40240,40241],{},"Dog.__init__()"," still wants that setup to happen",[160,40244,40245,40248],{},[33,40246,40247],{},"super().__init__(name)"," runs the parent setup first",[14,40250,40251],{},"This is a common and useful pattern.",[21,40253,40255],{"id":40254},"when-inheritance-is-a-good-fit","When inheritance is a good fit",[14,40257,40258],{},"Inheritance works best when there is a clear parent-child relationship.",[14,40260,13412],{},[157,40262,40263,40269],{},[160,40264,40265,8621,40267],{},[33,40266,3200],{},[33,40268,39202],{},[160,40270,40271,8621,40273],{},[33,40272,39356],{},[33,40274,39202],{},[14,40276,40277],{},"Use inheritance when:",[157,40279,40280,40283],{},[160,40281,40282],{},"Multiple classes share the same fields or methods",[160,40284,40285],{},"The child classes are specialized versions of a more general class",[14,40287,40288],{},"Be careful not to force inheritance just to share a small amount of code. Sometimes simple separate classes are easier to read.",[21,40290,2245],{"id":2244},[14,40292,40293],{},"A few points often cause confusion:",[157,40295,40296,40302,40305,40308],{},[160,40297,40298,40299,40301],{},"Inheritance does ",[331,40300,8193],{}," copy and paste code into the child class",[160,40303,40304],{},"The child class can access parent behavior because of the class relationship",[160,40306,40307],{},"A child class can have extra behavior that the parent does not have",[160,40309,40310],{},"Not every related class should use inheritance",[14,40312,40313,40314,40316,40317,351],{},"It also helps to remember that inheritance is about ",[331,40315,18165],{},", not about creating objects. If that part feels unclear, review ",[347,40318,40320],{"href":40319},"\u002Flearn\u002Fobject-oriented-programming-in-python-explained","object-oriented programming in Python",[21,40322,905],{"id":904},[14,40324,40325],{},"Beginners often make these mistakes:",[157,40327,40328,40331,40334,40337],{},[160,40329,40330],{},"Confusing inheritance with creating an object",[160,40332,40333],{},"Thinking the child class must rewrite every parent method",[160,40335,40336],{},"Not understanding the difference between parent class and child class",[160,40338,40339],{},"Using inheritance when separate classes would be clearer",[14,40341,40342],{},"If you want to inspect what an object or class can do, these tools can help:",[26,40344,40346],{"className":28,"code":40345,"language":30,"meta":31,"style":31},"print(type(obj))\nprint(isinstance(obj, Animal))\nprint(isinstance(obj, Dog))\nprint(dir(obj))\nhelp(Dog)\n",[33,40347,40348,40362,40380,40398,40412],{"__ignoreMap":31},[36,40349,40350,40352,40354,40356,40358,40360],{"class":38,"line":39},[36,40351,76],{"class":75},[36,40353,79],{"class":50},[36,40355,97],{"class":96},[36,40357,79],{"class":50},[36,40359,36704],{"class":82},[36,40361,104],{"class":50},[36,40363,40364,40366,40368,40370,40372,40374,40376,40378],{"class":38,"line":72},[36,40365,76],{"class":75},[36,40367,79],{"class":50},[36,40369,5730],{"class":75},[36,40371,79],{"class":50},[36,40373,36704],{"class":82},[36,40375,58],{"class":50},[36,40377,39161],{"class":82},[36,40379,104],{"class":50},[36,40381,40382,40384,40386,40388,40390,40392,40394,40396],{"class":38,"line":89},[36,40383,76],{"class":75},[36,40385,79],{"class":50},[36,40387,5730],{"class":75},[36,40389,79],{"class":50},[36,40391,36704],{"class":82},[36,40393,58],{"class":50},[36,40395,3011],{"class":82},[36,40397,104],{"class":50},[36,40399,40400,40402,40404,40406,40408,40410],{"class":38,"line":496},[36,40401,76],{"class":75},[36,40403,79],{"class":50},[36,40405,17985],{"class":75},[36,40407,79],{"class":50},[36,40409,36704],{"class":82},[36,40411,104],{"class":50},[36,40413,40414,40416,40418,40420],{"class":38,"line":501},[36,40415,4392],{"class":75},[36,40417,79],{"class":50},[36,40419,3200],{"class":82},[36,40421,86],{"class":50},[14,40423,33158],{},[157,40425,40426,40432,40440,40448,40454],{},[160,40427,40428,40431],{},[33,40429,40430],{},"type(obj)"," shows the object's class",[160,40433,40434,40437,40438],{},[33,40435,40436],{},"isinstance(obj, Animal)"," checks if the object is treated as an ",[33,40439,39202],{},[160,40441,40442,40445,40446],{},[33,40443,40444],{},"isinstance(obj, Dog)"," checks if it is specifically a ",[33,40447,3200],{},[160,40449,40450,40453],{},[33,40451,40452],{},"dir(obj)"," lists available attributes and methods",[160,40455,40456,40458],{},[33,40457,4426],{}," shows information about the class",[21,40460,1063],{"id":1062},[1065,40462,40464],{"id":40463},"what-is-inheritance-in-python-in-simple-words","What is inheritance in Python in simple words?",[14,40466,40467],{},"It is a way for one class to use code from another class.",[1065,40469,40471],{"id":40470},"what-is-the-parent-class","What is the parent class?",[14,40473,40474],{},"The parent class is the class that provides methods and attributes to another class.",[1065,40476,40478],{"id":40477},"what-is-the-child-class","What is the child class?",[14,40480,40481],{},"The child class inherits from the parent class and can also add or change behavior.",[1065,40483,40485],{"id":40484},"can-a-child-class-change-a-parent-method","Can a child class change a parent method?",[14,40487,40488],{},"Yes. This is called overriding.",[1065,40490,40492],{"id":40491},"should-beginners-always-use-inheritance","Should beginners always use inheritance?",[14,40494,40495],{},"No. Use it when there is a clear shared relationship between classes.",[21,40497,1105],{"id":1104},[157,40499,40500,40504,40508,40514],{},[160,40501,40502],{},[347,40503,4616],{"href":40319},[160,40505,40506],{},[347,40507,3343],{"href":39146},[160,40509,40510],{},[347,40511,40513],{"href":40512},"\u002Fhow-to\u002Fhow-to-inherit-from-a-class-in-python","How to inherit from a class in Python",[160,40515,40516],{},[347,40517,4632],{"href":18136},[14,40519,40520],{},"Next, learn how classes and objects work so inheritance makes more sense in real code.",[1137,40522,40523],{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .smCYv, html code.shiki .smCYv{--shiki-light:#E53935;--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 .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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":31,"searchDepth":72,"depth":72,"links":40525},[40526,40527,40528,40529,40530,40531,40532,40534,40535,40536,40537,40544],{"id":39269,"depth":72,"text":39270},{"id":39327,"depth":72,"text":39328},{"id":39369,"depth":72,"text":39370},{"id":39481,"depth":72,"text":39482},{"id":35966,"depth":72,"text":35967},{"id":39837,"depth":72,"text":39838},{"id":40008,"depth":72,"text":40533},"Using super() at a beginner level",{"id":40254,"depth":72,"text":40255},{"id":2244,"depth":72,"text":2245},{"id":904,"depth":72,"text":905},{"id":1062,"depth":72,"text":1063,"children":40538},[40539,40540,40541,40542,40543],{"id":40463,"depth":89,"text":40464},{"id":40470,"depth":89,"text":40471},{"id":40477,"depth":89,"text":40478},{"id":40484,"depth":89,"text":40485},{"id":40491,"depth":89,"text":40492},{"id":1104,"depth":72,"text":1105},"Master what is inheritance in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-inheritance-in-python",{"title":39132,"description":40545},"glossary\u002Fwhat-is-inheritance-in-python","QOocROruvMCrkSFkvja_KuG1F8dNH-gIy8q4jV8hp14",{"id":40552,"title":40553,"body":40554,"description":41823,"extension":1159,"meta":41824,"navigation":475,"path":41825,"seo":41826,"stem":41827,"__hash__":41828},"content\u002Fglossary\u002Fwhat-is-recursion-in-python.md","What Is Recursion in Python?",{"type":7,"value":40555,"toc":41803},[40556,40559,40562,40565,40568,40582,40585,40675,40677,40699,40702,40723,40727,40731,40734,40737,40748,40751,40759,40790,40794,40797,40809,40816,40818,40895,40898,40902,40908,40992,40995,41042,41045,41059,41063,41067,41152,41155,41159,41162,41164,41177,41180,41255,41257,41266,41268,41282,41286,41289,41292,41423,41425,41454,41457,41461,41464,41466,41480,41483,41489,41493,41496,41507,41510,41519,41523,41526,41543,41546,41587,41590,41593,41664,41671,41674,41688,41691,41725,41728,41740,41742,41746,41753,41757,41760,41764,41767,41771,41774,41776,41800],[10,40557,40553],{"id":40558},"what-is-recursion-in-python",[14,40560,40561],{},"Recursion in Python means a function calls itself.",[14,40563,40564],{},"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,40566,40567],{},"A recursive function has two main parts:",[157,40569,40570,40576],{},[160,40571,40572,40575],{},[331,40573,40574],{},"Base case",": the condition that stops the function",[160,40577,40578,40581],{},[331,40579,40580],{},"Recursive case",": the part where the function calls itself again",[14,40583,40584],{},"Here is the simplest working example:",[26,40586,40588],{"className":28,"code":40587,"language":30,"meta":31,"style":31},"def countdown(n):\n    if n == 0:\n        print(\"Done\")\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(3)\n",[33,40589,40590,40603,40615,40630,40635,40645,40660,40664],{"__ignoreMap":31},[36,40591,40592,40594,40597,40599,40601],{"class":38,"line":39},[36,40593,8785],{"class":137},[36,40595,40596],{"class":3082}," countdown",[36,40598,79],{"class":50},[36,40600,9924],{"class":3050},[36,40602,3054],{"class":50},[36,40604,40605,40607,40609,40611,40613],{"class":38,"line":72},[36,40606,14166],{"class":1718},[36,40608,9908],{"class":42},[36,40610,1965],{"class":46},[36,40612,1993],{"class":54},[36,40614,1730],{"class":50},[36,40616,40617,40619,40621,40623,40626,40628],{"class":38,"line":89},[36,40618,3095],{"class":75},[36,40620,79],{"class":50},[36,40622,214],{"class":141},[36,40624,40625],{"class":145},"Done",[36,40627,214],{"class":141},[36,40629,86],{"class":50},[36,40631,40632],{"class":38,"line":496},[36,40633,40634],{"class":1718},"        return\n",[36,40636,40637,40639,40641,40643],{"class":38,"line":501},[36,40638,1735],{"class":75},[36,40640,79],{"class":50},[36,40642,9924],{"class":82},[36,40644,86],{"class":50},[36,40646,40647,40650,40652,40654,40656,40658],{"class":38,"line":522},[36,40648,40649],{"class":82},"    countdown",[36,40651,79],{"class":50},[36,40653,12856],{"class":82},[36,40655,25103],{"class":46},[36,40657,16839],{"class":54},[36,40659,86],{"class":50},[36,40661,40662],{"class":38,"line":527},[36,40663,476],{"emptyLinePlaceholder":475},[36,40665,40666,40669,40671,40673],{"class":38,"line":3092},[36,40667,40668],{"class":82},"countdown",[36,40670,79],{"class":50},[36,40672,1247],{"class":54},[36,40674,86],{"class":50},[14,40676,107],{},[26,40678,40680],{"className":28,"code":40679,"language":30,"meta":31,"style":31},"3\n2\n1\nDone\n",[33,40681,40682,40686,40690,40694],{"__ignoreMap":31},[36,40683,40684],{"class":38,"line":39},[36,40685,9948],{"class":54},[36,40687,40688],{"class":38,"line":72},[36,40689,9943],{"class":54},[36,40691,40692],{"class":38,"line":89},[36,40693,9938],{"class":54},[36,40695,40696],{"class":38,"line":496},[36,40697,40698],{"class":42},"Done\n",[14,40700,40701],{},"How this works:",[157,40703,40704,40712,40720],{},[160,40705,40706,6195,40709],{},[33,40707,40708],{},"if n == 0:",[331,40710,40711],{},"base case",[160,40713,40714,6195,40717],{},[33,40715,40716],{},"countdown(n - 1)",[331,40718,40719],{},"recursive call",[160,40721,40722],{},"Each call uses a smaller value, so the function moves toward stopping",[14,40724,11641,40725,351],{},[347,40726,29085],{"href":20857},[21,40728,40730],{"id":40729},"what-recursion-means","What recursion means",[14,40732,40733],{},"Recursion is when a function calls itself.",[14,40735,40736],{},"A recursive function usually works like this:",[157,40738,40739,40742,40745],{},[160,40740,40741],{},"It handles one small part of the problem",[160,40743,40744],{},"Then it calls itself to handle the rest",[160,40746,40747],{},"Each call should get closer to stopping",[14,40749,40750],{},"This is why recursion is often used for problems that can be broken into smaller versions of the same task.",[14,40752,40753,40754,27943,40756,40758],{},"For example, counting down from ",[33,40755,1247],{},[33,40757,490],{}," can be seen as:",[157,40760,40761,40766,40771,40775,40779,40783,40787],{},[160,40762,40763,40764],{},"print ",[33,40765,1247],{},[160,40767,40768,40769],{},"count down from ",[33,40770,1543],{},[160,40772,40763,40773],{},[33,40774,1543],{},[160,40776,40768,40777],{},[33,40778,55],{},[160,40780,40763,40781],{},[33,40782,55],{},[160,40784,40768,40785],{},[33,40786,490],{},[160,40788,40789],{},"stop",[21,40791,40793],{"id":40792},"the-two-required-parts","The two required parts",[14,40795,40796],{},"A recursive function needs both of these parts:",[157,40798,40799,40804],{},[160,40800,40801,40803],{},[331,40802,40574],{},": the condition that stops the recursion",[160,40805,40806,40808],{},[331,40807,40580],{},": the part where the function calls itself",[14,40810,40811,40812,351],{},"Without a base case, the function will keep calling itself until Python raises an error such as ",[347,40813,40815],{"href":40814},"\u002Ferrors\u002Frecursionerror-maximum-recursion-depth-exceeded-fix\u002F","RecursionError: maximum recursion depth exceeded",[14,40817,197],{},[26,40819,40821],{"className":28,"code":40820,"language":30,"meta":31,"style":31},"def countdown(n):\n    if n == 0:          # base case\n        print(\"Done\")\n        return\n    print(n)\n    countdown(n - 1)    # recursive case\n",[33,40822,40823,40835,40850,40864,40868,40878],{"__ignoreMap":31},[36,40824,40825,40827,40829,40831,40833],{"class":38,"line":39},[36,40826,8785],{"class":137},[36,40828,40596],{"class":3082},[36,40830,79],{"class":50},[36,40832,9924],{"class":3050},[36,40834,3054],{"class":50},[36,40836,40837,40839,40841,40843,40845,40847],{"class":38,"line":72},[36,40838,14166],{"class":1718},[36,40840,9908],{"class":42},[36,40842,1965],{"class":46},[36,40844,1993],{"class":54},[36,40846,822],{"class":50},[36,40848,40849],{"class":2570},"          # base case\n",[36,40851,40852,40854,40856,40858,40860,40862],{"class":38,"line":89},[36,40853,3095],{"class":75},[36,40855,79],{"class":50},[36,40857,214],{"class":141},[36,40859,40625],{"class":145},[36,40861,214],{"class":141},[36,40863,86],{"class":50},[36,40865,40866],{"class":38,"line":496},[36,40867,40634],{"class":1718},[36,40869,40870,40872,40874,40876],{"class":38,"line":501},[36,40871,1735],{"class":75},[36,40873,79],{"class":50},[36,40875,9924],{"class":82},[36,40877,86],{"class":50},[36,40879,40880,40882,40884,40886,40888,40890,40892],{"class":38,"line":522},[36,40881,40649],{"class":82},[36,40883,79],{"class":50},[36,40885,12856],{"class":82},[36,40887,25103],{"class":46},[36,40889,16839],{"class":54},[36,40891,1988],{"class":50},[36,40893,40894],{"class":2570},"    # recursive case\n",[14,40896,40897],{},"The key idea is that the recursive call must move toward the base case.",[21,40899,40901],{"id":40900},"how-recursion-works-step-by-step","How recursion works step by step",[14,40903,40904,40905,351],{},"Let’s walk through ",[33,40906,40907],{},"countdown(3)",[26,40909,40910],{"className":28,"code":40587,"language":30,"meta":31,"style":31},[33,40911,40912,40924,40936,40950,40954,40964,40978,40982],{"__ignoreMap":31},[36,40913,40914,40916,40918,40920,40922],{"class":38,"line":39},[36,40915,8785],{"class":137},[36,40917,40596],{"class":3082},[36,40919,79],{"class":50},[36,40921,9924],{"class":3050},[36,40923,3054],{"class":50},[36,40925,40926,40928,40930,40932,40934],{"class":38,"line":72},[36,40927,14166],{"class":1718},[36,40929,9908],{"class":42},[36,40931,1965],{"class":46},[36,40933,1993],{"class":54},[36,40935,1730],{"class":50},[36,40937,40938,40940,40942,40944,40946,40948],{"class":38,"line":89},[36,40939,3095],{"class":75},[36,40941,79],{"class":50},[36,40943,214],{"class":141},[36,40945,40625],{"class":145},[36,40947,214],{"class":141},[36,40949,86],{"class":50},[36,40951,40952],{"class":38,"line":496},[36,40953,40634],{"class":1718},[36,40955,40956,40958,40960,40962],{"class":38,"line":501},[36,40957,1735],{"class":75},[36,40959,79],{"class":50},[36,40961,9924],{"class":82},[36,40963,86],{"class":50},[36,40965,40966,40968,40970,40972,40974,40976],{"class":38,"line":522},[36,40967,40649],{"class":82},[36,40969,79],{"class":50},[36,40971,12856],{"class":82},[36,40973,25103],{"class":46},[36,40975,16839],{"class":54},[36,40977,86],{"class":50},[36,40979,40980],{"class":38,"line":527},[36,40981,476],{"emptyLinePlaceholder":475},[36,40983,40984,40986,40988,40990],{"class":38,"line":3092},[36,40985,40668],{"class":82},[36,40987,79],{"class":50},[36,40989,1247],{"class":54},[36,40991,86],{"class":50},[14,40993,40994],{},"Step by step:",[27331,40996,40997,41004,41010,41016,41021,41027,41032,41039],{},[160,40998,40999,41001,41002],{},[33,41000,40907],{}," prints ",[33,41003,1247],{},[160,41005,41006,41007],{},"It calls ",[33,41008,41009],{},"countdown(2)",[160,41011,41012,41001,41014],{},[33,41013,41009],{},[33,41015,1543],{},[160,41017,41006,41018],{},[33,41019,41020],{},"countdown(1)",[160,41022,41023,41001,41025],{},[33,41024,41020],{},[33,41026,55],{},[160,41028,41006,41029],{},[33,41030,41031],{},"countdown(0)",[160,41033,41034,41036,41037],{},[33,41035,41031],{}," reaches the base case and prints ",[33,41038,40625],{},[160,41040,41041],{},"The function calls then finish one by one",[14,41043,41044],{},"In other words:",[157,41046,41047,41050,41053,41056],{},[160,41048,41049],{},"The function starts with one input value",[160,41051,41052],{},"It calls itself with a smaller value",[160,41054,41055],{},"This repeats until the base case is reached",[160,41057,41058],{},"Then the calls return back up",[21,41060,41062],{"id":41061},"simple-beginner-examples","Simple beginner examples",[1065,41064,41066],{"id":41065},"countdown-from-a-number-to-zero","Countdown from a number to zero",[26,41068,41070],{"className":28,"code":41069,"language":30,"meta":31,"style":31},"def countdown(n):\n    if n == 0:\n        print(\"Done\")\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(5)\n",[33,41071,41072,41084,41096,41110,41114,41124,41138,41142],{"__ignoreMap":31},[36,41073,41074,41076,41078,41080,41082],{"class":38,"line":39},[36,41075,8785],{"class":137},[36,41077,40596],{"class":3082},[36,41079,79],{"class":50},[36,41081,9924],{"class":3050},[36,41083,3054],{"class":50},[36,41085,41086,41088,41090,41092,41094],{"class":38,"line":72},[36,41087,14166],{"class":1718},[36,41089,9908],{"class":42},[36,41091,1965],{"class":46},[36,41093,1993],{"class":54},[36,41095,1730],{"class":50},[36,41097,41098,41100,41102,41104,41106,41108],{"class":38,"line":89},[36,41099,3095],{"class":75},[36,41101,79],{"class":50},[36,41103,214],{"class":141},[36,41105,40625],{"class":145},[36,41107,214],{"class":141},[36,41109,86],{"class":50},[36,41111,41112],{"class":38,"line":496},[36,41113,40634],{"class":1718},[36,41115,41116,41118,41120,41122],{"class":38,"line":501},[36,41117,1735],{"class":75},[36,41119,79],{"class":50},[36,41121,9924],{"class":82},[36,41123,86],{"class":50},[36,41125,41126,41128,41130,41132,41134,41136],{"class":38,"line":522},[36,41127,40649],{"class":82},[36,41129,79],{"class":50},[36,41131,12856],{"class":82},[36,41133,25103],{"class":46},[36,41135,16839],{"class":54},[36,41137,86],{"class":50},[36,41139,41140],{"class":38,"line":527},[36,41141,476],{"emptyLinePlaceholder":475},[36,41143,41144,41146,41148,41150],{"class":38,"line":3092},[36,41145,40668],{"class":82},[36,41147,79],{"class":50},[36,41149,1243],{"class":54},[36,41151,86],{"class":50},[14,41153,41154],{},"This is a good first recursion example because it is easy to follow.",[1065,41156,41158],{"id":41157},"factorial-of-a-number","Factorial of a number",[14,41160,41161],{},"A factorial multiplies a number by all positive integers below it.",[14,41163,1235],{},[157,41165,41166,41171],{},[160,41167,41168],{},[33,41169,41170],{},"4! = 4 × 3 × 2 × 1",[160,41172,41173,41174],{},"so the result is ",[33,41175,41176],{},"24",[14,41178,41179],{},"Recursive version:",[26,41181,41183],{"className":28,"code":41182,"language":30,"meta":31,"style":31},"def factorial(n):\n    if n == 1:\n        return 1\n    return n * factorial(n - 1)\n\nprint(factorial(4))\n",[33,41184,41185,41198,41210,41216,41236,41240],{"__ignoreMap":31},[36,41186,41187,41189,41192,41194,41196],{"class":38,"line":39},[36,41188,8785],{"class":137},[36,41190,41191],{"class":3082}," factorial",[36,41193,79],{"class":50},[36,41195,9924],{"class":3050},[36,41197,3054],{"class":50},[36,41199,41200,41202,41204,41206,41208],{"class":38,"line":72},[36,41201,14166],{"class":1718},[36,41203,9908],{"class":42},[36,41205,1965],{"class":46},[36,41207,16839],{"class":54},[36,41209,1730],{"class":50},[36,41211,41212,41214],{"class":38,"line":89},[36,41213,17379],{"class":1718},[36,41215,9884],{"class":54},[36,41217,41218,41220,41222,41224,41226,41228,41230,41232,41234],{"class":38,"line":496},[36,41219,9201],{"class":1718},[36,41221,9908],{"class":42},[36,41223,10824],{"class":46},[36,41225,41191],{"class":82},[36,41227,79],{"class":50},[36,41229,12856],{"class":82},[36,41231,25103],{"class":46},[36,41233,16839],{"class":54},[36,41235,86],{"class":50},[36,41237,41238],{"class":38,"line":501},[36,41239,476],{"emptyLinePlaceholder":475},[36,41241,41242,41244,41246,41249,41251,41253],{"class":38,"line":522},[36,41243,76],{"class":75},[36,41245,79],{"class":50},[36,41247,41248],{"class":82},"factorial",[36,41250,79],{"class":50},[36,41252,11622],{"class":54},[36,41254,104],{"class":50},[14,41256,107],{},[26,41258,41260],{"className":28,"code":41259,"language":30,"meta":31,"style":31},"24\n",[33,41261,41262],{"__ignoreMap":31},[36,41263,41264],{"class":38,"line":39},[36,41265,41259],{"class":54},[14,41267,9951],{},[157,41269,41270,41276],{},[160,41271,41272,41273],{},"The base case is ",[33,41274,41275],{},"n == 1",[160,41277,41278,41279],{},"The recursive case is ",[33,41280,41281],{},"n * factorial(n - 1)",[1065,41283,41285],{"id":41284},"walking-through-nested-data-structures","Walking through nested data structures",[14,41287,41288],{},"Recursion is often useful when data contains smaller versions of itself.",[14,41290,41291],{},"Example with a nested list:",[26,41293,41295],{"className":28,"code":41294,"language":30,"meta":31,"style":31},"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",[33,41296,41297,41310,41322,41340,41351,41358,41369,41373,41412],{"__ignoreMap":31},[36,41298,41299,41301,41304,41306,41308],{"class":38,"line":39},[36,41300,8785],{"class":137},[36,41302,41303],{"class":3082}," print_items",[36,41305,79],{"class":50},[36,41307,17080],{"class":3050},[36,41309,3054],{"class":50},[36,41311,41312,41314,41316,41318,41320],{"class":38,"line":72},[36,41313,34239],{"class":1718},[36,41315,10439],{"class":42},[36,41317,9911],{"class":1718},[36,41319,33486],{"class":42},[36,41321,1730],{"class":50},[36,41323,41324,41327,41330,41332,41334,41336,41338],{"class":38,"line":89},[36,41325,41326],{"class":1718},"        if",[36,41328,41329],{"class":75}," isinstance",[36,41331,79],{"class":50},[36,41333,10455],{"class":82},[36,41335,58],{"class":50},[36,41337,10542],{"class":96},[36,41339,3054],{"class":50},[36,41341,41342,41345,41347,41349],{"class":38,"line":496},[36,41343,41344],{"class":82},"            print_items",[36,41346,79],{"class":50},[36,41348,10455],{"class":82},[36,41350,86],{"class":50},[36,41352,41353,41356],{"class":38,"line":501},[36,41354,41355],{"class":1718},"        else",[36,41357,1730],{"class":50},[36,41359,41360,41363,41365,41367],{"class":38,"line":522},[36,41361,41362],{"class":75},"            print",[36,41364,79],{"class":50},[36,41366,10455],{"class":82},[36,41368,86],{"class":50},[36,41370,41371],{"class":38,"line":527},[36,41372,476],{"emptyLinePlaceholder":475},[36,41374,41375,41377,41379,41381,41383,41385,41387,41389,41391,41393,41395,41397,41399,41401,41403,41405,41407,41409],{"class":38,"line":3092},[36,41376,25900],{"class":42},[36,41378,47],{"class":46},[36,41380,51],{"class":50},[36,41382,55],{"class":54},[36,41384,58],{"class":50},[36,41386,51],{"class":50},[36,41388,1543],{"class":54},[36,41390,58],{"class":50},[36,41392,66],{"class":54},[36,41394,23999],{"class":50},[36,41396,51],{"class":50},[36,41398,11622],{"class":54},[36,41400,58],{"class":50},[36,41402,51],{"class":50},[36,41404,1243],{"class":54},[36,41406,58],{"class":50},[36,41408,12225],{"class":54},[36,41410,41411],{"class":50},"]]]\n",[36,41413,41414,41417,41419,41421],{"class":38,"line":3118},[36,41415,41416],{"class":82},"print_items",[36,41418,79],{"class":50},[36,41420,25933],{"class":82},[36,41422,86],{"class":50},[14,41424,107],{},[26,41426,41428],{"className":28,"code":41427,"language":30,"meta":31,"style":31},"1\n2\n3\n4\n5\n6\n",[33,41429,41430,41434,41438,41442,41446,41450],{"__ignoreMap":31},[36,41431,41432],{"class":38,"line":39},[36,41433,9938],{"class":54},[36,41435,41436],{"class":38,"line":72},[36,41437,9943],{"class":54},[36,41439,41440],{"class":38,"line":89},[36,41441,9948],{"class":54},[36,41443,41444],{"class":38,"line":496},[36,41445,10891],{"class":54},[36,41447,41448],{"class":38,"line":501},[36,41449,4825],{"class":54},[36,41451,41452],{"class":38,"line":522},[36,41453,9435],{"class":54},[14,41455,41456],{},"This works because when the function finds another list, it calls itself again to process that smaller list.",[21,41458,41460],{"id":41459},"when-recursion-is-useful","When recursion is useful",[14,41462,41463],{},"Recursion is useful when a problem naturally repeats in smaller versions.",[14,41465,18299],{},[157,41467,41468,41471,41474,41477],{},[160,41469,41470],{},"Tree-like structures",[160,41472,41473],{},"Nested lists",[160,41475,41476],{},"Folder and file structures",[160,41478,41479],{},"Problems where each step looks like the previous step",[14,41481,41482],{},"Recursion can also make some code easier to read than a loop, especially when the data is nested.",[14,41484,41485,41486,41488],{},"Still, recursion is not always the best choice. For many simple repeated tasks, ",[347,41487,15848],{"href":15847}," will be easier to understand.",[21,41490,41492],{"id":41491},"when-beginners-should-avoid-recursion","When beginners should avoid recursion",[14,41494,41495],{},"Beginners should be careful with recursion in these situations:",[157,41497,41498,41501,41504],{},[160,41499,41500],{},"Simple counting tasks are often easier with loops",[160,41502,41503],{},"Deep recursion can hit Python’s recursion limit",[160,41505,41506],{},"Recursive code can be harder to debug at first",[14,41508,41509],{},"For example, if you just need to repeat something 10 times, a loop is usually clearer than a recursive function.",[14,41511,41512,41513,1281,41517,351],{},"If you want to compare the two ideas, it helps to understand ",[347,41514,41516],{"href":41515},"\u002Fglossary\u002Fwhat-is-a-loop-in-python\u002F","what is a loop in Python",[347,41518,8993],{"href":8992},[21,41520,41522],{"id":41521},"common-recursion-problems","Common recursion problems",[14,41524,41525],{},"These are the most common beginner mistakes with recursion:",[157,41527,41528,41531,41534,41537,41540],{},[160,41529,41530],{},"Forgetting the base case",[160,41532,41533],{},"Writing a base case that is never reached",[160,41535,41536],{},"Changing the value in the wrong direction",[160,41538,41539],{},"Calling the function with the same value again",[160,41541,41542],{},"Expecting recursion to be faster than loops",[14,41544,41545],{},"Here is an example of a broken recursive function:",[26,41547,41549],{"className":28,"code":41548,"language":30,"meta":31,"style":31},"def countdown(n):\n    print(n)\n    countdown(n - 1)\n",[33,41550,41551,41563,41573],{"__ignoreMap":31},[36,41552,41553,41555,41557,41559,41561],{"class":38,"line":39},[36,41554,8785],{"class":137},[36,41556,40596],{"class":3082},[36,41558,79],{"class":50},[36,41560,9924],{"class":3050},[36,41562,3054],{"class":50},[36,41564,41565,41567,41569,41571],{"class":38,"line":72},[36,41566,1735],{"class":75},[36,41568,79],{"class":50},[36,41570,9924],{"class":82},[36,41572,86],{"class":50},[36,41574,41575,41577,41579,41581,41583,41585],{"class":38,"line":89},[36,41576,40649],{"class":82},[36,41578,79],{"class":50},[36,41580,12856],{"class":82},[36,41582,25103],{"class":46},[36,41584,16839],{"class":54},[36,41586,86],{"class":50},[14,41588,41589],{},"This keeps calling itself forever because there is no base case.",[14,41591,41592],{},"Here is another common mistake:",[26,41594,41596],{"className":28,"code":41595,"language":30,"meta":31,"style":31},"def countdown(n):\n    if n == 0:\n        print(\"Done\")\n        return\n    print(n)\n    countdown(n + 1)\n",[33,41597,41598,41610,41622,41636,41640,41650],{"__ignoreMap":31},[36,41599,41600,41602,41604,41606,41608],{"class":38,"line":39},[36,41601,8785],{"class":137},[36,41603,40596],{"class":3082},[36,41605,79],{"class":50},[36,41607,9924],{"class":3050},[36,41609,3054],{"class":50},[36,41611,41612,41614,41616,41618,41620],{"class":38,"line":72},[36,41613,14166],{"class":1718},[36,41615,9908],{"class":42},[36,41617,1965],{"class":46},[36,41619,1993],{"class":54},[36,41621,1730],{"class":50},[36,41623,41624,41626,41628,41630,41632,41634],{"class":38,"line":89},[36,41625,3095],{"class":75},[36,41627,79],{"class":50},[36,41629,214],{"class":141},[36,41631,40625],{"class":145},[36,41633,214],{"class":141},[36,41635,86],{"class":50},[36,41637,41638],{"class":38,"line":496},[36,41639,40634],{"class":1718},[36,41641,41642,41644,41646,41648],{"class":38,"line":501},[36,41643,1735],{"class":75},[36,41645,79],{"class":50},[36,41647,9924],{"class":82},[36,41649,86],{"class":50},[36,41651,41652,41654,41656,41658,41660,41662],{"class":38,"line":522},[36,41653,40649],{"class":82},[36,41655,79],{"class":50},[36,41657,12856],{"class":82},[36,41659,4763],{"class":46},[36,41661,16839],{"class":54},[36,41663,86],{"class":50},[14,41665,41666,41667,41670],{},"This is wrong because ",[33,41668,41669],{},"n + 1"," moves away from the base case instead of toward it.",[14,41672,41673],{},"If your code is not working, these quick checks help:",[157,41675,41676,41679,41682,41685],{},[160,41677,41678],{},"Make sure there is a base case",[160,41680,41681],{},"Make sure the base case can actually happen",[160,41683,41684],{},"Make sure each recursive call reduces the problem",[160,41686,41687],{},"Ask whether a loop would be simpler",[14,41689,41690],{},"Useful debugging tools:",[26,41692,41694],{"className":28,"code":41693,"language":30,"meta":31,"style":31},"print(n)\nprint(\"calling with:\", n)\n",[33,41695,41696,41706],{"__ignoreMap":31},[36,41697,41698,41700,41702,41704],{"class":38,"line":39},[36,41699,76],{"class":75},[36,41701,79],{"class":50},[36,41703,9924],{"class":82},[36,41705,86],{"class":50},[36,41707,41708,41710,41712,41714,41717,41719,41721,41723],{"class":38,"line":72},[36,41709,76],{"class":75},[36,41711,79],{"class":50},[36,41713,214],{"class":141},[36,41715,41716],{"class":145},"calling with:",[36,41718,214],{"class":141},[36,41720,58],{"class":50},[36,41722,13294],{"class":82},[36,41724,86],{"class":50},[14,41726,41727],{},"You can also use:",[157,41729,41730,41735],{},[160,41731,41732],{},[33,41733,41734],{},"help()",[160,41736,41737],{},[33,41738,41739],{},"traceback.print_exc()",[21,41741,1063],{"id":1062},[1065,41743,41745],{"id":41744},"is-recursion-the-same-as-a-loop","Is recursion the same as a loop?",[14,41747,41748,41749,1281,41751,351],{},"No. Both repeat work, but recursion uses function calls while loops use statements like ",[33,41750,9852],{},[33,41752,1780],{},[1065,41754,41756],{"id":41755},"what-is-a-base-case-in-recursion","What is a base case in recursion?",[14,41758,41759],{},"It is the stopping condition that prevents the function from calling itself forever.",[1065,41761,41763],{"id":41762},"why-does-python-give-a-recursion-error","Why does Python give a recursion error?",[14,41765,41766],{},"Usually because the function keeps calling itself too many times or never reaches the base case.",[1065,41768,41770],{"id":41769},"should-beginners-use-recursion","Should beginners use recursion?",[14,41772,41773],{},"Yes, to understand the idea. But for many simple tasks, loops are easier to write and debug.",[21,41775,1105],{"id":1104},[157,41777,41778,41782,41786,41790,41795],{},[160,41779,41780],{},[347,41781,8993],{"href":8992},[160,41783,41784],{},[347,41785,15848],{"href":15847},[160,41787,41788],{},[347,41789,40815],{"href":40814},[160,41791,41792],{},[347,41793,41794],{"href":20857},"What is a function in Python",[160,41796,41797],{},[347,41798,41799],{"href":41515},"What is a loop in Python",[1137,41801,41802],{},"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":31,"searchDepth":72,"depth":72,"links":41804},[41805,41806,41807,41808,41813,41814,41815,41816,41822],{"id":40729,"depth":72,"text":40730},{"id":40792,"depth":72,"text":40793},{"id":40900,"depth":72,"text":40901},{"id":41061,"depth":72,"text":41062,"children":41809},[41810,41811,41812],{"id":41065,"depth":89,"text":41066},{"id":41157,"depth":89,"text":41158},{"id":41284,"depth":89,"text":41285},{"id":41459,"depth":72,"text":41460},{"id":41491,"depth":72,"text":41492},{"id":41521,"depth":72,"text":41522},{"id":1062,"depth":72,"text":1063,"children":41817},[41818,41819,41820,41821],{"id":41744,"depth":89,"text":41745},{"id":41755,"depth":89,"text":41756},{"id":41762,"depth":89,"text":41763},{"id":41769,"depth":89,"text":41770},{"id":1104,"depth":72,"text":1105},"Master what is recursion in python in our comprehensive Python beginner guide.",{},"\u002Fglossary\u002Fwhat-is-recursion-in-python",{"title":40553,"description":41823},"glossary\u002Fwhat-is-recursion-in-python","6OR4kuwdmFeYDvo7jNoADsYvZT95NwuT_tirq6xF0RM",1776944728469]