[{"data":1,"prerenderedAt":97437},["ShallowReactive",2],{"doc-\u002Flearn\u002Fpython-if-else-and-elif-explained":3,"nav-learn":2405},{"id":4,"title":5,"body":6,"description":2398,"extension":2399,"meta":2400,"navigation":66,"path":2401,"seo":2402,"stem":2403,"__hash__":2404},"content\u002Flearn\u002Fpython-if-else-and-elif-explained.md","Python if-else and elif Explained",{"type":7,"value":8,"toc":2370},"minimark",[9,13,30,35,163,175,178,187,191,194,224,241,244,342,345,366,368,377,381,388,394,397,447,450,464,467,532,534,543,555,559,564,570,572,700,702,711,716,719,816,818,827,834,837,926,929,933,943,971,973,1063,1066,1089,1091,1100,1113,1117,1120,1123,1158,1160,1272,1274,1311,1319,1331,1334,1338,1347,1356,1359,1402,1407,1450,1454,1464,1467,1535,1542,1546,1549,1551,1608,1615,1619,1622,1715,1717,1730,1740,1743,1832,1836,1846,1852,1856,1865,1868,1870,1978,1980,1989,1992,1995,2009,2013,2023,2053,2056,2113,2116,2249,2251,2275,2278,2282,2286,2296,2300,2306,2310,2316,2320,2332,2336,2339,2343,2363,2366],[10,11,5],"h1",{"id":12},"python-if-else-and-elif-explained",[14,15,16,17,21,22,25,26,29],"p",{},"Learn how Python chooses between different code paths using ",[18,19,20],"code",{},"if",", ",[18,23,24],{},"elif",", and ",[18,27,28],{},"else",". This page helps beginners read and write basic conditional logic with clear examples.",[31,32,34],"h2",{"id":33},"quick-example","Quick example",[36,37,42],"pre",{"className":38,"code":39,"language":40,"meta":41,"style":41},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","age = 18\n\nif age \u003C 13:\n    print(\"child\")\nelif age \u003C 18:\n    print(\"teen\")\nelse:\n    print(\"adult\")\n","python","",[18,43,44,61,68,87,110,124,140,147],{"__ignoreMap":41},[45,46,49,53,57],"span",{"class":47,"line":48},"line",1,[45,50,52],{"class":51},"su5hD","age ",[45,54,56],{"class":55},"smGrS","=",[45,58,60],{"class":59},"srdBf"," 18\n",[45,62,64],{"class":47,"line":63},2,[45,65,67],{"emptyLinePlaceholder":66},true,"\n",[45,69,71,74,77,80,83],{"class":47,"line":70},3,[45,72,20],{"class":73},"sVHd0",[45,75,76],{"class":51}," age ",[45,78,79],{"class":55},"\u003C",[45,81,82],{"class":59}," 13",[45,84,86],{"class":85},"sP7_E",":\n",[45,88,90,94,97,101,105,107],{"class":47,"line":89},4,[45,91,93],{"class":92},"sptTA","    print",[45,95,96],{"class":85},"(",[45,98,100],{"class":99},"sjJ54","\"",[45,102,104],{"class":103},"s_sjI","child",[45,106,100],{"class":99},[45,108,109],{"class":85},")\n",[45,111,113,115,117,119,122],{"class":47,"line":112},5,[45,114,24],{"class":73},[45,116,76],{"class":51},[45,118,79],{"class":55},[45,120,121],{"class":59}," 18",[45,123,86],{"class":85},[45,125,127,129,131,133,136,138],{"class":47,"line":126},6,[45,128,93],{"class":92},[45,130,96],{"class":85},[45,132,100],{"class":99},[45,134,135],{"class":103},"teen",[45,137,100],{"class":99},[45,139,109],{"class":85},[45,141,143,145],{"class":47,"line":142},7,[45,144,28],{"class":73},[45,146,86],{"class":85},[45,148,150,152,154,156,159,161],{"class":47,"line":149},8,[45,151,93],{"class":92},[45,153,96],{"class":85},[45,155,100],{"class":99},[45,157,158],{"class":103},"adult",[45,160,100],{"class":99},[45,162,109],{"class":85},[14,164,165,166,168,169,171,172,174],{},"Use ",[18,167,20],{}," for the first condition, ",[18,170,24],{}," for extra conditions, and ",[18,173,28],{}," for the fallback case.",[14,176,177],{},"Output:",[36,179,181],{"className":38,"code":180,"language":40,"meta":41,"style":41},"adult\n",[18,182,183],{"__ignoreMap":41},[45,184,185],{"class":47,"line":48},[45,186,180],{"class":51},[31,188,190],{"id":189},"what-if-elif-and-else-do","What if, elif, and else do",[14,192,193],{},"Python uses conditional statements to decide which code should run.",[195,196,197,203,208,213],"ul",{},[198,199,200,202],"li",{},[18,201,20],{}," checks a condition first",[198,204,205,207],{},[18,206,24],{}," means “else if” and checks another condition if earlier ones were false",[198,209,210,212],{},[18,211,28],{}," runs when no earlier condition matched",[198,214,215,216,218,219,218,221,223],{},"Python runs only the first matching branch in one ",[18,217,20],{},"-",[18,220,24],{},[18,222,28],{}," chain",[14,225,226,227,230,231,234,235,240],{},"A condition is something that becomes ",[18,228,229],{},"True"," or ",[18,232,233],{},"False",". If you are new to that idea, see ",[236,237,239],"a",{"href":238},"\u002Flearn\u002Fpython-booleans-explained-true-and-false\u002F","Python booleans explained: true and false",".",[14,242,243],{},"Here is a small example:",[36,245,247],{"className":38,"code":246,"language":40,"meta":41,"style":41},"temperature = 30\n\nif temperature > 35:\n    print(\"Very hot\")\nelif temperature > 25:\n    print(\"Warm\")\nelse:\n    print(\"Cool\")\n",[18,248,249,259,263,278,293,306,321,327],{"__ignoreMap":41},[45,250,251,254,256],{"class":47,"line":48},[45,252,253],{"class":51},"temperature ",[45,255,56],{"class":55},[45,257,258],{"class":59}," 30\n",[45,260,261],{"class":47,"line":63},[45,262,67],{"emptyLinePlaceholder":66},[45,264,265,267,270,273,276],{"class":47,"line":70},[45,266,20],{"class":73},[45,268,269],{"class":51}," temperature ",[45,271,272],{"class":55},">",[45,274,275],{"class":59}," 35",[45,277,86],{"class":85},[45,279,280,282,284,286,289,291],{"class":47,"line":89},[45,281,93],{"class":92},[45,283,96],{"class":85},[45,285,100],{"class":99},[45,287,288],{"class":103},"Very hot",[45,290,100],{"class":99},[45,292,109],{"class":85},[45,294,295,297,299,301,304],{"class":47,"line":112},[45,296,24],{"class":73},[45,298,269],{"class":51},[45,300,272],{"class":55},[45,302,303],{"class":59}," 25",[45,305,86],{"class":85},[45,307,308,310,312,314,317,319],{"class":47,"line":126},[45,309,93],{"class":92},[45,311,96],{"class":85},[45,313,100],{"class":99},[45,315,316],{"class":103},"Warm",[45,318,100],{"class":99},[45,320,109],{"class":85},[45,322,323,325],{"class":47,"line":142},[45,324,28],{"class":73},[45,326,86],{"class":85},[45,328,329,331,333,335,338,340],{"class":47,"line":149},[45,330,93],{"class":92},[45,332,96],{"class":85},[45,334,100],{"class":99},[45,336,337],{"class":103},"Cool",[45,339,100],{"class":99},[45,341,109],{"class":85},[14,343,344],{},"Python checks from top to bottom:",[346,347,348,355,361],"ol",{},[198,349,350,351,354],{},"Is ",[18,352,353],{},"temperature > 35","?",[198,356,357,358,354],{},"If not, is ",[18,359,360],{},"temperature > 25",[198,362,363,364],{},"If not, run ",[18,365,28],{},[14,367,177],{},[36,369,371],{"className":38,"code":370,"language":40,"meta":41,"style":41},"Warm\n",[18,372,373],{"__ignoreMap":41},[45,374,375],{"class":47,"line":48},[45,376,370],{"class":51},[31,378,380],{"id":379},"basic-if-else-structure","Basic if-else structure",[14,382,165,383,385,386,240],{},[18,384,20],{}," when you want code to run only when a condition is ",[18,387,229],{},[14,389,390,391,393],{},"Add ",[18,392,28],{}," when you want a fallback action.",[14,395,396],{},"Basic structure:",[36,398,400],{"className":38,"code":399,"language":40,"meta":41,"style":41},"if condition:\n    print(\"This runs when the condition is True\")\nelse:\n    print(\"This runs when the condition is False\")\n",[18,401,402,411,426,432],{"__ignoreMap":41},[45,403,404,406,409],{"class":47,"line":48},[45,405,20],{"class":73},[45,407,408],{"class":51}," condition",[45,410,86],{"class":85},[45,412,413,415,417,419,422,424],{"class":47,"line":63},[45,414,93],{"class":92},[45,416,96],{"class":85},[45,418,100],{"class":99},[45,420,421],{"class":103},"This runs when the condition is True",[45,423,100],{"class":99},[45,425,109],{"class":85},[45,427,428,430],{"class":47,"line":70},[45,429,28],{"class":73},[45,431,86],{"class":85},[45,433,434,436,438,440,443,445],{"class":47,"line":89},[45,435,93],{"class":92},[45,437,96],{"class":85},[45,439,100],{"class":99},[45,441,442],{"class":103},"This runs when the condition is False",[45,444,100],{"class":99},[45,446,109],{"class":85},[14,448,449],{},"Important rules:",[195,451,452,458,461],{},[198,453,454,455],{},"The condition must end with a colon ",[18,456,457],{},":",[198,459,460],{},"The code inside each branch must be indented",[198,462,463],{},"Indentation matters in Python",[14,465,466],{},"Example:",[36,468,470],{"className":38,"code":469,"language":40,"meta":41,"style":41},"is_raining = True\n\nif is_raining:\n    print(\"Take an umbrella\")\nelse:\n    print(\"No umbrella needed\")\n",[18,471,472,483,487,496,511,517],{"__ignoreMap":41},[45,473,474,477,479],{"class":47,"line":48},[45,475,476],{"class":51},"is_raining ",[45,478,56],{"class":55},[45,480,482],{"class":481},"s39Yj"," True\n",[45,484,485],{"class":47,"line":63},[45,486,67],{"emptyLinePlaceholder":66},[45,488,489,491,494],{"class":47,"line":70},[45,490,20],{"class":73},[45,492,493],{"class":51}," is_raining",[45,495,86],{"class":85},[45,497,498,500,502,504,507,509],{"class":47,"line":89},[45,499,93],{"class":92},[45,501,96],{"class":85},[45,503,100],{"class":99},[45,505,506],{"class":103},"Take an umbrella",[45,508,100],{"class":99},[45,510,109],{"class":85},[45,512,513,515],{"class":47,"line":112},[45,514,28],{"class":73},[45,516,86],{"class":85},[45,518,519,521,523,525,528,530],{"class":47,"line":126},[45,520,93],{"class":92},[45,522,96],{"class":85},[45,524,100],{"class":99},[45,526,527],{"class":103},"No umbrella needed",[45,529,100],{"class":99},[45,531,109],{"class":85},[14,533,177],{},[36,535,537],{"className":38,"code":536,"language":40,"meta":41,"style":41},"Take an umbrella\n",[18,538,539],{"__ignoreMap":41},[45,540,541],{"class":47,"line":48},[45,542,536],{"class":51},[14,544,545,546,550,551,240],{},"If you want a more focused introduction, see ",[236,547,549],{"href":548},"\u002Flearn\u002Fpython-if-statements-explained\u002F","Python if statements explained",". If your code fails because of spacing, read ",[236,552,554],{"href":553},"\u002Flearn\u002Fpython-indentation-rules-and-why-they-matter\u002F","Python indentation rules and why they matter",[31,556,558],{"id":557},"how-elif-works","How elif works",[14,560,165,561,563],{},[18,562,24],{}," when there are multiple possible cases.",[14,565,566,567,569],{},"Python checks conditions from top to bottom. The first ",[18,568,229],{}," condition wins, and the rest are skipped.",[14,571,466],{},[36,573,575],{"className":38,"code":574,"language":40,"meta":41,"style":41},"score = 82\n\nif score >= 90:\n    print(\"Grade A\")\nelif score >= 80:\n    print(\"Grade B\")\nelif score >= 70:\n    print(\"Grade C\")\nelse:\n    print(\"Grade D or below\")\n",[18,576,577,587,591,606,621,634,649,662,677,684],{"__ignoreMap":41},[45,578,579,582,584],{"class":47,"line":48},[45,580,581],{"class":51},"score ",[45,583,56],{"class":55},[45,585,586],{"class":59}," 82\n",[45,588,589],{"class":47,"line":63},[45,590,67],{"emptyLinePlaceholder":66},[45,592,593,595,598,601,604],{"class":47,"line":70},[45,594,20],{"class":73},[45,596,597],{"class":51}," score ",[45,599,600],{"class":55},">=",[45,602,603],{"class":59}," 90",[45,605,86],{"class":85},[45,607,608,610,612,614,617,619],{"class":47,"line":89},[45,609,93],{"class":92},[45,611,96],{"class":85},[45,613,100],{"class":99},[45,615,616],{"class":103},"Grade A",[45,618,100],{"class":99},[45,620,109],{"class":85},[45,622,623,625,627,629,632],{"class":47,"line":112},[45,624,24],{"class":73},[45,626,597],{"class":51},[45,628,600],{"class":55},[45,630,631],{"class":59}," 80",[45,633,86],{"class":85},[45,635,636,638,640,642,645,647],{"class":47,"line":126},[45,637,93],{"class":92},[45,639,96],{"class":85},[45,641,100],{"class":99},[45,643,644],{"class":103},"Grade B",[45,646,100],{"class":99},[45,648,109],{"class":85},[45,650,651,653,655,657,660],{"class":47,"line":142},[45,652,24],{"class":73},[45,654,597],{"class":51},[45,656,600],{"class":55},[45,658,659],{"class":59}," 70",[45,661,86],{"class":85},[45,663,664,666,668,670,673,675],{"class":47,"line":149},[45,665,93],{"class":92},[45,667,96],{"class":85},[45,669,100],{"class":99},[45,671,672],{"class":103},"Grade C",[45,674,100],{"class":99},[45,676,109],{"class":85},[45,678,680,682],{"class":47,"line":679},9,[45,681,28],{"class":73},[45,683,86],{"class":85},[45,685,687,689,691,693,696,698],{"class":47,"line":686},10,[45,688,93],{"class":92},[45,690,96],{"class":85},[45,692,100],{"class":99},[45,694,695],{"class":103},"Grade D or below",[45,697,100],{"class":99},[45,699,109],{"class":85},[14,701,177],{},[36,703,705],{"className":38,"code":704,"language":40,"meta":41,"style":41},"Grade B\n",[18,706,707],{"__ignoreMap":41},[45,708,709],{"class":47,"line":48},[45,710,704],{"class":51},[712,713,715],"h3",{"id":714},"why-order-matters","Why order matters",[14,717,718],{},"Look at this example:",[36,720,722],{"className":38,"code":721,"language":40,"meta":41,"style":41},"number = 5\n\nif number > 0:\n    print(\"Positive\")\nelif number > 3:\n    print(\"Greater than 3\")\nelse:\n    print(\"Other\")\n",[18,723,724,734,738,752,767,780,795,801],{"__ignoreMap":41},[45,725,726,729,731],{"class":47,"line":48},[45,727,728],{"class":51},"number ",[45,730,56],{"class":55},[45,732,733],{"class":59}," 5\n",[45,735,736],{"class":47,"line":63},[45,737,67],{"emptyLinePlaceholder":66},[45,739,740,742,745,747,750],{"class":47,"line":70},[45,741,20],{"class":73},[45,743,744],{"class":51}," number ",[45,746,272],{"class":55},[45,748,749],{"class":59}," 0",[45,751,86],{"class":85},[45,753,754,756,758,760,763,765],{"class":47,"line":89},[45,755,93],{"class":92},[45,757,96],{"class":85},[45,759,100],{"class":99},[45,761,762],{"class":103},"Positive",[45,764,100],{"class":99},[45,766,109],{"class":85},[45,768,769,771,773,775,778],{"class":47,"line":112},[45,770,24],{"class":73},[45,772,744],{"class":51},[45,774,272],{"class":55},[45,776,777],{"class":59}," 3",[45,779,86],{"class":85},[45,781,782,784,786,788,791,793],{"class":47,"line":126},[45,783,93],{"class":92},[45,785,96],{"class":85},[45,787,100],{"class":99},[45,789,790],{"class":103},"Greater than 3",[45,792,100],{"class":99},[45,794,109],{"class":85},[45,796,797,799],{"class":47,"line":142},[45,798,28],{"class":73},[45,800,86],{"class":85},[45,802,803,805,807,809,812,814],{"class":47,"line":149},[45,804,93],{"class":92},[45,806,96],{"class":85},[45,808,100],{"class":99},[45,810,811],{"class":103},"Other",[45,813,100],{"class":99},[45,815,109],{"class":85},[14,817,177],{},[36,819,821],{"className":38,"code":820,"language":40,"meta":41,"style":41},"Positive\n",[18,822,823],{"__ignoreMap":41},[45,824,825],{"class":47,"line":48},[45,826,820],{"class":51},[14,828,829,830,833],{},"Even though ",[18,831,832],{},"number > 3"," is also true, Python never gets that far. The first matching branch already ran.",[14,835,836],{},"When needed, write more specific conditions before broader ones:",[36,838,840],{"className":38,"code":839,"language":40,"meta":41,"style":41},"number = 5\n\nif number > 3:\n    print(\"Greater than 3\")\nelif number > 0:\n    print(\"Positive\")\nelse:\n    print(\"Other\")\n",[18,841,842,850,854,866,880,892,906,912],{"__ignoreMap":41},[45,843,844,846,848],{"class":47,"line":48},[45,845,728],{"class":51},[45,847,56],{"class":55},[45,849,733],{"class":59},[45,851,852],{"class":47,"line":63},[45,853,67],{"emptyLinePlaceholder":66},[45,855,856,858,860,862,864],{"class":47,"line":70},[45,857,20],{"class":73},[45,859,744],{"class":51},[45,861,272],{"class":55},[45,863,777],{"class":59},[45,865,86],{"class":85},[45,867,868,870,872,874,876,878],{"class":47,"line":89},[45,869,93],{"class":92},[45,871,96],{"class":85},[45,873,100],{"class":99},[45,875,790],{"class":103},[45,877,100],{"class":99},[45,879,109],{"class":85},[45,881,882,884,886,888,890],{"class":47,"line":112},[45,883,24],{"class":73},[45,885,744],{"class":51},[45,887,272],{"class":55},[45,889,749],{"class":59},[45,891,86],{"class":85},[45,893,894,896,898,900,902,904],{"class":47,"line":126},[45,895,93],{"class":92},[45,897,96],{"class":85},[45,899,100],{"class":99},[45,901,762],{"class":103},[45,903,100],{"class":99},[45,905,109],{"class":85},[45,907,908,910],{"class":47,"line":142},[45,909,28],{"class":73},[45,911,86],{"class":85},[45,913,914,916,918,920,922,924],{"class":47,"line":149},[45,915,93],{"class":92},[45,917,96],{"class":85},[45,919,100],{"class":99},[45,921,811],{"class":103},[45,923,100],{"class":99},[45,925,109],{"class":85},[14,927,928],{},"This version gives a different result because the order changed.",[31,930,932],{"id":931},"reading-conditions-in-order","Reading conditions in order",[14,934,935,936,218,938,218,940,942],{},"A good way to understand an ",[18,937,20],{},[18,939,24],{},[18,941,28],{}," chain is to read it step by step:",[195,944,945,950,958,963],{},[198,946,947,948,408],{},"Start with the first ",[18,949,20],{},[198,951,952,953,955,956],{},"If it is ",[18,954,233],{},", move to the next ",[18,957,24],{},[198,959,960,961],{},"Keep checking until one is ",[18,962,229],{},[198,964,965,966,21,968,970],{},"If none are ",[18,967,229],{},[18,969,28],{}," runs if it exists",[14,972,466],{},[36,974,976],{"className":38,"code":975,"language":40,"meta":41,"style":41},"age = 16\n\nif age \u003C 13:\n    print(\"child\")\nelif age \u003C 18:\n    print(\"teen\")\nelse:\n    print(\"adult\")\n",[18,977,978,987,991,1003,1017,1029,1043,1049],{"__ignoreMap":41},[45,979,980,982,984],{"class":47,"line":48},[45,981,52],{"class":51},[45,983,56],{"class":55},[45,985,986],{"class":59}," 16\n",[45,988,989],{"class":47,"line":63},[45,990,67],{"emptyLinePlaceholder":66},[45,992,993,995,997,999,1001],{"class":47,"line":70},[45,994,20],{"class":73},[45,996,76],{"class":51},[45,998,79],{"class":55},[45,1000,82],{"class":59},[45,1002,86],{"class":85},[45,1004,1005,1007,1009,1011,1013,1015],{"class":47,"line":89},[45,1006,93],{"class":92},[45,1008,96],{"class":85},[45,1010,100],{"class":99},[45,1012,104],{"class":103},[45,1014,100],{"class":99},[45,1016,109],{"class":85},[45,1018,1019,1021,1023,1025,1027],{"class":47,"line":112},[45,1020,24],{"class":73},[45,1022,76],{"class":51},[45,1024,79],{"class":55},[45,1026,121],{"class":59},[45,1028,86],{"class":85},[45,1030,1031,1033,1035,1037,1039,1041],{"class":47,"line":126},[45,1032,93],{"class":92},[45,1034,96],{"class":85},[45,1036,100],{"class":99},[45,1038,135],{"class":103},[45,1040,100],{"class":99},[45,1042,109],{"class":85},[45,1044,1045,1047],{"class":47,"line":142},[45,1046,28],{"class":73},[45,1048,86],{"class":85},[45,1050,1051,1053,1055,1057,1059,1061],{"class":47,"line":149},[45,1052,93],{"class":92},[45,1054,96],{"class":85},[45,1056,100],{"class":99},[45,1058,158],{"class":103},[45,1060,100],{"class":99},[45,1062,109],{"class":85},[14,1064,1065],{},"Python reads it like this:",[195,1067,1068,1074,1080,1086],{},[198,1069,350,1070,1073],{},[18,1071,1072],{},"age \u003C 13","? No",[198,1075,350,1076,1079],{},[18,1077,1078],{},"age \u003C 18","? Yes",[198,1081,1082,1083],{},"Run ",[18,1084,1085],{},"print(\"teen\")",[198,1087,1088],{},"Stop checking the chain",[14,1090,177],{},[36,1092,1094],{"className":38,"code":1093,"language":40,"meta":41,"style":41},"teen\n",[18,1095,1096],{"__ignoreMap":41},[45,1097,1098],{"class":47,"line":48},[45,1099,1093],{"class":51},[14,1101,1102,1103,218,1105,218,1107,1109,1110,1112],{},"This is why ",[18,1104,20],{},[18,1106,24],{},[18,1108,28],{}," is different from writing several separate ",[18,1111,20],{}," statements.",[31,1114,1116],{"id":1115},"common-comparison-operators-used-in-conditions","Common comparison operators used in conditions",[14,1118,1119],{},"You often compare values inside conditions.",[14,1121,1122],{},"Common comparison operators:",[195,1124,1125,1131,1137,1142,1147,1152],{},[198,1126,1127,1130],{},[18,1128,1129],{},"=="," means equal to",[198,1132,1133,1136],{},[18,1134,1135],{},"!="," means not equal to",[198,1138,1139,1141],{},[18,1140,272],{}," means greater than",[198,1143,1144,1146],{},[18,1145,79],{}," means less than",[198,1148,1149,1151],{},[18,1150,600],{}," means greater than or equal to",[198,1153,1154,1157],{},[18,1155,1156],{},"\u003C="," means less than or equal to",[14,1159,466],{},[36,1161,1163],{"className":38,"code":1162,"language":40,"meta":41,"style":41},"x = 10\n\nif x == 10:\n    print(\"x is 10\")\n\nif x != 5:\n    print(\"x is not 5\")\n\nif x >= 8:\n    print(\"x is at least 8\")\n",[18,1164,1165,1175,1179,1193,1208,1212,1225,1240,1244,1257],{"__ignoreMap":41},[45,1166,1167,1170,1172],{"class":47,"line":48},[45,1168,1169],{"class":51},"x ",[45,1171,56],{"class":55},[45,1173,1174],{"class":59}," 10\n",[45,1176,1177],{"class":47,"line":63},[45,1178,67],{"emptyLinePlaceholder":66},[45,1180,1181,1183,1186,1188,1191],{"class":47,"line":70},[45,1182,20],{"class":73},[45,1184,1185],{"class":51}," x ",[45,1187,1129],{"class":55},[45,1189,1190],{"class":59}," 10",[45,1192,86],{"class":85},[45,1194,1195,1197,1199,1201,1204,1206],{"class":47,"line":89},[45,1196,93],{"class":92},[45,1198,96],{"class":85},[45,1200,100],{"class":99},[45,1202,1203],{"class":103},"x is 10",[45,1205,100],{"class":99},[45,1207,109],{"class":85},[45,1209,1210],{"class":47,"line":112},[45,1211,67],{"emptyLinePlaceholder":66},[45,1213,1214,1216,1218,1220,1223],{"class":47,"line":126},[45,1215,20],{"class":73},[45,1217,1185],{"class":51},[45,1219,1135],{"class":55},[45,1221,1222],{"class":59}," 5",[45,1224,86],{"class":85},[45,1226,1227,1229,1231,1233,1236,1238],{"class":47,"line":142},[45,1228,93],{"class":92},[45,1230,96],{"class":85},[45,1232,100],{"class":99},[45,1234,1235],{"class":103},"x is not 5",[45,1237,100],{"class":99},[45,1239,109],{"class":85},[45,1241,1242],{"class":47,"line":149},[45,1243,67],{"emptyLinePlaceholder":66},[45,1245,1246,1248,1250,1252,1255],{"class":47,"line":679},[45,1247,20],{"class":73},[45,1249,1185],{"class":51},[45,1251,600],{"class":55},[45,1253,1254],{"class":59}," 8",[45,1256,86],{"class":85},[45,1258,1259,1261,1263,1265,1268,1270],{"class":47,"line":686},[45,1260,93],{"class":92},[45,1262,96],{"class":85},[45,1264,100],{"class":99},[45,1266,1267],{"class":103},"x is at least 8",[45,1269,100],{"class":99},[45,1271,109],{"class":85},[14,1273,177],{},[36,1275,1277],{"className":38,"code":1276,"language":40,"meta":41,"style":41},"x is 10\nx is not 5\nx is at least 8\n",[18,1278,1279,1288,1299],{"__ignoreMap":41},[45,1280,1281,1283,1286],{"class":47,"line":48},[45,1282,1169],{"class":51},[45,1284,1285],{"class":55},"is",[45,1287,1174],{"class":59},[45,1289,1290,1292,1294,1297],{"class":47,"line":63},[45,1291,1169],{"class":51},[45,1293,1285],{"class":55},[45,1295,1296],{"class":55}," not",[45,1298,733],{"class":59},[45,1300,1301,1303,1305,1308],{"class":47,"line":70},[45,1302,1169],{"class":51},[45,1304,1285],{"class":55},[45,1306,1307],{"class":51}," at least ",[45,1309,1310],{"class":59},"8\n",[14,1312,1313,1314,1316,1317,240],{},"Be careful with ",[18,1315,56],{}," and ",[18,1318,1129],{},[195,1320,1321,1326],{},[198,1322,1323,1325],{},[18,1324,56],{}," assigns a value",[198,1327,1328,1330],{},[18,1329,1129],{}," compares two values",[14,1332,1333],{},"This is a very common beginner mistake.",[31,1335,1337],{"id":1336},"common-beginner-mistakes","Common beginner mistakes",[14,1339,1340,1341,21,1343,25,1345,240],{},"Here are some problems beginners often run into with ",[18,1342,20],{},[18,1344,24],{},[18,1346,28],{},[712,1348,1350,1351,1353,1354],{"id":1349},"using-instead-of","Using ",[18,1352,56],{}," instead of ",[18,1355,1129],{},[14,1357,1358],{},"This is wrong:",[36,1360,1362],{"className":38,"code":1361,"language":40,"meta":41,"style":41},"age = 18\n\nif age = 18:\n    print(\"adult\")\n",[18,1363,1364,1372,1376,1388],{"__ignoreMap":41},[45,1365,1366,1368,1370],{"class":47,"line":48},[45,1367,52],{"class":51},[45,1369,56],{"class":55},[45,1371,60],{"class":59},[45,1373,1374],{"class":47,"line":63},[45,1375,67],{"emptyLinePlaceholder":66},[45,1377,1378,1380,1382,1384,1386],{"class":47,"line":70},[45,1379,20],{"class":73},[45,1381,76],{"class":51},[45,1383,56],{"class":55},[45,1385,121],{"class":59},[45,1387,86],{"class":85},[45,1389,1390,1392,1394,1396,1398,1400],{"class":47,"line":89},[45,1391,93],{"class":92},[45,1393,96],{"class":85},[45,1395,100],{"class":99},[45,1397,158],{"class":103},[45,1399,100],{"class":99},[45,1401,109],{"class":85},[14,1403,165,1404,1406],{},[18,1405,1129],{}," for comparison:",[36,1408,1410],{"className":38,"code":1409,"language":40,"meta":41,"style":41},"age = 18\n\nif age == 18:\n    print(\"adult\")\n",[18,1411,1412,1420,1424,1436],{"__ignoreMap":41},[45,1413,1414,1416,1418],{"class":47,"line":48},[45,1415,52],{"class":51},[45,1417,56],{"class":55},[45,1419,60],{"class":59},[45,1421,1422],{"class":47,"line":63},[45,1423,67],{"emptyLinePlaceholder":66},[45,1425,1426,1428,1430,1432,1434],{"class":47,"line":70},[45,1427,20],{"class":73},[45,1429,76],{"class":51},[45,1431,1129],{"class":55},[45,1433,121],{"class":59},[45,1435,86],{"class":85},[45,1437,1438,1440,1442,1444,1446,1448],{"class":47,"line":89},[45,1439,93],{"class":92},[45,1441,96],{"class":85},[45,1443,100],{"class":99},[45,1445,158],{"class":103},[45,1447,100],{"class":99},[45,1449,109],{"class":85},[712,1451,1453],{"id":1452},"forgetting-the-colon","Forgetting the colon",[14,1455,1456,1457,21,1459,25,1461,1463],{},"Each ",[18,1458,20],{},[18,1460,24],{},[18,1462,28],{}," line must end with a colon.",[14,1465,1466],{},"Correct:",[36,1468,1470],{"className":38,"code":1469,"language":40,"meta":41,"style":41},"value = 3\n\nif value > 0:\n    print(\"positive\")\nelse:\n    print(\"not positive\")\n",[18,1471,1472,1482,1486,1499,1514,1520],{"__ignoreMap":41},[45,1473,1474,1477,1479],{"class":47,"line":48},[45,1475,1476],{"class":51},"value ",[45,1478,56],{"class":55},[45,1480,1481],{"class":59}," 3\n",[45,1483,1484],{"class":47,"line":63},[45,1485,67],{"emptyLinePlaceholder":66},[45,1487,1488,1490,1493,1495,1497],{"class":47,"line":70},[45,1489,20],{"class":73},[45,1491,1492],{"class":51}," value ",[45,1494,272],{"class":55},[45,1496,749],{"class":59},[45,1498,86],{"class":85},[45,1500,1501,1503,1505,1507,1510,1512],{"class":47,"line":89},[45,1502,93],{"class":92},[45,1504,96],{"class":85},[45,1506,100],{"class":99},[45,1508,1509],{"class":103},"positive",[45,1511,100],{"class":99},[45,1513,109],{"class":85},[45,1515,1516,1518],{"class":47,"line":112},[45,1517,28],{"class":73},[45,1519,86],{"class":85},[45,1521,1522,1524,1526,1528,1531,1533],{"class":47,"line":126},[45,1523,93],{"class":92},[45,1525,96],{"class":85},[45,1527,100],{"class":99},[45,1529,1530],{"class":103},"not positive",[45,1532,100],{"class":99},[45,1534,109],{"class":85},[14,1536,1537,1538,240],{},"If you forget the colon, Python raises a syntax error. See ",[236,1539,1541],{"href":1540},"\u002Ferrors\u002Fsyntaxerror-missing-colon-fix\u002F","SyntaxError: missing colon",[712,1543,1545],{"id":1544},"incorrect-indentation","Incorrect indentation",[14,1547,1548],{},"The code inside each branch must be indented.",[14,1550,1466],{},[36,1552,1554],{"className":38,"code":1553,"language":40,"meta":41,"style":41},"name = \"Sam\"\n\nif name == \"Sam\":\n    print(\"Hello, Sam\")\n",[18,1555,1556,1572,1576,1593],{"__ignoreMap":41},[45,1557,1558,1561,1563,1566,1569],{"class":47,"line":48},[45,1559,1560],{"class":51},"name ",[45,1562,56],{"class":55},[45,1564,1565],{"class":99}," \"",[45,1567,1568],{"class":103},"Sam",[45,1570,1571],{"class":99},"\"\n",[45,1573,1574],{"class":47,"line":63},[45,1575,67],{"emptyLinePlaceholder":66},[45,1577,1578,1580,1583,1585,1587,1589,1591],{"class":47,"line":70},[45,1579,20],{"class":73},[45,1581,1582],{"class":51}," name ",[45,1584,1129],{"class":55},[45,1586,1565],{"class":99},[45,1588,1568],{"class":103},[45,1590,100],{"class":99},[45,1592,86],{"class":85},[45,1594,1595,1597,1599,1601,1604,1606],{"class":47,"line":89},[45,1596,93],{"class":92},[45,1598,96],{"class":85},[45,1600,100],{"class":99},[45,1602,1603],{"class":103},"Hello, Sam",[45,1605,100],{"class":99},[45,1607,109],{"class":85},[14,1609,1610,1611,240],{},"Bad indentation can cause an error such as ",[236,1612,1614],{"href":1613},"\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix\u002F","IndentationError: expected an indented block",[712,1616,1618],{"id":1617},"writing-overlapping-conditions-in-the-wrong-order","Writing overlapping conditions in the wrong order",[14,1620,1621],{},"This example has the wrong order:",[36,1623,1625],{"className":38,"code":1624,"language":40,"meta":41,"style":41},"age = 12\n\nif age \u003C 18:\n    print(\"Under 18\")\nelif age \u003C 13:\n    print(\"Child\")\nelse:\n    print(\"Adult\")\n",[18,1626,1627,1636,1640,1652,1667,1679,1694,1700],{"__ignoreMap":41},[45,1628,1629,1631,1633],{"class":47,"line":48},[45,1630,52],{"class":51},[45,1632,56],{"class":55},[45,1634,1635],{"class":59}," 12\n",[45,1637,1638],{"class":47,"line":63},[45,1639,67],{"emptyLinePlaceholder":66},[45,1641,1642,1644,1646,1648,1650],{"class":47,"line":70},[45,1643,20],{"class":73},[45,1645,76],{"class":51},[45,1647,79],{"class":55},[45,1649,121],{"class":59},[45,1651,86],{"class":85},[45,1653,1654,1656,1658,1660,1663,1665],{"class":47,"line":89},[45,1655,93],{"class":92},[45,1657,96],{"class":85},[45,1659,100],{"class":99},[45,1661,1662],{"class":103},"Under 18",[45,1664,100],{"class":99},[45,1666,109],{"class":85},[45,1668,1669,1671,1673,1675,1677],{"class":47,"line":112},[45,1670,24],{"class":73},[45,1672,76],{"class":51},[45,1674,79],{"class":55},[45,1676,82],{"class":59},[45,1678,86],{"class":85},[45,1680,1681,1683,1685,1687,1690,1692],{"class":47,"line":126},[45,1682,93],{"class":92},[45,1684,96],{"class":85},[45,1686,100],{"class":99},[45,1688,1689],{"class":103},"Child",[45,1691,100],{"class":99},[45,1693,109],{"class":85},[45,1695,1696,1698],{"class":47,"line":142},[45,1697,28],{"class":73},[45,1699,86],{"class":85},[45,1701,1702,1704,1706,1708,1711,1713],{"class":47,"line":149},[45,1703,93],{"class":92},[45,1705,96],{"class":85},[45,1707,100],{"class":99},[45,1709,1710],{"class":103},"Adult",[45,1712,100],{"class":99},[45,1714,109],{"class":85},[14,1716,177],{},[36,1718,1720],{"className":38,"code":1719,"language":40,"meta":41,"style":41},"Under 18\n",[18,1721,1722],{"__ignoreMap":41},[45,1723,1724,1727],{"class":47,"line":48},[45,1725,1726],{"class":51},"Under ",[45,1728,1729],{"class":59},"18\n",[14,1731,1732,1733,1736,1737,1739],{},"The ",[18,1734,1735],{},"elif age \u003C 13"," branch never runs, because ",[18,1738,1078],{}," is already true first.",[14,1741,1742],{},"A better version is:",[36,1744,1746],{"className":38,"code":1745,"language":40,"meta":41,"style":41},"age = 12\n\nif age \u003C 13:\n    print(\"Child\")\nelif age \u003C 18:\n    print(\"Under 18\")\nelse:\n    print(\"Adult\")\n",[18,1747,1748,1756,1760,1772,1786,1798,1812,1818],{"__ignoreMap":41},[45,1749,1750,1752,1754],{"class":47,"line":48},[45,1751,52],{"class":51},[45,1753,56],{"class":55},[45,1755,1635],{"class":59},[45,1757,1758],{"class":47,"line":63},[45,1759,67],{"emptyLinePlaceholder":66},[45,1761,1762,1764,1766,1768,1770],{"class":47,"line":70},[45,1763,20],{"class":73},[45,1765,76],{"class":51},[45,1767,79],{"class":55},[45,1769,82],{"class":59},[45,1771,86],{"class":85},[45,1773,1774,1776,1778,1780,1782,1784],{"class":47,"line":89},[45,1775,93],{"class":92},[45,1777,96],{"class":85},[45,1779,100],{"class":99},[45,1781,1689],{"class":103},[45,1783,100],{"class":99},[45,1785,109],{"class":85},[45,1787,1788,1790,1792,1794,1796],{"class":47,"line":112},[45,1789,24],{"class":73},[45,1791,76],{"class":51},[45,1793,79],{"class":55},[45,1795,121],{"class":59},[45,1797,86],{"class":85},[45,1799,1800,1802,1804,1806,1808,1810],{"class":47,"line":126},[45,1801,93],{"class":92},[45,1803,96],{"class":85},[45,1805,100],{"class":99},[45,1807,1662],{"class":103},[45,1809,100],{"class":99},[45,1811,109],{"class":85},[45,1813,1814,1816],{"class":47,"line":142},[45,1815,28],{"class":73},[45,1817,86],{"class":85},[45,1819,1820,1822,1824,1826,1828,1830],{"class":47,"line":149},[45,1821,93],{"class":92},[45,1823,96],{"class":85},[45,1825,100],{"class":99},[45,1827,1710],{"class":103},[45,1829,100],{"class":99},[45,1831,109],{"class":85},[712,1833,1835],{"id":1834},"expecting-more-than-one-branch-to-run","Expecting more than one branch to run",[14,1837,1838,1839,218,1841,218,1843,1845],{},"In one ",[18,1840,20],{},[18,1842,24],{},[18,1844,28],{}," chain, only one branch runs.",[14,1847,1848,1849,1851],{},"If you need multiple checks to run, you may need separate ",[18,1850,20],{}," statements instead of one chain.",[31,1853,1855],{"id":1854},"when-to-use-nested-conditions","When to use nested conditions",[14,1857,1858,1859,1861,1862,1864],{},"Nested conditions are ",[18,1860,20],{}," statements inside another ",[18,1863,20],{}," block.",[14,1866,1867],{},"Use them when a second check depends on a first check.",[14,1869,466],{},[36,1871,1873],{"className":38,"code":1872,"language":40,"meta":41,"style":41},"age = 20\nhas_id = True\n\nif age >= 18:\n    if has_id:\n        print(\"Entry allowed\")\n    else:\n        print(\"ID required\")\nelse:\n    print(\"Too young\")\n",[18,1874,1875,1884,1893,1897,1909,1919,1935,1942,1957,1963],{"__ignoreMap":41},[45,1876,1877,1879,1881],{"class":47,"line":48},[45,1878,52],{"class":51},[45,1880,56],{"class":55},[45,1882,1883],{"class":59}," 20\n",[45,1885,1886,1889,1891],{"class":47,"line":63},[45,1887,1888],{"class":51},"has_id ",[45,1890,56],{"class":55},[45,1892,482],{"class":481},[45,1894,1895],{"class":47,"line":70},[45,1896,67],{"emptyLinePlaceholder":66},[45,1898,1899,1901,1903,1905,1907],{"class":47,"line":89},[45,1900,20],{"class":73},[45,1902,76],{"class":51},[45,1904,600],{"class":55},[45,1906,121],{"class":59},[45,1908,86],{"class":85},[45,1910,1911,1914,1917],{"class":47,"line":112},[45,1912,1913],{"class":73},"    if",[45,1915,1916],{"class":51}," has_id",[45,1918,86],{"class":85},[45,1920,1921,1924,1926,1928,1931,1933],{"class":47,"line":126},[45,1922,1923],{"class":92},"        print",[45,1925,96],{"class":85},[45,1927,100],{"class":99},[45,1929,1930],{"class":103},"Entry allowed",[45,1932,100],{"class":99},[45,1934,109],{"class":85},[45,1936,1937,1940],{"class":47,"line":142},[45,1938,1939],{"class":73},"    else",[45,1941,86],{"class":85},[45,1943,1944,1946,1948,1950,1953,1955],{"class":47,"line":149},[45,1945,1923],{"class":92},[45,1947,96],{"class":85},[45,1949,100],{"class":99},[45,1951,1952],{"class":103},"ID required",[45,1954,100],{"class":99},[45,1956,109],{"class":85},[45,1958,1959,1961],{"class":47,"line":679},[45,1960,28],{"class":73},[45,1962,86],{"class":85},[45,1964,1965,1967,1969,1971,1974,1976],{"class":47,"line":686},[45,1966,93],{"class":92},[45,1968,96],{"class":85},[45,1970,100],{"class":99},[45,1972,1973],{"class":103},"Too young",[45,1975,100],{"class":99},[45,1977,109],{"class":85},[14,1979,177],{},[36,1981,1983],{"className":38,"code":1982,"language":40,"meta":41,"style":41},"Entry allowed\n",[18,1984,1985],{"__ignoreMap":41},[45,1986,1987],{"class":47,"line":48},[45,1988,1982],{"class":51},[14,1990,1991],{},"This works, but try to keep nesting shallow so your code stays readable.",[14,1993,1994],{},"If your logic starts to feel confusing:",[195,1996,1997,2000,2003,2006],{},[198,1998,1999],{},"simplify the conditions",[198,2001,2002],{},"check the order carefully",[198,2004,2005],{},"use clear variable names",[198,2007,2008],{},"print values while testing",[31,2010,2012],{"id":2011},"common-causes-of-confusion","Common causes of confusion",[14,2014,2015,2016,218,2018,218,2020,2022],{},"These are common reasons ",[18,2017,20],{},[18,2019,24],{},[18,2021,28],{}," code does not behave as expected:",[195,2024,2025,2038,2041,2047,2050],{},[198,2026,2027,2028,218,2030,218,2032,2034,2035,2037],{},"Confusing one ",[18,2029,20],{},[18,2031,24],{},[18,2033,28],{}," chain with separate ",[18,2036,20],{}," statements",[198,2039,2040],{},"Placing a broad condition before a specific one",[198,2042,2043,2044,2046],{},"Forgetting that ",[18,2045,28],{}," has no condition",[198,2048,2049],{},"Mixing tabs and spaces in indentation",[198,2051,2052],{},"Using a comparison that does not match the intended logic",[14,2054,2055],{},"These quick checks can help while debugging:",[36,2057,2059],{"className":38,"code":2058,"language":40,"meta":41,"style":41},"print(condition)\nprint(age)\nprint(\"checking first condition\")\nhelp(bool)\n",[18,2060,2061,2074,2085,2100],{"__ignoreMap":41},[45,2062,2063,2066,2068,2072],{"class":47,"line":48},[45,2064,2065],{"class":92},"print",[45,2067,96],{"class":85},[45,2069,2071],{"class":2070},"slqww","condition",[45,2073,109],{"class":85},[45,2075,2076,2078,2080,2083],{"class":47,"line":63},[45,2077,2065],{"class":92},[45,2079,96],{"class":85},[45,2081,2082],{"class":2070},"age",[45,2084,109],{"class":85},[45,2086,2087,2089,2091,2093,2096,2098],{"class":47,"line":70},[45,2088,2065],{"class":92},[45,2090,96],{"class":85},[45,2092,100],{"class":99},[45,2094,2095],{"class":103},"checking first condition",[45,2097,100],{"class":99},[45,2099,109],{"class":85},[45,2101,2102,2105,2107,2111],{"class":47,"line":89},[45,2103,2104],{"class":92},"help",[45,2106,96],{"class":85},[45,2108,2110],{"class":2109},"sZMiF","bool",[45,2112,109],{"class":85},[14,2114,2115],{},"For example:",[36,2117,2119],{"className":38,"code":2118,"language":40,"meta":41,"style":41},"age = 16\n\nprint(age)\nprint(age \u003C 13)\nprint(age \u003C 18)\n\nif age \u003C 13:\n    print(\"child\")\nelif age \u003C 18:\n    print(\"teen\")\nelse:\n    print(\"adult\")\n",[18,2120,2121,2129,2133,2143,2157,2171,2175,2187,2201,2213,2227,2234],{"__ignoreMap":41},[45,2122,2123,2125,2127],{"class":47,"line":48},[45,2124,52],{"class":51},[45,2126,56],{"class":55},[45,2128,986],{"class":59},[45,2130,2131],{"class":47,"line":63},[45,2132,67],{"emptyLinePlaceholder":66},[45,2134,2135,2137,2139,2141],{"class":47,"line":70},[45,2136,2065],{"class":92},[45,2138,96],{"class":85},[45,2140,2082],{"class":2070},[45,2142,109],{"class":85},[45,2144,2145,2147,2149,2151,2153,2155],{"class":47,"line":89},[45,2146,2065],{"class":92},[45,2148,96],{"class":85},[45,2150,52],{"class":2070},[45,2152,79],{"class":55},[45,2154,82],{"class":59},[45,2156,109],{"class":85},[45,2158,2159,2161,2163,2165,2167,2169],{"class":47,"line":112},[45,2160,2065],{"class":92},[45,2162,96],{"class":85},[45,2164,52],{"class":2070},[45,2166,79],{"class":55},[45,2168,121],{"class":59},[45,2170,109],{"class":85},[45,2172,2173],{"class":47,"line":126},[45,2174,67],{"emptyLinePlaceholder":66},[45,2176,2177,2179,2181,2183,2185],{"class":47,"line":142},[45,2178,20],{"class":73},[45,2180,76],{"class":51},[45,2182,79],{"class":55},[45,2184,82],{"class":59},[45,2186,86],{"class":85},[45,2188,2189,2191,2193,2195,2197,2199],{"class":47,"line":149},[45,2190,93],{"class":92},[45,2192,96],{"class":85},[45,2194,100],{"class":99},[45,2196,104],{"class":103},[45,2198,100],{"class":99},[45,2200,109],{"class":85},[45,2202,2203,2205,2207,2209,2211],{"class":47,"line":679},[45,2204,24],{"class":73},[45,2206,76],{"class":51},[45,2208,79],{"class":55},[45,2210,121],{"class":59},[45,2212,86],{"class":85},[45,2214,2215,2217,2219,2221,2223,2225],{"class":47,"line":686},[45,2216,93],{"class":92},[45,2218,96],{"class":85},[45,2220,100],{"class":99},[45,2222,135],{"class":103},[45,2224,100],{"class":99},[45,2226,109],{"class":85},[45,2228,2230,2232],{"class":47,"line":2229},11,[45,2231,28],{"class":73},[45,2233,86],{"class":85},[45,2235,2237,2239,2241,2243,2245,2247],{"class":47,"line":2236},12,[45,2238,93],{"class":92},[45,2240,96],{"class":85},[45,2242,100],{"class":99},[45,2244,158],{"class":103},[45,2246,100],{"class":99},[45,2248,109],{"class":85},[14,2250,177],{},[36,2252,2254],{"className":38,"code":2253,"language":40,"meta":41,"style":41},"16\nFalse\nTrue\nteen\n",[18,2255,2256,2261,2266,2271],{"__ignoreMap":41},[45,2257,2258],{"class":47,"line":48},[45,2259,2260],{"class":59},"16\n",[45,2262,2263],{"class":47,"line":63},[45,2264,2265],{"class":481},"False\n",[45,2267,2268],{"class":47,"line":70},[45,2269,2270],{"class":481},"True\n",[45,2272,2273],{"class":47,"line":89},[45,2274,1093],{"class":51},[14,2276,2277],{},"Printing the values and condition results helps you see why a branch did or did not run.",[31,2279,2281],{"id":2280},"faq","FAQ",[712,2283,2285],{"id":2284},"what-is-the-difference-between-if-and-elif","What is the difference between if and elif?",[14,2287,2288,2290,2291,2293,2294,240],{},[18,2289,20],{}," starts the condition chain. ",[18,2292,24],{}," adds another condition that is checked only if earlier ones were ",[18,2295,233],{},[712,2297,2299],{"id":2298},"do-i-have-to-use-else","Do I have to use else?",[14,2301,2302,2303,2305],{},"No. ",[18,2304,28],{}," is optional. Use it when you want a default action.",[712,2307,2309],{"id":2308},"can-i-have-more-than-one-elif","Can I have more than one elif?",[14,2311,2312,2313,2315],{},"Yes. You can use as many ",[18,2314,24],{}," branches as needed.",[712,2317,2319],{"id":2318},"can-more-than-one-branch-run","Can more than one branch run?",[14,2321,2322,2323,218,2325,218,2327,2329,2330,240],{},"No. In one ",[18,2324,20],{},[18,2326,24],{},[18,2328,28],{}," chain, Python runs only the first branch whose condition is ",[18,2331,229],{},[712,2333,2335],{"id":2334},"what-happens-if-all-conditions-are-false-and-there-is-no-else","What happens if all conditions are False and there is no else?",[14,2337,2338],{},"Nothing in that chain runs, and Python continues to the next line after it.",[31,2340,2342],{"id":2341},"see-also","See also",[195,2344,2345,2349,2353,2357],{},[198,2346,2347],{},[236,2348,549],{"href":548},[198,2350,2351],{},[236,2352,239],{"href":238},[198,2354,2355],{},[236,2356,554],{"href":553},[198,2358,2359],{},[236,2360,2362],{"href":2361},"\u002Fglossary\u002Fwhat-is-an-if-statement-in-python\u002F","What is an if statement in Python?",[14,2364,2365],{},"Next, learn how conditions work with loops or practice by writing a small input-based program that chooses between different outputs.",[2367,2368,2369],"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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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}",{"title":41,"searchDepth":63,"depth":63,"links":2371},[2372,2373,2374,2375,2378,2379,2380,2388,2389,2390,2397],{"id":33,"depth":63,"text":34},{"id":189,"depth":63,"text":190},{"id":379,"depth":63,"text":380},{"id":557,"depth":63,"text":558,"children":2376},[2377],{"id":714,"depth":70,"text":715},{"id":931,"depth":63,"text":932},{"id":1115,"depth":63,"text":1116},{"id":1336,"depth":63,"text":1337,"children":2381},[2382,2384,2385,2386,2387],{"id":1349,"depth":70,"text":2383},"Using = instead of ==",{"id":1452,"depth":70,"text":1453},{"id":1544,"depth":70,"text":1545},{"id":1617,"depth":70,"text":1618},{"id":1834,"depth":70,"text":1835},{"id":1854,"depth":63,"text":1855},{"id":2011,"depth":63,"text":2012},{"id":2280,"depth":63,"text":2281,"children":2391},[2392,2393,2394,2395,2396],{"id":2284,"depth":70,"text":2285},{"id":2298,"depth":70,"text":2299},{"id":2308,"depth":70,"text":2309},{"id":2318,"depth":70,"text":2319},{"id":2334,"depth":70,"text":2335},{"id":2341,"depth":63,"text":2342},"Master python if else and elif explained in our comprehensive Python beginner guide.","md",{},"\u002Flearn\u002Fpython-if-else-and-elif-explained",{"title":5,"description":2398},"learn\u002Fpython-if-else-and-elif-explained","KvkhnZv7lvetWffn43ytDqbnRXVbANp1EN0ZNZpzPQA",[2406,4552,6390,8537,10967,12901,14858,16520,17499,19165,22085,24914,27122,29387,31961,33475,35646,37402,39237,40411,42551,45797,47559,49347,51475,53319,55343,57293,58541,59918,62340,63239,64930,66115,68284,70565,72227,74669,76832,78608,80306,82261,84424,87011,88520,91116,91858,93853,95047,96527],{"id":2407,"title":2408,"body":2409,"description":4546,"extension":2399,"meta":4547,"navigation":66,"path":4548,"seo":4549,"stem":4550,"__hash__":4551},"content\u002Flearn\u002Fbasic-methods-in-python-classes-explained.md","Basic Methods in Python Classes Explained",{"type":7,"value":2410,"toc":4509},[2411,2414,2417,2429,2431,2508,2512,2521,2527,2531,2547,2551,2554,2561,2564,2592,2598,2615,2618,2687,2691,2709,2712,2719,2725,2728,2745,2748,2773,2776,2778,2840,2852,2859,2878,2881,2885,2888,2914,2916,2985,2989,2998,3002,3028,3035,3039,3048,3051,3053,3213,3217,3250,3257,3271,3275,3278,3423,3427,3442,3449,3453,3456,3463,3465,3623,3627,3636,3639,3642,3780,3784,3792,3794,3797,3804,3807,3843,3845,3884,3890,3894,3896,3949,3954,3956,4019,4029,4038,4041,4055,4061,4064,4079,4087,4089,4156,4158,4229,4234,4238,4241,4244,4247,4285,4289,4292,4320,4324,4327,4390,4394,4420,4429,4431,4435,4438,4444,4450,4454,4457,4463,4469,4471,4506],[10,2412,2408],{"id":2413},"basic-methods-in-python-classes-explained",[14,2415,2416],{},"Methods are one of the most important parts of Python classes.",[14,2418,2419,2420,2424,2425,2428],{},"A ",[2421,2422,2423],"strong",{},"method"," is a function written inside a class. You use methods to describe actions that an object can perform. This page explains what methods are, why ",[18,2426,2427],{},"self"," is used, and how to create and call simple instance methods.",[31,2430,34],{"id":33},[36,2432,2434],{"className":38,"code":2433,"language":40,"meta":41,"style":41},"class Dog:\n    def bark(self):\n        print(\"Woof!\")\n\nmy_dog = Dog()\nmy_dog.bark()\n",[18,2435,2436,2448,2465,2480,2484,2496],{"__ignoreMap":41},[45,2437,2438,2442,2446],{"class":47,"line":48},[45,2439,2441],{"class":2440},"sbsja","class",[45,2443,2445],{"class":2444},"sbgvK"," Dog",[45,2447,86],{"class":85},[45,2449,2450,2453,2457,2459,2462],{"class":47,"line":63},[45,2451,2452],{"class":2440},"    def",[45,2454,2456],{"class":2455},"sGLFI"," bark",[45,2458,96],{"class":85},[45,2460,2427],{"class":2461},"smCYv",[45,2463,2464],{"class":85},"):\n",[45,2466,2467,2469,2471,2473,2476,2478],{"class":47,"line":70},[45,2468,1923],{"class":92},[45,2470,96],{"class":85},[45,2472,100],{"class":99},[45,2474,2475],{"class":103},"Woof!",[45,2477,100],{"class":99},[45,2479,109],{"class":85},[45,2481,2482],{"class":47,"line":89},[45,2483,67],{"emptyLinePlaceholder":66},[45,2485,2486,2489,2491,2493],{"class":47,"line":112},[45,2487,2488],{"class":51},"my_dog ",[45,2490,56],{"class":55},[45,2492,2445],{"class":2070},[45,2494,2495],{"class":85},"()\n",[45,2497,2498,2501,2503,2506],{"class":47,"line":126},[45,2499,2500],{"class":51},"my_dog",[45,2502,240],{"class":85},[45,2504,2505],{"class":2070},"bark",[45,2507,2495],{"class":85},[14,2509,2510],{},[2421,2511,177],{},[36,2513,2515],{"className":38,"code":2514,"language":40,"meta":41,"style":41},"Woof!\n",[18,2516,2517],{"__ignoreMap":41},[45,2518,2519],{"class":47,"line":48},[45,2520,2514],{"class":51},[14,2522,2523,2524,2526],{},"Use a method when you want an object to perform an action. The ",[18,2525,2427],{}," parameter refers to the current object.",[31,2528,2530],{"id":2529},"what-this-page-covers","What this page covers",[195,2532,2533,2536,2539,2544],{},[198,2534,2535],{},"Define a method in simple words",[198,2537,2538],{},"Show how methods belong to a class",[198,2540,2541,2542],{},"Explain why methods use ",[18,2543,2427],{},[198,2545,2546],{},"Show how to call a method on an object",[31,2548,2550],{"id":2549},"what-is-a-method-in-python","What is a method in Python?",[14,2552,2553],{},"A method is a function inside a class.",[14,2555,2556,2557,240],{},"If you are still getting used to classes, see ",[236,2558,2560],{"href":2559},"\u002Flearn\u002Fpython-classes-and-objects-explained","Python classes and objects explained",[14,2562,2563],{},"Methods are used to describe behavior. For example:",[195,2565,2566,2576,2584],{},[198,2567,2419,2568,2571,2572,2575],{},[18,2569,2570],{},"Dog"," object might have a ",[18,2573,2574],{},"bark()"," method",[198,2577,2419,2578,2571,2581,2575],{},[18,2579,2580],{},"Person",[18,2582,2583],{},"greet()",[198,2585,2419,2586,2571,2589,2575],{},[18,2587,2588],{},"Car",[18,2590,2591],{},"start()",[14,2593,2594,2595,457],{},"You call a method with ",[2421,2596,2597],{},"dot notation",[36,2599,2601],{"className":38,"code":2600,"language":40,"meta":41,"style":41},"object_name.method_name()\n",[18,2602,2603],{"__ignoreMap":41},[45,2604,2605,2608,2610,2613],{"class":47,"line":48},[45,2606,2607],{"class":51},"object_name",[45,2609,240],{"class":85},[45,2611,2612],{"class":2070},"method_name",[45,2614,2495],{"class":85},[14,2616,2617],{},"Here is a simple example:",[36,2619,2621],{"className":38,"code":2620,"language":40,"meta":41,"style":41},"class Lamp:\n    def turn_on(self):\n        print(\"The lamp is now on.\")\n\nlamp = Lamp()\nlamp.turn_on()\n",[18,2622,2623,2632,2645,2660,2664,2675],{"__ignoreMap":41},[45,2624,2625,2627,2630],{"class":47,"line":48},[45,2626,2441],{"class":2440},[45,2628,2629],{"class":2444}," Lamp",[45,2631,86],{"class":85},[45,2633,2634,2636,2639,2641,2643],{"class":47,"line":63},[45,2635,2452],{"class":2440},[45,2637,2638],{"class":2455}," turn_on",[45,2640,96],{"class":85},[45,2642,2427],{"class":2461},[45,2644,2464],{"class":85},[45,2646,2647,2649,2651,2653,2656,2658],{"class":47,"line":70},[45,2648,1923],{"class":92},[45,2650,96],{"class":85},[45,2652,100],{"class":99},[45,2654,2655],{"class":103},"The lamp is now on.",[45,2657,100],{"class":99},[45,2659,109],{"class":85},[45,2661,2662],{"class":47,"line":89},[45,2663,67],{"emptyLinePlaceholder":66},[45,2665,2666,2669,2671,2673],{"class":47,"line":112},[45,2667,2668],{"class":51},"lamp ",[45,2670,56],{"class":55},[45,2672,2629],{"class":2070},[45,2674,2495],{"class":85},[45,2676,2677,2680,2682,2685],{"class":47,"line":126},[45,2678,2679],{"class":51},"lamp",[45,2681,240],{"class":85},[45,2683,2684],{"class":2070},"turn_on",[45,2686,2495],{"class":85},[14,2688,2689],{},[2421,2690,177],{},[36,2692,2694],{"className":38,"code":2693,"language":40,"meta":41,"style":41},"The lamp is now on.\n",[18,2695,2696],{"__ignoreMap":41},[45,2697,2698,2701,2703,2706],{"class":47,"line":48},[45,2699,2700],{"class":51},"The lamp ",[45,2702,1285],{"class":55},[45,2704,2705],{"class":51}," now on",[45,2707,2708],{"class":85},".\n",[14,2710,2711],{},"Methods help keep data and behavior together. That is one of the main ideas behind classes.",[31,2713,2715,2716,2718],{"id":2714},"why-self-is-used","Why ",[18,2717,2427],{}," is used",[14,2720,2721,2722,2724],{},"In an instance method, ",[18,2723,2427],{}," refers to the current object.",[14,2726,2727],{},"That means:",[195,2729,2730,2735,2740],{},[198,2731,2732,2734],{},[18,2733,2427],{}," lets the method work with that object",[198,2736,2737,2739],{},[18,2738,2427],{}," can read the object's attributes",[198,2741,2742,2744],{},[18,2743,2427],{}," can change the object's attributes",[14,2746,2747],{},"A basic instance method looks like this:",[36,2749,2751],{"className":38,"code":2750,"language":40,"meta":41,"style":41},"def method_name(self):\n    ...\n",[18,2752,2753,2767],{"__ignoreMap":41},[45,2754,2755,2758,2761,2763,2765],{"class":47,"line":48},[45,2756,2757],{"class":2440},"def",[45,2759,2760],{"class":2455}," method_name",[45,2762,96],{"class":85},[45,2764,2427],{"class":2461},[45,2766,2464],{"class":85},[45,2768,2769],{"class":47,"line":63},[45,2770,2772],{"class":2771},"s_hVV","    ...\n",[14,2774,2775],{},"Python automatically passes the object as the first argument when you call the method on an object.",[14,2777,466],{},[36,2779,2780],{"className":38,"code":2433,"language":40,"meta":41,"style":41},[18,2781,2782,2790,2802,2816,2820,2830],{"__ignoreMap":41},[45,2783,2784,2786,2788],{"class":47,"line":48},[45,2785,2441],{"class":2440},[45,2787,2445],{"class":2444},[45,2789,86],{"class":85},[45,2791,2792,2794,2796,2798,2800],{"class":47,"line":63},[45,2793,2452],{"class":2440},[45,2795,2456],{"class":2455},[45,2797,96],{"class":85},[45,2799,2427],{"class":2461},[45,2801,2464],{"class":85},[45,2803,2804,2806,2808,2810,2812,2814],{"class":47,"line":70},[45,2805,1923],{"class":92},[45,2807,96],{"class":85},[45,2809,100],{"class":99},[45,2811,2475],{"class":103},[45,2813,100],{"class":99},[45,2815,109],{"class":85},[45,2817,2818],{"class":47,"line":89},[45,2819,67],{"emptyLinePlaceholder":66},[45,2821,2822,2824,2826,2828],{"class":47,"line":112},[45,2823,2488],{"class":51},[45,2825,56],{"class":55},[45,2827,2445],{"class":2070},[45,2829,2495],{"class":85},[45,2831,2832,2834,2836,2838],{"class":47,"line":126},[45,2833,2500],{"class":51},[45,2835,240],{"class":85},[45,2837,2505],{"class":2070},[45,2839,2495],{"class":85},[14,2841,2842,2843,2846,2847,2849,2850,240],{},"When Python runs ",[18,2844,2845],{},"my_dog.bark()",", it automatically uses ",[18,2848,2500],{}," as ",[18,2851,2427],{},[14,2853,2854,2855,2858],{},"You do ",[2421,2856,2857],{},"not"," write this:",[36,2860,2862],{"className":38,"code":2861,"language":40,"meta":41,"style":41},"my_dog.bark(my_dog)\n",[18,2863,2864],{"__ignoreMap":41},[45,2865,2866,2868,2870,2872,2874,2876],{"class":47,"line":48},[45,2867,2500],{"class":51},[45,2869,240],{"class":85},[45,2871,2505],{"class":2070},[45,2873,96],{"class":85},[45,2875,2500],{"class":2070},[45,2877,109],{"class":85},[14,2879,2880],{},"Python handles that for you.",[31,2882,2884],{"id":2883},"how-to-write-a-basic-method","How to write a basic method",[14,2886,2887],{},"To create a basic method:",[346,2889,2890,2896,2902,2905,2908],{},[198,2891,2892,2893],{},"Create a class with ",[18,2894,2895],{},"class ClassName:",[198,2897,2898,2899],{},"Define a method with ",[18,2900,2901],{},"def method_name(self):",[198,2903,2904],{},"Indent the method inside the class",[198,2906,2907],{},"Create an object",[198,2909,2910,2911],{},"Call the method with ",[18,2912,2913],{},"object.method_name()",[14,2915,466],{},[36,2917,2919],{"className":38,"code":2918,"language":40,"meta":41,"style":41},"class Person:\n    def greet(self):\n        print(\"Hello!\")\n\nperson = Person()\nperson.greet()\n",[18,2920,2921,2930,2943,2958,2962,2973],{"__ignoreMap":41},[45,2922,2923,2925,2928],{"class":47,"line":48},[45,2924,2441],{"class":2440},[45,2926,2927],{"class":2444}," Person",[45,2929,86],{"class":85},[45,2931,2932,2934,2937,2939,2941],{"class":47,"line":63},[45,2933,2452],{"class":2440},[45,2935,2936],{"class":2455}," greet",[45,2938,96],{"class":85},[45,2940,2427],{"class":2461},[45,2942,2464],{"class":85},[45,2944,2945,2947,2949,2951,2954,2956],{"class":47,"line":70},[45,2946,1923],{"class":92},[45,2948,96],{"class":85},[45,2950,100],{"class":99},[45,2952,2953],{"class":103},"Hello!",[45,2955,100],{"class":99},[45,2957,109],{"class":85},[45,2959,2960],{"class":47,"line":89},[45,2961,67],{"emptyLinePlaceholder":66},[45,2963,2964,2967,2969,2971],{"class":47,"line":112},[45,2965,2966],{"class":51},"person ",[45,2968,56],{"class":55},[45,2970,2927],{"class":2070},[45,2972,2495],{"class":85},[45,2974,2975,2978,2980,2983],{"class":47,"line":126},[45,2976,2977],{"class":51},"person",[45,2979,240],{"class":85},[45,2981,2982],{"class":2070},"greet",[45,2984,2495],{"class":85},[14,2986,2987],{},[2421,2988,177],{},[36,2990,2992],{"className":38,"code":2991,"language":40,"meta":41,"style":41},"Hello!\n",[18,2993,2994],{"__ignoreMap":41},[45,2995,2996],{"class":47,"line":48},[45,2997,2991],{"class":51},[712,2999,3001],{"id":3000},"key-parts-of-this-example","Key parts of this example",[195,3003,3004,3010,3016,3022],{},[198,3005,3006,3009],{},[18,3007,3008],{},"class Person:"," creates the class",[198,3011,3012,3015],{},[18,3013,3014],{},"def greet(self):"," creates a method",[198,3017,3018,3021],{},[18,3019,3020],{},"person = Person()"," creates an object",[198,3023,3024,3027],{},[18,3025,3026],{},"person.greet()"," calls the method",[14,3029,3030,3031,240],{},"If you want more practice with creating behaviors in classes, see ",[236,3032,3034],{"href":3033},"\u002Fhow-to\u002Fhow-to-add-methods-to-a-class-in-python","how to add methods to a class in Python",[31,3036,3038],{"id":3037},"methods-can-use-object-data","Methods can use object data",[14,3040,3041,3042,230,3045,240],{},"Methods often work with attributes such as ",[18,3043,3044],{},"self.name",[18,3046,3047],{},"self.age",[14,3049,3050],{},"This is what makes objects useful. Each object can store its own data, and methods can use that data.",[14,3052,466],{},[36,3054,3056],{"className":38,"code":3055,"language":40,"meta":41,"style":41},"class Person:\n    def __init__(self, name):\n        self.name = name\n\n    def greet(self):\n        print(\"Hello, my name is\", self.name)\n\nperson1 = Person(\"Ava\")\nperson2 = Person(\"Liam\")\n\nperson1.greet()\nperson2.greet()\n",[18,3057,3058,3066,3086,3103,3107,3119,3143,3147,3167,3187,3191,3202],{"__ignoreMap":41},[45,3059,3060,3062,3064],{"class":47,"line":48},[45,3061,2441],{"class":2440},[45,3063,2927],{"class":2444},[45,3065,86],{"class":85},[45,3067,3068,3070,3073,3075,3077,3080,3084],{"class":47,"line":63},[45,3069,2452],{"class":2440},[45,3071,3072],{"class":92}," __init__",[45,3074,96],{"class":85},[45,3076,2427],{"class":2461},[45,3078,3079],{"class":85},",",[45,3081,3083],{"class":3082},"sFwrP"," name",[45,3085,2464],{"class":85},[45,3087,3088,3091,3093,3097,3100],{"class":47,"line":70},[45,3089,3090],{"class":2771},"        self",[45,3092,240],{"class":85},[45,3094,3096],{"class":3095},"skxfh","name",[45,3098,3099],{"class":55}," =",[45,3101,3102],{"class":51}," name\n",[45,3104,3105],{"class":47,"line":89},[45,3106,67],{"emptyLinePlaceholder":66},[45,3108,3109,3111,3113,3115,3117],{"class":47,"line":112},[45,3110,2452],{"class":2440},[45,3112,2936],{"class":2455},[45,3114,96],{"class":85},[45,3116,2427],{"class":2461},[45,3118,2464],{"class":85},[45,3120,3121,3123,3125,3127,3130,3132,3134,3137,3139,3141],{"class":47,"line":126},[45,3122,1923],{"class":92},[45,3124,96],{"class":85},[45,3126,100],{"class":99},[45,3128,3129],{"class":103},"Hello, my name is",[45,3131,100],{"class":99},[45,3133,3079],{"class":85},[45,3135,3136],{"class":2771}," self",[45,3138,240],{"class":85},[45,3140,3096],{"class":3095},[45,3142,109],{"class":85},[45,3144,3145],{"class":47,"line":142},[45,3146,67],{"emptyLinePlaceholder":66},[45,3148,3149,3152,3154,3156,3158,3160,3163,3165],{"class":47,"line":149},[45,3150,3151],{"class":51},"person1 ",[45,3153,56],{"class":55},[45,3155,2927],{"class":2070},[45,3157,96],{"class":85},[45,3159,100],{"class":99},[45,3161,3162],{"class":103},"Ava",[45,3164,100],{"class":99},[45,3166,109],{"class":85},[45,3168,3169,3172,3174,3176,3178,3180,3183,3185],{"class":47,"line":679},[45,3170,3171],{"class":51},"person2 ",[45,3173,56],{"class":55},[45,3175,2927],{"class":2070},[45,3177,96],{"class":85},[45,3179,100],{"class":99},[45,3181,3182],{"class":103},"Liam",[45,3184,100],{"class":99},[45,3186,109],{"class":85},[45,3188,3189],{"class":47,"line":686},[45,3190,67],{"emptyLinePlaceholder":66},[45,3192,3193,3196,3198,3200],{"class":47,"line":2229},[45,3194,3195],{"class":51},"person1",[45,3197,240],{"class":85},[45,3199,2982],{"class":2070},[45,3201,2495],{"class":85},[45,3203,3204,3207,3209,3211],{"class":47,"line":2236},[45,3205,3206],{"class":51},"person2",[45,3208,240],{"class":85},[45,3210,2982],{"class":2070},[45,3212,2495],{"class":85},[14,3214,3215],{},[2421,3216,177],{},[36,3218,3220],{"className":38,"code":3219,"language":40,"meta":41,"style":41},"Hello, my name is Ava\nHello, my name is Liam\n",[18,3221,3222,3237],{"__ignoreMap":41},[45,3223,3224,3227,3229,3232,3234],{"class":47,"line":48},[45,3225,3226],{"class":51},"Hello",[45,3228,3079],{"class":85},[45,3230,3231],{"class":51}," my name ",[45,3233,1285],{"class":55},[45,3235,3236],{"class":51}," Ava\n",[45,3238,3239,3241,3243,3245,3247],{"class":47,"line":63},[45,3240,3226],{"class":51},[45,3242,3079],{"class":85},[45,3244,3231],{"class":51},[45,3246,1285],{"class":55},[45,3248,3249],{"class":51}," Liam\n",[14,3251,1732,3252,3254,3255,240],{},[18,3253,2583],{}," method works differently for each object because each object has its own ",[18,3256,3096],{},[14,3258,3259,3260,3263,3264,240],{},"If ",[18,3261,3262],{},"__init__"," is new to you, read ",[236,3265,3267,3268,3270],{"href":3266},"\u002Flearn\u002Fthe-__init__-method-in-python-explained","the ",[18,3269,3262],{}," method in Python explained",[712,3272,3274],{"id":3273},"example-changing-object-data","Example: changing object data",[14,3276,3277],{},"A method can also update an attribute:",[36,3279,3281],{"className":38,"code":3280,"language":40,"meta":41,"style":41},"class Person:\n    def __init__(self, age):\n        self.age = age\n\n    def update_age(self, new_age):\n        self.age = new_age\n\nperson = Person(20)\nprint(person.age)\n\nperson.update_age(21)\nprint(person.age)\n",[18,3282,3283,3291,3308,3321,3325,3343,3356,3360,3375,3389,3393,3409],{"__ignoreMap":41},[45,3284,3285,3287,3289],{"class":47,"line":48},[45,3286,2441],{"class":2440},[45,3288,2927],{"class":2444},[45,3290,86],{"class":85},[45,3292,3293,3295,3297,3299,3301,3303,3306],{"class":47,"line":63},[45,3294,2452],{"class":2440},[45,3296,3072],{"class":92},[45,3298,96],{"class":85},[45,3300,2427],{"class":2461},[45,3302,3079],{"class":85},[45,3304,3305],{"class":3082}," age",[45,3307,2464],{"class":85},[45,3309,3310,3312,3314,3316,3318],{"class":47,"line":70},[45,3311,3090],{"class":2771},[45,3313,240],{"class":85},[45,3315,2082],{"class":3095},[45,3317,3099],{"class":55},[45,3319,3320],{"class":51}," age\n",[45,3322,3323],{"class":47,"line":89},[45,3324,67],{"emptyLinePlaceholder":66},[45,3326,3327,3329,3332,3334,3336,3338,3341],{"class":47,"line":112},[45,3328,2452],{"class":2440},[45,3330,3331],{"class":2455}," update_age",[45,3333,96],{"class":85},[45,3335,2427],{"class":2461},[45,3337,3079],{"class":85},[45,3339,3340],{"class":3082}," new_age",[45,3342,2464],{"class":85},[45,3344,3345,3347,3349,3351,3353],{"class":47,"line":126},[45,3346,3090],{"class":2771},[45,3348,240],{"class":85},[45,3350,2082],{"class":3095},[45,3352,3099],{"class":55},[45,3354,3355],{"class":51}," new_age\n",[45,3357,3358],{"class":47,"line":142},[45,3359,67],{"emptyLinePlaceholder":66},[45,3361,3362,3364,3366,3368,3370,3373],{"class":47,"line":149},[45,3363,2966],{"class":51},[45,3365,56],{"class":55},[45,3367,2927],{"class":2070},[45,3369,96],{"class":85},[45,3371,3372],{"class":59},"20",[45,3374,109],{"class":85},[45,3376,3377,3379,3381,3383,3385,3387],{"class":47,"line":679},[45,3378,2065],{"class":92},[45,3380,96],{"class":85},[45,3382,2977],{"class":2070},[45,3384,240],{"class":85},[45,3386,2082],{"class":3095},[45,3388,109],{"class":85},[45,3390,3391],{"class":47,"line":686},[45,3392,67],{"emptyLinePlaceholder":66},[45,3394,3395,3397,3399,3402,3404,3407],{"class":47,"line":2229},[45,3396,2977],{"class":51},[45,3398,240],{"class":85},[45,3400,3401],{"class":2070},"update_age",[45,3403,96],{"class":85},[45,3405,3406],{"class":59},"21",[45,3408,109],{"class":85},[45,3410,3411,3413,3415,3417,3419,3421],{"class":47,"line":2236},[45,3412,2065],{"class":92},[45,3414,96],{"class":85},[45,3416,2977],{"class":2070},[45,3418,240],{"class":85},[45,3420,2082],{"class":3095},[45,3422,109],{"class":85},[14,3424,3425],{},[2421,3426,177],{},[36,3428,3430],{"className":38,"code":3429,"language":40,"meta":41,"style":41},"20\n21\n",[18,3431,3432,3437],{"__ignoreMap":41},[45,3433,3434],{"class":47,"line":48},[45,3435,3436],{"class":59},"20\n",[45,3438,3439],{"class":47,"line":63},[45,3440,3441],{"class":59},"21\n",[14,3443,3444,3445,3448],{},"Here, ",[18,3446,3447],{},"update_age()"," changes the data stored in the object.",[31,3450,3452],{"id":3451},"methods-can-return-values","Methods can return values",[14,3454,3455],{},"A method does not have to only print something.",[14,3457,3458,3459,3462],{},"Sometimes it is better to use ",[18,3460,3461],{},"return"," so the method gives a value back. This is more flexible because you can store the result in a variable or use it in another expression.",[14,3464,466],{},[36,3466,3468],{"className":38,"code":3467,"language":40,"meta":41,"style":41},"class Rectangle:\n    def __init__(self, width, height):\n        self.width = width\n        self.height = height\n\n    def area(self):\n        return self.width * self.height\n\nshape = Rectangle(4, 3)\nresult = shape.area()\n\nprint(result)\n",[18,3469,3470,3479,3501,3515,3529,3533,3546,3567,3571,3591,3608,3612],{"__ignoreMap":41},[45,3471,3472,3474,3477],{"class":47,"line":48},[45,3473,2441],{"class":2440},[45,3475,3476],{"class":2444}," Rectangle",[45,3478,86],{"class":85},[45,3480,3481,3483,3485,3487,3489,3491,3494,3496,3499],{"class":47,"line":63},[45,3482,2452],{"class":2440},[45,3484,3072],{"class":92},[45,3486,96],{"class":85},[45,3488,2427],{"class":2461},[45,3490,3079],{"class":85},[45,3492,3493],{"class":3082}," width",[45,3495,3079],{"class":85},[45,3497,3498],{"class":3082}," height",[45,3500,2464],{"class":85},[45,3502,3503,3505,3507,3510,3512],{"class":47,"line":70},[45,3504,3090],{"class":2771},[45,3506,240],{"class":85},[45,3508,3509],{"class":3095},"width",[45,3511,3099],{"class":55},[45,3513,3514],{"class":51}," width\n",[45,3516,3517,3519,3521,3524,3526],{"class":47,"line":89},[45,3518,3090],{"class":2771},[45,3520,240],{"class":85},[45,3522,3523],{"class":3095},"height",[45,3525,3099],{"class":55},[45,3527,3528],{"class":51}," height\n",[45,3530,3531],{"class":47,"line":112},[45,3532,67],{"emptyLinePlaceholder":66},[45,3534,3535,3537,3540,3542,3544],{"class":47,"line":126},[45,3536,2452],{"class":2440},[45,3538,3539],{"class":2455}," area",[45,3541,96],{"class":85},[45,3543,2427],{"class":2461},[45,3545,2464],{"class":85},[45,3547,3548,3551,3553,3555,3557,3560,3562,3564],{"class":47,"line":142},[45,3549,3550],{"class":73},"        return",[45,3552,3136],{"class":2771},[45,3554,240],{"class":85},[45,3556,3509],{"class":3095},[45,3558,3559],{"class":55}," *",[45,3561,3136],{"class":2771},[45,3563,240],{"class":85},[45,3565,3566],{"class":3095},"height\n",[45,3568,3569],{"class":47,"line":149},[45,3570,67],{"emptyLinePlaceholder":66},[45,3572,3573,3576,3578,3580,3582,3585,3587,3589],{"class":47,"line":679},[45,3574,3575],{"class":51},"shape ",[45,3577,56],{"class":55},[45,3579,3476],{"class":2070},[45,3581,96],{"class":85},[45,3583,3584],{"class":59},"4",[45,3586,3079],{"class":85},[45,3588,777],{"class":59},[45,3590,109],{"class":85},[45,3592,3593,3596,3598,3601,3603,3606],{"class":47,"line":686},[45,3594,3595],{"class":51},"result ",[45,3597,56],{"class":55},[45,3599,3600],{"class":51}," shape",[45,3602,240],{"class":85},[45,3604,3605],{"class":2070},"area",[45,3607,2495],{"class":85},[45,3609,3610],{"class":47,"line":2229},[45,3611,67],{"emptyLinePlaceholder":66},[45,3613,3614,3616,3618,3621],{"class":47,"line":2236},[45,3615,2065],{"class":92},[45,3617,96],{"class":85},[45,3619,3620],{"class":2070},"result",[45,3622,109],{"class":85},[14,3624,3625],{},[2421,3626,177],{},[36,3628,3630],{"className":38,"code":3629,"language":40,"meta":41,"style":41},"12\n",[18,3631,3632],{"__ignoreMap":41},[45,3633,3634],{"class":47,"line":48},[45,3635,3629],{"class":59},[14,3637,3638],{},"This method returns the area instead of printing it directly.",[14,3640,3641],{},"That makes it easier to reuse:",[36,3643,3645],{"className":38,"code":3644,"language":40,"meta":41,"style":41},"class Rectangle:\n    def __init__(self, width, height):\n        self.width = width\n        self.height = height\n\n    def area(self):\n        return self.width * self.height\n\nshape = Rectangle(5, 2)\nprint(shape.area() + 10)\n",[18,3646,3647,3655,3675,3687,3699,3703,3715,3733,3737,3757],{"__ignoreMap":41},[45,3648,3649,3651,3653],{"class":47,"line":48},[45,3650,2441],{"class":2440},[45,3652,3476],{"class":2444},[45,3654,86],{"class":85},[45,3656,3657,3659,3661,3663,3665,3667,3669,3671,3673],{"class":47,"line":63},[45,3658,2452],{"class":2440},[45,3660,3072],{"class":92},[45,3662,96],{"class":85},[45,3664,2427],{"class":2461},[45,3666,3079],{"class":85},[45,3668,3493],{"class":3082},[45,3670,3079],{"class":85},[45,3672,3498],{"class":3082},[45,3674,2464],{"class":85},[45,3676,3677,3679,3681,3683,3685],{"class":47,"line":70},[45,3678,3090],{"class":2771},[45,3680,240],{"class":85},[45,3682,3509],{"class":3095},[45,3684,3099],{"class":55},[45,3686,3514],{"class":51},[45,3688,3689,3691,3693,3695,3697],{"class":47,"line":89},[45,3690,3090],{"class":2771},[45,3692,240],{"class":85},[45,3694,3523],{"class":3095},[45,3696,3099],{"class":55},[45,3698,3528],{"class":51},[45,3700,3701],{"class":47,"line":112},[45,3702,67],{"emptyLinePlaceholder":66},[45,3704,3705,3707,3709,3711,3713],{"class":47,"line":126},[45,3706,2452],{"class":2440},[45,3708,3539],{"class":2455},[45,3710,96],{"class":85},[45,3712,2427],{"class":2461},[45,3714,2464],{"class":85},[45,3716,3717,3719,3721,3723,3725,3727,3729,3731],{"class":47,"line":142},[45,3718,3550],{"class":73},[45,3720,3136],{"class":2771},[45,3722,240],{"class":85},[45,3724,3509],{"class":3095},[45,3726,3559],{"class":55},[45,3728,3136],{"class":2771},[45,3730,240],{"class":85},[45,3732,3566],{"class":3095},[45,3734,3735],{"class":47,"line":149},[45,3736,67],{"emptyLinePlaceholder":66},[45,3738,3739,3741,3743,3745,3747,3750,3752,3755],{"class":47,"line":679},[45,3740,3575],{"class":51},[45,3742,56],{"class":55},[45,3744,3476],{"class":2070},[45,3746,96],{"class":85},[45,3748,3749],{"class":59},"5",[45,3751,3079],{"class":85},[45,3753,3754],{"class":59}," 2",[45,3756,109],{"class":85},[45,3758,3759,3761,3763,3766,3768,3770,3773,3776,3778],{"class":47,"line":686},[45,3760,2065],{"class":92},[45,3762,96],{"class":85},[45,3764,3765],{"class":2070},"shape",[45,3767,240],{"class":85},[45,3769,3605],{"class":2070},[45,3771,3772],{"class":85},"()",[45,3774,3775],{"class":55}," +",[45,3777,1190],{"class":59},[45,3779,109],{"class":85},[14,3781,3782],{},[2421,3783,177],{},[36,3785,3786],{"className":38,"code":3436,"language":40,"meta":41,"style":41},[18,3787,3788],{"__ignoreMap":41},[45,3789,3790],{"class":47,"line":48},[45,3791,3436],{"class":59},[31,3793,1337],{"id":1336},[14,3795,3796],{},"Here are some common problems beginners run into with methods.",[712,3798,3800,3801,3803],{"id":3799},"forgetting-self-in-the-method-definition","Forgetting ",[18,3802,2427],{}," in the method definition",[14,3805,3806],{},"Wrong:",[36,3808,3810],{"className":38,"code":3809,"language":40,"meta":41,"style":41},"class Person:\n    def greet():\n        print(\"Hello\")\n",[18,3811,3812,3820,3829],{"__ignoreMap":41},[45,3813,3814,3816,3818],{"class":47,"line":48},[45,3815,2441],{"class":2440},[45,3817,2927],{"class":2444},[45,3819,86],{"class":85},[45,3821,3822,3824,3826],{"class":47,"line":63},[45,3823,2452],{"class":2440},[45,3825,2936],{"class":2455},[45,3827,3828],{"class":85},"():\n",[45,3830,3831,3833,3835,3837,3839,3841],{"class":47,"line":70},[45,3832,1923],{"class":92},[45,3834,96],{"class":85},[45,3836,100],{"class":99},[45,3838,3226],{"class":103},[45,3840,100],{"class":99},[45,3842,109],{"class":85},[14,3844,1466],{},[36,3846,3848],{"className":38,"code":3847,"language":40,"meta":41,"style":41},"class Person:\n    def greet(self):\n        print(\"Hello\")\n",[18,3849,3850,3858,3870],{"__ignoreMap":41},[45,3851,3852,3854,3856],{"class":47,"line":48},[45,3853,2441],{"class":2440},[45,3855,2927],{"class":2444},[45,3857,86],{"class":85},[45,3859,3860,3862,3864,3866,3868],{"class":47,"line":63},[45,3861,2452],{"class":2440},[45,3863,2936],{"class":2455},[45,3865,96],{"class":85},[45,3867,2427],{"class":2461},[45,3869,2464],{"class":85},[45,3871,3872,3874,3876,3878,3880,3882],{"class":47,"line":70},[45,3873,1923],{"class":92},[45,3875,96],{"class":85},[45,3877,100],{"class":99},[45,3879,3226],{"class":103},[45,3881,100],{"class":99},[45,3883,109],{"class":85},[14,3885,3886,3887,3889],{},"Inside a class, instance methods need ",[18,3888,2427],{}," as the first parameter.",[712,3891,3893],{"id":3892},"calling-a-method-on-the-class-instead-of-an-object","Calling a method on the class instead of an object",[14,3895,3806],{},[36,3897,3899],{"className":38,"code":3898,"language":40,"meta":41,"style":41},"class Person:\n    def greet(self):\n        print(\"Hello\")\n\nPerson.greet()\n",[18,3900,3901,3909,3921,3935,3939],{"__ignoreMap":41},[45,3902,3903,3905,3907],{"class":47,"line":48},[45,3904,2441],{"class":2440},[45,3906,2927],{"class":2444},[45,3908,86],{"class":85},[45,3910,3911,3913,3915,3917,3919],{"class":47,"line":63},[45,3912,2452],{"class":2440},[45,3914,2936],{"class":2455},[45,3916,96],{"class":85},[45,3918,2427],{"class":2461},[45,3920,2464],{"class":85},[45,3922,3923,3925,3927,3929,3931,3933],{"class":47,"line":70},[45,3924,1923],{"class":92},[45,3926,96],{"class":85},[45,3928,100],{"class":99},[45,3930,3226],{"class":103},[45,3932,100],{"class":99},[45,3934,109],{"class":85},[45,3936,3937],{"class":47,"line":89},[45,3938,67],{"emptyLinePlaceholder":66},[45,3940,3941,3943,3945,3947],{"class":47,"line":112},[45,3942,2580],{"class":51},[45,3944,240],{"class":85},[45,3946,2982],{"class":2070},[45,3948,2495],{"class":85},[14,3950,3951,3952,240],{},"This can cause an error because no object is being passed as ",[18,3953,2427],{},[14,3955,1466],{},[36,3957,3959],{"className":38,"code":3958,"language":40,"meta":41,"style":41},"class Person:\n    def greet(self):\n        print(\"Hello\")\n\nperson = Person()\nperson.greet()\n",[18,3960,3961,3969,3981,3995,3999,4009],{"__ignoreMap":41},[45,3962,3963,3965,3967],{"class":47,"line":48},[45,3964,2441],{"class":2440},[45,3966,2927],{"class":2444},[45,3968,86],{"class":85},[45,3970,3971,3973,3975,3977,3979],{"class":47,"line":63},[45,3972,2452],{"class":2440},[45,3974,2936],{"class":2455},[45,3976,96],{"class":85},[45,3978,2427],{"class":2461},[45,3980,2464],{"class":85},[45,3982,3983,3985,3987,3989,3991,3993],{"class":47,"line":70},[45,3984,1923],{"class":92},[45,3986,96],{"class":85},[45,3988,100],{"class":99},[45,3990,3226],{"class":103},[45,3992,100],{"class":99},[45,3994,109],{"class":85},[45,3996,3997],{"class":47,"line":89},[45,3998,67],{"emptyLinePlaceholder":66},[45,4000,4001,4003,4005,4007],{"class":47,"line":112},[45,4002,2966],{"class":51},[45,4004,56],{"class":55},[45,4006,2927],{"class":2070},[45,4008,2495],{"class":85},[45,4010,4011,4013,4015,4017],{"class":47,"line":126},[45,4012,2977],{"class":51},[45,4014,240],{"class":85},[45,4016,2982],{"class":2070},[45,4018,2495],{"class":85},[14,4020,4021,4022,240],{},"If you see an error like this, read ",[236,4023,4025,4026],{"href":4024},"\u002Ferrors\u002Ftypeerror-missing-required-positional-argument-fix","how to fix ",[18,4027,4028],{},"TypeError: missing required positional argument",[712,4030,4032,4033,1353,4036],{"id":4031},"writing-persongreet-instead-of-persongreet","Writing ",[18,4034,4035],{},"person.greet",[18,4037,3026],{},[14,4039,4040],{},"This:",[36,4042,4044],{"className":38,"code":4043,"language":40,"meta":41,"style":41},"person.greet\n",[18,4045,4046],{"__ignoreMap":41},[45,4047,4048,4050,4052],{"class":47,"line":48},[45,4049,2977],{"class":51},[45,4051,240],{"class":85},[45,4053,4054],{"class":3095},"greet\n",[14,4056,4057,4058,4060],{},"does ",[2421,4059,2857],{}," call the method. It only refers to the method.",[14,4062,4063],{},"To run it, use parentheses:",[36,4065,4067],{"className":38,"code":4066,"language":40,"meta":41,"style":41},"person.greet()\n",[18,4068,4069],{"__ignoreMap":41},[45,4070,4071,4073,4075,4077],{"class":47,"line":48},[45,4072,2977],{"class":51},[45,4074,240],{"class":85},[45,4076,2982],{"class":2070},[45,4078,2495],{"class":85},[712,4080,4082,4083,1353,4085],{"id":4081},"trying-to-use-name-instead-of-selfname","Trying to use ",[18,4084,3096],{},[18,4086,3044],{},[14,4088,3806],{},[36,4090,4092],{"className":38,"code":4091,"language":40,"meta":41,"style":41},"class Person:\n    def __init__(self, name):\n        self.name = name\n\n    def greet(self):\n        print(name)\n",[18,4093,4094,4102,4118,4130,4134,4146],{"__ignoreMap":41},[45,4095,4096,4098,4100],{"class":47,"line":48},[45,4097,2441],{"class":2440},[45,4099,2927],{"class":2444},[45,4101,86],{"class":85},[45,4103,4104,4106,4108,4110,4112,4114,4116],{"class":47,"line":63},[45,4105,2452],{"class":2440},[45,4107,3072],{"class":92},[45,4109,96],{"class":85},[45,4111,2427],{"class":2461},[45,4113,3079],{"class":85},[45,4115,3083],{"class":3082},[45,4117,2464],{"class":85},[45,4119,4120,4122,4124,4126,4128],{"class":47,"line":70},[45,4121,3090],{"class":2771},[45,4123,240],{"class":85},[45,4125,3096],{"class":3095},[45,4127,3099],{"class":55},[45,4129,3102],{"class":51},[45,4131,4132],{"class":47,"line":89},[45,4133,67],{"emptyLinePlaceholder":66},[45,4135,4136,4138,4140,4142,4144],{"class":47,"line":112},[45,4137,2452],{"class":2440},[45,4139,2936],{"class":2455},[45,4141,96],{"class":85},[45,4143,2427],{"class":2461},[45,4145,2464],{"class":85},[45,4147,4148,4150,4152,4154],{"class":47,"line":126},[45,4149,1923],{"class":92},[45,4151,96],{"class":85},[45,4153,3096],{"class":2070},[45,4155,109],{"class":85},[14,4157,1466],{},[36,4159,4161],{"className":38,"code":4160,"language":40,"meta":41,"style":41},"class Person:\n    def __init__(self, name):\n        self.name = name\n\n    def greet(self):\n        print(self.name)\n",[18,4162,4163,4171,4187,4199,4203,4215],{"__ignoreMap":41},[45,4164,4165,4167,4169],{"class":47,"line":48},[45,4166,2441],{"class":2440},[45,4168,2927],{"class":2444},[45,4170,86],{"class":85},[45,4172,4173,4175,4177,4179,4181,4183,4185],{"class":47,"line":63},[45,4174,2452],{"class":2440},[45,4176,3072],{"class":92},[45,4178,96],{"class":85},[45,4180,2427],{"class":2461},[45,4182,3079],{"class":85},[45,4184,3083],{"class":3082},[45,4186,2464],{"class":85},[45,4188,4189,4191,4193,4195,4197],{"class":47,"line":70},[45,4190,3090],{"class":2771},[45,4192,240],{"class":85},[45,4194,3096],{"class":3095},[45,4196,3099],{"class":55},[45,4198,3102],{"class":51},[45,4200,4201],{"class":47,"line":89},[45,4202,67],{"emptyLinePlaceholder":66},[45,4204,4205,4207,4209,4211,4213],{"class":47,"line":112},[45,4206,2452],{"class":2440},[45,4208,2936],{"class":2455},[45,4210,96],{"class":85},[45,4212,2427],{"class":2461},[45,4214,2464],{"class":85},[45,4216,4217,4219,4221,4223,4225,4227],{"class":47,"line":126},[45,4218,1923],{"class":92},[45,4220,96],{"class":85},[45,4222,2427],{"class":2771},[45,4224,240],{"class":85},[45,4226,3096],{"class":3095},[45,4228,109],{"class":85},[14,4230,4231,4232,240],{},"Inside a method, object attributes should usually be accessed through ",[18,4233,2427],{},[712,4235,4237],{"id":4236},"using-the-wrong-indentation","Using the wrong indentation",[14,4239,4240],{},"Python uses indentation to define blocks.",[14,4242,4243],{},"Wrong indentation inside a class can cause errors or make your method part of the wrong block.",[14,4245,4246],{},"Correct structure:",[36,4248,4249],{"className":38,"code":3847,"language":40,"meta":41,"style":41},[18,4250,4251,4259,4271],{"__ignoreMap":41},[45,4252,4253,4255,4257],{"class":47,"line":48},[45,4254,2441],{"class":2440},[45,4256,2927],{"class":2444},[45,4258,86],{"class":85},[45,4260,4261,4263,4265,4267,4269],{"class":47,"line":63},[45,4262,2452],{"class":2440},[45,4264,2936],{"class":2455},[45,4266,96],{"class":85},[45,4268,2427],{"class":2461},[45,4270,2464],{"class":85},[45,4272,4273,4275,4277,4279,4281,4283],{"class":47,"line":70},[45,4274,1923],{"class":92},[45,4276,96],{"class":85},[45,4278,100],{"class":99},[45,4280,3226],{"class":103},[45,4282,100],{"class":99},[45,4284,109],{"class":85},[31,4286,4288],{"id":4287},"when-to-learn-next","When to learn next",[14,4290,4291],{},"After basic methods, these are good next topics:",[195,4293,4294,4302,4308,4315],{},[198,4295,4296,4297,4301],{},"Learn ",[236,4298,3267,4299,3270],{"href":3266},[18,4300,3262],{}," to set up object data",[198,4303,4304,4305,4307],{},"Review ",[236,4306,2560],{"href":2559}," if classes still feel unclear",[198,4309,4296,4310,4314],{},[236,4311,4313],{"href":4312},"\u002Flearn\u002Finstance-vs-class-variables-in-python","instance vs class variables in Python"," to understand per-object data versus shared data",[198,4316,4317,4318],{},"Practice with ",[236,4319,3034],{"href":3033},[31,4321,4323],{"id":4322},"debugging-tips","Debugging tips",[14,4325,4326],{},"If your method is not working, these can help:",[36,4328,4330],{"className":38,"code":4329,"language":40,"meta":41,"style":41},"print(type(my_object))\nprint(dir(my_object))\nhelp(MyClass)\nprint(my_object.__dict__)\n",[18,4331,4332,4349,4364,4375],{"__ignoreMap":41},[45,4333,4334,4336,4338,4341,4343,4346],{"class":47,"line":48},[45,4335,2065],{"class":92},[45,4337,96],{"class":85},[45,4339,4340],{"class":2109},"type",[45,4342,96],{"class":85},[45,4344,4345],{"class":2070},"my_object",[45,4347,4348],{"class":85},"))\n",[45,4350,4351,4353,4355,4358,4360,4362],{"class":47,"line":63},[45,4352,2065],{"class":92},[45,4354,96],{"class":85},[45,4356,4357],{"class":92},"dir",[45,4359,96],{"class":85},[45,4361,4345],{"class":2070},[45,4363,4348],{"class":85},[45,4365,4366,4368,4370,4373],{"class":47,"line":70},[45,4367,2104],{"class":92},[45,4369,96],{"class":85},[45,4371,4372],{"class":2070},"MyClass",[45,4374,109],{"class":85},[45,4376,4377,4379,4381,4383,4385,4388],{"class":47,"line":89},[45,4378,2065],{"class":92},[45,4380,96],{"class":85},[45,4382,4345],{"class":2070},[45,4384,240],{"class":85},[45,4386,4387],{"class":2771},"__dict__",[45,4389,109],{"class":85},[712,4391,4393],{"id":4392},"what-these-do","What these do",[195,4395,4396,4402,4408,4414],{},[198,4397,4398,4401],{},[18,4399,4400],{},"print(type(my_object))"," shows what kind of object you created",[198,4403,4404,4407],{},[18,4405,4406],{},"print(dir(my_object))"," shows available attributes and methods",[198,4409,4410,4413],{},[18,4411,4412],{},"help(MyClass)"," shows information about the class",[198,4415,4416,4419],{},[18,4417,4418],{},"print(my_object.__dict__)"," shows the object's current attributes",[14,4421,4422,4423,240],{},"If Python says an object does not have a method or attribute you expected, see ",[236,4424,4025,4426],{"href":4425},"\u002Ferrors\u002Fattributeerror-object-has-no-attribute-fix",[18,4427,4428],{},"AttributeError: object has no attribute",[31,4430,2281],{"id":2280},[712,4432,4434],{"id":4433},"what-is-the-difference-between-a-function-and-a-method-in-python","What is the difference between a function and a method in Python?",[14,4436,4437],{},"A function is standalone. A method is a function defined inside a class and usually works with an object.",[712,4439,4441,4442,354],{"id":4440},"do-all-methods-need-self","Do all methods need ",[18,4443,2427],{},[14,4445,4446,4447,4449],{},"Instance methods do. Python uses ",[18,4448,2427],{}," to refer to the current object.",[712,4451,4453],{"id":4452},"can-a-method-return-a-value","Can a method return a value?",[14,4455,4456],{},"Yes. A method can return values just like a normal function.",[712,4458,4460,4461,354],{"id":4459},"why-do-i-get-an-error-about-a-missing-positional-argument-self","Why do I get an error about a missing positional argument ",[18,4462,2427],{},[14,4464,4465,4466,4468],{},"This usually happens when the method is called incorrectly on the class instead of on an object, or when ",[18,4467,2427],{}," was left out of the definition.",[31,4470,2342],{"id":2341},[195,4472,4473,4477,4483,4488,4493,4500],{},[198,4474,4475],{},[236,4476,2560],{"href":2559},[198,4478,4479],{},[236,4480,1732,4481,3270],{"href":3266},[18,4482,3262],{},[198,4484,4485],{},[236,4486,4487],{"href":4312},"Instance vs class variables in Python",[198,4489,4490],{},[236,4491,4492],{"href":3033},"How to add methods to a class in Python",[198,4494,4495],{},[236,4496,4497,4498],{"href":4024},"How to fix ",[18,4499,4028],{},[198,4501,4502],{},[236,4503,4497,4504],{"href":4425},[18,4505,4428],{},[2367,4507,4508],{},"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 .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 .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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":4510},[4511,4512,4513,4514,4516,4519,4522,4523,4533,4534,4537,4545],{"id":33,"depth":63,"text":34},{"id":2529,"depth":63,"text":2530},{"id":2549,"depth":63,"text":2550},{"id":2714,"depth":63,"text":4515},"Why self is used",{"id":2883,"depth":63,"text":2884,"children":4517},[4518],{"id":3000,"depth":70,"text":3001},{"id":3037,"depth":63,"text":3038,"children":4520},[4521],{"id":3273,"depth":70,"text":3274},{"id":3451,"depth":63,"text":3452},{"id":1336,"depth":63,"text":1337,"children":4524},[4525,4527,4528,4530,4532],{"id":3799,"depth":70,"text":4526},"Forgetting self in the method definition",{"id":3892,"depth":70,"text":3893},{"id":4031,"depth":70,"text":4529},"Writing person.greet instead of person.greet()",{"id":4081,"depth":70,"text":4531},"Trying to use name instead of self.name",{"id":4236,"depth":70,"text":4237},{"id":4287,"depth":63,"text":4288},{"id":4322,"depth":63,"text":4323,"children":4535},[4536],{"id":4392,"depth":70,"text":4393},{"id":2280,"depth":63,"text":2281,"children":4538},[4539,4540,4542,4543],{"id":4433,"depth":70,"text":4434},{"id":4440,"depth":70,"text":4541},"Do all methods need self?",{"id":4452,"depth":70,"text":4453},{"id":4459,"depth":70,"text":4544},"Why do I get an error about a missing positional argument self?",{"id":2341,"depth":63,"text":2342},"Master basic methods in python classes explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fbasic-methods-in-python-classes-explained",{"title":2408,"description":4546},"learn\u002Fbasic-methods-in-python-classes-explained","4fOwl-v5h0mKFGBmJxv45B-54e5IuXuQ6RcO4cMIHI4",{"id":4553,"title":4554,"body":4555,"description":6384,"extension":2399,"meta":6385,"navigation":66,"path":6386,"seo":6387,"stem":6388,"__hash__":6389},"content\u002Flearn\u002Fdecorators-in-python-beginner-introduction.md","Decorators in Python (Beginner Introduction)",{"type":7,"value":4556,"toc":6347},[4557,4560,4567,4574,4585,4711,4714,4718,4721,4736,4740,4743,4746,4749,4763,4766,4769,4773,4776,4779,4796,4799,4803,4809,4811,4925,4929,4963,4982,4988,4991,4995,5003,5006,5017,5020,5024,5029,5032,5141,5144,5253,5256,5278,5281,5307,5310,5328,5333,5337,5340,5449,5453,5459,5463,5491,5497,5507,5510,5514,5517,5520,5561,5564,5574,5742,5745,5751,5760,5762,5774,5777,5792,5796,5799,5815,5822,5826,5829,5832,5846,5849,5860,5863,5867,5870,5873,5900,5904,5907,5924,5928,5930,5992,6001,6004,6070,6074,6077,6136,6138,6202,6206,6209,6258,6261,6263,6267,6270,6274,6280,6284,6287,6291,6294,6298,6306,6308,6344],[10,4558,4554],{"id":4559},"decorators-in-python-beginner-introduction",[14,4561,4562,4563,4566],{},"Decorators can look confusing when you first see code like ",[18,4564,4565],{},"@something"," above a function.",[14,4568,4569,4570,4573],{},"This page explains the basic idea in simple terms. You will learn what a decorator is, why it is useful, how the ",[18,4571,4572],{},"@"," syntax works, and how to read simple decorator examples without getting into advanced patterns.",[14,4575,4576,4577,1316,4581,240],{},"If decorators still feel unclear, it helps to first understand ",[236,4578,4580],{"href":4579},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions",[236,4582,4584],{"href":4583},"\u002Flearn\u002Ffunction-parameters-and-arguments-in-python\u002F","function parameters and arguments",[36,4586,4588],{"className":38,"code":4587,"language":40,"meta":41,"style":41},"def say_hello():\n    print(\"Hello\")\n\ndef simple_decorator(func):\n    def wrapper():\n        print(\"Before function\")\n        func()\n        print(\"After function\")\n    return wrapper\n\nsay_hello = simple_decorator(say_hello)\nsay_hello()\n",[18,4589,4590,4599,4613,4617,4631,4640,4655,4662,4677,4685,4689,4705],{"__ignoreMap":41},[45,4591,4592,4594,4597],{"class":47,"line":48},[45,4593,2757],{"class":2440},[45,4595,4596],{"class":2455}," say_hello",[45,4598,3828],{"class":85},[45,4600,4601,4603,4605,4607,4609,4611],{"class":47,"line":63},[45,4602,93],{"class":92},[45,4604,96],{"class":85},[45,4606,100],{"class":99},[45,4608,3226],{"class":103},[45,4610,100],{"class":99},[45,4612,109],{"class":85},[45,4614,4615],{"class":47,"line":70},[45,4616,67],{"emptyLinePlaceholder":66},[45,4618,4619,4621,4624,4626,4629],{"class":47,"line":89},[45,4620,2757],{"class":2440},[45,4622,4623],{"class":2455}," simple_decorator",[45,4625,96],{"class":85},[45,4627,4628],{"class":3082},"func",[45,4630,2464],{"class":85},[45,4632,4633,4635,4638],{"class":47,"line":112},[45,4634,2452],{"class":2440},[45,4636,4637],{"class":2455}," wrapper",[45,4639,3828],{"class":85},[45,4641,4642,4644,4646,4648,4651,4653],{"class":47,"line":126},[45,4643,1923],{"class":92},[45,4645,96],{"class":85},[45,4647,100],{"class":99},[45,4649,4650],{"class":103},"Before function",[45,4652,100],{"class":99},[45,4654,109],{"class":85},[45,4656,4657,4660],{"class":47,"line":142},[45,4658,4659],{"class":2070},"        func",[45,4661,2495],{"class":85},[45,4663,4664,4666,4668,4670,4673,4675],{"class":47,"line":149},[45,4665,1923],{"class":92},[45,4667,96],{"class":85},[45,4669,100],{"class":99},[45,4671,4672],{"class":103},"After function",[45,4674,100],{"class":99},[45,4676,109],{"class":85},[45,4678,4679,4682],{"class":47,"line":679},[45,4680,4681],{"class":73},"    return",[45,4683,4684],{"class":51}," wrapper\n",[45,4686,4687],{"class":47,"line":686},[45,4688,67],{"emptyLinePlaceholder":66},[45,4690,4691,4694,4696,4698,4700,4703],{"class":47,"line":2229},[45,4692,4693],{"class":51},"say_hello ",[45,4695,56],{"class":55},[45,4697,4623],{"class":2070},[45,4699,96],{"class":85},[45,4701,4702],{"class":2070},"say_hello",[45,4704,109],{"class":85},[45,4706,4707,4709],{"class":47,"line":2236},[45,4708,4702],{"class":2070},[45,4710,2495],{"class":85},[14,4712,4713],{},"Use this small example to understand the main idea: a decorator takes a function, wraps extra behavior around it, and returns a new function.",[31,4715,4717],{"id":4716},"what-this-page-is-for","What this page is for",[14,4719,4720],{},"This page is meant for beginners who:",[195,4722,4723,4730,4733],{},[198,4724,4725,4726,4729],{},"Have seen ",[18,4727,4728],{},"@decorator"," syntax and want a simple explanation",[198,4731,4732],{},"Want to understand the core idea before learning advanced patterns",[198,4734,4735],{},"Need to see that decorators are just functions that modify other functions",[31,4737,4739],{"id":4738},"what-a-decorator-is","What a decorator is",[14,4741,4742],{},"A decorator is a function that takes another function as input.",[14,4744,4745],{},"Then it returns a new function. That new function usually adds some extra behavior.",[14,4747,4748],{},"In simple terms:",[195,4750,4751,4754,4757,4760],{},[198,4752,4753],{},"A decorator receives a function",[198,4755,4756],{},"It creates a wrapper function",[198,4758,4759],{},"The wrapper can do something before or after the original function",[198,4761,4762],{},"The decorator returns the wrapper",[14,4764,4765],{},"The original function can still run inside the wrapper.",[14,4767,4768],{},"This is useful because you can reuse the same extra behavior in many places instead of repeating code.",[31,4770,4772],{"id":4771},"why-decorators-are-useful","Why decorators are useful",[14,4774,4775],{},"Decorators are useful when you want to add the same kind of behavior to multiple functions.",[14,4777,4778],{},"Common uses include:",[195,4780,4781,4784,4787,4790,4793],{},[198,4782,4783],{},"Running code before a function starts",[198,4785,4786],{},"Running code after a function finishes",[198,4788,4789],{},"Logging function calls",[198,4791,4792],{},"Checking permissions or conditions",[198,4794,4795],{},"Measuring how long a function takes",[14,4797,4798],{},"You do not need advanced projects to understand the benefit. Even a simple message before and after a function shows the idea clearly.",[31,4800,4802],{"id":4801},"understand-the-function-wrapping-idea-first","Understand the function-wrapping idea first",[14,4804,4805,4806,4808],{},"Before using the ",[18,4807,4572],{}," symbol, it helps to understand the manual version.",[14,4810,718],{},[36,4812,4813],{"className":38,"code":4587,"language":40,"meta":41,"style":41},[18,4814,4815,4823,4837,4841,4853,4861,4875,4881,4895,4901,4905,4919],{"__ignoreMap":41},[45,4816,4817,4819,4821],{"class":47,"line":48},[45,4818,2757],{"class":2440},[45,4820,4596],{"class":2455},[45,4822,3828],{"class":85},[45,4824,4825,4827,4829,4831,4833,4835],{"class":47,"line":63},[45,4826,93],{"class":92},[45,4828,96],{"class":85},[45,4830,100],{"class":99},[45,4832,3226],{"class":103},[45,4834,100],{"class":99},[45,4836,109],{"class":85},[45,4838,4839],{"class":47,"line":70},[45,4840,67],{"emptyLinePlaceholder":66},[45,4842,4843,4845,4847,4849,4851],{"class":47,"line":89},[45,4844,2757],{"class":2440},[45,4846,4623],{"class":2455},[45,4848,96],{"class":85},[45,4850,4628],{"class":3082},[45,4852,2464],{"class":85},[45,4854,4855,4857,4859],{"class":47,"line":112},[45,4856,2452],{"class":2440},[45,4858,4637],{"class":2455},[45,4860,3828],{"class":85},[45,4862,4863,4865,4867,4869,4871,4873],{"class":47,"line":126},[45,4864,1923],{"class":92},[45,4866,96],{"class":85},[45,4868,100],{"class":99},[45,4870,4650],{"class":103},[45,4872,100],{"class":99},[45,4874,109],{"class":85},[45,4876,4877,4879],{"class":47,"line":142},[45,4878,4659],{"class":2070},[45,4880,2495],{"class":85},[45,4882,4883,4885,4887,4889,4891,4893],{"class":47,"line":149},[45,4884,1923],{"class":92},[45,4886,96],{"class":85},[45,4888,100],{"class":99},[45,4890,4672],{"class":103},[45,4892,100],{"class":99},[45,4894,109],{"class":85},[45,4896,4897,4899],{"class":47,"line":679},[45,4898,4681],{"class":73},[45,4900,4684],{"class":51},[45,4902,4903],{"class":47,"line":686},[45,4904,67],{"emptyLinePlaceholder":66},[45,4906,4907,4909,4911,4913,4915,4917],{"class":47,"line":2229},[45,4908,4693],{"class":51},[45,4910,56],{"class":55},[45,4912,4623],{"class":2070},[45,4914,96],{"class":85},[45,4916,4702],{"class":2070},[45,4918,109],{"class":85},[45,4920,4921,4923],{"class":47,"line":2236},[45,4922,4702],{"class":2070},[45,4924,2495],{"class":85},[712,4926,4928],{"id":4927},"what-happens-here","What happens here",[195,4930,4931,4936,4942,4949,4954,4960],{},[198,4932,4933,4935],{},[18,4934,4702],{}," is a normal function",[198,4937,4938,4941],{},[18,4939,4940],{},"simple_decorator"," takes a function as input",[198,4943,4944,4945,4948],{},"Inside it, ",[18,4946,4947],{},"wrapper()"," is created",[198,4950,4951,4953],{},[18,4952,4947],{}," prints a message, calls the original function, and prints another message",[198,4955,4956,4957],{},"The decorator returns ",[18,4958,4959],{},"wrapper",[198,4961,4962],{},"This line replaces the old function reference:",[36,4964,4966],{"className":38,"code":4965,"language":40,"meta":41,"style":41},"say_hello = simple_decorator(say_hello)\n",[18,4967,4968],{"__ignoreMap":41},[45,4969,4970,4972,4974,4976,4978,4980],{"class":47,"line":48},[45,4971,4693],{"class":51},[45,4973,56],{"class":55},[45,4975,4623],{"class":2070},[45,4977,96],{"class":85},[45,4979,4702],{"class":2070},[45,4981,109],{"class":85},[14,4983,4984,4985,4987],{},"After that line, ",[18,4986,4702],{}," no longer refers to the original function directly.",[14,4989,4990],{},"It now refers to the wrapped version.",[712,4992,4994],{"id":4993},"expected-output","Expected output",[36,4996,5001],{"className":4997,"code":4999,"language":5000,"meta":41},[4998],"language-text","Before function\nHello\nAfter function\n","text",[18,5002,4999],{"__ignoreMap":41},[14,5004,5005],{},"The key line inside the wrapper is:",[36,5007,5009],{"className":38,"code":5008,"language":40,"meta":41,"style":41},"func()\n",[18,5010,5011],{"__ignoreMap":41},[45,5012,5013,5015],{"class":47,"line":48},[45,5014,4628],{"class":2070},[45,5016,2495],{"class":85},[14,5018,5019],{},"That line runs the original function. If you remove it, the original function will not run.",[31,5021,5023],{"id":5022},"how-the-syntax-works","How the @ syntax works",[14,5025,1732,5026,5028],{},[18,5027,4572],{}," syntax is just a shorter way to apply a decorator.",[14,5030,5031],{},"This code:",[36,5033,5035],{"className":38,"code":5034,"language":40,"meta":41,"style":41},"def say_hello():\n    print(\"Hello\")\n\ndef simple_decorator(func):\n    def wrapper():\n        print(\"Before function\")\n        func()\n        print(\"After function\")\n    return wrapper\n\nsay_hello = simple_decorator(say_hello)\n",[18,5036,5037,5045,5059,5063,5075,5083,5097,5103,5117,5123,5127],{"__ignoreMap":41},[45,5038,5039,5041,5043],{"class":47,"line":48},[45,5040,2757],{"class":2440},[45,5042,4596],{"class":2455},[45,5044,3828],{"class":85},[45,5046,5047,5049,5051,5053,5055,5057],{"class":47,"line":63},[45,5048,93],{"class":92},[45,5050,96],{"class":85},[45,5052,100],{"class":99},[45,5054,3226],{"class":103},[45,5056,100],{"class":99},[45,5058,109],{"class":85},[45,5060,5061],{"class":47,"line":70},[45,5062,67],{"emptyLinePlaceholder":66},[45,5064,5065,5067,5069,5071,5073],{"class":47,"line":89},[45,5066,2757],{"class":2440},[45,5068,4623],{"class":2455},[45,5070,96],{"class":85},[45,5072,4628],{"class":3082},[45,5074,2464],{"class":85},[45,5076,5077,5079,5081],{"class":47,"line":112},[45,5078,2452],{"class":2440},[45,5080,4637],{"class":2455},[45,5082,3828],{"class":85},[45,5084,5085,5087,5089,5091,5093,5095],{"class":47,"line":126},[45,5086,1923],{"class":92},[45,5088,96],{"class":85},[45,5090,100],{"class":99},[45,5092,4650],{"class":103},[45,5094,100],{"class":99},[45,5096,109],{"class":85},[45,5098,5099,5101],{"class":47,"line":142},[45,5100,4659],{"class":2070},[45,5102,2495],{"class":85},[45,5104,5105,5107,5109,5111,5113,5115],{"class":47,"line":149},[45,5106,1923],{"class":92},[45,5108,96],{"class":85},[45,5110,100],{"class":99},[45,5112,4672],{"class":103},[45,5114,100],{"class":99},[45,5116,109],{"class":85},[45,5118,5119,5121],{"class":47,"line":679},[45,5120,4681],{"class":73},[45,5122,4684],{"class":51},[45,5124,5125],{"class":47,"line":686},[45,5126,67],{"emptyLinePlaceholder":66},[45,5128,5129,5131,5133,5135,5137,5139],{"class":47,"line":2229},[45,5130,4693],{"class":51},[45,5132,56],{"class":55},[45,5134,4623],{"class":2070},[45,5136,96],{"class":85},[45,5138,4702],{"class":2070},[45,5140,109],{"class":85},[14,5142,5143],{},"can be written as:",[36,5145,5147],{"className":38,"code":5146,"language":40,"meta":41,"style":41},"def simple_decorator(func):\n    def wrapper():\n        print(\"Before function\")\n        func()\n        print(\"After function\")\n    return wrapper\n\n@simple_decorator\ndef say_hello():\n    print(\"Hello\")\n\nsay_hello()\n",[18,5148,5149,5161,5169,5183,5189,5203,5209,5213,5221,5229,5243,5247],{"__ignoreMap":41},[45,5150,5151,5153,5155,5157,5159],{"class":47,"line":48},[45,5152,2757],{"class":2440},[45,5154,4623],{"class":2455},[45,5156,96],{"class":85},[45,5158,4628],{"class":3082},[45,5160,2464],{"class":85},[45,5162,5163,5165,5167],{"class":47,"line":63},[45,5164,2452],{"class":2440},[45,5166,4637],{"class":2455},[45,5168,3828],{"class":85},[45,5170,5171,5173,5175,5177,5179,5181],{"class":47,"line":70},[45,5172,1923],{"class":92},[45,5174,96],{"class":85},[45,5176,100],{"class":99},[45,5178,4650],{"class":103},[45,5180,100],{"class":99},[45,5182,109],{"class":85},[45,5184,5185,5187],{"class":47,"line":89},[45,5186,4659],{"class":2070},[45,5188,2495],{"class":85},[45,5190,5191,5193,5195,5197,5199,5201],{"class":47,"line":112},[45,5192,1923],{"class":92},[45,5194,96],{"class":85},[45,5196,100],{"class":99},[45,5198,4672],{"class":103},[45,5200,100],{"class":99},[45,5202,109],{"class":85},[45,5204,5205,5207],{"class":47,"line":126},[45,5206,4681],{"class":73},[45,5208,4684],{"class":51},[45,5210,5211],{"class":47,"line":142},[45,5212,67],{"emptyLinePlaceholder":66},[45,5214,5215,5218],{"class":47,"line":149},[45,5216,4572],{"class":5217},"stp6e",[45,5219,5220],{"class":2455},"simple_decorator\n",[45,5222,5223,5225,5227],{"class":47,"line":679},[45,5224,2757],{"class":2440},[45,5226,4596],{"class":2455},[45,5228,3828],{"class":85},[45,5230,5231,5233,5235,5237,5239,5241],{"class":47,"line":686},[45,5232,93],{"class":92},[45,5234,96],{"class":85},[45,5236,100],{"class":99},[45,5238,3226],{"class":103},[45,5240,100],{"class":99},[45,5242,109],{"class":85},[45,5244,5245],{"class":47,"line":2229},[45,5246,67],{"emptyLinePlaceholder":66},[45,5248,5249,5251],{"class":47,"line":2236},[45,5250,4702],{"class":2070},[45,5252,2495],{"class":85},[14,5254,5255],{},"A good mental model is:",[36,5257,5259],{"className":38,"code":5258,"language":40,"meta":41,"style":41},"my_function = my_decorator(my_function)\n",[18,5260,5261],{"__ignoreMap":41},[45,5262,5263,5266,5268,5271,5273,5276],{"class":47,"line":48},[45,5264,5265],{"class":51},"my_function ",[45,5267,56],{"class":55},[45,5269,5270],{"class":2070}," my_decorator",[45,5272,96],{"class":85},[45,5274,5275],{"class":2070},"my_function",[45,5277,109],{"class":85},[14,5279,5280],{},"So when you see:",[36,5282,5284],{"className":38,"code":5283,"language":40,"meta":41,"style":41},"@my_decorator\ndef my_function():\n    pass\n",[18,5285,5286,5293,5302],{"__ignoreMap":41},[45,5287,5288,5290],{"class":47,"line":48},[45,5289,4572],{"class":5217},[45,5291,5292],{"class":2455},"my_decorator\n",[45,5294,5295,5297,5300],{"class":47,"line":63},[45,5296,2757],{"class":2440},[45,5298,5299],{"class":2455}," my_function",[45,5301,3828],{"class":85},[45,5303,5304],{"class":47,"line":70},[45,5305,5306],{"class":73},"    pass\n",[14,5308,5309],{},"you can read it as:",[195,5311,5312,5317,5323],{},[198,5313,5314,5315],{},"Create ",[18,5316,5275],{},[198,5318,5319,5320],{},"Pass it into ",[18,5321,5322],{},"my_decorator",[198,5324,5325,5326],{},"Store the returned function back in ",[18,5327,5275],{},[14,5329,1732,5330,5332],{},[18,5331,4572],{}," form is cleaner, but it does not change the basic idea.",[31,5334,5336],{"id":5335},"a-minimal-beginner-example","A minimal beginner example",[14,5338,5339],{},"Here is a small decorator example with clear output:",[36,5341,5343],{"className":38,"code":5342,"language":40,"meta":41,"style":41},"def simple_decorator(func):\n    def wrapper():\n        print(\"Starting...\")\n        func()\n        print(\"Done.\")\n    return wrapper\n\n@simple_decorator\ndef greet():\n    print(\"Hello\")\n\ngreet()\n",[18,5344,5345,5357,5365,5380,5386,5401,5407,5411,5417,5425,5439,5443],{"__ignoreMap":41},[45,5346,5347,5349,5351,5353,5355],{"class":47,"line":48},[45,5348,2757],{"class":2440},[45,5350,4623],{"class":2455},[45,5352,96],{"class":85},[45,5354,4628],{"class":3082},[45,5356,2464],{"class":85},[45,5358,5359,5361,5363],{"class":47,"line":63},[45,5360,2452],{"class":2440},[45,5362,4637],{"class":2455},[45,5364,3828],{"class":85},[45,5366,5367,5369,5371,5373,5376,5378],{"class":47,"line":70},[45,5368,1923],{"class":92},[45,5370,96],{"class":85},[45,5372,100],{"class":99},[45,5374,5375],{"class":103},"Starting...",[45,5377,100],{"class":99},[45,5379,109],{"class":85},[45,5381,5382,5384],{"class":47,"line":89},[45,5383,4659],{"class":2070},[45,5385,2495],{"class":85},[45,5387,5388,5390,5392,5394,5397,5399],{"class":47,"line":112},[45,5389,1923],{"class":92},[45,5391,96],{"class":85},[45,5393,100],{"class":99},[45,5395,5396],{"class":103},"Done.",[45,5398,100],{"class":99},[45,5400,109],{"class":85},[45,5402,5403,5405],{"class":47,"line":126},[45,5404,4681],{"class":73},[45,5406,4684],{"class":51},[45,5408,5409],{"class":47,"line":142},[45,5410,67],{"emptyLinePlaceholder":66},[45,5412,5413,5415],{"class":47,"line":149},[45,5414,4572],{"class":5217},[45,5416,5220],{"class":2455},[45,5418,5419,5421,5423],{"class":47,"line":679},[45,5420,2757],{"class":2440},[45,5422,2936],{"class":2455},[45,5424,3828],{"class":85},[45,5426,5427,5429,5431,5433,5435,5437],{"class":47,"line":686},[45,5428,93],{"class":92},[45,5430,96],{"class":85},[45,5432,100],{"class":99},[45,5434,3226],{"class":103},[45,5436,100],{"class":99},[45,5438,109],{"class":85},[45,5440,5441],{"class":47,"line":2229},[45,5442,67],{"emptyLinePlaceholder":66},[45,5444,5445,5447],{"class":47,"line":2236},[45,5446,2982],{"class":2070},[45,5448,2495],{"class":85},[712,5450,5452],{"id":5451},"output","Output",[36,5454,5457],{"className":5455,"code":5456,"language":5000,"meta":41},[4998],"Starting...\nHello\nDone.\n",[18,5458,5456],{"__ignoreMap":41},[712,5460,5462],{"id":5461},"why-this-works","Why this works",[195,5464,5465,5470,5479,5483],{},[198,5466,5467,5469],{},[18,5468,2583],{}," is the original function",[198,5471,5472,5475,5476,5478],{},[18,5473,5474],{},"@simple_decorator"," passes ",[18,5477,2982],{}," into the decorator",[198,5480,4956,5481],{},[18,5482,4959],{},[198,5484,5485,5486,5488,5489],{},"Calling ",[18,5487,2583],{}," now really calls ",[18,5490,4947],{},[14,5492,5493,5494,5496],{},"Inside ",[18,5495,4947],{},", this line runs the original function:",[36,5498,5499],{"className":38,"code":5008,"language":40,"meta":41,"style":41},[18,5500,5501],{"__ignoreMap":41},[45,5502,5503,5505],{"class":47,"line":48},[45,5504,4628],{"class":2070},[45,5506,2495],{"class":85},[14,5508,5509],{},"That is the most important line in a basic decorator.",[31,5511,5513],{"id":5512},"decorators-and-function-arguments","Decorators and function arguments",[14,5515,5516],{},"A common beginner problem is that the decorator works for functions with no arguments, but breaks for functions that need values.",[14,5518,5519],{},"For example, this function needs a name:",[36,5521,5523],{"className":38,"code":5522,"language":40,"meta":41,"style":41},"def greet(name):\n    print(f\"Hello, {name}\")\n",[18,5524,5525,5537],{"__ignoreMap":41},[45,5526,5527,5529,5531,5533,5535],{"class":47,"line":48},[45,5528,2757],{"class":2440},[45,5530,2936],{"class":2455},[45,5532,96],{"class":85},[45,5534,3096],{"class":3082},[45,5536,2464],{"class":85},[45,5538,5539,5541,5543,5546,5549,5552,5554,5557,5559],{"class":47,"line":63},[45,5540,93],{"class":92},[45,5542,96],{"class":85},[45,5544,5545],{"class":2440},"f",[45,5547,5548],{"class":103},"\"Hello, ",[45,5550,5551],{"class":59},"{",[45,5553,3096],{"class":2070},[45,5555,5556],{"class":59},"}",[45,5558,100],{"class":103},[45,5560,109],{"class":85},[14,5562,5563],{},"If your wrapper does not accept arguments, it will fail.",[712,5565,5567,5568,1316,5571],{"id":5566},"better-version-with-args-and-kwargs","Better version with ",[18,5569,5570],{},"*args",[18,5572,5573],{},"**kwargs",[36,5575,5577],{"className":38,"code":5576,"language":40,"meta":41,"style":41},"def simple_decorator(func):\n    def wrapper(*args, **kwargs):\n        print(\"Before function\")\n        result = func(*args, **kwargs)\n        print(\"After function\")\n        return result\n    return wrapper\n\n@simple_decorator\ndef greet(name):\n    print(f\"Hello, {name}\")\n\ngreet(\"Maya\")\n",[18,5578,5579,5591,5615,5629,5653,5667,5674,5680,5684,5690,5702,5722,5726],{"__ignoreMap":41},[45,5580,5581,5583,5585,5587,5589],{"class":47,"line":48},[45,5582,2757],{"class":2440},[45,5584,4623],{"class":2455},[45,5586,96],{"class":85},[45,5588,4628],{"class":3082},[45,5590,2464],{"class":85},[45,5592,5593,5595,5597,5599,5602,5605,5607,5610,5613],{"class":47,"line":63},[45,5594,2452],{"class":2440},[45,5596,4637],{"class":2455},[45,5598,96],{"class":85},[45,5600,5601],{"class":55},"*",[45,5603,5604],{"class":3082},"args",[45,5606,3079],{"class":85},[45,5608,5609],{"class":55}," **",[45,5611,5612],{"class":3082},"kwargs",[45,5614,2464],{"class":85},[45,5616,5617,5619,5621,5623,5625,5627],{"class":47,"line":70},[45,5618,1923],{"class":92},[45,5620,96],{"class":85},[45,5622,100],{"class":99},[45,5624,4650],{"class":103},[45,5626,100],{"class":99},[45,5628,109],{"class":85},[45,5630,5631,5634,5636,5639,5641,5643,5645,5647,5649,5651],{"class":47,"line":89},[45,5632,5633],{"class":51},"        result ",[45,5635,56],{"class":55},[45,5637,5638],{"class":2070}," func",[45,5640,96],{"class":85},[45,5642,5601],{"class":55},[45,5644,5604],{"class":2070},[45,5646,3079],{"class":85},[45,5648,5609],{"class":55},[45,5650,5612],{"class":2070},[45,5652,109],{"class":85},[45,5654,5655,5657,5659,5661,5663,5665],{"class":47,"line":112},[45,5656,1923],{"class":92},[45,5658,96],{"class":85},[45,5660,100],{"class":99},[45,5662,4672],{"class":103},[45,5664,100],{"class":99},[45,5666,109],{"class":85},[45,5668,5669,5671],{"class":47,"line":126},[45,5670,3550],{"class":73},[45,5672,5673],{"class":51}," result\n",[45,5675,5676,5678],{"class":47,"line":142},[45,5677,4681],{"class":73},[45,5679,4684],{"class":51},[45,5681,5682],{"class":47,"line":149},[45,5683,67],{"emptyLinePlaceholder":66},[45,5685,5686,5688],{"class":47,"line":679},[45,5687,4572],{"class":5217},[45,5689,5220],{"class":2455},[45,5691,5692,5694,5696,5698,5700],{"class":47,"line":686},[45,5693,2757],{"class":2440},[45,5695,2936],{"class":2455},[45,5697,96],{"class":85},[45,5699,3096],{"class":3082},[45,5701,2464],{"class":85},[45,5703,5704,5706,5708,5710,5712,5714,5716,5718,5720],{"class":47,"line":2229},[45,5705,93],{"class":92},[45,5707,96],{"class":85},[45,5709,5545],{"class":2440},[45,5711,5548],{"class":103},[45,5713,5551],{"class":59},[45,5715,3096],{"class":2070},[45,5717,5556],{"class":59},[45,5719,100],{"class":103},[45,5721,109],{"class":85},[45,5723,5724],{"class":47,"line":2236},[45,5725,67],{"emptyLinePlaceholder":66},[45,5727,5729,5731,5733,5735,5738,5740],{"class":47,"line":5728},13,[45,5730,2982],{"class":2070},[45,5732,96],{"class":85},[45,5734,100],{"class":99},[45,5736,5737],{"class":103},"Maya",[45,5739,100],{"class":99},[45,5741,109],{"class":85},[712,5743,5452],{"id":5744},"output-1",[36,5746,5749],{"className":5747,"code":5748,"language":5000,"meta":41},[4998],"Before function\nHello, Maya\nAfter function\n",[18,5750,5748],{"__ignoreMap":41},[712,5752,5754,5755,1316,5757,5759],{"id":5753},"what-args-and-kwargs-mean","What ",[18,5756,5570],{},[18,5758,5573],{}," mean",[14,5761,4748],{},[195,5763,5764,5769],{},[198,5765,5766,5768],{},[18,5767,5570],{}," means “accept any positional arguments”",[198,5770,5771,5773],{},[18,5772,5573],{}," means “accept any keyword arguments”",[14,5775,5776],{},"This makes the wrapper more flexible.",[14,5778,5779,5780,5783,5784,1316,5788,240],{},"If function arguments feel unclear, review ",[236,5781,5782],{"href":4583},"function parameters and arguments in Python"," or the glossary pages for ",[236,5785,5787],{"href":5786},"\u002Fglossary\u002Fwhat-is-a-parameter-in-python\u002F","parameter",[236,5789,5791],{"href":5790},"\u002Fglossary\u002Fwhat-is-an-argument-in-python\u002F","argument",[31,5793,5795],{"id":5794},"common-beginner-confusion","Common beginner confusion",[14,5797,5798],{},"These points often cause problems:",[195,5800,5801,5804,5807,5810],{},[198,5802,5803],{},"A decorator does not only run once forever. The wrapper runs each time the function is called.",[198,5805,5806],{},"The decorator returns a function.",[198,5808,5809],{},"The wrapper must call the original function if you want the original behavior to happen.",[198,5811,1350,5812,5814],{},[18,5813,4572],{}," is optional. It is only a cleaner syntax.",[14,5816,5817,5818,240],{},"It also helps to remember that functions are values in Python. You can pass them into other functions, return them, and store them in variables. If that idea feels new, start with ",[236,5819,5821],{"href":5820},"\u002Fglossary\u002Fwhat-is-a-function-in-python\u002F","what a function is in Python",[31,5823,5825],{"id":5824},"when-beginners-should-use-decorators","When beginners should use decorators",[14,5827,5828],{},"For most beginners, decorators are more important to read than to write at first.",[14,5830,5831],{},"A good approach is:",[195,5833,5834,5837,5840,5843],{},[198,5835,5836],{},"First learn normal functions",[198,5838,5839],{},"Then learn nested functions",[198,5841,5842],{},"Then read simple decorators",[198,5844,5845],{},"Then try writing very small ones yourself",[14,5847,5848],{},"It is fine to avoid advanced topics for now, such as:",[195,5850,5851,5854,5857],{},[198,5852,5853],{},"Decorator factories",[198,5855,5856],{},"Multiple stacked decorators",[198,5858,5859],{},"Metadata preservation tools",[14,5861,5862],{},"Learn the core pattern first. Later, you will see decorators in real code, libraries, and sometimes in class-related code or module-based projects.",[31,5864,5866],{"id":5865},"natural-next-steps","Natural next steps",[14,5868,5869],{},"If decorators still feel confusing, go back to the basics first.",[14,5871,5872],{},"Helpful next steps:",[195,5874,5875,5880,5886,5893],{},[198,5876,4304,5877],{},[236,5878,5879],{"href":4579},"Python functions explained",[198,5881,4296,5882],{},[236,5883,5885],{"href":5884},"\u002Flearn\u002Freturn-values-in-python-functions\u002F","return values in Python functions",[198,5887,5888,5889],{},"Practice ",[236,5890,5892],{"href":5891},"\u002Fhow-to\u002Fhow-to-create-a-simple-function-in-python\u002F","how to create a simple function in Python",[198,5894,4296,5895,5899],{},[236,5896,5898],{"href":5897},"\u002Flearn\u002Fpython-modules-explained\u002F","Python modules explained"," later, since decorators often appear in larger projects",[31,5901,5903],{"id":5902},"common-mistakes","Common mistakes",[14,5905,5906],{},"Here are some common causes of decorator confusion:",[195,5908,5909,5912,5915,5918,5921],{},[198,5910,5911],{},"Trying to learn decorators before understanding functions",[198,5913,5914],{},"Not realizing a function can be passed as a value",[198,5916,5917],{},"Forgetting to return the wrapper function from the decorator",[198,5919,5920],{},"Forgetting to call the original function inside the wrapper",[198,5922,5923],{},"Writing a wrapper with no parameters for a function that needs arguments",[712,5925,5927],{"id":5926},"example-forgetting-to-return-the-wrapper","Example: forgetting to return the wrapper",[14,5929,1358],{},[36,5931,5933],{"className":38,"code":5932,"language":40,"meta":41,"style":41},"def bad_decorator(func):\n    def wrapper():\n        print(\"Before\")\n        func()\n        print(\"After\")\n",[18,5934,5935,5948,5956,5971,5977],{"__ignoreMap":41},[45,5936,5937,5939,5942,5944,5946],{"class":47,"line":48},[45,5938,2757],{"class":2440},[45,5940,5941],{"class":2455}," bad_decorator",[45,5943,96],{"class":85},[45,5945,4628],{"class":3082},[45,5947,2464],{"class":85},[45,5949,5950,5952,5954],{"class":47,"line":63},[45,5951,2452],{"class":2440},[45,5953,4637],{"class":2455},[45,5955,3828],{"class":85},[45,5957,5958,5960,5962,5964,5967,5969],{"class":47,"line":70},[45,5959,1923],{"class":92},[45,5961,96],{"class":85},[45,5963,100],{"class":99},[45,5965,5966],{"class":103},"Before",[45,5968,100],{"class":99},[45,5970,109],{"class":85},[45,5972,5973,5975],{"class":47,"line":89},[45,5974,4659],{"class":2070},[45,5976,2495],{"class":85},[45,5978,5979,5981,5983,5985,5988,5990],{"class":47,"line":112},[45,5980,1923],{"class":92},[45,5982,96],{"class":85},[45,5984,100],{"class":99},[45,5986,5987],{"class":103},"After",[45,5989,100],{"class":99},[45,5991,109],{"class":85},[14,5993,5994,5995,5998,5999,240],{},"This decorator returns ",[18,5996,5997],{},"None"," because it never returns ",[18,6000,4959],{},[14,6002,6003],{},"Correct version:",[36,6005,6007],{"className":38,"code":6006,"language":40,"meta":41,"style":41},"def good_decorator(func):\n    def wrapper():\n        print(\"Before\")\n        func()\n        print(\"After\")\n    return wrapper\n",[18,6008,6009,6022,6030,6044,6050,6064],{"__ignoreMap":41},[45,6010,6011,6013,6016,6018,6020],{"class":47,"line":48},[45,6012,2757],{"class":2440},[45,6014,6015],{"class":2455}," good_decorator",[45,6017,96],{"class":85},[45,6019,4628],{"class":3082},[45,6021,2464],{"class":85},[45,6023,6024,6026,6028],{"class":47,"line":63},[45,6025,2452],{"class":2440},[45,6027,4637],{"class":2455},[45,6029,3828],{"class":85},[45,6031,6032,6034,6036,6038,6040,6042],{"class":47,"line":70},[45,6033,1923],{"class":92},[45,6035,96],{"class":85},[45,6037,100],{"class":99},[45,6039,5966],{"class":103},[45,6041,100],{"class":99},[45,6043,109],{"class":85},[45,6045,6046,6048],{"class":47,"line":89},[45,6047,4659],{"class":2070},[45,6049,2495],{"class":85},[45,6051,6052,6054,6056,6058,6060,6062],{"class":47,"line":112},[45,6053,1923],{"class":92},[45,6055,96],{"class":85},[45,6057,100],{"class":99},[45,6059,5987],{"class":103},[45,6061,100],{"class":99},[45,6063,109],{"class":85},[45,6065,6066,6068],{"class":47,"line":126},[45,6067,4681],{"class":73},[45,6069,4684],{"class":51},[712,6071,6073],{"id":6072},"example-forgetting-to-call-the-original-function","Example: forgetting to call the original function",[14,6075,6076],{},"This code does not run the original function:",[36,6078,6080],{"className":38,"code":6079,"language":40,"meta":41,"style":41},"def bad_decorator(func):\n    def wrapper():\n        print(\"Before\")\n        print(\"After\")\n    return wrapper\n",[18,6081,6082,6094,6102,6116,6130],{"__ignoreMap":41},[45,6083,6084,6086,6088,6090,6092],{"class":47,"line":48},[45,6085,2757],{"class":2440},[45,6087,5941],{"class":2455},[45,6089,96],{"class":85},[45,6091,4628],{"class":3082},[45,6093,2464],{"class":85},[45,6095,6096,6098,6100],{"class":47,"line":63},[45,6097,2452],{"class":2440},[45,6099,4637],{"class":2455},[45,6101,3828],{"class":85},[45,6103,6104,6106,6108,6110,6112,6114],{"class":47,"line":70},[45,6105,1923],{"class":92},[45,6107,96],{"class":85},[45,6109,100],{"class":99},[45,6111,5966],{"class":103},[45,6113,100],{"class":99},[45,6115,109],{"class":85},[45,6117,6118,6120,6122,6124,6126,6128],{"class":47,"line":89},[45,6119,1923],{"class":92},[45,6121,96],{"class":85},[45,6123,100],{"class":99},[45,6125,5987],{"class":103},[45,6127,100],{"class":99},[45,6129,109],{"class":85},[45,6131,6132,6134],{"class":47,"line":112},[45,6133,4681],{"class":73},[45,6135,4684],{"class":51},[14,6137,6003],{},[36,6139,6140],{"className":38,"code":6006,"language":40,"meta":41,"style":41},[18,6141,6142,6154,6162,6176,6182,6196],{"__ignoreMap":41},[45,6143,6144,6146,6148,6150,6152],{"class":47,"line":48},[45,6145,2757],{"class":2440},[45,6147,6015],{"class":2455},[45,6149,96],{"class":85},[45,6151,4628],{"class":3082},[45,6153,2464],{"class":85},[45,6155,6156,6158,6160],{"class":47,"line":63},[45,6157,2452],{"class":2440},[45,6159,4637],{"class":2455},[45,6161,3828],{"class":85},[45,6163,6164,6166,6168,6170,6172,6174],{"class":47,"line":70},[45,6165,1923],{"class":92},[45,6167,96],{"class":85},[45,6169,100],{"class":99},[45,6171,5966],{"class":103},[45,6173,100],{"class":99},[45,6175,109],{"class":85},[45,6177,6178,6180],{"class":47,"line":89},[45,6179,4659],{"class":2070},[45,6181,2495],{"class":85},[45,6183,6184,6186,6188,6190,6192,6194],{"class":47,"line":112},[45,6185,1923],{"class":92},[45,6187,96],{"class":85},[45,6189,100],{"class":99},[45,6191,5987],{"class":103},[45,6193,100],{"class":99},[45,6195,109],{"class":85},[45,6197,6198,6200],{"class":47,"line":126},[45,6199,4681],{"class":73},[45,6201,4684],{"class":51},[712,6203,6205],{"id":6204},"simple-debugging-checks","Simple debugging checks",[14,6207,6208],{},"If you are experimenting and something feels wrong, these can help:",[36,6210,6212],{"className":38,"code":6211,"language":40,"meta":41,"style":41},"print(my_function)\nprint(type(my_function))\nhelp(my_function)\nprint(result)\n",[18,6213,6214,6224,6238,6248],{"__ignoreMap":41},[45,6215,6216,6218,6220,6222],{"class":47,"line":48},[45,6217,2065],{"class":92},[45,6219,96],{"class":85},[45,6221,5275],{"class":2070},[45,6223,109],{"class":85},[45,6225,6226,6228,6230,6232,6234,6236],{"class":47,"line":63},[45,6227,2065],{"class":92},[45,6229,96],{"class":85},[45,6231,4340],{"class":2109},[45,6233,96],{"class":85},[45,6235,5275],{"class":2070},[45,6237,4348],{"class":85},[45,6239,6240,6242,6244,6246],{"class":47,"line":70},[45,6241,2104],{"class":92},[45,6243,96],{"class":85},[45,6245,5275],{"class":2070},[45,6247,109],{"class":85},[45,6249,6250,6252,6254,6256],{"class":47,"line":89},[45,6251,2065],{"class":92},[45,6253,96],{"class":85},[45,6255,3620],{"class":2070},[45,6257,109],{"class":85},[14,6259,6260],{},"These are basic checks that help you see what your function variable now refers to and what value was returned.",[31,6262,2281],{"id":2280},[712,6264,6266],{"id":6265},"what-is-a-decorator-in-python-in-simple-terms","What is a decorator in Python in simple terms?",[14,6268,6269],{},"It is a function that changes or extends another function without rewriting that function.",[712,6271,6273],{"id":6272},"do-i-have-to-use-the-symbol","Do I have to use the @ symbol?",[14,6275,6276,6277,6279],{},"No. You can call the decorator manually. The ",[18,6278,4572],{}," syntax is just a shorter and cleaner form.",[712,6281,6283],{"id":6282},"should-beginners-use-decorators-right-away","Should beginners use decorators right away?",[14,6285,6286],{},"Usually no. First understand functions, arguments, and nested functions. Then decorators become much easier.",[712,6288,6290],{"id":6289},"why-does-a-decorator-return-another-function","Why does a decorator return another function?",[14,6292,6293],{},"Because the new returned function is the wrapped version that adds extra behavior.",[712,6295,6297],{"id":6296},"why-does-my-decorator-fail-when-the-function-has-arguments","Why does my decorator fail when the function has arguments?",[14,6299,6300,6301,1316,6303,6305],{},"Your wrapper may not accept parameters. Use ",[18,6302,5570],{},[18,6304,5573],{}," to forward arguments to the original function.",[31,6307,2342],{"id":2341},[195,6309,6310,6314,6319,6324,6329,6334,6339],{},[198,6311,6312],{},[236,6313,5879],{"href":4579},[198,6315,6316],{},[236,6317,6318],{"href":4583},"Function parameters and arguments in Python",[198,6320,6321],{},[236,6322,6323],{"href":5884},"Return values in Python functions",[198,6325,6326],{},[236,6327,6328],{"href":5891},"How to create a simple function in Python",[198,6330,6331],{},[236,6332,6333],{"href":5820},"What is a function in Python?",[198,6335,6336],{},[236,6337,6338],{"href":5786},"What is a parameter in Python?",[198,6340,6341],{},[236,6342,6343],{"href":5790},"What is an argument in Python?",[2367,6345,6346],{},"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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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 .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 .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 .stp6e, html code.shiki .stp6e{--shiki-light:#39ADB5;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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":41,"searchDepth":63,"depth":63,"links":6348},[6349,6350,6351,6352,6356,6357,6361,6368,6369,6370,6371,6376,6383],{"id":4716,"depth":63,"text":4717},{"id":4738,"depth":63,"text":4739},{"id":4771,"depth":63,"text":4772},{"id":4801,"depth":63,"text":4802,"children":6353},[6354,6355],{"id":4927,"depth":70,"text":4928},{"id":4993,"depth":70,"text":4994},{"id":5022,"depth":63,"text":5023},{"id":5335,"depth":63,"text":5336,"children":6358},[6359,6360],{"id":5451,"depth":70,"text":5452},{"id":5461,"depth":70,"text":5462},{"id":5512,"depth":63,"text":5513,"children":6362},[6363,6365,6366],{"id":5566,"depth":70,"text":6364},"Better version with *args and **kwargs",{"id":5744,"depth":70,"text":5452},{"id":5753,"depth":70,"text":6367},"What *args and **kwargs mean",{"id":5794,"depth":63,"text":5795},{"id":5824,"depth":63,"text":5825},{"id":5865,"depth":63,"text":5866},{"id":5902,"depth":63,"text":5903,"children":6372},[6373,6374,6375],{"id":5926,"depth":70,"text":5927},{"id":6072,"depth":70,"text":6073},{"id":6204,"depth":70,"text":6205},{"id":2280,"depth":63,"text":2281,"children":6377},[6378,6379,6380,6381,6382],{"id":6265,"depth":70,"text":6266},{"id":6272,"depth":70,"text":6273},{"id":6282,"depth":70,"text":6283},{"id":6289,"depth":70,"text":6290},{"id":6296,"depth":70,"text":6297},{"id":2341,"depth":63,"text":2342},"Master decorators in python beginner introduction in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fdecorators-in-python-beginner-introduction",{"title":4554,"description":6384},"learn\u002Fdecorators-in-python-beginner-introduction","wBxhqAJYBOIlyg2Wqm5xQrDpS_pDhn7JuXca0afIOgo",{"id":6391,"title":6392,"body":6393,"description":8531,"extension":2399,"meta":8532,"navigation":66,"path":8533,"seo":8534,"stem":8535,"__hash__":8536},"content\u002Flearn\u002Fdefault-and-keyword-arguments-explained.md","Default and Keyword Arguments Explained",{"type":7,"value":6394,"toc":8497},[6395,6398,6401,6404,6415,6422,6424,6546,6548,6568,6571,6597,6601,6615,6619,6624,6627,6633,6635,6678,6681,6702,6706,6789,6791,6804,6807,6826,6829,6833,6839,6841,6918,6920,6928,6931,6946,6949,6960,6964,6968,6973,7049,7051,7066,7069,7086,7090,7095,7172,7174,7186,7189,7192,7196,7199,7315,7317,7333,7336,7364,7367,7371,7375,7378,7420,7423,7466,7469,7473,7476,7490,7494,7497,7570,7573,7626,7629,7633,7635,7687,7692,7706,7712,7716,7729,7733,7785,7788,7799,7803,7853,7855,7872,7875,7907,7911,7963,7965,7978,7980,8004,8008,8058,8060,8068,8072,8078,8089,8091,8110,8114,8125,8127,8215,8220,8228,8237,8239,8242,8259,8262,8332,8335,8361,8363,8367,8370,8374,8377,8381,8384,8386,8451,8455,8458,8462,8465,8467,8494],[10,6396,6392],{"id":6397},"default-and-keyword-arguments-explained",[14,6399,6400],{},"Default arguments and keyword arguments make Python functions easier to call.",[14,6402,6403],{},"They help you:",[195,6405,6406,6409,6412],{},[198,6407,6408],{},"make some function inputs optional",[198,6410,6411],{},"write clearer function calls",[198,6413,6414],{},"avoid passing every value every time",[14,6416,6417,6418,230,6420,240],{},"This page explains what they are, how they work, and the main rules beginners should remember. If you are new to functions, start with ",[236,6419,5879],{"href":4579},[236,6421,5782],{"href":4583},[31,6423,34],{"id":33},[36,6425,6427],{"className":38,"code":6426,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\ngreet(\"Sam\")\ngreet(\"Sam\", message=\"Hi\")\ngreet(name=\"Sam\", message=\"Welcome\")\n",[18,6428,6429,6454,6469,6473,6487,6515],{"__ignoreMap":41},[45,6430,6431,6433,6435,6437,6439,6441,6444,6446,6448,6450,6452],{"class":47,"line":48},[45,6432,2757],{"class":2440},[45,6434,2936],{"class":2455},[45,6436,96],{"class":85},[45,6438,3096],{"class":3082},[45,6440,3079],{"class":85},[45,6442,6443],{"class":3082}," message",[45,6445,56],{"class":55},[45,6447,100],{"class":99},[45,6449,3226],{"class":103},[45,6451,100],{"class":99},[45,6453,2464],{"class":85},[45,6455,6456,6458,6460,6463,6465,6467],{"class":47,"line":63},[45,6457,93],{"class":92},[45,6459,96],{"class":85},[45,6461,6462],{"class":2070},"message",[45,6464,3079],{"class":85},[45,6466,3083],{"class":2070},[45,6468,109],{"class":85},[45,6470,6471],{"class":47,"line":70},[45,6472,67],{"emptyLinePlaceholder":66},[45,6474,6475,6477,6479,6481,6483,6485],{"class":47,"line":89},[45,6476,2982],{"class":2070},[45,6478,96],{"class":85},[45,6480,100],{"class":99},[45,6482,1568],{"class":103},[45,6484,100],{"class":99},[45,6486,109],{"class":85},[45,6488,6489,6491,6493,6495,6497,6499,6501,6504,6506,6508,6511,6513],{"class":47,"line":112},[45,6490,2982],{"class":2070},[45,6492,96],{"class":85},[45,6494,100],{"class":99},[45,6496,1568],{"class":103},[45,6498,100],{"class":99},[45,6500,3079],{"class":85},[45,6502,6443],{"class":6503},"s99_P",[45,6505,56],{"class":55},[45,6507,100],{"class":99},[45,6509,6510],{"class":103},"Hi",[45,6512,100],{"class":99},[45,6514,109],{"class":85},[45,6516,6517,6519,6521,6523,6525,6527,6529,6531,6533,6535,6537,6539,6542,6544],{"class":47,"line":126},[45,6518,2982],{"class":2070},[45,6520,96],{"class":85},[45,6522,3096],{"class":6503},[45,6524,56],{"class":55},[45,6526,100],{"class":99},[45,6528,1568],{"class":103},[45,6530,100],{"class":99},[45,6532,3079],{"class":85},[45,6534,6443],{"class":6503},[45,6536,56],{"class":55},[45,6538,100],{"class":99},[45,6540,6541],{"class":103},"Welcome",[45,6543,100],{"class":99},[45,6545,109],{"class":85},[14,6547,177],{},[36,6549,6551],{"className":38,"code":6550,"language":40,"meta":41,"style":41},"Hello Sam\nHi Sam\nWelcome Sam\n",[18,6552,6553,6558,6563],{"__ignoreMap":41},[45,6554,6555],{"class":47,"line":48},[45,6556,6557],{"class":51},"Hello Sam\n",[45,6559,6560],{"class":47,"line":63},[45,6561,6562],{"class":51},"Hi Sam\n",[45,6564,6565],{"class":47,"line":70},[45,6566,6567],{"class":51},"Welcome Sam\n",[14,6569,6570],{},"In this example:",[195,6572,6573,6582,6594],{},[198,6574,6575,6578,6579],{},[18,6576,6577],{},"message=\"Hello\""," is a ",[2421,6580,6581],{},"default argument",[198,6583,6584,1316,6587,6590,6591],{},[18,6585,6586],{},"message=\"Hi\"",[18,6588,6589],{},"name=\"Sam\""," in the function call are ",[2421,6592,6593],{},"keyword arguments",[198,6595,6596],{},"all three function calls are valid",[31,6598,6600],{"id":6599},"what-this-page-teaches","What this page teaches",[195,6602,6603,6606,6609,6612],{},[198,6604,6605],{},"What default arguments mean",[198,6607,6608],{},"What keyword arguments mean",[198,6610,6611],{},"How they make functions easier to call",[198,6613,6614],{},"How to avoid common mistakes when calling functions",[31,6616,6618],{"id":6617},"what-is-a-default-argument","What is a default argument?",[14,6620,2419,6621,6623],{},[2421,6622,6581],{}," is a parameter that already has a value.",[14,6625,6626],{},"If you do not pass that argument when calling the function, Python uses the default value instead.",[14,6628,6629,6630,240],{},"This makes the parameter ",[2421,6631,6632],{},"optional",[14,6634,466],{},[36,6636,6638],{"className":38,"code":6637,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n",[18,6639,6640,6664],{"__ignoreMap":41},[45,6641,6642,6644,6646,6648,6650,6652,6654,6656,6658,6660,6662],{"class":47,"line":48},[45,6643,2757],{"class":2440},[45,6645,2936],{"class":2455},[45,6647,96],{"class":85},[45,6649,3096],{"class":3082},[45,6651,3079],{"class":85},[45,6653,6443],{"class":3082},[45,6655,56],{"class":55},[45,6657,100],{"class":99},[45,6659,3226],{"class":103},[45,6661,100],{"class":99},[45,6663,2464],{"class":85},[45,6665,6666,6668,6670,6672,6674,6676],{"class":47,"line":63},[45,6667,93],{"class":92},[45,6669,96],{"class":85},[45,6671,6462],{"class":2070},[45,6673,3079],{"class":85},[45,6675,3083],{"class":2070},[45,6677,109],{"class":85},[14,6679,6680],{},"Here:",[195,6682,6683,6688,6693],{},[198,6684,6685,6687],{},[18,6686,3096],{}," is required",[198,6689,6690,6692],{},[18,6691,6462],{}," is optional",[198,6694,6695,6696,6698,6699],{},"if no ",[18,6697,6462],{}," is given, Python uses ",[18,6700,6701],{},"\"Hello\"",[31,6703,6705],{"id":6704},"simple-default-argument-example","Simple default argument example",[36,6707,6709],{"className":38,"code":6708,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\ngreet(\"Sam\")\ngreet(\"Sam\", \"Hi\")\n",[18,6710,6711,6735,6749,6753,6767],{"__ignoreMap":41},[45,6712,6713,6715,6717,6719,6721,6723,6725,6727,6729,6731,6733],{"class":47,"line":48},[45,6714,2757],{"class":2440},[45,6716,2936],{"class":2455},[45,6718,96],{"class":85},[45,6720,3096],{"class":3082},[45,6722,3079],{"class":85},[45,6724,6443],{"class":3082},[45,6726,56],{"class":55},[45,6728,100],{"class":99},[45,6730,3226],{"class":103},[45,6732,100],{"class":99},[45,6734,2464],{"class":85},[45,6736,6737,6739,6741,6743,6745,6747],{"class":47,"line":63},[45,6738,93],{"class":92},[45,6740,96],{"class":85},[45,6742,6462],{"class":2070},[45,6744,3079],{"class":85},[45,6746,3083],{"class":2070},[45,6748,109],{"class":85},[45,6750,6751],{"class":47,"line":70},[45,6752,67],{"emptyLinePlaceholder":66},[45,6754,6755,6757,6759,6761,6763,6765],{"class":47,"line":89},[45,6756,2982],{"class":2070},[45,6758,96],{"class":85},[45,6760,100],{"class":99},[45,6762,1568],{"class":103},[45,6764,100],{"class":99},[45,6766,109],{"class":85},[45,6768,6769,6771,6773,6775,6777,6779,6781,6783,6785,6787],{"class":47,"line":112},[45,6770,2982],{"class":2070},[45,6772,96],{"class":85},[45,6774,100],{"class":99},[45,6776,1568],{"class":103},[45,6778,100],{"class":99},[45,6780,3079],{"class":85},[45,6782,1565],{"class":99},[45,6784,6510],{"class":103},[45,6786,100],{"class":99},[45,6788,109],{"class":85},[14,6790,177],{},[36,6792,6794],{"className":38,"code":6793,"language":40,"meta":41,"style":41},"Hello Sam\nHi Sam\n",[18,6795,6796,6800],{"__ignoreMap":41},[45,6797,6798],{"class":47,"line":48},[45,6799,6557],{"class":51},[45,6801,6802],{"class":47,"line":63},[45,6803,6562],{"class":51},[14,6805,6806],{},"What happens here:",[195,6808,6809,6817],{},[198,6810,6811,6814,6815],{},[18,6812,6813],{},"greet(\"Sam\")"," uses the default value ",[18,6816,6701],{},[198,6818,6819,6822,6823],{},[18,6820,6821],{},"greet(\"Sam\", \"Hi\")"," replaces the default with ",[18,6824,6825],{},"\"Hi\"",[14,6827,6828],{},"Default arguments are useful when a function usually uses the same value, but sometimes needs a different one.",[31,6830,6832],{"id":6831},"what-is-a-keyword-argument","What is a keyword argument?",[14,6834,2419,6835,6838],{},[2421,6836,6837],{},"keyword argument"," is an argument passed using the parameter name.",[14,6840,466],{},[36,6842,6844],{"className":38,"code":6843,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\ngreet(name=\"Sam\", message=\"Hi\")\n",[18,6845,6846,6870,6884,6888],{"__ignoreMap":41},[45,6847,6848,6850,6852,6854,6856,6858,6860,6862,6864,6866,6868],{"class":47,"line":48},[45,6849,2757],{"class":2440},[45,6851,2936],{"class":2455},[45,6853,96],{"class":85},[45,6855,3096],{"class":3082},[45,6857,3079],{"class":85},[45,6859,6443],{"class":3082},[45,6861,56],{"class":55},[45,6863,100],{"class":99},[45,6865,3226],{"class":103},[45,6867,100],{"class":99},[45,6869,2464],{"class":85},[45,6871,6872,6874,6876,6878,6880,6882],{"class":47,"line":63},[45,6873,93],{"class":92},[45,6875,96],{"class":85},[45,6877,6462],{"class":2070},[45,6879,3079],{"class":85},[45,6881,3083],{"class":2070},[45,6883,109],{"class":85},[45,6885,6886],{"class":47,"line":70},[45,6887,67],{"emptyLinePlaceholder":66},[45,6889,6890,6892,6894,6896,6898,6900,6902,6904,6906,6908,6910,6912,6914,6916],{"class":47,"line":89},[45,6891,2982],{"class":2070},[45,6893,96],{"class":85},[45,6895,3096],{"class":6503},[45,6897,56],{"class":55},[45,6899,100],{"class":99},[45,6901,1568],{"class":103},[45,6903,100],{"class":99},[45,6905,3079],{"class":85},[45,6907,6443],{"class":6503},[45,6909,56],{"class":55},[45,6911,100],{"class":99},[45,6913,6510],{"class":103},[45,6915,100],{"class":99},[45,6917,109],{"class":85},[14,6919,177],{},[36,6921,6922],{"className":38,"code":6562,"language":40,"meta":41,"style":41},[18,6923,6924],{"__ignoreMap":41},[45,6925,6926],{"class":47,"line":48},[45,6927,6562],{"class":51},[14,6929,6930],{},"Here, Python matches values by parameter name:",[195,6932,6933,6940],{},[198,6934,6935,6937,6938],{},[18,6936,6589],{}," goes to ",[18,6939,3096],{},[198,6941,6942,6937,6944],{},[18,6943,6586],{},[18,6945,6462],{},[14,6947,6948],{},"Keyword arguments are helpful because they:",[195,6950,6951,6954,6957],{},[198,6952,6953],{},"make code easier to read",[198,6955,6956],{},"show clearly what each value means",[198,6958,6959],{},"help when a function has several parameters",[31,6961,6963],{"id":6962},"positional-arguments-vs-keyword-arguments","Positional arguments vs keyword arguments",[712,6965,6967],{"id":6966},"positional-arguments","Positional arguments",[14,6969,6970,6972],{},[2421,6971,6967],{}," are matched by order.",[36,6974,6976],{"className":38,"code":6975,"language":40,"meta":41,"style":41},"def describe_pet(animal, name):\n    print(name, \"is a\", animal)\n\ndescribe_pet(\"cat\", \"Luna\")\n",[18,6977,6978,6996,7020,7024],{"__ignoreMap":41},[45,6979,6980,6982,6985,6987,6990,6992,6994],{"class":47,"line":48},[45,6981,2757],{"class":2440},[45,6983,6984],{"class":2455}," describe_pet",[45,6986,96],{"class":85},[45,6988,6989],{"class":3082},"animal",[45,6991,3079],{"class":85},[45,6993,3083],{"class":3082},[45,6995,2464],{"class":85},[45,6997,6998,7000,7002,7004,7006,7008,7011,7013,7015,7018],{"class":47,"line":63},[45,6999,93],{"class":92},[45,7001,96],{"class":85},[45,7003,3096],{"class":2070},[45,7005,3079],{"class":85},[45,7007,1565],{"class":99},[45,7009,7010],{"class":103},"is a",[45,7012,100],{"class":99},[45,7014,3079],{"class":85},[45,7016,7017],{"class":2070}," animal",[45,7019,109],{"class":85},[45,7021,7022],{"class":47,"line":70},[45,7023,67],{"emptyLinePlaceholder":66},[45,7025,7026,7029,7031,7033,7036,7038,7040,7042,7045,7047],{"class":47,"line":89},[45,7027,7028],{"class":2070},"describe_pet",[45,7030,96],{"class":85},[45,7032,100],{"class":99},[45,7034,7035],{"class":103},"cat",[45,7037,100],{"class":99},[45,7039,3079],{"class":85},[45,7041,1565],{"class":99},[45,7043,7044],{"class":103},"Luna",[45,7046,100],{"class":99},[45,7048,109],{"class":85},[14,7050,177],{},[36,7052,7054],{"className":38,"code":7053,"language":40,"meta":41,"style":41},"Luna is a cat\n",[18,7055,7056],{"__ignoreMap":41},[45,7057,7058,7061,7063],{"class":47,"line":48},[45,7059,7060],{"class":51},"Luna ",[45,7062,1285],{"class":55},[45,7064,7065],{"class":51}," a cat\n",[14,7067,7068],{},"Python matches:",[195,7070,7071,7079],{},[198,7072,7073,7076,7077],{},[18,7074,7075],{},"\"cat\""," to ",[18,7078,6989],{},[198,7080,7081,7076,7084],{},[18,7082,7083],{},"\"Luna\"",[18,7085,3096],{},[712,7087,7089],{"id":7088},"keyword-arguments","Keyword arguments",[14,7091,7092,7094],{},[2421,7093,7089],{}," are matched by parameter name.",[36,7096,7098],{"className":38,"code":7097,"language":40,"meta":41,"style":41},"def describe_pet(animal, name):\n    print(name, \"is a\", animal)\n\ndescribe_pet(name=\"Luna\", animal=\"cat\")\n",[18,7099,7100,7116,7138,7142],{"__ignoreMap":41},[45,7101,7102,7104,7106,7108,7110,7112,7114],{"class":47,"line":48},[45,7103,2757],{"class":2440},[45,7105,6984],{"class":2455},[45,7107,96],{"class":85},[45,7109,6989],{"class":3082},[45,7111,3079],{"class":85},[45,7113,3083],{"class":3082},[45,7115,2464],{"class":85},[45,7117,7118,7120,7122,7124,7126,7128,7130,7132,7134,7136],{"class":47,"line":63},[45,7119,93],{"class":92},[45,7121,96],{"class":85},[45,7123,3096],{"class":2070},[45,7125,3079],{"class":85},[45,7127,1565],{"class":99},[45,7129,7010],{"class":103},[45,7131,100],{"class":99},[45,7133,3079],{"class":85},[45,7135,7017],{"class":2070},[45,7137,109],{"class":85},[45,7139,7140],{"class":47,"line":70},[45,7141,67],{"emptyLinePlaceholder":66},[45,7143,7144,7146,7148,7150,7152,7154,7156,7158,7160,7162,7164,7166,7168,7170],{"class":47,"line":89},[45,7145,7028],{"class":2070},[45,7147,96],{"class":85},[45,7149,3096],{"class":6503},[45,7151,56],{"class":55},[45,7153,100],{"class":99},[45,7155,7044],{"class":103},[45,7157,100],{"class":99},[45,7159,3079],{"class":85},[45,7161,7017],{"class":6503},[45,7163,56],{"class":55},[45,7165,100],{"class":99},[45,7167,7035],{"class":103},[45,7169,100],{"class":99},[45,7171,109],{"class":85},[14,7173,177],{},[36,7175,7176],{"className":38,"code":7053,"language":40,"meta":41,"style":41},[18,7177,7178],{"__ignoreMap":41},[45,7179,7180,7182,7184],{"class":47,"line":48},[45,7181,7060],{"class":51},[45,7183,1285],{"class":55},[45,7185,7065],{"class":51},[14,7187,7188],{},"This time, the order does not matter because the names are included.",[14,7190,7191],{},"Beginners should learn both styles because many Python functions allow both.",[31,7193,7195],{"id":7194},"using-default-arguments-with-keyword-arguments","Using default arguments with keyword arguments",[14,7197,7198],{},"Default arguments and keyword arguments often work together.",[36,7200,7201],{"className":38,"code":6426,"language":40,"meta":41,"style":41},[18,7202,7203,7227,7241,7245,7259,7285],{"__ignoreMap":41},[45,7204,7205,7207,7209,7211,7213,7215,7217,7219,7221,7223,7225],{"class":47,"line":48},[45,7206,2757],{"class":2440},[45,7208,2936],{"class":2455},[45,7210,96],{"class":85},[45,7212,3096],{"class":3082},[45,7214,3079],{"class":85},[45,7216,6443],{"class":3082},[45,7218,56],{"class":55},[45,7220,100],{"class":99},[45,7222,3226],{"class":103},[45,7224,100],{"class":99},[45,7226,2464],{"class":85},[45,7228,7229,7231,7233,7235,7237,7239],{"class":47,"line":63},[45,7230,93],{"class":92},[45,7232,96],{"class":85},[45,7234,6462],{"class":2070},[45,7236,3079],{"class":85},[45,7238,3083],{"class":2070},[45,7240,109],{"class":85},[45,7242,7243],{"class":47,"line":70},[45,7244,67],{"emptyLinePlaceholder":66},[45,7246,7247,7249,7251,7253,7255,7257],{"class":47,"line":89},[45,7248,2982],{"class":2070},[45,7250,96],{"class":85},[45,7252,100],{"class":99},[45,7254,1568],{"class":103},[45,7256,100],{"class":99},[45,7258,109],{"class":85},[45,7260,7261,7263,7265,7267,7269,7271,7273,7275,7277,7279,7281,7283],{"class":47,"line":112},[45,7262,2982],{"class":2070},[45,7264,96],{"class":85},[45,7266,100],{"class":99},[45,7268,1568],{"class":103},[45,7270,100],{"class":99},[45,7272,3079],{"class":85},[45,7274,6443],{"class":6503},[45,7276,56],{"class":55},[45,7278,100],{"class":99},[45,7280,6510],{"class":103},[45,7282,100],{"class":99},[45,7284,109],{"class":85},[45,7286,7287,7289,7291,7293,7295,7297,7299,7301,7303,7305,7307,7309,7311,7313],{"class":47,"line":126},[45,7288,2982],{"class":2070},[45,7290,96],{"class":85},[45,7292,3096],{"class":6503},[45,7294,56],{"class":55},[45,7296,100],{"class":99},[45,7298,1568],{"class":103},[45,7300,100],{"class":99},[45,7302,3079],{"class":85},[45,7304,6443],{"class":6503},[45,7306,56],{"class":55},[45,7308,100],{"class":99},[45,7310,6541],{"class":103},[45,7312,100],{"class":99},[45,7314,109],{"class":85},[14,7316,177],{},[36,7318,7319],{"className":38,"code":6550,"language":40,"meta":41,"style":41},[18,7320,7321,7325,7329],{"__ignoreMap":41},[45,7322,7323],{"class":47,"line":48},[45,7324,6557],{"class":51},[45,7326,7327],{"class":47,"line":63},[45,7328,6562],{"class":51},[45,7330,7331],{"class":47,"line":70},[45,7332,6567],{"class":51},[14,7334,7335],{},"What each call does:",[195,7337,7338,7348,7356],{},[198,7339,7340,7342,7345,7346],{},[18,7341,6813],{},[7343,7344],"br",{},"\nUses the default value for ",[18,7347,6462],{},[198,7349,7350,7353,7355],{},[18,7351,7352],{},"greet(\"Sam\", message=\"Hi\")",[7343,7354],{},"\nOverrides the default value with a keyword argument",[198,7357,7358,7361,7363],{},[18,7359,7360],{},"greet(name=\"Sam\", message=\"Welcome\")",[7343,7362],{},"\nPasses both values by keyword",[14,7365,7366],{},"Using keywords is often clearer when you want to override an optional value.",[31,7368,7370],{"id":7369},"rules-beginners-should-remember","Rules beginners should remember",[712,7372,7374],{"id":7373},"_1-required-parameters-come-before-default-parameters","1. Required parameters come before default parameters",[14,7376,7377],{},"This is valid:",[36,7379,7380],{"className":38,"code":6637,"language":40,"meta":41,"style":41},[18,7381,7382,7406],{"__ignoreMap":41},[45,7383,7384,7386,7388,7390,7392,7394,7396,7398,7400,7402,7404],{"class":47,"line":48},[45,7385,2757],{"class":2440},[45,7387,2936],{"class":2455},[45,7389,96],{"class":85},[45,7391,3096],{"class":3082},[45,7393,3079],{"class":85},[45,7395,6443],{"class":3082},[45,7397,56],{"class":55},[45,7399,100],{"class":99},[45,7401,3226],{"class":103},[45,7403,100],{"class":99},[45,7405,2464],{"class":85},[45,7407,7408,7410,7412,7414,7416,7418],{"class":47,"line":63},[45,7409,93],{"class":92},[45,7411,96],{"class":85},[45,7413,6462],{"class":2070},[45,7415,3079],{"class":85},[45,7417,3083],{"class":2070},[45,7419,109],{"class":85},[14,7421,7422],{},"This is not valid:",[36,7424,7426],{"className":38,"code":7425,"language":40,"meta":41,"style":41},"def greet(message=\"Hello\", name):\n    print(message, name)\n",[18,7427,7428,7452],{"__ignoreMap":41},[45,7429,7430,7432,7434,7436,7438,7440,7442,7444,7446,7448,7450],{"class":47,"line":48},[45,7431,2757],{"class":2440},[45,7433,2936],{"class":2455},[45,7435,96],{"class":85},[45,7437,6462],{"class":3082},[45,7439,56],{"class":55},[45,7441,100],{"class":99},[45,7443,3226],{"class":103},[45,7445,100],{"class":99},[45,7447,3079],{"class":85},[45,7449,3083],{"class":3082},[45,7451,2464],{"class":85},[45,7453,7454,7456,7458,7460,7462,7464],{"class":47,"line":63},[45,7455,93],{"class":92},[45,7457,96],{"class":85},[45,7459,6462],{"class":2070},[45,7461,3079],{"class":85},[45,7463,3083],{"class":2070},[45,7465,109],{"class":85},[14,7467,7468],{},"Python will raise a syntax error because a required parameter cannot come after a default parameter.",[712,7470,7472],{"id":7471},"_2-you-cannot-place-a-non-default-parameter-after-a-default-parameter","2. You cannot place a non-default parameter after a default parameter",[14,7474,7475],{},"This is the same rule in another form:",[195,7477,7478,7484],{},[198,7479,7480,7481],{},"valid: ",[18,7482,7483],{},"def func(a, b=10):",[198,7485,7486,7487],{},"invalid: ",[18,7488,7489],{},"def func(a=10, b):",[712,7491,7493],{"id":7492},"_3-positional-arguments-must-come-before-keyword-arguments-in-a-call","3. Positional arguments must come before keyword arguments in a call",[14,7495,7496],{},"Valid:",[36,7498,7500],{"className":38,"code":7499,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\ngreet(\"Sam\", message=\"Hi\")\n",[18,7501,7502,7526,7540,7544],{"__ignoreMap":41},[45,7503,7504,7506,7508,7510,7512,7514,7516,7518,7520,7522,7524],{"class":47,"line":48},[45,7505,2757],{"class":2440},[45,7507,2936],{"class":2455},[45,7509,96],{"class":85},[45,7511,3096],{"class":3082},[45,7513,3079],{"class":85},[45,7515,6443],{"class":3082},[45,7517,56],{"class":55},[45,7519,100],{"class":99},[45,7521,3226],{"class":103},[45,7523,100],{"class":99},[45,7525,2464],{"class":85},[45,7527,7528,7530,7532,7534,7536,7538],{"class":47,"line":63},[45,7529,93],{"class":92},[45,7531,96],{"class":85},[45,7533,6462],{"class":2070},[45,7535,3079],{"class":85},[45,7537,3083],{"class":2070},[45,7539,109],{"class":85},[45,7541,7542],{"class":47,"line":70},[45,7543,67],{"emptyLinePlaceholder":66},[45,7545,7546,7548,7550,7552,7554,7556,7558,7560,7562,7564,7566,7568],{"class":47,"line":89},[45,7547,2982],{"class":2070},[45,7549,96],{"class":85},[45,7551,100],{"class":99},[45,7553,1568],{"class":103},[45,7555,100],{"class":99},[45,7557,3079],{"class":85},[45,7559,6443],{"class":6503},[45,7561,56],{"class":55},[45,7563,100],{"class":99},[45,7565,6510],{"class":103},[45,7567,100],{"class":99},[45,7569,109],{"class":85},[14,7571,7572],{},"Invalid:",[36,7574,7576],{"className":38,"code":7575,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\n# greet(name=\"Sam\", \"Hi\")\n",[18,7577,7578,7602,7616,7620],{"__ignoreMap":41},[45,7579,7580,7582,7584,7586,7588,7590,7592,7594,7596,7598,7600],{"class":47,"line":48},[45,7581,2757],{"class":2440},[45,7583,2936],{"class":2455},[45,7585,96],{"class":85},[45,7587,3096],{"class":3082},[45,7589,3079],{"class":85},[45,7591,6443],{"class":3082},[45,7593,56],{"class":55},[45,7595,100],{"class":99},[45,7597,3226],{"class":103},[45,7599,100],{"class":99},[45,7601,2464],{"class":85},[45,7603,7604,7606,7608,7610,7612,7614],{"class":47,"line":63},[45,7605,93],{"class":92},[45,7607,96],{"class":85},[45,7609,6462],{"class":2070},[45,7611,3079],{"class":85},[45,7613,3083],{"class":2070},[45,7615,109],{"class":85},[45,7617,7618],{"class":47,"line":70},[45,7619,67],{"emptyLinePlaceholder":66},[45,7621,7622],{"class":47,"line":89},[45,7623,7625],{"class":7624},"sutJx","# greet(name=\"Sam\", \"Hi\")\n",[14,7627,7628],{},"The invalid call is wrong because a positional argument comes after a keyword argument.",[712,7630,7632],{"id":7631},"_4-do-not-pass-the-same-argument-twice","4. Do not pass the same argument twice",[14,7634,7572],{},[36,7636,7638],{"className":38,"code":7637,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\n# greet(\"Sam\", name=\"Alex\")\n",[18,7639,7640,7664,7678,7682],{"__ignoreMap":41},[45,7641,7642,7644,7646,7648,7650,7652,7654,7656,7658,7660,7662],{"class":47,"line":48},[45,7643,2757],{"class":2440},[45,7645,2936],{"class":2455},[45,7647,96],{"class":85},[45,7649,3096],{"class":3082},[45,7651,3079],{"class":85},[45,7653,6443],{"class":3082},[45,7655,56],{"class":55},[45,7657,100],{"class":99},[45,7659,3226],{"class":103},[45,7661,100],{"class":99},[45,7663,2464],{"class":85},[45,7665,7666,7668,7670,7672,7674,7676],{"class":47,"line":63},[45,7667,93],{"class":92},[45,7669,96],{"class":85},[45,7671,6462],{"class":2070},[45,7673,3079],{"class":85},[45,7675,3083],{"class":2070},[45,7677,109],{"class":85},[45,7679,7680],{"class":47,"line":70},[45,7681,67],{"emptyLinePlaceholder":66},[45,7683,7684],{"class":47,"line":89},[45,7685,7686],{"class":7624},"# greet(\"Sam\", name=\"Alex\")\n",[14,7688,3444,7689,7691],{},[18,7690,3096],{}," gets a value twice:",[195,7693,7694,7700],{},[198,7695,7696,7699],{},[18,7697,7698],{},"\"Sam\""," by position",[198,7701,7702,7705],{},[18,7703,7704],{},"\"Alex\""," by keyword",[14,7707,7708,7709,240],{},"That causes a ",[18,7710,7711],{},"TypeError",[31,7713,7715],{"id":7714},"common-errors-and-confusing-cases","Common errors and confusing cases",[14,7717,7718,7719,7721,7722,1316,7725,240],{},"A lot of problems with arguments lead to ",[18,7720,7711],{},". If you want a deeper explanation, see ",[236,7723,4028],{"href":7724},"\u002Ferrors\u002Ftypeerror-missing-required-positional-argument-fix\u002F",[236,7726,7728],{"href":7727},"\u002Ferrors\u002Ftypeerror-unexpected-keyword-argument-fix\u002F","TypeError: unexpected keyword argument",[712,7730,7732],{"id":7731},"missing-a-required-argument","Missing a required argument",[36,7734,7736],{"className":38,"code":7735,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\n# greet()\n",[18,7737,7738,7762,7776,7780],{"__ignoreMap":41},[45,7739,7740,7742,7744,7746,7748,7750,7752,7754,7756,7758,7760],{"class":47,"line":48},[45,7741,2757],{"class":2440},[45,7743,2936],{"class":2455},[45,7745,96],{"class":85},[45,7747,3096],{"class":3082},[45,7749,3079],{"class":85},[45,7751,6443],{"class":3082},[45,7753,56],{"class":55},[45,7755,100],{"class":99},[45,7757,3226],{"class":103},[45,7759,100],{"class":99},[45,7761,2464],{"class":85},[45,7763,7764,7766,7768,7770,7772,7774],{"class":47,"line":63},[45,7765,93],{"class":92},[45,7767,96],{"class":85},[45,7769,6462],{"class":2070},[45,7771,3079],{"class":85},[45,7773,3083],{"class":2070},[45,7775,109],{"class":85},[45,7777,7778],{"class":47,"line":70},[45,7779,67],{"emptyLinePlaceholder":66},[45,7781,7782],{"class":47,"line":89},[45,7783,7784],{"class":7624},"# greet()\n",[14,7786,7787],{},"Why it fails:",[195,7789,7790,7794],{},[198,7791,7792,6687],{},[18,7793,3096],{},[198,7795,7796,7797],{},"no value was passed for ",[18,7798,3096],{},[712,7800,7802],{"id":7801},"passing-the-same-parameter-twice","Passing the same parameter twice",[36,7804,7805],{"className":38,"code":7637,"language":40,"meta":41,"style":41},[18,7806,7807,7831,7845,7849],{"__ignoreMap":41},[45,7808,7809,7811,7813,7815,7817,7819,7821,7823,7825,7827,7829],{"class":47,"line":48},[45,7810,2757],{"class":2440},[45,7812,2936],{"class":2455},[45,7814,96],{"class":85},[45,7816,3096],{"class":3082},[45,7818,3079],{"class":85},[45,7820,6443],{"class":3082},[45,7822,56],{"class":55},[45,7824,100],{"class":99},[45,7826,3226],{"class":103},[45,7828,100],{"class":99},[45,7830,2464],{"class":85},[45,7832,7833,7835,7837,7839,7841,7843],{"class":47,"line":63},[45,7834,93],{"class":92},[45,7836,96],{"class":85},[45,7838,6462],{"class":2070},[45,7840,3079],{"class":85},[45,7842,3083],{"class":2070},[45,7844,109],{"class":85},[45,7846,7847],{"class":47,"line":70},[45,7848,67],{"emptyLinePlaceholder":66},[45,7850,7851],{"class":47,"line":89},[45,7852,7686],{"class":7624},[14,7854,7787],{},[195,7856,7857,7865],{},[198,7858,7859,7861,7862,7864],{},[18,7860,3096],{}," already got ",[18,7863,7698],{}," as the first positional argument",[198,7866,7867,7868,7871],{},"then ",[18,7869,7870],{},"name=\"Alex\""," tries to assign it again",[14,7873,7874],{},"Python reports something like:",[36,7876,7878],{"className":38,"code":7877,"language":40,"meta":41,"style":41},"TypeError: greet() got multiple values for argument 'name'\n",[18,7879,7880],{"__ignoreMap":41},[45,7881,7882,7884,7886,7888,7890,7893,7896,7899,7902,7904],{"class":47,"line":48},[45,7883,7711],{"class":2109},[45,7885,457],{"class":85},[45,7887,2936],{"class":2070},[45,7889,3772],{"class":85},[45,7891,7892],{"class":51}," got multiple values ",[45,7894,7895],{"class":73},"for",[45,7897,7898],{"class":51}," argument ",[45,7900,7901],{"class":99},"'",[45,7903,3096],{"class":103},[45,7905,7906],{"class":99},"'\n",[712,7908,7910],{"id":7909},"using-an-unexpected-keyword-name","Using an unexpected keyword name",[36,7912,7914],{"className":38,"code":7913,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\n# greet(username=\"Sam\")\n",[18,7915,7916,7940,7954,7958],{"__ignoreMap":41},[45,7917,7918,7920,7922,7924,7926,7928,7930,7932,7934,7936,7938],{"class":47,"line":48},[45,7919,2757],{"class":2440},[45,7921,2936],{"class":2455},[45,7923,96],{"class":85},[45,7925,3096],{"class":3082},[45,7927,3079],{"class":85},[45,7929,6443],{"class":3082},[45,7931,56],{"class":55},[45,7933,100],{"class":99},[45,7935,3226],{"class":103},[45,7937,100],{"class":99},[45,7939,2464],{"class":85},[45,7941,7942,7944,7946,7948,7950,7952],{"class":47,"line":63},[45,7943,93],{"class":92},[45,7945,96],{"class":85},[45,7947,6462],{"class":2070},[45,7949,3079],{"class":85},[45,7951,3083],{"class":2070},[45,7953,109],{"class":85},[45,7955,7956],{"class":47,"line":70},[45,7957,67],{"emptyLinePlaceholder":66},[45,7959,7960],{"class":47,"line":89},[45,7961,7962],{"class":7624},"# greet(username=\"Sam\")\n",[14,7964,7787],{},[195,7966,7967,7972],{},[198,7968,7969,7970],{},"the function has ",[18,7971,3096],{},[198,7973,7974,7975],{},"it does not have ",[18,7976,7977],{},"username",[14,7979,7874],{},[36,7981,7983],{"className":38,"code":7982,"language":40,"meta":41,"style":41},"TypeError: greet() got an unexpected keyword argument 'username'\n",[18,7984,7985],{"__ignoreMap":41},[45,7986,7987,7989,7991,7993,7995,7998,8000,8002],{"class":47,"line":48},[45,7988,7711],{"class":2109},[45,7990,457],{"class":85},[45,7992,2936],{"class":2070},[45,7994,3772],{"class":85},[45,7996,7997],{"class":51}," got an unexpected keyword argument ",[45,7999,7901],{"class":99},[45,8001,7977],{"class":103},[45,8003,7906],{"class":99},[712,8005,8007],{"id":8006},"mixing-positional-and-keyword-arguments-in-the-wrong-order","Mixing positional and keyword arguments in the wrong order",[36,8009,8010],{"className":38,"code":7575,"language":40,"meta":41,"style":41},[18,8011,8012,8036,8050,8054],{"__ignoreMap":41},[45,8013,8014,8016,8018,8020,8022,8024,8026,8028,8030,8032,8034],{"class":47,"line":48},[45,8015,2757],{"class":2440},[45,8017,2936],{"class":2455},[45,8019,96],{"class":85},[45,8021,3096],{"class":3082},[45,8023,3079],{"class":85},[45,8025,6443],{"class":3082},[45,8027,56],{"class":55},[45,8029,100],{"class":99},[45,8031,3226],{"class":103},[45,8033,100],{"class":99},[45,8035,2464],{"class":85},[45,8037,8038,8040,8042,8044,8046,8048],{"class":47,"line":63},[45,8039,93],{"class":92},[45,8041,96],{"class":85},[45,8043,6462],{"class":2070},[45,8045,3079],{"class":85},[45,8047,3083],{"class":2070},[45,8049,109],{"class":85},[45,8051,8052],{"class":47,"line":70},[45,8053,67],{"emptyLinePlaceholder":66},[45,8055,8056],{"class":47,"line":89},[45,8057,7625],{"class":7624},[14,8059,7787],{},[195,8061,8062,8065],{},[198,8063,8064],{},"positional arguments must come first",[198,8066,8067],{},"keyword arguments come after them",[31,8069,8071],{"id":8070},"beginner-tip-when-to-use-each-style","Beginner tip: when to use each style",[14,8073,165,8074,8077],{},[2421,8075,8076],{},"positional arguments"," when:",[195,8079,8080,8083,8086],{},[198,8081,8082],{},"the function call is short",[198,8084,8085],{},"the meaning is obvious",[198,8087,8088],{},"the order is easy to remember",[14,8090,466],{},[36,8092,8094],{"className":38,"code":8093,"language":40,"meta":41,"style":41},"print(\"Hello\")\n",[18,8095,8096],{"__ignoreMap":41},[45,8097,8098,8100,8102,8104,8106,8108],{"class":47,"line":48},[45,8099,2065],{"class":92},[45,8101,96],{"class":85},[45,8103,100],{"class":99},[45,8105,3226],{"class":103},[45,8107,100],{"class":99},[45,8109,109],{"class":85},[14,8111,165,8112,8077],{},[2421,8113,6593],{},[195,8115,8116,8119,8122],{},[198,8117,8118],{},"the function has several parameters",[198,8120,8121],{},"the meaning of each value is not obvious",[198,8123,8124],{},"you want the call to be easier to read",[14,8126,466],{},[36,8128,8130],{"className":38,"code":8129,"language":40,"meta":41,"style":41},"def create_user(name, age, active=True):\n    print(name, age, active)\n\ncreate_user(name=\"Sam\", age=20, active=False)\n",[18,8131,8132,8158,8176,8180],{"__ignoreMap":41},[45,8133,8134,8136,8139,8141,8143,8145,8147,8149,8152,8154,8156],{"class":47,"line":48},[45,8135,2757],{"class":2440},[45,8137,8138],{"class":2455}," create_user",[45,8140,96],{"class":85},[45,8142,3096],{"class":3082},[45,8144,3079],{"class":85},[45,8146,3305],{"class":3082},[45,8148,3079],{"class":85},[45,8150,8151],{"class":3082}," active",[45,8153,56],{"class":55},[45,8155,229],{"class":481},[45,8157,2464],{"class":85},[45,8159,8160,8162,8164,8166,8168,8170,8172,8174],{"class":47,"line":63},[45,8161,93],{"class":92},[45,8163,96],{"class":85},[45,8165,3096],{"class":2070},[45,8167,3079],{"class":85},[45,8169,3305],{"class":2070},[45,8171,3079],{"class":85},[45,8173,8151],{"class":2070},[45,8175,109],{"class":85},[45,8177,8178],{"class":47,"line":70},[45,8179,67],{"emptyLinePlaceholder":66},[45,8181,8182,8185,8187,8189,8191,8193,8195,8197,8199,8201,8203,8205,8207,8209,8211,8213],{"class":47,"line":89},[45,8183,8184],{"class":2070},"create_user",[45,8186,96],{"class":85},[45,8188,3096],{"class":6503},[45,8190,56],{"class":55},[45,8192,100],{"class":99},[45,8194,1568],{"class":103},[45,8196,100],{"class":99},[45,8198,3079],{"class":85},[45,8200,3305],{"class":6503},[45,8202,56],{"class":55},[45,8204,3372],{"class":59},[45,8206,3079],{"class":85},[45,8208,8151],{"class":6503},[45,8210,56],{"class":55},[45,8212,233],{"class":481},[45,8214,109],{"class":85},[14,8216,165,8217,8077],{},[2421,8218,8219],{},"default values",[195,8221,8222,8225],{},[198,8223,8224],{},"a parameter usually has one common value",[198,8226,8227],{},"you want to make part of the function optional",[14,8229,8230,8231,1316,8235,240],{},"If you want to practice more function calls, see ",[236,8232,8234],{"href":8233},"\u002Fhow-to\u002Fhow-to-use-default-arguments-in-python\u002F","how to use default arguments in Python",[236,8236,5885],{"href":5884},[31,8238,5903],{"id":5902},[14,8240,8241],{},"Beginners often make these mistakes:",[195,8243,8244,8247,8250,8253,8256],{},[198,8245,8246],{},"forgetting that default values make parameters optional, not required",[198,8248,8249],{},"placing a required parameter after a parameter with a default value",[198,8251,8252],{},"using a keyword name that does not match the function definition",[198,8254,8255],{},"mixing positional and keyword arguments in the wrong order",[198,8257,8258],{},"passing one argument by position and again by keyword",[14,8260,8261],{},"If you are unsure how a function is defined, these tools can help:",[36,8263,8265],{"className":38,"code":8264,"language":40,"meta":41,"style":41},"help(function_name)\nprint(function_name.__defaults__)\nprint(function_name.__code__.co_varnames)\nprint(function_name.__code__.co_argcount)\n",[18,8266,8267,8278,8293,8313],{"__ignoreMap":41},[45,8268,8269,8271,8273,8276],{"class":47,"line":48},[45,8270,2104],{"class":92},[45,8272,96],{"class":85},[45,8274,8275],{"class":2070},"function_name",[45,8277,109],{"class":85},[45,8279,8280,8282,8284,8286,8288,8291],{"class":47,"line":63},[45,8281,2065],{"class":92},[45,8283,96],{"class":85},[45,8285,8275],{"class":2070},[45,8287,240],{"class":85},[45,8289,8290],{"class":2771},"__defaults__",[45,8292,109],{"class":85},[45,8294,8295,8297,8299,8301,8303,8306,8308,8311],{"class":47,"line":70},[45,8296,2065],{"class":92},[45,8298,96],{"class":85},[45,8300,8275],{"class":2070},[45,8302,240],{"class":85},[45,8304,8305],{"class":2771},"__code__",[45,8307,240],{"class":85},[45,8309,8310],{"class":3095},"co_varnames",[45,8312,109],{"class":85},[45,8314,8315,8317,8319,8321,8323,8325,8327,8330],{"class":47,"line":89},[45,8316,2065],{"class":92},[45,8318,96],{"class":85},[45,8320,8275],{"class":2070},[45,8322,240],{"class":85},[45,8324,8305],{"class":2771},[45,8326,240],{"class":85},[45,8328,8329],{"class":3095},"co_argcount",[45,8331,109],{"class":85},[14,8333,8334],{},"What they show:",[195,8336,8337,8343,8349,8355],{},[198,8338,8339,8342],{},[18,8340,8341],{},"help(function_name)"," shows documentation if available",[198,8344,8345,8348],{},[18,8346,8347],{},"function_name.__defaults__"," shows default values",[198,8350,8351,8354],{},[18,8352,8353],{},"function_name.__code__.co_varnames"," shows parameter and local variable names",[198,8356,8357,8360],{},[18,8358,8359],{},"function_name.__code__.co_argcount"," shows how many positional parameters the function has",[31,8362,2281],{"id":2280},[712,8364,8366],{"id":8365},"what-is-the-difference-between-default-and-keyword-arguments","What is the difference between default and keyword arguments?",[14,8368,8369],{},"A default argument gives a parameter a fallback value. A keyword argument is a way to pass a value using the parameter name.",[712,8371,8373],{"id":8372},"are-keyword-arguments-required-when-a-function-has-default-values","Are keyword arguments required when a function has default values?",[14,8375,8376],{},"No. You can still call the function positionally if the order is correct.",[712,8378,8380],{"id":8379},"can-i-skip-one-positional-argument-and-only-pass-a-later-one","Can I skip one positional argument and only pass a later one?",[14,8382,8383],{},"Not with positional arguments. Use a keyword argument to target a later parameter.",[14,8385,466],{},[36,8387,8389],{"className":38,"code":8388,"language":40,"meta":41,"style":41},"def greet(name, message=\"Hello\"):\n    print(message, name)\n\ngreet(name=\"Sam\")\n",[18,8390,8391,8415,8429,8433],{"__ignoreMap":41},[45,8392,8393,8395,8397,8399,8401,8403,8405,8407,8409,8411,8413],{"class":47,"line":48},[45,8394,2757],{"class":2440},[45,8396,2936],{"class":2455},[45,8398,96],{"class":85},[45,8400,3096],{"class":3082},[45,8402,3079],{"class":85},[45,8404,6443],{"class":3082},[45,8406,56],{"class":55},[45,8408,100],{"class":99},[45,8410,3226],{"class":103},[45,8412,100],{"class":99},[45,8414,2464],{"class":85},[45,8416,8417,8419,8421,8423,8425,8427],{"class":47,"line":63},[45,8418,93],{"class":92},[45,8420,96],{"class":85},[45,8422,6462],{"class":2070},[45,8424,3079],{"class":85},[45,8426,3083],{"class":2070},[45,8428,109],{"class":85},[45,8430,8431],{"class":47,"line":70},[45,8432,67],{"emptyLinePlaceholder":66},[45,8434,8435,8437,8439,8441,8443,8445,8447,8449],{"class":47,"line":89},[45,8436,2982],{"class":2070},[45,8438,96],{"class":85},[45,8440,3096],{"class":6503},[45,8442,56],{"class":55},[45,8444,100],{"class":99},[45,8446,1568],{"class":103},[45,8448,100],{"class":99},[45,8450,109],{"class":85},[712,8452,8454],{"id":8453},"why-does-python-say-got-multiple-values-for-argument","Why does Python say got multiple values for argument?",[14,8456,8457],{},"This usually means the same parameter was passed once by position and again by keyword.",[712,8459,8461],{"id":8460},"why-must-default-parameters-come-after-required-ones","Why must default parameters come after required ones?",[14,8463,8464],{},"Because Python matches positional arguments from left to right. Putting defaults first would make calls ambiguous.",[31,8466,2342],{"id":2341},[195,8468,8469,8473,8477,8481,8486,8490],{},[198,8470,8471],{},[236,8472,5879],{"href":4579},[198,8474,8475],{},[236,8476,6318],{"href":4583},[198,8478,8479],{},[236,8480,6323],{"href":5884},[198,8482,8483],{},[236,8484,8485],{"href":8233},"How to use default arguments in Python",[198,8487,8488],{},[236,8489,4028],{"href":7724},[198,8491,8492],{},[236,8493,7728],{"href":7727},[2367,8495,8496],{},"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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":41,"searchDepth":63,"depth":63,"links":8498},[8499,8500,8501,8502,8503,8504,8508,8509,8515,8521,8522,8523,8530],{"id":33,"depth":63,"text":34},{"id":6599,"depth":63,"text":6600},{"id":6617,"depth":63,"text":6618},{"id":6704,"depth":63,"text":6705},{"id":6831,"depth":63,"text":6832},{"id":6962,"depth":63,"text":6963,"children":8505},[8506,8507],{"id":6966,"depth":70,"text":6967},{"id":7088,"depth":70,"text":7089},{"id":7194,"depth":63,"text":7195},{"id":7369,"depth":63,"text":7370,"children":8510},[8511,8512,8513,8514],{"id":7373,"depth":70,"text":7374},{"id":7471,"depth":70,"text":7472},{"id":7492,"depth":70,"text":7493},{"id":7631,"depth":70,"text":7632},{"id":7714,"depth":63,"text":7715,"children":8516},[8517,8518,8519,8520],{"id":7731,"depth":70,"text":7732},{"id":7801,"depth":70,"text":7802},{"id":7909,"depth":70,"text":7910},{"id":8006,"depth":70,"text":8007},{"id":8070,"depth":63,"text":8071},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":8524},[8525,8526,8527,8528,8529],{"id":8365,"depth":70,"text":8366},{"id":8372,"depth":70,"text":8373},{"id":8379,"depth":70,"text":8380},{"id":8453,"depth":70,"text":8454},{"id":8460,"depth":70,"text":8461},{"id":2341,"depth":63,"text":2342},"Master default and keyword arguments explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fdefault-and-keyword-arguments-explained",{"title":6392,"description":8531},"learn\u002Fdefault-and-keyword-arguments-explained","6eYaBxUUu3QPenVhGAqI1e8FOD-VzLg58hOpU0yhNoo",{"id":8538,"title":8539,"body":8540,"description":10961,"extension":2399,"meta":10962,"navigation":66,"path":10963,"seo":10964,"stem":10965,"__hash__":10966},"content\u002Flearn\u002Fdictionary-comprehensions-explained.md","Dictionary Comprehensions Explained",{"type":7,"value":8541,"toc":10929},[8542,8545,8548,8551,8559,8561,8646,8652,8656,8659,8673,8676,8690,8693,8704,8708,8711,8740,8743,8762,8765,8795,8801,8805,8874,8876,8911,8913,8931,8935,8938,9013,9015,9059,9062,9076,9079,9174,9176,9222,9225,9233,9240,9244,9250,9253,9354,9356,9391,9394,9408,9411,9423,9425,9545,9547,9580,9584,9587,9590,9601,9604,9615,9619,9709,9711,9751,9755,9826,9829,9832,9835,9843,9845,9848,9852,9854,9884,9887,9950,9960,9964,9967,9970,10030,10032,10053,10056,10059,10121,10125,10128,10131,10135,10138,10141,10222,10224,10257,10263,10267,10270,10284,10288,10352,10354,10416,10420,10469,10471,10524,10528,10654,10656,10709,10711,10714,10731,10734,10833,10836,10850,10861,10863,10867,10872,10876,10882,10886,10889,10893,10896,10898,10923,10926],[10,8543,8539],{"id":8544},"dictionary-comprehensions-explained",[14,8546,8547],{},"A dictionary comprehension is a short way to create a dictionary in Python.",[14,8549,8550],{},"It lets you build keys and values in one line instead of writing a full loop. This is useful when you want to transform existing data into a new dictionary.",[14,8552,8553,8554,8558],{},"If you already know what a Python dictionary is, this is the next step toward writing shorter and cleaner code. If not, read ",[236,8555,8557],{"href":8556},"\u002Flearn\u002Fpython-dictionaries-explained","Python dictionaries explained"," first.",[31,8560,34],{"id":33},[36,8562,8564],{"className":38,"code":8563,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\nsquares = {n: n * n for n in numbers}\nprint(squares)\n# {1: 1, 2: 4, 3: 9, 4: 16}\n",[18,8565,8566,8595,8630,8641],{"__ignoreMap":41},[45,8567,8568,8571,8573,8576,8579,8581,8583,8585,8587,8589,8592],{"class":47,"line":48},[45,8569,8570],{"class":51},"numbers ",[45,8572,56],{"class":55},[45,8574,8575],{"class":85}," [",[45,8577,8578],{"class":59},"1",[45,8580,3079],{"class":85},[45,8582,3754],{"class":59},[45,8584,3079],{"class":85},[45,8586,777],{"class":59},[45,8588,3079],{"class":85},[45,8590,8591],{"class":59}," 4",[45,8593,8594],{"class":85},"]\n",[45,8596,8597,8600,8602,8605,8608,8610,8613,8615,8617,8619,8621,8624,8627],{"class":47,"line":63},[45,8598,8599],{"class":51},"squares ",[45,8601,56],{"class":55},[45,8603,8604],{"class":85}," {",[45,8606,8607],{"class":51},"n",[45,8609,457],{"class":85},[45,8611,8612],{"class":51}," n ",[45,8614,5601],{"class":55},[45,8616,8612],{"class":51},[45,8618,7895],{"class":73},[45,8620,8612],{"class":51},[45,8622,8623],{"class":73},"in",[45,8625,8626],{"class":51}," numbers",[45,8628,8629],{"class":85},"}\n",[45,8631,8632,8634,8636,8639],{"class":47,"line":70},[45,8633,2065],{"class":92},[45,8635,96],{"class":85},[45,8637,8638],{"class":2070},"squares",[45,8640,109],{"class":85},[45,8642,8643],{"class":47,"line":89},[45,8644,8645],{"class":7624},"# {1: 1, 2: 4, 3: 9, 4: 16}\n",[14,8647,8648,8649,240],{},"A dictionary comprehension builds a dictionary in one line using ",[18,8650,8651],{},"key: value for item in iterable",[31,8653,8655],{"id":8654},"what-a-dictionary-comprehension-is","What a dictionary comprehension is",[14,8657,8658],{},"A dictionary comprehension is:",[195,8660,8661,8664,8667,8670],{},[198,8662,8663],{},"A short way to create a dictionary",[198,8665,8666],{},"A combination of a loop and dictionary creation in one expression",[198,8668,8669],{},"A way to produce a new dictionary",[198,8671,8672],{},"Useful when keys and values can be built from existing data",[14,8674,8675],{},"You can think of it like this:",[195,8677,8678,8681,8684,8687],{},[198,8679,8680],{},"Python loops through some data",[198,8682,8683],{},"For each item, it creates a key",[198,8685,8686],{},"It also creates a value",[198,8688,8689],{},"It stores them in a new dictionary",[14,8691,8692],{},"This makes dictionary comprehensions helpful for tasks like:",[195,8694,8695,8698,8701],{},[198,8696,8697],{},"Building lookup tables",[198,8699,8700],{},"Changing the shape of data",[198,8702,8703],{},"Filtering items while creating a dictionary",[31,8705,8707],{"id":8706},"basic-syntax","Basic syntax",[14,8709,8710],{},"The general form is:",[36,8712,8714],{"className":38,"code":8713,"language":40,"meta":41,"style":41},"{key_expression: value_expression for item in iterable}\n",[18,8715,8716],{"__ignoreMap":41},[45,8717,8718,8720,8723,8725,8728,8730,8733,8735,8738],{"class":47,"line":48},[45,8719,5551],{"class":85},[45,8721,8722],{"class":51},"key_expression",[45,8724,457],{"class":85},[45,8726,8727],{"class":51}," value_expression ",[45,8729,7895],{"class":73},[45,8731,8732],{"class":51}," item ",[45,8734,8623],{"class":73},[45,8736,8737],{"class":51}," iterable",[45,8739,8629],{"class":85},[14,8741,8742],{},"Each part has a job:",[195,8744,8745,8750,8756],{},[198,8746,8747,8749],{},[18,8748,8722],{}," creates the dictionary key",[198,8751,8752,8755],{},[18,8753,8754],{},"value_expression"," creates the dictionary value",[198,8757,8758,8761],{},[18,8759,8760],{},"for item in iterable"," loops through the data",[14,8763,8764],{},"You can also add a condition at the end:",[36,8766,8768],{"className":38,"code":8767,"language":40,"meta":41,"style":41},"{key_expression: value_expression for item in iterable if condition}\n",[18,8769,8770],{"__ignoreMap":41},[45,8771,8772,8774,8776,8778,8780,8782,8784,8786,8789,8791,8793],{"class":47,"line":48},[45,8773,5551],{"class":85},[45,8775,8722],{"class":51},[45,8777,457],{"class":85},[45,8779,8727],{"class":51},[45,8781,7895],{"class":73},[45,8783,8732],{"class":51},[45,8785,8623],{"class":73},[45,8787,8788],{"class":51}," iterable ",[45,8790,20],{"class":73},[45,8792,408],{"class":51},[45,8794,8629],{"class":85},[14,8796,8797,8798,8800],{},"That ",[18,8799,20],{}," filters items before they are added to the dictionary.",[712,8802,8804],{"id":8803},"example-of-the-syntax","Example of the syntax",[36,8806,8808],{"className":38,"code":8807,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\n\nresult = {n: n + 10 for n in numbers}\nprint(result)\n",[18,8809,8810,8830,8834,8864],{"__ignoreMap":41},[45,8811,8812,8814,8816,8818,8820,8822,8824,8826,8828],{"class":47,"line":48},[45,8813,8570],{"class":51},[45,8815,56],{"class":55},[45,8817,8575],{"class":85},[45,8819,8578],{"class":59},[45,8821,3079],{"class":85},[45,8823,3754],{"class":59},[45,8825,3079],{"class":85},[45,8827,777],{"class":59},[45,8829,8594],{"class":85},[45,8831,8832],{"class":47,"line":63},[45,8833,67],{"emptyLinePlaceholder":66},[45,8835,8836,8838,8840,8842,8844,8846,8848,8851,8853,8856,8858,8860,8862],{"class":47,"line":70},[45,8837,3595],{"class":51},[45,8839,56],{"class":55},[45,8841,8604],{"class":85},[45,8843,8607],{"class":51},[45,8845,457],{"class":85},[45,8847,8612],{"class":51},[45,8849,8850],{"class":55},"+",[45,8852,1190],{"class":59},[45,8854,8855],{"class":73}," for",[45,8857,8612],{"class":51},[45,8859,8623],{"class":73},[45,8861,8626],{"class":51},[45,8863,8629],{"class":85},[45,8865,8866,8868,8870,8872],{"class":47,"line":89},[45,8867,2065],{"class":92},[45,8869,96],{"class":85},[45,8871,3620],{"class":2070},[45,8873,109],{"class":85},[14,8875,177],{},[36,8877,8879],{"className":38,"code":8878,"language":40,"meta":41,"style":41},"{1: 11, 2: 12, 3: 13}\n",[18,8880,8881],{"__ignoreMap":41},[45,8882,8883,8885,8887,8889,8892,8894,8896,8898,8901,8903,8905,8907,8909],{"class":47,"line":48},[45,8884,5551],{"class":85},[45,8886,8578],{"class":59},[45,8888,457],{"class":85},[45,8890,8891],{"class":59}," 11",[45,8893,3079],{"class":85},[45,8895,3754],{"class":59},[45,8897,457],{"class":85},[45,8899,8900],{"class":59}," 12",[45,8902,3079],{"class":85},[45,8904,777],{"class":59},[45,8906,457],{"class":85},[45,8908,82],{"class":59},[45,8910,8629],{"class":85},[14,8912,6680],{},[195,8914,8915,8920,8925],{},[198,8916,8917,8919],{},[18,8918,8607],{}," is the current item from the list",[198,8921,8922,8924],{},[18,8923,8607],{}," becomes the key",[198,8926,8927,8930],{},[18,8928,8929],{},"n + 10"," becomes the value",[31,8932,8934],{"id":8933},"simple-example-with-a-list","Simple example with a list",[14,8936,8937],{},"A common use is creating a dictionary from a list of values.",[36,8939,8941],{"className":38,"code":8940,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\n\nsquares = {n: n * n for n in numbers}\n\nprint(squares)\n",[18,8942,8943,8967,8971,8999,9003],{"__ignoreMap":41},[45,8944,8945,8947,8949,8951,8953,8955,8957,8959,8961,8963,8965],{"class":47,"line":48},[45,8946,8570],{"class":51},[45,8948,56],{"class":55},[45,8950,8575],{"class":85},[45,8952,8578],{"class":59},[45,8954,3079],{"class":85},[45,8956,3754],{"class":59},[45,8958,3079],{"class":85},[45,8960,777],{"class":59},[45,8962,3079],{"class":85},[45,8964,8591],{"class":59},[45,8966,8594],{"class":85},[45,8968,8969],{"class":47,"line":63},[45,8970,67],{"emptyLinePlaceholder":66},[45,8972,8973,8975,8977,8979,8981,8983,8985,8987,8989,8991,8993,8995,8997],{"class":47,"line":70},[45,8974,8599],{"class":51},[45,8976,56],{"class":55},[45,8978,8604],{"class":85},[45,8980,8607],{"class":51},[45,8982,457],{"class":85},[45,8984,8612],{"class":51},[45,8986,5601],{"class":55},[45,8988,8612],{"class":51},[45,8990,7895],{"class":73},[45,8992,8612],{"class":51},[45,8994,8623],{"class":73},[45,8996,8626],{"class":51},[45,8998,8629],{"class":85},[45,9000,9001],{"class":47,"line":89},[45,9002,67],{"emptyLinePlaceholder":66},[45,9004,9005,9007,9009,9011],{"class":47,"line":112},[45,9006,2065],{"class":92},[45,9008,96],{"class":85},[45,9010,8638],{"class":2070},[45,9012,109],{"class":85},[14,9014,177],{},[36,9016,9018],{"className":38,"code":9017,"language":40,"meta":41,"style":41},"{1: 1, 2: 4, 3: 9, 4: 16}\n",[18,9019,9020],{"__ignoreMap":41},[45,9021,9022,9024,9026,9028,9031,9033,9035,9037,9039,9041,9043,9045,9048,9050,9052,9054,9057],{"class":47,"line":48},[45,9023,5551],{"class":85},[45,9025,8578],{"class":59},[45,9027,457],{"class":85},[45,9029,9030],{"class":59}," 1",[45,9032,3079],{"class":85},[45,9034,3754],{"class":59},[45,9036,457],{"class":85},[45,9038,8591],{"class":59},[45,9040,3079],{"class":85},[45,9042,777],{"class":59},[45,9044,457],{"class":85},[45,9046,9047],{"class":59}," 9",[45,9049,3079],{"class":85},[45,9051,8591],{"class":59},[45,9053,457],{"class":85},[45,9055,9056],{"class":59}," 16",[45,9058,8629],{"class":85},[14,9060,9061],{},"What this does:",[195,9063,9064,9070,9073],{},[198,9065,9066,9067],{},"Loops through each number in ",[18,9068,9069],{},"numbers",[198,9071,9072],{},"Uses the number itself as the key",[198,9074,9075],{},"Uses the square of the number as the value",[14,9077,9078],{},"You are not limited to numbers. You can also use strings.",[36,9080,9082],{"className":38,"code":9081,"language":40,"meta":41,"style":41},"words = [\"cat\", \"dog\", \"elephant\"]\n\nlengths = {word: len(word) for word in words}\n\nprint(lengths)\n",[18,9083,9084,9119,9123,9159,9163],{"__ignoreMap":41},[45,9085,9086,9089,9091,9093,9095,9097,9099,9101,9103,9106,9108,9110,9112,9115,9117],{"class":47,"line":48},[45,9087,9088],{"class":51},"words ",[45,9090,56],{"class":55},[45,9092,8575],{"class":85},[45,9094,100],{"class":99},[45,9096,7035],{"class":103},[45,9098,100],{"class":99},[45,9100,3079],{"class":85},[45,9102,1565],{"class":99},[45,9104,9105],{"class":103},"dog",[45,9107,100],{"class":99},[45,9109,3079],{"class":85},[45,9111,1565],{"class":99},[45,9113,9114],{"class":103},"elephant",[45,9116,100],{"class":99},[45,9118,8594],{"class":85},[45,9120,9121],{"class":47,"line":63},[45,9122,67],{"emptyLinePlaceholder":66},[45,9124,9125,9128,9130,9132,9135,9137,9140,9142,9144,9147,9149,9152,9154,9157],{"class":47,"line":70},[45,9126,9127],{"class":51},"lengths ",[45,9129,56],{"class":55},[45,9131,8604],{"class":85},[45,9133,9134],{"class":51},"word",[45,9136,457],{"class":85},[45,9138,9139],{"class":92}," len",[45,9141,96],{"class":85},[45,9143,9134],{"class":2070},[45,9145,9146],{"class":85},")",[45,9148,8855],{"class":73},[45,9150,9151],{"class":51}," word ",[45,9153,8623],{"class":73},[45,9155,9156],{"class":51}," words",[45,9158,8629],{"class":85},[45,9160,9161],{"class":47,"line":89},[45,9162,67],{"emptyLinePlaceholder":66},[45,9164,9165,9167,9169,9172],{"class":47,"line":112},[45,9166,2065],{"class":92},[45,9168,96],{"class":85},[45,9170,9171],{"class":2070},"lengths",[45,9173,109],{"class":85},[14,9175,177],{},[36,9177,9179],{"className":38,"code":9178,"language":40,"meta":41,"style":41},"{'cat': 3, 'dog': 3, 'elephant': 8}\n",[18,9180,9181],{"__ignoreMap":41},[45,9182,9183,9185,9187,9189,9191,9193,9195,9197,9200,9202,9204,9206,9208,9210,9212,9214,9216,9218,9220],{"class":47,"line":48},[45,9184,5551],{"class":85},[45,9186,7901],{"class":99},[45,9188,7035],{"class":103},[45,9190,7901],{"class":99},[45,9192,457],{"class":85},[45,9194,777],{"class":59},[45,9196,3079],{"class":85},[45,9198,9199],{"class":99}," '",[45,9201,9105],{"class":103},[45,9203,7901],{"class":99},[45,9205,457],{"class":85},[45,9207,777],{"class":59},[45,9209,3079],{"class":85},[45,9211,9199],{"class":99},[45,9213,9114],{"class":103},[45,9215,7901],{"class":99},[45,9217,457],{"class":85},[45,9219,1254],{"class":59},[45,9221,8629],{"class":85},[14,9223,9224],{},"This creates a dictionary where:",[195,9226,9227,9230],{},[198,9228,9229],{},"Each word is a key",[198,9231,9232],{},"Each value is the length of that word",[14,9234,9235,9236,240],{},"If you want to compare this idea with another comprehension type, see ",[236,9237,9239],{"href":9238},"\u002Flearn\u002Flist-comprehensions-in-python-explained","list comprehensions in Python explained",[31,9241,9243],{"id":9242},"using-conditions-in-a-dictionary-comprehension","Using conditions in a dictionary comprehension",[14,9245,9246,9247,9249],{},"You can add ",[18,9248,20],{}," to keep only some items.",[14,9251,9252],{},"For example, this keeps only even numbers:",[36,9254,9256],{"className":38,"code":9255,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4, 5, 6]\n\neven_squares = {n: n * n for n in numbers if n % 2 == 0}\n\nprint(even_squares)\n",[18,9257,9258,9291,9295,9339,9343],{"__ignoreMap":41},[45,9259,9260,9262,9264,9266,9268,9270,9272,9274,9276,9278,9280,9282,9284,9286,9289],{"class":47,"line":48},[45,9261,8570],{"class":51},[45,9263,56],{"class":55},[45,9265,8575],{"class":85},[45,9267,8578],{"class":59},[45,9269,3079],{"class":85},[45,9271,3754],{"class":59},[45,9273,3079],{"class":85},[45,9275,777],{"class":59},[45,9277,3079],{"class":85},[45,9279,8591],{"class":59},[45,9281,3079],{"class":85},[45,9283,1222],{"class":59},[45,9285,3079],{"class":85},[45,9287,9288],{"class":59}," 6",[45,9290,8594],{"class":85},[45,9292,9293],{"class":47,"line":63},[45,9294,67],{"emptyLinePlaceholder":66},[45,9296,9297,9300,9302,9304,9306,9308,9310,9312,9314,9316,9318,9320,9323,9325,9327,9330,9332,9335,9337],{"class":47,"line":70},[45,9298,9299],{"class":51},"even_squares ",[45,9301,56],{"class":55},[45,9303,8604],{"class":85},[45,9305,8607],{"class":51},[45,9307,457],{"class":85},[45,9309,8612],{"class":51},[45,9311,5601],{"class":55},[45,9313,8612],{"class":51},[45,9315,7895],{"class":73},[45,9317,8612],{"class":51},[45,9319,8623],{"class":73},[45,9321,9322],{"class":51}," numbers ",[45,9324,20],{"class":73},[45,9326,8612],{"class":51},[45,9328,9329],{"class":55},"%",[45,9331,3754],{"class":59},[45,9333,9334],{"class":55}," ==",[45,9336,749],{"class":59},[45,9338,8629],{"class":85},[45,9340,9341],{"class":47,"line":89},[45,9342,67],{"emptyLinePlaceholder":66},[45,9344,9345,9347,9349,9352],{"class":47,"line":112},[45,9346,2065],{"class":92},[45,9348,96],{"class":85},[45,9350,9351],{"class":2070},"even_squares",[45,9353,109],{"class":85},[14,9355,177],{},[36,9357,9359],{"className":38,"code":9358,"language":40,"meta":41,"style":41},"{2: 4, 4: 16, 6: 36}\n",[18,9360,9361],{"__ignoreMap":41},[45,9362,9363,9365,9368,9370,9372,9374,9376,9378,9380,9382,9384,9386,9389],{"class":47,"line":48},[45,9364,5551],{"class":85},[45,9366,9367],{"class":59},"2",[45,9369,457],{"class":85},[45,9371,8591],{"class":59},[45,9373,3079],{"class":85},[45,9375,8591],{"class":59},[45,9377,457],{"class":85},[45,9379,9056],{"class":59},[45,9381,3079],{"class":85},[45,9383,9288],{"class":59},[45,9385,457],{"class":85},[45,9387,9388],{"class":59}," 36",[45,9390,8629],{"class":85},[14,9392,9393],{},"How it works:",[195,9395,9396,9399,9405],{},[198,9397,9398],{},"Python loops through all numbers",[198,9400,9401,9402],{},"It checks ",[18,9403,9404],{},"n % 2 == 0",[198,9406,9407],{},"Only even numbers are added to the dictionary",[14,9409,9410],{},"This is useful because filtering happens during dictionary creation. You do not need extra lines to build the dictionary step by step.",[14,9412,9413,9414,9417,9418,9422],{},"You can also filter an existing dictionary. To do that, you will often use ",[18,9415,9416],{},".items()",". See ",[236,9419,9421],{"href":9420},"\u002Freference\u002Fpython-dictionary-items-method","Python dictionary items() method"," if that method is new to you.",[14,9424,466],{},[36,9426,9428],{"className":38,"code":9427,"language":40,"meta":41,"style":41},"prices = {\"apple\": 2, \"banana\": 1, \"cherry\": 3}\n\nexpensive = {key: value for key, value in prices.items() if value >= 2}\n\nprint(expensive)\n",[18,9429,9430,9478,9482,9530,9534],{"__ignoreMap":41},[45,9431,9432,9435,9437,9439,9441,9444,9446,9448,9450,9452,9454,9457,9459,9461,9463,9465,9467,9470,9472,9474,9476],{"class":47,"line":48},[45,9433,9434],{"class":51},"prices ",[45,9436,56],{"class":55},[45,9438,8604],{"class":85},[45,9440,100],{"class":99},[45,9442,9443],{"class":103},"apple",[45,9445,100],{"class":99},[45,9447,457],{"class":85},[45,9449,3754],{"class":59},[45,9451,3079],{"class":85},[45,9453,1565],{"class":99},[45,9455,9456],{"class":103},"banana",[45,9458,100],{"class":99},[45,9460,457],{"class":85},[45,9462,9030],{"class":59},[45,9464,3079],{"class":85},[45,9466,1565],{"class":99},[45,9468,9469],{"class":103},"cherry",[45,9471,100],{"class":99},[45,9473,457],{"class":85},[45,9475,777],{"class":59},[45,9477,8629],{"class":85},[45,9479,9480],{"class":47,"line":63},[45,9481,67],{"emptyLinePlaceholder":66},[45,9483,9484,9487,9489,9491,9494,9496,9498,9500,9503,9505,9507,9509,9512,9514,9517,9519,9522,9524,9526,9528],{"class":47,"line":70},[45,9485,9486],{"class":51},"expensive ",[45,9488,56],{"class":55},[45,9490,8604],{"class":85},[45,9492,9493],{"class":51},"key",[45,9495,457],{"class":85},[45,9497,1492],{"class":51},[45,9499,7895],{"class":73},[45,9501,9502],{"class":51}," key",[45,9504,3079],{"class":85},[45,9506,1492],{"class":51},[45,9508,8623],{"class":73},[45,9510,9511],{"class":51}," prices",[45,9513,240],{"class":85},[45,9515,9516],{"class":2070},"items",[45,9518,3772],{"class":85},[45,9520,9521],{"class":73}," if",[45,9523,1492],{"class":51},[45,9525,600],{"class":55},[45,9527,3754],{"class":59},[45,9529,8629],{"class":85},[45,9531,9532],{"class":47,"line":89},[45,9533,67],{"emptyLinePlaceholder":66},[45,9535,9536,9538,9540,9543],{"class":47,"line":112},[45,9537,2065],{"class":92},[45,9539,96],{"class":85},[45,9541,9542],{"class":2070},"expensive",[45,9544,109],{"class":85},[14,9546,177],{},[36,9548,9550],{"className":38,"code":9549,"language":40,"meta":41,"style":41},"{'apple': 2, 'cherry': 3}\n",[18,9551,9552],{"__ignoreMap":41},[45,9553,9554,9556,9558,9560,9562,9564,9566,9568,9570,9572,9574,9576,9578],{"class":47,"line":48},[45,9555,5551],{"class":85},[45,9557,7901],{"class":99},[45,9559,9443],{"class":103},[45,9561,7901],{"class":99},[45,9563,457],{"class":85},[45,9565,3754],{"class":59},[45,9567,3079],{"class":85},[45,9569,9199],{"class":99},[45,9571,9469],{"class":103},[45,9573,7901],{"class":99},[45,9575,457],{"class":85},[45,9577,777],{"class":59},[45,9579,8629],{"class":85},[31,9581,9583],{"id":9582},"dictionary-comprehension-vs-normal-loop","Dictionary comprehension vs normal loop",[14,9585,9586],{},"Dictionary comprehensions are useful, but they are not always the best choice.",[14,9588,9589],{},"Use a dictionary comprehension when:",[195,9591,9592,9595,9598],{},[198,9593,9594],{},"The transformation is short",[198,9596,9597],{},"The logic is simple",[198,9599,9600],{},"The result is easy to read in one line",[14,9602,9603],{},"Use a normal loop when:",[195,9605,9606,9609,9612],{},[198,9607,9608],{},"The logic is more complex",[198,9610,9611],{},"You need multiple steps",[198,9613,9614],{},"The one-line version is hard to understand",[712,9616,9618],{"id":9617},"same-result-with-a-normal-loop","Same result with a normal loop",[36,9620,9622],{"className":38,"code":9621,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\nsquares = {}\n\nfor n in numbers:\n    squares[n] = n * n\n\nprint(squares)\n",[18,9623,9624,9648,9657,9661,9673,9695,9699],{"__ignoreMap":41},[45,9625,9626,9628,9630,9632,9634,9636,9638,9640,9642,9644,9646],{"class":47,"line":48},[45,9627,8570],{"class":51},[45,9629,56],{"class":55},[45,9631,8575],{"class":85},[45,9633,8578],{"class":59},[45,9635,3079],{"class":85},[45,9637,3754],{"class":59},[45,9639,3079],{"class":85},[45,9641,777],{"class":59},[45,9643,3079],{"class":85},[45,9645,8591],{"class":59},[45,9647,8594],{"class":85},[45,9649,9650,9652,9654],{"class":47,"line":63},[45,9651,8599],{"class":51},[45,9653,56],{"class":55},[45,9655,9656],{"class":85}," {}\n",[45,9658,9659],{"class":47,"line":70},[45,9660,67],{"emptyLinePlaceholder":66},[45,9662,9663,9665,9667,9669,9671],{"class":47,"line":89},[45,9664,7895],{"class":73},[45,9666,8612],{"class":51},[45,9668,8623],{"class":73},[45,9670,8626],{"class":51},[45,9672,86],{"class":85},[45,9674,9675,9678,9681,9683,9686,9688,9690,9692],{"class":47,"line":112},[45,9676,9677],{"class":51},"    squares",[45,9679,9680],{"class":85},"[",[45,9682,8607],{"class":51},[45,9684,9685],{"class":85},"]",[45,9687,3099],{"class":55},[45,9689,8612],{"class":51},[45,9691,5601],{"class":55},[45,9693,9694],{"class":51}," n\n",[45,9696,9697],{"class":47,"line":126},[45,9698,67],{"emptyLinePlaceholder":66},[45,9700,9701,9703,9705,9707],{"class":47,"line":142},[45,9702,2065],{"class":92},[45,9704,96],{"class":85},[45,9706,8638],{"class":2070},[45,9708,109],{"class":85},[14,9710,177],{},[36,9712,9713],{"className":38,"code":9017,"language":40,"meta":41,"style":41},[18,9714,9715],{"__ignoreMap":41},[45,9716,9717,9719,9721,9723,9725,9727,9729,9731,9733,9735,9737,9739,9741,9743,9745,9747,9749],{"class":47,"line":48},[45,9718,5551],{"class":85},[45,9720,8578],{"class":59},[45,9722,457],{"class":85},[45,9724,9030],{"class":59},[45,9726,3079],{"class":85},[45,9728,3754],{"class":59},[45,9730,457],{"class":85},[45,9732,8591],{"class":59},[45,9734,3079],{"class":85},[45,9736,777],{"class":59},[45,9738,457],{"class":85},[45,9740,9047],{"class":59},[45,9742,3079],{"class":85},[45,9744,8591],{"class":59},[45,9746,457],{"class":85},[45,9748,9056],{"class":59},[45,9750,8629],{"class":85},[712,9752,9754],{"id":9753},"same-result-with-a-dictionary-comprehension","Same result with a dictionary comprehension",[36,9756,9758],{"className":38,"code":9757,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\nsquares = {n: n * n for n in numbers}\n\nprint(squares)\n",[18,9759,9760,9784,9812,9816],{"__ignoreMap":41},[45,9761,9762,9764,9766,9768,9770,9772,9774,9776,9778,9780,9782],{"class":47,"line":48},[45,9763,8570],{"class":51},[45,9765,56],{"class":55},[45,9767,8575],{"class":85},[45,9769,8578],{"class":59},[45,9771,3079],{"class":85},[45,9773,3754],{"class":59},[45,9775,3079],{"class":85},[45,9777,777],{"class":59},[45,9779,3079],{"class":85},[45,9781,8591],{"class":59},[45,9783,8594],{"class":85},[45,9785,9786,9788,9790,9792,9794,9796,9798,9800,9802,9804,9806,9808,9810],{"class":47,"line":63},[45,9787,8599],{"class":51},[45,9789,56],{"class":55},[45,9791,8604],{"class":85},[45,9793,8607],{"class":51},[45,9795,457],{"class":85},[45,9797,8612],{"class":51},[45,9799,5601],{"class":55},[45,9801,8612],{"class":51},[45,9803,7895],{"class":73},[45,9805,8612],{"class":51},[45,9807,8623],{"class":73},[45,9809,8626],{"class":51},[45,9811,8629],{"class":85},[45,9813,9814],{"class":47,"line":70},[45,9815,67],{"emptyLinePlaceholder":66},[45,9817,9818,9820,9822,9824],{"class":47,"line":89},[45,9819,2065],{"class":92},[45,9821,96],{"class":85},[45,9823,8638],{"class":2070},[45,9825,109],{"class":85},[14,9827,9828],{},"Both versions are correct.",[14,9830,9831],{},"The comprehension is shorter. The loop may feel clearer when you are still learning.",[14,9833,9834],{},"A good rule for beginners is:",[195,9836,9837,9840],{},[198,9838,9839],{},"Prefer the clearest version",[198,9841,9842],{},"Use a comprehension when it still feels easy to read",[31,9844,1337],{"id":1336},[14,9846,9847],{},"Here are some common problems beginners run into.",[712,9849,9851],{"id":9850},"forgetting-the-colon-between-key-and-value","Forgetting the colon between key and value",[14,9853,1358],{},[36,9855,9857],{"className":38,"code":9856,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\n# result = {n n * n for n in numbers}\n",[18,9858,9859,9879],{"__ignoreMap":41},[45,9860,9861,9863,9865,9867,9869,9871,9873,9875,9877],{"class":47,"line":48},[45,9862,8570],{"class":51},[45,9864,56],{"class":55},[45,9866,8575],{"class":85},[45,9868,8578],{"class":59},[45,9870,3079],{"class":85},[45,9872,3754],{"class":59},[45,9874,3079],{"class":85},[45,9876,777],{"class":59},[45,9878,8594],{"class":85},[45,9880,9881],{"class":47,"line":63},[45,9882,9883],{"class":7624},"# result = {n n * n for n in numbers}\n",[14,9885,9886],{},"This is correct:",[36,9888,9890],{"className":38,"code":9889,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\nresult = {n: n * n for n in numbers}\nprint(result)\n",[18,9891,9892,9912,9940],{"__ignoreMap":41},[45,9893,9894,9896,9898,9900,9902,9904,9906,9908,9910],{"class":47,"line":48},[45,9895,8570],{"class":51},[45,9897,56],{"class":55},[45,9899,8575],{"class":85},[45,9901,8578],{"class":59},[45,9903,3079],{"class":85},[45,9905,3754],{"class":59},[45,9907,3079],{"class":85},[45,9909,777],{"class":59},[45,9911,8594],{"class":85},[45,9913,9914,9916,9918,9920,9922,9924,9926,9928,9930,9932,9934,9936,9938],{"class":47,"line":63},[45,9915,3595],{"class":51},[45,9917,56],{"class":55},[45,9919,8604],{"class":85},[45,9921,8607],{"class":51},[45,9923,457],{"class":85},[45,9925,8612],{"class":51},[45,9927,5601],{"class":55},[45,9929,8612],{"class":51},[45,9931,7895],{"class":73},[45,9933,8612],{"class":51},[45,9935,8623],{"class":73},[45,9937,8626],{"class":51},[45,9939,8629],{"class":85},[45,9941,9942,9944,9946,9948],{"class":47,"line":70},[45,9943,2065],{"class":92},[45,9945,96],{"class":85},[45,9947,3620],{"class":2070},[45,9949,109],{"class":85},[14,9951,9952,9953,9955,9956,9959],{},"The colon ",[18,9954,457],{}," is required because dictionaries store ",[18,9957,9958],{},"key: value"," pairs.",[712,9961,9963],{"id":9962},"using-square-brackets-instead-of-curly-braces","Using square brackets instead of curly braces",[14,9965,9966],{},"Square brackets create a list comprehension, not a dictionary comprehension.",[14,9968,9969],{},"Wrong shape:",[36,9971,9973],{"className":38,"code":9972,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\nresult = [n * n for n in numbers]\nprint(result)\n",[18,9974,9975,9995,10020],{"__ignoreMap":41},[45,9976,9977,9979,9981,9983,9985,9987,9989,9991,9993],{"class":47,"line":48},[45,9978,8570],{"class":51},[45,9980,56],{"class":55},[45,9982,8575],{"class":85},[45,9984,8578],{"class":59},[45,9986,3079],{"class":85},[45,9988,3754],{"class":59},[45,9990,3079],{"class":85},[45,9992,777],{"class":59},[45,9994,8594],{"class":85},[45,9996,9997,9999,10001,10003,10006,10008,10010,10012,10014,10016,10018],{"class":47,"line":63},[45,9998,3595],{"class":51},[45,10000,56],{"class":55},[45,10002,8575],{"class":85},[45,10004,10005],{"class":51},"n ",[45,10007,5601],{"class":55},[45,10009,8612],{"class":51},[45,10011,7895],{"class":73},[45,10013,8612],{"class":51},[45,10015,8623],{"class":73},[45,10017,8626],{"class":51},[45,10019,8594],{"class":85},[45,10021,10022,10024,10026,10028],{"class":47,"line":70},[45,10023,2065],{"class":92},[45,10025,96],{"class":85},[45,10027,3620],{"class":2070},[45,10029,109],{"class":85},[14,10031,177],{},[36,10033,10035],{"className":38,"code":10034,"language":40,"meta":41,"style":41},"[1, 4, 9]\n",[18,10036,10037],{"__ignoreMap":41},[45,10038,10039,10041,10043,10045,10047,10049,10051],{"class":47,"line":48},[45,10040,9680],{"class":85},[45,10042,8578],{"class":59},[45,10044,3079],{"class":85},[45,10046,8591],{"class":59},[45,10048,3079],{"class":85},[45,10050,9047],{"class":59},[45,10052,8594],{"class":85},[14,10054,10055],{},"That is a list, not a dictionary.",[14,10057,10058],{},"Dictionary comprehensions must use curly braces:",[36,10060,10061],{"className":38,"code":9889,"language":40,"meta":41,"style":41},[18,10062,10063,10083,10111],{"__ignoreMap":41},[45,10064,10065,10067,10069,10071,10073,10075,10077,10079,10081],{"class":47,"line":48},[45,10066,8570],{"class":51},[45,10068,56],{"class":55},[45,10070,8575],{"class":85},[45,10072,8578],{"class":59},[45,10074,3079],{"class":85},[45,10076,3754],{"class":59},[45,10078,3079],{"class":85},[45,10080,777],{"class":59},[45,10082,8594],{"class":85},[45,10084,10085,10087,10089,10091,10093,10095,10097,10099,10101,10103,10105,10107,10109],{"class":47,"line":63},[45,10086,3595],{"class":51},[45,10088,56],{"class":55},[45,10090,8604],{"class":85},[45,10092,8607],{"class":51},[45,10094,457],{"class":85},[45,10096,8612],{"class":51},[45,10098,5601],{"class":55},[45,10100,8612],{"class":51},[45,10102,7895],{"class":73},[45,10104,8612],{"class":51},[45,10106,8623],{"class":73},[45,10108,8626],{"class":51},[45,10110,8629],{"class":85},[45,10112,10113,10115,10117,10119],{"class":47,"line":70},[45,10114,2065],{"class":92},[45,10116,96],{"class":85},[45,10118,3620],{"class":2070},[45,10120,109],{"class":85},[712,10122,10124],{"id":10123},"trying-to-do-too-much-in-one-comprehension","Trying to do too much in one comprehension",[14,10126,10127],{},"Short code is not always better.",[14,10129,10130],{},"If your comprehension becomes hard to read, rewrite it as a normal loop. This is especially important for beginners.",[712,10132,10134],{"id":10133},"creating-duplicate-keys-without-noticing","Creating duplicate keys without noticing",[14,10136,10137],{},"Dictionary keys must be unique.",[14,10139,10140],{},"If two items create the same key, the later value replaces the earlier one.",[36,10142,10144],{"className":38,"code":10143,"language":40,"meta":41,"style":41},"words = [\"cat\", \"dog\", \"cat\"]\n\nresult = {word: len(word) for word in words}\nprint(result)\n",[18,10145,10146,10178,10182,10212],{"__ignoreMap":41},[45,10147,10148,10150,10152,10154,10156,10158,10160,10162,10164,10166,10168,10170,10172,10174,10176],{"class":47,"line":48},[45,10149,9088],{"class":51},[45,10151,56],{"class":55},[45,10153,8575],{"class":85},[45,10155,100],{"class":99},[45,10157,7035],{"class":103},[45,10159,100],{"class":99},[45,10161,3079],{"class":85},[45,10163,1565],{"class":99},[45,10165,9105],{"class":103},[45,10167,100],{"class":99},[45,10169,3079],{"class":85},[45,10171,1565],{"class":99},[45,10173,7035],{"class":103},[45,10175,100],{"class":99},[45,10177,8594],{"class":85},[45,10179,10180],{"class":47,"line":63},[45,10181,67],{"emptyLinePlaceholder":66},[45,10183,10184,10186,10188,10190,10192,10194,10196,10198,10200,10202,10204,10206,10208,10210],{"class":47,"line":70},[45,10185,3595],{"class":51},[45,10187,56],{"class":55},[45,10189,8604],{"class":85},[45,10191,9134],{"class":51},[45,10193,457],{"class":85},[45,10195,9139],{"class":92},[45,10197,96],{"class":85},[45,10199,9134],{"class":2070},[45,10201,9146],{"class":85},[45,10203,8855],{"class":73},[45,10205,9151],{"class":51},[45,10207,8623],{"class":73},[45,10209,9156],{"class":51},[45,10211,8629],{"class":85},[45,10213,10214,10216,10218,10220],{"class":47,"line":89},[45,10215,2065],{"class":92},[45,10217,96],{"class":85},[45,10219,3620],{"class":2070},[45,10221,109],{"class":85},[14,10223,177],{},[36,10225,10227],{"className":38,"code":10226,"language":40,"meta":41,"style":41},"{'cat': 3, 'dog': 3}\n",[18,10228,10229],{"__ignoreMap":41},[45,10230,10231,10233,10235,10237,10239,10241,10243,10245,10247,10249,10251,10253,10255],{"class":47,"line":48},[45,10232,5551],{"class":85},[45,10234,7901],{"class":99},[45,10236,7035],{"class":103},[45,10238,7901],{"class":99},[45,10240,457],{"class":85},[45,10242,777],{"class":59},[45,10244,3079],{"class":85},[45,10246,9199],{"class":99},[45,10248,9105],{"class":103},[45,10250,7901],{"class":99},[45,10252,457],{"class":85},[45,10254,777],{"class":59},[45,10256,8629],{"class":85},[14,10258,10259,10260,10262],{},"There is only one ",[18,10261,7075],{}," key in the final dictionary.",[31,10264,10266],{"id":10265},"when-to-use-dictionary-comprehensions","When to use dictionary comprehensions",[14,10268,10269],{},"Dictionary comprehensions are a good choice when you want to:",[195,10271,10272,10275,10278,10281],{},[198,10273,10274],{},"Build lookup tables from lists",[198,10276,10277],{},"Convert one dictionary into another shape",[198,10279,10280],{},"Filter dictionary items",[198,10282,10283],{},"Create dictionaries from strings, ranges, or zipped data",[712,10285,10287],{"id":10286},"from-a-string","From a string",[36,10289,10291],{"className":38,"code":10290,"language":40,"meta":41,"style":41},"text = \"abc\"\nresult = {char: char.upper() for char in text}\nprint(result)\n",[18,10292,10293,10307,10342],{"__ignoreMap":41},[45,10294,10295,10298,10300,10302,10305],{"class":47,"line":48},[45,10296,10297],{"class":51},"text ",[45,10299,56],{"class":55},[45,10301,1565],{"class":99},[45,10303,10304],{"class":103},"abc",[45,10306,1571],{"class":99},[45,10308,10309,10311,10313,10315,10318,10320,10323,10325,10328,10330,10332,10335,10337,10340],{"class":47,"line":63},[45,10310,3595],{"class":51},[45,10312,56],{"class":55},[45,10314,8604],{"class":85},[45,10316,10317],{"class":51},"char",[45,10319,457],{"class":85},[45,10321,10322],{"class":51}," char",[45,10324,240],{"class":85},[45,10326,10327],{"class":2070},"upper",[45,10329,3772],{"class":85},[45,10331,8855],{"class":73},[45,10333,10334],{"class":51}," char ",[45,10336,8623],{"class":73},[45,10338,10339],{"class":51}," text",[45,10341,8629],{"class":85},[45,10343,10344,10346,10348,10350],{"class":47,"line":70},[45,10345,2065],{"class":92},[45,10347,96],{"class":85},[45,10349,3620],{"class":2070},[45,10351,109],{"class":85},[14,10353,177],{},[36,10355,10357],{"className":38,"code":10356,"language":40,"meta":41,"style":41},"{'a': 'A', 'b': 'B', 'c': 'C'}\n",[18,10358,10359],{"__ignoreMap":41},[45,10360,10361,10363,10365,10367,10369,10371,10373,10376,10378,10380,10382,10385,10387,10389,10391,10394,10396,10398,10400,10403,10405,10407,10409,10412,10414],{"class":47,"line":48},[45,10362,5551],{"class":85},[45,10364,7901],{"class":99},[45,10366,236],{"class":103},[45,10368,7901],{"class":99},[45,10370,457],{"class":85},[45,10372,9199],{"class":99},[45,10374,10375],{"class":103},"A",[45,10377,7901],{"class":99},[45,10379,3079],{"class":85},[45,10381,9199],{"class":99},[45,10383,10384],{"class":103},"b",[45,10386,7901],{"class":99},[45,10388,457],{"class":85},[45,10390,9199],{"class":99},[45,10392,10393],{"class":103},"B",[45,10395,7901],{"class":99},[45,10397,3079],{"class":85},[45,10399,9199],{"class":99},[45,10401,10402],{"class":103},"c",[45,10404,7901],{"class":99},[45,10406,457],{"class":85},[45,10408,9199],{"class":99},[45,10410,10411],{"class":103},"C",[45,10413,7901],{"class":99},[45,10415,8629],{"class":85},[712,10417,10419],{"id":10418},"from-a-range","From a range",[36,10421,10423],{"className":38,"code":10422,"language":40,"meta":41,"style":41},"result = {n: n * 10 for n in range(5)}\nprint(result)\n",[18,10424,10425,10459],{"__ignoreMap":41},[45,10426,10427,10429,10431,10433,10435,10437,10439,10441,10443,10445,10447,10449,10452,10454,10456],{"class":47,"line":48},[45,10428,3595],{"class":51},[45,10430,56],{"class":55},[45,10432,8604],{"class":85},[45,10434,8607],{"class":51},[45,10436,457],{"class":85},[45,10438,8612],{"class":51},[45,10440,5601],{"class":55},[45,10442,1190],{"class":59},[45,10444,8855],{"class":73},[45,10446,8612],{"class":51},[45,10448,8623],{"class":73},[45,10450,10451],{"class":92}," range",[45,10453,96],{"class":85},[45,10455,3749],{"class":59},[45,10457,10458],{"class":85},")}\n",[45,10460,10461,10463,10465,10467],{"class":47,"line":63},[45,10462,2065],{"class":92},[45,10464,96],{"class":85},[45,10466,3620],{"class":2070},[45,10468,109],{"class":85},[14,10470,177],{},[36,10472,10474],{"className":38,"code":10473,"language":40,"meta":41,"style":41},"{0: 0, 1: 10, 2: 20, 3: 30, 4: 40}\n",[18,10475,10476],{"__ignoreMap":41},[45,10477,10478,10480,10483,10485,10487,10489,10491,10493,10495,10497,10499,10501,10504,10506,10508,10510,10513,10515,10517,10519,10522],{"class":47,"line":48},[45,10479,5551],{"class":85},[45,10481,10482],{"class":59},"0",[45,10484,457],{"class":85},[45,10486,749],{"class":59},[45,10488,3079],{"class":85},[45,10490,9030],{"class":59},[45,10492,457],{"class":85},[45,10494,1190],{"class":59},[45,10496,3079],{"class":85},[45,10498,3754],{"class":59},[45,10500,457],{"class":85},[45,10502,10503],{"class":59}," 20",[45,10505,3079],{"class":85},[45,10507,777],{"class":59},[45,10509,457],{"class":85},[45,10511,10512],{"class":59}," 30",[45,10514,3079],{"class":85},[45,10516,8591],{"class":59},[45,10518,457],{"class":85},[45,10520,10521],{"class":59}," 40",[45,10523,8629],{"class":85},[712,10525,10527],{"id":10526},"from-zipped-data","From zipped data",[36,10529,10531],{"className":38,"code":10530,"language":40,"meta":41,"style":41},"keys = [\"name\", \"age\", \"city\"]\nvalues = [\"Ana\", 25, \"Paris\"]\n\nresult = {k: v for k, v in zip(keys, values)}\nprint(result)\n",[18,10532,10533,10567,10598,10602,10644],{"__ignoreMap":41},[45,10534,10535,10538,10540,10542,10544,10546,10548,10550,10552,10554,10556,10558,10560,10563,10565],{"class":47,"line":48},[45,10536,10537],{"class":51},"keys ",[45,10539,56],{"class":55},[45,10541,8575],{"class":85},[45,10543,100],{"class":99},[45,10545,3096],{"class":103},[45,10547,100],{"class":99},[45,10549,3079],{"class":85},[45,10551,1565],{"class":99},[45,10553,2082],{"class":103},[45,10555,100],{"class":99},[45,10557,3079],{"class":85},[45,10559,1565],{"class":99},[45,10561,10562],{"class":103},"city",[45,10564,100],{"class":99},[45,10566,8594],{"class":85},[45,10568,10569,10572,10574,10576,10578,10581,10583,10585,10587,10589,10591,10594,10596],{"class":47,"line":63},[45,10570,10571],{"class":51},"values ",[45,10573,56],{"class":55},[45,10575,8575],{"class":85},[45,10577,100],{"class":99},[45,10579,10580],{"class":103},"Ana",[45,10582,100],{"class":99},[45,10584,3079],{"class":85},[45,10586,303],{"class":59},[45,10588,3079],{"class":85},[45,10590,1565],{"class":99},[45,10592,10593],{"class":103},"Paris",[45,10595,100],{"class":99},[45,10597,8594],{"class":85},[45,10599,10600],{"class":47,"line":70},[45,10601,67],{"emptyLinePlaceholder":66},[45,10603,10604,10606,10608,10610,10613,10615,10618,10620,10623,10625,10627,10629,10632,10634,10637,10639,10642],{"class":47,"line":89},[45,10605,3595],{"class":51},[45,10607,56],{"class":55},[45,10609,8604],{"class":85},[45,10611,10612],{"class":51},"k",[45,10614,457],{"class":85},[45,10616,10617],{"class":51}," v ",[45,10619,7895],{"class":73},[45,10621,10622],{"class":51}," k",[45,10624,3079],{"class":85},[45,10626,10617],{"class":51},[45,10628,8623],{"class":73},[45,10630,10631],{"class":92}," zip",[45,10633,96],{"class":85},[45,10635,10636],{"class":2070},"keys",[45,10638,3079],{"class":85},[45,10640,10641],{"class":2070}," values",[45,10643,10458],{"class":85},[45,10645,10646,10648,10650,10652],{"class":47,"line":112},[45,10647,2065],{"class":92},[45,10649,96],{"class":85},[45,10651,3620],{"class":2070},[45,10653,109],{"class":85},[14,10655,177],{},[36,10657,10659],{"className":38,"code":10658,"language":40,"meta":41,"style":41},"{'name': 'Ana', 'age': 25, 'city': 'Paris'}\n",[18,10660,10661],{"__ignoreMap":41},[45,10662,10663,10665,10667,10669,10671,10673,10675,10677,10679,10681,10683,10685,10687,10689,10691,10693,10695,10697,10699,10701,10703,10705,10707],{"class":47,"line":48},[45,10664,5551],{"class":85},[45,10666,7901],{"class":99},[45,10668,3096],{"class":103},[45,10670,7901],{"class":99},[45,10672,457],{"class":85},[45,10674,9199],{"class":99},[45,10676,10580],{"class":103},[45,10678,7901],{"class":99},[45,10680,3079],{"class":85},[45,10682,9199],{"class":99},[45,10684,2082],{"class":103},[45,10686,7901],{"class":99},[45,10688,457],{"class":85},[45,10690,303],{"class":59},[45,10692,3079],{"class":85},[45,10694,9199],{"class":99},[45,10696,10562],{"class":103},[45,10698,7901],{"class":99},[45,10700,457],{"class":85},[45,10702,9199],{"class":99},[45,10704,10593],{"class":103},[45,10706,7901],{"class":99},[45,10708,8629],{"class":85},[31,10710,2012],{"id":2011},[14,10712,10713],{},"Beginners often struggle with dictionary comprehensions because of these issues:",[195,10715,10716,10719,10725,10728],{},[198,10717,10718],{},"Confusing list comprehensions with dictionary comprehensions",[198,10720,10721,10722,10724],{},"Not understanding that dictionaries need ",[18,10723,9958],{}," pairs",[198,10726,10727],{},"Using complex logic that is better written as a normal loop",[198,10729,10730],{},"Accidentally overwriting values because keys repeat",[14,10732,10733],{},"If something looks wrong, these quick checks can help:",[36,10735,10737],{"className":38,"code":10736,"language":40,"meta":41,"style":41},"print(result)\nprint(type(result))\nprint(result.keys())\nprint(result.values())\n\nfor k, v in result.items():\n    print(k, v)\n",[18,10738,10739,10749,10763,10778,10793,10797,10818],{"__ignoreMap":41},[45,10740,10741,10743,10745,10747],{"class":47,"line":48},[45,10742,2065],{"class":92},[45,10744,96],{"class":85},[45,10746,3620],{"class":2070},[45,10748,109],{"class":85},[45,10750,10751,10753,10755,10757,10759,10761],{"class":47,"line":63},[45,10752,2065],{"class":92},[45,10754,96],{"class":85},[45,10756,4340],{"class":2109},[45,10758,96],{"class":85},[45,10760,3620],{"class":2070},[45,10762,4348],{"class":85},[45,10764,10765,10767,10769,10771,10773,10775],{"class":47,"line":70},[45,10766,2065],{"class":92},[45,10768,96],{"class":85},[45,10770,3620],{"class":2070},[45,10772,240],{"class":85},[45,10774,10636],{"class":2070},[45,10776,10777],{"class":85},"())\n",[45,10779,10780,10782,10784,10786,10788,10791],{"class":47,"line":89},[45,10781,2065],{"class":92},[45,10783,96],{"class":85},[45,10785,3620],{"class":2070},[45,10787,240],{"class":85},[45,10789,10790],{"class":2070},"values",[45,10792,10777],{"class":85},[45,10794,10795],{"class":47,"line":112},[45,10796,67],{"emptyLinePlaceholder":66},[45,10798,10799,10801,10803,10805,10807,10809,10812,10814,10816],{"class":47,"line":126},[45,10800,7895],{"class":73},[45,10802,10622],{"class":51},[45,10804,3079],{"class":85},[45,10806,10617],{"class":51},[45,10808,8623],{"class":73},[45,10810,10811],{"class":51}," result",[45,10813,240],{"class":85},[45,10815,9516],{"class":2070},[45,10817,3828],{"class":85},[45,10819,10820,10822,10824,10826,10828,10831],{"class":47,"line":142},[45,10821,93],{"class":92},[45,10823,96],{"class":85},[45,10825,10612],{"class":2070},[45,10827,3079],{"class":85},[45,10829,10830],{"class":2070}," v",[45,10832,109],{"class":85},[14,10834,10835],{},"These checks help you confirm:",[195,10837,10838,10841,10844,10847],{},[198,10839,10840],{},"Whether the result is really a dictionary",[198,10842,10843],{},"Which keys were created",[198,10845,10846],{},"Which values were created",[198,10848,10849],{},"Whether the keys and values match what you expected",[14,10851,10852,10853,1316,10857,240],{},"If you need more practice working with dictionary contents, read ",[236,10854,10856],{"href":10855},"\u002Fhow-to\u002Fhow-to-loop-through-a-dictionary-in-python","how to loop through a dictionary in Python",[236,10858,10860],{"href":10859},"\u002Fhow-to\u002Fhow-to-check-if-a-key-exists-in-a-dictionary-in-python","how to check if a key exists in a dictionary in Python",[31,10862,2281],{"id":2280},[712,10864,10866],{"id":10865},"what-is-the-difference-between-a-list-comprehension-and-a-dictionary-comprehension","What is the difference between a list comprehension and a dictionary comprehension?",[14,10868,10869,10870,9959],{},"A list comprehension creates a list. A dictionary comprehension creates a dictionary with ",[18,10871,9958],{},[712,10873,10875],{"id":10874},"can-i-use-if-in-a-dictionary-comprehension","Can I use if in a dictionary comprehension?",[14,10877,10878,10879,10881],{},"Yes. You can add an ",[18,10880,20],{}," condition at the end to filter which items are included.",[712,10883,10885],{"id":10884},"what-happens-if-two-items-create-the-same-key","What happens if two items create the same key?",[14,10887,10888],{},"The later value replaces the earlier one because dictionary keys must be unique.",[712,10890,10892],{"id":10891},"should-beginners-use-dictionary-comprehensions","Should beginners use dictionary comprehensions?",[14,10894,10895],{},"Yes, for simple cases. If the code becomes hard to read, use a normal loop instead.",[31,10897,2342],{"id":2341},[195,10899,10900,10905,10909,10913,10918],{},[198,10901,10902],{},[236,10903,10904],{"href":9238},"List comprehensions in Python explained",[198,10906,10907],{},[236,10908,8557],{"href":8556},[198,10910,10911],{},[236,10912,9421],{"href":9420},[198,10914,10915],{},[236,10916,10917],{"href":10855},"How to loop through a dictionary in Python",[198,10919,10920],{},[236,10921,10922],{"href":10859},"How to check if a key exists in a dictionary in Python",[14,10924,10925],{},"Compare a dictionary comprehension with a normal loop, then practice by turning a small list or dictionary into a new dictionary. That is the fastest way to get comfortable with this syntax.",[2367,10927,10928],{},"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":41,"searchDepth":63,"depth":63,"links":10930},[10931,10932,10933,10936,10937,10938,10942,10948,10953,10954,10960],{"id":33,"depth":63,"text":34},{"id":8654,"depth":63,"text":8655},{"id":8706,"depth":63,"text":8707,"children":10934},[10935],{"id":8803,"depth":70,"text":8804},{"id":8933,"depth":63,"text":8934},{"id":9242,"depth":63,"text":9243},{"id":9582,"depth":63,"text":9583,"children":10939},[10940,10941],{"id":9617,"depth":70,"text":9618},{"id":9753,"depth":70,"text":9754},{"id":1336,"depth":63,"text":1337,"children":10943},[10944,10945,10946,10947],{"id":9850,"depth":70,"text":9851},{"id":9962,"depth":70,"text":9963},{"id":10123,"depth":70,"text":10124},{"id":10133,"depth":70,"text":10134},{"id":10265,"depth":63,"text":10266,"children":10949},[10950,10951,10952],{"id":10286,"depth":70,"text":10287},{"id":10418,"depth":70,"text":10419},{"id":10526,"depth":70,"text":10527},{"id":2011,"depth":63,"text":2012},{"id":2280,"depth":63,"text":2281,"children":10955},[10956,10957,10958,10959],{"id":10865,"depth":70,"text":10866},{"id":10874,"depth":70,"text":10875},{"id":10884,"depth":70,"text":10885},{"id":10891,"depth":70,"text":10892},{"id":2341,"depth":63,"text":2342},"Master dictionary comprehensions explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fdictionary-comprehensions-explained",{"title":8539,"description":10961},"learn\u002Fdictionary-comprehensions-explained","o-dBEl7cqvqkhYqtP0C9gL1I_YZwp73BAAUG0_Fp5v4",{"id":10968,"title":10969,"body":10970,"description":12895,"extension":2399,"meta":12896,"navigation":66,"path":12897,"seo":12898,"stem":12899,"__hash__":12900},"content\u002Flearn\u002Ffunction-parameters-and-arguments-in-python.md","Function Parameters and Arguments in Python",{"type":7,"value":10971,"toc":12858},[10972,10975,10978,10986,10989,10991,11044,11064,11067,11069,11072,11083,11088,11092,11095,11130,11136,11147,11150,11170,11173,11185,11188,11200,11203,11207,11210,11213,11216,11227,11229,11310,11313,11318,11322,11325,11377,11381,11400,11406,11408,11417,11420,11482,11484,11493,11505,11509,11512,11566,11568,11577,11579,11595,11598,11600,11673,11675,11684,11687,11714,11716,11725,11728,11730,11735,11737,11793,11795,11804,11807,11824,11827,11846,11848,11859,11862,11865,11867,11872,11874,11931,11933,11941,11944,12029,12031,12048,12051,12082,12084,12098,12101,12108,12112,12115,12118,12186,12188,12202,12205,12225,12231,12236,12238,12240,12244,12293,12296,12304,12308,12369,12375,12379,12382,12443,12451,12454,12458,12461,12473,12476,12480,12483,12490,12495,12527,12530,12540,12544,12547,12566,12569,12579,12583,12586,12590,12593,12611,12614,12641,12644,12646,12649,12668,12678,12682,12685,12739,12742,12745,12761,12764,12783,12786,12788,12792,12795,12799,12802,12806,12812,12816,12819,12823,12826,12828,12855],[10,10973,10969],{"id":10974},"function-parameters-and-arguments-in-python",[14,10976,10977],{},"Function parameters and arguments are closely related, but they are not the same thing.",[14,10979,2419,10980,10982,10983,10985],{},[2421,10981,5787],{}," is a name written in a function definition. An ",[2421,10984,5791],{}," is the actual value you pass to the function when you call it.",[14,10987,10988],{},"Understanding this difference makes it much easier to read Python functions, write your own functions, and fix function call errors.",[31,10990,34],{"id":33},[36,10992,10994],{"className":38,"code":10993,"language":40,"meta":41,"style":41},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Maya\")\n",[18,10995,10996,11008,11026,11030],{"__ignoreMap":41},[45,10997,10998,11000,11002,11004,11006],{"class":47,"line":48},[45,10999,2757],{"class":2440},[45,11001,2936],{"class":2455},[45,11003,96],{"class":85},[45,11005,3096],{"class":3082},[45,11007,2464],{"class":85},[45,11009,11010,11012,11014,11016,11018,11020,11022,11024],{"class":47,"line":63},[45,11011,93],{"class":92},[45,11013,96],{"class":85},[45,11015,100],{"class":99},[45,11017,3226],{"class":103},[45,11019,100],{"class":99},[45,11021,3079],{"class":85},[45,11023,3083],{"class":2070},[45,11025,109],{"class":85},[45,11027,11028],{"class":47,"line":70},[45,11029,67],{"emptyLinePlaceholder":66},[45,11031,11032,11034,11036,11038,11040,11042],{"class":47,"line":89},[45,11033,2982],{"class":2070},[45,11035,96],{"class":85},[45,11037,100],{"class":99},[45,11039,5737],{"class":103},[45,11041,100],{"class":99},[45,11043,109],{"class":85},[195,11045,11046,11055],{},[198,11047,11048,11051,11052,11054],{},[2421,11049,11050],{},"Parameter:"," ",[18,11053,3096],{}," in the function definition",[198,11056,11057,11051,11060,11063],{},[2421,11058,11059],{},"Argument:",[18,11061,11062],{},"\"Maya\""," in the function call",[11065,11066],"hr",{},[31,11068,6600],{"id":6599},[14,11070,11071],{},"By the end of this page, you should understand that:",[195,11073,11074,11077,11080],{},[198,11075,11076],{},"Parameters are variable names in a function definition",[198,11078,11079],{},"Arguments are actual values passed when calling the function",[198,11081,11082],{},"You can read both parts of a simple function with confidence",[14,11084,11085,11086,240],{},"If you are new to functions in general, see ",[236,11087,5879],{"href":4579},[31,11089,11091],{"id":11090},"parameter-vs-argument","Parameter vs argument",[14,11093,11094],{},"Look at this function:",[36,11096,11098],{"className":38,"code":11097,"language":40,"meta":41,"style":41},"def add(a, b):\n    return a + b\n",[18,11099,11100,11118],{"__ignoreMap":41},[45,11101,11102,11104,11107,11109,11111,11113,11116],{"class":47,"line":48},[45,11103,2757],{"class":2440},[45,11105,11106],{"class":2455}," add",[45,11108,96],{"class":85},[45,11110,236],{"class":3082},[45,11112,3079],{"class":85},[45,11114,11115],{"class":3082}," b",[45,11117,2464],{"class":85},[45,11119,11120,11122,11125,11127],{"class":47,"line":63},[45,11121,4681],{"class":73},[45,11123,11124],{"class":51}," a ",[45,11126,8850],{"class":55},[45,11128,11129],{"class":51}," b\n",[14,11131,11132,11133,11135],{},"In the ",[18,11134,2757],{}," line:",[195,11137,11138,11143],{},[198,11139,11140,11142],{},[18,11141,236],{}," is a parameter",[198,11144,11145,11142],{},[18,11146,10384],{},[14,11148,11149],{},"Now look at the function call:",[36,11151,11153],{"className":38,"code":11152,"language":40,"meta":41,"style":41},"add(2, 3)\n",[18,11154,11155],{"__ignoreMap":41},[45,11156,11157,11160,11162,11164,11166,11168],{"class":47,"line":48},[45,11158,11159],{"class":2070},"add",[45,11161,96],{"class":85},[45,11163,9367],{"class":59},[45,11165,3079],{"class":85},[45,11167,777],{"class":59},[45,11169,109],{"class":85},[14,11171,11172],{},"In this call:",[195,11174,11175,11180],{},[198,11176,11177,11179],{},[18,11178,9367],{}," is an argument",[198,11181,11182,11179],{},[18,11183,11184],{},"3",[14,11186,11187],{},"Python takes the arguments and assigns them to the parameters:",[195,11189,11190,11195],{},[198,11191,11192],{},[18,11193,11194],{},"a = 2",[198,11196,11197],{},[18,11198,11199],{},"b = 3",[14,11201,11202],{},"So the function can use those values inside its body.",[31,11204,11206],{"id":11205},"why-functions-use-parameters","Why functions use parameters",[14,11208,11209],{},"Parameters make functions flexible.",[14,11211,11212],{},"Without parameters, you would need to write a new function for every value. With parameters, one function can work with many different inputs.",[14,11214,11215],{},"Parameters are useful because they:",[195,11217,11218,11221,11224],{},[198,11219,11220],{},"Make one function reusable with different values",[198,11222,11223],{},"Avoid repeating very similar code",[198,11225,11226],{},"Let input come from variables, user input, or calculations",[14,11228,2115],{},[36,11230,11232],{"className":38,"code":11231,"language":40,"meta":41,"style":41},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Maya\")\ngreet(\"Sam\")\ngreet(\"Ava\")\n",[18,11233,11234,11246,11264,11268,11282,11296],{"__ignoreMap":41},[45,11235,11236,11238,11240,11242,11244],{"class":47,"line":48},[45,11237,2757],{"class":2440},[45,11239,2936],{"class":2455},[45,11241,96],{"class":85},[45,11243,3096],{"class":3082},[45,11245,2464],{"class":85},[45,11247,11248,11250,11252,11254,11256,11258,11260,11262],{"class":47,"line":63},[45,11249,93],{"class":92},[45,11251,96],{"class":85},[45,11253,100],{"class":99},[45,11255,3226],{"class":103},[45,11257,100],{"class":99},[45,11259,3079],{"class":85},[45,11261,3083],{"class":2070},[45,11263,109],{"class":85},[45,11265,11266],{"class":47,"line":70},[45,11267,67],{"emptyLinePlaceholder":66},[45,11269,11270,11272,11274,11276,11278,11280],{"class":47,"line":89},[45,11271,2982],{"class":2070},[45,11273,96],{"class":85},[45,11275,100],{"class":99},[45,11277,5737],{"class":103},[45,11279,100],{"class":99},[45,11281,109],{"class":85},[45,11283,11284,11286,11288,11290,11292,11294],{"class":47,"line":112},[45,11285,2982],{"class":2070},[45,11287,96],{"class":85},[45,11289,100],{"class":99},[45,11291,1568],{"class":103},[45,11293,100],{"class":99},[45,11295,109],{"class":85},[45,11297,11298,11300,11302,11304,11306,11308],{"class":47,"line":126},[45,11299,2982],{"class":2070},[45,11301,96],{"class":85},[45,11303,100],{"class":99},[45,11305,3162],{"class":103},[45,11307,100],{"class":99},[45,11309,109],{"class":85},[14,11311,11312],{},"The same function works for different names because it uses a parameter.",[14,11314,11315,11316,240],{},"If you want to practice building functions from scratch, see ",[236,11317,5892],{"href":5891},[31,11319,11321],{"id":11320},"basic-example-with-one-parameter","Basic example with one parameter",[14,11323,11324],{},"Here is a simple function with one parameter:",[36,11326,11327],{"className":38,"code":10993,"language":40,"meta":41,"style":41},[18,11328,11329,11341,11359,11363],{"__ignoreMap":41},[45,11330,11331,11333,11335,11337,11339],{"class":47,"line":48},[45,11332,2757],{"class":2440},[45,11334,2936],{"class":2455},[45,11336,96],{"class":85},[45,11338,3096],{"class":3082},[45,11340,2464],{"class":85},[45,11342,11343,11345,11347,11349,11351,11353,11355,11357],{"class":47,"line":63},[45,11344,93],{"class":92},[45,11346,96],{"class":85},[45,11348,100],{"class":99},[45,11350,3226],{"class":103},[45,11352,100],{"class":99},[45,11354,3079],{"class":85},[45,11356,3083],{"class":2070},[45,11358,109],{"class":85},[45,11360,11361],{"class":47,"line":70},[45,11362,67],{"emptyLinePlaceholder":66},[45,11364,11365,11367,11369,11371,11373,11375],{"class":47,"line":89},[45,11366,2982],{"class":2070},[45,11368,96],{"class":85},[45,11370,100],{"class":99},[45,11372,5737],{"class":103},[45,11374,100],{"class":99},[45,11376,109],{"class":85},[712,11378,11380],{"id":11379},"how-it-works","How it works",[195,11382,11383,11388,11393],{},[198,11384,11385,11387],{},[18,11386,3096],{}," is the parameter",[198,11389,11390,11392],{},[18,11391,11062],{}," is the argument",[198,11394,11395,11396,7076,11398],{},"When the function runs, Python gives the value ",[18,11397,11062],{},[18,11399,3096],{},[14,11401,11402,11403,11405],{},"Inside the function, ",[18,11404,3096],{}," behaves like a normal variable.",[712,11407,5452],{"id":5451},[36,11409,11411],{"className":38,"code":11410,"language":40,"meta":41,"style":41},"Hello Maya\n",[18,11412,11413],{"__ignoreMap":41},[45,11414,11415],{"class":47,"line":48},[45,11416,11410],{"class":51},[14,11418,11419],{},"You can also pass a variable instead of writing the value directly:",[36,11421,11423],{"className":38,"code":11422,"language":40,"meta":41,"style":41},"def greet(name):\n    print(\"Hello\", name)\n\nperson = \"Leo\"\ngreet(person)\n",[18,11424,11425,11437,11455,11459,11472],{"__ignoreMap":41},[45,11426,11427,11429,11431,11433,11435],{"class":47,"line":48},[45,11428,2757],{"class":2440},[45,11430,2936],{"class":2455},[45,11432,96],{"class":85},[45,11434,3096],{"class":3082},[45,11436,2464],{"class":85},[45,11438,11439,11441,11443,11445,11447,11449,11451,11453],{"class":47,"line":63},[45,11440,93],{"class":92},[45,11442,96],{"class":85},[45,11444,100],{"class":99},[45,11446,3226],{"class":103},[45,11448,100],{"class":99},[45,11450,3079],{"class":85},[45,11452,3083],{"class":2070},[45,11454,109],{"class":85},[45,11456,11457],{"class":47,"line":70},[45,11458,67],{"emptyLinePlaceholder":66},[45,11460,11461,11463,11465,11467,11470],{"class":47,"line":89},[45,11462,2966],{"class":51},[45,11464,56],{"class":55},[45,11466,1565],{"class":99},[45,11468,11469],{"class":103},"Leo",[45,11471,1571],{"class":99},[45,11473,11474,11476,11478,11480],{"class":47,"line":112},[45,11475,2982],{"class":2070},[45,11477,96],{"class":85},[45,11479,2977],{"class":2070},[45,11481,109],{"class":85},[14,11483,177],{},[36,11485,11487],{"className":38,"code":11486,"language":40,"meta":41,"style":41},"Hello Leo\n",[18,11488,11489],{"__ignoreMap":41},[45,11490,11491],{"class":47,"line":48},[45,11492,11486],{"class":51},[14,11494,11495,11496,11498,11499,11501,11502,11504],{},"The outside variable is called ",[18,11497,2977],{},", but the parameter is called ",[18,11500,3096],{},". That is fine. The names do ",[2421,11503,2857],{}," need to match.",[31,11506,11508],{"id":11507},"example-with-multiple-parameters","Example with multiple parameters",[14,11510,11511],{},"Functions can have more than one parameter.",[36,11513,11515],{"className":38,"code":11514,"language":40,"meta":41,"style":41},"def add(a, b):\n    print(a + b)\n\nadd(2, 3)\n",[18,11516,11517,11533,11548,11552],{"__ignoreMap":41},[45,11518,11519,11521,11523,11525,11527,11529,11531],{"class":47,"line":48},[45,11520,2757],{"class":2440},[45,11522,11106],{"class":2455},[45,11524,96],{"class":85},[45,11526,236],{"class":3082},[45,11528,3079],{"class":85},[45,11530,11115],{"class":3082},[45,11532,2464],{"class":85},[45,11534,11535,11537,11539,11542,11544,11546],{"class":47,"line":63},[45,11536,93],{"class":92},[45,11538,96],{"class":85},[45,11540,11541],{"class":2070},"a ",[45,11543,8850],{"class":55},[45,11545,11115],{"class":2070},[45,11547,109],{"class":85},[45,11549,11550],{"class":47,"line":70},[45,11551,67],{"emptyLinePlaceholder":66},[45,11553,11554,11556,11558,11560,11562,11564],{"class":47,"line":89},[45,11555,11159],{"class":2070},[45,11557,96],{"class":85},[45,11559,9367],{"class":59},[45,11561,3079],{"class":85},[45,11563,777],{"class":59},[45,11565,109],{"class":85},[14,11567,177],{},[36,11569,11571],{"className":38,"code":11570,"language":40,"meta":41,"style":41},"5\n",[18,11572,11573],{"__ignoreMap":41},[45,11574,11575],{"class":47,"line":48},[45,11576,11570],{"class":59},[14,11578,6680],{},[195,11580,11581,11588],{},[198,11582,11583,11585,11586],{},[18,11584,236],{}," gets the first argument: ",[18,11587,9367],{},[198,11589,11590,11592,11593],{},[18,11591,10384],{}," gets the second argument: ",[18,11594,11184],{},[14,11596,11597],{},"The order matters with this style of function call.",[14,11599,2115],{},[36,11601,11603],{"className":38,"code":11602,"language":40,"meta":41,"style":41},"def greet(first_name, last_name):\n    print(\"Hello\", first_name, last_name)\n\ngreet(\"Maya\", \"Lopez\")\n",[18,11604,11605,11623,11646,11650],{"__ignoreMap":41},[45,11606,11607,11609,11611,11613,11616,11618,11621],{"class":47,"line":48},[45,11608,2757],{"class":2440},[45,11610,2936],{"class":2455},[45,11612,96],{"class":85},[45,11614,11615],{"class":3082},"first_name",[45,11617,3079],{"class":85},[45,11619,11620],{"class":3082}," last_name",[45,11622,2464],{"class":85},[45,11624,11625,11627,11629,11631,11633,11635,11637,11640,11642,11644],{"class":47,"line":63},[45,11626,93],{"class":92},[45,11628,96],{"class":85},[45,11630,100],{"class":99},[45,11632,3226],{"class":103},[45,11634,100],{"class":99},[45,11636,3079],{"class":85},[45,11638,11639],{"class":2070}," first_name",[45,11641,3079],{"class":85},[45,11643,11620],{"class":2070},[45,11645,109],{"class":85},[45,11647,11648],{"class":47,"line":70},[45,11649,67],{"emptyLinePlaceholder":66},[45,11651,11652,11654,11656,11658,11660,11662,11664,11666,11669,11671],{"class":47,"line":89},[45,11653,2982],{"class":2070},[45,11655,96],{"class":85},[45,11657,100],{"class":99},[45,11659,5737],{"class":103},[45,11661,100],{"class":99},[45,11663,3079],{"class":85},[45,11665,1565],{"class":99},[45,11667,11668],{"class":103},"Lopez",[45,11670,100],{"class":99},[45,11672,109],{"class":85},[14,11674,177],{},[36,11676,11678],{"className":38,"code":11677,"language":40,"meta":41,"style":41},"Hello Maya Lopez\n",[18,11679,11680],{"__ignoreMap":41},[45,11681,11682],{"class":47,"line":48},[45,11683,11677],{"class":51},[14,11685,11686],{},"If you reverse the arguments:",[36,11688,11690],{"className":38,"code":11689,"language":40,"meta":41,"style":41},"greet(\"Lopez\", \"Maya\")\n",[18,11691,11692],{"__ignoreMap":41},[45,11693,11694,11696,11698,11700,11702,11704,11706,11708,11710,11712],{"class":47,"line":48},[45,11695,2982],{"class":2070},[45,11697,96],{"class":85},[45,11699,100],{"class":99},[45,11701,11668],{"class":103},[45,11703,100],{"class":99},[45,11705,3079],{"class":85},[45,11707,1565],{"class":99},[45,11709,5737],{"class":103},[45,11711,100],{"class":99},[45,11713,109],{"class":85},[14,11715,177],{},[36,11717,11719],{"className":38,"code":11718,"language":40,"meta":41,"style":41},"Hello Lopez Maya\n",[18,11720,11721],{"__ignoreMap":41},[45,11722,11723],{"class":47,"line":48},[45,11724,11718],{"class":51},[14,11726,11727],{},"The function still runs, but the result changes because the arguments were passed in a different order.",[31,11729,6967],{"id":6966},[14,11731,11732,11733,240],{},"When arguments are matched by order, they are called ",[2421,11734,8076],{},[14,11736,466],{},[36,11738,11740],{"className":38,"code":11739,"language":40,"meta":41,"style":41},"def subtract(a, b):\n    print(a - b)\n\nsubtract(10, 4)\n",[18,11741,11742,11759,11773,11777],{"__ignoreMap":41},[45,11743,11744,11746,11749,11751,11753,11755,11757],{"class":47,"line":48},[45,11745,2757],{"class":2440},[45,11747,11748],{"class":2455}," subtract",[45,11750,96],{"class":85},[45,11752,236],{"class":3082},[45,11754,3079],{"class":85},[45,11756,11115],{"class":3082},[45,11758,2464],{"class":85},[45,11760,11761,11763,11765,11767,11769,11771],{"class":47,"line":63},[45,11762,93],{"class":92},[45,11764,96],{"class":85},[45,11766,11541],{"class":2070},[45,11768,218],{"class":55},[45,11770,11115],{"class":2070},[45,11772,109],{"class":85},[45,11774,11775],{"class":47,"line":70},[45,11776,67],{"emptyLinePlaceholder":66},[45,11778,11779,11782,11784,11787,11789,11791],{"class":47,"line":89},[45,11780,11781],{"class":2070},"subtract",[45,11783,96],{"class":85},[45,11785,11786],{"class":59},"10",[45,11788,3079],{"class":85},[45,11790,8591],{"class":59},[45,11792,109],{"class":85},[14,11794,177],{},[36,11796,11798],{"className":38,"code":11797,"language":40,"meta":41,"style":41},"6\n",[18,11799,11800],{"__ignoreMap":41},[45,11801,11802],{"class":47,"line":48},[45,11803,11797],{"class":59},[14,11805,11806],{},"Python matches them like this:",[195,11808,11809,11817],{},[198,11810,11811,11812,11814,11815],{},"first argument ",[18,11813,11786],{}," → parameter ",[18,11816,236],{},[198,11818,11819,11820,11814,11822],{},"second argument ",[18,11821,3584],{},[18,11823,10384],{},[14,11825,11826],{},"If you switch the order:",[36,11828,11830],{"className":38,"code":11829,"language":40,"meta":41,"style":41},"subtract(4, 10)\n",[18,11831,11832],{"__ignoreMap":41},[45,11833,11834,11836,11838,11840,11842,11844],{"class":47,"line":48},[45,11835,11781],{"class":2070},[45,11837,96],{"class":85},[45,11839,3584],{"class":59},[45,11841,3079],{"class":85},[45,11843,1190],{"class":59},[45,11845,109],{"class":85},[14,11847,177],{},[36,11849,11851],{"className":38,"code":11850,"language":40,"meta":41,"style":41},"-6\n",[18,11852,11853],{"__ignoreMap":41},[45,11854,11855,11857],{"class":47,"line":48},[45,11856,218],{"class":55},[45,11858,11797],{"class":59},[14,11860,11861],{},"That is why order matters with positional arguments.",[14,11863,11864],{},"For beginners, positional arguments are usually the easiest way to start calling functions.",[31,11866,7089],{"id":7088},[14,11868,11869,11870,240],{},"You can also pass arguments by parameter name. These are called ",[2421,11871,6593],{},[14,11873,466],{},[36,11875,11877],{"className":38,"code":11876,"language":40,"meta":41,"style":41},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(name=\"Sam\")\n",[18,11878,11879,11891,11909,11913],{"__ignoreMap":41},[45,11880,11881,11883,11885,11887,11889],{"class":47,"line":48},[45,11882,2757],{"class":2440},[45,11884,2936],{"class":2455},[45,11886,96],{"class":85},[45,11888,3096],{"class":3082},[45,11890,2464],{"class":85},[45,11892,11893,11895,11897,11899,11901,11903,11905,11907],{"class":47,"line":63},[45,11894,93],{"class":92},[45,11896,96],{"class":85},[45,11898,100],{"class":99},[45,11900,3226],{"class":103},[45,11902,100],{"class":99},[45,11904,3079],{"class":85},[45,11906,3083],{"class":2070},[45,11908,109],{"class":85},[45,11910,11911],{"class":47,"line":70},[45,11912,67],{"emptyLinePlaceholder":66},[45,11914,11915,11917,11919,11921,11923,11925,11927,11929],{"class":47,"line":89},[45,11916,2982],{"class":2070},[45,11918,96],{"class":85},[45,11920,3096],{"class":6503},[45,11922,56],{"class":55},[45,11924,100],{"class":99},[45,11926,1568],{"class":103},[45,11928,100],{"class":99},[45,11930,109],{"class":85},[14,11932,177],{},[36,11934,11935],{"className":38,"code":6557,"language":40,"meta":41,"style":41},[18,11936,11937],{"__ignoreMap":41},[45,11938,11939],{"class":47,"line":48},[45,11940,6557],{"class":51},[14,11942,11943],{},"This can be easier to read, especially when a function has several parameters.",[36,11945,11947],{"className":38,"code":11946,"language":40,"meta":41,"style":41},"def introduce(name, age):\n    print(name, \"is\", age, \"years old\")\n\nintroduce(name=\"Maya\", age=12)\n",[18,11948,11949,11966,11997,12001],{"__ignoreMap":41},[45,11950,11951,11953,11956,11958,11960,11962,11964],{"class":47,"line":48},[45,11952,2757],{"class":2440},[45,11954,11955],{"class":2455}," introduce",[45,11957,96],{"class":85},[45,11959,3096],{"class":3082},[45,11961,3079],{"class":85},[45,11963,3305],{"class":3082},[45,11965,2464],{"class":85},[45,11967,11968,11970,11972,11974,11976,11978,11980,11982,11984,11986,11988,11990,11993,11995],{"class":47,"line":63},[45,11969,93],{"class":92},[45,11971,96],{"class":85},[45,11973,3096],{"class":2070},[45,11975,3079],{"class":85},[45,11977,1565],{"class":99},[45,11979,1285],{"class":103},[45,11981,100],{"class":99},[45,11983,3079],{"class":85},[45,11985,3305],{"class":2070},[45,11987,3079],{"class":85},[45,11989,1565],{"class":99},[45,11991,11992],{"class":103},"years old",[45,11994,100],{"class":99},[45,11996,109],{"class":85},[45,11998,11999],{"class":47,"line":70},[45,12000,67],{"emptyLinePlaceholder":66},[45,12002,12003,12006,12008,12010,12012,12014,12016,12018,12020,12022,12024,12027],{"class":47,"line":89},[45,12004,12005],{"class":2070},"introduce",[45,12007,96],{"class":85},[45,12009,3096],{"class":6503},[45,12011,56],{"class":55},[45,12013,100],{"class":99},[45,12015,5737],{"class":103},[45,12017,100],{"class":99},[45,12019,3079],{"class":85},[45,12021,3305],{"class":6503},[45,12023,56],{"class":55},[45,12025,12026],{"class":59},"12",[45,12028,109],{"class":85},[14,12030,177],{},[36,12032,12034],{"className":38,"code":12033,"language":40,"meta":41,"style":41},"Maya is 12 years old\n",[18,12035,12036],{"__ignoreMap":41},[45,12037,12038,12041,12043,12045],{"class":47,"line":48},[45,12039,12040],{"class":51},"Maya ",[45,12042,1285],{"class":55},[45,12044,8900],{"class":59},[45,12046,12047],{"class":51}," years old\n",[14,12049,12050],{},"Keyword arguments also help avoid mistakes with argument order:",[36,12052,12054],{"className":38,"code":12053,"language":40,"meta":41,"style":41},"introduce(age=12, name=\"Maya\")\n",[18,12055,12056],{"__ignoreMap":41},[45,12057,12058,12060,12062,12064,12066,12068,12070,12072,12074,12076,12078,12080],{"class":47,"line":48},[45,12059,12005],{"class":2070},[45,12061,96],{"class":85},[45,12063,2082],{"class":6503},[45,12065,56],{"class":55},[45,12067,12026],{"class":59},[45,12069,3079],{"class":85},[45,12071,3083],{"class":6503},[45,12073,56],{"class":55},[45,12075,100],{"class":99},[45,12077,5737],{"class":103},[45,12079,100],{"class":99},[45,12081,109],{"class":85},[14,12083,177],{},[36,12085,12086],{"className":38,"code":12033,"language":40,"meta":41,"style":41},[18,12087,12088],{"__ignoreMap":41},[45,12089,12090,12092,12094,12096],{"class":47,"line":48},[45,12091,12040],{"class":51},[45,12093,1285],{"class":55},[45,12095,8900],{"class":59},[45,12097,12047],{"class":51},[14,12099,12100],{},"Even though the order in the call changed, Python still knows which value belongs to which parameter because the names were included.",[14,12102,12103,12104,240],{},"To learn more, see ",[236,12105,12107],{"href":12106},"\u002Flearn\u002Fdefault-and-keyword-arguments-explained\u002F","default and keyword arguments explained",[31,12109,12111],{"id":12110},"default-parameter-values","Default parameter values",[14,12113,12114],{},"A parameter can have a default value.",[14,12116,12117],{},"That means the function can still run even if you do not pass an argument for that parameter.",[36,12119,12121],{"className":38,"code":12120,"language":40,"meta":41,"style":41},"def greet(name=\"friend\"):\n    print(\"Hello\", name)\n\ngreet()\ngreet(\"Maya\")\n",[18,12122,12123,12144,12162,12166,12172],{"__ignoreMap":41},[45,12124,12125,12127,12129,12131,12133,12135,12137,12140,12142],{"class":47,"line":48},[45,12126,2757],{"class":2440},[45,12128,2936],{"class":2455},[45,12130,96],{"class":85},[45,12132,3096],{"class":3082},[45,12134,56],{"class":55},[45,12136,100],{"class":99},[45,12138,12139],{"class":103},"friend",[45,12141,100],{"class":99},[45,12143,2464],{"class":85},[45,12145,12146,12148,12150,12152,12154,12156,12158,12160],{"class":47,"line":63},[45,12147,93],{"class":92},[45,12149,96],{"class":85},[45,12151,100],{"class":99},[45,12153,3226],{"class":103},[45,12155,100],{"class":99},[45,12157,3079],{"class":85},[45,12159,3083],{"class":2070},[45,12161,109],{"class":85},[45,12163,12164],{"class":47,"line":70},[45,12165,67],{"emptyLinePlaceholder":66},[45,12167,12168,12170],{"class":47,"line":89},[45,12169,2982],{"class":2070},[45,12171,2495],{"class":85},[45,12173,12174,12176,12178,12180,12182,12184],{"class":47,"line":112},[45,12175,2982],{"class":2070},[45,12177,96],{"class":85},[45,12179,100],{"class":99},[45,12181,5737],{"class":103},[45,12183,100],{"class":99},[45,12185,109],{"class":85},[14,12187,177],{},[36,12189,12191],{"className":38,"code":12190,"language":40,"meta":41,"style":41},"Hello friend\nHello Maya\n",[18,12192,12193,12198],{"__ignoreMap":41},[45,12194,12195],{"class":47,"line":48},[45,12196,12197],{"class":51},"Hello friend\n",[45,12199,12200],{"class":47,"line":63},[45,12201,11410],{"class":51},[14,12203,12204],{},"How this works:",[195,12206,12207,12216],{},[198,12208,12209,12210,12212,12213],{},"If you call ",[18,12211,2583],{}," with no argument, Python uses the default value ",[18,12214,12215],{},"\"friend\"",[198,12217,12209,12218,12221,12222,12224],{},[18,12219,12220],{},"greet(\"Maya\")",", Python uses ",[18,12223,11062],{}," instead",[14,12226,12227,12228,12230],{},"Default values belong to the ",[2421,12229,5787],{},", not the argument.",[14,12232,12233,12234,240],{},"If you want a full beginner guide to this topic, read ",[236,12235,12107],{"href":12106},[31,12237,1337],{"id":1336},[14,12239,9847],{},[712,12241,12243],{"id":12242},"calling-a-function-with-too-few-arguments","Calling a function with too few arguments",[36,12245,12247],{"className":38,"code":12246,"language":40,"meta":41,"style":41},"def add(a, b):\n    print(a + b)\n\nadd(2)\n",[18,12248,12249,12265,12279,12283],{"__ignoreMap":41},[45,12250,12251,12253,12255,12257,12259,12261,12263],{"class":47,"line":48},[45,12252,2757],{"class":2440},[45,12254,11106],{"class":2455},[45,12256,96],{"class":85},[45,12258,236],{"class":3082},[45,12260,3079],{"class":85},[45,12262,11115],{"class":3082},[45,12264,2464],{"class":85},[45,12266,12267,12269,12271,12273,12275,12277],{"class":47,"line":63},[45,12268,93],{"class":92},[45,12270,96],{"class":85},[45,12272,11541],{"class":2070},[45,12274,8850],{"class":55},[45,12276,11115],{"class":2070},[45,12278,109],{"class":85},[45,12280,12281],{"class":47,"line":70},[45,12282,67],{"emptyLinePlaceholder":66},[45,12284,12285,12287,12289,12291],{"class":47,"line":89},[45,12286,11159],{"class":2070},[45,12288,96],{"class":85},[45,12290,9367],{"class":59},[45,12292,109],{"class":85},[14,12294,12295],{},"This causes an error because the function expects two arguments, but only one was given.",[14,12297,12298,12299,9417,12301,240],{},"You will usually get a ",[18,12300,7711],{},[236,12302,12303],{"href":7724},"how to fix missing required positional argument errors",[712,12305,12307],{"id":12306},"calling-a-function-with-too-many-arguments","Calling a function with too many arguments",[36,12309,12311],{"className":38,"code":12310,"language":40,"meta":41,"style":41},"def greet(name):\n    print(\"Hello\", name)\n\ngreet(\"Maya\", \"Sam\")\n",[18,12312,12313,12325,12343,12347],{"__ignoreMap":41},[45,12314,12315,12317,12319,12321,12323],{"class":47,"line":48},[45,12316,2757],{"class":2440},[45,12318,2936],{"class":2455},[45,12320,96],{"class":85},[45,12322,3096],{"class":3082},[45,12324,2464],{"class":85},[45,12326,12327,12329,12331,12333,12335,12337,12339,12341],{"class":47,"line":63},[45,12328,93],{"class":92},[45,12330,96],{"class":85},[45,12332,100],{"class":99},[45,12334,3226],{"class":103},[45,12336,100],{"class":99},[45,12338,3079],{"class":85},[45,12340,3083],{"class":2070},[45,12342,109],{"class":85},[45,12344,12345],{"class":47,"line":70},[45,12346,67],{"emptyLinePlaceholder":66},[45,12348,12349,12351,12353,12355,12357,12359,12361,12363,12365,12367],{"class":47,"line":89},[45,12350,2982],{"class":2070},[45,12352,96],{"class":85},[45,12354,100],{"class":99},[45,12356,5737],{"class":103},[45,12358,100],{"class":99},[45,12360,3079],{"class":85},[45,12362,1565],{"class":99},[45,12364,1568],{"class":103},[45,12366,100],{"class":99},[45,12368,109],{"class":85},[14,12370,12371,12372,12374],{},"This also causes a ",[18,12373,7711],{}," because the function only expects one argument.",[712,12376,12378],{"id":12377},"confusing-parameter-names-with-outside-variable-names","Confusing parameter names with outside variable names",[14,12380,12381],{},"This works:",[36,12383,12385],{"className":38,"code":12384,"language":40,"meta":41,"style":41},"def greet(name):\n    print(\"Hello\", name)\n\nperson = \"Maya\"\ngreet(person)\n",[18,12386,12387,12399,12417,12421,12433],{"__ignoreMap":41},[45,12388,12389,12391,12393,12395,12397],{"class":47,"line":48},[45,12390,2757],{"class":2440},[45,12392,2936],{"class":2455},[45,12394,96],{"class":85},[45,12396,3096],{"class":3082},[45,12398,2464],{"class":85},[45,12400,12401,12403,12405,12407,12409,12411,12413,12415],{"class":47,"line":63},[45,12402,93],{"class":92},[45,12404,96],{"class":85},[45,12406,100],{"class":99},[45,12408,3226],{"class":103},[45,12410,100],{"class":99},[45,12412,3079],{"class":85},[45,12414,3083],{"class":2070},[45,12416,109],{"class":85},[45,12418,12419],{"class":47,"line":70},[45,12420,67],{"emptyLinePlaceholder":66},[45,12422,12423,12425,12427,12429,12431],{"class":47,"line":89},[45,12424,2966],{"class":51},[45,12426,56],{"class":55},[45,12428,1565],{"class":99},[45,12430,5737],{"class":103},[45,12432,1571],{"class":99},[45,12434,12435,12437,12439,12441],{"class":47,"line":112},[45,12436,2982],{"class":2070},[45,12438,96],{"class":85},[45,12440,2977],{"class":2070},[45,12442,109],{"class":85},[14,12444,12445,12446,12448,12449,240],{},"The parameter is ",[18,12447,3096],{},", but the outside variable is ",[18,12450,2977],{},[14,12452,12453],{},"They do not need to match.",[712,12455,12457],{"id":12456},"using-the-terms-like-they-mean-exactly-the-same-thing","Using the terms like they mean exactly the same thing",[14,12459,12460],{},"In everyday conversation, many people use these words loosely. But in Python learning, the distinction helps:",[195,12462,12463,12468],{},[198,12464,12465,12467],{},[2421,12466,5787],{}," = name in the function definition",[198,12469,12470,12472],{},[2421,12471,5791],{}," = value in the function call",[14,12474,12475],{},"That small difference makes function code easier to read.",[31,12477,12479],{"id":12478},"how-to-read-function-definitions-and-calls","How to read function definitions and calls",[14,12481,12482],{},"When you see a function, use this simple process:",[712,12484,12486,12487,12489],{"id":12485},"_1-look-at-the-def-line","1. Look at the ",[18,12488,2757],{}," line",[14,12491,1732,12492,12494],{},[18,12493,2757],{}," line shows the parameters.",[36,12496,12498],{"className":38,"code":12497,"language":40,"meta":41,"style":41},"def area(width, height):\n    return width * height\n",[18,12499,12500,12516],{"__ignoreMap":41},[45,12501,12502,12504,12506,12508,12510,12512,12514],{"class":47,"line":48},[45,12503,2757],{"class":2440},[45,12505,3539],{"class":2455},[45,12507,96],{"class":85},[45,12509,3509],{"class":3082},[45,12511,3079],{"class":85},[45,12513,3498],{"class":3082},[45,12515,2464],{"class":85},[45,12517,12518,12520,12523,12525],{"class":47,"line":63},[45,12519,4681],{"class":73},[45,12521,12522],{"class":51}," width ",[45,12524,5601],{"class":55},[45,12526,3528],{"class":51},[14,12528,12529],{},"Parameters:",[195,12531,12532,12536],{},[198,12533,12534],{},[18,12535,3509],{},[198,12537,12538],{},[18,12539,3523],{},[712,12541,12543],{"id":12542},"_2-look-at-the-call-line","2. Look at the call line",[14,12545,12546],{},"The call line shows the arguments.",[36,12548,12550],{"className":38,"code":12549,"language":40,"meta":41,"style":41},"area(5, 3)\n",[18,12551,12552],{"__ignoreMap":41},[45,12553,12554,12556,12558,12560,12562,12564],{"class":47,"line":48},[45,12555,3605],{"class":2070},[45,12557,96],{"class":85},[45,12559,3749],{"class":59},[45,12561,3079],{"class":85},[45,12563,777],{"class":59},[45,12565,109],{"class":85},[14,12567,12568],{},"Arguments:",[195,12570,12571,12575],{},[198,12572,12573],{},[18,12574,3749],{},[198,12576,12577],{},[18,12578,11184],{},[712,12580,12582],{"id":12581},"_3-count-how-many-values-the-function-expects","3. Count how many values the function expects",[14,12584,12585],{},"If the function has two required parameters, you usually need to pass two arguments.",[712,12587,12589],{"id":12588},"_4-check-whether-the-arguments-are-positional-or-keyword","4. Check whether the arguments are positional or keyword",[14,12591,12592],{},"Positional call:",[36,12594,12595],{"className":38,"code":12549,"language":40,"meta":41,"style":41},[18,12596,12597],{"__ignoreMap":41},[45,12598,12599,12601,12603,12605,12607,12609],{"class":47,"line":48},[45,12600,3605],{"class":2070},[45,12602,96],{"class":85},[45,12604,3749],{"class":59},[45,12606,3079],{"class":85},[45,12608,777],{"class":59},[45,12610,109],{"class":85},[14,12612,12613],{},"Keyword call:",[36,12615,12617],{"className":38,"code":12616,"language":40,"meta":41,"style":41},"area(width=5, height=3)\n",[18,12618,12619],{"__ignoreMap":41},[45,12620,12621,12623,12625,12627,12629,12631,12633,12635,12637,12639],{"class":47,"line":48},[45,12622,3605],{"class":2070},[45,12624,96],{"class":85},[45,12626,3509],{"class":6503},[45,12628,56],{"class":55},[45,12630,3749],{"class":59},[45,12632,3079],{"class":85},[45,12634,3498],{"class":6503},[45,12636,56],{"class":55},[45,12638,11184],{"class":59},[45,12640,109],{"class":85},[14,12642,12643],{},"Both can work, but they match values in different ways.",[31,12645,2012],{"id":2011},[14,12647,12648],{},"Beginners often struggle with this topic for a few specific reasons:",[195,12650,12651,12654,12657,12660,12663],{},[198,12652,12653],{},"Mixing up the function definition and the function call",[198,12655,12656],{},"Passing arguments in the wrong order",[198,12658,12659],{},"Forgetting a required argument",[198,12661,12662],{},"Assuming parameter names must match caller variable names",[198,12664,12665,12666],{},"Confusing parameters and arguments with ",[236,12667,5885],{"href":5884},[14,12669,12670,12671,12674,12675,240],{},"A return value is different. Parameters and arguments are about getting values ",[2421,12672,12673],{},"into"," a function. A return value is about sending a value ",[2421,12676,12677],{},"back out",[31,12679,12681],{"id":12680},"simple-debugging-ideas","Simple debugging ideas",[14,12683,12684],{},"If a function call is not working, try these simple checks:",[36,12686,12688],{"className":38,"code":12687,"language":40,"meta":41,"style":41},"print(function_name)\nhelp(function_name)\nprint(type(value))\nprint(a, b)\n",[18,12689,12690,12700,12710,12725],{"__ignoreMap":41},[45,12691,12692,12694,12696,12698],{"class":47,"line":48},[45,12693,2065],{"class":92},[45,12695,96],{"class":85},[45,12697,8275],{"class":2070},[45,12699,109],{"class":85},[45,12701,12702,12704,12706,12708],{"class":47,"line":63},[45,12703,2104],{"class":92},[45,12705,96],{"class":85},[45,12707,8275],{"class":2070},[45,12709,109],{"class":85},[45,12711,12712,12714,12716,12718,12720,12723],{"class":47,"line":70},[45,12713,2065],{"class":92},[45,12715,96],{"class":85},[45,12717,4340],{"class":2109},[45,12719,96],{"class":85},[45,12721,12722],{"class":2070},"value",[45,12724,4348],{"class":85},[45,12726,12727,12729,12731,12733,12735,12737],{"class":47,"line":89},[45,12728,2065],{"class":92},[45,12730,96],{"class":85},[45,12732,236],{"class":2070},[45,12734,3079],{"class":85},[45,12736,11115],{"class":2070},[45,12738,109],{"class":85},[14,12740,12741],{},"You can also try calling the function with simple literal values first.",[14,12743,12744],{},"For example, if this is not working:",[36,12746,12748],{"className":38,"code":12747,"language":40,"meta":41,"style":41},"greet(user_name)\n",[18,12749,12750],{"__ignoreMap":41},[45,12751,12752,12754,12756,12759],{"class":47,"line":48},[45,12753,2982],{"class":2070},[45,12755,96],{"class":85},[45,12757,12758],{"class":2070},"user_name",[45,12760,109],{"class":85},[14,12762,12763],{},"try this first:",[36,12765,12767],{"className":38,"code":12766,"language":40,"meta":41,"style":41},"greet(\"Maya\")\n",[18,12768,12769],{"__ignoreMap":41},[45,12770,12771,12773,12775,12777,12779,12781],{"class":47,"line":48},[45,12772,2982],{"class":2070},[45,12774,96],{"class":85},[45,12776,100],{"class":99},[45,12778,5737],{"class":103},[45,12780,100],{"class":99},[45,12782,109],{"class":85},[14,12784,12785],{},"That helps you find out whether the problem is in the function itself or in the variable you passed.",[31,12787,2281],{"id":2280},[712,12789,12791],{"id":12790},"what-is-the-difference-between-a-parameter-and-an-argument-in-python","What is the difference between a parameter and an argument in Python?",[14,12793,12794],{},"A parameter is the name in the function definition. An argument is the value passed into the function when it is called.",[712,12796,12798],{"id":12797},"do-the-variable-names-outside-the-function-need-to-match-the-parameter-names","Do the variable names outside the function need to match the parameter names?",[14,12800,12801],{},"No. Python matches positional arguments by order and keyword arguments by parameter name.",[712,12803,12805],{"id":12804},"what-happens-if-i-do-not-pass-enough-arguments","What happens if I do not pass enough arguments?",[14,12807,12808,12809,12811],{},"Python raises a ",[18,12810,7711],{}," because the function did not receive the required values.",[712,12813,12815],{"id":12814},"when-should-i-use-keyword-arguments","When should I use keyword arguments?",[14,12817,12818],{},"Use them when they make the call easier to read or when you want to avoid mistakes with argument order.",[712,12820,12822],{"id":12821},"are-default-values-the-same-as-arguments","Are default values the same as arguments?",[14,12824,12825],{},"No. A default value belongs to a parameter and is used only when no argument is passed for that parameter.",[31,12827,2342],{"id":2341},[195,12829,12830,12834,12838,12843,12847,12851],{},[198,12831,12832],{},[236,12833,5879],{"href":4579},[198,12835,12836],{},[236,12837,6323],{"href":5884},[198,12839,12840],{},[236,12841,12842],{"href":12106},"Default and keyword arguments explained",[198,12844,12845],{},[236,12846,6328],{"href":5891},[198,12848,12849],{},[236,12850,6338],{"href":5786},[198,12852,12853],{},[236,12854,6343],{"href":5790},[2367,12856,12857],{},"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 .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 .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":41,"searchDepth":63,"depth":63,"links":12859},[12860,12861,12862,12863,12864,12868,12869,12870,12871,12872,12878,12885,12886,12887,12894],{"id":33,"depth":63,"text":34},{"id":6599,"depth":63,"text":6600},{"id":11090,"depth":63,"text":11091},{"id":11205,"depth":63,"text":11206},{"id":11320,"depth":63,"text":11321,"children":12865},[12866,12867],{"id":11379,"depth":70,"text":11380},{"id":5451,"depth":70,"text":5452},{"id":11507,"depth":63,"text":11508},{"id":6966,"depth":63,"text":6967},{"id":7088,"depth":63,"text":7089},{"id":12110,"depth":63,"text":12111},{"id":1336,"depth":63,"text":1337,"children":12873},[12874,12875,12876,12877],{"id":12242,"depth":70,"text":12243},{"id":12306,"depth":70,"text":12307},{"id":12377,"depth":70,"text":12378},{"id":12456,"depth":70,"text":12457},{"id":12478,"depth":63,"text":12479,"children":12879},[12880,12882,12883,12884],{"id":12485,"depth":70,"text":12881},"1. Look at the def line",{"id":12542,"depth":70,"text":12543},{"id":12581,"depth":70,"text":12582},{"id":12588,"depth":70,"text":12589},{"id":2011,"depth":63,"text":2012},{"id":12680,"depth":63,"text":12681},{"id":2280,"depth":63,"text":2281,"children":12888},[12889,12890,12891,12892,12893],{"id":12790,"depth":70,"text":12791},{"id":12797,"depth":70,"text":12798},{"id":12804,"depth":70,"text":12805},{"id":12814,"depth":70,"text":12815},{"id":12821,"depth":70,"text":12822},{"id":2341,"depth":63,"text":2342},"Master function parameters and arguments in python in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Ffunction-parameters-and-arguments-in-python",{"title":10969,"description":12895},"learn\u002Ffunction-parameters-and-arguments-in-python","kgXfxzwmQtWLTBEyVDlm3yMHCRVmT9XipuPeo1WqZvQ",{"id":12902,"title":12903,"body":12904,"description":14852,"extension":2399,"meta":14853,"navigation":66,"path":14854,"seo":14855,"stem":14856,"__hash__":14857},"content\u002Flearn\u002Fgenerators-in-python-explained.md","Generators in Python Explained",{"type":7,"value":12905,"toc":14826},[12906,12909,12912,12915,12926,12932,12934,13025,13027,13047,13052,13056,13059,13062,13065,13076,13089,13096,13101,13104,13111,13123,13125,13246,13248,13272,13275,13294,13297,13301,13306,13311,13315,13371,13373,13394,13398,13454,13457,13485,13488,13491,13549,13551,13567,13571,13577,13658,13660,13686,13693,13808,13810,13826,13834,13885,13895,13899,13902,13906,13971,13973,13998,14002,14083,14085,14106,14109,14112,14117,14121,14124,14127,14141,14143,14227,14229,14254,14257,14271,14275,14278,14281,14301,14304,14402,14404,14429,14432,14484,14486,14512,14515,14517,14520,14547,14550,14677,14679,14732,14735,14749,14757,14759,14763,14766,14773,14778,14782,14785,14789,14795,14797,14823],[10,12907,12903],{"id":12908},"generators-in-python-explained",[14,12910,12911],{},"Generators let you produce values one at a time instead of building a full list all at once.",[14,12913,12914],{},"This is useful when:",[195,12916,12917,12920,12923],{},[198,12918,12919],{},"you have many values",[198,12921,12922],{},"you only need to loop through them once",[198,12924,12925],{},"you want to create values only when needed",[14,12927,12928,12929,240],{},"A Python function becomes a generator function when it uses ",[18,12930,12931],{},"yield",[31,12933,34],{"id":33},[36,12935,12937],{"className":38,"code":12936,"language":40,"meta":41,"style":41},"def count_up_to(limit):\n    number = 1\n    while number \u003C= limit:\n        yield number\n        number += 1\n\nfor value in count_up_to(3):\n    print(value)\n",[18,12938,12939,12953,12963,12977,12985,12995,12999,13015],{"__ignoreMap":41},[45,12940,12941,12943,12946,12948,12951],{"class":47,"line":48},[45,12942,2757],{"class":2440},[45,12944,12945],{"class":2455}," count_up_to",[45,12947,96],{"class":85},[45,12949,12950],{"class":3082},"limit",[45,12952,2464],{"class":85},[45,12954,12955,12958,12960],{"class":47,"line":63},[45,12956,12957],{"class":51},"    number ",[45,12959,56],{"class":55},[45,12961,12962],{"class":59}," 1\n",[45,12964,12965,12968,12970,12972,12975],{"class":47,"line":70},[45,12966,12967],{"class":73},"    while",[45,12969,744],{"class":51},[45,12971,1156],{"class":55},[45,12973,12974],{"class":51}," limit",[45,12976,86],{"class":85},[45,12978,12979,12982],{"class":47,"line":89},[45,12980,12981],{"class":73},"        yield",[45,12983,12984],{"class":51}," number\n",[45,12986,12987,12990,12993],{"class":47,"line":112},[45,12988,12989],{"class":51},"        number ",[45,12991,12992],{"class":55},"+=",[45,12994,12962],{"class":59},[45,12996,12997],{"class":47,"line":126},[45,12998,67],{"emptyLinePlaceholder":66},[45,13000,13001,13003,13005,13007,13009,13011,13013],{"class":47,"line":142},[45,13002,7895],{"class":73},[45,13004,1492],{"class":51},[45,13006,8623],{"class":73},[45,13008,12945],{"class":2070},[45,13010,96],{"class":85},[45,13012,11184],{"class":59},[45,13014,2464],{"class":85},[45,13016,13017,13019,13021,13023],{"class":47,"line":149},[45,13018,93],{"class":92},[45,13020,96],{"class":85},[45,13022,12722],{"class":2070},[45,13024,109],{"class":85},[14,13026,177],{},[36,13028,13030],{"className":38,"code":13029,"language":40,"meta":41,"style":41},"1\n2\n3\n",[18,13031,13032,13037,13042],{"__ignoreMap":41},[45,13033,13034],{"class":47,"line":48},[45,13035,13036],{"class":59},"1\n",[45,13038,13039],{"class":47,"line":63},[45,13040,13041],{"class":59},"2\n",[45,13043,13044],{"class":47,"line":70},[45,13045,13046],{"class":59},"3\n",[14,13048,165,13049,13051],{},[18,13050,12931],{}," inside a function to create a generator. The values are produced one at a time when you loop over it.",[31,13053,13055],{"id":13054},"what-a-generator-is","What a generator is",[14,13057,13058],{},"A generator is an object that gives values one at a time.",[14,13060,13061],{},"Unlike a list, it does not create and store all results in memory immediately. Instead, it waits until Python asks for the next value.",[14,13063,13064],{},"Generators are helpful when:",[195,13066,13067,13070,13073],{},[198,13068,13069],{},"you are working with lots of data",[198,13071,13072],{},"values should be created as needed",[198,13074,13075],{},"you only need to loop through the results once",[14,13077,13078,13079,13083,13084,13088],{},"If you are new to this topic, it also helps to understand the idea of an ",[236,13080,13082],{"href":13081},"\u002Fglossary\u002Fwhat-is-an-iterable-in-python","iterable in Python"," and an ",[236,13085,13087],{"href":13086},"\u002Fglossary\u002Fwhat-is-an-iterator-in-python","iterator in Python",", because generators are closely related to both.",[31,13090,13092,13093,13095],{"id":13091},"how-yield-works","How ",[18,13094,12931],{}," works",[14,13097,13098,13100],{},[18,13099,12931],{}," sends back one value and pauses the function.",[14,13102,13103],{},"When Python asks for the next value, the function continues from where it stopped.",[14,13105,13106,13107,1316,13109,457],{},"That is the main difference between ",[18,13108,12931],{},[18,13110,3461],{},[195,13112,13113,13118],{},[198,13114,13115,13117],{},[18,13116,3461],{}," ends the function completely",[198,13119,13120,13122],{},[18,13121,12931],{}," pauses the function and allows it to continue later",[14,13124,466],{},[36,13126,13128],{"className":38,"code":13127,"language":40,"meta":41,"style":41},"def simple_generator():\n    print(\"Start\")\n    yield 10\n    print(\"Middle\")\n    yield 20\n    print(\"End\")\n\ngen = simple_generator()\n\nprint(next(gen))\nprint(next(gen))\n",[18,13129,13130,13139,13154,13161,13176,13182,13197,13201,13212,13216,13232],{"__ignoreMap":41},[45,13131,13132,13134,13137],{"class":47,"line":48},[45,13133,2757],{"class":2440},[45,13135,13136],{"class":2455}," simple_generator",[45,13138,3828],{"class":85},[45,13140,13141,13143,13145,13147,13150,13152],{"class":47,"line":63},[45,13142,93],{"class":92},[45,13144,96],{"class":85},[45,13146,100],{"class":99},[45,13148,13149],{"class":103},"Start",[45,13151,100],{"class":99},[45,13153,109],{"class":85},[45,13155,13156,13159],{"class":47,"line":70},[45,13157,13158],{"class":73},"    yield",[45,13160,1174],{"class":59},[45,13162,13163,13165,13167,13169,13172,13174],{"class":47,"line":89},[45,13164,93],{"class":92},[45,13166,96],{"class":85},[45,13168,100],{"class":99},[45,13170,13171],{"class":103},"Middle",[45,13173,100],{"class":99},[45,13175,109],{"class":85},[45,13177,13178,13180],{"class":47,"line":112},[45,13179,13158],{"class":73},[45,13181,1883],{"class":59},[45,13183,13184,13186,13188,13190,13193,13195],{"class":47,"line":126},[45,13185,93],{"class":92},[45,13187,96],{"class":85},[45,13189,100],{"class":99},[45,13191,13192],{"class":103},"End",[45,13194,100],{"class":99},[45,13196,109],{"class":85},[45,13198,13199],{"class":47,"line":142},[45,13200,67],{"emptyLinePlaceholder":66},[45,13202,13203,13206,13208,13210],{"class":47,"line":149},[45,13204,13205],{"class":51},"gen ",[45,13207,56],{"class":55},[45,13209,13136],{"class":2070},[45,13211,2495],{"class":85},[45,13213,13214],{"class":47,"line":679},[45,13215,67],{"emptyLinePlaceholder":66},[45,13217,13218,13220,13222,13225,13227,13230],{"class":47,"line":686},[45,13219,2065],{"class":92},[45,13221,96],{"class":85},[45,13223,13224],{"class":92},"next",[45,13226,96],{"class":85},[45,13228,13229],{"class":2070},"gen",[45,13231,4348],{"class":85},[45,13233,13234,13236,13238,13240,13242,13244],{"class":47,"line":2229},[45,13235,2065],{"class":92},[45,13237,96],{"class":85},[45,13239,13224],{"class":92},[45,13241,96],{"class":85},[45,13243,13229],{"class":2070},[45,13245,4348],{"class":85},[14,13247,177],{},[36,13249,13251],{"className":38,"code":13250,"language":40,"meta":41,"style":41},"Start\n10\nMiddle\n20\n",[18,13252,13253,13258,13263,13268],{"__ignoreMap":41},[45,13254,13255],{"class":47,"line":48},[45,13256,13257],{"class":51},"Start\n",[45,13259,13260],{"class":47,"line":63},[45,13261,13262],{"class":59},"10\n",[45,13264,13265],{"class":47,"line":70},[45,13266,13267],{"class":51},"Middle\n",[45,13269,13270],{"class":47,"line":89},[45,13271,3436],{"class":59},[14,13273,13274],{},"Notice what happens:",[195,13276,13277,13280,13285,13288],{},[198,13278,13279],{},"the function does not run all at once",[198,13281,13282,13283],{},"it runs until the first ",[18,13284,12931],{},[198,13286,13287],{},"then it pauses",[198,13289,13290,13293],{},[18,13291,13292],{},"next(gen)"," starts it again from the same place",[14,13295,13296],{},"After all values are yielded, the generator is exhausted.",[31,13298,13300],{"id":13299},"generator-function-vs-normal-function","Generator function vs normal function",[14,13302,13303,13304,240],{},"A normal function gives one final result with ",[18,13305,3461],{},[14,13307,13308,13309,240],{},"A generator function can give many results over time with ",[18,13310,12931],{},[712,13312,13314],{"id":13313},"normal-function","Normal function",[36,13316,13318],{"className":38,"code":13317,"language":40,"meta":41,"style":41},"def get_numbers():\n    return [1, 2, 3]\n\nresult = get_numbers()\nprint(result)\n",[18,13319,13320,13329,13347,13351,13361],{"__ignoreMap":41},[45,13321,13322,13324,13327],{"class":47,"line":48},[45,13323,2757],{"class":2440},[45,13325,13326],{"class":2455}," get_numbers",[45,13328,3828],{"class":85},[45,13330,13331,13333,13335,13337,13339,13341,13343,13345],{"class":47,"line":63},[45,13332,4681],{"class":73},[45,13334,8575],{"class":85},[45,13336,8578],{"class":59},[45,13338,3079],{"class":85},[45,13340,3754],{"class":59},[45,13342,3079],{"class":85},[45,13344,777],{"class":59},[45,13346,8594],{"class":85},[45,13348,13349],{"class":47,"line":70},[45,13350,67],{"emptyLinePlaceholder":66},[45,13352,13353,13355,13357,13359],{"class":47,"line":89},[45,13354,3595],{"class":51},[45,13356,56],{"class":55},[45,13358,13326],{"class":2070},[45,13360,2495],{"class":85},[45,13362,13363,13365,13367,13369],{"class":47,"line":112},[45,13364,2065],{"class":92},[45,13366,96],{"class":85},[45,13368,3620],{"class":2070},[45,13370,109],{"class":85},[14,13372,177],{},[36,13374,13376],{"className":38,"code":13375,"language":40,"meta":41,"style":41},"[1, 2, 3]\n",[18,13377,13378],{"__ignoreMap":41},[45,13379,13380,13382,13384,13386,13388,13390,13392],{"class":47,"line":48},[45,13381,9680],{"class":85},[45,13383,8578],{"class":59},[45,13385,3079],{"class":85},[45,13387,3754],{"class":59},[45,13389,3079],{"class":85},[45,13391,777],{"class":59},[45,13393,8594],{"class":85},[712,13395,13397],{"id":13396},"generator-function","Generator function",[36,13399,13401],{"className":38,"code":13400,"language":40,"meta":41,"style":41},"def get_numbers():\n    yield 1\n    yield 2\n    yield 3\n\nresult = get_numbers()\nprint(result)\n",[18,13402,13403,13411,13417,13424,13430,13434,13444],{"__ignoreMap":41},[45,13404,13405,13407,13409],{"class":47,"line":48},[45,13406,2757],{"class":2440},[45,13408,13326],{"class":2455},[45,13410,3828],{"class":85},[45,13412,13413,13415],{"class":47,"line":63},[45,13414,13158],{"class":73},[45,13416,12962],{"class":59},[45,13418,13419,13421],{"class":47,"line":70},[45,13420,13158],{"class":73},[45,13422,13423],{"class":59}," 2\n",[45,13425,13426,13428],{"class":47,"line":89},[45,13427,13158],{"class":73},[45,13429,1481],{"class":59},[45,13431,13432],{"class":47,"line":112},[45,13433,67],{"emptyLinePlaceholder":66},[45,13435,13436,13438,13440,13442],{"class":47,"line":126},[45,13437,3595],{"class":51},[45,13439,56],{"class":55},[45,13441,13326],{"class":2070},[45,13443,2495],{"class":85},[45,13445,13446,13448,13450,13452],{"class":47,"line":142},[45,13447,2065],{"class":92},[45,13449,96],{"class":85},[45,13451,3620],{"class":2070},[45,13453,109],{"class":85},[14,13455,13456],{},"Possible output:",[36,13458,13460],{"className":38,"code":13459,"language":40,"meta":41,"style":41},"\u003Cgenerator object get_numbers at 0x...>\n",[18,13461,13462],{"__ignoreMap":41},[45,13463,13464,13466,13469,13472,13475,13479,13482],{"class":47,"line":48},[45,13465,79],{"class":55},[45,13467,13468],{"class":51},"generator ",[45,13470,13471],{"class":2109},"object",[45,13473,13474],{"class":51}," get_numbers at ",[45,13476,13478],{"class":13477},"srjyR","0x",[45,13480,13481],{"class":2771},"...",[45,13483,13484],{"class":55},">\n",[14,13486,13487],{},"Calling a generator function does not run all its code immediately. It returns a generator object.",[14,13489,13490],{},"You can then loop over it:",[36,13492,13494],{"className":38,"code":13493,"language":40,"meta":41,"style":41},"def get_numbers():\n    yield 1\n    yield 2\n    yield 3\n\nfor number in get_numbers():\n    print(number)\n",[18,13495,13496,13504,13510,13516,13522,13526,13538],{"__ignoreMap":41},[45,13497,13498,13500,13502],{"class":47,"line":48},[45,13499,2757],{"class":2440},[45,13501,13326],{"class":2455},[45,13503,3828],{"class":85},[45,13505,13506,13508],{"class":47,"line":63},[45,13507,13158],{"class":73},[45,13509,12962],{"class":59},[45,13511,13512,13514],{"class":47,"line":70},[45,13513,13158],{"class":73},[45,13515,13423],{"class":59},[45,13517,13518,13520],{"class":47,"line":89},[45,13519,13158],{"class":73},[45,13521,1481],{"class":59},[45,13523,13524],{"class":47,"line":112},[45,13525,67],{"emptyLinePlaceholder":66},[45,13527,13528,13530,13532,13534,13536],{"class":47,"line":126},[45,13529,7895],{"class":73},[45,13531,744],{"class":51},[45,13533,8623],{"class":73},[45,13535,13326],{"class":2070},[45,13537,3828],{"class":85},[45,13539,13540,13542,13544,13547],{"class":47,"line":142},[45,13541,93],{"class":92},[45,13543,96],{"class":85},[45,13545,13546],{"class":2070},"number",[45,13548,109],{"class":85},[14,13550,177],{},[36,13552,13553],{"className":38,"code":13029,"language":40,"meta":41,"style":41},[18,13554,13555,13559,13563],{"__ignoreMap":41},[45,13556,13557],{"class":47,"line":48},[45,13558,13036],{"class":59},[45,13560,13561],{"class":47,"line":63},[45,13562,13041],{"class":59},[45,13564,13565],{"class":47,"line":70},[45,13566,13046],{"class":59},[31,13568,13570],{"id":13569},"looping-through-a-generator","Looping through a generator",[14,13572,13573,13574,13576],{},"The easiest way to use a generator is with a ",[18,13575,7895],{}," loop.",[36,13578,13580],{"className":38,"code":13579,"language":40,"meta":41,"style":41},"def count_up_to(limit):\n    number = 1\n    while number \u003C= limit:\n        yield number\n        number += 1\n\nfor value in count_up_to(5):\n    print(value)\n",[18,13581,13582,13594,13602,13614,13620,13628,13632,13648],{"__ignoreMap":41},[45,13583,13584,13586,13588,13590,13592],{"class":47,"line":48},[45,13585,2757],{"class":2440},[45,13587,12945],{"class":2455},[45,13589,96],{"class":85},[45,13591,12950],{"class":3082},[45,13593,2464],{"class":85},[45,13595,13596,13598,13600],{"class":47,"line":63},[45,13597,12957],{"class":51},[45,13599,56],{"class":55},[45,13601,12962],{"class":59},[45,13603,13604,13606,13608,13610,13612],{"class":47,"line":70},[45,13605,12967],{"class":73},[45,13607,744],{"class":51},[45,13609,1156],{"class":55},[45,13611,12974],{"class":51},[45,13613,86],{"class":85},[45,13615,13616,13618],{"class":47,"line":89},[45,13617,12981],{"class":73},[45,13619,12984],{"class":51},[45,13621,13622,13624,13626],{"class":47,"line":112},[45,13623,12989],{"class":51},[45,13625,12992],{"class":55},[45,13627,12962],{"class":59},[45,13629,13630],{"class":47,"line":126},[45,13631,67],{"emptyLinePlaceholder":66},[45,13633,13634,13636,13638,13640,13642,13644,13646],{"class":47,"line":142},[45,13635,7895],{"class":73},[45,13637,1492],{"class":51},[45,13639,8623],{"class":73},[45,13641,12945],{"class":2070},[45,13643,96],{"class":85},[45,13645,3749],{"class":59},[45,13647,2464],{"class":85},[45,13649,13650,13652,13654,13656],{"class":47,"line":149},[45,13651,93],{"class":92},[45,13653,96],{"class":85},[45,13655,12722],{"class":2070},[45,13657,109],{"class":85},[14,13659,177],{},[36,13661,13663],{"className":38,"code":13662,"language":40,"meta":41,"style":41},"1\n2\n3\n4\n5\n",[18,13664,13665,13669,13673,13677,13682],{"__ignoreMap":41},[45,13666,13667],{"class":47,"line":48},[45,13668,13036],{"class":59},[45,13670,13671],{"class":47,"line":63},[45,13672,13041],{"class":59},[45,13674,13675],{"class":47,"line":70},[45,13676,13046],{"class":59},[45,13678,13679],{"class":47,"line":89},[45,13680,13681],{"class":59},"4\n",[45,13683,13684],{"class":47,"line":112},[45,13685,11570],{"class":59},[14,13687,13688,13689,13692],{},"You can also use ",[18,13690,13691],{},"next()"," to get one value at a time:",[36,13694,13696],{"className":38,"code":13695,"language":40,"meta":41,"style":41},"def count_up_to(limit):\n    number = 1\n    while number \u003C= limit:\n        yield number\n        number += 1\n\ngen = count_up_to(3)\n\nprint(next(gen))\nprint(next(gen))\nprint(next(gen))\n",[18,13697,13698,13710,13718,13730,13736,13744,13748,13762,13766,13780,13794],{"__ignoreMap":41},[45,13699,13700,13702,13704,13706,13708],{"class":47,"line":48},[45,13701,2757],{"class":2440},[45,13703,12945],{"class":2455},[45,13705,96],{"class":85},[45,13707,12950],{"class":3082},[45,13709,2464],{"class":85},[45,13711,13712,13714,13716],{"class":47,"line":63},[45,13713,12957],{"class":51},[45,13715,56],{"class":55},[45,13717,12962],{"class":59},[45,13719,13720,13722,13724,13726,13728],{"class":47,"line":70},[45,13721,12967],{"class":73},[45,13723,744],{"class":51},[45,13725,1156],{"class":55},[45,13727,12974],{"class":51},[45,13729,86],{"class":85},[45,13731,13732,13734],{"class":47,"line":89},[45,13733,12981],{"class":73},[45,13735,12984],{"class":51},[45,13737,13738,13740,13742],{"class":47,"line":112},[45,13739,12989],{"class":51},[45,13741,12992],{"class":55},[45,13743,12962],{"class":59},[45,13745,13746],{"class":47,"line":126},[45,13747,67],{"emptyLinePlaceholder":66},[45,13749,13750,13752,13754,13756,13758,13760],{"class":47,"line":142},[45,13751,13205],{"class":51},[45,13753,56],{"class":55},[45,13755,12945],{"class":2070},[45,13757,96],{"class":85},[45,13759,11184],{"class":59},[45,13761,109],{"class":85},[45,13763,13764],{"class":47,"line":149},[45,13765,67],{"emptyLinePlaceholder":66},[45,13767,13768,13770,13772,13774,13776,13778],{"class":47,"line":679},[45,13769,2065],{"class":92},[45,13771,96],{"class":85},[45,13773,13224],{"class":92},[45,13775,96],{"class":85},[45,13777,13229],{"class":2070},[45,13779,4348],{"class":85},[45,13781,13782,13784,13786,13788,13790,13792],{"class":47,"line":686},[45,13783,2065],{"class":92},[45,13785,96],{"class":85},[45,13787,13224],{"class":92},[45,13789,96],{"class":85},[45,13791,13229],{"class":2070},[45,13793,4348],{"class":85},[45,13795,13796,13798,13800,13802,13804,13806],{"class":47,"line":2229},[45,13797,2065],{"class":92},[45,13799,96],{"class":85},[45,13801,13224],{"class":92},[45,13803,96],{"class":85},[45,13805,13229],{"class":2070},[45,13807,4348],{"class":85},[14,13809,177],{},[36,13811,13812],{"className":38,"code":13029,"language":40,"meta":41,"style":41},[18,13813,13814,13818,13822],{"__ignoreMap":41},[45,13815,13816],{"class":47,"line":48},[45,13817,13036],{"class":59},[45,13819,13820],{"class":47,"line":63},[45,13821,13041],{"class":59},[45,13823,13824],{"class":47,"line":70},[45,13825,13046],{"class":59},[14,13827,12209,13828,13830,13831,240],{},[18,13829,13691],{}," again after the generator is finished, Python raises ",[18,13832,13833],{},"StopIteration",[36,13835,13837],{"className":38,"code":13836,"language":40,"meta":41,"style":41},"gen = count_up_to(1)\n\nprint(next(gen))\nprint(next(gen))\n",[18,13838,13839,13853,13857,13871],{"__ignoreMap":41},[45,13840,13841,13843,13845,13847,13849,13851],{"class":47,"line":48},[45,13842,13205],{"class":51},[45,13844,56],{"class":55},[45,13846,12945],{"class":2070},[45,13848,96],{"class":85},[45,13850,8578],{"class":59},[45,13852,109],{"class":85},[45,13854,13855],{"class":47,"line":63},[45,13856,67],{"emptyLinePlaceholder":66},[45,13858,13859,13861,13863,13865,13867,13869],{"class":47,"line":70},[45,13860,2065],{"class":92},[45,13862,96],{"class":85},[45,13864,13224],{"class":92},[45,13866,96],{"class":85},[45,13868,13229],{"class":2070},[45,13870,4348],{"class":85},[45,13872,13873,13875,13877,13879,13881,13883],{"class":47,"line":89},[45,13874,2065],{"class":92},[45,13876,96],{"class":85},[45,13878,13224],{"class":92},[45,13880,96],{"class":85},[45,13882,13229],{"class":2070},[45,13884,4348],{"class":85},[14,13886,13887,13888,13890,13891,240],{},"For beginners, a ",[18,13889,7895],{}," loop is usually the best choice because it stops automatically when the generator is done. If you want to understand this error better, see ",[236,13892,13894],{"href":13893},"\u002Ferrors\u002Fstopiteration-exception-in-python-explained","StopIteration in Python explained",[31,13896,13898],{"id":13897},"generator-expressions","Generator expressions",[14,13900,13901],{},"A generator expression is similar to a list comprehension, but it uses round brackets instead of square brackets.",[712,13903,13905],{"id":13904},"list-comprehension","List comprehension",[36,13907,13909],{"className":38,"code":13908,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\ndoubled = [x * 2 for x in numbers]\nprint(doubled)\n",[18,13910,13911,13935,13960],{"__ignoreMap":41},[45,13912,13913,13915,13917,13919,13921,13923,13925,13927,13929,13931,13933],{"class":47,"line":48},[45,13914,8570],{"class":51},[45,13916,56],{"class":55},[45,13918,8575],{"class":85},[45,13920,8578],{"class":59},[45,13922,3079],{"class":85},[45,13924,3754],{"class":59},[45,13926,3079],{"class":85},[45,13928,777],{"class":59},[45,13930,3079],{"class":85},[45,13932,8591],{"class":59},[45,13934,8594],{"class":85},[45,13936,13937,13940,13942,13944,13946,13948,13950,13952,13954,13956,13958],{"class":47,"line":63},[45,13938,13939],{"class":51},"doubled ",[45,13941,56],{"class":55},[45,13943,8575],{"class":85},[45,13945,1169],{"class":51},[45,13947,5601],{"class":55},[45,13949,3754],{"class":59},[45,13951,8855],{"class":73},[45,13953,1185],{"class":51},[45,13955,8623],{"class":73},[45,13957,8626],{"class":51},[45,13959,8594],{"class":85},[45,13961,13962,13964,13966,13969],{"class":47,"line":70},[45,13963,2065],{"class":92},[45,13965,96],{"class":85},[45,13967,13968],{"class":2070},"doubled",[45,13970,109],{"class":85},[14,13972,177],{},[36,13974,13976],{"className":38,"code":13975,"language":40,"meta":41,"style":41},"[2, 4, 6, 8]\n",[18,13977,13978],{"__ignoreMap":41},[45,13979,13980,13982,13984,13986,13988,13990,13992,13994,13996],{"class":47,"line":48},[45,13981,9680],{"class":85},[45,13983,9367],{"class":59},[45,13985,3079],{"class":85},[45,13987,8591],{"class":59},[45,13989,3079],{"class":85},[45,13991,9288],{"class":59},[45,13993,3079],{"class":85},[45,13995,1254],{"class":59},[45,13997,8594],{"class":85},[712,13999,14001],{"id":14000},"generator-expression","Generator expression",[36,14003,14005],{"className":38,"code":14004,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\ndoubled = (x * 2 for x in numbers)\n\nfor value in doubled:\n    print(value)\n",[18,14006,14007,14031,14056,14060,14073],{"__ignoreMap":41},[45,14008,14009,14011,14013,14015,14017,14019,14021,14023,14025,14027,14029],{"class":47,"line":48},[45,14010,8570],{"class":51},[45,14012,56],{"class":55},[45,14014,8575],{"class":85},[45,14016,8578],{"class":59},[45,14018,3079],{"class":85},[45,14020,3754],{"class":59},[45,14022,3079],{"class":85},[45,14024,777],{"class":59},[45,14026,3079],{"class":85},[45,14028,8591],{"class":59},[45,14030,8594],{"class":85},[45,14032,14033,14035,14037,14040,14042,14044,14046,14048,14050,14052,14054],{"class":47,"line":63},[45,14034,13939],{"class":51},[45,14036,56],{"class":55},[45,14038,14039],{"class":85}," (",[45,14041,1169],{"class":51},[45,14043,5601],{"class":55},[45,14045,3754],{"class":59},[45,14047,8855],{"class":73},[45,14049,1185],{"class":51},[45,14051,8623],{"class":73},[45,14053,8626],{"class":51},[45,14055,109],{"class":85},[45,14057,14058],{"class":47,"line":70},[45,14059,67],{"emptyLinePlaceholder":66},[45,14061,14062,14064,14066,14068,14071],{"class":47,"line":89},[45,14063,7895],{"class":73},[45,14065,1492],{"class":51},[45,14067,8623],{"class":73},[45,14069,14070],{"class":51}," doubled",[45,14072,86],{"class":85},[45,14074,14075,14077,14079,14081],{"class":47,"line":112},[45,14076,93],{"class":92},[45,14078,96],{"class":85},[45,14080,12722],{"class":2070},[45,14082,109],{"class":85},[14,14084,177],{},[36,14086,14088],{"className":38,"code":14087,"language":40,"meta":41,"style":41},"2\n4\n6\n8\n",[18,14089,14090,14094,14098,14102],{"__ignoreMap":41},[45,14091,14092],{"class":47,"line":48},[45,14093,13041],{"class":59},[45,14095,14096],{"class":47,"line":63},[45,14097,13681],{"class":59},[45,14099,14100],{"class":47,"line":70},[45,14101,11797],{"class":59},[45,14103,14104],{"class":47,"line":89},[45,14105,1310],{"class":59},[14,14107,14108],{},"A generator expression creates values lazily. That means the values are produced when needed, not all at once.",[14,14110,14111],{},"Use this when you only need to loop through the results.",[14,14113,14114,14115,240],{},"If you want to compare this more directly with list creation, see ",[236,14116,9239],{"href":9238},[31,14118,14120],{"id":14119},"when-to-use-generators","When to use generators",[14,14122,14123],{},"Generators are a good choice when you want to process data step by step.",[14,14125,14126],{},"Use generators when:",[195,14128,14129,14132,14135,14138],{},[198,14130,14131],{},"you are working with large amounts of data",[198,14133,14134],{},"you only need each value once",[198,14136,14137],{},"you want to avoid storing everything at the same time",[198,14139,14140],{},"you are reading data gradually, such as lines from a file",[14,14142,466],{},[36,14144,14146],{"className":38,"code":14145,"language":40,"meta":41,"style":41},"def even_numbers(limit):\n    for number in range(2, limit + 1, 2):\n        yield number\n\nfor value in even_numbers(10):\n    print(value)\n",[18,14147,14148,14161,14191,14197,14201,14217],{"__ignoreMap":41},[45,14149,14150,14152,14155,14157,14159],{"class":47,"line":48},[45,14151,2757],{"class":2440},[45,14153,14154],{"class":2455}," even_numbers",[45,14156,96],{"class":85},[45,14158,12950],{"class":3082},[45,14160,2464],{"class":85},[45,14162,14163,14166,14168,14170,14172,14174,14176,14178,14181,14183,14185,14187,14189],{"class":47,"line":63},[45,14164,14165],{"class":73},"    for",[45,14167,744],{"class":51},[45,14169,8623],{"class":73},[45,14171,10451],{"class":92},[45,14173,96],{"class":85},[45,14175,9367],{"class":59},[45,14177,3079],{"class":85},[45,14179,14180],{"class":2070}," limit ",[45,14182,8850],{"class":55},[45,14184,9030],{"class":59},[45,14186,3079],{"class":85},[45,14188,3754],{"class":59},[45,14190,2464],{"class":85},[45,14192,14193,14195],{"class":47,"line":70},[45,14194,12981],{"class":73},[45,14196,12984],{"class":51},[45,14198,14199],{"class":47,"line":89},[45,14200,67],{"emptyLinePlaceholder":66},[45,14202,14203,14205,14207,14209,14211,14213,14215],{"class":47,"line":112},[45,14204,7895],{"class":73},[45,14206,1492],{"class":51},[45,14208,8623],{"class":73},[45,14210,14154],{"class":2070},[45,14212,96],{"class":85},[45,14214,11786],{"class":59},[45,14216,2464],{"class":85},[45,14218,14219,14221,14223,14225],{"class":47,"line":126},[45,14220,93],{"class":92},[45,14222,96],{"class":85},[45,14224,12722],{"class":2070},[45,14226,109],{"class":85},[14,14228,177],{},[36,14230,14232],{"className":38,"code":14231,"language":40,"meta":41,"style":41},"2\n4\n6\n8\n10\n",[18,14233,14234,14238,14242,14246,14250],{"__ignoreMap":41},[45,14235,14236],{"class":47,"line":48},[45,14237,13041],{"class":59},[45,14239,14240],{"class":47,"line":63},[45,14241,13681],{"class":59},[45,14243,14244],{"class":47,"line":70},[45,14245,11797],{"class":59},[45,14247,14248],{"class":47,"line":89},[45,14249,1310],{"class":59},[45,14251,14252],{"class":47,"line":112},[45,14253,13262],{"class":59},[14,14255,14256],{},"Use a list instead when:",[195,14258,14259,14265,14268],{},[198,14260,14261,14262],{},"you need indexing like ",[18,14263,14264],{},"items[0]",[198,14266,14267],{},"you need to reuse the values many times",[198,14269,14270],{},"you want all results available immediately",[31,14272,14274],{"id":14273},"limits-beginners-should-know","Limits beginners should know",[14,14276,14277],{},"Generators are useful, but they are not the same as lists.",[14,14279,14280],{},"Important limits:",[195,14282,14283,14288,14291,14294],{},[198,14284,14285,14286],{},"a generator does not support indexing like ",[18,14287,14264],{},[198,14289,14290],{},"once fully used, a generator cannot be restarted",[198,14292,14293],{},"if you need the values again, create a new generator",[198,14295,14296,14297,14300],{},"you can convert a generator to a list with ",[18,14298,14299],{},"list(generator)"," if needed",[14,14302,14303],{},"Example of converting to a list:",[36,14305,14307],{"className":38,"code":14306,"language":40,"meta":41,"style":41},"def count_up_to(limit):\n    number = 1\n    while number \u003C= limit:\n        yield number\n        number += 1\n\ngen = count_up_to(4)\nnumbers = list(gen)\n\nprint(numbers)\n",[18,14308,14309,14321,14329,14341,14347,14355,14359,14373,14388,14392],{"__ignoreMap":41},[45,14310,14311,14313,14315,14317,14319],{"class":47,"line":48},[45,14312,2757],{"class":2440},[45,14314,12945],{"class":2455},[45,14316,96],{"class":85},[45,14318,12950],{"class":3082},[45,14320,2464],{"class":85},[45,14322,14323,14325,14327],{"class":47,"line":63},[45,14324,12957],{"class":51},[45,14326,56],{"class":55},[45,14328,12962],{"class":59},[45,14330,14331,14333,14335,14337,14339],{"class":47,"line":70},[45,14332,12967],{"class":73},[45,14334,744],{"class":51},[45,14336,1156],{"class":55},[45,14338,12974],{"class":51},[45,14340,86],{"class":85},[45,14342,14343,14345],{"class":47,"line":89},[45,14344,12981],{"class":73},[45,14346,12984],{"class":51},[45,14348,14349,14351,14353],{"class":47,"line":112},[45,14350,12989],{"class":51},[45,14352,12992],{"class":55},[45,14354,12962],{"class":59},[45,14356,14357],{"class":47,"line":126},[45,14358,67],{"emptyLinePlaceholder":66},[45,14360,14361,14363,14365,14367,14369,14371],{"class":47,"line":142},[45,14362,13205],{"class":51},[45,14364,56],{"class":55},[45,14366,12945],{"class":2070},[45,14368,96],{"class":85},[45,14370,3584],{"class":59},[45,14372,109],{"class":85},[45,14374,14375,14377,14379,14382,14384,14386],{"class":47,"line":149},[45,14376,8570],{"class":51},[45,14378,56],{"class":55},[45,14380,14381],{"class":2109}," list",[45,14383,96],{"class":85},[45,14385,13229],{"class":2070},[45,14387,109],{"class":85},[45,14389,14390],{"class":47,"line":679},[45,14391,67],{"emptyLinePlaceholder":66},[45,14393,14394,14396,14398,14400],{"class":47,"line":686},[45,14395,2065],{"class":92},[45,14397,96],{"class":85},[45,14399,9069],{"class":2070},[45,14401,109],{"class":85},[14,14403,177],{},[36,14405,14407],{"className":38,"code":14406,"language":40,"meta":41,"style":41},"[1, 2, 3, 4]\n",[18,14408,14409],{"__ignoreMap":41},[45,14410,14411,14413,14415,14417,14419,14421,14423,14425,14427],{"class":47,"line":48},[45,14412,9680],{"class":85},[45,14414,8578],{"class":59},[45,14416,3079],{"class":85},[45,14418,3754],{"class":59},[45,14420,3079],{"class":85},[45,14422,777],{"class":59},[45,14424,3079],{"class":85},[45,14426,8591],{"class":59},[45,14428,8594],{"class":85},[14,14430,14431],{},"Be careful: converting a generator to a list uses up the generator.",[36,14433,14435],{"className":38,"code":14434,"language":40,"meta":41,"style":41},"gen = count_up_to(3)\n\nprint(list(gen))\nprint(list(gen))\n",[18,14436,14437,14451,14455,14470],{"__ignoreMap":41},[45,14438,14439,14441,14443,14445,14447,14449],{"class":47,"line":48},[45,14440,13205],{"class":51},[45,14442,56],{"class":55},[45,14444,12945],{"class":2070},[45,14446,96],{"class":85},[45,14448,11184],{"class":59},[45,14450,109],{"class":85},[45,14452,14453],{"class":47,"line":63},[45,14454,67],{"emptyLinePlaceholder":66},[45,14456,14457,14459,14461,14464,14466,14468],{"class":47,"line":70},[45,14458,2065],{"class":92},[45,14460,96],{"class":85},[45,14462,14463],{"class":2109},"list",[45,14465,96],{"class":85},[45,14467,13229],{"class":2070},[45,14469,4348],{"class":85},[45,14471,14472,14474,14476,14478,14480,14482],{"class":47,"line":89},[45,14473,2065],{"class":92},[45,14475,96],{"class":85},[45,14477,14463],{"class":2109},[45,14479,96],{"class":85},[45,14481,13229],{"class":2070},[45,14483,4348],{"class":85},[14,14485,177],{},[36,14487,14489],{"className":38,"code":14488,"language":40,"meta":41,"style":41},"[1, 2, 3]\n[]\n",[18,14490,14491,14507],{"__ignoreMap":41},[45,14492,14493,14495,14497,14499,14501,14503,14505],{"class":47,"line":48},[45,14494,9680],{"class":85},[45,14496,8578],{"class":59},[45,14498,3079],{"class":85},[45,14500,3754],{"class":59},[45,14502,3079],{"class":85},[45,14504,777],{"class":59},[45,14506,8594],{"class":85},[45,14508,14509],{"class":47,"line":63},[45,14510,14511],{"class":85},"[]\n",[14,14513,14514],{},"The second result is empty because the generator was already exhausted.",[31,14516,5903],{"id":5902},[14,14518,14519],{},"These are some common beginner mistakes with generators:",[195,14521,14522,14530,14533,14536,14539],{},[198,14523,14524,14525,14527,14528],{},"confusing ",[18,14526,12931],{}," with ",[18,14529,3461],{},[198,14531,14532],{},"trying to reuse an exhausted generator",[198,14534,14535],{},"trying to index a generator like a list",[198,14537,14538],{},"expecting a generator expression to print all values by itself",[198,14540,14541,14542,14544,14545],{},"using ",[18,14543,13691],{}," too many times and hitting ",[18,14546,13833],{},[14,14548,14549],{},"Useful debugging checks:",[36,14551,14553],{"className":38,"code":14552,"language":40,"meta":41,"style":41},"def count_up_to(limit):\n    number = 1\n    while number \u003C= limit:\n        yield number\n        number += 1\n\ngenerator_obj = count_up_to(3)\n\nprint(generator_obj)\nprint(type(generator_obj))\nprint(next(generator_obj))\nprint(list(generator_obj))\n",[18,14554,14555,14567,14575,14587,14593,14601,14605,14620,14624,14635,14649,14663],{"__ignoreMap":41},[45,14556,14557,14559,14561,14563,14565],{"class":47,"line":48},[45,14558,2757],{"class":2440},[45,14560,12945],{"class":2455},[45,14562,96],{"class":85},[45,14564,12950],{"class":3082},[45,14566,2464],{"class":85},[45,14568,14569,14571,14573],{"class":47,"line":63},[45,14570,12957],{"class":51},[45,14572,56],{"class":55},[45,14574,12962],{"class":59},[45,14576,14577,14579,14581,14583,14585],{"class":47,"line":70},[45,14578,12967],{"class":73},[45,14580,744],{"class":51},[45,14582,1156],{"class":55},[45,14584,12974],{"class":51},[45,14586,86],{"class":85},[45,14588,14589,14591],{"class":47,"line":89},[45,14590,12981],{"class":73},[45,14592,12984],{"class":51},[45,14594,14595,14597,14599],{"class":47,"line":112},[45,14596,12989],{"class":51},[45,14598,12992],{"class":55},[45,14600,12962],{"class":59},[45,14602,14603],{"class":47,"line":126},[45,14604,67],{"emptyLinePlaceholder":66},[45,14606,14607,14610,14612,14614,14616,14618],{"class":47,"line":142},[45,14608,14609],{"class":51},"generator_obj ",[45,14611,56],{"class":55},[45,14613,12945],{"class":2070},[45,14615,96],{"class":85},[45,14617,11184],{"class":59},[45,14619,109],{"class":85},[45,14621,14622],{"class":47,"line":149},[45,14623,67],{"emptyLinePlaceholder":66},[45,14625,14626,14628,14630,14633],{"class":47,"line":679},[45,14627,2065],{"class":92},[45,14629,96],{"class":85},[45,14631,14632],{"class":2070},"generator_obj",[45,14634,109],{"class":85},[45,14636,14637,14639,14641,14643,14645,14647],{"class":47,"line":686},[45,14638,2065],{"class":92},[45,14640,96],{"class":85},[45,14642,4340],{"class":2109},[45,14644,96],{"class":85},[45,14646,14632],{"class":2070},[45,14648,4348],{"class":85},[45,14650,14651,14653,14655,14657,14659,14661],{"class":47,"line":2229},[45,14652,2065],{"class":92},[45,14654,96],{"class":85},[45,14656,13224],{"class":92},[45,14658,96],{"class":85},[45,14660,14632],{"class":2070},[45,14662,4348],{"class":85},[45,14664,14665,14667,14669,14671,14673,14675],{"class":47,"line":2236},[45,14666,2065],{"class":92},[45,14668,96],{"class":85},[45,14670,14463],{"class":2109},[45,14672,96],{"class":85},[45,14674,14632],{"class":2070},[45,14676,4348],{"class":85},[14,14678,13456],{},[36,14680,14682],{"className":38,"code":14681,"language":40,"meta":41,"style":41},"\u003Cgenerator object count_up_to at 0x...>\n\u003Cclass 'generator'>\n1\n[2, 3]\n",[18,14683,14684,14701,14716,14720],{"__ignoreMap":41},[45,14685,14686,14688,14690,14692,14695,14697,14699],{"class":47,"line":48},[45,14687,79],{"class":55},[45,14689,13468],{"class":51},[45,14691,13471],{"class":2109},[45,14693,14694],{"class":51}," count_up_to at ",[45,14696,13478],{"class":13477},[45,14698,13481],{"class":2771},[45,14700,13484],{"class":55},[45,14702,14703,14705,14707,14709,14712,14714],{"class":47,"line":63},[45,14704,79],{"class":55},[45,14706,2441],{"class":2440},[45,14708,9199],{"class":99},[45,14710,14711],{"class":103},"generator",[45,14713,7901],{"class":99},[45,14715,13484],{"class":55},[45,14717,14718],{"class":47,"line":70},[45,14719,13036],{"class":59},[45,14721,14722,14724,14726,14728,14730],{"class":47,"line":89},[45,14723,9680],{"class":85},[45,14725,9367],{"class":59},[45,14727,3079],{"class":85},[45,14729,777],{"class":59},[45,14731,8594],{"class":85},[14,14733,14734],{},"This example shows an important detail:",[195,14736,14737,14743],{},[198,14738,14739,14742],{},[18,14740,14741],{},"next(generator_obj)"," uses the first value",[198,14744,14745,14748],{},[18,14746,14747],{},"list(generator_obj)"," only gets the remaining values",[14,14750,14751,14752,14756],{},"If you are confused about why generators work this way, reading about ",[236,14753,14755],{"href":14754},"\u002Flearn\u002Fiterators-and-iterable-objects-explained","iterators and iterable objects explained"," can help.",[31,14758,2281],{"id":2280},[712,14760,14762],{"id":14761},"what-is-the-difference-between-a-generator-and-a-list","What is the difference between a generator and a list?",[14,14764,14765],{},"A list stores all values immediately. A generator produces values one at a time when needed.",[712,14767,14769,14770,14772],{"id":14768},"does-yield-end-the-function","Does ",[18,14771,12931],{}," end the function?",[14,14774,2302,14775,14777],{},[18,14776,12931],{}," pauses the function. The function continues later from the same place.",[712,14779,14781],{"id":14780},"can-i-loop-through-a-generator-more-than-once","Can I loop through a generator more than once?",[14,14783,14784],{},"Not the same exhausted generator. Create a new generator if you need to loop again.",[712,14786,14788],{"id":14787},"should-beginners-use-generators","Should beginners use generators?",[14,14790,14791,14792,14794],{},"Yes, for simple cases. Start with basic ",[18,14793,12931],{}," examples and use them when values should be produced one at a time.",[31,14796,2342],{"id":2341},[195,14798,14799,14804,14808,14813,14818],{},[198,14800,14801],{},[236,14802,14803],{"href":14754},"Iterators and iterable objects explained",[198,14805,14806],{},[236,14807,10904],{"href":9238},[198,14809,14810],{},[236,14811,14812],{"href":13086},"What is an iterator in Python",[198,14814,14815],{},[236,14816,14817],{"href":13081},"What is an iterable in Python",[198,14819,14820],{},[236,14821,14822],{"href":13893},"StopIteration exception in Python explained",[2367,14824,14825],{},"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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srjyR, html code.shiki .srjyR{--shiki-light:#90A4AE;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":14827},[14828,14829,14830,14832,14836,14837,14841,14842,14843,14844,14851],{"id":33,"depth":63,"text":34},{"id":13054,"depth":63,"text":13055},{"id":13091,"depth":63,"text":14831},"How yield works",{"id":13299,"depth":63,"text":13300,"children":14833},[14834,14835],{"id":13313,"depth":70,"text":13314},{"id":13396,"depth":70,"text":13397},{"id":13569,"depth":63,"text":13570},{"id":13897,"depth":63,"text":13898,"children":14838},[14839,14840],{"id":13904,"depth":70,"text":13905},{"id":14000,"depth":70,"text":14001},{"id":14119,"depth":63,"text":14120},{"id":14273,"depth":63,"text":14274},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":14845},[14846,14847,14849,14850],{"id":14761,"depth":70,"text":14762},{"id":14768,"depth":70,"text":14848},"Does yield end the function?",{"id":14780,"depth":70,"text":14781},{"id":14787,"depth":70,"text":14788},{"id":2341,"depth":63,"text":2342},"Master generators in python explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fgenerators-in-python-explained",{"title":12903,"description":14852},"learn\u002Fgenerators-in-python-explained","yNA_3GdZvFMFAmbQLPpCJvpIRuEy89gflmUUJ4fkY00",{"id":14859,"title":14860,"body":14861,"description":16514,"extension":2399,"meta":16515,"navigation":66,"path":16516,"seo":16517,"stem":16518,"__hash__":16519},"content\u002Flearn\u002Fhow-import-works-in-python.md","How Import Works in Python",{"type":7,"value":14862,"toc":16471},[14863,14866,14872,14878,14880,14945,14947,14962,14967,14987,14991,14997,15000,15021,15033,15036,15047,15053,15057,15060,15066,15100,15103,15106,15117,15123,15156,15159,15162,15170,15173,15179,15219,15222,15225,15233,15239,15279,15281,15289,15293,15299,15355,15357,15365,15367,15389,15392,15406,15410,15413,15416,15440,15446,15481,15487,15537,15539,15553,15556,15570,15573,15590,15593,15607,15614,15618,15621,15624,15638,15644,15647,15650,15672,15683,15695,15699,15702,15705,15716,15718,15724,15770,15775,15802,15804,15824,15827,15838,15841,15847,15854,15857,15930,15933,15945,15948,15957,15960,15971,15974,15978,15981,15985,15999,16002,16042,16045,16048,16052,16055,16058,16069,16075,16078,16094,16097,16100,16111,16115,16118,16121,16133,16143,16150,16154,16159,16166,16170,16173,16202,16205,16252,16260,16264,16267,16284,16287,16340,16343,16357,16359,16367,16373,16380,16386,16389,16397,16401,16404,16407,16411,16414,16417,16423,16426,16429,16431,16468],[10,14864,14860],{"id":14865},"how-import-works-in-python",[14,14867,14868,14871],{},[18,14869,14870],{},"import"," lets you use code from another Python file or from Python’s built-in and installed modules.",[14,14873,14874,14875,14877],{},"This is one of the most important Python basics. Once you understand how ",[18,14876,14870],{}," works, it becomes much easier to read other people’s code and organize your own code into separate files.",[31,14879,34],{"id":33},[36,14881,14883],{"className":38,"code":14882,"language":40,"meta":41,"style":41},"import math\nprint(math.sqrt(16))\n\nfrom math import sqrt\nprint(sqrt(25))\n",[18,14884,14885,14892,14913,14917,14930],{"__ignoreMap":41},[45,14886,14887,14889],{"class":47,"line":48},[45,14888,14870],{"class":73},[45,14890,14891],{"class":51}," math\n",[45,14893,14894,14896,14898,14901,14903,14906,14908,14911],{"class":47,"line":63},[45,14895,2065],{"class":92},[45,14897,96],{"class":85},[45,14899,14900],{"class":2070},"math",[45,14902,240],{"class":85},[45,14904,14905],{"class":2070},"sqrt",[45,14907,96],{"class":85},[45,14909,14910],{"class":59},"16",[45,14912,4348],{"class":85},[45,14914,14915],{"class":47,"line":70},[45,14916,67],{"emptyLinePlaceholder":66},[45,14918,14919,14922,14925,14927],{"class":47,"line":89},[45,14920,14921],{"class":73},"from",[45,14923,14924],{"class":51}," math ",[45,14926,14870],{"class":73},[45,14928,14929],{"class":51}," sqrt\n",[45,14931,14932,14934,14936,14938,14940,14943],{"class":47,"line":112},[45,14933,2065],{"class":92},[45,14935,96],{"class":85},[45,14937,14905],{"class":2070},[45,14939,96],{"class":85},[45,14941,14942],{"class":59},"25",[45,14944,4348],{"class":85},[14,14946,177],{},[36,14948,14950],{"className":38,"code":14949,"language":40,"meta":41,"style":41},"4.0\n5.0\n",[18,14951,14952,14957],{"__ignoreMap":41},[45,14953,14954],{"class":47,"line":48},[45,14955,14956],{"class":59},"4.0\n",[45,14958,14959],{"class":47,"line":63},[45,14960,14961],{"class":59},"5.0\n",[14,14963,165,14964,14966],{},[18,14965,14870],{}," when you want to access code from another module.",[195,14968,14969,14978],{},[198,14970,14971,14974,14975],{},[18,14972,14973],{},"import math"," keeps the module name, so you write ",[18,14976,14977],{},"math.sqrt(...)",[198,14979,14980,14983,14984],{},[18,14981,14982],{},"from math import sqrt"," imports one specific name, so you write ",[18,14985,14986],{},"sqrt(...)",[31,14988,14990],{"id":14989},"what-import-means","What import means",[14,14992,14993,14994,14996],{},"In Python, ",[18,14995,14870],{}," brings code from another module into your program.",[14,14998,14999],{},"A few useful terms:",[195,15001,15002,15012,15018],{},[198,15003,2419,15004,15007,15008,15011],{},[2421,15005,15006],{},"module"," is usually one ",[18,15009,15010],{},".py"," file",[198,15013,2419,15014,15017],{},[2421,15015,15016],{},"package"," is a folder that contains Python modules",[198,15019,15020],{},"An imported module is usually loaded once, then reused",[14,15022,15023,15024,21,15026,25,15029,15032],{},"For example, Python’s standard library includes modules like ",[18,15025,14900],{},[18,15027,15028],{},"random",[18,15030,15031],{},"json",". You can also import your own files.",[14,15034,15035],{},"When Python imports a module, it:",[195,15037,15038,15041,15044],{},[198,15039,15040],{},"finds the module",[198,15042,15043],{},"runs its top-level code once",[198,15045,15046],{},"makes its functions, classes, and variables available",[14,15048,15049,15050,240],{},"If you are new to this topic, you may also want to read ",[236,15051,5898],{"href":15052},"\u002Flearn\u002Fpython-modules-explained",[31,15054,15056],{"id":15055},"basic-import-forms","Basic import forms",[14,15058,15059],{},"These are the main import patterns beginners see most often.",[712,15061,15063],{"id":15062},"import-module_name",[18,15064,15065],{},"import module_name",[36,15067,15069],{"className":38,"code":15068,"language":40,"meta":41,"style":41},"import math\n\nprint(math.sqrt(9))\n",[18,15070,15071,15077,15081],{"__ignoreMap":41},[45,15072,15073,15075],{"class":47,"line":48},[45,15074,14870],{"class":73},[45,15076,14891],{"class":51},[45,15078,15079],{"class":47,"line":63},[45,15080,67],{"emptyLinePlaceholder":66},[45,15082,15083,15085,15087,15089,15091,15093,15095,15098],{"class":47,"line":70},[45,15084,2065],{"class":92},[45,15086,96],{"class":85},[45,15088,14900],{"class":2070},[45,15090,240],{"class":85},[45,15092,14905],{"class":2070},[45,15094,96],{"class":85},[45,15096,15097],{"class":59},"9",[45,15099,4348],{"class":85},[14,15101,15102],{},"This form is usually the easiest to read.",[14,15104,15105],{},"Why:",[195,15107,15108,15111,15114],{},[198,15109,15110],{},"it shows where the function came from",[198,15112,15113],{},"it avoids name conflicts",[198,15115,15116],{},"it makes code clearer for beginners",[712,15118,15120],{"id":15119},"from-module_name-import-name",[18,15121,15122],{},"from module_name import name",[36,15124,15126],{"className":38,"code":15125,"language":40,"meta":41,"style":41},"from math import sqrt\n\nprint(sqrt(9))\n",[18,15127,15128,15138,15142],{"__ignoreMap":41},[45,15129,15130,15132,15134,15136],{"class":47,"line":48},[45,15131,14921],{"class":73},[45,15133,14924],{"class":51},[45,15135,14870],{"class":73},[45,15137,14929],{"class":51},[45,15139,15140],{"class":47,"line":63},[45,15141,67],{"emptyLinePlaceholder":66},[45,15143,15144,15146,15148,15150,15152,15154],{"class":47,"line":70},[45,15145,2065],{"class":92},[45,15147,96],{"class":85},[45,15149,14905],{"class":2070},[45,15151,96],{"class":85},[45,15153,15097],{"class":59},[45,15155,4348],{"class":85},[14,15157,15158],{},"This is shorter.",[14,15160,15161],{},"It is useful when:",[195,15163,15164,15167],{},[198,15165,15166],{},"you only need one or two names",[198,15168,15169],{},"the imported name is very common in your code",[14,15171,15172],{},"The trade-off is that you no longer see the module name in each call.",[712,15174,15176],{"id":15175},"import-module_name-as-alias",[18,15177,15178],{},"import module_name as alias",[36,15180,15182],{"className":38,"code":15181,"language":40,"meta":41,"style":41},"import math as m\n\nprint(m.sqrt(9))\n",[18,15183,15184,15196,15200],{"__ignoreMap":41},[45,15185,15186,15188,15190,15193],{"class":47,"line":48},[45,15187,14870],{"class":73},[45,15189,14924],{"class":51},[45,15191,15192],{"class":73},"as",[45,15194,15195],{"class":51}," m\n",[45,15197,15198],{"class":47,"line":63},[45,15199,67],{"emptyLinePlaceholder":66},[45,15201,15202,15204,15206,15209,15211,15213,15215,15217],{"class":47,"line":70},[45,15203,2065],{"class":92},[45,15205,96],{"class":85},[45,15207,15208],{"class":2070},"m",[45,15210,240],{"class":85},[45,15212,14905],{"class":2070},[45,15214,96],{"class":85},[45,15216,15097],{"class":59},[45,15218,4348],{"class":85},[14,15220,15221],{},"This gives the module a shorter name.",[14,15223,15224],{},"This is helpful when:",[195,15226,15227,15230],{},[198,15228,15229],{},"the module name is long",[198,15231,15232],{},"the alias is widely understood",[712,15234,15236],{"id":15235},"from-module_name-import-name-as-alias",[18,15237,15238],{},"from module_name import name as alias",[36,15240,15242],{"className":38,"code":15241,"language":40,"meta":41,"style":41},"from math import sqrt as square_root\n\nprint(square_root(9))\n",[18,15243,15244,15260,15264],{"__ignoreMap":41},[45,15245,15246,15248,15250,15252,15255,15257],{"class":47,"line":48},[45,15247,14921],{"class":73},[45,15249,14924],{"class":51},[45,15251,14870],{"class":73},[45,15253,15254],{"class":51}," sqrt ",[45,15256,15192],{"class":73},[45,15258,15259],{"class":51}," square_root\n",[45,15261,15262],{"class":47,"line":63},[45,15263,67],{"emptyLinePlaceholder":66},[45,15265,15266,15268,15270,15273,15275,15277],{"class":47,"line":70},[45,15267,2065],{"class":92},[45,15269,96],{"class":85},[45,15271,15272],{"class":2070},"square_root",[45,15274,96],{"class":85},[45,15276,15097],{"class":59},[45,15278,4348],{"class":85},[14,15280,12914],{},[195,15282,15283,15286],{},[198,15284,15285],{},"you want a clearer local name",[198,15287,15288],{},"you need to avoid a naming conflict",[31,15290,15292],{"id":15291},"simple-example-with-a-standard-library-module","Simple example with a standard library module",[14,15294,15295,15296,15298],{},"A good first example is the ",[18,15297,14900],{}," module.",[36,15300,15302],{"className":38,"code":15301,"language":40,"meta":41,"style":41},"import math\n\nnumber = 16\nresult = math.sqrt(number)\n\nprint(result)\n",[18,15303,15304,15310,15314,15322,15341,15345],{"__ignoreMap":41},[45,15305,15306,15308],{"class":47,"line":48},[45,15307,14870],{"class":73},[45,15309,14891],{"class":51},[45,15311,15312],{"class":47,"line":63},[45,15313,67],{"emptyLinePlaceholder":66},[45,15315,15316,15318,15320],{"class":47,"line":70},[45,15317,728],{"class":51},[45,15319,56],{"class":55},[45,15321,986],{"class":59},[45,15323,15324,15326,15328,15331,15333,15335,15337,15339],{"class":47,"line":89},[45,15325,3595],{"class":51},[45,15327,56],{"class":55},[45,15329,15330],{"class":51}," math",[45,15332,240],{"class":85},[45,15334,14905],{"class":2070},[45,15336,96],{"class":85},[45,15338,13546],{"class":2070},[45,15340,109],{"class":85},[45,15342,15343],{"class":47,"line":112},[45,15344,67],{"emptyLinePlaceholder":66},[45,15346,15347,15349,15351,15353],{"class":47,"line":126},[45,15348,2065],{"class":92},[45,15350,96],{"class":85},[45,15352,3620],{"class":2070},[45,15354,109],{"class":85},[14,15356,177],{},[36,15358,15359],{"className":38,"code":14956,"language":40,"meta":41,"style":41},[18,15360,15361],{"__ignoreMap":41},[45,15362,15363],{"class":47,"line":48},[45,15364,14956],{"class":59},[14,15366,6680],{},[195,15368,15369,15374,15380],{},[198,15370,15371,15373],{},[18,15372,14900],{}," is the module",[198,15375,15376,15379],{},[18,15377,15378],{},"sqrt()"," is a function inside that module",[198,15381,15382,15383,15386,15387],{},"the dot in ",[18,15384,15385],{},"math.sqrt"," is called ",[2421,15388,2597],{},[14,15390,15391],{},"Dot notation helps show exactly where a function comes from. That makes code easier to understand.",[14,15393,15394,15395,15398,15399,15402,15403,15405],{},"For example, ",[18,15396,15397],{},"math.sqrt(9)"," is clearer than just ",[18,15400,15401],{},"sqrt(9)"," if the reader does not already know where ",[18,15404,14905],{}," came from.",[31,15407,15409],{"id":15408},"importing-your-own-python-file","Importing your own Python file",[14,15411,15412],{},"You can also import your own files.",[14,15414,15415],{},"Suppose you have this file structure:",[36,15417,15419],{"className":38,"code":15418,"language":40,"meta":41,"style":41},"helpers.py\nmain.py\n",[18,15420,15421,15431],{"__ignoreMap":41},[45,15422,15423,15426,15428],{"class":47,"line":48},[45,15424,15425],{"class":51},"helpers",[45,15427,240],{"class":85},[45,15429,15430],{"class":3095},"py\n",[45,15432,15433,15436,15438],{"class":47,"line":63},[45,15434,15435],{"class":51},"main",[45,15437,240],{"class":85},[45,15439,15430],{"class":3095},[712,15441,15443],{"id":15442},"helperspy",[18,15444,15445],{},"helpers.py",[36,15447,15449],{"className":38,"code":15448,"language":40,"meta":41,"style":41},"def greet(name):\n    return f\"Hello, {name}!\"\n",[18,15450,15451,15463],{"__ignoreMap":41},[45,15452,15453,15455,15457,15459,15461],{"class":47,"line":48},[45,15454,2757],{"class":2440},[45,15456,2936],{"class":2455},[45,15458,96],{"class":85},[45,15460,3096],{"class":3082},[45,15462,2464],{"class":85},[45,15464,15465,15467,15470,15472,15474,15476,15478],{"class":47,"line":63},[45,15466,4681],{"class":73},[45,15468,15469],{"class":2440}," f",[45,15471,5548],{"class":103},[45,15473,5551],{"class":59},[45,15475,3096],{"class":51},[45,15477,5556],{"class":59},[45,15479,15480],{"class":103},"!\"\n",[712,15482,15484],{"id":15483},"mainpy",[18,15485,15486],{},"main.py",[36,15488,15490],{"className":38,"code":15489,"language":40,"meta":41,"style":41},"import helpers\n\nmessage = helpers.greet(\"Sam\")\nprint(message)\n",[18,15491,15492,15499,15503,15527],{"__ignoreMap":41},[45,15493,15494,15496],{"class":47,"line":48},[45,15495,14870],{"class":73},[45,15497,15498],{"class":51}," helpers\n",[45,15500,15501],{"class":47,"line":63},[45,15502,67],{"emptyLinePlaceholder":66},[45,15504,15505,15508,15510,15513,15515,15517,15519,15521,15523,15525],{"class":47,"line":70},[45,15506,15507],{"class":51},"message ",[45,15509,56],{"class":55},[45,15511,15512],{"class":51}," helpers",[45,15514,240],{"class":85},[45,15516,2982],{"class":2070},[45,15518,96],{"class":85},[45,15520,100],{"class":99},[45,15522,1568],{"class":103},[45,15524,100],{"class":99},[45,15526,109],{"class":85},[45,15528,15529,15531,15533,15535],{"class":47,"line":89},[45,15530,2065],{"class":92},[45,15532,96],{"class":85},[45,15534,6462],{"class":2070},[45,15536,109],{"class":85},[14,15538,177],{},[36,15540,15542],{"className":38,"code":15541,"language":40,"meta":41,"style":41},"Hello, Sam!\n",[18,15543,15544],{"__ignoreMap":41},[45,15545,15546,15548,15550],{"class":47,"line":48},[45,15547,3226],{"class":51},[45,15549,3079],{"class":85},[45,15551,15552],{"class":51}," Sam!\n",[14,15554,15555],{},"This works because:",[195,15557,15558,15565],{},[198,15559,15560,15562,15563],{},[18,15561,15445],{}," is in the same folder as ",[18,15564,15486],{},[198,15566,15567,15568],{},"the module name is the file name without ",[18,15569,15010],{},[14,15571,15572],{},"So:",[195,15574,15575,15582],{},[198,15576,15577,15579,15580],{},[18,15578,15445],{}," becomes ",[18,15581,15425],{},[198,15583,15584,15579,15587],{},[18,15585,15586],{},"tools.py",[18,15588,15589],{},"tools",[14,15591,15592],{},"Use simple file names:",[195,15594,15595,15598,15601,15604],{},[198,15596,15597],{},"lowercase is best",[198,15599,15600],{},"avoid spaces",[198,15602,15603],{},"avoid special characters",[198,15605,15606],{},"do not start the file name with a number",[14,15608,15609,15610,240],{},"If you want more step-by-step examples, see ",[236,15611,15613],{"href":15612},"\u002Fhow-to\u002Fhow-to-import-a-module-in-python","how to import a module in Python",[31,15615,15617],{"id":15616},"how-python-finds-modules","How Python finds modules",[14,15619,15620],{},"When you write an import statement, Python has to find that module.",[14,15622,15623],{},"At a beginner level, the main idea is simple. Python checks places such as:",[195,15625,15626,15629,15632,15635],{},[198,15627,15628],{},"built-in modules",[198,15630,15631],{},"installed packages",[198,15633,15634],{},"folders in the module search path",[198,15636,15637],{},"your current project folder",[14,15639,15640,15641,240],{},"That search path is stored in ",[18,15642,15643],{},"sys.path",[14,15645,15646],{},"You do not need to memorize the internals yet, but it helps to know that Python only imports modules it can find in one of those locations.",[14,15648,15649],{},"You can inspect the search path with this command:",[36,15651,15655],{"className":15652,"code":15653,"language":15654,"meta":41,"style":41},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python -c \"import sys; print(sys.path)\"\n","bash",[18,15656,15657],{"__ignoreMap":41},[45,15658,15659,15661,15665,15667,15670],{"class":47,"line":48},[45,15660,40],{"class":2444},[45,15662,15664],{"class":15663},"stzsN"," -c",[45,15666,1565],{"class":99},[45,15668,15669],{"class":103},"import sys; print(sys.path)",[45,15671,1571],{"class":99},[14,15673,15674,15675,15678,15679,240],{},"If a module cannot be found, you will often get a ",[18,15676,15677],{},"ModuleNotFoundError",". For help with that, see ",[236,15680,15682],{"href":15681},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix","ModuleNotFoundError: No module named X",[14,15684,15685,15686,15690,15691,240],{},"If you want to learn more about the search path itself, see ",[236,15687,15689],{"href":15688},"\u002Fstandard-library\u002Fsys.path-explained","sys.path explained"," and the ",[236,15692,15694],{"href":15693},"\u002Fstandard-library\u002Fpython-sys-module-overview","Python sys module overview",[31,15696,15698],{"id":15697},"what-happens-when-a-module-is-imported","What happens when a module is imported",[14,15700,15701],{},"Importing a module does more than just “copy code.”",[14,15703,15704],{},"When Python imports a module:",[195,15706,15707,15710,15713],{},[198,15708,15709],{},"it runs the module’s top-level code",[198,15711,15712],{},"it defines the functions and classes in that file",[198,15714,15715],{},"it stores the loaded module so it can be reused",[14,15717,466],{},[712,15719,15721],{"id":15720},"example_modulepy",[18,15722,15723],{},"example_module.py",[36,15725,15727],{"className":38,"code":15726,"language":40,"meta":41,"style":41},"print(\"example_module is running\")\n\ndef say_hello():\n    print(\"Hello\")\n",[18,15728,15729,15744,15748,15756],{"__ignoreMap":41},[45,15730,15731,15733,15735,15737,15740,15742],{"class":47,"line":48},[45,15732,2065],{"class":92},[45,15734,96],{"class":85},[45,15736,100],{"class":99},[45,15738,15739],{"class":103},"example_module is running",[45,15741,100],{"class":99},[45,15743,109],{"class":85},[45,15745,15746],{"class":47,"line":63},[45,15747,67],{"emptyLinePlaceholder":66},[45,15749,15750,15752,15754],{"class":47,"line":70},[45,15751,2757],{"class":2440},[45,15753,4596],{"class":2455},[45,15755,3828],{"class":85},[45,15757,15758,15760,15762,15764,15766,15768],{"class":47,"line":89},[45,15759,93],{"class":92},[45,15761,96],{"class":85},[45,15763,100],{"class":99},[45,15765,3226],{"class":103},[45,15767,100],{"class":99},[45,15769,109],{"class":85},[712,15771,15773],{"id":15772},"mainpy-1",[18,15774,15486],{},[36,15776,15778],{"className":38,"code":15777,"language":40,"meta":41,"style":41},"import example_module\n\nexample_module.say_hello()\n",[18,15779,15780,15787,15791],{"__ignoreMap":41},[45,15781,15782,15784],{"class":47,"line":48},[45,15783,14870],{"class":73},[45,15785,15786],{"class":51}," example_module\n",[45,15788,15789],{"class":47,"line":63},[45,15790,67],{"emptyLinePlaceholder":66},[45,15792,15793,15796,15798,15800],{"class":47,"line":70},[45,15794,15795],{"class":51},"example_module",[45,15797,240],{"class":85},[45,15799,4702],{"class":2070},[45,15801,2495],{"class":85},[14,15803,177],{},[36,15805,15807],{"className":38,"code":15806,"language":40,"meta":41,"style":41},"example_module is running\nHello\n",[18,15808,15809,15819],{"__ignoreMap":41},[45,15810,15811,15814,15816],{"class":47,"line":48},[45,15812,15813],{"class":51},"example_module ",[45,15815,1285],{"class":55},[45,15817,15818],{"class":51}," running\n",[45,15820,15821],{"class":47,"line":63},[45,15822,15823],{"class":51},"Hello\n",[14,15825,15826],{},"Notice what happened:",[195,15828,15829,15835],{},[198,15830,3267,15831,15834],{},[18,15832,15833],{},"print()"," at the top level ran during import",[198,15836,15837],{},"the function became available after the import",[14,15839,15840],{},"This can surprise beginners. If a file has code at the top level, that code runs when the file is imported.",[14,15842,15843,15844,15846],{},"Also, repeated imports in the same program usually do ",[2421,15845,2857],{}," rerun the whole file, because Python caches imported modules.",[31,15848,2715,15850,15853],{"id":15849},"why-__name__-__main__-matters",[18,15851,15852],{},"__name__ == '__main__'"," matters",[14,15855,15856],{},"This pattern helps prevent test code or demo code from running during import.",[36,15858,15860],{"className":38,"code":15859,"language":40,"meta":41,"style":41},"def greet(name):\n    return f\"Hello, {name}!\"\n\nif __name__ == \"__main__\":\n    print(greet(\"Sam\"))\n",[18,15861,15862,15874,15890,15894,15912],{"__ignoreMap":41},[45,15863,15864,15866,15868,15870,15872],{"class":47,"line":48},[45,15865,2757],{"class":2440},[45,15867,2936],{"class":2455},[45,15869,96],{"class":85},[45,15871,3096],{"class":3082},[45,15873,2464],{"class":85},[45,15875,15876,15878,15880,15882,15884,15886,15888],{"class":47,"line":63},[45,15877,4681],{"class":73},[45,15879,15469],{"class":2440},[45,15881,5548],{"class":103},[45,15883,5551],{"class":59},[45,15885,3096],{"class":51},[45,15887,5556],{"class":59},[45,15889,15480],{"class":103},[45,15891,15892],{"class":47,"line":70},[45,15893,67],{"emptyLinePlaceholder":66},[45,15895,15896,15898,15901,15903,15905,15908,15910],{"class":47,"line":89},[45,15897,20],{"class":73},[45,15899,15900],{"class":2771}," __name__",[45,15902,9334],{"class":55},[45,15904,1565],{"class":99},[45,15906,15907],{"class":103},"__main__",[45,15909,100],{"class":99},[45,15911,86],{"class":85},[45,15913,15914,15916,15918,15920,15922,15924,15926,15928],{"class":47,"line":112},[45,15915,93],{"class":92},[45,15917,96],{"class":85},[45,15919,2982],{"class":2070},[45,15921,96],{"class":85},[45,15923,100],{"class":99},[45,15925,1568],{"class":103},[45,15927,100],{"class":99},[45,15929,4348],{"class":85},[14,15931,15932],{},"If you run the file directly:",[36,15934,15936],{"className":15652,"code":15935,"language":15654,"meta":41,"style":41},"python helpers.py\n",[18,15937,15938],{"__ignoreMap":41},[45,15939,15940,15942],{"class":47,"line":48},[45,15941,40],{"class":2444},[45,15943,15944],{"class":103}," helpers.py\n",[14,15946,15947],{},"then the code inside this block runs.",[14,15949,15950,15951,15953,15954,15956],{},"But if another file imports ",[18,15952,15425],{},", this block does ",[2421,15955,2857],{}," run.",[14,15958,15959],{},"That makes it useful for:",[195,15961,15962,15965,15968],{},[198,15963,15964],{},"quick tests",[198,15966,15967],{},"example usage",[198,15969,15970],{},"demo output inside a module",[14,15972,15973],{},"Without this pattern, top-level test code can run every time the module is imported.",[31,15975,15977],{"id":15976},"common-beginner-import-mistakes","Common beginner import mistakes",[14,15979,15980],{},"Here are some of the most common problems beginners run into.",[712,15982,15984],{"id":15983},"naming-your-file-like-a-standard-library-module","Naming your file like a standard library module",[14,15986,15987,15988,21,15991,15994,15995,15998],{},"If you create a file called ",[18,15989,15990],{},"random.py",[18,15992,15993],{},"json.py",", or ",[18,15996,15997],{},"sys.py",", it can conflict with Python’s real module of the same name.",[14,16000,16001],{},"Example problem:",[36,16003,16005],{"className":38,"code":16004,"language":40,"meta":41,"style":41},"# random.py\nimport random\nprint(random.randint(1, 10))\n",[18,16006,16007,16012,16019],{"__ignoreMap":41},[45,16008,16009],{"class":47,"line":48},[45,16010,16011],{"class":7624},"# random.py\n",[45,16013,16014,16016],{"class":47,"line":63},[45,16015,14870],{"class":73},[45,16017,16018],{"class":51}," random\n",[45,16020,16021,16023,16025,16027,16029,16032,16034,16036,16038,16040],{"class":47,"line":70},[45,16022,2065],{"class":92},[45,16024,96],{"class":85},[45,16026,15028],{"class":2070},[45,16028,240],{"class":85},[45,16030,16031],{"class":2070},"randint",[45,16033,96],{"class":85},[45,16035,8578],{"class":59},[45,16037,3079],{"class":85},[45,16039,1190],{"class":59},[45,16041,4348],{"class":85},[14,16043,16044],{},"This can break because your file may be imported instead of the standard library module.",[14,16046,16047],{},"Use unique file names for your own scripts.",[712,16049,16051],{"id":16050},"importing-from-the-wrong-folder","Importing from the wrong folder",[14,16053,16054],{},"If your file is not in the expected location, Python may not find it.",[14,16056,16057],{},"Common causes:",[195,16059,16060,16063,16066],{},[198,16061,16062],{},"the file is in a different folder",[198,16064,16065],{},"you are running the script from the wrong directory",[198,16067,16068],{},"your project structure is not what you think it is",[712,16070,1350,16072],{"id":16071},"using-from-module-import",[18,16073,16074],{},"from module import *",[14,16076,16077],{},"Wildcard imports make code harder to read.",[36,16079,16081],{"className":38,"code":16080,"language":40,"meta":41,"style":41},"from math import *\n",[18,16082,16083],{"__ignoreMap":41},[45,16084,16085,16087,16089,16091],{"class":47,"line":48},[45,16086,14921],{"class":73},[45,16088,14924],{"class":51},[45,16090,14870],{"class":73},[45,16092,16093],{"class":55}," *\n",[14,16095,16096],{},"This imports many names without showing where they came from.",[14,16098,16099],{},"That can cause confusion because:",[195,16101,16102,16105,16108],{},[198,16103,16104],{},"readers cannot easily tell where a name came from",[198,16106,16107],{},"names can overwrite each other",[198,16109,16110],{},"debugging becomes harder",[712,16112,16114],{"id":16113},"forgetting-to-install-a-package","Forgetting to install a package",[14,16116,16117],{},"Some modules are built in, but many are not.",[14,16119,16120],{},"For example, this may fail if the package is not installed:",[36,16122,16124],{"className":38,"code":16123,"language":40,"meta":41,"style":41},"import requests\n",[18,16125,16126],{"__ignoreMap":41},[45,16127,16128,16130],{"class":47,"line":48},[45,16129,14870],{"class":73},[45,16131,16132],{"class":51}," requests\n",[14,16134,16135,16136,9417,16139,240],{},"If needed, install it with ",[18,16137,16138],{},"pip",[236,16140,16142],{"href":16141},"\u002Fhow-to\u002Fhow-to-install-a-python-package-with-pip","how to install a Python package with pip",[14,16144,16145,16146,240],{},"If you use virtual environments, also see ",[236,16147,16149],{"href":16148},"\u002Fhow-to\u002Fhow-to-create-and-use-a-virtual-environment-in-python","how to create and use a virtual environment in Python",[712,16151,16153],{"id":16152},"expecting-import-to-copy-code","Expecting import to copy code",[14,16155,16156,16158],{},[18,16157,14870],{}," does not paste code into your file.",[14,16160,16161,16162,16165],{},"Instead, Python loads the module and lets you access its contents. That is why ",[18,16163,16164],{},"math.sqrt()"," still uses the module name, and why top-level code in the module can run during import.",[31,16167,16169],{"id":16168},"best-practices-for-beginners","Best practices for beginners",[14,16171,16172],{},"These habits will help you avoid many import problems.",[195,16174,16175,16182,16188,16193,16196],{},[198,16176,16177,16178,16181],{},"Prefer ",[18,16179,16180],{},"import module"," when possible",[198,16183,165,16184,16187],{},[18,16185,16186],{},"from module import name"," when you only need one or two names",[198,16189,16190,16191],{},"Avoid ",[18,16192,16074],{},[198,16194,16195],{},"Keep module file names simple and lowercase",[198,16197,16198,16199],{},"Put test or demo code inside ",[18,16200,16201],{},"if __name__ == \"__main__\":",[14,16203,16204],{},"A good beginner-friendly style looks like this:",[36,16206,16208],{"className":38,"code":16207,"language":40,"meta":41,"style":41},"import math\n\narea = math.pi * 5 ** 2\nprint(area)\n",[18,16209,16210,16216,16220,16242],{"__ignoreMap":41},[45,16211,16212,16214],{"class":47,"line":48},[45,16213,14870],{"class":73},[45,16215,14891],{"class":51},[45,16217,16218],{"class":47,"line":63},[45,16219,67],{"emptyLinePlaceholder":66},[45,16221,16222,16225,16227,16229,16231,16234,16236,16238,16240],{"class":47,"line":70},[45,16223,16224],{"class":51},"area ",[45,16226,56],{"class":55},[45,16228,15330],{"class":51},[45,16230,240],{"class":85},[45,16232,16233],{"class":3095},"pi",[45,16235,3559],{"class":55},[45,16237,1222],{"class":59},[45,16239,5609],{"class":55},[45,16241,13423],{"class":59},[45,16243,16244,16246,16248,16250],{"class":47,"line":89},[45,16245,2065],{"class":92},[45,16247,96],{"class":85},[45,16249,3605],{"class":2070},[45,16251,109],{"class":85},[14,16253,16254,16255,16257,16258,240],{},"This is easy to read because it is clear that ",[18,16256,16233],{}," came from ",[18,16259,14900],{},[31,16261,16263],{"id":16262},"common-causes-of-import-problems","Common causes of import problems",[14,16265,16266],{},"If your import is not working, check these first:",[195,16268,16269,16272,16275,16278,16281],{},[198,16270,16271],{},"the module file is not in the expected folder",[198,16273,16274],{},"your file name conflicts with a standard library module or installed package",[198,16276,16277],{},"the package is not installed",[198,16279,16280],{},"the spelling or capitalization is wrong in the import statement",[198,16282,16283],{},"top-level code in the imported file runs and causes unexpected behavior",[14,16285,16286],{},"Useful commands:",[36,16288,16290],{"className":15652,"code":16289,"language":15654,"meta":41,"style":41},"python --version\npython your_script.py\npython -c \"import sys; print(sys.path)\"\npython -c \"import math; print(math.__file__ if hasattr(math, '__file__') else 'built-in module')\"\npip show requests\n",[18,16291,16292,16299,16306,16318,16331],{"__ignoreMap":41},[45,16293,16294,16296],{"class":47,"line":48},[45,16295,40],{"class":2444},[45,16297,16298],{"class":15663}," --version\n",[45,16300,16301,16303],{"class":47,"line":63},[45,16302,40],{"class":2444},[45,16304,16305],{"class":103}," your_script.py\n",[45,16307,16308,16310,16312,16314,16316],{"class":47,"line":70},[45,16309,40],{"class":2444},[45,16311,15664],{"class":15663},[45,16313,1565],{"class":99},[45,16315,15669],{"class":103},[45,16317,1571],{"class":99},[45,16319,16320,16322,16324,16326,16329],{"class":47,"line":89},[45,16321,40],{"class":2444},[45,16323,15664],{"class":15663},[45,16325,1565],{"class":99},[45,16327,16328],{"class":103},"import math; print(math.__file__ if hasattr(math, '__file__') else 'built-in module')",[45,16330,1571],{"class":99},[45,16332,16333,16335,16338],{"class":47,"line":112},[45,16334,16138],{"class":2444},[45,16336,16337],{"class":103}," show",[45,16339,16132],{"class":103},[14,16341,16342],{},"These help you check:",[195,16344,16345,16348,16351,16354],{},[198,16346,16347],{},"which Python version you are using",[198,16349,16350],{},"whether the script runs in the environment you expect",[198,16352,16353],{},"where Python is looking for modules",[198,16355,16356],{},"whether a package is installed",[31,16358,2281],{"id":2280},[712,16360,16362,16363,1316,16365,354],{"id":16361},"what-is-the-difference-between-import-math-and-from-math-import-sqrt","What is the difference between ",[18,16364,14973],{},[18,16366,14982],{},[14,16368,16369,14974,16371,240],{},[18,16370,14973],{},[18,16372,16164],{},[14,16374,16375,16377,16378,240],{},[18,16376,14982],{}," imports one name directly, so you write ",[18,16379,15378],{},[712,16381,16383,16384,354],{"id":16382},"why-does-python-say-modulenotfounderror","Why does Python say ",[18,16385,15677],{},[14,16387,16388],{},"Usually Python cannot find the module file or package, or the package is not installed in the current environment.",[14,16390,16391,16392,16396],{},"See also ",[236,16393,16395],{"href":16394},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix","ImportError: No module named X"," if you are troubleshooting an import problem.",[712,16398,16400],{"id":16399},"does-python-run-an-imported-file-every-time","Does Python run an imported file every time?",[14,16402,16403],{},"Normally no.",[14,16405,16406],{},"Python caches imported modules, so repeated imports in the same run usually reuse the loaded module.",[712,16408,16410],{"id":16409},"can-i-import-my-own-python-file","Can I import my own Python file?",[14,16412,16413],{},"Yes.",[14,16415,16416],{},"If the file is in the right folder and has a valid name, Python can import it as a module.",[712,16418,16420,16421,354],{"id":16419},"should-beginners-use-from-module-import","Should beginners use ",[18,16422,16074],{},[14,16424,16425],{},"Usually no.",[14,16427,16428],{},"It makes code harder to read because it hides where names came from.",[31,16430,2342],{"id":2341},[195,16432,16433,16437,16442,16447,16452,16456,16460,16464],{},[198,16434,16435],{},[236,16436,5898],{"href":15052},[198,16438,16439],{},[236,16440,16441],{"href":15612},"How to import a module in Python",[198,16443,16444],{},[236,16445,16446],{"href":16141},"How to install a Python package with pip",[198,16448,16449],{},[236,16450,16451],{"href":16148},"How to create and use a virtual environment in Python",[198,16453,16454],{},[236,16455,15682],{"href":15681},[198,16457,16458],{},[236,16459,16395],{"href":16394},[198,16461,16462],{},[236,16463,15694],{"href":15693},[198,16465,16466],{},[236,16467,15689],{"href":15688},[2367,16469,16470],{},"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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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":41,"searchDepth":63,"depth":63,"links":16472},[16473,16474,16475,16481,16482,16486,16487,16491,16493,16501,16502,16503,16513],{"id":33,"depth":63,"text":34},{"id":14989,"depth":63,"text":14990},{"id":15055,"depth":63,"text":15056,"children":16476},[16477,16478,16479,16480],{"id":15062,"depth":70,"text":15065},{"id":15119,"depth":70,"text":15122},{"id":15175,"depth":70,"text":15178},{"id":15235,"depth":70,"text":15238},{"id":15291,"depth":63,"text":15292},{"id":15408,"depth":63,"text":15409,"children":16483},[16484,16485],{"id":15442,"depth":70,"text":15445},{"id":15483,"depth":70,"text":15486},{"id":15616,"depth":63,"text":15617},{"id":15697,"depth":63,"text":15698,"children":16488},[16489,16490],{"id":15720,"depth":70,"text":15723},{"id":15772,"depth":70,"text":15486},{"id":15849,"depth":63,"text":16492},"Why __name__ == '__main__' matters",{"id":15976,"depth":63,"text":15977,"children":16494},[16495,16496,16497,16499,16500],{"id":15983,"depth":70,"text":15984},{"id":16050,"depth":70,"text":16051},{"id":16071,"depth":70,"text":16498},"Using from module import *",{"id":16113,"depth":70,"text":16114},{"id":16152,"depth":70,"text":16153},{"id":16168,"depth":63,"text":16169},{"id":16262,"depth":63,"text":16263},{"id":2280,"depth":63,"text":2281,"children":16504},[16505,16507,16509,16510,16511],{"id":16361,"depth":70,"text":16506},"What is the difference between import math and from math import sqrt?",{"id":16382,"depth":70,"text":16508},"Why does Python say ModuleNotFoundError?",{"id":16399,"depth":70,"text":16400},{"id":16409,"depth":70,"text":16410},{"id":16419,"depth":70,"text":16512},"Should beginners use from module import *?",{"id":2341,"depth":63,"text":2342},"Master how import works in python in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fhow-import-works-in-python",{"title":14860,"description":16514},"learn\u002Fhow-import-works-in-python","Dy6beggA2Q2Qrjafc_QobeW3VlxOwC-tjH-KbCkuovA",{"id":16521,"title":16522,"body":16523,"description":17493,"extension":2399,"meta":17494,"navigation":66,"path":17495,"seo":17496,"stem":17497,"__hash__":17498},"content\u002Flearn\u002Fhow-to-install-python-on-windows-macos-and-linux.md","How to Install Python on Windows, macOS, and Linux",{"type":7,"value":16524,"toc":17453},[16525,16528,16531,16534,16538,16541,16559,16562,16566,16587,16591,16594,16612,16615,16619,16622,16648,16651,16662,16665,16676,16680,16707,16711,16714,16752,16762,16766,16775,16779,16782,16795,16799,16805,16815,16818,16838,16848,16855,16859,16862,16872,16875,16879,16898,16902,16918,16922,16940,16943,16953,16959,16963,16966,16969,16985,16988,16997,17000,17009,17016,17036,17039,17045,17048,17052,17057,17069,17092,17096,17099,17103,17106,17126,17130,17147,17156,17168,17175,17179,17182,17202,17205,17224,17228,17231,17235,17238,17240,17256,17268,17272,17275,17278,17321,17325,17343,17346,17350,17374,17376,17380,17383,17387,17392,17396,17399,17403,17406,17410,17417,17419,17450],[10,16526,16522],{"id":16527},"how-to-install-python-on-windows-macos-and-linux",[14,16529,16530],{},"Installing Python is one of the first steps in learning the language. This guide shows you how to install Python 3 on Windows, macOS, and Linux, how to check whether it is already installed, and how to verify that it works correctly.",[14,16532,16533],{},"The goal is simple: get one working Python 3 installation that you can run from the command line without confusion.",[31,16535,16537],{"id":16536},"quick-check","Quick check",[14,16539,16540],{},"Run these commands after installation to check whether Python is installed and which command name your system uses:",[36,16542,16544],{"className":15652,"code":16543,"language":15654,"meta":41,"style":41},"python --version\npython3 --version\n",[18,16545,16546,16552],{"__ignoreMap":41},[45,16547,16548,16550],{"class":47,"line":48},[45,16549,40],{"class":2444},[45,16551,16298],{"class":15663},[45,16553,16554,16557],{"class":47,"line":63},[45,16555,16556],{"class":2444},"python3",[45,16558,16298],{"class":15663},[14,16560,16561],{},"If one of these shows a Python 3 version number, Python is installed.",[31,16563,16565],{"id":16564},"what-this-page-helps-you-do","What this page helps you do",[195,16567,16568,16571,16574,16577,16584],{},[198,16569,16570],{},"Install Python on Windows, macOS, or Linux",[198,16572,16573],{},"Check whether Python is already installed",[198,16575,16576],{},"Verify the installation from the command line",[198,16578,16579,16580,230,16582],{},"Understand whether to use ",[18,16581,40],{},[18,16583,16556],{},[198,16585,16586],{},"Avoid common setup mistakes",[31,16588,16590],{"id":16589},"before-you-install-python","Before you install Python",[14,16592,16593],{},"A few important things to know first:",[195,16595,16596,16599,16602,16605],{},[198,16597,16598],{},"You only need one working Python 3 installation to start learning.",[198,16600,16601],{},"Many systems already include some version of Python.",[198,16603,16604],{},"The main goal is to make sure Python 3 is installed and easy to run.",[198,16606,16607,16608,1353,16610,240],{},"It is normal if your computer uses ",[18,16609,16556],{},[18,16611,40],{},[14,16613,16614],{},"If you are a beginner, install Python 3 and ignore Python 2. Python 2 is outdated and should not be used for new code.",[31,16616,16618],{"id":16617},"how-to-check-if-python-is-already-installed","How to check if Python is already installed",[14,16620,16621],{},"Before downloading anything, check whether Python is already available.",[195,16623,16624,16637],{},[198,16625,16626,16627,16630,16631,230,16634],{},"On ",[2421,16628,16629],{},"Windows",", open ",[2421,16632,16633],{},"Command Prompt",[2421,16635,16636],{},"PowerShell",[198,16638,16626,16639,230,16642,16630,16645],{},[2421,16640,16641],{},"macOS",[2421,16643,16644],{},"Linux",[2421,16646,16647],{},"Terminal",[14,16649,16650],{},"Then run:",[36,16652,16654],{"className":15652,"code":16653,"language":15654,"meta":41,"style":41},"python --version\n",[18,16655,16656],{"__ignoreMap":41},[45,16657,16658,16660],{"class":47,"line":48},[45,16659,40],{"class":2444},[45,16661,16298],{"class":15663},[14,16663,16664],{},"If that does not work, try:",[36,16666,16668],{"className":15652,"code":16667,"language":15654,"meta":41,"style":41},"python3 --version\n",[18,16669,16670],{"__ignoreMap":41},[45,16671,16672,16674],{"class":47,"line":48},[45,16673,16556],{"class":2444},[45,16675,16298],{"class":15663},[712,16677,16679],{"id":16678},"what-the-result-means","What the result means",[195,16681,16682,16689,16699],{},[198,16683,16684,16685,16688],{},"If you see something like ",[18,16686,16687],{},"Python 3.12.3",", Python is installed.",[198,16690,16691,16692,230,16695,16698],{},"If you get a message such as ",[18,16693,16694],{},"command not found",[18,16696,16697],{},"'python' is not recognized",", Python is not available through that command.",[198,16700,3259,16701,16703,16704,16706],{},[18,16702,40],{}," fails but ",[18,16705,16556],{}," works, that is normal on many systems.",[31,16708,16710],{"id":16709},"install-python-on-windows","Install Python on Windows",[14,16712,16713],{},"To install Python on Windows:",[346,16715,16716,16722,16725,16728,16733,16739,16742,16749],{},[198,16717,16718,16719],{},"Go to the official Python website: ",[18,16720,16721],{},"https:\u002F\u002Fwww.python.org\u002F",[198,16723,16724],{},"Download the latest Python 3 installer for Windows.",[198,16726,16727],{},"Run the installer.",[198,16729,16730],{},[2421,16731,16732],{},"Turn on the checkbox that says \"Add Python to PATH\"",[198,16734,16735,16736],{},"Click ",[2421,16737,16738],{},"Install Now",[198,16740,16741],{},"Wait for the installation to finish",[198,16743,16744,16745,16748],{},"Open a ",[2421,16746,16747],{},"new"," Command Prompt window",[198,16750,16751],{},"Run:",[36,16753,16754],{"className":15652,"code":16653,"language":15654,"meta":41,"style":41},[18,16755,16756],{"__ignoreMap":41},[45,16757,16758,16760],{"class":47,"line":48},[45,16759,40],{"class":2444},[45,16761,16298],{"class":15663},[712,16763,16765],{"id":16764},"important-windows-tip","Important Windows tip",[14,16767,1732,16768,16771,16772,16774],{},[2421,16769,16770],{},"Add Python to PATH"," checkbox matters. If you skip it, Python may install correctly but the ",[18,16773,40],{}," command may not work in Command Prompt.",[712,16776,16778],{"id":16777},"if-installation-worked","If installation worked",[14,16780,16781],{},"You should see a version number, for example:",[36,16783,16785],{"className":15652,"code":16784,"language":15654,"meta":41,"style":41},"Python 3.12.3\n",[18,16786,16787],{"__ignoreMap":41},[45,16788,16789,16792],{"class":47,"line":48},[45,16790,16791],{"class":2444},"Python",[45,16793,16794],{"class":59}," 3.12.3\n",[31,16796,16798],{"id":16797},"install-python-on-macos","Install Python on macOS",[14,16800,16801,16802,16804],{},"Many macOS systems already have ",[18,16803,16556],{}," available, so check first:",[36,16806,16807],{"className":15652,"code":16667,"language":15654,"meta":41,"style":41},[18,16808,16809],{"__ignoreMap":41},[45,16810,16811,16813],{"class":47,"line":48},[45,16812,16556],{"class":2444},[45,16814,16298],{"class":15663},[14,16816,16817],{},"If that does not work:",[346,16819,16820,16824,16827,16830,16833,16836],{},[198,16821,16718,16822],{},[18,16823,16721],{},[198,16825,16826],{},"Download the latest Python 3 installer for macOS",[198,16828,16829],{},"Open the installer package",[198,16831,16832],{},"Follow the installation steps",[198,16834,16835],{},"Open Terminal after installation",[198,16837,16751],{},[36,16839,16840],{"className":15652,"code":16667,"language":15654,"meta":41,"style":41},[18,16841,16842],{"__ignoreMap":41},[45,16843,16844,16846],{"class":47,"line":48},[45,16845,16556],{"class":2444},[45,16847,16298],{"class":15663},[14,16849,16850,16851,1353,16853,240],{},"On many macOS systems, you should use ",[18,16852,16556],{},[18,16854,40],{},[31,16856,16858],{"id":16857},"install-python-on-linux","Install Python on Linux",[14,16860,16861],{},"Many Linux distributions already include Python 3. Check first:",[36,16863,16864],{"className":15652,"code":16667,"language":15654,"meta":41,"style":41},[18,16865,16866],{"__ignoreMap":41},[45,16867,16868,16870],{"class":47,"line":48},[45,16869,16556],{"class":2444},[45,16871,16298],{"class":15663},[14,16873,16874],{},"If Python 3 is not installed, use your package manager.",[712,16876,16878],{"id":16877},"ubuntu-or-debian","Ubuntu or Debian",[36,16880,16882],{"className":15652,"code":16881,"language":15654,"meta":41,"style":41},"sudo apt install python3\n",[18,16883,16884],{"__ignoreMap":41},[45,16885,16886,16889,16892,16895],{"class":47,"line":48},[45,16887,16888],{"class":2444},"sudo",[45,16890,16891],{"class":103}," apt",[45,16893,16894],{"class":103}," install",[45,16896,16897],{"class":103}," python3\n",[712,16899,16901],{"id":16900},"fedora","Fedora",[36,16903,16905],{"className":15652,"code":16904,"language":15654,"meta":41,"style":41},"sudo dnf install python3\n",[18,16906,16907],{"__ignoreMap":41},[45,16908,16909,16911,16914,16916],{"class":47,"line":48},[45,16910,16888],{"class":2444},[45,16912,16913],{"class":103}," dnf",[45,16915,16894],{"class":103},[45,16917,16897],{"class":103},[712,16919,16921],{"id":16920},"arch-linux","Arch Linux",[36,16923,16925],{"className":15652,"code":16924,"language":15654,"meta":41,"style":41},"sudo pacman -S python\n",[18,16926,16927],{"__ignoreMap":41},[45,16928,16929,16931,16934,16937],{"class":47,"line":48},[45,16930,16888],{"class":2444},[45,16932,16933],{"class":103}," pacman",[45,16935,16936],{"class":15663}," -S",[45,16938,16939],{"class":103}," python\n",[14,16941,16942],{},"After installation, verify it:",[36,16944,16945],{"className":15652,"code":16667,"language":15654,"meta":41,"style":41},[18,16946,16947],{"__ignoreMap":41},[45,16948,16949,16951],{"class":47,"line":48},[45,16950,16556],{"class":2444},[45,16952,16298],{"class":15663},[14,16954,16955,16956,16958],{},"On Linux, ",[18,16957,16556],{}," is often the correct command for Python 3.",[31,16960,16962],{"id":16961},"how-to-verify-the-installation","How to verify the installation",[14,16964,16965],{},"After installing Python, confirm that it actually runs.",[14,16967,16968],{},"First, check the version:",[36,16970,16971],{"className":15652,"code":16543,"language":15654,"meta":41,"style":41},[18,16972,16973,16979],{"__ignoreMap":41},[45,16974,16975,16977],{"class":47,"line":48},[45,16976,40],{"class":2444},[45,16978,16298],{"class":15663},[45,16980,16981,16983],{"class":47,"line":63},[45,16982,16556],{"class":2444},[45,16984,16298],{"class":15663},[14,16986,16987],{},"Then start the Python shell with whichever command works on your system:",[36,16989,16991],{"className":15652,"code":16990,"language":15654,"meta":41,"style":41},"python\n",[18,16992,16993],{"__ignoreMap":41},[45,16994,16995],{"class":47,"line":48},[45,16996,16990],{"class":2444},[14,16998,16999],{},"or:",[36,17001,17003],{"className":15652,"code":17002,"language":15654,"meta":41,"style":41},"python3\n",[18,17004,17005],{"__ignoreMap":41},[45,17006,17007],{"class":47,"line":48},[45,17008,17002],{"class":2444},[14,17010,17011,17012,17015],{},"You should see a prompt like ",[18,17013,17014],{},">>>",". Now type:",[36,17017,17019],{"className":38,"code":17018,"language":40,"meta":41,"style":41},"print(\"Hello, world!\")\n",[18,17020,17021],{"__ignoreMap":41},[45,17022,17023,17025,17027,17029,17032,17034],{"class":47,"line":48},[45,17024,2065],{"class":92},[45,17026,96],{"class":85},[45,17028,100],{"class":99},[45,17030,17031],{"class":103},"Hello, world!",[45,17033,100],{"class":99},[45,17035,109],{"class":85},[14,17037,17038],{},"Expected output:",[36,17040,17043],{"className":17041,"code":17042,"language":5000,"meta":41},[4998],"Hello, world!\n",[18,17044,17042],{"__ignoreMap":41},[14,17046,17047],{},"If you see the output and no error appears, Python is working correctly.",[712,17049,17051],{"id":17050},"how-to-exit-the-python-shell","How to exit the Python shell",[195,17053,17054],{},[198,17055,17056],{},"On all systems, you can type:",[36,17058,17060],{"className":38,"code":17059,"language":40,"meta":41,"style":41},"exit()\n",[18,17061,17062],{"__ignoreMap":41},[45,17063,17064,17067],{"class":47,"line":48},[45,17065,17066],{"class":92},"exit",[45,17068,2495],{"class":85},[195,17070,17071,17083],{},[198,17072,16626,17073,17075,17076,17079,17080],{},[2421,17074,16629],{},", you can also press ",[18,17077,17078],{},"Ctrl+Z",", then ",[18,17081,17082],{},"Enter",[198,17084,16626,17085,1316,17087,17075,17089],{},[2421,17086,16641],{},[2421,17088,16644],{},[18,17090,17091],{},"Ctrl+D",[31,17093,17095],{"id":17094},"understand-python-vs-python3","Understand python vs python3",[14,17097,17098],{},"This is one of the most common beginner questions.",[712,17100,17102],{"id":17101},"why-there-are-two-command-names","Why there are two command names",[14,17104,17105],{},"Different operating systems use different command names:",[195,17107,17108,17114,17120],{},[198,17109,17110,17111,17113],{},"Some systems map ",[18,17112,40],{}," to Python 3",[198,17115,17116,17117,17119],{},"Some systems use ",[18,17118,16556],{}," for Python 3",[198,17121,17122,17123,17125],{},"Older systems may reserve ",[18,17124,40],{}," for Python 2",[712,17127,17129],{"id":17128},"what-you-should-do","What you should do",[195,17131,17132,17138,17144],{},[198,17133,17134,17135],{},"Try ",[18,17136,17137],{},"python --version",[198,17139,17140,17141],{},"If that does not work, try ",[18,17142,17143],{},"python3 --version",[198,17145,17146],{},"Use the command that works on your system consistently",[14,17148,17149,17150,17152,17153,17155],{},"For example, if ",[18,17151,16556],{}," works and ",[18,17154,40],{}," does not, run your programs like this:",[36,17157,17159],{"className":15652,"code":17158,"language":15654,"meta":41,"style":41},"python3 my_file.py\n",[18,17160,17161],{"__ignoreMap":41},[45,17162,17163,17165],{"class":47,"line":48},[45,17164,16556],{"class":2444},[45,17166,17167],{"class":103}," my_file.py\n",[14,17169,17170,17171,240],{},"If you are not sure how to run a script after installation, see ",[236,17172,17174],{"href":17173},"\u002Flearn\u002Fhow-to-run-python-code-command-line-and-ides","how to run Python code from the command line and IDEs",[31,17176,17178],{"id":17177},"what-to-do-after-installation","What to do after installation",[14,17180,17181],{},"Once Python is installed, the next good steps are:",[195,17183,17184,17187,17190,17193,17199],{},[198,17185,17186],{},"Learn how to run a Python file",[198,17188,17189],{},"Create your first Python program",[198,17191,17192],{},"Choose a code editor or IDE",[198,17194,17195,17196,17198],{},"Install packages with ",[18,17197,16138],{}," later when needed",[198,17200,17201],{},"Ignore virtual environments for now unless a project specifically asks for one",[14,17203,17204],{},"Good next steps:",[195,17206,17207,17212,17218],{},[198,17208,17209],{},[236,17210,17211],{"href":17173},"Run Python code from the command line and IDEs",[198,17213,17214],{},[236,17215,17217],{"href":17216},"\u002Flearn\u002Fyour-first-python-program-hello-world-explained\u002F","Write your first Python program",[198,17219,17220],{},[236,17221,17223],{"href":17222},"\u002Fhow-to\u002Fhow-to-install-a-python-package-with-pip\u002F","Install a Python package with pip",[31,17225,17227],{"id":17226},"troubleshooting-common-install-problems","Troubleshooting common install problems",[14,17229,17230],{},"If Python still does not work after installation, these are the most common reasons.",[712,17232,17234],{"id":17233},"python-installed-but-command-not-found","Python installed but command not found",[14,17236,17237],{},"This usually means Python was installed, but the command is not available in your terminal.",[14,17239,16057],{},[195,17241,17242,17245,17248],{},[198,17243,17244],{},"Python was installed but not added to PATH",[198,17246,17247],{},"The terminal was not reopened after installation",[198,17249,17250,17251,17253,17254],{},"You are using ",[18,17252,40],{}," when your system expects ",[18,17255,16556],{},[14,17257,17258,17259,17261,17262,17264,17265,17267],{},"On Windows, try closing and reopening Command Prompt or PowerShell.",[7343,17260],{},"\nOn macOS and Linux, try ",[18,17263,16556],{}," if ",[18,17266,40],{}," fails.",[712,17269,17271],{"id":17270},"multiple-python-versions-are-causing-confusion","Multiple Python versions are causing confusion",[14,17273,17274],{},"Some computers already have another Python version installed. That can make it unclear which one is running.",[14,17276,17277],{},"Use these commands to check:",[36,17279,17281],{"className":15652,"code":17280,"language":15654,"meta":41,"style":41},"python --version\npython3 --version\nwhere python\nwhere python3\nwhich python\nwhich python3\n",[18,17282,17283,17289,17295,17302,17308,17315],{"__ignoreMap":41},[45,17284,17285,17287],{"class":47,"line":48},[45,17286,40],{"class":2444},[45,17288,16298],{"class":15663},[45,17290,17291,17293],{"class":47,"line":63},[45,17292,16556],{"class":2444},[45,17294,16298],{"class":15663},[45,17296,17297,17300],{"class":47,"line":70},[45,17298,17299],{"class":2444},"where",[45,17301,16939],{"class":103},[45,17303,17304,17306],{"class":47,"line":89},[45,17305,17299],{"class":2444},[45,17307,16897],{"class":103},[45,17309,17310,17313],{"class":47,"line":112},[45,17311,17312],{"class":92},"which",[45,17314,16939],{"class":103},[45,17316,17317,17319],{"class":47,"line":126},[45,17318,17312],{"class":92},[45,17320,16897],{"class":103},[712,17322,17324],{"id":17323},"how-to-use-these-commands","How to use these commands",[195,17326,17327,17334],{},[198,17328,16626,17329,21,17331,17333],{},[2421,17330,16629],{},[18,17332,17299],{}," shows the path to the executable",[198,17335,16626,17336,1316,17338,21,17340,17342],{},[2421,17337,16641],{},[2421,17339,16644],{},[18,17341,17312],{}," shows the path",[14,17344,17345],{},"If the path points to an unexpected location, you may be using a different Python installation than you thought.",[712,17347,17349],{"id":17348},"common-setup-mistakes","Common setup mistakes",[195,17351,17352,17355,17362,17365,17368,17371],{},[198,17353,17354],{},"Installing Python but forgetting to add it to PATH",[198,17356,1350,17357,17359,17360,13095],{},[18,17358,40],{}," when only ",[18,17361,16556],{},[198,17363,17364],{},"Not reopening the terminal after installation",[198,17366,17367],{},"Assuming the newest installed version is the one being used",[198,17369,17370],{},"Using a store or package source that behaves differently from the official installer",[198,17372,17373],{},"Checking the wrong command when Python is already installed",[31,17375,2281],{"id":2280},[712,17377,17379],{"id":17378},"should-i-install-python-2-or-python-3","Should I install Python 2 or Python 3?",[14,17381,17382],{},"Install Python 3. Python 2 is outdated and should not be used for new learning.",[712,17384,17386],{"id":17385},"why-does-python-not-work-but-python3-does","Why does python not work but python3 does?",[14,17388,17116,17389,17391],{},[18,17390,16556],{}," as the command name for Python 3. This is normal on many macOS and Linux setups.",[712,17393,17395],{"id":17394},"what-does-add-python-to-path-mean","What does Add Python to PATH mean?",[14,17397,17398],{},"It lets you run Python from the command line without typing the full installation folder path.",[712,17400,17402],{"id":17401},"do-i-need-an-ide-before-learning-python","Do I need an IDE before learning Python?",[14,17404,17405],{},"No. You can start with the command line and a simple text editor, then move to an IDE later.",[712,17407,17409],{"id":17408},"how-do-i-know-installation-worked","How do I know installation worked?",[14,17411,1082,17412,230,17414,17416],{},[18,17413,17137],{},[18,17415,17143],{},", then open the Python shell and test a simple print statement.",[31,17418,2342],{"id":2341},[195,17420,17421,17426,17431,17435,17440,17445],{},[198,17422,17423],{},[236,17424,17425],{"href":17173},"How to run Python code from the command line and IDEs",[198,17427,17428],{},[236,17429,17430],{"href":17216},"Your first Python program: Hello, world!",[198,17432,17433],{},[236,17434,16446],{"href":17222},[198,17436,17437],{},[236,17438,16451],{"href":17439},"\u002Fhow-to\u002Fhow-to-create-and-use-a-virtual-environment-in-python\u002F",[198,17441,17442],{},[236,17443,15682],{"href":17444},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix\u002F",[198,17446,17447],{},[236,17448,16395],{"href":17449},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix\u002F",[2367,17451,17452],{},"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}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 .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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":41,"searchDepth":63,"depth":63,"links":17454},[17455,17456,17457,17458,17461,17465,17466,17471,17474,17478,17479,17485,17492],{"id":16536,"depth":63,"text":16537},{"id":16564,"depth":63,"text":16565},{"id":16589,"depth":63,"text":16590},{"id":16617,"depth":63,"text":16618,"children":17459},[17460],{"id":16678,"depth":70,"text":16679},{"id":16709,"depth":63,"text":16710,"children":17462},[17463,17464],{"id":16764,"depth":70,"text":16765},{"id":16777,"depth":70,"text":16778},{"id":16797,"depth":63,"text":16798},{"id":16857,"depth":63,"text":16858,"children":17467},[17468,17469,17470],{"id":16877,"depth":70,"text":16878},{"id":16900,"depth":70,"text":16901},{"id":16920,"depth":70,"text":16921},{"id":16961,"depth":63,"text":16962,"children":17472},[17473],{"id":17050,"depth":70,"text":17051},{"id":17094,"depth":63,"text":17095,"children":17475},[17476,17477],{"id":17101,"depth":70,"text":17102},{"id":17128,"depth":70,"text":17129},{"id":17177,"depth":63,"text":17178},{"id":17226,"depth":63,"text":17227,"children":17480},[17481,17482,17483,17484],{"id":17233,"depth":70,"text":17234},{"id":17270,"depth":70,"text":17271},{"id":17323,"depth":70,"text":17324},{"id":17348,"depth":70,"text":17349},{"id":2280,"depth":63,"text":2281,"children":17486},[17487,17488,17489,17490,17491],{"id":17378,"depth":70,"text":17379},{"id":17385,"depth":70,"text":17386},{"id":17394,"depth":70,"text":17395},{"id":17401,"depth":70,"text":17402},{"id":17408,"depth":70,"text":17409},{"id":2341,"depth":63,"text":2342},"Master how to install python on windows macos and linux in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fhow-to-install-python-on-windows-macos-and-linux",{"title":16522,"description":17493},"learn\u002Fhow-to-install-python-on-windows-macos-and-linux","ELUgZmPUe0P6_GOOsQXVQzn30SE3sn9VEvYV2OkT5HM",{"id":17500,"title":17501,"body":17502,"description":19160,"extension":2399,"meta":19161,"navigation":66,"path":17173,"seo":19162,"stem":19163,"__hash__":19164},"content\u002Flearn\u002Fhow-to-run-python-code-command-line-and-ides.md","How to Run Python Code (Command Line and IDEs)",{"type":7,"value":17503,"toc":19102},[17504,17507,17513,17516,17533,17536,17618,17637,17639,17642,17657,17663,17667,17670,17673,17683,17685,17695,17698,17709,17712,17718,17727,17747,17751,17757,17760,17771,17775,17778,17786,17789,17797,17803,17805,17811,17815,17821,17839,17841,17846,17849,17862,17864,17869,17873,17876,17896,17903,17907,17912,17918,17922,17929,17948,17952,17957,17961,17964,17976,17978,17989,17992,18003,18005,18011,18014,18021,18024,18030,18074,18077,18089,18092,18098,18113,18117,18123,18126,18133,18138,18150,18152,18164,18168,18171,18180,18183,18192,18195,18199,18201,18210,18216,18225,18228,18230,18240,18243,18249,18252,18263,18266,18269,18276,18279,18290,18294,18310,18314,18317,18336,18339,18356,18362,18365,18412,18417,18420,18424,18427,18431,18434,18448,18450,18456,18460,18463,18477,18482,18517,18519,18525,18528,18532,18535,18539,18542,18550,18553,18571,18573,18578,18582,18587,18605,18607,18617,18619,18624,18628,18633,18675,18677,18689,18692,18698,18702,18705,18711,18714,18730,18733,18756,18763,18766,18771,18788,18792,18800,18803,18811,18814,18820,18830,18834,18837,18841,18844,18847,18861,18868,18872,18875,18934,18937,18951,18953,18961,18964,18978,18982,18985,18988,18999,19003,19006,19015,19022,19025,19028,19032,19040,19044,19047,19055,19057,19099],[10,17505,17501],{"id":17506},"how-to-run-python-code-command-line-and-ides",[14,17508,17509,17510,240],{},"If you are new to Python, one of the first things to learn is ",[2421,17511,17512],{},"how to actually run your code",[14,17514,17515],{},"This page shows you how to:",[195,17517,17518,17521,17527,17530],{},[198,17519,17520],{},"Run Python in the terminal for quick tests",[198,17522,17523,17524,17526],{},"Run a saved ",[18,17525,15010],{}," file from the command line",[198,17528,17529],{},"Run Python code in common IDEs and editors",[198,17531,17532],{},"Understand when to use interactive mode and when to use script files",[14,17534,17535],{},"If you just want the fastest possible test, try this first:",[36,17537,17539],{"className":38,"code":17538,"language":40,"meta":41,"style":41},"python --version\npython\nprint(\"Hello, world!\")\nexit()\n\n# Save this as hello.py\nprint(\"Hello from a file\")\n\n# Run the file\npython hello.py\n",[18,17540,17541,17552,17556,17570,17576,17580,17585,17600,17604,17609],{"__ignoreMap":41},[45,17542,17543,17546,17549],{"class":47,"line":48},[45,17544,17545],{"class":51},"python ",[45,17547,17548],{"class":13477},"--",[45,17550,17551],{"class":51},"version\n",[45,17553,17554],{"class":47,"line":63},[45,17555,16990],{"class":51},[45,17557,17558,17560,17562,17564,17566,17568],{"class":47,"line":70},[45,17559,2065],{"class":92},[45,17561,96],{"class":85},[45,17563,100],{"class":99},[45,17565,17031],{"class":103},[45,17567,100],{"class":99},[45,17569,109],{"class":85},[45,17571,17572,17574],{"class":47,"line":89},[45,17573,17066],{"class":92},[45,17575,2495],{"class":85},[45,17577,17578],{"class":47,"line":112},[45,17579,67],{"emptyLinePlaceholder":66},[45,17581,17582],{"class":47,"line":126},[45,17583,17584],{"class":7624},"# Save this as hello.py\n",[45,17586,17587,17589,17591,17593,17596,17598],{"class":47,"line":142},[45,17588,2065],{"class":92},[45,17590,96],{"class":85},[45,17592,100],{"class":99},[45,17594,17595],{"class":103},"Hello from a file",[45,17597,100],{"class":99},[45,17599,109],{"class":85},[45,17601,17602],{"class":47,"line":149},[45,17603,67],{"emptyLinePlaceholder":66},[45,17605,17606],{"class":47,"line":679},[45,17607,17608],{"class":7624},"# Run the file\n",[45,17610,17611,17614,17616],{"class":47,"line":686},[45,17612,17613],{"class":51},"python hello",[45,17615,240],{"class":85},[45,17617,15430],{"class":3095},[17619,17620,17621],"blockquote",{},[14,17622,3259,17623,17625,17626,17628,17629,17632,17633,17636],{},[18,17624,40],{}," does not work, try ",[18,17627,16556],{}," instead. On some systems the Python launcher ",[18,17630,17631],{},"py"," also works, such as ",[18,17634,17635],{},"py hello.py"," on Windows.",[31,17638,16565],{"id":16564},[14,17640,17641],{},"After reading this page, you should be able to:",[195,17643,17644,17647,17651,17654],{},[198,17645,17646],{},"Run Python interactively in a terminal",[198,17648,17523,17649,17526],{},[18,17650,15010],{},[198,17652,17653],{},"Run Python code in an IDE or code editor",[198,17655,17656],{},"Understand the difference between interactive mode and script files",[14,17658,17659,17660,240],{},"If you have not installed Python yet, see ",[236,17661,17662],{"href":17495},"how to install Python on Windows, macOS, and Linux",[31,17664,17666],{"id":17665},"before-you-run-python-code","Before you run Python code",[14,17668,17669],{},"First, make sure Python is installed.",[14,17671,17672],{},"Open a terminal or command prompt and try one of these commands:",[36,17674,17675],{"className":15652,"code":16653,"language":15654,"meta":41,"style":41},[18,17676,17677],{"__ignoreMap":41},[45,17678,17679,17681],{"class":47,"line":48},[45,17680,40],{"class":2444},[45,17682,16298],{"class":15663},[14,17684,16664],{},[36,17686,17687],{"className":15652,"code":16667,"language":15654,"meta":41,"style":41},[18,17688,17689],{"__ignoreMap":41},[45,17690,17691,17693],{"class":47,"line":48},[45,17692,16556],{"class":2444},[45,17694,16298],{"class":15663},[14,17696,17697],{},"On Windows, this may also work:",[36,17699,17701],{"className":15652,"code":17700,"language":15654,"meta":41,"style":41},"py --version\n",[18,17702,17703],{"__ignoreMap":41},[45,17704,17705,17707],{"class":47,"line":48},[45,17706,17631],{"class":2444},[45,17708,16298],{"class":15663},[14,17710,17711],{},"You should see a version number, such as:",[36,17713,17716],{"className":17714,"code":17715,"language":5000,"meta":41},[4998],"Python 3.12.2\n",[18,17717,17715],{"__ignoreMap":41},[14,17719,17720,17721,230,17723,17726],{},"If you get a message like ",[2421,17722,16694],{},[2421,17724,17725],{},"python is not recognized",", one of these is probably true:",[195,17728,17729,17732,17738],{},[198,17730,17731],{},"Python is not installed",[198,17733,17734,17735],{},"Python is installed but not added to your system ",[18,17736,17737],{},"PATH",[198,17739,17740,17741,230,17743,1353,17745],{},"You need to use ",[18,17742,16556],{},[18,17744,17631],{},[18,17746,40],{},[31,17748,17750],{"id":17749},"run-python-in-interactive-mode","Run Python in interactive mode",[14,17752,17753,17754,240],{},"Interactive mode lets you type Python code ",[2421,17755,17756],{},"one line at a time",[14,17758,17759],{},"This is useful for:",[195,17761,17762,17765,17768],{},[198,17763,17764],{},"Quick tests",[198,17766,17767],{},"Trying small examples",[198,17769,17770],{},"Checking how a function works",[712,17772,17774],{"id":17773},"start-interactive-mode","Start interactive mode",[14,17776,17777],{},"In your terminal, type:",[36,17779,17780],{"className":15652,"code":16990,"language":15654,"meta":41,"style":41},[18,17781,17782],{"__ignoreMap":41},[45,17783,17784],{"class":47,"line":48},[45,17785,16990],{"class":2444},[14,17787,17788],{},"Or:",[36,17790,17791],{"className":15652,"code":17002,"language":15654,"meta":41,"style":41},[18,17792,17793],{"__ignoreMap":41},[45,17794,17795],{"class":47,"line":48},[45,17796,17002],{"class":2444},[14,17798,17799,17800,17802],{},"If it works, you will see the ",[18,17801,17014],{}," prompt. That means Python is ready.",[14,17804,466],{},[36,17806,17809],{"className":17807,"code":17808,"language":5000,"meta":41},[4998],">>> \n",[18,17810,17808],{"__ignoreMap":41},[712,17812,17814],{"id":17813},"try-a-simple-example","Try a simple example",[14,17816,17817,17818,17820],{},"Type this at the ",[18,17819,17014],{}," prompt:",[36,17822,17823],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,17824,17825],{"__ignoreMap":41},[45,17826,17827,17829,17831,17833,17835,17837],{"class":47,"line":48},[45,17828,2065],{"class":92},[45,17830,96],{"class":85},[45,17832,100],{"class":99},[45,17834,17031],{"class":103},[45,17836,100],{"class":99},[45,17838,109],{"class":85},[14,17840,17038],{},[36,17842,17844],{"className":17843,"code":17042,"language":5000,"meta":41},[4998],[18,17845,17042],{"__ignoreMap":41},[14,17847,17848],{},"You can also try simple math:",[36,17850,17852],{"className":38,"code":17851,"language":40,"meta":41,"style":41},"2 + 3\n",[18,17853,17854],{"__ignoreMap":41},[45,17855,17856,17858,17860],{"class":47,"line":48},[45,17857,9367],{"class":59},[45,17859,3775],{"class":55},[45,17861,1481],{"class":59},[14,17863,17038],{},[36,17865,17867],{"className":17866,"code":11570,"language":5000,"meta":41},[4998],[18,17868,11570],{"__ignoreMap":41},[712,17870,17872],{"id":17871},"exit-interactive-mode","Exit interactive mode",[14,17874,17875],{},"To leave interactive mode:",[195,17877,17878,17885,17891],{},[198,17879,17880,17881,17884],{},"Type ",[18,17882,17883],{},"exit()"," and press Enter",[198,17886,17887,17888,17890],{},"On Windows, press ",[18,17889,17078],{},", then Enter",[198,17892,17893,17894],{},"On macOS\u002FLinux, press ",[18,17895,17091],{},[14,17897,17898,17899,17902],{},"If you are learning basic Python syntax, ",[236,17900,17901],{"href":17216},"your first Python program: Hello World explained"," is a good next step.",[31,17904,17906],{"id":17905},"run-a-python-file-from-the-command-line","Run a Python file from the command line",[14,17908,17909,17910,240],{},"For real programs, you usually save your code in a file ending with ",[18,17911,15010],{},[14,17913,17914,17915,240],{},"This is called a ",[2421,17916,17917],{},"script file",[712,17919,17921],{"id":17920},"step-1-create-a-python-file","Step 1: Create a Python file",[14,17923,17924,17925,17928],{},"Create a file named ",[18,17926,17927],{},"hello.py"," and put this code in it:",[36,17930,17932],{"className":38,"code":17931,"language":40,"meta":41,"style":41},"print(\"Hello from a file\")\n",[18,17933,17934],{"__ignoreMap":41},[45,17935,17936,17938,17940,17942,17944,17946],{"class":47,"line":48},[45,17937,2065],{"class":92},[45,17939,96],{"class":85},[45,17941,100],{"class":99},[45,17943,17595],{"class":103},[45,17945,100],{"class":99},[45,17947,109],{"class":85},[712,17949,17951],{"id":17950},"step-2-open-the-terminal-in-the-same-folder","Step 2: Open the terminal in the same folder",[14,17953,17954,17955,240],{},"Your terminal needs to be in the folder that contains ",[18,17956,17927],{},[712,17958,17960],{"id":17959},"step-3-run-the-file","Step 3: Run the file",[14,17962,17963],{},"Use one of these commands:",[36,17965,17967],{"className":15652,"code":17966,"language":15654,"meta":41,"style":41},"python hello.py\n",[18,17968,17969],{"__ignoreMap":41},[45,17970,17971,17973],{"class":47,"line":48},[45,17972,40],{"class":2444},[45,17974,17975],{"class":103}," hello.py\n",[14,17977,17788],{},[36,17979,17981],{"className":15652,"code":17980,"language":15654,"meta":41,"style":41},"python3 hello.py\n",[18,17982,17983],{"__ignoreMap":41},[45,17984,17985,17987],{"class":47,"line":48},[45,17986,16556],{"class":2444},[45,17988,17975],{"class":103},[14,17990,17991],{},"Or on Windows:",[36,17993,17995],{"className":15652,"code":17994,"language":15654,"meta":41,"style":41},"py hello.py\n",[18,17996,17997],{"__ignoreMap":41},[45,17998,17999,18001],{"class":47,"line":48},[45,18000,17631],{"class":2444},[45,18002,17975],{"class":103},[14,18004,17038],{},[36,18006,18009],{"className":18007,"code":18008,"language":5000,"meta":41},[4998],"Hello from a file\n",[18,18010,18008],{"__ignoreMap":41},[14,18012,18013],{},"This is the normal way to run saved Python programs.",[712,18015,18017,18018],{"id":18016},"example-with-input","Example with ",[18,18019,18020],{},"input()",[14,18022,18023],{},"Scripts can also pause and wait for user input.",[14,18025,18026,18027,457],{},"Save this as ",[18,18028,18029],{},"name.py",[36,18031,18033],{"className":38,"code":18032,"language":40,"meta":41,"style":41},"name = input(\"What is your name? \")\nprint(\"Hello,\", name)\n",[18,18034,18035,18055],{"__ignoreMap":41},[45,18036,18037,18039,18041,18044,18046,18048,18051,18053],{"class":47,"line":48},[45,18038,1560],{"class":51},[45,18040,56],{"class":55},[45,18042,18043],{"class":92}," input",[45,18045,96],{"class":85},[45,18047,100],{"class":99},[45,18049,18050],{"class":103},"What is your name? ",[45,18052,100],{"class":99},[45,18054,109],{"class":85},[45,18056,18057,18059,18061,18063,18066,18068,18070,18072],{"class":47,"line":63},[45,18058,2065],{"class":92},[45,18060,96],{"class":85},[45,18062,100],{"class":99},[45,18064,18065],{"class":103},"Hello,",[45,18067,100],{"class":99},[45,18069,3079],{"class":85},[45,18071,3083],{"class":2070},[45,18073,109],{"class":85},[14,18075,18076],{},"Run it:",[36,18078,18080],{"className":15652,"code":18079,"language":15654,"meta":41,"style":41},"python name.py\n",[18,18081,18082],{"__ignoreMap":41},[45,18083,18084,18086],{"class":47,"line":48},[45,18085,40],{"class":2444},[45,18087,18088],{"class":103}," name.py\n",[14,18090,18091],{},"Example session:",[36,18093,18096],{"className":18094,"code":18095,"language":5000,"meta":41},[4998],"What is your name? Sam\nHello, Sam\n",[18,18097,18095],{"__ignoreMap":41},[14,18099,18100,18101,1316,18108,240],{},"If you want to understand this line better, see ",[236,18102,18104,18105,18107],{"href":18103},"\u002Freference\u002Fpython-input-function-explained\u002F","Python ",[18,18106,18020],{}," explained",[236,18109,18104,18111,18107],{"href":18110},"\u002Freference\u002Fpython-print-function-explained\u002F",[18,18112,15833],{},[31,18114,18116],{"id":18115},"how-to-open-the-correct-folder-in-the-terminal","How to open the correct folder in the terminal",[14,18118,18119,18120,240],{},"A very common beginner problem is being in the ",[2421,18121,18122],{},"wrong folder",[14,18124,18125],{},"If Python says it cannot open your file, check where your terminal is currently located.",[712,18127,18129,18130],{"id":18128},"move-to-a-folder-with-cd","Move to a folder with ",[18,18131,18132],{},"cd",[14,18134,165,18135,18137],{},[18,18136,18132],{}," to change folders:",[36,18139,18141],{"className":15652,"code":18140,"language":15654,"meta":41,"style":41},"cd path\u002Fto\u002Ffolder\n",[18,18142,18143],{"__ignoreMap":41},[45,18144,18145,18147],{"class":47,"line":48},[45,18146,18132],{"class":92},[45,18148,18149],{"class":103}," path\u002Fto\u002Ffolder\n",[14,18151,466],{},[36,18153,18155],{"className":15652,"code":18154,"language":15654,"meta":41,"style":41},"cd Desktop\u002Fpython-practice\n",[18,18156,18157],{"__ignoreMap":41},[45,18158,18159,18161],{"class":47,"line":48},[45,18160,18132],{"class":92},[45,18162,18163],{"class":103}," Desktop\u002Fpython-practice\n",[712,18165,18167],{"id":18166},"check-the-files-in-the-current-folder","Check the files in the current folder",[14,18169,18170],{},"On Windows:",[36,18172,18174],{"className":15652,"code":18173,"language":15654,"meta":41,"style":41},"dir\n",[18,18175,18176],{"__ignoreMap":41},[45,18177,18178],{"class":47,"line":48},[45,18179,18173],{"class":2444},[14,18181,18182],{},"On macOS\u002FLinux:",[36,18184,18186],{"className":15652,"code":18185,"language":15654,"meta":41,"style":41},"ls\n",[18,18187,18188],{"__ignoreMap":41},[45,18189,18190],{"class":47,"line":48},[45,18191,18185],{"class":2444},[14,18193,18194],{},"You should see your Python file listed.",[712,18196,18198],{"id":18197},"check-your-current-location","Check your current location",[14,18200,18182],{},[36,18202,18204],{"className":15652,"code":18203,"language":15654,"meta":41,"style":41},"pwd\n",[18,18205,18206],{"__ignoreMap":41},[45,18207,18208],{"class":47,"line":48},[45,18209,18203],{"class":92},[14,18211,18212,18213,18215],{},"On Windows, ",[18,18214,18132],{}," by itself often shows the current folder:",[36,18217,18219],{"className":15652,"code":18218,"language":15654,"meta":41,"style":41},"cd\n",[18,18220,18221],{"__ignoreMap":41},[45,18222,18223],{"class":47,"line":48},[45,18224,18218],{"class":92},[14,18226,18227],{},"If your file is not in that folder, Python will not find it.",[14,18229,16001],{},[36,18231,18232],{"className":15652,"code":17966,"language":15654,"meta":41,"style":41},[18,18233,18234],{"__ignoreMap":41},[45,18235,18236,18238],{"class":47,"line":48},[45,18237,40],{"class":2444},[45,18239,17975],{"class":103},[14,18241,18242],{},"Possible error:",[36,18244,18247],{"className":18245,"code":18246,"language":5000,"meta":41},[4998],"python: can't open file 'hello.py': [Errno 2] No such file or directory\n",[18,18248,18246],{"__ignoreMap":41},[14,18250,18251],{},"This usually means:",[195,18253,18254,18257,18260],{},[198,18255,18256],{},"You are in the wrong folder",[198,18258,18259],{},"The file name is wrong",[198,18261,18262],{},"The file was not saved yet",[31,18264,17653],{"id":18265},"run-python-code-in-an-ide-or-code-editor",[14,18267,18268],{},"You do not have to use the terminal every time.",[14,18270,18271,18272,18275],{},"Most IDEs and code editors can run Python files with a ",[2421,18273,18274],{},"Run"," button.",[14,18277,18278],{},"Common beginner tools include:",[195,18280,18281,18284,18287],{},[198,18282,18283],{},"IDLE",[198,18285,18286],{},"VS Code",[198,18288,18289],{},"PyCharm",[712,18291,18293],{"id":18292},"the-basic-process-is-usually-the-same","The basic process is usually the same",[346,18295,18296,18299,18302,18305],{},[198,18297,18298],{},"Open your Python file",[198,18300,18301],{},"Make sure the file is saved",[198,18303,18304],{},"Make sure the editor is using a valid Python interpreter",[198,18306,16735,18307,18309],{},[2421,18308,18274],{}," or use the editor's run shortcut",[712,18311,18313],{"id":18312},"important-checks","Important checks",[14,18315,18316],{},"If the Run button does nothing, check these things:",[195,18318,18319,18322,18327,18330,18333],{},[198,18320,18321],{},"The file is saved",[198,18323,18324,18325],{},"The file name ends with ",[18,18326,15010],{},[198,18328,18329],{},"Python is installed",[198,18331,18332],{},"The editor is using the correct Python interpreter",[198,18334,18335],{},"Your code does not contain a syntax error",[14,18337,18338],{},"For example, this code will fail because it is missing a closing parenthesis:",[36,18340,18342],{"className":38,"code":18341,"language":40,"meta":41,"style":41},"print(\"Hello\"\n",[18,18343,18344],{"__ignoreMap":41},[45,18345,18346,18348,18350,18352,18354],{"class":47,"line":48},[45,18347,2065],{"class":92},[45,18349,96],{"class":85},[45,18351,100],{"class":99},[45,18353,3226],{"class":103},[45,18355,1571],{"class":99},[14,18357,7708,18358,240],{},[236,18359,18361],{"href":18360},"\u002Ferrors\u002Fsyntaxerror-invalid-syntax-fix\u002F","SyntaxError: invalid syntax",[14,18363,18364],{},"This code can also fail because of indentation problems:",[36,18366,18368],{"className":38,"code":18367,"language":40,"meta":41,"style":41},"name = \"Sam\"\nif name == \"Sam\":\nprint(\"Hi\")\n",[18,18369,18370,18382,18398],{"__ignoreMap":41},[45,18371,18372,18374,18376,18378,18380],{"class":47,"line":48},[45,18373,1560],{"class":51},[45,18375,56],{"class":55},[45,18377,1565],{"class":99},[45,18379,1568],{"class":103},[45,18381,1571],{"class":99},[45,18383,18384,18386,18388,18390,18392,18394,18396],{"class":47,"line":63},[45,18385,20],{"class":73},[45,18387,1582],{"class":51},[45,18389,1129],{"class":55},[45,18391,1565],{"class":99},[45,18393,1568],{"class":103},[45,18395,100],{"class":99},[45,18397,86],{"class":85},[45,18399,18400,18402,18404,18406,18408,18410],{"class":47,"line":70},[45,18401,2065],{"class":92},[45,18403,96],{"class":85},[45,18405,100],{"class":99},[45,18407,6510],{"class":103},[45,18409,100],{"class":99},[45,18411,109],{"class":85},[14,18413,18414,18415,240],{},"That can cause an ",[236,18416,1614],{"href":1613},[14,18418,18419],{},"If your code runs in the terminal but not in your editor, the editor may be using a different Python installation.",[31,18421,18423],{"id":18422},"interactive-mode-vs-running-a-script","Interactive mode vs running a script",[14,18425,18426],{},"These two ways of running Python are similar, but they are used for different purposes.",[712,18428,18430],{"id":18429},"interactive-mode","Interactive mode",[14,18432,18433],{},"Interactive mode:",[195,18435,18436,18439,18442,18445],{},[198,18437,18438],{},"Runs one command at a time",[198,18440,18441],{},"Shows results immediately",[198,18443,18444],{},"Is good for quick testing",[198,18446,18447],{},"Does not automatically save your code",[14,18449,466],{},[36,18451,18454],{"className":18452,"code":18453,"language":5000,"meta":41},[4998],">>> print(\"test\")\ntest\n>>> 10 * 2\n20\n",[18,18455,18453],{"__ignoreMap":41},[712,18457,18459],{"id":18458},"script-file","Script file",[14,18461,18462],{},"A script file:",[195,18464,18465,18468,18471,18474],{},[198,18466,18467],{},"Runs all saved code from top to bottom",[198,18469,18470],{},"Can be saved and used again later",[198,18472,18473],{},"Is better for larger programs",[198,18475,18476],{},"Is better for repeatable work",[14,18478,18479,18480,457],{},"Example ",[18,18481,17927],{},[36,18483,18485],{"className":38,"code":18484,"language":40,"meta":41,"style":41},"print(\"Hello from a script\")\nprint(\"This runs from top to bottom\")\n",[18,18486,18487,18502],{"__ignoreMap":41},[45,18488,18489,18491,18493,18495,18498,18500],{"class":47,"line":48},[45,18490,2065],{"class":92},[45,18492,96],{"class":85},[45,18494,100],{"class":99},[45,18496,18497],{"class":103},"Hello from a script",[45,18499,100],{"class":99},[45,18501,109],{"class":85},[45,18503,18504,18506,18508,18510,18513,18515],{"class":47,"line":63},[45,18505,2065],{"class":92},[45,18507,96],{"class":85},[45,18509,100],{"class":99},[45,18511,18512],{"class":103},"This runs from top to bottom",[45,18514,100],{"class":99},[45,18516,109],{"class":85},[14,18518,17038],{},[36,18520,18523],{"className":18521,"code":18522,"language":5000,"meta":41},[4998],"Hello from a script\nThis runs from top to bottom\n",[18,18524,18522],{"__ignoreMap":41},[14,18526,18527],{},"For beginners, interactive mode is great for experiments, but script files are better for real practice.",[31,18529,18531],{"id":18530},"expected-beginner-examples-to-include","Expected beginner examples to include",[14,18533,18534],{},"Here are three simple examples you can try right away.",[712,18536,18538],{"id":18537},"example-1-interactive-mode","Example 1: Interactive mode",[14,18540,18541],{},"Start Python:",[36,18543,18544],{"className":15652,"code":16990,"language":15654,"meta":41,"style":41},[18,18545,18546],{"__ignoreMap":41},[45,18547,18548],{"class":47,"line":48},[45,18549,16990],{"class":2444},[14,18551,18552],{},"Then type:",[36,18554,18555],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,18556,18557],{"__ignoreMap":41},[45,18558,18559,18561,18563,18565,18567,18569],{"class":47,"line":48},[45,18560,2065],{"class":92},[45,18562,96],{"class":85},[45,18564,100],{"class":99},[45,18566,17031],{"class":103},[45,18568,100],{"class":99},[45,18570,109],{"class":85},[14,18572,17038],{},[36,18574,18576],{"className":18575,"code":17042,"language":5000,"meta":41},[4998],[18,18577,17042],{"__ignoreMap":41},[712,18579,18581],{"id":18580},"example-2-run-a-saved-file","Example 2: Run a saved file",[14,18583,18584,18585,457],{},"Save this in ",[18,18586,17927],{},[36,18588,18589],{"className":38,"code":17931,"language":40,"meta":41,"style":41},[18,18590,18591],{"__ignoreMap":41},[45,18592,18593,18595,18597,18599,18601,18603],{"class":47,"line":48},[45,18594,2065],{"class":92},[45,18596,96],{"class":85},[45,18598,100],{"class":99},[45,18600,17595],{"class":103},[45,18602,100],{"class":99},[45,18604,109],{"class":85},[14,18606,18076],{},[36,18608,18609],{"className":15652,"code":17966,"language":15654,"meta":41,"style":41},[18,18610,18611],{"__ignoreMap":41},[45,18612,18613,18615],{"class":47,"line":48},[45,18614,40],{"class":2444},[45,18616,17975],{"class":103},[14,18618,17038],{},[36,18620,18622],{"className":18621,"code":18008,"language":5000,"meta":41},[4998],[18,18623,18008],{"__ignoreMap":41},[712,18625,18627],{"id":18626},"example-3-script-with-user-input","Example 3: Script with user input",[14,18629,18584,18630,457],{},[18,18631,18632],{},"ask_name.py",[36,18634,18636],{"className":38,"code":18635,"language":40,"meta":41,"style":41},"name = input(\"What is your name? \")\nprint(\"Nice to meet you,\", name)\n",[18,18637,18638,18656],{"__ignoreMap":41},[45,18639,18640,18642,18644,18646,18648,18650,18652,18654],{"class":47,"line":48},[45,18641,1560],{"class":51},[45,18643,56],{"class":55},[45,18645,18043],{"class":92},[45,18647,96],{"class":85},[45,18649,100],{"class":99},[45,18651,18050],{"class":103},[45,18653,100],{"class":99},[45,18655,109],{"class":85},[45,18657,18658,18660,18662,18664,18667,18669,18671,18673],{"class":47,"line":63},[45,18659,2065],{"class":92},[45,18661,96],{"class":85},[45,18663,100],{"class":99},[45,18665,18666],{"class":103},"Nice to meet you,",[45,18668,100],{"class":99},[45,18670,3079],{"class":85},[45,18672,3083],{"class":2070},[45,18674,109],{"class":85},[14,18676,18076],{},[36,18678,18680],{"className":15652,"code":18679,"language":15654,"meta":41,"style":41},"python ask_name.py\n",[18,18681,18682],{"__ignoreMap":41},[45,18683,18684,18686],{"class":47,"line":48},[45,18685,40],{"class":2444},[45,18687,18688],{"class":103}," ask_name.py\n",[14,18690,18691],{},"Example output:",[36,18693,18696],{"className":18694,"code":18695,"language":5000,"meta":41},[4998],"What is your name? Maya\nNice to meet you, Maya\n",[18,18697,18695],{"__ignoreMap":41},[31,18699,18701],{"id":18700},"common-problems-and-quick-checks","Common problems and quick checks",[14,18703,18704],{},"If Python is not running correctly, check these common problems first.",[712,18706,18708,18710],{"id":18707},"python-is-not-recognized-or-command-not-found",[18,18709,40],{}," is not recognized or command not found",[14,18712,18713],{},"Possible causes:",[195,18715,18716,18718,18723],{},[198,18717,17731],{},[198,18719,18720,18721],{},"Python is not in ",[18,18722,17737],{},[198,18724,18725,18726,230,18728,12224],{},"Your system uses ",[18,18727,16556],{},[18,18729,17631],{},[14,18731,18732],{},"Try:",[36,18734,18736],{"className":15652,"code":18735,"language":15654,"meta":41,"style":41},"python --version\npython3 --version\npy --version\n",[18,18737,18738,18744,18750],{"__ignoreMap":41},[45,18739,18740,18742],{"class":47,"line":48},[45,18741,40],{"class":2444},[45,18743,16298],{"class":15663},[45,18745,18746,18748],{"class":47,"line":63},[45,18747,16556],{"class":2444},[45,18749,16298],{"class":15663},[45,18751,18752,18754],{"class":47,"line":70},[45,18753,17631],{"class":2444},[45,18755,16298],{"class":15663},[712,18757,18759,18760,18762],{"id":18758},"wrong-file-name-or-missing-py-extension","Wrong file name or missing ",[18,18761,15010],{}," extension",[14,18764,18765],{},"Make sure the file name is exactly correct.",[14,18767,15394,18768,18770],{},[18,18769,17927],{}," is not the same as:",[195,18772,18773,18778,18783],{},[198,18774,18775],{},[18,18776,18777],{},"Hello.py",[198,18779,18780],{},[18,18781,18782],{},"hello",[198,18784,18785],{},[18,18786,18787],{},"hello.py.txt",[712,18789,18791],{"id":18790},"running-from-the-wrong-folder","Running from the wrong folder",[14,18793,165,18794,230,18797,18799],{},[18,18795,18796],{},"ls",[18,18798,4357],{}," to make sure your file is in the current folder.",[14,18801,18802],{},"Then run it again.",[712,18804,1350,18806,18808,18809],{"id":18805},"using-python-when-the-system-needs-python3",[18,18807,40],{}," when the system needs ",[18,18810,16556],{},[14,18812,18813],{},"Some systems separate Python 2 and Python 3 commands.",[14,18815,3259,18816,18819],{},[18,18817,18818],{},"python hello.py"," fails, try:",[36,18821,18822],{"className":15652,"code":17980,"language":15654,"meta":41,"style":41},[18,18823,18824],{"__ignoreMap":41},[45,18825,18826,18828],{"class":47,"line":48},[45,18827,16556],{"class":2444},[45,18829,17975],{"class":103},[712,18831,18833],{"id":18832},"interpreter-not-selected-in-the-ide","Interpreter not selected in the IDE",[14,18835,18836],{},"If the terminal works but your IDE does not, check the selected interpreter in the editor settings.",[712,18838,18840],{"id":18839},"syntax-errors-stop-the-script","Syntax errors stop the script",[14,18842,18843],{},"If your script has invalid code, it will stop before finishing.",[14,18845,18846],{},"Common examples include:",[195,18848,18849,18854,18858],{},[198,18850,18851,18852],{},"Missing ",[18,18853,457],{},[198,18855,18851,18856],{},[18,18857,9146],{},[198,18859,18860],{},"Wrong indentation",[14,18862,18863,18864,240],{},"If you are stuck, see this ",[236,18865,18867],{"href":18866},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide\u002F","beginner guide to debugging Python code",[31,18869,18871],{"id":18870},"beginner-debugging-commands","Beginner debugging commands",[14,18873,18874],{},"These commands help you quickly check what is wrong.",[36,18876,18878],{"className":15652,"code":18877,"language":15654,"meta":41,"style":41},"python --version\npython3 --version\npy --version\npwd\ncd path\u002Fto\u002Ffolder\nls\ndir\npython hello.py\npython3 hello.py\npy hello.py\n",[18,18879,18880,18886,18892,18898,18902,18908,18912,18916,18922,18928],{"__ignoreMap":41},[45,18881,18882,18884],{"class":47,"line":48},[45,18883,40],{"class":2444},[45,18885,16298],{"class":15663},[45,18887,18888,18890],{"class":47,"line":63},[45,18889,16556],{"class":2444},[45,18891,16298],{"class":15663},[45,18893,18894,18896],{"class":47,"line":70},[45,18895,17631],{"class":2444},[45,18897,16298],{"class":15663},[45,18899,18900],{"class":47,"line":89},[45,18901,18203],{"class":92},[45,18903,18904,18906],{"class":47,"line":112},[45,18905,18132],{"class":92},[45,18907,18149],{"class":103},[45,18909,18910],{"class":47,"line":126},[45,18911,18185],{"class":2444},[45,18913,18914],{"class":47,"line":142},[45,18915,18173],{"class":2444},[45,18917,18918,18920],{"class":47,"line":149},[45,18919,40],{"class":2444},[45,18921,17975],{"class":103},[45,18923,18924,18926],{"class":47,"line":679},[45,18925,16556],{"class":2444},[45,18927,17975],{"class":103},[45,18929,18930,18932],{"class":47,"line":686},[45,18931,17631],{"class":2444},[45,18933,17975],{"class":103},[14,18935,18936],{},"These commands can help you answer basic questions:",[195,18938,18939,18942,18945,18948],{},[198,18940,18941],{},"Is Python installed?",[198,18943,18944],{},"Which command works on this computer?",[198,18946,18947],{},"Am I in the correct folder?",[198,18949,18950],{},"Can Python find my file?",[31,18952,2281],{"id":2280},[712,18954,18956,18957,230,18959,354],{"id":18955},"should-i-use-python-or-python3","Should I use ",[18,18958,40],{},[18,18960,16556],{},[14,18962,18963],{},"Use the command that works on your system.",[195,18965,18966,18973],{},[198,18967,18968,18969,230,18971],{},"Many Windows systems use ",[18,18970,40],{},[18,18972,17631],{},[198,18974,18975,18976],{},"Many macOS and Linux systems use ",[18,18977,16556],{},[712,18979,18981],{"id":18980},"what-is-the-difference-between-the-terminal-and-an-ide","What is the difference between the terminal and an IDE?",[14,18983,18984],{},"The terminal runs commands directly.",[14,18986,18987],{},"An IDE gives you:",[195,18989,18990,18993,18996],{},[198,18991,18992],{},"A code editor",[198,18994,18995],{},"A Run button",[198,18997,18998],{},"Built-in tools for writing and debugging code",[712,19000,19002],{"id":19001},"why-does-python-say-it-cannot-open-my-file","Why does Python say it cannot open my file?",[14,19004,19005],{},"Usually one of these is the cause:",[195,19007,19008,19010,19012],{},[198,19009,18256],{},[198,19011,18259],{},[198,19013,19014],{},"The file was not saved",[712,19016,19018,19019,19021],{"id":19017},"what-does-mean-in-python","What does ",[18,19020,17014],{}," mean in Python?",[14,19023,19024],{},"It is the prompt for Python interactive mode.",[14,19026,19027],{},"It means Python is ready for you to type code.",[712,19029,19031],{"id":19030},"can-i-run-python-without-saving-a-file","Can I run Python without saving a file?",[14,19033,19034,19035,230,19037,19039],{},"Yes. Use interactive mode by typing ",[18,19036,40],{},[18,19038,16556],{}," in the terminal.",[712,19041,19043],{"id":19042},"why-does-my-code-work-in-the-terminal-but-not-in-my-ide","Why does my code work in the terminal but not in my IDE?",[14,19045,19046],{},"Usually:",[195,19048,19049,19052],{},[198,19050,19051],{},"The IDE is using a different Python interpreter",[198,19053,19054],{},"The file was not saved before running it",[31,19056,2342],{"id":2341},[195,19058,19059,19064,19069,19076,19082,19086,19090,19095],{},[198,19060,19061],{},[236,19062,19063],{"href":17495},"How to install Python on Windows, macOS, and Linux",[198,19065,19066],{},[236,19067,19068],{"href":17216},"Your first Python program: Hello World explained",[198,19070,19071],{},[236,19072,18104,19073,19075],{"href":18110},[18,19074,15833],{}," function explained",[198,19077,19078],{},[236,19079,18104,19080,19075],{"href":18103},[18,19081,18020],{},[198,19083,19084],{},[236,19085,18361],{"href":18360},[198,19087,19088],{},[236,19089,1614],{"href":1613},[198,19091,19092],{},[236,19093,19094],{"href":18866},"How to debug Python code: beginner guide",[198,19096,19097],{},[236,19098,5898],{"href":5897},[2367,19100,19101],{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srjyR, html code.shiki .srjyR{--shiki-light:#90A4AE;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 .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}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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--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}",{"title":41,"searchDepth":63,"depth":63,"links":19103},[19104,19105,19106,19111,19118,19124,19128,19132,19137,19148,19149,19159],{"id":16564,"depth":63,"text":16565},{"id":17665,"depth":63,"text":17666},{"id":17749,"depth":63,"text":17750,"children":19107},[19108,19109,19110],{"id":17773,"depth":70,"text":17774},{"id":17813,"depth":70,"text":17814},{"id":17871,"depth":70,"text":17872},{"id":17905,"depth":63,"text":17906,"children":19112},[19113,19114,19115,19116],{"id":17920,"depth":70,"text":17921},{"id":17950,"depth":70,"text":17951},{"id":17959,"depth":70,"text":17960},{"id":18016,"depth":70,"text":19117},"Example with input()",{"id":18115,"depth":63,"text":18116,"children":19119},[19120,19122,19123],{"id":18128,"depth":70,"text":19121},"Move to a folder with cd",{"id":18166,"depth":70,"text":18167},{"id":18197,"depth":70,"text":18198},{"id":18265,"depth":63,"text":17653,"children":19125},[19126,19127],{"id":18292,"depth":70,"text":18293},{"id":18312,"depth":70,"text":18313},{"id":18422,"depth":63,"text":18423,"children":19129},[19130,19131],{"id":18429,"depth":70,"text":18430},{"id":18458,"depth":70,"text":18459},{"id":18530,"depth":63,"text":18531,"children":19133},[19134,19135,19136],{"id":18537,"depth":70,"text":18538},{"id":18580,"depth":70,"text":18581},{"id":18626,"depth":70,"text":18627},{"id":18700,"depth":63,"text":18701,"children":19138},[19139,19141,19143,19144,19146,19147],{"id":18707,"depth":70,"text":19140},"python is not recognized or command not found",{"id":18758,"depth":70,"text":19142},"Wrong file name or missing .py extension",{"id":18790,"depth":70,"text":18791},{"id":18805,"depth":70,"text":19145},"Using python when the system needs python3",{"id":18832,"depth":70,"text":18833},{"id":18839,"depth":70,"text":18840},{"id":18870,"depth":63,"text":18871},{"id":2280,"depth":63,"text":2281,"children":19150},[19151,19153,19154,19155,19157,19158],{"id":18955,"depth":70,"text":19152},"Should I use python or python3?",{"id":18980,"depth":70,"text":18981},{"id":19001,"depth":70,"text":19002},{"id":19017,"depth":70,"text":19156},"What does >>> mean in Python?",{"id":19030,"depth":70,"text":19031},{"id":19042,"depth":70,"text":19043},{"id":2341,"depth":63,"text":2342},"Master how to run python code command line and ides in our comprehensive Python beginner guide.",{},{"title":17501,"description":19160},"learn\u002Fhow-to-run-python-code-command-line-and-ides","xNtiJ4fMvdKnUWiRmdCW9_q63hEf9kcc66mczXtaah4",{"id":19166,"title":19167,"body":19168,"description":22080,"extension":2399,"meta":22081,"navigation":66,"path":4312,"seo":22082,"stem":22083,"__hash__":22084},"content\u002Flearn\u002Finstance-vs-class-variables-in-python.md","Instance vs Class Variables in Python",{"type":7,"value":19169,"toc":22058},[19170,19173,19183,19199,19202,19216,19218,19393,19397,19421,19429,19431,19445,19449,19455,19470,19472,19617,19621,19634,19636,19651,19654,19708,19712,19726,19730,19735,19738,19740,19887,19891,19908,19910,19926,19929,19940,19950,19954,19957,19971,19974,20003,20005,20230,20234,20241,20244,20252,20255,20257,20321,20325,20334,20345,20348,20434,20438,20452,20455,20474,20481,20485,20488,20491,20502,20504,20685,20688,20692,20695,20697,20711,20713,20873,20879,20883,20886,20897,20900,20903,21066,21070,21091,21094,21097,21251,21255,21272,21279,21283,21286,21345,21348,21464,21468,21485,21488,21506,21509,21620,21624,21641,21645,21659,21661,21664,21688,21692,21695,21781,21784,21815,21817,21942,21946,21975,21978,21990,21992,21996,21999,22003,22006,22010,22013,22017,22020,22022,22055],[10,19171,19167],{"id":19172},"instance-vs-class-variables-in-python",[14,19174,19175,19176,1316,19179,19182],{},"In Python classes, ",[2421,19177,19178],{},"instance variables",[2421,19180,19181],{},"class variables"," are used for different kinds of data.",[195,19184,19185,19192],{},[198,19186,19187,19188,19191],{},"Use an ",[2421,19189,19190],{},"instance variable"," for data that belongs to one object",[198,19193,19194,19195,19198],{},"Use a ",[2421,19196,19197],{},"class variable"," for data shared by all objects of the class",[14,19200,19201],{},"This page explains:",[195,19203,19204,19207,19210,19213],{},[198,19205,19206],{},"what each one means",[198,19208,19209],{},"when each one is created",[198,19211,19212],{},"how Python finds them",[198,19214,19215],{},"common beginner mistakes",[31,19217,34],{"id":33},[36,19219,19221],{"className":38,"code":19220,"language":40,"meta":41,"style":41},"class Dog:\n    species = \"Canis familiaris\"  # class variable\n\n    def __init__(self, name):\n        self.name = name            # instance variable\n\n\na = Dog(\"Max\")\nb = Dog(\"Bella\")\n\nprint(a.species)\nprint(b.species)\nprint(a.name)\nprint(b.name)\n",[18,19222,19223,19231,19248,19252,19268,19284,19288,19292,19311,19331,19335,19350,19364,19378],{"__ignoreMap":41},[45,19224,19225,19227,19229],{"class":47,"line":48},[45,19226,2441],{"class":2440},[45,19228,2445],{"class":2444},[45,19230,86],{"class":85},[45,19232,19233,19236,19238,19240,19243,19245],{"class":47,"line":63},[45,19234,19235],{"class":51},"    species ",[45,19237,56],{"class":55},[45,19239,1565],{"class":99},[45,19241,19242],{"class":103},"Canis familiaris",[45,19244,100],{"class":99},[45,19246,19247],{"class":7624},"  # class variable\n",[45,19249,19250],{"class":47,"line":70},[45,19251,67],{"emptyLinePlaceholder":66},[45,19253,19254,19256,19258,19260,19262,19264,19266],{"class":47,"line":89},[45,19255,2452],{"class":2440},[45,19257,3072],{"class":92},[45,19259,96],{"class":85},[45,19261,2427],{"class":2461},[45,19263,3079],{"class":85},[45,19265,3083],{"class":3082},[45,19267,2464],{"class":85},[45,19269,19270,19272,19274,19276,19278,19281],{"class":47,"line":112},[45,19271,3090],{"class":2771},[45,19273,240],{"class":85},[45,19275,3096],{"class":3095},[45,19277,3099],{"class":55},[45,19279,19280],{"class":51}," name            ",[45,19282,19283],{"class":7624},"# instance variable\n",[45,19285,19286],{"class":47,"line":126},[45,19287,67],{"emptyLinePlaceholder":66},[45,19289,19290],{"class":47,"line":142},[45,19291,67],{"emptyLinePlaceholder":66},[45,19293,19294,19296,19298,19300,19302,19304,19307,19309],{"class":47,"line":149},[45,19295,11541],{"class":51},[45,19297,56],{"class":55},[45,19299,2445],{"class":2070},[45,19301,96],{"class":85},[45,19303,100],{"class":99},[45,19305,19306],{"class":103},"Max",[45,19308,100],{"class":99},[45,19310,109],{"class":85},[45,19312,19313,19316,19318,19320,19322,19324,19327,19329],{"class":47,"line":679},[45,19314,19315],{"class":51},"b ",[45,19317,56],{"class":55},[45,19319,2445],{"class":2070},[45,19321,96],{"class":85},[45,19323,100],{"class":99},[45,19325,19326],{"class":103},"Bella",[45,19328,100],{"class":99},[45,19330,109],{"class":85},[45,19332,19333],{"class":47,"line":686},[45,19334,67],{"emptyLinePlaceholder":66},[45,19336,19337,19339,19341,19343,19345,19348],{"class":47,"line":2229},[45,19338,2065],{"class":92},[45,19340,96],{"class":85},[45,19342,236],{"class":2070},[45,19344,240],{"class":85},[45,19346,19347],{"class":3095},"species",[45,19349,109],{"class":85},[45,19351,19352,19354,19356,19358,19360,19362],{"class":47,"line":2236},[45,19353,2065],{"class":92},[45,19355,96],{"class":85},[45,19357,10384],{"class":2070},[45,19359,240],{"class":85},[45,19361,19347],{"class":3095},[45,19363,109],{"class":85},[45,19365,19366,19368,19370,19372,19374,19376],{"class":47,"line":5728},[45,19367,2065],{"class":92},[45,19369,96],{"class":85},[45,19371,236],{"class":2070},[45,19373,240],{"class":85},[45,19375,3096],{"class":3095},[45,19377,109],{"class":85},[45,19379,19381,19383,19385,19387,19389,19391],{"class":47,"line":19380},14,[45,19382,2065],{"class":92},[45,19384,96],{"class":85},[45,19386,10384],{"class":2070},[45,19388,240],{"class":85},[45,19390,3096],{"class":3095},[45,19392,109],{"class":85},[14,19394,19395],{},[2421,19396,177],{},[36,19398,19400],{"className":38,"code":19399,"language":40,"meta":41,"style":41},"Canis familiaris\nCanis familiaris\nMax\nBella\n",[18,19401,19402,19407,19411,19416],{"__ignoreMap":41},[45,19403,19404],{"class":47,"line":48},[45,19405,19406],{"class":51},"Canis familiaris\n",[45,19408,19409],{"class":47,"line":63},[45,19410,19406],{"class":51},[45,19412,19413],{"class":47,"line":70},[45,19414,19415],{"class":51},"Max\n",[45,19417,19418],{"class":47,"line":89},[45,19419,19420],{"class":51},"Bella\n",[14,19422,19194,19423,19425,19426,19428],{},[2421,19424,19197],{}," for shared data. Use an ",[2421,19427,19190],{}," for data that should be different for each object.",[31,19430,6600],{"id":6599},[195,19432,19433,19436,19439,19442],{},[198,19434,19435],{},"What instance variables are",[198,19437,19438],{},"What class variables are",[198,19440,19441],{},"How they are different",[198,19443,19444],{},"How to choose the right one",[31,19446,19448],{"id":19447},"what-is-an-instance-variable","What is an instance variable?",[14,19450,19451,19452,19454],{},"An ",[2421,19453,19190],{}," belongs to one object.",[14,19456,19457,19458,19461,19462,19464,19465,240],{},"It is usually created with ",[18,19459,19460],{},"self.variable_name"," inside ",[18,19463,3262],{},". If you are new to this, see ",[236,19466,3267,19468,3270],{"href":19467},"\u002Flearn\u002Fthe-__init__-method-in-python-explained\u002F",[18,19469,3262],{},[14,19471,466],{},[36,19473,19475],{"className":38,"code":19474,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n\ndog1 = Dog(\"Max\", 5)\ndog2 = Dog(\"Bella\", 3)\n\nprint(dog1.name)\nprint(dog2.name)\n",[18,19476,19477,19485,19505,19517,19529,19533,19537,19560,19583,19587,19602],{"__ignoreMap":41},[45,19478,19479,19481,19483],{"class":47,"line":48},[45,19480,2441],{"class":2440},[45,19482,2445],{"class":2444},[45,19484,86],{"class":85},[45,19486,19487,19489,19491,19493,19495,19497,19499,19501,19503],{"class":47,"line":63},[45,19488,2452],{"class":2440},[45,19490,3072],{"class":92},[45,19492,96],{"class":85},[45,19494,2427],{"class":2461},[45,19496,3079],{"class":85},[45,19498,3083],{"class":3082},[45,19500,3079],{"class":85},[45,19502,3305],{"class":3082},[45,19504,2464],{"class":85},[45,19506,19507,19509,19511,19513,19515],{"class":47,"line":70},[45,19508,3090],{"class":2771},[45,19510,240],{"class":85},[45,19512,3096],{"class":3095},[45,19514,3099],{"class":55},[45,19516,3102],{"class":51},[45,19518,19519,19521,19523,19525,19527],{"class":47,"line":89},[45,19520,3090],{"class":2771},[45,19522,240],{"class":85},[45,19524,2082],{"class":3095},[45,19526,3099],{"class":55},[45,19528,3320],{"class":51},[45,19530,19531],{"class":47,"line":112},[45,19532,67],{"emptyLinePlaceholder":66},[45,19534,19535],{"class":47,"line":126},[45,19536,67],{"emptyLinePlaceholder":66},[45,19538,19539,19542,19544,19546,19548,19550,19552,19554,19556,19558],{"class":47,"line":142},[45,19540,19541],{"class":51},"dog1 ",[45,19543,56],{"class":55},[45,19545,2445],{"class":2070},[45,19547,96],{"class":85},[45,19549,100],{"class":99},[45,19551,19306],{"class":103},[45,19553,100],{"class":99},[45,19555,3079],{"class":85},[45,19557,1222],{"class":59},[45,19559,109],{"class":85},[45,19561,19562,19565,19567,19569,19571,19573,19575,19577,19579,19581],{"class":47,"line":149},[45,19563,19564],{"class":51},"dog2 ",[45,19566,56],{"class":55},[45,19568,2445],{"class":2070},[45,19570,96],{"class":85},[45,19572,100],{"class":99},[45,19574,19326],{"class":103},[45,19576,100],{"class":99},[45,19578,3079],{"class":85},[45,19580,777],{"class":59},[45,19582,109],{"class":85},[45,19584,19585],{"class":47,"line":679},[45,19586,67],{"emptyLinePlaceholder":66},[45,19588,19589,19591,19593,19596,19598,19600],{"class":47,"line":686},[45,19590,2065],{"class":92},[45,19592,96],{"class":85},[45,19594,19595],{"class":2070},"dog1",[45,19597,240],{"class":85},[45,19599,3096],{"class":3095},[45,19601,109],{"class":85},[45,19603,19604,19606,19608,19611,19613,19615],{"class":47,"line":2229},[45,19605,2065],{"class":92},[45,19607,96],{"class":85},[45,19609,19610],{"class":2070},"dog2",[45,19612,240],{"class":85},[45,19614,3096],{"class":3095},[45,19616,109],{"class":85},[14,19618,19619],{},[2421,19620,177],{},[36,19622,19624],{"className":38,"code":19623,"language":40,"meta":41,"style":41},"Max\nBella\n",[18,19625,19626,19630],{"__ignoreMap":41},[45,19627,19628],{"class":47,"line":48},[45,19629,19415],{"class":51},[45,19631,19632],{"class":47,"line":63},[45,19633,19420],{"class":51},[14,19635,6680],{},[195,19637,19638,19643,19648],{},[198,19639,19640,19642],{},[18,19641,3044],{}," is an instance variable",[198,19644,19645,19647],{},[18,19646,3047],{}," is also an instance variable",[198,19649,19650],{},"each object gets its own values",[14,19652,19653],{},"If you change one object's instance variable, the other objects do not change.",[36,19655,19657],{"className":38,"code":19656,"language":40,"meta":41,"style":41},"dog1.name = \"Rocky\"\n\nprint(dog1.name)\nprint(dog2.name)\n",[18,19658,19659,19676,19680,19694],{"__ignoreMap":41},[45,19660,19661,19663,19665,19667,19669,19671,19674],{"class":47,"line":48},[45,19662,19595],{"class":51},[45,19664,240],{"class":85},[45,19666,3096],{"class":3095},[45,19668,3099],{"class":55},[45,19670,1565],{"class":99},[45,19672,19673],{"class":103},"Rocky",[45,19675,1571],{"class":99},[45,19677,19678],{"class":47,"line":63},[45,19679,67],{"emptyLinePlaceholder":66},[45,19681,19682,19684,19686,19688,19690,19692],{"class":47,"line":70},[45,19683,2065],{"class":92},[45,19685,96],{"class":85},[45,19687,19595],{"class":2070},[45,19689,240],{"class":85},[45,19691,3096],{"class":3095},[45,19693,109],{"class":85},[45,19695,19696,19698,19700,19702,19704,19706],{"class":47,"line":89},[45,19697,2065],{"class":92},[45,19699,96],{"class":85},[45,19701,19610],{"class":2070},[45,19703,240],{"class":85},[45,19705,3096],{"class":3095},[45,19707,109],{"class":85},[14,19709,19710],{},[2421,19711,177],{},[36,19713,19715],{"className":38,"code":19714,"language":40,"meta":41,"style":41},"Rocky\nBella\n",[18,19716,19717,19722],{"__ignoreMap":41},[45,19718,19719],{"class":47,"line":48},[45,19720,19721],{"class":51},"Rocky\n",[45,19723,19724],{"class":47,"line":63},[45,19725,19420],{"class":51},[31,19727,19729],{"id":19728},"what-is-a-class-variable","What is a class variable?",[14,19731,2419,19732,19734],{},[2421,19733,19197],{}," belongs to the class itself.",[14,19736,19737],{},"It is defined inside the class, but outside methods.",[14,19739,466],{},[36,19741,19743],{"className":38,"code":19742,"language":40,"meta":41,"style":41},"class Dog:\n    species = \"Canis familiaris\"\n\n    def __init__(self, name):\n        self.name = name\n\n\ndog1 = Dog(\"Max\")\ndog2 = Dog(\"Bella\")\n\nprint(Dog.species)\nprint(dog1.species)\nprint(dog2.species)\n",[18,19744,19745,19753,19765,19769,19785,19797,19801,19805,19823,19841,19845,19859,19873],{"__ignoreMap":41},[45,19746,19747,19749,19751],{"class":47,"line":48},[45,19748,2441],{"class":2440},[45,19750,2445],{"class":2444},[45,19752,86],{"class":85},[45,19754,19755,19757,19759,19761,19763],{"class":47,"line":63},[45,19756,19235],{"class":51},[45,19758,56],{"class":55},[45,19760,1565],{"class":99},[45,19762,19242],{"class":103},[45,19764,1571],{"class":99},[45,19766,19767],{"class":47,"line":70},[45,19768,67],{"emptyLinePlaceholder":66},[45,19770,19771,19773,19775,19777,19779,19781,19783],{"class":47,"line":89},[45,19772,2452],{"class":2440},[45,19774,3072],{"class":92},[45,19776,96],{"class":85},[45,19778,2427],{"class":2461},[45,19780,3079],{"class":85},[45,19782,3083],{"class":3082},[45,19784,2464],{"class":85},[45,19786,19787,19789,19791,19793,19795],{"class":47,"line":112},[45,19788,3090],{"class":2771},[45,19790,240],{"class":85},[45,19792,3096],{"class":3095},[45,19794,3099],{"class":55},[45,19796,3102],{"class":51},[45,19798,19799],{"class":47,"line":126},[45,19800,67],{"emptyLinePlaceholder":66},[45,19802,19803],{"class":47,"line":142},[45,19804,67],{"emptyLinePlaceholder":66},[45,19806,19807,19809,19811,19813,19815,19817,19819,19821],{"class":47,"line":149},[45,19808,19541],{"class":51},[45,19810,56],{"class":55},[45,19812,2445],{"class":2070},[45,19814,96],{"class":85},[45,19816,100],{"class":99},[45,19818,19306],{"class":103},[45,19820,100],{"class":99},[45,19822,109],{"class":85},[45,19824,19825,19827,19829,19831,19833,19835,19837,19839],{"class":47,"line":679},[45,19826,19564],{"class":51},[45,19828,56],{"class":55},[45,19830,2445],{"class":2070},[45,19832,96],{"class":85},[45,19834,100],{"class":99},[45,19836,19326],{"class":103},[45,19838,100],{"class":99},[45,19840,109],{"class":85},[45,19842,19843],{"class":47,"line":686},[45,19844,67],{"emptyLinePlaceholder":66},[45,19846,19847,19849,19851,19853,19855,19857],{"class":47,"line":2229},[45,19848,2065],{"class":92},[45,19850,96],{"class":85},[45,19852,2570],{"class":2070},[45,19854,240],{"class":85},[45,19856,19347],{"class":3095},[45,19858,109],{"class":85},[45,19860,19861,19863,19865,19867,19869,19871],{"class":47,"line":2236},[45,19862,2065],{"class":92},[45,19864,96],{"class":85},[45,19866,19595],{"class":2070},[45,19868,240],{"class":85},[45,19870,19347],{"class":3095},[45,19872,109],{"class":85},[45,19874,19875,19877,19879,19881,19883,19885],{"class":47,"line":5728},[45,19876,2065],{"class":92},[45,19878,96],{"class":85},[45,19880,19610],{"class":2070},[45,19882,240],{"class":85},[45,19884,19347],{"class":3095},[45,19886,109],{"class":85},[14,19888,19889],{},[2421,19890,177],{},[36,19892,19894],{"className":38,"code":19893,"language":40,"meta":41,"style":41},"Canis familiaris\nCanis familiaris\nCanis familiaris\n",[18,19895,19896,19900,19904],{"__ignoreMap":41},[45,19897,19898],{"class":47,"line":48},[45,19899,19406],{"class":51},[45,19901,19902],{"class":47,"line":63},[45,19903,19406],{"class":51},[45,19905,19906],{"class":47,"line":70},[45,19907,19406],{"class":51},[14,19909,6680],{},[195,19911,19912,19917,19923],{},[198,19913,19914,19916],{},[18,19915,19347],{}," is a class variable",[198,19918,19919,19920,19922],{},"it is shared by all ",[18,19921,2570],{}," objects",[198,19924,19925],{},"both objects can read it",[14,19927,19928],{},"Class variables are useful for:",[195,19930,19931,19934,19937],{},[198,19932,19933],{},"constants",[198,19935,19936],{},"shared settings",[198,19938,19939],{},"values that should be the same for every object",[14,19941,19942,19943,230,19946,240],{},"If you need a refresher on classes and objects, see ",[236,19944,2560],{"href":19945},"\u002Flearn\u002Fpython-classes-and-objects-explained\u002F",[236,19947,19949],{"href":19948},"\u002Fglossary\u002Fwhat-is-a-class-in-python\u002F","what is a class in Python",[31,19951,19953],{"id":19952},"main-difference-at-a-glance","Main difference at a glance",[14,19955,19956],{},"The difference is simple:",[195,19958,19959,19965],{},[198,19960,19961,19964],{},[2421,19962,19963],{},"Instance variable",": one value per object",[198,19966,19967,19970],{},[2421,19968,19969],{},"Class variable",": one shared value on the class",[14,19972,19973],{},"Typical uses:",[195,19975,19976,19989],{},[198,19977,19978,19979,21,19981,21,19983,21,19986],{},"instance variables: ",[18,19980,3096],{},[18,19982,2082],{},[18,19984,19985],{},"price",[18,19987,19988],{},"file_path",[198,19990,19991,19992,21,19994,21,19997,21,20000],{},"class variables: ",[18,19993,19347],{},[18,19995,19996],{},"app_name",[18,19998,19999],{},"tax_rate",[18,20001,20002],{},"counter",[14,20004,466],{},[36,20006,20008],{"className":38,"code":20007,"language":40,"meta":41,"style":41},"class Product:\n    store_name = \"Python Shop\"   # class variable\n\n    def __init__(self, name, price):\n        self.name = name         # instance variable\n        self.price = price       # instance variable\n\n\np1 = Product(\"Keyboard\", 50)\np2 = Product(\"Mouse\", 25)\n\nprint(p1.store_name)\nprint(p2.store_name)\nprint(p1.name, p1.price)\nprint(p2.name, p2.price)\n",[18,20009,20010,20019,20036,20040,20061,20076,20091,20095,20099,20124,20148,20152,20168,20183,20206],{"__ignoreMap":41},[45,20011,20012,20014,20017],{"class":47,"line":48},[45,20013,2441],{"class":2440},[45,20015,20016],{"class":2444}," Product",[45,20018,86],{"class":85},[45,20020,20021,20024,20026,20028,20031,20033],{"class":47,"line":63},[45,20022,20023],{"class":51},"    store_name ",[45,20025,56],{"class":55},[45,20027,1565],{"class":99},[45,20029,20030],{"class":103},"Python Shop",[45,20032,100],{"class":99},[45,20034,20035],{"class":7624},"   # class variable\n",[45,20037,20038],{"class":47,"line":70},[45,20039,67],{"emptyLinePlaceholder":66},[45,20041,20042,20044,20046,20048,20050,20052,20054,20056,20059],{"class":47,"line":89},[45,20043,2452],{"class":2440},[45,20045,3072],{"class":92},[45,20047,96],{"class":85},[45,20049,2427],{"class":2461},[45,20051,3079],{"class":85},[45,20053,3083],{"class":3082},[45,20055,3079],{"class":85},[45,20057,20058],{"class":3082}," price",[45,20060,2464],{"class":85},[45,20062,20063,20065,20067,20069,20071,20074],{"class":47,"line":112},[45,20064,3090],{"class":2771},[45,20066,240],{"class":85},[45,20068,3096],{"class":3095},[45,20070,3099],{"class":55},[45,20072,20073],{"class":51}," name         ",[45,20075,19283],{"class":7624},[45,20077,20078,20080,20082,20084,20086,20089],{"class":47,"line":126},[45,20079,3090],{"class":2771},[45,20081,240],{"class":85},[45,20083,19985],{"class":3095},[45,20085,3099],{"class":55},[45,20087,20088],{"class":51}," price       ",[45,20090,19283],{"class":7624},[45,20092,20093],{"class":47,"line":142},[45,20094,67],{"emptyLinePlaceholder":66},[45,20096,20097],{"class":47,"line":149},[45,20098,67],{"emptyLinePlaceholder":66},[45,20100,20101,20104,20106,20108,20110,20112,20115,20117,20119,20122],{"class":47,"line":679},[45,20102,20103],{"class":51},"p1 ",[45,20105,56],{"class":55},[45,20107,20016],{"class":2070},[45,20109,96],{"class":85},[45,20111,100],{"class":99},[45,20113,20114],{"class":103},"Keyboard",[45,20116,100],{"class":99},[45,20118,3079],{"class":85},[45,20120,20121],{"class":59}," 50",[45,20123,109],{"class":85},[45,20125,20126,20129,20131,20133,20135,20137,20140,20142,20144,20146],{"class":47,"line":686},[45,20127,20128],{"class":51},"p2 ",[45,20130,56],{"class":55},[45,20132,20016],{"class":2070},[45,20134,96],{"class":85},[45,20136,100],{"class":99},[45,20138,20139],{"class":103},"Mouse",[45,20141,100],{"class":99},[45,20143,3079],{"class":85},[45,20145,303],{"class":59},[45,20147,109],{"class":85},[45,20149,20150],{"class":47,"line":2229},[45,20151,67],{"emptyLinePlaceholder":66},[45,20153,20154,20156,20158,20161,20163,20166],{"class":47,"line":2236},[45,20155,2065],{"class":92},[45,20157,96],{"class":85},[45,20159,20160],{"class":2070},"p1",[45,20162,240],{"class":85},[45,20164,20165],{"class":3095},"store_name",[45,20167,109],{"class":85},[45,20169,20170,20172,20174,20177,20179,20181],{"class":47,"line":5728},[45,20171,2065],{"class":92},[45,20173,96],{"class":85},[45,20175,20176],{"class":2070},"p2",[45,20178,240],{"class":85},[45,20180,20165],{"class":3095},[45,20182,109],{"class":85},[45,20184,20185,20187,20189,20191,20193,20195,20197,20200,20202,20204],{"class":47,"line":19380},[45,20186,2065],{"class":92},[45,20188,96],{"class":85},[45,20190,20160],{"class":2070},[45,20192,240],{"class":85},[45,20194,3096],{"class":3095},[45,20196,3079],{"class":85},[45,20198,20199],{"class":2070}," p1",[45,20201,240],{"class":85},[45,20203,19985],{"class":3095},[45,20205,109],{"class":85},[45,20207,20209,20211,20213,20215,20217,20219,20221,20224,20226,20228],{"class":47,"line":20208},15,[45,20210,2065],{"class":92},[45,20212,96],{"class":85},[45,20214,20176],{"class":2070},[45,20216,240],{"class":85},[45,20218,3096],{"class":3095},[45,20220,3079],{"class":85},[45,20222,20223],{"class":2070}," p2",[45,20225,240],{"class":85},[45,20227,19985],{"class":3095},[45,20229,109],{"class":85},[31,20231,20233],{"id":20232},"how-python-looks-up-variables","How Python looks up variables",[14,20235,20236,20237,20240],{},"When you write ",[18,20238,20239],{},"obj.variable_name",", Python does not only check the object.",[14,20242,20243],{},"Python looks in this order:",[346,20245,20246,20249],{},[198,20247,20248],{},"first, it checks the object for an instance variable",[198,20250,20251],{},"if it does not find one, it checks the class",[14,20253,20254],{},"This is why objects can read class variables.",[14,20256,466],{},[36,20258,20260],{"className":38,"code":20259,"language":40,"meta":41,"style":41},"class Example:\n    value = 100  # class variable\n\n\nobj = Example()\n\nprint(obj.value)\n",[18,20261,20262,20271,20283,20287,20291,20302,20306],{"__ignoreMap":41},[45,20263,20264,20266,20269],{"class":47,"line":48},[45,20265,2441],{"class":2440},[45,20267,20268],{"class":2444}," Example",[45,20270,86],{"class":85},[45,20272,20273,20276,20278,20281],{"class":47,"line":63},[45,20274,20275],{"class":51},"    value ",[45,20277,56],{"class":55},[45,20279,20280],{"class":59}," 100",[45,20282,19247],{"class":7624},[45,20284,20285],{"class":47,"line":70},[45,20286,67],{"emptyLinePlaceholder":66},[45,20288,20289],{"class":47,"line":89},[45,20290,67],{"emptyLinePlaceholder":66},[45,20292,20293,20296,20298,20300],{"class":47,"line":112},[45,20294,20295],{"class":51},"obj ",[45,20297,56],{"class":55},[45,20299,20268],{"class":2070},[45,20301,2495],{"class":85},[45,20303,20304],{"class":47,"line":126},[45,20305,67],{"emptyLinePlaceholder":66},[45,20307,20308,20310,20312,20315,20317,20319],{"class":47,"line":142},[45,20309,2065],{"class":92},[45,20311,96],{"class":85},[45,20313,20314],{"class":2070},"obj",[45,20316,240],{"class":85},[45,20318,12722],{"class":3095},[45,20320,109],{"class":85},[14,20322,20323],{},[2421,20324,177],{},[36,20326,20328],{"className":38,"code":20327,"language":40,"meta":41,"style":41},"100\n",[18,20329,20330],{"__ignoreMap":41},[45,20331,20332],{"class":47,"line":48},[45,20333,20327],{"class":59},[14,20335,20336,20338,20339,20341,20342,20344],{},[18,20337,20314],{}," does not have its own ",[18,20340,12722],{},", so Python gets ",[18,20343,12722],{}," from the class.",[14,20346,20347],{},"Now look at this:",[36,20349,20351],{"className":38,"code":20350,"language":40,"meta":41,"style":41},"class Example:\n    value = 100\n\n\nobj = Example()\nobj.value = 200\n\nprint(obj.value)\nprint(Example.value)\n",[18,20352,20353,20361,20370,20374,20378,20388,20401,20405,20419],{"__ignoreMap":41},[45,20354,20355,20357,20359],{"class":47,"line":48},[45,20356,2441],{"class":2440},[45,20358,20268],{"class":2444},[45,20360,86],{"class":85},[45,20362,20363,20365,20367],{"class":47,"line":63},[45,20364,20275],{"class":51},[45,20366,56],{"class":55},[45,20368,20369],{"class":59}," 100\n",[45,20371,20372],{"class":47,"line":70},[45,20373,67],{"emptyLinePlaceholder":66},[45,20375,20376],{"class":47,"line":89},[45,20377,67],{"emptyLinePlaceholder":66},[45,20379,20380,20382,20384,20386],{"class":47,"line":112},[45,20381,20295],{"class":51},[45,20383,56],{"class":55},[45,20385,20268],{"class":2070},[45,20387,2495],{"class":85},[45,20389,20390,20392,20394,20396,20398],{"class":47,"line":126},[45,20391,20314],{"class":51},[45,20393,240],{"class":85},[45,20395,12722],{"class":3095},[45,20397,3099],{"class":55},[45,20399,20400],{"class":59}," 200\n",[45,20402,20403],{"class":47,"line":142},[45,20404,67],{"emptyLinePlaceholder":66},[45,20406,20407,20409,20411,20413,20415,20417],{"class":47,"line":149},[45,20408,2065],{"class":92},[45,20410,96],{"class":85},[45,20412,20314],{"class":2070},[45,20414,240],{"class":85},[45,20416,12722],{"class":3095},[45,20418,109],{"class":85},[45,20420,20421,20423,20425,20428,20430,20432],{"class":47,"line":679},[45,20422,2065],{"class":92},[45,20424,96],{"class":85},[45,20426,20427],{"class":2070},"Example",[45,20429,240],{"class":85},[45,20431,12722],{"class":3095},[45,20433,109],{"class":85},[14,20435,20436],{},[2421,20437,177],{},[36,20439,20441],{"className":38,"code":20440,"language":40,"meta":41,"style":41},"200\n100\n",[18,20442,20443,20448],{"__ignoreMap":41},[45,20444,20445],{"class":47,"line":48},[45,20446,20447],{"class":59},"200\n",[45,20449,20450],{"class":47,"line":63},[45,20451,20327],{"class":59},[14,20453,20454],{},"What happened?",[195,20456,20457,20465,20471],{},[198,20458,20459,20462,20463],{},[18,20460,20461],{},"obj.value = 200"," created an ",[2421,20464,19190],{},[198,20466,20467,20468,20470],{},"it did ",[2421,20469,2857],{}," change the class variable",[198,20472,20473],{},"the instance variable now hides the class variable for that object",[14,20475,20476,20477,240],{},"This idea becomes easier once you are comfortable with ",[236,20478,20480],{"href":20479},"\u002Fglossary\u002Fwhat-is-an-object-in-python\u002F","what is an object in Python",[31,20482,20484],{"id":20483},"when-to-use-instance-variables","When to use instance variables",[14,20486,20487],{},"Use instance variables when each object should store its own data.",[14,20489,20490],{},"Good examples:",[195,20492,20493,20495,20497,20499],{},[198,20494,3096],{},[198,20496,2082],{},[198,20498,19985],{},[198,20500,20501],{},"file path",[14,20503,466],{},[36,20505,20507],{"className":38,"code":20506,"language":40,"meta":41,"style":41},"class User:\n    def __init__(self, username, email):\n        self.username = username\n        self.email = email\n\n\nu1 = User(\"anna\", \"anna@example.com\")\nu2 = User(\"ben\", \"ben@example.com\")\n\nprint(u1.username, u1.email)\nprint(u2.username, u2.email)\n",[18,20508,20509,20518,20540,20553,20567,20571,20575,20604,20633,20637,20661],{"__ignoreMap":41},[45,20510,20511,20513,20516],{"class":47,"line":48},[45,20512,2441],{"class":2440},[45,20514,20515],{"class":2444}," User",[45,20517,86],{"class":85},[45,20519,20520,20522,20524,20526,20528,20530,20533,20535,20538],{"class":47,"line":63},[45,20521,2452],{"class":2440},[45,20523,3072],{"class":92},[45,20525,96],{"class":85},[45,20527,2427],{"class":2461},[45,20529,3079],{"class":85},[45,20531,20532],{"class":3082}," username",[45,20534,3079],{"class":85},[45,20536,20537],{"class":3082}," email",[45,20539,2464],{"class":85},[45,20541,20542,20544,20546,20548,20550],{"class":47,"line":70},[45,20543,3090],{"class":2771},[45,20545,240],{"class":85},[45,20547,7977],{"class":3095},[45,20549,3099],{"class":55},[45,20551,20552],{"class":51}," username\n",[45,20554,20555,20557,20559,20562,20564],{"class":47,"line":89},[45,20556,3090],{"class":2771},[45,20558,240],{"class":85},[45,20560,20561],{"class":3095},"email",[45,20563,3099],{"class":55},[45,20565,20566],{"class":51}," email\n",[45,20568,20569],{"class":47,"line":112},[45,20570,67],{"emptyLinePlaceholder":66},[45,20572,20573],{"class":47,"line":126},[45,20574,67],{"emptyLinePlaceholder":66},[45,20576,20577,20580,20582,20584,20586,20588,20591,20593,20595,20597,20600,20602],{"class":47,"line":142},[45,20578,20579],{"class":51},"u1 ",[45,20581,56],{"class":55},[45,20583,20515],{"class":2070},[45,20585,96],{"class":85},[45,20587,100],{"class":99},[45,20589,20590],{"class":103},"anna",[45,20592,100],{"class":99},[45,20594,3079],{"class":85},[45,20596,1565],{"class":99},[45,20598,20599],{"class":103},"anna@example.com",[45,20601,100],{"class":99},[45,20603,109],{"class":85},[45,20605,20606,20609,20611,20613,20615,20617,20620,20622,20624,20626,20629,20631],{"class":47,"line":149},[45,20607,20608],{"class":51},"u2 ",[45,20610,56],{"class":55},[45,20612,20515],{"class":2070},[45,20614,96],{"class":85},[45,20616,100],{"class":99},[45,20618,20619],{"class":103},"ben",[45,20621,100],{"class":99},[45,20623,3079],{"class":85},[45,20625,1565],{"class":99},[45,20627,20628],{"class":103},"ben@example.com",[45,20630,100],{"class":99},[45,20632,109],{"class":85},[45,20634,20635],{"class":47,"line":679},[45,20636,67],{"emptyLinePlaceholder":66},[45,20638,20639,20641,20643,20646,20648,20650,20652,20655,20657,20659],{"class":47,"line":686},[45,20640,2065],{"class":92},[45,20642,96],{"class":85},[45,20644,20645],{"class":2070},"u1",[45,20647,240],{"class":85},[45,20649,7977],{"class":3095},[45,20651,3079],{"class":85},[45,20653,20654],{"class":2070}," u1",[45,20656,240],{"class":85},[45,20658,20561],{"class":3095},[45,20660,109],{"class":85},[45,20662,20663,20665,20667,20670,20672,20674,20676,20679,20681,20683],{"class":47,"line":2229},[45,20664,2065],{"class":92},[45,20666,96],{"class":85},[45,20668,20669],{"class":2070},"u2",[45,20671,240],{"class":85},[45,20673,7977],{"class":3095},[45,20675,3079],{"class":85},[45,20677,20678],{"class":2070}," u2",[45,20680,240],{"class":85},[45,20682,20561],{"class":3095},[45,20684,109],{"class":85},[14,20686,20687],{},"This is the best choice for values that are different from one object to another.",[31,20689,20691],{"id":20690},"when-to-use-class-variables","When to use class variables",[14,20693,20694],{},"Use class variables when the value should be shared.",[14,20696,20490],{},[195,20698,20699,20702,20705,20708],{},[198,20700,20701],{},"default tax rate",[198,20703,20704],{},"app name",[198,20706,20707],{},"category",[198,20709,20710],{},"object counter",[14,20712,466],{},[36,20714,20716],{"className":38,"code":20715,"language":40,"meta":41,"style":41},"class Account:\n    bank_name = \"Python Bank\"\n\n    def __init__(self, owner):\n        self.owner = owner\n\n\na1 = Account(\"Ava\")\na2 = Account(\"Noah\")\n\nprint(Account.bank_name)\nprint(a1.bank_name)\nprint(a2.bank_name)\n",[18,20717,20718,20727,20741,20745,20762,20776,20780,20784,20803,20823,20827,20843,20858],{"__ignoreMap":41},[45,20719,20720,20722,20725],{"class":47,"line":48},[45,20721,2441],{"class":2440},[45,20723,20724],{"class":2444}," Account",[45,20726,86],{"class":85},[45,20728,20729,20732,20734,20736,20739],{"class":47,"line":63},[45,20730,20731],{"class":51},"    bank_name ",[45,20733,56],{"class":55},[45,20735,1565],{"class":99},[45,20737,20738],{"class":103},"Python Bank",[45,20740,1571],{"class":99},[45,20742,20743],{"class":47,"line":70},[45,20744,67],{"emptyLinePlaceholder":66},[45,20746,20747,20749,20751,20753,20755,20757,20760],{"class":47,"line":89},[45,20748,2452],{"class":2440},[45,20750,3072],{"class":92},[45,20752,96],{"class":85},[45,20754,2427],{"class":2461},[45,20756,3079],{"class":85},[45,20758,20759],{"class":3082}," owner",[45,20761,2464],{"class":85},[45,20763,20764,20766,20768,20771,20773],{"class":47,"line":112},[45,20765,3090],{"class":2771},[45,20767,240],{"class":85},[45,20769,20770],{"class":3095},"owner",[45,20772,3099],{"class":55},[45,20774,20775],{"class":51}," owner\n",[45,20777,20778],{"class":47,"line":126},[45,20779,67],{"emptyLinePlaceholder":66},[45,20781,20782],{"class":47,"line":142},[45,20783,67],{"emptyLinePlaceholder":66},[45,20785,20786,20789,20791,20793,20795,20797,20799,20801],{"class":47,"line":149},[45,20787,20788],{"class":51},"a1 ",[45,20790,56],{"class":55},[45,20792,20724],{"class":2070},[45,20794,96],{"class":85},[45,20796,100],{"class":99},[45,20798,3162],{"class":103},[45,20800,100],{"class":99},[45,20802,109],{"class":85},[45,20804,20805,20808,20810,20812,20814,20816,20819,20821],{"class":47,"line":679},[45,20806,20807],{"class":51},"a2 ",[45,20809,56],{"class":55},[45,20811,20724],{"class":2070},[45,20813,96],{"class":85},[45,20815,100],{"class":99},[45,20817,20818],{"class":103},"Noah",[45,20820,100],{"class":99},[45,20822,109],{"class":85},[45,20824,20825],{"class":47,"line":686},[45,20826,67],{"emptyLinePlaceholder":66},[45,20828,20829,20831,20833,20836,20838,20841],{"class":47,"line":2229},[45,20830,2065],{"class":92},[45,20832,96],{"class":85},[45,20834,20835],{"class":2070},"Account",[45,20837,240],{"class":85},[45,20839,20840],{"class":3095},"bank_name",[45,20842,109],{"class":85},[45,20844,20845,20847,20849,20852,20854,20856],{"class":47,"line":2236},[45,20846,2065],{"class":92},[45,20848,96],{"class":85},[45,20850,20851],{"class":2070},"a1",[45,20853,240],{"class":85},[45,20855,20840],{"class":3095},[45,20857,109],{"class":85},[45,20859,20860,20862,20864,20867,20869,20871],{"class":47,"line":5728},[45,20861,2065],{"class":92},[45,20863,96],{"class":85},[45,20865,20866],{"class":2070},"a2",[45,20868,240],{"class":85},[45,20870,20840],{"class":3095},[45,20872,109],{"class":85},[14,20874,20875,20876,20878],{},"If the data should be different for each object, do ",[2421,20877,2857],{}," use a class variable.",[31,20880,20882],{"id":20881},"common-beginner-mistake-mutable-class-variables","Common beginner mistake: mutable class variables",[14,20884,20885],{},"Be careful with mutable values like:",[195,20887,20888,20891,20894],{},[198,20889,20890],{},"lists",[198,20892,20893],{},"dictionaries",[198,20895,20896],{},"sets",[14,20898,20899],{},"If you put one of these in a class variable, all objects share the same value.",[14,20901,20902],{},"Example of the problem:",[36,20904,20906],{"className":38,"code":20905,"language":40,"meta":41,"style":41},"class Student:\n    grades = []  # class variable\n\n    def __init__(self, name):\n        self.name = name\n\n\ns1 = Student(\"Ava\")\ns2 = Student(\"Noah\")\n\ns1.grades.append(95)\n\nprint(s1.grades)\nprint(s2.grades)\n",[18,20907,20908,20917,20929,20933,20949,20961,20965,20969,20988,21007,21011,21033,21037,21051],{"__ignoreMap":41},[45,20909,20910,20912,20915],{"class":47,"line":48},[45,20911,2441],{"class":2440},[45,20913,20914],{"class":2444}," Student",[45,20916,86],{"class":85},[45,20918,20919,20922,20924,20927],{"class":47,"line":63},[45,20920,20921],{"class":51},"    grades ",[45,20923,56],{"class":55},[45,20925,20926],{"class":85}," []",[45,20928,19247],{"class":7624},[45,20930,20931],{"class":47,"line":70},[45,20932,67],{"emptyLinePlaceholder":66},[45,20934,20935,20937,20939,20941,20943,20945,20947],{"class":47,"line":89},[45,20936,2452],{"class":2440},[45,20938,3072],{"class":92},[45,20940,96],{"class":85},[45,20942,2427],{"class":2461},[45,20944,3079],{"class":85},[45,20946,3083],{"class":3082},[45,20948,2464],{"class":85},[45,20950,20951,20953,20955,20957,20959],{"class":47,"line":112},[45,20952,3090],{"class":2771},[45,20954,240],{"class":85},[45,20956,3096],{"class":3095},[45,20958,3099],{"class":55},[45,20960,3102],{"class":51},[45,20962,20963],{"class":47,"line":126},[45,20964,67],{"emptyLinePlaceholder":66},[45,20966,20967],{"class":47,"line":142},[45,20968,67],{"emptyLinePlaceholder":66},[45,20970,20971,20974,20976,20978,20980,20982,20984,20986],{"class":47,"line":149},[45,20972,20973],{"class":51},"s1 ",[45,20975,56],{"class":55},[45,20977,20914],{"class":2070},[45,20979,96],{"class":85},[45,20981,100],{"class":99},[45,20983,3162],{"class":103},[45,20985,100],{"class":99},[45,20987,109],{"class":85},[45,20989,20990,20993,20995,20997,20999,21001,21003,21005],{"class":47,"line":679},[45,20991,20992],{"class":51},"s2 ",[45,20994,56],{"class":55},[45,20996,20914],{"class":2070},[45,20998,96],{"class":85},[45,21000,100],{"class":99},[45,21002,20818],{"class":103},[45,21004,100],{"class":99},[45,21006,109],{"class":85},[45,21008,21009],{"class":47,"line":686},[45,21010,67],{"emptyLinePlaceholder":66},[45,21012,21013,21016,21018,21021,21023,21026,21028,21031],{"class":47,"line":2229},[45,21014,21015],{"class":51},"s1",[45,21017,240],{"class":85},[45,21019,21020],{"class":3095},"grades",[45,21022,240],{"class":85},[45,21024,21025],{"class":2070},"append",[45,21027,96],{"class":85},[45,21029,21030],{"class":59},"95",[45,21032,109],{"class":85},[45,21034,21035],{"class":47,"line":2236},[45,21036,67],{"emptyLinePlaceholder":66},[45,21038,21039,21041,21043,21045,21047,21049],{"class":47,"line":5728},[45,21040,2065],{"class":92},[45,21042,96],{"class":85},[45,21044,21015],{"class":2070},[45,21046,240],{"class":85},[45,21048,21020],{"class":3095},[45,21050,109],{"class":85},[45,21052,21053,21055,21057,21060,21062,21064],{"class":47,"line":19380},[45,21054,2065],{"class":92},[45,21056,96],{"class":85},[45,21058,21059],{"class":2070},"s2",[45,21061,240],{"class":85},[45,21063,21020],{"class":3095},[45,21065,109],{"class":85},[14,21067,21068],{},[2421,21069,177],{},[36,21071,21073],{"className":38,"code":21072,"language":40,"meta":41,"style":41},"[95]\n[95]\n",[18,21074,21075,21083],{"__ignoreMap":41},[45,21076,21077,21079,21081],{"class":47,"line":48},[45,21078,9680],{"class":85},[45,21080,21030],{"class":59},[45,21082,8594],{"class":85},[45,21084,21085,21087,21089],{"class":47,"line":63},[45,21086,9680],{"class":85},[45,21088,21030],{"class":59},[45,21090,8594],{"class":85},[14,21092,21093],{},"Both objects show the same list because there is only one shared class variable.",[14,21095,21096],{},"If each object should have its own list, create it as an instance variable instead:",[36,21098,21100],{"className":38,"code":21099,"language":40,"meta":41,"style":41},"class Student:\n    def __init__(self, name):\n        self.name = name\n        self.grades = []  # instance variable\n\n\ns1 = Student(\"Ava\")\ns2 = Student(\"Noah\")\n\ns1.grades.append(95)\n\nprint(s1.grades)\nprint(s2.grades)\n",[18,21101,21102,21110,21126,21138,21153,21157,21161,21179,21197,21201,21219,21223,21237],{"__ignoreMap":41},[45,21103,21104,21106,21108],{"class":47,"line":48},[45,21105,2441],{"class":2440},[45,21107,20914],{"class":2444},[45,21109,86],{"class":85},[45,21111,21112,21114,21116,21118,21120,21122,21124],{"class":47,"line":63},[45,21113,2452],{"class":2440},[45,21115,3072],{"class":92},[45,21117,96],{"class":85},[45,21119,2427],{"class":2461},[45,21121,3079],{"class":85},[45,21123,3083],{"class":3082},[45,21125,2464],{"class":85},[45,21127,21128,21130,21132,21134,21136],{"class":47,"line":70},[45,21129,3090],{"class":2771},[45,21131,240],{"class":85},[45,21133,3096],{"class":3095},[45,21135,3099],{"class":55},[45,21137,3102],{"class":51},[45,21139,21140,21142,21144,21146,21148,21150],{"class":47,"line":89},[45,21141,3090],{"class":2771},[45,21143,240],{"class":85},[45,21145,21020],{"class":3095},[45,21147,3099],{"class":55},[45,21149,20926],{"class":85},[45,21151,21152],{"class":7624},"  # instance variable\n",[45,21154,21155],{"class":47,"line":112},[45,21156,67],{"emptyLinePlaceholder":66},[45,21158,21159],{"class":47,"line":126},[45,21160,67],{"emptyLinePlaceholder":66},[45,21162,21163,21165,21167,21169,21171,21173,21175,21177],{"class":47,"line":142},[45,21164,20973],{"class":51},[45,21166,56],{"class":55},[45,21168,20914],{"class":2070},[45,21170,96],{"class":85},[45,21172,100],{"class":99},[45,21174,3162],{"class":103},[45,21176,100],{"class":99},[45,21178,109],{"class":85},[45,21180,21181,21183,21185,21187,21189,21191,21193,21195],{"class":47,"line":149},[45,21182,20992],{"class":51},[45,21184,56],{"class":55},[45,21186,20914],{"class":2070},[45,21188,96],{"class":85},[45,21190,100],{"class":99},[45,21192,20818],{"class":103},[45,21194,100],{"class":99},[45,21196,109],{"class":85},[45,21198,21199],{"class":47,"line":679},[45,21200,67],{"emptyLinePlaceholder":66},[45,21202,21203,21205,21207,21209,21211,21213,21215,21217],{"class":47,"line":686},[45,21204,21015],{"class":51},[45,21206,240],{"class":85},[45,21208,21020],{"class":3095},[45,21210,240],{"class":85},[45,21212,21025],{"class":2070},[45,21214,96],{"class":85},[45,21216,21030],{"class":59},[45,21218,109],{"class":85},[45,21220,21221],{"class":47,"line":2229},[45,21222,67],{"emptyLinePlaceholder":66},[45,21224,21225,21227,21229,21231,21233,21235],{"class":47,"line":2236},[45,21226,2065],{"class":92},[45,21228,96],{"class":85},[45,21230,21015],{"class":2070},[45,21232,240],{"class":85},[45,21234,21020],{"class":3095},[45,21236,109],{"class":85},[45,21238,21239,21241,21243,21245,21247,21249],{"class":47,"line":5728},[45,21240,2065],{"class":92},[45,21242,96],{"class":85},[45,21244,21059],{"class":2070},[45,21246,240],{"class":85},[45,21248,21020],{"class":3095},[45,21250,109],{"class":85},[14,21252,21253],{},[2421,21254,177],{},[36,21256,21258],{"className":38,"code":21257,"language":40,"meta":41,"style":41},"[95]\n[]\n",[18,21259,21260,21268],{"__ignoreMap":41},[45,21261,21262,21264,21266],{"class":47,"line":48},[45,21263,9680],{"class":85},[45,21265,21030],{"class":59},[45,21267,8594],{"class":85},[45,21269,21270],{"class":47,"line":63},[45,21271,14511],{"class":85},[14,21273,21274,21275,240],{},"If this topic feels confusing, it helps to understand ",[236,21276,21278],{"href":21277},"\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types\u002F","mutability in Python: mutable vs immutable types",[31,21280,21282],{"id":21281},"common-beginner-mistake-changing-through-the-object","Common beginner mistake: changing through the object",[14,21284,21285],{},"Reading a class variable through an object is fine:",[36,21287,21289],{"className":38,"code":21288,"language":40,"meta":41,"style":41},"class Car:\n    wheels = 4\n\n\ncar = Car()\nprint(car.wheels)\n",[18,21290,21291,21300,21310,21314,21318,21329],{"__ignoreMap":41},[45,21292,21293,21295,21298],{"class":47,"line":48},[45,21294,2441],{"class":2440},[45,21296,21297],{"class":2444}," Car",[45,21299,86],{"class":85},[45,21301,21302,21305,21307],{"class":47,"line":63},[45,21303,21304],{"class":51},"    wheels ",[45,21306,56],{"class":55},[45,21308,21309],{"class":59}," 4\n",[45,21311,21312],{"class":47,"line":70},[45,21313,67],{"emptyLinePlaceholder":66},[45,21315,21316],{"class":47,"line":89},[45,21317,67],{"emptyLinePlaceholder":66},[45,21319,21320,21323,21325,21327],{"class":47,"line":112},[45,21321,21322],{"class":51},"car ",[45,21324,56],{"class":55},[45,21326,21297],{"class":2070},[45,21328,2495],{"class":85},[45,21330,21331,21333,21335,21338,21340,21343],{"class":47,"line":126},[45,21332,2065],{"class":92},[45,21334,96],{"class":85},[45,21336,21337],{"class":2070},"car",[45,21339,240],{"class":85},[45,21341,21342],{"class":3095},"wheels",[45,21344,109],{"class":85},[14,21346,21347],{},"But assigning through the object can create a new instance variable:",[36,21349,21351],{"className":38,"code":21350,"language":40,"meta":41,"style":41},"class Car:\n    wheels = 4\n\n\ncar1 = Car()\ncar2 = Car()\n\ncar1.wheels = 6\n\nprint(car1.wheels)\nprint(car2.wheels)\nprint(Car.wheels)\n",[18,21352,21353,21361,21369,21373,21377,21388,21399,21403,21417,21421,21435,21450],{"__ignoreMap":41},[45,21354,21355,21357,21359],{"class":47,"line":48},[45,21356,2441],{"class":2440},[45,21358,21297],{"class":2444},[45,21360,86],{"class":85},[45,21362,21363,21365,21367],{"class":47,"line":63},[45,21364,21304],{"class":51},[45,21366,56],{"class":55},[45,21368,21309],{"class":59},[45,21370,21371],{"class":47,"line":70},[45,21372,67],{"emptyLinePlaceholder":66},[45,21374,21375],{"class":47,"line":89},[45,21376,67],{"emptyLinePlaceholder":66},[45,21378,21379,21382,21384,21386],{"class":47,"line":112},[45,21380,21381],{"class":51},"car1 ",[45,21383,56],{"class":55},[45,21385,21297],{"class":2070},[45,21387,2495],{"class":85},[45,21389,21390,21393,21395,21397],{"class":47,"line":126},[45,21391,21392],{"class":51},"car2 ",[45,21394,56],{"class":55},[45,21396,21297],{"class":2070},[45,21398,2495],{"class":85},[45,21400,21401],{"class":47,"line":142},[45,21402,67],{"emptyLinePlaceholder":66},[45,21404,21405,21408,21410,21412,21414],{"class":47,"line":149},[45,21406,21407],{"class":51},"car1",[45,21409,240],{"class":85},[45,21411,21342],{"class":3095},[45,21413,3099],{"class":55},[45,21415,21416],{"class":59}," 6\n",[45,21418,21419],{"class":47,"line":679},[45,21420,67],{"emptyLinePlaceholder":66},[45,21422,21423,21425,21427,21429,21431,21433],{"class":47,"line":686},[45,21424,2065],{"class":92},[45,21426,96],{"class":85},[45,21428,21407],{"class":2070},[45,21430,240],{"class":85},[45,21432,21342],{"class":3095},[45,21434,109],{"class":85},[45,21436,21437,21439,21441,21444,21446,21448],{"class":47,"line":2229},[45,21438,2065],{"class":92},[45,21440,96],{"class":85},[45,21442,21443],{"class":2070},"car2",[45,21445,240],{"class":85},[45,21447,21342],{"class":3095},[45,21449,109],{"class":85},[45,21451,21452,21454,21456,21458,21460,21462],{"class":47,"line":2236},[45,21453,2065],{"class":92},[45,21455,96],{"class":85},[45,21457,2588],{"class":2070},[45,21459,240],{"class":85},[45,21461,21342],{"class":3095},[45,21463,109],{"class":85},[14,21465,21466],{},[2421,21467,177],{},[36,21469,21471],{"className":38,"code":21470,"language":40,"meta":41,"style":41},"6\n4\n4\n",[18,21472,21473,21477,21481],{"__ignoreMap":41},[45,21474,21475],{"class":47,"line":48},[45,21476,11797],{"class":59},[45,21478,21479],{"class":47,"line":63},[45,21480,13681],{"class":59},[45,21482,21483],{"class":47,"line":70},[45,21484,13681],{"class":59},[14,21486,21487],{},"Important:",[195,21489,21490,21496,21501],{},[198,21491,21492,21495],{},[18,21493,21494],{},"car1.wheels = 6"," did not change the class variable",[198,21497,21498,21499],{},"it created an instance variable only on ",[18,21500,21407],{},[198,21502,21503,21505],{},[18,21504,21443],{}," still reads the value from the class",[14,21507,21508],{},"If you want to change the shared value, change it on the class:",[36,21510,21512],{"className":38,"code":21511,"language":40,"meta":41,"style":41},"class Car:\n    wheels = 4\n\n\ncar1 = Car()\ncar2 = Car()\n\nCar.wheels = 6\n\nprint(car1.wheels)\nprint(car2.wheels)\nprint(Car.wheels)\n",[18,21513,21514,21522,21530,21534,21538,21548,21558,21562,21574,21578,21592,21606],{"__ignoreMap":41},[45,21515,21516,21518,21520],{"class":47,"line":48},[45,21517,2441],{"class":2440},[45,21519,21297],{"class":2444},[45,21521,86],{"class":85},[45,21523,21524,21526,21528],{"class":47,"line":63},[45,21525,21304],{"class":51},[45,21527,56],{"class":55},[45,21529,21309],{"class":59},[45,21531,21532],{"class":47,"line":70},[45,21533,67],{"emptyLinePlaceholder":66},[45,21535,21536],{"class":47,"line":89},[45,21537,67],{"emptyLinePlaceholder":66},[45,21539,21540,21542,21544,21546],{"class":47,"line":112},[45,21541,21381],{"class":51},[45,21543,56],{"class":55},[45,21545,21297],{"class":2070},[45,21547,2495],{"class":85},[45,21549,21550,21552,21554,21556],{"class":47,"line":126},[45,21551,21392],{"class":51},[45,21553,56],{"class":55},[45,21555,21297],{"class":2070},[45,21557,2495],{"class":85},[45,21559,21560],{"class":47,"line":142},[45,21561,67],{"emptyLinePlaceholder":66},[45,21563,21564,21566,21568,21570,21572],{"class":47,"line":149},[45,21565,2588],{"class":51},[45,21567,240],{"class":85},[45,21569,21342],{"class":3095},[45,21571,3099],{"class":55},[45,21573,21416],{"class":59},[45,21575,21576],{"class":47,"line":679},[45,21577,67],{"emptyLinePlaceholder":66},[45,21579,21580,21582,21584,21586,21588,21590],{"class":47,"line":686},[45,21581,2065],{"class":92},[45,21583,96],{"class":85},[45,21585,21407],{"class":2070},[45,21587,240],{"class":85},[45,21589,21342],{"class":3095},[45,21591,109],{"class":85},[45,21593,21594,21596,21598,21600,21602,21604],{"class":47,"line":2229},[45,21595,2065],{"class":92},[45,21597,96],{"class":85},[45,21599,21443],{"class":2070},[45,21601,240],{"class":85},[45,21603,21342],{"class":3095},[45,21605,109],{"class":85},[45,21607,21608,21610,21612,21614,21616,21618],{"class":47,"line":2236},[45,21609,2065],{"class":92},[45,21611,96],{"class":85},[45,21613,2588],{"class":2070},[45,21615,240],{"class":85},[45,21617,21342],{"class":3095},[45,21619,109],{"class":85},[14,21621,21622],{},[2421,21623,177],{},[36,21625,21627],{"className":38,"code":21626,"language":40,"meta":41,"style":41},"6\n6\n6\n",[18,21628,21629,21633,21637],{"__ignoreMap":41},[45,21630,21631],{"class":47,"line":48},[45,21632,11797],{"class":59},[45,21634,21635],{"class":47,"line":63},[45,21636,11797],{"class":59},[45,21638,21639],{"class":47,"line":70},[45,21640,11797],{"class":59},[31,21642,21644],{"id":21643},"simple-rule-to-remember","Simple rule to remember",[195,21646,21647,21653],{},[198,21648,21649,21652],{},[2421,21650,21651],{},"Shared by all objects"," = class variable",[198,21654,21655,21658],{},[2421,21656,21657],{},"Unique to each object"," = instance variable",[31,21660,2012],{"id":2011},[14,21662,21663],{},"These are the most common reasons beginners mix them up:",[195,21665,21666,21669,21675,21685],{},[198,21667,21668],{},"Using a class variable for data that should be unique per object",[198,21670,21671,21672,21674],{},"Creating ",[18,21673,19460],{}," and thinking it changes the class variable",[198,21676,21677,21678,230,21681,21684],{},"Using a mutable class variable like ",[18,21679,21680],{},"[]",[18,21682,21683],{},"{}"," by mistake",[198,21686,21687],{},"Not understanding that objects can read values from the class",[31,21689,21691],{"id":21690},"useful-debugging-checks","Useful debugging checks",[14,21693,21694],{},"If you are not sure where a variable is stored, these checks can help:",[36,21696,21698],{"className":38,"code":21697,"language":40,"meta":41,"style":41},"print(obj.__dict__)\nprint(ClassName.__dict__)\nprint(obj.variable_name)\nprint(ClassName.variable_name)\nprint(hasattr(obj, 'variable_name'))\n",[18,21699,21700,21714,21729,21744,21758],{"__ignoreMap":41},[45,21701,21702,21704,21706,21708,21710,21712],{"class":47,"line":48},[45,21703,2065],{"class":92},[45,21705,96],{"class":85},[45,21707,20314],{"class":2070},[45,21709,240],{"class":85},[45,21711,4387],{"class":2771},[45,21713,109],{"class":85},[45,21715,21716,21718,21720,21723,21725,21727],{"class":47,"line":63},[45,21717,2065],{"class":92},[45,21719,96],{"class":85},[45,21721,21722],{"class":2070},"ClassName",[45,21724,240],{"class":85},[45,21726,4387],{"class":2771},[45,21728,109],{"class":85},[45,21730,21731,21733,21735,21737,21739,21742],{"class":47,"line":70},[45,21732,2065],{"class":92},[45,21734,96],{"class":85},[45,21736,20314],{"class":2070},[45,21738,240],{"class":85},[45,21740,21741],{"class":3095},"variable_name",[45,21743,109],{"class":85},[45,21745,21746,21748,21750,21752,21754,21756],{"class":47,"line":89},[45,21747,2065],{"class":92},[45,21749,96],{"class":85},[45,21751,21722],{"class":2070},[45,21753,240],{"class":85},[45,21755,21741],{"class":3095},[45,21757,109],{"class":85},[45,21759,21760,21762,21764,21767,21769,21771,21773,21775,21777,21779],{"class":47,"line":112},[45,21761,2065],{"class":92},[45,21763,96],{"class":85},[45,21765,21766],{"class":92},"hasattr",[45,21768,96],{"class":85},[45,21770,20314],{"class":2070},[45,21772,3079],{"class":85},[45,21774,9199],{"class":99},[45,21776,21741],{"class":103},[45,21778,7901],{"class":99},[45,21780,4348],{"class":85},[14,21782,21783],{},"What they do:",[195,21785,21786,21792,21798,21803,21809],{},[198,21787,21788,21791],{},[18,21789,21790],{},"obj.__dict__"," shows the instance variables on that object",[198,21793,21794,21797],{},[18,21795,21796],{},"ClassName.__dict__"," shows attributes on the class",[198,21799,21800,21802],{},[18,21801,20239],{}," shows what Python finds when looking through the object",[198,21804,21805,21808],{},[18,21806,21807],{},"ClassName.variable_name"," reads directly from the class",[198,21810,21811,21814],{},[18,21812,21813],{},"hasattr(obj, 'variable_name')"," checks whether the object can access that name",[14,21816,466],{},[36,21818,21820],{"className":38,"code":21819,"language":40,"meta":41,"style":41},"class Dog:\n    species = \"Canis familiaris\"\n\n    def __init__(self, name):\n        self.name = name\n\n\ndog = Dog(\"Max\")\n\nprint(dog.__dict__)\nprint(Dog.__dict__['species'])\n",[18,21821,21822,21830,21842,21846,21862,21874,21878,21882,21901,21905,21919],{"__ignoreMap":41},[45,21823,21824,21826,21828],{"class":47,"line":48},[45,21825,2441],{"class":2440},[45,21827,2445],{"class":2444},[45,21829,86],{"class":85},[45,21831,21832,21834,21836,21838,21840],{"class":47,"line":63},[45,21833,19235],{"class":51},[45,21835,56],{"class":55},[45,21837,1565],{"class":99},[45,21839,19242],{"class":103},[45,21841,1571],{"class":99},[45,21843,21844],{"class":47,"line":70},[45,21845,67],{"emptyLinePlaceholder":66},[45,21847,21848,21850,21852,21854,21856,21858,21860],{"class":47,"line":89},[45,21849,2452],{"class":2440},[45,21851,3072],{"class":92},[45,21853,96],{"class":85},[45,21855,2427],{"class":2461},[45,21857,3079],{"class":85},[45,21859,3083],{"class":3082},[45,21861,2464],{"class":85},[45,21863,21864,21866,21868,21870,21872],{"class":47,"line":112},[45,21865,3090],{"class":2771},[45,21867,240],{"class":85},[45,21869,3096],{"class":3095},[45,21871,3099],{"class":55},[45,21873,3102],{"class":51},[45,21875,21876],{"class":47,"line":126},[45,21877,67],{"emptyLinePlaceholder":66},[45,21879,21880],{"class":47,"line":142},[45,21881,67],{"emptyLinePlaceholder":66},[45,21883,21884,21887,21889,21891,21893,21895,21897,21899],{"class":47,"line":149},[45,21885,21886],{"class":51},"dog ",[45,21888,56],{"class":55},[45,21890,2445],{"class":2070},[45,21892,96],{"class":85},[45,21894,100],{"class":99},[45,21896,19306],{"class":103},[45,21898,100],{"class":99},[45,21900,109],{"class":85},[45,21902,21903],{"class":47,"line":679},[45,21904,67],{"emptyLinePlaceholder":66},[45,21906,21907,21909,21911,21913,21915,21917],{"class":47,"line":686},[45,21908,2065],{"class":92},[45,21910,96],{"class":85},[45,21912,9105],{"class":2070},[45,21914,240],{"class":85},[45,21916,4387],{"class":2771},[45,21918,109],{"class":85},[45,21920,21921,21923,21925,21927,21929,21931,21933,21935,21937,21939],{"class":47,"line":2229},[45,21922,2065],{"class":92},[45,21924,96],{"class":85},[45,21926,2570],{"class":2070},[45,21928,240],{"class":85},[45,21930,4387],{"class":2771},[45,21932,9680],{"class":85},[45,21934,7901],{"class":99},[45,21936,19347],{"class":103},[45,21938,7901],{"class":99},[45,21940,21941],{"class":85},"])\n",[14,21943,21944],{},[2421,21945,177],{},[36,21947,21949],{"className":38,"code":21948,"language":40,"meta":41,"style":41},"{'name': 'Max'}\nCanis familiaris\n",[18,21950,21951,21971],{"__ignoreMap":41},[45,21952,21953,21955,21957,21959,21961,21963,21965,21967,21969],{"class":47,"line":48},[45,21954,5551],{"class":85},[45,21956,7901],{"class":99},[45,21958,3096],{"class":103},[45,21960,7901],{"class":99},[45,21962,457],{"class":85},[45,21964,9199],{"class":99},[45,21966,19306],{"class":103},[45,21968,7901],{"class":99},[45,21970,8629],{"class":85},[45,21972,21973],{"class":47,"line":63},[45,21974,19406],{"class":51},[14,21976,21977],{},"This shows:",[195,21979,21980,21985],{},[198,21981,21982,21984],{},[18,21983,3096],{}," is stored on the object",[198,21986,21987,21989],{},[18,21988,19347],{}," is stored on the class",[31,21991,2281],{"id":2280},[712,21993,21995],{"id":21994},"can-an-object-access-a-class-variable","Can an object access a class variable?",[14,21997,21998],{},"Yes. If the object does not have an instance variable with that name, Python reads the value from the class.",[712,22000,22002],{"id":22001},"can-a-class-variable-be-changed","Can a class variable be changed?",[14,22004,22005],{},"Yes. Change it on the class when you want the shared value to update for all objects.",[712,22007,22009],{"id":22008},"why-did-changing-a-class-variable-on-one-object-only-affect-that-object","Why did changing a class variable on one object only affect that object?",[14,22011,22012],{},"You likely created a new instance variable with the same name instead of changing the variable on the class.",[712,22014,22016],{"id":22015},"should-i-put-lists-and-dictionaries-in-class-variables","Should I put lists and dictionaries in class variables?",[14,22018,22019],{},"Only if you want all objects to share the same list or dictionary. Otherwise use an instance variable.",[31,22021,2342],{"id":2341},[195,22023,22024,22028,22034,22040,22045,22050],{},[198,22025,22026],{},[236,22027,2560],{"href":19945},[198,22029,22030],{},[236,22031,1732,22032,3270],{"href":19467},[18,22033,3262],{},[198,22035,22036],{},[236,22037,22039],{"href":22038},"\u002Flearn\u002Fbasic-methods-in-python-classes-explained\u002F","Basic methods in Python classes explained",[198,22041,22042],{},[236,22043,22044],{"href":21277},"Mutability in Python explained: mutable vs immutable types",[198,22046,22047],{},[236,22048,22049],{"href":19948},"What is a class in Python",[198,22051,22052],{},[236,22053,22054],{"href":20479},"What is an object in Python",[2367,22056,22057],{},"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 .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 .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 .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}",{"title":41,"searchDepth":63,"depth":63,"links":22059},[22060,22061,22062,22063,22064,22065,22066,22067,22068,22069,22070,22071,22072,22073,22079],{"id":33,"depth":63,"text":34},{"id":6599,"depth":63,"text":6600},{"id":19447,"depth":63,"text":19448},{"id":19728,"depth":63,"text":19729},{"id":19952,"depth":63,"text":19953},{"id":20232,"depth":63,"text":20233},{"id":20483,"depth":63,"text":20484},{"id":20690,"depth":63,"text":20691},{"id":20881,"depth":63,"text":20882},{"id":21281,"depth":63,"text":21282},{"id":21643,"depth":63,"text":21644},{"id":2011,"depth":63,"text":2012},{"id":21690,"depth":63,"text":21691},{"id":2280,"depth":63,"text":2281,"children":22074},[22075,22076,22077,22078],{"id":21994,"depth":70,"text":21995},{"id":22001,"depth":70,"text":22002},{"id":22008,"depth":70,"text":22009},{"id":22015,"depth":70,"text":22016},{"id":2341,"depth":63,"text":2342},"Master instance vs class variables in python in our comprehensive Python beginner guide.",{},{"title":19167,"description":22080},"learn\u002Finstance-vs-class-variables-in-python","iwFZzwYDLfJsQPvll2TOmU--6Wr6Ps_hL37w_7uSV6A",{"id":22086,"title":22087,"body":22088,"description":24909,"extension":2399,"meta":24910,"navigation":66,"path":14754,"seo":24911,"stem":24912,"__hash__":24913},"content\u002Flearn\u002Fiterators-and-iterable-objects-explained.md","Iterators and Iterable Objects Explained",{"type":7,"value":22089,"toc":24870},[22090,22093,22103,22106,22123,22126,22138,22140,22237,22246,22248,22273,22277,22284,22287,22309,22311,22381,22383,22403,22409,22415,22480,22486,22493,22497,22502,22508,22510,22595,22597,22615,22620,22722,22725,22734,22741,22745,22748,22760,22762,22837,22839,22852,22855,22858,22945,22947,22960,22963,22969,23035,23037,23045,23052,23055,23061,23066,23068,23145,23147,23167,23173,23178,23183,23261,23267,23276,23364,23366,23389,23395,23401,23406,23425,23428,23479,23482,23580,23583,23599,23605,23612,23616,23619,23623,23719,23722,23755,23757,23775,23779,23782,23851,23854,23905,23910,23915,23946,23948,23966,23973,23977,23980,24049,24052,24056,24059,24062,24076,24084,24088,24090,24096,24098,24137,24143,24200,24204,24211,24214,24253,24255,24263,24267,24270,24364,24367,24452,24462,24467,24577,24579,24597,24601,24604,24691,24694,24765,24767,24793,24795,24799,24805,24809,24818,24822,24825,24829,24835,24837,24864,24867],[10,22091,22087],{"id":22092},"iterators-and-iterable-objects-explained",[14,22094,19451,22095,22098,22099,22102],{},[2421,22096,22097],{},"iterable"," is something you can loop over. An ",[2421,22100,22101],{},"iterator"," is the object that gives you values one at a time during that loop.",[14,22104,22105],{},"This difference is important because it helps you understand:",[195,22107,22108,22114,22120],{},[198,22109,22110,22111,22113],{},"how ",[18,22112,7895],{}," loops work",[198,22115,22116,22117,22119],{},"why ",[18,22118,13691],{}," works on some objects but not others",[198,22121,22122],{},"why some objects get \"used up\" after you loop through them",[14,22124,22125],{},"A good short way to remember it is:",[195,22127,22128,22133],{},[198,22129,22130,22132],{},[2421,22131,22097],{}," = can be looped over",[198,22134,22135,22137],{},[2421,22136,22101],{}," = produces the next value",[31,22139,34],{"id":33},[36,22141,22143],{"className":38,"code":22142,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\niterator = iter(numbers)\n\nprint(next(iterator))  # 10\nprint(next(iterator))  # 20\nprint(next(iterator))  # 30\n",[18,22144,22145,22165,22181,22185,22203,22220],{"__ignoreMap":41},[45,22146,22147,22149,22151,22153,22155,22157,22159,22161,22163],{"class":47,"line":48},[45,22148,8570],{"class":51},[45,22150,56],{"class":55},[45,22152,8575],{"class":85},[45,22154,11786],{"class":59},[45,22156,3079],{"class":85},[45,22158,10503],{"class":59},[45,22160,3079],{"class":85},[45,22162,10512],{"class":59},[45,22164,8594],{"class":85},[45,22166,22167,22170,22172,22175,22177,22179],{"class":47,"line":63},[45,22168,22169],{"class":51},"iterator ",[45,22171,56],{"class":55},[45,22173,22174],{"class":92}," iter",[45,22176,96],{"class":85},[45,22178,9069],{"class":2070},[45,22180,109],{"class":85},[45,22182,22183],{"class":47,"line":70},[45,22184,67],{"emptyLinePlaceholder":66},[45,22186,22187,22189,22191,22193,22195,22197,22200],{"class":47,"line":89},[45,22188,2065],{"class":92},[45,22190,96],{"class":85},[45,22192,13224],{"class":92},[45,22194,96],{"class":85},[45,22196,22101],{"class":2070},[45,22198,22199],{"class":85},"))",[45,22201,22202],{"class":7624},"  # 10\n",[45,22204,22205,22207,22209,22211,22213,22215,22217],{"class":47,"line":112},[45,22206,2065],{"class":92},[45,22208,96],{"class":85},[45,22210,13224],{"class":92},[45,22212,96],{"class":85},[45,22214,22101],{"class":2070},[45,22216,22199],{"class":85},[45,22218,22219],{"class":7624},"  # 20\n",[45,22221,22222,22224,22226,22228,22230,22232,22234],{"class":47,"line":126},[45,22223,2065],{"class":92},[45,22225,96],{"class":85},[45,22227,13224],{"class":92},[45,22229,96],{"class":85},[45,22231,22101],{"class":2070},[45,22233,22199],{"class":85},[45,22235,22236],{"class":7624},"  # 30\n",[14,22238,165,22239,22242,22243,22245],{},[18,22240,22241],{},"iter()"," to get an iterator from an iterable like a list. Use ",[18,22244,13691],{}," to read one item at a time.",[31,22247,2530],{"id":2529},[195,22249,22250,22253,22256,22263,22268],{},[198,22251,22252],{},"What an iterable is",[198,22254,22255],{},"What an iterator is",[198,22257,13092,22258,1316,22260,22262],{},[18,22259,22241],{},[18,22261,13691],{}," work",[198,22264,13092,22265,22267],{},[18,22266,7895],{}," loops use iterators behind the scenes",[198,22269,5754,22270,22272],{},[18,22271,13833],{}," means",[31,22274,22276],{"id":22275},"what-is-an-iterable","What is an iterable?",[14,22278,19451,22279,22281,22282,13576],{},[2421,22280,22097],{}," is an object you can loop over with a ",[18,22283,7895],{},[14,22285,22286],{},"Common iterable objects include:",[195,22288,22289,22291,22294,22297,22299,22301,22306],{},[198,22290,20890],{},[198,22292,22293],{},"tuples",[198,22295,22296],{},"strings",[198,22298,20893],{},[198,22300,20896],{},[198,22302,22303,19922],{},[18,22304,22305],{},"range()",[198,22307,22308],{},"files opened for reading",[14,22310,466],{},[36,22312,22314],{"className":38,"code":22313,"language":40,"meta":41,"style":41},"colors = [\"red\", \"green\", \"blue\"]\n\nfor color in colors:\n    print(color)\n",[18,22315,22316,22352,22356,22370],{"__ignoreMap":41},[45,22317,22318,22321,22323,22325,22327,22330,22332,22334,22336,22339,22341,22343,22345,22348,22350],{"class":47,"line":48},[45,22319,22320],{"class":51},"colors ",[45,22322,56],{"class":55},[45,22324,8575],{"class":85},[45,22326,100],{"class":99},[45,22328,22329],{"class":103},"red",[45,22331,100],{"class":99},[45,22333,3079],{"class":85},[45,22335,1565],{"class":99},[45,22337,22338],{"class":103},"green",[45,22340,100],{"class":99},[45,22342,3079],{"class":85},[45,22344,1565],{"class":99},[45,22346,22347],{"class":103},"blue",[45,22349,100],{"class":99},[45,22351,8594],{"class":85},[45,22353,22354],{"class":47,"line":63},[45,22355,67],{"emptyLinePlaceholder":66},[45,22357,22358,22360,22363,22365,22368],{"class":47,"line":70},[45,22359,7895],{"class":73},[45,22361,22362],{"class":51}," color ",[45,22364,8623],{"class":73},[45,22366,22367],{"class":51}," colors",[45,22369,86],{"class":85},[45,22371,22372,22374,22376,22379],{"class":47,"line":89},[45,22373,93],{"class":92},[45,22375,96],{"class":85},[45,22377,22378],{"class":2070},"color",[45,22380,109],{"class":85},[14,22382,177],{},[36,22384,22386],{"className":38,"code":22385,"language":40,"meta":41,"style":41},"red\ngreen\nblue\n",[18,22387,22388,22393,22398],{"__ignoreMap":41},[45,22389,22390],{"class":47,"line":48},[45,22391,22392],{"class":51},"red\n",[45,22394,22395],{"class":47,"line":63},[45,22396,22397],{"class":51},"green\n",[45,22399,22400],{"class":47,"line":70},[45,22401,22402],{"class":51},"blue\n",[14,22404,3444,22405,22408],{},[18,22406,22407],{},"colors"," is iterable because Python can go through its values one by one.",[14,22410,22411,22412,22414],{},"You can also pass an iterable to ",[18,22413,22241],{}," to get an iterator:",[36,22416,22418],{"className":38,"code":22417,"language":40,"meta":41,"style":41},"colors = [\"red\", \"green\", \"blue\"]\niterator = iter(colors)\n\nprint(iterator)\n",[18,22419,22420,22452,22466,22470],{"__ignoreMap":41},[45,22421,22422,22424,22426,22428,22430,22432,22434,22436,22438,22440,22442,22444,22446,22448,22450],{"class":47,"line":48},[45,22423,22320],{"class":51},[45,22425,56],{"class":55},[45,22427,8575],{"class":85},[45,22429,100],{"class":99},[45,22431,22329],{"class":103},[45,22433,100],{"class":99},[45,22435,3079],{"class":85},[45,22437,1565],{"class":99},[45,22439,22338],{"class":103},[45,22441,100],{"class":99},[45,22443,3079],{"class":85},[45,22445,1565],{"class":99},[45,22447,22347],{"class":103},[45,22449,100],{"class":99},[45,22451,8594],{"class":85},[45,22453,22454,22456,22458,22460,22462,22464],{"class":47,"line":63},[45,22455,22169],{"class":51},[45,22457,56],{"class":55},[45,22459,22174],{"class":92},[45,22461,96],{"class":85},[45,22463,22407],{"class":2070},[45,22465,109],{"class":85},[45,22467,22468],{"class":47,"line":70},[45,22469,67],{"emptyLinePlaceholder":66},[45,22471,22472,22474,22476,22478],{"class":47,"line":89},[45,22473,2065],{"class":92},[45,22475,96],{"class":85},[45,22477,22101],{"class":2070},[45,22479,109],{"class":85},[14,22481,22482,22483,22485],{},"The exact printed result may look different, but ",[18,22484,22101],{}," is now an iterator object.",[14,22487,22488,22489,240],{},"If you want a fuller beginner definition, see ",[236,22490,22492],{"href":22491},"\u002Fglossary\u002Fwhat-is-an-iterable-in-python\u002F","what is an iterable in Python",[31,22494,22496],{"id":22495},"what-is-an-iterator","What is an iterator?",[14,22498,19451,22499,22501],{},[2421,22500,22101],{}," is an object that gives values one at a time.",[14,22503,22504,22505,22507],{},"It keeps track of where it is. Each time you call ",[18,22506,13691],{},", it returns the next item.",[14,22509,466],{},[36,22511,22513],{"className":38,"code":22512,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\niterator = iter(numbers)\n\nprint(next(iterator))\nprint(next(iterator))\nprint(next(iterator))\n",[18,22514,22515,22535,22549,22553,22567,22581],{"__ignoreMap":41},[45,22516,22517,22519,22521,22523,22525,22527,22529,22531,22533],{"class":47,"line":48},[45,22518,8570],{"class":51},[45,22520,56],{"class":55},[45,22522,8575],{"class":85},[45,22524,11786],{"class":59},[45,22526,3079],{"class":85},[45,22528,10503],{"class":59},[45,22530,3079],{"class":85},[45,22532,10512],{"class":59},[45,22534,8594],{"class":85},[45,22536,22537,22539,22541,22543,22545,22547],{"class":47,"line":63},[45,22538,22169],{"class":51},[45,22540,56],{"class":55},[45,22542,22174],{"class":92},[45,22544,96],{"class":85},[45,22546,9069],{"class":2070},[45,22548,109],{"class":85},[45,22550,22551],{"class":47,"line":70},[45,22552,67],{"emptyLinePlaceholder":66},[45,22554,22555,22557,22559,22561,22563,22565],{"class":47,"line":89},[45,22556,2065],{"class":92},[45,22558,96],{"class":85},[45,22560,13224],{"class":92},[45,22562,96],{"class":85},[45,22564,22101],{"class":2070},[45,22566,4348],{"class":85},[45,22568,22569,22571,22573,22575,22577,22579],{"class":47,"line":112},[45,22570,2065],{"class":92},[45,22572,96],{"class":85},[45,22574,13224],{"class":92},[45,22576,96],{"class":85},[45,22578,22101],{"class":2070},[45,22580,4348],{"class":85},[45,22582,22583,22585,22587,22589,22591,22593],{"class":47,"line":126},[45,22584,2065],{"class":92},[45,22586,96],{"class":85},[45,22588,13224],{"class":92},[45,22590,96],{"class":85},[45,22592,22101],{"class":2070},[45,22594,4348],{"class":85},[14,22596,177],{},[36,22598,22600],{"className":38,"code":22599,"language":40,"meta":41,"style":41},"10\n20\n30\n",[18,22601,22602,22606,22610],{"__ignoreMap":41},[45,22603,22604],{"class":47,"line":48},[45,22605,13262],{"class":59},[45,22607,22608],{"class":47,"line":63},[45,22609,3436],{"class":59},[45,22611,22612],{"class":47,"line":70},[45,22613,22614],{"class":59},"30\n",[14,22616,22617,22618,240],{},"After the last value, there is nothing left to return. At that point, Python raises ",[18,22619,13833],{},[36,22621,22623],{"className":38,"code":22622,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\niterator = iter(numbers)\n\nprint(next(iterator))\nprint(next(iterator))\nprint(next(iterator))\nprint(next(iterator))  # no more items\n",[18,22624,22625,22645,22659,22663,22677,22691,22705],{"__ignoreMap":41},[45,22626,22627,22629,22631,22633,22635,22637,22639,22641,22643],{"class":47,"line":48},[45,22628,8570],{"class":51},[45,22630,56],{"class":55},[45,22632,8575],{"class":85},[45,22634,11786],{"class":59},[45,22636,3079],{"class":85},[45,22638,10503],{"class":59},[45,22640,3079],{"class":85},[45,22642,10512],{"class":59},[45,22644,8594],{"class":85},[45,22646,22647,22649,22651,22653,22655,22657],{"class":47,"line":63},[45,22648,22169],{"class":51},[45,22650,56],{"class":55},[45,22652,22174],{"class":92},[45,22654,96],{"class":85},[45,22656,9069],{"class":2070},[45,22658,109],{"class":85},[45,22660,22661],{"class":47,"line":70},[45,22662,67],{"emptyLinePlaceholder":66},[45,22664,22665,22667,22669,22671,22673,22675],{"class":47,"line":89},[45,22666,2065],{"class":92},[45,22668,96],{"class":85},[45,22670,13224],{"class":92},[45,22672,96],{"class":85},[45,22674,22101],{"class":2070},[45,22676,4348],{"class":85},[45,22678,22679,22681,22683,22685,22687,22689],{"class":47,"line":112},[45,22680,2065],{"class":92},[45,22682,96],{"class":85},[45,22684,13224],{"class":92},[45,22686,96],{"class":85},[45,22688,22101],{"class":2070},[45,22690,4348],{"class":85},[45,22692,22693,22695,22697,22699,22701,22703],{"class":47,"line":126},[45,22694,2065],{"class":92},[45,22696,96],{"class":85},[45,22698,13224],{"class":92},[45,22700,96],{"class":85},[45,22702,22101],{"class":2070},[45,22704,4348],{"class":85},[45,22706,22707,22709,22711,22713,22715,22717,22719],{"class":47,"line":142},[45,22708,2065],{"class":92},[45,22710,96],{"class":85},[45,22712,13224],{"class":92},[45,22714,96],{"class":85},[45,22716,22101],{"class":2070},[45,22718,22199],{"class":85},[45,22720,22721],{"class":7624},"  # no more items\n",[14,22723,22724],{},"This causes:",[36,22726,22728],{"className":38,"code":22727,"language":40,"meta":41,"style":41},"StopIteration\n",[18,22729,22730],{"__ignoreMap":41},[45,22731,22732],{"class":47,"line":48},[45,22733,22727],{"class":2109},[14,22735,22736,22737,240],{},"If you want a short definition page, see ",[236,22738,22740],{"href":22739},"\u002Fglossary\u002Fwhat-is-an-iterator-in-python\u002F","what is an iterator in Python",[31,22742,22744],{"id":22743},"iterable-vs-iterator","Iterable vs iterator",[14,22746,22747],{},"These two terms are related, but they are not the same.",[195,22749,22750,22755],{},[198,22751,19451,22752,22754],{},[2421,22753,22097],{}," is the object you can loop over",[198,22756,19451,22757,22759],{},[2421,22758,22101],{}," is the object that produces items during the loop",[14,22761,2115],{},[36,22763,22765],{"className":38,"code":22764,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\n\nprint(hasattr(numbers, \"__iter__\"))\nprint(hasattr(numbers, \"__next__\"))\n",[18,22766,22767,22787,22791,22814],{"__ignoreMap":41},[45,22768,22769,22771,22773,22775,22777,22779,22781,22783,22785],{"class":47,"line":48},[45,22770,8570],{"class":51},[45,22772,56],{"class":55},[45,22774,8575],{"class":85},[45,22776,8578],{"class":59},[45,22778,3079],{"class":85},[45,22780,3754],{"class":59},[45,22782,3079],{"class":85},[45,22784,777],{"class":59},[45,22786,8594],{"class":85},[45,22788,22789],{"class":47,"line":63},[45,22790,67],{"emptyLinePlaceholder":66},[45,22792,22793,22795,22797,22799,22801,22803,22805,22807,22810,22812],{"class":47,"line":70},[45,22794,2065],{"class":92},[45,22796,96],{"class":85},[45,22798,21766],{"class":92},[45,22800,96],{"class":85},[45,22802,9069],{"class":2070},[45,22804,3079],{"class":85},[45,22806,1565],{"class":99},[45,22808,22809],{"class":103},"__iter__",[45,22811,100],{"class":99},[45,22813,4348],{"class":85},[45,22815,22816,22818,22820,22822,22824,22826,22828,22830,22833,22835],{"class":47,"line":89},[45,22817,2065],{"class":92},[45,22819,96],{"class":85},[45,22821,21766],{"class":92},[45,22823,96],{"class":85},[45,22825,9069],{"class":2070},[45,22827,3079],{"class":85},[45,22829,1565],{"class":99},[45,22831,22832],{"class":103},"__next__",[45,22834,100],{"class":99},[45,22836,4348],{"class":85},[14,22838,177],{},[36,22840,22842],{"className":38,"code":22841,"language":40,"meta":41,"style":41},"True\nFalse\n",[18,22843,22844,22848],{"__ignoreMap":41},[45,22845,22846],{"class":47,"line":48},[45,22847,2270],{"class":481},[45,22849,22850],{"class":47,"line":63},[45,22851,2265],{"class":481},[14,22853,22854],{},"A list is iterable, but it is not usually an iterator.",[14,22856,22857],{},"Now look at an iterator created from that list:",[36,22859,22861],{"className":38,"code":22860,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\niterator = iter(numbers)\n\nprint(hasattr(iterator, \"__iter__\"))\nprint(hasattr(iterator, \"__next__\"))\n",[18,22862,22863,22883,22897,22901,22923],{"__ignoreMap":41},[45,22864,22865,22867,22869,22871,22873,22875,22877,22879,22881],{"class":47,"line":48},[45,22866,8570],{"class":51},[45,22868,56],{"class":55},[45,22870,8575],{"class":85},[45,22872,8578],{"class":59},[45,22874,3079],{"class":85},[45,22876,3754],{"class":59},[45,22878,3079],{"class":85},[45,22880,777],{"class":59},[45,22882,8594],{"class":85},[45,22884,22885,22887,22889,22891,22893,22895],{"class":47,"line":63},[45,22886,22169],{"class":51},[45,22888,56],{"class":55},[45,22890,22174],{"class":92},[45,22892,96],{"class":85},[45,22894,9069],{"class":2070},[45,22896,109],{"class":85},[45,22898,22899],{"class":47,"line":70},[45,22900,67],{"emptyLinePlaceholder":66},[45,22902,22903,22905,22907,22909,22911,22913,22915,22917,22919,22921],{"class":47,"line":89},[45,22904,2065],{"class":92},[45,22906,96],{"class":85},[45,22908,21766],{"class":92},[45,22910,96],{"class":85},[45,22912,22101],{"class":2070},[45,22914,3079],{"class":85},[45,22916,1565],{"class":99},[45,22918,22809],{"class":103},[45,22920,100],{"class":99},[45,22922,4348],{"class":85},[45,22924,22925,22927,22929,22931,22933,22935,22937,22939,22941,22943],{"class":47,"line":112},[45,22926,2065],{"class":92},[45,22928,96],{"class":85},[45,22930,21766],{"class":92},[45,22932,96],{"class":85},[45,22934,22101],{"class":2070},[45,22936,3079],{"class":85},[45,22938,1565],{"class":99},[45,22940,22832],{"class":103},[45,22942,100],{"class":99},[45,22944,4348],{"class":85},[14,22946,177],{},[36,22948,22950],{"className":38,"code":22949,"language":40,"meta":41,"style":41},"True\nTrue\n",[18,22951,22952,22956],{"__ignoreMap":41},[45,22953,22954],{"class":47,"line":48},[45,22955,2270],{"class":481},[45,22957,22958],{"class":47,"line":63},[45,22959,2270],{"class":481},[14,22961,22962],{},"This shows that the iterator can both be iterated over and provide the next value.",[14,22964,22965,22966,22968],{},"Also, calling ",[18,22967,22241],{}," on an iterator usually returns the same iterator:",[36,22970,22972],{"className":38,"code":22971,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\niterator = iter(numbers)\n\nprint(iter(iterator) is iterator)\n",[18,22973,22974,22994,23008,23012],{"__ignoreMap":41},[45,22975,22976,22978,22980,22982,22984,22986,22988,22990,22992],{"class":47,"line":48},[45,22977,8570],{"class":51},[45,22979,56],{"class":55},[45,22981,8575],{"class":85},[45,22983,8578],{"class":59},[45,22985,3079],{"class":85},[45,22987,3754],{"class":59},[45,22989,3079],{"class":85},[45,22991,777],{"class":59},[45,22993,8594],{"class":85},[45,22995,22996,22998,23000,23002,23004,23006],{"class":47,"line":63},[45,22997,22169],{"class":51},[45,22999,56],{"class":55},[45,23001,22174],{"class":92},[45,23003,96],{"class":85},[45,23005,9069],{"class":2070},[45,23007,109],{"class":85},[45,23009,23010],{"class":47,"line":70},[45,23011,67],{"emptyLinePlaceholder":66},[45,23013,23014,23016,23018,23021,23023,23025,23027,23030,23033],{"class":47,"line":89},[45,23015,2065],{"class":92},[45,23017,96],{"class":85},[45,23019,23020],{"class":92},"iter",[45,23022,96],{"class":85},[45,23024,22101],{"class":2070},[45,23026,9146],{"class":85},[45,23028,23029],{"class":73}," is",[45,23031,23032],{"class":2070}," iterator",[45,23034,109],{"class":85},[14,23036,177],{},[36,23038,23039],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,23040,23041],{"__ignoreMap":41},[45,23042,23043],{"class":47,"line":48},[45,23044,2270],{"class":481},[31,23046,13092,23048,1316,23050,22262],{"id":23047},"how-iter-and-next-work",[18,23049,22241],{},[18,23051,13691],{},[14,23053,23054],{},"These two functions are the core of Python's iteration system.",[712,23056,23058],{"id":23057},"iterobject",[18,23059,23060],{},"iter(object)",[14,23062,23063,23065],{},[18,23064,23060],{}," asks Python for an iterator for that object.",[14,23067,466],{},[36,23069,23071],{"className":38,"code":23070,"language":40,"meta":41,"style":41},"text = \"abc\"\niterator = iter(text)\n\nprint(next(iterator))\nprint(next(iterator))\nprint(next(iterator))\n",[18,23072,23073,23085,23099,23103,23117,23131],{"__ignoreMap":41},[45,23074,23075,23077,23079,23081,23083],{"class":47,"line":48},[45,23076,10297],{"class":51},[45,23078,56],{"class":55},[45,23080,1565],{"class":99},[45,23082,10304],{"class":103},[45,23084,1571],{"class":99},[45,23086,23087,23089,23091,23093,23095,23097],{"class":47,"line":63},[45,23088,22169],{"class":51},[45,23090,56],{"class":55},[45,23092,22174],{"class":92},[45,23094,96],{"class":85},[45,23096,5000],{"class":2070},[45,23098,109],{"class":85},[45,23100,23101],{"class":47,"line":70},[45,23102,67],{"emptyLinePlaceholder":66},[45,23104,23105,23107,23109,23111,23113,23115],{"class":47,"line":89},[45,23106,2065],{"class":92},[45,23108,96],{"class":85},[45,23110,13224],{"class":92},[45,23112,96],{"class":85},[45,23114,22101],{"class":2070},[45,23116,4348],{"class":85},[45,23118,23119,23121,23123,23125,23127,23129],{"class":47,"line":112},[45,23120,2065],{"class":92},[45,23122,96],{"class":85},[45,23124,13224],{"class":92},[45,23126,96],{"class":85},[45,23128,22101],{"class":2070},[45,23130,4348],{"class":85},[45,23132,23133,23135,23137,23139,23141,23143],{"class":47,"line":126},[45,23134,2065],{"class":92},[45,23136,96],{"class":85},[45,23138,13224],{"class":92},[45,23140,96],{"class":85},[45,23142,22101],{"class":2070},[45,23144,4348],{"class":85},[14,23146,177],{},[36,23148,23150],{"className":38,"code":23149,"language":40,"meta":41,"style":41},"a\nb\nc\n",[18,23151,23152,23157,23162],{"__ignoreMap":41},[45,23153,23154],{"class":47,"line":48},[45,23155,23156],{"class":51},"a\n",[45,23158,23159],{"class":47,"line":63},[45,23160,23161],{"class":51},"b\n",[45,23163,23164],{"class":47,"line":70},[45,23165,23166],{"class":51},"c\n",[712,23168,23170],{"id":23169},"nextiterator",[18,23171,23172],{},"next(iterator)",[14,23174,23175,23177],{},[18,23176,23172],{}," gets the next item from the iterator.",[14,23179,23180,23181,240],{},"If there are no items left, Python raises ",[18,23182,13833],{},[36,23184,23186],{"className":38,"code":23185,"language":40,"meta":41,"style":41},"text = \"ab\"\niterator = iter(text)\n\nprint(next(iterator))\nprint(next(iterator))\nprint(next(iterator))\n",[18,23187,23188,23201,23215,23219,23233,23247],{"__ignoreMap":41},[45,23189,23190,23192,23194,23196,23199],{"class":47,"line":48},[45,23191,10297],{"class":51},[45,23193,56],{"class":55},[45,23195,1565],{"class":99},[45,23197,23198],{"class":103},"ab",[45,23200,1571],{"class":99},[45,23202,23203,23205,23207,23209,23211,23213],{"class":47,"line":63},[45,23204,22169],{"class":51},[45,23206,56],{"class":55},[45,23208,22174],{"class":92},[45,23210,96],{"class":85},[45,23212,5000],{"class":2070},[45,23214,109],{"class":85},[45,23216,23217],{"class":47,"line":70},[45,23218,67],{"emptyLinePlaceholder":66},[45,23220,23221,23223,23225,23227,23229,23231],{"class":47,"line":89},[45,23222,2065],{"class":92},[45,23224,96],{"class":85},[45,23226,13224],{"class":92},[45,23228,96],{"class":85},[45,23230,22101],{"class":2070},[45,23232,4348],{"class":85},[45,23234,23235,23237,23239,23241,23243,23245],{"class":47,"line":112},[45,23236,2065],{"class":92},[45,23238,96],{"class":85},[45,23240,13224],{"class":92},[45,23242,96],{"class":85},[45,23244,22101],{"class":2070},[45,23246,4348],{"class":85},[45,23248,23249,23251,23253,23255,23257,23259],{"class":47,"line":126},[45,23250,2065],{"class":92},[45,23252,96],{"class":85},[45,23254,13224],{"class":92},[45,23256,96],{"class":85},[45,23258,22101],{"class":2070},[45,23260,4348],{"class":85},[14,23262,23263,23264,23266],{},"This raises ",[18,23265,13833],{}," on the third call.",[14,23268,23269,23270,1316,23273,457],{},"You can handle that yourself with ",[18,23271,23272],{},"try",[18,23274,23275],{},"except",[36,23277,23279],{"className":38,"code":23278,"language":40,"meta":41,"style":41},"text = \"ab\"\niterator = iter(text)\n\ntry:\n    while True:\n        print(next(iterator))\nexcept StopIteration:\n    print(\"Iterator is finished\")\n",[18,23280,23281,23293,23307,23311,23317,23326,23340,23349],{"__ignoreMap":41},[45,23282,23283,23285,23287,23289,23291],{"class":47,"line":48},[45,23284,10297],{"class":51},[45,23286,56],{"class":55},[45,23288,1565],{"class":99},[45,23290,23198],{"class":103},[45,23292,1571],{"class":99},[45,23294,23295,23297,23299,23301,23303,23305],{"class":47,"line":63},[45,23296,22169],{"class":51},[45,23298,56],{"class":55},[45,23300,22174],{"class":92},[45,23302,96],{"class":85},[45,23304,5000],{"class":2070},[45,23306,109],{"class":85},[45,23308,23309],{"class":47,"line":70},[45,23310,67],{"emptyLinePlaceholder":66},[45,23312,23313,23315],{"class":47,"line":89},[45,23314,23272],{"class":73},[45,23316,86],{"class":85},[45,23318,23319,23321,23324],{"class":47,"line":112},[45,23320,12967],{"class":73},[45,23322,23323],{"class":481}," True",[45,23325,86],{"class":85},[45,23327,23328,23330,23332,23334,23336,23338],{"class":47,"line":126},[45,23329,1923],{"class":92},[45,23331,96],{"class":85},[45,23333,13224],{"class":92},[45,23335,96],{"class":85},[45,23337,22101],{"class":2070},[45,23339,4348],{"class":85},[45,23341,23342,23344,23347],{"class":47,"line":142},[45,23343,23275],{"class":73},[45,23345,23346],{"class":2109}," StopIteration",[45,23348,86],{"class":85},[45,23350,23351,23353,23355,23357,23360,23362],{"class":47,"line":149},[45,23352,93],{"class":92},[45,23354,96],{"class":85},[45,23356,100],{"class":99},[45,23358,23359],{"class":103},"Iterator is finished",[45,23361,100],{"class":99},[45,23363,109],{"class":85},[14,23365,177],{},[36,23367,23369],{"className":38,"code":23368,"language":40,"meta":41,"style":41},"a\nb\nIterator is finished\n",[18,23370,23371,23375,23379],{"__ignoreMap":41},[45,23372,23373],{"class":47,"line":48},[45,23374,23156],{"class":51},[45,23376,23377],{"class":47,"line":63},[45,23378,23161],{"class":51},[45,23380,23381,23384,23386],{"class":47,"line":70},[45,23382,23383],{"class":51},"Iterator ",[45,23385,1285],{"class":55},[45,23387,23388],{"class":51}," finished\n",[14,23390,23391,23392,240],{},"If you want to learn more about this exception, see ",[236,23393,14822],{"href":23394},"\u002Ferrors\u002Fstopiteration-exception-in-python-explained\u002F",[31,23396,13092,23398,23400],{"id":23397},"how-for-loops-use-iterators",[18,23399,7895],{}," loops use iterators",[14,23402,2419,23403,23405],{},[18,23404,7895],{}," loop does this behind the scenes:",[346,23407,23408,23414,23419],{},[198,23409,23410,23411,23413],{},"Calls ",[18,23412,22241],{}," on the object",[198,23415,23416,23417],{},"Repeatedly calls ",[18,23418,13691],{},[198,23420,23421,23422,23424],{},"Stops when ",[18,23423,13833],{}," is raised",[14,23426,23427],{},"This means code like this:",[36,23429,23431],{"className":38,"code":23430,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\n\nfor number in numbers:\n    print(number)\n",[18,23432,23433,23453,23457,23469],{"__ignoreMap":41},[45,23434,23435,23437,23439,23441,23443,23445,23447,23449,23451],{"class":47,"line":48},[45,23436,8570],{"class":51},[45,23438,56],{"class":55},[45,23440,8575],{"class":85},[45,23442,11786],{"class":59},[45,23444,3079],{"class":85},[45,23446,10503],{"class":59},[45,23448,3079],{"class":85},[45,23450,10512],{"class":59},[45,23452,8594],{"class":85},[45,23454,23455],{"class":47,"line":63},[45,23456,67],{"emptyLinePlaceholder":66},[45,23458,23459,23461,23463,23465,23467],{"class":47,"line":70},[45,23460,7895],{"class":73},[45,23462,744],{"class":51},[45,23464,8623],{"class":73},[45,23466,8626],{"class":51},[45,23468,86],{"class":85},[45,23470,23471,23473,23475,23477],{"class":47,"line":89},[45,23472,93],{"class":92},[45,23474,96],{"class":85},[45,23476,13546],{"class":2070},[45,23478,109],{"class":85},[14,23480,23481],{},"works roughly like this:",[36,23483,23485],{"className":38,"code":23484,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\niterator = iter(numbers)\n\nwhile True:\n    try:\n        number = next(iterator)\n        print(number)\n    except StopIteration:\n        break\n",[18,23486,23487,23507,23521,23525,23534,23541,23556,23566,23575],{"__ignoreMap":41},[45,23488,23489,23491,23493,23495,23497,23499,23501,23503,23505],{"class":47,"line":48},[45,23490,8570],{"class":51},[45,23492,56],{"class":55},[45,23494,8575],{"class":85},[45,23496,11786],{"class":59},[45,23498,3079],{"class":85},[45,23500,10503],{"class":59},[45,23502,3079],{"class":85},[45,23504,10512],{"class":59},[45,23506,8594],{"class":85},[45,23508,23509,23511,23513,23515,23517,23519],{"class":47,"line":63},[45,23510,22169],{"class":51},[45,23512,56],{"class":55},[45,23514,22174],{"class":92},[45,23516,96],{"class":85},[45,23518,9069],{"class":2070},[45,23520,109],{"class":85},[45,23522,23523],{"class":47,"line":70},[45,23524,67],{"emptyLinePlaceholder":66},[45,23526,23527,23530,23532],{"class":47,"line":89},[45,23528,23529],{"class":73},"while",[45,23531,23323],{"class":481},[45,23533,86],{"class":85},[45,23535,23536,23539],{"class":47,"line":112},[45,23537,23538],{"class":73},"    try",[45,23540,86],{"class":85},[45,23542,23543,23545,23547,23550,23552,23554],{"class":47,"line":126},[45,23544,12989],{"class":51},[45,23546,56],{"class":55},[45,23548,23549],{"class":92}," next",[45,23551,96],{"class":85},[45,23553,22101],{"class":2070},[45,23555,109],{"class":85},[45,23557,23558,23560,23562,23564],{"class":47,"line":142},[45,23559,1923],{"class":92},[45,23561,96],{"class":85},[45,23563,13546],{"class":2070},[45,23565,109],{"class":85},[45,23567,23568,23571,23573],{"class":47,"line":149},[45,23569,23570],{"class":73},"    except",[45,23572,23346],{"class":2109},[45,23574,86],{"class":85},[45,23576,23577],{"class":47,"line":679},[45,23578,23579],{"class":73},"        break\n",[14,23581,23582],{},"Output for both:",[36,23584,23585],{"className":38,"code":22599,"language":40,"meta":41,"style":41},[18,23586,23587,23591,23595],{"__ignoreMap":41},[45,23588,23589],{"class":47,"line":48},[45,23590,13262],{"class":59},[45,23592,23593],{"class":47,"line":63},[45,23594,3436],{"class":59},[45,23596,23597],{"class":47,"line":70},[45,23598,22614],{"class":59},[14,23600,23601,23602,23604],{},"You do not need to write loops this way in normal code. But understanding it makes ",[18,23603,7895],{}," loops much easier to understand.",[14,23606,23607,23608,240],{},"If you want a full beginner guide, see ",[236,23609,23611],{"href":23610},"\u002Flearn\u002Fpython-for-loops-explained\u002F","Python for loops explained",[31,23613,23615],{"id":23614},"examples-of-iterable-objects","Examples of iterable objects",[14,23617,23618],{},"Here are some common iterable objects in Python.",[712,23620,23622],{"id":23621},"lists-and-tuples","Lists and tuples",[36,23624,23626],{"className":38,"code":23625,"language":40,"meta":41,"style":41},"items = [1, 2, 3]\nfor item in items:\n    print(item)\n\nvalues = (4, 5, 6)\nfor value in values:\n    print(value)\n",[18,23627,23628,23649,23662,23673,23677,23697,23709],{"__ignoreMap":41},[45,23629,23630,23633,23635,23637,23639,23641,23643,23645,23647],{"class":47,"line":48},[45,23631,23632],{"class":51},"items ",[45,23634,56],{"class":55},[45,23636,8575],{"class":85},[45,23638,8578],{"class":59},[45,23640,3079],{"class":85},[45,23642,3754],{"class":59},[45,23644,3079],{"class":85},[45,23646,777],{"class":59},[45,23648,8594],{"class":85},[45,23650,23651,23653,23655,23657,23660],{"class":47,"line":63},[45,23652,7895],{"class":73},[45,23654,8732],{"class":51},[45,23656,8623],{"class":73},[45,23658,23659],{"class":51}," items",[45,23661,86],{"class":85},[45,23663,23664,23666,23668,23671],{"class":47,"line":70},[45,23665,93],{"class":92},[45,23667,96],{"class":85},[45,23669,23670],{"class":2070},"item",[45,23672,109],{"class":85},[45,23674,23675],{"class":47,"line":89},[45,23676,67],{"emptyLinePlaceholder":66},[45,23678,23679,23681,23683,23685,23687,23689,23691,23693,23695],{"class":47,"line":112},[45,23680,10571],{"class":51},[45,23682,56],{"class":55},[45,23684,14039],{"class":85},[45,23686,3584],{"class":59},[45,23688,3079],{"class":85},[45,23690,1222],{"class":59},[45,23692,3079],{"class":85},[45,23694,9288],{"class":59},[45,23696,109],{"class":85},[45,23698,23699,23701,23703,23705,23707],{"class":47,"line":126},[45,23700,7895],{"class":73},[45,23702,1492],{"class":51},[45,23704,8623],{"class":73},[45,23706,10641],{"class":51},[45,23708,86],{"class":85},[45,23710,23711,23713,23715,23717],{"class":47,"line":142},[45,23712,93],{"class":92},[45,23714,96],{"class":85},[45,23716,12722],{"class":2070},[45,23718,109],{"class":85},[712,23720,23721],{"id":22296},"Strings",[36,23723,23725],{"className":38,"code":23724,"language":40,"meta":41,"style":41},"for letter in \"cat\":\n    print(letter)\n",[18,23726,23727,23744],{"__ignoreMap":41},[45,23728,23729,23731,23734,23736,23738,23740,23742],{"class":47,"line":48},[45,23730,7895],{"class":73},[45,23732,23733],{"class":51}," letter ",[45,23735,8623],{"class":73},[45,23737,1565],{"class":99},[45,23739,7035],{"class":103},[45,23741,100],{"class":99},[45,23743,86],{"class":85},[45,23745,23746,23748,23750,23753],{"class":47,"line":63},[45,23747,93],{"class":92},[45,23749,96],{"class":85},[45,23751,23752],{"class":2070},"letter",[45,23754,109],{"class":85},[14,23756,177],{},[36,23758,23760],{"className":38,"code":23759,"language":40,"meta":41,"style":41},"c\na\nt\n",[18,23761,23762,23766,23770],{"__ignoreMap":41},[45,23763,23764],{"class":47,"line":48},[45,23765,23166],{"class":51},[45,23767,23768],{"class":47,"line":63},[45,23769,23156],{"class":51},[45,23771,23772],{"class":47,"line":70},[45,23773,23774],{"class":51},"t\n",[712,23776,23778],{"id":23777},"dictionaries-and-sets","Dictionaries and sets",[14,23780,23781],{},"A dictionary loops over its keys by default:",[36,23783,23785],{"className":38,"code":23784,"language":40,"meta":41,"style":41},"person = {\"name\": \"Ava\", \"age\": 25}\n\nfor key in person:\n    print(key)\n",[18,23786,23787,23823,23827,23841],{"__ignoreMap":41},[45,23788,23789,23791,23793,23795,23797,23799,23801,23803,23805,23807,23809,23811,23813,23815,23817,23819,23821],{"class":47,"line":48},[45,23790,2966],{"class":51},[45,23792,56],{"class":55},[45,23794,8604],{"class":85},[45,23796,100],{"class":99},[45,23798,3096],{"class":103},[45,23800,100],{"class":99},[45,23802,457],{"class":85},[45,23804,1565],{"class":99},[45,23806,3162],{"class":103},[45,23808,100],{"class":99},[45,23810,3079],{"class":85},[45,23812,1565],{"class":99},[45,23814,2082],{"class":103},[45,23816,100],{"class":99},[45,23818,457],{"class":85},[45,23820,303],{"class":59},[45,23822,8629],{"class":85},[45,23824,23825],{"class":47,"line":63},[45,23826,67],{"emptyLinePlaceholder":66},[45,23828,23829,23831,23834,23836,23839],{"class":47,"line":70},[45,23830,7895],{"class":73},[45,23832,23833],{"class":51}," key ",[45,23835,8623],{"class":73},[45,23837,23838],{"class":51}," person",[45,23840,86],{"class":85},[45,23842,23843,23845,23847,23849],{"class":47,"line":89},[45,23844,93],{"class":92},[45,23846,96],{"class":85},[45,23848,9493],{"class":2070},[45,23850,109],{"class":85},[14,23852,23853],{},"A set is also iterable:",[36,23855,23857],{"className":38,"code":23856,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\n\nfor number in numbers:\n    print(number)\n",[18,23858,23859,23879,23883,23895],{"__ignoreMap":41},[45,23860,23861,23863,23865,23867,23869,23871,23873,23875,23877],{"class":47,"line":48},[45,23862,8570],{"class":51},[45,23864,56],{"class":55},[45,23866,8604],{"class":85},[45,23868,8578],{"class":59},[45,23870,3079],{"class":85},[45,23872,3754],{"class":59},[45,23874,3079],{"class":85},[45,23876,777],{"class":59},[45,23878,8629],{"class":85},[45,23880,23881],{"class":47,"line":63},[45,23882,67],{"emptyLinePlaceholder":66},[45,23884,23885,23887,23889,23891,23893],{"class":47,"line":70},[45,23886,7895],{"class":73},[45,23888,744],{"class":51},[45,23890,8623],{"class":73},[45,23892,8626],{"class":51},[45,23894,86],{"class":85},[45,23896,23897,23899,23901,23903],{"class":47,"line":89},[45,23898,93],{"class":92},[45,23900,96],{"class":85},[45,23902,13546],{"class":2070},[45,23904,109],{"class":85},[712,23906,23908,19922],{"id":23907},"range-objects",[18,23909,22305],{},[14,23911,23912,23914],{},[18,23913,22305],{}," is iterable, which is why it works well in loops.",[36,23916,23918],{"className":38,"code":23917,"language":40,"meta":41,"style":41},"for number in range(3):\n    print(number)\n",[18,23919,23920,23936],{"__ignoreMap":41},[45,23921,23922,23924,23926,23928,23930,23932,23934],{"class":47,"line":48},[45,23923,7895],{"class":73},[45,23925,744],{"class":51},[45,23927,8623],{"class":73},[45,23929,10451],{"class":92},[45,23931,96],{"class":85},[45,23933,11184],{"class":59},[45,23935,2464],{"class":85},[45,23937,23938,23940,23942,23944],{"class":47,"line":63},[45,23939,93],{"class":92},[45,23941,96],{"class":85},[45,23943,13546],{"class":2070},[45,23945,109],{"class":85},[14,23947,177],{},[36,23949,23951],{"className":38,"code":23950,"language":40,"meta":41,"style":41},"0\n1\n2\n",[18,23952,23953,23958,23962],{"__ignoreMap":41},[45,23954,23955],{"class":47,"line":48},[45,23956,23957],{"class":59},"0\n",[45,23959,23960],{"class":47,"line":63},[45,23961,13036],{"class":59},[45,23963,23964],{"class":47,"line":70},[45,23965,13041],{"class":59},[14,23967,23968,23969,240],{},"You can learn more on the ",[236,23970,23972],{"href":23971},"\u002Freference\u002Fpython-range-function-explained\u002F","Python range() function explained",[712,23974,23976],{"id":23975},"files-opened-for-reading","Files opened for reading",[14,23978,23979],{},"Files are iterable too. Python can read them one line at a time.",[36,23981,23983],{"className":38,"code":23982,"language":40,"meta":41,"style":41},"with open(\"example.txt\", \"r\") as file:\n    for line in file:\n        print(line.strip())\n",[18,23984,23985,24021,24034],{"__ignoreMap":41},[45,23986,23987,23990,23993,23995,23997,24000,24002,24004,24006,24009,24011,24013,24016,24019],{"class":47,"line":48},[45,23988,23989],{"class":73},"with",[45,23991,23992],{"class":92}," open",[45,23994,96],{"class":85},[45,23996,100],{"class":99},[45,23998,23999],{"class":103},"example.txt",[45,24001,100],{"class":99},[45,24003,3079],{"class":85},[45,24005,1565],{"class":99},[45,24007,24008],{"class":103},"r",[45,24010,100],{"class":99},[45,24012,9146],{"class":85},[45,24014,24015],{"class":73}," as",[45,24017,15011],{"class":24018},"sMMDD",[45,24020,86],{"class":85},[45,24022,24023,24025,24028,24030,24032],{"class":47,"line":63},[45,24024,14165],{"class":73},[45,24026,24027],{"class":51}," line ",[45,24029,8623],{"class":73},[45,24031,15011],{"class":24018},[45,24033,86],{"class":85},[45,24035,24036,24038,24040,24042,24044,24047],{"class":47,"line":70},[45,24037,1923],{"class":92},[45,24039,96],{"class":85},[45,24041,47],{"class":2070},[45,24043,240],{"class":85},[45,24045,24046],{"class":2070},"strip",[45,24048,10777],{"class":85},[14,24050,24051],{},"This is useful because it reads values one at a time instead of loading everything at once.",[31,24053,24055],{"id":24054},"when-iterators-are-useful","When iterators are useful",[14,24057,24058],{},"Iterators are helpful when you want to work with data step by step.",[14,24060,24061],{},"Common uses:",[195,24063,24064,24067,24070,24073],{},[198,24065,24066],{},"Reading values one at a time",[198,24068,24069],{},"Working with large data without loading everything at once",[198,24071,24072],{},"Building custom classes that support loops",[198,24074,24075],{},"Understanding generators more easily",[14,24077,24078,24079,24083],{},"A very common real-world example is a generator. Generators are a special kind of iterator, so learning this topic makes ",[236,24080,24082],{"href":24081},"\u002Flearn\u002Fgenerators-in-python-explained\u002F","generators in Python explained"," much easier to understand.",[31,24085,24087],{"id":24086},"beginner-mistakes-to-avoid","Beginner mistakes to avoid",[14,24089,9847],{},[712,24091,1350,24093,24095],{"id":24092},"using-next-on-a-list-instead-of-on-an-iterator",[18,24094,13691],{}," on a list instead of on an iterator",[14,24097,1358],{},[36,24099,24101],{"className":38,"code":24100,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\nprint(next(numbers))\n",[18,24102,24103,24123],{"__ignoreMap":41},[45,24104,24105,24107,24109,24111,24113,24115,24117,24119,24121],{"class":47,"line":48},[45,24106,8570],{"class":51},[45,24108,56],{"class":55},[45,24110,8575],{"class":85},[45,24112,11786],{"class":59},[45,24114,3079],{"class":85},[45,24116,10503],{"class":59},[45,24118,3079],{"class":85},[45,24120,10512],{"class":59},[45,24122,8594],{"class":85},[45,24124,24125,24127,24129,24131,24133,24135],{"class":47,"line":63},[45,24126,2065],{"class":92},[45,24128,96],{"class":85},[45,24130,13224],{"class":92},[45,24132,96],{"class":85},[45,24134,9069],{"class":2070},[45,24136,4348],{"class":85},[14,24138,24139,24140,24142],{},"A list is iterable, but it is not an iterator. You need ",[18,24141,22241],{}," first:",[36,24144,24146],{"className":38,"code":24145,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\niterator = iter(numbers)\n\nprint(next(iterator))\n",[18,24147,24148,24168,24182,24186],{"__ignoreMap":41},[45,24149,24150,24152,24154,24156,24158,24160,24162,24164,24166],{"class":47,"line":48},[45,24151,8570],{"class":51},[45,24153,56],{"class":55},[45,24155,8575],{"class":85},[45,24157,11786],{"class":59},[45,24159,3079],{"class":85},[45,24161,10503],{"class":59},[45,24163,3079],{"class":85},[45,24165,10512],{"class":59},[45,24167,8594],{"class":85},[45,24169,24170,24172,24174,24176,24178,24180],{"class":47,"line":63},[45,24171,22169],{"class":51},[45,24173,56],{"class":55},[45,24175,22174],{"class":92},[45,24177,96],{"class":85},[45,24179,9069],{"class":2070},[45,24181,109],{"class":85},[45,24183,24184],{"class":47,"line":70},[45,24185,67],{"emptyLinePlaceholder":66},[45,24187,24188,24190,24192,24194,24196,24198],{"class":47,"line":89},[45,24189,2065],{"class":92},[45,24191,96],{"class":85},[45,24193,13224],{"class":92},[45,24195,96],{"class":85},[45,24197,22101],{"class":2070},[45,24199,4348],{"class":85},[712,24201,24203],{"id":24202},"thinking-every-iterable-is-already-an-iterator","Thinking every iterable is already an iterator",[14,24205,24206,24207,24210],{},"Objects like lists, strings, and tuples are iterable, but they do not usually have ",[18,24208,24209],{},"__next__()"," directly.",[14,24212,24213],{},"Check it:",[36,24215,24217],{"className":38,"code":24216,"language":40,"meta":41,"style":41},"text = \"hello\"\nprint(hasattr(text, \"__next__\"))\n",[18,24218,24219,24231],{"__ignoreMap":41},[45,24220,24221,24223,24225,24227,24229],{"class":47,"line":48},[45,24222,10297],{"class":51},[45,24224,56],{"class":55},[45,24226,1565],{"class":99},[45,24228,18782],{"class":103},[45,24230,1571],{"class":99},[45,24232,24233,24235,24237,24239,24241,24243,24245,24247,24249,24251],{"class":47,"line":63},[45,24234,2065],{"class":92},[45,24236,96],{"class":85},[45,24238,21766],{"class":92},[45,24240,96],{"class":85},[45,24242,5000],{"class":2070},[45,24244,3079],{"class":85},[45,24246,1565],{"class":99},[45,24248,22832],{"class":103},[45,24250,100],{"class":99},[45,24252,4348],{"class":85},[14,24254,177],{},[36,24256,24257],{"className":38,"code":2265,"language":40,"meta":41,"style":41},[18,24258,24259],{"__ignoreMap":41},[45,24260,24261],{"class":47,"line":48},[45,24262,2265],{"class":481},[712,24264,24266],{"id":24265},"forgetting-that-iterators-get-used-up","Forgetting that iterators get used up",[14,24268,24269],{},"Once you consume an iterator, it does not automatically restart.",[36,24271,24273],{"className":38,"code":24272,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\niterator = iter(numbers)\n\nfor item in iterator:\n    print(item)\n\nfor item in iterator:\n    print(item)  # nothing prints\n",[18,24274,24275,24295,24309,24313,24325,24335,24339,24351],{"__ignoreMap":41},[45,24276,24277,24279,24281,24283,24285,24287,24289,24291,24293],{"class":47,"line":48},[45,24278,8570],{"class":51},[45,24280,56],{"class":55},[45,24282,8575],{"class":85},[45,24284,8578],{"class":59},[45,24286,3079],{"class":85},[45,24288,3754],{"class":59},[45,24290,3079],{"class":85},[45,24292,777],{"class":59},[45,24294,8594],{"class":85},[45,24296,24297,24299,24301,24303,24305,24307],{"class":47,"line":63},[45,24298,22169],{"class":51},[45,24300,56],{"class":55},[45,24302,22174],{"class":92},[45,24304,96],{"class":85},[45,24306,9069],{"class":2070},[45,24308,109],{"class":85},[45,24310,24311],{"class":47,"line":70},[45,24312,67],{"emptyLinePlaceholder":66},[45,24314,24315,24317,24319,24321,24323],{"class":47,"line":89},[45,24316,7895],{"class":73},[45,24318,8732],{"class":51},[45,24320,8623],{"class":73},[45,24322,23032],{"class":51},[45,24324,86],{"class":85},[45,24326,24327,24329,24331,24333],{"class":47,"line":112},[45,24328,93],{"class":92},[45,24330,96],{"class":85},[45,24332,23670],{"class":2070},[45,24334,109],{"class":85},[45,24336,24337],{"class":47,"line":126},[45,24338,67],{"emptyLinePlaceholder":66},[45,24340,24341,24343,24345,24347,24349],{"class":47,"line":142},[45,24342,7895],{"class":73},[45,24344,8732],{"class":51},[45,24346,8623],{"class":73},[45,24348,23032],{"class":51},[45,24350,86],{"class":85},[45,24352,24353,24355,24357,24359,24361],{"class":47,"line":149},[45,24354,93],{"class":92},[45,24356,96],{"class":85},[45,24358,23670],{"class":2070},[45,24360,9146],{"class":85},[45,24362,24363],{"class":7624},"  # nothing prints\n",[14,24365,24366],{},"If you want to loop again, create a new iterator:",[36,24368,24370],{"className":38,"code":24369,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\n\nfor item in iter(numbers):\n    print(item)\n\nfor item in iter(numbers):\n    print(item)\n",[18,24371,24372,24392,24396,24412,24422,24426,24442],{"__ignoreMap":41},[45,24373,24374,24376,24378,24380,24382,24384,24386,24388,24390],{"class":47,"line":48},[45,24375,8570],{"class":51},[45,24377,56],{"class":55},[45,24379,8575],{"class":85},[45,24381,8578],{"class":59},[45,24383,3079],{"class":85},[45,24385,3754],{"class":59},[45,24387,3079],{"class":85},[45,24389,777],{"class":59},[45,24391,8594],{"class":85},[45,24393,24394],{"class":47,"line":63},[45,24395,67],{"emptyLinePlaceholder":66},[45,24397,24398,24400,24402,24404,24406,24408,24410],{"class":47,"line":70},[45,24399,7895],{"class":73},[45,24401,8732],{"class":51},[45,24403,8623],{"class":73},[45,24405,22174],{"class":92},[45,24407,96],{"class":85},[45,24409,9069],{"class":2070},[45,24411,2464],{"class":85},[45,24413,24414,24416,24418,24420],{"class":47,"line":89},[45,24415,93],{"class":92},[45,24417,96],{"class":85},[45,24419,23670],{"class":2070},[45,24421,109],{"class":85},[45,24423,24424],{"class":47,"line":112},[45,24425,67],{"emptyLinePlaceholder":66},[45,24427,24428,24430,24432,24434,24436,24438,24440],{"class":47,"line":126},[45,24429,7895],{"class":73},[45,24431,8732],{"class":51},[45,24433,8623],{"class":73},[45,24435,22174],{"class":92},[45,24437,96],{"class":85},[45,24439,9069],{"class":2070},[45,24441,2464],{"class":85},[45,24443,24444,24446,24448,24450],{"class":47,"line":142},[45,24445,93],{"class":92},[45,24447,96],{"class":85},[45,24449,23670],{"class":2070},[45,24451,109],{"class":85},[712,24453,24455,24456,24458,24459,24461],{"id":24454},"not-handling-stopiteration-when-calling-next-manually","Not handling ",[18,24457,13833],{}," when calling ",[18,24460,13691],{}," manually",[14,24463,12209,24464,24466],{},[18,24465,13691],{}," yourself, you may need to handle the end of the iterator.",[36,24468,24470],{"className":38,"code":24469,"language":40,"meta":41,"style":41},"numbers = [1, 2]\niterator = iter(numbers)\n\ntry:\n    print(next(iterator))\n    print(next(iterator))\n    print(next(iterator))\nexcept StopIteration:\n    print(\"No more items\")\n",[18,24471,24472,24488,24502,24506,24512,24526,24540,24554,24562],{"__ignoreMap":41},[45,24473,24474,24476,24478,24480,24482,24484,24486],{"class":47,"line":48},[45,24475,8570],{"class":51},[45,24477,56],{"class":55},[45,24479,8575],{"class":85},[45,24481,8578],{"class":59},[45,24483,3079],{"class":85},[45,24485,3754],{"class":59},[45,24487,8594],{"class":85},[45,24489,24490,24492,24494,24496,24498,24500],{"class":47,"line":63},[45,24491,22169],{"class":51},[45,24493,56],{"class":55},[45,24495,22174],{"class":92},[45,24497,96],{"class":85},[45,24499,9069],{"class":2070},[45,24501,109],{"class":85},[45,24503,24504],{"class":47,"line":70},[45,24505,67],{"emptyLinePlaceholder":66},[45,24507,24508,24510],{"class":47,"line":89},[45,24509,23272],{"class":73},[45,24511,86],{"class":85},[45,24513,24514,24516,24518,24520,24522,24524],{"class":47,"line":112},[45,24515,93],{"class":92},[45,24517,96],{"class":85},[45,24519,13224],{"class":92},[45,24521,96],{"class":85},[45,24523,22101],{"class":2070},[45,24525,4348],{"class":85},[45,24527,24528,24530,24532,24534,24536,24538],{"class":47,"line":126},[45,24529,93],{"class":92},[45,24531,96],{"class":85},[45,24533,13224],{"class":92},[45,24535,96],{"class":85},[45,24537,22101],{"class":2070},[45,24539,4348],{"class":85},[45,24541,24542,24544,24546,24548,24550,24552],{"class":47,"line":142},[45,24543,93],{"class":92},[45,24545,96],{"class":85},[45,24547,13224],{"class":92},[45,24549,96],{"class":85},[45,24551,22101],{"class":2070},[45,24553,4348],{"class":85},[45,24555,24556,24558,24560],{"class":47,"line":149},[45,24557,23275],{"class":73},[45,24559,23346],{"class":2109},[45,24561,86],{"class":85},[45,24563,24564,24566,24568,24570,24573,24575],{"class":47,"line":679},[45,24565,93],{"class":92},[45,24567,96],{"class":85},[45,24569,100],{"class":99},[45,24571,24572],{"class":103},"No more items",[45,24574,100],{"class":99},[45,24576,109],{"class":85},[14,24578,177],{},[36,24580,24582],{"className":38,"code":24581,"language":40,"meta":41,"style":41},"1\n2\nNo more items\n",[18,24583,24584,24588,24592],{"__ignoreMap":41},[45,24585,24586],{"class":47,"line":48},[45,24587,13036],{"class":59},[45,24589,24590],{"class":47,"line":63},[45,24591,13041],{"class":59},[45,24593,24594],{"class":47,"line":70},[45,24595,24596],{"class":51},"No more items\n",[31,24598,24600],{"id":24599},"useful-checks-when-debugging","Useful checks when debugging",[14,24602,24603],{},"If you are not sure whether an object is iterable or an iterator, these checks can help:",[36,24605,24607],{"className":38,"code":24606,"language":40,"meta":41,"style":41},"obj = [1, 2, 3]\n\nprint(type(obj))\nprint(hasattr(obj, \"__iter__\"))\nprint(hasattr(obj, \"__next__\"))\n",[18,24608,24609,24629,24633,24647,24669],{"__ignoreMap":41},[45,24610,24611,24613,24615,24617,24619,24621,24623,24625,24627],{"class":47,"line":48},[45,24612,20295],{"class":51},[45,24614,56],{"class":55},[45,24616,8575],{"class":85},[45,24618,8578],{"class":59},[45,24620,3079],{"class":85},[45,24622,3754],{"class":59},[45,24624,3079],{"class":85},[45,24626,777],{"class":59},[45,24628,8594],{"class":85},[45,24630,24631],{"class":47,"line":63},[45,24632,67],{"emptyLinePlaceholder":66},[45,24634,24635,24637,24639,24641,24643,24645],{"class":47,"line":70},[45,24636,2065],{"class":92},[45,24638,96],{"class":85},[45,24640,4340],{"class":2109},[45,24642,96],{"class":85},[45,24644,20314],{"class":2070},[45,24646,4348],{"class":85},[45,24648,24649,24651,24653,24655,24657,24659,24661,24663,24665,24667],{"class":47,"line":89},[45,24650,2065],{"class":92},[45,24652,96],{"class":85},[45,24654,21766],{"class":92},[45,24656,96],{"class":85},[45,24658,20314],{"class":2070},[45,24660,3079],{"class":85},[45,24662,1565],{"class":99},[45,24664,22809],{"class":103},[45,24666,100],{"class":99},[45,24668,4348],{"class":85},[45,24670,24671,24673,24675,24677,24679,24681,24683,24685,24687,24689],{"class":47,"line":112},[45,24672,2065],{"class":92},[45,24674,96],{"class":85},[45,24676,21766],{"class":92},[45,24678,96],{"class":85},[45,24680,20314],{"class":2070},[45,24682,3079],{"class":85},[45,24684,1565],{"class":99},[45,24686,22832],{"class":103},[45,24688,100],{"class":99},[45,24690,4348],{"class":85},[14,24692,24693],{},"You can also try creating an iterator:",[36,24695,24697],{"className":38,"code":24696,"language":40,"meta":41,"style":41},"obj = [1, 2, 3]\niterator = iter(obj)\n\nprint(type(iterator))\nprint(next(iterator))\n",[18,24698,24699,24719,24733,24737,24751],{"__ignoreMap":41},[45,24700,24701,24703,24705,24707,24709,24711,24713,24715,24717],{"class":47,"line":48},[45,24702,20295],{"class":51},[45,24704,56],{"class":55},[45,24706,8575],{"class":85},[45,24708,8578],{"class":59},[45,24710,3079],{"class":85},[45,24712,3754],{"class":59},[45,24714,3079],{"class":85},[45,24716,777],{"class":59},[45,24718,8594],{"class":85},[45,24720,24721,24723,24725,24727,24729,24731],{"class":47,"line":63},[45,24722,22169],{"class":51},[45,24724,56],{"class":55},[45,24726,22174],{"class":92},[45,24728,96],{"class":85},[45,24730,20314],{"class":2070},[45,24732,109],{"class":85},[45,24734,24735],{"class":47,"line":70},[45,24736,67],{"emptyLinePlaceholder":66},[45,24738,24739,24741,24743,24745,24747,24749],{"class":47,"line":89},[45,24740,2065],{"class":92},[45,24742,96],{"class":85},[45,24744,4340],{"class":2109},[45,24746,96],{"class":85},[45,24748,22101],{"class":2070},[45,24750,4348],{"class":85},[45,24752,24753,24755,24757,24759,24761,24763],{"class":47,"line":112},[45,24754,2065],{"class":92},[45,24756,96],{"class":85},[45,24758,13224],{"class":92},[45,24760,96],{"class":85},[45,24762,22101],{"class":2070},[45,24764,4348],{"class":85},[14,24766,16286],{},[195,24768,24769,24774,24779,24783,24788],{},[198,24770,24771],{},[18,24772,24773],{},"type(obj)",[198,24775,24776],{},[18,24777,24778],{},"iter(obj)",[198,24780,24781],{},[18,24782,23172],{},[198,24784,24785],{},[18,24786,24787],{},"hasattr(obj, '__iter__')",[198,24789,24790],{},[18,24791,24792],{},"hasattr(obj, '__next__')",[31,24794,2281],{"id":2280},[712,24796,24798],{"id":24797},"is-every-iterable-also-an-iterator","Is every iterable also an iterator?",[14,24800,24801,24802,24804],{},"No. Many objects like lists and strings are iterable, but you must call ",[18,24803,22241],{}," to get an iterator from them.",[712,24806,24808],{"id":24807},"what-happens-when-an-iterator-runs-out-of-values","What happens when an iterator runs out of values?",[14,24810,24811,24812,24814,24815,24817],{},"Python raises ",[18,24813,13833],{},". A ",[18,24816,7895],{}," loop handles this automatically.",[712,24819,24821],{"id":24820},"can-i-loop-over-the-same-iterator-twice","Can I loop over the same iterator twice?",[14,24823,24824],{},"Usually no. Once an iterator is exhausted, it does not start over unless you create a new one.",[712,24826,24828],{"id":24827},"why-should-beginners-learn-this","Why should beginners learn this?",[14,24830,24831,24832,24834],{},"It helps you understand ",[18,24833,7895],{}," loops, generators, files, and many common Python tools.",[31,24836,2342],{"id":2341},[195,24838,24839,24843,24848,24852,24856,24860],{},[198,24840,24841],{},[236,24842,23611],{"href":23610},[198,24844,24845],{},[236,24846,24847],{"href":24081},"Generators in Python explained",[198,24849,24850],{},[236,24851,23972],{"href":23971},[198,24853,24854],{},[236,24855,14822],{"href":23394},[198,24857,24858],{},[236,24859,14817],{"href":22491},[198,24861,24862],{},[236,24863,14812],{"href":22739},[14,24865,24866],{},"If this topic makes sense now, the best next step is to learn generators. They are one of the most common real-world examples of iterators in Python.",[2367,24868,24869],{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .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 .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 pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":41,"searchDepth":63,"depth":63,"links":24871},[24872,24873,24874,24875,24876,24877,24882,24884,24892,24893,24901,24902,24908],{"id":33,"depth":63,"text":34},{"id":2529,"depth":63,"text":2530},{"id":22275,"depth":63,"text":22276},{"id":22495,"depth":63,"text":22496},{"id":22743,"depth":63,"text":22744},{"id":23047,"depth":63,"text":24878,"children":24879},"How iter() and next() work",[24880,24881],{"id":23057,"depth":70,"text":23060},{"id":23169,"depth":70,"text":23172},{"id":23397,"depth":63,"text":24883},"How for loops use iterators",{"id":23614,"depth":63,"text":23615,"children":24885},[24886,24887,24888,24889,24891],{"id":23621,"depth":70,"text":23622},{"id":22296,"depth":70,"text":23721},{"id":23777,"depth":70,"text":23778},{"id":23907,"depth":70,"text":24890},"range() objects",{"id":23975,"depth":70,"text":23976},{"id":24054,"depth":63,"text":24055},{"id":24086,"depth":63,"text":24087,"children":24894},[24895,24897,24898,24899],{"id":24092,"depth":70,"text":24896},"Using next() on a list instead of on an iterator",{"id":24202,"depth":70,"text":24203},{"id":24265,"depth":70,"text":24266},{"id":24454,"depth":70,"text":24900},"Not handling StopIteration when calling next() manually",{"id":24599,"depth":63,"text":24600},{"id":2280,"depth":63,"text":2281,"children":24903},[24904,24905,24906,24907],{"id":24797,"depth":70,"text":24798},{"id":24807,"depth":70,"text":24808},{"id":24820,"depth":70,"text":24821},{"id":24827,"depth":70,"text":24828},{"id":2341,"depth":63,"text":2342},"Master iterators and iterable objects explained in our comprehensive Python beginner guide.",{},{"title":22087,"description":24909},"learn\u002Fiterators-and-iterable-objects-explained","eNEaRPXL4e21qjVTG5gEn07yLtxa5ri_otVFqJLjC-M",{"id":24915,"title":24916,"body":24917,"description":27116,"extension":2399,"meta":27117,"navigation":66,"path":27118,"seo":27119,"stem":27120,"__hash__":27121},"content\u002Flearn\u002Flambda-functions-in-python-explained.md","Lambda Functions in Python Explained",{"type":7,"value":24918,"toc":27077},[24919,24922,24925,24927,24944,24953,24955,25076,25078,25107,25115,25119,25122,25125,25151,25153,25194,25196,25204,25214,25218,25220,25237,25241,25266,25269,25308,25310,25318,25321,25366,25368,25376,25381,25434,25441,25445,25450,25454,25471,25475,25522,25528,25582,25585,25591,25595,25598,25606,25611,25678,25680,25701,25707,25715,25722,25729,25801,25803,25820,25826,25833,25840,25846,25948,25950,26000,26003,26011,26015,26018,26049,26051,26059,26062,26066,26069,26102,26108,26133,26136,26141,26197,26199,26207,26211,26216,26232,26238,26309,26311,26320,26323,26335,26338,26404,26463,26466,26468,26471,26475,26478,26483,26487,26490,26495,26499,26504,26506,26550,26554,26557,26580,26583,26602,26610,26618,26624,26718,26721,26914,26916,26987,26989,26993,26998,27002,27007,27011,27014,27018,27024,27028,27031,27033,27074],[10,24920,24916],{"id":24921},"lambda-functions-in-python-explained",[14,24923,24924],{},"Lambda functions are a short way to create small functions in Python.",[14,24926,19201],{},[195,24928,24929,24932,24935,24938],{},[198,24930,24931],{},"what a lambda function is",[198,24933,24934],{},"what its syntax means",[198,24936,24937],{},"where beginners usually see it",[198,24939,24940,24941,24943],{},"when ",[18,24942,2757],{}," is a better choice",[14,24945,24946,24947,24949,24950,240],{},"If you are new to functions, it also helps to understand ",[236,24948,5821],{"href":5820}," and how ",[236,24951,24952],{"href":4579},"Python functions work",[31,24954,34],{"id":33},[36,24956,24958],{"className":38,"code":24957,"language":40,"meta":41,"style":41},"add = lambda a, b: a + b\nprint(add(2, 3))\n\nnumbers = [1, 2, 3, 4]\ndoubled = list(map(lambda n: n * 2, numbers))\nprint(doubled)\n",[18,24959,24960,24985,25003,25007,25031,25066],{"__ignoreMap":41},[45,24961,24962,24965,24967,24970,24973,24975,24977,24979,24981,24983],{"class":47,"line":48},[45,24963,24964],{"class":51},"add ",[45,24966,56],{"class":55},[45,24968,24969],{"class":2440}," lambda",[45,24971,24972],{"class":3082}," a",[45,24974,3079],{"class":85},[45,24976,11115],{"class":3082},[45,24978,457],{"class":85},[45,24980,11124],{"class":51},[45,24982,8850],{"class":55},[45,24984,11129],{"class":51},[45,24986,24987,24989,24991,24993,24995,24997,24999,25001],{"class":47,"line":63},[45,24988,2065],{"class":92},[45,24990,96],{"class":85},[45,24992,11159],{"class":2070},[45,24994,96],{"class":85},[45,24996,9367],{"class":59},[45,24998,3079],{"class":85},[45,25000,777],{"class":59},[45,25002,4348],{"class":85},[45,25004,25005],{"class":47,"line":70},[45,25006,67],{"emptyLinePlaceholder":66},[45,25008,25009,25011,25013,25015,25017,25019,25021,25023,25025,25027,25029],{"class":47,"line":89},[45,25010,8570],{"class":51},[45,25012,56],{"class":55},[45,25014,8575],{"class":85},[45,25016,8578],{"class":59},[45,25018,3079],{"class":85},[45,25020,3754],{"class":59},[45,25022,3079],{"class":85},[45,25024,777],{"class":59},[45,25026,3079],{"class":85},[45,25028,8591],{"class":59},[45,25030,8594],{"class":85},[45,25032,25033,25035,25037,25039,25041,25044,25046,25049,25052,25054,25056,25058,25060,25062,25064],{"class":47,"line":112},[45,25034,13939],{"class":51},[45,25036,56],{"class":55},[45,25038,14381],{"class":2109},[45,25040,96],{"class":85},[45,25042,25043],{"class":92},"map",[45,25045,96],{"class":85},[45,25047,25048],{"class":2440},"lambda",[45,25050,25051],{"class":3082}," n",[45,25053,457],{"class":85},[45,25055,8612],{"class":2070},[45,25057,5601],{"class":55},[45,25059,3754],{"class":59},[45,25061,3079],{"class":85},[45,25063,8626],{"class":2070},[45,25065,4348],{"class":85},[45,25067,25068,25070,25072,25074],{"class":47,"line":126},[45,25069,2065],{"class":92},[45,25071,96],{"class":85},[45,25073,13968],{"class":2070},[45,25075,109],{"class":85},[14,25077,177],{},[36,25079,25081],{"className":38,"code":25080,"language":40,"meta":41,"style":41},"5\n[2, 4, 6, 8]\n",[18,25082,25083,25087],{"__ignoreMap":41},[45,25084,25085],{"class":47,"line":48},[45,25086,11570],{"class":59},[45,25088,25089,25091,25093,25095,25097,25099,25101,25103,25105],{"class":47,"line":63},[45,25090,9680],{"class":85},[45,25092,9367],{"class":59},[45,25094,3079],{"class":85},[45,25096,8591],{"class":59},[45,25098,3079],{"class":85},[45,25100,9288],{"class":59},[45,25102,3079],{"class":85},[45,25104,1254],{"class":59},[45,25106,8594],{"class":85},[14,25108,165,25109,25111,25112,25114],{},[18,25110,25048],{}," for short, one-expression functions. If the logic needs multiple steps, use ",[18,25113,2757],{}," instead.",[31,25116,25118],{"id":25117},"what-a-lambda-function-is","What a lambda function is",[14,25120,25121],{},"A lambda function is a small anonymous function.",[14,25123,25124],{},"Here is what that means:",[195,25126,25127,25133,25139,25145,25148],{},[198,25128,25129,25132],{},[2421,25130,25131],{},"Small"," means it is usually used for short logic.",[198,25134,25135,25138],{},[2421,25136,25137],{},"Anonymous"," means it does not need a normal function name.",[198,25140,25141,25142,25144],{},"It is created with the ",[18,25143,25048],{}," keyword.",[198,25146,25147],{},"It can take arguments.",[198,25149,25150],{},"It returns the result of one expression.",[14,25152,466],{},[36,25154,25156],{"className":38,"code":25155,"language":40,"meta":41,"style":41},"square = lambda x: x * x\nprint(square(4))\n",[18,25157,25158,25179],{"__ignoreMap":41},[45,25159,25160,25163,25165,25167,25170,25172,25174,25176],{"class":47,"line":48},[45,25161,25162],{"class":51},"square ",[45,25164,56],{"class":55},[45,25166,24969],{"class":2440},[45,25168,25169],{"class":3082}," x",[45,25171,457],{"class":85},[45,25173,1185],{"class":51},[45,25175,5601],{"class":55},[45,25177,25178],{"class":51}," x\n",[45,25180,25181,25183,25185,25188,25190,25192],{"class":47,"line":63},[45,25182,2065],{"class":92},[45,25184,96],{"class":85},[45,25186,25187],{"class":2070},"square",[45,25189,96],{"class":85},[45,25191,3584],{"class":59},[45,25193,4348],{"class":85},[14,25195,177],{},[36,25197,25198],{"className":38,"code":2260,"language":40,"meta":41,"style":41},[18,25199,25200],{"__ignoreMap":41},[45,25201,25202],{"class":47,"line":48},[45,25203,2260],{"class":59},[14,25205,25206,25207,25209,25210,1353,25212,240],{},"Even though lambda functions are called \"anonymous\", you can still assign one to a variable like ",[18,25208,25187],{},". The main difference is that the function itself is written with ",[18,25211,25048],{},[18,25213,2757],{},[31,25215,25217],{"id":25216},"basic-lambda-syntax","Basic lambda syntax",[14,25219,8710],{},[36,25221,25223],{"className":38,"code":25222,"language":40,"meta":41,"style":41},"lambda parameters: expression\n",[18,25224,25225],{"__ignoreMap":41},[45,25226,25227,25229,25232,25234],{"class":47,"line":48},[45,25228,25048],{"class":2440},[45,25230,25231],{"class":3082}," parameters",[45,25233,457],{"class":85},[45,25235,25236],{"class":51}," expression\n",[712,25238,25240],{"id":25239},"parts-of-the-syntax","Parts of the syntax",[195,25242,25243,25249,25255,25258],{},[198,25244,1732,25245,25248],{},[2421,25246,25247],{},"parameters"," go before the colon.",[198,25250,1732,25251,25254],{},[2421,25252,25253],{},"expression"," goes after the colon.",[198,25256,25257],{},"The result of the expression is returned automatically.",[198,25259,2854,25260,25262,25263,25265],{},[2421,25261,2857],{}," write ",[18,25264,3461],{}," inside a lambda.",[14,25267,25268],{},"Example with one argument:",[36,25270,25272],{"className":38,"code":25271,"language":40,"meta":41,"style":41},"double = lambda x: x * 2\nprint(double(5))\n",[18,25273,25274,25293],{"__ignoreMap":41},[45,25275,25276,25279,25281,25283,25285,25287,25289,25291],{"class":47,"line":48},[45,25277,25278],{"class":51},"double ",[45,25280,56],{"class":55},[45,25282,24969],{"class":2440},[45,25284,25169],{"class":3082},[45,25286,457],{"class":85},[45,25288,1185],{"class":51},[45,25290,5601],{"class":55},[45,25292,13423],{"class":59},[45,25294,25295,25297,25299,25302,25304,25306],{"class":47,"line":63},[45,25296,2065],{"class":92},[45,25298,96],{"class":85},[45,25300,25301],{"class":2070},"double",[45,25303,96],{"class":85},[45,25305,3749],{"class":59},[45,25307,4348],{"class":85},[14,25309,177],{},[36,25311,25312],{"className":38,"code":13262,"language":40,"meta":41,"style":41},[18,25313,25314],{"__ignoreMap":41},[45,25315,25316],{"class":47,"line":48},[45,25317,13262],{"class":59},[14,25319,25320],{},"Example with two arguments:",[36,25322,25324],{"className":38,"code":25323,"language":40,"meta":41,"style":41},"add = lambda a, b: a + b\nprint(add(2, 3))\n",[18,25325,25326,25348],{"__ignoreMap":41},[45,25327,25328,25330,25332,25334,25336,25338,25340,25342,25344,25346],{"class":47,"line":48},[45,25329,24964],{"class":51},[45,25331,56],{"class":55},[45,25333,24969],{"class":2440},[45,25335,24972],{"class":3082},[45,25337,3079],{"class":85},[45,25339,11115],{"class":3082},[45,25341,457],{"class":85},[45,25343,11124],{"class":51},[45,25345,8850],{"class":55},[45,25347,11129],{"class":51},[45,25349,25350,25352,25354,25356,25358,25360,25362,25364],{"class":47,"line":63},[45,25351,2065],{"class":92},[45,25353,96],{"class":85},[45,25355,11159],{"class":2070},[45,25357,96],{"class":85},[45,25359,9367],{"class":59},[45,25361,3079],{"class":85},[45,25363,777],{"class":59},[45,25365,4348],{"class":85},[14,25367,177],{},[36,25369,25370],{"className":38,"code":11570,"language":40,"meta":41,"style":41},[18,25371,25372],{"__ignoreMap":41},[45,25373,25374],{"class":47,"line":48},[45,25375,11570],{"class":59},[14,25377,25378,25379,457],{},"This works like a normal function written with ",[18,25380,2757],{},[36,25382,25384],{"className":38,"code":25383,"language":40,"meta":41,"style":41},"def add(a, b):\n    return a + b\n\nprint(add(2, 3))\n",[18,25385,25386,25402,25412,25416],{"__ignoreMap":41},[45,25387,25388,25390,25392,25394,25396,25398,25400],{"class":47,"line":48},[45,25389,2757],{"class":2440},[45,25391,11106],{"class":2455},[45,25393,96],{"class":85},[45,25395,236],{"class":3082},[45,25397,3079],{"class":85},[45,25399,11115],{"class":3082},[45,25401,2464],{"class":85},[45,25403,25404,25406,25408,25410],{"class":47,"line":63},[45,25405,4681],{"class":73},[45,25407,11124],{"class":51},[45,25409,8850],{"class":55},[45,25411,11129],{"class":51},[45,25413,25414],{"class":47,"line":70},[45,25415,67],{"emptyLinePlaceholder":66},[45,25417,25418,25420,25422,25424,25426,25428,25430,25432],{"class":47,"line":89},[45,25419,2065],{"class":92},[45,25421,96],{"class":85},[45,25423,11159],{"class":2070},[45,25425,96],{"class":85},[45,25427,9367],{"class":59},[45,25429,3079],{"class":85},[45,25431,777],{"class":59},[45,25433,4348],{"class":85},[14,25435,25436,25437,1316,25439,240],{},"If you want to understand arguments more clearly, see ",[236,25438,5782],{"href":4583},[236,25440,5885],{"href":5884},[31,25442,25444],{"id":25443},"lambda-vs-normal-functions","Lambda vs normal functions",[14,25446,165,25447,25449],{},[18,25448,25048],{}," for very short and simple functions.",[14,25451,165,25452,8077],{},[18,25453,2757],{},[195,25455,25456,25459,25462,25465,25468],{},[198,25457,25458],{},"the function needs multiple lines",[198,25460,25461],{},"the logic has more than one step",[198,25463,25464],{},"you want clearer code",[198,25466,25467],{},"you plan to reuse the function in several places",[198,25469,25470],{},"debugging would be easier with a named function",[712,25472,25474],{"id":25473},"lambda-example","Lambda example",[36,25476,25478],{"className":38,"code":25477,"language":40,"meta":41,"style":41},"multiply = lambda a, b: a * b\nprint(multiply(3, 4))\n",[18,25479,25480,25503],{"__ignoreMap":41},[45,25481,25482,25485,25487,25489,25491,25493,25495,25497,25499,25501],{"class":47,"line":48},[45,25483,25484],{"class":51},"multiply ",[45,25486,56],{"class":55},[45,25488,24969],{"class":2440},[45,25490,24972],{"class":3082},[45,25492,3079],{"class":85},[45,25494,11115],{"class":3082},[45,25496,457],{"class":85},[45,25498,11124],{"class":51},[45,25500,5601],{"class":55},[45,25502,11129],{"class":51},[45,25504,25505,25507,25509,25512,25514,25516,25518,25520],{"class":47,"line":63},[45,25506,2065],{"class":92},[45,25508,96],{"class":85},[45,25510,25511],{"class":2070},"multiply",[45,25513,96],{"class":85},[45,25515,11184],{"class":59},[45,25517,3079],{"class":85},[45,25519,8591],{"class":59},[45,25521,4348],{"class":85},[712,25523,25525,25526],{"id":25524},"the-same-logic-with-def","The same logic with ",[18,25527,2757],{},[36,25529,25531],{"className":38,"code":25530,"language":40,"meta":41,"style":41},"def multiply(a, b):\n    return a * b\n\nprint(multiply(3, 4))\n",[18,25532,25533,25550,25560,25564],{"__ignoreMap":41},[45,25534,25535,25537,25540,25542,25544,25546,25548],{"class":47,"line":48},[45,25536,2757],{"class":2440},[45,25538,25539],{"class":2455}," multiply",[45,25541,96],{"class":85},[45,25543,236],{"class":3082},[45,25545,3079],{"class":85},[45,25547,11115],{"class":3082},[45,25549,2464],{"class":85},[45,25551,25552,25554,25556,25558],{"class":47,"line":63},[45,25553,4681],{"class":73},[45,25555,11124],{"class":51},[45,25557,5601],{"class":55},[45,25559,11129],{"class":51},[45,25561,25562],{"class":47,"line":70},[45,25563,67],{"emptyLinePlaceholder":66},[45,25565,25566,25568,25570,25572,25574,25576,25578,25580],{"class":47,"line":89},[45,25567,2065],{"class":92},[45,25569,96],{"class":85},[45,25571,25511],{"class":2070},[45,25573,96],{"class":85},[45,25575,11184],{"class":59},[45,25577,3079],{"class":85},[45,25579,8591],{"class":59},[45,25581,4348],{"class":85},[14,25583,25584],{},"Both versions accept arguments and return values.",[14,25586,25587,25588,25590],{},"For beginners, ",[18,25589,2757],{}," is often easier to read. Lambda is most useful when you need a small function only once.",[31,25592,25594],{"id":25593},"where-beginners-usually-see-lambda","Where beginners usually see lambda",[14,25596,25597],{},"Beginners often see lambda functions as short helper functions passed into other functions.",[712,25599,25601,25602,25605],{"id":25600},"with-map-to-change-each-item","With ",[18,25603,25604],{},"map()"," to change each item",[14,25607,25608,25610],{},[18,25609,25604],{}," applies a function to every item in an iterable.",[36,25612,25614],{"className":38,"code":25613,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\ndoubled = list(map(lambda n: n * 2, numbers))\nprint(doubled)\n",[18,25615,25616,25636,25668],{"__ignoreMap":41},[45,25617,25618,25620,25622,25624,25626,25628,25630,25632,25634],{"class":47,"line":48},[45,25619,8570],{"class":51},[45,25621,56],{"class":55},[45,25623,8575],{"class":85},[45,25625,8578],{"class":59},[45,25627,3079],{"class":85},[45,25629,3754],{"class":59},[45,25631,3079],{"class":85},[45,25633,777],{"class":59},[45,25635,8594],{"class":85},[45,25637,25638,25640,25642,25644,25646,25648,25650,25652,25654,25656,25658,25660,25662,25664,25666],{"class":47,"line":63},[45,25639,13939],{"class":51},[45,25641,56],{"class":55},[45,25643,14381],{"class":2109},[45,25645,96],{"class":85},[45,25647,25043],{"class":92},[45,25649,96],{"class":85},[45,25651,25048],{"class":2440},[45,25653,25051],{"class":3082},[45,25655,457],{"class":85},[45,25657,8612],{"class":2070},[45,25659,5601],{"class":55},[45,25661,3754],{"class":59},[45,25663,3079],{"class":85},[45,25665,8626],{"class":2070},[45,25667,4348],{"class":85},[45,25669,25670,25672,25674,25676],{"class":47,"line":70},[45,25671,2065],{"class":92},[45,25673,96],{"class":85},[45,25675,13968],{"class":2070},[45,25677,109],{"class":85},[14,25679,177],{},[36,25681,25683],{"className":38,"code":25682,"language":40,"meta":41,"style":41},"[2, 4, 6]\n",[18,25684,25685],{"__ignoreMap":41},[45,25686,25687,25689,25691,25693,25695,25697,25699],{"class":47,"line":48},[45,25688,9680],{"class":85},[45,25690,9367],{"class":59},[45,25692,3079],{"class":85},[45,25694,8591],{"class":59},[45,25696,3079],{"class":85},[45,25698,9288],{"class":59},[45,25700,8594],{"class":85},[14,25702,3444,25703,25706],{},[18,25704,25705],{},"lambda n: n * 2"," takes each number and doubles it.",[14,25708,25709,25710,240],{},"For more details, see ",[236,25711,18104,25713,18107],{"href":25712},"\u002Freference\u002Fpython-map-function-explained\u002F",[18,25714,25604],{},[712,25716,25601,25718,25721],{"id":25717},"with-filter-to-keep-matching-items",[18,25719,25720],{},"filter()"," to keep matching items",[14,25723,25724,25726,25727,240],{},[18,25725,25720],{}," keeps only items where the function returns ",[18,25728,229],{},[36,25730,25732],{"className":38,"code":25731,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\nresult = list(filter(lambda n: n > 2, numbers))\nprint(result)\n",[18,25733,25734,25758,25791],{"__ignoreMap":41},[45,25735,25736,25738,25740,25742,25744,25746,25748,25750,25752,25754,25756],{"class":47,"line":48},[45,25737,8570],{"class":51},[45,25739,56],{"class":55},[45,25741,8575],{"class":85},[45,25743,8578],{"class":59},[45,25745,3079],{"class":85},[45,25747,3754],{"class":59},[45,25749,3079],{"class":85},[45,25751,777],{"class":59},[45,25753,3079],{"class":85},[45,25755,8591],{"class":59},[45,25757,8594],{"class":85},[45,25759,25760,25762,25764,25766,25768,25771,25773,25775,25777,25779,25781,25783,25785,25787,25789],{"class":47,"line":63},[45,25761,3595],{"class":51},[45,25763,56],{"class":55},[45,25765,14381],{"class":2109},[45,25767,96],{"class":85},[45,25769,25770],{"class":92},"filter",[45,25772,96],{"class":85},[45,25774,25048],{"class":2440},[45,25776,25051],{"class":3082},[45,25778,457],{"class":85},[45,25780,8612],{"class":2070},[45,25782,272],{"class":55},[45,25784,3754],{"class":59},[45,25786,3079],{"class":85},[45,25788,8626],{"class":2070},[45,25790,4348],{"class":85},[45,25792,25793,25795,25797,25799],{"class":47,"line":70},[45,25794,2065],{"class":92},[45,25796,96],{"class":85},[45,25798,3620],{"class":2070},[45,25800,109],{"class":85},[14,25802,177],{},[36,25804,25806],{"className":38,"code":25805,"language":40,"meta":41,"style":41},"[3, 4]\n",[18,25807,25808],{"__ignoreMap":41},[45,25809,25810,25812,25814,25816,25818],{"class":47,"line":48},[45,25811,9680],{"class":85},[45,25813,11184],{"class":59},[45,25815,3079],{"class":85},[45,25817,8591],{"class":59},[45,25819,8594],{"class":85},[14,25821,25822,25823,25825],{},"Here, only numbers greater than ",[18,25824,9367],{}," stay in the result.",[14,25827,16391,25828,240],{},[236,25829,18104,25831,18107],{"href":25830},"\u002Freference\u002Fpython-filter-function-explained\u002F",[18,25832,25720],{},[712,25834,25601,25836,25839],{"id":25835},"with-sorted-as-a-key-function",[18,25837,25838],{},"sorted()"," as a key function",[14,25841,25842,25843,25845],{},"A lambda is often used to tell ",[18,25844,25838],{}," what value to sort by.",[36,25847,25849],{"className":38,"code":25848,"language":40,"meta":41,"style":41},"pairs = [('a', 3), ('b', 1), ('c', 2)]\nresult = sorted(pairs, key=lambda item: item[1])\nprint(result)\n",[18,25850,25851,25903,25938],{"__ignoreMap":41},[45,25852,25853,25856,25858,25861,25863,25865,25867,25869,25871,25874,25876,25878,25880,25882,25884,25886,25888,25890,25892,25894,25896,25898,25900],{"class":47,"line":48},[45,25854,25855],{"class":51},"pairs ",[45,25857,56],{"class":55},[45,25859,25860],{"class":85}," [(",[45,25862,7901],{"class":99},[45,25864,236],{"class":103},[45,25866,7901],{"class":99},[45,25868,3079],{"class":85},[45,25870,777],{"class":59},[45,25872,25873],{"class":85},"),",[45,25875,14039],{"class":85},[45,25877,7901],{"class":99},[45,25879,10384],{"class":103},[45,25881,7901],{"class":99},[45,25883,3079],{"class":85},[45,25885,9030],{"class":59},[45,25887,25873],{"class":85},[45,25889,14039],{"class":85},[45,25891,7901],{"class":99},[45,25893,10402],{"class":103},[45,25895,7901],{"class":99},[45,25897,3079],{"class":85},[45,25899,3754],{"class":59},[45,25901,25902],{"class":85},")]\n",[45,25904,25905,25907,25909,25912,25914,25917,25919,25921,25923,25925,25928,25930,25932,25934,25936],{"class":47,"line":63},[45,25906,3595],{"class":51},[45,25908,56],{"class":55},[45,25910,25911],{"class":92}," sorted",[45,25913,96],{"class":85},[45,25915,25916],{"class":2070},"pairs",[45,25918,3079],{"class":85},[45,25920,9502],{"class":6503},[45,25922,56],{"class":55},[45,25924,25048],{"class":2440},[45,25926,25927],{"class":3082}," item",[45,25929,457],{"class":85},[45,25931,25927],{"class":2070},[45,25933,9680],{"class":85},[45,25935,8578],{"class":59},[45,25937,21941],{"class":85},[45,25939,25940,25942,25944,25946],{"class":47,"line":70},[45,25941,2065],{"class":92},[45,25943,96],{"class":85},[45,25945,3620],{"class":2070},[45,25947,109],{"class":85},[14,25949,177],{},[36,25951,25953],{"className":38,"code":25952,"language":40,"meta":41,"style":41},"[('b', 1), ('c', 2), ('a', 3)]\n",[18,25954,25955],{"__ignoreMap":41},[45,25956,25957,25960,25962,25964,25966,25968,25970,25972,25974,25976,25978,25980,25982,25984,25986,25988,25990,25992,25994,25996,25998],{"class":47,"line":48},[45,25958,25959],{"class":85},"[(",[45,25961,7901],{"class":99},[45,25963,10384],{"class":103},[45,25965,7901],{"class":99},[45,25967,3079],{"class":85},[45,25969,9030],{"class":59},[45,25971,25873],{"class":85},[45,25973,14039],{"class":85},[45,25975,7901],{"class":99},[45,25977,10402],{"class":103},[45,25979,7901],{"class":99},[45,25981,3079],{"class":85},[45,25983,3754],{"class":59},[45,25985,25873],{"class":85},[45,25987,14039],{"class":85},[45,25989,7901],{"class":99},[45,25991,236],{"class":103},[45,25993,7901],{"class":99},[45,25995,3079],{"class":85},[45,25997,777],{"class":59},[45,25999,25902],{"class":85},[14,26001,26002],{},"This sorts by the second item in each tuple.",[14,26004,26005,26006,240],{},"See ",[236,26007,18104,26009,18107],{"href":26008},"\u002Freference\u002Fpython-sorted-function-explained\u002F",[18,26010,25838],{},[712,26012,26014],{"id":26013},"inside-short-one-time-helper-expressions","Inside short one-time helper expressions",[14,26016,26017],{},"You can also call a lambda immediately:",[36,26019,26021],{"className":38,"code":26020,"language":40,"meta":41,"style":41},"print((lambda x: x * 2)(5))\n",[18,26022,26023],{"__ignoreMap":41},[45,26024,26025,26027,26030,26032,26034,26036,26038,26040,26042,26045,26047],{"class":47,"line":48},[45,26026,2065],{"class":92},[45,26028,26029],{"class":85},"((",[45,26031,25048],{"class":2440},[45,26033,25169],{"class":3082},[45,26035,457],{"class":85},[45,26037,1185],{"class":2070},[45,26039,5601],{"class":55},[45,26041,3754],{"class":59},[45,26043,26044],{"class":85},")(",[45,26046,3749],{"class":59},[45,26048,4348],{"class":85},[14,26050,177],{},[36,26052,26053],{"className":38,"code":13262,"language":40,"meta":41,"style":41},[18,26054,26055],{"__ignoreMap":41},[45,26056,26057],{"class":47,"line":48},[45,26058,13262],{"class":59},[14,26060,26061],{},"This is valid Python, but it is less common in beginner code.",[31,26063,26065],{"id":26064},"important-limits-of-lambda","Important limits of lambda",[14,26067,26068],{},"Lambda functions have important limits.",[195,26070,26071,26077,26096,26099],{},[198,26072,26073,26074,240],{},"A lambda can contain only ",[2421,26075,26076],{},"one expression",[198,26078,26079,26080],{},"It cannot contain normal statements in the usual form, such as:\n",[195,26081,26082,26085,26091],{},[198,26083,26084],{},"assignment statements",[198,26086,26087,26088,26090],{},"regular ",[18,26089,20],{}," blocks",[198,26092,26093,26095],{},[18,26094,7895],{}," loops",[198,26097,26098],{},"Complex lambda code becomes hard to read very quickly.",[198,26100,26101],{},"Readable code is usually better than shorter code.",[14,26103,26104,26105,457],{},"This is ",[2421,26106,26107],{},"not valid",[36,26109,26111],{"className":38,"code":26110,"language":40,"meta":41,"style":41},"# Invalid Python\n# func = lambda x:\n#     y = x + 1\n#     return y\n",[18,26112,26113,26118,26123,26128],{"__ignoreMap":41},[45,26114,26115],{"class":47,"line":48},[45,26116,26117],{"class":7624},"# Invalid Python\n",[45,26119,26120],{"class":47,"line":63},[45,26121,26122],{"class":7624},"# func = lambda x:\n",[45,26124,26125],{"class":47,"line":70},[45,26126,26127],{"class":7624},"#     y = x + 1\n",[45,26129,26130],{"class":47,"line":89},[45,26131,26132],{"class":7624},"#     return y\n",[14,26134,26135],{},"A lambda must stay on one expression.",[14,26137,26138,26139,457],{},"If you need multiple steps, use ",[18,26140,2757],{},[36,26142,26144],{"className":38,"code":26143,"language":40,"meta":41,"style":41},"def func(x):\n    y = x + 1\n    return y\n\nprint(func(5))\n",[18,26145,26146,26159,26172,26179,26183],{"__ignoreMap":41},[45,26147,26148,26150,26152,26154,26157],{"class":47,"line":48},[45,26149,2757],{"class":2440},[45,26151,5638],{"class":2455},[45,26153,96],{"class":85},[45,26155,26156],{"class":3082},"x",[45,26158,2464],{"class":85},[45,26160,26161,26164,26166,26168,26170],{"class":47,"line":63},[45,26162,26163],{"class":51},"    y ",[45,26165,56],{"class":55},[45,26167,1185],{"class":51},[45,26169,8850],{"class":55},[45,26171,12962],{"class":59},[45,26173,26174,26176],{"class":47,"line":70},[45,26175,4681],{"class":73},[45,26177,26178],{"class":51}," y\n",[45,26180,26181],{"class":47,"line":89},[45,26182,67],{"emptyLinePlaceholder":66},[45,26184,26185,26187,26189,26191,26193,26195],{"class":47,"line":112},[45,26186,2065],{"class":92},[45,26188,96],{"class":85},[45,26190,4628],{"class":2070},[45,26192,96],{"class":85},[45,26194,3749],{"class":59},[45,26196,4348],{"class":85},[14,26198,177],{},[36,26200,26201],{"className":38,"code":11797,"language":40,"meta":41,"style":41},[18,26202,26203],{"__ignoreMap":41},[45,26204,26205],{"class":47,"line":48},[45,26206,11797],{"class":59},[31,26208,26210],{"id":26209},"when-not-to-use-lambda","When not to use lambda",[14,26212,26213,26214,8077],{},"Do not use ",[18,26215,25048],{},[195,26217,26218,26221,26224,26227],{},[198,26219,26220],{},"the logic takes more than one step",[198,26222,26223],{},"the function will be reused in many places",[198,26225,26226],{},"a named function makes the code easier to understand",[198,26228,26229,26230],{},"debugging would be easier with ",[18,26231,2757],{},[14,26233,26234,26235,26237],{},"Example where ",[18,26236,2757],{}," is better:",[36,26239,26241],{"className":38,"code":26240,"language":40,"meta":41,"style":41},"def format_name(name):\n    cleaned = name.strip()\n    return cleaned.title()\n\nprint(format_name(\"  alice \"))\n",[18,26242,26243,26256,26271,26285,26289],{"__ignoreMap":41},[45,26244,26245,26247,26250,26252,26254],{"class":47,"line":48},[45,26246,2757],{"class":2440},[45,26248,26249],{"class":2455}," format_name",[45,26251,96],{"class":85},[45,26253,3096],{"class":3082},[45,26255,2464],{"class":85},[45,26257,26258,26261,26263,26265,26267,26269],{"class":47,"line":63},[45,26259,26260],{"class":51},"    cleaned ",[45,26262,56],{"class":55},[45,26264,3083],{"class":51},[45,26266,240],{"class":85},[45,26268,24046],{"class":2070},[45,26270,2495],{"class":85},[45,26272,26273,26275,26278,26280,26283],{"class":47,"line":70},[45,26274,4681],{"class":73},[45,26276,26277],{"class":51}," cleaned",[45,26279,240],{"class":85},[45,26281,26282],{"class":2070},"title",[45,26284,2495],{"class":85},[45,26286,26287],{"class":47,"line":89},[45,26288,67],{"emptyLinePlaceholder":66},[45,26290,26291,26293,26295,26298,26300,26302,26305,26307],{"class":47,"line":112},[45,26292,2065],{"class":92},[45,26294,96],{"class":85},[45,26296,26297],{"class":2070},"format_name",[45,26299,96],{"class":85},[45,26301,100],{"class":99},[45,26303,26304],{"class":103},"  alice ",[45,26306,100],{"class":99},[45,26308,4348],{"class":85},[14,26310,177],{},[36,26312,26314],{"className":38,"code":26313,"language":40,"meta":41,"style":41},"Alice\n",[18,26315,26316],{"__ignoreMap":41},[45,26317,26318],{"class":47,"line":48},[45,26319,26313],{"class":51},[14,26321,26322],{},"This would be harder to read as a lambda because it does more than a very small one-time task.",[14,26324,26325,26326,26330,26331,14527,26333,240],{},"Also, sometimes a ",[236,26327,26329],{"href":26328},"\u002Fhow-to\u002Fhow-to-use-list-comprehensions-in-python\u002F","list comprehension"," is clearer than ",[18,26332,25604],{},[18,26334,25048],{},[14,26336,26337],{},"For example, these do the same thing:",[36,26339,26340],{"className":38,"code":25613,"language":40,"meta":41,"style":41},[18,26341,26342,26362,26394],{"__ignoreMap":41},[45,26343,26344,26346,26348,26350,26352,26354,26356,26358,26360],{"class":47,"line":48},[45,26345,8570],{"class":51},[45,26347,56],{"class":55},[45,26349,8575],{"class":85},[45,26351,8578],{"class":59},[45,26353,3079],{"class":85},[45,26355,3754],{"class":59},[45,26357,3079],{"class":85},[45,26359,777],{"class":59},[45,26361,8594],{"class":85},[45,26363,26364,26366,26368,26370,26372,26374,26376,26378,26380,26382,26384,26386,26388,26390,26392],{"class":47,"line":63},[45,26365,13939],{"class":51},[45,26367,56],{"class":55},[45,26369,14381],{"class":2109},[45,26371,96],{"class":85},[45,26373,25043],{"class":92},[45,26375,96],{"class":85},[45,26377,25048],{"class":2440},[45,26379,25051],{"class":3082},[45,26381,457],{"class":85},[45,26383,8612],{"class":2070},[45,26385,5601],{"class":55},[45,26387,3754],{"class":59},[45,26389,3079],{"class":85},[45,26391,8626],{"class":2070},[45,26393,4348],{"class":85},[45,26395,26396,26398,26400,26402],{"class":47,"line":70},[45,26397,2065],{"class":92},[45,26399,96],{"class":85},[45,26401,13968],{"class":2070},[45,26403,109],{"class":85},[36,26405,26407],{"className":38,"code":26406,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\ndoubled = [n * 2 for n in numbers]\nprint(doubled)\n",[18,26408,26409,26429,26453],{"__ignoreMap":41},[45,26410,26411,26413,26415,26417,26419,26421,26423,26425,26427],{"class":47,"line":48},[45,26412,8570],{"class":51},[45,26414,56],{"class":55},[45,26416,8575],{"class":85},[45,26418,8578],{"class":59},[45,26420,3079],{"class":85},[45,26422,3754],{"class":59},[45,26424,3079],{"class":85},[45,26426,777],{"class":59},[45,26428,8594],{"class":85},[45,26430,26431,26433,26435,26437,26439,26441,26443,26445,26447,26449,26451],{"class":47,"line":63},[45,26432,13939],{"class":51},[45,26434,56],{"class":55},[45,26436,8575],{"class":85},[45,26438,10005],{"class":51},[45,26440,5601],{"class":55},[45,26442,3754],{"class":59},[45,26444,8855],{"class":73},[45,26446,8612],{"class":51},[45,26448,8623],{"class":73},[45,26450,8626],{"class":51},[45,26452,8594],{"class":85},[45,26454,26455,26457,26459,26461],{"class":47,"line":70},[45,26456,2065],{"class":92},[45,26458,96],{"class":85},[45,26460,13968],{"class":2070},[45,26462,109],{"class":85},[14,26464,26465],{},"Many beginners find the list comprehension easier to read.",[31,26467,5903],{"id":5902},[14,26469,26470],{},"Here are some common beginner mistakes with lambda functions.",[712,26472,26474],{"id":26473},"trying-to-write-multiple-statements-inside-a-lambda","Trying to write multiple statements inside a lambda",[14,26476,26477],{},"This does not work because lambda allows only one expression.",[14,26479,165,26480,26482],{},[18,26481,2757],{}," if you need multiple steps.",[712,26484,26486],{"id":26485},"using-lambda-when-a-normal-function-would-be-clearer","Using lambda when a normal function would be clearer",[14,26488,26489],{},"Shorter code is not always better.",[14,26491,26492,26493,240],{},"If a named function helps explain the code, use ",[18,26494,2757],{},[712,26496,26498],{"id":26497},"forgetting-that-lambda-returns-one-expression-automatically","Forgetting that lambda returns one expression automatically",[14,26500,26501,26502,25265],{},"You do not write ",[18,26503,3461],{},[14,26505,9886],{},[36,26507,26508],{"className":38,"code":25323,"language":40,"meta":41,"style":41},[18,26509,26510,26532],{"__ignoreMap":41},[45,26511,26512,26514,26516,26518,26520,26522,26524,26526,26528,26530],{"class":47,"line":48},[45,26513,24964],{"class":51},[45,26515,56],{"class":55},[45,26517,24969],{"class":2440},[45,26519,24972],{"class":3082},[45,26521,3079],{"class":85},[45,26523,11115],{"class":3082},[45,26525,457],{"class":85},[45,26527,11124],{"class":51},[45,26529,8850],{"class":55},[45,26531,11129],{"class":51},[45,26533,26534,26536,26538,26540,26542,26544,26546,26548],{"class":47,"line":63},[45,26535,2065],{"class":92},[45,26537,96],{"class":85},[45,26539,11159],{"class":2070},[45,26541,96],{"class":85},[45,26543,9367],{"class":59},[45,26545,3079],{"class":85},[45,26547,777],{"class":59},[45,26549,4348],{"class":85},[712,26551,26553],{"id":26552},"confusing-lambda-syntax-with-function-calls","Confusing lambda syntax with function calls",[14,26555,26556],{},"This creates a function:",[36,26558,26560],{"className":38,"code":26559,"language":40,"meta":41,"style":41},"double = lambda x: x * 2\n",[18,26561,26562],{"__ignoreMap":41},[45,26563,26564,26566,26568,26570,26572,26574,26576,26578],{"class":47,"line":48},[45,26565,25278],{"class":51},[45,26567,56],{"class":55},[45,26569,24969],{"class":2440},[45,26571,25169],{"class":3082},[45,26573,457],{"class":85},[45,26575,1185],{"class":51},[45,26577,5601],{"class":55},[45,26579,13423],{"class":59},[14,26581,26582],{},"This calls the function:",[36,26584,26586],{"className":38,"code":26585,"language":40,"meta":41,"style":41},"print(double(5))\n",[18,26587,26588],{"__ignoreMap":41},[45,26589,26590,26592,26594,26596,26598,26600],{"class":47,"line":48},[45,26591,2065],{"class":92},[45,26593,96],{"class":85},[45,26595,25301],{"class":2070},[45,26597,96],{"class":85},[45,26599,3749],{"class":59},[45,26601,4348],{"class":85},[712,26603,1350,26605,230,26607,26609],{"id":26604},"using-map-or-filter-without-converting-to-a-list-when-needed",[18,26606,25604],{},[18,26608,25720],{}," without converting to a list when needed",[14,26611,26612,26613,1316,26615,26617],{},"In Python 3, ",[18,26614,25604],{},[18,26616,25720],{}," return iterator objects.",[14,26619,26620,26621,457],{},"If you want to see all the results at once, wrap them with ",[18,26622,26623],{},"list()",[36,26625,26627],{"className":38,"code":26626,"language":40,"meta":41,"style":41},"print(list(map(lambda n: n * 2, [1, 2, 3])))\nprint(list(filter(lambda n: n > 2, [1, 2, 3, 4])))\n",[18,26628,26629,26672],{"__ignoreMap":41},[45,26630,26631,26633,26635,26637,26639,26641,26643,26645,26647,26649,26651,26653,26655,26657,26659,26661,26663,26665,26667,26669],{"class":47,"line":48},[45,26632,2065],{"class":92},[45,26634,96],{"class":85},[45,26636,14463],{"class":2109},[45,26638,96],{"class":85},[45,26640,25043],{"class":92},[45,26642,96],{"class":85},[45,26644,25048],{"class":2440},[45,26646,25051],{"class":3082},[45,26648,457],{"class":85},[45,26650,8612],{"class":2070},[45,26652,5601],{"class":55},[45,26654,3754],{"class":59},[45,26656,3079],{"class":85},[45,26658,8575],{"class":85},[45,26660,8578],{"class":59},[45,26662,3079],{"class":85},[45,26664,3754],{"class":59},[45,26666,3079],{"class":85},[45,26668,777],{"class":59},[45,26670,26671],{"class":85},"])))\n",[45,26673,26674,26676,26678,26680,26682,26684,26686,26688,26690,26692,26694,26696,26698,26700,26702,26704,26706,26708,26710,26712,26714,26716],{"class":47,"line":63},[45,26675,2065],{"class":92},[45,26677,96],{"class":85},[45,26679,14463],{"class":2109},[45,26681,96],{"class":85},[45,26683,25770],{"class":92},[45,26685,96],{"class":85},[45,26687,25048],{"class":2440},[45,26689,25051],{"class":3082},[45,26691,457],{"class":85},[45,26693,8612],{"class":2070},[45,26695,272],{"class":55},[45,26697,3754],{"class":59},[45,26699,3079],{"class":85},[45,26701,8575],{"class":85},[45,26703,8578],{"class":59},[45,26705,3079],{"class":85},[45,26707,3754],{"class":59},[45,26709,3079],{"class":85},[45,26711,777],{"class":59},[45,26713,3079],{"class":85},[45,26715,8591],{"class":59},[45,26717,26671],{"class":85},[14,26719,26720],{},"Helpful test examples:",[36,26722,26724],{"className":38,"code":26723,"language":40,"meta":41,"style":41},"print(add(2, 3))\nprint((lambda x: x * 2)(5))\nprint(list(map(lambda n: n * 2, [1, 2, 3])))\nprint(list(filter(lambda n: n > 2, [1, 2, 3, 4])))\nprint(sorted([('a', 3), ('b', 1)], key=lambda item: item[1]))\n",[18,26725,26726,26744,26768,26810,26856],{"__ignoreMap":41},[45,26727,26728,26730,26732,26734,26736,26738,26740,26742],{"class":47,"line":48},[45,26729,2065],{"class":92},[45,26731,96],{"class":85},[45,26733,11159],{"class":2070},[45,26735,96],{"class":85},[45,26737,9367],{"class":59},[45,26739,3079],{"class":85},[45,26741,777],{"class":59},[45,26743,4348],{"class":85},[45,26745,26746,26748,26750,26752,26754,26756,26758,26760,26762,26764,26766],{"class":47,"line":63},[45,26747,2065],{"class":92},[45,26749,26029],{"class":85},[45,26751,25048],{"class":2440},[45,26753,25169],{"class":3082},[45,26755,457],{"class":85},[45,26757,1185],{"class":2070},[45,26759,5601],{"class":55},[45,26761,3754],{"class":59},[45,26763,26044],{"class":85},[45,26765,3749],{"class":59},[45,26767,4348],{"class":85},[45,26769,26770,26772,26774,26776,26778,26780,26782,26784,26786,26788,26790,26792,26794,26796,26798,26800,26802,26804,26806,26808],{"class":47,"line":70},[45,26771,2065],{"class":92},[45,26773,96],{"class":85},[45,26775,14463],{"class":2109},[45,26777,96],{"class":85},[45,26779,25043],{"class":92},[45,26781,96],{"class":85},[45,26783,25048],{"class":2440},[45,26785,25051],{"class":3082},[45,26787,457],{"class":85},[45,26789,8612],{"class":2070},[45,26791,5601],{"class":55},[45,26793,3754],{"class":59},[45,26795,3079],{"class":85},[45,26797,8575],{"class":85},[45,26799,8578],{"class":59},[45,26801,3079],{"class":85},[45,26803,3754],{"class":59},[45,26805,3079],{"class":85},[45,26807,777],{"class":59},[45,26809,26671],{"class":85},[45,26811,26812,26814,26816,26818,26820,26822,26824,26826,26828,26830,26832,26834,26836,26838,26840,26842,26844,26846,26848,26850,26852,26854],{"class":47,"line":89},[45,26813,2065],{"class":92},[45,26815,96],{"class":85},[45,26817,14463],{"class":2109},[45,26819,96],{"class":85},[45,26821,25770],{"class":92},[45,26823,96],{"class":85},[45,26825,25048],{"class":2440},[45,26827,25051],{"class":3082},[45,26829,457],{"class":85},[45,26831,8612],{"class":2070},[45,26833,272],{"class":55},[45,26835,3754],{"class":59},[45,26837,3079],{"class":85},[45,26839,8575],{"class":85},[45,26841,8578],{"class":59},[45,26843,3079],{"class":85},[45,26845,3754],{"class":59},[45,26847,3079],{"class":85},[45,26849,777],{"class":59},[45,26851,3079],{"class":85},[45,26853,8591],{"class":59},[45,26855,26671],{"class":85},[45,26857,26858,26860,26862,26865,26868,26870,26872,26874,26876,26878,26880,26882,26884,26886,26888,26890,26892,26895,26897,26899,26901,26903,26905,26907,26909,26911],{"class":47,"line":112},[45,26859,2065],{"class":92},[45,26861,96],{"class":85},[45,26863,26864],{"class":92},"sorted",[45,26866,26867],{"class":85},"([(",[45,26869,7901],{"class":99},[45,26871,236],{"class":103},[45,26873,7901],{"class":99},[45,26875,3079],{"class":85},[45,26877,777],{"class":59},[45,26879,25873],{"class":85},[45,26881,14039],{"class":85},[45,26883,7901],{"class":99},[45,26885,10384],{"class":103},[45,26887,7901],{"class":99},[45,26889,3079],{"class":85},[45,26891,9030],{"class":59},[45,26893,26894],{"class":85},")],",[45,26896,9502],{"class":6503},[45,26898,56],{"class":55},[45,26900,25048],{"class":2440},[45,26902,25927],{"class":3082},[45,26904,457],{"class":85},[45,26906,25927],{"class":2070},[45,26908,9680],{"class":85},[45,26910,8578],{"class":59},[45,26912,26913],{"class":85},"]))\n",[14,26915,17038],{},[36,26917,26919],{"className":38,"code":26918,"language":40,"meta":41,"style":41},"5\n10\n[2, 4, 6]\n[3, 4]\n[('b', 1), ('a', 3)]\n",[18,26920,26921,26925,26929,26945,26957],{"__ignoreMap":41},[45,26922,26923],{"class":47,"line":48},[45,26924,11570],{"class":59},[45,26926,26927],{"class":47,"line":63},[45,26928,13262],{"class":59},[45,26930,26931,26933,26935,26937,26939,26941,26943],{"class":47,"line":70},[45,26932,9680],{"class":85},[45,26934,9367],{"class":59},[45,26936,3079],{"class":85},[45,26938,8591],{"class":59},[45,26940,3079],{"class":85},[45,26942,9288],{"class":59},[45,26944,8594],{"class":85},[45,26946,26947,26949,26951,26953,26955],{"class":47,"line":89},[45,26948,9680],{"class":85},[45,26950,11184],{"class":59},[45,26952,3079],{"class":85},[45,26954,8591],{"class":59},[45,26956,8594],{"class":85},[45,26958,26959,26961,26963,26965,26967,26969,26971,26973,26975,26977,26979,26981,26983,26985],{"class":47,"line":112},[45,26960,25959],{"class":85},[45,26962,7901],{"class":99},[45,26964,10384],{"class":103},[45,26966,7901],{"class":99},[45,26968,3079],{"class":85},[45,26970,9030],{"class":59},[45,26972,25873],{"class":85},[45,26974,14039],{"class":85},[45,26976,7901],{"class":99},[45,26978,236],{"class":103},[45,26980,7901],{"class":99},[45,26982,3079],{"class":85},[45,26984,777],{"class":59},[45,26986,25902],{"class":85},[31,26988,2281],{"id":2280},[712,26990,26992],{"id":26991},"what-is-a-lambda-function-in-python","What is a lambda function in Python?",[14,26994,26995,26996,25144],{},"It is a short function written in one line with the ",[18,26997,25048],{},[712,26999,27001],{"id":27000},"why-is-it-called-anonymous","Why is it called anonymous?",[14,27003,27004,27005,240],{},"Because it does not need a regular function name like one created with ",[18,27006,2757],{},[712,27008,27010],{"id":27009},"can-lambda-have-multiple-lines","Can lambda have multiple lines?",[14,27012,27013],{},"No. A lambda is limited to a single expression.",[712,27015,27017],{"id":27016},"is-lambda-better-than-def","Is lambda better than def?",[14,27019,27020,27021,27023],{},"Not always. Lambda is useful for short one-time functions, but ",[18,27022,2757],{}," is usually clearer for larger logic.",[712,27025,27027],{"id":27026},"does-lambda-return-a-value","Does lambda return a value?",[14,27029,27030],{},"Yes. It automatically returns the result of its expression.",[31,27032,2342],{"id":2341},[195,27034,27035,27039,27043,27047,27053,27059,27065,27070],{},[198,27036,27037],{},[236,27038,5879],{"href":4579},[198,27040,27041],{},[236,27042,6318],{"href":4583},[198,27044,27045],{},[236,27046,6323],{"href":5884},[198,27048,27049],{},[236,27050,18104,27051,18107],{"href":25712},[18,27052,25604],{},[198,27054,27055],{},[236,27056,18104,27057,18107],{"href":25830},[18,27058,25720],{},[198,27060,27061],{},[236,27062,18104,27063,18107],{"href":26008},[18,27064,25838],{},[198,27066,27067],{},[236,27068,27069],{"href":26328},"How to use list comprehensions in Python",[198,27071,27072],{},[236,27073,6333],{"href":5820},[2367,27075,27076],{},"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 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 .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 .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":41,"searchDepth":63,"depth":63,"links":27078},[27079,27080,27081,27084,27089,27098,27099,27100,27108,27115],{"id":33,"depth":63,"text":34},{"id":25117,"depth":63,"text":25118},{"id":25216,"depth":63,"text":25217,"children":27082},[27083],{"id":25239,"depth":70,"text":25240},{"id":25443,"depth":63,"text":25444,"children":27085},[27086,27087],{"id":25473,"depth":70,"text":25474},{"id":25524,"depth":70,"text":27088},"The same logic with def",{"id":25593,"depth":63,"text":25594,"children":27090},[27091,27093,27095,27097],{"id":25600,"depth":70,"text":27092},"With map() to change each item",{"id":25717,"depth":70,"text":27094},"With filter() to keep matching items",{"id":25835,"depth":70,"text":27096},"With sorted() as a key function",{"id":26013,"depth":70,"text":26014},{"id":26064,"depth":63,"text":26065},{"id":26209,"depth":63,"text":26210},{"id":5902,"depth":63,"text":5903,"children":27101},[27102,27103,27104,27105,27106],{"id":26473,"depth":70,"text":26474},{"id":26485,"depth":70,"text":26486},{"id":26497,"depth":70,"text":26498},{"id":26552,"depth":70,"text":26553},{"id":26604,"depth":70,"text":27107},"Using map() or filter() without converting to a list when needed",{"id":2280,"depth":63,"text":2281,"children":27109},[27110,27111,27112,27113,27114],{"id":26991,"depth":70,"text":26992},{"id":27000,"depth":70,"text":27001},{"id":27009,"depth":70,"text":27010},{"id":27016,"depth":70,"text":27017},{"id":27026,"depth":70,"text":27027},{"id":2341,"depth":63,"text":2342},"Master lambda functions in python explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Flambda-functions-in-python-explained",{"title":24916,"description":27116},"learn\u002Flambda-functions-in-python-explained","lPgM0ASvepsKmrigeZL4hqxwUge2X6SKD9-T10nYFDo",{"id":27123,"title":27124,"body":27125,"description":29382,"extension":2399,"meta":29383,"navigation":66,"path":9238,"seo":29384,"stem":29385,"__hash__":29386},"content\u002Flearn\u002Flist-comprehensions-in-python-explained.md","List Comprehensions in Python Explained",{"type":7,"value":27126,"toc":29343},[27127,27130,27133,27136,27147,27153,27155,27222,27224,27253,27256,27260,27263,27277,27280,27338,27340,27360,27367,27369,27372,27394,27397,27414,27417,27435,27437,27500,27502,27527,27529,27547,27553,27557,27560,27567,27653,27655,27680,27684,27751,27753,27777,27780,27785,27789,27792,27795,27820,27825,27829,27892,27894,27923,27927,28016,28018,28051,28055,28135,28137,28154,28161,28165,28168,28171,28175,28238,28240,28264,28268,28347,28349,28383,28387,28446,28448,28472,28475,28479,28482,28508,28510,28568,28571,28595,28597,28618,28621,28625,28628,28642,28645,28662,28666,28744,28746,28767,28771,28888,28890,28907,28915,28921,28923,28925,28929,28932,28935,28968,28970,29006,29010,29013,29026,29028,29073,29075,29112,29118,29121,29124,29185,29187,29221,29232,29236,29239,29242,29252,29254,29258,29261,29265,29268,29272,29275,29300,29304,29310,29312,29340],[10,27128,27124],{"id":27129},"list-comprehensions-in-python-explained",[14,27131,27132],{},"A list comprehension is a short way to create a new list in Python.",[14,27134,27135],{},"It lets you:",[195,27137,27138,27141,27144],{},[198,27139,27140],{},"loop through values",[198,27142,27143],{},"optionally filter them",[198,27145,27146],{},"build a new list in one expression",[14,27148,27149,27150,27152],{},"List comprehensions are useful when you want to transform or filter data in a clear, compact way. They are best for simple cases. If the logic becomes hard to read, a normal ",[18,27151,7895],{}," loop is usually the better choice.",[31,27154,34],{"id":33},[36,27156,27158],{"className":38,"code":27157,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4, 5]\nsquares = [n * n for n in numbers]\nprint(squares)\n",[18,27159,27160,27188,27212],{"__ignoreMap":41},[45,27161,27162,27164,27166,27168,27170,27172,27174,27176,27178,27180,27182,27184,27186],{"class":47,"line":48},[45,27163,8570],{"class":51},[45,27165,56],{"class":55},[45,27167,8575],{"class":85},[45,27169,8578],{"class":59},[45,27171,3079],{"class":85},[45,27173,3754],{"class":59},[45,27175,3079],{"class":85},[45,27177,777],{"class":59},[45,27179,3079],{"class":85},[45,27181,8591],{"class":59},[45,27183,3079],{"class":85},[45,27185,1222],{"class":59},[45,27187,8594],{"class":85},[45,27189,27190,27192,27194,27196,27198,27200,27202,27204,27206,27208,27210],{"class":47,"line":63},[45,27191,8599],{"class":51},[45,27193,56],{"class":55},[45,27195,8575],{"class":85},[45,27197,10005],{"class":51},[45,27199,5601],{"class":55},[45,27201,8612],{"class":51},[45,27203,7895],{"class":73},[45,27205,8612],{"class":51},[45,27207,8623],{"class":73},[45,27209,8626],{"class":51},[45,27211,8594],{"class":85},[45,27213,27214,27216,27218,27220],{"class":47,"line":70},[45,27215,2065],{"class":92},[45,27217,96],{"class":85},[45,27219,8638],{"class":2070},[45,27221,109],{"class":85},[14,27223,177],{},[36,27225,27227],{"className":38,"code":27226,"language":40,"meta":41,"style":41},"[1, 4, 9, 16, 25]\n",[18,27228,27229],{"__ignoreMap":41},[45,27230,27231,27233,27235,27237,27239,27241,27243,27245,27247,27249,27251],{"class":47,"line":48},[45,27232,9680],{"class":85},[45,27234,8578],{"class":59},[45,27236,3079],{"class":85},[45,27238,8591],{"class":59},[45,27240,3079],{"class":85},[45,27242,9047],{"class":59},[45,27244,3079],{"class":85},[45,27246,9056],{"class":59},[45,27248,3079],{"class":85},[45,27250,303],{"class":59},[45,27252,8594],{"class":85},[14,27254,27255],{},"Use a list comprehension when you want to build a new list from another iterable in one clear line.",[31,27257,27259],{"id":27258},"what-a-list-comprehension-is","What a list comprehension is",[14,27261,27262],{},"A list comprehension is:",[195,27264,27265,27268,27271,27274],{},[198,27266,27267],{},"a short way to create a new list",[198,27269,27270],{},"a combination of looping and optional filtering",[198,27272,27273],{},"something that returns a real list object",[198,27275,27276],{},"best for simple transformations and simple filtering",[14,27278,27279],{},"For example, if you want a list of doubled numbers, you can build it directly:",[36,27281,27282],{"className":38,"code":26406,"language":40,"meta":41,"style":41},[18,27283,27284,27304,27328],{"__ignoreMap":41},[45,27285,27286,27288,27290,27292,27294,27296,27298,27300,27302],{"class":47,"line":48},[45,27287,8570],{"class":51},[45,27289,56],{"class":55},[45,27291,8575],{"class":85},[45,27293,8578],{"class":59},[45,27295,3079],{"class":85},[45,27297,3754],{"class":59},[45,27299,3079],{"class":85},[45,27301,777],{"class":59},[45,27303,8594],{"class":85},[45,27305,27306,27308,27310,27312,27314,27316,27318,27320,27322,27324,27326],{"class":47,"line":63},[45,27307,13939],{"class":51},[45,27309,56],{"class":55},[45,27311,8575],{"class":85},[45,27313,10005],{"class":51},[45,27315,5601],{"class":55},[45,27317,3754],{"class":59},[45,27319,8855],{"class":73},[45,27321,8612],{"class":51},[45,27323,8623],{"class":73},[45,27325,8626],{"class":51},[45,27327,8594],{"class":85},[45,27329,27330,27332,27334,27336],{"class":47,"line":70},[45,27331,2065],{"class":92},[45,27333,96],{"class":85},[45,27335,13968],{"class":2070},[45,27337,109],{"class":85},[14,27339,177],{},[36,27341,27342],{"className":38,"code":25682,"language":40,"meta":41,"style":41},[18,27343,27344],{"__ignoreMap":41},[45,27345,27346,27348,27350,27352,27354,27356,27358],{"class":47,"line":48},[45,27347,9680],{"class":85},[45,27349,9367],{"class":59},[45,27351,3079],{"class":85},[45,27353,8591],{"class":59},[45,27355,3079],{"class":85},[45,27357,9288],{"class":59},[45,27359,8594],{"class":85},[14,27361,27362,27363,240],{},"If you are new to lists, see ",[236,27364,27366],{"href":27365},"\u002Flearn\u002Fpython-lists-explained-beginner-guide\u002F","Python lists explained",[31,27368,8707],{"id":8706},[14,27370,27371],{},"The basic pattern is:",[36,27373,27375],{"className":38,"code":27374,"language":40,"meta":41,"style":41},"[expression for item in iterable]\n",[18,27376,27377],{"__ignoreMap":41},[45,27378,27379,27381,27384,27386,27388,27390,27392],{"class":47,"line":48},[45,27380,9680],{"class":85},[45,27382,27383],{"class":51},"expression ",[45,27385,7895],{"class":73},[45,27387,8732],{"class":51},[45,27389,8623],{"class":73},[45,27391,8737],{"class":51},[45,27393,8594],{"class":85},[14,27395,27396],{},"Here is what each part means:",[195,27398,27399,27404,27409],{},[198,27400,27401,27403],{},[18,27402,25253],{}," is the value that gets added to the new list",[198,27405,27406,27408],{},[18,27407,23670],{}," is each value taken one at a time",[198,27410,27411,27413],{},[18,27412,22097],{}," is the thing you loop over",[14,27415,27416],{},"An iterable can be:",[195,27418,27419,27422,27425,27428,27432],{},[198,27420,27421],{},"a list",[198,27423,27424],{},"a tuple",[198,27426,27427],{},"a string",[198,27429,11541,27430],{},[18,27431,22305],{},[198,27433,27434],{},"other objects you can loop through",[14,27436,466],{},[36,27438,27440],{"className":38,"code":27439,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\nresult = [n + 1 for n in numbers]\nprint(result)\n",[18,27441,27442,27466,27490],{"__ignoreMap":41},[45,27443,27444,27446,27448,27450,27452,27454,27456,27458,27460,27462,27464],{"class":47,"line":48},[45,27445,8570],{"class":51},[45,27447,56],{"class":55},[45,27449,8575],{"class":85},[45,27451,8578],{"class":59},[45,27453,3079],{"class":85},[45,27455,3754],{"class":59},[45,27457,3079],{"class":85},[45,27459,777],{"class":59},[45,27461,3079],{"class":85},[45,27463,8591],{"class":59},[45,27465,8594],{"class":85},[45,27467,27468,27470,27472,27474,27476,27478,27480,27482,27484,27486,27488],{"class":47,"line":63},[45,27469,3595],{"class":51},[45,27471,56],{"class":55},[45,27473,8575],{"class":85},[45,27475,10005],{"class":51},[45,27477,8850],{"class":55},[45,27479,9030],{"class":59},[45,27481,8855],{"class":73},[45,27483,8612],{"class":51},[45,27485,8623],{"class":73},[45,27487,8626],{"class":51},[45,27489,8594],{"class":85},[45,27491,27492,27494,27496,27498],{"class":47,"line":70},[45,27493,2065],{"class":92},[45,27495,96],{"class":85},[45,27497,3620],{"class":2070},[45,27499,109],{"class":85},[14,27501,177],{},[36,27503,27505],{"className":38,"code":27504,"language":40,"meta":41,"style":41},"[2, 3, 4, 5]\n",[18,27506,27507],{"__ignoreMap":41},[45,27508,27509,27511,27513,27515,27517,27519,27521,27523,27525],{"class":47,"line":48},[45,27510,9680],{"class":85},[45,27512,9367],{"class":59},[45,27514,3079],{"class":85},[45,27516,777],{"class":59},[45,27518,3079],{"class":85},[45,27520,8591],{"class":59},[45,27522,3079],{"class":85},[45,27524,1222],{"class":59},[45,27526,8594],{"class":85},[14,27528,6570],{},[195,27530,27531,27536,27541],{},[198,27532,27533,27535],{},[18,27534,9069],{}," is the iterable",[198,27537,27538,27540],{},[18,27539,8607],{}," is each item",[198,27542,27543,27546],{},[18,27544,27545],{},"n + 1"," is the expression",[14,27548,3259,27549,3263,27551,240],{},[18,27550,22305],{},[236,27552,23972],{"href":23971},[31,27554,27556],{"id":27555},"compare-with-a-normal-for-loop","Compare with a normal for loop",[14,27558,27559],{},"It helps to see the longer version first.",[712,27561,27563,27564,27566],{"id":27562},"normal-for-loop","Normal ",[18,27565,7895],{}," loop",[36,27568,27570],{"className":38,"code":27569,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\nsquares = []\n\nfor n in numbers:\n    squares.append(n * n)\n\nprint(squares)\n",[18,27571,27572,27596,27605,27609,27621,27639,27643],{"__ignoreMap":41},[45,27573,27574,27576,27578,27580,27582,27584,27586,27588,27590,27592,27594],{"class":47,"line":48},[45,27575,8570],{"class":51},[45,27577,56],{"class":55},[45,27579,8575],{"class":85},[45,27581,8578],{"class":59},[45,27583,3079],{"class":85},[45,27585,3754],{"class":59},[45,27587,3079],{"class":85},[45,27589,777],{"class":59},[45,27591,3079],{"class":85},[45,27593,8591],{"class":59},[45,27595,8594],{"class":85},[45,27597,27598,27600,27602],{"class":47,"line":63},[45,27599,8599],{"class":51},[45,27601,56],{"class":55},[45,27603,27604],{"class":85}," []\n",[45,27606,27607],{"class":47,"line":70},[45,27608,67],{"emptyLinePlaceholder":66},[45,27610,27611,27613,27615,27617,27619],{"class":47,"line":89},[45,27612,7895],{"class":73},[45,27614,8612],{"class":51},[45,27616,8623],{"class":73},[45,27618,8626],{"class":51},[45,27620,86],{"class":85},[45,27622,27623,27625,27627,27629,27631,27633,27635,27637],{"class":47,"line":112},[45,27624,9677],{"class":51},[45,27626,240],{"class":85},[45,27628,21025],{"class":2070},[45,27630,96],{"class":85},[45,27632,10005],{"class":2070},[45,27634,5601],{"class":55},[45,27636,25051],{"class":2070},[45,27638,109],{"class":85},[45,27640,27641],{"class":47,"line":126},[45,27642,67],{"emptyLinePlaceholder":66},[45,27644,27645,27647,27649,27651],{"class":47,"line":142},[45,27646,2065],{"class":92},[45,27648,96],{"class":85},[45,27650,8638],{"class":2070},[45,27652,109],{"class":85},[14,27654,177],{},[36,27656,27658],{"className":38,"code":27657,"language":40,"meta":41,"style":41},"[1, 4, 9, 16]\n",[18,27659,27660],{"__ignoreMap":41},[45,27661,27662,27664,27666,27668,27670,27672,27674,27676,27678],{"class":47,"line":48},[45,27663,9680],{"class":85},[45,27665,8578],{"class":59},[45,27667,3079],{"class":85},[45,27669,8591],{"class":59},[45,27671,3079],{"class":85},[45,27673,9047],{"class":59},[45,27675,3079],{"class":85},[45,27677,9056],{"class":59},[45,27679,8594],{"class":85},[712,27681,27683],{"id":27682},"matching-list-comprehension","Matching list comprehension",[36,27685,27687],{"className":38,"code":27686,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\nsquares = [n * n for n in numbers]\n\nprint(squares)\n",[18,27688,27689,27713,27737,27741],{"__ignoreMap":41},[45,27690,27691,27693,27695,27697,27699,27701,27703,27705,27707,27709,27711],{"class":47,"line":48},[45,27692,8570],{"class":51},[45,27694,56],{"class":55},[45,27696,8575],{"class":85},[45,27698,8578],{"class":59},[45,27700,3079],{"class":85},[45,27702,3754],{"class":59},[45,27704,3079],{"class":85},[45,27706,777],{"class":59},[45,27708,3079],{"class":85},[45,27710,8591],{"class":59},[45,27712,8594],{"class":85},[45,27714,27715,27717,27719,27721,27723,27725,27727,27729,27731,27733,27735],{"class":47,"line":63},[45,27716,8599],{"class":51},[45,27718,56],{"class":55},[45,27720,8575],{"class":85},[45,27722,10005],{"class":51},[45,27724,5601],{"class":55},[45,27726,8612],{"class":51},[45,27728,7895],{"class":73},[45,27730,8612],{"class":51},[45,27732,8623],{"class":73},[45,27734,8626],{"class":51},[45,27736,8594],{"class":85},[45,27738,27739],{"class":47,"line":70},[45,27740,67],{"emptyLinePlaceholder":66},[45,27742,27743,27745,27747,27749],{"class":47,"line":89},[45,27744,2065],{"class":92},[45,27746,96],{"class":85},[45,27748,8638],{"class":2070},[45,27750,109],{"class":85},[14,27752,177],{},[36,27754,27755],{"className":38,"code":27657,"language":40,"meta":41,"style":41},[18,27756,27757],{"__ignoreMap":41},[45,27758,27759,27761,27763,27765,27767,27769,27771,27773,27775],{"class":47,"line":48},[45,27760,9680],{"class":85},[45,27762,8578],{"class":59},[45,27764,3079],{"class":85},[45,27766,8591],{"class":59},[45,27768,3079],{"class":85},[45,27770,9047],{"class":59},[45,27772,3079],{"class":85},[45,27774,9056],{"class":59},[45,27776,8594],{"class":85},[14,27778,27779],{},"Both versions produce the same result.",[14,27781,27782,27783,240],{},"The list comprehension is shorter, but shorter is not always better. Use it when it stays easy to read. If you need help with loops first, see ",[236,27784,23611],{"href":23610},[31,27786,27788],{"id":27787},"using-conditions","Using conditions",[14,27790,27791],{},"You can add a condition to keep only some items.",[14,27793,27794],{},"The pattern is:",[36,27796,27798],{"className":38,"code":27797,"language":40,"meta":41,"style":41},"[expression for item in iterable if condition]\n",[18,27799,27800],{"__ignoreMap":41},[45,27801,27802,27804,27806,27808,27810,27812,27814,27816,27818],{"class":47,"line":48},[45,27803,9680],{"class":85},[45,27805,27383],{"class":51},[45,27807,7895],{"class":73},[45,27809,8732],{"class":51},[45,27811,8623],{"class":73},[45,27813,8788],{"class":51},[45,27815,20],{"class":73},[45,27817,408],{"class":51},[45,27819,8594],{"class":85},[14,27821,1732,27822,27824],{},[18,27823,20],{}," part filters the items before they are added to the new list.",[712,27826,27828],{"id":27827},"example-keep-only-even-numbers","Example: keep only even numbers",[36,27830,27832],{"className":38,"code":27831,"language":40,"meta":41,"style":41},"numbers = range(10)\nevens = [n for n in numbers if n % 2 == 0]\nprint(evens)\n",[18,27833,27834,27848,27881],{"__ignoreMap":41},[45,27835,27836,27838,27840,27842,27844,27846],{"class":47,"line":48},[45,27837,8570],{"class":51},[45,27839,56],{"class":55},[45,27841,10451],{"class":92},[45,27843,96],{"class":85},[45,27845,11786],{"class":59},[45,27847,109],{"class":85},[45,27849,27850,27853,27855,27857,27859,27861,27863,27865,27867,27869,27871,27873,27875,27877,27879],{"class":47,"line":63},[45,27851,27852],{"class":51},"evens ",[45,27854,56],{"class":55},[45,27856,8575],{"class":85},[45,27858,10005],{"class":51},[45,27860,7895],{"class":73},[45,27862,8612],{"class":51},[45,27864,8623],{"class":73},[45,27866,9322],{"class":51},[45,27868,20],{"class":73},[45,27870,8612],{"class":51},[45,27872,9329],{"class":55},[45,27874,3754],{"class":59},[45,27876,9334],{"class":55},[45,27878,749],{"class":59},[45,27880,8594],{"class":85},[45,27882,27883,27885,27887,27890],{"class":47,"line":70},[45,27884,2065],{"class":92},[45,27886,96],{"class":85},[45,27888,27889],{"class":2070},"evens",[45,27891,109],{"class":85},[14,27893,177],{},[36,27895,27897],{"className":38,"code":27896,"language":40,"meta":41,"style":41},"[0, 2, 4, 6, 8]\n",[18,27898,27899],{"__ignoreMap":41},[45,27900,27901,27903,27905,27907,27909,27911,27913,27915,27917,27919,27921],{"class":47,"line":48},[45,27902,9680],{"class":85},[45,27904,10482],{"class":59},[45,27906,3079],{"class":85},[45,27908,3754],{"class":59},[45,27910,3079],{"class":85},[45,27912,8591],{"class":59},[45,27914,3079],{"class":85},[45,27916,9288],{"class":59},[45,27918,3079],{"class":85},[45,27920,1254],{"class":59},[45,27922,8594],{"class":85},[712,27924,27926],{"id":27925},"example-keep-only-non-empty-strings","Example: keep only non-empty strings",[36,27928,27930],{"className":38,"code":27929,"language":40,"meta":41,"style":41},"words = [\"apple\", \"\", \"banana\", \"\", \"grape\"]\nnon_empty = [word for word in words if word != \"\"]\nprint(non_empty)\n",[18,27931,27932,27974,28005],{"__ignoreMap":41},[45,27933,27934,27936,27938,27940,27942,27944,27946,27948,27951,27953,27955,27957,27959,27961,27963,27965,27967,27970,27972],{"class":47,"line":48},[45,27935,9088],{"class":51},[45,27937,56],{"class":55},[45,27939,8575],{"class":85},[45,27941,100],{"class":99},[45,27943,9443],{"class":103},[45,27945,100],{"class":99},[45,27947,3079],{"class":85},[45,27949,27950],{"class":99}," \"\"",[45,27952,3079],{"class":85},[45,27954,1565],{"class":99},[45,27956,9456],{"class":103},[45,27958,100],{"class":99},[45,27960,3079],{"class":85},[45,27962,27950],{"class":99},[45,27964,3079],{"class":85},[45,27966,1565],{"class":99},[45,27968,27969],{"class":103},"grape",[45,27971,100],{"class":99},[45,27973,8594],{"class":85},[45,27975,27976,27979,27981,27983,27986,27988,27990,27992,27995,27997,27999,28001,28003],{"class":47,"line":63},[45,27977,27978],{"class":51},"non_empty ",[45,27980,56],{"class":55},[45,27982,8575],{"class":85},[45,27984,27985],{"class":51},"word ",[45,27987,7895],{"class":73},[45,27989,9151],{"class":51},[45,27991,8623],{"class":73},[45,27993,27994],{"class":51}," words ",[45,27996,20],{"class":73},[45,27998,9151],{"class":51},[45,28000,1135],{"class":55},[45,28002,27950],{"class":99},[45,28004,8594],{"class":85},[45,28006,28007,28009,28011,28014],{"class":47,"line":70},[45,28008,2065],{"class":92},[45,28010,96],{"class":85},[45,28012,28013],{"class":2070},"non_empty",[45,28015,109],{"class":85},[14,28017,177],{},[36,28019,28021],{"className":38,"code":28020,"language":40,"meta":41,"style":41},"['apple', 'banana', 'grape']\n",[18,28022,28023],{"__ignoreMap":41},[45,28024,28025,28027,28029,28031,28033,28035,28037,28039,28041,28043,28045,28047,28049],{"class":47,"line":48},[45,28026,9680],{"class":85},[45,28028,7901],{"class":99},[45,28030,9443],{"class":103},[45,28032,7901],{"class":99},[45,28034,3079],{"class":85},[45,28036,9199],{"class":99},[45,28038,9456],{"class":103},[45,28040,7901],{"class":99},[45,28042,3079],{"class":85},[45,28044,9199],{"class":99},[45,28046,27969],{"class":103},[45,28048,7901],{"class":99},[45,28050,8594],{"class":85},[712,28052,28054],{"id":28053},"example-keep-only-positive-values","Example: keep only positive values",[36,28056,28058],{"className":38,"code":28057,"language":40,"meta":41,"style":41},"values = [-2, 5, 0, 7, -1]\npositive = [v for v in values if v > 0]\nprint(positive)\n",[18,28059,28060,28094,28125],{"__ignoreMap":41},[45,28061,28062,28064,28066,28068,28070,28072,28074,28076,28078,28080,28082,28085,28087,28090,28092],{"class":47,"line":48},[45,28063,10571],{"class":51},[45,28065,56],{"class":55},[45,28067,8575],{"class":85},[45,28069,218],{"class":55},[45,28071,9367],{"class":59},[45,28073,3079],{"class":85},[45,28075,1222],{"class":59},[45,28077,3079],{"class":85},[45,28079,749],{"class":59},[45,28081,3079],{"class":85},[45,28083,28084],{"class":59}," 7",[45,28086,3079],{"class":85},[45,28088,28089],{"class":55}," -",[45,28091,8578],{"class":59},[45,28093,8594],{"class":85},[45,28095,28096,28099,28101,28103,28106,28108,28110,28112,28115,28117,28119,28121,28123],{"class":47,"line":63},[45,28097,28098],{"class":51},"positive ",[45,28100,56],{"class":55},[45,28102,8575],{"class":85},[45,28104,28105],{"class":51},"v ",[45,28107,7895],{"class":73},[45,28109,10617],{"class":51},[45,28111,8623],{"class":73},[45,28113,28114],{"class":51}," values ",[45,28116,20],{"class":73},[45,28118,10617],{"class":51},[45,28120,272],{"class":55},[45,28122,749],{"class":59},[45,28124,8594],{"class":85},[45,28126,28127,28129,28131,28133],{"class":47,"line":70},[45,28128,2065],{"class":92},[45,28130,96],{"class":85},[45,28132,1509],{"class":2070},[45,28134,109],{"class":85},[14,28136,177],{},[36,28138,28140],{"className":38,"code":28139,"language":40,"meta":41,"style":41},"[5, 7]\n",[18,28141,28142],{"__ignoreMap":41},[45,28143,28144,28146,28148,28150,28152],{"class":47,"line":48},[45,28145,9680],{"class":85},[45,28147,3749],{"class":59},[45,28149,3079],{"class":85},[45,28151,28084],{"class":59},[45,28153,8594],{"class":85},[14,28155,28156,28157,240],{},"If you want more filtering examples, see ",[236,28158,28160],{"href":28159},"\u002Fhow-to\u002Fhow-to-filter-a-list-in-python\u002F","how to filter a list in Python",[31,28162,28164],{"id":28163},"changing-each-item","Changing each item",[14,28166,28167],{},"The expression part is where you change each value.",[14,28169,28170],{},"This is useful for simple one-step transformations.",[712,28172,28174],{"id":28173},"square-numbers","Square numbers",[36,28176,28178],{"className":38,"code":28177,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4]\nsquares = [n * n for n in numbers]\nprint(squares)\n",[18,28179,28180,28204,28228],{"__ignoreMap":41},[45,28181,28182,28184,28186,28188,28190,28192,28194,28196,28198,28200,28202],{"class":47,"line":48},[45,28183,8570],{"class":51},[45,28185,56],{"class":55},[45,28187,8575],{"class":85},[45,28189,8578],{"class":59},[45,28191,3079],{"class":85},[45,28193,3754],{"class":59},[45,28195,3079],{"class":85},[45,28197,777],{"class":59},[45,28199,3079],{"class":85},[45,28201,8591],{"class":59},[45,28203,8594],{"class":85},[45,28205,28206,28208,28210,28212,28214,28216,28218,28220,28222,28224,28226],{"class":47,"line":63},[45,28207,8599],{"class":51},[45,28209,56],{"class":55},[45,28211,8575],{"class":85},[45,28213,10005],{"class":51},[45,28215,5601],{"class":55},[45,28217,8612],{"class":51},[45,28219,7895],{"class":73},[45,28221,8612],{"class":51},[45,28223,8623],{"class":73},[45,28225,8626],{"class":51},[45,28227,8594],{"class":85},[45,28229,28230,28232,28234,28236],{"class":47,"line":70},[45,28231,2065],{"class":92},[45,28233,96],{"class":85},[45,28235,8638],{"class":2070},[45,28237,109],{"class":85},[14,28239,177],{},[36,28241,28242],{"className":38,"code":27657,"language":40,"meta":41,"style":41},[18,28243,28244],{"__ignoreMap":41},[45,28245,28246,28248,28250,28252,28254,28256,28258,28260,28262],{"class":47,"line":48},[45,28247,9680],{"class":85},[45,28249,8578],{"class":59},[45,28251,3079],{"class":85},[45,28253,8591],{"class":59},[45,28255,3079],{"class":85},[45,28257,9047],{"class":59},[45,28259,3079],{"class":85},[45,28261,9056],{"class":59},[45,28263,8594],{"class":85},[712,28265,28267],{"id":28266},"convert-strings-to-lowercase","Convert strings to lowercase",[36,28269,28271],{"className":38,"code":28270,"language":40,"meta":41,"style":41},"words = [\"PYTHON\", \"List\", \"Comprehension\"]\nlowercase_words = [word.lower() for word in words]\nprint(lowercase_words)\n",[18,28272,28273,28308,28336],{"__ignoreMap":41},[45,28274,28275,28277,28279,28281,28283,28286,28288,28290,28292,28295,28297,28299,28301,28304,28306],{"class":47,"line":48},[45,28276,9088],{"class":51},[45,28278,56],{"class":55},[45,28280,8575],{"class":85},[45,28282,100],{"class":99},[45,28284,28285],{"class":103},"PYTHON",[45,28287,100],{"class":99},[45,28289,3079],{"class":85},[45,28291,1565],{"class":99},[45,28293,28294],{"class":103},"List",[45,28296,100],{"class":99},[45,28298,3079],{"class":85},[45,28300,1565],{"class":99},[45,28302,28303],{"class":103},"Comprehension",[45,28305,100],{"class":99},[45,28307,8594],{"class":85},[45,28309,28310,28313,28315,28317,28319,28321,28324,28326,28328,28330,28332,28334],{"class":47,"line":63},[45,28311,28312],{"class":51},"lowercase_words ",[45,28314,56],{"class":55},[45,28316,8575],{"class":85},[45,28318,9134],{"class":51},[45,28320,240],{"class":85},[45,28322,28323],{"class":2070},"lower",[45,28325,3772],{"class":85},[45,28327,8855],{"class":73},[45,28329,9151],{"class":51},[45,28331,8623],{"class":73},[45,28333,9156],{"class":51},[45,28335,8594],{"class":85},[45,28337,28338,28340,28342,28345],{"class":47,"line":70},[45,28339,2065],{"class":92},[45,28341,96],{"class":85},[45,28343,28344],{"class":2070},"lowercase_words",[45,28346,109],{"class":85},[14,28348,177],{},[36,28350,28352],{"className":38,"code":28351,"language":40,"meta":41,"style":41},"['python', 'list', 'comprehension']\n",[18,28353,28354],{"__ignoreMap":41},[45,28355,28356,28358,28360,28362,28364,28366,28368,28370,28372,28374,28376,28379,28381],{"class":47,"line":48},[45,28357,9680],{"class":85},[45,28359,7901],{"class":99},[45,28361,40],{"class":103},[45,28363,7901],{"class":99},[45,28365,3079],{"class":85},[45,28367,9199],{"class":99},[45,28369,14463],{"class":103},[45,28371,7901],{"class":99},[45,28373,3079],{"class":85},[45,28375,9199],{"class":99},[45,28377,28378],{"class":103},"comprehension",[45,28380,7901],{"class":99},[45,28382,8594],{"class":85},[712,28384,28386],{"id":28385},"add-1-to-each-number","Add 1 to each number",[36,28388,28390],{"className":38,"code":28389,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\nresult = [n + 1 for n in numbers]\nprint(result)\n",[18,28391,28392,28412,28436],{"__ignoreMap":41},[45,28393,28394,28396,28398,28400,28402,28404,28406,28408,28410],{"class":47,"line":48},[45,28395,8570],{"class":51},[45,28397,56],{"class":55},[45,28399,8575],{"class":85},[45,28401,11786],{"class":59},[45,28403,3079],{"class":85},[45,28405,10503],{"class":59},[45,28407,3079],{"class":85},[45,28409,10512],{"class":59},[45,28411,8594],{"class":85},[45,28413,28414,28416,28418,28420,28422,28424,28426,28428,28430,28432,28434],{"class":47,"line":63},[45,28415,3595],{"class":51},[45,28417,56],{"class":55},[45,28419,8575],{"class":85},[45,28421,10005],{"class":51},[45,28423,8850],{"class":55},[45,28425,9030],{"class":59},[45,28427,8855],{"class":73},[45,28429,8612],{"class":51},[45,28431,8623],{"class":73},[45,28433,8626],{"class":51},[45,28435,8594],{"class":85},[45,28437,28438,28440,28442,28444],{"class":47,"line":70},[45,28439,2065],{"class":92},[45,28441,96],{"class":85},[45,28443,3620],{"class":2070},[45,28445,109],{"class":85},[14,28447,177],{},[36,28449,28451],{"className":38,"code":28450,"language":40,"meta":41,"style":41},"[11, 21, 31]\n",[18,28452,28453],{"__ignoreMap":41},[45,28454,28455,28457,28460,28462,28465,28467,28470],{"class":47,"line":48},[45,28456,9680],{"class":85},[45,28458,28459],{"class":59},"11",[45,28461,3079],{"class":85},[45,28463,28464],{"class":59}," 21",[45,28466,3079],{"class":85},[45,28468,28469],{"class":59}," 31",[45,28471,8594],{"class":85},[14,28473,28474],{},"These examples are simple and readable. That is the best use case for list comprehensions.",[31,28476,28478],{"id":28477},"reading-list-comprehensions-step-by-step","Reading list comprehensions step by step",[14,28480,28481],{},"A list comprehension can look confusing at first. A good way to read it is in this order:",[346,28483,28484,28493,28502],{},[198,28485,28486,28487,28489,28490,28492],{},"Start with the ",[18,28488,7895],{}," part",[7343,28491],{},"\nAsk: where do the values come from?",[198,28494,28495,28496,28498,28499,28501],{},"Check the ",[18,28497,20],{}," part, if there is one",[7343,28500],{},"\nAsk: which values are kept?",[198,28503,28504,28505,28507],{},"Read the expression at the beginning",[7343,28506],{},"\nAsk: what gets added to the new list?",[14,28509,466],{},[36,28511,28513],{"className":38,"code":28512,"language":40,"meta":41,"style":41},"result = [n * 2 for n in range(6) if n % 2 == 0]\nprint(result)\n",[18,28514,28515,28558],{"__ignoreMap":41},[45,28516,28517,28519,28521,28523,28525,28527,28529,28531,28533,28535,28537,28539,28542,28544,28546,28548,28550,28552,28554,28556],{"class":47,"line":48},[45,28518,3595],{"class":51},[45,28520,56],{"class":55},[45,28522,8575],{"class":85},[45,28524,10005],{"class":51},[45,28526,5601],{"class":55},[45,28528,3754],{"class":59},[45,28530,8855],{"class":73},[45,28532,8612],{"class":51},[45,28534,8623],{"class":73},[45,28536,10451],{"class":92},[45,28538,96],{"class":85},[45,28540,28541],{"class":59},"6",[45,28543,9146],{"class":85},[45,28545,9521],{"class":73},[45,28547,8612],{"class":51},[45,28549,9329],{"class":55},[45,28551,3754],{"class":59},[45,28553,9334],{"class":55},[45,28555,749],{"class":59},[45,28557,8594],{"class":85},[45,28559,28560,28562,28564,28566],{"class":47,"line":63},[45,28561,2065],{"class":92},[45,28563,96],{"class":85},[45,28565,3620],{"class":2070},[45,28567,109],{"class":85},[14,28569,28570],{},"Read it step by step:",[195,28572,28573,28583,28589],{},[198,28574,28575,28578,28579,7076,28581],{},[18,28576,28577],{},"for n in range(6)"," means values come from ",[18,28580,10482],{},[18,28582,3749],{},[198,28584,28585,28588],{},[18,28586,28587],{},"if n % 2 == 0"," means keep only even numbers",[198,28590,28591,28594],{},[18,28592,28593],{},"n * 2"," means double each remaining number",[14,28596,177],{},[36,28598,28600],{"className":38,"code":28599,"language":40,"meta":41,"style":41},"[0, 4, 8]\n",[18,28601,28602],{"__ignoreMap":41},[45,28603,28604,28606,28608,28610,28612,28614,28616],{"class":47,"line":48},[45,28605,9680],{"class":85},[45,28607,10482],{"class":59},[45,28609,3079],{"class":85},[45,28611,8591],{"class":59},[45,28613,3079],{"class":85},[45,28615,1254],{"class":59},[45,28617,8594],{"class":85},[14,28619,28620],{},"If a comprehension feels hard to read, rewrite it as a normal loop. That is a good habit for beginners.",[31,28622,28624],{"id":28623},"when-to-use-them-and-when-not-to","When to use them and when not to",[14,28626,28627],{},"Use list comprehensions when:",[195,28629,28630,28633,28636,28639],{},[198,28631,28632],{},"you are building a new list",[198,28634,28635],{},"the task is short and clear",[198,28637,28638],{},"you are doing a simple transformation",[198,28640,28641],{},"you are doing simple filtering",[14,28643,28644],{},"Do not use them when:",[195,28646,28647,28650,28653,28659],{},[198,28648,28649],{},"the logic is hard to read",[198,28651,28652],{},"you need many steps",[198,28654,28655,28656,28658],{},"you need complex ",[18,28657,20],{}," logic",[198,28660,28661],{},"a nested list comprehension makes the code confusing",[712,28663,28665],{"id":28664},"good-use","Good use",[36,28667,28669],{"className":38,"code":28668,"language":40,"meta":41,"style":41},"names = [\"Ana\", \"Ben\", \"Cara\"]\nlengths = [len(name) for name in names]\nprint(lengths)\n",[18,28670,28671,28706,28734],{"__ignoreMap":41},[45,28672,28673,28676,28678,28680,28682,28684,28686,28688,28690,28693,28695,28697,28699,28702,28704],{"class":47,"line":48},[45,28674,28675],{"class":51},"names ",[45,28677,56],{"class":55},[45,28679,8575],{"class":85},[45,28681,100],{"class":99},[45,28683,10580],{"class":103},[45,28685,100],{"class":99},[45,28687,3079],{"class":85},[45,28689,1565],{"class":99},[45,28691,28692],{"class":103},"Ben",[45,28694,100],{"class":99},[45,28696,3079],{"class":85},[45,28698,1565],{"class":99},[45,28700,28701],{"class":103},"Cara",[45,28703,100],{"class":99},[45,28705,8594],{"class":85},[45,28707,28708,28710,28712,28714,28717,28719,28721,28723,28725,28727,28729,28732],{"class":47,"line":63},[45,28709,9127],{"class":51},[45,28711,56],{"class":55},[45,28713,8575],{"class":85},[45,28715,28716],{"class":92},"len",[45,28718,96],{"class":85},[45,28720,3096],{"class":2070},[45,28722,9146],{"class":85},[45,28724,8855],{"class":73},[45,28726,1582],{"class":51},[45,28728,8623],{"class":73},[45,28730,28731],{"class":51}," names",[45,28733,8594],{"class":85},[45,28735,28736,28738,28740,28742],{"class":47,"line":70},[45,28737,2065],{"class":92},[45,28739,96],{"class":85},[45,28741,9171],{"class":2070},[45,28743,109],{"class":85},[14,28745,177],{},[36,28747,28749],{"className":38,"code":28748,"language":40,"meta":41,"style":41},"[3, 3, 4]\n",[18,28750,28751],{"__ignoreMap":41},[45,28752,28753,28755,28757,28759,28761,28763,28765],{"class":47,"line":48},[45,28754,9680],{"class":85},[45,28756,11184],{"class":59},[45,28758,3079],{"class":85},[45,28760,777],{"class":59},[45,28762,3079],{"class":85},[45,28764,8591],{"class":59},[45,28766,8594],{"class":85},[712,28768,28770],{"id":28769},"better-as-a-normal-loop","Better as a normal loop",[36,28772,28774],{"className":38,"code":28773,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3, 4, 5]\nresults = []\n\nfor n in numbers:\n    if n % 2 == 0:\n        value = n * 10\n        results.append(value)\n\nprint(results)\n",[18,28775,28776,28804,28813,28817,28829,28845,28858,28873,28877],{"__ignoreMap":41},[45,28777,28778,28780,28782,28784,28786,28788,28790,28792,28794,28796,28798,28800,28802],{"class":47,"line":48},[45,28779,8570],{"class":51},[45,28781,56],{"class":55},[45,28783,8575],{"class":85},[45,28785,8578],{"class":59},[45,28787,3079],{"class":85},[45,28789,3754],{"class":59},[45,28791,3079],{"class":85},[45,28793,777],{"class":59},[45,28795,3079],{"class":85},[45,28797,8591],{"class":59},[45,28799,3079],{"class":85},[45,28801,1222],{"class":59},[45,28803,8594],{"class":85},[45,28805,28806,28809,28811],{"class":47,"line":63},[45,28807,28808],{"class":51},"results ",[45,28810,56],{"class":55},[45,28812,27604],{"class":85},[45,28814,28815],{"class":47,"line":70},[45,28816,67],{"emptyLinePlaceholder":66},[45,28818,28819,28821,28823,28825,28827],{"class":47,"line":89},[45,28820,7895],{"class":73},[45,28822,8612],{"class":51},[45,28824,8623],{"class":73},[45,28826,8626],{"class":51},[45,28828,86],{"class":85},[45,28830,28831,28833,28835,28837,28839,28841,28843],{"class":47,"line":112},[45,28832,1913],{"class":73},[45,28834,8612],{"class":51},[45,28836,9329],{"class":55},[45,28838,3754],{"class":59},[45,28840,9334],{"class":55},[45,28842,749],{"class":59},[45,28844,86],{"class":85},[45,28846,28847,28850,28852,28854,28856],{"class":47,"line":126},[45,28848,28849],{"class":51},"        value ",[45,28851,56],{"class":55},[45,28853,8612],{"class":51},[45,28855,5601],{"class":55},[45,28857,1174],{"class":59},[45,28859,28860,28863,28865,28867,28869,28871],{"class":47,"line":142},[45,28861,28862],{"class":51},"        results",[45,28864,240],{"class":85},[45,28866,21025],{"class":2070},[45,28868,96],{"class":85},[45,28870,12722],{"class":2070},[45,28872,109],{"class":85},[45,28874,28875],{"class":47,"line":149},[45,28876,67],{"emptyLinePlaceholder":66},[45,28878,28879,28881,28883,28886],{"class":47,"line":679},[45,28880,2065],{"class":92},[45,28882,96],{"class":85},[45,28884,28885],{"class":2070},"results",[45,28887,109],{"class":85},[14,28889,177],{},[36,28891,28893],{"className":38,"code":28892,"language":40,"meta":41,"style":41},"[20, 40]\n",[18,28894,28895],{"__ignoreMap":41},[45,28896,28897,28899,28901,28903,28905],{"class":47,"line":48},[45,28898,9680],{"class":85},[45,28900,3372],{"class":59},[45,28902,3079],{"class":85},[45,28904,10521],{"class":59},[45,28906,8594],{"class":85},[14,28908,28909,28910,28914],{},"You ",[28911,28912,28913],"em",{},"could"," write that in one line, but a normal loop may be easier to understand.",[14,28916,28917,28918,240],{},"For more practice-focused examples, see ",[236,28919,28920],{"href":26328},"how to use list comprehensions in Python",[31,28922,1337],{"id":1336},[14,28924,9847],{},[712,28926,28928],{"id":28927},"forgetting-the-brackets","Forgetting the brackets",[14,28930,28931],{},"A list comprehension must use square brackets.",[14,28933,28934],{},"Incorrect:",[36,28936,28938],{"className":38,"code":28937,"language":40,"meta":41,"style":41},"n * n for n in [1, 2, 3]\n",[18,28939,28940],{"__ignoreMap":41},[45,28941,28942,28944,28946,28948,28950,28952,28954,28956,28958,28960,28962,28964,28966],{"class":47,"line":48},[45,28943,10005],{"class":51},[45,28945,5601],{"class":55},[45,28947,8612],{"class":51},[45,28949,7895],{"class":73},[45,28951,8612],{"class":51},[45,28953,8623],{"class":73},[45,28955,8575],{"class":85},[45,28957,8578],{"class":59},[45,28959,3079],{"class":85},[45,28961,3754],{"class":59},[45,28963,3079],{"class":85},[45,28965,777],{"class":59},[45,28967,8594],{"class":85},[14,28969,1466],{},[36,28971,28973],{"className":38,"code":28972,"language":40,"meta":41,"style":41},"[n * n for n in [1, 2, 3]]\n",[18,28974,28975],{"__ignoreMap":41},[45,28976,28977,28979,28981,28983,28985,28987,28989,28991,28993,28995,28997,28999,29001,29003],{"class":47,"line":48},[45,28978,9680],{"class":85},[45,28980,10005],{"class":51},[45,28982,5601],{"class":55},[45,28984,8612],{"class":51},[45,28986,7895],{"class":73},[45,28988,8612],{"class":51},[45,28990,8623],{"class":73},[45,28992,8575],{"class":85},[45,28994,8578],{"class":59},[45,28996,3079],{"class":85},[45,28998,3754],{"class":59},[45,29000,3079],{"class":85},[45,29002,777],{"class":59},[45,29004,29005],{"class":85},"]]\n",[712,29007,29009],{"id":29008},"putting-parts-in-the-wrong-order","Putting parts in the wrong order",[14,29011,29012],{},"The order is:",[195,29014,29015,29017,29021],{},[198,29016,25253],{},[198,29018,29019],{},[18,29020,7895],{},[198,29022,29023,29024],{},"optional ",[18,29025,20],{},[14,29027,1466],{},[36,29029,29031],{"className":38,"code":29030,"language":40,"meta":41,"style":41},"[n * 2 for n in [1, 2, 3] if n > 1]\n",[18,29032,29033],{"__ignoreMap":41},[45,29034,29035,29037,29039,29041,29043,29045,29047,29049,29051,29053,29055,29057,29059,29061,29063,29065,29067,29069,29071],{"class":47,"line":48},[45,29036,9680],{"class":85},[45,29038,10005],{"class":51},[45,29040,5601],{"class":55},[45,29042,3754],{"class":59},[45,29044,8855],{"class":73},[45,29046,8612],{"class":51},[45,29048,8623],{"class":73},[45,29050,8575],{"class":85},[45,29052,8578],{"class":59},[45,29054,3079],{"class":85},[45,29056,3754],{"class":59},[45,29058,3079],{"class":85},[45,29060,777],{"class":59},[45,29062,9685],{"class":85},[45,29064,9521],{"class":73},[45,29066,8612],{"class":51},[45,29068,272],{"class":55},[45,29070,9030],{"class":59},[45,29072,8594],{"class":85},[14,29074,28934],{},[36,29076,29078],{"className":38,"code":29077,"language":40,"meta":41,"style":41},"[for n in [1, 2, 3] n * 2]\n",[18,29079,29080],{"__ignoreMap":41},[45,29081,29082,29084,29086,29088,29090,29092,29094,29096,29098,29100,29102,29104,29106,29108,29110],{"class":47,"line":48},[45,29083,9680],{"class":85},[45,29085,7895],{"class":73},[45,29087,8612],{"class":51},[45,29089,8623],{"class":73},[45,29091,8575],{"class":85},[45,29093,8578],{"class":59},[45,29095,3079],{"class":85},[45,29097,3754],{"class":59},[45,29099,3079],{"class":85},[45,29101,777],{"class":59},[45,29103,9685],{"class":85},[45,29105,8612],{"class":51},[45,29107,5601],{"class":55},[45,29109,3754],{"class":59},[45,29111,8594],{"class":85},[712,29113,1350,29115,29117],{"id":29114},"using-print-inside-the-comprehension",[18,29116,15833],{}," inside the comprehension",[14,29119,29120],{},"If you want to build a list, the expression should return values to store.",[14,29122,29123],{},"This is usually not useful:",[36,29125,29127],{"className":38,"code":29126,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\nresult = [print(n) for n in numbers]\nprint(result)\n",[18,29128,29129,29149,29175],{"__ignoreMap":41},[45,29130,29131,29133,29135,29137,29139,29141,29143,29145,29147],{"class":47,"line":48},[45,29132,8570],{"class":51},[45,29134,56],{"class":55},[45,29136,8575],{"class":85},[45,29138,8578],{"class":59},[45,29140,3079],{"class":85},[45,29142,3754],{"class":59},[45,29144,3079],{"class":85},[45,29146,777],{"class":59},[45,29148,8594],{"class":85},[45,29150,29151,29153,29155,29157,29159,29161,29163,29165,29167,29169,29171,29173],{"class":47,"line":63},[45,29152,3595],{"class":51},[45,29154,56],{"class":55},[45,29156,8575],{"class":85},[45,29158,2065],{"class":92},[45,29160,96],{"class":85},[45,29162,8607],{"class":2070},[45,29164,9146],{"class":85},[45,29166,8855],{"class":73},[45,29168,8612],{"class":51},[45,29170,8623],{"class":73},[45,29172,8626],{"class":51},[45,29174,8594],{"class":85},[45,29176,29177,29179,29181,29183],{"class":47,"line":70},[45,29178,2065],{"class":92},[45,29180,96],{"class":85},[45,29182,3620],{"class":2070},[45,29184,109],{"class":85},[14,29186,177],{},[36,29188,29190],{"className":38,"code":29189,"language":40,"meta":41,"style":41},"1\n2\n3\n[None, None, None]\n",[18,29191,29192,29196,29200,29204],{"__ignoreMap":41},[45,29193,29194],{"class":47,"line":48},[45,29195,13036],{"class":59},[45,29197,29198],{"class":47,"line":63},[45,29199,13041],{"class":59},[45,29201,29202],{"class":47,"line":70},[45,29203,13046],{"class":59},[45,29205,29206,29208,29210,29212,29215,29217,29219],{"class":47,"line":89},[45,29207,9680],{"class":85},[45,29209,5997],{"class":481},[45,29211,3079],{"class":85},[45,29213,29214],{"class":481}," None",[45,29216,3079],{"class":85},[45,29218,29214],{"class":481},[45,29220,8594],{"class":85},[14,29222,29223,29225,29226,29228,29229,29231],{},[18,29224,15833],{}," prints to the screen, but it returns ",[18,29227,5997],{},". So the list fills with ",[18,29230,5997],{}," values.",[712,29233,29235],{"id":29234},"writing-code-that-is-too-complex-for-one-line","Writing code that is too complex for one line",[14,29237,29238],{},"If you need to stop and decode the line slowly, a normal loop is probably better.",[14,29240,29241],{},"This is one reason beginners confuse list comprehensions with normal loops. The syntax is similar, but the order is different:",[195,29243,29244,29249],{},[198,29245,29246,29247],{},"normal loop: start with ",[18,29248,7895],{},[198,29250,29251],{},"list comprehension: start with the expression",[31,29253,2281],{"id":2280},[712,29255,29257],{"id":29256},"what-is-a-list-comprehension-in-python","What is a list comprehension in Python?",[14,29259,29260],{},"It is a short way to create a new list from another iterable, with optional filtering.",[712,29262,29264],{"id":29263},"are-list-comprehensions-faster-than-for-loops","Are list comprehensions faster than for loops?",[14,29266,29267],{},"They are often concise and can be efficient, but beginners should focus first on readability.",[712,29269,29271],{"id":29270},"can-i-use-if-in-a-list-comprehension","Can I use if in a list comprehension?",[14,29273,29274],{},"Yes. You can filter items with:",[36,29276,29278],{"className":38,"code":29277,"language":40,"meta":41,"style":41},"[value for item in iterable if condition]\n",[18,29279,29280],{"__ignoreMap":41},[45,29281,29282,29284,29286,29288,29290,29292,29294,29296,29298],{"class":47,"line":48},[45,29283,9680],{"class":85},[45,29285,1476],{"class":51},[45,29287,7895],{"class":73},[45,29289,8732],{"class":51},[45,29291,8623],{"class":73},[45,29293,8788],{"class":51},[45,29295,20],{"class":73},[45,29297,408],{"class":51},[45,29299,8594],{"class":85},[712,29301,29303],{"id":29302},"should-beginners-use-list-comprehensions","Should beginners use list comprehensions?",[14,29305,29306,29307,29309],{},"Yes, for simple cases. If the line becomes hard to read, use a normal ",[18,29308,7895],{}," loop instead.",[31,29311,2342],{"id":2341},[195,29313,29314,29318,29322,29327,29331,29335],{},[198,29315,29316],{},[236,29317,27366],{"href":27365},[198,29319,29320],{},[236,29321,23611],{"href":23610},[198,29323,29324],{},[236,29325,29326],{"href":28159},"How to filter a list in Python",[198,29328,29329],{},[236,29330,27069],{"href":26328},[198,29332,29333],{},[236,29334,23972],{"href":23971},[198,29336,29337],{},[236,29338,29257],{"href":29339},"\u002Fglossary\u002Fwhat-is-a-list-comprehension-in-python\u002F",[2367,29341,29342],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":29344},[29345,29346,29347,29348,29353,29358,29363,29364,29368,29375,29381],{"id":33,"depth":63,"text":34},{"id":27258,"depth":63,"text":27259},{"id":8706,"depth":63,"text":8707},{"id":27555,"depth":63,"text":27556,"children":29349},[29350,29352],{"id":27562,"depth":70,"text":29351},"Normal for loop",{"id":27682,"depth":70,"text":27683},{"id":27787,"depth":63,"text":27788,"children":29354},[29355,29356,29357],{"id":27827,"depth":70,"text":27828},{"id":27925,"depth":70,"text":27926},{"id":28053,"depth":70,"text":28054},{"id":28163,"depth":63,"text":28164,"children":29359},[29360,29361,29362],{"id":28173,"depth":70,"text":28174},{"id":28266,"depth":70,"text":28267},{"id":28385,"depth":70,"text":28386},{"id":28477,"depth":63,"text":28478},{"id":28623,"depth":63,"text":28624,"children":29365},[29366,29367],{"id":28664,"depth":70,"text":28665},{"id":28769,"depth":70,"text":28770},{"id":1336,"depth":63,"text":1337,"children":29369},[29370,29371,29372,29374],{"id":28927,"depth":70,"text":28928},{"id":29008,"depth":70,"text":29009},{"id":29114,"depth":70,"text":29373},"Using print() inside the comprehension",{"id":29234,"depth":70,"text":29235},{"id":2280,"depth":63,"text":2281,"children":29376},[29377,29378,29379,29380],{"id":29256,"depth":70,"text":29257},{"id":29263,"depth":70,"text":29264},{"id":29270,"depth":70,"text":29271},{"id":29302,"depth":70,"text":29303},{"id":2341,"depth":63,"text":2342},"Master list comprehensions in python explained in our comprehensive Python beginner guide.",{},{"title":27124,"description":29382},"learn\u002Flist-comprehensions-in-python-explained","zDbzJS7-V1gKMStPdp1NhPKIbK6pPumkN78pA7KeVBs",{"id":29388,"title":29389,"body":29390,"description":31955,"extension":2399,"meta":31956,"navigation":66,"path":31957,"seo":31958,"stem":31959,"__hash__":31960},"content\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types.md","Mutability in Python Explained (Mutable vs Immutable Types)",{"type":7,"value":29391,"toc":31934},[29392,29395,29398,29401,29404,29421,29423,29539,29542,29546,29552,29558,29561,29575,29577,29626,29628,29652,29655,29658,29703,29705,29714,29720,29724,29727,29753,29756,29767,29770,29827,29829,29844,29856,29859,29894,29896,29905,29911,29918,29922,29925,29941,29944,29955,29958,30015,30017,30050,30053,30113,30115,30152,30155,30212,30214,30247,30250,30259,30263,30266,30279,30282,30354,30356,30393,30403,30411,30414,30483,30485,30500,30512,30526,30530,30533,30535,30543,30545,30631,30633,30658,30661,30664,30762,30764,30789,30792,30798,30802,30808,30878,30880,30904,30907,31011,31013,31062,31068,31163,31165,31224,31231,31250,31254,31257,31273,31276,31364,31367,31457,31459,31477,31483,31486,31549,31551,31577,31585,31588,31592,31595,31616,31618,31621,31638,31641,31701,31703,31723,31726,31764,31766,31775,31778,31819,31821,31829,31831,31835,31838,31842,31857,31863,31866,31870,31873,31877,31890,31892,31931],[10,29393,29389],{"id":29394},"mutability-in-python-explained-mutable-vs-immutable-types",[14,29396,29397],{},"Mutability describes whether a Python object can change after it is created.",[14,29399,29400],{},"This matters because Python variables do not store independent copied values by default. They refer to objects. Once you understand that, many confusing results with lists, strings, and function arguments start to make sense.",[14,29402,29403],{},"In this guide, you will learn:",[195,29405,29406,29409,29412,29415,29418],{},[198,29407,29408],{},"What mutable and immutable mean",[198,29410,29411],{},"Which common Python types are mutable or immutable",[198,29413,29414],{},"How assignment works with objects",[198,29416,29417],{},"Why functions can sometimes change your original data",[198,29419,29420],{},"How to avoid common bugs",[31,29422,34],{"id":33},[36,29424,29426],{"className":38,"code":29425,"language":40,"meta":41,"style":41},"x = [1, 2]\ny = x\ny.append(3)\nprint(x)  # [1, 2, 3]\n\nname = \"Sam\"\nnew_name = name.upper()\nprint(name)      # Sam\nprint(new_name)  # SAM\n",[18,29427,29428,29444,29453,29468,29481,29485,29497,29512,29525],{"__ignoreMap":41},[45,29429,29430,29432,29434,29436,29438,29440,29442],{"class":47,"line":48},[45,29431,1169],{"class":51},[45,29433,56],{"class":55},[45,29435,8575],{"class":85},[45,29437,8578],{"class":59},[45,29439,3079],{"class":85},[45,29441,3754],{"class":59},[45,29443,8594],{"class":85},[45,29445,29446,29449,29451],{"class":47,"line":63},[45,29447,29448],{"class":51},"y ",[45,29450,56],{"class":55},[45,29452,25178],{"class":51},[45,29454,29455,29458,29460,29462,29464,29466],{"class":47,"line":70},[45,29456,29457],{"class":51},"y",[45,29459,240],{"class":85},[45,29461,21025],{"class":2070},[45,29463,96],{"class":85},[45,29465,11184],{"class":59},[45,29467,109],{"class":85},[45,29469,29470,29472,29474,29476,29478],{"class":47,"line":89},[45,29471,2065],{"class":92},[45,29473,96],{"class":85},[45,29475,26156],{"class":2070},[45,29477,9146],{"class":85},[45,29479,29480],{"class":7624},"  # [1, 2, 3]\n",[45,29482,29483],{"class":47,"line":112},[45,29484,67],{"emptyLinePlaceholder":66},[45,29486,29487,29489,29491,29493,29495],{"class":47,"line":126},[45,29488,1560],{"class":51},[45,29490,56],{"class":55},[45,29492,1565],{"class":99},[45,29494,1568],{"class":103},[45,29496,1571],{"class":99},[45,29498,29499,29502,29504,29506,29508,29510],{"class":47,"line":142},[45,29500,29501],{"class":51},"new_name ",[45,29503,56],{"class":55},[45,29505,3083],{"class":51},[45,29507,240],{"class":85},[45,29509,10327],{"class":2070},[45,29511,2495],{"class":85},[45,29513,29514,29516,29518,29520,29522],{"class":47,"line":149},[45,29515,2065],{"class":92},[45,29517,96],{"class":85},[45,29519,3096],{"class":2070},[45,29521,9146],{"class":85},[45,29523,29524],{"class":7624},"      # Sam\n",[45,29526,29527,29529,29531,29534,29536],{"class":47,"line":679},[45,29528,2065],{"class":92},[45,29530,96],{"class":85},[45,29532,29533],{"class":2070},"new_name",[45,29535,9146],{"class":85},[45,29537,29538],{"class":7624},"  # SAM\n",[14,29540,29541],{},"Lists can change in place. Strings cannot. Many beginner mistakes come from not knowing this difference.",[31,29543,29545],{"id":29544},"what-mutability-means","What mutability means",[14,29547,2419,29548,29551],{},[2421,29549,29550],{},"mutable"," object can be changed after it is created.",[14,29553,19451,29554,29557],{},[2421,29555,29556],{},"immutable"," object cannot be changed after it is created.",[14,29559,29560],{},"That sounds simple, but it affects how Python behaves in important ways:",[195,29562,29563,29566,29569,29572],{},[198,29564,29565],{},"If an object is mutable, code can change the same object in place",[198,29567,29568],{},"If an object is immutable, any “change” actually creates a new object",[198,29570,29571],{},"This matters most when more than one variable refers to the same object",[198,29573,29574],{},"It helps explain many surprising results in beginner Python code",[14,29576,2617],{},[36,29578,29580],{"className":38,"code":29579,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\nnumbers.append(4)\nprint(numbers)\n",[18,29581,29582,29602,29616],{"__ignoreMap":41},[45,29583,29584,29586,29588,29590,29592,29594,29596,29598,29600],{"class":47,"line":48},[45,29585,8570],{"class":51},[45,29587,56],{"class":55},[45,29589,8575],{"class":85},[45,29591,8578],{"class":59},[45,29593,3079],{"class":85},[45,29595,3754],{"class":59},[45,29597,3079],{"class":85},[45,29599,777],{"class":59},[45,29601,8594],{"class":85},[45,29603,29604,29606,29608,29610,29612,29614],{"class":47,"line":63},[45,29605,9069],{"class":51},[45,29607,240],{"class":85},[45,29609,21025],{"class":2070},[45,29611,96],{"class":85},[45,29613,3584],{"class":59},[45,29615,109],{"class":85},[45,29617,29618,29620,29622,29624],{"class":47,"line":70},[45,29619,2065],{"class":92},[45,29621,96],{"class":85},[45,29623,9069],{"class":2070},[45,29625,109],{"class":85},[14,29627,177],{},[36,29629,29630],{"className":38,"code":14406,"language":40,"meta":41,"style":41},[18,29631,29632],{"__ignoreMap":41},[45,29633,29634,29636,29638,29640,29642,29644,29646,29648,29650],{"class":47,"line":48},[45,29635,9680],{"class":85},[45,29637,8578],{"class":59},[45,29639,3079],{"class":85},[45,29641,3754],{"class":59},[45,29643,3079],{"class":85},[45,29645,777],{"class":59},[45,29647,3079],{"class":85},[45,29649,8591],{"class":59},[45,29651,8594],{"class":85},[14,29653,29654],{},"The list was changed in place.",[14,29656,29657],{},"Now compare that to a string:",[36,29659,29661],{"className":38,"code":29660,"language":40,"meta":41,"style":41},"text = \"cat\"\ntext = text + \"s\"\nprint(text)\n",[18,29662,29663,29675,29693],{"__ignoreMap":41},[45,29664,29665,29667,29669,29671,29673],{"class":47,"line":48},[45,29666,10297],{"class":51},[45,29668,56],{"class":55},[45,29670,1565],{"class":99},[45,29672,7035],{"class":103},[45,29674,1571],{"class":99},[45,29676,29677,29679,29681,29684,29686,29688,29691],{"class":47,"line":63},[45,29678,10297],{"class":51},[45,29680,56],{"class":55},[45,29682,29683],{"class":51}," text ",[45,29685,8850],{"class":55},[45,29687,1565],{"class":99},[45,29689,29690],{"class":103},"s",[45,29692,1571],{"class":99},[45,29694,29695,29697,29699,29701],{"class":47,"line":70},[45,29696,2065],{"class":92},[45,29698,96],{"class":85},[45,29700,5000],{"class":2070},[45,29702,109],{"class":85},[14,29704,177],{},[36,29706,29708],{"className":38,"code":29707,"language":40,"meta":41,"style":41},"cats\n",[18,29709,29710],{"__ignoreMap":41},[45,29711,29712],{"class":47,"line":48},[45,29713,29707],{"class":51},[14,29715,29716,29717,29719],{},"This looks like the string changed, but it did not. Python created a new string and then reassigned ",[18,29718,5000],{}," to point to it.",[31,29721,29723],{"id":29722},"common-immutable-types","Common immutable types",[14,29725,29726],{},"Common immutable Python types include:",[195,29728,29729,29734,29739,29744,29748],{},[198,29730,29731],{},[18,29732,29733],{},"str",[198,29735,29736],{},[18,29737,29738],{},"int",[198,29740,29741],{},[18,29742,29743],{},"float",[198,29745,29746],{},[18,29747,2110],{},[198,29749,29750],{},[18,29751,29752],{},"tuple",[14,29754,29755],{},"With immutable objects:",[195,29757,29758,29761,29764],{},[198,29759,29760],{},"You cannot change the object itself after creation",[198,29762,29763],{},"Operations usually create a new object",[198,29765,29766],{},"Reassigning a variable does not change the original object",[14,29768,29769],{},"Example with a string:",[36,29771,29773],{"className":38,"code":29772,"language":40,"meta":41,"style":41},"name = \"Sam\"\nupper_name = name.upper()\n\nprint(name)\nprint(upper_name)\n",[18,29774,29775,29787,29802,29806,29816],{"__ignoreMap":41},[45,29776,29777,29779,29781,29783,29785],{"class":47,"line":48},[45,29778,1560],{"class":51},[45,29780,56],{"class":55},[45,29782,1565],{"class":99},[45,29784,1568],{"class":103},[45,29786,1571],{"class":99},[45,29788,29789,29792,29794,29796,29798,29800],{"class":47,"line":63},[45,29790,29791],{"class":51},"upper_name ",[45,29793,56],{"class":55},[45,29795,3083],{"class":51},[45,29797,240],{"class":85},[45,29799,10327],{"class":2070},[45,29801,2495],{"class":85},[45,29803,29804],{"class":47,"line":70},[45,29805,67],{"emptyLinePlaceholder":66},[45,29807,29808,29810,29812,29814],{"class":47,"line":89},[45,29809,2065],{"class":92},[45,29811,96],{"class":85},[45,29813,3096],{"class":2070},[45,29815,109],{"class":85},[45,29817,29818,29820,29822,29825],{"class":47,"line":112},[45,29819,2065],{"class":92},[45,29821,96],{"class":85},[45,29823,29824],{"class":2070},"upper_name",[45,29826,109],{"class":85},[14,29828,177],{},[36,29830,29832],{"className":38,"code":29831,"language":40,"meta":41,"style":41},"Sam\nSAM\n",[18,29833,29834,29839],{"__ignoreMap":41},[45,29835,29836],{"class":47,"line":48},[45,29837,29838],{"class":51},"Sam\n",[45,29840,29841],{"class":47,"line":63},[45,29842,29843],{"class":2771},"SAM\n",[14,29845,29846,29849,29850,29852,29853,29855],{},[18,29847,29848],{},"name.upper()"," does ",[2421,29851,2857],{}," change ",[18,29854,3096],{},". It returns a new string.",[14,29857,29858],{},"Example with an integer:",[36,29860,29862],{"className":38,"code":29861,"language":40,"meta":41,"style":41},"x = 10\nx = x + 1\nprint(x)\n",[18,29863,29864,29872,29884],{"__ignoreMap":41},[45,29865,29866,29868,29870],{"class":47,"line":48},[45,29867,1169],{"class":51},[45,29869,56],{"class":55},[45,29871,1174],{"class":59},[45,29873,29874,29876,29878,29880,29882],{"class":47,"line":63},[45,29875,1169],{"class":51},[45,29877,56],{"class":55},[45,29879,1185],{"class":51},[45,29881,8850],{"class":55},[45,29883,12962],{"class":59},[45,29885,29886,29888,29890,29892],{"class":47,"line":70},[45,29887,2065],{"class":92},[45,29889,96],{"class":85},[45,29891,26156],{"class":2070},[45,29893,109],{"class":85},[14,29895,177],{},[36,29897,29899],{"className":38,"code":29898,"language":40,"meta":41,"style":41},"11\n",[18,29900,29901],{"__ignoreMap":41},[45,29902,29903],{"class":47,"line":48},[45,29904,29898],{"class":59},[14,29906,29907,29908,29910],{},"Again, Python does not modify the original integer in place. It creates a new integer and assigns ",[18,29909,26156],{}," to it.",[14,29912,29913,29914,240],{},"If you want to learn more about string behavior, see ",[236,29915,29917],{"href":29916},"\u002Flearn\u002Fpython-strings-explained-basics-and-examples","Python strings explained",[31,29919,29921],{"id":29920},"common-mutable-types","Common mutable types",[14,29923,29924],{},"Common mutable Python types include:",[195,29926,29927,29931,29936],{},[198,29928,29929],{},[18,29930,14463],{},[198,29932,29933],{},[18,29934,29935],{},"dict",[198,29937,29938],{},[18,29939,29940],{},"set",[14,29942,29943],{},"With mutable objects:",[195,29945,29946,29949,29952],{},[198,29947,29948],{},"The object can be changed after creation",[198,29950,29951],{},"Many methods modify the object in place",[198,29953,29954],{},"If two variables refer to the same object, both will reflect the change",[14,29956,29957],{},"Example with a list:",[36,29959,29961],{"className":38,"code":29960,"language":40,"meta":41,"style":41},"items = [\"a\", \"b\"]\nitems.append(\"c\")\nprint(items)\n",[18,29962,29963,29987,30005],{"__ignoreMap":41},[45,29964,29965,29967,29969,29971,29973,29975,29977,29979,29981,29983,29985],{"class":47,"line":48},[45,29966,23632],{"class":51},[45,29968,56],{"class":55},[45,29970,8575],{"class":85},[45,29972,100],{"class":99},[45,29974,236],{"class":103},[45,29976,100],{"class":99},[45,29978,3079],{"class":85},[45,29980,1565],{"class":99},[45,29982,10384],{"class":103},[45,29984,100],{"class":99},[45,29986,8594],{"class":85},[45,29988,29989,29991,29993,29995,29997,29999,30001,30003],{"class":47,"line":63},[45,29990,9516],{"class":51},[45,29992,240],{"class":85},[45,29994,21025],{"class":2070},[45,29996,96],{"class":85},[45,29998,100],{"class":99},[45,30000,10402],{"class":103},[45,30002,100],{"class":99},[45,30004,109],{"class":85},[45,30006,30007,30009,30011,30013],{"class":47,"line":70},[45,30008,2065],{"class":92},[45,30010,96],{"class":85},[45,30012,9516],{"class":2070},[45,30014,109],{"class":85},[14,30016,177],{},[36,30018,30020],{"className":38,"code":30019,"language":40,"meta":41,"style":41},"['a', 'b', 'c']\n",[18,30021,30022],{"__ignoreMap":41},[45,30023,30024,30026,30028,30030,30032,30034,30036,30038,30040,30042,30044,30046,30048],{"class":47,"line":48},[45,30025,9680],{"class":85},[45,30027,7901],{"class":99},[45,30029,236],{"class":103},[45,30031,7901],{"class":99},[45,30033,3079],{"class":85},[45,30035,9199],{"class":99},[45,30037,10384],{"class":103},[45,30039,7901],{"class":99},[45,30041,3079],{"class":85},[45,30043,9199],{"class":99},[45,30045,10402],{"class":103},[45,30047,7901],{"class":99},[45,30049,8594],{"class":85},[14,30051,30052],{},"Example with a dictionary:",[36,30054,30056],{"className":38,"code":30055,"language":40,"meta":41,"style":41},"user = {\"name\": \"Ana\"}\nuser[\"age\"] = 25\nprint(user)\n",[18,30057,30058,30083,30103],{"__ignoreMap":41},[45,30059,30060,30063,30065,30067,30069,30071,30073,30075,30077,30079,30081],{"class":47,"line":48},[45,30061,30062],{"class":51},"user ",[45,30064,56],{"class":55},[45,30066,8604],{"class":85},[45,30068,100],{"class":99},[45,30070,3096],{"class":103},[45,30072,100],{"class":99},[45,30074,457],{"class":85},[45,30076,1565],{"class":99},[45,30078,10580],{"class":103},[45,30080,100],{"class":99},[45,30082,8629],{"class":85},[45,30084,30085,30088,30090,30092,30094,30096,30098,30100],{"class":47,"line":63},[45,30086,30087],{"class":51},"user",[45,30089,9680],{"class":85},[45,30091,100],{"class":99},[45,30093,2082],{"class":103},[45,30095,100],{"class":99},[45,30097,9685],{"class":85},[45,30099,3099],{"class":55},[45,30101,30102],{"class":59}," 25\n",[45,30104,30105,30107,30109,30111],{"class":47,"line":70},[45,30106,2065],{"class":92},[45,30108,96],{"class":85},[45,30110,30087],{"class":2070},[45,30112,109],{"class":85},[14,30114,177],{},[36,30116,30118],{"className":38,"code":30117,"language":40,"meta":41,"style":41},"{'name': 'Ana', 'age': 25}\n",[18,30119,30120],{"__ignoreMap":41},[45,30121,30122,30124,30126,30128,30130,30132,30134,30136,30138,30140,30142,30144,30146,30148,30150],{"class":47,"line":48},[45,30123,5551],{"class":85},[45,30125,7901],{"class":99},[45,30127,3096],{"class":103},[45,30129,7901],{"class":99},[45,30131,457],{"class":85},[45,30133,9199],{"class":99},[45,30135,10580],{"class":103},[45,30137,7901],{"class":99},[45,30139,3079],{"class":85},[45,30141,9199],{"class":99},[45,30143,2082],{"class":103},[45,30145,7901],{"class":99},[45,30147,457],{"class":85},[45,30149,303],{"class":59},[45,30151,8629],{"class":85},[14,30153,30154],{},"Example with a set:",[36,30156,30158],{"className":38,"code":30157,"language":40,"meta":41,"style":41},"colors = {\"red\", \"blue\"}\ncolors.add(\"green\")\nprint(colors)\n",[18,30159,30160,30184,30202],{"__ignoreMap":41},[45,30161,30162,30164,30166,30168,30170,30172,30174,30176,30178,30180,30182],{"class":47,"line":48},[45,30163,22320],{"class":51},[45,30165,56],{"class":55},[45,30167,8604],{"class":85},[45,30169,100],{"class":99},[45,30171,22329],{"class":103},[45,30173,100],{"class":99},[45,30175,3079],{"class":85},[45,30177,1565],{"class":99},[45,30179,22347],{"class":103},[45,30181,100],{"class":99},[45,30183,8629],{"class":85},[45,30185,30186,30188,30190,30192,30194,30196,30198,30200],{"class":47,"line":63},[45,30187,22407],{"class":51},[45,30189,240],{"class":85},[45,30191,11159],{"class":2070},[45,30193,96],{"class":85},[45,30195,100],{"class":99},[45,30197,22338],{"class":103},[45,30199,100],{"class":99},[45,30201,109],{"class":85},[45,30203,30204,30206,30208,30210],{"class":47,"line":70},[45,30205,2065],{"class":92},[45,30207,96],{"class":85},[45,30209,22407],{"class":2070},[45,30211,109],{"class":85},[14,30213,177],{},[36,30215,30217],{"className":38,"code":30216,"language":40,"meta":41,"style":41},"{'red', 'blue', 'green'}\n",[18,30218,30219],{"__ignoreMap":41},[45,30220,30221,30223,30225,30227,30229,30231,30233,30235,30237,30239,30241,30243,30245],{"class":47,"line":48},[45,30222,5551],{"class":85},[45,30224,7901],{"class":99},[45,30226,22329],{"class":103},[45,30228,7901],{"class":99},[45,30230,3079],{"class":85},[45,30232,9199],{"class":99},[45,30234,22347],{"class":103},[45,30236,7901],{"class":99},[45,30238,3079],{"class":85},[45,30240,9199],{"class":99},[45,30242,22338],{"class":103},[45,30244,7901],{"class":99},[45,30246,8629],{"class":85},[14,30248,30249],{},"These methods change the original object instead of returning a brand-new one.",[14,30251,30252,30253,1316,30256,240],{},"For more on list behavior, see ",[236,30254,27366],{"href":30255},"\u002Flearn\u002Fpython-lists-explained-beginner-guide",[236,30257,8557],{"href":30258},"\u002Flearn\u002Fpython-dictionaries-explained\u002F",[31,30260,30262],{"id":30261},"variables-store-references-not-boxes-of-copied-values","Variables store references, not boxes of copied values",[14,30264,30265],{},"A beginner-friendly way to think about this is:",[195,30267,30268,30271,30276],{},[198,30269,30270],{},"A variable name points to an object",[198,30272,1350,30273,30275],{},[18,30274,56],{}," does not always make a new independent copy",[198,30277,30278],{},"Sometimes two variable names point to the same object",[14,30280,30281],{},"This is especially important with mutable types.",[36,30283,30285],{"className":38,"code":30284,"language":40,"meta":41,"style":41},"a = [1, 2]\nb = a\n\nb.append(3)\n\nprint(a)\nprint(b)\n",[18,30286,30287,30303,30312,30316,30330,30334,30344],{"__ignoreMap":41},[45,30288,30289,30291,30293,30295,30297,30299,30301],{"class":47,"line":48},[45,30290,11541],{"class":51},[45,30292,56],{"class":55},[45,30294,8575],{"class":85},[45,30296,8578],{"class":59},[45,30298,3079],{"class":85},[45,30300,3754],{"class":59},[45,30302,8594],{"class":85},[45,30304,30305,30307,30309],{"class":47,"line":63},[45,30306,19315],{"class":51},[45,30308,56],{"class":55},[45,30310,30311],{"class":51}," a\n",[45,30313,30314],{"class":47,"line":70},[45,30315,67],{"emptyLinePlaceholder":66},[45,30317,30318,30320,30322,30324,30326,30328],{"class":47,"line":89},[45,30319,10384],{"class":51},[45,30321,240],{"class":85},[45,30323,21025],{"class":2070},[45,30325,96],{"class":85},[45,30327,11184],{"class":59},[45,30329,109],{"class":85},[45,30331,30332],{"class":47,"line":112},[45,30333,67],{"emptyLinePlaceholder":66},[45,30335,30336,30338,30340,30342],{"class":47,"line":126},[45,30337,2065],{"class":92},[45,30339,96],{"class":85},[45,30341,236],{"class":2070},[45,30343,109],{"class":85},[45,30345,30346,30348,30350,30352],{"class":47,"line":142},[45,30347,2065],{"class":92},[45,30349,96],{"class":85},[45,30351,10384],{"class":2070},[45,30353,109],{"class":85},[14,30355,177],{},[36,30357,30359],{"className":38,"code":30358,"language":40,"meta":41,"style":41},"[1, 2, 3]\n[1, 2, 3]\n",[18,30360,30361,30377],{"__ignoreMap":41},[45,30362,30363,30365,30367,30369,30371,30373,30375],{"class":47,"line":48},[45,30364,9680],{"class":85},[45,30366,8578],{"class":59},[45,30368,3079],{"class":85},[45,30370,3754],{"class":59},[45,30372,3079],{"class":85},[45,30374,777],{"class":59},[45,30376,8594],{"class":85},[45,30378,30379,30381,30383,30385,30387,30389,30391],{"class":47,"line":63},[45,30380,9680],{"class":85},[45,30382,8578],{"class":59},[45,30384,3079],{"class":85},[45,30386,3754],{"class":59},[45,30388,3079],{"class":85},[45,30390,777],{"class":59},[45,30392,8594],{"class":85},[14,30394,30395,30396,30398,30399,30402],{},"Why did ",[18,30397,236],{}," change when only ",[18,30400,30401],{},"b.append(3)"," was called?",[14,30404,30405,30406,1316,30408,30410],{},"Because ",[18,30407,236],{},[18,30409,10384],{}," refer to the same list.",[14,30412,30413],{},"Now compare that with an immutable value:",[36,30415,30417],{"className":38,"code":30416,"language":40,"meta":41,"style":41},"x = \"hi\"\ny = x\n\ny = y.upper()\n\nprint(x)\nprint(y)\n",[18,30418,30419,30432,30440,30444,30459,30463,30473],{"__ignoreMap":41},[45,30420,30421,30423,30425,30427,30430],{"class":47,"line":48},[45,30422,1169],{"class":51},[45,30424,56],{"class":55},[45,30426,1565],{"class":99},[45,30428,30429],{"class":103},"hi",[45,30431,1571],{"class":99},[45,30433,30434,30436,30438],{"class":47,"line":63},[45,30435,29448],{"class":51},[45,30437,56],{"class":55},[45,30439,25178],{"class":51},[45,30441,30442],{"class":47,"line":70},[45,30443,67],{"emptyLinePlaceholder":66},[45,30445,30446,30448,30450,30453,30455,30457],{"class":47,"line":89},[45,30447,29448],{"class":51},[45,30449,56],{"class":55},[45,30451,30452],{"class":51}," y",[45,30454,240],{"class":85},[45,30456,10327],{"class":2070},[45,30458,2495],{"class":85},[45,30460,30461],{"class":47,"line":112},[45,30462,67],{"emptyLinePlaceholder":66},[45,30464,30465,30467,30469,30471],{"class":47,"line":126},[45,30466,2065],{"class":92},[45,30468,96],{"class":85},[45,30470,26156],{"class":2070},[45,30472,109],{"class":85},[45,30474,30475,30477,30479,30481],{"class":47,"line":142},[45,30476,2065],{"class":92},[45,30478,96],{"class":85},[45,30480,29457],{"class":2070},[45,30482,109],{"class":85},[14,30484,177],{},[36,30486,30488],{"className":38,"code":30487,"language":40,"meta":41,"style":41},"hi\nHI\n",[18,30489,30490,30495],{"__ignoreMap":41},[45,30491,30492],{"class":47,"line":48},[45,30493,30494],{"class":51},"hi\n",[45,30496,30497],{"class":47,"line":63},[45,30498,30499],{"class":2771},"HI\n",[14,30501,3444,30502,30504,30505,30508,30509,30511],{},[18,30503,26156],{}," does not change because strings are immutable. ",[18,30506,30507],{},"y.upper()"," returns a new string, and ",[18,30510,29457],{}," is updated to refer to that new object.",[14,30513,30514,30515,30522,30523,30525],{},"If you want a simple way to inspect object identity, Python has the ",[236,30516,30518,30521],{"href":30517},"\u002Freference\u002Fpython-id-function-explained\u002F",[18,30519,30520],{},"id()"," function",". It can help while debugging, but it is better to understand the behavior first instead of depending on ",[18,30524,30520],{}," alone.",[31,30527,30529],{"id":30528},"why-mutability-matters-with-functions","Why mutability matters with functions",[14,30531,30532],{},"When you pass an object to a function, the function receives access to that same object.",[14,30534,2727],{},[195,30536,30537,30540],{},[198,30538,30539],{},"If the object is mutable, the function can change the original",[198,30541,30542],{},"If the object is immutable, the function cannot change the original in place",[14,30544,29957],{},[36,30546,30548],{"className":38,"code":30547,"language":40,"meta":41,"style":41},"def add_item(my_list):\n    my_list.append(\"new\")\n\ndata = [\"old\"]\nadd_item(data)\n\nprint(data)\n",[18,30549,30550,30564,30583,30587,30605,30617,30621],{"__ignoreMap":41},[45,30551,30552,30554,30557,30559,30562],{"class":47,"line":48},[45,30553,2757],{"class":2440},[45,30555,30556],{"class":2455}," add_item",[45,30558,96],{"class":85},[45,30560,30561],{"class":3082},"my_list",[45,30563,2464],{"class":85},[45,30565,30566,30569,30571,30573,30575,30577,30579,30581],{"class":47,"line":63},[45,30567,30568],{"class":51},"    my_list",[45,30570,240],{"class":85},[45,30572,21025],{"class":2070},[45,30574,96],{"class":85},[45,30576,100],{"class":99},[45,30578,16747],{"class":103},[45,30580,100],{"class":99},[45,30582,109],{"class":85},[45,30584,30585],{"class":47,"line":70},[45,30586,67],{"emptyLinePlaceholder":66},[45,30588,30589,30592,30594,30596,30598,30601,30603],{"class":47,"line":89},[45,30590,30591],{"class":51},"data ",[45,30593,56],{"class":55},[45,30595,8575],{"class":85},[45,30597,100],{"class":99},[45,30599,30600],{"class":103},"old",[45,30602,100],{"class":99},[45,30604,8594],{"class":85},[45,30606,30607,30610,30612,30615],{"class":47,"line":112},[45,30608,30609],{"class":2070},"add_item",[45,30611,96],{"class":85},[45,30613,30614],{"class":2070},"data",[45,30616,109],{"class":85},[45,30618,30619],{"class":47,"line":126},[45,30620,67],{"emptyLinePlaceholder":66},[45,30622,30623,30625,30627,30629],{"class":47,"line":142},[45,30624,2065],{"class":92},[45,30626,96],{"class":85},[45,30628,30614],{"class":2070},[45,30630,109],{"class":85},[14,30632,177],{},[36,30634,30636],{"className":38,"code":30635,"language":40,"meta":41,"style":41},"['old', 'new']\n",[18,30637,30638],{"__ignoreMap":41},[45,30639,30640,30642,30644,30646,30648,30650,30652,30654,30656],{"class":47,"line":48},[45,30641,9680],{"class":85},[45,30643,7901],{"class":99},[45,30645,30600],{"class":103},[45,30647,7901],{"class":99},[45,30649,3079],{"class":85},[45,30651,9199],{"class":99},[45,30653,16747],{"class":103},[45,30655,7901],{"class":99},[45,30657,8594],{"class":85},[14,30659,30660],{},"The function changed the original list.",[14,30662,30663],{},"Now compare that with a string:",[36,30665,30667],{"className":38,"code":30666,"language":40,"meta":41,"style":41},"def make_upper(text):\n    text = text.upper()\n    print(\"Inside function:\", text)\n\nname = \"Sam\"\nmake_upper(name)\nprint(\"Outside function:\", name)\n",[18,30668,30669,30682,30697,30716,30720,30732,30743],{"__ignoreMap":41},[45,30670,30671,30673,30676,30678,30680],{"class":47,"line":48},[45,30672,2757],{"class":2440},[45,30674,30675],{"class":2455}," make_upper",[45,30677,96],{"class":85},[45,30679,5000],{"class":3082},[45,30681,2464],{"class":85},[45,30683,30684,30687,30689,30691,30693,30695],{"class":47,"line":63},[45,30685,30686],{"class":51},"    text ",[45,30688,56],{"class":55},[45,30690,10339],{"class":51},[45,30692,240],{"class":85},[45,30694,10327],{"class":2070},[45,30696,2495],{"class":85},[45,30698,30699,30701,30703,30705,30708,30710,30712,30714],{"class":47,"line":70},[45,30700,93],{"class":92},[45,30702,96],{"class":85},[45,30704,100],{"class":99},[45,30706,30707],{"class":103},"Inside function:",[45,30709,100],{"class":99},[45,30711,3079],{"class":85},[45,30713,10339],{"class":2070},[45,30715,109],{"class":85},[45,30717,30718],{"class":47,"line":89},[45,30719,67],{"emptyLinePlaceholder":66},[45,30721,30722,30724,30726,30728,30730],{"class":47,"line":112},[45,30723,1560],{"class":51},[45,30725,56],{"class":55},[45,30727,1565],{"class":99},[45,30729,1568],{"class":103},[45,30731,1571],{"class":99},[45,30733,30734,30737,30739,30741],{"class":47,"line":126},[45,30735,30736],{"class":2070},"make_upper",[45,30738,96],{"class":85},[45,30740,3096],{"class":2070},[45,30742,109],{"class":85},[45,30744,30745,30747,30749,30751,30754,30756,30758,30760],{"class":47,"line":142},[45,30746,2065],{"class":92},[45,30748,96],{"class":85},[45,30750,100],{"class":99},[45,30752,30753],{"class":103},"Outside function:",[45,30755,100],{"class":99},[45,30757,3079],{"class":85},[45,30759,3083],{"class":2070},[45,30761,109],{"class":85},[14,30763,177],{},[36,30765,30767],{"className":38,"code":30766,"language":40,"meta":41,"style":41},"Inside function: SAM\nOutside function: Sam\n",[18,30768,30769,30779],{"__ignoreMap":41},[45,30770,30771,30774,30776],{"class":47,"line":48},[45,30772,30773],{"class":51},"Inside function",[45,30775,457],{"class":85},[45,30777,30778],{"class":2771}," SAM\n",[45,30780,30781,30784,30786],{"class":47,"line":63},[45,30782,30783],{"class":51},"Outside function",[45,30785,457],{"class":85},[45,30787,30788],{"class":51}," Sam\n",[14,30790,30791],{},"The original string did not change.",[14,30793,30794,30795,30797],{},"This is one reason beginners get confused by ",[236,30796,5879],{"href":4579},". It can seem like arguments are copied automatically, but that is not what happens.",[31,30799,30801],{"id":30800},"mutability-and-copying","Mutability and copying",[14,30803,30804,30805,30807],{},"Assignment shares a reference. It does ",[2421,30806,2857],{}," usually make a copy.",[36,30809,30811],{"className":38,"code":30810,"language":40,"meta":41,"style":41},"original = [1, 2, 3]\nalias = original\n\nalias.append(4)\n\nprint(original)\n",[18,30812,30813,30834,30844,30848,30863,30867],{"__ignoreMap":41},[45,30814,30815,30818,30820,30822,30824,30826,30828,30830,30832],{"class":47,"line":48},[45,30816,30817],{"class":51},"original ",[45,30819,56],{"class":55},[45,30821,8575],{"class":85},[45,30823,8578],{"class":59},[45,30825,3079],{"class":85},[45,30827,3754],{"class":59},[45,30829,3079],{"class":85},[45,30831,777],{"class":59},[45,30833,8594],{"class":85},[45,30835,30836,30839,30841],{"class":47,"line":63},[45,30837,30838],{"class":51},"alias ",[45,30840,56],{"class":55},[45,30842,30843],{"class":51}," original\n",[45,30845,30846],{"class":47,"line":70},[45,30847,67],{"emptyLinePlaceholder":66},[45,30849,30850,30853,30855,30857,30859,30861],{"class":47,"line":89},[45,30851,30852],{"class":51},"alias",[45,30854,240],{"class":85},[45,30856,21025],{"class":2070},[45,30858,96],{"class":85},[45,30860,3584],{"class":59},[45,30862,109],{"class":85},[45,30864,30865],{"class":47,"line":112},[45,30866,67],{"emptyLinePlaceholder":66},[45,30868,30869,30871,30873,30876],{"class":47,"line":126},[45,30870,2065],{"class":92},[45,30872,96],{"class":85},[45,30874,30875],{"class":2070},"original",[45,30877,109],{"class":85},[14,30879,177],{},[36,30881,30882],{"className":38,"code":14406,"language":40,"meta":41,"style":41},[18,30883,30884],{"__ignoreMap":41},[45,30885,30886,30888,30890,30892,30894,30896,30898,30900,30902],{"class":47,"line":48},[45,30887,9680],{"class":85},[45,30889,8578],{"class":59},[45,30891,3079],{"class":85},[45,30893,3754],{"class":59},[45,30895,3079],{"class":85},[45,30897,777],{"class":59},[45,30899,3079],{"class":85},[45,30901,8591],{"class":59},[45,30903,8594],{"class":85},[14,30905,30906],{},"If you need an independent list, make a copy:",[36,30908,30910],{"className":38,"code":30909,"language":40,"meta":41,"style":41},"original = [1, 2, 3]\ncopied = original.copy()\n\ncopied.append(4)\n\nprint(\"original:\", original)\nprint(\"copied:\", copied)\n",[18,30911,30912,30932,30949,30953,30968,30972,30991],{"__ignoreMap":41},[45,30913,30914,30916,30918,30920,30922,30924,30926,30928,30930],{"class":47,"line":48},[45,30915,30817],{"class":51},[45,30917,56],{"class":55},[45,30919,8575],{"class":85},[45,30921,8578],{"class":59},[45,30923,3079],{"class":85},[45,30925,3754],{"class":59},[45,30927,3079],{"class":85},[45,30929,777],{"class":59},[45,30931,8594],{"class":85},[45,30933,30934,30937,30939,30942,30944,30947],{"class":47,"line":63},[45,30935,30936],{"class":51},"copied ",[45,30938,56],{"class":55},[45,30940,30941],{"class":51}," original",[45,30943,240],{"class":85},[45,30945,30946],{"class":2070},"copy",[45,30948,2495],{"class":85},[45,30950,30951],{"class":47,"line":70},[45,30952,67],{"emptyLinePlaceholder":66},[45,30954,30955,30958,30960,30962,30964,30966],{"class":47,"line":89},[45,30956,30957],{"class":51},"copied",[45,30959,240],{"class":85},[45,30961,21025],{"class":2070},[45,30963,96],{"class":85},[45,30965,3584],{"class":59},[45,30967,109],{"class":85},[45,30969,30970],{"class":47,"line":112},[45,30971,67],{"emptyLinePlaceholder":66},[45,30973,30974,30976,30978,30980,30983,30985,30987,30989],{"class":47,"line":126},[45,30975,2065],{"class":92},[45,30977,96],{"class":85},[45,30979,100],{"class":99},[45,30981,30982],{"class":103},"original:",[45,30984,100],{"class":99},[45,30986,3079],{"class":85},[45,30988,30941],{"class":2070},[45,30990,109],{"class":85},[45,30992,30993,30995,30997,30999,31002,31004,31006,31009],{"class":47,"line":142},[45,30994,2065],{"class":92},[45,30996,96],{"class":85},[45,30998,100],{"class":99},[45,31000,31001],{"class":103},"copied:",[45,31003,100],{"class":99},[45,31005,3079],{"class":85},[45,31007,31008],{"class":2070}," copied",[45,31010,109],{"class":85},[14,31012,177],{},[36,31014,31016],{"className":38,"code":31015,"language":40,"meta":41,"style":41},"original: [1, 2, 3]\ncopied: [1, 2, 3, 4]\n",[18,31017,31018,31038],{"__ignoreMap":41},[45,31019,31020,31022,31024,31026,31028,31030,31032,31034,31036],{"class":47,"line":48},[45,31021,30875],{"class":51},[45,31023,457],{"class":85},[45,31025,8575],{"class":85},[45,31027,8578],{"class":59},[45,31029,3079],{"class":85},[45,31031,3754],{"class":59},[45,31033,3079],{"class":85},[45,31035,777],{"class":59},[45,31037,8594],{"class":85},[45,31039,31040,31042,31044,31046,31048,31050,31052,31054,31056,31058,31060],{"class":47,"line":63},[45,31041,30957],{"class":51},[45,31043,457],{"class":85},[45,31045,8575],{"class":85},[45,31047,8578],{"class":59},[45,31049,3079],{"class":85},[45,31051,3754],{"class":59},[45,31053,3079],{"class":85},[45,31055,777],{"class":59},[45,31057,3079],{"class":85},[45,31059,8591],{"class":59},[45,31061,8594],{"class":85},[14,31063,2419,31064,31067],{},[2421,31065,31066],{},"shallow copy"," creates a new outer object, but nested objects may still be shared.",[36,31069,31071],{"className":38,"code":31070,"language":40,"meta":41,"style":41},"a = [[1, 2], [3, 4]]\nb = a.copy()\n\nb[0].append(99)\n\nprint(a)\nprint(b)\n",[18,31072,31073,31101,31115,31119,31139,31143,31153],{"__ignoreMap":41},[45,31074,31075,31077,31079,31082,31084,31086,31088,31091,31093,31095,31097,31099],{"class":47,"line":48},[45,31076,11541],{"class":51},[45,31078,56],{"class":55},[45,31080,31081],{"class":85}," [[",[45,31083,8578],{"class":59},[45,31085,3079],{"class":85},[45,31087,3754],{"class":59},[45,31089,31090],{"class":85},"],",[45,31092,8575],{"class":85},[45,31094,11184],{"class":59},[45,31096,3079],{"class":85},[45,31098,8591],{"class":59},[45,31100,29005],{"class":85},[45,31102,31103,31105,31107,31109,31111,31113],{"class":47,"line":63},[45,31104,19315],{"class":51},[45,31106,56],{"class":55},[45,31108,24972],{"class":51},[45,31110,240],{"class":85},[45,31112,30946],{"class":2070},[45,31114,2495],{"class":85},[45,31116,31117],{"class":47,"line":70},[45,31118,67],{"emptyLinePlaceholder":66},[45,31120,31121,31123,31125,31127,31130,31132,31134,31137],{"class":47,"line":89},[45,31122,10384],{"class":51},[45,31124,9680],{"class":85},[45,31126,10482],{"class":59},[45,31128,31129],{"class":85},"].",[45,31131,21025],{"class":2070},[45,31133,96],{"class":85},[45,31135,31136],{"class":59},"99",[45,31138,109],{"class":85},[45,31140,31141],{"class":47,"line":112},[45,31142,67],{"emptyLinePlaceholder":66},[45,31144,31145,31147,31149,31151],{"class":47,"line":126},[45,31146,2065],{"class":92},[45,31148,96],{"class":85},[45,31150,236],{"class":2070},[45,31152,109],{"class":85},[45,31154,31155,31157,31159,31161],{"class":47,"line":142},[45,31156,2065],{"class":92},[45,31158,96],{"class":85},[45,31160,10384],{"class":2070},[45,31162,109],{"class":85},[14,31164,177],{},[36,31166,31168],{"className":38,"code":31167,"language":40,"meta":41,"style":41},"[[1, 2, 99], [3, 4]]\n[[1, 2, 99], [3, 4]]\n",[18,31169,31170,31198],{"__ignoreMap":41},[45,31171,31172,31175,31177,31179,31181,31183,31186,31188,31190,31192,31194,31196],{"class":47,"line":48},[45,31173,31174],{"class":85},"[[",[45,31176,8578],{"class":59},[45,31178,3079],{"class":85},[45,31180,3754],{"class":59},[45,31182,3079],{"class":85},[45,31184,31185],{"class":59}," 99",[45,31187,31090],{"class":85},[45,31189,8575],{"class":85},[45,31191,11184],{"class":59},[45,31193,3079],{"class":85},[45,31195,8591],{"class":59},[45,31197,29005],{"class":85},[45,31199,31200,31202,31204,31206,31208,31210,31212,31214,31216,31218,31220,31222],{"class":47,"line":63},[45,31201,31174],{"class":85},[45,31203,8578],{"class":59},[45,31205,3079],{"class":85},[45,31207,3754],{"class":59},[45,31209,3079],{"class":85},[45,31211,31185],{"class":59},[45,31213,31090],{"class":85},[45,31215,8575],{"class":85},[45,31217,11184],{"class":59},[45,31219,3079],{"class":85},[45,31221,8591],{"class":59},[45,31223,29005],{"class":85},[14,31225,31226,1316,31228,31230],{},[18,31227,236],{},[18,31229,10384],{}," are different outer lists, but the inner lists are still shared.",[14,31232,31233,31234,21,31238,31242,31243,240],{},"For more on this, see ",[236,31235,31237],{"href":31236},"\u002Freference\u002Fpython-shallow-copy-vs-deep-copy-explained","Python shallow copy vs deep copy explained",[236,31239,31241],{"href":31240},"\u002Fhow-to\u002Fhow-to-copy-a-list-in-python\u002F","how to copy a list in Python",", and the ",[236,31244,31246,31247,2575],{"href":31245},"\u002Freference\u002Fpython-list-copy-method\u002F","Python list ",[18,31248,31249],{},"copy()",[31,31251,31253],{"id":31252},"how-to-check-and-reason-about-object-behavior","How to check and reason about object behavior",[14,31255,31256],{},"When you are unsure whether something changes in place, use this process:",[195,31258,31259,31262,31265,31270],{},[198,31260,31261],{},"Look at the data type first",[198,31263,31264],{},"Check whether the method changes the object or returns a new one",[198,31266,165,31267,31269],{},[18,31268,30520],{}," only as a helper",[198,31271,31272],{},"Read the method documentation when needed",[14,31274,31275],{},"Useful debugging commands:",[36,31277,31279],{"className":38,"code":31278,"language":40,"meta":41,"style":41},"print(type(value))\nprint(id(value))\nprint(value)\nprint(value is other_value)\nhelp(list.append)\nhelp(str.replace)\n",[18,31280,31281,31295,31310,31320,31335,31349],{"__ignoreMap":41},[45,31282,31283,31285,31287,31289,31291,31293],{"class":47,"line":48},[45,31284,2065],{"class":92},[45,31286,96],{"class":85},[45,31288,4340],{"class":2109},[45,31290,96],{"class":85},[45,31292,12722],{"class":2070},[45,31294,4348],{"class":85},[45,31296,31297,31299,31301,31304,31306,31308],{"class":47,"line":63},[45,31298,2065],{"class":92},[45,31300,96],{"class":85},[45,31302,31303],{"class":92},"id",[45,31305,96],{"class":85},[45,31307,12722],{"class":2070},[45,31309,4348],{"class":85},[45,31311,31312,31314,31316,31318],{"class":47,"line":70},[45,31313,2065],{"class":92},[45,31315,96],{"class":85},[45,31317,12722],{"class":2070},[45,31319,109],{"class":85},[45,31321,31322,31324,31326,31328,31330,31333],{"class":47,"line":89},[45,31323,2065],{"class":92},[45,31325,96],{"class":85},[45,31327,1476],{"class":2070},[45,31329,1285],{"class":73},[45,31331,31332],{"class":2070}," other_value",[45,31334,109],{"class":85},[45,31336,31337,31339,31341,31343,31345,31347],{"class":47,"line":112},[45,31338,2104],{"class":92},[45,31340,96],{"class":85},[45,31342,14463],{"class":2109},[45,31344,240],{"class":85},[45,31346,21025],{"class":3095},[45,31348,109],{"class":85},[45,31350,31351,31353,31355,31357,31359,31362],{"class":47,"line":126},[45,31352,2104],{"class":92},[45,31354,96],{"class":85},[45,31356,29733],{"class":2109},[45,31358,240],{"class":85},[45,31360,31361],{"class":3095},"replace",[45,31363,109],{"class":85},[14,31365,31366],{},"A practical example:",[36,31368,31370],{"className":38,"code":31369,"language":40,"meta":41,"style":41},"text = \"hello\"\nnew_text = text.replace(\"h\", \"H\")\n\nprint(text)\nprint(new_text)\nprint(text is new_text)\n",[18,31371,31372,31384,31417,31421,31431,31442],{"__ignoreMap":41},[45,31373,31374,31376,31378,31380,31382],{"class":47,"line":48},[45,31375,10297],{"class":51},[45,31377,56],{"class":55},[45,31379,1565],{"class":99},[45,31381,18782],{"class":103},[45,31383,1571],{"class":99},[45,31385,31386,31389,31391,31393,31395,31397,31399,31401,31404,31406,31408,31410,31413,31415],{"class":47,"line":63},[45,31387,31388],{"class":51},"new_text ",[45,31390,56],{"class":55},[45,31392,10339],{"class":51},[45,31394,240],{"class":85},[45,31396,31361],{"class":2070},[45,31398,96],{"class":85},[45,31400,100],{"class":99},[45,31402,31403],{"class":103},"h",[45,31405,100],{"class":99},[45,31407,3079],{"class":85},[45,31409,1565],{"class":99},[45,31411,31412],{"class":103},"H",[45,31414,100],{"class":99},[45,31416,109],{"class":85},[45,31418,31419],{"class":47,"line":70},[45,31420,67],{"emptyLinePlaceholder":66},[45,31422,31423,31425,31427,31429],{"class":47,"line":89},[45,31424,2065],{"class":92},[45,31426,96],{"class":85},[45,31428,5000],{"class":2070},[45,31430,109],{"class":85},[45,31432,31433,31435,31437,31440],{"class":47,"line":112},[45,31434,2065],{"class":92},[45,31436,96],{"class":85},[45,31438,31439],{"class":2070},"new_text",[45,31441,109],{"class":85},[45,31443,31444,31446,31448,31450,31452,31455],{"class":47,"line":126},[45,31445,2065],{"class":92},[45,31447,96],{"class":85},[45,31449,10297],{"class":2070},[45,31451,1285],{"class":73},[45,31453,31454],{"class":2070}," new_text",[45,31456,109],{"class":85},[14,31458,177],{},[36,31460,31462],{"className":38,"code":31461,"language":40,"meta":41,"style":41},"hello\nHello\nFalse\n",[18,31463,31464,31469,31473],{"__ignoreMap":41},[45,31465,31466],{"class":47,"line":48},[45,31467,31468],{"class":51},"hello\n",[45,31470,31471],{"class":47,"line":63},[45,31472,15823],{"class":51},[45,31474,31475],{"class":47,"line":70},[45,31476,2265],{"class":481},[14,31478,31479,31482],{},[18,31480,31481],{},"str.replace()"," returns a new string.",[14,31484,31485],{},"Now compare that to a list method:",[36,31487,31489],{"className":38,"code":31488,"language":40,"meta":41,"style":41},"numbers = [1, 2]\nresult = numbers.append(3)\n\nprint(numbers)\nprint(result)\n",[18,31490,31491,31507,31525,31529,31539],{"__ignoreMap":41},[45,31492,31493,31495,31497,31499,31501,31503,31505],{"class":47,"line":48},[45,31494,8570],{"class":51},[45,31496,56],{"class":55},[45,31498,8575],{"class":85},[45,31500,8578],{"class":59},[45,31502,3079],{"class":85},[45,31504,3754],{"class":59},[45,31506,8594],{"class":85},[45,31508,31509,31511,31513,31515,31517,31519,31521,31523],{"class":47,"line":63},[45,31510,3595],{"class":51},[45,31512,56],{"class":55},[45,31514,8626],{"class":51},[45,31516,240],{"class":85},[45,31518,21025],{"class":2070},[45,31520,96],{"class":85},[45,31522,11184],{"class":59},[45,31524,109],{"class":85},[45,31526,31527],{"class":47,"line":70},[45,31528,67],{"emptyLinePlaceholder":66},[45,31530,31531,31533,31535,31537],{"class":47,"line":89},[45,31532,2065],{"class":92},[45,31534,96],{"class":85},[45,31536,9069],{"class":2070},[45,31538,109],{"class":85},[45,31540,31541,31543,31545,31547],{"class":47,"line":112},[45,31542,2065],{"class":92},[45,31544,96],{"class":85},[45,31546,3620],{"class":2070},[45,31548,109],{"class":85},[14,31550,177],{},[36,31552,31554],{"className":38,"code":31553,"language":40,"meta":41,"style":41},"[1, 2, 3]\nNone\n",[18,31555,31556,31572],{"__ignoreMap":41},[45,31557,31558,31560,31562,31564,31566,31568,31570],{"class":47,"line":48},[45,31559,9680],{"class":85},[45,31561,8578],{"class":59},[45,31563,3079],{"class":85},[45,31565,3754],{"class":59},[45,31567,3079],{"class":85},[45,31569,777],{"class":59},[45,31571,8594],{"class":85},[45,31573,31574],{"class":47,"line":63},[45,31575,31576],{"class":481},"None\n",[14,31578,31579,31582,31583,240],{},[18,31580,31581],{},"list.append()"," changes the list in place and returns ",[18,31584,5997],{},[14,31586,31587],{},"That difference is very important when reading Python code.",[31,31589,31591],{"id":31590},"beginner-rules-to-remember","Beginner rules to remember",[14,31593,31594],{},"These rules are not everything, but they will help you avoid many bugs:",[195,31596,31597,31600,31603,31606,31609],{},[198,31598,31599],{},"Strings and numbers usually create new objects",[198,31601,31602],{},"Lists, dictionaries, and sets often change in place",[198,31604,31605],{},"Be careful when reusing the same list or dictionary in multiple places",[198,31607,31608],{},"Copy mutable data when you need an independent version",[198,31610,31611,31612,31615],{},"Do not assume ",[18,31613,31614],{},"a = b"," makes a copy",[31,31617,5903],{"id":5902},[14,31619,31620],{},"These are some very common beginner problems related to mutability:",[195,31622,31623,31626,31629,31632,31635],{},[198,31624,31625],{},"Assigning one list to another variable and expecting a separate copy",[198,31627,31628],{},"Changing a list inside a function and being surprised the original changed",[198,31630,31631],{},"Expecting string methods to modify the original string",[198,31633,31634],{},"Using mutable default values in functions without understanding shared state",[198,31636,31637],{},"Assuming all Python objects behave like lists",[14,31639,31640],{},"Example of a shared-list mistake:",[36,31642,31644],{"className":38,"code":31643,"language":40,"meta":41,"style":41},"a = [1, 2]\nb = a\nb.append(3)\n\nprint(a)  # Unexpected for many beginners\n",[18,31645,31646,31662,31670,31684,31688],{"__ignoreMap":41},[45,31647,31648,31650,31652,31654,31656,31658,31660],{"class":47,"line":48},[45,31649,11541],{"class":51},[45,31651,56],{"class":55},[45,31653,8575],{"class":85},[45,31655,8578],{"class":59},[45,31657,3079],{"class":85},[45,31659,3754],{"class":59},[45,31661,8594],{"class":85},[45,31663,31664,31666,31668],{"class":47,"line":63},[45,31665,19315],{"class":51},[45,31667,56],{"class":55},[45,31669,30311],{"class":51},[45,31671,31672,31674,31676,31678,31680,31682],{"class":47,"line":70},[45,31673,10384],{"class":51},[45,31675,240],{"class":85},[45,31677,21025],{"class":2070},[45,31679,96],{"class":85},[45,31681,11184],{"class":59},[45,31683,109],{"class":85},[45,31685,31686],{"class":47,"line":89},[45,31687,67],{"emptyLinePlaceholder":66},[45,31689,31690,31692,31694,31696,31698],{"class":47,"line":112},[45,31691,2065],{"class":92},[45,31693,96],{"class":85},[45,31695,236],{"class":2070},[45,31697,9146],{"class":85},[45,31699,31700],{"class":7624},"  # Unexpected for many beginners\n",[14,31702,177],{},[36,31704,31705],{"className":38,"code":13375,"language":40,"meta":41,"style":41},[18,31706,31707],{"__ignoreMap":41},[45,31708,31709,31711,31713,31715,31717,31719,31721],{"class":47,"line":48},[45,31710,9680],{"class":85},[45,31712,8578],{"class":59},[45,31714,3079],{"class":85},[45,31716,3754],{"class":59},[45,31718,3079],{"class":85},[45,31720,777],{"class":59},[45,31722,8594],{"class":85},[14,31724,31725],{},"Example of expecting a string to change in place:",[36,31727,31729],{"className":38,"code":31728,"language":40,"meta":41,"style":41},"name = \"sam\"\nname.upper()\nprint(name)\n",[18,31730,31731,31744,31754],{"__ignoreMap":41},[45,31732,31733,31735,31737,31739,31742],{"class":47,"line":48},[45,31734,1560],{"class":51},[45,31736,56],{"class":55},[45,31738,1565],{"class":99},[45,31740,31741],{"class":103},"sam",[45,31743,1571],{"class":99},[45,31745,31746,31748,31750,31752],{"class":47,"line":63},[45,31747,3096],{"class":51},[45,31749,240],{"class":85},[45,31751,10327],{"class":2070},[45,31753,2495],{"class":85},[45,31755,31756,31758,31760,31762],{"class":47,"line":70},[45,31757,2065],{"class":92},[45,31759,96],{"class":85},[45,31761,3096],{"class":2070},[45,31763,109],{"class":85},[14,31765,177],{},[36,31767,31769],{"className":38,"code":31768,"language":40,"meta":41,"style":41},"sam\n",[18,31770,31771],{"__ignoreMap":41},[45,31772,31773],{"class":47,"line":48},[45,31774,31768],{"class":51},[14,31776,31777],{},"To keep the result, assign it:",[36,31779,31781],{"className":38,"code":31780,"language":40,"meta":41,"style":41},"name = \"sam\"\nname = name.upper()\nprint(name)\n",[18,31782,31783,31795,31809],{"__ignoreMap":41},[45,31784,31785,31787,31789,31791,31793],{"class":47,"line":48},[45,31786,1560],{"class":51},[45,31788,56],{"class":55},[45,31790,1565],{"class":99},[45,31792,31741],{"class":103},[45,31794,1571],{"class":99},[45,31796,31797,31799,31801,31803,31805,31807],{"class":47,"line":63},[45,31798,1560],{"class":51},[45,31800,56],{"class":55},[45,31802,3083],{"class":51},[45,31804,240],{"class":85},[45,31806,10327],{"class":2070},[45,31808,2495],{"class":85},[45,31810,31811,31813,31815,31817],{"class":47,"line":70},[45,31812,2065],{"class":92},[45,31814,96],{"class":85},[45,31816,3096],{"class":2070},[45,31818,109],{"class":85},[14,31820,177],{},[36,31822,31823],{"className":38,"code":29843,"language":40,"meta":41,"style":41},[18,31824,31825],{"__ignoreMap":41},[45,31826,31827],{"class":47,"line":48},[45,31828,29843],{"class":2771},[31,31830,2281],{"id":2280},[712,31832,31834],{"id":31833},"are-strings-mutable-in-python","Are strings mutable in Python?",[14,31836,31837],{},"No. Strings are immutable. String operations return a new string.",[712,31839,31841],{"id":31840},"are-lists-mutable-in-python","Are lists mutable in Python?",[14,31843,31844,31845,21,31848,21,31851,25,31854,240],{},"Yes. Lists can be changed in place with methods like ",[18,31846,31847],{},"append()",[18,31849,31850],{},"insert()",[18,31852,31853],{},"remove()",[18,31855,31856],{},"sort()",[712,31858,14769,31860,31862],{"id":31859},"does-a-b-copy-a-list",[18,31861,31614],{}," copy a list?",[14,31864,31865],{},"No. It usually creates another reference to the same list.",[712,31867,31869],{"id":31868},"why-did-my-function-change-my-original-list","Why did my function change my original list?",[14,31871,31872],{},"Because lists are mutable, and the function may have modified the same object you passed in.",[712,31874,31876],{"id":31875},"how-do-i-make-an-independent-copy-of-a-list","How do I make an independent copy of a list?",[14,31878,165,31879,31882,31883,31886,31887,31889],{},[18,31880,31881],{},"list.copy()",", slicing like ",[18,31884,31885],{},"my_list[:]",", or the ",[18,31888,30946],{}," module for more complex cases.",[31,31891,2342],{"id":2341},[195,31893,31894,31898,31902,31906,31910,31916,31920,31926],{},[198,31895,31896],{},[236,31897,27366],{"href":30255},[198,31899,31900],{},[236,31901,29917],{"href":29916},[198,31903,31904],{},[236,31905,8557],{"href":30258},[198,31907,31908],{},[236,31909,5879],{"href":4579},[198,31911,31912],{},[236,31913,31246,31914,2575],{"href":31245},[18,31915,31249],{},[198,31917,31918],{},[236,31919,31237],{"href":31236},[198,31921,31922],{},[236,31923,18104,31924,19075],{"href":30517},[18,31925,30520],{},[198,31927,31928],{},[236,31929,31930],{"href":31240},"How to copy a list in Python",[2367,31932,31933],{},"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 .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 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 .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 .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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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 .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}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":31935},[31936,31937,31938,31939,31940,31941,31942,31943,31944,31945,31946,31954],{"id":33,"depth":63,"text":34},{"id":29544,"depth":63,"text":29545},{"id":29722,"depth":63,"text":29723},{"id":29920,"depth":63,"text":29921},{"id":30261,"depth":63,"text":30262},{"id":30528,"depth":63,"text":30529},{"id":30800,"depth":63,"text":30801},{"id":31252,"depth":63,"text":31253},{"id":31590,"depth":63,"text":31591},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":31947},[31948,31949,31950,31952,31953],{"id":31833,"depth":70,"text":31834},{"id":31840,"depth":70,"text":31841},{"id":31859,"depth":70,"text":31951},"Does a = b copy a list?",{"id":31868,"depth":70,"text":31869},{"id":31875,"depth":70,"text":31876},{"id":2341,"depth":63,"text":2342},"Master mutability in python explained mutable vs immutable types in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types",{"title":29389,"description":31955},"learn\u002Fmutability-in-python-explained-mutable-vs-immutable-types","6u4w-aU6uw1y455fa3EXmFs5bdrBLZRVgRni06cAo2M",{"id":31962,"title":31963,"body":31964,"description":33469,"extension":2399,"meta":33470,"navigation":66,"path":33471,"seo":33472,"stem":33473,"__hash__":33474},"content\u002Flearn\u002Fobject-oriented-programming-in-python-explained.md","Object-Oriented Programming in Python Explained",{"type":7,"value":31965,"toc":33447},[31966,31969,31982,31985,31997,32000,32004,32116,32120,32129,32132,32162,32165,32169,32172,32175,32202,32205,32211,32215,32218,32221,32238,32241,32245,32248,32251,32324,32328,32362,32365,32385,32394,32398,32404,32410,32412,32541,32545,32559,32561,32578,32583,32594,32600,32605,32608,32686,32690,32699,32701,32725,32731,32738,32742,32745,32769,32771,32794,32797,32801,32804,32818,32821,33006,33010,33036,33039,33042,33097,33101,33109,33112,33116,33119,33122,33138,33141,33164,33166,33169,33188,33191,33227,33233,33272,33275,33332,33335,33361,33363,33367,33370,33374,33377,33381,33384,33391,33396,33400,33403,33405,33438,33444],[10,31967,31963],{"id":31968},"object-oriented-programming-in-python-explained",[14,31970,31971,31972,31975,31976,1316,31979,240],{},"Object-oriented programming, usually called ",[2421,31973,31974],{},"OOP",", is a way to organize Python code using ",[2421,31977,31978],{},"classes",[2421,31980,31981],{},"objects",[14,31983,31984],{},"For beginners, the main idea is simple:",[195,31986,31987,31992],{},[198,31988,2419,31989,31991],{},[2421,31990,2441],{}," describes what something should look like and do",[198,31993,19451,31994,31996],{},[2421,31995,13471],{}," is one real thing created from that class",[14,31998,31999],{},"OOP helps you keep related data and behavior together. It is useful in bigger programs, and you will see it often in real Python code, libraries, and tutorials.",[31,32001,32003],{"id":32002},"a-quick-example","A quick example",[36,32005,32007],{"className":38,"code":32006,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(self.name, 'says woof')\n\ndog = Dog('Max')\ndog.bark()\n",[18,32008,32009,32017,32033,32045,32049,32061,32084,32088,32106],{"__ignoreMap":41},[45,32010,32011,32013,32015],{"class":47,"line":48},[45,32012,2441],{"class":2440},[45,32014,2445],{"class":2444},[45,32016,86],{"class":85},[45,32018,32019,32021,32023,32025,32027,32029,32031],{"class":47,"line":63},[45,32020,2452],{"class":2440},[45,32022,3072],{"class":92},[45,32024,96],{"class":85},[45,32026,2427],{"class":2461},[45,32028,3079],{"class":85},[45,32030,3083],{"class":3082},[45,32032,2464],{"class":85},[45,32034,32035,32037,32039,32041,32043],{"class":47,"line":70},[45,32036,3090],{"class":2771},[45,32038,240],{"class":85},[45,32040,3096],{"class":3095},[45,32042,3099],{"class":55},[45,32044,3102],{"class":51},[45,32046,32047],{"class":47,"line":89},[45,32048,67],{"emptyLinePlaceholder":66},[45,32050,32051,32053,32055,32057,32059],{"class":47,"line":112},[45,32052,2452],{"class":2440},[45,32054,2456],{"class":2455},[45,32056,96],{"class":85},[45,32058,2427],{"class":2461},[45,32060,2464],{"class":85},[45,32062,32063,32065,32067,32069,32071,32073,32075,32077,32080,32082],{"class":47,"line":126},[45,32064,1923],{"class":92},[45,32066,96],{"class":85},[45,32068,2427],{"class":2771},[45,32070,240],{"class":85},[45,32072,3096],{"class":3095},[45,32074,3079],{"class":85},[45,32076,9199],{"class":99},[45,32078,32079],{"class":103},"says woof",[45,32081,7901],{"class":99},[45,32083,109],{"class":85},[45,32085,32086],{"class":47,"line":142},[45,32087,67],{"emptyLinePlaceholder":66},[45,32089,32090,32092,32094,32096,32098,32100,32102,32104],{"class":47,"line":149},[45,32091,21886],{"class":51},[45,32093,56],{"class":55},[45,32095,2445],{"class":2070},[45,32097,96],{"class":85},[45,32099,7901],{"class":99},[45,32101,19306],{"class":103},[45,32103,7901],{"class":99},[45,32105,109],{"class":85},[45,32107,32108,32110,32112,32114],{"class":47,"line":679},[45,32109,9105],{"class":51},[45,32111,240],{"class":85},[45,32113,2505],{"class":2070},[45,32115,2495],{"class":85},[14,32117,32118],{},[2421,32119,177],{},[36,32121,32123],{"className":38,"code":32122,"language":40,"meta":41,"style":41},"Max says woof\n",[18,32124,32125],{"__ignoreMap":41},[45,32126,32127],{"class":47,"line":48},[45,32128,32122],{"class":51},[14,32130,32131],{},"What this shows:",[195,32133,32134,32140,32148,32156],{},[198,32135,32136,6578,32138],{},[18,32137,2570],{},[2421,32139,2441],{},[198,32141,32142,32145,32146],{},[18,32143,32144],{},"dog = Dog('Max')"," creates an ",[2421,32147,13471],{},[198,32149,32150,32152,32153],{},[18,32151,3044],{}," is an ",[2421,32154,32155],{},"attribute",[198,32157,32158,6578,32160],{},[18,32159,2574],{},[2421,32161,2423],{},[14,32163,32164],{},"This is the core OOP idea: a class defines behavior, and an object is a real instance created from that class.",[31,32166,32168],{"id":32167},"what-object-oriented-programming-means","What object-oriented programming means",[14,32170,32171],{},"OOP is a way to organize code using classes and objects.",[14,32173,32174],{},"In Python:",[195,32176,32177,32182,32187,32190,32196],{},[198,32178,2419,32179,32181],{},[2421,32180,2441],{}," is a blueprint",[198,32183,19451,32184,32186],{},[2421,32185,13471],{}," is a value created from that blueprint",[198,32188,32189],{},"Objects can store data and perform actions",[198,32191,32192,32193],{},"Object data is usually stored in ",[2421,32194,32195],{},"attributes",[198,32197,32198,32199],{},"Object actions are usually written as ",[2421,32200,32201],{},"methods",[14,32203,32204],{},"This lets you group related pieces of code together instead of keeping everything separate.",[14,32206,32207,32208,32210],{},"For example, a ",[18,32209,2570],{}," object can store its name and also know how to bark. That is often easier to understand than managing names and dog functions in different places.",[31,32212,32214],{"id":32213},"why-beginners-should-learn-oop","Why beginners should learn OOP",[14,32216,32217],{},"Beginners do not need OOP for every program, but it is important to understand the basic idea.",[14,32219,32220],{},"OOP helps because it:",[195,32222,32223,32226,32229,32232,32235],{},[198,32224,32225],{},"Groups related data and behavior together",[198,32227,32228],{},"Makes larger programs easier to read",[198,32230,32231],{},"Helps model real things like users, files, or game characters",[198,32233,32234],{},"Appears often in real-world Python code",[198,32236,32237],{},"Makes many libraries easier to understand",[14,32239,32240],{},"If you are still learning variables, functions, and lists, that is fine. You do not need to master OOP right away. But once you know the basics, learning OOP will help you read and write more structured code.",[31,32242,32244],{"id":32243},"classes-and-objects","Classes and objects",[14,32246,32247],{},"A class defines what objects of that type should have.",[14,32249,32250],{},"An object is created by calling the class name.",[36,32252,32254],{"className":38,"code":32253,"language":40,"meta":41,"style":41},"class Dog:\n    pass\n\ndog1 = Dog()\ndog2 = Dog()\n\nprint(type(dog1))\nprint(type(dog2))\n",[18,32255,32256,32264,32268,32272,32282,32292,32296,32310],{"__ignoreMap":41},[45,32257,32258,32260,32262],{"class":47,"line":48},[45,32259,2441],{"class":2440},[45,32261,2445],{"class":2444},[45,32263,86],{"class":85},[45,32265,32266],{"class":47,"line":63},[45,32267,5306],{"class":73},[45,32269,32270],{"class":47,"line":70},[45,32271,67],{"emptyLinePlaceholder":66},[45,32273,32274,32276,32278,32280],{"class":47,"line":89},[45,32275,19541],{"class":51},[45,32277,56],{"class":55},[45,32279,2445],{"class":2070},[45,32281,2495],{"class":85},[45,32283,32284,32286,32288,32290],{"class":47,"line":112},[45,32285,19564],{"class":51},[45,32287,56],{"class":55},[45,32289,2445],{"class":2070},[45,32291,2495],{"class":85},[45,32293,32294],{"class":47,"line":126},[45,32295,67],{"emptyLinePlaceholder":66},[45,32297,32298,32300,32302,32304,32306,32308],{"class":47,"line":142},[45,32299,2065],{"class":92},[45,32301,96],{"class":85},[45,32303,4340],{"class":2109},[45,32305,96],{"class":85},[45,32307,19595],{"class":2070},[45,32309,4348],{"class":85},[45,32311,32312,32314,32316,32318,32320,32322],{"class":47,"line":149},[45,32313,2065],{"class":92},[45,32315,96],{"class":85},[45,32317,4340],{"class":2109},[45,32319,96],{"class":85},[45,32321,19610],{"class":2070},[45,32323,4348],{"class":85},[14,32325,32326],{},[2421,32327,177],{},[36,32329,32331],{"className":38,"code":32330,"language":40,"meta":41,"style":41},"\u003Cclass '__main__.Dog'>\n\u003Cclass '__main__.Dog'>\n",[18,32332,32333,32348],{"__ignoreMap":41},[45,32334,32335,32337,32339,32341,32344,32346],{"class":47,"line":48},[45,32336,79],{"class":55},[45,32338,2441],{"class":2440},[45,32340,9199],{"class":99},[45,32342,32343],{"class":103},"__main__.Dog",[45,32345,7901],{"class":99},[45,32347,13484],{"class":55},[45,32349,32350,32352,32354,32356,32358,32360],{"class":47,"line":63},[45,32351,79],{"class":55},[45,32353,2441],{"class":2440},[45,32355,9199],{"class":99},[45,32357,32343],{"class":103},[45,32359,7901],{"class":99},[45,32361,13484],{"class":55},[14,32363,32364],{},"Important points:",[195,32366,32367,32372,32379,32382],{},[198,32368,32369,32371],{},[18,32370,2570],{}," is the class",[198,32373,32374,1316,32376,32378],{},[18,32375,19595],{},[18,32377,19610],{}," are objects",[198,32380,32381],{},"Multiple objects can come from the same class",[198,32383,32384],{},"Each object can later have its own data",[14,32386,32387,32388,32390,32391,240],{},"If you want a full beginner explanation, see ",[236,32389,2560],{"href":19945}," or the glossary page on ",[236,32392,32393],{"href":19948},"what a class is in Python",[31,32395,32397],{"id":32396},"attributes-and-methods","Attributes and methods",[14,32399,32400,32403],{},[2421,32401,32402],{},"Attributes"," are values stored on an object.",[14,32405,32406,32409],{},[2421,32407,32408],{},"Methods"," are functions defined inside a class.",[14,32411,2617],{},[36,32413,32415],{"className":38,"code":32414,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(self.name, 'says woof')\n\ndog = Dog('Bella')\n\nprint(dog.name)\ndog.bark()\n",[18,32416,32417,32425,32441,32453,32457,32469,32491,32495,32513,32517,32531],{"__ignoreMap":41},[45,32418,32419,32421,32423],{"class":47,"line":48},[45,32420,2441],{"class":2440},[45,32422,2445],{"class":2444},[45,32424,86],{"class":85},[45,32426,32427,32429,32431,32433,32435,32437,32439],{"class":47,"line":63},[45,32428,2452],{"class":2440},[45,32430,3072],{"class":92},[45,32432,96],{"class":85},[45,32434,2427],{"class":2461},[45,32436,3079],{"class":85},[45,32438,3083],{"class":3082},[45,32440,2464],{"class":85},[45,32442,32443,32445,32447,32449,32451],{"class":47,"line":70},[45,32444,3090],{"class":2771},[45,32446,240],{"class":85},[45,32448,3096],{"class":3095},[45,32450,3099],{"class":55},[45,32452,3102],{"class":51},[45,32454,32455],{"class":47,"line":89},[45,32456,67],{"emptyLinePlaceholder":66},[45,32458,32459,32461,32463,32465,32467],{"class":47,"line":112},[45,32460,2452],{"class":2440},[45,32462,2456],{"class":2455},[45,32464,96],{"class":85},[45,32466,2427],{"class":2461},[45,32468,2464],{"class":85},[45,32470,32471,32473,32475,32477,32479,32481,32483,32485,32487,32489],{"class":47,"line":126},[45,32472,1923],{"class":92},[45,32474,96],{"class":85},[45,32476,2427],{"class":2771},[45,32478,240],{"class":85},[45,32480,3096],{"class":3095},[45,32482,3079],{"class":85},[45,32484,9199],{"class":99},[45,32486,32079],{"class":103},[45,32488,7901],{"class":99},[45,32490,109],{"class":85},[45,32492,32493],{"class":47,"line":142},[45,32494,67],{"emptyLinePlaceholder":66},[45,32496,32497,32499,32501,32503,32505,32507,32509,32511],{"class":47,"line":149},[45,32498,21886],{"class":51},[45,32500,56],{"class":55},[45,32502,2445],{"class":2070},[45,32504,96],{"class":85},[45,32506,7901],{"class":99},[45,32508,19326],{"class":103},[45,32510,7901],{"class":99},[45,32512,109],{"class":85},[45,32514,32515],{"class":47,"line":679},[45,32516,67],{"emptyLinePlaceholder":66},[45,32518,32519,32521,32523,32525,32527,32529],{"class":47,"line":686},[45,32520,2065],{"class":92},[45,32522,96],{"class":85},[45,32524,9105],{"class":2070},[45,32526,240],{"class":85},[45,32528,3096],{"class":3095},[45,32530,109],{"class":85},[45,32532,32533,32535,32537,32539],{"class":47,"line":2229},[45,32534,9105],{"class":51},[45,32536,240],{"class":85},[45,32538,2505],{"class":2070},[45,32540,2495],{"class":85},[14,32542,32543],{},[2421,32544,177],{},[36,32546,32548],{"className":38,"code":32547,"language":40,"meta":41,"style":41},"Bella\nBella says woof\n",[18,32549,32550,32554],{"__ignoreMap":41},[45,32551,32552],{"class":47,"line":48},[45,32553,19420],{"class":51},[45,32555,32556],{"class":47,"line":63},[45,32557,32558],{"class":51},"Bella says woof\n",[14,32560,6570],{},[195,32562,32563,32568,32573],{},[198,32564,32565,32567],{},[18,32566,3096],{}," is an attribute",[198,32569,32570,32572],{},[18,32571,2574],{}," is a method",[198,32574,32575,32576],{},"The method uses the object's own data with ",[18,32577,3044],{},[14,32579,1732,32580,32582],{},[18,32581,2427],{}," parameter refers to the current object. It lets a method work with that object's attributes and other methods.",[14,32584,32585,32586,32589,32590,240],{},"If you want more detail, read ",[236,32587,32588],{"href":22038},"basic methods in Python classes explained"," and the glossary page on ",[236,32591,32593],{"href":32592},"\u002Fglossary\u002Fwhat-is-a-method-in-python\u002F","what a method is in Python",[31,32595,32597,32598],{"id":32596},"the-role-of-__init__","The role of ",[18,32599,3262],{},[14,32601,32602,32604],{},[18,32603,3262],{}," runs when a new object is created.",[14,32606,32607],{},"It is commonly used to set starting attribute values.",[36,32609,32611],{"className":38,"code":32610,"language":40,"meta":41,"style":41},"class User:\n    def __init__(self, username):\n        self.username = username\n\nuser = User(\"alice\")\nprint(user.username)\n",[18,32612,32613,32621,32637,32649,32653,32672],{"__ignoreMap":41},[45,32614,32615,32617,32619],{"class":47,"line":48},[45,32616,2441],{"class":2440},[45,32618,20515],{"class":2444},[45,32620,86],{"class":85},[45,32622,32623,32625,32627,32629,32631,32633,32635],{"class":47,"line":63},[45,32624,2452],{"class":2440},[45,32626,3072],{"class":92},[45,32628,96],{"class":85},[45,32630,2427],{"class":2461},[45,32632,3079],{"class":85},[45,32634,20532],{"class":3082},[45,32636,2464],{"class":85},[45,32638,32639,32641,32643,32645,32647],{"class":47,"line":70},[45,32640,3090],{"class":2771},[45,32642,240],{"class":85},[45,32644,7977],{"class":3095},[45,32646,3099],{"class":55},[45,32648,20552],{"class":51},[45,32650,32651],{"class":47,"line":89},[45,32652,67],{"emptyLinePlaceholder":66},[45,32654,32655,32657,32659,32661,32663,32665,32668,32670],{"class":47,"line":112},[45,32656,30062],{"class":51},[45,32658,56],{"class":55},[45,32660,20515],{"class":2070},[45,32662,96],{"class":85},[45,32664,100],{"class":99},[45,32666,32667],{"class":103},"alice",[45,32669,100],{"class":99},[45,32671,109],{"class":85},[45,32673,32674,32676,32678,32680,32682,32684],{"class":47,"line":126},[45,32675,2065],{"class":92},[45,32677,96],{"class":85},[45,32679,30087],{"class":2070},[45,32681,240],{"class":85},[45,32683,7977],{"class":3095},[45,32685,109],{"class":85},[14,32687,32688],{},[2421,32689,177],{},[36,32691,32693],{"className":38,"code":32692,"language":40,"meta":41,"style":41},"alice\n",[18,32694,32695],{"__ignoreMap":41},[45,32696,32697],{"class":47,"line":48},[45,32698,32692],{"class":51},[14,32700,6806],{},[195,32702,32703,32710,32715],{},[198,32704,32705,32706,32709],{},"Python creates a new ",[18,32707,32708],{},"User"," object",[198,32711,32712,32714],{},[18,32713,3262],{}," runs automatically",[198,32716,32717,32718,32721,32722],{},"The value ",[18,32719,32720],{},"\"alice\""," is stored in ",[18,32723,32724],{},"self.username",[14,32726,32727,32728,32730],{},"Beginners can think of ",[18,32729,3262],{}," as the setup method for a class.",[14,32732,32733,32734,240],{},"For a full guide, see ",[236,32735,3267,32736,3270],{"href":3266},[18,32737,3262],{},[31,32739,32741],{"id":32740},"a-simple-mental-model","A simple mental model",[14,32743,32744],{},"A useful way to think about OOP is this:",[195,32746,32747,32753,32759,32764],{},[198,32748,32749,32752],{},[2421,32750,32751],{},"Class"," = recipe or template",[198,32754,32755,32758],{},[2421,32756,32757],{},"Object"," = thing made from the recipe",[198,32760,32761,32763],{},[2421,32762,32402],{}," = data about the thing",[198,32765,32766,32768],{},[2421,32767,32408],{}," = actions the thing can do",[14,32770,466],{},[195,32772,32773,32778,32784,32789],{},[198,32774,32775,32776],{},"Class: ",[18,32777,2570],{},[198,32779,32780,32781],{},"Object: ",[18,32782,32783],{},"Dog(\"Max\")",[198,32785,32786,32787],{},"Attribute: ",[18,32788,3096],{},[198,32790,32791,32792],{},"Method: ",[18,32793,2574],{},[14,32795,32796],{},"This mental model is not perfect for every situation, but it is very helpful when you are starting.",[31,32798,32800],{"id":32799},"when-to-use-oop-and-when-not-to","When to use OOP and when not to",[14,32802,32803],{},"Use OOP when:",[195,32805,32806,32809,32812,32815],{},[198,32807,32808],{},"Related data and functions belong together",[198,32810,32811],{},"You need many similar objects",[198,32813,32814],{},"You are modeling things like users, accounts, products, or game characters",[198,32816,32817],{},"You want code to be easier to organize as a program grows",[14,32819,32820],{},"Simple example: if you have many dogs, each with its own name, age, and behavior, a class can make sense.",[36,32822,32824],{"className":38,"code":32823,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def describe(self):\n        print(self.name, \"is\", self.age, \"years old\")\n\ndog1 = Dog(\"Max\", 3)\ndog2 = Dog(\"Luna\", 5)\n\ndog1.describe()\ndog2.describe()\n",[18,32825,32826,32834,32854,32866,32878,32882,32895,32933,32937,32959,32981,32985,32996],{"__ignoreMap":41},[45,32827,32828,32830,32832],{"class":47,"line":48},[45,32829,2441],{"class":2440},[45,32831,2445],{"class":2444},[45,32833,86],{"class":85},[45,32835,32836,32838,32840,32842,32844,32846,32848,32850,32852],{"class":47,"line":63},[45,32837,2452],{"class":2440},[45,32839,3072],{"class":92},[45,32841,96],{"class":85},[45,32843,2427],{"class":2461},[45,32845,3079],{"class":85},[45,32847,3083],{"class":3082},[45,32849,3079],{"class":85},[45,32851,3305],{"class":3082},[45,32853,2464],{"class":85},[45,32855,32856,32858,32860,32862,32864],{"class":47,"line":70},[45,32857,3090],{"class":2771},[45,32859,240],{"class":85},[45,32861,3096],{"class":3095},[45,32863,3099],{"class":55},[45,32865,3102],{"class":51},[45,32867,32868,32870,32872,32874,32876],{"class":47,"line":89},[45,32869,3090],{"class":2771},[45,32871,240],{"class":85},[45,32873,2082],{"class":3095},[45,32875,3099],{"class":55},[45,32877,3320],{"class":51},[45,32879,32880],{"class":47,"line":112},[45,32881,67],{"emptyLinePlaceholder":66},[45,32883,32884,32886,32889,32891,32893],{"class":47,"line":126},[45,32885,2452],{"class":2440},[45,32887,32888],{"class":2455}," describe",[45,32890,96],{"class":85},[45,32892,2427],{"class":2461},[45,32894,2464],{"class":85},[45,32896,32897,32899,32901,32903,32905,32907,32909,32911,32913,32915,32917,32919,32921,32923,32925,32927,32929,32931],{"class":47,"line":142},[45,32898,1923],{"class":92},[45,32900,96],{"class":85},[45,32902,2427],{"class":2771},[45,32904,240],{"class":85},[45,32906,3096],{"class":3095},[45,32908,3079],{"class":85},[45,32910,1565],{"class":99},[45,32912,1285],{"class":103},[45,32914,100],{"class":99},[45,32916,3079],{"class":85},[45,32918,3136],{"class":2771},[45,32920,240],{"class":85},[45,32922,2082],{"class":3095},[45,32924,3079],{"class":85},[45,32926,1565],{"class":99},[45,32928,11992],{"class":103},[45,32930,100],{"class":99},[45,32932,109],{"class":85},[45,32934,32935],{"class":47,"line":149},[45,32936,67],{"emptyLinePlaceholder":66},[45,32938,32939,32941,32943,32945,32947,32949,32951,32953,32955,32957],{"class":47,"line":679},[45,32940,19541],{"class":51},[45,32942,56],{"class":55},[45,32944,2445],{"class":2070},[45,32946,96],{"class":85},[45,32948,100],{"class":99},[45,32950,19306],{"class":103},[45,32952,100],{"class":99},[45,32954,3079],{"class":85},[45,32956,777],{"class":59},[45,32958,109],{"class":85},[45,32960,32961,32963,32965,32967,32969,32971,32973,32975,32977,32979],{"class":47,"line":686},[45,32962,19564],{"class":51},[45,32964,56],{"class":55},[45,32966,2445],{"class":2070},[45,32968,96],{"class":85},[45,32970,100],{"class":99},[45,32972,7044],{"class":103},[45,32974,100],{"class":99},[45,32976,3079],{"class":85},[45,32978,1222],{"class":59},[45,32980,109],{"class":85},[45,32982,32983],{"class":47,"line":2229},[45,32984,67],{"emptyLinePlaceholder":66},[45,32986,32987,32989,32991,32994],{"class":47,"line":2236},[45,32988,19595],{"class":51},[45,32990,240],{"class":85},[45,32992,32993],{"class":2070},"describe",[45,32995,2495],{"class":85},[45,32997,32998,33000,33002,33004],{"class":47,"line":5728},[45,32999,19610],{"class":51},[45,33001,240],{"class":85},[45,33003,32993],{"class":2070},[45,33005,2495],{"class":85},[14,33007,33008],{},[2421,33009,177],{},[36,33011,33013],{"className":38,"code":33012,"language":40,"meta":41,"style":41},"Max is 3 years old\nLuna is 5 years old\n",[18,33014,33015,33026],{"__ignoreMap":41},[45,33016,33017,33020,33022,33024],{"class":47,"line":48},[45,33018,33019],{"class":51},"Max ",[45,33021,1285],{"class":55},[45,33023,777],{"class":59},[45,33025,12047],{"class":51},[45,33027,33028,33030,33032,33034],{"class":47,"line":63},[45,33029,7060],{"class":51},[45,33031,1285],{"class":55},[45,33033,1222],{"class":59},[45,33035,12047],{"class":51},[14,33037,33038],{},"Do not force OOP into very small scripts.",[14,33040,33041],{},"For example, if you only need to add two numbers or read one file once, a simple function is often better.",[36,33043,33045],{"className":38,"code":33044,"language":40,"meta":41,"style":41},"def add_numbers(a, b):\n    return a + b\n\nprint(add_numbers(2, 3))\n",[18,33046,33047,33064,33074,33078],{"__ignoreMap":41},[45,33048,33049,33051,33054,33056,33058,33060,33062],{"class":47,"line":48},[45,33050,2757],{"class":2440},[45,33052,33053],{"class":2455}," add_numbers",[45,33055,96],{"class":85},[45,33057,236],{"class":3082},[45,33059,3079],{"class":85},[45,33061,11115],{"class":3082},[45,33063,2464],{"class":85},[45,33065,33066,33068,33070,33072],{"class":47,"line":63},[45,33067,4681],{"class":73},[45,33069,11124],{"class":51},[45,33071,8850],{"class":55},[45,33073,11129],{"class":51},[45,33075,33076],{"class":47,"line":70},[45,33077,67],{"emptyLinePlaceholder":66},[45,33079,33080,33082,33084,33087,33089,33091,33093,33095],{"class":47,"line":89},[45,33081,2065],{"class":92},[45,33083,96],{"class":85},[45,33085,33086],{"class":2070},"add_numbers",[45,33088,96],{"class":85},[45,33090,9367],{"class":59},[45,33092,3079],{"class":85},[45,33094,777],{"class":59},[45,33096,4348],{"class":85},[14,33098,33099],{},[2421,33100,177],{},[36,33102,33103],{"className":38,"code":11570,"language":40,"meta":41,"style":41},[18,33104,33105],{"__ignoreMap":41},[45,33106,33107],{"class":47,"line":48},[45,33108,11570],{"class":59},[14,33110,33111],{},"OOP is a tool. It is useful in the right situation, but not required for every Python program.",[31,33113,33115],{"id":33114},"what-this-page-does-not-cover-in-depth","What this page does not cover in depth",[14,33117,33118],{},"This page is a concept overview, not a full class reference.",[14,33120,33121],{},"It does not explain these topics in detail:",[195,33123,33124,33127,33132,33135],{},[198,33125,33126],{},"Full class syntax",[198,33128,33129,33130],{},"All details of ",[18,33131,3262],{},[198,33133,33134],{},"Inheritance",[198,33136,33137],{},"The difference between instance variables and class variables",[14,33139,33140],{},"For the next step, use these focused pages:",[195,33142,33143,33147,33153,33158],{},[198,33144,33145],{},[236,33146,2560],{"href":19945},[198,33148,33149],{},[236,33150,3267,33151,3270],{"href":3266},[18,33152,3262],{},[198,33154,33155],{},[236,33156,4313],{"href":33157},"\u002Flearn\u002Finstance-vs-class-variables-in-python\u002F",[198,33159,33160],{},[236,33161,33163],{"href":33162},"\u002Fhow-to\u002Fhow-to-inherit-from-a-class-in-python\u002F","how to inherit from a class in Python",[31,33165,5903],{"id":5902},[14,33167,33168],{},"These are common beginner mistakes when learning OOP:",[195,33170,33171,33174,33177,33182,33185],{},[198,33172,33173],{},"Thinking OOP is required for all Python programs",[198,33175,33176],{},"Confusing a class with an object",[198,33178,33179,33180],{},"Forgetting that methods need ",[18,33181,2427],{},[198,33183,33184],{},"Treating attributes and methods as the same thing",[198,33186,33187],{},"Using classes before understanding functions and variables",[14,33189,33190],{},"Here is a common method mistake:",[36,33192,33194],{"className":38,"code":33193,"language":40,"meta":41,"style":41},"class Dog:\n    def bark():\n        print(\"woof\")\n",[18,33195,33196,33204,33212],{"__ignoreMap":41},[45,33197,33198,33200,33202],{"class":47,"line":48},[45,33199,2441],{"class":2440},[45,33201,2445],{"class":2444},[45,33203,86],{"class":85},[45,33205,33206,33208,33210],{"class":47,"line":63},[45,33207,2452],{"class":2440},[45,33209,2456],{"class":2455},[45,33211,3828],{"class":85},[45,33213,33214,33216,33218,33220,33223,33225],{"class":47,"line":70},[45,33215,1923],{"class":92},[45,33217,96],{"class":85},[45,33219,100],{"class":99},[45,33221,33222],{"class":103},"woof",[45,33224,100],{"class":99},[45,33226,109],{"class":85},[14,33228,33229,33230,33232],{},"This method is missing ",[18,33231,2427],{},". In most beginner class methods, you need:",[36,33234,33236],{"className":38,"code":33235,"language":40,"meta":41,"style":41},"class Dog:\n    def bark(self):\n        print(\"woof\")\n",[18,33237,33238,33246,33258],{"__ignoreMap":41},[45,33239,33240,33242,33244],{"class":47,"line":48},[45,33241,2441],{"class":2440},[45,33243,2445],{"class":2444},[45,33245,86],{"class":85},[45,33247,33248,33250,33252,33254,33256],{"class":47,"line":63},[45,33249,2452],{"class":2440},[45,33251,2456],{"class":2455},[45,33253,96],{"class":85},[45,33255,2427],{"class":2461},[45,33257,2464],{"class":85},[45,33259,33260,33262,33264,33266,33268,33270],{"class":47,"line":70},[45,33261,1923],{"class":92},[45,33263,96],{"class":85},[45,33265,100],{"class":99},[45,33267,33222],{"class":103},[45,33269,100],{"class":99},[45,33271,109],{"class":85},[14,33273,33274],{},"If you want to inspect an object while learning, these commands can help:",[36,33276,33278],{"className":38,"code":33277,"language":40,"meta":41,"style":41},"print(type(dog))\nprint(dog.name)\nprint(dir(dog))\nhelp(Dog)\n",[18,33279,33280,33294,33308,33322],{"__ignoreMap":41},[45,33281,33282,33284,33286,33288,33290,33292],{"class":47,"line":48},[45,33283,2065],{"class":92},[45,33285,96],{"class":85},[45,33287,4340],{"class":2109},[45,33289,96],{"class":85},[45,33291,9105],{"class":2070},[45,33293,4348],{"class":85},[45,33295,33296,33298,33300,33302,33304,33306],{"class":47,"line":63},[45,33297,2065],{"class":92},[45,33299,96],{"class":85},[45,33301,9105],{"class":2070},[45,33303,240],{"class":85},[45,33305,3096],{"class":3095},[45,33307,109],{"class":85},[45,33309,33310,33312,33314,33316,33318,33320],{"class":47,"line":70},[45,33311,2065],{"class":92},[45,33313,96],{"class":85},[45,33315,4357],{"class":92},[45,33317,96],{"class":85},[45,33319,9105],{"class":2070},[45,33321,4348],{"class":85},[45,33323,33324,33326,33328,33330],{"class":47,"line":89},[45,33325,2104],{"class":92},[45,33327,96],{"class":85},[45,33329,2570],{"class":2070},[45,33331,109],{"class":85},[14,33333,33334],{},"What these do:",[195,33336,33337,33343,33349,33355],{},[198,33338,33339,33342],{},[18,33340,33341],{},"type(dog)"," shows the object's type",[198,33344,33345,33348],{},[18,33346,33347],{},"dog.name"," shows one attribute value",[198,33350,33351,33354],{},[18,33352,33353],{},"dir(dog)"," lists available attributes and methods",[198,33356,33357,33360],{},[18,33358,33359],{},"help(Dog)"," shows built-in help for the class",[31,33362,2281],{"id":2280},[712,33364,33366],{"id":33365},"what-is-object-oriented-programming-in-simple-words","What is object-oriented programming in simple words?",[14,33368,33369],{},"It is a way to organize code by creating classes and objects that keep related data and actions together.",[712,33371,33373],{"id":33372},"what-is-the-difference-between-a-class-and-an-object","What is the difference between a class and an object?",[14,33375,33376],{},"A class is a blueprint. An object is a real instance created from that blueprint.",[712,33378,33380],{"id":33379},"do-beginners-need-oop-right-away","Do beginners need OOP right away?",[14,33382,33383],{},"Not at the start, but beginners should learn the basic idea after functions, variables, and data types.",[712,33385,33387,33388,33390],{"id":33386},"what-is-self-in-a-python-class","What is ",[18,33389,2427],{}," in a Python class?",[14,33392,33393,33395],{},[18,33394,2427],{}," refers to the current object, so methods can access that object's attributes and other methods.",[712,33397,33399],{"id":33398},"is-oop-better-than-functions","Is OOP better than functions?",[14,33401,33402],{},"Not always. OOP is helpful for organizing related data and behavior, but simple functions are often enough for small scripts.",[31,33404,2342],{"id":2341},[195,33406,33407,33411,33417,33421,33427,33433],{},[198,33408,33409],{},[236,33410,2560],{"href":19945},[198,33412,33413],{},[236,33414,1732,33415,3270],{"href":3266},[18,33416,3262],{},[198,33418,33419],{},[236,33420,22039],{"href":22038},[198,33422,33423],{},[236,33424,33426],{"href":33425},"\u002Fhow-to\u002Fhow-to-create-a-class-in-python\u002F","How to create a class in Python",[198,33428,33429],{},[236,33430,33432],{"href":33431},"\u002Fhow-to\u002Fhow-to-create-an-object-in-python\u002F","How to create an object in Python",[198,33434,33435],{},[236,33436,33437],{"href":20479},"What is an object in Python?",[14,33439,33440,33441,33443],{},"Next, learn the practical building blocks: classes, objects, ",[18,33442,3262],{},", and methods.",[2367,33445,33446],{},"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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .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 .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}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":33448},[33449,33450,33451,33452,33453,33454,33456,33457,33458,33459,33460,33468],{"id":32002,"depth":63,"text":32003},{"id":32167,"depth":63,"text":32168},{"id":32213,"depth":63,"text":32214},{"id":32243,"depth":63,"text":32244},{"id":32396,"depth":63,"text":32397},{"id":32596,"depth":63,"text":33455},"The role of __init__",{"id":32740,"depth":63,"text":32741},{"id":32799,"depth":63,"text":32800},{"id":33114,"depth":63,"text":33115},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":33461},[33462,33463,33464,33465,33467],{"id":33365,"depth":70,"text":33366},{"id":33372,"depth":70,"text":33373},{"id":33379,"depth":70,"text":33380},{"id":33386,"depth":70,"text":33466},"What is self in a Python class?",{"id":33398,"depth":70,"text":33399},{"id":2341,"depth":63,"text":2342},"Master object oriented programming in python explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fobject-oriented-programming-in-python-explained",{"title":31963,"description":33469},"learn\u002Fobject-oriented-programming-in-python-explained","P_yUnKejR4SEauR0Jk0gA5gonb7-gUX2iE11Q2c7uH0",{"id":33476,"title":33477,"body":33478,"description":35640,"extension":2399,"meta":35641,"navigation":66,"path":35642,"seo":35643,"stem":35644,"__hash__":35645},"content\u002Flearn\u002Fpython-booleans-explained-true-and-false.md","Python Booleans Explained (True and False)",{"type":7,"value":33479,"toc":35590},[33480,33483,33486,33494,33500,33504,33507,33517,33520,33531,33534,33559,33561,33573,33576,33610,33612,33624,33627,33631,33634,33643,33646,33696,33698,33711,33714,33718,33721,33770,33772,33789,33793,33801,33863,33865,33877,33880,33884,33887,33893,33900,33944,33946,33958,33963,33968,33975,34027,34029,34046,34050,34053,34129,34131,34143,34147,34150,34175,34177,34185,34188,34192,34199,34332,34334,34398,34400,34421,34428,34432,34435,34440,34450,34494,34496,34504,34509,34518,34560,34562,34570,34574,34579,34609,34611,34619,34622,34626,34634,34637,34674,34679,34681,34755,34757,34778,34787,34802,34806,34809,34813,34854,34856,34864,34868,34910,34912,34926,34935,35001,35003,35015,35019,35084,35086,35103,35110,35112,35115,35128,35131,35133,35148,35150,35163,35174,35182,35192,35194,35237,35239,35282,35289,35292,35336,35339,35378,35388,35391,35452,35454,35495,35498,35500,35504,35511,35515,35521,35525,35533,35539,35542,35546,35560,35562,35587],[10,33481,33477],{"id":33482},"python-booleans-explained-true-and-false",[14,33484,33485],{},"Boolean values are one of the most important parts of Python.",[14,33487,33488,33489,230,33491,33493],{},"A Boolean is simply a value that is either ",[18,33490,229],{},[18,33492,233],{},". You will use Booleans when checking conditions, comparing values, and controlling what your program does next.",[14,33495,33496,33497,33499],{},"If you are new to Python, this is the foundation you need before moving on to ",[236,33498,549],{"href":548}," and comparisons.",[31,33501,33503],{"id":33502},"what-a-boolean-is","What a Boolean is",[14,33505,33506],{},"A Boolean is a data type with only two possible values:",[195,33508,33509,33513],{},[198,33510,33511],{},[18,33512,229],{},[198,33514,33515],{},[18,33516,233],{},[14,33518,33519],{},"Booleans are used to represent conditions such as:",[195,33521,33522,33525,33528],{},[198,33523,33524],{},"yes or no",[198,33526,33527],{},"on or off",[198,33529,33530],{},"true or false",[14,33532,33533],{},"In Python, you must write them with capital first letters:",[36,33535,33537],{"className":38,"code":33536,"language":40,"meta":41,"style":41},"print(True)\nprint(False)\n",[18,33538,33539,33549],{"__ignoreMap":41},[45,33540,33541,33543,33545,33547],{"class":47,"line":48},[45,33542,2065],{"class":92},[45,33544,96],{"class":85},[45,33546,229],{"class":481},[45,33548,109],{"class":85},[45,33550,33551,33553,33555,33557],{"class":47,"line":63},[45,33552,2065],{"class":92},[45,33554,96],{"class":85},[45,33556,233],{"class":481},[45,33558,109],{"class":85},[14,33560,177],{},[36,33562,33563],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,33564,33565,33569],{"__ignoreMap":41},[45,33566,33567],{"class":47,"line":48},[45,33568,2270],{"class":481},[45,33570,33571],{"class":47,"line":63},[45,33572,2265],{"class":481},[14,33574,33575],{},"Booleans are often used in conditions and comparisons. For example:",[36,33577,33579],{"className":38,"code":33578,"language":40,"meta":41,"style":41},"print(5 > 3)\nprint(10 == 2)\n",[18,33580,33581,33596],{"__ignoreMap":41},[45,33582,33583,33585,33587,33589,33592,33594],{"class":47,"line":48},[45,33584,2065],{"class":92},[45,33586,96],{"class":85},[45,33588,3749],{"class":59},[45,33590,33591],{"class":55}," >",[45,33593,777],{"class":59},[45,33595,109],{"class":85},[45,33597,33598,33600,33602,33604,33606,33608],{"class":47,"line":63},[45,33599,2065],{"class":92},[45,33601,96],{"class":85},[45,33603,11786],{"class":59},[45,33605,9334],{"class":55},[45,33607,3754],{"class":59},[45,33609,109],{"class":85},[14,33611,177],{},[36,33613,33614],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,33615,33616,33620],{"__ignoreMap":41},[45,33617,33618],{"class":47,"line":48},[45,33619,2270],{"class":481},[45,33621,33622],{"class":47,"line":63},[45,33623,2265],{"class":481},[14,33625,33626],{},"Here, Python checks each comparison and returns a Boolean result.",[31,33628,33630],{"id":33629},"how-to-create-boolean-values","How to create Boolean values",[14,33632,33633],{},"There are a few common ways to create Boolean values in Python.",[712,33635,33637,33638,230,33640,33642],{"id":33636},"assign-true-or-false-directly","Assign ",[18,33639,229],{},[18,33641,233],{}," directly",[14,33644,33645],{},"You can store a Boolean in a variable:",[36,33647,33649],{"className":38,"code":33648,"language":40,"meta":41,"style":41},"is_logged_in = False\nhas_permission = True\n\nprint(is_logged_in)\nprint(has_permission)\n",[18,33650,33651,33661,33670,33674,33685],{"__ignoreMap":41},[45,33652,33653,33656,33658],{"class":47,"line":48},[45,33654,33655],{"class":51},"is_logged_in ",[45,33657,56],{"class":55},[45,33659,33660],{"class":481}," False\n",[45,33662,33663,33666,33668],{"class":47,"line":63},[45,33664,33665],{"class":51},"has_permission ",[45,33667,56],{"class":55},[45,33669,482],{"class":481},[45,33671,33672],{"class":47,"line":70},[45,33673,67],{"emptyLinePlaceholder":66},[45,33675,33676,33678,33680,33683],{"class":47,"line":89},[45,33677,2065],{"class":92},[45,33679,96],{"class":85},[45,33681,33682],{"class":2070},"is_logged_in",[45,33684,109],{"class":85},[45,33686,33687,33689,33691,33694],{"class":47,"line":112},[45,33688,2065],{"class":92},[45,33690,96],{"class":85},[45,33692,33693],{"class":2070},"has_permission",[45,33695,109],{"class":85},[14,33697,177],{},[36,33699,33701],{"className":38,"code":33700,"language":40,"meta":41,"style":41},"False\nTrue\n",[18,33702,33703,33707],{"__ignoreMap":41},[45,33704,33705],{"class":47,"line":48},[45,33706,2265],{"class":481},[45,33708,33709],{"class":47,"line":63},[45,33710,2270],{"class":481},[14,33712,33713],{},"This is useful when you want to keep track of a state or setting.",[712,33715,33717],{"id":33716},"use-a-comparison","Use a comparison",[14,33719,33720],{},"Comparisons often produce Boolean values:",[36,33722,33724],{"className":38,"code":33723,"language":40,"meta":41,"style":41},"print(5 > 3)\nprint(10 == 2)\nprint(7 != 4)\n",[18,33725,33726,33740,33754],{"__ignoreMap":41},[45,33727,33728,33730,33732,33734,33736,33738],{"class":47,"line":48},[45,33729,2065],{"class":92},[45,33731,96],{"class":85},[45,33733,3749],{"class":59},[45,33735,33591],{"class":55},[45,33737,777],{"class":59},[45,33739,109],{"class":85},[45,33741,33742,33744,33746,33748,33750,33752],{"class":47,"line":63},[45,33743,2065],{"class":92},[45,33745,96],{"class":85},[45,33747,11786],{"class":59},[45,33749,9334],{"class":55},[45,33751,3754],{"class":59},[45,33753,109],{"class":85},[45,33755,33756,33758,33760,33763,33766,33768],{"class":47,"line":70},[45,33757,2065],{"class":92},[45,33759,96],{"class":85},[45,33761,33762],{"class":59},"7",[45,33764,33765],{"class":55}," !=",[45,33767,8591],{"class":59},[45,33769,109],{"class":85},[14,33771,177],{},[36,33773,33775],{"className":38,"code":33774,"language":40,"meta":41,"style":41},"True\nFalse\nTrue\n",[18,33776,33777,33781,33785],{"__ignoreMap":41},[45,33778,33779],{"class":47,"line":48},[45,33780,2270],{"class":481},[45,33782,33783],{"class":47,"line":63},[45,33784,2265],{"class":481},[45,33786,33787],{"class":47,"line":70},[45,33788,2270],{"class":481},[712,33790,33792],{"id":33791},"return-a-boolean-from-a-function","Return a Boolean from a function",[14,33794,33795,33796,230,33798,33800],{},"Functions can return ",[18,33797,229],{},[18,33799,233],{}," to answer a question:",[36,33802,33804],{"className":38,"code":33803,"language":40,"meta":41,"style":41},"def is_adult(age):\n    return age >= 18\n\nprint(is_adult(20))\nprint(is_adult(15))\n",[18,33805,33806,33819,33829,33833,33848],{"__ignoreMap":41},[45,33807,33808,33810,33813,33815,33817],{"class":47,"line":48},[45,33809,2757],{"class":2440},[45,33811,33812],{"class":2455}," is_adult",[45,33814,96],{"class":85},[45,33816,2082],{"class":3082},[45,33818,2464],{"class":85},[45,33820,33821,33823,33825,33827],{"class":47,"line":63},[45,33822,4681],{"class":73},[45,33824,76],{"class":51},[45,33826,600],{"class":55},[45,33828,60],{"class":59},[45,33830,33831],{"class":47,"line":70},[45,33832,67],{"emptyLinePlaceholder":66},[45,33834,33835,33837,33839,33842,33844,33846],{"class":47,"line":89},[45,33836,2065],{"class":92},[45,33838,96],{"class":85},[45,33840,33841],{"class":2070},"is_adult",[45,33843,96],{"class":85},[45,33845,3372],{"class":59},[45,33847,4348],{"class":85},[45,33849,33850,33852,33854,33856,33858,33861],{"class":47,"line":112},[45,33851,2065],{"class":92},[45,33853,96],{"class":85},[45,33855,33841],{"class":2070},[45,33857,96],{"class":85},[45,33859,33860],{"class":59},"15",[45,33862,4348],{"class":85},[14,33864,177],{},[36,33866,33867],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,33868,33869,33873],{"__ignoreMap":41},[45,33870,33871],{"class":47,"line":48},[45,33872,2270],{"class":481},[45,33874,33875],{"class":47,"line":63},[45,33876,2265],{"class":481},[14,33878,33879],{},"This pattern is very common in real Python code.",[31,33881,33883],{"id":33882},"where-booleans-are-used","Where Booleans are used",[14,33885,33886],{},"Booleans are mainly used to help your program make decisions.",[712,33888,33890,33891,2037],{"id":33889},"in-if-statements","In ",[18,33892,20],{},[14,33894,19451,33895,33897,33898,457],{},[18,33896,20],{}," statement runs code only when a condition is ",[18,33899,229],{},[36,33901,33903],{"className":38,"code":33902,"language":40,"meta":41,"style":41},"age = 20\n\nif age >= 18:\n    print(\"You can vote.\")\n",[18,33904,33905,33913,33917,33929],{"__ignoreMap":41},[45,33906,33907,33909,33911],{"class":47,"line":48},[45,33908,52],{"class":51},[45,33910,56],{"class":55},[45,33912,1883],{"class":59},[45,33914,33915],{"class":47,"line":63},[45,33916,67],{"emptyLinePlaceholder":66},[45,33918,33919,33921,33923,33925,33927],{"class":47,"line":70},[45,33920,20],{"class":73},[45,33922,76],{"class":51},[45,33924,600],{"class":55},[45,33926,121],{"class":59},[45,33928,86],{"class":85},[45,33930,33931,33933,33935,33937,33940,33942],{"class":47,"line":89},[45,33932,93],{"class":92},[45,33934,96],{"class":85},[45,33936,100],{"class":99},[45,33938,33939],{"class":103},"You can vote.",[45,33941,100],{"class":99},[45,33943,109],{"class":85},[14,33945,177],{},[36,33947,33949],{"className":38,"code":33948,"language":40,"meta":41,"style":41},"You can vote.\n",[18,33950,33951],{"__ignoreMap":41},[45,33952,33953,33956],{"class":47,"line":48},[45,33954,33955],{"class":51},"You can vote",[45,33957,2708],{"class":85},[14,33959,33960,33961,240],{},"If you want to learn this next, see ",[236,33962,549],{"href":548},[712,33964,33890,33966,26095],{"id":33965},"in-while-loops",[18,33967,23529],{},[14,33969,2419,33970,33972,33973,457],{},[18,33971,23529],{}," loop continues running while its condition stays ",[18,33974,229],{},[36,33976,33978],{"className":38,"code":33977,"language":40,"meta":41,"style":41},"count = 3\n\nwhile count > 0:\n    print(count)\n    count -= 1\n",[18,33979,33980,33989,33993,34006,34017],{"__ignoreMap":41},[45,33981,33982,33985,33987],{"class":47,"line":48},[45,33983,33984],{"class":51},"count ",[45,33986,56],{"class":55},[45,33988,1481],{"class":59},[45,33990,33991],{"class":47,"line":63},[45,33992,67],{"emptyLinePlaceholder":66},[45,33994,33995,33997,34000,34002,34004],{"class":47,"line":70},[45,33996,23529],{"class":73},[45,33998,33999],{"class":51}," count ",[45,34001,272],{"class":55},[45,34003,749],{"class":59},[45,34005,86],{"class":85},[45,34007,34008,34010,34012,34015],{"class":47,"line":89},[45,34009,93],{"class":92},[45,34011,96],{"class":85},[45,34013,34014],{"class":2070},"count",[45,34016,109],{"class":85},[45,34018,34019,34022,34025],{"class":47,"line":112},[45,34020,34021],{"class":51},"    count ",[45,34023,34024],{"class":55},"-=",[45,34026,12962],{"class":59},[14,34028,177],{},[36,34030,34032],{"className":38,"code":34031,"language":40,"meta":41,"style":41},"3\n2\n1\n",[18,34033,34034,34038,34042],{"__ignoreMap":41},[45,34035,34036],{"class":47,"line":48},[45,34037,13046],{"class":59},[45,34039,34040],{"class":47,"line":63},[45,34041,13041],{"class":59},[45,34043,34044],{"class":47,"line":70},[45,34045,13036],{"class":59},[712,34047,34049],{"id":34048},"in-functions","In functions",[14,34051,34052],{},"Functions often return Booleans so your code can check a condition:",[36,34054,34056],{"className":38,"code":34055,"language":40,"meta":41,"style":41},"def has_long_name(name):\n    return len(name) > 5\n\nprint(has_long_name(\"Sam\"))\nprint(has_long_name(\"Sophia\"))\n",[18,34057,34058,34071,34087,34091,34110],{"__ignoreMap":41},[45,34059,34060,34062,34065,34067,34069],{"class":47,"line":48},[45,34061,2757],{"class":2440},[45,34063,34064],{"class":2455}," has_long_name",[45,34066,96],{"class":85},[45,34068,3096],{"class":3082},[45,34070,2464],{"class":85},[45,34072,34073,34075,34077,34079,34081,34083,34085],{"class":47,"line":63},[45,34074,4681],{"class":73},[45,34076,9139],{"class":92},[45,34078,96],{"class":85},[45,34080,3096],{"class":2070},[45,34082,9146],{"class":85},[45,34084,33591],{"class":55},[45,34086,733],{"class":59},[45,34088,34089],{"class":47,"line":70},[45,34090,67],{"emptyLinePlaceholder":66},[45,34092,34093,34095,34097,34100,34102,34104,34106,34108],{"class":47,"line":89},[45,34094,2065],{"class":92},[45,34096,96],{"class":85},[45,34098,34099],{"class":2070},"has_long_name",[45,34101,96],{"class":85},[45,34103,100],{"class":99},[45,34105,1568],{"class":103},[45,34107,100],{"class":99},[45,34109,4348],{"class":85},[45,34111,34112,34114,34116,34118,34120,34122,34125,34127],{"class":47,"line":112},[45,34113,2065],{"class":92},[45,34115,96],{"class":85},[45,34117,34099],{"class":2070},[45,34119,96],{"class":85},[45,34121,100],{"class":99},[45,34123,34124],{"class":103},"Sophia",[45,34126,100],{"class":99},[45,34128,4348],{"class":85},[14,34130,177],{},[36,34132,34133],{"className":38,"code":33700,"language":40,"meta":41,"style":41},[18,34134,34135,34139],{"__ignoreMap":41},[45,34136,34137],{"class":47,"line":48},[45,34138,2265],{"class":481},[45,34140,34141],{"class":47,"line":63},[45,34142,2270],{"class":481},[712,34144,34146],{"id":34145},"as-flags","As flags",[14,34148,34149],{},"A flag is a variable that stores a simple status:",[36,34151,34153],{"className":38,"code":34152,"language":40,"meta":41,"style":41},"is_game_over = False\nprint(is_game_over)\n",[18,34154,34155,34164],{"__ignoreMap":41},[45,34156,34157,34160,34162],{"class":47,"line":48},[45,34158,34159],{"class":51},"is_game_over ",[45,34161,56],{"class":55},[45,34163,33660],{"class":481},[45,34165,34166,34168,34170,34173],{"class":47,"line":63},[45,34167,2065],{"class":92},[45,34169,96],{"class":85},[45,34171,34172],{"class":2070},"is_game_over",[45,34174,109],{"class":85},[14,34176,177],{},[36,34178,34179],{"className":38,"code":2265,"language":40,"meta":41,"style":41},[18,34180,34181],{"__ignoreMap":41},[45,34182,34183],{"class":47,"line":48},[45,34184,2265],{"class":481},[14,34186,34187],{},"Flags are often used to control program flow.",[31,34189,34191],{"id":34190},"comparison-operators-that-return-booleans","Comparison operators that return Booleans",[14,34193,34194,34195,230,34197,240],{},"Python has several comparison operators that return ",[18,34196,229],{},[18,34198,233],{},[34200,34201,34202,34220],"table",{},[34203,34204,34205],"thead",{},[34206,34207,34208,34212,34215,34217],"tr",{},[34209,34210,34211],"th",{},"Operator",[34209,34213,34214],{},"Meaning",[34209,34216,20427],{},[34209,34218,34219],{},"Result",[34221,34222,34223,34242,34260,34278,34296,34314],"tbody",{},[34206,34224,34225,34230,34233,34238],{},[34226,34227,34228],"td",{},[18,34229,1129],{},[34226,34231,34232],{},"equal to",[34226,34234,34235],{},[18,34236,34237],{},"5 == 5",[34226,34239,34240],{},[18,34241,229],{},[34206,34243,34244,34248,34251,34256],{},[34226,34245,34246],{},[18,34247,1135],{},[34226,34249,34250],{},"not equal to",[34226,34252,34253],{},[18,34254,34255],{},"5 != 3",[34226,34257,34258],{},[18,34259,229],{},[34206,34261,34262,34266,34269,34274],{},[34226,34263,34264],{},[18,34265,272],{},[34226,34267,34268],{},"greater than",[34226,34270,34271],{},[18,34272,34273],{},"7 > 2",[34226,34275,34276],{},[18,34277,229],{},[34206,34279,34280,34284,34287,34292],{},[34226,34281,34282],{},[18,34283,79],{},[34226,34285,34286],{},"less than",[34226,34288,34289],{},[18,34290,34291],{},"2 \u003C 7",[34226,34293,34294],{},[18,34295,229],{},[34206,34297,34298,34302,34305,34310],{},[34226,34299,34300],{},[18,34301,600],{},[34226,34303,34304],{},"greater than or equal to",[34226,34306,34307],{},[18,34308,34309],{},"5 >= 5",[34226,34311,34312],{},[18,34313,229],{},[34206,34315,34316,34320,34323,34328],{},[34226,34317,34318],{},[18,34319,1156],{},[34226,34321,34322],{},"less than or equal to",[34226,34324,34325],{},[18,34326,34327],{},"4 \u003C= 3",[34226,34329,34330],{},[18,34331,233],{},[14,34333,466],{},[36,34335,34337],{"className":38,"code":34336,"language":40,"meta":41,"style":41},"print(8 == 8)\nprint(8 != 5)\nprint(10 \u003C 3)\nprint(6 >= 6)\n",[18,34338,34339,34354,34368,34383],{"__ignoreMap":41},[45,34340,34341,34343,34345,34348,34350,34352],{"class":47,"line":48},[45,34342,2065],{"class":92},[45,34344,96],{"class":85},[45,34346,34347],{"class":59},"8",[45,34349,9334],{"class":55},[45,34351,1254],{"class":59},[45,34353,109],{"class":85},[45,34355,34356,34358,34360,34362,34364,34366],{"class":47,"line":63},[45,34357,2065],{"class":92},[45,34359,96],{"class":85},[45,34361,34347],{"class":59},[45,34363,33765],{"class":55},[45,34365,1222],{"class":59},[45,34367,109],{"class":85},[45,34369,34370,34372,34374,34376,34379,34381],{"class":47,"line":70},[45,34371,2065],{"class":92},[45,34373,96],{"class":85},[45,34375,11786],{"class":59},[45,34377,34378],{"class":55}," \u003C",[45,34380,777],{"class":59},[45,34382,109],{"class":85},[45,34384,34385,34387,34389,34391,34394,34396],{"class":47,"line":89},[45,34386,2065],{"class":92},[45,34388,96],{"class":85},[45,34390,28541],{"class":59},[45,34392,34393],{"class":55}," >=",[45,34395,9288],{"class":59},[45,34397,109],{"class":85},[14,34399,177],{},[36,34401,34403],{"className":38,"code":34402,"language":40,"meta":41,"style":41},"True\nTrue\nFalse\nTrue\n",[18,34404,34405,34409,34413,34417],{"__ignoreMap":41},[45,34406,34407],{"class":47,"line":48},[45,34408,2270],{"class":481},[45,34410,34411],{"class":47,"line":63},[45,34412,2270],{"class":481},[45,34414,34415],{"class":47,"line":70},[45,34416,2265],{"class":481},[45,34418,34419],{"class":47,"line":89},[45,34420,2270],{"class":481},[14,34422,34423,34424,240],{},"These operators are used all the time in conditions. They are especially important when writing ",[236,34425,34427],{"href":34426},"\u002Flearn\u002Fpython-if-else-and-elif-explained\u002F","if, else, and elif statements in Python",[31,34429,34431],{"id":34430},"boolean-operations","Boolean operations",[14,34433,34434],{},"Python also lets you combine Boolean values with logical operators.",[712,34436,34438],{"id":34437},"and",[18,34439,34437],{},[14,34441,34442,34444,34445,34447,34448,240],{},[18,34443,34437],{}," returns ",[18,34446,229],{}," only if both sides are ",[18,34449,229],{},[36,34451,34453],{"className":38,"code":34452,"language":40,"meta":41,"style":41},"age = 20\nhas_id = True\n\nprint(age >= 18 and has_id)\n",[18,34454,34455,34463,34471,34475],{"__ignoreMap":41},[45,34456,34457,34459,34461],{"class":47,"line":48},[45,34458,52],{"class":51},[45,34460,56],{"class":55},[45,34462,1883],{"class":59},[45,34464,34465,34467,34469],{"class":47,"line":63},[45,34466,1888],{"class":51},[45,34468,56],{"class":55},[45,34470,482],{"class":481},[45,34472,34473],{"class":47,"line":70},[45,34474,67],{"emptyLinePlaceholder":66},[45,34476,34477,34479,34481,34483,34485,34487,34490,34492],{"class":47,"line":89},[45,34478,2065],{"class":92},[45,34480,96],{"class":85},[45,34482,52],{"class":2070},[45,34484,600],{"class":55},[45,34486,121],{"class":59},[45,34488,34489],{"class":73}," and",[45,34491,1916],{"class":2070},[45,34493,109],{"class":85},[14,34495,177],{},[36,34497,34498],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,34499,34500],{"__ignoreMap":41},[45,34501,34502],{"class":47,"line":48},[45,34503,2270],{"class":481},[712,34505,34507],{"id":34506},"or",[18,34508,34506],{},[14,34510,34511,34444,34513,34515,34516,240],{},[18,34512,34506],{},[18,34514,229],{}," if at least one side is ",[18,34517,229],{},[36,34519,34521],{"className":38,"code":34520,"language":40,"meta":41,"style":41},"is_weekend = False\nis_holiday = True\n\nprint(is_weekend or is_holiday)\n",[18,34522,34523,34532,34541,34545],{"__ignoreMap":41},[45,34524,34525,34528,34530],{"class":47,"line":48},[45,34526,34527],{"class":51},"is_weekend ",[45,34529,56],{"class":55},[45,34531,33660],{"class":481},[45,34533,34534,34537,34539],{"class":47,"line":63},[45,34535,34536],{"class":51},"is_holiday ",[45,34538,56],{"class":55},[45,34540,482],{"class":481},[45,34542,34543],{"class":47,"line":70},[45,34544,67],{"emptyLinePlaceholder":66},[45,34546,34547,34549,34551,34553,34555,34558],{"class":47,"line":89},[45,34548,2065],{"class":92},[45,34550,96],{"class":85},[45,34552,34527],{"class":2070},[45,34554,34506],{"class":73},[45,34556,34557],{"class":2070}," is_holiday",[45,34559,109],{"class":85},[14,34561,177],{},[36,34563,34564],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,34565,34566],{"__ignoreMap":41},[45,34567,34568],{"class":47,"line":48},[45,34569,2270],{"class":481},[712,34571,34572],{"id":2857},[18,34573,2857],{},[14,34575,34576,34578],{},[18,34577,2857],{}," reverses a Boolean value.",[36,34580,34582],{"className":38,"code":34581,"language":40,"meta":41,"style":41},"is_logged_in = False\n\nprint(not is_logged_in)\n",[18,34583,34584,34592,34596],{"__ignoreMap":41},[45,34585,34586,34588,34590],{"class":47,"line":48},[45,34587,33655],{"class":51},[45,34589,56],{"class":55},[45,34591,33660],{"class":481},[45,34593,34594],{"class":47,"line":63},[45,34595,67],{"emptyLinePlaceholder":66},[45,34597,34598,34600,34602,34604,34607],{"class":47,"line":70},[45,34599,2065],{"class":92},[45,34601,96],{"class":85},[45,34603,2857],{"class":73},[45,34605,34606],{"class":2070}," is_logged_in",[45,34608,109],{"class":85},[14,34610,177],{},[36,34612,34613],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,34614,34615],{"__ignoreMap":41},[45,34616,34617],{"class":47,"line":48},[45,34618,2270],{"class":481},[14,34620,34621],{},"These operators are useful when you need to combine conditions in one statement.",[31,34623,34625],{"id":34624},"truthiness-in-python","Truthiness in Python",[14,34627,34628,34629,34631,34632,240],{},"In Python, some values behave like ",[18,34630,233],{}," in conditions, even though they are not the Boolean value ",[18,34633,233],{},[14,34635,34636],{},"Common falsy values include:",[195,34638,34639,34643,34648,34654,34659,34664,34670],{},[198,34640,34641],{},[18,34642,10482],{},[198,34644,34645],{},[18,34646,34647],{},"0.0",[198,34649,34650,34653],{},[18,34651,34652],{},"''"," empty string",[198,34655,34656,34658],{},[18,34657,21680],{}," empty list",[198,34660,34661,34663],{},[18,34662,21683],{}," empty dictionary",[198,34665,34666,34669],{},[18,34667,34668],{},"set()"," empty set",[198,34671,34672],{},[18,34673,5997],{},[14,34675,34676,34677,240],{},"Most other values behave like ",[18,34678,229],{},[14,34680,466],{},[36,34682,34684],{"className":38,"code":34683,"language":40,"meta":41,"style":41},"print(bool(0))\nprint(bool(''))\nprint(bool('hello'))\nprint(bool([1, 2, 3]))\n",[18,34685,34686,34700,34714,34732],{"__ignoreMap":41},[45,34687,34688,34690,34692,34694,34696,34698],{"class":47,"line":48},[45,34689,2065],{"class":92},[45,34691,96],{"class":85},[45,34693,2110],{"class":2109},[45,34695,96],{"class":85},[45,34697,10482],{"class":59},[45,34699,4348],{"class":85},[45,34701,34702,34704,34706,34708,34710,34712],{"class":47,"line":63},[45,34703,2065],{"class":92},[45,34705,96],{"class":85},[45,34707,2110],{"class":2109},[45,34709,96],{"class":85},[45,34711,34652],{"class":99},[45,34713,4348],{"class":85},[45,34715,34716,34718,34720,34722,34724,34726,34728,34730],{"class":47,"line":70},[45,34717,2065],{"class":92},[45,34719,96],{"class":85},[45,34721,2110],{"class":2109},[45,34723,96],{"class":85},[45,34725,7901],{"class":99},[45,34727,18782],{"class":103},[45,34729,7901],{"class":99},[45,34731,4348],{"class":85},[45,34733,34734,34736,34738,34740,34743,34745,34747,34749,34751,34753],{"class":47,"line":89},[45,34735,2065],{"class":92},[45,34737,96],{"class":85},[45,34739,2110],{"class":2109},[45,34741,34742],{"class":85},"([",[45,34744,8578],{"class":59},[45,34746,3079],{"class":85},[45,34748,3754],{"class":59},[45,34750,3079],{"class":85},[45,34752,777],{"class":59},[45,34754,26913],{"class":85},[14,34756,177],{},[36,34758,34760],{"className":38,"code":34759,"language":40,"meta":41,"style":41},"False\nFalse\nTrue\nTrue\n",[18,34761,34762,34766,34770,34774],{"__ignoreMap":41},[45,34763,34764],{"class":47,"line":48},[45,34765,2265],{"class":481},[45,34767,34768],{"class":47,"line":63},[45,34769,2265],{"class":481},[45,34771,34772],{"class":47,"line":70},[45,34773,2270],{"class":481},[45,34775,34776],{"class":47,"line":89},[45,34777,2270],{"class":481},[14,34779,34780,34781,1316,34784,240],{},"This behavior is called ",[2421,34782,34783],{},"truthy",[2421,34785,34786],{},"falsy",[14,34788,34789,34790,1316,34792,34794,34795,34801],{},"For beginners, it is best to first understand real Boolean values like ",[18,34791,229],{},[18,34793,233],{},". After that, the ",[236,34796,34798,30521],{"href":34797},"\u002Freference\u002Fpython-bool-function-explained\u002F",[18,34799,34800],{},"bool()"," makes more sense.",[31,34803,34805],{"id":34804},"boolean-examples-for-beginners","Boolean examples for beginners",[14,34807,34808],{},"Here are a few simple examples of how Booleans are used in real code.",[712,34810,34812],{"id":34811},"check-an-age-rule","Check an age rule",[36,34814,34816],{"className":38,"code":34815,"language":40,"meta":41,"style":41},"age = 16\ncan_drive = age >= 16\n\nprint(can_drive)\n",[18,34817,34818,34826,34839,34843],{"__ignoreMap":41},[45,34819,34820,34822,34824],{"class":47,"line":48},[45,34821,52],{"class":51},[45,34823,56],{"class":55},[45,34825,986],{"class":59},[45,34827,34828,34831,34833,34835,34837],{"class":47,"line":63},[45,34829,34830],{"class":51},"can_drive ",[45,34832,56],{"class":55},[45,34834,76],{"class":51},[45,34836,600],{"class":55},[45,34838,986],{"class":59},[45,34840,34841],{"class":47,"line":70},[45,34842,67],{"emptyLinePlaceholder":66},[45,34844,34845,34847,34849,34852],{"class":47,"line":89},[45,34846,2065],{"class":92},[45,34848,96],{"class":85},[45,34850,34851],{"class":2070},"can_drive",[45,34853,109],{"class":85},[14,34855,177],{},[36,34857,34858],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,34859,34860],{"__ignoreMap":41},[45,34861,34862],{"class":47,"line":48},[45,34863,2270],{"class":481},[712,34865,34867],{"id":34866},"store-a-status-flag","Store a status flag",[36,34869,34871],{"className":38,"code":34870,"language":40,"meta":41,"style":41},"is_logged_in = False\n\nif not is_logged_in:\n    print(\"Please log in.\")\n",[18,34872,34873,34881,34885,34895],{"__ignoreMap":41},[45,34874,34875,34877,34879],{"class":47,"line":48},[45,34876,33655],{"class":51},[45,34878,56],{"class":55},[45,34880,33660],{"class":481},[45,34882,34883],{"class":47,"line":63},[45,34884,67],{"emptyLinePlaceholder":66},[45,34886,34887,34889,34891,34893],{"class":47,"line":70},[45,34888,20],{"class":73},[45,34890,1296],{"class":55},[45,34892,34606],{"class":51},[45,34894,86],{"class":85},[45,34896,34897,34899,34901,34903,34906,34908],{"class":47,"line":89},[45,34898,93],{"class":92},[45,34900,96],{"class":85},[45,34902,100],{"class":99},[45,34904,34905],{"class":103},"Please log in.",[45,34907,100],{"class":99},[45,34909,109],{"class":85},[14,34911,177],{},[36,34913,34915],{"className":38,"code":34914,"language":40,"meta":41,"style":41},"Please log in.\n",[18,34916,34917],{"__ignoreMap":41},[45,34918,34919,34922,34924],{"class":47,"line":48},[45,34920,34921],{"class":51},"Please log ",[45,34923,8623],{"class":55},[45,34925,2708],{"class":85},[712,34927,34929,34930,230,34932,34934],{"id":34928},"return-true-or-false-from-a-function","Return ",[18,34931,229],{},[18,34933,233],{}," from a function",[36,34936,34938],{"className":38,"code":34937,"language":40,"meta":41,"style":41},"def is_even(number):\n    return number % 2 == 0\n\nprint(is_even(4))\nprint(is_even(7))\n",[18,34939,34940,34953,34968,34972,34987],{"__ignoreMap":41},[45,34941,34942,34944,34947,34949,34951],{"class":47,"line":48},[45,34943,2757],{"class":2440},[45,34945,34946],{"class":2455}," is_even",[45,34948,96],{"class":85},[45,34950,13546],{"class":3082},[45,34952,2464],{"class":85},[45,34954,34955,34957,34959,34961,34963,34965],{"class":47,"line":63},[45,34956,4681],{"class":73},[45,34958,744],{"class":51},[45,34960,9329],{"class":55},[45,34962,3754],{"class":59},[45,34964,9334],{"class":55},[45,34966,34967],{"class":59}," 0\n",[45,34969,34970],{"class":47,"line":70},[45,34971,67],{"emptyLinePlaceholder":66},[45,34973,34974,34976,34978,34981,34983,34985],{"class":47,"line":89},[45,34975,2065],{"class":92},[45,34977,96],{"class":85},[45,34979,34980],{"class":2070},"is_even",[45,34982,96],{"class":85},[45,34984,3584],{"class":59},[45,34986,4348],{"class":85},[45,34988,34989,34991,34993,34995,34997,34999],{"class":47,"line":112},[45,34990,2065],{"class":92},[45,34992,96],{"class":85},[45,34994,34980],{"class":2070},[45,34996,96],{"class":85},[45,34998,33762],{"class":59},[45,35000,4348],{"class":85},[14,35002,177],{},[36,35004,35005],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,35006,35007,35011],{"__ignoreMap":41},[45,35008,35009],{"class":47,"line":48},[45,35010,2270],{"class":481},[45,35012,35013],{"class":47,"line":63},[45,35014,2265],{"class":481},[712,35016,35018],{"id":35017},"use-booleans-to-control-program-flow","Use Booleans to control program flow",[36,35020,35022],{"className":38,"code":35021,"language":40,"meta":41,"style":41},"temperature = 30\n\nif temperature > 25:\n    print(\"It is hot today.\")\nelse:\n    print(\"It is not hot today.\")\n",[18,35023,35024,35032,35036,35048,35063,35069],{"__ignoreMap":41},[45,35025,35026,35028,35030],{"class":47,"line":48},[45,35027,253],{"class":51},[45,35029,56],{"class":55},[45,35031,258],{"class":59},[45,35033,35034],{"class":47,"line":63},[45,35035,67],{"emptyLinePlaceholder":66},[45,35037,35038,35040,35042,35044,35046],{"class":47,"line":70},[45,35039,20],{"class":73},[45,35041,269],{"class":51},[45,35043,272],{"class":55},[45,35045,303],{"class":59},[45,35047,86],{"class":85},[45,35049,35050,35052,35054,35056,35059,35061],{"class":47,"line":89},[45,35051,93],{"class":92},[45,35053,96],{"class":85},[45,35055,100],{"class":99},[45,35057,35058],{"class":103},"It is hot today.",[45,35060,100],{"class":99},[45,35062,109],{"class":85},[45,35064,35065,35067],{"class":47,"line":112},[45,35066,28],{"class":73},[45,35068,86],{"class":85},[45,35070,35071,35073,35075,35077,35080,35082],{"class":47,"line":126},[45,35072,93],{"class":92},[45,35074,96],{"class":85},[45,35076,100],{"class":99},[45,35078,35079],{"class":103},"It is not hot today.",[45,35081,100],{"class":99},[45,35083,109],{"class":85},[14,35085,177],{},[36,35087,35089],{"className":38,"code":35088,"language":40,"meta":41,"style":41},"It is hot today.\n",[18,35090,35091],{"__ignoreMap":41},[45,35092,35093,35096,35098,35101],{"class":47,"line":48},[45,35094,35095],{"class":51},"It ",[45,35097,1285],{"class":55},[45,35099,35100],{"class":51}," hot today",[45,35102,2708],{"class":85},[14,35104,35105,35106,240],{},"Examples like these connect Booleans to other core Python ideas, especially conditions and basic ",[236,35107,35109],{"href":35108},"\u002Flearn\u002Fpython-data-types-overview\u002F","Python data types",[31,35111,1337],{"id":1336},[14,35113,35114],{},"Here are some common Boolean mistakes and how to avoid them.",[712,35116,4032,35118,230,35121,1353,35124,230,35126],{"id":35117},"writing-true-or-false-instead-of-true-or-false",[18,35119,35120],{},"true",[18,35122,35123],{},"false",[18,35125,229],{},[18,35127,233],{},[14,35129,35130],{},"This causes an error because Python is case-sensitive.",[14,35132,3806],{},[36,35134,35136],{"className":38,"code":35135,"language":40,"meta":41,"style":41},"is_ready = true\n",[18,35137,35138],{"__ignoreMap":41},[45,35139,35140,35143,35145],{"class":47,"line":48},[45,35141,35142],{"class":51},"is_ready ",[45,35144,56],{"class":55},[45,35146,35147],{"class":51}," true\n",[14,35149,1466],{},[36,35151,35153],{"className":38,"code":35152,"language":40,"meta":41,"style":41},"is_ready = True\n",[18,35154,35155],{"__ignoreMap":41},[45,35156,35157,35159,35161],{"class":47,"line":48},[45,35158,35142],{"class":51},[45,35160,56],{"class":55},[45,35162,482],{"class":481},[14,35164,35165,35166,35169,35170,240],{},"If you use lowercase names by mistake, Python may raise an error such as ",[18,35167,35168],{},"NameError",". You can learn more about Python problems in ",[236,35171,35173],{"href":35172},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained",[712,35175,1350,35177,1353,35179,35181],{"id":35176},"using-instead-of-in-a-comparison",[18,35178,56],{},[18,35180,1129],{}," in a comparison",[14,35183,35184,35186,35187,35189,35191],{},[18,35185,56],{}," assigns a value.",[7343,35188],{},[18,35190,1129],{}," compares two values.",[14,35193,3806],{},[36,35195,35197],{"className":38,"code":35196,"language":40,"meta":41,"style":41},"age = 18\n\nif age = 18:\n    print(\"Adult\")\n",[18,35198,35199,35207,35211,35223],{"__ignoreMap":41},[45,35200,35201,35203,35205],{"class":47,"line":48},[45,35202,52],{"class":51},[45,35204,56],{"class":55},[45,35206,60],{"class":59},[45,35208,35209],{"class":47,"line":63},[45,35210,67],{"emptyLinePlaceholder":66},[45,35212,35213,35215,35217,35219,35221],{"class":47,"line":70},[45,35214,20],{"class":73},[45,35216,76],{"class":51},[45,35218,56],{"class":55},[45,35220,121],{"class":59},[45,35222,86],{"class":85},[45,35224,35225,35227,35229,35231,35233,35235],{"class":47,"line":89},[45,35226,93],{"class":92},[45,35228,96],{"class":85},[45,35230,100],{"class":99},[45,35232,1710],{"class":103},[45,35234,100],{"class":99},[45,35236,109],{"class":85},[14,35238,1466],{},[36,35240,35242],{"className":38,"code":35241,"language":40,"meta":41,"style":41},"age = 18\n\nif age == 18:\n    print(\"Adult\")\n",[18,35243,35244,35252,35256,35268],{"__ignoreMap":41},[45,35245,35246,35248,35250],{"class":47,"line":48},[45,35247,52],{"class":51},[45,35249,56],{"class":55},[45,35251,60],{"class":59},[45,35253,35254],{"class":47,"line":63},[45,35255,67],{"emptyLinePlaceholder":66},[45,35257,35258,35260,35262,35264,35266],{"class":47,"line":70},[45,35259,20],{"class":73},[45,35261,76],{"class":51},[45,35263,1129],{"class":55},[45,35265,121],{"class":59},[45,35267,86],{"class":85},[45,35269,35270,35272,35274,35276,35278,35280],{"class":47,"line":89},[45,35271,93],{"class":92},[45,35273,96],{"class":85},[45,35275,100],{"class":99},[45,35277,1710],{"class":103},[45,35279,100],{"class":99},[45,35281,109],{"class":85},[712,35283,35285,35286,35288],{"id":35284},"comparing-to-true-when-a-direct-condition-is-enough","Comparing to ",[18,35287,229],{}," when a direct condition is enough",[14,35290,35291],{},"This works, but it is usually unnecessary:",[36,35293,35295],{"className":38,"code":35294,"language":40,"meta":41,"style":41},"is_logged_in = True\n\nif is_logged_in == True:\n    print(\"Welcome\")\n",[18,35296,35297,35305,35309,35322],{"__ignoreMap":41},[45,35298,35299,35301,35303],{"class":47,"line":48},[45,35300,33655],{"class":51},[45,35302,56],{"class":55},[45,35304,482],{"class":481},[45,35306,35307],{"class":47,"line":63},[45,35308,67],{"emptyLinePlaceholder":66},[45,35310,35311,35313,35316,35318,35320],{"class":47,"line":70},[45,35312,20],{"class":73},[45,35314,35315],{"class":51}," is_logged_in ",[45,35317,1129],{"class":55},[45,35319,23323],{"class":481},[45,35321,86],{"class":85},[45,35323,35324,35326,35328,35330,35332,35334],{"class":47,"line":89},[45,35325,93],{"class":92},[45,35327,96],{"class":85},[45,35329,100],{"class":99},[45,35331,6541],{"class":103},[45,35333,100],{"class":99},[45,35335,109],{"class":85},[14,35337,35338],{},"A cleaner version is:",[36,35340,35342],{"className":38,"code":35341,"language":40,"meta":41,"style":41},"is_logged_in = True\n\nif is_logged_in:\n    print(\"Welcome\")\n",[18,35343,35344,35352,35356,35364],{"__ignoreMap":41},[45,35345,35346,35348,35350],{"class":47,"line":48},[45,35347,33655],{"class":51},[45,35349,56],{"class":55},[45,35351,482],{"class":481},[45,35353,35354],{"class":47,"line":63},[45,35355,67],{"emptyLinePlaceholder":66},[45,35357,35358,35360,35362],{"class":47,"line":70},[45,35359,20],{"class":73},[45,35361,34606],{"class":51},[45,35363,86],{"class":85},[45,35365,35366,35368,35370,35372,35374,35376],{"class":47,"line":89},[45,35367,93],{"class":92},[45,35369,96],{"class":85},[45,35371,100],{"class":99},[45,35373,6541],{"class":103},[45,35375,100],{"class":99},[45,35377,109],{"class":85},[712,35379,35381,35382,35385,35386],{"id":35380},"confusing-the-string-true-with-the-boolean-true","Confusing the string ",[18,35383,35384],{},"'True'"," with the Boolean ",[18,35387,229],{},[14,35389,35390],{},"These are not the same thing.",[36,35392,35394],{"className":38,"code":35393,"language":40,"meta":41,"style":41},"print('True')\nprint(True)\nprint(type('True'))\nprint(type(True))\n",[18,35395,35396,35410,35420,35438],{"__ignoreMap":41},[45,35397,35398,35400,35402,35404,35406,35408],{"class":47,"line":48},[45,35399,2065],{"class":92},[45,35401,96],{"class":85},[45,35403,7901],{"class":99},[45,35405,229],{"class":103},[45,35407,7901],{"class":99},[45,35409,109],{"class":85},[45,35411,35412,35414,35416,35418],{"class":47,"line":63},[45,35413,2065],{"class":92},[45,35415,96],{"class":85},[45,35417,229],{"class":481},[45,35419,109],{"class":85},[45,35421,35422,35424,35426,35428,35430,35432,35434,35436],{"class":47,"line":70},[45,35423,2065],{"class":92},[45,35425,96],{"class":85},[45,35427,4340],{"class":2109},[45,35429,96],{"class":85},[45,35431,7901],{"class":99},[45,35433,229],{"class":103},[45,35435,7901],{"class":99},[45,35437,4348],{"class":85},[45,35439,35440,35442,35444,35446,35448,35450],{"class":47,"line":89},[45,35441,2065],{"class":92},[45,35443,96],{"class":85},[45,35445,4340],{"class":2109},[45,35447,96],{"class":85},[45,35449,229],{"class":481},[45,35451,4348],{"class":85},[14,35453,177],{},[36,35455,35457],{"className":38,"code":35456,"language":40,"meta":41,"style":41},"True\nTrue\n\u003Cclass 'str'>\n\u003Cclass 'bool'>\n",[18,35458,35459,35463,35467,35481],{"__ignoreMap":41},[45,35460,35461],{"class":47,"line":48},[45,35462,2270],{"class":481},[45,35464,35465],{"class":47,"line":63},[45,35466,2270],{"class":481},[45,35468,35469,35471,35473,35475,35477,35479],{"class":47,"line":70},[45,35470,79],{"class":55},[45,35472,2441],{"class":2440},[45,35474,9199],{"class":99},[45,35476,29733],{"class":103},[45,35478,7901],{"class":99},[45,35480,13484],{"class":55},[45,35482,35483,35485,35487,35489,35491,35493],{"class":47,"line":89},[45,35484,79],{"class":55},[45,35486,2441],{"class":2440},[45,35488,9199],{"class":99},[45,35490,2110],{"class":103},[45,35492,7901],{"class":99},[45,35494,13484],{"class":55},[14,35496,35497],{},"The first value is a string. The second is a Boolean.",[31,35499,2281],{"id":2280},[712,35501,35503],{"id":35502},"what-is-a-boolean-in-python","What is a Boolean in Python?",[14,35505,35506,35507,230,35509,240],{},"A Boolean is a value that is either ",[18,35508,229],{},[18,35510,233],{},[712,35512,35514],{"id":35513},"why-are-booleans-useful-in-python","Why are Booleans useful in Python?",[14,35516,35517,35518,35520],{},"They help programs make decisions using conditions in ",[18,35519,20],{}," statements and loops.",[712,35522,35524],{"id":35523},"is-true-the-same-as-true-in-python","Is true the same as True in Python?",[14,35526,35527,35528,1316,35530,35532],{},"No. Python is case-sensitive. You must write ",[18,35529,229],{},[18,35531,233],{}," with capital first letters.",[712,35534,19018,35536,35538],{"id":35535},"what-does-bool-do-in-python",[18,35537,34800],{}," do in Python?",[14,35540,35541],{},"It converts a value into a Boolean based on whether Python treats that value as true or false.",[712,35543,35545],{"id":35544},"can-a-comparison-return-a-boolean","Can a comparison return a Boolean?",[14,35547,35548,35549,230,35552,35555,35556,230,35558,240],{},"Yes. Expressions like ",[18,35550,35551],{},"4 \u003C 10",[18,35553,35554],{},"name == 'Sam'"," return ",[18,35557,229],{},[18,35559,233],{},[31,35561,2342],{"id":2341},[195,35563,35564,35568,35573,35578,35583],{},[198,35565,35566],{},[236,35567,549],{"href":548},[198,35569,35570],{},[236,35571,35572],{"href":34426},"Python if, else, and elif explained",[198,35574,35575],{},[236,35576,35577],{"href":34797},"Python bool() function explained",[198,35579,35580],{},[236,35581,35582],{"href":35108},"Python data types overview",[198,35584,35585],{},[236,35586,35173],{"href":35172},[2367,35588,35589],{},"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 .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 .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 .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 .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 .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 .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":41,"searchDepth":63,"depth":63,"links":35591},[35592,35593,35599,35607,35608,35613,35614,35621,35631,35639],{"id":33502,"depth":63,"text":33503},{"id":33629,"depth":63,"text":33630,"children":35594},[35595,35597,35598],{"id":33636,"depth":70,"text":35596},"Assign True or False directly",{"id":33716,"depth":70,"text":33717},{"id":33791,"depth":70,"text":33792},{"id":33882,"depth":63,"text":33883,"children":35600},[35601,35603,35605,35606],{"id":33889,"depth":70,"text":35602},"In if statements",{"id":33965,"depth":70,"text":35604},"In while loops",{"id":34048,"depth":70,"text":34049},{"id":34145,"depth":70,"text":34146},{"id":34190,"depth":63,"text":34191},{"id":34430,"depth":63,"text":34431,"children":35609},[35610,35611,35612],{"id":34437,"depth":70,"text":34437},{"id":34506,"depth":70,"text":34506},{"id":2857,"depth":70,"text":2857},{"id":34624,"depth":63,"text":34625},{"id":34804,"depth":63,"text":34805,"children":35615},[35616,35617,35618,35620],{"id":34811,"depth":70,"text":34812},{"id":34866,"depth":70,"text":34867},{"id":34928,"depth":70,"text":35619},"Return True or False from a function",{"id":35017,"depth":70,"text":35018},{"id":1336,"depth":63,"text":1337,"children":35622},[35623,35625,35627,35629],{"id":35117,"depth":70,"text":35624},"Writing true or false instead of True or False",{"id":35176,"depth":70,"text":35626},"Using = instead of == in a comparison",{"id":35284,"depth":70,"text":35628},"Comparing to True when a direct condition is enough",{"id":35380,"depth":70,"text":35630},"Confusing the string 'True' with the Boolean True",{"id":2280,"depth":63,"text":2281,"children":35632},[35633,35634,35635,35636,35638],{"id":35502,"depth":70,"text":35503},{"id":35513,"depth":70,"text":35514},{"id":35523,"depth":70,"text":35524},{"id":35535,"depth":70,"text":35637},"What does bool() do in Python?",{"id":35544,"depth":70,"text":35545},{"id":2341,"depth":63,"text":2342},"Master python booleans explained true and false in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-booleans-explained-true-and-false",{"title":33477,"description":35640},"learn\u002Fpython-booleans-explained-true-and-false","cirgKCwTKBA6r1J0rjfKa1G1414Xz2gEwgo67qBO6Ok",{"id":35647,"title":35648,"body":35649,"description":37396,"extension":2399,"meta":37397,"navigation":66,"path":37398,"seo":37399,"stem":37400,"__hash__":37401},"content\u002Flearn\u002Fpython-break-and-continue-statements.md","Python break and continue Statements",{"type":7,"value":35650,"toc":37359},[35651,35654,35663,35675,35684,35686,35778,35786,35788,35791,35813,35822,35826,35831,35833,35847,35852,35856,35859,35979,35981,36019,36023,36026,36048,36058,36062,36067,36069,36085,36090,36094,36097,36148,36150,36171,36175,36189,36196,36200,36202,36214,36218,36229,36233,36244,36247,36380,36383,36428,36432,36438,36444,36452,36515,36518,36534,36541,36604,36607,36627,36640,36647,36649,36656,36660,36666,36717,36720,36733,36741,36745,36750,36801,36803,36823,36830,36834,36837,36899,36908,36913,36917,36920,36983,36989,36992,37021,37027,37089,37093,37097,37108,37112,37123,37129,37134,37181,37183,37186,37211,37214,37268,37276,37278,37282,37290,37294,37304,37308,37311,37315,37321,37325,37328,37330,37356],[10,35652,35648],{"id":35653},"python-break-and-continue-statements",[14,35655,35656,1316,35659,35662],{},[18,35657,35658],{},"break",[18,35660,35661],{},"continue"," are used inside loops to control what happens next.",[195,35664,35665,35670],{},[198,35666,35667,35669],{},[18,35668,35658],{}," stops the loop completely",[198,35671,35672,35674],{},[18,35673,35661],{}," skips the current loop step and moves to the next one",[14,35676,35677,35678,35680,35681,35683],{},"These statements work in both ",[18,35679,7895],{}," loops and ",[18,35682,23529],{}," loops. They are useful when you want more control over loop behavior without rewriting the whole loop.",[31,35685,34],{"id":33},[36,35687,35689],{"className":38,"code":35688,"language":40,"meta":41,"style":41},"for number in range(1, 6):\n    if number == 3:\n        continue\n    if number == 5:\n        break\n    print(number)\n\n# Output:\n# 1\n# 2\n# 4\n",[18,35690,35691,35711,35723,35728,35740,35744,35754,35758,35763,35768,35773],{"__ignoreMap":41},[45,35692,35693,35695,35697,35699,35701,35703,35705,35707,35709],{"class":47,"line":48},[45,35694,7895],{"class":73},[45,35696,744],{"class":51},[45,35698,8623],{"class":73},[45,35700,10451],{"class":92},[45,35702,96],{"class":85},[45,35704,8578],{"class":59},[45,35706,3079],{"class":85},[45,35708,9288],{"class":59},[45,35710,2464],{"class":85},[45,35712,35713,35715,35717,35719,35721],{"class":47,"line":63},[45,35714,1913],{"class":73},[45,35716,744],{"class":51},[45,35718,1129],{"class":55},[45,35720,777],{"class":59},[45,35722,86],{"class":85},[45,35724,35725],{"class":47,"line":70},[45,35726,35727],{"class":73},"        continue\n",[45,35729,35730,35732,35734,35736,35738],{"class":47,"line":89},[45,35731,1913],{"class":73},[45,35733,744],{"class":51},[45,35735,1129],{"class":55},[45,35737,1222],{"class":59},[45,35739,86],{"class":85},[45,35741,35742],{"class":47,"line":112},[45,35743,23579],{"class":73},[45,35745,35746,35748,35750,35752],{"class":47,"line":126},[45,35747,93],{"class":92},[45,35749,96],{"class":85},[45,35751,13546],{"class":2070},[45,35753,109],{"class":85},[45,35755,35756],{"class":47,"line":142},[45,35757,67],{"emptyLinePlaceholder":66},[45,35759,35760],{"class":47,"line":149},[45,35761,35762],{"class":7624},"# Output:\n",[45,35764,35765],{"class":47,"line":679},[45,35766,35767],{"class":7624},"# 1\n",[45,35769,35770],{"class":47,"line":686},[45,35771,35772],{"class":7624},"# 2\n",[45,35774,35775],{"class":47,"line":2229},[45,35776,35777],{"class":7624},"# 4\n",[14,35779,165,35780,35782,35783,35785],{},[18,35781,35661],{}," to skip the current loop step. Use ",[18,35784,35658],{}," to stop the loop completely.",[31,35787,6600],{"id":6599},[14,35789,35790],{},"By the end of this page, you will understand that:",[195,35792,35793,35798,35803,35810],{},[198,35794,35795,35797],{},[18,35796,35658],{}," stops a loop early",[198,35799,35800,35802],{},[18,35801,35661],{}," skips to the next loop iteration",[198,35804,35805,35806,35680,35808,26095],{},"Both can be used inside ",[18,35807,7895],{},[18,35809,23529],{},[198,35811,35812],{},"They help control loop flow without changing the whole loop",[14,35814,35815,35816,1316,35818,240],{},"If you are still getting comfortable with loops, see ",[236,35817,23611],{"href":23610},[236,35819,35821],{"href":35820},"\u002Flearn\u002Fpython-while-loops-explained\u002F","Python while loops explained",[31,35823,35825],{"id":35824},"what-break-does","What break does",[14,35827,35828,35830],{},[18,35829,35658],{}," exits the nearest loop immediately.",[14,35832,2727],{},[195,35834,35835,35838,35844],{},[198,35836,35837],{},"the loop stops right away",[198,35839,35840,35841,35843],{},"any code after ",[18,35842,35658],{}," inside that loop does not run",[198,35845,35846],{},"Python continues with the first line after the loop",[14,35848,165,35849,35851],{},[18,35850,35658],{}," when the job is already done. For example, if you are searching for the first match, there is no reason to keep looping after you find it.",[31,35853,35855],{"id":35854},"simple-break-example","Simple break example",[14,35857,35858],{},"This example stops when the target value is found.",[36,35860,35862],{"className":38,"code":35861,"language":40,"meta":41,"style":41},"numbers = [4, 7, 9, 12, 15]\n\nfor number in numbers:\n    if number == 12:\n        print(\"Found 12\")\n        break\n    print(\"Checked\", number)\n\nprint(\"Loop ended\")\n",[18,35863,35864,35893,35897,35909,35921,35936,35940,35960,35964],{"__ignoreMap":41},[45,35865,35866,35868,35870,35872,35874,35876,35878,35880,35882,35884,35886,35888,35891],{"class":47,"line":48},[45,35867,8570],{"class":51},[45,35869,56],{"class":55},[45,35871,8575],{"class":85},[45,35873,3584],{"class":59},[45,35875,3079],{"class":85},[45,35877,28084],{"class":59},[45,35879,3079],{"class":85},[45,35881,9047],{"class":59},[45,35883,3079],{"class":85},[45,35885,8900],{"class":59},[45,35887,3079],{"class":85},[45,35889,35890],{"class":59}," 15",[45,35892,8594],{"class":85},[45,35894,35895],{"class":47,"line":63},[45,35896,67],{"emptyLinePlaceholder":66},[45,35898,35899,35901,35903,35905,35907],{"class":47,"line":70},[45,35900,7895],{"class":73},[45,35902,744],{"class":51},[45,35904,8623],{"class":73},[45,35906,8626],{"class":51},[45,35908,86],{"class":85},[45,35910,35911,35913,35915,35917,35919],{"class":47,"line":89},[45,35912,1913],{"class":73},[45,35914,744],{"class":51},[45,35916,1129],{"class":55},[45,35918,8900],{"class":59},[45,35920,86],{"class":85},[45,35922,35923,35925,35927,35929,35932,35934],{"class":47,"line":112},[45,35924,1923],{"class":92},[45,35926,96],{"class":85},[45,35928,100],{"class":99},[45,35930,35931],{"class":103},"Found 12",[45,35933,100],{"class":99},[45,35935,109],{"class":85},[45,35937,35938],{"class":47,"line":126},[45,35939,23579],{"class":73},[45,35941,35942,35944,35946,35948,35951,35953,35955,35958],{"class":47,"line":142},[45,35943,93],{"class":92},[45,35945,96],{"class":85},[45,35947,100],{"class":99},[45,35949,35950],{"class":103},"Checked",[45,35952,100],{"class":99},[45,35954,3079],{"class":85},[45,35956,35957],{"class":2070}," number",[45,35959,109],{"class":85},[45,35961,35962],{"class":47,"line":149},[45,35963,67],{"emptyLinePlaceholder":66},[45,35965,35966,35968,35970,35972,35975,35977],{"class":47,"line":679},[45,35967,2065],{"class":92},[45,35969,96],{"class":85},[45,35971,100],{"class":99},[45,35973,35974],{"class":103},"Loop ended",[45,35976,100],{"class":99},[45,35978,109],{"class":85},[712,35980,5452],{"id":5451},[36,35982,35984],{"className":38,"code":35983,"language":40,"meta":41,"style":41},"Checked 4\nChecked 7\nChecked 9\nFound 12\nLoop ended\n",[18,35985,35986,35993,36000,36007,36014],{"__ignoreMap":41},[45,35987,35988,35991],{"class":47,"line":48},[45,35989,35990],{"class":51},"Checked ",[45,35992,13681],{"class":59},[45,35994,35995,35997],{"class":47,"line":63},[45,35996,35990],{"class":51},[45,35998,35999],{"class":59},"7\n",[45,36001,36002,36004],{"class":47,"line":70},[45,36003,35990],{"class":51},[45,36005,36006],{"class":59},"9\n",[45,36008,36009,36012],{"class":47,"line":89},[45,36010,36011],{"class":51},"Found ",[45,36013,3629],{"class":59},[45,36015,36016],{"class":47,"line":112},[45,36017,36018],{"class":51},"Loop ended\n",[712,36020,36022],{"id":36021},"why-it-stops","Why it stops",[14,36024,36025],{},"The important line is:",[36,36027,36029],{"className":38,"code":36028,"language":40,"meta":41,"style":41},"if number == 12:\n    break\n",[18,36030,36031,36043],{"__ignoreMap":41},[45,36032,36033,36035,36037,36039,36041],{"class":47,"line":48},[45,36034,20],{"class":73},[45,36036,744],{"class":51},[45,36038,1129],{"class":55},[45,36040,8900],{"class":59},[45,36042,86],{"class":85},[45,36044,36045],{"class":47,"line":63},[45,36046,36047],{"class":73},"    break\n",[14,36049,36050,36051,15579,36053,36055,36056,240],{},"As soon as ",[18,36052,13546],{},[18,36054,12026],{},", the loop ends. It does not continue to ",[18,36057,33860],{},[31,36059,36061],{"id":36060},"what-continue-does","What continue does",[14,36063,36064,36066],{},[18,36065,35661],{}," skips the rest of the current iteration.",[14,36068,2727],{},[195,36070,36071,36077,36080],{},[198,36072,36073,36074,36076],{},"the loop does ",[2421,36075,2857],{}," stop",[198,36078,36079],{},"Python jumps straight to the next iteration",[198,36081,35840,36082,36084],{},[18,36083,35661],{}," in that iteration does not run",[14,36086,165,36087,36089],{},[18,36088,35661],{}," when you want to ignore some values but keep looping through the rest.",[31,36091,36093],{"id":36092},"simple-continue-example","Simple continue example",[14,36095,36096],{},"This loop skips one value but keeps going.",[36,36098,36100],{"className":38,"code":36099,"language":40,"meta":41,"style":41},"for number in range(1, 6):\n    if number == 3:\n        continue\n    print(number)\n",[18,36101,36102,36122,36134,36138],{"__ignoreMap":41},[45,36103,36104,36106,36108,36110,36112,36114,36116,36118,36120],{"class":47,"line":48},[45,36105,7895],{"class":73},[45,36107,744],{"class":51},[45,36109,8623],{"class":73},[45,36111,10451],{"class":92},[45,36113,96],{"class":85},[45,36115,8578],{"class":59},[45,36117,3079],{"class":85},[45,36119,9288],{"class":59},[45,36121,2464],{"class":85},[45,36123,36124,36126,36128,36130,36132],{"class":47,"line":63},[45,36125,1913],{"class":73},[45,36127,744],{"class":51},[45,36129,1129],{"class":55},[45,36131,777],{"class":59},[45,36133,86],{"class":85},[45,36135,36136],{"class":47,"line":70},[45,36137,35727],{"class":73},[45,36139,36140,36142,36144,36146],{"class":47,"line":89},[45,36141,93],{"class":92},[45,36143,96],{"class":85},[45,36145,13546],{"class":2070},[45,36147,109],{"class":85},[712,36149,5452],{"id":5744},[36,36151,36153],{"className":38,"code":36152,"language":40,"meta":41,"style":41},"1\n2\n4\n5\n",[18,36154,36155,36159,36163,36167],{"__ignoreMap":41},[45,36156,36157],{"class":47,"line":48},[45,36158,13036],{"class":59},[45,36160,36161],{"class":47,"line":63},[45,36162,13041],{"class":59},[45,36164,36165],{"class":47,"line":70},[45,36166,13681],{"class":59},[45,36168,36169],{"class":47,"line":89},[45,36170,11570],{"class":59},[712,36172,36174],{"id":36173},"why-3-is-missing","Why 3 is missing",[14,36176,36177,36178,36181,36182,36184,36185,36188],{},"When ",[18,36179,36180],{},"number == 3",", Python runs ",[18,36183,35661],{},". That skips the ",[18,36186,36187],{},"print(number)"," line for that iteration.",[14,36190,36191,36192,1316,36194,240],{},"The loop then moves on to ",[18,36193,3584],{},[18,36195,3749],{},[31,36197,36199],{"id":36198},"break-vs-continue","break vs continue",[14,36201,19956],{},[195,36203,36204,36209],{},[198,36205,36206,36208],{},[18,36207,35658],{}," = stop the loop",[198,36210,36211,36213],{},[18,36212,35661],{}," = skip one pass of the loop",[14,36215,165,36216,8077],{},[18,36217,35658],{},[195,36219,36220,36223,36226],{},[198,36221,36222],{},"the task is finished",[198,36224,36225],{},"you found what you were looking for",[198,36227,36228],{},"there is no need to keep checking more values",[14,36230,165,36231,8077],{},[18,36232,35661],{},[195,36234,36235,36238,36241],{},[198,36236,36237],{},"you want to ignore certain values",[198,36239,36240],{},"you want to filter items during a loop",[198,36242,36243],{},"the loop should keep going after skipping one case",[14,36245,36246],{},"Here is a side-by-side example:",[36,36248,36250],{"className":38,"code":36249,"language":40,"meta":41,"style":41},"print(\"Using break:\")\nfor i in range(1, 6):\n    if i == 3:\n        break\n    print(i)\n\nprint(\"Using continue:\")\nfor i in range(1, 6):\n    if i == 3:\n        continue\n    print(i)\n",[18,36251,36252,36267,36288,36300,36304,36315,36319,36334,36354,36366,36370],{"__ignoreMap":41},[45,36253,36254,36256,36258,36260,36263,36265],{"class":47,"line":48},[45,36255,2065],{"class":92},[45,36257,96],{"class":85},[45,36259,100],{"class":99},[45,36261,36262],{"class":103},"Using break:",[45,36264,100],{"class":99},[45,36266,109],{"class":85},[45,36268,36269,36271,36274,36276,36278,36280,36282,36284,36286],{"class":47,"line":63},[45,36270,7895],{"class":73},[45,36272,36273],{"class":51}," i ",[45,36275,8623],{"class":73},[45,36277,10451],{"class":92},[45,36279,96],{"class":85},[45,36281,8578],{"class":59},[45,36283,3079],{"class":85},[45,36285,9288],{"class":59},[45,36287,2464],{"class":85},[45,36289,36290,36292,36294,36296,36298],{"class":47,"line":70},[45,36291,1913],{"class":73},[45,36293,36273],{"class":51},[45,36295,1129],{"class":55},[45,36297,777],{"class":59},[45,36299,86],{"class":85},[45,36301,36302],{"class":47,"line":89},[45,36303,23579],{"class":73},[45,36305,36306,36308,36310,36313],{"class":47,"line":112},[45,36307,93],{"class":92},[45,36309,96],{"class":85},[45,36311,36312],{"class":2070},"i",[45,36314,109],{"class":85},[45,36316,36317],{"class":47,"line":126},[45,36318,67],{"emptyLinePlaceholder":66},[45,36320,36321,36323,36325,36327,36330,36332],{"class":47,"line":142},[45,36322,2065],{"class":92},[45,36324,96],{"class":85},[45,36326,100],{"class":99},[45,36328,36329],{"class":103},"Using continue:",[45,36331,100],{"class":99},[45,36333,109],{"class":85},[45,36335,36336,36338,36340,36342,36344,36346,36348,36350,36352],{"class":47,"line":149},[45,36337,7895],{"class":73},[45,36339,36273],{"class":51},[45,36341,8623],{"class":73},[45,36343,10451],{"class":92},[45,36345,96],{"class":85},[45,36347,8578],{"class":59},[45,36349,3079],{"class":85},[45,36351,9288],{"class":59},[45,36353,2464],{"class":85},[45,36355,36356,36358,36360,36362,36364],{"class":47,"line":679},[45,36357,1913],{"class":73},[45,36359,36273],{"class":51},[45,36361,1129],{"class":55},[45,36363,777],{"class":59},[45,36365,86],{"class":85},[45,36367,36368],{"class":47,"line":686},[45,36369,35727],{"class":73},[45,36371,36372,36374,36376,36378],{"class":47,"line":2229},[45,36373,93],{"class":92},[45,36375,96],{"class":85},[45,36377,36312],{"class":2070},[45,36379,109],{"class":85},[712,36381,5452],{"id":36382},"output-2",[36,36384,36386],{"className":38,"code":36385,"language":40,"meta":41,"style":41},"Using break:\n1\n2\nUsing continue:\n1\n2\n4\n5\n",[18,36387,36388,36396,36400,36404,36412,36416,36420,36424],{"__ignoreMap":41},[45,36389,36390,36392,36394],{"class":47,"line":48},[45,36391,1350],{"class":51},[45,36393,35658],{"class":73},[45,36395,86],{"class":85},[45,36397,36398],{"class":47,"line":63},[45,36399,13036],{"class":59},[45,36401,36402],{"class":47,"line":70},[45,36403,13041],{"class":59},[45,36405,36406,36408,36410],{"class":47,"line":89},[45,36407,1350],{"class":51},[45,36409,35661],{"class":73},[45,36411,86],{"class":85},[45,36413,36414],{"class":47,"line":112},[45,36415,13036],{"class":59},[45,36417,36418],{"class":47,"line":126},[45,36419,13041],{"class":59},[45,36421,36422],{"class":47,"line":142},[45,36423,13681],{"class":59},[45,36425,36426],{"class":47,"line":149},[45,36427,11570],{"class":59},[31,36429,36431],{"id":36430},"using-break-and-continue-in-while-loops","Using break and continue in while loops",[14,36433,36434,36435,36437],{},"Both statements also work in ",[18,36436,23529],{}," loops.",[14,36439,36440,36441,36443],{},"The main thing to watch is the loop variable. In a ",[18,36442,23529],{}," loop, you usually update the variable yourself. If that update does not happen, the loop may never end.",[14,36445,36446,36447,36449,36450,457],{},"Here is a safe ",[18,36448,23529],{}," loop with ",[18,36451,35658],{},[36,36453,36455],{"className":38,"code":36454,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 5:\n    if count == 4:\n        break\n    print(count)\n    count += 1\n",[18,36456,36457,36465,36469,36481,36493,36497,36507],{"__ignoreMap":41},[45,36458,36459,36461,36463],{"class":47,"line":48},[45,36460,33984],{"class":51},[45,36462,56],{"class":55},[45,36464,12962],{"class":59},[45,36466,36467],{"class":47,"line":63},[45,36468,67],{"emptyLinePlaceholder":66},[45,36470,36471,36473,36475,36477,36479],{"class":47,"line":70},[45,36472,23529],{"class":73},[45,36474,33999],{"class":51},[45,36476,1156],{"class":55},[45,36478,1222],{"class":59},[45,36480,86],{"class":85},[45,36482,36483,36485,36487,36489,36491],{"class":47,"line":89},[45,36484,1913],{"class":73},[45,36486,33999],{"class":51},[45,36488,1129],{"class":55},[45,36490,8591],{"class":59},[45,36492,86],{"class":85},[45,36494,36495],{"class":47,"line":112},[45,36496,23579],{"class":73},[45,36498,36499,36501,36503,36505],{"class":47,"line":126},[45,36500,93],{"class":92},[45,36502,96],{"class":85},[45,36504,34014],{"class":2070},[45,36506,109],{"class":85},[45,36508,36509,36511,36513],{"class":47,"line":142},[45,36510,34021],{"class":51},[45,36512,12992],{"class":55},[45,36514,12962],{"class":59},[712,36516,5452],{"id":36517},"output-3",[36,36519,36520],{"className":38,"code":13029,"language":40,"meta":41,"style":41},[18,36521,36522,36526,36530],{"__ignoreMap":41},[45,36523,36524],{"class":47,"line":48},[45,36525,13036],{"class":59},[45,36527,36528],{"class":47,"line":63},[45,36529,13041],{"class":59},[45,36531,36532],{"class":47,"line":70},[45,36533,13046],{"class":59},[14,36535,36536,36537,36449,36539,457],{},"And here is a ",[18,36538,23529],{},[18,36540,35661],{},[36,36542,36544],{"className":38,"code":36543,"language":40,"meta":41,"style":41},"count = 0\n\nwhile count \u003C 5:\n    count += 1\n    if count == 3:\n        continue\n    print(count)\n",[18,36545,36546,36554,36558,36570,36578,36590,36594],{"__ignoreMap":41},[45,36547,36548,36550,36552],{"class":47,"line":48},[45,36549,33984],{"class":51},[45,36551,56],{"class":55},[45,36553,34967],{"class":59},[45,36555,36556],{"class":47,"line":63},[45,36557,67],{"emptyLinePlaceholder":66},[45,36559,36560,36562,36564,36566,36568],{"class":47,"line":70},[45,36561,23529],{"class":73},[45,36563,33999],{"class":51},[45,36565,79],{"class":55},[45,36567,1222],{"class":59},[45,36569,86],{"class":85},[45,36571,36572,36574,36576],{"class":47,"line":89},[45,36573,34021],{"class":51},[45,36575,12992],{"class":55},[45,36577,12962],{"class":59},[45,36579,36580,36582,36584,36586,36588],{"class":47,"line":112},[45,36581,1913],{"class":73},[45,36583,33999],{"class":51},[45,36585,1129],{"class":55},[45,36587,777],{"class":59},[45,36589,86],{"class":85},[45,36591,36592],{"class":47,"line":126},[45,36593,35727],{"class":73},[45,36595,36596,36598,36600,36602],{"class":47,"line":142},[45,36597,93],{"class":92},[45,36599,96],{"class":85},[45,36601,34014],{"class":2070},[45,36603,109],{"class":85},[712,36605,5452],{"id":36606},"output-4",[36,36608,36609],{"className":38,"code":36152,"language":40,"meta":41,"style":41},[18,36610,36611,36615,36619,36623],{"__ignoreMap":41},[45,36612,36613],{"class":47,"line":48},[45,36614,13036],{"class":59},[45,36616,36617],{"class":47,"line":63},[45,36618,13041],{"class":59},[45,36620,36621],{"class":47,"line":70},[45,36622,13681],{"class":59},[45,36624,36625],{"class":47,"line":89},[45,36626,11570],{"class":59},[14,36628,36629,36630,36633,36634,11051,36637,36639],{},"In this example, ",[18,36631,36632],{},"count += 1"," happens ",[2421,36635,36636],{},"before",[18,36638,35661],{},", so the loop still makes progress.",[14,36641,36642,36643,1316,36645,240],{},"If you want to review loop conditions first, see ",[236,36644,549],{"href":548},[236,36646,35821],{"href":35820},[31,36648,1337],{"id":1336},[14,36650,36651,36652,1316,36654,240],{},"These are some common problems with ",[18,36653,35658],{},[18,36655,35661],{},[712,36657,36659],{"id":36658},"using-break-when-continue-was-needed","Using break when continue was needed",[14,36661,36662,36663,36665],{},"If you use ",[18,36664,35658],{},", the whole loop stops.",[36,36667,36669],{"className":38,"code":36668,"language":40,"meta":41,"style":41},"for i in range(1, 6):\n    if i == 3:\n        break\n    print(i)\n",[18,36670,36671,36691,36703,36707],{"__ignoreMap":41},[45,36672,36673,36675,36677,36679,36681,36683,36685,36687,36689],{"class":47,"line":48},[45,36674,7895],{"class":73},[45,36676,36273],{"class":51},[45,36678,8623],{"class":73},[45,36680,10451],{"class":92},[45,36682,96],{"class":85},[45,36684,8578],{"class":59},[45,36686,3079],{"class":85},[45,36688,9288],{"class":59},[45,36690,2464],{"class":85},[45,36692,36693,36695,36697,36699,36701],{"class":47,"line":63},[45,36694,1913],{"class":73},[45,36696,36273],{"class":51},[45,36698,1129],{"class":55},[45,36700,777],{"class":59},[45,36702,86],{"class":85},[45,36704,36705],{"class":47,"line":70},[45,36706,23579],{"class":73},[45,36708,36709,36711,36713,36715],{"class":47,"line":89},[45,36710,93],{"class":92},[45,36712,96],{"class":85},[45,36714,36312],{"class":2070},[45,36716,109],{"class":85},[14,36718,36719],{},"This prints:",[36,36721,36723],{"className":38,"code":36722,"language":40,"meta":41,"style":41},"1\n2\n",[18,36724,36725,36729],{"__ignoreMap":41},[45,36726,36727],{"class":47,"line":48},[45,36728,13036],{"class":59},[45,36730,36731],{"class":47,"line":63},[45,36732,13041],{"class":59},[14,36734,36735,36736,36738,36739,25114],{},"If you only wanted to skip ",[18,36737,11184],{},", use ",[18,36740,35661],{},[712,36742,36744],{"id":36743},"using-continue-when-break-was-needed","Using continue when break was needed",[14,36746,36662,36747,36749],{},[18,36748,35661],{},", the loop keeps going.",[36,36751,36753],{"className":38,"code":36752,"language":40,"meta":41,"style":41},"for i in range(1, 6):\n    if i == 3:\n        continue\n    print(i)\n",[18,36754,36755,36775,36787,36791],{"__ignoreMap":41},[45,36756,36757,36759,36761,36763,36765,36767,36769,36771,36773],{"class":47,"line":48},[45,36758,7895],{"class":73},[45,36760,36273],{"class":51},[45,36762,8623],{"class":73},[45,36764,10451],{"class":92},[45,36766,96],{"class":85},[45,36768,8578],{"class":59},[45,36770,3079],{"class":85},[45,36772,9288],{"class":59},[45,36774,2464],{"class":85},[45,36776,36777,36779,36781,36783,36785],{"class":47,"line":63},[45,36778,1913],{"class":73},[45,36780,36273],{"class":51},[45,36782,1129],{"class":55},[45,36784,777],{"class":59},[45,36786,86],{"class":85},[45,36788,36789],{"class":47,"line":70},[45,36790,35727],{"class":73},[45,36792,36793,36795,36797,36799],{"class":47,"line":89},[45,36794,93],{"class":92},[45,36796,96],{"class":85},[45,36798,36312],{"class":2070},[45,36800,109],{"class":85},[14,36802,36719],{},[36,36804,36805],{"className":38,"code":36152,"language":40,"meta":41,"style":41},[18,36806,36807,36811,36815,36819],{"__ignoreMap":41},[45,36808,36809],{"class":47,"line":48},[45,36810,13036],{"class":59},[45,36812,36813],{"class":47,"line":63},[45,36814,13041],{"class":59},[45,36816,36817],{"class":47,"line":70},[45,36818,13681],{"class":59},[45,36820,36821],{"class":47,"line":89},[45,36822,11570],{"class":59},[14,36824,36825,36826,36738,36828,240],{},"If you wanted the loop to stop at ",[18,36827,11184],{},[18,36829,35658],{},[712,36831,36833],{"id":36832},"placing-code-after-break-or-continue-and-expecting-it-to-run","Placing code after break or continue and expecting it to run",[14,36835,36836],{},"Code after these statements in the same iteration will not run.",[36,36838,36840],{"className":38,"code":36839,"language":40,"meta":41,"style":41},"for i in range(3):\n    if i == 1:\n        continue\n        print(\"This will never print\")\n    print(i)\n",[18,36841,36842,36858,36870,36874,36889],{"__ignoreMap":41},[45,36843,36844,36846,36848,36850,36852,36854,36856],{"class":47,"line":48},[45,36845,7895],{"class":73},[45,36847,36273],{"class":51},[45,36849,8623],{"class":73},[45,36851,10451],{"class":92},[45,36853,96],{"class":85},[45,36855,11184],{"class":59},[45,36857,2464],{"class":85},[45,36859,36860,36862,36864,36866,36868],{"class":47,"line":63},[45,36861,1913],{"class":73},[45,36863,36273],{"class":51},[45,36865,1129],{"class":55},[45,36867,9030],{"class":59},[45,36869,86],{"class":85},[45,36871,36872],{"class":47,"line":70},[45,36873,35727],{"class":73},[45,36875,36876,36878,36880,36882,36885,36887],{"class":47,"line":89},[45,36877,1923],{"class":92},[45,36879,96],{"class":85},[45,36881,100],{"class":99},[45,36883,36884],{"class":103},"This will never print",[45,36886,100],{"class":99},[45,36888,109],{"class":85},[45,36890,36891,36893,36895,36897],{"class":47,"line":112},[45,36892,93],{"class":92},[45,36894,96],{"class":85},[45,36896,36312],{"class":2070},[45,36898,109],{"class":85},[14,36900,1732,36901,36904,36905,36907],{},[18,36902,36903],{},"print(\"This will never print\")"," line is skipped because ",[18,36906,35661],{}," already jumped to the next iteration.",[14,36909,36910,36911,240],{},"The same idea applies to ",[18,36912,35658],{},[712,36914,36916],{"id":36915},"creating-an-infinite-while-loop-by-not-changing-the-condition-value","Creating an infinite while loop by not changing the condition value",[14,36918,36919],{},"This is a very common mistake.",[36,36921,36923],{"className":38,"code":36922,"language":40,"meta":41,"style":41},"count = 0\n\nwhile count \u003C 5:\n    if count == 2:\n        continue\n    print(count)\n    count += 1\n",[18,36924,36925,36933,36937,36949,36961,36965,36975],{"__ignoreMap":41},[45,36926,36927,36929,36931],{"class":47,"line":48},[45,36928,33984],{"class":51},[45,36930,56],{"class":55},[45,36932,34967],{"class":59},[45,36934,36935],{"class":47,"line":63},[45,36936,67],{"emptyLinePlaceholder":66},[45,36938,36939,36941,36943,36945,36947],{"class":47,"line":70},[45,36940,23529],{"class":73},[45,36942,33999],{"class":51},[45,36944,79],{"class":55},[45,36946,1222],{"class":59},[45,36948,86],{"class":85},[45,36950,36951,36953,36955,36957,36959],{"class":47,"line":89},[45,36952,1913],{"class":73},[45,36954,33999],{"class":51},[45,36956,1129],{"class":55},[45,36958,3754],{"class":59},[45,36960,86],{"class":85},[45,36962,36963],{"class":47,"line":112},[45,36964,35727],{"class":73},[45,36966,36967,36969,36971,36973],{"class":47,"line":126},[45,36968,93],{"class":92},[45,36970,96],{"class":85},[45,36972,34014],{"class":2070},[45,36974,109],{"class":85},[45,36976,36977,36979,36981],{"class":47,"line":142},[45,36978,34021],{"class":51},[45,36980,12992],{"class":55},[45,36982,12962],{"class":59},[14,36984,36985,36986,240],{},"This loop becomes infinite when ",[18,36987,36988],{},"count == 2",[14,36990,36991],{},"Why?",[195,36993,36994,37001,37006,37011,37018],{},[198,36995,36996,36998,36999],{},[18,36997,34014],{}," is ",[18,37000,9367],{},[198,37002,37003,37005],{},[18,37004,35661],{}," runs",[198,37007,37008,37010],{},[18,37009,36632],{}," is skipped",[198,37012,37013,37015,37016],{},[18,37014,34014],{}," stays ",[18,37017,9367],{},[198,37019,37020],{},"the loop condition stays true forever",[14,37022,37023,37024,37026],{},"A correct version updates the counter before ",[18,37025,35661],{}," can happen:",[36,37028,37029],{"className":38,"code":36543,"language":40,"meta":41,"style":41},[18,37030,37031,37039,37043,37055,37063,37075,37079],{"__ignoreMap":41},[45,37032,37033,37035,37037],{"class":47,"line":48},[45,37034,33984],{"class":51},[45,37036,56],{"class":55},[45,37038,34967],{"class":59},[45,37040,37041],{"class":47,"line":63},[45,37042,67],{"emptyLinePlaceholder":66},[45,37044,37045,37047,37049,37051,37053],{"class":47,"line":70},[45,37046,23529],{"class":73},[45,37048,33999],{"class":51},[45,37050,79],{"class":55},[45,37052,1222],{"class":59},[45,37054,86],{"class":85},[45,37056,37057,37059,37061],{"class":47,"line":89},[45,37058,34021],{"class":51},[45,37060,12992],{"class":55},[45,37062,12962],{"class":59},[45,37064,37065,37067,37069,37071,37073],{"class":47,"line":112},[45,37066,1913],{"class":73},[45,37068,33999],{"class":51},[45,37070,1129],{"class":55},[45,37072,777],{"class":59},[45,37074,86],{"class":85},[45,37076,37077],{"class":47,"line":126},[45,37078,35727],{"class":73},[45,37080,37081,37083,37085,37087],{"class":47,"line":142},[45,37082,93],{"class":92},[45,37084,96],{"class":85},[45,37086,34014],{"class":2070},[45,37088,109],{"class":85},[31,37090,37092],{"id":37091},"when-to-use-each-statement","When to use each statement",[14,37094,165,37095,8077],{},[18,37096,35658],{},[195,37098,37099,37102,37105],{},[198,37100,37101],{},"you are searching for the first match",[198,37103,37104],{},"the loop should end as soon as something happens",[198,37106,37107],{},"continuing would waste time",[14,37109,165,37110,8077],{},[18,37111,35661],{},[195,37113,37114,37117,37120],{},[198,37115,37116],{},"you want to skip unwanted values",[198,37118,37119],{},"you are filtering items during a loop",[198,37121,37122],{},"the loop should keep running after one special case",[14,37124,37125,37126,37128],{},"Do not use them if they make the code harder to read. In some cases, a simple ",[18,37127,20],{}," statement is clearer.",[14,37130,37131,37132,457],{},"For example, this can be easier to read than using ",[18,37133,35661],{},[36,37135,37137],{"className":38,"code":37136,"language":40,"meta":41,"style":41},"for number in range(1, 6):\n    if number != 3:\n        print(number)\n",[18,37138,37139,37159,37171],{"__ignoreMap":41},[45,37140,37141,37143,37145,37147,37149,37151,37153,37155,37157],{"class":47,"line":48},[45,37142,7895],{"class":73},[45,37144,744],{"class":51},[45,37146,8623],{"class":73},[45,37148,10451],{"class":92},[45,37150,96],{"class":85},[45,37152,8578],{"class":59},[45,37154,3079],{"class":85},[45,37156,9288],{"class":59},[45,37158,2464],{"class":85},[45,37160,37161,37163,37165,37167,37169],{"class":47,"line":63},[45,37162,1913],{"class":73},[45,37164,744],{"class":51},[45,37166,1135],{"class":55},[45,37168,777],{"class":59},[45,37170,86],{"class":85},[45,37172,37173,37175,37177,37179],{"class":47,"line":70},[45,37174,1923],{"class":92},[45,37176,96],{"class":85},[45,37178,13546],{"class":2070},[45,37180,109],{"class":85},[31,37182,2012],{"id":2011},[14,37184,37185],{},"Beginners often get stuck because of one of these:",[195,37187,37188,37191,37197,37203],{},[198,37189,37190],{},"confusing “stop the loop” with “skip this iteration”",[198,37192,37193,37194,37196],{},"not realizing that ",[18,37195,35658],{}," only exits the nearest loop",[198,37198,37199,37200,37202],{},"forgetting that ",[18,37201,35661],{}," skips the remaining code in the current iteration",[198,37204,14541,37205,37207,37208,37210],{},[18,37206,35661],{}," in a ",[18,37209,23529],{}," loop before updating the counter",[14,37212,37213],{},"If your loop is not behaving as expected, add simple debug prints:",[36,37215,37217],{"className":38,"code":37216,"language":40,"meta":41,"style":41},"print(i)\nprint('before continue', i)\nprint('before break', i)\n",[18,37218,37219,37229,37249],{"__ignoreMap":41},[45,37220,37221,37223,37225,37227],{"class":47,"line":48},[45,37222,2065],{"class":92},[45,37224,96],{"class":85},[45,37226,36312],{"class":2070},[45,37228,109],{"class":85},[45,37230,37231,37233,37235,37237,37240,37242,37244,37247],{"class":47,"line":63},[45,37232,2065],{"class":92},[45,37234,96],{"class":85},[45,37236,7901],{"class":99},[45,37238,37239],{"class":103},"before continue",[45,37241,7901],{"class":99},[45,37243,3079],{"class":85},[45,37245,37246],{"class":2070}," i",[45,37248,109],{"class":85},[45,37250,37251,37253,37255,37257,37260,37262,37264,37266],{"class":47,"line":70},[45,37252,2065],{"class":92},[45,37254,96],{"class":85},[45,37256,7901],{"class":99},[45,37258,37259],{"class":103},"before break",[45,37261,7901],{"class":99},[45,37263,3079],{"class":85},[45,37265,37246],{"class":2070},[45,37267,109],{"class":85},[14,37269,37270,37271,230,37273,37275],{},"These help you see which values the loop is reaching and whether ",[18,37272,35658],{},[18,37274,35661],{}," runs before later code.",[31,37277,2281],{"id":2280},[712,37279,37281],{"id":37280},"what-is-the-difference-between-break-and-continue-in-python","What is the difference between break and continue in Python?",[14,37283,37284,37286,37287,37289],{},[18,37285,35658],{}," ends the loop completely. ",[18,37288,35661],{}," skips the current iteration and moves to the next one.",[712,37291,37293],{"id":37292},"can-i-use-break-in-a-for-loop-and-a-while-loop","Can I use break in a for loop and a while loop?",[14,37295,37296,37297,37299,37300,35680,37302,36437],{},"Yes. ",[18,37298,35658],{}," works in both ",[18,37301,7895],{},[18,37303,23529],{},[712,37305,37307],{"id":37306},"does-continue-stop-the-loop","Does continue stop the loop?",[14,37309,37310],{},"No. It only skips the rest of the current iteration.",[712,37312,37314],{"id":37313},"why-did-my-while-loop-become-infinite-after-using-continue","Why did my while loop become infinite after using continue?",[14,37316,37317,37318,37320],{},"The loop variable may not have been updated before ",[18,37319,35661],{}," ran, so the condition never changed.",[712,37322,37324],{"id":37323},"does-break-exit-nested-loops","Does break exit nested loops?",[14,37326,37327],{},"No. It only exits the nearest loop that contains it.",[31,37329,2342],{"id":2341},[195,37331,37332,37336,37340,37344,37350],{},[198,37333,37334],{},[236,37335,23611],{"href":23610},[198,37337,37338],{},[236,37339,35821],{"href":35820},[198,37341,37342],{},[236,37343,549],{"href":548},[198,37345,37346],{},[236,37347,37349],{"href":37348},"\u002Flearn\u002Fpython-pass-statement-explained\u002F","Python pass statement explained",[198,37351,37352],{},[236,37353,37355],{"href":37354},"\u002Fhow-to\u002Fhow-to-loop-through-a-list-in-python\u002F","How to loop through a list in Python",[2367,37357,37358],{},"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 .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 .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}",{"title":41,"searchDepth":63,"depth":63,"links":37360},[37361,37362,37363,37364,37368,37369,37373,37376,37380,37386,37387,37388,37395],{"id":33,"depth":63,"text":34},{"id":6599,"depth":63,"text":6600},{"id":35824,"depth":63,"text":35825},{"id":35854,"depth":63,"text":35855,"children":37365},[37366,37367],{"id":5451,"depth":70,"text":5452},{"id":36021,"depth":70,"text":36022},{"id":36060,"depth":63,"text":36061},{"id":36092,"depth":63,"text":36093,"children":37370},[37371,37372],{"id":5744,"depth":70,"text":5452},{"id":36173,"depth":70,"text":36174},{"id":36198,"depth":63,"text":36199,"children":37374},[37375],{"id":36382,"depth":70,"text":5452},{"id":36430,"depth":63,"text":36431,"children":37377},[37378,37379],{"id":36517,"depth":70,"text":5452},{"id":36606,"depth":70,"text":5452},{"id":1336,"depth":63,"text":1337,"children":37381},[37382,37383,37384,37385],{"id":36658,"depth":70,"text":36659},{"id":36743,"depth":70,"text":36744},{"id":36832,"depth":70,"text":36833},{"id":36915,"depth":70,"text":36916},{"id":37091,"depth":63,"text":37092},{"id":2011,"depth":63,"text":2012},{"id":2280,"depth":63,"text":2281,"children":37389},[37390,37391,37392,37393,37394],{"id":37280,"depth":70,"text":37281},{"id":37292,"depth":70,"text":37293},{"id":37306,"depth":70,"text":37307},{"id":37313,"depth":70,"text":37314},{"id":37323,"depth":70,"text":37324},{"id":2341,"depth":63,"text":2342},"Master python break and continue statements in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-break-and-continue-statements",{"title":35648,"description":37396},"learn\u002Fpython-break-and-continue-statements","F17znJA2g6m_VSHb5fsJB1wP4sOizQCBQcyKDlFk-1A",{"id":37403,"title":37404,"body":37405,"description":39232,"extension":2399,"meta":39233,"navigation":66,"path":2559,"seo":39234,"stem":39235,"__hash__":39236},"content\u002Flearn\u002Fpython-classes-and-objects-explained.md","Python Classes and Objects Explained",{"type":7,"value":37406,"toc":39203},[37407,37410,37413,37421,37424,37426,37506,37510,37519,37521,37537,37540,37552,37554,37568,37572,37577,37580,37583,37595,37598,37615,37620,37626,37631,37635,37640,37645,37647,37722,37726,37758,37760,37777,37780,37785,37789,37792,37795,37809,37812,37819,37823,37829,37847,37850,37889,37892,37914,37919,37924,37928,37931,37962,37968,37973,38036,38040,38048,38051,38062,38068,38070,38073,38076,38079,38205,38209,38223,38225,38244,38247,38260,38265,38270,38273,38275,38351,38357,38374,38382,38389,38393,38400,38597,38601,38636,38639,38661,38664,38682,38688,38690,38693,38699,38704,38706,38741,38743,38781,38785,38788,38790,38829,38841,38843,38914,38918,38921,38924,38959,38966,38969,39045,39049,39052,39054,39093,39095,39133,39135,39139,39142,39146,39149,39155,39160,39166,39169,39171,39200],[10,37408,37404],{"id":37409},"python-classes-and-objects-explained",[14,37411,37412],{},"Classes and objects are a big part of Python, but the basic idea is simple.",[14,37414,2419,37415,37417,37418,37420],{},[2421,37416,2441],{}," is a blueprint. An ",[2421,37419,13471],{}," is one real thing made from that blueprint.",[14,37422,37423],{},"If that sounds abstract, do not worry. This page shows what classes and objects mean, how they work together, and how to use them in simple Python code.",[31,37425,34],{"id":33},[36,37427,37429],{"className":38,"code":37428,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\nbuddy = Dog(\"Buddy\")\nprint(buddy.name)\n",[18,37430,37431,37439,37455,37467,37471,37491],{"__ignoreMap":41},[45,37432,37433,37435,37437],{"class":47,"line":48},[45,37434,2441],{"class":2440},[45,37436,2445],{"class":2444},[45,37438,86],{"class":85},[45,37440,37441,37443,37445,37447,37449,37451,37453],{"class":47,"line":63},[45,37442,2452],{"class":2440},[45,37444,3072],{"class":92},[45,37446,96],{"class":85},[45,37448,2427],{"class":2461},[45,37450,3079],{"class":85},[45,37452,3083],{"class":3082},[45,37454,2464],{"class":85},[45,37456,37457,37459,37461,37463,37465],{"class":47,"line":70},[45,37458,3090],{"class":2771},[45,37460,240],{"class":85},[45,37462,3096],{"class":3095},[45,37464,3099],{"class":55},[45,37466,3102],{"class":51},[45,37468,37469],{"class":47,"line":89},[45,37470,67],{"emptyLinePlaceholder":66},[45,37472,37473,37476,37478,37480,37482,37484,37487,37489],{"class":47,"line":112},[45,37474,37475],{"class":51},"buddy ",[45,37477,56],{"class":55},[45,37479,2445],{"class":2070},[45,37481,96],{"class":85},[45,37483,100],{"class":99},[45,37485,37486],{"class":103},"Buddy",[45,37488,100],{"class":99},[45,37490,109],{"class":85},[45,37492,37493,37495,37497,37500,37502,37504],{"class":47,"line":126},[45,37494,2065],{"class":92},[45,37496,96],{"class":85},[45,37498,37499],{"class":2070},"buddy",[45,37501,240],{"class":85},[45,37503,3096],{"class":3095},[45,37505,109],{"class":85},[14,37507,37508],{},[2421,37509,177],{},[36,37511,37513],{"className":38,"code":37512,"language":40,"meta":41,"style":41},"Buddy\n",[18,37514,37515],{"__ignoreMap":41},[45,37516,37517],{"class":47,"line":48},[45,37518,37512],{"class":51},[14,37520,6570],{},[195,37522,37523,37527,37532],{},[198,37524,37525,32371],{},[18,37526,2570],{},[198,37528,37529,37531],{},[18,37530,37499],{}," is an object created from that class",[198,37533,37534,37536],{},[18,37535,3096],{}," is data stored in the object",[14,37538,37539],{},"Use this as the basic mental model:",[195,37541,37542,37547],{},[198,37543,37544,37546],{},[2421,37545,32751],{}," = blueprint",[198,37548,37549,37551],{},[2421,37550,32757],{}," = one thing created from the blueprint",[31,37553,2530],{"id":2529},[195,37555,37556,37559,37562,37565],{},[198,37557,37558],{},"What a class means in simple terms",[198,37560,37561],{},"What an object means in simple terms",[198,37563,37564],{},"How classes and objects work together",[198,37566,37567],{},"The beginner basics you need before moving to more advanced OOP topics",[31,37569,37571],{"id":37570},"what-is-a-class","What is a class?",[14,37573,2419,37574,37576],{},[2421,37575,2441],{}," is a way to group related data and behavior together.",[14,37578,37579],{},"You can think of a class as a template for making objects.",[14,37581,37582],{},"A class can contain:",[195,37584,37585,37590],{},[198,37586,37587,37589],{},[2421,37588,32402],{},": data stored in an object",[198,37591,37592,37594],{},[2421,37593,32408],{},": functions that belong to the class",[14,37596,37597],{},"Here is a very small class:",[36,37599,37601],{"className":38,"code":37600,"language":40,"meta":41,"style":41},"class Dog:\n    pass\n",[18,37602,37603,37611],{"__ignoreMap":41},[45,37604,37605,37607,37609],{"class":47,"line":48},[45,37606,2441],{"class":2440},[45,37608,2445],{"class":2444},[45,37610,86],{"class":85},[45,37612,37613],{"class":47,"line":63},[45,37614,5306],{"class":73},[14,37616,37617,37618,240],{},"This creates a class named ",[18,37619,2570],{},[14,37621,37622,37623,37625],{},"Right now, the class is empty. It does not store data or define behavior yet. But Python now knows that ",[18,37624,2570],{}," is a class you can use to create objects.",[14,37627,37628,37629,240],{},"If you want a shorter definition, see ",[236,37630,19949],{"href":19948},[31,37632,37634],{"id":37633},"what-is-an-object","What is an object?",[14,37636,19451,37637,37639],{},[2421,37638,13471],{}," is one value created from a class.",[14,37641,3259,37642,37644],{},[18,37643,2570],{}," is the blueprint, then a real dog object is something made from it.",[14,37646,2115],{},[36,37648,37650],{"className":38,"code":37649,"language":40,"meta":41,"style":41},"class Dog:\n    pass\n\nbuddy = Dog()\nmax_dog = Dog()\n\nprint(type(buddy))\nprint(type(max_dog))\n",[18,37651,37652,37660,37664,37668,37678,37689,37693,37707],{"__ignoreMap":41},[45,37653,37654,37656,37658],{"class":47,"line":48},[45,37655,2441],{"class":2440},[45,37657,2445],{"class":2444},[45,37659,86],{"class":85},[45,37661,37662],{"class":47,"line":63},[45,37663,5306],{"class":73},[45,37665,37666],{"class":47,"line":70},[45,37667,67],{"emptyLinePlaceholder":66},[45,37669,37670,37672,37674,37676],{"class":47,"line":89},[45,37671,37475],{"class":51},[45,37673,56],{"class":55},[45,37675,2445],{"class":2070},[45,37677,2495],{"class":85},[45,37679,37680,37683,37685,37687],{"class":47,"line":112},[45,37681,37682],{"class":51},"max_dog ",[45,37684,56],{"class":55},[45,37686,2445],{"class":2070},[45,37688,2495],{"class":85},[45,37690,37691],{"class":47,"line":126},[45,37692,67],{"emptyLinePlaceholder":66},[45,37694,37695,37697,37699,37701,37703,37705],{"class":47,"line":142},[45,37696,2065],{"class":92},[45,37698,96],{"class":85},[45,37700,4340],{"class":2109},[45,37702,96],{"class":85},[45,37704,37499],{"class":2070},[45,37706,4348],{"class":85},[45,37708,37709,37711,37713,37715,37717,37720],{"class":47,"line":149},[45,37710,2065],{"class":92},[45,37712,96],{"class":85},[45,37714,4340],{"class":2109},[45,37716,96],{"class":85},[45,37718,37719],{"class":2070},"max_dog",[45,37721,4348],{"class":85},[14,37723,37724],{},[2421,37725,177],{},[36,37727,37728],{"className":38,"code":32330,"language":40,"meta":41,"style":41},[18,37729,37730,37744],{"__ignoreMap":41},[45,37731,37732,37734,37736,37738,37740,37742],{"class":47,"line":48},[45,37733,79],{"class":55},[45,37735,2441],{"class":2440},[45,37737,9199],{"class":99},[45,37739,32343],{"class":103},[45,37741,7901],{"class":99},[45,37743,13484],{"class":55},[45,37745,37746,37748,37750,37752,37754,37756],{"class":47,"line":63},[45,37747,79],{"class":55},[45,37749,2441],{"class":2440},[45,37751,9199],{"class":99},[45,37753,32343],{"class":103},[45,37755,7901],{"class":99},[45,37757,13484],{"class":55},[14,37759,6680],{},[195,37761,37762,37767,37772],{},[198,37763,37764,37766],{},[18,37765,37499],{}," is an object",[198,37768,37769,37771],{},[18,37770,37719],{}," is another object",[198,37773,37774,37775],{},"both were created from the same class, ",[18,37776,2570],{},[14,37778,37779],{},"Each object can store its own data. That is one reason classes are useful.",[14,37781,37782,37783,240],{},"If you want a simple definition, see ",[236,37784,20480],{"href":20479},[31,37786,37788],{"id":37787},"why-classes-are-useful","Why classes are useful",[14,37790,37791],{},"Classes help when you want to keep related code together.",[14,37793,37794],{},"They are useful because:",[195,37796,37797,37800,37803,37806],{},[198,37798,37799],{},"They organize data and behavior in one place",[198,37801,37802],{},"They help model real things like dogs, cars, or students",[198,37804,37805],{},"They let you reuse the same structure many times",[198,37807,37808],{},"They make larger programs easier to understand",[14,37810,37811],{},"For very small scripts, you may not need classes. But as programs grow, classes often make code easier to manage.",[14,37813,37814,37815,240],{},"If you want the bigger picture, see ",[236,37816,37818],{"href":37817},"\u002Flearn\u002Fobject-oriented-programming-in-python-explained\u002F","object-oriented programming in Python explained",[31,37820,37822],{"id":37821},"basic-class-syntax","Basic class syntax",[14,37824,37825,37826,37828],{},"You define a class with the ",[18,37827,2441],{}," keyword:",[36,37830,37832],{"className":38,"code":37831,"language":40,"meta":41,"style":41},"class ClassName:\n    pass\n",[18,37833,37834,37843],{"__ignoreMap":41},[45,37835,37836,37838,37841],{"class":47,"line":48},[45,37837,2441],{"class":2440},[45,37839,37840],{"class":2444}," ClassName",[45,37842,86],{"class":85},[45,37844,37845],{"class":47,"line":63},[45,37846,5306],{"class":73},[14,37848,37849],{},"A more useful class often includes methods:",[36,37851,37853],{"className":38,"code":37852,"language":40,"meta":41,"style":41},"class Dog:\n    def bark(self):\n        print(\"Woof!\")\n",[18,37854,37855,37863,37875],{"__ignoreMap":41},[45,37856,37857,37859,37861],{"class":47,"line":48},[45,37858,2441],{"class":2440},[45,37860,2445],{"class":2444},[45,37862,86],{"class":85},[45,37864,37865,37867,37869,37871,37873],{"class":47,"line":63},[45,37866,2452],{"class":2440},[45,37868,2456],{"class":2455},[45,37870,96],{"class":85},[45,37872,2427],{"class":2461},[45,37874,2464],{"class":85},[45,37876,37877,37879,37881,37883,37885,37887],{"class":47,"line":70},[45,37878,1923],{"class":92},[45,37880,96],{"class":85},[45,37882,100],{"class":99},[45,37884,2475],{"class":103},[45,37886,100],{"class":99},[45,37888,109],{"class":85},[14,37890,37891],{},"A few important points:",[195,37893,37894,37900,37906,37911],{},[198,37895,37896,37899],{},[18,37897,37898],{},"class Dog:"," starts the class definition",[198,37901,37902,37905],{},[18,37903,37904],{},"def bark(self):"," defines a method inside the class",[198,37907,37908,37910],{},[18,37909,2427],{}," refers to the current object",[198,37912,37913],{},"Indentation matters inside the class",[14,37915,2419,37916,37918],{},[2421,37917,2423],{}," is just a function inside a class.",[14,37920,37921,37922,240],{},"When you call a method on an object, Python automatically connects that object to ",[18,37923,2427],{},[31,37925,37927],{"id":37926},"creating-objects-from-a-class","Creating objects from a class",[14,37929,37930],{},"You create an object by calling the class name like a function:",[36,37932,37934],{"className":38,"code":37933,"language":40,"meta":41,"style":41},"class Dog:\n    pass\n\nbuddy = Dog()\n",[18,37935,37936,37944,37948,37952],{"__ignoreMap":41},[45,37937,37938,37940,37942],{"class":47,"line":48},[45,37939,2441],{"class":2440},[45,37941,2445],{"class":2444},[45,37943,86],{"class":85},[45,37945,37946],{"class":47,"line":63},[45,37947,5306],{"class":73},[45,37949,37950],{"class":47,"line":70},[45,37951,67],{"emptyLinePlaceholder":66},[45,37953,37954,37956,37958,37960],{"class":47,"line":89},[45,37955,37475],{"class":51},[45,37957,56],{"class":55},[45,37959,2445],{"class":2070},[45,37961,2495],{"class":85},[14,37963,37964,37965,37967],{},"Now ",[18,37966,37499],{}," stores a new object.",[14,37969,37970,37971,457],{},"You can then access attributes and methods with ",[2421,37972,2597],{},[36,37974,37976],{"className":38,"code":37975,"language":40,"meta":41,"style":41},"class Dog:\n    def bark(self):\n        print(\"Woof!\")\n\nbuddy = Dog()\nbuddy.bark()\n",[18,37977,37978,37986,37998,38012,38016,38026],{"__ignoreMap":41},[45,37979,37980,37982,37984],{"class":47,"line":48},[45,37981,2441],{"class":2440},[45,37983,2445],{"class":2444},[45,37985,86],{"class":85},[45,37987,37988,37990,37992,37994,37996],{"class":47,"line":63},[45,37989,2452],{"class":2440},[45,37991,2456],{"class":2455},[45,37993,96],{"class":85},[45,37995,2427],{"class":2461},[45,37997,2464],{"class":85},[45,37999,38000,38002,38004,38006,38008,38010],{"class":47,"line":70},[45,38001,1923],{"class":92},[45,38003,96],{"class":85},[45,38005,100],{"class":99},[45,38007,2475],{"class":103},[45,38009,100],{"class":99},[45,38011,109],{"class":85},[45,38013,38014],{"class":47,"line":89},[45,38015,67],{"emptyLinePlaceholder":66},[45,38017,38018,38020,38022,38024],{"class":47,"line":112},[45,38019,37475],{"class":51},[45,38021,56],{"class":55},[45,38023,2445],{"class":2070},[45,38025,2495],{"class":85},[45,38027,38028,38030,38032,38034],{"class":47,"line":126},[45,38029,37499],{"class":51},[45,38031,240],{"class":85},[45,38033,2505],{"class":2070},[45,38035,2495],{"class":85},[14,38037,38038],{},[2421,38039,177],{},[36,38041,38042],{"className":38,"code":2514,"language":40,"meta":41,"style":41},[18,38043,38044],{"__ignoreMap":41},[45,38045,38046],{"class":47,"line":48},[45,38047,2514],{"class":51},[14,38049,38050],{},"This is the basic pattern:",[195,38052,38053,38056,38059],{},[198,38054,38055],{},"define a class",[198,38057,38058],{},"create an object from it",[198,38060,38061],{},"use dot notation to work with it",[14,38063,38064,38065,240],{},"For a step-by-step guide, see ",[236,38066,38067],{"href":33431},"how to create an object in Python",[31,38069,32397],{"id":32396},[14,38071,38072],{},"Attributes are the data stored in an object.",[14,38074,38075],{},"Methods are the actions an object can perform.",[14,38077,38078],{},"Here is a simple example that shows both:",[36,38080,38082],{"className":38,"code":38081,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(self.name + \" says woof!\")\n\nbuddy = Dog(\"Buddy\")\nprint(buddy.name)\nbuddy.bark()\n",[18,38083,38084,38092,38108,38120,38124,38136,38159,38163,38181,38195],{"__ignoreMap":41},[45,38085,38086,38088,38090],{"class":47,"line":48},[45,38087,2441],{"class":2440},[45,38089,2445],{"class":2444},[45,38091,86],{"class":85},[45,38093,38094,38096,38098,38100,38102,38104,38106],{"class":47,"line":63},[45,38095,2452],{"class":2440},[45,38097,3072],{"class":92},[45,38099,96],{"class":85},[45,38101,2427],{"class":2461},[45,38103,3079],{"class":85},[45,38105,3083],{"class":3082},[45,38107,2464],{"class":85},[45,38109,38110,38112,38114,38116,38118],{"class":47,"line":70},[45,38111,3090],{"class":2771},[45,38113,240],{"class":85},[45,38115,3096],{"class":3095},[45,38117,3099],{"class":55},[45,38119,3102],{"class":51},[45,38121,38122],{"class":47,"line":89},[45,38123,67],{"emptyLinePlaceholder":66},[45,38125,38126,38128,38130,38132,38134],{"class":47,"line":112},[45,38127,2452],{"class":2440},[45,38129,2456],{"class":2455},[45,38131,96],{"class":85},[45,38133,2427],{"class":2461},[45,38135,2464],{"class":85},[45,38137,38138,38140,38142,38144,38146,38148,38150,38152,38155,38157],{"class":47,"line":126},[45,38139,1923],{"class":92},[45,38141,96],{"class":85},[45,38143,2427],{"class":2771},[45,38145,240],{"class":85},[45,38147,3096],{"class":3095},[45,38149,3775],{"class":55},[45,38151,1565],{"class":99},[45,38153,38154],{"class":103}," says woof!",[45,38156,100],{"class":99},[45,38158,109],{"class":85},[45,38160,38161],{"class":47,"line":142},[45,38162,67],{"emptyLinePlaceholder":66},[45,38164,38165,38167,38169,38171,38173,38175,38177,38179],{"class":47,"line":149},[45,38166,37475],{"class":51},[45,38168,56],{"class":55},[45,38170,2445],{"class":2070},[45,38172,96],{"class":85},[45,38174,100],{"class":99},[45,38176,37486],{"class":103},[45,38178,100],{"class":99},[45,38180,109],{"class":85},[45,38182,38183,38185,38187,38189,38191,38193],{"class":47,"line":679},[45,38184,2065],{"class":92},[45,38186,96],{"class":85},[45,38188,37499],{"class":2070},[45,38190,240],{"class":85},[45,38192,3096],{"class":3095},[45,38194,109],{"class":85},[45,38196,38197,38199,38201,38203],{"class":47,"line":686},[45,38198,37499],{"class":51},[45,38200,240],{"class":85},[45,38202,2505],{"class":2070},[45,38204,2495],{"class":85},[14,38206,38207],{},[2421,38208,177],{},[36,38210,38212],{"className":38,"code":38211,"language":40,"meta":41,"style":41},"Buddy\nBuddy says woof!\n",[18,38213,38214,38218],{"__ignoreMap":41},[45,38215,38216],{"class":47,"line":48},[45,38217,37512],{"class":51},[45,38219,38220],{"class":47,"line":63},[45,38221,38222],{"class":51},"Buddy says woof!\n",[14,38224,6570],{},[195,38226,38227,38233,38239],{},[198,38228,38229,32152,38231],{},[18,38230,3044],{},[2421,38232,32155],{},[198,38234,38235,6578,38237],{},[18,38236,2574],{},[2421,38238,2423],{},[198,38240,38241,38243],{},[18,38242,37499],{}," is the object using both",[14,38245,38246],{},"This is an important idea:",[195,38248,38249,38254],{},[198,38250,38251,38253],{},[2421,38252,30614],{}," belongs in attributes",[198,38255,38256,38259],{},[2421,38257,38258],{},"behavior"," belongs in methods",[31,38261,1732,38263,2575],{"id":38262},"the-__init__-method",[18,38264,3262],{},[14,38266,1732,38267,38269],{},[18,38268,3262],{}," method is a special method that runs when an object is created.",[14,38271,38272],{},"It is commonly used to give the object its starting values.",[14,38274,466],{},[36,38276,38277],{"className":38,"code":37428,"language":40,"meta":41,"style":41},[18,38278,38279,38287,38303,38315,38319,38337],{"__ignoreMap":41},[45,38280,38281,38283,38285],{"class":47,"line":48},[45,38282,2441],{"class":2440},[45,38284,2445],{"class":2444},[45,38286,86],{"class":85},[45,38288,38289,38291,38293,38295,38297,38299,38301],{"class":47,"line":63},[45,38290,2452],{"class":2440},[45,38292,3072],{"class":92},[45,38294,96],{"class":85},[45,38296,2427],{"class":2461},[45,38298,3079],{"class":85},[45,38300,3083],{"class":3082},[45,38302,2464],{"class":85},[45,38304,38305,38307,38309,38311,38313],{"class":47,"line":70},[45,38306,3090],{"class":2771},[45,38308,240],{"class":85},[45,38310,3096],{"class":3095},[45,38312,3099],{"class":55},[45,38314,3102],{"class":51},[45,38316,38317],{"class":47,"line":89},[45,38318,67],{"emptyLinePlaceholder":66},[45,38320,38321,38323,38325,38327,38329,38331,38333,38335],{"class":47,"line":112},[45,38322,37475],{"class":51},[45,38324,56],{"class":55},[45,38326,2445],{"class":2070},[45,38328,96],{"class":85},[45,38330,100],{"class":99},[45,38332,37486],{"class":103},[45,38334,100],{"class":99},[45,38336,109],{"class":85},[45,38338,38339,38341,38343,38345,38347,38349],{"class":47,"line":126},[45,38340,2065],{"class":92},[45,38342,96],{"class":85},[45,38344,37499],{"class":2070},[45,38346,240],{"class":85},[45,38348,3096],{"class":3095},[45,38350,109],{"class":85},[14,38352,36177,38353,38356],{},[18,38354,38355],{},"Dog(\"Buddy\")"," runs:",[195,38358,38359,38362,38366],{},[198,38360,38361],{},"Python creates a new object",[198,38363,38364,32714],{},[18,38365,3262],{},[198,38367,38368,38371,38372],{},[18,38369,38370],{},"\"Buddy\""," gets assigned to ",[18,38373,3044],{},[14,38375,2854,38376,38378,38379,38381],{},[2421,38377,2857],{}," need ",[18,38380,3262],{}," in every class. Use it when you want to set up attributes when the object is made.",[14,38383,38384,38385,240],{},"To learn this in more detail, see ",[236,38386,3267,38387,3270],{"href":3266},[18,38388,3262],{},[31,38390,38392],{"id":38391},"simple-real-example","Simple real example",[14,38394,38395,38396,38399],{},"Here is a short, beginner-friendly example using a ",[18,38397,38398],{},"Student"," class.",[36,38401,38403],{"className":38,"code":38402,"language":40,"meta":41,"style":41},"class Student:\n    def __init__(self, name, grade):\n        self.name = name\n        self.grade = grade\n\n    def introduce(self):\n        print(\"Hi, my name is \" + self.name)\n        print(\"My grade is \" + str(self.grade))\n\nstudent1 = Student(\"Ava\", 5)\n\nprint(student1.name)\nprint(student1.grade)\nstudent1.introduce()\n",[18,38404,38405,38413,38434,38446,38460,38464,38476,38499,38527,38531,38554,38558,38573,38587],{"__ignoreMap":41},[45,38406,38407,38409,38411],{"class":47,"line":48},[45,38408,2441],{"class":2440},[45,38410,20914],{"class":2444},[45,38412,86],{"class":85},[45,38414,38415,38417,38419,38421,38423,38425,38427,38429,38432],{"class":47,"line":63},[45,38416,2452],{"class":2440},[45,38418,3072],{"class":92},[45,38420,96],{"class":85},[45,38422,2427],{"class":2461},[45,38424,3079],{"class":85},[45,38426,3083],{"class":3082},[45,38428,3079],{"class":85},[45,38430,38431],{"class":3082}," grade",[45,38433,2464],{"class":85},[45,38435,38436,38438,38440,38442,38444],{"class":47,"line":70},[45,38437,3090],{"class":2771},[45,38439,240],{"class":85},[45,38441,3096],{"class":3095},[45,38443,3099],{"class":55},[45,38445,3102],{"class":51},[45,38447,38448,38450,38452,38455,38457],{"class":47,"line":89},[45,38449,3090],{"class":2771},[45,38451,240],{"class":85},[45,38453,38454],{"class":3095},"grade",[45,38456,3099],{"class":55},[45,38458,38459],{"class":51}," grade\n",[45,38461,38462],{"class":47,"line":112},[45,38463,67],{"emptyLinePlaceholder":66},[45,38465,38466,38468,38470,38472,38474],{"class":47,"line":126},[45,38467,2452],{"class":2440},[45,38469,11955],{"class":2455},[45,38471,96],{"class":85},[45,38473,2427],{"class":2461},[45,38475,2464],{"class":85},[45,38477,38478,38480,38482,38484,38487,38489,38491,38493,38495,38497],{"class":47,"line":142},[45,38479,1923],{"class":92},[45,38481,96],{"class":85},[45,38483,100],{"class":99},[45,38485,38486],{"class":103},"Hi, my name is ",[45,38488,100],{"class":99},[45,38490,3775],{"class":55},[45,38492,3136],{"class":2771},[45,38494,240],{"class":85},[45,38496,3096],{"class":3095},[45,38498,109],{"class":85},[45,38500,38501,38503,38505,38507,38510,38512,38514,38517,38519,38521,38523,38525],{"class":47,"line":149},[45,38502,1923],{"class":92},[45,38504,96],{"class":85},[45,38506,100],{"class":99},[45,38508,38509],{"class":103},"My grade is ",[45,38511,100],{"class":99},[45,38513,3775],{"class":55},[45,38515,38516],{"class":2109}," str",[45,38518,96],{"class":85},[45,38520,2427],{"class":2771},[45,38522,240],{"class":85},[45,38524,38454],{"class":3095},[45,38526,4348],{"class":85},[45,38528,38529],{"class":47,"line":679},[45,38530,67],{"emptyLinePlaceholder":66},[45,38532,38533,38536,38538,38540,38542,38544,38546,38548,38550,38552],{"class":47,"line":686},[45,38534,38535],{"class":51},"student1 ",[45,38537,56],{"class":55},[45,38539,20914],{"class":2070},[45,38541,96],{"class":85},[45,38543,100],{"class":99},[45,38545,3162],{"class":103},[45,38547,100],{"class":99},[45,38549,3079],{"class":85},[45,38551,1222],{"class":59},[45,38553,109],{"class":85},[45,38555,38556],{"class":47,"line":2229},[45,38557,67],{"emptyLinePlaceholder":66},[45,38559,38560,38562,38564,38567,38569,38571],{"class":47,"line":2236},[45,38561,2065],{"class":92},[45,38563,96],{"class":85},[45,38565,38566],{"class":2070},"student1",[45,38568,240],{"class":85},[45,38570,3096],{"class":3095},[45,38572,109],{"class":85},[45,38574,38575,38577,38579,38581,38583,38585],{"class":47,"line":5728},[45,38576,2065],{"class":92},[45,38578,96],{"class":85},[45,38580,38566],{"class":2070},[45,38582,240],{"class":85},[45,38584,38454],{"class":3095},[45,38586,109],{"class":85},[45,38588,38589,38591,38593,38595],{"class":47,"line":19380},[45,38590,38566],{"class":51},[45,38592,240],{"class":85},[45,38594,12005],{"class":2070},[45,38596,2495],{"class":85},[14,38598,38599],{},[2421,38600,177],{},[36,38602,38604],{"className":38,"code":38603,"language":40,"meta":41,"style":41},"Ava\n5\nHi, my name is Ava\nMy grade is 5\n",[18,38605,38606,38611,38615,38627],{"__ignoreMap":41},[45,38607,38608],{"class":47,"line":48},[45,38609,38610],{"class":51},"Ava\n",[45,38612,38613],{"class":47,"line":63},[45,38614,11570],{"class":59},[45,38616,38617,38619,38621,38623,38625],{"class":47,"line":70},[45,38618,6510],{"class":51},[45,38620,3079],{"class":85},[45,38622,3231],{"class":51},[45,38624,1285],{"class":55},[45,38626,3236],{"class":51},[45,38628,38629,38632,38634],{"class":47,"line":89},[45,38630,38631],{"class":51},"My grade ",[45,38633,1285],{"class":55},[45,38635,733],{"class":59},[14,38637,38638],{},"What this example shows:",[195,38640,38641,38645,38649,38656],{},[198,38642,38643,32371],{},[18,38644,38398],{},[198,38646,38647,37766],{},[18,38648,38566],{},[198,38650,38651,1316,38653,38655],{},[18,38652,3096],{},[18,38654,38454],{}," are attributes",[198,38657,38658,32572],{},[18,38659,38660],{},"introduce()",[14,38662,38663],{},"This is the full beginner pattern in one example:",[346,38665,38666,38669,38674,38676,38679],{},[198,38667,38668],{},"Define a class",[198,38670,390,38671,38673],{},[18,38672,3262],{}," to set starting values",[198,38675,2907],{},[198,38677,38678],{},"Access attributes with dot notation",[198,38680,38681],{},"Call methods with dot notation",[14,38683,38684,38685,240],{},"If you want to build one from scratch, see ",[236,38686,38687],{"href":33425},"how to create a class in Python",[31,38689,1337],{"id":1336},[14,38691,38692],{},"Here are some very common problems when learning classes and objects.",[712,38694,3800,38696,38698],{"id":38695},"forgetting-self-in-a-method",[18,38697,2427],{}," in a method",[14,38700,38701,38702,3889],{},"This causes errors because instance methods need ",[18,38703,2427],{},[14,38705,3806],{},[36,38707,38709],{"className":38,"code":38708,"language":40,"meta":41,"style":41},"class Dog:\n    def bark():\n        print(\"Woof!\")\n",[18,38710,38711,38719,38727],{"__ignoreMap":41},[45,38712,38713,38715,38717],{"class":47,"line":48},[45,38714,2441],{"class":2440},[45,38716,2445],{"class":2444},[45,38718,86],{"class":85},[45,38720,38721,38723,38725],{"class":47,"line":63},[45,38722,2452],{"class":2440},[45,38724,2456],{"class":2455},[45,38726,3828],{"class":85},[45,38728,38729,38731,38733,38735,38737,38739],{"class":47,"line":70},[45,38730,1923],{"class":92},[45,38732,96],{"class":85},[45,38734,100],{"class":99},[45,38736,2475],{"class":103},[45,38738,100],{"class":99},[45,38740,109],{"class":85},[14,38742,1466],{},[36,38744,38745],{"className":38,"code":37852,"language":40,"meta":41,"style":41},[18,38746,38747,38755,38767],{"__ignoreMap":41},[45,38748,38749,38751,38753],{"class":47,"line":48},[45,38750,2441],{"class":2440},[45,38752,2445],{"class":2444},[45,38754,86],{"class":85},[45,38756,38757,38759,38761,38763,38765],{"class":47,"line":63},[45,38758,2452],{"class":2440},[45,38760,2456],{"class":2455},[45,38762,96],{"class":85},[45,38764,2427],{"class":2461},[45,38766,2464],{"class":85},[45,38768,38769,38771,38773,38775,38777,38779],{"class":47,"line":70},[45,38770,1923],{"class":92},[45,38772,96],{"class":85},[45,38774,100],{"class":99},[45,38776,2475],{"class":103},[45,38778,100],{"class":99},[45,38780,109],{"class":85},[712,38782,38784],{"id":38783},"trying-to-use-an-attribute-before-setting-it","Trying to use an attribute before setting it",[14,38786,38787],{},"If the attribute was never created, Python will raise an error.",[14,38789,3806],{},[36,38791,38793],{"className":38,"code":38792,"language":40,"meta":41,"style":41},"class Dog:\n    def bark(self):\n        print(self.name)\n",[18,38794,38795,38803,38815],{"__ignoreMap":41},[45,38796,38797,38799,38801],{"class":47,"line":48},[45,38798,2441],{"class":2440},[45,38800,2445],{"class":2444},[45,38802,86],{"class":85},[45,38804,38805,38807,38809,38811,38813],{"class":47,"line":63},[45,38806,2452],{"class":2440},[45,38808,2456],{"class":2455},[45,38810,96],{"class":85},[45,38812,2427],{"class":2461},[45,38814,2464],{"class":85},[45,38816,38817,38819,38821,38823,38825,38827],{"class":47,"line":70},[45,38818,1923],{"class":92},[45,38820,96],{"class":85},[45,38822,2427],{"class":2771},[45,38824,240],{"class":85},[45,38826,3096],{"class":3095},[45,38828,109],{"class":85},[14,38830,38831,38832,38834,38835,38837,38838,38840],{},"If you create a ",[18,38833,2570],{}," object and call ",[18,38836,2574],{}," before setting ",[18,38839,3044],{},", the code will fail.",[14,38842,1466],{},[36,38844,38846],{"className":38,"code":38845,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(self.name)\n",[18,38847,38848,38856,38872,38884,38888,38900],{"__ignoreMap":41},[45,38849,38850,38852,38854],{"class":47,"line":48},[45,38851,2441],{"class":2440},[45,38853,2445],{"class":2444},[45,38855,86],{"class":85},[45,38857,38858,38860,38862,38864,38866,38868,38870],{"class":47,"line":63},[45,38859,2452],{"class":2440},[45,38861,3072],{"class":92},[45,38863,96],{"class":85},[45,38865,2427],{"class":2461},[45,38867,3079],{"class":85},[45,38869,3083],{"class":3082},[45,38871,2464],{"class":85},[45,38873,38874,38876,38878,38880,38882],{"class":47,"line":70},[45,38875,3090],{"class":2771},[45,38877,240],{"class":85},[45,38879,3096],{"class":3095},[45,38881,3099],{"class":55},[45,38883,3102],{"class":51},[45,38885,38886],{"class":47,"line":89},[45,38887,67],{"emptyLinePlaceholder":66},[45,38889,38890,38892,38894,38896,38898],{"class":47,"line":112},[45,38891,2452],{"class":2440},[45,38893,2456],{"class":2455},[45,38895,96],{"class":85},[45,38897,2427],{"class":2461},[45,38899,2464],{"class":85},[45,38901,38902,38904,38906,38908,38910,38912],{"class":47,"line":126},[45,38903,1923],{"class":92},[45,38905,96],{"class":85},[45,38907,2427],{"class":2771},[45,38909,240],{"class":85},[45,38911,3096],{"class":3095},[45,38913,109],{"class":85},[712,38915,38917],{"id":38916},"confusing-the-class-with-an-object","Confusing the class with an object",[14,38919,38920],{},"A class is not the same thing as an object.",[14,38922,38923],{},"Wrong idea:",[36,38925,38927],{"className":38,"code":38926,"language":40,"meta":41,"style":41},"class Dog:\n    pass\n\nprint(Dog.name)\n",[18,38928,38929,38937,38941,38945],{"__ignoreMap":41},[45,38930,38931,38933,38935],{"class":47,"line":48},[45,38932,2441],{"class":2440},[45,38934,2445],{"class":2444},[45,38936,86],{"class":85},[45,38938,38939],{"class":47,"line":63},[45,38940,5306],{"class":73},[45,38942,38943],{"class":47,"line":70},[45,38944,67],{"emptyLinePlaceholder":66},[45,38946,38947,38949,38951,38953,38955,38957],{"class":47,"line":89},[45,38948,2065],{"class":92},[45,38950,96],{"class":85},[45,38952,2570],{"class":2070},[45,38954,240],{"class":85},[45,38956,3096],{"class":3095},[45,38958,109],{"class":85},[14,38960,38961,38963,38964,240],{},[18,38962,2570],{}," is the class itself. It does not automatically have object-specific data like ",[18,38965,3096],{},[14,38967,38968],{},"You usually need to create an object first:",[36,38970,38971],{"className":38,"code":37428,"language":40,"meta":41,"style":41},[18,38972,38973,38981,38997,39009,39013,39031],{"__ignoreMap":41},[45,38974,38975,38977,38979],{"class":47,"line":48},[45,38976,2441],{"class":2440},[45,38978,2445],{"class":2444},[45,38980,86],{"class":85},[45,38982,38983,38985,38987,38989,38991,38993,38995],{"class":47,"line":63},[45,38984,2452],{"class":2440},[45,38986,3072],{"class":92},[45,38988,96],{"class":85},[45,38990,2427],{"class":2461},[45,38992,3079],{"class":85},[45,38994,3083],{"class":3082},[45,38996,2464],{"class":85},[45,38998,38999,39001,39003,39005,39007],{"class":47,"line":70},[45,39000,3090],{"class":2771},[45,39002,240],{"class":85},[45,39004,3096],{"class":3095},[45,39006,3099],{"class":55},[45,39008,3102],{"class":51},[45,39010,39011],{"class":47,"line":89},[45,39012,67],{"emptyLinePlaceholder":66},[45,39014,39015,39017,39019,39021,39023,39025,39027,39029],{"class":47,"line":112},[45,39016,37475],{"class":51},[45,39018,56],{"class":55},[45,39020,2445],{"class":2070},[45,39022,96],{"class":85},[45,39024,100],{"class":99},[45,39026,37486],{"class":103},[45,39028,100],{"class":99},[45,39030,109],{"class":85},[45,39032,39033,39035,39037,39039,39041,39043],{"class":47,"line":126},[45,39034,2065],{"class":92},[45,39036,96],{"class":85},[45,39038,37499],{"class":2070},[45,39040,240],{"class":85},[45,39042,3096],{"class":3095},[45,39044,109],{"class":85},[712,39046,39048],{"id":39047},"indentation-problems-inside-the-class","Indentation problems inside the class",[14,39050,39051],{},"Python uses indentation to show which code belongs inside the class and inside each method.",[14,39053,3806],{},[36,39055,39057],{"className":38,"code":39056,"language":40,"meta":41,"style":41},"class Dog:\ndef bark(self):\n    print(\"Woof!\")\n",[18,39058,39059,39067,39079],{"__ignoreMap":41},[45,39060,39061,39063,39065],{"class":47,"line":48},[45,39062,2441],{"class":2440},[45,39064,2445],{"class":2444},[45,39066,86],{"class":85},[45,39068,39069,39071,39073,39075,39077],{"class":47,"line":63},[45,39070,2757],{"class":2440},[45,39072,2456],{"class":2455},[45,39074,96],{"class":85},[45,39076,2427],{"class":2461},[45,39078,2464],{"class":85},[45,39080,39081,39083,39085,39087,39089,39091],{"class":47,"line":70},[45,39082,93],{"class":92},[45,39084,96],{"class":85},[45,39086,100],{"class":99},[45,39088,2475],{"class":103},[45,39090,100],{"class":99},[45,39092,109],{"class":85},[14,39094,1466],{},[36,39096,39097],{"className":38,"code":37852,"language":40,"meta":41,"style":41},[18,39098,39099,39107,39119],{"__ignoreMap":41},[45,39100,39101,39103,39105],{"class":47,"line":48},[45,39102,2441],{"class":2440},[45,39104,2445],{"class":2444},[45,39106,86],{"class":85},[45,39108,39109,39111,39113,39115,39117],{"class":47,"line":63},[45,39110,2452],{"class":2440},[45,39112,2456],{"class":2455},[45,39114,96],{"class":85},[45,39116,2427],{"class":2461},[45,39118,2464],{"class":85},[45,39120,39121,39123,39125,39127,39129,39131],{"class":47,"line":70},[45,39122,1923],{"class":92},[45,39124,96],{"class":85},[45,39126,100],{"class":99},[45,39128,2475],{"class":103},[45,39130,100],{"class":99},[45,39132,109],{"class":85},[31,39134,2281],{"id":2280},[712,39136,39138],{"id":39137},"what-is-the-difference-between-a-class-and-an-object-in-python","What is the difference between a class and an object in Python?",[14,39140,39141],{},"A class is a blueprint. An object is one actual instance created from that blueprint.",[712,39143,39145],{"id":39144},"do-i-need-classes-as-a-beginner","Do I need classes as a beginner?",[14,39147,39148],{},"Not for every program, but classes become useful when you want to group data and behavior together.",[712,39150,19018,39152,39154],{"id":39151},"what-does-self-mean-in-a-python-class",[18,39153,2427],{}," mean in a Python class?",[14,39156,39157,39159],{},[18,39158,2427],{}," refers to the current object. It lets each object store and use its own data.",[712,39161,350,39163,39165],{"id":39162},"is-__init__-required-in-every-class",[18,39164,3262],{}," required in every class?",[14,39167,39168],{},"No. You only need it when you want to set up attributes when an object is created.",[31,39170,2342],{"id":2341},[195,39172,39173,39178,39184,39188,39192,39196],{},[198,39174,39175],{},[236,39176,39177],{"href":37817},"Object-oriented programming in Python explained",[198,39179,39180],{},[236,39181,1732,39182,3270],{"href":3266},[18,39183,3262],{},[198,39185,39186],{},[236,39187,22039],{"href":22038},[198,39189,39190],{},[236,39191,4487],{"href":33157},[198,39193,39194],{},[236,39195,33426],{"href":33425},[198,39197,39198],{},[236,39199,33432],{"href":33431},[2367,39201,39202],{},"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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .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 .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}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":39204},[39205,39206,39207,39208,39209,39210,39211,39212,39213,39215,39216,39223,39231],{"id":33,"depth":63,"text":34},{"id":2529,"depth":63,"text":2530},{"id":37570,"depth":63,"text":37571},{"id":37633,"depth":63,"text":37634},{"id":37787,"depth":63,"text":37788},{"id":37821,"depth":63,"text":37822},{"id":37926,"depth":63,"text":37927},{"id":32396,"depth":63,"text":32397},{"id":38262,"depth":63,"text":39214},"The __init__ method",{"id":38391,"depth":63,"text":38392},{"id":1336,"depth":63,"text":1337,"children":39217},[39218,39220,39221,39222],{"id":38695,"depth":70,"text":39219},"Forgetting self in a method",{"id":38783,"depth":70,"text":38784},{"id":38916,"depth":70,"text":38917},{"id":39047,"depth":70,"text":39048},{"id":2280,"depth":63,"text":2281,"children":39224},[39225,39226,39227,39229],{"id":39137,"depth":70,"text":39138},{"id":39144,"depth":70,"text":39145},{"id":39151,"depth":70,"text":39228},"What does self mean in a Python class?",{"id":39162,"depth":70,"text":39230},"Is __init__ required in every class?",{"id":2341,"depth":63,"text":2342},"Master python classes and objects explained in our comprehensive Python beginner guide.",{},{"title":37404,"description":39232},"learn\u002Fpython-classes-and-objects-explained","gETW6bTccgV1sNKJR9y_N6yeDT9DuHBSJN6SRuDg5nQ",{"id":39238,"title":39239,"body":39240,"description":40405,"extension":2399,"meta":40406,"navigation":66,"path":40407,"seo":40408,"stem":40409,"__hash__":40410},"content\u002Flearn\u002Fpython-comments-explained-single-line-and-multi-line.md","Python Comments Explained (Single-Line and Multi-Line)",{"type":7,"value":39241,"toc":40378},[39242,39245,39248,39251,39254,39256,39321,39330,39334,39353,39357,39360,39363,39377,39383,39385,39421,39424,39428,39433,39439,39463,39466,39495,39498,39501,39512,39516,39519,39539,39542,39545,39556,39560,39583,39587,39607,39610,39620,39624,39627,39633,39690,39693,39696,39733,39736,39740,39743,39782,39784,39814,39817,39821,39845,39849,39878,39885,39887,39930,39933,39943,39947,39950,39953,39971,39973,39997,40000,40003,40033,40037,40040,40043,40057,40060,40082,40085,40088,40091,40093,40096,40113,40116,40124,40126,40151,40154,40162,40164,40167,40184,40187,40220,40223,40226,40260,40263,40274,40277,40303,40306,40308,40312,40317,40321,40327,40331,40334,40338,40341,40345,40348,40350,40375],[10,39243,39239],{"id":39244},"python-comments-explained-single-line-and-multi-line",[14,39246,39247],{},"Comments help you write code that is easier to read and understand.",[14,39249,39250],{},"In Python, comments are notes for humans. Python ignores them when it runs your program. This makes comments useful for explaining what your code does, why it exists, or what to watch out for.",[14,39252,39253],{},"This page shows you how to write comments in Python, when to use them, and how to avoid confusing comments with triple-quoted strings.",[31,39255,34],{"id":33},[36,39257,39259],{"className":38,"code":39258,"language":40,"meta":41,"style":41},"# Single-line comment\nname = \"Maya\"  # Inline comment\n\n\"\"\"\nThis is a multi-line string.\nIt is often used like a comment,\nbut it is not a real comment.\n\"\"\"\nprint(name)\n",[18,39260,39261,39266,39281,39285,39291,39297,39302,39307,39311],{"__ignoreMap":41},[45,39262,39263],{"class":47,"line":48},[45,39264,39265],{"class":7624},"# Single-line comment\n",[45,39267,39268,39270,39272,39274,39276,39278],{"class":47,"line":63},[45,39269,1560],{"class":51},[45,39271,56],{"class":55},[45,39273,1565],{"class":99},[45,39275,5737],{"class":103},[45,39277,100],{"class":99},[45,39279,39280],{"class":7624},"  # Inline comment\n",[45,39282,39283],{"class":47,"line":70},[45,39284,67],{"emptyLinePlaceholder":66},[45,39286,39287],{"class":47,"line":89},[45,39288,39290],{"class":39289},"s2W-s","\"\"\"\n",[45,39292,39293],{"class":47,"line":112},[45,39294,39296],{"class":39295},"sithA","This is a multi-line string.\n",[45,39298,39299],{"class":47,"line":126},[45,39300,39301],{"class":39295},"It is often used like a comment,\n",[45,39303,39304],{"class":47,"line":142},[45,39305,39306],{"class":39295},"but it is not a real comment.\n",[45,39308,39309],{"class":47,"line":149},[45,39310,39290],{"class":39289},[45,39312,39313,39315,39317,39319],{"class":47,"line":679},[45,39314,2065],{"class":92},[45,39316,96],{"class":85},[45,39318,3096],{"class":2070},[45,39320,109],{"class":85},[14,39322,39323,39325,39326,39329],{},[2421,39324,21487],{}," Use ",[18,39327,39328],{},"#"," for real comments. Triple quotes create a string, not a true comment.",[31,39331,39333],{"id":39332},"what-this-page-helps-with","What this page helps with",[195,39335,39336,39339,39344,39347,39350],{},[198,39337,39338],{},"Understand what a comment is",[198,39340,39341,39342],{},"Write single-line comments with ",[18,39343,39328],{},[198,39345,39346],{},"Understand inline comments",[198,39348,39349],{},"Learn the common \"multi-line comment\" style",[198,39351,39352],{},"Avoid confusing comments with strings",[31,39354,39356],{"id":39355},"what-a-comment-is-in-python","What a comment is in Python",[14,39358,39359],{},"A comment is text that Python ignores when running code.",[14,39361,39362],{},"Comments are useful because they help people reading the code:",[195,39364,39365,39368,39371,39374],{},[198,39366,39367],{},"Explain what a step does",[198,39369,39370],{},"Explain why code was written a certain way",[198,39372,39373],{},"Add a short note before something tricky",[198,39375,39376],{},"Make code easier to come back to later",[14,39378,39379,39380,39382],{},"Comments do ",[2421,39381,2857],{}," change the result of your program.",[14,39384,466],{},[36,39386,39388],{"className":38,"code":39387,"language":40,"meta":41,"style":41},"# Store the user's name\nname = \"Maya\"\n\nprint(name)\n",[18,39389,39390,39395,39407,39411],{"__ignoreMap":41},[45,39391,39392],{"class":47,"line":48},[45,39393,39394],{"class":7624},"# Store the user's name\n",[45,39396,39397,39399,39401,39403,39405],{"class":47,"line":63},[45,39398,1560],{"class":51},[45,39400,56],{"class":55},[45,39402,1565],{"class":99},[45,39404,5737],{"class":103},[45,39406,1571],{"class":99},[45,39408,39409],{"class":47,"line":70},[45,39410,67],{"emptyLinePlaceholder":66},[45,39412,39413,39415,39417,39419],{"class":47,"line":89},[45,39414,2065],{"class":92},[45,39416,96],{"class":85},[45,39418,3096],{"class":2070},[45,39420,109],{"class":85},[14,39422,39423],{},"When Python runs this code, it ignores the comment and only runs the real code.",[31,39425,39427],{"id":39426},"how-to-write-a-single-line-comment","How to write a single-line comment",[14,39429,39430,39431,240],{},"To write a comment in Python, start the line with ",[18,39432,39328],{},[14,39434,39435,39436,39438],{},"Everything after ",[18,39437,39328],{}," on that line is ignored.",[36,39440,39442],{"className":38,"code":39441,"language":40,"meta":41,"style":41},"# This is a comment\nprint(\"Hello\")\n",[18,39443,39444,39449],{"__ignoreMap":41},[45,39445,39446],{"class":47,"line":48},[45,39447,39448],{"class":7624},"# This is a comment\n",[45,39450,39451,39453,39455,39457,39459,39461],{"class":47,"line":63},[45,39452,2065],{"class":92},[45,39454,96],{"class":85},[45,39456,100],{"class":99},[45,39458,3226],{"class":103},[45,39460,100],{"class":99},[45,39462,109],{"class":85},[14,39464,39465],{},"You can also use comments to explain the next step:",[36,39467,39469],{"className":38,"code":39468,"language":40,"meta":41,"style":41},"# Ask the user for their age\nage = input(\"Enter your age: \")\n",[18,39470,39471,39476],{"__ignoreMap":41},[45,39472,39473],{"class":47,"line":48},[45,39474,39475],{"class":7624},"# Ask the user for their age\n",[45,39477,39478,39480,39482,39484,39486,39488,39491,39493],{"class":47,"line":63},[45,39479,52],{"class":51},[45,39481,56],{"class":55},[45,39483,18043],{"class":92},[45,39485,96],{"class":85},[45,39487,100],{"class":99},[45,39489,39490],{"class":103},"Enter your age: ",[45,39492,100],{"class":99},[45,39494,109],{"class":85},[14,39496,39497],{},"Single-line comments are best for short, clear notes.",[14,39499,39500],{},"Good comments are:",[195,39502,39503,39506,39509],{},[198,39504,39505],{},"Short",[198,39507,39508],{},"Specific",[198,39510,39511],{},"Easy to understand",[31,39513,39515],{"id":39514},"inline-comments","Inline comments",[14,39517,39518],{},"An inline comment is written after code on the same line.",[36,39520,39522],{"className":38,"code":39521,"language":40,"meta":41,"style":41},"name = \"Maya\"  # Save the user's name\n",[18,39523,39524],{"__ignoreMap":41},[45,39525,39526,39528,39530,39532,39534,39536],{"class":47,"line":48},[45,39527,1560],{"class":51},[45,39529,56],{"class":55},[45,39531,1565],{"class":99},[45,39533,5737],{"class":103},[45,39535,100],{"class":99},[45,39537,39538],{"class":7624},"  # Save the user's name\n",[14,39540,39541],{},"This can be helpful, but only when the comment adds useful context.",[14,39543,39544],{},"Use inline comments carefully:",[195,39546,39547,39550,39553],{},[198,39548,39549],{},"Good when the reason is not obvious",[198,39551,39552],{},"Bad when they only repeat the code",[198,39554,39555],{},"Too many can make code harder to read",[712,39557,39559],{"id":39558},"good-inline-comment","Good inline comment",[36,39561,39563],{"className":38,"code":39562,"language":40,"meta":41,"style":41},"total = price * 1.2  # Add 20% tax\n",[18,39564,39565],{"__ignoreMap":41},[45,39566,39567,39570,39572,39575,39577,39580],{"class":47,"line":48},[45,39568,39569],{"class":51},"total ",[45,39571,56],{"class":55},[45,39573,39574],{"class":51}," price ",[45,39576,5601],{"class":55},[45,39578,39579],{"class":59}," 1.2",[45,39581,39582],{"class":7624},"  # Add 20% tax\n",[712,39584,39586],{"id":39585},"not-very-helpful","Not very helpful",[36,39588,39590],{"className":38,"code":39589,"language":40,"meta":41,"style":41},"name = \"Maya\"  # Set name to Maya\n",[18,39591,39592],{"__ignoreMap":41},[45,39593,39594,39596,39598,39600,39602,39604],{"class":47,"line":48},[45,39595,1560],{"class":51},[45,39597,56],{"class":55},[45,39599,1565],{"class":99},[45,39601,5737],{"class":103},[45,39603,100],{"class":99},[45,39605,39606],{"class":7624},"  # Set name to Maya\n",[14,39608,39609],{},"The second comment does not add much. The code already makes that clear.",[14,39611,39612,39613,1316,39617,240],{},"If you are still learning basic code structure, this fits well with ",[236,39614,39616],{"href":39615},"\u002Flearn\u002Fpython-syntax-basics-explained\u002F","Python syntax basics",[236,39618,39619],{"href":553},"Python indentation rules",[31,39621,39623],{"id":39622},"multi-line-comments-in-python","Multi-line comments in Python",[14,39625,39626],{},"Python does not have a special multi-line comment syntax.",[14,39628,39629,39630,39632],{},"The normal way to write a comment across several lines is to put ",[18,39631,39328],{}," at the start of each line:",[36,39634,39636],{"className":38,"code":39635,"language":40,"meta":41,"style":41},"# This program asks for a name,\n# stores it in a variable,\n# and then prints a greeting.\nname = input(\"Enter your name: \")\nprint(\"Hello,\", name)\n",[18,39637,39638,39643,39648,39653,39672],{"__ignoreMap":41},[45,39639,39640],{"class":47,"line":48},[45,39641,39642],{"class":7624},"# This program asks for a name,\n",[45,39644,39645],{"class":47,"line":63},[45,39646,39647],{"class":7624},"# stores it in a variable,\n",[45,39649,39650],{"class":47,"line":70},[45,39651,39652],{"class":7624},"# and then prints a greeting.\n",[45,39654,39655,39657,39659,39661,39663,39665,39668,39670],{"class":47,"line":89},[45,39656,1560],{"class":51},[45,39658,56],{"class":55},[45,39660,18043],{"class":92},[45,39662,96],{"class":85},[45,39664,100],{"class":99},[45,39666,39667],{"class":103},"Enter your name: ",[45,39669,100],{"class":99},[45,39671,109],{"class":85},[45,39673,39674,39676,39678,39680,39682,39684,39686,39688],{"class":47,"line":112},[45,39675,2065],{"class":92},[45,39677,96],{"class":85},[45,39679,100],{"class":99},[45,39681,18065],{"class":103},[45,39683,100],{"class":99},[45,39685,3079],{"class":85},[45,39687,3083],{"class":2070},[45,39689,109],{"class":85},[14,39691,39692],{},"This is the standard way to write multi-line comments in Python.",[14,39694,39695],{},"Beginners often see triple quotes used like this:",[36,39697,39699],{"className":38,"code":39698,"language":40,"meta":41,"style":41},"\"\"\"\nThis looks like a multi-line comment,\nbut Python treats it as a string.\n\"\"\"\nprint(\"Hello\")\n",[18,39700,39701,39705,39710,39715,39719],{"__ignoreMap":41},[45,39702,39703],{"class":47,"line":48},[45,39704,39290],{"class":39289},[45,39706,39707],{"class":47,"line":63},[45,39708,39709],{"class":39295},"This looks like a multi-line comment,\n",[45,39711,39712],{"class":47,"line":70},[45,39713,39714],{"class":39295},"but Python treats it as a string.\n",[45,39716,39717],{"class":47,"line":89},[45,39718,39290],{"class":39289},[45,39720,39721,39723,39725,39727,39729,39731],{"class":47,"line":112},[45,39722,2065],{"class":92},[45,39724,96],{"class":85},[45,39726,100],{"class":99},[45,39728,3226],{"class":103},[45,39730,100],{"class":99},[45,39732,109],{"class":85},[14,39734,39735],{},"That style is common, but it is not a real comment.",[31,39737,39739],{"id":39738},"triple-quotes-vs-real-comments","Triple quotes vs real comments",[14,39741,39742],{},"Triple quotes create a multi-line string.",[36,39744,39746],{"className":38,"code":39745,"language":40,"meta":41,"style":41},"text = \"\"\"This is\na multi-line\nstring.\"\"\"\nprint(text)\n",[18,39747,39748,39760,39765,39772],{"__ignoreMap":41},[45,39749,39750,39752,39754,39757],{"class":47,"line":48},[45,39751,10297],{"class":51},[45,39753,56],{"class":55},[45,39755,39756],{"class":99}," \"\"\"",[45,39758,39759],{"class":103},"This is\n",[45,39761,39762],{"class":47,"line":63},[45,39763,39764],{"class":103},"a multi-line\n",[45,39766,39767,39770],{"class":47,"line":70},[45,39768,39769],{"class":103},"string.",[45,39771,39290],{"class":99},[45,39773,39774,39776,39778,39780],{"class":47,"line":89},[45,39775,2065],{"class":92},[45,39777,96],{"class":85},[45,39779,5000],{"class":2070},[45,39781,109],{"class":85},[14,39783,177],{},[36,39785,39787],{"className":38,"code":39786,"language":40,"meta":41,"style":41},"This is\na multi-line\nstring.\n",[18,39788,39789,39797,39807],{"__ignoreMap":41},[45,39790,39791,39794],{"class":47,"line":48},[45,39792,39793],{"class":51},"This ",[45,39795,39796],{"class":55},"is\n",[45,39798,39799,39802,39804],{"class":47,"line":63},[45,39800,39801],{"class":51},"a multi",[45,39803,218],{"class":55},[45,39805,39806],{"class":51},"line\n",[45,39808,39809,39812],{"class":47,"line":70},[45,39810,39811],{"class":51},"string",[45,39813,2708],{"class":85},[14,39815,39816],{},"That means triple-quoted text is different from a real comment.",[712,39818,39820],{"id":39819},"real-comment","Real comment",[36,39822,39824],{"className":38,"code":39823,"language":40,"meta":41,"style":41},"# Python ignores this line\nprint(\"Hello\")\n",[18,39825,39826,39831],{"__ignoreMap":41},[45,39827,39828],{"class":47,"line":48},[45,39829,39830],{"class":7624},"# Python ignores this line\n",[45,39832,39833,39835,39837,39839,39841,39843],{"class":47,"line":63},[45,39834,2065],{"class":92},[45,39836,96],{"class":85},[45,39838,100],{"class":99},[45,39840,3226],{"class":103},[45,39842,100],{"class":99},[45,39844,109],{"class":85},[712,39846,39848],{"id":39847},"triple-quoted-string","Triple-quoted string",[36,39850,39852],{"className":38,"code":39851,"language":40,"meta":41,"style":41},"\"\"\"Python reads this as a string.\"\"\"\nprint(\"Hello\")\n",[18,39853,39854,39864],{"__ignoreMap":41},[45,39855,39856,39859,39862],{"class":47,"line":48},[45,39857,39858],{"class":39289},"\"\"\"",[45,39860,39861],{"class":39295},"Python reads this as a string.",[45,39863,39290],{"class":39289},[45,39865,39866,39868,39870,39872,39874,39876],{"class":47,"line":63},[45,39867,2065],{"class":92},[45,39869,96],{"class":85},[45,39871,100],{"class":99},[45,39873,3226],{"class":103},[45,39875,100],{"class":99},[45,39877,109],{"class":85},[14,39879,39880,39881,39884],{},"In some places, triple-quoted strings are used as ",[2421,39882,39883],{},"docstrings",". A docstring is a special string used to describe a module, function, class, or method.",[14,39886,466],{},[36,39888,39890],{"className":38,"code":39889,"language":40,"meta":41,"style":41},"def greet(name):\n    \"\"\"Return a friendly greeting.\"\"\"\n    return f\"Hello, {name}\"\n",[18,39891,39892,39904,39914],{"__ignoreMap":41},[45,39893,39894,39896,39898,39900,39902],{"class":47,"line":48},[45,39895,2757],{"class":2440},[45,39897,2936],{"class":2455},[45,39899,96],{"class":85},[45,39901,3096],{"class":3082},[45,39903,2464],{"class":85},[45,39905,39906,39909,39912],{"class":47,"line":63},[45,39907,39908],{"class":39289},"    \"\"\"",[45,39910,39911],{"class":39295},"Return a friendly greeting.",[45,39913,39290],{"class":39289},[45,39915,39916,39918,39920,39922,39924,39926,39928],{"class":47,"line":70},[45,39917,4681],{"class":73},[45,39919,15469],{"class":2440},[45,39921,5548],{"class":103},[45,39923,5551],{"class":59},[45,39925,3096],{"class":51},[45,39927,5556],{"class":59},[45,39929,1571],{"class":103},[14,39931,39932],{},"Here, the triple-quoted text is not just a note. It is documentation attached to the function.",[14,39934,39935,39936,39938,39939,240],{},"If you want to learn more about functions, see ",[236,39937,5879],{"href":4579},". If you are not sure what a module is, see ",[236,39940,39942],{"href":39941},"\u002Fglossary\u002Fwhat-is-a-module-in-python\u002F","what is a module in Python",[31,39944,39946],{"id":39945},"when-comments-are-helpful","When comments are helpful",[14,39948,39949],{},"Comments are most useful when they explain something the code does not clearly say on its own.",[14,39951,39952],{},"Helpful comments can:",[195,39954,39955,39962,39965,39968],{},[198,39956,39957,39958,39961],{},"Explain ",[2421,39959,39960],{},"why"," something is done",[198,39963,39964],{},"Mark important steps for beginners",[198,39966,39967],{},"Add a short note before more complex logic",[198,39969,39970],{},"Clarify unusual behavior or edge cases",[14,39972,466],{},[36,39974,39976],{"className":38,"code":39975,"language":40,"meta":41,"style":41},"# Use 1-based numbering because the report format starts at 1\nreport_number = user_index + 1\n",[18,39977,39978,39983],{"__ignoreMap":41},[45,39979,39980],{"class":47,"line":48},[45,39981,39982],{"class":7624},"# Use 1-based numbering because the report format starts at 1\n",[45,39984,39985,39988,39990,39993,39995],{"class":47,"line":63},[45,39986,39987],{"class":51},"report_number ",[45,39989,56],{"class":55},[45,39991,39992],{"class":51}," user_index ",[45,39994,8850],{"class":55},[45,39996,12962],{"class":59},[14,39998,39999],{},"This is useful because the reason may not be obvious just from reading the code.",[14,40001,40002],{},"Another example:",[36,40004,40006],{"className":38,"code":40005,"language":40,"meta":41,"style":41},"# Skip empty lines to avoid counting blank input\nif line != \"\":\n    count += 1\n",[18,40007,40008,40013,40025],{"__ignoreMap":41},[45,40009,40010],{"class":47,"line":48},[45,40011,40012],{"class":7624},"# Skip empty lines to avoid counting blank input\n",[45,40014,40015,40017,40019,40021,40023],{"class":47,"line":63},[45,40016,20],{"class":73},[45,40018,24027],{"class":51},[45,40020,1135],{"class":55},[45,40022,27950],{"class":99},[45,40024,86],{"class":85},[45,40026,40027,40029,40031],{"class":47,"line":70},[45,40028,34021],{"class":51},[45,40030,12992],{"class":55},[45,40032,12962],{"class":59},[31,40034,40036],{"id":40035},"when-comments-are-not-helpful","When comments are not helpful",[14,40038,40039],{},"Comments can also make code worse if they are unnecessary or outdated.",[14,40041,40042],{},"Avoid comments like these:",[195,40044,40045,40048,40051,40054],{},[198,40046,40047],{},"Comments that repeat obvious code",[198,40049,40050],{},"Comments that are no longer true",[198,40052,40053],{},"Very long comments that interrupt reading",[198,40055,40056],{},"Comments used instead of clear variable names",[14,40058,40059],{},"Example of an unhelpful comment:",[36,40061,40063],{"className":38,"code":40062,"language":40,"meta":41,"style":41},"# Add 1 to count\ncount = count + 1\n",[18,40064,40065,40070],{"__ignoreMap":41},[45,40066,40067],{"class":47,"line":48},[45,40068,40069],{"class":7624},"# Add 1 to count\n",[45,40071,40072,40074,40076,40078,40080],{"class":47,"line":63},[45,40073,33984],{"class":51},[45,40075,56],{"class":55},[45,40077,33999],{"class":51},[45,40079,8850],{"class":55},[45,40081,12962],{"class":59},[14,40083,40084],{},"The code already says that.",[14,40086,40087],{},"A better improvement might be a clearer variable name or cleaner code.",[14,40089,40090],{},"You should also avoid using comments to explain confusing code when you could rewrite the code more clearly.",[31,40092,16169],{"id":16168},[14,40094,40095],{},"If you are new to Python, these habits will help:",[195,40097,40098,40101,40104,40107,40110],{},[198,40099,40100],{},"Write comments in plain language",[198,40102,40103],{},"Keep comments short",[198,40105,40106],{},"Put comments close to the code they describe",[198,40108,40109],{},"Update comments when the code changes",[198,40111,40112],{},"Use comments to support readable code, not replace it",[14,40114,40115],{},"A good goal is this:",[195,40117,40118,40121],{},[198,40119,40120],{},"Write code that is clear on its own",[198,40122,40123],{},"Add comments only where they truly help",[14,40125,2115],{},[36,40127,40129],{"className":38,"code":40128,"language":40,"meta":41,"style":41},"# Convert minutes to seconds before saving\nseconds = minutes * 60\n",[18,40130,40131,40136],{"__ignoreMap":41},[45,40132,40133],{"class":47,"line":48},[45,40134,40135],{"class":7624},"# Convert minutes to seconds before saving\n",[45,40137,40138,40141,40143,40146,40148],{"class":47,"line":63},[45,40139,40140],{"class":51},"seconds ",[45,40142,56],{"class":55},[45,40144,40145],{"class":51}," minutes ",[45,40147,5601],{"class":55},[45,40149,40150],{"class":59}," 60\n",[14,40152,40153],{},"This comment gives useful context without being too long.",[14,40155,40156,40157,40161],{},"You may also notice that Python code depends on clean layout and structure. That is one reason ",[236,40158,40160],{"href":40159},"\u002Flearn\u002Fpython-keywords-explained-beginner-guide\u002F","Python keywords"," and indentation rules matter when writing readable code.",[31,40163,5903],{"id":5902},[14,40165,40166],{},"These are common comment-related mistakes beginners make:",[195,40168,40169,40172,40175,40178,40181],{},[198,40170,40171],{},"Using triple quotes and thinking they are always comments",[198,40173,40174],{},"Writing comments that only repeat the code",[198,40176,40177],{},"Leaving old comments after changing the code",[198,40179,40180],{},"Putting too many inline comments on one line",[198,40182,40183],{},"Mixing comments with code in a way that hurts readability",[14,40185,40186],{},"Example of cluttered code:",[36,40188,40190],{"className":38,"code":40189,"language":40,"meta":41,"style":41},"name = \"Maya\"  # user's name  # stored as text  # printed below\nprint(name)  # show name\n",[18,40191,40192,40207],{"__ignoreMap":41},[45,40193,40194,40196,40198,40200,40202,40204],{"class":47,"line":48},[45,40195,1560],{"class":51},[45,40197,56],{"class":55},[45,40199,1565],{"class":99},[45,40201,5737],{"class":103},[45,40203,100],{"class":99},[45,40205,40206],{"class":7624},"  # user's name  # stored as text  # printed below\n",[45,40208,40209,40211,40213,40215,40217],{"class":47,"line":63},[45,40210,2065],{"class":92},[45,40212,96],{"class":85},[45,40214,3096],{"class":2070},[45,40216,9146],{"class":85},[45,40218,40219],{"class":7624},"  # show name\n",[14,40221,40222],{},"This works, but it is harder to read than necessary.",[14,40224,40225],{},"A cleaner version:",[36,40227,40228],{"className":38,"code":39387,"language":40,"meta":41,"style":41},[18,40229,40230,40234,40246,40250],{"__ignoreMap":41},[45,40231,40232],{"class":47,"line":48},[45,40233,39394],{"class":7624},[45,40235,40236,40238,40240,40242,40244],{"class":47,"line":63},[45,40237,1560],{"class":51},[45,40239,56],{"class":55},[45,40241,1565],{"class":99},[45,40243,5737],{"class":103},[45,40245,1571],{"class":99},[45,40247,40248],{"class":47,"line":70},[45,40249,67],{"emptyLinePlaceholder":66},[45,40251,40252,40254,40256,40258],{"class":47,"line":89},[45,40253,2065],{"class":92},[45,40255,96],{"class":85},[45,40257,3096],{"class":2070},[45,40259,109],{"class":85},[14,40261,40262],{},"If you want to test your script while learning, you can run it from the terminal:",[36,40264,40266],{"className":15652,"code":40265,"language":15654,"meta":41,"style":41},"python your_script.py\n",[18,40267,40268],{"__ignoreMap":41},[45,40269,40270,40272],{"class":47,"line":48},[45,40271,40],{"class":2444},[45,40273,16305],{"class":103},[14,40275,40276],{},"You may also see beginners explore Python with:",[36,40278,40280],{"className":38,"code":40279,"language":40,"meta":41,"style":41},"help(print)\ndir(__builtins__)\n",[18,40281,40282,40292],{"__ignoreMap":41},[45,40283,40284,40286,40288,40290],{"class":47,"line":48},[45,40285,2104],{"class":92},[45,40287,96],{"class":85},[45,40289,2065],{"class":92},[45,40291,109],{"class":85},[45,40293,40294,40296,40298,40301],{"class":47,"line":63},[45,40295,4357],{"class":92},[45,40297,96],{"class":85},[45,40299,40300],{"class":2771},"__builtins__",[45,40302,109],{"class":85},[14,40304,40305],{},"These are not comment tools, but they can help you learn how Python works.",[31,40307,2281],{"id":2280},[712,40309,40311],{"id":40310},"how-do-you-write-a-comment-in-python","How do you write a comment in Python?",[14,40313,165,40314,40316],{},[18,40315,39328],{}," followed by your note. Python ignores the rest of that line.",[712,40318,40320],{"id":40319},"does-python-have-multi-line-comments","Does Python have multi-line comments?",[14,40322,40323,40324,40326],{},"Not as a separate syntax. The usual approach is to write ",[18,40325,39328],{}," on each line.",[712,40328,40330],{"id":40329},"are-triple-quotes-comments-in-python","Are triple quotes comments in Python?",[14,40332,40333],{},"No. Triple quotes create a string. They are often used for docstrings.",[712,40335,40337],{"id":40336},"what-is-an-inline-comment","What is an inline comment?",[14,40339,40340],{},"It is a comment written after code on the same line.",[712,40342,40344],{"id":40343},"should-beginners-use-many-comments","Should beginners use many comments?",[14,40346,40347],{},"Use comments when they make code clearer, but do not comment every obvious line.",[31,40349,2342],{"id":2341},[195,40351,40352,40357,40361,40366,40370],{},[198,40353,40354],{},[236,40355,40356],{"href":39615},"Python syntax basics explained",[198,40358,40359],{},[236,40360,554],{"href":553},[198,40362,40363],{},[236,40364,40365],{"href":40159},"Python keywords explained",[198,40367,40368],{},[236,40369,5879],{"href":4579},[198,40371,40372],{},[236,40373,40374],{"href":39941},"What is a module in Python?",[2367,40376,40377],{},"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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--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 .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 .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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":40379},[40380,40381,40382,40383,40384,40388,40389,40393,40394,40395,40396,40397,40404],{"id":33,"depth":63,"text":34},{"id":39332,"depth":63,"text":39333},{"id":39355,"depth":63,"text":39356},{"id":39426,"depth":63,"text":39427},{"id":39514,"depth":63,"text":39515,"children":40385},[40386,40387],{"id":39558,"depth":70,"text":39559},{"id":39585,"depth":70,"text":39586},{"id":39622,"depth":63,"text":39623},{"id":39738,"depth":63,"text":39739,"children":40390},[40391,40392],{"id":39819,"depth":70,"text":39820},{"id":39847,"depth":70,"text":39848},{"id":39945,"depth":63,"text":39946},{"id":40035,"depth":63,"text":40036},{"id":16168,"depth":63,"text":16169},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":40398},[40399,40400,40401,40402,40403],{"id":40310,"depth":70,"text":40311},{"id":40319,"depth":70,"text":40320},{"id":40329,"depth":70,"text":40330},{"id":40336,"depth":70,"text":40337},{"id":40343,"depth":70,"text":40344},{"id":2341,"depth":63,"text":2342},"Master python comments explained single line and multi line in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-comments-explained-single-line-and-multi-line",{"title":39239,"description":40405},"learn\u002Fpython-comments-explained-single-line-and-multi-line","96wn4tVwkYR_m6owMglraghBVIuBWMplTZ7uRQsaANo",{"id":40412,"title":40413,"body":40414,"description":42545,"extension":2399,"meta":42546,"navigation":66,"path":42547,"seo":42548,"stem":42549,"__hash__":42550},"content\u002Flearn\u002Fpython-data-types-overview.md","Python Data Types Overview",{"type":7,"value":40415,"toc":42512},[40416,40419,40426,40429,40433,40439,40536,40538,40585,40588,40590,40604,40608,40614,40616,40635,40638,40649,40652,40656,40659,40662,40681,40683,40691,40694,40721,40723,40732,40735,40758,40763,40766,40777,40784,40788,40791,40795,40800,40803,40818,40867,40871,40876,40878,40895,40950,40957,40961,40966,40969,40985,41042,41045,41077,41083,41087,41092,41102,41105,41156,41162,41166,41169,41173,41178,41231,41233,41257,41260,41268,41273,41277,41282,41329,41332,41336,41341,41380,41382,41403,41406,41410,41418,41494,41496,41505,41510,41514,41520,41593,41596,41607,41613,41672,41674,41686,41692,41696,41699,41731,41733,41858,41861,41878,41882,41885,41902,41904,41970,41972,41996,42002,42059,42062,42093,42098,42109,42111,42114,42138,42140,42197,42199,42232,42234,42237,42254,42257,42337,42339,42371,42374,42388,42392,42395,42398,42411,42414,42416,42420,42423,42427,42439,42443,42462,42468,42477,42481,42484,42486,42509],[10,40417,40413],{"id":40418},"python-data-types-overview",[14,40420,40421,40422,40425],{},"Python has different ",[2421,40423,40424],{},"data types"," for different kinds of values.",[14,40427,40428],{},"This beginner-friendly overview explains what data types are, why they matter, and how to recognize the most common built-in types. You do not need to memorize everything at once. The goal is to understand the basic idea and know which types you will use most often.",[31,40430,40432],{"id":40431},"quick-way-to-check-a-type","Quick way to check a type",[14,40434,165,40435,40438],{},[18,40436,40437],{},"type()"," to quickly check what kind of value you are working with.",[36,40440,40442],{"className":38,"code":40441,"language":40,"meta":41,"style":41},"x = 10\nprint(type(x))\n\ny = \"hello\"\nprint(type(y))\n\nz = [1, 2, 3]\nprint(type(z))\n",[18,40443,40444,40452,40466,40470,40482,40496,40500,40521],{"__ignoreMap":41},[45,40445,40446,40448,40450],{"class":47,"line":48},[45,40447,1169],{"class":51},[45,40449,56],{"class":55},[45,40451,1174],{"class":59},[45,40453,40454,40456,40458,40460,40462,40464],{"class":47,"line":63},[45,40455,2065],{"class":92},[45,40457,96],{"class":85},[45,40459,4340],{"class":2109},[45,40461,96],{"class":85},[45,40463,26156],{"class":2070},[45,40465,4348],{"class":85},[45,40467,40468],{"class":47,"line":70},[45,40469,67],{"emptyLinePlaceholder":66},[45,40471,40472,40474,40476,40478,40480],{"class":47,"line":89},[45,40473,29448],{"class":51},[45,40475,56],{"class":55},[45,40477,1565],{"class":99},[45,40479,18782],{"class":103},[45,40481,1571],{"class":99},[45,40483,40484,40486,40488,40490,40492,40494],{"class":47,"line":112},[45,40485,2065],{"class":92},[45,40487,96],{"class":85},[45,40489,4340],{"class":2109},[45,40491,96],{"class":85},[45,40493,29457],{"class":2070},[45,40495,4348],{"class":85},[45,40497,40498],{"class":47,"line":126},[45,40499,67],{"emptyLinePlaceholder":66},[45,40501,40502,40505,40507,40509,40511,40513,40515,40517,40519],{"class":47,"line":142},[45,40503,40504],{"class":51},"z ",[45,40506,56],{"class":55},[45,40508,8575],{"class":85},[45,40510,8578],{"class":59},[45,40512,3079],{"class":85},[45,40514,3754],{"class":59},[45,40516,3079],{"class":85},[45,40518,777],{"class":59},[45,40520,8594],{"class":85},[45,40522,40523,40525,40527,40529,40531,40534],{"class":47,"line":149},[45,40524,2065],{"class":92},[45,40526,96],{"class":85},[45,40528,4340],{"class":2109},[45,40530,96],{"class":85},[45,40532,40533],{"class":2070},"z",[45,40535,4348],{"class":85},[14,40537,17038],{},[36,40539,40541],{"className":38,"code":40540,"language":40,"meta":41,"style":41},"\u003Cclass 'int'>\n\u003Cclass 'str'>\n\u003Cclass 'list'>\n",[18,40542,40543,40557,40571],{"__ignoreMap":41},[45,40544,40545,40547,40549,40551,40553,40555],{"class":47,"line":48},[45,40546,79],{"class":55},[45,40548,2441],{"class":2440},[45,40550,9199],{"class":99},[45,40552,29738],{"class":103},[45,40554,7901],{"class":99},[45,40556,13484],{"class":55},[45,40558,40559,40561,40563,40565,40567,40569],{"class":47,"line":63},[45,40560,79],{"class":55},[45,40562,2441],{"class":2440},[45,40564,9199],{"class":99},[45,40566,29733],{"class":103},[45,40568,7901],{"class":99},[45,40570,13484],{"class":55},[45,40572,40573,40575,40577,40579,40581,40583],{"class":47,"line":70},[45,40574,79],{"class":55},[45,40576,2441],{"class":2440},[45,40578,9199],{"class":99},[45,40580,14463],{"class":103},[45,40582,7901],{"class":99},[45,40584,13484],{"class":55},[14,40586,40587],{},"This is a useful first step when a program is not behaving the way you expect.",[31,40589,2530],{"id":2529},[195,40591,40592,40595,40598,40601],{},[198,40593,40594],{},"Explain what a data type is",[198,40596,40597],{},"Show the main built-in Python data types beginners use first",[198,40599,40600],{},"Help you choose the right type for simple tasks",[198,40602,40603],{},"Keep details light and point you to focused pages for each type",[31,40605,40607],{"id":40606},"what-a-data-type-means","What a data type means",[14,40609,2419,40610,40613],{},[2421,40611,40612],{},"data type"," tells Python what kind of value something is.",[14,40615,2115],{},[195,40617,40618,40623,40629],{},[198,40619,40620,40622],{},[18,40621,11786],{}," is a number",[198,40624,40625,40628],{},[18,40626,40627],{},"\"hello\""," is text",[198,40630,40631,40634],{},[18,40632,40633],{},"[1, 2, 3]"," is a collection of values",[14,40636,40637],{},"The type matters because it affects what you can do with the value.",[195,40639,40640,40643,40646],{},[198,40641,40642],{},"You can add numbers",[198,40644,40645],{},"You can join strings",[198,40647,40648],{},"You can store many items in a list",[14,40650,40651],{},"Using the right type helps you write correct code and avoid common errors.",[31,40653,40655],{"id":40654},"why-data-types-matter","Why data types matter",[14,40657,40658],{},"Different types behave differently.",[14,40660,40661],{},"For example, this works because both values are numbers:",[36,40663,40665],{"className":38,"code":40664,"language":40,"meta":41,"style":41},"print(2 + 3)\n",[18,40666,40667],{"__ignoreMap":41},[45,40668,40669,40671,40673,40675,40677,40679],{"class":47,"line":48},[45,40670,2065],{"class":92},[45,40672,96],{"class":85},[45,40674,9367],{"class":59},[45,40676,3775],{"class":55},[45,40678,777],{"class":59},[45,40680,109],{"class":85},[14,40682,177],{},[36,40684,40685],{"className":38,"code":11570,"language":40,"meta":41,"style":41},[18,40686,40687],{"__ignoreMap":41},[45,40688,40689],{"class":47,"line":48},[45,40690,11570],{"class":59},[14,40692,40693],{},"But this does something different because both values are strings:",[36,40695,40697],{"className":38,"code":40696,"language":40,"meta":41,"style":41},"print(\"2\" + \"3\")\n",[18,40698,40699],{"__ignoreMap":41},[45,40700,40701,40703,40705,40707,40709,40711,40713,40715,40717,40719],{"class":47,"line":48},[45,40702,2065],{"class":92},[45,40704,96],{"class":85},[45,40706,100],{"class":99},[45,40708,9367],{"class":103},[45,40710,100],{"class":99},[45,40712,3775],{"class":55},[45,40714,1565],{"class":99},[45,40716,11184],{"class":103},[45,40718,100],{"class":99},[45,40720,109],{"class":85},[14,40722,177],{},[36,40724,40726],{"className":38,"code":40725,"language":40,"meta":41,"style":41},"23\n",[18,40727,40728],{"__ignoreMap":41},[45,40729,40730],{"class":47,"line":48},[45,40731,40725],{"class":59},[14,40733,40734],{},"And this causes an error because Python does not automatically combine a string and an integer in this way:",[36,40736,40738],{"className":38,"code":40737,"language":40,"meta":41,"style":41},"print(\"2\" + 3)\n",[18,40739,40740],{"__ignoreMap":41},[45,40741,40742,40744,40746,40748,40750,40752,40754,40756],{"class":47,"line":48},[45,40743,2065],{"class":92},[45,40745,96],{"class":85},[45,40747,100],{"class":99},[45,40749,9367],{"class":103},[45,40751,100],{"class":99},[45,40753,3775],{"class":55},[45,40755,777],{"class":59},[45,40757,109],{"class":85},[14,40759,40760,40761,240],{},"This raises a ",[18,40762,7711],{},[14,40764,40765],{},"Knowing the type of a value helps you:",[195,40767,40768,40771,40774],{},[198,40769,40770],{},"understand what operations are allowed",[198,40772,40773],{},"use functions correctly",[198,40775,40776],{},"debug problems faster",[14,40778,40779,40780,240],{},"If you are new to variables, see ",[236,40781,40783],{"href":40782},"\u002Flearn\u002Fpython-variables-explained-for-beginners\u002F","Python variables explained for beginners",[31,40785,40787],{"id":40786},"main-simple-data-types","Main simple data types",[14,40789,40790],{},"These are the first built-in types most beginners use.",[712,40792,40793],{"id":29738},[18,40794,29738],{},[14,40796,19451,40797,40799],{},[18,40798,29738],{}," is a whole number.",[14,40801,40802],{},"Examples:",[195,40804,40805,40809,40813],{},[198,40806,40807],{},[18,40808,8578],{},[198,40810,40811],{},[18,40812,3372],{},[198,40814,40815],{},[18,40816,40817],{},"-5",[36,40819,40821],{"className":38,"code":40820,"language":40,"meta":41,"style":41},"age = 25\ncount = 10\n\nprint(age)\nprint(type(count))\n",[18,40822,40823,40831,40839,40843,40853],{"__ignoreMap":41},[45,40824,40825,40827,40829],{"class":47,"line":48},[45,40826,52],{"class":51},[45,40828,56],{"class":55},[45,40830,30102],{"class":59},[45,40832,40833,40835,40837],{"class":47,"line":63},[45,40834,33984],{"class":51},[45,40836,56],{"class":55},[45,40838,1174],{"class":59},[45,40840,40841],{"class":47,"line":70},[45,40842,67],{"emptyLinePlaceholder":66},[45,40844,40845,40847,40849,40851],{"class":47,"line":89},[45,40846,2065],{"class":92},[45,40848,96],{"class":85},[45,40850,2082],{"class":2070},[45,40852,109],{"class":85},[45,40854,40855,40857,40859,40861,40863,40865],{"class":47,"line":112},[45,40856,2065],{"class":92},[45,40858,96],{"class":85},[45,40860,4340],{"class":2109},[45,40862,96],{"class":85},[45,40864,34014],{"class":2070},[45,40866,4348],{"class":85},[712,40868,40869],{"id":29743},[18,40870,29743],{},[14,40872,2419,40873,40875],{},[18,40874,29743],{}," is a number with a decimal point.",[14,40877,40802],{},[195,40879,40880,40885,40890],{},[198,40881,40882],{},[18,40883,40884],{},"3.14",[198,40886,40887],{},[18,40888,40889],{},"0.5",[198,40891,40892],{},[18,40893,40894],{},"-2.75",[36,40896,40898],{"className":38,"code":40897,"language":40,"meta":41,"style":41},"price = 19.99\ntemperature = -0.5\n\nprint(price)\nprint(type(temperature))\n",[18,40899,40900,40910,40921,40925,40935],{"__ignoreMap":41},[45,40901,40902,40905,40907],{"class":47,"line":48},[45,40903,40904],{"class":51},"price ",[45,40906,56],{"class":55},[45,40908,40909],{"class":59}," 19.99\n",[45,40911,40912,40914,40916,40918],{"class":47,"line":63},[45,40913,253],{"class":51},[45,40915,56],{"class":55},[45,40917,28089],{"class":55},[45,40919,40920],{"class":59},"0.5\n",[45,40922,40923],{"class":47,"line":70},[45,40924,67],{"emptyLinePlaceholder":66},[45,40926,40927,40929,40931,40933],{"class":47,"line":89},[45,40928,2065],{"class":92},[45,40930,96],{"class":85},[45,40932,19985],{"class":2070},[45,40934,109],{"class":85},[45,40936,40937,40939,40941,40943,40945,40948],{"class":47,"line":112},[45,40938,2065],{"class":92},[45,40940,96],{"class":85},[45,40942,4340],{"class":2109},[45,40944,96],{"class":85},[45,40946,40947],{"class":2070},"temperature",[45,40949,4348],{"class":85},[14,40951,40952,40953,240],{},"For a focused guide, read ",[236,40954,40956],{"href":40955},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex\u002F","Python numbers explained: int and float",[712,40958,40959],{"id":29733},[18,40960,29733],{},[14,40962,2419,40963,40965],{},[18,40964,29733],{}," is a text value.",[14,40967,40968],{},"Strings go inside quotes:",[195,40970,40971,40975,40980],{},[198,40972,40973],{},[18,40974,40627],{},[198,40976,40977],{},[18,40978,40979],{},"'Python'",[198,40981,40982],{},[18,40983,40984],{},"\"123\"",[36,40986,40988],{"className":38,"code":40987,"language":40,"meta":41,"style":41},"name = \"Ava\"\nmessage = \"Hello\"\n\nprint(name)\nprint(type(message))\n",[18,40989,40990,41002,41014,41018,41028],{"__ignoreMap":41},[45,40991,40992,40994,40996,40998,41000],{"class":47,"line":48},[45,40993,1560],{"class":51},[45,40995,56],{"class":55},[45,40997,1565],{"class":99},[45,40999,3162],{"class":103},[45,41001,1571],{"class":99},[45,41003,41004,41006,41008,41010,41012],{"class":47,"line":63},[45,41005,15507],{"class":51},[45,41007,56],{"class":55},[45,41009,1565],{"class":99},[45,41011,3226],{"class":103},[45,41013,1571],{"class":99},[45,41015,41016],{"class":47,"line":70},[45,41017,67],{"emptyLinePlaceholder":66},[45,41019,41020,41022,41024,41026],{"class":47,"line":89},[45,41021,2065],{"class":92},[45,41023,96],{"class":85},[45,41025,3096],{"class":2070},[45,41027,109],{"class":85},[45,41029,41030,41032,41034,41036,41038,41040],{"class":47,"line":112},[45,41031,2065],{"class":92},[45,41033,96],{"class":85},[45,41035,4340],{"class":2109},[45,41037,96],{"class":85},[45,41039,6462],{"class":2070},[45,41041,4348],{"class":85},[14,41043,41044],{},"Even if a string looks like a number, it is still text:",[36,41046,41048],{"className":38,"code":41047,"language":40,"meta":41,"style":41},"value = \"123\"\nprint(type(value))\n",[18,41049,41050,41063],{"__ignoreMap":41},[45,41051,41052,41054,41056,41058,41061],{"class":47,"line":48},[45,41053,1476],{"class":51},[45,41055,56],{"class":55},[45,41057,1565],{"class":99},[45,41059,41060],{"class":103},"123",[45,41062,1571],{"class":99},[45,41064,41065,41067,41069,41071,41073,41075],{"class":47,"line":63},[45,41066,2065],{"class":92},[45,41068,96],{"class":85},[45,41070,4340],{"class":2109},[45,41072,96],{"class":85},[45,41074,12722],{"class":2070},[45,41076,4348],{"class":85},[14,41078,12103,41079,240],{},[236,41080,41082],{"href":41081},"\u002Flearn\u002Fpython-strings-explained-basics-and-examples\u002F","Python strings explained: basics and examples",[712,41084,41085],{"id":2110},[18,41086,2110],{},[14,41088,2419,41089,41091],{},[18,41090,2110],{}," has only two possible values:",[195,41093,41094,41098],{},[198,41095,41096],{},[18,41097,229],{},[198,41099,41100],{},[18,41101,233],{},[14,41103,41104],{},"Booleans are often used in comparisons and conditions.",[36,41106,41108],{"className":38,"code":41107,"language":40,"meta":41,"style":41},"is_logged_in = True\nis_admin = False\n\nprint(is_logged_in)\nprint(type(is_admin))\n",[18,41109,41110,41118,41127,41131,41141],{"__ignoreMap":41},[45,41111,41112,41114,41116],{"class":47,"line":48},[45,41113,33655],{"class":51},[45,41115,56],{"class":55},[45,41117,482],{"class":481},[45,41119,41120,41123,41125],{"class":47,"line":63},[45,41121,41122],{"class":51},"is_admin ",[45,41124,56],{"class":55},[45,41126,33660],{"class":481},[45,41128,41129],{"class":47,"line":70},[45,41130,67],{"emptyLinePlaceholder":66},[45,41132,41133,41135,41137,41139],{"class":47,"line":89},[45,41134,2065],{"class":92},[45,41136,96],{"class":85},[45,41138,33682],{"class":2070},[45,41140,109],{"class":85},[45,41142,41143,41145,41147,41149,41151,41154],{"class":47,"line":112},[45,41144,2065],{"class":92},[45,41146,96],{"class":85},[45,41148,4340],{"class":2109},[45,41150,96],{"class":85},[45,41152,41153],{"class":2070},"is_admin",[45,41155,4348],{"class":85},[14,41157,41158,41159,240],{},"For more detail, see ",[236,41160,41161],{"href":238},"Python booleans explained: True and False",[31,41163,41165],{"id":41164},"main-collection-data-types","Main collection data types",[14,41167,41168],{},"Collection types store multiple values.",[712,41170,41171],{"id":14463},[18,41172,14463],{},[14,41174,2419,41175,41177],{},[18,41176,14463],{}," is an ordered, changeable collection.",[36,41179,41181],{"className":38,"code":41180,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\nnumbers.append(4)\n\nprint(numbers)\n",[18,41182,41183,41203,41217,41221],{"__ignoreMap":41},[45,41184,41185,41187,41189,41191,41193,41195,41197,41199,41201],{"class":47,"line":48},[45,41186,8570],{"class":51},[45,41188,56],{"class":55},[45,41190,8575],{"class":85},[45,41192,8578],{"class":59},[45,41194,3079],{"class":85},[45,41196,3754],{"class":59},[45,41198,3079],{"class":85},[45,41200,777],{"class":59},[45,41202,8594],{"class":85},[45,41204,41205,41207,41209,41211,41213,41215],{"class":47,"line":63},[45,41206,9069],{"class":51},[45,41208,240],{"class":85},[45,41210,21025],{"class":2070},[45,41212,96],{"class":85},[45,41214,3584],{"class":59},[45,41216,109],{"class":85},[45,41218,41219],{"class":47,"line":70},[45,41220,67],{"emptyLinePlaceholder":66},[45,41222,41223,41225,41227,41229],{"class":47,"line":89},[45,41224,2065],{"class":92},[45,41226,96],{"class":85},[45,41228,9069],{"class":2070},[45,41230,109],{"class":85},[14,41232,177],{},[36,41234,41235],{"className":38,"code":14406,"language":40,"meta":41,"style":41},[18,41236,41237],{"__ignoreMap":41},[45,41238,41239,41241,41243,41245,41247,41249,41251,41253,41255],{"class":47,"line":48},[45,41240,9680],{"class":85},[45,41242,8578],{"class":59},[45,41244,3079],{"class":85},[45,41246,3754],{"class":59},[45,41248,3079],{"class":85},[45,41250,777],{"class":59},[45,41252,3079],{"class":85},[45,41254,8591],{"class":59},[45,41256,8594],{"class":85},[14,41258,41259],{},"Lists are useful when:",[195,41261,41262,41265],{},[198,41263,41264],{},"order matters",[198,41266,41267],{},"you want to add, remove, or change items",[14,41269,26005,41270,240],{},[236,41271,41272],{"href":27365},"Python lists explained for beginners",[712,41274,41275],{"id":29752},[18,41276,29752],{},[14,41278,2419,41279,41281],{},[18,41280,29752],{}," is ordered, but it is not usually changed after creation.",[36,41283,41285],{"className":38,"code":41284,"language":40,"meta":41,"style":41},"point = (10, 20)\nprint(point)\nprint(type(point))\n",[18,41286,41287,41304,41315],{"__ignoreMap":41},[45,41288,41289,41292,41294,41296,41298,41300,41302],{"class":47,"line":48},[45,41290,41291],{"class":51},"point ",[45,41293,56],{"class":55},[45,41295,14039],{"class":85},[45,41297,11786],{"class":59},[45,41299,3079],{"class":85},[45,41301,10503],{"class":59},[45,41303,109],{"class":85},[45,41305,41306,41308,41310,41313],{"class":47,"line":63},[45,41307,2065],{"class":92},[45,41309,96],{"class":85},[45,41311,41312],{"class":2070},"point",[45,41314,109],{"class":85},[45,41316,41317,41319,41321,41323,41325,41327],{"class":47,"line":70},[45,41318,2065],{"class":92},[45,41320,96],{"class":85},[45,41322,4340],{"class":2109},[45,41324,96],{"class":85},[45,41326,41312],{"class":2070},[45,41328,4348],{"class":85},[14,41330,41331],{},"Use tuples when you want a fixed group of values.",[712,41333,41334],{"id":29940},[18,41335,29940],{},[14,41337,2419,41338,41340],{},[18,41339,29940],{}," is an unordered collection of unique values.",[36,41342,41344],{"className":38,"code":41343,"language":40,"meta":41,"style":41},"items = {1, 2, 2, 3}\nprint(items)\n",[18,41345,41346,41370],{"__ignoreMap":41},[45,41347,41348,41350,41352,41354,41356,41358,41360,41362,41364,41366,41368],{"class":47,"line":48},[45,41349,23632],{"class":51},[45,41351,56],{"class":55},[45,41353,8604],{"class":85},[45,41355,8578],{"class":59},[45,41357,3079],{"class":85},[45,41359,3754],{"class":59},[45,41361,3079],{"class":85},[45,41363,3754],{"class":59},[45,41365,3079],{"class":85},[45,41367,777],{"class":59},[45,41369,8629],{"class":85},[45,41371,41372,41374,41376,41378],{"class":47,"line":63},[45,41373,2065],{"class":92},[45,41375,96],{"class":85},[45,41377,9516],{"class":2070},[45,41379,109],{"class":85},[14,41381,13456],{},[36,41383,41385],{"className":38,"code":41384,"language":40,"meta":41,"style":41},"{1, 2, 3}\n",[18,41386,41387],{"__ignoreMap":41},[45,41388,41389,41391,41393,41395,41397,41399,41401],{"class":47,"line":48},[45,41390,5551],{"class":85},[45,41392,8578],{"class":59},[45,41394,3079],{"class":85},[45,41396,3754],{"class":59},[45,41398,3079],{"class":85},[45,41400,777],{"class":59},[45,41402,8629],{"class":85},[14,41404,41405],{},"Sets are useful when duplicates should be removed.",[712,41407,41408],{"id":29935},[18,41409,29935],{},[14,41411,2419,41412,41414,41415,240],{},[18,41413,29935],{}," stores ",[2421,41416,41417],{},"key-value pairs",[36,41419,41421],{"className":38,"code":41420,"language":40,"meta":41,"style":41},"student = {\n    \"name\": \"Mia\",\n    \"age\": 14\n}\n\nprint(student[\"name\"])\n",[18,41422,41423,41433,41454,41467,41471,41475],{"__ignoreMap":41},[45,41424,41425,41428,41430],{"class":47,"line":48},[45,41426,41427],{"class":51},"student ",[45,41429,56],{"class":55},[45,41431,41432],{"class":85}," {\n",[45,41434,41435,41438,41440,41442,41444,41446,41449,41451],{"class":47,"line":63},[45,41436,41437],{"class":99},"    \"",[45,41439,3096],{"class":103},[45,41441,100],{"class":99},[45,41443,457],{"class":85},[45,41445,1565],{"class":99},[45,41447,41448],{"class":103},"Mia",[45,41450,100],{"class":99},[45,41452,41453],{"class":85},",\n",[45,41455,41456,41458,41460,41462,41464],{"class":47,"line":70},[45,41457,41437],{"class":99},[45,41459,2082],{"class":103},[45,41461,100],{"class":99},[45,41463,457],{"class":85},[45,41465,41466],{"class":59}," 14\n",[45,41468,41469],{"class":47,"line":89},[45,41470,8629],{"class":85},[45,41472,41473],{"class":47,"line":112},[45,41474,67],{"emptyLinePlaceholder":66},[45,41476,41477,41479,41481,41484,41486,41488,41490,41492],{"class":47,"line":126},[45,41478,2065],{"class":92},[45,41480,96],{"class":85},[45,41482,41483],{"class":2070},"student",[45,41485,9680],{"class":85},[45,41487,100],{"class":99},[45,41489,3096],{"class":103},[45,41491,100],{"class":99},[45,41493,21941],{"class":85},[14,41495,177],{},[36,41497,41499],{"className":38,"code":41498,"language":40,"meta":41,"style":41},"Mia\n",[18,41500,41501],{"__ignoreMap":41},[45,41502,41503],{"class":47,"line":48},[45,41504,41498],{"class":51},[14,41506,41507,41508,240],{},"Dictionaries are useful when you want to look up values by a name or key. See ",[236,41509,8557],{"href":30258},[31,41511,41513],{"id":41512},"how-to-check-a-values-type","How to check a value's type",[14,41515,165,41516,41519],{},[18,41517,41518],{},"type(value)"," to inspect a value.",[36,41521,41523],{"className":38,"code":41522,"language":40,"meta":41,"style":41},"print(type(10))\nprint(type(3.14))\nprint(type(\"hello\"))\nprint(type([1, 2, 3]))\n",[18,41524,41525,41539,41553,41571],{"__ignoreMap":41},[45,41526,41527,41529,41531,41533,41535,41537],{"class":47,"line":48},[45,41528,2065],{"class":92},[45,41530,96],{"class":85},[45,41532,4340],{"class":2109},[45,41534,96],{"class":85},[45,41536,11786],{"class":59},[45,41538,4348],{"class":85},[45,41540,41541,41543,41545,41547,41549,41551],{"class":47,"line":63},[45,41542,2065],{"class":92},[45,41544,96],{"class":85},[45,41546,4340],{"class":2109},[45,41548,96],{"class":85},[45,41550,40884],{"class":59},[45,41552,4348],{"class":85},[45,41554,41555,41557,41559,41561,41563,41565,41567,41569],{"class":47,"line":70},[45,41556,2065],{"class":92},[45,41558,96],{"class":85},[45,41560,4340],{"class":2109},[45,41562,96],{"class":85},[45,41564,100],{"class":99},[45,41566,18782],{"class":103},[45,41568,100],{"class":99},[45,41570,4348],{"class":85},[45,41572,41573,41575,41577,41579,41581,41583,41585,41587,41589,41591],{"class":47,"line":89},[45,41574,2065],{"class":92},[45,41576,96],{"class":85},[45,41578,4340],{"class":2109},[45,41580,34742],{"class":85},[45,41582,8578],{"class":59},[45,41584,3079],{"class":85},[45,41586,3754],{"class":59},[45,41588,3079],{"class":85},[45,41590,777],{"class":59},[45,41592,26913],{"class":85},[14,41594,41595],{},"This is especially useful when:",[195,41597,41598,41601,41604],{},[198,41599,41600],{},"input data is unclear",[198,41602,41603],{},"you are reading data from a file",[198,41605,41606],{},"an error suggests the wrong type is being used",[14,41608,13688,41609,41612],{},[18,41610,41611],{},"isinstance()"," when you want to check whether a value is a specific type:",[36,41614,41616],{"className":38,"code":41615,"language":40,"meta":41,"style":41},"value = \"hello\"\n\nprint(isinstance(value, str))\nprint(isinstance(value, int))\n",[18,41617,41618,41630,41634,41653],{"__ignoreMap":41},[45,41619,41620,41622,41624,41626,41628],{"class":47,"line":48},[45,41621,1476],{"class":51},[45,41623,56],{"class":55},[45,41625,1565],{"class":99},[45,41627,18782],{"class":103},[45,41629,1571],{"class":99},[45,41631,41632],{"class":47,"line":63},[45,41633,67],{"emptyLinePlaceholder":66},[45,41635,41636,41638,41640,41643,41645,41647,41649,41651],{"class":47,"line":70},[45,41637,2065],{"class":92},[45,41639,96],{"class":85},[45,41641,41642],{"class":92},"isinstance",[45,41644,96],{"class":85},[45,41646,12722],{"class":2070},[45,41648,3079],{"class":85},[45,41650,38516],{"class":2109},[45,41652,4348],{"class":85},[45,41654,41655,41657,41659,41661,41663,41665,41667,41670],{"class":47,"line":89},[45,41656,2065],{"class":92},[45,41658,96],{"class":85},[45,41660,41642],{"class":92},[45,41662,96],{"class":85},[45,41664,12722],{"class":2070},[45,41666,3079],{"class":85},[45,41668,41669],{"class":2109}," int",[45,41671,4348],{"class":85},[14,41673,177],{},[36,41675,41676],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,41677,41678,41682],{"__ignoreMap":41},[45,41679,41680],{"class":47,"line":48},[45,41681,2270],{"class":481},[45,41683,41684],{"class":47,"line":63},[45,41685,2265],{"class":481},[14,41687,41158,41688,240],{},[236,41689,41691],{"href":41690},"\u002Freference\u002Fpython-type-function-explained\u002F","Python type() function explained",[31,41693,41695],{"id":41694},"choosing-the-right-type","Choosing the right type",[14,41697,41698],{},"Use these simple rules as a starting point:",[195,41700,41701,41706,41711,41716,41721,41726],{},[198,41702,165,41703,41705],{},[18,41704,29738],{}," for counting and whole-number values",[198,41707,165,41708,41710],{},[18,41709,29743],{}," for decimal values",[198,41712,165,41713,41715],{},[18,41714,29733],{}," for names, messages, and text input",[198,41717,165,41718,41720],{},[18,41719,14463],{}," for ordered groups you need to change",[198,41722,165,41723,41725],{},[18,41724,29935],{}," when values should be looked up by key",[198,41727,165,41728,41730],{},[18,41729,29940],{}," when you want unique values only",[14,41732,40802],{},[36,41734,41736],{"className":38,"code":41735,"language":40,"meta":41,"style":41},"count = 5                  # int\nprice = 8.99               # float\nusername = \"sam\"           # str\nscores = [80, 90, 75]      # list\nuser = {\"name\": \"Sam\"}     # dict\ntags = {\"python\", \"code\"}  # set\n",[18,41737,41738,41749,41761,41777,41803,41830],{"__ignoreMap":41},[45,41739,41740,41742,41744,41746],{"class":47,"line":48},[45,41741,33984],{"class":51},[45,41743,56],{"class":55},[45,41745,1222],{"class":59},[45,41747,41748],{"class":7624},"                  # int\n",[45,41750,41751,41753,41755,41758],{"class":47,"line":63},[45,41752,40904],{"class":51},[45,41754,56],{"class":55},[45,41756,41757],{"class":59}," 8.99",[45,41759,41760],{"class":7624},"               # float\n",[45,41762,41763,41766,41768,41770,41772,41774],{"class":47,"line":70},[45,41764,41765],{"class":51},"username ",[45,41767,56],{"class":55},[45,41769,1565],{"class":99},[45,41771,31741],{"class":103},[45,41773,100],{"class":99},[45,41775,41776],{"class":7624},"           # str\n",[45,41778,41779,41782,41784,41786,41789,41791,41793,41795,41798,41800],{"class":47,"line":89},[45,41780,41781],{"class":51},"scores ",[45,41783,56],{"class":55},[45,41785,8575],{"class":85},[45,41787,41788],{"class":59},"80",[45,41790,3079],{"class":85},[45,41792,603],{"class":59},[45,41794,3079],{"class":85},[45,41796,41797],{"class":59}," 75",[45,41799,9685],{"class":85},[45,41801,41802],{"class":7624},"      # list\n",[45,41804,41805,41807,41809,41811,41813,41815,41817,41819,41821,41823,41825,41827],{"class":47,"line":112},[45,41806,30062],{"class":51},[45,41808,56],{"class":55},[45,41810,8604],{"class":85},[45,41812,100],{"class":99},[45,41814,3096],{"class":103},[45,41816,100],{"class":99},[45,41818,457],{"class":85},[45,41820,1565],{"class":99},[45,41822,1568],{"class":103},[45,41824,100],{"class":99},[45,41826,5556],{"class":85},[45,41828,41829],{"class":7624},"     # dict\n",[45,41831,41832,41835,41837,41839,41841,41843,41845,41847,41849,41851,41853,41855],{"class":47,"line":126},[45,41833,41834],{"class":51},"tags ",[45,41836,56],{"class":55},[45,41838,8604],{"class":85},[45,41840,100],{"class":99},[45,41842,40],{"class":103},[45,41844,100],{"class":99},[45,41846,3079],{"class":85},[45,41848,1565],{"class":99},[45,41850,18],{"class":103},[45,41852,100],{"class":99},[45,41854,5556],{"class":85},[45,41856,41857],{"class":7624},"  # set\n",[14,41859,41860],{},"If you are unsure, ask:",[195,41862,41863,41866,41869,41872,41875],{},[198,41864,41865],{},"Is this a number or text?",[198,41867,41868],{},"Do I need one value or many values?",[198,41870,41871],{},"Does order matter?",[198,41873,41874],{},"Do I need to change it later?",[198,41876,41877],{},"Do I need names for each value?",[31,41879,41881],{"id":41880},"type-conversion-basics","Type conversion basics",[14,41883,41884],{},"Python can convert values from one type to another with functions such as:",[195,41886,41887,41892,41897],{},[198,41888,41889],{},[18,41890,41891],{},"int()",[198,41893,41894],{},[18,41895,41896],{},"float()",[198,41898,41899],{},[18,41900,41901],{},"str()",[14,41903,466],{},[36,41905,41907],{"className":38,"code":41906,"language":40,"meta":41,"style":41},"text_number = \"42\"\n\nnumber = int(text_number)\n\nprint(number)\nprint(type(number))\n",[18,41908,41909,41923,41927,41942,41946,41956],{"__ignoreMap":41},[45,41910,41911,41914,41916,41918,41921],{"class":47,"line":48},[45,41912,41913],{"class":51},"text_number ",[45,41915,56],{"class":55},[45,41917,1565],{"class":99},[45,41919,41920],{"class":103},"42",[45,41922,1571],{"class":99},[45,41924,41925],{"class":47,"line":63},[45,41926,67],{"emptyLinePlaceholder":66},[45,41928,41929,41931,41933,41935,41937,41940],{"class":47,"line":70},[45,41930,728],{"class":51},[45,41932,56],{"class":55},[45,41934,41669],{"class":2109},[45,41936,96],{"class":85},[45,41938,41939],{"class":2070},"text_number",[45,41941,109],{"class":85},[45,41943,41944],{"class":47,"line":89},[45,41945,67],{"emptyLinePlaceholder":66},[45,41947,41948,41950,41952,41954],{"class":47,"line":112},[45,41949,2065],{"class":92},[45,41951,96],{"class":85},[45,41953,13546],{"class":2070},[45,41955,109],{"class":85},[45,41957,41958,41960,41962,41964,41966,41968],{"class":47,"line":126},[45,41959,2065],{"class":92},[45,41961,96],{"class":85},[45,41963,4340],{"class":2109},[45,41965,96],{"class":85},[45,41967,13546],{"class":2070},[45,41969,4348],{"class":85},[14,41971,177],{},[36,41973,41975],{"className":38,"code":41974,"language":40,"meta":41,"style":41},"42\n\u003Cclass 'int'>\n",[18,41976,41977,41982],{"__ignoreMap":41},[45,41978,41979],{"class":47,"line":48},[45,41980,41981],{"class":59},"42\n",[45,41983,41984,41986,41988,41990,41992,41994],{"class":47,"line":63},[45,41985,79],{"class":55},[45,41987,2441],{"class":2440},[45,41989,9199],{"class":99},[45,41991,29738],{"class":103},[45,41993,7901],{"class":99},[45,41995,13484],{"class":55},[14,41997,41998,41999,42001],{},"This is very common when working with user input, because ",[18,42000,18020],{}," returns a string by default.",[36,42003,42005],{"className":38,"code":42004,"language":40,"meta":41,"style":41},"age_text = input(\"Enter your age: \")\nage = int(age_text)\n\nprint(age + 1)\n",[18,42006,42007,42026,42041,42045],{"__ignoreMap":41},[45,42008,42009,42012,42014,42016,42018,42020,42022,42024],{"class":47,"line":48},[45,42010,42011],{"class":51},"age_text ",[45,42013,56],{"class":55},[45,42015,18043],{"class":92},[45,42017,96],{"class":85},[45,42019,100],{"class":99},[45,42021,39490],{"class":103},[45,42023,100],{"class":99},[45,42025,109],{"class":85},[45,42027,42028,42030,42032,42034,42036,42039],{"class":47,"line":63},[45,42029,52],{"class":51},[45,42031,56],{"class":55},[45,42033,41669],{"class":2109},[45,42035,96],{"class":85},[45,42037,42038],{"class":2070},"age_text",[45,42040,109],{"class":85},[45,42042,42043],{"class":47,"line":70},[45,42044,67],{"emptyLinePlaceholder":66},[45,42046,42047,42049,42051,42053,42055,42057],{"class":47,"line":89},[45,42048,2065],{"class":92},[45,42050,96],{"class":85},[45,42052,52],{"class":2070},[45,42054,8850],{"class":55},[45,42056,9030],{"class":59},[45,42058,109],{"class":85},[14,42060,42061],{},"Be careful: not every string can be converted to a number.",[36,42063,42065],{"className":38,"code":42064,"language":40,"meta":41,"style":41},"value = \"hello\"\nnumber = int(value)\n",[18,42066,42067,42079],{"__ignoreMap":41},[45,42068,42069,42071,42073,42075,42077],{"class":47,"line":48},[45,42070,1476],{"class":51},[45,42072,56],{"class":55},[45,42074,1565],{"class":99},[45,42076,18782],{"class":103},[45,42078,1571],{"class":99},[45,42080,42081,42083,42085,42087,42089,42091],{"class":47,"line":63},[45,42082,728],{"class":51},[45,42084,56],{"class":55},[45,42086,41669],{"class":2109},[45,42088,96],{"class":85},[45,42090,12722],{"class":2070},[45,42092,109],{"class":85},[14,42094,40760,42095,240],{},[18,42096,42097],{},"ValueError",[14,42099,42100,42101,230,42105,240],{},"If you want to practice this next, read ",[236,42102,42104],{"href":42103},"\u002Flearn\u002Ftype-conversion-in-python-casting-between-types\u002F","type conversion in Python",[236,42106,42108],{"href":42107},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python\u002F","how to convert string to int in Python",[31,42110,5795],{"id":5794},[14,42112,42113],{},"Here are some common things that confuse new Python learners.",[195,42115,42116,42123,42128,42135],{},[198,42117,42118,42119,230,42121],{},"Numbers in quotes are strings, not ",[18,42120,29738],{},[18,42122,29743],{},[198,42124,42125,42127],{},[18,42126,18020],{}," returns a string by default",[198,42129,42130,1316,42132,42134],{},[18,42131,229],{},[18,42133,233],{}," are booleans, not strings",[198,42136,42137],{},"Lists, tuples, sets, and dictionaries all store groups of values, but they behave differently",[14,42139,466],{},[36,42141,42143],{"className":38,"code":42142,"language":40,"meta":41,"style":41},"a = \"5\"\nb = 5\n\nprint(type(a))\nprint(type(b))\n",[18,42144,42145,42157,42165,42169,42183],{"__ignoreMap":41},[45,42146,42147,42149,42151,42153,42155],{"class":47,"line":48},[45,42148,11541],{"class":51},[45,42150,56],{"class":55},[45,42152,1565],{"class":99},[45,42154,3749],{"class":103},[45,42156,1571],{"class":99},[45,42158,42159,42161,42163],{"class":47,"line":63},[45,42160,19315],{"class":51},[45,42162,56],{"class":55},[45,42164,733],{"class":59},[45,42166,42167],{"class":47,"line":70},[45,42168,67],{"emptyLinePlaceholder":66},[45,42170,42171,42173,42175,42177,42179,42181],{"class":47,"line":89},[45,42172,2065],{"class":92},[45,42174,96],{"class":85},[45,42176,4340],{"class":2109},[45,42178,96],{"class":85},[45,42180,236],{"class":2070},[45,42182,4348],{"class":85},[45,42184,42185,42187,42189,42191,42193,42195],{"class":47,"line":112},[45,42186,2065],{"class":92},[45,42188,96],{"class":85},[45,42190,4340],{"class":2109},[45,42192,96],{"class":85},[45,42194,10384],{"class":2070},[45,42196,4348],{"class":85},[14,42198,177],{},[36,42200,42202],{"className":38,"code":42201,"language":40,"meta":41,"style":41},"\u003Cclass 'str'>\n\u003Cclass 'int'>\n",[18,42203,42204,42218],{"__ignoreMap":41},[45,42205,42206,42208,42210,42212,42214,42216],{"class":47,"line":48},[45,42207,79],{"class":55},[45,42209,2441],{"class":2440},[45,42211,9199],{"class":99},[45,42213,29733],{"class":103},[45,42215,7901],{"class":99},[45,42217,13484],{"class":55},[45,42219,42220,42222,42224,42226,42228,42230],{"class":47,"line":63},[45,42221,79],{"class":55},[45,42223,2441],{"class":2440},[45,42225,9199],{"class":99},[45,42227,29738],{"class":103},[45,42229,7901],{"class":99},[45,42231,13484],{"class":55},[31,42233,5903],{"id":5902},[14,42235,42236],{},"These problems often happen when learning data types:",[195,42238,42239,42242,42245,42248,42251],{},[198,42240,42241],{},"Treating user input as a number without converting it",[198,42243,42244],{},"Putting numbers in quotes and expecting math to work",[198,42246,42247],{},"Using the wrong collection type for the task",[198,42249,42250],{},"Confusing list, tuple, set, and dictionary behavior",[198,42252,42253],{},"Trying to combine values of different types directly",[14,42255,42256],{},"Helpful debugging steps:",[36,42258,42260],{"className":38,"code":42259,"language":40,"meta":41,"style":41},"print(type(value))\nprint(value)\nprint(repr(value))\nprint(isinstance(value, int))\nprint(isinstance(value, str))\n",[18,42261,42262,42276,42286,42301,42319],{"__ignoreMap":41},[45,42263,42264,42266,42268,42270,42272,42274],{"class":47,"line":48},[45,42265,2065],{"class":92},[45,42267,96],{"class":85},[45,42269,4340],{"class":2109},[45,42271,96],{"class":85},[45,42273,12722],{"class":2070},[45,42275,4348],{"class":85},[45,42277,42278,42280,42282,42284],{"class":47,"line":63},[45,42279,2065],{"class":92},[45,42281,96],{"class":85},[45,42283,12722],{"class":2070},[45,42285,109],{"class":85},[45,42287,42288,42290,42292,42295,42297,42299],{"class":47,"line":70},[45,42289,2065],{"class":92},[45,42291,96],{"class":85},[45,42293,42294],{"class":92},"repr",[45,42296,96],{"class":85},[45,42298,12722],{"class":2070},[45,42300,4348],{"class":85},[45,42302,42303,42305,42307,42309,42311,42313,42315,42317],{"class":47,"line":89},[45,42304,2065],{"class":92},[45,42306,96],{"class":85},[45,42308,41642],{"class":92},[45,42310,96],{"class":85},[45,42312,12722],{"class":2070},[45,42314,3079],{"class":85},[45,42316,41669],{"class":2109},[45,42318,4348],{"class":85},[45,42320,42321,42323,42325,42327,42329,42331,42333,42335],{"class":47,"line":112},[45,42322,2065],{"class":92},[45,42324,96],{"class":85},[45,42326,41642],{"class":92},[45,42328,96],{"class":85},[45,42330,12722],{"class":2070},[45,42332,3079],{"class":85},[45,42334,38516],{"class":2109},[45,42336,4348],{"class":85},[14,42338,33334],{},[195,42340,42341,42347,42353,42359,42365],{},[198,42342,42343,42346],{},[18,42344,42345],{},"print(type(value))"," shows the type",[198,42348,42349,42352],{},[18,42350,42351],{},"print(value)"," shows the normal output",[198,42354,42355,42358],{},[18,42356,42357],{},"print(repr(value))"," shows a more exact representation, which is useful for spotting quotes and special characters",[198,42360,42361,42364],{},[18,42362,42363],{},"isinstance(value, int)"," checks whether the value is an integer",[198,42366,42367,42370],{},[18,42368,42369],{},"isinstance(value, str)"," checks whether the value is a string",[14,42372,42373],{},"If you run into conversion problems, these pages may help:",[195,42375,42376,42382],{},[198,42377,42378],{},[236,42379,42381],{"href":42380},"\u002Ferrors\u002Ftypeerror-can-only-concatenate-str-not-int-to-str-fix\u002F","TypeError: can only concatenate str not int to str",[198,42383,42384],{},[236,42385,42387],{"href":42386},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","ValueError: invalid literal for int() with base 10",[31,42389,42391],{"id":42390},"good-next-steps","Good next steps",[14,42393,42394],{},"After this overview, the best next step is to learn one type at a time.",[14,42396,42397],{},"A good order is:",[346,42399,42400,42402,42405,42408],{},[198,42401,23721],{},[198,42403,42404],{},"Numbers",[198,42406,42407],{},"Lists",[198,42409,42410],{},"Type conversion",[14,42412,42413],{},"That gives you the basics needed for most beginner Python programs.",[31,42415,2281],{"id":2280},[712,42417,42419],{"id":42418},"what-is-a-data-type-in-python","What is a data type in Python?",[14,42421,42422],{},"A data type is the kind of value a variable holds, such as a number, string, list, or dictionary.",[712,42424,42426],{"id":42425},"how-do-i-check-a-data-type-in-python","How do I check a data type in Python?",[14,42428,42429,42430,42432,42433,230,42436,240],{},"Use the ",[18,42431,40437],{}," function, such as ",[18,42434,42435],{},"type(10)",[18,42437,42438],{},"type(\"hello\")",[712,42440,42442],{"id":42441},"what-are-the-main-python-data-types-for-beginners","What are the main Python data types for beginners?",[14,42444,42445,42446,21,42448,21,42450,21,42452,21,42454,21,42456,21,42458,25,42460,240],{},"The most important ones are ",[18,42447,29738],{},[18,42449,29743],{},[18,42451,29733],{},[18,42453,2110],{},[18,42455,14463],{},[18,42457,29752],{},[18,42459,29940],{},[18,42461,29935],{},[712,42463,350,42465,42467],{"id":42464},"is-input-a-number-in-python",[18,42466,18020],{}," a number in Python?",[14,42469,2302,42470,42472,42473,230,42475,240],{},[18,42471,18020],{}," returns a string unless you convert it with ",[18,42474,41891],{},[18,42476,41896],{},[712,42478,42480],{"id":42479},"what-is-the-difference-between-list-and-tuple","What is the difference between list and tuple?",[14,42482,42483],{},"A list can be changed after creation. A tuple cannot be changed in the usual way.",[31,42485,2342],{"id":2341},[195,42487,42488,42492,42496,42500,42505],{},[198,42489,42490],{},[236,42491,40783],{"href":40782},[198,42493,42494],{},[236,42495,41082],{"href":41081},[198,42497,42498],{},[236,42499,41272],{"href":27365},[198,42501,42502],{},[236,42503,42504],{"href":42103},"Type conversion in Python",[198,42506,42507],{},[236,42508,41691],{"href":41690},[2367,42510,42511],{},"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 .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":41,"searchDepth":63,"depth":63,"links":42513},[42514,42515,42516,42517,42518,42524,42530,42531,42532,42533,42534,42535,42536,42544],{"id":40431,"depth":63,"text":40432},{"id":2529,"depth":63,"text":2530},{"id":40606,"depth":63,"text":40607},{"id":40654,"depth":63,"text":40655},{"id":40786,"depth":63,"text":40787,"children":42519},[42520,42521,42522,42523],{"id":29738,"depth":70,"text":29738},{"id":29743,"depth":70,"text":29743},{"id":29733,"depth":70,"text":29733},{"id":2110,"depth":70,"text":2110},{"id":41164,"depth":63,"text":41165,"children":42525},[42526,42527,42528,42529],{"id":14463,"depth":70,"text":14463},{"id":29752,"depth":70,"text":29752},{"id":29940,"depth":70,"text":29940},{"id":29935,"depth":70,"text":29935},{"id":41512,"depth":63,"text":41513},{"id":41694,"depth":63,"text":41695},{"id":41880,"depth":63,"text":41881},{"id":5794,"depth":63,"text":5795},{"id":5902,"depth":63,"text":5903},{"id":42390,"depth":63,"text":42391},{"id":2280,"depth":63,"text":2281,"children":42537},[42538,42539,42540,42541,42543],{"id":42418,"depth":70,"text":42419},{"id":42425,"depth":70,"text":42426},{"id":42441,"depth":70,"text":42442},{"id":42464,"depth":70,"text":42542},"Is input() a number in Python?",{"id":42479,"depth":70,"text":42480},{"id":2341,"depth":63,"text":2342},"Master python data types overview in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-data-types-overview",{"title":40413,"description":42545},"learn\u002Fpython-data-types-overview","KbkurlyY48Ep9-o6F9QXdQPwFlygCOmREpPbTT9tSFY",{"id":42552,"title":42553,"body":42554,"description":45792,"extension":2399,"meta":45793,"navigation":66,"path":8556,"seo":45794,"stem":45795,"__hash__":45796},"content\u002Flearn\u002Fpython-dictionaries-explained.md","Python Dictionaries Explained",{"type":7,"value":42555,"toc":45747},[42556,42559,42565,42575,42577,42618,42621,42640,42647,42649,42652,42797,42799,42825,42829,42832,42848,42850,42891,42905,42909,42915,42918,42935,42937,43008,43011,43015,43020,43071,43073,43110,43114,43137,43139,43148,43154,43159,43205,43207,43243,43250,43254,43257,43320,43322,43330,43336,43341,43431,43433,43446,43449,43509,43522,43526,43529,43606,43608,43661,43664,43737,43739,43776,43785,43789,43795,43905,43907,43949,43955,44026,44028,44053,44059,44125,44127,44135,44138,44176,44183,44187,44190,44194,44277,44281,44368,44372,44375,44470,44472,44491,44516,44520,44523,44545,44548,44551,44607,44609,44634,44637,44640,44649,44652,44655,44658,44796,44799,44802,44868,44872,44875,44902,44904,45046,45048,45181,45185,45188,45192,45199,45262,45269,45273,45280,45352,45356,45359,45414,45416,45441,45445,45448,45504,45506,45547,45554,45559,45561,45564,45656,45659,45673,45675,45679,45682,45686,45689,45693,45696,45700,45703,45707,45712,45714,45744],[10,42557,42553],{"id":42558},"python-dictionaries-explained",[14,42560,42561,42562,42564],{},"A Python dictionary stores data as ",[2421,42563,41417],{},". It is one of the most useful built-in data types in Python.",[14,42566,42567,42568,21,42570,15994,42572,42574],{},"Dictionaries are great when your data has labels such as ",[18,42569,3096],{},[18,42571,2082],{},[18,42573,19985],{},". Instead of finding a value by position, you look it up by its key.",[14,42576,2115],{},[36,42578,42580],{"className":38,"code":42579,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\n",[18,42581,42582],{"__ignoreMap":41},[45,42583,42584,42586,42588,42590,42592,42594,42596,42598,42600,42602,42604,42606,42608,42610,42612,42614,42616],{"class":47,"line":48},[45,42585,41427],{"class":51},[45,42587,56],{"class":55},[45,42589,8604],{"class":85},[45,42591,100],{"class":99},[45,42593,3096],{"class":103},[45,42595,100],{"class":99},[45,42597,457],{"class":85},[45,42599,1565],{"class":99},[45,42601,10580],{"class":103},[45,42603,100],{"class":99},[45,42605,3079],{"class":85},[45,42607,1565],{"class":99},[45,42609,2082],{"class":103},[45,42611,100],{"class":99},[45,42613,457],{"class":85},[45,42615,10503],{"class":59},[45,42617,8629],{"class":85},[14,42619,42620],{},"In this dictionary:",[195,42622,42623,42633],{},[198,42624,42625,42628,42629,42632],{},[18,42626,42627],{},"\"name\""," is a key, and ",[18,42630,42631],{},"\"Ana\""," is its value",[198,42634,42635,42628,42638,42632],{},[18,42636,42637],{},"\"age\"",[18,42639,3372],{},[14,42641,42642,42643,240],{},"If you are completely new to the term, see ",[236,42644,42646],{"href":42645},"\u002Fglossary\u002Fwhat-is-a-dictionary-in-python\u002F","what a dictionary in Python is",[31,42648,34],{"id":33},[14,42650,42651],{},"Use this example to see the most common dictionary tasks: create, read, update, add, and loop.",[36,42653,42655],{"className":38,"code":42654,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\n\nprint(student[\"name\"])\nstudent[\"age\"] = 21\nstudent[\"city\"] = \"Madrid\"\n\nfor key, value in student.items():\n    print(key, value)\n",[18,42656,42657,42693,42697,42715,42734,42757,42761,42782],{"__ignoreMap":41},[45,42658,42659,42661,42663,42665,42667,42669,42671,42673,42675,42677,42679,42681,42683,42685,42687,42689,42691],{"class":47,"line":48},[45,42660,41427],{"class":51},[45,42662,56],{"class":55},[45,42664,8604],{"class":85},[45,42666,100],{"class":99},[45,42668,3096],{"class":103},[45,42670,100],{"class":99},[45,42672,457],{"class":85},[45,42674,1565],{"class":99},[45,42676,10580],{"class":103},[45,42678,100],{"class":99},[45,42680,3079],{"class":85},[45,42682,1565],{"class":99},[45,42684,2082],{"class":103},[45,42686,100],{"class":99},[45,42688,457],{"class":85},[45,42690,10503],{"class":59},[45,42692,8629],{"class":85},[45,42694,42695],{"class":47,"line":63},[45,42696,67],{"emptyLinePlaceholder":66},[45,42698,42699,42701,42703,42705,42707,42709,42711,42713],{"class":47,"line":70},[45,42700,2065],{"class":92},[45,42702,96],{"class":85},[45,42704,41483],{"class":2070},[45,42706,9680],{"class":85},[45,42708,100],{"class":99},[45,42710,3096],{"class":103},[45,42712,100],{"class":99},[45,42714,21941],{"class":85},[45,42716,42717,42719,42721,42723,42725,42727,42729,42731],{"class":47,"line":89},[45,42718,41483],{"class":51},[45,42720,9680],{"class":85},[45,42722,100],{"class":99},[45,42724,2082],{"class":103},[45,42726,100],{"class":99},[45,42728,9685],{"class":85},[45,42730,3099],{"class":55},[45,42732,42733],{"class":59}," 21\n",[45,42735,42736,42738,42740,42742,42744,42746,42748,42750,42752,42755],{"class":47,"line":112},[45,42737,41483],{"class":51},[45,42739,9680],{"class":85},[45,42741,100],{"class":99},[45,42743,10562],{"class":103},[45,42745,100],{"class":99},[45,42747,9685],{"class":85},[45,42749,3099],{"class":55},[45,42751,1565],{"class":99},[45,42753,42754],{"class":103},"Madrid",[45,42756,1571],{"class":99},[45,42758,42759],{"class":47,"line":126},[45,42760,67],{"emptyLinePlaceholder":66},[45,42762,42763,42765,42767,42769,42771,42773,42776,42778,42780],{"class":47,"line":142},[45,42764,7895],{"class":73},[45,42766,9502],{"class":51},[45,42768,3079],{"class":85},[45,42770,1492],{"class":51},[45,42772,8623],{"class":73},[45,42774,42775],{"class":51}," student",[45,42777,240],{"class":85},[45,42779,9516],{"class":2070},[45,42781,3828],{"class":85},[45,42783,42784,42786,42788,42790,42792,42795],{"class":47,"line":149},[45,42785,93],{"class":92},[45,42787,96],{"class":85},[45,42789,9493],{"class":2070},[45,42791,3079],{"class":85},[45,42793,42794],{"class":2070}," value",[45,42796,109],{"class":85},[14,42798,17038],{},[36,42800,42802],{"className":38,"code":42801,"language":40,"meta":41,"style":41},"Ana\nname Ana\nage 21\ncity Madrid\n",[18,42803,42804,42809,42814,42820],{"__ignoreMap":41},[45,42805,42806],{"class":47,"line":48},[45,42807,42808],{"class":51},"Ana\n",[45,42810,42811],{"class":47,"line":63},[45,42812,42813],{"class":51},"name Ana\n",[45,42815,42816,42818],{"class":47,"line":70},[45,42817,52],{"class":51},[45,42819,3441],{"class":59},[45,42821,42822],{"class":47,"line":89},[45,42823,42824],{"class":51},"city Madrid\n",[31,42826,42828],{"id":42827},"what-a-dictionary-is","What a dictionary is",[14,42830,42831],{},"A dictionary:",[195,42833,42834,42837,42840,42843],{},[198,42835,42836],{},"Stores data as key-value pairs",[198,42838,42839],{},"Uses each key to look up a value",[198,42841,42842],{},"Works well for labeled data",[198,42844,42845,42846],{},"Is written with curly braces ",[18,42847,21683],{},[14,42849,466],{},[36,42851,42853],{"className":38,"code":42852,"language":40,"meta":41,"style":41},"person = {\"name\": \"Ana\", \"age\": 20}\n",[18,42854,42855],{"__ignoreMap":41},[45,42856,42857,42859,42861,42863,42865,42867,42869,42871,42873,42875,42877,42879,42881,42883,42885,42887,42889],{"class":47,"line":48},[45,42858,2966],{"class":51},[45,42860,56],{"class":55},[45,42862,8604],{"class":85},[45,42864,100],{"class":99},[45,42866,3096],{"class":103},[45,42868,100],{"class":99},[45,42870,457],{"class":85},[45,42872,1565],{"class":99},[45,42874,10580],{"class":103},[45,42876,100],{"class":99},[45,42878,3079],{"class":85},[45,42880,1565],{"class":99},[45,42882,2082],{"class":103},[45,42884,100],{"class":99},[45,42886,457],{"class":85},[45,42888,10503],{"class":59},[45,42890,8629],{"class":85},[14,42892,42893,42894,21,42896,25,42898,42900,42901,1316,42903,240],{},"This is different from a list. A list uses positions like ",[18,42895,10482],{},[18,42897,8578],{},[18,42899,9367],{},". A dictionary uses keys like ",[18,42902,42627],{},[18,42904,42637],{},[31,42906,42908],{"id":42907},"when-to-use-a-dictionary","When to use a dictionary",[14,42910,42911,42912,240],{},"Use a dictionary when values need ",[2421,42913,42914],{},"names instead of positions",[14,42916,42917],{},"Common examples:",[195,42919,42920,42923,42926,42929,42932],{},[198,42921,42922],{},"A student record",[198,42924,42925],{},"App settings",[198,42927,42928],{},"Word counters",[198,42930,42931],{},"Product details",[198,42933,42934],{},"Grouped data",[14,42936,466],{},[36,42938,42940],{"className":38,"code":42939,"language":40,"meta":41,"style":41},"settings = {\n    \"theme\": \"dark\",\n    \"language\": \"English\",\n    \"notifications\": True\n}\n",[18,42941,42942,42951,42971,42991,43004],{"__ignoreMap":41},[45,42943,42944,42947,42949],{"class":47,"line":48},[45,42945,42946],{"class":51},"settings ",[45,42948,56],{"class":55},[45,42950,41432],{"class":85},[45,42952,42953,42955,42958,42960,42962,42964,42967,42969],{"class":47,"line":63},[45,42954,41437],{"class":99},[45,42956,42957],{"class":103},"theme",[45,42959,100],{"class":99},[45,42961,457],{"class":85},[45,42963,1565],{"class":99},[45,42965,42966],{"class":103},"dark",[45,42968,100],{"class":99},[45,42970,41453],{"class":85},[45,42972,42973,42975,42978,42980,42982,42984,42987,42989],{"class":47,"line":70},[45,42974,41437],{"class":99},[45,42976,42977],{"class":103},"language",[45,42979,100],{"class":99},[45,42981,457],{"class":85},[45,42983,1565],{"class":99},[45,42985,42986],{"class":103},"English",[45,42988,100],{"class":99},[45,42990,41453],{"class":85},[45,42992,42993,42995,42998,43000,43002],{"class":47,"line":89},[45,42994,41437],{"class":99},[45,42996,42997],{"class":103},"notifications",[45,42999,100],{"class":99},[45,43001,457],{"class":85},[45,43003,482],{"class":481},[45,43005,43006],{"class":47,"line":112},[45,43007,8629],{"class":85},[14,43009,43010],{},"A dictionary is often a better choice than a list when you want fast lookup by key.",[31,43012,43014],{"id":43013},"how-to-create-a-dictionary","How to create a dictionary",[14,43016,43017,43018,240],{},"You can create a dictionary with curly braces ",[18,43019,21683],{},[36,43021,43023],{"className":38,"code":43022,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\nprint(student)\n",[18,43024,43025,43061],{"__ignoreMap":41},[45,43026,43027,43029,43031,43033,43035,43037,43039,43041,43043,43045,43047,43049,43051,43053,43055,43057,43059],{"class":47,"line":48},[45,43028,41427],{"class":51},[45,43030,56],{"class":55},[45,43032,8604],{"class":85},[45,43034,100],{"class":99},[45,43036,3096],{"class":103},[45,43038,100],{"class":99},[45,43040,457],{"class":85},[45,43042,1565],{"class":99},[45,43044,10580],{"class":103},[45,43046,100],{"class":99},[45,43048,3079],{"class":85},[45,43050,1565],{"class":99},[45,43052,2082],{"class":103},[45,43054,100],{"class":99},[45,43056,457],{"class":85},[45,43058,10503],{"class":59},[45,43060,8629],{"class":85},[45,43062,43063,43065,43067,43069],{"class":47,"line":63},[45,43064,2065],{"class":92},[45,43066,96],{"class":85},[45,43068,41483],{"class":2070},[45,43070,109],{"class":85},[14,43072,177],{},[36,43074,43076],{"className":38,"code":43075,"language":40,"meta":41,"style":41},"{'name': 'Ana', 'age': 20}\n",[18,43077,43078],{"__ignoreMap":41},[45,43079,43080,43082,43084,43086,43088,43090,43092,43094,43096,43098,43100,43102,43104,43106,43108],{"class":47,"line":48},[45,43081,5551],{"class":85},[45,43083,7901],{"class":99},[45,43085,3096],{"class":103},[45,43087,7901],{"class":99},[45,43089,457],{"class":85},[45,43091,9199],{"class":99},[45,43093,10580],{"class":103},[45,43095,7901],{"class":99},[45,43097,3079],{"class":85},[45,43099,9199],{"class":99},[45,43101,2082],{"class":103},[45,43103,7901],{"class":99},[45,43105,457],{"class":85},[45,43107,10503],{"class":59},[45,43109,8629],{"class":85},[712,43111,43113],{"id":43112},"empty-dictionary","Empty dictionary",[36,43115,43117],{"className":38,"code":43116,"language":40,"meta":41,"style":41},"data = {}\nprint(data)\n",[18,43118,43119,43127],{"__ignoreMap":41},[45,43120,43121,43123,43125],{"class":47,"line":48},[45,43122,30591],{"class":51},[45,43124,56],{"class":55},[45,43126,9656],{"class":85},[45,43128,43129,43131,43133,43135],{"class":47,"line":63},[45,43130,2065],{"class":92},[45,43132,96],{"class":85},[45,43134,30614],{"class":2070},[45,43136,109],{"class":85},[14,43138,177],{},[36,43140,43142],{"className":38,"code":43141,"language":40,"meta":41,"style":41},"{}\n",[18,43143,43144],{"__ignoreMap":41},[45,43145,43146],{"class":47,"line":48},[45,43147,43141],{"class":85},[712,43149,1350,43151],{"id":43150},"using-dict",[18,43152,43153],{},"dict()",[14,43155,43156,43157,457],{},"You can also create a dictionary with ",[18,43158,43153],{},[36,43160,43162],{"className":38,"code":43161,"language":40,"meta":41,"style":41},"student = dict(name=\"Ana\", age=20)\nprint(student)\n",[18,43163,43164,43195],{"__ignoreMap":41},[45,43165,43166,43168,43170,43173,43175,43177,43179,43181,43183,43185,43187,43189,43191,43193],{"class":47,"line":48},[45,43167,41427],{"class":51},[45,43169,56],{"class":55},[45,43171,43172],{"class":2109}," dict",[45,43174,96],{"class":85},[45,43176,3096],{"class":6503},[45,43178,56],{"class":55},[45,43180,100],{"class":99},[45,43182,10580],{"class":103},[45,43184,100],{"class":99},[45,43186,3079],{"class":85},[45,43188,3305],{"class":6503},[45,43190,56],{"class":55},[45,43192,3372],{"class":59},[45,43194,109],{"class":85},[45,43196,43197,43199,43201,43203],{"class":47,"line":63},[45,43198,2065],{"class":92},[45,43200,96],{"class":85},[45,43202,41483],{"class":2070},[45,43204,109],{"class":85},[14,43206,177],{},[36,43208,43209],{"className":38,"code":43075,"language":40,"meta":41,"style":41},[18,43210,43211],{"__ignoreMap":41},[45,43212,43213,43215,43217,43219,43221,43223,43225,43227,43229,43231,43233,43235,43237,43239,43241],{"class":47,"line":48},[45,43214,5551],{"class":85},[45,43216,7901],{"class":99},[45,43218,3096],{"class":103},[45,43220,7901],{"class":99},[45,43222,457],{"class":85},[45,43224,9199],{"class":99},[45,43226,10580],{"class":103},[45,43228,7901],{"class":99},[45,43230,3079],{"class":85},[45,43232,9199],{"class":99},[45,43234,2082],{"class":103},[45,43236,7901],{"class":99},[45,43238,457],{"class":85},[45,43240,10503],{"class":59},[45,43242,8629],{"class":85},[14,43244,43245,43246,240],{},"If you want more ways to create one, see ",[236,43247,43249],{"href":43248},"\u002Freference\u002Fpython-dictionary-creating-a-dictionary\u002F","creating a dictionary in Python",[31,43251,43253],{"id":43252},"how-to-access-dictionary-values","How to access dictionary values",[14,43255,43256],{},"Use square brackets with a key:",[36,43258,43260],{"className":38,"code":43259,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\n\nprint(student[\"name\"])\n",[18,43261,43262,43298,43302],{"__ignoreMap":41},[45,43263,43264,43266,43268,43270,43272,43274,43276,43278,43280,43282,43284,43286,43288,43290,43292,43294,43296],{"class":47,"line":48},[45,43265,41427],{"class":51},[45,43267,56],{"class":55},[45,43269,8604],{"class":85},[45,43271,100],{"class":99},[45,43273,3096],{"class":103},[45,43275,100],{"class":99},[45,43277,457],{"class":85},[45,43279,1565],{"class":99},[45,43281,10580],{"class":103},[45,43283,100],{"class":99},[45,43285,3079],{"class":85},[45,43287,1565],{"class":99},[45,43289,2082],{"class":103},[45,43291,100],{"class":99},[45,43293,457],{"class":85},[45,43295,10503],{"class":59},[45,43297,8629],{"class":85},[45,43299,43300],{"class":47,"line":63},[45,43301,67],{"emptyLinePlaceholder":66},[45,43303,43304,43306,43308,43310,43312,43314,43316,43318],{"class":47,"line":70},[45,43305,2065],{"class":92},[45,43307,96],{"class":85},[45,43309,41483],{"class":2070},[45,43311,9680],{"class":85},[45,43313,100],{"class":99},[45,43315,3096],{"class":103},[45,43317,100],{"class":99},[45,43319,21941],{"class":85},[14,43321,177],{},[36,43323,43324],{"className":38,"code":42808,"language":40,"meta":41,"style":41},[18,43325,43326],{"__ignoreMap":41},[45,43327,43328],{"class":47,"line":48},[45,43329,42808],{"class":51},[712,43331,1350,43333],{"id":43332},"using-get",[18,43334,43335],{},"get()",[14,43337,43338,43339,240],{},"If a key might not exist, use ",[18,43340,43335],{},[36,43342,43344],{"className":38,"code":43343,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\n\nprint(student.get(\"name\"))\nprint(student.get(\"city\"))\n",[18,43345,43346,43382,43386,43409],{"__ignoreMap":41},[45,43347,43348,43350,43352,43354,43356,43358,43360,43362,43364,43366,43368,43370,43372,43374,43376,43378,43380],{"class":47,"line":48},[45,43349,41427],{"class":51},[45,43351,56],{"class":55},[45,43353,8604],{"class":85},[45,43355,100],{"class":99},[45,43357,3096],{"class":103},[45,43359,100],{"class":99},[45,43361,457],{"class":85},[45,43363,1565],{"class":99},[45,43365,10580],{"class":103},[45,43367,100],{"class":99},[45,43369,3079],{"class":85},[45,43371,1565],{"class":99},[45,43373,2082],{"class":103},[45,43375,100],{"class":99},[45,43377,457],{"class":85},[45,43379,10503],{"class":59},[45,43381,8629],{"class":85},[45,43383,43384],{"class":47,"line":63},[45,43385,67],{"emptyLinePlaceholder":66},[45,43387,43388,43390,43392,43394,43396,43399,43401,43403,43405,43407],{"class":47,"line":70},[45,43389,2065],{"class":92},[45,43391,96],{"class":85},[45,43393,41483],{"class":2070},[45,43395,240],{"class":85},[45,43397,43398],{"class":2070},"get",[45,43400,96],{"class":85},[45,43402,100],{"class":99},[45,43404,3096],{"class":103},[45,43406,100],{"class":99},[45,43408,4348],{"class":85},[45,43410,43411,43413,43415,43417,43419,43421,43423,43425,43427,43429],{"class":47,"line":89},[45,43412,2065],{"class":92},[45,43414,96],{"class":85},[45,43416,41483],{"class":2070},[45,43418,240],{"class":85},[45,43420,43398],{"class":2070},[45,43422,96],{"class":85},[45,43424,100],{"class":99},[45,43426,10562],{"class":103},[45,43428,100],{"class":99},[45,43430,4348],{"class":85},[14,43432,177],{},[36,43434,43436],{"className":38,"code":43435,"language":40,"meta":41,"style":41},"Ana\nNone\n",[18,43437,43438,43442],{"__ignoreMap":41},[45,43439,43440],{"class":47,"line":48},[45,43441,42808],{"class":51},[45,43443,43444],{"class":47,"line":63},[45,43445,31576],{"class":481},[14,43447,43448],{},"This is safer than using square brackets when the key may be missing.",[36,43450,43452],{"className":38,"code":43451,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\"}\n\n# print(student[\"city\"])   # This would raise an error\nprint(student.get(\"city\"))\n",[18,43453,43454,43478,43482,43487],{"__ignoreMap":41},[45,43455,43456,43458,43460,43462,43464,43466,43468,43470,43472,43474,43476],{"class":47,"line":48},[45,43457,41427],{"class":51},[45,43459,56],{"class":55},[45,43461,8604],{"class":85},[45,43463,100],{"class":99},[45,43465,3096],{"class":103},[45,43467,100],{"class":99},[45,43469,457],{"class":85},[45,43471,1565],{"class":99},[45,43473,10580],{"class":103},[45,43475,100],{"class":99},[45,43477,8629],{"class":85},[45,43479,43480],{"class":47,"line":63},[45,43481,67],{"emptyLinePlaceholder":66},[45,43483,43484],{"class":47,"line":70},[45,43485,43486],{"class":7624},"# print(student[\"city\"])   # This would raise an error\n",[45,43488,43489,43491,43493,43495,43497,43499,43501,43503,43505,43507],{"class":47,"line":89},[45,43490,2065],{"class":92},[45,43492,96],{"class":85},[45,43494,41483],{"class":2070},[45,43496,240],{"class":85},[45,43498,43398],{"class":2070},[45,43500,96],{"class":85},[45,43502,100],{"class":99},[45,43504,10562],{"class":103},[45,43506,100],{"class":99},[45,43508,4348],{"class":85},[14,43510,43511,43512,230,43518,240],{},"To learn more, see the ",[236,43513,43515,43516,2575],{"href":43514},"\u002Freference\u002Fpython-dictionary-get-method\u002F","Python dictionary ",[18,43517,43335],{},[236,43519,43521],{"href":43520},"\u002Fhow-to\u002Fhow-to-access-values-in-a-dictionary-in-python\u002F","how to access values in a dictionary in Python",[31,43523,43525],{"id":43524},"how-to-add-and-change-items","How to add and change items",[14,43527,43528],{},"To add a new item, assign a value to a new key.",[36,43530,43532],{"className":38,"code":43531,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\nstudent[\"city\"] = \"Paris\"\n\nprint(student)\n",[18,43533,43534,43570,43592,43596],{"__ignoreMap":41},[45,43535,43536,43538,43540,43542,43544,43546,43548,43550,43552,43554,43556,43558,43560,43562,43564,43566,43568],{"class":47,"line":48},[45,43537,41427],{"class":51},[45,43539,56],{"class":55},[45,43541,8604],{"class":85},[45,43543,100],{"class":99},[45,43545,3096],{"class":103},[45,43547,100],{"class":99},[45,43549,457],{"class":85},[45,43551,1565],{"class":99},[45,43553,10580],{"class":103},[45,43555,100],{"class":99},[45,43557,3079],{"class":85},[45,43559,1565],{"class":99},[45,43561,2082],{"class":103},[45,43563,100],{"class":99},[45,43565,457],{"class":85},[45,43567,10503],{"class":59},[45,43569,8629],{"class":85},[45,43571,43572,43574,43576,43578,43580,43582,43584,43586,43588,43590],{"class":47,"line":63},[45,43573,41483],{"class":51},[45,43575,9680],{"class":85},[45,43577,100],{"class":99},[45,43579,10562],{"class":103},[45,43581,100],{"class":99},[45,43583,9685],{"class":85},[45,43585,3099],{"class":55},[45,43587,1565],{"class":99},[45,43589,10593],{"class":103},[45,43591,1571],{"class":99},[45,43593,43594],{"class":47,"line":70},[45,43595,67],{"emptyLinePlaceholder":66},[45,43597,43598,43600,43602,43604],{"class":47,"line":89},[45,43599,2065],{"class":92},[45,43601,96],{"class":85},[45,43603,41483],{"class":2070},[45,43605,109],{"class":85},[14,43607,177],{},[36,43609,43611],{"className":38,"code":43610,"language":40,"meta":41,"style":41},"{'name': 'Ana', 'age': 20, 'city': 'Paris'}\n",[18,43612,43613],{"__ignoreMap":41},[45,43614,43615,43617,43619,43621,43623,43625,43627,43629,43631,43633,43635,43637,43639,43641,43643,43645,43647,43649,43651,43653,43655,43657,43659],{"class":47,"line":48},[45,43616,5551],{"class":85},[45,43618,7901],{"class":99},[45,43620,3096],{"class":103},[45,43622,7901],{"class":99},[45,43624,457],{"class":85},[45,43626,9199],{"class":99},[45,43628,10580],{"class":103},[45,43630,7901],{"class":99},[45,43632,3079],{"class":85},[45,43634,9199],{"class":99},[45,43636,2082],{"class":103},[45,43638,7901],{"class":99},[45,43640,457],{"class":85},[45,43642,10503],{"class":59},[45,43644,3079],{"class":85},[45,43646,9199],{"class":99},[45,43648,10562],{"class":103},[45,43650,7901],{"class":99},[45,43652,457],{"class":85},[45,43654,9199],{"class":99},[45,43656,10593],{"class":103},[45,43658,7901],{"class":99},[45,43660,8629],{"class":85},[14,43662,43663],{},"To change an existing item, assign a new value to an existing key.",[36,43665,43667],{"className":38,"code":43666,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\nstudent[\"age\"] = 21\n\nprint(student)\n",[18,43668,43669,43705,43723,43727],{"__ignoreMap":41},[45,43670,43671,43673,43675,43677,43679,43681,43683,43685,43687,43689,43691,43693,43695,43697,43699,43701,43703],{"class":47,"line":48},[45,43672,41427],{"class":51},[45,43674,56],{"class":55},[45,43676,8604],{"class":85},[45,43678,100],{"class":99},[45,43680,3096],{"class":103},[45,43682,100],{"class":99},[45,43684,457],{"class":85},[45,43686,1565],{"class":99},[45,43688,10580],{"class":103},[45,43690,100],{"class":99},[45,43692,3079],{"class":85},[45,43694,1565],{"class":99},[45,43696,2082],{"class":103},[45,43698,100],{"class":99},[45,43700,457],{"class":85},[45,43702,10503],{"class":59},[45,43704,8629],{"class":85},[45,43706,43707,43709,43711,43713,43715,43717,43719,43721],{"class":47,"line":63},[45,43708,41483],{"class":51},[45,43710,9680],{"class":85},[45,43712,100],{"class":99},[45,43714,2082],{"class":103},[45,43716,100],{"class":99},[45,43718,9685],{"class":85},[45,43720,3099],{"class":55},[45,43722,42733],{"class":59},[45,43724,43725],{"class":47,"line":70},[45,43726,67],{"emptyLinePlaceholder":66},[45,43728,43729,43731,43733,43735],{"class":47,"line":89},[45,43730,2065],{"class":92},[45,43732,96],{"class":85},[45,43734,41483],{"class":2070},[45,43736,109],{"class":85},[14,43738,177],{},[36,43740,43742],{"className":38,"code":43741,"language":40,"meta":41,"style":41},"{'name': 'Ana', 'age': 21}\n",[18,43743,43744],{"__ignoreMap":41},[45,43745,43746,43748,43750,43752,43754,43756,43758,43760,43762,43764,43766,43768,43770,43772,43774],{"class":47,"line":48},[45,43747,5551],{"class":85},[45,43749,7901],{"class":99},[45,43751,3096],{"class":103},[45,43753,7901],{"class":99},[45,43755,457],{"class":85},[45,43757,9199],{"class":99},[45,43759,10580],{"class":103},[45,43761,7901],{"class":99},[45,43763,3079],{"class":85},[45,43765,9199],{"class":99},[45,43767,2082],{"class":103},[45,43769,7901],{"class":99},[45,43771,457],{"class":85},[45,43773,28464],{"class":59},[45,43775,8629],{"class":85},[14,43777,43778,43779,240],{},"You can also change multiple items with the ",[236,43780,43782,2575],{"href":43781},"\u002Freference\u002Fpython-dictionary-update-method\u002F",[18,43783,43784],{},"update()",[31,43786,43788],{"id":43787},"how-to-remove-items","How to remove items",[14,43790,165,43791,43794],{},[18,43792,43793],{},"pop()"," to remove a key and get its value back.",[36,43796,43798],{"className":38,"code":43797,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20, \"city\": \"Paris\"}\n\nremoved_value = student.pop(\"city\")\n\nprint(removed_value)\nprint(student)\n",[18,43799,43800,43852,43856,43880,43884,43895],{"__ignoreMap":41},[45,43801,43802,43804,43806,43808,43810,43812,43814,43816,43818,43820,43822,43824,43826,43828,43830,43832,43834,43836,43838,43840,43842,43844,43846,43848,43850],{"class":47,"line":48},[45,43803,41427],{"class":51},[45,43805,56],{"class":55},[45,43807,8604],{"class":85},[45,43809,100],{"class":99},[45,43811,3096],{"class":103},[45,43813,100],{"class":99},[45,43815,457],{"class":85},[45,43817,1565],{"class":99},[45,43819,10580],{"class":103},[45,43821,100],{"class":99},[45,43823,3079],{"class":85},[45,43825,1565],{"class":99},[45,43827,2082],{"class":103},[45,43829,100],{"class":99},[45,43831,457],{"class":85},[45,43833,10503],{"class":59},[45,43835,3079],{"class":85},[45,43837,1565],{"class":99},[45,43839,10562],{"class":103},[45,43841,100],{"class":99},[45,43843,457],{"class":85},[45,43845,1565],{"class":99},[45,43847,10593],{"class":103},[45,43849,100],{"class":99},[45,43851,8629],{"class":85},[45,43853,43854],{"class":47,"line":63},[45,43855,67],{"emptyLinePlaceholder":66},[45,43857,43858,43861,43863,43865,43867,43870,43872,43874,43876,43878],{"class":47,"line":70},[45,43859,43860],{"class":51},"removed_value ",[45,43862,56],{"class":55},[45,43864,42775],{"class":51},[45,43866,240],{"class":85},[45,43868,43869],{"class":2070},"pop",[45,43871,96],{"class":85},[45,43873,100],{"class":99},[45,43875,10562],{"class":103},[45,43877,100],{"class":99},[45,43879,109],{"class":85},[45,43881,43882],{"class":47,"line":89},[45,43883,67],{"emptyLinePlaceholder":66},[45,43885,43886,43888,43890,43893],{"class":47,"line":112},[45,43887,2065],{"class":92},[45,43889,96],{"class":85},[45,43891,43892],{"class":2070},"removed_value",[45,43894,109],{"class":85},[45,43896,43897,43899,43901,43903],{"class":47,"line":126},[45,43898,2065],{"class":92},[45,43900,96],{"class":85},[45,43902,41483],{"class":2070},[45,43904,109],{"class":85},[14,43906,177],{},[36,43908,43910],{"className":38,"code":43909,"language":40,"meta":41,"style":41},"Paris\n{'name': 'Ana', 'age': 20}\n",[18,43911,43912,43917],{"__ignoreMap":41},[45,43913,43914],{"class":47,"line":48},[45,43915,43916],{"class":51},"Paris\n",[45,43918,43919,43921,43923,43925,43927,43929,43931,43933,43935,43937,43939,43941,43943,43945,43947],{"class":47,"line":63},[45,43920,5551],{"class":85},[45,43922,7901],{"class":99},[45,43924,3096],{"class":103},[45,43926,7901],{"class":99},[45,43928,457],{"class":85},[45,43930,9199],{"class":99},[45,43932,10580],{"class":103},[45,43934,7901],{"class":99},[45,43936,3079],{"class":85},[45,43938,9199],{"class":99},[45,43940,2082],{"class":103},[45,43942,7901],{"class":99},[45,43944,457],{"class":85},[45,43946,10503],{"class":59},[45,43948,8629],{"class":85},[14,43950,165,43951,43954],{},[18,43952,43953],{},"del"," to remove a key without returning its value.",[36,43956,43958],{"className":38,"code":43957,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\n\ndel student[\"age\"]\nprint(student)\n",[18,43959,43960,43996,44000,44016],{"__ignoreMap":41},[45,43961,43962,43964,43966,43968,43970,43972,43974,43976,43978,43980,43982,43984,43986,43988,43990,43992,43994],{"class":47,"line":48},[45,43963,41427],{"class":51},[45,43965,56],{"class":55},[45,43967,8604],{"class":85},[45,43969,100],{"class":99},[45,43971,3096],{"class":103},[45,43973,100],{"class":99},[45,43975,457],{"class":85},[45,43977,1565],{"class":99},[45,43979,10580],{"class":103},[45,43981,100],{"class":99},[45,43983,3079],{"class":85},[45,43985,1565],{"class":99},[45,43987,2082],{"class":103},[45,43989,100],{"class":99},[45,43991,457],{"class":85},[45,43993,10503],{"class":59},[45,43995,8629],{"class":85},[45,43997,43998],{"class":47,"line":63},[45,43999,67],{"emptyLinePlaceholder":66},[45,44001,44002,44004,44006,44008,44010,44012,44014],{"class":47,"line":70},[45,44003,43953],{"class":73},[45,44005,42775],{"class":51},[45,44007,9680],{"class":85},[45,44009,100],{"class":99},[45,44011,2082],{"class":103},[45,44013,100],{"class":99},[45,44015,8594],{"class":85},[45,44017,44018,44020,44022,44024],{"class":47,"line":89},[45,44019,2065],{"class":92},[45,44021,96],{"class":85},[45,44023,41483],{"class":2070},[45,44025,109],{"class":85},[14,44027,177],{},[36,44029,44031],{"className":38,"code":44030,"language":40,"meta":41,"style":41},"{'name': 'Ana'}\n",[18,44032,44033],{"__ignoreMap":41},[45,44034,44035,44037,44039,44041,44043,44045,44047,44049,44051],{"class":47,"line":48},[45,44036,5551],{"class":85},[45,44038,7901],{"class":99},[45,44040,3096],{"class":103},[45,44042,7901],{"class":99},[45,44044,457],{"class":85},[45,44046,9199],{"class":99},[45,44048,10580],{"class":103},[45,44050,7901],{"class":99},[45,44052,8629],{"class":85},[14,44054,165,44055,44058],{},[18,44056,44057],{},"clear()"," to remove everything.",[36,44060,44062],{"className":38,"code":44061,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\n\nstudent.clear()\nprint(student)\n",[18,44063,44064,44100,44104,44115],{"__ignoreMap":41},[45,44065,44066,44068,44070,44072,44074,44076,44078,44080,44082,44084,44086,44088,44090,44092,44094,44096,44098],{"class":47,"line":48},[45,44067,41427],{"class":51},[45,44069,56],{"class":55},[45,44071,8604],{"class":85},[45,44073,100],{"class":99},[45,44075,3096],{"class":103},[45,44077,100],{"class":99},[45,44079,457],{"class":85},[45,44081,1565],{"class":99},[45,44083,10580],{"class":103},[45,44085,100],{"class":99},[45,44087,3079],{"class":85},[45,44089,1565],{"class":99},[45,44091,2082],{"class":103},[45,44093,100],{"class":99},[45,44095,457],{"class":85},[45,44097,10503],{"class":59},[45,44099,8629],{"class":85},[45,44101,44102],{"class":47,"line":63},[45,44103,67],{"emptyLinePlaceholder":66},[45,44105,44106,44108,44110,44113],{"class":47,"line":70},[45,44107,41483],{"class":51},[45,44109,240],{"class":85},[45,44111,44112],{"class":2070},"clear",[45,44114,2495],{"class":85},[45,44116,44117,44119,44121,44123],{"class":47,"line":89},[45,44118,2065],{"class":92},[45,44120,96],{"class":85},[45,44122,41483],{"class":2070},[45,44124,109],{"class":85},[14,44126,177],{},[36,44128,44129],{"className":38,"code":43141,"language":40,"meta":41,"style":41},[18,44130,44131],{"__ignoreMap":41},[45,44132,44133],{"class":47,"line":48},[45,44134,43141],{"class":85},[14,44136,44137],{},"Be careful: removing a missing key can raise an error.",[36,44139,44141],{"className":38,"code":44140,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\"}\n\n# del student[\"age\"]   # This would raise an error\n",[18,44142,44143,44167,44171],{"__ignoreMap":41},[45,44144,44145,44147,44149,44151,44153,44155,44157,44159,44161,44163,44165],{"class":47,"line":48},[45,44146,41427],{"class":51},[45,44148,56],{"class":55},[45,44150,8604],{"class":85},[45,44152,100],{"class":99},[45,44154,3096],{"class":103},[45,44156,100],{"class":99},[45,44158,457],{"class":85},[45,44160,1565],{"class":99},[45,44162,10580],{"class":103},[45,44164,100],{"class":99},[45,44166,8629],{"class":85},[45,44168,44169],{"class":47,"line":63},[45,44170,67],{"emptyLinePlaceholder":66},[45,44172,44173],{"class":47,"line":70},[45,44174,44175],{"class":7624},"# del student[\"age\"]   # This would raise an error\n",[14,44177,44178,44179,240],{},"For step-by-step help, see ",[236,44180,44182],{"href":44181},"\u002Fhow-to\u002Fhow-to-remove-a-key-from-a-dictionary-in-python\u002F","how to remove a key from a dictionary in Python",[31,44184,44186],{"id":44185},"looping-through-a-dictionary","Looping through a dictionary",[14,44188,44189],{},"You can loop through keys, values, or both.",[712,44191,44193],{"id":44192},"loop-through-keys","Loop through keys",[36,44195,44197],{"className":38,"code":44196,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20, \"city\": \"Madrid\"}\n\nfor key in student:\n    print(key)\n",[18,44198,44199,44251,44255,44267],{"__ignoreMap":41},[45,44200,44201,44203,44205,44207,44209,44211,44213,44215,44217,44219,44221,44223,44225,44227,44229,44231,44233,44235,44237,44239,44241,44243,44245,44247,44249],{"class":47,"line":48},[45,44202,41427],{"class":51},[45,44204,56],{"class":55},[45,44206,8604],{"class":85},[45,44208,100],{"class":99},[45,44210,3096],{"class":103},[45,44212,100],{"class":99},[45,44214,457],{"class":85},[45,44216,1565],{"class":99},[45,44218,10580],{"class":103},[45,44220,100],{"class":99},[45,44222,3079],{"class":85},[45,44224,1565],{"class":99},[45,44226,2082],{"class":103},[45,44228,100],{"class":99},[45,44230,457],{"class":85},[45,44232,10503],{"class":59},[45,44234,3079],{"class":85},[45,44236,1565],{"class":99},[45,44238,10562],{"class":103},[45,44240,100],{"class":99},[45,44242,457],{"class":85},[45,44244,1565],{"class":99},[45,44246,42754],{"class":103},[45,44248,100],{"class":99},[45,44250,8629],{"class":85},[45,44252,44253],{"class":47,"line":63},[45,44254,67],{"emptyLinePlaceholder":66},[45,44256,44257,44259,44261,44263,44265],{"class":47,"line":70},[45,44258,7895],{"class":73},[45,44260,23833],{"class":51},[45,44262,8623],{"class":73},[45,44264,42775],{"class":51},[45,44266,86],{"class":85},[45,44268,44269,44271,44273,44275],{"class":47,"line":89},[45,44270,93],{"class":92},[45,44272,96],{"class":85},[45,44274,9493],{"class":2070},[45,44276,109],{"class":85},[712,44278,44280],{"id":44279},"loop-through-values","Loop through values",[36,44282,44284],{"className":38,"code":44283,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20, \"city\": \"Madrid\"}\n\nfor value in student.values():\n    print(value)\n",[18,44285,44286,44338,44342,44358],{"__ignoreMap":41},[45,44287,44288,44290,44292,44294,44296,44298,44300,44302,44304,44306,44308,44310,44312,44314,44316,44318,44320,44322,44324,44326,44328,44330,44332,44334,44336],{"class":47,"line":48},[45,44289,41427],{"class":51},[45,44291,56],{"class":55},[45,44293,8604],{"class":85},[45,44295,100],{"class":99},[45,44297,3096],{"class":103},[45,44299,100],{"class":99},[45,44301,457],{"class":85},[45,44303,1565],{"class":99},[45,44305,10580],{"class":103},[45,44307,100],{"class":99},[45,44309,3079],{"class":85},[45,44311,1565],{"class":99},[45,44313,2082],{"class":103},[45,44315,100],{"class":99},[45,44317,457],{"class":85},[45,44319,10503],{"class":59},[45,44321,3079],{"class":85},[45,44323,1565],{"class":99},[45,44325,10562],{"class":103},[45,44327,100],{"class":99},[45,44329,457],{"class":85},[45,44331,1565],{"class":99},[45,44333,42754],{"class":103},[45,44335,100],{"class":99},[45,44337,8629],{"class":85},[45,44339,44340],{"class":47,"line":63},[45,44341,67],{"emptyLinePlaceholder":66},[45,44343,44344,44346,44348,44350,44352,44354,44356],{"class":47,"line":70},[45,44345,7895],{"class":73},[45,44347,1492],{"class":51},[45,44349,8623],{"class":73},[45,44351,42775],{"class":51},[45,44353,240],{"class":85},[45,44355,10790],{"class":2070},[45,44357,3828],{"class":85},[45,44359,44360,44362,44364,44366],{"class":47,"line":89},[45,44361,93],{"class":92},[45,44363,96],{"class":85},[45,44365,12722],{"class":2070},[45,44367,109],{"class":85},[712,44369,44371],{"id":44370},"loop-through-key-value-pairs","Loop through key-value pairs",[14,44373,44374],{},"This is often the easiest option when you need both the key and the value.",[36,44376,44378],{"className":38,"code":44377,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20, \"city\": \"Madrid\"}\n\nfor key, value in student.items():\n    print(key, value)\n",[18,44379,44380,44432,44436,44456],{"__ignoreMap":41},[45,44381,44382,44384,44386,44388,44390,44392,44394,44396,44398,44400,44402,44404,44406,44408,44410,44412,44414,44416,44418,44420,44422,44424,44426,44428,44430],{"class":47,"line":48},[45,44383,41427],{"class":51},[45,44385,56],{"class":55},[45,44387,8604],{"class":85},[45,44389,100],{"class":99},[45,44391,3096],{"class":103},[45,44393,100],{"class":99},[45,44395,457],{"class":85},[45,44397,1565],{"class":99},[45,44399,10580],{"class":103},[45,44401,100],{"class":99},[45,44403,3079],{"class":85},[45,44405,1565],{"class":99},[45,44407,2082],{"class":103},[45,44409,100],{"class":99},[45,44411,457],{"class":85},[45,44413,10503],{"class":59},[45,44415,3079],{"class":85},[45,44417,1565],{"class":99},[45,44419,10562],{"class":103},[45,44421,100],{"class":99},[45,44423,457],{"class":85},[45,44425,1565],{"class":99},[45,44427,42754],{"class":103},[45,44429,100],{"class":99},[45,44431,8629],{"class":85},[45,44433,44434],{"class":47,"line":63},[45,44435,67],{"emptyLinePlaceholder":66},[45,44437,44438,44440,44442,44444,44446,44448,44450,44452,44454],{"class":47,"line":70},[45,44439,7895],{"class":73},[45,44441,9502],{"class":51},[45,44443,3079],{"class":85},[45,44445,1492],{"class":51},[45,44447,8623],{"class":73},[45,44449,42775],{"class":51},[45,44451,240],{"class":85},[45,44453,9516],{"class":2070},[45,44455,3828],{"class":85},[45,44457,44458,44460,44462,44464,44466,44468],{"class":47,"line":89},[45,44459,93],{"class":92},[45,44461,96],{"class":85},[45,44463,9493],{"class":2070},[45,44465,3079],{"class":85},[45,44467,42794],{"class":2070},[45,44469,109],{"class":85},[14,44471,13456],{},[36,44473,44475],{"className":38,"code":44474,"language":40,"meta":41,"style":41},"name Ana\nage 20\ncity Madrid\n",[18,44476,44477,44481,44487],{"__ignoreMap":41},[45,44478,44479],{"class":47,"line":48},[45,44480,42813],{"class":51},[45,44482,44483,44485],{"class":47,"line":63},[45,44484,52],{"class":51},[45,44486,3436],{"class":59},[45,44488,44489],{"class":47,"line":70},[45,44490,42824],{"class":51},[14,44492,44493,44494,21,44500,25,44506,44512,44513,240],{},"You can learn more about ",[236,44495,44497],{"href":44496},"\u002Freference\u002Fpython-dictionary-items-method\u002F",[18,44498,44499],{},"items()",[236,44501,44503],{"href":44502},"\u002Freference\u002Fpython-dictionary-keys-method\u002F",[18,44504,44505],{},"keys()",[236,44507,44509],{"href":44508},"\u002Freference\u002Fpython-dictionary-values-method\u002F",[18,44510,44511],{},"values()",", or see ",[236,44514,10856],{"href":44515},"\u002Fhow-to\u002Fhow-to-loop-through-a-dictionary-in-python\u002F",[31,44517,44519],{"id":44518},"important-dictionary-rules","Important dictionary rules",[14,44521,44522],{},"Here are some important rules beginners should know:",[195,44524,44525,44530,44535,44540],{},[198,44526,44527],{},[2421,44528,44529],{},"Keys must be unique",[198,44531,44532],{},[2421,44533,44534],{},"Keys should be immutable",[198,44536,44537],{},[2421,44538,44539],{},"Values can be any type",[198,44541,44542],{},[2421,44543,44544],{},"Dictionaries are mutable",[712,44546,44529],{"id":44547},"keys-must-be-unique",[14,44549,44550],{},"If the same key appears more than once, the later value replaces the earlier one.",[36,44552,44554],{"className":38,"code":44553,"language":40,"meta":41,"style":41},"data = {\"name\": \"Ana\", \"name\": \"Luis\"}\nprint(data)\n",[18,44555,44556,44597],{"__ignoreMap":41},[45,44557,44558,44560,44562,44564,44566,44568,44570,44572,44574,44576,44578,44580,44582,44584,44586,44588,44590,44593,44595],{"class":47,"line":48},[45,44559,30591],{"class":51},[45,44561,56],{"class":55},[45,44563,8604],{"class":85},[45,44565,100],{"class":99},[45,44567,3096],{"class":103},[45,44569,100],{"class":99},[45,44571,457],{"class":85},[45,44573,1565],{"class":99},[45,44575,10580],{"class":103},[45,44577,100],{"class":99},[45,44579,3079],{"class":85},[45,44581,1565],{"class":99},[45,44583,3096],{"class":103},[45,44585,100],{"class":99},[45,44587,457],{"class":85},[45,44589,1565],{"class":99},[45,44591,44592],{"class":103},"Luis",[45,44594,100],{"class":99},[45,44596,8629],{"class":85},[45,44598,44599,44601,44603,44605],{"class":47,"line":63},[45,44600,2065],{"class":92},[45,44602,96],{"class":85},[45,44604,30614],{"class":2070},[45,44606,109],{"class":85},[14,44608,177],{},[36,44610,44612],{"className":38,"code":44611,"language":40,"meta":41,"style":41},"{'name': 'Luis'}\n",[18,44613,44614],{"__ignoreMap":41},[45,44615,44616,44618,44620,44622,44624,44626,44628,44630,44632],{"class":47,"line":48},[45,44617,5551],{"class":85},[45,44619,7901],{"class":99},[45,44621,3096],{"class":103},[45,44623,7901],{"class":99},[45,44625,457],{"class":85},[45,44627,9199],{"class":99},[45,44629,44592],{"class":103},[45,44631,7901],{"class":99},[45,44633,8629],{"class":85},[712,44635,44534],{"id":44636},"keys-should-be-immutable",[14,44638,44639],{},"Good key types include:",[195,44641,44642,44644,44646],{},[198,44643,23721],{},[198,44645,42404],{},[198,44647,44648],{},"Tuples",[14,44650,44651],{},"These work well because they do not change.",[712,44653,44539],{"id":44654},"values-can-be-any-type",[14,44656,44657],{},"A dictionary value can be a string, number, list, or even another dictionary.",[36,44659,44661],{"className":38,"code":44660,"language":40,"meta":41,"style":41},"student = {\n    \"name\": \"Ana\",\n    \"age\": 20,\n    \"grades\": [90, 85, 88],\n    \"address\": {\"city\": \"Madrid\", \"zip\": \"28001\"}\n}\n\nprint(student)\n",[18,44662,44663,44671,44689,44703,44731,44778,44782,44786],{"__ignoreMap":41},[45,44664,44665,44667,44669],{"class":47,"line":48},[45,44666,41427],{"class":51},[45,44668,56],{"class":55},[45,44670,41432],{"class":85},[45,44672,44673,44675,44677,44679,44681,44683,44685,44687],{"class":47,"line":63},[45,44674,41437],{"class":99},[45,44676,3096],{"class":103},[45,44678,100],{"class":99},[45,44680,457],{"class":85},[45,44682,1565],{"class":99},[45,44684,10580],{"class":103},[45,44686,100],{"class":99},[45,44688,41453],{"class":85},[45,44690,44691,44693,44695,44697,44699,44701],{"class":47,"line":70},[45,44692,41437],{"class":99},[45,44694,2082],{"class":103},[45,44696,100],{"class":99},[45,44698,457],{"class":85},[45,44700,10503],{"class":59},[45,44702,41453],{"class":85},[45,44704,44705,44707,44709,44711,44713,44715,44718,44720,44723,44725,44728],{"class":47,"line":89},[45,44706,41437],{"class":99},[45,44708,21020],{"class":103},[45,44710,100],{"class":99},[45,44712,457],{"class":85},[45,44714,8575],{"class":85},[45,44716,44717],{"class":59},"90",[45,44719,3079],{"class":85},[45,44721,44722],{"class":59}," 85",[45,44724,3079],{"class":85},[45,44726,44727],{"class":59}," 88",[45,44729,44730],{"class":85},"],\n",[45,44732,44733,44735,44738,44740,44742,44744,44746,44748,44750,44752,44754,44756,44758,44760,44762,44765,44767,44769,44771,44774,44776],{"class":47,"line":112},[45,44734,41437],{"class":99},[45,44736,44737],{"class":103},"address",[45,44739,100],{"class":99},[45,44741,457],{"class":85},[45,44743,8604],{"class":85},[45,44745,100],{"class":99},[45,44747,10562],{"class":103},[45,44749,100],{"class":99},[45,44751,457],{"class":85},[45,44753,1565],{"class":99},[45,44755,42754],{"class":103},[45,44757,100],{"class":99},[45,44759,3079],{"class":85},[45,44761,1565],{"class":99},[45,44763,44764],{"class":103},"zip",[45,44766,100],{"class":99},[45,44768,457],{"class":85},[45,44770,1565],{"class":99},[45,44772,44773],{"class":103},"28001",[45,44775,100],{"class":99},[45,44777,8629],{"class":85},[45,44779,44780],{"class":47,"line":126},[45,44781,8629],{"class":85},[45,44783,44784],{"class":47,"line":142},[45,44785,67],{"emptyLinePlaceholder":66},[45,44787,44788,44790,44792,44794],{"class":47,"line":149},[45,44789,2065],{"class":92},[45,44791,96],{"class":85},[45,44793,41483],{"class":2070},[45,44795,109],{"class":85},[712,44797,44544],{"id":44798},"dictionaries-are-mutable",[14,44800,44801],{},"This means you can change them after creation.",[36,44803,44805],{"className":38,"code":44804,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\"}\nstudent[\"name\"] = \"Eva\"\n\nprint(student)\n",[18,44806,44807,44831,44854,44858],{"__ignoreMap":41},[45,44808,44809,44811,44813,44815,44817,44819,44821,44823,44825,44827,44829],{"class":47,"line":48},[45,44810,41427],{"class":51},[45,44812,56],{"class":55},[45,44814,8604],{"class":85},[45,44816,100],{"class":99},[45,44818,3096],{"class":103},[45,44820,100],{"class":99},[45,44822,457],{"class":85},[45,44824,1565],{"class":99},[45,44826,10580],{"class":103},[45,44828,100],{"class":99},[45,44830,8629],{"class":85},[45,44832,44833,44835,44837,44839,44841,44843,44845,44847,44849,44852],{"class":47,"line":63},[45,44834,41483],{"class":51},[45,44836,9680],{"class":85},[45,44838,100],{"class":99},[45,44840,3096],{"class":103},[45,44842,100],{"class":99},[45,44844,9685],{"class":85},[45,44846,3099],{"class":55},[45,44848,1565],{"class":99},[45,44850,44851],{"class":103},"Eva",[45,44853,1571],{"class":99},[45,44855,44856],{"class":47,"line":70},[45,44857,67],{"emptyLinePlaceholder":66},[45,44859,44860,44862,44864,44866],{"class":47,"line":89},[45,44861,2065],{"class":92},[45,44863,96],{"class":85},[45,44865,41483],{"class":2070},[45,44867,109],{"class":85},[31,44869,44871],{"id":44870},"dictionary-methods-to-learn-next","Dictionary methods to learn next",[14,44873,44874],{},"These methods are very useful:",[195,44876,44877,44882,44887,44892,44897],{},[198,44878,44879,44881],{},[18,44880,43335],{}," for safe lookup",[198,44883,44884,44886],{},[18,44885,44505],{}," to view all keys",[198,44888,44889,44891],{},[18,44890,44511],{}," to view all values",[198,44893,44894,44896],{},[18,44895,44499],{}," to work with key-value pairs",[198,44898,44899,44901],{},[18,44900,43784],{}," to merge or change multiple items",[14,44903,466],{},[36,44905,44907],{"className":38,"code":44906,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\n\nprint(student.keys())\nprint(student.values())\nprint(student.items())\n\nstudent.update({\"age\": 21, \"city\": \"Madrid\"})\nprint(student)\n",[18,44908,44909,44945,44949,44963,44977,44991,44995,45036],{"__ignoreMap":41},[45,44910,44911,44913,44915,44917,44919,44921,44923,44925,44927,44929,44931,44933,44935,44937,44939,44941,44943],{"class":47,"line":48},[45,44912,41427],{"class":51},[45,44914,56],{"class":55},[45,44916,8604],{"class":85},[45,44918,100],{"class":99},[45,44920,3096],{"class":103},[45,44922,100],{"class":99},[45,44924,457],{"class":85},[45,44926,1565],{"class":99},[45,44928,10580],{"class":103},[45,44930,100],{"class":99},[45,44932,3079],{"class":85},[45,44934,1565],{"class":99},[45,44936,2082],{"class":103},[45,44938,100],{"class":99},[45,44940,457],{"class":85},[45,44942,10503],{"class":59},[45,44944,8629],{"class":85},[45,44946,44947],{"class":47,"line":63},[45,44948,67],{"emptyLinePlaceholder":66},[45,44950,44951,44953,44955,44957,44959,44961],{"class":47,"line":70},[45,44952,2065],{"class":92},[45,44954,96],{"class":85},[45,44956,41483],{"class":2070},[45,44958,240],{"class":85},[45,44960,10636],{"class":2070},[45,44962,10777],{"class":85},[45,44964,44965,44967,44969,44971,44973,44975],{"class":47,"line":89},[45,44966,2065],{"class":92},[45,44968,96],{"class":85},[45,44970,41483],{"class":2070},[45,44972,240],{"class":85},[45,44974,10790],{"class":2070},[45,44976,10777],{"class":85},[45,44978,44979,44981,44983,44985,44987,44989],{"class":47,"line":112},[45,44980,2065],{"class":92},[45,44982,96],{"class":85},[45,44984,41483],{"class":2070},[45,44986,240],{"class":85},[45,44988,9516],{"class":2070},[45,44990,10777],{"class":85},[45,44992,44993],{"class":47,"line":126},[45,44994,67],{"emptyLinePlaceholder":66},[45,44996,44997,44999,45001,45004,45007,45009,45011,45013,45015,45017,45019,45021,45023,45025,45027,45029,45031,45033],{"class":47,"line":142},[45,44998,41483],{"class":51},[45,45000,240],{"class":85},[45,45002,45003],{"class":2070},"update",[45,45005,45006],{"class":85},"({",[45,45008,100],{"class":99},[45,45010,2082],{"class":103},[45,45012,100],{"class":99},[45,45014,457],{"class":85},[45,45016,28464],{"class":59},[45,45018,3079],{"class":85},[45,45020,1565],{"class":99},[45,45022,10562],{"class":103},[45,45024,100],{"class":99},[45,45026,457],{"class":85},[45,45028,1565],{"class":99},[45,45030,42754],{"class":103},[45,45032,100],{"class":99},[45,45034,45035],{"class":85},"})\n",[45,45037,45038,45040,45042,45044],{"class":47,"line":149},[45,45039,2065],{"class":92},[45,45041,96],{"class":85},[45,45043,41483],{"class":2070},[45,45045,109],{"class":85},[14,45047,177],{},[36,45049,45051],{"className":38,"code":45050,"language":40,"meta":41,"style":41},"dict_keys(['name', 'age'])\ndict_values(['name', 20])\ndict_items([('name', 'Ana'), ('age', 20)])\n{'name': 'Ana', 'age': 21, 'city': 'Madrid'}\n",[18,45052,45053,45076,45095,45133],{"__ignoreMap":41},[45,45054,45055,45058,45060,45062,45064,45066,45068,45070,45072,45074],{"class":47,"line":48},[45,45056,45057],{"class":2070},"dict_keys",[45,45059,34742],{"class":85},[45,45061,7901],{"class":99},[45,45063,3096],{"class":103},[45,45065,7901],{"class":99},[45,45067,3079],{"class":85},[45,45069,9199],{"class":99},[45,45071,2082],{"class":103},[45,45073,7901],{"class":99},[45,45075,21941],{"class":85},[45,45077,45078,45081,45083,45085,45087,45089,45091,45093],{"class":47,"line":63},[45,45079,45080],{"class":2070},"dict_values",[45,45082,34742],{"class":85},[45,45084,7901],{"class":99},[45,45086,3096],{"class":103},[45,45088,7901],{"class":99},[45,45090,3079],{"class":85},[45,45092,10503],{"class":59},[45,45094,21941],{"class":85},[45,45096,45097,45100,45102,45104,45106,45108,45110,45112,45114,45116,45118,45120,45122,45124,45126,45128,45130],{"class":47,"line":70},[45,45098,45099],{"class":2070},"dict_items",[45,45101,26867],{"class":85},[45,45103,7901],{"class":99},[45,45105,3096],{"class":103},[45,45107,7901],{"class":99},[45,45109,3079],{"class":85},[45,45111,9199],{"class":99},[45,45113,10580],{"class":103},[45,45115,7901],{"class":99},[45,45117,25873],{"class":85},[45,45119,14039],{"class":85},[45,45121,7901],{"class":99},[45,45123,2082],{"class":103},[45,45125,7901],{"class":99},[45,45127,3079],{"class":85},[45,45129,10503],{"class":59},[45,45131,45132],{"class":85},")])\n",[45,45134,45135,45137,45139,45141,45143,45145,45147,45149,45151,45153,45155,45157,45159,45161,45163,45165,45167,45169,45171,45173,45175,45177,45179],{"class":47,"line":89},[45,45136,5551],{"class":85},[45,45138,7901],{"class":99},[45,45140,3096],{"class":103},[45,45142,7901],{"class":99},[45,45144,457],{"class":85},[45,45146,9199],{"class":99},[45,45148,10580],{"class":103},[45,45150,7901],{"class":99},[45,45152,3079],{"class":85},[45,45154,9199],{"class":99},[45,45156,2082],{"class":103},[45,45158,7901],{"class":99},[45,45160,457],{"class":85},[45,45162,28464],{"class":59},[45,45164,3079],{"class":85},[45,45166,9199],{"class":99},[45,45168,10562],{"class":103},[45,45170,7901],{"class":99},[45,45172,457],{"class":85},[45,45174,9199],{"class":99},[45,45176,42754],{"class":103},[45,45178,7901],{"class":99},[45,45180,8629],{"class":85},[31,45182,45184],{"id":45183},"common-beginner-problems","Common beginner problems",[14,45186,45187],{},"Here are some common mistakes when working with dictionaries.",[712,45189,45191],{"id":45190},"using-a-key-that-does-not-exist","Using a key that does not exist",[14,45193,45194,45195,45198],{},"This causes a ",[18,45196,45197],{},"KeyError"," if you use square brackets.",[36,45200,45202],{"className":38,"code":45201,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\"}\n\n# print(student[\"age\"])   # KeyError\nprint(student.get(\"age\"))  # Safer\n",[18,45203,45204,45228,45232,45237],{"__ignoreMap":41},[45,45205,45206,45208,45210,45212,45214,45216,45218,45220,45222,45224,45226],{"class":47,"line":48},[45,45207,41427],{"class":51},[45,45209,56],{"class":55},[45,45211,8604],{"class":85},[45,45213,100],{"class":99},[45,45215,3096],{"class":103},[45,45217,100],{"class":99},[45,45219,457],{"class":85},[45,45221,1565],{"class":99},[45,45223,10580],{"class":103},[45,45225,100],{"class":99},[45,45227,8629],{"class":85},[45,45229,45230],{"class":47,"line":63},[45,45231,67],{"emptyLinePlaceholder":66},[45,45233,45234],{"class":47,"line":70},[45,45235,45236],{"class":7624},"# print(student[\"age\"])   # KeyError\n",[45,45238,45239,45241,45243,45245,45247,45249,45251,45253,45255,45257,45259],{"class":47,"line":89},[45,45240,2065],{"class":92},[45,45242,96],{"class":85},[45,45244,41483],{"class":2070},[45,45246,240],{"class":85},[45,45248,43398],{"class":2070},[45,45250,96],{"class":85},[45,45252,100],{"class":99},[45,45254,2082],{"class":103},[45,45256,100],{"class":99},[45,45258,22199],{"class":85},[45,45260,45261],{"class":7624},"  # Safer\n",[14,45263,45264,45265,240],{},"If you are seeing this error, read ",[236,45266,45268],{"href":45267},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes\u002F","KeyError in Python: causes and fixes",[712,45270,45272],{"id":45271},"confusing-dictionaries-with-lists","Confusing dictionaries with lists",[14,45274,45275,45276,1316,45278,240],{},"A dictionary does not use list positions like ",[18,45277,10482],{},[18,45279,8578],{},[36,45281,45283],{"className":38,"code":45282,"language":40,"meta":41,"style":41},"student = {\"name\": \"Ana\", \"age\": 20}\n\n# print(student[0])   # Wrong\nprint(student[\"name\"])  # Correct\n",[18,45284,45285,45321,45325,45330],{"__ignoreMap":41},[45,45286,45287,45289,45291,45293,45295,45297,45299,45301,45303,45305,45307,45309,45311,45313,45315,45317,45319],{"class":47,"line":48},[45,45288,41427],{"class":51},[45,45290,56],{"class":55},[45,45292,8604],{"class":85},[45,45294,100],{"class":99},[45,45296,3096],{"class":103},[45,45298,100],{"class":99},[45,45300,457],{"class":85},[45,45302,1565],{"class":99},[45,45304,10580],{"class":103},[45,45306,100],{"class":99},[45,45308,3079],{"class":85},[45,45310,1565],{"class":99},[45,45312,2082],{"class":103},[45,45314,100],{"class":99},[45,45316,457],{"class":85},[45,45318,10503],{"class":59},[45,45320,8629],{"class":85},[45,45322,45323],{"class":47,"line":63},[45,45324,67],{"emptyLinePlaceholder":66},[45,45326,45327],{"class":47,"line":70},[45,45328,45329],{"class":7624},"# print(student[0])   # Wrong\n",[45,45331,45332,45334,45336,45338,45340,45342,45344,45346,45349],{"class":47,"line":89},[45,45333,2065],{"class":92},[45,45335,96],{"class":85},[45,45337,41483],{"class":2070},[45,45339,9680],{"class":85},[45,45341,100],{"class":99},[45,45343,3096],{"class":103},[45,45345,100],{"class":99},[45,45347,45348],{"class":85},"])",[45,45350,45351],{"class":7624},"  # Correct\n",[712,45353,45355],{"id":45354},"trying-to-use-duplicate-keys","Trying to use duplicate keys",[14,45357,45358],{},"Only the last value is kept.",[36,45360,45362],{"className":38,"code":45361,"language":40,"meta":41,"style":41},"data = {\"color\": \"red\", \"color\": \"blue\"}\nprint(data)\n",[18,45363,45364,45404],{"__ignoreMap":41},[45,45365,45366,45368,45370,45372,45374,45376,45378,45380,45382,45384,45386,45388,45390,45392,45394,45396,45398,45400,45402],{"class":47,"line":48},[45,45367,30591],{"class":51},[45,45369,56],{"class":55},[45,45371,8604],{"class":85},[45,45373,100],{"class":99},[45,45375,22378],{"class":103},[45,45377,100],{"class":99},[45,45379,457],{"class":85},[45,45381,1565],{"class":99},[45,45383,22329],{"class":103},[45,45385,100],{"class":99},[45,45387,3079],{"class":85},[45,45389,1565],{"class":99},[45,45391,22378],{"class":103},[45,45393,100],{"class":99},[45,45395,457],{"class":85},[45,45397,1565],{"class":99},[45,45399,22347],{"class":103},[45,45401,100],{"class":99},[45,45403,8629],{"class":85},[45,45405,45406,45408,45410,45412],{"class":47,"line":63},[45,45407,2065],{"class":92},[45,45409,96],{"class":85},[45,45411,30614],{"class":2070},[45,45413,109],{"class":85},[14,45415,177],{},[36,45417,45419],{"className":38,"code":45418,"language":40,"meta":41,"style":41},"{'color': 'blue'}\n",[18,45420,45421],{"__ignoreMap":41},[45,45422,45423,45425,45427,45429,45431,45433,45435,45437,45439],{"class":47,"line":48},[45,45424,5551],{"class":85},[45,45426,7901],{"class":99},[45,45428,22378],{"class":103},[45,45430,7901],{"class":99},[45,45432,457],{"class":85},[45,45434,9199],{"class":99},[45,45436,22347],{"class":103},[45,45438,7901],{"class":99},[45,45440,8629],{"class":85},[712,45442,45444],{"id":45443},"forgetting-that-keys-are-case-sensitive","Forgetting that keys are case-sensitive",[14,45446,45447],{},"These are different keys:",[36,45449,45451],{"className":38,"code":45450,"language":40,"meta":41,"style":41},"data = {\"Name\": \"Ana\", \"name\": \"Luis\"}\nprint(data)\n",[18,45452,45453,45494],{"__ignoreMap":41},[45,45454,45455,45457,45459,45461,45463,45466,45468,45470,45472,45474,45476,45478,45480,45482,45484,45486,45488,45490,45492],{"class":47,"line":48},[45,45456,30591],{"class":51},[45,45458,56],{"class":55},[45,45460,8604],{"class":85},[45,45462,100],{"class":99},[45,45464,45465],{"class":103},"Name",[45,45467,100],{"class":99},[45,45469,457],{"class":85},[45,45471,1565],{"class":99},[45,45473,10580],{"class":103},[45,45475,100],{"class":99},[45,45477,3079],{"class":85},[45,45479,1565],{"class":99},[45,45481,3096],{"class":103},[45,45483,100],{"class":99},[45,45485,457],{"class":85},[45,45487,1565],{"class":99},[45,45489,44592],{"class":103},[45,45491,100],{"class":99},[45,45493,8629],{"class":85},[45,45495,45496,45498,45500,45502],{"class":47,"line":63},[45,45497,2065],{"class":92},[45,45499,96],{"class":85},[45,45501,30614],{"class":2070},[45,45503,109],{"class":85},[14,45505,177],{},[36,45507,45509],{"className":38,"code":45508,"language":40,"meta":41,"style":41},"{'Name': 'Ana', 'name': 'Luis'}\n",[18,45510,45511],{"__ignoreMap":41},[45,45512,45513,45515,45517,45519,45521,45523,45525,45527,45529,45531,45533,45535,45537,45539,45541,45543,45545],{"class":47,"line":48},[45,45514,5551],{"class":85},[45,45516,7901],{"class":99},[45,45518,45465],{"class":103},[45,45520,7901],{"class":99},[45,45522,457],{"class":85},[45,45524,9199],{"class":99},[45,45526,10580],{"class":103},[45,45528,7901],{"class":99},[45,45530,3079],{"class":85},[45,45532,9199],{"class":99},[45,45534,3096],{"class":103},[45,45536,7901],{"class":99},[45,45538,457],{"class":85},[45,45540,9199],{"class":99},[45,45542,44592],{"class":103},[45,45544,7901],{"class":99},[45,45546,8629],{"class":85},[712,45548,45550,45551,45553],{"id":45549},"using-brackets-instead-of-get-when-a-key-may-be-missing","Using brackets instead of ",[18,45552,43335],{}," when a key may be missing",[14,45555,165,45556,45558],{},[18,45557,43335],{}," when you are not sure a key exists.",[31,45560,21691],{"id":21690},[14,45562,45563],{},"If your dictionary code is not working, these quick checks can help:",[36,45565,45567],{"className":38,"code":45566,"language":40,"meta":41,"style":41},"print(my_dict)\nprint(my_dict.keys())\nprint(my_dict.values())\nprint(my_dict.items())\nprint(type(my_dict))\nprint('name' in my_dict)\n",[18,45568,45569,45580,45594,45608,45622,45636],{"__ignoreMap":41},[45,45570,45571,45573,45575,45578],{"class":47,"line":48},[45,45572,2065],{"class":92},[45,45574,96],{"class":85},[45,45576,45577],{"class":2070},"my_dict",[45,45579,109],{"class":85},[45,45581,45582,45584,45586,45588,45590,45592],{"class":47,"line":63},[45,45583,2065],{"class":92},[45,45585,96],{"class":85},[45,45587,45577],{"class":2070},[45,45589,240],{"class":85},[45,45591,10636],{"class":2070},[45,45593,10777],{"class":85},[45,45595,45596,45598,45600,45602,45604,45606],{"class":47,"line":70},[45,45597,2065],{"class":92},[45,45599,96],{"class":85},[45,45601,45577],{"class":2070},[45,45603,240],{"class":85},[45,45605,10790],{"class":2070},[45,45607,10777],{"class":85},[45,45609,45610,45612,45614,45616,45618,45620],{"class":47,"line":89},[45,45611,2065],{"class":92},[45,45613,96],{"class":85},[45,45615,45577],{"class":2070},[45,45617,240],{"class":85},[45,45619,9516],{"class":2070},[45,45621,10777],{"class":85},[45,45623,45624,45626,45628,45630,45632,45634],{"class":47,"line":112},[45,45625,2065],{"class":92},[45,45627,96],{"class":85},[45,45629,4340],{"class":2109},[45,45631,96],{"class":85},[45,45633,45577],{"class":2070},[45,45635,4348],{"class":85},[45,45637,45638,45640,45642,45644,45646,45648,45651,45654],{"class":47,"line":126},[45,45639,2065],{"class":92},[45,45641,96],{"class":85},[45,45643,7901],{"class":99},[45,45645,3096],{"class":103},[45,45647,7901],{"class":99},[45,45649,45650],{"class":73}," in",[45,45652,45653],{"class":2070}," my_dict",[45,45655,109],{"class":85},[14,45657,45658],{},"These are useful for spotting:",[195,45660,45661,45664,45667,45670],{},[198,45662,45663],{},"Misspelled keys",[198,45665,45666],{},"Wrong data type",[198,45668,45669],{},"Missing keys",[198,45671,45672],{},"Unexpected dictionary contents",[31,45674,2281],{"id":2280},[712,45676,45678],{"id":45677},"what-is-a-python-dictionary","What is a Python dictionary?",[14,45680,45681],{},"A dictionary is a mutable data type that stores values as key-value pairs.",[712,45683,45685],{"id":45684},"how-is-a-dictionary-different-from-a-list","How is a dictionary different from a list?",[14,45687,45688],{},"A list uses positions called indexes. A dictionary uses keys like names or labels.",[712,45690,45692],{"id":45691},"can-dictionary-keys-be-duplicated","Can dictionary keys be duplicated?",[14,45694,45695],{},"No. If the same key appears more than once, the later value replaces the earlier one.",[712,45697,45699],{"id":45698},"can-dictionary-values-have-different-data-types","Can dictionary values have different data types?",[14,45701,45702],{},"Yes. One dictionary can store strings, numbers, lists, or other dictionaries as values.",[712,45704,45706],{"id":45705},"how-do-i-safely-get-a-value-from-a-dictionary","How do I safely get a value from a dictionary?",[14,45708,165,45709,45711],{},[18,45710,43335],{},", because it does not raise an error when the key is missing.",[31,45713,2342],{"id":2341},[195,45715,45716,45721,45727,45733,45738],{},[198,45717,45718],{},[236,45719,45720],{"href":42645},"What is a dictionary in Python?",[198,45722,45723],{},[236,45724,43515,45725,2575],{"href":43514},[18,45726,43335],{},[198,45728,45729],{},[236,45730,45732],{"href":45731},"\u002Fhow-to\u002Fhow-to-add-a-key-to-a-dictionary-in-python\u002F","How to add a key to a dictionary in Python",[198,45734,45735],{},[236,45736,10922],{"href":45737},"\u002Fhow-to\u002Fhow-to-check-if-a-key-exists-in-a-dictionary-in-python\u002F",[198,45739,45740],{},[236,45741,45743],{"href":45742},"\u002Ferrors\u002Fkeyerror-when-accessing-dictionary-values-fix\u002F","KeyError when accessing dictionary values: fix",[2367,45745,45746],{},"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 .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}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .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":41,"searchDepth":63,"depth":63,"links":45748},[45749,45750,45751,45752,45757,45761,45762,45763,45768,45774,45775,45783,45784,45791],{"id":33,"depth":63,"text":34},{"id":42827,"depth":63,"text":42828},{"id":42907,"depth":63,"text":42908},{"id":43013,"depth":63,"text":43014,"children":45753},[45754,45755],{"id":43112,"depth":70,"text":43113},{"id":43150,"depth":70,"text":45756},"Using dict()",{"id":43252,"depth":63,"text":43253,"children":45758},[45759],{"id":43332,"depth":70,"text":45760},"Using get()",{"id":43524,"depth":63,"text":43525},{"id":43787,"depth":63,"text":43788},{"id":44185,"depth":63,"text":44186,"children":45764},[45765,45766,45767],{"id":44192,"depth":70,"text":44193},{"id":44279,"depth":70,"text":44280},{"id":44370,"depth":70,"text":44371},{"id":44518,"depth":63,"text":44519,"children":45769},[45770,45771,45772,45773],{"id":44547,"depth":70,"text":44529},{"id":44636,"depth":70,"text":44534},{"id":44654,"depth":70,"text":44539},{"id":44798,"depth":70,"text":44544},{"id":44870,"depth":63,"text":44871},{"id":45183,"depth":63,"text":45184,"children":45776},[45777,45778,45779,45780,45781],{"id":45190,"depth":70,"text":45191},{"id":45271,"depth":70,"text":45272},{"id":45354,"depth":70,"text":45355},{"id":45443,"depth":70,"text":45444},{"id":45549,"depth":70,"text":45782},"Using brackets instead of get() when a key may be missing",{"id":21690,"depth":63,"text":21691},{"id":2280,"depth":63,"text":2281,"children":45785},[45786,45787,45788,45789,45790],{"id":45677,"depth":70,"text":45678},{"id":45684,"depth":70,"text":45685},{"id":45691,"depth":70,"text":45692},{"id":45698,"depth":70,"text":45699},{"id":45705,"depth":70,"text":45706},{"id":2341,"depth":63,"text":2342},"Master python dictionaries explained in our comprehensive Python beginner guide.",{},{"title":42553,"description":45792},"learn\u002Fpython-dictionaries-explained","eGXWgpi8jYXPwwiN6jxOJh4ZLUZDeIIYzQfLbGbD2a4",{"id":45798,"title":45799,"body":45800,"description":47553,"extension":2399,"meta":47554,"navigation":66,"path":47555,"seo":47556,"stem":47557,"__hash__":47558},"content\u002Flearn\u002Fpython-errors-and-exceptions-explained.md","Python Errors and Exceptions Explained",{"type":7,"value":45801,"toc":47512},[45802,45805,45808,45810,45831,45833,45836,45934,45937,45967,45975,45977,45991,45995,45998,46001,46009,46011,46024,46027,46038,46042,46045,46048,46050,46069,46081,46083,46106,46114,46118,46121,46125,46128,46130,46141,46143,46168,46179,46187,46191,46194,46196,46206,46208,46231,46238,46242,46245,46248,46259,46261,46300,46303,46376,46380,46383,46400,46403,46416,46419,46465,46467,46478,46482,46485,46502,46515,46519,46522,46527,46530,46546,46553,46561,46566,46569,46593,46598,46604,46609,46612,46634,46642,46651,46656,46659,46702,46707,46710,46757,46765,46771,46774,46826,46832,46838,46841,46860,46865,46869,46877,46879,46959,46961,46979,46983,46986,46989,47006,47013,47017,47020,47023,47072,47075,47121,47128,47132,47138,47141,47173,47179,47185,47188,47190,47251,47256,47260,47263,47280,47284,47287,47334,47336,47361,47365,47368,47391,47395,47398,47401,47420,47422,47426,47429,47433,47436,47440,47443,47447,47450,47454,47470,47472,47509],[10,45803,45799],{"id":45804},"python-errors-and-exceptions-explained",[14,45806,45807],{},"Python errors and exceptions are messages that tell you something went wrong in your code. As a beginner, these messages can look confusing at first, but they are actually very useful.",[14,45809,19201],{},[195,45811,45812,45815,45818,45821,45828],{},[198,45813,45814],{},"what errors and exceptions are",[198,45816,45817],{},"how Python behaves when it finds a problem",[198,45819,45820],{},"how to read a traceback",[198,45822,45823,45824,1316,45826],{},"when to handle an exception with ",[18,45825,23272],{},[18,45827,23275],{},[198,45829,45830],{},"when you should fix the code instead",[31,45832,34],{"id":33},[14,45834,45835],{},"Use this pattern when user input or calculations might fail. It shows how exceptions can be caught without crashing the program.",[36,45837,45839],{"className":38,"code":45838,"language":40,"meta":41,"style":41},"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",[18,45840,45841,45847,45871,45886,45895,45910,45919],{"__ignoreMap":41},[45,45842,45843,45845],{"class":47,"line":48},[45,45844,23272],{"class":73},[45,45846,86],{"class":85},[45,45848,45849,45851,45853,45855,45857,45860,45862,45864,45867,45869],{"class":47,"line":63},[45,45850,12957],{"class":51},[45,45852,56],{"class":55},[45,45854,41669],{"class":2109},[45,45856,96],{"class":85},[45,45858,45859],{"class":92},"input",[45,45861,96],{"class":85},[45,45863,100],{"class":99},[45,45865,45866],{"class":103},"Enter a number: ",[45,45868,100],{"class":99},[45,45870,4348],{"class":85},[45,45872,45873,45875,45877,45879,45882,45884],{"class":47,"line":70},[45,45874,93],{"class":92},[45,45876,96],{"class":85},[45,45878,11786],{"class":59},[45,45880,45881],{"class":55}," \u002F",[45,45883,35957],{"class":2070},[45,45885,109],{"class":85},[45,45887,45888,45890,45893],{"class":47,"line":89},[45,45889,23275],{"class":73},[45,45891,45892],{"class":2109}," ValueError",[45,45894,86],{"class":85},[45,45896,45897,45899,45901,45903,45906,45908],{"class":47,"line":112},[45,45898,93],{"class":92},[45,45900,96],{"class":85},[45,45902,100],{"class":99},[45,45904,45905],{"class":103},"Please enter a valid whole number.",[45,45907,100],{"class":99},[45,45909,109],{"class":85},[45,45911,45912,45914,45917],{"class":47,"line":126},[45,45913,23275],{"class":73},[45,45915,45916],{"class":2109}," ZeroDivisionError",[45,45918,86],{"class":85},[45,45920,45921,45923,45925,45927,45930,45932],{"class":47,"line":142},[45,45922,93],{"class":92},[45,45924,96],{"class":85},[45,45926,100],{"class":99},[45,45928,45929],{"class":103},"You cannot divide by zero.",[45,45931,100],{"class":99},[45,45933,109],{"class":85},[14,45935,45936],{},"What this code does:",[195,45938,45939,45947,45955,45961],{},[198,45940,45941,45944,45945],{},[18,45942,45943],{},"int(input(...))"," can fail if the user types something like ",[18,45946,40627],{},[198,45948,45949,45952,45953],{},[18,45950,45951],{},"10 \u002F number"," can fail if the user enters ",[18,45954,10482],{},[198,45956,45957,45960],{},[18,45958,45959],{},"except ValueError"," handles bad number input",[198,45962,45963,45966],{},[18,45964,45965],{},"except ZeroDivisionError"," handles division by zero",[14,45968,45969,45970,1316,45972,45974],{},"Without ",[18,45971,23272],{},[18,45973,23275],{},", the program would stop with an error message.",[31,45976,2530],{"id":2529},[195,45978,45979,45982,45985,45988],{},[198,45980,45981],{},"Difference between an error and an exception in simple terms",[198,45983,45984],{},"How Python stops when it hits a problem",[198,45986,45987],{},"How to read a traceback and find the useful line",[198,45989,45990],{},"When to handle exceptions and when to fix the code directly",[31,45992,45994],{"id":45993},"what-is-an-error-in-python","What is an error in Python?",[14,45996,45997],{},"An error means something went wrong in your code.",[14,45999,46000],{},"In beginner Python, \"error\" is often used as a general word for any problem. That problem might happen:",[195,46002,46003,46006],{},[198,46004,46005],{},"before the program runs properly",[198,46007,46008],{},"while the program is running",[14,46010,2115],{},[195,46012,46013,46019],{},[198,46014,46015,46016],{},"a missing colon can cause a ",[18,46017,46018],{},"SyntaxError",[198,46020,46021,46022],{},"trying to use a variable that does not exist can cause a ",[18,46023,35168],{},[14,46025,46026],{},"When Python finds a problem, it usually shows:",[195,46028,46029,46032,46035],{},[198,46030,46031],{},"an error message",[198,46033,46034],{},"a traceback",[198,46036,46037],{},"the line where the problem happened",[31,46039,46041],{"id":46040},"what-is-an-exception","What is an exception?",[14,46043,46044],{},"An exception is a runtime problem. That means it happens after Python has already started running your code.",[14,46046,46047],{},"Python raises an exception when it cannot continue normally.",[14,46049,2115],{},[195,46051,46052,46056,46060,46064],{},[198,46053,46054],{},[18,46055,42097],{},[198,46057,46058],{},[18,46059,7711],{},[198,46061,46062],{},[18,46063,45197],{},[198,46065,46066],{},[18,46067,46068],{},"IndexError",[14,46070,46071,46072,1316,46074,46076,46077,240],{},"Many exceptions can be handled with ",[18,46073,23272],{},[18,46075,23275],{},". If you want to learn that syntax in more detail, see ",[236,46078,46080],{"href":46079},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python","using try-except, else, and finally in Python",[14,46082,466],{},[36,46084,46086],{"className":38,"code":46085,"language":40,"meta":41,"style":41},"age = int(\"hello\")\n",[18,46087,46088],{"__ignoreMap":41},[45,46089,46090,46092,46094,46096,46098,46100,46102,46104],{"class":47,"line":48},[45,46091,52],{"class":51},[45,46093,56],{"class":55},[45,46095,41669],{"class":2109},[45,46097,96],{"class":85},[45,46099,100],{"class":99},[45,46101,18782],{"class":103},[45,46103,100],{"class":99},[45,46105,109],{"class":85},[14,46107,40760,46108,46110,46111,46113],{},[18,46109,42097],{}," because ",[18,46112,40627],{}," is a string, but it is not a valid integer.",[31,46115,46117],{"id":46116},"errors-before-running-vs-errors-during-running","Errors before running vs errors during running",[14,46119,46120],{},"Some problems happen before your code can run normally. Others happen while it is already running.",[712,46122,46124],{"id":46123},"errors-before-normal-execution","Errors before normal execution",[14,46126,46127],{},"These happen when Python cannot understand your code.",[14,46129,42917],{},[195,46131,46132,46136],{},[198,46133,46134],{},[18,46135,46018],{},[198,46137,46138],{},[18,46139,46140],{},"IndentationError",[14,46142,466],{},[36,46144,46146],{"className":38,"code":46145,"language":40,"meta":41,"style":41},"if True\n    print(\"Hello\")\n",[18,46147,46148,46154],{"__ignoreMap":41},[45,46149,46150,46152],{"class":47,"line":48},[45,46151,20],{"class":73},[45,46153,482],{"class":481},[45,46155,46156,46158,46160,46162,46164,46166],{"class":47,"line":63},[45,46157,93],{"class":92},[45,46159,96],{"class":85},[45,46161,100],{"class":99},[45,46163,3226],{"class":103},[45,46165,100],{"class":99},[45,46167,109],{"class":85},[14,46169,46170,46171,46173,46174,46176,46177,240],{},"Python will report a ",[18,46172,46018],{}," because the colon ",[18,46175,457],{}," is missing after ",[18,46178,229],{},[14,46180,46181,46182,240],{},"If you are dealing with this specific problem, see ",[236,46183,4025,46185],{"href":46184},"\u002Ferrors\u002Fsyntaxerror-invalid-syntax-fix",[18,46186,18361],{},[712,46188,46190],{"id":46189},"errors-during-running","Errors during running",[14,46192,46193],{},"These happen after Python has started executing your code.",[14,46195,42917],{},[195,46197,46198,46202],{},[198,46199,46200],{},[18,46201,7711],{},[198,46203,46204],{},[18,46205,42097],{},[14,46207,466],{},[36,46209,46211],{"className":38,"code":46210,"language":40,"meta":41,"style":41},"number = int(\"abc\")\n",[18,46212,46213],{"__ignoreMap":41},[45,46214,46215,46217,46219,46221,46223,46225,46227,46229],{"class":47,"line":48},[45,46216,728],{"class":51},[45,46218,56],{"class":55},[45,46220,41669],{"class":2109},[45,46222,96],{"class":85},[45,46224,100],{"class":99},[45,46226,10304],{"class":103},[45,46228,100],{"class":99},[45,46230,109],{"class":85},[14,46232,46233,46234,46237],{},"This starts running, but fails when Python tries to convert ",[18,46235,46236],{},"\"abc\""," to an integer.",[31,46239,46241],{"id":46240},"how-to-read-a-python-traceback","How to read a Python traceback",[14,46243,46244],{},"A traceback is the block of text Python shows when an exception happens.",[14,46246,46247],{},"It tells you:",[195,46249,46250,46253,46256],{},[198,46251,46252],{},"where the error happened",[198,46254,46255],{},"what kind of exception occurred",[198,46257,46258],{},"sometimes why it happened",[14,46260,466],{},[36,46262,46264],{"className":38,"code":46263,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\nprint(numbers[5])\n",[18,46265,46266,46286],{"__ignoreMap":41},[45,46267,46268,46270,46272,46274,46276,46278,46280,46282,46284],{"class":47,"line":48},[45,46269,8570],{"class":51},[45,46271,56],{"class":55},[45,46273,8575],{"class":85},[45,46275,11786],{"class":59},[45,46277,3079],{"class":85},[45,46279,10503],{"class":59},[45,46281,3079],{"class":85},[45,46283,10512],{"class":59},[45,46285,8594],{"class":85},[45,46287,46288,46290,46292,46294,46296,46298],{"class":47,"line":63},[45,46289,2065],{"class":92},[45,46291,96],{"class":85},[45,46293,9069],{"class":2070},[45,46295,9680],{"class":85},[45,46297,3749],{"class":59},[45,46299,21941],{"class":85},[14,46301,46302],{},"You might see something like this:",[36,46304,46306],{"className":38,"code":46305,"language":40,"meta":41,"style":41},"Traceback (most recent call last):\n  File \"example.py\", line 2, in \u003Cmodule>\n    print(numbers[5])\nIndexError: list index out of range\n",[18,46307,46308,46320,46348,46362],{"__ignoreMap":41},[45,46309,46310,46313,46315,46318],{"class":47,"line":48},[45,46311,46312],{"class":2070},"Traceback ",[45,46314,96],{"class":85},[45,46316,46317],{"class":2070},"most recent call last",[45,46319,2464],{"class":85},[45,46321,46322,46325,46327,46330,46332,46334,46336,46338,46340,46342,46344,46346],{"class":47,"line":63},[45,46323,46324],{"class":51},"  File ",[45,46326,100],{"class":99},[45,46328,46329],{"class":103},"example.py",[45,46331,100],{"class":99},[45,46333,3079],{"class":85},[45,46335,24027],{"class":51},[45,46337,9367],{"class":59},[45,46339,3079],{"class":85},[45,46341,45650],{"class":55},[45,46343,34378],{"class":55},[45,46345,15006],{"class":51},[45,46347,13484],{"class":55},[45,46349,46350,46352,46354,46356,46358,46360],{"class":47,"line":70},[45,46351,93],{"class":92},[45,46353,96],{"class":85},[45,46355,9069],{"class":2070},[45,46357,9680],{"class":85},[45,46359,3749],{"class":59},[45,46361,21941],{"class":85},[45,46363,46364,46366,46368,46370,46373],{"class":47,"line":89},[45,46365,46068],{"class":2109},[45,46367,457],{"class":85},[45,46369,14381],{"class":2109},[45,46371,46372],{"class":51}," index out of ",[45,46374,46375],{"class":92},"range\n",[712,46377,46379],{"id":46378},"how-to-read-it","How to read it",[14,46381,46382],{},"Start with the last line first:",[36,46384,46386],{"className":38,"code":46385,"language":40,"meta":41,"style":41},"IndexError: list index out of range\n",[18,46387,46388],{"__ignoreMap":41},[45,46389,46390,46392,46394,46396,46398],{"class":47,"line":48},[45,46391,46068],{"class":2109},[45,46393,457],{"class":85},[45,46395,14381],{"class":2109},[45,46397,46372],{"class":51},[45,46399,46375],{"class":92},[14,46401,46402],{},"This tells you:",[195,46404,46405,46410],{},[198,46406,46407,46408],{},"the exception type is ",[18,46409,46068],{},[198,46411,46412,46413],{},"the message is ",[18,46414,46415],{},"list index out of range",[14,46417,46418],{},"Then look above it:",[36,46420,46422],{"className":38,"code":46421,"language":40,"meta":41,"style":41},"File \"example.py\", line 2, in \u003Cmodule>\n    print(numbers[5])\n",[18,46423,46424,46451],{"__ignoreMap":41},[45,46425,46426,46429,46431,46433,46435,46437,46439,46441,46443,46445,46447,46449],{"class":47,"line":48},[45,46427,46428],{"class":51},"File ",[45,46430,100],{"class":99},[45,46432,46329],{"class":103},[45,46434,100],{"class":99},[45,46436,3079],{"class":85},[45,46438,24027],{"class":51},[45,46440,9367],{"class":59},[45,46442,3079],{"class":85},[45,46444,45650],{"class":55},[45,46446,34378],{"class":55},[45,46448,15006],{"class":51},[45,46450,13484],{"class":55},[45,46452,46453,46455,46457,46459,46461,46463],{"class":47,"line":63},[45,46454,93],{"class":92},[45,46456,96],{"class":85},[45,46458,9069],{"class":2070},[45,46460,9680],{"class":85},[45,46462,3749],{"class":59},[45,46464,21941],{"class":85},[14,46466,46402],{},[195,46468,46469,46472,46475],{},[198,46470,46471],{},"the file name",[198,46473,46474],{},"the line number",[198,46476,46477],{},"the exact line that failed",[712,46479,46481],{"id":46480},"a-good-beginner-method","A good beginner method",[14,46483,46484],{},"When reading a traceback:",[346,46486,46487,46490,46493,46496,46499],{},[198,46488,46489],{},"Read the last line first",[198,46491,46492],{},"Find the file name and line number",[198,46494,46495],{},"Look at that line in your code",[198,46497,46498],{},"Check the values used on that line",[198,46500,46501],{},"Look one or two lines above it for the real cause",[14,46503,46504,46505,46507,46508,240],{},"If you see ",[18,46506,46068],{},", this page may help next: ",[236,46509,46511,46512],{"href":46510},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained","fix ",[18,46513,46514],{},"IndexError: list index out of range",[31,46516,46518],{"id":46517},"common-exception-types-beginners-should-know","Common exception types beginners should know",[14,46520,46521],{},"Here are some of the most common exceptions you will see early on.",[712,46523,46525],{"id":46524},"nameerror",[18,46526,35168],{},[14,46528,46529],{},"This happens when you use a variable or function name that does not exist.",[36,46531,46533],{"className":38,"code":46532,"language":40,"meta":41,"style":41},"print(score)\n",[18,46534,46535],{"__ignoreMap":41},[45,46536,46537,46539,46541,46544],{"class":47,"line":48},[45,46538,2065],{"class":92},[45,46540,96],{"class":85},[45,46542,46543],{"class":2070},"score",[45,46545,109],{"class":85},[14,46547,3259,46548,46550,46551,240],{},[18,46549,46543],{}," was never created, Python raises a ",[18,46552,35168],{},[14,46554,26005,46555,240],{},[236,46556,4025,46558],{"href":46557},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix",[18,46559,46560],{},"NameError: name is not defined",[712,46562,46564],{"id":46563},"typeerror",[18,46565,7711],{},[14,46567,46568],{},"This happens when you use the wrong data type in an operation.",[36,46570,46572],{"className":38,"code":46571,"language":40,"meta":41,"style":41},"print(\"Age: \" + 25)\n",[18,46573,46574],{"__ignoreMap":41},[45,46575,46576,46578,46580,46582,46585,46587,46589,46591],{"class":47,"line":48},[45,46577,2065],{"class":92},[45,46579,96],{"class":85},[45,46581,100],{"class":99},[45,46583,46584],{"class":103},"Age: ",[45,46586,100],{"class":99},[45,46588,3775],{"class":55},[45,46590,303],{"class":59},[45,46592,109],{"class":85},[14,46594,46595,46596,240],{},"You cannot add a string and an integer directly, so Python raises a ",[18,46597,7711],{},[14,46599,46600,46601,240],{},"Related help: fix ",[18,46602,46603],{},"TypeError: unsupported operand type(s) for +",[712,46605,46607],{"id":46606},"valueerror",[18,46608,42097],{},[14,46610,46611],{},"This happens when the type is correct, but the value is not valid.",[36,46613,46614],{"className":38,"code":46210,"language":40,"meta":41,"style":41},[18,46615,46616],{"__ignoreMap":41},[45,46617,46618,46620,46622,46624,46626,46628,46630,46632],{"class":47,"line":48},[45,46619,728],{"class":51},[45,46621,56],{"class":55},[45,46623,41669],{"class":2109},[45,46625,96],{"class":85},[45,46627,100],{"class":99},[45,46629,10304],{"class":103},[45,46631,100],{"class":99},[45,46633,109],{"class":85},[14,46635,46636,46638,46639,46641],{},[18,46637,46236],{}," is a string, which ",[18,46640,41891],{}," can accept, but its value is not a valid integer.",[14,46643,26005,46644,240],{},[236,46645,46647,46648,46650],{"href":46646},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes","common ",[18,46649,42097],{}," causes and fixes",[712,46652,46654],{"id":46653},"indexerror",[18,46655,46068],{},[14,46657,46658],{},"This happens when you use a list position that does not exist.",[36,46660,46662],{"className":38,"code":46661,"language":40,"meta":41,"style":41},"colors = [\"red\", \"blue\"]\nprint(colors[3])\n",[18,46663,46664,46688],{"__ignoreMap":41},[45,46665,46666,46668,46670,46672,46674,46676,46678,46680,46682,46684,46686],{"class":47,"line":48},[45,46667,22320],{"class":51},[45,46669,56],{"class":55},[45,46671,8575],{"class":85},[45,46673,100],{"class":99},[45,46675,22329],{"class":103},[45,46677,100],{"class":99},[45,46679,3079],{"class":85},[45,46681,1565],{"class":99},[45,46683,22347],{"class":103},[45,46685,100],{"class":99},[45,46687,8594],{"class":85},[45,46689,46690,46692,46694,46696,46698,46700],{"class":47,"line":63},[45,46691,2065],{"class":92},[45,46693,96],{"class":85},[45,46695,22407],{"class":2070},[45,46697,9680],{"class":85},[45,46699,11184],{"class":59},[45,46701,21941],{"class":85},[712,46703,46705],{"id":46704},"keyerror",[18,46706,45197],{},[14,46708,46709],{},"This happens when you use a dictionary key that does not exist.",[36,46711,46713],{"className":38,"code":46712,"language":40,"meta":41,"style":41},"user = {\"name\": \"Sam\"}\nprint(user[\"age\"])\n",[18,46714,46715,46739],{"__ignoreMap":41},[45,46716,46717,46719,46721,46723,46725,46727,46729,46731,46733,46735,46737],{"class":47,"line":48},[45,46718,30062],{"class":51},[45,46720,56],{"class":55},[45,46722,8604],{"class":85},[45,46724,100],{"class":99},[45,46726,3096],{"class":103},[45,46728,100],{"class":99},[45,46730,457],{"class":85},[45,46732,1565],{"class":99},[45,46734,1568],{"class":103},[45,46736,100],{"class":99},[45,46738,8629],{"class":85},[45,46740,46741,46743,46745,46747,46749,46751,46753,46755],{"class":47,"line":63},[45,46742,2065],{"class":92},[45,46744,96],{"class":85},[45,46746,30087],{"class":2070},[45,46748,9680],{"class":85},[45,46750,100],{"class":99},[45,46752,2082],{"class":103},[45,46754,100],{"class":99},[45,46756,21941],{"class":85},[14,46758,26005,46759,240],{},[236,46760,22116,46762,46764],{"href":46761},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes",[18,46763,45197],{}," happens and how to fix it",[712,46766,46768],{"id":46767},"filenotfounderror",[18,46769,46770],{},"FileNotFoundError",[14,46772,46773],{},"This happens when you try to open a file that does not exist at the given path.",[36,46775,46777],{"className":38,"code":46776,"language":40,"meta":41,"style":41},"with open(\"missing.txt\", \"r\") as file:\n    print(file.read())\n",[18,46778,46779,46810],{"__ignoreMap":41},[45,46780,46781,46783,46785,46787,46789,46792,46794,46796,46798,46800,46802,46804,46806,46808],{"class":47,"line":48},[45,46782,23989],{"class":73},[45,46784,23992],{"class":92},[45,46786,96],{"class":85},[45,46788,100],{"class":99},[45,46790,46791],{"class":103},"missing.txt",[45,46793,100],{"class":99},[45,46795,3079],{"class":85},[45,46797,1565],{"class":99},[45,46799,24008],{"class":103},[45,46801,100],{"class":99},[45,46803,9146],{"class":85},[45,46805,24015],{"class":73},[45,46807,15011],{"class":24018},[45,46809,86],{"class":85},[45,46811,46812,46814,46816,46819,46821,46824],{"class":47,"line":63},[45,46813,93],{"class":92},[45,46815,96],{"class":85},[45,46817,46818],{"class":24018},"file",[45,46820,240],{"class":85},[45,46822,46823],{"class":2070},"read",[45,46825,10777],{"class":85},[14,46827,26005,46828,240],{},[236,46829,46831],{"href":46830},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes","FileNotFoundError causes and fixes",[712,46833,46835],{"id":46834},"zerodivisionerror",[18,46836,46837],{},"ZeroDivisionError",[14,46839,46840],{},"This happens when you divide by zero.",[36,46842,46844],{"className":38,"code":46843,"language":40,"meta":41,"style":41},"print(10 \u002F 0)\n",[18,46845,46846],{"__ignoreMap":41},[45,46847,46848,46850,46852,46854,46856,46858],{"class":47,"line":48},[45,46849,2065],{"class":92},[45,46851,96],{"class":85},[45,46853,11786],{"class":59},[45,46855,45881],{"class":55},[45,46857,749],{"class":59},[45,46859,109],{"class":85},[14,46861,46862,46863,240],{},"Python cannot do that calculation, so it raises a ",[18,46864,46837],{},[31,46866,46868],{"id":46867},"basic-exception-handling","Basic exception handling",[14,46870,165,46871,46873,46874,46876],{},[18,46872,23272],{}," for code that might fail. Use ",[18,46875,23275],{}," to handle a specific problem.",[14,46878,466],{},[36,46880,46882],{"className":38,"code":46881,"language":40,"meta":41,"style":41},"try:\n    age = int(input(\"Enter your age: \"))\n    print(\"Next year you will be\", age + 1)\nexcept ValueError:\n    print(\"Please enter a valid number.\")\n",[18,46883,46884,46890,46913,46936,46944],{"__ignoreMap":41},[45,46885,46886,46888],{"class":47,"line":48},[45,46887,23272],{"class":73},[45,46889,86],{"class":85},[45,46891,46892,46895,46897,46899,46901,46903,46905,46907,46909,46911],{"class":47,"line":63},[45,46893,46894],{"class":51},"    age ",[45,46896,56],{"class":55},[45,46898,41669],{"class":2109},[45,46900,96],{"class":85},[45,46902,45859],{"class":92},[45,46904,96],{"class":85},[45,46906,100],{"class":99},[45,46908,39490],{"class":103},[45,46910,100],{"class":99},[45,46912,4348],{"class":85},[45,46914,46915,46917,46919,46921,46924,46926,46928,46930,46932,46934],{"class":47,"line":70},[45,46916,93],{"class":92},[45,46918,96],{"class":85},[45,46920,100],{"class":99},[45,46922,46923],{"class":103},"Next year you will be",[45,46925,100],{"class":99},[45,46927,3079],{"class":85},[45,46929,76],{"class":2070},[45,46931,8850],{"class":55},[45,46933,9030],{"class":59},[45,46935,109],{"class":85},[45,46937,46938,46940,46942],{"class":47,"line":89},[45,46939,23275],{"class":73},[45,46941,45892],{"class":2109},[45,46943,86],{"class":85},[45,46945,46946,46948,46950,46952,46955,46957],{"class":47,"line":112},[45,46947,93],{"class":92},[45,46949,96],{"class":85},[45,46951,100],{"class":99},[45,46953,46954],{"class":103},"Please enter a valid number.",[45,46956,100],{"class":99},[45,46958,109],{"class":85},[14,46960,9393],{},[195,46962,46963,46968,46976],{},[198,46964,46965,46966],{},"Python runs the code inside ",[18,46967,23272],{},[198,46969,46970,46971,46973,46974],{},"if a ",[18,46972,42097],{}," happens, Python jumps to ",[18,46975,23275],{},[198,46977,46978],{},"the program shows a helpful message instead of crashing",[712,46980,46982],{"id":46981},"why-this-is-useful","Why this is useful",[14,46984,46985],{},"Exception handling helps your program fail more gracefully.",[14,46987,46988],{},"This is especially helpful when working with:",[195,46990,46991,46994,46997,47000,47003],{},[198,46992,46993],{},"user input",[198,46995,46996],{},"files",[198,46998,46999],{},"dictionary keys",[198,47001,47002],{},"list indexes",[198,47004,47005],{},"number conversions",[14,47007,47008,47009,240],{},"A broader practical guide is available here: ",[236,47010,47012],{"href":47011},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python","how to handle exceptions in Python",[712,47014,47016],{"id":47015},"handle-specific-exceptions","Handle specific exceptions",[14,47018,47019],{},"Try to catch the exact exception you expect.",[14,47021,47022],{},"Good:",[36,47024,47026],{"className":38,"code":47025,"language":40,"meta":41,"style":41},"try:\n    number = int(user_input)\nexcept ValueError:\n    print(\"That was not a valid number.\")\n",[18,47027,47028,47034,47049,47057],{"__ignoreMap":41},[45,47029,47030,47032],{"class":47,"line":48},[45,47031,23272],{"class":73},[45,47033,86],{"class":85},[45,47035,47036,47038,47040,47042,47044,47047],{"class":47,"line":63},[45,47037,12957],{"class":51},[45,47039,56],{"class":55},[45,47041,41669],{"class":2109},[45,47043,96],{"class":85},[45,47045,47046],{"class":2070},"user_input",[45,47048,109],{"class":85},[45,47050,47051,47053,47055],{"class":47,"line":70},[45,47052,23275],{"class":73},[45,47054,45892],{"class":2109},[45,47056,86],{"class":85},[45,47058,47059,47061,47063,47065,47068,47070],{"class":47,"line":89},[45,47060,93],{"class":92},[45,47062,96],{"class":85},[45,47064,100],{"class":99},[45,47066,47067],{"class":103},"That was not a valid number.",[45,47069,100],{"class":99},[45,47071,109],{"class":85},[14,47073,47074],{},"Less helpful:",[36,47076,47078],{"className":38,"code":47077,"language":40,"meta":41,"style":41},"try:\n    number = int(user_input)\nexcept:\n    print(\"Something went wrong.\")\n",[18,47079,47080,47086,47100,47106],{"__ignoreMap":41},[45,47081,47082,47084],{"class":47,"line":48},[45,47083,23272],{"class":73},[45,47085,86],{"class":85},[45,47087,47088,47090,47092,47094,47096,47098],{"class":47,"line":63},[45,47089,12957],{"class":51},[45,47091,56],{"class":55},[45,47093,41669],{"class":2109},[45,47095,96],{"class":85},[45,47097,47046],{"class":2070},[45,47099,109],{"class":85},[45,47101,47102,47104],{"class":47,"line":70},[45,47103,23275],{"class":73},[45,47105,86],{"class":85},[45,47107,47108,47110,47112,47114,47117,47119],{"class":47,"line":89},[45,47109,93],{"class":92},[45,47111,96],{"class":85},[45,47113,100],{"class":99},[45,47115,47116],{"class":103},"Something went wrong.",[45,47118,100],{"class":99},[45,47120,109],{"class":85},[14,47122,47123,47124,47127],{},"A bare ",[18,47125,47126],{},"except:"," catches almost everything, which can hide real bugs.",[31,47129,47131],{"id":47130},"when-not-to-use-try-except","When not to use try-except",[14,47133,26213,47134,47137],{},[18,47135,47136],{},"try-except"," to hide mistakes in your code.",[14,47139,47140],{},"For example, this is not a good fix:",[36,47142,47144],{"className":38,"code":47143,"language":40,"meta":41,"style":41},"try:\n    print(total_price)\nexcept:\n    pass\n",[18,47145,47146,47152,47163,47169],{"__ignoreMap":41},[45,47147,47148,47150],{"class":47,"line":48},[45,47149,23272],{"class":73},[45,47151,86],{"class":85},[45,47153,47154,47156,47158,47161],{"class":47,"line":63},[45,47155,93],{"class":92},[45,47157,96],{"class":85},[45,47159,47160],{"class":2070},"total_price",[45,47162,109],{"class":85},[45,47164,47165,47167],{"class":47,"line":70},[45,47166,23275],{"class":73},[45,47168,86],{"class":85},[45,47170,47171],{"class":47,"line":89},[45,47172,5306],{"class":73},[14,47174,47175,47176,47178],{},"This hides the problem instead of solving it. If ",[18,47177,47160],{}," was never defined, the real fix is to define it correctly.",[14,47180,47181,47182,47184],{},"Avoid broad ",[18,47183,23275],{}," blocks unless you have a clear reason and you are handling the error properly.",[14,47186,47187],{},"Sometimes a simple check is clearer than exception handling.",[14,47189,466],{},[36,47191,47193],{"className":38,"code":47192,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\n\nif len(numbers) > 2:\n    print(numbers[2])\n",[18,47194,47195,47215,47219,47237],{"__ignoreMap":41},[45,47196,47197,47199,47201,47203,47205,47207,47209,47211,47213],{"class":47,"line":48},[45,47198,8570],{"class":51},[45,47200,56],{"class":55},[45,47202,8575],{"class":85},[45,47204,8578],{"class":59},[45,47206,3079],{"class":85},[45,47208,3754],{"class":59},[45,47210,3079],{"class":85},[45,47212,777],{"class":59},[45,47214,8594],{"class":85},[45,47216,47217],{"class":47,"line":63},[45,47218,67],{"emptyLinePlaceholder":66},[45,47220,47221,47223,47225,47227,47229,47231,47233,47235],{"class":47,"line":70},[45,47222,20],{"class":73},[45,47224,9139],{"class":92},[45,47226,96],{"class":85},[45,47228,9069],{"class":2070},[45,47230,9146],{"class":85},[45,47232,33591],{"class":55},[45,47234,3754],{"class":59},[45,47236,86],{"class":85},[45,47238,47239,47241,47243,47245,47247,47249],{"class":47,"line":89},[45,47240,93],{"class":92},[45,47242,96],{"class":85},[45,47244,9069],{"class":2070},[45,47246,9680],{"class":85},[45,47248,9367],{"class":59},[45,47250,21941],{"class":85},[14,47252,47253,47254,240],{},"This can be easier to understand than catching an ",[18,47255,46068],{},[31,47257,47259],{"id":47258},"beginner-debugging-steps","Beginner debugging steps",[14,47261,47262],{},"When you get an error, use this simple process:",[346,47264,47265,47268,47271,47274,47277],{},[198,47266,47267],{},"Read the full message carefully",[198,47269,47270],{},"Check the exact line number",[198,47272,47273],{},"Print variable values before the failing line",[198,47275,47276],{},"Reduce the code to a small example",[198,47278,47279],{},"Test one fix at a time",[712,47281,47283],{"id":47282},"useful-debugging-commands","Useful debugging commands",[14,47285,47286],{},"These basic tools can help:",[36,47288,47290],{"className":38,"code":47289,"language":40,"meta":41,"style":41},"print(variable)\ntype(variable)\nhelp(Exception)\ndir(object)\n",[18,47291,47292,47303,47313,47324],{"__ignoreMap":41},[45,47293,47294,47296,47298,47301],{"class":47,"line":48},[45,47295,2065],{"class":92},[45,47297,96],{"class":85},[45,47299,47300],{"class":2070},"variable",[45,47302,109],{"class":85},[45,47304,47305,47307,47309,47311],{"class":47,"line":63},[45,47306,4340],{"class":2109},[45,47308,96],{"class":85},[45,47310,47300],{"class":2070},[45,47312,109],{"class":85},[45,47314,47315,47317,47319,47322],{"class":47,"line":70},[45,47316,2104],{"class":92},[45,47318,96],{"class":85},[45,47320,47321],{"class":2109},"Exception",[45,47323,109],{"class":85},[45,47325,47326,47328,47330,47332],{"class":47,"line":89},[45,47327,4357],{"class":92},[45,47329,96],{"class":85},[45,47331,13471],{"class":2109},[45,47333,109],{"class":85},[14,47335,21783],{},[195,47337,47338,47344,47350,47356],{},[198,47339,47340,47343],{},[18,47341,47342],{},"print(variable)"," shows the current value",[198,47345,47346,47349],{},[18,47347,47348],{},"type(variable)"," shows the data type",[198,47351,47352,47355],{},[18,47353,47354],{},"help(Exception)"," shows information about exceptions",[198,47357,47358,4407],{},[18,47359,47360],{},"dir(object)",[712,47362,47364],{"id":47363},"common-causes-of-errors","Common causes of errors",[14,47366,47367],{},"Many beginner errors come from a small set of problems:",[195,47369,47370,47373,47376,47379,47382,47385,47388],{},[198,47371,47372],{},"typos in variable or function names",[198,47374,47375],{},"wrong indentation",[198,47377,47378],{},"using the wrong data type",[198,47380,47381],{},"accessing missing list indexes or dictionary keys",[198,47383,47384],{},"bad user input",[198,47386,47387],{},"missing files or wrong file paths",[198,47389,47390],{},"dividing by zero",[31,47392,47394],{"id":47393},"what-to-learn-next","What to learn next",[14,47396,47397],{},"Once you understand the basics, the next step is to practice handling exceptions yourself.",[14,47399,47400],{},"A good path is:",[195,47402,47403,47411,47414,47417],{},[198,47404,47405,47406,1316,47408,47410],{},"learn ",[18,47407,23272],{},[18,47409,23275],{}," syntax in more detail",[198,47412,47413],{},"read the page for the exact error message you see",[198,47415,47416],{},"learn common built-in exceptions one by one",[198,47418,47419],{},"practice with small examples that trigger real errors",[31,47421,2281],{"id":2280},[712,47423,47425],{"id":47424},"what-is-the-difference-between-an-error-and-an-exception-in-python","What is the difference between an error and an exception in Python?",[14,47427,47428],{},"An error is a general problem in code. An exception is a specific runtime problem that Python can raise while the program is running.",[712,47430,47432],{"id":47431},"is-syntaxerror-an-exception","Is SyntaxError an exception?",[14,47434,47435],{},"It is an error type reported by Python when the code cannot be parsed correctly. Beginners usually treat it as a code mistake found before normal execution.",[712,47437,47439],{"id":47438},"should-i-use-try-except-everywhere","Should I use try-except everywhere?",[14,47441,47442],{},"No. Use it only where failure is expected or possible. If the code itself is incorrect, fix the code instead.",[712,47444,47446],{"id":47445},"what-part-of-the-traceback-should-i-read-first","What part of the traceback should I read first?",[14,47448,47449],{},"Start with the last line because it shows the exception type and message. Then check the line number in your code.",[712,47451,47453],{"id":47452},"what-are-the-most-common-python-exceptions-for-beginners","What are the most common Python exceptions for beginners?",[14,47455,47456,21,47458,21,47460,21,47462,21,47464,21,47466,25,47468,240],{},[18,47457,35168],{},[18,47459,7711],{},[18,47461,42097],{},[18,47463,46068],{},[18,47465,45197],{},[18,47467,46770],{},[18,47469,46837],{},[31,47471,2342],{"id":2341},[195,47473,47474,47480,47485,47490,47496,47502],{},[198,47475,47476],{},[236,47477,47479],{"href":47478},"\u002Ferrors\u002Fcommon-python-errors-explained-beginner-guide","Common Python errors explained for beginners",[198,47481,47482],{},[236,47483,47484],{"href":46079},"Using try-except, else, and finally in Python",[198,47486,47487],{},[236,47488,47489],{"href":47011},"How to handle exceptions in Python",[198,47491,47492],{},[236,47493,4497,47494],{"href":46184},[18,47495,18361],{},[198,47497,47498],{},[236,47499,4497,47500],{"href":46557},[18,47501,46560],{},[198,47503,47504],{},[236,47505,47506,47507,46650],{"href":46646},"Common ",[18,47508,42097],{},[2367,47510,47511],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":41,"searchDepth":63,"depth":63,"links":47513},[47514,47515,47516,47517,47518,47522,47526,47535,47539,47540,47544,47545,47552],{"id":33,"depth":63,"text":34},{"id":2529,"depth":63,"text":2530},{"id":45993,"depth":63,"text":45994},{"id":46040,"depth":63,"text":46041},{"id":46116,"depth":63,"text":46117,"children":47519},[47520,47521],{"id":46123,"depth":70,"text":46124},{"id":46189,"depth":70,"text":46190},{"id":46240,"depth":63,"text":46241,"children":47523},[47524,47525],{"id":46378,"depth":70,"text":46379},{"id":46480,"depth":70,"text":46481},{"id":46517,"depth":63,"text":46518,"children":47527},[47528,47529,47530,47531,47532,47533,47534],{"id":46524,"depth":70,"text":35168},{"id":46563,"depth":70,"text":7711},{"id":46606,"depth":70,"text":42097},{"id":46653,"depth":70,"text":46068},{"id":46704,"depth":70,"text":45197},{"id":46767,"depth":70,"text":46770},{"id":46834,"depth":70,"text":46837},{"id":46867,"depth":63,"text":46868,"children":47536},[47537,47538],{"id":46981,"depth":70,"text":46982},{"id":47015,"depth":70,"text":47016},{"id":47130,"depth":63,"text":47131},{"id":47258,"depth":63,"text":47259,"children":47541},[47542,47543],{"id":47282,"depth":70,"text":47283},{"id":47363,"depth":70,"text":47364},{"id":47393,"depth":63,"text":47394},{"id":2280,"depth":63,"text":2281,"children":47546},[47547,47548,47549,47550,47551],{"id":47424,"depth":70,"text":47425},{"id":47431,"depth":70,"text":47432},{"id":47438,"depth":70,"text":47439},{"id":47445,"depth":70,"text":47446},{"id":47452,"depth":70,"text":47453},{"id":2341,"depth":63,"text":2342},"Master python errors and exceptions explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-errors-and-exceptions-explained",{"title":45799,"description":47553},"learn\u002Fpython-errors-and-exceptions-explained","tc4jLPL2R9-JmAnA_it9GlC3IRSSLtE4qzlyN9i1GYI",{"id":47560,"title":47561,"body":47562,"description":49341,"extension":2399,"meta":49342,"navigation":66,"path":49343,"seo":49344,"stem":49345,"__hash__":49346},"content\u002Flearn\u002Fpython-file-handling-basics-read-and-write.md","Python File Handling Basics (Read and Write)",{"type":7,"value":47563,"toc":49293},[47564,47567,47570,47573,47587,47597,47599,47606,47726,47728,47744,47746,47772,47776,47779,47797,47800,47814,47824,47828,47831,47859,47862,47917,47923,47927,47930,47936,47941,48000,48002,48012,48018,48023,48077,48079,48087,48094,48099,48155,48157,48165,48172,48179,48184,48239,48241,48246,48249,48256,48261,48320,48323,48334,48343,48410,48415,48419,48422,48428,48433,48491,48498,48503,48565,48572,48577,48639,48641,48672,48675,48692,48703,48707,48712,48719,48796,48802,48808,48864,48867,48871,48876,48879,48946,48953,48957,48960,48979,48983,49007,49011,49014,49113,49115,49147,49162,49166,49169,49171,49191,49193,49197,49203,49211,49219,49226,49232,49236,49239,49246,49252,49254,49290],[10,47565,47561],{"id":47566},"python-file-handling-basics-read-and-write",[14,47568,47569],{},"File handling in Python means working with files saved on your computer.",[14,47571,47572],{},"At the beginner level, this usually means:",[195,47574,47575,47578,47581,47584],{},[198,47576,47577],{},"opening a file",[198,47579,47580],{},"reading data from it",[198,47582,47583],{},"writing data to it",[198,47585,47586],{},"closing it safely",[14,47588,47589,47590,47593,47594,47596],{},"Python makes this simple with the built-in ",[18,47591,47592],{},"open()"," function. This page focuses on the core ideas only. If you need a specific task, such as reading line by line or checking whether a file exists, use the related guides in the ",[2421,47595,2342],{}," section.",[31,47598,34],{"id":33},[14,47600,47601,47602,47605],{},"The safest beginner pattern is to use ",[18,47603,47604],{},"with open(...)",". Python will close the file automatically when the block ends.",[36,47607,47609],{"className":38,"code":47608,"language":40,"meta":41,"style":41},"with open(\"example.txt\", \"w\") as file:\n    file.write(\"Hello, file!\\n\")\n\nwith open(\"example.txt\", \"r\") as file:\n    content = file.read()\n    print(content)\n",[18,47610,47611,47642,47666,47670,47700,47715],{"__ignoreMap":41},[45,47612,47613,47615,47617,47619,47621,47623,47625,47627,47629,47632,47634,47636,47638,47640],{"class":47,"line":48},[45,47614,23989],{"class":73},[45,47616,23992],{"class":92},[45,47618,96],{"class":85},[45,47620,100],{"class":99},[45,47622,23999],{"class":103},[45,47624,100],{"class":99},[45,47626,3079],{"class":85},[45,47628,1565],{"class":99},[45,47630,47631],{"class":103},"w",[45,47633,100],{"class":99},[45,47635,9146],{"class":85},[45,47637,24015],{"class":73},[45,47639,15011],{"class":24018},[45,47641,86],{"class":85},[45,47643,47644,47647,47649,47652,47654,47656,47659,47662,47664],{"class":47,"line":63},[45,47645,47646],{"class":24018},"    file",[45,47648,240],{"class":85},[45,47650,47651],{"class":2070},"write",[45,47653,96],{"class":85},[45,47655,100],{"class":99},[45,47657,47658],{"class":103},"Hello, file!",[45,47660,47661],{"class":2771},"\\n",[45,47663,100],{"class":99},[45,47665,109],{"class":85},[45,47667,47668],{"class":47,"line":70},[45,47669,67],{"emptyLinePlaceholder":66},[45,47671,47672,47674,47676,47678,47680,47682,47684,47686,47688,47690,47692,47694,47696,47698],{"class":47,"line":89},[45,47673,23989],{"class":73},[45,47675,23992],{"class":92},[45,47677,96],{"class":85},[45,47679,100],{"class":99},[45,47681,23999],{"class":103},[45,47683,100],{"class":99},[45,47685,3079],{"class":85},[45,47687,1565],{"class":99},[45,47689,24008],{"class":103},[45,47691,100],{"class":99},[45,47693,9146],{"class":85},[45,47695,24015],{"class":73},[45,47697,15011],{"class":24018},[45,47699,86],{"class":85},[45,47701,47702,47705,47707,47709,47711,47713],{"class":47,"line":112},[45,47703,47704],{"class":51},"    content ",[45,47706,56],{"class":55},[45,47708,15011],{"class":24018},[45,47710,240],{"class":85},[45,47712,46823],{"class":2070},[45,47714,2495],{"class":85},[45,47716,47717,47719,47721,47724],{"class":47,"line":126},[45,47718,93],{"class":92},[45,47720,96],{"class":85},[45,47722,47723],{"class":2070},"content",[45,47725,109],{"class":85},[14,47727,17038],{},[36,47729,47731],{"className":38,"code":47730,"language":40,"meta":41,"style":41},"Hello, file!\n",[18,47732,47733],{"__ignoreMap":41},[45,47734,47735,47737,47739,47741],{"class":47,"line":48},[45,47736,3226],{"class":51},[45,47738,3079],{"class":85},[45,47740,15011],{"class":24018},[45,47742,47743],{"class":51},"!\n",[14,47745,6570],{},[195,47747,47748,47754,47760,47766],{},[198,47749,47750,47753],{},[18,47751,47752],{},"\"w\""," opens the file for writing",[198,47755,47756,47759],{},[18,47757,47758],{},"file.write(...)"," saves text to the file",[198,47761,47762,47765],{},[18,47763,47764],{},"\"r\""," opens the file for reading",[198,47767,47768,47771],{},[18,47769,47770],{},"file.read()"," gets the file content as a string",[31,47773,47775],{"id":47774},"what-file-handling-means-in-python","What file handling means in Python",[14,47777,47778],{},"A file is data stored on your computer, such as:",[195,47780,47781,47786,47791,47794],{},[198,47782,11541,47783,15011],{},[18,47784,47785],{},".txt",[198,47787,11541,47788,15011],{},[18,47789,47790],{},".csv",[198,47792,47793],{},"a log file",[198,47795,47796],{},"a configuration file",[14,47798,47799],{},"File handling means:",[346,47801,47802,47805,47808,47811],{},[198,47803,47804],{},"choosing a file",[198,47806,47807],{},"opening it",[198,47809,47810],{},"reading or writing data",[198,47812,47813],{},"closing it when finished",[14,47815,47816,47817,47819,47820,240],{},"Python uses the built-in ",[18,47818,47592],{}," function for this. If you want a full function guide, see ",[236,47821,47823],{"href":47822},"\u002Freference\u002Fpython-open-function-explained\u002F","Python open() function explained",[31,47825,47827],{"id":47826},"the-basic-file-workflow","The basic file workflow",[14,47829,47830],{},"Most file code follows the same steps:",[346,47832,47833,47836,47841,47844,47853],{},[198,47834,47835],{},"Choose the file path you want to use.",[198,47837,47838,47839,240],{},"Open the file with ",[18,47840,47592],{},[198,47842,47843],{},"Pick a mode such as read or write.",[198,47845,47846,47847,230,47850,240],{},"Use methods like ",[18,47848,47849],{},"read()",[18,47851,47852],{},"write()",[198,47854,47855,47856,47858],{},"Close the file, or use ",[18,47857,23989],{}," so it closes automatically.",[14,47860,47861],{},"A simple example:",[36,47863,47865],{"className":38,"code":47864,"language":40,"meta":41,"style":41},"with open(\"notes.txt\", \"w\") as file:\n    file.write(\"Learn Python file handling\")\n",[18,47866,47867,47898],{"__ignoreMap":41},[45,47868,47869,47871,47873,47875,47877,47880,47882,47884,47886,47888,47890,47892,47894,47896],{"class":47,"line":48},[45,47870,23989],{"class":73},[45,47872,23992],{"class":92},[45,47874,96],{"class":85},[45,47876,100],{"class":99},[45,47878,47879],{"class":103},"notes.txt",[45,47881,100],{"class":99},[45,47883,3079],{"class":85},[45,47885,1565],{"class":99},[45,47887,47631],{"class":103},[45,47889,100],{"class":99},[45,47891,9146],{"class":85},[45,47893,24015],{"class":73},[45,47895,15011],{"class":24018},[45,47897,86],{"class":85},[45,47899,47900,47902,47904,47906,47908,47910,47913,47915],{"class":47,"line":63},[45,47901,47646],{"class":24018},[45,47903,240],{"class":85},[45,47905,47651],{"class":2070},[45,47907,96],{"class":85},[45,47909,100],{"class":99},[45,47911,47912],{"class":103},"Learn Python file handling",[45,47914,100],{"class":99},[45,47916,109],{"class":85},[14,47918,47919,47920,47922],{},"This creates or replaces ",[18,47921,47879],{}," and writes one line of text into it.",[31,47924,47926],{"id":47925},"understanding-file-modes","Understanding file modes",[14,47928,47929],{},"When you open a file, you choose a mode. The mode tells Python what you want to do with the file.",[712,47931,47933,47935],{"id":47932},"r-read-mode",[18,47934,47764],{}," read mode",[14,47937,165,47938,47940],{},[18,47939,47764],{}," to read an existing file.",[36,47942,47944],{"className":38,"code":47943,"language":40,"meta":41,"style":41},"with open(\"notes.txt\", \"r\") as file:\n    content = file.read()\n    print(content)\n",[18,47945,47946,47976,47990],{"__ignoreMap":41},[45,47947,47948,47950,47952,47954,47956,47958,47960,47962,47964,47966,47968,47970,47972,47974],{"class":47,"line":48},[45,47949,23989],{"class":73},[45,47951,23992],{"class":92},[45,47953,96],{"class":85},[45,47955,100],{"class":99},[45,47957,47879],{"class":103},[45,47959,100],{"class":99},[45,47961,3079],{"class":85},[45,47963,1565],{"class":99},[45,47965,24008],{"class":103},[45,47967,100],{"class":99},[45,47969,9146],{"class":85},[45,47971,24015],{"class":73},[45,47973,15011],{"class":24018},[45,47975,86],{"class":85},[45,47977,47978,47980,47982,47984,47986,47988],{"class":47,"line":63},[45,47979,47704],{"class":51},[45,47981,56],{"class":55},[45,47983,15011],{"class":24018},[45,47985,240],{"class":85},[45,47987,46823],{"class":2070},[45,47989,2495],{"class":85},[45,47991,47992,47994,47996,47998],{"class":47,"line":70},[45,47993,93],{"class":92},[45,47995,96],{"class":85},[45,47997,47723],{"class":2070},[45,47999,109],{"class":85},[14,48001,21487],{},[195,48003,48004,48007],{},[198,48005,48006],{},"the file must already exist",[198,48008,48009,48010],{},"if it does not exist, Python raises ",[18,48011,46770],{},[712,48013,48015,48017],{"id":48014},"w-write-mode",[18,48016,47752],{}," write mode",[14,48019,165,48020,48022],{},[18,48021,47752],{}," to write to a file.",[36,48024,48026],{"className":38,"code":48025,"language":40,"meta":41,"style":41},"with open(\"notes.txt\", \"w\") as file:\n    file.write(\"New content\")\n",[18,48027,48028,48058],{"__ignoreMap":41},[45,48029,48030,48032,48034,48036,48038,48040,48042,48044,48046,48048,48050,48052,48054,48056],{"class":47,"line":48},[45,48031,23989],{"class":73},[45,48033,23992],{"class":92},[45,48035,96],{"class":85},[45,48037,100],{"class":99},[45,48039,47879],{"class":103},[45,48041,100],{"class":99},[45,48043,3079],{"class":85},[45,48045,1565],{"class":99},[45,48047,47631],{"class":103},[45,48049,100],{"class":99},[45,48051,9146],{"class":85},[45,48053,24015],{"class":73},[45,48055,15011],{"class":24018},[45,48057,86],{"class":85},[45,48059,48060,48062,48064,48066,48068,48070,48073,48075],{"class":47,"line":63},[45,48061,47646],{"class":24018},[45,48063,240],{"class":85},[45,48065,47651],{"class":2070},[45,48067,96],{"class":85},[45,48069,100],{"class":99},[45,48071,48072],{"class":103},"New content",[45,48074,100],{"class":99},[45,48076,109],{"class":85},[14,48078,21487],{},[195,48080,48081,48084],{},[198,48082,48083],{},"if the file does not exist, Python creates it",[198,48085,48086],{},"if the file already exists, old content is erased",[712,48088,48090,48093],{"id":48089},"a-append-mode",[18,48091,48092],{},"\"a\""," append mode",[14,48095,165,48096,48098],{},[18,48097,48092],{}," to add content to the end of a file.",[36,48100,48102],{"className":38,"code":48101,"language":40,"meta":41,"style":41},"with open(\"notes.txt\", \"a\") as file:\n    file.write(\"\\nAnother line\")\n",[18,48103,48104,48134],{"__ignoreMap":41},[45,48105,48106,48108,48110,48112,48114,48116,48118,48120,48122,48124,48126,48128,48130,48132],{"class":47,"line":48},[45,48107,23989],{"class":73},[45,48109,23992],{"class":92},[45,48111,96],{"class":85},[45,48113,100],{"class":99},[45,48115,47879],{"class":103},[45,48117,100],{"class":99},[45,48119,3079],{"class":85},[45,48121,1565],{"class":99},[45,48123,236],{"class":103},[45,48125,100],{"class":99},[45,48127,9146],{"class":85},[45,48129,24015],{"class":73},[45,48131,15011],{"class":24018},[45,48133,86],{"class":85},[45,48135,48136,48138,48140,48142,48144,48146,48148,48151,48153],{"class":47,"line":63},[45,48137,47646],{"class":24018},[45,48139,240],{"class":85},[45,48141,47651],{"class":2070},[45,48143,96],{"class":85},[45,48145,100],{"class":99},[45,48147,47661],{"class":2771},[45,48149,48150],{"class":103},"Another line",[45,48152,100],{"class":99},[45,48154,109],{"class":85},[14,48156,21487],{},[195,48158,48159,48162],{},[198,48160,48161],{},"existing content stays",[198,48163,48164],{},"new content is added at the end",[14,48166,48167,48168,240],{},"If you want more practice with this, see ",[236,48169,48171],{"href":48170},"\u002Fhow-to\u002Fhow-to-append-to-a-file-in-python\u002F","How to append to a file in Python",[712,48173,48175,48178],{"id":48174},"x-create-mode",[18,48176,48177],{},"\"x\""," create mode",[14,48180,165,48181,48183],{},[18,48182,48177],{}," to create a brand-new file.",[36,48185,48187],{"className":38,"code":48186,"language":40,"meta":41,"style":41},"with open(\"new_file.txt\", \"x\") as file:\n    file.write(\"Created for the first time\")\n",[18,48188,48189,48220],{"__ignoreMap":41},[45,48190,48191,48193,48195,48197,48199,48202,48204,48206,48208,48210,48212,48214,48216,48218],{"class":47,"line":48},[45,48192,23989],{"class":73},[45,48194,23992],{"class":92},[45,48196,96],{"class":85},[45,48198,100],{"class":99},[45,48200,48201],{"class":103},"new_file.txt",[45,48203,100],{"class":99},[45,48205,3079],{"class":85},[45,48207,1565],{"class":99},[45,48209,26156],{"class":103},[45,48211,100],{"class":99},[45,48213,9146],{"class":85},[45,48215,24015],{"class":73},[45,48217,15011],{"class":24018},[45,48219,86],{"class":85},[45,48221,48222,48224,48226,48228,48230,48232,48235,48237],{"class":47,"line":63},[45,48223,47646],{"class":24018},[45,48225,240],{"class":85},[45,48227,47651],{"class":2070},[45,48229,96],{"class":85},[45,48231,100],{"class":99},[45,48233,48234],{"class":103},"Created for the first time",[45,48236,100],{"class":99},[45,48238,109],{"class":85},[14,48240,21487],{},[195,48242,48243],{},[198,48244,48245],{},"this fails if the file already exists",[14,48247,48248],{},"For beginner examples, text mode is usually enough. That is the default, so you do not need to add anything extra for normal text files.",[31,48250,48252,48253],{"id":48251},"why-use-with-open","Why use ",[18,48254,48255],{},"with open()",[14,48257,25587,48258,48260],{},[18,48259,48255],{}," is the best pattern to learn first.",[36,48262,48264],{"className":38,"code":48263,"language":40,"meta":41,"style":41},"with open(\"example.txt\", \"r\") as file:\n    content = file.read()\n    print(content)\n",[18,48265,48266,48296,48310],{"__ignoreMap":41},[45,48267,48268,48270,48272,48274,48276,48278,48280,48282,48284,48286,48288,48290,48292,48294],{"class":47,"line":48},[45,48269,23989],{"class":73},[45,48271,23992],{"class":92},[45,48273,96],{"class":85},[45,48275,100],{"class":99},[45,48277,23999],{"class":103},[45,48279,100],{"class":99},[45,48281,3079],{"class":85},[45,48283,1565],{"class":99},[45,48285,24008],{"class":103},[45,48287,100],{"class":99},[45,48289,9146],{"class":85},[45,48291,24015],{"class":73},[45,48293,15011],{"class":24018},[45,48295,86],{"class":85},[45,48297,48298,48300,48302,48304,48306,48308],{"class":47,"line":63},[45,48299,47704],{"class":51},[45,48301,56],{"class":55},[45,48303,15011],{"class":24018},[45,48305,240],{"class":85},[45,48307,46823],{"class":2070},[45,48309,2495],{"class":85},[45,48311,48312,48314,48316,48318],{"class":47,"line":70},[45,48313,93],{"class":92},[45,48315,96],{"class":85},[45,48317,47723],{"class":2070},[45,48319,109],{"class":85},[14,48321,48322],{},"Why this is recommended:",[195,48324,48325,48328,48331],{},[198,48326,48327],{},"it closes the file automatically",[198,48329,48330],{},"it helps prevent mistakes",[198,48332,48333],{},"it keeps code shorter and cleaner",[14,48335,45969,48336,48338,48339,48342],{},[18,48337,23989],{},", you would need to remember to call ",[18,48340,48341],{},"close()"," yourself:",[36,48344,48346],{"className":38,"code":48345,"language":40,"meta":41,"style":41},"file = open(\"example.txt\", \"r\")\ncontent = file.read()\nprint(content)\nfile.close()\n",[18,48347,48348,48374,48389,48399],{"__ignoreMap":41},[45,48349,48350,48352,48354,48356,48358,48360,48362,48364,48366,48368,48370,48372],{"class":47,"line":48},[45,48351,46818],{"class":24018},[45,48353,3099],{"class":55},[45,48355,23992],{"class":92},[45,48357,96],{"class":85},[45,48359,100],{"class":99},[45,48361,23999],{"class":103},[45,48363,100],{"class":99},[45,48365,3079],{"class":85},[45,48367,1565],{"class":99},[45,48369,24008],{"class":103},[45,48371,100],{"class":99},[45,48373,109],{"class":85},[45,48375,48376,48379,48381,48383,48385,48387],{"class":47,"line":63},[45,48377,48378],{"class":51},"content ",[45,48380,56],{"class":55},[45,48382,15011],{"class":24018},[45,48384,240],{"class":85},[45,48386,46823],{"class":2070},[45,48388,2495],{"class":85},[45,48390,48391,48393,48395,48397],{"class":47,"line":70},[45,48392,2065],{"class":92},[45,48394,96],{"class":85},[45,48396,47723],{"class":2070},[45,48398,109],{"class":85},[45,48400,48401,48403,48405,48408],{"class":47,"line":89},[45,48402,46818],{"class":24018},[45,48404,240],{"class":85},[45,48406,48407],{"class":2070},"close",[45,48409,2495],{"class":85},[14,48411,48412,48413,240],{},"This works, but beginners often forget the last line. That is why most examples should use ",[18,48414,48255],{},[31,48416,48418],{"id":48417},"reading-from-a-file","Reading from a file",[14,48420,48421],{},"Python gives you a few common ways to read file data.",[712,48423,48425,48426],{"id":48424},"read-the-whole-file-with-read","Read the whole file with ",[18,48427,47849],{},[14,48429,165,48430,48432],{},[18,48431,47849],{}," when you want all the contents as one string.",[36,48434,48435],{"className":38,"code":48263,"language":40,"meta":41,"style":41},[18,48436,48437,48467,48481],{"__ignoreMap":41},[45,48438,48439,48441,48443,48445,48447,48449,48451,48453,48455,48457,48459,48461,48463,48465],{"class":47,"line":48},[45,48440,23989],{"class":73},[45,48442,23992],{"class":92},[45,48444,96],{"class":85},[45,48446,100],{"class":99},[45,48448,23999],{"class":103},[45,48450,100],{"class":99},[45,48452,3079],{"class":85},[45,48454,1565],{"class":99},[45,48456,24008],{"class":103},[45,48458,100],{"class":99},[45,48460,9146],{"class":85},[45,48462,24015],{"class":73},[45,48464,15011],{"class":24018},[45,48466,86],{"class":85},[45,48468,48469,48471,48473,48475,48477,48479],{"class":47,"line":63},[45,48470,47704],{"class":51},[45,48472,56],{"class":55},[45,48474,15011],{"class":24018},[45,48476,240],{"class":85},[45,48478,46823],{"class":2070},[45,48480,2495],{"class":85},[45,48482,48483,48485,48487,48489],{"class":47,"line":70},[45,48484,93],{"class":92},[45,48486,96],{"class":85},[45,48488,47723],{"class":2070},[45,48490,109],{"class":85},[712,48492,48494,48495],{"id":48493},"read-one-line-with-readline","Read one line with ",[18,48496,48497],{},"readline()",[14,48499,165,48500,48502],{},[18,48501,48497],{}," when you want just one line at a time.",[36,48504,48506],{"className":38,"code":48505,"language":40,"meta":41,"style":41},"with open(\"example.txt\", \"r\") as file:\n    first_line = file.readline()\n    print(first_line)\n",[18,48507,48508,48538,48554],{"__ignoreMap":41},[45,48509,48510,48512,48514,48516,48518,48520,48522,48524,48526,48528,48530,48532,48534,48536],{"class":47,"line":48},[45,48511,23989],{"class":73},[45,48513,23992],{"class":92},[45,48515,96],{"class":85},[45,48517,100],{"class":99},[45,48519,23999],{"class":103},[45,48521,100],{"class":99},[45,48523,3079],{"class":85},[45,48525,1565],{"class":99},[45,48527,24008],{"class":103},[45,48529,100],{"class":99},[45,48531,9146],{"class":85},[45,48533,24015],{"class":73},[45,48535,15011],{"class":24018},[45,48537,86],{"class":85},[45,48539,48540,48543,48545,48547,48549,48552],{"class":47,"line":63},[45,48541,48542],{"class":51},"    first_line ",[45,48544,56],{"class":55},[45,48546,15011],{"class":24018},[45,48548,240],{"class":85},[45,48550,48551],{"class":2070},"readline",[45,48553,2495],{"class":85},[45,48555,48556,48558,48560,48563],{"class":47,"line":70},[45,48557,93],{"class":92},[45,48559,96],{"class":85},[45,48561,48562],{"class":2070},"first_line",[45,48564,109],{"class":85},[712,48566,48568,48569],{"id":48567},"read-all-lines-with-readlines","Read all lines with ",[18,48570,48571],{},"readlines()",[14,48573,165,48574,48576],{},[18,48575,48571],{}," when you want a list of lines.",[36,48578,48580],{"className":38,"code":48579,"language":40,"meta":41,"style":41},"with open(\"example.txt\", \"r\") as file:\n    lines = file.readlines()\n    print(lines)\n",[18,48581,48582,48612,48628],{"__ignoreMap":41},[45,48583,48584,48586,48588,48590,48592,48594,48596,48598,48600,48602,48604,48606,48608,48610],{"class":47,"line":48},[45,48585,23989],{"class":73},[45,48587,23992],{"class":92},[45,48589,96],{"class":85},[45,48591,100],{"class":99},[45,48593,23999],{"class":103},[45,48595,100],{"class":99},[45,48597,3079],{"class":85},[45,48599,1565],{"class":99},[45,48601,24008],{"class":103},[45,48603,100],{"class":99},[45,48605,9146],{"class":85},[45,48607,24015],{"class":73},[45,48609,15011],{"class":24018},[45,48611,86],{"class":85},[45,48613,48614,48617,48619,48621,48623,48626],{"class":47,"line":63},[45,48615,48616],{"class":51},"    lines ",[45,48618,56],{"class":55},[45,48620,15011],{"class":24018},[45,48622,240],{"class":85},[45,48624,48625],{"class":2070},"readlines",[45,48627,2495],{"class":85},[45,48629,48630,48632,48634,48637],{"class":47,"line":70},[45,48631,93],{"class":92},[45,48633,96],{"class":85},[45,48635,48636],{"class":2070},"lines",[45,48638,109],{"class":85},[14,48640,18691],{},[36,48642,48644],{"className":38,"code":48643,"language":40,"meta":41,"style":41},"['First line\\n', 'Second line\\n']\n",[18,48645,48646],{"__ignoreMap":41},[45,48647,48648,48650,48652,48655,48657,48659,48661,48663,48666,48668,48670],{"class":47,"line":48},[45,48649,9680],{"class":85},[45,48651,7901],{"class":99},[45,48653,48654],{"class":103},"First line",[45,48656,47661],{"class":2771},[45,48658,7901],{"class":99},[45,48660,3079],{"class":85},[45,48662,9199],{"class":99},[45,48664,48665],{"class":103},"Second line",[45,48667,47661],{"class":2771},[45,48669,7901],{"class":99},[45,48671,8594],{"class":85},[14,48673,48674],{},"Choose the method based on what you need:",[195,48676,48677,48682,48687],{},[198,48678,48679,48681],{},[18,48680,47849],{}," → one string",[198,48683,48684,48686],{},[18,48685,48497],{}," → one line",[198,48688,48689,48691],{},[18,48690,48571],{}," → list of lines",[14,48693,48694,48695,230,48699,240],{},"If your goal is a specific reading task, see ",[236,48696,48698],{"href":48697},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","How to read a file in Python",[236,48700,48702],{"href":48701},"\u002Fhow-to\u002Fhow-to-read-a-file-line-by-line-in-python\u002F","How to read a file line by line in Python",[31,48704,48706],{"id":48705},"writing-to-a-file","Writing to a file",[14,48708,165,48709,48711],{},[18,48710,47852],{}," to save text into a file.",[712,48713,48715,48716,48718],{"id":48714},"write-text-in-w-mode","Write text in ",[18,48717,47752],{}," mode",[36,48720,48722],{"className":38,"code":48721,"language":40,"meta":41,"style":41},"with open(\"output.txt\", \"w\") as file:\n    file.write(\"Hello\\n\")\n    file.write(\"World\\n\")\n",[18,48723,48724,48755,48775],{"__ignoreMap":41},[45,48725,48726,48728,48730,48732,48734,48737,48739,48741,48743,48745,48747,48749,48751,48753],{"class":47,"line":48},[45,48727,23989],{"class":73},[45,48729,23992],{"class":92},[45,48731,96],{"class":85},[45,48733,100],{"class":99},[45,48735,48736],{"class":103},"output.txt",[45,48738,100],{"class":99},[45,48740,3079],{"class":85},[45,48742,1565],{"class":99},[45,48744,47631],{"class":103},[45,48746,100],{"class":99},[45,48748,9146],{"class":85},[45,48750,24015],{"class":73},[45,48752,15011],{"class":24018},[45,48754,86],{"class":85},[45,48756,48757,48759,48761,48763,48765,48767,48769,48771,48773],{"class":47,"line":63},[45,48758,47646],{"class":24018},[45,48760,240],{"class":85},[45,48762,47651],{"class":2070},[45,48764,96],{"class":85},[45,48766,100],{"class":99},[45,48768,3226],{"class":103},[45,48770,47661],{"class":2771},[45,48772,100],{"class":99},[45,48774,109],{"class":85},[45,48776,48777,48779,48781,48783,48785,48787,48790,48792,48794],{"class":47,"line":70},[45,48778,47646],{"class":24018},[45,48780,240],{"class":85},[45,48782,47651],{"class":2070},[45,48784,96],{"class":85},[45,48786,100],{"class":99},[45,48788,48789],{"class":103},"World",[45,48791,47661],{"class":2771},[45,48793,100],{"class":99},[45,48795,109],{"class":85},[14,48797,48798,48799,48801],{},"This writes text to the file. If ",[18,48800,48736],{}," already had content, it is replaced.",[712,48803,48805,48806,48718],{"id":48804},"add-text-in-a-mode","Add text in ",[18,48807,48092],{},[36,48809,48811],{"className":38,"code":48810,"language":40,"meta":41,"style":41},"with open(\"output.txt\", \"a\") as file:\n    file.write(\"More text\\n\")\n",[18,48812,48813,48843],{"__ignoreMap":41},[45,48814,48815,48817,48819,48821,48823,48825,48827,48829,48831,48833,48835,48837,48839,48841],{"class":47,"line":48},[45,48816,23989],{"class":73},[45,48818,23992],{"class":92},[45,48820,96],{"class":85},[45,48822,100],{"class":99},[45,48824,48736],{"class":103},[45,48826,100],{"class":99},[45,48828,3079],{"class":85},[45,48830,1565],{"class":99},[45,48832,236],{"class":103},[45,48834,100],{"class":99},[45,48836,9146],{"class":85},[45,48838,24015],{"class":73},[45,48840,15011],{"class":24018},[45,48842,86],{"class":85},[45,48844,48845,48847,48849,48851,48853,48855,48858,48860,48862],{"class":47,"line":63},[45,48846,47646],{"class":24018},[45,48848,240],{"class":85},[45,48850,47651],{"class":2070},[45,48852,96],{"class":85},[45,48854,100],{"class":99},[45,48856,48857],{"class":103},"More text",[45,48859,47661],{"class":2771},[45,48861,100],{"class":99},[45,48863,109],{"class":85},[14,48865,48866],{},"This adds new text after the existing content.",[712,48868,48870],{"id":48869},"writing-numbers","Writing numbers",[14,48872,48873,48875],{},[18,48874,47852],{}," only accepts strings. If you try to write a number directly, Python will raise an error.",[14,48877,48878],{},"Correct example:",[36,48880,48882],{"className":38,"code":48881,"language":40,"meta":41,"style":41},"score = 123\n\nwith open(\"score.txt\", \"w\") as file:\n    file.write(str(score))\n",[18,48883,48884,48893,48897,48928],{"__ignoreMap":41},[45,48885,48886,48888,48890],{"class":47,"line":48},[45,48887,581],{"class":51},[45,48889,56],{"class":55},[45,48891,48892],{"class":59}," 123\n",[45,48894,48895],{"class":47,"line":63},[45,48896,67],{"emptyLinePlaceholder":66},[45,48898,48899,48901,48903,48905,48907,48910,48912,48914,48916,48918,48920,48922,48924,48926],{"class":47,"line":70},[45,48900,23989],{"class":73},[45,48902,23992],{"class":92},[45,48904,96],{"class":85},[45,48906,100],{"class":99},[45,48908,48909],{"class":103},"score.txt",[45,48911,100],{"class":99},[45,48913,3079],{"class":85},[45,48915,1565],{"class":99},[45,48917,47631],{"class":103},[45,48919,100],{"class":99},[45,48921,9146],{"class":85},[45,48923,24015],{"class":73},[45,48925,15011],{"class":24018},[45,48927,86],{"class":85},[45,48929,48930,48932,48934,48936,48938,48940,48942,48944],{"class":47,"line":89},[45,48931,47646],{"class":24018},[45,48933,240],{"class":85},[45,48935,47651],{"class":2070},[45,48937,96],{"class":85},[45,48939,29733],{"class":2109},[45,48941,96],{"class":85},[45,48943,46543],{"class":2070},[45,48945,4348],{"class":85},[14,48947,48948,48949,240],{},"If you want a task-focused guide, see ",[236,48950,48952],{"href":48951},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python\u002F","How to write to a file in Python",[31,48954,48956],{"id":48955},"what-beginners-should-watch-out-for","What beginners should watch out for",[14,48958,48959],{},"These are some very common file mistakes:",[195,48961,48962,48967,48970,48973,48976],{},[198,48963,1350,48964,48966],{},[18,48965,47752],{}," by mistake and erasing existing content",[198,48968,48969],{},"Trying to read a file that does not exist",[198,48971,48972],{},"Using the wrong file path",[198,48974,48975],{},"Trying to write a number without converting it to a string",[198,48977,48978],{},"Trying to open a folder as if it were a file",[712,48980,48982],{"id":48981},"common-causes","Common causes",[195,48984,48985,48988,48991,48996,49001,49004],{},[198,48986,48987],{},"Opening a file with the wrong mode",[198,48989,48990],{},"Using an incorrect relative or absolute path",[198,48992,2043,48993,48995],{},[18,48994,47752],{}," overwrites existing content",[198,48997,48998,48999],{},"Trying to write non-string data with ",[18,49000,47852],{},[198,49002,49003],{},"Trying to open a file that does not exist",[198,49005,49006],{},"Attempting to open a directory as if it were a file",[712,49008,49010],{"id":49009},"helpful-debugging-commands","Helpful debugging commands",[14,49012,49013],{},"If file code is not working, these quick checks often help:",[36,49015,49017],{"className":38,"code":49016,"language":40,"meta":41,"style":41},"print(content)\nprint(type(content))\n\nimport os\nprint(os.getcwd())\nprint(os.path.exists(\"example.txt\"))\nprint(os.listdir())\n",[18,49018,49019,49029,49043,49047,49054,49070,49098],{"__ignoreMap":41},[45,49020,49021,49023,49025,49027],{"class":47,"line":48},[45,49022,2065],{"class":92},[45,49024,96],{"class":85},[45,49026,47723],{"class":2070},[45,49028,109],{"class":85},[45,49030,49031,49033,49035,49037,49039,49041],{"class":47,"line":63},[45,49032,2065],{"class":92},[45,49034,96],{"class":85},[45,49036,4340],{"class":2109},[45,49038,96],{"class":85},[45,49040,47723],{"class":2070},[45,49042,4348],{"class":85},[45,49044,49045],{"class":47,"line":70},[45,49046,67],{"emptyLinePlaceholder":66},[45,49048,49049,49051],{"class":47,"line":89},[45,49050,14870],{"class":73},[45,49052,49053],{"class":51}," os\n",[45,49055,49056,49058,49060,49063,49065,49068],{"class":47,"line":112},[45,49057,2065],{"class":92},[45,49059,96],{"class":85},[45,49061,49062],{"class":2070},"os",[45,49064,240],{"class":85},[45,49066,49067],{"class":2070},"getcwd",[45,49069,10777],{"class":85},[45,49071,49072,49074,49076,49078,49080,49083,49085,49088,49090,49092,49094,49096],{"class":47,"line":126},[45,49073,2065],{"class":92},[45,49075,96],{"class":85},[45,49077,49062],{"class":2070},[45,49079,240],{"class":85},[45,49081,49082],{"class":3095},"path",[45,49084,240],{"class":85},[45,49086,49087],{"class":2070},"exists",[45,49089,96],{"class":85},[45,49091,100],{"class":99},[45,49093,23999],{"class":103},[45,49095,100],{"class":99},[45,49097,4348],{"class":85},[45,49099,49100,49102,49104,49106,49108,49111],{"class":47,"line":142},[45,49101,2065],{"class":92},[45,49103,96],{"class":85},[45,49105,49062],{"class":2070},[45,49107,240],{"class":85},[45,49109,49110],{"class":2070},"listdir",[45,49112,10777],{"class":85},[14,49114,33334],{},[195,49116,49117,49123,49129,49135,49141],{},[198,49118,49119,49122],{},[18,49120,49121],{},"print(content)"," shows what you actually read",[198,49124,49125,49128],{},[18,49126,49127],{},"print(type(content))"," confirms the data type",[198,49130,49131,49134],{},[18,49132,49133],{},"os.getcwd()"," shows the current working folder",[198,49136,49137,49140],{},[18,49138,49139],{},"os.path.exists(\"example.txt\")"," checks whether the file exists",[198,49142,49143,49146],{},[18,49144,49145],{},"os.listdir()"," shows files in the current folder",[14,49148,49149,49150,240,49154,49156,49157,49161],{},"If Python says a file does not exist, see ",[236,49151,49153],{"href":49152},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes\u002F","FileNotFoundError in Python: causes and fixes",[7343,49155],{},"\nIf the problem is really a path issue, ",[236,49158,49160],{"href":49159},"\u002Flearn\u002Fworking-with-file-paths-in-python\u002F","Working with file paths in Python"," will help.",[31,49163,49165],{"id":49164},"when-to-go-to-a-more-specific-page","When to go to a more specific page",[14,49167,49168],{},"This page teaches the basic idea of file handling. Use a more specific page when you need to solve one task.",[14,49170,2115],{},[195,49172,49173,49176,49179,49182,49185,49188],{},[198,49174,49175],{},"read a file for a real task",[198,49177,49178],{},"write data to a file",[198,49180,49181],{},"append new content",[198,49183,49184],{},"read line by line",[198,49186,49187],{},"check whether a file exists",[198,49189,49190],{},"fix a file-related error",[31,49192,2281],{"id":2280},[712,49194,49196],{"id":49195},"what-is-the-easiest-way-to-open-a-file-in-python","What is the easiest way to open a file in Python?",[14,49198,165,49199,49202],{},[18,49200,49201],{},"with open(...) as file:",". It is simple and closes the file automatically.",[712,49204,16362,49206,1316,49208,49210],{"id":49205},"what-is-the-difference-between-w-and-a-mode",[18,49207,47752],{},[18,49209,48092],{}," mode?",[14,49212,49213,49215,49216,49218],{},[18,49214,47752],{}," replaces the file contents. ",[18,49217,48092],{}," adds new content to the end.",[712,49220,49222,49223,49225],{"id":49221},"do-i-need-to-call-close-every-time","Do I need to call ",[18,49224,48341],{}," every time?",[14,49227,49228,49229,49231],{},"Not if you use ",[18,49230,48255],{},". Python handles closing the file for you.",[712,49233,49235],{"id":49234},"why-does-python-say-the-file-does-not-exist","Why does Python say the file does not exist?",[14,49237,49238],{},"The file path may be wrong, or your script is running in a different folder than you expect.",[712,49240,49242,49243,49245],{"id":49241},"can-write-save-numbers-directly","Can ",[18,49244,47852],{}," save numbers directly?",[14,49247,49248,49249,240],{},"No. Convert numbers to strings first, for example ",[18,49250,49251],{},"str(123)",[31,49253,2342],{"id":2341},[195,49255,49256,49260,49264,49268,49272,49276,49282,49286],{},[198,49257,49258],{},[236,49259,47823],{"href":47822},[198,49261,49262],{},[236,49263,48698],{"href":48697},[198,49265,49266],{},[236,49267,48952],{"href":48951},[198,49269,49270],{},[236,49271,48171],{"href":48170},[198,49273,49274],{},[236,49275,48702],{"href":48701},[198,49277,49278],{},[236,49279,49281],{"href":49280},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python\u002F","How to check if a file exists in Python",[198,49283,49284],{},[236,49285,49160],{"href":49159},[198,49287,49288],{},[236,49289,49153],{"href":49152},[2367,49291,49292],{},"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 .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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":41,"searchDepth":63,"depth":63,"links":49294},[49295,49296,49297,49298,49308,49310,49318,49325,49329,49330,49340],{"id":33,"depth":63,"text":34},{"id":47774,"depth":63,"text":47775},{"id":47826,"depth":63,"text":47827},{"id":47925,"depth":63,"text":47926,"children":49299},[49300,49302,49304,49306],{"id":47932,"depth":70,"text":49301},"\"r\" read mode",{"id":48014,"depth":70,"text":49303},"\"w\" write mode",{"id":48089,"depth":70,"text":49305},"\"a\" append mode",{"id":48174,"depth":70,"text":49307},"\"x\" create mode",{"id":48251,"depth":63,"text":49309},"Why use with open()",{"id":48417,"depth":63,"text":48418,"children":49311},[49312,49314,49316],{"id":48424,"depth":70,"text":49313},"Read the whole file with read()",{"id":48493,"depth":70,"text":49315},"Read one line with readline()",{"id":48567,"depth":70,"text":49317},"Read all lines with readlines()",{"id":48705,"depth":63,"text":48706,"children":49319},[49320,49322,49324],{"id":48714,"depth":70,"text":49321},"Write text in \"w\" mode",{"id":48804,"depth":70,"text":49323},"Add text in \"a\" mode",{"id":48869,"depth":70,"text":48870},{"id":48955,"depth":63,"text":48956,"children":49326},[49327,49328],{"id":48981,"depth":70,"text":48982},{"id":49009,"depth":70,"text":49010},{"id":49164,"depth":63,"text":49165},{"id":2280,"depth":63,"text":2281,"children":49331},[49332,49333,49335,49337,49338],{"id":49195,"depth":70,"text":49196},{"id":49205,"depth":70,"text":49334},"What is the difference between \"w\" and \"a\" mode?",{"id":49221,"depth":70,"text":49336},"Do I need to call close() every time?",{"id":49234,"depth":70,"text":49235},{"id":49241,"depth":70,"text":49339},"Can write() save numbers directly?",{"id":2341,"depth":63,"text":2342},"Master python file handling basics read and write in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write",{"title":47561,"description":49341},"learn\u002Fpython-file-handling-basics-read-and-write","kLKnzSjnRfv7HDRVIwMHmHu9z5YD4gxdapb3nMr5rYs",{"id":49348,"title":49349,"body":49350,"description":51469,"extension":2399,"meta":51470,"navigation":66,"path":51471,"seo":51472,"stem":51473,"__hash__":51474},"content\u002Flearn\u002Fpython-for-loops-explained.md","Python for Loops Explained",{"type":7,"value":49351,"toc":51428},[49352,49355,49363,49369,49383,49385,49452,49454,49474,49479,49483,49488,49491,49507,49513,49515,49518,49546,49548,49574,49577,49579,49617,49619,49635,49638,49649,49654,49658,49661,49723,49725,49741,49743,49769,49772,49780,49783,49789,49793,49798,49829,49831,49856,49867,49870,49909,49911,49931,49934,49958,49961,49986,49991,49995,49999,50002,50046,50048,50064,50066,50069,50142,50144,50159,50164,50249,50251,50267,50270,50278,50282,50284,50289,50336,50338,50359,50366,50368,50373,50420,50422,50443,50452,50458,50462,50469,50474,50526,50528,50550,50559,50626,50628,50636,50639,50641,50646,50648,50652,50654,50691,50693,50730,50734,50737,50739,50776,50778,50814,50819,50826,50831,50861,50863,50887,50891,50894,50958,50960,50980,50986,50990,50993,51060,51070,51074,51079,51090,51094,51102,51105,51120,51125,51129,51132,51136,51151,51154,51158,51177,51180,51187,51211,51213,51242,51246,51265,51268,51272,51287,51293,51296,51320,51327,51329,51333,51338,51342,51348,51352,51365,51369,51377,51381,51386,51388,51415,51425],[10,49353,49349],{"id":49354},"python-for-loops-explained",[14,49356,2419,49357,49359,49360,49362],{},[18,49358,7895],{}," loop in Python lets you repeat code once for each item in a group of values. It is one of the most useful tools for beginners because it makes it easy to work through lists, strings, dictionaries, and ",[18,49361,22305],{}," values without manually managing positions.",[14,49364,49365,49366,49368],{},"You will use ",[18,49367,7895],{}," loops when you want to:",[195,49370,49371,49374,49377,49380],{},[198,49372,49373],{},"print each item in a list",[198,49375,49376],{},"process each character in a string",[198,49378,49379],{},"repeat something a fixed number of times",[198,49381,49382],{},"go through dictionary data one piece at a time",[31,49384,34],{"id":33},[36,49386,49388],{"className":38,"code":49387,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"cherry\"]\n\nfor fruit in fruits:\n    print(fruit)\n",[18,49389,49390,49423,49427,49441],{"__ignoreMap":41},[45,49391,49392,49395,49397,49399,49401,49403,49405,49407,49409,49411,49413,49415,49417,49419,49421],{"class":47,"line":48},[45,49393,49394],{"class":51},"fruits ",[45,49396,56],{"class":55},[45,49398,8575],{"class":85},[45,49400,100],{"class":99},[45,49402,9443],{"class":103},[45,49404,100],{"class":99},[45,49406,3079],{"class":85},[45,49408,1565],{"class":99},[45,49410,9456],{"class":103},[45,49412,100],{"class":99},[45,49414,3079],{"class":85},[45,49416,1565],{"class":99},[45,49418,9469],{"class":103},[45,49420,100],{"class":99},[45,49422,8594],{"class":85},[45,49424,49425],{"class":47,"line":63},[45,49426,67],{"emptyLinePlaceholder":66},[45,49428,49429,49431,49434,49436,49439],{"class":47,"line":70},[45,49430,7895],{"class":73},[45,49432,49433],{"class":51}," fruit ",[45,49435,8623],{"class":73},[45,49437,49438],{"class":51}," fruits",[45,49440,86],{"class":85},[45,49442,49443,49445,49447,49450],{"class":47,"line":89},[45,49444,93],{"class":92},[45,49446,96],{"class":85},[45,49448,49449],{"class":2070},"fruit",[45,49451,109],{"class":85},[14,49453,177],{},[36,49455,49457],{"className":38,"code":49456,"language":40,"meta":41,"style":41},"apple\nbanana\ncherry\n",[18,49458,49459,49464,49469],{"__ignoreMap":41},[45,49460,49461],{"class":47,"line":48},[45,49462,49463],{"class":51},"apple\n",[45,49465,49466],{"class":47,"line":63},[45,49467,49468],{"class":51},"banana\n",[45,49470,49471],{"class":47,"line":70},[45,49472,49473],{"class":51},"cherry\n",[14,49475,19194,49476,49478],{},[18,49477,7895],{}," loop when you want to go through each item in a collection one by one.",[31,49480,49482],{"id":49481},"what-a-for-loop-does","What a for loop does",[14,49484,2419,49485,49487],{},[18,49486,7895],{}," loop repeats a block of code once for each item in something iterable.",[14,49489,49490],{},"Common things you can loop over include:",[195,49492,49493,49495,49497,49499,49501,49503],{},[198,49494,20890],{},[198,49496,22296],{},[198,49498,22293],{},[198,49500,20896],{},[198,49502,20893],{},[198,49504,49505,19922],{},[18,49506,22305],{},[14,49508,49509,49510,49512],{},"This is useful when you know you want to process items one by one. In many beginner cases, a ",[18,49511,7895],{}," loop is simpler and safer than trying to track an index yourself.",[31,49514,8707],{"id":8706},[14,49516,49517],{},"The basic pattern looks like this:",[36,49519,49521],{"className":38,"code":49520,"language":40,"meta":41,"style":41},"for item in collection:\n    print(item)\n",[18,49522,49523,49536],{"__ignoreMap":41},[45,49524,49525,49527,49529,49531,49534],{"class":47,"line":48},[45,49526,7895],{"class":73},[45,49528,8732],{"class":51},[45,49530,8623],{"class":73},[45,49532,49533],{"class":51}," collection",[45,49535,86],{"class":85},[45,49537,49538,49540,49542,49544],{"class":47,"line":63},[45,49539,93],{"class":92},[45,49541,96],{"class":85},[45,49543,23670],{"class":2070},[45,49545,109],{"class":85},[14,49547,27396],{},[195,49549,49550,49555,49560,49565,49571],{},[198,49551,49552,49554],{},[18,49553,7895],{}," starts the loop",[198,49556,49557,49559],{},[18,49558,23670],{}," is a variable name for the current value",[198,49561,49562,49564],{},[18,49563,8623],{}," connects the variable to the collection",[198,49566,49567,49570],{},[18,49568,49569],{},"collection"," is the thing you are looping over",[198,49572,49573],{},"the indented block runs once on each loop",[14,49575,49576],{},"Indentation matters in Python. The code inside the loop must be indented.",[14,49578,2115],{},[36,49580,49582],{"className":38,"code":49581,"language":40,"meta":41,"style":41},"for number in [1, 2, 3]:\n    print(number)\n",[18,49583,49584,49607],{"__ignoreMap":41},[45,49585,49586,49588,49590,49592,49594,49596,49598,49600,49602,49604],{"class":47,"line":48},[45,49587,7895],{"class":73},[45,49589,744],{"class":51},[45,49591,8623],{"class":73},[45,49593,8575],{"class":85},[45,49595,8578],{"class":59},[45,49597,3079],{"class":85},[45,49599,3754],{"class":59},[45,49601,3079],{"class":85},[45,49603,777],{"class":59},[45,49605,49606],{"class":85},"]:\n",[45,49608,49609,49611,49613,49615],{"class":47,"line":63},[45,49610,93],{"class":92},[45,49612,96],{"class":85},[45,49614,13546],{"class":2070},[45,49616,109],{"class":85},[14,49618,177],{},[36,49620,49621],{"className":38,"code":13029,"language":40,"meta":41,"style":41},[18,49622,49623,49627,49631],{"__ignoreMap":41},[45,49624,49625],{"class":47,"line":48},[45,49626,13036],{"class":59},[45,49628,49629],{"class":47,"line":63},[45,49630,13041],{"class":59},[45,49632,49633],{"class":47,"line":70},[45,49634,13046],{"class":59},[14,49636,49637],{},"You can read this as:",[17619,49639,49640],{},[14,49641,49642,49643,49645,49646,49648],{},"For each ",[18,49644,13546],{}," in ",[18,49647,40633],{},", print that number.",[14,49650,49651,49652,240],{},"If indentation is confusing, see ",[236,49653,554],{"href":553},[31,49655,49657],{"id":49656},"looping-through-a-list","Looping through a list",[14,49659,49660],{},"Lists are one of the most common things to loop over.",[36,49662,49663],{"className":38,"code":22313,"language":40,"meta":41,"style":41},[18,49664,49665,49697,49701,49713],{"__ignoreMap":41},[45,49666,49667,49669,49671,49673,49675,49677,49679,49681,49683,49685,49687,49689,49691,49693,49695],{"class":47,"line":48},[45,49668,22320],{"class":51},[45,49670,56],{"class":55},[45,49672,8575],{"class":85},[45,49674,100],{"class":99},[45,49676,22329],{"class":103},[45,49678,100],{"class":99},[45,49680,3079],{"class":85},[45,49682,1565],{"class":99},[45,49684,22338],{"class":103},[45,49686,100],{"class":99},[45,49688,3079],{"class":85},[45,49690,1565],{"class":99},[45,49692,22347],{"class":103},[45,49694,100],{"class":99},[45,49696,8594],{"class":85},[45,49698,49699],{"class":47,"line":63},[45,49700,67],{"emptyLinePlaceholder":66},[45,49702,49703,49705,49707,49709,49711],{"class":47,"line":70},[45,49704,7895],{"class":73},[45,49706,22362],{"class":51},[45,49708,8623],{"class":73},[45,49710,22367],{"class":51},[45,49712,86],{"class":85},[45,49714,49715,49717,49719,49721],{"class":47,"line":89},[45,49716,93],{"class":92},[45,49718,96],{"class":85},[45,49720,22378],{"class":2070},[45,49722,109],{"class":85},[14,49724,177],{},[36,49726,49727],{"className":38,"code":22385,"language":40,"meta":41,"style":41},[18,49728,49729,49733,49737],{"__ignoreMap":41},[45,49730,49731],{"class":47,"line":48},[45,49732,22392],{"class":51},[45,49734,49735],{"class":47,"line":63},[45,49736,22397],{"class":51},[45,49738,49739],{"class":47,"line":70},[45,49740,22402],{"class":51},[14,49742,6806],{},[195,49744,49745,49753,49761],{},[198,49746,49747,49748,36998,49750],{},"On the first loop, ",[18,49749,22378],{},[18,49751,49752],{},"\"red\"",[198,49754,49755,49756,36998,49758],{},"On the second loop, ",[18,49757,22378],{},[18,49759,49760],{},"\"green\"",[198,49762,49763,49764,36998,49766],{},"On the third loop, ",[18,49765,22378],{},[18,49767,49768],{},"\"blue\"",[14,49770,49771],{},"A good way to read this is:",[17619,49773,49774],{},[14,49775,49642,49776,49645,49778],{},[18,49777,22378],{},[18,49779,22407],{},[14,49781,49782],{},"That mental pattern makes loops much easier to understand.",[14,49784,49785,49786,240],{},"If you want more list-specific examples, see ",[236,49787,49788],{"href":37354},"how to loop through a list in Python",[31,49790,49792],{"id":49791},"using-range-with-for-loops","Using range() with for loops",[14,49794,165,49795,49797],{},[18,49796,22305],{}," when you want to repeat something a fixed number of times.",[36,49799,49801],{"className":38,"code":49800,"language":40,"meta":41,"style":41},"for number in range(5):\n    print(number)\n",[18,49802,49803,49819],{"__ignoreMap":41},[45,49804,49805,49807,49809,49811,49813,49815,49817],{"class":47,"line":48},[45,49806,7895],{"class":73},[45,49808,744],{"class":51},[45,49810,8623],{"class":73},[45,49812,10451],{"class":92},[45,49814,96],{"class":85},[45,49816,3749],{"class":59},[45,49818,2464],{"class":85},[45,49820,49821,49823,49825,49827],{"class":47,"line":63},[45,49822,93],{"class":92},[45,49824,96],{"class":85},[45,49826,13546],{"class":2070},[45,49828,109],{"class":85},[14,49830,177],{},[36,49832,49834],{"className":38,"code":49833,"language":40,"meta":41,"style":41},"0\n1\n2\n3\n4\n",[18,49835,49836,49840,49844,49848,49852],{"__ignoreMap":41},[45,49837,49838],{"class":47,"line":48},[45,49839,23957],{"class":59},[45,49841,49842],{"class":47,"line":63},[45,49843,13036],{"class":59},[45,49845,49846],{"class":47,"line":70},[45,49847,13041],{"class":59},[45,49849,49850],{"class":47,"line":89},[45,49851,13046],{"class":59},[45,49853,49854],{"class":47,"line":112},[45,49855,13681],{"class":59},[14,49857,49858,49861,49862,49864,49865,240],{},[18,49859,49860],{},"range(5)"," gives numbers from ",[18,49863,10482],{}," up to, but not including, ",[18,49866,3749],{},[14,49868,49869],{},"You can also use this form:",[36,49871,49873],{"className":38,"code":49872,"language":40,"meta":41,"style":41},"for number in range(2, 10, 2):\n    print(number)\n",[18,49874,49875,49899],{"__ignoreMap":41},[45,49876,49877,49879,49881,49883,49885,49887,49889,49891,49893,49895,49897],{"class":47,"line":48},[45,49878,7895],{"class":73},[45,49880,744],{"class":51},[45,49882,8623],{"class":73},[45,49884,10451],{"class":92},[45,49886,96],{"class":85},[45,49888,9367],{"class":59},[45,49890,3079],{"class":85},[45,49892,1190],{"class":59},[45,49894,3079],{"class":85},[45,49896,3754],{"class":59},[45,49898,2464],{"class":85},[45,49900,49901,49903,49905,49907],{"class":47,"line":63},[45,49902,93],{"class":92},[45,49904,96],{"class":85},[45,49906,13546],{"class":2070},[45,49908,109],{"class":85},[14,49910,177],{},[36,49912,49913],{"className":38,"code":14087,"language":40,"meta":41,"style":41},[18,49914,49915,49919,49923,49927],{"__ignoreMap":41},[45,49916,49917],{"class":47,"line":48},[45,49918,13041],{"class":59},[45,49920,49921],{"class":47,"line":63},[45,49922,13681],{"class":59},[45,49924,49925],{"class":47,"line":70},[45,49926,11797],{"class":59},[45,49928,49929],{"class":47,"line":89},[45,49930,1310],{"class":59},[14,49932,49933],{},"This uses:",[195,49935,49936,49944,49951],{},[198,49937,49938,49941,49942],{},[18,49939,49940],{},"start",": ",[18,49943,9367],{},[198,49945,49946,49941,49949],{},[18,49947,49948],{},"stop",[18,49950,11786],{},[198,49952,49953,49941,49956],{},[18,49954,49955],{},"step",[18,49957,9367],{},[14,49959,49960],{},"So the full pattern is:",[36,49962,49964],{"className":38,"code":49963,"language":40,"meta":41,"style":41},"range(start, stop, step)\n",[18,49965,49966],{"__ignoreMap":41},[45,49967,49968,49971,49973,49975,49977,49979,49981,49984],{"class":47,"line":48},[45,49969,49970],{"class":92},"range",[45,49972,96],{"class":85},[45,49974,49940],{"class":2070},[45,49976,3079],{"class":85},[45,49978,36076],{"class":2070},[45,49980,3079],{"class":85},[45,49982,49983],{"class":2070}," step",[45,49985,109],{"class":85},[14,49987,49988,49989,240],{},"For a full explanation, see ",[236,49990,23972],{"href":23971},[31,49992,49994],{"id":49993},"looping-through-strings-and-dictionaries","Looping through strings and dictionaries",[712,49996,49998],{"id":49997},"looping-through-a-string","Looping through a string",[14,50000,50001],{},"A string loops one character at a time.",[36,50003,50005],{"className":38,"code":50004,"language":40,"meta":41,"style":41},"word = \"cat\"\n\nfor letter in word:\n    print(letter)\n",[18,50006,50007,50019,50023,50036],{"__ignoreMap":41},[45,50008,50009,50011,50013,50015,50017],{"class":47,"line":48},[45,50010,27985],{"class":51},[45,50012,56],{"class":55},[45,50014,1565],{"class":99},[45,50016,7035],{"class":103},[45,50018,1571],{"class":99},[45,50020,50021],{"class":47,"line":63},[45,50022,67],{"emptyLinePlaceholder":66},[45,50024,50025,50027,50029,50031,50034],{"class":47,"line":70},[45,50026,7895],{"class":73},[45,50028,23733],{"class":51},[45,50030,8623],{"class":73},[45,50032,50033],{"class":51}," word",[45,50035,86],{"class":85},[45,50037,50038,50040,50042,50044],{"class":47,"line":89},[45,50039,93],{"class":92},[45,50041,96],{"class":85},[45,50043,23752],{"class":2070},[45,50045,109],{"class":85},[14,50047,177],{},[36,50049,50050],{"className":38,"code":23759,"language":40,"meta":41,"style":41},[18,50051,50052,50056,50060],{"__ignoreMap":41},[45,50053,50054],{"class":47,"line":48},[45,50055,23166],{"class":51},[45,50057,50058],{"class":47,"line":63},[45,50059,23156],{"class":51},[45,50061,50062],{"class":47,"line":70},[45,50063,23774],{"class":51},[712,50065,44186],{"id":44185},[14,50067,50068],{},"A dictionary loops through its keys by default.",[36,50070,50072],{"className":38,"code":50071,"language":40,"meta":41,"style":41},"person = {\n    \"name\": \"Maya\",\n    \"age\": 12\n}\n\nfor key in person:\n    print(key)\n",[18,50073,50074,50082,50100,50112,50116,50120,50132],{"__ignoreMap":41},[45,50075,50076,50078,50080],{"class":47,"line":48},[45,50077,2966],{"class":51},[45,50079,56],{"class":55},[45,50081,41432],{"class":85},[45,50083,50084,50086,50088,50090,50092,50094,50096,50098],{"class":47,"line":63},[45,50085,41437],{"class":99},[45,50087,3096],{"class":103},[45,50089,100],{"class":99},[45,50091,457],{"class":85},[45,50093,1565],{"class":99},[45,50095,5737],{"class":103},[45,50097,100],{"class":99},[45,50099,41453],{"class":85},[45,50101,50102,50104,50106,50108,50110],{"class":47,"line":70},[45,50103,41437],{"class":99},[45,50105,2082],{"class":103},[45,50107,100],{"class":99},[45,50109,457],{"class":85},[45,50111,1635],{"class":59},[45,50113,50114],{"class":47,"line":89},[45,50115,8629],{"class":85},[45,50117,50118],{"class":47,"line":112},[45,50119,67],{"emptyLinePlaceholder":66},[45,50121,50122,50124,50126,50128,50130],{"class":47,"line":126},[45,50123,7895],{"class":73},[45,50125,23833],{"class":51},[45,50127,8623],{"class":73},[45,50129,23838],{"class":51},[45,50131,86],{"class":85},[45,50133,50134,50136,50138,50140],{"class":47,"line":142},[45,50135,93],{"class":92},[45,50137,96],{"class":85},[45,50139,9493],{"class":2070},[45,50141,109],{"class":85},[14,50143,177],{},[36,50145,50147],{"className":38,"code":50146,"language":40,"meta":41,"style":41},"name\nage\n",[18,50148,50149,50154],{"__ignoreMap":41},[45,50150,50151],{"class":47,"line":48},[45,50152,50153],{"class":51},"name\n",[45,50155,50156],{"class":47,"line":63},[45,50157,50158],{"class":51},"age\n",[14,50160,50161,50162,457],{},"If you want both keys and values, you can use ",[18,50163,44499],{},[36,50165,50167],{"className":38,"code":50166,"language":40,"meta":41,"style":41},"person = {\n    \"name\": \"Maya\",\n    \"age\": 12\n}\n\nfor key, value in person.items():\n    print(key, value)\n",[18,50168,50169,50177,50195,50207,50211,50215,50235],{"__ignoreMap":41},[45,50170,50171,50173,50175],{"class":47,"line":48},[45,50172,2966],{"class":51},[45,50174,56],{"class":55},[45,50176,41432],{"class":85},[45,50178,50179,50181,50183,50185,50187,50189,50191,50193],{"class":47,"line":63},[45,50180,41437],{"class":99},[45,50182,3096],{"class":103},[45,50184,100],{"class":99},[45,50186,457],{"class":85},[45,50188,1565],{"class":99},[45,50190,5737],{"class":103},[45,50192,100],{"class":99},[45,50194,41453],{"class":85},[45,50196,50197,50199,50201,50203,50205],{"class":47,"line":70},[45,50198,41437],{"class":99},[45,50200,2082],{"class":103},[45,50202,100],{"class":99},[45,50204,457],{"class":85},[45,50206,1635],{"class":59},[45,50208,50209],{"class":47,"line":89},[45,50210,8629],{"class":85},[45,50212,50213],{"class":47,"line":112},[45,50214,67],{"emptyLinePlaceholder":66},[45,50216,50217,50219,50221,50223,50225,50227,50229,50231,50233],{"class":47,"line":126},[45,50218,7895],{"class":73},[45,50220,9502],{"class":51},[45,50222,3079],{"class":85},[45,50224,1492],{"class":51},[45,50226,8623],{"class":73},[45,50228,23838],{"class":51},[45,50230,240],{"class":85},[45,50232,9516],{"class":2070},[45,50234,3828],{"class":85},[45,50236,50237,50239,50241,50243,50245,50247],{"class":47,"line":142},[45,50238,93],{"class":92},[45,50240,96],{"class":85},[45,50242,9493],{"class":2070},[45,50244,3079],{"class":85},[45,50246,42794],{"class":2070},[45,50248,109],{"class":85},[14,50250,177],{},[36,50252,50254],{"className":38,"code":50253,"language":40,"meta":41,"style":41},"name Maya\nage 12\n",[18,50255,50256,50261],{"__ignoreMap":41},[45,50257,50258],{"class":47,"line":48},[45,50259,50260],{"class":51},"name Maya\n",[45,50262,50263,50265],{"class":47,"line":63},[45,50264,52],{"class":51},[45,50266,3629],{"class":59},[14,50268,50269],{},"Keep it simple at first:",[195,50271,50272,50275],{},[198,50273,50274],{},"looping over a string gives characters",[198,50276,50277],{},"looping over a dictionary gives keys by default",[31,50279,50281],{"id":50280},"using-break-and-continue","Using break and continue",[712,50283,35658],{"id":35658},[14,50285,50286,50288],{},[18,50287,35658],{}," stops the loop early.",[36,50290,50292],{"className":38,"code":50291,"language":40,"meta":41,"style":41},"for number in range(10):\n    if number == 4:\n        break\n    print(number)\n",[18,50293,50294,50310,50322,50326],{"__ignoreMap":41},[45,50295,50296,50298,50300,50302,50304,50306,50308],{"class":47,"line":48},[45,50297,7895],{"class":73},[45,50299,744],{"class":51},[45,50301,8623],{"class":73},[45,50303,10451],{"class":92},[45,50305,96],{"class":85},[45,50307,11786],{"class":59},[45,50309,2464],{"class":85},[45,50311,50312,50314,50316,50318,50320],{"class":47,"line":63},[45,50313,1913],{"class":73},[45,50315,744],{"class":51},[45,50317,1129],{"class":55},[45,50319,8591],{"class":59},[45,50321,86],{"class":85},[45,50323,50324],{"class":47,"line":70},[45,50325,23579],{"class":73},[45,50327,50328,50330,50332,50334],{"class":47,"line":89},[45,50329,93],{"class":92},[45,50331,96],{"class":85},[45,50333,13546],{"class":2070},[45,50335,109],{"class":85},[14,50337,177],{},[36,50339,50341],{"className":38,"code":50340,"language":40,"meta":41,"style":41},"0\n1\n2\n3\n",[18,50342,50343,50347,50351,50355],{"__ignoreMap":41},[45,50344,50345],{"class":47,"line":48},[45,50346,23957],{"class":59},[45,50348,50349],{"class":47,"line":63},[45,50350,13036],{"class":59},[45,50352,50353],{"class":47,"line":70},[45,50354,13041],{"class":59},[45,50356,50357],{"class":47,"line":89},[45,50358,13046],{"class":59},[14,50360,36050,50361,15579,50363,50365],{},[18,50362,13546],{},[18,50364,3584],{},", the loop ends.",[712,50367,35661],{"id":35661},[14,50369,50370,50372],{},[18,50371,35661],{}," skips the current loop and moves to the next one.",[36,50374,50376],{"className":38,"code":50375,"language":40,"meta":41,"style":41},"for number in range(5):\n    if number == 2:\n        continue\n    print(number)\n",[18,50377,50378,50394,50406,50410],{"__ignoreMap":41},[45,50379,50380,50382,50384,50386,50388,50390,50392],{"class":47,"line":48},[45,50381,7895],{"class":73},[45,50383,744],{"class":51},[45,50385,8623],{"class":73},[45,50387,10451],{"class":92},[45,50389,96],{"class":85},[45,50391,3749],{"class":59},[45,50393,2464],{"class":85},[45,50395,50396,50398,50400,50402,50404],{"class":47,"line":63},[45,50397,1913],{"class":73},[45,50399,744],{"class":51},[45,50401,1129],{"class":55},[45,50403,3754],{"class":59},[45,50405,86],{"class":85},[45,50407,50408],{"class":47,"line":70},[45,50409,35727],{"class":73},[45,50411,50412,50414,50416,50418],{"class":47,"line":89},[45,50413,93],{"class":92},[45,50415,96],{"class":85},[45,50417,13546],{"class":2070},[45,50419,109],{"class":85},[14,50421,177],{},[36,50423,50425],{"className":38,"code":50424,"language":40,"meta":41,"style":41},"0\n1\n3\n4\n",[18,50426,50427,50431,50435,50439],{"__ignoreMap":41},[45,50428,50429],{"class":47,"line":48},[45,50430,23957],{"class":59},[45,50432,50433],{"class":47,"line":63},[45,50434,13036],{"class":59},[45,50436,50437],{"class":47,"line":70},[45,50438,13046],{"class":59},[45,50440,50441],{"class":47,"line":89},[45,50442,13681],{"class":59},[14,50444,36177,50445,36998,50447,21,50449,50451],{},[18,50446,13546],{},[18,50448,9367],{},[18,50450,36187],{}," is skipped.",[14,50453,32733,50454,240],{},[236,50455,50457],{"href":50456},"\u002Flearn\u002Fpython-break-and-continue-statements\u002F","Python break and continue statements",[31,50459,50461],{"id":50460},"using-else-with-a-for-loop","Using else with a for loop",[14,50463,2419,50464,50466,50467,1864],{},[18,50465,7895],{}," loop can have an ",[18,50468,28],{},[14,50470,1732,50471,50473],{},[18,50472,28],{}," runs if the loop finishes normally.",[36,50475,50477],{"className":38,"code":50476,"language":40,"meta":41,"style":41},"for number in range(3):\n    print(number)\nelse:\n    print(\"Loop finished\")\n",[18,50478,50479,50495,50505,50511],{"__ignoreMap":41},[45,50480,50481,50483,50485,50487,50489,50491,50493],{"class":47,"line":48},[45,50482,7895],{"class":73},[45,50484,744],{"class":51},[45,50486,8623],{"class":73},[45,50488,10451],{"class":92},[45,50490,96],{"class":85},[45,50492,11184],{"class":59},[45,50494,2464],{"class":85},[45,50496,50497,50499,50501,50503],{"class":47,"line":63},[45,50498,93],{"class":92},[45,50500,96],{"class":85},[45,50502,13546],{"class":2070},[45,50504,109],{"class":85},[45,50506,50507,50509],{"class":47,"line":70},[45,50508,28],{"class":73},[45,50510,86],{"class":85},[45,50512,50513,50515,50517,50519,50522,50524],{"class":47,"line":89},[45,50514,93],{"class":92},[45,50516,96],{"class":85},[45,50518,100],{"class":99},[45,50520,50521],{"class":103},"Loop finished",[45,50523,100],{"class":99},[45,50525,109],{"class":85},[14,50527,177],{},[36,50529,50531],{"className":38,"code":50530,"language":40,"meta":41,"style":41},"0\n1\n2\nLoop finished\n",[18,50532,50533,50537,50541,50545],{"__ignoreMap":41},[45,50534,50535],{"class":47,"line":48},[45,50536,23957],{"class":59},[45,50538,50539],{"class":47,"line":63},[45,50540,13036],{"class":59},[45,50542,50543],{"class":47,"line":70},[45,50544,13041],{"class":59},[45,50546,50547],{"class":47,"line":89},[45,50548,50549],{"class":51},"Loop finished\n",[14,50551,50552,50553,50555,50556,50558],{},"But if ",[18,50554,35658],{}," happens, the ",[18,50557,28],{}," block does not run.",[36,50560,50562],{"className":38,"code":50561,"language":40,"meta":41,"style":41},"for number in range(3):\n    if number == 1:\n        break\n    print(number)\nelse:\n    print(\"Loop finished\")\n",[18,50563,50564,50580,50592,50596,50606,50612],{"__ignoreMap":41},[45,50565,50566,50568,50570,50572,50574,50576,50578],{"class":47,"line":48},[45,50567,7895],{"class":73},[45,50569,744],{"class":51},[45,50571,8623],{"class":73},[45,50573,10451],{"class":92},[45,50575,96],{"class":85},[45,50577,11184],{"class":59},[45,50579,2464],{"class":85},[45,50581,50582,50584,50586,50588,50590],{"class":47,"line":63},[45,50583,1913],{"class":73},[45,50585,744],{"class":51},[45,50587,1129],{"class":55},[45,50589,9030],{"class":59},[45,50591,86],{"class":85},[45,50593,50594],{"class":47,"line":70},[45,50595,23579],{"class":73},[45,50597,50598,50600,50602,50604],{"class":47,"line":89},[45,50599,93],{"class":92},[45,50601,96],{"class":85},[45,50603,13546],{"class":2070},[45,50605,109],{"class":85},[45,50607,50608,50610],{"class":47,"line":112},[45,50609,28],{"class":73},[45,50611,86],{"class":85},[45,50613,50614,50616,50618,50620,50622,50624],{"class":47,"line":126},[45,50615,93],{"class":92},[45,50617,96],{"class":85},[45,50619,100],{"class":99},[45,50621,50521],{"class":103},[45,50623,100],{"class":99},[45,50625,109],{"class":85},[14,50627,177],{},[36,50629,50630],{"className":38,"code":23957,"language":40,"meta":41,"style":41},[18,50631,50632],{"__ignoreMap":41},[45,50633,50634],{"class":47,"line":48},[45,50635,23957],{"class":59},[14,50637,50638],{},"This feature is real and useful sometimes, but many beginners do not need it right away.",[31,50640,1337],{"id":1336},[14,50642,50643,50644,36437],{},"Here are some common problems with ",[18,50645,7895],{},[712,50647,1453],{"id":1452},[14,50649,45194,50650,240],{},[18,50651,46018],{},[14,50653,3806],{},[36,50655,50657],{"className":38,"code":50656,"language":40,"meta":41,"style":41},"for item in [1, 2, 3]\n    print(item)\n",[18,50658,50659,50681],{"__ignoreMap":41},[45,50660,50661,50663,50665,50667,50669,50671,50673,50675,50677,50679],{"class":47,"line":48},[45,50662,7895],{"class":73},[45,50664,8732],{"class":51},[45,50666,8623],{"class":73},[45,50668,8575],{"class":85},[45,50670,8578],{"class":59},[45,50672,3079],{"class":85},[45,50674,3754],{"class":59},[45,50676,3079],{"class":85},[45,50678,777],{"class":59},[45,50680,8594],{"class":85},[45,50682,50683,50685,50687,50689],{"class":47,"line":63},[45,50684,93],{"class":92},[45,50686,96],{"class":85},[45,50688,23670],{"class":2070},[45,50690,109],{"class":85},[14,50692,1466],{},[36,50694,50696],{"className":38,"code":50695,"language":40,"meta":41,"style":41},"for item in [1, 2, 3]:\n    print(item)\n",[18,50697,50698,50720],{"__ignoreMap":41},[45,50699,50700,50702,50704,50706,50708,50710,50712,50714,50716,50718],{"class":47,"line":48},[45,50701,7895],{"class":73},[45,50703,8732],{"class":51},[45,50705,8623],{"class":73},[45,50707,8575],{"class":85},[45,50709,8578],{"class":59},[45,50711,3079],{"class":85},[45,50713,3754],{"class":59},[45,50715,3079],{"class":85},[45,50717,777],{"class":59},[45,50719,49606],{"class":85},[45,50721,50722,50724,50726,50728],{"class":47,"line":63},[45,50723,93],{"class":92},[45,50725,96],{"class":85},[45,50727,23670],{"class":2070},[45,50729,109],{"class":85},[712,50731,50733],{"id":50732},"using-bad-indentation","Using bad indentation",[14,50735,50736],{},"The code inside the loop must be indented.",[14,50738,3806],{},[36,50740,50742],{"className":38,"code":50741,"language":40,"meta":41,"style":41},"for item in [1, 2, 3]:\nprint(item)\n",[18,50743,50744,50766],{"__ignoreMap":41},[45,50745,50746,50748,50750,50752,50754,50756,50758,50760,50762,50764],{"class":47,"line":48},[45,50747,7895],{"class":73},[45,50749,8732],{"class":51},[45,50751,8623],{"class":73},[45,50753,8575],{"class":85},[45,50755,8578],{"class":59},[45,50757,3079],{"class":85},[45,50759,3754],{"class":59},[45,50761,3079],{"class":85},[45,50763,777],{"class":59},[45,50765,49606],{"class":85},[45,50767,50768,50770,50772,50774],{"class":47,"line":63},[45,50769,2065],{"class":92},[45,50771,96],{"class":85},[45,50773,23670],{"class":2070},[45,50775,109],{"class":85},[14,50777,1466],{},[36,50779,50780],{"className":38,"code":50695,"language":40,"meta":41,"style":41},[18,50781,50782,50804],{"__ignoreMap":41},[45,50783,50784,50786,50788,50790,50792,50794,50796,50798,50800,50802],{"class":47,"line":48},[45,50785,7895],{"class":73},[45,50787,8732],{"class":51},[45,50789,8623],{"class":73},[45,50791,8575],{"class":85},[45,50793,8578],{"class":59},[45,50795,3079],{"class":85},[45,50797,3754],{"class":59},[45,50799,3079],{"class":85},[45,50801,777],{"class":59},[45,50803,49606],{"class":85},[45,50805,50806,50808,50810,50812],{"class":47,"line":63},[45,50807,93],{"class":92},[45,50809,96],{"class":85},[45,50811,23670],{"class":2070},[45,50813,109],{"class":85},[14,50815,50816,50817,240],{},"If you get this error, see ",[236,50818,1614],{"href":1613},[712,50820,50822,50823,50825],{"id":50821},"expecting-range5-to-include-5","Expecting ",[18,50824,49860],{}," to include 5",[14,50827,50828,50829,240],{},"It does not include ",[18,50830,3749],{},[36,50832,50833],{"className":38,"code":49800,"language":40,"meta":41,"style":41},[18,50834,50835,50851],{"__ignoreMap":41},[45,50836,50837,50839,50841,50843,50845,50847,50849],{"class":47,"line":48},[45,50838,7895],{"class":73},[45,50840,744],{"class":51},[45,50842,8623],{"class":73},[45,50844,10451],{"class":92},[45,50846,96],{"class":85},[45,50848,3749],{"class":59},[45,50850,2464],{"class":85},[45,50852,50853,50855,50857,50859],{"class":47,"line":63},[45,50854,93],{"class":92},[45,50856,96],{"class":85},[45,50858,13546],{"class":2070},[45,50860,109],{"class":85},[14,50862,177],{},[36,50864,50865],{"className":38,"code":49833,"language":40,"meta":41,"style":41},[18,50866,50867,50871,50875,50879,50883],{"__ignoreMap":41},[45,50868,50869],{"class":47,"line":48},[45,50870,23957],{"class":59},[45,50872,50873],{"class":47,"line":63},[45,50874,13036],{"class":59},[45,50876,50877],{"class":47,"line":70},[45,50878,13041],{"class":59},[45,50880,50881],{"class":47,"line":89},[45,50882,13046],{"class":59},[45,50884,50885],{"class":47,"line":112},[45,50886,13681],{"class":59},[712,50888,50890],{"id":50889},"changing-the-loop-variable-and-expecting-the-original-collection-to-change","Changing the loop variable and expecting the original collection to change",[14,50892,50893],{},"This does not change the original list:",[36,50895,50897],{"className":38,"code":50896,"language":40,"meta":41,"style":41},"numbers = [1, 2, 3]\n\nfor number in numbers:\n    number = 99\n\nprint(numbers)\n",[18,50898,50899,50919,50923,50935,50944,50948],{"__ignoreMap":41},[45,50900,50901,50903,50905,50907,50909,50911,50913,50915,50917],{"class":47,"line":48},[45,50902,8570],{"class":51},[45,50904,56],{"class":55},[45,50906,8575],{"class":85},[45,50908,8578],{"class":59},[45,50910,3079],{"class":85},[45,50912,3754],{"class":59},[45,50914,3079],{"class":85},[45,50916,777],{"class":59},[45,50918,8594],{"class":85},[45,50920,50921],{"class":47,"line":63},[45,50922,67],{"emptyLinePlaceholder":66},[45,50924,50925,50927,50929,50931,50933],{"class":47,"line":70},[45,50926,7895],{"class":73},[45,50928,744],{"class":51},[45,50930,8623],{"class":73},[45,50932,8626],{"class":51},[45,50934,86],{"class":85},[45,50936,50937,50939,50941],{"class":47,"line":89},[45,50938,12957],{"class":51},[45,50940,56],{"class":55},[45,50942,50943],{"class":59}," 99\n",[45,50945,50946],{"class":47,"line":112},[45,50947,67],{"emptyLinePlaceholder":66},[45,50949,50950,50952,50954,50956],{"class":47,"line":126},[45,50951,2065],{"class":92},[45,50953,96],{"class":85},[45,50955,9069],{"class":2070},[45,50957,109],{"class":85},[14,50959,177],{},[36,50961,50962],{"className":38,"code":13375,"language":40,"meta":41,"style":41},[18,50963,50964],{"__ignoreMap":41},[45,50965,50966,50968,50970,50972,50974,50976,50978],{"class":47,"line":48},[45,50967,9680],{"class":85},[45,50969,8578],{"class":59},[45,50971,3079],{"class":85},[45,50973,3754],{"class":59},[45,50975,3079],{"class":85},[45,50977,777],{"class":59},[45,50979,8594],{"class":85},[14,50981,50982,50983,50985],{},"The loop variable ",[18,50984,13546],{}," is just a temporary name for each value.",[712,50987,50989],{"id":50988},"confusing-values-with-indexes","Confusing values with indexes",[14,50991,50992],{},"This loop gives values, not index positions:",[36,50994,50996],{"className":38,"code":50995,"language":40,"meta":41,"style":41},"animals = [\"cat\", \"dog\", \"bird\"]\n\nfor animal in animals:\n    print(animal)\n",[18,50997,50998,51032,51036,51050],{"__ignoreMap":41},[45,50999,51000,51003,51005,51007,51009,51011,51013,51015,51017,51019,51021,51023,51025,51028,51030],{"class":47,"line":48},[45,51001,51002],{"class":51},"animals ",[45,51004,56],{"class":55},[45,51006,8575],{"class":85},[45,51008,100],{"class":99},[45,51010,7035],{"class":103},[45,51012,100],{"class":99},[45,51014,3079],{"class":85},[45,51016,1565],{"class":99},[45,51018,9105],{"class":103},[45,51020,100],{"class":99},[45,51022,3079],{"class":85},[45,51024,1565],{"class":99},[45,51026,51027],{"class":103},"bird",[45,51029,100],{"class":99},[45,51031,8594],{"class":85},[45,51033,51034],{"class":47,"line":63},[45,51035,67],{"emptyLinePlaceholder":66},[45,51037,51038,51040,51043,51045,51048],{"class":47,"line":70},[45,51039,7895],{"class":73},[45,51041,51042],{"class":51}," animal ",[45,51044,8623],{"class":73},[45,51046,51047],{"class":51}," animals",[45,51049,86],{"class":85},[45,51051,51052,51054,51056,51058],{"class":47,"line":89},[45,51053,93],{"class":92},[45,51055,96],{"class":85},[45,51057,6989],{"class":2070},[45,51059,109],{"class":85},[14,51061,51062,51063,9417,51066,240],{},"If you need both the index and the value, use ",[18,51064,51065],{},"enumerate()",[236,51067,51069],{"href":51068},"\u002Fhow-to\u002Fhow-to-use-enumerate-in-python\u002F","how to use enumerate in Python",[31,51071,51073],{"id":51072},"when-to-use-a-for-loop-vs-while-loop","When to use a for loop vs while loop",[14,51075,19194,51076,51078],{},[18,51077,7895],{}," loop when:",[195,51080,51081,51084,51087],{},[198,51082,51083],{},"you are looping over items",[198,51085,51086],{},"you are looping over a known range",[198,51088,51089],{},"you know how many times something should happen",[14,51091,19194,51092,51078],{},[18,51093,23529],{},[195,51095,51096,51099],{},[198,51097,51098],{},"you want to keep going until a condition becomes false",[198,51100,51101],{},"you do not know in advance how many times the loop will run",[14,51103,51104],{},"Example idea:",[195,51106,51107,51112],{},[198,51108,51109,51111],{},[18,51110,7895],{},": go through every item in a list",[198,51113,51114,51116,51117],{},[18,51115,23529],{},": keep asking for input until the user enters ",[18,51118,51119],{},"\"quit\"",[14,51121,51122,51123,240],{},"For a full comparison, see ",[236,51124,35821],{"href":35820},[31,51126,51128],{"id":51127},"debugging-for-loop-problems","Debugging for loop problems",[14,51130,51131],{},"If your loop is not working as expected, these quick checks can help.",[712,51133,51135],{"id":51134},"check-the-current-value","Check the current value",[36,51137,51139],{"className":38,"code":51138,"language":40,"meta":41,"style":41},"print(item)\n",[18,51140,51141],{"__ignoreMap":41},[45,51142,51143,51145,51147,51149],{"class":47,"line":48},[45,51144,2065],{"class":92},[45,51146,96],{"class":85},[45,51148,23670],{"class":2070},[45,51150,109],{"class":85},[14,51152,51153],{},"This helps you see what the loop variable contains on each iteration.",[712,51155,51157],{"id":51156},"check-the-type-of-the-thing-you-are-looping-over","Check the type of the thing you are looping over",[36,51159,51161],{"className":38,"code":51160,"language":40,"meta":41,"style":41},"print(type(collection))\n",[18,51162,51163],{"__ignoreMap":41},[45,51164,51165,51167,51169,51171,51173,51175],{"class":47,"line":48},[45,51166,2065],{"class":92},[45,51168,96],{"class":85},[45,51170,4340],{"class":2109},[45,51172,96],{"class":85},[45,51174,49569],{"class":2070},[45,51176,4348],{"class":85},[14,51178,51179],{},"This is useful if you are not sure whether your variable is a list, string, dictionary, or something else.",[712,51181,51183,51184,51186],{"id":51182},"see-what-range5-really-contains","See what ",[18,51185,49860],{}," really contains",[36,51188,51190],{"className":38,"code":51189,"language":40,"meta":41,"style":41},"print(list(range(5)))\n",[18,51191,51192],{"__ignoreMap":41},[45,51193,51194,51196,51198,51200,51202,51204,51206,51208],{"class":47,"line":48},[45,51195,2065],{"class":92},[45,51197,96],{"class":85},[45,51199,14463],{"class":2109},[45,51201,96],{"class":85},[45,51203,49970],{"class":92},[45,51205,96],{"class":85},[45,51207,3749],{"class":59},[45,51209,51210],{"class":85},")))\n",[14,51212,177],{},[36,51214,51216],{"className":38,"code":51215,"language":40,"meta":41,"style":41},"[0, 1, 2, 3, 4]\n",[18,51217,51218],{"__ignoreMap":41},[45,51219,51220,51222,51224,51226,51228,51230,51232,51234,51236,51238,51240],{"class":47,"line":48},[45,51221,9680],{"class":85},[45,51223,10482],{"class":59},[45,51225,3079],{"class":85},[45,51227,9030],{"class":59},[45,51229,3079],{"class":85},[45,51231,3754],{"class":59},[45,51233,3079],{"class":85},[45,51235,777],{"class":59},[45,51237,3079],{"class":85},[45,51239,8591],{"class":59},[45,51241,8594],{"class":85},[712,51243,51245],{"id":51244},"check-dictionary-keys","Check dictionary keys",[36,51247,51249],{"className":38,"code":51248,"language":40,"meta":41,"style":41},"print(my_dict.keys())\n",[18,51250,51251],{"__ignoreMap":41},[45,51252,51253,51255,51257,51259,51261,51263],{"class":47,"line":48},[45,51254,2065],{"class":92},[45,51256,96],{"class":85},[45,51258,45577],{"class":2070},[45,51260,240],{"class":85},[45,51262,10636],{"class":2070},[45,51264,10777],{"class":85},[14,51266,51267],{},"This shows the keys in a dictionary.",[712,51269,51271],{"id":51270},"read-help-for-range","Read help for range()",[36,51273,51275],{"className":38,"code":51274,"language":40,"meta":41,"style":41},"help(range)\n",[18,51276,51277],{"__ignoreMap":41},[45,51278,51279,51281,51283,51285],{"class":47,"line":48},[45,51280,2104],{"class":92},[45,51282,96],{"class":85},[45,51284,49970],{"class":92},[45,51286,109],{"class":85},[14,51288,51289,51290,51292],{},"This can show how ",[18,51291,22305],{}," works directly in Python.",[14,51294,51295],{},"Common errors related to loops include:",[195,51297,51298,51305,51310,51315],{},[198,51299,51300,51302,51303],{},[18,51301,46018],{}," from a missing colon after ",[18,51304,7895],{},[198,51306,51307,51309],{},[18,51308,46140],{}," from missing or inconsistent indentation",[198,51311,51312,51314],{},[18,51313,7711],{}," when trying to loop over a non-iterable value like an integer",[198,51316,51317,51319],{},[18,51318,35168],{}," from using a variable that was never defined",[14,51321,51322,51323,240],{},"If you see a non-iterable error, see ",[236,51324,51326],{"href":51325},"\u002Ferrors\u002Ftypeerror-int-object-is-not-iterable-fix\u002F","TypeError: int object is not iterable",[31,51328,2281],{"id":2280},[712,51330,51332],{"id":51331},"what-does-a-for-loop-do-in-python","What does a for loop do in Python?",[14,51334,51335,51336,240],{},"It runs a block of code once for each item in an iterable such as a list, string, or ",[18,51337,49970],{},[712,51339,51341],{"id":51340},"what-can-i-loop-over-in-python","What can I loop over in Python?",[14,51343,51344,51345,51347],{},"You can loop over lists, tuples, strings, sets, dictionaries, and ",[18,51346,22305],{}," objects.",[712,51349,51351],{"id":51350},"does-range5-include-5","Does range(5) include 5?",[14,51353,51354,51355,21,51357,21,51359,21,51361,25,51363,240],{},"No. It gives ",[18,51356,10482],{},[18,51358,8578],{},[18,51360,9367],{},[18,51362,11184],{},[18,51364,3584],{},[712,51366,51368],{"id":51367},"what-is-the-difference-between-for-and-while-in-python","What is the difference between for and while in Python?",[14,51370,2419,51371,51373,51374,51376],{},[18,51372,7895],{}," loop is usually for going through items. A ",[18,51375,23529],{}," loop repeats while a condition is true.",[712,51378,51380],{"id":51379},"how-do-i-stop-a-for-loop-early","How do I stop a for loop early?",[14,51382,165,51383,51385],{},[18,51384,35658],{}," to exit the loop before it finishes.",[31,51387,2342],{"id":2341},[195,51389,51390,51394,51398,51402,51406,51411],{},[198,51391,51392],{},[236,51393,35821],{"href":35820},[198,51395,51396],{},[236,51397,50457],{"href":50456},[198,51399,51400],{},[236,51401,23972],{"href":23971},[198,51403,51404],{},[236,51405,37355],{"href":37354},[198,51407,51408],{},[236,51409,51410],{"href":51068},"How to use enumerate in Python",[198,51412,51413],{},[236,51414,554],{"href":553},[14,51416,51417,51418,51421,51422,51424],{},"Once you understand the basic ",[18,51419,51420],{},"for item in collection:"," pattern, you can use loops for many real Python tasks like printing values, counting with ",[18,51423,22305],{},", and processing data one item at a time.",[2367,51426,51427],{},"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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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":41,"searchDepth":63,"depth":63,"links":51429},[51430,51431,51432,51433,51434,51435,51439,51443,51444,51452,51453,51461,51468],{"id":33,"depth":63,"text":34},{"id":49481,"depth":63,"text":49482},{"id":8706,"depth":63,"text":8707},{"id":49656,"depth":63,"text":49657},{"id":49791,"depth":63,"text":49792},{"id":49993,"depth":63,"text":49994,"children":51436},[51437,51438],{"id":49997,"depth":70,"text":49998},{"id":44185,"depth":70,"text":44186},{"id":50280,"depth":63,"text":50281,"children":51440},[51441,51442],{"id":35658,"depth":70,"text":35658},{"id":35661,"depth":70,"text":35661},{"id":50460,"depth":63,"text":50461},{"id":1336,"depth":63,"text":1337,"children":51445},[51446,51447,51448,51450,51451],{"id":1452,"depth":70,"text":1453},{"id":50732,"depth":70,"text":50733},{"id":50821,"depth":70,"text":51449},"Expecting range(5) to include 5",{"id":50889,"depth":70,"text":50890},{"id":50988,"depth":70,"text":50989},{"id":51072,"depth":63,"text":51073},{"id":51127,"depth":63,"text":51128,"children":51454},[51455,51456,51457,51459,51460],{"id":51134,"depth":70,"text":51135},{"id":51156,"depth":70,"text":51157},{"id":51182,"depth":70,"text":51458},"See what range(5) really contains",{"id":51244,"depth":70,"text":51245},{"id":51270,"depth":70,"text":51271},{"id":2280,"depth":63,"text":2281,"children":51462},[51463,51464,51465,51466,51467],{"id":51331,"depth":70,"text":51332},{"id":51340,"depth":70,"text":51341},{"id":51350,"depth":70,"text":51351},{"id":51367,"depth":70,"text":51368},{"id":51379,"depth":70,"text":51380},{"id":2341,"depth":63,"text":2342},"Master python for loops explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-for-loops-explained",{"title":49349,"description":51469},"learn\u002Fpython-for-loops-explained","HEIiab2Yq8XSoRMek96eYs94xipHXnu3M07viULNZbU",{"id":51476,"title":51477,"body":51478,"description":53313,"extension":2399,"meta":53314,"navigation":66,"path":53315,"seo":53316,"stem":53317,"__hash__":53318},"content\u002Flearn\u002Fpython-functions-explained.md","Python Functions Explained",{"type":7,"value":51479,"toc":53277},[51480,51483,51486,51489,51491,51508,51514,51518,51521,51523,51534,51537,51540,51551,51554,51581,51584,51588,51591,51594,51607,51610,51658,51661,51712,51715,51719,51724,51727,51749,51751,51778,51782,51807,51810,51813,51840,51843,51869,51873,51876,51879,51916,51918,51926,51929,51932,51958,51964,51967,52016,52018,52035,52039,52042,52045,52047,52114,52116,52138,52140,52155,52161,52166,52170,52175,52178,52189,52191,52254,52256,52264,52272,52274,52284,52287,52405,52407,52420,52423,52437,52443,52452,52503,52505,52518,52523,52527,52530,52541,52543,52601,52603,52611,52615,52647,52650,52752,52754,52767,52774,52778,52781,52795,52798,52801,52806,52808,52811,52815,52818,52844,52847,52858,52862,52865,52906,52909,52924,52926,52938,52952,52960,52966,52970,52980,52984,52987,52990,52994,52997,53045,53050,53053,53109,53111,53114,53159,53161,53192,53199,53201,53204,53207,53211,53214,53218,53225,53229,53232,53236,53241,53243,53274],[10,51481,51477],{"id":51482},"python-functions-explained",[14,51484,51485],{},"Functions are one of the most useful parts of Python.",[14,51487,51488],{},"A function is a named block of code that does a specific job. You define it once, then call it whenever you need it. This helps you avoid repeating the same code and makes programs easier to read.",[14,51490,29403],{},[195,51492,51493,51496,51499,51502,51505],{},[198,51494,51495],{},"what a function is",[198,51497,51498],{},"why functions are useful",[198,51500,51501],{},"how to define and call a function",[198,51503,51504],{},"how parameters work at a basic level",[198,51506,51507],{},"how return values work",[14,51509,51510,51511,240],{},"If you want a shorter definition first, see ",[236,51512,51513],{"href":5820},"what is a function in Python",[31,51515,51517],{"id":51516},"what-a-function-is","What a function is",[14,51519,51520],{},"A function is a reusable block of code.",[14,51522,2727],{},[195,51524,51525,51528,51531],{},[198,51526,51527],{},"you write the code once",[198,51529,51530],{},"you give it a name",[198,51532,51533],{},"you run it when needed",[14,51535,51536],{},"Functions are useful because they let you group related code into one place.",[14,51538,51539],{},"A function can:",[195,51541,51542,51545,51548],{},[198,51543,51544],{},"take input values",[198,51546,51547],{},"do some work",[198,51549,51550],{},"produce an output value",[14,51552,51553],{},"Here is a very simple function:",[36,51555,51557],{"className":38,"code":51556,"language":40,"meta":41,"style":41},"def say_hello():\n    print(\"Hello\")\n",[18,51558,51559,51567],{"__ignoreMap":41},[45,51560,51561,51563,51565],{"class":47,"line":48},[45,51562,2757],{"class":2440},[45,51564,4596],{"class":2455},[45,51566,3828],{"class":85},[45,51568,51569,51571,51573,51575,51577,51579],{"class":47,"line":63},[45,51570,93],{"class":92},[45,51572,96],{"class":85},[45,51574,100],{"class":99},[45,51576,3226],{"class":103},[45,51578,100],{"class":99},[45,51580,109],{"class":85},[14,51582,51583],{},"This creates the function, but it does not run yet.",[31,51585,51587],{"id":51586},"why-functions-are-useful","Why functions are useful",[14,51589,51590],{},"Functions help you write better code.",[14,51592,51593],{},"They are useful because they:",[195,51595,51596,51598,51601,51604],{},[198,51597,6953],{},[198,51599,51600],{},"reduce repetition",[198,51602,51603],{},"break large programs into smaller parts",[198,51605,51606],{},"make testing and debugging easier",[14,51608,51609],{},"For example, without a function you might repeat the same lines many times:",[36,51611,51613],{"className":38,"code":51612,"language":40,"meta":41,"style":41},"print(\"Welcome!\")\nprint(\"Welcome!\")\nprint(\"Welcome!\")\n",[18,51614,51615,51630,51644],{"__ignoreMap":41},[45,51616,51617,51619,51621,51623,51626,51628],{"class":47,"line":48},[45,51618,2065],{"class":92},[45,51620,96],{"class":85},[45,51622,100],{"class":99},[45,51624,51625],{"class":103},"Welcome!",[45,51627,100],{"class":99},[45,51629,109],{"class":85},[45,51631,51632,51634,51636,51638,51640,51642],{"class":47,"line":63},[45,51633,2065],{"class":92},[45,51635,96],{"class":85},[45,51637,100],{"class":99},[45,51639,51625],{"class":103},[45,51641,100],{"class":99},[45,51643,109],{"class":85},[45,51645,51646,51648,51650,51652,51654,51656],{"class":47,"line":70},[45,51647,2065],{"class":92},[45,51649,96],{"class":85},[45,51651,100],{"class":99},[45,51653,51625],{"class":103},[45,51655,100],{"class":99},[45,51657,109],{"class":85},[14,51659,51660],{},"With a function, you can reuse the same code:",[36,51662,51664],{"className":38,"code":51663,"language":40,"meta":41,"style":41},"def show_welcome():\n    print(\"Welcome!\")\n\nshow_welcome()\nshow_welcome()\nshow_welcome()\n",[18,51665,51666,51675,51689,51693,51700,51706],{"__ignoreMap":41},[45,51667,51668,51670,51673],{"class":47,"line":48},[45,51669,2757],{"class":2440},[45,51671,51672],{"class":2455}," show_welcome",[45,51674,3828],{"class":85},[45,51676,51677,51679,51681,51683,51685,51687],{"class":47,"line":63},[45,51678,93],{"class":92},[45,51680,96],{"class":85},[45,51682,100],{"class":99},[45,51684,51625],{"class":103},[45,51686,100],{"class":99},[45,51688,109],{"class":85},[45,51690,51691],{"class":47,"line":70},[45,51692,67],{"emptyLinePlaceholder":66},[45,51694,51695,51698],{"class":47,"line":89},[45,51696,51697],{"class":2070},"show_welcome",[45,51699,2495],{"class":85},[45,51701,51702,51704],{"class":47,"line":112},[45,51703,51697],{"class":2070},[45,51705,2495],{"class":85},[45,51707,51708,51710],{"class":47,"line":126},[45,51709,51697],{"class":2070},[45,51711,2495],{"class":85},[14,51713,51714],{},"This is easier to change later. If you want to update the message, you only change it in one place.",[31,51716,51718],{"id":51717},"how-to-define-a-function","How to define a function",[14,51720,51721,51722,25144],{},"To define a function in Python, use the ",[18,51723,2757],{},[14,51725,51726],{},"A basic function definition has these parts:",[195,51728,51729,51733,51736,51741,51746],{},[198,51730,51731],{},[18,51732,2757],{},[198,51734,51735],{},"the function name",[198,51737,51738,51739],{},"parentheses ",[18,51740,3772],{},[198,51742,51743,51744],{},"a colon ",[18,51745,457],{},[198,51747,51748],{},"an indented block of code",[14,51750,466],{},[36,51752,51754],{"className":38,"code":51753,"language":40,"meta":41,"style":41},"def greet():\n    print(\"Hello\")\n",[18,51755,51756,51764],{"__ignoreMap":41},[45,51757,51758,51760,51762],{"class":47,"line":48},[45,51759,2757],{"class":2440},[45,51761,2936],{"class":2455},[45,51763,3828],{"class":85},[45,51765,51766,51768,51770,51772,51774,51776],{"class":47,"line":63},[45,51767,93],{"class":92},[45,51769,96],{"class":85},[45,51771,100],{"class":99},[45,51773,3226],{"class":103},[45,51775,100],{"class":99},[45,51777,109],{"class":85},[712,51779,51781],{"id":51780},"what-each-part-means","What each part means",[195,51783,51784,51789,51794,51799,51804],{},[198,51785,51786,51788],{},[18,51787,2757],{}," tells Python you are creating a function",[198,51790,51791,51793],{},[18,51792,2982],{}," is the function name",[198,51795,51796,51798],{},[18,51797,3772],{}," means this function takes no input values",[198,51800,51801,51803],{},[18,51802,457],{}," starts the function body",[198,51805,51806],{},"the indented line is the code that belongs to the function",[14,51808,51809],{},"Indentation matters in Python. The code inside the function must be indented.",[14,51811,51812],{},"This will cause an error:",[36,51814,51816],{"className":38,"code":51815,"language":40,"meta":41,"style":41},"def greet():\nprint(\"Hello\")\n",[18,51817,51818,51826],{"__ignoreMap":41},[45,51819,51820,51822,51824],{"class":47,"line":48},[45,51821,2757],{"class":2440},[45,51823,2936],{"class":2455},[45,51825,3828],{"class":85},[45,51827,51828,51830,51832,51834,51836,51838],{"class":47,"line":63},[45,51829,2065],{"class":92},[45,51831,96],{"class":85},[45,51833,100],{"class":99},[45,51835,3226],{"class":103},[45,51837,100],{"class":99},[45,51839,109],{"class":85},[14,51841,51842],{},"This works correctly:",[36,51844,51845],{"className":38,"code":51753,"language":40,"meta":41,"style":41},[18,51846,51847,51855],{"__ignoreMap":41},[45,51848,51849,51851,51853],{"class":47,"line":48},[45,51850,2757],{"class":2440},[45,51852,2936],{"class":2455},[45,51854,3828],{"class":85},[45,51856,51857,51859,51861,51863,51865,51867],{"class":47,"line":63},[45,51858,93],{"class":92},[45,51860,96],{"class":85},[45,51862,100],{"class":99},[45,51864,3226],{"class":103},[45,51866,100],{"class":99},[45,51868,109],{"class":85},[31,51870,51872],{"id":51871},"how-to-call-a-function","How to call a function",[14,51874,51875],{},"Calling a function means running it.",[14,51877,51878],{},"To call a function, write its name followed by parentheses:",[36,51880,51882],{"className":38,"code":51881,"language":40,"meta":41,"style":41},"def greet():\n    print(\"Hello\")\n\ngreet()\n",[18,51883,51884,51892,51906,51910],{"__ignoreMap":41},[45,51885,51886,51888,51890],{"class":47,"line":48},[45,51887,2757],{"class":2440},[45,51889,2936],{"class":2455},[45,51891,3828],{"class":85},[45,51893,51894,51896,51898,51900,51902,51904],{"class":47,"line":63},[45,51895,93],{"class":92},[45,51897,96],{"class":85},[45,51899,100],{"class":99},[45,51901,3226],{"class":103},[45,51903,100],{"class":99},[45,51905,109],{"class":85},[45,51907,51908],{"class":47,"line":70},[45,51909,67],{"emptyLinePlaceholder":66},[45,51911,51912,51914],{"class":47,"line":89},[45,51913,2982],{"class":2070},[45,51915,2495],{"class":85},[14,51917,177],{},[36,51919,51920],{"className":38,"code":15823,"language":40,"meta":41,"style":41},[18,51921,51922],{"__ignoreMap":41},[45,51923,51924],{"class":47,"line":48},[45,51925,15823],{"class":51},[14,51927,51928],{},"A function does nothing until it is called.",[14,51930,51931],{},"In this example, the function is defined but never used:",[36,51933,51934],{"className":38,"code":51753,"language":40,"meta":41,"style":41},[18,51935,51936,51944],{"__ignoreMap":41},[45,51937,51938,51940,51942],{"class":47,"line":48},[45,51939,2757],{"class":2440},[45,51941,2936],{"class":2455},[45,51943,3828],{"class":85},[45,51945,51946,51948,51950,51952,51954,51956],{"class":47,"line":63},[45,51947,93],{"class":92},[45,51949,96],{"class":85},[45,51951,100],{"class":99},[45,51953,3226],{"class":103},[45,51955,100],{"class":99},[45,51957,109],{"class":85},[14,51959,51960,51961,51963],{},"Since there is no ",[18,51962,2583],{}," call, nothing is printed.",[14,51965,51966],{},"You can call the same function many times:",[36,51968,51970],{"className":38,"code":51969,"language":40,"meta":41,"style":41},"def greet():\n    print(\"Hello\")\n\ngreet()\ngreet()\ngreet()\n",[18,51971,51972,51980,51994,51998,52004,52010],{"__ignoreMap":41},[45,51973,51974,51976,51978],{"class":47,"line":48},[45,51975,2757],{"class":2440},[45,51977,2936],{"class":2455},[45,51979,3828],{"class":85},[45,51981,51982,51984,51986,51988,51990,51992],{"class":47,"line":63},[45,51983,93],{"class":92},[45,51985,96],{"class":85},[45,51987,100],{"class":99},[45,51989,3226],{"class":103},[45,51991,100],{"class":99},[45,51993,109],{"class":85},[45,51995,51996],{"class":47,"line":70},[45,51997,67],{"emptyLinePlaceholder":66},[45,51999,52000,52002],{"class":47,"line":89},[45,52001,2982],{"class":2070},[45,52003,2495],{"class":85},[45,52005,52006,52008],{"class":47,"line":112},[45,52007,2982],{"class":2070},[45,52009,2495],{"class":85},[45,52011,52012,52014],{"class":47,"line":126},[45,52013,2982],{"class":2070},[45,52015,2495],{"class":85},[14,52017,177],{},[36,52019,52021],{"className":38,"code":52020,"language":40,"meta":41,"style":41},"Hello\nHello\nHello\n",[18,52022,52023,52027,52031],{"__ignoreMap":41},[45,52024,52025],{"class":47,"line":48},[45,52026,15823],{"class":51},[45,52028,52029],{"class":47,"line":63},[45,52030,15823],{"class":51},[45,52032,52033],{"class":47,"line":70},[45,52034,15823],{"class":51},[31,52036,52038],{"id":52037},"functions-with-parameters","Functions with parameters",[14,52040,52041],{},"Parameters are names for input values inside a function.",[14,52043,52044],{},"They let one function work with different data.",[14,52046,466],{},[36,52048,52050],{"className":38,"code":52049,"language":40,"meta":41,"style":41},"def greet(name):\n    print(\"Hello,\", name)\n\ngreet(\"Ava\")\ngreet(\"Noah\")\n",[18,52051,52052,52064,52082,52086,52100],{"__ignoreMap":41},[45,52053,52054,52056,52058,52060,52062],{"class":47,"line":48},[45,52055,2757],{"class":2440},[45,52057,2936],{"class":2455},[45,52059,96],{"class":85},[45,52061,3096],{"class":3082},[45,52063,2464],{"class":85},[45,52065,52066,52068,52070,52072,52074,52076,52078,52080],{"class":47,"line":63},[45,52067,93],{"class":92},[45,52069,96],{"class":85},[45,52071,100],{"class":99},[45,52073,18065],{"class":103},[45,52075,100],{"class":99},[45,52077,3079],{"class":85},[45,52079,3083],{"class":2070},[45,52081,109],{"class":85},[45,52083,52084],{"class":47,"line":70},[45,52085,67],{"emptyLinePlaceholder":66},[45,52087,52088,52090,52092,52094,52096,52098],{"class":47,"line":89},[45,52089,2982],{"class":2070},[45,52091,96],{"class":85},[45,52093,100],{"class":99},[45,52095,3162],{"class":103},[45,52097,100],{"class":99},[45,52099,109],{"class":85},[45,52101,52102,52104,52106,52108,52110,52112],{"class":47,"line":112},[45,52103,2982],{"class":2070},[45,52105,96],{"class":85},[45,52107,100],{"class":99},[45,52109,20818],{"class":103},[45,52111,100],{"class":99},[45,52113,109],{"class":85},[14,52115,177],{},[36,52117,52119],{"className":38,"code":52118,"language":40,"meta":41,"style":41},"Hello, Ava\nHello, Noah\n",[18,52120,52121,52129],{"__ignoreMap":41},[45,52122,52123,52125,52127],{"class":47,"line":48},[45,52124,3226],{"class":51},[45,52126,3079],{"class":85},[45,52128,3236],{"class":51},[45,52130,52131,52133,52135],{"class":47,"line":63},[45,52132,3226],{"class":51},[45,52134,3079],{"class":85},[45,52136,52137],{"class":51}," Noah\n",[14,52139,6570],{},[195,52141,52142,52146],{},[198,52143,52144,11387],{},[18,52145,3096],{},[198,52147,52148,1316,52151,52154],{},[18,52149,52150],{},"\"Ava\"",[18,52152,52153],{},"\"Noah\""," are arguments",[14,52156,52157,52158,52160],{},"A parameter is the variable name in the function definition.",[7343,52159],{},"\nAn argument is the value you pass when calling the function.",[14,52162,52163,52164,240],{},"This is just the basic idea. For a full beginner guide, see ",[236,52165,5782],{"href":4583},[31,52167,52169],{"id":52168},"return-values","Return values",[14,52171,165,52172,52174],{},[18,52173,3461],{}," when you want a function to send a value back.",[14,52176,52177],{},"That returned value can then be:",[195,52179,52180,52183,52186],{},[198,52181,52182],{},"stored in a variable",[198,52184,52185],{},"used in another expression",[198,52187,52188],{},"printed later",[14,52190,466],{},[36,52192,52194],{"className":38,"code":52193,"language":40,"meta":41,"style":41},"def add_numbers(a, b):\n    return a + b\n\nresult = add_numbers(3, 4)\nprint(result)\n",[18,52195,52196,52212,52222,52226,52244],{"__ignoreMap":41},[45,52197,52198,52200,52202,52204,52206,52208,52210],{"class":47,"line":48},[45,52199,2757],{"class":2440},[45,52201,33053],{"class":2455},[45,52203,96],{"class":85},[45,52205,236],{"class":3082},[45,52207,3079],{"class":85},[45,52209,11115],{"class":3082},[45,52211,2464],{"class":85},[45,52213,52214,52216,52218,52220],{"class":47,"line":63},[45,52215,4681],{"class":73},[45,52217,11124],{"class":51},[45,52219,8850],{"class":55},[45,52221,11129],{"class":51},[45,52223,52224],{"class":47,"line":70},[45,52225,67],{"emptyLinePlaceholder":66},[45,52227,52228,52230,52232,52234,52236,52238,52240,52242],{"class":47,"line":89},[45,52229,3595],{"class":51},[45,52231,56],{"class":55},[45,52233,33053],{"class":2070},[45,52235,96],{"class":85},[45,52237,11184],{"class":59},[45,52239,3079],{"class":85},[45,52241,8591],{"class":59},[45,52243,109],{"class":85},[45,52245,52246,52248,52250,52252],{"class":47,"line":112},[45,52247,2065],{"class":92},[45,52249,96],{"class":85},[45,52251,3620],{"class":2070},[45,52253,109],{"class":85},[14,52255,177],{},[36,52257,52258],{"className":38,"code":35999,"language":40,"meta":41,"style":41},[18,52259,52260],{"__ignoreMap":41},[45,52261,52262],{"class":47,"line":48},[45,52263,35999],{"class":59},[712,52265,52267,1316,52269,52271],{"id":52266},"print-and-return-are-not-the-same",[18,52268,15833],{},[18,52270,3461],{}," are not the same",[14,52273,1333],{},[14,52275,52276,52278,52279,52281,52283],{},[18,52277,15833],{}," shows something on the screen.",[7343,52280],{},[18,52282,3461],{}," sends a value back from the function.",[14,52285,52286],{},"Compare these two functions:",[36,52288,52290],{"className":38,"code":52289,"language":40,"meta":41,"style":41},"def show_sum(a, b):\n    print(a + b)\n\ndef get_sum(a, b):\n    return a + b\n\nshow_sum(3, 4)\n\nresult = get_sum(3, 4)\nprint(result)\n",[18,52291,52292,52309,52323,52327,52344,52354,52358,52373,52377,52395],{"__ignoreMap":41},[45,52293,52294,52296,52299,52301,52303,52305,52307],{"class":47,"line":48},[45,52295,2757],{"class":2440},[45,52297,52298],{"class":2455}," show_sum",[45,52300,96],{"class":85},[45,52302,236],{"class":3082},[45,52304,3079],{"class":85},[45,52306,11115],{"class":3082},[45,52308,2464],{"class":85},[45,52310,52311,52313,52315,52317,52319,52321],{"class":47,"line":63},[45,52312,93],{"class":92},[45,52314,96],{"class":85},[45,52316,11541],{"class":2070},[45,52318,8850],{"class":55},[45,52320,11115],{"class":2070},[45,52322,109],{"class":85},[45,52324,52325],{"class":47,"line":70},[45,52326,67],{"emptyLinePlaceholder":66},[45,52328,52329,52331,52334,52336,52338,52340,52342],{"class":47,"line":89},[45,52330,2757],{"class":2440},[45,52332,52333],{"class":2455}," get_sum",[45,52335,96],{"class":85},[45,52337,236],{"class":3082},[45,52339,3079],{"class":85},[45,52341,11115],{"class":3082},[45,52343,2464],{"class":85},[45,52345,52346,52348,52350,52352],{"class":47,"line":112},[45,52347,4681],{"class":73},[45,52349,11124],{"class":51},[45,52351,8850],{"class":55},[45,52353,11129],{"class":51},[45,52355,52356],{"class":47,"line":126},[45,52357,67],{"emptyLinePlaceholder":66},[45,52359,52360,52363,52365,52367,52369,52371],{"class":47,"line":142},[45,52361,52362],{"class":2070},"show_sum",[45,52364,96],{"class":85},[45,52366,11184],{"class":59},[45,52368,3079],{"class":85},[45,52370,8591],{"class":59},[45,52372,109],{"class":85},[45,52374,52375],{"class":47,"line":149},[45,52376,67],{"emptyLinePlaceholder":66},[45,52378,52379,52381,52383,52385,52387,52389,52391,52393],{"class":47,"line":679},[45,52380,3595],{"class":51},[45,52382,56],{"class":55},[45,52384,52333],{"class":2070},[45,52386,96],{"class":85},[45,52388,11184],{"class":59},[45,52390,3079],{"class":85},[45,52392,8591],{"class":59},[45,52394,109],{"class":85},[45,52396,52397,52399,52401,52403],{"class":47,"line":686},[45,52398,2065],{"class":92},[45,52400,96],{"class":85},[45,52402,3620],{"class":2070},[45,52404,109],{"class":85},[14,52406,177],{},[36,52408,52410],{"className":38,"code":52409,"language":40,"meta":41,"style":41},"7\n7\n",[18,52411,52412,52416],{"__ignoreMap":41},[45,52413,52414],{"class":47,"line":48},[45,52415,35999],{"class":59},[45,52417,52418],{"class":47,"line":63},[45,52419,35999],{"class":59},[14,52421,52422],{},"The output looks similar, but the functions behave differently.",[195,52424,52425,52431],{},[198,52426,52427,52430],{},[18,52428,52429],{},"show_sum()"," prints the result, but does not return it",[198,52432,52433,52436],{},[18,52434,52435],{},"get_sum()"," returns the result, so you can store and reuse it",[712,52438,52440,52441],{"id":52439},"what-happens-if-there-is-no-return","What happens if there is no ",[18,52442,3461],{},[14,52444,52445,52446,52448,52449,52451],{},"If a function does not use ",[18,52447,3461],{},", Python returns ",[18,52450,5997],{}," by default.",[36,52453,52455],{"className":38,"code":52454,"language":40,"meta":41,"style":41},"def greet():\n    print(\"Hello\")\n\nresult = greet()\nprint(result)\n",[18,52456,52457,52465,52479,52483,52493],{"__ignoreMap":41},[45,52458,52459,52461,52463],{"class":47,"line":48},[45,52460,2757],{"class":2440},[45,52462,2936],{"class":2455},[45,52464,3828],{"class":85},[45,52466,52467,52469,52471,52473,52475,52477],{"class":47,"line":63},[45,52468,93],{"class":92},[45,52470,96],{"class":85},[45,52472,100],{"class":99},[45,52474,3226],{"class":103},[45,52476,100],{"class":99},[45,52478,109],{"class":85},[45,52480,52481],{"class":47,"line":70},[45,52482,67],{"emptyLinePlaceholder":66},[45,52484,52485,52487,52489,52491],{"class":47,"line":89},[45,52486,3595],{"class":51},[45,52488,56],{"class":55},[45,52490,2936],{"class":2070},[45,52492,2495],{"class":85},[45,52494,52495,52497,52499,52501],{"class":47,"line":112},[45,52496,2065],{"class":92},[45,52498,96],{"class":85},[45,52500,3620],{"class":2070},[45,52502,109],{"class":85},[14,52504,177],{},[36,52506,52508],{"className":38,"code":52507,"language":40,"meta":41,"style":41},"Hello\nNone\n",[18,52509,52510,52514],{"__ignoreMap":41},[45,52511,52512],{"class":47,"line":48},[45,52513,15823],{"class":51},[45,52515,52516],{"class":47,"line":63},[45,52517,31576],{"class":481},[14,52519,52520,52521,240],{},"If you want to learn this in more detail, see ",[236,52522,5885],{"href":5884},[31,52524,52526],{"id":52525},"simple-example-flow","Simple example flow",[14,52528,52529],{},"A good way to understand functions is to follow the usual order:",[346,52531,52532,52535,52538],{},[198,52533,52534],{},"define the function",[198,52536,52537],{},"call the function",[198,52539,52540],{},"see the result",[14,52542,2617],{},[36,52544,52546],{"className":38,"code":52545,"language":40,"meta":41,"style":41},"def multiply_by_two(number):\n    return number * 2\n\nanswer = multiply_by_two(5)\nprint(answer)\n",[18,52547,52548,52561,52571,52575,52590],{"__ignoreMap":41},[45,52549,52550,52552,52555,52557,52559],{"class":47,"line":48},[45,52551,2757],{"class":2440},[45,52553,52554],{"class":2455}," multiply_by_two",[45,52556,96],{"class":85},[45,52558,13546],{"class":3082},[45,52560,2464],{"class":85},[45,52562,52563,52565,52567,52569],{"class":47,"line":63},[45,52564,4681],{"class":73},[45,52566,744],{"class":51},[45,52568,5601],{"class":55},[45,52570,13423],{"class":59},[45,52572,52573],{"class":47,"line":70},[45,52574,67],{"emptyLinePlaceholder":66},[45,52576,52577,52580,52582,52584,52586,52588],{"class":47,"line":89},[45,52578,52579],{"class":51},"answer ",[45,52581,56],{"class":55},[45,52583,52554],{"class":2070},[45,52585,96],{"class":85},[45,52587,3749],{"class":59},[45,52589,109],{"class":85},[45,52591,52592,52594,52596,52599],{"class":47,"line":112},[45,52593,2065],{"class":92},[45,52595,96],{"class":85},[45,52597,52598],{"class":2070},"answer",[45,52600,109],{"class":85},[14,52602,177],{},[36,52604,52605],{"className":38,"code":13262,"language":40,"meta":41,"style":41},[18,52606,52607],{"__ignoreMap":41},[45,52608,52609],{"class":47,"line":48},[45,52610,13262],{"class":59},[712,52612,52614],{"id":52613},"step-by-step","Step by step",[195,52616,52617,52623,52628,52634,52642],{},[198,52618,52619,52622],{},[18,52620,52621],{},"def multiply_by_two(number):"," defines the function",[198,52624,52625,52627],{},[18,52626,13546],{}," is the input",[198,52629,52630,52633],{},[18,52631,52632],{},"return number * 2"," sends back the result",[198,52635,52636,52639,52640],{},[18,52637,52638],{},"multiply_by_two(5)"," calls the function with ",[18,52641,3749],{},[198,52643,52644,52645],{},"the returned value is stored in ",[18,52646,52598],{},[14,52648,52649],{},"Now compare a print-based function and a return-based function:",[36,52651,52653],{"className":38,"code":52652,"language":40,"meta":41,"style":41},"def print_double(number):\n    print(number * 2)\n\ndef return_double(number):\n    return number * 2\n\nprint_double(5)\n\nvalue = return_double(5)\nprint(value)\n",[18,52654,52655,52668,52682,52686,52699,52709,52713,52724,52728,52742],{"__ignoreMap":41},[45,52656,52657,52659,52662,52664,52666],{"class":47,"line":48},[45,52658,2757],{"class":2440},[45,52660,52661],{"class":2455}," print_double",[45,52663,96],{"class":85},[45,52665,13546],{"class":3082},[45,52667,2464],{"class":85},[45,52669,52670,52672,52674,52676,52678,52680],{"class":47,"line":63},[45,52671,93],{"class":92},[45,52673,96],{"class":85},[45,52675,728],{"class":2070},[45,52677,5601],{"class":55},[45,52679,3754],{"class":59},[45,52681,109],{"class":85},[45,52683,52684],{"class":47,"line":70},[45,52685,67],{"emptyLinePlaceholder":66},[45,52687,52688,52690,52693,52695,52697],{"class":47,"line":89},[45,52689,2757],{"class":2440},[45,52691,52692],{"class":2455}," return_double",[45,52694,96],{"class":85},[45,52696,13546],{"class":3082},[45,52698,2464],{"class":85},[45,52700,52701,52703,52705,52707],{"class":47,"line":112},[45,52702,4681],{"class":73},[45,52704,744],{"class":51},[45,52706,5601],{"class":55},[45,52708,13423],{"class":59},[45,52710,52711],{"class":47,"line":126},[45,52712,67],{"emptyLinePlaceholder":66},[45,52714,52715,52718,52720,52722],{"class":47,"line":142},[45,52716,52717],{"class":2070},"print_double",[45,52719,96],{"class":85},[45,52721,3749],{"class":59},[45,52723,109],{"class":85},[45,52725,52726],{"class":47,"line":149},[45,52727,67],{"emptyLinePlaceholder":66},[45,52729,52730,52732,52734,52736,52738,52740],{"class":47,"line":679},[45,52731,1476],{"class":51},[45,52733,56],{"class":55},[45,52735,52692],{"class":2070},[45,52737,96],{"class":85},[45,52739,3749],{"class":59},[45,52741,109],{"class":85},[45,52743,52744,52746,52748,52750],{"class":47,"line":686},[45,52745,2065],{"class":92},[45,52747,96],{"class":85},[45,52749,12722],{"class":2070},[45,52751,109],{"class":85},[14,52753,177],{},[36,52755,52757],{"className":38,"code":52756,"language":40,"meta":41,"style":41},"10\n10\n",[18,52758,52759,52763],{"__ignoreMap":41},[45,52760,52761],{"class":47,"line":48},[45,52762,13262],{"class":59},[45,52764,52765],{"class":47,"line":63},[45,52766,13262],{"class":59},[14,52768,52769,52770,52773],{},"Again, the screen output is similar, but only ",[18,52771,52772],{},"return_double()"," gives you a value you can save and use later.",[31,52775,52777],{"id":52776},"when-to-use-a-function","When to use a function",[14,52779,52780],{},"Use a function when:",[195,52782,52783,52786,52789,52792],{},[198,52784,52785],{},"the same task appears more than once",[198,52787,52788],{},"one part of the program should do one clear job",[198,52790,52791],{},"you want code that is easier to reuse",[198,52793,52794],{},"a long script needs to be split into smaller pieces",[14,52796,52797],{},"For example, if your program needs to calculate tax in several places, a function is better than repeating the same formula again and again.",[14,52799,52800],{},"Functions also make your code easier to understand. A good function name can explain what that part of the program does.",[14,52802,52803,52804,240],{},"If you want to practice writing one yourself, see ",[236,52805,5892],{"href":5891},[31,52807,5903],{"id":5902},[14,52809,52810],{},"Beginners often run into the same problems when learning functions.",[712,52812,52814],{"id":52813},"forgetting-to-call-the-function","Forgetting to call the function",[14,52816,52817],{},"Defining a function does not run it.",[36,52819,52820],{"className":38,"code":51753,"language":40,"meta":41,"style":41},[18,52821,52822,52830],{"__ignoreMap":41},[45,52823,52824,52826,52828],{"class":47,"line":48},[45,52825,2757],{"class":2440},[45,52827,2936],{"class":2455},[45,52829,3828],{"class":85},[45,52831,52832,52834,52836,52838,52840,52842],{"class":47,"line":63},[45,52833,93],{"class":92},[45,52835,96],{"class":85},[45,52837,100],{"class":99},[45,52839,3226],{"class":103},[45,52841,100],{"class":99},[45,52843,109],{"class":85},[14,52845,52846],{},"Nothing happens until you do this:",[36,52848,52850],{"className":38,"code":52849,"language":40,"meta":41,"style":41},"greet()\n",[18,52851,52852],{"__ignoreMap":41},[45,52853,52854,52856],{"class":47,"line":48},[45,52855,2982],{"class":2070},[45,52857,2495],{"class":85},[712,52859,52861],{"id":52860},"missing-parentheses-when-calling-a-function","Missing parentheses when calling a function",[14,52863,52864],{},"This prints the function object, not the result:",[36,52866,52868],{"className":38,"code":52867,"language":40,"meta":41,"style":41},"def greet():\n    print(\"Hello\")\n\nprint(greet)\n",[18,52869,52870,52878,52892,52896],{"__ignoreMap":41},[45,52871,52872,52874,52876],{"class":47,"line":48},[45,52873,2757],{"class":2440},[45,52875,2936],{"class":2455},[45,52877,3828],{"class":85},[45,52879,52880,52882,52884,52886,52888,52890],{"class":47,"line":63},[45,52881,93],{"class":92},[45,52883,96],{"class":85},[45,52885,100],{"class":99},[45,52887,3226],{"class":103},[45,52889,100],{"class":99},[45,52891,109],{"class":85},[45,52893,52894],{"class":47,"line":70},[45,52895,67],{"emptyLinePlaceholder":66},[45,52897,52898,52900,52902,52904],{"class":47,"line":89},[45,52899,2065],{"class":92},[45,52901,96],{"class":85},[45,52903,2982],{"class":2070},[45,52905,109],{"class":85},[14,52907,52908],{},"You need parentheses to call it:",[36,52910,52912],{"className":38,"code":52911,"language":40,"meta":41,"style":41},"print(greet())\n",[18,52913,52914],{"__ignoreMap":41},[45,52915,52916,52918,52920,52922],{"class":47,"line":48},[45,52917,2065],{"class":92},[45,52919,96],{"class":85},[45,52921,2982],{"class":2070},[45,52923,10777],{"class":85},[14,52925,177],{},[36,52927,52928],{"className":38,"code":52507,"language":40,"meta":41,"style":41},[18,52929,52930,52934],{"__ignoreMap":41},[45,52931,52932],{"class":47,"line":48},[45,52933,15823],{"class":51},[45,52935,52936],{"class":47,"line":63},[45,52937,31576],{"class":481},[14,52939,1732,52940,52942,52943,52945,52946,52948,52949,52951],{},[18,52941,3226],{}," comes from inside the function.",[7343,52944],{},"\nThe ",[18,52947,5997],{}," appears because ",[18,52950,2583],{}," does not return a value.",[712,52953,52955,52956,14527,52958],{"id":52954},"confusing-print-with-return","Confusing ",[18,52957,15833],{},[18,52959,3461],{},[14,52961,52962,52963,52965],{},"If you need to use the result later, ",[18,52964,15833],{}," is not enough.",[14,52967,165,52968,25114],{},[18,52969,3461],{},[14,52971,52972,52973,52975,52976,240],{},"If you are unsure how ",[18,52974,15833],{}," works, see ",[236,52977,18104,52978,19075],{"href":18110},[18,52979,15833],{},[712,52981,52983],{"id":52982},"using-bad-indentation-inside-the-function","Using bad indentation inside the function",[14,52985,52986],{},"Python uses indentation to know which lines belong to the function.",[14,52988,52989],{},"Incorrect indentation will cause an error or change how your code behaves.",[712,52991,52993],{"id":52992},"trying-to-use-a-variable-from-inside-the-function-outside-of-it","Trying to use a variable from inside the function outside of it",[14,52995,52996],{},"This does not work:",[36,52998,53000],{"className":38,"code":52999,"language":40,"meta":41,"style":41},"def make_message():\n    message = \"Hello\"\n\nmake_message()\nprint(message)\n",[18,53001,53002,53011,53024,53028,53035],{"__ignoreMap":41},[45,53003,53004,53006,53009],{"class":47,"line":48},[45,53005,2757],{"class":2440},[45,53007,53008],{"class":2455}," make_message",[45,53010,3828],{"class":85},[45,53012,53013,53016,53018,53020,53022],{"class":47,"line":63},[45,53014,53015],{"class":51},"    message ",[45,53017,56],{"class":55},[45,53019,1565],{"class":99},[45,53021,3226],{"class":103},[45,53023,1571],{"class":99},[45,53025,53026],{"class":47,"line":70},[45,53027,67],{"emptyLinePlaceholder":66},[45,53029,53030,53033],{"class":47,"line":89},[45,53031,53032],{"class":2070},"make_message",[45,53034,2495],{"class":85},[45,53036,53037,53039,53041,53043],{"class":47,"line":112},[45,53038,2065],{"class":92},[45,53040,96],{"class":85},[45,53042,6462],{"class":2070},[45,53044,109],{"class":85},[14,53046,53047,53049],{},[18,53048,6462],{}," only exists inside the function.",[14,53051,53052],{},"A simple fix is to return it:",[36,53054,53056],{"className":38,"code":53055,"language":40,"meta":41,"style":41},"def make_message():\n    message = \"Hello\"\n    return message\n\ntext = make_message()\nprint(text)\n",[18,53057,53058,53066,53078,53085,53089,53099],{"__ignoreMap":41},[45,53059,53060,53062,53064],{"class":47,"line":48},[45,53061,2757],{"class":2440},[45,53063,53008],{"class":2455},[45,53065,3828],{"class":85},[45,53067,53068,53070,53072,53074,53076],{"class":47,"line":63},[45,53069,53015],{"class":51},[45,53071,56],{"class":55},[45,53073,1565],{"class":99},[45,53075,3226],{"class":103},[45,53077,1571],{"class":99},[45,53079,53080,53082],{"class":47,"line":70},[45,53081,4681],{"class":73},[45,53083,53084],{"class":51}," message\n",[45,53086,53087],{"class":47,"line":89},[45,53088,67],{"emptyLinePlaceholder":66},[45,53090,53091,53093,53095,53097],{"class":47,"line":112},[45,53092,10297],{"class":51},[45,53094,56],{"class":55},[45,53096,53008],{"class":2070},[45,53098,2495],{"class":85},[45,53100,53101,53103,53105,53107],{"class":47,"line":126},[45,53102,2065],{"class":92},[45,53104,96],{"class":85},[45,53106,5000],{"class":2070},[45,53108,109],{"class":85},[712,53110,6205],{"id":6204},[14,53112,53113],{},"These commands can help you understand what is happening:",[36,53115,53117],{"className":38,"code":53116,"language":40,"meta":41,"style":41},"print(my_function)\nprint(my_function())\nhelp(print)\ntype(my_function)\n",[18,53118,53119,53129,53139,53149],{"__ignoreMap":41},[45,53120,53121,53123,53125,53127],{"class":47,"line":48},[45,53122,2065],{"class":92},[45,53124,96],{"class":85},[45,53126,5275],{"class":2070},[45,53128,109],{"class":85},[45,53130,53131,53133,53135,53137],{"class":47,"line":63},[45,53132,2065],{"class":92},[45,53134,96],{"class":85},[45,53136,5275],{"class":2070},[45,53138,10777],{"class":85},[45,53140,53141,53143,53145,53147],{"class":47,"line":70},[45,53142,2104],{"class":92},[45,53144,96],{"class":85},[45,53146,2065],{"class":92},[45,53148,109],{"class":85},[45,53150,53151,53153,53155,53157],{"class":47,"line":89},[45,53152,4340],{"class":2109},[45,53154,96],{"class":85},[45,53156,5275],{"class":2070},[45,53158,109],{"class":85},[14,53160,21783],{},[195,53162,53163,53172,53178,53186],{},[198,53164,53165,53168,53169,53171],{},[18,53166,53167],{},"print(my_function)"," shows that ",[18,53170,5275],{}," is a function",[198,53173,53174,53177],{},[18,53175,53176],{},"print(my_function())"," runs the function and prints its return value",[198,53179,53180,53183,53184,30521],{},[18,53181,53182],{},"help(print)"," shows help for the built-in ",[18,53185,15833],{},[198,53187,53188,53191],{},[18,53189,53190],{},"type(my_function)"," shows the object type",[14,53193,53194,53195,240],{},"If a function call gives an argument error, you may also need ",[236,53196,4025,53197],{"href":7724},[18,53198,4028],{},[31,53200,2281],{"id":2280},[712,53202,6333],{"id":53203},"what-is-a-function-in-python",[14,53205,53206],{},"A function is a named block of code that runs when you call it.",[712,53208,53210],{"id":53209},"why-should-i-use-functions","Why should I use functions?",[14,53212,53213],{},"Functions help you reuse code, reduce repetition, and keep programs organized.",[712,53215,53217],{"id":53216},"what-is-the-difference-between-print-and-return","What is the difference between print and return?",[14,53219,53220,53222,53223,52283],{},[18,53221,2065],{}," shows a value on the screen. ",[18,53224,3461],{},[712,53226,53228],{"id":53227},"can-a-function-run-without-parameters","Can a function run without parameters?",[14,53230,53231],{},"Yes. Some functions take no input values.",[712,53233,53235],{"id":53234},"what-happens-if-i-do-not-use-return","What happens if I do not use return?",[14,53237,53238,53239,52451],{},"The function returns ",[18,53240,5997],{},[31,53242,2342],{"id":2341},[195,53244,53245,53250,53254,53258,53262,53268],{},[198,53246,53247],{},[236,53248,53249],{"href":5820},"What is a function in Python",[198,53251,53252],{},[236,53253,6318],{"href":4583},[198,53255,53256],{},[236,53257,6323],{"href":5884},[198,53259,53260],{},[236,53261,6328],{"href":5891},[198,53263,53264],{},[236,53265,18104,53266,19075],{"href":18110},[18,53267,15833],{},[198,53269,53270],{},[236,53271,4497,53272],{"href":7724},[18,53273,4028],{},[2367,53275,53276],{},"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 .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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .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 .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":41,"searchDepth":63,"depth":63,"links":53278},[53279,53280,53281,53284,53285,53286,53292,53295,53296,53305,53312],{"id":51516,"depth":63,"text":51517},{"id":51586,"depth":63,"text":51587},{"id":51717,"depth":63,"text":51718,"children":53282},[53283],{"id":51780,"depth":70,"text":51781},{"id":51871,"depth":63,"text":51872},{"id":52037,"depth":63,"text":52038},{"id":52168,"depth":63,"text":52169,"children":53287},[53288,53290],{"id":52266,"depth":70,"text":53289},"print() and return are not the same",{"id":52439,"depth":70,"text":53291},"What happens if there is no return",{"id":52525,"depth":63,"text":52526,"children":53293},[53294],{"id":52613,"depth":70,"text":52614},{"id":52776,"depth":63,"text":52777},{"id":5902,"depth":63,"text":5903,"children":53297},[53298,53299,53300,53302,53303,53304],{"id":52813,"depth":70,"text":52814},{"id":52860,"depth":70,"text":52861},{"id":52954,"depth":70,"text":53301},"Confusing print() with return",{"id":52982,"depth":70,"text":52983},{"id":52992,"depth":70,"text":52993},{"id":6204,"depth":70,"text":6205},{"id":2280,"depth":63,"text":2281,"children":53306},[53307,53308,53309,53310,53311],{"id":53203,"depth":70,"text":6333},{"id":53209,"depth":70,"text":53210},{"id":53216,"depth":70,"text":53217},{"id":53227,"depth":70,"text":53228},{"id":53234,"depth":70,"text":53235},{"id":2341,"depth":63,"text":2342},"Master python functions explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-functions-explained",{"title":51477,"description":53313},"learn\u002Fpython-functions-explained","QIoG9mhR-grFFnDK5mfIGJ7M989eJQze6BGFR5TaBJI",{"id":4,"title":5,"body":53320,"description":2398,"extension":2399,"meta":55341,"navigation":66,"path":2401,"seo":55342,"stem":2403,"__hash__":2404},{"type":7,"value":53321,"toc":55314},[53322,53324,53332,53334,53422,53430,53432,53440,53442,53444,53466,53474,53476,53564,53566,53580,53582,53590,53592,53598,53602,53604,53650,53652,53662,53664,53722,53724,53732,53738,53740,53744,53748,53750,53864,53866,53874,53876,53878,53966,53968,53976,53980,53982,54070,54072,54074,54082,54104,54106,54194,54196,54212,54214,54222,54232,54234,54236,54238,54264,54266,54368,54370,54402,54408,54418,54420,54422,54430,54436,54438,54480,54484,54526,54528,54536,54538,54600,54604,54606,54608,54610,54660,54664,54666,54668,54756,54758,54768,54774,54776,54864,54866,54874,54878,54880,54886,54888,54890,54988,54990,54998,55000,55002,55012,55014,55022,55044,55046,55094,55096,55226,55228,55248,55250,55252,55254,55262,55264,55268,55270,55274,55276,55286,55288,55290,55292,55310,55312],[10,53323,5],{"id":12},[14,53325,16,53326,21,53328,25,53330,29],{},[18,53327,20],{},[18,53329,24],{},[18,53331,28],{},[31,53333,34],{"id":33},[36,53335,53336],{"className":38,"code":39,"language":40,"meta":41,"style":41},[18,53337,53338,53346,53350,53362,53376,53388,53402,53408],{"__ignoreMap":41},[45,53339,53340,53342,53344],{"class":47,"line":48},[45,53341,52],{"class":51},[45,53343,56],{"class":55},[45,53345,60],{"class":59},[45,53347,53348],{"class":47,"line":63},[45,53349,67],{"emptyLinePlaceholder":66},[45,53351,53352,53354,53356,53358,53360],{"class":47,"line":70},[45,53353,20],{"class":73},[45,53355,76],{"class":51},[45,53357,79],{"class":55},[45,53359,82],{"class":59},[45,53361,86],{"class":85},[45,53363,53364,53366,53368,53370,53372,53374],{"class":47,"line":89},[45,53365,93],{"class":92},[45,53367,96],{"class":85},[45,53369,100],{"class":99},[45,53371,104],{"class":103},[45,53373,100],{"class":99},[45,53375,109],{"class":85},[45,53377,53378,53380,53382,53384,53386],{"class":47,"line":112},[45,53379,24],{"class":73},[45,53381,76],{"class":51},[45,53383,79],{"class":55},[45,53385,121],{"class":59},[45,53387,86],{"class":85},[45,53389,53390,53392,53394,53396,53398,53400],{"class":47,"line":126},[45,53391,93],{"class":92},[45,53393,96],{"class":85},[45,53395,100],{"class":99},[45,53397,135],{"class":103},[45,53399,100],{"class":99},[45,53401,109],{"class":85},[45,53403,53404,53406],{"class":47,"line":142},[45,53405,28],{"class":73},[45,53407,86],{"class":85},[45,53409,53410,53412,53414,53416,53418,53420],{"class":47,"line":149},[45,53411,93],{"class":92},[45,53413,96],{"class":85},[45,53415,100],{"class":99},[45,53417,158],{"class":103},[45,53419,100],{"class":99},[45,53421,109],{"class":85},[14,53423,165,53424,168,53426,171,53428,174],{},[18,53425,20],{},[18,53427,24],{},[18,53429,28],{},[14,53431,177],{},[36,53433,53434],{"className":38,"code":180,"language":40,"meta":41,"style":41},[18,53435,53436],{"__ignoreMap":41},[45,53437,53438],{"class":47,"line":48},[45,53439,180],{"class":51},[31,53441,190],{"id":189},[14,53443,193],{},[195,53445,53446,53450,53454,53458],{},[198,53447,53448,202],{},[18,53449,20],{},[198,53451,53452,207],{},[18,53453,24],{},[198,53455,53456,212],{},[18,53457,28],{},[198,53459,215,53460,218,53462,218,53464,223],{},[18,53461,20],{},[18,53463,24],{},[18,53465,28],{},[14,53467,226,53468,230,53470,234,53472,240],{},[18,53469,229],{},[18,53471,233],{},[236,53473,239],{"href":238},[14,53475,243],{},[36,53477,53478],{"className":38,"code":246,"language":40,"meta":41,"style":41},[18,53479,53480,53488,53492,53504,53518,53530,53544,53550],{"__ignoreMap":41},[45,53481,53482,53484,53486],{"class":47,"line":48},[45,53483,253],{"class":51},[45,53485,56],{"class":55},[45,53487,258],{"class":59},[45,53489,53490],{"class":47,"line":63},[45,53491,67],{"emptyLinePlaceholder":66},[45,53493,53494,53496,53498,53500,53502],{"class":47,"line":70},[45,53495,20],{"class":73},[45,53497,269],{"class":51},[45,53499,272],{"class":55},[45,53501,275],{"class":59},[45,53503,86],{"class":85},[45,53505,53506,53508,53510,53512,53514,53516],{"class":47,"line":89},[45,53507,93],{"class":92},[45,53509,96],{"class":85},[45,53511,100],{"class":99},[45,53513,288],{"class":103},[45,53515,100],{"class":99},[45,53517,109],{"class":85},[45,53519,53520,53522,53524,53526,53528],{"class":47,"line":112},[45,53521,24],{"class":73},[45,53523,269],{"class":51},[45,53525,272],{"class":55},[45,53527,303],{"class":59},[45,53529,86],{"class":85},[45,53531,53532,53534,53536,53538,53540,53542],{"class":47,"line":126},[45,53533,93],{"class":92},[45,53535,96],{"class":85},[45,53537,100],{"class":99},[45,53539,316],{"class":103},[45,53541,100],{"class":99},[45,53543,109],{"class":85},[45,53545,53546,53548],{"class":47,"line":142},[45,53547,28],{"class":73},[45,53549,86],{"class":85},[45,53551,53552,53554,53556,53558,53560,53562],{"class":47,"line":149},[45,53553,93],{"class":92},[45,53555,96],{"class":85},[45,53557,100],{"class":99},[45,53559,337],{"class":103},[45,53561,100],{"class":99},[45,53563,109],{"class":85},[14,53565,344],{},[346,53567,53568,53572,53576],{},[198,53569,350,53570,354],{},[18,53571,353],{},[198,53573,357,53574,354],{},[18,53575,360],{},[198,53577,363,53578],{},[18,53579,28],{},[14,53581,177],{},[36,53583,53584],{"className":38,"code":370,"language":40,"meta":41,"style":41},[18,53585,53586],{"__ignoreMap":41},[45,53587,53588],{"class":47,"line":48},[45,53589,370],{"class":51},[31,53591,380],{"id":379},[14,53593,165,53594,385,53596,240],{},[18,53595,20],{},[18,53597,229],{},[14,53599,390,53600,393],{},[18,53601,28],{},[14,53603,396],{},[36,53605,53606],{"className":38,"code":399,"language":40,"meta":41,"style":41},[18,53607,53608,53616,53630,53636],{"__ignoreMap":41},[45,53609,53610,53612,53614],{"class":47,"line":48},[45,53611,20],{"class":73},[45,53613,408],{"class":51},[45,53615,86],{"class":85},[45,53617,53618,53620,53622,53624,53626,53628],{"class":47,"line":63},[45,53619,93],{"class":92},[45,53621,96],{"class":85},[45,53623,100],{"class":99},[45,53625,421],{"class":103},[45,53627,100],{"class":99},[45,53629,109],{"class":85},[45,53631,53632,53634],{"class":47,"line":70},[45,53633,28],{"class":73},[45,53635,86],{"class":85},[45,53637,53638,53640,53642,53644,53646,53648],{"class":47,"line":89},[45,53639,93],{"class":92},[45,53641,96],{"class":85},[45,53643,100],{"class":99},[45,53645,442],{"class":103},[45,53647,100],{"class":99},[45,53649,109],{"class":85},[14,53651,449],{},[195,53653,53654,53658,53660],{},[198,53655,454,53656],{},[18,53657,457],{},[198,53659,460],{},[198,53661,463],{},[14,53663,466],{},[36,53665,53666],{"className":38,"code":469,"language":40,"meta":41,"style":41},[18,53667,53668,53676,53680,53688,53702,53708],{"__ignoreMap":41},[45,53669,53670,53672,53674],{"class":47,"line":48},[45,53671,476],{"class":51},[45,53673,56],{"class":55},[45,53675,482],{"class":481},[45,53677,53678],{"class":47,"line":63},[45,53679,67],{"emptyLinePlaceholder":66},[45,53681,53682,53684,53686],{"class":47,"line":70},[45,53683,20],{"class":73},[45,53685,493],{"class":51},[45,53687,86],{"class":85},[45,53689,53690,53692,53694,53696,53698,53700],{"class":47,"line":89},[45,53691,93],{"class":92},[45,53693,96],{"class":85},[45,53695,100],{"class":99},[45,53697,506],{"class":103},[45,53699,100],{"class":99},[45,53701,109],{"class":85},[45,53703,53704,53706],{"class":47,"line":112},[45,53705,28],{"class":73},[45,53707,86],{"class":85},[45,53709,53710,53712,53714,53716,53718,53720],{"class":47,"line":126},[45,53711,93],{"class":92},[45,53713,96],{"class":85},[45,53715,100],{"class":99},[45,53717,527],{"class":103},[45,53719,100],{"class":99},[45,53721,109],{"class":85},[14,53723,177],{},[36,53725,53726],{"className":38,"code":536,"language":40,"meta":41,"style":41},[18,53727,53728],{"__ignoreMap":41},[45,53729,53730],{"class":47,"line":48},[45,53731,536],{"class":51},[14,53733,545,53734,550,53736,240],{},[236,53735,549],{"href":548},[236,53737,554],{"href":553},[31,53739,558],{"id":557},[14,53741,165,53742,563],{},[18,53743,24],{},[14,53745,566,53746,569],{},[18,53747,229],{},[14,53749,466],{},[36,53751,53752],{"className":38,"code":574,"language":40,"meta":41,"style":41},[18,53753,53754,53762,53766,53778,53792,53804,53818,53830,53844,53850],{"__ignoreMap":41},[45,53755,53756,53758,53760],{"class":47,"line":48},[45,53757,581],{"class":51},[45,53759,56],{"class":55},[45,53761,586],{"class":59},[45,53763,53764],{"class":47,"line":63},[45,53765,67],{"emptyLinePlaceholder":66},[45,53767,53768,53770,53772,53774,53776],{"class":47,"line":70},[45,53769,20],{"class":73},[45,53771,597],{"class":51},[45,53773,600],{"class":55},[45,53775,603],{"class":59},[45,53777,86],{"class":85},[45,53779,53780,53782,53784,53786,53788,53790],{"class":47,"line":89},[45,53781,93],{"class":92},[45,53783,96],{"class":85},[45,53785,100],{"class":99},[45,53787,616],{"class":103},[45,53789,100],{"class":99},[45,53791,109],{"class":85},[45,53793,53794,53796,53798,53800,53802],{"class":47,"line":112},[45,53795,24],{"class":73},[45,53797,597],{"class":51},[45,53799,600],{"class":55},[45,53801,631],{"class":59},[45,53803,86],{"class":85},[45,53805,53806,53808,53810,53812,53814,53816],{"class":47,"line":126},[45,53807,93],{"class":92},[45,53809,96],{"class":85},[45,53811,100],{"class":99},[45,53813,644],{"class":103},[45,53815,100],{"class":99},[45,53817,109],{"class":85},[45,53819,53820,53822,53824,53826,53828],{"class":47,"line":142},[45,53821,24],{"class":73},[45,53823,597],{"class":51},[45,53825,600],{"class":55},[45,53827,659],{"class":59},[45,53829,86],{"class":85},[45,53831,53832,53834,53836,53838,53840,53842],{"class":47,"line":149},[45,53833,93],{"class":92},[45,53835,96],{"class":85},[45,53837,100],{"class":99},[45,53839,672],{"class":103},[45,53841,100],{"class":99},[45,53843,109],{"class":85},[45,53845,53846,53848],{"class":47,"line":679},[45,53847,28],{"class":73},[45,53849,86],{"class":85},[45,53851,53852,53854,53856,53858,53860,53862],{"class":47,"line":686},[45,53853,93],{"class":92},[45,53855,96],{"class":85},[45,53857,100],{"class":99},[45,53859,695],{"class":103},[45,53861,100],{"class":99},[45,53863,109],{"class":85},[14,53865,177],{},[36,53867,53868],{"className":38,"code":704,"language":40,"meta":41,"style":41},[18,53869,53870],{"__ignoreMap":41},[45,53871,53872],{"class":47,"line":48},[45,53873,704],{"class":51},[712,53875,715],{"id":714},[14,53877,718],{},[36,53879,53880],{"className":38,"code":721,"language":40,"meta":41,"style":41},[18,53881,53882,53890,53894,53906,53920,53932,53946,53952],{"__ignoreMap":41},[45,53883,53884,53886,53888],{"class":47,"line":48},[45,53885,728],{"class":51},[45,53887,56],{"class":55},[45,53889,733],{"class":59},[45,53891,53892],{"class":47,"line":63},[45,53893,67],{"emptyLinePlaceholder":66},[45,53895,53896,53898,53900,53902,53904],{"class":47,"line":70},[45,53897,20],{"class":73},[45,53899,744],{"class":51},[45,53901,272],{"class":55},[45,53903,749],{"class":59},[45,53905,86],{"class":85},[45,53907,53908,53910,53912,53914,53916,53918],{"class":47,"line":89},[45,53909,93],{"class":92},[45,53911,96],{"class":85},[45,53913,100],{"class":99},[45,53915,762],{"class":103},[45,53917,100],{"class":99},[45,53919,109],{"class":85},[45,53921,53922,53924,53926,53928,53930],{"class":47,"line":112},[45,53923,24],{"class":73},[45,53925,744],{"class":51},[45,53927,272],{"class":55},[45,53929,777],{"class":59},[45,53931,86],{"class":85},[45,53933,53934,53936,53938,53940,53942,53944],{"class":47,"line":126},[45,53935,93],{"class":92},[45,53937,96],{"class":85},[45,53939,100],{"class":99},[45,53941,790],{"class":103},[45,53943,100],{"class":99},[45,53945,109],{"class":85},[45,53947,53948,53950],{"class":47,"line":142},[45,53949,28],{"class":73},[45,53951,86],{"class":85},[45,53953,53954,53956,53958,53960,53962,53964],{"class":47,"line":149},[45,53955,93],{"class":92},[45,53957,96],{"class":85},[45,53959,100],{"class":99},[45,53961,811],{"class":103},[45,53963,100],{"class":99},[45,53965,109],{"class":85},[14,53967,177],{},[36,53969,53970],{"className":38,"code":820,"language":40,"meta":41,"style":41},[18,53971,53972],{"__ignoreMap":41},[45,53973,53974],{"class":47,"line":48},[45,53975,820],{"class":51},[14,53977,829,53978,833],{},[18,53979,832],{},[14,53981,836],{},[36,53983,53984],{"className":38,"code":839,"language":40,"meta":41,"style":41},[18,53985,53986,53994,53998,54010,54024,54036,54050,54056],{"__ignoreMap":41},[45,53987,53988,53990,53992],{"class":47,"line":48},[45,53989,728],{"class":51},[45,53991,56],{"class":55},[45,53993,733],{"class":59},[45,53995,53996],{"class":47,"line":63},[45,53997,67],{"emptyLinePlaceholder":66},[45,53999,54000,54002,54004,54006,54008],{"class":47,"line":70},[45,54001,20],{"class":73},[45,54003,744],{"class":51},[45,54005,272],{"class":55},[45,54007,777],{"class":59},[45,54009,86],{"class":85},[45,54011,54012,54014,54016,54018,54020,54022],{"class":47,"line":89},[45,54013,93],{"class":92},[45,54015,96],{"class":85},[45,54017,100],{"class":99},[45,54019,790],{"class":103},[45,54021,100],{"class":99},[45,54023,109],{"class":85},[45,54025,54026,54028,54030,54032,54034],{"class":47,"line":112},[45,54027,24],{"class":73},[45,54029,744],{"class":51},[45,54031,272],{"class":55},[45,54033,749],{"class":59},[45,54035,86],{"class":85},[45,54037,54038,54040,54042,54044,54046,54048],{"class":47,"line":126},[45,54039,93],{"class":92},[45,54041,96],{"class":85},[45,54043,100],{"class":99},[45,54045,762],{"class":103},[45,54047,100],{"class":99},[45,54049,109],{"class":85},[45,54051,54052,54054],{"class":47,"line":142},[45,54053,28],{"class":73},[45,54055,86],{"class":85},[45,54057,54058,54060,54062,54064,54066,54068],{"class":47,"line":149},[45,54059,93],{"class":92},[45,54061,96],{"class":85},[45,54063,100],{"class":99},[45,54065,811],{"class":103},[45,54067,100],{"class":99},[45,54069,109],{"class":85},[14,54071,928],{},[31,54073,932],{"id":931},[14,54075,935,54076,218,54078,218,54080,942],{},[18,54077,20],{},[18,54079,24],{},[18,54081,28],{},[195,54083,54084,54088,54094,54098],{},[198,54085,947,54086,408],{},[18,54087,20],{},[198,54089,952,54090,955,54092],{},[18,54091,233],{},[18,54093,24],{},[198,54095,960,54096],{},[18,54097,229],{},[198,54099,965,54100,21,54102,970],{},[18,54101,229],{},[18,54103,28],{},[14,54105,466],{},[36,54107,54108],{"className":38,"code":975,"language":40,"meta":41,"style":41},[18,54109,54110,54118,54122,54134,54148,54160,54174,54180],{"__ignoreMap":41},[45,54111,54112,54114,54116],{"class":47,"line":48},[45,54113,52],{"class":51},[45,54115,56],{"class":55},[45,54117,986],{"class":59},[45,54119,54120],{"class":47,"line":63},[45,54121,67],{"emptyLinePlaceholder":66},[45,54123,54124,54126,54128,54130,54132],{"class":47,"line":70},[45,54125,20],{"class":73},[45,54127,76],{"class":51},[45,54129,79],{"class":55},[45,54131,82],{"class":59},[45,54133,86],{"class":85},[45,54135,54136,54138,54140,54142,54144,54146],{"class":47,"line":89},[45,54137,93],{"class":92},[45,54139,96],{"class":85},[45,54141,100],{"class":99},[45,54143,104],{"class":103},[45,54145,100],{"class":99},[45,54147,109],{"class":85},[45,54149,54150,54152,54154,54156,54158],{"class":47,"line":112},[45,54151,24],{"class":73},[45,54153,76],{"class":51},[45,54155,79],{"class":55},[45,54157,121],{"class":59},[45,54159,86],{"class":85},[45,54161,54162,54164,54166,54168,54170,54172],{"class":47,"line":126},[45,54163,93],{"class":92},[45,54165,96],{"class":85},[45,54167,100],{"class":99},[45,54169,135],{"class":103},[45,54171,100],{"class":99},[45,54173,109],{"class":85},[45,54175,54176,54178],{"class":47,"line":142},[45,54177,28],{"class":73},[45,54179,86],{"class":85},[45,54181,54182,54184,54186,54188,54190,54192],{"class":47,"line":149},[45,54183,93],{"class":92},[45,54185,96],{"class":85},[45,54187,100],{"class":99},[45,54189,158],{"class":103},[45,54191,100],{"class":99},[45,54193,109],{"class":85},[14,54195,1065],{},[195,54197,54198,54202,54206,54210],{},[198,54199,350,54200,1073],{},[18,54201,1072],{},[198,54203,350,54204,1079],{},[18,54205,1078],{},[198,54207,1082,54208],{},[18,54209,1085],{},[198,54211,1088],{},[14,54213,177],{},[36,54215,54216],{"className":38,"code":1093,"language":40,"meta":41,"style":41},[18,54217,54218],{"__ignoreMap":41},[45,54219,54220],{"class":47,"line":48},[45,54221,1093],{"class":51},[14,54223,1102,54224,218,54226,218,54228,1109,54230,1112],{},[18,54225,20],{},[18,54227,24],{},[18,54229,28],{},[18,54231,20],{},[31,54233,1116],{"id":1115},[14,54235,1119],{},[14,54237,1122],{},[195,54239,54240,54244,54248,54252,54256,54260],{},[198,54241,54242,1130],{},[18,54243,1129],{},[198,54245,54246,1136],{},[18,54247,1135],{},[198,54249,54250,1141],{},[18,54251,272],{},[198,54253,54254,1146],{},[18,54255,79],{},[198,54257,54258,1151],{},[18,54259,600],{},[198,54261,54262,1157],{},[18,54263,1156],{},[14,54265,466],{},[36,54267,54268],{"className":38,"code":1162,"language":40,"meta":41,"style":41},[18,54269,54270,54278,54282,54294,54308,54312,54324,54338,54342,54354],{"__ignoreMap":41},[45,54271,54272,54274,54276],{"class":47,"line":48},[45,54273,1169],{"class":51},[45,54275,56],{"class":55},[45,54277,1174],{"class":59},[45,54279,54280],{"class":47,"line":63},[45,54281,67],{"emptyLinePlaceholder":66},[45,54283,54284,54286,54288,54290,54292],{"class":47,"line":70},[45,54285,20],{"class":73},[45,54287,1185],{"class":51},[45,54289,1129],{"class":55},[45,54291,1190],{"class":59},[45,54293,86],{"class":85},[45,54295,54296,54298,54300,54302,54304,54306],{"class":47,"line":89},[45,54297,93],{"class":92},[45,54299,96],{"class":85},[45,54301,100],{"class":99},[45,54303,1203],{"class":103},[45,54305,100],{"class":99},[45,54307,109],{"class":85},[45,54309,54310],{"class":47,"line":112},[45,54311,67],{"emptyLinePlaceholder":66},[45,54313,54314,54316,54318,54320,54322],{"class":47,"line":126},[45,54315,20],{"class":73},[45,54317,1185],{"class":51},[45,54319,1135],{"class":55},[45,54321,1222],{"class":59},[45,54323,86],{"class":85},[45,54325,54326,54328,54330,54332,54334,54336],{"class":47,"line":142},[45,54327,93],{"class":92},[45,54329,96],{"class":85},[45,54331,100],{"class":99},[45,54333,1235],{"class":103},[45,54335,100],{"class":99},[45,54337,109],{"class":85},[45,54339,54340],{"class":47,"line":149},[45,54341,67],{"emptyLinePlaceholder":66},[45,54343,54344,54346,54348,54350,54352],{"class":47,"line":679},[45,54345,20],{"class":73},[45,54347,1185],{"class":51},[45,54349,600],{"class":55},[45,54351,1254],{"class":59},[45,54353,86],{"class":85},[45,54355,54356,54358,54360,54362,54364,54366],{"class":47,"line":686},[45,54357,93],{"class":92},[45,54359,96],{"class":85},[45,54361,100],{"class":99},[45,54363,1267],{"class":103},[45,54365,100],{"class":99},[45,54367,109],{"class":85},[14,54369,177],{},[36,54371,54372],{"className":38,"code":1276,"language":40,"meta":41,"style":41},[18,54373,54374,54382,54392],{"__ignoreMap":41},[45,54375,54376,54378,54380],{"class":47,"line":48},[45,54377,1169],{"class":51},[45,54379,1285],{"class":55},[45,54381,1174],{"class":59},[45,54383,54384,54386,54388,54390],{"class":47,"line":63},[45,54385,1169],{"class":51},[45,54387,1285],{"class":55},[45,54389,1296],{"class":55},[45,54391,733],{"class":59},[45,54393,54394,54396,54398,54400],{"class":47,"line":70},[45,54395,1169],{"class":51},[45,54397,1285],{"class":55},[45,54399,1307],{"class":51},[45,54401,1310],{"class":59},[14,54403,1313,54404,1316,54406,240],{},[18,54405,56],{},[18,54407,1129],{},[195,54409,54410,54414],{},[198,54411,54412,1325],{},[18,54413,56],{},[198,54415,54416,1330],{},[18,54417,1129],{},[14,54419,1333],{},[31,54421,1337],{"id":1336},[14,54423,1340,54424,21,54426,25,54428,240],{},[18,54425,20],{},[18,54427,24],{},[18,54429,28],{},[712,54431,1350,54432,1353,54434],{"id":1349},[18,54433,56],{},[18,54435,1129],{},[14,54437,1358],{},[36,54439,54440],{"className":38,"code":1361,"language":40,"meta":41,"style":41},[18,54441,54442,54450,54454,54466],{"__ignoreMap":41},[45,54443,54444,54446,54448],{"class":47,"line":48},[45,54445,52],{"class":51},[45,54447,56],{"class":55},[45,54449,60],{"class":59},[45,54451,54452],{"class":47,"line":63},[45,54453,67],{"emptyLinePlaceholder":66},[45,54455,54456,54458,54460,54462,54464],{"class":47,"line":70},[45,54457,20],{"class":73},[45,54459,76],{"class":51},[45,54461,56],{"class":55},[45,54463,121],{"class":59},[45,54465,86],{"class":85},[45,54467,54468,54470,54472,54474,54476,54478],{"class":47,"line":89},[45,54469,93],{"class":92},[45,54471,96],{"class":85},[45,54473,100],{"class":99},[45,54475,158],{"class":103},[45,54477,100],{"class":99},[45,54479,109],{"class":85},[14,54481,165,54482,1406],{},[18,54483,1129],{},[36,54485,54486],{"className":38,"code":1409,"language":40,"meta":41,"style":41},[18,54487,54488,54496,54500,54512],{"__ignoreMap":41},[45,54489,54490,54492,54494],{"class":47,"line":48},[45,54491,52],{"class":51},[45,54493,56],{"class":55},[45,54495,60],{"class":59},[45,54497,54498],{"class":47,"line":63},[45,54499,67],{"emptyLinePlaceholder":66},[45,54501,54502,54504,54506,54508,54510],{"class":47,"line":70},[45,54503,20],{"class":73},[45,54505,76],{"class":51},[45,54507,1129],{"class":55},[45,54509,121],{"class":59},[45,54511,86],{"class":85},[45,54513,54514,54516,54518,54520,54522,54524],{"class":47,"line":89},[45,54515,93],{"class":92},[45,54517,96],{"class":85},[45,54519,100],{"class":99},[45,54521,158],{"class":103},[45,54523,100],{"class":99},[45,54525,109],{"class":85},[712,54527,1453],{"id":1452},[14,54529,1456,54530,21,54532,25,54534,1463],{},[18,54531,20],{},[18,54533,24],{},[18,54535,28],{},[14,54537,1466],{},[36,54539,54540],{"className":38,"code":1469,"language":40,"meta":41,"style":41},[18,54541,54542,54550,54554,54566,54580,54586],{"__ignoreMap":41},[45,54543,54544,54546,54548],{"class":47,"line":48},[45,54545,1476],{"class":51},[45,54547,56],{"class":55},[45,54549,1481],{"class":59},[45,54551,54552],{"class":47,"line":63},[45,54553,67],{"emptyLinePlaceholder":66},[45,54555,54556,54558,54560,54562,54564],{"class":47,"line":70},[45,54557,20],{"class":73},[45,54559,1492],{"class":51},[45,54561,272],{"class":55},[45,54563,749],{"class":59},[45,54565,86],{"class":85},[45,54567,54568,54570,54572,54574,54576,54578],{"class":47,"line":89},[45,54569,93],{"class":92},[45,54571,96],{"class":85},[45,54573,100],{"class":99},[45,54575,1509],{"class":103},[45,54577,100],{"class":99},[45,54579,109],{"class":85},[45,54581,54582,54584],{"class":47,"line":112},[45,54583,28],{"class":73},[45,54585,86],{"class":85},[45,54587,54588,54590,54592,54594,54596,54598],{"class":47,"line":126},[45,54589,93],{"class":92},[45,54591,96],{"class":85},[45,54593,100],{"class":99},[45,54595,1530],{"class":103},[45,54597,100],{"class":99},[45,54599,109],{"class":85},[14,54601,1537,54602,240],{},[236,54603,1541],{"href":1540},[712,54605,1545],{"id":1544},[14,54607,1548],{},[14,54609,1466],{},[36,54611,54612],{"className":38,"code":1553,"language":40,"meta":41,"style":41},[18,54613,54614,54626,54630,54646],{"__ignoreMap":41},[45,54615,54616,54618,54620,54622,54624],{"class":47,"line":48},[45,54617,1560],{"class":51},[45,54619,56],{"class":55},[45,54621,1565],{"class":99},[45,54623,1568],{"class":103},[45,54625,1571],{"class":99},[45,54627,54628],{"class":47,"line":63},[45,54629,67],{"emptyLinePlaceholder":66},[45,54631,54632,54634,54636,54638,54640,54642,54644],{"class":47,"line":70},[45,54633,20],{"class":73},[45,54635,1582],{"class":51},[45,54637,1129],{"class":55},[45,54639,1565],{"class":99},[45,54641,1568],{"class":103},[45,54643,100],{"class":99},[45,54645,86],{"class":85},[45,54647,54648,54650,54652,54654,54656,54658],{"class":47,"line":89},[45,54649,93],{"class":92},[45,54651,96],{"class":85},[45,54653,100],{"class":99},[45,54655,1603],{"class":103},[45,54657,100],{"class":99},[45,54659,109],{"class":85},[14,54661,1610,54662,240],{},[236,54663,1614],{"href":1613},[712,54665,1618],{"id":1617},[14,54667,1621],{},[36,54669,54670],{"className":38,"code":1624,"language":40,"meta":41,"style":41},[18,54671,54672,54680,54684,54696,54710,54722,54736,54742],{"__ignoreMap":41},[45,54673,54674,54676,54678],{"class":47,"line":48},[45,54675,52],{"class":51},[45,54677,56],{"class":55},[45,54679,1635],{"class":59},[45,54681,54682],{"class":47,"line":63},[45,54683,67],{"emptyLinePlaceholder":66},[45,54685,54686,54688,54690,54692,54694],{"class":47,"line":70},[45,54687,20],{"class":73},[45,54689,76],{"class":51},[45,54691,79],{"class":55},[45,54693,121],{"class":59},[45,54695,86],{"class":85},[45,54697,54698,54700,54702,54704,54706,54708],{"class":47,"line":89},[45,54699,93],{"class":92},[45,54701,96],{"class":85},[45,54703,100],{"class":99},[45,54705,1662],{"class":103},[45,54707,100],{"class":99},[45,54709,109],{"class":85},[45,54711,54712,54714,54716,54718,54720],{"class":47,"line":112},[45,54713,24],{"class":73},[45,54715,76],{"class":51},[45,54717,79],{"class":55},[45,54719,82],{"class":59},[45,54721,86],{"class":85},[45,54723,54724,54726,54728,54730,54732,54734],{"class":47,"line":126},[45,54725,93],{"class":92},[45,54727,96],{"class":85},[45,54729,100],{"class":99},[45,54731,1689],{"class":103},[45,54733,100],{"class":99},[45,54735,109],{"class":85},[45,54737,54738,54740],{"class":47,"line":142},[45,54739,28],{"class":73},[45,54741,86],{"class":85},[45,54743,54744,54746,54748,54750,54752,54754],{"class":47,"line":149},[45,54745,93],{"class":92},[45,54747,96],{"class":85},[45,54749,100],{"class":99},[45,54751,1710],{"class":103},[45,54753,100],{"class":99},[45,54755,109],{"class":85},[14,54757,177],{},[36,54759,54760],{"className":38,"code":1719,"language":40,"meta":41,"style":41},[18,54761,54762],{"__ignoreMap":41},[45,54763,54764,54766],{"class":47,"line":48},[45,54765,1726],{"class":51},[45,54767,1729],{"class":59},[14,54769,1732,54770,1736,54772,1739],{},[18,54771,1735],{},[18,54773,1078],{},[14,54775,1742],{},[36,54777,54778],{"className":38,"code":1745,"language":40,"meta":41,"style":41},[18,54779,54780,54788,54792,54804,54818,54830,54844,54850],{"__ignoreMap":41},[45,54781,54782,54784,54786],{"class":47,"line":48},[45,54783,52],{"class":51},[45,54785,56],{"class":55},[45,54787,1635],{"class":59},[45,54789,54790],{"class":47,"line":63},[45,54791,67],{"emptyLinePlaceholder":66},[45,54793,54794,54796,54798,54800,54802],{"class":47,"line":70},[45,54795,20],{"class":73},[45,54797,76],{"class":51},[45,54799,79],{"class":55},[45,54801,82],{"class":59},[45,54803,86],{"class":85},[45,54805,54806,54808,54810,54812,54814,54816],{"class":47,"line":89},[45,54807,93],{"class":92},[45,54809,96],{"class":85},[45,54811,100],{"class":99},[45,54813,1689],{"class":103},[45,54815,100],{"class":99},[45,54817,109],{"class":85},[45,54819,54820,54822,54824,54826,54828],{"class":47,"line":112},[45,54821,24],{"class":73},[45,54823,76],{"class":51},[45,54825,79],{"class":55},[45,54827,121],{"class":59},[45,54829,86],{"class":85},[45,54831,54832,54834,54836,54838,54840,54842],{"class":47,"line":126},[45,54833,93],{"class":92},[45,54835,96],{"class":85},[45,54837,100],{"class":99},[45,54839,1662],{"class":103},[45,54841,100],{"class":99},[45,54843,109],{"class":85},[45,54845,54846,54848],{"class":47,"line":142},[45,54847,28],{"class":73},[45,54849,86],{"class":85},[45,54851,54852,54854,54856,54858,54860,54862],{"class":47,"line":149},[45,54853,93],{"class":92},[45,54855,96],{"class":85},[45,54857,100],{"class":99},[45,54859,1710],{"class":103},[45,54861,100],{"class":99},[45,54863,109],{"class":85},[712,54865,1835],{"id":1834},[14,54867,1838,54868,218,54870,218,54872,1845],{},[18,54869,20],{},[18,54871,24],{},[18,54873,28],{},[14,54875,1848,54876,1851],{},[18,54877,20],{},[31,54879,1855],{"id":1854},[14,54881,1858,54882,1861,54884,1864],{},[18,54883,20],{},[18,54885,20],{},[14,54887,1867],{},[14,54889,466],{},[36,54891,54892],{"className":38,"code":1872,"language":40,"meta":41,"style":41},[18,54893,54894,54902,54910,54914,54926,54934,54948,54954,54968,54974],{"__ignoreMap":41},[45,54895,54896,54898,54900],{"class":47,"line":48},[45,54897,52],{"class":51},[45,54899,56],{"class":55},[45,54901,1883],{"class":59},[45,54903,54904,54906,54908],{"class":47,"line":63},[45,54905,1888],{"class":51},[45,54907,56],{"class":55},[45,54909,482],{"class":481},[45,54911,54912],{"class":47,"line":70},[45,54913,67],{"emptyLinePlaceholder":66},[45,54915,54916,54918,54920,54922,54924],{"class":47,"line":89},[45,54917,20],{"class":73},[45,54919,76],{"class":51},[45,54921,600],{"class":55},[45,54923,121],{"class":59},[45,54925,86],{"class":85},[45,54927,54928,54930,54932],{"class":47,"line":112},[45,54929,1913],{"class":73},[45,54931,1916],{"class":51},[45,54933,86],{"class":85},[45,54935,54936,54938,54940,54942,54944,54946],{"class":47,"line":126},[45,54937,1923],{"class":92},[45,54939,96],{"class":85},[45,54941,100],{"class":99},[45,54943,1930],{"class":103},[45,54945,100],{"class":99},[45,54947,109],{"class":85},[45,54949,54950,54952],{"class":47,"line":142},[45,54951,1939],{"class":73},[45,54953,86],{"class":85},[45,54955,54956,54958,54960,54962,54964,54966],{"class":47,"line":149},[45,54957,1923],{"class":92},[45,54959,96],{"class":85},[45,54961,100],{"class":99},[45,54963,1952],{"class":103},[45,54965,100],{"class":99},[45,54967,109],{"class":85},[45,54969,54970,54972],{"class":47,"line":679},[45,54971,28],{"class":73},[45,54973,86],{"class":85},[45,54975,54976,54978,54980,54982,54984,54986],{"class":47,"line":686},[45,54977,93],{"class":92},[45,54979,96],{"class":85},[45,54981,100],{"class":99},[45,54983,1973],{"class":103},[45,54985,100],{"class":99},[45,54987,109],{"class":85},[14,54989,177],{},[36,54991,54992],{"className":38,"code":1982,"language":40,"meta":41,"style":41},[18,54993,54994],{"__ignoreMap":41},[45,54995,54996],{"class":47,"line":48},[45,54997,1982],{"class":51},[14,54999,1991],{},[14,55001,1994],{},[195,55003,55004,55006,55008,55010],{},[198,55005,1999],{},[198,55007,2002],{},[198,55009,2005],{},[198,55011,2008],{},[31,55013,2012],{"id":2011},[14,55015,2015,55016,218,55018,218,55020,2022],{},[18,55017,20],{},[18,55019,24],{},[18,55021,28],{},[195,55023,55024,55034,55036,55040,55042],{},[198,55025,2027,55026,218,55028,218,55030,2034,55032,2037],{},[18,55027,20],{},[18,55029,24],{},[18,55031,28],{},[18,55033,20],{},[198,55035,2040],{},[198,55037,2043,55038,2046],{},[18,55039,28],{},[198,55041,2049],{},[198,55043,2052],{},[14,55045,2055],{},[36,55047,55048],{"className":38,"code":2058,"language":40,"meta":41,"style":41},[18,55049,55050,55060,55070,55084],{"__ignoreMap":41},[45,55051,55052,55054,55056,55058],{"class":47,"line":48},[45,55053,2065],{"class":92},[45,55055,96],{"class":85},[45,55057,2071],{"class":2070},[45,55059,109],{"class":85},[45,55061,55062,55064,55066,55068],{"class":47,"line":63},[45,55063,2065],{"class":92},[45,55065,96],{"class":85},[45,55067,2082],{"class":2070},[45,55069,109],{"class":85},[45,55071,55072,55074,55076,55078,55080,55082],{"class":47,"line":70},[45,55073,2065],{"class":92},[45,55075,96],{"class":85},[45,55077,100],{"class":99},[45,55079,2095],{"class":103},[45,55081,100],{"class":99},[45,55083,109],{"class":85},[45,55085,55086,55088,55090,55092],{"class":47,"line":89},[45,55087,2104],{"class":92},[45,55089,96],{"class":85},[45,55091,2110],{"class":2109},[45,55093,109],{"class":85},[14,55095,2115],{},[36,55097,55098],{"className":38,"code":2118,"language":40,"meta":41,"style":41},[18,55099,55100,55108,55112,55122,55136,55150,55154,55166,55180,55192,55206,55212],{"__ignoreMap":41},[45,55101,55102,55104,55106],{"class":47,"line":48},[45,55103,52],{"class":51},[45,55105,56],{"class":55},[45,55107,986],{"class":59},[45,55109,55110],{"class":47,"line":63},[45,55111,67],{"emptyLinePlaceholder":66},[45,55113,55114,55116,55118,55120],{"class":47,"line":70},[45,55115,2065],{"class":92},[45,55117,96],{"class":85},[45,55119,2082],{"class":2070},[45,55121,109],{"class":85},[45,55123,55124,55126,55128,55130,55132,55134],{"class":47,"line":89},[45,55125,2065],{"class":92},[45,55127,96],{"class":85},[45,55129,52],{"class":2070},[45,55131,79],{"class":55},[45,55133,82],{"class":59},[45,55135,109],{"class":85},[45,55137,55138,55140,55142,55144,55146,55148],{"class":47,"line":112},[45,55139,2065],{"class":92},[45,55141,96],{"class":85},[45,55143,52],{"class":2070},[45,55145,79],{"class":55},[45,55147,121],{"class":59},[45,55149,109],{"class":85},[45,55151,55152],{"class":47,"line":126},[45,55153,67],{"emptyLinePlaceholder":66},[45,55155,55156,55158,55160,55162,55164],{"class":47,"line":142},[45,55157,20],{"class":73},[45,55159,76],{"class":51},[45,55161,79],{"class":55},[45,55163,82],{"class":59},[45,55165,86],{"class":85},[45,55167,55168,55170,55172,55174,55176,55178],{"class":47,"line":149},[45,55169,93],{"class":92},[45,55171,96],{"class":85},[45,55173,100],{"class":99},[45,55175,104],{"class":103},[45,55177,100],{"class":99},[45,55179,109],{"class":85},[45,55181,55182,55184,55186,55188,55190],{"class":47,"line":679},[45,55183,24],{"class":73},[45,55185,76],{"class":51},[45,55187,79],{"class":55},[45,55189,121],{"class":59},[45,55191,86],{"class":85},[45,55193,55194,55196,55198,55200,55202,55204],{"class":47,"line":686},[45,55195,93],{"class":92},[45,55197,96],{"class":85},[45,55199,100],{"class":99},[45,55201,135],{"class":103},[45,55203,100],{"class":99},[45,55205,109],{"class":85},[45,55207,55208,55210],{"class":47,"line":2229},[45,55209,28],{"class":73},[45,55211,86],{"class":85},[45,55213,55214,55216,55218,55220,55222,55224],{"class":47,"line":2236},[45,55215,93],{"class":92},[45,55217,96],{"class":85},[45,55219,100],{"class":99},[45,55221,158],{"class":103},[45,55223,100],{"class":99},[45,55225,109],{"class":85},[14,55227,177],{},[36,55229,55230],{"className":38,"code":2253,"language":40,"meta":41,"style":41},[18,55231,55232,55236,55240,55244],{"__ignoreMap":41},[45,55233,55234],{"class":47,"line":48},[45,55235,2260],{"class":59},[45,55237,55238],{"class":47,"line":63},[45,55239,2265],{"class":481},[45,55241,55242],{"class":47,"line":70},[45,55243,2270],{"class":481},[45,55245,55246],{"class":47,"line":89},[45,55247,1093],{"class":51},[14,55249,2277],{},[31,55251,2281],{"id":2280},[712,55253,2285],{"id":2284},[14,55255,55256,2290,55258,2293,55260,240],{},[18,55257,20],{},[18,55259,24],{},[18,55261,233],{},[712,55263,2299],{"id":2298},[14,55265,2302,55266,2305],{},[18,55267,28],{},[712,55269,2309],{"id":2308},[14,55271,2312,55272,2315],{},[18,55273,24],{},[712,55275,2319],{"id":2318},[14,55277,2322,55278,218,55280,218,55282,2329,55284,240],{},[18,55279,20],{},[18,55281,24],{},[18,55283,28],{},[18,55285,229],{},[712,55287,2335],{"id":2334},[14,55289,2338],{},[31,55291,2342],{"id":2341},[195,55293,55294,55298,55302,55306],{},[198,55295,55296],{},[236,55297,549],{"href":548},[198,55299,55300],{},[236,55301,239],{"href":238},[198,55303,55304],{},[236,55305,554],{"href":553},[198,55307,55308],{},[236,55309,2362],{"href":2361},[14,55311,2365],{},[2367,55313,2369],{},{"title":41,"searchDepth":63,"depth":63,"links":55315},[55316,55317,55318,55319,55322,55323,55324,55331,55332,55333,55340],{"id":33,"depth":63,"text":34},{"id":189,"depth":63,"text":190},{"id":379,"depth":63,"text":380},{"id":557,"depth":63,"text":558,"children":55320},[55321],{"id":714,"depth":70,"text":715},{"id":931,"depth":63,"text":932},{"id":1115,"depth":63,"text":1116},{"id":1336,"depth":63,"text":1337,"children":55325},[55326,55327,55328,55329,55330],{"id":1349,"depth":70,"text":2383},{"id":1452,"depth":70,"text":1453},{"id":1544,"depth":70,"text":1545},{"id":1617,"depth":70,"text":1618},{"id":1834,"depth":70,"text":1835},{"id":1854,"depth":63,"text":1855},{"id":2011,"depth":63,"text":2012},{"id":2280,"depth":63,"text":2281,"children":55334},[55335,55336,55337,55338,55339],{"id":2284,"depth":70,"text":2285},{"id":2298,"depth":70,"text":2299},{"id":2308,"depth":70,"text":2309},{"id":2318,"depth":70,"text":2319},{"id":2334,"depth":70,"text":2335},{"id":2341,"depth":63,"text":2342},{},{"title":5,"description":2398},{"id":55344,"title":55345,"body":55346,"description":57287,"extension":2399,"meta":57288,"navigation":66,"path":57289,"seo":57290,"stem":57291,"__hash__":57292},"content\u002Flearn\u002Fpython-if-statements-explained.md","Python if Statements Explained",{"type":7,"value":55347,"toc":57257},[55348,55351,55356,55365,55368,55370,55414,55420,55423,55439,55443,55448,55451,55467,55470,55475,55477,55521,55523,55537,55545,55549,55555,55573,55575,55620,55623,55640,55643,55657,55662,55666,55673,55678,55680,55712,55714,55745,55747,55755,55767,55810,55813,55822,55825,55879,55889,55893,55896,55900,55945,55949,56004,56008,56053,56057,56101,56104,56115,56119,56126,56132,56134,56174,56180,56183,56200,56203,56216,56218,56260,56268,56272,56277,56280,56282,56345,56347,56356,56363,56368,56374,56378,56381,56387,56389,56432,56434,56477,56483,56486,56499,56503,56505,56510,56512,56514,56555,56560,56562,56604,56610,56616,56618,56620,56671,56673,56724,56727,56737,56741,56743,56785,56787,56829,56835,56839,56846,56849,56852,56929,56931,56992,56995,57006,57010,57013,57015,57063,57070,57073,57104,57106,57109,57114,57118,57123,57127,57134,57138,57144,57146,57204,57208,57217,57219,57241,57254],[10,55349,55345],{"id":55350},"python-if-statements-explained",[14,55352,19451,55353,55355],{},[18,55354,20],{}," statement lets Python make a decision.",[14,55357,55358,55359,55361,55362,55364],{},"It checks a condition. If that condition is ",[18,55360,229],{},", Python runs the indented code underneath it. If the condition is ",[18,55363,233],{},", Python skips that code.",[14,55366,55367],{},"This is one of the most important tools in Python because it controls the flow of your program.",[31,55369,34],{"id":33},[36,55371,55373],{"className":38,"code":55372,"language":40,"meta":41,"style":41},"age = 18\n\nif age >= 18:\n    print(\"You are an adult\")\n",[18,55374,55375,55383,55387,55399],{"__ignoreMap":41},[45,55376,55377,55379,55381],{"class":47,"line":48},[45,55378,52],{"class":51},[45,55380,56],{"class":55},[45,55382,60],{"class":59},[45,55384,55385],{"class":47,"line":63},[45,55386,67],{"emptyLinePlaceholder":66},[45,55388,55389,55391,55393,55395,55397],{"class":47,"line":70},[45,55390,20],{"class":73},[45,55392,76],{"class":51},[45,55394,600],{"class":55},[45,55396,121],{"class":59},[45,55398,86],{"class":85},[45,55400,55401,55403,55405,55407,55410,55412],{"class":47,"line":89},[45,55402,93],{"class":92},[45,55404,96],{"class":85},[45,55406,100],{"class":99},[45,55408,55409],{"class":103},"You are an adult",[45,55411,100],{"class":99},[45,55413,109],{"class":85},[14,55415,165,55416,385,55418,240],{},[18,55417,20],{},[18,55419,229],{},[14,55421,55422],{},"Two important rules:",[195,55424,55425,55433],{},[198,55426,55427,55428,55430,55431],{},"End the ",[18,55429,20],{}," line with a colon ",[18,55432,457],{},[198,55434,55435,55436,55438],{},"Indent the code inside the ",[18,55437,20],{}," block",[31,55440,55442],{"id":55441},"what-an-if-statement-does","What an if statement does",[14,55444,19451,55445,55447],{},[18,55446,20],{}," statement helps your program choose whether to do something.",[14,55449,55450],{},"It works like this:",[195,55452,55453,55456,55462],{},[198,55454,55455],{},"Python checks a condition",[198,55457,55458,55459,55461],{},"If the condition is ",[18,55460,229],{},", the block runs",[198,55463,55458,55464,55466],{},[18,55465,233],{},", the block is skipped",[14,55468,55469],{},"You can think of it as:",[17619,55471,55472],{},[14,55473,55474],{},"“If this is true, do this.”",[14,55476,466],{},[36,55478,55480],{"className":38,"code":55479,"language":40,"meta":41,"style":41},"temperature = 30\n\nif temperature > 25:\n    print(\"It is a warm day\")\n",[18,55481,55482,55490,55494,55506],{"__ignoreMap":41},[45,55483,55484,55486,55488],{"class":47,"line":48},[45,55485,253],{"class":51},[45,55487,56],{"class":55},[45,55489,258],{"class":59},[45,55491,55492],{"class":47,"line":63},[45,55493,67],{"emptyLinePlaceholder":66},[45,55495,55496,55498,55500,55502,55504],{"class":47,"line":70},[45,55497,20],{"class":73},[45,55499,269],{"class":51},[45,55501,272],{"class":55},[45,55503,303],{"class":59},[45,55505,86],{"class":85},[45,55507,55508,55510,55512,55514,55517,55519],{"class":47,"line":89},[45,55509,93],{"class":92},[45,55511,96],{"class":85},[45,55513,100],{"class":99},[45,55515,55516],{"class":103},"It is a warm day",[45,55518,100],{"class":99},[45,55520,109],{"class":85},[14,55522,177],{},[36,55524,55526],{"className":38,"code":55525,"language":40,"meta":41,"style":41},"It is a warm day\n",[18,55527,55528],{"__ignoreMap":41},[45,55529,55530,55532,55534],{"class":47,"line":48},[45,55531,35095],{"class":51},[45,55533,1285],{"class":55},[45,55535,55536],{"class":51}," a warm day\n",[14,55538,30405,55539,55541,55542,55544],{},[18,55540,360],{}," is true, Python runs the ",[18,55543,15833],{}," line.",[31,55546,55548],{"id":55547},"basic-if-statement-syntax","Basic if statement syntax",[14,55550,55551,55552,55554],{},"A basic ",[18,55553,20],{}," statement has four parts:",[195,55556,55557,55562,55565,55570],{},[198,55558,55559,55560],{},"The keyword ",[18,55561,20],{},[198,55563,55564],{},"A condition",[198,55566,55567,55568],{},"A colon ",[18,55569,457],{},[198,55571,55572],{},"An indented block of code",[14,55574,466],{},[36,55576,55578],{"className":38,"code":55577,"language":40,"meta":41,"style":41},"score = 80\n\nif score >= 50:\n    print(\"You passed\")\n",[18,55579,55580,55589,55593,55605],{"__ignoreMap":41},[45,55581,55582,55584,55586],{"class":47,"line":48},[45,55583,581],{"class":51},[45,55585,56],{"class":55},[45,55587,55588],{"class":59}," 80\n",[45,55590,55591],{"class":47,"line":63},[45,55592,67],{"emptyLinePlaceholder":66},[45,55594,55595,55597,55599,55601,55603],{"class":47,"line":70},[45,55596,20],{"class":73},[45,55598,597],{"class":51},[45,55600,600],{"class":55},[45,55602,20121],{"class":59},[45,55604,86],{"class":85},[45,55606,55607,55609,55611,55613,55616,55618],{"class":47,"line":89},[45,55608,93],{"class":92},[45,55610,96],{"class":85},[45,55612,100],{"class":99},[45,55614,55615],{"class":103},"You passed",[45,55617,100],{"class":99},[45,55619,109],{"class":85},[14,55621,55622],{},"The key line is:",[36,55624,55626],{"className":38,"code":55625,"language":40,"meta":41,"style":41},"if score >= 50:\n",[18,55627,55628],{"__ignoreMap":41},[45,55629,55630,55632,55634,55636,55638],{"class":47,"line":48},[45,55631,20],{"class":73},[45,55633,597],{"class":51},[45,55635,600],{"class":55},[45,55637,20121],{"class":59},[45,55639,86],{"class":85},[14,55641,55642],{},"This means:",[195,55644,55645,55654],{},[198,55646,55647,55648,55650,55651],{},"check whether ",[18,55649,46543],{}," is greater than or equal to ",[18,55652,55653],{},"50",[198,55655,55656],{},"if it is, run the indented line below it",[14,55658,55659,55660,240],{},"If you are still getting used to variables, see ",[236,55661,40783],{"href":40782},[31,55663,55665],{"id":55664},"how-conditions-work","How conditions work",[14,55667,55668,55669,230,55671,240],{},"A condition is an expression that becomes either ",[18,55670,229],{},[18,55672,233],{},[14,55674,55675,55676,1864],{},"Python uses that result to decide whether to run the ",[18,55677,20],{},[14,55679,1122],{},[195,55681,55682,55687,55692,55697,55702,55707],{},[198,55683,55684,55686],{},[18,55685,1129],{}," equal to",[198,55688,55689,55691],{},[18,55690,1135],{}," not equal to",[198,55693,55694,55696],{},[18,55695,272],{}," greater than",[198,55698,55699,55701],{},[18,55700,79],{}," less than",[198,55703,55704,55706],{},[18,55705,600],{}," greater than or equal to",[198,55708,55709,55711],{},[18,55710,1156],{}," less than or equal to",[14,55713,466],{},[36,55715,55717],{"className":38,"code":55716,"language":40,"meta":41,"style":41},"age = 16\n\nprint(age >= 18)\n",[18,55718,55719,55727,55731],{"__ignoreMap":41},[45,55720,55721,55723,55725],{"class":47,"line":48},[45,55722,52],{"class":51},[45,55724,56],{"class":55},[45,55726,986],{"class":59},[45,55728,55729],{"class":47,"line":63},[45,55730,67],{"emptyLinePlaceholder":66},[45,55732,55733,55735,55737,55739,55741,55743],{"class":47,"line":70},[45,55734,2065],{"class":92},[45,55736,96],{"class":85},[45,55738,52],{"class":2070},[45,55740,600],{"class":55},[45,55742,121],{"class":59},[45,55744,109],{"class":85},[14,55746,177],{},[36,55748,55749],{"className":38,"code":2265,"language":40,"meta":41,"style":41},[18,55750,55751],{"__ignoreMap":41},[45,55752,55753],{"class":47,"line":48},[45,55754,2265],{"class":481},[14,55756,55757,55758,36998,55761,55763,55764,55766],{},"Since ",[18,55759,55760],{},"age >= 18",[18,55762,233],{},", this ",[18,55765,20],{}," block will not run:",[36,55768,55770],{"className":38,"code":55769,"language":40,"meta":41,"style":41},"age = 16\n\nif age >= 18:\n    print(\"You can vote\")\n",[18,55771,55772,55780,55784,55796],{"__ignoreMap":41},[45,55773,55774,55776,55778],{"class":47,"line":48},[45,55775,52],{"class":51},[45,55777,56],{"class":55},[45,55779,986],{"class":59},[45,55781,55782],{"class":47,"line":63},[45,55783,67],{"emptyLinePlaceholder":66},[45,55785,55786,55788,55790,55792,55794],{"class":47,"line":70},[45,55787,20],{"class":73},[45,55789,76],{"class":51},[45,55791,600],{"class":55},[45,55793,121],{"class":59},[45,55795,86],{"class":85},[45,55797,55798,55800,55802,55804,55806,55808],{"class":47,"line":89},[45,55799,93],{"class":92},[45,55801,96],{"class":85},[45,55803,100],{"class":99},[45,55805,33955],{"class":103},[45,55807,100],{"class":99},[45,55809,109],{"class":85},[14,55811,55812],{},"Conditions often compare:",[195,55814,55815,55817,55819],{},[198,55816,9069],{},[198,55818,22296],{},[198,55820,55821],{},"variables",[14,55823,55824],{},"String example:",[36,55826,55828],{"className":38,"code":55827,"language":40,"meta":41,"style":41},"username = \"admin\"\n\nif username == \"admin\":\n    print(\"Welcome, admin\")\n",[18,55829,55830,55843,55847,55864],{"__ignoreMap":41},[45,55831,55832,55834,55836,55838,55841],{"class":47,"line":48},[45,55833,41765],{"class":51},[45,55835,56],{"class":55},[45,55837,1565],{"class":99},[45,55839,55840],{"class":103},"admin",[45,55842,1571],{"class":99},[45,55844,55845],{"class":47,"line":63},[45,55846,67],{"emptyLinePlaceholder":66},[45,55848,55849,55851,55854,55856,55858,55860,55862],{"class":47,"line":70},[45,55850,20],{"class":73},[45,55852,55853],{"class":51}," username ",[45,55855,1129],{"class":55},[45,55857,1565],{"class":99},[45,55859,55840],{"class":103},[45,55861,100],{"class":99},[45,55863,86],{"class":85},[45,55865,55866,55868,55870,55872,55875,55877],{"class":47,"line":89},[45,55867,93],{"class":92},[45,55869,96],{"class":85},[45,55871,100],{"class":99},[45,55873,55874],{"class":103},"Welcome, admin",[45,55876,100],{"class":99},[45,55878,109],{"class":85},[14,55880,55881,55882,1316,55884,55886,55887,240],{},"If you want to understand ",[18,55883,229],{},[18,55885,233],{}," better, read ",[236,55888,41161],{"href":238},[31,55890,55892],{"id":55891},"simple-examples-beginners-understand","Simple examples beginners understand",[14,55894,55895],{},"Here are a few common beginner examples.",[712,55897,55899],{"id":55898},"check-if-a-number-is-positive","Check if a number is positive",[36,55901,55903],{"className":38,"code":55902,"language":40,"meta":41,"style":41},"number = 7\n\nif number > 0:\n    print(\"The number is positive\")\n",[18,55904,55905,55914,55918,55930],{"__ignoreMap":41},[45,55906,55907,55909,55911],{"class":47,"line":48},[45,55908,728],{"class":51},[45,55910,56],{"class":55},[45,55912,55913],{"class":59}," 7\n",[45,55915,55916],{"class":47,"line":63},[45,55917,67],{"emptyLinePlaceholder":66},[45,55919,55920,55922,55924,55926,55928],{"class":47,"line":70},[45,55921,20],{"class":73},[45,55923,744],{"class":51},[45,55925,272],{"class":55},[45,55927,749],{"class":59},[45,55929,86],{"class":85},[45,55931,55932,55934,55936,55938,55941,55943],{"class":47,"line":89},[45,55933,93],{"class":92},[45,55935,96],{"class":85},[45,55937,100],{"class":99},[45,55939,55940],{"class":103},"The number is positive",[45,55942,100],{"class":99},[45,55944,109],{"class":85},[712,55946,55948],{"id":55947},"check-if-a-password-matches","Check if a password matches",[36,55950,55952],{"className":38,"code":55951,"language":40,"meta":41,"style":41},"password = \"python123\"\n\nif password == \"python123\":\n    print(\"Access granted\")\n",[18,55953,55954,55968,55972,55989],{"__ignoreMap":41},[45,55955,55956,55959,55961,55963,55966],{"class":47,"line":48},[45,55957,55958],{"class":51},"password ",[45,55960,56],{"class":55},[45,55962,1565],{"class":99},[45,55964,55965],{"class":103},"python123",[45,55967,1571],{"class":99},[45,55969,55970],{"class":47,"line":63},[45,55971,67],{"emptyLinePlaceholder":66},[45,55973,55974,55976,55979,55981,55983,55985,55987],{"class":47,"line":70},[45,55975,20],{"class":73},[45,55977,55978],{"class":51}," password ",[45,55980,1129],{"class":55},[45,55982,1565],{"class":99},[45,55984,55965],{"class":103},[45,55986,100],{"class":99},[45,55988,86],{"class":85},[45,55990,55991,55993,55995,55997,56000,56002],{"class":47,"line":89},[45,55992,93],{"class":92},[45,55994,96],{"class":85},[45,55996,100],{"class":99},[45,55998,55999],{"class":103},"Access granted",[45,56001,100],{"class":99},[45,56003,109],{"class":85},[712,56005,56007],{"id":56006},"check-if-a-string-is-empty","Check if a string is empty",[36,56009,56011],{"className":38,"code":56010,"language":40,"meta":41,"style":41},"name = \"\"\n\nif name == \"\":\n    print(\"The string is empty\")\n",[18,56012,56013,56022,56026,56038],{"__ignoreMap":41},[45,56014,56015,56017,56019],{"class":47,"line":48},[45,56016,1560],{"class":51},[45,56018,56],{"class":55},[45,56020,56021],{"class":99}," \"\"\n",[45,56023,56024],{"class":47,"line":63},[45,56025,67],{"emptyLinePlaceholder":66},[45,56027,56028,56030,56032,56034,56036],{"class":47,"line":70},[45,56029,20],{"class":73},[45,56031,1582],{"class":51},[45,56033,1129],{"class":55},[45,56035,27950],{"class":99},[45,56037,86],{"class":85},[45,56039,56040,56042,56044,56046,56049,56051],{"class":47,"line":89},[45,56041,93],{"class":92},[45,56043,96],{"class":85},[45,56045,100],{"class":99},[45,56047,56048],{"class":103},"The string is empty",[45,56050,100],{"class":99},[45,56052,109],{"class":85},[712,56054,56056],{"id":56055},"check-if-a-user-is-old-enough","Check if a user is old enough",[36,56058,56060],{"className":38,"code":56059,"language":40,"meta":41,"style":41},"age = 20\n\nif age >= 18:\n    print(\"You are old enough\")\n",[18,56061,56062,56070,56074,56086],{"__ignoreMap":41},[45,56063,56064,56066,56068],{"class":47,"line":48},[45,56065,52],{"class":51},[45,56067,56],{"class":55},[45,56069,1883],{"class":59},[45,56071,56072],{"class":47,"line":63},[45,56073,67],{"emptyLinePlaceholder":66},[45,56075,56076,56078,56080,56082,56084],{"class":47,"line":70},[45,56077,20],{"class":73},[45,56079,76],{"class":51},[45,56081,600],{"class":55},[45,56083,121],{"class":59},[45,56085,86],{"class":85},[45,56087,56088,56090,56092,56094,56097,56099],{"class":47,"line":89},[45,56089,93],{"class":92},[45,56091,96],{"class":85},[45,56093,100],{"class":99},[45,56095,56096],{"class":103},"You are old enough",[45,56098,100],{"class":99},[45,56100,109],{"class":85},[14,56102,56103],{},"These examples all follow the same pattern:",[195,56105,56106,56109,56112],{},[198,56107,56108],{},"write a condition",[198,56110,56111],{},"let Python check it",[198,56113,56114],{},"run code only if it is true",[31,56116,56118],{"id":56117},"using-if-with-boolean-values","Using if with boolean values",[14,56120,56121,56122,230,56124,240],{},"A boolean value is either ",[18,56123,229],{},[18,56125,233],{},[14,56127,56128,56129,56131],{},"You can use a boolean variable directly in an ",[18,56130,20],{}," statement.",[14,56133,466],{},[36,56135,56137],{"className":38,"code":56136,"language":40,"meta":41,"style":41},"is_logged_in = True\n\nif is_logged_in:\n    print(\"Welcome back\")\n",[18,56138,56139,56147,56151,56159],{"__ignoreMap":41},[45,56140,56141,56143,56145],{"class":47,"line":48},[45,56142,33655],{"class":51},[45,56144,56],{"class":55},[45,56146,482],{"class":481},[45,56148,56149],{"class":47,"line":63},[45,56150,67],{"emptyLinePlaceholder":66},[45,56152,56153,56155,56157],{"class":47,"line":70},[45,56154,20],{"class":73},[45,56156,34606],{"class":51},[45,56158,86],{"class":85},[45,56160,56161,56163,56165,56167,56170,56172],{"class":47,"line":89},[45,56162,93],{"class":92},[45,56164,96],{"class":85},[45,56166,100],{"class":99},[45,56168,56169],{"class":103},"Welcome back",[45,56171,100],{"class":99},[45,56173,109],{"class":85},[14,56175,56176,56177,56179],{},"This works because ",[18,56178,33682],{}," already holds a boolean value.",[14,56181,56182],{},"You do not need to write:",[36,56184,56186],{"className":38,"code":56185,"language":40,"meta":41,"style":41},"if is_logged_in == True:\n",[18,56187,56188],{"__ignoreMap":41},[45,56189,56190,56192,56194,56196,56198],{"class":47,"line":48},[45,56191,20],{"class":73},[45,56193,35315],{"class":51},[45,56195,1129],{"class":55},[45,56197,23323],{"class":481},[45,56199,86],{"class":85},[14,56201,56202],{},"That works, but this is cleaner:",[36,56204,56206],{"className":38,"code":56205,"language":40,"meta":41,"style":41},"if is_logged_in:\n",[18,56207,56208],{"__ignoreMap":41},[45,56209,56210,56212,56214],{"class":47,"line":48},[45,56211,20],{"class":73},[45,56213,34606],{"class":51},[45,56215,86],{"class":85},[14,56217,40002],{},[36,56219,56221],{"className":38,"code":56220,"language":40,"meta":41,"style":41},"has_email = False\n\nif has_email:\n    print(\"Email found\")\n",[18,56222,56223,56232,56236,56245],{"__ignoreMap":41},[45,56224,56225,56228,56230],{"class":47,"line":48},[45,56226,56227],{"class":51},"has_email ",[45,56229,56],{"class":55},[45,56231,33660],{"class":481},[45,56233,56234],{"class":47,"line":63},[45,56235,67],{"emptyLinePlaceholder":66},[45,56237,56238,56240,56243],{"class":47,"line":70},[45,56239,20],{"class":73},[45,56241,56242],{"class":51}," has_email",[45,56244,86],{"class":85},[45,56246,56247,56249,56251,56253,56256,56258],{"class":47,"line":89},[45,56248,93],{"class":92},[45,56250,96],{"class":85},[45,56252,100],{"class":99},[45,56254,56255],{"class":103},"Email found",[45,56257,100],{"class":99},[45,56259,109],{"class":85},[14,56261,55757,56262,36998,56265,56267],{},[18,56263,56264],{},"has_email",[18,56266,233],{},", nothing is printed.",[31,56269,56271],{"id":56270},"what-happens-when-the-condition-is-false","What happens when the condition is False",[14,56273,55458,56274,56276],{},[18,56275,233],{},", Python skips the indented block.",[14,56278,56279],{},"Then the program continues with the next line after the block.",[14,56281,466],{},[36,56283,56285],{"className":38,"code":56284,"language":40,"meta":41,"style":41},"age = 15\n\nif age >= 18:\n    print(\"Adult\")\n\nprint(\"Program finished\")\n",[18,56286,56287,56296,56300,56312,56326,56330],{"__ignoreMap":41},[45,56288,56289,56291,56293],{"class":47,"line":48},[45,56290,52],{"class":51},[45,56292,56],{"class":55},[45,56294,56295],{"class":59}," 15\n",[45,56297,56298],{"class":47,"line":63},[45,56299,67],{"emptyLinePlaceholder":66},[45,56301,56302,56304,56306,56308,56310],{"class":47,"line":70},[45,56303,20],{"class":73},[45,56305,76],{"class":51},[45,56307,600],{"class":55},[45,56309,121],{"class":59},[45,56311,86],{"class":85},[45,56313,56314,56316,56318,56320,56322,56324],{"class":47,"line":89},[45,56315,93],{"class":92},[45,56317,96],{"class":85},[45,56319,100],{"class":99},[45,56321,1710],{"class":103},[45,56323,100],{"class":99},[45,56325,109],{"class":85},[45,56327,56328],{"class":47,"line":112},[45,56329,67],{"emptyLinePlaceholder":66},[45,56331,56332,56334,56336,56338,56341,56343],{"class":47,"line":126},[45,56333,2065],{"class":92},[45,56335,96],{"class":85},[45,56337,100],{"class":99},[45,56339,56340],{"class":103},"Program finished",[45,56342,100],{"class":99},[45,56344,109],{"class":85},[14,56346,177],{},[36,56348,56350],{"className":38,"code":56349,"language":40,"meta":41,"style":41},"Program finished\n",[18,56351,56352],{"__ignoreMap":41},[45,56353,56354],{"class":47,"line":48},[45,56355,56349],{"class":51},[14,56357,56358,56359,56362],{},"The line ",[18,56360,56361],{},"print(\"Adult\")"," does not run because the condition is false.",[14,56364,56365,56366,1112],{},"This page focuses only on plain ",[18,56367,20],{},[14,56369,56370,56371,240],{},"If you want code to run when the condition is false, learn ",[236,56372,56373],{"href":34426},"Python if-else and elif explained",[31,56375,56377],{"id":56376},"indentation-rules-to-remember","Indentation rules to remember",[14,56379,56380],{},"Indentation is very important in Python.",[14,56382,56383,56384,56386],{},"The code inside an ",[18,56385,20],{}," statement must be indented. This tells Python which lines belong to the block.",[14,56388,1466],{},[36,56390,56392],{"className":38,"code":56391,"language":40,"meta":41,"style":41},"age = 18\n\nif age >= 18:\n    print(\"Adult\")\n",[18,56393,56394,56402,56406,56418],{"__ignoreMap":41},[45,56395,56396,56398,56400],{"class":47,"line":48},[45,56397,52],{"class":51},[45,56399,56],{"class":55},[45,56401,60],{"class":59},[45,56403,56404],{"class":47,"line":63},[45,56405,67],{"emptyLinePlaceholder":66},[45,56407,56408,56410,56412,56414,56416],{"class":47,"line":70},[45,56409,20],{"class":73},[45,56411,76],{"class":51},[45,56413,600],{"class":55},[45,56415,121],{"class":59},[45,56417,86],{"class":85},[45,56419,56420,56422,56424,56426,56428,56430],{"class":47,"line":89},[45,56421,93],{"class":92},[45,56423,96],{"class":85},[45,56425,100],{"class":99},[45,56427,1710],{"class":103},[45,56429,100],{"class":99},[45,56431,109],{"class":85},[14,56433,28934],{},[36,56435,56437],{"className":38,"code":56436,"language":40,"meta":41,"style":41},"age = 18\n\nif age >= 18:\nprint(\"Adult\")\n",[18,56438,56439,56447,56451,56463],{"__ignoreMap":41},[45,56440,56441,56443,56445],{"class":47,"line":48},[45,56442,52],{"class":51},[45,56444,56],{"class":55},[45,56446,60],{"class":59},[45,56448,56449],{"class":47,"line":63},[45,56450,67],{"emptyLinePlaceholder":66},[45,56452,56453,56455,56457,56459,56461],{"class":47,"line":70},[45,56454,20],{"class":73},[45,56456,76],{"class":51},[45,56458,600],{"class":55},[45,56460,121],{"class":59},[45,56462,86],{"class":85},[45,56464,56465,56467,56469,56471,56473,56475],{"class":47,"line":89},[45,56466,2065],{"class":92},[45,56468,96],{"class":85},[45,56470,100],{"class":99},[45,56472,1710],{"class":103},[45,56474,100],{"class":99},[45,56476,109],{"class":85},[14,56478,56479,56480,56482],{},"The second example causes an error because the ",[18,56481,15833],{}," line is not indented.",[14,56484,56485],{},"Rules to remember:",[195,56487,56488,56493,56496],{},[198,56489,56490,56491,55438],{},"indent every line inside the ",[18,56492,20],{},[198,56494,56495],{},"use consistent spaces",[198,56497,56498],{},"do not mix indentation styles in the same file",[14,56500,49651,56501,240],{},[236,56502,554],{"href":553},[31,56504,1337],{"id":1336},[14,56506,56507,56508,1112],{},"Here are some common problems when writing ",[18,56509,20],{},[712,56511,1453],{"id":1452},[14,56513,1358],{},[36,56515,56517],{"className":38,"code":56516,"language":40,"meta":41,"style":41},"age = 18\n\nif age >= 18\n    print(\"Adult\")\n",[18,56518,56519,56527,56531,56541],{"__ignoreMap":41},[45,56520,56521,56523,56525],{"class":47,"line":48},[45,56522,52],{"class":51},[45,56524,56],{"class":55},[45,56526,60],{"class":59},[45,56528,56529],{"class":47,"line":63},[45,56530,67],{"emptyLinePlaceholder":66},[45,56532,56533,56535,56537,56539],{"class":47,"line":70},[45,56534,20],{"class":73},[45,56536,76],{"class":51},[45,56538,600],{"class":55},[45,56540,60],{"class":59},[45,56542,56543,56545,56547,56549,56551,56553],{"class":47,"line":89},[45,56544,93],{"class":92},[45,56546,96],{"class":85},[45,56548,100],{"class":99},[45,56550,1710],{"class":103},[45,56552,100],{"class":99},[45,56554,109],{"class":85},[14,56556,56557,56558,55544],{},"Python expects a colon at the end of the ",[18,56559,20],{},[14,56561,6003],{},[36,56563,56564],{"className":38,"code":56391,"language":40,"meta":41,"style":41},[18,56565,56566,56574,56578,56590],{"__ignoreMap":41},[45,56567,56568,56570,56572],{"class":47,"line":48},[45,56569,52],{"class":51},[45,56571,56],{"class":55},[45,56573,60],{"class":59},[45,56575,56576],{"class":47,"line":63},[45,56577,67],{"emptyLinePlaceholder":66},[45,56579,56580,56582,56584,56586,56588],{"class":47,"line":70},[45,56581,20],{"class":73},[45,56583,76],{"class":51},[45,56585,600],{"class":55},[45,56587,121],{"class":59},[45,56589,86],{"class":85},[45,56591,56592,56594,56596,56598,56600,56602],{"class":47,"line":89},[45,56593,93],{"class":92},[45,56595,96],{"class":85},[45,56597,100],{"class":99},[45,56599,1710],{"class":103},[45,56601,100],{"class":99},[45,56603,109],{"class":85},[14,56605,56606,56607,240],{},"If you see this error, read ",[236,56608,56609],{"href":1540},"SyntaxError: missing colon fix",[712,56611,1350,56612,1353,56614],{"id":1349},[18,56613,56],{},[18,56615,1129],{},[14,56617,36919],{},[14,56619,3806],{},[36,56621,56623],{"className":38,"code":56622,"language":40,"meta":41,"style":41},"username = \"admin\"\n\nif username = \"admin\":\n    print(\"Welcome\")\n",[18,56624,56625,56637,56641,56657],{"__ignoreMap":41},[45,56626,56627,56629,56631,56633,56635],{"class":47,"line":48},[45,56628,41765],{"class":51},[45,56630,56],{"class":55},[45,56632,1565],{"class":99},[45,56634,55840],{"class":103},[45,56636,1571],{"class":99},[45,56638,56639],{"class":47,"line":63},[45,56640,67],{"emptyLinePlaceholder":66},[45,56642,56643,56645,56647,56649,56651,56653,56655],{"class":47,"line":70},[45,56644,20],{"class":73},[45,56646,55853],{"class":51},[45,56648,56],{"class":55},[45,56650,1565],{"class":99},[45,56652,55840],{"class":103},[45,56654,100],{"class":99},[45,56656,86],{"class":85},[45,56658,56659,56661,56663,56665,56667,56669],{"class":47,"line":89},[45,56660,93],{"class":92},[45,56662,96],{"class":85},[45,56664,100],{"class":99},[45,56666,6541],{"class":103},[45,56668,100],{"class":99},[45,56670,109],{"class":85},[14,56672,1466],{},[36,56674,56676],{"className":38,"code":56675,"language":40,"meta":41,"style":41},"username = \"admin\"\n\nif username == \"admin\":\n    print(\"Welcome\")\n",[18,56677,56678,56690,56694,56710],{"__ignoreMap":41},[45,56679,56680,56682,56684,56686,56688],{"class":47,"line":48},[45,56681,41765],{"class":51},[45,56683,56],{"class":55},[45,56685,1565],{"class":99},[45,56687,55840],{"class":103},[45,56689,1571],{"class":99},[45,56691,56692],{"class":47,"line":63},[45,56693,67],{"emptyLinePlaceholder":66},[45,56695,56696,56698,56700,56702,56704,56706,56708],{"class":47,"line":70},[45,56697,20],{"class":73},[45,56699,55853],{"class":51},[45,56701,1129],{"class":55},[45,56703,1565],{"class":99},[45,56705,55840],{"class":103},[45,56707,100],{"class":99},[45,56709,86],{"class":85},[45,56711,56712,56714,56716,56718,56720,56722],{"class":47,"line":89},[45,56713,93],{"class":92},[45,56715,96],{"class":85},[45,56717,100],{"class":99},[45,56719,6541],{"class":103},[45,56721,100],{"class":99},[45,56723,109],{"class":85},[14,56725,56726],{},"Remember:",[195,56728,56729,56733],{},[198,56730,56731,1325],{},[18,56732,56],{},[198,56734,56735,1330],{},[18,56736,1129],{},[712,56738,56740],{"id":56739},"not-indenting-the-block","Not indenting the block",[14,56742,3806],{},[36,56744,56745],{"className":38,"code":56436,"language":40,"meta":41,"style":41},[18,56746,56747,56755,56759,56771],{"__ignoreMap":41},[45,56748,56749,56751,56753],{"class":47,"line":48},[45,56750,52],{"class":51},[45,56752,56],{"class":55},[45,56754,60],{"class":59},[45,56756,56757],{"class":47,"line":63},[45,56758,67],{"emptyLinePlaceholder":66},[45,56760,56761,56763,56765,56767,56769],{"class":47,"line":70},[45,56762,20],{"class":73},[45,56764,76],{"class":51},[45,56766,600],{"class":55},[45,56768,121],{"class":59},[45,56770,86],{"class":85},[45,56772,56773,56775,56777,56779,56781,56783],{"class":47,"line":89},[45,56774,2065],{"class":92},[45,56776,96],{"class":85},[45,56778,100],{"class":99},[45,56780,1710],{"class":103},[45,56782,100],{"class":99},[45,56784,109],{"class":85},[14,56786,1466],{},[36,56788,56789],{"className":38,"code":56391,"language":40,"meta":41,"style":41},[18,56790,56791,56799,56803,56815],{"__ignoreMap":41},[45,56792,56793,56795,56797],{"class":47,"line":48},[45,56794,52],{"class":51},[45,56796,56],{"class":55},[45,56798,60],{"class":59},[45,56800,56801],{"class":47,"line":63},[45,56802,67],{"emptyLinePlaceholder":66},[45,56804,56805,56807,56809,56811,56813],{"class":47,"line":70},[45,56806,20],{"class":73},[45,56808,76],{"class":51},[45,56810,600],{"class":55},[45,56812,121],{"class":59},[45,56814,86],{"class":85},[45,56816,56817,56819,56821,56823,56825,56827],{"class":47,"line":89},[45,56818,93],{"class":92},[45,56820,96],{"class":85},[45,56822,100],{"class":99},[45,56824,1710],{"class":103},[45,56826,100],{"class":99},[45,56828,109],{"class":85},[14,56830,56831,56832,240],{},"If Python says it expected an indented block, see ",[236,56833,56834],{"href":1613},"IndentationError: expected an indented block fix",[712,56836,56838],{"id":56837},"expecting-the-block-to-run-when-the-condition-is-false","Expecting the block to run when the condition is False",[14,56840,19451,56841,56843,56844,240],{},[18,56842,20],{}," block only runs when the condition is ",[18,56845,229],{},[14,56847,56848],{},"If the condition is false, Python skips it.",[14,56850,56851],{},"To check what is happening, print the value and the condition result:",[36,56853,56855],{"className":38,"code":56854,"language":40,"meta":41,"style":41},"age = 16\n\nprint(age)\nprint(type(age))\nprint(age >= 18)\nprint(\"admin\" == \"admin\")\n",[18,56856,56857,56865,56869,56879,56893,56907],{"__ignoreMap":41},[45,56858,56859,56861,56863],{"class":47,"line":48},[45,56860,52],{"class":51},[45,56862,56],{"class":55},[45,56864,986],{"class":59},[45,56866,56867],{"class":47,"line":63},[45,56868,67],{"emptyLinePlaceholder":66},[45,56870,56871,56873,56875,56877],{"class":47,"line":70},[45,56872,2065],{"class":92},[45,56874,96],{"class":85},[45,56876,2082],{"class":2070},[45,56878,109],{"class":85},[45,56880,56881,56883,56885,56887,56889,56891],{"class":47,"line":89},[45,56882,2065],{"class":92},[45,56884,96],{"class":85},[45,56886,4340],{"class":2109},[45,56888,96],{"class":85},[45,56890,2082],{"class":2070},[45,56892,4348],{"class":85},[45,56894,56895,56897,56899,56901,56903,56905],{"class":47,"line":112},[45,56896,2065],{"class":92},[45,56898,96],{"class":85},[45,56900,52],{"class":2070},[45,56902,600],{"class":55},[45,56904,121],{"class":59},[45,56906,109],{"class":85},[45,56908,56909,56911,56913,56915,56917,56919,56921,56923,56925,56927],{"class":47,"line":126},[45,56910,2065],{"class":92},[45,56912,96],{"class":85},[45,56914,100],{"class":99},[45,56916,55840],{"class":103},[45,56918,100],{"class":99},[45,56920,9334],{"class":55},[45,56922,1565],{"class":99},[45,56924,55840],{"class":103},[45,56926,100],{"class":99},[45,56928,109],{"class":85},[14,56930,14549],{},[36,56932,56934],{"className":38,"code":56933,"language":40,"meta":41,"style":41},"print(age)\nprint(type(age))\nprint(age >= 18)\nprint(username == \"admin\")\n",[18,56935,56936,56946,56960,56974],{"__ignoreMap":41},[45,56937,56938,56940,56942,56944],{"class":47,"line":48},[45,56939,2065],{"class":92},[45,56941,96],{"class":85},[45,56943,2082],{"class":2070},[45,56945,109],{"class":85},[45,56947,56948,56950,56952,56954,56956,56958],{"class":47,"line":63},[45,56949,2065],{"class":92},[45,56951,96],{"class":85},[45,56953,4340],{"class":2109},[45,56955,96],{"class":85},[45,56957,2082],{"class":2070},[45,56959,4348],{"class":85},[45,56961,56962,56964,56966,56968,56970,56972],{"class":47,"line":70},[45,56963,2065],{"class":92},[45,56965,96],{"class":85},[45,56967,52],{"class":2070},[45,56969,600],{"class":55},[45,56971,121],{"class":59},[45,56973,109],{"class":85},[45,56975,56976,56978,56980,56982,56984,56986,56988,56990],{"class":47,"line":89},[45,56977,2065],{"class":92},[45,56979,96],{"class":85},[45,56981,41765],{"class":2070},[45,56983,1129],{"class":55},[45,56985,1565],{"class":99},[45,56987,55840],{"class":103},[45,56989,100],{"class":99},[45,56991,109],{"class":85},[14,56993,56994],{},"These help you confirm:",[195,56996,56997,57000,57003],{},[198,56998,56999],{},"the actual value",[198,57001,57002],{},"the data type",[198,57004,57005],{},"whether the condition is true or false",[712,57007,57009],{"id":57008},"comparing-unexpected-types","Comparing unexpected types",[14,57011,57012],{},"Sometimes a value looks correct but has the wrong type.",[14,57014,466],{},[36,57016,57018],{"className":38,"code":57017,"language":40,"meta":41,"style":41},"age = \"18\"\n\nif age >= 18:\n    print(\"Adult\")\n",[18,57019,57020,57033,57037,57049],{"__ignoreMap":41},[45,57021,57022,57024,57026,57028,57031],{"class":47,"line":48},[45,57023,52],{"class":51},[45,57025,56],{"class":55},[45,57027,1565],{"class":99},[45,57029,57030],{"class":103},"18",[45,57032,1571],{"class":99},[45,57034,57035],{"class":47,"line":63},[45,57036,67],{"emptyLinePlaceholder":66},[45,57038,57039,57041,57043,57045,57047],{"class":47,"line":70},[45,57040,20],{"class":73},[45,57042,76],{"class":51},[45,57044,600],{"class":55},[45,57046,121],{"class":59},[45,57048,86],{"class":85},[45,57050,57051,57053,57055,57057,57059,57061],{"class":47,"line":89},[45,57052,93],{"class":92},[45,57054,96],{"class":85},[45,57056,100],{"class":99},[45,57058,1710],{"class":103},[45,57060,100],{"class":99},[45,57062,109],{"class":85},[14,57064,57065,57066,57069],{},"This does not work because ",[18,57067,57068],{},"\"18\""," is a string, not a number.",[14,57071,57072],{},"You can inspect the type with:",[36,57074,57076],{"className":38,"code":57075,"language":40,"meta":41,"style":41},"age = \"18\"\nprint(type(age))\n",[18,57077,57078,57090],{"__ignoreMap":41},[45,57079,57080,57082,57084,57086,57088],{"class":47,"line":48},[45,57081,52],{"class":51},[45,57083,56],{"class":55},[45,57085,1565],{"class":99},[45,57087,57030],{"class":103},[45,57089,1571],{"class":99},[45,57091,57092,57094,57096,57098,57100,57102],{"class":47,"line":63},[45,57093,2065],{"class":92},[45,57095,96],{"class":85},[45,57097,4340],{"class":2109},[45,57099,96],{"class":85},[45,57101,2082],{"class":2070},[45,57103,4348],{"class":85},[31,57105,2281],{"id":2280},[712,57107,2362],{"id":57108},"what-is-an-if-statement-in-python",[14,57110,57111,57112,240],{},"It is a control statement that runs code only when a condition is ",[18,57113,229],{},[712,57115,57117],{"id":57116},"do-i-need-a-colon-after-if-in-python","Do I need a colon after if in Python?",[14,57119,57120,57121,1463],{},"Yes. The ",[18,57122,20],{},[712,57124,57126],{"id":57125},"what-is-the-difference-between-and-in-an-if-statement","What is the difference between = and == in an if statement?",[14,57128,57129,57131,57132,35191],{},[18,57130,56],{}," assigns a value. ",[18,57133,1129],{},[712,57135,57137],{"id":57136},"can-an-if-statement-run-more-than-one-line","Can an if statement run more than one line?",[14,57139,57140,57141,57143],{},"Yes. Any indented lines under the ",[18,57142,20],{}," statement are part of the block.",[14,57145,466],{},[36,57147,57149],{"className":38,"code":57148,"language":40,"meta":41,"style":41},"age = 18\n\nif age >= 18:\n    print(\"Adult\")\n    print(\"Access allowed\")\n",[18,57150,57151,57159,57163,57175,57189],{"__ignoreMap":41},[45,57152,57153,57155,57157],{"class":47,"line":48},[45,57154,52],{"class":51},[45,57156,56],{"class":55},[45,57158,60],{"class":59},[45,57160,57161],{"class":47,"line":63},[45,57162,67],{"emptyLinePlaceholder":66},[45,57164,57165,57167,57169,57171,57173],{"class":47,"line":70},[45,57166,20],{"class":73},[45,57168,76],{"class":51},[45,57170,600],{"class":55},[45,57172,121],{"class":59},[45,57174,86],{"class":85},[45,57176,57177,57179,57181,57183,57185,57187],{"class":47,"line":89},[45,57178,93],{"class":92},[45,57180,96],{"class":85},[45,57182,100],{"class":99},[45,57184,1710],{"class":103},[45,57186,100],{"class":99},[45,57188,109],{"class":85},[45,57190,57191,57193,57195,57197,57200,57202],{"class":47,"line":112},[45,57192,93],{"class":92},[45,57194,96],{"class":85},[45,57196,100],{"class":99},[45,57198,57199],{"class":103},"Access allowed",[45,57201,100],{"class":99},[45,57203,109],{"class":85},[712,57205,57207],{"id":57206},"what-if-i-want-code-to-run-when-the-condition-is-false","What if I want code to run when the condition is False?",[14,57209,165,57210,57213,57214,57216],{},[18,57211,57212],{},"if-else",". That is covered on the ",[236,57215,56373],{"href":34426}," page.",[31,57218,2342],{"id":2341},[195,57220,57221,57225,57229,57233,57237],{},[198,57222,57223],{},[236,57224,41161],{"href":238},[198,57226,57227],{},[236,57228,56373],{"href":34426},[198,57230,57231],{},[236,57232,554],{"href":553},[198,57234,57235],{},[236,57236,40783],{"href":40782},[198,57238,57239],{},[236,57240,2362],{"href":2361},[14,57242,57243,57244,57246,57247,21,57249,21,57251,57253],{},"Once you understand a single ",[18,57245,20],{}," statement, the next step is to learn how to build full decision logic with ",[18,57248,20],{},[18,57250,28],{},[18,57252,24],{},", and boolean expressions.",[2367,57255,57256],{},"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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":57258},[57259,57260,57261,57262,57263,57269,57270,57271,57272,57279,57286],{"id":33,"depth":63,"text":34},{"id":55441,"depth":63,"text":55442},{"id":55547,"depth":63,"text":55548},{"id":55664,"depth":63,"text":55665},{"id":55891,"depth":63,"text":55892,"children":57264},[57265,57266,57267,57268],{"id":55898,"depth":70,"text":55899},{"id":55947,"depth":70,"text":55948},{"id":56006,"depth":70,"text":56007},{"id":56055,"depth":70,"text":56056},{"id":56117,"depth":63,"text":56118},{"id":56270,"depth":63,"text":56271},{"id":56376,"depth":63,"text":56377},{"id":1336,"depth":63,"text":1337,"children":57273},[57274,57275,57276,57277,57278],{"id":1452,"depth":70,"text":1453},{"id":1349,"depth":70,"text":2383},{"id":56739,"depth":70,"text":56740},{"id":56837,"depth":70,"text":56838},{"id":57008,"depth":70,"text":57009},{"id":2280,"depth":63,"text":2281,"children":57280},[57281,57282,57283,57284,57285],{"id":57108,"depth":70,"text":2362},{"id":57116,"depth":70,"text":57117},{"id":57125,"depth":70,"text":57126},{"id":57136,"depth":70,"text":57137},{"id":57206,"depth":70,"text":57207},{"id":2341,"depth":63,"text":2342},"Master python if statements explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-if-statements-explained",{"title":55345,"description":57287},"learn\u002Fpython-if-statements-explained","EcoJ8FYLrdjhgFEFzZIYBrEb8aJtnNzGZx3r8iF3j7c",{"id":57294,"title":57295,"body":57296,"description":58535,"extension":2399,"meta":58536,"navigation":66,"path":58537,"seo":58538,"stem":58539,"__hash__":58540},"content\u002Flearn\u002Fpython-indentation-rules-and-why-they-matter.md","Python Indentation Rules and Why They Matter",{"type":7,"value":57297,"toc":58512},[57298,57301,57304,57310,57313,57319,57321,57379,57382,57386,57389,57392,57395,57421,57424,57426,57477,57481,57494,57496,57529,57536,57540,57543,57546,57560,57562,57638,57646,57649,57710,57719,57722,57726,57729,57732,57770,57773,57804,57806,57822,57825,57832,57836,57839,57853,57856,57859,57900,57903,57914,57917,57921,57924,57927,57930,57933,58015,58019,58054,58056,58080,58083,58087,58090,58106,58109,58120,58123,58150,58157,58159,58186,58188,58230,58236,58239,58256,58260,58263,58280,58283,58286,58288,58291,58308,58311,58345,58348,58392,58395,58399,58402,58414,58421,58438,58441,58443,58447,58450,58454,58457,58461,58467,58471,58477,58479,58509],[10,57299,57295],{"id":57300},"python-indentation-rules-and-why-they-matter",[14,57302,57303],{},"Indentation in Python means the spaces at the beginning of a line.",[14,57305,57306,57307,57309],{},"In many programming languages, blocks of code are grouped with braces like ",[18,57308,21683],{},". Python works differently. It uses indentation to show which lines belong together.",[14,57311,57312],{},"This is why indentation is not just a style choice in Python. It is part of the language syntax.",[14,57314,57315,57316,57318],{},"If you are learning Python syntax, indentation is one of the first things to understand. It affects ",[18,57317,20],{}," statements, loops, functions, classes, and error handling blocks.",[31,57320,34],{"id":33},[36,57322,57324],{"className":38,"code":57323,"language":40,"meta":41,"style":41},"if True:\n    print(\"Indented correctly\")\n\nfor i in range(3):\n    print(i)\n",[18,57325,57326,57334,57349,57353,57369],{"__ignoreMap":41},[45,57327,57328,57330,57332],{"class":47,"line":48},[45,57329,20],{"class":73},[45,57331,23323],{"class":481},[45,57333,86],{"class":85},[45,57335,57336,57338,57340,57342,57345,57347],{"class":47,"line":63},[45,57337,93],{"class":92},[45,57339,96],{"class":85},[45,57341,100],{"class":99},[45,57343,57344],{"class":103},"Indented correctly",[45,57346,100],{"class":99},[45,57348,109],{"class":85},[45,57350,57351],{"class":47,"line":70},[45,57352,67],{"emptyLinePlaceholder":66},[45,57354,57355,57357,57359,57361,57363,57365,57367],{"class":47,"line":89},[45,57356,7895],{"class":73},[45,57358,36273],{"class":51},[45,57360,8623],{"class":73},[45,57362,10451],{"class":92},[45,57364,96],{"class":85},[45,57366,11184],{"class":59},[45,57368,2464],{"class":85},[45,57370,57371,57373,57375,57377],{"class":47,"line":112},[45,57372,93],{"class":92},[45,57374,96],{"class":85},[45,57376,36312],{"class":2070},[45,57378,109],{"class":85},[14,57380,57381],{},"Use consistent spaces for code inside blocks. A common beginner rule is: indent each block with 4 spaces.",[31,57383,57385],{"id":57384},"what-indentation-means-in-python","What indentation means in Python",[14,57387,57388],{},"Indentation is the space at the start of a line.",[14,57390,57391],{},"Python uses that space to decide which lines belong to the same block of code.",[14,57393,57394],{},"A block is a group of lines that run together under statements such as:",[195,57396,57397,57401,57405,57409,57413,57417],{},[198,57398,57399],{},[18,57400,20],{},[198,57402,57403],{},[18,57404,7895],{},[198,57406,57407],{},[18,57408,23529],{},[198,57410,57411],{},[18,57412,2757],{},[198,57414,57415],{},[18,57416,2441],{},[198,57418,57419],{},[18,57420,23272],{},[14,57422,57423],{},"Unlike some languages, Python does not use braces to define blocks. The indentation itself defines the block.",[14,57425,2115],{},[36,57427,57429],{"className":38,"code":57428,"language":40,"meta":41,"style":41},"if 5 > 2:\n    print(\"This line is inside the if block\")\n\nprint(\"This line is outside the if block\")\n",[18,57430,57431,57443,57458,57462],{"__ignoreMap":41},[45,57432,57433,57435,57437,57439,57441],{"class":47,"line":48},[45,57434,20],{"class":73},[45,57436,1222],{"class":59},[45,57438,33591],{"class":55},[45,57440,3754],{"class":59},[45,57442,86],{"class":85},[45,57444,57445,57447,57449,57451,57454,57456],{"class":47,"line":63},[45,57446,93],{"class":92},[45,57448,96],{"class":85},[45,57450,100],{"class":99},[45,57452,57453],{"class":103},"This line is inside the if block",[45,57455,100],{"class":99},[45,57457,109],{"class":85},[45,57459,57460],{"class":47,"line":70},[45,57461,67],{"emptyLinePlaceholder":66},[45,57463,57464,57466,57468,57470,57473,57475],{"class":47,"line":89},[45,57465,2065],{"class":92},[45,57467,96],{"class":85},[45,57469,100],{"class":99},[45,57471,57472],{"class":103},"This line is outside the if block",[45,57474,100],{"class":99},[45,57476,109],{"class":85},[712,57478,57480],{"id":57479},"what-this-code-does","What this code does",[195,57482,57483,57488],{},[198,57484,57485,57486,55438],{},"The line with 4 spaces belongs to the ",[18,57487,20],{},[198,57489,57490,57491,57493],{},"The last ",[18,57492,15833],{}," line is not indented, so it is outside the block",[14,57495,177],{},[36,57497,57499],{"className":38,"code":57498,"language":40,"meta":41,"style":41},"This line is inside the if block\nThis line is outside the if block\n",[18,57500,57501,57516],{"__ignoreMap":41},[45,57502,57503,57506,57508,57511,57513],{"class":47,"line":48},[45,57504,57505],{"class":51},"This line ",[45,57507,1285],{"class":55},[45,57509,57510],{"class":51}," inside the ",[45,57512,20],{"class":73},[45,57514,57515],{"class":51}," block\n",[45,57517,57518,57520,57522,57525,57527],{"class":47,"line":63},[45,57519,57505],{"class":51},[45,57521,1285],{"class":55},[45,57523,57524],{"class":51}," outside the ",[45,57526,20],{"class":73},[45,57528,57515],{"class":51},[14,57530,57531,57532,1316,57534,240],{},"If you are new to Python structure, this idea also appears in ",[236,57533,549],{"href":548},[236,57535,5879],{"href":4579},[31,57537,57539],{"id":57538},"why-indentation-matters","Why indentation matters",[14,57541,57542],{},"Indentation matters because it tells Python where a block starts and ends.",[14,57544,57545],{},"This affects both correctness and meaning.",[195,57547,57548,57551,57554,57557],{},[198,57549,57550],{},"It tells Python where a block starts and ends",[198,57552,57553],{},"Wrong indentation can change program behavior",[198,57555,57556],{},"Missing indentation can cause an error",[198,57558,57559],{},"Inconsistent indentation makes code harder to read",[14,57561,718],{},[36,57563,57565],{"className":38,"code":57564,"language":40,"meta":41,"style":41},"age = 18\n\nif age >= 18:\n    print(\"Adult\")\n    print(\"You can vote\")\n\nprint(\"Done\")\n",[18,57566,57567,57575,57579,57591,57605,57619,57623],{"__ignoreMap":41},[45,57568,57569,57571,57573],{"class":47,"line":48},[45,57570,52],{"class":51},[45,57572,56],{"class":55},[45,57574,60],{"class":59},[45,57576,57577],{"class":47,"line":63},[45,57578,67],{"emptyLinePlaceholder":66},[45,57580,57581,57583,57585,57587,57589],{"class":47,"line":70},[45,57582,20],{"class":73},[45,57584,76],{"class":51},[45,57586,600],{"class":55},[45,57588,121],{"class":59},[45,57590,86],{"class":85},[45,57592,57593,57595,57597,57599,57601,57603],{"class":47,"line":89},[45,57594,93],{"class":92},[45,57596,96],{"class":85},[45,57598,100],{"class":99},[45,57600,1710],{"class":103},[45,57602,100],{"class":99},[45,57604,109],{"class":85},[45,57606,57607,57609,57611,57613,57615,57617],{"class":47,"line":112},[45,57608,93],{"class":92},[45,57610,96],{"class":85},[45,57612,100],{"class":99},[45,57614,33955],{"class":103},[45,57616,100],{"class":99},[45,57618,109],{"class":85},[45,57620,57621],{"class":47,"line":126},[45,57622,67],{"emptyLinePlaceholder":66},[45,57624,57625,57627,57629,57631,57634,57636],{"class":47,"line":142},[45,57626,2065],{"class":92},[45,57628,96],{"class":85},[45,57630,100],{"class":99},[45,57632,57633],{"class":103},"Done",[45,57635,100],{"class":99},[45,57637,109],{"class":85},[14,57639,57640,57641,57643,57644,1864],{},"Here, both indented ",[18,57642,15833],{}," lines are part of the ",[18,57645,20],{},[14,57647,57648],{},"Now compare it to this:",[36,57650,57652],{"className":38,"code":57651,"language":40,"meta":41,"style":41},"age = 18\n\nif age >= 18:\n    print(\"Adult\")\n\nprint(\"You can vote\")\n",[18,57653,57654,57662,57666,57678,57692,57696],{"__ignoreMap":41},[45,57655,57656,57658,57660],{"class":47,"line":48},[45,57657,52],{"class":51},[45,57659,56],{"class":55},[45,57661,60],{"class":59},[45,57663,57664],{"class":47,"line":63},[45,57665,67],{"emptyLinePlaceholder":66},[45,57667,57668,57670,57672,57674,57676],{"class":47,"line":70},[45,57669,20],{"class":73},[45,57671,76],{"class":51},[45,57673,600],{"class":55},[45,57675,121],{"class":59},[45,57677,86],{"class":85},[45,57679,57680,57682,57684,57686,57688,57690],{"class":47,"line":89},[45,57681,93],{"class":92},[45,57683,96],{"class":85},[45,57685,100],{"class":99},[45,57687,1710],{"class":103},[45,57689,100],{"class":99},[45,57691,109],{"class":85},[45,57693,57694],{"class":47,"line":112},[45,57695,67],{"emptyLinePlaceholder":66},[45,57697,57698,57700,57702,57704,57706,57708],{"class":47,"line":126},[45,57699,2065],{"class":92},[45,57701,96],{"class":85},[45,57703,100],{"class":99},[45,57705,33955],{"class":103},[45,57707,100],{"class":99},[45,57709,109],{"class":85},[14,57711,57712,57713,57715,57716,57718],{},"The second version is valid Python, but it means something different. The last line is outside the ",[18,57714,20],{}," block, so it will run no matter what ",[18,57717,2082],{}," is.",[14,57720,57721],{},"Small indentation changes can change how your program works.",[31,57723,57725],{"id":57724},"where-indentation-is-required","Where indentation is required",[14,57727,57728],{},"Indentation is required after statements that introduce a block.",[14,57730,57731],{},"You will use it in places like these:",[195,57733,57734,57743,57749,57754,57759],{},[198,57735,57736,57737,21,57739,25,57741,26090],{},"After ",[18,57738,20],{},[18,57740,24],{},[18,57742,28],{},[198,57744,5493,57745,1316,57747,26095],{},[18,57746,7895],{},[18,57748,23529],{},[198,57750,57751,57752],{},"Inside functions created with ",[18,57753,2757],{},[198,57755,57756,57757],{},"Inside classes created with ",[18,57758,2441],{},[198,57760,5493,57761,21,57763,21,57765,25,57767,26090],{},[18,57762,23272],{},[18,57764,23275],{},[18,57766,28],{},[18,57768,57769],{},"finally",[14,57771,57772],{},"Here is a simple example with a loop:",[36,57774,57776],{"className":38,"code":57775,"language":40,"meta":41,"style":41},"for i in range(3):\n    print(i)\n",[18,57777,57778,57794],{"__ignoreMap":41},[45,57779,57780,57782,57784,57786,57788,57790,57792],{"class":47,"line":48},[45,57781,7895],{"class":73},[45,57783,36273],{"class":51},[45,57785,8623],{"class":73},[45,57787,10451],{"class":92},[45,57789,96],{"class":85},[45,57791,11184],{"class":59},[45,57793,2464],{"class":85},[45,57795,57796,57798,57800,57802],{"class":47,"line":63},[45,57797,93],{"class":92},[45,57799,96],{"class":85},[45,57801,36312],{"class":2070},[45,57803,109],{"class":85},[14,57805,177],{},[36,57807,57808],{"className":38,"code":23950,"language":40,"meta":41,"style":41},[18,57809,57810,57814,57818],{"__ignoreMap":41},[45,57811,57812],{"class":47,"line":48},[45,57813,23957],{"class":59},[45,57815,57816],{"class":47,"line":63},[45,57817,13036],{"class":59},[45,57819,57820],{"class":47,"line":70},[45,57821,13041],{"class":59},[14,57823,57824],{},"Without the indentation, Python does not know which line belongs to the loop.",[14,57826,57827,57828,1316,57830,240],{},"If you want to learn these block types in more detail, see ",[236,57829,23611],{"href":23610},[236,57831,40356],{"href":39615},[31,57833,57835],{"id":57834},"the-standard-beginner-rule","The standard beginner rule",[14,57837,57838],{},"A simple rule for beginners is:",[195,57840,57841,57844,57847,57850],{},[198,57842,57843],{},"Use 4 spaces for each indentation level",[198,57845,57846],{},"Keep the same indentation style throughout a file",[198,57848,57849],{},"Do not mix tabs and spaces",[198,57851,57852],{},"Most code editors can insert 4 spaces automatically",[14,57854,57855],{},"This is the standard Python style and the easiest habit to follow.",[14,57857,57858],{},"Good example:",[36,57860,57862],{"className":38,"code":57861,"language":40,"meta":41,"style":41},"def say_hello():\n    print(\"Hello\")\n    print(\"Welcome\")\n",[18,57863,57864,57872,57886],{"__ignoreMap":41},[45,57865,57866,57868,57870],{"class":47,"line":48},[45,57867,2757],{"class":2440},[45,57869,4596],{"class":2455},[45,57871,3828],{"class":85},[45,57873,57874,57876,57878,57880,57882,57884],{"class":47,"line":63},[45,57875,93],{"class":92},[45,57877,96],{"class":85},[45,57879,100],{"class":99},[45,57881,3226],{"class":103},[45,57883,100],{"class":99},[45,57885,109],{"class":85},[45,57887,57888,57890,57892,57894,57896,57898],{"class":47,"line":70},[45,57889,93],{"class":92},[45,57891,96],{"class":85},[45,57893,100],{"class":99},[45,57895,6541],{"class":103},[45,57897,100],{"class":99},[45,57899,109],{"class":85},[14,57901,57902],{},"Less safe for beginners:",[195,57904,57905,57908,57911],{},[198,57906,57907],{},"Using tabs in one place",[198,57909,57910],{},"Using spaces in another place",[198,57912,57913],{},"Using 2 spaces on one line and 4 on another",[14,57915,57916],{},"Even if code looks aligned on your screen, Python may still see the indentation as different.",[31,57918,57920],{"id":57919},"how-indentation-changes-code-meaning","How indentation changes code meaning",[14,57922,57923],{},"Lines at the same indentation level belong to the same block.",[14,57925,57926],{},"A line indented further is nested inside another block.",[14,57928,57929],{},"A line moved back to the left ends the current block.",[14,57931,57932],{},"Here is an example:",[36,57934,57936],{"className":38,"code":57935,"language":40,"meta":41,"style":41},"for i in range(2):\n    print(\"Outer loop\")\n    if i == 1:\n        print(\"Inner block\")\n\nprint(\"Finished\")\n",[18,57937,57938,57954,57969,57981,57996,58000],{"__ignoreMap":41},[45,57939,57940,57942,57944,57946,57948,57950,57952],{"class":47,"line":48},[45,57941,7895],{"class":73},[45,57943,36273],{"class":51},[45,57945,8623],{"class":73},[45,57947,10451],{"class":92},[45,57949,96],{"class":85},[45,57951,9367],{"class":59},[45,57953,2464],{"class":85},[45,57955,57956,57958,57960,57962,57965,57967],{"class":47,"line":63},[45,57957,93],{"class":92},[45,57959,96],{"class":85},[45,57961,100],{"class":99},[45,57963,57964],{"class":103},"Outer loop",[45,57966,100],{"class":99},[45,57968,109],{"class":85},[45,57970,57971,57973,57975,57977,57979],{"class":47,"line":70},[45,57972,1913],{"class":73},[45,57974,36273],{"class":51},[45,57976,1129],{"class":55},[45,57978,9030],{"class":59},[45,57980,86],{"class":85},[45,57982,57983,57985,57987,57989,57992,57994],{"class":47,"line":89},[45,57984,1923],{"class":92},[45,57986,96],{"class":85},[45,57988,100],{"class":99},[45,57990,57991],{"class":103},"Inner block",[45,57993,100],{"class":99},[45,57995,109],{"class":85},[45,57997,57998],{"class":47,"line":112},[45,57999,67],{"emptyLinePlaceholder":66},[45,58001,58002,58004,58006,58008,58011,58013],{"class":47,"line":126},[45,58003,2065],{"class":92},[45,58005,96],{"class":85},[45,58007,100],{"class":99},[45,58009,58010],{"class":103},"Finished",[45,58012,100],{"class":99},[45,58014,109],{"class":85},[712,58016,58018],{"id":58017},"how-python-reads-this","How Python reads this",[195,58020,58021,58029,58037,58048],{},[198,58022,58023,58026,58027,27566],{},[18,58024,58025],{},"print(\"Outer loop\")"," is inside the ",[18,58028,7895],{},[198,58030,58031,58034,58035,27566],{},[18,58032,58033],{},"if i == 1:"," is also inside the ",[18,58036,7895],{},[198,58038,58039,58042,58043,58045,58046,55438],{},[18,58040,58041],{},"print(\"Inner block\")"," is inside both the ",[18,58044,7895],{}," loop and the ",[18,58047,20],{},[198,58049,58050,58053],{},[18,58051,58052],{},"print(\"Finished\")"," is outside all blocks",[14,58055,177],{},[36,58057,58059],{"className":38,"code":58058,"language":40,"meta":41,"style":41},"Outer loop\nOuter loop\nInner block\nFinished\n",[18,58060,58061,58066,58070,58075],{"__ignoreMap":41},[45,58062,58063],{"class":47,"line":48},[45,58064,58065],{"class":51},"Outer loop\n",[45,58067,58068],{"class":47,"line":63},[45,58069,58065],{"class":51},[45,58071,58072],{"class":47,"line":70},[45,58073,58074],{"class":51},"Inner block\n",[45,58076,58077],{"class":47,"line":89},[45,58078,58079],{"class":51},"Finished\n",[14,58081,58082],{},"This nesting is a basic part of Python program structure.",[31,58084,58086],{"id":58085},"common-indentation-errors-beginners-see","Common indentation errors beginners see",[14,58088,58089],{},"Beginners often see these errors:",[195,58091,58092,58096,58101],{},[198,58093,58094],{},[18,58095,1614],{},[198,58097,58098],{},[18,58099,58100],{},"IndentationError: unexpected indent",[198,58102,58103],{},[18,58104,58105],{},"unindent does not match any outer indentation level",[14,58107,58108],{},"These usually happen:",[195,58110,58111,58114,58117],{},[198,58112,58113],{},"after a line ending with a colon",[198,58115,58116],{},"when tabs and spaces are mixed",[198,58118,58119],{},"when one line is indented more or less than Python expects",[14,58121,58122],{},"Example that causes an error:",[36,58124,58126],{"className":38,"code":58125,"language":40,"meta":41,"style":41},"if True:\nprint(\"Hello\")\n",[18,58127,58128,58136],{"__ignoreMap":41},[45,58129,58130,58132,58134],{"class":47,"line":48},[45,58131,20],{"class":73},[45,58133,23323],{"class":481},[45,58135,86],{"class":85},[45,58137,58138,58140,58142,58144,58146,58148],{"class":47,"line":63},[45,58139,2065],{"class":92},[45,58141,96],{"class":85},[45,58143,100],{"class":99},[45,58145,3226],{"class":103},[45,58147,100],{"class":99},[45,58149,109],{"class":85},[14,58151,58152,58153,58156],{},"This raises an indentation error because the line after ",[18,58154,58155],{},"if True:"," must be indented.",[14,58158,6003],{},[36,58160,58162],{"className":38,"code":58161,"language":40,"meta":41,"style":41},"if True:\n    print(\"Hello\")\n",[18,58163,58164,58172],{"__ignoreMap":41},[45,58165,58166,58168,58170],{"class":47,"line":48},[45,58167,20],{"class":73},[45,58169,23323],{"class":481},[45,58171,86],{"class":85},[45,58173,58174,58176,58178,58180,58182,58184],{"class":47,"line":63},[45,58175,93],{"class":92},[45,58177,96],{"class":85},[45,58179,100],{"class":99},[45,58181,3226],{"class":103},[45,58183,100],{"class":99},[45,58185,109],{"class":85},[14,58187,40002],{},[36,58189,58191],{"className":38,"code":58190,"language":40,"meta":41,"style":41},"if True:\n    print(\"Hello\")\n      print(\"World\")\n",[18,58192,58193,58201,58215],{"__ignoreMap":41},[45,58194,58195,58197,58199],{"class":47,"line":48},[45,58196,20],{"class":73},[45,58198,23323],{"class":481},[45,58200,86],{"class":85},[45,58202,58203,58205,58207,58209,58211,58213],{"class":47,"line":63},[45,58204,93],{"class":92},[45,58206,96],{"class":85},[45,58208,100],{"class":99},[45,58210,3226],{"class":103},[45,58212,100],{"class":99},[45,58214,109],{"class":85},[45,58216,58217,58220,58222,58224,58226,58228],{"class":47,"line":70},[45,58218,58219],{"class":92},"      print",[45,58221,96],{"class":85},[45,58223,100],{"class":99},[45,58225,48789],{"class":103},[45,58227,100],{"class":99},[45,58229,109],{"class":85},[14,58231,58232,58233,58235],{},"This can raise an indentation error because the second ",[18,58234,15833],{}," line has a different indentation level that does not match the block structure.",[14,58237,58238],{},"If you are trying to fix a real error message, these pages can help:",[195,58240,58241,58245,58250],{},[198,58242,58243],{},[236,58244,1614],{"href":1613},[198,58246,58247],{},[236,58248,58100],{"href":58249},"\u002Ferrors\u002Findentationerror-unexpected-indent-fix\u002F",[198,58251,58252],{},[236,58253,58255],{"href":58254},"\u002Ferrors\u002Findentationerror-in-python-causes-and-fixes\u002F","IndentationError in Python: causes and fixes",[31,58257,58259],{"id":58258},"how-to-avoid-indentation-problems","How to avoid indentation problems",[14,58261,58262],{},"You can prevent most indentation problems with a few habits:",[195,58264,58265,58268,58271,58274,58277],{},[198,58266,58267],{},"Use a code editor instead of a plain text editor",[198,58269,58270],{},"Turn on visible whitespace if your editor supports it",[198,58272,58273],{},"Use auto-formatting or reindent tools",[198,58275,58276],{},"Check the line after statements ending with a colon",[198,58278,58279],{},"If an error appears, compare the spaces on nearby lines",[14,58281,58282],{},"It also helps to type code yourself when learning, instead of only copy-pasting it.",[14,58284,58285],{},"If you do paste code from another source, check whether the indentation stayed consistent.",[31,58287,5903],{"id":5902},[14,58289,58290],{},"These are the most common causes of indentation problems:",[195,58292,58293,58296,58299,58302,58305],{},[198,58294,58295],{},"Forgetting to indent after a colon",[198,58297,58298],{},"Mixing tabs and spaces",[198,58300,58301],{},"Indenting a line that should not be indented",[198,58303,58304],{},"Using different numbers of spaces in the same block",[198,58306,58307],{},"Copy-pasting code from different editors",[14,58309,58310],{},"For example, this is wrong because the second line should not be indented:",[36,58312,58314],{"className":38,"code":58313,"language":40,"meta":41,"style":41},"print(\"Start\")\n    print(\"Wrong indent\")\n",[18,58315,58316,58330],{"__ignoreMap":41},[45,58317,58318,58320,58322,58324,58326,58328],{"class":47,"line":48},[45,58319,2065],{"class":92},[45,58321,96],{"class":85},[45,58323,100],{"class":99},[45,58325,13149],{"class":103},[45,58327,100],{"class":99},[45,58329,109],{"class":85},[45,58331,58332,58334,58336,58338,58341,58343],{"class":47,"line":63},[45,58333,93],{"class":92},[45,58335,96],{"class":85},[45,58337,100],{"class":99},[45,58339,58340],{"class":103},"Wrong indent",[45,58342,100],{"class":99},[45,58344,109],{"class":85},[14,58346,58347],{},"And this is wrong because the block uses inconsistent spacing:",[36,58349,58351],{"className":38,"code":58350,"language":40,"meta":41,"style":41},"if True:\n    print(\"Line 1\")\n  print(\"Line 2\")\n",[18,58352,58353,58361,58376],{"__ignoreMap":41},[45,58354,58355,58357,58359],{"class":47,"line":48},[45,58356,20],{"class":73},[45,58358,23323],{"class":481},[45,58360,86],{"class":85},[45,58362,58363,58365,58367,58369,58372,58374],{"class":47,"line":63},[45,58364,93],{"class":92},[45,58366,96],{"class":85},[45,58368,100],{"class":99},[45,58370,58371],{"class":103},"Line 1",[45,58373,100],{"class":99},[45,58375,109],{"class":85},[45,58377,58378,58381,58383,58385,58388,58390],{"class":47,"line":70},[45,58379,58380],{"class":92},"  print",[45,58382,96],{"class":85},[45,58384,100],{"class":99},[45,58386,58387],{"class":103},"Line 2",[45,58389,100],{"class":99},[45,58391,109],{"class":85},[14,58393,58394],{},"A good debugging habit is to look at the lines just above and below the error location.",[31,58396,58398],{"id":58397},"useful-commands-for-debugging","Useful commands for debugging",[14,58400,58401],{},"Run your file normally:",[36,58403,58405],{"className":15652,"code":58404,"language":15654,"meta":41,"style":41},"python your_file.py\n",[18,58406,58407],{"__ignoreMap":41},[45,58408,58409,58411],{"class":47,"line":48},[45,58410,40],{"class":2444},[45,58412,58413],{"class":103}," your_file.py\n",[14,58415,58416,58417,58420],{},"You can also use Python's ",[18,58418,58419],{},"tabnanny"," tool to check indentation problems:",[36,58422,58424],{"className":15652,"code":58423,"language":15654,"meta":41,"style":41},"python -m tabnanny your_file.py\n",[18,58425,58426],{"__ignoreMap":41},[45,58427,58428,58430,58433,58436],{"class":47,"line":48},[45,58429,40],{"class":2444},[45,58431,58432],{"class":15663}," -m",[45,58434,58435],{"class":103}," tabnanny",[45,58437,58413],{"class":103},[14,58439,58440],{},"This can help detect inconsistent indentation, especially when tabs and spaces are mixed.",[31,58442,2281],{"id":2280},[712,58444,58446],{"id":58445},"how-many-spaces-should-i-use-for-indentation-in-python","How many spaces should I use for indentation in Python?",[14,58448,58449],{},"Use 4 spaces for each indentation level. This is the standard style and the easiest rule for beginners.",[712,58451,58453],{"id":58452},"can-i-use-tabs-instead-of-spaces-in-python","Can I use tabs instead of spaces in Python?",[14,58455,58456],{},"You can in some cases, but beginners should avoid tabs. Mixing tabs and spaces often causes indentation errors.",[712,58458,58460],{"id":58459},"why-does-python-care-about-indentation","Why does Python care about indentation?",[14,58462,58463,58464,58466],{},"Python uses indentation to define blocks of code, such as the body of an ",[18,58465,20],{}," statement or loop.",[712,58468,58470],{"id":58469},"what-happens-if-indentation-is-wrong","What happens if indentation is wrong?",[14,58472,58473,58474,58476],{},"Python may raise an ",[18,58475,46140],{},", or the code may run with different logic than you expected.",[31,58478,2342],{"id":2341},[195,58480,58481,58485,58489,58493,58497,58501,58505],{},[198,58482,58483],{},[236,58484,40356],{"href":39615},[198,58486,58487],{},[236,58488,549],{"href":548},[198,58490,58491],{},[236,58492,23611],{"href":23610},[198,58494,58495],{},[236,58496,5879],{"href":4579},[198,58498,58499],{},[236,58500,1614],{"href":1613},[198,58502,58503],{},[236,58504,58100],{"href":58249},[198,58506,58507],{},[236,58508,58255],{"href":58254},[2367,58510,58511],{},"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 .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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .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 .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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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":41,"searchDepth":63,"depth":63,"links":58513},[58514,58515,58518,58519,58520,58521,58524,58525,58526,58527,58528,58534],{"id":33,"depth":63,"text":34},{"id":57384,"depth":63,"text":57385,"children":58516},[58517],{"id":57479,"depth":70,"text":57480},{"id":57538,"depth":63,"text":57539},{"id":57724,"depth":63,"text":57725},{"id":57834,"depth":63,"text":57835},{"id":57919,"depth":63,"text":57920,"children":58522},[58523],{"id":58017,"depth":70,"text":58018},{"id":58085,"depth":63,"text":58086},{"id":58258,"depth":63,"text":58259},{"id":5902,"depth":63,"text":5903},{"id":58397,"depth":63,"text":58398},{"id":2280,"depth":63,"text":2281,"children":58529},[58530,58531,58532,58533],{"id":58445,"depth":70,"text":58446},{"id":58452,"depth":70,"text":58453},{"id":58459,"depth":70,"text":58460},{"id":58469,"depth":70,"text":58470},{"id":2341,"depth":63,"text":2342},"Master python indentation rules and why they matter in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-indentation-rules-and-why-they-matter",{"title":57295,"description":58535},"learn\u002Fpython-indentation-rules-and-why-they-matter","rzvKaMBhlmm40zum1GVDT2TVXfZMjBMu74EjUoUBuU4",{"id":58542,"title":58543,"body":58544,"description":59912,"extension":2399,"meta":59913,"navigation":66,"path":59914,"seo":59915,"stem":59916,"__hash__":59917},"content\u002Flearn\u002Fpython-keywords-explained-beginner-guide.md","Python Keywords Explained (Beginner Guide)",{"type":7,"value":58545,"toc":59873},[58546,58549,58552,58555,58560,58564,58571,58647,58651,58673,58676,58680,58683,58685,58703,58706,58740,58746,58760,58766,58770,58773,58775,58797,58800,58807,58817,58821,58824,58828,58842,58847,58851,58874,58879,58883,58897,58902,58906,58912,58919,58923,58949,58952,58956,58975,58978,58982,58985,58989,58992,58994,59012,59015,59047,59052,59056,59059,59061,59076,59079,59116,59118,59133,59143,59147,59150,59254,59258,59285,59289,59327,59331,59349,59352,59357,59361,59367,59396,59399,59402,59462,59466,59478,59481,59484,59528,59532,59534,59538,59541,59559,59562,59580,59584,59592,59595,59598,59606,59616,59619,59621,59644,59647,59651,59654,59656,59699,59708,59711,59722,59726,59736,59739,59750,59752,59755,59758,59781,59784,59786,59790,59799,59803,59806,59810,59815,59819,59827,59831,59834,59836,59870],[10,58547,58543],{"id":58548},"python-keywords-explained-beginner-guide",[14,58550,58551],{},"Python keywords are reserved words that have a special meaning in the language.",[14,58553,58554],{},"They matter because Python uses them to understand the structure of your code. If you use a keyword in the wrong place, you will usually get a syntax error. As a beginner, learning to recognize keywords will make Python code much easier to read.",[14,58556,2854,58557,58559],{},[2421,58558,2857],{}," need to memorize every keyword right away. Start by understanding what keywords are, what they do, and how to avoid using them as names.",[31,58561,58563],{"id":58562},"quick-way-to-check-python-keywords","Quick way to check Python keywords",[14,58565,58566,58567,58570],{},"Python includes a built-in module named ",[18,58568,58569],{},"keyword"," that can show you the current keyword list and check whether a word is reserved.",[36,58572,58574],{"className":38,"code":58573,"language":40,"meta":41,"style":41},"import keyword\n\nprint(keyword.kwlist)\nprint(keyword.iskeyword(\"if\"))\nprint(keyword.iskeyword(\"name\"))\n",[18,58575,58576,58583,58587,58602,58625],{"__ignoreMap":41},[45,58577,58578,58580],{"class":47,"line":48},[45,58579,14870],{"class":73},[45,58581,58582],{"class":51}," keyword\n",[45,58584,58585],{"class":47,"line":63},[45,58586,67],{"emptyLinePlaceholder":66},[45,58588,58589,58591,58593,58595,58597,58600],{"class":47,"line":70},[45,58590,2065],{"class":92},[45,58592,96],{"class":85},[45,58594,58569],{"class":2070},[45,58596,240],{"class":85},[45,58598,58599],{"class":3095},"kwlist",[45,58601,109],{"class":85},[45,58603,58604,58606,58608,58610,58612,58615,58617,58619,58621,58623],{"class":47,"line":89},[45,58605,2065],{"class":92},[45,58607,96],{"class":85},[45,58609,58569],{"class":2070},[45,58611,240],{"class":85},[45,58613,58614],{"class":2070},"iskeyword",[45,58616,96],{"class":85},[45,58618,100],{"class":99},[45,58620,20],{"class":103},[45,58622,100],{"class":99},[45,58624,4348],{"class":85},[45,58626,58627,58629,58631,58633,58635,58637,58639,58641,58643,58645],{"class":47,"line":112},[45,58628,2065],{"class":92},[45,58630,96],{"class":85},[45,58632,58569],{"class":2070},[45,58634,240],{"class":85},[45,58636,58614],{"class":2070},[45,58638,96],{"class":85},[45,58640,100],{"class":99},[45,58642,3096],{"class":103},[45,58644,100],{"class":99},[45,58646,4348],{"class":85},[14,58648,58649],{},[2421,58650,9061],{},[195,58652,58653,58659,58666],{},[198,58654,58655,58658],{},[18,58656,58657],{},"keyword.kwlist"," shows all Python keywords",[198,58660,58661,34444,58664],{},[18,58662,58663],{},"keyword.iskeyword(\"if\")",[18,58665,229],{},[198,58667,58668,34444,58671],{},[18,58669,58670],{},"keyword.iskeyword(\"name\")",[18,58672,233],{},[14,58674,58675],{},"This is useful when you are not sure whether a word is allowed as a variable name.",[31,58677,58679],{"id":58678},"what-python-keywords-are","What Python keywords are",[14,58681,58682],{},"Keywords are reserved words with special meaning in Python.",[14,58684,2727],{},[195,58686,58687,58693,58698],{},[198,58688,28909,58689,58692],{},[2421,58690,58691],{},"cannot"," use them as variable names",[198,58694,28909,58695,58697],{},[2421,58696,58691],{}," use them as function names",[198,58699,28909,58700,58702],{},[2421,58701,58691],{}," use them as class names",[14,58704,58705],{},"Examples of Python keywords include:",[195,58707,58708,58712,58716,58720,58724,58728,58732,58736],{},[198,58709,58710],{},[18,58711,20],{},[198,58713,58714],{},[18,58715,28],{},[198,58717,58718],{},[18,58719,7895],{},[198,58721,58722],{},[18,58723,23529],{},[198,58725,58726],{},[18,58727,2757],{},[198,58729,58730],{},[18,58731,2441],{},[198,58733,58734],{},[18,58735,3461],{},[198,58737,58738],{},[18,58739,14870],{},[14,58741,58742,58743,58745],{},"For example, this code is invalid because ",[18,58744,7895],{}," is a keyword:",[36,58747,58749],{"className":38,"code":58748,"language":40,"meta":41,"style":41},"for = 10\n",[18,58750,58751],{"__ignoreMap":41},[45,58752,58753,58755,58758],{"class":47,"line":48},[45,58754,7895],{"class":73},[45,58756,58757],{"class":51}," = ",[45,58759,13262],{"class":59},[14,58761,58762,58763,58765],{},"Python will raise an error because ",[18,58764,7895],{}," already has a special meaning in the language.",[31,58767,58769],{"id":58768},"why-keywords-matter","Why keywords matter",[14,58771,58772],{},"Keywords are important because Python uses them to understand how your program is written.",[14,58774,2115],{},[195,58776,58777,58782,58787,58792],{},[198,58778,58779,58781],{},[18,58780,20],{}," starts a condition",[198,58783,58784,58786],{},[18,58785,7895],{}," starts a loop",[198,58788,58789,58791],{},[18,58790,2757],{}," starts a function",[198,58793,58794,58796],{},[18,58795,3461],{}," sends a value back from a function",[14,58798,58799],{},"If Python sees one of these words, it treats it as part of the language syntax, not as a normal name.",[14,58801,58802,58803,240],{},"Using a keyword incorrectly often causes a syntax error, such as ",[236,58804,58805],{"href":18360},[18,58806,18361],{},[14,58808,58809,58810,21,58812,15994,58814,58816],{},"Recognizing keywords also helps you read code faster. When you see ",[18,58811,20],{},[18,58813,7895],{},[18,58815,2757],{},", you can immediately tell what kind of code block you are looking at.",[31,58818,58820],{"id":58819},"common-python-keywords-beginners-should-know-first","Common Python keywords beginners should know first",[14,58822,58823],{},"You do not need the full keyword list on day one. These are the most useful ones to learn first.",[712,58825,58827],{"id":58826},"condition-keywords","Condition keywords",[195,58829,58830,58834,58838],{},[198,58831,58832],{},[18,58833,20],{},[198,58835,58836],{},[18,58837,24],{},[198,58839,58840],{},[18,58841,28],{},[14,58843,58844,58845,240],{},"These are used to make decisions in code. If you are new to conditions, see ",[236,58846,549],{"href":548},[712,58848,58850],{"id":58849},"loop-keywords","Loop keywords",[195,58852,58853,58857,58861,58865,58869],{},[198,58854,58855],{},[18,58856,7895],{},[198,58858,58859],{},[18,58860,23529],{},[198,58862,58863],{},[18,58864,35658],{},[198,58866,58867],{},[18,58868,35661],{},[198,58870,58871],{},[18,58872,58873],{},"pass",[14,58875,58876,58877,240],{},"These are used for repetition and loop control. To go deeper, read ",[236,58878,23611],{"href":23610},[712,58880,58882],{"id":58881},"function-keywords","Function keywords",[195,58884,58885,58889,58893],{},[198,58886,58887],{},[18,58888,2757],{},[198,58890,58891],{},[18,58892,3461],{},[198,58894,58895],{},[18,58896,25048],{},[14,58898,58899,58900,240],{},"These are related to functions. If you want the basics first, see ",[236,58901,5879],{"href":4579},[712,58903,58905],{"id":58904},"import-keyword","Import keyword",[195,58907,58908],{},[198,58909,58910],{},[18,58911,14870],{},[14,58913,58914,58915,240],{},"This is used to bring code in from modules. For more on that, see ",[236,58916,58918],{"href":58917},"\u002Flearn\u002Fhow-import-works-in-python\u002F","how import works in Python",[712,58920,58922],{"id":58921},"boolean-and-logic-keywords","Boolean and logic keywords",[195,58924,58925,58929,58933,58937,58941,58945],{},[198,58926,58927],{},[18,58928,229],{},[198,58930,58931],{},[18,58932,233],{},[198,58934,58935],{},[18,58936,5997],{},[198,58938,58939],{},[18,58940,34437],{},[198,58942,58943],{},[18,58944,34506],{},[198,58946,58947],{},[18,58948,2857],{},[14,58950,58951],{},"These are used in conditions and logical expressions.",[712,58953,58955],{"id":58954},"exception-handling-keywords","Exception handling keywords",[195,58957,58958,58962,58966,58970],{},[198,58959,58960],{},[18,58961,23272],{},[198,58963,58964],{},[18,58965,23275],{},[198,58967,58968],{},[18,58969,57769],{},[198,58971,58972],{},[18,58973,58974],{},"raise",[14,58976,58977],{},"These are used when handling errors in your program.",[31,58979,58981],{"id":58980},"how-keywords-are-different-from-built-in-functions","How keywords are different from built-in functions",[14,58983,58984],{},"This is a very common beginner confusion.",[712,58986,58988],{"id":58987},"keywords","Keywords",[14,58990,58991],{},"Keywords are part of Python syntax.",[14,58993,40802],{},[195,58995,58996,59000,59004,59008],{},[198,58997,58998],{},[18,58999,20],{},[198,59001,59002],{},[18,59003,7895],{},[198,59005,59006],{},[18,59007,2757],{},[198,59009,59010],{},[18,59011,3461],{},[14,59013,59014],{},"You do not call a keyword like a function. For example, this is valid syntax:",[36,59016,59018],{"className":38,"code":59017,"language":40,"meta":41,"style":41},"if 5 > 3:\n    print(\"Yes\")\n",[18,59019,59020,59032],{"__ignoreMap":41},[45,59021,59022,59024,59026,59028,59030],{"class":47,"line":48},[45,59023,20],{"class":73},[45,59025,1222],{"class":59},[45,59027,33591],{"class":55},[45,59029,777],{"class":59},[45,59031,86],{"class":85},[45,59033,59034,59036,59038,59040,59043,59045],{"class":47,"line":63},[45,59035,93],{"class":92},[45,59037,96],{"class":85},[45,59039,100],{"class":99},[45,59041,59042],{"class":103},"Yes",[45,59044,100],{"class":99},[45,59046,109],{"class":85},[14,59048,3444,59049,59051],{},[18,59050,20],{}," is a keyword. It does not use parentheses as a function call.",[712,59053,59055],{"id":59054},"built-in-functions","Built-in functions",[14,59057,59058],{},"Built-in functions are names you can call.",[14,59060,40802],{},[195,59062,59063,59067,59072],{},[198,59064,59065],{},[18,59066,15833],{},[198,59068,59069],{},[18,59070,59071],{},"len()",[198,59073,59074],{},[18,59075,40437],{},[14,59077,59078],{},"These use parentheses because you are calling them as functions:",[36,59080,59082],{"className":38,"code":59081,"language":40,"meta":41,"style":41},"print(\"Hello\")\nprint(len(\"Python\"))\n",[18,59083,59084,59098],{"__ignoreMap":41},[45,59085,59086,59088,59090,59092,59094,59096],{"class":47,"line":48},[45,59087,2065],{"class":92},[45,59089,96],{"class":85},[45,59091,100],{"class":99},[45,59093,3226],{"class":103},[45,59095,100],{"class":99},[45,59097,109],{"class":85},[45,59099,59100,59102,59104,59106,59108,59110,59112,59114],{"class":47,"line":63},[45,59101,2065],{"class":92},[45,59103,96],{"class":85},[45,59105,28716],{"class":92},[45,59107,96],{"class":85},[45,59109,100],{"class":99},[45,59111,16791],{"class":103},[45,59113,100],{"class":99},[45,59115,4348],{"class":85},[14,59117,15572],{},[195,59119,59120,59125,59129],{},[198,59121,59122,59124],{},[18,59123,20],{}," is a keyword",[198,59126,59127,53171],{},[18,59128,15833],{},[198,59130,59131,53171],{},[18,59132,59071],{},[14,59134,59135,59136,1316,59139,240],{},"If you want to learn more about these, see ",[236,59137,59138],{"href":18110},"Python print() function explained",[236,59140,59142],{"href":59141},"\u002Freference\u002Fpython-len-function-explained\u002F","Python len() function explained",[31,59144,59146],{"id":59145},"example-keywords-used-in-real-code","Example: keywords used in real code",[14,59148,59149],{},"Here is a short program that uses several common keywords:",[36,59151,59153],{"className":38,"code":59152,"language":40,"meta":41,"style":41},"def get_message(number):\n    if number > 0:\n        return \"Positive\"\n    else:\n        return \"Zero or negative\"\n\n\nfor value in [3, 0, -2]:\n    print(get_message(value))\n",[18,59154,59155,59168,59180,59190,59196,59207,59211,59215,59239],{"__ignoreMap":41},[45,59156,59157,59159,59162,59164,59166],{"class":47,"line":48},[45,59158,2757],{"class":2440},[45,59160,59161],{"class":2455}," get_message",[45,59163,96],{"class":85},[45,59165,13546],{"class":3082},[45,59167,2464],{"class":85},[45,59169,59170,59172,59174,59176,59178],{"class":47,"line":63},[45,59171,1913],{"class":73},[45,59173,744],{"class":51},[45,59175,272],{"class":55},[45,59177,749],{"class":59},[45,59179,86],{"class":85},[45,59181,59182,59184,59186,59188],{"class":47,"line":70},[45,59183,3550],{"class":73},[45,59185,1565],{"class":99},[45,59187,762],{"class":103},[45,59189,1571],{"class":99},[45,59191,59192,59194],{"class":47,"line":89},[45,59193,1939],{"class":73},[45,59195,86],{"class":85},[45,59197,59198,59200,59202,59205],{"class":47,"line":112},[45,59199,3550],{"class":73},[45,59201,1565],{"class":99},[45,59203,59204],{"class":103},"Zero or negative",[45,59206,1571],{"class":99},[45,59208,59209],{"class":47,"line":126},[45,59210,67],{"emptyLinePlaceholder":66},[45,59212,59213],{"class":47,"line":142},[45,59214,67],{"emptyLinePlaceholder":66},[45,59216,59217,59219,59221,59223,59225,59227,59229,59231,59233,59235,59237],{"class":47,"line":149},[45,59218,7895],{"class":73},[45,59220,1492],{"class":51},[45,59222,8623],{"class":73},[45,59224,8575],{"class":85},[45,59226,11184],{"class":59},[45,59228,3079],{"class":85},[45,59230,749],{"class":59},[45,59232,3079],{"class":85},[45,59234,28089],{"class":55},[45,59236,9367],{"class":59},[45,59238,49606],{"class":85},[45,59240,59241,59243,59245,59248,59250,59252],{"class":47,"line":679},[45,59242,93],{"class":92},[45,59244,96],{"class":85},[45,59246,59247],{"class":2070},"get_message",[45,59249,96],{"class":85},[45,59251,12722],{"class":2070},[45,59253,4348],{"class":85},[14,59255,59256],{},[2421,59257,177],{},[36,59259,59261],{"className":38,"code":59260,"language":40,"meta":41,"style":41},"Positive\nZero or negative\nZero or negative\n",[18,59262,59263,59267,59277],{"__ignoreMap":41},[45,59264,59265],{"class":47,"line":48},[45,59266,820],{"class":51},[45,59268,59269,59272,59274],{"class":47,"line":63},[45,59270,59271],{"class":51},"Zero ",[45,59273,34506],{"class":55},[45,59275,59276],{"class":51}," negative\n",[45,59278,59279,59281,59283],{"class":47,"line":70},[45,59280,59271],{"class":51},[45,59282,34506],{"class":55},[45,59284,59276],{"class":51},[712,59286,59288],{"id":59287},"keywords-used-here","Keywords used here",[195,59290,59291,59296,59301,59306,59314,59319],{},[198,59292,59293,59295],{},[18,59294,2757],{}," — starts the function definition",[198,59297,59298,59300],{},[18,59299,20],{}," — checks a condition",[198,59302,59303,59305],{},[18,59304,3461],{}," — sends a value back from the function",[198,59307,59308,59310,59311,59313],{},[18,59309,28],{}," — runs when the ",[18,59312,20],{}," condition is false",[198,59315,59316,59318],{},[18,59317,7895],{}," — loops through the list",[198,59320,59321,59323,59324,59326],{},[18,59322,8623],{}," — used as part of the ",[18,59325,7895],{}," loop syntax",[712,59328,59330],{"id":59329},"what-the-code-does","What the code does",[195,59332,59333,59338,59341,59344],{},[198,59334,59335,59336],{},"It defines a function called ",[18,59337,59247],{},[198,59339,59340],{},"The function checks whether a number is greater than zero",[198,59342,59343],{},"It returns a string based on the result",[198,59345,2419,59346,59348],{},[18,59347,7895],{}," loop then calls the function for each number in a list",[14,59350,59351],{},"This is a good example of how keywords help form the structure of Python code.",[14,59353,59354,59355,17902],{},"If you want help understanding the bigger picture, ",[236,59356,40356],{"href":39615},[31,59358,59360],{"id":59359},"how-to-see-the-full-keyword-list-in-python","How to see the full keyword list in Python",[14,59362,59363,59364,59366],{},"You can use the ",[18,59365,58569],{}," module from Python’s standard library.",[36,59368,59370],{"className":38,"code":59369,"language":40,"meta":41,"style":41},"import keyword\n\nprint(keyword.kwlist)\n",[18,59371,59372,59378,59382],{"__ignoreMap":41},[45,59373,59374,59376],{"class":47,"line":48},[45,59375,14870],{"class":73},[45,59377,58582],{"class":51},[45,59379,59380],{"class":47,"line":63},[45,59381,67],{"emptyLinePlaceholder":66},[45,59383,59384,59386,59388,59390,59392,59394],{"class":47,"line":70},[45,59385,2065],{"class":92},[45,59387,96],{"class":85},[45,59389,58569],{"class":2070},[45,59391,240],{"class":85},[45,59393,58599],{"class":3095},[45,59395,109],{"class":85},[14,59397,59398],{},"This prints the full list of keywords for the version of Python you are using.",[14,59400,59401],{},"You can also check one word at a time:",[36,59403,59405],{"className":38,"code":59404,"language":40,"meta":41,"style":41},"import keyword\n\nprint(keyword.iskeyword(\"for\"))\nprint(keyword.iskeyword(\"my_variable\"))\n",[18,59406,59407,59413,59417,59439],{"__ignoreMap":41},[45,59408,59409,59411],{"class":47,"line":48},[45,59410,14870],{"class":73},[45,59412,58582],{"class":51},[45,59414,59415],{"class":47,"line":63},[45,59416,67],{"emptyLinePlaceholder":66},[45,59418,59419,59421,59423,59425,59427,59429,59431,59433,59435,59437],{"class":47,"line":70},[45,59420,2065],{"class":92},[45,59422,96],{"class":85},[45,59424,58569],{"class":2070},[45,59426,240],{"class":85},[45,59428,58614],{"class":2070},[45,59430,96],{"class":85},[45,59432,100],{"class":99},[45,59434,7895],{"class":103},[45,59436,100],{"class":99},[45,59438,4348],{"class":85},[45,59440,59441,59443,59445,59447,59449,59451,59453,59455,59458,59460],{"class":47,"line":89},[45,59442,2065],{"class":92},[45,59444,96],{"class":85},[45,59446,58569],{"class":2070},[45,59448,240],{"class":85},[45,59450,58614],{"class":2070},[45,59452,96],{"class":85},[45,59454,100],{"class":99},[45,59456,59457],{"class":103},"my_variable",[45,59459,100],{"class":99},[45,59461,4348],{"class":85},[14,59463,59464],{},[2421,59465,13456],{},[36,59467,59468],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,59469,59470,59474],{"__ignoreMap":41},[45,59471,59472],{"class":47,"line":48},[45,59473,2270],{"class":481},[45,59475,59476],{"class":47,"line":63},[45,59477,2265],{"class":481},[14,59479,59480],{},"This is useful because keyword lists can change slightly between Python versions.",[14,59482,59483],{},"If you want to run these checks quickly from the command line, you can use:",[36,59485,59487],{"className":15652,"code":59486,"language":15654,"meta":41,"style":41},"python -c \"import keyword; print(keyword.kwlist)\"\npython -c \"import keyword; print(keyword.iskeyword('for'))\"\npython -c \"import keyword; print(keyword.iskeyword('my_variable'))\"\n",[18,59488,59489,59502,59515],{"__ignoreMap":41},[45,59490,59491,59493,59495,59497,59500],{"class":47,"line":48},[45,59492,40],{"class":2444},[45,59494,15664],{"class":15663},[45,59496,1565],{"class":99},[45,59498,59499],{"class":103},"import keyword; print(keyword.kwlist)",[45,59501,1571],{"class":99},[45,59503,59504,59506,59508,59510,59513],{"class":47,"line":63},[45,59505,40],{"class":2444},[45,59507,15664],{"class":15663},[45,59509,1565],{"class":99},[45,59511,59512],{"class":103},"import keyword; print(keyword.iskeyword('for'))",[45,59514,1571],{"class":99},[45,59516,59517,59519,59521,59523,59526],{"class":47,"line":70},[45,59518,40],{"class":2444},[45,59520,15664],{"class":15663},[45,59522,1565],{"class":99},[45,59524,59525],{"class":103},"import keyword; print(keyword.iskeyword('my_variable'))",[45,59527,1571],{"class":99},[31,59529,59531],{"id":59530},"common-beginner-mistakes-with-keywords","Common beginner mistakes with keywords",[14,59533,15980],{},[712,59535,59537],{"id":59536},"trying-to-use-a-keyword-as-a-variable-name","Trying to use a keyword as a variable name",[14,59539,59540],{},"This will fail:",[36,59542,59544],{"className":38,"code":59543,"language":40,"meta":41,"style":41},"class = \"Beginner\"\n",[18,59545,59546],{"__ignoreMap":41},[45,59547,59548,59550,59552,59554,59557],{"class":47,"line":48},[45,59549,2441],{"class":2440},[45,59551,3099],{"class":55},[45,59553,1565],{"class":99},[45,59555,59556],{"class":103},"Beginner",[45,59558,1571],{"class":99},[14,59560,59561],{},"Use a different name instead:",[36,59563,59565],{"className":38,"code":59564,"language":40,"meta":41,"style":41},"class_name = \"Beginner\"\n",[18,59566,59567],{"__ignoreMap":41},[45,59568,59569,59572,59574,59576,59578],{"class":47,"line":48},[45,59570,59571],{"class":51},"class_name ",[45,59573,56],{"class":55},[45,59575,1565],{"class":99},[45,59577,59556],{"class":103},[45,59579,1571],{"class":99},[712,59581,59583],{"id":59582},"confusing-keywords-with-built-in-functions","Confusing keywords with built-in functions",[14,59585,59586,59587,1316,59589,59591],{},"Many beginners think ",[18,59588,2065],{},[18,59590,28716],{}," are keywords.",[14,59593,59594],{},"They are not. They are built-in functions.",[14,59596,59597],{},"This matters because:",[195,59599,59600,59603],{},[198,59601,59602],{},"functions are called with parentheses",[198,59604,59605],{},"keywords are part of syntax",[712,59607,2043,59609,21,59611,25,59613,59615],{"id":59608},"forgetting-that-true-false-and-none-are-special-names",[18,59610,229],{},[18,59612,233],{},[18,59614,5997],{}," are special names",[14,59617,59618],{},"These are not ordinary variable names. They have special meaning in Python.",[14,59620,2115],{},[36,59622,59624],{"className":38,"code":59623,"language":40,"meta":41,"style":41},"value = True\nempty = None\n",[18,59625,59626,59634],{"__ignoreMap":41},[45,59627,59628,59630,59632],{"class":47,"line":48},[45,59629,1476],{"class":51},[45,59631,56],{"class":55},[45,59633,482],{"class":481},[45,59635,59636,59639,59641],{"class":47,"line":63},[45,59637,59638],{"class":51},"empty ",[45,59640,56],{"class":55},[45,59642,59643],{"class":481}," None\n",[14,59645,59646],{},"These names are part of Python itself and should be used as intended.",[712,59648,59650],{"id":59649},"misspelling-a-keyword","Misspelling a keyword",[14,59652,59653],{},"Small spelling mistakes can break your code.",[14,59655,466],{},[36,59657,59659],{"className":38,"code":59658,"language":40,"meta":41,"style":41},"x = 10\n\nelf x > 5:\n    print(\"Big\")\n",[18,59660,59661,59669,59673,59684],{"__ignoreMap":41},[45,59662,59663,59665,59667],{"class":47,"line":48},[45,59664,1169],{"class":51},[45,59666,56],{"class":55},[45,59668,1174],{"class":59},[45,59670,59671],{"class":47,"line":63},[45,59672,67],{"emptyLinePlaceholder":66},[45,59674,59675,59678,59680,59682],{"class":47,"line":70},[45,59676,59677],{"class":51},"elf x ",[45,59679,272],{"class":55},[45,59681,1222],{"class":59},[45,59683,86],{"class":85},[45,59685,59686,59688,59690,59692,59695,59697],{"class":47,"line":89},[45,59687,93],{"class":92},[45,59689,96],{"class":85},[45,59691,100],{"class":99},[45,59693,59694],{"class":103},"Big",[45,59696,100],{"class":99},[45,59698,109],{"class":85},[14,59700,59701,59702,59705,59706,240],{},"This is invalid because ",[18,59703,59704],{},"elf"," is not the keyword ",[18,59707,24],{},[14,59709,59710],{},"Misspelled keywords may cause:",[195,59712,59713,59716,59719],{},[198,59714,59715],{},"a syntax error",[198,59717,59718],{},"a name error",[198,59720,59721],{},"confusing behavior if you do not notice the typo",[712,59723,59725],{"id":59724},"copying-code-without-understanding-reserved-words","Copying code without understanding reserved words",[14,59727,59728,59729,21,59731,15994,59733,59735],{},"It is easy to copy code that uses words like ",[18,59730,23272],{},[18,59732,3461],{},[18,59734,58873],{}," without knowing what they do.",[14,59737,59738],{},"A better approach is to pause and ask:",[195,59740,59741,59744,59747],{},[198,59742,59743],{},"Is this word a keyword?",[198,59745,59746],{},"What role does it play in the code?",[198,59748,59749],{},"Is it starting a block, returning a value, or controlling flow?",[31,59751,47394],{"id":47393},[14,59753,59754],{},"Once you can recognize Python keywords, the next step is learning how they work together in real syntax.",[14,59756,59757],{},"Good next topics are:",[195,59759,59760,59764,59768,59772,59776],{},[198,59761,59762],{},[236,59763,40356],{"href":39615},[198,59765,59766],{},[236,59767,549],{"href":548},[198,59769,59770],{},[236,59771,23611],{"href":23610},[198,59773,59774],{},[236,59775,5879],{"href":4579},[198,59777,59778],{},[236,59779,59780],{"href":58917},"How import works in Python",[14,59782,59783],{},"The goal is not just to spot keywords, but to use them correctly in simple Python programs.",[31,59785,2281],{"id":2280},[712,59787,59789],{"id":59788},"what-is-a-keyword-in-python","What is a keyword in Python?",[14,59791,59792,59793,21,59795,25,59797,240],{},"A keyword is a reserved word that Python uses as part of its syntax, such as ",[18,59794,20],{},[18,59796,7895],{},[18,59798,2757],{},[712,59800,59802],{"id":59801},"can-i-use-a-python-keyword-as-a-variable-name","Can I use a Python keyword as a variable name?",[14,59804,59805],{},"No. Keywords are reserved and will cause an error if you try to use them as names.",[712,59807,59809],{"id":59808},"is-print-a-keyword-in-python","Is print a keyword in Python?",[14,59811,2302,59812,59814],{},[18,59813,15833],{}," is a built-in function, not a keyword.",[712,59816,59818],{"id":59817},"how-do-i-see-all-python-keywords","How do I see all Python keywords?",[14,59820,42429,59821,59823,59824,240],{},[18,59822,58569],{}," module and run ",[18,59825,59826],{},"print(keyword.kwlist)",[712,59828,59830],{"id":59829},"do-python-keywords-change","Do Python keywords change?",[14,59832,59833],{},"Sometimes. The list can change slightly between Python versions.",[31,59835,2342],{"id":2341},[195,59837,59838,59842,59846,59850,59854,59858,59862,59866],{},[198,59839,59840],{},[236,59841,40356],{"href":39615},[198,59843,59844],{},[236,59845,549],{"href":548},[198,59847,59848],{},[236,59849,23611],{"href":23610},[198,59851,59852],{},[236,59853,5879],{"href":4579},[198,59855,59856],{},[236,59857,59780],{"href":58917},[198,59859,59860],{},[236,59861,59138],{"href":18110},[198,59863,59864],{},[236,59865,59142],{"href":59141},[198,59867,59868],{},[236,59869,18361],{"href":18360},[2367,59871,59872],{},"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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 .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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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":41,"searchDepth":63,"depth":63,"links":59874},[59875,59876,59877,59878,59886,59890,59894,59895,59903,59904,59911],{"id":58562,"depth":63,"text":58563},{"id":58678,"depth":63,"text":58679},{"id":58768,"depth":63,"text":58769},{"id":58819,"depth":63,"text":58820,"children":59879},[59880,59881,59882,59883,59884,59885],{"id":58826,"depth":70,"text":58827},{"id":58849,"depth":70,"text":58850},{"id":58881,"depth":70,"text":58882},{"id":58904,"depth":70,"text":58905},{"id":58921,"depth":70,"text":58922},{"id":58954,"depth":70,"text":58955},{"id":58980,"depth":63,"text":58981,"children":59887},[59888,59889],{"id":58987,"depth":70,"text":58988},{"id":59054,"depth":70,"text":59055},{"id":59145,"depth":63,"text":59146,"children":59891},[59892,59893],{"id":59287,"depth":70,"text":59288},{"id":59329,"depth":70,"text":59330},{"id":59359,"depth":63,"text":59360},{"id":59530,"depth":63,"text":59531,"children":59896},[59897,59898,59899,59901,59902],{"id":59536,"depth":70,"text":59537},{"id":59582,"depth":70,"text":59583},{"id":59608,"depth":70,"text":59900},"Forgetting that True, False, and None are special names",{"id":59649,"depth":70,"text":59650},{"id":59724,"depth":70,"text":59725},{"id":47393,"depth":63,"text":47394},{"id":2280,"depth":63,"text":2281,"children":59905},[59906,59907,59908,59909,59910],{"id":59788,"depth":70,"text":59789},{"id":59801,"depth":70,"text":59802},{"id":59808,"depth":70,"text":59809},{"id":59817,"depth":70,"text":59818},{"id":59829,"depth":70,"text":59830},{"id":2341,"depth":63,"text":2342},"Master python keywords explained beginner guide in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-keywords-explained-beginner-guide",{"title":58543,"description":59912},"learn\u002Fpython-keywords-explained-beginner-guide","KGyGCDxBZbEaLpefZSty4pWRFWWBp0MM3zT8DrCvkIU",{"id":59919,"title":59920,"body":59921,"description":62335,"extension":2399,"meta":62336,"navigation":66,"path":30255,"seo":62337,"stem":62338,"__hash__":62339},"content\u002Flearn\u002Fpython-lists-explained-beginner-guide.md","Python Lists Explained (Beginner Guide)",{"type":7,"value":59922,"toc":62289},[59923,59926,59929,59932,59935,60026,60028,60101,60104,60122,60126,60129,60132,60154,60156,60191,60193,60214,60221,60225,60228,60231,60245,60247,60290,60293,60297,60300,60304,60327,60329,60337,60341,60388,60392,60395,60437,60440,60444,60450,60452,60472,60474,60568,60570,60588,60592,60637,60643,60649,60653,60656,60725,60727,60760,60763,60768,60772,60775,60781,60842,60844,60877,60884,60891,60961,60963,61004,61010,61076,61078,61110,61116,61186,61188,61213,61219,61300,61302,61331,61337,61388,61390,61398,61404,61410,61487,61489,61501,61503,61508,61571,61573,61589,61592,61598,61676,61678,61704,61709,61713,61716,61719,61738,61740,61757,61759,61814,61816,61837,61845,61852,61856,61859,61862,61887,61890,62007,62014,62016,62019,62046,62049,62075,62078,62169,62172,62186,62188,62192,62195,62199,62202,62206,62211,62215,62223,62230,62238,62242,62245,62247,62286],[10,59924,59920],{"id":59925},"python-lists-explained-beginner-guide",[14,59927,59928],{},"A Python list is one of the most useful data types for beginners to learn.",[14,59930,59931],{},"A list lets you store multiple values in one variable, keep them in order, and change them later. This makes lists a good choice when you need to work with groups of related data such as names, numbers, scores, file paths, or user input.",[14,59933,59934],{},"This guide explains what a list is, how to create one, how to access and change items, and the most common things beginners do with lists.",[36,59936,59938],{"className":38,"code":59937,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\nprint(fruits)\nprint(fruits[0])\nfruits.append(\"grape\")\nprint(fruits)\n",[18,59939,59940,59973,59984,59998,60016],{"__ignoreMap":41},[45,59941,59942,59944,59946,59948,59950,59952,59954,59956,59958,59960,59962,59964,59966,59969,59971],{"class":47,"line":48},[45,59943,49394],{"class":51},[45,59945,56],{"class":55},[45,59947,8575],{"class":85},[45,59949,100],{"class":99},[45,59951,9443],{"class":103},[45,59953,100],{"class":99},[45,59955,3079],{"class":85},[45,59957,1565],{"class":99},[45,59959,9456],{"class":103},[45,59961,100],{"class":99},[45,59963,3079],{"class":85},[45,59965,1565],{"class":99},[45,59967,59968],{"class":103},"orange",[45,59970,100],{"class":99},[45,59972,8594],{"class":85},[45,59974,59975,59977,59979,59982],{"class":47,"line":63},[45,59976,2065],{"class":92},[45,59978,96],{"class":85},[45,59980,59981],{"class":2070},"fruits",[45,59983,109],{"class":85},[45,59985,59986,59988,59990,59992,59994,59996],{"class":47,"line":70},[45,59987,2065],{"class":92},[45,59989,96],{"class":85},[45,59991,59981],{"class":2070},[45,59993,9680],{"class":85},[45,59995,10482],{"class":59},[45,59997,21941],{"class":85},[45,59999,60000,60002,60004,60006,60008,60010,60012,60014],{"class":47,"line":89},[45,60001,59981],{"class":51},[45,60003,240],{"class":85},[45,60005,21025],{"class":2070},[45,60007,96],{"class":85},[45,60009,100],{"class":99},[45,60011,27969],{"class":103},[45,60013,100],{"class":99},[45,60015,109],{"class":85},[45,60017,60018,60020,60022,60024],{"class":47,"line":112},[45,60019,2065],{"class":92},[45,60021,96],{"class":85},[45,60023,59981],{"class":2070},[45,60025,109],{"class":85},[14,60027,177],{},[36,60029,60031],{"className":38,"code":60030,"language":40,"meta":41,"style":41},"['apple', 'banana', 'orange']\napple\n['apple', 'banana', 'orange', 'grape']\n",[18,60032,60033,60061,60065],{"__ignoreMap":41},[45,60034,60035,60037,60039,60041,60043,60045,60047,60049,60051,60053,60055,60057,60059],{"class":47,"line":48},[45,60036,9680],{"class":85},[45,60038,7901],{"class":99},[45,60040,9443],{"class":103},[45,60042,7901],{"class":99},[45,60044,3079],{"class":85},[45,60046,9199],{"class":99},[45,60048,9456],{"class":103},[45,60050,7901],{"class":99},[45,60052,3079],{"class":85},[45,60054,9199],{"class":99},[45,60056,59968],{"class":103},[45,60058,7901],{"class":99},[45,60060,8594],{"class":85},[45,60062,60063],{"class":47,"line":63},[45,60064,49463],{"class":51},[45,60066,60067,60069,60071,60073,60075,60077,60079,60081,60083,60085,60087,60089,60091,60093,60095,60097,60099],{"class":47,"line":70},[45,60068,9680],{"class":85},[45,60070,7901],{"class":99},[45,60072,9443],{"class":103},[45,60074,7901],{"class":99},[45,60076,3079],{"class":85},[45,60078,9199],{"class":99},[45,60080,9456],{"class":103},[45,60082,7901],{"class":99},[45,60084,3079],{"class":85},[45,60086,9199],{"class":99},[45,60088,59968],{"class":103},[45,60090,7901],{"class":99},[45,60092,3079],{"class":85},[45,60094,9199],{"class":99},[45,60096,27969],{"class":103},[45,60098,7901],{"class":99},[45,60100,8594],{"class":85},[14,60102,60103],{},"This example shows three important ideas:",[195,60105,60106,60111,60117],{},[198,60107,60108,60109],{},"A list uses square brackets ",[18,60110,21680],{},[198,60112,60113,60114],{},"You can get one item with an index like ",[18,60115,60116],{},"fruits[0]",[198,60118,60119,60120],{},"A list can grow with methods like ",[18,60121,31847],{},[31,60123,60125],{"id":60124},"what-a-python-list-is","What a Python list is",[14,60127,60128],{},"A Python list is an ordered collection of items.",[14,60130,60131],{},"Here are the main ideas:",[195,60133,60134,60137,60142,60145,60148],{},[198,60135,60136],{},"A list stores multiple values in one variable",[198,60138,60139,60140],{},"List items are written inside square brackets: ",[18,60141,21680],{},[198,60143,60144],{},"Items are separated with commas",[198,60146,60147],{},"A list keeps items in order",[198,60149,60150,60151,60153],{},"Lists are ",[2421,60152,29550],{},", which means you can change them after creation",[14,60155,466],{},[36,60157,60159],{"className":38,"code":60158,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\nprint(numbers)\n",[18,60160,60161,60181],{"__ignoreMap":41},[45,60162,60163,60165,60167,60169,60171,60173,60175,60177,60179],{"class":47,"line":48},[45,60164,8570],{"class":51},[45,60166,56],{"class":55},[45,60168,8575],{"class":85},[45,60170,11786],{"class":59},[45,60172,3079],{"class":85},[45,60174,10503],{"class":59},[45,60176,3079],{"class":85},[45,60178,10512],{"class":59},[45,60180,8594],{"class":85},[45,60182,60183,60185,60187,60189],{"class":47,"line":63},[45,60184,2065],{"class":92},[45,60186,96],{"class":85},[45,60188,9069],{"class":2070},[45,60190,109],{"class":85},[14,60192,177],{},[36,60194,60196],{"className":38,"code":60195,"language":40,"meta":41,"style":41},"[10, 20, 30]\n",[18,60197,60198],{"__ignoreMap":41},[45,60199,60200,60202,60204,60206,60208,60210,60212],{"class":47,"line":48},[45,60201,9680],{"class":85},[45,60203,11786],{"class":59},[45,60205,3079],{"class":85},[45,60207,10503],{"class":59},[45,60209,3079],{"class":85},[45,60211,10512],{"class":59},[45,60213,8594],{"class":85},[14,60215,60216,60217,240],{},"If you want a step-by-step page just on creating lists, see ",[236,60218,60220],{"href":60219},"\u002Freference\u002Fpython-list-creating-a-list\u002F","creating a Python list",[31,60222,60224],{"id":60223},"why-beginners-use-lists","Why beginners use lists",[14,60226,60227],{},"Lists are useful because they help you work with many related values at once.",[14,60229,60230],{},"Beginners often use lists to:",[195,60232,60233,60236,60239,60242],{},[198,60234,60235],{},"Store many related values in one place",[198,60237,60238],{},"Loop through items one by one",[198,60240,60241],{},"Add or remove items as a program runs",[198,60243,60244],{},"Keep track of names, numbers, scores, file paths, and user input",[14,60246,466],{},[36,60248,60250],{"className":38,"code":60249,"language":40,"meta":41,"style":41},"scores = [85, 90, 78, 92]\nprint(scores)\n",[18,60251,60252,60279],{"__ignoreMap":41},[45,60253,60254,60256,60258,60260,60263,60265,60267,60269,60272,60274,60277],{"class":47,"line":48},[45,60255,41781],{"class":51},[45,60257,56],{"class":55},[45,60259,8575],{"class":85},[45,60261,60262],{"class":59},"85",[45,60264,3079],{"class":85},[45,60266,603],{"class":59},[45,60268,3079],{"class":85},[45,60270,60271],{"class":59}," 78",[45,60273,3079],{"class":85},[45,60275,60276],{"class":59}," 92",[45,60278,8594],{"class":85},[45,60280,60281,60283,60285,60288],{"class":47,"line":63},[45,60282,2065],{"class":92},[45,60284,96],{"class":85},[45,60286,60287],{"class":2070},"scores",[45,60289,109],{"class":85},[14,60291,60292],{},"Instead of creating four separate variables, you can keep all the scores in one list.",[31,60294,60296],{"id":60295},"how-to-create-a-list","How to create a list",[14,60298,60299],{},"You can create a list in different ways.",[712,60301,60303],{"id":60302},"create-an-empty-list","Create an empty list",[36,60305,60307],{"className":38,"code":60306,"language":40,"meta":41,"style":41},"items = []\nprint(items)\n",[18,60308,60309,60317],{"__ignoreMap":41},[45,60310,60311,60313,60315],{"class":47,"line":48},[45,60312,23632],{"class":51},[45,60314,56],{"class":55},[45,60316,27604],{"class":85},[45,60318,60319,60321,60323,60325],{"class":47,"line":63},[45,60320,2065],{"class":92},[45,60322,96],{"class":85},[45,60324,9516],{"class":2070},[45,60326,109],{"class":85},[14,60328,177],{},[36,60330,60331],{"className":38,"code":14511,"language":40,"meta":41,"style":41},[18,60332,60333],{"__ignoreMap":41},[45,60334,60335],{"class":47,"line":48},[45,60336,14511],{"class":85},[712,60338,60340],{"id":60339},"create-a-list-with-starting-values","Create a list with starting values",[36,60342,60344],{"className":38,"code":60343,"language":40,"meta":41,"style":41},"colors = [\"red\", \"green\", \"blue\"]\nprint(colors)\n",[18,60345,60346,60378],{"__ignoreMap":41},[45,60347,60348,60350,60352,60354,60356,60358,60360,60362,60364,60366,60368,60370,60372,60374,60376],{"class":47,"line":48},[45,60349,22320],{"class":51},[45,60351,56],{"class":55},[45,60353,8575],{"class":85},[45,60355,100],{"class":99},[45,60357,22329],{"class":103},[45,60359,100],{"class":99},[45,60361,3079],{"class":85},[45,60363,1565],{"class":99},[45,60365,22338],{"class":103},[45,60367,100],{"class":99},[45,60369,3079],{"class":85},[45,60371,1565],{"class":99},[45,60373,22347],{"class":103},[45,60375,100],{"class":99},[45,60377,8594],{"class":85},[45,60379,60380,60382,60384,60386],{"class":47,"line":63},[45,60381,2065],{"class":92},[45,60383,96],{"class":85},[45,60385,22407],{"class":2070},[45,60387,109],{"class":85},[712,60389,60391],{"id":60390},"lists-can-store-different-types","Lists can store different types",[14,60393,60394],{},"A list can hold strings, numbers, booleans, and other objects.",[36,60396,60398],{"className":38,"code":60397,"language":40,"meta":41,"style":41},"mixed = [\"Alice\", 25, True]\nprint(mixed)\n",[18,60399,60400,60426],{"__ignoreMap":41},[45,60401,60402,60405,60407,60409,60411,60414,60416,60418,60420,60422,60424],{"class":47,"line":48},[45,60403,60404],{"class":51},"mixed ",[45,60406,56],{"class":55},[45,60408,8575],{"class":85},[45,60410,100],{"class":99},[45,60412,60413],{"class":103},"Alice",[45,60415,100],{"class":99},[45,60417,3079],{"class":85},[45,60419,303],{"class":59},[45,60421,3079],{"class":85},[45,60423,23323],{"class":481},[45,60425,8594],{"class":85},[45,60427,60428,60430,60432,60435],{"class":47,"line":63},[45,60429,2065],{"class":92},[45,60431,96],{"class":85},[45,60433,60434],{"class":2070},"mixed",[45,60436,109],{"class":85},[14,60438,60439],{},"This works, but beginners should usually keep related data together. A list of all names or all numbers is easier to understand than a list with unrelated values mixed together.",[31,60441,60443],{"id":60442},"how-list-indexing-works","How list indexing works",[14,60445,60446,60447,240],{},"Each item in a list has a position called an ",[2421,60448,60449],{},"index",[14,60451,449],{},[195,60453,60454,60459,60464,60467],{},[198,60455,60456,60457],{},"The first item has index ",[18,60458,10482],{},[198,60460,60461,60462],{},"The second item has index ",[18,60463,8578],{},[198,60465,60466],{},"Negative indexes count from the end",[198,60468,60469,60470],{},"Using an index that does not exist causes an ",[18,60471,46068],{},[14,60473,466],{},[36,60475,60477],{"className":38,"code":60476,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\n\nprint(fruits[0])   # first item\nprint(fruits[1])   # second item\nprint(fruits[-1])  # last item\n",[18,60478,60479,60511,60515,60532,60549],{"__ignoreMap":41},[45,60480,60481,60483,60485,60487,60489,60491,60493,60495,60497,60499,60501,60503,60505,60507,60509],{"class":47,"line":48},[45,60482,49394],{"class":51},[45,60484,56],{"class":55},[45,60486,8575],{"class":85},[45,60488,100],{"class":99},[45,60490,9443],{"class":103},[45,60492,100],{"class":99},[45,60494,3079],{"class":85},[45,60496,1565],{"class":99},[45,60498,9456],{"class":103},[45,60500,100],{"class":99},[45,60502,3079],{"class":85},[45,60504,1565],{"class":99},[45,60506,59968],{"class":103},[45,60508,100],{"class":99},[45,60510,8594],{"class":85},[45,60512,60513],{"class":47,"line":63},[45,60514,67],{"emptyLinePlaceholder":66},[45,60516,60517,60519,60521,60523,60525,60527,60529],{"class":47,"line":70},[45,60518,2065],{"class":92},[45,60520,96],{"class":85},[45,60522,59981],{"class":2070},[45,60524,9680],{"class":85},[45,60526,10482],{"class":59},[45,60528,45348],{"class":85},[45,60530,60531],{"class":7624},"   # first item\n",[45,60533,60534,60536,60538,60540,60542,60544,60546],{"class":47,"line":89},[45,60535,2065],{"class":92},[45,60537,96],{"class":85},[45,60539,59981],{"class":2070},[45,60541,9680],{"class":85},[45,60543,8578],{"class":59},[45,60545,45348],{"class":85},[45,60547,60548],{"class":7624},"   # second item\n",[45,60550,60551,60553,60555,60557,60559,60561,60563,60565],{"class":47,"line":112},[45,60552,2065],{"class":92},[45,60554,96],{"class":85},[45,60556,59981],{"class":2070},[45,60558,9680],{"class":85},[45,60560,218],{"class":55},[45,60562,8578],{"class":59},[45,60564,45348],{"class":85},[45,60566,60567],{"class":7624},"  # last item\n",[14,60569,177],{},[36,60571,60573],{"className":38,"code":60572,"language":40,"meta":41,"style":41},"apple\nbanana\norange\n",[18,60574,60575,60579,60583],{"__ignoreMap":41},[45,60576,60577],{"class":47,"line":48},[45,60578,49463],{"class":51},[45,60580,60581],{"class":47,"line":63},[45,60582,49468],{"class":51},[45,60584,60585],{"class":47,"line":70},[45,60586,60587],{"class":51},"orange\n",[712,60589,60591],{"id":60590},"what-happens-if-the-index-does-not-exist","What happens if the index does not exist?",[36,60593,60595],{"className":38,"code":60594,"language":40,"meta":41,"style":41},"letters = [\"a\", \"b\"]\nprint(letters[5])\n",[18,60596,60597,60622],{"__ignoreMap":41},[45,60598,60599,60602,60604,60606,60608,60610,60612,60614,60616,60618,60620],{"class":47,"line":48},[45,60600,60601],{"class":51},"letters ",[45,60603,56],{"class":55},[45,60605,8575],{"class":85},[45,60607,100],{"class":99},[45,60609,236],{"class":103},[45,60611,100],{"class":99},[45,60613,3079],{"class":85},[45,60615,1565],{"class":99},[45,60617,10384],{"class":103},[45,60619,100],{"class":99},[45,60621,8594],{"class":85},[45,60623,60624,60626,60628,60631,60633,60635],{"class":47,"line":63},[45,60625,2065],{"class":92},[45,60627,96],{"class":85},[45,60629,60630],{"class":2070},"letters",[45,60632,9680],{"class":85},[45,60634,3749],{"class":59},[45,60636,21941],{"class":85},[14,60638,60639,60640,60642],{},"This raises an error because index ",[18,60641,3749],{}," is not in the list.",[14,60644,60645,60646,240],{},"If you need help with that error, see ",[236,60647,46514],{"href":60648},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained\u002F",[31,60650,60652],{"id":60651},"how-to-change-list-items","How to change list items",[14,60654,60655],{},"Because lists are mutable, you can change an item by assigning a new value to its index.",[36,60657,60659],{"className":38,"code":60658,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\nfruits[1] = \"grape\"\n\nprint(fruits)\n",[18,60660,60661,60693,60711,60715],{"__ignoreMap":41},[45,60662,60663,60665,60667,60669,60671,60673,60675,60677,60679,60681,60683,60685,60687,60689,60691],{"class":47,"line":48},[45,60664,49394],{"class":51},[45,60666,56],{"class":55},[45,60668,8575],{"class":85},[45,60670,100],{"class":99},[45,60672,9443],{"class":103},[45,60674,100],{"class":99},[45,60676,3079],{"class":85},[45,60678,1565],{"class":99},[45,60680,9456],{"class":103},[45,60682,100],{"class":99},[45,60684,3079],{"class":85},[45,60686,1565],{"class":99},[45,60688,59968],{"class":103},[45,60690,100],{"class":99},[45,60692,8594],{"class":85},[45,60694,60695,60697,60699,60701,60703,60705,60707,60709],{"class":47,"line":63},[45,60696,59981],{"class":51},[45,60698,9680],{"class":85},[45,60700,8578],{"class":59},[45,60702,9685],{"class":85},[45,60704,3099],{"class":55},[45,60706,1565],{"class":99},[45,60708,27969],{"class":103},[45,60710,1571],{"class":99},[45,60712,60713],{"class":47,"line":70},[45,60714,67],{"emptyLinePlaceholder":66},[45,60716,60717,60719,60721,60723],{"class":47,"line":89},[45,60718,2065],{"class":92},[45,60720,96],{"class":85},[45,60722,59981],{"class":2070},[45,60724,109],{"class":85},[14,60726,177],{},[36,60728,60730],{"className":38,"code":60729,"language":40,"meta":41,"style":41},"['apple', 'grape', 'orange']\n",[18,60731,60732],{"__ignoreMap":41},[45,60733,60734,60736,60738,60740,60742,60744,60746,60748,60750,60752,60754,60756,60758],{"class":47,"line":48},[45,60735,9680],{"class":85},[45,60737,7901],{"class":99},[45,60739,9443],{"class":103},[45,60741,7901],{"class":99},[45,60743,3079],{"class":85},[45,60745,9199],{"class":99},[45,60747,27969],{"class":103},[45,60749,7901],{"class":99},[45,60751,3079],{"class":85},[45,60753,9199],{"class":99},[45,60755,59968],{"class":103},[45,60757,7901],{"class":99},[45,60759,8594],{"class":85},[14,60761,60762],{},"The original list changes.",[14,60764,60765,60766,240],{},"This is different from strings, which cannot be changed in place. If you want to understand that difference better, read ",[236,60767,21278],{"href":21277},[31,60769,60771],{"id":60770},"common-list-operations","Common list operations",[14,60773,60774],{},"Here are some of the most common things you will do with lists.",[712,60776,60778,60779],{"id":60777},"add-one-item-with-append","Add one item with ",[18,60780,31847],{},[36,60782,60784],{"className":38,"code":60783,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\"]\nfruits.append(\"orange\")\n\nprint(fruits)\n",[18,60785,60786,60810,60828,60832],{"__ignoreMap":41},[45,60787,60788,60790,60792,60794,60796,60798,60800,60802,60804,60806,60808],{"class":47,"line":48},[45,60789,49394],{"class":51},[45,60791,56],{"class":55},[45,60793,8575],{"class":85},[45,60795,100],{"class":99},[45,60797,9443],{"class":103},[45,60799,100],{"class":99},[45,60801,3079],{"class":85},[45,60803,1565],{"class":99},[45,60805,9456],{"class":103},[45,60807,100],{"class":99},[45,60809,8594],{"class":85},[45,60811,60812,60814,60816,60818,60820,60822,60824,60826],{"class":47,"line":63},[45,60813,59981],{"class":51},[45,60815,240],{"class":85},[45,60817,21025],{"class":2070},[45,60819,96],{"class":85},[45,60821,100],{"class":99},[45,60823,59968],{"class":103},[45,60825,100],{"class":99},[45,60827,109],{"class":85},[45,60829,60830],{"class":47,"line":70},[45,60831,67],{"emptyLinePlaceholder":66},[45,60833,60834,60836,60838,60840],{"class":47,"line":89},[45,60835,2065],{"class":92},[45,60837,96],{"class":85},[45,60839,59981],{"class":2070},[45,60841,109],{"class":85},[14,60843,177],{},[36,60845,60847],{"className":38,"code":60846,"language":40,"meta":41,"style":41},"['apple', 'banana', 'orange']\n",[18,60848,60849],{"__ignoreMap":41},[45,60850,60851,60853,60855,60857,60859,60861,60863,60865,60867,60869,60871,60873,60875],{"class":47,"line":48},[45,60852,9680],{"class":85},[45,60854,7901],{"class":99},[45,60856,9443],{"class":103},[45,60858,7901],{"class":99},[45,60860,3079],{"class":85},[45,60862,9199],{"class":99},[45,60864,9456],{"class":103},[45,60866,7901],{"class":99},[45,60868,3079],{"class":85},[45,60870,9199],{"class":99},[45,60872,59968],{"class":103},[45,60874,7901],{"class":99},[45,60876,8594],{"class":85},[14,60878,60879,60880,240],{},"For more detail, see the ",[236,60881,60883],{"href":60882},"\u002Freference\u002Fpython-list-append-method\u002F","Python list append() method",[712,60885,60887,60888],{"id":60886},"add-many-items-with-extend","Add many items with ",[18,60889,60890],{},"extend()",[36,60892,60894],{"className":38,"code":60893,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\"]\nfruits.extend([\"orange\", \"grape\"])\n\nprint(fruits)\n",[18,60895,60896,60920,60947,60951],{"__ignoreMap":41},[45,60897,60898,60900,60902,60904,60906,60908,60910,60912,60914,60916,60918],{"class":47,"line":48},[45,60899,49394],{"class":51},[45,60901,56],{"class":55},[45,60903,8575],{"class":85},[45,60905,100],{"class":99},[45,60907,9443],{"class":103},[45,60909,100],{"class":99},[45,60911,3079],{"class":85},[45,60913,1565],{"class":99},[45,60915,9456],{"class":103},[45,60917,100],{"class":99},[45,60919,8594],{"class":85},[45,60921,60922,60924,60926,60929,60931,60933,60935,60937,60939,60941,60943,60945],{"class":47,"line":63},[45,60923,59981],{"class":51},[45,60925,240],{"class":85},[45,60927,60928],{"class":2070},"extend",[45,60930,34742],{"class":85},[45,60932,100],{"class":99},[45,60934,59968],{"class":103},[45,60936,100],{"class":99},[45,60938,3079],{"class":85},[45,60940,1565],{"class":99},[45,60942,27969],{"class":103},[45,60944,100],{"class":99},[45,60946,21941],{"class":85},[45,60948,60949],{"class":47,"line":70},[45,60950,67],{"emptyLinePlaceholder":66},[45,60952,60953,60955,60957,60959],{"class":47,"line":89},[45,60954,2065],{"class":92},[45,60956,96],{"class":85},[45,60958,59981],{"class":2070},[45,60960,109],{"class":85},[14,60962,177],{},[36,60964,60966],{"className":38,"code":60965,"language":40,"meta":41,"style":41},"['apple', 'banana', 'orange', 'grape']\n",[18,60967,60968],{"__ignoreMap":41},[45,60969,60970,60972,60974,60976,60978,60980,60982,60984,60986,60988,60990,60992,60994,60996,60998,61000,61002],{"class":47,"line":48},[45,60971,9680],{"class":85},[45,60973,7901],{"class":99},[45,60975,9443],{"class":103},[45,60977,7901],{"class":99},[45,60979,3079],{"class":85},[45,60981,9199],{"class":99},[45,60983,9456],{"class":103},[45,60985,7901],{"class":99},[45,60987,3079],{"class":85},[45,60989,9199],{"class":99},[45,60991,59968],{"class":103},[45,60993,7901],{"class":99},[45,60995,3079],{"class":85},[45,60997,9199],{"class":99},[45,60999,27969],{"class":103},[45,61001,7901],{"class":99},[45,61003,8594],{"class":85},[712,61005,61007,61008],{"id":61006},"insert-at-a-specific-position-with-insert","Insert at a specific position with ",[18,61009,31850],{},[36,61011,61013],{"className":38,"code":61012,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"orange\"]\nfruits.insert(1, \"banana\")\n\nprint(fruits)\n",[18,61014,61015,61039,61062,61066],{"__ignoreMap":41},[45,61016,61017,61019,61021,61023,61025,61027,61029,61031,61033,61035,61037],{"class":47,"line":48},[45,61018,49394],{"class":51},[45,61020,56],{"class":55},[45,61022,8575],{"class":85},[45,61024,100],{"class":99},[45,61026,9443],{"class":103},[45,61028,100],{"class":99},[45,61030,3079],{"class":85},[45,61032,1565],{"class":99},[45,61034,59968],{"class":103},[45,61036,100],{"class":99},[45,61038,8594],{"class":85},[45,61040,61041,61043,61045,61048,61050,61052,61054,61056,61058,61060],{"class":47,"line":63},[45,61042,59981],{"class":51},[45,61044,240],{"class":85},[45,61046,61047],{"class":2070},"insert",[45,61049,96],{"class":85},[45,61051,8578],{"class":59},[45,61053,3079],{"class":85},[45,61055,1565],{"class":99},[45,61057,9456],{"class":103},[45,61059,100],{"class":99},[45,61061,109],{"class":85},[45,61063,61064],{"class":47,"line":70},[45,61065,67],{"emptyLinePlaceholder":66},[45,61067,61068,61070,61072,61074],{"class":47,"line":89},[45,61069,2065],{"class":92},[45,61071,96],{"class":85},[45,61073,59981],{"class":2070},[45,61075,109],{"class":85},[14,61077,177],{},[36,61079,61080],{"className":38,"code":60846,"language":40,"meta":41,"style":41},[18,61081,61082],{"__ignoreMap":41},[45,61083,61084,61086,61088,61090,61092,61094,61096,61098,61100,61102,61104,61106,61108],{"class":47,"line":48},[45,61085,9680],{"class":85},[45,61087,7901],{"class":99},[45,61089,9443],{"class":103},[45,61091,7901],{"class":99},[45,61093,3079],{"class":85},[45,61095,9199],{"class":99},[45,61097,9456],{"class":103},[45,61099,7901],{"class":99},[45,61101,3079],{"class":85},[45,61103,9199],{"class":99},[45,61105,59968],{"class":103},[45,61107,7901],{"class":99},[45,61109,8594],{"class":85},[712,61111,61113,61114],{"id":61112},"remove-by-value-with-remove","Remove by value with ",[18,61115,31853],{},[36,61117,61119],{"className":38,"code":61118,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\nfruits.remove(\"banana\")\n\nprint(fruits)\n",[18,61120,61121,61153,61172,61176],{"__ignoreMap":41},[45,61122,61123,61125,61127,61129,61131,61133,61135,61137,61139,61141,61143,61145,61147,61149,61151],{"class":47,"line":48},[45,61124,49394],{"class":51},[45,61126,56],{"class":55},[45,61128,8575],{"class":85},[45,61130,100],{"class":99},[45,61132,9443],{"class":103},[45,61134,100],{"class":99},[45,61136,3079],{"class":85},[45,61138,1565],{"class":99},[45,61140,9456],{"class":103},[45,61142,100],{"class":99},[45,61144,3079],{"class":85},[45,61146,1565],{"class":99},[45,61148,59968],{"class":103},[45,61150,100],{"class":99},[45,61152,8594],{"class":85},[45,61154,61155,61157,61159,61162,61164,61166,61168,61170],{"class":47,"line":63},[45,61156,59981],{"class":51},[45,61158,240],{"class":85},[45,61160,61161],{"class":2070},"remove",[45,61163,96],{"class":85},[45,61165,100],{"class":99},[45,61167,9456],{"class":103},[45,61169,100],{"class":99},[45,61171,109],{"class":85},[45,61173,61174],{"class":47,"line":70},[45,61175,67],{"emptyLinePlaceholder":66},[45,61177,61178,61180,61182,61184],{"class":47,"line":89},[45,61179,2065],{"class":92},[45,61181,96],{"class":85},[45,61183,59981],{"class":2070},[45,61185,109],{"class":85},[14,61187,177],{},[36,61189,61191],{"className":38,"code":61190,"language":40,"meta":41,"style":41},"['apple', 'orange']\n",[18,61192,61193],{"__ignoreMap":41},[45,61194,61195,61197,61199,61201,61203,61205,61207,61209,61211],{"class":47,"line":48},[45,61196,9680],{"class":85},[45,61198,7901],{"class":99},[45,61200,9443],{"class":103},[45,61202,7901],{"class":99},[45,61204,3079],{"class":85},[45,61206,9199],{"class":99},[45,61208,59968],{"class":103},[45,61210,7901],{"class":99},[45,61212,8594],{"class":85},[712,61214,61216,61217],{"id":61215},"remove-by-position-with-pop","Remove by position with ",[18,61218,43793],{},[36,61220,61222],{"className":38,"code":61221,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\nremoved_item = fruits.pop(1)\n\nprint(removed_item)\nprint(fruits)\n",[18,61223,61224,61256,61275,61279,61290],{"__ignoreMap":41},[45,61225,61226,61228,61230,61232,61234,61236,61238,61240,61242,61244,61246,61248,61250,61252,61254],{"class":47,"line":48},[45,61227,49394],{"class":51},[45,61229,56],{"class":55},[45,61231,8575],{"class":85},[45,61233,100],{"class":99},[45,61235,9443],{"class":103},[45,61237,100],{"class":99},[45,61239,3079],{"class":85},[45,61241,1565],{"class":99},[45,61243,9456],{"class":103},[45,61245,100],{"class":99},[45,61247,3079],{"class":85},[45,61249,1565],{"class":99},[45,61251,59968],{"class":103},[45,61253,100],{"class":99},[45,61255,8594],{"class":85},[45,61257,61258,61261,61263,61265,61267,61269,61271,61273],{"class":47,"line":63},[45,61259,61260],{"class":51},"removed_item ",[45,61262,56],{"class":55},[45,61264,49438],{"class":51},[45,61266,240],{"class":85},[45,61268,43869],{"class":2070},[45,61270,96],{"class":85},[45,61272,8578],{"class":59},[45,61274,109],{"class":85},[45,61276,61277],{"class":47,"line":70},[45,61278,67],{"emptyLinePlaceholder":66},[45,61280,61281,61283,61285,61288],{"class":47,"line":89},[45,61282,2065],{"class":92},[45,61284,96],{"class":85},[45,61286,61287],{"class":2070},"removed_item",[45,61289,109],{"class":85},[45,61291,61292,61294,61296,61298],{"class":47,"line":112},[45,61293,2065],{"class":92},[45,61295,96],{"class":85},[45,61297,59981],{"class":2070},[45,61299,109],{"class":85},[14,61301,177],{},[36,61303,61305],{"className":38,"code":61304,"language":40,"meta":41,"style":41},"banana\n['apple', 'orange']\n",[18,61306,61307,61311],{"__ignoreMap":41},[45,61308,61309],{"class":47,"line":48},[45,61310,49468],{"class":51},[45,61312,61313,61315,61317,61319,61321,61323,61325,61327,61329],{"class":47,"line":63},[45,61314,9680],{"class":85},[45,61316,7901],{"class":99},[45,61318,9443],{"class":103},[45,61320,7901],{"class":99},[45,61322,3079],{"class":85},[45,61324,9199],{"class":99},[45,61326,59968],{"class":103},[45,61328,7901],{"class":99},[45,61330,8594],{"class":85},[712,61332,61334,61335],{"id":61333},"check-length-with-len","Check length with ",[18,61336,59071],{},[36,61338,61340],{"className":38,"code":61339,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\nprint(len(fruits))\n",[18,61341,61342,61374],{"__ignoreMap":41},[45,61343,61344,61346,61348,61350,61352,61354,61356,61358,61360,61362,61364,61366,61368,61370,61372],{"class":47,"line":48},[45,61345,49394],{"class":51},[45,61347,56],{"class":55},[45,61349,8575],{"class":85},[45,61351,100],{"class":99},[45,61353,9443],{"class":103},[45,61355,100],{"class":99},[45,61357,3079],{"class":85},[45,61359,1565],{"class":99},[45,61361,9456],{"class":103},[45,61363,100],{"class":99},[45,61365,3079],{"class":85},[45,61367,1565],{"class":99},[45,61369,59968],{"class":103},[45,61371,100],{"class":99},[45,61373,8594],{"class":85},[45,61375,61376,61378,61380,61382,61384,61386],{"class":47,"line":63},[45,61377,2065],{"class":92},[45,61379,96],{"class":85},[45,61381,28716],{"class":92},[45,61383,96],{"class":85},[45,61385,59981],{"class":2070},[45,61387,4348],{"class":85},[14,61389,177],{},[36,61391,61392],{"className":38,"code":13046,"language":40,"meta":41,"style":41},[18,61393,61394],{"__ignoreMap":41},[45,61395,61396],{"class":47,"line":48},[45,61397,13046],{"class":59},[14,61399,23968,61400,240],{},[236,61401,61403],{"href":61402},"\u002Freference\u002Fpython-list-length-len\u002F","Python list length with len()",[712,61405,61407,61408],{"id":61406},"check-membership-with-in","Check membership with ",[18,61409,8623],{},[36,61411,61413],{"className":38,"code":61412,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\n\nprint(\"banana\" in fruits)\nprint(\"grape\" in fruits)\n",[18,61414,61415,61447,61451,61469],{"__ignoreMap":41},[45,61416,61417,61419,61421,61423,61425,61427,61429,61431,61433,61435,61437,61439,61441,61443,61445],{"class":47,"line":48},[45,61418,49394],{"class":51},[45,61420,56],{"class":55},[45,61422,8575],{"class":85},[45,61424,100],{"class":99},[45,61426,9443],{"class":103},[45,61428,100],{"class":99},[45,61430,3079],{"class":85},[45,61432,1565],{"class":99},[45,61434,9456],{"class":103},[45,61436,100],{"class":99},[45,61438,3079],{"class":85},[45,61440,1565],{"class":99},[45,61442,59968],{"class":103},[45,61444,100],{"class":99},[45,61446,8594],{"class":85},[45,61448,61449],{"class":47,"line":63},[45,61450,67],{"emptyLinePlaceholder":66},[45,61452,61453,61455,61457,61459,61461,61463,61465,61467],{"class":47,"line":70},[45,61454,2065],{"class":92},[45,61456,96],{"class":85},[45,61458,100],{"class":99},[45,61460,9456],{"class":103},[45,61462,100],{"class":99},[45,61464,45650],{"class":73},[45,61466,49438],{"class":2070},[45,61468,109],{"class":85},[45,61470,61471,61473,61475,61477,61479,61481,61483,61485],{"class":47,"line":89},[45,61472,2065],{"class":92},[45,61474,96],{"class":85},[45,61476,100],{"class":99},[45,61478,27969],{"class":103},[45,61480,100],{"class":99},[45,61482,45650],{"class":73},[45,61484,49438],{"class":2070},[45,61486,109],{"class":85},[14,61488,177],{},[36,61490,61491],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,61492,61493,61497],{"__ignoreMap":41},[45,61494,61495],{"class":47,"line":48},[45,61496,2270],{"class":481},[45,61498,61499],{"class":47,"line":63},[45,61500,2265],{"class":481},[31,61502,49657],{"id":49656},[14,61504,2419,61505,61507],{},[18,61506,7895],{}," loop is the most common way to read each item in a list.",[36,61509,61511],{"className":38,"code":61510,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\n\nfor fruit in fruits:\n    print(fruit)\n",[18,61512,61513,61545,61549,61561],{"__ignoreMap":41},[45,61514,61515,61517,61519,61521,61523,61525,61527,61529,61531,61533,61535,61537,61539,61541,61543],{"class":47,"line":48},[45,61516,49394],{"class":51},[45,61518,56],{"class":55},[45,61520,8575],{"class":85},[45,61522,100],{"class":99},[45,61524,9443],{"class":103},[45,61526,100],{"class":99},[45,61528,3079],{"class":85},[45,61530,1565],{"class":99},[45,61532,9456],{"class":103},[45,61534,100],{"class":99},[45,61536,3079],{"class":85},[45,61538,1565],{"class":99},[45,61540,59968],{"class":103},[45,61542,100],{"class":99},[45,61544,8594],{"class":85},[45,61546,61547],{"class":47,"line":63},[45,61548,67],{"emptyLinePlaceholder":66},[45,61550,61551,61553,61555,61557,61559],{"class":47,"line":70},[45,61552,7895],{"class":73},[45,61554,49433],{"class":51},[45,61556,8623],{"class":73},[45,61558,49438],{"class":51},[45,61560,86],{"class":85},[45,61562,61563,61565,61567,61569],{"class":47,"line":89},[45,61564,93],{"class":92},[45,61566,96],{"class":85},[45,61568,49449],{"class":2070},[45,61570,109],{"class":85},[14,61572,177],{},[36,61574,61575],{"className":38,"code":60572,"language":40,"meta":41,"style":41},[18,61576,61577,61581,61585],{"__ignoreMap":41},[45,61578,61579],{"class":47,"line":48},[45,61580,49463],{"class":51},[45,61582,61583],{"class":47,"line":63},[45,61584,49468],{"class":51},[45,61586,61587],{"class":47,"line":70},[45,61588,60587],{"class":51},[14,61590,61591],{},"Use this when you want to process items one by one.",[712,61593,165,61595,61597],{"id":61594},"use-enumerate-when-you-need-index-and-value",[18,61596,51065],{}," when you need index and value",[36,61599,61601],{"className":38,"code":61600,"language":40,"meta":41,"style":41},"fruits = [\"apple\", \"banana\", \"orange\"]\n\nfor index, fruit in enumerate(fruits):\n    print(index, fruit)\n",[18,61602,61603,61635,61639,61661],{"__ignoreMap":41},[45,61604,61605,61607,61609,61611,61613,61615,61617,61619,61621,61623,61625,61627,61629,61631,61633],{"class":47,"line":48},[45,61606,49394],{"class":51},[45,61608,56],{"class":55},[45,61610,8575],{"class":85},[45,61612,100],{"class":99},[45,61614,9443],{"class":103},[45,61616,100],{"class":99},[45,61618,3079],{"class":85},[45,61620,1565],{"class":99},[45,61622,9456],{"class":103},[45,61624,100],{"class":99},[45,61626,3079],{"class":85},[45,61628,1565],{"class":99},[45,61630,59968],{"class":103},[45,61632,100],{"class":99},[45,61634,8594],{"class":85},[45,61636,61637],{"class":47,"line":63},[45,61638,67],{"emptyLinePlaceholder":66},[45,61640,61641,61643,61646,61648,61650,61652,61655,61657,61659],{"class":47,"line":70},[45,61642,7895],{"class":73},[45,61644,61645],{"class":51}," index",[45,61647,3079],{"class":85},[45,61649,49433],{"class":51},[45,61651,8623],{"class":73},[45,61653,61654],{"class":92}," enumerate",[45,61656,96],{"class":85},[45,61658,59981],{"class":2070},[45,61660,2464],{"class":85},[45,61662,61663,61665,61667,61669,61671,61674],{"class":47,"line":89},[45,61664,93],{"class":92},[45,61666,96],{"class":85},[45,61668,60449],{"class":2070},[45,61670,3079],{"class":85},[45,61672,61673],{"class":2070}," fruit",[45,61675,109],{"class":85},[14,61677,177],{},[36,61679,61681],{"className":38,"code":61680,"language":40,"meta":41,"style":41},"0 apple\n1 banana\n2 orange\n",[18,61682,61683,61690,61697],{"__ignoreMap":41},[45,61684,61685,61687],{"class":47,"line":48},[45,61686,10482],{"class":59},[45,61688,61689],{"class":51}," apple\n",[45,61691,61692,61694],{"class":47,"line":63},[45,61693,8578],{"class":59},[45,61695,61696],{"class":51}," banana\n",[45,61698,61699,61701],{"class":47,"line":70},[45,61700,9367],{"class":59},[45,61702,61703],{"class":51}," orange\n",[14,61705,61706,61707,240],{},"For a task-focused guide, see ",[236,61708,49788],{"href":37354},[31,61710,61712],{"id":61711},"list-slicing-basics","List slicing basics",[14,61714,61715],{},"Slicing lets you get part of a list.",[14,61717,61718],{},"Use this format:",[36,61720,61722],{"className":38,"code":61721,"language":40,"meta":41,"style":41},"my_list[start:stop]\n",[18,61723,61724],{"__ignoreMap":41},[45,61725,61726,61728,61730,61732,61734,61736],{"class":47,"line":48},[45,61727,30561],{"class":51},[45,61729,9680],{"class":85},[45,61731,49940],{"class":51},[45,61733,457],{"class":85},[45,61735,49948],{"class":51},[45,61737,8594],{"class":85},[14,61739,56485],{},[195,61741,61742,61745,61751,61754],{},[198,61743,61744],{},"Slicing gets part of a list",[198,61746,165,61747,61750],{},[18,61748,61749],{},"start:stop"," syntax",[198,61752,61753],{},"The stop position is not included",[198,61755,61756],{},"Slicing creates a new list",[14,61758,466],{},[36,61760,61762],{"className":38,"code":61761,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30, 40, 50]\n\nprint(numbers[1:4])\n",[18,61763,61764,61792,61796],{"__ignoreMap":41},[45,61765,61766,61768,61770,61772,61774,61776,61778,61780,61782,61784,61786,61788,61790],{"class":47,"line":48},[45,61767,8570],{"class":51},[45,61769,56],{"class":55},[45,61771,8575],{"class":85},[45,61773,11786],{"class":59},[45,61775,3079],{"class":85},[45,61777,10503],{"class":59},[45,61779,3079],{"class":85},[45,61781,10512],{"class":59},[45,61783,3079],{"class":85},[45,61785,10521],{"class":59},[45,61787,3079],{"class":85},[45,61789,20121],{"class":59},[45,61791,8594],{"class":85},[45,61793,61794],{"class":47,"line":63},[45,61795,67],{"emptyLinePlaceholder":66},[45,61797,61798,61800,61802,61804,61806,61808,61810,61812],{"class":47,"line":70},[45,61799,2065],{"class":92},[45,61801,96],{"class":85},[45,61803,9069],{"class":2070},[45,61805,9680],{"class":85},[45,61807,8578],{"class":59},[45,61809,457],{"class":85},[45,61811,3584],{"class":59},[45,61813,21941],{"class":85},[14,61815,177],{},[36,61817,61819],{"className":38,"code":61818,"language":40,"meta":41,"style":41},"[20, 30, 40]\n",[18,61820,61821],{"__ignoreMap":41},[45,61822,61823,61825,61827,61829,61831,61833,61835],{"class":47,"line":48},[45,61824,9680],{"class":85},[45,61826,3372],{"class":59},[45,61828,3079],{"class":85},[45,61830,10512],{"class":59},[45,61832,3079],{"class":85},[45,61834,10521],{"class":59},[45,61836,8594],{"class":85},[14,61838,61839,61840,61842,61843,240],{},"This starts at index ",[18,61841,8578],{}," and stops before index ",[18,61844,3584],{},[14,61846,61847,61848,240],{},"More examples are on ",[236,61849,61851],{"href":61850},"\u002Freference\u002Fpython-list-slicing-explained\u002F","Python list slicing explained",[31,61853,61855],{"id":61854},"lists-vs-tuples-sets-and-dictionaries","Lists vs tuples, sets, and dictionaries",[14,61857,61858],{},"Lists are not the only collection type in Python.",[14,61860,61861],{},"Use a:",[195,61863,61864,61869,61875,61881],{},[198,61865,61866,61868],{},[2421,61867,28294],{}," when order matters and values may change",[198,61870,61871,61874],{},[2421,61872,61873],{},"Tuple"," for fixed data",[198,61876,61877,61880],{},[2421,61878,61879],{},"Set"," for unique items",[198,61882,61883,61886],{},[2421,61884,61885],{},"Dictionary"," for key-value pairs",[14,61888,61889],{},"Simple examples:",[36,61891,61893],{"className":38,"code":61892,"language":40,"meta":41,"style":41},"my_list = [\"apple\", \"banana\"]\nmy_tuple = (\"apple\", \"banana\")\nmy_set = {\"apple\", \"banana\"}\nmy_dict = {\"name\": \"Alice\", \"age\": 25}\n",[18,61894,61895,61920,61945,61970],{"__ignoreMap":41},[45,61896,61897,61900,61902,61904,61906,61908,61910,61912,61914,61916,61918],{"class":47,"line":48},[45,61898,61899],{"class":51},"my_list ",[45,61901,56],{"class":55},[45,61903,8575],{"class":85},[45,61905,100],{"class":99},[45,61907,9443],{"class":103},[45,61909,100],{"class":99},[45,61911,3079],{"class":85},[45,61913,1565],{"class":99},[45,61915,9456],{"class":103},[45,61917,100],{"class":99},[45,61919,8594],{"class":85},[45,61921,61922,61925,61927,61929,61931,61933,61935,61937,61939,61941,61943],{"class":47,"line":63},[45,61923,61924],{"class":51},"my_tuple ",[45,61926,56],{"class":55},[45,61928,14039],{"class":85},[45,61930,100],{"class":99},[45,61932,9443],{"class":103},[45,61934,100],{"class":99},[45,61936,3079],{"class":85},[45,61938,1565],{"class":99},[45,61940,9456],{"class":103},[45,61942,100],{"class":99},[45,61944,109],{"class":85},[45,61946,61947,61950,61952,61954,61956,61958,61960,61962,61964,61966,61968],{"class":47,"line":70},[45,61948,61949],{"class":51},"my_set ",[45,61951,56],{"class":55},[45,61953,8604],{"class":85},[45,61955,100],{"class":99},[45,61957,9443],{"class":103},[45,61959,100],{"class":99},[45,61961,3079],{"class":85},[45,61963,1565],{"class":99},[45,61965,9456],{"class":103},[45,61967,100],{"class":99},[45,61969,8629],{"class":85},[45,61971,61972,61975,61977,61979,61981,61983,61985,61987,61989,61991,61993,61995,61997,61999,62001,62003,62005],{"class":47,"line":89},[45,61973,61974],{"class":51},"my_dict ",[45,61976,56],{"class":55},[45,61978,8604],{"class":85},[45,61980,100],{"class":99},[45,61982,3096],{"class":103},[45,61984,100],{"class":99},[45,61986,457],{"class":85},[45,61988,1565],{"class":99},[45,61990,60413],{"class":103},[45,61992,100],{"class":99},[45,61994,3079],{"class":85},[45,61996,1565],{"class":99},[45,61998,2082],{"class":103},[45,62000,100],{"class":99},[45,62002,457],{"class":85},[45,62004,303],{"class":59},[45,62006,8629],{"class":85},[14,62008,62009,62010,240],{},"If you are not sure which one to use, read ",[236,62011,62013],{"href":62012},"\u002Flearn\u002Fwhen-to-use-lists-vs-tuples-vs-sets-vs-dictionaries\u002F","when to use lists vs tuples vs sets vs dictionaries",[31,62015,24087],{"id":24086},[14,62017,62018],{},"These are some common list mistakes:",[195,62020,62021,62026,62029,62035,62038],{},[198,62022,62023,62024],{},"Forgetting that indexes start at ",[18,62025,10482],{},[198,62027,62028],{},"Trying to access an item that is not in the list",[198,62030,52955,62031,14527,62033],{},[18,62032,31847],{},[18,62034,60890],{},[198,62036,62037],{},"Changing a list when you meant to make a copy",[198,62039,62040,62041,62043,62044],{},"Using parentheses ",[18,62042,3772],{}," instead of square brackets ",[18,62045,21680],{},[14,62047,62048],{},"Here are a few common causes of problems:",[195,62050,62051,62057,62062,62065,62072],{},[198,62052,1350,62053,62056],{},[18,62054,62055],{},"list[1]"," when the list has only one item",[198,62058,50822,62059,62061],{},[18,62060,31847],{}," to add multiple separate items",[198,62063,62064],{},"Assuming lists cannot be changed after creation",[198,62066,62067,62068,1316,62070],{},"Mixing up ",[18,62069,31853],{},[18,62071,43793],{},[198,62073,62074],{},"Forgetting that negative indexes start from the end",[14,62076,62077],{},"If something is not working, these quick checks often help:",[36,62079,62081],{"className":38,"code":62080,"language":40,"meta":41,"style":41},"print(my_list)\nprint(len(my_list))\nprint(my_list[0])\nprint(type(my_list))\nfor index, value in enumerate(my_list):\n    print(index, value)\n",[18,62082,62083,62093,62107,62121,62135,62155],{"__ignoreMap":41},[45,62084,62085,62087,62089,62091],{"class":47,"line":48},[45,62086,2065],{"class":92},[45,62088,96],{"class":85},[45,62090,30561],{"class":2070},[45,62092,109],{"class":85},[45,62094,62095,62097,62099,62101,62103,62105],{"class":47,"line":63},[45,62096,2065],{"class":92},[45,62098,96],{"class":85},[45,62100,28716],{"class":92},[45,62102,96],{"class":85},[45,62104,30561],{"class":2070},[45,62106,4348],{"class":85},[45,62108,62109,62111,62113,62115,62117,62119],{"class":47,"line":70},[45,62110,2065],{"class":92},[45,62112,96],{"class":85},[45,62114,30561],{"class":2070},[45,62116,9680],{"class":85},[45,62118,10482],{"class":59},[45,62120,21941],{"class":85},[45,62122,62123,62125,62127,62129,62131,62133],{"class":47,"line":89},[45,62124,2065],{"class":92},[45,62126,96],{"class":85},[45,62128,4340],{"class":2109},[45,62130,96],{"class":85},[45,62132,30561],{"class":2070},[45,62134,4348],{"class":85},[45,62136,62137,62139,62141,62143,62145,62147,62149,62151,62153],{"class":47,"line":112},[45,62138,7895],{"class":73},[45,62140,61645],{"class":51},[45,62142,3079],{"class":85},[45,62144,1492],{"class":51},[45,62146,8623],{"class":73},[45,62148,61654],{"class":92},[45,62150,96],{"class":85},[45,62152,30561],{"class":2070},[45,62154,2464],{"class":85},[45,62156,62157,62159,62161,62163,62165,62167],{"class":47,"line":126},[45,62158,93],{"class":92},[45,62160,96],{"class":85},[45,62162,60449],{"class":2070},[45,62164,3079],{"class":85},[45,62166,42794],{"class":2070},[45,62168,109],{"class":85},[14,62170,62171],{},"These can help you see:",[195,62173,62174,62177,62180,62183],{},[198,62175,62176],{},"What is actually in the list",[198,62178,62179],{},"How many items it has",[198,62181,62182],{},"Whether it is really a list",[198,62184,62185],{},"Which index each item has",[31,62187,2281],{"id":2280},[712,62189,62191],{"id":62190},"what-is-a-python-list","What is a Python list?",[14,62193,62194],{},"A Python list is an ordered collection of items that can be changed after it is created.",[712,62196,62198],{"id":62197},"can-a-python-list-store-different-data-types","Can a Python list store different data types?",[14,62200,62201],{},"Yes. A list can store strings, numbers, booleans, and other objects together.",[712,62203,62205],{"id":62204},"do-python-lists-start-at-index-0","Do Python lists start at index 0?",[14,62207,62208,62209,240],{},"Yes. The first item in a list is at index ",[18,62210,10482],{},[712,62212,62214],{"id":62213},"what-happens-if-i-use-an-index-that-does-not-exist","What happens if I use an index that does not exist?",[14,62216,62217,62218,62220,62221,240],{},"Python raises an ",[18,62219,46068],{},", usually with the message ",[18,62222,46415],{},[712,62224,16362,62226,1316,62228,354],{"id":62225},"what-is-the-difference-between-append-and-extend",[18,62227,31847],{},[18,62229,60890],{},[14,62231,62232,62234,62235,62237],{},[18,62233,31847],{}," adds one item to the end of the list. ",[18,62236,60890],{}," adds each item from another iterable.",[712,62239,62241],{"id":62240},"when-should-i-use-a-list-instead-of-a-tuple","When should I use a list instead of a tuple?",[14,62243,62244],{},"Use a list when you need to change, add, or remove items.",[31,62246,2342],{"id":2341},[195,62248,62249,62254,62258,62264,62270,62276,62282],{},[198,62250,62251],{},[236,62252,62253],{"href":60219},"Creating a Python list",[198,62255,62256],{},[236,62257,60883],{"href":60882},[198,62259,62260],{},[236,62261,62263],{"href":62262},"\u002Freference\u002Fpython-list-extend-method\u002F","Python list extend() method",[198,62265,62266],{},[236,62267,62269],{"href":62268},"\u002Fhow-to\u002Fhow-to-add-an-item-to-a-list-in-python\u002F","How to add an item to a list in Python",[198,62271,62272],{},[236,62273,62275],{"href":62274},"\u002Fhow-to\u002Fhow-to-find-an-item-in-a-list-in-python\u002F","How to find an item in a list in Python",[198,62277,62278],{},[236,62279,62281],{"href":62280},"\u002Fhow-to\u002Fhow-to-get-the-length-of-a-list-in-python\u002F","How to get the length of a list in Python",[198,62283,62284],{},[236,62285,46514],{"href":60648},[2367,62287,62288],{},"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 .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 .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 .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":41,"searchDepth":63,"depth":63,"links":62290},[62291,62292,62293,62298,62301,62302,62318,62322,62323,62324,62325,62334],{"id":60124,"depth":63,"text":60125},{"id":60223,"depth":63,"text":60224},{"id":60295,"depth":63,"text":60296,"children":62294},[62295,62296,62297],{"id":60302,"depth":70,"text":60303},{"id":60339,"depth":70,"text":60340},{"id":60390,"depth":70,"text":60391},{"id":60442,"depth":63,"text":60443,"children":62299},[62300],{"id":60590,"depth":70,"text":60591},{"id":60651,"depth":63,"text":60652},{"id":60770,"depth":63,"text":60771,"children":62303},[62304,62306,62308,62310,62312,62314,62316],{"id":60777,"depth":70,"text":62305},"Add one item with append()",{"id":60886,"depth":70,"text":62307},"Add many items with extend()",{"id":61006,"depth":70,"text":62309},"Insert at a specific position with insert()",{"id":61112,"depth":70,"text":62311},"Remove by value with remove()",{"id":61215,"depth":70,"text":62313},"Remove by position with pop()",{"id":61333,"depth":70,"text":62315},"Check length with len()",{"id":61406,"depth":70,"text":62317},"Check membership with in",{"id":49656,"depth":63,"text":49657,"children":62319},[62320],{"id":61594,"depth":70,"text":62321},"Use enumerate() when you need index and value",{"id":61711,"depth":63,"text":61712},{"id":61854,"depth":63,"text":61855},{"id":24086,"depth":63,"text":24087},{"id":2280,"depth":63,"text":2281,"children":62326},[62327,62328,62329,62330,62331,62333],{"id":62190,"depth":70,"text":62191},{"id":62197,"depth":70,"text":62198},{"id":62204,"depth":70,"text":62205},{"id":62213,"depth":70,"text":62214},{"id":62225,"depth":70,"text":62332},"What is the difference between append() and extend()?",{"id":62240,"depth":70,"text":62241},{"id":2341,"depth":63,"text":2342},"Master python lists explained beginner guide in our comprehensive Python beginner guide.",{},{"title":59920,"description":62335},"learn\u002Fpython-lists-explained-beginner-guide","lMdZxP37ZJUbWHZG6CnY9Ku7jjoT-GHWkJDF22MyKKo",{"id":62341,"title":62342,"body":62343,"description":63234,"extension":2399,"meta":63235,"navigation":66,"path":15052,"seo":63236,"stem":63237,"__hash__":63238},"content\u002Flearn\u002Fpython-modules-explained.md","Python Modules Explained",{"type":7,"value":62344,"toc":63201},[62345,62348,62351,62354,62357,62390,62396,62398,62412,62416,62422,62425,62436,62439,62442,62445,62456,62460,62463,62466,62477,62480,62483,62486,62490,62493,62497,62500,62502,62517,62520,62524,62527,62530,62549,62567,62571,62574,62581,62585,62590,62633,62637,62645,62648,62670,62673,62689,62694,62698,62701,62707,62740,62745,62751,62800,62804,62818,62822,62825,62839,62841,62865,62867,62879,62883,62886,62900,62902,62921,62924,62932,62935,62938,62971,62979,62981,62984,62990,63009,63011,63014,63018,63027,63030,63034,63037,63040,63047,63051,63053,63076,63081,63084,63116,63120,63123,63126,63128,63131,63134,63138,63141,63145,63148,63152,63155,63166,63168,63198],[10,62346,62342],{"id":62347},"python-modules-explained",[14,62349,62350],{},"A Python module is one of the main ways Python keeps code organized and reusable.",[14,62352,62353],{},"In simple terms, a module is usually a Python file that contains code such as variables, functions, or classes. You can then use that code in another Python file instead of writing it again.",[14,62355,62356],{},"This page explains what modules are, why they matter, and how beginners use them. It keeps the focus on the idea of modules, not the full details of import syntax or packages.",[36,62358,62360],{"className":38,"code":62359,"language":40,"meta":41,"style":41},"import math\n\nprint(math.sqrt(25))\n",[18,62361,62362,62368,62372],{"__ignoreMap":41},[45,62363,62364,62366],{"class":47,"line":48},[45,62365,14870],{"class":73},[45,62367,14891],{"class":51},[45,62369,62370],{"class":47,"line":63},[45,62371,67],{"emptyLinePlaceholder":66},[45,62373,62374,62376,62378,62380,62382,62384,62386,62388],{"class":47,"line":70},[45,62375,2065],{"class":92},[45,62377,96],{"class":85},[45,62379,14900],{"class":2070},[45,62381,240],{"class":85},[45,62383,14905],{"class":2070},[45,62385,96],{"class":85},[45,62387,14942],{"class":59},[45,62389,4348],{"class":85},[14,62391,62392,62395],{},[2421,62393,62394],{},"Quick example:"," a module is a Python file you can import to use code written elsewhere.",[31,62397,6600],{"id":6599},[195,62399,62400,62403,62406,62409],{},[198,62401,62402],{},"What a module is in simple terms",[198,62404,62405],{},"How modules help organize code",[198,62407,62408],{},"The difference between your own modules and built-in modules",[198,62410,62411],{},"A beginner-level introduction to importing",[31,62413,62415],{"id":62414},"what-is-a-python-module","What is a Python module?",[14,62417,62418,62419,62421],{},"A Python module is usually a single ",[18,62420,15010],{}," file.",[14,62423,62424],{},"That file can contain:",[195,62426,62427,62430,62433],{},[198,62428,62429],{},"Variables",[198,62431,62432],{},"Functions",[198,62434,62435],{},"Classes",[14,62437,62438],{},"The main idea is simple: if you write useful code in one file, you can reuse it from another file.",[14,62440,62441],{},"For example, instead of writing the same function in many places, you can put it in one module and import it when needed.",[14,62443,62444],{},"Modules help you:",[195,62446,62447,62450,62453],{},[198,62448,62449],{},"Avoid repeating code",[198,62451,62452],{},"Keep related code together",[198,62454,62455],{},"Make programs easier to read",[31,62457,62459],{"id":62458},"why-modules-are-useful","Why modules are useful",[14,62461,62462],{},"Modules are useful because they help you split code into smaller, clearer parts.",[14,62464,62465],{},"Instead of putting everything in one long file, you can organize code by topic. For example:",[195,62467,62468,62471,62474],{},[198,62469,62470],{},"One file for math helpers",[198,62472,62473],{},"One file for file handling",[198,62475,62476],{},"One file for user input",[14,62478,62479],{},"This makes larger programs easier to manage.",[14,62481,62482],{},"Modules also let you reuse code in multiple files. If you improve a function in one module, every file that imports it can use the updated version.",[14,62484,62485],{},"Another big benefit is that Python already includes many useful modules in its standard library. For example, you can use modules for math, JSON data, file paths, and system information without writing those tools yourself.",[31,62487,62489],{"id":62488},"types-of-modules-beginners-should-know","Types of modules beginners should know",[14,62491,62492],{},"There are two main types of modules beginners should understand.",[712,62494,62496],{"id":62495},"your-own-modules","Your own modules",[14,62498,62499],{},"These are Python files you create yourself.",[14,62501,466],{},[195,62503,62504,62508,62512],{},[198,62505,62506],{},[18,62507,15445],{},[198,62509,62510],{},[18,62511,15586],{},[198,62513,62514],{},[18,62515,62516],{},"greetings.py",[14,62518,62519],{},"If a file contains Python code, it can often be used as a module.",[712,62521,62523],{"id":62522},"built-in-and-standard-library-modules","Built-in and standard library modules",[14,62525,62526],{},"These are modules that come with Python.",[14,62528,62529],{},"Examples include:",[195,62531,62532,62536,62540,62544],{},[198,62533,62534],{},[18,62535,14900],{},[198,62537,62538],{},[18,62539,15031],{},[198,62541,62542],{},[18,62543,49062],{},[198,62545,62546],{},[18,62547,62548],{},"sys",[14,62550,62551,62552,21,62556,21,62560,25,62564,240],{},"You can learn more from pages like the ",[236,62553,62555],{"href":62554},"\u002Fstandard-library\u002Fpython-math-module-overview\u002F","Python math module overview",[236,62557,62559],{"href":62558},"\u002Fstandard-library\u002Fpython-json-module-overview\u002F","Python json module overview",[236,62561,62563],{"href":62562},"\u002Fstandard-library\u002Fpython-os-module-overview\u002F","Python os module overview",[236,62565,15694],{"href":62566},"\u002Fstandard-library\u002Fpython-sys-module-overview\u002F",[712,62568,62570],{"id":62569},"what-about-third-party-packages","What about third-party packages?",[14,62572,62573],{},"Third-party packages are code written by other people and installed separately.",[14,62575,62576,62577,240],{},"They are different from a simple module, so this page does not cover them in depth. If you want the bigger picture, it also helps to understand ",[236,62578,62580],{"href":62579},"\u002Fglossary\u002Fwhat-is-a-package-in-python\u002F","what a package is in Python",[31,62582,62584],{"id":62583},"basic-example-of-using-a-module","Basic example of using a module",[14,62586,62587,62588,15298],{},"A common beginner example is the ",[18,62589,14900],{},[36,62591,62593],{"className":38,"code":62592,"language":40,"meta":41,"style":41},"import math\n\nresult = math.sqrt(25)\nprint(result)\n",[18,62594,62595,62601,62605,62623],{"__ignoreMap":41},[45,62596,62597,62599],{"class":47,"line":48},[45,62598,14870],{"class":73},[45,62600,14891],{"class":51},[45,62602,62603],{"class":47,"line":63},[45,62604,67],{"emptyLinePlaceholder":66},[45,62606,62607,62609,62611,62613,62615,62617,62619,62621],{"class":47,"line":70},[45,62608,3595],{"class":51},[45,62610,56],{"class":55},[45,62612,15330],{"class":51},[45,62614,240],{"class":85},[45,62616,14905],{"class":2070},[45,62618,96],{"class":85},[45,62620,14942],{"class":59},[45,62622,109],{"class":85},[45,62624,62625,62627,62629,62631],{"class":47,"line":89},[45,62626,2065],{"class":92},[45,62628,96],{"class":85},[45,62630,3620],{"class":2070},[45,62632,109],{"class":85},[14,62634,62635],{},[2421,62636,177],{},[36,62638,62639],{"className":38,"code":14961,"language":40,"meta":41,"style":41},[18,62640,62641],{"__ignoreMap":41},[45,62642,62643],{"class":47,"line":48},[45,62644,14961],{"class":59},[14,62646,62647],{},"What is happening here:",[195,62649,62650,62658,62667],{},[198,62651,62652,62654,62655,62657],{},[18,62653,14973],{}," makes the ",[18,62656,14900],{}," module available",[198,62659,62660,62663,62664,62666],{},[18,62661,62662],{},"math.sqrt(25)"," uses the ",[18,62665,15378],{}," function from that module",[198,62668,62669],{},"The dot means “use this item from that module”",[14,62671,62672],{},"This is one of the most common patterns in Python:",[36,62674,62676],{"className":38,"code":62675,"language":40,"meta":41,"style":41},"module_name.function_name()\n",[18,62677,62678],{"__ignoreMap":41},[45,62679,62680,62683,62685,62687],{"class":47,"line":48},[45,62681,62682],{"class":51},"module_name",[45,62684,240],{"class":85},[45,62686,8275],{"class":2070},[45,62688,2495],{"class":85},[14,62690,62691,62692,240],{},"If you want to go deeper into import syntax later, read ",[236,62693,14860],{"href":58917},[31,62695,62697],{"id":62696},"creating-your-own-module","Creating your own module",[14,62699,62700],{},"You can create your own module with just two files.",[712,62702,62704,62705],{"id":62703},"file-1-greetingspy","File 1: ",[18,62706,62516],{},[36,62708,62710],{"className":38,"code":62709,"language":40,"meta":41,"style":41},"def say_hello(name):\n    return f\"Hello, {name}!\"\n",[18,62711,62712,62724],{"__ignoreMap":41},[45,62713,62714,62716,62718,62720,62722],{"class":47,"line":48},[45,62715,2757],{"class":2440},[45,62717,4596],{"class":2455},[45,62719,96],{"class":85},[45,62721,3096],{"class":3082},[45,62723,2464],{"class":85},[45,62725,62726,62728,62730,62732,62734,62736,62738],{"class":47,"line":63},[45,62727,4681],{"class":73},[45,62729,15469],{"class":2440},[45,62731,5548],{"class":103},[45,62733,5551],{"class":59},[45,62735,3096],{"class":51},[45,62737,5556],{"class":59},[45,62739,15480],{"class":103},[14,62741,62742,62743,240],{},"This file is your module. It contains one function called ",[18,62744,4702],{},[712,62746,62748,62749],{"id":62747},"file-2-mainpy","File 2: ",[18,62750,15486],{},[36,62752,62754],{"className":38,"code":62753,"language":40,"meta":41,"style":41},"import greetings\n\nmessage = greetings.say_hello(\"Maya\")\nprint(message)\n",[18,62755,62756,62763,62767,62790],{"__ignoreMap":41},[45,62757,62758,62760],{"class":47,"line":48},[45,62759,14870],{"class":73},[45,62761,62762],{"class":51}," greetings\n",[45,62764,62765],{"class":47,"line":63},[45,62766,67],{"emptyLinePlaceholder":66},[45,62768,62769,62771,62773,62776,62778,62780,62782,62784,62786,62788],{"class":47,"line":70},[45,62770,15507],{"class":51},[45,62772,56],{"class":55},[45,62774,62775],{"class":51}," greetings",[45,62777,240],{"class":85},[45,62779,4702],{"class":2070},[45,62781,96],{"class":85},[45,62783,100],{"class":99},[45,62785,5737],{"class":103},[45,62787,100],{"class":99},[45,62789,109],{"class":85},[45,62791,62792,62794,62796,62798],{"class":47,"line":89},[45,62793,2065],{"class":92},[45,62795,96],{"class":85},[45,62797,6462],{"class":2070},[45,62799,109],{"class":85},[14,62801,62802],{},[2421,62803,177],{},[36,62805,62807],{"className":38,"code":62806,"language":40,"meta":41,"style":41},"Hello, Maya!\n",[18,62808,62809],{"__ignoreMap":41},[45,62810,62811,62813,62815],{"class":47,"line":48},[45,62812,3226],{"class":51},[45,62814,3079],{"class":85},[45,62816,62817],{"class":51}," Maya!\n",[712,62819,62821],{"id":62820},"how-this-works","How this works",[14,62823,62824],{},"For this simple example:",[195,62826,62827,62830,62834],{},[198,62828,62829],{},"Put both files in the same folder",[198,62831,1082,62832],{},[18,62833,15486],{},[198,62835,62836,62837],{},"Python imports the code from ",[18,62838,62516],{},[14,62840,55622],{},[36,62842,62844],{"className":38,"code":62843,"language":40,"meta":41,"style":41},"greetings.say_hello(\"Maya\")\n",[18,62845,62846],{"__ignoreMap":41},[45,62847,62848,62851,62853,62855,62857,62859,62861,62863],{"class":47,"line":48},[45,62849,62850],{"class":51},"greetings",[45,62852,240],{"class":85},[45,62854,4702],{"class":2070},[45,62856,96],{"class":85},[45,62858,100],{"class":99},[45,62860,5737],{"class":103},[45,62862,100],{"class":99},[45,62864,109],{"class":85},[14,62866,2727],{},[195,62868,62869,62874],{},[198,62870,62871,62872],{},"Use the module named ",[18,62873,62850],{},[198,62875,62876,62877],{},"Then use the function named ",[18,62878,4702],{},[31,62880,62882],{"id":62881},"important-beginner-idea-module-name-dot-function-name","Important beginner idea: module name dot function name",[14,62884,62885],{},"A very important pattern in Python is:",[36,62887,62888],{"className":38,"code":62675,"language":40,"meta":41,"style":41},[18,62889,62890],{"__ignoreMap":41},[45,62891,62892,62894,62896,62898],{"class":47,"line":48},[45,62893,62682],{"class":51},[45,62895,240],{"class":85},[45,62897,8275],{"class":2070},[45,62899,2495],{"class":85},[14,62901,466],{},[36,62903,62905],{"className":38,"code":62904,"language":40,"meta":41,"style":41},"math.sqrt(25)\n",[18,62906,62907],{"__ignoreMap":41},[45,62908,62909,62911,62913,62915,62917,62919],{"class":47,"line":48},[45,62910,14900],{"class":51},[45,62912,240],{"class":85},[45,62914,14905],{"class":2070},[45,62916,96],{"class":85},[45,62918,14942],{"class":59},[45,62920,109],{"class":85},[14,62922,62923],{},"The dot means:",[195,62925,62926,62929],{},[198,62927,62928],{},"start with the module",[198,62930,62931],{},"then access something inside it",[14,62933,62934],{},"This is helpful because it keeps names clear.",[14,62936,62937],{},"For example, many modules could have a function with the same name. Using the module name helps avoid confusion.",[36,62939,62941],{"className":38,"code":62940,"language":40,"meta":41,"style":41},"import math\n\nprint(math.sqrt(16))\n",[18,62942,62943,62949,62953],{"__ignoreMap":41},[45,62944,62945,62947],{"class":47,"line":48},[45,62946,14870],{"class":73},[45,62948,14891],{"class":51},[45,62950,62951],{"class":47,"line":63},[45,62952,67],{"emptyLinePlaceholder":66},[45,62954,62955,62957,62959,62961,62963,62965,62967,62969],{"class":47,"line":70},[45,62956,2065],{"class":92},[45,62958,96],{"class":85},[45,62960,14900],{"class":2070},[45,62962,240],{"class":85},[45,62964,14905],{"class":2070},[45,62966,96],{"class":85},[45,62968,14910],{"class":59},[45,62970,4348],{"class":85},[14,62972,62973,62974,62976,62977,15298],{},"Here, Python knows you want the ",[18,62975,15378],{}," function from the ",[18,62978,14900],{},[31,62980,33115],{"id":33114},[14,62982,62983],{},"This page focuses on the basic idea of modules.",[14,62985,62986,62987,62989],{},"It does ",[2421,62988,2857],{}," explain these topics in detail:",[195,62991,62992,62997,63003],{},[198,62993,62994,62995],{},"Full import syntax, which belongs on ",[236,62996,14860],{"href":58917},[198,62998,62999,63000],{},"Packages, which are covered in ",[236,63001,63002],{"href":62579},"What Is a Package in Python?",[198,63004,63005,63006,63008],{},"Installing third-party tools, which belongs on pages about ",[18,63007,16138],{}," and virtual environments",[31,63010,1337],{"id":1336},[14,63012,63013],{},"Here are some common module-related mistakes.",[712,63015,63017],{"id":63016},"naming-your-file-the-same-as-a-standard-module","Naming your file the same as a standard module",[14,63019,63020,63021,63024,63025,15298],{},"If you create a file named ",[18,63022,63023],{},"math.py",", it can conflict with Python’s real ",[18,63026,14900],{},[14,63028,63029],{},"That can cause confusing import problems.",[712,63031,63033],{"id":63032},"forgetting-that-python-must-be-able-to-find-the-file","Forgetting that Python must be able to find the file",[14,63035,63036],{},"If your own module is in the wrong folder, Python may not find it.",[14,63038,63039],{},"In simple beginner examples, keep both files in the same folder.",[14,63041,63042,63043,1316,63045,14756],{},"If import fails, pages like ",[236,63044,15682],{"href":17444},[236,63046,16395],{"href":17449},[712,63048,63050],{"id":63049},"trying-to-use-a-function-before-importing-the-module","Trying to use a function before importing the module",[14,63052,59540],{},[36,63054,63056],{"className":38,"code":63055,"language":40,"meta":41,"style":41},"print(math.sqrt(25))\n",[18,63057,63058],{"__ignoreMap":41},[45,63059,63060,63062,63064,63066,63068,63070,63072,63074],{"class":47,"line":48},[45,63061,2065],{"class":92},[45,63063,96],{"class":85},[45,63065,14900],{"class":2070},[45,63067,240],{"class":85},[45,63069,14905],{"class":2070},[45,63071,96],{"class":85},[45,63073,14942],{"class":59},[45,63075,4348],{"class":85},[14,63077,30405,63078,63080],{},[18,63079,14900],{}," has not been imported yet.",[14,63082,63083],{},"You need:",[36,63085,63086],{"className":38,"code":62359,"language":40,"meta":41,"style":41},[18,63087,63088,63094,63098],{"__ignoreMap":41},[45,63089,63090,63092],{"class":47,"line":48},[45,63091,14870],{"class":73},[45,63093,14891],{"class":51},[45,63095,63096],{"class":47,"line":63},[45,63097,67],{"emptyLinePlaceholder":66},[45,63099,63100,63102,63104,63106,63108,63110,63112,63114],{"class":47,"line":70},[45,63101,2065],{"class":92},[45,63103,96],{"class":85},[45,63105,14900],{"class":2070},[45,63107,240],{"class":85},[45,63109,14905],{"class":2070},[45,63111,96],{"class":85},[45,63113,14942],{"class":59},[45,63115,4348],{"class":85},[712,63117,63119],{"id":63118},"confusing-modules-with-packages","Confusing modules with packages",[14,63121,63122],{},"A module is usually one file.",[14,63124,63125],{},"A package is a way to organize multiple modules together.",[31,63127,2281],{"id":2280},[712,63129,40374],{"id":63130},"what-is-a-module-in-python",[14,63132,63133],{},"A module is usually a Python file that contains code you can reuse in another Python file.",[712,63135,63137],{"id":63136},"what-is-the-difference-between-a-module-and-a-package","What is the difference between a module and a package?",[14,63139,63140],{},"A module is usually one file. A package is a way to organize multiple modules together.",[712,63142,63144],{"id":63143},"do-i-need-pip-to-use-modules","Do I need pip to use modules?",[14,63146,63147],{},"No. Many modules come with Python already, and you can also create your own modules.",[712,63149,63151],{"id":63150},"why-does-import-fail-for-my-own-module","Why does import fail for my own module?",[14,63153,63154],{},"Common reasons are:",[195,63156,63157,63160,63163],{},[198,63158,63159],{},"The file is in the wrong folder",[198,63161,63162],{},"The name is wrong",[198,63164,63165],{},"The filename conflicts with another module",[31,63167,2342],{"id":2341},[195,63169,63170,63174,63178,63182,63186,63190,63194],{},[198,63171,63172],{},[236,63173,14860],{"href":58917},[198,63175,63176],{},[236,63177,63002],{"href":62579},[198,63179,63180],{},[236,63181,15682],{"href":17444},[198,63183,63184],{},[236,63185,62555],{"href":62554},[198,63187,63188],{},[236,63189,62559],{"href":62558},[198,63191,63192],{},[236,63193,62563],{"href":62562},[198,63195,63196],{},[236,63197,15694],{"href":62566},[2367,63199,63200],{},"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 .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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":41,"searchDepth":63,"depth":63,"links":63202},[63203,63204,63205,63206,63211,63212,63219,63220,63221,63227,63233],{"id":6599,"depth":63,"text":6600},{"id":62414,"depth":63,"text":62415},{"id":62458,"depth":63,"text":62459},{"id":62488,"depth":63,"text":62489,"children":63207},[63208,63209,63210],{"id":62495,"depth":70,"text":62496},{"id":62522,"depth":70,"text":62523},{"id":62569,"depth":70,"text":62570},{"id":62583,"depth":63,"text":62584},{"id":62696,"depth":63,"text":62697,"children":63213},[63214,63216,63218],{"id":62703,"depth":70,"text":63215},"File 1: greetings.py",{"id":62747,"depth":70,"text":63217},"File 2: main.py",{"id":62820,"depth":70,"text":62821},{"id":62881,"depth":63,"text":62882},{"id":33114,"depth":63,"text":33115},{"id":1336,"depth":63,"text":1337,"children":63222},[63223,63224,63225,63226],{"id":63016,"depth":70,"text":63017},{"id":63032,"depth":70,"text":63033},{"id":63049,"depth":70,"text":63050},{"id":63118,"depth":70,"text":63119},{"id":2280,"depth":63,"text":2281,"children":63228},[63229,63230,63231,63232],{"id":63130,"depth":70,"text":40374},{"id":63136,"depth":70,"text":63137},{"id":63143,"depth":70,"text":63144},{"id":63150,"depth":70,"text":63151},{"id":2341,"depth":63,"text":2342},"Master python modules explained in our comprehensive Python beginner guide.",{},{"title":62342,"description":63234},"learn\u002Fpython-modules-explained","349aA4w_kM-rTzqL9prtU-h7pRtcmUhTDdoBg8cAU6Q",{"id":63240,"title":63241,"body":63242,"description":64924,"extension":2399,"meta":64925,"navigation":66,"path":64926,"seo":64927,"stem":64928,"__hash__":64929},"content\u002Flearn\u002Fpython-numbers-explained-int-float-complex.md","Python Numbers Explained (int, float, complex)",{"type":7,"value":63243,"toc":64898},[63244,63247,63250,63253,63268,63276,63278,63303,63307,63311,63316,63319,63339,63344,63356,63420,63422,63445,63449,63453,63455,63471,63476,63490,63498,63574,63576,63614,63618,63621,63629,63635,63637,63654,63657,63705,63707,63740,63747,63751,63758,63811,63813,63860,63863,63867,63870,63893,63954,63956,63978,63981,64006,64010,64018,64023,64073,64075,64095,64098,64102,64110,64118,64188,64190,64212,64221,64230,64264,64266,64279,64282,64295,64299,64302,64336,64338,64351,64353,64367,64369,64387,64392,64397,64399,64402,64406,64411,64448,64459,64462,64513,64520,64524,64527,64546,64548,64556,64561,64565,64568,64587,64592,64595,64603,64606,64610,64613,64644,64647,64650,64685,64690,64722,64730,64734,64739,64753,64757,64768,64773,64776,64793,64795,64799,64806,64810,64818,64822,64830,64834,64839,64843,64851,64853,64895],[10,63245,63241],{"id":63246},"python-numbers-explained-int-float-complex",[14,63248,63249],{},"Python has different number types for different kinds of values.",[14,63251,63252],{},"The three main built-in numeric types are:",[195,63254,63255,63259,63263],{},[198,63256,63257],{},[18,63258,29738],{},[198,63260,63261],{},[18,63262,29743],{},[198,63264,63265],{},[18,63266,63267],{},"complex",[14,63269,63270,63271,1316,63273,63275],{},"If you are just starting Python, you will use ",[18,63272,29738],{},[18,63274,29743],{}," most of the time. This page explains what each type means, how to recognize it, and a few common mistakes beginners run into.",[31,63277,2530],{"id":2529},[195,63279,63280,63283,63292,63300],{},[198,63281,63282],{},"Python has different number types for different kinds of values",[198,63284,63285,63286,21,63288,25,63290],{},"The main built-in numeric types are ",[18,63287,29738],{},[18,63289,29743],{},[18,63291,63267],{},[198,63293,63294,63295,1316,63297,63299],{},"Beginners mostly use ",[18,63296,29738],{},[18,63298,29743],{}," first",[198,63301,63302],{},"This page explains what each type is and how to identify it",[31,63304,63306],{"id":63305},"what-is-an-int-in-python","What is an int in Python?",[14,63308,19451,63309,40799],{},[18,63310,29738],{},[14,63312,62986,63313,63315],{},[2421,63314,2857],{}," have a decimal point.",[14,63317,63318],{},"Examples of integers:",[195,63320,63321,63325,63329,63334],{},[198,63322,63323],{},[18,63324,10482],{},[198,63326,63327],{},[18,63328,3749],{},[198,63330,63331],{},[18,63332,63333],{},"-3",[198,63335,63336],{},[18,63337,63338],{},"100",[14,63340,165,63341,63343],{},[18,63342,29738],{}," for things like:",[195,63345,63346,63349,63351,63353],{},[198,63347,63348],{},"counting items",[198,63350,47002],{},[198,63352,9171],{},[198,63354,63355],{},"whole-number math",[36,63357,63359],{"className":38,"code":63358,"language":40,"meta":41,"style":41},"apples = 5\ntemperature_change = -3\nyear = 2024\n\nprint(apples)\nprint(type(apples))\n",[18,63360,63361,63370,63381,63391,63395,63406],{"__ignoreMap":41},[45,63362,63363,63366,63368],{"class":47,"line":48},[45,63364,63365],{"class":51},"apples ",[45,63367,56],{"class":55},[45,63369,733],{"class":59},[45,63371,63372,63375,63377,63379],{"class":47,"line":63},[45,63373,63374],{"class":51},"temperature_change ",[45,63376,56],{"class":55},[45,63378,28089],{"class":55},[45,63380,13046],{"class":59},[45,63382,63383,63386,63388],{"class":47,"line":70},[45,63384,63385],{"class":51},"year ",[45,63387,56],{"class":55},[45,63389,63390],{"class":59}," 2024\n",[45,63392,63393],{"class":47,"line":89},[45,63394,67],{"emptyLinePlaceholder":66},[45,63396,63397,63399,63401,63404],{"class":47,"line":112},[45,63398,2065],{"class":92},[45,63400,96],{"class":85},[45,63402,63403],{"class":2070},"apples",[45,63405,109],{"class":85},[45,63407,63408,63410,63412,63414,63416,63418],{"class":47,"line":126},[45,63409,2065],{"class":92},[45,63411,96],{"class":85},[45,63413,4340],{"class":2109},[45,63415,96],{"class":85},[45,63417,63403],{"class":2070},[45,63419,4348],{"class":85},[14,63421,177],{},[36,63423,63425],{"className":38,"code":63424,"language":40,"meta":41,"style":41},"5\n\u003Cclass 'int'>\n",[18,63426,63427,63431],{"__ignoreMap":41},[45,63428,63429],{"class":47,"line":48},[45,63430,11570],{"class":59},[45,63432,63433,63435,63437,63439,63441,63443],{"class":47,"line":63},[45,63434,79],{"class":55},[45,63436,2441],{"class":2440},[45,63438,9199],{"class":99},[45,63440,29738],{"class":103},[45,63442,7901],{"class":99},[45,63444,13484],{"class":55},[31,63446,63448],{"id":63447},"what-is-a-float-in-python","What is a float in Python?",[14,63450,2419,63451,40875],{},[18,63452,29743],{},[14,63454,40802],{},[195,63456,63457,63461,63466],{},[198,63458,63459],{},[18,63460,40884],{},[198,63462,63463],{},[18,63464,63465],{},"2.0",[198,63467,63468],{},[18,63469,63470],{},"-0.5",[14,63472,165,63473,63475],{},[18,63474,29743],{}," for values like:",[195,63477,63478,63481,63484,63487],{},[198,63479,63480],{},"measurements",[198,63482,63483],{},"averages",[198,63485,63486],{},"prices",[198,63488,63489],{},"division results",[14,63491,63492,63493,6578,63495,63497],{},"Even ",[18,63494,63465],{},[18,63496,29743],{},", because it includes a decimal point.",[36,63499,63501],{"className":38,"code":63500,"language":40,"meta":41,"style":41},"price = 19.99\naverage = 4.5\nexact_two = 2.0\n\nprint(price)\nprint(type(price))\nprint(type(exact_two))\n",[18,63502,63503,63511,63521,63531,63535,63545,63559],{"__ignoreMap":41},[45,63504,63505,63507,63509],{"class":47,"line":48},[45,63506,40904],{"class":51},[45,63508,56],{"class":55},[45,63510,40909],{"class":59},[45,63512,63513,63516,63518],{"class":47,"line":63},[45,63514,63515],{"class":51},"average ",[45,63517,56],{"class":55},[45,63519,63520],{"class":59}," 4.5\n",[45,63522,63523,63526,63528],{"class":47,"line":70},[45,63524,63525],{"class":51},"exact_two ",[45,63527,56],{"class":55},[45,63529,63530],{"class":59}," 2.0\n",[45,63532,63533],{"class":47,"line":89},[45,63534,67],{"emptyLinePlaceholder":66},[45,63536,63537,63539,63541,63543],{"class":47,"line":112},[45,63538,2065],{"class":92},[45,63540,96],{"class":85},[45,63542,19985],{"class":2070},[45,63544,109],{"class":85},[45,63546,63547,63549,63551,63553,63555,63557],{"class":47,"line":126},[45,63548,2065],{"class":92},[45,63550,96],{"class":85},[45,63552,4340],{"class":2109},[45,63554,96],{"class":85},[45,63556,19985],{"class":2070},[45,63558,4348],{"class":85},[45,63560,63561,63563,63565,63567,63569,63572],{"class":47,"line":142},[45,63562,2065],{"class":92},[45,63564,96],{"class":85},[45,63566,4340],{"class":2109},[45,63568,96],{"class":85},[45,63570,63571],{"class":2070},"exact_two",[45,63573,4348],{"class":85},[14,63575,177],{},[36,63577,63579],{"className":38,"code":63578,"language":40,"meta":41,"style":41},"19.99\n\u003Cclass 'float'>\n\u003Cclass 'float'>\n",[18,63580,63581,63586,63600],{"__ignoreMap":41},[45,63582,63583],{"class":47,"line":48},[45,63584,63585],{"class":59},"19.99\n",[45,63587,63588,63590,63592,63594,63596,63598],{"class":47,"line":63},[45,63589,79],{"class":55},[45,63591,2441],{"class":2440},[45,63593,9199],{"class":99},[45,63595,29743],{"class":103},[45,63597,7901],{"class":99},[45,63599,13484],{"class":55},[45,63601,63602,63604,63606,63608,63610,63612],{"class":47,"line":70},[45,63603,79],{"class":55},[45,63605,2441],{"class":2440},[45,63607,9199],{"class":99},[45,63609,29743],{"class":103},[45,63611,7901],{"class":99},[45,63613,13484],{"class":55},[31,63615,63617],{"id":63616},"what-is-a-complex-number-in-python","What is a complex number in Python?",[14,63619,63620],{},"A complex number has:",[195,63622,63623,63626],{},[198,63624,63625],{},"a real part",[198,63627,63628],{},"an imaginary part",[14,63630,63631,63632,240],{},"In Python, the imaginary part uses ",[18,63633,63634],{},"j",[14,63636,40802],{},[195,63638,63639,63644,63649],{},[198,63640,63641],{},[18,63642,63643],{},"2 + 3j",[198,63645,63646],{},[18,63647,63648],{},"5j",[198,63650,63651],{},[18,63652,63653],{},"-1 + 0j",[14,63655,63656],{},"Complex numbers are real Python number types, but they are much less common in beginner programs.",[36,63658,63660],{"className":38,"code":63659,"language":40,"meta":41,"style":41},"number = 2 + 3j\n\nprint(number)\nprint(type(number))\n",[18,63661,63662,63677,63681,63691],{"__ignoreMap":41},[45,63663,63664,63666,63668,63670,63672,63674],{"class":47,"line":48},[45,63665,728],{"class":51},[45,63667,56],{"class":55},[45,63669,3754],{"class":59},[45,63671,3775],{"class":55},[45,63673,777],{"class":59},[45,63675,63676],{"class":2440},"j\n",[45,63678,63679],{"class":47,"line":63},[45,63680,67],{"emptyLinePlaceholder":66},[45,63682,63683,63685,63687,63689],{"class":47,"line":70},[45,63684,2065],{"class":92},[45,63686,96],{"class":85},[45,63688,13546],{"class":2070},[45,63690,109],{"class":85},[45,63692,63693,63695,63697,63699,63701,63703],{"class":47,"line":89},[45,63694,2065],{"class":92},[45,63696,96],{"class":85},[45,63698,4340],{"class":2109},[45,63700,96],{"class":85},[45,63702,13546],{"class":2070},[45,63704,4348],{"class":85},[14,63706,177],{},[36,63708,63710],{"className":38,"code":63709,"language":40,"meta":41,"style":41},"(2+3j)\n\u003Cclass 'complex'>\n",[18,63711,63712,63726],{"__ignoreMap":41},[45,63713,63714,63716,63718,63720,63722,63724],{"class":47,"line":48},[45,63715,96],{"class":85},[45,63717,9367],{"class":59},[45,63719,8850],{"class":55},[45,63721,11184],{"class":59},[45,63723,63634],{"class":2440},[45,63725,109],{"class":85},[45,63727,63728,63730,63732,63734,63736,63738],{"class":47,"line":63},[45,63729,79],{"class":55},[45,63731,2441],{"class":2440},[45,63733,9199],{"class":99},[45,63735,63267],{"class":103},[45,63737,7901],{"class":99},[45,63739,13484],{"class":55},[14,63741,63742,63743,1316,63745,8558],{},"If you are new to Python, it is completely normal to focus on ",[18,63744,29738],{},[18,63746,29743],{},[31,63748,63750],{"id":63749},"how-to-check-a-number-type","How to check a number type",[14,63752,42429,63753,63757],{},[236,63754,63755,30521],{"href":41690},[18,63756,40437],{}," to see the type of a value.",[36,63759,63761],{"className":38,"code":63760,"language":40,"meta":41,"style":41},"print(type(5))\nprint(type(3.14))\nprint(type(2 + 3j))\n",[18,63762,63763,63777,63791],{"__ignoreMap":41},[45,63764,63765,63767,63769,63771,63773,63775],{"class":47,"line":48},[45,63766,2065],{"class":92},[45,63768,96],{"class":85},[45,63770,4340],{"class":2109},[45,63772,96],{"class":85},[45,63774,3749],{"class":59},[45,63776,4348],{"class":85},[45,63778,63779,63781,63783,63785,63787,63789],{"class":47,"line":63},[45,63780,2065],{"class":92},[45,63782,96],{"class":85},[45,63784,4340],{"class":2109},[45,63786,96],{"class":85},[45,63788,40884],{"class":59},[45,63790,4348],{"class":85},[45,63792,63793,63795,63797,63799,63801,63803,63805,63807,63809],{"class":47,"line":70},[45,63794,2065],{"class":92},[45,63796,96],{"class":85},[45,63798,4340],{"class":2109},[45,63800,96],{"class":85},[45,63802,9367],{"class":59},[45,63804,3775],{"class":55},[45,63806,777],{"class":59},[45,63808,63634],{"class":2440},[45,63810,4348],{"class":85},[14,63812,177],{},[36,63814,63816],{"className":38,"code":63815,"language":40,"meta":41,"style":41},"\u003Cclass 'int'>\n\u003Cclass 'float'>\n\u003Cclass 'complex'>\n",[18,63817,63818,63832,63846],{"__ignoreMap":41},[45,63819,63820,63822,63824,63826,63828,63830],{"class":47,"line":48},[45,63821,79],{"class":55},[45,63823,2441],{"class":2440},[45,63825,9199],{"class":99},[45,63827,29738],{"class":103},[45,63829,7901],{"class":99},[45,63831,13484],{"class":55},[45,63833,63834,63836,63838,63840,63842,63844],{"class":47,"line":63},[45,63835,79],{"class":55},[45,63837,2441],{"class":2440},[45,63839,9199],{"class":99},[45,63841,29743],{"class":103},[45,63843,7901],{"class":99},[45,63845,13484],{"class":55},[45,63847,63848,63850,63852,63854,63856,63858],{"class":47,"line":70},[45,63849,79],{"class":55},[45,63851,2441],{"class":2440},[45,63853,9199],{"class":99},[45,63855,63267],{"class":103},[45,63857,7901],{"class":99},[45,63859,13484],{"class":55},[14,63861,63862],{},"This is very useful when you are debugging a program and want to see what kind of value you actually have.",[31,63864,63866],{"id":63865},"basic-operations-with-numbers","Basic operations with numbers",[14,63868,63869],{},"You can do basic math with Python numbers:",[195,63871,63872,63877,63882,63887],{},[198,63873,63874,63875],{},"addition with ",[18,63876,8850],{},[198,63878,63879,63880],{},"subtraction with ",[18,63881,218],{},[198,63883,63884,63885],{},"multiplication with ",[18,63886,5601],{},[198,63888,63889,63890],{},"division with ",[18,63891,63892],{},"\u002F",[36,63894,63896],{"className":38,"code":63895,"language":40,"meta":41,"style":41},"print(5 + 2)\nprint(5 - 2)\nprint(5 * 2)\nprint(5 \u002F 2)\n",[18,63897,63898,63912,63926,63940],{"__ignoreMap":41},[45,63899,63900,63902,63904,63906,63908,63910],{"class":47,"line":48},[45,63901,2065],{"class":92},[45,63903,96],{"class":85},[45,63905,3749],{"class":59},[45,63907,3775],{"class":55},[45,63909,3754],{"class":59},[45,63911,109],{"class":85},[45,63913,63914,63916,63918,63920,63922,63924],{"class":47,"line":63},[45,63915,2065],{"class":92},[45,63917,96],{"class":85},[45,63919,3749],{"class":59},[45,63921,28089],{"class":55},[45,63923,3754],{"class":59},[45,63925,109],{"class":85},[45,63927,63928,63930,63932,63934,63936,63938],{"class":47,"line":70},[45,63929,2065],{"class":92},[45,63931,96],{"class":85},[45,63933,3749],{"class":59},[45,63935,3559],{"class":55},[45,63937,3754],{"class":59},[45,63939,109],{"class":85},[45,63941,63942,63944,63946,63948,63950,63952],{"class":47,"line":89},[45,63943,2065],{"class":92},[45,63945,96],{"class":85},[45,63947,3749],{"class":59},[45,63949,45881],{"class":55},[45,63951,3754],{"class":59},[45,63953,109],{"class":85},[14,63955,177],{},[36,63957,63959],{"className":38,"code":63958,"language":40,"meta":41,"style":41},"7\n3\n10\n2.5\n",[18,63960,63961,63965,63969,63973],{"__ignoreMap":41},[45,63962,63963],{"class":47,"line":48},[45,63964,35999],{"class":59},[45,63966,63967],{"class":47,"line":63},[45,63968,13046],{"class":59},[45,63970,63971],{"class":47,"line":70},[45,63972,13262],{"class":59},[45,63974,63975],{"class":47,"line":89},[45,63976,63977],{"class":59},"2.5\n",[14,63979,63980],{},"A few important rules:",[195,63982,63983,63991,63999],{},[198,63984,63985,63986,63988,63989],{},"adding two ",[18,63987,29738],{}," values usually gives an ",[18,63990,29738],{},[198,63992,63993,63994,63996,63997],{},"using a ",[18,63995,29743],{}," in an expression usually gives a ",[18,63998,29743],{},[198,64000,63889,64001,64003,64004],{},[18,64002,63892],{}," returns a ",[18,64005,29743],{},[31,64007,64009],{"id":64008},"int-and-float-together","int and float together",[14,64011,64012,64013,1316,64015,64017],{},"Python can use ",[18,64014,29738],{},[18,64016,29743],{}," in the same expression.",[14,64019,64020,64021,240],{},"When that happens, the result usually becomes a ",[18,64022,29743],{},[36,64024,64026],{"className":38,"code":64025,"language":40,"meta":41,"style":41},"print(5 + 2.0)\nprint(10 - 3.5)\nprint(4 * 1.5)\n",[18,64027,64028,64043,64058],{"__ignoreMap":41},[45,64029,64030,64032,64034,64036,64038,64041],{"class":47,"line":48},[45,64031,2065],{"class":92},[45,64033,96],{"class":85},[45,64035,3749],{"class":59},[45,64037,3775],{"class":55},[45,64039,64040],{"class":59}," 2.0",[45,64042,109],{"class":85},[45,64044,64045,64047,64049,64051,64053,64056],{"class":47,"line":63},[45,64046,2065],{"class":92},[45,64048,96],{"class":85},[45,64050,11786],{"class":59},[45,64052,28089],{"class":55},[45,64054,64055],{"class":59}," 3.5",[45,64057,109],{"class":85},[45,64059,64060,64062,64064,64066,64068,64071],{"class":47,"line":70},[45,64061,2065],{"class":92},[45,64063,96],{"class":85},[45,64065,3584],{"class":59},[45,64067,3559],{"class":55},[45,64069,64070],{"class":59}," 1.5",[45,64072,109],{"class":85},[14,64074,177],{},[36,64076,64078],{"className":38,"code":64077,"language":40,"meta":41,"style":41},"7.0\n6.5\n6.0\n",[18,64079,64080,64085,64090],{"__ignoreMap":41},[45,64081,64082],{"class":47,"line":48},[45,64083,64084],{"class":59},"7.0\n",[45,64086,64087],{"class":47,"line":63},[45,64088,64089],{"class":59},"6.5\n",[45,64091,64092],{"class":47,"line":70},[45,64093,64094],{"class":59},"6.0\n",[14,64096,64097],{},"This is normal. Python keeps the decimal information instead of removing it.",[31,64099,64101],{"id":64100},"converting-between-number-types","Converting between number types",[14,64103,165,64104,64109],{},[236,64105,64107],{"href":64106},"\u002Freference\u002Fpython-int-function-explained\u002F",[18,64108,41891],{}," to convert a value to an integer.",[14,64111,165,64112,64117],{},[236,64113,64115],{"href":64114},"\u002Freference\u002Fpython-float-function-explained\u002F",[18,64116,41896],{}," to convert a value to a float.",[36,64119,64121],{"className":38,"code":64120,"language":40,"meta":41,"style":41},"print(int(3.9))\nprint(float(5))\nprint(int(\"10\"))\nprint(float(\"3.14\"))\n",[18,64122,64123,64138,64152,64170],{"__ignoreMap":41},[45,64124,64125,64127,64129,64131,64133,64136],{"class":47,"line":48},[45,64126,2065],{"class":92},[45,64128,96],{"class":85},[45,64130,29738],{"class":2109},[45,64132,96],{"class":85},[45,64134,64135],{"class":59},"3.9",[45,64137,4348],{"class":85},[45,64139,64140,64142,64144,64146,64148,64150],{"class":47,"line":63},[45,64141,2065],{"class":92},[45,64143,96],{"class":85},[45,64145,29743],{"class":2109},[45,64147,96],{"class":85},[45,64149,3749],{"class":59},[45,64151,4348],{"class":85},[45,64153,64154,64156,64158,64160,64162,64164,64166,64168],{"class":47,"line":70},[45,64155,2065],{"class":92},[45,64157,96],{"class":85},[45,64159,29738],{"class":2109},[45,64161,96],{"class":85},[45,64163,100],{"class":99},[45,64165,11786],{"class":103},[45,64167,100],{"class":99},[45,64169,4348],{"class":85},[45,64171,64172,64174,64176,64178,64180,64182,64184,64186],{"class":47,"line":89},[45,64173,2065],{"class":92},[45,64175,96],{"class":85},[45,64177,29743],{"class":2109},[45,64179,96],{"class":85},[45,64181,100],{"class":99},[45,64183,40884],{"class":103},[45,64185,100],{"class":99},[45,64187,4348],{"class":85},[14,64189,177],{},[36,64191,64193],{"className":38,"code":64192,"language":40,"meta":41,"style":41},"3\n5.0\n10\n3.14\n",[18,64194,64195,64199,64203,64207],{"__ignoreMap":41},[45,64196,64197],{"class":47,"line":48},[45,64198,13046],{"class":59},[45,64200,64201],{"class":47,"line":63},[45,64202,14961],{"class":59},[45,64204,64205],{"class":47,"line":70},[45,64206,13262],{"class":59},[45,64208,64209],{"class":47,"line":89},[45,64210,64211],{"class":59},"3.14\n",[14,64213,64214,64215,29849,64218,64220],{},"Important: ",[18,64216,64217],{},"int(3.9)",[2421,64219,2857],{}," round. It removes the decimal part.",[14,64222,64223,64224,240],{},"If you want rounding behavior, use ",[236,64225,64227],{"href":64226},"\u002Freference\u002Fpython-round-function-explained\u002F",[18,64228,64229],{},"round()",[36,64231,64233],{"className":38,"code":64232,"language":40,"meta":41,"style":41},"print(int(3.9))\nprint(round(3.9))\n",[18,64234,64235,64249],{"__ignoreMap":41},[45,64236,64237,64239,64241,64243,64245,64247],{"class":47,"line":48},[45,64238,2065],{"class":92},[45,64240,96],{"class":85},[45,64242,29738],{"class":2109},[45,64244,96],{"class":85},[45,64246,64135],{"class":59},[45,64248,4348],{"class":85},[45,64250,64251,64253,64255,64258,64260,64262],{"class":47,"line":63},[45,64252,2065],{"class":92},[45,64254,96],{"class":85},[45,64256,64257],{"class":92},"round",[45,64259,96],{"class":85},[45,64261,64135],{"class":59},[45,64263,4348],{"class":85},[14,64265,177],{},[36,64267,64269],{"className":38,"code":64268,"language":40,"meta":41,"style":41},"3\n4\n",[18,64270,64271,64275],{"__ignoreMap":41},[45,64272,64273],{"class":47,"line":48},[45,64274,13046],{"class":59},[45,64276,64277],{"class":47,"line":63},[45,64278,13681],{"class":59},[14,64280,64281],{},"If you are converting text input into numbers, these guides can help:",[195,64283,64284,64289],{},[198,64285,64286],{},[236,64287,64288],{"href":42107},"How to convert a string to an int in Python",[198,64290,64291],{},[236,64292,64294],{"href":64293},"\u002Fhow-to\u002Fhow-to-convert-string-to-float-in-python\u002F","How to convert a string to a float in Python",[31,64296,64298],{"id":64297},"important-beginner-note-about-division","Important beginner note about division",[14,64300,64301],{},"This is one of the most common beginner surprises:",[36,64303,64305],{"className":38,"code":64304,"language":40,"meta":41,"style":41},"print(10 \u002F 2)\nprint(10 \u002F\u002F 3)\n",[18,64306,64307,64321],{"__ignoreMap":41},[45,64308,64309,64311,64313,64315,64317,64319],{"class":47,"line":48},[45,64310,2065],{"class":92},[45,64312,96],{"class":85},[45,64314,11786],{"class":59},[45,64316,45881],{"class":55},[45,64318,3754],{"class":59},[45,64320,109],{"class":85},[45,64322,64323,64325,64327,64329,64332,64334],{"class":47,"line":63},[45,64324,2065],{"class":92},[45,64326,96],{"class":85},[45,64328,11786],{"class":59},[45,64330,64331],{"class":55}," \u002F\u002F",[45,64333,777],{"class":59},[45,64335,109],{"class":85},[14,64337,177],{},[36,64339,64341],{"className":38,"code":64340,"language":40,"meta":41,"style":41},"5.0\n3\n",[18,64342,64343,64347],{"__ignoreMap":41},[45,64344,64345],{"class":47,"line":48},[45,64346,14961],{"class":59},[45,64348,64349],{"class":47,"line":63},[45,64350,13046],{"class":59},[14,64352,36991],{},[195,64354,64355,64361],{},[198,64356,64357,64003,64359],{},[18,64358,63892],{},[18,64360,29743],{},[198,64362,64363,64366],{},[18,64364,64365],{},"\u002F\u002F"," does floor division",[14,64368,15572],{},[195,64370,64371,64380],{},[198,64372,64373,64376,64377],{},[18,64374,64375],{},"10 \u002F 2"," gives ",[18,64378,64379],{},"5.0",[198,64381,64382,64376,64385],{},[18,64383,64384],{},"10 \u002F\u002F 3",[18,64386,11184],{},[14,64388,165,64389,64391],{},[18,64390,63892],{}," when you want normal division.",[14,64393,165,64394,64396],{},[18,64395,64365],{}," when you want a whole-number result.",[31,64398,1337],{"id":1336},[14,64400,64401],{},"Here are some common problems when working with numbers in Python.",[712,64403,64405],{"id":64404},"_1-confusing-numbers-with-strings-from-input","1. Confusing numbers with strings from input()",[14,64407,1732,64408,64410],{},[18,64409,18020],{}," function returns text, not a number.",[36,64412,64414],{"className":38,"code":64413,"language":40,"meta":41,"style":41},"age = input(\"Enter your age: \")\nprint(type(age))\n",[18,64415,64416,64434],{"__ignoreMap":41},[45,64417,64418,64420,64422,64424,64426,64428,64430,64432],{"class":47,"line":48},[45,64419,52],{"class":51},[45,64421,56],{"class":55},[45,64423,18043],{"class":92},[45,64425,96],{"class":85},[45,64427,100],{"class":99},[45,64429,39490],{"class":103},[45,64431,100],{"class":99},[45,64433,109],{"class":85},[45,64435,64436,64438,64440,64442,64444,64446],{"class":47,"line":63},[45,64437,2065],{"class":92},[45,64439,96],{"class":85},[45,64441,4340],{"class":2109},[45,64443,96],{"class":85},[45,64445,2082],{"class":2070},[45,64447,4348],{"class":85},[14,64449,64450,64451,64453,64454,64456,64457,240],{},"If the user enters ",[18,64452,14942],{},", the type is still ",[18,64455,29733],{},", not ",[18,64458,29738],{},[14,64460,64461],{},"To fix this, convert the value:",[36,64463,64465],{"className":38,"code":64464,"language":40,"meta":41,"style":41},"age = int(input(\"Enter your age: \"))\nprint(age)\nprint(type(age))\n",[18,64466,64467,64489,64499],{"__ignoreMap":41},[45,64468,64469,64471,64473,64475,64477,64479,64481,64483,64485,64487],{"class":47,"line":48},[45,64470,52],{"class":51},[45,64472,56],{"class":55},[45,64474,41669],{"class":2109},[45,64476,96],{"class":85},[45,64478,45859],{"class":92},[45,64480,96],{"class":85},[45,64482,100],{"class":99},[45,64484,39490],{"class":103},[45,64486,100],{"class":99},[45,64488,4348],{"class":85},[45,64490,64491,64493,64495,64497],{"class":47,"line":63},[45,64492,2065],{"class":92},[45,64494,96],{"class":85},[45,64496,2082],{"class":2070},[45,64498,109],{"class":85},[45,64500,64501,64503,64505,64507,64509,64511],{"class":47,"line":70},[45,64502,2065],{"class":92},[45,64504,96],{"class":85},[45,64506,4340],{"class":2109},[45,64508,96],{"class":85},[45,64510,2082],{"class":2070},[45,64512,4348],{"class":85},[14,64514,64515,64516,240],{},"If the text cannot be converted, you may get a ",[236,64517,64518],{"href":42386},[18,64519,42387],{},[712,64521,64523],{"id":64522},"_2-expecting-int39-to-round-up","2. Expecting int(3.9) to round up",[14,64525,64526],{},"This does not round:",[36,64528,64530],{"className":38,"code":64529,"language":40,"meta":41,"style":41},"print(int(3.9))\n",[18,64531,64532],{"__ignoreMap":41},[45,64533,64534,64536,64538,64540,64542,64544],{"class":47,"line":48},[45,64535,2065],{"class":92},[45,64537,96],{"class":85},[45,64539,29738],{"class":2109},[45,64541,96],{"class":85},[45,64543,64135],{"class":59},[45,64545,4348],{"class":85},[14,64547,177],{},[36,64549,64550],{"className":38,"code":13046,"language":40,"meta":41,"style":41},[18,64551,64552],{"__ignoreMap":41},[45,64553,64554],{"class":47,"line":48},[45,64555,13046],{"class":59},[14,64557,64558,64559,25114],{},"If you want rounding, use ",[18,64560,64229],{},[712,64562,64564],{"id":64563},"_3-forgetting-that-returns-a-float","3. Forgetting that \u002F returns a float",[14,64566,64567],{},"Many beginners expect this:",[36,64569,64571],{"className":38,"code":64570,"language":40,"meta":41,"style":41},"print(10 \u002F 2)\n",[18,64572,64573],{"__ignoreMap":41},[45,64574,64575,64577,64579,64581,64583,64585],{"class":47,"line":48},[45,64576,2065],{"class":92},[45,64578,96],{"class":85},[45,64580,11786],{"class":59},[45,64582,45881],{"class":55},[45,64584,3754],{"class":59},[45,64586,109],{"class":85},[14,64588,64589,64590,240],{},"to return ",[18,64591,3749],{},[14,64593,64594],{},"But it returns:",[36,64596,64597],{"className":38,"code":14961,"language":40,"meta":41,"style":41},[18,64598,64599],{"__ignoreMap":41},[45,64600,64601],{"class":47,"line":48},[45,64602,14961],{"class":59},[14,64604,64605],{},"That is correct behavior in Python.",[712,64607,64609],{"id":64608},"_4-trying-to-mix-strings-and-numbers-with","4. Trying to mix strings and numbers with +",[14,64611,64612],{},"This causes an error:",[36,64614,64616],{"className":38,"code":64615,"language":40,"meta":41,"style":41},"age = 25\nprint(\"Age: \" + age)\n",[18,64617,64618,64626],{"__ignoreMap":41},[45,64619,64620,64622,64624],{"class":47,"line":48},[45,64621,52],{"class":51},[45,64623,56],{"class":55},[45,64625,30102],{"class":59},[45,64627,64628,64630,64632,64634,64636,64638,64640,64642],{"class":47,"line":63},[45,64629,2065],{"class":92},[45,64631,96],{"class":85},[45,64633,100],{"class":99},[45,64635,46584],{"class":103},[45,64637,100],{"class":99},[45,64639,3775],{"class":55},[45,64641,3305],{"class":2070},[45,64643,109],{"class":85},[14,64645,64646],{},"Python cannot join text and a number directly like that.",[14,64648,64649],{},"Fix it by converting the number to a string:",[36,64651,64653],{"className":38,"code":64652,"language":40,"meta":41,"style":41},"age = 25\nprint(\"Age: \" + str(age))\n",[18,64654,64655,64663],{"__ignoreMap":41},[45,64656,64657,64659,64661],{"class":47,"line":48},[45,64658,52],{"class":51},[45,64660,56],{"class":55},[45,64662,30102],{"class":59},[45,64664,64665,64667,64669,64671,64673,64675,64677,64679,64681,64683],{"class":47,"line":63},[45,64666,2065],{"class":92},[45,64668,96],{"class":85},[45,64670,100],{"class":99},[45,64672,46584],{"class":103},[45,64674,100],{"class":99},[45,64676,3775],{"class":55},[45,64678,38516],{"class":2109},[45,64680,96],{"class":85},[45,64682,2082],{"class":2070},[45,64684,4348],{"class":85},[14,64686,64687,64688,457],{},"Or use commas in ",[18,64689,15833],{},[36,64691,64693],{"className":38,"code":64692,"language":40,"meta":41,"style":41},"age = 25\nprint(\"Age:\", age)\n",[18,64694,64695,64703],{"__ignoreMap":41},[45,64696,64697,64699,64701],{"class":47,"line":48},[45,64698,52],{"class":51},[45,64700,56],{"class":55},[45,64702,30102],{"class":59},[45,64704,64705,64707,64709,64711,64714,64716,64718,64720],{"class":47,"line":63},[45,64706,2065],{"class":92},[45,64708,96],{"class":85},[45,64710,100],{"class":99},[45,64712,64713],{"class":103},"Age:",[45,64715,100],{"class":99},[45,64717,3079],{"class":85},[45,64719,3305],{"class":2070},[45,64721,109],{"class":85},[14,64723,64724,64725,240],{},"A related error is ",[236,64726,64727],{"href":42380},[18,64728,64729],{},"TypeError: can only concatenate str (not \"int\") to str",[31,64731,64733],{"id":64732},"when-to-use-each-number-type","When to use each number type",[14,64735,165,64736,64738],{},[18,64737,29738],{}," when you need:",[195,64740,64741,64744,64747,64750],{},[198,64742,64743],{},"counts",[198,64745,64746],{},"positions",[198,64748,64749],{},"indexes",[198,64751,64752],{},"whole numbers",[14,64754,165,64755,64738],{},[18,64756,29743],{},[195,64758,64759,64762,64764,64766],{},[198,64760,64761],{},"decimal values",[198,64763,63480],{},[198,64765,63486],{},[198,64767,63483],{},[14,64769,165,64770,64772],{},[18,64771,63267],{}," only when you specifically need imaginary numbers.",[14,64774,64775],{},"For most beginner programs:",[195,64777,64778,64783,64788],{},[198,64779,64780,64782],{},[18,64781,29738],{}," is very common",[198,64784,64785,64787],{},[18,64786,29743],{}," is also very common",[198,64789,64790,64792],{},[18,64791,63267],{}," is much less common",[31,64794,2281],{"id":2280},[712,64796,64798],{"id":64797},"what-is-the-difference-between-int-and-float-in-python","What is the difference between int and float in Python?",[14,64800,19451,64801,64803,64804,40875],{},[18,64802,29738],{}," is a whole number. A ",[18,64805,29743],{},[712,64807,64809],{"id":64808},"why-does-python-return-50-instead-of-5","Why does Python return 5.0 instead of 5?",[14,64811,1732,64812,64814,64815,64817],{},[18,64813,63892],{}," operator returns a ",[18,64816,29743],{},", even when the result looks like a whole number.",[712,64819,64821],{"id":64820},"does-int-round-a-number","Does int() round a number?",[14,64823,2302,64824,64826,64827,64829],{},[18,64825,41891],{}," removes the decimal part. Use ",[18,64828,64229],{}," if you want rounding.",[712,64831,64833],{"id":64832},"what-does-j-mean-in-a-complex-number","What does j mean in a complex number?",[14,64835,14993,64836,64838],{},[18,64837,63634],{}," represents the imaginary part of a complex number.",[712,64840,64842],{"id":64841},"which-number-type-should-beginners-focus-on-first","Which number type should beginners focus on first?",[14,64844,64845,64846,1316,64848,64850],{},"Start with ",[18,64847,29738],{},[18,64849,29743],{},". Complex numbers are usually needed later.",[31,64852,2342],{"id":2341},[195,64854,64855,64859,64863,64869,64875,64881,64887,64891],{},[198,64856,64857],{},[236,64858,35582],{"href":35108},[198,64860,64861],{},[236,64862,41082],{"href":41081},[198,64864,64865],{},[236,64866,18104,64867,19075],{"href":41690},[18,64868,40437],{},[198,64870,64871],{},[236,64872,18104,64873,19075],{"href":64106},[18,64874,41891],{},[198,64876,64877],{},[236,64878,18104,64879,19075],{"href":64114},[18,64880,41896],{},[198,64882,64883],{},[236,64884,18104,64885,19075],{"href":64226},[18,64886,64229],{},[198,64888,64889],{},[236,64890,64288],{"href":42107},[198,64892,64893],{},[236,64894,64294],{"href":64293},[2367,64896,64897],{},"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}",{"title":41,"searchDepth":63,"depth":63,"links":64899},[64900,64901,64902,64903,64904,64905,64906,64907,64908,64909,64915,64916,64923],{"id":2529,"depth":63,"text":2530},{"id":63305,"depth":63,"text":63306},{"id":63447,"depth":63,"text":63448},{"id":63616,"depth":63,"text":63617},{"id":63749,"depth":63,"text":63750},{"id":63865,"depth":63,"text":63866},{"id":64008,"depth":63,"text":64009},{"id":64100,"depth":63,"text":64101},{"id":64297,"depth":63,"text":64298},{"id":1336,"depth":63,"text":1337,"children":64910},[64911,64912,64913,64914],{"id":64404,"depth":70,"text":64405},{"id":64522,"depth":70,"text":64523},{"id":64563,"depth":70,"text":64564},{"id":64608,"depth":70,"text":64609},{"id":64732,"depth":63,"text":64733},{"id":2280,"depth":63,"text":2281,"children":64917},[64918,64919,64920,64921,64922],{"id":64797,"depth":70,"text":64798},{"id":64808,"depth":70,"text":64809},{"id":64820,"depth":70,"text":64821},{"id":64832,"depth":70,"text":64833},{"id":64841,"depth":70,"text":64842},{"id":2341,"depth":63,"text":2342},"Master python numbers explained int float complex in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex",{"title":63241,"description":64924},"learn\u002Fpython-numbers-explained-int-float-complex","8Eh9DQ0F8GWahHQd5XHN_E7438x2MZCzj3HaLsgiBpM",{"id":64931,"title":64932,"body":64933,"description":66109,"extension":2399,"meta":66110,"navigation":66,"path":66111,"seo":66112,"stem":66113,"__hash__":66114},"content\u002Flearn\u002Fpython-pass-statement-explained.md","Python pass Statement Explained",{"type":7,"value":64934,"toc":66060},[64935,64941,64947,64953,64955,64972,64977,64984,64989,64992,65003,65006,65009,65039,65041,65080,65082,65091,65099,65105,65108,65116,65119,65132,65137,65142,65158,65160,65174,65181,65188,65191,65244,65246,65255,65260,65264,65269,65312,65314,65322,65332,65336,65339,65375,65377,65386,65391,65395,65400,65445,65447,65467,65473,65477,65483,65543,65546,65555,65558,65564,65567,65571,65574,65577,65595,65598,65602,65607,65628,65633,65639,65642,65659,65668,65674,65679,65682,65700,65709,65724,65726,65774,65777,65801,65811,65819,65825,65828,65858,65861,65886,65889,65908,65911,65932,65935,65958,65965,65967,65973,65976,65984,65992,65998,66001,66007,66010,66017,66020,66022,66054,66057],[10,64936,18104,64938,64940],{"id":64937},"python-pass-statement-explained",[18,64939,58873],{}," Statement Explained",[14,64942,64943,64944,64946],{},"The Python ",[18,64945,58873],{}," statement is used when Python expects a block of code, but you do not want to add any real code yet.",[14,64948,64949,64950,64952],{},"Beginners often see ",[18,64951,58873],{}," in early practice programs, unfinished functions, or simple examples. It helps you write the structure of your program first without causing a syntax error.",[31,64954,34],{"id":33},[36,64956,64958],{"className":38,"code":64957,"language":40,"meta":41,"style":41},"if True:\n    pass\n",[18,64959,64960,64968],{"__ignoreMap":41},[45,64961,64962,64964,64966],{"class":47,"line":48},[45,64963,20],{"class":73},[45,64965,23323],{"class":481},[45,64967,86],{"class":85},[45,64969,64970],{"class":47,"line":63},[45,64971,5306],{"class":73},[14,64973,165,64974,64976],{},[18,64975,58873],{}," when Python requires an indented block but you do not want to run any code yet.",[31,64978,64980,64981,64983],{"id":64979},"what-the-pass-statement-does","What the ",[18,64982,58873],{}," statement does",[14,64985,64986,64988],{},[18,64987,58873],{}," is a placeholder statement.",[14,64990,64991],{},"When Python runs it:",[195,64993,64994,64997,65000],{},[198,64995,64996],{},"It does nothing",[198,64998,64999],{},"It produces no output",[198,65001,65002],{},"It does not change the program state",[14,65004,65005],{},"Its main purpose is to keep a block of code valid.",[14,65007,65008],{},"Python requires an indented block after lines that end with a colon, such as:",[195,65010,65011,65015,65019,65023,65027,65031,65035],{},[198,65012,65013],{},[18,65014,20],{},[198,65016,65017],{},[18,65018,7895],{},[198,65020,65021],{},[18,65022,23529],{},[198,65024,65025],{},[18,65026,2757],{},[198,65028,65029],{},[18,65030,2441],{},[198,65032,65033],{},[18,65034,23272],{},[198,65036,65037],{},[18,65038,23275],{},[14,65040,2115],{},[36,65042,65044],{"className":38,"code":65043,"language":40,"meta":41,"style":41},"if 5 > 3:\n    pass\n\nprint(\"Done\")\n",[18,65045,65046,65058,65062,65066],{"__ignoreMap":41},[45,65047,65048,65050,65052,65054,65056],{"class":47,"line":48},[45,65049,20],{"class":73},[45,65051,1222],{"class":59},[45,65053,33591],{"class":55},[45,65055,777],{"class":59},[45,65057,86],{"class":85},[45,65059,65060],{"class":47,"line":63},[45,65061,5306],{"class":73},[45,65063,65064],{"class":47,"line":70},[45,65065,67],{"emptyLinePlaceholder":66},[45,65067,65068,65070,65072,65074,65076,65078],{"class":47,"line":89},[45,65069,2065],{"class":92},[45,65071,96],{"class":85},[45,65073,100],{"class":99},[45,65075,57633],{"class":103},[45,65077,100],{"class":99},[45,65079,109],{"class":85},[712,65081,5452],{"id":5451},[36,65083,65085],{"className":38,"code":65084,"language":40,"meta":41,"style":41},"Done\n",[18,65086,65087],{"__ignoreMap":41},[45,65088,65089],{"class":47,"line":48},[45,65090,65084],{"class":51},[14,65092,1732,65093,65095,65096,65098],{},[18,65094,20],{}," block is valid because it contains ",[18,65097,58873],{},", even though nothing happens inside it.",[31,65100,65102,65103],{"id":65101},"why-beginners-need-pass","Why beginners need ",[18,65104,58873],{},[14,65106,65107],{},"Python does not allow empty code blocks.",[14,65109,65110,65111,240],{},"If you write the start of a block and leave it empty, Python raises an error. This is one reason beginners often run into ",[236,65112,65114],{"href":65113},"\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix",[18,65115,1614],{},[14,65117,65118],{},"For example, this is invalid:",[36,65120,65122],{"className":38,"code":65121,"language":40,"meta":41,"style":41},"if True:\n",[18,65123,65124],{"__ignoreMap":41},[45,65125,65126,65128,65130],{"class":47,"line":48},[45,65127,20],{"class":73},[45,65129,23323],{"class":481},[45,65131,86],{"class":85},[14,65133,65134,65135,240],{},"Python expects something indented under that ",[18,65136,20],{},[14,65138,1350,65139,65141],{},[18,65140,58873],{}," solves that problem:",[36,65143,65144],{"className":38,"code":64957,"language":40,"meta":41,"style":41},[18,65145,65146,65154],{"__ignoreMap":41},[45,65147,65148,65150,65152],{"class":47,"line":48},[45,65149,20],{"class":73},[45,65151,23323],{"class":481},[45,65153,86],{"class":85},[45,65155,65156],{"class":47,"line":63},[45,65157,5306],{"class":73},[14,65159,12914],{},[195,65161,65162,65165,65168,65171],{},[198,65163,65164],{},"You want to sketch your program first",[198,65166,65167],{},"You are building a function step by step",[198,65169,65170],{},"You know a block will be filled in later",[198,65172,65173],{},"You want your code to run without finishing every part yet",[31,65175,65177,65178,65180],{"id":65176},"where-pass-is-commonly-used","Where ",[18,65179,58873],{}," is commonly used",[712,65182,65184,65185,65187],{"id":65183},"inside-an-if-statement","Inside an ",[18,65186,20],{}," statement",[14,65189,65190],{},"When planning logic, you may want to create the condition first.",[36,65192,65194],{"className":38,"code":65193,"language":40,"meta":41,"style":41},"age = 15\n\nif age >= 18:\n    pass\nelse:\n    print(\"Too young\")\n",[18,65195,65196,65204,65208,65220,65224,65230],{"__ignoreMap":41},[45,65197,65198,65200,65202],{"class":47,"line":48},[45,65199,52],{"class":51},[45,65201,56],{"class":55},[45,65203,56295],{"class":59},[45,65205,65206],{"class":47,"line":63},[45,65207,67],{"emptyLinePlaceholder":66},[45,65209,65210,65212,65214,65216,65218],{"class":47,"line":70},[45,65211,20],{"class":73},[45,65213,76],{"class":51},[45,65215,600],{"class":55},[45,65217,121],{"class":59},[45,65219,86],{"class":85},[45,65221,65222],{"class":47,"line":89},[45,65223,5306],{"class":73},[45,65225,65226,65228],{"class":47,"line":112},[45,65227,28],{"class":73},[45,65229,86],{"class":85},[45,65231,65232,65234,65236,65238,65240,65242],{"class":47,"line":126},[45,65233,93],{"class":92},[45,65235,96],{"class":85},[45,65237,100],{"class":99},[45,65239,1973],{"class":103},[45,65241,100],{"class":99},[45,65243,109],{"class":85},[712,65245,5452],{"id":5744},[36,65247,65249],{"className":38,"code":65248,"language":40,"meta":41,"style":41},"Too young\n",[18,65250,65251],{"__ignoreMap":41},[45,65252,65253],{"class":47,"line":48},[45,65254,65248],{"class":51},[14,65256,65257,65258,240],{},"If you are still learning conditionals, see ",[236,65259,549],{"href":57289},[712,65261,65263],{"id":65262},"inside-a-loop","Inside a loop",[14,65265,65266,65268],{},[18,65267,58873],{}," can be used while testing loop structure.",[36,65270,65272],{"className":38,"code":65271,"language":40,"meta":41,"style":41},"for number in range(3):\n    pass\n\nprint(\"Loop finished\")\n",[18,65273,65274,65290,65294,65298],{"__ignoreMap":41},[45,65275,65276,65278,65280,65282,65284,65286,65288],{"class":47,"line":48},[45,65277,7895],{"class":73},[45,65279,744],{"class":51},[45,65281,8623],{"class":73},[45,65283,10451],{"class":92},[45,65285,96],{"class":85},[45,65287,11184],{"class":59},[45,65289,2464],{"class":85},[45,65291,65292],{"class":47,"line":63},[45,65293,5306],{"class":73},[45,65295,65296],{"class":47,"line":70},[45,65297,67],{"emptyLinePlaceholder":66},[45,65299,65300,65302,65304,65306,65308,65310],{"class":47,"line":89},[45,65301,2065],{"class":92},[45,65303,96],{"class":85},[45,65305,100],{"class":99},[45,65307,50521],{"class":103},[45,65309,100],{"class":99},[45,65311,109],{"class":85},[712,65313,5452],{"id":36382},[36,65315,65316],{"className":38,"code":50549,"language":40,"meta":41,"style":41},[18,65317,65318],{"__ignoreMap":41},[45,65319,65320],{"class":47,"line":48},[45,65321,50549],{"class":51},[14,65323,65324,65325,230,65328,240],{},"This can be useful while learning ",[236,65326,65327],{"href":51471},"Python for loops",[236,65329,65331],{"href":65330},"\u002Flearn\u002Fpython-while-loops-explained","Python while loops",[712,65333,65335],{"id":65334},"inside-a-function","Inside a function",[14,65337,65338],{},"You can create a function now and finish it later.",[36,65340,65342],{"className":38,"code":65341,"language":40,"meta":41,"style":41},"def greet():\n    pass\n\nprint(\"Function created\")\n",[18,65343,65344,65352,65356,65360],{"__ignoreMap":41},[45,65345,65346,65348,65350],{"class":47,"line":48},[45,65347,2757],{"class":2440},[45,65349,2936],{"class":2455},[45,65351,3828],{"class":85},[45,65353,65354],{"class":47,"line":63},[45,65355,5306],{"class":73},[45,65357,65358],{"class":47,"line":70},[45,65359,67],{"emptyLinePlaceholder":66},[45,65361,65362,65364,65366,65368,65371,65373],{"class":47,"line":89},[45,65363,2065],{"class":92},[45,65365,96],{"class":85},[45,65367,100],{"class":99},[45,65369,65370],{"class":103},"Function created",[45,65372,100],{"class":99},[45,65374,109],{"class":85},[712,65376,5452],{"id":36517},[36,65378,65380],{"className":38,"code":65379,"language":40,"meta":41,"style":41},"Function created\n",[18,65381,65382],{"__ignoreMap":41},[45,65383,65384],{"class":47,"line":48},[45,65385,65379],{"class":51},[14,65387,65388,65389,240],{},"This is common when learning ",[236,65390,4580],{"href":53315},[712,65392,65394],{"id":65393},"inside-a-class","Inside a class",[14,65396,65397,65399],{},[18,65398,58873],{}," is often used in empty class definitions.",[36,65401,65403],{"className":38,"code":65402,"language":40,"meta":41,"style":41},"class Person:\n    pass\n\nuser = Person()\nprint(type(user))\n",[18,65404,65405,65413,65417,65421,65431],{"__ignoreMap":41},[45,65406,65407,65409,65411],{"class":47,"line":48},[45,65408,2441],{"class":2440},[45,65410,2927],{"class":2444},[45,65412,86],{"class":85},[45,65414,65415],{"class":47,"line":63},[45,65416,5306],{"class":73},[45,65418,65419],{"class":47,"line":70},[45,65420,67],{"emptyLinePlaceholder":66},[45,65422,65423,65425,65427,65429],{"class":47,"line":89},[45,65424,30062],{"class":51},[45,65426,56],{"class":55},[45,65428,2927],{"class":2070},[45,65430,2495],{"class":85},[45,65432,65433,65435,65437,65439,65441,65443],{"class":47,"line":112},[45,65434,2065],{"class":92},[45,65436,96],{"class":85},[45,65438,4340],{"class":2109},[45,65440,96],{"class":85},[45,65442,30087],{"class":2070},[45,65444,4348],{"class":85},[712,65446,5452],{"id":36606},[36,65448,65450],{"className":38,"code":65449,"language":40,"meta":41,"style":41},"\u003Cclass '__main__.Person'>\n",[18,65451,65452],{"__ignoreMap":41},[45,65453,65454,65456,65458,65460,65463,65465],{"class":47,"line":48},[45,65455,79],{"class":55},[45,65457,2441],{"class":2440},[45,65459,9199],{"class":99},[45,65461,65462],{"class":103},"__main__.Person",[45,65464,7901],{"class":99},[45,65466,13484],{"class":55},[14,65468,65469,65470,240],{},"This is a common pattern when starting with ",[236,65471,65472],{"href":2559},"Python classes and objects",[712,65474,65476],{"id":65475},"inside-exception-handling","Inside exception handling",[14,65478,65479,65480,65482],{},"During early debugging, you may temporarily leave an ",[18,65481,23275],{}," block empty.",[36,65484,65486],{"className":38,"code":65485,"language":40,"meta":41,"style":41},"try:\n    number = int(\"abc\")\nexcept ValueError:\n    pass\n\nprint(\"Program continues\")\n",[18,65487,65488,65494,65512,65520,65524,65528],{"__ignoreMap":41},[45,65489,65490,65492],{"class":47,"line":48},[45,65491,23272],{"class":73},[45,65493,86],{"class":85},[45,65495,65496,65498,65500,65502,65504,65506,65508,65510],{"class":47,"line":63},[45,65497,12957],{"class":51},[45,65499,56],{"class":55},[45,65501,41669],{"class":2109},[45,65503,96],{"class":85},[45,65505,100],{"class":99},[45,65507,10304],{"class":103},[45,65509,100],{"class":99},[45,65511,109],{"class":85},[45,65513,65514,65516,65518],{"class":47,"line":70},[45,65515,23275],{"class":73},[45,65517,45892],{"class":2109},[45,65519,86],{"class":85},[45,65521,65522],{"class":47,"line":89},[45,65523,5306],{"class":73},[45,65525,65526],{"class":47,"line":112},[45,65527,67],{"emptyLinePlaceholder":66},[45,65529,65530,65532,65534,65536,65539,65541],{"class":47,"line":126},[45,65531,2065],{"class":92},[45,65533,96],{"class":85},[45,65535,100],{"class":99},[45,65537,65538],{"class":103},"Program continues",[45,65540,100],{"class":99},[45,65542,109],{"class":85},[712,65544,5452],{"id":65545},"output-5",[36,65547,65549],{"className":38,"code":65548,"language":40,"meta":41,"style":41},"Program continues\n",[18,65550,65551],{"__ignoreMap":41},[45,65552,65553],{"class":47,"line":48},[45,65554,65548],{"class":51},[14,65556,65557],{},"Be careful with this pattern. Ignoring errors can make bugs harder to find.",[31,65559,65561,65563],{"id":65560},"pass-vs-comments-vs-ellipsis",[18,65562,58873],{}," vs comments vs ellipsis",[14,65565,65566],{},"These are not the same.",[712,65568,65570],{"id":65569},"comment","Comment",[14,65572,65573],{},"A comment is ignored by Python, but it does not count as a real statement by itself.",[14,65575,65576],{},"This still causes an error:",[36,65578,65580],{"className":38,"code":65579,"language":40,"meta":41,"style":41},"if True:\n    # do this later\n",[18,65581,65582,65590],{"__ignoreMap":41},[45,65583,65584,65586,65588],{"class":47,"line":48},[45,65585,20],{"class":73},[45,65587,23323],{"class":481},[45,65589,86],{"class":85},[45,65591,65592],{"class":47,"line":63},[45,65593,65594],{"class":7624},"    # do this later\n",[14,65596,65597],{},"Python sees the block as empty.",[712,65599,65600],{"id":58873},[18,65601,58873],{},[14,65603,65604,65606],{},[18,65605,58873],{}," is a real Python statement, so it makes the block valid.",[36,65608,65610],{"className":38,"code":65609,"language":40,"meta":41,"style":41},"if True:\n    # do this later\n    pass\n",[18,65611,65612,65620,65624],{"__ignoreMap":41},[45,65613,65614,65616,65618],{"class":47,"line":48},[45,65615,20],{"class":73},[45,65617,23323],{"class":481},[45,65619,86],{"class":85},[45,65621,65622],{"class":47,"line":63},[45,65623,65594],{"class":7624},[45,65625,65626],{"class":47,"line":70},[45,65627,5306],{"class":73},[14,65629,56176,65630,65632],{},[18,65631,58873],{}," is an actual statement.",[712,65634,65636,65637,9146],{"id":65635},"ellipsis","Ellipsis (",[18,65638,13481],{},[14,65640,65641],{},"You may sometimes see this:",[36,65643,65645],{"className":38,"code":65644,"language":40,"meta":41,"style":41},"def my_function():\n    ...\n",[18,65646,65647,65655],{"__ignoreMap":41},[45,65648,65649,65651,65653],{"class":47,"line":48},[45,65650,2757],{"class":2440},[45,65652,5299],{"class":2455},[45,65654,3828],{"class":85},[45,65656,65657],{"class":47,"line":63},[45,65658,2772],{"class":2771},[14,65660,65661,65662,65664,65665,65667],{},"This is valid Python, but ",[18,65663,58873],{}," is usually clearer for beginners. If your goal is an intentionally empty block, ",[18,65666,58873],{}," is the better choice.",[31,65669,65671,65672],{"id":65670},"when-not-to-use-pass","When not to use ",[18,65673,58873],{},[14,65675,65676,65678],{},[18,65677,58873],{}," is helpful, but it should not be used carelessly.",[14,65680,65681],{},"Do not use it when:",[195,65683,65684,65687,65694,65697],{},[198,65685,65686],{},"You are hiding unfinished important logic for a long time",[198,65688,65689,65690,230,65692],{},"You actually need loop control like ",[18,65691,35658],{},[18,65693,35661],{},[198,65695,65696],{},"The block should contain real behavior",[198,65698,65699],{},"You only forgot to finish your code",[14,65701,65702,65703,14527,65705,230,65707,240],{},"A common mistake is confusing ",[18,65704,58873],{},[18,65706,35661],{},[18,65708,35658],{},[195,65710,65711,65716,65720],{},[198,65712,65713,65715],{},[18,65714,58873],{}," does nothing",[198,65717,65718,35802],{},[18,65719,35661],{},[198,65721,65722,35669],{},[18,65723,35658],{},[14,65725,466],{},[36,65727,65729],{"className":38,"code":65728,"language":40,"meta":41,"style":41},"for number in range(5):\n    if number == 2:\n        pass\n    print(number)\n",[18,65730,65731,65747,65759,65764],{"__ignoreMap":41},[45,65732,65733,65735,65737,65739,65741,65743,65745],{"class":47,"line":48},[45,65734,7895],{"class":73},[45,65736,744],{"class":51},[45,65738,8623],{"class":73},[45,65740,10451],{"class":92},[45,65742,96],{"class":85},[45,65744,3749],{"class":59},[45,65746,2464],{"class":85},[45,65748,65749,65751,65753,65755,65757],{"class":47,"line":63},[45,65750,1913],{"class":73},[45,65752,744],{"class":51},[45,65754,1129],{"class":55},[45,65756,3754],{"class":59},[45,65758,86],{"class":85},[45,65760,65761],{"class":47,"line":70},[45,65762,65763],{"class":73},"        pass\n",[45,65765,65766,65768,65770,65772],{"class":47,"line":89},[45,65767,93],{"class":92},[45,65769,96],{"class":85},[45,65771,13546],{"class":2070},[45,65773,109],{"class":85},[712,65775,5452],{"id":65776},"output-6",[36,65778,65779],{"className":38,"code":49833,"language":40,"meta":41,"style":41},[18,65780,65781,65785,65789,65793,65797],{"__ignoreMap":41},[45,65782,65783],{"class":47,"line":48},[45,65784,23957],{"class":59},[45,65786,65787],{"class":47,"line":63},[45,65788,13036],{"class":59},[45,65790,65791],{"class":47,"line":70},[45,65792,13041],{"class":59},[45,65794,65795],{"class":47,"line":89},[45,65796,13046],{"class":59},[45,65798,65799],{"class":47,"line":112},[45,65800,13681],{"class":59},[14,65802,65803,65804,65807,65808,65810],{},"Even when ",[18,65805,65806],{},"number == 2",", the loop continues normally because ",[18,65809,58873],{}," does not skip anything.",[14,65812,65813,65814,65816,65817,240],{},"If you want to skip one iteration, use ",[18,65815,35661],{}," instead. See ",[236,65818,50457],{"href":37398},[31,65820,65822,65823],{"id":65821},"errors-related-to-missing-pass","Errors related to missing ",[18,65824,58873],{},[14,65826,65827],{},"A missing block after one of these statements can cause an error:",[195,65829,65830,65834,65838,65842,65846,65850,65854],{},[198,65831,65832],{},[18,65833,20],{},[198,65835,65836],{},[18,65837,7895],{},[198,65839,65840],{},[18,65841,23529],{},[198,65843,65844],{},[18,65845,2757],{},[198,65847,65848],{},[18,65849,2441],{},[198,65851,65852],{},[18,65853,23272],{},[198,65855,65856],{},[18,65857,23275],{},[14,65859,65860],{},"This is especially common when:",[195,65862,65863,65869,65872,65875,65883],{},[198,65864,65865,65866,65868],{},"Writing an ",[18,65867,20],{}," statement and leaving the body empty",[198,65870,65871],{},"Creating a function skeleton without adding code",[198,65873,65874],{},"Adding only comments inside a class or function",[198,65876,52955,65877,14527,65879,230,65881],{},[18,65878,58873],{},[18,65880,35661],{},[18,65882,35658],{},[198,65884,65885],{},"Forgetting that Python requires an indented block after a colon",[14,65887,65888],{},"For example, this causes an error:",[36,65890,65892],{"className":38,"code":65891,"language":40,"meta":41,"style":41},"def hello():\n    # add greeting later\n",[18,65893,65894,65903],{"__ignoreMap":41},[45,65895,65896,65898,65901],{"class":47,"line":48},[45,65897,2757],{"class":2440},[45,65899,65900],{"class":2455}," hello",[45,65902,3828],{"class":85},[45,65904,65905],{"class":47,"line":63},[45,65906,65907],{"class":7624},"    # add greeting later\n",[14,65909,65910],{},"But this works:",[36,65912,65914],{"className":38,"code":65913,"language":40,"meta":41,"style":41},"def hello():\n    # add greeting later\n    pass\n",[18,65915,65916,65924,65928],{"__ignoreMap":41},[45,65917,65918,65920,65922],{"class":47,"line":48},[45,65919,2757],{"class":2440},[45,65921,65900],{"class":2455},[45,65923,3828],{"class":85},[45,65925,65926],{"class":47,"line":63},[45,65927,65907],{"class":7624},[45,65929,65930],{"class":47,"line":70},[45,65931,5306],{"class":73},[14,65933,65934],{},"If you want to check your file for syntax and indentation problems, you can run:",[36,65936,65938],{"className":15652,"code":65937,"language":15654,"meta":41,"style":41},"python script.py\npython -m py_compile script.py\n",[18,65939,65940,65947],{"__ignoreMap":41},[45,65941,65942,65944],{"class":47,"line":48},[45,65943,40],{"class":2444},[45,65945,65946],{"class":103}," script.py\n",[45,65948,65949,65951,65953,65956],{"class":47,"line":63},[45,65950,40],{"class":2444},[45,65952,58432],{"class":15663},[45,65954,65955],{"class":103}," py_compile",[45,65957,65946],{"class":103},[14,65959,65960,65961,240],{},"If Python reports an indentation-related problem, read ",[236,65962,65963],{"href":65113},[18,65964,1614],{},[31,65966,2281],{"id":2280},[712,65968,14769,65970,65972],{"id":65969},"does-pass-do-anything-in-python",[18,65971,58873],{}," do anything in Python?",[14,65974,65975],{},"No. It is a statement that does nothing, but it keeps the block syntactically valid.",[712,65977,350,65979,65981,65982,354],{"id":65978},"is-pass-the-same-as-continue",[18,65980,58873],{}," the same as ",[18,65983,35661],{},[14,65985,2302,65986,65988,65989,65991],{},[18,65987,58873],{}," does nothing. ",[18,65990,35661],{}," skips to the next loop iteration.",[712,65993,65995,65996,354],{"id":65994},"can-i-use-a-comment-instead-of-pass","Can I use a comment instead of ",[18,65997,58873],{},[14,65999,66000],{},"No. A comment does not count as a statement, so the block is still considered empty.",[712,66002,49242,66004,66006],{"id":66003},"can-pass-be-used-in-functions-and-classes",[18,66005,58873],{}," be used in functions and classes?",[14,66008,66009],{},"Yes. It is often used in empty functions, classes, conditionals, and loops.",[712,66011,66013,66014,66016],{"id":66012},"should-i-leave-pass-in-finished-code","Should I leave ",[18,66015,58873],{}," in finished code?",[14,66018,66019],{},"Only if the empty block is intentional. Otherwise, replace it with real logic.",[31,66021,2342],{"id":2341},[195,66023,66024,66030,66034,66038,66042,66046,66050],{},[198,66025,66026],{},[236,66027,66028],{"href":65113},[18,66029,1614],{},[198,66031,66032],{},[236,66033,549],{"href":57289},[198,66035,66036],{},[236,66037,23611],{"href":51471},[198,66039,66040],{},[236,66041,35821],{"href":65330},[198,66043,66044],{},[236,66045,5879],{"href":53315},[198,66047,66048],{},[236,66049,2560],{"href":2559},[198,66051,66052],{},[236,66053,50457],{"href":37398},[14,66055,66056],{},"If you keep learning Python block syntax, you will write cleaner code and avoid many common indentation errors.",[2367,66058,66059],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--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 pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":66061},[66062,66063,66067,66069,66083,66090,66094,66096,66108],{"id":33,"depth":63,"text":34},{"id":64979,"depth":63,"text":66064,"children":66065},"What the pass statement does",[66066],{"id":5451,"depth":70,"text":5452},{"id":65101,"depth":63,"text":66068},"Why beginners need pass",{"id":65176,"depth":63,"text":66070,"children":66071},"Where pass is commonly used",[66072,66074,66075,66076,66077,66078,66079,66080,66081,66082],{"id":65183,"depth":70,"text":66073},"Inside an if statement",{"id":5744,"depth":70,"text":5452},{"id":65262,"depth":70,"text":65263},{"id":36382,"depth":70,"text":5452},{"id":65334,"depth":70,"text":65335},{"id":36517,"depth":70,"text":5452},{"id":65393,"depth":70,"text":65394},{"id":36606,"depth":70,"text":5452},{"id":65475,"depth":70,"text":65476},{"id":65545,"depth":70,"text":5452},{"id":65560,"depth":63,"text":66084,"children":66085},"pass vs comments vs ellipsis",[66086,66087,66088],{"id":65569,"depth":70,"text":65570},{"id":58873,"depth":70,"text":58873},{"id":65635,"depth":70,"text":66089},"Ellipsis (...)",{"id":65670,"depth":63,"text":66091,"children":66092},"When not to use pass",[66093],{"id":65776,"depth":70,"text":5452},{"id":65821,"depth":63,"text":66095},"Errors related to missing pass",{"id":2280,"depth":63,"text":2281,"children":66097},[66098,66100,66102,66104,66106],{"id":65969,"depth":70,"text":66099},"Does pass do anything in Python?",{"id":65978,"depth":70,"text":66101},"Is pass the same as continue?",{"id":65994,"depth":70,"text":66103},"Can I use a comment instead of pass?",{"id":66003,"depth":70,"text":66105},"Can pass be used in functions and classes?",{"id":66012,"depth":70,"text":66107},"Should I leave pass in finished code?",{"id":2341,"depth":63,"text":2342},"Master python pass statement explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-pass-statement-explained",{"title":64932,"description":66109},"learn\u002Fpython-pass-statement-explained","-CC3wRS5WqH9DYWoLpSKZJ_tEks7eHsHKHmMKArNpU8",{"id":66116,"title":66117,"body":66118,"description":68278,"extension":2399,"meta":68279,"navigation":66,"path":68280,"seo":68281,"stem":68282,"__hash__":68283},"content\u002Flearn\u002Fpython-sets-explained.md","Python Sets Explained",{"type":7,"value":66119,"toc":68220},[66120,66123,66130,66140,66142,66209,66213,66238,66242,66271,66280,66284,66287,66304,66306,66361,66363,66396,66401,66405,66408,66422,66425,66493,66495,66515,66522,66526,66529,66533,66568,66572,66577,66608,66610,66629,66638,66667,66669,66688,66693,66699,66703,66708,66765,66767,66800,66804,66807,66811,66814,66861,66864,66868,66871,66913,66916,66920,66923,66927,66930,66939,66942,66944,66986,66988,66997,67000,67006,67010,67013,67019,67072,67081,67087,67140,67142,67159,67165,67195,67203,67210,67264,67270,67282,67288,67351,67354,67360,67409,67411,67422,67426,67429,67474,67481,67505,67507,67536,67543,67550,67574,67576,67589,67596,67603,67627,67629,67646,67653,67677,67679,67704,67708,67712,67726,67730,67741,67743,67848,67851,67856,67858,67861,67867,67870,67897,67900,67929,67933,67935,67977,67982,67986,67989,67993,67996,68002,68010,68012,68015,68036,68040,68043,68116,68119,68133,68135,68139,68142,68146,68149,68153,68161,68165,68168,68172,68175,68177,68217],[10,66121,66117],{"id":66122},"python-sets-explained",[14,66124,66125,66126,66129],{},"A Python set is a built-in collection type that stores ",[2421,66127,66128],{},"unique values only",". It is useful when you want to remove duplicates, check whether a value exists, or compare groups of items.",[14,66131,66132,66133,66136,66137,240],{},"Sets are different from lists and tuples because they are ",[2421,66134,66135],{},"unordered",". That means items do not have a fixed position, and you cannot access them with an index like ",[18,66138,66139],{},"my_set[0]",[31,66141,34],{"id":33},[36,66143,66145],{"className":38,"code":66144,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\nnumbers.add(4)\n\nprint(numbers)\nprint(2 in numbers)\n",[18,66146,66147,66167,66181,66185,66195],{"__ignoreMap":41},[45,66148,66149,66151,66153,66155,66157,66159,66161,66163,66165],{"class":47,"line":48},[45,66150,8570],{"class":51},[45,66152,56],{"class":55},[45,66154,8604],{"class":85},[45,66156,8578],{"class":59},[45,66158,3079],{"class":85},[45,66160,3754],{"class":59},[45,66162,3079],{"class":85},[45,66164,777],{"class":59},[45,66166,8629],{"class":85},[45,66168,66169,66171,66173,66175,66177,66179],{"class":47,"line":63},[45,66170,9069],{"class":51},[45,66172,240],{"class":85},[45,66174,11159],{"class":2070},[45,66176,96],{"class":85},[45,66178,3584],{"class":59},[45,66180,109],{"class":85},[45,66182,66183],{"class":47,"line":70},[45,66184,67],{"emptyLinePlaceholder":66},[45,66186,66187,66189,66191,66193],{"class":47,"line":89},[45,66188,2065],{"class":92},[45,66190,96],{"class":85},[45,66192,9069],{"class":2070},[45,66194,109],{"class":85},[45,66196,66197,66199,66201,66203,66205,66207],{"class":47,"line":112},[45,66198,2065],{"class":92},[45,66200,96],{"class":85},[45,66202,9367],{"class":59},[45,66204,45650],{"class":73},[45,66206,8626],{"class":2070},[45,66208,109],{"class":85},[14,66210,66211],{},[2421,66212,9061],{},[195,66214,66215,66224,66232],{},[198,66216,66217,66218,21,66220,25,66222],{},"Creates a set with the values ",[18,66219,8578],{},[18,66221,9367],{},[18,66223,11184],{},[198,66225,66226,66227,14527,66229],{},"Adds ",[18,66228,3584],{},[18,66230,66231],{},"add()",[198,66233,66234,66235,66237],{},"Checks whether ",[18,66236,9367],{}," is in the set",[14,66239,66240],{},[2421,66241,13456],{},[36,66243,66245],{"className":38,"code":66244,"language":40,"meta":41,"style":41},"{1, 2, 3, 4}\nTrue\n",[18,66246,66247,66267],{"__ignoreMap":41},[45,66248,66249,66251,66253,66255,66257,66259,66261,66263,66265],{"class":47,"line":48},[45,66250,5551],{"class":85},[45,66252,8578],{"class":59},[45,66254,3079],{"class":85},[45,66256,3754],{"class":59},[45,66258,3079],{"class":85},[45,66260,777],{"class":59},[45,66262,3079],{"class":85},[45,66264,8591],{"class":59},[45,66266,8629],{"class":85},[45,66268,66269],{"class":47,"line":63},[45,66270,2270],{"class":481},[14,66272,66273,66274,1316,66277,240],{},"Use a set when you want ",[2421,66275,66276],{},"unique values",[2421,66278,66279],{},"fast membership checks",[31,66281,66283],{"id":66282},"what-a-python-set-is","What a Python set is",[14,66285,66286],{},"A set in Python:",[195,66288,66289,66292,66295,66298,66301],{},[198,66290,66291],{},"Is a built-in collection type",[198,66293,66294],{},"Stores unique values only",[198,66296,66297],{},"Removes duplicate values automatically",[198,66299,66300],{},"Is unordered",[198,66302,66303],{},"Uses curly braces, like dictionaries, but contains only values",[14,66305,466],{},[36,66307,66309],{"className":38,"code":66308,"language":40,"meta":41,"style":41},"colors = {\"red\", \"blue\", \"red\", \"green\"}\nprint(colors)\n",[18,66310,66311,66351],{"__ignoreMap":41},[45,66312,66313,66315,66317,66319,66321,66323,66325,66327,66329,66331,66333,66335,66337,66339,66341,66343,66345,66347,66349],{"class":47,"line":48},[45,66314,22320],{"class":51},[45,66316,56],{"class":55},[45,66318,8604],{"class":85},[45,66320,100],{"class":99},[45,66322,22329],{"class":103},[45,66324,100],{"class":99},[45,66326,3079],{"class":85},[45,66328,1565],{"class":99},[45,66330,22347],{"class":103},[45,66332,100],{"class":99},[45,66334,3079],{"class":85},[45,66336,1565],{"class":99},[45,66338,22329],{"class":103},[45,66340,100],{"class":99},[45,66342,3079],{"class":85},[45,66344,1565],{"class":99},[45,66346,22338],{"class":103},[45,66348,100],{"class":99},[45,66350,8629],{"class":85},[45,66352,66353,66355,66357,66359],{"class":47,"line":63},[45,66354,2065],{"class":92},[45,66356,96],{"class":85},[45,66358,22407],{"class":2070},[45,66360,109],{"class":85},[14,66362,13456],{},[36,66364,66366],{"className":38,"code":66365,"language":40,"meta":41,"style":41},"{'blue', 'green', 'red'}\n",[18,66367,66368],{"__ignoreMap":41},[45,66369,66370,66372,66374,66376,66378,66380,66382,66384,66386,66388,66390,66392,66394],{"class":47,"line":48},[45,66371,5551],{"class":85},[45,66373,7901],{"class":99},[45,66375,22347],{"class":103},[45,66377,7901],{"class":99},[45,66379,3079],{"class":85},[45,66381,9199],{"class":99},[45,66383,22338],{"class":103},[45,66385,7901],{"class":99},[45,66387,3079],{"class":85},[45,66389,9199],{"class":99},[45,66391,22329],{"class":103},[45,66393,7901],{"class":99},[45,66395,8629],{"class":85},[14,66397,829,66398,66400],{},[18,66399,49752],{}," was written twice, it appears only once in the set.",[31,66402,66404],{"id":66403},"why-beginners-use-sets","Why beginners use sets",[14,66406,66407],{},"Beginners often use sets for practical tasks such as:",[195,66409,66410,66413,66416,66419],{},[198,66411,66412],{},"Removing duplicate items from data",[198,66414,66415],{},"Checking if a value exists quickly",[198,66417,66418],{},"Comparing groups of values",[198,66420,66421],{},"Finding shared or different items between collections",[14,66423,66424],{},"For example, if you have a list with repeated values, a set can help remove duplicates:",[36,66426,66428],{"className":38,"code":66427,"language":40,"meta":41,"style":41},"numbers = [1, 2, 2, 3, 3, 3]\nunique_numbers = set(numbers)\n\nprint(unique_numbers)\n",[18,66429,66430,66462,66478,66482],{"__ignoreMap":41},[45,66431,66432,66434,66436,66438,66440,66442,66444,66446,66448,66450,66452,66454,66456,66458,66460],{"class":47,"line":48},[45,66433,8570],{"class":51},[45,66435,56],{"class":55},[45,66437,8575],{"class":85},[45,66439,8578],{"class":59},[45,66441,3079],{"class":85},[45,66443,3754],{"class":59},[45,66445,3079],{"class":85},[45,66447,3754],{"class":59},[45,66449,3079],{"class":85},[45,66451,777],{"class":59},[45,66453,3079],{"class":85},[45,66455,777],{"class":59},[45,66457,3079],{"class":85},[45,66459,777],{"class":59},[45,66461,8594],{"class":85},[45,66463,66464,66467,66469,66472,66474,66476],{"class":47,"line":63},[45,66465,66466],{"class":51},"unique_numbers ",[45,66468,56],{"class":55},[45,66470,66471],{"class":2109}," set",[45,66473,96],{"class":85},[45,66475,9069],{"class":2070},[45,66477,109],{"class":85},[45,66479,66480],{"class":47,"line":70},[45,66481,67],{"emptyLinePlaceholder":66},[45,66483,66484,66486,66488,66491],{"class":47,"line":89},[45,66485,2065],{"class":92},[45,66487,96],{"class":85},[45,66489,66490],{"class":2070},"unique_numbers",[45,66492,109],{"class":85},[14,66494,177],{},[36,66496,66497],{"className":38,"code":41384,"language":40,"meta":41,"style":41},[18,66498,66499],{"__ignoreMap":41},[45,66500,66501,66503,66505,66507,66509,66511,66513],{"class":47,"line":48},[45,66502,5551],{"class":85},[45,66504,8578],{"class":59},[45,66506,3079],{"class":85},[45,66508,3754],{"class":59},[45,66510,3079],{"class":85},[45,66512,777],{"class":59},[45,66514,8629],{"class":85},[14,66516,66517,66518,240],{},"If your goal is specifically to remove duplicates from a list, see ",[236,66519,66521],{"href":66520},"\u002Fhow-to\u002Fhow-to-remove-duplicates-from-a-list-in-python\u002F","how to remove duplicates from a list in Python",[31,66523,66525],{"id":66524},"how-to-create-a-set","How to create a set",[14,66527,66528],{},"You can create a set in a few simple ways.",[712,66530,66532],{"id":66531},"create-a-set-with-curly-braces","Create a set with curly braces",[36,66534,66536],{"className":38,"code":66535,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\nprint(numbers)\n",[18,66537,66538,66558],{"__ignoreMap":41},[45,66539,66540,66542,66544,66546,66548,66550,66552,66554,66556],{"class":47,"line":48},[45,66541,8570],{"class":51},[45,66543,56],{"class":55},[45,66545,8604],{"class":85},[45,66547,8578],{"class":59},[45,66549,3079],{"class":85},[45,66551,3754],{"class":59},[45,66553,3079],{"class":85},[45,66555,777],{"class":59},[45,66557,8629],{"class":85},[45,66559,66560,66562,66564,66566],{"class":47,"line":63},[45,66561,2065],{"class":92},[45,66563,96],{"class":85},[45,66565,9069],{"class":2070},[45,66567,109],{"class":85},[712,66569,66571],{"id":66570},"create-an-empty-set","Create an empty set",[14,66573,165,66574,66576],{},[18,66575,34668],{}," for an empty set:",[36,66578,66580],{"className":38,"code":66579,"language":40,"meta":41,"style":41},"empty_set = set()\nprint(type(empty_set))\n",[18,66581,66582,66593],{"__ignoreMap":41},[45,66583,66584,66587,66589,66591],{"class":47,"line":48},[45,66585,66586],{"class":51},"empty_set ",[45,66588,56],{"class":55},[45,66590,66471],{"class":2109},[45,66592,2495],{"class":85},[45,66594,66595,66597,66599,66601,66603,66606],{"class":47,"line":63},[45,66596,2065],{"class":92},[45,66598,96],{"class":85},[45,66600,4340],{"class":2109},[45,66602,96],{"class":85},[45,66604,66605],{"class":2070},"empty_set",[45,66607,4348],{"class":85},[14,66609,177],{},[36,66611,66613],{"className":38,"code":66612,"language":40,"meta":41,"style":41},"\u003Cclass 'set'>\n",[18,66614,66615],{"__ignoreMap":41},[45,66616,66617,66619,66621,66623,66625,66627],{"class":47,"line":48},[45,66618,79],{"class":55},[45,66620,2441],{"class":2440},[45,66622,9199],{"class":99},[45,66624,29940],{"class":103},[45,66626,7901],{"class":99},[45,66628,13484],{"class":55},[14,66630,66631,66632,66634,66635,66637],{},"Do ",[2421,66633,2857],{}," use ",[18,66636,21683],{}," if you want an empty set.",[36,66639,66641],{"className":38,"code":66640,"language":40,"meta":41,"style":41},"empty_value = {}\nprint(type(empty_value))\n",[18,66642,66643,66652],{"__ignoreMap":41},[45,66644,66645,66648,66650],{"class":47,"line":48},[45,66646,66647],{"class":51},"empty_value ",[45,66649,56],{"class":55},[45,66651,9656],{"class":85},[45,66653,66654,66656,66658,66660,66662,66665],{"class":47,"line":63},[45,66655,2065],{"class":92},[45,66657,96],{"class":85},[45,66659,4340],{"class":2109},[45,66661,96],{"class":85},[45,66663,66664],{"class":2070},"empty_value",[45,66666,4348],{"class":85},[14,66668,177],{},[36,66670,66672],{"className":38,"code":66671,"language":40,"meta":41,"style":41},"\u003Cclass 'dict'>\n",[18,66673,66674],{"__ignoreMap":41},[45,66675,66676,66678,66680,66682,66684,66686],{"class":47,"line":48},[45,66677,79],{"class":55},[45,66679,2441],{"class":2440},[45,66681,9199],{"class":99},[45,66683,29935],{"class":103},[45,66685,7901],{"class":99},[45,66687,13484],{"class":55},[14,66689,66690,66692],{},[18,66691,21683],{}," creates an empty dictionary, not a set.",[14,66694,41158,66695,240],{},[236,66696,66698],{"href":66697},"\u002Freference\u002Fpython-set-creating-a-set\u002F","creating a set in Python",[712,66700,66702],{"id":66701},"build-a-set-from-another-collection","Build a set from another collection",[14,66704,66705,66706,240],{},"You can create a set from a list, tuple, or string with ",[18,66707,34668],{},[36,66709,66711],{"className":38,"code":66710,"language":40,"meta":41,"style":41},"letters = set([\"a\", \"b\", \"a\", \"c\"])\nprint(letters)\n",[18,66712,66713,66755],{"__ignoreMap":41},[45,66714,66715,66717,66719,66721,66723,66725,66727,66729,66731,66733,66735,66737,66739,66741,66743,66745,66747,66749,66751,66753],{"class":47,"line":48},[45,66716,60601],{"class":51},[45,66718,56],{"class":55},[45,66720,66471],{"class":2109},[45,66722,34742],{"class":85},[45,66724,100],{"class":99},[45,66726,236],{"class":103},[45,66728,100],{"class":99},[45,66730,3079],{"class":85},[45,66732,1565],{"class":99},[45,66734,10384],{"class":103},[45,66736,100],{"class":99},[45,66738,3079],{"class":85},[45,66740,1565],{"class":99},[45,66742,236],{"class":103},[45,66744,100],{"class":99},[45,66746,3079],{"class":85},[45,66748,1565],{"class":99},[45,66750,10402],{"class":103},[45,66752,100],{"class":99},[45,66754,21941],{"class":85},[45,66756,66757,66759,66761,66763],{"class":47,"line":63},[45,66758,2065],{"class":92},[45,66760,96],{"class":85},[45,66762,60630],{"class":2070},[45,66764,109],{"class":85},[14,66766,177],{},[36,66768,66770],{"className":38,"code":66769,"language":40,"meta":41,"style":41},"{'a', 'b', 'c'}\n",[18,66771,66772],{"__ignoreMap":41},[45,66773,66774,66776,66778,66780,66782,66784,66786,66788,66790,66792,66794,66796,66798],{"class":47,"line":48},[45,66775,5551],{"class":85},[45,66777,7901],{"class":99},[45,66779,236],{"class":103},[45,66781,7901],{"class":99},[45,66783,3079],{"class":85},[45,66785,9199],{"class":99},[45,66787,10384],{"class":103},[45,66789,7901],{"class":99},[45,66791,3079],{"class":85},[45,66793,9199],{"class":99},[45,66795,10402],{"class":103},[45,66797,7901],{"class":99},[45,66799,8629],{"class":85},[31,66801,66803],{"id":66802},"important-set-behavior","Important set behavior",[14,66805,66806],{},"There are a few rules beginners should understand early.",[712,66808,66810],{"id":66809},"sets-are-unordered","Sets are unordered",[14,66812,66813],{},"A set does not keep items in a fixed position.",[36,66815,66817],{"className":38,"code":66816,"language":40,"meta":41,"style":41},"fruits = {\"apple\", \"banana\", \"orange\"}\nprint(fruits)\n",[18,66818,66819,66851],{"__ignoreMap":41},[45,66820,66821,66823,66825,66827,66829,66831,66833,66835,66837,66839,66841,66843,66845,66847,66849],{"class":47,"line":48},[45,66822,49394],{"class":51},[45,66824,56],{"class":55},[45,66826,8604],{"class":85},[45,66828,100],{"class":99},[45,66830,9443],{"class":103},[45,66832,100],{"class":99},[45,66834,3079],{"class":85},[45,66836,1565],{"class":99},[45,66838,9456],{"class":103},[45,66840,100],{"class":99},[45,66842,3079],{"class":85},[45,66844,1565],{"class":99},[45,66846,59968],{"class":103},[45,66848,100],{"class":99},[45,66850,8629],{"class":85},[45,66852,66853,66855,66857,66859],{"class":47,"line":63},[45,66854,2065],{"class":92},[45,66856,96],{"class":85},[45,66858,59981],{"class":2070},[45,66860,109],{"class":85},[14,66862,66863],{},"The display order may not match the order you wrote.",[712,66865,66867],{"id":66866},"you-cannot-access-items-by-index","You cannot access items by index",[14,66869,66870],{},"This will not work:",[36,66872,66874],{"className":38,"code":66873,"language":40,"meta":41,"style":41},"fruits = {\"apple\", \"banana\", \"orange\"}\n# print(fruits[0])\n",[18,66875,66876,66908],{"__ignoreMap":41},[45,66877,66878,66880,66882,66884,66886,66888,66890,66892,66894,66896,66898,66900,66902,66904,66906],{"class":47,"line":48},[45,66879,49394],{"class":51},[45,66881,56],{"class":55},[45,66883,8604],{"class":85},[45,66885,100],{"class":99},[45,66887,9443],{"class":103},[45,66889,100],{"class":99},[45,66891,3079],{"class":85},[45,66893,1565],{"class":99},[45,66895,9456],{"class":103},[45,66897,100],{"class":99},[45,66899,3079],{"class":85},[45,66901,1565],{"class":99},[45,66903,59968],{"class":103},[45,66905,100],{"class":99},[45,66907,8629],{"class":85},[45,66909,66910],{"class":47,"line":63},[45,66911,66912],{"class":7624},"# print(fruits[0])\n",[14,66914,66915],{},"Sets do not support indexing because they are unordered.",[712,66917,66919],{"id":66918},"set-output-order-may-look-different","Set output order may look different",[14,66921,66922],{},"If you print a set, the order may look different across runs. This is normal.",[712,66924,66926],{"id":66925},"items-must-be-hashable","Items must be hashable",[14,66928,66929],{},"A set can store values like:",[195,66931,66932,66935,66937],{},[198,66933,66934],{},"Integers",[198,66936,23721],{},[198,66938,44648],{},[14,66940,66941],{},"But mutable values like lists and dictionaries cannot be stored in a set.",[14,66943,12381],{},[36,66945,66947],{"className":38,"code":66946,"language":40,"meta":41,"style":41},"items = {(1, 2), (3, 4)}\nprint(items)\n",[18,66948,66949,66976],{"__ignoreMap":41},[45,66950,66951,66953,66955,66958,66960,66962,66964,66966,66968,66970,66972,66974],{"class":47,"line":48},[45,66952,23632],{"class":51},[45,66954,56],{"class":55},[45,66956,66957],{"class":85}," {(",[45,66959,8578],{"class":59},[45,66961,3079],{"class":85},[45,66963,3754],{"class":59},[45,66965,25873],{"class":85},[45,66967,14039],{"class":85},[45,66969,11184],{"class":59},[45,66971,3079],{"class":85},[45,66973,8591],{"class":59},[45,66975,10458],{"class":85},[45,66977,66978,66980,66982,66984],{"class":47,"line":63},[45,66979,2065],{"class":92},[45,66981,96],{"class":85},[45,66983,9516],{"class":2070},[45,66985,109],{"class":85},[14,66987,52996],{},[36,66989,66991],{"className":38,"code":66990,"language":40,"meta":41,"style":41},"# bad_set = {[1, 2], [3, 4]}\n",[18,66992,66993],{"__ignoreMap":41},[45,66994,66995],{"class":47,"line":48},[45,66996,66990],{"class":7624},[14,66998,66999],{},"A list is mutable, so Python raises an error if you try to put it in a set.",[14,67001,37628,67002,240],{},[236,67003,67005],{"href":67004},"\u002Fglossary\u002Fwhat-is-a-set-in-python\u002F","what is a set in Python",[31,67007,67009],{"id":67008},"basic-set-operations-beginners-should-know","Basic set operations beginners should know",[14,67011,67012],{},"These are the main methods beginners use first.",[712,67014,67016,67018],{"id":67015},"add-adds-one-item",[18,67017,66231],{}," adds one item",[36,67020,67022],{"className":38,"code":67021,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\nnumbers.add(4)\n\nprint(numbers)\n",[18,67023,67024,67044,67058,67062],{"__ignoreMap":41},[45,67025,67026,67028,67030,67032,67034,67036,67038,67040,67042],{"class":47,"line":48},[45,67027,8570],{"class":51},[45,67029,56],{"class":55},[45,67031,8604],{"class":85},[45,67033,8578],{"class":59},[45,67035,3079],{"class":85},[45,67037,3754],{"class":59},[45,67039,3079],{"class":85},[45,67041,777],{"class":59},[45,67043,8629],{"class":85},[45,67045,67046,67048,67050,67052,67054,67056],{"class":47,"line":63},[45,67047,9069],{"class":51},[45,67049,240],{"class":85},[45,67051,11159],{"class":2070},[45,67053,96],{"class":85},[45,67055,3584],{"class":59},[45,67057,109],{"class":85},[45,67059,67060],{"class":47,"line":70},[45,67061,67],{"emptyLinePlaceholder":66},[45,67063,67064,67066,67068,67070],{"class":47,"line":89},[45,67065,2065],{"class":92},[45,67067,96],{"class":85},[45,67069,9069],{"class":2070},[45,67071,109],{"class":85},[14,67073,165,67074,67080],{},[236,67075,67077,67078],{"href":67076},"\u002Freference\u002Fpython-set-add-method\u002F","Python set ",[18,67079,66231],{}," if you want to learn this method in more detail.",[712,67082,67084,67086],{"id":67083},"remove-removes-an-item",[18,67085,31853],{}," removes an item",[36,67088,67090],{"className":38,"code":67089,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\nnumbers.remove(2)\n\nprint(numbers)\n",[18,67091,67092,67112,67126,67130],{"__ignoreMap":41},[45,67093,67094,67096,67098,67100,67102,67104,67106,67108,67110],{"class":47,"line":48},[45,67095,8570],{"class":51},[45,67097,56],{"class":55},[45,67099,8604],{"class":85},[45,67101,8578],{"class":59},[45,67103,3079],{"class":85},[45,67105,3754],{"class":59},[45,67107,3079],{"class":85},[45,67109,777],{"class":59},[45,67111,8629],{"class":85},[45,67113,67114,67116,67118,67120,67122,67124],{"class":47,"line":63},[45,67115,9069],{"class":51},[45,67117,240],{"class":85},[45,67119,61161],{"class":2070},[45,67121,96],{"class":85},[45,67123,9367],{"class":59},[45,67125,109],{"class":85},[45,67127,67128],{"class":47,"line":70},[45,67129,67],{"emptyLinePlaceholder":66},[45,67131,67132,67134,67136,67138],{"class":47,"line":89},[45,67133,2065],{"class":92},[45,67135,96],{"class":85},[45,67137,9069],{"class":2070},[45,67139,109],{"class":85},[14,67141,177],{},[36,67143,67145],{"className":38,"code":67144,"language":40,"meta":41,"style":41},"{1, 3}\n",[18,67146,67147],{"__ignoreMap":41},[45,67148,67149,67151,67153,67155,67157],{"class":47,"line":48},[45,67150,5551],{"class":85},[45,67152,8578],{"class":59},[45,67154,3079],{"class":85},[45,67156,777],{"class":59},[45,67158,8629],{"class":85},[14,67160,67161,67162,67164],{},"But ",[18,67163,31853],{}," raises an error if the item is missing:",[36,67166,67168],{"className":38,"code":67167,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\n# numbers.remove(5)\n",[18,67169,67170,67190],{"__ignoreMap":41},[45,67171,67172,67174,67176,67178,67180,67182,67184,67186,67188],{"class":47,"line":48},[45,67173,8570],{"class":51},[45,67175,56],{"class":55},[45,67177,8604],{"class":85},[45,67179,8578],{"class":59},[45,67181,3079],{"class":85},[45,67183,3754],{"class":59},[45,67185,3079],{"class":85},[45,67187,777],{"class":59},[45,67189,8629],{"class":85},[45,67191,67192],{"class":47,"line":63},[45,67193,67194],{"class":7624},"# numbers.remove(5)\n",[14,67196,26005,67197,67202],{},[236,67198,67077,67200],{"href":67199},"\u002Freference\u002Fpython-set-remove-method\u002F",[18,67201,31853],{}," for examples and error behavior.",[712,67204,67206,67209],{"id":67205},"discard-removes-an-item-without-an-error",[18,67207,67208],{},"discard()"," removes an item without an error",[36,67211,67213],{"className":38,"code":67212,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\nnumbers.discard(5)\n\nprint(numbers)\n",[18,67214,67215,67235,67250,67254],{"__ignoreMap":41},[45,67216,67217,67219,67221,67223,67225,67227,67229,67231,67233],{"class":47,"line":48},[45,67218,8570],{"class":51},[45,67220,56],{"class":55},[45,67222,8604],{"class":85},[45,67224,8578],{"class":59},[45,67226,3079],{"class":85},[45,67228,3754],{"class":59},[45,67230,3079],{"class":85},[45,67232,777],{"class":59},[45,67234,8629],{"class":85},[45,67236,67237,67239,67241,67244,67246,67248],{"class":47,"line":63},[45,67238,9069],{"class":51},[45,67240,240],{"class":85},[45,67242,67243],{"class":2070},"discard",[45,67245,96],{"class":85},[45,67247,3749],{"class":59},[45,67249,109],{"class":85},[45,67251,67252],{"class":47,"line":70},[45,67253,67],{"emptyLinePlaceholder":66},[45,67255,67256,67258,67260,67262],{"class":47,"line":89},[45,67257,2065],{"class":92},[45,67259,96],{"class":85},[45,67261,9069],{"class":2070},[45,67263,109],{"class":85},[14,67265,67266,67267,67269],{},"Nothing goes wrong here, even though ",[18,67268,3749],{}," is not in the set.",[14,67271,26005,67272,67277,67278,1316,67280,240],{},[236,67273,67077,67275],{"href":67274},"\u002Freference\u002Fpython-set-discard-method\u002F",[18,67276,67208],{}," for the difference between ",[18,67279,67208],{},[18,67281,31853],{},[712,67283,67285,67287],{"id":67284},"pop-removes-and-returns-a-random-item",[18,67286,43793],{}," removes and returns a random item",[36,67289,67291],{"className":38,"code":67290,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\nremoved_item = numbers.pop()\n\nprint(removed_item)\nprint(numbers)\n",[18,67292,67293,67313,67327,67331,67341],{"__ignoreMap":41},[45,67294,67295,67297,67299,67301,67303,67305,67307,67309,67311],{"class":47,"line":48},[45,67296,8570],{"class":51},[45,67298,56],{"class":55},[45,67300,8604],{"class":85},[45,67302,8578],{"class":59},[45,67304,3079],{"class":85},[45,67306,3754],{"class":59},[45,67308,3079],{"class":85},[45,67310,777],{"class":59},[45,67312,8629],{"class":85},[45,67314,67315,67317,67319,67321,67323,67325],{"class":47,"line":63},[45,67316,61260],{"class":51},[45,67318,56],{"class":55},[45,67320,8626],{"class":51},[45,67322,240],{"class":85},[45,67324,43869],{"class":2070},[45,67326,2495],{"class":85},[45,67328,67329],{"class":47,"line":70},[45,67330,67],{"emptyLinePlaceholder":66},[45,67332,67333,67335,67337,67339],{"class":47,"line":89},[45,67334,2065],{"class":92},[45,67336,96],{"class":85},[45,67338,61287],{"class":2070},[45,67340,109],{"class":85},[45,67342,67343,67345,67347,67349],{"class":47,"line":112},[45,67344,2065],{"class":92},[45,67346,96],{"class":85},[45,67348,9069],{"class":2070},[45,67350,109],{"class":85},[14,67352,67353],{},"Because sets are unordered, you should not expect a specific item to be removed.",[712,67355,67357,67359],{"id":67356},"clear-removes-all-items",[18,67358,44057],{}," removes all items",[36,67361,67363],{"className":38,"code":67362,"language":40,"meta":41,"style":41},"numbers = {1, 2, 3}\nnumbers.clear()\n\nprint(numbers)\n",[18,67364,67365,67385,67395,67399],{"__ignoreMap":41},[45,67366,67367,67369,67371,67373,67375,67377,67379,67381,67383],{"class":47,"line":48},[45,67368,8570],{"class":51},[45,67370,56],{"class":55},[45,67372,8604],{"class":85},[45,67374,8578],{"class":59},[45,67376,3079],{"class":85},[45,67378,3754],{"class":59},[45,67380,3079],{"class":85},[45,67382,777],{"class":59},[45,67384,8629],{"class":85},[45,67386,67387,67389,67391,67393],{"class":47,"line":63},[45,67388,9069],{"class":51},[45,67390,240],{"class":85},[45,67392,44112],{"class":2070},[45,67394,2495],{"class":85},[45,67396,67397],{"class":47,"line":70},[45,67398,67],{"emptyLinePlaceholder":66},[45,67400,67401,67403,67405,67407],{"class":47,"line":89},[45,67402,2065],{"class":92},[45,67404,96],{"class":85},[45,67406,9069],{"class":2070},[45,67408,109],{"class":85},[14,67410,177],{},[36,67412,67414],{"className":38,"code":67413,"language":40,"meta":41,"style":41},"set()\n",[18,67415,67416],{"__ignoreMap":41},[45,67417,67418,67420],{"class":47,"line":48},[45,67419,29940],{"class":2109},[45,67421,2495],{"class":85},[31,67423,67425],{"id":67424},"common-set-operations-for-comparing-values","Common set operations for comparing values",[14,67427,67428],{},"Sets are very useful when you want to compare values between two groups.",[36,67430,67432],{"className":38,"code":67431,"language":40,"meta":41,"style":41},"a = {1, 2, 3}\nb = {3, 4, 5}\n",[18,67433,67434,67454],{"__ignoreMap":41},[45,67435,67436,67438,67440,67442,67444,67446,67448,67450,67452],{"class":47,"line":48},[45,67437,11541],{"class":51},[45,67439,56],{"class":55},[45,67441,8604],{"class":85},[45,67443,8578],{"class":59},[45,67445,3079],{"class":85},[45,67447,3754],{"class":59},[45,67449,3079],{"class":85},[45,67451,777],{"class":59},[45,67453,8629],{"class":85},[45,67455,67456,67458,67460,67462,67464,67466,67468,67470,67472],{"class":47,"line":63},[45,67457,19315],{"class":51},[45,67459,56],{"class":55},[45,67461,8604],{"class":85},[45,67463,11184],{"class":59},[45,67465,3079],{"class":85},[45,67467,8591],{"class":59},[45,67469,3079],{"class":85},[45,67471,1222],{"class":59},[45,67473,8629],{"class":85},[712,67475,67477,67480],{"id":67476},"union-combines-items-from-both-sets",[18,67478,67479],{},"union()"," combines items from both sets",[36,67482,67484],{"className":38,"code":67483,"language":40,"meta":41,"style":41},"print(a.union(b))\n",[18,67485,67486],{"__ignoreMap":41},[45,67487,67488,67490,67492,67494,67496,67499,67501,67503],{"class":47,"line":48},[45,67489,2065],{"class":92},[45,67491,96],{"class":85},[45,67493,236],{"class":2070},[45,67495,240],{"class":85},[45,67497,67498],{"class":2070},"union",[45,67500,96],{"class":85},[45,67502,10384],{"class":2070},[45,67504,4348],{"class":85},[14,67506,177],{},[36,67508,67510],{"className":38,"code":67509,"language":40,"meta":41,"style":41},"{1, 2, 3, 4, 5}\n",[18,67511,67512],{"__ignoreMap":41},[45,67513,67514,67516,67518,67520,67522,67524,67526,67528,67530,67532,67534],{"class":47,"line":48},[45,67515,5551],{"class":85},[45,67517,8578],{"class":59},[45,67519,3079],{"class":85},[45,67521,3754],{"class":59},[45,67523,3079],{"class":85},[45,67525,777],{"class":59},[45,67527,3079],{"class":85},[45,67529,8591],{"class":59},[45,67531,3079],{"class":85},[45,67533,1222],{"class":59},[45,67535,8629],{"class":85},[14,67537,26005,67538,240],{},[236,67539,67077,67541],{"href":67540},"\u002Freference\u002Fpython-set-union-method\u002F",[18,67542,67479],{},[712,67544,67546,67549],{"id":67545},"intersection-finds-shared-items",[18,67547,67548],{},"intersection()"," finds shared items",[36,67551,67553],{"className":38,"code":67552,"language":40,"meta":41,"style":41},"print(a.intersection(b))\n",[18,67554,67555],{"__ignoreMap":41},[45,67556,67557,67559,67561,67563,67565,67568,67570,67572],{"class":47,"line":48},[45,67558,2065],{"class":92},[45,67560,96],{"class":85},[45,67562,236],{"class":2070},[45,67564,240],{"class":85},[45,67566,67567],{"class":2070},"intersection",[45,67569,96],{"class":85},[45,67571,10384],{"class":2070},[45,67573,4348],{"class":85},[14,67575,177],{},[36,67577,67579],{"className":38,"code":67578,"language":40,"meta":41,"style":41},"{3}\n",[18,67580,67581],{"__ignoreMap":41},[45,67582,67583,67585,67587],{"class":47,"line":48},[45,67584,5551],{"class":85},[45,67586,11184],{"class":59},[45,67588,8629],{"class":85},[14,67590,26005,67591,240],{},[236,67592,67077,67594],{"href":67593},"\u002Freference\u002Fpython-set-intersection-method\u002F",[18,67595,67548],{},[712,67597,67599,67602],{"id":67598},"difference-finds-items-only-in-the-first-set",[18,67600,67601],{},"difference()"," finds items only in the first set",[36,67604,67606],{"className":38,"code":67605,"language":40,"meta":41,"style":41},"print(a.difference(b))\n",[18,67607,67608],{"__ignoreMap":41},[45,67609,67610,67612,67614,67616,67618,67621,67623,67625],{"class":47,"line":48},[45,67611,2065],{"class":92},[45,67613,96],{"class":85},[45,67615,236],{"class":2070},[45,67617,240],{"class":85},[45,67619,67620],{"class":2070},"difference",[45,67622,96],{"class":85},[45,67624,10384],{"class":2070},[45,67626,4348],{"class":85},[14,67628,177],{},[36,67630,67632],{"className":38,"code":67631,"language":40,"meta":41,"style":41},"{1, 2}\n",[18,67633,67634],{"__ignoreMap":41},[45,67635,67636,67638,67640,67642,67644],{"class":47,"line":48},[45,67637,5551],{"class":85},[45,67639,8578],{"class":59},[45,67641,3079],{"class":85},[45,67643,3754],{"class":59},[45,67645,8629],{"class":85},[712,67647,67649,67652],{"id":67648},"symmetric_difference-finds-items-in-either-set-but-not-both",[18,67650,67651],{},"symmetric_difference()"," finds items in either set but not both",[36,67654,67656],{"className":38,"code":67655,"language":40,"meta":41,"style":41},"print(a.symmetric_difference(b))\n",[18,67657,67658],{"__ignoreMap":41},[45,67659,67660,67662,67664,67666,67668,67671,67673,67675],{"class":47,"line":48},[45,67661,2065],{"class":92},[45,67663,96],{"class":85},[45,67665,236],{"class":2070},[45,67667,240],{"class":85},[45,67669,67670],{"class":2070},"symmetric_difference",[45,67672,96],{"class":85},[45,67674,10384],{"class":2070},[45,67676,4348],{"class":85},[14,67678,177],{},[36,67680,67682],{"className":38,"code":67681,"language":40,"meta":41,"style":41},"{1, 2, 4, 5}\n",[18,67683,67684],{"__ignoreMap":41},[45,67685,67686,67688,67690,67692,67694,67696,67698,67700,67702],{"class":47,"line":48},[45,67687,5551],{"class":85},[45,67689,8578],{"class":59},[45,67691,3079],{"class":85},[45,67693,3754],{"class":59},[45,67695,3079],{"class":85},[45,67697,8591],{"class":59},[45,67699,3079],{"class":85},[45,67701,1222],{"class":59},[45,67703,8629],{"class":85},[31,67705,67707],{"id":67706},"when-to-use-a-set-instead-of-a-list","When to use a set instead of a list",[14,67709,19194,67710,8077],{},[2421,67711,29940],{},[195,67713,67714,67717,67720],{},[198,67715,67716],{},"Duplicates should not exist",[198,67718,67719],{},"Order does not matter",[198,67721,67722,67723],{},"You want fast membership checks like ",[18,67724,67725],{},"value in my_set",[14,67727,19194,67728,8077],{},[2421,67729,14463],{},[195,67731,67732,67735,67738],{},[198,67733,67734],{},"Item order matters",[198,67736,67737],{},"You need indexing",[198,67739,67740],{},"You need slicing",[14,67742,466],{},[36,67744,67746],{"className":38,"code":67745,"language":40,"meta":41,"style":41},"names_list = [\"Ana\", \"Ben\", \"Ana\"]\nnames_set = {\"Ana\", \"Ben\"}\n\nprint(\"Ana\" in names_list)\nprint(\"Ana\" in names_set)\n",[18,67747,67748,67781,67806,67810,67829],{"__ignoreMap":41},[45,67749,67750,67753,67755,67757,67759,67761,67763,67765,67767,67769,67771,67773,67775,67777,67779],{"class":47,"line":48},[45,67751,67752],{"class":51},"names_list ",[45,67754,56],{"class":55},[45,67756,8575],{"class":85},[45,67758,100],{"class":99},[45,67760,10580],{"class":103},[45,67762,100],{"class":99},[45,67764,3079],{"class":85},[45,67766,1565],{"class":99},[45,67768,28692],{"class":103},[45,67770,100],{"class":99},[45,67772,3079],{"class":85},[45,67774,1565],{"class":99},[45,67776,10580],{"class":103},[45,67778,100],{"class":99},[45,67780,8594],{"class":85},[45,67782,67783,67786,67788,67790,67792,67794,67796,67798,67800,67802,67804],{"class":47,"line":63},[45,67784,67785],{"class":51},"names_set ",[45,67787,56],{"class":55},[45,67789,8604],{"class":85},[45,67791,100],{"class":99},[45,67793,10580],{"class":103},[45,67795,100],{"class":99},[45,67797,3079],{"class":85},[45,67799,1565],{"class":99},[45,67801,28692],{"class":103},[45,67803,100],{"class":99},[45,67805,8629],{"class":85},[45,67807,67808],{"class":47,"line":70},[45,67809,67],{"emptyLinePlaceholder":66},[45,67811,67812,67814,67816,67818,67820,67822,67824,67827],{"class":47,"line":89},[45,67813,2065],{"class":92},[45,67815,96],{"class":85},[45,67817,100],{"class":99},[45,67819,10580],{"class":103},[45,67821,100],{"class":99},[45,67823,45650],{"class":73},[45,67825,67826],{"class":2070}," names_list",[45,67828,109],{"class":85},[45,67830,67831,67833,67835,67837,67839,67841,67843,67846],{"class":47,"line":112},[45,67832,2065],{"class":92},[45,67834,96],{"class":85},[45,67836,100],{"class":99},[45,67838,10580],{"class":103},[45,67840,100],{"class":99},[45,67842,45650],{"class":73},[45,67844,67845],{"class":2070}," names_set",[45,67847,109],{"class":85},[14,67849,67850],{},"Both can check membership, but a set is designed for this kind of lookup.",[14,67852,67853,67854,240],{},"If you want a bigger comparison of Python collection types, read ",[236,67855,62013],{"href":62012},[31,67857,1337],{"id":1336},[14,67859,67860],{},"These are some very common problems when learning sets.",[712,67862,1350,67864,67866],{"id":67863},"using-and-expecting-an-empty-set",[18,67865,21683],{}," and expecting an empty set",[14,67868,67869],{},"This creates a dictionary:",[36,67871,67873],{"className":38,"code":67872,"language":40,"meta":41,"style":41},"value = {}\nprint(type(value))\n",[18,67874,67875,67883],{"__ignoreMap":41},[45,67876,67877,67879,67881],{"class":47,"line":48},[45,67878,1476],{"class":51},[45,67880,56],{"class":55},[45,67882,9656],{"class":85},[45,67884,67885,67887,67889,67891,67893,67895],{"class":47,"line":63},[45,67886,2065],{"class":92},[45,67888,96],{"class":85},[45,67890,4340],{"class":2109},[45,67892,96],{"class":85},[45,67894,12722],{"class":2070},[45,67896,4348],{"class":85},[14,67898,67899],{},"Use this instead:",[36,67901,67903],{"className":38,"code":67902,"language":40,"meta":41,"style":41},"value = set()\nprint(type(value))\n",[18,67904,67905,67915],{"__ignoreMap":41},[45,67906,67907,67909,67911,67913],{"class":47,"line":48},[45,67908,1476],{"class":51},[45,67910,56],{"class":55},[45,67912,66471],{"class":2109},[45,67914,2495],{"class":85},[45,67916,67917,67919,67921,67923,67925,67927],{"class":47,"line":63},[45,67918,2065],{"class":92},[45,67920,96],{"class":85},[45,67922,4340],{"class":2109},[45,67924,96],{"class":85},[45,67926,12722],{"class":2070},[45,67928,4348],{"class":85},[712,67930,67932],{"id":67931},"trying-to-access-a-set-with-an-index","Trying to access a set with an index",[14,67934,52996],{},[36,67936,67938],{"className":38,"code":67937,"language":40,"meta":41,"style":41},"letters = {\"a\", \"b\", \"c\"}\n# print(letters[0])\n",[18,67939,67940,67972],{"__ignoreMap":41},[45,67941,67942,67944,67946,67948,67950,67952,67954,67956,67958,67960,67962,67964,67966,67968,67970],{"class":47,"line":48},[45,67943,60601],{"class":51},[45,67945,56],{"class":55},[45,67947,8604],{"class":85},[45,67949,100],{"class":99},[45,67951,236],{"class":103},[45,67953,100],{"class":99},[45,67955,3079],{"class":85},[45,67957,1565],{"class":99},[45,67959,10384],{"class":103},[45,67961,100],{"class":99},[45,67963,3079],{"class":85},[45,67965,1565],{"class":99},[45,67967,10402],{"class":103},[45,67969,100],{"class":99},[45,67971,8629],{"class":85},[45,67973,67974],{"class":47,"line":63},[45,67975,67976],{"class":7624},"# print(letters[0])\n",[14,67978,67979,67980,240],{},"Sets are unordered, so there is no index ",[18,67981,10482],{},[712,67983,67985],{"id":67984},"expecting-items-to-stay-in-insertion-order","Expecting items to stay in insertion order",[14,67987,67988],{},"A set is not the right choice if you need values to stay in a specific order.",[712,67990,67992],{"id":67991},"trying-to-add-a-list-or-dictionary-into-a-set","Trying to add a list or dictionary into a set",[14,67994,67995],{},"This causes an error because lists and dictionaries are mutable.",[712,67997,1350,67999,68001],{"id":67998},"using-remove-when-the-item-may-not-exist",[18,68000,31853],{}," when the item may not exist",[14,68003,68004,68005,68007,68008,240],{},"If the item might be missing, ",[18,68006,67208],{}," is often safer than ",[18,68009,31853],{},[31,68011,2012],{"id":2011},[14,68013,68014],{},"Beginners often run into trouble with sets because they:",[195,68016,68017,68020,68023,68026,68031],{},[198,68018,68019],{},"Confuse sets with lists because both store multiple items",[198,68021,68022],{},"Confuse sets with dictionaries because both use curly braces",[198,68024,68025],{},"Expect duplicates to remain in the collection",[198,68027,68028,68029],{},"Try to index a set like ",[18,68030,66139],{},[198,68032,68033,68034],{},"Create an empty dictionary instead of an empty set with ",[18,68035,21683],{},[31,68037,68039],{"id":68038},"helpful-debugging-checks","Helpful debugging checks",[14,68041,68042],{},"If a set is not behaving the way you expect, these simple checks can help:",[36,68044,68046],{"className":38,"code":68045,"language":40,"meta":41,"style":41},"print(my_set)\nprint(type(my_set))\nprint(len(my_set))\nprint(value in my_set)\nprint(list(my_set))\n",[18,68047,68048,68059,68073,68087,68102],{"__ignoreMap":41},[45,68049,68050,68052,68054,68057],{"class":47,"line":48},[45,68051,2065],{"class":92},[45,68053,96],{"class":85},[45,68055,68056],{"class":2070},"my_set",[45,68058,109],{"class":85},[45,68060,68061,68063,68065,68067,68069,68071],{"class":47,"line":63},[45,68062,2065],{"class":92},[45,68064,96],{"class":85},[45,68066,4340],{"class":2109},[45,68068,96],{"class":85},[45,68070,68056],{"class":2070},[45,68072,4348],{"class":85},[45,68074,68075,68077,68079,68081,68083,68085],{"class":47,"line":70},[45,68076,2065],{"class":92},[45,68078,96],{"class":85},[45,68080,28716],{"class":92},[45,68082,96],{"class":85},[45,68084,68056],{"class":2070},[45,68086,4348],{"class":85},[45,68088,68089,68091,68093,68095,68097,68100],{"class":47,"line":89},[45,68090,2065],{"class":92},[45,68092,96],{"class":85},[45,68094,1476],{"class":2070},[45,68096,8623],{"class":73},[45,68098,68099],{"class":2070}," my_set",[45,68101,109],{"class":85},[45,68103,68104,68106,68108,68110,68112,68114],{"class":47,"line":112},[45,68105,2065],{"class":92},[45,68107,96],{"class":85},[45,68109,14463],{"class":2109},[45,68111,96],{"class":85},[45,68113,68056],{"class":2070},[45,68115,4348],{"class":85},[14,68117,68118],{},"These checks help you answer questions like:",[195,68120,68121,68124,68127,68130],{},[198,68122,68123],{},"Is this really a set?",[198,68125,68126],{},"How many unique items are in it?",[198,68128,68129],{},"Does it contain a specific value?",[198,68131,68132],{},"What values are currently inside it?",[31,68134,2281],{"id":2280},[712,68136,68138],{"id":68137},"does-a-python-set-keep-items-in-order","Does a Python set keep items in order?",[14,68140,68141],{},"No. A set is unordered, so you should not rely on item position.",[712,68143,68145],{"id":68144},"can-a-set-contain-duplicate-values","Can a set contain duplicate values?",[14,68147,68148],{},"No. Duplicate values are removed automatically.",[712,68150,68152],{"id":68151},"how-do-i-create-an-empty-set-in-python","How do I create an empty set in Python?",[14,68154,165,68155,68157,68158,68160],{},[18,68156,34668],{},". If you use ",[18,68159,21683],{},", Python creates an empty dictionary.",[712,68162,68164],{"id":68163},"can-i-access-a-set-item-by-index","Can I access a set item by index?",[14,68166,68167],{},"No. Sets do not support indexing because they are unordered.",[712,68169,68171],{"id":68170},"when-should-i-use-a-set-instead-of-a-list","When should I use a set instead of a list?",[14,68173,68174],{},"Use a set when you need unique values or fast membership checks and do not need order.",[31,68176,2342],{"id":2341},[195,68178,68179,68184,68189,68195,68201,68207,68212],{},[198,68180,68181],{},[236,68182,68183],{"href":67004},"What is a set in Python",[198,68185,68186],{},[236,68187,68188],{"href":66697},"Creating a set in Python",[198,68190,68191],{},[236,68192,67077,68193,2575],{"href":67076},[18,68194,66231],{},[198,68196,68197],{},[236,68198,67077,68199,2575],{"href":67199},[18,68200,31853],{},[198,68202,68203],{},[236,68204,67077,68205,2575],{"href":67274},[18,68206,67208],{},[198,68208,68209],{},[236,68210,68211],{"href":66520},"How to remove duplicates from a list in Python",[198,68213,68214],{},[236,68215,68216],{"href":62012},"When to use lists vs tuples vs sets vs dictionaries",[2367,68218,68219],{},"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 .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 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 .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 .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 .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":41,"searchDepth":63,"depth":63,"links":68221},[68222,68223,68224,68225,68230,68236,68248,68258,68259,68268,68269,68270,68277],{"id":33,"depth":63,"text":34},{"id":66282,"depth":63,"text":66283},{"id":66403,"depth":63,"text":66404},{"id":66524,"depth":63,"text":66525,"children":68226},[68227,68228,68229],{"id":66531,"depth":70,"text":66532},{"id":66570,"depth":70,"text":66571},{"id":66701,"depth":70,"text":66702},{"id":66802,"depth":63,"text":66803,"children":68231},[68232,68233,68234,68235],{"id":66809,"depth":70,"text":66810},{"id":66866,"depth":70,"text":66867},{"id":66918,"depth":70,"text":66919},{"id":66925,"depth":70,"text":66926},{"id":67008,"depth":63,"text":67009,"children":68237},[68238,68240,68242,68244,68246],{"id":67015,"depth":70,"text":68239},"add() adds one item",{"id":67083,"depth":70,"text":68241},"remove() removes an item",{"id":67205,"depth":70,"text":68243},"discard() removes an item without an error",{"id":67284,"depth":70,"text":68245},"pop() removes and returns a random item",{"id":67356,"depth":70,"text":68247},"clear() removes all items",{"id":67424,"depth":63,"text":67425,"children":68249},[68250,68252,68254,68256],{"id":67476,"depth":70,"text":68251},"union() combines items from both sets",{"id":67545,"depth":70,"text":68253},"intersection() finds shared items",{"id":67598,"depth":70,"text":68255},"difference() finds items only in the first set",{"id":67648,"depth":70,"text":68257},"symmetric_difference() finds items in either set but not both",{"id":67706,"depth":63,"text":67707},{"id":1336,"depth":63,"text":1337,"children":68260},[68261,68263,68264,68265,68266],{"id":67863,"depth":70,"text":68262},"Using {} and expecting an empty set",{"id":67931,"depth":70,"text":67932},{"id":67984,"depth":70,"text":67985},{"id":67991,"depth":70,"text":67992},{"id":67998,"depth":70,"text":68267},"Using remove() when the item may not exist",{"id":2011,"depth":63,"text":2012},{"id":68038,"depth":63,"text":68039},{"id":2280,"depth":63,"text":2281,"children":68271},[68272,68273,68274,68275,68276],{"id":68137,"depth":70,"text":68138},{"id":68144,"depth":70,"text":68145},{"id":68151,"depth":70,"text":68152},{"id":68163,"depth":70,"text":68164},{"id":68170,"depth":70,"text":68171},{"id":2341,"depth":63,"text":2342},"Master python sets explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-sets-explained",{"title":66117,"description":68278},"learn\u002Fpython-sets-explained","zqMKXmQuudOMYdA8zLqUb7l-IUonwgJb5Ns3Pm32R90",{"id":68285,"title":68286,"body":68287,"description":70560,"extension":2399,"meta":70561,"navigation":66,"path":29916,"seo":70562,"stem":70563,"__hash__":70564},"content\u002Flearn\u002Fpython-strings-explained-basics-and-examples.md","Python Strings Explained (Basics and Examples)",{"type":7,"value":68288,"toc":70512},[68289,68292,68295,68298,68300,68303,68392,68394,68424,68428,68431,68434,68436,68465,68470,68479,68483,68486,68516,68520,68523,68540,68543,68561,68564,68568,68571,68609,68613,68616,68621,68627,68650,68657,68660,68674,68676,68750,68752,68770,68773,68777,68781,68784,68829,68831,68845,68849,68852,68855,68874,68883,68970,68972,68996,68999,69034,69045,69049,69052,69058,69063,69128,69130,69139,69145,69150,69174,69176,69185,69191,69196,69227,69229,69237,69241,69247,69252,69311,69313,69325,69329,69332,69337,69342,69374,69376,69384,69390,69395,69400,69431,69433,69441,69447,69452,69457,69489,69491,69499,69505,69510,69515,69565,69567,69576,69582,69588,69593,69647,69649,69682,69692,69698,69703,69776,69778,69792,69802,69806,69811,69814,69816,69851,69854,69857,69911,69913,69921,69924,69928,69933,69982,69984,69996,69999,70002,70059,70061,70069,70073,70076,70082,70088,70135,70142,70197,70201,70204,70208,70211,70242,70252,70255,70290,70294,70296,70299,70319,70322,70393,70395,70427,70429,70433,70436,70440,70443,70447,70450,70454,70457,70461,70470,70472,70509],[10,68290,68286],{"id":68291},"python-strings-explained-basics-and-examples",[14,68293,68294],{},"Strings are one of the most common data types in Python.",[14,68296,68297],{},"A string is used to store text. In this guide, you will learn what strings are, how to create them, and how to do basic string tasks like indexing, slicing, joining, and changing text.",[31,68299,34],{"id":33},[14,68301,68302],{},"Use this short example to see what a string looks like, how to access characters, and how string methods work.",[36,68304,68306],{"className":38,"code":68305,"language":40,"meta":41,"style":41},"text = \"Python\"\nprint(text)\nprint(text[0])\nprint(text[-1])\nprint(text[0:3])\nprint(text.upper())\n",[18,68307,68308,68320,68330,68344,68360,68378],{"__ignoreMap":41},[45,68309,68310,68312,68314,68316,68318],{"class":47,"line":48},[45,68311,10297],{"class":51},[45,68313,56],{"class":55},[45,68315,1565],{"class":99},[45,68317,16791],{"class":103},[45,68319,1571],{"class":99},[45,68321,68322,68324,68326,68328],{"class":47,"line":63},[45,68323,2065],{"class":92},[45,68325,96],{"class":85},[45,68327,5000],{"class":2070},[45,68329,109],{"class":85},[45,68331,68332,68334,68336,68338,68340,68342],{"class":47,"line":70},[45,68333,2065],{"class":92},[45,68335,96],{"class":85},[45,68337,5000],{"class":2070},[45,68339,9680],{"class":85},[45,68341,10482],{"class":59},[45,68343,21941],{"class":85},[45,68345,68346,68348,68350,68352,68354,68356,68358],{"class":47,"line":89},[45,68347,2065],{"class":92},[45,68349,96],{"class":85},[45,68351,5000],{"class":2070},[45,68353,9680],{"class":85},[45,68355,218],{"class":55},[45,68357,8578],{"class":59},[45,68359,21941],{"class":85},[45,68361,68362,68364,68366,68368,68370,68372,68374,68376],{"class":47,"line":112},[45,68363,2065],{"class":92},[45,68365,96],{"class":85},[45,68367,5000],{"class":2070},[45,68369,9680],{"class":85},[45,68371,10482],{"class":59},[45,68373,457],{"class":85},[45,68375,11184],{"class":59},[45,68377,21941],{"class":85},[45,68379,68380,68382,68384,68386,68388,68390],{"class":47,"line":126},[45,68381,2065],{"class":92},[45,68383,96],{"class":85},[45,68385,5000],{"class":2070},[45,68387,240],{"class":85},[45,68389,10327],{"class":2070},[45,68391,10777],{"class":85},[14,68393,177],{},[36,68395,68397],{"className":38,"code":68396,"language":40,"meta":41,"style":41},"Python\nP\nn\nPyt\nPYTHON\n",[18,68398,68399,68404,68409,68414,68419],{"__ignoreMap":41},[45,68400,68401],{"class":47,"line":48},[45,68402,68403],{"class":51},"Python\n",[45,68405,68406],{"class":47,"line":63},[45,68407,68408],{"class":51},"P\n",[45,68410,68411],{"class":47,"line":70},[45,68412,68413],{"class":51},"n\n",[45,68415,68416],{"class":47,"line":89},[45,68417,68418],{"class":51},"Pyt\n",[45,68420,68421],{"class":47,"line":112},[45,68422,68423],{"class":2771},"PYTHON\n",[31,68425,68427],{"id":68426},"what-a-string-is","What a string is",[14,68429,68430],{},"A string is text data in Python.",[14,68432,68433],{},"You write strings inside quotes. Both single quotes and double quotes work.",[14,68435,40802],{},[36,68437,68439],{"className":38,"code":68438,"language":40,"meta":41,"style":41},"\"hello\"\n'Python'\n\"123\"\n",[18,68440,68441,68449,68457],{"__ignoreMap":41},[45,68442,68443,68445,68447],{"class":47,"line":48},[45,68444,100],{"class":39289},[45,68446,18782],{"class":39295},[45,68448,1571],{"class":39289},[45,68450,68451,68453,68455],{"class":47,"line":63},[45,68452,7901],{"class":39289},[45,68454,16791],{"class":39295},[45,68456,7906],{"class":39289},[45,68458,68459,68461,68463],{"class":47,"line":70},[45,68460,100],{"class":39289},[45,68462,41060],{"class":39295},[45,68464,1571],{"class":39289},[14,68466,63492,68467,68469],{},[18,68468,40984],{}," is still a string because it is inside quotes. It is text, not a number.",[14,68471,68472,68473,230,68475,240],{},"If you are new to Python data types, see ",[236,68474,35582],{"href":35108},[236,68476,68478],{"href":68477},"\u002Fglossary\u002Fwhat-is-a-string-in-python\u002F","what is a string in Python",[31,68480,68482],{"id":68481},"how-to-create-strings","How to create strings",[14,68484,68485],{},"The most common way to create a string is to assign text to a variable.",[36,68487,68489],{"className":38,"code":68488,"language":40,"meta":41,"style":41},"name = \"Alice\"\nlanguage = 'Python'\n",[18,68490,68491,68503],{"__ignoreMap":41},[45,68492,68493,68495,68497,68499,68501],{"class":47,"line":48},[45,68494,1560],{"class":51},[45,68496,56],{"class":55},[45,68498,1565],{"class":99},[45,68500,60413],{"class":103},[45,68502,1571],{"class":99},[45,68504,68505,68508,68510,68512,68514],{"class":47,"line":63},[45,68506,68507],{"class":51},"language ",[45,68509,56],{"class":55},[45,68511,9199],{"class":99},[45,68513,16791],{"class":103},[45,68515,7906],{"class":99},[712,68517,68519],{"id":68518},"single-and-double-quotes","Single and double quotes",[14,68521,68522],{},"Single quotes are fine for simple text:",[36,68524,68526],{"className":38,"code":68525,"language":40,"meta":41,"style":41},"message = 'Hello'\n",[18,68527,68528],{"__ignoreMap":41},[45,68529,68530,68532,68534,68536,68538],{"class":47,"line":48},[45,68531,15507],{"class":51},[45,68533,56],{"class":55},[45,68535,9199],{"class":99},[45,68537,3226],{"class":103},[45,68539,7906],{"class":99},[14,68541,68542],{},"Double quotes are useful when the text contains an apostrophe:",[36,68544,68546],{"className":38,"code":68545,"language":40,"meta":41,"style":41},"message = \"It's working\"\n",[18,68547,68548],{"__ignoreMap":41},[45,68549,68550,68552,68554,68556,68559],{"class":47,"line":48},[45,68551,15507],{"class":51},[45,68553,56],{"class":55},[45,68555,1565],{"class":99},[45,68557,68558],{"class":103},"It's working",[45,68560,1571],{"class":99},[14,68562,68563],{},"If you wrote that with single quotes, Python would get confused about where the string ends.",[712,68565,68567],{"id":68566},"triple-quotes-for-multi-line-text","Triple quotes for multi-line text",[14,68569,68570],{},"Use triple quotes when you want a string that spans multiple lines.",[36,68572,68574],{"className":38,"code":68573,"language":40,"meta":41,"style":41},"text = \"\"\"Line one\nLine two\nLine three\"\"\"\nprint(text)\n",[18,68575,68576,68587,68592,68599],{"__ignoreMap":41},[45,68577,68578,68580,68582,68584],{"class":47,"line":48},[45,68579,10297],{"class":51},[45,68581,56],{"class":55},[45,68583,39756],{"class":99},[45,68585,68586],{"class":103},"Line one\n",[45,68588,68589],{"class":47,"line":63},[45,68590,68591],{"class":103},"Line two\n",[45,68593,68594,68597],{"class":47,"line":70},[45,68595,68596],{"class":103},"Line three",[45,68598,39290],{"class":99},[45,68600,68601,68603,68605,68607],{"class":47,"line":89},[45,68602,2065],{"class":92},[45,68604,96],{"class":85},[45,68606,5000],{"class":2070},[45,68608,109],{"class":85},[31,68610,68612],{"id":68611},"strings-are-ordered-sequences","Strings are ordered sequences",[14,68614,68615],{},"A string is an ordered sequence of characters.",[14,68617,68618,68619,240],{},"That means each character has a position, called an ",[2421,68620,60449],{},[14,68622,68623,68624,457],{},"In the string ",[18,68625,68626],{},"\"Python\"",[195,68628,68629,68637,68643],{},[198,68630,68631,68634,68635],{},[18,68632,68633],{},"P"," is at index ",[18,68636,10482],{},[198,68638,68639,68634,68641],{},[18,68640,29457],{},[18,68642,8578],{},[198,68644,68645,68634,68648],{},[18,68646,68647],{},"t",[18,68649,9367],{},[14,68651,68652,68653,64456,68655,240],{},"Python starts counting at ",[18,68654,10482],{},[18,68656,8578],{},[14,68658,68659],{},"Negative indexes count from the end:",[195,68661,68662,68668],{},[198,68663,68664,68667],{},[18,68665,68666],{},"-1"," is the last character",[198,68669,68670,68673],{},[18,68671,68672],{},"-2"," is the second-to-last character",[14,68675,466],{},[36,68677,68679],{"className":38,"code":68678,"language":40,"meta":41,"style":41},"text = \"Python\"\n\nprint(text[0])   # first character\nprint(text[1])   # second character\nprint(text[-1])  # last character\n",[18,68680,68681,68693,68697,68714,68731],{"__ignoreMap":41},[45,68682,68683,68685,68687,68689,68691],{"class":47,"line":48},[45,68684,10297],{"class":51},[45,68686,56],{"class":55},[45,68688,1565],{"class":99},[45,68690,16791],{"class":103},[45,68692,1571],{"class":99},[45,68694,68695],{"class":47,"line":63},[45,68696,67],{"emptyLinePlaceholder":66},[45,68698,68699,68701,68703,68705,68707,68709,68711],{"class":47,"line":70},[45,68700,2065],{"class":92},[45,68702,96],{"class":85},[45,68704,5000],{"class":2070},[45,68706,9680],{"class":85},[45,68708,10482],{"class":59},[45,68710,45348],{"class":85},[45,68712,68713],{"class":7624},"   # first character\n",[45,68715,68716,68718,68720,68722,68724,68726,68728],{"class":47,"line":89},[45,68717,2065],{"class":92},[45,68719,96],{"class":85},[45,68721,5000],{"class":2070},[45,68723,9680],{"class":85},[45,68725,8578],{"class":59},[45,68727,45348],{"class":85},[45,68729,68730],{"class":7624},"   # second character\n",[45,68732,68733,68735,68737,68739,68741,68743,68745,68747],{"class":47,"line":112},[45,68734,2065],{"class":92},[45,68736,96],{"class":85},[45,68738,5000],{"class":2070},[45,68740,9680],{"class":85},[45,68742,218],{"class":55},[45,68744,8578],{"class":59},[45,68746,45348],{"class":85},[45,68748,68749],{"class":7624},"  # last character\n",[14,68751,177],{},[36,68753,68755],{"className":38,"code":68754,"language":40,"meta":41,"style":41},"P\ny\nn\n",[18,68756,68757,68761,68766],{"__ignoreMap":41},[45,68758,68759],{"class":47,"line":48},[45,68760,68408],{"class":51},[45,68762,68763],{"class":47,"line":63},[45,68764,68765],{"class":51},"y\n",[45,68767,68768],{"class":47,"line":70},[45,68769,68413],{"class":51},[14,68771,68772],{},"You access one character by using square brackets after the string.",[31,68774,68776],{"id":68775},"indexing-and-slicing","Indexing and slicing",[712,68778,68780],{"id":68779},"indexing","Indexing",[14,68782,68783],{},"Indexing gets one character from a string.",[36,68785,68787],{"className":38,"code":68786,"language":40,"meta":41,"style":41},"text = \"Python\"\nprint(text[0])\nprint(text[3])\n",[18,68788,68789,68801,68815],{"__ignoreMap":41},[45,68790,68791,68793,68795,68797,68799],{"class":47,"line":48},[45,68792,10297],{"class":51},[45,68794,56],{"class":55},[45,68796,1565],{"class":99},[45,68798,16791],{"class":103},[45,68800,1571],{"class":99},[45,68802,68803,68805,68807,68809,68811,68813],{"class":47,"line":63},[45,68804,2065],{"class":92},[45,68806,96],{"class":85},[45,68808,5000],{"class":2070},[45,68810,9680],{"class":85},[45,68812,10482],{"class":59},[45,68814,21941],{"class":85},[45,68816,68817,68819,68821,68823,68825,68827],{"class":47,"line":70},[45,68818,2065],{"class":92},[45,68820,96],{"class":85},[45,68822,5000],{"class":2070},[45,68824,9680],{"class":85},[45,68826,11184],{"class":59},[45,68828,21941],{"class":85},[14,68830,177],{},[36,68832,68834],{"className":38,"code":68833,"language":40,"meta":41,"style":41},"P\nh\n",[18,68835,68836,68840],{"__ignoreMap":41},[45,68837,68838],{"class":47,"line":48},[45,68839,68408],{"class":51},[45,68841,68842],{"class":47,"line":63},[45,68843,68844],{"class":51},"h\n",[712,68846,68848],{"id":68847},"slicing","Slicing",[14,68850,68851],{},"Slicing gets part of a string.",[14,68853,68854],{},"The basic format is:",[36,68856,68858],{"className":38,"code":68857,"language":40,"meta":41,"style":41},"text[start:stop]\n",[18,68859,68860],{"__ignoreMap":41},[45,68861,68862,68864,68866,68868,68870,68872],{"class":47,"line":48},[45,68863,5000],{"class":51},[45,68865,9680],{"class":85},[45,68867,49940],{"class":51},[45,68869,457],{"class":85},[45,68871,49948],{"class":51},[45,68873,8594],{"class":85},[14,68875,68876,68877,68879,68880,240],{},"Important: the ",[18,68878,49948],{}," position is ",[2421,68881,68882],{},"not included",[36,68884,68886],{"className":38,"code":68885,"language":40,"meta":41,"style":41},"text = \"Python\"\n\nprint(text[0:3])\nprint(text[2:6])\nprint(text[:4])\nprint(text[4:])\n",[18,68887,68888,68900,68904,68922,68940,68955],{"__ignoreMap":41},[45,68889,68890,68892,68894,68896,68898],{"class":47,"line":48},[45,68891,10297],{"class":51},[45,68893,56],{"class":55},[45,68895,1565],{"class":99},[45,68897,16791],{"class":103},[45,68899,1571],{"class":99},[45,68901,68902],{"class":47,"line":63},[45,68903,67],{"emptyLinePlaceholder":66},[45,68905,68906,68908,68910,68912,68914,68916,68918,68920],{"class":47,"line":70},[45,68907,2065],{"class":92},[45,68909,96],{"class":85},[45,68911,5000],{"class":2070},[45,68913,9680],{"class":85},[45,68915,10482],{"class":59},[45,68917,457],{"class":85},[45,68919,11184],{"class":59},[45,68921,21941],{"class":85},[45,68923,68924,68926,68928,68930,68932,68934,68936,68938],{"class":47,"line":89},[45,68925,2065],{"class":92},[45,68927,96],{"class":85},[45,68929,5000],{"class":2070},[45,68931,9680],{"class":85},[45,68933,9367],{"class":59},[45,68935,457],{"class":85},[45,68937,28541],{"class":59},[45,68939,21941],{"class":85},[45,68941,68942,68944,68946,68948,68951,68953],{"class":47,"line":112},[45,68943,2065],{"class":92},[45,68945,96],{"class":85},[45,68947,5000],{"class":2070},[45,68949,68950],{"class":85},"[:",[45,68952,3584],{"class":59},[45,68954,21941],{"class":85},[45,68956,68957,68959,68961,68963,68965,68967],{"class":47,"line":126},[45,68958,2065],{"class":92},[45,68960,96],{"class":85},[45,68962,5000],{"class":2070},[45,68964,9680],{"class":85},[45,68966,3584],{"class":59},[45,68968,68969],{"class":85},":])\n",[14,68971,177],{},[36,68973,68975],{"className":38,"code":68974,"language":40,"meta":41,"style":41},"Pyt\nthon\nPyth\non\n",[18,68976,68977,68981,68986,68991],{"__ignoreMap":41},[45,68978,68979],{"class":47,"line":48},[45,68980,68418],{"class":51},[45,68982,68983],{"class":47,"line":63},[45,68984,68985],{"class":51},"thon\n",[45,68987,68988],{"class":47,"line":70},[45,68989,68990],{"class":51},"Pyth\n",[45,68992,68993],{"class":47,"line":89},[45,68994,68995],{"class":51},"on\n",[14,68997,68998],{},"A common beginner example is:",[36,69000,69002],{"className":38,"code":69001,"language":40,"meta":41,"style":41},"text = \"Python\"\nprint(text[0:3])\n",[18,69003,69004,69016],{"__ignoreMap":41},[45,69005,69006,69008,69010,69012,69014],{"class":47,"line":48},[45,69007,10297],{"class":51},[45,69009,56],{"class":55},[45,69011,1565],{"class":99},[45,69013,16791],{"class":103},[45,69015,1571],{"class":99},[45,69017,69018,69020,69022,69024,69026,69028,69030,69032],{"class":47,"line":63},[45,69019,2065],{"class":92},[45,69021,96],{"class":85},[45,69023,5000],{"class":2070},[45,69025,9680],{"class":85},[45,69027,10482],{"class":59},[45,69029,457],{"class":85},[45,69031,11184],{"class":59},[45,69033,21941],{"class":85},[14,69035,69036,69037,69040,69041,61842,69043,240],{},"This gives ",[18,69038,69039],{},"\"Pyt\""," because it starts at index ",[18,69042,10482],{},[18,69044,11184],{},[31,69046,69048],{"id":69047},"common-string-operations","Common string operations",[14,69050,69051],{},"You can do several simple operations with strings.",[712,69053,69055,69056],{"id":69054},"join-strings-with","Join strings with ",[18,69057,8850],{},[14,69059,165,69060,69062],{},[18,69061,8850],{}," to combine strings.",[36,69064,69066],{"className":38,"code":69065,"language":40,"meta":41,"style":41},"first = \"Hello\"\nsecond = \"World\"\n\nresult = first + \" \" + second\nprint(result)\n",[18,69067,69068,69081,69094,69098,69118],{"__ignoreMap":41},[45,69069,69070,69073,69075,69077,69079],{"class":47,"line":48},[45,69071,69072],{"class":51},"first ",[45,69074,56],{"class":55},[45,69076,1565],{"class":99},[45,69078,3226],{"class":103},[45,69080,1571],{"class":99},[45,69082,69083,69086,69088,69090,69092],{"class":47,"line":63},[45,69084,69085],{"class":51},"second ",[45,69087,56],{"class":55},[45,69089,1565],{"class":99},[45,69091,48789],{"class":103},[45,69093,1571],{"class":99},[45,69095,69096],{"class":47,"line":70},[45,69097,67],{"emptyLinePlaceholder":66},[45,69099,69100,69102,69104,69107,69109,69111,69113,69115],{"class":47,"line":89},[45,69101,3595],{"class":51},[45,69103,56],{"class":55},[45,69105,69106],{"class":51}," first ",[45,69108,8850],{"class":55},[45,69110,1565],{"class":99},[45,69112,1565],{"class":99},[45,69114,3775],{"class":55},[45,69116,69117],{"class":51}," second\n",[45,69119,69120,69122,69124,69126],{"class":47,"line":112},[45,69121,2065],{"class":92},[45,69123,96],{"class":85},[45,69125,3620],{"class":2070},[45,69127,109],{"class":85},[14,69129,177],{},[36,69131,69133],{"className":38,"code":69132,"language":40,"meta":41,"style":41},"Hello World\n",[18,69134,69135],{"__ignoreMap":41},[45,69136,69137],{"class":47,"line":48},[45,69138,69132],{"class":51},[712,69140,69142,69143],{"id":69141},"repeat-strings-with","Repeat strings with ",[18,69144,5601],{},[14,69146,165,69147,69149],{},[18,69148,5601],{}," to repeat a string.",[36,69151,69153],{"className":38,"code":69152,"language":40,"meta":41,"style":41},"print(\"ha\" * 3)\n",[18,69154,69155],{"__ignoreMap":41},[45,69156,69157,69159,69161,69163,69166,69168,69170,69172],{"class":47,"line":48},[45,69158,2065],{"class":92},[45,69160,96],{"class":85},[45,69162,100],{"class":99},[45,69164,69165],{"class":103},"ha",[45,69167,100],{"class":99},[45,69169,3559],{"class":55},[45,69171,777],{"class":59},[45,69173,109],{"class":85},[14,69175,177],{},[36,69177,69179],{"className":38,"code":69178,"language":40,"meta":41,"style":41},"hahaha\n",[18,69180,69181],{"__ignoreMap":41},[45,69182,69183],{"class":47,"line":48},[45,69184,69178],{"class":51},[712,69186,69188,69189],{"id":69187},"get-the-length-with-len","Get the length with ",[18,69190,59071],{},[14,69192,165,69193,69195],{},[18,69194,59071],{}," to count how many characters are in a string.",[36,69197,69199],{"className":38,"code":69198,"language":40,"meta":41,"style":41},"text = \"Python\"\nprint(len(text))\n",[18,69200,69201,69213],{"__ignoreMap":41},[45,69202,69203,69205,69207,69209,69211],{"class":47,"line":48},[45,69204,10297],{"class":51},[45,69206,56],{"class":55},[45,69208,1565],{"class":99},[45,69210,16791],{"class":103},[45,69212,1571],{"class":99},[45,69214,69215,69217,69219,69221,69223,69225],{"class":47,"line":63},[45,69216,2065],{"class":92},[45,69218,96],{"class":85},[45,69220,28716],{"class":92},[45,69222,96],{"class":85},[45,69224,5000],{"class":2070},[45,69226,4348],{"class":85},[14,69228,177],{},[36,69230,69231],{"className":38,"code":11797,"language":40,"meta":41,"style":41},[18,69232,69233],{"__ignoreMap":41},[45,69234,69235],{"class":47,"line":48},[45,69236,11797],{"class":59},[14,69238,31233,69239,240],{},[236,69240,59142],{"href":59141},[712,69242,69244,69245],{"id":69243},"check-for-text-with-in","Check for text with ",[18,69246,8623],{},[14,69248,165,69249,69251],{},[18,69250,8623],{}," to check whether one string appears inside another.",[36,69253,69255],{"className":38,"code":69254,"language":40,"meta":41,"style":41},"text = \"Python programming\"\n\nprint(\"Python\" in text)\nprint(\"java\" in text)\n",[18,69256,69257,69270,69274,69292],{"__ignoreMap":41},[45,69258,69259,69261,69263,69265,69268],{"class":47,"line":48},[45,69260,10297],{"class":51},[45,69262,56],{"class":55},[45,69264,1565],{"class":99},[45,69266,69267],{"class":103},"Python programming",[45,69269,1571],{"class":99},[45,69271,69272],{"class":47,"line":63},[45,69273,67],{"emptyLinePlaceholder":66},[45,69275,69276,69278,69280,69282,69284,69286,69288,69290],{"class":47,"line":70},[45,69277,2065],{"class":92},[45,69279,96],{"class":85},[45,69281,100],{"class":99},[45,69283,16791],{"class":103},[45,69285,100],{"class":99},[45,69287,45650],{"class":73},[45,69289,10339],{"class":2070},[45,69291,109],{"class":85},[45,69293,69294,69296,69298,69300,69303,69305,69307,69309],{"class":47,"line":89},[45,69295,2065],{"class":92},[45,69297,96],{"class":85},[45,69299,100],{"class":99},[45,69301,69302],{"class":103},"java",[45,69304,100],{"class":99},[45,69306,45650],{"class":73},[45,69308,10339],{"class":2070},[45,69310,109],{"class":85},[14,69312,177],{},[36,69314,69315],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,69316,69317,69321],{"__ignoreMap":41},[45,69318,69319],{"class":47,"line":48},[45,69320,2270],{"class":481},[45,69322,69323],{"class":47,"line":63},[45,69324,2265],{"class":481},[31,69326,69328],{"id":69327},"useful-string-methods","Useful string methods",[14,69330,69331],{},"String methods are built-in tools that help you work with text.",[712,69333,69334],{"id":28323},[18,69335,69336],{},"lower()",[14,69338,69339,69341],{},[18,69340,69336],{}," makes all letters lowercase.",[36,69343,69345],{"className":38,"code":69344,"language":40,"meta":41,"style":41},"text = \"PyThOn\"\nprint(text.lower())\n",[18,69346,69347,69360],{"__ignoreMap":41},[45,69348,69349,69351,69353,69355,69358],{"class":47,"line":48},[45,69350,10297],{"class":51},[45,69352,56],{"class":55},[45,69354,1565],{"class":99},[45,69356,69357],{"class":103},"PyThOn",[45,69359,1571],{"class":99},[45,69361,69362,69364,69366,69368,69370,69372],{"class":47,"line":63},[45,69363,2065],{"class":92},[45,69365,96],{"class":85},[45,69367,5000],{"class":2070},[45,69369,240],{"class":85},[45,69371,28323],{"class":2070},[45,69373,10777],{"class":85},[14,69375,177],{},[36,69377,69378],{"className":38,"code":16990,"language":40,"meta":41,"style":41},[18,69379,69380],{"__ignoreMap":41},[45,69381,69382],{"class":47,"line":48},[45,69383,16990],{"class":51},[14,69385,26005,69386,240],{},[236,69387,69389],{"href":69388},"\u002Freference\u002Fpython-string-lower-method\u002F","Python string lower() method",[712,69391,69392],{"id":10327},[18,69393,69394],{},"upper()",[14,69396,69397,69399],{},[18,69398,69394],{}," makes all letters uppercase.",[36,69401,69403],{"className":38,"code":69402,"language":40,"meta":41,"style":41},"text = \"Python\"\nprint(text.upper())\n",[18,69404,69405,69417],{"__ignoreMap":41},[45,69406,69407,69409,69411,69413,69415],{"class":47,"line":48},[45,69408,10297],{"class":51},[45,69410,56],{"class":55},[45,69412,1565],{"class":99},[45,69414,16791],{"class":103},[45,69416,1571],{"class":99},[45,69418,69419,69421,69423,69425,69427,69429],{"class":47,"line":63},[45,69420,2065],{"class":92},[45,69422,96],{"class":85},[45,69424,5000],{"class":2070},[45,69426,240],{"class":85},[45,69428,10327],{"class":2070},[45,69430,10777],{"class":85},[14,69432,177],{},[36,69434,69435],{"className":38,"code":68423,"language":40,"meta":41,"style":41},[18,69436,69437],{"__ignoreMap":41},[45,69438,69439],{"class":47,"line":48},[45,69440,68423],{"class":2771},[14,69442,26005,69443,240],{},[236,69444,69446],{"href":69445},"\u002Freference\u002Fpython-string-upper-method\u002F","Python string upper() method",[712,69448,69449],{"id":24046},[18,69450,69451],{},"strip()",[14,69453,69454,69456],{},[18,69455,69451],{}," removes whitespace from the start and end of a string.",[36,69458,69460],{"className":38,"code":69459,"language":40,"meta":41,"style":41},"text = \"  hello  \"\nprint(text.strip())\n",[18,69461,69462,69475],{"__ignoreMap":41},[45,69463,69464,69466,69468,69470,69473],{"class":47,"line":48},[45,69465,10297],{"class":51},[45,69467,56],{"class":55},[45,69469,1565],{"class":99},[45,69471,69472],{"class":103},"  hello  ",[45,69474,1571],{"class":99},[45,69476,69477,69479,69481,69483,69485,69487],{"class":47,"line":63},[45,69478,2065],{"class":92},[45,69480,96],{"class":85},[45,69482,5000],{"class":2070},[45,69484,240],{"class":85},[45,69486,24046],{"class":2070},[45,69488,10777],{"class":85},[14,69490,177],{},[36,69492,69493],{"className":38,"code":31468,"language":40,"meta":41,"style":41},[18,69494,69495],{"__ignoreMap":41},[45,69496,69497],{"class":47,"line":48},[45,69498,31468],{"class":51},[14,69500,26005,69501,240],{},[236,69502,69504],{"href":69503},"\u002Freference\u002Fpython-string-strip-method\u002F","Python string strip() method",[712,69506,69507],{"id":31361},[18,69508,69509],{},"replace()",[14,69511,69512,69514],{},[18,69513,69509],{}," changes one part of the text into another.",[36,69516,69518],{"className":38,"code":69517,"language":40,"meta":41,"style":41},"text = \"I like cats\"\nprint(text.replace(\"cats\", \"dogs\"))\n",[18,69519,69520,69533],{"__ignoreMap":41},[45,69521,69522,69524,69526,69528,69531],{"class":47,"line":48},[45,69523,10297],{"class":51},[45,69525,56],{"class":55},[45,69527,1565],{"class":99},[45,69529,69530],{"class":103},"I like cats",[45,69532,1571],{"class":99},[45,69534,69535,69537,69539,69541,69543,69545,69547,69549,69552,69554,69556,69558,69561,69563],{"class":47,"line":63},[45,69536,2065],{"class":92},[45,69538,96],{"class":85},[45,69540,5000],{"class":2070},[45,69542,240],{"class":85},[45,69544,31361],{"class":2070},[45,69546,96],{"class":85},[45,69548,100],{"class":99},[45,69550,69551],{"class":103},"cats",[45,69553,100],{"class":99},[45,69555,3079],{"class":85},[45,69557,1565],{"class":99},[45,69559,69560],{"class":103},"dogs",[45,69562,100],{"class":99},[45,69564,4348],{"class":85},[14,69566,177],{},[36,69568,69570],{"className":38,"code":69569,"language":40,"meta":41,"style":41},"I like dogs\n",[18,69571,69572],{"__ignoreMap":41},[45,69573,69574],{"class":47,"line":48},[45,69575,69569],{"class":51},[14,69577,26005,69578,240],{},[236,69579,69581],{"href":69580},"\u002Freference\u002Fpython-string-replace-method\u002F","Python string replace() method",[712,69583,69585],{"id":69584},"split",[18,69586,69587],{},"split()",[14,69589,69590,69592],{},[18,69591,69587],{}," turns a string into a list.",[36,69594,69596],{"className":38,"code":69595,"language":40,"meta":41,"style":41},"text = \"red,green,blue\"\ncolors = text.split(\",\")\n\nprint(colors)\n",[18,69597,69598,69611,69633,69637],{"__ignoreMap":41},[45,69599,69600,69602,69604,69606,69609],{"class":47,"line":48},[45,69601,10297],{"class":51},[45,69603,56],{"class":55},[45,69605,1565],{"class":99},[45,69607,69608],{"class":103},"red,green,blue",[45,69610,1571],{"class":99},[45,69612,69613,69615,69617,69619,69621,69623,69625,69627,69629,69631],{"class":47,"line":63},[45,69614,22320],{"class":51},[45,69616,56],{"class":55},[45,69618,10339],{"class":51},[45,69620,240],{"class":85},[45,69622,69584],{"class":2070},[45,69624,96],{"class":85},[45,69626,100],{"class":99},[45,69628,3079],{"class":103},[45,69630,100],{"class":99},[45,69632,109],{"class":85},[45,69634,69635],{"class":47,"line":70},[45,69636,67],{"emptyLinePlaceholder":66},[45,69638,69639,69641,69643,69645],{"class":47,"line":89},[45,69640,2065],{"class":92},[45,69642,96],{"class":85},[45,69644,22407],{"class":2070},[45,69646,109],{"class":85},[14,69648,177],{},[36,69650,69652],{"className":38,"code":69651,"language":40,"meta":41,"style":41},"['red', 'green', 'blue']\n",[18,69653,69654],{"__ignoreMap":41},[45,69655,69656,69658,69660,69662,69664,69666,69668,69670,69672,69674,69676,69678,69680],{"class":47,"line":48},[45,69657,9680],{"class":85},[45,69659,7901],{"class":99},[45,69661,22329],{"class":103},[45,69663,7901],{"class":99},[45,69665,3079],{"class":85},[45,69667,9199],{"class":99},[45,69669,22338],{"class":103},[45,69671,7901],{"class":99},[45,69673,3079],{"class":85},[45,69675,9199],{"class":99},[45,69677,22347],{"class":103},[45,69679,7901],{"class":99},[45,69681,8594],{"class":85},[14,69683,26005,69684,230,69688,240],{},[236,69685,69687],{"href":69686},"\u002Freference\u002Fpython-string-split-method\u002F","Python string split() method",[236,69689,69691],{"href":69690},"\u002Fhow-to\u002Fhow-to-split-a-string-in-python\u002F","how to split a string in Python",[712,69693,69695],{"id":69694},"join",[18,69696,69697],{},"join()",[14,69699,69700,69702],{},[18,69701,69697],{}," joins multiple strings into one string.",[36,69704,69706],{"className":38,"code":69705,"language":40,"meta":41,"style":41},"words = [\"Python\", \"is\", \"fun\"]\nresult = \" \".join(words)\n\nprint(result)\n",[18,69707,69708,69741,69762,69766],{"__ignoreMap":41},[45,69709,69710,69712,69714,69716,69718,69720,69722,69724,69726,69728,69730,69732,69734,69737,69739],{"class":47,"line":48},[45,69711,9088],{"class":51},[45,69713,56],{"class":55},[45,69715,8575],{"class":85},[45,69717,100],{"class":99},[45,69719,16791],{"class":103},[45,69721,100],{"class":99},[45,69723,3079],{"class":85},[45,69725,1565],{"class":99},[45,69727,1285],{"class":103},[45,69729,100],{"class":99},[45,69731,3079],{"class":85},[45,69733,1565],{"class":99},[45,69735,69736],{"class":103},"fun",[45,69738,100],{"class":99},[45,69740,8594],{"class":85},[45,69742,69743,69745,69747,69749,69751,69753,69755,69757,69760],{"class":47,"line":63},[45,69744,3595],{"class":51},[45,69746,56],{"class":55},[45,69748,1565],{"class":99},[45,69750,1565],{"class":99},[45,69752,240],{"class":85},[45,69754,69694],{"class":2070},[45,69756,96],{"class":85},[45,69758,69759],{"class":2070},"words",[45,69761,109],{"class":85},[45,69763,69764],{"class":47,"line":70},[45,69765,67],{"emptyLinePlaceholder":66},[45,69767,69768,69770,69772,69774],{"class":47,"line":89},[45,69769,2065],{"class":92},[45,69771,96],{"class":85},[45,69773,3620],{"class":2070},[45,69775,109],{"class":85},[14,69777,177],{},[36,69779,69781],{"className":38,"code":69780,"language":40,"meta":41,"style":41},"Python is fun\n",[18,69782,69783],{"__ignoreMap":41},[45,69784,69785,69787,69789],{"class":47,"line":48},[45,69786,18104],{"class":51},[45,69788,1285],{"class":55},[45,69790,69791],{"class":51}," fun\n",[14,69793,26005,69794,230,69798,240],{},[236,69795,69797],{"href":69796},"\u002Freference\u002Fpython-string-join-method\u002F","Python string join() method",[236,69799,69801],{"href":69800},"\u002Fhow-to\u002Fhow-to-join-strings-in-python\u002F","how to join strings in Python",[31,69803,69805],{"id":69804},"strings-are-immutable","Strings are immutable",[14,69807,69808,69809,240],{},"Strings are ",[2421,69810,29556],{},[14,69812,69813],{},"This means you cannot change one character directly.",[14,69815,65888],{},[36,69817,69819],{"className":38,"code":69818,"language":40,"meta":41,"style":41},"text = \"hello\"\ntext[0] = \"H\"\n",[18,69820,69821,69833],{"__ignoreMap":41},[45,69822,69823,69825,69827,69829,69831],{"class":47,"line":48},[45,69824,10297],{"class":51},[45,69826,56],{"class":55},[45,69828,1565],{"class":99},[45,69830,18782],{"class":103},[45,69832,1571],{"class":99},[45,69834,69835,69837,69839,69841,69843,69845,69847,69849],{"class":47,"line":63},[45,69836,5000],{"class":51},[45,69838,9680],{"class":85},[45,69840,10482],{"class":59},[45,69842,9685],{"class":85},[45,69844,3099],{"class":55},[45,69846,1565],{"class":99},[45,69848,31412],{"class":103},[45,69850,1571],{"class":99},[14,69852,69853],{},"Python does not allow direct character assignment in strings.",[14,69855,69856],{},"Instead, create a new string:",[36,69858,69860],{"className":38,"code":69859,"language":40,"meta":41,"style":41},"text = \"hello\"\ntext = \"H\" + text[1:]\n\nprint(text)\n",[18,69861,69862,69874,69897,69901],{"__ignoreMap":41},[45,69863,69864,69866,69868,69870,69872],{"class":47,"line":48},[45,69865,10297],{"class":51},[45,69867,56],{"class":55},[45,69869,1565],{"class":99},[45,69871,18782],{"class":103},[45,69873,1571],{"class":99},[45,69875,69876,69878,69880,69882,69884,69886,69888,69890,69892,69894],{"class":47,"line":63},[45,69877,10297],{"class":51},[45,69879,56],{"class":55},[45,69881,1565],{"class":99},[45,69883,31412],{"class":103},[45,69885,100],{"class":99},[45,69887,3775],{"class":55},[45,69889,10339],{"class":51},[45,69891,9680],{"class":85},[45,69893,8578],{"class":59},[45,69895,69896],{"class":85},":]\n",[45,69898,69899],{"class":47,"line":70},[45,69900,67],{"emptyLinePlaceholder":66},[45,69902,69903,69905,69907,69909],{"class":47,"line":89},[45,69904,2065],{"class":92},[45,69906,96],{"class":85},[45,69908,5000],{"class":2070},[45,69910,109],{"class":85},[14,69912,177],{},[36,69914,69915],{"className":38,"code":15823,"language":40,"meta":41,"style":41},[18,69916,69917],{"__ignoreMap":41},[45,69918,69919],{"class":47,"line":48},[45,69920,15823],{"class":51},[14,69922,69923],{},"This is an important beginner idea. When a string changes, Python creates a new string instead of changing the old one in place.",[31,69925,69927],{"id":69926},"comparing-strings","Comparing strings",[14,69929,69930,69931,240],{},"You can compare strings with ",[18,69932,1129],{},[36,69934,69936],{"className":38,"code":69935,"language":40,"meta":41,"style":41},"print(\"Python\" == \"Python\")\nprint(\"Python\" == \"python\")\n",[18,69937,69938,69960],{"__ignoreMap":41},[45,69939,69940,69942,69944,69946,69948,69950,69952,69954,69956,69958],{"class":47,"line":48},[45,69941,2065],{"class":92},[45,69943,96],{"class":85},[45,69945,100],{"class":99},[45,69947,16791],{"class":103},[45,69949,100],{"class":99},[45,69951,9334],{"class":55},[45,69953,1565],{"class":99},[45,69955,16791],{"class":103},[45,69957,100],{"class":99},[45,69959,109],{"class":85},[45,69961,69962,69964,69966,69968,69970,69972,69974,69976,69978,69980],{"class":47,"line":63},[45,69963,2065],{"class":92},[45,69965,96],{"class":85},[45,69967,100],{"class":99},[45,69969,16791],{"class":103},[45,69971,100],{"class":99},[45,69973,9334],{"class":55},[45,69975,1565],{"class":99},[45,69977,40],{"class":103},[45,69979,100],{"class":99},[45,69981,109],{"class":85},[14,69983,177],{},[36,69985,69986],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,69987,69988,69992],{"__ignoreMap":41},[45,69989,69990],{"class":47,"line":48},[45,69991,2270],{"class":481},[45,69993,69994],{"class":47,"line":63},[45,69995,2265],{"class":481},[14,69997,69998],{},"String comparisons are case-sensitive. That means uppercase and lowercase letters matter.",[14,70000,70001],{},"If you want a case-insensitive comparison, convert both strings first:",[36,70003,70005],{"className":38,"code":70004,"language":40,"meta":41,"style":41},"a = \"Python\"\nb = \"python\"\n\nprint(a.lower() == b.lower())\n",[18,70006,70007,70019,70031,70035],{"__ignoreMap":41},[45,70008,70009,70011,70013,70015,70017],{"class":47,"line":48},[45,70010,11541],{"class":51},[45,70012,56],{"class":55},[45,70014,1565],{"class":99},[45,70016,16791],{"class":103},[45,70018,1571],{"class":99},[45,70020,70021,70023,70025,70027,70029],{"class":47,"line":63},[45,70022,19315],{"class":51},[45,70024,56],{"class":55},[45,70026,1565],{"class":99},[45,70028,40],{"class":103},[45,70030,1571],{"class":99},[45,70032,70033],{"class":47,"line":70},[45,70034,67],{"emptyLinePlaceholder":66},[45,70036,70037,70039,70041,70043,70045,70047,70049,70051,70053,70055,70057],{"class":47,"line":89},[45,70038,2065],{"class":92},[45,70040,96],{"class":85},[45,70042,236],{"class":2070},[45,70044,240],{"class":85},[45,70046,28323],{"class":2070},[45,70048,3772],{"class":85},[45,70050,9334],{"class":55},[45,70052,11115],{"class":2070},[45,70054,240],{"class":85},[45,70056,28323],{"class":2070},[45,70058,10777],{"class":85},[14,70060,177],{},[36,70062,70063],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,70064,70065],{"__ignoreMap":41},[45,70066,70067],{"class":47,"line":48},[45,70068,2270],{"class":481},[31,70070,70072],{"id":70071},"when-beginners-use-strings","When beginners use strings",[14,70074,70075],{},"Beginners work with strings all the time, often without noticing it.",[712,70077,70079,70081],{"id":70078},"input-returns-a-string",[18,70080,18020],{}," returns a string",[14,70083,70084,70085,70087],{},"Anything entered with ",[18,70086,18020],{}," comes in as a string.",[36,70089,70091],{"className":38,"code":70090,"language":40,"meta":41,"style":41},"age = input(\"Enter your age: \")\nprint(age)\nprint(type(age))\n",[18,70092,70093,70111,70121],{"__ignoreMap":41},[45,70094,70095,70097,70099,70101,70103,70105,70107,70109],{"class":47,"line":48},[45,70096,52],{"class":51},[45,70098,56],{"class":55},[45,70100,18043],{"class":92},[45,70102,96],{"class":85},[45,70104,100],{"class":99},[45,70106,39490],{"class":103},[45,70108,100],{"class":99},[45,70110,109],{"class":85},[45,70112,70113,70115,70117,70119],{"class":47,"line":63},[45,70114,2065],{"class":92},[45,70116,96],{"class":85},[45,70118,2082],{"class":2070},[45,70120,109],{"class":85},[45,70122,70123,70125,70127,70129,70131,70133],{"class":47,"line":70},[45,70124,2065],{"class":92},[45,70126,96],{"class":85},[45,70128,4340],{"class":2109},[45,70130,96],{"class":85},[45,70132,2082],{"class":2070},[45,70134,4348],{"class":85},[14,70136,70137,70138,230,70140,240],{},"If you need a number, convert it with ",[18,70139,41891],{},[18,70141,41896],{},[36,70143,70145],{"className":38,"code":70144,"language":40,"meta":41,"style":41},"age = input(\"Enter your age: \")\nage = int(age)\n\nprint(age + 1)\n",[18,70146,70147,70165,70179,70183],{"__ignoreMap":41},[45,70148,70149,70151,70153,70155,70157,70159,70161,70163],{"class":47,"line":48},[45,70150,52],{"class":51},[45,70152,56],{"class":55},[45,70154,18043],{"class":92},[45,70156,96],{"class":85},[45,70158,100],{"class":99},[45,70160,39490],{"class":103},[45,70162,100],{"class":99},[45,70164,109],{"class":85},[45,70166,70167,70169,70171,70173,70175,70177],{"class":47,"line":63},[45,70168,52],{"class":51},[45,70170,56],{"class":55},[45,70172,41669],{"class":2109},[45,70174,96],{"class":85},[45,70176,2082],{"class":2070},[45,70178,109],{"class":85},[45,70180,70181],{"class":47,"line":70},[45,70182,67],{"emptyLinePlaceholder":66},[45,70184,70185,70187,70189,70191,70193,70195],{"class":47,"line":89},[45,70186,2065],{"class":92},[45,70188,96],{"class":85},[45,70190,52],{"class":2070},[45,70192,8850],{"class":55},[45,70194,9030],{"class":59},[45,70196,109],{"class":85},[712,70198,70200],{"id":70199},"file-content-is-often-read-as-strings","File content is often read as strings",[14,70202,70203],{},"When you read text from a file, Python usually gives you strings.",[712,70205,70207],{"id":70206},"mixing-strings-and-numbers-causes-errors","Mixing strings and numbers causes errors",[14,70209,70210],{},"A common beginner mistake is trying to add text and numbers directly.",[36,70212,70214],{"className":38,"code":70213,"language":40,"meta":41,"style":41},"age = 20\nprint(\"Age: \" + age)\n",[18,70215,70216,70224],{"__ignoreMap":41},[45,70217,70218,70220,70222],{"class":47,"line":48},[45,70219,52],{"class":51},[45,70221,56],{"class":55},[45,70223,1883],{"class":59},[45,70225,70226,70228,70230,70232,70234,70236,70238,70240],{"class":47,"line":63},[45,70227,2065],{"class":92},[45,70229,96],{"class":85},[45,70231,100],{"class":99},[45,70233,46584],{"class":103},[45,70235,100],{"class":99},[45,70237,3775],{"class":55},[45,70239,3305],{"class":2070},[45,70241,109],{"class":85},[14,70243,70244,70245,70248,70249,70251],{},"This fails because ",[18,70246,70247],{},"\"Age: \""," is a string and ",[18,70250,2082],{}," is an integer.",[14,70253,70254],{},"Fix it by converting the number:",[36,70256,70258],{"className":38,"code":70257,"language":40,"meta":41,"style":41},"age = 20\nprint(\"Age: \" + str(age))\n",[18,70259,70260,70268],{"__ignoreMap":41},[45,70261,70262,70264,70266],{"class":47,"line":48},[45,70263,52],{"class":51},[45,70265,56],{"class":55},[45,70267,1883],{"class":59},[45,70269,70270,70272,70274,70276,70278,70280,70282,70284,70286,70288],{"class":47,"line":63},[45,70271,2065],{"class":92},[45,70273,96],{"class":85},[45,70275,100],{"class":99},[45,70277,46584],{"class":103},[45,70279,100],{"class":99},[45,70281,3775],{"class":55},[45,70283,38516],{"class":2109},[45,70285,96],{"class":85},[45,70287,2082],{"class":2070},[45,70289,4348],{"class":85},[14,70291,50816,70292,240],{},[236,70293,42381],{"href":42380},[31,70295,5903],{"id":5902},[14,70297,70298],{},"Here are some common problems beginners have with strings:",[195,70300,70301,70304,70307,70312,70315],{},[198,70302,70303],{},"Using quotes incorrectly when creating text",[198,70305,70306],{},"Trying to change a string character directly",[198,70308,70309,70310],{},"Mixing strings and numbers with ",[18,70311,8850],{},[198,70313,70314],{},"Using the wrong index position",[198,70316,2043,70317,70081],{},[18,70318,18020],{},[14,70320,70321],{},"These quick checks can help when debugging:",[36,70323,70325],{"className":38,"code":70324,"language":40,"meta":41,"style":41},"print(text)\nprint(type(text))\nprint(len(text))\nprint(text[0])\nprint(text.lower())\n",[18,70326,70327,70337,70351,70365,70379],{"__ignoreMap":41},[45,70328,70329,70331,70333,70335],{"class":47,"line":48},[45,70330,2065],{"class":92},[45,70332,96],{"class":85},[45,70334,5000],{"class":2070},[45,70336,109],{"class":85},[45,70338,70339,70341,70343,70345,70347,70349],{"class":47,"line":63},[45,70340,2065],{"class":92},[45,70342,96],{"class":85},[45,70344,4340],{"class":2109},[45,70346,96],{"class":85},[45,70348,5000],{"class":2070},[45,70350,4348],{"class":85},[45,70352,70353,70355,70357,70359,70361,70363],{"class":47,"line":70},[45,70354,2065],{"class":92},[45,70356,96],{"class":85},[45,70358,28716],{"class":92},[45,70360,96],{"class":85},[45,70362,5000],{"class":2070},[45,70364,4348],{"class":85},[45,70366,70367,70369,70371,70373,70375,70377],{"class":47,"line":89},[45,70368,2065],{"class":92},[45,70370,96],{"class":85},[45,70372,5000],{"class":2070},[45,70374,9680],{"class":85},[45,70376,10482],{"class":59},[45,70378,21941],{"class":85},[45,70380,70381,70383,70385,70387,70389,70391],{"class":47,"line":112},[45,70382,2065],{"class":92},[45,70384,96],{"class":85},[45,70386,5000],{"class":2070},[45,70388,240],{"class":85},[45,70390,28323],{"class":2070},[45,70392,10777],{"class":85},[14,70394,33334],{},[195,70396,70397,70403,70409,70415,70421],{},[198,70398,70399,70402],{},[18,70400,70401],{},"print(text)"," shows the actual value",[198,70404,70405,70408],{},[18,70406,70407],{},"print(type(text))"," shows whether it is a string",[198,70410,70411,70414],{},[18,70412,70413],{},"print(len(text))"," shows the number of characters",[198,70416,70417,70420],{},[18,70418,70419],{},"print(text[0])"," checks the first character",[198,70422,70423,70426],{},[18,70424,70425],{},"print(text.lower())"," helps test case-insensitive text handling",[31,70428,2281],{"id":2280},[712,70430,70432],{"id":70431},"what-is-a-string-in-python","What is a string in Python?",[14,70434,70435],{},"A string is a sequence of characters used to store text.",[712,70437,70439],{"id":70438},"how-do-i-create-a-string-in-python","How do I create a string in Python?",[14,70441,70442],{},"Put text inside single quotes, double quotes, or triple quotes.",[712,70444,70446],{"id":70445},"can-i-change-one-character-in-a-string","Can I change one character in a string?",[14,70448,70449],{},"No. Strings are immutable, so you must create a new string.",[712,70451,70453],{"id":70452},"what-is-the-difference-between-indexing-and-slicing","What is the difference between indexing and slicing?",[14,70455,70456],{},"Indexing gets one character. Slicing gets part of the string.",[712,70458,70460],{"id":70459},"why-does-adding-a-string-and-a-number-fail","Why does adding a string and a number fail?",[14,70462,70463,70464,70466,70467,70469],{},"They are different data types. Convert the number with ",[18,70465,41901],{}," or the string with ",[18,70468,41891],{}," when appropriate.",[31,70471,2342],{"id":2341},[195,70473,70474,70478,70482,70487,70493,70499,70505],{},[198,70475,70476],{},[236,70477,70432],{"href":68477},[198,70479,70480],{},[236,70481,35582],{"href":35108},[198,70483,70484],{},[236,70485,70486],{"href":69690},"How to split a string in Python",[198,70488,70489],{},[236,70490,70492],{"href":70491},"\u002Fhow-to\u002Fhow-to-replace-text-in-a-string-in-python\u002F","How to replace text in a string in Python",[198,70494,70495],{},[236,70496,70498],{"href":70497},"\u002Fhow-to\u002Fhow-to-remove-whitespace-from-a-string-in-python\u002F","How to remove whitespace from a string in Python",[198,70500,70501],{},[236,70502,70504],{"href":70503},"\u002Fhow-to\u002Fhow-to-format-strings-in-python\u002F","How to format strings in Python",[198,70506,70507],{},[236,70508,42381],{"href":42380},[2367,70510,70511],{},"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 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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":70513},[70514,70515,70516,70520,70521,70525,70535,70543,70544,70545,70551,70552,70559],{"id":33,"depth":63,"text":34},{"id":68426,"depth":63,"text":68427},{"id":68481,"depth":63,"text":68482,"children":70517},[70518,70519],{"id":68518,"depth":70,"text":68519},{"id":68566,"depth":70,"text":68567},{"id":68611,"depth":63,"text":68612},{"id":68775,"depth":63,"text":68776,"children":70522},[70523,70524],{"id":68779,"depth":70,"text":68780},{"id":68847,"depth":70,"text":68848},{"id":69047,"depth":63,"text":69048,"children":70526},[70527,70529,70531,70533],{"id":69054,"depth":70,"text":70528},"Join strings with +",{"id":69141,"depth":70,"text":70530},"Repeat strings with *",{"id":69187,"depth":70,"text":70532},"Get the length with len()",{"id":69243,"depth":70,"text":70534},"Check for text with in",{"id":69327,"depth":63,"text":69328,"children":70536},[70537,70538,70539,70540,70541,70542],{"id":28323,"depth":70,"text":69336},{"id":10327,"depth":70,"text":69394},{"id":24046,"depth":70,"text":69451},{"id":31361,"depth":70,"text":69509},{"id":69584,"depth":70,"text":69587},{"id":69694,"depth":70,"text":69697},{"id":69804,"depth":63,"text":69805},{"id":69926,"depth":63,"text":69927},{"id":70071,"depth":63,"text":70072,"children":70546},[70547,70549,70550],{"id":70078,"depth":70,"text":70548},"input() returns a string",{"id":70199,"depth":70,"text":70200},{"id":70206,"depth":70,"text":70207},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":70553},[70554,70555,70556,70557,70558],{"id":70431,"depth":70,"text":70432},{"id":70438,"depth":70,"text":70439},{"id":70445,"depth":70,"text":70446},{"id":70452,"depth":70,"text":70453},{"id":70459,"depth":70,"text":70460},{"id":2341,"depth":63,"text":2342},"Master python strings explained basics and examples in our comprehensive Python beginner guide.",{},{"title":68286,"description":70560},"learn\u002Fpython-strings-explained-basics-and-examples","kfHHXTF-MRjM-10JYe665ugYLveEu3W_Bz1-_LnXxA8",{"id":70566,"title":70567,"body":70568,"description":72221,"extension":2399,"meta":72222,"navigation":66,"path":72223,"seo":72224,"stem":72225,"__hash__":72226},"content\u002Flearn\u002Fpython-syntax-basics-explained.md","Python Syntax Basics Explained",{"type":7,"value":70569,"toc":72195},[70570,70573,70576,70582,70584,70587,70638,70640,70666,70668,70676,70680,70683,70686,70694,70701,70705,70711,70713,70740,70743,70762,70769,70773,70776,70778,70828,70831,70834,70842,70845,70849,70852,70855,70857,70901,70909,70912,70955,70958,70961,70972,70980,70984,70989,70991,71013,71017,71062,71064,71078,71081,71111,71116,71120,71125,71127,71149,71152,71155,71166,71168,71198,71201,71207,71211,71218,71221,71236,71238,71265,71268,71271,71298,71306,71319,71325,71329,71332,71334,71353,71358,71364,71391,71393,71405,71408,71437,71440,71457,71460,71483,71490,71494,71497,71583,71586,71621,71623,71646,71649,71670,71674,71677,71710,71713,71717,71759,71762,71772,71775,71818,71822,71865,71867,71874,71876,71918,71922,71938,71940,71950,71952,71970,71974,71993,71995,72003,72005,72023,72026,72036,72039,72059,72062,72077,72083,72102,72107,72109,72113,72116,72120,72125,72129,72140,72144,72147,72151,72154,72156,72187,72192],[10,70571,70567],{"id":70572},"python-syntax-basics-explained",[14,70574,70575],{},"Python syntax is the set of rules for writing Python code correctly. If your code follows these rules, Python can read and run it.",[14,70577,70578,70579,70581],{},"This page explains the basic writing rules you need before learning variables, ",[18,70580,20],{}," statements, loops, and functions. The goal is simple: help you read and write small pieces of valid Python code.",[31,70583,34],{"id":33},[14,70585,70586],{},"Use this small example to see the main syntax rules in one place:",[36,70588,70590],{"className":38,"code":70589,"language":40,"meta":41,"style":41},"name = \"Sam\"\nage = 12\n\nif age > 10:\n    print(name)\n",[18,70591,70592,70604,70612,70616,70628],{"__ignoreMap":41},[45,70593,70594,70596,70598,70600,70602],{"class":47,"line":48},[45,70595,1560],{"class":51},[45,70597,56],{"class":55},[45,70599,1565],{"class":99},[45,70601,1568],{"class":103},[45,70603,1571],{"class":99},[45,70605,70606,70608,70610],{"class":47,"line":63},[45,70607,52],{"class":51},[45,70609,56],{"class":55},[45,70611,1635],{"class":59},[45,70613,70614],{"class":47,"line":70},[45,70615,67],{"emptyLinePlaceholder":66},[45,70617,70618,70620,70622,70624,70626],{"class":47,"line":89},[45,70619,20],{"class":73},[45,70621,76],{"class":51},[45,70623,272],{"class":55},[45,70625,1190],{"class":59},[45,70627,86],{"class":85},[45,70629,70630,70632,70634,70636],{"class":47,"line":112},[45,70631,93],{"class":92},[45,70633,96],{"class":85},[45,70635,3096],{"class":2070},[45,70637,109],{"class":85},[14,70639,32131],{},[195,70641,70642,70647,70652,70658],{},[198,70643,70644,70646],{},[18,70645,56],{}," assigns a value to a variable",[198,70648,70649,70651],{},[18,70650,7698],{}," is a string, so it uses quotes",[198,70653,70654,70657],{},[18,70655,70656],{},"if age > 10:"," ends with a colon",[198,70659,70660,70663,70664,55438],{},[18,70661,70662],{},"print(name)"," is indented because it belongs inside the ",[18,70665,20],{},[14,70667,177],{},[36,70669,70670],{"className":38,"code":29838,"language":40,"meta":41,"style":41},[18,70671,70672],{"__ignoreMap":41},[45,70673,70674],{"class":47,"line":48},[45,70675,29838],{"class":51},[31,70677,70679],{"id":70678},"what-python-syntax-means","What Python syntax means",[14,70681,70682],{},"Syntax means the rules for writing Python code in a form Python understands.",[14,70684,70685],{},"Think of it like grammar in a language:",[195,70687,70688,70691],{},[198,70689,70690],{},"If the rules are correct, the meaning is clear",[198,70692,70693],{},"If the rules are broken, Python cannot run the code",[14,70695,70696,70697,240],{},"This page focuses on the basic writing rules beginners need most. It does not go deep into error fixing, but if your code will not run, see ",[236,70698,4025,70699],{"href":18360},[18,70700,18361],{},[31,70702,70704],{"id":70703},"a-simple-python-statement","A simple Python statement",[14,70706,2419,70707,70710],{},[2421,70708,70709],{},"statement"," is one instruction Python can run.",[14,70712,40802],{},[36,70714,70716],{"className":38,"code":70715,"language":40,"meta":41,"style":41},"print(\"Hello\")\nx = 5\n",[18,70717,70718,70732],{"__ignoreMap":41},[45,70719,70720,70722,70724,70726,70728,70730],{"class":47,"line":48},[45,70721,2065],{"class":92},[45,70723,96],{"class":85},[45,70725,100],{"class":99},[45,70727,3226],{"class":103},[45,70729,100],{"class":99},[45,70731,109],{"class":85},[45,70733,70734,70736,70738],{"class":47,"line":63},[45,70735,1169],{"class":51},[45,70737,56],{"class":55},[45,70739,733],{"class":59},[14,70741,70742],{},"What these statements do:",[195,70744,70745,70751],{},[198,70746,70747,70750],{},[18,70748,70749],{},"print(\"Hello\")"," displays text",[198,70752,70753,70756,70757,70759,70760],{},[18,70754,70755],{},"x = 5"," stores the number ",[18,70758,3749],{}," in a variable named ",[18,70761,26156],{},[14,70763,70764,70765,240],{},"Many beginner programs are just small statements written one after another. If you want to learn the difference between statements and expressions later, see ",[236,70766,70768],{"href":70767},"\u002Flearn\u002Funderstanding-python-statements-and-expressions\u002F","understanding Python statements and expressions",[31,70770,70772],{"id":70771},"how-python-uses-new-lines","How Python uses new lines",[14,70774,70775],{},"In Python, a statement usually ends at the end of a line.",[14,70777,2115],{},[36,70779,70781],{"className":38,"code":70780,"language":40,"meta":41,"style":41},"print(\"One\")\nprint(\"Two\")\nprint(\"Three\")\n",[18,70782,70783,70798,70813],{"__ignoreMap":41},[45,70784,70785,70787,70789,70791,70794,70796],{"class":47,"line":48},[45,70786,2065],{"class":92},[45,70788,96],{"class":85},[45,70790,100],{"class":99},[45,70792,70793],{"class":103},"One",[45,70795,100],{"class":99},[45,70797,109],{"class":85},[45,70799,70800,70802,70804,70806,70809,70811],{"class":47,"line":63},[45,70801,2065],{"class":92},[45,70803,96],{"class":85},[45,70805,100],{"class":99},[45,70807,70808],{"class":103},"Two",[45,70810,100],{"class":99},[45,70812,109],{"class":85},[45,70814,70815,70817,70819,70821,70824,70826],{"class":47,"line":70},[45,70816,2065],{"class":92},[45,70818,96],{"class":85},[45,70820,100],{"class":99},[45,70822,70823],{"class":103},"Three",[45,70825,100],{"class":99},[45,70827,109],{"class":85},[14,70829,70830],{},"Python reads this as three separate statements.",[14,70832,70833],{},"Because of this, beginner code is usually written as:",[195,70835,70836,70839],{},[198,70837,70838],{},"one statement per line",[198,70840,70841],{},"in a clear top-to-bottom order",[14,70843,70844],{},"Some long statements can continue across lines, but that is not something you need right away. For now, the safest habit is to write one complete instruction on each line.",[31,70846,70848],{"id":70847},"indentation-is-part-of-python-syntax","Indentation is part of Python syntax",[14,70850,70851],{},"In many languages, spaces at the start of a line are just for style. In Python, they are part of the syntax.",[14,70853,70854],{},"Indentation tells Python that a line belongs inside a block.",[14,70856,466],{},[36,70858,70860],{"className":38,"code":70859,"language":40,"meta":41,"style":41},"age = 12\n\nif age > 10:\n    print(\"Older than ten\")\n",[18,70861,70862,70870,70874,70886],{"__ignoreMap":41},[45,70863,70864,70866,70868],{"class":47,"line":48},[45,70865,52],{"class":51},[45,70867,56],{"class":55},[45,70869,1635],{"class":59},[45,70871,70872],{"class":47,"line":63},[45,70873,67],{"emptyLinePlaceholder":66},[45,70875,70876,70878,70880,70882,70884],{"class":47,"line":70},[45,70877,20],{"class":73},[45,70879,76],{"class":51},[45,70881,272],{"class":55},[45,70883,1190],{"class":59},[45,70885,86],{"class":85},[45,70887,70888,70890,70892,70894,70897,70899],{"class":47,"line":89},[45,70889,93],{"class":92},[45,70891,96],{"class":85},[45,70893,100],{"class":99},[45,70895,70896],{"class":103},"Older than ten",[45,70898,100],{"class":99},[45,70900,109],{"class":85},[14,70902,3444,70903,70906,70907,56131],{},[18,70904,70905],{},"print(\"Older than ten\")"," is indented, so Python knows it belongs inside the ",[18,70908,20],{},[14,70910,70911],{},"Without indentation, the code is not valid:",[36,70913,70915],{"className":38,"code":70914,"language":40,"meta":41,"style":41},"age = 12\n\nif age > 10:\nprint(\"Older than ten\")\n",[18,70916,70917,70925,70929,70941],{"__ignoreMap":41},[45,70918,70919,70921,70923],{"class":47,"line":48},[45,70920,52],{"class":51},[45,70922,56],{"class":55},[45,70924,1635],{"class":59},[45,70926,70927],{"class":47,"line":63},[45,70928,67],{"emptyLinePlaceholder":66},[45,70930,70931,70933,70935,70937,70939],{"class":47,"line":70},[45,70932,20],{"class":73},[45,70934,76],{"class":51},[45,70936,272],{"class":55},[45,70938,1190],{"class":59},[45,70940,86],{"class":85},[45,70942,70943,70945,70947,70949,70951,70953],{"class":47,"line":89},[45,70944,2065],{"class":92},[45,70946,96],{"class":85},[45,70948,100],{"class":99},[45,70950,70896],{"class":103},[45,70952,100],{"class":99},[45,70954,109],{"class":85},[14,70956,70957],{},"That causes an error because Python expected an indented block.",[14,70959,70960],{},"Good habits:",[195,70962,70963,70966,70969],{},[198,70964,70965],{},"use 4 spaces for each level of indentation",[198,70967,70968],{},"keep indentation consistent",[198,70970,70971],{},"do not mix tabs and spaces",[14,70973,70974,70975,70977,70978,240],{},"To learn this rule in more detail, read ",[236,70976,554],{"href":553},". If indentation causes an error, see ",[236,70979,1614],{"href":1613},[31,70981,70983],{"id":70982},"colons-start-a-code-block","Colons start a code block",[14,70985,70986,70987,240],{},"In Python, lines that start a block often end with a colon ",[18,70988,457],{},[14,70990,18846],{},[195,70992,70993,70997,71001,71005,71009],{},[198,70994,70995],{},[18,70996,20],{},[198,70998,70999],{},[18,71000,7895],{},[198,71002,71003],{},[18,71004,23529],{},[198,71006,71007],{},[18,71008,2757],{},[198,71010,71011],{},[18,71012,2441],{},[14,71014,18017,71015,457],{},[18,71016,20],{},[36,71018,71020],{"className":38,"code":71019,"language":40,"meta":41,"style":41},"score = 90\n\nif score > 50:\n    print(\"Pass\")\n",[18,71021,71022,71031,71035,71047],{"__ignoreMap":41},[45,71023,71024,71026,71028],{"class":47,"line":48},[45,71025,581],{"class":51},[45,71027,56],{"class":55},[45,71029,71030],{"class":59}," 90\n",[45,71032,71033],{"class":47,"line":63},[45,71034,67],{"emptyLinePlaceholder":66},[45,71036,71037,71039,71041,71043,71045],{"class":47,"line":70},[45,71038,20],{"class":73},[45,71040,597],{"class":51},[45,71042,272],{"class":55},[45,71044,20121],{"class":59},[45,71046,86],{"class":85},[45,71048,71049,71051,71053,71055,71058,71060],{"class":47,"line":89},[45,71050,93],{"class":92},[45,71052,96],{"class":85},[45,71054,100],{"class":99},[45,71056,71057],{"class":103},"Pass",[45,71059,100],{"class":99},[45,71061,109],{"class":85},[14,71063,27794],{},[346,71065,71066,71069,71072,71075],{},[198,71067,71068],{},"Write the block-starting line",[198,71070,71071],{},"End it with a colon",[198,71073,71074],{},"Put the block on the next line",[198,71076,71077],{},"Indent the lines inside the block",[14,71079,71080],{},"Another short example:",[36,71082,71083],{"className":38,"code":57775,"language":40,"meta":41,"style":41},[18,71084,71085,71101],{"__ignoreMap":41},[45,71086,71087,71089,71091,71093,71095,71097,71099],{"class":47,"line":48},[45,71088,7895],{"class":73},[45,71090,36273],{"class":51},[45,71092,8623],{"class":73},[45,71094,10451],{"class":92},[45,71096,96],{"class":85},[45,71098,11184],{"class":59},[45,71100,2464],{"class":85},[45,71102,71103,71105,71107,71109],{"class":47,"line":63},[45,71104,93],{"class":92},[45,71106,96],{"class":85},[45,71108,36312],{"class":2070},[45,71110,109],{"class":85},[14,71112,71113,71114,240],{},"If you forget the colon, Python will raise an error. For that specific problem, see ",[236,71115,1541],{"href":1540},[31,71117,71119],{"id":71118},"comments-are-ignored-by-python","Comments are ignored by Python",[14,71121,71122,71123,240],{},"A comment starts with ",[18,71124,39328],{},[14,71126,466],{},[36,71128,71129],{"className":38,"code":39441,"language":40,"meta":41,"style":41},[18,71130,71131,71135],{"__ignoreMap":41},[45,71132,71133],{"class":47,"line":48},[45,71134,39448],{"class":7624},[45,71136,71137,71139,71141,71143,71145,71147],{"class":47,"line":63},[45,71138,2065],{"class":92},[45,71140,96],{"class":85},[45,71142,100],{"class":99},[45,71144,3226],{"class":103},[45,71146,100],{"class":99},[45,71148,109],{"class":85},[14,71150,71151],{},"Comments are for humans reading the code. Python ignores them when the program runs.",[14,71153,71154],{},"You can use comments to:",[195,71156,71157,71160,71163],{},[198,71158,71159],{},"explain what code does",[198,71161,71162],{},"leave notes for yourself",[198,71164,71165],{},"make code easier to understand later",[14,71167,466],{},[36,71169,71171],{"className":38,"code":71170,"language":40,"meta":41,"style":41},"name = \"Sam\"  # store a name\nprint(name)\n",[18,71172,71173,71188],{"__ignoreMap":41},[45,71174,71175,71177,71179,71181,71183,71185],{"class":47,"line":48},[45,71176,1560],{"class":51},[45,71178,56],{"class":55},[45,71180,1565],{"class":99},[45,71182,1568],{"class":103},[45,71184,100],{"class":99},[45,71186,71187],{"class":7624},"  # store a name\n",[45,71189,71190,71192,71194,71196],{"class":47,"line":63},[45,71191,2065],{"class":92},[45,71193,96],{"class":85},[45,71195,3096],{"class":2070},[45,71197,109],{"class":85},[14,71199,71200],{},"Python runs the code, but ignores the comment.",[14,71202,71203,71204,240],{},"If you want to learn more, see ",[236,71205,71206],{"href":40407},"Python comments explained: single-line and multi-line",[31,71208,71210],{"id":71209},"case-sensitivity-in-python","Case sensitivity in Python",[14,71212,71213,71214,71217],{},"Python is ",[2421,71215,71216],{},"case-sensitive",". That means uppercase and lowercase letters are treated as different.",[14,71219,71220],{},"These are three different names:",[195,71222,71223,71227,71231],{},[198,71224,71225],{},[18,71226,3096],{},[198,71228,71229],{},[18,71230,45465],{},[198,71232,71233],{},[18,71234,71235],{},"NAME",[14,71237,466],{},[36,71239,71241],{"className":38,"code":71240,"language":40,"meta":41,"style":41},"name = \"Sam\"\nprint(name)\n",[18,71242,71243,71255],{"__ignoreMap":41},[45,71244,71245,71247,71249,71251,71253],{"class":47,"line":48},[45,71246,1560],{"class":51},[45,71248,56],{"class":55},[45,71250,1565],{"class":99},[45,71252,1568],{"class":103},[45,71254,1571],{"class":99},[45,71256,71257,71259,71261,71263],{"class":47,"line":63},[45,71258,2065],{"class":92},[45,71260,96],{"class":85},[45,71262,3096],{"class":2070},[45,71264,109],{"class":85},[14,71266,71267],{},"This works.",[14,71269,71270],{},"But this does not:",[36,71272,71274],{"className":38,"code":71273,"language":40,"meta":41,"style":41},"name = \"Sam\"\nprint(Name)\n",[18,71275,71276,71288],{"__ignoreMap":41},[45,71277,71278,71280,71282,71284,71286],{"class":47,"line":48},[45,71279,1560],{"class":51},[45,71281,56],{"class":55},[45,71283,1565],{"class":99},[45,71285,1568],{"class":103},[45,71287,1571],{"class":99},[45,71289,71290,71292,71294,71296],{"class":47,"line":63},[45,71291,2065],{"class":92},[45,71293,96],{"class":85},[45,71295,45465],{"class":2070},[45,71297,109],{"class":85},[14,71299,71300,71301,71303,71304,240],{},"Why? Because ",[18,71302,45465],{}," is not the same as ",[18,71305,3096],{},[14,71307,71308,71309,71311,71312,71314,71315,71318],{},"Wrong capitalization can lead to bugs or errors such as ",[18,71310,35168],{},". This also matters for Python keywords and built-in names. For example, ",[18,71313,2065],{}," is correct, but ",[18,71316,71317],{},"Print"," is not.",[14,71320,71321,71322,240],{},"To learn more about special words in Python, see ",[236,71323,71324],{"href":40159},"Python keywords explained for beginners",[31,71326,71328],{"id":71327},"strings-numbers-and-names","Strings, numbers, and names",[14,71330,71331],{},"In Python, text usually goes in quotes.",[14,71333,466],{},[36,71335,71337],{"className":38,"code":71336,"language":40,"meta":41,"style":41},"print(\"hello\")\n",[18,71338,71339],{"__ignoreMap":41},[45,71340,71341,71343,71345,71347,71349,71351],{"class":47,"line":48},[45,71342,2065],{"class":92},[45,71344,96],{"class":85},[45,71346,100],{"class":99},[45,71348,18782],{"class":103},[45,71350,100],{"class":99},[45,71352,109],{"class":85},[14,71354,3444,71355,71357],{},[18,71356,40627],{}," is a string.",[14,71359,71360,71361,71363],{},"Variable names do ",[2421,71362,2857],{}," use quotes:",[36,71365,71367],{"className":38,"code":71366,"language":40,"meta":41,"style":41},"name = \"hello\"\nprint(name)\n",[18,71368,71369,71381],{"__ignoreMap":41},[45,71370,71371,71373,71375,71377,71379],{"class":47,"line":48},[45,71372,1560],{"class":51},[45,71374,56],{"class":55},[45,71376,1565],{"class":99},[45,71378,18782],{"class":103},[45,71380,1571],{"class":99},[45,71382,71383,71385,71387,71389],{"class":47,"line":63},[45,71384,2065],{"class":92},[45,71386,96],{"class":85},[45,71388,3096],{"class":2070},[45,71390,109],{"class":85},[14,71392,6570],{},[195,71394,71395,71400],{},[198,71396,71397,71399],{},[18,71398,40627],{}," is the text value",[198,71401,71402,71404],{},[18,71403,3096],{}," is the variable name",[14,71406,71407],{},"Compare these two lines:",[36,71409,71411],{"className":38,"code":71410,"language":40,"meta":41,"style":41},"print(\"name\")\nprint(name)\n",[18,71412,71413,71427],{"__ignoreMap":41},[45,71414,71415,71417,71419,71421,71423,71425],{"class":47,"line":48},[45,71416,2065],{"class":92},[45,71418,96],{"class":85},[45,71420,100],{"class":99},[45,71422,3096],{"class":103},[45,71424,100],{"class":99},[45,71426,109],{"class":85},[45,71428,71429,71431,71433,71435],{"class":47,"line":63},[45,71430,2065],{"class":92},[45,71432,96],{"class":85},[45,71434,3096],{"class":2070},[45,71436,109],{"class":85},[14,71438,71439],{},"They are different:",[195,71441,71442,71450],{},[198,71443,71444,71447,71448],{},[18,71445,71446],{},"print(\"name\")"," prints the text ",[18,71449,3096],{},[198,71451,71452,71454,71455],{},[18,71453,70662],{}," prints the value stored in the variable ",[18,71456,3096],{},[14,71458,71459],{},"Numbers also do not use quotes if you want them treated as numbers:",[36,71461,71463],{"className":38,"code":71462,"language":40,"meta":41,"style":41},"age = 12\nprint(age)\n",[18,71464,71465,71473],{"__ignoreMap":41},[45,71466,71467,71469,71471],{"class":47,"line":48},[45,71468,52],{"class":51},[45,71470,56],{"class":55},[45,71472,1635],{"class":59},[45,71474,71475,71477,71479,71481],{"class":47,"line":63},[45,71476,2065],{"class":92},[45,71478,96],{"class":85},[45,71480,2082],{"class":2070},[45,71482,109],{"class":85},[14,71484,71485,71486,71489],{},"If you write ",[18,71487,71488],{},"\"12\""," instead, that is text, not a number.",[31,71491,71493],{"id":71492},"a-small-valid-program","A small valid program",[14,71495,71496],{},"Here is a short program that combines several basic syntax rules:",[36,71498,71500],{"className":38,"code":71499,"language":40,"meta":41,"style":41},"name = \"Lina\"\nage = 15\n\nprint(\"Checking age...\")\n\nif age >= 13:\n    print(name)\n    print(\"Teenager\")\n",[18,71501,71502,71515,71523,71527,71542,71546,71558,71568],{"__ignoreMap":41},[45,71503,71504,71506,71508,71510,71513],{"class":47,"line":48},[45,71505,1560],{"class":51},[45,71507,56],{"class":55},[45,71509,1565],{"class":99},[45,71511,71512],{"class":103},"Lina",[45,71514,1571],{"class":99},[45,71516,71517,71519,71521],{"class":47,"line":63},[45,71518,52],{"class":51},[45,71520,56],{"class":55},[45,71522,56295],{"class":59},[45,71524,71525],{"class":47,"line":70},[45,71526,67],{"emptyLinePlaceholder":66},[45,71528,71529,71531,71533,71535,71538,71540],{"class":47,"line":89},[45,71530,2065],{"class":92},[45,71532,96],{"class":85},[45,71534,100],{"class":99},[45,71536,71537],{"class":103},"Checking age...",[45,71539,100],{"class":99},[45,71541,109],{"class":85},[45,71543,71544],{"class":47,"line":112},[45,71545,67],{"emptyLinePlaceholder":66},[45,71547,71548,71550,71552,71554,71556],{"class":47,"line":126},[45,71549,20],{"class":73},[45,71551,76],{"class":51},[45,71553,600],{"class":55},[45,71555,82],{"class":59},[45,71557,86],{"class":85},[45,71559,71560,71562,71564,71566],{"class":47,"line":142},[45,71561,93],{"class":92},[45,71563,96],{"class":85},[45,71565,3096],{"class":2070},[45,71567,109],{"class":85},[45,71569,71570,71572,71574,71576,71579,71581],{"class":47,"line":149},[45,71571,93],{"class":92},[45,71573,96],{"class":85},[45,71575,100],{"class":99},[45,71577,71578],{"class":103},"Teenager",[45,71580,100],{"class":99},[45,71582,109],{"class":85},[14,71584,71585],{},"What this program uses:",[195,71587,71588,71595,71604,71610,71616],{},[198,71589,71590,71591,1316,71593],{},"variables: ",[18,71592,3096],{},[18,71594,2082],{},[198,71596,71597,71598,1316,71601],{},"strings in quotes: ",[18,71599,71600],{},"\"Lina\"",[18,71602,71603],{},"\"Checking age...\"",[198,71605,71606,71607],{},"a normal statement: ",[18,71608,71609],{},"print(...)",[198,71611,71612,71613,71615],{},"an ",[18,71614,20],{}," statement ending with a colon",[198,71617,71618,71619,55438],{},"indentation inside the ",[18,71620,20],{},[14,71622,17038],{},[36,71624,71626],{"className":38,"code":71625,"language":40,"meta":41,"style":41},"Checking age...\nLina\nTeenager\n",[18,71627,71628,71636,71641],{"__ignoreMap":41},[45,71629,71630,71633],{"class":47,"line":48},[45,71631,71632],{"class":51},"Checking age",[45,71634,71635],{"class":2771},"...\n",[45,71637,71638],{"class":47,"line":63},[45,71639,71640],{"class":51},"Lina\n",[45,71642,71643],{"class":47,"line":70},[45,71644,71645],{"class":51},"Teenager\n",[14,71647,71648],{},"You can use this as a quick syntax checklist:",[195,71650,71651,71653,71656,71659,71664,71667],{},[198,71652,70838],{},[198,71654,71655],{},"quotes around text",[198,71657,71658],{},"no quotes around variable names",[198,71660,71661,71662],{},"colon after ",[18,71663,20],{},[198,71665,71666],{},"indentation inside the block",[198,71668,71669],{},"correct capitalization",[31,71671,71673],{"id":71672},"common-syntax-problems-beginners-make","Common syntax problems beginners make",[14,71675,71676],{},"These are some of the most common beginner mistakes:",[195,71678,71679,71690,71693,71695,71698,71707],{},[198,71680,71681,71682,21,71684,21,71686,15994,71688],{},"Missing the colon after ",[18,71683,20],{},[18,71685,7895],{},[18,71687,23529],{},[18,71689,2757],{},[198,71691,71692],{},"Forgetting to indent code after a colon",[198,71694,58298],{},[198,71696,71697],{},"Leaving a quote, bracket, or parenthesis unclosed",[198,71699,71700,71701,21,71703,15994,71705],{},"Using the wrong capitalization in names like ",[18,71702,2065],{},[18,71704,3096],{},[18,71706,229],{},[198,71708,71709],{},"Writing text without quotes when a string is intended",[14,71711,71712],{},"Examples of common problems:",[712,71714,71716],{"id":71715},"missing-colon","Missing colon",[36,71718,71720],{"className":38,"code":71719,"language":40,"meta":41,"style":41},"age = 12\n\nif age > 10\n    print(\"OK\")\n",[18,71721,71722,71730,71734,71744],{"__ignoreMap":41},[45,71723,71724,71726,71728],{"class":47,"line":48},[45,71725,52],{"class":51},[45,71727,56],{"class":55},[45,71729,1635],{"class":59},[45,71731,71732],{"class":47,"line":63},[45,71733,67],{"emptyLinePlaceholder":66},[45,71735,71736,71738,71740,71742],{"class":47,"line":70},[45,71737,20],{"class":73},[45,71739,76],{"class":51},[45,71741,272],{"class":55},[45,71743,1174],{"class":59},[45,71745,71746,71748,71750,71752,71755,71757],{"class":47,"line":89},[45,71747,93],{"class":92},[45,71749,96],{"class":85},[45,71751,100],{"class":99},[45,71753,71754],{"class":103},"OK",[45,71756,100],{"class":99},[45,71758,109],{"class":85},[14,71760,71761],{},"Problem:",[195,71763,71764],{},[198,71765,71766,71769,71770],{},[18,71767,71768],{},"if age > 10"," should end with ",[18,71771,457],{},[14,71773,71774],{},"Fixed version:",[36,71776,71778],{"className":38,"code":71777,"language":40,"meta":41,"style":41},"age = 12\n\nif age > 10:\n    print(\"OK\")\n",[18,71779,71780,71788,71792,71804],{"__ignoreMap":41},[45,71781,71782,71784,71786],{"class":47,"line":48},[45,71783,52],{"class":51},[45,71785,56],{"class":55},[45,71787,1635],{"class":59},[45,71789,71790],{"class":47,"line":63},[45,71791,67],{"emptyLinePlaceholder":66},[45,71793,71794,71796,71798,71800,71802],{"class":47,"line":70},[45,71795,20],{"class":73},[45,71797,76],{"class":51},[45,71799,272],{"class":55},[45,71801,1190],{"class":59},[45,71803,86],{"class":85},[45,71805,71806,71808,71810,71812,71814,71816],{"class":47,"line":89},[45,71807,93],{"class":92},[45,71809,96],{"class":85},[45,71811,100],{"class":99},[45,71813,71754],{"class":103},[45,71815,100],{"class":99},[45,71817,109],{"class":85},[712,71819,71821],{"id":71820},"missing-indentation","Missing indentation",[36,71823,71825],{"className":38,"code":71824,"language":40,"meta":41,"style":41},"age = 12\n\nif age > 10:\nprint(\"OK\")\n",[18,71826,71827,71835,71839,71851],{"__ignoreMap":41},[45,71828,71829,71831,71833],{"class":47,"line":48},[45,71830,52],{"class":51},[45,71832,56],{"class":55},[45,71834,1635],{"class":59},[45,71836,71837],{"class":47,"line":63},[45,71838,67],{"emptyLinePlaceholder":66},[45,71840,71841,71843,71845,71847,71849],{"class":47,"line":70},[45,71842,20],{"class":73},[45,71844,76],{"class":51},[45,71846,272],{"class":55},[45,71848,1190],{"class":59},[45,71850,86],{"class":85},[45,71852,71853,71855,71857,71859,71861,71863],{"class":47,"line":89},[45,71854,2065],{"class":92},[45,71856,96],{"class":85},[45,71858,100],{"class":99},[45,71860,71754],{"class":103},[45,71862,100],{"class":99},[45,71864,109],{"class":85},[14,71866,71761],{},[195,71868,71869],{},[198,71870,3267,71871,71873],{},[18,71872,2065],{}," line must be indented",[14,71875,71774],{},[36,71877,71878],{"className":38,"code":71777,"language":40,"meta":41,"style":41},[18,71879,71880,71888,71892,71904],{"__ignoreMap":41},[45,71881,71882,71884,71886],{"class":47,"line":48},[45,71883,52],{"class":51},[45,71885,56],{"class":55},[45,71887,1635],{"class":59},[45,71889,71890],{"class":47,"line":63},[45,71891,67],{"emptyLinePlaceholder":66},[45,71893,71894,71896,71898,71900,71902],{"class":47,"line":70},[45,71895,20],{"class":73},[45,71897,76],{"class":51},[45,71899,272],{"class":55},[45,71901,1190],{"class":59},[45,71903,86],{"class":85},[45,71905,71906,71908,71910,71912,71914,71916],{"class":47,"line":89},[45,71907,93],{"class":92},[45,71909,96],{"class":85},[45,71911,100],{"class":99},[45,71913,71754],{"class":103},[45,71915,100],{"class":99},[45,71917,109],{"class":85},[712,71919,71921],{"id":71920},"unclosed-quote","Unclosed quote",[36,71923,71925],{"className":38,"code":71924,"language":40,"meta":41,"style":41},"print(\"Hello)\n",[18,71926,71927],{"__ignoreMap":41},[45,71928,71929,71931,71933,71935],{"class":47,"line":48},[45,71930,2065],{"class":92},[45,71932,96],{"class":85},[45,71934,100],{"class":99},[45,71936,71937],{"class":103},"Hello)\n",[14,71939,71761],{},[195,71941,71942],{},[198,71943,71944,71945,71947,71948],{},"the string starts with ",[18,71946,100],{}," but does not close with ",[18,71949,100],{},[14,71951,71774],{},[36,71953,71954],{"className":38,"code":8093,"language":40,"meta":41,"style":41},[18,71955,71956],{"__ignoreMap":41},[45,71957,71958,71960,71962,71964,71966,71968],{"class":47,"line":48},[45,71959,2065],{"class":92},[45,71961,96],{"class":85},[45,71963,100],{"class":99},[45,71965,3226],{"class":103},[45,71967,100],{"class":99},[45,71969,109],{"class":85},[712,71971,71973],{"id":71972},"wrong-capitalization","Wrong capitalization",[36,71975,71977],{"className":38,"code":71976,"language":40,"meta":41,"style":41},"Print(\"Hello\")\n",[18,71978,71979],{"__ignoreMap":41},[45,71980,71981,71983,71985,71987,71989,71991],{"class":47,"line":48},[45,71982,71317],{"class":2070},[45,71984,96],{"class":85},[45,71986,100],{"class":99},[45,71988,3226],{"class":103},[45,71990,100],{"class":99},[45,71992,109],{"class":85},[14,71994,71761],{},[195,71996,71997],{},[198,71998,71999,71303,72001],{},[18,72000,71317],{},[18,72002,2065],{},[14,72004,71774],{},[36,72006,72007],{"className":38,"code":8093,"language":40,"meta":41,"style":41},[18,72008,72009],{"__ignoreMap":41},[45,72010,72011,72013,72015,72017,72019,72021],{"class":47,"line":48},[45,72012,2065],{"class":92},[45,72014,96],{"class":85},[45,72016,100],{"class":99},[45,72018,3226],{"class":103},[45,72020,100],{"class":99},[45,72022,109],{"class":85},[14,72024,72025],{},"If you are not sure what is wrong, these simple commands can help while learning:",[36,72027,72028],{"className":15652,"code":58404,"language":15654,"meta":41,"style":41},[18,72029,72030],{"__ignoreMap":41},[45,72031,72032,72034],{"class":47,"line":48},[45,72033,40],{"class":2444},[45,72035,58413],{"class":103},[14,72037,72038],{},"Runs your Python file so you can see the error message.",[36,72040,72042],{"className":38,"code":72041,"language":40,"meta":41,"style":41},"print(\"debug\")\n",[18,72043,72044],{"__ignoreMap":41},[45,72045,72046,72048,72050,72052,72055,72057],{"class":47,"line":48},[45,72047,2065],{"class":92},[45,72049,96],{"class":85},[45,72051,100],{"class":99},[45,72053,72054],{"class":103},"debug",[45,72056,100],{"class":99},[45,72058,109],{"class":85},[14,72060,72061],{},"A quick way to check whether a line or block runs.",[36,72063,72065],{"className":38,"code":72064,"language":40,"meta":41,"style":41},"help(print)\n",[18,72066,72067],{"__ignoreMap":41},[45,72068,72069,72071,72073,72075],{"class":47,"line":48},[45,72070,2104],{"class":92},[45,72072,96],{"class":85},[45,72074,2065],{"class":92},[45,72076,109],{"class":85},[14,72078,72079,72080,72082],{},"Shows help for the ",[18,72081,2065],{}," function.",[36,72084,72086],{"className":38,"code":72085,"language":40,"meta":41,"style":41},"type(\"hello\")\n",[18,72087,72088],{"__ignoreMap":41},[45,72089,72090,72092,72094,72096,72098,72100],{"class":47,"line":48},[45,72091,4340],{"class":2109},[45,72093,96],{"class":85},[45,72095,100],{"class":99},[45,72097,18782],{"class":103},[45,72099,100],{"class":99},[45,72101,109],{"class":85},[14,72103,72104,72105,240],{},"Shows the type of a value. In this case, it returns ",[18,72106,29733],{},[31,72108,2281],{"id":2280},[712,72110,72112],{"id":72111},"what-is-python-syntax-in-simple-words","What is Python syntax in simple words?",[14,72114,72115],{},"Python syntax is the set of writing rules Python uses to understand your code.",[712,72117,72119],{"id":72118},"why-does-indentation-matter-in-python","Why does indentation matter in Python?",[14,72121,72122,72123,58466],{},"Indentation shows which lines belong inside a block, such as an ",[18,72124,20],{},[712,72126,72128],{"id":72127},"do-i-always-need-a-colon-in-python","Do I always need a colon in Python?",[14,72130,72131,72132,21,72134,21,72136,25,72138,240],{},"No. You usually need a colon after statements that start a block, such as ",[18,72133,20],{},[18,72135,7895],{},[18,72137,23529],{},[18,72139,2757],{},[712,72141,72143],{"id":72142},"is-python-case-sensitive","Is Python case-sensitive?",[14,72145,72146],{},"Yes. Lowercase and uppercase letters are treated as different.",[712,72148,72150],{"id":72149},"are-comments-part-of-python-syntax","Are comments part of Python syntax?",[14,72152,72153],{},"Yes, but Python ignores comments when running the code.",[31,72155,2342],{"id":2341},[195,72157,72158,72162,72166,72171,72175,72179,72183],{},[198,72159,72160],{},[236,72161,554],{"href":553},[198,72163,72164],{},[236,72165,71206],{"href":40407},[198,72167,72168],{},[236,72169,72170],{"href":70767},"Understanding Python statements and expressions",[198,72172,72173],{},[236,72174,71324],{"href":40159},[198,72176,72177],{},[236,72178,18361],{"href":18360},[198,72180,72181],{},[236,72182,1614],{"href":1613},[198,72184,72185],{},[236,72186,1541],{"href":1540},[14,72188,72189,72190,1112],{},"Once these basic writing rules make sense, the next good topics are indentation, comments, and statements. After that, move on to variables and simple ",[18,72191,20],{},[2367,72193,72194],{},"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 .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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":72196},[72197,72198,72199,72200,72201,72202,72203,72204,72205,72206,72207,72213,72220],{"id":33,"depth":63,"text":34},{"id":70678,"depth":63,"text":70679},{"id":70703,"depth":63,"text":70704},{"id":70771,"depth":63,"text":70772},{"id":70847,"depth":63,"text":70848},{"id":70982,"depth":63,"text":70983},{"id":71118,"depth":63,"text":71119},{"id":71209,"depth":63,"text":71210},{"id":71327,"depth":63,"text":71328},{"id":71492,"depth":63,"text":71493},{"id":71672,"depth":63,"text":71673,"children":72208},[72209,72210,72211,72212],{"id":71715,"depth":70,"text":71716},{"id":71820,"depth":70,"text":71821},{"id":71920,"depth":70,"text":71921},{"id":71972,"depth":70,"text":71973},{"id":2280,"depth":63,"text":2281,"children":72214},[72215,72216,72217,72218,72219],{"id":72111,"depth":70,"text":72112},{"id":72118,"depth":70,"text":72119},{"id":72127,"depth":70,"text":72128},{"id":72142,"depth":70,"text":72143},{"id":72149,"depth":70,"text":72150},{"id":2341,"depth":63,"text":2342},"Master python syntax basics explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-syntax-basics-explained",{"title":70567,"description":72221},"learn\u002Fpython-syntax-basics-explained","rp-07k1USQd1xOcKADYoVkEydN9QZjYzP5Zw9vkU2s8",{"id":72228,"title":72229,"body":72230,"description":74663,"extension":2399,"meta":74664,"navigation":66,"path":74665,"seo":74666,"stem":74667,"__hash__":74668},"content\u002Flearn\u002Fpython-tuples-explained.md","Python Tuples Explained",{"type":7,"value":72231,"toc":74621},[72232,72235,72238,72241,72245,72251,72253,72267,72272,72275,72332,72336,72375,72377,72395,72398,72405,72409,72412,72416,72460,72464,72487,72491,72530,72534,72555,72559,72562,72608,72612,72639,72645,72692,72696,72718,72725,72729,72732,72736,72760,72762,72855,72859,72896,72899,72906,72910,72913,72918,73001,73005,73021,73025,73028,73101,73105,73118,73122,73127,73182,73186,73204,73216,73220,73223,73225,73245,73251,73334,73338,73392,73394,73419,73425,73429,73433,73436,73481,73485,73516,73519,73523,73526,73585,73589,73602,73605,73607,73644,73648,73674,73677,73706,73710,73743,73746,73760,73764,73767,73771,73776,73819,73823,73831,73835,73840,73897,73901,73913,73918,73923,73978,73982,73990,73995,74000,74063,74067,74075,74078,74098,74102,74108,74110,74130,74132,74205,74209,74224,74227,74239,74244,74246,74249,74253,74256,74287,74290,74321,74325,74328,74367,74371,74374,74399,74402,74427,74431,74434,74467,74471,74474,74476,74533,74535,74549,74551,74555,74558,74562,74565,74569,74572,74576,74579,74583,74586,74588,74618],[10,72233,72229],{"id":72234},"python-tuples-explained",[14,72236,72237],{},"A tuple is one of Python’s built-in collection types. It lets you store multiple values in a single object.",[14,72239,72240],{},"Tuples are useful when values belong together and should not change after they are created. In this guide, you’ll learn what tuples are, how to create them, how to access their items, and when to use them instead of lists.",[31,72242,72244],{"id":72243},"what-a-tuple-is","What a tuple is",[14,72246,72247,72248,240],{},"A tuple is an ",[2421,72249,72250],{},"ordered collection of values",[14,72252,55642],{},[195,72254,72255,72258,72261,72264],{},[198,72256,72257],{},"A tuple can hold more than one item",[198,72259,72260],{},"The items stay in a specific order",[198,72262,72263],{},"You can store different data types in the same tuple",[198,72265,72266],{},"Each item keeps its position",[14,72268,72269,72270,240],{},"One important rule is that tuples are ",[2421,72271,29556],{},[14,72273,72274],{},"Immutable means you cannot change the items after the tuple is created. You cannot replace, add, or remove items directly.",[36,72276,72278],{"className":38,"code":72277,"language":40,"meta":41,"style":41},"person = (\"Alice\", 25, True)\n\nprint(person)\nprint(type(person))\n",[18,72279,72280,72304,72308,72318],{"__ignoreMap":41},[45,72281,72282,72284,72286,72288,72290,72292,72294,72296,72298,72300,72302],{"class":47,"line":48},[45,72283,2966],{"class":51},[45,72285,56],{"class":55},[45,72287,14039],{"class":85},[45,72289,100],{"class":99},[45,72291,60413],{"class":103},[45,72293,100],{"class":99},[45,72295,3079],{"class":85},[45,72297,303],{"class":59},[45,72299,3079],{"class":85},[45,72301,23323],{"class":481},[45,72303,109],{"class":85},[45,72305,72306],{"class":47,"line":63},[45,72307,67],{"emptyLinePlaceholder":66},[45,72309,72310,72312,72314,72316],{"class":47,"line":70},[45,72311,2065],{"class":92},[45,72313,96],{"class":85},[45,72315,2977],{"class":2070},[45,72317,109],{"class":85},[45,72319,72320,72322,72324,72326,72328,72330],{"class":47,"line":89},[45,72321,2065],{"class":92},[45,72323,96],{"class":85},[45,72325,4340],{"class":2109},[45,72327,96],{"class":85},[45,72329,2977],{"class":2070},[45,72331,4348],{"class":85},[14,72333,72334],{},[2421,72335,177],{},[36,72337,72339],{"className":38,"code":72338,"language":40,"meta":41,"style":41},"('Alice', 25, True)\n\u003Cclass 'tuple'>\n",[18,72340,72341,72361],{"__ignoreMap":41},[45,72342,72343,72345,72347,72349,72351,72353,72355,72357,72359],{"class":47,"line":48},[45,72344,96],{"class":85},[45,72346,7901],{"class":99},[45,72348,60413],{"class":103},[45,72350,7901],{"class":99},[45,72352,3079],{"class":85},[45,72354,303],{"class":59},[45,72356,3079],{"class":85},[45,72358,23323],{"class":481},[45,72360,109],{"class":85},[45,72362,72363,72365,72367,72369,72371,72373],{"class":47,"line":63},[45,72364,79],{"class":55},[45,72366,2441],{"class":2440},[45,72368,9199],{"class":99},[45,72370,29752],{"class":103},[45,72372,7901],{"class":99},[45,72374,13484],{"class":55},[14,72376,6570],{},[195,72378,72379,72385,72390],{},[198,72380,72381,72384],{},[18,72382,72383],{},"\"Alice\""," is a string",[198,72386,72387,72389],{},[18,72388,14942],{}," is an integer",[198,72391,72392,72394],{},[18,72393,229],{}," is a boolean",[14,72396,72397],{},"All three values are stored in one tuple.",[14,72399,72400,72401,240],{},"If you are new to this idea, see also ",[236,72402,72404],{"href":72403},"\u002Fglossary\u002Fwhat-is-a-tuple-in-python\u002F","what a tuple means in Python",[31,72406,72408],{"id":72407},"how-to-create-a-tuple","How to create a tuple",[14,72410,72411],{},"You create a tuple using comma-separated values, usually inside parentheses.",[712,72413,72415],{"id":72414},"empty-tuple","Empty tuple",[36,72417,72419],{"className":38,"code":72418,"language":40,"meta":41,"style":41},"empty_tuple = ()\n\nprint(empty_tuple)\nprint(type(empty_tuple))\n",[18,72420,72421,72431,72435,72446],{"__ignoreMap":41},[45,72422,72423,72426,72428],{"class":47,"line":48},[45,72424,72425],{"class":51},"empty_tuple ",[45,72427,56],{"class":55},[45,72429,72430],{"class":85}," ()\n",[45,72432,72433],{"class":47,"line":63},[45,72434,67],{"emptyLinePlaceholder":66},[45,72436,72437,72439,72441,72444],{"class":47,"line":70},[45,72438,2065],{"class":92},[45,72440,96],{"class":85},[45,72442,72443],{"class":2070},"empty_tuple",[45,72445,109],{"class":85},[45,72447,72448,72450,72452,72454,72456,72458],{"class":47,"line":89},[45,72449,2065],{"class":92},[45,72451,96],{"class":85},[45,72453,4340],{"class":2109},[45,72455,96],{"class":85},[45,72457,72443],{"class":2070},[45,72459,4348],{"class":85},[14,72461,72462],{},[2421,72463,177],{},[36,72465,72467],{"className":38,"code":72466,"language":40,"meta":41,"style":41},"()\n\u003Cclass 'tuple'>\n",[18,72468,72469,72473],{"__ignoreMap":41},[45,72470,72471],{"class":47,"line":48},[45,72472,2495],{"class":85},[45,72474,72475,72477,72479,72481,72483,72485],{"class":47,"line":63},[45,72476,79],{"class":55},[45,72478,2441],{"class":2440},[45,72480,9199],{"class":99},[45,72482,29752],{"class":103},[45,72484,7901],{"class":99},[45,72486,13484],{"class":55},[712,72488,72490],{"id":72489},"tuple-with-multiple-items","Tuple with multiple items",[36,72492,72494],{"className":38,"code":72493,"language":40,"meta":41,"style":41},"numbers = (1, 2, 3)\n\nprint(numbers)\n",[18,72495,72496,72516,72520],{"__ignoreMap":41},[45,72497,72498,72500,72502,72504,72506,72508,72510,72512,72514],{"class":47,"line":48},[45,72499,8570],{"class":51},[45,72501,56],{"class":55},[45,72503,14039],{"class":85},[45,72505,8578],{"class":59},[45,72507,3079],{"class":85},[45,72509,3754],{"class":59},[45,72511,3079],{"class":85},[45,72513,777],{"class":59},[45,72515,109],{"class":85},[45,72517,72518],{"class":47,"line":63},[45,72519,67],{"emptyLinePlaceholder":66},[45,72521,72522,72524,72526,72528],{"class":47,"line":70},[45,72523,2065],{"class":92},[45,72525,96],{"class":85},[45,72527,9069],{"class":2070},[45,72529,109],{"class":85},[14,72531,72532],{},[2421,72533,177],{},[36,72535,72537],{"className":38,"code":72536,"language":40,"meta":41,"style":41},"(1, 2, 3)\n",[18,72538,72539],{"__ignoreMap":41},[45,72540,72541,72543,72545,72547,72549,72551,72553],{"class":47,"line":48},[45,72542,96],{"class":85},[45,72544,8578],{"class":59},[45,72546,3079],{"class":85},[45,72548,3754],{"class":59},[45,72550,3079],{"class":85},[45,72552,777],{"class":59},[45,72554,109],{"class":85},[712,72556,72558],{"id":72557},"single-item-tuple","Single-item tuple",[14,72560,72561],{},"A single-item tuple must include a trailing comma.",[36,72563,72565],{"className":38,"code":72564,"language":40,"meta":41,"style":41},"value = (5,)\n\nprint(value)\nprint(type(value))\n",[18,72566,72567,72580,72584,72594],{"__ignoreMap":41},[45,72568,72569,72571,72573,72575,72577],{"class":47,"line":48},[45,72570,1476],{"class":51},[45,72572,56],{"class":55},[45,72574,14039],{"class":85},[45,72576,3749],{"class":59},[45,72578,72579],{"class":85},",)\n",[45,72581,72582],{"class":47,"line":63},[45,72583,67],{"emptyLinePlaceholder":66},[45,72585,72586,72588,72590,72592],{"class":47,"line":70},[45,72587,2065],{"class":92},[45,72589,96],{"class":85},[45,72591,12722],{"class":2070},[45,72593,109],{"class":85},[45,72595,72596,72598,72600,72602,72604,72606],{"class":47,"line":89},[45,72597,2065],{"class":92},[45,72599,96],{"class":85},[45,72601,4340],{"class":2109},[45,72603,96],{"class":85},[45,72605,12722],{"class":2070},[45,72607,4348],{"class":85},[14,72609,72610],{},[2421,72611,177],{},[36,72613,72615],{"className":38,"code":72614,"language":40,"meta":41,"style":41},"(5,)\n\u003Cclass 'tuple'>\n",[18,72616,72617,72625],{"__ignoreMap":41},[45,72618,72619,72621,72623],{"class":47,"line":48},[45,72620,96],{"class":85},[45,72622,3749],{"class":59},[45,72624,72579],{"class":85},[45,72626,72627,72629,72631,72633,72635,72637],{"class":47,"line":63},[45,72628,79],{"class":55},[45,72630,2441],{"class":2440},[45,72632,9199],{"class":99},[45,72634,29752],{"class":103},[45,72636,7901],{"class":99},[45,72638,13484],{"class":55},[14,72640,72641,72642,72644],{},"Without the comma, Python does ",[2421,72643,2857],{}," create a tuple.",[36,72646,72648],{"className":38,"code":72647,"language":40,"meta":41,"style":41},"not_a_tuple = (5)\n\nprint(not_a_tuple)\nprint(type(not_a_tuple))\n",[18,72649,72650,72663,72667,72678],{"__ignoreMap":41},[45,72651,72652,72655,72657,72659,72661],{"class":47,"line":48},[45,72653,72654],{"class":51},"not_a_tuple ",[45,72656,56],{"class":55},[45,72658,14039],{"class":85},[45,72660,3749],{"class":59},[45,72662,109],{"class":85},[45,72664,72665],{"class":47,"line":63},[45,72666,67],{"emptyLinePlaceholder":66},[45,72668,72669,72671,72673,72676],{"class":47,"line":70},[45,72670,2065],{"class":92},[45,72672,96],{"class":85},[45,72674,72675],{"class":2070},"not_a_tuple",[45,72677,109],{"class":85},[45,72679,72680,72682,72684,72686,72688,72690],{"class":47,"line":89},[45,72681,2065],{"class":92},[45,72683,96],{"class":85},[45,72685,4340],{"class":2109},[45,72687,96],{"class":85},[45,72689,72675],{"class":2070},[45,72691,4348],{"class":85},[14,72693,72694],{},[2421,72695,177],{},[36,72697,72698],{"className":38,"code":63424,"language":40,"meta":41,"style":41},[18,72699,72700,72704],{"__ignoreMap":41},[45,72701,72702],{"class":47,"line":48},[45,72703,11570],{"class":59},[45,72705,72706,72708,72710,72712,72714,72716],{"class":47,"line":63},[45,72707,79],{"class":55},[45,72709,2441],{"class":2440},[45,72711,9199],{"class":99},[45,72713,29738],{"class":103},[45,72715,7901],{"class":99},[45,72717,13484],{"class":55},[14,72719,72720,72721,240],{},"If you want more examples, see ",[236,72722,72724],{"href":72723},"\u002Freference\u002Fpython-tuple-creating-a-tuple\u002F","creating a tuple in Python",[31,72726,72728],{"id":72727},"how-tuples-differ-from-lists","How tuples differ from lists",[14,72730,72731],{},"Tuples and lists are similar in some ways, but they are not the same.",[712,72733,72735],{"id":72734},"main-differences","Main differences",[195,72737,72738,72743,72748,72751,72754,72757],{},[198,72739,72740,72741],{},"Lists use square brackets: ",[18,72742,21680],{},[198,72744,72745,72746],{},"Tuples usually use parentheses: ",[18,72747,3772],{},[198,72749,72750],{},"Lists are mutable",[198,72752,72753],{},"Tuples are immutable",[198,72755,72756],{},"Both are ordered",[198,72758,72759],{},"Both support indexing and slicing",[14,72761,466],{},[36,72763,72765],{"className":38,"code":72764,"language":40,"meta":41,"style":41},"my_list = [1, 2, 3]\nmy_tuple = (1, 2, 3)\n\nmy_list[0] = 99\nprint(my_list)\n\n# my_tuple[0] = 99  # This would cause an error\nprint(my_tuple)\n",[18,72766,72767,72787,72807,72811,72825,72835,72839,72844],{"__ignoreMap":41},[45,72768,72769,72771,72773,72775,72777,72779,72781,72783,72785],{"class":47,"line":48},[45,72770,61899],{"class":51},[45,72772,56],{"class":55},[45,72774,8575],{"class":85},[45,72776,8578],{"class":59},[45,72778,3079],{"class":85},[45,72780,3754],{"class":59},[45,72782,3079],{"class":85},[45,72784,777],{"class":59},[45,72786,8594],{"class":85},[45,72788,72789,72791,72793,72795,72797,72799,72801,72803,72805],{"class":47,"line":63},[45,72790,61924],{"class":51},[45,72792,56],{"class":55},[45,72794,14039],{"class":85},[45,72796,8578],{"class":59},[45,72798,3079],{"class":85},[45,72800,3754],{"class":59},[45,72802,3079],{"class":85},[45,72804,777],{"class":59},[45,72806,109],{"class":85},[45,72808,72809],{"class":47,"line":70},[45,72810,67],{"emptyLinePlaceholder":66},[45,72812,72813,72815,72817,72819,72821,72823],{"class":47,"line":89},[45,72814,30561],{"class":51},[45,72816,9680],{"class":85},[45,72818,10482],{"class":59},[45,72820,9685],{"class":85},[45,72822,3099],{"class":55},[45,72824,50943],{"class":59},[45,72826,72827,72829,72831,72833],{"class":47,"line":112},[45,72828,2065],{"class":92},[45,72830,96],{"class":85},[45,72832,30561],{"class":2070},[45,72834,109],{"class":85},[45,72836,72837],{"class":47,"line":126},[45,72838,67],{"emptyLinePlaceholder":66},[45,72840,72841],{"class":47,"line":142},[45,72842,72843],{"class":7624},"# my_tuple[0] = 99  # This would cause an error\n",[45,72845,72846,72848,72850,72853],{"class":47,"line":149},[45,72847,2065],{"class":92},[45,72849,96],{"class":85},[45,72851,72852],{"class":2070},"my_tuple",[45,72854,109],{"class":85},[14,72856,72857],{},[2421,72858,177],{},[36,72860,72862],{"className":38,"code":72861,"language":40,"meta":41,"style":41},"[99, 2, 3]\n(1, 2, 3)\n",[18,72863,72864,72880],{"__ignoreMap":41},[45,72865,72866,72868,72870,72872,72874,72876,72878],{"class":47,"line":48},[45,72867,9680],{"class":85},[45,72869,31136],{"class":59},[45,72871,3079],{"class":85},[45,72873,3754],{"class":59},[45,72875,3079],{"class":85},[45,72877,777],{"class":59},[45,72879,8594],{"class":85},[45,72881,72882,72884,72886,72888,72890,72892,72894],{"class":47,"line":63},[45,72883,96],{"class":85},[45,72885,8578],{"class":59},[45,72887,3079],{"class":85},[45,72889,3754],{"class":59},[45,72891,3079],{"class":85},[45,72893,777],{"class":59},[45,72895,109],{"class":85},[14,72897,72898],{},"Use a tuple when the data should stay fixed. Use a list when you need to change the data.",[14,72900,72901,72902,230,72904,240],{},"For a full comparison, read ",[236,72903,27366],{"href":27365},[236,72905,62013],{"href":62012},[31,72907,72909],{"id":72908},"accessing-tuple-items","Accessing tuple items",[14,72911,72912],{},"You use an index to get one item from a tuple.",[14,72914,72915,72916,240],{},"Index positions start at ",[18,72917,10482],{},[36,72919,72921],{"className":38,"code":72920,"language":40,"meta":41,"style":41},"colors = (\"red\", \"green\", \"blue\")\n\nprint(colors[0])\nprint(colors[1])\nprint(colors[2])\n",[18,72922,72923,72955,72959,72973,72987],{"__ignoreMap":41},[45,72924,72925,72927,72929,72931,72933,72935,72937,72939,72941,72943,72945,72947,72949,72951,72953],{"class":47,"line":48},[45,72926,22320],{"class":51},[45,72928,56],{"class":55},[45,72930,14039],{"class":85},[45,72932,100],{"class":99},[45,72934,22329],{"class":103},[45,72936,100],{"class":99},[45,72938,3079],{"class":85},[45,72940,1565],{"class":99},[45,72942,22338],{"class":103},[45,72944,100],{"class":99},[45,72946,3079],{"class":85},[45,72948,1565],{"class":99},[45,72950,22347],{"class":103},[45,72952,100],{"class":99},[45,72954,109],{"class":85},[45,72956,72957],{"class":47,"line":63},[45,72958,67],{"emptyLinePlaceholder":66},[45,72960,72961,72963,72965,72967,72969,72971],{"class":47,"line":70},[45,72962,2065],{"class":92},[45,72964,96],{"class":85},[45,72966,22407],{"class":2070},[45,72968,9680],{"class":85},[45,72970,10482],{"class":59},[45,72972,21941],{"class":85},[45,72974,72975,72977,72979,72981,72983,72985],{"class":47,"line":89},[45,72976,2065],{"class":92},[45,72978,96],{"class":85},[45,72980,22407],{"class":2070},[45,72982,9680],{"class":85},[45,72984,8578],{"class":59},[45,72986,21941],{"class":85},[45,72988,72989,72991,72993,72995,72997,72999],{"class":47,"line":112},[45,72990,2065],{"class":92},[45,72992,96],{"class":85},[45,72994,22407],{"class":2070},[45,72996,9680],{"class":85},[45,72998,9367],{"class":59},[45,73000,21941],{"class":85},[14,73002,73003],{},[2421,73004,177],{},[36,73006,73007],{"className":38,"code":22385,"language":40,"meta":41,"style":41},[18,73008,73009,73013,73017],{"__ignoreMap":41},[45,73010,73011],{"class":47,"line":48},[45,73012,22392],{"class":51},[45,73014,73015],{"class":47,"line":63},[45,73016,22397],{"class":51},[45,73018,73019],{"class":47,"line":70},[45,73020,22402],{"class":51},[712,73022,73024],{"id":73023},"negative-indexes","Negative indexes",[14,73026,73027],{},"Negative indexes count from the end.",[36,73029,73031],{"className":38,"code":73030,"language":40,"meta":41,"style":41},"colors = (\"red\", \"green\", \"blue\")\n\nprint(colors[-1])\nprint(colors[-2])\n",[18,73032,73033,73065,73069,73085],{"__ignoreMap":41},[45,73034,73035,73037,73039,73041,73043,73045,73047,73049,73051,73053,73055,73057,73059,73061,73063],{"class":47,"line":48},[45,73036,22320],{"class":51},[45,73038,56],{"class":55},[45,73040,14039],{"class":85},[45,73042,100],{"class":99},[45,73044,22329],{"class":103},[45,73046,100],{"class":99},[45,73048,3079],{"class":85},[45,73050,1565],{"class":99},[45,73052,22338],{"class":103},[45,73054,100],{"class":99},[45,73056,3079],{"class":85},[45,73058,1565],{"class":99},[45,73060,22347],{"class":103},[45,73062,100],{"class":99},[45,73064,109],{"class":85},[45,73066,73067],{"class":47,"line":63},[45,73068,67],{"emptyLinePlaceholder":66},[45,73070,73071,73073,73075,73077,73079,73081,73083],{"class":47,"line":70},[45,73072,2065],{"class":92},[45,73074,96],{"class":85},[45,73076,22407],{"class":2070},[45,73078,9680],{"class":85},[45,73080,218],{"class":55},[45,73082,8578],{"class":59},[45,73084,21941],{"class":85},[45,73086,73087,73089,73091,73093,73095,73097,73099],{"class":47,"line":89},[45,73088,2065],{"class":92},[45,73090,96],{"class":85},[45,73092,22407],{"class":2070},[45,73094,9680],{"class":85},[45,73096,218],{"class":55},[45,73098,9367],{"class":59},[45,73100,21941],{"class":85},[14,73102,73103],{},[2421,73104,177],{},[36,73106,73108],{"className":38,"code":73107,"language":40,"meta":41,"style":41},"blue\ngreen\n",[18,73109,73110,73114],{"__ignoreMap":41},[45,73111,73112],{"class":47,"line":48},[45,73113,22402],{"class":51},[45,73115,73116],{"class":47,"line":63},[45,73117,22397],{"class":51},[712,73119,73121],{"id":73120},"invalid-indexes","Invalid indexes",[14,73123,73124,73125,240],{},"If you try to access a position that does not exist, Python raises an ",[18,73126,46068],{},[36,73128,73130],{"className":38,"code":73129,"language":40,"meta":41,"style":41},"colors = (\"red\", \"green\", \"blue\")\n\nprint(colors[5])\n",[18,73131,73132,73164,73168],{"__ignoreMap":41},[45,73133,73134,73136,73138,73140,73142,73144,73146,73148,73150,73152,73154,73156,73158,73160,73162],{"class":47,"line":48},[45,73135,22320],{"class":51},[45,73137,56],{"class":55},[45,73139,14039],{"class":85},[45,73141,100],{"class":99},[45,73143,22329],{"class":103},[45,73145,100],{"class":99},[45,73147,3079],{"class":85},[45,73149,1565],{"class":99},[45,73151,22338],{"class":103},[45,73153,100],{"class":99},[45,73155,3079],{"class":85},[45,73157,1565],{"class":99},[45,73159,22347],{"class":103},[45,73161,100],{"class":99},[45,73163,109],{"class":85},[45,73165,73166],{"class":47,"line":63},[45,73167,67],{"emptyLinePlaceholder":66},[45,73169,73170,73172,73174,73176,73178,73180],{"class":47,"line":70},[45,73171,2065],{"class":92},[45,73173,96],{"class":85},[45,73175,22407],{"class":2070},[45,73177,9680],{"class":85},[45,73179,3749],{"class":59},[45,73181,21941],{"class":85},[14,73183,73184],{},[2421,73185,177],{},[36,73187,73189],{"className":38,"code":73188,"language":40,"meta":41,"style":41},"IndexError: tuple index out of range\n",[18,73190,73191],{"__ignoreMap":41},[45,73192,73193,73195,73197,73200,73202],{"class":47,"line":48},[45,73194,46068],{"class":2109},[45,73196,457],{"class":85},[45,73198,73199],{"class":2109}," tuple",[45,73201,46372],{"class":51},[45,73203,46375],{"class":92},[14,73205,73206,73207,73211,73212,240],{},"If you get that error, see ",[236,73208,73210],{"href":73209},"\u002Ferrors\u002Findexerror-tuple-index-out-of-range-fix\u002F","how to fix tuple index out of range"," or read more about ",[236,73213,73215],{"href":73214},"\u002Freference\u002Fpython-tuple-indexing-explained\u002F","tuple indexing in Python",[31,73217,73219],{"id":73218},"tuple-slicing","Tuple slicing",[14,73221,73222],{},"Slicing lets you get part of a tuple.",[14,73224,27371],{},[36,73226,73228],{"className":38,"code":73227,"language":40,"meta":41,"style":41},"my_tuple[start:end]\n",[18,73229,73230],{"__ignoreMap":41},[45,73231,73232,73234,73236,73238,73240,73243],{"class":47,"line":48},[45,73233,72852],{"class":51},[45,73235,9680],{"class":85},[45,73237,49940],{"class":51},[45,73239,457],{"class":85},[45,73241,73242],{"class":51},"end",[45,73244,8594],{"class":85},[14,73246,73247,73248,240],{},"Slicing returns a ",[2421,73249,73250],{},"new tuple",[36,73252,73254],{"className":38,"code":73253,"language":40,"meta":41,"style":41},"numbers = (10, 20, 30, 40, 50)\n\nprint(numbers[1:4])\nprint(numbers[:3])\nprint(numbers[2:])\n",[18,73255,73256,73284,73288,73306,73320],{"__ignoreMap":41},[45,73257,73258,73260,73262,73264,73266,73268,73270,73272,73274,73276,73278,73280,73282],{"class":47,"line":48},[45,73259,8570],{"class":51},[45,73261,56],{"class":55},[45,73263,14039],{"class":85},[45,73265,11786],{"class":59},[45,73267,3079],{"class":85},[45,73269,10503],{"class":59},[45,73271,3079],{"class":85},[45,73273,10512],{"class":59},[45,73275,3079],{"class":85},[45,73277,10521],{"class":59},[45,73279,3079],{"class":85},[45,73281,20121],{"class":59},[45,73283,109],{"class":85},[45,73285,73286],{"class":47,"line":63},[45,73287,67],{"emptyLinePlaceholder":66},[45,73289,73290,73292,73294,73296,73298,73300,73302,73304],{"class":47,"line":70},[45,73291,2065],{"class":92},[45,73293,96],{"class":85},[45,73295,9069],{"class":2070},[45,73297,9680],{"class":85},[45,73299,8578],{"class":59},[45,73301,457],{"class":85},[45,73303,3584],{"class":59},[45,73305,21941],{"class":85},[45,73307,73308,73310,73312,73314,73316,73318],{"class":47,"line":89},[45,73309,2065],{"class":92},[45,73311,96],{"class":85},[45,73313,9069],{"class":2070},[45,73315,68950],{"class":85},[45,73317,11184],{"class":59},[45,73319,21941],{"class":85},[45,73321,73322,73324,73326,73328,73330,73332],{"class":47,"line":112},[45,73323,2065],{"class":92},[45,73325,96],{"class":85},[45,73327,9069],{"class":2070},[45,73329,9680],{"class":85},[45,73331,9367],{"class":59},[45,73333,68969],{"class":85},[14,73335,73336],{},[2421,73337,177],{},[36,73339,73341],{"className":38,"code":73340,"language":40,"meta":41,"style":41},"(20, 30, 40)\n(10, 20, 30)\n(30, 40, 50)\n",[18,73342,73343,73359,73375],{"__ignoreMap":41},[45,73344,73345,73347,73349,73351,73353,73355,73357],{"class":47,"line":48},[45,73346,96],{"class":85},[45,73348,3372],{"class":59},[45,73350,3079],{"class":85},[45,73352,10512],{"class":59},[45,73354,3079],{"class":85},[45,73356,10521],{"class":59},[45,73358,109],{"class":85},[45,73360,73361,73363,73365,73367,73369,73371,73373],{"class":47,"line":63},[45,73362,96],{"class":85},[45,73364,11786],{"class":59},[45,73366,3079],{"class":85},[45,73368,10503],{"class":59},[45,73370,3079],{"class":85},[45,73372,10512],{"class":59},[45,73374,109],{"class":85},[45,73376,73377,73379,73382,73384,73386,73388,73390],{"class":47,"line":70},[45,73378,96],{"class":85},[45,73380,73381],{"class":59},"30",[45,73383,3079],{"class":85},[45,73385,10521],{"class":59},[45,73387,3079],{"class":85},[45,73389,20121],{"class":59},[45,73391,109],{"class":85},[14,73393,9393],{},[195,73395,73396,73407,73413],{},[198,73397,73398,73401,73402,73404,73405],{},[18,73399,73400],{},"numbers[1:4]"," gets items from index ",[18,73403,8578],{}," up to but not including ",[18,73406,3584],{},[198,73408,73409,73412],{},[18,73410,73411],{},"numbers[:3]"," starts from the beginning",[198,73414,73415,73418],{},[18,73416,73417],{},"numbers[2:]"," goes to the end",[14,73420,23968,73421,57216],{},[236,73422,73424],{"href":73423},"\u002Freference\u002Fpython-tuple-slicing-explained\u002F","tuple slicing explained",[31,73426,73428],{"id":73427},"tuple-packing-and-unpacking","Tuple packing and unpacking",[712,73430,73432],{"id":73431},"packing","Packing",[14,73434,73435],{},"Packing means storing multiple values in one tuple.",[36,73437,73439],{"className":38,"code":73438,"language":40,"meta":41,"style":41},"point = 10, 20\n\nprint(point)\nprint(type(point))\n",[18,73440,73441,73453,73457,73467],{"__ignoreMap":41},[45,73442,73443,73445,73447,73449,73451],{"class":47,"line":48},[45,73444,41291],{"class":51},[45,73446,56],{"class":55},[45,73448,1190],{"class":59},[45,73450,3079],{"class":85},[45,73452,1883],{"class":59},[45,73454,73455],{"class":47,"line":63},[45,73456,67],{"emptyLinePlaceholder":66},[45,73458,73459,73461,73463,73465],{"class":47,"line":70},[45,73460,2065],{"class":92},[45,73462,96],{"class":85},[45,73464,41312],{"class":2070},[45,73466,109],{"class":85},[45,73468,73469,73471,73473,73475,73477,73479],{"class":47,"line":89},[45,73470,2065],{"class":92},[45,73472,96],{"class":85},[45,73474,4340],{"class":2109},[45,73476,96],{"class":85},[45,73478,41312],{"class":2070},[45,73480,4348],{"class":85},[14,73482,73483],{},[2421,73484,177],{},[36,73486,73488],{"className":38,"code":73487,"language":40,"meta":41,"style":41},"(10, 20)\n\u003Cclass 'tuple'>\n",[18,73489,73490,73502],{"__ignoreMap":41},[45,73491,73492,73494,73496,73498,73500],{"class":47,"line":48},[45,73493,96],{"class":85},[45,73495,11786],{"class":59},[45,73497,3079],{"class":85},[45,73499,10503],{"class":59},[45,73501,109],{"class":85},[45,73503,73504,73506,73508,73510,73512,73514],{"class":47,"line":63},[45,73505,79],{"class":55},[45,73507,2441],{"class":2440},[45,73509,9199],{"class":99},[45,73511,29752],{"class":103},[45,73513,7901],{"class":99},[45,73515,13484],{"class":55},[14,73517,73518],{},"Notice that parentheses are not always required. The commas are what matter most.",[712,73520,73522],{"id":73521},"unpacking","Unpacking",[14,73524,73525],{},"Unpacking means assigning tuple items to separate variables.",[36,73527,73529],{"className":38,"code":73528,"language":40,"meta":41,"style":41},"point = (10, 20)\nx, y = point\n\nprint(x)\nprint(y)\n",[18,73530,73531,73547,73561,73565,73575],{"__ignoreMap":41},[45,73532,73533,73535,73537,73539,73541,73543,73545],{"class":47,"line":48},[45,73534,41291],{"class":51},[45,73536,56],{"class":55},[45,73538,14039],{"class":85},[45,73540,11786],{"class":59},[45,73542,3079],{"class":85},[45,73544,10503],{"class":59},[45,73546,109],{"class":85},[45,73548,73549,73551,73553,73556,73558],{"class":47,"line":63},[45,73550,26156],{"class":51},[45,73552,3079],{"class":85},[45,73554,73555],{"class":51}," y ",[45,73557,56],{"class":55},[45,73559,73560],{"class":51}," point\n",[45,73562,73563],{"class":47,"line":70},[45,73564,67],{"emptyLinePlaceholder":66},[45,73566,73567,73569,73571,73573],{"class":47,"line":89},[45,73568,2065],{"class":92},[45,73570,96],{"class":85},[45,73572,26156],{"class":2070},[45,73574,109],{"class":85},[45,73576,73577,73579,73581,73583],{"class":47,"line":112},[45,73578,2065],{"class":92},[45,73580,96],{"class":85},[45,73582,29457],{"class":2070},[45,73584,109],{"class":85},[14,73586,73587],{},[2421,73588,177],{},[36,73590,73592],{"className":38,"code":73591,"language":40,"meta":41,"style":41},"10\n20\n",[18,73593,73594,73598],{"__ignoreMap":41},[45,73595,73596],{"class":47,"line":48},[45,73597,13262],{"class":59},[45,73599,73600],{"class":47,"line":63},[45,73601,3436],{"class":59},[14,73603,73604],{},"The number of variables must match the number of tuple items.",[14,73606,64612],{},[36,73608,73610],{"className":38,"code":73609,"language":40,"meta":41,"style":41},"point = (10, 20, 30)\nx, y = point\n",[18,73611,73612,73632],{"__ignoreMap":41},[45,73613,73614,73616,73618,73620,73622,73624,73626,73628,73630],{"class":47,"line":48},[45,73615,41291],{"class":51},[45,73617,56],{"class":55},[45,73619,14039],{"class":85},[45,73621,11786],{"class":59},[45,73623,3079],{"class":85},[45,73625,10503],{"class":59},[45,73627,3079],{"class":85},[45,73629,10512],{"class":59},[45,73631,109],{"class":85},[45,73633,73634,73636,73638,73640,73642],{"class":47,"line":63},[45,73635,26156],{"class":51},[45,73637,3079],{"class":85},[45,73639,73555],{"class":51},[45,73641,56],{"class":55},[45,73643,73560],{"class":51},[14,73645,73646],{},[2421,73647,177],{},[36,73649,73651],{"className":38,"code":73650,"language":40,"meta":41,"style":41},"ValueError: too many values to unpack (expected 2)\n",[18,73652,73653],{"__ignoreMap":41},[45,73654,73655,73657,73659,73662,73665,73667,73670,73672],{"class":47,"line":48},[45,73656,42097],{"class":2109},[45,73658,457],{"class":85},[45,73660,73661],{"class":51}," too many values to ",[45,73663,73664],{"class":2070},"unpack ",[45,73666,96],{"class":85},[45,73668,73669],{"class":2070},"expected ",[45,73671,9367],{"class":59},[45,73673,109],{"class":85},[14,73675,73676],{},"This also causes an error:",[36,73678,73680],{"className":38,"code":73679,"language":40,"meta":41,"style":41},"point = (10,)\nx, y = point\n",[18,73681,73682,73694],{"__ignoreMap":41},[45,73683,73684,73686,73688,73690,73692],{"class":47,"line":48},[45,73685,41291],{"class":51},[45,73687,56],{"class":55},[45,73689,14039],{"class":85},[45,73691,11786],{"class":59},[45,73693,72579],{"class":85},[45,73695,73696,73698,73700,73702,73704],{"class":47,"line":63},[45,73697,26156],{"class":51},[45,73699,3079],{"class":85},[45,73701,73555],{"class":51},[45,73703,56],{"class":55},[45,73705,73560],{"class":51},[14,73707,73708],{},[2421,73709,177],{},[36,73711,73713],{"className":38,"code":73712,"language":40,"meta":41,"style":41},"ValueError: not enough values to unpack (expected 2, got 1)\n",[18,73714,73715],{"__ignoreMap":41},[45,73716,73717,73719,73721,73723,73726,73728,73730,73732,73734,73736,73739,73741],{"class":47,"line":48},[45,73718,42097],{"class":2109},[45,73720,457],{"class":85},[45,73722,1296],{"class":55},[45,73724,73725],{"class":51}," enough values to ",[45,73727,73664],{"class":2070},[45,73729,96],{"class":85},[45,73731,73669],{"class":2070},[45,73733,9367],{"class":59},[45,73735,3079],{"class":85},[45,73737,73738],{"class":2070}," got ",[45,73740,8578],{"class":59},[45,73742,109],{"class":85},[14,73744,73745],{},"If that happens, see:",[195,73747,73748,73754],{},[198,73749,73750],{},[236,73751,73753],{"href":73752},"\u002Ferrors\u002Fvalueerror-too-many-values-to-unpack-fix\u002F","ValueError: too many values to unpack",[198,73755,73756],{},[236,73757,73759],{"href":73758},"\u002Ferrors\u002Fvalueerror-not-enough-values-to-unpack-fix\u002F","ValueError: not enough values to unpack",[31,73761,73763],{"id":73762},"useful-tuple-operations","Useful tuple operations",[14,73765,73766],{},"Python gives you a few simple tools for working with tuples.",[712,73768,73769],{"id":28716},[18,73770,59071],{},[14,73772,165,73773,73775],{},[18,73774,59071],{}," to get the number of items.",[36,73777,73779],{"className":38,"code":73778,"language":40,"meta":41,"style":41},"numbers = (10, 20, 30)\n\nprint(len(numbers))\n",[18,73780,73781,73801,73805],{"__ignoreMap":41},[45,73782,73783,73785,73787,73789,73791,73793,73795,73797,73799],{"class":47,"line":48},[45,73784,8570],{"class":51},[45,73786,56],{"class":55},[45,73788,14039],{"class":85},[45,73790,11786],{"class":59},[45,73792,3079],{"class":85},[45,73794,10503],{"class":59},[45,73796,3079],{"class":85},[45,73798,10512],{"class":59},[45,73800,109],{"class":85},[45,73802,73803],{"class":47,"line":63},[45,73804,67],{"emptyLinePlaceholder":66},[45,73806,73807,73809,73811,73813,73815,73817],{"class":47,"line":70},[45,73808,2065],{"class":92},[45,73810,96],{"class":85},[45,73812,28716],{"class":92},[45,73814,96],{"class":85},[45,73816,9069],{"class":2070},[45,73818,4348],{"class":85},[14,73820,73821],{},[2421,73822,177],{},[36,73824,73825],{"className":38,"code":13046,"language":40,"meta":41,"style":41},[18,73826,73827],{"__ignoreMap":41},[45,73828,73829],{"class":47,"line":48},[45,73830,13046],{"class":59},[712,73832,73833],{"id":8623},[18,73834,8623],{},[14,73836,165,73837,73839],{},[18,73838,8623],{}," to check whether a value exists.",[36,73841,73843],{"className":38,"code":73842,"language":40,"meta":41,"style":41},"numbers = (10, 20, 30)\n\nprint(20 in numbers)\nprint(99 in numbers)\n",[18,73844,73845,73865,73869,73883],{"__ignoreMap":41},[45,73846,73847,73849,73851,73853,73855,73857,73859,73861,73863],{"class":47,"line":48},[45,73848,8570],{"class":51},[45,73850,56],{"class":55},[45,73852,14039],{"class":85},[45,73854,11786],{"class":59},[45,73856,3079],{"class":85},[45,73858,10503],{"class":59},[45,73860,3079],{"class":85},[45,73862,10512],{"class":59},[45,73864,109],{"class":85},[45,73866,73867],{"class":47,"line":63},[45,73868,67],{"emptyLinePlaceholder":66},[45,73870,73871,73873,73875,73877,73879,73881],{"class":47,"line":70},[45,73872,2065],{"class":92},[45,73874,96],{"class":85},[45,73876,3372],{"class":59},[45,73878,45650],{"class":73},[45,73880,8626],{"class":2070},[45,73882,109],{"class":85},[45,73884,73885,73887,73889,73891,73893,73895],{"class":47,"line":89},[45,73886,2065],{"class":92},[45,73888,96],{"class":85},[45,73890,31136],{"class":59},[45,73892,45650],{"class":73},[45,73894,8626],{"class":2070},[45,73896,109],{"class":85},[14,73898,73899],{},[2421,73900,177],{},[36,73902,73903],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,73904,73905,73909],{"__ignoreMap":41},[45,73906,73907],{"class":47,"line":48},[45,73908,2270],{"class":481},[45,73910,73911],{"class":47,"line":63},[45,73912,2265],{"class":481},[712,73914,73915],{"id":34014},[18,73916,73917],{},"count()",[14,73919,165,73920,73922],{},[18,73921,73917],{}," to count how many times a value appears.",[36,73924,73926],{"className":38,"code":73925,"language":40,"meta":41,"style":41},"values = (1, 2, 2, 3, 2)\n\nprint(values.count(2))\n",[18,73927,73928,73956,73960],{"__ignoreMap":41},[45,73929,73930,73932,73934,73936,73938,73940,73942,73944,73946,73948,73950,73952,73954],{"class":47,"line":48},[45,73931,10571],{"class":51},[45,73933,56],{"class":55},[45,73935,14039],{"class":85},[45,73937,8578],{"class":59},[45,73939,3079],{"class":85},[45,73941,3754],{"class":59},[45,73943,3079],{"class":85},[45,73945,3754],{"class":59},[45,73947,3079],{"class":85},[45,73949,777],{"class":59},[45,73951,3079],{"class":85},[45,73953,3754],{"class":59},[45,73955,109],{"class":85},[45,73957,73958],{"class":47,"line":63},[45,73959,67],{"emptyLinePlaceholder":66},[45,73961,73962,73964,73966,73968,73970,73972,73974,73976],{"class":47,"line":70},[45,73963,2065],{"class":92},[45,73965,96],{"class":85},[45,73967,10790],{"class":2070},[45,73969,240],{"class":85},[45,73971,34014],{"class":2070},[45,73973,96],{"class":85},[45,73975,9367],{"class":59},[45,73977,4348],{"class":85},[14,73979,73980],{},[2421,73981,177],{},[36,73983,73984],{"className":38,"code":13046,"language":40,"meta":41,"style":41},[18,73985,73986],{"__ignoreMap":41},[45,73987,73988],{"class":47,"line":48},[45,73989,13046],{"class":59},[712,73991,73992],{"id":60449},[18,73993,73994],{},"index()",[14,73996,165,73997,73999],{},[18,73998,73994],{}," to find the position of a value.",[36,74001,74003],{"className":38,"code":74002,"language":40,"meta":41,"style":41},"values = (\"a\", \"b\", \"c\")\n\nprint(values.index(\"b\"))\n",[18,74004,74005,74037,74041],{"__ignoreMap":41},[45,74006,74007,74009,74011,74013,74015,74017,74019,74021,74023,74025,74027,74029,74031,74033,74035],{"class":47,"line":48},[45,74008,10571],{"class":51},[45,74010,56],{"class":55},[45,74012,14039],{"class":85},[45,74014,100],{"class":99},[45,74016,236],{"class":103},[45,74018,100],{"class":99},[45,74020,3079],{"class":85},[45,74022,1565],{"class":99},[45,74024,10384],{"class":103},[45,74026,100],{"class":99},[45,74028,3079],{"class":85},[45,74030,1565],{"class":99},[45,74032,10402],{"class":103},[45,74034,100],{"class":99},[45,74036,109],{"class":85},[45,74038,74039],{"class":47,"line":63},[45,74040,67],{"emptyLinePlaceholder":66},[45,74042,74043,74045,74047,74049,74051,74053,74055,74057,74059,74061],{"class":47,"line":70},[45,74044,2065],{"class":92},[45,74046,96],{"class":85},[45,74048,10790],{"class":2070},[45,74050,240],{"class":85},[45,74052,60449],{"class":2070},[45,74054,96],{"class":85},[45,74056,100],{"class":99},[45,74058,10384],{"class":103},[45,74060,100],{"class":99},[45,74062,4348],{"class":85},[14,74064,74065],{},[2421,74066,177],{},[36,74068,74069],{"className":38,"code":13036,"language":40,"meta":41,"style":41},[18,74070,74071],{"__ignoreMap":41},[45,74072,74073],{"class":47,"line":48},[45,74074,13036],{"class":59},[14,74076,74077],{},"Related references:",[195,74079,74080,74086,74092],{},[198,74081,74082],{},[236,74083,74085],{"href":74084},"\u002Freference\u002Fpython-tuple-length-len\u002F","Python tuple len()",[198,74087,74088],{},[236,74089,74091],{"href":74090},"\u002Freference\u002Fpython-tuple-count-method\u002F","Python tuple count() method",[198,74093,74094],{},[236,74095,74097],{"href":74096},"\u002Freference\u002Fpython-tuple-index-method\u002F","Python tuple index() method",[31,74099,74101],{"id":74100},"when-to-use-tuples","When to use tuples",[14,74103,74104,74105,240],{},"Use tuples when you have a ",[2421,74106,74107],{},"fixed group of related values",[14,74109,42917],{},[195,74111,74112,74118,74124,74127],{},[198,74113,74114,74115],{},"Coordinates like ",[18,74116,74117],{},"(10, 20)",[198,74119,74120,74121],{},"RGB colors like ",[18,74122,74123],{},"(255, 0, 0)",[198,74125,74126],{},"Settings that should stay unchanged",[198,74128,74129],{},"Multiple values returned from a function",[14,74131,466],{},[36,74133,74135],{"className":38,"code":74134,"language":40,"meta":41,"style":41},"def get_dimensions():\n    return (1920, 1080)\n\nwidth, height = get_dimensions()\n\nprint(width)\nprint(height)\n",[18,74136,74137,74146,74162,74166,74181,74185,74195],{"__ignoreMap":41},[45,74138,74139,74141,74144],{"class":47,"line":48},[45,74140,2757],{"class":2440},[45,74142,74143],{"class":2455}," get_dimensions",[45,74145,3828],{"class":85},[45,74147,74148,74150,74152,74155,74157,74160],{"class":47,"line":63},[45,74149,4681],{"class":73},[45,74151,14039],{"class":85},[45,74153,74154],{"class":59},"1920",[45,74156,3079],{"class":85},[45,74158,74159],{"class":59}," 1080",[45,74161,109],{"class":85},[45,74163,74164],{"class":47,"line":70},[45,74165,67],{"emptyLinePlaceholder":66},[45,74167,74168,74170,74172,74175,74177,74179],{"class":47,"line":89},[45,74169,3509],{"class":51},[45,74171,3079],{"class":85},[45,74173,74174],{"class":51}," height ",[45,74176,56],{"class":55},[45,74178,74143],{"class":2070},[45,74180,2495],{"class":85},[45,74182,74183],{"class":47,"line":112},[45,74184,67],{"emptyLinePlaceholder":66},[45,74186,74187,74189,74191,74193],{"class":47,"line":126},[45,74188,2065],{"class":92},[45,74190,96],{"class":85},[45,74192,3509],{"class":2070},[45,74194,109],{"class":85},[45,74196,74197,74199,74201,74203],{"class":47,"line":142},[45,74198,2065],{"class":92},[45,74200,96],{"class":85},[45,74202,3523],{"class":2070},[45,74204,109],{"class":85},[14,74206,74207],{},[2421,74208,177],{},[36,74210,74212],{"className":38,"code":74211,"language":40,"meta":41,"style":41},"1920\n1080\n",[18,74213,74214,74219],{"__ignoreMap":41},[45,74215,74216],{"class":47,"line":48},[45,74217,74218],{"class":59},"1920\n",[45,74220,74221],{"class":47,"line":63},[45,74222,74223],{"class":59},"1080\n",[14,74225,74226],{},"A good rule is:",[195,74228,74229,74234],{},[198,74230,19194,74231,74233],{},[2421,74232,29752],{}," when the values should stay unchanged",[198,74235,19194,74236,74238],{},[2421,74237,14463],{}," when you need to add, remove, or edit items",[14,74240,74241,74242,240],{},"If you want to understand this idea better, see ",[236,74243,21278],{"href":21277},[31,74245,5903],{"id":5902},[14,74247,74248],{},"Beginners often run into the same tuple problems.",[712,74250,74252],{"id":74251},"forgetting-the-comma-in-a-single-item-tuple","Forgetting the comma in a single-item tuple",[14,74254,74255],{},"This does not create a tuple:",[36,74257,74259],{"className":38,"code":74258,"language":40,"meta":41,"style":41},"value = (5)\nprint(type(value))\n",[18,74260,74261,74273],{"__ignoreMap":41},[45,74262,74263,74265,74267,74269,74271],{"class":47,"line":48},[45,74264,1476],{"class":51},[45,74266,56],{"class":55},[45,74268,14039],{"class":85},[45,74270,3749],{"class":59},[45,74272,109],{"class":85},[45,74274,74275,74277,74279,74281,74283,74285],{"class":47,"line":63},[45,74276,2065],{"class":92},[45,74278,96],{"class":85},[45,74280,4340],{"class":2109},[45,74282,96],{"class":85},[45,74284,12722],{"class":2070},[45,74286,4348],{"class":85},[14,74288,74289],{},"This does:",[36,74291,74293],{"className":38,"code":74292,"language":40,"meta":41,"style":41},"value = (5,)\nprint(type(value))\n",[18,74294,74295,74307],{"__ignoreMap":41},[45,74296,74297,74299,74301,74303,74305],{"class":47,"line":48},[45,74298,1476],{"class":51},[45,74300,56],{"class":55},[45,74302,14039],{"class":85},[45,74304,3749],{"class":59},[45,74306,72579],{"class":85},[45,74308,74309,74311,74313,74315,74317,74319],{"class":47,"line":63},[45,74310,2065],{"class":92},[45,74312,96],{"class":85},[45,74314,4340],{"class":2109},[45,74316,96],{"class":85},[45,74318,12722],{"class":2070},[45,74320,4348],{"class":85},[712,74322,74324],{"id":74323},"trying-to-change-a-tuple-item","Trying to change a tuple item",[14,74326,74327],{},"This causes an error because tuples are immutable:",[36,74329,74331],{"className":38,"code":74330,"language":40,"meta":41,"style":41},"numbers = (1, 2, 3)\nnumbers[0] = 99\n",[18,74332,74333,74353],{"__ignoreMap":41},[45,74334,74335,74337,74339,74341,74343,74345,74347,74349,74351],{"class":47,"line":48},[45,74336,8570],{"class":51},[45,74338,56],{"class":55},[45,74340,14039],{"class":85},[45,74342,8578],{"class":59},[45,74344,3079],{"class":85},[45,74346,3754],{"class":59},[45,74348,3079],{"class":85},[45,74350,777],{"class":59},[45,74352,109],{"class":85},[45,74354,74355,74357,74359,74361,74363,74365],{"class":47,"line":63},[45,74356,9069],{"class":51},[45,74358,9680],{"class":85},[45,74360,10482],{"class":59},[45,74362,9685],{"class":85},[45,74364,3099],{"class":55},[45,74366,50943],{"class":59},[712,74368,74370],{"id":74369},"mixing-up-tuple-syntax-with-list-syntax","Mixing up tuple syntax with list syntax",[14,74372,74373],{},"Lists use square brackets:",[36,74375,74377],{"className":38,"code":74376,"language":40,"meta":41,"style":41},"my_list = [1, 2, 3]\n",[18,74378,74379],{"__ignoreMap":41},[45,74380,74381,74383,74385,74387,74389,74391,74393,74395,74397],{"class":47,"line":48},[45,74382,61899],{"class":51},[45,74384,56],{"class":55},[45,74386,8575],{"class":85},[45,74388,8578],{"class":59},[45,74390,3079],{"class":85},[45,74392,3754],{"class":59},[45,74394,3079],{"class":85},[45,74396,777],{"class":59},[45,74398,8594],{"class":85},[14,74400,74401],{},"Tuples usually use parentheses:",[36,74403,74405],{"className":38,"code":74404,"language":40,"meta":41,"style":41},"my_tuple = (1, 2, 3)\n",[18,74406,74407],{"__ignoreMap":41},[45,74408,74409,74411,74413,74415,74417,74419,74421,74423,74425],{"class":47,"line":48},[45,74410,61924],{"class":51},[45,74412,56],{"class":55},[45,74414,14039],{"class":85},[45,74416,8578],{"class":59},[45,74418,3079],{"class":85},[45,74420,3754],{"class":59},[45,74422,3079],{"class":85},[45,74424,777],{"class":59},[45,74426,109],{"class":85},[712,74428,74430],{"id":74429},"using-the-wrong-number-of-variables-during-unpacking","Using the wrong number of variables during unpacking",[14,74432,74433],{},"Make sure the number of variables matches the number of items.",[36,74435,74437],{"className":38,"code":74436,"language":40,"meta":41,"style":41},"point = (10, 20)\nx, y = point\n",[18,74438,74439,74455],{"__ignoreMap":41},[45,74440,74441,74443,74445,74447,74449,74451,74453],{"class":47,"line":48},[45,74442,41291],{"class":51},[45,74444,56],{"class":55},[45,74446,14039],{"class":85},[45,74448,11786],{"class":59},[45,74450,3079],{"class":85},[45,74452,10503],{"class":59},[45,74454,109],{"class":85},[45,74456,74457,74459,74461,74463,74465],{"class":47,"line":63},[45,74458,26156],{"class":51},[45,74460,3079],{"class":85},[45,74462,73555],{"class":51},[45,74464,56],{"class":55},[45,74466,73560],{"class":51},[712,74468,74470],{"id":74469},"accessing-an-index-that-does-not-exist","Accessing an index that does not exist",[14,74472,74473],{},"Check the tuple length before using a position if you are not sure.",[14,74475,31275],{},[36,74477,74479],{"className":38,"code":74478,"language":40,"meta":41,"style":41},"print(my_tuple)\nprint(type(my_tuple))\nprint(len(my_tuple))\nprint(my_tuple[0])\n",[18,74480,74481,74491,74505,74519],{"__ignoreMap":41},[45,74482,74483,74485,74487,74489],{"class":47,"line":48},[45,74484,2065],{"class":92},[45,74486,96],{"class":85},[45,74488,72852],{"class":2070},[45,74490,109],{"class":85},[45,74492,74493,74495,74497,74499,74501,74503],{"class":47,"line":63},[45,74494,2065],{"class":92},[45,74496,96],{"class":85},[45,74498,4340],{"class":2109},[45,74500,96],{"class":85},[45,74502,72852],{"class":2070},[45,74504,4348],{"class":85},[45,74506,74507,74509,74511,74513,74515,74517],{"class":47,"line":70},[45,74508,2065],{"class":92},[45,74510,96],{"class":85},[45,74512,28716],{"class":92},[45,74514,96],{"class":85},[45,74516,72852],{"class":2070},[45,74518,4348],{"class":85},[45,74520,74521,74523,74525,74527,74529,74531],{"class":47,"line":89},[45,74522,2065],{"class":92},[45,74524,96],{"class":85},[45,74526,72852],{"class":2070},[45,74528,9680],{"class":85},[45,74530,10482],{"class":59},[45,74532,21941],{"class":85},[14,74534,56994],{},[195,74536,74537,74540,74543,74546],{},[198,74538,74539],{},"what the tuple contains",[198,74541,74542],{},"whether the object is really a tuple",[198,74544,74545],{},"how many items it has",[198,74547,74548],{},"what is stored at the first position",[31,74550,2281],{"id":2280},[712,74552,74554],{"id":74553},"are-tuples-faster-than-lists","Are tuples faster than lists?",[14,74556,74557],{},"They can be slightly faster for some operations, but beginners should mainly think about whether the data should be changeable or fixed.",[712,74559,74561],{"id":74560},"can-a-tuple-contain-different-data-types","Can a tuple contain different data types?",[14,74563,74564],{},"Yes. A tuple can contain strings, numbers, booleans, and other objects in the same tuple.",[712,74566,74568],{"id":74567},"can-you-change-a-tuple-after-creating-it","Can you change a tuple after creating it?",[14,74570,74571],{},"No. You cannot directly add, remove, or replace items in a tuple.",[712,74573,74575],{"id":74574},"why-does-a-one-item-tuple-need-a-comma","Why does a one-item tuple need a comma?",[14,74577,74578],{},"Without the comma, Python treats the value as a normal expression, not as a tuple.",[712,74580,74582],{"id":74581},"when-should-i-use-a-tuple-instead-of-a-list","When should I use a tuple instead of a list?",[14,74584,74585],{},"Use a tuple when the values belong together and should not change.",[31,74587,2342],{"id":2341},[195,74589,74590,74595,74600,74605,74610,74614],{},[198,74591,74592],{},[236,74593,74594],{"href":72403},"What is a tuple in Python?",[198,74596,74597],{},[236,74598,74599],{"href":72723},"Creating a tuple in Python",[198,74601,74602],{},[236,74603,74604],{"href":73214},"Tuple indexing explained",[198,74606,74607],{},[236,74608,74609],{"href":73423},"Tuple slicing explained",[198,74611,74612],{},[236,74613,27366],{"href":27365},[198,74615,74616],{},[236,74617,68216],{"href":62012},[2367,74619,74620],{},"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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .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 .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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":41,"searchDepth":63,"depth":63,"links":74622},[74623,74624,74629,74632,74636,74637,74641,74647,74648,74655,74662],{"id":72243,"depth":63,"text":72244},{"id":72407,"depth":63,"text":72408,"children":74625},[74626,74627,74628],{"id":72414,"depth":70,"text":72415},{"id":72489,"depth":70,"text":72490},{"id":72557,"depth":70,"text":72558},{"id":72727,"depth":63,"text":72728,"children":74630},[74631],{"id":72734,"depth":70,"text":72735},{"id":72908,"depth":63,"text":72909,"children":74633},[74634,74635],{"id":73023,"depth":70,"text":73024},{"id":73120,"depth":70,"text":73121},{"id":73218,"depth":63,"text":73219},{"id":73427,"depth":63,"text":73428,"children":74638},[74639,74640],{"id":73431,"depth":70,"text":73432},{"id":73521,"depth":70,"text":73522},{"id":73762,"depth":63,"text":73763,"children":74642},[74643,74644,74645,74646],{"id":28716,"depth":70,"text":59071},{"id":8623,"depth":70,"text":8623},{"id":34014,"depth":70,"text":73917},{"id":60449,"depth":70,"text":73994},{"id":74100,"depth":63,"text":74101},{"id":5902,"depth":63,"text":5903,"children":74649},[74650,74651,74652,74653,74654],{"id":74251,"depth":70,"text":74252},{"id":74323,"depth":70,"text":74324},{"id":74369,"depth":70,"text":74370},{"id":74429,"depth":70,"text":74430},{"id":74469,"depth":70,"text":74470},{"id":2280,"depth":63,"text":2281,"children":74656},[74657,74658,74659,74660,74661],{"id":74553,"depth":70,"text":74554},{"id":74560,"depth":70,"text":74561},{"id":74567,"depth":70,"text":74568},{"id":74574,"depth":70,"text":74575},{"id":74581,"depth":70,"text":74582},{"id":2341,"depth":63,"text":2342},"Master python tuples explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-tuples-explained",{"title":72229,"description":74663},"learn\u002Fpython-tuples-explained","LiIBzJZkMLb0jHtXoS-uWPMrCLy95CE3qdIWM-AmY80",{"id":74670,"title":74671,"body":74672,"description":76826,"extension":2399,"meta":76827,"navigation":66,"path":76828,"seo":76829,"stem":76830,"__hash__":76831},"content\u002Flearn\u002Fpython-variables-explained-for-beginners.md","Python Variables Explained for Beginners",{"type":7,"value":74673,"toc":76784},[74674,74677,74680,74683,74695,74697,74746,74751,74755,74758,74761,74772,74775,74789,74794,74828,74830,74852,74858,74862,74865,74879,74881,74894,74903,74906,74924,74927,74930,74982,74984,74998,75003,75007,75010,75013,75030,75034,75061,75063,75071,75078,75082,75140,75142,75164,75168,75213,75215,75227,75230,75234,75284,75286,75294,75298,75301,75305,75308,75320,75322,75361,75365,75370,75373,75382,75385,75399,75403,75405,75414,75416,75435,75439,75447,75493,75495,75508,75512,75522,75525,75534,75538,75541,75543,75557,75560,75574,75577,75580,75623,75626,75629,75681,75683,75692,75695,75699,75702,75705,75749,75751,75763,75766,75769,75783,75787,75790,75793,75819,75821,75875,75880,75937,75939,75971,75978,75982,75985,75989,76018,76020,76033,76037,76084,76086,76094,76100,76169,76171,76193,76197,76238,76246,76252,76254,76257,76261,76263,76272,76275,76293,76297,76299,76308,76310,76324,76331,76341,76344,76357,76360,76379,76381,76389,76393,76395,76409,76416,76419,76442,76449,76453,76455,76464,76467,76481,76483,76486,76488,76510,76512,76516,76519,76523,76526,76584,76586,76619,76627,76632,76637,76664,76666,76674,76678,76681,76697,76699,76744,76746,76781],[10,74675,74671],{"id":74676},"python-variables-explained-for-beginners",[14,74678,74679],{},"Variables are one of the first things you learn in Python.",[14,74681,74682],{},"A variable is a name that points to a value. You use variables to store data, reuse it later, and make your programs easier to read.",[14,74684,74685,74686,74688,74689,230,74692,240],{},"In Python, you create a variable by assigning a value with the ",[18,74687,56],{}," sign. You do not need a special keyword like ",[18,74690,74691],{},"var",[18,74693,74694],{},"let",[31,74696,34],{"id":33},[36,74698,74700],{"className":38,"code":74699,"language":40,"meta":41,"style":41},"name = \"Alice\"\nage = 25\n\nprint(name)\nprint(age)\n",[18,74701,74702,74714,74722,74726,74736],{"__ignoreMap":41},[45,74703,74704,74706,74708,74710,74712],{"class":47,"line":48},[45,74705,1560],{"class":51},[45,74707,56],{"class":55},[45,74709,1565],{"class":99},[45,74711,60413],{"class":103},[45,74713,1571],{"class":99},[45,74715,74716,74718,74720],{"class":47,"line":63},[45,74717,52],{"class":51},[45,74719,56],{"class":55},[45,74721,30102],{"class":59},[45,74723,74724],{"class":47,"line":70},[45,74725,67],{"emptyLinePlaceholder":66},[45,74727,74728,74730,74732,74734],{"class":47,"line":89},[45,74729,2065],{"class":92},[45,74731,96],{"class":85},[45,74733,3096],{"class":2070},[45,74735,109],{"class":85},[45,74737,74738,74740,74742,74744],{"class":47,"line":112},[45,74739,2065],{"class":92},[45,74741,96],{"class":85},[45,74743,2082],{"class":2070},[45,74745,109],{"class":85},[14,74747,42429,74748,74750],{},[18,74749,56],{}," sign to assign a value to a variable. Then use the variable name later in your code.",[31,74752,74754],{"id":74753},"what-a-variable-is","What a variable is",[14,74756,74757],{},"A variable is a name that refers to a value.",[14,74759,74760],{},"This lets you:",[195,74762,74763,74766,74769],{},[198,74764,74765],{},"store data",[198,74767,74768],{},"use it later",[198,74770,74771],{},"update it when needed",[14,74773,74774],{},"For example, a variable might store:",[195,74776,74777,74780,74783,74786],{},[198,74778,74779],{},"a person's name",[198,74781,74782],{},"a number",[198,74784,74785],{},"a price",[198,74787,74788],{},"a value entered by the user",[14,74790,74791,74792,240],{},"In Python, a variable is created when you assign a value to it with ",[18,74793,56],{},[36,74795,74797],{"className":38,"code":74796,"language":40,"meta":41,"style":41},"message = \"Hello\"\nscore = 10\nprice = 4.99\n",[18,74798,74799,74811,74819],{"__ignoreMap":41},[45,74800,74801,74803,74805,74807,74809],{"class":47,"line":48},[45,74802,15507],{"class":51},[45,74804,56],{"class":55},[45,74806,1565],{"class":99},[45,74808,3226],{"class":103},[45,74810,1571],{"class":99},[45,74812,74813,74815,74817],{"class":47,"line":63},[45,74814,581],{"class":51},[45,74816,56],{"class":55},[45,74818,1174],{"class":59},[45,74820,74821,74823,74825],{"class":47,"line":70},[45,74822,40904],{"class":51},[45,74824,56],{"class":55},[45,74826,74827],{"class":59}," 4.99\n",[14,74829,6680],{},[195,74831,74832,74839,74845],{},[198,74833,74834,74836,74837],{},[18,74835,6462],{}," refers to ",[18,74838,6701],{},[198,74840,74841,74836,74843],{},[18,74842,46543],{},[18,74844,11786],{},[198,74846,74847,74836,74849],{},[18,74848,19985],{},[18,74850,74851],{},"4.99",[14,74853,37628,74854,240],{},[236,74855,74857],{"href":74856},"\u002Fglossary\u002Fwhat-is-a-variable-in-python\u002F","what is a variable in Python",[31,74859,74861],{"id":74860},"how-to-create-a-variable-in-python","How to create a variable in Python",[14,74863,74864],{},"To create a variable:",[195,74866,74867,74870,74876],{},[198,74868,74869],{},"put the variable name on the left",[198,74871,74872,74873,74875],{},"put ",[18,74874,56],{}," in the middle",[198,74877,74878],{},"put the value on the right",[14,74880,466],{},[36,74882,74884],{"className":38,"code":74883,"language":40,"meta":41,"style":41},"score = 10\n",[18,74885,74886],{"__ignoreMap":41},[45,74887,74888,74890,74892],{"class":47,"line":48},[45,74889,581],{"class":51},[45,74891,56],{"class":55},[45,74893,1174],{"class":59},[14,74895,74896,74897,74899,74900,74902],{},"This means: “store the value ",[18,74898,11786],{}," in the variable named ",[18,74901,46543],{},".”",[14,74904,74905],{},"Python does not require a declaration first. This is valid:",[36,74907,74909],{"className":38,"code":74908,"language":40,"meta":41,"style":41},"city = \"Paris\"\n",[18,74910,74911],{"__ignoreMap":41},[45,74912,74913,74916,74918,74920,74922],{"class":47,"line":48},[45,74914,74915],{"class":51},"city ",[45,74917,56],{"class":55},[45,74919,1565],{"class":99},[45,74921,10593],{"class":103},[45,74923,1571],{"class":99},[14,74925,74926],{},"You do not need to write anything before it.",[14,74928,74929],{},"Here is another simple example:",[36,74931,74933],{"className":38,"code":74932,"language":40,"meta":41,"style":41},"user_name = \"Maya\"\nitem_count = 3\n\nprint(user_name)\nprint(item_count)\n",[18,74934,74935,74948,74957,74961,74971],{"__ignoreMap":41},[45,74936,74937,74940,74942,74944,74946],{"class":47,"line":48},[45,74938,74939],{"class":51},"user_name ",[45,74941,56],{"class":55},[45,74943,1565],{"class":99},[45,74945,5737],{"class":103},[45,74947,1571],{"class":99},[45,74949,74950,74953,74955],{"class":47,"line":63},[45,74951,74952],{"class":51},"item_count ",[45,74954,56],{"class":55},[45,74956,1481],{"class":59},[45,74958,74959],{"class":47,"line":70},[45,74960,67],{"emptyLinePlaceholder":66},[45,74962,74963,74965,74967,74969],{"class":47,"line":89},[45,74964,2065],{"class":92},[45,74966,96],{"class":85},[45,74968,12758],{"class":2070},[45,74970,109],{"class":85},[45,74972,74973,74975,74977,74980],{"class":47,"line":112},[45,74974,2065],{"class":92},[45,74976,96],{"class":85},[45,74978,74979],{"class":2070},"item_count",[45,74981,109],{"class":85},[14,74983,17038],{},[36,74985,74987],{"className":38,"code":74986,"language":40,"meta":41,"style":41},"Maya\n3\n",[18,74988,74989,74994],{"__ignoreMap":41},[45,74990,74991],{"class":47,"line":48},[45,74992,74993],{"class":51},"Maya\n",[45,74995,74996],{"class":47,"line":63},[45,74997,13046],{"class":59},[14,74999,75000,75001,240],{},"If you are also learning the basic rules of writing Python code, see ",[236,75002,40356],{"href":39615},[31,75004,75006],{"id":75005},"how-variables-are-used","How variables are used",[14,75008,75009],{},"Variables are useful because they let you reuse values.",[14,75011,75012],{},"You can use them to:",[195,75014,75015,75018,75021,75024,75027],{},[198,75016,75017],{},"print stored values",[198,75019,75020],{},"use the same value in multiple places",[198,75022,75023],{},"update values",[198,75025,75026],{},"do calculations",[198,75028,75029],{},"check conditions",[712,75031,75033],{"id":75032},"print-stored-values","Print stored values",[36,75035,75037],{"className":38,"code":75036,"language":40,"meta":41,"style":41},"language = \"Python\"\nprint(language)\n",[18,75038,75039,75051],{"__ignoreMap":41},[45,75040,75041,75043,75045,75047,75049],{"class":47,"line":48},[45,75042,68507],{"class":51},[45,75044,56],{"class":55},[45,75046,1565],{"class":99},[45,75048,16791],{"class":103},[45,75050,1571],{"class":99},[45,75052,75053,75055,75057,75059],{"class":47,"line":63},[45,75054,2065],{"class":92},[45,75056,96],{"class":85},[45,75058,42977],{"class":2070},[45,75060,109],{"class":85},[14,75062,17038],{},[36,75064,75065],{"className":38,"code":68403,"language":40,"meta":41,"style":41},[18,75066,75067],{"__ignoreMap":41},[45,75068,75069],{"class":47,"line":48},[45,75070,68403],{"class":51},[14,75072,75073,75074,240],{},"You can learn more about this on the ",[236,75075,18104,75076,19075],{"href":18110},[18,75077,15833],{},[712,75079,75081],{"id":75080},"reuse-a-value","Reuse a value",[36,75083,75085],{"className":38,"code":75084,"language":40,"meta":41,"style":41},"name = \"Ava\"\n\nprint(\"Hello,\", name)\nprint(\"Your name is\", name)\n",[18,75086,75087,75099,75103,75121],{"__ignoreMap":41},[45,75088,75089,75091,75093,75095,75097],{"class":47,"line":48},[45,75090,1560],{"class":51},[45,75092,56],{"class":55},[45,75094,1565],{"class":99},[45,75096,3162],{"class":103},[45,75098,1571],{"class":99},[45,75100,75101],{"class":47,"line":63},[45,75102,67],{"emptyLinePlaceholder":66},[45,75104,75105,75107,75109,75111,75113,75115,75117,75119],{"class":47,"line":70},[45,75106,2065],{"class":92},[45,75108,96],{"class":85},[45,75110,100],{"class":99},[45,75112,18065],{"class":103},[45,75114,100],{"class":99},[45,75116,3079],{"class":85},[45,75118,3083],{"class":2070},[45,75120,109],{"class":85},[45,75122,75123,75125,75127,75129,75132,75134,75136,75138],{"class":47,"line":89},[45,75124,2065],{"class":92},[45,75126,96],{"class":85},[45,75128,100],{"class":99},[45,75130,75131],{"class":103},"Your name is",[45,75133,100],{"class":99},[45,75135,3079],{"class":85},[45,75137,3083],{"class":2070},[45,75139,109],{"class":85},[14,75141,17038],{},[36,75143,75145],{"className":38,"code":75144,"language":40,"meta":41,"style":41},"Hello, Ava\nYour name is Ava\n",[18,75146,75147,75155],{"__ignoreMap":41},[45,75148,75149,75151,75153],{"class":47,"line":48},[45,75150,3226],{"class":51},[45,75152,3079],{"class":85},[45,75154,3236],{"class":51},[45,75156,75157,75160,75162],{"class":47,"line":63},[45,75158,75159],{"class":51},"Your name ",[45,75161,1285],{"class":55},[45,75163,3236],{"class":51},[712,75165,75167],{"id":75166},"update-a-variable","Update a variable",[36,75169,75171],{"className":38,"code":75170,"language":40,"meta":41,"style":41},"count = 1\nprint(count)\n\ncount = 2\nprint(count)\n",[18,75172,75173,75181,75191,75195,75203],{"__ignoreMap":41},[45,75174,75175,75177,75179],{"class":47,"line":48},[45,75176,33984],{"class":51},[45,75178,56],{"class":55},[45,75180,12962],{"class":59},[45,75182,75183,75185,75187,75189],{"class":47,"line":63},[45,75184,2065],{"class":92},[45,75186,96],{"class":85},[45,75188,34014],{"class":2070},[45,75190,109],{"class":85},[45,75192,75193],{"class":47,"line":70},[45,75194,67],{"emptyLinePlaceholder":66},[45,75196,75197,75199,75201],{"class":47,"line":89},[45,75198,33984],{"class":51},[45,75200,56],{"class":55},[45,75202,13423],{"class":59},[45,75204,75205,75207,75209,75211],{"class":47,"line":112},[45,75206,2065],{"class":92},[45,75208,96],{"class":85},[45,75210,34014],{"class":2070},[45,75212,109],{"class":85},[14,75214,17038],{},[36,75216,75217],{"className":38,"code":36722,"language":40,"meta":41,"style":41},[18,75218,75219,75223],{"__ignoreMap":41},[45,75220,75221],{"class":47,"line":48},[45,75222,13036],{"class":59},[45,75224,75225],{"class":47,"line":63},[45,75226,13041],{"class":59},[14,75228,75229],{},"The second assignment replaces the first value.",[712,75231,75233],{"id":75232},"use-variables-in-calculations","Use variables in calculations",[36,75235,75237],{"className":38,"code":75236,"language":40,"meta":41,"style":41},"price = 10\ntax = 2\ntotal = price + tax\n\nprint(total)\n",[18,75238,75239,75247,75256,75269,75273],{"__ignoreMap":41},[45,75240,75241,75243,75245],{"class":47,"line":48},[45,75242,40904],{"class":51},[45,75244,56],{"class":55},[45,75246,1174],{"class":59},[45,75248,75249,75252,75254],{"class":47,"line":63},[45,75250,75251],{"class":51},"tax ",[45,75253,56],{"class":55},[45,75255,13423],{"class":59},[45,75257,75258,75260,75262,75264,75266],{"class":47,"line":70},[45,75259,39569],{"class":51},[45,75261,56],{"class":55},[45,75263,39574],{"class":51},[45,75265,8850],{"class":55},[45,75267,75268],{"class":51}," tax\n",[45,75270,75271],{"class":47,"line":89},[45,75272,67],{"emptyLinePlaceholder":66},[45,75274,75275,75277,75279,75282],{"class":47,"line":112},[45,75276,2065],{"class":92},[45,75278,96],{"class":85},[45,75280,75281],{"class":2070},"total",[45,75283,109],{"class":85},[14,75285,17038],{},[36,75287,75288],{"className":38,"code":3629,"language":40,"meta":41,"style":41},[18,75289,75290],{"__ignoreMap":41},[45,75291,75292],{"class":47,"line":48},[45,75293,3629],{"class":59},[31,75295,75297],{"id":75296},"variable-naming-rules","Variable naming rules",[14,75299,75300],{},"Python has a few rules for variable names.",[712,75302,75304],{"id":75303},"allowed","Allowed",[14,75306,75307],{},"Variable names can contain:",[195,75309,75310,75312,75314],{},[198,75311,60630],{},[198,75313,9069],{},[198,75315,75316,75317,9146],{},"underscores (",[18,75318,75319],{},"_",[14,75321,40802],{},[36,75323,75325],{"className":38,"code":75324,"language":40,"meta":41,"style":41},"name = \"Sam\"\nscore1 = 99\nuser_name = \"sam123\"\n",[18,75326,75327,75339,75348],{"__ignoreMap":41},[45,75328,75329,75331,75333,75335,75337],{"class":47,"line":48},[45,75330,1560],{"class":51},[45,75332,56],{"class":55},[45,75334,1565],{"class":99},[45,75336,1568],{"class":103},[45,75338,1571],{"class":99},[45,75340,75341,75344,75346],{"class":47,"line":63},[45,75342,75343],{"class":51},"score1 ",[45,75345,56],{"class":55},[45,75347,50943],{"class":59},[45,75349,75350,75352,75354,75356,75359],{"class":47,"line":70},[45,75351,74939],{"class":51},[45,75353,56],{"class":55},[45,75355,1565],{"class":99},[45,75357,75358],{"class":103},"sam123",[45,75360,1571],{"class":99},[712,75362,75364],{"id":75363},"not-allowed","Not allowed",[75366,75367,75369],"h4",{"id":75368},"names-cannot-start-with-a-number","Names cannot start with a number",[14,75371,75372],{},"This is invalid:",[36,75374,75376],{"className":38,"code":75375,"language":40,"meta":41,"style":41},"# 2score = 10\n",[18,75377,75378],{"__ignoreMap":41},[45,75379,75380],{"class":47,"line":48},[45,75381,75375],{"class":7624},[14,75383,75384],{},"A valid version would be:",[36,75386,75388],{"className":38,"code":75387,"language":40,"meta":41,"style":41},"score2 = 10\n",[18,75389,75390],{"__ignoreMap":41},[45,75391,75392,75395,75397],{"class":47,"line":48},[45,75393,75394],{"class":51},"score2 ",[45,75396,56],{"class":55},[45,75398,1174],{"class":59},[75366,75400,75402],{"id":75401},"names-cannot-contain-spaces","Names cannot contain spaces",[14,75404,75372],{},[36,75406,75408],{"className":38,"code":75407,"language":40,"meta":41,"style":41},"# first name = \"Lena\"\n",[18,75409,75410],{"__ignoreMap":41},[45,75411,75412],{"class":47,"line":48},[45,75413,75407],{"class":7624},[14,75415,75384],{},[36,75417,75419],{"className":38,"code":75418,"language":40,"meta":41,"style":41},"first_name = \"Lena\"\n",[18,75420,75421],{"__ignoreMap":41},[45,75422,75423,75426,75428,75430,75433],{"class":47,"line":48},[45,75424,75425],{"class":51},"first_name ",[45,75427,56],{"class":55},[45,75429,1565],{"class":99},[45,75431,75432],{"class":103},"Lena",[45,75434,1571],{"class":99},[75366,75436,75438],{"id":75437},"names-are-case-sensitive","Names are case-sensitive",[14,75440,75441,1316,75443,75446],{},[18,75442,2082],{},[18,75444,75445],{},"Age"," are different variables.",[36,75448,75450],{"className":38,"code":75449,"language":40,"meta":41,"style":41},"age = 20\nAge = 30\n\nprint(age)\nprint(Age)\n",[18,75451,75452,75460,75469,75473,75483],{"__ignoreMap":41},[45,75453,75454,75456,75458],{"class":47,"line":48},[45,75455,52],{"class":51},[45,75457,56],{"class":55},[45,75459,1883],{"class":59},[45,75461,75462,75465,75467],{"class":47,"line":63},[45,75463,75464],{"class":51},"Age ",[45,75466,56],{"class":55},[45,75468,258],{"class":59},[45,75470,75471],{"class":47,"line":70},[45,75472,67],{"emptyLinePlaceholder":66},[45,75474,75475,75477,75479,75481],{"class":47,"line":89},[45,75476,2065],{"class":92},[45,75478,96],{"class":85},[45,75480,2082],{"class":2070},[45,75482,109],{"class":85},[45,75484,75485,75487,75489,75491],{"class":47,"line":112},[45,75486,2065],{"class":92},[45,75488,96],{"class":85},[45,75490,75445],{"class":2070},[45,75492,109],{"class":85},[14,75494,17038],{},[36,75496,75498],{"className":38,"code":75497,"language":40,"meta":41,"style":41},"20\n30\n",[18,75499,75500,75504],{"__ignoreMap":41},[45,75501,75502],{"class":47,"line":48},[45,75503,3436],{"class":59},[45,75505,75506],{"class":47,"line":63},[45,75507,22614],{"class":59},[75366,75509,75511],{"id":75510},"do-not-use-python-keywords","Do not use Python keywords",[14,75513,75514,75515,21,75517,25,75519,75521],{},"Words like ",[18,75516,20],{},[18,75518,7895],{},[18,75520,2441],{}," have special meaning in Python, so you cannot use them as variable names.",[14,75523,75524],{},"Invalid example:",[36,75526,75528],{"className":38,"code":75527,"language":40,"meta":41,"style":41},"# class = \"Math\"\n",[18,75529,75530],{"__ignoreMap":41},[45,75531,75532],{"class":47,"line":48},[45,75533,75527],{"class":7624},[31,75535,75537],{"id":75536},"good-variable-names","Good variable names",[14,75539,75540],{},"Choose names that clearly describe the value.",[14,75542,20490],{},[195,75544,75545,75549,75553],{},[198,75546,75547],{},[18,75548,12758],{},[198,75550,75551],{},[18,75552,47160],{},[198,75554,75555],{},[18,75556,74979],{},[14,75558,75559],{},"These are easier to understand than unclear names like:",[195,75561,75562,75566,75570],{},[198,75563,75564],{},[18,75565,26156],{},[198,75567,75568],{},[18,75569,236],{},[198,75571,75572],{},[18,75573,30614],{},[14,75575,75576],{},"Short names are not always wrong, but beginners should usually prefer readable names.",[14,75578,75579],{},"Compare these:",[36,75581,75583],{"className":38,"code":75582,"language":40,"meta":41,"style":41},"x = 19.99\ny = 2\nz = x * y\nprint(z)\n",[18,75584,75585,75593,75601,75613],{"__ignoreMap":41},[45,75586,75587,75589,75591],{"class":47,"line":48},[45,75588,1169],{"class":51},[45,75590,56],{"class":55},[45,75592,40909],{"class":59},[45,75594,75595,75597,75599],{"class":47,"line":63},[45,75596,29448],{"class":51},[45,75598,56],{"class":55},[45,75600,13423],{"class":59},[45,75602,75603,75605,75607,75609,75611],{"class":47,"line":70},[45,75604,40504],{"class":51},[45,75606,56],{"class":55},[45,75608,1185],{"class":51},[45,75610,5601],{"class":55},[45,75612,26178],{"class":51},[45,75614,75615,75617,75619,75621],{"class":47,"line":89},[45,75616,2065],{"class":92},[45,75618,96],{"class":85},[45,75620,40533],{"class":2070},[45,75622,109],{"class":85},[14,75624,75625],{},"This works, but it is not very clear.",[14,75627,75628],{},"A better version:",[36,75630,75632],{"className":38,"code":75631,"language":40,"meta":41,"style":41},"item_price = 19.99\nquantity = 2\ntotal_price = item_price * quantity\n\nprint(total_price)\n",[18,75633,75634,75643,75652,75667,75671],{"__ignoreMap":41},[45,75635,75636,75639,75641],{"class":47,"line":48},[45,75637,75638],{"class":51},"item_price ",[45,75640,56],{"class":55},[45,75642,40909],{"class":59},[45,75644,75645,75648,75650],{"class":47,"line":63},[45,75646,75647],{"class":51},"quantity ",[45,75649,56],{"class":55},[45,75651,13423],{"class":59},[45,75653,75654,75657,75659,75662,75664],{"class":47,"line":70},[45,75655,75656],{"class":51},"total_price ",[45,75658,56],{"class":55},[45,75660,75661],{"class":51}," item_price ",[45,75663,5601],{"class":55},[45,75665,75666],{"class":51}," quantity\n",[45,75668,75669],{"class":47,"line":89},[45,75670,67],{"emptyLinePlaceholder":66},[45,75672,75673,75675,75677,75679],{"class":47,"line":112},[45,75674,2065],{"class":92},[45,75676,96],{"class":85},[45,75678,47160],{"class":2070},[45,75680,109],{"class":85},[14,75682,17038],{},[36,75684,75686],{"className":38,"code":75685,"language":40,"meta":41,"style":41},"39.98\n",[18,75687,75688],{"__ignoreMap":41},[45,75689,75690],{"class":47,"line":48},[45,75691,75685],{"class":59},[14,75693,75694],{},"Good names make your code easier to read and easier to debug.",[31,75696,75698],{"id":75697},"variables-can-change","Variables can change",[14,75700,75701],{},"Variables are called “variables” because their values can change.",[14,75703,75704],{},"You can assign a new value to the same variable at any time.",[36,75706,75707],{"className":38,"code":75170,"language":40,"meta":41,"style":41},[18,75708,75709,75717,75727,75731,75739],{"__ignoreMap":41},[45,75710,75711,75713,75715],{"class":47,"line":48},[45,75712,33984],{"class":51},[45,75714,56],{"class":55},[45,75716,12962],{"class":59},[45,75718,75719,75721,75723,75725],{"class":47,"line":63},[45,75720,2065],{"class":92},[45,75722,96],{"class":85},[45,75724,34014],{"class":2070},[45,75726,109],{"class":85},[45,75728,75729],{"class":47,"line":70},[45,75730,67],{"emptyLinePlaceholder":66},[45,75732,75733,75735,75737],{"class":47,"line":89},[45,75734,33984],{"class":51},[45,75736,56],{"class":55},[45,75738,13423],{"class":59},[45,75740,75741,75743,75745,75747],{"class":47,"line":112},[45,75742,2065],{"class":92},[45,75744,96],{"class":85},[45,75746,34014],{"class":2070},[45,75748,109],{"class":85},[14,75750,17038],{},[36,75752,75753],{"className":38,"code":36722,"language":40,"meta":41,"style":41},[18,75754,75755,75759],{"__ignoreMap":41},[45,75756,75757],{"class":47,"line":48},[45,75758,13036],{"class":59},[45,75760,75761],{"class":47,"line":63},[45,75762,13041],{"class":59},[14,75764,75765],{},"The old value is replaced by the new one.",[14,75767,75768],{},"This is useful in programs that track changing data, such as:",[195,75770,75771,75774,75777,75780],{},[198,75772,75773],{},"a score",[198,75775,75776],{},"a total",[198,75778,75779],{},"a user's answer",[198,75781,75782],{},"the current step in a process",[31,75784,75786],{"id":75785},"variables-and-data-types","Variables and data types",[14,75788,75789],{},"A variable can refer to different kinds of data.",[14,75791,75792],{},"Some common Python data types are:",[195,75794,75795,75800,75805,75810],{},[198,75796,75797,75799],{},[18,75798,29733],{}," for text",[198,75801,75802,75804],{},[18,75803,29738],{}," for whole numbers",[198,75806,75807,75809],{},[18,75808,29743],{}," for numbers with decimals",[198,75811,75812,75814,75815,230,75817],{},[18,75813,2110],{}," for ",[18,75816,229],{},[18,75818,233],{},[14,75820,40802],{},[36,75822,75824],{"className":38,"code":75823,"language":40,"meta":41,"style":41},"city = \"Paris\"      # str\nage = 20            # int\nprice = 9.99        # float\nis_ready = True     # bool\n",[18,75825,75826,75841,75852,75864],{"__ignoreMap":41},[45,75827,75828,75830,75832,75834,75836,75838],{"class":47,"line":48},[45,75829,74915],{"class":51},[45,75831,56],{"class":55},[45,75833,1565],{"class":99},[45,75835,10593],{"class":103},[45,75837,100],{"class":99},[45,75839,75840],{"class":7624},"      # str\n",[45,75842,75843,75845,75847,75849],{"class":47,"line":63},[45,75844,52],{"class":51},[45,75846,56],{"class":55},[45,75848,10503],{"class":59},[45,75850,75851],{"class":7624},"            # int\n",[45,75853,75854,75856,75858,75861],{"class":47,"line":70},[45,75855,40904],{"class":51},[45,75857,56],{"class":55},[45,75859,75860],{"class":59}," 9.99",[45,75862,75863],{"class":7624},"        # float\n",[45,75865,75866,75868,75870,75872],{"class":47,"line":89},[45,75867,35142],{"class":51},[45,75869,56],{"class":55},[45,75871,23323],{"class":481},[45,75873,75874],{"class":7624},"     # bool\n",[14,75876,75877,75878,457],{},"You can check the type of a value with ",[18,75879,40437],{},[36,75881,75883],{"className":38,"code":75882,"language":40,"meta":41,"style":41},"city = \"Paris\"\nage = 20\n\nprint(type(city))\nprint(type(age))\n",[18,75884,75885,75897,75905,75909,75923],{"__ignoreMap":41},[45,75886,75887,75889,75891,75893,75895],{"class":47,"line":48},[45,75888,74915],{"class":51},[45,75890,56],{"class":55},[45,75892,1565],{"class":99},[45,75894,10593],{"class":103},[45,75896,1571],{"class":99},[45,75898,75899,75901,75903],{"class":47,"line":63},[45,75900,52],{"class":51},[45,75902,56],{"class":55},[45,75904,1883],{"class":59},[45,75906,75907],{"class":47,"line":70},[45,75908,67],{"emptyLinePlaceholder":66},[45,75910,75911,75913,75915,75917,75919,75921],{"class":47,"line":89},[45,75912,2065],{"class":92},[45,75914,96],{"class":85},[45,75916,4340],{"class":2109},[45,75918,96],{"class":85},[45,75920,10562],{"class":2070},[45,75922,4348],{"class":85},[45,75924,75925,75927,75929,75931,75933,75935],{"class":47,"line":112},[45,75926,2065],{"class":92},[45,75928,96],{"class":85},[45,75930,4340],{"class":2109},[45,75932,96],{"class":85},[45,75934,2082],{"class":2070},[45,75936,4348],{"class":85},[14,75938,17038],{},[36,75940,75941],{"className":38,"code":42201,"language":40,"meta":41,"style":41},[18,75942,75943,75957],{"__ignoreMap":41},[45,75944,75945,75947,75949,75951,75953,75955],{"class":47,"line":48},[45,75946,79],{"class":55},[45,75948,2441],{"class":2440},[45,75950,9199],{"class":99},[45,75952,29733],{"class":103},[45,75954,7901],{"class":99},[45,75956,13484],{"class":55},[45,75958,75959,75961,75963,75965,75967,75969],{"class":47,"line":63},[45,75960,79],{"class":55},[45,75962,2441],{"class":2440},[45,75964,9199],{"class":99},[45,75966,29738],{"class":103},[45,75968,7901],{"class":99},[45,75970,13484],{"class":55},[14,75972,75973,75974,1316,75976,240],{},"If you want to learn these in more detail, see ",[236,75975,35582],{"href":35108},[236,75977,42104],{"href":42103},[31,75979,75981],{"id":75980},"simple-beginner-examples","Simple beginner examples",[14,75983,75984],{},"Here are a few short examples that show common ways to use variables.",[712,75986,75988],{"id":75987},"store-text-in-a-variable","Store text in a variable",[36,75990,75992],{"className":38,"code":75991,"language":40,"meta":41,"style":41},"greeting = \"Hello, world!\"\nprint(greeting)\n",[18,75993,75994,76007],{"__ignoreMap":41},[45,75995,75996,75999,76001,76003,76005],{"class":47,"line":48},[45,75997,75998],{"class":51},"greeting ",[45,76000,56],{"class":55},[45,76002,1565],{"class":99},[45,76004,17031],{"class":103},[45,76006,1571],{"class":99},[45,76008,76009,76011,76013,76016],{"class":47,"line":63},[45,76010,2065],{"class":92},[45,76012,96],{"class":85},[45,76014,76015],{"class":2070},"greeting",[45,76017,109],{"class":85},[14,76019,17038],{},[36,76021,76022],{"className":38,"code":17042,"language":40,"meta":41,"style":41},[18,76023,76024],{"__ignoreMap":41},[45,76025,76026,76028,76030],{"class":47,"line":48},[45,76027,3226],{"class":51},[45,76029,3079],{"class":85},[45,76031,76032],{"class":51}," world!\n",[712,76034,76036],{"id":76035},"store-numbers-and-add-them","Store numbers and add them",[36,76038,76040],{"className":38,"code":76039,"language":40,"meta":41,"style":41},"a = 5\nb = 7\ntotal = a + b\n\nprint(total)\n",[18,76041,76042,76050,76058,76070,76074],{"__ignoreMap":41},[45,76043,76044,76046,76048],{"class":47,"line":48},[45,76045,11541],{"class":51},[45,76047,56],{"class":55},[45,76049,733],{"class":59},[45,76051,76052,76054,76056],{"class":47,"line":63},[45,76053,19315],{"class":51},[45,76055,56],{"class":55},[45,76057,55913],{"class":59},[45,76059,76060,76062,76064,76066,76068],{"class":47,"line":70},[45,76061,39569],{"class":51},[45,76063,56],{"class":55},[45,76065,11124],{"class":51},[45,76067,8850],{"class":55},[45,76069,11129],{"class":51},[45,76071,76072],{"class":47,"line":89},[45,76073,67],{"emptyLinePlaceholder":66},[45,76075,76076,76078,76080,76082],{"class":47,"line":112},[45,76077,2065],{"class":92},[45,76079,96],{"class":85},[45,76081,75281],{"class":2070},[45,76083,109],{"class":85},[14,76085,17038],{},[36,76087,76088],{"className":38,"code":3629,"language":40,"meta":41,"style":41},[18,76089,76090],{"__ignoreMap":41},[45,76091,76092],{"class":47,"line":48},[45,76093,3629],{"class":59},[712,76095,76097,76098],{"id":76096},"use-variables-with-print","Use variables with ",[18,76099,15833],{},[36,76101,76103],{"className":38,"code":76102,"language":40,"meta":41,"style":41},"name = \"Noah\"\nscore = 95\n\nprint(\"Student:\", name)\nprint(\"Score:\", score)\n",[18,76104,76105,76117,76126,76130,76149],{"__ignoreMap":41},[45,76106,76107,76109,76111,76113,76115],{"class":47,"line":48},[45,76108,1560],{"class":51},[45,76110,56],{"class":55},[45,76112,1565],{"class":99},[45,76114,20818],{"class":103},[45,76116,1571],{"class":99},[45,76118,76119,76121,76123],{"class":47,"line":63},[45,76120,581],{"class":51},[45,76122,56],{"class":55},[45,76124,76125],{"class":59}," 95\n",[45,76127,76128],{"class":47,"line":70},[45,76129,67],{"emptyLinePlaceholder":66},[45,76131,76132,76134,76136,76138,76141,76143,76145,76147],{"class":47,"line":89},[45,76133,2065],{"class":92},[45,76135,96],{"class":85},[45,76137,100],{"class":99},[45,76139,76140],{"class":103},"Student:",[45,76142,100],{"class":99},[45,76144,3079],{"class":85},[45,76146,3083],{"class":2070},[45,76148,109],{"class":85},[45,76150,76151,76153,76155,76157,76160,76162,76164,76167],{"class":47,"line":112},[45,76152,2065],{"class":92},[45,76154,96],{"class":85},[45,76156,100],{"class":99},[45,76158,76159],{"class":103},"Score:",[45,76161,100],{"class":99},[45,76163,3079],{"class":85},[45,76165,76166],{"class":2070}," score",[45,76168,109],{"class":85},[14,76170,17038],{},[36,76172,76174],{"className":38,"code":76173,"language":40,"meta":41,"style":41},"Student: Noah\nScore: 95\n",[18,76175,76176,76184],{"__ignoreMap":41},[45,76177,76178,76180,76182],{"class":47,"line":48},[45,76179,38398],{"class":51},[45,76181,457],{"class":85},[45,76183,52137],{"class":51},[45,76185,76186,76189,76191],{"class":47,"line":63},[45,76187,76188],{"class":51},"Score",[45,76190,457],{"class":85},[45,76192,76125],{"class":59},[712,76194,76196],{"id":76195},"store-user-input-in-a-variable","Store user input in a variable",[36,76198,76200],{"className":38,"code":76199,"language":40,"meta":41,"style":41},"name = input(\"Enter your name: \")\nprint(\"Hello,\", name)\n",[18,76201,76202,76220],{"__ignoreMap":41},[45,76203,76204,76206,76208,76210,76212,76214,76216,76218],{"class":47,"line":48},[45,76205,1560],{"class":51},[45,76207,56],{"class":55},[45,76209,18043],{"class":92},[45,76211,96],{"class":85},[45,76213,100],{"class":99},[45,76215,39667],{"class":103},[45,76217,100],{"class":99},[45,76219,109],{"class":85},[45,76221,76222,76224,76226,76228,76230,76232,76234,76236],{"class":47,"line":63},[45,76223,2065],{"class":92},[45,76225,96],{"class":85},[45,76227,100],{"class":99},[45,76229,18065],{"class":103},[45,76231,100],{"class":99},[45,76233,3079],{"class":85},[45,76235,3083],{"class":2070},[45,76237,109],{"class":85},[14,76239,1732,76240,76242,76243,76245],{},[18,76241,18020],{}," function returns text. That means if a user types ",[18,76244,14942],{},", Python stores it as a string unless you convert it.",[14,76247,23968,76248,240],{},[236,76249,18104,76250,19075],{"href":18103},[18,76251,18020],{},[31,76253,1337],{"id":1336},[14,76255,76256],{},"These are some of the most common variable mistakes in Python.",[712,76258,76260],{"id":76259},"using-spaces-in-variable-names","Using spaces in variable names",[14,76262,75372],{},[36,76264,76266],{"className":38,"code":76265,"language":40,"meta":41,"style":41},"# first name = \"Ali\"\n",[18,76267,76268],{"__ignoreMap":41},[45,76269,76270],{"class":47,"line":48},[45,76271,76265],{"class":7624},[14,76273,76274],{},"Use an underscore instead:",[36,76276,76278],{"className":38,"code":76277,"language":40,"meta":41,"style":41},"first_name = \"Ali\"\n",[18,76279,76280],{"__ignoreMap":41},[45,76281,76282,76284,76286,76288,76291],{"class":47,"line":48},[45,76283,75425],{"class":51},[45,76285,56],{"class":55},[45,76287,1565],{"class":99},[45,76289,76290],{"class":103},"Ali",[45,76292,1571],{"class":99},[712,76294,76296],{"id":76295},"starting-a-variable-name-with-a-number","Starting a variable name with a number",[14,76298,75372],{},[36,76300,76302],{"className":38,"code":76301,"language":40,"meta":41,"style":41},"# 2score = 50\n",[18,76303,76304],{"__ignoreMap":41},[45,76305,76306],{"class":47,"line":48},[45,76307,76301],{"class":7624},[14,76309,67899],{},[36,76311,76313],{"className":38,"code":76312,"language":40,"meta":41,"style":41},"score2 = 50\n",[18,76314,76315],{"__ignoreMap":41},[45,76316,76317,76319,76321],{"class":47,"line":48},[45,76318,75394],{"class":51},[45,76320,56],{"class":55},[45,76322,76323],{"class":59}," 50\n",[712,76325,52955,76327,14527,76329],{"id":76326},"confusing-with",[18,76328,56],{},[18,76330,1129],{},[195,76332,76333,76337],{},[198,76334,76335,1325],{},[18,76336,56],{},[198,76338,76339,1330],{},[18,76340,1129],{},[14,76342,76343],{},"Assignment:",[36,76345,76347],{"className":38,"code":76346,"language":40,"meta":41,"style":41},"age = 20\n",[18,76348,76349],{"__ignoreMap":41},[45,76350,76351,76353,76355],{"class":47,"line":48},[45,76352,52],{"class":51},[45,76354,56],{"class":55},[45,76356,1883],{"class":59},[14,76358,76359],{},"Comparison:",[36,76361,76363],{"className":38,"code":76362,"language":40,"meta":41,"style":41},"print(age == 20)\n",[18,76364,76365],{"__ignoreMap":41},[45,76366,76367,76369,76371,76373,76375,76377],{"class":47,"line":48},[45,76368,2065],{"class":92},[45,76370,96],{"class":85},[45,76372,52],{"class":2070},[45,76374,1129],{"class":55},[45,76376,10503],{"class":59},[45,76378,109],{"class":85},[14,76380,17038],{},[36,76382,76383],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,76384,76385],{"__ignoreMap":41},[45,76386,76387],{"class":47,"line":48},[45,76388,2270],{"class":481},[712,76390,76392],{"id":76391},"using-a-variable-before-assigning-a-value","Using a variable before assigning a value",[14,76394,64612],{},[36,76396,76397],{"className":38,"code":46532,"language":40,"meta":41,"style":41},[18,76398,76399],{"__ignoreMap":41},[45,76400,76401,76403,76405,76407],{"class":47,"line":48},[45,76402,2065],{"class":92},[45,76404,96],{"class":85},[45,76406,46543],{"class":2070},[45,76408,109],{"class":85},[14,76410,3259,76411,76413,76414,240],{},[18,76412,46543],{}," was never created, Python will raise a ",[18,76415,35168],{},[14,76417,76418],{},"Fix it by assigning a value first:",[36,76420,76422],{"className":38,"code":76421,"language":40,"meta":41,"style":41},"score = 10\nprint(score)\n",[18,76423,76424,76432],{"__ignoreMap":41},[45,76425,76426,76428,76430],{"class":47,"line":48},[45,76427,581],{"class":51},[45,76429,56],{"class":55},[45,76431,1174],{"class":59},[45,76433,76434,76436,76438,76440],{"class":47,"line":63},[45,76435,2065],{"class":92},[45,76437,96],{"class":85},[45,76439,46543],{"class":2070},[45,76441,109],{"class":85},[14,76443,76444,76445,240],{},"If you see this error often, read ",[236,76446,76448],{"href":76447},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix\u002F","NameError: name is not defined fix",[712,76450,76452],{"id":76451},"writing-a-keyword-as-a-variable-name","Writing a keyword as a variable name",[14,76454,75372],{},[36,76456,76458],{"className":38,"code":76457,"language":40,"meta":41,"style":41},"# if = 5\n",[18,76459,76460],{"__ignoreMap":41},[45,76461,76462],{"class":47,"line":48},[45,76463,76457],{"class":7624},[14,76465,76466],{},"Choose another name:",[36,76468,76470],{"className":38,"code":76469,"language":40,"meta":41,"style":41},"if_count = 5\n",[18,76471,76472],{"__ignoreMap":41},[45,76473,76474,76477,76479],{"class":47,"line":48},[45,76475,76476],{"class":51},"if_count ",[45,76478,56],{"class":55},[45,76480,733],{"class":59},[31,76482,47394],{"id":47393},[14,76484,76485],{},"Once you understand variables, the next step is learning what kinds of values they can store and how those values behave.",[14,76487,59757],{},[195,76489,76490,76494,76500,76506],{},[198,76491,76492],{},[236,76493,35582],{"href":35108},[198,76495,76496],{},[236,76497,18104,76498,19075],{"href":18110},[18,76499,15833],{},[198,76501,76502],{},[236,76503,18104,76504,19075],{"href":18103},[18,76505,18020],{},[198,76507,76508],{},[236,76509,42104],{"href":42103},[31,76511,2281],{"id":2280},[712,76513,76515],{"id":76514},"do-i-need-to-declare-variables-in-python-first","Do I need to declare variables in Python first?",[14,76517,76518],{},"No. In Python, a variable is created when you assign a value to it.",[712,76520,76522],{"id":76521},"can-a-variable-change-to-a-different-type","Can a variable change to a different type?",[14,76524,76525],{},"Yes. Python lets you assign a new value of a different type to the same variable.",[36,76527,76529],{"className":38,"code":76528,"language":40,"meta":41,"style":41},"value = 10\nprint(type(value))\n\nvalue = \"ten\"\nprint(type(value))\n",[18,76530,76531,76539,76553,76557,76570],{"__ignoreMap":41},[45,76532,76533,76535,76537],{"class":47,"line":48},[45,76534,1476],{"class":51},[45,76536,56],{"class":55},[45,76538,1174],{"class":59},[45,76540,76541,76543,76545,76547,76549,76551],{"class":47,"line":63},[45,76542,2065],{"class":92},[45,76544,96],{"class":85},[45,76546,4340],{"class":2109},[45,76548,96],{"class":85},[45,76550,12722],{"class":2070},[45,76552,4348],{"class":85},[45,76554,76555],{"class":47,"line":70},[45,76556,67],{"emptyLinePlaceholder":66},[45,76558,76559,76561,76563,76565,76568],{"class":47,"line":89},[45,76560,1476],{"class":51},[45,76562,56],{"class":55},[45,76564,1565],{"class":99},[45,76566,76567],{"class":103},"ten",[45,76569,1571],{"class":99},[45,76571,76572,76574,76576,76578,76580,76582],{"class":47,"line":112},[45,76573,2065],{"class":92},[45,76575,96],{"class":85},[45,76577,4340],{"class":2109},[45,76579,96],{"class":85},[45,76581,12722],{"class":2070},[45,76583,4348],{"class":85},[14,76585,17038],{},[36,76587,76589],{"className":38,"code":76588,"language":40,"meta":41,"style":41},"\u003Cclass 'int'>\n\u003Cclass 'str'>\n",[18,76590,76591,76605],{"__ignoreMap":41},[45,76592,76593,76595,76597,76599,76601,76603],{"class":47,"line":48},[45,76594,79],{"class":55},[45,76596,2441],{"class":2440},[45,76598,9199],{"class":99},[45,76600,29738],{"class":103},[45,76602,7901],{"class":99},[45,76604,13484],{"class":55},[45,76606,76607,76609,76611,76613,76615,76617],{"class":47,"line":63},[45,76608,79],{"class":55},[45,76610,2441],{"class":2440},[45,76612,9199],{"class":99},[45,76614,29733],{"class":103},[45,76616,7901],{"class":99},[45,76618,13484],{"class":55},[712,76620,16362,76622,1316,76624,76626],{"id":76621},"what-is-the-difference-between-and-in-python",[18,76623,56],{},[18,76625,1129],{}," in Python?",[14,76628,76629,76631],{},[18,76630,56],{}," assigns a value to a variable.",[14,76633,76634,76636],{},[18,76635,1129],{}," compares two values to check if they are equal.",[36,76638,76640],{"className":38,"code":76639,"language":40,"meta":41,"style":41},"x = 5\nprint(x == 5)\n",[18,76641,76642,76650],{"__ignoreMap":41},[45,76643,76644,76646,76648],{"class":47,"line":48},[45,76645,1169],{"class":51},[45,76647,56],{"class":55},[45,76649,733],{"class":59},[45,76651,76652,76654,76656,76658,76660,76662],{"class":47,"line":63},[45,76653,2065],{"class":92},[45,76655,96],{"class":85},[45,76657,1169],{"class":2070},[45,76659,1129],{"class":55},[45,76661,1222],{"class":59},[45,76663,109],{"class":85},[14,76665,17038],{},[36,76667,76668],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,76669,76670],{"__ignoreMap":41},[45,76671,76672],{"class":47,"line":48},[45,76673,2270],{"class":481},[712,76675,76677],{"id":76676},"why-does-python-say-a-name-is-not-defined","Why does Python say a name is not defined?",[14,76679,76680],{},"Usually because:",[195,76682,76683,76686,76689],{},[198,76684,76685],{},"the variable was used before it was assigned",[198,76687,76688],{},"the name was misspelled",[198,76690,76691,76692,76694,76695],{},"the capitalization was different, such as ",[18,76693,2082],{}," vs ",[18,76696,75445],{},[14,76698,14549],{},[36,76700,76702],{"className":38,"code":76701,"language":40,"meta":41,"style":41},"print(my_variable)\nprint(type(my_variable))\ndir()\nhelp(type)\n",[18,76703,76704,76714,76728,76734],{"__ignoreMap":41},[45,76705,76706,76708,76710,76712],{"class":47,"line":48},[45,76707,2065],{"class":92},[45,76709,96],{"class":85},[45,76711,59457],{"class":2070},[45,76713,109],{"class":85},[45,76715,76716,76718,76720,76722,76724,76726],{"class":47,"line":63},[45,76717,2065],{"class":92},[45,76719,96],{"class":85},[45,76721,4340],{"class":2109},[45,76723,96],{"class":85},[45,76725,59457],{"class":2070},[45,76727,4348],{"class":85},[45,76729,76730,76732],{"class":47,"line":70},[45,76731,4357],{"class":92},[45,76733,2495],{"class":85},[45,76735,76736,76738,76740,76742],{"class":47,"line":89},[45,76737,2104],{"class":92},[45,76739,96],{"class":85},[45,76741,4340],{"class":2109},[45,76743,109],{"class":85},[31,76745,2342],{"id":2341},[195,76747,76748,76753,76757,76761,76767,76773,76777],{},[198,76749,76750],{},[236,76751,76752],{"href":74856},"What is a variable in Python",[198,76754,76755],{},[236,76756,35582],{"href":35108},[198,76758,76759],{},[236,76760,40356],{"href":39615},[198,76762,76763],{},[236,76764,18104,76765,19075],{"href":18110},[18,76766,15833],{},[198,76768,76769],{},[236,76770,18104,76771,19075],{"href":18103},[18,76772,18020],{},[198,76774,76775],{},[236,76776,76448],{"href":76447},[198,76778,76779],{},[236,76780,42504],{"href":42103},[2367,76782,76783],{},"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 .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}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":41,"searchDepth":63,"depth":63,"links":76785},[76786,76787,76788,76789,76795,76799,76800,76801,76802,76809,76817,76818,76825],{"id":33,"depth":63,"text":34},{"id":74753,"depth":63,"text":74754},{"id":74860,"depth":63,"text":74861},{"id":75005,"depth":63,"text":75006,"children":76790},[76791,76792,76793,76794],{"id":75032,"depth":70,"text":75033},{"id":75080,"depth":70,"text":75081},{"id":75166,"depth":70,"text":75167},{"id":75232,"depth":70,"text":75233},{"id":75296,"depth":63,"text":75297,"children":76796},[76797,76798],{"id":75303,"depth":70,"text":75304},{"id":75363,"depth":70,"text":75364},{"id":75536,"depth":63,"text":75537},{"id":75697,"depth":63,"text":75698},{"id":75785,"depth":63,"text":75786},{"id":75980,"depth":63,"text":75981,"children":76803},[76804,76805,76806,76808],{"id":75987,"depth":70,"text":75988},{"id":76035,"depth":70,"text":76036},{"id":76096,"depth":70,"text":76807},"Use variables with print()",{"id":76195,"depth":70,"text":76196},{"id":1336,"depth":63,"text":1337,"children":76810},[76811,76812,76813,76815,76816],{"id":76259,"depth":70,"text":76260},{"id":76295,"depth":70,"text":76296},{"id":76326,"depth":70,"text":76814},"Confusing = with ==",{"id":76391,"depth":70,"text":76392},{"id":76451,"depth":70,"text":76452},{"id":47393,"depth":63,"text":47394},{"id":2280,"depth":63,"text":2281,"children":76819},[76820,76821,76822,76824],{"id":76514,"depth":70,"text":76515},{"id":76521,"depth":70,"text":76522},{"id":76621,"depth":70,"text":76823},"What is the difference between = and == in Python?",{"id":76676,"depth":70,"text":76677},{"id":2341,"depth":63,"text":2342},"Master python variables explained for beginners in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fpython-variables-explained-for-beginners",{"title":74671,"description":76826},"learn\u002Fpython-variables-explained-for-beginners","Lz2fOLWaUrpHlI_zsKZkiqimqfJyOgxWTlqVh4N6xUQ",{"id":76833,"title":76834,"body":76835,"description":78603,"extension":2399,"meta":78604,"navigation":66,"path":65330,"seo":78605,"stem":78606,"__hash__":78607},"content\u002Flearn\u002Fpython-while-loops-explained.md","Python while Loops Explained",{"type":7,"value":76836,"toc":78573},[76837,76840,76847,76853,76856,76899,76901,76906,76914,76918,76923,76941,76943,76951,76960,76965,76969,76974,76994,76997,77015,77017,77064,77066,77071,77078,77082,77088,77106,77108,77183,77185,77191,77194,77240,77244,77249,77296,77298,77303,77306,77325,77334,77338,77343,77348,77359,77364,77375,77380,77451,77456,77461,77491,77496,77500,77505,77508,77547,77556,77559,77606,77609,77627,77630,77674,77677,77699,77701,77706,77711,77713,77776,77778,77783,77792,77797,77802,77804,77866,77868,77873,77884,77893,77897,77903,77910,77915,77983,77985,77991,77995,78078,78080,78085,78093,78095,78101,78105,78107,78145,78148,78160,78163,78209,78217,78219,78258,78267,78269,78312,78316,78318,78357,78360,78362,78400,78405,78409,78411,78439,78445,78448,78494,78496,78500,78507,78511,78514,78518,78524,78526,78534,78538,78542,78544,78570],[10,76838,76834],{"id":76839},"python-while-loops-explained",[14,76841,2419,76842,76844,76845,240],{},[18,76843,23529],{}," loop lets you repeat code as long as a condition stays ",[18,76846,229],{},[14,76848,76849,76850,76852],{},"This is useful when you do not know in advance how many times the loop should run. Python checks the condition before each run of the loop. When the condition becomes ",[18,76851,233],{},", the loop stops.",[14,76854,76855],{},"Here is a quick example:",[36,76857,76859],{"className":38,"code":76858,"language":40,"meta":41,"style":41},"count = 1\nwhile count \u003C= 3:\n    print(count)\n    count += 1\n",[18,76860,76861,76869,76881,76891],{"__ignoreMap":41},[45,76862,76863,76865,76867],{"class":47,"line":48},[45,76864,33984],{"class":51},[45,76866,56],{"class":55},[45,76868,12962],{"class":59},[45,76870,76871,76873,76875,76877,76879],{"class":47,"line":63},[45,76872,23529],{"class":73},[45,76874,33999],{"class":51},[45,76876,1156],{"class":55},[45,76878,777],{"class":59},[45,76880,86],{"class":85},[45,76882,76883,76885,76887,76889],{"class":47,"line":70},[45,76884,93],{"class":92},[45,76886,96],{"class":85},[45,76888,34014],{"class":2070},[45,76890,109],{"class":85},[45,76892,76893,76895,76897],{"class":47,"line":89},[45,76894,34021],{"class":51},[45,76896,12992],{"class":55},[45,76898,12962],{"class":59},[14,76900,177],{},[36,76902,76904],{"className":76903,"code":13029,"language":5000,"meta":41},[4998],[18,76905,13029],{"__ignoreMap":41},[14,76907,19194,76908,76910,76911,76913],{},[18,76909,23529],{}," loop when you want code to repeat as long as a condition stays ",[18,76912,229],{},". Make sure something inside the loop changes, or the loop may never end.",[31,76915,76917],{"id":76916},"what-a-while-loop-does","What a while loop does",[14,76919,2419,76920,76922],{},[18,76921,23529],{}," loop:",[195,76924,76925,76930,76933,76938],{},[198,76926,76927,76928],{},"Repeats a block of code while a condition is ",[18,76929,229],{},[198,76931,76932],{},"Checks the condition before each loop run",[198,76934,76935,76936],{},"Stops when the condition becomes ",[18,76937,233],{},[198,76939,76940],{},"Is useful when you do not know the exact number of repeats in advance",[14,76942,8675],{},[195,76944,76945,76948],{},[198,76946,76947],{},"“While this condition is true, keep going.”",[198,76949,76950],{},"“When it becomes false, stop.”",[14,76952,76953,76954,76956,76957,76959],{},"For example, if you want to keep asking for input until the user types ",[18,76955,51119],{},", a ",[18,76958,23529],{}," loop is a good fit.",[14,76961,76962,76963,240],{},"If you are still getting comfortable with conditions, see ",[236,76964,549],{"href":548},[31,76966,76968],{"id":76967},"basic-while-loop-syntax","Basic while loop syntax",[14,76970,2419,76971,76973],{},[18,76972,23529],{}," loop has four important parts:",[195,76975,76976,76981,76986,76991],{},[198,76977,76978,76979],{},"It starts with the keyword ",[18,76980,23529],{},[198,76982,76983,76984],{},"It needs a condition after ",[18,76985,23529],{},[198,76987,76988,76989],{},"The first line ends with a colon ",[18,76990,457],{},[198,76992,76993],{},"The loop body must be indented",[14,76995,76996],{},"Basic form:",[36,76998,77000],{"className":38,"code":76999,"language":40,"meta":41,"style":41},"while condition:\n    # code to repeat\n",[18,77001,77002,77010],{"__ignoreMap":41},[45,77003,77004,77006,77008],{"class":47,"line":48},[45,77005,23529],{"class":73},[45,77007,408],{"class":51},[45,77009,86],{"class":85},[45,77011,77012],{"class":47,"line":63},[45,77013,77014],{"class":7624},"    # code to repeat\n",[14,77016,466],{},[36,77018,77020],{"className":38,"code":77019,"language":40,"meta":41,"style":41},"number = 1\n\nwhile number \u003C 4:\n    print(number)\n    number += 1\n",[18,77021,77022,77030,77034,77046,77056],{"__ignoreMap":41},[45,77023,77024,77026,77028],{"class":47,"line":48},[45,77025,728],{"class":51},[45,77027,56],{"class":55},[45,77029,12962],{"class":59},[45,77031,77032],{"class":47,"line":63},[45,77033,67],{"emptyLinePlaceholder":66},[45,77035,77036,77038,77040,77042,77044],{"class":47,"line":70},[45,77037,23529],{"class":73},[45,77039,744],{"class":51},[45,77041,79],{"class":55},[45,77043,8591],{"class":59},[45,77045,86],{"class":85},[45,77047,77048,77050,77052,77054],{"class":47,"line":89},[45,77049,93],{"class":92},[45,77051,96],{"class":85},[45,77053,13546],{"class":2070},[45,77055,109],{"class":85},[45,77057,77058,77060,77062],{"class":47,"line":112},[45,77059,12957],{"class":51},[45,77061,12992],{"class":55},[45,77063,12962],{"class":59},[14,77065,177],{},[36,77067,77069],{"className":77068,"code":13029,"language":5000,"meta":41},[4998],[18,77070,13029],{"__ignoreMap":41},[14,77072,77073,77074,1316,77076,240],{},"If you forget the colon, Python raises a syntax error. If you forget indentation, Python raises an indentation error. You can learn more in ",[236,77075,1541],{"href":1540},[236,77077,1614],{"href":1613},[31,77079,77081],{"id":77080},"how-execution-works-step-by-step","How execution works step by step",[14,77083,77084,77085,77087],{},"Python runs a ",[18,77086,23529],{}," loop in this order:",[346,77089,77090,77093,77098,77101],{},[198,77091,77092],{},"Python checks the condition.",[198,77094,55458,77095,77097],{},[18,77096,229],{},", it runs the indented code block.",[198,77099,77100],{},"When that block finishes, Python checks the condition again.",[198,77102,77103,77104,240],{},"This repeats until the condition is ",[18,77105,233],{},[14,77107,718],{},[36,77109,77111],{"className":38,"code":77110,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 3:\n    print(\"Count is\", count)\n    count += 1\n\nprint(\"Loop finished\")\n",[18,77112,77113,77121,77125,77137,77157,77165,77169],{"__ignoreMap":41},[45,77114,77115,77117,77119],{"class":47,"line":48},[45,77116,33984],{"class":51},[45,77118,56],{"class":55},[45,77120,12962],{"class":59},[45,77122,77123],{"class":47,"line":63},[45,77124,67],{"emptyLinePlaceholder":66},[45,77126,77127,77129,77131,77133,77135],{"class":47,"line":70},[45,77128,23529],{"class":73},[45,77130,33999],{"class":51},[45,77132,1156],{"class":55},[45,77134,777],{"class":59},[45,77136,86],{"class":85},[45,77138,77139,77141,77143,77145,77148,77150,77152,77155],{"class":47,"line":89},[45,77140,93],{"class":92},[45,77142,96],{"class":85},[45,77144,100],{"class":99},[45,77146,77147],{"class":103},"Count is",[45,77149,100],{"class":99},[45,77151,3079],{"class":85},[45,77153,77154],{"class":2070}," count",[45,77156,109],{"class":85},[45,77158,77159,77161,77163],{"class":47,"line":112},[45,77160,34021],{"class":51},[45,77162,12992],{"class":55},[45,77164,12962],{"class":59},[45,77166,77167],{"class":47,"line":126},[45,77168,67],{"emptyLinePlaceholder":66},[45,77170,77171,77173,77175,77177,77179,77181],{"class":47,"line":142},[45,77172,2065],{"class":92},[45,77174,96],{"class":85},[45,77176,100],{"class":99},[45,77178,50521],{"class":103},[45,77180,100],{"class":99},[45,77182,109],{"class":85},[14,77184,177],{},[36,77186,77189],{"className":77187,"code":77188,"language":5000,"meta":41},[4998],"Count is 1\nCount is 2\nCount is 3\nLoop finished\n",[18,77190,77188],{"__ignoreMap":41},[14,77192,77193],{},"What happens step by step:",[195,77195,77196,77203,77214,77220,77228,77237],{},[198,77197,77198,77200,77201],{},[18,77199,34014],{}," starts at ",[18,77202,8578],{},[198,77204,77205,36998,77208,77210,77211],{},[18,77206,77207],{},"count \u003C= 3",[18,77209,229],{},", so Python prints ",[18,77212,77213],{},"Count is 1",[198,77215,77216,15579,77218],{},[18,77217,34014],{},[18,77219,9367],{},[198,77221,77222,77224,77225,77227],{},[18,77223,77207],{}," is still ",[18,77226,229],{},", so Python runs the loop again",[198,77229,57736,77230,15579,77232,77234,77235],{},[18,77231,34014],{},[18,77233,3584],{},", the condition is ",[18,77236,233],{},[198,77238,77239],{},"Python exits the loop and moves to the next line",[31,77241,77243],{"id":77242},"simple-counting-example","Simple counting example",[14,77245,77246,77247,240],{},"A counting loop is one of the easiest ways to understand ",[18,77248,23529],{},[36,77250,77252],{"className":38,"code":77251,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 5:\n    print(count)\n    count += 1\n",[18,77253,77254,77262,77266,77278,77288],{"__ignoreMap":41},[45,77255,77256,77258,77260],{"class":47,"line":48},[45,77257,33984],{"class":51},[45,77259,56],{"class":55},[45,77261,12962],{"class":59},[45,77263,77264],{"class":47,"line":63},[45,77265,67],{"emptyLinePlaceholder":66},[45,77267,77268,77270,77272,77274,77276],{"class":47,"line":70},[45,77269,23529],{"class":73},[45,77271,33999],{"class":51},[45,77273,1156],{"class":55},[45,77275,1222],{"class":59},[45,77277,86],{"class":85},[45,77279,77280,77282,77284,77286],{"class":47,"line":89},[45,77281,93],{"class":92},[45,77283,96],{"class":85},[45,77285,34014],{"class":2070},[45,77287,109],{"class":85},[45,77289,77290,77292,77294],{"class":47,"line":112},[45,77291,34021],{"class":51},[45,77293,12992],{"class":55},[45,77295,12962],{"class":59},[14,77297,177],{},[36,77299,77301],{"className":77300,"code":13662,"language":5000,"meta":41},[4998],[18,77302,13662],{"__ignoreMap":41},[14,77304,77305],{},"Important parts:",[195,77307,77308,77314,77320],{},[198,77309,77310,77313],{},[18,77311,77312],{},"count = 1"," creates the variable before the loop",[198,77315,77316,77319],{},[18,77317,77318],{},"count \u003C= 5"," is the condition",[198,77321,77322,77324],{},[18,77323,36632],{}," updates the variable inside the loop",[14,77326,77327,77328,77330,77331,77333],{},"That last line is very important. Without it, ",[18,77329,34014],{}," would stay ",[18,77332,8578],{},", and the loop would never stop.",[31,77335,77337],{"id":77336},"when-to-use-while-instead-of-for","When to use while instead of for",[14,77339,19194,77340,77342],{},[18,77341,23529],{}," loop when repetition depends on a changing condition.",[14,77344,77345,77346,457],{},"Good uses for ",[18,77347,23529],{},[195,77349,77350,77353,77356],{},[198,77351,77352],{},"Repeating until the user chooses to stop",[198,77354,77355],{},"Running code until a value reaches a target",[198,77357,77358],{},"Waiting for a condition to change",[14,77360,19194,77361,77363],{},[18,77362,7895],{}," loop when you are going through a sequence such as:",[195,77365,77366,77369,77372],{},[198,77367,77368],{},"A list",[198,77370,77371],{},"A string",[198,77373,77374],{},"A range of numbers",[14,77376,26234,77377,77379],{},[18,77378,23529],{}," makes sense:",[36,77381,77383],{"className":38,"code":77382,"language":40,"meta":41,"style":41},"password = \"\"\n\nwhile password != \"python123\":\n    password = input(\"Enter the password: \")\n\nprint(\"Access granted\")\n",[18,77384,77385,77393,77397,77413,77433,77437],{"__ignoreMap":41},[45,77386,77387,77389,77391],{"class":47,"line":48},[45,77388,55958],{"class":51},[45,77390,56],{"class":55},[45,77392,56021],{"class":99},[45,77394,77395],{"class":47,"line":63},[45,77396,67],{"emptyLinePlaceholder":66},[45,77398,77399,77401,77403,77405,77407,77409,77411],{"class":47,"line":70},[45,77400,23529],{"class":73},[45,77402,55978],{"class":51},[45,77404,1135],{"class":55},[45,77406,1565],{"class":99},[45,77408,55965],{"class":103},[45,77410,100],{"class":99},[45,77412,86],{"class":85},[45,77414,77415,77418,77420,77422,77424,77426,77429,77431],{"class":47,"line":89},[45,77416,77417],{"class":51},"    password ",[45,77419,56],{"class":55},[45,77421,18043],{"class":92},[45,77423,96],{"class":85},[45,77425,100],{"class":99},[45,77427,77428],{"class":103},"Enter the password: ",[45,77430,100],{"class":99},[45,77432,109],{"class":85},[45,77434,77435],{"class":47,"line":112},[45,77436,67],{"emptyLinePlaceholder":66},[45,77438,77439,77441,77443,77445,77447,77449],{"class":47,"line":126},[45,77440,2065],{"class":92},[45,77442,96],{"class":85},[45,77444,100],{"class":99},[45,77446,55999],{"class":103},[45,77448,100],{"class":99},[45,77450,109],{"class":85},[14,77452,77453,77454,240],{},"The loop keeps running until the condition becomes ",[18,77455,233],{},[14,77457,26234,77458,77460],{},[18,77459,7895],{}," is usually better:",[36,77462,77463],{"className":38,"code":23724,"language":40,"meta":41,"style":41},[18,77464,77465,77481],{"__ignoreMap":41},[45,77466,77467,77469,77471,77473,77475,77477,77479],{"class":47,"line":48},[45,77468,7895],{"class":73},[45,77470,23733],{"class":51},[45,77472,8623],{"class":73},[45,77474,1565],{"class":99},[45,77476,7035],{"class":103},[45,77478,100],{"class":99},[45,77480,86],{"class":85},[45,77482,77483,77485,77487,77489],{"class":47,"line":63},[45,77484,93],{"class":92},[45,77486,96],{"class":85},[45,77488,23752],{"class":2070},[45,77490,109],{"class":85},[14,77492,77493,77494,240],{},"If you are comparing loop types, see ",[236,77495,23611],{"href":23610},[31,77497,77499],{"id":77498},"avoiding-infinite-loops","Avoiding infinite loops",[14,77501,77502,77503,240],{},"An infinite loop is a loop that never ends because its condition never becomes ",[18,77504,233],{},[14,77506,77507],{},"Example of an infinite loop:",[36,77509,77511],{"className":38,"code":77510,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 3:\n    print(count)\n",[18,77512,77513,77521,77525,77537],{"__ignoreMap":41},[45,77514,77515,77517,77519],{"class":47,"line":48},[45,77516,33984],{"class":51},[45,77518,56],{"class":55},[45,77520,12962],{"class":59},[45,77522,77523],{"class":47,"line":63},[45,77524,67],{"emptyLinePlaceholder":66},[45,77526,77527,77529,77531,77533,77535],{"class":47,"line":70},[45,77528,23529],{"class":73},[45,77530,33999],{"class":51},[45,77532,1156],{"class":55},[45,77534,777],{"class":59},[45,77536,86],{"class":85},[45,77538,77539,77541,77543,77545],{"class":47,"line":89},[45,77540,93],{"class":92},[45,77542,96],{"class":85},[45,77544,34014],{"class":2070},[45,77546,109],{"class":85},[14,77548,77549,77550,77552,77553,77555],{},"This keeps printing ",[18,77551,8578],{}," forever because ",[18,77554,34014],{}," never changes.",[14,77557,77558],{},"A fixed version:",[36,77560,77562],{"className":38,"code":77561,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 3:\n    print(count)\n    count += 1\n",[18,77563,77564,77572,77576,77588,77598],{"__ignoreMap":41},[45,77565,77566,77568,77570],{"class":47,"line":48},[45,77567,33984],{"class":51},[45,77569,56],{"class":55},[45,77571,12962],{"class":59},[45,77573,77574],{"class":47,"line":63},[45,77575,67],{"emptyLinePlaceholder":66},[45,77577,77578,77580,77582,77584,77586],{"class":47,"line":70},[45,77579,23529],{"class":73},[45,77581,33999],{"class":51},[45,77583,1156],{"class":55},[45,77585,777],{"class":59},[45,77587,86],{"class":85},[45,77589,77590,77592,77594,77596],{"class":47,"line":89},[45,77591,93],{"class":92},[45,77593,96],{"class":85},[45,77595,34014],{"class":2070},[45,77597,109],{"class":85},[45,77599,77600,77602,77604],{"class":47,"line":112},[45,77601,34021],{"class":51},[45,77603,12992],{"class":55},[45,77605,12962],{"class":59},[14,77607,77608],{},"To avoid infinite loops:",[195,77610,77611,77614,77619,77622],{},[198,77612,77613],{},"Make sure the loop variable changes",[198,77615,77616,77617],{},"Check that the condition can become ",[18,77618,233],{},[198,77620,77621],{},"Test with small examples first",[198,77623,165,77624,77626],{},[18,77625,15833],{}," statements if you need to see what is happening",[14,77628,77629],{},"Helpful debugging lines:",[36,77631,77633],{"className":38,"code":77632,"language":40,"meta":41,"style":41},"print(count)\nprint(\"condition still true\")\nhelp(\"while\")\n",[18,77634,77635,77645,77660],{"__ignoreMap":41},[45,77636,77637,77639,77641,77643],{"class":47,"line":48},[45,77638,2065],{"class":92},[45,77640,96],{"class":85},[45,77642,34014],{"class":2070},[45,77644,109],{"class":85},[45,77646,77647,77649,77651,77653,77656,77658],{"class":47,"line":63},[45,77648,2065],{"class":92},[45,77650,96],{"class":85},[45,77652,100],{"class":99},[45,77654,77655],{"class":103},"condition still true",[45,77657,100],{"class":99},[45,77659,109],{"class":85},[45,77661,77662,77664,77666,77668,77670,77672],{"class":47,"line":70},[45,77663,2104],{"class":92},[45,77665,96],{"class":85},[45,77667,100],{"class":99},[45,77669,23529],{"class":103},[45,77671,100],{"class":99},[45,77673,109],{"class":85},[14,77675,77676],{},"Common causes of loop problems:",[195,77678,77679,77682,77687,77692],{},[198,77680,77681],{},"Loop variable never changes",[198,77683,77684,77685],{},"Condition is always ",[18,77686,229],{},[198,77688,77689,77690],{},"Missing indentation after ",[18,77691,23529],{},[198,77693,1350,77694,77696,77697],{},[18,77695,23529],{}," for a task better suited to ",[18,77698,7895],{},[31,77700,36431],{"id":36430},[712,77702,1350,77704],{"id":77703},"using-break",[18,77705,35658],{},[14,77707,77708,77710],{},[18,77709,35658],{}," stops the loop immediately.",[14,77712,466],{},[36,77714,77716],{"className":38,"code":77715,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 5:\n    if count == 3:\n        break\n    print(count)\n    count += 1\n",[18,77717,77718,77726,77730,77742,77754,77758,77768],{"__ignoreMap":41},[45,77719,77720,77722,77724],{"class":47,"line":48},[45,77721,33984],{"class":51},[45,77723,56],{"class":55},[45,77725,12962],{"class":59},[45,77727,77728],{"class":47,"line":63},[45,77729,67],{"emptyLinePlaceholder":66},[45,77731,77732,77734,77736,77738,77740],{"class":47,"line":70},[45,77733,23529],{"class":73},[45,77735,33999],{"class":51},[45,77737,1156],{"class":55},[45,77739,1222],{"class":59},[45,77741,86],{"class":85},[45,77743,77744,77746,77748,77750,77752],{"class":47,"line":89},[45,77745,1913],{"class":73},[45,77747,33999],{"class":51},[45,77749,1129],{"class":55},[45,77751,777],{"class":59},[45,77753,86],{"class":85},[45,77755,77756],{"class":47,"line":112},[45,77757,23579],{"class":73},[45,77759,77760,77762,77764,77766],{"class":47,"line":126},[45,77761,93],{"class":92},[45,77763,96],{"class":85},[45,77765,34014],{"class":2070},[45,77767,109],{"class":85},[45,77769,77770,77772,77774],{"class":47,"line":142},[45,77771,34021],{"class":51},[45,77773,12992],{"class":55},[45,77775,12962],{"class":59},[14,77777,177],{},[36,77779,77781],{"className":77780,"code":36722,"language":5000,"meta":41},[4998],[18,77782,36722],{"__ignoreMap":41},[14,77784,36177,77785,15579,77787,21,77789,77791],{},[18,77786,34014],{},[18,77788,11184],{},[18,77790,35658],{}," ends the loop right away.",[712,77793,1350,77795],{"id":77794},"using-continue",[18,77796,35661],{},[14,77798,77799,77801],{},[18,77800,35661],{}," skips the rest of the current loop run and moves to the next check.",[14,77803,466],{},[36,77805,77806],{"className":38,"code":36543,"language":40,"meta":41,"style":41},[18,77807,77808,77816,77820,77832,77840,77852,77856],{"__ignoreMap":41},[45,77809,77810,77812,77814],{"class":47,"line":48},[45,77811,33984],{"class":51},[45,77813,56],{"class":55},[45,77815,34967],{"class":59},[45,77817,77818],{"class":47,"line":63},[45,77819,67],{"emptyLinePlaceholder":66},[45,77821,77822,77824,77826,77828,77830],{"class":47,"line":70},[45,77823,23529],{"class":73},[45,77825,33999],{"class":51},[45,77827,79],{"class":55},[45,77829,1222],{"class":59},[45,77831,86],{"class":85},[45,77833,77834,77836,77838],{"class":47,"line":89},[45,77835,34021],{"class":51},[45,77837,12992],{"class":55},[45,77839,12962],{"class":59},[45,77841,77842,77844,77846,77848,77850],{"class":47,"line":112},[45,77843,1913],{"class":73},[45,77845,33999],{"class":51},[45,77847,1129],{"class":55},[45,77849,777],{"class":59},[45,77851,86],{"class":85},[45,77853,77854],{"class":47,"line":126},[45,77855,35727],{"class":73},[45,77857,77858,77860,77862,77864],{"class":47,"line":142},[45,77859,93],{"class":92},[45,77861,96],{"class":85},[45,77863,34014],{"class":2070},[45,77865,109],{"class":85},[14,77867,177],{},[36,77869,77871],{"className":77870,"code":36152,"language":5000,"meta":41},[4998],[18,77872,36152],{"__ignoreMap":41},[14,77874,36177,77875,36998,77877,77879,77880,77883],{},[18,77876,34014],{},[18,77878,11184],{},", Python skips ",[18,77881,77882],{},"print(count)"," for that loop run.",[14,77885,165,77886,1316,77888,77890,77891,240],{},[18,77887,35658],{},[18,77889,35661],{}," only when they make the loop easier to understand. For more examples, see ",[236,77892,50457],{"href":50456},[31,77894,77896],{"id":77895},"the-optional-while-else-block","The optional while-else block",[14,77898,2419,77899,50466,77901,1864],{},[18,77900,23529],{},[18,77902,28],{},[14,77904,1732,77905,77907,77908,240],{},[18,77906,28],{}," runs if the loop ends normally. It does not run if the loop stops because of ",[18,77909,35658],{},[14,77911,77912,77913,457],{},"Example without ",[18,77914,35658],{},[36,77916,77918],{"className":38,"code":77917,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 3:\n    print(count)\n    count += 1\nelse:\n    print(\"Loop ended normally\")\n",[18,77919,77920,77928,77932,77944,77954,77962,77968],{"__ignoreMap":41},[45,77921,77922,77924,77926],{"class":47,"line":48},[45,77923,33984],{"class":51},[45,77925,56],{"class":55},[45,77927,12962],{"class":59},[45,77929,77930],{"class":47,"line":63},[45,77931,67],{"emptyLinePlaceholder":66},[45,77933,77934,77936,77938,77940,77942],{"class":47,"line":70},[45,77935,23529],{"class":73},[45,77937,33999],{"class":51},[45,77939,1156],{"class":55},[45,77941,777],{"class":59},[45,77943,86],{"class":85},[45,77945,77946,77948,77950,77952],{"class":47,"line":89},[45,77947,93],{"class":92},[45,77949,96],{"class":85},[45,77951,34014],{"class":2070},[45,77953,109],{"class":85},[45,77955,77956,77958,77960],{"class":47,"line":112},[45,77957,34021],{"class":51},[45,77959,12992],{"class":55},[45,77961,12962],{"class":59},[45,77963,77964,77966],{"class":47,"line":126},[45,77965,28],{"class":73},[45,77967,86],{"class":85},[45,77969,77970,77972,77974,77976,77979,77981],{"class":47,"line":142},[45,77971,93],{"class":92},[45,77973,96],{"class":85},[45,77975,100],{"class":99},[45,77977,77978],{"class":103},"Loop ended normally",[45,77980,100],{"class":99},[45,77982,109],{"class":85},[14,77984,177],{},[36,77986,77989],{"className":77987,"code":77988,"language":5000,"meta":41},[4998],"1\n2\n3\nLoop ended normally\n",[18,77990,77988],{"__ignoreMap":41},[14,77992,18017,77993,457],{},[18,77994,35658],{},[36,77996,77998],{"className":38,"code":77997,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 3:\n    if count == 2:\n        break\n    print(count)\n    count += 1\nelse:\n    print(\"Loop ended normally\")\n",[18,77999,78000,78008,78012,78024,78036,78040,78050,78058,78064],{"__ignoreMap":41},[45,78001,78002,78004,78006],{"class":47,"line":48},[45,78003,33984],{"class":51},[45,78005,56],{"class":55},[45,78007,12962],{"class":59},[45,78009,78010],{"class":47,"line":63},[45,78011,67],{"emptyLinePlaceholder":66},[45,78013,78014,78016,78018,78020,78022],{"class":47,"line":70},[45,78015,23529],{"class":73},[45,78017,33999],{"class":51},[45,78019,1156],{"class":55},[45,78021,777],{"class":59},[45,78023,86],{"class":85},[45,78025,78026,78028,78030,78032,78034],{"class":47,"line":89},[45,78027,1913],{"class":73},[45,78029,33999],{"class":51},[45,78031,1129],{"class":55},[45,78033,3754],{"class":59},[45,78035,86],{"class":85},[45,78037,78038],{"class":47,"line":112},[45,78039,23579],{"class":73},[45,78041,78042,78044,78046,78048],{"class":47,"line":126},[45,78043,93],{"class":92},[45,78045,96],{"class":85},[45,78047,34014],{"class":2070},[45,78049,109],{"class":85},[45,78051,78052,78054,78056],{"class":47,"line":142},[45,78053,34021],{"class":51},[45,78055,12992],{"class":55},[45,78057,12962],{"class":59},[45,78059,78060,78062],{"class":47,"line":149},[45,78061,28],{"class":73},[45,78063,86],{"class":85},[45,78065,78066,78068,78070,78072,78074,78076],{"class":47,"line":679},[45,78067,93],{"class":92},[45,78069,96],{"class":85},[45,78071,100],{"class":99},[45,78073,77978],{"class":103},[45,78075,100],{"class":99},[45,78077,109],{"class":85},[14,78079,177],{},[36,78081,78083],{"className":78082,"code":13036,"language":5000,"meta":41},[4998],[18,78084,13036],{"__ignoreMap":41},[14,78086,1732,78087,78089,78090,78092],{},[18,78088,28],{}," block does not run here because ",[18,78091,35658],{}," stopped the loop early.",[31,78094,1337],{"id":1336},[14,78096,78097,78098,78100],{},"Here are some of the most common ",[18,78099,23529],{}," loop mistakes.",[712,78102,78104],{"id":78103},"forgetting-to-change-the-loop-variable","Forgetting to change the loop variable",[14,78106,71761],{},[36,78108,78109],{"className":38,"code":77510,"language":40,"meta":41,"style":41},[18,78110,78111,78119,78123,78135],{"__ignoreMap":41},[45,78112,78113,78115,78117],{"class":47,"line":48},[45,78114,33984],{"class":51},[45,78116,56],{"class":55},[45,78118,12962],{"class":59},[45,78120,78121],{"class":47,"line":63},[45,78122,67],{"emptyLinePlaceholder":66},[45,78124,78125,78127,78129,78131,78133],{"class":47,"line":70},[45,78126,23529],{"class":73},[45,78128,33999],{"class":51},[45,78130,1156],{"class":55},[45,78132,777],{"class":59},[45,78134,86],{"class":85},[45,78136,78137,78139,78141,78143],{"class":47,"line":89},[45,78138,93],{"class":92},[45,78140,96],{"class":85},[45,78142,34014],{"class":2070},[45,78144,109],{"class":85},[14,78146,78147],{},"Why it happens:",[195,78149,78150,78155],{},[198,78151,78152,78154],{},[18,78153,34014],{}," never changes",[198,78156,78157,78158],{},"The condition stays ",[18,78159,229],{},[14,78161,78162],{},"Fix:",[36,78164,78165],{"className":38,"code":77561,"language":40,"meta":41,"style":41},[18,78166,78167,78175,78179,78191,78201],{"__ignoreMap":41},[45,78168,78169,78171,78173],{"class":47,"line":48},[45,78170,33984],{"class":51},[45,78172,56],{"class":55},[45,78174,12962],{"class":59},[45,78176,78177],{"class":47,"line":63},[45,78178,67],{"emptyLinePlaceholder":66},[45,78180,78181,78183,78185,78187,78189],{"class":47,"line":70},[45,78182,23529],{"class":73},[45,78184,33999],{"class":51},[45,78186,1156],{"class":55},[45,78188,777],{"class":59},[45,78190,86],{"class":85},[45,78192,78193,78195,78197,78199],{"class":47,"line":89},[45,78194,93],{"class":92},[45,78196,96],{"class":85},[45,78198,34014],{"class":2070},[45,78200,109],{"class":85},[45,78202,78203,78205,78207],{"class":47,"line":112},[45,78204,34021],{"class":51},[45,78206,12992],{"class":55},[45,78208,12962],{"class":59},[712,78210,1350,78212,1353,78214,78216],{"id":78211},"using-instead-of-in-the-condition",[18,78213,56],{},[18,78215,1129],{}," in the condition",[14,78218,71761],{},[36,78220,78222],{"className":38,"code":78221,"language":40,"meta":41,"style":41},"count = 3\n\nwhile count = 3:\n    print(count)\n",[18,78223,78224,78232,78236,78248],{"__ignoreMap":41},[45,78225,78226,78228,78230],{"class":47,"line":48},[45,78227,33984],{"class":51},[45,78229,56],{"class":55},[45,78231,1481],{"class":59},[45,78233,78234],{"class":47,"line":63},[45,78235,67],{"emptyLinePlaceholder":66},[45,78237,78238,78240,78242,78244,78246],{"class":47,"line":70},[45,78239,23529],{"class":73},[45,78241,33999],{"class":51},[45,78243,56],{"class":55},[45,78245,777],{"class":59},[45,78247,86],{"class":85},[45,78249,78250,78252,78254,78256],{"class":47,"line":89},[45,78251,93],{"class":92},[45,78253,96],{"class":85},[45,78255,34014],{"class":2070},[45,78257,109],{"class":85},[14,78259,78260,78261,78263,78264,78266],{},"This causes an error because ",[18,78262,56],{}," assigns a value, but ",[18,78265,1129],{}," compares values.",[14,78268,78162],{},[36,78270,78272],{"className":38,"code":78271,"language":40,"meta":41,"style":41},"count = 3\n\nwhile count == 3:\n    print(count)\n    break\n",[18,78273,78274,78282,78286,78298,78308],{"__ignoreMap":41},[45,78275,78276,78278,78280],{"class":47,"line":48},[45,78277,33984],{"class":51},[45,78279,56],{"class":55},[45,78281,1481],{"class":59},[45,78283,78284],{"class":47,"line":63},[45,78285,67],{"emptyLinePlaceholder":66},[45,78287,78288,78290,78292,78294,78296],{"class":47,"line":70},[45,78289,23529],{"class":73},[45,78291,33999],{"class":51},[45,78293,1129],{"class":55},[45,78295,777],{"class":59},[45,78297,86],{"class":85},[45,78299,78300,78302,78304,78306],{"class":47,"line":89},[45,78301,93],{"class":92},[45,78303,96],{"class":85},[45,78305,34014],{"class":2070},[45,78307,109],{"class":85},[45,78309,78310],{"class":47,"line":112},[45,78311,36047],{"class":73},[712,78313,78315],{"id":78314},"wrong-indentation-inside-the-loop","Wrong indentation inside the loop",[14,78317,71761],{},[36,78319,78321],{"className":38,"code":78320,"language":40,"meta":41,"style":41},"count = 1\n\nwhile count \u003C= 3:\nprint(count)\n",[18,78322,78323,78331,78335,78347],{"__ignoreMap":41},[45,78324,78325,78327,78329],{"class":47,"line":48},[45,78326,33984],{"class":51},[45,78328,56],{"class":55},[45,78330,12962],{"class":59},[45,78332,78333],{"class":47,"line":63},[45,78334,67],{"emptyLinePlaceholder":66},[45,78336,78337,78339,78341,78343,78345],{"class":47,"line":70},[45,78338,23529],{"class":73},[45,78340,33999],{"class":51},[45,78342,1156],{"class":55},[45,78344,777],{"class":59},[45,78346,86],{"class":85},[45,78348,78349,78351,78353,78355],{"class":47,"line":89},[45,78350,2065],{"class":92},[45,78352,96],{"class":85},[45,78354,34014],{"class":2070},[45,78356,109],{"class":85},[14,78358,78359],{},"This causes an indentation error because the loop body must be indented.",[14,78361,78162],{},[36,78363,78364],{"className":38,"code":77510,"language":40,"meta":41,"style":41},[18,78365,78366,78374,78378,78390],{"__ignoreMap":41},[45,78367,78368,78370,78372],{"class":47,"line":48},[45,78369,33984],{"class":51},[45,78371,56],{"class":55},[45,78373,12962],{"class":59},[45,78375,78376],{"class":47,"line":63},[45,78377,67],{"emptyLinePlaceholder":66},[45,78379,78380,78382,78384,78386,78388],{"class":47,"line":70},[45,78381,23529],{"class":73},[45,78383,33999],{"class":51},[45,78385,1156],{"class":55},[45,78387,777],{"class":59},[45,78389,86],{"class":85},[45,78391,78392,78394,78396,78398],{"class":47,"line":89},[45,78393,93],{"class":92},[45,78395,96],{"class":85},[45,78397,34014],{"class":2070},[45,78399,109],{"class":85},[14,78401,78402,78403,240],{},"If indentation is still confusing, read ",[236,78404,554],{"href":553},[712,78406,78408],{"id":78407},"writing-a-condition-that-is-always-true","Writing a condition that is always True",[14,78410,71761],{},[36,78412,78414],{"className":38,"code":78413,"language":40,"meta":41,"style":41},"while True:\n    print(\"This keeps running\")\n",[18,78415,78416,78424],{"__ignoreMap":41},[45,78417,78418,78420,78422],{"class":47,"line":48},[45,78419,23529],{"class":73},[45,78421,23323],{"class":481},[45,78423,86],{"class":85},[45,78425,78426,78428,78430,78432,78435,78437],{"class":47,"line":63},[45,78427,93],{"class":92},[45,78429,96],{"class":85},[45,78431,100],{"class":99},[45,78433,78434],{"class":103},"This keeps running",[45,78436,100],{"class":99},[45,78438,109],{"class":85},[14,78440,78441,78442,78444],{},"This is not always wrong, but it will run forever unless you stop it with ",[18,78443,35658],{}," or another exit condition.",[14,78446,78447],{},"Safer version:",[36,78449,78450],{"className":38,"code":77561,"language":40,"meta":41,"style":41},[18,78451,78452,78460,78464,78476,78486],{"__ignoreMap":41},[45,78453,78454,78456,78458],{"class":47,"line":48},[45,78455,33984],{"class":51},[45,78457,56],{"class":55},[45,78459,12962],{"class":59},[45,78461,78462],{"class":47,"line":63},[45,78463,67],{"emptyLinePlaceholder":66},[45,78465,78466,78468,78470,78472,78474],{"class":47,"line":70},[45,78467,23529],{"class":73},[45,78469,33999],{"class":51},[45,78471,1156],{"class":55},[45,78473,777],{"class":59},[45,78475,86],{"class":85},[45,78477,78478,78480,78482,78484],{"class":47,"line":89},[45,78479,93],{"class":92},[45,78481,96],{"class":85},[45,78483,34014],{"class":2070},[45,78485,109],{"class":85},[45,78487,78488,78490,78492],{"class":47,"line":112},[45,78489,34021],{"class":51},[45,78491,12992],{"class":55},[45,78493,12962],{"class":59},[31,78495,2281],{"id":2280},[712,78497,78499],{"id":78498},"what-is-a-while-loop-in-python","What is a while loop in Python?",[14,78501,2419,78502,78504,78505,240],{},[18,78503,23529],{}," loop repeats code as long as its condition stays ",[18,78506,229],{},[712,78508,78510],{"id":78509},"when-should-i-use-a-while-loop","When should I use a while loop?",[14,78512,78513],{},"Use it when the number of repeats depends on a condition, not a fixed sequence.",[712,78515,78517],{"id":78516},"why-does-my-while-loop-never-stop","Why does my while loop never stop?",[14,78519,78520,78521,78523],{},"Usually because the condition never becomes ",[18,78522,233],{}," or the loop variable is not updated.",[712,78525,51368],{"id":51367},[14,78527,2419,78528,78530,78531,78533],{},[18,78529,7895],{}," loop usually goes through items in a sequence. A ",[18,78532,23529],{}," loop keeps going based on a condition.",[712,78535,78537],{"id":78536},"can-i-use-break-in-a-while-loop","Can I use break in a while loop?",[14,78539,37296,78540,77710],{},[18,78541,35658],{},[31,78543,2342],{"id":2341},[195,78545,78546,78550,78554,78558,78562,78566],{},[198,78547,78548],{},[236,78549,23611],{"href":23610},[198,78551,78552],{},[236,78553,50457],{"href":50456},[198,78555,78556],{},[236,78557,549],{"href":548},[198,78559,78560],{},[236,78561,554],{"href":553},[198,78563,78564],{},[236,78565,1614],{"href":1613},[198,78567,78568],{},[236,78569,1541],{"href":1540},[2367,78571,78572],{},"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 .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 .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":41,"searchDepth":63,"depth":63,"links":78574},[78575,78576,78577,78578,78579,78580,78581,78587,78588,78595,78602],{"id":76916,"depth":63,"text":76917},{"id":76967,"depth":63,"text":76968},{"id":77080,"depth":63,"text":77081},{"id":77242,"depth":63,"text":77243},{"id":77336,"depth":63,"text":77337},{"id":77498,"depth":63,"text":77499},{"id":36430,"depth":63,"text":36431,"children":78582},[78583,78585],{"id":77703,"depth":70,"text":78584},"Using break",{"id":77794,"depth":70,"text":78586},"Using continue",{"id":77895,"depth":63,"text":77896},{"id":1336,"depth":63,"text":1337,"children":78589},[78590,78591,78593,78594],{"id":78103,"depth":70,"text":78104},{"id":78211,"depth":70,"text":78592},"Using = instead of == in the condition",{"id":78314,"depth":70,"text":78315},{"id":78407,"depth":70,"text":78408},{"id":2280,"depth":63,"text":2281,"children":78596},[78597,78598,78599,78600,78601],{"id":78498,"depth":70,"text":78499},{"id":78509,"depth":70,"text":78510},{"id":78516,"depth":70,"text":78517},{"id":51367,"depth":70,"text":51368},{"id":78536,"depth":70,"text":78537},{"id":2341,"depth":63,"text":2342},"Master python while loops explained in our comprehensive Python beginner guide.",{},{"title":76834,"description":78603},"learn\u002Fpython-while-loops-explained","Q3D3T-LrnUKoHPgaVmTIZ-yqiSY330jq8GVXkZSzFVc",{"id":78609,"title":78610,"body":78611,"description":80300,"extension":2399,"meta":80301,"navigation":66,"path":80302,"seo":80303,"stem":80304,"__hash__":80305},"content\u002Flearn\u002Fraising-exceptions-in-python.md","Raising Exceptions in Python",{"type":7,"value":78612,"toc":80273},[78613,78616,78621,78624,78626,78675,78680,78684,78687,78692,78695,78713,78715,78762,78768,78776,78780,78785,78788,78802,78804,78848,78851,78855,78858,78880,78882,78904,78907,78920,78923,78927,78930,78932,78937,78984,78986,78991,79045,79047,79052,79121,79123,79128,79200,79204,79209,79256,79269,79273,79276,79279,79281,79462,79464,79472,79478,79482,79487,79493,79495,79602,79604,79619,79622,79708,79711,79717,79721,79724,79733,79790,79793,79796,79800,79803,79806,79811,79892,79895,79899,79902,79905,79922,79924,79980,79983,80005,80008,80010,80015,80042,80045,80094,80097,80143,80146,80186,80188,80192,80200,80204,80212,80216,80222,80226,80231,80235,80238,80240,80270],[10,78614,78610],{"id":78615},"raising-exceptions-in-python",[14,78617,78618,78620],{},[18,78619,58974],{}," lets you stop your program and report a problem clearly.",[14,78622,78623],{},"You use it when something is wrong with the data, the input, or the current state of the program. This helps you catch bugs early and makes your code easier to understand.",[31,78625,34],{"id":33},[36,78627,78629],{"className":38,"code":78628,"language":40,"meta":41,"style":41},"age = -1\n\nif age \u003C 0:\n    raise ValueError(\"age cannot be negative\")\n",[18,78630,78631,78641,78645,78657],{"__ignoreMap":41},[45,78632,78633,78635,78637,78639],{"class":47,"line":48},[45,78634,52],{"class":51},[45,78636,56],{"class":55},[45,78638,28089],{"class":55},[45,78640,13036],{"class":59},[45,78642,78643],{"class":47,"line":63},[45,78644,67],{"emptyLinePlaceholder":66},[45,78646,78647,78649,78651,78653,78655],{"class":47,"line":70},[45,78648,20],{"class":73},[45,78650,76],{"class":51},[45,78652,79],{"class":55},[45,78654,749],{"class":59},[45,78656,86],{"class":85},[45,78658,78659,78662,78664,78666,78668,78671,78673],{"class":47,"line":89},[45,78660,78661],{"class":73},"    raise",[45,78663,45892],{"class":2109},[45,78665,96],{"class":85},[45,78667,100],{"class":99},[45,78669,78670],{"class":103},"age cannot be negative",[45,78672,100],{"class":99},[45,78674,109],{"class":85},[14,78676,165,78677,78679],{},[18,78678,58974],{}," when you want your code to stop and report a problem clearly.",[31,78681,78683],{"id":78682},"what-raising-an-exception-means","What raising an exception means",[14,78685,78686],{},"An exception is an error signal in Python.",[14,78688,20236,78689,78691],{},[18,78690,58974],{},", you tell Python to create that error right now. If the exception is not caught, the program stops and shows an error message.",[14,78693,78694],{},"Basic idea:",[195,78696,78697,78700,78705],{},[198,78698,78699],{},"An exception signals that something went wrong.",[198,78701,78702,78704],{},[18,78703,58974],{}," creates that exception.",[198,78706,78707,78708,1316,78710,78712],{},"It stops normal execution unless ",[18,78709,23272],{},[18,78711,23275],{}," catch it.",[14,78714,466],{},[36,78716,78718],{"className":38,"code":78717,"language":40,"meta":41,"style":41},"score = 150\n\nif score > 100:\n    raise ValueError(\"score must be between 0 and 100\")\n",[18,78719,78720,78729,78733,78745],{"__ignoreMap":41},[45,78721,78722,78724,78726],{"class":47,"line":48},[45,78723,581],{"class":51},[45,78725,56],{"class":55},[45,78727,78728],{"class":59}," 150\n",[45,78730,78731],{"class":47,"line":63},[45,78732,67],{"emptyLinePlaceholder":66},[45,78734,78735,78737,78739,78741,78743],{"class":47,"line":70},[45,78736,20],{"class":73},[45,78738,597],{"class":51},[45,78740,272],{"class":55},[45,78742,20280],{"class":59},[45,78744,86],{"class":85},[45,78746,78747,78749,78751,78753,78755,78758,78760],{"class":47,"line":89},[45,78748,78661],{"class":73},[45,78750,45892],{"class":2109},[45,78752,96],{"class":85},[45,78754,100],{"class":99},[45,78756,78757],{"class":103},"score must be between 0 and 100",[45,78759,100],{"class":99},[45,78761,109],{"class":85},[14,78763,78764,78765,78767],{},"Here, Python raises a ",[18,78766,42097],{}," because the value is outside the allowed range.",[14,78769,78770,78771,78775],{},"If you are new to this topic, see ",[236,78772,78774],{"href":78773},"\u002Fglossary\u002Fwhat-is-an-exception-in-python","what an exception is in Python"," for the basic definition.",[31,78777,78779],{"id":78778},"when-to-use-raise","When to use raise",[14,78781,165,78782,78784],{},[18,78783,58974],{}," when your code finds a real problem.",[14,78786,78787],{},"Common cases:",[195,78789,78790,78793,78796,78799],{},[198,78791,78792],{},"Validate function inputs",[198,78794,78795],{},"Stop when required data is missing",[198,78797,78798],{},"Reject values outside an allowed range",[198,78800,78801],{},"Catch bugs early instead of letting them cause confusing errors later",[14,78803,466],{},[36,78805,78807],{"className":38,"code":78806,"language":40,"meta":41,"style":41},"name = \"\"\n\nif not name:\n    raise ValueError(\"name is required\")\n",[18,78808,78809,78817,78821,78831],{"__ignoreMap":41},[45,78810,78811,78813,78815],{"class":47,"line":48},[45,78812,1560],{"class":51},[45,78814,56],{"class":55},[45,78816,56021],{"class":99},[45,78818,78819],{"class":47,"line":63},[45,78820,67],{"emptyLinePlaceholder":66},[45,78822,78823,78825,78827,78829],{"class":47,"line":70},[45,78824,20],{"class":73},[45,78826,1296],{"class":55},[45,78828,3083],{"class":51},[45,78830,86],{"class":85},[45,78832,78833,78835,78837,78839,78841,78844,78846],{"class":47,"line":89},[45,78834,78661],{"class":73},[45,78836,45892],{"class":2109},[45,78838,96],{"class":85},[45,78840,100],{"class":99},[45,78842,78843],{"class":103},"name is required",[45,78845,100],{"class":99},[45,78847,109],{"class":85},[14,78849,78850],{},"This is better than letting the program continue with bad data.",[31,78852,78854],{"id":78853},"basic-raise-syntax","Basic raise syntax",[14,78856,78857],{},"The most common pattern is:",[36,78859,78861],{"className":38,"code":78860,"language":40,"meta":41,"style":41},"raise ExceptionType(\"message\")\n",[18,78862,78863],{"__ignoreMap":41},[45,78864,78865,78867,78870,78872,78874,78876,78878],{"class":47,"line":48},[45,78866,58974],{"class":73},[45,78868,78869],{"class":2070}," ExceptionType",[45,78871,96],{"class":85},[45,78873,100],{"class":99},[45,78875,6462],{"class":103},[45,78877,100],{"class":99},[45,78879,109],{"class":85},[14,78881,466],{},[36,78883,78885],{"className":38,"code":78884,"language":40,"meta":41,"style":41},"raise ValueError(\"price cannot be negative\")\n",[18,78886,78887],{"__ignoreMap":41},[45,78888,78889,78891,78893,78895,78897,78900,78902],{"class":47,"line":48},[45,78890,58974],{"class":73},[45,78892,45892],{"class":2109},[45,78894,96],{"class":85},[45,78896,100],{"class":99},[45,78898,78899],{"class":103},"price cannot be negative",[45,78901,100],{"class":99},[45,78903,109],{"class":85},[14,78905,78906],{},"This has two parts:",[195,78908,78909,78914],{},[198,78910,78911,78913],{},[18,78912,42097],{}," is the exception type",[198,78915,78916,78919],{},[18,78917,78918],{},"\"price cannot be negative\""," is the message",[14,78921,78922],{},"Try to choose an exception type that matches the problem. A clear message makes debugging much easier.",[31,78924,78926],{"id":78925},"common-built-in-exceptions-to-raise","Common built-in exceptions to raise",[14,78928,78929],{},"Python has many built-in exception types. Here are some of the most common ones you will raise yourself.",[712,78931,42097],{"id":46606},[14,78933,165,78934,78936],{},[18,78935,42097],{}," when the type is correct, but the value is wrong.",[36,78938,78940],{"className":38,"code":78939,"language":40,"meta":41,"style":41},"age = -5\n\nif age \u003C 0:\n    raise ValueError(\"age cannot be negative\")\n",[18,78941,78942,78952,78956,78968],{"__ignoreMap":41},[45,78943,78944,78946,78948,78950],{"class":47,"line":48},[45,78945,52],{"class":51},[45,78947,56],{"class":55},[45,78949,28089],{"class":55},[45,78951,11570],{"class":59},[45,78953,78954],{"class":47,"line":63},[45,78955,67],{"emptyLinePlaceholder":66},[45,78957,78958,78960,78962,78964,78966],{"class":47,"line":70},[45,78959,20],{"class":73},[45,78961,76],{"class":51},[45,78963,79],{"class":55},[45,78965,749],{"class":59},[45,78967,86],{"class":85},[45,78969,78970,78972,78974,78976,78978,78980,78982],{"class":47,"line":89},[45,78971,78661],{"class":73},[45,78973,45892],{"class":2109},[45,78975,96],{"class":85},[45,78977,100],{"class":99},[45,78979,78670],{"class":103},[45,78981,100],{"class":99},[45,78983,109],{"class":85},[712,78985,7711],{"id":46563},[14,78987,165,78988,78990],{},[18,78989,7711],{}," when the value has the wrong type.",[36,78992,78994],{"className":38,"code":78993,"language":40,"meta":41,"style":41},"name = 123\n\nif not isinstance(name, str):\n    raise TypeError(\"name must be a string\")\n",[18,78995,78996,79004,79008,79027],{"__ignoreMap":41},[45,78997,78998,79000,79002],{"class":47,"line":48},[45,78999,1560],{"class":51},[45,79001,56],{"class":55},[45,79003,48892],{"class":59},[45,79005,79006],{"class":47,"line":63},[45,79007,67],{"emptyLinePlaceholder":66},[45,79009,79010,79012,79014,79017,79019,79021,79023,79025],{"class":47,"line":70},[45,79011,20],{"class":73},[45,79013,1296],{"class":55},[45,79015,79016],{"class":92}," isinstance",[45,79018,96],{"class":85},[45,79020,3096],{"class":2070},[45,79022,3079],{"class":85},[45,79024,38516],{"class":2109},[45,79026,2464],{"class":85},[45,79028,79029,79031,79034,79036,79038,79041,79043],{"class":47,"line":89},[45,79030,78661],{"class":73},[45,79032,79033],{"class":2109}," TypeError",[45,79035,96],{"class":85},[45,79037,100],{"class":99},[45,79039,79040],{"class":103},"name must be a string",[45,79042,100],{"class":99},[45,79044,109],{"class":85},[712,79046,45197],{"id":46704},[14,79048,165,79049,79051],{},[18,79050,45197],{}," when a required dictionary key is missing.",[36,79053,79055],{"className":38,"code":79054,"language":40,"meta":41,"style":41},"user = {\"name\": \"Ava\"}\n\nif \"email\" not in user:\n    raise KeyError(\"email\")\n",[18,79056,79057,79081,79085,79104],{"__ignoreMap":41},[45,79058,79059,79061,79063,79065,79067,79069,79071,79073,79075,79077,79079],{"class":47,"line":48},[45,79060,30062],{"class":51},[45,79062,56],{"class":55},[45,79064,8604],{"class":85},[45,79066,100],{"class":99},[45,79068,3096],{"class":103},[45,79070,100],{"class":99},[45,79072,457],{"class":85},[45,79074,1565],{"class":99},[45,79076,3162],{"class":103},[45,79078,100],{"class":99},[45,79080,8629],{"class":85},[45,79082,79083],{"class":47,"line":63},[45,79084,67],{"emptyLinePlaceholder":66},[45,79086,79087,79089,79091,79093,79095,79097,79099,79102],{"class":47,"line":70},[45,79088,20],{"class":73},[45,79090,1565],{"class":99},[45,79092,20561],{"class":103},[45,79094,100],{"class":99},[45,79096,1296],{"class":55},[45,79098,45650],{"class":55},[45,79100,79101],{"class":51}," user",[45,79103,86],{"class":85},[45,79105,79106,79108,79111,79113,79115,79117,79119],{"class":47,"line":89},[45,79107,78661],{"class":73},[45,79109,79110],{"class":2109}," KeyError",[45,79112,96],{"class":85},[45,79114,100],{"class":99},[45,79116,20561],{"class":103},[45,79118,100],{"class":99},[45,79120,109],{"class":85},[712,79122,46068],{"id":46653},[14,79124,165,79125,79127],{},[18,79126,46068],{}," for a bad list or tuple index.",[36,79129,79131],{"className":38,"code":79130,"language":40,"meta":41,"style":41},"numbers = [10, 20, 30]\nindex = 5\n\nif index >= len(numbers):\n    raise IndexError(\"list index out of range\")\n",[18,79132,79133,79153,79162,79166,79183],{"__ignoreMap":41},[45,79134,79135,79137,79139,79141,79143,79145,79147,79149,79151],{"class":47,"line":48},[45,79136,8570],{"class":51},[45,79138,56],{"class":55},[45,79140,8575],{"class":85},[45,79142,11786],{"class":59},[45,79144,3079],{"class":85},[45,79146,10503],{"class":59},[45,79148,3079],{"class":85},[45,79150,10512],{"class":59},[45,79152,8594],{"class":85},[45,79154,79155,79158,79160],{"class":47,"line":63},[45,79156,79157],{"class":51},"index ",[45,79159,56],{"class":55},[45,79161,733],{"class":59},[45,79163,79164],{"class":47,"line":70},[45,79165,67],{"emptyLinePlaceholder":66},[45,79167,79168,79170,79173,79175,79177,79179,79181],{"class":47,"line":89},[45,79169,20],{"class":73},[45,79171,79172],{"class":51}," index ",[45,79174,600],{"class":55},[45,79176,9139],{"class":92},[45,79178,96],{"class":85},[45,79180,9069],{"class":2070},[45,79182,2464],{"class":85},[45,79184,79185,79187,79190,79192,79194,79196,79198],{"class":47,"line":112},[45,79186,78661],{"class":73},[45,79188,79189],{"class":2109}," IndexError",[45,79191,96],{"class":85},[45,79193,100],{"class":99},[45,79195,46415],{"class":103},[45,79197,100],{"class":99},[45,79199,109],{"class":85},[712,79201,79203],{"id":79202},"runtimeerror","RuntimeError",[14,79205,165,79206,79208],{},[18,79207,79203],{}," for a general runtime problem when no better built-in type fits.",[36,79210,79212],{"className":38,"code":79211,"language":40,"meta":41,"style":41},"connected = False\n\nif not connected:\n    raise RuntimeError(\"database is not connected\")\n",[18,79213,79214,79223,79227,79238],{"__ignoreMap":41},[45,79215,79216,79219,79221],{"class":47,"line":48},[45,79217,79218],{"class":51},"connected ",[45,79220,56],{"class":55},[45,79222,33660],{"class":481},[45,79224,79225],{"class":47,"line":63},[45,79226,67],{"emptyLinePlaceholder":66},[45,79228,79229,79231,79233,79236],{"class":47,"line":70},[45,79230,20],{"class":73},[45,79232,1296],{"class":55},[45,79234,79235],{"class":51}," connected",[45,79237,86],{"class":85},[45,79239,79240,79242,79245,79247,79249,79252,79254],{"class":47,"line":89},[45,79241,78661],{"class":73},[45,79243,79244],{"class":2109}," RuntimeError",[45,79246,96],{"class":85},[45,79248,100],{"class":99},[45,79250,79251],{"class":103},"database is not connected",[45,79253,100],{"class":99},[45,79255,109],{"class":85},[14,79257,79258,79259,79261,79262,1316,79265,240],{},"If you want a broader introduction first, read ",[236,79260,35173],{"href":47555},". For specific error types, see ",[236,79263,79264],{"href":46646},"ValueError in Python: causes and fixes",[236,79266,79268],{"href":79267},"\u002Ferrors\u002Ftypeerror-vs-valueerror-in-python-explained","TypeError in Python: causes and fixes",[31,79270,79272],{"id":79271},"raising-exceptions-in-functions","Raising exceptions in functions",[14,79274,79275],{},"It is usually best to check inputs near the start of a function.",[14,79277,79278],{},"This is called failing early. It makes the function behavior clearer and prevents bad data from spreading through your code.",[14,79280,466],{},[36,79282,79284],{"className":38,"code":79283,"language":40,"meta":41,"style":41},"def divide(total, count):\n    if not isinstance(total, (int, float)):\n        raise TypeError(\"total must be a number\")\n\n    if not isinstance(count, int):\n        raise TypeError(\"count must be an integer\")\n\n    if count == 0:\n        raise ValueError(\"count cannot be zero\")\n\n    return total \u002F count\n\n\nprint(divide(10, 2))\n",[18,79285,79286,79303,79329,79347,79351,79369,79386,79390,79402,79419,79423,79435,79439,79443],{"__ignoreMap":41},[45,79287,79288,79290,79293,79295,79297,79299,79301],{"class":47,"line":48},[45,79289,2757],{"class":2440},[45,79291,79292],{"class":2455}," divide",[45,79294,96],{"class":85},[45,79296,75281],{"class":3082},[45,79298,3079],{"class":85},[45,79300,77154],{"class":3082},[45,79302,2464],{"class":85},[45,79304,79305,79307,79309,79311,79313,79315,79317,79319,79321,79323,79326],{"class":47,"line":63},[45,79306,1913],{"class":73},[45,79308,1296],{"class":55},[45,79310,79016],{"class":92},[45,79312,96],{"class":85},[45,79314,75281],{"class":2070},[45,79316,3079],{"class":85},[45,79318,14039],{"class":85},[45,79320,29738],{"class":2109},[45,79322,3079],{"class":85},[45,79324,79325],{"class":2109}," float",[45,79327,79328],{"class":85},")):\n",[45,79330,79331,79334,79336,79338,79340,79343,79345],{"class":47,"line":70},[45,79332,79333],{"class":73},"        raise",[45,79335,79033],{"class":2109},[45,79337,96],{"class":85},[45,79339,100],{"class":99},[45,79341,79342],{"class":103},"total must be a number",[45,79344,100],{"class":99},[45,79346,109],{"class":85},[45,79348,79349],{"class":47,"line":89},[45,79350,67],{"emptyLinePlaceholder":66},[45,79352,79353,79355,79357,79359,79361,79363,79365,79367],{"class":47,"line":112},[45,79354,1913],{"class":73},[45,79356,1296],{"class":55},[45,79358,79016],{"class":92},[45,79360,96],{"class":85},[45,79362,34014],{"class":2070},[45,79364,3079],{"class":85},[45,79366,41669],{"class":2109},[45,79368,2464],{"class":85},[45,79370,79371,79373,79375,79377,79379,79382,79384],{"class":47,"line":126},[45,79372,79333],{"class":73},[45,79374,79033],{"class":2109},[45,79376,96],{"class":85},[45,79378,100],{"class":99},[45,79380,79381],{"class":103},"count must be an integer",[45,79383,100],{"class":99},[45,79385,109],{"class":85},[45,79387,79388],{"class":47,"line":142},[45,79389,67],{"emptyLinePlaceholder":66},[45,79391,79392,79394,79396,79398,79400],{"class":47,"line":149},[45,79393,1913],{"class":73},[45,79395,33999],{"class":51},[45,79397,1129],{"class":55},[45,79399,749],{"class":59},[45,79401,86],{"class":85},[45,79403,79404,79406,79408,79410,79412,79415,79417],{"class":47,"line":679},[45,79405,79333],{"class":73},[45,79407,45892],{"class":2109},[45,79409,96],{"class":85},[45,79411,100],{"class":99},[45,79413,79414],{"class":103},"count cannot be zero",[45,79416,100],{"class":99},[45,79418,109],{"class":85},[45,79420,79421],{"class":47,"line":686},[45,79422,67],{"emptyLinePlaceholder":66},[45,79424,79425,79427,79430,79432],{"class":47,"line":2229},[45,79426,4681],{"class":73},[45,79428,79429],{"class":51}," total ",[45,79431,63892],{"class":55},[45,79433,79434],{"class":51}," count\n",[45,79436,79437],{"class":47,"line":2236},[45,79438,67],{"emptyLinePlaceholder":66},[45,79440,79441],{"class":47,"line":5728},[45,79442,67],{"emptyLinePlaceholder":66},[45,79444,79445,79447,79449,79452,79454,79456,79458,79460],{"class":47,"line":19380},[45,79446,2065],{"class":92},[45,79448,96],{"class":85},[45,79450,79451],{"class":2070},"divide",[45,79453,96],{"class":85},[45,79455,11786],{"class":59},[45,79457,3079],{"class":85},[45,79459,3754],{"class":59},[45,79461,4348],{"class":85},[14,79463,177],{},[36,79465,79466],{"className":38,"code":14961,"language":40,"meta":41,"style":41},[18,79467,79468],{"__ignoreMap":41},[45,79469,79470],{"class":47,"line":48},[45,79471,14961],{"class":59},[14,79473,12209,79474,79477],{},[18,79475,79476],{},"divide(10, 0)",", the function raises a clear error right away.",[31,79479,79481],{"id":79480},"how-raise-works-with-try-and-except","How raise works with try and except",[14,79483,79484,79485,240],{},"A raised exception can be caught with ",[18,79486,23275],{},[14,79488,79489,79490,79492],{},"If it is not caught, the program stops with an error message. You can also raise an exception inside a ",[18,79491,23272],{}," block, or catch one exception and raise a different one.",[14,79494,466],{},[36,79496,79498],{"className":38,"code":79497,"language":40,"meta":41,"style":41},"def set_age(age):\n    if age \u003C 0:\n        raise ValueError(\"age cannot be negative\")\n    return age\n\ntry:\n    set_age(-2)\nexcept ValueError as error:\n    print(\"Caught an error:\", error)\n",[18,79499,79500,79513,79525,79541,79547,79551,79557,79570,79583],{"__ignoreMap":41},[45,79501,79502,79504,79507,79509,79511],{"class":47,"line":48},[45,79503,2757],{"class":2440},[45,79505,79506],{"class":2455}," set_age",[45,79508,96],{"class":85},[45,79510,2082],{"class":3082},[45,79512,2464],{"class":85},[45,79514,79515,79517,79519,79521,79523],{"class":47,"line":63},[45,79516,1913],{"class":73},[45,79518,76],{"class":51},[45,79520,79],{"class":55},[45,79522,749],{"class":59},[45,79524,86],{"class":85},[45,79526,79527,79529,79531,79533,79535,79537,79539],{"class":47,"line":70},[45,79528,79333],{"class":73},[45,79530,45892],{"class":2109},[45,79532,96],{"class":85},[45,79534,100],{"class":99},[45,79536,78670],{"class":103},[45,79538,100],{"class":99},[45,79540,109],{"class":85},[45,79542,79543,79545],{"class":47,"line":89},[45,79544,4681],{"class":73},[45,79546,3320],{"class":51},[45,79548,79549],{"class":47,"line":112},[45,79550,67],{"emptyLinePlaceholder":66},[45,79552,79553,79555],{"class":47,"line":126},[45,79554,23272],{"class":73},[45,79556,86],{"class":85},[45,79558,79559,79562,79564,79566,79568],{"class":47,"line":142},[45,79560,79561],{"class":2070},"    set_age",[45,79563,96],{"class":85},[45,79565,218],{"class":55},[45,79567,9367],{"class":59},[45,79569,109],{"class":85},[45,79571,79572,79574,79576,79578,79581],{"class":47,"line":149},[45,79573,23275],{"class":73},[45,79575,45892],{"class":2109},[45,79577,24015],{"class":73},[45,79579,79580],{"class":51}," error",[45,79582,86],{"class":85},[45,79584,79585,79587,79589,79591,79594,79596,79598,79600],{"class":47,"line":679},[45,79586,93],{"class":92},[45,79588,96],{"class":85},[45,79590,100],{"class":99},[45,79592,79593],{"class":103},"Caught an error:",[45,79595,100],{"class":99},[45,79597,3079],{"class":85},[45,79599,79580],{"class":2070},[45,79601,109],{"class":85},[14,79603,177],{},[36,79605,79607],{"className":38,"code":79606,"language":40,"meta":41,"style":41},"Caught an error: age cannot be negative\n",[18,79608,79609],{"__ignoreMap":41},[45,79610,79611,79614,79616],{"class":47,"line":48},[45,79612,79613],{"class":51},"Caught an error",[45,79615,457],{"class":85},[45,79617,79618],{"class":51}," age cannot be negative\n",[14,79620,79621],{},"You can also convert one error into another:",[36,79623,79625],{"className":38,"code":79624,"language":40,"meta":41,"style":41},"data = {\"count\": \"abc\"}\n\ntry:\n    count = int(data[\"count\"])\nexcept ValueError:\n    raise TypeError(\"count must contain a valid integer\")\n",[18,79626,79627,79651,79655,79661,79683,79691],{"__ignoreMap":41},[45,79628,79629,79631,79633,79635,79637,79639,79641,79643,79645,79647,79649],{"class":47,"line":48},[45,79630,30591],{"class":51},[45,79632,56],{"class":55},[45,79634,8604],{"class":85},[45,79636,100],{"class":99},[45,79638,34014],{"class":103},[45,79640,100],{"class":99},[45,79642,457],{"class":85},[45,79644,1565],{"class":99},[45,79646,10304],{"class":103},[45,79648,100],{"class":99},[45,79650,8629],{"class":85},[45,79652,79653],{"class":47,"line":63},[45,79654,67],{"emptyLinePlaceholder":66},[45,79656,79657,79659],{"class":47,"line":70},[45,79658,23272],{"class":73},[45,79660,86],{"class":85},[45,79662,79663,79665,79667,79669,79671,79673,79675,79677,79679,79681],{"class":47,"line":89},[45,79664,34021],{"class":51},[45,79666,56],{"class":55},[45,79668,41669],{"class":2109},[45,79670,96],{"class":85},[45,79672,30614],{"class":2070},[45,79674,9680],{"class":85},[45,79676,100],{"class":99},[45,79678,34014],{"class":103},[45,79680,100],{"class":99},[45,79682,21941],{"class":85},[45,79684,79685,79687,79689],{"class":47,"line":112},[45,79686,23275],{"class":73},[45,79688,45892],{"class":2109},[45,79690,86],{"class":85},[45,79692,79693,79695,79697,79699,79701,79704,79706],{"class":47,"line":126},[45,79694,78661],{"class":73},[45,79696,79033],{"class":2109},[45,79698,96],{"class":85},[45,79700,100],{"class":99},[45,79702,79703],{"class":103},"count must contain a valid integer",[45,79705,100],{"class":99},[45,79707,109],{"class":85},[14,79709,79710],{},"This can be useful, but do it carefully so you do not hide the real cause.",[14,79712,79713,79714,240],{},"For more on this pattern, see ",[236,79715,79716],{"href":46079},"using try, except, else, and finally in Python",[31,79718,79720],{"id":79719},"re-raising-an-exception","Re-raising an exception",[14,79722,79723],{},"Sometimes you want to do something when an error happens, such as logging or printing a message, and then let the same exception continue upward.",[14,79725,79726,79727,79729,79730,79732],{},"Use plain ",[18,79728,58974],{}," inside an ",[18,79731,23275],{}," block:",[36,79734,79736],{"className":38,"code":79735,"language":40,"meta":41,"style":41},"try:\n    number = int(\"hello\")\nexcept ValueError:\n    print(\"Could not convert the text to an integer\")\n    raise\n",[18,79737,79738,79744,79762,79770,79785],{"__ignoreMap":41},[45,79739,79740,79742],{"class":47,"line":48},[45,79741,23272],{"class":73},[45,79743,86],{"class":85},[45,79745,79746,79748,79750,79752,79754,79756,79758,79760],{"class":47,"line":63},[45,79747,12957],{"class":51},[45,79749,56],{"class":55},[45,79751,41669],{"class":2109},[45,79753,96],{"class":85},[45,79755,100],{"class":99},[45,79757,18782],{"class":103},[45,79759,100],{"class":99},[45,79761,109],{"class":85},[45,79763,79764,79766,79768],{"class":47,"line":70},[45,79765,23275],{"class":73},[45,79767,45892],{"class":2109},[45,79769,86],{"class":85},[45,79771,79772,79774,79776,79778,79781,79783],{"class":47,"line":89},[45,79773,93],{"class":92},[45,79775,96],{"class":85},[45,79777,100],{"class":99},[45,79779,79780],{"class":103},"Could not convert the text to an integer",[45,79782,100],{"class":99},[45,79784,109],{"class":85},[45,79786,79787],{"class":47,"line":112},[45,79788,79789],{"class":73},"    raise\n",[14,79791,79792],{},"This keeps the original exception information.",[14,79794,79795],{},"That is better than creating a completely new exception if you still want the original traceback.",[31,79797,79799],{"id":79798},"creating-custom-exceptions","Creating custom exceptions",[14,79801,79802],{},"You can create your own exception class for special cases.",[14,79804,79805],{},"This is useful in larger programs where built-in exceptions are not specific enough.",[14,79807,79808,79809,457],{},"A custom exception usually inherits from ",[18,79810,47321],{},[36,79812,79814],{"className":38,"code":79813,"language":40,"meta":41,"style":41},"class InvalidUsernameError(Exception):\n    pass\n\n\nusername = \"ab\"\n\nif len(username) \u003C 3:\n    raise InvalidUsernameError(\"username must be at least 3 characters long\")\n",[18,79815,79816,79829,79833,79837,79841,79853,79857,79875],{"__ignoreMap":41},[45,79817,79818,79820,79823,79825,79827],{"class":47,"line":48},[45,79819,2441],{"class":2440},[45,79821,79822],{"class":2444}," InvalidUsernameError",[45,79824,96],{"class":85},[45,79826,47321],{"class":2109},[45,79828,2464],{"class":85},[45,79830,79831],{"class":47,"line":63},[45,79832,5306],{"class":73},[45,79834,79835],{"class":47,"line":70},[45,79836,67],{"emptyLinePlaceholder":66},[45,79838,79839],{"class":47,"line":89},[45,79840,67],{"emptyLinePlaceholder":66},[45,79842,79843,79845,79847,79849,79851],{"class":47,"line":112},[45,79844,41765],{"class":51},[45,79846,56],{"class":55},[45,79848,1565],{"class":99},[45,79850,23198],{"class":103},[45,79852,1571],{"class":99},[45,79854,79855],{"class":47,"line":126},[45,79856,67],{"emptyLinePlaceholder":66},[45,79858,79859,79861,79863,79865,79867,79869,79871,79873],{"class":47,"line":142},[45,79860,20],{"class":73},[45,79862,9139],{"class":92},[45,79864,96],{"class":85},[45,79866,7977],{"class":2070},[45,79868,9146],{"class":85},[45,79870,34378],{"class":55},[45,79872,777],{"class":59},[45,79874,86],{"class":85},[45,79876,79877,79879,79881,79883,79885,79888,79890],{"class":47,"line":149},[45,79878,78661],{"class":73},[45,79880,79822],{"class":2070},[45,79882,96],{"class":85},[45,79884,100],{"class":99},[45,79886,79887],{"class":103},"username must be at least 3 characters long",[45,79889,100],{"class":99},[45,79891,109],{"class":85},[14,79893,79894],{},"Custom exceptions make your code easier to read because the error name explains the problem.",[31,79896,79898],{"id":79897},"how-to-write-useful-exception-messages","How to write useful exception messages",[14,79900,79901],{},"A good exception message should help someone fix the problem quickly.",[14,79903,79904],{},"Tips:",[195,79906,79907,79910,79913,79916],{},[198,79908,79909],{},"Say what was wrong",[198,79911,79912],{},"Include the bad value when helpful",[198,79914,79915],{},"Keep the message short",[198,79917,79918,79919],{},"Avoid vague messages like ",[18,79920,79921],{},"\"something went wrong\"",[14,79923,57858],{},[36,79925,79927],{"className":38,"code":79926,"language":40,"meta":41,"style":41},"quantity = -4\n\nif quantity \u003C 0:\n    raise ValueError(f\"quantity cannot be negative: {quantity}\")\n",[18,79928,79929,79939,79943,79956],{"__ignoreMap":41},[45,79930,79931,79933,79935,79937],{"class":47,"line":48},[45,79932,75647],{"class":51},[45,79934,56],{"class":55},[45,79936,28089],{"class":55},[45,79938,13681],{"class":59},[45,79940,79941],{"class":47,"line":63},[45,79942,67],{"emptyLinePlaceholder":66},[45,79944,79945,79947,79950,79952,79954],{"class":47,"line":70},[45,79946,20],{"class":73},[45,79948,79949],{"class":51}," quantity ",[45,79951,79],{"class":55},[45,79953,749],{"class":59},[45,79955,86],{"class":85},[45,79957,79958,79960,79962,79964,79966,79969,79971,79974,79976,79978],{"class":47,"line":89},[45,79959,78661],{"class":73},[45,79961,45892],{"class":2109},[45,79963,96],{"class":85},[45,79965,5545],{"class":2440},[45,79967,79968],{"class":103},"\"quantity cannot be negative: ",[45,79970,5551],{"class":59},[45,79972,79973],{"class":2070},"quantity",[45,79975,5556],{"class":59},[45,79977,100],{"class":103},[45,79979,109],{"class":85},[14,79981,79982],{},"Less helpful example:",[36,79984,79986],{"className":38,"code":79985,"language":40,"meta":41,"style":41},"raise ValueError(\"invalid input\")\n",[18,79987,79988],{"__ignoreMap":41},[45,79989,79990,79992,79994,79996,79998,80001,80003],{"class":47,"line":48},[45,79991,58974],{"class":73},[45,79993,45892],{"class":2109},[45,79995,96],{"class":85},[45,79997,100],{"class":99},[45,79999,80000],{"class":103},"invalid input",[45,80002,100],{"class":99},[45,80004,109],{"class":85},[14,80006,80007],{},"The first message tells you exactly what failed.",[31,80009,5903],{"id":5902},[14,80011,80012,80013,457],{},"Here are common problems beginners run into when using ",[18,80014,58974],{},[195,80016,80017,80020,80025,80028,80039],{},[198,80018,80019],{},"Raising the wrong exception type for the problem",[198,80021,1350,80022,80024],{},[18,80023,58974],{}," for normal program flow instead of real errors",[198,80026,80027],{},"Forgetting to include a helpful message",[198,80029,80030,80031,80033,80034,230,80036,80038],{},"Raising generic ",[18,80032,47321],{}," when ",[18,80035,42097],{},[18,80037,7711],{}," would be clearer",[198,80040,80041],{},"Catching an exception and hiding the real cause",[14,80043,80044],{},"For example, this is usually too general:",[36,80046,80048],{"className":38,"code":80047,"language":40,"meta":41,"style":41},"age = -1\n\nif age \u003C 0:\n    raise Exception(\"bad age\")\n",[18,80049,80050,80060,80064,80076],{"__ignoreMap":41},[45,80051,80052,80054,80056,80058],{"class":47,"line":48},[45,80053,52],{"class":51},[45,80055,56],{"class":55},[45,80057,28089],{"class":55},[45,80059,13036],{"class":59},[45,80061,80062],{"class":47,"line":63},[45,80063,67],{"emptyLinePlaceholder":66},[45,80065,80066,80068,80070,80072,80074],{"class":47,"line":70},[45,80067,20],{"class":73},[45,80069,76],{"class":51},[45,80071,79],{"class":55},[45,80073,749],{"class":59},[45,80075,86],{"class":85},[45,80077,80078,80080,80083,80085,80087,80090,80092],{"class":47,"line":89},[45,80079,78661],{"class":73},[45,80081,80082],{"class":2109}," Exception",[45,80084,96],{"class":85},[45,80086,100],{"class":99},[45,80088,80089],{"class":103},"bad age",[45,80091,100],{"class":99},[45,80093,109],{"class":85},[14,80095,80096],{},"This is clearer:",[36,80098,80099],{"className":38,"code":78628,"language":40,"meta":41,"style":41},[18,80100,80101,80111,80115,80127],{"__ignoreMap":41},[45,80102,80103,80105,80107,80109],{"class":47,"line":48},[45,80104,52],{"class":51},[45,80106,56],{"class":55},[45,80108,28089],{"class":55},[45,80110,13036],{"class":59},[45,80112,80113],{"class":47,"line":63},[45,80114,67],{"emptyLinePlaceholder":66},[45,80116,80117,80119,80121,80123,80125],{"class":47,"line":70},[45,80118,20],{"class":73},[45,80120,76],{"class":51},[45,80122,79],{"class":55},[45,80124,749],{"class":59},[45,80126,86],{"class":85},[45,80128,80129,80131,80133,80135,80137,80139,80141],{"class":47,"line":89},[45,80130,78661],{"class":73},[45,80132,45892],{"class":2109},[45,80134,96],{"class":85},[45,80136,100],{"class":99},[45,80138,78670],{"class":103},[45,80140,100],{"class":99},[45,80142,109],{"class":85},[14,80144,80145],{},"If you are debugging, these commands can help:",[36,80147,80149],{"className":15652,"code":80148,"language":15654,"meta":41,"style":41},"python your_script.py\nprint(type(value), value)\nhelp(ValueError)\nhelp(TypeError)\n",[18,80150,80151,80157,80172,80179],{"__ignoreMap":41},[45,80152,80153,80155],{"class":47,"line":48},[45,80154,40],{"class":2444},[45,80156,16305],{"class":103},[45,80158,80159,80161,80163,80165,80167,80169],{"class":47,"line":63},[45,80160,2065],{"class":92},[45,80162,96],{"class":51},[45,80164,4340],{"class":92},[45,80166,96],{"class":51},[45,80168,12722],{"class":103},[45,80170,80171],{"class":51},"), value)\n",[45,80173,80174,80177],{"class":47,"line":70},[45,80175,80176],{"class":2444},"help(ValueError",[45,80178,109],{"class":51},[45,80180,80181,80184],{"class":47,"line":89},[45,80182,80183],{"class":2444},"help(TypeError",[45,80185,109],{"class":51},[31,80187,2281],{"id":2280},[712,80189,80191],{"id":80190},"what-is-the-difference-between-raise-and-except","What is the difference between raise and except?",[14,80193,80194,80196,80197,80199],{},[18,80195,58974],{}," creates an exception. ",[18,80198,23275],{}," catches an exception.",[712,80201,80203],{"id":80202},"should-i-use-valueerror-or-typeerror","Should I use ValueError or TypeError?",[14,80205,165,80206,80208,80209,80211],{},[18,80207,7711],{}," for the wrong type. Use ",[18,80210,42097],{}," for the right type but a bad value.",[712,80213,80215],{"id":80214},"can-i-raise-my-own-exception-class","Can I raise my own exception class?",[14,80217,80218,80219,80221],{},"Yes. Create a class that inherits from ",[18,80220,47321],{},", then raise it.",[712,80223,80225],{"id":80224},"does-raise-always-stop-the-program","Does raise always stop the program?",[14,80227,80228,80229,240],{},"It stops normal execution unless the exception is caught by ",[18,80230,47136],{},[712,80232,80234],{"id":80233},"can-i-raise-an-exception-without-a-message","Can I raise an exception without a message?",[14,80236,80237],{},"Yes, but a short message is usually better for debugging.",[31,80239,2342],{"id":2341},[195,80241,80242,80246,80251,80257,80261,80265],{},[198,80243,80244],{},[236,80245,35173],{"href":47555},[198,80247,80248],{},[236,80249,80250],{"href":46079},"Using try, except, else, and finally in Python",[198,80252,80253],{},[236,80254,80256],{"href":80255},"\u002Fhow-to\u002Fhow-to-raise-an-exception-in-python","How to raise an exception in Python",[198,80258,80259],{},[236,80260,79264],{"href":46646},[198,80262,80263],{},[236,80264,79268],{"href":79267},[198,80266,80267],{},[236,80268,80269],{"href":78773},"What is an exception in Python?",[2367,80271,80272],{},"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 .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 .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 pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":41,"searchDepth":63,"depth":63,"links":80274},[80275,80276,80277,80278,80279,80286,80287,80288,80289,80290,80291,80292,80299],{"id":33,"depth":63,"text":34},{"id":78682,"depth":63,"text":78683},{"id":78778,"depth":63,"text":78779},{"id":78853,"depth":63,"text":78854},{"id":78925,"depth":63,"text":78926,"children":80280},[80281,80282,80283,80284,80285],{"id":46606,"depth":70,"text":42097},{"id":46563,"depth":70,"text":7711},{"id":46704,"depth":70,"text":45197},{"id":46653,"depth":70,"text":46068},{"id":79202,"depth":70,"text":79203},{"id":79271,"depth":63,"text":79272},{"id":79480,"depth":63,"text":79481},{"id":79719,"depth":63,"text":79720},{"id":79798,"depth":63,"text":79799},{"id":79897,"depth":63,"text":79898},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":80293},[80294,80295,80296,80297,80298],{"id":80190,"depth":70,"text":80191},{"id":80202,"depth":70,"text":80203},{"id":80214,"depth":70,"text":80215},{"id":80224,"depth":70,"text":80225},{"id":80233,"depth":70,"text":80234},{"id":2341,"depth":63,"text":2342},"Master raising exceptions in python in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fraising-exceptions-in-python",{"title":78610,"description":80300},"learn\u002Fraising-exceptions-in-python","ev3wr-iSFaS3ew2WD7jmWB5ytzDOP25fghQJhE5YJEs",{"id":80307,"title":80308,"body":80309,"description":82255,"extension":2399,"meta":82256,"navigation":66,"path":82257,"seo":82258,"stem":82259,"__hash__":82260},"content\u002Flearn\u002Freturn-values-in-python-functions.md","Return Values in Python Functions",{"type":7,"value":80310,"toc":82228},[80311,80314,80317,80322,80329,80392,80394,80402,80412,80416,80419,80422,80438,80440,80504,80506,80520,80522,80539,80544,80548,80551,80553,80567,80570,80658,80660,80673,80676,80680,80683,80746,80748,80756,80759,80780,80783,80788,80792,80800,80812,80815,80964,80966,80991,80994,81023,81026,81032,81036,81044,81046,81097,81099,81111,81117,81124,81167,81169,81177,81183,81187,81190,81262,81264,81281,81284,81287,81398,81400,81423,81426,81430,81433,81436,81453,81455,81542,81544,81552,81554,81577,81580,81643,81645,81657,81660,81666,81668,81671,81677,81680,81742,81744,81752,81755,81785,81793,81796,81872,81874,81891,81894,81901,81907,81962,81964,81973,81979,81985,81988,82044,82056,82059,82133,82141,82148,82150,82154,82157,82161,82167,82171,82179,82183,82186,82190,82196,82198,82222,82225],[10,80312,80308],{"id":80313},"return-values-in-python-functions",[14,80315,80316],{},"A return value is the result a function sends back after it runs. Understanding this is an important step in learning how Python functions work.",[14,80318,1732,80319,80321],{},[18,80320,3461],{}," statement lets a function give data back to the code that called it. You can then store that value, print it, compare it, or use it in another calculation.",[14,80323,52445,80324,80326,80327,25114],{},[18,80325,3461],{},", Python gives back ",[18,80328,5997],{},[36,80330,80332],{"className":38,"code":80331,"language":40,"meta":41,"style":41},"def add(a, b):\n    return a + b\n\nresult = add(2, 3)\nprint(result)\n",[18,80333,80334,80350,80360,80364,80382],{"__ignoreMap":41},[45,80335,80336,80338,80340,80342,80344,80346,80348],{"class":47,"line":48},[45,80337,2757],{"class":2440},[45,80339,11106],{"class":2455},[45,80341,96],{"class":85},[45,80343,236],{"class":3082},[45,80345,3079],{"class":85},[45,80347,11115],{"class":3082},[45,80349,2464],{"class":85},[45,80351,80352,80354,80356,80358],{"class":47,"line":63},[45,80353,4681],{"class":73},[45,80355,11124],{"class":51},[45,80357,8850],{"class":55},[45,80359,11129],{"class":51},[45,80361,80362],{"class":47,"line":70},[45,80363,67],{"emptyLinePlaceholder":66},[45,80365,80366,80368,80370,80372,80374,80376,80378,80380],{"class":47,"line":89},[45,80367,3595],{"class":51},[45,80369,56],{"class":55},[45,80371,11106],{"class":2070},[45,80373,96],{"class":85},[45,80375,9367],{"class":59},[45,80377,3079],{"class":85},[45,80379,777],{"class":59},[45,80381,109],{"class":85},[45,80383,80384,80386,80388,80390],{"class":47,"line":112},[45,80385,2065],{"class":92},[45,80387,96],{"class":85},[45,80389,3620],{"class":2070},[45,80391,109],{"class":85},[14,80393,177],{},[36,80395,80396],{"className":38,"code":11570,"language":40,"meta":41,"style":41},[18,80397,80398],{"__ignoreMap":41},[45,80399,80400],{"class":47,"line":48},[45,80401,11570],{"class":59},[14,80403,165,80404,80406,80407,80409,80410,240],{},[18,80405,3461],{}," when a function should send a value back. Without ",[18,80408,3461],{},", the function gives back ",[18,80411,5997],{},[31,80413,80415],{"id":80414},"what-a-return-value-is","What a return value is",[14,80417,80418],{},"A return value is the result a function sends back.",[14,80420,80421],{},"Here is the basic idea:",[195,80423,80424,80427,80432,80435],{},[198,80425,80426],{},"A function runs some code",[198,80428,80429,80430,65187],{},"It reaches a ",[18,80431,3461],{},[198,80433,80434],{},"Python sends that value back to the caller",[198,80436,80437],{},"The function stops running at that point",[14,80439,2115],{},[36,80441,80443],{"className":38,"code":80442,"language":40,"meta":41,"style":41},"def greet(name):\n    return \"Hello, \" + name\n\nmessage = greet(\"Maya\")\nprint(message)\n",[18,80444,80445,80457,80472,80476,80494],{"__ignoreMap":41},[45,80446,80447,80449,80451,80453,80455],{"class":47,"line":48},[45,80448,2757],{"class":2440},[45,80450,2936],{"class":2455},[45,80452,96],{"class":85},[45,80454,3096],{"class":3082},[45,80456,2464],{"class":85},[45,80458,80459,80461,80463,80466,80468,80470],{"class":47,"line":63},[45,80460,4681],{"class":73},[45,80462,1565],{"class":99},[45,80464,80465],{"class":103},"Hello, ",[45,80467,100],{"class":99},[45,80469,3775],{"class":55},[45,80471,3102],{"class":51},[45,80473,80474],{"class":47,"line":70},[45,80475,67],{"emptyLinePlaceholder":66},[45,80477,80478,80480,80482,80484,80486,80488,80490,80492],{"class":47,"line":89},[45,80479,15507],{"class":51},[45,80481,56],{"class":55},[45,80483,2936],{"class":2070},[45,80485,96],{"class":85},[45,80487,100],{"class":99},[45,80489,5737],{"class":103},[45,80491,100],{"class":99},[45,80493,109],{"class":85},[45,80495,80496,80498,80500,80502],{"class":47,"line":112},[45,80497,2065],{"class":92},[45,80499,96],{"class":85},[45,80501,6462],{"class":2070},[45,80503,109],{"class":85},[14,80505,177],{},[36,80507,80509],{"className":38,"code":80508,"language":40,"meta":41,"style":41},"Hello, Maya\n",[18,80510,80511],{"__ignoreMap":41},[45,80512,80513,80515,80517],{"class":47,"line":48},[45,80514,3226],{"class":51},[45,80516,3079],{"class":85},[45,80518,80519],{"class":51}," Maya\n",[14,80521,6570],{},[195,80523,80524,80529,80534],{},[198,80525,80526,80528],{},[18,80527,12220],{}," calls the function",[198,80530,53238,80531],{},[18,80532,80533],{},"\"Hello, Maya\"",[198,80535,80536,80537],{},"That returned value is stored in ",[18,80538,6462],{},[14,80540,80541,80542,8558],{},"If you are new to functions, it may help to read ",[236,80543,5879],{"href":53315},[31,80545,80547],{"id":80546},"why-return-values-matter","Why return values matter",[14,80549,80550],{},"Return values are useful because they make functions more flexible.",[14,80552,6403],{},[195,80554,80555,80558,80561,80564],{},[198,80556,80557],{},"Use the result of one function in another part of your program",[198,80559,80560],{},"Write reusable functions",[198,80562,80563],{},"Separate calculation from display",[198,80565,80566],{},"Test your code more easily",[14,80568,80569],{},"For example, this function is reusable because it returns a result instead of printing it directly:",[36,80571,80573],{"className":38,"code":80572,"language":40,"meta":41,"style":41},"def square(number):\n    return number * number\n\nresult1 = square(4)\nresult2 = square(10)\n\nprint(result1)\nprint(result2)\n",[18,80574,80575,80588,80598,80602,80617,80632,80636,80647],{"__ignoreMap":41},[45,80576,80577,80579,80582,80584,80586],{"class":47,"line":48},[45,80578,2757],{"class":2440},[45,80580,80581],{"class":2455}," square",[45,80583,96],{"class":85},[45,80585,13546],{"class":3082},[45,80587,2464],{"class":85},[45,80589,80590,80592,80594,80596],{"class":47,"line":63},[45,80591,4681],{"class":73},[45,80593,744],{"class":51},[45,80595,5601],{"class":55},[45,80597,12984],{"class":51},[45,80599,80600],{"class":47,"line":70},[45,80601,67],{"emptyLinePlaceholder":66},[45,80603,80604,80607,80609,80611,80613,80615],{"class":47,"line":89},[45,80605,80606],{"class":51},"result1 ",[45,80608,56],{"class":55},[45,80610,80581],{"class":2070},[45,80612,96],{"class":85},[45,80614,3584],{"class":59},[45,80616,109],{"class":85},[45,80618,80619,80622,80624,80626,80628,80630],{"class":47,"line":112},[45,80620,80621],{"class":51},"result2 ",[45,80623,56],{"class":55},[45,80625,80581],{"class":2070},[45,80627,96],{"class":85},[45,80629,11786],{"class":59},[45,80631,109],{"class":85},[45,80633,80634],{"class":47,"line":126},[45,80635,67],{"emptyLinePlaceholder":66},[45,80637,80638,80640,80642,80645],{"class":47,"line":142},[45,80639,2065],{"class":92},[45,80641,96],{"class":85},[45,80643,80644],{"class":2070},"result1",[45,80646,109],{"class":85},[45,80648,80649,80651,80653,80656],{"class":47,"line":149},[45,80650,2065],{"class":92},[45,80652,96],{"class":85},[45,80654,80655],{"class":2070},"result2",[45,80657,109],{"class":85},[14,80659,177],{},[36,80661,80663],{"className":38,"code":80662,"language":40,"meta":41,"style":41},"16\n100\n",[18,80664,80665,80669],{"__ignoreMap":41},[45,80666,80667],{"class":47,"line":48},[45,80668,2260],{"class":59},[45,80670,80671],{"class":47,"line":63},[45,80672,20327],{"class":59},[14,80674,80675],{},"Because the function returns a value, you can use it in many different places.",[31,80677,80679],{"id":80678},"basic-return-statement-example","Basic return statement example",[14,80681,80682],{},"A simple example is a function that adds two numbers and returns the answer.",[36,80684,80686],{"className":38,"code":80685,"language":40,"meta":41,"style":41},"def add(a, b):\n    return a + b\n\nanswer = add(7, 5)\nprint(answer)\n",[18,80687,80688,80704,80714,80718,80736],{"__ignoreMap":41},[45,80689,80690,80692,80694,80696,80698,80700,80702],{"class":47,"line":48},[45,80691,2757],{"class":2440},[45,80693,11106],{"class":2455},[45,80695,96],{"class":85},[45,80697,236],{"class":3082},[45,80699,3079],{"class":85},[45,80701,11115],{"class":3082},[45,80703,2464],{"class":85},[45,80705,80706,80708,80710,80712],{"class":47,"line":63},[45,80707,4681],{"class":73},[45,80709,11124],{"class":51},[45,80711,8850],{"class":55},[45,80713,11129],{"class":51},[45,80715,80716],{"class":47,"line":70},[45,80717,67],{"emptyLinePlaceholder":66},[45,80719,80720,80722,80724,80726,80728,80730,80732,80734],{"class":47,"line":89},[45,80721,52579],{"class":51},[45,80723,56],{"class":55},[45,80725,11106],{"class":2070},[45,80727,96],{"class":85},[45,80729,33762],{"class":59},[45,80731,3079],{"class":85},[45,80733,1222],{"class":59},[45,80735,109],{"class":85},[45,80737,80738,80740,80742,80744],{"class":47,"line":112},[45,80739,2065],{"class":92},[45,80741,96],{"class":85},[45,80743,52598],{"class":2070},[45,80745,109],{"class":85},[14,80747,177],{},[36,80749,80750],{"className":38,"code":3629,"language":40,"meta":41,"style":41},[18,80751,80752],{"__ignoreMap":41},[45,80753,80754],{"class":47,"line":48},[45,80755,3629],{"class":59},[14,80757,80758],{},"What matters here:",[195,80760,80761,80768,80774],{},[198,80762,80763,1316,80765,80767],{},[18,80764,236],{},[18,80766,10384],{}," are the input values",[198,80769,80770,80773],{},[18,80771,80772],{},"return a + b"," sends the answer back",[198,80775,80776,80779],{},[18,80777,80778],{},"answer = add(7, 5)"," saves the returned value",[14,80781,80782],{},"This is different from just printing the result. The value is sent back to the caller, so it can be reused later.",[14,80784,80785,80786,240],{},"If function inputs still feel unclear, see ",[236,80787,5782],{"href":12897},[31,80789,80791],{"id":80790},"return-vs-print","return vs print",[14,80793,80794,80795,1316,80797,80799],{},"Beginners often mix up ",[18,80796,3461],{},[18,80798,2065],{},", but they do different jobs.",[195,80801,80802,80807],{},[198,80803,80804,80806],{},[18,80805,15833],{}," displays a value on the screen",[198,80808,80809,80811],{},[18,80810,3461],{}," sends a value back to the code that called the function",[14,80813,80814],{},"Here is a comparison:",[36,80816,80818],{"className":38,"code":80817,"language":40,"meta":41,"style":41},"def print_sum(a, b):\n    print(a + b)\n\ndef return_sum(a, b):\n    return a + b\n\nx = print_sum(2, 3)\ny = return_sum(2, 3)\n\nprint(\"x:\", x)\nprint(\"y:\", y)\n",[18,80819,80820,80837,80851,80855,80872,80882,80886,80904,80922,80926,80945],{"__ignoreMap":41},[45,80821,80822,80824,80827,80829,80831,80833,80835],{"class":47,"line":48},[45,80823,2757],{"class":2440},[45,80825,80826],{"class":2455}," print_sum",[45,80828,96],{"class":85},[45,80830,236],{"class":3082},[45,80832,3079],{"class":85},[45,80834,11115],{"class":3082},[45,80836,2464],{"class":85},[45,80838,80839,80841,80843,80845,80847,80849],{"class":47,"line":63},[45,80840,93],{"class":92},[45,80842,96],{"class":85},[45,80844,11541],{"class":2070},[45,80846,8850],{"class":55},[45,80848,11115],{"class":2070},[45,80850,109],{"class":85},[45,80852,80853],{"class":47,"line":70},[45,80854,67],{"emptyLinePlaceholder":66},[45,80856,80857,80859,80862,80864,80866,80868,80870],{"class":47,"line":89},[45,80858,2757],{"class":2440},[45,80860,80861],{"class":2455}," return_sum",[45,80863,96],{"class":85},[45,80865,236],{"class":3082},[45,80867,3079],{"class":85},[45,80869,11115],{"class":3082},[45,80871,2464],{"class":85},[45,80873,80874,80876,80878,80880],{"class":47,"line":112},[45,80875,4681],{"class":73},[45,80877,11124],{"class":51},[45,80879,8850],{"class":55},[45,80881,11129],{"class":51},[45,80883,80884],{"class":47,"line":126},[45,80885,67],{"emptyLinePlaceholder":66},[45,80887,80888,80890,80892,80894,80896,80898,80900,80902],{"class":47,"line":142},[45,80889,1169],{"class":51},[45,80891,56],{"class":55},[45,80893,80826],{"class":2070},[45,80895,96],{"class":85},[45,80897,9367],{"class":59},[45,80899,3079],{"class":85},[45,80901,777],{"class":59},[45,80903,109],{"class":85},[45,80905,80906,80908,80910,80912,80914,80916,80918,80920],{"class":47,"line":149},[45,80907,29448],{"class":51},[45,80909,56],{"class":55},[45,80911,80861],{"class":2070},[45,80913,96],{"class":85},[45,80915,9367],{"class":59},[45,80917,3079],{"class":85},[45,80919,777],{"class":59},[45,80921,109],{"class":85},[45,80923,80924],{"class":47,"line":679},[45,80925,67],{"emptyLinePlaceholder":66},[45,80927,80928,80930,80932,80934,80937,80939,80941,80943],{"class":47,"line":686},[45,80929,2065],{"class":92},[45,80931,96],{"class":85},[45,80933,100],{"class":99},[45,80935,80936],{"class":103},"x:",[45,80938,100],{"class":99},[45,80940,3079],{"class":85},[45,80942,25169],{"class":2070},[45,80944,109],{"class":85},[45,80946,80947,80949,80951,80953,80956,80958,80960,80962],{"class":47,"line":2229},[45,80948,2065],{"class":92},[45,80950,96],{"class":85},[45,80952,100],{"class":99},[45,80954,80955],{"class":103},"y:",[45,80957,100],{"class":99},[45,80959,3079],{"class":85},[45,80961,30452],{"class":2070},[45,80963,109],{"class":85},[14,80965,177],{},[36,80967,80969],{"className":38,"code":80968,"language":40,"meta":41,"style":41},"5\nx: None\ny: 5\n",[18,80970,80971,80975,80983],{"__ignoreMap":41},[45,80972,80973],{"class":47,"line":48},[45,80974,11570],{"class":59},[45,80976,80977,80979,80981],{"class":47,"line":63},[45,80978,26156],{"class":51},[45,80980,457],{"class":85},[45,80982,59643],{"class":481},[45,80984,80985,80987,80989],{"class":47,"line":70},[45,80986,29457],{"class":51},[45,80988,457],{"class":85},[45,80990,733],{"class":59},[14,80992,80993],{},"Why this happens:",[195,80995,80996,81005,81012],{},[198,80997,80998,81001,81002,81004],{},[18,80999,81000],{},"print_sum(2, 3)"," displays ",[18,81003,3749],{},", but it does not return anything",[198,81006,81007,81008,80326,81010],{},"Because it has no ",[18,81009,3461],{},[18,81011,5997],{},[198,81013,81014,34444,81017,81019,81020,81022],{},[18,81015,81016],{},"return_sum(2, 3)",[18,81018,3749],{},", so ",[18,81021,29457],{}," stores that value",[14,81024,81025],{},"A function can print and return, but they are not the same thing. This confusion often causes bugs.",[14,81027,81028,81029,240],{},"If you want a deeper look at printing, see ",[236,81030,59138],{"href":81031},"\u002Freference\u002Fpython-print-function-explained",[31,81033,81035],{"id":81034},"what-happens-when-a-function-returns-nothing","What happens when a function returns nothing",[14,81037,81038,81039,81041,81042,240],{},"If a function has no ",[18,81040,3461],{}," statement, Python returns ",[18,81043,5997],{},[14,81045,466],{},[36,81047,81049],{"className":38,"code":81048,"language":40,"meta":41,"style":41},"def say_hello():\n    print(\"Hello\")\n\nresult = say_hello()\nprint(result)\n",[18,81050,81051,81059,81073,81077,81087],{"__ignoreMap":41},[45,81052,81053,81055,81057],{"class":47,"line":48},[45,81054,2757],{"class":2440},[45,81056,4596],{"class":2455},[45,81058,3828],{"class":85},[45,81060,81061,81063,81065,81067,81069,81071],{"class":47,"line":63},[45,81062,93],{"class":92},[45,81064,96],{"class":85},[45,81066,100],{"class":99},[45,81068,3226],{"class":103},[45,81070,100],{"class":99},[45,81072,109],{"class":85},[45,81074,81075],{"class":47,"line":70},[45,81076,67],{"emptyLinePlaceholder":66},[45,81078,81079,81081,81083,81085],{"class":47,"line":89},[45,81080,3595],{"class":51},[45,81082,56],{"class":55},[45,81084,4596],{"class":2070},[45,81086,2495],{"class":85},[45,81088,81089,81091,81093,81095],{"class":47,"line":112},[45,81090,2065],{"class":92},[45,81092,96],{"class":85},[45,81094,3620],{"class":2070},[45,81096,109],{"class":85},[14,81098,177],{},[36,81100,81101],{"className":38,"code":52507,"language":40,"meta":41,"style":41},[18,81102,81103,81107],{"__ignoreMap":41},[45,81104,81105],{"class":47,"line":48},[45,81106,15823],{"class":51},[45,81108,81109],{"class":47,"line":63},[45,81110,31576],{"class":481},[14,81112,81113,81114,81116],{},"The function prints ",[18,81115,6701],{},", but it does not return a useful value.",[14,81118,47123,81119,81121,81122,457],{},[18,81120,3461],{}," also returns ",[18,81123,5997],{},[36,81125,81127],{"className":38,"code":81126,"language":40,"meta":41,"style":41},"def do_nothing():\n    return\n\nresult = do_nothing()\nprint(result)\n",[18,81128,81129,81138,81143,81147,81157],{"__ignoreMap":41},[45,81130,81131,81133,81136],{"class":47,"line":48},[45,81132,2757],{"class":2440},[45,81134,81135],{"class":2455}," do_nothing",[45,81137,3828],{"class":85},[45,81139,81140],{"class":47,"line":63},[45,81141,81142],{"class":73},"    return\n",[45,81144,81145],{"class":47,"line":70},[45,81146,67],{"emptyLinePlaceholder":66},[45,81148,81149,81151,81153,81155],{"class":47,"line":89},[45,81150,3595],{"class":51},[45,81152,56],{"class":55},[45,81154,81135],{"class":2070},[45,81156,2495],{"class":85},[45,81158,81159,81161,81163,81165],{"class":47,"line":112},[45,81160,2065],{"class":92},[45,81162,96],{"class":85},[45,81164,3620],{"class":2070},[45,81166,109],{"class":85},[14,81168,177],{},[36,81170,81171],{"className":38,"code":31576,"language":40,"meta":41,"style":41},[18,81172,81173],{"__ignoreMap":41},[45,81174,81175],{"class":47,"line":48},[45,81176,31576],{"class":481},[14,81178,81179,81180,81182],{},"This matters because ",[18,81181,5997],{}," is a special value. If you try to use it like a normal number, string, or list, you may get errors.",[31,81184,81186],{"id":81185},"returning-multiple-values","Returning multiple values",[14,81188,81189],{},"Python can return more than one value by separating them with commas.",[36,81191,81193],{"className":38,"code":81192,"language":40,"meta":41,"style":41},"def divide_and_remainder(a, b):\n    return a \u002F\u002F b, a % b\n\nresult = divide_and_remainder(10, 3)\nprint(result)\n",[18,81194,81195,81212,81230,81234,81252],{"__ignoreMap":41},[45,81196,81197,81199,81202,81204,81206,81208,81210],{"class":47,"line":48},[45,81198,2757],{"class":2440},[45,81200,81201],{"class":2455}," divide_and_remainder",[45,81203,96],{"class":85},[45,81205,236],{"class":3082},[45,81207,3079],{"class":85},[45,81209,11115],{"class":3082},[45,81211,2464],{"class":85},[45,81213,81214,81216,81218,81220,81222,81224,81226,81228],{"class":47,"line":63},[45,81215,4681],{"class":73},[45,81217,11124],{"class":51},[45,81219,64365],{"class":55},[45,81221,11115],{"class":51},[45,81223,3079],{"class":85},[45,81225,11124],{"class":51},[45,81227,9329],{"class":55},[45,81229,11129],{"class":51},[45,81231,81232],{"class":47,"line":70},[45,81233,67],{"emptyLinePlaceholder":66},[45,81235,81236,81238,81240,81242,81244,81246,81248,81250],{"class":47,"line":89},[45,81237,3595],{"class":51},[45,81239,56],{"class":55},[45,81241,81201],{"class":2070},[45,81243,96],{"class":85},[45,81245,11786],{"class":59},[45,81247,3079],{"class":85},[45,81249,777],{"class":59},[45,81251,109],{"class":85},[45,81253,81254,81256,81258,81260],{"class":47,"line":112},[45,81255,2065],{"class":92},[45,81257,96],{"class":85},[45,81259,3620],{"class":2070},[45,81261,109],{"class":85},[14,81263,177],{},[36,81265,81267],{"className":38,"code":81266,"language":40,"meta":41,"style":41},"(3, 1)\n",[18,81268,81269],{"__ignoreMap":41},[45,81270,81271,81273,81275,81277,81279],{"class":47,"line":48},[45,81272,96],{"class":85},[45,81274,11184],{"class":59},[45,81276,3079],{"class":85},[45,81278,9030],{"class":59},[45,81280,109],{"class":85},[14,81282,81283],{},"Python packs these values into a tuple.",[14,81285,81286],{},"You can also unpack them into separate variables:",[36,81288,81290],{"className":38,"code":81289,"language":40,"meta":41,"style":41},"def divide_and_remainder(a, b):\n    return a \u002F\u002F b, a % b\n\nquotient, remainder = divide_and_remainder(10, 3)\n\nprint(\"Quotient:\", quotient)\nprint(\"Remainder:\", remainder)\n",[18,81291,81292,81308,81326,81330,81354,81358,81378],{"__ignoreMap":41},[45,81293,81294,81296,81298,81300,81302,81304,81306],{"class":47,"line":48},[45,81295,2757],{"class":2440},[45,81297,81201],{"class":2455},[45,81299,96],{"class":85},[45,81301,236],{"class":3082},[45,81303,3079],{"class":85},[45,81305,11115],{"class":3082},[45,81307,2464],{"class":85},[45,81309,81310,81312,81314,81316,81318,81320,81322,81324],{"class":47,"line":63},[45,81311,4681],{"class":73},[45,81313,11124],{"class":51},[45,81315,64365],{"class":55},[45,81317,11115],{"class":51},[45,81319,3079],{"class":85},[45,81321,11124],{"class":51},[45,81323,9329],{"class":55},[45,81325,11129],{"class":51},[45,81327,81328],{"class":47,"line":70},[45,81329,67],{"emptyLinePlaceholder":66},[45,81331,81332,81335,81337,81340,81342,81344,81346,81348,81350,81352],{"class":47,"line":89},[45,81333,81334],{"class":51},"quotient",[45,81336,3079],{"class":85},[45,81338,81339],{"class":51}," remainder ",[45,81341,56],{"class":55},[45,81343,81201],{"class":2070},[45,81345,96],{"class":85},[45,81347,11786],{"class":59},[45,81349,3079],{"class":85},[45,81351,777],{"class":59},[45,81353,109],{"class":85},[45,81355,81356],{"class":47,"line":112},[45,81357,67],{"emptyLinePlaceholder":66},[45,81359,81360,81362,81364,81366,81369,81371,81373,81376],{"class":47,"line":126},[45,81361,2065],{"class":92},[45,81363,96],{"class":85},[45,81365,100],{"class":99},[45,81367,81368],{"class":103},"Quotient:",[45,81370,100],{"class":99},[45,81372,3079],{"class":85},[45,81374,81375],{"class":2070}," quotient",[45,81377,109],{"class":85},[45,81379,81380,81382,81384,81386,81389,81391,81393,81396],{"class":47,"line":142},[45,81381,2065],{"class":92},[45,81383,96],{"class":85},[45,81385,100],{"class":99},[45,81387,81388],{"class":103},"Remainder:",[45,81390,100],{"class":99},[45,81392,3079],{"class":85},[45,81394,81395],{"class":2070}," remainder",[45,81397,109],{"class":85},[14,81399,177],{},[36,81401,81403],{"className":38,"code":81402,"language":40,"meta":41,"style":41},"Quotient: 3\nRemainder: 1\n",[18,81404,81405,81414],{"__ignoreMap":41},[45,81406,81407,81410,81412],{"class":47,"line":48},[45,81408,81409],{"class":51},"Quotient",[45,81411,457],{"class":85},[45,81413,1481],{"class":59},[45,81415,81416,81419,81421],{"class":47,"line":63},[45,81417,81418],{"class":51},"Remainder",[45,81420,457],{"class":85},[45,81422,12962],{"class":59},[14,81424,81425],{},"This is a simple and common Python pattern.",[31,81427,81429],{"id":81428},"using-returned-values-in-expressions","Using returned values in expressions",[14,81431,81432],{},"A returned value behaves like a normal value.",[14,81434,81435],{},"You can:",[195,81437,81438,81441,81444,81447,81450],{},[198,81439,81440],{},"Store it in a variable",[198,81442,81443],{},"Print it",[198,81445,81446],{},"Compare it",[198,81448,81449],{},"Pass it to another function",[198,81451,81452],{},"Use it in expressions",[14,81454,466],{},[36,81456,81458],{"className":38,"code":81457,"language":40,"meta":41,"style":41},"def double(n):\n    return n * 2\n\ndef add_three(n):\n    return n + 3\n\nresult = add_three(double(4))\nprint(result)\n",[18,81459,81460,81473,81483,81487,81500,81510,81514,81532],{"__ignoreMap":41},[45,81461,81462,81464,81467,81469,81471],{"class":47,"line":48},[45,81463,2757],{"class":2440},[45,81465,81466],{"class":2455}," double",[45,81468,96],{"class":85},[45,81470,8607],{"class":3082},[45,81472,2464],{"class":85},[45,81474,81475,81477,81479,81481],{"class":47,"line":63},[45,81476,4681],{"class":73},[45,81478,8612],{"class":51},[45,81480,5601],{"class":55},[45,81482,13423],{"class":59},[45,81484,81485],{"class":47,"line":70},[45,81486,67],{"emptyLinePlaceholder":66},[45,81488,81489,81491,81494,81496,81498],{"class":47,"line":89},[45,81490,2757],{"class":2440},[45,81492,81493],{"class":2455}," add_three",[45,81495,96],{"class":85},[45,81497,8607],{"class":3082},[45,81499,2464],{"class":85},[45,81501,81502,81504,81506,81508],{"class":47,"line":112},[45,81503,4681],{"class":73},[45,81505,8612],{"class":51},[45,81507,8850],{"class":55},[45,81509,1481],{"class":59},[45,81511,81512],{"class":47,"line":126},[45,81513,67],{"emptyLinePlaceholder":66},[45,81515,81516,81518,81520,81522,81524,81526,81528,81530],{"class":47,"line":142},[45,81517,3595],{"class":51},[45,81519,56],{"class":55},[45,81521,81493],{"class":2070},[45,81523,96],{"class":85},[45,81525,25301],{"class":2070},[45,81527,96],{"class":85},[45,81529,3584],{"class":59},[45,81531,4348],{"class":85},[45,81533,81534,81536,81538,81540],{"class":47,"line":149},[45,81535,2065],{"class":92},[45,81537,96],{"class":85},[45,81539,3620],{"class":2070},[45,81541,109],{"class":85},[14,81543,177],{},[36,81545,81546],{"className":38,"code":29898,"language":40,"meta":41,"style":41},[18,81547,81548],{"__ignoreMap":41},[45,81549,81550],{"class":47,"line":48},[45,81551,29898],{"class":59},[14,81553,77193],{},[195,81555,81556,81563,81570],{},[198,81557,81558,34444,81561],{},[18,81559,81560],{},"double(4)",[18,81562,34347],{},[198,81564,81565,34444,81568],{},[18,81566,81567],{},"add_three(8)",[18,81569,28459],{},[198,81571,81572,81001,81575],{},[18,81573,81574],{},"print(result)",[18,81576,28459],{},[14,81578,81579],{},"You can also compare a returned value:",[36,81581,81583],{"className":38,"code":81582,"language":40,"meta":41,"style":41},"def is_even(number):\n    return number % 2 == 0\n\nprint(is_even(6))\nprint(is_even(7))\n",[18,81584,81585,81597,81611,81615,81629],{"__ignoreMap":41},[45,81586,81587,81589,81591,81593,81595],{"class":47,"line":48},[45,81588,2757],{"class":2440},[45,81590,34946],{"class":2455},[45,81592,96],{"class":85},[45,81594,13546],{"class":3082},[45,81596,2464],{"class":85},[45,81598,81599,81601,81603,81605,81607,81609],{"class":47,"line":63},[45,81600,4681],{"class":73},[45,81602,744],{"class":51},[45,81604,9329],{"class":55},[45,81606,3754],{"class":59},[45,81608,9334],{"class":55},[45,81610,34967],{"class":59},[45,81612,81613],{"class":47,"line":70},[45,81614,67],{"emptyLinePlaceholder":66},[45,81616,81617,81619,81621,81623,81625,81627],{"class":47,"line":89},[45,81618,2065],{"class":92},[45,81620,96],{"class":85},[45,81622,34980],{"class":2070},[45,81624,96],{"class":85},[45,81626,28541],{"class":59},[45,81628,4348],{"class":85},[45,81630,81631,81633,81635,81637,81639,81641],{"class":47,"line":112},[45,81632,2065],{"class":92},[45,81634,96],{"class":85},[45,81636,34980],{"class":2070},[45,81638,96],{"class":85},[45,81640,33762],{"class":59},[45,81642,4348],{"class":85},[14,81644,177],{},[36,81646,81647],{"className":38,"code":22841,"language":40,"meta":41,"style":41},[18,81648,81649,81653],{"__ignoreMap":41},[45,81650,81651],{"class":47,"line":48},[45,81652,2270],{"class":481},[45,81654,81655],{"class":47,"line":63},[45,81656,2265],{"class":481},[14,81658,81659],{},"This is one reason return values are so useful. They let small functions work together.",[14,81661,81662,81663,240],{},"If you want practice building simple functions like these, see ",[236,81664,5892],{"href":81665},"\u002Fhow-to\u002Fhow-to-create-a-simple-function-in-python",[31,81667,1337],{"id":1336},[14,81669,81670],{},"Here are some common mistakes beginners make with return values.",[712,81672,81674,81675],{"id":81673},"forgetting-to-write-return","Forgetting to write ",[18,81676,3461],{},[14,81678,81679],{},"The function calculates a value, but never sends it back.",[36,81681,81683],{"className":38,"code":81682,"language":40,"meta":41,"style":41},"def add(a, b):\n    a + b\n\nresult = add(2, 3)\nprint(result)\n",[18,81684,81685,81701,81710,81714,81732],{"__ignoreMap":41},[45,81686,81687,81689,81691,81693,81695,81697,81699],{"class":47,"line":48},[45,81688,2757],{"class":2440},[45,81690,11106],{"class":2455},[45,81692,96],{"class":85},[45,81694,236],{"class":3082},[45,81696,3079],{"class":85},[45,81698,11115],{"class":3082},[45,81700,2464],{"class":85},[45,81702,81703,81706,81708],{"class":47,"line":63},[45,81704,81705],{"class":51},"    a ",[45,81707,8850],{"class":55},[45,81709,11129],{"class":51},[45,81711,81712],{"class":47,"line":70},[45,81713,67],{"emptyLinePlaceholder":66},[45,81715,81716,81718,81720,81722,81724,81726,81728,81730],{"class":47,"line":89},[45,81717,3595],{"class":51},[45,81719,56],{"class":55},[45,81721,11106],{"class":2070},[45,81723,96],{"class":85},[45,81725,9367],{"class":59},[45,81727,3079],{"class":85},[45,81729,777],{"class":59},[45,81731,109],{"class":85},[45,81733,81734,81736,81738,81740],{"class":47,"line":112},[45,81735,2065],{"class":92},[45,81737,96],{"class":85},[45,81739,3620],{"class":2070},[45,81741,109],{"class":85},[14,81743,177],{},[36,81745,81746],{"className":38,"code":31576,"language":40,"meta":41,"style":41},[18,81747,81748],{"__ignoreMap":41},[45,81749,81750],{"class":47,"line":48},[45,81751,31576],{"class":481},[14,81753,81754],{},"Fix it by returning the value:",[36,81756,81757],{"className":38,"code":11097,"language":40,"meta":41,"style":41},[18,81758,81759,81775],{"__ignoreMap":41},[45,81760,81761,81763,81765,81767,81769,81771,81773],{"class":47,"line":48},[45,81762,2757],{"class":2440},[45,81764,11106],{"class":2455},[45,81766,96],{"class":85},[45,81768,236],{"class":3082},[45,81770,3079],{"class":85},[45,81772,11115],{"class":3082},[45,81774,2464],{"class":85},[45,81776,81777,81779,81781,81783],{"class":47,"line":63},[45,81778,4681],{"class":73},[45,81780,11124],{"class":51},[45,81782,8850],{"class":55},[45,81784,11129],{"class":51},[712,81786,1350,81788,81790,81791],{"id":81787},"using-print-when-you-need-return",[18,81789,2065],{}," when you need ",[18,81792,3461],{},[14,81794,81795],{},"This is a very common problem.",[36,81797,81799],{"className":38,"code":81798,"language":40,"meta":41,"style":41},"def add(a, b):\n    print(a + b)\n\nresult = add(2, 3)\nprint(\"Result:\", result)\n",[18,81800,81801,81817,81831,81835,81853],{"__ignoreMap":41},[45,81802,81803,81805,81807,81809,81811,81813,81815],{"class":47,"line":48},[45,81804,2757],{"class":2440},[45,81806,11106],{"class":2455},[45,81808,96],{"class":85},[45,81810,236],{"class":3082},[45,81812,3079],{"class":85},[45,81814,11115],{"class":3082},[45,81816,2464],{"class":85},[45,81818,81819,81821,81823,81825,81827,81829],{"class":47,"line":63},[45,81820,93],{"class":92},[45,81822,96],{"class":85},[45,81824,11541],{"class":2070},[45,81826,8850],{"class":55},[45,81828,11115],{"class":2070},[45,81830,109],{"class":85},[45,81832,81833],{"class":47,"line":70},[45,81834,67],{"emptyLinePlaceholder":66},[45,81836,81837,81839,81841,81843,81845,81847,81849,81851],{"class":47,"line":89},[45,81838,3595],{"class":51},[45,81840,56],{"class":55},[45,81842,11106],{"class":2070},[45,81844,96],{"class":85},[45,81846,9367],{"class":59},[45,81848,3079],{"class":85},[45,81850,777],{"class":59},[45,81852,109],{"class":85},[45,81854,81855,81857,81859,81861,81864,81866,81868,81870],{"class":47,"line":112},[45,81856,2065],{"class":92},[45,81858,96],{"class":85},[45,81860,100],{"class":99},[45,81862,81863],{"class":103},"Result:",[45,81865,100],{"class":99},[45,81867,3079],{"class":85},[45,81869,10811],{"class":2070},[45,81871,109],{"class":85},[14,81873,177],{},[36,81875,81877],{"className":38,"code":81876,"language":40,"meta":41,"style":41},"5\nResult: None\n",[18,81878,81879,81883],{"__ignoreMap":41},[45,81880,81881],{"class":47,"line":48},[45,81882,11570],{"class":59},[45,81884,81885,81887,81889],{"class":47,"line":63},[45,81886,34219],{"class":51},[45,81888,457],{"class":85},[45,81890,59643],{"class":481},[14,81892,81893],{},"The function prints the answer, but does not return it.",[712,81895,81897,81898,81900],{"id":81896},"writing-code-after-return-and-expecting-it-to-run","Writing code after ",[18,81899,3461],{}," and expecting it to run",[14,81902,81903,81904,81906],{},"Once Python reaches ",[18,81905,3461],{},", the function stops immediately.",[36,81908,81910],{"className":38,"code":81909,"language":40,"meta":41,"style":41},"def example():\n    return \"done\"\n    print(\"This will not run\")\n\nprint(example())\n",[18,81911,81912,81921,81932,81947,81951],{"__ignoreMap":41},[45,81913,81914,81916,81919],{"class":47,"line":48},[45,81915,2757],{"class":2440},[45,81917,81918],{"class":2455}," example",[45,81920,3828],{"class":85},[45,81922,81923,81925,81927,81930],{"class":47,"line":63},[45,81924,4681],{"class":73},[45,81926,1565],{"class":99},[45,81928,81929],{"class":103},"done",[45,81931,1571],{"class":99},[45,81933,81934,81936,81938,81940,81943,81945],{"class":47,"line":70},[45,81935,93],{"class":92},[45,81937,96],{"class":85},[45,81939,100],{"class":99},[45,81941,81942],{"class":103},"This will not run",[45,81944,100],{"class":99},[45,81946,109],{"class":85},[45,81948,81949],{"class":47,"line":89},[45,81950,67],{"emptyLinePlaceholder":66},[45,81952,81953,81955,81957,81960],{"class":47,"line":112},[45,81954,2065],{"class":92},[45,81956,96],{"class":85},[45,81958,81959],{"class":2070},"example",[45,81961,10777],{"class":85},[14,81963,177],{},[36,81965,81967],{"className":38,"code":81966,"language":40,"meta":41,"style":41},"done\n",[18,81968,81969],{"__ignoreMap":41},[45,81970,81971],{"class":47,"line":48},[45,81972,81966],{"class":51},[14,81974,1732,81975,81978],{},[18,81976,81977],{},"print(\"This will not run\")"," line is never reached.",[712,81980,81982,81983],{"id":81981},"trying-to-use-a-function-result-when-the-function-returns-none","Trying to use a function result when the function returns ",[18,81984,5997],{},[14,81986,81987],{},"This often leads to confusing errors.",[36,81989,81991],{"className":38,"code":81990,"language":40,"meta":41,"style":41},"def show_name():\n    print(\"Ana\")\n\nname = show_name()\nprint(name.upper())\n",[18,81992,81993,82002,82016,82020,82030],{"__ignoreMap":41},[45,81994,81995,81997,82000],{"class":47,"line":48},[45,81996,2757],{"class":2440},[45,81998,81999],{"class":2455}," show_name",[45,82001,3828],{"class":85},[45,82003,82004,82006,82008,82010,82012,82014],{"class":47,"line":63},[45,82005,93],{"class":92},[45,82007,96],{"class":85},[45,82009,100],{"class":99},[45,82011,10580],{"class":103},[45,82013,100],{"class":99},[45,82015,109],{"class":85},[45,82017,82018],{"class":47,"line":70},[45,82019,67],{"emptyLinePlaceholder":66},[45,82021,82022,82024,82026,82028],{"class":47,"line":89},[45,82023,1560],{"class":51},[45,82025,56],{"class":55},[45,82027,81999],{"class":2070},[45,82029,2495],{"class":85},[45,82031,82032,82034,82036,82038,82040,82042],{"class":47,"line":112},[45,82033,2065],{"class":92},[45,82035,96],{"class":85},[45,82037,3096],{"class":2070},[45,82039,240],{"class":85},[45,82041,10327],{"class":2070},[45,82043,10777],{"class":85},[14,82045,78260,82046,36998,82048,25,82050,82052,82053,82055],{},[18,82047,3096],{},[18,82049,5997],{},[18,82051,5997],{}," does not have an ",[18,82054,69394],{}," method.",[14,82057,82058],{},"To debug this kind of problem, try checking what the function really returns:",[36,82060,82062],{"className":38,"code":82061,"language":40,"meta":41,"style":41},"print(my_function(2, 3))\nresult = my_function(2, 3); print(result)\nprint(type(my_function(2, 3)))\n",[18,82063,82064,82082,82111],{"__ignoreMap":41},[45,82065,82066,82068,82070,82072,82074,82076,82078,82080],{"class":47,"line":48},[45,82067,2065],{"class":92},[45,82069,96],{"class":85},[45,82071,5275],{"class":2070},[45,82073,96],{"class":85},[45,82075,9367],{"class":59},[45,82077,3079],{"class":85},[45,82079,777],{"class":59},[45,82081,4348],{"class":85},[45,82083,82084,82086,82088,82090,82092,82094,82096,82098,82100,82103,82105,82107,82109],{"class":47,"line":63},[45,82085,3595],{"class":51},[45,82087,56],{"class":55},[45,82089,5299],{"class":2070},[45,82091,96],{"class":85},[45,82093,9367],{"class":59},[45,82095,3079],{"class":85},[45,82097,777],{"class":59},[45,82099,9146],{"class":85},[45,82101,82102],{"class":51},"; ",[45,82104,2065],{"class":92},[45,82106,96],{"class":85},[45,82108,3620],{"class":2070},[45,82110,109],{"class":85},[45,82112,82113,82115,82117,82119,82121,82123,82125,82127,82129,82131],{"class":47,"line":70},[45,82114,2065],{"class":92},[45,82116,96],{"class":85},[45,82118,4340],{"class":2109},[45,82120,96],{"class":85},[45,82122,5275],{"class":2070},[45,82124,96],{"class":85},[45,82126,9367],{"class":59},[45,82128,3079],{"class":85},[45,82130,777],{"class":59},[45,82132,51210],{"class":85},[14,82134,82135,82136,82138,82139,240],{},"If your function returns ",[18,82137,5997],{}," when you expected something else, check whether you forgot to use ",[18,82140,3461],{},[14,82142,82143,82144,240],{},"A related problem is explained in ",[236,82145,82147],{"href":82146},"\u002Ferrors\u002Ftypeerror-nonetype-object-is-not-iterable-fix","TypeError: 'NoneType' object is not iterable",[31,82149,2281],{"id":2280},[712,82151,82153],{"id":82152},"what-does-return-do-in-python","What does return do in Python?",[14,82155,82156],{},"It ends a function and sends a value back to the code that called it.",[712,82158,82160],{"id":82159},"what-happens-if-a-function-has-no-return-statement","What happens if a function has no return statement?",[14,82162,82163,82164,82166],{},"Python returns ",[18,82165,5997],{}," automatically.",[712,82168,82170],{"id":82169},"is-print-the-same-as-return","Is print the same as return?",[14,82172,2302,82173,82175,82176,82178],{},[18,82174,2065],{}," shows something on the screen. ",[18,82177,3461],{}," gives a value back.",[712,82180,82182],{"id":82181},"can-a-function-return-more-than-one-value","Can a function return more than one value?",[14,82184,82185],{},"Yes. Python returns them together, usually as a tuple.",[712,82187,82189],{"id":82188},"does-code-after-return-run","Does code after return run?",[14,82191,82192,82193,82195],{},"No. Once ",[18,82194,3461],{}," runs, the function stops.",[31,82197,2342],{"id":2341},[195,82199,82200,82204,82208,82212,82216],{},[198,82201,82202],{},[236,82203,5879],{"href":53315},[198,82205,82206],{},[236,82207,6318],{"href":12897},[198,82209,82210],{},[236,82211,6328],{"href":81665},[198,82213,82214],{},[236,82215,59138],{"href":81031},[198,82217,82218],{},[236,82219,82221],{"href":82220},"\u002Fglossary\u002Fwhat-is-a-return-value-in-python","What is a return value in Python",[14,82223,82224],{},"Once you understand return values, try writing small functions that calculate something and send the result back. That is a big step toward writing reusable Python code.",[2367,82226,82227],{},"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 .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}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":82229},[82230,82231,82232,82233,82234,82235,82236,82237,82247,82254],{"id":80414,"depth":63,"text":80415},{"id":80546,"depth":63,"text":80547},{"id":80678,"depth":63,"text":80679},{"id":80790,"depth":63,"text":80791},{"id":81034,"depth":63,"text":81035},{"id":81185,"depth":63,"text":81186},{"id":81428,"depth":63,"text":81429},{"id":1336,"depth":63,"text":1337,"children":82238},[82239,82241,82243,82245],{"id":81673,"depth":70,"text":82240},"Forgetting to write return",{"id":81787,"depth":70,"text":82242},"Using print when you need return",{"id":81896,"depth":70,"text":82244},"Writing code after return and expecting it to run",{"id":81981,"depth":70,"text":82246},"Trying to use a function result when the function returns None",{"id":2280,"depth":63,"text":2281,"children":82248},[82249,82250,82251,82252,82253],{"id":82152,"depth":70,"text":82153},{"id":82159,"depth":70,"text":82160},{"id":82169,"depth":70,"text":82170},{"id":82181,"depth":70,"text":82182},{"id":82188,"depth":70,"text":82189},{"id":2341,"depth":63,"text":2342},"Master return values in python functions in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Freturn-values-in-python-functions",{"title":80308,"description":82255},"learn\u002Freturn-values-in-python-functions","WST1vlSylLZuJx0aooPPUXj2ulOMqyCvlpTc0CyOvhM",{"id":82262,"title":82263,"body":82264,"description":84419,"extension":2399,"meta":84420,"navigation":66,"path":3266,"seo":84421,"stem":84422,"__hash__":84423},"content\u002Flearn\u002Fthe-__init__-method-in-python-explained.md","The __init__ Method in Python Explained",{"type":7,"value":82265,"toc":84382},[82266,82272,82277,82289,82291,82402,82404,82417,82424,82429,82445,82450,82456,82470,82475,82477,82483,82524,82526,82547,82552,82556,82559,82578,82580,82663,82665,82674,82683,82688,82694,82699,82718,82723,82725,82850,82852,82866,82869,82886,82890,82896,83051,83053,83071,83074,83080,83085,83089,83163,83165,83174,83178,83289,83294,83298,83461,83463,83482,83488,83490,83493,83523,83526,83607,83612,83640,83646,83651,83662,83664,83706,83708,83755,83761,83766,83771,83774,83788,83794,83796,83801,83838,83841,83894,83900,83902,83959,83962,84000,84003,84005,84046,84048,84051,84104,84106,84130,84132,84247,84249,84300,84302,84306,84309,84315,84321,84329,84334,84340,84343,84349,84352,84354,84373,84379],[10,82267,1732,82269,82271],{"id":82268},"the-__init__-method-in-python-explained",[18,82270,3262],{}," Method in Python Explained",[14,82273,1732,82274,82276],{},[18,82275,3262],{}," method is a special method in a Python class. It runs automatically when you create a new object.",[14,82278,82279,82280,82282,82283,82285,82286,82288],{},"Beginners use ",[18,82281,3262],{}," to give each object its starting data. For example, if you create a ",[18,82284,2570],{}," object, ",[18,82287,3262],{}," can store its name and age right away.",[31,82290,34],{"id":33},[36,82292,82294],{"className":38,"code":82293,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\nmy_dog = Dog(\"Max\", 3)\nprint(my_dog.name)\nprint(my_dog.age)\n",[18,82295,82296,82304,82324,82336,82348,82352,82374,82388],{"__ignoreMap":41},[45,82297,82298,82300,82302],{"class":47,"line":48},[45,82299,2441],{"class":2440},[45,82301,2445],{"class":2444},[45,82303,86],{"class":85},[45,82305,82306,82308,82310,82312,82314,82316,82318,82320,82322],{"class":47,"line":63},[45,82307,2452],{"class":2440},[45,82309,3072],{"class":92},[45,82311,96],{"class":85},[45,82313,2427],{"class":2461},[45,82315,3079],{"class":85},[45,82317,3083],{"class":3082},[45,82319,3079],{"class":85},[45,82321,3305],{"class":3082},[45,82323,2464],{"class":85},[45,82325,82326,82328,82330,82332,82334],{"class":47,"line":70},[45,82327,3090],{"class":2771},[45,82329,240],{"class":85},[45,82331,3096],{"class":3095},[45,82333,3099],{"class":55},[45,82335,3102],{"class":51},[45,82337,82338,82340,82342,82344,82346],{"class":47,"line":89},[45,82339,3090],{"class":2771},[45,82341,240],{"class":85},[45,82343,2082],{"class":3095},[45,82345,3099],{"class":55},[45,82347,3320],{"class":51},[45,82349,82350],{"class":47,"line":112},[45,82351,67],{"emptyLinePlaceholder":66},[45,82353,82354,82356,82358,82360,82362,82364,82366,82368,82370,82372],{"class":47,"line":126},[45,82355,2488],{"class":51},[45,82357,56],{"class":55},[45,82359,2445],{"class":2070},[45,82361,96],{"class":85},[45,82363,100],{"class":99},[45,82365,19306],{"class":103},[45,82367,100],{"class":99},[45,82369,3079],{"class":85},[45,82371,777],{"class":59},[45,82373,109],{"class":85},[45,82375,82376,82378,82380,82382,82384,82386],{"class":47,"line":142},[45,82377,2065],{"class":92},[45,82379,96],{"class":85},[45,82381,2500],{"class":2070},[45,82383,240],{"class":85},[45,82385,3096],{"class":3095},[45,82387,109],{"class":85},[45,82389,82390,82392,82394,82396,82398,82400],{"class":47,"line":149},[45,82391,2065],{"class":92},[45,82393,96],{"class":85},[45,82395,2500],{"class":2070},[45,82397,240],{"class":85},[45,82399,2082],{"class":3095},[45,82401,109],{"class":85},[14,82403,177],{},[36,82405,82407],{"className":38,"code":82406,"language":40,"meta":41,"style":41},"Max\n3\n",[18,82408,82409,82413],{"__ignoreMap":41},[45,82410,82411],{"class":47,"line":48},[45,82412,19415],{"class":51},[45,82414,82415],{"class":47,"line":63},[45,82416,13046],{"class":59},[14,82418,82419,82421,82422,240],{},[18,82420,3262],{}," runs automatically when you create an object. It is commonly used to store starting values on ",[18,82423,2427],{},[31,82425,5754,82427,23029],{"id":82426},"what-__init__-is",[18,82428,3262],{},[195,82430,82431,82436,82439,82442],{},[198,82432,82433,82435],{},[18,82434,3262],{}," is a special method used inside a class.",[198,82437,82438],{},"It runs automatically when a new object is created.",[198,82440,82441],{},"Its main job is to set up the object.",[198,82443,82444],{},"Beginners often call it a constructor, but in Python it is the initializer.",[14,82446,82447,82448,240],{},"If you are new to classes, it helps to first understand ",[236,82449,65472],{"href":2559},[31,82451,2715,82453,82455],{"id":82452},"why-__init__-is-useful",[18,82454,3262],{}," is useful",[195,82457,82458,82461,82464,82467],{},[198,82459,82460],{},"It lets each object start with its own data.",[198,82462,82463],{},"It helps keep related values inside one object.",[198,82465,82466],{},"It makes objects ready to use right after creation.",[198,82468,82469],{},"It avoids setting every attribute manually after creating the object.",[14,82471,45969,82472,82474],{},[18,82473,3262],{},", you would often need to create an object first and then assign values one by one. That works sometimes, but it is less clear and easier to forget.",[31,82476,8707],{"id":8706},[14,82478,82479,82480,82482],{},"You define ",[18,82481,3262],{}," inside a class like this:",[36,82484,82486],{"className":38,"code":82485,"language":40,"meta":41,"style":41},"class Person:\n    def __init__(self, name):\n        self.name = name\n",[18,82487,82488,82496,82512],{"__ignoreMap":41},[45,82489,82490,82492,82494],{"class":47,"line":48},[45,82491,2441],{"class":2440},[45,82493,2927],{"class":2444},[45,82495,86],{"class":85},[45,82497,82498,82500,82502,82504,82506,82508,82510],{"class":47,"line":63},[45,82499,2452],{"class":2440},[45,82501,3072],{"class":92},[45,82503,96],{"class":85},[45,82505,2427],{"class":2461},[45,82507,3079],{"class":85},[45,82509,3083],{"class":3082},[45,82511,2464],{"class":85},[45,82513,82514,82516,82518,82520,82522],{"class":47,"line":70},[45,82515,3090],{"class":2771},[45,82517,240],{"class":85},[45,82519,3096],{"class":3095},[45,82521,3099],{"class":55},[45,82523,3102],{"class":51},[14,82525,77305],{},[195,82527,82528,82534,82539,82544],{},[198,82529,82530,82533],{},[18,82531,82532],{},"def __init__(self, ...):"," defines the initializer.",[198,82535,82536,82537,240],{},"The first parameter is usually ",[18,82538,2427],{},[198,82540,82541,82543],{},[18,82542,2427],{}," refers to the specific object being created.",[198,82545,82546],{},"Extra parameters are values passed in when creating the object.",[14,82548,82549,82550,240],{},"When you want a clearer review of parameters, see ",[236,82551,5782],{"href":12897},[31,82553,82555],{"id":82554},"how-object-creation-works","How object creation works",[14,82557,82558],{},"When you create an object, Python does these steps:",[346,82560,82561,82564,82567,82570],{},[198,82562,82563],{},"You write a class definition.",[198,82565,82566],{},"You create an object by calling the class like a function.",[198,82568,82569],{},"Python makes the new object.",[198,82571,82572,82573,82575,82576,240],{},"Python then calls ",[18,82574,3262],{}," automatically with that object as ",[18,82577,2427],{},[14,82579,466],{},[36,82581,82583],{"className":38,"code":82582,"language":40,"meta":41,"style":41},"class Car:\n    def __init__(self, brand):\n        self.brand = brand\n\nmy_car = Car(\"Toyota\")\nprint(my_car.brand)\n",[18,82584,82585,82593,82610,82624,82628,82648],{"__ignoreMap":41},[45,82586,82587,82589,82591],{"class":47,"line":48},[45,82588,2441],{"class":2440},[45,82590,21297],{"class":2444},[45,82592,86],{"class":85},[45,82594,82595,82597,82599,82601,82603,82605,82608],{"class":47,"line":63},[45,82596,2452],{"class":2440},[45,82598,3072],{"class":92},[45,82600,96],{"class":85},[45,82602,2427],{"class":2461},[45,82604,3079],{"class":85},[45,82606,82607],{"class":3082}," brand",[45,82609,2464],{"class":85},[45,82611,82612,82614,82616,82619,82621],{"class":47,"line":70},[45,82613,3090],{"class":2771},[45,82615,240],{"class":85},[45,82617,82618],{"class":3095},"brand",[45,82620,3099],{"class":55},[45,82622,82623],{"class":51}," brand\n",[45,82625,82626],{"class":47,"line":89},[45,82627,67],{"emptyLinePlaceholder":66},[45,82629,82630,82633,82635,82637,82639,82641,82644,82646],{"class":47,"line":112},[45,82631,82632],{"class":51},"my_car ",[45,82634,56],{"class":55},[45,82636,21297],{"class":2070},[45,82638,96],{"class":85},[45,82640,100],{"class":99},[45,82642,82643],{"class":103},"Toyota",[45,82645,100],{"class":99},[45,82647,109],{"class":85},[45,82649,82650,82652,82654,82657,82659,82661],{"class":47,"line":126},[45,82651,2065],{"class":92},[45,82653,96],{"class":85},[45,82655,82656],{"class":2070},"my_car",[45,82658,240],{"class":85},[45,82660,82618],{"class":3095},[45,82662,109],{"class":85},[14,82664,177],{},[36,82666,82668],{"className":38,"code":82667,"language":40,"meta":41,"style":41},"Toyota\n",[18,82669,82670],{"__ignoreMap":41},[45,82671,82672],{"class":47,"line":48},[45,82673,82667],{"class":51},[14,82675,82676,82677,82680,82681,240],{},"Even though you wrote ",[18,82678,82679],{},"Car(\"Toyota\")",", Python automatically passes the new object as ",[18,82682,2427],{},[14,82684,82685,82686,240],{},"If you want more practice with this step, see ",[236,82687,38067],{"href":33431},[31,82689,82691,82692],{"id":82690},"setting-attributes-with-self","Setting attributes with ",[18,82693,2427],{},[14,82695,5493,82696,82698],{},[18,82697,3262],{},", you usually save values like this:",[36,82700,82702],{"className":38,"code":82701,"language":40,"meta":41,"style":41},"self.attribute_name = value\n",[18,82703,82704],{"__ignoreMap":41},[45,82705,82706,82708,82710,82713,82715],{"class":47,"line":48},[45,82707,2427],{"class":2771},[45,82709,240],{"class":85},[45,82711,82712],{"class":3095},"attribute_name",[45,82714,3099],{"class":55},[45,82716,82717],{"class":51}," value\n",[14,82719,82720,82721,240],{},"These saved values are called ",[2421,82722,32195],{},[14,82724,466],{},[36,82726,82728],{"className":38,"code":82727,"language":40,"meta":41,"style":41},"class Book:\n    def __init__(self, title, pages):\n        self.title = title\n        self.pages = pages\n\nmy_book = Book(\"Python Basics\", 200)\n\nprint(my_book.title)\nprint(my_book.pages)\n",[18,82729,82730,82739,82761,82774,82788,82792,82817,82821,82836],{"__ignoreMap":41},[45,82731,82732,82734,82737],{"class":47,"line":48},[45,82733,2441],{"class":2440},[45,82735,82736],{"class":2444}," Book",[45,82738,86],{"class":85},[45,82740,82741,82743,82745,82747,82749,82751,82754,82756,82759],{"class":47,"line":63},[45,82742,2452],{"class":2440},[45,82744,3072],{"class":92},[45,82746,96],{"class":85},[45,82748,2427],{"class":2461},[45,82750,3079],{"class":85},[45,82752,82753],{"class":3082}," title",[45,82755,3079],{"class":85},[45,82757,82758],{"class":3082}," pages",[45,82760,2464],{"class":85},[45,82762,82763,82765,82767,82769,82771],{"class":47,"line":70},[45,82764,3090],{"class":2771},[45,82766,240],{"class":85},[45,82768,26282],{"class":3095},[45,82770,3099],{"class":55},[45,82772,82773],{"class":51}," title\n",[45,82775,82776,82778,82780,82783,82785],{"class":47,"line":89},[45,82777,3090],{"class":2771},[45,82779,240],{"class":85},[45,82781,82782],{"class":3095},"pages",[45,82784,3099],{"class":55},[45,82786,82787],{"class":51}," pages\n",[45,82789,82790],{"class":47,"line":112},[45,82791,67],{"emptyLinePlaceholder":66},[45,82793,82794,82797,82799,82801,82803,82805,82808,82810,82812,82815],{"class":47,"line":126},[45,82795,82796],{"class":51},"my_book ",[45,82798,56],{"class":55},[45,82800,82736],{"class":2070},[45,82802,96],{"class":85},[45,82804,100],{"class":99},[45,82806,82807],{"class":103},"Python Basics",[45,82809,100],{"class":99},[45,82811,3079],{"class":85},[45,82813,82814],{"class":59}," 200",[45,82816,109],{"class":85},[45,82818,82819],{"class":47,"line":142},[45,82820,67],{"emptyLinePlaceholder":66},[45,82822,82823,82825,82827,82830,82832,82834],{"class":47,"line":149},[45,82824,2065],{"class":92},[45,82826,96],{"class":85},[45,82828,82829],{"class":2070},"my_book",[45,82831,240],{"class":85},[45,82833,26282],{"class":3095},[45,82835,109],{"class":85},[45,82837,82838,82840,82842,82844,82846,82848],{"class":47,"line":679},[45,82839,2065],{"class":92},[45,82841,96],{"class":85},[45,82843,82829],{"class":2070},[45,82845,240],{"class":85},[45,82847,82782],{"class":3095},[45,82849,109],{"class":85},[14,82851,177],{},[36,82853,82855],{"className":38,"code":82854,"language":40,"meta":41,"style":41},"Python Basics\n200\n",[18,82856,82857,82862],{"__ignoreMap":41},[45,82858,82859],{"class":47,"line":48},[45,82860,82861],{"class":51},"Python Basics\n",[45,82863,82864],{"class":47,"line":63},[45,82865,20447],{"class":59},[14,82867,82868],{},"Key idea:",[195,82870,82871,82877,82883],{},[198,82872,82873,82876],{},[18,82874,82875],{},"self.title = title"," saves the value on the object.",[198,82878,82879,82882],{},[18,82880,82881],{},"self.pages = pages"," does the same for pages.",[198,82884,82885],{},"These attributes can be used later in other methods.",[31,82887,82889],{"id":82888},"example-with-two-objects","Example with two objects",[14,82891,82892,82893,82895],{},"A big reason to use ",[18,82894,3262],{}," is that each object can have its own values.",[36,82897,82899],{"className":38,"code":82898,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\ndog1 = Dog(\"Max\", 3)\ndog2 = Dog(\"Bella\", 5)\n\nprint(dog1.name, dog1.age)\nprint(dog2.name, dog2.age)\n",[18,82900,82901,82909,82929,82941,82953,82957,82979,83001,83005,83028],{"__ignoreMap":41},[45,82902,82903,82905,82907],{"class":47,"line":48},[45,82904,2441],{"class":2440},[45,82906,2445],{"class":2444},[45,82908,86],{"class":85},[45,82910,82911,82913,82915,82917,82919,82921,82923,82925,82927],{"class":47,"line":63},[45,82912,2452],{"class":2440},[45,82914,3072],{"class":92},[45,82916,96],{"class":85},[45,82918,2427],{"class":2461},[45,82920,3079],{"class":85},[45,82922,3083],{"class":3082},[45,82924,3079],{"class":85},[45,82926,3305],{"class":3082},[45,82928,2464],{"class":85},[45,82930,82931,82933,82935,82937,82939],{"class":47,"line":70},[45,82932,3090],{"class":2771},[45,82934,240],{"class":85},[45,82936,3096],{"class":3095},[45,82938,3099],{"class":55},[45,82940,3102],{"class":51},[45,82942,82943,82945,82947,82949,82951],{"class":47,"line":89},[45,82944,3090],{"class":2771},[45,82946,240],{"class":85},[45,82948,2082],{"class":3095},[45,82950,3099],{"class":55},[45,82952,3320],{"class":51},[45,82954,82955],{"class":47,"line":112},[45,82956,67],{"emptyLinePlaceholder":66},[45,82958,82959,82961,82963,82965,82967,82969,82971,82973,82975,82977],{"class":47,"line":126},[45,82960,19541],{"class":51},[45,82962,56],{"class":55},[45,82964,2445],{"class":2070},[45,82966,96],{"class":85},[45,82968,100],{"class":99},[45,82970,19306],{"class":103},[45,82972,100],{"class":99},[45,82974,3079],{"class":85},[45,82976,777],{"class":59},[45,82978,109],{"class":85},[45,82980,82981,82983,82985,82987,82989,82991,82993,82995,82997,82999],{"class":47,"line":142},[45,82982,19564],{"class":51},[45,82984,56],{"class":55},[45,82986,2445],{"class":2070},[45,82988,96],{"class":85},[45,82990,100],{"class":99},[45,82992,19326],{"class":103},[45,82994,100],{"class":99},[45,82996,3079],{"class":85},[45,82998,1222],{"class":59},[45,83000,109],{"class":85},[45,83002,83003],{"class":47,"line":149},[45,83004,67],{"emptyLinePlaceholder":66},[45,83006,83007,83009,83011,83013,83015,83017,83019,83022,83024,83026],{"class":47,"line":679},[45,83008,2065],{"class":92},[45,83010,96],{"class":85},[45,83012,19595],{"class":2070},[45,83014,240],{"class":85},[45,83016,3096],{"class":3095},[45,83018,3079],{"class":85},[45,83020,83021],{"class":2070}," dog1",[45,83023,240],{"class":85},[45,83025,2082],{"class":3095},[45,83027,109],{"class":85},[45,83029,83030,83032,83034,83036,83038,83040,83042,83045,83047,83049],{"class":47,"line":686},[45,83031,2065],{"class":92},[45,83033,96],{"class":85},[45,83035,19610],{"class":2070},[45,83037,240],{"class":85},[45,83039,3096],{"class":3095},[45,83041,3079],{"class":85},[45,83043,83044],{"class":2070}," dog2",[45,83046,240],{"class":85},[45,83048,2082],{"class":3095},[45,83050,109],{"class":85},[14,83052,177],{},[36,83054,83056],{"className":38,"code":83055,"language":40,"meta":41,"style":41},"Max 3\nBella 5\n",[18,83057,83058,83064],{"__ignoreMap":41},[45,83059,83060,83062],{"class":47,"line":48},[45,83061,33019],{"class":51},[45,83063,13046],{"class":59},[45,83065,83066,83069],{"class":47,"line":63},[45,83067,83068],{"class":51},"Bella ",[45,83070,11570],{"class":59},[14,83072,83073],{},"Both objects come from the same class, but each one stores different data.",[31,83075,83077,83078],{"id":83076},"parameters-in-__init__","Parameters in ",[18,83079,3262],{},[14,83081,83082,83084],{},[18,83083,3262],{}," can take zero, one, or many extra parameters.",[712,83086,83088],{"id":83087},"no-extra-parameters","No extra parameters",[36,83090,83092],{"className":38,"code":83091,"language":40,"meta":41,"style":41},"class Robot:\n    def __init__(self):\n        self.status = \"ready\"\n\nr1 = Robot()\nprint(r1.status)\n",[18,83093,83094,83103,83115,83133,83137,83148],{"__ignoreMap":41},[45,83095,83096,83098,83101],{"class":47,"line":48},[45,83097,2441],{"class":2440},[45,83099,83100],{"class":2444}," Robot",[45,83102,86],{"class":85},[45,83104,83105,83107,83109,83111,83113],{"class":47,"line":63},[45,83106,2452],{"class":2440},[45,83108,3072],{"class":92},[45,83110,96],{"class":85},[45,83112,2427],{"class":2461},[45,83114,2464],{"class":85},[45,83116,83117,83119,83121,83124,83126,83128,83131],{"class":47,"line":70},[45,83118,3090],{"class":2771},[45,83120,240],{"class":85},[45,83122,83123],{"class":3095},"status",[45,83125,3099],{"class":55},[45,83127,1565],{"class":99},[45,83129,83130],{"class":103},"ready",[45,83132,1571],{"class":99},[45,83134,83135],{"class":47,"line":89},[45,83136,67],{"emptyLinePlaceholder":66},[45,83138,83139,83142,83144,83146],{"class":47,"line":112},[45,83140,83141],{"class":51},"r1 ",[45,83143,56],{"class":55},[45,83145,83100],{"class":2070},[45,83147,2495],{"class":85},[45,83149,83150,83152,83154,83157,83159,83161],{"class":47,"line":126},[45,83151,2065],{"class":92},[45,83153,96],{"class":85},[45,83155,83156],{"class":2070},"r1",[45,83158,240],{"class":85},[45,83160,83123],{"class":3095},[45,83162,109],{"class":85},[14,83164,177],{},[36,83166,83168],{"className":38,"code":83167,"language":40,"meta":41,"style":41},"ready\n",[18,83169,83170],{"__ignoreMap":41},[45,83171,83172],{"class":47,"line":48},[45,83173,83167],{"class":51},[712,83175,83177],{"id":83176},"one-or-more-required-parameters","One or more required parameters",[36,83179,83181],{"className":38,"code":83180,"language":40,"meta":41,"style":41},"class Student:\n    def __init__(self, name, grade):\n        self.name = name\n        self.grade = grade\n\ns1 = Student(\"Ava\", 8)\nprint(s1.name)\nprint(s1.grade)\n",[18,83182,83183,83191,83211,83223,83235,83239,83261,83275],{"__ignoreMap":41},[45,83184,83185,83187,83189],{"class":47,"line":48},[45,83186,2441],{"class":2440},[45,83188,20914],{"class":2444},[45,83190,86],{"class":85},[45,83192,83193,83195,83197,83199,83201,83203,83205,83207,83209],{"class":47,"line":63},[45,83194,2452],{"class":2440},[45,83196,3072],{"class":92},[45,83198,96],{"class":85},[45,83200,2427],{"class":2461},[45,83202,3079],{"class":85},[45,83204,3083],{"class":3082},[45,83206,3079],{"class":85},[45,83208,38431],{"class":3082},[45,83210,2464],{"class":85},[45,83212,83213,83215,83217,83219,83221],{"class":47,"line":70},[45,83214,3090],{"class":2771},[45,83216,240],{"class":85},[45,83218,3096],{"class":3095},[45,83220,3099],{"class":55},[45,83222,3102],{"class":51},[45,83224,83225,83227,83229,83231,83233],{"class":47,"line":89},[45,83226,3090],{"class":2771},[45,83228,240],{"class":85},[45,83230,38454],{"class":3095},[45,83232,3099],{"class":55},[45,83234,38459],{"class":51},[45,83236,83237],{"class":47,"line":112},[45,83238,67],{"emptyLinePlaceholder":66},[45,83240,83241,83243,83245,83247,83249,83251,83253,83255,83257,83259],{"class":47,"line":126},[45,83242,20973],{"class":51},[45,83244,56],{"class":55},[45,83246,20914],{"class":2070},[45,83248,96],{"class":85},[45,83250,100],{"class":99},[45,83252,3162],{"class":103},[45,83254,100],{"class":99},[45,83256,3079],{"class":85},[45,83258,1254],{"class":59},[45,83260,109],{"class":85},[45,83262,83263,83265,83267,83269,83271,83273],{"class":47,"line":142},[45,83264,2065],{"class":92},[45,83266,96],{"class":85},[45,83268,21015],{"class":2070},[45,83270,240],{"class":85},[45,83272,3096],{"class":3095},[45,83274,109],{"class":85},[45,83276,83277,83279,83281,83283,83285,83287],{"class":47,"line":149},[45,83278,2065],{"class":92},[45,83280,96],{"class":85},[45,83282,21015],{"class":2070},[45,83284,240],{"class":85},[45,83286,38454],{"class":3095},[45,83288,109],{"class":85},[14,83290,83291,83292,240],{},"If required parameters are missing, Python raises a ",[18,83293,7711],{},[712,83295,83297],{"id":83296},"default-values-for-optional-parameters","Default values for optional parameters",[36,83299,83301],{"className":38,"code":83300,"language":40,"meta":41,"style":41},"class User:\n    def __init__(self, username, active=True):\n        self.username = username\n        self.active = active\n\nuser1 = User(\"sam\")\nuser2 = User(\"lee\", False)\n\nprint(user1.username, user1.active)\nprint(user2.username, user2.active)\n",[18,83302,83303,83311,83335,83347,83361,83365,83384,83409,83413,83437],{"__ignoreMap":41},[45,83304,83305,83307,83309],{"class":47,"line":48},[45,83306,2441],{"class":2440},[45,83308,20515],{"class":2444},[45,83310,86],{"class":85},[45,83312,83313,83315,83317,83319,83321,83323,83325,83327,83329,83331,83333],{"class":47,"line":63},[45,83314,2452],{"class":2440},[45,83316,3072],{"class":92},[45,83318,96],{"class":85},[45,83320,2427],{"class":2461},[45,83322,3079],{"class":85},[45,83324,20532],{"class":3082},[45,83326,3079],{"class":85},[45,83328,8151],{"class":3082},[45,83330,56],{"class":55},[45,83332,229],{"class":481},[45,83334,2464],{"class":85},[45,83336,83337,83339,83341,83343,83345],{"class":47,"line":70},[45,83338,3090],{"class":2771},[45,83340,240],{"class":85},[45,83342,7977],{"class":3095},[45,83344,3099],{"class":55},[45,83346,20552],{"class":51},[45,83348,83349,83351,83353,83356,83358],{"class":47,"line":89},[45,83350,3090],{"class":2771},[45,83352,240],{"class":85},[45,83354,83355],{"class":3095},"active",[45,83357,3099],{"class":55},[45,83359,83360],{"class":51}," active\n",[45,83362,83363],{"class":47,"line":112},[45,83364,67],{"emptyLinePlaceholder":66},[45,83366,83367,83370,83372,83374,83376,83378,83380,83382],{"class":47,"line":126},[45,83368,83369],{"class":51},"user1 ",[45,83371,56],{"class":55},[45,83373,20515],{"class":2070},[45,83375,96],{"class":85},[45,83377,100],{"class":99},[45,83379,31741],{"class":103},[45,83381,100],{"class":99},[45,83383,109],{"class":85},[45,83385,83386,83389,83391,83393,83395,83397,83400,83402,83404,83407],{"class":47,"line":142},[45,83387,83388],{"class":51},"user2 ",[45,83390,56],{"class":55},[45,83392,20515],{"class":2070},[45,83394,96],{"class":85},[45,83396,100],{"class":99},[45,83398,83399],{"class":103},"lee",[45,83401,100],{"class":99},[45,83403,3079],{"class":85},[45,83405,83406],{"class":481}," False",[45,83408,109],{"class":85},[45,83410,83411],{"class":47,"line":149},[45,83412,67],{"emptyLinePlaceholder":66},[45,83414,83415,83417,83419,83422,83424,83426,83428,83431,83433,83435],{"class":47,"line":679},[45,83416,2065],{"class":92},[45,83418,96],{"class":85},[45,83420,83421],{"class":2070},"user1",[45,83423,240],{"class":85},[45,83425,7977],{"class":3095},[45,83427,3079],{"class":85},[45,83429,83430],{"class":2070}," user1",[45,83432,240],{"class":85},[45,83434,83355],{"class":3095},[45,83436,109],{"class":85},[45,83438,83439,83441,83443,83446,83448,83450,83452,83455,83457,83459],{"class":47,"line":686},[45,83440,2065],{"class":92},[45,83442,96],{"class":85},[45,83444,83445],{"class":2070},"user2",[45,83447,240],{"class":85},[45,83449,7977],{"class":3095},[45,83451,3079],{"class":85},[45,83453,83454],{"class":2070}," user2",[45,83456,240],{"class":85},[45,83458,83355],{"class":3095},[45,83460,109],{"class":85},[14,83462,177],{},[36,83464,83466],{"className":38,"code":83465,"language":40,"meta":41,"style":41},"sam True\nlee False\n",[18,83467,83468,83475],{"__ignoreMap":41},[45,83469,83470,83473],{"class":47,"line":48},[45,83471,83472],{"class":51},"sam ",[45,83474,2270],{"class":481},[45,83476,83477,83480],{"class":47,"line":63},[45,83478,83479],{"class":51},"lee ",[45,83481,2265],{"class":481},[14,83483,83484,83485,240],{},"If you see an argument error here, this guide may help: ",[236,83486,83487],{"href":4024},"fix missing required positional argument errors",[31,83489,5795],{"id":5794},[14,83491,83492],{},"These points cause a lot of mistakes:",[195,83494,83495,83500,83505,83513,83518],{},[198,83496,83497,83499],{},[18,83498,3262],{}," is not usually called directly.",[198,83501,83502,83504],{},[18,83503,2427],{}," is passed automatically when creating an object.",[198,83506,83507,83509,83510,240],{},[18,83508,3262],{}," must be spelled exactly with two underscores before and after ",[18,83511,83512],{},"init",[198,83514,83515,83517],{},[18,83516,3262],{}," does not print the object.",[198,83519,83520,83522],{},[18,83521,3262],{}," does not return the object.",[14,83524,83525],{},"For example, this is normal:",[36,83527,83529],{"className":38,"code":83528,"language":40,"meta":41,"style":41},"class Cat:\n    def __init__(self, name):\n        self.name = name\n\nmy_cat = Cat(\"Milo\")\nprint(my_cat.name)\n",[18,83530,83531,83540,83556,83568,83572,83592],{"__ignoreMap":41},[45,83532,83533,83535,83538],{"class":47,"line":48},[45,83534,2441],{"class":2440},[45,83536,83537],{"class":2444}," Cat",[45,83539,86],{"class":85},[45,83541,83542,83544,83546,83548,83550,83552,83554],{"class":47,"line":63},[45,83543,2452],{"class":2440},[45,83545,3072],{"class":92},[45,83547,96],{"class":85},[45,83549,2427],{"class":2461},[45,83551,3079],{"class":85},[45,83553,3083],{"class":3082},[45,83555,2464],{"class":85},[45,83557,83558,83560,83562,83564,83566],{"class":47,"line":70},[45,83559,3090],{"class":2771},[45,83561,240],{"class":85},[45,83563,3096],{"class":3095},[45,83565,3099],{"class":55},[45,83567,3102],{"class":51},[45,83569,83570],{"class":47,"line":89},[45,83571,67],{"emptyLinePlaceholder":66},[45,83573,83574,83577,83579,83581,83583,83585,83588,83590],{"class":47,"line":112},[45,83575,83576],{"class":51},"my_cat ",[45,83578,56],{"class":55},[45,83580,83537],{"class":2070},[45,83582,96],{"class":85},[45,83584,100],{"class":99},[45,83586,83587],{"class":103},"Milo",[45,83589,100],{"class":99},[45,83591,109],{"class":85},[45,83593,83594,83596,83598,83601,83603,83605],{"class":47,"line":126},[45,83595,2065],{"class":92},[45,83597,96],{"class":85},[45,83599,83600],{"class":2070},"my_cat",[45,83602,240],{"class":85},[45,83604,3096],{"class":3095},[45,83606,109],{"class":85},[14,83608,2854,83609,83611],{},[2421,83610,2857],{}," usually write something like this:",[36,83613,83615],{"className":38,"code":83614,"language":40,"meta":41,"style":41},"# Not the normal beginner pattern\nmy_cat.__init__(\"Milo\")\n",[18,83616,83617,83622],{"__ignoreMap":41},[45,83618,83619],{"class":47,"line":48},[45,83620,83621],{"class":7624},"# Not the normal beginner pattern\n",[45,83623,83624,83626,83628,83630,83632,83634,83636,83638],{"class":47,"line":63},[45,83625,83600],{"class":51},[45,83627,240],{"class":85},[45,83629,3262],{"class":92},[45,83631,96],{"class":85},[45,83633,100],{"class":99},[45,83635,83587],{"class":103},[45,83637,100],{"class":99},[45,83639,109],{"class":85},[31,83641,5754,83643,83645],{"id":83642},"what-__init__-should-return",[18,83644,3262],{}," should return",[14,83647,83648,83650],{},[18,83649,3262],{}," should not return a normal value.",[14,83652,83653,83654,83656,83657,83659,83660,240],{},"It should return ",[18,83655,5997],{}," automatically. In most cases, you do not write any ",[18,83658,3461],{}," statement in ",[18,83661,3262],{},[14,83663,1466],{},[36,83665,83667],{"className":38,"code":83666,"language":40,"meta":41,"style":41},"class Game:\n    def __init__(self, title):\n        self.title = title\n",[18,83668,83669,83678,83694],{"__ignoreMap":41},[45,83670,83671,83673,83676],{"class":47,"line":48},[45,83672,2441],{"class":2440},[45,83674,83675],{"class":2444}," Game",[45,83677,86],{"class":85},[45,83679,83680,83682,83684,83686,83688,83690,83692],{"class":47,"line":63},[45,83681,2452],{"class":2440},[45,83683,3072],{"class":92},[45,83685,96],{"class":85},[45,83687,2427],{"class":2461},[45,83689,3079],{"class":85},[45,83691,82753],{"class":3082},[45,83693,2464],{"class":85},[45,83695,83696,83698,83700,83702,83704],{"class":47,"line":70},[45,83697,3090],{"class":2771},[45,83699,240],{"class":85},[45,83701,26282],{"class":3095},[45,83703,3099],{"class":55},[45,83705,82773],{"class":51},[14,83707,3806],{},[36,83709,83711],{"className":38,"code":83710,"language":40,"meta":41,"style":41},"class Game:\n    def __init__(self, title):\n        self.title = title\n        return title\n",[18,83712,83713,83721,83737,83749],{"__ignoreMap":41},[45,83714,83715,83717,83719],{"class":47,"line":48},[45,83716,2441],{"class":2440},[45,83718,83675],{"class":2444},[45,83720,86],{"class":85},[45,83722,83723,83725,83727,83729,83731,83733,83735],{"class":47,"line":63},[45,83724,2452],{"class":2440},[45,83726,3072],{"class":92},[45,83728,96],{"class":85},[45,83730,2427],{"class":2461},[45,83732,3079],{"class":85},[45,83734,82753],{"class":3082},[45,83736,2464],{"class":85},[45,83738,83739,83741,83743,83745,83747],{"class":47,"line":70},[45,83740,3090],{"class":2771},[45,83742,240],{"class":85},[45,83744,26282],{"class":3095},[45,83746,3099],{"class":55},[45,83748,82773],{"class":51},[45,83750,83751,83753],{"class":47,"line":89},[45,83752,3550],{"class":73},[45,83754,82773],{"class":51},[14,83756,83757,83758,83760],{},"The second example causes an error because ",[18,83759,3262],{}," is only for setup. It is not meant to produce output.",[31,83762,65671,83764],{"id":83763},"when-not-to-use-__init__",[18,83765,3262],{},[14,83767,83768,83770],{},[18,83769,3262],{}," is useful, but it should stay focused on setting up object state.",[14,83772,83773],{},"Avoid these patterns:",[195,83775,83776,83779,83782,83785],{},[198,83777,83778],{},"Do not put unrelated logic there.",[198,83780,83781],{},"Do not make it too long if helper methods would be clearer.",[198,83783,83784],{},"Do not use it when you only need class-level shared data.",[198,83786,83787],{},"Keep it focused on starting values for the object.",[14,83789,83790,83791,240],{},"If you want to add behavior to a class, that usually belongs in normal methods. See ",[236,83792,83793],{"href":4548},"basic methods in Python classes",[31,83795,5903],{"id":5902},[14,83797,83798,83799,457],{},"These are some common causes of problems when using ",[18,83800,3262],{},[195,83802,83803,83813,83819,83822,83828,83833],{},[198,83804,83805,83806,2849,83808,230,83811],{},"Misspelling ",[18,83807,3262],{},[18,83809,83810],{},"_init_",[18,83812,83512],{},[198,83814,83815,83816,83818],{},"Forgetting the ",[18,83817,2427],{}," parameter",[198,83820,83821],{},"Forgetting to pass required arguments when creating an object",[198,83823,83824,83825],{},"Using a local variable instead of ",[18,83826,83827],{},"self.attribute",[198,83829,83830,83831],{},"Trying to return a value from ",[18,83832,3262],{},[198,83834,50822,83835,83837],{},[18,83836,3262],{}," to run without creating an object",[14,83839,83840],{},"Example of a common mistake:",[36,83842,83844],{"className":38,"code":83843,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(name, age):\n        self.name = name\n        self.age = age\n",[18,83845,83846,83854,83870,83882],{"__ignoreMap":41},[45,83847,83848,83850,83852],{"class":47,"line":48},[45,83849,2441],{"class":2440},[45,83851,2445],{"class":2444},[45,83853,86],{"class":85},[45,83855,83856,83858,83860,83862,83864,83866,83868],{"class":47,"line":63},[45,83857,2452],{"class":2440},[45,83859,3072],{"class":92},[45,83861,96],{"class":85},[45,83863,3096],{"class":3082},[45,83865,3079],{"class":85},[45,83867,3305],{"class":3082},[45,83869,2464],{"class":85},[45,83871,83872,83874,83876,83878,83880],{"class":47,"line":70},[45,83873,3090],{"class":2771},[45,83875,240],{"class":85},[45,83877,3096],{"class":3095},[45,83879,3099],{"class":55},[45,83881,3102],{"class":51},[45,83883,83884,83886,83888,83890,83892],{"class":47,"line":89},[45,83885,3090],{"class":2771},[45,83887,240],{"class":85},[45,83889,2082],{"class":3095},[45,83891,3099],{"class":55},[45,83893,3320],{"class":51},[14,83895,83896,83897,83899],{},"This is wrong because ",[18,83898,2427],{}," is missing from the parameter list.",[14,83901,6003],{},[36,83903,83905],{"className":38,"code":83904,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n",[18,83906,83907,83915,83935,83947],{"__ignoreMap":41},[45,83908,83909,83911,83913],{"class":47,"line":48},[45,83910,2441],{"class":2440},[45,83912,2445],{"class":2444},[45,83914,86],{"class":85},[45,83916,83917,83919,83921,83923,83925,83927,83929,83931,83933],{"class":47,"line":63},[45,83918,2452],{"class":2440},[45,83920,3072],{"class":92},[45,83922,96],{"class":85},[45,83924,2427],{"class":2461},[45,83926,3079],{"class":85},[45,83928,3083],{"class":3082},[45,83930,3079],{"class":85},[45,83932,3305],{"class":3082},[45,83934,2464],{"class":85},[45,83936,83937,83939,83941,83943,83945],{"class":47,"line":70},[45,83938,3090],{"class":2771},[45,83940,240],{"class":85},[45,83942,3096],{"class":3095},[45,83944,3099],{"class":55},[45,83946,3102],{"class":51},[45,83948,83949,83951,83953,83955,83957],{"class":47,"line":89},[45,83950,3090],{"class":2771},[45,83952,240],{"class":85},[45,83954,2082],{"class":3095},[45,83956,3099],{"class":55},[45,83958,3320],{"class":51},[14,83960,83961],{},"Another common mistake:",[36,83963,83965],{"className":38,"code":83964,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name):\n        name = name\n",[18,83966,83967,83975,83991],{"__ignoreMap":41},[45,83968,83969,83971,83973],{"class":47,"line":48},[45,83970,2441],{"class":2440},[45,83972,2445],{"class":2444},[45,83974,86],{"class":85},[45,83976,83977,83979,83981,83983,83985,83987,83989],{"class":47,"line":63},[45,83978,2452],{"class":2440},[45,83980,3072],{"class":92},[45,83982,96],{"class":85},[45,83984,2427],{"class":2461},[45,83986,3079],{"class":85},[45,83988,3083],{"class":3082},[45,83990,2464],{"class":85},[45,83992,83993,83996,83998],{"class":47,"line":70},[45,83994,83995],{"class":51},"        name ",[45,83997,56],{"class":55},[45,83999,3102],{"class":51},[14,84001,84002],{},"This does not save the value on the object. It only creates a local variable.",[14,84004,6003],{},[36,84006,84008],{"className":38,"code":84007,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name):\n        self.name = name\n",[18,84009,84010,84018,84034],{"__ignoreMap":41},[45,84011,84012,84014,84016],{"class":47,"line":48},[45,84013,2441],{"class":2440},[45,84015,2445],{"class":2444},[45,84017,86],{"class":85},[45,84019,84020,84022,84024,84026,84028,84030,84032],{"class":47,"line":63},[45,84021,2452],{"class":2440},[45,84023,3072],{"class":92},[45,84025,96],{"class":85},[45,84027,2427],{"class":2461},[45,84029,3079],{"class":85},[45,84031,3083],{"class":3082},[45,84033,2464],{"class":85},[45,84035,84036,84038,84040,84042,84044],{"class":47,"line":70},[45,84037,3090],{"class":2771},[45,84039,240],{"class":85},[45,84041,3096],{"class":3095},[45,84043,3099],{"class":55},[45,84045,3102],{"class":51},[31,84047,4323],{"id":4322},[14,84049,84050],{},"If your object is not behaving as expected, these commands can help:",[36,84052,84054],{"className":38,"code":84053,"language":40,"meta":41,"style":41},"print(type(my_object))\nprint(my_object.__dict__)\nhelp(ClassName)\ndir(my_object)\n",[18,84055,84056,84070,84084,84094],{"__ignoreMap":41},[45,84057,84058,84060,84062,84064,84066,84068],{"class":47,"line":48},[45,84059,2065],{"class":92},[45,84061,96],{"class":85},[45,84063,4340],{"class":2109},[45,84065,96],{"class":85},[45,84067,4345],{"class":2070},[45,84069,4348],{"class":85},[45,84071,84072,84074,84076,84078,84080,84082],{"class":47,"line":63},[45,84073,2065],{"class":92},[45,84075,96],{"class":85},[45,84077,4345],{"class":2070},[45,84079,240],{"class":85},[45,84081,4387],{"class":2771},[45,84083,109],{"class":85},[45,84085,84086,84088,84090,84092],{"class":47,"line":70},[45,84087,2104],{"class":92},[45,84089,96],{"class":85},[45,84091,21722],{"class":2070},[45,84093,109],{"class":85},[45,84095,84096,84098,84100,84102],{"class":47,"line":89},[45,84097,4357],{"class":92},[45,84099,96],{"class":85},[45,84101,4345],{"class":2070},[45,84103,109],{"class":85},[14,84105,21783],{},[195,84107,84108,84113,84118,84124],{},[198,84109,84110,84112],{},[18,84111,4400],{}," shows what class the object belongs to.",[198,84114,84115,84117],{},[18,84116,4418],{}," shows the attributes stored on the object.",[198,84119,84120,84123],{},[18,84121,84122],{},"help(ClassName)"," shows information about the class and its methods.",[198,84125,84126,84129],{},[18,84127,84128],{},"dir(my_object)"," lists available attributes and methods.",[14,84131,466],{},[36,84133,84135],{"className":38,"code":84134,"language":40,"meta":41,"style":41},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\nmy_dog = Dog(\"Max\", 3)\n\nprint(type(my_dog))\nprint(my_dog.__dict__)\n",[18,84136,84137,84145,84165,84177,84189,84193,84215,84219,84233],{"__ignoreMap":41},[45,84138,84139,84141,84143],{"class":47,"line":48},[45,84140,2441],{"class":2440},[45,84142,2445],{"class":2444},[45,84144,86],{"class":85},[45,84146,84147,84149,84151,84153,84155,84157,84159,84161,84163],{"class":47,"line":63},[45,84148,2452],{"class":2440},[45,84150,3072],{"class":92},[45,84152,96],{"class":85},[45,84154,2427],{"class":2461},[45,84156,3079],{"class":85},[45,84158,3083],{"class":3082},[45,84160,3079],{"class":85},[45,84162,3305],{"class":3082},[45,84164,2464],{"class":85},[45,84166,84167,84169,84171,84173,84175],{"class":47,"line":70},[45,84168,3090],{"class":2771},[45,84170,240],{"class":85},[45,84172,3096],{"class":3095},[45,84174,3099],{"class":55},[45,84176,3102],{"class":51},[45,84178,84179,84181,84183,84185,84187],{"class":47,"line":89},[45,84180,3090],{"class":2771},[45,84182,240],{"class":85},[45,84184,2082],{"class":3095},[45,84186,3099],{"class":55},[45,84188,3320],{"class":51},[45,84190,84191],{"class":47,"line":112},[45,84192,67],{"emptyLinePlaceholder":66},[45,84194,84195,84197,84199,84201,84203,84205,84207,84209,84211,84213],{"class":47,"line":126},[45,84196,2488],{"class":51},[45,84198,56],{"class":55},[45,84200,2445],{"class":2070},[45,84202,96],{"class":85},[45,84204,100],{"class":99},[45,84206,19306],{"class":103},[45,84208,100],{"class":99},[45,84210,3079],{"class":85},[45,84212,777],{"class":59},[45,84214,109],{"class":85},[45,84216,84217],{"class":47,"line":142},[45,84218,67],{"emptyLinePlaceholder":66},[45,84220,84221,84223,84225,84227,84229,84231],{"class":47,"line":149},[45,84222,2065],{"class":92},[45,84224,96],{"class":85},[45,84226,4340],{"class":2109},[45,84228,96],{"class":85},[45,84230,2500],{"class":2070},[45,84232,4348],{"class":85},[45,84234,84235,84237,84239,84241,84243,84245],{"class":47,"line":679},[45,84236,2065],{"class":92},[45,84238,96],{"class":85},[45,84240,2500],{"class":2070},[45,84242,240],{"class":85},[45,84244,4387],{"class":2771},[45,84246,109],{"class":85},[14,84248,13456],{},[36,84250,84252],{"className":38,"code":84251,"language":40,"meta":41,"style":41},"\u003Cclass '__main__.Dog'>\n{'name': 'Max', 'age': 3}\n",[18,84253,84254,84268],{"__ignoreMap":41},[45,84255,84256,84258,84260,84262,84264,84266],{"class":47,"line":48},[45,84257,79],{"class":55},[45,84259,2441],{"class":2440},[45,84261,9199],{"class":99},[45,84263,32343],{"class":103},[45,84265,7901],{"class":99},[45,84267,13484],{"class":55},[45,84269,84270,84272,84274,84276,84278,84280,84282,84284,84286,84288,84290,84292,84294,84296,84298],{"class":47,"line":63},[45,84271,5551],{"class":85},[45,84273,7901],{"class":99},[45,84275,3096],{"class":103},[45,84277,7901],{"class":99},[45,84279,457],{"class":85},[45,84281,9199],{"class":99},[45,84283,19306],{"class":103},[45,84285,7901],{"class":99},[45,84287,3079],{"class":85},[45,84289,9199],{"class":99},[45,84291,2082],{"class":103},[45,84293,7901],{"class":99},[45,84295,457],{"class":85},[45,84297,777],{"class":59},[45,84299,8629],{"class":85},[31,84301,2281],{"id":2280},[712,84303,350,84304,39165],{"id":39162},[18,84305,3262],{},[14,84307,84308],{},"No. You only need it when your object needs setup or starting values.",[712,84310,14769,84312,84314],{"id":84311},"does-__init__-create-the-object",[18,84313,3262],{}," create the object?",[14,84316,84317,84318,84320],{},"No. Python creates the object first, then ",[18,84319,3262],{}," initializes it.",[712,84322,84324,84325,49645,84327,354],{"id":84323},"why-do-we-use-self-in-__init__","Why do we use ",[18,84326,2427],{},[18,84328,3262],{},[14,84330,84331,84333],{},[18,84332,2427],{}," refers to the current object, so you can store values on that specific object.",[712,84335,49242,84337,84339],{"id":84336},"can-__init__-have-default-values",[18,84338,3262],{}," have default values?",[14,84341,84342],{},"Yes. You can give parameters default values just like in normal functions.",[712,84344,84346,84347,354],{"id":84345},"can-a-class-work-without-__init__","Can a class work without ",[18,84348,3262],{},[14,84350,84351],{},"Yes. But objects from that class will not get automatic setup unless you add it.",[31,84353,2342],{"id":2341},[195,84355,84356,84360,84364,84368],{},[198,84357,84358],{},[236,84359,2560],{"href":2559},[198,84361,84362],{},[236,84363,39177],{"href":33471},[198,84365,84366],{},[236,84367,33426],{"href":33425},[198,84369,84370],{},[236,84371,33437],{"href":84372},"\u002Fglossary\u002Fwhat-is-an-object-in-python",[14,84374,84375,84376,84378],{},"Once you understand ",[18,84377,3262],{},", the next good step is building a simple class of your own and creating a few objects with different starting values.",[2367,84380,84381],{},"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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .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 .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 .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":41,"searchDepth":63,"depth":63,"links":84383},[84384,84385,84387,84389,84390,84391,84393,84394,84400,84401,84403,84405,84406,84407,84418],{"id":33,"depth":63,"text":34},{"id":82426,"depth":63,"text":84386},"What __init__ is",{"id":82452,"depth":63,"text":84388},"Why __init__ is useful",{"id":8706,"depth":63,"text":8707},{"id":82554,"depth":63,"text":82555},{"id":82690,"depth":63,"text":84392},"Setting attributes with self",{"id":82888,"depth":63,"text":82889},{"id":83076,"depth":63,"text":84395,"children":84396},"Parameters in __init__",[84397,84398,84399],{"id":83087,"depth":70,"text":83088},{"id":83176,"depth":70,"text":83177},{"id":83296,"depth":70,"text":83297},{"id":5794,"depth":63,"text":5795},{"id":83642,"depth":63,"text":84402},"What __init__ should return",{"id":83763,"depth":63,"text":84404},"When not to use __init__",{"id":5902,"depth":63,"text":5903},{"id":4322,"depth":63,"text":4323},{"id":2280,"depth":63,"text":2281,"children":84408},[84409,84410,84412,84414,84416],{"id":39162,"depth":70,"text":39230},{"id":84311,"depth":70,"text":84411},"Does __init__ create the object?",{"id":84323,"depth":70,"text":84413},"Why do we use self in __init__?",{"id":84336,"depth":70,"text":84415},"Can __init__ have default values?",{"id":84345,"depth":70,"text":84417},"Can a class work without __init__?",{"id":2341,"depth":63,"text":2342},"Master the __init__ method in python explained in our comprehensive Python beginner guide.",{},{"title":82263,"description":84419},"learn\u002Fthe-__init__-method-in-python-explained","Lx1b5nOnpymA1BLjakSFfykxioVA3Uvqby0sr3wi8M0",{"id":84425,"title":84426,"body":84427,"description":87005,"extension":2399,"meta":87006,"navigation":66,"path":87007,"seo":87008,"stem":87009,"__hash__":87010},"content\u002Flearn\u002Ftype-conversion-in-python-casting-between-types.md","Type Conversion in Python (Casting Between Types)",{"type":7,"value":84428,"toc":86976},[84429,84432,84435,84438,84451,84466,84468,84612,84614,84637,84640,84657,84661,84664,84666,84685,84688,84691,84709,84711,84770,84772,84794,84798,84801,84803,84822,84826,84880,84896,84903,84907,84910,84942,84944,85117,85119,85228,85232,85237,85284,85286,85309,85312,85315,85376,85378,85397,85400,85424,85427,85463,85470,85480,85484,85489,85536,85538,85561,85564,85566,85619,85621,85630,85633,85656,85658,85687,85696,85700,85705,85711,85761,85763,85777,85782,85813,85815,85848,85859,85863,85872,85880,85882,85948,85950,85971,85974,85997,85999,86007,86018,86021,86069,86071,86088,86095,86099,86102,86105,86147,86149,86173,86176,86182,86184,86236,86239,86250,86254,86257,86259,86265,86267,86290,86292,86325,86327,86350,86357,86359,86364,86366,86396,86398,86428,86434,86437,86471,86478,86482,86485,86489,86492,86571,86575,86578,86668,86671,86675,86678,86724,86727,86729,86732,86767,86770,86849,86851,86873,86875,86879,86882,86886,86895,86899,86904,86908,86917,86921,86929,86931,86973],[10,84430,84426],{"id":84431},"type-conversion-in-python-casting-between-types",[14,84433,84434],{},"Type conversion in Python means changing a value from one type to another.",[14,84436,84437],{},"This is very common when working with:",[195,84439,84440,84442,84445,84448],{},[198,84441,46993],{},[198,84443,84444],{},"file data",[198,84446,84447],{},"numbers stored as text",[198,84449,84450],{},"text messages that include numbers",[14,84452,84453,84454,21,84456,21,84458,25,84460,84462,84463,240],{},"Python gives you built-in functions like ",[18,84455,41891],{},[18,84457,41896],{},[18,84459,41901],{},[18,84461,34800],{}," to do this. Beginners often call this ",[2421,84464,84465],{},"casting",[31,84467,34],{"id":33},[36,84469,84471],{"className":38,"code":84470,"language":40,"meta":41,"style":41},"age_text = \"25\"\nage = int(age_text)\n\nprice_text = \"19.99\"\nprice = float(price_text)\n\ncount = 5\ncount_text = str(count)\n\nprint(age + 5)\nprint(price + 0.01)\nprint(\"Count: \" + count_text)\n",[18,84472,84473,84485,84499,84503,84517,84532,84536,84544,84559,84563,84577,84592],{"__ignoreMap":41},[45,84474,84475,84477,84479,84481,84483],{"class":47,"line":48},[45,84476,42011],{"class":51},[45,84478,56],{"class":55},[45,84480,1565],{"class":99},[45,84482,14942],{"class":103},[45,84484,1571],{"class":99},[45,84486,84487,84489,84491,84493,84495,84497],{"class":47,"line":63},[45,84488,52],{"class":51},[45,84490,56],{"class":55},[45,84492,41669],{"class":2109},[45,84494,96],{"class":85},[45,84496,42038],{"class":2070},[45,84498,109],{"class":85},[45,84500,84501],{"class":47,"line":70},[45,84502,67],{"emptyLinePlaceholder":66},[45,84504,84505,84508,84510,84512,84515],{"class":47,"line":89},[45,84506,84507],{"class":51},"price_text ",[45,84509,56],{"class":55},[45,84511,1565],{"class":99},[45,84513,84514],{"class":103},"19.99",[45,84516,1571],{"class":99},[45,84518,84519,84521,84523,84525,84527,84530],{"class":47,"line":112},[45,84520,40904],{"class":51},[45,84522,56],{"class":55},[45,84524,79325],{"class":2109},[45,84526,96],{"class":85},[45,84528,84529],{"class":2070},"price_text",[45,84531,109],{"class":85},[45,84533,84534],{"class":47,"line":126},[45,84535,67],{"emptyLinePlaceholder":66},[45,84537,84538,84540,84542],{"class":47,"line":142},[45,84539,33984],{"class":51},[45,84541,56],{"class":55},[45,84543,733],{"class":59},[45,84545,84546,84549,84551,84553,84555,84557],{"class":47,"line":149},[45,84547,84548],{"class":51},"count_text ",[45,84550,56],{"class":55},[45,84552,38516],{"class":2109},[45,84554,96],{"class":85},[45,84556,34014],{"class":2070},[45,84558,109],{"class":85},[45,84560,84561],{"class":47,"line":679},[45,84562,67],{"emptyLinePlaceholder":66},[45,84564,84565,84567,84569,84571,84573,84575],{"class":47,"line":686},[45,84566,2065],{"class":92},[45,84568,96],{"class":85},[45,84570,52],{"class":2070},[45,84572,8850],{"class":55},[45,84574,1222],{"class":59},[45,84576,109],{"class":85},[45,84578,84579,84581,84583,84585,84587,84590],{"class":47,"line":2229},[45,84580,2065],{"class":92},[45,84582,96],{"class":85},[45,84584,40904],{"class":2070},[45,84586,8850],{"class":55},[45,84588,84589],{"class":59}," 0.01",[45,84591,109],{"class":85},[45,84593,84594,84596,84598,84600,84603,84605,84607,84610],{"class":47,"line":2236},[45,84595,2065],{"class":92},[45,84597,96],{"class":85},[45,84599,100],{"class":99},[45,84601,84602],{"class":103},"Count: ",[45,84604,100],{"class":99},[45,84606,3775],{"class":55},[45,84608,84609],{"class":2070}," count_text",[45,84611,109],{"class":85},[14,84613,177],{},[36,84615,84617],{"className":38,"code":84616,"language":40,"meta":41,"style":41},"30\n20.0\nCount: 5\n",[18,84618,84619,84623,84628],{"__ignoreMap":41},[45,84620,84621],{"class":47,"line":48},[45,84622,22614],{"class":59},[45,84624,84625],{"class":47,"line":63},[45,84626,84627],{"class":59},"20.0\n",[45,84629,84630,84633,84635],{"class":47,"line":70},[45,84631,84632],{"class":51},"Count",[45,84634,457],{"class":85},[45,84636,733],{"class":59},[14,84638,84639],{},"This shows the most common conversions:",[195,84641,84642,84647,84652],{},[198,84643,84644,84645],{},"string to integer with ",[18,84646,41891],{},[198,84648,84649,84650],{},"string to float with ",[18,84651,41896],{},[198,84653,84654,84655],{},"number to string with ",[18,84656,41901],{},[31,84658,84660],{"id":84659},"what-type-conversion-means","What type conversion means",[14,84662,84663],{},"Every value in Python has a type.",[14,84665,2115],{},[195,84667,84668,84672,84676,84681],{},[198,84669,84670,72384],{},[18,84671,40627],{},[198,84673,84674,72389],{},[18,84675,11786],{},[198,84677,84678,84680],{},[18,84679,40884],{}," is a float",[198,84682,84683,72394],{},[18,84684,229],{},[14,84686,84687],{},"Type conversion means changing a value from one type to another.",[14,84689,84690],{},"Python usually does this with built-in functions such as:",[195,84692,84693,84697,84701,84705],{},[198,84694,84695],{},[18,84696,41891],{},[198,84698,84699],{},[18,84700,41896],{},[198,84702,84703],{},[18,84704,41901],{},[198,84706,84707],{},[18,84708,34800],{},[14,84710,466],{},[36,84712,84714],{"className":38,"code":84713,"language":40,"meta":41,"style":41},"text_number = \"42\"\nnumber = int(text_number)\n\nprint(number)\nprint(type(number))\n",[18,84715,84716,84728,84742,84746,84756],{"__ignoreMap":41},[45,84717,84718,84720,84722,84724,84726],{"class":47,"line":48},[45,84719,41913],{"class":51},[45,84721,56],{"class":55},[45,84723,1565],{"class":99},[45,84725,41920],{"class":103},[45,84727,1571],{"class":99},[45,84729,84730,84732,84734,84736,84738,84740],{"class":47,"line":63},[45,84731,728],{"class":51},[45,84733,56],{"class":55},[45,84735,41669],{"class":2109},[45,84737,96],{"class":85},[45,84739,41939],{"class":2070},[45,84741,109],{"class":85},[45,84743,84744],{"class":47,"line":70},[45,84745,67],{"emptyLinePlaceholder":66},[45,84747,84748,84750,84752,84754],{"class":47,"line":89},[45,84749,2065],{"class":92},[45,84751,96],{"class":85},[45,84753,13546],{"class":2070},[45,84755,109],{"class":85},[45,84757,84758,84760,84762,84764,84766,84768],{"class":47,"line":112},[45,84759,2065],{"class":92},[45,84761,96],{"class":85},[45,84763,4340],{"class":2109},[45,84765,96],{"class":85},[45,84767,13546],{"class":2070},[45,84769,4348],{"class":85},[14,84771,177],{},[36,84773,84774],{"className":38,"code":41974,"language":40,"meta":41,"style":41},[18,84775,84776,84780],{"__ignoreMap":41},[45,84777,84778],{"class":47,"line":48},[45,84779,41981],{"class":59},[45,84781,84782,84784,84786,84788,84790,84792],{"class":47,"line":63},[45,84783,79],{"class":55},[45,84785,2441],{"class":2440},[45,84787,9199],{"class":99},[45,84789,29738],{"class":103},[45,84791,7901],{"class":99},[45,84793,13484],{"class":55},[31,84795,84797],{"id":84796},"why-type-conversion-is-needed","Why type conversion is needed",[14,84799,84800],{},"Type conversion is needed because data does not always arrive in the type you want.",[14,84802,42917],{},[195,84804,84805,84810,84813,84816,84819],{},[198,84806,84807,84809],{},[18,84808,18020],{}," always returns a string",[198,84811,84812],{},"file contents are often read as text",[198,84814,84815],{},"API data may contain numbers as strings",[198,84817,84818],{},"math requires number types",[198,84820,84821],{},"building messages often requires strings",[14,84823,18017,84824,457],{},[18,84825,18020],{},[36,84827,84828],{"className":38,"code":42004,"language":40,"meta":41,"style":41},[18,84829,84830,84848,84862,84866],{"__ignoreMap":41},[45,84831,84832,84834,84836,84838,84840,84842,84844,84846],{"class":47,"line":48},[45,84833,42011],{"class":51},[45,84835,56],{"class":55},[45,84837,18043],{"class":92},[45,84839,96],{"class":85},[45,84841,100],{"class":99},[45,84843,39490],{"class":103},[45,84845,100],{"class":99},[45,84847,109],{"class":85},[45,84849,84850,84852,84854,84856,84858,84860],{"class":47,"line":63},[45,84851,52],{"class":51},[45,84853,56],{"class":55},[45,84855,41669],{"class":2109},[45,84857,96],{"class":85},[45,84859,42038],{"class":2070},[45,84861,109],{"class":85},[45,84863,84864],{"class":47,"line":70},[45,84865,67],{"emptyLinePlaceholder":66},[45,84867,84868,84870,84872,84874,84876,84878],{"class":47,"line":89},[45,84869,2065],{"class":92},[45,84871,96],{"class":85},[45,84873,52],{"class":2070},[45,84875,8850],{"class":55},[45,84877,9030],{"class":59},[45,84879,109],{"class":85},[14,84881,84882,84883,84885,84886,84889,84890,84892,84893,84895],{},"If the user types ",[18,84884,14942],{},", Python first receives ",[18,84887,84888],{},"\"25\""," as text.",[7343,84891],{},"\nYou convert it with ",[18,84894,41891],{}," so you can do math.",[14,84897,84898,84899,240],{},"If you want to understand this better, see the ",[236,84900,84901,19075],{"href":18103},[18,84902,18020],{},[31,84904,84906],{"id":84905},"common-conversions-beginners-use","Common conversions beginners use",[14,84908,84909],{},"These are the conversions you will use most often:",[195,84911,84912,84916,84920,84925,84930],{},[198,84913,84644,84914],{},[18,84915,41891],{},[198,84917,84649,84918],{},[18,84919,41896],{},[198,84921,84922,84923],{},"integer or float to string with ",[18,84924,41901],{},[198,84926,84927,84928],{},"other values to boolean with ",[18,84929,34800],{},[198,84931,84932,84933,21,84935,21,84938,25,84940],{},"basic sequence conversions with ",[18,84934,26623],{},[18,84936,84937],{},"tuple()",[18,84939,34668],{},[18,84941,43153],{},[14,84943,466],{},[36,84945,84947],{"className":38,"code":84946,"language":40,"meta":41,"style":41},"print(int(\"7\"))\nprint(float(\"2.5\"))\nprint(str(100))\nprint(bool(1))\nprint(list(\"abc\"))\nprint(tuple([1, 2, 3]))\nprint(set([1, 2, 2, 3]))\nprint(dict([(\"a\", 1), (\"b\", 2)]))\n",[18,84948,84949,84967,84986,85000,85014,85032,85054,85080],{"__ignoreMap":41},[45,84950,84951,84953,84955,84957,84959,84961,84963,84965],{"class":47,"line":48},[45,84952,2065],{"class":92},[45,84954,96],{"class":85},[45,84956,29738],{"class":2109},[45,84958,96],{"class":85},[45,84960,100],{"class":99},[45,84962,33762],{"class":103},[45,84964,100],{"class":99},[45,84966,4348],{"class":85},[45,84968,84969,84971,84973,84975,84977,84979,84982,84984],{"class":47,"line":63},[45,84970,2065],{"class":92},[45,84972,96],{"class":85},[45,84974,29743],{"class":2109},[45,84976,96],{"class":85},[45,84978,100],{"class":99},[45,84980,84981],{"class":103},"2.5",[45,84983,100],{"class":99},[45,84985,4348],{"class":85},[45,84987,84988,84990,84992,84994,84996,84998],{"class":47,"line":70},[45,84989,2065],{"class":92},[45,84991,96],{"class":85},[45,84993,29733],{"class":2109},[45,84995,96],{"class":85},[45,84997,63338],{"class":59},[45,84999,4348],{"class":85},[45,85001,85002,85004,85006,85008,85010,85012],{"class":47,"line":89},[45,85003,2065],{"class":92},[45,85005,96],{"class":85},[45,85007,2110],{"class":2109},[45,85009,96],{"class":85},[45,85011,8578],{"class":59},[45,85013,4348],{"class":85},[45,85015,85016,85018,85020,85022,85024,85026,85028,85030],{"class":47,"line":112},[45,85017,2065],{"class":92},[45,85019,96],{"class":85},[45,85021,14463],{"class":2109},[45,85023,96],{"class":85},[45,85025,100],{"class":99},[45,85027,10304],{"class":103},[45,85029,100],{"class":99},[45,85031,4348],{"class":85},[45,85033,85034,85036,85038,85040,85042,85044,85046,85048,85050,85052],{"class":47,"line":126},[45,85035,2065],{"class":92},[45,85037,96],{"class":85},[45,85039,29752],{"class":2109},[45,85041,34742],{"class":85},[45,85043,8578],{"class":59},[45,85045,3079],{"class":85},[45,85047,3754],{"class":59},[45,85049,3079],{"class":85},[45,85051,777],{"class":59},[45,85053,26913],{"class":85},[45,85055,85056,85058,85060,85062,85064,85066,85068,85070,85072,85074,85076,85078],{"class":47,"line":142},[45,85057,2065],{"class":92},[45,85059,96],{"class":85},[45,85061,29940],{"class":2109},[45,85063,34742],{"class":85},[45,85065,8578],{"class":59},[45,85067,3079],{"class":85},[45,85069,3754],{"class":59},[45,85071,3079],{"class":85},[45,85073,3754],{"class":59},[45,85075,3079],{"class":85},[45,85077,777],{"class":59},[45,85079,26913],{"class":85},[45,85081,85082,85084,85086,85088,85090,85092,85094,85096,85098,85100,85102,85104,85106,85108,85110,85112,85114],{"class":47,"line":149},[45,85083,2065],{"class":92},[45,85085,96],{"class":85},[45,85087,29935],{"class":2109},[45,85089,26867],{"class":85},[45,85091,100],{"class":99},[45,85093,236],{"class":103},[45,85095,100],{"class":99},[45,85097,3079],{"class":85},[45,85099,9030],{"class":59},[45,85101,25873],{"class":85},[45,85103,14039],{"class":85},[45,85105,100],{"class":99},[45,85107,10384],{"class":103},[45,85109,100],{"class":99},[45,85111,3079],{"class":85},[45,85113,3754],{"class":59},[45,85115,85116],{"class":85},")]))\n",[14,85118,177],{},[36,85120,85122],{"className":38,"code":85121,"language":40,"meta":41,"style":41},"7\n2.5\n100\nTrue\n['a', 'b', 'c']\n(1, 2, 3)\n{1, 2, 3}\n{'a': 1, 'b': 2}\n",[18,85123,85124,85128,85132,85136,85140,85168,85184,85200],{"__ignoreMap":41},[45,85125,85126],{"class":47,"line":48},[45,85127,35999],{"class":59},[45,85129,85130],{"class":47,"line":63},[45,85131,63977],{"class":59},[45,85133,85134],{"class":47,"line":70},[45,85135,20327],{"class":59},[45,85137,85138],{"class":47,"line":89},[45,85139,2270],{"class":481},[45,85141,85142,85144,85146,85148,85150,85152,85154,85156,85158,85160,85162,85164,85166],{"class":47,"line":112},[45,85143,9680],{"class":85},[45,85145,7901],{"class":99},[45,85147,236],{"class":103},[45,85149,7901],{"class":99},[45,85151,3079],{"class":85},[45,85153,9199],{"class":99},[45,85155,10384],{"class":103},[45,85157,7901],{"class":99},[45,85159,3079],{"class":85},[45,85161,9199],{"class":99},[45,85163,10402],{"class":103},[45,85165,7901],{"class":99},[45,85167,8594],{"class":85},[45,85169,85170,85172,85174,85176,85178,85180,85182],{"class":47,"line":126},[45,85171,96],{"class":85},[45,85173,8578],{"class":59},[45,85175,3079],{"class":85},[45,85177,3754],{"class":59},[45,85179,3079],{"class":85},[45,85181,777],{"class":59},[45,85183,109],{"class":85},[45,85185,85186,85188,85190,85192,85194,85196,85198],{"class":47,"line":142},[45,85187,5551],{"class":85},[45,85189,8578],{"class":59},[45,85191,3079],{"class":85},[45,85193,3754],{"class":59},[45,85195,3079],{"class":85},[45,85197,777],{"class":59},[45,85199,8629],{"class":85},[45,85201,85202,85204,85206,85208,85210,85212,85214,85216,85218,85220,85222,85224,85226],{"class":47,"line":149},[45,85203,5551],{"class":85},[45,85205,7901],{"class":99},[45,85207,236],{"class":103},[45,85209,7901],{"class":99},[45,85211,457],{"class":85},[45,85213,9030],{"class":59},[45,85215,3079],{"class":85},[45,85217,9199],{"class":99},[45,85219,10384],{"class":103},[45,85221,7901],{"class":99},[45,85223,457],{"class":85},[45,85225,3754],{"class":59},[45,85227,8629],{"class":85},[31,85229,85231],{"id":85230},"convert-strings-to-integers","Convert strings to integers",[14,85233,165,85234,85236],{},[18,85235,41891],{}," to convert a string to a whole number.",[36,85238,85240],{"className":38,"code":85239,"language":40,"meta":41,"style":41},"number = int(\"10\")\nprint(number)\nprint(type(number))\n",[18,85241,85242,85260,85270],{"__ignoreMap":41},[45,85243,85244,85246,85248,85250,85252,85254,85256,85258],{"class":47,"line":48},[45,85245,728],{"class":51},[45,85247,56],{"class":55},[45,85249,41669],{"class":2109},[45,85251,96],{"class":85},[45,85253,100],{"class":99},[45,85255,11786],{"class":103},[45,85257,100],{"class":99},[45,85259,109],{"class":85},[45,85261,85262,85264,85266,85268],{"class":47,"line":63},[45,85263,2065],{"class":92},[45,85265,96],{"class":85},[45,85267,13546],{"class":2070},[45,85269,109],{"class":85},[45,85271,85272,85274,85276,85278,85280,85282],{"class":47,"line":70},[45,85273,2065],{"class":92},[45,85275,96],{"class":85},[45,85277,4340],{"class":2109},[45,85279,96],{"class":85},[45,85281,13546],{"class":2070},[45,85283,4348],{"class":85},[14,85285,177],{},[36,85287,85289],{"className":38,"code":85288,"language":40,"meta":41,"style":41},"10\n\u003Cclass 'int'>\n",[18,85290,85291,85295],{"__ignoreMap":41},[45,85292,85293],{"class":47,"line":48},[45,85294,13262],{"class":59},[45,85296,85297,85299,85301,85303,85305,85307],{"class":47,"line":63},[45,85298,79],{"class":55},[45,85300,2441],{"class":2440},[45,85302,9199],{"class":99},[45,85304,29738],{"class":103},[45,85306,7901],{"class":99},[45,85308,13484],{"class":55},[14,85310,85311],{},"The string must look like a whole number.",[14,85313,85314],{},"These work:",[36,85316,85318],{"className":38,"code":85317,"language":40,"meta":41,"style":41},"print(int(\"42\"))\nprint(int(\" 42 \"))\nprint(int(\"-7\"))\n",[18,85319,85320,85338,85357],{"__ignoreMap":41},[45,85321,85322,85324,85326,85328,85330,85332,85334,85336],{"class":47,"line":48},[45,85323,2065],{"class":92},[45,85325,96],{"class":85},[45,85327,29738],{"class":2109},[45,85329,96],{"class":85},[45,85331,100],{"class":99},[45,85333,41920],{"class":103},[45,85335,100],{"class":99},[45,85337,4348],{"class":85},[45,85339,85340,85342,85344,85346,85348,85350,85353,85355],{"class":47,"line":63},[45,85341,2065],{"class":92},[45,85343,96],{"class":85},[45,85345,29738],{"class":2109},[45,85347,96],{"class":85},[45,85349,100],{"class":99},[45,85351,85352],{"class":103}," 42 ",[45,85354,100],{"class":99},[45,85356,4348],{"class":85},[45,85358,85359,85361,85363,85365,85367,85369,85372,85374],{"class":47,"line":70},[45,85360,2065],{"class":92},[45,85362,96],{"class":85},[45,85364,29738],{"class":2109},[45,85366,96],{"class":85},[45,85368,100],{"class":99},[45,85370,85371],{"class":103},"-7",[45,85373,100],{"class":99},[45,85375,4348],{"class":85},[14,85377,177],{},[36,85379,85381],{"className":38,"code":85380,"language":40,"meta":41,"style":41},"42\n42\n-7\n",[18,85382,85383,85387,85391],{"__ignoreMap":41},[45,85384,85385],{"class":47,"line":48},[45,85386,41981],{"class":59},[45,85388,85389],{"class":47,"line":63},[45,85390,41981],{"class":59},[45,85392,85393,85395],{"class":47,"line":70},[45,85394,218],{"class":55},[45,85396,35999],{"class":59},[14,85398,85399],{},"This fails:",[36,85401,85403],{"className":38,"code":85402,"language":40,"meta":41,"style":41},"print(int(\"10.5\"))\n",[18,85404,85405],{"__ignoreMap":41},[45,85406,85407,85409,85411,85413,85415,85417,85420,85422],{"class":47,"line":48},[45,85408,2065],{"class":92},[45,85410,96],{"class":85},[45,85412,29738],{"class":2109},[45,85414,96],{"class":85},[45,85416,100],{"class":99},[45,85418,85419],{"class":103},"10.5",[45,85421,100],{"class":99},[45,85423,4348],{"class":85},[14,85425,85426],{},"Error:",[36,85428,85430],{"className":38,"code":85429,"language":40,"meta":41,"style":41},"ValueError: invalid literal for int() with base 10: '10.5'\n",[18,85431,85432],{"__ignoreMap":41},[45,85433,85434,85436,85438,85441,85443,85445,85447,85450,85453,85455,85457,85459,85461],{"class":47,"line":48},[45,85435,42097],{"class":2109},[45,85437,457],{"class":85},[45,85439,85440],{"class":51}," invalid literal ",[45,85442,7895],{"class":73},[45,85444,41669],{"class":2109},[45,85446,3772],{"class":85},[45,85448,85449],{"class":73}," with",[45,85451,85452],{"class":51}," base ",[45,85454,11786],{"class":59},[45,85456,457],{"class":85},[45,85458,9199],{"class":99},[45,85460,85419],{"class":103},[45,85462,7906],{"class":99},[14,85464,85465,85466,85469],{},"That happens because ",[18,85467,85468],{},"\"10.5\""," is a decimal number, not a whole number string.",[14,85471,85472,85473,230,85477,240],{},"If you need more detail, see ",[236,85474,85475,18107],{"href":64106},[18,85476,41891],{},[236,85478,85479],{"href":42107},"how to convert a string to int in Python",[31,85481,85483],{"id":85482},"convert-strings-to-floats","Convert strings to floats",[14,85485,165,85486,85488],{},[18,85487,41896],{}," to convert a string to a decimal number.",[36,85490,85492],{"className":38,"code":85491,"language":40,"meta":41,"style":41},"price = float(\"3.14\")\nprint(price)\nprint(type(price))\n",[18,85493,85494,85512,85522],{"__ignoreMap":41},[45,85495,85496,85498,85500,85502,85504,85506,85508,85510],{"class":47,"line":48},[45,85497,40904],{"class":51},[45,85499,56],{"class":55},[45,85501,79325],{"class":2109},[45,85503,96],{"class":85},[45,85505,100],{"class":99},[45,85507,40884],{"class":103},[45,85509,100],{"class":99},[45,85511,109],{"class":85},[45,85513,85514,85516,85518,85520],{"class":47,"line":63},[45,85515,2065],{"class":92},[45,85517,96],{"class":85},[45,85519,19985],{"class":2070},[45,85521,109],{"class":85},[45,85523,85524,85526,85528,85530,85532,85534],{"class":47,"line":70},[45,85525,2065],{"class":92},[45,85527,96],{"class":85},[45,85529,4340],{"class":2109},[45,85531,96],{"class":85},[45,85533,19985],{"class":2070},[45,85535,4348],{"class":85},[14,85537,177],{},[36,85539,85541],{"className":38,"code":85540,"language":40,"meta":41,"style":41},"3.14\n\u003Cclass 'float'>\n",[18,85542,85543,85547],{"__ignoreMap":41},[45,85544,85545],{"class":47,"line":48},[45,85546,64211],{"class":59},[45,85548,85549,85551,85553,85555,85557,85559],{"class":47,"line":63},[45,85550,79],{"class":55},[45,85552,2441],{"class":2440},[45,85554,9199],{"class":99},[45,85556,29743],{"class":103},[45,85558,7901],{"class":99},[45,85560,13484],{"class":55},[14,85562,85563],{},"This is useful when numbers may contain decimal points.",[14,85565,466],{},[36,85567,85569],{"className":38,"code":85568,"language":40,"meta":41,"style":41},"height_text = \"1.75\"\nheight = float(height_text)\n\nprint(height * 2)\n",[18,85570,85571,85585,85601,85605],{"__ignoreMap":41},[45,85572,85573,85576,85578,85580,85583],{"class":47,"line":48},[45,85574,85575],{"class":51},"height_text ",[45,85577,56],{"class":55},[45,85579,1565],{"class":99},[45,85581,85582],{"class":103},"1.75",[45,85584,1571],{"class":99},[45,85586,85587,85590,85592,85594,85596,85599],{"class":47,"line":63},[45,85588,85589],{"class":51},"height ",[45,85591,56],{"class":55},[45,85593,79325],{"class":2109},[45,85595,96],{"class":85},[45,85597,85598],{"class":2070},"height_text",[45,85600,109],{"class":85},[45,85602,85603],{"class":47,"line":70},[45,85604,67],{"emptyLinePlaceholder":66},[45,85606,85607,85609,85611,85613,85615,85617],{"class":47,"line":89},[45,85608,2065],{"class":92},[45,85610,96],{"class":85},[45,85612,85589],{"class":2070},[45,85614,5601],{"class":55},[45,85616,3754],{"class":59},[45,85618,109],{"class":85},[14,85620,177],{},[36,85622,85624],{"className":38,"code":85623,"language":40,"meta":41,"style":41},"3.5\n",[18,85625,85626],{"__ignoreMap":41},[45,85627,85628],{"class":47,"line":48},[45,85629,85623],{"class":59},[14,85631,85632],{},"Invalid text causes an error:",[36,85634,85636],{"className":38,"code":85635,"language":40,"meta":41,"style":41},"print(float(\"abc\"))\n",[18,85637,85638],{"__ignoreMap":41},[45,85639,85640,85642,85644,85646,85648,85650,85652,85654],{"class":47,"line":48},[45,85641,2065],{"class":92},[45,85643,96],{"class":85},[45,85645,29743],{"class":2109},[45,85647,96],{"class":85},[45,85649,100],{"class":99},[45,85651,10304],{"class":103},[45,85653,100],{"class":99},[45,85655,4348],{"class":85},[14,85657,85426],{},[36,85659,85661],{"className":38,"code":85660,"language":40,"meta":41,"style":41},"ValueError: could not convert string to float: 'abc'\n",[18,85662,85663],{"__ignoreMap":41},[45,85664,85665,85667,85669,85672,85674,85677,85679,85681,85683,85685],{"class":47,"line":48},[45,85666,42097],{"class":2109},[45,85668,457],{"class":85},[45,85670,85671],{"class":51}," could ",[45,85673,2857],{"class":55},[45,85675,85676],{"class":51}," convert string to ",[45,85678,29743],{"class":2109},[45,85680,457],{"class":85},[45,85682,9199],{"class":99},[45,85684,10304],{"class":103},[45,85686,7906],{"class":99},[14,85688,26005,85689,230,85693,240],{},[236,85690,85691,18107],{"href":64114},[18,85692,41896],{},[236,85694,85695],{"href":64293},"how to convert a string to float in Python",[31,85697,85699],{"id":85698},"convert-numbers-to-strings","Convert numbers to strings",[14,85701,165,85702,85704],{},[18,85703,41901],{}," when you need to combine numbers with text.",[14,85706,85707,85708,85710],{},"This is very common in ",[18,85709,15833],{}," messages, file output, and labels.",[36,85712,85714],{"className":38,"code":85713,"language":40,"meta":41,"style":41},"score = 95\nmessage = \"Your score is \" + str(score)\n\nprint(message)\n",[18,85715,85716,85724,85747,85751],{"__ignoreMap":41},[45,85717,85718,85720,85722],{"class":47,"line":48},[45,85719,581],{"class":51},[45,85721,56],{"class":55},[45,85723,76125],{"class":59},[45,85725,85726,85728,85730,85732,85735,85737,85739,85741,85743,85745],{"class":47,"line":63},[45,85727,15507],{"class":51},[45,85729,56],{"class":55},[45,85731,1565],{"class":99},[45,85733,85734],{"class":103},"Your score is ",[45,85736,100],{"class":99},[45,85738,3775],{"class":55},[45,85740,38516],{"class":2109},[45,85742,96],{"class":85},[45,85744,46543],{"class":2070},[45,85746,109],{"class":85},[45,85748,85749],{"class":47,"line":70},[45,85750,67],{"emptyLinePlaceholder":66},[45,85752,85753,85755,85757,85759],{"class":47,"line":89},[45,85754,2065],{"class":92},[45,85756,96],{"class":85},[45,85758,6462],{"class":2070},[45,85760,109],{"class":85},[14,85762,177],{},[36,85764,85766],{"className":38,"code":85765,"language":40,"meta":41,"style":41},"Your score is 95\n",[18,85767,85768],{"__ignoreMap":41},[45,85769,85770,85773,85775],{"class":47,"line":48},[45,85771,85772],{"class":51},"Your score ",[45,85774,1285],{"class":55},[45,85776,76125],{"class":59},[14,85778,45969,85779,85781],{},[18,85780,41901],{},", this would fail:",[36,85783,85785],{"className":38,"code":85784,"language":40,"meta":41,"style":41},"score = 95\nprint(\"Your score is \" + score)\n",[18,85786,85787,85795],{"__ignoreMap":41},[45,85788,85789,85791,85793],{"class":47,"line":48},[45,85790,581],{"class":51},[45,85792,56],{"class":55},[45,85794,76125],{"class":59},[45,85796,85797,85799,85801,85803,85805,85807,85809,85811],{"class":47,"line":63},[45,85798,2065],{"class":92},[45,85800,96],{"class":85},[45,85802,100],{"class":99},[45,85804,85734],{"class":103},[45,85806,100],{"class":99},[45,85808,3775],{"class":55},[45,85810,76166],{"class":2070},[45,85812,109],{"class":85},[14,85814,85426],{},[36,85816,85818],{"className":38,"code":85817,"language":40,"meta":41,"style":41},"TypeError: can only concatenate str (not \"int\") to str\n",[18,85819,85820],{"__ignoreMap":41},[45,85821,85822,85824,85826,85829,85831,85833,85835,85837,85839,85841,85843,85845],{"class":47,"line":48},[45,85823,7711],{"class":2109},[45,85825,457],{"class":85},[45,85827,85828],{"class":51}," can only concatenate ",[45,85830,29733],{"class":2109},[45,85832,14039],{"class":85},[45,85834,2857],{"class":73},[45,85836,1565],{"class":99},[45,85838,29738],{"class":103},[45,85840,100],{"class":99},[45,85842,9146],{"class":85},[45,85844,7076],{"class":51},[45,85846,85847],{"class":2109},"str\n",[14,85849,26005,85850,230,85855,240],{},[236,85851,85853,18107],{"href":85852},"\u002Freference\u002Fpython-str-function-explained\u002F",[18,85854,41901],{},[236,85856,85858],{"href":85857},"\u002Fhow-to\u002Fhow-to-convert-int-to-string-in-python\u002F","how to convert int to string in Python",[31,85860,85862],{"id":85861},"convert-values-to-booleans","Convert values to booleans",[14,85864,165,85865,85867,85868,230,85870,240],{},[18,85866,34800],{}," to convert a value to ",[18,85869,229],{},[18,85871,233],{},[14,85873,85874,85875,1316,85877,85879],{},"This follows Python's ",[2421,85876,34783],{},[2421,85878,34786],{}," rules.",[14,85881,40802],{},[36,85883,85885],{"className":38,"code":85884,"language":40,"meta":41,"style":41},"print(bool(0))\nprint(bool(1))\nprint(bool(\"\"))\nprint(bool(\"hello\"))\n",[18,85886,85887,85901,85915,85930],{"__ignoreMap":41},[45,85888,85889,85891,85893,85895,85897,85899],{"class":47,"line":48},[45,85890,2065],{"class":92},[45,85892,96],{"class":85},[45,85894,2110],{"class":2109},[45,85896,96],{"class":85},[45,85898,10482],{"class":59},[45,85900,4348],{"class":85},[45,85902,85903,85905,85907,85909,85911,85913],{"class":47,"line":63},[45,85904,2065],{"class":92},[45,85906,96],{"class":85},[45,85908,2110],{"class":2109},[45,85910,96],{"class":85},[45,85912,8578],{"class":59},[45,85914,4348],{"class":85},[45,85916,85917,85919,85921,85923,85925,85928],{"class":47,"line":70},[45,85918,2065],{"class":92},[45,85920,96],{"class":85},[45,85922,2110],{"class":2109},[45,85924,96],{"class":85},[45,85926,85927],{"class":99},"\"\"",[45,85929,4348],{"class":85},[45,85931,85932,85934,85936,85938,85940,85942,85944,85946],{"class":47,"line":89},[45,85933,2065],{"class":92},[45,85935,96],{"class":85},[45,85937,2110],{"class":2109},[45,85939,96],{"class":85},[45,85941,100],{"class":99},[45,85943,18782],{"class":103},[45,85945,100],{"class":99},[45,85947,4348],{"class":85},[14,85949,177],{},[36,85951,85953],{"className":38,"code":85952,"language":40,"meta":41,"style":41},"False\nTrue\nFalse\nTrue\n",[18,85954,85955,85959,85963,85967],{"__ignoreMap":41},[45,85956,85957],{"class":47,"line":48},[45,85958,2265],{"class":481},[45,85960,85961],{"class":47,"line":63},[45,85962,2270],{"class":481},[45,85964,85965],{"class":47,"line":70},[45,85966,2265],{"class":481},[45,85968,85969],{"class":47,"line":89},[45,85970,2270],{"class":481},[14,85972,85973],{},"A very common beginner trap is this:",[36,85975,85977],{"className":38,"code":85976,"language":40,"meta":41,"style":41},"print(bool(\"False\"))\n",[18,85978,85979],{"__ignoreMap":41},[45,85980,85981,85983,85985,85987,85989,85991,85993,85995],{"class":47,"line":48},[45,85982,2065],{"class":92},[45,85984,96],{"class":85},[45,85986,2110],{"class":2109},[45,85988,96],{"class":85},[45,85990,100],{"class":99},[45,85992,233],{"class":103},[45,85994,100],{"class":99},[45,85996,4348],{"class":85},[14,85998,177],{},[36,86000,86001],{"className":38,"code":2270,"language":40,"meta":41,"style":41},[18,86002,86003],{"__ignoreMap":41},[45,86004,86005],{"class":47,"line":48},[45,86006,2270],{"class":481},[14,86008,71300,86009,6578,86012,86015,86016,240],{},[18,86010,86011],{},"\"False\"",[2421,86013,86014],{},"non-empty string",". In Python, non-empty strings are ",[18,86017,229],{},[14,86019,86020],{},"More examples:",[36,86022,86024],{"className":38,"code":86023,"language":40,"meta":41,"style":41},"print(bool([]))\nprint(bool([1, 2]))\nprint(bool(None))\n",[18,86025,86026,86037,86055],{"__ignoreMap":41},[45,86027,86028,86030,86032,86034],{"class":47,"line":48},[45,86029,2065],{"class":92},[45,86031,96],{"class":85},[45,86033,2110],{"class":2109},[45,86035,86036],{"class":85},"([]))\n",[45,86038,86039,86041,86043,86045,86047,86049,86051,86053],{"class":47,"line":63},[45,86040,2065],{"class":92},[45,86042,96],{"class":85},[45,86044,2110],{"class":2109},[45,86046,34742],{"class":85},[45,86048,8578],{"class":59},[45,86050,3079],{"class":85},[45,86052,3754],{"class":59},[45,86054,26913],{"class":85},[45,86056,86057,86059,86061,86063,86065,86067],{"class":47,"line":70},[45,86058,2065],{"class":92},[45,86060,96],{"class":85},[45,86062,2110],{"class":2109},[45,86064,96],{"class":85},[45,86066,5997],{"class":481},[45,86068,4348],{"class":85},[14,86070,177],{},[36,86072,86074],{"className":38,"code":86073,"language":40,"meta":41,"style":41},"False\nTrue\nFalse\n",[18,86075,86076,86080,86084],{"__ignoreMap":41},[45,86077,86078],{"class":47,"line":48},[45,86079,2265],{"class":481},[45,86081,86082],{"class":47,"line":63},[45,86083,2270],{"class":481},[45,86085,86086],{"class":47,"line":70},[45,86087,2265],{"class":481},[14,86089,86090,86091,240],{},"For more examples, see ",[236,86092,86093,18107],{"href":34797},[18,86094,34800],{},[31,86096,86098],{"id":86097},"automatic-vs-explicit-conversion","Automatic vs explicit conversion",[14,86100,86101],{},"Python sometimes converts values automatically.",[14,86103,86104],{},"For example, when you mix an integer and a float:",[36,86106,86108],{"className":38,"code":86107,"language":40,"meta":41,"style":41},"result = 5 + 2.5\nprint(result)\nprint(type(result))\n",[18,86109,86110,86123,86133],{"__ignoreMap":41},[45,86111,86112,86114,86116,86118,86120],{"class":47,"line":48},[45,86113,3595],{"class":51},[45,86115,56],{"class":55},[45,86117,1222],{"class":59},[45,86119,3775],{"class":55},[45,86121,86122],{"class":59}," 2.5\n",[45,86124,86125,86127,86129,86131],{"class":47,"line":63},[45,86126,2065],{"class":92},[45,86128,96],{"class":85},[45,86130,3620],{"class":2070},[45,86132,109],{"class":85},[45,86134,86135,86137,86139,86141,86143,86145],{"class":47,"line":70},[45,86136,2065],{"class":92},[45,86138,96],{"class":85},[45,86140,4340],{"class":2109},[45,86142,96],{"class":85},[45,86144,3620],{"class":2070},[45,86146,4348],{"class":85},[14,86148,177],{},[36,86150,86152],{"className":38,"code":86151,"language":40,"meta":41,"style":41},"7.5\n\u003Cclass 'float'>\n",[18,86153,86154,86159],{"__ignoreMap":41},[45,86155,86156],{"class":47,"line":48},[45,86157,86158],{"class":59},"7.5\n",[45,86160,86161,86163,86165,86167,86169,86171],{"class":47,"line":63},[45,86162,79],{"class":55},[45,86164,2441],{"class":2440},[45,86166,9199],{"class":99},[45,86168,29743],{"class":103},[45,86170,7901],{"class":99},[45,86172,13484],{"class":55},[14,86174,86175],{},"Python automatically turns the result into a float here.",[14,86177,86178,86179,240],{},"But beginners should usually prefer ",[2421,86180,86181],{},"explicit conversion",[14,86183,466],{},[36,86185,86187],{"className":38,"code":86186,"language":40,"meta":41,"style":41},"user_text = \"12\"\nuser_number = int(user_text)\n\nprint(user_number + 3)\n",[18,86188,86189,86202,86218,86222],{"__ignoreMap":41},[45,86190,86191,86194,86196,86198,86200],{"class":47,"line":48},[45,86192,86193],{"class":51},"user_text ",[45,86195,56],{"class":55},[45,86197,1565],{"class":99},[45,86199,12026],{"class":103},[45,86201,1571],{"class":99},[45,86203,86204,86207,86209,86211,86213,86216],{"class":47,"line":63},[45,86205,86206],{"class":51},"user_number ",[45,86208,56],{"class":55},[45,86210,41669],{"class":2109},[45,86212,96],{"class":85},[45,86214,86215],{"class":2070},"user_text",[45,86217,109],{"class":85},[45,86219,86220],{"class":47,"line":70},[45,86221,67],{"emptyLinePlaceholder":66},[45,86223,86224,86226,86228,86230,86232,86234],{"class":47,"line":89},[45,86225,2065],{"class":92},[45,86227,96],{"class":85},[45,86229,86206],{"class":2070},[45,86231,8850],{"class":55},[45,86233,777],{"class":59},[45,86235,109],{"class":85},[14,86237,86238],{},"Explicit conversion is better because:",[195,86240,86241,86244,86247],{},[198,86242,86243],{},"it makes your code easier to read",[198,86245,86246],{},"it makes the intended type clear",[198,86248,86249],{},"it helps you catch mistakes earlier",[31,86251,86253],{"id":86252},"errors-you-may-see-during-conversion","Errors you may see during conversion",[14,86255,86256],{},"Two common errors appear often.",[712,86258,42097],{"id":46606},[14,86260,86261,86262,86264],{},"You get a ",[18,86263,42097],{}," when text cannot be turned into the number you asked for.",[14,86266,466],{},[36,86268,86270],{"className":38,"code":86269,"language":40,"meta":41,"style":41},"number = int(\"hello\")\n",[18,86271,86272],{"__ignoreMap":41},[45,86273,86274,86276,86278,86280,86282,86284,86286,86288],{"class":47,"line":48},[45,86275,728],{"class":51},[45,86277,56],{"class":55},[45,86279,41669],{"class":2109},[45,86281,96],{"class":85},[45,86283,100],{"class":99},[45,86285,18782],{"class":103},[45,86287,100],{"class":99},[45,86289,109],{"class":85},[14,86291,85426],{},[36,86293,86295],{"className":38,"code":86294,"language":40,"meta":41,"style":41},"ValueError: invalid literal for int() with base 10: 'hello'\n",[18,86296,86297],{"__ignoreMap":41},[45,86298,86299,86301,86303,86305,86307,86309,86311,86313,86315,86317,86319,86321,86323],{"class":47,"line":48},[45,86300,42097],{"class":2109},[45,86302,457],{"class":85},[45,86304,85440],{"class":51},[45,86306,7895],{"class":73},[45,86308,41669],{"class":2109},[45,86310,3772],{"class":85},[45,86312,85449],{"class":73},[45,86314,85452],{"class":51},[45,86316,11786],{"class":59},[45,86318,457],{"class":85},[45,86320,9199],{"class":99},[45,86322,18782],{"class":103},[45,86324,7906],{"class":99},[14,86326,40002],{},[36,86328,86330],{"className":38,"code":86329,"language":40,"meta":41,"style":41},"price = float(\"abc\")\n",[18,86331,86332],{"__ignoreMap":41},[45,86333,86334,86336,86338,86340,86342,86344,86346,86348],{"class":47,"line":48},[45,86335,40904],{"class":51},[45,86337,56],{"class":55},[45,86339,79325],{"class":2109},[45,86341,96],{"class":85},[45,86343,100],{"class":99},[45,86345,10304],{"class":103},[45,86347,100],{"class":99},[45,86349,109],{"class":85},[14,86351,86352,86353,240],{},"If you hit this problem, see ",[236,86354,4025,86355],{"href":42386},[18,86356,42387],{},[712,86358,7711],{"id":46563},[14,86360,86261,86361,86363],{},[18,86362,7711],{}," when you use values in the wrong way.",[14,86365,466],{},[36,86367,86368],{"className":38,"code":64615,"language":40,"meta":41,"style":41},[18,86369,86370,86378],{"__ignoreMap":41},[45,86371,86372,86374,86376],{"class":47,"line":48},[45,86373,52],{"class":51},[45,86375,56],{"class":55},[45,86377,30102],{"class":59},[45,86379,86380,86382,86384,86386,86388,86390,86392,86394],{"class":47,"line":63},[45,86381,2065],{"class":92},[45,86383,96],{"class":85},[45,86385,100],{"class":99},[45,86387,46584],{"class":103},[45,86389,100],{"class":99},[45,86391,3775],{"class":55},[45,86393,3305],{"class":2070},[45,86395,109],{"class":85},[14,86397,85426],{},[36,86399,86400],{"className":38,"code":85817,"language":40,"meta":41,"style":41},[18,86401,86402],{"__ignoreMap":41},[45,86403,86404,86406,86408,86410,86412,86414,86416,86418,86420,86422,86424,86426],{"class":47,"line":48},[45,86405,7711],{"class":2109},[45,86407,457],{"class":85},[45,86409,85828],{"class":51},[45,86411,29733],{"class":2109},[45,86413,14039],{"class":85},[45,86415,2857],{"class":73},[45,86417,1565],{"class":99},[45,86419,29738],{"class":103},[45,86421,100],{"class":99},[45,86423,9146],{"class":85},[45,86425,7076],{"class":51},[45,86427,85847],{"class":2109},[14,86429,86430,86431,86433],{},"This happens because ",[18,86432,8850],{}," cannot directly join a string and an integer.",[14,86435,86436],{},"To fix it:",[36,86438,86439],{"className":38,"code":64652,"language":40,"meta":41,"style":41},[18,86440,86441,86449],{"__ignoreMap":41},[45,86442,86443,86445,86447],{"class":47,"line":48},[45,86444,52],{"class":51},[45,86446,56],{"class":55},[45,86448,30102],{"class":59},[45,86450,86451,86453,86455,86457,86459,86461,86463,86465,86467,86469],{"class":47,"line":63},[45,86452,2065],{"class":92},[45,86454,96],{"class":85},[45,86456,100],{"class":99},[45,86458,46584],{"class":103},[45,86460,100],{"class":99},[45,86462,3775],{"class":55},[45,86464,38516],{"class":2109},[45,86466,96],{"class":85},[45,86468,2082],{"class":2070},[45,86470,4348],{"class":85},[14,86472,26005,86473,240],{},[236,86474,4025,86475],{"href":42380},[18,86476,86477],{},"TypeError: can only concatenate str (not int) to str",[31,86479,86481],{"id":86480},"safe-conversion-patterns-for-beginners","Safe conversion patterns for beginners",[14,86483,86484],{},"When data may be invalid, convert carefully.",[712,86486,86488],{"id":86487},"check-before-converting-when-possible","Check before converting when possible",[14,86490,86491],{},"If you already know what the value should look like, check it first.",[36,86493,86495],{"className":38,"code":86494,"language":40,"meta":41,"style":41},"value = \"42\"\n\nif value.isdigit():\n    number = int(value)\n    print(number)\nelse:\n    print(\"Not a whole number\")\n",[18,86496,86497,86509,86513,86526,86540,86550,86556],{"__ignoreMap":41},[45,86498,86499,86501,86503,86505,86507],{"class":47,"line":48},[45,86500,1476],{"class":51},[45,86502,56],{"class":55},[45,86504,1565],{"class":99},[45,86506,41920],{"class":103},[45,86508,1571],{"class":99},[45,86510,86511],{"class":47,"line":63},[45,86512,67],{"emptyLinePlaceholder":66},[45,86514,86515,86517,86519,86521,86524],{"class":47,"line":70},[45,86516,20],{"class":73},[45,86518,42794],{"class":51},[45,86520,240],{"class":85},[45,86522,86523],{"class":2070},"isdigit",[45,86525,3828],{"class":85},[45,86527,86528,86530,86532,86534,86536,86538],{"class":47,"line":89},[45,86529,12957],{"class":51},[45,86531,56],{"class":55},[45,86533,41669],{"class":2109},[45,86535,96],{"class":85},[45,86537,12722],{"class":2070},[45,86539,109],{"class":85},[45,86541,86542,86544,86546,86548],{"class":47,"line":112},[45,86543,93],{"class":92},[45,86545,96],{"class":85},[45,86547,13546],{"class":2070},[45,86549,109],{"class":85},[45,86551,86552,86554],{"class":47,"line":126},[45,86553,28],{"class":73},[45,86555,86],{"class":85},[45,86557,86558,86560,86562,86564,86567,86569],{"class":47,"line":142},[45,86559,93],{"class":92},[45,86561,96],{"class":85},[45,86563,100],{"class":99},[45,86565,86566],{"class":103},"Not a whole number",[45,86568,100],{"class":99},[45,86570,109],{"class":85},[712,86572,86574],{"id":86573},"use-try-except-for-unsafe-input","Use try-except for unsafe input",[14,86576,86577],{},"This is helpful for user input and file data.",[36,86579,86581],{"className":38,"code":86580,"language":40,"meta":41,"style":41},"value = input(\"Enter a whole number: \")\n\ntry:\n    number = int(value)\n    print(\"You entered:\", number)\nexcept ValueError:\n    print(\"That was not a valid whole number.\")\n",[18,86582,86583,86602,86606,86612,86626,86645,86653],{"__ignoreMap":41},[45,86584,86585,86587,86589,86591,86593,86595,86598,86600],{"class":47,"line":48},[45,86586,1476],{"class":51},[45,86588,56],{"class":55},[45,86590,18043],{"class":92},[45,86592,96],{"class":85},[45,86594,100],{"class":99},[45,86596,86597],{"class":103},"Enter a whole number: ",[45,86599,100],{"class":99},[45,86601,109],{"class":85},[45,86603,86604],{"class":47,"line":63},[45,86605,67],{"emptyLinePlaceholder":66},[45,86607,86608,86610],{"class":47,"line":70},[45,86609,23272],{"class":73},[45,86611,86],{"class":85},[45,86613,86614,86616,86618,86620,86622,86624],{"class":47,"line":89},[45,86615,12957],{"class":51},[45,86617,56],{"class":55},[45,86619,41669],{"class":2109},[45,86621,96],{"class":85},[45,86623,12722],{"class":2070},[45,86625,109],{"class":85},[45,86627,86628,86630,86632,86634,86637,86639,86641,86643],{"class":47,"line":112},[45,86629,93],{"class":92},[45,86631,96],{"class":85},[45,86633,100],{"class":99},[45,86635,86636],{"class":103},"You entered:",[45,86638,100],{"class":99},[45,86640,3079],{"class":85},[45,86642,35957],{"class":2070},[45,86644,109],{"class":85},[45,86646,86647,86649,86651],{"class":47,"line":126},[45,86648,23275],{"class":73},[45,86650,45892],{"class":2109},[45,86652,86],{"class":85},[45,86654,86655,86657,86659,86661,86664,86666],{"class":47,"line":142},[45,86656,93],{"class":92},[45,86658,96],{"class":85},[45,86660,100],{"class":99},[45,86662,86663],{"class":103},"That was not a valid whole number.",[45,86665,100],{"class":99},[45,86667,109],{"class":85},[14,86669,86670],{},"This prevents your program from crashing if the input is bad.",[712,86672,86674],{"id":86673},"keep-conversions-close-to-where-data-is-used","Keep conversions close to where data is used",[14,86676,86677],{},"This makes code easier to understand.",[36,86679,86681],{"className":38,"code":86680,"language":40,"meta":41,"style":41},"price_text = \"9.99\"\ntotal = float(price_text) * 2\nprint(total)\n",[18,86682,86683,86696,86714],{"__ignoreMap":41},[45,86684,86685,86687,86689,86691,86694],{"class":47,"line":48},[45,86686,84507],{"class":51},[45,86688,56],{"class":55},[45,86690,1565],{"class":99},[45,86692,86693],{"class":103},"9.99",[45,86695,1571],{"class":99},[45,86697,86698,86700,86702,86704,86706,86708,86710,86712],{"class":47,"line":63},[45,86699,39569],{"class":51},[45,86701,56],{"class":55},[45,86703,79325],{"class":2109},[45,86705,96],{"class":85},[45,86707,84529],{"class":2070},[45,86709,9146],{"class":85},[45,86711,3559],{"class":55},[45,86713,13423],{"class":59},[45,86715,86716,86718,86720,86722],{"class":47,"line":70},[45,86717,2065],{"class":92},[45,86719,96],{"class":85},[45,86721,75281],{"class":2070},[45,86723,109],{"class":85},[14,86725,86726],{},"Instead of waiting many lines before converting, convert near the place where you need the correct type.",[31,86728,5903],{"id":5902},[14,86730,86731],{},"These are some very common beginner mistakes:",[195,86733,86734,86741,86745,86750,86758],{},[198,86735,86736,86737,230,86739],{},"Trying to convert non-numeric text with ",[18,86738,41891],{},[18,86740,41896],{},[198,86742,2043,86743,70081],{},[18,86744,18020],{},[198,86746,1350,86747,86749],{},[18,86748,8850],{}," between a string and an integer",[198,86751,86752,86753,15579,86756],{},"Assuming ",[18,86754,86755],{},"bool(\"False\")",[18,86757,233],{},[198,86759,86760,86761,86763,86764],{},"Trying ",[18,86762,41891],{}," on a decimal string like ",[18,86765,86766],{},"\"5.2\"",[14,86768,86769],{},"If something is not working, these quick checks help:",[36,86771,86773],{"className":38,"code":86772,"language":40,"meta":41,"style":41},"print(value)\nprint(type(value))\nprint(repr(value))\n\nvalue = input('Enter a number: ')\nprint(value.strip())\n",[18,86774,86775,86785,86799,86813,86817,86835],{"__ignoreMap":41},[45,86776,86777,86779,86781,86783],{"class":47,"line":48},[45,86778,2065],{"class":92},[45,86780,96],{"class":85},[45,86782,12722],{"class":2070},[45,86784,109],{"class":85},[45,86786,86787,86789,86791,86793,86795,86797],{"class":47,"line":63},[45,86788,2065],{"class":92},[45,86790,96],{"class":85},[45,86792,4340],{"class":2109},[45,86794,96],{"class":85},[45,86796,12722],{"class":2070},[45,86798,4348],{"class":85},[45,86800,86801,86803,86805,86807,86809,86811],{"class":47,"line":70},[45,86802,2065],{"class":92},[45,86804,96],{"class":85},[45,86806,42294],{"class":92},[45,86808,96],{"class":85},[45,86810,12722],{"class":2070},[45,86812,4348],{"class":85},[45,86814,86815],{"class":47,"line":89},[45,86816,67],{"emptyLinePlaceholder":66},[45,86818,86819,86821,86823,86825,86827,86829,86831,86833],{"class":47,"line":112},[45,86820,1476],{"class":51},[45,86822,56],{"class":55},[45,86824,18043],{"class":92},[45,86826,96],{"class":85},[45,86828,7901],{"class":99},[45,86830,45866],{"class":103},[45,86832,7901],{"class":99},[45,86834,109],{"class":85},[45,86836,86837,86839,86841,86843,86845,86847],{"class":47,"line":126},[45,86838,2065],{"class":92},[45,86840,96],{"class":85},[45,86842,12722],{"class":2070},[45,86844,240],{"class":85},[45,86846,24046],{"class":2070},[45,86848,10777],{"class":85},[14,86850,33334],{},[195,86852,86853,86857,86862,86867],{},[198,86854,86855,47343],{},[18,86856,42351],{},[198,86858,86859,86861],{},[18,86860,42345],{}," shows its type",[198,86863,86864,86866],{},[18,86865,42357],{}," shows hidden spaces or characters more clearly",[198,86868,86869,86872],{},[18,86870,86871],{},"value.strip()"," removes spaces at the start and end",[31,86874,2281],{"id":2280},[712,86876,86878],{"id":86877},"what-is-type-conversion-in-python","What is type conversion in Python?",[14,86880,86881],{},"It means changing a value from one type to another, such as from a string to an integer.",[712,86883,86885],{"id":86884},"is-casting-and-type-conversion-the-same-thing-in-python","Is casting and type conversion the same thing in Python?",[14,86887,86888,86889,21,86891,25,86893,240],{},"Beginners often use both terms for the same idea. In Python, this usually means using functions like ",[18,86890,41891],{},[18,86892,41896],{},[18,86894,41901],{},[712,86896,86898],{"id":86897},"why-does-input-need-conversion","Why does input() need conversion?",[14,86900,30405,86901,86903],{},[18,86902,18020],{}," always returns text, even if the user types a number.",[712,86905,86907],{"id":86906},"why-does-int105-fail","Why does int(\"10.5\") fail?",[14,86909,30405,86910,86912,86913,86916],{},[18,86911,41891],{}," expects a whole number string. Use ",[18,86914,86915],{},"float(\"10.5\")"," first if needed.",[712,86918,86920],{"id":86919},"why-is-boolfalse-true","Why is bool(\"False\") True?",[14,86922,86923,86924,86926,86927,240],{},"Because any non-empty string is ",[18,86925,229],{}," in Python, even the text ",[18,86928,86011],{},[31,86930,2342],{"id":2341},[195,86932,86933,86939,86945,86951,86957,86963,86968],{},[198,86934,86935],{},[236,86936,86937,19075],{"href":64106},[18,86938,41891],{},[198,86940,86941],{},[236,86942,86943,19075],{"href":64114},[18,86944,41896],{},[198,86946,86947],{},[236,86948,86949,19075],{"href":85852},[18,86950,41901],{},[198,86952,86953],{},[236,86954,86955,19075],{"href":34797},[18,86956,34800],{},[198,86958,86959],{},[236,86960,86962],{"href":86961},"\u002Fhow-to\u002Fhow-to-convert-user-input-to-numbers-in-python\u002F","How to convert user input to numbers in Python",[198,86964,86965],{},[236,86966,86967],{"href":42107},"How to convert a string to int in Python",[198,86969,86970],{},[236,86971,86972],{"href":64293},"How to convert a string to float in Python",[2367,86974,86975],{},"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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .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 .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}",{"title":41,"searchDepth":63,"depth":63,"links":86977},[86978,86979,86980,86981,86982,86983,86984,86985,86986,86987,86991,86996,86997,87004],{"id":33,"depth":63,"text":34},{"id":84659,"depth":63,"text":84660},{"id":84796,"depth":63,"text":84797},{"id":84905,"depth":63,"text":84906},{"id":85230,"depth":63,"text":85231},{"id":85482,"depth":63,"text":85483},{"id":85698,"depth":63,"text":85699},{"id":85861,"depth":63,"text":85862},{"id":86097,"depth":63,"text":86098},{"id":86252,"depth":63,"text":86253,"children":86988},[86989,86990],{"id":46606,"depth":70,"text":42097},{"id":46563,"depth":70,"text":7711},{"id":86480,"depth":63,"text":86481,"children":86992},[86993,86994,86995],{"id":86487,"depth":70,"text":86488},{"id":86573,"depth":70,"text":86574},{"id":86673,"depth":70,"text":86674},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":86998},[86999,87000,87001,87002,87003],{"id":86877,"depth":70,"text":86878},{"id":86884,"depth":70,"text":86885},{"id":86897,"depth":70,"text":86898},{"id":86906,"depth":70,"text":86907},{"id":86919,"depth":70,"text":86920},{"id":2341,"depth":63,"text":2342},"Master type conversion in python casting between types in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Ftype-conversion-in-python-casting-between-types",{"title":84426,"description":87005},"learn\u002Ftype-conversion-in-python-casting-between-types","-X2DWfPsyxD7T6jsgc8J26Trf76_KXuCrodeny3LOok",{"id":87012,"title":87013,"body":87014,"description":88514,"extension":2399,"meta":88515,"navigation":66,"path":88516,"seo":88517,"stem":88518,"__hash__":88519},"content\u002Flearn\u002Funderstanding-python-statements-and-expressions.md","Understanding Python Statements and Expressions",{"type":7,"value":87015,"toc":88469},[87016,87019,87027,87032,87037,87040,87044,87090,87096,87100,87114,87118,87121,87124,87146,87149,87163,87169,87173,87223,87225,87252,87258,87262,87265,87268,87285,87289,87336,87339,87377,87380,87388,87392,87395,87409,87411,87458,87461,87487,87491,87494,87498,87515,87527,87531,87546,87557,87561,87592,87607,87612,87616,87643,87659,87663,87666,87670,87697,87714,87716,87724,87728,87771,87787,87791,87834,87852,87857,87861,87864,87868,87871,87873,87901,87905,87908,87910,87938,87942,87945,87971,87975,87978,87996,88000,88003,88006,88023,88026,88034,88037,88039,88042,88046,88049,88051,88068,88079,88083,88086,88103,88116,88122,88145,88151,88162,88166,88193,88203,88207,88210,88301,88303,88338,88341,88367,88373,88375,88381,88390,88394,88397,88399,88416,88421,88425,88431,88435,88438,88440,88463,88466],[10,87017,87013],{"id":87018},"understanding-python-statements-and-expressions",[14,87020,87021,87022,1316,87024,87026],{},"When you start learning Python, you will see terms like ",[2421,87023,70709],{},[2421,87025,25253],{},". These words sound technical, but the idea is simple.",[14,87028,2419,87029,87031],{},[2421,87030,70709],{}," is code that tells Python to do something.",[14,87033,19451,87034,87036],{},[2421,87035,25253],{}," is code that Python evaluates to a value.",[14,87038,87039],{},"You will often see both working together in the same line of code. Once you understand that, beginner code becomes much easier to read.",[31,87041,87043],{"id":87042},"quick-rule","Quick rule",[36,87045,87047],{"className":38,"code":87046,"language":40,"meta":41,"style":41},"x = 10\nprint(x + 5)\n\n# x = 10 is a statement\n# x + 5 is an expression\n# print(x + 5) is a statement that contains an expression\n",[18,87048,87049,87057,87071,87075,87080,87085],{"__ignoreMap":41},[45,87050,87051,87053,87055],{"class":47,"line":48},[45,87052,1169],{"class":51},[45,87054,56],{"class":55},[45,87056,1174],{"class":59},[45,87058,87059,87061,87063,87065,87067,87069],{"class":47,"line":63},[45,87060,2065],{"class":92},[45,87062,96],{"class":85},[45,87064,1169],{"class":2070},[45,87066,8850],{"class":55},[45,87068,1222],{"class":59},[45,87070,109],{"class":85},[45,87072,87073],{"class":47,"line":70},[45,87074,67],{"emptyLinePlaceholder":66},[45,87076,87077],{"class":47,"line":89},[45,87078,87079],{"class":7624},"# x = 10 is a statement\n",[45,87081,87082],{"class":47,"line":112},[45,87083,87084],{"class":7624},"# x + 5 is an expression\n",[45,87086,87087],{"class":47,"line":126},[45,87088,87089],{"class":7624},"# print(x + 5) is a statement that contains an expression\n",[14,87091,87092,87093,240],{},"Use this simple rule: ",[2421,87094,87095],{},"an expression produces a value, while a statement performs an action",[31,87097,87099],{"id":87098},"what-this-page-helps-you-understand","What this page helps you understand",[195,87101,87102,87105,87108,87111],{},[198,87103,87104],{},"A statement is a line of code that does something",[198,87106,87107],{},"An expression is code that produces a value",[198,87109,87110],{},"Many Python lines contain both a statement and an expression",[198,87112,87113],{},"You do not need advanced theory to use this idea in practice",[31,87115,87117],{"id":87116},"what-is-a-statement-in-python","What is a statement in Python?",[14,87119,87120],{},"A statement tells Python to perform an action.",[14,87122,87123],{},"Common examples of statements include:",[195,87125,87126,87129,87133,87137,87141],{},[198,87127,87128],{},"variable assignment",[198,87130,87131,2037],{},[18,87132,20],{},[198,87134,87135,26095],{},[18,87136,7895],{},[198,87138,87139,2037],{},[18,87140,14870],{},[198,87142,87143,87144],{},"function definitions with ",[18,87145,2757],{},[14,87147,87148],{},"Statements often:",[195,87150,87151,87154,87157,87160],{},[198,87152,87153],{},"control the flow of a program",[198,87155,87156],{},"create or change variables",[198,87158,87159],{},"run blocks of code",[198,87161,87162],{},"perform actions",[14,87164,87165,87166,87168],{},"A statement does ",[2421,87167,2857],{}," need to return a value.",[712,87170,87172],{"id":87171},"examples-of-statements","Examples of statements",[36,87174,87176],{"className":38,"code":87175,"language":40,"meta":41,"style":41},"x = 10\nimport math\n\nif x > 5:\n    print(\"x is greater than 5\")\n",[18,87177,87178,87186,87192,87196,87208],{"__ignoreMap":41},[45,87179,87180,87182,87184],{"class":47,"line":48},[45,87181,1169],{"class":51},[45,87183,56],{"class":55},[45,87185,1174],{"class":59},[45,87187,87188,87190],{"class":47,"line":63},[45,87189,14870],{"class":73},[45,87191,14891],{"class":51},[45,87193,87194],{"class":47,"line":70},[45,87195,67],{"emptyLinePlaceholder":66},[45,87197,87198,87200,87202,87204,87206],{"class":47,"line":89},[45,87199,20],{"class":73},[45,87201,1185],{"class":51},[45,87203,272],{"class":55},[45,87205,1222],{"class":59},[45,87207,86],{"class":85},[45,87209,87210,87212,87214,87216,87219,87221],{"class":47,"line":112},[45,87211,93],{"class":92},[45,87213,96],{"class":85},[45,87215,100],{"class":99},[45,87217,87218],{"class":103},"x is greater than 5",[45,87220,100],{"class":99},[45,87222,109],{"class":85},[14,87224,6570],{},[195,87226,87227,87233,87238,87246],{},[198,87228,87229,87232],{},[18,87230,87231],{},"x = 10"," is an assignment statement",[198,87234,87235,87237],{},[18,87236,14973],{}," is an import statement",[198,87239,87240,87243,87244,65187],{},[18,87241,87242],{},"if x > 5:"," starts an ",[18,87245,20],{},[198,87247,87248,87251],{},[18,87249,87250],{},"print(\"x is greater than 5\")"," is commonly treated as a statement in beginner code because it performs an action",[14,87253,87254,87255,87257],{},"If you are still learning Python basics, it helps to first understand ",[236,87256,39616],{"href":39615}," because statements are a big part of Python syntax.",[31,87259,87261],{"id":87260},"what-is-an-expression-in-python","What is an expression in Python?",[14,87263,87264],{},"An expression is code that Python can evaluate to a value.",[14,87266,87267],{},"That value might be:",[195,87269,87270,87272,87274,87280,87282],{},[198,87271,74782],{},[198,87273,27427],{},[198,87275,87276,230,87278],{},[18,87277,229],{},[18,87279,233],{},[198,87281,27421],{},[198,87283,87284],{},"the result of a function call",[712,87286,87288],{"id":87287},"examples-of-expressions","Examples of expressions",[36,87290,87292],{"className":38,"code":87291,"language":40,"meta":41,"style":41},"5\n\"hello\"\nx + 1\nlen(\"Sam\")\nage > 18\n",[18,87293,87294,87298,87306,87314,87328],{"__ignoreMap":41},[45,87295,87296],{"class":47,"line":48},[45,87297,11570],{"class":59},[45,87299,87300,87302,87304],{"class":47,"line":63},[45,87301,100],{"class":39289},[45,87303,18782],{"class":39295},[45,87305,1571],{"class":39289},[45,87307,87308,87310,87312],{"class":47,"line":70},[45,87309,1169],{"class":51},[45,87311,8850],{"class":55},[45,87313,12962],{"class":59},[45,87315,87316,87318,87320,87322,87324,87326],{"class":47,"line":89},[45,87317,28716],{"class":92},[45,87319,96],{"class":85},[45,87321,100],{"class":99},[45,87323,1568],{"class":103},[45,87325,100],{"class":99},[45,87327,109],{"class":85},[45,87329,87330,87332,87334],{"class":47,"line":112},[45,87331,52],{"class":51},[45,87333,272],{"class":55},[45,87335,60],{"class":59},[14,87337,87338],{},"Each of these produces a value:",[195,87340,87341,87348,87353,87359,87367],{},[198,87342,87343,87345,87346],{},[18,87344,3749],{}," produces the value ",[18,87347,3749],{},[198,87349,87350,87352],{},[18,87351,40627],{}," produces a string",[198,87354,87355,87358],{},[18,87356,87357],{},"x + 1"," produces a calculated result",[198,87360,87361,87364,87365],{},[18,87362,87363],{},"len(\"Sam\")"," produces ",[18,87366,11184],{},[198,87368,87369,87372,87373,230,87375],{},[18,87370,87371],{},"age > 18"," produces either ",[18,87374,229],{},[18,87376,233],{},[14,87378,87379],{},"Expressions can be simple or built from smaller parts.",[14,87381,87382,87383,49645,87385,240],{},"You will often see expressions on the right side of ",[18,87384,56],{},[236,87386,87387],{"href":40782},"Python variables",[31,87389,87391],{"id":87390},"statement-vs-expression-the-simple-difference","Statement vs expression: the simple difference",[14,87393,87394],{},"The easiest way to remember the difference is this:",[195,87396,87397,87403],{},[198,87398,87399,87402],{},[2421,87400,87401],{},"Expression"," = produces a value",[198,87404,87405,87408],{},[2421,87406,87407],{},"Statement"," = performs an action",[14,87410,2617],{},[36,87412,87414],{"className":38,"code":87413,"language":40,"meta":41,"style":41},"price = 20\ntax = 5\ntotal = price + tax\n\nprint(total)\n",[18,87415,87416,87424,87432,87444,87448],{"__ignoreMap":41},[45,87417,87418,87420,87422],{"class":47,"line":48},[45,87419,40904],{"class":51},[45,87421,56],{"class":55},[45,87423,1883],{"class":59},[45,87425,87426,87428,87430],{"class":47,"line":63},[45,87427,75251],{"class":51},[45,87429,56],{"class":55},[45,87431,733],{"class":59},[45,87433,87434,87436,87438,87440,87442],{"class":47,"line":70},[45,87435,39569],{"class":51},[45,87437,56],{"class":55},[45,87439,39574],{"class":51},[45,87441,8850],{"class":55},[45,87443,75268],{"class":51},[45,87445,87446],{"class":47,"line":89},[45,87447,67],{"emptyLinePlaceholder":66},[45,87449,87450,87452,87454,87456],{"class":47,"line":112},[45,87451,2065],{"class":92},[45,87453,96],{"class":85},[45,87455,75281],{"class":2070},[45,87457,109],{"class":85},[14,87459,87460],{},"What is happening here?",[195,87462,87463,87468,87474,87480],{},[198,87464,87465,87232],{},[18,87466,87467],{},"total = price + tax",[198,87469,87470,87473],{},[18,87471,87472],{},"price + tax"," is an expression",[198,87475,87476,87479],{},[18,87477,87478],{},"print(total)"," performs an action, so beginners usually treat it as a statement",[198,87481,87482,19461,87484,87486],{},[18,87483,75281],{},[18,87485,87478],{}," is an expression because Python evaluates it to its current value",[31,87488,87490],{"id":87489},"common-examples-beginners-see","Common examples beginners see",[14,87492,87493],{},"These are patterns you will see often in real code.",[712,87495,87497],{"id":87496},"assignment","Assignment",[36,87499,87501],{"className":38,"code":87500,"language":40,"meta":41,"style":41},"name = \"Sam\"\n",[18,87502,87503],{"__ignoreMap":41},[45,87504,87505,87507,87509,87511,87513],{"class":47,"line":48},[45,87506,1560],{"class":51},[45,87508,56],{"class":55},[45,87510,1565],{"class":99},[45,87512,1568],{"class":103},[45,87514,1571],{"class":99},[195,87516,87517,87523],{},[198,87518,87519,87522],{},[18,87520,87521],{},"name = \"Sam\""," is a statement",[198,87524,87525,87473],{},[18,87526,7698],{},[712,87528,87530],{"id":87529},"printing-a-value","Printing a value",[36,87532,87534],{"className":38,"code":87533,"language":40,"meta":41,"style":41},"print(name)\n",[18,87535,87536],{"__ignoreMap":41},[45,87537,87538,87540,87542,87544],{"class":47,"line":48},[45,87539,2065],{"class":92},[45,87541,96],{"class":85},[45,87543,3096],{"class":2070},[45,87545,109],{"class":85},[195,87547,87548,87553],{},[198,87549,87550,87552],{},[18,87551,70662],{}," is usually treated as a statement in beginner explanations",[198,87554,87555,87473],{},[18,87556,3096],{},[712,87558,87560],{"id":87559},"condition-in-an-if-statement","Condition in an if statement",[36,87562,87564],{"className":38,"code":87563,"language":40,"meta":41,"style":41},"if score > 50:\n    print(\"Pass\")\n",[18,87565,87566,87578],{"__ignoreMap":41},[45,87567,87568,87570,87572,87574,87576],{"class":47,"line":48},[45,87569,20],{"class":73},[45,87571,597],{"class":51},[45,87573,272],{"class":55},[45,87575,20121],{"class":59},[45,87577,86],{"class":85},[45,87579,87580,87582,87584,87586,87588,87590],{"class":47,"line":63},[45,87581,93],{"class":92},[45,87583,96],{"class":85},[45,87585,100],{"class":99},[45,87587,71057],{"class":103},[45,87589,100],{"class":99},[45,87591,109],{"class":85},[195,87593,87594,87601],{},[198,87595,87596,32152,87599,65187],{},[18,87597,87598],{},"if score > 50:",[18,87600,20],{},[198,87602,87603,87606],{},[18,87604,87605],{},"score > 50"," is the condition expression",[14,87608,87609,87610,240],{},"If you want to go deeper into conditions, see ",[236,87611,549],{"href":548},[712,87613,87615],{"id":87614},"loop","Loop",[36,87617,87619],{"className":38,"code":87618,"language":40,"meta":41,"style":41},"for item in items:\n    print(item)\n",[18,87620,87621,87633],{"__ignoreMap":41},[45,87622,87623,87625,87627,87629,87631],{"class":47,"line":48},[45,87624,7895],{"class":73},[45,87626,8732],{"class":51},[45,87628,8623],{"class":73},[45,87630,23659],{"class":51},[45,87632,86],{"class":85},[45,87634,87635,87637,87639,87641],{"class":47,"line":63},[45,87636,93],{"class":92},[45,87638,96],{"class":85},[45,87640,23670],{"class":2070},[45,87642,109],{"class":85},[195,87644,87645,87652],{},[198,87646,87647,6578,87650,65187],{},[18,87648,87649],{},"for item in items:",[18,87651,7895],{},[198,87653,87654,1316,87656,87658],{},[18,87655,23670],{},[18,87657,9516],{}," are names used in the statement",[31,87660,87662],{"id":87661},"how-statements-and-expressions-work-together","How statements and expressions work together",[14,87664,87665],{},"Statements and expressions are not separate worlds. In real Python code, statements often contain expressions.",[712,87667,87669],{"id":87668},"assignment-uses-an-expression","Assignment uses an expression",[36,87671,87673],{"className":38,"code":87672,"language":40,"meta":41,"style":41},"x = 2 + 3\nprint(x)\n",[18,87674,87675,87687],{"__ignoreMap":41},[45,87676,87677,87679,87681,87683,87685],{"class":47,"line":48},[45,87678,1169],{"class":51},[45,87680,56],{"class":55},[45,87682,3754],{"class":59},[45,87684,3775],{"class":55},[45,87686,1481],{"class":59},[45,87688,87689,87691,87693,87695],{"class":47,"line":63},[45,87690,2065],{"class":92},[45,87692,96],{"class":85},[45,87694,26156],{"class":2070},[45,87696,109],{"class":85},[195,87698,87699,87704,87709],{},[198,87700,87701,87522],{},[18,87702,87703],{},"x = 2 + 3",[198,87705,87706,27546],{},[18,87707,87708],{},"2 + 3",[198,87710,87711,87712],{},"Python evaluates the expression first, then assigns the result to ",[18,87713,26156],{},[14,87715,177],{},[36,87717,87718],{"className":38,"code":11570,"language":40,"meta":41,"style":41},[18,87719,87720],{"__ignoreMap":41},[45,87721,87722],{"class":47,"line":48},[45,87723,11570],{"class":59},[712,87725,87727],{"id":87726},"if-statements-use-expressions-as-conditions","If statements use expressions as conditions",[36,87729,87731],{"className":38,"code":87730,"language":40,"meta":41,"style":41},"age = 20\n\nif age >= 18:\n    print(\"Adult\")\n",[18,87732,87733,87741,87745,87757],{"__ignoreMap":41},[45,87734,87735,87737,87739],{"class":47,"line":48},[45,87736,52],{"class":51},[45,87738,56],{"class":55},[45,87740,1883],{"class":59},[45,87742,87743],{"class":47,"line":63},[45,87744,67],{"emptyLinePlaceholder":66},[45,87746,87747,87749,87751,87753,87755],{"class":47,"line":70},[45,87748,20],{"class":73},[45,87750,76],{"class":51},[45,87752,600],{"class":55},[45,87754,121],{"class":59},[45,87756,86],{"class":85},[45,87758,87759,87761,87763,87765,87767,87769],{"class":47,"line":89},[45,87760,93],{"class":92},[45,87762,96],{"class":85},[45,87764,100],{"class":99},[45,87766,1710],{"class":103},[45,87768,100],{"class":99},[45,87770,109],{"class":85},[195,87772,87773,87778],{},[198,87774,87775,87522],{},[18,87776,87777],{},"if age >= 18:",[198,87779,87780,87782,87783,230,87785],{},[18,87781,55760],{}," is an expression that becomes ",[18,87784,229],{},[18,87786,233],{},[712,87788,87790],{"id":87789},"function-calls-can-produce-values","Function calls can produce values",[36,87792,87794],{"className":38,"code":87793,"language":40,"meta":41,"style":41},"name = \"hello\"\nlength = len(name)\nprint(length)\n",[18,87795,87796,87808,87823],{"__ignoreMap":41},[45,87797,87798,87800,87802,87804,87806],{"class":47,"line":48},[45,87799,1560],{"class":51},[45,87801,56],{"class":55},[45,87803,1565],{"class":99},[45,87805,18782],{"class":103},[45,87807,1571],{"class":99},[45,87809,87810,87813,87815,87817,87819,87821],{"class":47,"line":63},[45,87811,87812],{"class":51},"length ",[45,87814,56],{"class":55},[45,87816,9139],{"class":92},[45,87818,96],{"class":85},[45,87820,3096],{"class":2070},[45,87822,109],{"class":85},[45,87824,87825,87827,87829,87832],{"class":47,"line":70},[45,87826,2065],{"class":92},[45,87828,96],{"class":85},[45,87830,87831],{"class":2070},"length",[45,87833,109],{"class":85},[195,87835,87836,87841,87847],{},[198,87837,87838,87522],{},[18,87839,87840],{},"length = len(name)",[198,87842,87843,87846],{},[18,87844,87845],{},"len(name)"," is an expression because it returns a value",[198,87848,87849,87851],{},[18,87850,3096],{}," is also an expression when Python reads its value",[14,87853,87854,87855,17902],{},"If functions still feel unclear, ",[236,87856,5879],{"href":4579},[31,87858,87860],{"id":87859},"how-to-identify-each-one-in-code","How to identify each one in code",[14,87862,87863],{},"When you are not sure, ask these questions.",[712,87865,87867],{"id":87866},"ask-does-this-code-produce-a-value","Ask: does this code produce a value?",[14,87869,87870],{},"If yes, it is an expression.",[14,87872,40802],{},[195,87874,87875,87879,87883,87887,87891,87896],{},[198,87876,87877],{},[18,87878,11786],{},[198,87880,87881],{},[18,87882,7075],{},[198,87884,87885],{},[18,87886,26156],{},[198,87888,87889],{},[18,87890,87357],{},[198,87892,87893],{},[18,87894,87895],{},"len(text)",[198,87897,87898],{},[18,87899,87900],{},"4 > 2",[712,87902,87904],{"id":87903},"ask-does-this-code-tell-python-to-do-something","Ask: does this code tell Python to do something?",[14,87906,87907],{},"If yes, it is a statement.",[14,87909,40802],{},[195,87911,87912,87916,87920,87924,87929,87934],{},[198,87913,87914],{},[18,87915,87231],{},[198,87917,87918],{},[18,87919,87242],{},[198,87921,87922],{},[18,87923,87649],{},[198,87925,87926],{},[18,87927,87928],{},"while count \u003C 3:",[198,87930,87931],{},[18,87932,87933],{},"def greet():",[198,87935,87936],{},[18,87937,14973],{},[712,87939,87941],{"id":87940},"look-for-common-statement-patterns","Look for common statement patterns",[14,87943,87944],{},"These often signal statements:",[195,87946,87947,87951,87955,87959,87963,87967],{},[198,87948,87949],{},[18,87950,56],{},[198,87952,87953],{},[18,87954,20],{},[198,87956,87957],{},[18,87958,7895],{},[198,87960,87961],{},[18,87962,23529],{},[198,87964,87965],{},[18,87966,2757],{},[198,87968,87969],{},[18,87970,14870],{},[712,87972,87974],{"id":87973},"look-for-common-expression-patterns","Look for common expression patterns",[14,87976,87977],{},"These often signal expressions:",[195,87979,87980,87982,87984,87987,87990,87993],{},[198,87981,9069],{},[198,87983,22296],{},[198,87985,87986],{},"variable names",[198,87988,87989],{},"calculations",[198,87991,87992],{},"comparisons",[198,87994,87995],{},"function results",[31,87997,87999],{"id":87998},"why-this-matters-for-beginners","Why this matters for beginners",[14,88001,88002],{},"This idea helps more than many beginners expect.",[14,88004,88005],{},"It can help you:",[195,88007,88008,88011,88014,88017,88020],{},[198,88009,88010],{},"read code more clearly",[198,88012,88013],{},"understand what each part of a line is doing",[198,88015,88016],{},"make better sense of error messages",[198,88018,88019],{},"learn variables, conditions, loops, and functions faster",[198,88021,88022],{},"write cleaner Python code",[14,88024,88025],{},"You do not need formal computer science theory here. You just need to recognize:",[195,88027,88028,88031],{},[198,88029,88030],{},"what part gives a value",[198,88032,88033],{},"what part performs an action",[14,88035,88036],{},"That is enough for most beginner Python code.",[31,88038,5903],{"id":5902},[14,88040,88041],{},"Beginners often get confused in these situations.",[712,88043,88045],{"id":88044},"thinking-every-line-of-code-is-only-one-thing","Thinking every line of code is only one thing",[14,88047,88048],{},"Some lines contain both a statement and one or more expressions.",[14,88050,466],{},[36,88052,88054],{"className":38,"code":88053,"language":40,"meta":41,"style":41},"x = 10 + 5\n",[18,88055,88056],{"__ignoreMap":41},[45,88057,88058,88060,88062,88064,88066],{"class":47,"line":48},[45,88059,1169],{"class":51},[45,88061,56],{"class":55},[45,88063,1190],{"class":59},[45,88065,3775],{"class":55},[45,88067,733],{"class":59},[195,88069,88070,88073],{},[198,88071,88072],{},"whole line: statement",[198,88074,88075,88078],{},[18,88076,88077],{},"10 + 5",": expression",[712,88080,88082],{"id":88081},"confusing-assignment-with-calculation","Confusing assignment with calculation",[14,88084,88085],{},"In this code:",[36,88087,88089],{"className":38,"code":88088,"language":40,"meta":41,"style":41},"total = price + tax\n",[18,88090,88091],{"__ignoreMap":41},[45,88092,88093,88095,88097,88099,88101],{"class":47,"line":48},[45,88094,39569],{"class":51},[45,88096,56],{"class":55},[45,88098,39574],{"class":51},[45,88100,8850],{"class":55},[45,88102,75268],{"class":51},[195,88104,88105,88111],{},[198,88106,88107,88110],{},[18,88108,88109],{},"total = ..."," is the statement",[198,88112,88113,88115],{},[18,88114,87472],{}," is the calculation expression",[712,88117,86752,88119,88121],{"id":88118},"assuming-print-is-the-same-as-the-value-it-displays",[18,88120,15833],{}," is the same as the value it displays",[36,88123,88125],{"className":38,"code":88124,"language":40,"meta":41,"style":41},"x = 7\nprint(x)\n",[18,88126,88127,88135],{"__ignoreMap":41},[45,88128,88129,88131,88133],{"class":47,"line":48},[45,88130,1169],{"class":51},[45,88132,56],{"class":55},[45,88134,55913],{"class":59},[45,88136,88137,88139,88141,88143],{"class":47,"line":63},[45,88138,2065],{"class":92},[45,88140,96],{"class":85},[45,88142,26156],{"class":2070},[45,88144,109],{"class":85},[14,88146,88147,88150],{},[18,88148,88149],{},"print(x)"," shows the value on the screen, but it is not the same thing as the value itself.",[195,88152,88153,88157],{},[198,88154,88155,87473],{},[18,88156,26156],{},[198,88158,88159,88161],{},[18,88160,88149],{}," performs an action",[712,88163,88165],{"id":88164},"not-realizing-conditions-are-expressions","Not realizing conditions are expressions",[36,88167,88169],{"className":38,"code":88168,"language":40,"meta":41,"style":41},"x = 10\nprint(x > 5)\n",[18,88170,88171,88179],{"__ignoreMap":41},[45,88172,88173,88175,88177],{"class":47,"line":48},[45,88174,1169],{"class":51},[45,88176,56],{"class":55},[45,88178,1174],{"class":59},[45,88180,88181,88183,88185,88187,88189,88191],{"class":47,"line":63},[45,88182,2065],{"class":92},[45,88184,96],{"class":85},[45,88186,1169],{"class":2070},[45,88188,272],{"class":55},[45,88190,1222],{"class":59},[45,88192,109],{"class":85},[14,88194,3444,88195,88198,88199,230,88201,240],{},[18,88196,88197],{},"x > 5"," is an expression. It produces ",[18,88200,229],{},[18,88202,233],{},[31,88204,88206],{"id":88205},"try-these-examples-yourself","Try these examples yourself",[14,88208,88209],{},"Run these small examples and notice which parts are statements and which parts are expressions.",[36,88211,88213],{"className":38,"code":88212,"language":40,"meta":41,"style":41},"print(type(5))\nprint(2 + 3)\n\nx = 10\nprint(x)\n\nprint(len(\"hello\"))\nprint(4 > 2)\n",[18,88214,88215,88229,88243,88247,88255,88265,88269,88287],{"__ignoreMap":41},[45,88216,88217,88219,88221,88223,88225,88227],{"class":47,"line":48},[45,88218,2065],{"class":92},[45,88220,96],{"class":85},[45,88222,4340],{"class":2109},[45,88224,96],{"class":85},[45,88226,3749],{"class":59},[45,88228,4348],{"class":85},[45,88230,88231,88233,88235,88237,88239,88241],{"class":47,"line":63},[45,88232,2065],{"class":92},[45,88234,96],{"class":85},[45,88236,9367],{"class":59},[45,88238,3775],{"class":55},[45,88240,777],{"class":59},[45,88242,109],{"class":85},[45,88244,88245],{"class":47,"line":70},[45,88246,67],{"emptyLinePlaceholder":66},[45,88248,88249,88251,88253],{"class":47,"line":89},[45,88250,1169],{"class":51},[45,88252,56],{"class":55},[45,88254,1174],{"class":59},[45,88256,88257,88259,88261,88263],{"class":47,"line":112},[45,88258,2065],{"class":92},[45,88260,96],{"class":85},[45,88262,26156],{"class":2070},[45,88264,109],{"class":85},[45,88266,88267],{"class":47,"line":126},[45,88268,67],{"emptyLinePlaceholder":66},[45,88270,88271,88273,88275,88277,88279,88281,88283,88285],{"class":47,"line":142},[45,88272,2065],{"class":92},[45,88274,96],{"class":85},[45,88276,28716],{"class":92},[45,88278,96],{"class":85},[45,88280,100],{"class":99},[45,88282,18782],{"class":103},[45,88284,100],{"class":99},[45,88286,4348],{"class":85},[45,88288,88289,88291,88293,88295,88297,88299],{"class":47,"line":149},[45,88290,2065],{"class":92},[45,88292,96],{"class":85},[45,88294,3584],{"class":59},[45,88296,33591],{"class":55},[45,88298,3754],{"class":59},[45,88300,109],{"class":85},[14,88302,17038],{},[36,88304,88306],{"className":38,"code":88305,"language":40,"meta":41,"style":41},"\u003Cclass 'int'>\n5\n10\n5\nTrue\n",[18,88307,88308,88322,88326,88330,88334],{"__ignoreMap":41},[45,88309,88310,88312,88314,88316,88318,88320],{"class":47,"line":48},[45,88311,79],{"class":55},[45,88313,2441],{"class":2440},[45,88315,9199],{"class":99},[45,88317,29738],{"class":103},[45,88319,7901],{"class":99},[45,88321,13484],{"class":55},[45,88323,88324],{"class":47,"line":63},[45,88325,11570],{"class":59},[45,88327,88328],{"class":47,"line":70},[45,88329,13262],{"class":59},[45,88331,88332],{"class":47,"line":89},[45,88333,11570],{"class":59},[45,88335,88336],{"class":47,"line":112},[45,88337,2270],{"class":481},[14,88339,88340],{},"What to notice:",[195,88342,88343,88357,88362],{},[198,88344,88345,21,88347,21,88349,21,88351,25,88354,88356],{},[18,88346,3749],{},[18,88348,87708],{},[18,88350,26156],{},[18,88352,88353],{},"len(\"hello\")",[18,88355,87900],{}," are expressions",[198,88358,3267,88359,88361],{},[18,88360,71609],{}," lines perform actions",[198,88363,88364,88366],{},[18,88365,87231],{}," is a statement that sets a variable",[14,88368,88369,88370,240],{},"If you are new to variables, you may also want to read ",[236,88371,88372],{"href":74856},"what a variable means in Python",[31,88374,2281],{"id":2280},[712,88376,350,88378,88380],{"id":88377},"is-print-a-statement-or-an-expression-in-python",[18,88379,15833],{}," a statement or an expression in Python?",[14,88382,88383,88384,88386,88387,88389],{},"In beginner-focused Python explanations, ",[18,88385,71609],{}," is best treated as a statement because it performs an action. The value inside ",[18,88388,71609],{}," is usually an expression.",[712,88391,88393],{"id":88392},"can-a-statement-contain-an-expression","Can a statement contain an expression?",[14,88395,88396],{},"Yes. This is very common.",[14,88398,466],{},[36,88400,88402],{"className":38,"code":88401,"language":40,"meta":41,"style":41},"x = 2 + 3\n",[18,88403,88404],{"__ignoreMap":41},[45,88405,88406,88408,88410,88412,88414],{"class":47,"line":48},[45,88407,1169],{"class":51},[45,88409,56],{"class":55},[45,88411,3754],{"class":59},[45,88413,3775],{"class":55},[45,88415,1481],{"class":59},[14,88417,88418,88419,240],{},"This is a statement that contains the expression ",[18,88420,87708],{},[712,88422,88424],{"id":88423},"is-a-variable-name-an-expression","Is a variable name an expression?",[14,88426,88427,88428,88430],{},"Yes. A variable name like ",[18,88429,26156],{}," can be used as an expression because Python evaluates it to its current value.",[712,88432,88434],{"id":88433},"why-do-i-need-to-learn-statements-and-expressions","Why do I need to learn statements and expressions?",[14,88436,88437],{},"It helps you understand how Python code is built. That makes it easier to read examples, write code, and fix mistakes.",[31,88439,2342],{"id":2341},[195,88441,88442,88446,88450,88454,88458],{},[198,88443,88444],{},[236,88445,40356],{"href":39615},[198,88447,88448],{},[236,88449,40783],{"href":40782},[198,88451,88452],{},[236,88453,549],{"href":548},[198,88455,88456],{},[236,88457,5879],{"href":4579},[198,88459,88460],{},[236,88461,88462],{"href":74856},"What is a variable in Python?",[14,88464,88465],{},"Understanding statements and expressions is a strong first step. The next best topics are Python syntax and variables, because these ideas become much clearer when you see them in real beginner code.",[2367,88467,88468],{},"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 .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 .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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--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}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":88470},[88471,88472,88473,88476,88479,88480,88486,88491,88497,88498,88505,88506,88513],{"id":87042,"depth":63,"text":87043},{"id":87098,"depth":63,"text":87099},{"id":87116,"depth":63,"text":87117,"children":88474},[88475],{"id":87171,"depth":70,"text":87172},{"id":87260,"depth":63,"text":87261,"children":88477},[88478],{"id":87287,"depth":70,"text":87288},{"id":87390,"depth":63,"text":87391},{"id":87489,"depth":63,"text":87490,"children":88481},[88482,88483,88484,88485],{"id":87496,"depth":70,"text":87497},{"id":87529,"depth":70,"text":87530},{"id":87559,"depth":70,"text":87560},{"id":87614,"depth":70,"text":87615},{"id":87661,"depth":63,"text":87662,"children":88487},[88488,88489,88490],{"id":87668,"depth":70,"text":87669},{"id":87726,"depth":70,"text":87727},{"id":87789,"depth":70,"text":87790},{"id":87859,"depth":63,"text":87860,"children":88492},[88493,88494,88495,88496],{"id":87866,"depth":70,"text":87867},{"id":87903,"depth":70,"text":87904},{"id":87940,"depth":70,"text":87941},{"id":87973,"depth":70,"text":87974},{"id":87998,"depth":63,"text":87999},{"id":5902,"depth":63,"text":5903,"children":88499},[88500,88501,88502,88504],{"id":88044,"depth":70,"text":88045},{"id":88081,"depth":70,"text":88082},{"id":88118,"depth":70,"text":88503},"Assuming print() is the same as the value it displays",{"id":88164,"depth":70,"text":88165},{"id":88205,"depth":63,"text":88206},{"id":2280,"depth":63,"text":2281,"children":88507},[88508,88510,88511,88512],{"id":88377,"depth":70,"text":88509},"Is print() a statement or an expression in Python?",{"id":88392,"depth":70,"text":88393},{"id":88423,"depth":70,"text":88424},{"id":88433,"depth":70,"text":88434},{"id":2341,"depth":63,"text":2342},"Master understanding python statements and expressions in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Funderstanding-python-statements-and-expressions",{"title":87013,"description":88514},"learn\u002Funderstanding-python-statements-and-expressions","8XEMCgq1NOecs-dbGzxPOLHVCvERLF9wzbpQKUvzvQw",{"id":88521,"title":80250,"body":88522,"description":91111,"extension":2399,"meta":91112,"navigation":66,"path":46079,"seo":91113,"stem":91114,"__hash__":91115},"content\u002Flearn\u002Fusing-try-except-else-and-finally-in-python.md",{"type":7,"value":88523,"toc":91065},[88524,88527,88530,88532,88632,88646,88648,88670,88676,88681,88684,88689,88704,88706,88767,88769,88781,88794,88799,88804,88811,88814,88817,88831,88833,88885,88887,88899,88905,88954,88957,88959,89010,89017,89022,89030,89032,89044,89049,89051,89128,89130,89145,89148,89162,89170,89175,89180,89183,89197,89199,89269,89271,89286,89299,89301,89369,89371,89388,89392,89397,89401,89403,89417,89419,89510,89512,89532,89536,89538,89552,89554,89645,89647,89661,89665,89674,89743,89745,89778,89782,89785,89807,89809,89831,89835,89838,89860,89959,89964,89993,89997,90024,90030,90034,90037,90060,90225,90232,90249,90254,90258,90282,90284,90357,90363,90369,90371,90377,90380,90475,90478,90575,90578,90584,90587,90590,90603,90606,90617,90624,90626,90638,90644,90650,90654,90657,90694,90697,90700,90702,90705,90740,90742,90745,90755,90808,90811,90836,90838,90842,90849,90856,90864,90870,90873,90879,90882,90889,90895,90897,91031,91033,91062],[10,88525,80250],{"id":88526},"using-try-except-else-and-finally-in-python",[14,88528,88529],{},"Learn how Python exception handling blocks work together so you can catch errors, run code only when no error happens, and always clean up resources.",[31,88531,34],{"id":33},[36,88533,88535],{"className":38,"code":88534,"language":40,"meta":41,"style":41},"try:\n    number = int(input(\"Enter a number: \"))\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\nelse:\n    print(\"You entered:\", number)\nfinally:\n    print(\"Program finished.\")\n",[18,88536,88537,88543,88565,88573,88587,88593,88611,88617],{"__ignoreMap":41},[45,88538,88539,88541],{"class":47,"line":48},[45,88540,23272],{"class":73},[45,88542,86],{"class":85},[45,88544,88545,88547,88549,88551,88553,88555,88557,88559,88561,88563],{"class":47,"line":63},[45,88546,12957],{"class":51},[45,88548,56],{"class":55},[45,88550,41669],{"class":2109},[45,88552,96],{"class":85},[45,88554,45859],{"class":92},[45,88556,96],{"class":85},[45,88558,100],{"class":99},[45,88560,45866],{"class":103},[45,88562,100],{"class":99},[45,88564,4348],{"class":85},[45,88566,88567,88569,88571],{"class":47,"line":70},[45,88568,23275],{"class":73},[45,88570,45892],{"class":2109},[45,88572,86],{"class":85},[45,88574,88575,88577,88579,88581,88583,88585],{"class":47,"line":89},[45,88576,93],{"class":92},[45,88578,96],{"class":85},[45,88580,100],{"class":99},[45,88582,45905],{"class":103},[45,88584,100],{"class":99},[45,88586,109],{"class":85},[45,88588,88589,88591],{"class":47,"line":112},[45,88590,28],{"class":73},[45,88592,86],{"class":85},[45,88594,88595,88597,88599,88601,88603,88605,88607,88609],{"class":47,"line":126},[45,88596,93],{"class":92},[45,88598,96],{"class":85},[45,88600,100],{"class":99},[45,88602,86636],{"class":103},[45,88604,100],{"class":99},[45,88606,3079],{"class":85},[45,88608,35957],{"class":2070},[45,88610,109],{"class":85},[45,88612,88613,88615],{"class":47,"line":142},[45,88614,57769],{"class":73},[45,88616,86],{"class":85},[45,88618,88619,88621,88623,88625,88628,88630],{"class":47,"line":149},[45,88620,93],{"class":92},[45,88622,96],{"class":85},[45,88624,100],{"class":99},[45,88626,88627],{"class":103},"Program finished.",[45,88629,100],{"class":99},[45,88631,109],{"class":85},[14,88633,165,88634,88636,88637,88639,88640,88642,88643,88645],{},[18,88635,23272],{}," for risky code, ",[18,88638,23275],{}," for handling specific errors, ",[18,88641,28],{}," for success-only code, and ",[18,88644,57769],{}," for cleanup code that must always run.",[31,88647,2530],{"id":2529},[195,88649,88650,88661,88664,88667],{},[198,88651,88652,88653,21,88655,21,88657,25,88659],{},"Explain the purpose of ",[18,88654,23272],{},[18,88656,23275],{},[18,88658,28],{},[18,88660,57769],{},[198,88662,88663],{},"Show the order these blocks run",[198,88665,88666],{},"Help beginners choose the right block for each task",[198,88668,88669],{},"Focus on practical exception handling patterns",[31,88671,5754,88673,88675],{"id":88672},"what-try-does",[18,88674,23272],{}," does",[14,88677,88678,88679,1864],{},"Put code that might fail inside a ",[18,88680,23272],{},[14,88682,88683],{},"Python watches this block for exceptions. An exception is an error that happens while the program is running.",[14,88685,88686,88687,457],{},"If an error happens inside ",[18,88688,23272],{},[195,88690,88691,88696,88701],{},[198,88692,88693,88694,55438],{},"Python stops running the rest of that ",[18,88695,23272],{},[198,88697,88698,88699,55438],{},"Python looks for a matching ",[18,88700,23275],{},[198,88702,88703],{},"If it finds one, that handler runs",[14,88705,466],{},[36,88707,88709],{"className":38,"code":88708,"language":40,"meta":41,"style":41},"try:\n    result = 10 \u002F 0\n    print(\"This line will not run.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n",[18,88710,88711,88717,88730,88745,88753],{"__ignoreMap":41},[45,88712,88713,88715],{"class":47,"line":48},[45,88714,23272],{"class":73},[45,88716,86],{"class":85},[45,88718,88719,88722,88724,88726,88728],{"class":47,"line":63},[45,88720,88721],{"class":51},"    result ",[45,88723,56],{"class":55},[45,88725,1190],{"class":59},[45,88727,45881],{"class":55},[45,88729,34967],{"class":59},[45,88731,88732,88734,88736,88738,88741,88743],{"class":47,"line":70},[45,88733,93],{"class":92},[45,88735,96],{"class":85},[45,88737,100],{"class":99},[45,88739,88740],{"class":103},"This line will not run.",[45,88742,100],{"class":99},[45,88744,109],{"class":85},[45,88746,88747,88749,88751],{"class":47,"line":89},[45,88748,23275],{"class":73},[45,88750,45916],{"class":2109},[45,88752,86],{"class":85},[45,88754,88755,88757,88759,88761,88763,88765],{"class":47,"line":112},[45,88756,93],{"class":92},[45,88758,96],{"class":85},[45,88760,100],{"class":99},[45,88762,45929],{"class":103},[45,88764,100],{"class":99},[45,88766,109],{"class":85},[14,88768,177],{},[36,88770,88772],{"className":38,"code":88771,"language":40,"meta":41,"style":41},"You cannot divide by zero.\n",[18,88773,88774],{"__ignoreMap":41},[45,88775,88776,88779],{"class":47,"line":48},[45,88777,88778],{"class":51},"You cannot divide by zero",[45,88780,2708],{"class":85},[14,88782,3444,88783,88786,88787,88789,88790,19461,88792,240],{},[18,88784,88785],{},"10 \u002F 0"," raises a ",[18,88788,46837],{},", so Python skips the ",[18,88791,15833],{},[18,88793,23272],{},[14,88795,88796,88797,240],{},"If you are new to exceptions, see ",[236,88798,35173],{"href":35172},[31,88800,5754,88802,88675],{"id":88801},"what-except-does",[18,88803,23275],{},[14,88805,88806,88808,88809,240],{},[18,88807,23275],{}," handles an error that happened in ",[18,88810,23272],{},[14,88812,88813],{},"You should catch specific exceptions when possible. This makes your code easier to understand and debug.",[14,88815,88816],{},"Common exception types include:",[195,88818,88819,88823,88827],{},[198,88820,88821],{},[18,88822,42097],{},[198,88824,88825],{},[18,88826,46837],{},[198,88828,88829],{},[18,88830,46770],{},[14,88832,466],{},[36,88834,88836],{"className":38,"code":88835,"language":40,"meta":41,"style":41},"try:\n    age = int(\"hello\")\nexcept ValueError:\n    print(\"That value cannot be converted to an integer.\")\n",[18,88837,88838,88844,88862,88870],{"__ignoreMap":41},[45,88839,88840,88842],{"class":47,"line":48},[45,88841,23272],{"class":73},[45,88843,86],{"class":85},[45,88845,88846,88848,88850,88852,88854,88856,88858,88860],{"class":47,"line":63},[45,88847,46894],{"class":51},[45,88849,56],{"class":55},[45,88851,41669],{"class":2109},[45,88853,96],{"class":85},[45,88855,100],{"class":99},[45,88857,18782],{"class":103},[45,88859,100],{"class":99},[45,88861,109],{"class":85},[45,88863,88864,88866,88868],{"class":47,"line":70},[45,88865,23275],{"class":73},[45,88867,45892],{"class":2109},[45,88869,86],{"class":85},[45,88871,88872,88874,88876,88878,88881,88883],{"class":47,"line":89},[45,88873,93],{"class":92},[45,88875,96],{"class":85},[45,88877,100],{"class":99},[45,88879,88880],{"class":103},"That value cannot be converted to an integer.",[45,88882,100],{"class":99},[45,88884,109],{"class":85},[14,88886,177],{},[36,88888,88890],{"className":38,"code":88889,"language":40,"meta":41,"style":41},"That value cannot be converted to an integer.\n",[18,88891,88892],{"__ignoreMap":41},[45,88893,88894,88897],{"class":47,"line":48},[45,88895,88896],{"class":51},"That value cannot be converted to an integer",[45,88898,2708],{"class":85},[14,88900,88901,88902,88904],{},"Try to avoid a bare ",[18,88903,23275],{}," like this:",[36,88906,88908],{"className":38,"code":88907,"language":40,"meta":41,"style":41},"try:\n    age = int(\"hello\")\nexcept:\n    print(\"Something went wrong.\")\n",[18,88909,88910,88916,88934,88940],{"__ignoreMap":41},[45,88911,88912,88914],{"class":47,"line":48},[45,88913,23272],{"class":73},[45,88915,86],{"class":85},[45,88917,88918,88920,88922,88924,88926,88928,88930,88932],{"class":47,"line":63},[45,88919,46894],{"class":51},[45,88921,56],{"class":55},[45,88923,41669],{"class":2109},[45,88925,96],{"class":85},[45,88927,100],{"class":99},[45,88929,18782],{"class":103},[45,88931,100],{"class":99},[45,88933,109],{"class":85},[45,88935,88936,88938],{"class":47,"line":70},[45,88937,23275],{"class":73},[45,88939,86],{"class":85},[45,88941,88942,88944,88946,88948,88950,88952],{"class":47,"line":89},[45,88943,93],{"class":92},[45,88945,96],{"class":85},[45,88947,100],{"class":99},[45,88949,47116],{"class":103},[45,88951,100],{"class":99},[45,88953,109],{"class":85},[14,88955,88956],{},"This catches almost everything, which can hide useful error details.",[14,88958,1742],{},[36,88960,88962],{"className":38,"code":88961,"language":40,"meta":41,"style":41},"try:\n    age = int(\"hello\")\nexcept ValueError:\n    print(\"Please enter a valid number.\")\n",[18,88963,88964,88970,88988,88996],{"__ignoreMap":41},[45,88965,88966,88968],{"class":47,"line":48},[45,88967,23272],{"class":73},[45,88969,86],{"class":85},[45,88971,88972,88974,88976,88978,88980,88982,88984,88986],{"class":47,"line":63},[45,88973,46894],{"class":51},[45,88975,56],{"class":55},[45,88977,41669],{"class":2109},[45,88979,96],{"class":85},[45,88981,100],{"class":99},[45,88983,18782],{"class":103},[45,88985,100],{"class":99},[45,88987,109],{"class":85},[45,88989,88990,88992,88994],{"class":47,"line":70},[45,88991,23275],{"class":73},[45,88993,45892],{"class":2109},[45,88995,86],{"class":85},[45,88997,88998,89000,89002,89004,89006,89008],{"class":47,"line":89},[45,88999,93],{"class":92},[45,89001,96],{"class":85},[45,89003,100],{"class":99},[45,89005,46954],{"class":103},[45,89007,100],{"class":99},[45,89009,109],{"class":85},[14,89011,89012,89013,240],{},"If you want more practice with this pattern, see ",[236,89014,89016],{"href":89015},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use try except blocks in Python",[31,89018,5754,89020,88675],{"id":89019},"what-else-does",[18,89021,28],{},[14,89023,89024,89026,89027,89029],{},[18,89025,28],{}," runs only if the ",[18,89028,23272],{}," block succeeds.",[14,89031,2727],{},[195,89033,89034,89039],{},[198,89035,89036,89037],{},"no exception happened in ",[18,89038,23272],{},[198,89040,89041,89042],{},"Python did not jump to ",[18,89043,23275],{},[14,89045,165,89046,89048],{},[18,89047,28],{}," for code that should run only after the risky code worked.",[14,89050,466],{},[36,89052,89054],{"className":38,"code":89053,"language":40,"meta":41,"style":41},"try:\n    number = int(\"42\")\nexcept ValueError:\n    print(\"Invalid number\")\nelse:\n    print(\"Conversion worked:\", number)\n",[18,89055,89056,89062,89080,89088,89103,89109],{"__ignoreMap":41},[45,89057,89058,89060],{"class":47,"line":48},[45,89059,23272],{"class":73},[45,89061,86],{"class":85},[45,89063,89064,89066,89068,89070,89072,89074,89076,89078],{"class":47,"line":63},[45,89065,12957],{"class":51},[45,89067,56],{"class":55},[45,89069,41669],{"class":2109},[45,89071,96],{"class":85},[45,89073,100],{"class":99},[45,89075,41920],{"class":103},[45,89077,100],{"class":99},[45,89079,109],{"class":85},[45,89081,89082,89084,89086],{"class":47,"line":70},[45,89083,23275],{"class":73},[45,89085,45892],{"class":2109},[45,89087,86],{"class":85},[45,89089,89090,89092,89094,89096,89099,89101],{"class":47,"line":89},[45,89091,93],{"class":92},[45,89093,96],{"class":85},[45,89095,100],{"class":99},[45,89097,89098],{"class":103},"Invalid number",[45,89100,100],{"class":99},[45,89102,109],{"class":85},[45,89104,89105,89107],{"class":47,"line":112},[45,89106,28],{"class":73},[45,89108,86],{"class":85},[45,89110,89111,89113,89115,89117,89120,89122,89124,89126],{"class":47,"line":126},[45,89112,93],{"class":92},[45,89114,96],{"class":85},[45,89116,100],{"class":99},[45,89118,89119],{"class":103},"Conversion worked:",[45,89121,100],{"class":99},[45,89123,3079],{"class":85},[45,89125,35957],{"class":2070},[45,89127,109],{"class":85},[14,89129,177],{},[36,89131,89133],{"className":38,"code":89132,"language":40,"meta":41,"style":41},"Conversion worked: 42\n",[18,89134,89135],{"__ignoreMap":41},[45,89136,89137,89140,89142],{"class":47,"line":48},[45,89138,89139],{"class":51},"Conversion worked",[45,89141,457],{"class":85},[45,89143,89144],{"class":59}," 42\n",[14,89146,89147],{},"Why is this useful?",[195,89149,89150,89156,89159],{},[198,89151,89152,89153,89155],{},"It keeps the ",[18,89154,23272],{}," block small",[198,89157,89158],{},"It makes debugging easier",[198,89160,89161],{},"It clearly separates risky code from success code",[14,89163,26213,89164,89166,89167,89169],{},[18,89165,28],{}," to handle errors. That is what ",[18,89168,23275],{}," is for.",[31,89171,5754,89173,88675],{"id":89172},"what-finally-does",[18,89174,57769],{},[14,89176,89177,89179],{},[18,89178,57769],{}," runs whether an error happens or not.",[14,89181,89182],{},"Use it for cleanup work, such as:",[195,89184,89185,89188,89191,89194],{},[198,89186,89187],{},"closing files",[198,89189,89190],{},"releasing resources",[198,89192,89193],{},"printing a final message",[198,89195,89196],{},"resetting temporary state",[14,89198,466],{},[36,89200,89202],{"className":38,"code":89201,"language":40,"meta":41,"style":41},"try:\n    print(\"Trying something\")\nexcept ValueError:\n    print(\"A value error happened\")\nfinally:\n    print(\"This always runs\")\n",[18,89203,89204,89210,89225,89233,89248,89254],{"__ignoreMap":41},[45,89205,89206,89208],{"class":47,"line":48},[45,89207,23272],{"class":73},[45,89209,86],{"class":85},[45,89211,89212,89214,89216,89218,89221,89223],{"class":47,"line":63},[45,89213,93],{"class":92},[45,89215,96],{"class":85},[45,89217,100],{"class":99},[45,89219,89220],{"class":103},"Trying something",[45,89222,100],{"class":99},[45,89224,109],{"class":85},[45,89226,89227,89229,89231],{"class":47,"line":70},[45,89228,23275],{"class":73},[45,89230,45892],{"class":2109},[45,89232,86],{"class":85},[45,89234,89235,89237,89239,89241,89244,89246],{"class":47,"line":89},[45,89236,93],{"class":92},[45,89238,96],{"class":85},[45,89240,100],{"class":99},[45,89242,89243],{"class":103},"A value error happened",[45,89245,100],{"class":99},[45,89247,109],{"class":85},[45,89249,89250,89252],{"class":47,"line":112},[45,89251,57769],{"class":73},[45,89253,86],{"class":85},[45,89255,89256,89258,89260,89262,89265,89267],{"class":47,"line":126},[45,89257,93],{"class":92},[45,89259,96],{"class":85},[45,89261,100],{"class":99},[45,89263,89264],{"class":103},"This always runs",[45,89266,100],{"class":99},[45,89268,109],{"class":85},[14,89270,177],{},[36,89272,89274],{"className":38,"code":89273,"language":40,"meta":41,"style":41},"Trying something\nThis always runs\n",[18,89275,89276,89281],{"__ignoreMap":41},[45,89277,89278],{"class":47,"line":48},[45,89279,89280],{"class":51},"Trying something\n",[45,89282,89283],{"class":47,"line":63},[45,89284,89285],{"class":51},"This always runs\n",[14,89287,89288,89289,89291,89292,83659,89294,230,89296,89298],{},"In normal Python execution, ",[18,89290,57769],{}," still runs even if there is a ",[18,89293,3461],{},[18,89295,23272],{},[18,89297,23275],{}," in many cases.",[14,89300,466],{},[36,89302,89304],{"className":38,"code":89303,"language":40,"meta":41,"style":41},"def test():\n    try:\n        return \"from try\"\n    finally:\n        print(\"Cleaning up\")\n\nprint(test())\n",[18,89305,89306,89315,89321,89332,89339,89354,89358],{"__ignoreMap":41},[45,89307,89308,89310,89313],{"class":47,"line":48},[45,89309,2757],{"class":2440},[45,89311,89312],{"class":2455}," test",[45,89314,3828],{"class":85},[45,89316,89317,89319],{"class":47,"line":63},[45,89318,23538],{"class":73},[45,89320,86],{"class":85},[45,89322,89323,89325,89327,89330],{"class":47,"line":70},[45,89324,3550],{"class":73},[45,89326,1565],{"class":99},[45,89328,89329],{"class":103},"from try",[45,89331,1571],{"class":99},[45,89333,89334,89337],{"class":47,"line":89},[45,89335,89336],{"class":73},"    finally",[45,89338,86],{"class":85},[45,89340,89341,89343,89345,89347,89350,89352],{"class":47,"line":112},[45,89342,1923],{"class":92},[45,89344,96],{"class":85},[45,89346,100],{"class":99},[45,89348,89349],{"class":103},"Cleaning up",[45,89351,100],{"class":99},[45,89353,109],{"class":85},[45,89355,89356],{"class":47,"line":126},[45,89357,67],{"emptyLinePlaceholder":66},[45,89359,89360,89362,89364,89367],{"class":47,"line":142},[45,89361,2065],{"class":92},[45,89363,96],{"class":85},[45,89365,89366],{"class":2070},"test",[45,89368,10777],{"class":85},[14,89370,177],{},[36,89372,89374],{"className":38,"code":89373,"language":40,"meta":41,"style":41},"Cleaning up\nfrom try\n",[18,89375,89376,89381],{"__ignoreMap":41},[45,89377,89378],{"class":47,"line":48},[45,89379,89380],{"class":51},"Cleaning up\n",[45,89382,89383,89385],{"class":47,"line":63},[45,89384,14921],{"class":73},[45,89386,89387],{"class":73}," try\n",[31,89389,89391],{"id":89390},"execution-order-of-the-blocks","Execution order of the blocks",[14,89393,89394,89395,240],{},"Python always starts with ",[18,89396,23272],{},[712,89398,89400],{"id":89399},"if-no-exception-happens","If no exception happens",[14,89402,29012],{},[346,89404,89405,89409,89413],{},[198,89406,89407],{},[18,89408,23272],{},[198,89410,89411],{},[18,89412,28],{},[198,89414,89415],{},[18,89416,57769],{},[14,89418,466],{},[36,89420,89422],{"className":38,"code":89421,"language":40,"meta":41,"style":41},"try:\n    print(\"Try block\")\nexcept ValueError:\n    print(\"Except block\")\nelse:\n    print(\"Else block\")\nfinally:\n    print(\"Finally block\")\n",[18,89423,89424,89430,89445,89453,89468,89474,89489,89495],{"__ignoreMap":41},[45,89425,89426,89428],{"class":47,"line":48},[45,89427,23272],{"class":73},[45,89429,86],{"class":85},[45,89431,89432,89434,89436,89438,89441,89443],{"class":47,"line":63},[45,89433,93],{"class":92},[45,89435,96],{"class":85},[45,89437,100],{"class":99},[45,89439,89440],{"class":103},"Try block",[45,89442,100],{"class":99},[45,89444,109],{"class":85},[45,89446,89447,89449,89451],{"class":47,"line":70},[45,89448,23275],{"class":73},[45,89450,45892],{"class":2109},[45,89452,86],{"class":85},[45,89454,89455,89457,89459,89461,89464,89466],{"class":47,"line":89},[45,89456,93],{"class":92},[45,89458,96],{"class":85},[45,89460,100],{"class":99},[45,89462,89463],{"class":103},"Except block",[45,89465,100],{"class":99},[45,89467,109],{"class":85},[45,89469,89470,89472],{"class":47,"line":112},[45,89471,28],{"class":73},[45,89473,86],{"class":85},[45,89475,89476,89478,89480,89482,89485,89487],{"class":47,"line":126},[45,89477,93],{"class":92},[45,89479,96],{"class":85},[45,89481,100],{"class":99},[45,89483,89484],{"class":103},"Else block",[45,89486,100],{"class":99},[45,89488,109],{"class":85},[45,89490,89491,89493],{"class":47,"line":142},[45,89492,57769],{"class":73},[45,89494,86],{"class":85},[45,89496,89497,89499,89501,89503,89506,89508],{"class":47,"line":149},[45,89498,93],{"class":92},[45,89500,96],{"class":85},[45,89502,100],{"class":99},[45,89504,89505],{"class":103},"Finally block",[45,89507,100],{"class":99},[45,89509,109],{"class":85},[14,89511,177],{},[36,89513,89515],{"className":38,"code":89514,"language":40,"meta":41,"style":41},"Try block\nElse block\nFinally block\n",[18,89516,89517,89522,89527],{"__ignoreMap":41},[45,89518,89519],{"class":47,"line":48},[45,89520,89521],{"class":51},"Try block\n",[45,89523,89524],{"class":47,"line":63},[45,89525,89526],{"class":51},"Else block\n",[45,89528,89529],{"class":47,"line":70},[45,89530,89531],{"class":51},"Finally block\n",[712,89533,89535],{"id":89534},"if-a-matching-exception-happens","If a matching exception happens",[14,89537,29012],{},[346,89539,89540,89544,89548],{},[198,89541,89542],{},[18,89543,23272],{},[198,89545,89546],{},[18,89547,23275],{},[198,89549,89550],{},[18,89551,57769],{},[14,89553,466],{},[36,89555,89557],{"className":38,"code":89556,"language":40,"meta":41,"style":41},"try:\n    number = int(\"abc\")\nexcept ValueError:\n    print(\"Except block\")\nelse:\n    print(\"Else block\")\nfinally:\n    print(\"Finally block\")\n",[18,89558,89559,89565,89583,89591,89605,89611,89625,89631],{"__ignoreMap":41},[45,89560,89561,89563],{"class":47,"line":48},[45,89562,23272],{"class":73},[45,89564,86],{"class":85},[45,89566,89567,89569,89571,89573,89575,89577,89579,89581],{"class":47,"line":63},[45,89568,12957],{"class":51},[45,89570,56],{"class":55},[45,89572,41669],{"class":2109},[45,89574,96],{"class":85},[45,89576,100],{"class":99},[45,89578,10304],{"class":103},[45,89580,100],{"class":99},[45,89582,109],{"class":85},[45,89584,89585,89587,89589],{"class":47,"line":70},[45,89586,23275],{"class":73},[45,89588,45892],{"class":2109},[45,89590,86],{"class":85},[45,89592,89593,89595,89597,89599,89601,89603],{"class":47,"line":89},[45,89594,93],{"class":92},[45,89596,96],{"class":85},[45,89598,100],{"class":99},[45,89600,89463],{"class":103},[45,89602,100],{"class":99},[45,89604,109],{"class":85},[45,89606,89607,89609],{"class":47,"line":112},[45,89608,28],{"class":73},[45,89610,86],{"class":85},[45,89612,89613,89615,89617,89619,89621,89623],{"class":47,"line":126},[45,89614,93],{"class":92},[45,89616,96],{"class":85},[45,89618,100],{"class":99},[45,89620,89484],{"class":103},[45,89622,100],{"class":99},[45,89624,109],{"class":85},[45,89626,89627,89629],{"class":47,"line":142},[45,89628,57769],{"class":73},[45,89630,86],{"class":85},[45,89632,89633,89635,89637,89639,89641,89643],{"class":47,"line":149},[45,89634,93],{"class":92},[45,89636,96],{"class":85},[45,89638,100],{"class":99},[45,89640,89505],{"class":103},[45,89642,100],{"class":99},[45,89644,109],{"class":85},[14,89646,177],{},[36,89648,89650],{"className":38,"code":89649,"language":40,"meta":41,"style":41},"Except block\nFinally block\n",[18,89651,89652,89657],{"__ignoreMap":41},[45,89653,89654],{"class":47,"line":48},[45,89655,89656],{"class":51},"Except block\n",[45,89658,89659],{"class":47,"line":63},[45,89660,89531],{"class":51},[712,89662,89664],{"id":89663},"if-the-exception-is-not-caught","If the exception is not caught",[14,89666,89667,89668,89670,89671,89673],{},"If no matching ",[18,89669,23275],{}," block handles the error, ",[18,89672,57769],{}," still runs before the program stops with the error.",[36,89675,89677],{"className":38,"code":89676,"language":40,"meta":41,"style":41},"try:\n    print(10 \u002F 0)\nexcept ValueError:\n    print(\"Wrong type of exception\")\nfinally:\n    print(\"Finally still runs\")\n",[18,89678,89679,89685,89699,89707,89722,89728],{"__ignoreMap":41},[45,89680,89681,89683],{"class":47,"line":48},[45,89682,23272],{"class":73},[45,89684,86],{"class":85},[45,89686,89687,89689,89691,89693,89695,89697],{"class":47,"line":63},[45,89688,93],{"class":92},[45,89690,96],{"class":85},[45,89692,11786],{"class":59},[45,89694,45881],{"class":55},[45,89696,749],{"class":59},[45,89698,109],{"class":85},[45,89700,89701,89703,89705],{"class":47,"line":70},[45,89702,23275],{"class":73},[45,89704,45892],{"class":2109},[45,89706,86],{"class":85},[45,89708,89709,89711,89713,89715,89718,89720],{"class":47,"line":89},[45,89710,93],{"class":92},[45,89712,96],{"class":85},[45,89714,100],{"class":99},[45,89716,89717],{"class":103},"Wrong type of exception",[45,89719,100],{"class":99},[45,89721,109],{"class":85},[45,89723,89724,89726],{"class":47,"line":112},[45,89725,57769],{"class":73},[45,89727,86],{"class":85},[45,89729,89730,89732,89734,89736,89739,89741],{"class":47,"line":126},[45,89731,93],{"class":92},[45,89733,96],{"class":85},[45,89735,100],{"class":99},[45,89737,89738],{"class":103},"Finally still runs",[45,89740,100],{"class":99},[45,89742,109],{"class":85},[14,89744,177],{},[36,89746,89748],{"className":38,"code":89747,"language":40,"meta":41,"style":41},"Finally still runs\nTraceback (most recent call last):\n...\nZeroDivisionError: division by zero\n",[18,89749,89750,89755,89765,89769],{"__ignoreMap":41},[45,89751,89752],{"class":47,"line":48},[45,89753,89754],{"class":51},"Finally still runs\n",[45,89756,89757,89759,89761,89763],{"class":47,"line":63},[45,89758,46312],{"class":2070},[45,89760,96],{"class":85},[45,89762,46317],{"class":2070},[45,89764,2464],{"class":85},[45,89766,89767],{"class":47,"line":70},[45,89768,71635],{"class":2771},[45,89770,89771,89773,89775],{"class":47,"line":89},[45,89772,46837],{"class":2109},[45,89774,457],{"class":85},[45,89776,89777],{"class":51}," division by zero\n",[31,89779,89781],{"id":89780},"when-to-use-each-block","When to use each block",[14,89783,89784],{},"Use each block for one clear purpose:",[195,89786,89787,89792,89797,89802],{},[198,89788,89789,89791],{},[18,89790,23272],{},": risky code",[198,89793,89794,89796],{},[18,89795,23275],{},": what to do if it fails",[198,89798,89799,89801],{},[18,89800,28],{},": code to run only on success",[198,89803,89804,89806],{},[18,89805,57769],{},": cleanup that must always happen",[14,89808,5255],{},[195,89810,89811,89816,89821,89826],{},[198,89812,89813],{},[2421,89814,89815],{},"Try it",[198,89817,89818],{},[2421,89819,89820],{},"Handle failure",[198,89822,89823],{},[2421,89824,89825],{},"Continue on success",[198,89827,89828],{},[2421,89829,89830],{},"Clean up at the end",[31,89832,89834],{"id":89833},"beginner-example-with-user-input","Beginner example with user input",[14,89836,89837],{},"This example converts user input to an integer.",[195,89839,89840,89845,89850,89855],{},[198,89841,89842,89844],{},[18,89843,41891],{}," may fail",[198,89846,89847,89849],{},[18,89848,42097],{}," is caught if the input is not a valid whole number",[198,89851,89852,89854],{},[18,89853,28],{}," runs only when conversion succeeds",[198,89856,89857,89859],{},[18,89858,57769],{}," always prints the final message",[36,89861,89863],{"className":38,"code":89862,"language":40,"meta":41,"style":41},"try:\n    number = int(input(\"Enter a whole number: \"))\nexcept ValueError:\n    print(\"Please enter a valid whole number.\")\nelse:\n    print(\"You entered:\", number)\nfinally:\n    print(\"Program finished.\")\n",[18,89864,89865,89871,89893,89901,89915,89921,89939,89945],{"__ignoreMap":41},[45,89866,89867,89869],{"class":47,"line":48},[45,89868,23272],{"class":73},[45,89870,86],{"class":85},[45,89872,89873,89875,89877,89879,89881,89883,89885,89887,89889,89891],{"class":47,"line":63},[45,89874,12957],{"class":51},[45,89876,56],{"class":55},[45,89878,41669],{"class":2109},[45,89880,96],{"class":85},[45,89882,45859],{"class":92},[45,89884,96],{"class":85},[45,89886,100],{"class":99},[45,89888,86597],{"class":103},[45,89890,100],{"class":99},[45,89892,4348],{"class":85},[45,89894,89895,89897,89899],{"class":47,"line":70},[45,89896,23275],{"class":73},[45,89898,45892],{"class":2109},[45,89900,86],{"class":85},[45,89902,89903,89905,89907,89909,89911,89913],{"class":47,"line":89},[45,89904,93],{"class":92},[45,89906,96],{"class":85},[45,89908,100],{"class":99},[45,89910,45905],{"class":103},[45,89912,100],{"class":99},[45,89914,109],{"class":85},[45,89916,89917,89919],{"class":47,"line":112},[45,89918,28],{"class":73},[45,89920,86],{"class":85},[45,89922,89923,89925,89927,89929,89931,89933,89935,89937],{"class":47,"line":126},[45,89924,93],{"class":92},[45,89926,96],{"class":85},[45,89928,100],{"class":99},[45,89930,86636],{"class":103},[45,89932,100],{"class":99},[45,89934,3079],{"class":85},[45,89936,35957],{"class":2070},[45,89938,109],{"class":85},[45,89940,89941,89943],{"class":47,"line":142},[45,89942,57769],{"class":73},[45,89944,86],{"class":85},[45,89946,89947,89949,89951,89953,89955,89957],{"class":47,"line":149},[45,89948,93],{"class":92},[45,89950,96],{"class":85},[45,89952,100],{"class":99},[45,89954,88627],{"class":103},[45,89956,100],{"class":99},[45,89958,109],{"class":85},[14,89960,89961,89962,457],{},"Possible output if the user enters ",[18,89963,14942],{},[36,89965,89967],{"className":38,"code":89966,"language":40,"meta":41,"style":41},"Enter a whole number: 25\nYou entered: 25\nProgram finished.\n",[18,89968,89969,89978,89987],{"__ignoreMap":41},[45,89970,89971,89974,89976],{"class":47,"line":48},[45,89972,89973],{"class":51},"Enter a whole number",[45,89975,457],{"class":85},[45,89977,30102],{"class":59},[45,89979,89980,89983,89985],{"class":47,"line":63},[45,89981,89982],{"class":51},"You entered",[45,89984,457],{"class":85},[45,89986,30102],{"class":59},[45,89988,89989,89991],{"class":47,"line":70},[45,89990,56340],{"class":51},[45,89992,2708],{"class":85},[14,89994,89961,89995,457],{},[18,89996,18782],{},[36,89998,90000],{"className":38,"code":89999,"language":40,"meta":41,"style":41},"Enter a whole number: hello\nPlease enter a valid whole number.\nProgram finished.\n",[18,90001,90002,90011,90018],{"__ignoreMap":41},[45,90003,90004,90006,90008],{"class":47,"line":48},[45,90005,89973],{"class":51},[45,90007,457],{"class":85},[45,90009,90010],{"class":51}," hello\n",[45,90012,90013,90016],{"class":47,"line":63},[45,90014,90015],{"class":51},"Please enter a valid whole number",[45,90017,2708],{"class":85},[45,90019,90020,90022],{"class":47,"line":70},[45,90021,56340],{"class":51},[45,90023,2708],{"class":85},[14,90025,90026,90027,240],{},"If you keep seeing this kind of problem, see ",[236,90028,79264],{"href":90029},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F",[31,90031,90033],{"id":90032},"example-with-file-handling","Example with file handling",[14,90035,90036],{},"This example tries to open and read a file.",[195,90038,90039,90044,90050,90055],{},[198,90040,90041,90043],{},[18,90042,47592],{}," may fail if the file does not exist",[198,90045,90046,90049],{},[18,90047,90048],{},"except FileNotFoundError"," handles that case",[198,90051,90052,90054],{},[18,90053,28],{}," runs only when the file was opened successfully",[198,90056,90057,90059],{},[18,90058,57769],{}," closes the file if it was opened",[36,90061,90063],{"className":38,"code":90062,"language":40,"meta":41,"style":41},"file = None\n\ntry:\n    file = open(\"message.txt\", \"r\")\n    content = file.read()\nexcept FileNotFoundError:\n    print(\"The file was not found.\")\nelse:\n    print(\"File content:\")\n    print(content)\nfinally:\n    if file is not None:\n        file.close()\n        print(\"File closed.\")\n",[18,90064,90065,90073,90077,90083,90110,90124,90133,90148,90154,90169,90179,90185,90199,90210],{"__ignoreMap":41},[45,90066,90067,90069,90071],{"class":47,"line":48},[45,90068,46818],{"class":24018},[45,90070,3099],{"class":55},[45,90072,59643],{"class":481},[45,90074,90075],{"class":47,"line":63},[45,90076,67],{"emptyLinePlaceholder":66},[45,90078,90079,90081],{"class":47,"line":70},[45,90080,23272],{"class":73},[45,90082,86],{"class":85},[45,90084,90085,90087,90089,90091,90093,90095,90098,90100,90102,90104,90106,90108],{"class":47,"line":89},[45,90086,47646],{"class":24018},[45,90088,3099],{"class":55},[45,90090,23992],{"class":92},[45,90092,96],{"class":85},[45,90094,100],{"class":99},[45,90096,90097],{"class":103},"message.txt",[45,90099,100],{"class":99},[45,90101,3079],{"class":85},[45,90103,1565],{"class":99},[45,90105,24008],{"class":103},[45,90107,100],{"class":99},[45,90109,109],{"class":85},[45,90111,90112,90114,90116,90118,90120,90122],{"class":47,"line":112},[45,90113,47704],{"class":51},[45,90115,56],{"class":55},[45,90117,15011],{"class":24018},[45,90119,240],{"class":85},[45,90121,46823],{"class":2070},[45,90123,2495],{"class":85},[45,90125,90126,90128,90131],{"class":47,"line":126},[45,90127,23275],{"class":73},[45,90129,90130],{"class":2109}," FileNotFoundError",[45,90132,86],{"class":85},[45,90134,90135,90137,90139,90141,90144,90146],{"class":47,"line":142},[45,90136,93],{"class":92},[45,90138,96],{"class":85},[45,90140,100],{"class":99},[45,90142,90143],{"class":103},"The file was not found.",[45,90145,100],{"class":99},[45,90147,109],{"class":85},[45,90149,90150,90152],{"class":47,"line":149},[45,90151,28],{"class":73},[45,90153,86],{"class":85},[45,90155,90156,90158,90160,90162,90165,90167],{"class":47,"line":679},[45,90157,93],{"class":92},[45,90159,96],{"class":85},[45,90161,100],{"class":99},[45,90163,90164],{"class":103},"File content:",[45,90166,100],{"class":99},[45,90168,109],{"class":85},[45,90170,90171,90173,90175,90177],{"class":47,"line":686},[45,90172,93],{"class":92},[45,90174,96],{"class":85},[45,90176,47723],{"class":2070},[45,90178,109],{"class":85},[45,90180,90181,90183],{"class":47,"line":2229},[45,90182,57769],{"class":73},[45,90184,86],{"class":85},[45,90186,90187,90189,90191,90193,90195,90197],{"class":47,"line":2236},[45,90188,1913],{"class":73},[45,90190,15011],{"class":24018},[45,90192,23029],{"class":55},[45,90194,1296],{"class":55},[45,90196,29214],{"class":481},[45,90198,86],{"class":85},[45,90200,90201,90204,90206,90208],{"class":47,"line":5728},[45,90202,90203],{"class":24018},"        file",[45,90205,240],{"class":85},[45,90207,48407],{"class":2070},[45,90209,2495],{"class":85},[45,90211,90212,90214,90216,90218,90221,90223],{"class":47,"line":19380},[45,90213,1923],{"class":92},[45,90215,96],{"class":85},[45,90217,100],{"class":99},[45,90219,90220],{"class":103},"File closed.",[45,90222,100],{"class":99},[45,90224,109],{"class":85},[14,90226,90227,90228,90231],{},"Why do we set ",[18,90229,90230],{},"file = None"," first?",[14,90233,90234,90235,90237,90238,90240,90241,90243,90244,90246,90247,240],{},"Because if ",[18,90236,47592],{}," fails, the ",[18,90239,46818],{}," variable would not be created inside ",[18,90242,23272],{},". Setting it before the ",[18,90245,23272],{}," block makes it safe to check in ",[18,90248,57769],{},[14,90250,90251,90252,240],{},"If you are working with file errors, see ",[236,90253,49153],{"href":49152},[31,90255,90257],{"id":90256},"best-practices","Best practices",[195,90259,90260,90263,90269,90272,90277],{},[198,90261,90262],{},"Catch the most specific exception you can",[198,90264,90265,90266,90268],{},"Keep the ",[18,90267,23272],{}," block as small as possible",[198,90270,90271],{},"Do not hide errors silently",[198,90273,165,90274,90276],{},[18,90275,57769],{}," for cleanup, not normal program logic",[198,90278,165,90279,90281],{},[18,90280,28],{}," to separate success code from risky code",[14,90283,57858],{},[36,90285,90287],{"className":38,"code":90286,"language":40,"meta":41,"style":41},"try:\n    number = int(user_input)\nexcept ValueError:\n    print(\"Invalid input\")\nelse:\n    print(\"Valid number:\", number)\n",[18,90288,90289,90295,90309,90317,90332,90338],{"__ignoreMap":41},[45,90290,90291,90293],{"class":47,"line":48},[45,90292,23272],{"class":73},[45,90294,86],{"class":85},[45,90296,90297,90299,90301,90303,90305,90307],{"class":47,"line":63},[45,90298,12957],{"class":51},[45,90300,56],{"class":55},[45,90302,41669],{"class":2109},[45,90304,96],{"class":85},[45,90306,47046],{"class":2070},[45,90308,109],{"class":85},[45,90310,90311,90313,90315],{"class":47,"line":70},[45,90312,23275],{"class":73},[45,90314,45892],{"class":2109},[45,90316,86],{"class":85},[45,90318,90319,90321,90323,90325,90328,90330],{"class":47,"line":89},[45,90320,93],{"class":92},[45,90322,96],{"class":85},[45,90324,100],{"class":99},[45,90326,90327],{"class":103},"Invalid input",[45,90329,100],{"class":99},[45,90331,109],{"class":85},[45,90333,90334,90336],{"class":47,"line":112},[45,90335,28],{"class":73},[45,90337,86],{"class":85},[45,90339,90340,90342,90344,90346,90349,90351,90353,90355],{"class":47,"line":126},[45,90341,93],{"class":92},[45,90343,96],{"class":85},[45,90345,100],{"class":99},[45,90347,90348],{"class":103},"Valid number:",[45,90350,100],{"class":99},[45,90352,3079],{"class":85},[45,90354,35957],{"class":2070},[45,90356,109],{"class":85},[14,90358,90359,90360,90362],{},"This is better than putting everything inside ",[18,90361,23272],{},", because it is clearer which line might fail.",[14,90364,90365,90366,240],{},"You can learn more patterns in ",[236,90367,47012],{"href":90368},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F",[31,90370,1337],{"id":1336},[712,90372,90374,90375],{"id":90373},"putting-too-much-code-inside-try","Putting too much code inside ",[18,90376,23272],{},[14,90378,90379],{},"Bad:",[36,90381,90383],{"className":38,"code":90382,"language":40,"meta":41,"style":41},"try:\n    number = int(user_input)\n    print(\"Number entered\")\n    total = number * 10\n    print(\"Total:\", total)\nexcept ValueError:\n    print(\"Invalid input\")\n",[18,90384,90385,90391,90405,90420,90433,90453,90461],{"__ignoreMap":41},[45,90386,90387,90389],{"class":47,"line":48},[45,90388,23272],{"class":73},[45,90390,86],{"class":85},[45,90392,90393,90395,90397,90399,90401,90403],{"class":47,"line":63},[45,90394,12957],{"class":51},[45,90396,56],{"class":55},[45,90398,41669],{"class":2109},[45,90400,96],{"class":85},[45,90402,47046],{"class":2070},[45,90404,109],{"class":85},[45,90406,90407,90409,90411,90413,90416,90418],{"class":47,"line":70},[45,90408,93],{"class":92},[45,90410,96],{"class":85},[45,90412,100],{"class":99},[45,90414,90415],{"class":103},"Number entered",[45,90417,100],{"class":99},[45,90419,109],{"class":85},[45,90421,90422,90425,90427,90429,90431],{"class":47,"line":89},[45,90423,90424],{"class":51},"    total ",[45,90426,56],{"class":55},[45,90428,744],{"class":51},[45,90430,5601],{"class":55},[45,90432,1174],{"class":59},[45,90434,90435,90437,90439,90441,90444,90446,90448,90451],{"class":47,"line":112},[45,90436,93],{"class":92},[45,90438,96],{"class":85},[45,90440,100],{"class":99},[45,90442,90443],{"class":103},"Total:",[45,90445,100],{"class":99},[45,90447,3079],{"class":85},[45,90449,90450],{"class":2070}," total",[45,90452,109],{"class":85},[45,90454,90455,90457,90459],{"class":47,"line":126},[45,90456,23275],{"class":73},[45,90458,45892],{"class":2109},[45,90460,86],{"class":85},[45,90462,90463,90465,90467,90469,90471,90473],{"class":47,"line":142},[45,90464,93],{"class":92},[45,90466,96],{"class":85},[45,90468,100],{"class":99},[45,90470,90327],{"class":103},[45,90472,100],{"class":99},[45,90474,109],{"class":85},[14,90476,90477],{},"Better:",[36,90479,90481],{"className":38,"code":90480,"language":40,"meta":41,"style":41},"try:\n    number = int(user_input)\nexcept ValueError:\n    print(\"Invalid input\")\nelse:\n    print(\"Number entered\")\n    total = number * 10\n    print(\"Total:\", total)\n",[18,90482,90483,90489,90503,90511,90525,90531,90545,90557],{"__ignoreMap":41},[45,90484,90485,90487],{"class":47,"line":48},[45,90486,23272],{"class":73},[45,90488,86],{"class":85},[45,90490,90491,90493,90495,90497,90499,90501],{"class":47,"line":63},[45,90492,12957],{"class":51},[45,90494,56],{"class":55},[45,90496,41669],{"class":2109},[45,90498,96],{"class":85},[45,90500,47046],{"class":2070},[45,90502,109],{"class":85},[45,90504,90505,90507,90509],{"class":47,"line":70},[45,90506,23275],{"class":73},[45,90508,45892],{"class":2109},[45,90510,86],{"class":85},[45,90512,90513,90515,90517,90519,90521,90523],{"class":47,"line":89},[45,90514,93],{"class":92},[45,90516,96],{"class":85},[45,90518,100],{"class":99},[45,90520,90327],{"class":103},[45,90522,100],{"class":99},[45,90524,109],{"class":85},[45,90526,90527,90529],{"class":47,"line":112},[45,90528,28],{"class":73},[45,90530,86],{"class":85},[45,90532,90533,90535,90537,90539,90541,90543],{"class":47,"line":126},[45,90534,93],{"class":92},[45,90536,96],{"class":85},[45,90538,100],{"class":99},[45,90540,90415],{"class":103},[45,90542,100],{"class":99},[45,90544,109],{"class":85},[45,90546,90547,90549,90551,90553,90555],{"class":47,"line":142},[45,90548,90424],{"class":51},[45,90550,56],{"class":55},[45,90552,744],{"class":51},[45,90554,5601],{"class":55},[45,90556,1174],{"class":59},[45,90558,90559,90561,90563,90565,90567,90569,90571,90573],{"class":47,"line":149},[45,90560,93],{"class":92},[45,90562,96],{"class":85},[45,90564,100],{"class":99},[45,90566,90443],{"class":103},[45,90568,100],{"class":99},[45,90570,3079],{"class":85},[45,90572,90450],{"class":2070},[45,90574,109],{"class":85},[14,90576,90577],{},"This makes it clearer that only the conversion is risky.",[712,90579,1350,90581,90583],{"id":90580},"using-except-without-naming-an-exception-type",[18,90582,23275],{}," without naming an exception type",[14,90585,90586],{},"This can catch unexpected errors and make debugging harder.",[14,90588,90589],{},"Prefer:",[36,90591,90593],{"className":38,"code":90592,"language":40,"meta":41,"style":41},"except ValueError:\n",[18,90594,90595],{"__ignoreMap":41},[45,90596,90597,90599,90601],{"class":47,"line":48},[45,90598,23275],{"class":73},[45,90600,45892],{"class":2109},[45,90602,86],{"class":85},[14,90604,90605],{},"instead of:",[36,90607,90609],{"className":38,"code":90608,"language":40,"meta":41,"style":41},"except:\n",[18,90610,90611],{"__ignoreMap":41},[45,90612,90613,90615],{"class":47,"line":48},[45,90614,23275],{"class":73},[45,90616,86],{"class":85},[712,90618,52955,90620,14527,90622],{"id":90619},"confusing-else-with-except",[18,90621,28],{},[18,90623,23275],{},[14,90625,56726],{},[195,90627,90628,90633],{},[198,90629,90630,90632],{},[18,90631,23275],{}," runs when an error happens",[198,90634,90635,90637],{},[18,90636,28],{}," runs when no error happens",[712,90639,1350,90641,90643],{"id":90640},"using-finally-when-cleanup-is-not-needed",[18,90642,57769],{}," when cleanup is not needed",[14,90645,90646,90647,90649],{},"You do not have to use ",[18,90648,57769],{}," every time. Use it when something must always happen at the end.",[712,90651,90653],{"id":90652},"catching-errors-but-not-fixing-the-real-cause","Catching errors but not fixing the real cause",[14,90655,90656],{},"This is not very helpful:",[36,90658,90660],{"className":38,"code":90659,"language":40,"meta":41,"style":41},"try:\n    number = int(text)\nexcept ValueError:\n    pass\n",[18,90661,90662,90668,90682,90690],{"__ignoreMap":41},[45,90663,90664,90666],{"class":47,"line":48},[45,90665,23272],{"class":73},[45,90667,86],{"class":85},[45,90669,90670,90672,90674,90676,90678,90680],{"class":47,"line":63},[45,90671,12957],{"class":51},[45,90673,56],{"class":55},[45,90675,41669],{"class":2109},[45,90677,96],{"class":85},[45,90679,5000],{"class":2070},[45,90681,109],{"class":85},[45,90683,90684,90686,90688],{"class":47,"line":70},[45,90685,23275],{"class":73},[45,90687,45892],{"class":2109},[45,90689,86],{"class":85},[45,90691,90692],{"class":47,"line":89},[45,90693,5306],{"class":73},[14,90695,90696],{},"It hides the problem.",[14,90698,90699],{},"At minimum, print a useful message or handle the input another way.",[31,90701,2012],{"id":2011},[14,90703,90704],{},"These are common reasons beginners struggle with exception blocks:",[195,90706,90707,90712,90721,90727,90735],{},[198,90708,1350,90709,90711],{},[18,90710,23272],{}," around a large block of unrelated code",[198,90713,90714,90715,90717,90718,90720],{},"Catching ",[18,90716,47321],{}," or using bare ",[18,90719,23275],{}," too early in learning",[198,90722,90723,90724,90726],{},"Putting code that can also fail inside ",[18,90725,23275],{}," without another plan",[198,90728,90729,90730,90732,90733],{},"Trying to use a variable in ",[18,90731,57769],{}," that was never created in ",[18,90734,23272],{},[198,90736,2043,90737,90739],{},[18,90738,28],{}," runs only when no exception occurs",[31,90741,47283],{"id":47282},[14,90743,90744],{},"These commands can help when you are learning exception handling:",[36,90746,90747],{"className":15652,"code":40265,"language":15654,"meta":41,"style":41},[18,90748,90749],{"__ignoreMap":41},[45,90750,90751,90753],{"class":47,"line":48},[45,90752,40],{"class":2444},[45,90754,16305],{"class":103},[36,90756,90758],{"className":38,"code":90757,"language":40,"meta":41,"style":41},"print(type(value))\nprint(repr(value))\nhelp(ValueError)\nhelp(FileNotFoundError)\n",[18,90759,90760,90774,90788,90798],{"__ignoreMap":41},[45,90761,90762,90764,90766,90768,90770,90772],{"class":47,"line":48},[45,90763,2065],{"class":92},[45,90765,96],{"class":85},[45,90767,4340],{"class":2109},[45,90769,96],{"class":85},[45,90771,12722],{"class":2070},[45,90773,4348],{"class":85},[45,90775,90776,90778,90780,90782,90784,90786],{"class":47,"line":63},[45,90777,2065],{"class":92},[45,90779,96],{"class":85},[45,90781,42294],{"class":92},[45,90783,96],{"class":85},[45,90785,12722],{"class":2070},[45,90787,4348],{"class":85},[45,90789,90790,90792,90794,90796],{"class":47,"line":70},[45,90791,2104],{"class":92},[45,90793,96],{"class":85},[45,90795,42097],{"class":2109},[45,90797,109],{"class":85},[45,90799,90800,90802,90804,90806],{"class":47,"line":89},[45,90801,2104],{"class":92},[45,90803,96],{"class":85},[45,90805,46770],{"class":2109},[45,90807,109],{"class":85},[14,90809,90810],{},"These are useful because:",[195,90812,90813,90819,90824,90830],{},[198,90814,90815,90818],{},[18,90816,90817],{},"python your_script.py"," runs your program from the terminal",[198,90820,90821,90823],{},[18,90822,40437],{}," shows what kind of value you have",[198,90825,90826,90829],{},[18,90827,90828],{},"repr()"," shows a more exact version of a value",[198,90831,90832,90835],{},[18,90833,90834],{},"help()"," shows built-in documentation for an exception type",[31,90837,2281],{"id":2280},[712,90839,90841],{"id":90840},"do-i-need-to-use-all-four-blocks-every-time","Do I need to use all four blocks every time?",[14,90843,90844,90845,1316,90847,240],{},"No. You can use only the blocks you need. Many programs use just ",[18,90846,23272],{},[18,90848,23275],{},[712,90850,16362,90852,1316,90854,354],{"id":90851},"what-is-the-difference-between-except-and-else",[18,90853,23275],{},[18,90855,28],{},[14,90857,90858,90860,90861,90863],{},[18,90859,23275],{}," runs when an error happens. ",[18,90862,28],{}," runs when no error happens.",[712,90865,14769,90867,90869],{"id":90866},"does-finally-always-run",[18,90868,57769],{}," always run?",[14,90871,90872],{},"In normal Python execution, yes. It is meant for cleanup code that should always run.",[712,90874,90876,90877,354],{"id":90875},"should-i-use-bare-except","Should I use bare ",[18,90878,23275],{},[14,90880,90881],{},"Usually no. It can hide useful error details and make debugging harder.",[712,90883,90885,90886,90888],{"id":90884},"can-i-have-more-than-one-except-block","Can I have more than one ",[18,90887,23275],{}," block?",[14,90890,90891,90892,90894],{},"Yes. You can catch different exception types with separate ",[18,90893,23275],{}," blocks.",[14,90896,466],{},[36,90898,90900],{"className":38,"code":90899,"language":40,"meta":41,"style":41},"try:\n    value = int(input(\"Enter a number: \"))\n    result = 10 \u002F value\nexcept ValueError:\n    print(\"That was not a valid integer.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\nelse:\n    print(\"Result:\", result)\nfinally:\n    print(\"Done.\")\n",[18,90901,90902,90908,90930,90942,90950,90965,90973,90987,90993,91011,91017],{"__ignoreMap":41},[45,90903,90904,90906],{"class":47,"line":48},[45,90905,23272],{"class":73},[45,90907,86],{"class":85},[45,90909,90910,90912,90914,90916,90918,90920,90922,90924,90926,90928],{"class":47,"line":63},[45,90911,20275],{"class":51},[45,90913,56],{"class":55},[45,90915,41669],{"class":2109},[45,90917,96],{"class":85},[45,90919,45859],{"class":92},[45,90921,96],{"class":85},[45,90923,100],{"class":99},[45,90925,45866],{"class":103},[45,90927,100],{"class":99},[45,90929,4348],{"class":85},[45,90931,90932,90934,90936,90938,90940],{"class":47,"line":70},[45,90933,88721],{"class":51},[45,90935,56],{"class":55},[45,90937,1190],{"class":59},[45,90939,45881],{"class":55},[45,90941,82717],{"class":51},[45,90943,90944,90946,90948],{"class":47,"line":89},[45,90945,23275],{"class":73},[45,90947,45892],{"class":2109},[45,90949,86],{"class":85},[45,90951,90952,90954,90956,90958,90961,90963],{"class":47,"line":112},[45,90953,93],{"class":92},[45,90955,96],{"class":85},[45,90957,100],{"class":99},[45,90959,90960],{"class":103},"That was not a valid integer.",[45,90962,100],{"class":99},[45,90964,109],{"class":85},[45,90966,90967,90969,90971],{"class":47,"line":126},[45,90968,23275],{"class":73},[45,90970,45916],{"class":2109},[45,90972,86],{"class":85},[45,90974,90975,90977,90979,90981,90983,90985],{"class":47,"line":142},[45,90976,93],{"class":92},[45,90978,96],{"class":85},[45,90980,100],{"class":99},[45,90982,45929],{"class":103},[45,90984,100],{"class":99},[45,90986,109],{"class":85},[45,90988,90989,90991],{"class":47,"line":149},[45,90990,28],{"class":73},[45,90992,86],{"class":85},[45,90994,90995,90997,90999,91001,91003,91005,91007,91009],{"class":47,"line":679},[45,90996,93],{"class":92},[45,90998,96],{"class":85},[45,91000,100],{"class":99},[45,91002,81863],{"class":103},[45,91004,100],{"class":99},[45,91006,3079],{"class":85},[45,91008,10811],{"class":2070},[45,91010,109],{"class":85},[45,91012,91013,91015],{"class":47,"line":686},[45,91014,57769],{"class":73},[45,91016,86],{"class":85},[45,91018,91019,91021,91023,91025,91027,91029],{"class":47,"line":2229},[45,91020,93],{"class":92},[45,91022,96],{"class":85},[45,91024,100],{"class":99},[45,91026,5396],{"class":103},[45,91028,100],{"class":99},[45,91030,109],{"class":85},[31,91032,2342],{"id":2341},[195,91034,91035,91039,91044,91048,91054,91058],{},[198,91036,91037],{},[236,91038,35173],{"href":35172},[198,91040,91041],{},[236,91042,91043],{"href":89015},"How to use try except blocks in Python",[198,91045,91046],{},[236,91047,47489],{"href":90368},[198,91049,91050],{},[236,91051,91053],{"href":91052},"\u002Flearn\u002Fraising-exceptions-in-python\u002F","Raising exceptions in Python",[198,91055,91056],{},[236,91057,79264],{"href":90029},[198,91059,91060],{},[236,91061,49153],{"href":49152},[2367,91063,91064],{},"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 .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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":41,"searchDepth":63,"depth":63,"links":91066},[91067,91068,91069,91071,91073,91075,91077,91082,91083,91084,91085,91086,91097,91098,91099,91110],{"id":33,"depth":63,"text":34},{"id":2529,"depth":63,"text":2530},{"id":88672,"depth":63,"text":91070},"What try does",{"id":88801,"depth":63,"text":91072},"What except does",{"id":89019,"depth":63,"text":91074},"What else does",{"id":89172,"depth":63,"text":91076},"What finally does",{"id":89390,"depth":63,"text":89391,"children":91078},[91079,91080,91081],{"id":89399,"depth":70,"text":89400},{"id":89534,"depth":70,"text":89535},{"id":89663,"depth":70,"text":89664},{"id":89780,"depth":63,"text":89781},{"id":89833,"depth":63,"text":89834},{"id":90032,"depth":63,"text":90033},{"id":90256,"depth":63,"text":90257},{"id":1336,"depth":63,"text":1337,"children":91087},[91088,91090,91092,91094,91096],{"id":90373,"depth":70,"text":91089},"Putting too much code inside try",{"id":90580,"depth":70,"text":91091},"Using except without naming an exception type",{"id":90619,"depth":70,"text":91093},"Confusing else with except",{"id":90640,"depth":70,"text":91095},"Using finally when cleanup is not needed",{"id":90652,"depth":70,"text":90653},{"id":2011,"depth":63,"text":2012},{"id":47282,"depth":63,"text":47283},{"id":2280,"depth":63,"text":2281,"children":91100},[91101,91102,91104,91106,91108],{"id":90840,"depth":70,"text":90841},{"id":90851,"depth":70,"text":91103},"What is the difference between except and else?",{"id":90866,"depth":70,"text":91105},"Does finally always run?",{"id":90875,"depth":70,"text":91107},"Should I use bare except?",{"id":90884,"depth":70,"text":91109},"Can I have more than one except block?",{"id":2341,"depth":63,"text":2342},"Master using try except else and finally in python in our comprehensive Python beginner guide.",{},{"title":80250,"description":91111},"learn\u002Fusing-try-except-else-and-finally-in-python","5abiL9O4faxnyA7B9L3C4BlQ5RP8KTConUQYGEWX10A",{"id":91117,"title":91118,"body":91119,"description":91852,"extension":2399,"meta":91853,"navigation":66,"path":91854,"seo":91855,"stem":91856,"__hash__":91857},"content\u002Flearn\u002Fwhat-is-python-a-beginner-friendly-introduction.md","What Is Python? A Beginner-Friendly Introduction",{"type":7,"value":91120,"toc":91814},[91121,91124,91127,91130,91134,91140,91143,91146,91149,91166,91169,91173,91176,91178,91198,91201,91218,91225,91229,91232,91236,91239,91243,91246,91250,91257,91261,91264,91268,91271,91276,91280,91283,91301,91303,91308,91311,91327,91330,91371,91373,91379,91381,91398,91408,91411,91414,91416,91448,91453,91456,91460,91463,91473,91480,91483,91497,91500,91514,91519,91523,91526,91530,91535,91539,91542,91552,91555,91561,91566,91571,91591,91595,91598,91600,91606,91609,91613,91616,91620,91623,91627,91630,91634,91637,91662,91668,91672,91675,91679,91682,91685,91710,91714,91717,91734,91737,91739,91743,91746,91750,91753,91757,91760,91764,91767,91771,91774,91776,91811],[10,91122,91118],{"id":91123},"what-is-python-a-beginner-friendly-introduction",[14,91125,91126],{},"Python is a beginner-friendly programming language used for many real tasks.",[14,91128,91129],{},"This page explains what Python is, why people use it, what makes it beginner-friendly, and what to learn next. If you are new to programming, this is a good place to start before moving on to installation, running code, and basic syntax.",[31,91131,91133],{"id":91132},"what-python-is","What Python is",[14,91135,91136,91137,240],{},"Python is a ",[2421,91138,91139],{},"programming language",[14,91141,91142],{},"A programming language is a way to give instructions to a computer. You write those instructions as code, and the computer runs them.",[14,91144,91145],{},"Python is popular because its syntax is simple and readable. In other words, Python code often looks cleaner and easier to follow than code in many other languages.",[14,91147,91148],{},"Python is used by:",[195,91150,91151,91154,91157,91160,91163],{},[198,91152,91153],{},"Complete beginners",[198,91155,91156],{},"Professional developers",[198,91158,91159],{},"Companies",[198,91161,91162],{},"Teachers and students",[198,91164,91165],{},"Researchers and analysts",[14,91167,91168],{},"That does not mean Python is only for simple practice. It is used for real software, automation, websites, data work, and more.",[31,91170,91172],{"id":91171},"what-python-is-used-for","What Python is used for",[14,91174,91175],{},"Python is a general-purpose language. That means you can use it for many different kinds of programming.",[14,91177,4778],{},[195,91179,91180,91183,91186,91189,91192,91195],{},[198,91181,91182],{},"Writing small scripts to automate repeated tasks",[198,91184,91185],{},"Building websites and web applications",[198,91187,91188],{},"Working with data, CSV files, and JSON",[198,91190,91191],{},"Making API requests and handling responses",[198,91193,91194],{},"Creating tools and command-line programs",[198,91196,91197],{},"Building beginner projects while learning programming fundamentals",[14,91199,91200],{},"For example, a Python program might:",[195,91202,91203,91206,91209,91212,91215],{},[198,91204,91205],{},"Rename many files automatically",[198,91207,91208],{},"Read a CSV file and summarize the data",[198,91210,91211],{},"Send a request to a web API and print the result",[198,91213,91214],{},"Power part of a website",[198,91216,91217],{},"Check input from a user and respond to it",[14,91219,91220,91221,240],{},"If you want a broader overview, see ",[236,91222,91224],{"href":91223},"\u002Flearn\u002Fwhy-learn-python-use-cases-and-benefits\u002F","why learn Python: use cases and benefits",[31,91226,91228],{"id":91227},"why-python-is-beginner-friendly","Why Python is beginner-friendly",[14,91230,91231],{},"Python is often recommended to beginners for practical reasons.",[712,91233,91235],{"id":91234},"simple-syntax","Simple syntax",[14,91237,91238],{},"Python code is easier to read than many other languages. You can often understand the basic idea of a program even when you are new.",[712,91240,91242],{"id":91241},"useful-results-with-a-small-amount-of-code","Useful results with a small amount of code",[14,91244,91245],{},"You do not need to write a large program before you can do something useful. Even a few lines can print text, work with numbers, or read a file.",[712,91247,91249],{"id":91248},"large-standard-library","Large standard library",[14,91251,91252,91253,91256],{},"Python comes with many built-in tools. This collection is called the ",[2421,91254,91255],{},"standard library",". It helps you do common tasks without installing extra packages right away.",[712,91258,91260],{"id":91259},"lots-of-learning-resources","Lots of learning resources",[14,91262,91263],{},"Python has a large community. That means there are many tutorials, examples, guides, and answers available for beginners.",[712,91265,91267],{"id":91266},"better-focus-on-problem-solving","Better focus on problem solving",[14,91269,91270],{},"Because the syntax is relatively clean, beginners can spend more time learning programming ideas and less time fighting complicated syntax.",[14,91272,91273,91274,240],{},"After this page, a good next step is learning ",[236,91275,39616],{"href":39615},[31,91277,91279],{"id":91278},"what-python-code-looks-like","What Python code looks like",[14,91281,91282],{},"Here is a very small Python program:",[36,91284,91285],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,91286,91287],{"__ignoreMap":41},[45,91288,91289,91291,91293,91295,91297,91299],{"class":47,"line":48},[45,91290,2065],{"class":92},[45,91292,96],{"class":85},[45,91294,100],{"class":99},[45,91296,17031],{"class":103},[45,91298,100],{"class":99},[45,91300,109],{"class":85},[14,91302,177],{},[36,91304,91306],{"className":91305,"code":17042,"language":5000,"meta":41},[4998],[18,91307,17042],{"__ignoreMap":41},[14,91309,91310],{},"This example shows a few important ideas:",[195,91312,91313,91316,91321,91324],{},[198,91314,91315],{},"Python code is read one line at a time",[198,91317,91318,91320],{},[18,91319,2065],{}," is a built-in Python tool",[198,91322,91323],{},"The text inside quotes is a string",[198,91325,91326],{},"Parentheses hold the value you want to print",[14,91328,91329],{},"You can also write more than one line:",[36,91331,91333],{"className":38,"code":91332,"language":40,"meta":41,"style":41},"name = \"Maya\"\nprint(\"Hello\")\nprint(name)\n",[18,91334,91335,91347,91361],{"__ignoreMap":41},[45,91336,91337,91339,91341,91343,91345],{"class":47,"line":48},[45,91338,1560],{"class":51},[45,91340,56],{"class":55},[45,91342,1565],{"class":99},[45,91344,5737],{"class":103},[45,91346,1571],{"class":99},[45,91348,91349,91351,91353,91355,91357,91359],{"class":47,"line":63},[45,91350,2065],{"class":92},[45,91352,96],{"class":85},[45,91354,100],{"class":99},[45,91356,3226],{"class":103},[45,91358,100],{"class":99},[45,91360,109],{"class":85},[45,91362,91363,91365,91367,91369],{"class":47,"line":70},[45,91364,2065],{"class":92},[45,91366,96],{"class":85},[45,91368,3096],{"class":2070},[45,91370,109],{"class":85},[14,91372,177],{},[36,91374,91377],{"className":91375,"code":91376,"language":5000,"meta":41},[4998],"Hello\nMaya\n",[18,91378,91376],{"__ignoreMap":41},[14,91380,6570],{},[195,91382,91383,91388,91393],{},[198,91384,91385,91387],{},[18,91386,3096],{}," is a variable",[198,91389,91390,91392],{},[18,91391,11062],{}," is a string value",[198,91394,91395,91397],{},[18,91396,70662],{}," prints the value stored in the variable",[14,91399,3259,91400,91402,91403,91405,91406,240],{},[18,91401,47300],{}," is a new word for you, see ",[236,91404,74857],{"href":74856}," or continue with ",[236,91407,40783],{"href":40782},[712,91409,463],{"id":91410},"indentation-matters-in-python",[14,91412,91413],{},"Python uses indentation to show blocks of code. Indentation usually means spaces at the start of a line.",[14,91415,2115],{},[36,91417,91419],{"className":38,"code":91418,"language":40,"meta":41,"style":41},"if 5 > 2:\n    print(\"5 is greater than 2\")\n",[18,91420,91421,91433],{"__ignoreMap":41},[45,91422,91423,91425,91427,91429,91431],{"class":47,"line":48},[45,91424,20],{"class":73},[45,91426,1222],{"class":59},[45,91428,33591],{"class":55},[45,91430,3754],{"class":59},[45,91432,86],{"class":85},[45,91434,91435,91437,91439,91441,91444,91446],{"class":47,"line":63},[45,91436,93],{"class":92},[45,91438,96],{"class":85},[45,91440,100],{"class":99},[45,91442,91443],{"class":103},"5 is greater than 2",[45,91445,100],{"class":99},[45,91447,109],{"class":85},[14,91449,91450,91451,56131],{},"The indented line belongs to the ",[18,91452,20],{},[14,91454,91455],{},"If the indentation is missing or incorrect, Python may raise an error. This is one reason spacing matters more in Python than in some other languages.",[31,91457,91459],{"id":91458},"how-python-runs-your-code","How Python runs your code",[14,91461,91462],{},"You can write Python code in two common ways:",[195,91464,91465,91470],{},[198,91466,91467,91468,15011],{},"In a ",[18,91469,15010],{},[198,91471,91472],{},"In an interactive shell, where you type code and see the result immediately",[14,91474,91475,91476,91479],{},"When you run Python code, the ",[2421,91477,91478],{},"Python interpreter"," reads your code and executes it.",[14,91481,91482],{},"You do not need deep technical details yet. For now, the important idea is simple:",[346,91484,91485,91488,91491,91494],{},[198,91486,91487],{},"You write Python code",[198,91489,91490],{},"The interpreter reads it",[198,91492,91493],{},"Python runs the instructions",[198,91495,91496],{},"If something is wrong, Python shows an error",[14,91498,91499],{},"Errors happen when Python cannot understand your code or cannot complete an action. For example, this can happen if:",[195,91501,91502,91505,91508,91511],{},[198,91503,91504],{},"You misspell something",[198,91506,91507],{},"You forget quotes or parentheses",[198,91509,91510],{},"You use bad indentation",[198,91512,91513],{},"You try to use a variable that does not exist yet",[14,91515,91516,91517,240],{},"A practical next step is learning ",[236,91518,17174],{"href":17173},[31,91520,91522],{"id":91521},"what-you-need-to-start-using-python","What you need to start using Python",[14,91524,91525],{},"You do not need much to begin.",[712,91527,91529],{"id":91528},"_1-install-python","1. Install Python",[14,91531,91532,91533,240],{},"First, install Python on your computer. If you have not done that yet, follow ",[236,91534,17662],{"href":17495},[712,91536,91538],{"id":91537},"_2-choose-a-way-to-write-code","2. Choose a way to write code",[14,91540,91541],{},"You can use:",[195,91543,91544,91547,91549],{},[198,91545,91546],{},"The command line",[198,91548,18283],{},[198,91550,91551],{},"An IDE such as VS Code or PyCharm",[14,91553,91554],{},"As a beginner, any of these can work. The best choice is usually the one that helps you write and run code easily.",[712,91556,91558,91559,15011],{"id":91557},"_3-create-a-py-file","3. Create a ",[18,91560,15010],{},[14,91562,91563,91564,240],{},"A Python file usually ends with ",[18,91565,15010],{},[14,91567,91568,91569],{},"Example file: ",[18,91570,17927],{},[36,91572,91574],{"className":38,"code":91573,"language":40,"meta":41,"style":41},"print(\"Hello from Python\")\n",[18,91575,91576],{"__ignoreMap":41},[45,91577,91578,91580,91582,91584,91587,91589],{"class":47,"line":48},[45,91579,2065],{"class":92},[45,91581,96],{"class":85},[45,91583,100],{"class":99},[45,91585,91586],{"class":103},"Hello from Python",[45,91588,100],{"class":99},[45,91590,109],{"class":85},[712,91592,91594],{"id":91593},"_4-run-the-file","4. Run the file",[14,91596,91597],{},"When you run the file, Python executes the code and shows the output.",[14,91599,177],{},[36,91601,91604],{"className":91602,"code":91603,"language":5000,"meta":41},[4998],"Hello from Python\n",[18,91605,91603],{"__ignoreMap":41},[14,91607,91608],{},"Once you can do this, you are ready to move on to writing your first small programs.",[31,91610,91612],{"id":91611},"what-python-is-not","What Python is not",[14,91614,91615],{},"Beginners often hear about Python in specific areas and get the wrong idea. Here are a few important clarifications.",[712,91617,91619],{"id":91618},"python-is-not-only-for-data-science","Python is not only for data science",[14,91621,91622],{},"Python is used in data science, but that is only one part of what it can do. It is also used for scripting, automation, websites, APIs, testing, and general programming.",[712,91624,91626],{"id":91625},"python-is-not-only-for-experts","Python is not only for experts",[14,91628,91629],{},"Python is widely used by beginners because it is easier to read than many other languages.",[712,91631,91633],{"id":91632},"python-is-not-the-same-as-a-library-framework-or-ide","Python is not the same as a library, framework, or IDE",[14,91635,91636],{},"These are different things:",[195,91638,91639,91644,91650,91656],{},[198,91640,91641,91643],{},[2421,91642,16791],{}," is the language",[198,91645,2419,91646,91649],{},[2421,91647,91648],{},"library"," is code you can use in your program",[198,91651,2419,91652,91655],{},[2421,91653,91654],{},"framework"," is a larger tool for building certain kinds of applications",[198,91657,19451,91658,91661],{},[2421,91659,91660],{},"IDE"," is a program for writing and running code",[14,91663,91664,91665,91667],{},"For example, Python is the language, while VS Code is an editor and ",[18,91666,16138],{}," is a package tool.",[712,91669,91671],{"id":91670},"learning-python-does-not-require-advanced-math","Learning Python does not require advanced math",[14,91673,91674],{},"For basic programming, you do not need advanced math. You mainly need practice, patience, and a willingness to solve problems step by step.",[31,91676,91678],{"id":91677},"best-next-steps-after-this-page","Best next steps after this page",[14,91680,91681],{},"After learning what Python is, the best next move is to start using it.",[14,91683,91684],{},"A simple path is:",[346,91686,91687,91692,91697,91701,91705],{},[198,91688,91689],{},[236,91690,91691],{"href":17495},"Install Python on your computer",[198,91693,91694],{},[236,91695,91696],{"href":17173},"Learn how to run Python code",[198,91698,91699],{},[236,91700,17217],{"href":17216},[198,91702,4296,91703],{},[236,91704,39616],{"href":39615},[198,91706,91707,91708],{},"Continue with ",[236,91709,35582],{"href":35108},[31,91711,91713],{"id":91712},"common-mistakes-and-misunderstandings","Common mistakes and misunderstandings",[14,91715,91716],{},"These are common beginner misunderstandings about Python:",[195,91718,91719,91722,91728,91731],{},[198,91720,91721],{},"Thinking Python is only for one field such as AI or data science",[198,91723,91724,91725,91727],{},"Confusing Python the language with tools like ",[18,91726,16138],{},", IDLE, or VS Code",[198,91729,91730],{},"Expecting to understand all Python features before writing any code",[198,91732,91733],{},"Believing simple syntax means Python is only for simple programs",[14,91735,91736],{},"A better approach is to start small, run simple code, and learn new ideas as you need them.",[31,91738,2281],{"id":2280},[712,91740,91742],{"id":91741},"is-python-good-for-complete-beginners","Is Python good for complete beginners?",[14,91744,91745],{},"Yes. Python is widely used for learning because its syntax is easier to read and beginners can build useful programs quickly.",[712,91747,91749],{"id":91748},"what-can-i-build-with-python","What can I build with Python?",[14,91751,91752],{},"You can build scripts, file tools, websites, APIs, data-processing programs, and many beginner projects.",[712,91754,91756],{"id":91755},"do-i-need-to-install-python-before-learning-it","Do I need to install Python before learning it?",[14,91758,91759],{},"You can read examples without installing it, but you should install Python soon so you can practice by running code.",[712,91761,91763],{"id":91762},"is-python-a-good-language-for-jobs","Is Python a good language for jobs?",[14,91765,91766],{},"Yes. Python is used in web development, automation, testing, data work, and many other real-world areas.",[712,91768,91770],{"id":91769},"is-python-only-used-for-data-science","Is Python only used for data science?",[14,91772,91773],{},"No. It is also used for scripting, web development, APIs, automation, file handling, and general programming.",[31,91775,2342],{"id":2341},[195,91777,91778,91783,91787,91791,91795,91799,91803,91807],{},[198,91779,91780],{},[236,91781,91782],{"href":91223},"Why learn Python: use cases and benefits",[198,91784,91785],{},[236,91786,19063],{"href":17495},[198,91788,91789],{},[236,91790,17425],{"href":17173},[198,91792,91793],{},[236,91794,19068],{"href":17216},[198,91796,91797],{},[236,91798,40356],{"href":39615},[198,91800,91801],{},[236,91802,40783],{"href":40782},[198,91804,91805],{},[236,91806,35582],{"href":35108},[198,91808,91809],{},[236,91810,88462],{"href":74856},[2367,91812,91813],{},"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 .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 .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 .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}",{"title":41,"searchDepth":63,"depth":63,"links":91815},[91816,91817,91818,91825,91828,91829,91836,91842,91843,91844,91851],{"id":91132,"depth":63,"text":91133},{"id":91171,"depth":63,"text":91172},{"id":91227,"depth":63,"text":91228,"children":91819},[91820,91821,91822,91823,91824],{"id":91234,"depth":70,"text":91235},{"id":91241,"depth":70,"text":91242},{"id":91248,"depth":70,"text":91249},{"id":91259,"depth":70,"text":91260},{"id":91266,"depth":70,"text":91267},{"id":91278,"depth":63,"text":91279,"children":91826},[91827],{"id":91410,"depth":70,"text":463},{"id":91458,"depth":63,"text":91459},{"id":91521,"depth":63,"text":91522,"children":91830},[91831,91832,91833,91835],{"id":91528,"depth":70,"text":91529},{"id":91537,"depth":70,"text":91538},{"id":91557,"depth":70,"text":91834},"3. Create a .py file",{"id":91593,"depth":70,"text":91594},{"id":91611,"depth":63,"text":91612,"children":91837},[91838,91839,91840,91841],{"id":91618,"depth":70,"text":91619},{"id":91625,"depth":70,"text":91626},{"id":91632,"depth":70,"text":91633},{"id":91670,"depth":70,"text":91671},{"id":91677,"depth":63,"text":91678},{"id":91712,"depth":63,"text":91713},{"id":2280,"depth":63,"text":2281,"children":91845},[91846,91847,91848,91849,91850],{"id":91741,"depth":70,"text":91742},{"id":91748,"depth":70,"text":91749},{"id":91755,"depth":70,"text":91756},{"id":91762,"depth":70,"text":91763},{"id":91769,"depth":70,"text":91770},{"id":2341,"depth":63,"text":2342},"Master what is python a beginner friendly introduction in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fwhat-is-python-a-beginner-friendly-introduction",{"title":91118,"description":91852},"learn\u002Fwhat-is-python-a-beginner-friendly-introduction","w9QKHHM5vsTRNUJrYIqNt6PgqZh_1l7HF9vn7FZuVd8",{"id":91859,"title":91860,"body":91861,"description":93847,"extension":2399,"meta":93848,"navigation":66,"path":93849,"seo":93850,"stem":93851,"__hash__":93852},"content\u002Flearn\u002Fwhen-to-use-lists-vs-tuples-vs-sets-vs-dictionaries.md","When to Use Lists vs Tuples vs Sets vs Dictionaries",{"type":7,"value":91862,"toc":93821},[91863,91866,91869,91872,91893,91896,91900,92040,92055,92059,92062,92073,92077,92080,92102,92105,92268,92270,92363,92366,92387,92391,92394,92397,92416,92418,92432,92526,92528,92569,92574,92578,92581,92584,92598,92601,92676,92678,92692,92695,92698,92733,92743,92747,92750,92753,92764,92767,92777,92897,92899,92940,92943,92952,92956,92959,92962,92979,92982,93088,93090,93104,93107,93110,93129,93132,93148,93155,93159,93162,93184,93187,93238,93242,93245,93249,93302,93305,93309,93355,93358,93362,93422,93425,93429,93517,93520,93522,93525,93539,93542,93555,93558,93686,93689,93702,93706,93712,93715,93737,93740,93742,93746,93749,93751,93754,93758,93767,93771,93774,93778,93781,93783,93818],[10,91864,91860],{"id":91865},"when-to-use-lists-vs-tuples-vs-sets-vs-dictionaries",[14,91867,91868],{},"Choosing the right Python collection type is easier when you focus on how the data will be used.",[14,91870,91871],{},"This page helps you decide between:",[195,91873,91874,91879,91884,91889],{},[198,91875,91876,91878],{},[2421,91877,20890],{}," for ordered items you may change",[198,91880,91881,91883],{},[2421,91882,22293],{}," for ordered data that should stay fixed",[198,91885,91886,91888],{},[2421,91887,20896],{}," for unique values",[198,91890,91891,61886],{},[2421,91892,20893],{},[14,91894,91895],{},"If you are new to Python, it is common to use a list for everything at first. But Python gives you different collection types because they solve different problems.",[31,91897,91899],{"id":91898},"quick-answer","Quick answer",[36,91901,91903],{"className":38,"code":91902,"language":40,"meta":41,"style":41},"shopping_list = [\"milk\", \"eggs\", \"bread\"]      # list: ordered, changeable\npoint = (10, 20)                                # tuple: ordered, not meant to change\nunique_tags = {\"python\", \"beginner\", \"tips\"}   # set: unique items, no duplicates\nuser = {\"name\": \"Ana\", \"age\": 25}              # dictionary: key-value pairs\n",[18,91904,91905,91944,91963,92001],{"__ignoreMap":41},[45,91906,91907,91910,91912,91914,91916,91919,91921,91923,91925,91928,91930,91932,91934,91937,91939,91941],{"class":47,"line":48},[45,91908,91909],{"class":51},"shopping_list ",[45,91911,56],{"class":55},[45,91913,8575],{"class":85},[45,91915,100],{"class":99},[45,91917,91918],{"class":103},"milk",[45,91920,100],{"class":99},[45,91922,3079],{"class":85},[45,91924,1565],{"class":99},[45,91926,91927],{"class":103},"eggs",[45,91929,100],{"class":99},[45,91931,3079],{"class":85},[45,91933,1565],{"class":99},[45,91935,91936],{"class":103},"bread",[45,91938,100],{"class":99},[45,91940,9685],{"class":85},[45,91942,91943],{"class":7624},"      # list: ordered, changeable\n",[45,91945,91946,91948,91950,91952,91954,91956,91958,91960],{"class":47,"line":63},[45,91947,41291],{"class":51},[45,91949,56],{"class":55},[45,91951,14039],{"class":85},[45,91953,11786],{"class":59},[45,91955,3079],{"class":85},[45,91957,10503],{"class":59},[45,91959,9146],{"class":85},[45,91961,91962],{"class":7624},"                                # tuple: ordered, not meant to change\n",[45,91964,91965,91968,91970,91972,91974,91976,91978,91980,91982,91985,91987,91989,91991,91994,91996,91998],{"class":47,"line":70},[45,91966,91967],{"class":51},"unique_tags ",[45,91969,56],{"class":55},[45,91971,8604],{"class":85},[45,91973,100],{"class":99},[45,91975,40],{"class":103},[45,91977,100],{"class":99},[45,91979,3079],{"class":85},[45,91981,1565],{"class":99},[45,91983,91984],{"class":103},"beginner",[45,91986,100],{"class":99},[45,91988,3079],{"class":85},[45,91990,1565],{"class":99},[45,91992,91993],{"class":103},"tips",[45,91995,100],{"class":99},[45,91997,5556],{"class":85},[45,91999,92000],{"class":7624},"   # set: unique items, no duplicates\n",[45,92002,92003,92005,92007,92009,92011,92013,92015,92017,92019,92021,92023,92025,92027,92029,92031,92033,92035,92037],{"class":47,"line":89},[45,92004,30062],{"class":51},[45,92006,56],{"class":55},[45,92008,8604],{"class":85},[45,92010,100],{"class":99},[45,92012,3096],{"class":103},[45,92014,100],{"class":99},[45,92016,457],{"class":85},[45,92018,1565],{"class":99},[45,92020,10580],{"class":103},[45,92022,100],{"class":99},[45,92024,3079],{"class":85},[45,92026,1565],{"class":99},[45,92028,2082],{"class":103},[45,92030,100],{"class":99},[45,92032,457],{"class":85},[45,92034,303],{"class":59},[45,92036,5556],{"class":85},[45,92038,92039],{"class":7624},"              # dictionary: key-value pairs\n",[14,92041,19194,92042,92044,92045,92047,92048,92050,92051,92054],{},[2421,92043,14463],{}," for a sequence, a ",[2421,92046,29752],{}," for fixed data, a ",[2421,92049,29940],{}," for unique values, and a ",[2421,92052,92053],{},"dictionary"," for named values.",[31,92056,92058],{"id":92057},"what-this-page-helps-you-decide","What this page helps you decide",[14,92060,92061],{},"This page will help you:",[195,92063,92064,92067,92070],{},[198,92065,92066],{},"Choose the right collection type for a beginner-level problem",[198,92068,92069],{},"Compare list, tuple, set, and dictionary by use case",[198,92071,92072],{},"Avoid guessing based only on syntax",[31,92074,92076],{"id":92075},"quick-comparison-list-vs-tuple-vs-set-vs-dictionary","Quick comparison: list vs tuple vs set vs dictionary",[14,92078,92079],{},"Here is the simplest way to compare them:",[195,92081,92082,92087,92092,92097],{},[198,92083,92084,92086],{},[2421,92085,28294],{},": ordered, changeable, allows duplicates",[198,92088,92089,92091],{},[2421,92090,61873],{},": ordered, usually used for fixed data, allows duplicates",[198,92093,92094,92096],{},[2421,92095,61879],{},": unordered, changeable, no duplicate values",[198,92098,92099,92101],{},[2421,92100,61885],{},": key-value pairs, changeable, keys must be unique",[14,92103,92104],{},"You can also see the difference in code:",[36,92106,92108],{"className":38,"code":92107,"language":40,"meta":41,"style":41},"numbers_list = [1, 2, 2, 3]\nnumbers_tuple = (1, 2, 2, 3)\nnumbers_set = {1, 2, 2, 3}\nuser = {\"name\": \"Sam\", \"age\": 30}\n\nprint(numbers_list)\nprint(numbers_tuple)\nprint(numbers_set)\nprint(user)\n",[18,92109,92110,92135,92160,92185,92221,92225,92236,92247,92258],{"__ignoreMap":41},[45,92111,92112,92115,92117,92119,92121,92123,92125,92127,92129,92131,92133],{"class":47,"line":48},[45,92113,92114],{"class":51},"numbers_list ",[45,92116,56],{"class":55},[45,92118,8575],{"class":85},[45,92120,8578],{"class":59},[45,92122,3079],{"class":85},[45,92124,3754],{"class":59},[45,92126,3079],{"class":85},[45,92128,3754],{"class":59},[45,92130,3079],{"class":85},[45,92132,777],{"class":59},[45,92134,8594],{"class":85},[45,92136,92137,92140,92142,92144,92146,92148,92150,92152,92154,92156,92158],{"class":47,"line":63},[45,92138,92139],{"class":51},"numbers_tuple ",[45,92141,56],{"class":55},[45,92143,14039],{"class":85},[45,92145,8578],{"class":59},[45,92147,3079],{"class":85},[45,92149,3754],{"class":59},[45,92151,3079],{"class":85},[45,92153,3754],{"class":59},[45,92155,3079],{"class":85},[45,92157,777],{"class":59},[45,92159,109],{"class":85},[45,92161,92162,92165,92167,92169,92171,92173,92175,92177,92179,92181,92183],{"class":47,"line":70},[45,92163,92164],{"class":51},"numbers_set ",[45,92166,56],{"class":55},[45,92168,8604],{"class":85},[45,92170,8578],{"class":59},[45,92172,3079],{"class":85},[45,92174,3754],{"class":59},[45,92176,3079],{"class":85},[45,92178,3754],{"class":59},[45,92180,3079],{"class":85},[45,92182,777],{"class":59},[45,92184,8629],{"class":85},[45,92186,92187,92189,92191,92193,92195,92197,92199,92201,92203,92205,92207,92209,92211,92213,92215,92217,92219],{"class":47,"line":89},[45,92188,30062],{"class":51},[45,92190,56],{"class":55},[45,92192,8604],{"class":85},[45,92194,100],{"class":99},[45,92196,3096],{"class":103},[45,92198,100],{"class":99},[45,92200,457],{"class":85},[45,92202,1565],{"class":99},[45,92204,1568],{"class":103},[45,92206,100],{"class":99},[45,92208,3079],{"class":85},[45,92210,1565],{"class":99},[45,92212,2082],{"class":103},[45,92214,100],{"class":99},[45,92216,457],{"class":85},[45,92218,10512],{"class":59},[45,92220,8629],{"class":85},[45,92222,92223],{"class":47,"line":112},[45,92224,67],{"emptyLinePlaceholder":66},[45,92226,92227,92229,92231,92234],{"class":47,"line":126},[45,92228,2065],{"class":92},[45,92230,96],{"class":85},[45,92232,92233],{"class":2070},"numbers_list",[45,92235,109],{"class":85},[45,92237,92238,92240,92242,92245],{"class":47,"line":142},[45,92239,2065],{"class":92},[45,92241,96],{"class":85},[45,92243,92244],{"class":2070},"numbers_tuple",[45,92246,109],{"class":85},[45,92248,92249,92251,92253,92256],{"class":47,"line":149},[45,92250,2065],{"class":92},[45,92252,96],{"class":85},[45,92254,92255],{"class":2070},"numbers_set",[45,92257,109],{"class":85},[45,92259,92260,92262,92264,92266],{"class":47,"line":679},[45,92261,2065],{"class":92},[45,92263,96],{"class":85},[45,92265,30087],{"class":2070},[45,92267,109],{"class":85},[14,92269,13456],{},[36,92271,92273],{"className":38,"code":92272,"language":40,"meta":41,"style":41},"[1, 2, 2, 3]\n(1, 2, 2, 3)\n{1, 2, 3}\n{'name': 'Sam', 'age': 30}\n",[18,92274,92275,92295,92315,92331],{"__ignoreMap":41},[45,92276,92277,92279,92281,92283,92285,92287,92289,92291,92293],{"class":47,"line":48},[45,92278,9680],{"class":85},[45,92280,8578],{"class":59},[45,92282,3079],{"class":85},[45,92284,3754],{"class":59},[45,92286,3079],{"class":85},[45,92288,3754],{"class":59},[45,92290,3079],{"class":85},[45,92292,777],{"class":59},[45,92294,8594],{"class":85},[45,92296,92297,92299,92301,92303,92305,92307,92309,92311,92313],{"class":47,"line":63},[45,92298,96],{"class":85},[45,92300,8578],{"class":59},[45,92302,3079],{"class":85},[45,92304,3754],{"class":59},[45,92306,3079],{"class":85},[45,92308,3754],{"class":59},[45,92310,3079],{"class":85},[45,92312,777],{"class":59},[45,92314,109],{"class":85},[45,92316,92317,92319,92321,92323,92325,92327,92329],{"class":47,"line":70},[45,92318,5551],{"class":85},[45,92320,8578],{"class":59},[45,92322,3079],{"class":85},[45,92324,3754],{"class":59},[45,92326,3079],{"class":85},[45,92328,777],{"class":59},[45,92330,8629],{"class":85},[45,92332,92333,92335,92337,92339,92341,92343,92345,92347,92349,92351,92353,92355,92357,92359,92361],{"class":47,"line":89},[45,92334,5551],{"class":85},[45,92336,7901],{"class":99},[45,92338,3096],{"class":103},[45,92340,7901],{"class":99},[45,92342,457],{"class":85},[45,92344,9199],{"class":99},[45,92346,1568],{"class":103},[45,92348,7901],{"class":99},[45,92350,3079],{"class":85},[45,92352,9199],{"class":99},[45,92354,2082],{"class":103},[45,92356,7901],{"class":99},[45,92358,457],{"class":85},[45,92360,10512],{"class":59},[45,92362,8629],{"class":85},[14,92364,92365],{},"Notice:",[195,92367,92368,92373,92377,92382],{},[198,92369,1732,92370,92372],{},[2421,92371,14463],{}," keeps order and duplicates",[198,92374,1732,92375,92372],{},[2421,92376,29752],{},[198,92378,1732,92379,92381],{},[2421,92380,29940],{}," removes duplicates",[198,92383,1732,92384,92386],{},[2421,92385,92053],{}," stores values with names",[31,92388,92390],{"id":92389},"use-a-list-when-order-matters-and-you-may-change-the-data","Use a list when order matters and you may change the data",[14,92392,92393],{},"Use a list when you have a sequence of items and you may add, remove, or change them later.",[14,92395,92396],{},"A list is best when:",[195,92398,92399,92402,92405,92413],{},[198,92400,92401],{},"Order matters",[198,92403,92404],{},"You want to access items by position",[198,92406,92407,92408],{},"You may need methods like ",[236,92409,92410,92412],{"href":60882},[18,92411,31847],{}," for lists",[198,92414,92415],{},"Duplicates are allowed",[14,92417,42917],{},[195,92419,92420,92423,92426,92429],{},[198,92421,92422],{},"To-do items",[198,92424,92425],{},"Names",[198,92427,92428],{},"Scores",[198,92430,92431],{},"Lines from a file",[36,92433,92435],{"className":38,"code":92434,"language":40,"meta":41,"style":41},"shopping_list = [\"milk\", \"eggs\", \"bread\"]\n\nshopping_list.append(\"butter\")\nshopping_list[1] = \"cheese\"\n\nprint(shopping_list)\n",[18,92436,92437,92469,92473,92493,92512,92516],{"__ignoreMap":41},[45,92438,92439,92441,92443,92445,92447,92449,92451,92453,92455,92457,92459,92461,92463,92465,92467],{"class":47,"line":48},[45,92440,91909],{"class":51},[45,92442,56],{"class":55},[45,92444,8575],{"class":85},[45,92446,100],{"class":99},[45,92448,91918],{"class":103},[45,92450,100],{"class":99},[45,92452,3079],{"class":85},[45,92454,1565],{"class":99},[45,92456,91927],{"class":103},[45,92458,100],{"class":99},[45,92460,3079],{"class":85},[45,92462,1565],{"class":99},[45,92464,91936],{"class":103},[45,92466,100],{"class":99},[45,92468,8594],{"class":85},[45,92470,92471],{"class":47,"line":63},[45,92472,67],{"emptyLinePlaceholder":66},[45,92474,92475,92478,92480,92482,92484,92486,92489,92491],{"class":47,"line":70},[45,92476,92477],{"class":51},"shopping_list",[45,92479,240],{"class":85},[45,92481,21025],{"class":2070},[45,92483,96],{"class":85},[45,92485,100],{"class":99},[45,92487,92488],{"class":103},"butter",[45,92490,100],{"class":99},[45,92492,109],{"class":85},[45,92494,92495,92497,92499,92501,92503,92505,92507,92510],{"class":47,"line":89},[45,92496,92477],{"class":51},[45,92498,9680],{"class":85},[45,92500,8578],{"class":59},[45,92502,9685],{"class":85},[45,92504,3099],{"class":55},[45,92506,1565],{"class":99},[45,92508,92509],{"class":103},"cheese",[45,92511,1571],{"class":99},[45,92513,92514],{"class":47,"line":112},[45,92515,67],{"emptyLinePlaceholder":66},[45,92517,92518,92520,92522,92524],{"class":47,"line":126},[45,92519,2065],{"class":92},[45,92521,96],{"class":85},[45,92523,92477],{"class":2070},[45,92525,109],{"class":85},[14,92527,177],{},[36,92529,92531],{"className":38,"code":92530,"language":40,"meta":41,"style":41},"['milk', 'cheese', 'bread', 'butter']\n",[18,92532,92533],{"__ignoreMap":41},[45,92534,92535,92537,92539,92541,92543,92545,92547,92549,92551,92553,92555,92557,92559,92561,92563,92565,92567],{"class":47,"line":48},[45,92536,9680],{"class":85},[45,92538,7901],{"class":99},[45,92540,91918],{"class":103},[45,92542,7901],{"class":99},[45,92544,3079],{"class":85},[45,92546,9199],{"class":99},[45,92548,92509],{"class":103},[45,92550,7901],{"class":99},[45,92552,3079],{"class":85},[45,92554,9199],{"class":99},[45,92556,91936],{"class":103},[45,92558,7901],{"class":99},[45,92560,3079],{"class":85},[45,92562,9199],{"class":99},[45,92564,92488],{"class":103},[45,92566,7901],{"class":99},[45,92568,8594],{"class":85},[14,92570,92571,92572,240],{},"A list is usually the right choice for general ordered data. If you want a full beginner guide, see ",[236,92573,27366],{"href":27365},[31,92575,92577],{"id":92576},"use-a-tuple-when-the-data-should-stay-fixed","Use a tuple when the data should stay fixed",[14,92579,92580],{},"Use a tuple when values belong together and should not be changed often.",[14,92582,92583],{},"A tuple is good for:",[195,92585,92586,92589,92592,92595],{},[198,92587,92588],{},"Coordinates",[198,92590,92591],{},"RGB color values",[198,92593,92594],{},"Dates",[198,92596,92597],{},"Returning multiple values from a function",[14,92599,92600],{},"Like a list, a tuple is ordered. You can still read items by index.",[36,92602,92604],{"className":38,"code":92603,"language":40,"meta":41,"style":41},"point = (10, 20)\ncolor = (255, 100, 50)\n\nprint(point[0])\nprint(color[2])\n",[18,92605,92606,92622,92644,92648,92662],{"__ignoreMap":41},[45,92607,92608,92610,92612,92614,92616,92618,92620],{"class":47,"line":48},[45,92609,41291],{"class":51},[45,92611,56],{"class":55},[45,92613,14039],{"class":85},[45,92615,11786],{"class":59},[45,92617,3079],{"class":85},[45,92619,10503],{"class":59},[45,92621,109],{"class":85},[45,92623,92624,92627,92629,92631,92634,92636,92638,92640,92642],{"class":47,"line":63},[45,92625,92626],{"class":51},"color ",[45,92628,56],{"class":55},[45,92630,14039],{"class":85},[45,92632,92633],{"class":59},"255",[45,92635,3079],{"class":85},[45,92637,20280],{"class":59},[45,92639,3079],{"class":85},[45,92641,20121],{"class":59},[45,92643,109],{"class":85},[45,92645,92646],{"class":47,"line":70},[45,92647,67],{"emptyLinePlaceholder":66},[45,92649,92650,92652,92654,92656,92658,92660],{"class":47,"line":89},[45,92651,2065],{"class":92},[45,92653,96],{"class":85},[45,92655,41312],{"class":2070},[45,92657,9680],{"class":85},[45,92659,10482],{"class":59},[45,92661,21941],{"class":85},[45,92663,92664,92666,92668,92670,92672,92674],{"class":47,"line":112},[45,92665,2065],{"class":92},[45,92667,96],{"class":85},[45,92669,22378],{"class":2070},[45,92671,9680],{"class":85},[45,92673,9367],{"class":59},[45,92675,21941],{"class":85},[14,92677,177],{},[36,92679,92681],{"className":38,"code":92680,"language":40,"meta":41,"style":41},"10\n50\n",[18,92682,92683,92687],{"__ignoreMap":41},[45,92684,92685],{"class":47,"line":48},[45,92686,13262],{"class":59},[45,92688,92689],{"class":47,"line":63},[45,92690,92691],{"class":59},"50\n",[14,92693,92694],{},"The main idea is not just syntax. It is meaning. A tuple tells readers, “this data is fixed.”",[14,92696,92697],{},"If you try to change a tuple item, Python raises an error:",[36,92699,92701],{"className":38,"code":92700,"language":40,"meta":41,"style":41},"point = (10, 20)\npoint[0] = 99\n",[18,92702,92703,92719],{"__ignoreMap":41},[45,92704,92705,92707,92709,92711,92713,92715,92717],{"class":47,"line":48},[45,92706,41291],{"class":51},[45,92708,56],{"class":55},[45,92710,14039],{"class":85},[45,92712,11786],{"class":59},[45,92714,3079],{"class":85},[45,92716,10503],{"class":59},[45,92718,109],{"class":85},[45,92720,92721,92723,92725,92727,92729,92731],{"class":47,"line":63},[45,92722,41312],{"class":51},[45,92724,9680],{"class":85},[45,92726,10482],{"class":59},[45,92728,9685],{"class":85},[45,92730,3099],{"class":55},[45,92732,50943],{"class":59},[14,92734,92735,92736,1316,92740,240],{},"You can learn more in ",[236,92737,92739],{"href":92738},"\u002Flearn\u002Fpython-tuples-explained\u002F","Python tuples explained",[236,92741,92742],{"href":21277},"mutability in Python",[31,92744,92746],{"id":92745},"use-a-set-when-you-only-care-about-unique-values","Use a set when you only care about unique values",[14,92748,92749],{},"Use a set when duplicates should be removed or when you only care whether a value exists.",[14,92751,92752],{},"A set is best for:",[195,92754,92755,92758,92761],{},[198,92756,92757],{},"Removing duplicates",[198,92759,92760],{},"Fast membership checks",[198,92762,92763],{},"Keeping unique categories, tags, or IDs",[14,92765,92766],{},"Do not use a set if you need:",[195,92768,92769,92772],{},[198,92770,92771],{},"Item order",[198,92773,92774,92775],{},"Indexing like ",[18,92776,14264],{},[36,92778,92780],{"className":38,"code":92779,"language":40,"meta":41,"style":41},"tags = [\"python\", \"beginner\", \"python\", \"tips\", \"tips\"]\nunique_tags = set(tags)\n\nprint(unique_tags)\nprint(\"python\" in unique_tags)\nprint(\"java\" in unique_tags)\n",[18,92781,92782,92830,92845,92849,92860,92879],{"__ignoreMap":41},[45,92783,92784,92786,92788,92790,92792,92794,92796,92798,92800,92802,92804,92806,92808,92810,92812,92814,92816,92818,92820,92822,92824,92826,92828],{"class":47,"line":48},[45,92785,41834],{"class":51},[45,92787,56],{"class":55},[45,92789,8575],{"class":85},[45,92791,100],{"class":99},[45,92793,40],{"class":103},[45,92795,100],{"class":99},[45,92797,3079],{"class":85},[45,92799,1565],{"class":99},[45,92801,91984],{"class":103},[45,92803,100],{"class":99},[45,92805,3079],{"class":85},[45,92807,1565],{"class":99},[45,92809,40],{"class":103},[45,92811,100],{"class":99},[45,92813,3079],{"class":85},[45,92815,1565],{"class":99},[45,92817,91993],{"class":103},[45,92819,100],{"class":99},[45,92821,3079],{"class":85},[45,92823,1565],{"class":99},[45,92825,91993],{"class":103},[45,92827,100],{"class":99},[45,92829,8594],{"class":85},[45,92831,92832,92834,92836,92838,92840,92843],{"class":47,"line":63},[45,92833,91967],{"class":51},[45,92835,56],{"class":55},[45,92837,66471],{"class":2109},[45,92839,96],{"class":85},[45,92841,92842],{"class":2070},"tags",[45,92844,109],{"class":85},[45,92846,92847],{"class":47,"line":70},[45,92848,67],{"emptyLinePlaceholder":66},[45,92850,92851,92853,92855,92858],{"class":47,"line":89},[45,92852,2065],{"class":92},[45,92854,96],{"class":85},[45,92856,92857],{"class":2070},"unique_tags",[45,92859,109],{"class":85},[45,92861,92862,92864,92866,92868,92870,92872,92874,92877],{"class":47,"line":112},[45,92863,2065],{"class":92},[45,92865,96],{"class":85},[45,92867,100],{"class":99},[45,92869,40],{"class":103},[45,92871,100],{"class":99},[45,92873,45650],{"class":73},[45,92875,92876],{"class":2070}," unique_tags",[45,92878,109],{"class":85},[45,92880,92881,92883,92885,92887,92889,92891,92893,92895],{"class":47,"line":126},[45,92882,2065],{"class":92},[45,92884,96],{"class":85},[45,92886,100],{"class":99},[45,92888,69302],{"class":103},[45,92890,100],{"class":99},[45,92892,45650],{"class":73},[45,92894,92876],{"class":2070},[45,92896,109],{"class":85},[14,92898,13456],{},[36,92900,92902],{"className":38,"code":92901,"language":40,"meta":41,"style":41},"{'python', 'beginner', 'tips'}\nTrue\nFalse\n",[18,92903,92904,92932,92936],{"__ignoreMap":41},[45,92905,92906,92908,92910,92912,92914,92916,92918,92920,92922,92924,92926,92928,92930],{"class":47,"line":48},[45,92907,5551],{"class":85},[45,92909,7901],{"class":99},[45,92911,40],{"class":103},[45,92913,7901],{"class":99},[45,92915,3079],{"class":85},[45,92917,9199],{"class":99},[45,92919,91984],{"class":103},[45,92921,7901],{"class":99},[45,92923,3079],{"class":85},[45,92925,9199],{"class":99},[45,92927,91993],{"class":103},[45,92929,7901],{"class":99},[45,92931,8629],{"class":85},[45,92933,92934],{"class":47,"line":63},[45,92935,2270],{"class":481},[45,92937,92938],{"class":47,"line":70},[45,92939,2265],{"class":481},[14,92941,92942],{},"The exact order of set output can vary.",[14,92944,66517,92945,92947,92948,240],{},[236,92946,66521],{"href":66520},". For a full overview, read ",[236,92949,92951],{"href":92950},"\u002Flearn\u002Fpython-sets-explained\u002F","Python sets explained",[31,92953,92955],{"id":92954},"use-a-dictionary-when-values-have-names","Use a dictionary when values have names",[14,92957,92958],{},"Use a dictionary when each piece of data has a label.",[14,92960,92961],{},"A dictionary is best for:",[195,92963,92964,92967,92970,92973,92976],{},[198,92965,92966],{},"User data",[198,92968,92969],{},"Settings",[198,92971,92972],{},"API responses",[198,92974,92975],{},"Counting words",[198,92977,92978],{},"Any data where a key describes a value",[14,92980,92981],{},"This is often clearer than using a list and trying to remember positions.",[36,92983,92985],{"className":38,"code":92984,"language":40,"meta":41,"style":41},"user = {\n    \"name\": \"Ana\",\n    \"age\": 25,\n    \"email\": \"ana@example.com\"\n}\n\nprint(user[\"name\"])\nprint(user[\"age\"])\n",[18,92986,92987,92995,93013,93027,93044,93048,93052,93070],{"__ignoreMap":41},[45,92988,92989,92991,92993],{"class":47,"line":48},[45,92990,30062],{"class":51},[45,92992,56],{"class":55},[45,92994,41432],{"class":85},[45,92996,92997,92999,93001,93003,93005,93007,93009,93011],{"class":47,"line":63},[45,92998,41437],{"class":99},[45,93000,3096],{"class":103},[45,93002,100],{"class":99},[45,93004,457],{"class":85},[45,93006,1565],{"class":99},[45,93008,10580],{"class":103},[45,93010,100],{"class":99},[45,93012,41453],{"class":85},[45,93014,93015,93017,93019,93021,93023,93025],{"class":47,"line":70},[45,93016,41437],{"class":99},[45,93018,2082],{"class":103},[45,93020,100],{"class":99},[45,93022,457],{"class":85},[45,93024,303],{"class":59},[45,93026,41453],{"class":85},[45,93028,93029,93031,93033,93035,93037,93039,93042],{"class":47,"line":89},[45,93030,41437],{"class":99},[45,93032,20561],{"class":103},[45,93034,100],{"class":99},[45,93036,457],{"class":85},[45,93038,1565],{"class":99},[45,93040,93041],{"class":103},"ana@example.com",[45,93043,1571],{"class":99},[45,93045,93046],{"class":47,"line":112},[45,93047,8629],{"class":85},[45,93049,93050],{"class":47,"line":126},[45,93051,67],{"emptyLinePlaceholder":66},[45,93053,93054,93056,93058,93060,93062,93064,93066,93068],{"class":47,"line":142},[45,93055,2065],{"class":92},[45,93057,96],{"class":85},[45,93059,30087],{"class":2070},[45,93061,9680],{"class":85},[45,93063,100],{"class":99},[45,93065,3096],{"class":103},[45,93067,100],{"class":99},[45,93069,21941],{"class":85},[45,93071,93072,93074,93076,93078,93080,93082,93084,93086],{"class":47,"line":149},[45,93073,2065],{"class":92},[45,93075,96],{"class":85},[45,93077,30087],{"class":2070},[45,93079,9680],{"class":85},[45,93081,100],{"class":99},[45,93083,2082],{"class":103},[45,93085,100],{"class":99},[45,93087,21941],{"class":85},[14,93089,177],{},[36,93091,93093],{"className":38,"code":93092,"language":40,"meta":41,"style":41},"Ana\n25\n",[18,93094,93095,93099],{"__ignoreMap":41},[45,93096,93097],{"class":47,"line":48},[45,93098,42808],{"class":51},[45,93100,93101],{"class":47,"line":63},[45,93102,93103],{"class":59},"25\n",[14,93105,93106],{},"A dictionary lets you access values by key instead of by position.",[14,93108,93109],{},"That means this:",[36,93111,93113],{"className":38,"code":93112,"language":40,"meta":41,"style":41},"user[\"email\"]\n",[18,93114,93115],{"__ignoreMap":41},[45,93116,93117,93119,93121,93123,93125,93127],{"class":47,"line":48},[45,93118,30087],{"class":51},[45,93120,9680],{"class":85},[45,93122,100],{"class":99},[45,93124,20561],{"class":103},[45,93126,100],{"class":99},[45,93128,8594],{"class":85},[14,93130,93131],{},"is usually clearer than something like this:",[36,93133,93135],{"className":38,"code":93134,"language":40,"meta":41,"style":41},"user_data[2]\n",[18,93136,93137],{"__ignoreMap":41},[45,93138,93139,93142,93144,93146],{"class":47,"line":48},[45,93140,93141],{"class":51},"user_data",[45,93143,9680],{"class":85},[45,93145,9367],{"class":59},[45,93147,8594],{"class":85},[14,93149,93150,93151,230,93153,240],{},"If you want to go deeper, read ",[236,93152,8557],{"href":30258},[236,93154,10860],{"href":45737},[31,93156,93158],{"id":93157},"simple-decision-checklist","Simple decision checklist",[14,93160,93161],{},"Use this quick checklist when choosing a type:",[195,93163,93164,93169,93174,93179],{},[198,93165,93166,93167],{},"Need key-value pairs? Use a ",[2421,93168,92053],{},[198,93170,93171,93172],{},"Need unique items only? Use a ",[2421,93173,29940],{},[198,93175,93176,93177],{},"Need ordered items you may change? Use a ",[2421,93178,14463],{},[198,93180,93181,93182],{},"Need ordered items that should stay fixed? Use a ",[2421,93183,29752],{},[14,93185,93186],{},"Another way to think about it:",[346,93188,93189,93203,93212,93223],{},[198,93190,93191,93192,230,93194,93196,93197],{},"Does each value have a name like ",[18,93193,42627],{},[18,93195,42637],{},"?\n",[195,93198,93199],{},[198,93200,19194,93201],{},[2421,93202,92053],{},[198,93204,93205,93206],{},"Do you only care about unique values?\n",[195,93207,93208],{},[198,93209,19194,93210],{},[2421,93211,29940],{},[198,93213,93214,93215],{},"Do you need an ordered sequence?\n",[195,93216,93217],{},[198,93218,19194,93219,230,93221],{},[2421,93220,14463],{},[2421,93222,29752],{},[198,93224,93225,93226],{},"Will the sequence change?\n",[195,93227,93228,93233],{},[198,93229,19194,93230,93232],{},[2421,93231,14463],{}," if yes",[198,93234,19194,93235,93237],{},[2421,93236,29752],{}," if no",[31,93239,93241],{"id":93240},"beginner-examples-of-the-same-data-in-different-structures","Beginner examples of the same data in different structures",[14,93243,93244],{},"Here are simple examples of real beginner-level data.",[712,93246,93248],{"id":93247},"shopping-items-as-a-list","Shopping items as a list",[36,93250,93252],{"className":38,"code":93251,"language":40,"meta":41,"style":41},"shopping_items = [\"milk\", \"eggs\", \"bread\"]\nprint(shopping_items[0])\n",[18,93253,93254,93287],{"__ignoreMap":41},[45,93255,93256,93259,93261,93263,93265,93267,93269,93271,93273,93275,93277,93279,93281,93283,93285],{"class":47,"line":48},[45,93257,93258],{"class":51},"shopping_items ",[45,93260,56],{"class":55},[45,93262,8575],{"class":85},[45,93264,100],{"class":99},[45,93266,91918],{"class":103},[45,93268,100],{"class":99},[45,93270,3079],{"class":85},[45,93272,1565],{"class":99},[45,93274,91927],{"class":103},[45,93276,100],{"class":99},[45,93278,3079],{"class":85},[45,93280,1565],{"class":99},[45,93282,91936],{"class":103},[45,93284,100],{"class":99},[45,93286,8594],{"class":85},[45,93288,93289,93291,93293,93296,93298,93300],{"class":47,"line":63},[45,93290,2065],{"class":92},[45,93292,96],{"class":85},[45,93294,93295],{"class":2070},"shopping_items",[45,93297,9680],{"class":85},[45,93299,10482],{"class":59},[45,93301,21941],{"class":85},[14,93303,93304],{},"Use a list because the order matters and the items may change.",[712,93306,93308],{"id":93307},"a-screen-size-as-a-tuple","A screen size as a tuple",[36,93310,93312],{"className":38,"code":93311,"language":40,"meta":41,"style":41},"screen_size = (1920, 1080)\nprint(screen_size[0], screen_size[1])\n",[18,93313,93314,93331],{"__ignoreMap":41},[45,93315,93316,93319,93321,93323,93325,93327,93329],{"class":47,"line":48},[45,93317,93318],{"class":51},"screen_size ",[45,93320,56],{"class":55},[45,93322,14039],{"class":85},[45,93324,74154],{"class":59},[45,93326,3079],{"class":85},[45,93328,74159],{"class":59},[45,93330,109],{"class":85},[45,93332,93333,93335,93337,93340,93342,93344,93346,93349,93351,93353],{"class":47,"line":63},[45,93334,2065],{"class":92},[45,93336,96],{"class":85},[45,93338,93339],{"class":2070},"screen_size",[45,93341,9680],{"class":85},[45,93343,10482],{"class":59},[45,93345,31090],{"class":85},[45,93347,93348],{"class":2070}," screen_size",[45,93350,9680],{"class":85},[45,93352,8578],{"class":59},[45,93354,21941],{"class":85},[14,93356,93357],{},"Use a tuple because width and height belong together and usually stay fixed.",[712,93359,93361],{"id":93360},"unique-categories-as-a-set","Unique categories as a set",[36,93363,93365],{"className":38,"code":93364,"language":40,"meta":41,"style":41},"categories = {\"books\", \"games\", \"books\", \"music\"}\nprint(categories)\n",[18,93366,93367,93411],{"__ignoreMap":41},[45,93368,93369,93372,93374,93376,93378,93381,93383,93385,93387,93390,93392,93394,93396,93398,93400,93402,93404,93407,93409],{"class":47,"line":48},[45,93370,93371],{"class":51},"categories ",[45,93373,56],{"class":55},[45,93375,8604],{"class":85},[45,93377,100],{"class":99},[45,93379,93380],{"class":103},"books",[45,93382,100],{"class":99},[45,93384,3079],{"class":85},[45,93386,1565],{"class":99},[45,93388,93389],{"class":103},"games",[45,93391,100],{"class":99},[45,93393,3079],{"class":85},[45,93395,1565],{"class":99},[45,93397,93380],{"class":103},[45,93399,100],{"class":99},[45,93401,3079],{"class":85},[45,93403,1565],{"class":99},[45,93405,93406],{"class":103},"music",[45,93408,100],{"class":99},[45,93410,8629],{"class":85},[45,93412,93413,93415,93417,93420],{"class":47,"line":63},[45,93414,2065],{"class":92},[45,93416,96],{"class":85},[45,93418,93419],{"class":2070},"categories",[45,93421,109],{"class":85},[14,93423,93424],{},"Use a set because you only want unique category names.",[712,93426,93428],{"id":93427},"a-person-record-as-a-dictionary","A person record as a dictionary",[36,93430,93432],{"className":38,"code":93431,"language":40,"meta":41,"style":41},"person = {\n    \"name\": \"Liam\",\n    \"age\": 18,\n    \"city\": \"Boston\"\n}\n\nprint(person[\"city\"])\n",[18,93433,93434,93442,93460,93474,93491,93495,93499],{"__ignoreMap":41},[45,93435,93436,93438,93440],{"class":47,"line":48},[45,93437,2966],{"class":51},[45,93439,56],{"class":55},[45,93441,41432],{"class":85},[45,93443,93444,93446,93448,93450,93452,93454,93456,93458],{"class":47,"line":63},[45,93445,41437],{"class":99},[45,93447,3096],{"class":103},[45,93449,100],{"class":99},[45,93451,457],{"class":85},[45,93453,1565],{"class":99},[45,93455,3182],{"class":103},[45,93457,100],{"class":99},[45,93459,41453],{"class":85},[45,93461,93462,93464,93466,93468,93470,93472],{"class":47,"line":70},[45,93463,41437],{"class":99},[45,93465,2082],{"class":103},[45,93467,100],{"class":99},[45,93469,457],{"class":85},[45,93471,121],{"class":59},[45,93473,41453],{"class":85},[45,93475,93476,93478,93480,93482,93484,93486,93489],{"class":47,"line":89},[45,93477,41437],{"class":99},[45,93479,10562],{"class":103},[45,93481,100],{"class":99},[45,93483,457],{"class":85},[45,93485,1565],{"class":99},[45,93487,93488],{"class":103},"Boston",[45,93490,1571],{"class":99},[45,93492,93493],{"class":47,"line":112},[45,93494,8629],{"class":85},[45,93496,93497],{"class":47,"line":126},[45,93498,67],{"emptyLinePlaceholder":66},[45,93500,93501,93503,93505,93507,93509,93511,93513,93515],{"class":47,"line":142},[45,93502,2065],{"class":92},[45,93504,96],{"class":85},[45,93506,2977],{"class":2070},[45,93508,9680],{"class":85},[45,93510,100],{"class":99},[45,93512,10562],{"class":103},[45,93514,100],{"class":99},[45,93516,21941],{"class":85},[14,93518,93519],{},"Use a dictionary because each value has a label.",[31,93521,1337],{"id":1336},[14,93523,93524],{},"Some common causes of confusion are:",[195,93526,93527,93530,93533,93536],{},[198,93528,93529],{},"Choosing a type based on what “looks right” instead of how the data will be used",[198,93531,93532],{},"Using a list for everything because it is the first collection beginners learn",[198,93534,93535],{},"Confusing ordered data with unique data",[198,93537,93538],{},"Not knowing that dictionaries are for named values",[14,93540,93541],{},"Here are some specific mistakes to avoid:",[195,93543,93544,93547,93550,93552],{},[198,93545,93546],{},"Using a set and expecting item order",[198,93548,93549],{},"Using a list when key-value data would be clearer as a dictionary",[198,93551,74324],{},[198,93553,93554],{},"Using a dictionary when only a simple sequence is needed",[14,93556,93557],{},"If you are unsure what kind of object you have while debugging, these checks can help:",[36,93559,93561],{"className":38,"code":93560,"language":40,"meta":41,"style":41},"data = {\"name\": \"Ana\", \"age\": 25}\nvalue = \"name\"\n\nprint(type(data))\nprint(data)\nprint(len(data))\nprint(\"key\" in data)\nprint(value in data)\n",[18,93562,93563,93599,93611,93615,93629,93639,93653,93672],{"__ignoreMap":41},[45,93564,93565,93567,93569,93571,93573,93575,93577,93579,93581,93583,93585,93587,93589,93591,93593,93595,93597],{"class":47,"line":48},[45,93566,30591],{"class":51},[45,93568,56],{"class":55},[45,93570,8604],{"class":85},[45,93572,100],{"class":99},[45,93574,3096],{"class":103},[45,93576,100],{"class":99},[45,93578,457],{"class":85},[45,93580,1565],{"class":99},[45,93582,10580],{"class":103},[45,93584,100],{"class":99},[45,93586,3079],{"class":85},[45,93588,1565],{"class":99},[45,93590,2082],{"class":103},[45,93592,100],{"class":99},[45,93594,457],{"class":85},[45,93596,303],{"class":59},[45,93598,8629],{"class":85},[45,93600,93601,93603,93605,93607,93609],{"class":47,"line":63},[45,93602,1476],{"class":51},[45,93604,56],{"class":55},[45,93606,1565],{"class":99},[45,93608,3096],{"class":103},[45,93610,1571],{"class":99},[45,93612,93613],{"class":47,"line":70},[45,93614,67],{"emptyLinePlaceholder":66},[45,93616,93617,93619,93621,93623,93625,93627],{"class":47,"line":89},[45,93618,2065],{"class":92},[45,93620,96],{"class":85},[45,93622,4340],{"class":2109},[45,93624,96],{"class":85},[45,93626,30614],{"class":2070},[45,93628,4348],{"class":85},[45,93630,93631,93633,93635,93637],{"class":47,"line":112},[45,93632,2065],{"class":92},[45,93634,96],{"class":85},[45,93636,30614],{"class":2070},[45,93638,109],{"class":85},[45,93640,93641,93643,93645,93647,93649,93651],{"class":47,"line":126},[45,93642,2065],{"class":92},[45,93644,96],{"class":85},[45,93646,28716],{"class":92},[45,93648,96],{"class":85},[45,93650,30614],{"class":2070},[45,93652,4348],{"class":85},[45,93654,93655,93657,93659,93661,93663,93665,93667,93670],{"class":47,"line":142},[45,93656,2065],{"class":92},[45,93658,96],{"class":85},[45,93660,100],{"class":99},[45,93662,9493],{"class":103},[45,93664,100],{"class":99},[45,93666,45650],{"class":73},[45,93668,93669],{"class":2070}," data",[45,93671,109],{"class":85},[45,93673,93674,93676,93678,93680,93682,93684],{"class":47,"line":149},[45,93675,2065],{"class":92},[45,93677,96],{"class":85},[45,93679,1476],{"class":2070},[45,93681,8623],{"class":73},[45,93683,93669],{"class":2070},[45,93685,109],{"class":85},[14,93687,93688],{},"These are useful for understanding:",[195,93690,93691,93694,93697,93699],{},[198,93692,93693],{},"what the object type is",[198,93695,93696],{},"what values it currently contains",[198,93698,74545],{},[198,93700,93701],{},"whether a key or value exists",[31,93703,93705],{"id":93704},"how-this-page-differs-from-the-type-specific-pages","How this page differs from the type-specific pages",[14,93707,93708,93709,240],{},"This page helps you ",[2421,93710,93711],{},"choose between collection types",[14,93713,93714],{},"The type-specific pages go deeper:",[195,93716,93717,93722,93727,93732],{},[198,93718,93719,93721],{},[236,93720,27366],{"href":27365}," covers list syntax and common list operations",[198,93723,93724,93726],{},[236,93725,92739],{"href":92738}," explains tuple syntax and how tuples are used",[198,93728,93729,93731],{},[236,93730,8557],{"href":30258}," covers keys, values, and common dictionary patterns",[198,93733,93734,93736],{},[236,93735,92951],{"href":92950}," explains uniqueness and set behavior",[14,93738,93739],{},"How-to pages solve one task, such as removing duplicates or checking whether a dictionary key exists.",[31,93741,2281],{"id":2280},[712,93743,93745],{"id":93744},"should-i-use-a-list-or-a-tuple","Should I use a list or a tuple?",[14,93747,93748],{},"Use a list if you will change the items. Use a tuple if the values should stay the same.",[712,93750,68171],{"id":68170},[14,93752,93753],{},"Use a set when you need unique values and do not care about indexing.",[712,93755,93757],{"id":93756},"why-use-a-dictionary-instead-of-a-list","Why use a dictionary instead of a list?",[14,93759,93760,93761,21,93763,15994,93765,240],{},"Use a dictionary when each value has a name, like ",[18,93762,3096],{},[18,93764,2082],{},[18,93766,20561],{},[712,93768,93770],{"id":93769},"can-a-tuple-contain-duplicate-values","Can a tuple contain duplicate values?",[14,93772,93773],{},"Yes. Tuples can contain duplicates, just like lists.",[712,93775,93777],{"id":93776},"can-i-store-different-data-types-in-these-collections","Can I store different data types in these collections?",[14,93779,93780],{},"Yes. Lists, tuples, sets, and dictionaries can all hold different data types.",[31,93782,2342],{"id":2341},[195,93784,93785,93789,93793,93797,93801,93806,93810,93814],{},[198,93786,93787],{},[236,93788,27366],{"href":27365},[198,93790,93791],{},[236,93792,92739],{"href":92738},[198,93794,93795],{},[236,93796,8557],{"href":30258},[198,93798,93799],{},[236,93800,92951],{"href":92950},[198,93802,93803],{},[236,93804,93805],{"href":21277},"Mutability in Python explained",[198,93807,93808],{},[236,93809,68211],{"href":66520},[198,93811,93812],{},[236,93813,10922],{"href":45737},[198,93815,93816],{},[236,93817,60883],{"href":60882},[2367,93819,93820],{},"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 .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 .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}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}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":93822},[93823,93824,93825,93826,93827,93828,93829,93830,93831,93837,93838,93839,93846],{"id":91898,"depth":63,"text":91899},{"id":92057,"depth":63,"text":92058},{"id":92075,"depth":63,"text":92076},{"id":92389,"depth":63,"text":92390},{"id":92576,"depth":63,"text":92577},{"id":92745,"depth":63,"text":92746},{"id":92954,"depth":63,"text":92955},{"id":93157,"depth":63,"text":93158},{"id":93240,"depth":63,"text":93241,"children":93832},[93833,93834,93835,93836],{"id":93247,"depth":70,"text":93248},{"id":93307,"depth":70,"text":93308},{"id":93360,"depth":70,"text":93361},{"id":93427,"depth":70,"text":93428},{"id":1336,"depth":63,"text":1337},{"id":93704,"depth":63,"text":93705},{"id":2280,"depth":63,"text":2281,"children":93840},[93841,93842,93843,93844,93845],{"id":93744,"depth":70,"text":93745},{"id":68170,"depth":70,"text":68171},{"id":93756,"depth":70,"text":93757},{"id":93769,"depth":70,"text":93770},{"id":93776,"depth":70,"text":93777},{"id":2341,"depth":63,"text":2342},"Master when to use lists vs tuples vs sets vs dictionaries in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fwhen-to-use-lists-vs-tuples-vs-sets-vs-dictionaries",{"title":91860,"description":93847},"learn\u002Fwhen-to-use-lists-vs-tuples-vs-sets-vs-dictionaries","rFeggN7IZJM08BKD31z-QABQPjuFdI00bWFnCV2yUfY",{"id":93854,"title":93855,"body":93856,"description":95041,"extension":2399,"meta":95042,"navigation":66,"path":95043,"seo":95044,"stem":95045,"__hash__":95046},"content\u002Flearn\u002Fwhy-learn-python-use-cases-and-benefits.md","Why Learn Python? Use Cases and Benefits",{"type":7,"value":93857,"toc":95016},[93858,93861,93864,93867,93871,93877,93880,93894,93901,93905,93908,93911,94041,94044,94048,94051,94177,94181,94187,94190,94192,94195,94198,94275,94279,94282,94367,94371,94377,94380,94389,94393,94396,94399,94474,94484,94488,94491,94494,94520,94523,94527,94645,94649,94655,94662,94666,94673,94767,94772,94778,94781,94787,94790,94794,94797,94800,94832,94835,94846,94849,94853,94856,94862,94876,94879,94895,94899,94902,94925,94928,94940,94942,94946,94949,94953,94956,94960,94963,94967,94970,94974,94977,94979,95013],[10,93859,93855],{"id":93860},"why-learn-python-use-cases-and-benefits",[14,93862,93863],{},"Python is one of the most popular programming languages in the world. For beginners, that matters because it usually means two things: it is practical, and it is easier to get help when you get stuck.",[14,93865,93866],{},"This page explains why people learn Python, what it is commonly used for, and whether it is a good first language for you. The goal is not to teach Python syntax yet. The goal is to help you decide if Python is worth learning.",[31,93868,93870],{"id":93869},"what-this-page-helps-the-reader-decide","What this page helps the reader decide",[14,93872,93873,93874],{},"This page is here to help you answer a simple question: ",[2421,93875,93876],{},"should I learn Python?",[14,93878,93879],{},"It will help you:",[195,93881,93882,93885,93888,93891],{},[198,93883,93884],{},"Decide whether Python is worth learning",[198,93886,93887],{},"See common real-world uses of Python",[198,93889,93890],{},"Understand why many beginners start with it",[198,93892,93893],{},"Choose Python with realistic expectations",[14,93895,93896,93897,93900],{},"If you are completely new, you may also want to read ",[236,93898,91118],{"href":93899},"\u002Flearn\u002Fwhat-is-python-a-beginner-friendly-introduction\u002F"," next.",[31,93902,93904],{"id":93903},"what-python-is-commonly-used-for","What Python is commonly used for",[14,93906,93907],{},"Python is a general-purpose language. That means it can be used for many different kinds of work.",[14,93909,93910],{},"Some of the most common uses are:",[195,93912,93913,93932,93951,93967,93983,93996,94012,94028],{},[198,93914,93915,93918],{},[2421,93916,93917],{},"Automation",[195,93919,93920,93923,93926,93929],{},[198,93921,93922],{},"Rename files",[198,93924,93925],{},"Move files between folders",[198,93927,93928],{},"Process many files at once",[198,93930,93931],{},"Save time on repeated tasks",[198,93933,93934,93937],{},[2421,93935,93936],{},"Data work",[195,93938,93939,93942,93945,93948],{},[198,93940,93941],{},"Read CSV files",[198,93943,93944],{},"Clean messy data",[198,93946,93947],{},"Summarize information",[198,93949,93950],{},"Generate reports",[198,93952,93953,93956],{},[2421,93954,93955],{},"Web development",[195,93957,93958,93961,93964],{},[198,93959,93960],{},"Build back-end logic",[198,93962,93963],{},"Create web applications",[198,93965,93966],{},"Handle forms, users, and databases",[198,93968,93969,93972],{},[2421,93970,93971],{},"APIs and web services",[195,93973,93974,93977,93980],{},[198,93975,93976],{},"Send requests to websites and services",[198,93978,93979],{},"Read JSON responses",[198,93981,93982],{},"Connect one app to another",[198,93984,93985,93988],{},[2421,93986,93987],{},"Scripting",[195,93989,93990,93993],{},[198,93991,93992],{},"Write small programs for one specific task",[198,93994,93995],{},"Turn manual work into a quick command",[198,93997,93998,94001],{},[2421,93999,94000],{},"Education",[195,94002,94003,94006,94009],{},[198,94004,94005],{},"Learn programming basics",[198,94007,94008],{},"Practice problem-solving",[198,94010,94011],{},"Build confidence with small projects",[198,94013,94014,94017],{},[2421,94015,94016],{},"Scientific and technical work",[195,94018,94019,94022,94025],{},[198,94020,94021],{},"Perform calculations",[198,94023,94024],{},"Analyze results",[198,94026,94027],{},"Build research tools",[198,94029,94030,94033],{},[2421,94031,94032],{},"AI and machine learning",[195,94034,94035,94038],{},[198,94036,94037],{},"Use popular libraries for data analysis and models",[198,94039,94040],{},"Experiment with prediction and automation tools",[14,94042,94043],{},"A big reason Python is popular is that it works for both small beginner scripts and larger real-world projects.",[712,94045,94047],{"id":94046},"example-a-small-automation-script","Example: a small automation script",[14,94049,94050],{},"Here is a very simple Python script that loops through file names and prints a new name for each one:",[36,94052,94054],{"className":38,"code":94053,"language":40,"meta":41,"style":41},"files = [\"photo1.jpg\", \"photo2.jpg\", \"photo3.jpg\"]\n\nfor index, filename in enumerate(files, start=1):\n    new_name = f\"image_{index}.jpg\"\n    print(f\"{filename} -> {new_name}\")\n",[18,94055,94056,94092,94096,94126,94147],{"__ignoreMap":41},[45,94057,94058,94061,94063,94065,94067,94070,94072,94074,94076,94079,94081,94083,94085,94088,94090],{"class":47,"line":48},[45,94059,94060],{"class":51},"files ",[45,94062,56],{"class":55},[45,94064,8575],{"class":85},[45,94066,100],{"class":99},[45,94068,94069],{"class":103},"photo1.jpg",[45,94071,100],{"class":99},[45,94073,3079],{"class":85},[45,94075,1565],{"class":99},[45,94077,94078],{"class":103},"photo2.jpg",[45,94080,100],{"class":99},[45,94082,3079],{"class":85},[45,94084,1565],{"class":99},[45,94086,94087],{"class":103},"photo3.jpg",[45,94089,100],{"class":99},[45,94091,8594],{"class":85},[45,94093,94094],{"class":47,"line":63},[45,94095,67],{"emptyLinePlaceholder":66},[45,94097,94098,94100,94102,94104,94107,94109,94111,94113,94115,94117,94120,94122,94124],{"class":47,"line":70},[45,94099,7895],{"class":73},[45,94101,61645],{"class":51},[45,94103,3079],{"class":85},[45,94105,94106],{"class":51}," filename ",[45,94108,8623],{"class":73},[45,94110,61654],{"class":92},[45,94112,96],{"class":85},[45,94114,46996],{"class":2070},[45,94116,3079],{"class":85},[45,94118,94119],{"class":6503}," start",[45,94121,56],{"class":55},[45,94123,8578],{"class":59},[45,94125,2464],{"class":85},[45,94127,94128,94131,94133,94135,94138,94140,94142,94144],{"class":47,"line":89},[45,94129,94130],{"class":51},"    new_name ",[45,94132,56],{"class":55},[45,94134,15469],{"class":2440},[45,94136,94137],{"class":103},"\"image_",[45,94139,5551],{"class":59},[45,94141,60449],{"class":51},[45,94143,5556],{"class":59},[45,94145,94146],{"class":103},".jpg\"\n",[45,94148,94149,94151,94153,94155,94157,94159,94162,94164,94167,94169,94171,94173,94175],{"class":47,"line":112},[45,94150,93],{"class":92},[45,94152,96],{"class":85},[45,94154,5545],{"class":2440},[45,94156,100],{"class":103},[45,94158,5551],{"class":59},[45,94160,94161],{"class":2070},"filename",[45,94163,5556],{"class":59},[45,94165,94166],{"class":103}," -> ",[45,94168,5551],{"class":59},[45,94170,29533],{"class":2070},[45,94172,5556],{"class":59},[45,94174,100],{"class":103},[45,94176,109],{"class":85},[14,94178,94179],{},[2421,94180,177],{},[36,94182,94185],{"className":94183,"code":94184,"language":5000,"meta":41},[4998],"photo1.jpg -> image_1.jpg\nphoto2.jpg -> image_2.jpg\nphoto3.jpg -> image_3.jpg\n",[18,94186,94184],{"__ignoreMap":41},[14,94188,94189],{},"This does not rename real files yet, but it shows the kind of task Python is good at: simple scripts that save time.",[31,94191,91228],{"id":91227},[14,94193,94194],{},"Many people choose Python as a first language because it is easier to read than many alternatives.",[14,94196,94197],{},"Here are some of the main reasons:",[195,94199,94200,94213,94226,94239,94252,94265],{},[198,94201,94202,94205],{},[2421,94203,94204],{},"Readable syntax",[195,94206,94207,94210],{},[198,94208,94209],{},"Python code often looks closer to plain English",[198,94211,94212],{},"Beginners can focus more on what the program does",[198,94214,94215,94218],{},[2421,94216,94217],{},"Less boilerplate",[195,94219,94220,94223],{},[198,94221,94222],{},"You usually write fewer lines to do basic tasks",[198,94224,94225],{},"There is less setup before you can start practicing",[198,94227,94228,94231],{},[2421,94229,94230],{},"Fast feedback",[195,94232,94233,94236],{},[198,94234,94235],{},"You can run a small script right away",[198,94237,94238],{},"This makes learning feel more interactive",[198,94240,94241,94244],{},[2421,94242,94243],{},"Lots of tutorials and help",[195,94245,94246,94249],{},[198,94247,94248],{},"Python has a large learning community",[198,94250,94251],{},"There are many beginner examples, guides, and forums",[198,94253,94254,94257],{},[2421,94255,94256],{},"Useful standard library",[195,94258,94259,94262],{},[198,94260,94261],{},"Python includes tools for files, text, math, dates, and more",[198,94263,94264],{},"You can do many common tasks without installing extra packages",[198,94266,94267,94270],{},[2421,94268,94269],{},"Good for both practice and real work",[195,94271,94272],{},[198,94273,94274],{},"The same language you use for learning can also be used in real projects",[712,94276,94278],{"id":94277},"example-a-simple-useful-script","Example: a simple useful script",[14,94280,94281],{},"This Python code reads a list of numbers and prints the total:",[36,94283,94285],{"className":38,"code":94284,"language":40,"meta":41,"style":41},"numbers = [5, 10, 15, 20]\ntotal = sum(numbers)\n\nprint(\"Numbers:\", numbers)\nprint(\"Total:\", total)\n",[18,94286,94287,94311,94326,94330,94349],{"__ignoreMap":41},[45,94288,94289,94291,94293,94295,94297,94299,94301,94303,94305,94307,94309],{"class":47,"line":48},[45,94290,8570],{"class":51},[45,94292,56],{"class":55},[45,94294,8575],{"class":85},[45,94296,3749],{"class":59},[45,94298,3079],{"class":85},[45,94300,1190],{"class":59},[45,94302,3079],{"class":85},[45,94304,35890],{"class":59},[45,94306,3079],{"class":85},[45,94308,10503],{"class":59},[45,94310,8594],{"class":85},[45,94312,94313,94315,94317,94320,94322,94324],{"class":47,"line":63},[45,94314,39569],{"class":51},[45,94316,56],{"class":55},[45,94318,94319],{"class":92}," sum",[45,94321,96],{"class":85},[45,94323,9069],{"class":2070},[45,94325,109],{"class":85},[45,94327,94328],{"class":47,"line":70},[45,94329,67],{"emptyLinePlaceholder":66},[45,94331,94332,94334,94336,94338,94341,94343,94345,94347],{"class":47,"line":89},[45,94333,2065],{"class":92},[45,94335,96],{"class":85},[45,94337,100],{"class":99},[45,94339,94340],{"class":103},"Numbers:",[45,94342,100],{"class":99},[45,94344,3079],{"class":85},[45,94346,8626],{"class":2070},[45,94348,109],{"class":85},[45,94350,94351,94353,94355,94357,94359,94361,94363,94365],{"class":47,"line":112},[45,94352,2065],{"class":92},[45,94354,96],{"class":85},[45,94356,100],{"class":99},[45,94358,90443],{"class":103},[45,94360,100],{"class":99},[45,94362,3079],{"class":85},[45,94364,90450],{"class":2070},[45,94366,109],{"class":85},[14,94368,94369],{},[2421,94370,177],{},[36,94372,94375],{"className":94373,"code":94374,"language":5000,"meta":41},[4998],"Numbers: [5, 10, 15, 20]\nTotal: 50\n",[18,94376,94374],{"__ignoreMap":41},[14,94378,94379],{},"This is a small example, but it shows an important point: even beginner-level Python can do useful work.",[14,94381,94382,94383,94386,94387,240],{},"If you want to start writing code soon, the next practical step is to ",[236,94384,94385],{"href":17495},"install Python on Windows, macOS, and Linux"," and then learn ",[236,94388,17174],{"href":17173},[31,94390,94392],{"id":94391},"main-benefits-of-learning-python","Main benefits of learning Python",[14,94394,94395],{},"Learning Python gives you more than just one language. It also helps you build core programming skills.",[14,94397,94398],{},"Main benefits include:",[195,94400,94401,94411,94421,94434,94454,94464],{},[198,94402,94403,94406],{},[2421,94404,94405],{},"It is a good first step into programming",[195,94407,94408],{},[198,94409,94410],{},"You can learn the basics without fighting too much syntax",[198,94412,94413,94416],{},[2421,94414,94415],{},"You can do real tasks early",[195,94417,94418],{},[198,94419,94420],{},"Even simple scripts can save time and solve problems",[198,94422,94423,94426],{},[2421,94424,94425],{},"It is useful in many fields",[195,94427,94428,94431],{},[198,94429,94430],{},"Python is not only for software developers",[198,94432,94433],{},"It is also used in business, science, education, research, and automation",[198,94435,94436,94439],{},[2421,94437,94438],{},"It teaches core programming ideas",[195,94440,94441,94443,94446,94448,94451],{},[198,94442,62429],{},[198,94444,94445],{},"Loops",[198,94447,62432],{},[198,94449,94450],{},"Working with files",[198,94452,94453],{},"Basic problem-solving",[198,94455,94456,94459],{},[2421,94457,94458],{},"It is widely used",[195,94460,94461],{},[198,94462,94463],{},"Python appears in schools, online courses, and technical jobs",[198,94465,94466,94469],{},[2421,94467,94468],{},"The skills transfer",[195,94470,94471],{},[198,94472,94473],{},"After learning Python, it is easier to learn other languages later",[14,94475,94476,94477,94480,94481,240],{},"If you continue, you will eventually learn basics such as ",[236,94478,94479],{"href":39615},"Python syntax"," and your ",[236,94482,94483],{"href":17216},"first Python program with Hello World",[31,94485,94487],{"id":94486},"what-beginners-can-build-with-python","What beginners can build with Python",[14,94489,94490],{},"One reason Python is motivating is that beginners can build small projects fairly early.",[14,94492,94493],{},"Common beginner projects include:",[195,94495,94496,94499,94502,94505,94508,94511,94514,94517],{},[198,94497,94498],{},"Simple calculator",[198,94500,94501],{},"Number guessing game",[198,94503,94504],{},"To-do list script",[198,94506,94507],{},"CSV reader",[198,94509,94510],{},"File organizer",[198,94512,94513],{},"Basic API request script",[198,94515,94516],{},"Command line tools",[198,94518,94519],{},"Small data cleaning scripts",[14,94521,94522],{},"These projects are useful because they help you practice basic programming ideas while making something real.",[712,94524,94526],{"id":94525},"example-a-beginner-calculator","Example: a beginner calculator",[36,94528,94530],{"className":38,"code":94529,"language":40,"meta":41,"style":41},"a = 8\nb = 4\n\nprint(\"Addition:\", a + b)\nprint(\"Subtraction:\", a - b)\nprint(\"Multiplication:\", a * b)\nprint(\"Division:\", a \u002F b)\n",[18,94531,94532,94541,94549,94553,94576,94599,94622],{"__ignoreMap":41},[45,94533,94534,94536,94538],{"class":47,"line":48},[45,94535,11541],{"class":51},[45,94537,56],{"class":55},[45,94539,94540],{"class":59}," 8\n",[45,94542,94543,94545,94547],{"class":47,"line":63},[45,94544,19315],{"class":51},[45,94546,56],{"class":55},[45,94548,21309],{"class":59},[45,94550,94551],{"class":47,"line":70},[45,94552,67],{"emptyLinePlaceholder":66},[45,94554,94555,94557,94559,94561,94564,94566,94568,94570,94572,94574],{"class":47,"line":89},[45,94556,2065],{"class":92},[45,94558,96],{"class":85},[45,94560,100],{"class":99},[45,94562,94563],{"class":103},"Addition:",[45,94565,100],{"class":99},[45,94567,3079],{"class":85},[45,94569,11124],{"class":2070},[45,94571,8850],{"class":55},[45,94573,11115],{"class":2070},[45,94575,109],{"class":85},[45,94577,94578,94580,94582,94584,94587,94589,94591,94593,94595,94597],{"class":47,"line":112},[45,94579,2065],{"class":92},[45,94581,96],{"class":85},[45,94583,100],{"class":99},[45,94585,94586],{"class":103},"Subtraction:",[45,94588,100],{"class":99},[45,94590,3079],{"class":85},[45,94592,11124],{"class":2070},[45,94594,218],{"class":55},[45,94596,11115],{"class":2070},[45,94598,109],{"class":85},[45,94600,94601,94603,94605,94607,94610,94612,94614,94616,94618,94620],{"class":47,"line":126},[45,94602,2065],{"class":92},[45,94604,96],{"class":85},[45,94606,100],{"class":99},[45,94608,94609],{"class":103},"Multiplication:",[45,94611,100],{"class":99},[45,94613,3079],{"class":85},[45,94615,11124],{"class":2070},[45,94617,5601],{"class":55},[45,94619,11115],{"class":2070},[45,94621,109],{"class":85},[45,94623,94624,94626,94628,94630,94633,94635,94637,94639,94641,94643],{"class":47,"line":142},[45,94625,2065],{"class":92},[45,94627,96],{"class":85},[45,94629,100],{"class":99},[45,94631,94632],{"class":103},"Division:",[45,94634,100],{"class":99},[45,94636,3079],{"class":85},[45,94638,11124],{"class":2070},[45,94640,63892],{"class":55},[45,94642,11115],{"class":2070},[45,94644,109],{"class":85},[14,94646,94647],{},[2421,94648,177],{},[36,94650,94653],{"className":94651,"code":94652,"language":5000,"meta":41},[4998],"Addition: 12\nSubtraction: 4\nMultiplication: 32\nDivision: 2.0\n",[18,94654,94652],{"__ignoreMap":41},[14,94656,94657,94658,240],{},"This is the kind of project many beginners start with. If you want a full walkthrough, see the ",[236,94659,94661],{"href":94660},"\u002Fexamples\u002Fpython-simple-calculator-example\u002F","Python simple calculator example",[712,94663,94665],{"id":94664},"example-reading-a-csv-file","Example: reading a CSV file",[14,94667,94668,94669,94672],{},"Python is also useful for basic data work. Here is a small example using the built-in ",[18,94670,94671],{},"csv"," module:",[36,94674,94676],{"className":38,"code":94675,"language":40,"meta":41,"style":41},"import csv\n\nwith open(\"people.csv\", newline=\"\") as file:\n    reader = csv.reader(file)\n    for row in reader:\n        print(row)\n",[18,94677,94678,94685,94689,94721,94742,94756],{"__ignoreMap":41},[45,94679,94680,94682],{"class":47,"line":48},[45,94681,14870],{"class":73},[45,94683,94684],{"class":51}," csv\n",[45,94686,94687],{"class":47,"line":63},[45,94688,67],{"emptyLinePlaceholder":66},[45,94690,94691,94693,94695,94697,94699,94702,94704,94706,94709,94711,94713,94715,94717,94719],{"class":47,"line":70},[45,94692,23989],{"class":73},[45,94694,23992],{"class":92},[45,94696,96],{"class":85},[45,94698,100],{"class":99},[45,94700,94701],{"class":103},"people.csv",[45,94703,100],{"class":99},[45,94705,3079],{"class":85},[45,94707,94708],{"class":6503}," newline",[45,94710,56],{"class":55},[45,94712,85927],{"class":99},[45,94714,9146],{"class":85},[45,94716,24015],{"class":73},[45,94718,15011],{"class":24018},[45,94720,86],{"class":85},[45,94722,94723,94726,94728,94731,94733,94736,94738,94740],{"class":47,"line":89},[45,94724,94725],{"class":51},"    reader ",[45,94727,56],{"class":55},[45,94729,94730],{"class":51}," csv",[45,94732,240],{"class":85},[45,94734,94735],{"class":2070},"reader",[45,94737,96],{"class":85},[45,94739,46818],{"class":24018},[45,94741,109],{"class":85},[45,94743,94744,94746,94749,94751,94754],{"class":47,"line":112},[45,94745,14165],{"class":73},[45,94747,94748],{"class":51}," row ",[45,94750,8623],{"class":73},[45,94752,94753],{"class":51}," reader",[45,94755,86],{"class":85},[45,94757,94758,94760,94762,94765],{"class":47,"line":126},[45,94759,1923],{"class":92},[45,94761,96],{"class":85},[45,94763,94764],{"class":2070},"row",[45,94766,109],{"class":85},[14,94768,3259,94769,94771],{},[18,94770,94701],{}," contains:",[36,94773,94776],{"className":94774,"code":94775,"language":5000,"meta":41},[4998],"name,age\nAna,25\nSam,31\n",[18,94777,94775],{"__ignoreMap":41},[14,94779,94780],{},"The output will be:",[36,94782,94785],{"className":94783,"code":94784,"language":5000,"meta":41},[4998],"['name', 'age']\n['Ana', '25']\n['Sam', '31']\n",[18,94786,94784],{"__ignoreMap":41},[14,94788,94789],{},"This is a good example of how Python can help with practical tasks, even at a beginner level.",[31,94791,94793],{"id":94792},"when-python-may-not-be-the-best-first-choice","When Python may not be the best first choice",[14,94795,94796],{},"Python is a strong first language, but it is not the best choice for every goal.",[14,94798,94799],{},"You may want to start somewhere else if:",[195,94801,94802,94812,94822],{},[198,94803,94804,94807],{},[2421,94805,94806],{},"Your goal is only front-end web design",[195,94808,94809],{},[198,94810,94811],{},"HTML, CSS, and JavaScript are more direct for browser-based pages",[198,94813,94814,94817],{},[2421,94815,94816],{},"Your goal is mobile app development",[195,94818,94819],{},[198,94820,94821],{},"Other languages and tools may be more direct for Android or iPhone apps",[198,94823,94824,94827],{},[2421,94825,94826],{},"Your goal is high-performance game engines",[195,94828,94829],{},[198,94830,94831],{},"Python is usually not the main language for that kind of work",[14,94833,94834],{},"The important idea is this:",[195,94836,94837,94840,94843],{},[198,94838,94839],{},"Python is flexible",[198,94841,94842],{},"Python is useful",[198,94844,94845],{},"But no language is the best tool for every job",[14,94847,94848],{},"If your goal is general programming, automation, learning core concepts, or working with data, Python is still a very strong choice.",[31,94850,94852],{"id":94851},"what-this-page-should-not-cover","What this page should not cover",[14,94854,94855],{},"This page is about motivation, uses, and benefits. It is not meant to teach the full language.",[14,94857,94858,94859,94861],{},"So it does ",[2421,94860,2857],{}," cover:",[195,94863,94864,94867,94870,94873],{},[198,94865,94866],{},"Detailed installation steps",[198,94868,94869],{},"Deep syntax lessons",[198,94871,94872],{},"Full explanations of variables, loops, or functions",[198,94874,94875],{},"In-depth comparisons with every other language",[14,94877,94878],{},"If you are ready for the next step, start with:",[346,94880,94881,94886,94890],{},[198,94882,94883],{},[236,94884,94885],{"href":17495},"Install Python on Windows, macOS, and Linux",[198,94887,94888],{},[236,94889,17211],{"href":17173},[198,94891,94892],{},[236,94893,94894],{"href":17216},"Write your first Python program: Hello World",[31,94896,94898],{"id":94897},"suggested-beginner-learning-path-after-this-page","Suggested beginner learning path after this page",[14,94900,94901],{},"A simple learning path is:",[346,94903,94904,94907,94910,94913,94919,94922],{},[198,94905,94906],{},"Learn what Python is",[198,94908,94909],{},"Install Python",[198,94911,94912],{},"Run a simple program",[198,94914,94915,94916],{},"Write ",[18,94917,94918],{},"Hello World",[198,94920,94921],{},"Learn syntax basics, variables, data types, and control flow",[198,94923,94924],{},"Build small practical scripts",[14,94926,94927],{},"Try to keep your first projects small. Short scripts are easier to finish, easier to understand, and better for learning.",[14,94929,94930,94931,94935,94936,240],{},"A good next step after installation is to study a simple example like the ",[236,94932,94934],{"href":94933},"\u002Fexamples\u002Fpython-hello-world-example-explained\u002F","Python Hello World example explained",". After that, try a practical project such as a ",[236,94937,94939],{"href":94938},"\u002Fexamples\u002Fpython-file-organizer-script-example\u002F","Python file organizer script example",[31,94941,2281],{"id":2280},[712,94943,94945],{"id":94944},"is-python-a-good-first-programming-language","Is Python a good first programming language?",[14,94947,94948],{},"Yes. Python is widely considered beginner-friendly because its syntax is readable and it lets you build useful programs early.",[712,94950,94952],{"id":94951},"what-can-i-do-with-python-as-a-beginner","What can I do with Python as a beginner?",[14,94954,94955],{},"You can automate repetitive tasks, work with files, build simple scripts, read data, and create small projects like games or tools.",[712,94957,94959],{"id":94958},"is-python-only-for-data-science","Is Python only for data science?",[14,94961,94962],{},"No. Python is also used for automation, web development, scripting, APIs, education, and general programming.",[712,94964,94966],{"id":94965},"can-learning-python-help-me-learn-other-languages-later","Can learning Python help me learn other languages later?",[14,94968,94969],{},"Yes. Python teaches core programming ideas that transfer to many other languages.",[712,94971,94973],{"id":94972},"should-i-learn-python-or-javascript-first","Should I learn Python or JavaScript first?",[14,94975,94976],{},"It depends on your goal. Python is great for general programming and automation. JavaScript is the direct choice for browser-based front-end web work.",[31,94978,2342],{"id":2341},[195,94980,94981,94985,94989,94994,94999,95003,95008],{},[198,94982,94983],{},[236,94984,91118],{"href":93899},[198,94986,94987],{},[236,94988,16522],{"href":17495},[198,94990,94991],{},[236,94992,94993],{"href":17173},"How to Run Python Code: Command Line and IDEs",[198,94995,94996],{},[236,94997,94998],{"href":17216},"Your First Python Program: Hello World Explained",[198,95000,95001],{},[236,95002,70567],{"href":39615},[198,95004,95005],{},[236,95006,95007],{"href":94660},"Python Simple Calculator Example",[198,95009,95010],{},[236,95011,95012],{"href":94938},"Python File Organizer Script Example",[2367,95014,95015],{},"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 .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 .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 .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 .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}",{"title":41,"searchDepth":63,"depth":63,"links":95017},[95018,95019,95022,95025,95026,95030,95031,95032,95033,95040],{"id":93869,"depth":63,"text":93870},{"id":93903,"depth":63,"text":93904,"children":95020},[95021],{"id":94046,"depth":70,"text":94047},{"id":91227,"depth":63,"text":91228,"children":95023},[95024],{"id":94277,"depth":70,"text":94278},{"id":94391,"depth":63,"text":94392},{"id":94486,"depth":63,"text":94487,"children":95027},[95028,95029],{"id":94525,"depth":70,"text":94526},{"id":94664,"depth":70,"text":94665},{"id":94792,"depth":63,"text":94793},{"id":94851,"depth":63,"text":94852},{"id":94897,"depth":63,"text":94898},{"id":2280,"depth":63,"text":2281,"children":95034},[95035,95036,95037,95038,95039],{"id":94944,"depth":70,"text":94945},{"id":94951,"depth":70,"text":94952},{"id":94958,"depth":70,"text":94959},{"id":94965,"depth":70,"text":94966},{"id":94972,"depth":70,"text":94973},{"id":2341,"depth":63,"text":2342},"Master why learn python use cases and benefits in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fwhy-learn-python-use-cases-and-benefits",{"title":93855,"description":95041},"learn\u002Fwhy-learn-python-use-cases-and-benefits","vTLH58AFT6uYl2ZZp6x89f1ERu3maSiRYNhYYUvPPdc",{"id":95048,"title":95049,"body":95050,"description":96521,"extension":2399,"meta":96522,"navigation":66,"path":96523,"seo":96524,"stem":96525,"__hash__":96526},"content\u002Flearn\u002Fworking-with-file-paths-in-python.md","Working with File Paths in Python",{"type":7,"value":95051,"toc":96494},[95052,95055,95058,95061,95063,95136,95142,95144,95167,95169,95189,95192,95195,95197,95217,95220,95237,95246,95304,95310,95314,95317,95321,95324,95326,95340,95343,95347,95350,95352,95368,95371,95375,95407,95411,95414,95417,95419,95454,95457,95460,95519,95525,95532,95536,95541,95544,95561,95564,95620,95623,95627,95630,95693,95696,95701,95755,95758,95830,95833,95837,95840,95844,95903,95907,95967,95971,96023,96026,96033,96037,96043,96051,96140,96145,96204,96207,96209,96212,96229,96232,96261,96299,96348,96373,96410,96416,96418,96422,96427,96436,96440,96443,96447,96455,96459,96462,96464,96488,96491],[10,95053,95049],{"id":95054},"working-with-file-paths-in-python",[14,95056,95057],{},"A file path tells Python where a file or folder is located.",[14,95059,95060],{},"When you read, write, rename, or delete files, Python needs a path. This page shows what file paths are, how relative and absolute paths work, and the safest beginner-friendly way to build paths in Python.",[31,95062,34],{"id":33},[36,95064,95066],{"className":38,"code":95065,"language":40,"meta":41,"style":41},"from pathlib import Path\n\nfile_path = Path(\"data\") \u002F \"notes.txt\"\nprint(file_path)\nprint(file_path.exists())\n",[18,95067,95068,95080,95084,95112,95122],{"__ignoreMap":41},[45,95069,95070,95072,95075,95077],{"class":47,"line":48},[45,95071,14921],{"class":73},[45,95073,95074],{"class":51}," pathlib ",[45,95076,14870],{"class":73},[45,95078,95079],{"class":51}," Path\n",[45,95081,95082],{"class":47,"line":63},[45,95083,67],{"emptyLinePlaceholder":66},[45,95085,95086,95089,95091,95094,95096,95098,95100,95102,95104,95106,95108,95110],{"class":47,"line":70},[45,95087,95088],{"class":51},"file_path ",[45,95090,56],{"class":55},[45,95092,95093],{"class":2070}," Path",[45,95095,96],{"class":85},[45,95097,100],{"class":99},[45,95099,30614],{"class":103},[45,95101,100],{"class":99},[45,95103,9146],{"class":85},[45,95105,45881],{"class":55},[45,95107,1565],{"class":99},[45,95109,47879],{"class":103},[45,95111,1571],{"class":99},[45,95113,95114,95116,95118,95120],{"class":47,"line":89},[45,95115,2065],{"class":92},[45,95117,96],{"class":85},[45,95119,19988],{"class":2070},[45,95121,109],{"class":85},[45,95123,95124,95126,95128,95130,95132,95134],{"class":47,"line":112},[45,95125,2065],{"class":92},[45,95127,96],{"class":85},[45,95129,19988],{"class":2070},[45,95131,240],{"class":85},[45,95133,49087],{"class":2070},[45,95135,10777],{"class":85},[14,95137,165,95138,95141],{},[18,95139,95140],{},"pathlib.Path"," to build paths safely instead of typing slashes by hand.",[14,95143,13456],{},[36,95145,95147],{"className":38,"code":95146,"language":40,"meta":41,"style":41},"data\u002Fnotes.txt\nFalse\n",[18,95148,95149,95163],{"__ignoreMap":41},[45,95150,95151,95153,95155,95158,95160],{"class":47,"line":48},[45,95152,30614],{"class":51},[45,95154,63892],{"class":55},[45,95156,95157],{"class":51},"notes",[45,95159,240],{"class":85},[45,95161,95162],{"class":3095},"txt\n",[45,95164,95165],{"class":47,"line":63},[45,95166,2265],{"class":481},[31,95168,2530],{"id":2529},[195,95170,95171,95174,95177,95180,95186],{},[198,95172,95173],{},"What a file path is",[198,95175,95176],{},"The difference between absolute and relative paths",[198,95178,95179],{},"How the current working directory affects file paths",[198,95181,95182,95183],{},"How to build paths safely with ",[18,95184,95185],{},"pathlib",[198,95187,95188],{},"How to check whether a path exists before opening a file",[31,95190,95173],{"id":95191},"what-a-file-path-is",[14,95193,95194],{},"A file path is the location of a file or folder.",[14,95196,40802],{},[195,95198,95199,95205,95211],{},[198,95200,95201,95202],{},"A file path: ",[18,95203,95204],{},"report.txt",[198,95206,95207,95208],{},"A file path inside a folder: ",[18,95209,95210],{},"data\u002Freport.txt",[198,95212,95213,95214],{},"A folder path: ",[18,95215,95216],{},"documents",[14,95218,95219],{},"Python uses paths in tasks like:",[195,95221,95222,95225,95228,95231,95234],{},[198,95223,95224],{},"Opening a file",[198,95226,95227],{},"Saving a file",[198,95229,95230],{},"Checking whether a file exists",[198,95232,95233],{},"Creating folders",[198,95235,95236],{},"Deleting files",[14,95238,95239,95240,95245],{},"For example, when you use ",[236,95241,95243],{"href":95242},"\u002Freference\u002Fpython-open-function-explained",[18,95244,47592],{},", the path tells Python which file to open.",[36,95247,95248],{"className":38,"code":47943,"language":40,"meta":41,"style":41},[18,95249,95250,95280,95294],{"__ignoreMap":41},[45,95251,95252,95254,95256,95258,95260,95262,95264,95266,95268,95270,95272,95274,95276,95278],{"class":47,"line":48},[45,95253,23989],{"class":73},[45,95255,23992],{"class":92},[45,95257,96],{"class":85},[45,95259,100],{"class":99},[45,95261,47879],{"class":103},[45,95263,100],{"class":99},[45,95265,3079],{"class":85},[45,95267,1565],{"class":99},[45,95269,24008],{"class":103},[45,95271,100],{"class":99},[45,95273,9146],{"class":85},[45,95275,24015],{"class":73},[45,95277,15011],{"class":24018},[45,95279,86],{"class":85},[45,95281,95282,95284,95286,95288,95290,95292],{"class":47,"line":63},[45,95283,47704],{"class":51},[45,95285,56],{"class":55},[45,95287,15011],{"class":24018},[45,95289,240],{"class":85},[45,95291,46823],{"class":2070},[45,95293,2495],{"class":85},[45,95295,95296,95298,95300,95302],{"class":47,"line":70},[45,95297,93],{"class":92},[45,95299,96],{"class":85},[45,95301,47723],{"class":2070},[45,95303,109],{"class":85},[14,95305,36629,95306,95309],{},[18,95307,95308],{},"\"notes.txt\""," is the file path.",[31,95311,95313],{"id":95312},"absolute-vs-relative-paths","Absolute vs relative paths",[14,95315,95316],{},"There are two common kinds of file paths.",[712,95318,95320],{"id":95319},"absolute-paths","Absolute paths",[14,95322,95323],{},"An absolute path starts from the full system location.",[14,95325,40802],{},[195,95327,95328,95334],{},[198,95329,95330,95331],{},"Windows: ",[18,95332,95333],{},"C:\\Users\\Sam\\Documents\\notes.txt",[198,95335,95336,95337],{},"macOS\u002FLinux: ",[18,95338,95339],{},"\u002Fhome\u002Fsam\u002Fdocuments\u002Fnotes.txt",[14,95341,95342],{},"An absolute path does not depend on where your script is running.",[712,95344,95346],{"id":95345},"relative-paths","Relative paths",[14,95348,95349],{},"A relative path starts from the current working directory.",[14,95351,40802],{},[195,95353,95354,95358,95363],{},[198,95355,95356],{},[18,95357,47879],{},[198,95359,95360],{},[18,95361,95362],{},"data\u002Fnotes.txt",[198,95364,95365],{},[18,95366,95367],{},"..\u002Fnotes.txt",[14,95369,95370],{},"Relative paths are shorter and often easier to move between computers. But they only work if Python starts in the folder you expect.",[712,95372,95374],{"id":95373},"quick-comparison","Quick comparison",[195,95376,95377,95392],{},[198,95378,95379,95381],{},[2421,95380,95320],{},[195,95382,95383,95386,95389],{},[198,95384,95385],{},"Clearer for debugging",[198,95387,95388],{},"Do not depend on the working directory",[198,95390,95391],{},"Less portable between computers",[198,95393,95394,95396],{},[2421,95395,95346],{},[195,95397,95398,95401,95404],{},[198,95399,95400],{},"Shorter and cleaner",[198,95402,95403],{},"Better for projects you move or share",[198,95405,95406],{},"Depend on the current working directory",[31,95408,95410],{"id":95409},"the-current-working-directory","The current working directory",[14,95412,95413],{},"The current working directory is the folder Python is using as its starting point.",[14,95415,95416],{},"When you use a relative path, Python looks for the file from that folder.",[14,95418,2115],{},[36,95420,95422],{"className":38,"code":95421,"language":40,"meta":41,"style":41},"from pathlib import Path\n\nprint(Path.cwd())\n",[18,95423,95424,95434,95438],{"__ignoreMap":41},[45,95425,95426,95428,95430,95432],{"class":47,"line":48},[45,95427,14921],{"class":73},[45,95429,95074],{"class":51},[45,95431,14870],{"class":73},[45,95433,95079],{"class":51},[45,95435,95436],{"class":47,"line":63},[45,95437,67],{"emptyLinePlaceholder":66},[45,95439,95440,95442,95444,95447,95449,95452],{"class":47,"line":70},[45,95441,2065],{"class":92},[45,95443,96],{"class":85},[45,95445,95446],{"class":2070},"Path",[45,95448,240],{"class":85},[45,95450,95451],{"class":2070},"cwd",[45,95453,10777],{"class":85},[14,95455,95456],{},"This prints the current working directory.",[14,95458,95459],{},"If your code uses this path:",[36,95461,95463],{"className":38,"code":95462,"language":40,"meta":41,"style":41},"from pathlib import Path\n\nfile_path = Path(\"data\") \u002F \"notes.txt\"\nprint(file_path.exists())\n",[18,95464,95465,95475,95479,95505],{"__ignoreMap":41},[45,95466,95467,95469,95471,95473],{"class":47,"line":48},[45,95468,14921],{"class":73},[45,95470,95074],{"class":51},[45,95472,14870],{"class":73},[45,95474,95079],{"class":51},[45,95476,95477],{"class":47,"line":63},[45,95478,67],{"emptyLinePlaceholder":66},[45,95480,95481,95483,95485,95487,95489,95491,95493,95495,95497,95499,95501,95503],{"class":47,"line":70},[45,95482,95088],{"class":51},[45,95484,56],{"class":55},[45,95486,95093],{"class":2070},[45,95488,96],{"class":85},[45,95490,100],{"class":99},[45,95492,30614],{"class":103},[45,95494,100],{"class":99},[45,95496,9146],{"class":85},[45,95498,45881],{"class":55},[45,95500,1565],{"class":99},[45,95502,47879],{"class":103},[45,95504,1571],{"class":99},[45,95506,95507,95509,95511,95513,95515,95517],{"class":47,"line":89},[45,95508,2065],{"class":92},[45,95510,96],{"class":85},[45,95512,19988],{"class":2070},[45,95514,240],{"class":85},[45,95516,49087],{"class":2070},[45,95518,10777],{"class":85},[14,95520,95521,95522,95524],{},"Python looks for ",[18,95523,95362],{}," inside the current working directory.",[14,95526,95527,95528,240],{},"If the working directory is not what you expected, Python may not find the file. This is one of the most common reasons for ",[236,95529,95530],{"href":46830},[18,95531,46770],{},[31,95533,95535],{"id":95534},"why-pathlib-is-the-best-starting-point","Why pathlib is the best starting point",[14,95537,95538,95540],{},[18,95539,95185],{}," is the best place for beginners to start working with paths.",[14,95542,95543],{},"Why it helps:",[195,95545,95546,95549,95552,95555],{},[198,95547,95548],{},"It is part of Python’s standard library",[198,95550,95551],{},"It is easier to read than older path code",[198,95553,95554],{},"It works across Windows, macOS, and Linux",[198,95556,95557,95558,95560],{},"It lets you join path parts with the ",[18,95559,63892],{}," operator",[14,95562,95563],{},"Basic example:",[36,95565,95567],{"className":38,"code":95566,"language":40,"meta":41,"style":41},"from pathlib import Path\n\npath = Path(\"data\") \u002F \"notes.txt\"\nprint(path)\n",[18,95568,95569,95579,95583,95610],{"__ignoreMap":41},[45,95570,95571,95573,95575,95577],{"class":47,"line":48},[45,95572,14921],{"class":73},[45,95574,95074],{"class":51},[45,95576,14870],{"class":73},[45,95578,95079],{"class":51},[45,95580,95581],{"class":47,"line":63},[45,95582,67],{"emptyLinePlaceholder":66},[45,95584,95585,95588,95590,95592,95594,95596,95598,95600,95602,95604,95606,95608],{"class":47,"line":70},[45,95586,95587],{"class":51},"path ",[45,95589,56],{"class":55},[45,95591,95093],{"class":2070},[45,95593,96],{"class":85},[45,95595,100],{"class":99},[45,95597,30614],{"class":103},[45,95599,100],{"class":99},[45,95601,9146],{"class":85},[45,95603,45881],{"class":55},[45,95605,1565],{"class":99},[45,95607,47879],{"class":103},[45,95609,1571],{"class":99},[45,95611,95612,95614,95616,95618],{"class":47,"line":89},[45,95613,2065],{"class":92},[45,95615,96],{"class":85},[45,95617,49082],{"class":2070},[45,95619,109],{"class":85},[14,95621,95622],{},"This is usually better than typing separators by hand.",[31,95624,95626],{"id":95625},"building-paths-safely","Building paths safely",[14,95628,95629],{},"A common beginner mistake is manually combining path parts like this:",[36,95631,95633],{"className":38,"code":95632,"language":40,"meta":41,"style":41},"folder = \"data\"\nfilename = \"notes.txt\"\npath = folder + \"\u002F\" + filename\nprint(path)\n",[18,95634,95635,95648,95661,95683],{"__ignoreMap":41},[45,95636,95637,95640,95642,95644,95646],{"class":47,"line":48},[45,95638,95639],{"class":51},"folder ",[45,95641,56],{"class":55},[45,95643,1565],{"class":99},[45,95645,30614],{"class":103},[45,95647,1571],{"class":99},[45,95649,95650,95653,95655,95657,95659],{"class":47,"line":63},[45,95651,95652],{"class":51},"filename ",[45,95654,56],{"class":55},[45,95656,1565],{"class":99},[45,95658,47879],{"class":103},[45,95660,1571],{"class":99},[45,95662,95663,95665,95667,95670,95672,95674,95676,95678,95680],{"class":47,"line":70},[45,95664,95587],{"class":51},[45,95666,56],{"class":55},[45,95668,95669],{"class":51}," folder ",[45,95671,8850],{"class":55},[45,95673,1565],{"class":99},[45,95675,63892],{"class":103},[45,95677,100],{"class":99},[45,95679,3775],{"class":55},[45,95681,95682],{"class":51}," filename\n",[45,95684,95685,95687,95689,95691],{"class":47,"line":89},[45,95686,2065],{"class":92},[45,95688,96],{"class":85},[45,95690,49082],{"class":2070},[45,95692,109],{"class":85},[14,95694,95695],{},"This may work, but it is easy to make mistakes.",[14,95697,95698,95699,457],{},"A safer way is to use ",[18,95700,95446],{},[36,95702,95703],{"className":38,"code":95566,"language":40,"meta":41,"style":41},[18,95704,95705,95715,95719,95745],{"__ignoreMap":41},[45,95706,95707,95709,95711,95713],{"class":47,"line":48},[45,95708,14921],{"class":73},[45,95710,95074],{"class":51},[45,95712,14870],{"class":73},[45,95714,95079],{"class":51},[45,95716,95717],{"class":47,"line":63},[45,95718,67],{"emptyLinePlaceholder":66},[45,95720,95721,95723,95725,95727,95729,95731,95733,95735,95737,95739,95741,95743],{"class":47,"line":70},[45,95722,95587],{"class":51},[45,95724,56],{"class":55},[45,95726,95093],{"class":2070},[45,95728,96],{"class":85},[45,95730,100],{"class":99},[45,95732,30614],{"class":103},[45,95734,100],{"class":99},[45,95736,9146],{"class":85},[45,95738,45881],{"class":55},[45,95740,1565],{"class":99},[45,95742,47879],{"class":103},[45,95744,1571],{"class":99},[45,95746,95747,95749,95751,95753],{"class":47,"line":89},[45,95748,2065],{"class":92},[45,95750,96],{"class":85},[45,95752,49082],{"class":2070},[45,95754,109],{"class":85},[14,95756,95757],{},"You can also build longer paths:",[36,95759,95761],{"className":38,"code":95760,"language":40,"meta":41,"style":41},"from pathlib import Path\n\npath = Path(\"projects\") \u002F \"python\" \u002F \"files\" \u002F \"notes.txt\"\nprint(path)\n",[18,95762,95763,95773,95777,95820],{"__ignoreMap":41},[45,95764,95765,95767,95769,95771],{"class":47,"line":48},[45,95766,14921],{"class":73},[45,95768,95074],{"class":51},[45,95770,14870],{"class":73},[45,95772,95079],{"class":51},[45,95774,95775],{"class":47,"line":63},[45,95776,67],{"emptyLinePlaceholder":66},[45,95778,95779,95781,95783,95785,95787,95789,95792,95794,95796,95798,95800,95802,95804,95806,95808,95810,95812,95814,95816,95818],{"class":47,"line":70},[45,95780,95587],{"class":51},[45,95782,56],{"class":55},[45,95784,95093],{"class":2070},[45,95786,96],{"class":85},[45,95788,100],{"class":99},[45,95790,95791],{"class":103},"projects",[45,95793,100],{"class":99},[45,95795,9146],{"class":85},[45,95797,45881],{"class":55},[45,95799,1565],{"class":99},[45,95801,40],{"class":103},[45,95803,100],{"class":99},[45,95805,45881],{"class":55},[45,95807,1565],{"class":99},[45,95809,46996],{"class":103},[45,95811,100],{"class":99},[45,95813,45881],{"class":55},[45,95815,1565],{"class":99},[45,95817,47879],{"class":103},[45,95819,1571],{"class":99},[45,95821,95822,95824,95826,95828],{"class":47,"line":89},[45,95823,2065],{"class":92},[45,95825,96],{"class":85},[45,95827,49082],{"class":2070},[45,95829,109],{"class":85},[14,95831,95832],{},"This avoids separator problems across operating systems.",[31,95834,95836],{"id":95835},"useful-path-checks","Useful path checks",[14,95838,95839],{},"Before opening a file, it is often helpful to check the path first.",[712,95841,95843],{"id":95842},"check-whether-a-path-exists","Check whether a path exists",[36,95845,95847],{"className":38,"code":95846,"language":40,"meta":41,"style":41},"from pathlib import Path\n\npath = Path(\"data\") \u002F \"notes.txt\"\nprint(path.exists())\n",[18,95848,95849,95859,95863,95889],{"__ignoreMap":41},[45,95850,95851,95853,95855,95857],{"class":47,"line":48},[45,95852,14921],{"class":73},[45,95854,95074],{"class":51},[45,95856,14870],{"class":73},[45,95858,95079],{"class":51},[45,95860,95861],{"class":47,"line":63},[45,95862,67],{"emptyLinePlaceholder":66},[45,95864,95865,95867,95869,95871,95873,95875,95877,95879,95881,95883,95885,95887],{"class":47,"line":70},[45,95866,95587],{"class":51},[45,95868,56],{"class":55},[45,95870,95093],{"class":2070},[45,95872,96],{"class":85},[45,95874,100],{"class":99},[45,95876,30614],{"class":103},[45,95878,100],{"class":99},[45,95880,9146],{"class":85},[45,95882,45881],{"class":55},[45,95884,1565],{"class":99},[45,95886,47879],{"class":103},[45,95888,1571],{"class":99},[45,95890,95891,95893,95895,95897,95899,95901],{"class":47,"line":89},[45,95892,2065],{"class":92},[45,95894,96],{"class":85},[45,95896,49082],{"class":2070},[45,95898,240],{"class":85},[45,95900,49087],{"class":2070},[45,95902,10777],{"class":85},[712,95904,95906],{"id":95905},"check-whether-it-is-a-file","Check whether it is a file",[36,95908,95910],{"className":38,"code":95909,"language":40,"meta":41,"style":41},"from pathlib import Path\n\npath = Path(\"data\") \u002F \"notes.txt\"\nprint(path.is_file())\n",[18,95911,95912,95922,95926,95952],{"__ignoreMap":41},[45,95913,95914,95916,95918,95920],{"class":47,"line":48},[45,95915,14921],{"class":73},[45,95917,95074],{"class":51},[45,95919,14870],{"class":73},[45,95921,95079],{"class":51},[45,95923,95924],{"class":47,"line":63},[45,95925,67],{"emptyLinePlaceholder":66},[45,95927,95928,95930,95932,95934,95936,95938,95940,95942,95944,95946,95948,95950],{"class":47,"line":70},[45,95929,95587],{"class":51},[45,95931,56],{"class":55},[45,95933,95093],{"class":2070},[45,95935,96],{"class":85},[45,95937,100],{"class":99},[45,95939,30614],{"class":103},[45,95941,100],{"class":99},[45,95943,9146],{"class":85},[45,95945,45881],{"class":55},[45,95947,1565],{"class":99},[45,95949,47879],{"class":103},[45,95951,1571],{"class":99},[45,95953,95954,95956,95958,95960,95962,95965],{"class":47,"line":89},[45,95955,2065],{"class":92},[45,95957,96],{"class":85},[45,95959,49082],{"class":2070},[45,95961,240],{"class":85},[45,95963,95964],{"class":2070},"is_file",[45,95966,10777],{"class":85},[712,95968,95970],{"id":95969},"check-whether-it-is-a-directory","Check whether it is a directory",[36,95972,95974],{"className":38,"code":95973,"language":40,"meta":41,"style":41},"from pathlib import Path\n\npath = Path(\"data\")\nprint(path.is_dir())\n",[18,95975,95976,95986,95990,96008],{"__ignoreMap":41},[45,95977,95978,95980,95982,95984],{"class":47,"line":48},[45,95979,14921],{"class":73},[45,95981,95074],{"class":51},[45,95983,14870],{"class":73},[45,95985,95079],{"class":51},[45,95987,95988],{"class":47,"line":63},[45,95989,67],{"emptyLinePlaceholder":66},[45,95991,95992,95994,95996,95998,96000,96002,96004,96006],{"class":47,"line":70},[45,95993,95587],{"class":51},[45,95995,56],{"class":55},[45,95997,95093],{"class":2070},[45,95999,96],{"class":85},[45,96001,100],{"class":99},[45,96003,30614],{"class":103},[45,96005,100],{"class":99},[45,96007,109],{"class":85},[45,96009,96010,96012,96014,96016,96018,96021],{"class":47,"line":89},[45,96011,2065],{"class":92},[45,96013,96],{"class":85},[45,96015,49082],{"class":2070},[45,96017,240],{"class":85},[45,96019,96020],{"class":2070},"is_dir",[45,96022,10777],{"class":85},[14,96024,96025],{},"These checks can help prevent errors before you try to open a file.",[14,96027,96028,96029,240],{},"If you want a full example, see ",[236,96030,96032],{"href":96031},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python","how to check if a file exists in Python",[31,96034,96036],{"id":96035},"converting-paths-to-strings","Converting paths to strings",[14,96038,96039,96040,96042],{},"Many Python functions accept ",[18,96041,95446],{}," objects directly.",[14,96044,15394,96045,96047,96048,96050],{},[18,96046,47592],{}," works with a ",[18,96049,95446],{}," object:",[36,96052,96054],{"className":38,"code":96053,"language":40,"meta":41,"style":41},"from pathlib import Path\n\nfile_path = Path(\"data\") \u002F \"notes.txt\"\n\nwith open(file_path, \"r\") as file:\n    print(file.read())\n",[18,96055,96056,96066,96070,96096,96100,96126],{"__ignoreMap":41},[45,96057,96058,96060,96062,96064],{"class":47,"line":48},[45,96059,14921],{"class":73},[45,96061,95074],{"class":51},[45,96063,14870],{"class":73},[45,96065,95079],{"class":51},[45,96067,96068],{"class":47,"line":63},[45,96069,67],{"emptyLinePlaceholder":66},[45,96071,96072,96074,96076,96078,96080,96082,96084,96086,96088,96090,96092,96094],{"class":47,"line":70},[45,96073,95088],{"class":51},[45,96075,56],{"class":55},[45,96077,95093],{"class":2070},[45,96079,96],{"class":85},[45,96081,100],{"class":99},[45,96083,30614],{"class":103},[45,96085,100],{"class":99},[45,96087,9146],{"class":85},[45,96089,45881],{"class":55},[45,96091,1565],{"class":99},[45,96093,47879],{"class":103},[45,96095,1571],{"class":99},[45,96097,96098],{"class":47,"line":89},[45,96099,67],{"emptyLinePlaceholder":66},[45,96101,96102,96104,96106,96108,96110,96112,96114,96116,96118,96120,96122,96124],{"class":47,"line":112},[45,96103,23989],{"class":73},[45,96105,23992],{"class":92},[45,96107,96],{"class":85},[45,96109,19988],{"class":2070},[45,96111,3079],{"class":85},[45,96113,1565],{"class":99},[45,96115,24008],{"class":103},[45,96117,100],{"class":99},[45,96119,9146],{"class":85},[45,96121,24015],{"class":73},[45,96123,15011],{"class":24018},[45,96125,86],{"class":85},[45,96127,96128,96130,96132,96134,96136,96138],{"class":47,"line":126},[45,96129,93],{"class":92},[45,96131,96],{"class":85},[45,96133,46818],{"class":24018},[45,96135,240],{"class":85},[45,96137,46823],{"class":2070},[45,96139,10777],{"class":85},[14,96141,96142,96143,457],{},"If you ever need a string version, convert it with ",[18,96144,41901],{},[36,96146,96148],{"className":38,"code":96147,"language":40,"meta":41,"style":41},"from pathlib import Path\n\nfile_path = Path(\"data\") \u002F \"notes.txt\"\nprint(str(file_path))\n",[18,96149,96150,96160,96164,96190],{"__ignoreMap":41},[45,96151,96152,96154,96156,96158],{"class":47,"line":48},[45,96153,14921],{"class":73},[45,96155,95074],{"class":51},[45,96157,14870],{"class":73},[45,96159,95079],{"class":51},[45,96161,96162],{"class":47,"line":63},[45,96163,67],{"emptyLinePlaceholder":66},[45,96165,96166,96168,96170,96172,96174,96176,96178,96180,96182,96184,96186,96188],{"class":47,"line":70},[45,96167,95088],{"class":51},[45,96169,56],{"class":55},[45,96171,95093],{"class":2070},[45,96173,96],{"class":85},[45,96175,100],{"class":99},[45,96177,30614],{"class":103},[45,96179,100],{"class":99},[45,96181,9146],{"class":85},[45,96183,45881],{"class":55},[45,96185,1565],{"class":99},[45,96187,47879],{"class":103},[45,96189,1571],{"class":99},[45,96191,96192,96194,96196,96198,96200,96202],{"class":47,"line":89},[45,96193,2065],{"class":92},[45,96195,96],{"class":85},[45,96197,29733],{"class":2109},[45,96199,96],{"class":85},[45,96201,19988],{"class":2070},[45,96203,4348],{"class":85},[14,96205,96206],{},"Most beginners do not need to convert paths unless a specific library requires a string.",[31,96208,5903],{"id":5902},[14,96210,96211],{},"These are some common file path problems:",[195,96213,96214,96217,96220,96223,96226],{},[198,96215,96216],{},"Using the wrong current working directory",[198,96218,96219],{},"Typing a relative path that does not match the folder Python is using",[198,96221,96222],{},"Building paths by hand with the wrong slash character",[198,96224,96225],{},"Mistaking a folder path for a file path",[198,96227,96228],{},"Trying to open a file before checking whether it exists",[14,96230,96231],{},"Helpful debugging commands:",[36,96233,96235],{"className":38,"code":96234,"language":40,"meta":41,"style":41},"from pathlib import Path\nprint(Path.cwd())\n",[18,96236,96237,96247],{"__ignoreMap":41},[45,96238,96239,96241,96243,96245],{"class":47,"line":48},[45,96240,14921],{"class":73},[45,96242,95074],{"class":51},[45,96244,14870],{"class":73},[45,96246,95079],{"class":51},[45,96248,96249,96251,96253,96255,96257,96259],{"class":47,"line":63},[45,96250,2065],{"class":92},[45,96252,96],{"class":85},[45,96254,95446],{"class":2070},[45,96256,240],{"class":85},[45,96258,95451],{"class":2070},[45,96260,10777],{"class":85},[36,96262,96264],{"className":38,"code":96263,"language":40,"meta":41,"style":41},"from pathlib import Path\nprint(Path(\"data\u002Fnotes.txt\").exists())\n",[18,96265,96266,96276],{"__ignoreMap":41},[45,96267,96268,96270,96272,96274],{"class":47,"line":48},[45,96269,14921],{"class":73},[45,96271,95074],{"class":51},[45,96273,14870],{"class":73},[45,96275,95079],{"class":51},[45,96277,96278,96280,96282,96284,96286,96288,96290,96292,96295,96297],{"class":47,"line":63},[45,96279,2065],{"class":92},[45,96281,96],{"class":85},[45,96283,95446],{"class":2070},[45,96285,96],{"class":85},[45,96287,100],{"class":99},[45,96289,95362],{"class":103},[45,96291,100],{"class":99},[45,96293,96294],{"class":85},").",[45,96296,49087],{"class":2070},[45,96298,10777],{"class":85},[36,96300,96302],{"className":38,"code":96301,"language":40,"meta":41,"style":41},"from pathlib import Path\np = Path(\"data\u002Fnotes.txt\")\nprint(p.resolve())\n",[18,96303,96304,96314,96333],{"__ignoreMap":41},[45,96305,96306,96308,96310,96312],{"class":47,"line":48},[45,96307,14921],{"class":73},[45,96309,95074],{"class":51},[45,96311,14870],{"class":73},[45,96313,95079],{"class":51},[45,96315,96316,96319,96321,96323,96325,96327,96329,96331],{"class":47,"line":63},[45,96317,96318],{"class":51},"p ",[45,96320,56],{"class":55},[45,96322,95093],{"class":2070},[45,96324,96],{"class":85},[45,96326,100],{"class":99},[45,96328,95362],{"class":103},[45,96330,100],{"class":99},[45,96332,109],{"class":85},[45,96334,96335,96337,96339,96341,96343,96346],{"class":47,"line":70},[45,96336,2065],{"class":92},[45,96338,96],{"class":85},[45,96340,14],{"class":2070},[45,96342,240],{"class":85},[45,96344,96345],{"class":2070},"resolve",[45,96347,10777],{"class":85},[36,96349,96351],{"className":38,"code":96350,"language":40,"meta":41,"style":41},"import os\nprint(os.getcwd())\n",[18,96352,96353,96359],{"__ignoreMap":41},[45,96354,96355,96357],{"class":47,"line":48},[45,96356,14870],{"class":73},[45,96358,49053],{"class":51},[45,96360,96361,96363,96365,96367,96369,96371],{"class":47,"line":63},[45,96362,2065],{"class":92},[45,96364,96],{"class":85},[45,96366,49062],{"class":2070},[45,96368,240],{"class":85},[45,96370,49067],{"class":2070},[45,96372,10777],{"class":85},[36,96374,96376],{"className":38,"code":96375,"language":40,"meta":41,"style":41},"import os\nprint(os.path.exists(\"data\u002Fnotes.txt\"))\n",[18,96377,96378,96384],{"__ignoreMap":41},[45,96379,96380,96382],{"class":47,"line":48},[45,96381,14870],{"class":73},[45,96383,49053],{"class":51},[45,96385,96386,96388,96390,96392,96394,96396,96398,96400,96402,96404,96406,96408],{"class":47,"line":63},[45,96387,2065],{"class":92},[45,96389,96],{"class":85},[45,96391,49062],{"class":2070},[45,96393,240],{"class":85},[45,96395,49082],{"class":3095},[45,96397,240],{"class":85},[45,96399,49087],{"class":2070},[45,96401,96],{"class":85},[45,96403,100],{"class":99},[45,96405,95362],{"class":103},[45,96407,100],{"class":99},[45,96409,4348],{"class":85},[14,96411,96412,96415],{},[18,96413,96414],{},"Path.resolve()"," is especially useful because it shows the full path Python is trying to use.",[31,96417,2281],{"id":2280},[712,96419,96421],{"id":96420},"should-beginners-use-pathlib-or-ospath","Should beginners use pathlib or os.path?",[14,96423,165,96424,96426],{},[18,96425,95185],{}," first. It is easier to read and usually simpler for beginners.",[14,96428,96429,96430,240],{},"If you want to learn the older style too, see ",[236,96431,96433,18107],{"href":96432},"\u002Fstandard-library\u002Fos.path.join-function-explained",[18,96434,96435],{},"os.path.join()",[712,96437,96439],{"id":96438},"why-does-my-relative-path-work-in-one-place-but-not-another","Why does my relative path work in one place but not another?",[14,96441,96442],{},"Because relative paths depend on the current working directory, not just the script file location.",[712,96444,96446],{"id":96445},"can-i-pass-a-path-object-to-open","Can I pass a Path object to open()?",[14,96448,37296,96449,96451,96452,96454],{},[18,96450,47592],{}," accepts ",[18,96453,95446],{}," objects in modern Python.",[712,96456,96458],{"id":96457},"what-is-the-difference-between-a-file-path-and-a-directory-path","What is the difference between a file path and a directory path?",[14,96460,96461],{},"A file path points to a file. A directory path points to a folder.",[31,96463,2342],{"id":2341},[195,96465,96466,96471,96476,96480,96484],{},[198,96467,96468],{},[236,96469,48698],{"href":96470},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python",[198,96472,96473],{},[236,96474,48952],{"href":96475},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python",[198,96477,96478],{},[236,96479,47823],{"href":95242},[198,96481,96482],{},[236,96483,49153],{"href":46830},[198,96485,96486],{},[236,96487,49281],{"href":96031},[14,96489,96490],{},"Once you understand paths, the next step is using them in real tasks like checking for files, opening them, reading them, and writing new ones.",[2367,96492,96493],{},"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 .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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":96495},[96496,96497,96498,96499,96504,96505,96506,96507,96512,96513,96514,96520],{"id":33,"depth":63,"text":34},{"id":2529,"depth":63,"text":2530},{"id":95191,"depth":63,"text":95173},{"id":95312,"depth":63,"text":95313,"children":96500},[96501,96502,96503],{"id":95319,"depth":70,"text":95320},{"id":95345,"depth":70,"text":95346},{"id":95373,"depth":70,"text":95374},{"id":95409,"depth":63,"text":95410},{"id":95534,"depth":63,"text":95535},{"id":95625,"depth":63,"text":95626},{"id":95835,"depth":63,"text":95836,"children":96508},[96509,96510,96511],{"id":95842,"depth":70,"text":95843},{"id":95905,"depth":70,"text":95906},{"id":95969,"depth":70,"text":95970},{"id":96035,"depth":63,"text":96036},{"id":5902,"depth":63,"text":5903},{"id":2280,"depth":63,"text":2281,"children":96515},[96516,96517,96518,96519],{"id":96420,"depth":70,"text":96421},{"id":96438,"depth":70,"text":96439},{"id":96445,"depth":70,"text":96446},{"id":96457,"depth":70,"text":96458},{"id":2341,"depth":63,"text":2342},"Master working with file paths in python in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fworking-with-file-paths-in-python",{"title":95049,"description":96521},"learn\u002Fworking-with-file-paths-in-python","bHlItwquXFybPo-qYCG53GLOoFTV4w3adPEvk11jRLY",{"id":96528,"title":94998,"body":96529,"description":97431,"extension":2399,"meta":97432,"navigation":66,"path":97433,"seo":97434,"stem":97435,"__hash__":97436},"content\u002Flearn\u002Fyour-first-python-program-hello-world-explained.md",{"type":7,"value":96530,"toc":97388},[96531,96534,96540,96543,96547,96565,96571,96575,96580,96583,96594,96597,96601,96604,96622,96625,96630,96634,96656,96662,96669,96673,96676,96680,96683,96689,96693,96696,96714,96718,96725,96727,96732,96734,96740,96742,96745,96755,96757,96767,96771,96774,96779,96784,96788,96791,96809,96813,96818,96821,96825,96828,96831,96836,96841,96844,96852,96856,96859,96861,96879,96882,96902,96916,96918,96921,96925,96927,96945,96947,96965,96968,96972,96975,96996,96999,97002,97020,97025,97028,97031,97065,97067,97085,97089,97095,97098,97103,97107,97113,97116,97157,97160,97171,97175,97181,97185,97205,97209,97243,97245,97251,97255,97275,97278,97280,97283,97303,97305,97309,97312,97316,97319,97322,97327,97332,97336,97342,97346,97349,97351,97379,97385],[10,96532,94998],{"id":96533},"your-first-python-program-hello-world-explained",[14,96535,96536,96537,96539],{},"Writing a ",[2421,96538,94918],{}," program is a common first step when learning Python. It helps you confirm that Python is installed, shows you how to run code, and introduces the idea that a program is just a set of instructions for the computer.",[14,96541,96542],{},"If you are completely new to Python, this is the right place to start.",[31,96544,96546],{"id":96545},"quick-start","Quick start",[36,96548,96549],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,96550,96551],{"__ignoreMap":41},[45,96552,96553,96555,96557,96559,96561,96563],{"class":47,"line":48},[45,96554,2065],{"class":92},[45,96556,96],{"class":85},[45,96558,100],{"class":99},[45,96560,17031],{"class":103},[45,96562,100],{"class":99},[45,96564,109],{"class":85},[14,96566,96567,96568,96570],{},"Save this in a ",[18,96569,15010],{}," file or run it in the Python interpreter to print text to the screen.",[31,96572,96574],{"id":96573},"what-hello-world-means","What Hello World means",[14,96576,96577,96579],{},[2421,96578,94918],{}," is the traditional first program in many programming languages.",[14,96581,96582],{},"It is useful because it helps you:",[195,96584,96585,96588,96591],{},[198,96586,96587],{},"check that Python is installed and working",[198,96589,96590],{},"run a very small program successfully",[198,96592,96593],{},"understand that code tells the computer what to do",[14,96595,96596],{},"The program is simple on purpose. You do not need to learn many rules before you can make Python do something visible.",[31,96598,96600],{"id":96599},"the-first-program","The first program",[14,96602,96603],{},"Here is the full program:",[36,96605,96606],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,96607,96608],{"__ignoreMap":41},[45,96609,96610,96612,96614,96616,96618,96620],{"class":47,"line":48},[45,96611,2065],{"class":92},[45,96613,96],{"class":85},[45,96615,100],{"class":99},[45,96617,17031],{"class":103},[45,96619,100],{"class":99},[45,96621,109],{"class":85},[14,96623,96624],{},"When you run this line, Python displays this output:",[36,96626,96628],{"className":96627,"code":17042,"language":5000,"meta":41},[4998],[18,96629,17042],{"__ignoreMap":41},[712,96631,96633],{"id":96632},"what-this-line-does","What this line does",[195,96635,96636,96641,96646,96651],{},[198,96637,96638,96640],{},[18,96639,15833],{}," is a Python function",[198,96642,96643,96645],{},[18,96644,15833],{}," displays output in the terminal or console",[198,96647,96648,40628],{},[18,96649,96650],{},"\"Hello, world!\"",[198,96652,96653,96654],{},"Text inside quotes is called a ",[2421,96655,39811],{},[14,96657,96658,96659,96661],{},"Python reads the line from left to right and runs the ",[18,96660,15833],{}," function with the text you gave it.",[14,96663,96664,96665,240],{},"If you want a deeper explanation of this function, see ",[236,96666,18104,96667,19075],{"href":81031},[18,96668,15833],{},[31,96670,96672],{"id":96671},"how-to-run-the-program","How to run the program",[14,96674,96675],{},"A simple way to run your first Python program is to save it in a file.",[712,96677,96679],{"id":96678},"_1-create-a-file","1. Create a file",[14,96681,96682],{},"Create a new file named:",[36,96684,96687],{"className":96685,"code":96686,"language":5000,"meta":41},[4998],"hello.py\n",[18,96688,96686],{"__ignoreMap":41},[712,96690,96692],{"id":96691},"_2-add-the-code","2. Add the code",[14,96694,96695],{},"Put this code in the file:",[36,96697,96698],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,96699,96700],{"__ignoreMap":41},[45,96701,96702,96704,96706,96708,96710,96712],{"class":47,"line":48},[45,96703,2065],{"class":92},[45,96705,96],{"class":85},[45,96707,100],{"class":99},[45,96709,17031],{"class":103},[45,96711,100],{"class":99},[45,96713,109],{"class":85},[712,96715,96717],{"id":96716},"_3-save-the-file","3. Save the file",[14,96719,96720,96721,64456,96723,240],{},"Make sure the file ends with ",[18,96722,15010],{},[18,96724,47785],{},[14,96726,47022],{},[36,96728,96730],{"className":96729,"code":96686,"language":5000,"meta":41},[4998],[18,96731,96686],{"__ignoreMap":41},[14,96733,3806],{},[36,96735,96738],{"className":96736,"code":96737,"language":5000,"meta":41},[4998],"hello.py.txt\n",[18,96739,96737],{"__ignoreMap":41},[712,96741,91594],{"id":91593},[14,96743,96744],{},"Open a terminal or command prompt in the folder where the file is saved, then run one of these commands:",[36,96746,96747],{"className":15652,"code":17966,"language":15654,"meta":41,"style":41},[18,96748,96749],{"__ignoreMap":41},[45,96750,96751,96753],{"class":47,"line":48},[45,96752,40],{"class":2444},[45,96754,17975],{"class":103},[14,96756,16999],{},[36,96758,96759],{"className":15652,"code":17980,"language":15654,"meta":41,"style":41},[18,96760,96761],{"__ignoreMap":41},[45,96762,96763,96765],{"class":47,"line":48},[45,96764,16556],{"class":2444},[45,96766,17975],{"class":103},[712,96768,96770],{"id":96769},"_5-check-the-output","5. Check the output",[14,96772,96773],{},"You should see:",[36,96775,96777],{"className":96776,"code":17042,"language":5000,"meta":41},[4998],[18,96778,17042],{"__ignoreMap":41},[14,96780,96781,96782,240],{},"If you want more help with terminals, editors, or IDEs, read ",[236,96783,17174],{"href":17173},[31,96785,96787],{"id":96786},"breaking-the-line-into-parts","Breaking the line into parts",[14,96789,96790],{},"Let’s look at the program one piece at a time:",[36,96792,96793],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,96794,96795],{"__ignoreMap":41},[45,96796,96797,96799,96801,96803,96805,96807],{"class":47,"line":48},[45,96798,2065],{"class":92},[45,96800,96],{"class":85},[45,96802,100],{"class":99},[45,96804,17031],{"class":103},[45,96806,100],{"class":99},[45,96808,109],{"class":85},[712,96810,96811],{"id":2065},[18,96812,2065],{},[14,96814,96815,96817],{},[18,96816,2065],{}," is a built-in Python function.",[14,96819,96820],{},"A function is a tool that does something when you use it. In this case, it shows output on the screen.",[712,96822,96823],{"id":41},[18,96824,3772],{},[14,96826,96827],{},"The parentheses call the function.",[14,96829,96830],{},"They tell Python: “run this function now.”",[712,96832,96834],{"id":96833},"hello-world",[18,96835,96650],{},[14,96837,96838,96839,240],{},"This is the value being passed into ",[18,96840,15833],{},[14,96842,96843],{},"Because it is inside quotation marks, Python treats it as text.",[14,96845,96846,96847,96849,96850,240],{},"That text is a ",[2421,96848,39811],{},". If you are new to strings, see ",[236,96851,29917],{"href":29916},[712,96853,96855],{"id":96854},"why-the-quotes-matter","Why the quotes matter",[14,96857,96858],{},"Quotes are required here because the message is text.",[14,96860,12381],{},[36,96862,96863],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,96864,96865],{"__ignoreMap":41},[45,96866,96867,96869,96871,96873,96875,96877],{"class":47,"line":48},[45,96868,2065],{"class":92},[45,96870,96],{"class":85},[45,96872,100],{"class":99},[45,96874,17031],{"class":103},[45,96876,100],{"class":99},[45,96878,109],{"class":85},[14,96880,96881],{},"This does not:",[36,96883,96885],{"className":38,"code":96884,"language":40,"meta":41,"style":41},"print(Hello, world!)\n",[18,96886,96887],{"__ignoreMap":41},[45,96888,96889,96891,96893,96895,96897,96900],{"class":47,"line":48},[45,96890,2065],{"class":92},[45,96892,96],{"class":85},[45,96894,3226],{"class":2070},[45,96896,3079],{"class":85},[45,96898,96899],{"class":2070}," world!",[45,96901,109],{"class":85},[14,96903,96904,96905,1316,96907,96910,96911,96913,96914,240],{},"Without quotes, Python tries to read ",[18,96906,3226],{},[18,96908,96909],{},"world"," as names instead of text. That often leads to an error such as ",[236,96912,46560],{"href":46557}," or a ",[236,96915,18361],{"href":46184},[31,96917,1337],{"id":1336},[14,96919,96920],{},"These are some very common problems when writing your first Python program.",[712,96922,96924],{"id":96923},"forgetting-quotation-marks","Forgetting quotation marks",[14,96926,3806],{},[36,96928,96929],{"className":38,"code":96884,"language":40,"meta":41,"style":41},[18,96930,96931],{"__ignoreMap":41},[45,96932,96933,96935,96937,96939,96941,96943],{"class":47,"line":48},[45,96934,2065],{"class":92},[45,96936,96],{"class":85},[45,96938,3226],{"class":2070},[45,96940,3079],{"class":85},[45,96942,96899],{"class":2070},[45,96944,109],{"class":85},[14,96946,1466],{},[36,96948,96949],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,96950,96951],{"__ignoreMap":41},[45,96952,96953,96955,96957,96959,96961,96963],{"class":47,"line":48},[45,96954,2065],{"class":92},[45,96956,96],{"class":85},[45,96958,100],{"class":99},[45,96960,17031],{"class":103},[45,96962,100],{"class":99},[45,96964,109],{"class":85},[14,96966,96967],{},"Without quotes, Python does not know that this is text.",[712,96969,96971],{"id":96970},"using-smart-quotes","Using smart quotes",[14,96973,96974],{},"Sometimes code copied from a website, document, or phone uses curly quotes like these:",[36,96976,96978],{"className":38,"code":96977,"language":40,"meta":41,"style":41},"print(“Hello, world!”)\n",[18,96979,96980],{"__ignoreMap":41},[45,96981,96982,96984,96986,96989,96991,96994],{"class":47,"line":48},[45,96983,2065],{"class":92},[45,96985,96],{"class":85},[45,96987,96988],{"class":2070},"“Hello",[45,96990,3079],{"class":85},[45,96992,96993],{"class":2070}," world!”",[45,96995,109],{"class":85},[14,96997,96998],{},"That will usually cause an error.",[14,97000,97001],{},"Use normal straight quotes instead:",[36,97003,97004],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,97005,97006],{"__ignoreMap":41},[45,97007,97008,97010,97012,97014,97016,97018],{"class":47,"line":48},[45,97009,2065],{"class":92},[45,97011,96],{"class":85},[45,97013,100],{"class":99},[45,97015,17031],{"class":103},[45,97017,100],{"class":99},[45,97019,109],{"class":85},[712,97021,83805,97023],{"id":97022},"misspelling-print",[18,97024,2065],{},[14,97026,97027],{},"Python is case-sensitive.",[14,97029,97030],{},"These are wrong:",[36,97032,97034],{"className":38,"code":97033,"language":40,"meta":41,"style":41},"Print(\"Hello, world!\")\npint(\"Hello, world!\")\n",[18,97035,97036,97050],{"__ignoreMap":41},[45,97037,97038,97040,97042,97044,97046,97048],{"class":47,"line":48},[45,97039,71317],{"class":2070},[45,97041,96],{"class":85},[45,97043,100],{"class":99},[45,97045,17031],{"class":103},[45,97047,100],{"class":99},[45,97049,109],{"class":85},[45,97051,97052,97055,97057,97059,97061,97063],{"class":47,"line":63},[45,97053,97054],{"class":2070},"pint",[45,97056,96],{"class":85},[45,97058,100],{"class":99},[45,97060,17031],{"class":103},[45,97062,100],{"class":99},[45,97064,109],{"class":85},[14,97066,9886],{},[36,97068,97069],{"className":38,"code":17018,"language":40,"meta":41,"style":41},[18,97070,97071],{"__ignoreMap":41},[45,97072,97073,97075,97077,97079,97081,97083],{"class":47,"line":48},[45,97074,2065],{"class":92},[45,97076,96],{"class":85},[45,97078,100],{"class":99},[45,97080,17031],{"class":103},[45,97082,100],{"class":99},[45,97084,109],{"class":85},[712,97086,97088],{"id":97087},"saving-the-file-with-the-wrong-extension","Saving the file with the wrong extension",[14,97090,97091,97092,97094],{},"If your file is saved as ",[18,97093,18787],{},", Python may not run it as a Python file.",[14,97096,97097],{},"Make sure the file is really named:",[36,97099,97101],{"className":97100,"code":96686,"language":5000,"meta":41},[4998],[18,97102,96686],{"__ignoreMap":41},[712,97104,97106],{"id":97105},"running-the-command-in-the-wrong-folder","Running the command in the wrong folder",[14,97108,97109,97110,97112],{},"If your terminal is not in the same folder as ",[18,97111,17927],{},", Python may say it cannot find the file.",[14,97114,97115],{},"Helpful commands:",[36,97117,97119],{"className":15652,"code":97118,"language":15654,"meta":41,"style":41},"python --version\npython3 --version\npython hello.py\npython3 hello.py\npwd\nls\ndir\n",[18,97120,97121,97127,97133,97139,97145,97149,97153],{"__ignoreMap":41},[45,97122,97123,97125],{"class":47,"line":48},[45,97124,40],{"class":2444},[45,97126,16298],{"class":15663},[45,97128,97129,97131],{"class":47,"line":63},[45,97130,16556],{"class":2444},[45,97132,16298],{"class":15663},[45,97134,97135,97137],{"class":47,"line":70},[45,97136,40],{"class":2444},[45,97138,17975],{"class":103},[45,97140,97141,97143],{"class":47,"line":89},[45,97142,16556],{"class":2444},[45,97144,17975],{"class":103},[45,97146,97147],{"class":47,"line":112},[45,97148,18203],{"class":92},[45,97150,97151],{"class":47,"line":126},[45,97152,18185],{"class":2444},[45,97154,97155],{"class":47,"line":142},[45,97156,18173],{"class":2444},[14,97158,97159],{},"These commands can help you:",[195,97161,97162,97165,97168],{},[198,97163,97164],{},"check whether Python is installed",[198,97166,97167],{},"confirm which folder you are in",[198,97169,97170],{},"list the files in the current folder",[31,97172,97174],{"id":97173},"simple-variations-to-try","Simple variations to try",[14,97176,97177,97178,97180],{},"Once ",[18,97179,17031],{}," works, try small changes.",[712,97182,97184],{"id":97183},"print-a-different-message","Print a different message",[36,97186,97188],{"className":38,"code":97187,"language":40,"meta":41,"style":41},"print(\"My name is Sam.\")\n",[18,97189,97190],{"__ignoreMap":41},[45,97191,97192,97194,97196,97198,97201,97203],{"class":47,"line":48},[45,97193,2065],{"class":92},[45,97195,96],{"class":85},[45,97197,100],{"class":99},[45,97199,97200],{"class":103},"My name is Sam.",[45,97202,100],{"class":99},[45,97204,109],{"class":85},[712,97206,97208],{"id":97207},"print-two-lines","Print two lines",[36,97210,97212],{"className":38,"code":97211,"language":40,"meta":41,"style":41},"print(\"Hello, world!\")\nprint(\"I am learning Python.\")\n",[18,97213,97214,97228],{"__ignoreMap":41},[45,97215,97216,97218,97220,97222,97224,97226],{"class":47,"line":48},[45,97217,2065],{"class":92},[45,97219,96],{"class":85},[45,97221,100],{"class":99},[45,97223,17031],{"class":103},[45,97225,100],{"class":99},[45,97227,109],{"class":85},[45,97229,97230,97232,97234,97236,97239,97241],{"class":47,"line":63},[45,97231,2065],{"class":92},[45,97233,96],{"class":85},[45,97235,100],{"class":99},[45,97237,97238],{"class":103},"I am learning Python.",[45,97240,100],{"class":99},[45,97242,109],{"class":85},[14,97244,177],{},[36,97246,97249],{"className":97247,"code":97248,"language":5000,"meta":41},[4998],"Hello, world!\nI am learning Python.\n",[18,97250,97248],{"__ignoreMap":41},[712,97252,97254],{"id":97253},"try-another-string","Try another string",[36,97256,97258],{"className":38,"code":97257,"language":40,"meta":41,"style":41},"print(\"Python is working.\")\n",[18,97259,97260],{"__ignoreMap":41},[45,97261,97262,97264,97266,97268,97271,97273],{"class":47,"line":48},[45,97263,2065],{"class":92},[45,97265,96],{"class":85},[45,97267,100],{"class":99},[45,97269,97270],{"class":103},"Python is working.",[45,97272,100],{"class":99},[45,97274,109],{"class":85},[14,97276,97277],{},"For now, keep your focus on basic output. You do not need user input yet.",[31,97279,47394],{"id":47393},[14,97281,97282],{},"After your first program works, the next useful topics are:",[195,97284,97285,97290,97295,97300],{},[198,97286,97287,97288],{},"basic Python rules and structure in ",[236,97289,40356],{"href":72223},[198,97291,97292,97293,72384],{},"strings, because ",[18,97294,96650],{},[198,97296,3267,97297,97299],{},[18,97298,15833],{}," function for printing multiple values and formatting",[198,97301,97302],{},"different ways to run Python code in files, terminals, and editors",[31,97304,2281],{"id":2280},[712,97306,97308],{"id":97307},"why-is-hello-world-used-as-the-first-program","Why is Hello World used as the first program?",[14,97310,97311],{},"It is simple, easy to test, and helps confirm that your Python setup works.",[712,97313,97315],{"id":97314},"do-i-need-to-save-the-code-in-a-file-first","Do I need to save the code in a file first?",[14,97317,97318],{},"No. You can also type it directly into the Python interpreter.",[14,97320,97321],{},"However, saving it in a file is useful because it teaches you how Python scripts work.",[712,97323,19018,97325,35538],{"id":97324},"what-does-print-do-in-python",[18,97326,15833],{},[14,97328,97329,97331],{},[18,97330,15833],{}," shows output on the screen.",[712,97333,97335],{"id":97334},"why-are-quotation-marks-needed","Why are quotation marks needed?",[14,97337,97338,97339,97341],{},"Quotation marks tell Python that ",[18,97340,17031],{}," is text, not a variable or function name.",[712,97343,97345],{"id":97344},"why-do-i-get-a-nameerror-when-i-type-hello-world-without-quotes","Why do I get a NameError when I type Hello, world! without quotes?",[14,97347,97348],{},"Without quotes, Python tries to treat the words as names instead of text.",[31,97350,2342],{"id":2341},[195,97352,97353,97357,97361,97365,97371,97375],{},[198,97354,97355],{},[236,97356,17425],{"href":17173},[198,97358,97359],{},[236,97360,40356],{"href":72223},[198,97362,97363],{},[236,97364,41082],{"href":29916},[198,97366,97367],{},[236,97368,18104,97369,19075],{"href":81031},[18,97370,15833],{},[198,97372,97373],{},[236,97374,46560],{"href":46557},[198,97376,97377],{},[236,97378,18361],{"href":46184},[14,97380,97381,97382,97384],{},"If you can run this program successfully, you have already completed an important first step. Next, continue with syntax basics and learn more about ",[18,97383,15833],{}," so you can write slightly larger Python programs with confidence.",[2367,97386,97387],{},"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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--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 pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":41,"searchDepth":63,"depth":63,"links":97389},[97390,97391,97392,97395,97402,97408,97416,97421,97422,97430],{"id":96545,"depth":63,"text":96546},{"id":96573,"depth":63,"text":96574},{"id":96599,"depth":63,"text":96600,"children":97393},[97394],{"id":96632,"depth":70,"text":96633},{"id":96671,"depth":63,"text":96672,"children":97396},[97397,97398,97399,97400,97401],{"id":96678,"depth":70,"text":96679},{"id":96691,"depth":70,"text":96692},{"id":96716,"depth":70,"text":96717},{"id":91593,"depth":70,"text":91594},{"id":96769,"depth":70,"text":96770},{"id":96786,"depth":63,"text":96787,"children":97403},[97404,97405,97406,97407],{"id":2065,"depth":70,"text":2065},{"id":41,"depth":70,"text":3772},{"id":96833,"depth":70,"text":96650},{"id":96854,"depth":70,"text":96855},{"id":1336,"depth":63,"text":1337,"children":97409},[97410,97411,97412,97414,97415],{"id":96923,"depth":70,"text":96924},{"id":96970,"depth":70,"text":96971},{"id":97022,"depth":70,"text":97413},"Misspelling print",{"id":97087,"depth":70,"text":97088},{"id":97105,"depth":70,"text":97106},{"id":97173,"depth":63,"text":97174,"children":97417},[97418,97419,97420],{"id":97183,"depth":70,"text":97184},{"id":97207,"depth":70,"text":97208},{"id":97253,"depth":70,"text":97254},{"id":47393,"depth":63,"text":47394},{"id":2280,"depth":63,"text":2281,"children":97423},[97424,97425,97426,97428,97429],{"id":97307,"depth":70,"text":97308},{"id":97314,"depth":70,"text":97315},{"id":97324,"depth":70,"text":97427},"What does print() do in Python?",{"id":97334,"depth":70,"text":97335},{"id":97344,"depth":70,"text":97345},{"id":2341,"depth":63,"text":2342},"Master your first python program hello world explained in our comprehensive Python beginner guide.",{},"\u002Flearn\u002Fyour-first-python-program-hello-world-explained",{"title":94998,"description":97431},"learn\u002Fyour-first-python-program-hello-world-explained","pCdTMlJhKn42jqpCU_zuwFcOI5__CICjQ3mzFYHaTEI",1776944939126]