[{"data":1,"prerenderedAt":94036},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-weather-api-example":3,"nav-examples":2081},{"id":4,"title":5,"body":6,"description":2074,"extension":2075,"meta":2076,"navigation":69,"path":2077,"seo":2078,"stem":2079,"__hash__":2080},"content\u002Fexamples\u002Fpython-weather-api-example.md","Python Weather API Example",{"type":7,"value":8,"toc":2037},"minimark",[9,13,17,20,36,41,298,305,308,320,324,327,344,348,351,368,376,380,385,388,406,409,429,432,451,455,458,638,643,654,660,664,681,684,688,747,750,753,768,776,807,810,817,821,840,850,862,866,923,926,940,944,947,950,958,962,968,971,1095,1098,1117,1120,1148,1151,1166,1173,1177,1180,1635,1638,1658,1662,1665,1684,1687,1715,1732,1736,1739,1743,1763,1766,1770,1789,1792,1796,1817,1820,1827,1850,1853,1857,1860,1863,1896,1899,1902,1939,1943,1947,1950,1953,1960,1965,1968,1979,1986,1989,1996,1999,2002,2006,2033],[10,11,5],"h1",{"id":12},"python-weather-api-example",[14,15,16],"p",{},"This beginner-friendly example shows how to request weather data from an API, read the JSON response, and print a few useful values.",[14,18,19],{},"The goal here is to build a simple real-world Python example. You do not need to fully understand HTTP yet. You only need to see the basic pattern:",[21,22,23,27,30,33],"ol",{},[24,25,26],"li",{},"Send a request",[24,28,29],{},"Get data back",[24,31,32],{},"Read the JSON",[24,34,35],{},"Print the values you want",[37,38,40],"h2",{"id":39},"quick-example","Quick example",[42,43,48],"pre",{"className":44,"code":45,"language":46,"meta":47,"style":47},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import requests\n\nurl = \"https:\u002F\u002Fapi.open-meteo.com\u002Fv1\u002Fforecast\"\nparams = {\n    \"latitude\": 40.7128,\n    \"longitude\": -74.0060,\n    \"current_weather\": True\n}\n\nresponse = requests.get(url, params=params)\ndata = response.json()\n\nprint(data[\"current_weather\"][\"temperature\"])\nprint(data[\"current_weather\"][\"windspeed\"])\n","python","",[49,50,51,64,71,92,104,126,146,162,168,173,212,231,236,270],"code",{"__ignoreMap":47},[52,53,56,60],"span",{"class":54,"line":55},"line",1,[52,57,59],{"class":58},"sVHd0","import",[52,61,63],{"class":62},"su5hD"," requests\n",[52,65,67],{"class":54,"line":66},2,[52,68,70],{"emptyLinePlaceholder":69},true,"\n",[52,72,74,77,81,85,89],{"class":54,"line":73},3,[52,75,76],{"class":62},"url ",[52,78,80],{"class":79},"smGrS","=",[52,82,84],{"class":83},"sjJ54"," \"",[52,86,88],{"class":87},"s_sjI","https:\u002F\u002Fapi.open-meteo.com\u002Fv1\u002Fforecast",[52,90,91],{"class":83},"\"\n",[52,93,95,98,100],{"class":54,"line":94},4,[52,96,97],{"class":62},"params ",[52,99,80],{"class":79},[52,101,103],{"class":102},"sP7_E"," {\n",[52,105,107,110,113,116,119,123],{"class":54,"line":106},5,[52,108,109],{"class":83},"    \"",[52,111,112],{"class":87},"latitude",[52,114,115],{"class":83},"\"",[52,117,118],{"class":102},":",[52,120,122],{"class":121},"srdBf"," 40.7128",[52,124,125],{"class":102},",\n",[52,127,129,131,134,136,138,141,144],{"class":54,"line":128},6,[52,130,109],{"class":83},[52,132,133],{"class":87},"longitude",[52,135,115],{"class":83},[52,137,118],{"class":102},[52,139,140],{"class":79}," -",[52,142,143],{"class":121},"74.0060",[52,145,125],{"class":102},[52,147,149,151,154,156,158],{"class":54,"line":148},7,[52,150,109],{"class":83},[52,152,153],{"class":87},"current_weather",[52,155,115],{"class":83},[52,157,118],{"class":102},[52,159,161],{"class":160},"s39Yj"," True\n",[52,163,165],{"class":54,"line":164},8,[52,166,167],{"class":102},"}\n",[52,169,171],{"class":54,"line":170},9,[52,172,70],{"emptyLinePlaceholder":69},[52,174,176,179,181,184,187,191,194,197,200,204,206,209],{"class":54,"line":175},10,[52,177,178],{"class":62},"response ",[52,180,80],{"class":79},[52,182,183],{"class":62}," requests",[52,185,186],{"class":102},".",[52,188,190],{"class":189},"slqww","get",[52,192,193],{"class":102},"(",[52,195,196],{"class":189},"url",[52,198,199],{"class":102},",",[52,201,203],{"class":202},"s99_P"," params",[52,205,80],{"class":79},[52,207,208],{"class":189},"params",[52,210,211],{"class":102},")\n",[52,213,215,218,220,223,225,228],{"class":54,"line":214},11,[52,216,217],{"class":62},"data ",[52,219,80],{"class":79},[52,221,222],{"class":62}," response",[52,224,186],{"class":102},[52,226,227],{"class":189},"json",[52,229,230],{"class":102},"()\n",[52,232,234],{"class":54,"line":233},12,[52,235,70],{"emptyLinePlaceholder":69},[52,237,239,243,245,248,251,253,255,257,260,262,265,267],{"class":54,"line":238},13,[52,240,242],{"class":241},"sptTA","print",[52,244,193],{"class":102},[52,246,247],{"class":189},"data",[52,249,250],{"class":102},"[",[52,252,115],{"class":83},[52,254,153],{"class":87},[52,256,115],{"class":83},[52,258,259],{"class":102},"][",[52,261,115],{"class":83},[52,263,264],{"class":87},"temperature",[52,266,115],{"class":83},[52,268,269],{"class":102},"])\n",[52,271,273,275,277,279,281,283,285,287,289,291,294,296],{"class":54,"line":272},14,[52,274,242],{"class":241},[52,276,193],{"class":102},[52,278,247],{"class":189},[52,280,250],{"class":102},[52,282,115],{"class":83},[52,284,153],{"class":87},[52,286,115],{"class":83},[52,288,259],{"class":102},[52,290,115],{"class":83},[52,292,293],{"class":87},"windspeed",[52,295,115],{"class":83},[52,297,269],{"class":102},[14,299,300,301,304],{},"This uses the free Open-Meteo API and the ",[49,302,303],{},"requests"," package.",[14,306,307],{},"It keeps the example small:",[309,310,311,314,317],"ul",{},[24,312,313],{},"Make a GET request",[24,315,316],{},"Parse JSON",[24,318,319],{},"Print values",[37,321,323],{"id":322},"what-this-example-does","What this example does",[14,325,326],{},"This example:",[309,328,329,332,335,338,341],{},[24,330,331],{},"Makes a GET request to a weather API",[24,333,334],{},"Sends location data as query parameters",[24,336,337],{},"Gets JSON data back from the API",[24,339,340],{},"Reads a few fields from the response",[24,342,343],{},"Prints the current weather values",[37,345,347],{"id":346},"what-you-need-before-running-it","What you need before running it",[14,349,350],{},"Before you run the code, make sure you have:",[309,352,353,356,362,365],{},[24,354,355],{},"Python installed",[24,357,358,359,361],{},"The ",[49,360,303],{}," package installed with pip",[24,363,364],{},"An internet connection",[24,366,367],{},"Basic understanding of dictionaries and function calls",[14,369,370,371,186],{},"If you are new to API requests, see ",[372,373,375],"a",{"href":374},"\u002Fhow-to\u002Fhow-to-make-an-api-request-in-python\u002F","how to make an API request in Python",[37,377,379],{"id":378},"install-requests","Install requests",[14,381,358,382,384],{},[49,383,303],{}," package is not built into Python, so you need to install it first.",[14,386,387],{},"Run:",[42,389,393],{"className":390,"code":391,"language":392,"meta":47,"style":47},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","pip install requests\n","bash",[49,394,395],{"__ignoreMap":47},[52,396,397,401,404],{"class":54,"line":55},[52,398,400],{"class":399},"sbgvK","pip",[52,402,403],{"class":87}," install",[52,405,63],{"class":87},[14,407,408],{},"If that does not work, try:",[42,410,412],{"className":390,"code":411,"language":392,"meta":47,"style":47},"python -m pip install requests\n",[49,413,414],{"__ignoreMap":47},[52,415,416,418,422,425,427],{"class":54,"line":55},[52,417,46],{"class":399},[52,419,421],{"class":420},"stzsN"," -m",[52,423,424],{"class":87}," pip",[52,426,403],{"class":87},[52,428,63],{"class":87},[14,430,431],{},"You can also check that it is installed with:",[42,433,435],{"className":390,"code":434,"language":392,"meta":47,"style":47},"python -c \"import requests; print(requests.__version__)\"\n",[49,436,437],{"__ignoreMap":47},[52,438,439,441,444,446,449],{"class":54,"line":55},[52,440,46],{"class":399},[52,442,443],{"class":420}," -c",[52,445,84],{"class":83},[52,447,448],{"class":87},"import requests; print(requests.__version__)",[52,450,91],{"class":83},[37,452,454],{"id":453},"how-the-code-works-step-by-step","How the code works step by step",[14,456,457],{},"Here is the same example again:",[42,459,460],{"className":44,"code":45,"language":46,"meta":47,"style":47},[49,461,462,468,472,484,492,506,522,534,538,542,568,582,586,612],{"__ignoreMap":47},[52,463,464,466],{"class":54,"line":55},[52,465,59],{"class":58},[52,467,63],{"class":62},[52,469,470],{"class":54,"line":66},[52,471,70],{"emptyLinePlaceholder":69},[52,473,474,476,478,480,482],{"class":54,"line":73},[52,475,76],{"class":62},[52,477,80],{"class":79},[52,479,84],{"class":83},[52,481,88],{"class":87},[52,483,91],{"class":83},[52,485,486,488,490],{"class":54,"line":94},[52,487,97],{"class":62},[52,489,80],{"class":79},[52,491,103],{"class":102},[52,493,494,496,498,500,502,504],{"class":54,"line":106},[52,495,109],{"class":83},[52,497,112],{"class":87},[52,499,115],{"class":83},[52,501,118],{"class":102},[52,503,122],{"class":121},[52,505,125],{"class":102},[52,507,508,510,512,514,516,518,520],{"class":54,"line":128},[52,509,109],{"class":83},[52,511,133],{"class":87},[52,513,115],{"class":83},[52,515,118],{"class":102},[52,517,140],{"class":79},[52,519,143],{"class":121},[52,521,125],{"class":102},[52,523,524,526,528,530,532],{"class":54,"line":148},[52,525,109],{"class":83},[52,527,153],{"class":87},[52,529,115],{"class":83},[52,531,118],{"class":102},[52,533,161],{"class":160},[52,535,536],{"class":54,"line":164},[52,537,167],{"class":102},[52,539,540],{"class":54,"line":170},[52,541,70],{"emptyLinePlaceholder":69},[52,543,544,546,548,550,552,554,556,558,560,562,564,566],{"class":54,"line":175},[52,545,178],{"class":62},[52,547,80],{"class":79},[52,549,183],{"class":62},[52,551,186],{"class":102},[52,553,190],{"class":189},[52,555,193],{"class":102},[52,557,196],{"class":189},[52,559,199],{"class":102},[52,561,203],{"class":202},[52,563,80],{"class":79},[52,565,208],{"class":189},[52,567,211],{"class":102},[52,569,570,572,574,576,578,580],{"class":54,"line":214},[52,571,217],{"class":62},[52,573,80],{"class":79},[52,575,222],{"class":62},[52,577,186],{"class":102},[52,579,227],{"class":189},[52,581,230],{"class":102},[52,583,584],{"class":54,"line":233},[52,585,70],{"emptyLinePlaceholder":69},[52,587,588,590,592,594,596,598,600,602,604,606,608,610],{"class":54,"line":238},[52,589,242],{"class":241},[52,591,193],{"class":102},[52,593,247],{"class":189},[52,595,250],{"class":102},[52,597,115],{"class":83},[52,599,153],{"class":87},[52,601,115],{"class":83},[52,603,259],{"class":102},[52,605,115],{"class":83},[52,607,264],{"class":87},[52,609,115],{"class":83},[52,611,269],{"class":102},[52,613,614,616,618,620,622,624,626,628,630,632,634,636],{"class":54,"line":272},[52,615,242],{"class":241},[52,617,193],{"class":102},[52,619,247],{"class":189},[52,621,250],{"class":102},[52,623,115],{"class":83},[52,625,153],{"class":87},[52,627,115],{"class":83},[52,629,259],{"class":102},[52,631,115],{"class":83},[52,633,293],{"class":87},[52,635,115],{"class":83},[52,637,269],{"class":102},[639,640,642],"h3",{"id":641},"_1-import-requests","1. Import requests",[42,644,646],{"className":44,"code":645,"language":46,"meta":47,"style":47},"import requests\n",[49,647,648],{"__ignoreMap":47},[52,649,650,652],{"class":54,"line":55},[52,651,59],{"class":58},[52,653,63],{"class":62},[14,655,656,657,659],{},"This gives you access to the ",[49,658,303],{}," package so you can send HTTP requests.",[639,661,663],{"id":662},"_2-set-the-api-url","2. Set the API URL",[42,665,667],{"className":44,"code":666,"language":46,"meta":47,"style":47},"url = \"https:\u002F\u002Fapi.open-meteo.com\u002Fv1\u002Fforecast\"\n",[49,668,669],{"__ignoreMap":47},[52,670,671,673,675,677,679],{"class":54,"line":55},[52,672,76],{"class":62},[52,674,80],{"class":79},[52,676,84],{"class":83},[52,678,88],{"class":87},[52,680,91],{"class":83},[14,682,683],{},"This is the API endpoint you are calling.",[639,685,687],{"id":686},"_3-create-a-params-dictionary","3. Create a params dictionary",[42,689,691],{"className":44,"code":690,"language":46,"meta":47,"style":47},"params = {\n    \"latitude\": 40.7128,\n    \"longitude\": -74.0060,\n    \"current_weather\": True\n}\n",[49,692,693,701,715,731,743],{"__ignoreMap":47},[52,694,695,697,699],{"class":54,"line":55},[52,696,97],{"class":62},[52,698,80],{"class":79},[52,700,103],{"class":102},[52,702,703,705,707,709,711,713],{"class":54,"line":66},[52,704,109],{"class":83},[52,706,112],{"class":87},[52,708,115],{"class":83},[52,710,118],{"class":102},[52,712,122],{"class":121},[52,714,125],{"class":102},[52,716,717,719,721,723,725,727,729],{"class":54,"line":73},[52,718,109],{"class":83},[52,720,133],{"class":87},[52,722,115],{"class":83},[52,724,118],{"class":102},[52,726,140],{"class":79},[52,728,143],{"class":121},[52,730,125],{"class":102},[52,732,733,735,737,739,741],{"class":54,"line":94},[52,734,109],{"class":83},[52,736,153],{"class":87},[52,738,115],{"class":83},[52,740,118],{"class":102},[52,742,161],{"class":160},[52,744,745],{"class":54,"line":106},[52,746,167],{"class":102},[14,748,749],{},"This dictionary holds the query parameters sent with the request.",[14,751,752],{},"In this example:",[309,754,755,763],{},[24,756,757,759,760,762],{},[49,758,112],{}," and ",[49,761,133],{}," choose the location",[24,764,765,767],{},[49,766,153],{}," asks for current weather data",[639,769,771,772,775],{"id":770},"_4-call-requestsget-with-the-url-and-params","4. Call ",[49,773,774],{},"requests.get()"," with the URL and params",[42,777,779],{"className":44,"code":778,"language":46,"meta":47,"style":47},"response = requests.get(url, params=params)\n",[49,780,781],{"__ignoreMap":47},[52,782,783,785,787,789,791,793,795,797,799,801,803,805],{"class":54,"line":55},[52,784,178],{"class":62},[52,786,80],{"class":79},[52,788,183],{"class":62},[52,790,186],{"class":102},[52,792,190],{"class":189},[52,794,193],{"class":102},[52,796,196],{"class":189},[52,798,199],{"class":102},[52,800,203],{"class":202},[52,802,80],{"class":79},[52,804,208],{"class":189},[52,806,211],{"class":102},[14,808,809],{},"This sends a GET request to the API.",[14,811,812,813,816],{},"If you print ",[49,814,815],{},"response.url",", you can see the full URL with the parameters added to it.",[639,818,820],{"id":819},"_5-convert-the-response-to-python-data","5. Convert the response to Python data",[42,822,824],{"className":44,"code":823,"language":46,"meta":47,"style":47},"data = response.json()\n",[49,825,826],{"__ignoreMap":47},[52,827,828,830,832,834,836,838],{"class":54,"line":55},[52,829,217],{"class":62},[52,831,80],{"class":79},[52,833,222],{"class":62},[52,835,186],{"class":102},[52,837,227],{"class":189},[52,839,230],{"class":102},[14,841,842,843,846,849],{},"The API sends JSON text back.",[844,845],"br",{},[49,847,848],{},"response.json()"," converts that JSON into normal Python data, usually dictionaries and lists.",[14,851,852,853,857,858,186],{},"If you want a deeper explanation, read ",[372,854,856],{"href":855},"\u002Fhow-to\u002Fhow-to-parse-json-in-python\u002F","how to parse JSON in Python"," or ",[372,859,861],{"href":860},"\u002Fstandard-library\u002Fpython-json-module-overview\u002F","Python JSON module overview",[639,863,865],{"id":864},"_6-access-nested-dictionary-values","6. Access nested dictionary values",[42,867,869],{"className":44,"code":868,"language":46,"meta":47,"style":47},"print(data[\"current_weather\"][\"temperature\"])\nprint(data[\"current_weather\"][\"windspeed\"])\n",[49,870,871,897],{"__ignoreMap":47},[52,872,873,875,877,879,881,883,885,887,889,891,893,895],{"class":54,"line":55},[52,874,242],{"class":241},[52,876,193],{"class":102},[52,878,247],{"class":189},[52,880,250],{"class":102},[52,882,115],{"class":83},[52,884,153],{"class":87},[52,886,115],{"class":83},[52,888,259],{"class":102},[52,890,115],{"class":83},[52,892,264],{"class":87},[52,894,115],{"class":83},[52,896,269],{"class":102},[52,898,899,901,903,905,907,909,911,913,915,917,919,921],{"class":54,"line":66},[52,900,242],{"class":241},[52,902,193],{"class":102},[52,904,247],{"class":189},[52,906,250],{"class":102},[52,908,115],{"class":83},[52,910,153],{"class":87},[52,912,115],{"class":83},[52,914,259],{"class":102},[52,916,115],{"class":83},[52,918,293],{"class":87},[52,920,115],{"class":83},[52,922,269],{"class":102},[14,924,925],{},"The response contains a top-level dictionary.",[14,927,928,929,931,932,934,935,759,937,939],{},"Inside it, ",[49,930,153],{}," is another dictionary.",[844,933],{},"\nInside that dictionary, ",[49,936,264],{},[49,938,293],{}," are keys.",[639,941,943],{"id":942},"_7-print-the-result","7. Print the result",[14,945,946],{},"The program prints the values returned by the API.",[14,948,949],{},"Your output will vary depending on the real weather, but it may look something like this:",[42,951,956],{"className":952,"code":954,"language":955,"meta":47},[953],"language-text","22.4\n13.1\n","text",[49,957,954],{"__ignoreMap":47},[37,959,961],{"id":960},"understanding-the-json-response","Understanding the JSON response",[14,963,964,965,967],{},"The API returns JSON text. After you call ",[49,966,848],{},", you get Python data you can work with.",[14,969,970],{},"A simplified response might look like this:",[42,972,974],{"className":44,"code":973,"language":46,"meta":47,"style":47},"{\n    \"latitude\": 40.71,\n    \"longitude\": -74.01,\n    \"current_weather\": {\n        \"temperature\": 22.4,\n        \"windspeed\": 13.1,\n        \"winddirection\": 240,\n        \"weathercode\": 3\n    }\n}\n",[49,975,976,981,996,1013,1025,1041,1056,1072,1086,1091],{"__ignoreMap":47},[52,977,978],{"class":54,"line":55},[52,979,980],{"class":102},"{\n",[52,982,983,985,987,989,991,994],{"class":54,"line":66},[52,984,109],{"class":83},[52,986,112],{"class":87},[52,988,115],{"class":83},[52,990,118],{"class":102},[52,992,993],{"class":121}," 40.71",[52,995,125],{"class":102},[52,997,998,1000,1002,1004,1006,1008,1011],{"class":54,"line":73},[52,999,109],{"class":83},[52,1001,133],{"class":87},[52,1003,115],{"class":83},[52,1005,118],{"class":102},[52,1007,140],{"class":79},[52,1009,1010],{"class":121},"74.01",[52,1012,125],{"class":102},[52,1014,1015,1017,1019,1021,1023],{"class":54,"line":94},[52,1016,109],{"class":83},[52,1018,153],{"class":87},[52,1020,115],{"class":83},[52,1022,118],{"class":102},[52,1024,103],{"class":102},[52,1026,1027,1030,1032,1034,1036,1039],{"class":54,"line":106},[52,1028,1029],{"class":83},"        \"",[52,1031,264],{"class":87},[52,1033,115],{"class":83},[52,1035,118],{"class":102},[52,1037,1038],{"class":121}," 22.4",[52,1040,125],{"class":102},[52,1042,1043,1045,1047,1049,1051,1054],{"class":54,"line":128},[52,1044,1029],{"class":83},[52,1046,293],{"class":87},[52,1048,115],{"class":83},[52,1050,118],{"class":102},[52,1052,1053],{"class":121}," 13.1",[52,1055,125],{"class":102},[52,1057,1058,1060,1063,1065,1067,1070],{"class":54,"line":148},[52,1059,1029],{"class":83},[52,1061,1062],{"class":87},"winddirection",[52,1064,115],{"class":83},[52,1066,118],{"class":102},[52,1068,1069],{"class":121}," 240",[52,1071,125],{"class":102},[52,1073,1074,1076,1079,1081,1083],{"class":54,"line":164},[52,1075,1029],{"class":83},[52,1077,1078],{"class":87},"weathercode",[52,1080,115],{"class":83},[52,1082,118],{"class":102},[52,1084,1085],{"class":121}," 3\n",[52,1087,1088],{"class":54,"line":170},[52,1089,1090],{"class":102},"    }\n",[52,1092,1093],{"class":54,"line":175},[52,1094,167],{"class":102},[14,1096,1097],{},"Important parts:",[309,1099,1100,1103,1108],{},[24,1101,1102],{},"The whole response is a dictionary",[24,1104,1105,1107],{},[49,1106,153],{}," is a dictionary inside the response",[24,1109,1110,759,1112,1114,1115],{},[49,1111,264],{},[49,1113,293],{}," are keys inside ",[49,1116,153],{},[14,1118,1119],{},"So this line:",[42,1121,1123],{"className":44,"code":1122,"language":46,"meta":47,"style":47},"data[\"current_weather\"][\"temperature\"]\n",[49,1124,1125],{"__ignoreMap":47},[52,1126,1127,1129,1131,1133,1135,1137,1139,1141,1143,1145],{"class":54,"line":55},[52,1128,247],{"class":62},[52,1130,250],{"class":102},[52,1132,115],{"class":83},[52,1134,153],{"class":87},[52,1136,115],{"class":83},[52,1138,259],{"class":102},[52,1140,115],{"class":83},[52,1142,264],{"class":87},[52,1144,115],{"class":83},[52,1146,1147],{"class":102},"]\n",[14,1149,1150],{},"means:",[309,1152,1153,1159],{},[24,1154,1155,1156],{},"get the value for ",[49,1157,1158],{},"\"current_weather\"",[24,1160,1161,1162,1165],{},"then get the value for ",[49,1163,1164],{},"\"temperature\""," inside it",[14,1167,1168,1169,186],{},"If you want more practice reading API data, see ",[372,1170,1172],{"href":1171},"\u002Fhow-to\u002Fhow-to-handle-api-responses-in-python\u002F","how to handle API responses in Python",[37,1174,1176],{"id":1175},"simple-improvements-to-the-example","Simple improvements to the example",[14,1178,1179],{},"Here is a slightly better version that prints labels, rounds values, and checks the status code first:",[42,1181,1183],{"className":44,"code":1182,"language":46,"meta":47,"style":47},"import requests\n\ncity_name = \"New York\"\nurl = \"https:\u002F\u002Fapi.open-meteo.com\u002Fv1\u002Fforecast\"\nparams = {\n    \"latitude\": 40.7128,\n    \"longitude\": -74.0060,\n    \"current_weather\": True\n}\n\nresponse = requests.get(url, params=params)\n\nif response.status_code == 200:\n    data = response.json()\n    current_weather = data.get(\"current_weather\", {})\n\n    temperature = current_weather.get(\"temperature\")\n    windspeed = current_weather.get(\"windspeed\")\n\n    if temperature is not None and windspeed is not None:\n        print(f\"Weather for {city_name}\")\n        print(f\"Temperature: {round(temperature, 1)}°C\")\n        print(f\"Wind Speed: {round(windspeed, 1)} km\u002Fh\")\n    else:\n        print(\"Weather data is missing expected values.\")\nelse:\n    print(\"Request failed.\")\n    print(\"Status code:\", response.status_code)\n",[49,1184,1185,1191,1195,1209,1221,1229,1243,1259,1271,1275,1279,1305,1309,1331,1346,1374,1379,1404,1428,1433,1465,1493,1529,1562,1570,1586,1594,1611],{"__ignoreMap":47},[52,1186,1187,1189],{"class":54,"line":55},[52,1188,59],{"class":58},[52,1190,63],{"class":62},[52,1192,1193],{"class":54,"line":66},[52,1194,70],{"emptyLinePlaceholder":69},[52,1196,1197,1200,1202,1204,1207],{"class":54,"line":73},[52,1198,1199],{"class":62},"city_name ",[52,1201,80],{"class":79},[52,1203,84],{"class":83},[52,1205,1206],{"class":87},"New York",[52,1208,91],{"class":83},[52,1210,1211,1213,1215,1217,1219],{"class":54,"line":94},[52,1212,76],{"class":62},[52,1214,80],{"class":79},[52,1216,84],{"class":83},[52,1218,88],{"class":87},[52,1220,91],{"class":83},[52,1222,1223,1225,1227],{"class":54,"line":106},[52,1224,97],{"class":62},[52,1226,80],{"class":79},[52,1228,103],{"class":102},[52,1230,1231,1233,1235,1237,1239,1241],{"class":54,"line":128},[52,1232,109],{"class":83},[52,1234,112],{"class":87},[52,1236,115],{"class":83},[52,1238,118],{"class":102},[52,1240,122],{"class":121},[52,1242,125],{"class":102},[52,1244,1245,1247,1249,1251,1253,1255,1257],{"class":54,"line":148},[52,1246,109],{"class":83},[52,1248,133],{"class":87},[52,1250,115],{"class":83},[52,1252,118],{"class":102},[52,1254,140],{"class":79},[52,1256,143],{"class":121},[52,1258,125],{"class":102},[52,1260,1261,1263,1265,1267,1269],{"class":54,"line":164},[52,1262,109],{"class":83},[52,1264,153],{"class":87},[52,1266,115],{"class":83},[52,1268,118],{"class":102},[52,1270,161],{"class":160},[52,1272,1273],{"class":54,"line":170},[52,1274,167],{"class":102},[52,1276,1277],{"class":54,"line":175},[52,1278,70],{"emptyLinePlaceholder":69},[52,1280,1281,1283,1285,1287,1289,1291,1293,1295,1297,1299,1301,1303],{"class":54,"line":214},[52,1282,178],{"class":62},[52,1284,80],{"class":79},[52,1286,183],{"class":62},[52,1288,186],{"class":102},[52,1290,190],{"class":189},[52,1292,193],{"class":102},[52,1294,196],{"class":189},[52,1296,199],{"class":102},[52,1298,203],{"class":202},[52,1300,80],{"class":79},[52,1302,208],{"class":189},[52,1304,211],{"class":102},[52,1306,1307],{"class":54,"line":233},[52,1308,70],{"emptyLinePlaceholder":69},[52,1310,1311,1314,1316,1318,1322,1325,1328],{"class":54,"line":238},[52,1312,1313],{"class":58},"if",[52,1315,222],{"class":62},[52,1317,186],{"class":102},[52,1319,1321],{"class":1320},"skxfh","status_code",[52,1323,1324],{"class":79}," ==",[52,1326,1327],{"class":121}," 200",[52,1329,1330],{"class":102},":\n",[52,1332,1333,1336,1338,1340,1342,1344],{"class":54,"line":272},[52,1334,1335],{"class":62},"    data ",[52,1337,80],{"class":79},[52,1339,222],{"class":62},[52,1341,186],{"class":102},[52,1343,227],{"class":189},[52,1345,230],{"class":102},[52,1347,1349,1352,1354,1357,1359,1361,1363,1365,1367,1369,1371],{"class":54,"line":1348},15,[52,1350,1351],{"class":62},"    current_weather ",[52,1353,80],{"class":79},[52,1355,1356],{"class":62}," data",[52,1358,186],{"class":102},[52,1360,190],{"class":189},[52,1362,193],{"class":102},[52,1364,115],{"class":83},[52,1366,153],{"class":87},[52,1368,115],{"class":83},[52,1370,199],{"class":102},[52,1372,1373],{"class":102}," {})\n",[52,1375,1377],{"class":54,"line":1376},16,[52,1378,70],{"emptyLinePlaceholder":69},[52,1380,1382,1385,1387,1390,1392,1394,1396,1398,1400,1402],{"class":54,"line":1381},17,[52,1383,1384],{"class":62},"    temperature ",[52,1386,80],{"class":79},[52,1388,1389],{"class":62}," current_weather",[52,1391,186],{"class":102},[52,1393,190],{"class":189},[52,1395,193],{"class":102},[52,1397,115],{"class":83},[52,1399,264],{"class":87},[52,1401,115],{"class":83},[52,1403,211],{"class":102},[52,1405,1407,1410,1412,1414,1416,1418,1420,1422,1424,1426],{"class":54,"line":1406},18,[52,1408,1409],{"class":62},"    windspeed ",[52,1411,80],{"class":79},[52,1413,1389],{"class":62},[52,1415,186],{"class":102},[52,1417,190],{"class":189},[52,1419,193],{"class":102},[52,1421,115],{"class":83},[52,1423,293],{"class":87},[52,1425,115],{"class":83},[52,1427,211],{"class":102},[52,1429,1431],{"class":54,"line":1430},19,[52,1432,70],{"emptyLinePlaceholder":69},[52,1434,1436,1439,1442,1445,1448,1451,1454,1457,1459,1461,1463],{"class":54,"line":1435},20,[52,1437,1438],{"class":58},"    if",[52,1440,1441],{"class":62}," temperature ",[52,1443,1444],{"class":79},"is",[52,1446,1447],{"class":79}," not",[52,1449,1450],{"class":160}," None",[52,1452,1453],{"class":79}," and",[52,1455,1456],{"class":62}," windspeed ",[52,1458,1444],{"class":79},[52,1460,1447],{"class":79},[52,1462,1450],{"class":160},[52,1464,1330],{"class":102},[52,1466,1468,1471,1473,1477,1480,1483,1486,1489,1491],{"class":54,"line":1467},21,[52,1469,1470],{"class":241},"        print",[52,1472,193],{"class":102},[52,1474,1476],{"class":1475},"sbsja","f",[52,1478,1479],{"class":87},"\"Weather for ",[52,1481,1482],{"class":121},"{",[52,1484,1485],{"class":189},"city_name",[52,1487,1488],{"class":121},"}",[52,1490,115],{"class":87},[52,1492,211],{"class":102},[52,1494,1496,1498,1500,1502,1505,1507,1510,1512,1514,1516,1519,1522,1524,1527],{"class":54,"line":1495},22,[52,1497,1470],{"class":241},[52,1499,193],{"class":102},[52,1501,1476],{"class":1475},[52,1503,1504],{"class":87},"\"Temperature: ",[52,1506,1482],{"class":121},[52,1508,1509],{"class":241},"round",[52,1511,193],{"class":102},[52,1513,264],{"class":189},[52,1515,199],{"class":102},[52,1517,1518],{"class":121}," 1",[52,1520,1521],{"class":102},")",[52,1523,1488],{"class":121},[52,1525,1526],{"class":87},"°C\"",[52,1528,211],{"class":102},[52,1530,1532,1534,1536,1538,1541,1543,1545,1547,1549,1551,1553,1555,1557,1560],{"class":54,"line":1531},23,[52,1533,1470],{"class":241},[52,1535,193],{"class":102},[52,1537,1476],{"class":1475},[52,1539,1540],{"class":87},"\"Wind Speed: ",[52,1542,1482],{"class":121},[52,1544,1509],{"class":241},[52,1546,193],{"class":102},[52,1548,293],{"class":189},[52,1550,199],{"class":102},[52,1552,1518],{"class":121},[52,1554,1521],{"class":102},[52,1556,1488],{"class":121},[52,1558,1559],{"class":87}," km\u002Fh\"",[52,1561,211],{"class":102},[52,1563,1565,1568],{"class":54,"line":1564},24,[52,1566,1567],{"class":58},"    else",[52,1569,1330],{"class":102},[52,1571,1573,1575,1577,1579,1582,1584],{"class":54,"line":1572},25,[52,1574,1470],{"class":241},[52,1576,193],{"class":102},[52,1578,115],{"class":83},[52,1580,1581],{"class":87},"Weather data is missing expected values.",[52,1583,115],{"class":83},[52,1585,211],{"class":102},[52,1587,1589,1592],{"class":54,"line":1588},26,[52,1590,1591],{"class":58},"else",[52,1593,1330],{"class":102},[52,1595,1597,1600,1602,1604,1607,1609],{"class":54,"line":1596},27,[52,1598,1599],{"class":241},"    print",[52,1601,193],{"class":102},[52,1603,115],{"class":83},[52,1605,1606],{"class":87},"Request failed.",[52,1608,115],{"class":83},[52,1610,211],{"class":102},[52,1612,1614,1616,1618,1620,1623,1625,1627,1629,1631,1633],{"class":54,"line":1613},28,[52,1615,1599],{"class":241},[52,1617,193],{"class":102},[52,1619,115],{"class":83},[52,1621,1622],{"class":87},"Status code:",[52,1624,115],{"class":83},[52,1626,199],{"class":102},[52,1628,222],{"class":189},[52,1630,186],{"class":102},[52,1632,1321],{"class":1320},[52,1634,211],{"class":102},[14,1636,1637],{},"This version improves the original example by:",[309,1639,1640,1643,1646,1649,1652],{},[24,1641,1642],{},"Storing the city name separately for display",[24,1644,1645],{},"Printing clear labels",[24,1647,1648],{},"Rounding values before printing",[24,1650,1651],{},"Checking the status code before using the response",[24,1653,1654,1655],{},"Handling missing keys more safely with ",[49,1656,1657],{},".get()",[37,1659,1661],{"id":1660},"common-problems-when-this-example-fails","Common problems when this example fails",[14,1663,1664],{},"Some common causes are:",[309,1666,1667,1672,1675,1678,1681],{},[24,1668,1669,1671],{},[49,1670,303],{}," is not installed",[24,1673,1674],{},"There is no internet connection",[24,1676,1677],{},"You used the wrong key name when reading the JSON",[24,1679,1680],{},"You switched to an API that requires an API key",[24,1682,1683],{},"You assumed every weather API uses the same JSON structure",[14,1685,1686],{},"You may also run into these specific errors:",[309,1688,1689,1697,1703,1709,1712],{},[24,1690,1691,1694,1695,1671],{},[49,1692,1693],{},"ModuleNotFoundError"," because ",[49,1696,303],{},[24,1698,1699,1702],{},[49,1700,1701],{},"KeyError"," because the expected JSON key is missing",[24,1704,1705,1708],{},[49,1706,1707],{},"TypeError"," from treating a list like a dictionary",[24,1710,1711],{},"Connection-related request failure due to network issues",[24,1713,1714],{},"An authentication error when using an API that needs a key",[14,1716,1717,1718,186,1722,1724,1725,1727,1728,186],{},"If you see a missing-key problem, read ",[372,1719,1721],{"href":1720},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes\u002F","KeyError in Python: causes and fixes",[844,1723],{},"\nIf Python cannot import ",[49,1726,303],{},", see ",[372,1729,1731],{"href":1730},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix\u002F","ModuleNotFoundError: no module named X fix",[37,1733,1735],{"id":1734},"beginner-debugging-steps","Beginner debugging steps",[14,1737,1738],{},"If the example does not work, try these steps.",[639,1740,1742],{"id":1741},"print-the-status-code","Print the status code",[42,1744,1746],{"className":44,"code":1745,"language":46,"meta":47,"style":47},"print(response.status_code)\n",[49,1747,1748],{"__ignoreMap":47},[52,1749,1750,1752,1754,1757,1759,1761],{"class":54,"line":55},[52,1751,242],{"class":241},[52,1753,193],{"class":102},[52,1755,1756],{"class":189},"response",[52,1758,186],{"class":102},[52,1760,1321],{"class":1320},[52,1762,211],{"class":102},[14,1764,1765],{},"This tells you whether the request succeeded.",[639,1767,1769],{"id":1768},"print-the-raw-response-text","Print the raw response text",[42,1771,1773],{"className":44,"code":1772,"language":46,"meta":47,"style":47},"print(response.text)\n",[49,1774,1775],{"__ignoreMap":47},[52,1776,1777,1779,1781,1783,1785,1787],{"class":54,"line":55},[52,1778,242],{"class":241},[52,1780,193],{"class":102},[52,1782,1756],{"class":189},[52,1784,186],{"class":102},[52,1786,955],{"class":1320},[52,1788,211],{"class":102},[14,1790,1791],{},"This helps you inspect the exact data returned by the API.",[639,1793,1795],{"id":1794},"print-the-top-level-keys","Print the top-level keys",[42,1797,1799],{"className":44,"code":1798,"language":46,"meta":47,"style":47},"print(data.keys())\n",[49,1800,1801],{"__ignoreMap":47},[52,1802,1803,1805,1807,1809,1811,1814],{"class":54,"line":55},[52,1804,242],{"class":241},[52,1806,193],{"class":102},[52,1808,247],{"class":189},[52,1810,186],{"class":102},[52,1812,1813],{"class":189},"keys",[52,1815,1816],{"class":102},"())\n",[14,1818,1819],{},"This shows which keys exist at the top level of the response.",[639,1821,1823,1824,1826],{"id":1822},"print-the-current_weather-section","Print the ",[49,1825,153],{}," section",[42,1828,1830],{"className":44,"code":1829,"language":46,"meta":47,"style":47},"print(data[\"current_weather\"])\n",[49,1831,1832],{"__ignoreMap":47},[52,1833,1834,1836,1838,1840,1842,1844,1846,1848],{"class":54,"line":55},[52,1835,242],{"class":241},[52,1837,193],{"class":102},[52,1839,247],{"class":189},[52,1841,250],{"class":102},[52,1843,115],{"class":83},[52,1845,153],{"class":87},[52,1847,115],{"class":83},[52,1849,269],{"class":102},[14,1851,1852],{},"Only do this if that key exists.",[639,1854,1856],{"id":1855},"compare-your-key-names-with-the-real-response","Compare your key names with the real response",[14,1858,1859],{},"A very common beginner mistake is using a key name that does not exactly match the API response.",[14,1861,1862],{},"For example, this will fail if the key is not present:",[42,1864,1866],{"className":44,"code":1865,"language":46,"meta":47,"style":47},"print(data[\"weather\"][\"temp\"])\n",[49,1867,1868],{"__ignoreMap":47},[52,1869,1870,1872,1874,1876,1878,1880,1883,1885,1887,1889,1892,1894],{"class":54,"line":55},[52,1871,242],{"class":241},[52,1873,193],{"class":102},[52,1875,247],{"class":189},[52,1877,250],{"class":102},[52,1879,115],{"class":83},[52,1881,1882],{"class":87},"weather",[52,1884,115],{"class":83},[52,1886,259],{"class":102},[52,1888,115],{"class":83},[52,1890,1891],{"class":87},"temp",[52,1893,115],{"class":83},[52,1895,269],{"class":102},[14,1897,1898],{},"Always compare your code with the actual JSON returned by the API.",[14,1900,1901],{},"Useful install and check commands:",[42,1903,1905],{"className":390,"code":1904,"language":392,"meta":47,"style":47},"pip install requests\npython -m pip install requests\npython -c \"import requests; print(requests.__version__)\"\n",[49,1906,1907,1915,1927],{"__ignoreMap":47},[52,1908,1909,1911,1913],{"class":54,"line":55},[52,1910,400],{"class":399},[52,1912,403],{"class":87},[52,1914,63],{"class":87},[52,1916,1917,1919,1921,1923,1925],{"class":54,"line":66},[52,1918,46],{"class":399},[52,1920,421],{"class":420},[52,1922,424],{"class":87},[52,1924,403],{"class":87},[52,1926,63],{"class":87},[52,1928,1929,1931,1933,1935,1937],{"class":54,"line":73},[52,1930,46],{"class":399},[52,1932,443],{"class":420},[52,1934,84],{"class":83},[52,1936,448],{"class":87},[52,1938,91],{"class":83},[37,1940,1942],{"id":1941},"faq","FAQ",[639,1944,1946],{"id":1945},"do-i-need-an-api-key-for-this-example","Do I need an API key for this example?",[14,1948,1949],{},"No. The Open-Meteo version shown here does not require an API key.",[14,1951,1952],{},"Some other weather APIs do require one.",[639,1954,1956,1957,1959],{"id":1955},"why-use-requestsget-instead-of-opening-a-url-manually","Why use ",[49,1958,774],{}," instead of opening a URL manually?",[14,1961,1962,1964],{},[49,1963,774],{}," is easier for beginners.",[14,1966,1967],{},"It makes it simple to:",[309,1969,1970,1973,1976],{},[24,1971,1972],{},"send query parameters",[24,1974,1975],{},"read the response",[24,1977,1978],{},"work with JSON data",[639,1980,1982,1983,1985],{"id":1981},"what-type-of-data-does-responsejson-return","What type of data does ",[49,1984,848],{}," return?",[14,1987,1988],{},"Usually Python dictionaries and lists, depending on the JSON structure.",[639,1990,1992,1993,1995],{"id":1991},"why-does-my-code-raise-keyerror","Why does my code raise ",[49,1994,1701],{},"?",[14,1997,1998],{},"The key name in your code may not match the real API response.",[14,2000,2001],{},"Print the response and inspect the keys before trying to read nested values.",[37,2003,2005],{"id":2004},"see-also","See also",[309,2007,2008,2013,2018,2023,2029],{},[24,2009,2010],{},[372,2011,2012],{"href":374},"How to make an API request in Python",[24,2014,2015],{},[372,2016,2017],{"href":1171},"How to handle API responses in Python",[24,2019,2020],{},[372,2021,2022],{"href":855},"How to parse JSON in Python",[24,2024,2025],{},[372,2026,2028],{"href":2027},"\u002Fexamples\u002Fpython-api-request-example-get-request\u002F","Python API request example: GET request",[24,2030,2031],{},[372,2032,1721],{"href":1720},[2034,2035,2036],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .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 .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 .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 .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 .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 .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 .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}",{"title":47,"searchDepth":66,"depth":66,"links":2038},[2039,2040,2041,2042,2043,2053,2054,2055,2056,2064,2073],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":346,"depth":66,"text":347},{"id":378,"depth":66,"text":379},{"id":453,"depth":66,"text":454,"children":2044},[2045,2046,2047,2048,2050,2051,2052],{"id":641,"depth":73,"text":642},{"id":662,"depth":73,"text":663},{"id":686,"depth":73,"text":687},{"id":770,"depth":73,"text":2049},"4. Call requests.get() with the URL and params",{"id":819,"depth":73,"text":820},{"id":864,"depth":73,"text":865},{"id":942,"depth":73,"text":943},{"id":960,"depth":66,"text":961},{"id":1175,"depth":66,"text":1176},{"id":1660,"depth":66,"text":1661},{"id":1734,"depth":66,"text":1735,"children":2057},[2058,2059,2060,2061,2063],{"id":1741,"depth":73,"text":1742},{"id":1768,"depth":73,"text":1769},{"id":1794,"depth":73,"text":1795},{"id":1822,"depth":73,"text":2062},"Print the current_weather section",{"id":1855,"depth":73,"text":1856},{"id":1941,"depth":66,"text":1942,"children":2065},[2066,2067,2069,2071],{"id":1945,"depth":73,"text":1946},{"id":1955,"depth":73,"text":2068},"Why use requests.get() instead of opening a URL manually?",{"id":1981,"depth":73,"text":2070},"What type of data does response.json() return?",{"id":1991,"depth":73,"text":2072},"Why does my code raise KeyError?",{"id":2004,"depth":66,"text":2005},"Master python weather api example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-weather-api-example",{"title":5,"description":2074},"examples\u002Fpython-weather-api-example","7eBGsyv6NuGDaw9g8nGi8sb8M17KEbtuPicWgN14bK4",[2082,3763,5031,6946,9075,10700,12975,14578,16581,18360,20842,22090,23714,25280,26962,28493,29990,32038,34545,36597,38129,39012,40409,42320,44281,46072,49060,51383,53413,56069,57548,59570,61259,62438,64190,66455,68043,70085,72039,73870,75433,77826,79301,81072,82724,85588,86979,88659,90509,92464],{"id":2083,"title":2084,"body":2085,"description":3757,"extension":2075,"meta":3758,"navigation":69,"path":3759,"seo":3760,"stem":3761,"__hash__":3762},"content\u002Fexamples\u002Fpython-api-post-request-example.md","Python API POST Request Example",{"type":7,"value":2086,"toc":3727},[2087,2090,2093,2102,2104,2244,2247,2253,2265,2269,2283,2287,2290,2307,2310,2313,2342,2346,2349,2507,2511,2717,2720,2731,2735,2741,2772,2775,2788,2794,2799,2846,2849,2857,2867,2873,2891,2894,2897,2902,2921,2924,2933,2937,2940,2962,2965,3179,3182,3187,3191,3194,3215,3218,3246,3252,3256,3259,3592,3596,3626,3628,3632,3635,3648,3657,3661,3664,3671,3674,3681,3684,3686,3718,3724],[10,2088,2084],{"id":2089},"python-api-post-request-example",[14,2091,2092],{},"This example shows how to send a POST request in Python, send data to an API, and read the response.",[14,2094,2095,2096,857,2100,186],{},"The goal is to give you one simple working example you can run right away. If you want a more general guide later, see ",[372,2097,2099],{"href":2098},"\u002Fhow-to\u002Fhow-to-send-a-post-request-in-python\u002F","how to send a POST request in Python",[372,2101,375],{"href":374},[37,2103,40],{"id":39},[42,2105,2107],{"className":44,"code":2106,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\npayload = {\n    \"name\": \"Alice\",\n    \"age\": 25\n}\n\nresponse = requests.post(url, json=payload)\n\nprint(response.status_code)\nprint(response.json())\n",[49,2108,2109,2115,2119,2132,2141,2161,2175,2179,2183,2212,2216,2230],{"__ignoreMap":47},[52,2110,2111,2113],{"class":54,"line":55},[52,2112,59],{"class":58},[52,2114,63],{"class":62},[52,2116,2117],{"class":54,"line":66},[52,2118,70],{"emptyLinePlaceholder":69},[52,2120,2121,2123,2125,2127,2130],{"class":54,"line":73},[52,2122,76],{"class":62},[52,2124,80],{"class":79},[52,2126,84],{"class":83},[52,2128,2129],{"class":87},"https:\u002F\u002Fhttpbin.org\u002Fpost",[52,2131,91],{"class":83},[52,2133,2134,2137,2139],{"class":54,"line":94},[52,2135,2136],{"class":62},"payload ",[52,2138,80],{"class":79},[52,2140,103],{"class":102},[52,2142,2143,2145,2148,2150,2152,2154,2157,2159],{"class":54,"line":106},[52,2144,109],{"class":83},[52,2146,2147],{"class":87},"name",[52,2149,115],{"class":83},[52,2151,118],{"class":102},[52,2153,84],{"class":83},[52,2155,2156],{"class":87},"Alice",[52,2158,115],{"class":83},[52,2160,125],{"class":102},[52,2162,2163,2165,2168,2170,2172],{"class":54,"line":128},[52,2164,109],{"class":83},[52,2166,2167],{"class":87},"age",[52,2169,115],{"class":83},[52,2171,118],{"class":102},[52,2173,2174],{"class":121}," 25\n",[52,2176,2177],{"class":54,"line":148},[52,2178,167],{"class":102},[52,2180,2181],{"class":54,"line":164},[52,2182,70],{"emptyLinePlaceholder":69},[52,2184,2185,2187,2189,2191,2193,2196,2198,2200,2202,2205,2207,2210],{"class":54,"line":170},[52,2186,178],{"class":62},[52,2188,80],{"class":79},[52,2190,183],{"class":62},[52,2192,186],{"class":102},[52,2194,2195],{"class":189},"post",[52,2197,193],{"class":102},[52,2199,196],{"class":189},[52,2201,199],{"class":102},[52,2203,2204],{"class":202}," json",[52,2206,80],{"class":79},[52,2208,2209],{"class":189},"payload",[52,2211,211],{"class":102},[52,2213,2214],{"class":54,"line":175},[52,2215,70],{"emptyLinePlaceholder":69},[52,2217,2218,2220,2222,2224,2226,2228],{"class":54,"line":214},[52,2219,242],{"class":241},[52,2221,193],{"class":102},[52,2223,1756],{"class":189},[52,2225,186],{"class":102},[52,2227,1321],{"class":1320},[52,2229,211],{"class":102},[52,2231,2232,2234,2236,2238,2240,2242],{"class":54,"line":233},[52,2233,242],{"class":241},[52,2235,193],{"class":102},[52,2237,1756],{"class":189},[52,2239,186],{"class":102},[52,2241,227],{"class":189},[52,2243,1816],{"class":102},[14,2245,2246],{},"This sends JSON data to a test API and prints the status code and returned JSON.",[14,2248,2249,2250,2252],{},"Install ",[49,2251,303],{}," first:",[42,2254,2255],{"className":390,"code":391,"language":392,"meta":47,"style":47},[49,2256,2257],{"__ignoreMap":47},[52,2258,2259,2261,2263],{"class":54,"line":55},[52,2260,400],{"class":399},[52,2262,403],{"class":87},[52,2264,63],{"class":87},[37,2266,2268],{"id":2267},"what-this-example-shows","What this example shows",[309,2270,2271,2274,2277,2280],{},[24,2272,2273],{},"How to send a POST request with Python",[24,2275,2276],{},"How to send data as JSON",[24,2278,2279],{},"How to read the server response",[24,2281,2282],{},"How to check whether the request worked",[37,2284,2286],{"id":2285},"before-you-run-the-code","Before you run the code",[14,2288,2289],{},"Make sure:",[309,2291,2292,2295,2302],{},[24,2293,2294],{},"Python is installed",[24,2296,358,2297,2299,2300],{},[49,2298,303],{}," package is installed with ",[49,2301,400],{},[24,2303,2304,2305],{},"You use a safe test endpoint like ",[49,2306,2129],{},[14,2308,2309],{},"A POST request is often used when you want to send new data to an API. For example, you might create a user, submit a form, or send a message.",[14,2311,2312],{},"Useful commands:",[42,2314,2316],{"className":390,"code":2315,"language":392,"meta":47,"style":47},"python --version\npip install requests\npip show requests\n",[49,2317,2318,2325,2333],{"__ignoreMap":47},[52,2319,2320,2322],{"class":54,"line":55},[52,2321,46],{"class":399},[52,2323,2324],{"class":420}," --version\n",[52,2326,2327,2329,2331],{"class":54,"line":66},[52,2328,400],{"class":399},[52,2330,403],{"class":87},[52,2332,63],{"class":87},[52,2334,2335,2337,2340],{"class":54,"line":73},[52,2336,400],{"class":399},[52,2338,2339],{"class":87}," show",[52,2341,63],{"class":87},[37,2343,2345],{"id":2344},"basic-post-request-example","Basic POST request example",[14,2347,2348],{},"Here is the same example again with a little more context:",[42,2350,2352],{"className":44,"code":2351,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\n\npayload = {\n    \"name\": \"Alice\",\n    \"age\": 25\n}\n\nresponse = requests.post(url, json=payload)\n\nprint(\"Status code:\", response.status_code)\nprint(\"Response JSON:\")\nprint(response.json())\n",[49,2353,2354,2360,2364,2376,2380,2388,2406,2418,2422,2426,2452,2456,2478,2493],{"__ignoreMap":47},[52,2355,2356,2358],{"class":54,"line":55},[52,2357,59],{"class":58},[52,2359,63],{"class":62},[52,2361,2362],{"class":54,"line":66},[52,2363,70],{"emptyLinePlaceholder":69},[52,2365,2366,2368,2370,2372,2374],{"class":54,"line":73},[52,2367,76],{"class":62},[52,2369,80],{"class":79},[52,2371,84],{"class":83},[52,2373,2129],{"class":87},[52,2375,91],{"class":83},[52,2377,2378],{"class":54,"line":94},[52,2379,70],{"emptyLinePlaceholder":69},[52,2381,2382,2384,2386],{"class":54,"line":106},[52,2383,2136],{"class":62},[52,2385,80],{"class":79},[52,2387,103],{"class":102},[52,2389,2390,2392,2394,2396,2398,2400,2402,2404],{"class":54,"line":128},[52,2391,109],{"class":83},[52,2393,2147],{"class":87},[52,2395,115],{"class":83},[52,2397,118],{"class":102},[52,2399,84],{"class":83},[52,2401,2156],{"class":87},[52,2403,115],{"class":83},[52,2405,125],{"class":102},[52,2407,2408,2410,2412,2414,2416],{"class":54,"line":148},[52,2409,109],{"class":83},[52,2411,2167],{"class":87},[52,2413,115],{"class":83},[52,2415,118],{"class":102},[52,2417,2174],{"class":121},[52,2419,2420],{"class":54,"line":164},[52,2421,167],{"class":102},[52,2423,2424],{"class":54,"line":170},[52,2425,70],{"emptyLinePlaceholder":69},[52,2427,2428,2430,2432,2434,2436,2438,2440,2442,2444,2446,2448,2450],{"class":54,"line":175},[52,2429,178],{"class":62},[52,2431,80],{"class":79},[52,2433,183],{"class":62},[52,2435,186],{"class":102},[52,2437,2195],{"class":189},[52,2439,193],{"class":102},[52,2441,196],{"class":189},[52,2443,199],{"class":102},[52,2445,2204],{"class":202},[52,2447,80],{"class":79},[52,2449,2209],{"class":189},[52,2451,211],{"class":102},[52,2453,2454],{"class":54,"line":214},[52,2455,70],{"emptyLinePlaceholder":69},[52,2457,2458,2460,2462,2464,2466,2468,2470,2472,2474,2476],{"class":54,"line":233},[52,2459,242],{"class":241},[52,2461,193],{"class":102},[52,2463,115],{"class":83},[52,2465,1622],{"class":87},[52,2467,115],{"class":83},[52,2469,199],{"class":102},[52,2471,222],{"class":189},[52,2473,186],{"class":102},[52,2475,1321],{"class":1320},[52,2477,211],{"class":102},[52,2479,2480,2482,2484,2486,2489,2491],{"class":54,"line":238},[52,2481,242],{"class":241},[52,2483,193],{"class":102},[52,2485,115],{"class":83},[52,2487,2488],{"class":87},"Response JSON:",[52,2490,115],{"class":83},[52,2492,211],{"class":102},[52,2494,2495,2497,2499,2501,2503,2505],{"class":54,"line":272},[52,2496,242],{"class":241},[52,2498,193],{"class":102},[52,2500,1756],{"class":189},[52,2502,186],{"class":102},[52,2504,227],{"class":189},[52,2506,1816],{"class":102},[639,2508,2510],{"id":2509},"example-output","Example output",[42,2512,2514],{"className":44,"code":2513,"language":46,"meta":47,"style":47},"Status code: 200\nResponse JSON:\n{\n    \"args\": {},\n    \"data\": \"{\\\"name\\\": \\\"Alice\\\", \\\"age\\\": 25}\",\n    \"files\": {},\n    \"form\": {},\n    \"headers\": {\n        ...\n    },\n    \"json\": {\n        \"age\": 25,\n        \"name\": \"Alice\"\n    },\n    \"url\": \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\n}\n",[49,2515,2516,2526,2537,2541,2555,2601,2614,2627,2640,2645,2650,2662,2677,2693,2697,2713],{"__ignoreMap":47},[52,2517,2518,2521,2523],{"class":54,"line":55},[52,2519,2520],{"class":62},"Status code",[52,2522,118],{"class":102},[52,2524,2525],{"class":121}," 200\n",[52,2527,2528,2531,2535],{"class":54,"line":66},[52,2529,2530],{"class":62},"Response ",[52,2532,2534],{"class":2533},"s_hVV","JSON",[52,2536,1330],{"class":102},[52,2538,2539],{"class":54,"line":73},[52,2540,980],{"class":102},[52,2542,2543,2545,2548,2550,2552],{"class":54,"line":94},[52,2544,109],{"class":83},[52,2546,2547],{"class":87},"args",[52,2549,115],{"class":83},[52,2551,118],{"class":102},[52,2553,2554],{"class":102}," {},\n",[52,2556,2557,2559,2561,2563,2565,2567,2569,2572,2574,2576,2579,2581,2583,2585,2588,2590,2592,2594,2597,2599],{"class":54,"line":106},[52,2558,109],{"class":83},[52,2560,247],{"class":87},[52,2562,115],{"class":83},[52,2564,118],{"class":102},[52,2566,84],{"class":83},[52,2568,1482],{"class":87},[52,2570,2571],{"class":2533},"\\\"",[52,2573,2147],{"class":87},[52,2575,2571],{"class":2533},[52,2577,2578],{"class":87},": ",[52,2580,2571],{"class":2533},[52,2582,2156],{"class":87},[52,2584,2571],{"class":2533},[52,2586,2587],{"class":87},", ",[52,2589,2571],{"class":2533},[52,2591,2167],{"class":87},[52,2593,2571],{"class":2533},[52,2595,2596],{"class":87},": 25}",[52,2598,115],{"class":83},[52,2600,125],{"class":102},[52,2602,2603,2605,2608,2610,2612],{"class":54,"line":128},[52,2604,109],{"class":83},[52,2606,2607],{"class":87},"files",[52,2609,115],{"class":83},[52,2611,118],{"class":102},[52,2613,2554],{"class":102},[52,2615,2616,2618,2621,2623,2625],{"class":54,"line":148},[52,2617,109],{"class":83},[52,2619,2620],{"class":87},"form",[52,2622,115],{"class":83},[52,2624,118],{"class":102},[52,2626,2554],{"class":102},[52,2628,2629,2631,2634,2636,2638],{"class":54,"line":164},[52,2630,109],{"class":83},[52,2632,2633],{"class":87},"headers",[52,2635,115],{"class":83},[52,2637,118],{"class":102},[52,2639,103],{"class":102},[52,2641,2642],{"class":54,"line":170},[52,2643,2644],{"class":2533},"        ...\n",[52,2646,2647],{"class":54,"line":175},[52,2648,2649],{"class":102},"    },\n",[52,2651,2652,2654,2656,2658,2660],{"class":54,"line":214},[52,2653,109],{"class":83},[52,2655,227],{"class":87},[52,2657,115],{"class":83},[52,2659,118],{"class":102},[52,2661,103],{"class":102},[52,2663,2664,2666,2668,2670,2672,2675],{"class":54,"line":233},[52,2665,1029],{"class":83},[52,2667,2167],{"class":87},[52,2669,115],{"class":83},[52,2671,118],{"class":102},[52,2673,2674],{"class":121}," 25",[52,2676,125],{"class":102},[52,2678,2679,2681,2683,2685,2687,2689,2691],{"class":54,"line":238},[52,2680,1029],{"class":83},[52,2682,2147],{"class":87},[52,2684,115],{"class":83},[52,2686,118],{"class":102},[52,2688,84],{"class":83},[52,2690,2156],{"class":87},[52,2692,91],{"class":83},[52,2694,2695],{"class":54,"line":272},[52,2696,2649],{"class":102},[52,2698,2699,2701,2703,2705,2707,2709,2711],{"class":54,"line":1348},[52,2700,109],{"class":83},[52,2702,196],{"class":87},[52,2704,115],{"class":83},[52,2706,118],{"class":102},[52,2708,84],{"class":83},[52,2710,2129],{"class":87},[52,2712,91],{"class":83},[52,2714,2715],{"class":54,"line":1376},[52,2716,167],{"class":102},[14,2718,2719],{},"The exact output may look a little different, but you should see:",[309,2721,2722,2728],{},[24,2723,2724,2725],{},"A status code such as ",[49,2726,2727],{},"200",[24,2729,2730],{},"Your sent data inside the returned JSON",[37,2732,2734],{"id":2733},"how-the-key-lines-work","How the key lines work",[639,2736,2738],{"id":2737},"requestspost",[49,2739,2740],{},"requests.post()",[42,2742,2744],{"className":44,"code":2743,"language":46,"meta":47,"style":47},"response = requests.post(url, json=payload)\n",[49,2745,2746],{"__ignoreMap":47},[52,2747,2748,2750,2752,2754,2756,2758,2760,2762,2764,2766,2768,2770],{"class":54,"line":55},[52,2749,178],{"class":62},[52,2751,80],{"class":79},[52,2753,183],{"class":62},[52,2755,186],{"class":102},[52,2757,2195],{"class":189},[52,2759,193],{"class":102},[52,2761,196],{"class":189},[52,2763,199],{"class":102},[52,2765,2204],{"class":202},[52,2767,80],{"class":79},[52,2769,2209],{"class":189},[52,2771,211],{"class":102},[14,2773,2774],{},"This sends a POST request to the server.",[309,2776,2777,2782],{},[24,2778,2779,2781],{},[49,2780,196],{}," is the API endpoint",[24,2783,2784,2787],{},[49,2785,2786],{},"json=payload"," sends your Python dictionary as JSON",[14,2789,2790,2791,186],{},"If you are new to JSON, see the ",[372,2792,2793],{"href":860},"Python json module overview",[639,2795,2797],{"id":2796},"jsonpayload",[49,2798,2786],{},[42,2800,2802],{"className":44,"code":2801,"language":46,"meta":47,"style":47},"payload = {\n    \"name\": \"Alice\",\n    \"age\": 25\n}\n",[49,2803,2804,2812,2830,2842],{"__ignoreMap":47},[52,2805,2806,2808,2810],{"class":54,"line":55},[52,2807,2136],{"class":62},[52,2809,80],{"class":79},[52,2811,103],{"class":102},[52,2813,2814,2816,2818,2820,2822,2824,2826,2828],{"class":54,"line":66},[52,2815,109],{"class":83},[52,2817,2147],{"class":87},[52,2819,115],{"class":83},[52,2821,118],{"class":102},[52,2823,84],{"class":83},[52,2825,2156],{"class":87},[52,2827,115],{"class":83},[52,2829,125],{"class":102},[52,2831,2832,2834,2836,2838,2840],{"class":54,"line":73},[52,2833,109],{"class":83},[52,2835,2167],{"class":87},[52,2837,115],{"class":83},[52,2839,118],{"class":102},[52,2841,2174],{"class":121},[52,2843,2844],{"class":54,"line":94},[52,2845,167],{"class":102},[14,2847,2848],{},"This is a normal Python dictionary.",[14,2850,2851,2852,2587,2854,2856],{},"When you pass it with ",[49,2853,2786],{},[49,2855,303],{}," converts it to JSON automatically before sending it.",[14,2858,2859,2860,186],{},"If you need to convert Python data to a JSON string yourself, see ",[372,2861,2863,2866],{"href":2862},"\u002Fstandard-library\u002Fjson.dumps-function-explained",[49,2864,2865],{},"json.dumps()"," explained",[639,2868,2870],{"id":2869},"responsestatus_code",[49,2871,2872],{},"response.status_code",[42,2874,2875],{"className":44,"code":1745,"language":46,"meta":47,"style":47},[49,2876,2877],{"__ignoreMap":47},[52,2878,2879,2881,2883,2885,2887,2889],{"class":54,"line":55},[52,2880,242],{"class":241},[52,2882,193],{"class":102},[52,2884,1756],{"class":189},[52,2886,186],{"class":102},[52,2888,1321],{"class":1320},[52,2890,211],{"class":102},[14,2892,2893],{},"This shows the HTTP status code from the server.",[14,2895,2896],{},"It helps you check whether the request worked.",[639,2898,2900],{"id":2899},"responsejson",[49,2901,848],{},[42,2903,2905],{"className":44,"code":2904,"language":46,"meta":47,"style":47},"print(response.json())\n",[49,2906,2907],{"__ignoreMap":47},[52,2908,2909,2911,2913,2915,2917,2919],{"class":54,"line":55},[52,2910,242],{"class":241},[52,2912,193],{"class":102},[52,2914,1756],{"class":189},[52,2916,186],{"class":102},[52,2918,227],{"class":189},[52,2920,1816],{"class":102},[14,2922,2923],{},"This converts the JSON response into a Python object, usually a dictionary.",[14,2925,2926,2927,186],{},"If you need to parse a JSON string manually, see ",[372,2928,2930,2866],{"href":2929},"\u002Fstandard-library\u002Fjson.loads-function-explained",[49,2931,2932],{},"json.loads()",[37,2934,2936],{"id":2935},"common-response-checks","Common response checks",[14,2938,2939],{},"When working with APIs, these status codes are common:",[309,2941,2942,2950,2956],{},[24,2943,2944,857,2946,2949],{},[49,2945,2727],{},[49,2947,2948],{},"201"," usually means success",[24,2951,2952,2955],{},[49,2953,2954],{},"400"," level codes usually mean your request has a problem",[24,2957,2958,2961],{},[49,2959,2960],{},"500"," level codes usually mean the server has a problem",[14,2963,2964],{},"A simple pattern is:",[42,2966,2968],{"className":44,"code":2967,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\npayload = {\"name\": \"Alice\", \"age\": 25}\n\nresponse = requests.post(url, json=payload)\n\nprint(\"Status code:\", response.status_code)\n\nif response.status_code in (200, 201):\n    print(\"Request worked\")\n    print(response.json())\nelse:\n    print(\"Request failed\")\n    print(response.text)\n",[49,2969,2970,2976,2980,2992,3029,3033,3059,3063,3085,3089,3115,3130,3144,3150,3165],{"__ignoreMap":47},[52,2971,2972,2974],{"class":54,"line":55},[52,2973,59],{"class":58},[52,2975,63],{"class":62},[52,2977,2978],{"class":54,"line":66},[52,2979,70],{"emptyLinePlaceholder":69},[52,2981,2982,2984,2986,2988,2990],{"class":54,"line":73},[52,2983,76],{"class":62},[52,2985,80],{"class":79},[52,2987,84],{"class":83},[52,2989,2129],{"class":87},[52,2991,91],{"class":83},[52,2993,2994,2996,2998,3001,3003,3005,3007,3009,3011,3013,3015,3017,3019,3021,3023,3025,3027],{"class":54,"line":94},[52,2995,2136],{"class":62},[52,2997,80],{"class":79},[52,2999,3000],{"class":102}," {",[52,3002,115],{"class":83},[52,3004,2147],{"class":87},[52,3006,115],{"class":83},[52,3008,118],{"class":102},[52,3010,84],{"class":83},[52,3012,2156],{"class":87},[52,3014,115],{"class":83},[52,3016,199],{"class":102},[52,3018,84],{"class":83},[52,3020,2167],{"class":87},[52,3022,115],{"class":83},[52,3024,118],{"class":102},[52,3026,2674],{"class":121},[52,3028,167],{"class":102},[52,3030,3031],{"class":54,"line":106},[52,3032,70],{"emptyLinePlaceholder":69},[52,3034,3035,3037,3039,3041,3043,3045,3047,3049,3051,3053,3055,3057],{"class":54,"line":128},[52,3036,178],{"class":62},[52,3038,80],{"class":79},[52,3040,183],{"class":62},[52,3042,186],{"class":102},[52,3044,2195],{"class":189},[52,3046,193],{"class":102},[52,3048,196],{"class":189},[52,3050,199],{"class":102},[52,3052,2204],{"class":202},[52,3054,80],{"class":79},[52,3056,2209],{"class":189},[52,3058,211],{"class":102},[52,3060,3061],{"class":54,"line":148},[52,3062,70],{"emptyLinePlaceholder":69},[52,3064,3065,3067,3069,3071,3073,3075,3077,3079,3081,3083],{"class":54,"line":164},[52,3066,242],{"class":241},[52,3068,193],{"class":102},[52,3070,115],{"class":83},[52,3072,1622],{"class":87},[52,3074,115],{"class":83},[52,3076,199],{"class":102},[52,3078,222],{"class":189},[52,3080,186],{"class":102},[52,3082,1321],{"class":1320},[52,3084,211],{"class":102},[52,3086,3087],{"class":54,"line":170},[52,3088,70],{"emptyLinePlaceholder":69},[52,3090,3091,3093,3095,3097,3099,3102,3105,3107,3109,3112],{"class":54,"line":175},[52,3092,1313],{"class":58},[52,3094,222],{"class":62},[52,3096,186],{"class":102},[52,3098,1321],{"class":1320},[52,3100,3101],{"class":79}," in",[52,3103,3104],{"class":102}," (",[52,3106,2727],{"class":121},[52,3108,199],{"class":102},[52,3110,3111],{"class":121}," 201",[52,3113,3114],{"class":102},"):\n",[52,3116,3117,3119,3121,3123,3126,3128],{"class":54,"line":214},[52,3118,1599],{"class":241},[52,3120,193],{"class":102},[52,3122,115],{"class":83},[52,3124,3125],{"class":87},"Request worked",[52,3127,115],{"class":83},[52,3129,211],{"class":102},[52,3131,3132,3134,3136,3138,3140,3142],{"class":54,"line":233},[52,3133,1599],{"class":241},[52,3135,193],{"class":102},[52,3137,1756],{"class":189},[52,3139,186],{"class":102},[52,3141,227],{"class":189},[52,3143,1816],{"class":102},[52,3145,3146,3148],{"class":54,"line":238},[52,3147,1591],{"class":58},[52,3149,1330],{"class":102},[52,3151,3152,3154,3156,3158,3161,3163],{"class":54,"line":272},[52,3153,1599],{"class":241},[52,3155,193],{"class":102},[52,3157,115],{"class":83},[52,3159,3160],{"class":87},"Request failed",[52,3162,115],{"class":83},[52,3164,211],{"class":102},[52,3166,3167,3169,3171,3173,3175,3177],{"class":54,"line":1348},[52,3168,1599],{"class":241},[52,3170,193],{"class":102},[52,3172,1756],{"class":189},[52,3174,186],{"class":102},[52,3176,955],{"class":1320},[52,3178,211],{"class":102},[14,3180,3181],{},"When debugging, always inspect the response body. It often tells you what went wrong.",[14,3183,3184,3185,186],{},"You can also read more in ",[372,3186,1172],{"href":1171},[37,3188,3190],{"id":3189},"common-beginner-problems","Common beginner problems",[14,3192,3193],{},"Here are some common causes of trouble:",[309,3195,3196,3200,3203,3206,3209,3212],{},[24,3197,3198,1671],{},[49,3199,303],{},[24,3201,3202],{},"The URL or endpoint is wrong",[24,3204,3205],{},"You send form data when the API expects JSON",[24,3207,3208],{},"You try to parse JSON when the response is not actually JSON",[24,3210,3211],{},"You forget to handle network errors",[24,3213,3214],{},"The API requires authentication headers or a token",[14,3216,3217],{},"More specifically:",[309,3219,3220,3231,3237,3240,3243],{},[24,3221,3222,3224,3225,3227,3228,186],{},[49,3223,1693],{}," can happen if ",[49,3226,303],{}," is missing. See ",[372,3229,3230],{"href":1730},"how to fix ModuleNotFoundError: No module named x",[24,3232,3233,3234,3236],{},"A ",[49,3235,2954],{}," response often means the server did not like your data format.",[24,3238,3239],{},"Connection errors often mean the URL is wrong or your network is down.",[24,3241,3242],{},"JSON parsing can fail if the server returns plain text or HTML instead of JSON.",[24,3244,3245],{},"Some APIs require login tokens, API keys, or headers.",[14,3247,3248,3249,186],{},"One more beginner tip: when reading response data, make sure the key exists before using it. This helps avoid errors like ",[372,3250,3251],{"href":1720},"KeyError in Python",[37,3253,3255],{"id":3254},"safer-version-with-error-handling","Safer version with error handling",[14,3257,3258],{},"This version is better for real programs because it handles common problems more safely.",[42,3260,3262],{"className":44,"code":3261,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\npayload = {\n    \"name\": \"Alice\",\n    \"age\": 25\n}\n\ntry:\n    response = requests.post(url, json=payload, timeout=10)\n    response.raise_for_status()\n\n    print(\"Status code:\", response.status_code)\n\n    content_type = response.headers.get(\"Content-Type\", \"\")\n    if \"application\u002Fjson\" in content_type:\n        data = response.json()\n        print(\"JSON response:\")\n        print(data)\n    else:\n        print(\"Response is not JSON:\")\n        print(response.text)\n\nexcept requests.exceptions.RequestException as e:\n    print(\"Request failed:\", e)\n",[49,3263,3264,3270,3274,3286,3294,3312,3324,3328,3332,3339,3376,3388,3392,3414,3418,3451,3469,3484,3499,3509,3515,3530,3544,3548,3573],{"__ignoreMap":47},[52,3265,3266,3268],{"class":54,"line":55},[52,3267,59],{"class":58},[52,3269,63],{"class":62},[52,3271,3272],{"class":54,"line":66},[52,3273,70],{"emptyLinePlaceholder":69},[52,3275,3276,3278,3280,3282,3284],{"class":54,"line":73},[52,3277,76],{"class":62},[52,3279,80],{"class":79},[52,3281,84],{"class":83},[52,3283,2129],{"class":87},[52,3285,91],{"class":83},[52,3287,3288,3290,3292],{"class":54,"line":94},[52,3289,2136],{"class":62},[52,3291,80],{"class":79},[52,3293,103],{"class":102},[52,3295,3296,3298,3300,3302,3304,3306,3308,3310],{"class":54,"line":106},[52,3297,109],{"class":83},[52,3299,2147],{"class":87},[52,3301,115],{"class":83},[52,3303,118],{"class":102},[52,3305,84],{"class":83},[52,3307,2156],{"class":87},[52,3309,115],{"class":83},[52,3311,125],{"class":102},[52,3313,3314,3316,3318,3320,3322],{"class":54,"line":128},[52,3315,109],{"class":83},[52,3317,2167],{"class":87},[52,3319,115],{"class":83},[52,3321,118],{"class":102},[52,3323,2174],{"class":121},[52,3325,3326],{"class":54,"line":148},[52,3327,167],{"class":102},[52,3329,3330],{"class":54,"line":164},[52,3331,70],{"emptyLinePlaceholder":69},[52,3333,3334,3337],{"class":54,"line":170},[52,3335,3336],{"class":58},"try",[52,3338,1330],{"class":102},[52,3340,3341,3344,3346,3348,3350,3352,3354,3356,3358,3360,3362,3364,3366,3369,3371,3374],{"class":54,"line":175},[52,3342,3343],{"class":62},"    response ",[52,3345,80],{"class":79},[52,3347,183],{"class":62},[52,3349,186],{"class":102},[52,3351,2195],{"class":189},[52,3353,193],{"class":102},[52,3355,196],{"class":189},[52,3357,199],{"class":102},[52,3359,2204],{"class":202},[52,3361,80],{"class":79},[52,3363,2209],{"class":189},[52,3365,199],{"class":102},[52,3367,3368],{"class":202}," timeout",[52,3370,80],{"class":79},[52,3372,3373],{"class":121},"10",[52,3375,211],{"class":102},[52,3377,3378,3381,3383,3386],{"class":54,"line":214},[52,3379,3380],{"class":62},"    response",[52,3382,186],{"class":102},[52,3384,3385],{"class":189},"raise_for_status",[52,3387,230],{"class":102},[52,3389,3390],{"class":54,"line":233},[52,3391,70],{"emptyLinePlaceholder":69},[52,3393,3394,3396,3398,3400,3402,3404,3406,3408,3410,3412],{"class":54,"line":238},[52,3395,1599],{"class":241},[52,3397,193],{"class":102},[52,3399,115],{"class":83},[52,3401,1622],{"class":87},[52,3403,115],{"class":83},[52,3405,199],{"class":102},[52,3407,222],{"class":189},[52,3409,186],{"class":102},[52,3411,1321],{"class":1320},[52,3413,211],{"class":102},[52,3415,3416],{"class":54,"line":272},[52,3417,70],{"emptyLinePlaceholder":69},[52,3419,3420,3423,3425,3427,3429,3431,3433,3435,3437,3439,3442,3444,3446,3449],{"class":54,"line":1348},[52,3421,3422],{"class":62},"    content_type ",[52,3424,80],{"class":79},[52,3426,222],{"class":62},[52,3428,186],{"class":102},[52,3430,2633],{"class":1320},[52,3432,186],{"class":102},[52,3434,190],{"class":189},[52,3436,193],{"class":102},[52,3438,115],{"class":83},[52,3440,3441],{"class":87},"Content-Type",[52,3443,115],{"class":83},[52,3445,199],{"class":102},[52,3447,3448],{"class":83}," \"\"",[52,3450,211],{"class":102},[52,3452,3453,3455,3457,3460,3462,3464,3467],{"class":54,"line":1376},[52,3454,1438],{"class":58},[52,3456,84],{"class":83},[52,3458,3459],{"class":87},"application\u002Fjson",[52,3461,115],{"class":83},[52,3463,3101],{"class":79},[52,3465,3466],{"class":62}," content_type",[52,3468,1330],{"class":102},[52,3470,3471,3474,3476,3478,3480,3482],{"class":54,"line":1381},[52,3472,3473],{"class":62},"        data ",[52,3475,80],{"class":79},[52,3477,222],{"class":62},[52,3479,186],{"class":102},[52,3481,227],{"class":189},[52,3483,230],{"class":102},[52,3485,3486,3488,3490,3492,3495,3497],{"class":54,"line":1406},[52,3487,1470],{"class":241},[52,3489,193],{"class":102},[52,3491,115],{"class":83},[52,3493,3494],{"class":87},"JSON response:",[52,3496,115],{"class":83},[52,3498,211],{"class":102},[52,3500,3501,3503,3505,3507],{"class":54,"line":1430},[52,3502,1470],{"class":241},[52,3504,193],{"class":102},[52,3506,247],{"class":189},[52,3508,211],{"class":102},[52,3510,3511,3513],{"class":54,"line":1435},[52,3512,1567],{"class":58},[52,3514,1330],{"class":102},[52,3516,3517,3519,3521,3523,3526,3528],{"class":54,"line":1467},[52,3518,1470],{"class":241},[52,3520,193],{"class":102},[52,3522,115],{"class":83},[52,3524,3525],{"class":87},"Response is not JSON:",[52,3527,115],{"class":83},[52,3529,211],{"class":102},[52,3531,3532,3534,3536,3538,3540,3542],{"class":54,"line":1495},[52,3533,1470],{"class":241},[52,3535,193],{"class":102},[52,3537,1756],{"class":189},[52,3539,186],{"class":102},[52,3541,955],{"class":1320},[52,3543,211],{"class":102},[52,3545,3546],{"class":54,"line":1531},[52,3547,70],{"emptyLinePlaceholder":69},[52,3549,3550,3553,3555,3557,3560,3562,3565,3568,3571],{"class":54,"line":1564},[52,3551,3552],{"class":58},"except",[52,3554,183],{"class":62},[52,3556,186],{"class":102},[52,3558,3559],{"class":1320},"exceptions",[52,3561,186],{"class":102},[52,3563,3564],{"class":1320},"RequestException",[52,3566,3567],{"class":58}," as",[52,3569,3570],{"class":62}," e",[52,3572,1330],{"class":102},[52,3574,3575,3577,3579,3581,3584,3586,3588,3590],{"class":54,"line":1572},[52,3576,1599],{"class":241},[52,3578,193],{"class":102},[52,3580,115],{"class":83},[52,3582,3583],{"class":87},"Request failed:",[52,3585,115],{"class":83},[52,3587,199],{"class":102},[52,3589,3570],{"class":189},[52,3591,211],{"class":102},[639,3593,3595],{"id":3594},"what-this-safer-version-does","What this safer version does",[309,3597,3598,3604,3607,3614,3619],{},[24,3599,3600,3601],{},"Wraps the request in ",[49,3602,3603],{},"try-except",[24,3605,3606],{},"Catches network and request errors",[24,3608,3609,3610,3613],{},"Uses ",[49,3611,3612],{},"raise_for_status()"," to catch bad HTTP status codes",[24,3615,3616,3617],{},"Checks the response type before calling ",[49,3618,848],{},[24,3620,3621,3622,3625],{},"Adds a ",[49,3623,3624],{},"timeout"," so the program does not wait forever",[37,3627,1942],{"id":1941},[639,3629,3631],{"id":3630},"what-is-a-post-request-in-python","What is a POST request in Python?",[14,3633,3634],{},"It is a request that sends data to a server, often to create or submit something.",[639,3636,3638,3639,857,3642,3645,3646,1995],{"id":3637},"should-i-use-data-or-json-in-requestspost","Should I use ",[49,3640,3641],{},"data=",[49,3643,3644],{},"json="," in ",[49,3647,2740],{},[14,3649,3650,3651,3653,3654,3656],{},"Use ",[49,3652,3644],{}," when the API expects JSON. Use ",[49,3655,3641],{}," for form-style data.",[639,3658,3660],{"id":3659},"why-use-httpbinorg-in-the-example","Why use httpbin.org in the example?",[14,3662,3663],{},"It is a simple test service that shows back the data you sent.",[639,3665,3667,3668,3670],{"id":3666},"what-does-responsestatus_code-mean","What does ",[49,3669,2872],{}," mean?",[14,3672,3673],{},"It is the HTTP status code that tells you whether the request succeeded or failed.",[639,3675,3677,3678,3680],{"id":3676},"why-does-responsejson-fail-sometimes","Why does ",[49,3679,848],{}," fail sometimes?",[14,3682,3683],{},"It fails when the server response is not valid JSON.",[37,3685,2005],{"id":2004},[309,3687,3688,3693,3697,3701,3705,3712],{},[24,3689,3690],{},[372,3691,3692],{"href":2098},"How to send a POST request in Python",[24,3694,3695],{},[372,3696,2012],{"href":374},[24,3698,3699],{},[372,3700,2017],{"href":1171},[24,3702,3703],{},[372,3704,2793],{"href":860},[24,3706,3707],{},[372,3708,3709,3711],{"href":2929},[49,3710,2932],{}," function explained",[24,3713,3714],{},[372,3715,3716,3711],{"href":2862},[49,3717,2865],{},[14,3719,3720,3721,3723],{},"If this copy-paste example works for you, the next step is learning how ",[49,3722,303],{},", JSON data, and response debugging fit together in a real API workflow.",[2034,3725,3726],{},"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 .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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":3728},[3729,3730,3731,3732,3735,3741,3742,3743,3746,3756],{"id":39,"depth":66,"text":40},{"id":2267,"depth":66,"text":2268},{"id":2285,"depth":66,"text":2286},{"id":2344,"depth":66,"text":2345,"children":3733},[3734],{"id":2509,"depth":73,"text":2510},{"id":2733,"depth":66,"text":2734,"children":3736},[3737,3738,3739,3740],{"id":2737,"depth":73,"text":2740},{"id":2796,"depth":73,"text":2786},{"id":2869,"depth":73,"text":2872},{"id":2899,"depth":73,"text":848},{"id":2935,"depth":66,"text":2936},{"id":3189,"depth":66,"text":3190},{"id":3254,"depth":66,"text":3255,"children":3744},[3745],{"id":3594,"depth":73,"text":3595},{"id":1941,"depth":66,"text":1942,"children":3747},[3748,3749,3751,3752,3754],{"id":3630,"depth":73,"text":3631},{"id":3637,"depth":73,"text":3750},"Should I use data= or json= in requests.post()?",{"id":3659,"depth":73,"text":3660},{"id":3666,"depth":73,"text":3753},"What does response.status_code mean?",{"id":3676,"depth":73,"text":3755},"Why does response.json() fail sometimes?",{"id":2004,"depth":66,"text":2005},"Master python api post request example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-api-post-request-example",{"title":2084,"description":3757},"examples\u002Fpython-api-post-request-example","XK7lgAbUVrDgsoYghjGTRn1nExx9y2rcMGjSlbANZj0",{"id":3764,"title":3765,"body":3766,"description":5025,"extension":2075,"meta":5026,"navigation":69,"path":5027,"seo":5028,"stem":5029,"__hash__":5030},"content\u002Fexamples\u002Fpython-api-request-example-get-request.md","Python API Request Example (GET Request)",{"type":7,"value":3767,"toc":5002},[3768,3771,3774,3777,3855,3861,3869,3871,3874,3877,3897,3899,3901,3916,3918,3924,3936,3938,3954,3957,3963,3979,3993,3997,4000,4098,4102,4132,4134,4137,4217,4222,4226,4229,4232,4257,4260,4415,4417,4436,4444,4446,4449,4452,4752,4756,4774,4777,4781,4784,4803,4806,4830,4833,4875,4886,4890,4893,4907,4912,4914,4918,4924,4928,4931,4935,4938,4942,4950,4954,4959,4961,4999],[10,3769,3765],{"id":3770},"python-api-request-example-get-request",[14,3772,3773],{},"This beginner-friendly example shows how to make a simple GET request in Python, read the response, and handle basic problems.",[14,3775,3776],{},"If you want the fastest working version first, use this:",[42,3778,3780],{"className":44,"code":3779,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fapi.github.com\u002Fusers\u002Foctocat\"\nresponse = requests.get(url)\n\nprint(response.status_code)\nprint(response.json())\n",[49,3781,3782,3788,3792,3805,3823,3827,3841],{"__ignoreMap":47},[52,3783,3784,3786],{"class":54,"line":55},[52,3785,59],{"class":58},[52,3787,63],{"class":62},[52,3789,3790],{"class":54,"line":66},[52,3791,70],{"emptyLinePlaceholder":69},[52,3793,3794,3796,3798,3800,3803],{"class":54,"line":73},[52,3795,76],{"class":62},[52,3797,80],{"class":79},[52,3799,84],{"class":83},[52,3801,3802],{"class":87},"https:\u002F\u002Fapi.github.com\u002Fusers\u002Foctocat",[52,3804,91],{"class":83},[52,3806,3807,3809,3811,3813,3815,3817,3819,3821],{"class":54,"line":94},[52,3808,178],{"class":62},[52,3810,80],{"class":79},[52,3812,183],{"class":62},[52,3814,186],{"class":102},[52,3816,190],{"class":189},[52,3818,193],{"class":102},[52,3820,196],{"class":189},[52,3822,211],{"class":102},[52,3824,3825],{"class":54,"line":106},[52,3826,70],{"emptyLinePlaceholder":69},[52,3828,3829,3831,3833,3835,3837,3839],{"class":54,"line":128},[52,3830,242],{"class":241},[52,3832,193],{"class":102},[52,3834,1756],{"class":189},[52,3836,186],{"class":102},[52,3838,1321],{"class":1320},[52,3840,211],{"class":102},[52,3842,3843,3845,3847,3849,3851,3853],{"class":54,"line":148},[52,3844,242],{"class":241},[52,3846,193],{"class":102},[52,3848,1756],{"class":189},[52,3850,186],{"class":102},[52,3852,227],{"class":189},[52,3854,1816],{"class":102},[14,3856,3857,3858,3860],{},"This example uses the ",[49,3859,303],{}," package and prints:",[309,3862,3863,3866],{},[24,3864,3865],{},"The HTTP status code",[24,3867,3868],{},"The JSON response from the API",[37,3870,323],{"id":322},[14,3872,3873],{},"This example is useful because it shows the basic pattern for working with APIs in Python.",[14,3875,3876],{},"It will:",[309,3878,3879,3882,3888,3891,3894],{},[24,3880,3881],{},"Send a simple GET request to a public API",[24,3883,3884,3885,3887],{},"Use the ",[49,3886,303],{}," package",[24,3889,3890],{},"Print the HTTP status code",[24,3892,3893],{},"Read JSON data from the response",[24,3895,3896],{},"Keep the code small and easy to follow",[37,3898,347],{"id":346},[14,3900,350],{},[309,3902,3903,3906,3911,3913],{},[24,3904,3905],{},"Python installed on your computer",[24,3907,358,3908,3910],{},[49,3909,303],{}," package installed",[24,3912,364],{},[24,3914,3915],{},"A public API URL that allows GET requests",[37,3917,379],{"id":378},[14,3919,3920,3921,3923],{},"To install ",[49,3922,303],{},", run this in your terminal:",[42,3925,3926],{"className":390,"code":391,"language":392,"meta":47,"style":47},[49,3927,3928],{"__ignoreMap":47},[52,3929,3930,3932,3934],{"class":54,"line":55},[52,3931,400],{"class":399},[52,3933,403],{"class":87},[52,3935,63],{"class":87},[14,3937,408],{},[42,3939,3940],{"className":390,"code":411,"language":392,"meta":47,"style":47},[49,3941,3942],{"__ignoreMap":47},[52,3943,3944,3946,3948,3950,3952],{"class":54,"line":55},[52,3945,46],{"class":399},[52,3947,421],{"class":420},[52,3949,424],{"class":87},[52,3951,403],{"class":87},[52,3953,63],{"class":87},[14,3955,3956],{},"Make sure you install it in the same Python environment you are using to run your script.",[14,3958,3959,3960,3962],{},"You can check that ",[49,3961,303],{}," is installed with:",[42,3964,3965],{"className":390,"code":434,"language":392,"meta":47,"style":47},[49,3966,3967],{"__ignoreMap":47},[52,3968,3969,3971,3973,3975,3977],{"class":54,"line":55},[52,3970,46],{"class":399},[52,3972,443],{"class":420},[52,3974,84],{"class":83},[52,3976,448],{"class":87},[52,3978,91],{"class":83},[14,3980,3981,3982,857,3987,186],{},"If Python says the module cannot be found, see ",[372,3983,3984,3985],{"href":1730},"how to fix ",[49,3986,1693],{},[372,3988,3984,3990],{"href":3989},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix\u002F",[49,3991,3992],{},"ImportError",[37,3994,3996],{"id":3995},"basic-get-request-example","Basic GET request example",[14,3998,3999],{},"Here is a complete beginner-friendly example:",[42,4001,4003],{"className":44,"code":4002,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fapi.github.com\u002Fusers\u002Foctocat\"\n\nresponse = requests.get(url)\n\nprint(\"Status code:\", response.status_code)\nprint(\"JSON data:\", response.json())\n",[49,4004,4005,4011,4015,4027,4031,4049,4053,4075],{"__ignoreMap":47},[52,4006,4007,4009],{"class":54,"line":55},[52,4008,59],{"class":58},[52,4010,63],{"class":62},[52,4012,4013],{"class":54,"line":66},[52,4014,70],{"emptyLinePlaceholder":69},[52,4016,4017,4019,4021,4023,4025],{"class":54,"line":73},[52,4018,76],{"class":62},[52,4020,80],{"class":79},[52,4022,84],{"class":83},[52,4024,3802],{"class":87},[52,4026,91],{"class":83},[52,4028,4029],{"class":54,"line":94},[52,4030,70],{"emptyLinePlaceholder":69},[52,4032,4033,4035,4037,4039,4041,4043,4045,4047],{"class":54,"line":106},[52,4034,178],{"class":62},[52,4036,80],{"class":79},[52,4038,183],{"class":62},[52,4040,186],{"class":102},[52,4042,190],{"class":189},[52,4044,193],{"class":102},[52,4046,196],{"class":189},[52,4048,211],{"class":102},[52,4050,4051],{"class":54,"line":128},[52,4052,70],{"emptyLinePlaceholder":69},[52,4054,4055,4057,4059,4061,4063,4065,4067,4069,4071,4073],{"class":54,"line":148},[52,4056,242],{"class":241},[52,4058,193],{"class":102},[52,4060,115],{"class":83},[52,4062,1622],{"class":87},[52,4064,115],{"class":83},[52,4066,199],{"class":102},[52,4068,222],{"class":189},[52,4070,186],{"class":102},[52,4072,1321],{"class":1320},[52,4074,211],{"class":102},[52,4076,4077,4079,4081,4083,4086,4088,4090,4092,4094,4096],{"class":54,"line":164},[52,4078,242],{"class":241},[52,4080,193],{"class":102},[52,4082,115],{"class":83},[52,4084,4085],{"class":87},"JSON data:",[52,4087,115],{"class":83},[52,4089,199],{"class":102},[52,4091,222],{"class":189},[52,4093,186],{"class":102},[52,4095,227],{"class":189},[52,4097,1816],{"class":102},[639,4099,4101],{"id":4100},"how-this-code-works","How this code works",[309,4103,4104,4110,4116,4122,4127],{},[24,4105,4106,4109],{},[49,4107,4108],{},"import requests"," loads the package",[24,4111,4112,4115],{},[49,4113,4114],{},"url = ..."," stores the API address in a variable",[24,4117,4118,4121],{},[49,4119,4120],{},"requests.get(url)"," sends a GET request to that URL",[24,4123,4124,4126],{},[49,4125,2872],{}," shows the result of the request",[24,4128,4129,4131],{},[49,4130,848],{}," converts the JSON response into Python data",[639,4133,2510],{"id":2509},[14,4135,4136],{},"The exact output may vary, but it will look something like this:",[42,4138,4140],{"className":44,"code":4139,"language":46,"meta":47,"style":47},"Status code: 200\nJSON data: {'login': 'octocat', 'id': 583231, 'type': 'User', ...}\n",[49,4141,4142,4150],{"__ignoreMap":47},[52,4143,4144,4146,4148],{"class":54,"line":55},[52,4145,2520],{"class":62},[52,4147,118],{"class":102},[52,4149,2525],{"class":121},[52,4151,4152,4154,4156,4158,4160,4163,4166,4168,4170,4173,4176,4178,4180,4182,4185,4187,4189,4192,4194,4196,4199,4201,4203,4205,4208,4210,4212,4215],{"class":54,"line":66},[52,4153,2534],{"class":2533},[52,4155,1356],{"class":62},[52,4157,118],{"class":102},[52,4159,3000],{"class":102},[52,4161,4162],{"class":83},"'",[52,4164,4165],{"class":87},"login",[52,4167,4162],{"class":83},[52,4169,118],{"class":102},[52,4171,4172],{"class":83}," '",[52,4174,4175],{"class":87},"octocat",[52,4177,4162],{"class":83},[52,4179,199],{"class":102},[52,4181,4172],{"class":83},[52,4183,4184],{"class":87},"id",[52,4186,4162],{"class":83},[52,4188,118],{"class":102},[52,4190,4191],{"class":121}," 583231",[52,4193,199],{"class":102},[52,4195,4172],{"class":83},[52,4197,4198],{"class":87},"type",[52,4200,4162],{"class":83},[52,4202,118],{"class":102},[52,4204,4172],{"class":83},[52,4206,4207],{"class":87},"User",[52,4209,4162],{"class":83},[52,4211,199],{"class":102},[52,4213,4214],{"class":2533}," ...",[52,4216,167],{"class":102},[14,4218,4219,4220,186],{},"If you want a broader walkthrough, see ",[372,4221,375],{"href":374},[37,4223,4225],{"id":4224},"how-to-understand-the-response","How to understand the response",[14,4227,4228],{},"When you send a request, the server sends back a response.",[14,4230,4231],{},"Here are the most important parts for beginners:",[309,4233,4234,4239,4243,4249,4254],{},[24,4235,4236,4238],{},[49,4237,1321],{}," tells you whether the request worked",[24,4240,4241,2949],{},[49,4242,2727],{},[24,4244,4245,4248],{},[49,4246,4247],{},"response.text"," gives the raw response as plain text",[24,4250,4251,4253],{},[49,4252,848],{}," converts JSON into Python objects",[24,4255,4256],{},"JSON objects usually become Python dictionaries",[14,4258,4259],{},"Example:",[42,4261,4263],{"className":44,"code":4262,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fapi.github.com\u002Fusers\u002Foctocat\"\nresponse = requests.get(url)\n\nprint(\"Status code:\", response.status_code)\nprint(\"Text response:\")\nprint(response.text[:200])  # first 200 characters\n\ndata = response.json()\nprint(\"User login:\", data[\"login\"])\n",[49,4264,4265,4271,4275,4287,4305,4309,4331,4346,4370,4374,4388],{"__ignoreMap":47},[52,4266,4267,4269],{"class":54,"line":55},[52,4268,59],{"class":58},[52,4270,63],{"class":62},[52,4272,4273],{"class":54,"line":66},[52,4274,70],{"emptyLinePlaceholder":69},[52,4276,4277,4279,4281,4283,4285],{"class":54,"line":73},[52,4278,76],{"class":62},[52,4280,80],{"class":79},[52,4282,84],{"class":83},[52,4284,3802],{"class":87},[52,4286,91],{"class":83},[52,4288,4289,4291,4293,4295,4297,4299,4301,4303],{"class":54,"line":94},[52,4290,178],{"class":62},[52,4292,80],{"class":79},[52,4294,183],{"class":62},[52,4296,186],{"class":102},[52,4298,190],{"class":189},[52,4300,193],{"class":102},[52,4302,196],{"class":189},[52,4304,211],{"class":102},[52,4306,4307],{"class":54,"line":106},[52,4308,70],{"emptyLinePlaceholder":69},[52,4310,4311,4313,4315,4317,4319,4321,4323,4325,4327,4329],{"class":54,"line":128},[52,4312,242],{"class":241},[52,4314,193],{"class":102},[52,4316,115],{"class":83},[52,4318,1622],{"class":87},[52,4320,115],{"class":83},[52,4322,199],{"class":102},[52,4324,222],{"class":189},[52,4326,186],{"class":102},[52,4328,1321],{"class":1320},[52,4330,211],{"class":102},[52,4332,4333,4335,4337,4339,4342,4344],{"class":54,"line":148},[52,4334,242],{"class":241},[52,4336,193],{"class":102},[52,4338,115],{"class":83},[52,4340,4341],{"class":87},"Text response:",[52,4343,115],{"class":83},[52,4345,211],{"class":102},[52,4347,4348,4350,4352,4354,4356,4358,4361,4363,4366],{"class":54,"line":164},[52,4349,242],{"class":241},[52,4351,193],{"class":102},[52,4353,1756],{"class":189},[52,4355,186],{"class":102},[52,4357,955],{"class":1320},[52,4359,4360],{"class":102},"[:",[52,4362,2727],{"class":121},[52,4364,4365],{"class":102},"])",[52,4367,4369],{"class":4368},"sutJx","  # first 200 characters\n",[52,4371,4372],{"class":54,"line":170},[52,4373,70],{"emptyLinePlaceholder":69},[52,4375,4376,4378,4380,4382,4384,4386],{"class":54,"line":175},[52,4377,217],{"class":62},[52,4379,80],{"class":79},[52,4381,222],{"class":62},[52,4383,186],{"class":102},[52,4385,227],{"class":189},[52,4387,230],{"class":102},[52,4389,4390,4392,4394,4396,4399,4401,4403,4405,4407,4409,4411,4413],{"class":54,"line":214},[52,4391,242],{"class":241},[52,4393,193],{"class":102},[52,4395,115],{"class":83},[52,4397,4398],{"class":87},"User login:",[52,4400,115],{"class":83},[52,4402,199],{"class":102},[52,4404,1356],{"class":189},[52,4406,250],{"class":102},[52,4408,115],{"class":83},[52,4410,4165],{"class":87},[52,4412,115],{"class":83},[52,4414,269],{"class":102},[14,4416,752],{},[309,4418,4419,4424,4430],{},[24,4420,4421,4423],{},[49,4422,4247],{}," shows the raw content",[24,4425,4426,4429],{},[49,4427,4428],{},"data = response.json()"," creates a Python dictionary",[24,4431,4432,4435],{},[49,4433,4434],{},"data[\"login\"]"," gets one value from that dictionary",[14,4437,4438,4439,4441,4442,186],{},"If you want more help reading API output, see ",[372,4440,1172],{"href":1171}," and the ",[372,4443,861],{"href":860},[37,4445,3255],{"id":3254},[14,4447,4448],{},"The first example works, but real programs should handle errors.",[14,4450,4451],{},"This version is safer for beginners:",[42,4453,4455],{"className":44,"code":4454,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fapi.github.com\u002Fusers\u002Foctocat\"\n\ntry:\n    response = requests.get(url, timeout=10)\n    response.raise_for_status()\n\n    data = response.json()\n    print(\"Request worked\")\n    print(\"Status code:\", response.status_code)\n    print(\"Login:\", data[\"login\"])\n\nexcept requests.exceptions.Timeout:\n    print(\"The request took too long and timed out.\")\n\nexcept requests.exceptions.ConnectionError:\n    print(\"Could not connect to the server.\")\n\nexcept requests.exceptions.HTTPError as error:\n    print(\"The server returned an HTTP error:\", error)\n\nexcept ValueError:\n    print(\"The response was not valid JSON.\")\n",[49,4456,4457,4463,4467,4479,4483,4489,4515,4525,4529,4543,4557,4579,4606,4610,4627,4642,4646,4663,4678,4682,4704,4723,4727,4737],{"__ignoreMap":47},[52,4458,4459,4461],{"class":54,"line":55},[52,4460,59],{"class":58},[52,4462,63],{"class":62},[52,4464,4465],{"class":54,"line":66},[52,4466,70],{"emptyLinePlaceholder":69},[52,4468,4469,4471,4473,4475,4477],{"class":54,"line":73},[52,4470,76],{"class":62},[52,4472,80],{"class":79},[52,4474,84],{"class":83},[52,4476,3802],{"class":87},[52,4478,91],{"class":83},[52,4480,4481],{"class":54,"line":94},[52,4482,70],{"emptyLinePlaceholder":69},[52,4484,4485,4487],{"class":54,"line":106},[52,4486,3336],{"class":58},[52,4488,1330],{"class":102},[52,4490,4491,4493,4495,4497,4499,4501,4503,4505,4507,4509,4511,4513],{"class":54,"line":128},[52,4492,3343],{"class":62},[52,4494,80],{"class":79},[52,4496,183],{"class":62},[52,4498,186],{"class":102},[52,4500,190],{"class":189},[52,4502,193],{"class":102},[52,4504,196],{"class":189},[52,4506,199],{"class":102},[52,4508,3368],{"class":202},[52,4510,80],{"class":79},[52,4512,3373],{"class":121},[52,4514,211],{"class":102},[52,4516,4517,4519,4521,4523],{"class":54,"line":148},[52,4518,3380],{"class":62},[52,4520,186],{"class":102},[52,4522,3385],{"class":189},[52,4524,230],{"class":102},[52,4526,4527],{"class":54,"line":164},[52,4528,70],{"emptyLinePlaceholder":69},[52,4530,4531,4533,4535,4537,4539,4541],{"class":54,"line":170},[52,4532,1335],{"class":62},[52,4534,80],{"class":79},[52,4536,222],{"class":62},[52,4538,186],{"class":102},[52,4540,227],{"class":189},[52,4542,230],{"class":102},[52,4544,4545,4547,4549,4551,4553,4555],{"class":54,"line":175},[52,4546,1599],{"class":241},[52,4548,193],{"class":102},[52,4550,115],{"class":83},[52,4552,3125],{"class":87},[52,4554,115],{"class":83},[52,4556,211],{"class":102},[52,4558,4559,4561,4563,4565,4567,4569,4571,4573,4575,4577],{"class":54,"line":214},[52,4560,1599],{"class":241},[52,4562,193],{"class":102},[52,4564,115],{"class":83},[52,4566,1622],{"class":87},[52,4568,115],{"class":83},[52,4570,199],{"class":102},[52,4572,222],{"class":189},[52,4574,186],{"class":102},[52,4576,1321],{"class":1320},[52,4578,211],{"class":102},[52,4580,4581,4583,4585,4587,4590,4592,4594,4596,4598,4600,4602,4604],{"class":54,"line":233},[52,4582,1599],{"class":241},[52,4584,193],{"class":102},[52,4586,115],{"class":83},[52,4588,4589],{"class":87},"Login:",[52,4591,115],{"class":83},[52,4593,199],{"class":102},[52,4595,1356],{"class":189},[52,4597,250],{"class":102},[52,4599,115],{"class":83},[52,4601,4165],{"class":87},[52,4603,115],{"class":83},[52,4605,269],{"class":102},[52,4607,4608],{"class":54,"line":238},[52,4609,70],{"emptyLinePlaceholder":69},[52,4611,4612,4614,4616,4618,4620,4622,4625],{"class":54,"line":272},[52,4613,3552],{"class":58},[52,4615,183],{"class":62},[52,4617,186],{"class":102},[52,4619,3559],{"class":1320},[52,4621,186],{"class":102},[52,4623,4624],{"class":1320},"Timeout",[52,4626,1330],{"class":102},[52,4628,4629,4631,4633,4635,4638,4640],{"class":54,"line":1348},[52,4630,1599],{"class":241},[52,4632,193],{"class":102},[52,4634,115],{"class":83},[52,4636,4637],{"class":87},"The request took too long and timed out.",[52,4639,115],{"class":83},[52,4641,211],{"class":102},[52,4643,4644],{"class":54,"line":1376},[52,4645,70],{"emptyLinePlaceholder":69},[52,4647,4648,4650,4652,4654,4656,4658,4661],{"class":54,"line":1381},[52,4649,3552],{"class":58},[52,4651,183],{"class":62},[52,4653,186],{"class":102},[52,4655,3559],{"class":1320},[52,4657,186],{"class":102},[52,4659,4660],{"class":1320},"ConnectionError",[52,4662,1330],{"class":102},[52,4664,4665,4667,4669,4671,4674,4676],{"class":54,"line":1406},[52,4666,1599],{"class":241},[52,4668,193],{"class":102},[52,4670,115],{"class":83},[52,4672,4673],{"class":87},"Could not connect to the server.",[52,4675,115],{"class":83},[52,4677,211],{"class":102},[52,4679,4680],{"class":54,"line":1430},[52,4681,70],{"emptyLinePlaceholder":69},[52,4683,4684,4686,4688,4690,4692,4694,4697,4699,4702],{"class":54,"line":1435},[52,4685,3552],{"class":58},[52,4687,183],{"class":62},[52,4689,186],{"class":102},[52,4691,3559],{"class":1320},[52,4693,186],{"class":102},[52,4695,4696],{"class":1320},"HTTPError",[52,4698,3567],{"class":58},[52,4700,4701],{"class":62}," error",[52,4703,1330],{"class":102},[52,4705,4706,4708,4710,4712,4715,4717,4719,4721],{"class":54,"line":1467},[52,4707,1599],{"class":241},[52,4709,193],{"class":102},[52,4711,115],{"class":83},[52,4713,4714],{"class":87},"The server returned an HTTP error:",[52,4716,115],{"class":83},[52,4718,199],{"class":102},[52,4720,4701],{"class":189},[52,4722,211],{"class":102},[52,4724,4725],{"class":54,"line":1495},[52,4726,70],{"emptyLinePlaceholder":69},[52,4728,4729,4731,4735],{"class":54,"line":1531},[52,4730,3552],{"class":58},[52,4732,4734],{"class":4733},"sZMiF"," ValueError",[52,4736,1330],{"class":102},[52,4738,4739,4741,4743,4745,4748,4750],{"class":54,"line":1564},[52,4740,1599],{"class":241},[52,4742,193],{"class":102},[52,4744,115],{"class":83},[52,4746,4747],{"class":87},"The response was not valid JSON.",[52,4749,115],{"class":83},[52,4751,211],{"class":102},[639,4753,4755],{"id":4754},"why-this-version-is-better","Why this version is better",[309,4757,4758,4764,4769],{},[24,4759,4760,4763],{},[49,4761,4762],{},"timeout=10"," prevents the program from waiting forever",[24,4765,4766,4768],{},[49,4767,3612],{}," turns bad HTTP responses into errors",[24,4770,4771,4773],{},[49,4772,3603],{}," lets you show simple messages instead of crashing",[14,4775,4776],{},"This is a good next step after learning the basic example.",[37,4778,4780],{"id":4779},"common-problems-beginners-hit","Common problems beginners hit",[14,4782,4783],{},"Here are some common causes when this example does not work:",[309,4785,4786,4791,4794,4797,4800],{},[24,4787,358,4788,4790],{},[49,4789,303],{}," package is not installed",[24,4792,4793],{},"The API URL is incorrect",[24,4795,4796],{},"The server returns text or HTML instead of JSON",[24,4798,4799],{},"The request needs headers or authentication",[24,4801,4802],{},"The network connection fails or times out",[14,4804,4805],{},"You may also run into these specific issues:",[309,4807,4808,4815,4818,4824,4827],{},[24,4809,4810,4812,4813,1671],{},[49,4811,1693],{}," if ",[49,4814,303],{},[24,4816,4817],{},"JSON decode errors if the API does not return JSON",[24,4819,4820,4823],{},[49,4821,4822],{},"404"," if the URL is wrong",[24,4825,4826],{},"Timeout errors if the server is slow",[24,4828,4829],{},"Permission or authentication problems with private APIs",[14,4831,4832],{},"Helpful commands:",[42,4834,4836],{"className":390,"code":4835,"language":392,"meta":47,"style":47},"python -m pip install requests\npython -c \"import requests; print(requests.__version__)\"\npython -c \"import requests; print(requests.get('https:\u002F\u002Fapi.github.com').status_code)\"\n",[49,4837,4838,4850,4862],{"__ignoreMap":47},[52,4839,4840,4842,4844,4846,4848],{"class":54,"line":55},[52,4841,46],{"class":399},[52,4843,421],{"class":420},[52,4845,424],{"class":87},[52,4847,403],{"class":87},[52,4849,63],{"class":87},[52,4851,4852,4854,4856,4858,4860],{"class":54,"line":66},[52,4853,46],{"class":399},[52,4855,443],{"class":420},[52,4857,84],{"class":83},[52,4859,448],{"class":87},[52,4861,91],{"class":83},[52,4863,4864,4866,4868,4870,4873],{"class":54,"line":73},[52,4865,46],{"class":399},[52,4867,443],{"class":420},[52,4869,84],{"class":83},[52,4871,4872],{"class":87},"import requests; print(requests.get('https:\u002F\u002Fapi.github.com').status_code)",[52,4874,91],{"class":83},[14,4876,4877,4878,4885],{},"If you are working with JSON strings directly, ",[372,4879,4881,4882,4884],{"href":4880},"\u002Freference\u002Fjson-loads-function-explained\u002F","how ",[49,4883,2932],{}," works"," can also help.",[37,4887,4889],{"id":4888},"when-to-use-this-example","When to use this example",[14,4891,4892],{},"This example is a good fit when you want to:",[309,4894,4895,4898,4901,4904],{},[24,4896,4897],{},"Test a public API",[24,4899,4900],{},"Learn how Python works with web data",[24,4902,4903],{},"Fetch JSON for scripts and small projects",[24,4905,4906],{},"Start with GET requests before learning POST requests or authentication",[14,4908,4909,4910,186],{},"After this, a good next step is learning ",[372,4911,2099],{"href":2098},[37,4913,1942],{"id":1941},[639,4915,4917],{"id":4916},"what-is-a-get-request-in-python","What is a GET request in Python?",[14,4919,4920,4921,4923],{},"A GET request asks a server for data. In Python, beginners often use ",[49,4922,774],{}," to do this.",[639,4925,4927],{"id":4926},"do-i-need-the-requests-package","Do I need the requests package?",[14,4929,4930],{},"For this example, yes. It is the easiest beginner-friendly way to send HTTP requests.",[639,4932,4934],{"id":4933},"why-use-responsejson","Why use response.json()?",[14,4936,4937],{},"Use it when the API sends JSON. It converts the response into Python data like dictionaries and lists.",[639,4939,4941],{"id":4940},"what-if-the-api-does-not-return-json","What if the API does not return JSON?",[14,4943,3650,4944,4946,4947,4949],{},[49,4945,4247],{}," instead. Trying ",[49,4948,848],{}," on non-JSON data can cause an error.",[639,4951,4953],{"id":4952},"what-status-code-means-success","What status code means success?",[14,4955,4956,4958],{},[49,4957,2727],{}," usually means the request worked.",[37,4960,2005],{"id":2004},[309,4962,4963,4967,4971,4975,4979,4986,4993],{},[24,4964,4965],{},[372,4966,2012],{"href":374},[24,4968,4969],{},[372,4970,2017],{"href":1171},[24,4972,4973],{},[372,4974,3692],{"href":2098},[24,4976,4977],{},[372,4978,861],{"href":860},[24,4980,4981],{},[372,4982,4983,4984,4884],{"href":4880},"How ",[49,4985,2932],{},[24,4987,4988],{},[372,4989,4990,4991],{"href":1730},"How to fix ",[49,4992,1693],{},[24,4994,4995],{},[372,4996,4990,4997],{"href":3989},[49,4998,3992],{},[2034,5000,5001],{},"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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .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}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":47,"searchDepth":66,"depth":66,"links":5003},[5004,5005,5006,5007,5011,5012,5015,5016,5017,5024],{"id":322,"depth":66,"text":323},{"id":346,"depth":66,"text":347},{"id":378,"depth":66,"text":379},{"id":3995,"depth":66,"text":3996,"children":5008},[5009,5010],{"id":4100,"depth":73,"text":4101},{"id":2509,"depth":73,"text":2510},{"id":4224,"depth":66,"text":4225},{"id":3254,"depth":66,"text":3255,"children":5013},[5014],{"id":4754,"depth":73,"text":4755},{"id":4779,"depth":66,"text":4780},{"id":4888,"depth":66,"text":4889},{"id":1941,"depth":66,"text":1942,"children":5018},[5019,5020,5021,5022,5023],{"id":4916,"depth":73,"text":4917},{"id":4926,"depth":73,"text":4927},{"id":4933,"depth":73,"text":4934},{"id":4940,"depth":73,"text":4941},{"id":4952,"depth":73,"text":4953},{"id":2004,"depth":66,"text":2005},"Master python api request example get request in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-api-request-example-get-request",{"title":3765,"description":5025},"examples\u002Fpython-api-request-example-get-request","jFxYexTOYXyrTAQb6fM50ar6b17JL0RBH0bEDfDew-Y",{"id":5032,"title":5033,"body":5034,"description":6940,"extension":2075,"meta":6941,"navigation":69,"path":6942,"seo":6943,"stem":6944,"__hash__":6945},"content\u002Fexamples\u002Fpython-argument-parser-example-argparse.md","Python Argument Parser Example (argparse)",{"type":7,"value":5035,"toc":6895},[5036,5039,5045,5048,5062,5064,5067,5244,5247,5265,5268,5274,5276,5279,5282,5299,5305,5309,5314,5317,5337,5343,5354,5366,5370,5373,5380,5411,5414,5418,5499,5506,5516,5519,5523,5542,5545,5549,5576,5585,5589,5592,5748,5751,5786,5792,5807,5811,5816,5820,5823,5834,5836,5859,5861,5875,5879,5882,5899,5901,5924,5926,5946,5949,5957,5961,5967,6191,6195,6198,6216,6219,6242,6246,6258,6260,6266,6270,6286,6288,6294,6297,6301,6307,6310,6314,6328,6330,6335,6339,6355,6367,6371,6385,6387,6392,6399,6403,6408,6412,6420,6433,6438,6444,6449,6463,6473,6477,6490,6493,6503,6506,6596,6599,6686,6690,6696,6698,6712,6715,6721,6724,6749,6760,6764,6767,6770,6792,6794,6798,6803,6807,6813,6817,6822,6826,6833,6845,6849,6855,6857,6892],[10,5037,5033],{"id":5038},"python-argument-parser-example-argparse",[14,5040,5041,5044],{},[49,5042,5043],{},"argparse"," helps you build Python programs that accept values from the command line.",[14,5046,5047],{},"This example shows one small, practical script so you can learn the basic idea without learning the whole module at once. You will see how to:",[309,5049,5050,5053,5056,5059],{},[24,5051,5052],{},"accept arguments after the script name",[24,5054,5055],{},"convert values to the right type",[24,5057,5058],{},"show built-in help",[24,5060,5061],{},"use an optional flag",[37,5063,40],{"id":39},[14,5065,5066],{},"Here is a simple script that adds two numbers:",[42,5068,5070],{"className":44,"code":5069,"language":46,"meta":47,"style":47},"import argparse\n\nparser = argparse.ArgumentParser(description=\"Add two numbers\")\nparser.add_argument(\"x\", type=int, help=\"First number\")\nparser.add_argument(\"y\", type=int, help=\"Second number\")\nargs = parser.parse_args()\n\nprint(args.x + args.y)\n",[49,5071,5072,5079,5083,5114,5159,5199,5216,5220],{"__ignoreMap":47},[52,5073,5074,5076],{"class":54,"line":55},[52,5075,59],{"class":58},[52,5077,5078],{"class":62}," argparse\n",[52,5080,5081],{"class":54,"line":66},[52,5082,70],{"emptyLinePlaceholder":69},[52,5084,5085,5088,5090,5093,5095,5098,5100,5103,5105,5107,5110,5112],{"class":54,"line":73},[52,5086,5087],{"class":62},"parser ",[52,5089,80],{"class":79},[52,5091,5092],{"class":62}," argparse",[52,5094,186],{"class":102},[52,5096,5097],{"class":189},"ArgumentParser",[52,5099,193],{"class":102},[52,5101,5102],{"class":202},"description",[52,5104,80],{"class":79},[52,5106,115],{"class":83},[52,5108,5109],{"class":87},"Add two numbers",[52,5111,115],{"class":83},[52,5113,211],{"class":102},[52,5115,5116,5119,5121,5124,5126,5128,5131,5133,5135,5138,5140,5143,5145,5148,5150,5152,5155,5157],{"class":54,"line":94},[52,5117,5118],{"class":62},"parser",[52,5120,186],{"class":102},[52,5122,5123],{"class":189},"add_argument",[52,5125,193],{"class":102},[52,5127,115],{"class":83},[52,5129,5130],{"class":87},"x",[52,5132,115],{"class":83},[52,5134,199],{"class":102},[52,5136,5137],{"class":202}," type",[52,5139,80],{"class":79},[52,5141,5142],{"class":4733},"int",[52,5144,199],{"class":102},[52,5146,5147],{"class":202}," help",[52,5149,80],{"class":79},[52,5151,115],{"class":83},[52,5153,5154],{"class":87},"First number",[52,5156,115],{"class":83},[52,5158,211],{"class":102},[52,5160,5161,5163,5165,5167,5169,5171,5174,5176,5178,5180,5182,5184,5186,5188,5190,5192,5195,5197],{"class":54,"line":106},[52,5162,5118],{"class":62},[52,5164,186],{"class":102},[52,5166,5123],{"class":189},[52,5168,193],{"class":102},[52,5170,115],{"class":83},[52,5172,5173],{"class":87},"y",[52,5175,115],{"class":83},[52,5177,199],{"class":102},[52,5179,5137],{"class":202},[52,5181,80],{"class":79},[52,5183,5142],{"class":4733},[52,5185,199],{"class":102},[52,5187,5147],{"class":202},[52,5189,80],{"class":79},[52,5191,115],{"class":83},[52,5193,5194],{"class":87},"Second number",[52,5196,115],{"class":83},[52,5198,211],{"class":102},[52,5200,5201,5204,5206,5209,5211,5214],{"class":54,"line":128},[52,5202,5203],{"class":62},"args ",[52,5205,80],{"class":79},[52,5207,5208],{"class":62}," parser",[52,5210,186],{"class":102},[52,5212,5213],{"class":189},"parse_args",[52,5215,230],{"class":102},[52,5217,5218],{"class":54,"line":148},[52,5219,70],{"emptyLinePlaceholder":69},[52,5221,5222,5224,5226,5228,5230,5232,5235,5238,5240,5242],{"class":54,"line":164},[52,5223,242],{"class":241},[52,5225,193],{"class":102},[52,5227,2547],{"class":189},[52,5229,186],{"class":102},[52,5231,5130],{"class":1320},[52,5233,5234],{"class":79}," +",[52,5236,5237],{"class":189}," args",[52,5239,186],{"class":102},[52,5241,5173],{"class":1320},[52,5243,211],{"class":102},[14,5245,5246],{},"Run it like this:",[42,5248,5250],{"className":390,"code":5249,"language":392,"meta":47,"style":47},"python app.py 3 5\n",[49,5251,5252],{"__ignoreMap":47},[52,5253,5254,5256,5259,5262],{"class":54,"line":55},[52,5255,46],{"class":399},[52,5257,5258],{"class":87}," app.py",[52,5260,5261],{"class":121}," 3",[52,5263,5264],{"class":121}," 5\n",[14,5266,5267],{},"Output:",[42,5269,5272],{"className":5270,"code":5271,"language":955,"meta":47},[953],"8\n",[49,5273,5271],{"__ignoreMap":47},[37,5275,323],{"id":322},[14,5277,5278],{},"This example is small on purpose.",[14,5280,5281],{},"It shows how to build a real command line program that:",[309,5283,5284,5290,5293,5296],{},[24,5285,5286,5287,5289],{},"uses ",[49,5288,5043],{}," to read values passed after the script name",[24,5291,5292],{},"expects two numbers from the user",[24,5294,5295],{},"converts those values to integers",[24,5297,5298],{},"prints the result",[14,5300,5301,5302,5304],{},"If you want a fast, working ",[49,5303,5043],{}," example, this is a good place to start.",[37,5306,5308],{"id":5307},"when-to-use-argparse","When to use argparse",[14,5310,3650,5311,5313],{},[49,5312,5043],{}," when you run a Python script from the terminal and want to pass extra values to it.",[14,5315,5316],{},"It is useful for scripts that need:",[309,5318,5319,5322,5325,5328],{},[24,5320,5321],{},"file names",[24,5323,5324],{},"numbers",[24,5326,5327],{},"names",[24,5329,5330,5331,857,5334],{},"flags such as ",[49,5332,5333],{},"--verbose",[49,5335,5336],{},"--uppercase",[14,5338,5339,5340,5342],{},"For beginners, ",[49,5341,5043],{}," is usually better than reading raw arguments manually because it gives you:",[309,5344,5345,5348,5351],{},[24,5346,5347],{},"automatic help messages",[24,5349,5350],{},"type conversion",[24,5352,5353],{},"clearer error messages",[14,5355,5356,5357,759,5361,5365],{},"If you are new to terminal arguments, you may also want to read ",[372,5358,5360],{"href":5359},"\u002Fhow-to\u002Fhow-to-use-command-line-arguments-in-python\u002F","how to use command line arguments in Python",[372,5362,5364],{"href":5363},"\u002Fstandard-library\u002Fsys.argv-explained","sys.argv explained"," later.",[37,5367,5369],{"id":5368},"main-parts-of-the-example","Main parts of the example",[14,5371,5372],{},"The script has four main steps.",[639,5374,5376,5377,5379],{"id":5375},"_1-create-an-argumentparser-object","1. Create an ",[49,5378,5097],{}," object",[42,5381,5383],{"className":44,"code":5382,"language":46,"meta":47,"style":47},"parser = argparse.ArgumentParser(description=\"Add two numbers\")\n",[49,5384,5385],{"__ignoreMap":47},[52,5386,5387,5389,5391,5393,5395,5397,5399,5401,5403,5405,5407,5409],{"class":54,"line":55},[52,5388,5087],{"class":62},[52,5390,80],{"class":79},[52,5392,5092],{"class":62},[52,5394,186],{"class":102},[52,5396,5097],{"class":189},[52,5398,193],{"class":102},[52,5400,5102],{"class":202},[52,5402,80],{"class":79},[52,5404,115],{"class":83},[52,5406,5109],{"class":87},[52,5408,115],{"class":83},[52,5410,211],{"class":102},[14,5412,5413],{},"This creates the parser and sets a short description.",[639,5415,5417],{"id":5416},"_2-add-expected-arguments","2. Add expected arguments",[42,5419,5421],{"className":44,"code":5420,"language":46,"meta":47,"style":47},"parser.add_argument(\"x\", type=int, help=\"First number\")\nparser.add_argument(\"y\", type=int, help=\"Second number\")\n",[49,5422,5423,5461],{"__ignoreMap":47},[52,5424,5425,5427,5429,5431,5433,5435,5437,5439,5441,5443,5445,5447,5449,5451,5453,5455,5457,5459],{"class":54,"line":55},[52,5426,5118],{"class":62},[52,5428,186],{"class":102},[52,5430,5123],{"class":189},[52,5432,193],{"class":102},[52,5434,115],{"class":83},[52,5436,5130],{"class":87},[52,5438,115],{"class":83},[52,5440,199],{"class":102},[52,5442,5137],{"class":202},[52,5444,80],{"class":79},[52,5446,5142],{"class":4733},[52,5448,199],{"class":102},[52,5450,5147],{"class":202},[52,5452,80],{"class":79},[52,5454,115],{"class":83},[52,5456,5154],{"class":87},[52,5458,115],{"class":83},[52,5460,211],{"class":102},[52,5462,5463,5465,5467,5469,5471,5473,5475,5477,5479,5481,5483,5485,5487,5489,5491,5493,5495,5497],{"class":54,"line":66},[52,5464,5118],{"class":62},[52,5466,186],{"class":102},[52,5468,5123],{"class":189},[52,5470,193],{"class":102},[52,5472,115],{"class":83},[52,5474,5173],{"class":87},[52,5476,115],{"class":83},[52,5478,199],{"class":102},[52,5480,5137],{"class":202},[52,5482,80],{"class":79},[52,5484,5142],{"class":4733},[52,5486,199],{"class":102},[52,5488,5147],{"class":202},[52,5490,80],{"class":79},[52,5492,115],{"class":83},[52,5494,5194],{"class":87},[52,5496,115],{"class":83},[52,5498,211],{"class":102},[14,5500,5501,5502,118],{},"These lines define two ",[5503,5504,5505],"strong",{},"positional arguments",[309,5507,5508,5512],{},[24,5509,5510],{},[49,5511,5130],{},[24,5513,5514],{},[49,5515,5173],{},[14,5517,5518],{},"They are required, and the user must provide them in the correct order.",[639,5520,5522],{"id":5521},"_3-parse-the-command-line-input","3. Parse the command line input",[42,5524,5526],{"className":44,"code":5525,"language":46,"meta":47,"style":47},"args = parser.parse_args()\n",[49,5527,5528],{"__ignoreMap":47},[52,5529,5530,5532,5534,5536,5538,5540],{"class":54,"line":55},[52,5531,5203],{"class":62},[52,5533,80],{"class":79},[52,5535,5208],{"class":62},[52,5537,186],{"class":102},[52,5539,5213],{"class":189},[52,5541,230],{"class":102},[14,5543,5544],{},"This reads the values typed in the terminal.",[639,5546,5548],{"id":5547},"_4-use-the-values","4. Use the values",[42,5550,5552],{"className":44,"code":5551,"language":46,"meta":47,"style":47},"print(args.x + args.y)\n",[49,5553,5554],{"__ignoreMap":47},[52,5555,5556,5558,5560,5562,5564,5566,5568,5570,5572,5574],{"class":54,"line":55},[52,5557,242],{"class":241},[52,5559,193],{"class":102},[52,5561,2547],{"class":189},[52,5563,186],{"class":102},[52,5565,5130],{"class":1320},[52,5567,5234],{"class":79},[52,5569,5237],{"class":189},[52,5571,186],{"class":102},[52,5573,5173],{"class":1320},[52,5575,211],{"class":102},[14,5577,5578,5579,759,5582,186],{},"After parsing, you can access the values with names like ",[49,5580,5581],{},"args.x",[49,5583,5584],{},"args.y",[37,5586,5588],{"id":5587},"example-walkthrough","Example walkthrough",[14,5590,5591],{},"Here is the full script again:",[42,5593,5594],{"className":44,"code":5069,"language":46,"meta":47,"style":47},[49,5595,5596,5602,5606,5632,5670,5708,5722,5726],{"__ignoreMap":47},[52,5597,5598,5600],{"class":54,"line":55},[52,5599,59],{"class":58},[52,5601,5078],{"class":62},[52,5603,5604],{"class":54,"line":66},[52,5605,70],{"emptyLinePlaceholder":69},[52,5607,5608,5610,5612,5614,5616,5618,5620,5622,5624,5626,5628,5630],{"class":54,"line":73},[52,5609,5087],{"class":62},[52,5611,80],{"class":79},[52,5613,5092],{"class":62},[52,5615,186],{"class":102},[52,5617,5097],{"class":189},[52,5619,193],{"class":102},[52,5621,5102],{"class":202},[52,5623,80],{"class":79},[52,5625,115],{"class":83},[52,5627,5109],{"class":87},[52,5629,115],{"class":83},[52,5631,211],{"class":102},[52,5633,5634,5636,5638,5640,5642,5644,5646,5648,5650,5652,5654,5656,5658,5660,5662,5664,5666,5668],{"class":54,"line":94},[52,5635,5118],{"class":62},[52,5637,186],{"class":102},[52,5639,5123],{"class":189},[52,5641,193],{"class":102},[52,5643,115],{"class":83},[52,5645,5130],{"class":87},[52,5647,115],{"class":83},[52,5649,199],{"class":102},[52,5651,5137],{"class":202},[52,5653,80],{"class":79},[52,5655,5142],{"class":4733},[52,5657,199],{"class":102},[52,5659,5147],{"class":202},[52,5661,80],{"class":79},[52,5663,115],{"class":83},[52,5665,5154],{"class":87},[52,5667,115],{"class":83},[52,5669,211],{"class":102},[52,5671,5672,5674,5676,5678,5680,5682,5684,5686,5688,5690,5692,5694,5696,5698,5700,5702,5704,5706],{"class":54,"line":106},[52,5673,5118],{"class":62},[52,5675,186],{"class":102},[52,5677,5123],{"class":189},[52,5679,193],{"class":102},[52,5681,115],{"class":83},[52,5683,5173],{"class":87},[52,5685,115],{"class":83},[52,5687,199],{"class":102},[52,5689,5137],{"class":202},[52,5691,80],{"class":79},[52,5693,5142],{"class":4733},[52,5695,199],{"class":102},[52,5697,5147],{"class":202},[52,5699,80],{"class":79},[52,5701,115],{"class":83},[52,5703,5194],{"class":87},[52,5705,115],{"class":83},[52,5707,211],{"class":102},[52,5709,5710,5712,5714,5716,5718,5720],{"class":54,"line":128},[52,5711,5203],{"class":62},[52,5713,80],{"class":79},[52,5715,5208],{"class":62},[52,5717,186],{"class":102},[52,5719,5213],{"class":189},[52,5721,230],{"class":102},[52,5723,5724],{"class":54,"line":148},[52,5725,70],{"emptyLinePlaceholder":69},[52,5727,5728,5730,5732,5734,5736,5738,5740,5742,5744,5746],{"class":54,"line":164},[52,5729,242],{"class":241},[52,5731,193],{"class":102},[52,5733,2547],{"class":189},[52,5735,186],{"class":102},[52,5737,5130],{"class":1320},[52,5739,5234],{"class":79},[52,5741,5237],{"class":189},[52,5743,186],{"class":102},[52,5745,5173],{"class":1320},[52,5747,211],{"class":102},[14,5749,5750],{},"What each part does:",[309,5752,5753,5759,5765,5774,5780],{},[24,5754,5755,5758],{},[49,5756,5757],{},"description=\"Add two numbers\""," sets the help text shown to the user",[24,5760,5761,5764],{},[49,5762,5763],{},"type=int"," converts the input from text to integers automatically",[24,5766,5767,759,5770,5773],{},[49,5768,5769],{},"help=\"First number\"",[49,5771,5772],{},"help=\"Second number\""," describe each argument",[24,5775,5776,5779],{},[49,5777,5778],{},"args = parser.parse_args()"," reads the command line input",[24,5781,5782,5785],{},[49,5783,5784],{},"print(args.x + args.y)"," uses the parsed values",[14,5787,5788,5789,5791],{},"Without ",[49,5790,5763],{},", the arguments would be read as text.",[14,5793,5794,5795,759,5798,5801,5802,5804,5805,186],{},"For example, ",[49,5796,5797],{},"\"3\"",[49,5799,5800],{},"\"5\""," are strings at first. ",[49,5803,5043],{}," converts them for you when you use ",[49,5806,5763],{},[37,5808,5810],{"id":5809},"positional-arguments-vs-optional-arguments","Positional arguments vs optional arguments",[14,5812,5813,5814,186],{},"There are two common kinds of arguments in ",[49,5815,5043],{},[639,5817,5819],{"id":5818},"positional-arguments","Positional arguments",[14,5821,5822],{},"Positional arguments:",[309,5824,5825,5828,5831],{},[24,5826,5827],{},"are usually required",[24,5829,5830],{},"are given by order",[24,5832,5833],{},"do not use dashes",[14,5835,4259],{},[42,5837,5839],{"className":44,"code":5838,"language":46,"meta":47,"style":47},"parser.add_argument(\"name\")\n",[49,5840,5841],{"__ignoreMap":47},[52,5842,5843,5845,5847,5849,5851,5853,5855,5857],{"class":54,"line":55},[52,5844,5118],{"class":62},[52,5846,186],{"class":102},[52,5848,5123],{"class":189},[52,5850,193],{"class":102},[52,5852,115],{"class":83},[52,5854,2147],{"class":87},[52,5856,115],{"class":83},[52,5858,211],{"class":102},[14,5860,387],{},[42,5862,5864],{"className":390,"code":5863,"language":392,"meta":47,"style":47},"python app.py Alice\n",[49,5865,5866],{"__ignoreMap":47},[52,5867,5868,5870,5872],{"class":54,"line":55},[52,5869,46],{"class":399},[52,5871,5258],{"class":87},[52,5873,5874],{"class":87}," Alice\n",[639,5876,5878],{"id":5877},"optional-arguments","Optional arguments",[14,5880,5881],{},"Optional arguments:",[309,5883,5884,5893,5896],{},[24,5885,5886,5887,857,5890],{},"usually start with ",[49,5888,5889],{},"-",[49,5891,5892],{},"--",[24,5894,5895],{},"are often used for settings or switches",[24,5897,5898],{},"do not have to be provided unless you make them required",[14,5900,4259],{},[42,5902,5904],{"className":44,"code":5903,"language":46,"meta":47,"style":47},"parser.add_argument(\"--uppercase\")\n",[49,5905,5906],{"__ignoreMap":47},[52,5907,5908,5910,5912,5914,5916,5918,5920,5922],{"class":54,"line":55},[52,5909,5118],{"class":62},[52,5911,186],{"class":102},[52,5913,5123],{"class":189},[52,5915,193],{"class":102},[52,5917,115],{"class":83},[52,5919,5336],{"class":87},[52,5921,115],{"class":83},[52,5923,211],{"class":102},[14,5925,387],{},[42,5927,5929],{"className":390,"code":5928,"language":392,"meta":47,"style":47},"python app.py Alice --uppercase yes\n",[49,5930,5931],{"__ignoreMap":47},[52,5932,5933,5935,5937,5940,5943],{"class":54,"line":55},[52,5934,46],{"class":399},[52,5936,5258],{"class":87},[52,5938,5939],{"class":87}," Alice",[52,5941,5942],{"class":420}," --uppercase",[52,5944,5945],{"class":87}," yes\n",[14,5947,5948],{},"For beginners, the main idea is simple:",[309,5950,5951,5954],{},[24,5952,5953],{},"use positional arguments for required input",[24,5955,5956],{},"use optional arguments for extra behavior",[37,5958,5960],{"id":5959},"second-example-with-an-optional-flag","Second example with an optional flag",[14,5962,5963,5964,5966],{},"Now let's build a second script that accepts a name and an optional ",[49,5965,5336],{}," flag.",[42,5968,5970],{"className":44,"code":5969,"language":46,"meta":47,"style":47},"import argparse\n\nparser = argparse.ArgumentParser(description=\"Greet a user\")\nparser.add_argument(\"name\", help=\"The person's name\")\nparser.add_argument(\n    \"--uppercase\",\n    action=\"store_true\",\n    help=\"Show the greeting in uppercase\"\n)\n\nargs = parser.parse_args()\n\nmessage = f\"Hello, {args.name}\"\n\nif args.uppercase:\n    message = message.upper()\n\nprint(message)\n",[49,5971,5972,5978,5982,6009,6040,6051,6061,6077,6091,6095,6099,6113,6117,6142,6146,6159,6176,6180],{"__ignoreMap":47},[52,5973,5974,5976],{"class":54,"line":55},[52,5975,59],{"class":58},[52,5977,5078],{"class":62},[52,5979,5980],{"class":54,"line":66},[52,5981,70],{"emptyLinePlaceholder":69},[52,5983,5984,5986,5988,5990,5992,5994,5996,5998,6000,6002,6005,6007],{"class":54,"line":73},[52,5985,5087],{"class":62},[52,5987,80],{"class":79},[52,5989,5092],{"class":62},[52,5991,186],{"class":102},[52,5993,5097],{"class":189},[52,5995,193],{"class":102},[52,5997,5102],{"class":202},[52,5999,80],{"class":79},[52,6001,115],{"class":83},[52,6003,6004],{"class":87},"Greet a user",[52,6006,115],{"class":83},[52,6008,211],{"class":102},[52,6010,6011,6013,6015,6017,6019,6021,6023,6025,6027,6029,6031,6033,6036,6038],{"class":54,"line":94},[52,6012,5118],{"class":62},[52,6014,186],{"class":102},[52,6016,5123],{"class":189},[52,6018,193],{"class":102},[52,6020,115],{"class":83},[52,6022,2147],{"class":87},[52,6024,115],{"class":83},[52,6026,199],{"class":102},[52,6028,5147],{"class":202},[52,6030,80],{"class":79},[52,6032,115],{"class":83},[52,6034,6035],{"class":87},"The person's name",[52,6037,115],{"class":83},[52,6039,211],{"class":102},[52,6041,6042,6044,6046,6048],{"class":54,"line":106},[52,6043,5118],{"class":62},[52,6045,186],{"class":102},[52,6047,5123],{"class":189},[52,6049,6050],{"class":102},"(\n",[52,6052,6053,6055,6057,6059],{"class":54,"line":128},[52,6054,109],{"class":83},[52,6056,5336],{"class":87},[52,6058,115],{"class":83},[52,6060,125],{"class":102},[52,6062,6063,6066,6068,6070,6073,6075],{"class":54,"line":148},[52,6064,6065],{"class":202},"    action",[52,6067,80],{"class":79},[52,6069,115],{"class":83},[52,6071,6072],{"class":87},"store_true",[52,6074,115],{"class":83},[52,6076,125],{"class":102},[52,6078,6079,6082,6084,6086,6089],{"class":54,"line":164},[52,6080,6081],{"class":202},"    help",[52,6083,80],{"class":79},[52,6085,115],{"class":83},[52,6087,6088],{"class":87},"Show the greeting in uppercase",[52,6090,91],{"class":83},[52,6092,6093],{"class":54,"line":170},[52,6094,211],{"class":102},[52,6096,6097],{"class":54,"line":175},[52,6098,70],{"emptyLinePlaceholder":69},[52,6100,6101,6103,6105,6107,6109,6111],{"class":54,"line":214},[52,6102,5203],{"class":62},[52,6104,80],{"class":79},[52,6106,5208],{"class":62},[52,6108,186],{"class":102},[52,6110,5213],{"class":189},[52,6112,230],{"class":102},[52,6114,6115],{"class":54,"line":233},[52,6116,70],{"emptyLinePlaceholder":69},[52,6118,6119,6122,6124,6127,6130,6132,6134,6136,6138,6140],{"class":54,"line":238},[52,6120,6121],{"class":62},"message ",[52,6123,80],{"class":79},[52,6125,6126],{"class":1475}," f",[52,6128,6129],{"class":87},"\"Hello, ",[52,6131,1482],{"class":121},[52,6133,2547],{"class":62},[52,6135,186],{"class":102},[52,6137,2147],{"class":1320},[52,6139,1488],{"class":121},[52,6141,91],{"class":87},[52,6143,6144],{"class":54,"line":272},[52,6145,70],{"emptyLinePlaceholder":69},[52,6147,6148,6150,6152,6154,6157],{"class":54,"line":1348},[52,6149,1313],{"class":58},[52,6151,5237],{"class":62},[52,6153,186],{"class":102},[52,6155,6156],{"class":1320},"uppercase",[52,6158,1330],{"class":102},[52,6160,6161,6164,6166,6169,6171,6174],{"class":54,"line":1376},[52,6162,6163],{"class":62},"    message ",[52,6165,80],{"class":79},[52,6167,6168],{"class":62}," message",[52,6170,186],{"class":102},[52,6172,6173],{"class":189},"upper",[52,6175,230],{"class":102},[52,6177,6178],{"class":54,"line":1381},[52,6179,70],{"emptyLinePlaceholder":69},[52,6181,6182,6184,6186,6189],{"class":54,"line":1406},[52,6183,242],{"class":241},[52,6185,193],{"class":102},[52,6187,6188],{"class":189},"message",[52,6190,211],{"class":102},[639,6192,6194],{"id":6193},"how-this-works","How this works",[14,6196,6197],{},"The important new part is:",[42,6199,6201],{"className":44,"code":6200,"language":46,"meta":47,"style":47},"action=\"store_true\"\n",[49,6202,6203],{"__ignoreMap":47},[52,6204,6205,6208,6210,6212,6214],{"class":54,"line":55},[52,6206,6207],{"class":62},"action",[52,6209,80],{"class":79},[52,6211,115],{"class":83},[52,6213,6072],{"class":87},[52,6215,91],{"class":83},[14,6217,6218],{},"This means:",[309,6220,6221,6234],{},[24,6222,6223,6224,6226,6227,6230,6231],{},"if ",[49,6225,5336],{}," is included, ",[49,6228,6229],{},"args.uppercase"," becomes ",[49,6232,6233],{},"True",[24,6235,6236,6237,6230,6239],{},"if it is not included, ",[49,6238,6229],{},[49,6240,6241],{},"False",[639,6243,6245],{"id":6244},"run-it-without-the-flag","Run it without the flag",[42,6247,6248],{"className":390,"code":5863,"language":392,"meta":47,"style":47},[49,6249,6250],{"__ignoreMap":47},[52,6251,6252,6254,6256],{"class":54,"line":55},[52,6253,46],{"class":399},[52,6255,5258],{"class":87},[52,6257,5874],{"class":87},[14,6259,5267],{},[42,6261,6264],{"className":6262,"code":6263,"language":955,"meta":47},[953],"Hello, Alice\n",[49,6265,6263],{"__ignoreMap":47},[639,6267,6269],{"id":6268},"run-it-with-the-flag","Run it with the flag",[42,6271,6273],{"className":390,"code":6272,"language":392,"meta":47,"style":47},"python app.py Alice --uppercase\n",[49,6274,6275],{"__ignoreMap":47},[52,6276,6277,6279,6281,6283],{"class":54,"line":55},[52,6278,46],{"class":399},[52,6280,5258],{"class":87},[52,6282,5939],{"class":87},[52,6284,6285],{"class":420}," --uppercase\n",[14,6287,5267],{},[42,6289,6292],{"className":6290,"code":6291,"language":955,"meta":47},[953],"HELLO, ALICE\n",[49,6293,6291],{"__ignoreMap":47},[14,6295,6296],{},"This is a common pattern for command line tools.",[37,6298,6300],{"id":6299},"how-to-run-the-script","How to run the script",[14,6302,6303,6304,186],{},"Save one of the examples in a file named ",[49,6305,6306],{},"app.py",[14,6308,6309],{},"Then open a terminal in the same folder.",[639,6311,6313],{"id":6312},"example-1-add-two-numbers","Example 1: add two numbers",[42,6315,6316],{"className":390,"code":5249,"language":392,"meta":47,"style":47},[49,6317,6318],{"__ignoreMap":47},[52,6319,6320,6322,6324,6326],{"class":54,"line":55},[52,6321,46],{"class":399},[52,6323,5258],{"class":87},[52,6325,5261],{"class":121},[52,6327,5264],{"class":121},[14,6329,5267],{},[42,6331,6333],{"className":6332,"code":5271,"language":955,"meta":47},[953],[49,6334,5271],{"__ignoreMap":47},[639,6336,6338],{"id":6337},"example-2-wrong-type","Example 2: wrong type",[42,6340,6342],{"className":390,"code":6341,"language":392,"meta":47,"style":47},"python app.py hello 5\n",[49,6343,6344],{"__ignoreMap":47},[52,6345,6346,6348,6350,6353],{"class":54,"line":55},[52,6347,46],{"class":399},[52,6349,5258],{"class":87},[52,6351,6352],{"class":87}," hello",[52,6354,5264],{"class":121},[14,6356,6357,6358,6360,6361,6364,6365,186],{},"If your script expects integers, ",[49,6359,5043],{}," will show an error because ",[49,6362,6363],{},"hello"," cannot be converted to ",[49,6366,5142],{},[639,6368,6370],{"id":6369},"example-3-greeting-script","Example 3: greeting script",[42,6372,6373],{"className":390,"code":6272,"language":392,"meta":47,"style":47},[49,6374,6375],{"__ignoreMap":47},[52,6376,6377,6379,6381,6383],{"class":54,"line":55},[52,6378,46],{"class":399},[52,6380,5258],{"class":87},[52,6382,5939],{"class":87},[52,6384,6285],{"class":420},[14,6386,5267],{},[42,6388,6390],{"className":6389,"code":6291,"language":955,"meta":47},[953],[49,6391,6291],{"__ignoreMap":47},[14,6393,6394,6395,186],{},"If you are not sure how terminal input differs from normal keyboard input in a script, compare this with ",[372,6396,6398],{"href":6397},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python\u002F","how to get user input in Python",[37,6400,6402],{"id":6401},"common-beginner-mistakes","Common beginner mistakes",[14,6404,6405,6406,186],{},"These are some common problems when learning ",[49,6407,5043],{},[639,6409,6411],{"id":6410},"forgetting-required-positional-arguments","Forgetting required positional arguments",[14,6413,6414,6415,759,6417,6419],{},"If your script expects ",[49,6416,5130],{},[49,6418,5173],{},", this will fail:",[42,6421,6423],{"className":390,"code":6422,"language":392,"meta":47,"style":47},"python app.py 3\n",[49,6424,6425],{"__ignoreMap":47},[52,6426,6427,6429,6431],{"class":54,"line":55},[52,6428,46],{"class":399},[52,6430,5258],{"class":87},[52,6432,1085],{"class":121},[14,6434,6435,6437],{},[49,6436,5043],{}," will show an error because one required argument is missing.",[639,6439,6441,6442],{"id":6440},"passing-text-when-the-script-expects-int","Passing text when the script expects ",[49,6443,5142],{},[14,6445,6446,6447,6419],{},"If you use ",[49,6448,5763],{},[42,6450,6451],{"className":390,"code":6341,"language":392,"meta":47,"style":47},[49,6452,6453],{"__ignoreMap":47},[52,6454,6455,6457,6459,6461],{"class":54,"line":55},[52,6456,46],{"class":399},[52,6458,5258],{"class":87},[52,6460,6352],{"class":87},[52,6462,5264],{"class":121},[14,6464,6465,6466,6468,6469,186],{},"That happens because ",[49,6467,6363],{}," cannot be converted to an integer. A related error you may see in other code is ",[372,6470,6472],{"href":6471},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","ValueError: invalid literal for int() with base 10",[639,6474,6476],{"id":6475},"misspelling-an-optional-flag","Misspelling an optional flag",[14,6478,6479,6480,6482,6483,857,6486,6489],{},"If the flag is ",[49,6481,5336],{},", then ",[49,6484,6485],{},"--upper-case",[49,6487,6488],{},"--upper"," will not work unless you defined them.",[14,6491,6492],{},"Always type the flag name exactly as written in the script.",[639,6494,6496,6497,6499,6500],{"id":6495},"trying-to-use-args-before-calling-parse_args","Trying to use ",[49,6498,2547],{}," before calling ",[49,6501,6502],{},"parse_args()",[14,6504,6505],{},"This is wrong:",[42,6507,6509],{"className":44,"code":6508,"language":46,"meta":47,"style":47},"import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument(\"x\", type=int)\n\nprint(args.x)  # args does not exist yet\nargs = parser.parse_args()\n",[49,6510,6511,6517,6521,6535,6561,6565,6582],{"__ignoreMap":47},[52,6512,6513,6515],{"class":54,"line":55},[52,6514,59],{"class":58},[52,6516,5078],{"class":62},[52,6518,6519],{"class":54,"line":66},[52,6520,70],{"emptyLinePlaceholder":69},[52,6522,6523,6525,6527,6529,6531,6533],{"class":54,"line":73},[52,6524,5087],{"class":62},[52,6526,80],{"class":79},[52,6528,5092],{"class":62},[52,6530,186],{"class":102},[52,6532,5097],{"class":189},[52,6534,230],{"class":102},[52,6536,6537,6539,6541,6543,6545,6547,6549,6551,6553,6555,6557,6559],{"class":54,"line":94},[52,6538,5118],{"class":62},[52,6540,186],{"class":102},[52,6542,5123],{"class":189},[52,6544,193],{"class":102},[52,6546,115],{"class":83},[52,6548,5130],{"class":87},[52,6550,115],{"class":83},[52,6552,199],{"class":102},[52,6554,5137],{"class":202},[52,6556,80],{"class":79},[52,6558,5142],{"class":4733},[52,6560,211],{"class":102},[52,6562,6563],{"class":54,"line":106},[52,6564,70],{"emptyLinePlaceholder":69},[52,6566,6567,6569,6571,6573,6575,6577,6579],{"class":54,"line":128},[52,6568,242],{"class":241},[52,6570,193],{"class":102},[52,6572,2547],{"class":189},[52,6574,186],{"class":102},[52,6576,5130],{"class":1320},[52,6578,1521],{"class":102},[52,6580,6581],{"class":4368},"  # args does not exist yet\n",[52,6583,6584,6586,6588,6590,6592,6594],{"class":54,"line":148},[52,6585,5203],{"class":62},[52,6587,80],{"class":79},[52,6589,5208],{"class":62},[52,6591,186],{"class":102},[52,6593,5213],{"class":189},[52,6595,230],{"class":102},[14,6597,6598],{},"You must parse first:",[42,6600,6602],{"className":44,"code":6601,"language":46,"meta":47,"style":47},"import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument(\"x\", type=int)\n\nargs = parser.parse_args()\nprint(args.x)\n",[49,6603,6604,6610,6614,6628,6654,6658,6672],{"__ignoreMap":47},[52,6605,6606,6608],{"class":54,"line":55},[52,6607,59],{"class":58},[52,6609,5078],{"class":62},[52,6611,6612],{"class":54,"line":66},[52,6613,70],{"emptyLinePlaceholder":69},[52,6615,6616,6618,6620,6622,6624,6626],{"class":54,"line":73},[52,6617,5087],{"class":62},[52,6619,80],{"class":79},[52,6621,5092],{"class":62},[52,6623,186],{"class":102},[52,6625,5097],{"class":189},[52,6627,230],{"class":102},[52,6629,6630,6632,6634,6636,6638,6640,6642,6644,6646,6648,6650,6652],{"class":54,"line":94},[52,6631,5118],{"class":62},[52,6633,186],{"class":102},[52,6635,5123],{"class":189},[52,6637,193],{"class":102},[52,6639,115],{"class":83},[52,6641,5130],{"class":87},[52,6643,115],{"class":83},[52,6645,199],{"class":102},[52,6647,5137],{"class":202},[52,6649,80],{"class":79},[52,6651,5142],{"class":4733},[52,6653,211],{"class":102},[52,6655,6656],{"class":54,"line":106},[52,6657,70],{"emptyLinePlaceholder":69},[52,6659,6660,6662,6664,6666,6668,6670],{"class":54,"line":128},[52,6661,5203],{"class":62},[52,6663,80],{"class":79},[52,6665,5208],{"class":62},[52,6667,186],{"class":102},[52,6669,5213],{"class":189},[52,6671,230],{"class":102},[52,6673,6674,6676,6678,6680,6682,6684],{"class":54,"line":148},[52,6675,242],{"class":241},[52,6677,193],{"class":102},[52,6679,2547],{"class":189},[52,6681,186],{"class":102},[52,6683,5130],{"class":1320},[52,6685,211],{"class":102},[37,6687,6689],{"id":6688},"how-to-read-the-help-output","How to read the help output",[14,6691,6692,6693,6695],{},"One of the best things about ",[49,6694,5043],{}," is that it builds help text automatically.",[14,6697,387],{},[42,6699,6701],{"className":390,"code":6700,"language":392,"meta":47,"style":47},"python app.py --help\n",[49,6702,6703],{"__ignoreMap":47},[52,6704,6705,6707,6709],{"class":54,"line":55},[52,6706,46],{"class":399},[52,6708,5258],{"class":87},[52,6710,6711],{"class":420}," --help\n",[14,6713,6714],{},"You will see output similar to this:",[42,6716,6719],{"className":6717,"code":6718,"language":955,"meta":47},[953],"usage: app.py [-h] x y\n\nAdd two numbers\n\npositional arguments:\n  x           First number\n  y           Second number\n\noptions:\n  -h, --help  show this help message and exit\n",[49,6720,6718],{"__ignoreMap":47},[14,6722,6723],{},"This help output includes:",[309,6725,6726,6733,6741],{},[24,6727,6728,6729,6732],{},"the ",[5503,6730,6731],{},"usage"," line, which shows how to run the script",[24,6734,6728,6735,6737,6738],{},[5503,6736,5102],{},", which comes from ",[49,6739,6740],{},"ArgumentParser(...)",[24,6742,6728,6743,6737,6746],{},[5503,6744,6745],{},"argument help text",[49,6747,6748],{},"help=...",[14,6750,6751,6752,6754,6755,186],{},"This is one reason ",[49,6753,5043],{}," is easier to use than reading raw values from ",[372,6756,6757],{"href":5363},[49,6758,6759],{},"sys.argv",[37,6761,6763],{"id":6762},"what-to-learn-next","What to learn next",[14,6765,6766],{},"After this example, a good next step is to learn the bigger picture of command line arguments.",[14,6768,6769],{},"You can continue with:",[309,6771,6772,6776,6780,6786],{},[24,6773,6774],{},[372,6775,5360],{"href":5359},[24,6777,6778],{},[372,6779,5364],{"href":5363},[24,6781,6782],{},[372,6783,6785],{"href":6784},"\u002Fstandard-library\u002Fpython-sys-module-overview\u002F","Python sys module overview",[24,6787,6788],{},[372,6789,6791],{"href":6790},"\u002Fexamples\u002Fpython-command-line-tool-example\u002F","Python command line tool example",[37,6793,1942],{"id":1941},[639,6795,6797],{"id":6796},"what-is-argparse-in-python","What is argparse in Python?",[14,6799,6800,6802],{},[49,6801,5043],{}," is a standard library module for reading command line arguments and options.",[639,6804,6806],{"id":6805},"what-is-the-difference-between-positional-and-optional-arguments","What is the difference between positional and optional arguments?",[14,6808,6809,6810,186],{},"Positional arguments are required and depend on order. Optional arguments use flags like ",[49,6811,6812],{},"--name",[639,6814,6816],{"id":6815},"why-use-argparse-instead-of-sysargv","Why use argparse instead of sys.argv?",[14,6818,6819,6821],{},[49,6820,5043],{}," is easier for beginners because it adds help messages, type conversion, and clearer error messages.",[639,6823,6825],{"id":6824},"how-do-i-show-the-help-message","How do I show the help message?",[14,6827,6828,6829,6832],{},"Run the script with ",[49,6830,6831],{},"--help",", such as:",[42,6834,6835],{"className":390,"code":6700,"language":392,"meta":47,"style":47},[49,6836,6837],{"__ignoreMap":47},[52,6838,6839,6841,6843],{"class":54,"line":55},[52,6840,46],{"class":399},[52,6842,5258],{"class":87},[52,6844,6711],{"class":420},[639,6846,6848],{"id":6847},"can-argparse-convert-input-types-automatically","Can argparse convert input types automatically?",[14,6850,6851,6852,6854],{},"Yes. For example, ",[49,6853,5763],{}," converts an argument to an integer.",[37,6856,2005],{"id":2004},[309,6858,6859,6864,6868,6872,6877,6882,6888],{},[24,6860,6861],{},[372,6862,6863],{"href":5359},"How to use command line arguments in Python",[24,6865,6866],{},[372,6867,6785],{"href":6784},[24,6869,6870],{},[372,6871,5364],{"href":5363},[24,6873,6874],{},[372,6875,6876],{"href":6397},"How to get user input in Python",[24,6878,6879],{},[372,6880,6881],{"href":6471},"ValueError: invalid literal for int() with base 10 fix",[24,6883,6884],{},[372,6885,6887],{"href":6886},"\u002Ferrors\u002Ftypeerror-str-object-cannot-be-interpreted-as-an-integer-fix\u002F","TypeError: str object cannot be interpreted as an integer fix",[24,6889,6890],{},[372,6891,6791],{"href":6790},[2034,6893,6894],{},"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 .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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--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":47,"searchDepth":66,"depth":66,"links":6896},[6897,6898,6899,6900,6907,6908,6912,6917,6922,6930,6931,6932,6939],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":5307,"depth":66,"text":5308},{"id":5368,"depth":66,"text":5369,"children":6901},[6902,6904,6905,6906],{"id":5375,"depth":73,"text":6903},"1. Create an ArgumentParser object",{"id":5416,"depth":73,"text":5417},{"id":5521,"depth":73,"text":5522},{"id":5547,"depth":73,"text":5548},{"id":5587,"depth":66,"text":5588},{"id":5809,"depth":66,"text":5810,"children":6909},[6910,6911],{"id":5818,"depth":73,"text":5819},{"id":5877,"depth":73,"text":5878},{"id":5959,"depth":66,"text":5960,"children":6913},[6914,6915,6916],{"id":6193,"depth":73,"text":6194},{"id":6244,"depth":73,"text":6245},{"id":6268,"depth":73,"text":6269},{"id":6299,"depth":66,"text":6300,"children":6918},[6919,6920,6921],{"id":6312,"depth":73,"text":6313},{"id":6337,"depth":73,"text":6338},{"id":6369,"depth":73,"text":6370},{"id":6401,"depth":66,"text":6402,"children":6923},[6924,6925,6927,6928],{"id":6410,"depth":73,"text":6411},{"id":6440,"depth":73,"text":6926},"Passing text when the script expects int",{"id":6475,"depth":73,"text":6476},{"id":6495,"depth":73,"text":6929},"Trying to use args before calling parse_args()",{"id":6688,"depth":66,"text":6689},{"id":6762,"depth":66,"text":6763},{"id":1941,"depth":66,"text":1942,"children":6933},[6934,6935,6936,6937,6938],{"id":6796,"depth":73,"text":6797},{"id":6805,"depth":73,"text":6806},{"id":6815,"depth":73,"text":6816},{"id":6824,"depth":73,"text":6825},{"id":6847,"depth":73,"text":6848},{"id":2004,"depth":66,"text":2005},"Master python argument parser example argparse in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-argument-parser-example-argparse",{"title":5033,"description":6940},"examples\u002Fpython-argument-parser-example-argparse","XOFn2h8Ncn1JP5lb1WbxpiWX_7k4DjdQz1kPspE7000",{"id":6947,"title":6948,"body":6949,"description":9069,"extension":2075,"meta":9070,"navigation":69,"path":9071,"seo":9072,"stem":9073,"__hash__":9074},"content\u002Fexamples\u002Fpython-basic-class-example-oop.md","Python Basic Class Example (OOP)",{"type":7,"value":6950,"toc":9030},[6951,6954,6957,6960,6980,6983,6985,7166,7170,7190,7193,7213,7217,7220,7249,7256,7260,7263,7360,7363,7384,7387,7394,7399,7401,7452,7455,7486,7493,7524,7536,7540,7543,7570,7572,7593,7596,7683,7687,7701,7704,7711,7715,7721,7725,7784,7788,7801,7804,7808,7845,7849,7857,7860,7864,7883,7890,7894,7897,7903,7916,7921,7924,7930,7955,7958,7964,7969,7986,7992,7995,8001,8042,8047,8053,8059,8085,8093,8096,8110,8112,8115,8122,8125,8162,8165,8204,8210,8214,8216,8230,8232,8258,8261,8269,8271,8285,8287,8302,8307,8311,8313,8333,8335,8354,8361,8365,8367,8424,8426,8483,8490,8494,8497,8516,8518,8796,8800,8832,8834,8838,8841,8861,8864,8867,8937,8940,8954,8956,8960,8963,8970,8975,8979,8982,8986,8989,8991,9021,9027],[10,6952,6948],{"id":6953},"python-basic-class-example-oop",[14,6955,6956],{},"This page shows a simple Python class example for beginners.",[14,6958,6959],{},"You will learn how to:",[309,6961,6962,6965,6968,6971,6974],{},[24,6963,6964],{},"Create a class",[24,6966,6967],{},"Make an object from that class",[24,6969,6970],{},"Store data in attributes",[24,6972,6973],{},"Add behavior with a method",[24,6975,3650,6976,6979],{},[49,6977,6978],{},"__init__"," to set starting values",[14,6981,6982],{},"If you are new to classes, this is a good first example because it uses a small real-world idea and keeps the code short.",[37,6984,40],{"id":39},[42,6986,6988],{"className":44,"code":6987,"language":46,"meta":47,"style":47},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def bark(self):\n        print(f\"{self.name} says woof!\")\n\nmy_dog = Dog(\"Max\", 3)\nprint(my_dog.name)\nprint(my_dog.age)\nmy_dog.bark()\n",[49,6989,6990,7000,7027,7042,7055,7059,7073,7098,7102,7126,7141,7155],{"__ignoreMap":47},[52,6991,6992,6995,6998],{"class":54,"line":55},[52,6993,6994],{"class":1475},"class",[52,6996,6997],{"class":399}," Dog",[52,6999,1330],{"class":102},[52,7001,7002,7005,7008,7010,7014,7016,7020,7022,7025],{"class":54,"line":66},[52,7003,7004],{"class":1475},"    def",[52,7006,7007],{"class":241}," __init__",[52,7009,193],{"class":102},[52,7011,7013],{"class":7012},"smCYv","self",[52,7015,199],{"class":102},[52,7017,7019],{"class":7018},"sFwrP"," name",[52,7021,199],{"class":102},[52,7023,7024],{"class":7018}," age",[52,7026,3114],{"class":102},[52,7028,7029,7032,7034,7036,7039],{"class":54,"line":73},[52,7030,7031],{"class":2533},"        self",[52,7033,186],{"class":102},[52,7035,2147],{"class":1320},[52,7037,7038],{"class":79}," =",[52,7040,7041],{"class":62}," name\n",[52,7043,7044,7046,7048,7050,7052],{"class":54,"line":94},[52,7045,7031],{"class":2533},[52,7047,186],{"class":102},[52,7049,2167],{"class":1320},[52,7051,7038],{"class":79},[52,7053,7054],{"class":62}," age\n",[52,7056,7057],{"class":54,"line":106},[52,7058,70],{"emptyLinePlaceholder":69},[52,7060,7061,7063,7067,7069,7071],{"class":54,"line":128},[52,7062,7004],{"class":1475},[52,7064,7066],{"class":7065},"sGLFI"," bark",[52,7068,193],{"class":102},[52,7070,7013],{"class":7012},[52,7072,3114],{"class":102},[52,7074,7075,7077,7079,7081,7083,7085,7087,7089,7091,7093,7096],{"class":54,"line":148},[52,7076,1470],{"class":241},[52,7078,193],{"class":102},[52,7080,1476],{"class":1475},[52,7082,115],{"class":87},[52,7084,1482],{"class":121},[52,7086,7013],{"class":2533},[52,7088,186],{"class":102},[52,7090,2147],{"class":1320},[52,7092,1488],{"class":121},[52,7094,7095],{"class":87}," says woof!\"",[52,7097,211],{"class":102},[52,7099,7100],{"class":54,"line":164},[52,7101,70],{"emptyLinePlaceholder":69},[52,7103,7104,7107,7109,7111,7113,7115,7118,7120,7122,7124],{"class":54,"line":170},[52,7105,7106],{"class":62},"my_dog ",[52,7108,80],{"class":79},[52,7110,6997],{"class":189},[52,7112,193],{"class":102},[52,7114,115],{"class":83},[52,7116,7117],{"class":87},"Max",[52,7119,115],{"class":83},[52,7121,199],{"class":102},[52,7123,5261],{"class":121},[52,7125,211],{"class":102},[52,7127,7128,7130,7132,7135,7137,7139],{"class":54,"line":175},[52,7129,242],{"class":241},[52,7131,193],{"class":102},[52,7133,7134],{"class":189},"my_dog",[52,7136,186],{"class":102},[52,7138,2147],{"class":1320},[52,7140,211],{"class":102},[52,7142,7143,7145,7147,7149,7151,7153],{"class":54,"line":214},[52,7144,242],{"class":241},[52,7146,193],{"class":102},[52,7148,7134],{"class":189},[52,7150,186],{"class":102},[52,7152,2167],{"class":1320},[52,7154,211],{"class":102},[52,7156,7157,7159,7161,7164],{"class":54,"line":233},[52,7158,7134],{"class":62},[52,7160,186],{"class":102},[52,7162,7163],{"class":189},"bark",[52,7165,230],{"class":102},[14,7167,7168],{},[5503,7169,5267],{},[42,7171,7173],{"className":44,"code":7172,"language":46,"meta":47,"style":47},"Max\n3\nMax says woof!\n",[49,7174,7175,7180,7185],{"__ignoreMap":47},[52,7176,7177],{"class":54,"line":55},[52,7178,7179],{"class":62},"Max\n",[52,7181,7182],{"class":54,"line":66},[52,7183,7184],{"class":121},"3\n",[52,7186,7187],{"class":54,"line":73},[52,7188,7189],{"class":62},"Max says woof!\n",[14,7191,7192],{},"Use this as your first working class example. It shows:",[309,7194,7195,7198,7204,7207,7210],{},[24,7196,7197],{},"A class",[24,7199,7200,7201,7203],{},"An ",[49,7202,6978],{}," method",[24,7205,7206],{},"Attributes",[24,7208,7209],{},"An object",[24,7211,7212],{},"A method call",[37,7214,7216],{"id":7215},"what-this-example-teaches","What this example teaches",[14,7218,7219],{},"This example helps you understand these basic OOP ideas:",[309,7221,7222,7227,7233,7238,7244],{},[24,7223,3233,7224,7226],{},[5503,7225,6994],{}," is a template or blueprint",[24,7228,7200,7229,7232],{},[5503,7230,7231],{},"object"," is one thing created from that class",[24,7234,7235,7237],{},[5503,7236,7206],{}," store data for the object",[24,7239,7240,7243],{},[5503,7241,7242],{},"Methods"," define what the object can do",[24,7245,7246,7248],{},[49,7247,6978],{}," sets the starting values when the object is created",[14,7250,7251,7252,186],{},"If you want a fuller beginner explanation, see ",[372,7253,7255],{"href":7254},"\u002Flearn\u002Fpython-classes-and-objects-explained\u002F","Python classes and objects explained",[37,7257,7259],{"id":7258},"the-example-class","The example class",[14,7261,7262],{},"Here is the full class again before we break it down:",[42,7264,7266],{"className":44,"code":7265,"language":46,"meta":47,"style":47},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def bark(self):\n        print(f\"{self.name} says woof!\")\n",[49,7267,7268,7276,7296,7308,7320,7324,7336],{"__ignoreMap":47},[52,7269,7270,7272,7274],{"class":54,"line":55},[52,7271,6994],{"class":1475},[52,7273,6997],{"class":399},[52,7275,1330],{"class":102},[52,7277,7278,7280,7282,7284,7286,7288,7290,7292,7294],{"class":54,"line":66},[52,7279,7004],{"class":1475},[52,7281,7007],{"class":241},[52,7283,193],{"class":102},[52,7285,7013],{"class":7012},[52,7287,199],{"class":102},[52,7289,7019],{"class":7018},[52,7291,199],{"class":102},[52,7293,7024],{"class":7018},[52,7295,3114],{"class":102},[52,7297,7298,7300,7302,7304,7306],{"class":54,"line":73},[52,7299,7031],{"class":2533},[52,7301,186],{"class":102},[52,7303,2147],{"class":1320},[52,7305,7038],{"class":79},[52,7307,7041],{"class":62},[52,7309,7310,7312,7314,7316,7318],{"class":54,"line":94},[52,7311,7031],{"class":2533},[52,7313,186],{"class":102},[52,7315,2167],{"class":1320},[52,7317,7038],{"class":79},[52,7319,7054],{"class":62},[52,7321,7322],{"class":54,"line":106},[52,7323,70],{"emptyLinePlaceholder":69},[52,7325,7326,7328,7330,7332,7334],{"class":54,"line":128},[52,7327,7004],{"class":1475},[52,7329,7066],{"class":7065},[52,7331,193],{"class":102},[52,7333,7013],{"class":7012},[52,7335,3114],{"class":102},[52,7337,7338,7340,7342,7344,7346,7348,7350,7352,7354,7356,7358],{"class":54,"line":148},[52,7339,1470],{"class":241},[52,7341,193],{"class":102},[52,7343,1476],{"class":1475},[52,7345,115],{"class":87},[52,7347,1482],{"class":121},[52,7349,7013],{"class":2533},[52,7351,186],{"class":102},[52,7353,2147],{"class":1320},[52,7355,1488],{"class":121},[52,7357,7095],{"class":87},[52,7359,211],{"class":102},[14,7361,7362],{},"This class uses:",[309,7364,7365,7371,7378],{},[24,7366,7367,7370],{},[49,7368,7369],{},"Dog"," as the class name",[24,7372,7373,759,7375,7377],{},[49,7374,2147],{},[49,7376,2167],{}," as the two attributes",[24,7379,7380,7383],{},[49,7381,7382],{},"bark()"," as the one method",[14,7385,7386],{},"This is a good beginner example because it is small and easy to read.",[37,7388,7390,7391,7393],{"id":7389},"how-the-__init__-method-works","How the ",[49,7392,6978],{}," method works",[14,7395,358,7396,7398],{},[49,7397,6978],{}," method runs when you create a new object.",[14,7400,752],{},[42,7402,7404],{"className":44,"code":7403,"language":46,"meta":47,"style":47},"def __init__(self, name, age):\n    self.name = name\n    self.age = age\n",[49,7405,7406,7427,7440],{"__ignoreMap":47},[52,7407,7408,7411,7413,7415,7417,7419,7421,7423,7425],{"class":54,"line":55},[52,7409,7410],{"class":1475},"def",[52,7412,7007],{"class":241},[52,7414,193],{"class":102},[52,7416,7013],{"class":7012},[52,7418,199],{"class":102},[52,7420,7019],{"class":7018},[52,7422,199],{"class":102},[52,7424,7024],{"class":7018},[52,7426,3114],{"class":102},[52,7428,7429,7432,7434,7436,7438],{"class":54,"line":66},[52,7430,7431],{"class":2533},"    self",[52,7433,186],{"class":102},[52,7435,2147],{"class":1320},[52,7437,7038],{"class":79},[52,7439,7041],{"class":62},[52,7441,7442,7444,7446,7448,7450],{"class":54,"line":73},[52,7443,7431],{"class":2533},[52,7445,186],{"class":102},[52,7447,2167],{"class":1320},[52,7449,7038],{"class":79},[52,7451,7054],{"class":62},[14,7453,7454],{},"Here is what each part means:",[309,7456,7457,7462,7467,7474,7480],{},[24,7458,7459,7461],{},[49,7460,6978],{}," is the special method Python uses to set up a new object",[24,7463,7464,7466],{},[49,7465,7013],{}," refers to the current object",[24,7468,7469,759,7471,7473],{},[49,7470,2147],{},[49,7472,2167],{}," are values passed in when the object is created",[24,7475,7476,7479],{},[49,7477,7478],{},"self.name = name"," stores the value in the object",[24,7481,7482,7485],{},[49,7483,7484],{},"self.age = age"," stores the age in the object",[14,7487,7488,7489,7492],{},"So if you create ",[49,7490,7491],{},"Dog(\"Max\", 3)",", Python does this setup:",[309,7494,7495,7502,7509,7517],{},[24,7496,7497,6230,7499],{},[49,7498,2147],{},[49,7500,7501],{},"\"Max\"",[24,7503,7504,6230,7506],{},[49,7505,2167],{},[49,7507,7508],{},"3",[24,7510,7511,7514,7515],{},[49,7512,7513],{},"self.name"," is set to ",[49,7516,7501],{},[24,7518,7519,7514,7522],{},[49,7520,7521],{},"self.age",[49,7523,7508],{},[14,7525,7526,7527,7529,7530,186],{},"If ",[49,7528,7013],{}," feels confusing, see ",[372,7531,6728,7533,7535],{"href":7532},"\u002Flearn\u002Fthe-__init__-method-in-python-explained\u002F",[49,7534,6978],{}," method in Python explained",[37,7537,7539],{"id":7538},"how-to-create-an-object","How to create an object",[14,7541,7542],{},"You create an object by calling the class name like a function:",[42,7544,7546],{"className":44,"code":7545,"language":46,"meta":47,"style":47},"my_dog = Dog(\"Max\", 3)\n",[49,7547,7548],{"__ignoreMap":47},[52,7549,7550,7552,7554,7556,7558,7560,7562,7564,7566,7568],{"class":54,"line":55},[52,7551,7106],{"class":62},[52,7553,80],{"class":79},[52,7555,6997],{"class":189},[52,7557,193],{"class":102},[52,7559,115],{"class":83},[52,7561,7117],{"class":87},[52,7563,115],{"class":83},[52,7565,199],{"class":102},[52,7567,5261],{"class":121},[52,7569,211],{"class":102},[14,7571,6218],{},[309,7573,7574,7579,7584],{},[24,7575,7576,7578],{},[49,7577,7369],{}," is the class",[24,7580,7581,7583],{},[49,7582,7134],{}," is the object",[24,7585,7586,759,7588,7590,7591],{},[49,7587,7501],{},[49,7589,7508],{}," are passed into ",[49,7592,6978],{},[14,7594,7595],{},"You can create more than one object from the same class:",[42,7597,7599],{"className":44,"code":7598,"language":46,"meta":47,"style":47},"dog1 = Dog(\"Max\", 3)\ndog2 = Dog(\"Bella\", 5)\n\nprint(dog1.name)\nprint(dog2.name)\n",[49,7600,7601,7624,7649,7653,7668],{"__ignoreMap":47},[52,7602,7603,7606,7608,7610,7612,7614,7616,7618,7620,7622],{"class":54,"line":55},[52,7604,7605],{"class":62},"dog1 ",[52,7607,80],{"class":79},[52,7609,6997],{"class":189},[52,7611,193],{"class":102},[52,7613,115],{"class":83},[52,7615,7117],{"class":87},[52,7617,115],{"class":83},[52,7619,199],{"class":102},[52,7621,5261],{"class":121},[52,7623,211],{"class":102},[52,7625,7626,7629,7631,7633,7635,7637,7640,7642,7644,7647],{"class":54,"line":66},[52,7627,7628],{"class":62},"dog2 ",[52,7630,80],{"class":79},[52,7632,6997],{"class":189},[52,7634,193],{"class":102},[52,7636,115],{"class":83},[52,7638,7639],{"class":87},"Bella",[52,7641,115],{"class":83},[52,7643,199],{"class":102},[52,7645,7646],{"class":121}," 5",[52,7648,211],{"class":102},[52,7650,7651],{"class":54,"line":73},[52,7652,70],{"emptyLinePlaceholder":69},[52,7654,7655,7657,7659,7662,7664,7666],{"class":54,"line":94},[52,7656,242],{"class":241},[52,7658,193],{"class":102},[52,7660,7661],{"class":189},"dog1",[52,7663,186],{"class":102},[52,7665,2147],{"class":1320},[52,7667,211],{"class":102},[52,7669,7670,7672,7674,7677,7679,7681],{"class":54,"line":106},[52,7671,242],{"class":241},[52,7673,193],{"class":102},[52,7675,7676],{"class":189},"dog2",[52,7678,186],{"class":102},[52,7680,2147],{"class":1320},[52,7682,211],{"class":102},[14,7684,7685],{},[5503,7686,5267],{},[42,7688,7690],{"className":44,"code":7689,"language":46,"meta":47,"style":47},"Max\nBella\n",[49,7691,7692,7696],{"__ignoreMap":47},[52,7693,7694],{"class":54,"line":55},[52,7695,7179],{"class":62},[52,7697,7698],{"class":54,"line":66},[52,7699,7700],{"class":62},"Bella\n",[14,7702,7703],{},"Both objects use the same class, but each object can store different values.",[14,7705,7706,7707,186],{},"You can learn more in ",[372,7708,7710],{"href":7709},"\u002Fhow-to\u002Fhow-to-create-an-object-in-python\u002F","how to create an object in Python",[37,7712,7714],{"id":7713},"how-to-access-attributes-and-call-methods","How to access attributes and call methods",[14,7716,3650,7717,7720],{},[5503,7718,7719],{},"dot notation"," to work with object data and behavior.",[639,7722,7724],{"id":7723},"read-attributes","Read attributes",[42,7726,7728],{"className":44,"code":7727,"language":46,"meta":47,"style":47},"my_dog = Dog(\"Max\", 3)\n\nprint(my_dog.name)\nprint(my_dog.age)\n",[49,7729,7730,7752,7756,7770],{"__ignoreMap":47},[52,7731,7732,7734,7736,7738,7740,7742,7744,7746,7748,7750],{"class":54,"line":55},[52,7733,7106],{"class":62},[52,7735,80],{"class":79},[52,7737,6997],{"class":189},[52,7739,193],{"class":102},[52,7741,115],{"class":83},[52,7743,7117],{"class":87},[52,7745,115],{"class":83},[52,7747,199],{"class":102},[52,7749,5261],{"class":121},[52,7751,211],{"class":102},[52,7753,7754],{"class":54,"line":66},[52,7755,70],{"emptyLinePlaceholder":69},[52,7757,7758,7760,7762,7764,7766,7768],{"class":54,"line":73},[52,7759,242],{"class":241},[52,7761,193],{"class":102},[52,7763,7134],{"class":189},[52,7765,186],{"class":102},[52,7767,2147],{"class":1320},[52,7769,211],{"class":102},[52,7771,7772,7774,7776,7778,7780,7782],{"class":54,"line":94},[52,7773,242],{"class":241},[52,7775,193],{"class":102},[52,7777,7134],{"class":189},[52,7779,186],{"class":102},[52,7781,2167],{"class":1320},[52,7783,211],{"class":102},[14,7785,7786],{},[5503,7787,5267],{},[42,7789,7791],{"className":44,"code":7790,"language":46,"meta":47,"style":47},"Max\n3\n",[49,7792,7793,7797],{"__ignoreMap":47},[52,7794,7795],{"class":54,"line":55},[52,7796,7179],{"class":62},[52,7798,7799],{"class":54,"line":66},[52,7800,7184],{"class":121},[14,7802,7803],{},"This reads stored data from the object.",[639,7805,7807],{"id":7806},"call-a-method","Call a method",[42,7809,7811],{"className":44,"code":7810,"language":46,"meta":47,"style":47},"my_dog = Dog(\"Max\", 3)\nmy_dog.bark()\n",[49,7812,7813,7835],{"__ignoreMap":47},[52,7814,7815,7817,7819,7821,7823,7825,7827,7829,7831,7833],{"class":54,"line":55},[52,7816,7106],{"class":62},[52,7818,80],{"class":79},[52,7820,6997],{"class":189},[52,7822,193],{"class":102},[52,7824,115],{"class":83},[52,7826,7117],{"class":87},[52,7828,115],{"class":83},[52,7830,199],{"class":102},[52,7832,5261],{"class":121},[52,7834,211],{"class":102},[52,7836,7837,7839,7841,7843],{"class":54,"line":66},[52,7838,7134],{"class":62},[52,7840,186],{"class":102},[52,7842,7163],{"class":189},[52,7844,230],{"class":102},[14,7846,7847],{},[5503,7848,5267],{},[42,7850,7851],{"className":44,"code":7189,"language":46,"meta":47,"style":47},[49,7852,7853],{"__ignoreMap":47},[52,7854,7855],{"class":54,"line":55},[52,7856,7189],{"class":62},[14,7858,7859],{},"This runs behavior defined in the class.",[639,7861,7863],{"id":7862},"data-vs-behavior","Data vs behavior",[309,7865,7866,7872,7877],{},[24,7867,7868,7871],{},[49,7869,7870],{},"my_dog.name"," reads data",[24,7873,7874,7871],{},[49,7875,7876],{},"my_dog.age",[24,7878,7879,7882],{},[49,7880,7881],{},"my_dog.bark()"," runs a method",[14,7884,7885,7886,186],{},"If you want more method examples, see ",[372,7887,7889],{"href":7888},"\u002Flearn\u002Fbasic-methods-in-python-classes-explained\u002F","basic methods in Python classes explained",[37,7891,7893],{"id":7892},"step-by-step-code-breakdown","Step-by-step code breakdown",[14,7895,7896],{},"Let’s go through the main lines one by one.",[639,7898,7900],{"id":7899},"class-dog",[49,7901,7902],{},"class Dog:",[42,7904,7906],{"className":44,"code":7905,"language":46,"meta":47,"style":47},"class Dog:\n",[49,7907,7908],{"__ignoreMap":47},[52,7909,7910,7912,7914],{"class":54,"line":55},[52,7911,6994],{"class":1475},[52,7913,6997],{"class":399},[52,7915,1330],{"class":102},[14,7917,7918,7919,186],{},"This creates a new class named ",[49,7920,7369],{},[14,7922,7923],{},"Think of it as a blueprint for making dog objects.",[639,7925,7927],{"id":7926},"def-__init__self-name-age",[49,7928,7929],{},"def __init__(self, name, age):",[42,7931,7933],{"className":44,"code":7932,"language":46,"meta":47,"style":47},"def __init__(self, name, age):\n",[49,7934,7935],{"__ignoreMap":47},[52,7936,7937,7939,7941,7943,7945,7947,7949,7951,7953],{"class":54,"line":55},[52,7938,7410],{"class":1475},[52,7940,7007],{"class":241},[52,7942,193],{"class":102},[52,7944,7013],{"class":7012},[52,7946,199],{"class":102},[52,7948,7019],{"class":7018},[52,7950,199],{"class":102},[52,7952,7024],{"class":7018},[52,7954,3114],{"class":102},[14,7956,7957],{},"This defines the setup method for new objects.",[14,7959,7960,7961,7963],{},"When you create a ",[49,7962,7369],{},", Python runs this method automatically.",[639,7965,7967],{"id":7966},"selfname-name",[49,7968,7478],{},[42,7970,7972],{"className":44,"code":7971,"language":46,"meta":47,"style":47},"self.name = name\n",[49,7973,7974],{"__ignoreMap":47},[52,7975,7976,7978,7980,7982,7984],{"class":54,"line":55},[52,7977,7013],{"class":2533},[52,7979,186],{"class":102},[52,7981,2147],{"class":1320},[52,7983,7038],{"class":79},[52,7985,7041],{"class":62},[14,7987,7988,7989,7991],{},"This creates an attribute named ",[49,7990,2147],{}," on the object.",[14,7993,7994],{},"The value passed in becomes part of that object.",[639,7996,7998],{"id":7997},"def-barkself",[49,7999,8000],{},"def bark(self):",[42,8002,8004],{"className":44,"code":8003,"language":46,"meta":47,"style":47},"def bark(self):\n    print(f\"{self.name} says woof!\")\n",[49,8005,8006,8018],{"__ignoreMap":47},[52,8007,8008,8010,8012,8014,8016],{"class":54,"line":55},[52,8009,7410],{"class":1475},[52,8011,7066],{"class":7065},[52,8013,193],{"class":102},[52,8015,7013],{"class":7012},[52,8017,3114],{"class":102},[52,8019,8020,8022,8024,8026,8028,8030,8032,8034,8036,8038,8040],{"class":54,"line":66},[52,8021,1599],{"class":241},[52,8023,193],{"class":102},[52,8025,1476],{"class":1475},[52,8027,115],{"class":87},[52,8029,1482],{"class":121},[52,8031,7013],{"class":2533},[52,8033,186],{"class":102},[52,8035,2147],{"class":1320},[52,8037,1488],{"class":121},[52,8039,7095],{"class":87},[52,8041,211],{"class":102},[14,8043,8044,8045,186],{},"This defines a method named ",[49,8046,7163],{},[14,8048,8049,8050,8052],{},"It uses ",[49,8051,7513],{}," so the message matches the current object.",[639,8054,8056],{"id":8055},"my_dog-dogmax-3",[49,8057,8058],{},"my_dog = Dog(\"Max\", 3)",[42,8060,8061],{"className":44,"code":7545,"language":46,"meta":47,"style":47},[49,8062,8063],{"__ignoreMap":47},[52,8064,8065,8067,8069,8071,8073,8075,8077,8079,8081,8083],{"class":54,"line":55},[52,8066,7106],{"class":62},[52,8068,80],{"class":79},[52,8070,6997],{"class":189},[52,8072,193],{"class":102},[52,8074,115],{"class":83},[52,8076,7117],{"class":87},[52,8078,115],{"class":83},[52,8080,199],{"class":102},[52,8082,5261],{"class":121},[52,8084,211],{"class":102},[14,8086,8087,8088,8090,8091,186],{},"This creates one ",[49,8089,7369],{}," object and stores it in the variable ",[49,8092,7134],{},[14,8094,8095],{},"At that moment:",[309,8097,8098,8104],{},[24,8099,8100,6230,8102],{},[49,8101,7513],{},[49,8103,7501],{},[24,8105,8106,6230,8108],{},[49,8107,7521],{},[49,8109,7508],{},[37,8111,6402],{"id":6401},[14,8113,8114],{},"Here are some common problems beginners run into with classes.",[639,8116,8118,8119,8121],{"id":8117},"forgetting-self-in-method-definitions","Forgetting ",[49,8120,7013],{}," in method definitions",[14,8123,8124],{},"Wrong:",[42,8126,8128],{"className":44,"code":8127,"language":46,"meta":47,"style":47},"class Dog:\n    def bark():\n        print(\"Woof!\")\n",[49,8129,8130,8138,8147],{"__ignoreMap":47},[52,8131,8132,8134,8136],{"class":54,"line":55},[52,8133,6994],{"class":1475},[52,8135,6997],{"class":399},[52,8137,1330],{"class":102},[52,8139,8140,8142,8144],{"class":54,"line":66},[52,8141,7004],{"class":1475},[52,8143,7066],{"class":7065},[52,8145,8146],{"class":102},"():\n",[52,8148,8149,8151,8153,8155,8158,8160],{"class":54,"line":73},[52,8150,1470],{"class":241},[52,8152,193],{"class":102},[52,8154,115],{"class":83},[52,8156,8157],{"class":87},"Woof!",[52,8159,115],{"class":83},[52,8161,211],{"class":102},[14,8163,8164],{},"Right:",[42,8166,8168],{"className":44,"code":8167,"language":46,"meta":47,"style":47},"class Dog:\n    def bark(self):\n        print(\"Woof!\")\n",[49,8169,8170,8178,8190],{"__ignoreMap":47},[52,8171,8172,8174,8176],{"class":54,"line":55},[52,8173,6994],{"class":1475},[52,8175,6997],{"class":399},[52,8177,1330],{"class":102},[52,8179,8180,8182,8184,8186,8188],{"class":54,"line":66},[52,8181,7004],{"class":1475},[52,8183,7066],{"class":7065},[52,8185,193],{"class":102},[52,8187,7013],{"class":7012},[52,8189,3114],{"class":102},[52,8191,8192,8194,8196,8198,8200,8202],{"class":54,"line":73},[52,8193,1470],{"class":241},[52,8195,193],{"class":102},[52,8197,115],{"class":83},[52,8199,8157],{"class":87},[52,8201,115],{"class":83},[52,8203,211],{"class":102},[14,8205,8206,8207,8209],{},"Inside a class, instance methods need ",[49,8208,7013],{}," as the first parameter.",[639,8211,8213],{"id":8212},"forgetting-parentheses-when-creating-an-object","Forgetting parentheses when creating an object",[14,8215,8124],{},[42,8217,8219],{"className":44,"code":8218,"language":46,"meta":47,"style":47},"my_dog = Dog\n",[49,8220,8221],{"__ignoreMap":47},[52,8222,8223,8225,8227],{"class":54,"line":55},[52,8224,7106],{"class":62},[52,8226,80],{"class":79},[52,8228,8229],{"class":62}," Dog\n",[14,8231,8164],{},[42,8233,8234],{"className":44,"code":7545,"language":46,"meta":47,"style":47},[49,8235,8236],{"__ignoreMap":47},[52,8237,8238,8240,8242,8244,8246,8248,8250,8252,8254,8256],{"class":54,"line":55},[52,8239,7106],{"class":62},[52,8241,80],{"class":79},[52,8243,6997],{"class":189},[52,8245,193],{"class":102},[52,8247,115],{"class":83},[52,8249,7117],{"class":87},[52,8251,115],{"class":83},[52,8253,199],{"class":102},[52,8255,5261],{"class":121},[52,8257,211],{"class":102},[14,8259,8260],{},"Without parentheses, you are referring to the class itself, not creating an object.",[639,8262,8264,8265,8268],{"id":8263},"using-a-method-name-without-when-trying-to-call-it","Using a method name without ",[49,8266,8267],{},"()"," when trying to call it",[14,8270,8124],{},[42,8272,8274],{"className":44,"code":8273,"language":46,"meta":47,"style":47},"my_dog.bark\n",[49,8275,8276],{"__ignoreMap":47},[52,8277,8278,8280,8282],{"class":54,"line":55},[52,8279,7134],{"class":62},[52,8281,186],{"class":102},[52,8283,8284],{"class":1320},"bark\n",[14,8286,8164],{},[42,8288,8290],{"className":44,"code":8289,"language":46,"meta":47,"style":47},"my_dog.bark()\n",[49,8291,8292],{"__ignoreMap":47},[52,8293,8294,8296,8298,8300],{"class":54,"line":55},[52,8295,7134],{"class":62},[52,8297,186],{"class":102},[52,8299,7163],{"class":189},[52,8301,230],{"class":102},[14,8303,5788,8304,8306],{},[49,8305,8267],{},", Python gives you the method itself instead of running it.",[639,8308,8310],{"id":8309},"misspelling-attribute-names","Misspelling attribute names",[14,8312,8124],{},[42,8314,8316],{"className":44,"code":8315,"language":46,"meta":47,"style":47},"print(my_dog.nam)\n",[49,8317,8318],{"__ignoreMap":47},[52,8319,8320,8322,8324,8326,8328,8331],{"class":54,"line":55},[52,8321,242],{"class":241},[52,8323,193],{"class":102},[52,8325,7134],{"class":189},[52,8327,186],{"class":102},[52,8329,8330],{"class":1320},"nam",[52,8332,211],{"class":102},[14,8334,8164],{},[42,8336,8338],{"className":44,"code":8337,"language":46,"meta":47,"style":47},"print(my_dog.name)\n",[49,8339,8340],{"__ignoreMap":47},[52,8341,8342,8344,8346,8348,8350,8352],{"class":54,"line":55},[52,8343,242],{"class":241},[52,8345,193],{"class":102},[52,8347,7134],{"class":189},[52,8349,186],{"class":102},[52,8351,2147],{"class":1320},[52,8353,211],{"class":102},[14,8355,8356,8357,186],{},"A misspelled attribute name can cause an ",[372,8358,8360],{"href":8359},"\u002Ferrors\u002Fattributeerror-object-has-no-attribute-fix\u002F","AttributeError: object has no attribute",[639,8362,8364],{"id":8363},"bad-indentation-inside-the-class","Bad indentation inside the class",[14,8366,8124],{},[42,8368,8370],{"className":44,"code":8369,"language":46,"meta":47,"style":47},"class Dog:\ndef __init__(self, name, age):\n    self.name = name\n    self.age = age\n",[49,8371,8372,8380,8400,8412],{"__ignoreMap":47},[52,8373,8374,8376,8378],{"class":54,"line":55},[52,8375,6994],{"class":1475},[52,8377,6997],{"class":399},[52,8379,1330],{"class":102},[52,8381,8382,8384,8386,8388,8390,8392,8394,8396,8398],{"class":54,"line":66},[52,8383,7410],{"class":1475},[52,8385,7007],{"class":241},[52,8387,193],{"class":102},[52,8389,7013],{"class":7012},[52,8391,199],{"class":102},[52,8393,7019],{"class":7018},[52,8395,199],{"class":102},[52,8397,7024],{"class":7018},[52,8399,3114],{"class":102},[52,8401,8402,8404,8406,8408,8410],{"class":54,"line":73},[52,8403,7431],{"class":2533},[52,8405,186],{"class":102},[52,8407,2147],{"class":1320},[52,8409,7038],{"class":79},[52,8411,7041],{"class":62},[52,8413,8414,8416,8418,8420,8422],{"class":54,"line":94},[52,8415,7431],{"class":2533},[52,8417,186],{"class":102},[52,8419,2167],{"class":1320},[52,8421,7038],{"class":79},[52,8423,7054],{"class":62},[14,8425,8164],{},[42,8427,8429],{"className":44,"code":8428,"language":46,"meta":47,"style":47},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n",[49,8430,8431,8439,8459,8471],{"__ignoreMap":47},[52,8432,8433,8435,8437],{"class":54,"line":55},[52,8434,6994],{"class":1475},[52,8436,6997],{"class":399},[52,8438,1330],{"class":102},[52,8440,8441,8443,8445,8447,8449,8451,8453,8455,8457],{"class":54,"line":66},[52,8442,7004],{"class":1475},[52,8444,7007],{"class":241},[52,8446,193],{"class":102},[52,8448,7013],{"class":7012},[52,8450,199],{"class":102},[52,8452,7019],{"class":7018},[52,8454,199],{"class":102},[52,8456,7024],{"class":7018},[52,8458,3114],{"class":102},[52,8460,8461,8463,8465,8467,8469],{"class":54,"line":73},[52,8462,7031],{"class":2533},[52,8464,186],{"class":102},[52,8466,2147],{"class":1320},[52,8468,7038],{"class":79},[52,8470,7041],{"class":62},[52,8472,8473,8475,8477,8479,8481],{"class":54,"line":94},[52,8474,7031],{"class":2533},[52,8476,186],{"class":102},[52,8478,2167],{"class":1320},[52,8480,7038],{"class":79},[52,8482,7054],{"class":62},[14,8484,8485,8486,186],{},"Python uses indentation to understand code blocks. Bad indentation can cause an ",[372,8487,8489],{"href":8488},"\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix\u002F","IndentationError: expected an indented block",[37,8491,8493],{"id":8492},"try-it-yourself","Try it yourself",[14,8495,8496],{},"Once the basic example works, try these small changes:",[309,8498,8499,8502,8507,8510],{},[24,8500,8501],{},"Change the object values",[24,8503,8504,8505,5379],{},"Create a second ",[49,8506,7369],{},[24,8508,8509],{},"Print both objects' data",[24,8511,8512,8513],{},"Add another method such as ",[49,8514,8515],{},"birthday()",[14,8517,4259],{},[42,8519,8521],{"className":44,"code":8520,"language":46,"meta":47,"style":47},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def bark(self):\n        print(f\"{self.name} says woof!\")\n\n    def birthday(self):\n        self.age += 1\n        print(f\"{self.name} is now {self.age}\")\n\ndog1 = Dog(\"Max\", 3)\ndog2 = Dog(\"Bella\", 5)\n\nprint(dog1.name, dog1.age)\nprint(dog2.name, dog2.age)\n\ndog1.birthday()\n",[49,8522,8523,8531,8551,8563,8575,8579,8591,8615,8619,8632,8646,8683,8687,8709,8731,8735,8758,8781,8785],{"__ignoreMap":47},[52,8524,8525,8527,8529],{"class":54,"line":55},[52,8526,6994],{"class":1475},[52,8528,6997],{"class":399},[52,8530,1330],{"class":102},[52,8532,8533,8535,8537,8539,8541,8543,8545,8547,8549],{"class":54,"line":66},[52,8534,7004],{"class":1475},[52,8536,7007],{"class":241},[52,8538,193],{"class":102},[52,8540,7013],{"class":7012},[52,8542,199],{"class":102},[52,8544,7019],{"class":7018},[52,8546,199],{"class":102},[52,8548,7024],{"class":7018},[52,8550,3114],{"class":102},[52,8552,8553,8555,8557,8559,8561],{"class":54,"line":73},[52,8554,7031],{"class":2533},[52,8556,186],{"class":102},[52,8558,2147],{"class":1320},[52,8560,7038],{"class":79},[52,8562,7041],{"class":62},[52,8564,8565,8567,8569,8571,8573],{"class":54,"line":94},[52,8566,7031],{"class":2533},[52,8568,186],{"class":102},[52,8570,2167],{"class":1320},[52,8572,7038],{"class":79},[52,8574,7054],{"class":62},[52,8576,8577],{"class":54,"line":106},[52,8578,70],{"emptyLinePlaceholder":69},[52,8580,8581,8583,8585,8587,8589],{"class":54,"line":128},[52,8582,7004],{"class":1475},[52,8584,7066],{"class":7065},[52,8586,193],{"class":102},[52,8588,7013],{"class":7012},[52,8590,3114],{"class":102},[52,8592,8593,8595,8597,8599,8601,8603,8605,8607,8609,8611,8613],{"class":54,"line":148},[52,8594,1470],{"class":241},[52,8596,193],{"class":102},[52,8598,1476],{"class":1475},[52,8600,115],{"class":87},[52,8602,1482],{"class":121},[52,8604,7013],{"class":2533},[52,8606,186],{"class":102},[52,8608,2147],{"class":1320},[52,8610,1488],{"class":121},[52,8612,7095],{"class":87},[52,8614,211],{"class":102},[52,8616,8617],{"class":54,"line":164},[52,8618,70],{"emptyLinePlaceholder":69},[52,8620,8621,8623,8626,8628,8630],{"class":54,"line":170},[52,8622,7004],{"class":1475},[52,8624,8625],{"class":7065}," birthday",[52,8627,193],{"class":102},[52,8629,7013],{"class":7012},[52,8631,3114],{"class":102},[52,8633,8634,8636,8638,8640,8643],{"class":54,"line":175},[52,8635,7031],{"class":2533},[52,8637,186],{"class":102},[52,8639,2167],{"class":1320},[52,8641,8642],{"class":79}," +=",[52,8644,8645],{"class":121}," 1\n",[52,8647,8648,8650,8652,8654,8656,8658,8660,8662,8664,8666,8669,8671,8673,8675,8677,8679,8681],{"class":54,"line":214},[52,8649,1470],{"class":241},[52,8651,193],{"class":102},[52,8653,1476],{"class":1475},[52,8655,115],{"class":87},[52,8657,1482],{"class":121},[52,8659,7013],{"class":2533},[52,8661,186],{"class":102},[52,8663,2147],{"class":1320},[52,8665,1488],{"class":121},[52,8667,8668],{"class":87}," is now ",[52,8670,1482],{"class":121},[52,8672,7013],{"class":2533},[52,8674,186],{"class":102},[52,8676,2167],{"class":1320},[52,8678,1488],{"class":121},[52,8680,115],{"class":87},[52,8682,211],{"class":102},[52,8684,8685],{"class":54,"line":233},[52,8686,70],{"emptyLinePlaceholder":69},[52,8688,8689,8691,8693,8695,8697,8699,8701,8703,8705,8707],{"class":54,"line":238},[52,8690,7605],{"class":62},[52,8692,80],{"class":79},[52,8694,6997],{"class":189},[52,8696,193],{"class":102},[52,8698,115],{"class":83},[52,8700,7117],{"class":87},[52,8702,115],{"class":83},[52,8704,199],{"class":102},[52,8706,5261],{"class":121},[52,8708,211],{"class":102},[52,8710,8711,8713,8715,8717,8719,8721,8723,8725,8727,8729],{"class":54,"line":272},[52,8712,7628],{"class":62},[52,8714,80],{"class":79},[52,8716,6997],{"class":189},[52,8718,193],{"class":102},[52,8720,115],{"class":83},[52,8722,7639],{"class":87},[52,8724,115],{"class":83},[52,8726,199],{"class":102},[52,8728,7646],{"class":121},[52,8730,211],{"class":102},[52,8732,8733],{"class":54,"line":1348},[52,8734,70],{"emptyLinePlaceholder":69},[52,8736,8737,8739,8741,8743,8745,8747,8749,8752,8754,8756],{"class":54,"line":1376},[52,8738,242],{"class":241},[52,8740,193],{"class":102},[52,8742,7661],{"class":189},[52,8744,186],{"class":102},[52,8746,2147],{"class":1320},[52,8748,199],{"class":102},[52,8750,8751],{"class":189}," dog1",[52,8753,186],{"class":102},[52,8755,2167],{"class":1320},[52,8757,211],{"class":102},[52,8759,8760,8762,8764,8766,8768,8770,8772,8775,8777,8779],{"class":54,"line":1381},[52,8761,242],{"class":241},[52,8763,193],{"class":102},[52,8765,7676],{"class":189},[52,8767,186],{"class":102},[52,8769,2147],{"class":1320},[52,8771,199],{"class":102},[52,8773,8774],{"class":189}," dog2",[52,8776,186],{"class":102},[52,8778,2167],{"class":1320},[52,8780,211],{"class":102},[52,8782,8783],{"class":54,"line":1406},[52,8784,70],{"emptyLinePlaceholder":69},[52,8786,8787,8789,8791,8794],{"class":54,"line":1430},[52,8788,7661],{"class":62},[52,8790,186],{"class":102},[52,8792,8793],{"class":189},"birthday",[52,8795,230],{"class":102},[14,8797,8798],{},[5503,8799,5267],{},[42,8801,8803],{"className":44,"code":8802,"language":46,"meta":47,"style":47},"Max 3\nBella 5\nMax is now 4\n",[49,8804,8805,8812,8820],{"__ignoreMap":47},[52,8806,8807,8810],{"class":54,"line":55},[52,8808,8809],{"class":62},"Max ",[52,8811,7184],{"class":121},[52,8813,8814,8817],{"class":54,"line":66},[52,8815,8816],{"class":62},"Bella ",[52,8818,8819],{"class":121},"5\n",[52,8821,8822,8824,8826,8829],{"class":54,"line":73},[52,8823,8809],{"class":62},[52,8825,1444],{"class":79},[52,8827,8828],{"class":62}," now ",[52,8830,8831],{"class":121},"4\n",[14,8833,4776],{},[37,8835,8837],{"id":8836},"common-causes-of-confusion","Common causes of confusion",[14,8839,8840],{},"Beginners often get stuck on these points:",[309,8842,8843,8846,8852,8855,8858],{},[24,8844,8845],{},"Confusing a class with an object",[24,8847,8848,8849,8851],{},"Not understanding what ",[49,8850,7013],{}," refers to",[24,8853,8854],{},"Mixing up attributes and methods",[24,8856,8857],{},"Indentation mistakes inside the class body",[24,8859,8860],{},"Calling methods with missing required arguments",[14,8862,8863],{},"If that happens, test one small part at a time and print values often.",[14,8865,8866],{},"Useful debugging checks:",[42,8868,8870],{"className":44,"code":8869,"language":46,"meta":47,"style":47},"print(type(my_dog))\nprint(my_dog.name)\nprint(my_dog.age)\ndir(my_dog)\nhelp(Dog)\n",[49,8871,8872,8887,8901,8915,8926],{"__ignoreMap":47},[52,8873,8874,8876,8878,8880,8882,8884],{"class":54,"line":55},[52,8875,242],{"class":241},[52,8877,193],{"class":102},[52,8879,4198],{"class":4733},[52,8881,193],{"class":102},[52,8883,7134],{"class":189},[52,8885,8886],{"class":102},"))\n",[52,8888,8889,8891,8893,8895,8897,8899],{"class":54,"line":66},[52,8890,242],{"class":241},[52,8892,193],{"class":102},[52,8894,7134],{"class":189},[52,8896,186],{"class":102},[52,8898,2147],{"class":1320},[52,8900,211],{"class":102},[52,8902,8903,8905,8907,8909,8911,8913],{"class":54,"line":73},[52,8904,242],{"class":241},[52,8906,193],{"class":102},[52,8908,7134],{"class":189},[52,8910,186],{"class":102},[52,8912,2167],{"class":1320},[52,8914,211],{"class":102},[52,8916,8917,8920,8922,8924],{"class":54,"line":94},[52,8918,8919],{"class":241},"dir",[52,8921,193],{"class":102},[52,8923,7134],{"class":189},[52,8925,211],{"class":102},[52,8927,8928,8931,8933,8935],{"class":54,"line":106},[52,8929,8930],{"class":241},"help",[52,8932,193],{"class":102},[52,8934,7369],{"class":189},[52,8936,211],{"class":102},[14,8938,8939],{},"These can help you see:",[309,8941,8942,8948,8951],{},[24,8943,8944,8945,8947],{},"What type ",[49,8946,7134],{}," is",[24,8949,8950],{},"Whether attributes exist",[24,8952,8953],{},"What names and methods are available on the object",[37,8955,1942],{"id":1941},[639,8957,8959],{"id":8958},"what-is-the-difference-between-a-class-and-an-object","What is the difference between a class and an object?",[14,8961,8962],{},"A class is a blueprint. An object is one created item based on that blueprint.",[639,8964,8966,8967,8969],{"id":8965},"why-do-i-need-self-in-a-class","Why do I need ",[49,8968,7013],{}," in a class?",[14,8971,8972,8974],{},[49,8973,7013],{}," lets Python refer to the current object so each object can store its own data.",[639,8976,8978],{"id":8977},"is-this-page-teaching-all-of-oop","Is this page teaching all of OOP?",[14,8980,8981],{},"No. This page focuses on one simple class example. Broader OOP concepts belong on separate learn pages.",[639,8983,8985],{"id":8984},"can-a-class-have-more-than-one-method","Can a class have more than one method?",[14,8987,8988],{},"Yes. A class can have many methods, but this example stays small to make the core idea clear.",[37,8990,2005],{"id":2004},[309,8992,8993,8997,9003,9009,9015],{},[24,8994,8995],{},[372,8996,7255],{"href":7254},[24,8998,8999],{},[372,9000,9002],{"href":9001},"\u002Flearn\u002Fobject-oriented-programming-in-python-explained\u002F","Object-oriented programming in Python explained",[24,9004,9005],{},[372,9006,358,9007,7535],{"href":7532},[49,9008,6978],{},[24,9010,9011],{},[372,9012,9014],{"href":9013},"\u002Fhow-to\u002Fhow-to-create-a-class-in-python\u002F","How to create a class in Python",[24,9016,9017],{},[372,9018,9020],{"href":9019},"\u002Fhow-to\u002Fhow-to-add-methods-to-a-class-in-python\u002F","How to add methods to a class in Python",[14,9022,9023,9024,9026],{},"After you understand this example, the next good step is to create your own class and then learn ",[49,9025,6978],{}," and methods in more detail.",[2034,9028,9029],{},"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 .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 pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":9031},[9032,9033,9034,9035,9037,9038,9043,9050,9059,9060,9061,9068],{"id":39,"depth":66,"text":40},{"id":7215,"depth":66,"text":7216},{"id":7258,"depth":66,"text":7259},{"id":7389,"depth":66,"text":9036},"How the __init__ method works",{"id":7538,"depth":66,"text":7539},{"id":7713,"depth":66,"text":7714,"children":9039},[9040,9041,9042],{"id":7723,"depth":73,"text":7724},{"id":7806,"depth":73,"text":7807},{"id":7862,"depth":73,"text":7863},{"id":7892,"depth":66,"text":7893,"children":9044},[9045,9046,9047,9048,9049],{"id":7899,"depth":73,"text":7902},{"id":7926,"depth":73,"text":7929},{"id":7966,"depth":73,"text":7478},{"id":7997,"depth":73,"text":8000},{"id":8055,"depth":73,"text":8058},{"id":6401,"depth":66,"text":6402,"children":9051},[9052,9054,9055,9057,9058],{"id":8117,"depth":73,"text":9053},"Forgetting self in method definitions",{"id":8212,"depth":73,"text":8213},{"id":8263,"depth":73,"text":9056},"Using a method name without () when trying to call it",{"id":8309,"depth":73,"text":8310},{"id":8363,"depth":73,"text":8364},{"id":8492,"depth":66,"text":8493},{"id":8836,"depth":66,"text":8837},{"id":1941,"depth":66,"text":1942,"children":9062},[9063,9064,9066,9067],{"id":8958,"depth":73,"text":8959},{"id":8965,"depth":73,"text":9065},"Why do I need self in a class?",{"id":8977,"depth":73,"text":8978},{"id":8984,"depth":73,"text":8985},{"id":2004,"depth":66,"text":2005},"Master python basic class example oop in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-basic-class-example-oop",{"title":6948,"description":9069},"examples\u002Fpython-basic-class-example-oop","HoyhiIDgGJNsaNyOVSC1mNHpA0Gf5OKXUfIYRwhZi9M",{"id":9076,"title":9077,"body":9078,"description":10694,"extension":2075,"meta":10695,"navigation":69,"path":10696,"seo":10697,"stem":10698,"__hash__":10699},"content\u002Fexamples\u002Fpython-command-line-tool-example.md","Python Command Line Tool Example",{"type":7,"value":9079,"toc":10659},[9080,9083,9086,9089,9107,9117,9119,9122,9128,9248,9250,9264,9267,9273,9276,9292,9296,9299,9302,9316,9324,9328,9331,9345,9348,9352,9357,9362,9382,9385,9397,9403,9428,9431,9446,9450,9484,9494,9498,9501,9607,9611,9616,9623,9628,9639,9644,9649,9654,9659,9664,9669,9674,9679,9688,9692,9695,9698,9704,9907,9911,9914,9926,9930,9933,9949,9951,9957,9960,9973,9975,9981,9984,10000,10002,10008,10011,10022,10033,10035,10038,10054,10058,10061,10073,10075,10080,10083,10097,10099,10104,10108,10111,10171,10174,10204,10206,10209,10213,10215,10229,10237,10240,10291,10298,10304,10307,10350,10353,10356,10453,10457,10460,10463,10486,10489,10493,10495,10508,10512,10515,10549,10552,10563,10565,10569,10572,10578,10581,10585,10591,10601,10613,10617,10628,10630,10656],[10,9081,9077],{"id":9082},"python-command-line-tool-example",[14,9084,9085],{},"A command line tool is a Python script that you run from the terminal and give input to when the script starts.",[14,9087,9088],{},"This beginner-friendly example shows:",[309,9090,9091,9094,9101,9104],{},[24,9092,9093],{},"how to build a small Python CLI tool",[24,9095,9096,9097],{},"how to read command line arguments with ",[372,9098,9099],{"href":5363},[49,9100,6759],{},[24,9102,9103],{},"how to handle missing or invalid input",[24,9105,9106],{},"how to run the script from the terminal",[14,9108,9109,9110,9113,9114,9116],{},"If you are new to this, the main idea is simple: the user runs a file like ",[49,9111,9112],{},"python tool.py Alice",", and your script uses ",[49,9115,2156],{}," as input.",[37,9118,40],{"id":39},[14,9120,9121],{},"Use this as the smallest possible command line tool example.",[14,9123,9124,9125,118],{},"Save it as ",[49,9126,9127],{},"tool.py",[42,9129,9131],{"className":44,"code":9130,"language":46,"meta":47,"style":47},"import sys\n\nif len(sys.argv) \u003C 2:\n    print(\"Usage: python tool.py NAME\")\n    sys.exit(1)\n\nname = sys.argv[1]\nprint(f\"Hello, {name}!\")\n",[49,9132,9133,9140,9144,9171,9186,9203,9207,9227],{"__ignoreMap":47},[52,9134,9135,9137],{"class":54,"line":55},[52,9136,59],{"class":58},[52,9138,9139],{"class":62}," sys\n",[52,9141,9142],{"class":54,"line":66},[52,9143,70],{"emptyLinePlaceholder":69},[52,9145,9146,9148,9151,9153,9156,9158,9161,9163,9166,9169],{"class":54,"line":73},[52,9147,1313],{"class":58},[52,9149,9150],{"class":241}," len",[52,9152,193],{"class":102},[52,9154,9155],{"class":189},"sys",[52,9157,186],{"class":102},[52,9159,9160],{"class":1320},"argv",[52,9162,1521],{"class":102},[52,9164,9165],{"class":79}," \u003C",[52,9167,9168],{"class":121}," 2",[52,9170,1330],{"class":102},[52,9172,9173,9175,9177,9179,9182,9184],{"class":54,"line":94},[52,9174,1599],{"class":241},[52,9176,193],{"class":102},[52,9178,115],{"class":83},[52,9180,9181],{"class":87},"Usage: python tool.py NAME",[52,9183,115],{"class":83},[52,9185,211],{"class":102},[52,9187,9188,9191,9193,9196,9198,9201],{"class":54,"line":106},[52,9189,9190],{"class":62},"    sys",[52,9192,186],{"class":102},[52,9194,9195],{"class":189},"exit",[52,9197,193],{"class":102},[52,9199,9200],{"class":121},"1",[52,9202,211],{"class":102},[52,9204,9205],{"class":54,"line":128},[52,9206,70],{"emptyLinePlaceholder":69},[52,9208,9209,9212,9214,9217,9219,9221,9223,9225],{"class":54,"line":148},[52,9210,9211],{"class":62},"name ",[52,9213,80],{"class":79},[52,9215,9216],{"class":62}," sys",[52,9218,186],{"class":102},[52,9220,9160],{"class":1320},[52,9222,250],{"class":102},[52,9224,9200],{"class":121},[52,9226,1147],{"class":102},[52,9228,9229,9231,9233,9235,9237,9239,9241,9243,9246],{"class":54,"line":164},[52,9230,242],{"class":241},[52,9232,193],{"class":102},[52,9234,1476],{"class":1475},[52,9236,6129],{"class":87},[52,9238,1482],{"class":121},[52,9240,2147],{"class":189},[52,9242,1488],{"class":121},[52,9244,9245],{"class":87},"!\"",[52,9247,211],{"class":102},[14,9249,5246],{},[42,9251,9253],{"className":390,"code":9252,"language":392,"meta":47,"style":47},"python tool.py Alice\n",[49,9254,9255],{"__ignoreMap":47},[52,9256,9257,9259,9262],{"class":54,"line":55},[52,9258,46],{"class":399},[52,9260,9261],{"class":87}," tool.py",[52,9263,5874],{"class":87},[14,9265,9266],{},"Expected output:",[42,9268,9271],{"className":9269,"code":9270,"language":955,"meta":47},[953],"Hello, Alice!\n",[49,9272,9270],{"__ignoreMap":47},[14,9274,9275],{},"This example teaches the basic pattern:",[309,9277,9278,9283,9286,9289],{},[24,9279,9280,9281],{},"import ",[49,9282,9155],{},[24,9284,9285],{},"check whether an argument exists",[24,9287,9288],{},"read the argument",[24,9290,9291],{},"print a result",[37,9293,9295],{"id":9294},"what-this-example-builds","What this example builds",[14,9297,9298],{},"This page builds a very small script that runs from the terminal.",[14,9300,9301],{},"It shows how to:",[309,9303,9304,9307,9310,9313],{},[24,9305,9306],{},"run a Python file from the command line",[24,9308,9309],{},"pass input as command line arguments",[24,9311,9312],{},"check whether the user gave enough input",[24,9314,9315],{},"print a useful result",[14,9317,9318,9319,186],{},"The goal is to help you understand the basic pattern of a command line tool before moving on to more advanced tools like ",[372,9320,9322],{"href":9321},"\u002Fexamples\u002Fpython-argument-parser-example-argparse\u002F",[49,9323,5043],{},[37,9325,9327],{"id":9326},"when-to-use-a-command-line-tool","When to use a command line tool",[14,9329,9330],{},"A command line tool is useful when:",[309,9332,9333,9336,9339,9342],{},[24,9334,9335],{},"you want to run a task quickly without editing the code",[24,9337,9338],{},"the script should accept different values each time",[24,9340,9341],{},"you want to automate a small task",[24,9343,9344],{},"you are working with files, text, or simple calculations",[14,9346,9347],{},"This is a good first step before learning full argument parsers.",[37,9349,9351],{"id":9350},"how-command-line-arguments-work","How command line arguments work",[14,9353,9354,9355,186],{},"Python stores command line arguments in ",[49,9356,6759],{},[14,9358,9359,9361],{},[49,9360,6759],{}," is a list:",[309,9363,9364,9370,9376],{},[24,9365,9366,9369],{},[49,9367,9368],{},"sys.argv[0]"," is the script filename",[24,9371,9372,9375],{},[49,9373,9374],{},"sys.argv[1]"," is the first value given by the user",[24,9377,9378,9381],{},[49,9379,9380],{},"sys.argv[2]"," is the second value, and so on",[14,9383,9384],{},"For example, if you run:",[42,9386,9387],{"className":390,"code":9252,"language":392,"meta":47,"style":47},[49,9388,9389],{"__ignoreMap":47},[52,9390,9391,9393,9395],{"class":54,"line":55},[52,9392,46],{"class":399},[52,9394,9261],{"class":87},[52,9396,5874],{"class":87},[14,9398,9399,9400,9402],{},"then ",[49,9401,6759],{}," looks like this:",[42,9404,9406],{"className":44,"code":9405,"language":46,"meta":47,"style":47},"['tool.py', 'Alice']\n",[49,9407,9408],{"__ignoreMap":47},[52,9409,9410,9412,9414,9416,9418,9420,9422,9424,9426],{"class":54,"line":55},[52,9411,250],{"class":102},[52,9413,4162],{"class":83},[52,9415,9127],{"class":87},[52,9417,4162],{"class":83},[52,9419,199],{"class":102},[52,9421,4172],{"class":83},[52,9423,2156],{"class":87},[52,9425,4162],{"class":83},[52,9427,1147],{"class":102},[14,9429,9430],{},"If you run:",[42,9432,9434],{"className":390,"code":9433,"language":392,"meta":47,"style":47},"python tool.py 3 5\n",[49,9435,9436],{"__ignoreMap":47},[52,9437,9438,9440,9442,9444],{"class":54,"line":55},[52,9439,46],{"class":399},[52,9441,9261],{"class":87},[52,9443,5261],{"class":121},[52,9445,5264],{"class":121},[14,9447,9399,9448,9402],{},[49,9449,6759],{},[42,9451,9453],{"className":44,"code":9452,"language":46,"meta":47,"style":47},"['tool.py', '3', '5']\n",[49,9454,9455],{"__ignoreMap":47},[52,9456,9457,9459,9461,9463,9465,9467,9469,9471,9473,9475,9477,9480,9482],{"class":54,"line":55},[52,9458,250],{"class":102},[52,9460,4162],{"class":83},[52,9462,9127],{"class":87},[52,9464,4162],{"class":83},[52,9466,199],{"class":102},[52,9468,4172],{"class":83},[52,9470,7508],{"class":87},[52,9472,4162],{"class":83},[52,9474,199],{"class":102},[52,9476,4172],{"class":83},[52,9478,9479],{"class":87},"5",[52,9481,4162],{"class":83},[52,9483,1147],{"class":102},[14,9485,9486,9487,9493],{},"Notice that command line values are strings. If you need numbers, you must convert them. You can learn more on the ",[372,9488,9490,3711],{"href":9489},"\u002Freference\u002Fpython-int-function-explained\u002F",[49,9491,9492],{},"int()"," page.",[37,9495,9497],{"id":9496},"build-the-first-working-version","Build the first working version",[14,9499,9500],{},"Start with this script:",[42,9502,9503],{"className":44,"code":9130,"language":46,"meta":47,"style":47},[49,9504,9505,9511,9515,9537,9551,9565,9569,9587],{"__ignoreMap":47},[52,9506,9507,9509],{"class":54,"line":55},[52,9508,59],{"class":58},[52,9510,9139],{"class":62},[52,9512,9513],{"class":54,"line":66},[52,9514,70],{"emptyLinePlaceholder":69},[52,9516,9517,9519,9521,9523,9525,9527,9529,9531,9533,9535],{"class":54,"line":73},[52,9518,1313],{"class":58},[52,9520,9150],{"class":241},[52,9522,193],{"class":102},[52,9524,9155],{"class":189},[52,9526,186],{"class":102},[52,9528,9160],{"class":1320},[52,9530,1521],{"class":102},[52,9532,9165],{"class":79},[52,9534,9168],{"class":121},[52,9536,1330],{"class":102},[52,9538,9539,9541,9543,9545,9547,9549],{"class":54,"line":94},[52,9540,1599],{"class":241},[52,9542,193],{"class":102},[52,9544,115],{"class":83},[52,9546,9181],{"class":87},[52,9548,115],{"class":83},[52,9550,211],{"class":102},[52,9552,9553,9555,9557,9559,9561,9563],{"class":54,"line":106},[52,9554,9190],{"class":62},[52,9556,186],{"class":102},[52,9558,9195],{"class":189},[52,9560,193],{"class":102},[52,9562,9200],{"class":121},[52,9564,211],{"class":102},[52,9566,9567],{"class":54,"line":128},[52,9568,70],{"emptyLinePlaceholder":69},[52,9570,9571,9573,9575,9577,9579,9581,9583,9585],{"class":54,"line":148},[52,9572,9211],{"class":62},[52,9574,80],{"class":79},[52,9576,9216],{"class":62},[52,9578,186],{"class":102},[52,9580,9160],{"class":1320},[52,9582,250],{"class":102},[52,9584,9200],{"class":121},[52,9586,1147],{"class":102},[52,9588,9589,9591,9593,9595,9597,9599,9601,9603,9605],{"class":54,"line":164},[52,9590,242],{"class":241},[52,9592,193],{"class":102},[52,9594,1476],{"class":1475},[52,9596,6129],{"class":87},[52,9598,1482],{"class":121},[52,9600,2147],{"class":189},[52,9602,1488],{"class":121},[52,9604,9245],{"class":87},[52,9606,211],{"class":102},[639,9608,9610],{"id":9609},"what-each-part-does","What each part does",[14,9612,9613],{},[49,9614,9615],{},"import sys",[309,9617,9618],{},[24,9619,9620,9621],{},"gives your script access to ",[49,9622,6759],{},[14,9624,9625],{},[49,9626,9627],{},"if len(sys.argv) \u003C 2:",[309,9629,9630,9633],{},[24,9631,9632],{},"checks whether the user gave at least one real argument",[24,9634,9635,9636,9638],{},"remember: ",[49,9637,9368],{}," is only the filename",[14,9640,9641],{},[49,9642,9643],{},"print(\"Usage: python tool.py NAME\")",[309,9645,9646],{},[24,9647,9648],{},"shows the user how to run the script correctly",[14,9650,9651],{},[49,9652,9653],{},"sys.exit(1)",[309,9655,9656],{},[24,9657,9658],{},"stops the program because the input is missing",[14,9660,9661],{},[49,9662,9663],{},"name = sys.argv[1]",[309,9665,9666],{},[24,9667,9668],{},"reads the first user argument",[14,9670,9671],{},[49,9672,9673],{},"print(f\"Hello, {name}!\")",[309,9675,9676],{},[24,9677,9678],{},"prints the result using an f-string",[14,9680,9681,9682,186],{},"If you want to review printing output, see ",[372,9683,9685,2866],{"href":9684},"\u002Freference\u002Fpython-print-function-explained\u002F",[49,9686,9687],{},"print()",[37,9689,9691],{"id":9690},"improve-the-tool-with-a-real-task","Improve the tool with a real task",[14,9693,9694],{},"A greeting example is useful for learning, but a real tool often does a task.",[14,9696,9697],{},"Here is a second example that adds two numbers.",[14,9699,9700,9701,118],{},"Save this as ",[49,9702,9703],{},"add_tool.py",[42,9705,9707],{"className":44,"code":9706,"language":46,"meta":47,"style":47},"import sys\n\nif len(sys.argv) \u003C 3:\n    print(\"Usage: python add_tool.py NUMBER1 NUMBER2\")\n    sys.exit(1)\n\ntry:\n    num1 = int(sys.argv[1])\n    num2 = int(sys.argv[2])\nexcept ValueError:\n    print(\"Error: both arguments must be whole numbers.\")\n    sys.exit(1)\n\ntotal = num1 + num2\nprint(f\"Result: {total}\")\n",[49,9708,9709,9715,9719,9741,9756,9770,9774,9780,9804,9828,9836,9851,9865,9869,9885],{"__ignoreMap":47},[52,9710,9711,9713],{"class":54,"line":55},[52,9712,59],{"class":58},[52,9714,9139],{"class":62},[52,9716,9717],{"class":54,"line":66},[52,9718,70],{"emptyLinePlaceholder":69},[52,9720,9721,9723,9725,9727,9729,9731,9733,9735,9737,9739],{"class":54,"line":73},[52,9722,1313],{"class":58},[52,9724,9150],{"class":241},[52,9726,193],{"class":102},[52,9728,9155],{"class":189},[52,9730,186],{"class":102},[52,9732,9160],{"class":1320},[52,9734,1521],{"class":102},[52,9736,9165],{"class":79},[52,9738,5261],{"class":121},[52,9740,1330],{"class":102},[52,9742,9743,9745,9747,9749,9752,9754],{"class":54,"line":94},[52,9744,1599],{"class":241},[52,9746,193],{"class":102},[52,9748,115],{"class":83},[52,9750,9751],{"class":87},"Usage: python add_tool.py NUMBER1 NUMBER2",[52,9753,115],{"class":83},[52,9755,211],{"class":102},[52,9757,9758,9760,9762,9764,9766,9768],{"class":54,"line":106},[52,9759,9190],{"class":62},[52,9761,186],{"class":102},[52,9763,9195],{"class":189},[52,9765,193],{"class":102},[52,9767,9200],{"class":121},[52,9769,211],{"class":102},[52,9771,9772],{"class":54,"line":128},[52,9773,70],{"emptyLinePlaceholder":69},[52,9775,9776,9778],{"class":54,"line":148},[52,9777,3336],{"class":58},[52,9779,1330],{"class":102},[52,9781,9782,9785,9787,9790,9792,9794,9796,9798,9800,9802],{"class":54,"line":164},[52,9783,9784],{"class":62},"    num1 ",[52,9786,80],{"class":79},[52,9788,9789],{"class":4733}," int",[52,9791,193],{"class":102},[52,9793,9155],{"class":189},[52,9795,186],{"class":102},[52,9797,9160],{"class":1320},[52,9799,250],{"class":102},[52,9801,9200],{"class":121},[52,9803,269],{"class":102},[52,9805,9806,9809,9811,9813,9815,9817,9819,9821,9823,9826],{"class":54,"line":170},[52,9807,9808],{"class":62},"    num2 ",[52,9810,80],{"class":79},[52,9812,9789],{"class":4733},[52,9814,193],{"class":102},[52,9816,9155],{"class":189},[52,9818,186],{"class":102},[52,9820,9160],{"class":1320},[52,9822,250],{"class":102},[52,9824,9825],{"class":121},"2",[52,9827,269],{"class":102},[52,9829,9830,9832,9834],{"class":54,"line":175},[52,9831,3552],{"class":58},[52,9833,4734],{"class":4733},[52,9835,1330],{"class":102},[52,9837,9838,9840,9842,9844,9847,9849],{"class":54,"line":214},[52,9839,1599],{"class":241},[52,9841,193],{"class":102},[52,9843,115],{"class":83},[52,9845,9846],{"class":87},"Error: both arguments must be whole numbers.",[52,9848,115],{"class":83},[52,9850,211],{"class":102},[52,9852,9853,9855,9857,9859,9861,9863],{"class":54,"line":233},[52,9854,9190],{"class":62},[52,9856,186],{"class":102},[52,9858,9195],{"class":189},[52,9860,193],{"class":102},[52,9862,9200],{"class":121},[52,9864,211],{"class":102},[52,9866,9867],{"class":54,"line":238},[52,9868,70],{"emptyLinePlaceholder":69},[52,9870,9871,9874,9876,9879,9882],{"class":54,"line":272},[52,9872,9873],{"class":62},"total ",[52,9875,80],{"class":79},[52,9877,9878],{"class":62}," num1 ",[52,9880,9881],{"class":79},"+",[52,9883,9884],{"class":62}," num2\n",[52,9886,9887,9889,9891,9893,9896,9898,9901,9903,9905],{"class":54,"line":1348},[52,9888,242],{"class":241},[52,9890,193],{"class":102},[52,9892,1476],{"class":1475},[52,9894,9895],{"class":87},"\"Result: ",[52,9897,1482],{"class":121},[52,9899,9900],{"class":189},"total",[52,9902,1488],{"class":121},[52,9904,115],{"class":87},[52,9906,211],{"class":102},[639,9908,9910],{"id":9909},"how-this-version-works","How this version works",[14,9912,9913],{},"This script adds two improvements:",[309,9915,9916,9919],{},[24,9917,9918],{},"it expects two arguments instead of one",[24,9920,9921,9922,759,9924],{},"it handles bad input with ",[49,9923,3336],{},[49,9925,3552],{},[639,9927,9929],{"id":9928},"example-runs","Example runs",[14,9931,9932],{},"Correct input:",[42,9934,9936],{"className":390,"code":9935,"language":392,"meta":47,"style":47},"python add_tool.py 3 5\n",[49,9937,9938],{"__ignoreMap":47},[52,9939,9940,9942,9945,9947],{"class":54,"line":55},[52,9941,46],{"class":399},[52,9943,9944],{"class":87}," add_tool.py",[52,9946,5261],{"class":121},[52,9948,5264],{"class":121},[14,9950,5267],{},[42,9952,9955],{"className":9953,"code":9954,"language":955,"meta":47},[953],"Result: 8\n",[49,9956,9954],{"__ignoreMap":47},[14,9958,9959],{},"Missing input:",[42,9961,9963],{"className":390,"code":9962,"language":392,"meta":47,"style":47},"python add_tool.py 3\n",[49,9964,9965],{"__ignoreMap":47},[52,9966,9967,9969,9971],{"class":54,"line":55},[52,9968,46],{"class":399},[52,9970,9944],{"class":87},[52,9972,1085],{"class":121},[14,9974,5267],{},[42,9976,9979],{"className":9977,"code":9978,"language":955,"meta":47},[953],"Usage: python add_tool.py NUMBER1 NUMBER2\n",[49,9980,9978],{"__ignoreMap":47},[14,9982,9983],{},"Invalid input:",[42,9985,9987],{"className":390,"code":9986,"language":392,"meta":47,"style":47},"python add_tool.py 3 hello\n",[49,9988,9989],{"__ignoreMap":47},[52,9990,9991,9993,9995,9997],{"class":54,"line":55},[52,9992,46],{"class":399},[52,9994,9944],{"class":87},[52,9996,5261],{"class":121},[52,9998,9999],{"class":87}," hello\n",[14,10001,5267],{},[42,10003,10006],{"className":10004,"code":10005,"language":955,"meta":47},[953],"Error: both arguments must be whole numbers.\n",[49,10007,10005],{"__ignoreMap":47},[14,10009,10010],{},"This is an important beginner pattern:",[21,10012,10013,10016,10019],{},[24,10014,10015],{},"check the number of arguments",[24,10017,10018],{},"convert values if needed",[24,10020,10021],{},"handle possible errors clearly",[14,10023,10024,10025,759,10029,186],{},"If you want more help with this part, see ",[372,10026,10028],{"href":10027},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",[372,10030,10032],{"href":10031},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained",[37,10034,6300],{"id":6299},[14,10036,10037],{},"Follow these steps:",[21,10039,10040,10046,10049],{},[24,10041,10042,10043],{},"Save the script in a file ending with ",[49,10044,10045],{},".py",[24,10047,10048],{},"Open a terminal in the same folder",[24,10050,6828,10051],{},[49,10052,10053],{},"python filename.py arguments",[639,10055,10057],{"id":10056},"example-commands","Example commands",[14,10059,10060],{},"For the greeting tool:",[42,10062,10063],{"className":390,"code":9252,"language":392,"meta":47,"style":47},[49,10064,10065],{"__ignoreMap":47},[52,10066,10067,10069,10071],{"class":54,"line":55},[52,10068,46],{"class":399},[52,10070,9261],{"class":87},[52,10072,5874],{"class":87},[14,10074,9266],{},[42,10076,10078],{"className":10077,"code":9270,"language":955,"meta":47},[953],[49,10079,9270],{"__ignoreMap":47},[14,10081,10082],{},"For the add tool:",[42,10084,10085],{"className":390,"code":9935,"language":392,"meta":47,"style":47},[49,10086,10087],{"__ignoreMap":47},[52,10088,10089,10091,10093,10095],{"class":54,"line":55},[52,10090,46],{"class":399},[52,10092,9944],{"class":87},[52,10094,5261],{"class":121},[52,10096,5264],{"class":121},[14,10098,9266],{},[42,10100,10102],{"className":10101,"code":9954,"language":955,"meta":47},[953],[49,10103,9954],{"__ignoreMap":47},[639,10105,10107],{"id":10106},"useful-terminal-commands","Useful terminal commands",[14,10109,10110],{},"These commands can help when testing:",[42,10112,10114],{"className":390,"code":10113,"language":392,"meta":47,"style":47},"python tool.py\npython tool.py Alice\npython tool.py 3 5\npython -V\npwd\ncd path\u002Fto\u002Fscript\u002Ffolder\nls\ndir\n",[49,10115,10116,10123,10131,10141,10148,10153,10161,10166],{"__ignoreMap":47},[52,10117,10118,10120],{"class":54,"line":55},[52,10119,46],{"class":399},[52,10121,10122],{"class":87}," tool.py\n",[52,10124,10125,10127,10129],{"class":54,"line":66},[52,10126,46],{"class":399},[52,10128,9261],{"class":87},[52,10130,5874],{"class":87},[52,10132,10133,10135,10137,10139],{"class":54,"line":73},[52,10134,46],{"class":399},[52,10136,9261],{"class":87},[52,10138,5261],{"class":121},[52,10140,5264],{"class":121},[52,10142,10143,10145],{"class":54,"line":94},[52,10144,46],{"class":399},[52,10146,10147],{"class":420}," -V\n",[52,10149,10150],{"class":54,"line":106},[52,10151,10152],{"class":241},"pwd\n",[52,10154,10155,10158],{"class":54,"line":128},[52,10156,10157],{"class":241},"cd",[52,10159,10160],{"class":87}," path\u002Fto\u002Fscript\u002Ffolder\n",[52,10162,10163],{"class":54,"line":148},[52,10164,10165],{"class":399},"ls\n",[52,10167,10168],{"class":54,"line":164},[52,10169,10170],{"class":399},"dir\n",[14,10172,10173],{},"Notes:",[309,10175,10176,10182,10188,10194,10199],{},[24,10177,10178,10181],{},[49,10179,10180],{},"python -V"," shows your Python version",[24,10183,10184,10187],{},[49,10185,10186],{},"pwd"," shows your current folder on macOS and Linux",[24,10189,10190,10193],{},[49,10191,10192],{},"ls"," lists files on macOS and Linux",[24,10195,10196,10198],{},[49,10197,8919],{}," lists files on Windows",[24,10200,10201,10203],{},[49,10202,10157],{}," changes to the folder that contains your script",[37,10205,6402],{"id":6401},[14,10207,10208],{},"Here are some common problems when building a Python command line tool.",[639,10210,10212],{"id":10211},"forgetting-that-arguments-are-strings","Forgetting that arguments are strings",[14,10214,9430],{},[42,10216,10217],{"className":390,"code":9935,"language":392,"meta":47,"style":47},[49,10218,10219],{"__ignoreMap":47},[52,10220,10221,10223,10225,10227],{"class":54,"line":55},[52,10222,46],{"class":399},[52,10224,9944],{"class":87},[52,10226,5261],{"class":121},[52,10228,5264],{"class":121},[14,10230,10231,10232,759,10234,10236],{},"the values ",[49,10233,5797],{},[49,10235,5800],{}," start as strings, not numbers.",[14,10238,10239],{},"That is why this fails to do numeric work unless you convert them:",[42,10241,10243],{"className":44,"code":10242,"language":46,"meta":47,"style":47},"num1 = int(sys.argv[1])\nnum2 = int(sys.argv[2])\n",[49,10244,10245,10268],{"__ignoreMap":47},[52,10246,10247,10250,10252,10254,10256,10258,10260,10262,10264,10266],{"class":54,"line":55},[52,10248,10249],{"class":62},"num1 ",[52,10251,80],{"class":79},[52,10253,9789],{"class":4733},[52,10255,193],{"class":102},[52,10257,9155],{"class":189},[52,10259,186],{"class":102},[52,10261,9160],{"class":1320},[52,10263,250],{"class":102},[52,10265,9200],{"class":121},[52,10267,269],{"class":102},[52,10269,10270,10273,10275,10277,10279,10281,10283,10285,10287,10289],{"class":54,"line":66},[52,10271,10272],{"class":62},"num2 ",[52,10274,80],{"class":79},[52,10276,9789],{"class":4733},[52,10278,193],{"class":102},[52,10280,9155],{"class":189},[52,10282,186],{"class":102},[52,10284,9160],{"class":1320},[52,10286,250],{"class":102},[52,10288,9825],{"class":121},[52,10290,269],{"class":102},[639,10292,10294,10295,10297],{"id":10293},"using-sysargv1-without-checking-length-first","Using ",[49,10296,9374],{}," without checking length first",[14,10299,10300,10301,186],{},"This is a common cause of ",[49,10302,10303],{},"IndexError",[14,10305,10306],{},"Bad example:",[42,10308,10310],{"className":44,"code":10309,"language":46,"meta":47,"style":47},"import sys\n\nname = sys.argv[1]\nprint(name)\n",[49,10311,10312,10318,10322,10340],{"__ignoreMap":47},[52,10313,10314,10316],{"class":54,"line":55},[52,10315,59],{"class":58},[52,10317,9139],{"class":62},[52,10319,10320],{"class":54,"line":66},[52,10321,70],{"emptyLinePlaceholder":69},[52,10323,10324,10326,10328,10330,10332,10334,10336,10338],{"class":54,"line":73},[52,10325,9211],{"class":62},[52,10327,80],{"class":79},[52,10329,9216],{"class":62},[52,10331,186],{"class":102},[52,10333,9160],{"class":1320},[52,10335,250],{"class":102},[52,10337,9200],{"class":121},[52,10339,1147],{"class":102},[52,10341,10342,10344,10346,10348],{"class":54,"line":94},[52,10343,242],{"class":241},[52,10345,193],{"class":102},[52,10347,2147],{"class":189},[52,10349,211],{"class":102},[14,10351,10352],{},"If no argument is given, the script crashes.",[14,10354,10355],{},"Safer version:",[42,10357,10359],{"className":44,"code":10358,"language":46,"meta":47,"style":47},"import sys\n\nif len(sys.argv) \u003C 2:\n    print(\"Usage: python tool.py NAME\")\n    sys.exit(1)\n\nname = sys.argv[1]\nprint(name)\n",[49,10360,10361,10367,10371,10393,10407,10421,10425,10443],{"__ignoreMap":47},[52,10362,10363,10365],{"class":54,"line":55},[52,10364,59],{"class":58},[52,10366,9139],{"class":62},[52,10368,10369],{"class":54,"line":66},[52,10370,70],{"emptyLinePlaceholder":69},[52,10372,10373,10375,10377,10379,10381,10383,10385,10387,10389,10391],{"class":54,"line":73},[52,10374,1313],{"class":58},[52,10376,9150],{"class":241},[52,10378,193],{"class":102},[52,10380,9155],{"class":189},[52,10382,186],{"class":102},[52,10384,9160],{"class":1320},[52,10386,1521],{"class":102},[52,10388,9165],{"class":79},[52,10390,9168],{"class":121},[52,10392,1330],{"class":102},[52,10394,10395,10397,10399,10401,10403,10405],{"class":54,"line":94},[52,10396,1599],{"class":241},[52,10398,193],{"class":102},[52,10400,115],{"class":83},[52,10402,9181],{"class":87},[52,10404,115],{"class":83},[52,10406,211],{"class":102},[52,10408,10409,10411,10413,10415,10417,10419],{"class":54,"line":106},[52,10410,9190],{"class":62},[52,10412,186],{"class":102},[52,10414,9195],{"class":189},[52,10416,193],{"class":102},[52,10418,9200],{"class":121},[52,10420,211],{"class":102},[52,10422,10423],{"class":54,"line":128},[52,10424,70],{"emptyLinePlaceholder":69},[52,10426,10427,10429,10431,10433,10435,10437,10439,10441],{"class":54,"line":148},[52,10428,9211],{"class":62},[52,10430,80],{"class":79},[52,10432,9216],{"class":62},[52,10434,186],{"class":102},[52,10436,9160],{"class":1320},[52,10438,250],{"class":102},[52,10440,9200],{"class":121},[52,10442,1147],{"class":102},[52,10444,10445,10447,10449,10451],{"class":54,"line":164},[52,10446,242],{"class":241},[52,10448,193],{"class":102},[52,10450,2147],{"class":189},[52,10452,211],{"class":102},[639,10454,10456],{"id":10455},"running-the-script-from-the-wrong-folder","Running the script from the wrong folder",[14,10458,10459],{},"If the terminal is not in the same folder as your file, Python may not find the script.",[14,10461,10462],{},"Use:",[42,10464,10466],{"className":390,"code":10465,"language":392,"meta":47,"style":47},"pwd\nls\ndir\ncd path\u002Fto\u002Fscript\u002Ffolder\n",[49,10467,10468,10472,10476,10480],{"__ignoreMap":47},[52,10469,10470],{"class":54,"line":55},[52,10471,10152],{"class":241},[52,10473,10474],{"class":54,"line":66},[52,10475,10165],{"class":399},[52,10477,10478],{"class":54,"line":73},[52,10479,10170],{"class":399},[52,10481,10482,10484],{"class":54,"line":94},[52,10483,10157],{"class":241},[52,10485,10160],{"class":87},[14,10487,10488],{},"to check where you are and move to the correct folder.",[639,10490,10492],{"id":10491},"misspelling-the-filename-or-command","Misspelling the filename or command",[14,10494,2289],{},[309,10496,10497,10500,10505],{},[24,10498,10499],{},"the filename is correct",[24,10501,10502,10503],{},"the extension is ",[49,10504,10045],{},[24,10506,10507],{},"you use the correct Python command for your system",[37,10509,10511],{"id":10510},"next-steps-after-this-example","Next steps after this example",[14,10513,10514],{},"After this example, a good path is:",[309,10516,10517,10525,10530,10541],{},[24,10518,10519,10520],{},"learn ",[372,10521,10522,10524],{"href":5363},[49,10523,6759],{}," in more detail",[24,10526,10527,10528],{},"read ",[372,10529,5360],{"href":5359},[24,10531,10532,10533],{},"practice ",[372,10534,10535,10537,10538,10540],{"href":10027},[49,10536,3336],{},"\u002F",[49,10539,3552],{}," error handling",[24,10542,10543,10544],{},"move on to ",[372,10545,10546,10548],{"href":9321},[49,10547,5043],{}," for better command line tools",[14,10550,10551],{},"A good exercise is to copy one of the scripts from this page and extend it into:",[309,10553,10554,10557,10560],{},[24,10555,10556],{},"a mini calculator",[24,10558,10559],{},"a text formatter",[24,10561,10562],{},"a simple file-based tool",[37,10564,1942],{"id":1941},[639,10566,10568],{"id":10567},"what-is-a-command-line-tool-in-python","What is a command line tool in Python?",[14,10570,10571],{},"It is a Python script that you run from the terminal and give input through command line arguments.",[639,10573,10575,10576,1995],{"id":10574},"what-is-sysargv","What is ",[49,10577,6759],{},[14,10579,10580],{},"It is a list of values passed to the script from the command line. The first item is the script name.",[639,10582,10584],{"id":10583},"why-does-my-script-fail-when-no-argument-is-given","Why does my script fail when no argument is given?",[14,10586,10587,10588,10590],{},"You are probably reading ",[49,10589,9374],{}," before checking that the user actually passed an argument.",[639,10592,10594,10595,10598,10599,1995],{"id":10593},"can-i-use-input-instead-of-sysargv","Can I use ",[49,10596,10597],{},"input()"," instead of ",[49,10600,6759],{},[14,10602,10603,10604,10609,10610,10612],{},"Yes, but ",[372,10605,10607],{"href":10606},"\u002Freference\u002Fpython-input-function-explained\u002F",[49,10608,10597],{}," asks during program execution. ",[49,10611,6759],{}," lets you pass values when starting the script.",[639,10614,10616],{"id":10615},"what-should-i-learn-after-this","What should I learn after this?",[14,10618,10619,10620,10622,10623,10625,10626,186],{},"Learn ",[49,10621,6759],{}," first, then ",[49,10624,5043],{},", plus basic error handling with ",[49,10627,3603],{},[37,10629,2005],{"id":2004},[309,10631,10632,10638,10642,10649],{},[24,10633,10634],{},[372,10635,10636,2866],{"href":5363},[49,10637,6759],{},[24,10639,10640],{},[372,10641,6863],{"href":5359},[24,10643,10644],{},[372,10645,10646,10647,3711],{"href":9489},"Python ",[49,10648,9492],{},[24,10650,10651],{},[372,10652,10646,10653,10655],{"href":9321},[49,10654,5043],{}," example",[2034,10657,10658],{},"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 .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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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":47,"searchDepth":66,"depth":66,"links":10660},[10661,10662,10663,10664,10665,10668,10672,10676,10683,10684,10693],{"id":39,"depth":66,"text":40},{"id":9294,"depth":66,"text":9295},{"id":9326,"depth":66,"text":9327},{"id":9350,"depth":66,"text":9351},{"id":9496,"depth":66,"text":9497,"children":10666},[10667],{"id":9609,"depth":73,"text":9610},{"id":9690,"depth":66,"text":9691,"children":10669},[10670,10671],{"id":9909,"depth":73,"text":9910},{"id":9928,"depth":73,"text":9929},{"id":6299,"depth":66,"text":6300,"children":10673},[10674,10675],{"id":10056,"depth":73,"text":10057},{"id":10106,"depth":73,"text":10107},{"id":6401,"depth":66,"text":6402,"children":10677},[10678,10679,10681,10682],{"id":10211,"depth":73,"text":10212},{"id":10293,"depth":73,"text":10680},"Using sys.argv[1] without checking length first",{"id":10455,"depth":73,"text":10456},{"id":10491,"depth":73,"text":10492},{"id":10510,"depth":66,"text":10511},{"id":1941,"depth":66,"text":1942,"children":10685},[10686,10687,10689,10690,10692],{"id":10567,"depth":73,"text":10568},{"id":10574,"depth":73,"text":10688},"What is sys.argv?",{"id":10583,"depth":73,"text":10584},{"id":10593,"depth":73,"text":10691},"Can I use input() instead of sys.argv?",{"id":10615,"depth":73,"text":10616},{"id":2004,"depth":66,"text":2005},"Master python command line tool example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-command-line-tool-example",{"title":9077,"description":10694},"examples\u002Fpython-command-line-tool-example","I0FVdj8BoazXg-HWmpgh1LIZJHks7wkS2THwWOkhWS4",{"id":10701,"title":10702,"body":10703,"description":12969,"extension":2075,"meta":12970,"navigation":69,"path":12971,"seo":12972,"stem":12973,"__hash__":12974},"content\u002Fexamples\u002Fpython-contact-book-example.md","Python Contact Book Example",{"type":7,"value":10704,"toc":12936},[10705,10708,10711,10714,10731,10734,10738,10741,10755,10759,10762,10792,10796,10802,10817,10819,10874,10877,10884,10888,10891,10916,10919,11528,11530,11565,11569,11572,11642,11645,11664,11667,11669,11696,11699,11724,11727,11731,11734,11812,11815,11829,11843,11846,11852,11856,11863,11866,11951,11956,11959,11967,11976,11980,11983,11986,12072,12074,12092,12095,12099,12102,12119,12122,12192,12197,12201,12204,12207,12296,12299,12319,12323,12326,12332,12337,12340,12378,12380,12431,12438,12441,12443,12498,12504,12507,12542,12545,12564,12571,12574,12577,12605,12608,12610,12637,12642,12646,12649,12651,12716,12718,12743,12746,12754,12757,12759,12798,12801,12840,12847,12849,12853,12856,12860,12863,12867,12870,12874,12885,12889,12892,12894,12930,12933],[10,10706,10702],{"id":10707},"python-contact-book-example",[14,10709,10710],{},"A small contact book project is a great way to practice Python basics.",[14,10712,10713],{},"In this example, you will build a simple command-line program that:",[309,10715,10716,10719,10722,10725,10728],{},[24,10717,10718],{},"stores contact names and phone numbers",[24,10720,10721],{},"asks the user for input",[24,10723,10724],{},"searches for contacts",[24,10726,10727],{},"deletes contacts",[24,10729,10730],{},"repeats with a menu until the user exits",[14,10732,10733],{},"This project is beginner-friendly because it uses a few core ideas in a practical way.",[37,10735,10737],{"id":10736},"what-this-project-builds","What this project builds",[14,10739,10740],{},"This example creates:",[309,10742,10743,10746,10749,10752],{},[24,10744,10745],{},"A simple command-line contact book",[24,10747,10748],{},"A program that stores contact names and phone numbers",[24,10750,10751],{},"A menu that lets the user add, view, search, and delete contacts",[24,10753,10754],{},"A good beginner project for practicing dictionaries and loops",[37,10756,10758],{"id":10757},"skills-this-example-practices","Skills this example practices",[14,10760,10761],{},"While building this project, you will practice:",[309,10763,10764,10767,10774,10781,10789],{},[24,10765,10766],{},"Using a dictionary to store related data",[24,10768,10769,10770,10773],{},"Using a ",[49,10771,10772],{},"while"," loop for a repeating menu",[24,10775,10294,10776],{},[372,10777,10778,10780],{"href":6397},[49,10779,10597],{}," to get user input",[24,10782,10294,10783,759,10785,10788],{},[49,10784,1313],{},[49,10786,10787],{},"elif"," to handle menu options",[24,10790,10791],{},"Checking if a key exists before reading or deleting it",[37,10793,10795],{"id":10794},"how-the-contact-data-is-stored","How the contact data is stored",[14,10797,10798,10799,186],{},"The program uses a dictionary named ",[49,10800,10801],{},"contacts",[309,10803,10804,10811],{},[24,10805,10806,10807,10810],{},"Each ",[5503,10808,10809],{},"key"," is a contact name",[24,10812,10806,10813,10816],{},[5503,10814,10815],{},"value"," is that contact's phone number",[14,10818,4259],{},[42,10820,10822],{"className":44,"code":10821,"language":46,"meta":47,"style":47},"contacts = {\n    \"Alice\": \"1234567890\",\n    \"Bob\": \"5551234567\"\n}\n",[49,10823,10824,10833,10852,10870],{"__ignoreMap":47},[52,10825,10826,10829,10831],{"class":54,"line":55},[52,10827,10828],{"class":62},"contacts ",[52,10830,80],{"class":79},[52,10832,103],{"class":102},[52,10834,10835,10837,10839,10841,10843,10845,10848,10850],{"class":54,"line":66},[52,10836,109],{"class":83},[52,10838,2156],{"class":87},[52,10840,115],{"class":83},[52,10842,118],{"class":102},[52,10844,84],{"class":83},[52,10846,10847],{"class":87},"1234567890",[52,10849,115],{"class":83},[52,10851,125],{"class":102},[52,10853,10854,10856,10859,10861,10863,10865,10868],{"class":54,"line":73},[52,10855,109],{"class":83},[52,10857,10858],{"class":87},"Bob",[52,10860,115],{"class":83},[52,10862,118],{"class":102},[52,10864,84],{"class":83},[52,10866,10867],{"class":87},"5551234567",[52,10869,91],{"class":83},[52,10871,10872],{"class":54,"line":94},[52,10873,167],{"class":102},[14,10875,10876],{},"This keeps the project simple and easy to understand.",[14,10878,10879,10880,186],{},"If you are new to dictionaries, see ",[372,10881,10883],{"href":10882},"\u002Flearn\u002Fpython-dictionaries-explained\u002F","Python dictionaries explained",[37,10885,10887],{"id":10886},"build-the-menu-step-by-step","Build the menu step by step",[14,10889,10890],{},"The basic idea is:",[21,10892,10893,10896,10902,10907,10910],{},[24,10894,10895],{},"Start with an empty dictionary",[24,10897,3650,10898,10901],{},[49,10899,10900],{},"while True"," to keep showing the menu",[24,10903,10904,10905],{},"Read the user's choice with ",[49,10906,10597],{},[24,10908,10909],{},"Run different code based on the choice",[24,10911,3650,10912,10915],{},[49,10913,10914],{},"break"," to exit",[14,10917,10918],{},"Here is the full working script:",[42,10920,10922],{"className":44,"code":10921,"language":46,"meta":47,"style":47},"contacts = {}\n\nwhile True:\n    print(\"\\n1. Add contact\")\n    print(\"2. View contacts\")\n    print(\"3. Search contact\")\n    print(\"4. Delete contact\")\n    print(\"5. Exit\")\n\n    choice = input(\"Choose an option: \")\n\n    if choice == \"1\":\n        name = input(\"Name: \")\n        phone = input(\"Phone: \")\n        contacts[name] = phone\n        print(\"Contact saved.\")\n\n    elif choice == \"2\":\n        if not contacts:\n            print(\"No contacts found.\")\n        else:\n            for name, phone in contacts.items():\n                print(name, \"-\", phone)\n\n    elif choice == \"3\":\n        name = input(\"Enter name to search: \")\n        if name in contacts:\n            print(name, \"-\", contacts[name])\n        else:\n            print(\"Contact not found.\")\n\n    elif choice == \"4\":\n        name = input(\"Enter name to delete: \")\n        if name in contacts:\n            del contacts[name]\n            print(\"Contact deleted.\")\n        else:\n            print(\"Contact not found.\")\n\n    elif choice == \"5\":\n        print(\"Goodbye!\")\n        break\n\n    else:\n        print(\"Invalid option.\")\n",[49,10923,10924,10933,10937,10946,10964,10979,10994,11009,11024,11028,11049,11053,11071,11091,11111,11128,11143,11147,11164,11176,11192,11199,11223,11247,11251,11267,11286,11299,11325,11332,11348,11353,11371,11391,11404,11418,11434,11441,11456,11461,11478,11494,11500,11505,11512],{"__ignoreMap":47},[52,10925,10926,10928,10930],{"class":54,"line":55},[52,10927,10828],{"class":62},[52,10929,80],{"class":79},[52,10931,10932],{"class":102}," {}\n",[52,10934,10935],{"class":54,"line":66},[52,10936,70],{"emptyLinePlaceholder":69},[52,10938,10939,10941,10944],{"class":54,"line":73},[52,10940,10772],{"class":58},[52,10942,10943],{"class":160}," True",[52,10945,1330],{"class":102},[52,10947,10948,10950,10952,10954,10957,10960,10962],{"class":54,"line":94},[52,10949,1599],{"class":241},[52,10951,193],{"class":102},[52,10953,115],{"class":83},[52,10955,10956],{"class":2533},"\\n",[52,10958,10959],{"class":87},"1. Add contact",[52,10961,115],{"class":83},[52,10963,211],{"class":102},[52,10965,10966,10968,10970,10972,10975,10977],{"class":54,"line":106},[52,10967,1599],{"class":241},[52,10969,193],{"class":102},[52,10971,115],{"class":83},[52,10973,10974],{"class":87},"2. View contacts",[52,10976,115],{"class":83},[52,10978,211],{"class":102},[52,10980,10981,10983,10985,10987,10990,10992],{"class":54,"line":128},[52,10982,1599],{"class":241},[52,10984,193],{"class":102},[52,10986,115],{"class":83},[52,10988,10989],{"class":87},"3. Search contact",[52,10991,115],{"class":83},[52,10993,211],{"class":102},[52,10995,10996,10998,11000,11002,11005,11007],{"class":54,"line":148},[52,10997,1599],{"class":241},[52,10999,193],{"class":102},[52,11001,115],{"class":83},[52,11003,11004],{"class":87},"4. Delete contact",[52,11006,115],{"class":83},[52,11008,211],{"class":102},[52,11010,11011,11013,11015,11017,11020,11022],{"class":54,"line":164},[52,11012,1599],{"class":241},[52,11014,193],{"class":102},[52,11016,115],{"class":83},[52,11018,11019],{"class":87},"5. Exit",[52,11021,115],{"class":83},[52,11023,211],{"class":102},[52,11025,11026],{"class":54,"line":170},[52,11027,70],{"emptyLinePlaceholder":69},[52,11029,11030,11033,11035,11038,11040,11042,11045,11047],{"class":54,"line":175},[52,11031,11032],{"class":62},"    choice ",[52,11034,80],{"class":79},[52,11036,11037],{"class":241}," input",[52,11039,193],{"class":102},[52,11041,115],{"class":83},[52,11043,11044],{"class":87},"Choose an option: ",[52,11046,115],{"class":83},[52,11048,211],{"class":102},[52,11050,11051],{"class":54,"line":214},[52,11052,70],{"emptyLinePlaceholder":69},[52,11054,11055,11057,11060,11063,11065,11067,11069],{"class":54,"line":233},[52,11056,1438],{"class":58},[52,11058,11059],{"class":62}," choice ",[52,11061,11062],{"class":79},"==",[52,11064,84],{"class":83},[52,11066,9200],{"class":87},[52,11068,115],{"class":83},[52,11070,1330],{"class":102},[52,11072,11073,11076,11078,11080,11082,11084,11087,11089],{"class":54,"line":238},[52,11074,11075],{"class":62},"        name ",[52,11077,80],{"class":79},[52,11079,11037],{"class":241},[52,11081,193],{"class":102},[52,11083,115],{"class":83},[52,11085,11086],{"class":87},"Name: ",[52,11088,115],{"class":83},[52,11090,211],{"class":102},[52,11092,11093,11096,11098,11100,11102,11104,11107,11109],{"class":54,"line":272},[52,11094,11095],{"class":62},"        phone ",[52,11097,80],{"class":79},[52,11099,11037],{"class":241},[52,11101,193],{"class":102},[52,11103,115],{"class":83},[52,11105,11106],{"class":87},"Phone: ",[52,11108,115],{"class":83},[52,11110,211],{"class":102},[52,11112,11113,11116,11118,11120,11123,11125],{"class":54,"line":1348},[52,11114,11115],{"class":62},"        contacts",[52,11117,250],{"class":102},[52,11119,2147],{"class":62},[52,11121,11122],{"class":102},"]",[52,11124,7038],{"class":79},[52,11126,11127],{"class":62}," phone\n",[52,11129,11130,11132,11134,11136,11139,11141],{"class":54,"line":1376},[52,11131,1470],{"class":241},[52,11133,193],{"class":102},[52,11135,115],{"class":83},[52,11137,11138],{"class":87},"Contact saved.",[52,11140,115],{"class":83},[52,11142,211],{"class":102},[52,11144,11145],{"class":54,"line":1381},[52,11146,70],{"emptyLinePlaceholder":69},[52,11148,11149,11152,11154,11156,11158,11160,11162],{"class":54,"line":1406},[52,11150,11151],{"class":58},"    elif",[52,11153,11059],{"class":62},[52,11155,11062],{"class":79},[52,11157,84],{"class":83},[52,11159,9825],{"class":87},[52,11161,115],{"class":83},[52,11163,1330],{"class":102},[52,11165,11166,11169,11171,11174],{"class":54,"line":1430},[52,11167,11168],{"class":58},"        if",[52,11170,1447],{"class":79},[52,11172,11173],{"class":62}," contacts",[52,11175,1330],{"class":102},[52,11177,11178,11181,11183,11185,11188,11190],{"class":54,"line":1435},[52,11179,11180],{"class":241},"            print",[52,11182,193],{"class":102},[52,11184,115],{"class":83},[52,11186,11187],{"class":87},"No contacts found.",[52,11189,115],{"class":83},[52,11191,211],{"class":102},[52,11193,11194,11197],{"class":54,"line":1467},[52,11195,11196],{"class":58},"        else",[52,11198,1330],{"class":102},[52,11200,11201,11204,11206,11208,11211,11214,11216,11218,11221],{"class":54,"line":1495},[52,11202,11203],{"class":58},"            for",[52,11205,7019],{"class":62},[52,11207,199],{"class":102},[52,11209,11210],{"class":62}," phone ",[52,11212,11213],{"class":58},"in",[52,11215,11173],{"class":62},[52,11217,186],{"class":102},[52,11219,11220],{"class":189},"items",[52,11222,8146],{"class":102},[52,11224,11225,11228,11230,11232,11234,11236,11238,11240,11242,11245],{"class":54,"line":1531},[52,11226,11227],{"class":241},"                print",[52,11229,193],{"class":102},[52,11231,2147],{"class":189},[52,11233,199],{"class":102},[52,11235,84],{"class":83},[52,11237,5889],{"class":87},[52,11239,115],{"class":83},[52,11241,199],{"class":102},[52,11243,11244],{"class":189}," phone",[52,11246,211],{"class":102},[52,11248,11249],{"class":54,"line":1564},[52,11250,70],{"emptyLinePlaceholder":69},[52,11252,11253,11255,11257,11259,11261,11263,11265],{"class":54,"line":1572},[52,11254,11151],{"class":58},[52,11256,11059],{"class":62},[52,11258,11062],{"class":79},[52,11260,84],{"class":83},[52,11262,7508],{"class":87},[52,11264,115],{"class":83},[52,11266,1330],{"class":102},[52,11268,11269,11271,11273,11275,11277,11279,11282,11284],{"class":54,"line":1588},[52,11270,11075],{"class":62},[52,11272,80],{"class":79},[52,11274,11037],{"class":241},[52,11276,193],{"class":102},[52,11278,115],{"class":83},[52,11280,11281],{"class":87},"Enter name to search: ",[52,11283,115],{"class":83},[52,11285,211],{"class":102},[52,11287,11288,11290,11293,11295,11297],{"class":54,"line":1596},[52,11289,11168],{"class":58},[52,11291,11292],{"class":62}," name ",[52,11294,11213],{"class":79},[52,11296,11173],{"class":62},[52,11298,1330],{"class":102},[52,11300,11301,11303,11305,11307,11309,11311,11313,11315,11317,11319,11321,11323],{"class":54,"line":1613},[52,11302,11180],{"class":241},[52,11304,193],{"class":102},[52,11306,2147],{"class":189},[52,11308,199],{"class":102},[52,11310,84],{"class":83},[52,11312,5889],{"class":87},[52,11314,115],{"class":83},[52,11316,199],{"class":102},[52,11318,11173],{"class":189},[52,11320,250],{"class":102},[52,11322,2147],{"class":189},[52,11324,269],{"class":102},[52,11326,11328,11330],{"class":54,"line":11327},29,[52,11329,11196],{"class":58},[52,11331,1330],{"class":102},[52,11333,11335,11337,11339,11341,11344,11346],{"class":54,"line":11334},30,[52,11336,11180],{"class":241},[52,11338,193],{"class":102},[52,11340,115],{"class":83},[52,11342,11343],{"class":87},"Contact not found.",[52,11345,115],{"class":83},[52,11347,211],{"class":102},[52,11349,11351],{"class":54,"line":11350},31,[52,11352,70],{"emptyLinePlaceholder":69},[52,11354,11356,11358,11360,11362,11364,11367,11369],{"class":54,"line":11355},32,[52,11357,11151],{"class":58},[52,11359,11059],{"class":62},[52,11361,11062],{"class":79},[52,11363,84],{"class":83},[52,11365,11366],{"class":87},"4",[52,11368,115],{"class":83},[52,11370,1330],{"class":102},[52,11372,11374,11376,11378,11380,11382,11384,11387,11389],{"class":54,"line":11373},33,[52,11375,11075],{"class":62},[52,11377,80],{"class":79},[52,11379,11037],{"class":241},[52,11381,193],{"class":102},[52,11383,115],{"class":83},[52,11385,11386],{"class":87},"Enter name to delete: ",[52,11388,115],{"class":83},[52,11390,211],{"class":102},[52,11392,11394,11396,11398,11400,11402],{"class":54,"line":11393},34,[52,11395,11168],{"class":58},[52,11397,11292],{"class":62},[52,11399,11213],{"class":79},[52,11401,11173],{"class":62},[52,11403,1330],{"class":102},[52,11405,11407,11410,11412,11414,11416],{"class":54,"line":11406},35,[52,11408,11409],{"class":58},"            del",[52,11411,11173],{"class":62},[52,11413,250],{"class":102},[52,11415,2147],{"class":62},[52,11417,1147],{"class":102},[52,11419,11421,11423,11425,11427,11430,11432],{"class":54,"line":11420},36,[52,11422,11180],{"class":241},[52,11424,193],{"class":102},[52,11426,115],{"class":83},[52,11428,11429],{"class":87},"Contact deleted.",[52,11431,115],{"class":83},[52,11433,211],{"class":102},[52,11435,11437,11439],{"class":54,"line":11436},37,[52,11438,11196],{"class":58},[52,11440,1330],{"class":102},[52,11442,11444,11446,11448,11450,11452,11454],{"class":54,"line":11443},38,[52,11445,11180],{"class":241},[52,11447,193],{"class":102},[52,11449,115],{"class":83},[52,11451,11343],{"class":87},[52,11453,115],{"class":83},[52,11455,211],{"class":102},[52,11457,11459],{"class":54,"line":11458},39,[52,11460,70],{"emptyLinePlaceholder":69},[52,11462,11464,11466,11468,11470,11472,11474,11476],{"class":54,"line":11463},40,[52,11465,11151],{"class":58},[52,11467,11059],{"class":62},[52,11469,11062],{"class":79},[52,11471,84],{"class":83},[52,11473,9479],{"class":87},[52,11475,115],{"class":83},[52,11477,1330],{"class":102},[52,11479,11481,11483,11485,11487,11490,11492],{"class":54,"line":11480},41,[52,11482,1470],{"class":241},[52,11484,193],{"class":102},[52,11486,115],{"class":83},[52,11488,11489],{"class":87},"Goodbye!",[52,11491,115],{"class":83},[52,11493,211],{"class":102},[52,11495,11497],{"class":54,"line":11496},42,[52,11498,11499],{"class":58},"        break\n",[52,11501,11503],{"class":54,"line":11502},43,[52,11504,70],{"emptyLinePlaceholder":69},[52,11506,11508,11510],{"class":54,"line":11507},44,[52,11509,1567],{"class":58},[52,11511,1330],{"class":102},[52,11513,11515,11517,11519,11521,11524,11526],{"class":54,"line":11514},45,[52,11516,1470],{"class":241},[52,11518,193],{"class":102},[52,11520,115],{"class":83},[52,11522,11523],{"class":87},"Invalid option.",[52,11525,115],{"class":83},[52,11527,211],{"class":102},[639,11529,6194],{"id":6193},[309,11531,11532,11538,11544,11550,11560],{},[24,11533,11534,11537],{},[49,11535,11536],{},"contacts = {}"," creates an empty dictionary",[24,11539,11540,11543],{},[49,11541,11542],{},"while True:"," creates a loop that keeps running",[24,11545,11546,11549],{},[49,11547,11548],{},"choice = input(...)"," reads the menu option",[24,11551,11552,2587,11554,11556,11557,11559],{},[49,11553,1313],{},[49,11555,10787],{},", and ",[49,11558,1591],{}," decide which action to run",[24,11561,11562,11564],{},[49,11563,10914],{}," stops the loop when the user chooses to exit",[37,11566,11568],{"id":11567},"add-a-new-contact","Add a new contact",[14,11570,11571],{},"To add a contact, ask for the name and phone number, then save it in the dictionary.",[42,11573,11575],{"className":44,"code":11574,"language":46,"meta":47,"style":47},"name = input(\"Name: \")\nphone = input(\"Phone: \")\ncontacts[name] = phone\nprint(\"Contact saved.\")\n",[49,11576,11577,11595,11614,11628],{"__ignoreMap":47},[52,11578,11579,11581,11583,11585,11587,11589,11591,11593],{"class":54,"line":55},[52,11580,9211],{"class":62},[52,11582,80],{"class":79},[52,11584,11037],{"class":241},[52,11586,193],{"class":102},[52,11588,115],{"class":83},[52,11590,11086],{"class":87},[52,11592,115],{"class":83},[52,11594,211],{"class":102},[52,11596,11597,11600,11602,11604,11606,11608,11610,11612],{"class":54,"line":66},[52,11598,11599],{"class":62},"phone ",[52,11601,80],{"class":79},[52,11603,11037],{"class":241},[52,11605,193],{"class":102},[52,11607,115],{"class":83},[52,11609,11106],{"class":87},[52,11611,115],{"class":83},[52,11613,211],{"class":102},[52,11615,11616,11618,11620,11622,11624,11626],{"class":54,"line":73},[52,11617,10801],{"class":62},[52,11619,250],{"class":102},[52,11621,2147],{"class":62},[52,11623,11122],{"class":102},[52,11625,7038],{"class":79},[52,11627,11127],{"class":62},[52,11629,11630,11632,11634,11636,11638,11640],{"class":54,"line":94},[52,11631,242],{"class":241},[52,11633,193],{"class":102},[52,11635,115],{"class":83},[52,11637,11138],{"class":87},[52,11639,115],{"class":83},[52,11641,211],{"class":102},[14,11643,11644],{},"Key line:",[42,11646,11648],{"className":44,"code":11647,"language":46,"meta":47,"style":47},"contacts[name] = phone\n",[49,11649,11650],{"__ignoreMap":47},[52,11651,11652,11654,11656,11658,11660,11662],{"class":54,"line":55},[52,11653,10801],{"class":62},[52,11655,250],{"class":102},[52,11657,2147],{"class":62},[52,11659,11122],{"class":102},[52,11661,7038],{"class":79},[52,11663,11127],{"class":62},[14,11665,11666],{},"This stores the phone number under the contact name.",[14,11668,4259],{},[42,11670,11672],{"className":44,"code":11671,"language":46,"meta":47,"style":47},"contacts[\"Alice\"] = \"1234567890\"\n",[49,11673,11674],{"__ignoreMap":47},[52,11675,11676,11678,11680,11682,11684,11686,11688,11690,11692,11694],{"class":54,"line":55},[52,11677,10801],{"class":62},[52,11679,250],{"class":102},[52,11681,115],{"class":83},[52,11683,2156],{"class":87},[52,11685,115],{"class":83},[52,11687,11122],{"class":102},[52,11689,7038],{"class":79},[52,11691,84],{"class":83},[52,11693,10847],{"class":87},[52,11695,91],{"class":83},[14,11697,11698],{},"After that, the dictionary becomes:",[42,11700,11702],{"className":44,"code":11701,"language":46,"meta":47,"style":47},"{\"Alice\": \"1234567890\"}\n",[49,11703,11704],{"__ignoreMap":47},[52,11705,11706,11708,11710,11712,11714,11716,11718,11720,11722],{"class":54,"line":55},[52,11707,1482],{"class":102},[52,11709,115],{"class":83},[52,11711,2156],{"class":87},[52,11713,115],{"class":83},[52,11715,118],{"class":102},[52,11717,84],{"class":83},[52,11719,10847],{"class":87},[52,11721,115],{"class":83},[52,11723,167],{"class":102},[14,11725,11726],{},"One important detail: if the same name is added again, the old phone number is replaced. That happens because dictionary keys must be unique.",[37,11728,11730],{"id":11729},"view-all-contacts","View all contacts",[14,11732,11733],{},"To show all contacts, first check whether the dictionary is empty.",[42,11735,11737],{"className":44,"code":11736,"language":46,"meta":47,"style":47},"if not contacts:\n    print(\"No contacts found.\")\nelse:\n    for name, phone in contacts.items():\n        print(name, \"-\", phone)\n",[49,11738,11739,11749,11763,11769,11790],{"__ignoreMap":47},[52,11740,11741,11743,11745,11747],{"class":54,"line":55},[52,11742,1313],{"class":58},[52,11744,1447],{"class":79},[52,11746,11173],{"class":62},[52,11748,1330],{"class":102},[52,11750,11751,11753,11755,11757,11759,11761],{"class":54,"line":66},[52,11752,1599],{"class":241},[52,11754,193],{"class":102},[52,11756,115],{"class":83},[52,11758,11187],{"class":87},[52,11760,115],{"class":83},[52,11762,211],{"class":102},[52,11764,11765,11767],{"class":54,"line":73},[52,11766,1591],{"class":58},[52,11768,1330],{"class":102},[52,11770,11771,11774,11776,11778,11780,11782,11784,11786,11788],{"class":54,"line":94},[52,11772,11773],{"class":58},"    for",[52,11775,7019],{"class":62},[52,11777,199],{"class":102},[52,11779,11210],{"class":62},[52,11781,11213],{"class":58},[52,11783,11173],{"class":62},[52,11785,186],{"class":102},[52,11787,11220],{"class":189},[52,11789,8146],{"class":102},[52,11791,11792,11794,11796,11798,11800,11802,11804,11806,11808,11810],{"class":54,"line":106},[52,11793,1470],{"class":241},[52,11795,193],{"class":102},[52,11797,2147],{"class":189},[52,11799,199],{"class":102},[52,11801,84],{"class":83},[52,11803,5889],{"class":87},[52,11805,115],{"class":83},[52,11807,199],{"class":102},[52,11809,11244],{"class":189},[52,11811,211],{"class":102},[14,11813,11814],{},"This does two things:",[309,11816,11817,11823],{},[24,11818,11819,11822],{},[49,11820,11821],{},"if not contacts:"," checks whether the dictionary is empty",[24,11824,11825,11828],{},[49,11826,11827],{},"contacts.items()"," gives you each name and phone number pair",[14,11830,11831,11832,4441,11836,186],{},"If you want more help with this pattern, see ",[372,11833,11835],{"href":11834},"\u002Fhow-to\u002Fhow-to-loop-through-a-dictionary-in-python\u002F","how to loop through a dictionary in Python",[372,11837,11839,11842],{"href":11838},"\u002Freference\u002Fpython-dictionary-items-method\u002F",[49,11840,11841],{},"dict.items()"," method reference",[14,11844,11845],{},"Example output:",[42,11847,11850],{"className":11848,"code":11849,"language":955,"meta":47},[953],"Alice - 1234567890\nBob - 5551234567\n",[49,11851,11849],{"__ignoreMap":47},[37,11853,11855],{"id":11854},"search-for-a-contact-safely","Search for a contact safely",[14,11857,11858,11859,11862],{},"When searching, do not directly use ",[49,11860,11861],{},"contacts[name]"," unless you know the name exists.",[14,11864,11865],{},"Use a key check first:",[42,11867,11869],{"className":44,"code":11868,"language":46,"meta":47,"style":47},"name = input(\"Enter name to search: \")\n\nif name in contacts:\n    print(name, \"-\", contacts[name])\nelse:\n    print(\"Contact not found.\")\n",[49,11870,11871,11889,11893,11905,11931,11937],{"__ignoreMap":47},[52,11872,11873,11875,11877,11879,11881,11883,11885,11887],{"class":54,"line":55},[52,11874,9211],{"class":62},[52,11876,80],{"class":79},[52,11878,11037],{"class":241},[52,11880,193],{"class":102},[52,11882,115],{"class":83},[52,11884,11281],{"class":87},[52,11886,115],{"class":83},[52,11888,211],{"class":102},[52,11890,11891],{"class":54,"line":66},[52,11892,70],{"emptyLinePlaceholder":69},[52,11894,11895,11897,11899,11901,11903],{"class":54,"line":73},[52,11896,1313],{"class":58},[52,11898,11292],{"class":62},[52,11900,11213],{"class":79},[52,11902,11173],{"class":62},[52,11904,1330],{"class":102},[52,11906,11907,11909,11911,11913,11915,11917,11919,11921,11923,11925,11927,11929],{"class":54,"line":94},[52,11908,1599],{"class":241},[52,11910,193],{"class":102},[52,11912,2147],{"class":189},[52,11914,199],{"class":102},[52,11916,84],{"class":83},[52,11918,5889],{"class":87},[52,11920,115],{"class":83},[52,11922,199],{"class":102},[52,11924,11173],{"class":189},[52,11926,250],{"class":102},[52,11928,2147],{"class":189},[52,11930,269],{"class":102},[52,11932,11933,11935],{"class":54,"line":106},[52,11934,1591],{"class":58},[52,11936,1330],{"class":102},[52,11938,11939,11941,11943,11945,11947,11949],{"class":54,"line":128},[52,11940,1599],{"class":241},[52,11942,193],{"class":102},[52,11944,115],{"class":83},[52,11946,11343],{"class":87},[52,11948,115],{"class":83},[52,11950,211],{"class":102},[14,11952,11953,11954,186],{},"This avoids a ",[49,11955,1701],{},[14,11957,11958],{},"Why this matters:",[309,11960,11961,11964],{},[24,11962,11963],{},"If the name exists, you can safely read the phone number",[24,11965,11966],{},"If the name does not exist, the program prints a clear message instead of crashing",[14,11968,11969,11970,759,11974,186],{},"If you want more detail, see ",[372,11971,11973],{"href":11972},"\u002Fhow-to\u002Fhow-to-check-if-a-key-exists-in-a-dictionary-in-python\u002F","how to check if a key exists in a dictionary in Python",[372,11975,1721],{"href":1720},[37,11977,11979],{"id":11978},"delete-a-contact-safely","Delete a contact safely",[14,11981,11982],{},"Deleting follows the same idea as searching.",[14,11984,11985],{},"Check first, then delete:",[42,11987,11989],{"className":44,"code":11988,"language":46,"meta":47,"style":47},"name = input(\"Enter name to delete: \")\n\nif name in contacts:\n    del contacts[name]\n    print(\"Contact deleted.\")\nelse:\n    print(\"Contact not found.\")\n",[49,11990,11991,12009,12013,12025,12038,12052,12058],{"__ignoreMap":47},[52,11992,11993,11995,11997,11999,12001,12003,12005,12007],{"class":54,"line":55},[52,11994,9211],{"class":62},[52,11996,80],{"class":79},[52,11998,11037],{"class":241},[52,12000,193],{"class":102},[52,12002,115],{"class":83},[52,12004,11386],{"class":87},[52,12006,115],{"class":83},[52,12008,211],{"class":102},[52,12010,12011],{"class":54,"line":66},[52,12012,70],{"emptyLinePlaceholder":69},[52,12014,12015,12017,12019,12021,12023],{"class":54,"line":73},[52,12016,1313],{"class":58},[52,12018,11292],{"class":62},[52,12020,11213],{"class":79},[52,12022,11173],{"class":62},[52,12024,1330],{"class":102},[52,12026,12027,12030,12032,12034,12036],{"class":54,"line":94},[52,12028,12029],{"class":58},"    del",[52,12031,11173],{"class":62},[52,12033,250],{"class":102},[52,12035,2147],{"class":62},[52,12037,1147],{"class":102},[52,12039,12040,12042,12044,12046,12048,12050],{"class":54,"line":106},[52,12041,1599],{"class":241},[52,12043,193],{"class":102},[52,12045,115],{"class":83},[52,12047,11429],{"class":87},[52,12049,115],{"class":83},[52,12051,211],{"class":102},[52,12053,12054,12056],{"class":54,"line":128},[52,12055,1591],{"class":58},[52,12057,1330],{"class":102},[52,12059,12060,12062,12064,12066,12068,12070],{"class":54,"line":148},[52,12061,1599],{"class":241},[52,12063,193],{"class":102},[52,12065,115],{"class":83},[52,12067,11343],{"class":87},[52,12069,115],{"class":83},[52,12071,211],{"class":102},[14,12073,11644],{},[42,12075,12077],{"className":44,"code":12076,"language":46,"meta":47,"style":47},"del contacts[name]\n",[49,12078,12079],{"__ignoreMap":47},[52,12080,12081,12084,12086,12088,12090],{"class":54,"line":55},[52,12082,12083],{"class":58},"del",[52,12085,11173],{"class":62},[52,12087,250],{"class":102},[52,12089,2147],{"class":62},[52,12091,1147],{"class":102},[14,12093,12094],{},"This removes the contact from the dictionary, but only if the name exists.",[37,12096,12098],{"id":12097},"common-beginner-improvements","Common beginner improvements",[14,12100,12101],{},"Once the basic version works, you can improve it in simple ways:",[309,12103,12104,12107,12110,12113,12116],{},[24,12105,12106],{},"Store more than one field per contact",[24,12108,12109],{},"Use nested dictionaries for phone and email",[24,12111,12112],{},"Make name search case-insensitive",[24,12114,12115],{},"Sort contacts before printing",[24,12117,12118],{},"Save contacts to a file later",[14,12120,12121],{},"For example, a contact could look like this:",[42,12123,12125],{"className":44,"code":12124,"language":46,"meta":47,"style":47},"contacts = {\n    \"Alice\": {\n        \"phone\": \"1234567890\",\n        \"email\": \"alice@example.com\"\n    }\n}\n",[49,12126,12127,12135,12147,12166,12184,12188],{"__ignoreMap":47},[52,12128,12129,12131,12133],{"class":54,"line":55},[52,12130,10828],{"class":62},[52,12132,80],{"class":79},[52,12134,103],{"class":102},[52,12136,12137,12139,12141,12143,12145],{"class":54,"line":66},[52,12138,109],{"class":83},[52,12140,2156],{"class":87},[52,12142,115],{"class":83},[52,12144,118],{"class":102},[52,12146,103],{"class":102},[52,12148,12149,12151,12154,12156,12158,12160,12162,12164],{"class":54,"line":73},[52,12150,1029],{"class":83},[52,12152,12153],{"class":87},"phone",[52,12155,115],{"class":83},[52,12157,118],{"class":102},[52,12159,84],{"class":83},[52,12161,10847],{"class":87},[52,12163,115],{"class":83},[52,12165,125],{"class":102},[52,12167,12168,12170,12173,12175,12177,12179,12182],{"class":54,"line":94},[52,12169,1029],{"class":83},[52,12171,12172],{"class":87},"email",[52,12174,115],{"class":83},[52,12176,118],{"class":102},[52,12178,84],{"class":83},[52,12180,12181],{"class":87},"alice@example.com",[52,12183,91],{"class":83},[52,12185,12186],{"class":54,"line":106},[52,12187,1090],{"class":102},[52,12189,12190],{"class":54,"line":128},[52,12191,167],{"class":102},[14,12193,12194,12195,186],{},"A useful next step is saving your contacts to a JSON file. See ",[372,12196,856],{"href":855},[37,12198,12200],{"id":12199},"beginner-debugging-tips","Beginner debugging tips",[14,12202,12203],{},"If your program does not behave as expected, test one small part at a time.",[14,12205,12206],{},"Helpful debug lines:",[42,12208,12210],{"className":44,"code":12209,"language":46,"meta":47,"style":47},"print(contacts)\nprint(choice)\nprint(type(choice))\nprint(name in contacts)\nfor name, phone in contacts.items():\n    print(name, phone)\n",[49,12211,12212,12222,12233,12247,12261,12282],{"__ignoreMap":47},[52,12213,12214,12216,12218,12220],{"class":54,"line":55},[52,12215,242],{"class":241},[52,12217,193],{"class":102},[52,12219,10801],{"class":189},[52,12221,211],{"class":102},[52,12223,12224,12226,12228,12231],{"class":54,"line":66},[52,12225,242],{"class":241},[52,12227,193],{"class":102},[52,12229,12230],{"class":189},"choice",[52,12232,211],{"class":102},[52,12234,12235,12237,12239,12241,12243,12245],{"class":54,"line":73},[52,12236,242],{"class":241},[52,12238,193],{"class":102},[52,12240,4198],{"class":4733},[52,12242,193],{"class":102},[52,12244,12230],{"class":189},[52,12246,8886],{"class":102},[52,12248,12249,12251,12253,12255,12257,12259],{"class":54,"line":94},[52,12250,242],{"class":241},[52,12252,193],{"class":102},[52,12254,9211],{"class":189},[52,12256,11213],{"class":58},[52,12258,11173],{"class":189},[52,12260,211],{"class":102},[52,12262,12263,12266,12268,12270,12272,12274,12276,12278,12280],{"class":54,"line":106},[52,12264,12265],{"class":58},"for",[52,12267,7019],{"class":62},[52,12269,199],{"class":102},[52,12271,11210],{"class":62},[52,12273,11213],{"class":58},[52,12275,11173],{"class":62},[52,12277,186],{"class":102},[52,12279,11220],{"class":189},[52,12281,8146],{"class":102},[52,12283,12284,12286,12288,12290,12292,12294],{"class":54,"line":128},[52,12285,1599],{"class":241},[52,12287,193],{"class":102},[52,12289,2147],{"class":189},[52,12291,199],{"class":102},[52,12293,11244],{"class":189},[52,12295,211],{"class":102},[14,12297,12298],{},"These can help you check:",[309,12300,12301,12304,12307,12313,12316],{},[24,12302,12303],{},"whether contacts are being stored correctly",[24,12305,12306],{},"what the user actually entered",[24,12308,12309,12310,12312],{},"whether ",[49,12311,12230],{}," is a string",[24,12314,12315],{},"whether a name exists in the dictionary",[24,12317,12318],{},"whether the loop is reading your data correctly",[37,12320,12322],{"id":12321},"common-mistakes","Common mistakes",[14,12324,12325],{},"Here are some common beginner problems in this project.",[639,12327,10294,12329,12331],{"id":12328},"using-contactsname-before-checking-if-the-name-exists",[49,12330,11861],{}," before checking if the name exists",[14,12333,12334,12335,186],{},"This can raise a ",[49,12336,1701],{},[14,12338,12339],{},"Problem example:",[42,12341,12343],{"className":44,"code":12342,"language":46,"meta":47,"style":47},"name = input(\"Enter name: \")\nprint(contacts[name])\n",[49,12344,12345,12364],{"__ignoreMap":47},[52,12346,12347,12349,12351,12353,12355,12357,12360,12362],{"class":54,"line":55},[52,12348,9211],{"class":62},[52,12350,80],{"class":79},[52,12352,11037],{"class":241},[52,12354,193],{"class":102},[52,12356,115],{"class":83},[52,12358,12359],{"class":87},"Enter name: ",[52,12361,115],{"class":83},[52,12363,211],{"class":102},[52,12365,12366,12368,12370,12372,12374,12376],{"class":54,"line":66},[52,12367,242],{"class":241},[52,12369,193],{"class":102},[52,12371,10801],{"class":189},[52,12373,250],{"class":102},[52,12375,2147],{"class":189},[52,12377,269],{"class":102},[14,12379,10355],{},[42,12381,12383],{"className":44,"code":12382,"language":46,"meta":47,"style":47},"if name in contacts:\n    print(contacts[name])\nelse:\n    print(\"Contact not found.\")\n",[49,12384,12385,12397,12411,12417],{"__ignoreMap":47},[52,12386,12387,12389,12391,12393,12395],{"class":54,"line":55},[52,12388,1313],{"class":58},[52,12390,11292],{"class":62},[52,12392,11213],{"class":79},[52,12394,11173],{"class":62},[52,12396,1330],{"class":102},[52,12398,12399,12401,12403,12405,12407,12409],{"class":54,"line":66},[52,12400,1599],{"class":241},[52,12402,193],{"class":102},[52,12404,10801],{"class":189},[52,12406,250],{"class":102},[52,12408,2147],{"class":189},[52,12410,269],{"class":102},[52,12412,12413,12415],{"class":54,"line":73},[52,12414,1591],{"class":58},[52,12416,1330],{"class":102},[52,12418,12419,12421,12423,12425,12427,12429],{"class":54,"line":94},[52,12420,1599],{"class":241},[52,12422,193],{"class":102},[52,12424,115],{"class":83},[52,12426,11343],{"class":87},[52,12428,115],{"class":83},[52,12430,211],{"class":102},[639,12432,12434,12435,12437],{"id":12433},"forgetting-that-input-returns-a-string","Forgetting that ",[49,12436,10597],{}," returns a string",[14,12439,12440],{},"This is a very common issue.",[14,12442,12339],{},[42,12444,12446],{"className":44,"code":12445,"language":46,"meta":47,"style":47},"choice = input(\"Choose an option: \")\n\nif choice == 1:\n    print(\"Add contact\")\n",[49,12447,12448,12467,12471,12483],{"__ignoreMap":47},[52,12449,12450,12453,12455,12457,12459,12461,12463,12465],{"class":54,"line":55},[52,12451,12452],{"class":62},"choice ",[52,12454,80],{"class":79},[52,12456,11037],{"class":241},[52,12458,193],{"class":102},[52,12460,115],{"class":83},[52,12462,11044],{"class":87},[52,12464,115],{"class":83},[52,12466,211],{"class":102},[52,12468,12469],{"class":54,"line":66},[52,12470,70],{"emptyLinePlaceholder":69},[52,12472,12473,12475,12477,12479,12481],{"class":54,"line":73},[52,12474,1313],{"class":58},[52,12476,11059],{"class":62},[52,12478,11062],{"class":79},[52,12480,1518],{"class":121},[52,12482,1330],{"class":102},[52,12484,12485,12487,12489,12491,12494,12496],{"class":54,"line":94},[52,12486,1599],{"class":241},[52,12488,193],{"class":102},[52,12490,115],{"class":83},[52,12492,12493],{"class":87},"Add contact",[52,12495,115],{"class":83},[52,12497,211],{"class":102},[14,12499,12500,12501,12503],{},"This does not match because ",[49,12502,12230],{}," is a string, not a number.",[14,12505,12506],{},"Correct version:",[42,12508,12510],{"className":44,"code":12509,"language":46,"meta":47,"style":47},"if choice == \"1\":\n    print(\"Add contact\")\n",[49,12511,12512,12528],{"__ignoreMap":47},[52,12513,12514,12516,12518,12520,12522,12524,12526],{"class":54,"line":55},[52,12515,1313],{"class":58},[52,12517,11059],{"class":62},[52,12519,11062],{"class":79},[52,12521,84],{"class":83},[52,12523,9200],{"class":87},[52,12525,115],{"class":83},[52,12527,1330],{"class":102},[52,12529,12530,12532,12534,12536,12538,12540],{"class":54,"line":66},[52,12531,1599],{"class":241},[52,12533,193],{"class":102},[52,12535,115],{"class":83},[52,12537,12493],{"class":87},[52,12539,115],{"class":83},[52,12541,211],{"class":102},[14,12543,12544],{},"You can confirm this with:",[42,12546,12548],{"className":44,"code":12547,"language":46,"meta":47,"style":47},"print(type(choice))\n",[49,12549,12550],{"__ignoreMap":47},[52,12551,12552,12554,12556,12558,12560,12562],{"class":54,"line":55},[52,12553,242],{"class":241},[52,12555,193],{"class":102},[52,12557,4198],{"class":4733},[52,12559,193],{"class":102},[52,12561,12230],{"class":189},[52,12563,8886],{"class":102},[639,12565,12567,12568,12570],{"id":12566},"indentation-mistakes-inside-the-while-loop","Indentation mistakes inside the ",[49,12569,10772],{}," loop",[14,12572,12573],{},"Python uses indentation to define blocks of code.",[14,12575,12576],{},"For example, this is wrong:",[42,12578,12580],{"className":44,"code":12579,"language":46,"meta":47,"style":47},"while True:\nprint(\"Menu\")\n",[49,12581,12582,12590],{"__ignoreMap":47},[52,12583,12584,12586,12588],{"class":54,"line":55},[52,12585,10772],{"class":58},[52,12587,10943],{"class":160},[52,12589,1330],{"class":102},[52,12591,12592,12594,12596,12598,12601,12603],{"class":54,"line":66},[52,12593,242],{"class":241},[52,12595,193],{"class":102},[52,12597,115],{"class":83},[52,12599,12600],{"class":87},"Menu",[52,12602,115],{"class":83},[52,12604,211],{"class":102},[14,12606,12607],{},"This causes an indentation error.",[14,12609,12506],{},[42,12611,12613],{"className":44,"code":12612,"language":46,"meta":47,"style":47},"while True:\n    print(\"Menu\")\n",[49,12614,12615,12623],{"__ignoreMap":47},[52,12616,12617,12619,12621],{"class":54,"line":55},[52,12618,10772],{"class":58},[52,12620,10943],{"class":160},[52,12622,1330],{"class":102},[52,12624,12625,12627,12629,12631,12633,12635],{"class":54,"line":66},[52,12626,1599],{"class":241},[52,12628,193],{"class":102},[52,12630,115],{"class":83},[52,12632,12600],{"class":87},[52,12634,115],{"class":83},[52,12636,211],{"class":102},[14,12638,12639,12640,186],{},"If you see this problem, read ",[372,12641,8489],{"href":8488},[639,12643,12645],{"id":12644},"overwriting-a-contact-by-using-the-same-name-again","Overwriting a contact by using the same name again",[14,12647,12648],{},"This is valid Python behavior, but beginners sometimes do not expect it.",[14,12650,4259],{},[42,12652,12654],{"className":44,"code":12653,"language":46,"meta":47,"style":47},"contacts[\"Alice\"] = \"1111111111\"\ncontacts[\"Alice\"] = \"2222222222\"\n\nprint(contacts)\n",[49,12655,12656,12679,12702,12706],{"__ignoreMap":47},[52,12657,12658,12660,12662,12664,12666,12668,12670,12672,12674,12677],{"class":54,"line":55},[52,12659,10801],{"class":62},[52,12661,250],{"class":102},[52,12663,115],{"class":83},[52,12665,2156],{"class":87},[52,12667,115],{"class":83},[52,12669,11122],{"class":102},[52,12671,7038],{"class":79},[52,12673,84],{"class":83},[52,12675,12676],{"class":87},"1111111111",[52,12678,91],{"class":83},[52,12680,12681,12683,12685,12687,12689,12691,12693,12695,12697,12700],{"class":54,"line":66},[52,12682,10801],{"class":62},[52,12684,250],{"class":102},[52,12686,115],{"class":83},[52,12688,2156],{"class":87},[52,12690,115],{"class":83},[52,12692,11122],{"class":102},[52,12694,7038],{"class":79},[52,12696,84],{"class":83},[52,12698,12699],{"class":87},"2222222222",[52,12701,91],{"class":83},[52,12703,12704],{"class":54,"line":73},[52,12705,70],{"emptyLinePlaceholder":69},[52,12707,12708,12710,12712,12714],{"class":54,"line":94},[52,12709,242],{"class":241},[52,12711,193],{"class":102},[52,12713,10801],{"class":189},[52,12715,211],{"class":102},[14,12717,5267],{},[42,12719,12721],{"className":44,"code":12720,"language":46,"meta":47,"style":47},"{'Alice': '2222222222'}\n",[49,12722,12723],{"__ignoreMap":47},[52,12724,12725,12727,12729,12731,12733,12735,12737,12739,12741],{"class":54,"line":55},[52,12726,1482],{"class":102},[52,12728,4162],{"class":83},[52,12730,2156],{"class":87},[52,12732,4162],{"class":83},[52,12734,118],{"class":102},[52,12736,4172],{"class":83},[52,12738,12699],{"class":87},[52,12740,4162],{"class":83},[52,12742,167],{"class":102},[14,12744,12745],{},"The second value replaces the first one.",[639,12747,10294,12749,10598,12752],{"id":12748},"using-contactsitems-instead-of-contactsitems",[49,12750,12751],{},"contacts.items",[49,12753,11827],{},[14,12755,12756],{},"This is another common mistake.",[14,12758,8124],{},[42,12760,12762],{"className":44,"code":12761,"language":46,"meta":47,"style":47},"for name, phone in contacts.items:\n    print(name, phone)\n",[49,12763,12764,12784],{"__ignoreMap":47},[52,12765,12766,12768,12770,12772,12774,12776,12778,12780,12782],{"class":54,"line":55},[52,12767,12265],{"class":58},[52,12769,7019],{"class":62},[52,12771,199],{"class":102},[52,12773,11210],{"class":62},[52,12775,11213],{"class":58},[52,12777,11173],{"class":62},[52,12779,186],{"class":102},[52,12781,11220],{"class":1320},[52,12783,1330],{"class":102},[52,12785,12786,12788,12790,12792,12794,12796],{"class":54,"line":66},[52,12787,1599],{"class":241},[52,12789,193],{"class":102},[52,12791,2147],{"class":189},[52,12793,199],{"class":102},[52,12795,11244],{"class":189},[52,12797,211],{"class":102},[14,12799,12800],{},"Correct:",[42,12802,12804],{"className":44,"code":12803,"language":46,"meta":47,"style":47},"for name, phone in contacts.items():\n    print(name, phone)\n",[49,12805,12806,12826],{"__ignoreMap":47},[52,12807,12808,12810,12812,12814,12816,12818,12820,12822,12824],{"class":54,"line":55},[52,12809,12265],{"class":58},[52,12811,7019],{"class":62},[52,12813,199],{"class":102},[52,12815,11210],{"class":62},[52,12817,11213],{"class":58},[52,12819,11173],{"class":62},[52,12821,186],{"class":102},[52,12823,11220],{"class":189},[52,12825,8146],{"class":102},[52,12827,12828,12830,12832,12834,12836,12838],{"class":54,"line":66},[52,12829,1599],{"class":241},[52,12831,193],{"class":102},[52,12833,2147],{"class":189},[52,12835,199],{"class":102},[52,12837,11244],{"class":189},[52,12839,211],{"class":102},[14,12841,12842,12843,12846],{},"The parentheses are required because ",[49,12844,12845],{},"items()"," is a method.",[37,12848,1942],{"id":1941},[639,12850,12852],{"id":12851},"why-is-a-dictionary-used-for-this-project","Why is a dictionary used for this project?",[14,12854,12855],{},"A dictionary lets you look up a contact by name quickly and keeps the example simple.",[639,12857,12859],{"id":12858},"what-happens-if-i-add-the-same-name-twice","What happens if I add the same name twice?",[14,12861,12862],{},"The new phone number replaces the old value because dictionary keys must be unique.",[639,12864,12866],{"id":12865},"can-i-store-email-addresses-too","Can I store email addresses too?",[14,12868,12869],{},"Yes. A good next step is to store each contact as a nested dictionary with fields like phone and email.",[639,12871,12873],{"id":12872},"why-does-my-menu-choice-not-match","Why does my menu choice not match?",[14,12875,12876,12878,12879,12882,12883,186],{},[49,12877,10597],{}," returns text, so compare against string values like ",[49,12880,12881],{},"\"1\""," instead of the number ",[49,12884,9200],{},[639,12886,12888],{"id":12887},"how-can-i-save-contacts-after-the-program-closes","How can I save contacts after the program closes?",[14,12890,12891],{},"A good next improvement is saving the dictionary to a file, such as JSON.",[37,12893,2005],{"id":2004},[309,12895,12896,12900,12904,12909,12914,12920,12924],{},[24,12897,12898],{},[372,12899,10883],{"href":10882},[24,12901,12902],{},[372,12903,6876],{"href":6397},[24,12905,12906],{},[372,12907,12908],{"href":11834},"How to loop through a dictionary in Python",[24,12910,12911],{},[372,12912,12913],{"href":11972},"How to check if a key exists in a dictionary in Python",[24,12915,12916],{},[372,12917,10646,12918,7203],{"href":11838},[49,12919,11841],{},[24,12921,12922],{},[372,12923,1721],{"href":1720},[24,12925,12926],{},[372,12927,12929],{"href":12928},"\u002Fexamples\u002Fpython-to-do-list-script-example\u002F","Python to-do list script example",[14,12931,12932],{},"After you build the basic contact book, try improving it by adding email fields, duplicate checks, sorting, and file saving with JSON.",[2034,12934,12935],{},"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 .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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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":47,"searchDepth":66,"depth":66,"links":12937},[12938,12939,12940,12941,12944,12945,12946,12947,12948,12949,12950,12961,12968],{"id":10736,"depth":66,"text":10737},{"id":10757,"depth":66,"text":10758},{"id":10794,"depth":66,"text":10795},{"id":10886,"depth":66,"text":10887,"children":12942},[12943],{"id":6193,"depth":73,"text":6194},{"id":11567,"depth":66,"text":11568},{"id":11729,"depth":66,"text":11730},{"id":11854,"depth":66,"text":11855},{"id":11978,"depth":66,"text":11979},{"id":12097,"depth":66,"text":12098},{"id":12199,"depth":66,"text":12200},{"id":12321,"depth":66,"text":12322,"children":12951},[12952,12954,12956,12958,12959],{"id":12328,"depth":73,"text":12953},"Using contacts[name] before checking if the name exists",{"id":12433,"depth":73,"text":12955},"Forgetting that input() returns a string",{"id":12566,"depth":73,"text":12957},"Indentation mistakes inside the while loop",{"id":12644,"depth":73,"text":12645},{"id":12748,"depth":73,"text":12960},"Using contacts.items instead of contacts.items()",{"id":1941,"depth":66,"text":1942,"children":12962},[12963,12964,12965,12966,12967],{"id":12851,"depth":73,"text":12852},{"id":12858,"depth":73,"text":12859},{"id":12865,"depth":73,"text":12866},{"id":12872,"depth":73,"text":12873},{"id":12887,"depth":73,"text":12888},{"id":2004,"depth":66,"text":2005},"Master python contact book example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-contact-book-example",{"title":10702,"description":12969},"examples\u002Fpython-contact-book-example","q5NNl2Lrh33VN1ZLB4J3uifWOSWE5eVcf2FQRre5C1I",{"id":12976,"title":12977,"body":12978,"description":14572,"extension":2075,"meta":14573,"navigation":69,"path":14574,"seo":14575,"stem":14576,"__hash__":14577},"content\u002Fexamples\u002Fpython-countdown-timer-example.md","Python Countdown Timer Example",{"type":7,"value":12979,"toc":14525},[12980,12983,12989,12992,13003,13006,13008,13108,13111,13113,13116,13127,13130,13149,13153,13156,13159,13173,13176,13187,13191,13193,13281,13284,13309,13313,13316,13322,13333,13339,13346,13352,13365,13368,13374,13380,13397,13399,13412,13420,13427,13433,13448,13451,13456,13475,13478,13481,13487,13500,13506,13509,13526,13531,13535,13554,13557,13560,13564,13567,13573,13576,13580,13583,13688,13692,13709,13717,13720,13738,13742,13748,13775,13781,13783,13809,13816,13820,13823,13840,13843,13952,13955,14059,14061,14064,14070,14081,14083,14144,14147,14157,14165,14170,14172,14196,14199,14201,14228,14232,14235,14247,14250,14254,14257,14273,14276,14283,14288,14292,14295,14299,14313,14316,14320,14340,14343,14347,14367,14373,14377,14380,14404,14408,14411,14443,14445,14449,14452,14457,14466,14472,14481,14485,14488,14490,14519,14522],[10,12981,12977],{"id":12982},"python-countdown-timer-example",[14,12984,12985,12986,186],{},"A countdown timer is a great beginner Python project. It helps you practice variables, loops, and pauses with ",[49,12987,12988],{},"sleep()",[14,12990,12991],{},"This example shows how to build a simple countdown timer that:",[309,12993,12994,12997,13000],{},[24,12995,12996],{},"counts down one number at a time",[24,12998,12999],{},"waits 1 second between numbers",[24,13001,13002],{},"prints a final message when it finishes",[14,13004,13005],{},"Start with the basic version first. Once it works, you can safely change the starting number or add user input.",[37,13007,40],{"id":39},[42,13009,13011],{"className":44,"code":13010,"language":46,"meta":47,"style":47},"import time\n\nseconds = 5\n\nwhile seconds > 0:\n    print(seconds)\n    time.sleep(1)\n    seconds -= 1\n\nprint(\"Time's up!\")\n",[49,13012,13013,13020,13024,13033,13037,13052,13063,13079,13089,13093],{"__ignoreMap":47},[52,13014,13015,13017],{"class":54,"line":55},[52,13016,59],{"class":58},[52,13018,13019],{"class":62}," time\n",[52,13021,13022],{"class":54,"line":66},[52,13023,70],{"emptyLinePlaceholder":69},[52,13025,13026,13029,13031],{"class":54,"line":73},[52,13027,13028],{"class":62},"seconds ",[52,13030,80],{"class":79},[52,13032,5264],{"class":121},[52,13034,13035],{"class":54,"line":94},[52,13036,70],{"emptyLinePlaceholder":69},[52,13038,13039,13041,13044,13047,13050],{"class":54,"line":106},[52,13040,10772],{"class":58},[52,13042,13043],{"class":62}," seconds ",[52,13045,13046],{"class":79},">",[52,13048,13049],{"class":121}," 0",[52,13051,1330],{"class":102},[52,13053,13054,13056,13058,13061],{"class":54,"line":128},[52,13055,1599],{"class":241},[52,13057,193],{"class":102},[52,13059,13060],{"class":189},"seconds",[52,13062,211],{"class":102},[52,13064,13065,13068,13070,13073,13075,13077],{"class":54,"line":148},[52,13066,13067],{"class":62},"    time",[52,13069,186],{"class":102},[52,13071,13072],{"class":189},"sleep",[52,13074,193],{"class":102},[52,13076,9200],{"class":121},[52,13078,211],{"class":102},[52,13080,13081,13084,13087],{"class":54,"line":164},[52,13082,13083],{"class":62},"    seconds ",[52,13085,13086],{"class":79},"-=",[52,13088,8645],{"class":121},[52,13090,13091],{"class":54,"line":170},[52,13092,70],{"emptyLinePlaceholder":69},[52,13094,13095,13097,13099,13101,13104,13106],{"class":54,"line":175},[52,13096,242],{"class":241},[52,13098,193],{"class":102},[52,13100,115],{"class":83},[52,13102,13103],{"class":87},"Time's up!",[52,13105,115],{"class":83},[52,13107,211],{"class":102},[14,13109,13110],{},"This is the fastest working version. It counts down from 5 to 1, waits 1 second between numbers, then prints a finish message.",[37,13112,9295],{"id":9294},[14,13114,13115],{},"This script creates:",[309,13117,13118,13121,13124],{},[24,13119,13120],{},"A countdown that prints one number at a time",[24,13122,13123],{},"A pause of 1 second between each number",[24,13125,13126],{},"A final message when the countdown ends",[14,13128,13129],{},"It is a good first project for practicing:",[309,13131,13132,13135,13140],{},[24,13133,13134],{},"variables",[24,13136,13137,13139],{},[49,13138,10772],{}," loops",[24,13141,6728,13142],{},[372,13143,13145,13148],{"href":13144},"\u002Fstandard-library\u002Ftime.sleep-function-explained",[49,13146,13147],{},"time.sleep()"," function",[37,13150,13152],{"id":13151},"before-you-start","Before you start",[14,13154,13155],{},"You do not need much to follow this example.",[14,13157,13158],{},"It helps if you already know:",[309,13160,13161,13164,13170],{},[24,13162,13163],{},"basic variables",[24,13165,13166,13167,13169],{},"the idea of a ",[49,13168,10772],{}," loop, or are willing to follow one simple loop",[24,13171,13172],{},"how to run a Python file",[14,13174,13175],{},"You also need Python installed.",[14,13177,13178,13179,13182,13183,186],{},"This example uses Python’s built-in ",[49,13180,13181],{},"time"," module. You can learn more in this ",[372,13184,13186],{"href":13185},"\u002Fstandard-library\u002Fpython-time-module-overview\u002F","Python time module overview",[37,13188,13190],{"id":13189},"simple-countdown-timer-example","Simple countdown timer example",[14,13192,5591],{},[42,13194,13195],{"className":44,"code":13010,"language":46,"meta":47,"style":47},[49,13196,13197,13203,13207,13215,13219,13231,13241,13255,13263,13267],{"__ignoreMap":47},[52,13198,13199,13201],{"class":54,"line":55},[52,13200,59],{"class":58},[52,13202,13019],{"class":62},[52,13204,13205],{"class":54,"line":66},[52,13206,70],{"emptyLinePlaceholder":69},[52,13208,13209,13211,13213],{"class":54,"line":73},[52,13210,13028],{"class":62},[52,13212,80],{"class":79},[52,13214,5264],{"class":121},[52,13216,13217],{"class":54,"line":94},[52,13218,70],{"emptyLinePlaceholder":69},[52,13220,13221,13223,13225,13227,13229],{"class":54,"line":106},[52,13222,10772],{"class":58},[52,13224,13043],{"class":62},[52,13226,13046],{"class":79},[52,13228,13049],{"class":121},[52,13230,1330],{"class":102},[52,13232,13233,13235,13237,13239],{"class":54,"line":128},[52,13234,1599],{"class":241},[52,13236,193],{"class":102},[52,13238,13060],{"class":189},[52,13240,211],{"class":102},[52,13242,13243,13245,13247,13249,13251,13253],{"class":54,"line":148},[52,13244,13067],{"class":62},[52,13246,186],{"class":102},[52,13248,13072],{"class":189},[52,13250,193],{"class":102},[52,13252,9200],{"class":121},[52,13254,211],{"class":102},[52,13256,13257,13259,13261],{"class":54,"line":164},[52,13258,13083],{"class":62},[52,13260,13086],{"class":79},[52,13262,8645],{"class":121},[52,13264,13265],{"class":54,"line":170},[52,13266,70],{"emptyLinePlaceholder":69},[52,13268,13269,13271,13273,13275,13277,13279],{"class":54,"line":175},[52,13270,242],{"class":241},[52,13272,193],{"class":102},[52,13274,115],{"class":83},[52,13276,13103],{"class":87},[52,13278,115],{"class":83},[52,13280,211],{"class":102},[14,13282,13283],{},"This version works like this:",[309,13285,13286,13289,13295,13298,13301,13306],{},[24,13287,13288],{},"Start with a number stored in a variable",[24,13290,13291,13292],{},"Keep looping while the number is greater than ",[49,13293,13294],{},"0",[24,13296,13297],{},"Print the current number",[24,13299,13300],{},"Pause for 1 second",[24,13302,13303,13304],{},"Subtract ",[49,13305,9200],{},[24,13307,13308],{},"Print a final message after the loop ends",[37,13310,13312],{"id":13311},"how-the-code-works","How the code works",[14,13314,13315],{},"Let’s go through it line by line.",[639,13317,13319],{"id":13318},"import-time",[49,13320,13321],{},"import time",[42,13323,13325],{"className":44,"code":13324,"language":46,"meta":47,"style":47},"import time\n",[49,13326,13327],{"__ignoreMap":47},[52,13328,13329,13331],{"class":54,"line":55},[52,13330,59],{"class":58},[52,13332,13019],{"class":62},[14,13334,13335,13336,13338],{},"This gives your program access to the ",[49,13337,13181],{}," module.",[14,13340,13341,13342,13345],{},"Without this line, ",[49,13343,13344],{},"time.sleep(1)"," will not work.",[639,13347,13349],{"id":13348},"seconds-5",[49,13350,13351],{},"seconds = 5",[42,13353,13355],{"className":44,"code":13354,"language":46,"meta":47,"style":47},"seconds = 5\n",[49,13356,13357],{"__ignoreMap":47},[52,13358,13359,13361,13363],{"class":54,"line":55},[52,13360,13028],{"class":62},[52,13362,80],{"class":79},[52,13364,5264],{"class":121},[14,13366,13367],{},"This sets the starting value for the countdown.",[14,13369,13370,13371,13373],{},"If you change it to ",[49,13372,3373],{},", the timer will count down from 10 instead.",[639,13375,13377],{"id":13376},"while-seconds-0",[49,13378,13379],{},"while seconds > 0",[42,13381,13383],{"className":44,"code":13382,"language":46,"meta":47,"style":47},"while seconds > 0:\n",[49,13384,13385],{"__ignoreMap":47},[52,13386,13387,13389,13391,13393,13395],{"class":54,"line":55},[52,13388,10772],{"class":58},[52,13390,13043],{"class":62},[52,13392,13046],{"class":79},[52,13394,13049],{"class":121},[52,13396,1330],{"class":102},[14,13398,6218],{},[309,13400,13401,13404],{},[24,13402,13403],{},"keep running the loop",[24,13405,13406,13407,13409,13410],{},"only while ",[49,13408,13060],{}," is greater than ",[49,13411,13294],{},[14,13413,13414,13415,6230,13417,13419],{},"When ",[49,13416,13060],{},[49,13418,13294],{},", the loop stops.",[14,13421,13422,13423,186],{},"If you are new to loops, see ",[372,13424,13426],{"href":13425},"\u002Flearn\u002Fpython-while-loops-explained\u002F","Python while loops explained",[639,13428,13430],{"id":13429},"printseconds",[49,13431,13432],{},"print(seconds)",[42,13434,13436],{"className":44,"code":13435,"language":46,"meta":47,"style":47},"print(seconds)\n",[49,13437,13438],{"__ignoreMap":47},[52,13439,13440,13442,13444,13446],{"class":54,"line":55},[52,13441,242],{"class":241},[52,13443,193],{"class":102},[52,13445,13060],{"class":189},[52,13447,211],{"class":102},[14,13449,13450],{},"This shows the current countdown number on the screen.",[639,13452,13454],{"id":13453},"timesleep1",[49,13455,13344],{},[42,13457,13459],{"className":44,"code":13458,"language":46,"meta":47,"style":47},"time.sleep(1)\n",[49,13460,13461],{"__ignoreMap":47},[52,13462,13463,13465,13467,13469,13471,13473],{"class":54,"line":55},[52,13464,13181],{"class":62},[52,13466,186],{"class":102},[52,13468,13072],{"class":189},[52,13470,193],{"class":102},[52,13472,9200],{"class":121},[52,13474,211],{"class":102},[14,13476,13477],{},"This pauses the program for 1 second.",[14,13479,13480],{},"It does not print anything by itself. It only makes the program wait.",[639,13482,13484],{"id":13483},"seconds-1",[49,13485,13486],{},"seconds -= 1",[42,13488,13490],{"className":44,"code":13489,"language":46,"meta":47,"style":47},"seconds -= 1\n",[49,13491,13492],{"__ignoreMap":47},[52,13493,13494,13496,13498],{"class":54,"line":55},[52,13495,13028],{"class":62},[52,13497,13086],{"class":79},[52,13499,8645],{"class":121},[14,13501,13502,13503,13505],{},"This reduces ",[49,13504,13060],{}," by 1 each time through the loop.",[14,13507,13508],{},"It is the same as writing:",[42,13510,13512],{"className":44,"code":13511,"language":46,"meta":47,"style":47},"seconds = seconds - 1\n",[49,13513,13514],{"__ignoreMap":47},[52,13515,13516,13518,13520,13522,13524],{"class":54,"line":55},[52,13517,13028],{"class":62},[52,13519,80],{"class":79},[52,13521,13043],{"class":62},[52,13523,5889],{"class":79},[52,13525,8645],{"class":121},[14,13527,13528,13529,186],{},"This line is very important. Without it, the countdown would never move toward ",[49,13530,13294],{},[639,13532,13534],{"id":13533},"final-message","Final message",[42,13536,13538],{"className":44,"code":13537,"language":46,"meta":47,"style":47},"print(\"Time's up!\")\n",[49,13539,13540],{"__ignoreMap":47},[52,13541,13542,13544,13546,13548,13550,13552],{"class":54,"line":55},[52,13543,242],{"class":241},[52,13545,193],{"class":102},[52,13547,115],{"class":83},[52,13549,13103],{"class":87},[52,13551,115],{"class":83},[52,13553,211],{"class":102},[14,13555,13556],{},"This line runs only after the loop finishes.",[14,13558,13559],{},"That is why you see the message at the end, not during the countdown.",[37,13561,13563],{"id":13562},"expected-output","Expected output",[14,13565,13566],{},"When you run the script, you should see something like this:",[42,13568,13571],{"className":13569,"code":13570,"language":955,"meta":47},[953],"5\n4\n3\n2\n1\nTime's up!\n",[49,13572,13570],{"__ignoreMap":47},[14,13574,13575],{},"There will be a 1-second pause between each number.",[37,13577,13579],{"id":13578},"version-that-gets-user-input","Version that gets user input",[14,13581,13582],{},"You can also ask the user for the starting number.",[42,13584,13586],{"className":44,"code":13585,"language":46,"meta":47,"style":47},"import time\n\nseconds = int(input(\"Enter the number of seconds: \"))\n\nwhile seconds > 0:\n    print(seconds)\n    time.sleep(1)\n    seconds -= 1\n\nprint(\"Time's up!\")\n",[49,13587,13588,13594,13598,13622,13626,13638,13648,13662,13670,13674],{"__ignoreMap":47},[52,13589,13590,13592],{"class":54,"line":55},[52,13591,59],{"class":58},[52,13593,13019],{"class":62},[52,13595,13596],{"class":54,"line":66},[52,13597,70],{"emptyLinePlaceholder":69},[52,13599,13600,13602,13604,13606,13608,13611,13613,13615,13618,13620],{"class":54,"line":73},[52,13601,13028],{"class":62},[52,13603,80],{"class":79},[52,13605,9789],{"class":4733},[52,13607,193],{"class":102},[52,13609,13610],{"class":241},"input",[52,13612,193],{"class":102},[52,13614,115],{"class":83},[52,13616,13617],{"class":87},"Enter the number of seconds: ",[52,13619,115],{"class":83},[52,13621,8886],{"class":102},[52,13623,13624],{"class":54,"line":94},[52,13625,70],{"emptyLinePlaceholder":69},[52,13627,13628,13630,13632,13634,13636],{"class":54,"line":106},[52,13629,10772],{"class":58},[52,13631,13043],{"class":62},[52,13633,13046],{"class":79},[52,13635,13049],{"class":121},[52,13637,1330],{"class":102},[52,13639,13640,13642,13644,13646],{"class":54,"line":128},[52,13641,1599],{"class":241},[52,13643,193],{"class":102},[52,13645,13060],{"class":189},[52,13647,211],{"class":102},[52,13649,13650,13652,13654,13656,13658,13660],{"class":54,"line":148},[52,13651,13067],{"class":62},[52,13653,186],{"class":102},[52,13655,13072],{"class":189},[52,13657,193],{"class":102},[52,13659,9200],{"class":121},[52,13661,211],{"class":102},[52,13663,13664,13666,13668],{"class":54,"line":164},[52,13665,13083],{"class":62},[52,13667,13086],{"class":79},[52,13669,8645],{"class":121},[52,13671,13672],{"class":54,"line":170},[52,13673,70],{"emptyLinePlaceholder":69},[52,13675,13676,13678,13680,13682,13684,13686],{"class":54,"line":175},[52,13677,242],{"class":241},[52,13679,193],{"class":102},[52,13681,115],{"class":83},[52,13683,13103],{"class":87},[52,13685,115],{"class":83},[52,13687,211],{"class":102},[639,13689,13691],{"id":13690},"how-this-version-is-different","How this version is different",[309,13693,13694,13699,13704],{},[24,13695,13696,13698],{},[49,13697,10597],{}," asks the user to type something",[24,13700,13701,13703],{},[49,13702,10597],{}," returns text",[24,13705,13706,13708],{},[49,13707,9492],{}," converts that text to a whole number",[14,13710,13711,13712,13714,13715,186],{},"So if the user types ",[49,13713,9479],{},", Python turns it into the number ",[49,13716,9479],{},[14,13718,13719],{},"If you want to learn more, see:",[309,13721,13722,13727,13732],{},[24,13723,13724],{},[372,13725,13726],{"href":10606},"Python input() function explained",[24,13728,13729],{},[372,13730,13731],{"href":9489},"Python int() function explained",[24,13733,13734],{},[372,13735,13737],{"href":13736},"\u002Fhow-to\u002Fhow-to-convert-user-input-to-numbers-in-python\u002F","How to convert user input to numbers in Python",[639,13739,13741],{"id":13740},"important-note-about-bad-input","Important note about bad input",[14,13743,13744,13745,13747],{},"If the user types something like ",[49,13746,6363],{}," instead of a number, this line will fail:",[42,13749,13751],{"className":44,"code":13750,"language":46,"meta":47,"style":47},"seconds = int(input(\"Enter the number of seconds: \"))\n",[49,13752,13753],{"__ignoreMap":47},[52,13754,13755,13757,13759,13761,13763,13765,13767,13769,13771,13773],{"class":54,"line":55},[52,13756,13028],{"class":62},[52,13758,80],{"class":79},[52,13760,9789],{"class":4733},[52,13762,193],{"class":102},[52,13764,13610],{"class":241},[52,13766,193],{"class":102},[52,13768,115],{"class":83},[52,13770,13617],{"class":87},[52,13772,115],{"class":83},[52,13774,8886],{"class":102},[14,13776,13777,13778,186],{},"That raises a ",[49,13779,13780],{},"ValueError",[14,13782,4259],{},[42,13784,13785],{"className":44,"code":13750,"language":46,"meta":47,"style":47},[49,13786,13787],{"__ignoreMap":47},[52,13788,13789,13791,13793,13795,13797,13799,13801,13803,13805,13807],{"class":54,"line":55},[52,13790,13028],{"class":62},[52,13792,80],{"class":79},[52,13794,9789],{"class":4733},[52,13796,193],{"class":102},[52,13798,13610],{"class":241},[52,13800,193],{"class":102},[52,13802,115],{"class":83},[52,13804,13617],{"class":87},[52,13806,115],{"class":83},[52,13808,8886],{"class":102},[14,13810,13811,13812,186],{},"If the input is not a valid whole number, read ",[372,13813,3984,13814],{"href":6471},[49,13815,6472],{},[37,13817,13819],{"id":13818},"small-improvements-beginners-can-try","Small improvements beginners can try",[14,13821,13822],{},"Once the basic version works, try small changes like these:",[309,13824,13825,13828,13831,13834,13837],{},[24,13826,13827],{},"Change the finish message",[24,13829,13830],{},"Start from a different number",[24,13832,13833],{},"Add a message before the countdown starts",[24,13835,13836],{},"Wrap the code in a function",[24,13838,13839],{},"Build a later version that shows minutes and seconds",[14,13841,13842],{},"Example with a start message:",[42,13844,13846],{"className":44,"code":13845,"language":46,"meta":47,"style":47},"import time\n\nseconds = 5\n\nprint(\"Starting countdown...\")\n\nwhile seconds > 0:\n    print(seconds)\n    time.sleep(1)\n    seconds -= 1\n\nprint(\"Done!\")\n",[49,13847,13848,13854,13858,13866,13870,13885,13889,13901,13911,13925,13933,13937],{"__ignoreMap":47},[52,13849,13850,13852],{"class":54,"line":55},[52,13851,59],{"class":58},[52,13853,13019],{"class":62},[52,13855,13856],{"class":54,"line":66},[52,13857,70],{"emptyLinePlaceholder":69},[52,13859,13860,13862,13864],{"class":54,"line":73},[52,13861,13028],{"class":62},[52,13863,80],{"class":79},[52,13865,5264],{"class":121},[52,13867,13868],{"class":54,"line":94},[52,13869,70],{"emptyLinePlaceholder":69},[52,13871,13872,13874,13876,13878,13881,13883],{"class":54,"line":106},[52,13873,242],{"class":241},[52,13875,193],{"class":102},[52,13877,115],{"class":83},[52,13879,13880],{"class":87},"Starting countdown...",[52,13882,115],{"class":83},[52,13884,211],{"class":102},[52,13886,13887],{"class":54,"line":128},[52,13888,70],{"emptyLinePlaceholder":69},[52,13890,13891,13893,13895,13897,13899],{"class":54,"line":148},[52,13892,10772],{"class":58},[52,13894,13043],{"class":62},[52,13896,13046],{"class":79},[52,13898,13049],{"class":121},[52,13900,1330],{"class":102},[52,13902,13903,13905,13907,13909],{"class":54,"line":164},[52,13904,1599],{"class":241},[52,13906,193],{"class":102},[52,13908,13060],{"class":189},[52,13910,211],{"class":102},[52,13912,13913,13915,13917,13919,13921,13923],{"class":54,"line":170},[52,13914,13067],{"class":62},[52,13916,186],{"class":102},[52,13918,13072],{"class":189},[52,13920,193],{"class":102},[52,13922,9200],{"class":121},[52,13924,211],{"class":102},[52,13926,13927,13929,13931],{"class":54,"line":175},[52,13928,13083],{"class":62},[52,13930,13086],{"class":79},[52,13932,8645],{"class":121},[52,13934,13935],{"class":54,"line":214},[52,13936,70],{"emptyLinePlaceholder":69},[52,13938,13939,13941,13943,13945,13948,13950],{"class":54,"line":233},[52,13940,242],{"class":241},[52,13942,193],{"class":102},[52,13944,115],{"class":83},[52,13946,13947],{"class":87},"Done!",[52,13949,115],{"class":83},[52,13951,211],{"class":102},[14,13953,13954],{},"Example wrapped in a function:",[42,13956,13958],{"className":44,"code":13957,"language":46,"meta":47,"style":47},"import time\n\ndef countdown(seconds):\n    while seconds > 0:\n        print(seconds)\n        time.sleep(1)\n        seconds -= 1\n    print(\"Time's up!\")\n\ncountdown(5)\n",[49,13959,13960,13966,13970,13983,13996,14006,14021,14030,14044,14048],{"__ignoreMap":47},[52,13961,13962,13964],{"class":54,"line":55},[52,13963,59],{"class":58},[52,13965,13019],{"class":62},[52,13967,13968],{"class":54,"line":66},[52,13969,70],{"emptyLinePlaceholder":69},[52,13971,13972,13974,13977,13979,13981],{"class":54,"line":73},[52,13973,7410],{"class":1475},[52,13975,13976],{"class":7065}," countdown",[52,13978,193],{"class":102},[52,13980,13060],{"class":7018},[52,13982,3114],{"class":102},[52,13984,13985,13988,13990,13992,13994],{"class":54,"line":94},[52,13986,13987],{"class":58},"    while",[52,13989,13043],{"class":62},[52,13991,13046],{"class":79},[52,13993,13049],{"class":121},[52,13995,1330],{"class":102},[52,13997,13998,14000,14002,14004],{"class":54,"line":106},[52,13999,1470],{"class":241},[52,14001,193],{"class":102},[52,14003,13060],{"class":189},[52,14005,211],{"class":102},[52,14007,14008,14011,14013,14015,14017,14019],{"class":54,"line":128},[52,14009,14010],{"class":62},"        time",[52,14012,186],{"class":102},[52,14014,13072],{"class":189},[52,14016,193],{"class":102},[52,14018,9200],{"class":121},[52,14020,211],{"class":102},[52,14022,14023,14026,14028],{"class":54,"line":148},[52,14024,14025],{"class":62},"        seconds ",[52,14027,13086],{"class":79},[52,14029,8645],{"class":121},[52,14031,14032,14034,14036,14038,14040,14042],{"class":54,"line":164},[52,14033,1599],{"class":241},[52,14035,193],{"class":102},[52,14037,115],{"class":83},[52,14039,13103],{"class":87},[52,14041,115],{"class":83},[52,14043,211],{"class":102},[52,14045,14046],{"class":54,"line":170},[52,14047,70],{"emptyLinePlaceholder":69},[52,14049,14050,14053,14055,14057],{"class":54,"line":175},[52,14051,14052],{"class":189},"countdown",[52,14054,193],{"class":102},[52,14056,9479],{"class":121},[52,14058,211],{"class":102},[37,14060,6402],{"id":6401},[14,14062,14063],{},"These are the most common problems with simple countdown timers.",[639,14065,14067,14068],{"id":14066},"forgetting-to-import-time","Forgetting to import ",[49,14069,13181],{},[14,14071,6446,14072,14074,14075,14077,14078,186],{},[49,14073,13344],{}," without importing ",[49,14076,13181],{},", Python will raise a ",[49,14079,14080],{},"NameError",[14,14082,8124],{},[42,14084,14086],{"className":44,"code":14085,"language":46,"meta":47,"style":47},"seconds = 5\n\nwhile seconds > 0:\n    print(seconds)\n    time.sleep(1)\n    seconds -= 1\n",[49,14087,14088,14096,14100,14112,14122,14136],{"__ignoreMap":47},[52,14089,14090,14092,14094],{"class":54,"line":55},[52,14091,13028],{"class":62},[52,14093,80],{"class":79},[52,14095,5264],{"class":121},[52,14097,14098],{"class":54,"line":66},[52,14099,70],{"emptyLinePlaceholder":69},[52,14101,14102,14104,14106,14108,14110],{"class":54,"line":73},[52,14103,10772],{"class":58},[52,14105,13043],{"class":62},[52,14107,13046],{"class":79},[52,14109,13049],{"class":121},[52,14111,1330],{"class":102},[52,14113,14114,14116,14118,14120],{"class":54,"line":94},[52,14115,1599],{"class":241},[52,14117,193],{"class":102},[52,14119,13060],{"class":189},[52,14121,211],{"class":102},[52,14123,14124,14126,14128,14130,14132,14134],{"class":54,"line":106},[52,14125,13067],{"class":62},[52,14127,186],{"class":102},[52,14129,13072],{"class":189},[52,14131,193],{"class":102},[52,14133,9200],{"class":121},[52,14135,211],{"class":102},[52,14137,14138,14140,14142],{"class":54,"line":128},[52,14139,13083],{"class":62},[52,14141,13086],{"class":79},[52,14143,8645],{"class":121},[14,14145,14146],{},"Fix:",[42,14148,14149],{"className":44,"code":13324,"language":46,"meta":47,"style":47},[49,14150,14151],{"__ignoreMap":47},[52,14152,14153,14155],{"class":54,"line":55},[52,14154,59],{"class":58},[52,14156,13019],{"class":62},[639,14158,10294,14160,14162,14163],{"id":14159},"using-input-without-int",[49,14161,10597],{}," without ",[49,14164,9492],{},[14,14166,14167,14169],{},[49,14168,10597],{}," returns text, not a number.",[14,14171,8124],{},[42,14173,14175],{"className":44,"code":14174,"language":46,"meta":47,"style":47},"seconds = input(\"Enter seconds: \")\n",[49,14176,14177],{"__ignoreMap":47},[52,14178,14179,14181,14183,14185,14187,14189,14192,14194],{"class":54,"line":55},[52,14180,13028],{"class":62},[52,14182,80],{"class":79},[52,14184,11037],{"class":241},[52,14186,193],{"class":102},[52,14188,115],{"class":83},[52,14190,14191],{"class":87},"Enter seconds: ",[52,14193,115],{"class":83},[52,14195,211],{"class":102},[14,14197,14198],{},"If you try to compare that text with numbers in a loop, your program may fail or behave incorrectly.",[14,14200,14146],{},[42,14202,14204],{"className":44,"code":14203,"language":46,"meta":47,"style":47},"seconds = int(input(\"Enter seconds: \"))\n",[49,14205,14206],{"__ignoreMap":47},[52,14207,14208,14210,14212,14214,14216,14218,14220,14222,14224,14226],{"class":54,"line":55},[52,14209,13028],{"class":62},[52,14211,80],{"class":79},[52,14213,9789],{"class":4733},[52,14215,193],{"class":102},[52,14217,13610],{"class":241},[52,14219,193],{"class":102},[52,14221,115],{"class":83},[52,14223,14191],{"class":87},[52,14225,115],{"class":83},[52,14227,8886],{"class":102},[639,14229,14231],{"id":14230},"forgetting-to-decrease-the-counter","Forgetting to decrease the counter",[14,14233,14234],{},"If you leave out this line:",[42,14236,14237],{"className":44,"code":13489,"language":46,"meta":47,"style":47},[49,14238,14239],{"__ignoreMap":47},[52,14240,14241,14243,14245],{"class":54,"line":55},[52,14242,13028],{"class":62},[52,14244,13086],{"class":79},[52,14246,8645],{"class":121},[14,14248,14249],{},"the value never changes, so the loop may run forever.",[639,14251,14253],{"id":14252},"using-the-wrong-loop-condition","Using the wrong loop condition",[14,14255,14256],{},"This line controls when the countdown stops:",[42,14258,14259],{"className":44,"code":13382,"language":46,"meta":47,"style":47},[49,14260,14261],{"__ignoreMap":47},[52,14262,14263,14265,14267,14269,14271],{"class":54,"line":55},[52,14264,10772],{"class":58},[52,14266,13043],{"class":62},[52,14268,13046],{"class":79},[52,14270,13049],{"class":121},[52,14272,1330],{"class":102},[14,14274,14275],{},"If the condition is wrong, the timer may stop too early or never stop.",[639,14277,14279,14280,14282],{"id":14278},"expecting-sleep-to-print-something","Expecting ",[49,14281,12988],{}," to print something",[14,14284,14285,14287],{},[49,14286,13344],{}," only pauses the program. It does not display output.",[37,14289,14291],{"id":14290},"debugging-tips","Debugging tips",[14,14293,14294],{},"If your countdown is not working, try these simple checks.",[639,14296,14298],{"id":14297},"print-the-counter-value-each-loop","Print the counter value each loop",[42,14300,14301],{"className":44,"code":13435,"language":46,"meta":47,"style":47},[49,14302,14303],{"__ignoreMap":47},[52,14304,14305,14307,14309,14311],{"class":54,"line":55},[52,14306,242],{"class":241},[52,14308,193],{"class":102},[52,14310,13060],{"class":189},[52,14312,211],{"class":102},[14,14314,14315],{},"This helps you confirm that the value is changing.",[639,14317,14319],{"id":14318},"print-a-message-inside-the-loop","Print a message inside the loop",[42,14321,14323],{"className":44,"code":14322,"language":46,"meta":47,"style":47},"print(\"loop running\")\n",[49,14324,14325],{"__ignoreMap":47},[52,14326,14327,14329,14331,14333,14336,14338],{"class":54,"line":55},[52,14328,242],{"class":241},[52,14330,193],{"class":102},[52,14332,115],{"class":83},[52,14334,14335],{"class":87},"loop running",[52,14337,115],{"class":83},[52,14339,211],{"class":102},[14,14341,14342],{},"If this prints forever, your loop condition or counter update is probably wrong.",[639,14344,14346],{"id":14345},"check-the-type-of-user-input","Check the type of user input",[42,14348,14350],{"className":44,"code":14349,"language":46,"meta":47,"style":47},"print(type(user_input))\n",[49,14351,14352],{"__ignoreMap":47},[52,14353,14354,14356,14358,14360,14362,14365],{"class":54,"line":55},[52,14355,242],{"class":241},[52,14357,193],{"class":102},[52,14359,4198],{"class":4733},[52,14361,193],{"class":102},[52,14363,14364],{"class":189},"user_input",[52,14366,8886],{"class":102},[14,14368,14369,14370,14372],{},"This helps when you are working with ",[49,14371,10597],{}," and want to see whether you still have text instead of a number.",[639,14374,14376],{"id":14375},"problems-and-likely-causes","Problems and likely causes",[14,14378,14379],{},"Common causes include:",[309,14381,14382,14391,14398,14401],{},[24,14383,14384,1694,14386,14388,14389],{},[49,14385,14080],{},[49,14387,13181],{}," was used without ",[49,14390,13321],{},[24,14392,14393,14395,14396],{},[49,14394,13780],{}," when converting non-number text with ",[49,14397,9492],{},[24,14399,14400],{},"an infinite loop because the counter is never decreased",[24,14402,14403],{},"the wrong countdown length because the starting value is not what the user expected",[37,14405,14407],{"id":14406},"next-steps","Next steps",[14,14409,14410],{},"After you get this version working, good next steps are:",[309,14412,14413,14419,14428,14431],{},[24,14414,14415,14416],{},"learn more about ",[372,14417,14418],{"href":13425},"Python while loops",[24,14420,14421,14422,759,14426],{},"practice with ",[372,14423,14424],{"href":10606},[49,14425,10597],{},[49,14427,9492],{},[24,14429,14430],{},"try a timer that uses minutes and seconds",[24,14432,14433,14434,14438,14439],{},"build another small script like a ",[372,14435,14437],{"href":14436},"\u002Fexamples\u002Fpython-timer-script-example\u002F","Python timer script example"," or a ",[372,14440,14442],{"href":14441},"\u002Fexamples\u002Fpython-stopwatch-example\u002F","Python stopwatch example",[37,14444,1942],{"id":1941},[639,14446,14448],{"id":14447},"why-does-my-countdown-timer-not-stop","Why does my countdown timer not stop?",[14,14450,14451],{},"Usually the counter is not being decreased inside the loop, or the loop condition is wrong.",[639,14453,8966,14455,1995],{"id":14454},"why-do-i-need-import-time",[49,14456,13321],{},[14,14458,14459,14460,14462,14463,14465],{},"Because ",[49,14461,12988],{}," is part of Python’s ",[49,14464,13181],{}," module. It is not available automatically by itself.",[639,14467,3677,14469,14471],{"id":14468},"why-does-input-cause-an-error",[49,14470,10597],{}," cause an error?",[14,14473,14474,14476,14477,14480],{},[49,14475,10597],{}," returns text. You usually need ",[49,14478,14479],{},"int(input(...))"," if you want to count down from a number.",[639,14482,14484],{"id":14483},"can-i-make-the-timer-use-minutes-and-seconds","Can I make the timer use minutes and seconds?",[14,14486,14487],{},"Yes. That is a good next step after the simple version works.",[37,14489,2005],{"id":2004},[309,14491,14492,14496,14501,14505,14509,14513],{},[24,14493,14494],{},[372,14495,13186],{"href":13185},[24,14497,14498],{},[372,14499,14500],{"href":13144},"time.sleep() function explained",[24,14502,14503],{},[372,14504,13426],{"href":13425},[24,14506,14507],{},[372,14508,6876],{"href":6397},[24,14510,14511],{},[372,14512,13737],{"href":13736},[24,14514,14515],{},[372,14516,4990,14517],{"href":6471},[49,14518,6472],{},[14,14520,14521],{},"Copy the simple script first and run it as-is. Then try one small change, like starting from a different number or letting the user choose the countdown value.",[2034,14523,14524],{},"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 .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 .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 .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 .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}",{"title":47,"searchDepth":66,"depth":66,"links":14526},[14527,14528,14529,14530,14531,14540,14541,14545,14546,14556,14562,14563,14571],{"id":39,"depth":66,"text":40},{"id":9294,"depth":66,"text":9295},{"id":13151,"depth":66,"text":13152},{"id":13189,"depth":66,"text":13190},{"id":13311,"depth":66,"text":13312,"children":14532},[14533,14534,14535,14536,14537,14538,14539],{"id":13318,"depth":73,"text":13321},{"id":13348,"depth":73,"text":13351},{"id":13376,"depth":73,"text":13379},{"id":13429,"depth":73,"text":13432},{"id":13453,"depth":73,"text":13344},{"id":13483,"depth":73,"text":13486},{"id":13533,"depth":73,"text":13534},{"id":13562,"depth":66,"text":13563},{"id":13578,"depth":66,"text":13579,"children":14542},[14543,14544],{"id":13690,"depth":73,"text":13691},{"id":13740,"depth":73,"text":13741},{"id":13818,"depth":66,"text":13819},{"id":6401,"depth":66,"text":6402,"children":14547},[14548,14550,14552,14553,14554],{"id":14066,"depth":73,"text":14549},"Forgetting to import time",{"id":14159,"depth":73,"text":14551},"Using input() without int()",{"id":14230,"depth":73,"text":14231},{"id":14252,"depth":73,"text":14253},{"id":14278,"depth":73,"text":14555},"Expecting sleep() to print something",{"id":14290,"depth":66,"text":14291,"children":14557},[14558,14559,14560,14561],{"id":14297,"depth":73,"text":14298},{"id":14318,"depth":73,"text":14319},{"id":14345,"depth":73,"text":14346},{"id":14375,"depth":73,"text":14376},{"id":14406,"depth":66,"text":14407},{"id":1941,"depth":66,"text":1942,"children":14564},[14565,14566,14568,14570],{"id":14447,"depth":73,"text":14448},{"id":14454,"depth":73,"text":14567},"Why do I need import time?",{"id":14468,"depth":73,"text":14569},"Why does input() cause an error?",{"id":14483,"depth":73,"text":14484},{"id":2004,"depth":66,"text":2005},"Master python countdown timer example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-countdown-timer-example",{"title":12977,"description":14572},"examples\u002Fpython-countdown-timer-example","_j5TvY0Bv5A1RH91lrVq5pFHLJSp3F81U8LhpxT3ibY",{"id":14579,"title":14580,"body":14581,"description":16575,"extension":2075,"meta":16576,"navigation":69,"path":16577,"seo":16578,"stem":16579,"__hash__":16580},"content\u002Fexamples\u002Fpython-csv-reader-example.md","Python CSV Reader Example",{"type":7,"value":14582,"toc":16549},[14583,14586,14592,14595,14609,14612,14726,14729,14731,14734,14755,14762,14766,14772,14775,14781,14784,14788,14790,14886,14888,14893,14904,14909,14929,14934,14942,14947,14955,14960,14965,14970,14975,14990,14994,14997,15122,15125,15136,15139,15156,15168,15171,15282,15285,15307,15311,15320,15327,15442,15444,15533,15536,15540,15545,15551,15687,15689,15724,15727,15744,15747,15749,15752,15756,15765,15767,15819,15825,15828,15856,15859,15898,15909,15913,15916,15981,15984,15995,15999,16002,16005,16038,16044,16047,16159,16163,16166,16199,16202,16207,16211,16214,16217,16228,16231,16282,16286,16289,16292,16298,16304,16307,16418,16422,16425,16436,16439,16459,16461,16465,16470,16474,16477,16481,16487,16491,16496,16498,16534,16540,16546],[10,14584,14580],{"id":14585},"python-csv-reader-example",[14,14587,14588,14589,13338],{},"This example shows how to read a CSV file in Python with the built-in ",[49,14590,14591],{},"csv",[14,14593,14594],{},"It focuses on one simple working script:",[309,14596,14597,14600,14603,14606],{},[24,14598,14599],{},"open a CSV file",[24,14601,14602],{},"read each row",[24,14604,14605],{},"print the row",[24,14607,14608],{},"understand what the output means",[14,14610,14611],{},"If you want the fastest working example, start here:",[42,14613,14615],{"className":44,"code":14614,"language":46,"meta":47,"style":47},"import csv\n\nwith open('data.csv', newline='', encoding='utf-8') as file:\n    reader = csv.reader(file)\n    for row in reader:\n        print(row)\n",[49,14616,14617,14624,14628,14679,14701,14715],{"__ignoreMap":47},[52,14618,14619,14621],{"class":54,"line":55},[52,14620,59],{"class":58},[52,14622,14623],{"class":62}," csv\n",[52,14625,14626],{"class":54,"line":66},[52,14627,70],{"emptyLinePlaceholder":69},[52,14629,14630,14633,14636,14638,14640,14643,14645,14647,14650,14652,14655,14657,14660,14662,14664,14667,14669,14671,14673,14677],{"class":54,"line":73},[52,14631,14632],{"class":58},"with",[52,14634,14635],{"class":241}," open",[52,14637,193],{"class":102},[52,14639,4162],{"class":83},[52,14641,14642],{"class":87},"data.csv",[52,14644,4162],{"class":83},[52,14646,199],{"class":102},[52,14648,14649],{"class":202}," newline",[52,14651,80],{"class":79},[52,14653,14654],{"class":83},"''",[52,14656,199],{"class":102},[52,14658,14659],{"class":202}," encoding",[52,14661,80],{"class":79},[52,14663,4162],{"class":83},[52,14665,14666],{"class":87},"utf-8",[52,14668,4162],{"class":83},[52,14670,1521],{"class":102},[52,14672,3567],{"class":58},[52,14674,14676],{"class":14675},"sMMDD"," file",[52,14678,1330],{"class":102},[52,14680,14681,14684,14686,14689,14691,14694,14696,14699],{"class":54,"line":94},[52,14682,14683],{"class":62},"    reader ",[52,14685,80],{"class":79},[52,14687,14688],{"class":62}," csv",[52,14690,186],{"class":102},[52,14692,14693],{"class":189},"reader",[52,14695,193],{"class":102},[52,14697,14698],{"class":14675},"file",[52,14700,211],{"class":102},[52,14702,14703,14705,14708,14710,14713],{"class":54,"line":106},[52,14704,11773],{"class":58},[52,14706,14707],{"class":62}," row ",[52,14709,11213],{"class":58},[52,14711,14712],{"class":62}," reader",[52,14714,1330],{"class":102},[52,14716,14717,14719,14721,14724],{"class":54,"line":128},[52,14718,1470],{"class":241},[52,14720,193],{"class":102},[52,14722,14723],{"class":189},"row",[52,14725,211],{"class":102},[14,14727,14728],{},"Use this when you want the quickest way to read rows from a CSV file.",[37,14730,2268],{"id":2267},[14,14732,14733],{},"In this page, you will learn:",[309,14735,14736,14742,14749,14752],{},[24,14737,14738,14739],{},"how to open a CSV file safely with ",[49,14740,14741],{},"with open(...)",[24,14743,14744,14745,14748],{},"how to use ",[49,14746,14747],{},"csv.reader()"," to read rows",[24,14750,14751],{},"how each row becomes a list of values",[24,14753,14754],{},"when this simple approach is enough for beginners",[14,14756,14757,14758,186],{},"This is a good starting example if you want to see CSV reading work before moving to a more detailed guide like ",[372,14759,14761],{"href":14760},"\u002Fhow-to\u002Fhow-to-read-a-csv-file-in-python\u002F","how to read a CSV file in Python",[37,14763,14765],{"id":14764},"example-csv-file-to-use","Example CSV file to use",[14,14767,14768,14769,14771],{},"Create a file named ",[49,14770,14642],{}," in the same folder as your Python script.",[14,14773,14774],{},"Put this content inside it:",[42,14776,14779],{"className":14777,"code":14778,"language":955,"meta":47},[953],"name,age,city\nAlice,30,London\nBob,25,Paris\nCharlie,35,Berlin\n",[49,14780,14778],{"__ignoreMap":47},[14,14782,14783],{},"This small file makes it easy to test the code right away.",[37,14785,14787],{"id":14786},"basic-csv-reader-script","Basic CSV reader script",[14,14789,5591],{},[42,14791,14792],{"className":44,"code":14614,"language":46,"meta":47,"style":47},[49,14793,14794,14800,14804,14846,14864,14876],{"__ignoreMap":47},[52,14795,14796,14798],{"class":54,"line":55},[52,14797,59],{"class":58},[52,14799,14623],{"class":62},[52,14801,14802],{"class":54,"line":66},[52,14803,70],{"emptyLinePlaceholder":69},[52,14805,14806,14808,14810,14812,14814,14816,14818,14820,14822,14824,14826,14828,14830,14832,14834,14836,14838,14840,14842,14844],{"class":54,"line":73},[52,14807,14632],{"class":58},[52,14809,14635],{"class":241},[52,14811,193],{"class":102},[52,14813,4162],{"class":83},[52,14815,14642],{"class":87},[52,14817,4162],{"class":83},[52,14819,199],{"class":102},[52,14821,14649],{"class":202},[52,14823,80],{"class":79},[52,14825,14654],{"class":83},[52,14827,199],{"class":102},[52,14829,14659],{"class":202},[52,14831,80],{"class":79},[52,14833,4162],{"class":83},[52,14835,14666],{"class":87},[52,14837,4162],{"class":83},[52,14839,1521],{"class":102},[52,14841,3567],{"class":58},[52,14843,14676],{"class":14675},[52,14845,1330],{"class":102},[52,14847,14848,14850,14852,14854,14856,14858,14860,14862],{"class":54,"line":94},[52,14849,14683],{"class":62},[52,14851,80],{"class":79},[52,14853,14688],{"class":62},[52,14855,186],{"class":102},[52,14857,14693],{"class":189},[52,14859,193],{"class":102},[52,14861,14698],{"class":14675},[52,14863,211],{"class":102},[52,14865,14866,14868,14870,14872,14874],{"class":54,"line":106},[52,14867,11773],{"class":58},[52,14869,14707],{"class":62},[52,14871,11213],{"class":58},[52,14873,14712],{"class":62},[52,14875,1330],{"class":102},[52,14877,14878,14880,14882,14884],{"class":54,"line":128},[52,14879,1470],{"class":241},[52,14881,193],{"class":102},[52,14883,14723],{"class":189},[52,14885,211],{"class":102},[639,14887,9610],{"id":9609},[14,14889,14890],{},[49,14891,14892],{},"import csv",[309,14894,14895,14898],{},[24,14896,14897],{},"loads Python's built-in CSV tools",[24,14899,14900,14901,14903],{},"without this line, ",[49,14902,14747],{}," will not work",[14,14905,14906],{},[49,14907,14908],{},"open('data.csv', newline='', encoding='utf-8')",[309,14910,14911,14914,14920],{},[24,14912,14913],{},"opens the file for reading",[24,14915,14916,14919],{},[49,14917,14918],{},"encoding='utf-8'"," helps Python read text correctly",[24,14921,14922,14925,14926,14928],{},[49,14923,14924],{},"newline=''"," is the recommended way to open CSV files with the ",[49,14927,14591],{}," module",[14,14930,14931],{},[49,14932,14933],{},"with open(...) as file",[309,14935,14936,14939],{},[24,14937,14938],{},"opens the file safely",[24,14940,14941],{},"closes it automatically when finished",[14,14943,14944],{},[49,14945,14946],{},"reader = csv.reader(file)",[309,14948,14949,14952],{},[24,14950,14951],{},"creates a CSV reader object",[24,14953,14954],{},"this lets you loop through the file row by row",[14,14956,14957],{},[49,14958,14959],{},"for row in reader:",[309,14961,14962],{},[24,14963,14964],{},"reads one row at a time",[14,14966,14967],{},[49,14968,14969],{},"print(row)",[309,14971,14972],{},[24,14973,14974],{},"prints each row so you can see what Python read",[14,14976,14977,14978,14981,14982,759,14986,186],{},"If you want to understand ",[49,14979,14980],{},"open()"," better, see ",[372,14983,14985],{"href":14984},"\u002Freference\u002Fpython-open-function-explained\u002F","Python open() function explained",[372,14987,14989],{"href":14988},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write\u002F","Python file handling basics",[37,14991,14993],{"id":14992},"how-to-understand-the-output","How to understand the output",[14,14995,14996],{},"For the sample CSV file above, the script prints:",[42,14998,15000],{"className":44,"code":14999,"language":46,"meta":47,"style":47},"['name', 'age', 'city']\n['Alice', '30', 'London']\n['Bob', '25', 'Paris']\n['Charlie', '35', 'Berlin']\n",[49,15001,15002,15031,15061,15091],{"__ignoreMap":47},[52,15003,15004,15006,15008,15010,15012,15014,15016,15018,15020,15022,15024,15027,15029],{"class":54,"line":55},[52,15005,250],{"class":102},[52,15007,4162],{"class":83},[52,15009,2147],{"class":87},[52,15011,4162],{"class":83},[52,15013,199],{"class":102},[52,15015,4172],{"class":83},[52,15017,2167],{"class":87},[52,15019,4162],{"class":83},[52,15021,199],{"class":102},[52,15023,4172],{"class":83},[52,15025,15026],{"class":87},"city",[52,15028,4162],{"class":83},[52,15030,1147],{"class":102},[52,15032,15033,15035,15037,15039,15041,15043,15045,15048,15050,15052,15054,15057,15059],{"class":54,"line":66},[52,15034,250],{"class":102},[52,15036,4162],{"class":83},[52,15038,2156],{"class":87},[52,15040,4162],{"class":83},[52,15042,199],{"class":102},[52,15044,4172],{"class":83},[52,15046,15047],{"class":87},"30",[52,15049,4162],{"class":83},[52,15051,199],{"class":102},[52,15053,4172],{"class":83},[52,15055,15056],{"class":87},"London",[52,15058,4162],{"class":83},[52,15060,1147],{"class":102},[52,15062,15063,15065,15067,15069,15071,15073,15075,15078,15080,15082,15084,15087,15089],{"class":54,"line":73},[52,15064,250],{"class":102},[52,15066,4162],{"class":83},[52,15068,10858],{"class":87},[52,15070,4162],{"class":83},[52,15072,199],{"class":102},[52,15074,4172],{"class":83},[52,15076,15077],{"class":87},"25",[52,15079,4162],{"class":83},[52,15081,199],{"class":102},[52,15083,4172],{"class":83},[52,15085,15086],{"class":87},"Paris",[52,15088,4162],{"class":83},[52,15090,1147],{"class":102},[52,15092,15093,15095,15097,15100,15102,15104,15106,15109,15111,15113,15115,15118,15120],{"class":54,"line":94},[52,15094,250],{"class":102},[52,15096,4162],{"class":83},[52,15098,15099],{"class":87},"Charlie",[52,15101,4162],{"class":83},[52,15103,199],{"class":102},[52,15105,4172],{"class":83},[52,15107,15108],{"class":87},"35",[52,15110,4162],{"class":83},[52,15112,199],{"class":102},[52,15114,4172],{"class":83},[52,15116,15117],{"class":87},"Berlin",[52,15119,4162],{"class":83},[52,15121,1147],{"class":102},[14,15123,15124],{},"A few important things to notice:",[309,15126,15127,15130,15133],{},[24,15128,15129],{},"each row is a Python list",[24,15131,15132],{},"the first row is the header row",[24,15134,15135],{},"all values are read as strings by default",[14,15137,15138],{},"That means:",[309,15140,15141,15147,15153],{},[24,15142,15143,15146],{},[49,15144,15145],{},"'30'"," is a string, not a number",[24,15148,15149,15152],{},[49,15150,15151],{},"'25'"," is also a string",[24,15154,15155],{},"if you need numbers, you must convert them yourself",[14,15157,15158,15159,15161,15162,1727,15164,186],{},"For example, if you later want to turn ",[49,15160,15145],{}," into ",[49,15163,15047],{},[372,15165,15167],{"href":15166},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python\u002F","how to convert string to int in Python",[14,15169,15170],{},"You can also test what type each row is:",[42,15172,15174],{"className":44,"code":15173,"language":46,"meta":47,"style":47},"import csv\n\nwith open('data.csv', newline='', encoding='utf-8') as file:\n    reader = csv.reader(file)\n    for row in reader:\n        print(row)\n        print(type(row))\n",[49,15175,15176,15182,15186,15228,15246,15258,15268],{"__ignoreMap":47},[52,15177,15178,15180],{"class":54,"line":55},[52,15179,59],{"class":58},[52,15181,14623],{"class":62},[52,15183,15184],{"class":54,"line":66},[52,15185,70],{"emptyLinePlaceholder":69},[52,15187,15188,15190,15192,15194,15196,15198,15200,15202,15204,15206,15208,15210,15212,15214,15216,15218,15220,15222,15224,15226],{"class":54,"line":73},[52,15189,14632],{"class":58},[52,15191,14635],{"class":241},[52,15193,193],{"class":102},[52,15195,4162],{"class":83},[52,15197,14642],{"class":87},[52,15199,4162],{"class":83},[52,15201,199],{"class":102},[52,15203,14649],{"class":202},[52,15205,80],{"class":79},[52,15207,14654],{"class":83},[52,15209,199],{"class":102},[52,15211,14659],{"class":202},[52,15213,80],{"class":79},[52,15215,4162],{"class":83},[52,15217,14666],{"class":87},[52,15219,4162],{"class":83},[52,15221,1521],{"class":102},[52,15223,3567],{"class":58},[52,15225,14676],{"class":14675},[52,15227,1330],{"class":102},[52,15229,15230,15232,15234,15236,15238,15240,15242,15244],{"class":54,"line":94},[52,15231,14683],{"class":62},[52,15233,80],{"class":79},[52,15235,14688],{"class":62},[52,15237,186],{"class":102},[52,15239,14693],{"class":189},[52,15241,193],{"class":102},[52,15243,14698],{"class":14675},[52,15245,211],{"class":102},[52,15247,15248,15250,15252,15254,15256],{"class":54,"line":106},[52,15249,11773],{"class":58},[52,15251,14707],{"class":62},[52,15253,11213],{"class":58},[52,15255,14712],{"class":62},[52,15257,1330],{"class":102},[52,15259,15260,15262,15264,15266],{"class":54,"line":128},[52,15261,1470],{"class":241},[52,15263,193],{"class":102},[52,15265,14723],{"class":189},[52,15267,211],{"class":102},[52,15269,15270,15272,15274,15276,15278,15280],{"class":54,"line":148},[52,15271,1470],{"class":241},[52,15273,193],{"class":102},[52,15275,4198],{"class":4733},[52,15277,193],{"class":102},[52,15279,14723],{"class":189},[52,15281,8886],{"class":102},[14,15283,15284],{},"Expected type:",[42,15286,15288],{"className":44,"code":15287,"language":46,"meta":47,"style":47},"\u003Cclass 'list'>\n",[49,15289,15290],{"__ignoreMap":47},[52,15291,15292,15295,15297,15299,15302,15304],{"class":54,"line":55},[52,15293,15294],{"class":79},"\u003C",[52,15296,6994],{"class":1475},[52,15298,4172],{"class":83},[52,15300,15301],{"class":87},"list",[52,15303,4162],{"class":83},[52,15305,15306],{"class":79},">\n",[37,15308,15310],{"id":15309},"skipping-the-header-row","Skipping the header row",[14,15312,15313,15314,2587,15316,11556,15318,186],{},"Often, the first row contains column names like ",[49,15315,2147],{},[49,15317,2167],{},[49,15319,15026],{},[14,15321,15322,15323,15326],{},"If you want to skip that row, call ",[49,15324,15325],{},"next(reader)"," once before the loop:",[42,15328,15330],{"className":44,"code":15329,"language":46,"meta":47,"style":47},"import csv\n\nwith open('data.csv', newline='', encoding='utf-8') as file:\n    reader = csv.reader(file)\n    next(reader)  # Skip the header row\n\n    for row in reader:\n        print(row)\n",[49,15331,15332,15338,15342,15384,15402,15416,15420,15432],{"__ignoreMap":47},[52,15333,15334,15336],{"class":54,"line":55},[52,15335,59],{"class":58},[52,15337,14623],{"class":62},[52,15339,15340],{"class":54,"line":66},[52,15341,70],{"emptyLinePlaceholder":69},[52,15343,15344,15346,15348,15350,15352,15354,15356,15358,15360,15362,15364,15366,15368,15370,15372,15374,15376,15378,15380,15382],{"class":54,"line":73},[52,15345,14632],{"class":58},[52,15347,14635],{"class":241},[52,15349,193],{"class":102},[52,15351,4162],{"class":83},[52,15353,14642],{"class":87},[52,15355,4162],{"class":83},[52,15357,199],{"class":102},[52,15359,14649],{"class":202},[52,15361,80],{"class":79},[52,15363,14654],{"class":83},[52,15365,199],{"class":102},[52,15367,14659],{"class":202},[52,15369,80],{"class":79},[52,15371,4162],{"class":83},[52,15373,14666],{"class":87},[52,15375,4162],{"class":83},[52,15377,1521],{"class":102},[52,15379,3567],{"class":58},[52,15381,14676],{"class":14675},[52,15383,1330],{"class":102},[52,15385,15386,15388,15390,15392,15394,15396,15398,15400],{"class":54,"line":94},[52,15387,14683],{"class":62},[52,15389,80],{"class":79},[52,15391,14688],{"class":62},[52,15393,186],{"class":102},[52,15395,14693],{"class":189},[52,15397,193],{"class":102},[52,15399,14698],{"class":14675},[52,15401,211],{"class":102},[52,15403,15404,15407,15409,15411,15413],{"class":54,"line":106},[52,15405,15406],{"class":241},"    next",[52,15408,193],{"class":102},[52,15410,14693],{"class":189},[52,15412,1521],{"class":102},[52,15414,15415],{"class":4368},"  # Skip the header row\n",[52,15417,15418],{"class":54,"line":128},[52,15419,70],{"emptyLinePlaceholder":69},[52,15421,15422,15424,15426,15428,15430],{"class":54,"line":148},[52,15423,11773],{"class":58},[52,15425,14707],{"class":62},[52,15427,11213],{"class":58},[52,15429,14712],{"class":62},[52,15431,1330],{"class":102},[52,15433,15434,15436,15438,15440],{"class":54,"line":164},[52,15435,1470],{"class":241},[52,15437,193],{"class":102},[52,15439,14723],{"class":189},[52,15441,211],{"class":102},[14,15443,5267],{},[42,15445,15447],{"className":44,"code":15446,"language":46,"meta":47,"style":47},"['Alice', '30', 'London']\n['Bob', '25', 'Paris']\n['Charlie', '35', 'Berlin']\n",[49,15448,15449,15477,15505],{"__ignoreMap":47},[52,15450,15451,15453,15455,15457,15459,15461,15463,15465,15467,15469,15471,15473,15475],{"class":54,"line":55},[52,15452,250],{"class":102},[52,15454,4162],{"class":83},[52,15456,2156],{"class":87},[52,15458,4162],{"class":83},[52,15460,199],{"class":102},[52,15462,4172],{"class":83},[52,15464,15047],{"class":87},[52,15466,4162],{"class":83},[52,15468,199],{"class":102},[52,15470,4172],{"class":83},[52,15472,15056],{"class":87},[52,15474,4162],{"class":83},[52,15476,1147],{"class":102},[52,15478,15479,15481,15483,15485,15487,15489,15491,15493,15495,15497,15499,15501,15503],{"class":54,"line":66},[52,15480,250],{"class":102},[52,15482,4162],{"class":83},[52,15484,10858],{"class":87},[52,15486,4162],{"class":83},[52,15488,199],{"class":102},[52,15490,4172],{"class":83},[52,15492,15077],{"class":87},[52,15494,4162],{"class":83},[52,15496,199],{"class":102},[52,15498,4172],{"class":83},[52,15500,15086],{"class":87},[52,15502,4162],{"class":83},[52,15504,1147],{"class":102},[52,15506,15507,15509,15511,15513,15515,15517,15519,15521,15523,15525,15527,15529,15531],{"class":54,"line":73},[52,15508,250],{"class":102},[52,15510,4162],{"class":83},[52,15512,15099],{"class":87},[52,15514,4162],{"class":83},[52,15516,199],{"class":102},[52,15518,4172],{"class":83},[52,15520,15108],{"class":87},[52,15522,4162],{"class":83},[52,15524,199],{"class":102},[52,15526,4172],{"class":83},[52,15528,15117],{"class":87},[52,15530,4162],{"class":83},[52,15532,1147],{"class":102},[14,15534,15535],{},"This is useful when you only want the real data rows.",[37,15537,15539],{"id":15538},"reading-csv-rows-as-dictionaries","Reading CSV rows as dictionaries",[14,15541,15542,15544],{},[49,15543,14747],{}," gives you lists.",[14,15546,15547,15548,118],{},"If you want to access values by column name instead of position, use ",[49,15549,15550],{},"csv.DictReader()",[42,15552,15554],{"className":44,"code":15553,"language":46,"meta":47,"style":47},"import csv\n\nwith open('data.csv', newline='', encoding='utf-8') as file:\n    reader = csv.DictReader(file)\n\n    for row in reader:\n        print(row['name'], row['age'], row['city'])\n",[49,15555,15556,15562,15566,15608,15627,15631,15643],{"__ignoreMap":47},[52,15557,15558,15560],{"class":54,"line":55},[52,15559,59],{"class":58},[52,15561,14623],{"class":62},[52,15563,15564],{"class":54,"line":66},[52,15565,70],{"emptyLinePlaceholder":69},[52,15567,15568,15570,15572,15574,15576,15578,15580,15582,15584,15586,15588,15590,15592,15594,15596,15598,15600,15602,15604,15606],{"class":54,"line":73},[52,15569,14632],{"class":58},[52,15571,14635],{"class":241},[52,15573,193],{"class":102},[52,15575,4162],{"class":83},[52,15577,14642],{"class":87},[52,15579,4162],{"class":83},[52,15581,199],{"class":102},[52,15583,14649],{"class":202},[52,15585,80],{"class":79},[52,15587,14654],{"class":83},[52,15589,199],{"class":102},[52,15591,14659],{"class":202},[52,15593,80],{"class":79},[52,15595,4162],{"class":83},[52,15597,14666],{"class":87},[52,15599,4162],{"class":83},[52,15601,1521],{"class":102},[52,15603,3567],{"class":58},[52,15605,14676],{"class":14675},[52,15607,1330],{"class":102},[52,15609,15610,15612,15614,15616,15618,15621,15623,15625],{"class":54,"line":94},[52,15611,14683],{"class":62},[52,15613,80],{"class":79},[52,15615,14688],{"class":62},[52,15617,186],{"class":102},[52,15619,15620],{"class":189},"DictReader",[52,15622,193],{"class":102},[52,15624,14698],{"class":14675},[52,15626,211],{"class":102},[52,15628,15629],{"class":54,"line":106},[52,15630,70],{"emptyLinePlaceholder":69},[52,15632,15633,15635,15637,15639,15641],{"class":54,"line":128},[52,15634,11773],{"class":58},[52,15636,14707],{"class":62},[52,15638,11213],{"class":58},[52,15640,14712],{"class":62},[52,15642,1330],{"class":102},[52,15644,15645,15647,15649,15651,15653,15655,15657,15659,15662,15665,15667,15669,15671,15673,15675,15677,15679,15681,15683,15685],{"class":54,"line":148},[52,15646,1470],{"class":241},[52,15648,193],{"class":102},[52,15650,14723],{"class":189},[52,15652,250],{"class":102},[52,15654,4162],{"class":83},[52,15656,2147],{"class":87},[52,15658,4162],{"class":83},[52,15660,15661],{"class":102},"],",[52,15663,15664],{"class":189}," row",[52,15666,250],{"class":102},[52,15668,4162],{"class":83},[52,15670,2167],{"class":87},[52,15672,4162],{"class":83},[52,15674,15661],{"class":102},[52,15676,15664],{"class":189},[52,15678,250],{"class":102},[52,15680,4162],{"class":83},[52,15682,15026],{"class":87},[52,15684,4162],{"class":83},[52,15686,269],{"class":102},[14,15688,5267],{},[42,15690,15692],{"className":44,"code":15691,"language":46,"meta":47,"style":47},"Alice 30 London\nBob 25 Paris\nCharlie 35 Berlin\n",[49,15693,15694,15704,15714],{"__ignoreMap":47},[52,15695,15696,15699,15701],{"class":54,"line":55},[52,15697,15698],{"class":62},"Alice ",[52,15700,15047],{"class":121},[52,15702,15703],{"class":62}," London\n",[52,15705,15706,15709,15711],{"class":54,"line":66},[52,15707,15708],{"class":62},"Bob ",[52,15710,15077],{"class":121},[52,15712,15713],{"class":62}," Paris\n",[52,15715,15716,15719,15721],{"class":54,"line":73},[52,15717,15718],{"class":62},"Charlie ",[52,15720,15108],{"class":121},[52,15722,15723],{"class":62}," Berlin\n",[14,15725,15726],{},"This is often easier for beginners because:",[309,15728,15729,15738,15741],{},[24,15730,15731,15734,15735],{},[49,15732,15733],{},"row['name']"," is clearer than ",[49,15736,15737],{},"row[0]",[24,15739,15740],{},"column names become dictionary keys",[24,15742,15743],{},"you do not need to manually skip the header row",[14,15745,15746],{},"If you want a broader overview, see the Python csv module overview.",[37,15748,6402],{"id":6401},[14,15750,15751],{},"Here are some common problems when reading CSV files.",[639,15753,15755],{"id":15754},"wrong-file-path","Wrong file path",[14,15757,15758,15759,15761,15762,186],{},"If Python cannot find ",[49,15760,14642],{},", you may get a ",[49,15763,15764],{},"FileNotFoundError",[14,15766,4259],{},[42,15768,15770],{"className":44,"code":15769,"language":46,"meta":47,"style":47},"with open('data.csv', newline='', encoding='utf-8') as file:\n    ...\n",[49,15771,15772,15814],{"__ignoreMap":47},[52,15773,15774,15776,15778,15780,15782,15784,15786,15788,15790,15792,15794,15796,15798,15800,15802,15804,15806,15808,15810,15812],{"class":54,"line":55},[52,15775,14632],{"class":58},[52,15777,14635],{"class":241},[52,15779,193],{"class":102},[52,15781,4162],{"class":83},[52,15783,14642],{"class":87},[52,15785,4162],{"class":83},[52,15787,199],{"class":102},[52,15789,14649],{"class":202},[52,15791,80],{"class":79},[52,15793,14654],{"class":83},[52,15795,199],{"class":102},[52,15797,14659],{"class":202},[52,15799,80],{"class":79},[52,15801,4162],{"class":83},[52,15803,14666],{"class":87},[52,15805,4162],{"class":83},[52,15807,1521],{"class":102},[52,15809,3567],{"class":58},[52,15811,14676],{"class":14675},[52,15813,1330],{"class":102},[52,15815,15816],{"class":54,"line":66},[52,15817,15818],{"class":2533},"    ...\n",[14,15820,15821,15822,15824],{},"This only works if ",[49,15823,14642],{}," is in the folder Python expects.",[14,15826,15827],{},"To check your current working folder:",[42,15829,15831],{"className":44,"code":15830,"language":46,"meta":47,"style":47},"import os\nprint(os.getcwd())\n",[49,15832,15833,15840],{"__ignoreMap":47},[52,15834,15835,15837],{"class":54,"line":55},[52,15836,59],{"class":58},[52,15838,15839],{"class":62}," os\n",[52,15841,15842,15844,15846,15849,15851,15854],{"class":54,"line":66},[52,15843,242],{"class":241},[52,15845,193],{"class":102},[52,15847,15848],{"class":189},"os",[52,15850,186],{"class":102},[52,15852,15853],{"class":189},"getcwd",[52,15855,1816],{"class":102},[14,15857,15858],{},"To check whether the file exists:",[42,15860,15862],{"className":44,"code":15861,"language":46,"meta":47,"style":47},"import os\nprint(os.path.exists('data.csv'))\n",[49,15863,15864,15870],{"__ignoreMap":47},[52,15865,15866,15868],{"class":54,"line":55},[52,15867,59],{"class":58},[52,15869,15839],{"class":62},[52,15871,15872,15874,15876,15878,15880,15883,15885,15888,15890,15892,15894,15896],{"class":54,"line":66},[52,15873,242],{"class":241},[52,15875,193],{"class":102},[52,15877,15848],{"class":189},[52,15879,186],{"class":102},[52,15881,15882],{"class":1320},"path",[52,15884,186],{"class":102},[52,15886,15887],{"class":189},"exists",[52,15889,193],{"class":102},[52,15891,4162],{"class":83},[52,15893,14642],{"class":87},[52,15895,4162],{"class":83},[52,15897,8886],{"class":102},[14,15899,15900,15901,857,15905,186],{},"If you need help with this error, see ",[372,15902,15904],{"href":15903},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","FileNotFoundError: No such file or directory fix",[372,15906,15908],{"href":15907},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python\u002F","how to check if a file exists in Python",[639,15910,15912],{"id":15911},"forgetting-to-import-csv","Forgetting to import csv",[14,15914,15915],{},"This will not work:",[42,15917,15919],{"className":44,"code":15918,"language":46,"meta":47,"style":47},"with open('data.csv', newline='', encoding='utf-8') as file:\n    reader = csv.reader(file)\n",[49,15920,15921,15963],{"__ignoreMap":47},[52,15922,15923,15925,15927,15929,15931,15933,15935,15937,15939,15941,15943,15945,15947,15949,15951,15953,15955,15957,15959,15961],{"class":54,"line":55},[52,15924,14632],{"class":58},[52,15926,14635],{"class":241},[52,15928,193],{"class":102},[52,15930,4162],{"class":83},[52,15932,14642],{"class":87},[52,15934,4162],{"class":83},[52,15936,199],{"class":102},[52,15938,14649],{"class":202},[52,15940,80],{"class":79},[52,15942,14654],{"class":83},[52,15944,199],{"class":102},[52,15946,14659],{"class":202},[52,15948,80],{"class":79},[52,15950,4162],{"class":83},[52,15952,14666],{"class":87},[52,15954,4162],{"class":83},[52,15956,1521],{"class":102},[52,15958,3567],{"class":58},[52,15960,14676],{"class":14675},[52,15962,1330],{"class":102},[52,15964,15965,15967,15969,15971,15973,15975,15977,15979],{"class":54,"line":66},[52,15966,14683],{"class":62},[52,15968,80],{"class":79},[52,15970,14688],{"class":62},[52,15972,186],{"class":102},[52,15974,14693],{"class":189},[52,15976,193],{"class":102},[52,15978,14698],{"class":14675},[52,15980,211],{"class":102},[14,15982,15983],{},"You must include:",[42,15985,15987],{"className":44,"code":15986,"language":46,"meta":47,"style":47},"import csv\n",[49,15988,15989],{"__ignoreMap":47},[52,15990,15991,15993],{"class":54,"line":55},[52,15992,59],{"class":58},[52,15994,14623],{"class":62},[639,15996,15998],{"id":15997},"expecting-numbers-instead-of-strings","Expecting numbers instead of strings",[14,16000,16001],{},"CSV files store text.",[14,16003,16004],{},"So this row:",[42,16006,16008],{"className":44,"code":16007,"language":46,"meta":47,"style":47},"['Alice', '30', 'London']\n",[49,16009,16010],{"__ignoreMap":47},[52,16011,16012,16014,16016,16018,16020,16022,16024,16026,16028,16030,16032,16034,16036],{"class":54,"line":55},[52,16013,250],{"class":102},[52,16015,4162],{"class":83},[52,16017,2156],{"class":87},[52,16019,4162],{"class":83},[52,16021,199],{"class":102},[52,16023,4172],{"class":83},[52,16025,15047],{"class":87},[52,16027,4162],{"class":83},[52,16029,199],{"class":102},[52,16031,4172],{"class":83},[52,16033,15056],{"class":87},[52,16035,4162],{"class":83},[52,16037,1147],{"class":102},[14,16039,16040,16041,16043],{},"contains ",[49,16042,15145],{}," as a string.",[14,16045,16046],{},"You can confirm that with debugging prints:",[42,16048,16050],{"className":44,"code":16049,"language":46,"meta":47,"style":47},"import csv\n\nwith open('data.csv', newline='', encoding='utf-8') as file:\n    reader = csv.reader(file)\n    for row in reader:\n        print(row)\n        print(len(row))\n",[49,16051,16052,16058,16062,16104,16122,16134,16144],{"__ignoreMap":47},[52,16053,16054,16056],{"class":54,"line":55},[52,16055,59],{"class":58},[52,16057,14623],{"class":62},[52,16059,16060],{"class":54,"line":66},[52,16061,70],{"emptyLinePlaceholder":69},[52,16063,16064,16066,16068,16070,16072,16074,16076,16078,16080,16082,16084,16086,16088,16090,16092,16094,16096,16098,16100,16102],{"class":54,"line":73},[52,16065,14632],{"class":58},[52,16067,14635],{"class":241},[52,16069,193],{"class":102},[52,16071,4162],{"class":83},[52,16073,14642],{"class":87},[52,16075,4162],{"class":83},[52,16077,199],{"class":102},[52,16079,14649],{"class":202},[52,16081,80],{"class":79},[52,16083,14654],{"class":83},[52,16085,199],{"class":102},[52,16087,14659],{"class":202},[52,16089,80],{"class":79},[52,16091,4162],{"class":83},[52,16093,14666],{"class":87},[52,16095,4162],{"class":83},[52,16097,1521],{"class":102},[52,16099,3567],{"class":58},[52,16101,14676],{"class":14675},[52,16103,1330],{"class":102},[52,16105,16106,16108,16110,16112,16114,16116,16118,16120],{"class":54,"line":94},[52,16107,14683],{"class":62},[52,16109,80],{"class":79},[52,16111,14688],{"class":62},[52,16113,186],{"class":102},[52,16115,14693],{"class":189},[52,16117,193],{"class":102},[52,16119,14698],{"class":14675},[52,16121,211],{"class":102},[52,16123,16124,16126,16128,16130,16132],{"class":54,"line":106},[52,16125,11773],{"class":58},[52,16127,14707],{"class":62},[52,16129,11213],{"class":58},[52,16131,14712],{"class":62},[52,16133,1330],{"class":102},[52,16135,16136,16138,16140,16142],{"class":54,"line":128},[52,16137,1470],{"class":241},[52,16139,193],{"class":102},[52,16141,14723],{"class":189},[52,16143,211],{"class":102},[52,16145,16146,16148,16150,16153,16155,16157],{"class":54,"line":148},[52,16147,1470],{"class":241},[52,16149,193],{"class":102},[52,16151,16152],{"class":241},"len",[52,16154,193],{"class":102},[52,16156,14723],{"class":189},[52,16158,8886],{"class":102},[639,16160,16162],{"id":16161},"not-handling-the-header-row","Not handling the header row",[14,16164,16165],{},"If your first printed row is:",[42,16167,16169],{"className":44,"code":16168,"language":46,"meta":47,"style":47},"['name', 'age', 'city']\n",[49,16170,16171],{"__ignoreMap":47},[52,16172,16173,16175,16177,16179,16181,16183,16185,16187,16189,16191,16193,16195,16197],{"class":54,"line":55},[52,16174,250],{"class":102},[52,16176,4162],{"class":83},[52,16178,2147],{"class":87},[52,16180,4162],{"class":83},[52,16182,199],{"class":102},[52,16184,4172],{"class":83},[52,16186,2167],{"class":87},[52,16188,4162],{"class":83},[52,16190,199],{"class":102},[52,16192,4172],{"class":83},[52,16194,15026],{"class":87},[52,16196,4162],{"class":83},[52,16198,1147],{"class":102},[14,16200,16201],{},"that is the header, not actual data.",[14,16203,3650,16204,16206],{},[49,16205,15325],{}," if you want to skip it.",[639,16208,16210],{"id":16209},"wrong-file-or-wrong-folder","Wrong file or wrong folder",[14,16212,16213],{},"Sometimes the script runs, but you are reading the wrong CSV file.",[14,16215,16216],{},"This can happen if:",[309,16218,16219,16222,16225],{},[24,16220,16221],{},"the file is in another folder",[24,16223,16224],{},"you have multiple files with similar names",[24,16226,16227],{},"your editor runs the script from a different location",[14,16229,16230],{},"Simple checks like these can help:",[42,16232,16234],{"className":44,"code":16233,"language":46,"meta":47,"style":47},"import os\nprint(os.getcwd())\nprint(os.path.exists('data.csv'))\n",[49,16235,16236,16242,16256],{"__ignoreMap":47},[52,16237,16238,16240],{"class":54,"line":55},[52,16239,59],{"class":58},[52,16241,15839],{"class":62},[52,16243,16244,16246,16248,16250,16252,16254],{"class":54,"line":66},[52,16245,242],{"class":241},[52,16247,193],{"class":102},[52,16249,15848],{"class":189},[52,16251,186],{"class":102},[52,16253,15853],{"class":189},[52,16255,1816],{"class":102},[52,16257,16258,16260,16262,16264,16266,16268,16270,16272,16274,16276,16278,16280],{"class":54,"line":73},[52,16259,242],{"class":241},[52,16261,193],{"class":102},[52,16263,15848],{"class":189},[52,16265,186],{"class":102},[52,16267,15882],{"class":1320},[52,16269,186],{"class":102},[52,16271,15887],{"class":189},[52,16273,193],{"class":102},[52,16275,4162],{"class":83},[52,16277,14642],{"class":87},[52,16279,4162],{"class":83},[52,16281,8886],{"class":102},[639,16283,16285],{"id":16284},"wrong-delimiter","Wrong delimiter",[14,16287,16288],{},"Some CSV-like files use semicolons instead of commas.",[14,16290,16291],{},"For example:",[42,16293,16296],{"className":16294,"code":16295,"language":955,"meta":47},[953],"name;age;city\nAlice;30;London\n",[49,16297,16295],{"__ignoreMap":47},[14,16299,16300,16301,16303],{},"If that happens, ",[49,16302,14747],{}," may not split the row the way you expect.",[14,16305,16306],{},"You would need to pass a delimiter:",[42,16308,16310],{"className":44,"code":16309,"language":46,"meta":47,"style":47},"import csv\n\nwith open('data.csv', newline='', encoding='utf-8') as file:\n    reader = csv.reader(file, delimiter=';')\n    for row in reader:\n        print(row)\n",[49,16311,16312,16318,16322,16364,16396,16408],{"__ignoreMap":47},[52,16313,16314,16316],{"class":54,"line":55},[52,16315,59],{"class":58},[52,16317,14623],{"class":62},[52,16319,16320],{"class":54,"line":66},[52,16321,70],{"emptyLinePlaceholder":69},[52,16323,16324,16326,16328,16330,16332,16334,16336,16338,16340,16342,16344,16346,16348,16350,16352,16354,16356,16358,16360,16362],{"class":54,"line":73},[52,16325,14632],{"class":58},[52,16327,14635],{"class":241},[52,16329,193],{"class":102},[52,16331,4162],{"class":83},[52,16333,14642],{"class":87},[52,16335,4162],{"class":83},[52,16337,199],{"class":102},[52,16339,14649],{"class":202},[52,16341,80],{"class":79},[52,16343,14654],{"class":83},[52,16345,199],{"class":102},[52,16347,14659],{"class":202},[52,16349,80],{"class":79},[52,16351,4162],{"class":83},[52,16353,14666],{"class":87},[52,16355,4162],{"class":83},[52,16357,1521],{"class":102},[52,16359,3567],{"class":58},[52,16361,14676],{"class":14675},[52,16363,1330],{"class":102},[52,16365,16366,16368,16370,16372,16374,16376,16378,16380,16382,16385,16387,16389,16392,16394],{"class":54,"line":94},[52,16367,14683],{"class":62},[52,16369,80],{"class":79},[52,16371,14688],{"class":62},[52,16373,186],{"class":102},[52,16375,14693],{"class":189},[52,16377,193],{"class":102},[52,16379,14698],{"class":14675},[52,16381,199],{"class":102},[52,16383,16384],{"class":202}," delimiter",[52,16386,80],{"class":79},[52,16388,4162],{"class":83},[52,16390,16391],{"class":87},";",[52,16393,4162],{"class":83},[52,16395,211],{"class":102},[52,16397,16398,16400,16402,16404,16406],{"class":54,"line":106},[52,16399,11773],{"class":58},[52,16401,14707],{"class":62},[52,16403,11213],{"class":58},[52,16405,14712],{"class":62},[52,16407,1330],{"class":102},[52,16409,16410,16412,16414,16416],{"class":54,"line":128},[52,16411,1470],{"class":241},[52,16413,193],{"class":102},[52,16415,14723],{"class":189},[52,16417,211],{"class":102},[37,16419,16421],{"id":16420},"when-to-use-this-example-vs-other-pages","When to use this example vs other pages",[14,16423,16424],{},"Use this page when you want:",[309,16426,16427,16430,16433],{},[24,16428,16429],{},"one full working CSV reader example",[24,16431,16432],{},"a quick way to test reading rows",[24,16434,16435],{},"a simple starting point before learning more",[14,16437,16438],{},"Use other pages when you need something more specific:",[309,16440,16441,16446,16449,16454],{},[24,16442,16443,16445],{},[372,16444,14761],{"href":14760}," for step-by-step task guidance",[24,16447,16448],{},"Python csv module overview for a broader look at CSV tools",[24,16450,16451,16453],{},[372,16452,14985],{"href":14984}," for deeper understanding of file opening",[24,16455,16456,16458],{},[372,16457,14989],{"href":14988}," for the bigger picture of working with files",[37,16460,1942],{"id":1941},[639,16462,16464],{"id":16463},"why-does-each-row-look-like-a-list","Why does each row look like a list?",[14,16466,16467,16469],{},[49,16468,14747],{}," returns each row as a list of strings.",[639,16471,16473],{"id":16472},"why-are-numbers-read-as-strings","Why are numbers read as strings?",[14,16475,16476],{},"CSV files store plain text, so Python reads values as strings unless you convert them.",[639,16478,16480],{"id":16479},"how-do-i-skip-the-first-row","How do I skip the first row?",[14,16482,16483,16484,16486],{},"Call ",[49,16485,15325],{}," once before looping through the remaining rows.",[639,16488,16490],{"id":16489},"when-should-i-use-dictreader-instead","When should I use DictReader instead?",[14,16492,3650,16493,16495],{},[49,16494,15550],{}," when you want to access values by column name instead of index position.",[37,16497,2005],{"id":2004},[309,16499,16500,16505,16509,16514,16518,16523,16528],{},[24,16501,16502],{},[372,16503,16504],{"href":14760},"How to read a CSV file in Python",[24,16506,16507],{},[372,16508,14985],{"href":14984},[24,16510,16511],{},[372,16512,16513],{"href":14988},"Python file handling basics: read and write",[24,16515,16516],{},[372,16517,15904],{"href":15903},[24,16519,16520],{},[372,16521,16522],{"href":15907},"How to check if a file exists in Python",[24,16524,16525],{},[372,16526,16527],{"href":15166},"How to convert string to int in Python",[24,16529,16530],{},[372,16531,16533],{"href":16532},"\u002Fexamples\u002Fpython-csv-writer-example\u002F","Python CSV writer example",[14,16535,16536,16537,16539],{},"Try the script with the sample ",[49,16538,14642],{}," file first.",[14,16541,16542,16543,16545],{},"Once that works, move on to ",[372,16544,14761],{"href":14760}," if you want to read specific columns or handle more realistic CSV data.",[2034,16547,16548],{},"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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .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":47,"searchDepth":66,"depth":66,"links":16550},[16551,16552,16553,16556,16557,16558,16559,16567,16568,16574],{"id":2267,"depth":66,"text":2268},{"id":14764,"depth":66,"text":14765},{"id":14786,"depth":66,"text":14787,"children":16554},[16555],{"id":9609,"depth":73,"text":9610},{"id":14992,"depth":66,"text":14993},{"id":15309,"depth":66,"text":15310},{"id":15538,"depth":66,"text":15539},{"id":6401,"depth":66,"text":6402,"children":16560},[16561,16562,16563,16564,16565,16566],{"id":15754,"depth":73,"text":15755},{"id":15911,"depth":73,"text":15912},{"id":15997,"depth":73,"text":15998},{"id":16161,"depth":73,"text":16162},{"id":16209,"depth":73,"text":16210},{"id":16284,"depth":73,"text":16285},{"id":16420,"depth":66,"text":16421},{"id":1941,"depth":66,"text":1942,"children":16569},[16570,16571,16572,16573],{"id":16463,"depth":73,"text":16464},{"id":16472,"depth":73,"text":16473},{"id":16479,"depth":73,"text":16480},{"id":16489,"depth":73,"text":16490},{"id":2004,"depth":66,"text":2005},"Master python csv reader example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-csv-reader-example",{"title":14580,"description":16575},"examples\u002Fpython-csv-reader-example","sZuYy6WZUxMXQTg56nCqkFhBHZQmhsRgts98xiGeYBo",{"id":16582,"title":16583,"body":16584,"description":18354,"extension":2075,"meta":18355,"navigation":69,"path":18356,"seo":18357,"stem":18358,"__hash__":18359},"content\u002Fexamples\u002Fpython-csv-writer-example.md","Python CSV Writer Example",{"type":7,"value":16585,"toc":18316},[16586,16589,16594,16601,16603,16848,16858,16860,16878,16881,16883,16886,17116,17122,17132,17138,17144,17148,17265,17271,17282,17286,17341,17344,17374,17381,17385,17409,17412,17415,17419,17438,17444,17447,17451,17457,17463,17466,17480,17483,17495,17498,17517,17521,17525,17534,17717,17731,17738,17742,17745,17801,17805,17808,17902,17906,17912,17926,17933,17937,17940,17954,17961,17963,17966,17970,17977,17980,17990,17993,18011,18017,18022,18024,18072,18076,18081,18086,18090,18093,18146,18149,18191,18194,18198,18201,18212,18215,18219,18222,18243,18245,18253,18258,18263,18269,18272,18276,18283,18287,18293,18295,18313],[10,16587,16583],{"id":16588},"python-csv-writer-example",[14,16590,16591,16592,13338],{},"This example shows how to write rows to a CSV file in Python with the built-in ",[49,16593,14591],{},[14,16595,16596,16597,16600],{},"You will create a simple file named ",[49,16598,16599],{},"people.csv",", write a header row and some data rows, and see what the finished file looks like. This is a good starting point if you want to export table-like data from a Python script.",[37,16602,40],{"id":39},[42,16604,16606],{"className":44,"code":16605,"language":46,"meta":47,"style":47},"import csv\n\nrows = [\n    [\"name\", \"age\", \"city\"],\n    [\"Alice\", 25, \"London\"],\n    [\"Bob\", 30, \"Paris\"],\n    [\"Charlie\", 22, \"Berlin\"]\n]\n\nwith open(\"people.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as file:\n    writer = csv.writer(file)\n    writer.writerows(rows)\n\nprint(\"CSV file written successfully\")\n",[49,16607,16608,16614,16618,16628,16658,16682,16707,16732,16736,16740,16792,16812,16829,16833],{"__ignoreMap":47},[52,16609,16610,16612],{"class":54,"line":55},[52,16611,59],{"class":58},[52,16613,14623],{"class":62},[52,16615,16616],{"class":54,"line":66},[52,16617,70],{"emptyLinePlaceholder":69},[52,16619,16620,16623,16625],{"class":54,"line":73},[52,16621,16622],{"class":62},"rows ",[52,16624,80],{"class":79},[52,16626,16627],{"class":102}," [\n",[52,16629,16630,16633,16635,16637,16639,16641,16643,16645,16647,16649,16651,16653,16655],{"class":54,"line":94},[52,16631,16632],{"class":102},"    [",[52,16634,115],{"class":83},[52,16636,2147],{"class":87},[52,16638,115],{"class":83},[52,16640,199],{"class":102},[52,16642,84],{"class":83},[52,16644,2167],{"class":87},[52,16646,115],{"class":83},[52,16648,199],{"class":102},[52,16650,84],{"class":83},[52,16652,15026],{"class":87},[52,16654,115],{"class":83},[52,16656,16657],{"class":102},"],\n",[52,16659,16660,16662,16664,16666,16668,16670,16672,16674,16676,16678,16680],{"class":54,"line":106},[52,16661,16632],{"class":102},[52,16663,115],{"class":83},[52,16665,2156],{"class":87},[52,16667,115],{"class":83},[52,16669,199],{"class":102},[52,16671,2674],{"class":121},[52,16673,199],{"class":102},[52,16675,84],{"class":83},[52,16677,15056],{"class":87},[52,16679,115],{"class":83},[52,16681,16657],{"class":102},[52,16683,16684,16686,16688,16690,16692,16694,16697,16699,16701,16703,16705],{"class":54,"line":128},[52,16685,16632],{"class":102},[52,16687,115],{"class":83},[52,16689,10858],{"class":87},[52,16691,115],{"class":83},[52,16693,199],{"class":102},[52,16695,16696],{"class":121}," 30",[52,16698,199],{"class":102},[52,16700,84],{"class":83},[52,16702,15086],{"class":87},[52,16704,115],{"class":83},[52,16706,16657],{"class":102},[52,16708,16709,16711,16713,16715,16717,16719,16722,16724,16726,16728,16730],{"class":54,"line":148},[52,16710,16632],{"class":102},[52,16712,115],{"class":83},[52,16714,15099],{"class":87},[52,16716,115],{"class":83},[52,16718,199],{"class":102},[52,16720,16721],{"class":121}," 22",[52,16723,199],{"class":102},[52,16725,84],{"class":83},[52,16727,15117],{"class":87},[52,16729,115],{"class":83},[52,16731,1147],{"class":102},[52,16733,16734],{"class":54,"line":164},[52,16735,1147],{"class":102},[52,16737,16738],{"class":54,"line":170},[52,16739,70],{"emptyLinePlaceholder":69},[52,16741,16742,16744,16746,16748,16750,16752,16754,16756,16758,16761,16763,16765,16767,16769,16772,16774,16776,16778,16780,16782,16784,16786,16788,16790],{"class":54,"line":175},[52,16743,14632],{"class":58},[52,16745,14635],{"class":241},[52,16747,193],{"class":102},[52,16749,115],{"class":83},[52,16751,16599],{"class":87},[52,16753,115],{"class":83},[52,16755,199],{"class":102},[52,16757,84],{"class":83},[52,16759,16760],{"class":87},"w",[52,16762,115],{"class":83},[52,16764,199],{"class":102},[52,16766,14649],{"class":202},[52,16768,80],{"class":79},[52,16770,16771],{"class":83},"\"\"",[52,16773,199],{"class":102},[52,16775,14659],{"class":202},[52,16777,80],{"class":79},[52,16779,115],{"class":83},[52,16781,14666],{"class":87},[52,16783,115],{"class":83},[52,16785,1521],{"class":102},[52,16787,3567],{"class":58},[52,16789,14676],{"class":14675},[52,16791,1330],{"class":102},[52,16793,16794,16797,16799,16801,16803,16806,16808,16810],{"class":54,"line":214},[52,16795,16796],{"class":62},"    writer ",[52,16798,80],{"class":79},[52,16800,14688],{"class":62},[52,16802,186],{"class":102},[52,16804,16805],{"class":189},"writer",[52,16807,193],{"class":102},[52,16809,14698],{"class":14675},[52,16811,211],{"class":102},[52,16813,16814,16817,16819,16822,16824,16827],{"class":54,"line":233},[52,16815,16816],{"class":62},"    writer",[52,16818,186],{"class":102},[52,16820,16821],{"class":189},"writerows",[52,16823,193],{"class":102},[52,16825,16826],{"class":189},"rows",[52,16828,211],{"class":102},[52,16830,16831],{"class":54,"line":238},[52,16832,70],{"emptyLinePlaceholder":69},[52,16834,16835,16837,16839,16841,16844,16846],{"class":54,"line":272},[52,16836,242],{"class":241},[52,16838,193],{"class":102},[52,16840,115],{"class":83},[52,16842,16843],{"class":87},"CSV file written successfully",[52,16845,115],{"class":83},[52,16847,211],{"class":102},[14,16849,16850,16853,16854,16857],{},[5503,16851,16852],{},"Note:"," Use ",[49,16855,16856],{},"newline=\"\""," when opening the file. This helps avoid blank lines in CSV output on some systems.",[37,16859,323],{"id":322},[309,16861,16862,16867,16870,16875],{},[24,16863,16864,16865],{},"Creates a new CSV file named ",[49,16866,16599],{},[24,16868,16869],{},"Writes a header row and several data rows",[24,16871,16872,16873,14928],{},"Uses Python's built-in ",[49,16874,14591],{},[24,16876,16877],{},"Shows a simple beginner-friendly way to generate CSV output",[14,16879,16880],{},"If you want a broader explanation of CSV tools in Python, see the Python csv module overview.",[37,16882,13312],{"id":13311},[14,16884,16885],{},"Here is the same code again:",[42,16887,16888],{"className":44,"code":16605,"language":46,"meta":47,"style":47},[49,16889,16890,16896,16900,16908,16936,16960,16984,17008,17012,17016,17066,17084,17098,17102],{"__ignoreMap":47},[52,16891,16892,16894],{"class":54,"line":55},[52,16893,59],{"class":58},[52,16895,14623],{"class":62},[52,16897,16898],{"class":54,"line":66},[52,16899,70],{"emptyLinePlaceholder":69},[52,16901,16902,16904,16906],{"class":54,"line":73},[52,16903,16622],{"class":62},[52,16905,80],{"class":79},[52,16907,16627],{"class":102},[52,16909,16910,16912,16914,16916,16918,16920,16922,16924,16926,16928,16930,16932,16934],{"class":54,"line":94},[52,16911,16632],{"class":102},[52,16913,115],{"class":83},[52,16915,2147],{"class":87},[52,16917,115],{"class":83},[52,16919,199],{"class":102},[52,16921,84],{"class":83},[52,16923,2167],{"class":87},[52,16925,115],{"class":83},[52,16927,199],{"class":102},[52,16929,84],{"class":83},[52,16931,15026],{"class":87},[52,16933,115],{"class":83},[52,16935,16657],{"class":102},[52,16937,16938,16940,16942,16944,16946,16948,16950,16952,16954,16956,16958],{"class":54,"line":106},[52,16939,16632],{"class":102},[52,16941,115],{"class":83},[52,16943,2156],{"class":87},[52,16945,115],{"class":83},[52,16947,199],{"class":102},[52,16949,2674],{"class":121},[52,16951,199],{"class":102},[52,16953,84],{"class":83},[52,16955,15056],{"class":87},[52,16957,115],{"class":83},[52,16959,16657],{"class":102},[52,16961,16962,16964,16966,16968,16970,16972,16974,16976,16978,16980,16982],{"class":54,"line":128},[52,16963,16632],{"class":102},[52,16965,115],{"class":83},[52,16967,10858],{"class":87},[52,16969,115],{"class":83},[52,16971,199],{"class":102},[52,16973,16696],{"class":121},[52,16975,199],{"class":102},[52,16977,84],{"class":83},[52,16979,15086],{"class":87},[52,16981,115],{"class":83},[52,16983,16657],{"class":102},[52,16985,16986,16988,16990,16992,16994,16996,16998,17000,17002,17004,17006],{"class":54,"line":148},[52,16987,16632],{"class":102},[52,16989,115],{"class":83},[52,16991,15099],{"class":87},[52,16993,115],{"class":83},[52,16995,199],{"class":102},[52,16997,16721],{"class":121},[52,16999,199],{"class":102},[52,17001,84],{"class":83},[52,17003,15117],{"class":87},[52,17005,115],{"class":83},[52,17007,1147],{"class":102},[52,17009,17010],{"class":54,"line":164},[52,17011,1147],{"class":102},[52,17013,17014],{"class":54,"line":170},[52,17015,70],{"emptyLinePlaceholder":69},[52,17017,17018,17020,17022,17024,17026,17028,17030,17032,17034,17036,17038,17040,17042,17044,17046,17048,17050,17052,17054,17056,17058,17060,17062,17064],{"class":54,"line":175},[52,17019,14632],{"class":58},[52,17021,14635],{"class":241},[52,17023,193],{"class":102},[52,17025,115],{"class":83},[52,17027,16599],{"class":87},[52,17029,115],{"class":83},[52,17031,199],{"class":102},[52,17033,84],{"class":83},[52,17035,16760],{"class":87},[52,17037,115],{"class":83},[52,17039,199],{"class":102},[52,17041,14649],{"class":202},[52,17043,80],{"class":79},[52,17045,16771],{"class":83},[52,17047,199],{"class":102},[52,17049,14659],{"class":202},[52,17051,80],{"class":79},[52,17053,115],{"class":83},[52,17055,14666],{"class":87},[52,17057,115],{"class":83},[52,17059,1521],{"class":102},[52,17061,3567],{"class":58},[52,17063,14676],{"class":14675},[52,17065,1330],{"class":102},[52,17067,17068,17070,17072,17074,17076,17078,17080,17082],{"class":54,"line":214},[52,17069,16796],{"class":62},[52,17071,80],{"class":79},[52,17073,14688],{"class":62},[52,17075,186],{"class":102},[52,17077,16805],{"class":189},[52,17079,193],{"class":102},[52,17081,14698],{"class":14675},[52,17083,211],{"class":102},[52,17085,17086,17088,17090,17092,17094,17096],{"class":54,"line":233},[52,17087,16816],{"class":62},[52,17089,186],{"class":102},[52,17091,16821],{"class":189},[52,17093,193],{"class":102},[52,17095,16826],{"class":189},[52,17097,211],{"class":102},[52,17099,17100],{"class":54,"line":238},[52,17101,70],{"emptyLinePlaceholder":69},[52,17103,17104,17106,17108,17110,17112,17114],{"class":54,"line":272},[52,17105,242],{"class":241},[52,17107,193],{"class":102},[52,17109,115],{"class":83},[52,17111,16843],{"class":87},[52,17113,115],{"class":83},[52,17115,211],{"class":102},[639,17117,17119,17120],{"id":17118},"_1-import-csv","1. Import ",[49,17121,14591],{},[42,17123,17124],{"className":44,"code":15986,"language":46,"meta":47,"style":47},[49,17125,17126],{"__ignoreMap":47},[52,17127,17128,17130],{"class":54,"line":55},[52,17129,59],{"class":58},[52,17131,14623],{"class":62},[14,17133,17134,17135,186],{},"This imports Python's built-in CSV module so you can use ",[49,17136,17137],{},"csv.writer()",[14,17139,17140,17141,17143],{},"If you forget this line, Python will not know what ",[49,17142,14591],{}," means.",[639,17145,17147],{"id":17146},"_2-store-the-data-as-rows","2. Store the data as rows",[42,17149,17151],{"className":44,"code":17150,"language":46,"meta":47,"style":47},"rows = [\n    [\"name\", \"age\", \"city\"],\n    [\"Alice\", 25, \"London\"],\n    [\"Bob\", 30, \"Paris\"],\n    [\"Charlie\", 22, \"Berlin\"]\n]\n",[49,17152,17153,17161,17189,17213,17237,17261],{"__ignoreMap":47},[52,17154,17155,17157,17159],{"class":54,"line":55},[52,17156,16622],{"class":62},[52,17158,80],{"class":79},[52,17160,16627],{"class":102},[52,17162,17163,17165,17167,17169,17171,17173,17175,17177,17179,17181,17183,17185,17187],{"class":54,"line":66},[52,17164,16632],{"class":102},[52,17166,115],{"class":83},[52,17168,2147],{"class":87},[52,17170,115],{"class":83},[52,17172,199],{"class":102},[52,17174,84],{"class":83},[52,17176,2167],{"class":87},[52,17178,115],{"class":83},[52,17180,199],{"class":102},[52,17182,84],{"class":83},[52,17184,15026],{"class":87},[52,17186,115],{"class":83},[52,17188,16657],{"class":102},[52,17190,17191,17193,17195,17197,17199,17201,17203,17205,17207,17209,17211],{"class":54,"line":73},[52,17192,16632],{"class":102},[52,17194,115],{"class":83},[52,17196,2156],{"class":87},[52,17198,115],{"class":83},[52,17200,199],{"class":102},[52,17202,2674],{"class":121},[52,17204,199],{"class":102},[52,17206,84],{"class":83},[52,17208,15056],{"class":87},[52,17210,115],{"class":83},[52,17212,16657],{"class":102},[52,17214,17215,17217,17219,17221,17223,17225,17227,17229,17231,17233,17235],{"class":54,"line":94},[52,17216,16632],{"class":102},[52,17218,115],{"class":83},[52,17220,10858],{"class":87},[52,17222,115],{"class":83},[52,17224,199],{"class":102},[52,17226,16696],{"class":121},[52,17228,199],{"class":102},[52,17230,84],{"class":83},[52,17232,15086],{"class":87},[52,17234,115],{"class":83},[52,17236,16657],{"class":102},[52,17238,17239,17241,17243,17245,17247,17249,17251,17253,17255,17257,17259],{"class":54,"line":106},[52,17240,16632],{"class":102},[52,17242,115],{"class":83},[52,17244,15099],{"class":87},[52,17246,115],{"class":83},[52,17248,199],{"class":102},[52,17250,16721],{"class":121},[52,17252,199],{"class":102},[52,17254,84],{"class":83},[52,17256,15117],{"class":87},[52,17258,115],{"class":83},[52,17260,1147],{"class":102},[52,17262,17263],{"class":54,"line":128},[52,17264,1147],{"class":102},[14,17266,17267,17268,118],{},"This is a ",[5503,17269,17270],{},"list of lists",[309,17272,17273,17276,17279],{},[24,17274,17275],{},"Each inner list is one row",[24,17277,17278],{},"The first row is the header",[24,17280,17281],{},"The remaining rows are the data",[639,17283,17285],{"id":17284},"_3-open-the-file-in-write-mode","3. Open the file in write mode",[42,17287,17289],{"className":44,"code":17288,"language":46,"meta":47,"style":47},"with open(\"people.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as file:\n",[49,17290,17291],{"__ignoreMap":47},[52,17292,17293,17295,17297,17299,17301,17303,17305,17307,17309,17311,17313,17315,17317,17319,17321,17323,17325,17327,17329,17331,17333,17335,17337,17339],{"class":54,"line":55},[52,17294,14632],{"class":58},[52,17296,14635],{"class":241},[52,17298,193],{"class":102},[52,17300,115],{"class":83},[52,17302,16599],{"class":87},[52,17304,115],{"class":83},[52,17306,199],{"class":102},[52,17308,84],{"class":83},[52,17310,16760],{"class":87},[52,17312,115],{"class":83},[52,17314,199],{"class":102},[52,17316,14649],{"class":202},[52,17318,80],{"class":79},[52,17320,16771],{"class":83},[52,17322,199],{"class":102},[52,17324,14659],{"class":202},[52,17326,80],{"class":79},[52,17328,115],{"class":83},[52,17330,14666],{"class":87},[52,17332,115],{"class":83},[52,17334,1521],{"class":102},[52,17336,3567],{"class":58},[52,17338,14676],{"class":14675},[52,17340,1330],{"class":102},[14,17342,17343],{},"This line does several important things:",[309,17345,17346,17352,17358,17363,17369],{},[24,17347,17348,17351],{},[49,17349,17350],{},"\"people.csv\""," is the file name",[24,17353,17354,17357],{},[49,17355,17356],{},"\"w\""," means write mode",[24,17359,17360,17362],{},[49,17361,16856],{}," helps prevent extra blank lines",[24,17364,17365,17368],{},[49,17366,17367],{},"encoding=\"utf-8\""," is a good default for text files",[24,17370,17371,17373],{},[49,17372,14741],{}," automatically closes the file when done",[14,17375,17376,17377,1727,17379,186],{},"If you are new to ",[49,17378,14980],{},[372,17380,14985],{"href":14984},[639,17382,17384],{"id":17383},"_4-create-a-csv-writer","4. Create a CSV writer",[42,17386,17388],{"className":44,"code":17387,"language":46,"meta":47,"style":47},"writer = csv.writer(file)\n",[49,17389,17390],{"__ignoreMap":47},[52,17391,17392,17395,17397,17399,17401,17403,17405,17407],{"class":54,"line":55},[52,17393,17394],{"class":62},"writer ",[52,17396,80],{"class":79},[52,17398,14688],{"class":62},[52,17400,186],{"class":102},[52,17402,16805],{"class":189},[52,17404,193],{"class":102},[52,17406,14698],{"class":14675},[52,17408,211],{"class":102},[14,17410,17411],{},"This creates a writer object connected to the file.",[14,17413,17414],{},"You use this writer to send rows into the CSV file.",[639,17416,17418],{"id":17417},"_5-write-all-rows-at-once","5. Write all rows at once",[42,17420,17422],{"className":44,"code":17421,"language":46,"meta":47,"style":47},"writer.writerows(rows)\n",[49,17423,17424],{"__ignoreMap":47},[52,17425,17426,17428,17430,17432,17434,17436],{"class":54,"line":55},[52,17427,16805],{"class":62},[52,17429,186],{"class":102},[52,17431,16821],{"class":189},[52,17433,193],{"class":102},[52,17435,16826],{"class":189},[52,17437,211],{"class":102},[14,17439,17440,17441,17443],{},"This writes every row from the ",[49,17442,16826],{}," list into the file.",[14,17445,17446],{},"After that, the file is ready to use.",[37,17448,17450],{"id":17449},"expected-file-content","Expected file content",[14,17452,17453,17454,17456],{},"After running the script, the file ",[49,17455,16599],{}," should contain:",[42,17458,17461],{"className":17459,"code":17460,"language":955,"meta":47},[953],"name,age,city\nAlice,25,London\nBob,30,Paris\nCharlie,22,Berlin\n",[49,17462,17460],{"__ignoreMap":47},[14,17464,17465],{},"You can open this file in:",[309,17467,17468,17471,17474,17477],{},[24,17469,17470],{},"A text editor",[24,17472,17473],{},"Excel",[24,17475,17476],{},"Google Sheets",[24,17478,17479],{},"Other spreadsheet programs",[14,17481,17482],{},"To run the script, you can use:",[42,17484,17486],{"className":390,"code":17485,"language":392,"meta":47,"style":47},"python your_script.py\n",[49,17487,17488],{"__ignoreMap":47},[52,17489,17490,17492],{"class":54,"line":55},[52,17491,46],{"class":399},[52,17493,17494],{"class":87}," your_script.py\n",[14,17496,17497],{},"Then check the file content with one of these commands:",[42,17499,17501],{"className":390,"code":17500,"language":392,"meta":47,"style":47},"type people.csv\ncat people.csv\n",[49,17502,17503,17510],{"__ignoreMap":47},[52,17504,17505,17507],{"class":54,"line":55},[52,17506,4198],{"class":241},[52,17508,17509],{"class":87}," people.csv\n",[52,17511,17512,17515],{"class":54,"line":66},[52,17513,17514],{"class":399},"cat",[52,17516,17509],{"class":87},[37,17518,17520],{"id":17519},"useful-beginner-variations","Useful beginner variations",[639,17522,17524],{"id":17523},"write-one-row-at-a-time","Write one row at a time",[14,17526,17527,17528,10598,17531,186],{},"If you want to write rows one by one, use ",[49,17529,17530],{},"writer.writerow(...)",[49,17532,17533],{},"writer.writerows(...)",[42,17535,17537],{"className":44,"code":17536,"language":46,"meta":47,"style":47},"import csv\n\nwith open(\"people.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as file:\n    writer = csv.writer(file)\n\n    writer.writerow([\"name\", \"age\", \"city\"])\n    writer.writerow([\"Alice\", 25, \"London\"])\n    writer.writerow([\"Bob\", 30, \"Paris\"])\n",[49,17538,17539,17545,17549,17599,17617,17621,17657,17687],{"__ignoreMap":47},[52,17540,17541,17543],{"class":54,"line":55},[52,17542,59],{"class":58},[52,17544,14623],{"class":62},[52,17546,17547],{"class":54,"line":66},[52,17548,70],{"emptyLinePlaceholder":69},[52,17550,17551,17553,17555,17557,17559,17561,17563,17565,17567,17569,17571,17573,17575,17577,17579,17581,17583,17585,17587,17589,17591,17593,17595,17597],{"class":54,"line":73},[52,17552,14632],{"class":58},[52,17554,14635],{"class":241},[52,17556,193],{"class":102},[52,17558,115],{"class":83},[52,17560,16599],{"class":87},[52,17562,115],{"class":83},[52,17564,199],{"class":102},[52,17566,84],{"class":83},[52,17568,16760],{"class":87},[52,17570,115],{"class":83},[52,17572,199],{"class":102},[52,17574,14649],{"class":202},[52,17576,80],{"class":79},[52,17578,16771],{"class":83},[52,17580,199],{"class":102},[52,17582,14659],{"class":202},[52,17584,80],{"class":79},[52,17586,115],{"class":83},[52,17588,14666],{"class":87},[52,17590,115],{"class":83},[52,17592,1521],{"class":102},[52,17594,3567],{"class":58},[52,17596,14676],{"class":14675},[52,17598,1330],{"class":102},[52,17600,17601,17603,17605,17607,17609,17611,17613,17615],{"class":54,"line":94},[52,17602,16796],{"class":62},[52,17604,80],{"class":79},[52,17606,14688],{"class":62},[52,17608,186],{"class":102},[52,17610,16805],{"class":189},[52,17612,193],{"class":102},[52,17614,14698],{"class":14675},[52,17616,211],{"class":102},[52,17618,17619],{"class":54,"line":106},[52,17620,70],{"emptyLinePlaceholder":69},[52,17622,17623,17625,17627,17630,17633,17635,17637,17639,17641,17643,17645,17647,17649,17651,17653,17655],{"class":54,"line":128},[52,17624,16816],{"class":62},[52,17626,186],{"class":102},[52,17628,17629],{"class":189},"writerow",[52,17631,17632],{"class":102},"([",[52,17634,115],{"class":83},[52,17636,2147],{"class":87},[52,17638,115],{"class":83},[52,17640,199],{"class":102},[52,17642,84],{"class":83},[52,17644,2167],{"class":87},[52,17646,115],{"class":83},[52,17648,199],{"class":102},[52,17650,84],{"class":83},[52,17652,15026],{"class":87},[52,17654,115],{"class":83},[52,17656,269],{"class":102},[52,17658,17659,17661,17663,17665,17667,17669,17671,17673,17675,17677,17679,17681,17683,17685],{"class":54,"line":148},[52,17660,16816],{"class":62},[52,17662,186],{"class":102},[52,17664,17629],{"class":189},[52,17666,17632],{"class":102},[52,17668,115],{"class":83},[52,17670,2156],{"class":87},[52,17672,115],{"class":83},[52,17674,199],{"class":102},[52,17676,2674],{"class":121},[52,17678,199],{"class":102},[52,17680,84],{"class":83},[52,17682,15056],{"class":87},[52,17684,115],{"class":83},[52,17686,269],{"class":102},[52,17688,17689,17691,17693,17695,17697,17699,17701,17703,17705,17707,17709,17711,17713,17715],{"class":54,"line":164},[52,17690,16816],{"class":62},[52,17692,186],{"class":102},[52,17694,17629],{"class":189},[52,17696,17632],{"class":102},[52,17698,115],{"class":83},[52,17700,10858],{"class":87},[52,17702,115],{"class":83},[52,17704,199],{"class":102},[52,17706,16696],{"class":121},[52,17708,199],{"class":102},[52,17710,84],{"class":83},[52,17712,15086],{"class":87},[52,17714,115],{"class":83},[52,17716,269],{"class":102},[309,17718,17719,17725],{},[24,17720,17721,17724],{},[49,17722,17723],{},"writerow()"," writes one row",[24,17726,17727,17730],{},[49,17728,17729],{},"writerows()"," writes many rows",[14,17732,17733,17734,186],{},"If you want a step-by-step guide, see ",[372,17735,17737],{"href":17736},"\u002Fhow-to\u002Fhow-to-write-a-csv-file-in-python\u002F","How to write a CSV file in Python",[639,17739,17741],{"id":17740},"change-the-file-name","Change the file name",[14,17743,17744],{},"You can create a different CSV file by changing the file name:",[42,17746,17748],{"className":44,"code":17747,"language":46,"meta":47,"style":47},"with open(\"students.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as file:\n",[49,17749,17750],{"__ignoreMap":47},[52,17751,17752,17754,17756,17758,17760,17763,17765,17767,17769,17771,17773,17775,17777,17779,17781,17783,17785,17787,17789,17791,17793,17795,17797,17799],{"class":54,"line":55},[52,17753,14632],{"class":58},[52,17755,14635],{"class":241},[52,17757,193],{"class":102},[52,17759,115],{"class":83},[52,17761,17762],{"class":87},"students.csv",[52,17764,115],{"class":83},[52,17766,199],{"class":102},[52,17768,84],{"class":83},[52,17770,16760],{"class":87},[52,17772,115],{"class":83},[52,17774,199],{"class":102},[52,17776,14649],{"class":202},[52,17778,80],{"class":79},[52,17780,16771],{"class":83},[52,17782,199],{"class":102},[52,17784,14659],{"class":202},[52,17786,80],{"class":79},[52,17788,115],{"class":83},[52,17790,14666],{"class":87},[52,17792,115],{"class":83},[52,17794,1521],{"class":102},[52,17796,3567],{"class":58},[52,17798,14676],{"class":14675},[52,17800,1330],{"class":102},[639,17802,17804],{"id":17803},"replace-the-example-data","Replace the example data",[14,17806,17807],{},"You can swap the sample rows with your own data:",[42,17809,17811],{"className":44,"code":17810,"language":46,"meta":47,"style":47},"rows = [\n    [\"product\", \"price\", \"stock\"],\n    [\"Pen\", 1.5, 100],\n    [\"Notebook\", 3.0, 50]\n]\n",[49,17812,17813,17821,17852,17875,17898],{"__ignoreMap":47},[52,17814,17815,17817,17819],{"class":54,"line":55},[52,17816,16622],{"class":62},[52,17818,80],{"class":79},[52,17820,16627],{"class":102},[52,17822,17823,17825,17827,17830,17832,17834,17836,17839,17841,17843,17845,17848,17850],{"class":54,"line":66},[52,17824,16632],{"class":102},[52,17826,115],{"class":83},[52,17828,17829],{"class":87},"product",[52,17831,115],{"class":83},[52,17833,199],{"class":102},[52,17835,84],{"class":83},[52,17837,17838],{"class":87},"price",[52,17840,115],{"class":83},[52,17842,199],{"class":102},[52,17844,84],{"class":83},[52,17846,17847],{"class":87},"stock",[52,17849,115],{"class":83},[52,17851,16657],{"class":102},[52,17853,17854,17856,17858,17861,17863,17865,17868,17870,17873],{"class":54,"line":73},[52,17855,16632],{"class":102},[52,17857,115],{"class":83},[52,17859,17860],{"class":87},"Pen",[52,17862,115],{"class":83},[52,17864,199],{"class":102},[52,17866,17867],{"class":121}," 1.5",[52,17869,199],{"class":102},[52,17871,17872],{"class":121}," 100",[52,17874,16657],{"class":102},[52,17876,17877,17879,17881,17884,17886,17888,17891,17893,17896],{"class":54,"line":94},[52,17878,16632],{"class":102},[52,17880,115],{"class":83},[52,17882,17883],{"class":87},"Notebook",[52,17885,115],{"class":83},[52,17887,199],{"class":102},[52,17889,17890],{"class":121}," 3.0",[52,17892,199],{"class":102},[52,17894,17895],{"class":121}," 50",[52,17897,1147],{"class":102},[52,17899,17900],{"class":54,"line":106},[52,17901,1147],{"class":102},[639,17903,17905],{"id":17904},"write-data-from-another-part-of-your-program","Write data from another part of your program",[14,17907,17908,17909,17911],{},"You can build the ",[49,17910,16826],{}," list from:",[309,17913,17914,17917,17920,17923],{},[24,17915,17916],{},"User input",[24,17918,17919],{},"Calculations",[24,17921,17922],{},"API results",[24,17924,17925],{},"Data read from another file",[14,17927,17928,17929,186],{},"If you want to add new rows without replacing the old file, see ",[372,17930,17932],{"href":17931},"\u002Fhow-to\u002Fhow-to-append-to-a-file-in-python\u002F","How to append to a file in Python",[37,17934,17936],{"id":17935},"when-this-example-is-useful","When this example is useful",[14,17938,17939],{},"This example is helpful when you need to:",[309,17941,17942,17945,17948,17951],{},[24,17943,17944],{},"Save simple table data to a file",[24,17946,17947],{},"Export results from a Python script",[24,17949,17950],{},"Create spreadsheet-friendly output",[24,17952,17953],{},"Learn the difference between plain text files and structured CSV files",[14,17955,17956,17957,186],{},"A matching next step is reading CSV files back into Python. For that, see the ",[372,17958,17960],{"href":17959},"\u002Fexamples\u002Fpython-csv-reader-example\u002F","Python CSV reader example",[37,17962,12322],{"id":12321},[14,17964,17965],{},"Here are some common problems beginners run into.",[639,17967,14067,17968],{"id":15911},[49,17969,14591],{},[14,17971,6446,17972,14074,17974,17976],{},[49,17973,17137],{},[49,17975,14591],{},", Python will raise an error.",[14,17978,17979],{},"Make sure this line is at the top:",[42,17981,17982],{"className":44,"code":15986,"language":46,"meta":47,"style":47},[49,17983,17984],{"__ignoreMap":47},[52,17985,17986,17988],{"class":54,"line":55},[52,17987,59],{"class":58},[52,17989,14623],{"class":62},[14,17991,17992],{},"You can quickly test the module with:",[42,17994,17996],{"className":390,"code":17995,"language":392,"meta":47,"style":47},"python -c \"import csv; print('csv module loaded')\"\n",[49,17997,17998],{"__ignoreMap":47},[52,17999,18000,18002,18004,18006,18009],{"class":54,"line":55},[52,18001,46],{"class":399},[52,18003,443],{"class":420},[52,18005,84],{"class":83},[52,18007,18008],{"class":87},"import csv; print('csv module loaded')",[52,18010,91],{"class":83},[639,18012,18014,18015],{"id":18013},"opening-the-file-without-newline","Opening the file without ",[49,18016,16856],{},[14,18018,5788,18019,18021],{},[49,18020,16856],{},", some systems may create extra blank lines in the CSV output.",[14,18023,10462],{},[42,18025,18027],{"className":44,"code":18026,"language":46,"meta":47,"style":47},"open(\"people.csv\", \"w\", newline=\"\", encoding=\"utf-8\")\n",[49,18028,18029],{"__ignoreMap":47},[52,18030,18031,18034,18036,18038,18040,18042,18044,18046,18048,18050,18052,18054,18056,18058,18060,18062,18064,18066,18068,18070],{"class":54,"line":55},[52,18032,18033],{"class":241},"open",[52,18035,193],{"class":102},[52,18037,115],{"class":83},[52,18039,16599],{"class":87},[52,18041,115],{"class":83},[52,18043,199],{"class":102},[52,18045,84],{"class":83},[52,18047,16760],{"class":87},[52,18049,115],{"class":83},[52,18051,199],{"class":102},[52,18053,14649],{"class":202},[52,18055,80],{"class":79},[52,18057,16771],{"class":83},[52,18059,199],{"class":102},[52,18061,14659],{"class":202},[52,18063,80],{"class":79},[52,18065,115],{"class":83},[52,18067,14666],{"class":87},[52,18069,115],{"class":83},[52,18071,211],{"class":102},[639,18073,18075],{"id":18074},"overwriting-an-existing-file-by-mistake","Overwriting an existing file by mistake",[14,18077,10294,18078,18080],{},[49,18079,17356],{}," replaces the old file content.",[14,18082,7526,18083,18085],{},[49,18084,16599],{}," already exists, it will be overwritten.",[639,18087,18089],{"id":18088},"passing-the-wrong-data-shape","Passing the wrong data shape",[14,18091,18092],{},"This works:",[42,18094,18096],{"className":44,"code":18095,"language":46,"meta":47,"style":47},"rows = [\n    [\"name\", \"age\"],\n    [\"Alice\", 25]\n]\n",[49,18097,18098,18106,18126,18142],{"__ignoreMap":47},[52,18099,18100,18102,18104],{"class":54,"line":55},[52,18101,16622],{"class":62},[52,18103,80],{"class":79},[52,18105,16627],{"class":102},[52,18107,18108,18110,18112,18114,18116,18118,18120,18122,18124],{"class":54,"line":66},[52,18109,16632],{"class":102},[52,18111,115],{"class":83},[52,18113,2147],{"class":87},[52,18115,115],{"class":83},[52,18117,199],{"class":102},[52,18119,84],{"class":83},[52,18121,2167],{"class":87},[52,18123,115],{"class":83},[52,18125,16657],{"class":102},[52,18127,18128,18130,18132,18134,18136,18138,18140],{"class":54,"line":73},[52,18129,16632],{"class":102},[52,18131,115],{"class":83},[52,18133,2156],{"class":87},[52,18135,115],{"class":83},[52,18137,199],{"class":102},[52,18139,2674],{"class":121},[52,18141,1147],{"class":102},[52,18143,18144],{"class":54,"line":94},[52,18145,1147],{"class":102},[14,18147,18148],{},"This is not a proper list of rows:",[42,18150,18152],{"className":44,"code":18151,"language":46,"meta":47,"style":47},"rows = [\"name\", \"age\", \"Alice\", 25]\n",[49,18153,18154],{"__ignoreMap":47},[52,18155,18156,18158,18160,18163,18165,18167,18169,18171,18173,18175,18177,18179,18181,18183,18185,18187,18189],{"class":54,"line":55},[52,18157,16622],{"class":62},[52,18159,80],{"class":79},[52,18161,18162],{"class":102}," [",[52,18164,115],{"class":83},[52,18166,2147],{"class":87},[52,18168,115],{"class":83},[52,18170,199],{"class":102},[52,18172,84],{"class":83},[52,18174,2167],{"class":87},[52,18176,115],{"class":83},[52,18178,199],{"class":102},[52,18180,84],{"class":83},[52,18182,2156],{"class":87},[52,18184,115],{"class":83},[52,18186,199],{"class":102},[52,18188,2674],{"class":121},[52,18190,1147],{"class":102},[14,18192,18193],{},"A CSV writer expects each row to be a separate list or iterable.",[639,18195,18197],{"id":18196},"trying-to-write-unsupported-custom-objects","Trying to write unsupported custom objects",[14,18199,18200],{},"CSV files work best with simple values such as:",[309,18202,18203,18206,18209],{},[24,18204,18205],{},"Strings",[24,18207,18208],{},"Integers",[24,18210,18211],{},"Floats",[14,18213,18214],{},"If you try to write custom objects directly, the output may not be what you expect. Convert values to simple text or numbers first.",[639,18216,18218],{"id":18217},"file-and-permission-problems","File and permission problems",[14,18220,18221],{},"If Python cannot create or write the file, you may see an error such as:",[309,18223,18224,18233],{},[24,18225,18226],{},[372,18227,18228,18229,18232],{"href":15903},"FileNotFoundError: ",[52,18230,18231],{},"Errno 2"," No such file or directory",[24,18234,18235],{},[372,18236,18238,18239,18242],{"href":18237},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix\u002F","PermissionError: ",[52,18240,18241],{},"Errno 13"," Permission denied",[37,18244,1942],{"id":1941},[639,18246,18248,18249,759,18251,1995],{"id":18247},"what-is-the-difference-between-writerow-and-writerows","What is the difference between ",[49,18250,17723],{},[49,18252,17729],{},[14,18254,18255,18257],{},[49,18256,17723],{}," writes one row.",[14,18259,18260,18262],{},[49,18261,17729],{}," writes multiple rows from a list or other iterable.",[639,18264,1956,18266,18268],{"id":18265},"why-use-newline-when-opening-a-csv-file",[49,18267,16856],{}," when opening a CSV file?",[14,18270,18271],{},"It prevents extra blank lines from appearing in the output on some operating systems.",[639,18273,18275],{"id":18274},"can-i-write-dictionaries-to-a-csv-file","Can I write dictionaries to a CSV file?",[14,18277,18278,18279,18282],{},"Yes, but that is usually done with ",[49,18280,18281],{},"csv.DictWriter",", which is better covered separately than in this basic example.",[639,18284,18286],{"id":18285},"will-this-overwrite-an-existing-file","Will this overwrite an existing file?",[14,18288,18289,18290,18292],{},"Yes. Opening a file with ",[49,18291,17356],{}," replaces the old content. Use append mode if you want to add rows instead.",[37,18294,2005],{"id":2004},[309,18296,18297,18301,18305,18309],{},[24,18298,18299],{},[372,18300,17737],{"href":17736},[24,18302,18303],{},[372,18304,14985],{"href":14984},[24,18306,18307],{},[372,18308,17932],{"href":17931},[24,18310,18311],{},[372,18312,17960],{"href":17959},[2034,18314,18315],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .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 .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}",{"title":47,"searchDepth":66,"depth":66,"links":18317},[18318,18319,18320,18328,18329,18335,18336,18345,18353],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":13311,"depth":66,"text":13312,"children":18321},[18322,18324,18325,18326,18327],{"id":17118,"depth":73,"text":18323},"1. Import csv",{"id":17146,"depth":73,"text":17147},{"id":17284,"depth":73,"text":17285},{"id":17383,"depth":73,"text":17384},{"id":17417,"depth":73,"text":17418},{"id":17449,"depth":66,"text":17450},{"id":17519,"depth":66,"text":17520,"children":18330},[18331,18332,18333,18334],{"id":17523,"depth":73,"text":17524},{"id":17740,"depth":73,"text":17741},{"id":17803,"depth":73,"text":17804},{"id":17904,"depth":73,"text":17905},{"id":17935,"depth":66,"text":17936},{"id":12321,"depth":66,"text":12322,"children":18337},[18338,18339,18341,18342,18343,18344],{"id":15911,"depth":73,"text":15912},{"id":18013,"depth":73,"text":18340},"Opening the file without newline=\"\"",{"id":18074,"depth":73,"text":18075},{"id":18088,"depth":73,"text":18089},{"id":18196,"depth":73,"text":18197},{"id":18217,"depth":73,"text":18218},{"id":1941,"depth":66,"text":1942,"children":18346},[18347,18349,18351,18352],{"id":18247,"depth":73,"text":18348},"What is the difference between writerow() and writerows()?",{"id":18265,"depth":73,"text":18350},"Why use newline=\"\" when opening a CSV file?",{"id":18274,"depth":73,"text":18275},{"id":18285,"depth":73,"text":18286},{"id":2004,"depth":66,"text":2005},"Master python csv writer example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-csv-writer-example",{"title":16583,"description":18354},"examples\u002Fpython-csv-writer-example","heoZ5PcqN6LowKTgQeDWlp4x3f_Vi88qZN7nhRAW5zQ",{"id":18361,"title":18362,"body":18363,"description":20836,"extension":2075,"meta":20837,"navigation":69,"path":20838,"seo":20839,"stem":20840,"__hash__":20841},"content\u002Fexamples\u002Fpython-data-cleaning-script-example.md","Python Data Cleaning Script Example",{"type":7,"value":18364,"toc":20816},[18365,18368,18371,18374,18391,18394,18396,18399,18813,18815,18818,18853,18857,18860,19044,19047,19061,19064,19068,19073,19242,19244,19264,19274,19281,19285,19291,19329,19331,19340,19343,19345,19362,19365,19372,19378,19382,19385,19436,19438,19447,19452,19455,19466,19470,19475,19506,19508,19528,19536,19579,19581,19604,19607,19618,19633,19637,19640,20022,20025,20047,20050,20052,20055,20205,20208,20224,20228,20231,20250,20253,20597,20609,20611,20614,20630,20633,20714,20717,20751,20753,20757,20760,20764,20769,20773,20779,20783,20786,20788,20813],[10,18366,18362],{"id":18367},"python-data-cleaning-script-example",[14,18369,18370],{},"This beginner-friendly example shows one practical Python data cleaning script.",[14,18372,18373],{},"You will start with messy data stored as a list of dictionaries, then clean it by:",[309,18375,18376,18379,18382,18385,18388],{},[24,18377,18378],{},"removing extra spaces",[24,18380,18381],{},"standardizing text",[24,18383,18384],{},"handling missing values",[24,18386,18387],{},"converting strings to numbers",[24,18389,18390],{},"building a new clean result",[14,18392,18393],{},"This is a useful pattern when you want to clean data before saving it, analyzing it, or using it in another part of your program.",[37,18395,40],{"id":39},[14,18397,18398],{},"Use this when you want a simple example of cleaning list-of-dictionary data before saving or using it.",[42,18400,18402],{"className":44,"code":18401,"language":46,"meta":47,"style":47},"raw_data = [\n    {\"name\": \" Alice \", \"age\": \"25\", \"city\": \"new york\"},\n    {\"name\": \"Bob\", \"age\": \"\", \"city\": \" london \"},\n    {\"name\": \"  Cara\", \"age\": \"31\", \"city\": \"PARIS\"}\n]\n\ncleaned_data = []\n\nfor row in raw_data:\n    name = row[\"name\"].strip()\n    age_text = row[\"age\"].strip()\n    city = row[\"city\"].strip().title()\n\n    if not name:\n        continue\n\n    age = int(age_text) if age_text else None\n\n    cleaned_data.append({\n        \"name\": name,\n        \"age\": age,\n        \"city\": city\n    })\n\nprint(cleaned_data)\n",[49,18403,18404,18413,18469,18518,18573,18577,18581,18591,18595,18608,18633,18656,18685,18689,18699,18704,18708,18735,18739,18752,18766,18780,18793,18798,18802],{"__ignoreMap":47},[52,18405,18406,18409,18411],{"class":54,"line":55},[52,18407,18408],{"class":62},"raw_data ",[52,18410,80],{"class":79},[52,18412,16627],{"class":102},[52,18414,18415,18418,18420,18422,18424,18426,18428,18431,18433,18435,18437,18439,18441,18443,18445,18447,18449,18451,18453,18455,18457,18459,18461,18464,18466],{"class":54,"line":66},[52,18416,18417],{"class":102},"    {",[52,18419,115],{"class":83},[52,18421,2147],{"class":87},[52,18423,115],{"class":83},[52,18425,118],{"class":102},[52,18427,84],{"class":83},[52,18429,18430],{"class":87}," Alice ",[52,18432,115],{"class":83},[52,18434,199],{"class":102},[52,18436,84],{"class":83},[52,18438,2167],{"class":87},[52,18440,115],{"class":83},[52,18442,118],{"class":102},[52,18444,84],{"class":83},[52,18446,15077],{"class":87},[52,18448,115],{"class":83},[52,18450,199],{"class":102},[52,18452,84],{"class":83},[52,18454,15026],{"class":87},[52,18456,115],{"class":83},[52,18458,118],{"class":102},[52,18460,84],{"class":83},[52,18462,18463],{"class":87},"new york",[52,18465,115],{"class":83},[52,18467,18468],{"class":102},"},\n",[52,18470,18471,18473,18475,18477,18479,18481,18483,18485,18487,18489,18491,18493,18495,18497,18499,18501,18503,18505,18507,18509,18511,18514,18516],{"class":54,"line":73},[52,18472,18417],{"class":102},[52,18474,115],{"class":83},[52,18476,2147],{"class":87},[52,18478,115],{"class":83},[52,18480,118],{"class":102},[52,18482,84],{"class":83},[52,18484,10858],{"class":87},[52,18486,115],{"class":83},[52,18488,199],{"class":102},[52,18490,84],{"class":83},[52,18492,2167],{"class":87},[52,18494,115],{"class":83},[52,18496,118],{"class":102},[52,18498,3448],{"class":83},[52,18500,199],{"class":102},[52,18502,84],{"class":83},[52,18504,15026],{"class":87},[52,18506,115],{"class":83},[52,18508,118],{"class":102},[52,18510,84],{"class":83},[52,18512,18513],{"class":87}," london ",[52,18515,115],{"class":83},[52,18517,18468],{"class":102},[52,18519,18520,18522,18524,18526,18528,18530,18532,18535,18537,18539,18541,18543,18545,18547,18549,18552,18554,18556,18558,18560,18562,18564,18566,18569,18571],{"class":54,"line":94},[52,18521,18417],{"class":102},[52,18523,115],{"class":83},[52,18525,2147],{"class":87},[52,18527,115],{"class":83},[52,18529,118],{"class":102},[52,18531,84],{"class":83},[52,18533,18534],{"class":87},"  Cara",[52,18536,115],{"class":83},[52,18538,199],{"class":102},[52,18540,84],{"class":83},[52,18542,2167],{"class":87},[52,18544,115],{"class":83},[52,18546,118],{"class":102},[52,18548,84],{"class":83},[52,18550,18551],{"class":87},"31",[52,18553,115],{"class":83},[52,18555,199],{"class":102},[52,18557,84],{"class":83},[52,18559,15026],{"class":87},[52,18561,115],{"class":83},[52,18563,118],{"class":102},[52,18565,84],{"class":83},[52,18567,18568],{"class":87},"PARIS",[52,18570,115],{"class":83},[52,18572,167],{"class":102},[52,18574,18575],{"class":54,"line":106},[52,18576,1147],{"class":102},[52,18578,18579],{"class":54,"line":128},[52,18580,70],{"emptyLinePlaceholder":69},[52,18582,18583,18586,18588],{"class":54,"line":148},[52,18584,18585],{"class":62},"cleaned_data ",[52,18587,80],{"class":79},[52,18589,18590],{"class":102}," []\n",[52,18592,18593],{"class":54,"line":164},[52,18594,70],{"emptyLinePlaceholder":69},[52,18596,18597,18599,18601,18603,18606],{"class":54,"line":170},[52,18598,12265],{"class":58},[52,18600,14707],{"class":62},[52,18602,11213],{"class":58},[52,18604,18605],{"class":62}," raw_data",[52,18607,1330],{"class":102},[52,18609,18610,18613,18615,18617,18619,18621,18623,18625,18628,18631],{"class":54,"line":175},[52,18611,18612],{"class":62},"    name ",[52,18614,80],{"class":79},[52,18616,15664],{"class":62},[52,18618,250],{"class":102},[52,18620,115],{"class":83},[52,18622,2147],{"class":87},[52,18624,115],{"class":83},[52,18626,18627],{"class":102},"].",[52,18629,18630],{"class":189},"strip",[52,18632,230],{"class":102},[52,18634,18635,18638,18640,18642,18644,18646,18648,18650,18652,18654],{"class":54,"line":214},[52,18636,18637],{"class":62},"    age_text ",[52,18639,80],{"class":79},[52,18641,15664],{"class":62},[52,18643,250],{"class":102},[52,18645,115],{"class":83},[52,18647,2167],{"class":87},[52,18649,115],{"class":83},[52,18651,18627],{"class":102},[52,18653,18630],{"class":189},[52,18655,230],{"class":102},[52,18657,18658,18661,18663,18665,18667,18669,18671,18673,18675,18677,18680,18683],{"class":54,"line":233},[52,18659,18660],{"class":62},"    city ",[52,18662,80],{"class":79},[52,18664,15664],{"class":62},[52,18666,250],{"class":102},[52,18668,115],{"class":83},[52,18670,15026],{"class":87},[52,18672,115],{"class":83},[52,18674,18627],{"class":102},[52,18676,18630],{"class":189},[52,18678,18679],{"class":102},"().",[52,18681,18682],{"class":189},"title",[52,18684,230],{"class":102},[52,18686,18687],{"class":54,"line":238},[52,18688,70],{"emptyLinePlaceholder":69},[52,18690,18691,18693,18695,18697],{"class":54,"line":272},[52,18692,1438],{"class":58},[52,18694,1447],{"class":79},[52,18696,7019],{"class":62},[52,18698,1330],{"class":102},[52,18700,18701],{"class":54,"line":1348},[52,18702,18703],{"class":58},"        continue\n",[52,18705,18706],{"class":54,"line":1376},[52,18707,70],{"emptyLinePlaceholder":69},[52,18709,18710,18713,18715,18717,18719,18722,18724,18727,18730,18732],{"class":54,"line":1381},[52,18711,18712],{"class":62},"    age ",[52,18714,80],{"class":79},[52,18716,9789],{"class":4733},[52,18718,193],{"class":102},[52,18720,18721],{"class":189},"age_text",[52,18723,1521],{"class":102},[52,18725,18726],{"class":58}," if",[52,18728,18729],{"class":62}," age_text ",[52,18731,1591],{"class":58},[52,18733,18734],{"class":160}," None\n",[52,18736,18737],{"class":54,"line":1406},[52,18738,70],{"emptyLinePlaceholder":69},[52,18740,18741,18744,18746,18749],{"class":54,"line":1430},[52,18742,18743],{"class":62},"    cleaned_data",[52,18745,186],{"class":102},[52,18747,18748],{"class":189},"append",[52,18750,18751],{"class":102},"({\n",[52,18753,18754,18756,18758,18760,18762,18764],{"class":54,"line":1435},[52,18755,1029],{"class":83},[52,18757,2147],{"class":87},[52,18759,115],{"class":83},[52,18761,118],{"class":102},[52,18763,7019],{"class":189},[52,18765,125],{"class":102},[52,18767,18768,18770,18772,18774,18776,18778],{"class":54,"line":1467},[52,18769,1029],{"class":83},[52,18771,2167],{"class":87},[52,18773,115],{"class":83},[52,18775,118],{"class":102},[52,18777,7024],{"class":189},[52,18779,125],{"class":102},[52,18781,18782,18784,18786,18788,18790],{"class":54,"line":1495},[52,18783,1029],{"class":83},[52,18785,15026],{"class":87},[52,18787,115],{"class":83},[52,18789,118],{"class":102},[52,18791,18792],{"class":189}," city\n",[52,18794,18795],{"class":54,"line":1531},[52,18796,18797],{"class":102},"    })\n",[52,18799,18800],{"class":54,"line":1564},[52,18801,70],{"emptyLinePlaceholder":69},[52,18803,18804,18806,18808,18811],{"class":54,"line":1572},[52,18805,242],{"class":241},[52,18807,193],{"class":102},[52,18809,18810],{"class":189},"cleaned_data",[52,18812,211],{"class":102},[37,18814,323],{"id":322},[14,18816,18817],{},"This script shows a simple data cleaning workflow:",[309,18819,18820,18823,18832,18838,18844,18850],{},[24,18821,18822],{},"Uses a small list of dictionaries as sample messy data",[24,18824,18825,18826],{},"Cleans text values with ",[372,18827,18829],{"href":18828},"\u002Freference\u002Fpython-string-strip-method",[49,18830,18831],{},"strip()",[24,18833,18834,18835],{},"Standardizes city names with ",[49,18836,18837],{},"title()",[24,18839,18840,18841,18843],{},"Converts age from string to ",[49,18842,5142],{}," when possible",[24,18845,18846,18847],{},"Keeps missing age as ",[49,18848,18849],{},"None",[24,18851,18852],{},"Builds a new cleaned list instead of changing the original",[37,18854,18856],{"id":18855},"the-messy-data-we-start-with","The messy data we start with",[14,18858,18859],{},"Here is the raw data:",[42,18861,18863],{"className":44,"code":18862,"language":46,"meta":47,"style":47},"raw_data = [\n    {\"name\": \" Alice \", \"age\": \"25\", \"city\": \"new york\"},\n    {\"name\": \"Bob\", \"age\": \"\", \"city\": \" london \"},\n    {\"name\": \"  Cara\", \"age\": \"31\", \"city\": \"PARIS\"}\n]\n\nprint(raw_data)\n",[49,18864,18865,18873,18925,18973,19025,19029,19033],{"__ignoreMap":47},[52,18866,18867,18869,18871],{"class":54,"line":55},[52,18868,18408],{"class":62},[52,18870,80],{"class":79},[52,18872,16627],{"class":102},[52,18874,18875,18877,18879,18881,18883,18885,18887,18889,18891,18893,18895,18897,18899,18901,18903,18905,18907,18909,18911,18913,18915,18917,18919,18921,18923],{"class":54,"line":66},[52,18876,18417],{"class":102},[52,18878,115],{"class":83},[52,18880,2147],{"class":87},[52,18882,115],{"class":83},[52,18884,118],{"class":102},[52,18886,84],{"class":83},[52,18888,18430],{"class":87},[52,18890,115],{"class":83},[52,18892,199],{"class":102},[52,18894,84],{"class":83},[52,18896,2167],{"class":87},[52,18898,115],{"class":83},[52,18900,118],{"class":102},[52,18902,84],{"class":83},[52,18904,15077],{"class":87},[52,18906,115],{"class":83},[52,18908,199],{"class":102},[52,18910,84],{"class":83},[52,18912,15026],{"class":87},[52,18914,115],{"class":83},[52,18916,118],{"class":102},[52,18918,84],{"class":83},[52,18920,18463],{"class":87},[52,18922,115],{"class":83},[52,18924,18468],{"class":102},[52,18926,18927,18929,18931,18933,18935,18937,18939,18941,18943,18945,18947,18949,18951,18953,18955,18957,18959,18961,18963,18965,18967,18969,18971],{"class":54,"line":73},[52,18928,18417],{"class":102},[52,18930,115],{"class":83},[52,18932,2147],{"class":87},[52,18934,115],{"class":83},[52,18936,118],{"class":102},[52,18938,84],{"class":83},[52,18940,10858],{"class":87},[52,18942,115],{"class":83},[52,18944,199],{"class":102},[52,18946,84],{"class":83},[52,18948,2167],{"class":87},[52,18950,115],{"class":83},[52,18952,118],{"class":102},[52,18954,3448],{"class":83},[52,18956,199],{"class":102},[52,18958,84],{"class":83},[52,18960,15026],{"class":87},[52,18962,115],{"class":83},[52,18964,118],{"class":102},[52,18966,84],{"class":83},[52,18968,18513],{"class":87},[52,18970,115],{"class":83},[52,18972,18468],{"class":102},[52,18974,18975,18977,18979,18981,18983,18985,18987,18989,18991,18993,18995,18997,18999,19001,19003,19005,19007,19009,19011,19013,19015,19017,19019,19021,19023],{"class":54,"line":94},[52,18976,18417],{"class":102},[52,18978,115],{"class":83},[52,18980,2147],{"class":87},[52,18982,115],{"class":83},[52,18984,118],{"class":102},[52,18986,84],{"class":83},[52,18988,18534],{"class":87},[52,18990,115],{"class":83},[52,18992,199],{"class":102},[52,18994,84],{"class":83},[52,18996,2167],{"class":87},[52,18998,115],{"class":83},[52,19000,118],{"class":102},[52,19002,84],{"class":83},[52,19004,18551],{"class":87},[52,19006,115],{"class":83},[52,19008,199],{"class":102},[52,19010,84],{"class":83},[52,19012,15026],{"class":87},[52,19014,115],{"class":83},[52,19016,118],{"class":102},[52,19018,84],{"class":83},[52,19020,18568],{"class":87},[52,19022,115],{"class":83},[52,19024,167],{"class":102},[52,19026,19027],{"class":54,"line":106},[52,19028,1147],{"class":102},[52,19030,19031],{"class":54,"line":128},[52,19032,70],{"emptyLinePlaceholder":69},[52,19034,19035,19037,19039,19042],{"class":54,"line":148},[52,19036,242],{"class":241},[52,19038,193],{"class":102},[52,19040,19041],{"class":189},"raw_data",[52,19043,211],{"class":102},[14,19045,19046],{},"This data has several common problems:",[309,19048,19049,19052,19055,19058],{},[24,19050,19051],{},"Extra spaces around values",[24,19053,19054],{},"Mixed uppercase and lowercase text",[24,19056,19057],{},"Missing values stored as empty strings",[24,19059,19060],{},"Numbers stored as text instead of real numbers",[14,19062,19063],{},"Real files often contain this kind of messy data, especially when the data comes from user input or CSV files.",[37,19065,19067],{"id":19066},"step-1-remove-extra-whitespace","Step 1: Remove extra whitespace",[14,19069,3650,19070,19072],{},[49,19071,18831],{}," on string values to remove spaces at the beginning and end.",[42,19074,19076],{"className":44,"code":19075,"language":46,"meta":47,"style":47},"row = {\"name\": \" Alice \", \"age\": \"25\", \"city\": \" london \"}\n\nname = row[\"name\"].strip()\nage_text = row[\"age\"].strip()\ncity_text = row[\"city\"].strip()\n\nprint(name)\nprint(age_text)\nprint(city_text)\n",[49,19077,19078,19135,19139,19161,19184,19207,19211,19221,19231],{"__ignoreMap":47},[52,19079,19080,19083,19085,19087,19089,19091,19093,19095,19097,19099,19101,19103,19105,19107,19109,19111,19113,19115,19117,19119,19121,19123,19125,19127,19129,19131,19133],{"class":54,"line":55},[52,19081,19082],{"class":62},"row ",[52,19084,80],{"class":79},[52,19086,3000],{"class":102},[52,19088,115],{"class":83},[52,19090,2147],{"class":87},[52,19092,115],{"class":83},[52,19094,118],{"class":102},[52,19096,84],{"class":83},[52,19098,18430],{"class":87},[52,19100,115],{"class":83},[52,19102,199],{"class":102},[52,19104,84],{"class":83},[52,19106,2167],{"class":87},[52,19108,115],{"class":83},[52,19110,118],{"class":102},[52,19112,84],{"class":83},[52,19114,15077],{"class":87},[52,19116,115],{"class":83},[52,19118,199],{"class":102},[52,19120,84],{"class":83},[52,19122,15026],{"class":87},[52,19124,115],{"class":83},[52,19126,118],{"class":102},[52,19128,84],{"class":83},[52,19130,18513],{"class":87},[52,19132,115],{"class":83},[52,19134,167],{"class":102},[52,19136,19137],{"class":54,"line":66},[52,19138,70],{"emptyLinePlaceholder":69},[52,19140,19141,19143,19145,19147,19149,19151,19153,19155,19157,19159],{"class":54,"line":73},[52,19142,9211],{"class":62},[52,19144,80],{"class":79},[52,19146,15664],{"class":62},[52,19148,250],{"class":102},[52,19150,115],{"class":83},[52,19152,2147],{"class":87},[52,19154,115],{"class":83},[52,19156,18627],{"class":102},[52,19158,18630],{"class":189},[52,19160,230],{"class":102},[52,19162,19163,19166,19168,19170,19172,19174,19176,19178,19180,19182],{"class":54,"line":94},[52,19164,19165],{"class":62},"age_text ",[52,19167,80],{"class":79},[52,19169,15664],{"class":62},[52,19171,250],{"class":102},[52,19173,115],{"class":83},[52,19175,2167],{"class":87},[52,19177,115],{"class":83},[52,19179,18627],{"class":102},[52,19181,18630],{"class":189},[52,19183,230],{"class":102},[52,19185,19186,19189,19191,19193,19195,19197,19199,19201,19203,19205],{"class":54,"line":106},[52,19187,19188],{"class":62},"city_text ",[52,19190,80],{"class":79},[52,19192,15664],{"class":62},[52,19194,250],{"class":102},[52,19196,115],{"class":83},[52,19198,15026],{"class":87},[52,19200,115],{"class":83},[52,19202,18627],{"class":102},[52,19204,18630],{"class":189},[52,19206,230],{"class":102},[52,19208,19209],{"class":54,"line":128},[52,19210,70],{"emptyLinePlaceholder":69},[52,19212,19213,19215,19217,19219],{"class":54,"line":148},[52,19214,242],{"class":241},[52,19216,193],{"class":102},[52,19218,2147],{"class":189},[52,19220,211],{"class":102},[52,19222,19223,19225,19227,19229],{"class":54,"line":164},[52,19224,242],{"class":241},[52,19226,193],{"class":102},[52,19228,18721],{"class":189},[52,19230,211],{"class":102},[52,19232,19233,19235,19237,19240],{"class":54,"line":170},[52,19234,242],{"class":241},[52,19236,193],{"class":102},[52,19238,19239],{"class":189},"city_text",[52,19241,211],{"class":102},[14,19243,9266],{},[42,19245,19247],{"className":44,"code":19246,"language":46,"meta":47,"style":47},"Alice\n25\nlondon\n",[49,19248,19249,19254,19259],{"__ignoreMap":47},[52,19250,19251],{"class":54,"line":55},[52,19252,19253],{"class":62},"Alice\n",[52,19255,19256],{"class":54,"line":66},[52,19257,19258],{"class":121},"25\n",[52,19260,19261],{"class":54,"line":73},[52,19262,19263],{"class":62},"london\n",[14,19265,19266,19267,759,19270,19273],{},"Leading and trailing spaces can cause matching problems. For example, ",[49,19268,19269],{},"\"Alice\"",[49,19271,19272],{},"\" Alice \""," look similar, but Python treats them as different strings.",[14,19275,19276,19277,186],{},"If you want a focused guide, see ",[372,19278,19280],{"href":19279},"\u002Fhow-to\u002Fhow-to-remove-whitespace-from-a-string-in-python","how to remove whitespace from a string in Python",[37,19282,19284],{"id":19283},"step-2-standardize-text-values","Step 2: Standardize text values",[14,19286,19287,19288,19290],{},"In this example, we use ",[49,19289,18837],{}," for city names.",[42,19292,19294],{"className":44,"code":19293,"language":46,"meta":47,"style":47},"city = \"PARIS\".strip().title()\nprint(city)\n",[49,19295,19296,19319],{"__ignoreMap":47},[52,19297,19298,19301,19303,19305,19307,19309,19311,19313,19315,19317],{"class":54,"line":55},[52,19299,19300],{"class":62},"city ",[52,19302,80],{"class":79},[52,19304,84],{"class":83},[52,19306,18568],{"class":87},[52,19308,115],{"class":83},[52,19310,186],{"class":102},[52,19312,18630],{"class":189},[52,19314,18679],{"class":102},[52,19316,18682],{"class":189},[52,19318,230],{"class":102},[52,19320,19321,19323,19325,19327],{"class":54,"line":66},[52,19322,242],{"class":241},[52,19324,193],{"class":102},[52,19326,15026],{"class":189},[52,19328,211],{"class":102},[14,19330,5267],{},[42,19332,19334],{"className":44,"code":19333,"language":46,"meta":47,"style":47},"Paris\n",[49,19335,19336],{"__ignoreMap":47},[52,19337,19338],{"class":54,"line":55},[52,19339,19333],{"class":62},[14,19341,19342],{},"Standard formatting makes data easier to compare.",[14,19344,16291],{},[309,19346,19347,19352,19357],{},[24,19348,19349],{},[49,19350,19351],{},"\"paris\"",[24,19353,19354],{},[49,19355,19356],{},"\"PARIS\"",[24,19358,19359],{},[49,19360,19361],{},"\" Paris \"",[14,19363,19364],{},"can all become:",[309,19366,19367],{},[24,19368,19369],{},[49,19370,19371],{},"\"Paris\"",[14,19373,19374,19375,19377],{},"This simple script uses ",[49,19376,18837],{}," because it is easy to understand. In real projects, the best format depends on the data and the rules you want to enforce.",[37,19379,19381],{"id":19380},"step-3-handle-missing-values","Step 3: Handle missing values",[14,19383,19384],{},"Before converting the age, check whether the string is empty.",[42,19386,19388],{"className":44,"code":19387,"language":46,"meta":47,"style":47},"age_text = \"\"\n\nage = int(age_text) if age_text else None\nprint(age)\n",[49,19389,19390,19399,19403,19426],{"__ignoreMap":47},[52,19391,19392,19394,19396],{"class":54,"line":55},[52,19393,19165],{"class":62},[52,19395,80],{"class":79},[52,19397,19398],{"class":83}," \"\"\n",[52,19400,19401],{"class":54,"line":66},[52,19402,70],{"emptyLinePlaceholder":69},[52,19404,19405,19408,19410,19412,19414,19416,19418,19420,19422,19424],{"class":54,"line":73},[52,19406,19407],{"class":62},"age ",[52,19409,80],{"class":79},[52,19411,9789],{"class":4733},[52,19413,193],{"class":102},[52,19415,18721],{"class":189},[52,19417,1521],{"class":102},[52,19419,18726],{"class":58},[52,19421,18729],{"class":62},[52,19423,1591],{"class":58},[52,19425,18734],{"class":160},[52,19427,19428,19430,19432,19434],{"class":54,"line":94},[52,19429,242],{"class":241},[52,19431,193],{"class":102},[52,19433,2167],{"class":189},[52,19435,211],{"class":102},[14,19437,5267],{},[42,19439,19441],{"className":44,"code":19440,"language":46,"meta":47,"style":47},"None\n",[49,19442,19443],{"__ignoreMap":47},[52,19444,19445],{"class":54,"line":55},[52,19446,19440],{"class":160},[14,19448,10294,19449,19451],{},[49,19450,18849],{}," makes it clear that the value is missing.",[14,19453,19454],{},"That is usually better than keeping an empty string for numeric data, because:",[309,19456,19457,19460,19463],{},[24,19458,19459],{},"empty strings are still strings",[24,19461,19462],{},"numbers should be stored as numbers",[24,19464,19465],{},"missing values should be handled intentionally",[37,19467,19469],{"id":19468},"step-4-convert-data-types","Step 4: Convert data types",[14,19471,358,19472,19474],{},[49,19473,2167],{}," value starts as text:",[42,19476,19478],{"className":44,"code":19477,"language":46,"meta":47,"style":47},"age_text = \"25\"\nprint(type(age_text))\n",[49,19479,19480,19492],{"__ignoreMap":47},[52,19481,19482,19484,19486,19488,19490],{"class":54,"line":55},[52,19483,19165],{"class":62},[52,19485,80],{"class":79},[52,19487,84],{"class":83},[52,19489,15077],{"class":87},[52,19491,91],{"class":83},[52,19493,19494,19496,19498,19500,19502,19504],{"class":54,"line":66},[52,19495,242],{"class":241},[52,19497,193],{"class":102},[52,19499,4198],{"class":4733},[52,19501,193],{"class":102},[52,19503,18721],{"class":189},[52,19505,8886],{"class":102},[14,19507,5267],{},[42,19509,19511],{"className":44,"code":19510,"language":46,"meta":47,"style":47},"\u003Cclass 'str'>\n",[49,19512,19513],{"__ignoreMap":47},[52,19514,19515,19517,19519,19521,19524,19526],{"class":54,"line":55},[52,19516,15294],{"class":79},[52,19518,6994],{"class":1475},[52,19520,4172],{"class":83},[52,19522,19523],{"class":87},"str",[52,19525,4162],{"class":83},[52,19527,15306],{"class":79},[14,19529,19530,19531,118],{},"You can convert it to an integer with ",[372,19532,19534],{"href":19533},"\u002Freference\u002Fpython-int-function-explained",[49,19535,9492],{},[42,19537,19539],{"className":44,"code":19538,"language":46,"meta":47,"style":47},"age = int(age_text)\nprint(age)\nprint(type(age))\n",[49,19540,19541,19555,19565],{"__ignoreMap":47},[52,19542,19543,19545,19547,19549,19551,19553],{"class":54,"line":55},[52,19544,19407],{"class":62},[52,19546,80],{"class":79},[52,19548,9789],{"class":4733},[52,19550,193],{"class":102},[52,19552,18721],{"class":189},[52,19554,211],{"class":102},[52,19556,19557,19559,19561,19563],{"class":54,"line":66},[52,19558,242],{"class":241},[52,19560,193],{"class":102},[52,19562,2167],{"class":189},[52,19564,211],{"class":102},[52,19566,19567,19569,19571,19573,19575,19577],{"class":54,"line":73},[52,19568,242],{"class":241},[52,19570,193],{"class":102},[52,19572,4198],{"class":4733},[52,19574,193],{"class":102},[52,19576,2167],{"class":189},[52,19578,8886],{"class":102},[14,19580,5267],{},[42,19582,19584],{"className":44,"code":19583,"language":46,"meta":47,"style":47},"25\n\u003Cclass 'int'>\n",[49,19585,19586,19590],{"__ignoreMap":47},[52,19587,19588],{"class":54,"line":55},[52,19589,19258],{"class":121},[52,19591,19592,19594,19596,19598,19600,19602],{"class":54,"line":66},[52,19593,15294],{"class":79},[52,19595,6994],{"class":1475},[52,19597,4172],{"class":83},[52,19599,5142],{"class":87},[52,19601,4162],{"class":83},[52,19603,15306],{"class":79},[14,19605,19606],{},"This matters because numbers should be stored as numbers when you want to:",[309,19608,19609,19612,19615],{},[24,19610,19611],{},"compare values",[24,19613,19614],{},"sort correctly",[24,19616,19617],{},"do math",[14,19619,19620,19621,19623,19624,19626,19627,759,19630,186],{},"If the text is not numeric, ",[49,19622,9492],{}," will fail with a ",[49,19625,13780],{},". If you need help with that, see ",[372,19628,6472],{"href":19629},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix",[372,19631,15167],{"href":19632},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python",[37,19634,19636],{"id":19635},"step-5-create-the-cleaned-result","Step 5: Create the cleaned result",[14,19638,19639],{},"Now put the steps together into one script.",[42,19641,19642],{"className":44,"code":18401,"language":46,"meta":47,"style":47},[49,19643,19644,19652,19704,19752,19804,19808,19812,19820,19824,19836,19858,19880,19906,19910,19920,19924,19928,19950,19954,19964,19978,19992,20004,20008,20012],{"__ignoreMap":47},[52,19645,19646,19648,19650],{"class":54,"line":55},[52,19647,18408],{"class":62},[52,19649,80],{"class":79},[52,19651,16627],{"class":102},[52,19653,19654,19656,19658,19660,19662,19664,19666,19668,19670,19672,19674,19676,19678,19680,19682,19684,19686,19688,19690,19692,19694,19696,19698,19700,19702],{"class":54,"line":66},[52,19655,18417],{"class":102},[52,19657,115],{"class":83},[52,19659,2147],{"class":87},[52,19661,115],{"class":83},[52,19663,118],{"class":102},[52,19665,84],{"class":83},[52,19667,18430],{"class":87},[52,19669,115],{"class":83},[52,19671,199],{"class":102},[52,19673,84],{"class":83},[52,19675,2167],{"class":87},[52,19677,115],{"class":83},[52,19679,118],{"class":102},[52,19681,84],{"class":83},[52,19683,15077],{"class":87},[52,19685,115],{"class":83},[52,19687,199],{"class":102},[52,19689,84],{"class":83},[52,19691,15026],{"class":87},[52,19693,115],{"class":83},[52,19695,118],{"class":102},[52,19697,84],{"class":83},[52,19699,18463],{"class":87},[52,19701,115],{"class":83},[52,19703,18468],{"class":102},[52,19705,19706,19708,19710,19712,19714,19716,19718,19720,19722,19724,19726,19728,19730,19732,19734,19736,19738,19740,19742,19744,19746,19748,19750],{"class":54,"line":73},[52,19707,18417],{"class":102},[52,19709,115],{"class":83},[52,19711,2147],{"class":87},[52,19713,115],{"class":83},[52,19715,118],{"class":102},[52,19717,84],{"class":83},[52,19719,10858],{"class":87},[52,19721,115],{"class":83},[52,19723,199],{"class":102},[52,19725,84],{"class":83},[52,19727,2167],{"class":87},[52,19729,115],{"class":83},[52,19731,118],{"class":102},[52,19733,3448],{"class":83},[52,19735,199],{"class":102},[52,19737,84],{"class":83},[52,19739,15026],{"class":87},[52,19741,115],{"class":83},[52,19743,118],{"class":102},[52,19745,84],{"class":83},[52,19747,18513],{"class":87},[52,19749,115],{"class":83},[52,19751,18468],{"class":102},[52,19753,19754,19756,19758,19760,19762,19764,19766,19768,19770,19772,19774,19776,19778,19780,19782,19784,19786,19788,19790,19792,19794,19796,19798,19800,19802],{"class":54,"line":94},[52,19755,18417],{"class":102},[52,19757,115],{"class":83},[52,19759,2147],{"class":87},[52,19761,115],{"class":83},[52,19763,118],{"class":102},[52,19765,84],{"class":83},[52,19767,18534],{"class":87},[52,19769,115],{"class":83},[52,19771,199],{"class":102},[52,19773,84],{"class":83},[52,19775,2167],{"class":87},[52,19777,115],{"class":83},[52,19779,118],{"class":102},[52,19781,84],{"class":83},[52,19783,18551],{"class":87},[52,19785,115],{"class":83},[52,19787,199],{"class":102},[52,19789,84],{"class":83},[52,19791,15026],{"class":87},[52,19793,115],{"class":83},[52,19795,118],{"class":102},[52,19797,84],{"class":83},[52,19799,18568],{"class":87},[52,19801,115],{"class":83},[52,19803,167],{"class":102},[52,19805,19806],{"class":54,"line":106},[52,19807,1147],{"class":102},[52,19809,19810],{"class":54,"line":128},[52,19811,70],{"emptyLinePlaceholder":69},[52,19813,19814,19816,19818],{"class":54,"line":148},[52,19815,18585],{"class":62},[52,19817,80],{"class":79},[52,19819,18590],{"class":102},[52,19821,19822],{"class":54,"line":164},[52,19823,70],{"emptyLinePlaceholder":69},[52,19825,19826,19828,19830,19832,19834],{"class":54,"line":170},[52,19827,12265],{"class":58},[52,19829,14707],{"class":62},[52,19831,11213],{"class":58},[52,19833,18605],{"class":62},[52,19835,1330],{"class":102},[52,19837,19838,19840,19842,19844,19846,19848,19850,19852,19854,19856],{"class":54,"line":175},[52,19839,18612],{"class":62},[52,19841,80],{"class":79},[52,19843,15664],{"class":62},[52,19845,250],{"class":102},[52,19847,115],{"class":83},[52,19849,2147],{"class":87},[52,19851,115],{"class":83},[52,19853,18627],{"class":102},[52,19855,18630],{"class":189},[52,19857,230],{"class":102},[52,19859,19860,19862,19864,19866,19868,19870,19872,19874,19876,19878],{"class":54,"line":214},[52,19861,18637],{"class":62},[52,19863,80],{"class":79},[52,19865,15664],{"class":62},[52,19867,250],{"class":102},[52,19869,115],{"class":83},[52,19871,2167],{"class":87},[52,19873,115],{"class":83},[52,19875,18627],{"class":102},[52,19877,18630],{"class":189},[52,19879,230],{"class":102},[52,19881,19882,19884,19886,19888,19890,19892,19894,19896,19898,19900,19902,19904],{"class":54,"line":233},[52,19883,18660],{"class":62},[52,19885,80],{"class":79},[52,19887,15664],{"class":62},[52,19889,250],{"class":102},[52,19891,115],{"class":83},[52,19893,15026],{"class":87},[52,19895,115],{"class":83},[52,19897,18627],{"class":102},[52,19899,18630],{"class":189},[52,19901,18679],{"class":102},[52,19903,18682],{"class":189},[52,19905,230],{"class":102},[52,19907,19908],{"class":54,"line":238},[52,19909,70],{"emptyLinePlaceholder":69},[52,19911,19912,19914,19916,19918],{"class":54,"line":272},[52,19913,1438],{"class":58},[52,19915,1447],{"class":79},[52,19917,7019],{"class":62},[52,19919,1330],{"class":102},[52,19921,19922],{"class":54,"line":1348},[52,19923,18703],{"class":58},[52,19925,19926],{"class":54,"line":1376},[52,19927,70],{"emptyLinePlaceholder":69},[52,19929,19930,19932,19934,19936,19938,19940,19942,19944,19946,19948],{"class":54,"line":1381},[52,19931,18712],{"class":62},[52,19933,80],{"class":79},[52,19935,9789],{"class":4733},[52,19937,193],{"class":102},[52,19939,18721],{"class":189},[52,19941,1521],{"class":102},[52,19943,18726],{"class":58},[52,19945,18729],{"class":62},[52,19947,1591],{"class":58},[52,19949,18734],{"class":160},[52,19951,19952],{"class":54,"line":1406},[52,19953,70],{"emptyLinePlaceholder":69},[52,19955,19956,19958,19960,19962],{"class":54,"line":1430},[52,19957,18743],{"class":62},[52,19959,186],{"class":102},[52,19961,18748],{"class":189},[52,19963,18751],{"class":102},[52,19965,19966,19968,19970,19972,19974,19976],{"class":54,"line":1435},[52,19967,1029],{"class":83},[52,19969,2147],{"class":87},[52,19971,115],{"class":83},[52,19973,118],{"class":102},[52,19975,7019],{"class":189},[52,19977,125],{"class":102},[52,19979,19980,19982,19984,19986,19988,19990],{"class":54,"line":1467},[52,19981,1029],{"class":83},[52,19983,2167],{"class":87},[52,19985,115],{"class":83},[52,19987,118],{"class":102},[52,19989,7024],{"class":189},[52,19991,125],{"class":102},[52,19993,19994,19996,19998,20000,20002],{"class":54,"line":1495},[52,19995,1029],{"class":83},[52,19997,15026],{"class":87},[52,19999,115],{"class":83},[52,20001,118],{"class":102},[52,20003,18792],{"class":189},[52,20005,20006],{"class":54,"line":1531},[52,20007,18797],{"class":102},[52,20009,20010],{"class":54,"line":1564},[52,20011,70],{"emptyLinePlaceholder":69},[52,20013,20014,20016,20018,20020],{"class":54,"line":1572},[52,20015,242],{"class":241},[52,20017,193],{"class":102},[52,20019,18810],{"class":189},[52,20021,211],{"class":102},[14,20023,20024],{},"What this script does:",[309,20026,20027,20030,20033,20039,20042],{},[24,20028,20029],{},"Loops through each row",[24,20031,20032],{},"Cleans each field",[24,20034,20035,20036,20038],{},"Skips rows where ",[49,20037,2147],{}," is empty",[24,20040,20041],{},"Converts age when possible",[24,20043,20044,20045],{},"Appends a new cleaned dictionary to ",[49,20046,18810],{},[14,20048,20049],{},"Using a new list is safer for beginners because you keep the original data unchanged.",[37,20051,13563],{"id":13562},[14,20053,20054],{},"Running the full script prints:",[42,20056,20058],{"className":44,"code":20057,"language":46,"meta":47,"style":47},"[{'name': 'Alice', 'age': 25, 'city': 'New York'}, {'name': 'Bob', 'age': None, 'city': 'London'}, {'name': 'Cara', 'age': 31, 'city': 'Paris'}]\n",[49,20059,20060],{"__ignoreMap":47},[52,20061,20062,20065,20067,20069,20071,20073,20075,20077,20079,20081,20083,20085,20087,20089,20091,20093,20095,20097,20099,20101,20103,20105,20107,20110,20112,20114,20116,20118,20120,20122,20124,20126,20128,20130,20132,20134,20136,20138,20140,20142,20144,20146,20148,20150,20152,20154,20156,20158,20160,20162,20164,20166,20168,20171,20173,20175,20177,20179,20181,20183,20186,20188,20190,20192,20194,20196,20198,20200,20202],{"class":54,"line":55},[52,20063,20064],{"class":102},"[{",[52,20066,4162],{"class":83},[52,20068,2147],{"class":87},[52,20070,4162],{"class":83},[52,20072,118],{"class":102},[52,20074,4172],{"class":83},[52,20076,2156],{"class":87},[52,20078,4162],{"class":83},[52,20080,199],{"class":102},[52,20082,4172],{"class":83},[52,20084,2167],{"class":87},[52,20086,4162],{"class":83},[52,20088,118],{"class":102},[52,20090,2674],{"class":121},[52,20092,199],{"class":102},[52,20094,4172],{"class":83},[52,20096,15026],{"class":87},[52,20098,4162],{"class":83},[52,20100,118],{"class":102},[52,20102,4172],{"class":83},[52,20104,1206],{"class":87},[52,20106,4162],{"class":83},[52,20108,20109],{"class":102},"},",[52,20111,3000],{"class":102},[52,20113,4162],{"class":83},[52,20115,2147],{"class":87},[52,20117,4162],{"class":83},[52,20119,118],{"class":102},[52,20121,4172],{"class":83},[52,20123,10858],{"class":87},[52,20125,4162],{"class":83},[52,20127,199],{"class":102},[52,20129,4172],{"class":83},[52,20131,2167],{"class":87},[52,20133,4162],{"class":83},[52,20135,118],{"class":102},[52,20137,1450],{"class":160},[52,20139,199],{"class":102},[52,20141,4172],{"class":83},[52,20143,15026],{"class":87},[52,20145,4162],{"class":83},[52,20147,118],{"class":102},[52,20149,4172],{"class":83},[52,20151,15056],{"class":87},[52,20153,4162],{"class":83},[52,20155,20109],{"class":102},[52,20157,3000],{"class":102},[52,20159,4162],{"class":83},[52,20161,2147],{"class":87},[52,20163,4162],{"class":83},[52,20165,118],{"class":102},[52,20167,4172],{"class":83},[52,20169,20170],{"class":87},"Cara",[52,20172,4162],{"class":83},[52,20174,199],{"class":102},[52,20176,4172],{"class":83},[52,20178,2167],{"class":87},[52,20180,4162],{"class":83},[52,20182,118],{"class":102},[52,20184,20185],{"class":121}," 31",[52,20187,199],{"class":102},[52,20189,4172],{"class":83},[52,20191,15026],{"class":87},[52,20193,4162],{"class":83},[52,20195,118],{"class":102},[52,20197,4172],{"class":83},[52,20199,15086],{"class":87},[52,20201,4162],{"class":83},[52,20203,20204],{"class":102},"}]\n",[14,20206,20207],{},"Notice what changed:",[309,20209,20210,20213,20216,20219],{},[24,20211,20212],{},"Extra spaces were removed",[24,20214,20215],{},"City names were standardized",[24,20217,20218],{},"Age values were converted from strings to integers",[24,20220,20221,20222],{},"Missing age became ",[49,20223,18849],{},[37,20225,20227],{"id":20226},"useful-improvements-for-real-projects","Useful improvements for real projects",[14,20229,20230],{},"This example is intentionally simple. In real projects, you may also want to:",[309,20232,20233,20236,20241,20244,20247],{},[24,20234,20235],{},"Skip rows with missing required fields",[24,20237,3650,20238,20240],{},[49,20239,3603],{}," when converting numbers",[24,20242,20243],{},"Read messy data from CSV files",[24,20245,20246],{},"Write cleaned data back to a file",[24,20248,20249],{},"Move cleaning steps into a function",[14,20251,20252],{},"For example, if your age values might contain invalid text, you can make the conversion safer:",[42,20254,20256],{"className":44,"code":20255,"language":46,"meta":47,"style":47},"raw_data = [\n    {\"name\": \"Alice\", \"age\": \"25\", \"city\": \"new york\"},\n    {\"name\": \"Bob\", \"age\": \"unknown\", \"city\": \"london\"}\n]\n\ncleaned_data = []\n\nfor row in raw_data:\n    name = row[\"name\"].strip()\n    age_text = row[\"age\"].strip()\n    city = row[\"city\"].strip().title()\n\n    try:\n        age = int(age_text) if age_text else None\n    except ValueError:\n        age = None\n\n    cleaned_data.append({\n        \"name\": name,\n        \"age\": age,\n        \"city\": city\n    })\n\nprint(cleaned_data)\n",[49,20257,20258,20266,20318,20372,20376,20380,20388,20392,20404,20426,20448,20474,20478,20485,20508,20517,20525,20529,20539,20553,20567,20579,20583,20587],{"__ignoreMap":47},[52,20259,20260,20262,20264],{"class":54,"line":55},[52,20261,18408],{"class":62},[52,20263,80],{"class":79},[52,20265,16627],{"class":102},[52,20267,20268,20270,20272,20274,20276,20278,20280,20282,20284,20286,20288,20290,20292,20294,20296,20298,20300,20302,20304,20306,20308,20310,20312,20314,20316],{"class":54,"line":66},[52,20269,18417],{"class":102},[52,20271,115],{"class":83},[52,20273,2147],{"class":87},[52,20275,115],{"class":83},[52,20277,118],{"class":102},[52,20279,84],{"class":83},[52,20281,2156],{"class":87},[52,20283,115],{"class":83},[52,20285,199],{"class":102},[52,20287,84],{"class":83},[52,20289,2167],{"class":87},[52,20291,115],{"class":83},[52,20293,118],{"class":102},[52,20295,84],{"class":83},[52,20297,15077],{"class":87},[52,20299,115],{"class":83},[52,20301,199],{"class":102},[52,20303,84],{"class":83},[52,20305,15026],{"class":87},[52,20307,115],{"class":83},[52,20309,118],{"class":102},[52,20311,84],{"class":83},[52,20313,18463],{"class":87},[52,20315,115],{"class":83},[52,20317,18468],{"class":102},[52,20319,20320,20322,20324,20326,20328,20330,20332,20334,20336,20338,20340,20342,20344,20346,20348,20351,20353,20355,20357,20359,20361,20363,20365,20368,20370],{"class":54,"line":73},[52,20321,18417],{"class":102},[52,20323,115],{"class":83},[52,20325,2147],{"class":87},[52,20327,115],{"class":83},[52,20329,118],{"class":102},[52,20331,84],{"class":83},[52,20333,10858],{"class":87},[52,20335,115],{"class":83},[52,20337,199],{"class":102},[52,20339,84],{"class":83},[52,20341,2167],{"class":87},[52,20343,115],{"class":83},[52,20345,118],{"class":102},[52,20347,84],{"class":83},[52,20349,20350],{"class":87},"unknown",[52,20352,115],{"class":83},[52,20354,199],{"class":102},[52,20356,84],{"class":83},[52,20358,15026],{"class":87},[52,20360,115],{"class":83},[52,20362,118],{"class":102},[52,20364,84],{"class":83},[52,20366,20367],{"class":87},"london",[52,20369,115],{"class":83},[52,20371,167],{"class":102},[52,20373,20374],{"class":54,"line":94},[52,20375,1147],{"class":102},[52,20377,20378],{"class":54,"line":106},[52,20379,70],{"emptyLinePlaceholder":69},[52,20381,20382,20384,20386],{"class":54,"line":128},[52,20383,18585],{"class":62},[52,20385,80],{"class":79},[52,20387,18590],{"class":102},[52,20389,20390],{"class":54,"line":148},[52,20391,70],{"emptyLinePlaceholder":69},[52,20393,20394,20396,20398,20400,20402],{"class":54,"line":164},[52,20395,12265],{"class":58},[52,20397,14707],{"class":62},[52,20399,11213],{"class":58},[52,20401,18605],{"class":62},[52,20403,1330],{"class":102},[52,20405,20406,20408,20410,20412,20414,20416,20418,20420,20422,20424],{"class":54,"line":170},[52,20407,18612],{"class":62},[52,20409,80],{"class":79},[52,20411,15664],{"class":62},[52,20413,250],{"class":102},[52,20415,115],{"class":83},[52,20417,2147],{"class":87},[52,20419,115],{"class":83},[52,20421,18627],{"class":102},[52,20423,18630],{"class":189},[52,20425,230],{"class":102},[52,20427,20428,20430,20432,20434,20436,20438,20440,20442,20444,20446],{"class":54,"line":175},[52,20429,18637],{"class":62},[52,20431,80],{"class":79},[52,20433,15664],{"class":62},[52,20435,250],{"class":102},[52,20437,115],{"class":83},[52,20439,2167],{"class":87},[52,20441,115],{"class":83},[52,20443,18627],{"class":102},[52,20445,18630],{"class":189},[52,20447,230],{"class":102},[52,20449,20450,20452,20454,20456,20458,20460,20462,20464,20466,20468,20470,20472],{"class":54,"line":214},[52,20451,18660],{"class":62},[52,20453,80],{"class":79},[52,20455,15664],{"class":62},[52,20457,250],{"class":102},[52,20459,115],{"class":83},[52,20461,15026],{"class":87},[52,20463,115],{"class":83},[52,20465,18627],{"class":102},[52,20467,18630],{"class":189},[52,20469,18679],{"class":102},[52,20471,18682],{"class":189},[52,20473,230],{"class":102},[52,20475,20476],{"class":54,"line":233},[52,20477,70],{"emptyLinePlaceholder":69},[52,20479,20480,20483],{"class":54,"line":238},[52,20481,20482],{"class":58},"    try",[52,20484,1330],{"class":102},[52,20486,20487,20490,20492,20494,20496,20498,20500,20502,20504,20506],{"class":54,"line":272},[52,20488,20489],{"class":62},"        age ",[52,20491,80],{"class":79},[52,20493,9789],{"class":4733},[52,20495,193],{"class":102},[52,20497,18721],{"class":189},[52,20499,1521],{"class":102},[52,20501,18726],{"class":58},[52,20503,18729],{"class":62},[52,20505,1591],{"class":58},[52,20507,18734],{"class":160},[52,20509,20510,20513,20515],{"class":54,"line":1348},[52,20511,20512],{"class":58},"    except",[52,20514,4734],{"class":4733},[52,20516,1330],{"class":102},[52,20518,20519,20521,20523],{"class":54,"line":1376},[52,20520,20489],{"class":62},[52,20522,80],{"class":79},[52,20524,18734],{"class":160},[52,20526,20527],{"class":54,"line":1381},[52,20528,70],{"emptyLinePlaceholder":69},[52,20530,20531,20533,20535,20537],{"class":54,"line":1406},[52,20532,18743],{"class":62},[52,20534,186],{"class":102},[52,20536,18748],{"class":189},[52,20538,18751],{"class":102},[52,20540,20541,20543,20545,20547,20549,20551],{"class":54,"line":1430},[52,20542,1029],{"class":83},[52,20544,2147],{"class":87},[52,20546,115],{"class":83},[52,20548,118],{"class":102},[52,20550,7019],{"class":189},[52,20552,125],{"class":102},[52,20554,20555,20557,20559,20561,20563,20565],{"class":54,"line":1435},[52,20556,1029],{"class":83},[52,20558,2167],{"class":87},[52,20560,115],{"class":83},[52,20562,118],{"class":102},[52,20564,7024],{"class":189},[52,20566,125],{"class":102},[52,20568,20569,20571,20573,20575,20577],{"class":54,"line":1467},[52,20570,1029],{"class":83},[52,20572,15026],{"class":87},[52,20574,115],{"class":83},[52,20576,118],{"class":102},[52,20578,18792],{"class":189},[52,20580,20581],{"class":54,"line":1495},[52,20582,18797],{"class":102},[52,20584,20585],{"class":54,"line":1531},[52,20586,70],{"emptyLinePlaceholder":69},[52,20588,20589,20591,20593,20595],{"class":54,"line":1564},[52,20590,242],{"class":241},[52,20592,193],{"class":102},[52,20594,18810],{"class":189},[52,20596,211],{"class":102},[14,20598,20599,20600,20604,20605,186],{},"If your data comes from a file, the next practical step is to ",[372,20601,20603],{"href":20602},"\u002Fhow-to\u002Fhow-to-read-a-csv-file-in-python","read a CSV file in Python",", clean each row, and then ",[372,20606,20608],{"href":20607},"\u002Fhow-to\u002Fhow-to-write-a-csv-file-in-python","write a CSV file in Python",[37,20610,12322],{"id":12321},[14,20612,20613],{},"These are common causes of messy data problems:",[309,20615,20616,20619,20622,20625,20627],{},[24,20617,20618],{},"Extra spaces in user input or file data",[24,20620,20621],{},"Numbers stored as strings",[24,20623,20624],{},"Empty strings used instead of missing values",[24,20626,19054],{},[24,20628,20629],{},"Assuming every row has valid data",[14,20631,20632],{},"If your script is not working, these quick debug prints can help:",[42,20634,20636],{"className":44,"code":20635,"language":46,"meta":47,"style":47},"print(raw_data)\nprint(row)\nprint(type(row['age']))\nprint(repr(row['name']))\nprint(cleaned_data)\n",[49,20637,20638,20648,20658,20681,20704],{"__ignoreMap":47},[52,20639,20640,20642,20644,20646],{"class":54,"line":55},[52,20641,242],{"class":241},[52,20643,193],{"class":102},[52,20645,19041],{"class":189},[52,20647,211],{"class":102},[52,20649,20650,20652,20654,20656],{"class":54,"line":66},[52,20651,242],{"class":241},[52,20653,193],{"class":102},[52,20655,14723],{"class":189},[52,20657,211],{"class":102},[52,20659,20660,20662,20664,20666,20668,20670,20672,20674,20676,20678],{"class":54,"line":73},[52,20661,242],{"class":241},[52,20663,193],{"class":102},[52,20665,4198],{"class":4733},[52,20667,193],{"class":102},[52,20669,14723],{"class":189},[52,20671,250],{"class":102},[52,20673,4162],{"class":83},[52,20675,2167],{"class":87},[52,20677,4162],{"class":83},[52,20679,20680],{"class":102},"]))\n",[52,20682,20683,20685,20687,20690,20692,20694,20696,20698,20700,20702],{"class":54,"line":94},[52,20684,242],{"class":241},[52,20686,193],{"class":102},[52,20688,20689],{"class":241},"repr",[52,20691,193],{"class":102},[52,20693,14723],{"class":189},[52,20695,250],{"class":102},[52,20697,4162],{"class":83},[52,20699,2147],{"class":87},[52,20701,4162],{"class":83},[52,20703,20680],{"class":102},[52,20705,20706,20708,20710,20712],{"class":54,"line":106},[52,20707,242],{"class":241},[52,20709,193],{"class":102},[52,20711,18810],{"class":189},[52,20713,211],{"class":102},[14,20715,20716],{},"Why these help:",[309,20718,20719,20725,20730,20739,20745],{},[24,20720,20721,20724],{},[49,20722,20723],{},"print(raw_data)"," shows the full original data",[24,20726,20727,20729],{},[49,20728,14969],{}," shows the current row being processed",[24,20731,20732,20735,20736,20738],{},[49,20733,20734],{},"print(type(row['age']))"," confirms whether ",[49,20737,2167],{}," is a string or number",[24,20740,20741,20744],{},[49,20742,20743],{},"print(repr(row['name']))"," makes hidden spaces visible",[24,20746,20747,20750],{},[49,20748,20749],{},"print(cleaned_data)"," shows the result after cleaning",[37,20752,1942],{"id":1941},[639,20754,20756],{"id":20755},"why-use-a-new-cleaned-list-instead-of-changing-the-old-one","Why use a new cleaned list instead of changing the old one?",[14,20758,20759],{},"It is safer for beginners. You keep the original data and can compare before and after.",[639,20761,20763],{"id":20762},"why-is-none-used-for-missing-values","Why is None used for missing values?",[14,20765,20766,20768],{},[49,20767,18849],{}," clearly means no value. It is better than keeping an empty string for numeric data.",[639,20770,20772],{"id":20771},"what-if-int-fails-during-cleaning","What if int() fails during cleaning?",[14,20774,20775,20776,20778],{},"Use a check first or wrap the conversion in ",[49,20777,3603],{}," if the input may contain invalid numbers.",[639,20780,20782],{"id":20781},"can-i-use-this-script-with-csv-data","Can I use this script with CSV data?",[14,20784,20785],{},"Yes. The same cleaning steps work after reading rows from a CSV file into dictionaries.",[37,20787,2005],{"id":2004},[309,20789,20790,20794,20798,20803,20807],{},[24,20791,20792],{},[372,20793,16504],{"href":20602},[24,20795,20796],{},[372,20797,17737],{"href":20607},[24,20799,20800],{},[372,20801,20802],{"href":18828},"Python string strip() method",[24,20804,20805],{},[372,20806,13731],{"href":19533},[24,20808,20809],{},[372,20810,20812],{"href":20811},"\u002Fexamples\u002Fpython-remove-duplicates-from-data-example","Python remove duplicates from data example",[2034,20814,20815],{},"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 .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 .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 .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}",{"title":47,"searchDepth":66,"depth":66,"links":20817},[20818,20819,20820,20821,20822,20823,20824,20825,20826,20827,20828,20829,20835],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":18855,"depth":66,"text":18856},{"id":19066,"depth":66,"text":19067},{"id":19283,"depth":66,"text":19284},{"id":19380,"depth":66,"text":19381},{"id":19468,"depth":66,"text":19469},{"id":19635,"depth":66,"text":19636},{"id":13562,"depth":66,"text":13563},{"id":20226,"depth":66,"text":20227},{"id":12321,"depth":66,"text":12322},{"id":1941,"depth":66,"text":1942,"children":20830},[20831,20832,20833,20834],{"id":20755,"depth":73,"text":20756},{"id":20762,"depth":73,"text":20763},{"id":20771,"depth":73,"text":20772},{"id":20781,"depth":73,"text":20782},{"id":2004,"depth":66,"text":2005},"Master python data cleaning script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-data-cleaning-script-example",{"title":18362,"description":20836},"examples\u002Fpython-data-cleaning-script-example","y-fF0jR133sNWpKVejFUZUHNR_Q1li2ozmul6FHMG04",{"id":20843,"title":20844,"body":20845,"description":22084,"extension":2075,"meta":22085,"navigation":69,"path":22086,"seo":22087,"stem":22088,"__hash__":22089},"content\u002Fexamples\u002Fpython-decorator-example-simple-use-case.md","Python Decorator Example (Simple Use Case)",{"type":7,"value":20846,"toc":22044},[20847,20850,20853,20860,20864,20867,20878,20885,20889,21010,21013,21042,21046,21049,21066,21074,21078,21082,21099,21102,21108,21116,21129,21132,21136,21155,21158,21162,21173,21176,21179,21192,21196,21215,21218,21224,21236,21239,21242,21245,21252,21285,21288,21334,21337,21341,21352,21358,21369,21371,21374,21380,21383,21394,21398,21401,21404,21421,21424,21435,21439,21442,21448,21451,21483,21486,21530,21536,21548,21552,21555,21571,21574,21600,21603,21642,21645,21655,21657,21659,21665,21667,21726,21729,21739,21745,21754,21756,21815,21818,21822,21825,21857,21860,21870,21874,21877,21940,21946,21954,21957,21969,21972,21974,21978,21981,21985,21988,21996,22002,22006,22013,22015,22041],[10,20848,20844],{"id":20849},"python-decorator-example-simple-use-case",[14,20851,20852],{},"If you are new to decorators, the easiest way to understand them is to see one small example that works.",[14,20854,20855,20856,20859],{},"This page shows a simple decorator that wraps a function, runs code before and after it, and helps you see why decorators are useful. It does ",[5503,20857,20858],{},"not"," try to cover every decorator pattern.",[37,20861,20863],{"id":20862},"page-goal","Page goal",[14,20865,20866],{},"This example focuses on one clear idea:",[309,20868,20869,20872,20875],{},[24,20870,20871],{},"Understand a simple decorator by example",[24,20873,20874],{},"Read the code step by step",[24,20876,20877],{},"Run it yourself and see the output",[14,20879,20880,20881,186],{},"If you want the full beginner explanation later, see ",[372,20882,20884],{"href":20883},"\u002Flearn\u002Fdecorators-in-python-beginner-introduction\u002F","Decorators in Python: beginner introduction",[37,20886,20888],{"id":20887},"a-simple-runnable-decorator-example","A simple runnable decorator example",[42,20890,20892],{"className":44,"code":20891,"language":46,"meta":47,"style":47},"def show_before_after(func):\n    def wrapper():\n        print(\"Before function runs\")\n        func()\n        print(\"After function runs\")\n    return wrapper\n\n@show_before_after\ndef say_hello():\n    print(\"Hello!\")\n\nsay_hello()\n",[49,20893,20894,20908,20917,20932,20939,20954,20962,20966,20975,20984,20999,21003],{"__ignoreMap":47},[52,20895,20896,20898,20901,20903,20906],{"class":54,"line":55},[52,20897,7410],{"class":1475},[52,20899,20900],{"class":7065}," show_before_after",[52,20902,193],{"class":102},[52,20904,20905],{"class":7018},"func",[52,20907,3114],{"class":102},[52,20909,20910,20912,20915],{"class":54,"line":66},[52,20911,7004],{"class":1475},[52,20913,20914],{"class":7065}," wrapper",[52,20916,8146],{"class":102},[52,20918,20919,20921,20923,20925,20928,20930],{"class":54,"line":73},[52,20920,1470],{"class":241},[52,20922,193],{"class":102},[52,20924,115],{"class":83},[52,20926,20927],{"class":87},"Before function runs",[52,20929,115],{"class":83},[52,20931,211],{"class":102},[52,20933,20934,20937],{"class":54,"line":94},[52,20935,20936],{"class":189},"        func",[52,20938,230],{"class":102},[52,20940,20941,20943,20945,20947,20950,20952],{"class":54,"line":106},[52,20942,1470],{"class":241},[52,20944,193],{"class":102},[52,20946,115],{"class":83},[52,20948,20949],{"class":87},"After function runs",[52,20951,115],{"class":83},[52,20953,211],{"class":102},[52,20955,20956,20959],{"class":54,"line":128},[52,20957,20958],{"class":58},"    return",[52,20960,20961],{"class":62}," wrapper\n",[52,20963,20964],{"class":54,"line":148},[52,20965,70],{"emptyLinePlaceholder":69},[52,20967,20968,20972],{"class":54,"line":164},[52,20969,20971],{"class":20970},"stp6e","@",[52,20973,20974],{"class":7065},"show_before_after\n",[52,20976,20977,20979,20982],{"class":54,"line":170},[52,20978,7410],{"class":1475},[52,20980,20981],{"class":7065}," say_hello",[52,20983,8146],{"class":102},[52,20985,20986,20988,20990,20992,20995,20997],{"class":54,"line":175},[52,20987,1599],{"class":241},[52,20989,193],{"class":102},[52,20991,115],{"class":83},[52,20993,20994],{"class":87},"Hello!",[52,20996,115],{"class":83},[52,20998,211],{"class":102},[52,21000,21001],{"class":54,"line":214},[52,21002,70],{"emptyLinePlaceholder":69},[52,21004,21005,21008],{"class":54,"line":233},[52,21006,21007],{"class":189},"say_hello",[52,21009,230],{"class":102},[14,21011,21012],{},"This is the main pattern to notice:",[309,21014,21015,21021,21027,21032,21037],{},[24,21016,21017,21020],{},[49,21018,21019],{},"show_before_after"," takes a function as input",[24,21022,21023,21024],{},"It defines a new function called ",[49,21025,21026],{},"wrapper",[24,21028,21029,21031],{},[49,21030,21026],{}," runs extra code",[24,21033,21034,21036],{},[49,21035,21026],{}," also calls the original function",[24,21038,21039,21040],{},"The decorator returns ",[49,21041,21026],{},[37,21043,21045],{"id":21044},"what-the-example-shows","What the example shows",[14,21047,21048],{},"This one example teaches the basic decorator idea:",[309,21050,21051,21054,21057,21060],{},[24,21052,21053],{},"A decorator takes a function",[24,21055,21056],{},"It returns a new function",[24,21058,21059],{},"The new function runs extra code before and after the original function",[24,21061,358,21062,21065],{},[49,21063,21064],{},"@decorator_name"," syntax is a shortcut",[14,21067,21068,21069,21073],{},"If you already understand ",[372,21070,21072],{"href":21071},"\u002Fglossary\u002Fwhat-is-a-function-in-python\u002F","what a function is in Python",", this pattern will make more sense.",[37,21075,21077],{"id":21076},"how-to-read-the-code-step-by-step","How to read the code step by step",[639,21079,21081],{"id":21080},"_1-define-the-decorator","1. Define the decorator",[42,21083,21085],{"className":44,"code":21084,"language":46,"meta":47,"style":47},"def show_before_after(func):\n",[49,21086,21087],{"__ignoreMap":47},[52,21088,21089,21091,21093,21095,21097],{"class":54,"line":55},[52,21090,7410],{"class":1475},[52,21092,20900],{"class":7065},[52,21094,193],{"class":102},[52,21096,20905],{"class":7018},[52,21098,3114],{"class":102},[14,21100,21101],{},"This creates a decorator function.",[14,21103,21104,21105,21107],{},"The parameter ",[49,21106,20905],{}," is the function being decorated.",[639,21109,21111,21112,21115],{"id":21110},"_2-define-wrapper-inside-the-decorator","2. Define ",[49,21113,21114],{},"wrapper()"," inside the decorator",[42,21117,21119],{"className":44,"code":21118,"language":46,"meta":47,"style":47},"def wrapper():\n",[49,21120,21121],{"__ignoreMap":47},[52,21122,21123,21125,21127],{"class":54,"line":55},[52,21124,7410],{"class":1475},[52,21126,20914],{"class":7065},[52,21128,8146],{"class":102},[14,21130,21131],{},"This inner function is the new function that will replace the original one.",[639,21133,21135],{"id":21134},"_3-run-code-before-the-original-function","3. Run code before the original function",[42,21137,21139],{"className":44,"code":21138,"language":46,"meta":47,"style":47},"print(\"Before function runs\")\n",[49,21140,21141],{"__ignoreMap":47},[52,21142,21143,21145,21147,21149,21151,21153],{"class":54,"line":55},[52,21144,242],{"class":241},[52,21146,193],{"class":102},[52,21148,115],{"class":83},[52,21150,20927],{"class":87},[52,21152,115],{"class":83},[52,21154,211],{"class":102},[14,21156,21157],{},"This is extra behavior added by the decorator.",[639,21159,21161],{"id":21160},"_4-call-the-original-function","4. Call the original function",[42,21163,21165],{"className":44,"code":21164,"language":46,"meta":47,"style":47},"func()\n",[49,21166,21167],{"__ignoreMap":47},[52,21168,21169,21171],{"class":54,"line":55},[52,21170,20905],{"class":189},[52,21172,230],{"class":102},[14,21174,21175],{},"This runs the original function that was passed into the decorator.",[14,21177,21178],{},"Be careful here:",[309,21180,21181,21186],{},[24,21182,21183,21185],{},[49,21184,20905],{}," means the function itself",[24,21187,21188,21191],{},[49,21189,21190],{},"func()"," means “run the function”",[639,21193,21195],{"id":21194},"_5-run-code-after-the-original-function","5. Run code after the original function",[42,21197,21199],{"className":44,"code":21198,"language":46,"meta":47,"style":47},"print(\"After function runs\")\n",[49,21200,21201],{"__ignoreMap":47},[52,21202,21203,21205,21207,21209,21211,21213],{"class":54,"line":55},[52,21204,242],{"class":241},[52,21206,193],{"class":102},[52,21208,115],{"class":83},[52,21210,20949],{"class":87},[52,21212,115],{"class":83},[52,21214,211],{"class":102},[14,21216,21217],{},"This is another piece of added behavior.",[639,21219,21221,21222],{"id":21220},"_6-return-wrapper","6. Return ",[49,21223,21026],{},[42,21225,21227],{"className":44,"code":21226,"language":46,"meta":47,"style":47},"return wrapper\n",[49,21228,21229],{"__ignoreMap":47},[52,21230,21231,21234],{"class":54,"line":55},[52,21232,21233],{"class":58},"return",[52,21235,20961],{"class":62},[14,21237,21238],{},"This is a very important line.",[14,21240,21241],{},"It means the decorator gives back the new wrapped function.",[14,21243,21244],{},"If you forget this line, the decorator will not work correctly.",[639,21246,21248,21249],{"id":21247},"_7-apply-the-decorator-with-show_before_after","7. Apply the decorator with ",[49,21250,21251],{},"@show_before_after",[42,21253,21255],{"className":44,"code":21254,"language":46,"meta":47,"style":47},"@show_before_after\ndef say_hello():\n    print(\"Hello!\")\n",[49,21256,21257,21263,21271],{"__ignoreMap":47},[52,21258,21259,21261],{"class":54,"line":55},[52,21260,20971],{"class":20970},[52,21262,20974],{"class":7065},[52,21264,21265,21267,21269],{"class":54,"line":66},[52,21266,7410],{"class":1475},[52,21268,20981],{"class":7065},[52,21270,8146],{"class":102},[52,21272,21273,21275,21277,21279,21281,21283],{"class":54,"line":73},[52,21274,1599],{"class":241},[52,21276,193],{"class":102},[52,21278,115],{"class":83},[52,21280,20994],{"class":87},[52,21282,115],{"class":83},[52,21284,211],{"class":102},[14,21286,21287],{},"This is a shorter way to write:",[42,21289,21291],{"className":44,"code":21290,"language":46,"meta":47,"style":47},"def say_hello():\n    print(\"Hello!\")\n\nsay_hello = show_before_after(say_hello)\n",[49,21292,21293,21301,21315,21319],{"__ignoreMap":47},[52,21294,21295,21297,21299],{"class":54,"line":55},[52,21296,7410],{"class":1475},[52,21298,20981],{"class":7065},[52,21300,8146],{"class":102},[52,21302,21303,21305,21307,21309,21311,21313],{"class":54,"line":66},[52,21304,1599],{"class":241},[52,21306,193],{"class":102},[52,21308,115],{"class":83},[52,21310,20994],{"class":87},[52,21312,115],{"class":83},[52,21314,211],{"class":102},[52,21316,21317],{"class":54,"line":73},[52,21318,70],{"emptyLinePlaceholder":69},[52,21320,21321,21324,21326,21328,21330,21332],{"class":54,"line":94},[52,21322,21323],{"class":62},"say_hello ",[52,21325,80],{"class":79},[52,21327,20900],{"class":189},[52,21329,193],{"class":102},[52,21331,21007],{"class":189},[52,21333,211],{"class":102},[14,21335,21336],{},"Both versions do the same thing.",[639,21338,21340],{"id":21339},"_8-call-the-decorated-function-normally","8. Call the decorated function normally",[42,21342,21344],{"className":44,"code":21343,"language":46,"meta":47,"style":47},"say_hello()\n",[49,21345,21346],{"__ignoreMap":47},[52,21347,21348,21350],{"class":54,"line":55},[52,21349,21007],{"class":189},[52,21351,230],{"class":102},[14,21353,21354,21355,21357],{},"Even though ",[49,21356,21007],{}," was decorated, you still call it like a normal function.",[14,21359,21360,21361,857,21365,186],{},"If you need a refresher, see ",[372,21362,21364],{"href":21363},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions explained",[372,21366,21368],{"href":21367},"\u002Fhow-to\u002Fhow-to-create-a-simple-function-in-python\u002F","how to create a simple function in Python",[37,21370,13563],{"id":13562},[14,21372,21373],{},"When you run the code, you should see:",[42,21375,21378],{"className":21376,"code":21377,"language":955,"meta":47},[953],"Before function runs\nHello!\nAfter function runs\n",[49,21379,21377],{"__ignoreMap":47},[14,21381,21382],{},"That output shows the order clearly:",[21,21384,21385,21388,21391],{},[24,21386,21387],{},"The decorator runs code before",[24,21389,21390],{},"The original function runs",[24,21392,21393],{},"The decorator runs code after",[37,21395,21397],{"id":21396},"why-beginners-use-decorators","Why beginners use decorators",[14,21399,21400],{},"Beginners usually meet decorators when they want to add repeated behavior without rewriting the same code again and again.",[14,21402,21403],{},"A decorator can help you:",[309,21405,21406,21409,21412,21415,21418],{},[24,21407,21408],{},"Add messages before or after a function runs",[24,21410,21411],{},"Log information",[24,21413,21414],{},"Time how long a function takes",[24,21416,21417],{},"Check permissions or access",[24,21419,21420],{},"Reuse the same pattern across many functions",[14,21422,21423],{},"The big benefit is this:",[309,21425,21426,21432],{},[24,21427,21428,21429,21431],{},"You do ",[5503,21430,20858],{}," need to change the original function body each time",[24,21433,21434],{},"You avoid copying the same extra code into many functions",[37,21436,21438],{"id":21437},"important-limitation-of-this-simple-example","Important limitation of this simple example",[14,21440,21441],{},"This example is intentionally basic.",[14,21443,21444,21445,186],{},"It only works with functions that take ",[5503,21446,21447],{},"no arguments",[14,21449,21450],{},"For example, this version works:",[42,21452,21453],{"className":44,"code":21254,"language":46,"meta":47,"style":47},[49,21454,21455,21461,21469],{"__ignoreMap":47},[52,21456,21457,21459],{"class":54,"line":55},[52,21458,20971],{"class":20970},[52,21460,20974],{"class":7065},[52,21462,21463,21465,21467],{"class":54,"line":66},[52,21464,7410],{"class":1475},[52,21466,20981],{"class":7065},[52,21468,8146],{"class":102},[52,21470,21471,21473,21475,21477,21479,21481],{"class":54,"line":73},[52,21472,1599],{"class":241},[52,21474,193],{"class":102},[52,21476,115],{"class":83},[52,21478,20994],{"class":87},[52,21480,115],{"class":83},[52,21482,211],{"class":102},[14,21484,21485],{},"But this version would fail:",[42,21487,21489],{"className":44,"code":21488,"language":46,"meta":47,"style":47},"@show_before_after\ndef greet(name):\n    print(f\"Hello, {name}!\")\n",[49,21490,21491,21497,21510],{"__ignoreMap":47},[52,21492,21493,21495],{"class":54,"line":55},[52,21494,20971],{"class":20970},[52,21496,20974],{"class":7065},[52,21498,21499,21501,21504,21506,21508],{"class":54,"line":66},[52,21500,7410],{"class":1475},[52,21502,21503],{"class":7065}," greet",[52,21505,193],{"class":102},[52,21507,2147],{"class":7018},[52,21509,3114],{"class":102},[52,21511,21512,21514,21516,21518,21520,21522,21524,21526,21528],{"class":54,"line":73},[52,21513,1599],{"class":241},[52,21515,193],{"class":102},[52,21517,1476],{"class":1475},[52,21519,6129],{"class":87},[52,21521,1482],{"class":121},[52,21523,2147],{"class":189},[52,21525,1488],{"class":121},[52,21527,9245],{"class":87},[52,21529,211],{"class":102},[14,21531,21532,21533,21535],{},"Why? Because ",[49,21534,21114],{}," does not accept any parameters.",[14,21537,21538,21539,759,21542,21545,21546,186],{},"A more flexible decorator uses ",[49,21540,21541],{},"*args",[49,21543,21544],{},"**kwargs",", but that is better explained on a dedicated lesson page. For the full beginner explanation, see ",[372,21547,20884],{"href":20883},[37,21549,21551],{"id":21550},"common-beginner-confusion","Common beginner confusion",[14,21553,21554],{},"These points often cause confusion:",[309,21556,21557,21560,21565],{},[24,21558,21559],{},"The decorator runs when the function is defined, not only when it is later called",[24,21561,21562,21564],{},[49,21563,21026],{}," is the function that replaces the original name",[24,21566,21567,21568,21570],{},"The original function still exists as ",[49,21569,20905],{}," inside the wrapper",[14,21572,21573],{},"In simple terms:",[309,21575,21576,21582,21590],{},[24,21577,21578,21579,21581],{},"Before decoration, ",[49,21580,21007],{}," refers to the original function",[24,21583,21584,21585,21587,21588],{},"After decoration, ",[49,21586,21007],{}," refers to ",[49,21589,21026],{},[24,21591,21592,21593,2587,21595,21597,21598],{},"Inside ",[49,21594,21026],{},[49,21596,20905],{}," still points to the original ",[49,21599,21007],{},[14,21601,21602],{},"You can inspect that a little with:",[42,21604,21606],{"className":44,"code":21605,"language":46,"meta":47,"style":47},"print(say_hello)\nprint(type(say_hello))\nhelp(say_hello)\n",[49,21607,21608,21618,21632],{"__ignoreMap":47},[52,21609,21610,21612,21614,21616],{"class":54,"line":55},[52,21611,242],{"class":241},[52,21613,193],{"class":102},[52,21615,21007],{"class":189},[52,21617,211],{"class":102},[52,21619,21620,21622,21624,21626,21628,21630],{"class":54,"line":66},[52,21621,242],{"class":241},[52,21623,193],{"class":102},[52,21625,4198],{"class":4733},[52,21627,193],{"class":102},[52,21629,21007],{"class":189},[52,21631,8886],{"class":102},[52,21633,21634,21636,21638,21640],{"class":54,"line":73},[52,21635,8930],{"class":241},[52,21637,193],{"class":102},[52,21639,21007],{"class":189},[52,21641,211],{"class":102},[14,21643,21644],{},"And to run your file:",[42,21646,21647],{"className":390,"code":17485,"language":392,"meta":47,"style":47},[49,21648,21649],{"__ignoreMap":47},[52,21650,21651,21653],{"class":54,"line":55},[52,21652,46],{"class":399},[52,21654,17494],{"class":87},[37,21656,12322],{"id":12321},[14,21658,17965],{},[639,21660,21662,21663],{"id":21661},"forgetting-to-return-wrapper","Forgetting to return ",[49,21664,21026],{},[14,21666,8124],{},[42,21668,21670],{"className":44,"code":21669,"language":46,"meta":47,"style":47},"def show_before_after(func):\n    def wrapper():\n        print(\"Before function runs\")\n        func()\n        print(\"After function runs\")\n",[49,21671,21672,21684,21692,21706,21712],{"__ignoreMap":47},[52,21673,21674,21676,21678,21680,21682],{"class":54,"line":55},[52,21675,7410],{"class":1475},[52,21677,20900],{"class":7065},[52,21679,193],{"class":102},[52,21681,20905],{"class":7018},[52,21683,3114],{"class":102},[52,21685,21686,21688,21690],{"class":54,"line":66},[52,21687,7004],{"class":1475},[52,21689,20914],{"class":7065},[52,21691,8146],{"class":102},[52,21693,21694,21696,21698,21700,21702,21704],{"class":54,"line":73},[52,21695,1470],{"class":241},[52,21697,193],{"class":102},[52,21699,115],{"class":83},[52,21701,20927],{"class":87},[52,21703,115],{"class":83},[52,21705,211],{"class":102},[52,21707,21708,21710],{"class":54,"line":94},[52,21709,20936],{"class":189},[52,21711,230],{"class":102},[52,21713,21714,21716,21718,21720,21722,21724],{"class":54,"line":106},[52,21715,1470],{"class":241},[52,21717,193],{"class":102},[52,21719,115],{"class":83},[52,21721,20949],{"class":87},[52,21723,115],{"class":83},[52,21725,211],{"class":102},[14,21727,21728],{},"This is missing:",[42,21730,21731],{"className":44,"code":21226,"language":46,"meta":47,"style":47},[49,21732,21733],{"__ignoreMap":47},[52,21734,21735,21737],{"class":54,"line":55},[52,21736,21233],{"class":58},[52,21738,20961],{"class":62},[14,21740,21741,21742,21744],{},"Without returning ",[49,21743,21026],{},", the decorated function will not be set up correctly.",[639,21746,21748,21749,21751,21752],{"id":21747},"forgetting-to-call-func-inside-wrapper","Forgetting to call ",[49,21750,21190],{}," inside ",[49,21753,21026],{},[14,21755,8124],{},[42,21757,21759],{"className":44,"code":21758,"language":46,"meta":47,"style":47},"def show_before_after(func):\n    def wrapper():\n        print(\"Before function runs\")\n        print(\"After function runs\")\n    return wrapper\n",[49,21760,21761,21773,21781,21795,21809],{"__ignoreMap":47},[52,21762,21763,21765,21767,21769,21771],{"class":54,"line":55},[52,21764,7410],{"class":1475},[52,21766,20900],{"class":7065},[52,21768,193],{"class":102},[52,21770,20905],{"class":7018},[52,21772,3114],{"class":102},[52,21774,21775,21777,21779],{"class":54,"line":66},[52,21776,7004],{"class":1475},[52,21778,20914],{"class":7065},[52,21780,8146],{"class":102},[52,21782,21783,21785,21787,21789,21791,21793],{"class":54,"line":73},[52,21784,1470],{"class":241},[52,21786,193],{"class":102},[52,21788,115],{"class":83},[52,21790,20927],{"class":87},[52,21792,115],{"class":83},[52,21794,211],{"class":102},[52,21796,21797,21799,21801,21803,21805,21807],{"class":54,"line":94},[52,21798,1470],{"class":241},[52,21800,193],{"class":102},[52,21802,115],{"class":83},[52,21804,20949],{"class":87},[52,21806,115],{"class":83},[52,21808,211],{"class":102},[52,21810,21811,21813],{"class":54,"line":106},[52,21812,20958],{"class":58},[52,21814,20961],{"class":62},[14,21816,21817],{},"This prints the messages, but it never runs the original function.",[639,21819,21821],{"id":21820},"adding-the-decorator-but-never-calling-the-function","Adding the decorator but never calling the function",[14,21823,21824],{},"This code defines the function:",[42,21826,21827],{"className":44,"code":21254,"language":46,"meta":47,"style":47},[49,21828,21829,21835,21843],{"__ignoreMap":47},[52,21830,21831,21833],{"class":54,"line":55},[52,21832,20971],{"class":20970},[52,21834,20974],{"class":7065},[52,21836,21837,21839,21841],{"class":54,"line":66},[52,21838,7410],{"class":1475},[52,21840,20981],{"class":7065},[52,21842,8146],{"class":102},[52,21844,21845,21847,21849,21851,21853,21855],{"class":54,"line":73},[52,21846,1599],{"class":241},[52,21848,193],{"class":102},[52,21850,115],{"class":83},[52,21852,20994],{"class":87},[52,21854,115],{"class":83},[52,21856,211],{"class":102},[14,21858,21859],{},"But nothing happens until you call:",[42,21861,21862],{"className":44,"code":21343,"language":46,"meta":47,"style":47},[49,21863,21864],{"__ignoreMap":47},[52,21865,21866,21868],{"class":54,"line":55},[52,21867,21007],{"class":189},[52,21869,230],{"class":102},[639,21871,21873],{"id":21872},"using-a-wrapper-with-no-parameters-on-a-function-that-needs-arguments","Using a wrapper with no parameters on a function that needs arguments",[14,21875,21876],{},"This will cause a problem:",[42,21878,21880],{"className":44,"code":21879,"language":46,"meta":47,"style":47},"@show_before_after\ndef greet(name):\n    print(f\"Hello, {name}!\")\n\ngreet(\"Sam\")\n",[49,21881,21882,21888,21900,21920,21924],{"__ignoreMap":47},[52,21883,21884,21886],{"class":54,"line":55},[52,21885,20971],{"class":20970},[52,21887,20974],{"class":7065},[52,21889,21890,21892,21894,21896,21898],{"class":54,"line":66},[52,21891,7410],{"class":1475},[52,21893,21503],{"class":7065},[52,21895,193],{"class":102},[52,21897,2147],{"class":7018},[52,21899,3114],{"class":102},[52,21901,21902,21904,21906,21908,21910,21912,21914,21916,21918],{"class":54,"line":73},[52,21903,1599],{"class":241},[52,21905,193],{"class":102},[52,21907,1476],{"class":1475},[52,21909,6129],{"class":87},[52,21911,1482],{"class":121},[52,21913,2147],{"class":189},[52,21915,1488],{"class":121},[52,21917,9245],{"class":87},[52,21919,211],{"class":102},[52,21921,21922],{"class":54,"line":94},[52,21923,70],{"emptyLinePlaceholder":69},[52,21925,21926,21929,21931,21933,21936,21938],{"class":54,"line":106},[52,21927,21928],{"class":189},"greet",[52,21930,193],{"class":102},[52,21932,115],{"class":83},[52,21934,21935],{"class":87},"Sam",[52,21937,115],{"class":83},[52,21939,211],{"class":102},[14,21941,21942,21943,186],{},"Your wrapper has no parameters, so it cannot accept ",[49,21944,21945],{},"\"Sam\"",[639,21947,21949,21950,759,21952],{"id":21948},"mixing-up-func-and-func","Mixing up ",[49,21951,20905],{},[49,21953,21190],{},[14,21955,21956],{},"Remember:",[309,21958,21959,21964],{},[24,21960,21961,21963],{},[49,21962,20905],{}," = the function object",[24,21965,21966,21968],{},[49,21967,21190],{}," = run the function",[14,21970,21971],{},"That small difference matters a lot in Python.",[37,21973,1942],{"id":1941},[639,21975,21977],{"id":21976},"what-is-the-simplest-way-to-describe-a-decorator","What is the simplest way to describe a decorator?",[14,21979,21980],{},"A decorator is a function that changes or extends another function.",[639,21982,21984],{"id":21983},"do-i-need-decorators-as-a-beginner","Do I need decorators as a beginner?",[14,21986,21987],{},"Not at first, but a simple example helps you understand code you may see in real projects.",[639,21989,1956,21991,10598,21993,1995],{"id":21990},"why-use-show_before_after-instead-of-say_hello-show_before_aftersay_hello",[49,21992,21251],{},[49,21994,21995],{},"say_hello = show_before_after(say_hello)",[14,21997,21998,21999,22001],{},"They do the same thing. The ",[49,22000,20971],{}," syntax is shorter and easier to read.",[639,22003,22005],{"id":22004},"why-does-my-decorator-fail-when-my-function-has-parameters","Why does my decorator fail when my function has parameters?",[14,22007,22008,22009,759,22011,186],{},"Your wrapper probably does not accept arguments. Use a wrapper that accepts ",[49,22010,21541],{},[49,22012,21544],{},[37,22014,2005],{"id":2004},[309,22016,22017,22021,22025,22030,22035],{},[24,22018,22019],{},[372,22020,20884],{"href":20883},[24,22022,22023],{},[372,22024,21364],{"href":21363},[24,22026,22027],{},[372,22028,22029],{"href":21367},"How to create a simple function in Python",[24,22031,22032],{},[372,22033,22034],{"href":21071},"What is a function in Python?",[24,22036,22037],{},[372,22038,22040],{"href":22039},"\u002Fglossary\u002Fwhat-is-a-method-in-python\u002F","What is a method in Python?",[2034,22042,22043],{},"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 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 .stp6e, html code.shiki .stp6e{--shiki-light:#39ADB5;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":47,"searchDepth":66,"depth":66,"links":22045},[22046,22047,22048,22049,22062,22063,22064,22065,22066,22076,22083],{"id":20862,"depth":66,"text":20863},{"id":20887,"depth":66,"text":20888},{"id":21044,"depth":66,"text":21045},{"id":21076,"depth":66,"text":21077,"children":22050},[22051,22052,22054,22055,22056,22057,22059,22061],{"id":21080,"depth":73,"text":21081},{"id":21110,"depth":73,"text":22053},"2. Define wrapper() inside the decorator",{"id":21134,"depth":73,"text":21135},{"id":21160,"depth":73,"text":21161},{"id":21194,"depth":73,"text":21195},{"id":21220,"depth":73,"text":22058},"6. Return wrapper",{"id":21247,"depth":73,"text":22060},"7. Apply the decorator with @show_before_after",{"id":21339,"depth":73,"text":21340},{"id":13562,"depth":66,"text":13563},{"id":21396,"depth":66,"text":21397},{"id":21437,"depth":66,"text":21438},{"id":21550,"depth":66,"text":21551},{"id":12321,"depth":66,"text":12322,"children":22067},[22068,22070,22072,22073,22074],{"id":21661,"depth":73,"text":22069},"Forgetting to return wrapper",{"id":21747,"depth":73,"text":22071},"Forgetting to call func() inside wrapper",{"id":21820,"depth":73,"text":21821},{"id":21872,"depth":73,"text":21873},{"id":21948,"depth":73,"text":22075},"Mixing up func and func()",{"id":1941,"depth":66,"text":1942,"children":22077},[22078,22079,22080,22082],{"id":21976,"depth":73,"text":21977},{"id":21983,"depth":73,"text":21984},{"id":21990,"depth":73,"text":22081},"Why use @show_before_after instead of say_hello = show_before_after(say_hello)?",{"id":22004,"depth":73,"text":22005},{"id":2004,"depth":66,"text":2005},"Master python decorator example simple use case in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-decorator-example-simple-use-case",{"title":20844,"description":22084},"examples\u002Fpython-decorator-example-simple-use-case","CXbLtx7v6rfSKdy91M5n-hwct34tOD8BBsSEyn1kaR8",{"id":22091,"title":22092,"body":22093,"description":23708,"extension":2075,"meta":23709,"navigation":69,"path":23710,"seo":23711,"stem":23712,"__hash__":23713},"content\u002Fexamples\u002Fpython-dictionary-processing-example.md","Python Dictionary Processing Example",{"type":7,"value":22094,"toc":23685},[22095,22098,22101,22104,22121,22129,22131,22134,22154,22158,22161,22164,22181,22184,22431,22433,22439,22443,22449,22451,22464,22467,22524,22527,22538,22542,22545,22624,22626,22632,22635,22676,22678,22684,22689,22713,22720,22747,22749,22754,22765,22769,22772,22805,22807,22813,22816,22823,22827,22833,22944,22946,22952,22955,22974,22982,22986,22989,22992,23137,23139,23145,23148,23165,23173,23177,23179,23183,23185,23208,23213,23216,23239,23243,23246,23270,23275,23284,23344,23348,23352,23355,23390,23393,23432,23436,23439,23442,23446,23449,23452,23469,23476,23478,23482,23485,23488,23492,23499,23502,23506,23508,23550,23553,23557,23560,23574,23576,23644,23646,23682],[10,22096,22092],{"id":22097},"python-dictionary-processing-example",[14,22099,22100],{},"This example shows how to process data stored in a Python dictionary.",[14,22102,22103],{},"You will use a small dictionary of student scores and learn how to:",[309,22105,22106,22109,22112,22115,22118],{},[24,22107,22108],{},"read values by key",[24,22110,22111],{},"update an existing value",[24,22113,22114],{},"add a new key-value pair",[24,22116,22117],{},"loop through all entries",[24,22119,22120],{},"calculate a simple result from the data",[14,22122,22123,22124,22126,22127,186],{},"This is ",[5503,22125,20858],{}," a full dictionary tutorial. It is one practical example that helps you see how dictionaries work in a small script. If you want the full concept, see ",[372,22128,10883],{"href":10882},[37,22130,2268],{"id":2267},[14,22132,22133],{},"In this example, you will:",[309,22135,22136,22139,22142,22145,22148,22151],{},[24,22137,22138],{},"Use a dictionary to store related data as key-value pairs",[24,22140,22141],{},"Read values by key",[24,22143,22144],{},"Change existing values",[24,22146,22147],{},"Add new entries",[24,22149,22150],{},"Loop through all entries",[24,22152,22153],{},"Calculate a simple result from dictionary data",[37,22155,22157],{"id":22156},"example-problem","Example problem",[14,22159,22160],{},"Suppose you have student names and test scores.",[14,22162,22163],{},"You want to:",[309,22165,22166,22169,22172,22175,22178],{},[24,22167,22168],{},"start with a small student score dictionary",[24,22170,22171],{},"update one student's score",[24,22173,22174],{},"add a new student",[24,22176,22177],{},"print all names and scores",[24,22179,22180],{},"find the highest score",[14,22182,22183],{},"Here is the complete example:",[42,22185,22187],{"className":44,"code":22186,"language":46,"meta":47,"style":47},"student_scores = {\n    \"Alice\": 85,\n    \"Bob\": 92,\n    \"Cara\": 78\n}\n\n# update a value\nstudent_scores[\"Alice\"] = 88\n\n# add a new key-value pair\nstudent_scores[\"Dan\"] = 95\n\n# loop through the dictionary\nfor name, score in student_scores.items():\n    print(name, score)\n\n# simple summary\nhighest_name = max(student_scores, key=student_scores.get)\nprint(\"Top student:\", highest_name)\nprint(\"Top score:\", student_scores[highest_name])\n",[49,22188,22189,22198,22213,22228,22241,22245,22249,22254,22274,22278,22283,22303,22307,22312,22334,22349,22353,22358,22387,22407],{"__ignoreMap":47},[52,22190,22191,22194,22196],{"class":54,"line":55},[52,22192,22193],{"class":62},"student_scores ",[52,22195,80],{"class":79},[52,22197,103],{"class":102},[52,22199,22200,22202,22204,22206,22208,22211],{"class":54,"line":66},[52,22201,109],{"class":83},[52,22203,2156],{"class":87},[52,22205,115],{"class":83},[52,22207,118],{"class":102},[52,22209,22210],{"class":121}," 85",[52,22212,125],{"class":102},[52,22214,22215,22217,22219,22221,22223,22226],{"class":54,"line":73},[52,22216,109],{"class":83},[52,22218,10858],{"class":87},[52,22220,115],{"class":83},[52,22222,118],{"class":102},[52,22224,22225],{"class":121}," 92",[52,22227,125],{"class":102},[52,22229,22230,22232,22234,22236,22238],{"class":54,"line":94},[52,22231,109],{"class":83},[52,22233,20170],{"class":87},[52,22235,115],{"class":83},[52,22237,118],{"class":102},[52,22239,22240],{"class":121}," 78\n",[52,22242,22243],{"class":54,"line":106},[52,22244,167],{"class":102},[52,22246,22247],{"class":54,"line":128},[52,22248,70],{"emptyLinePlaceholder":69},[52,22250,22251],{"class":54,"line":148},[52,22252,22253],{"class":4368},"# update a value\n",[52,22255,22256,22259,22261,22263,22265,22267,22269,22271],{"class":54,"line":164},[52,22257,22258],{"class":62},"student_scores",[52,22260,250],{"class":102},[52,22262,115],{"class":83},[52,22264,2156],{"class":87},[52,22266,115],{"class":83},[52,22268,11122],{"class":102},[52,22270,7038],{"class":79},[52,22272,22273],{"class":121}," 88\n",[52,22275,22276],{"class":54,"line":170},[52,22277,70],{"emptyLinePlaceholder":69},[52,22279,22280],{"class":54,"line":175},[52,22281,22282],{"class":4368},"# add a new key-value pair\n",[52,22284,22285,22287,22289,22291,22294,22296,22298,22300],{"class":54,"line":214},[52,22286,22258],{"class":62},[52,22288,250],{"class":102},[52,22290,115],{"class":83},[52,22292,22293],{"class":87},"Dan",[52,22295,115],{"class":83},[52,22297,11122],{"class":102},[52,22299,7038],{"class":79},[52,22301,22302],{"class":121}," 95\n",[52,22304,22305],{"class":54,"line":233},[52,22306,70],{"emptyLinePlaceholder":69},[52,22308,22309],{"class":54,"line":238},[52,22310,22311],{"class":4368},"# loop through the dictionary\n",[52,22313,22314,22316,22318,22320,22323,22325,22328,22330,22332],{"class":54,"line":272},[52,22315,12265],{"class":58},[52,22317,7019],{"class":62},[52,22319,199],{"class":102},[52,22321,22322],{"class":62}," score ",[52,22324,11213],{"class":58},[52,22326,22327],{"class":62}," student_scores",[52,22329,186],{"class":102},[52,22331,11220],{"class":189},[52,22333,8146],{"class":102},[52,22335,22336,22338,22340,22342,22344,22347],{"class":54,"line":1348},[52,22337,1599],{"class":241},[52,22339,193],{"class":102},[52,22341,2147],{"class":189},[52,22343,199],{"class":102},[52,22345,22346],{"class":189}," score",[52,22348,211],{"class":102},[52,22350,22351],{"class":54,"line":1376},[52,22352,70],{"emptyLinePlaceholder":69},[52,22354,22355],{"class":54,"line":1381},[52,22356,22357],{"class":4368},"# simple summary\n",[52,22359,22360,22363,22365,22368,22370,22372,22374,22377,22379,22381,22383,22385],{"class":54,"line":1406},[52,22361,22362],{"class":62},"highest_name ",[52,22364,80],{"class":79},[52,22366,22367],{"class":241}," max",[52,22369,193],{"class":102},[52,22371,22258],{"class":189},[52,22373,199],{"class":102},[52,22375,22376],{"class":202}," key",[52,22378,80],{"class":79},[52,22380,22258],{"class":189},[52,22382,186],{"class":102},[52,22384,190],{"class":1320},[52,22386,211],{"class":102},[52,22388,22389,22391,22393,22395,22398,22400,22402,22405],{"class":54,"line":1430},[52,22390,242],{"class":241},[52,22392,193],{"class":102},[52,22394,115],{"class":83},[52,22396,22397],{"class":87},"Top student:",[52,22399,115],{"class":83},[52,22401,199],{"class":102},[52,22403,22404],{"class":189}," highest_name",[52,22406,211],{"class":102},[52,22408,22409,22411,22413,22415,22418,22420,22422,22424,22426,22429],{"class":54,"line":1435},[52,22410,242],{"class":241},[52,22412,193],{"class":102},[52,22414,115],{"class":83},[52,22416,22417],{"class":87},"Top score:",[52,22419,115],{"class":83},[52,22421,199],{"class":102},[52,22423,22327],{"class":189},[52,22425,250],{"class":102},[52,22427,22428],{"class":189},"highest_name",[52,22430,269],{"class":102},[14,22432,11845],{},[42,22434,22437],{"className":22435,"code":22436,"language":955,"meta":47},[953],"Alice 88\nBob 92\nCara 78\nDan 95\nTop student: Dan\nTop score: 95\n",[49,22438,22436],{"__ignoreMap":47},[37,22440,22442],{"id":22441},"build-the-dictionary","Build the dictionary",[14,22444,22445,22446,186],{},"A dictionary stores data as ",[5503,22447,22448],{},"key-value pairs",[14,22450,752],{},[309,22452,22453,22458],{},[24,22454,6728,22455,22457],{},[5503,22456,1813],{}," are student names",[24,22459,6728,22460,22463],{},[5503,22461,22462],{},"values"," are student scores",[14,22465,22466],{},"Start with a small dictionary literal:",[42,22468,22470],{"className":44,"code":22469,"language":46,"meta":47,"style":47},"student_scores = {\n    \"Alice\": 85,\n    \"Bob\": 92,\n    \"Cara\": 78\n}\n",[49,22471,22472,22480,22494,22508,22520],{"__ignoreMap":47},[52,22473,22474,22476,22478],{"class":54,"line":55},[52,22475,22193],{"class":62},[52,22477,80],{"class":79},[52,22479,103],{"class":102},[52,22481,22482,22484,22486,22488,22490,22492],{"class":54,"line":66},[52,22483,109],{"class":83},[52,22485,2156],{"class":87},[52,22487,115],{"class":83},[52,22489,118],{"class":102},[52,22491,22210],{"class":121},[52,22493,125],{"class":102},[52,22495,22496,22498,22500,22502,22504,22506],{"class":54,"line":73},[52,22497,109],{"class":83},[52,22499,10858],{"class":87},[52,22501,115],{"class":83},[52,22503,118],{"class":102},[52,22505,22225],{"class":121},[52,22507,125],{"class":102},[52,22509,22510,22512,22514,22516,22518],{"class":54,"line":94},[52,22511,109],{"class":83},[52,22513,20170],{"class":87},[52,22515,115],{"class":83},[52,22517,118],{"class":102},[52,22519,22240],{"class":121},[52,22521,22522],{"class":54,"line":106},[52,22523,167],{"class":102},[14,22525,22526],{},"This is a good beginner example because:",[309,22528,22529,22532,22535],{},[24,22530,22531],{},"the keys are strings",[24,22533,22534],{},"the values are integers",[24,22536,22537],{},"each name clearly matches one score",[37,22539,22541],{"id":22540},"access-and-update-values","Access and update values",[14,22543,22544],{},"You can read a value by using its key inside square brackets.",[42,22546,22548],{"className":44,"code":22547,"language":46,"meta":47,"style":47},"student_scores = {\n    \"Alice\": 85,\n    \"Bob\": 92,\n    \"Cara\": 78\n}\n\nprint(student_scores[\"Alice\"])\n",[49,22549,22550,22558,22572,22586,22598,22602,22606],{"__ignoreMap":47},[52,22551,22552,22554,22556],{"class":54,"line":55},[52,22553,22193],{"class":62},[52,22555,80],{"class":79},[52,22557,103],{"class":102},[52,22559,22560,22562,22564,22566,22568,22570],{"class":54,"line":66},[52,22561,109],{"class":83},[52,22563,2156],{"class":87},[52,22565,115],{"class":83},[52,22567,118],{"class":102},[52,22569,22210],{"class":121},[52,22571,125],{"class":102},[52,22573,22574,22576,22578,22580,22582,22584],{"class":54,"line":73},[52,22575,109],{"class":83},[52,22577,10858],{"class":87},[52,22579,115],{"class":83},[52,22581,118],{"class":102},[52,22583,22225],{"class":121},[52,22585,125],{"class":102},[52,22587,22588,22590,22592,22594,22596],{"class":54,"line":94},[52,22589,109],{"class":83},[52,22591,20170],{"class":87},[52,22593,115],{"class":83},[52,22595,118],{"class":102},[52,22597,22240],{"class":121},[52,22599,22600],{"class":54,"line":106},[52,22601,167],{"class":102},[52,22603,22604],{"class":54,"line":128},[52,22605,70],{"emptyLinePlaceholder":69},[52,22607,22608,22610,22612,22614,22616,22618,22620,22622],{"class":54,"line":148},[52,22609,242],{"class":241},[52,22611,193],{"class":102},[52,22613,22258],{"class":189},[52,22615,250],{"class":102},[52,22617,115],{"class":83},[52,22619,2156],{"class":87},[52,22621,115],{"class":83},[52,22623,269],{"class":102},[14,22625,5267],{},[42,22627,22630],{"className":22628,"code":22629,"language":955,"meta":47},[953],"85\n",[49,22631,22629],{"__ignoreMap":47},[14,22633,22634],{},"To change a value, assign a new value to the same key:",[42,22636,22638],{"className":44,"code":22637,"language":46,"meta":47,"style":47},"student_scores[\"Alice\"] = 88\nprint(student_scores[\"Alice\"])\n",[49,22639,22640,22658],{"__ignoreMap":47},[52,22641,22642,22644,22646,22648,22650,22652,22654,22656],{"class":54,"line":55},[52,22643,22258],{"class":62},[52,22645,250],{"class":102},[52,22647,115],{"class":83},[52,22649,2156],{"class":87},[52,22651,115],{"class":83},[52,22653,11122],{"class":102},[52,22655,7038],{"class":79},[52,22657,22273],{"class":121},[52,22659,22660,22662,22664,22666,22668,22670,22672,22674],{"class":54,"line":66},[52,22661,242],{"class":241},[52,22663,193],{"class":102},[52,22665,22258],{"class":189},[52,22667,250],{"class":102},[52,22669,115],{"class":83},[52,22671,2156],{"class":87},[52,22673,115],{"class":83},[52,22675,269],{"class":102},[14,22677,5267],{},[42,22679,22682],{"className":22680,"code":22681,"language":955,"meta":47},[953],"88\n",[49,22683,22681],{"__ignoreMap":47},[14,22685,22686,22687,186],{},"If you use square brackets with a key that does not exist, Python raises a ",[49,22688,1701],{},[42,22690,22692],{"className":44,"code":22691,"language":46,"meta":47,"style":47},"print(student_scores[\"Eli\"])\n",[49,22693,22694],{"__ignoreMap":47},[52,22695,22696,22698,22700,22702,22704,22706,22709,22711],{"class":54,"line":55},[52,22697,242],{"class":241},[52,22699,193],{"class":102},[52,22701,22258],{"class":189},[52,22703,250],{"class":102},[52,22705,115],{"class":83},[52,22707,22708],{"class":87},"Eli",[52,22710,115],{"class":83},[52,22712,269],{"class":102},[14,22714,22715,22716,22719],{},"If the key may be missing, use ",[49,22717,22718],{},"get()"," instead:",[42,22721,22723],{"className":44,"code":22722,"language":46,"meta":47,"style":47},"print(student_scores.get(\"Eli\"))\n",[49,22724,22725],{"__ignoreMap":47},[52,22726,22727,22729,22731,22733,22735,22737,22739,22741,22743,22745],{"class":54,"line":55},[52,22728,242],{"class":241},[52,22730,193],{"class":102},[52,22732,22258],{"class":189},[52,22734,186],{"class":102},[52,22736,190],{"class":189},[52,22738,193],{"class":102},[52,22740,115],{"class":83},[52,22742,22708],{"class":87},[52,22744,115],{"class":83},[52,22746,8886],{"class":102},[14,22748,5267],{},[42,22750,22752],{"className":22751,"code":19440,"language":955,"meta":47},[953],[49,22753,19440],{"__ignoreMap":47},[14,22755,22756,22757,4441,22761,186],{},"To learn more, see ",[372,22758,22760],{"href":22759},"\u002Fhow-to\u002Fhow-to-access-values-in-a-dictionary-in-python\u002F","how to access values in a dictionary in Python",[372,22762,22764],{"href":22763},"\u002Freference\u002Fpython-dictionary-get-method\u002F","Python dictionary get() method",[37,22766,22768],{"id":22767},"add-new-data","Add new data",[14,22770,22771],{},"To add a new item, assign a value to a new key:",[42,22773,22775],{"className":44,"code":22774,"language":46,"meta":47,"style":47},"student_scores[\"Dan\"] = 95\nprint(student_scores)\n",[49,22776,22777,22795],{"__ignoreMap":47},[52,22778,22779,22781,22783,22785,22787,22789,22791,22793],{"class":54,"line":55},[52,22780,22258],{"class":62},[52,22782,250],{"class":102},[52,22784,115],{"class":83},[52,22786,22293],{"class":87},[52,22788,115],{"class":83},[52,22790,11122],{"class":102},[52,22792,7038],{"class":79},[52,22794,22302],{"class":121},[52,22796,22797,22799,22801,22803],{"class":54,"line":66},[52,22798,242],{"class":241},[52,22800,193],{"class":102},[52,22802,22258],{"class":189},[52,22804,211],{"class":102},[14,22806,5267],{},[42,22808,22811],{"className":22809,"code":22810,"language":955,"meta":47},[953],"{'Alice': 88, 'Bob': 92, 'Cara': 78, 'Dan': 95}\n",[49,22812,22810],{"__ignoreMap":47},[14,22814,22815],{},"Dictionaries grow automatically. You do not need to create space first.",[14,22817,22818,22819,186],{},"If you want more practice with this step, see ",[372,22820,22822],{"href":22821},"\u002Fhow-to\u002Fhow-to-add-a-key-to-a-dictionary-in-python\u002F","how to add a key to a dictionary in Python",[37,22824,22826],{"id":22825},"loop-through-the-dictionary","Loop through the dictionary",[14,22828,22829,22830,22832],{},"When you need both the key and the value, ",[49,22831,12845],{}," is usually the easiest choice.",[42,22834,22836],{"className":44,"code":22835,"language":46,"meta":47,"style":47},"student_scores = {\n    \"Alice\": 88,\n    \"Bob\": 92,\n    \"Cara\": 78,\n    \"Dan\": 95\n}\n\nfor name, score in student_scores.items():\n    print(name, score)\n",[49,22837,22838,22846,22861,22875,22890,22902,22906,22910,22930],{"__ignoreMap":47},[52,22839,22840,22842,22844],{"class":54,"line":55},[52,22841,22193],{"class":62},[52,22843,80],{"class":79},[52,22845,103],{"class":102},[52,22847,22848,22850,22852,22854,22856,22859],{"class":54,"line":66},[52,22849,109],{"class":83},[52,22851,2156],{"class":87},[52,22853,115],{"class":83},[52,22855,118],{"class":102},[52,22857,22858],{"class":121}," 88",[52,22860,125],{"class":102},[52,22862,22863,22865,22867,22869,22871,22873],{"class":54,"line":73},[52,22864,109],{"class":83},[52,22866,10858],{"class":87},[52,22868,115],{"class":83},[52,22870,118],{"class":102},[52,22872,22225],{"class":121},[52,22874,125],{"class":102},[52,22876,22877,22879,22881,22883,22885,22888],{"class":54,"line":94},[52,22878,109],{"class":83},[52,22880,20170],{"class":87},[52,22882,115],{"class":83},[52,22884,118],{"class":102},[52,22886,22887],{"class":121}," 78",[52,22889,125],{"class":102},[52,22891,22892,22894,22896,22898,22900],{"class":54,"line":106},[52,22893,109],{"class":83},[52,22895,22293],{"class":87},[52,22897,115],{"class":83},[52,22899,118],{"class":102},[52,22901,22302],{"class":121},[52,22903,22904],{"class":54,"line":128},[52,22905,167],{"class":102},[52,22907,22908],{"class":54,"line":148},[52,22909,70],{"emptyLinePlaceholder":69},[52,22911,22912,22914,22916,22918,22920,22922,22924,22926,22928],{"class":54,"line":164},[52,22913,12265],{"class":58},[52,22915,7019],{"class":62},[52,22917,199],{"class":102},[52,22919,22322],{"class":62},[52,22921,11213],{"class":58},[52,22923,22327],{"class":62},[52,22925,186],{"class":102},[52,22927,11220],{"class":189},[52,22929,8146],{"class":102},[52,22931,22932,22934,22936,22938,22940,22942],{"class":54,"line":170},[52,22933,1599],{"class":241},[52,22935,193],{"class":102},[52,22937,2147],{"class":189},[52,22939,199],{"class":102},[52,22941,22346],{"class":189},[52,22943,211],{"class":102},[14,22945,5267],{},[42,22947,22950],{"className":22948,"code":22949,"language":955,"meta":47},[953],"Alice 88\nBob 92\nCara 78\nDan 95\n",[49,22951,22949],{"__ignoreMap":47},[14,22953,22954],{},"Here:",[309,22956,22957,22962,22968],{},[24,22958,22959,22961],{},[49,22960,2147],{}," gets each key",[24,22963,22964,22967],{},[49,22965,22966],{},"score"," gets the matching value",[24,22969,22970,22973],{},[49,22971,22972],{},"student_scores.items()"," gives both parts together",[14,22975,22976,22977,759,22980,186],{},"For more help, see the ",[372,22978,22979],{"href":11838},"Python dictionary items() method",[372,22981,11835],{"href":11834},[37,22983,22985],{"id":22984},"find-a-result-from-the-data","Find a result from the data",[14,22987,22988],{},"You can use the dictionary data to calculate a simple result.",[14,22990,22991],{},"In this case, you want the student with the highest score.",[42,22993,22995],{"className":44,"code":22994,"language":46,"meta":47,"style":47},"student_scores = {\n    \"Alice\": 88,\n    \"Bob\": 92,\n    \"Cara\": 78,\n    \"Dan\": 95\n}\n\nhighest_name = max(student_scores, key=student_scores.get)\n\nprint(\"Top student:\", highest_name)\nprint(\"Top score:\", student_scores[highest_name])\n",[49,22996,22997,23005,23019,23033,23047,23059,23063,23067,23093,23097,23115],{"__ignoreMap":47},[52,22998,22999,23001,23003],{"class":54,"line":55},[52,23000,22193],{"class":62},[52,23002,80],{"class":79},[52,23004,103],{"class":102},[52,23006,23007,23009,23011,23013,23015,23017],{"class":54,"line":66},[52,23008,109],{"class":83},[52,23010,2156],{"class":87},[52,23012,115],{"class":83},[52,23014,118],{"class":102},[52,23016,22858],{"class":121},[52,23018,125],{"class":102},[52,23020,23021,23023,23025,23027,23029,23031],{"class":54,"line":73},[52,23022,109],{"class":83},[52,23024,10858],{"class":87},[52,23026,115],{"class":83},[52,23028,118],{"class":102},[52,23030,22225],{"class":121},[52,23032,125],{"class":102},[52,23034,23035,23037,23039,23041,23043,23045],{"class":54,"line":94},[52,23036,109],{"class":83},[52,23038,20170],{"class":87},[52,23040,115],{"class":83},[52,23042,118],{"class":102},[52,23044,22887],{"class":121},[52,23046,125],{"class":102},[52,23048,23049,23051,23053,23055,23057],{"class":54,"line":106},[52,23050,109],{"class":83},[52,23052,22293],{"class":87},[52,23054,115],{"class":83},[52,23056,118],{"class":102},[52,23058,22302],{"class":121},[52,23060,23061],{"class":54,"line":128},[52,23062,167],{"class":102},[52,23064,23065],{"class":54,"line":148},[52,23066,70],{"emptyLinePlaceholder":69},[52,23068,23069,23071,23073,23075,23077,23079,23081,23083,23085,23087,23089,23091],{"class":54,"line":164},[52,23070,22362],{"class":62},[52,23072,80],{"class":79},[52,23074,22367],{"class":241},[52,23076,193],{"class":102},[52,23078,22258],{"class":189},[52,23080,199],{"class":102},[52,23082,22376],{"class":202},[52,23084,80],{"class":79},[52,23086,22258],{"class":189},[52,23088,186],{"class":102},[52,23090,190],{"class":1320},[52,23092,211],{"class":102},[52,23094,23095],{"class":54,"line":170},[52,23096,70],{"emptyLinePlaceholder":69},[52,23098,23099,23101,23103,23105,23107,23109,23111,23113],{"class":54,"line":175},[52,23100,242],{"class":241},[52,23102,193],{"class":102},[52,23104,115],{"class":83},[52,23106,22397],{"class":87},[52,23108,115],{"class":83},[52,23110,199],{"class":102},[52,23112,22404],{"class":189},[52,23114,211],{"class":102},[52,23116,23117,23119,23121,23123,23125,23127,23129,23131,23133,23135],{"class":54,"line":214},[52,23118,242],{"class":241},[52,23120,193],{"class":102},[52,23122,115],{"class":83},[52,23124,22417],{"class":87},[52,23126,115],{"class":83},[52,23128,199],{"class":102},[52,23130,22327],{"class":189},[52,23132,250],{"class":102},[52,23134,22428],{"class":189},[52,23136,269],{"class":102},[14,23138,5267],{},[42,23140,23143],{"className":23141,"code":23142,"language":955,"meta":47},[953],"Top student: Dan\nTop score: 95\n",[49,23144,23142],{"__ignoreMap":47},[14,23146,23147],{},"How this works:",[309,23149,23150,23156,23162],{},[24,23151,23152,23155],{},[49,23153,23154],{},"max(student_scores, key=student_scores.get)"," checks the dictionary keys",[24,23157,23158,23159],{},"for each key, it looks up the score with ",[49,23160,23161],{},"student_scores.get",[24,23163,23164],{},"it returns the key with the largest value",[14,23166,23167,23168,6230,23170,186],{},"So ",[49,23169,22428],{},[49,23171,23172],{},"\"Dan\"",[37,23174,23176],{"id":23175},"common-beginner-mistakes-in-this-example","Common beginner mistakes in this example",[14,23178,17965],{},[639,23180,23182],{"id":23181},"using-parentheses-instead-of-square-brackets","Using parentheses instead of square brackets",[14,23184,6505],{},[42,23186,23188],{"className":44,"code":23187,"language":46,"meta":47,"style":47},"print(student_scores(\"Alice\"))\n",[49,23189,23190],{"__ignoreMap":47},[52,23191,23192,23194,23196,23198,23200,23202,23204,23206],{"class":54,"line":55},[52,23193,242],{"class":241},[52,23195,193],{"class":102},[52,23197,22258],{"class":189},[52,23199,193],{"class":102},[52,23201,115],{"class":83},[52,23203,2156],{"class":87},[52,23205,115],{"class":83},[52,23207,8886],{"class":102},[14,23209,23210,23211,186],{},"A dictionary is not a function, so this causes a ",[49,23212,1707],{},[14,23214,23215],{},"Use square brackets instead:",[42,23217,23219],{"className":44,"code":23218,"language":46,"meta":47,"style":47},"print(student_scores[\"Alice\"])\n",[49,23220,23221],{"__ignoreMap":47},[52,23222,23223,23225,23227,23229,23231,23233,23235,23237],{"class":54,"line":55},[52,23224,242],{"class":241},[52,23226,193],{"class":102},[52,23228,22258],{"class":189},[52,23230,250],{"class":102},[52,23232,115],{"class":83},[52,23234,2156],{"class":87},[52,23236,115],{"class":83},[52,23238,269],{"class":102},[639,23240,23242],{"id":23241},"misspelling-a-key-name","Misspelling a key name",[14,23244,23245],{},"This is wrong if the key does not exist:",[42,23247,23249],{"className":44,"code":23248,"language":46,"meta":47,"style":47},"print(student_scores[\"Alic\"])\n",[49,23250,23251],{"__ignoreMap":47},[52,23252,23253,23255,23257,23259,23261,23263,23266,23268],{"class":54,"line":55},[52,23254,242],{"class":241},[52,23256,193],{"class":102},[52,23258,22258],{"class":189},[52,23260,250],{"class":102},[52,23262,115],{"class":83},[52,23264,23265],{"class":87},"Alic",[52,23267,115],{"class":83},[52,23269,269],{"class":102},[14,23271,23272,23273,186],{},"That causes a ",[49,23274,1701],{},[14,23276,23277,23278,23280,23281,23283],{},"If you are not sure whether the key exists, use ",[49,23279,22718],{}," or check with ",[49,23282,11213],{}," first.",[42,23285,23287],{"className":44,"code":23286,"language":46,"meta":47,"style":47},"if \"Alic\" in student_scores:\n    print(student_scores[\"Alic\"])\nelse:\n    print(\"Key not found\")\n",[49,23288,23289,23305,23323,23329],{"__ignoreMap":47},[52,23290,23291,23293,23295,23297,23299,23301,23303],{"class":54,"line":55},[52,23292,1313],{"class":58},[52,23294,84],{"class":83},[52,23296,23265],{"class":87},[52,23298,115],{"class":83},[52,23300,3101],{"class":79},[52,23302,22327],{"class":62},[52,23304,1330],{"class":102},[52,23306,23307,23309,23311,23313,23315,23317,23319,23321],{"class":54,"line":66},[52,23308,1599],{"class":241},[52,23310,193],{"class":102},[52,23312,22258],{"class":189},[52,23314,250],{"class":102},[52,23316,115],{"class":83},[52,23318,23265],{"class":87},[52,23320,115],{"class":83},[52,23322,269],{"class":102},[52,23324,23325,23327],{"class":54,"line":73},[52,23326,1591],{"class":58},[52,23328,1330],{"class":102},[52,23330,23331,23333,23335,23337,23340,23342],{"class":54,"line":94},[52,23332,1599],{"class":241},[52,23334,193],{"class":102},[52,23336,115],{"class":83},[52,23338,23339],{"class":87},"Key not found",[52,23341,115],{"class":83},[52,23343,211],{"class":102},[14,23345,15900,23346,186],{},[372,23347,1721],{"href":1720},[639,23349,23351],{"id":23350},"looping-over-keys-only-when-both-key-and-value-are-needed","Looping over keys only when both key and value are needed",[14,23353,23354],{},"This works, but it is less clear for beginners:",[42,23356,23358],{"className":44,"code":23357,"language":46,"meta":47,"style":47},"for name in student_scores:\n    print(name, student_scores[name])\n",[49,23359,23360,23372],{"__ignoreMap":47},[52,23361,23362,23364,23366,23368,23370],{"class":54,"line":55},[52,23363,12265],{"class":58},[52,23365,11292],{"class":62},[52,23367,11213],{"class":58},[52,23369,22327],{"class":62},[52,23371,1330],{"class":102},[52,23373,23374,23376,23378,23380,23382,23384,23386,23388],{"class":54,"line":66},[52,23375,1599],{"class":241},[52,23377,193],{"class":102},[52,23379,2147],{"class":189},[52,23381,199],{"class":102},[52,23383,22327],{"class":189},[52,23385,250],{"class":102},[52,23387,2147],{"class":189},[52,23389,269],{"class":102},[14,23391,23392],{},"A cleaner beginner-friendly version is:",[42,23394,23396],{"className":44,"code":23395,"language":46,"meta":47,"style":47},"for name, score in student_scores.items():\n    print(name, score)\n",[49,23397,23398,23418],{"__ignoreMap":47},[52,23399,23400,23402,23404,23406,23408,23410,23412,23414,23416],{"class":54,"line":55},[52,23401,12265],{"class":58},[52,23403,7019],{"class":62},[52,23405,199],{"class":102},[52,23407,22322],{"class":62},[52,23409,11213],{"class":58},[52,23411,22327],{"class":62},[52,23413,186],{"class":102},[52,23415,11220],{"class":189},[52,23417,8146],{"class":102},[52,23419,23420,23422,23424,23426,23428,23430],{"class":54,"line":66},[52,23421,1599],{"class":241},[52,23423,193],{"class":102},[52,23425,2147],{"class":189},[52,23427,199],{"class":102},[52,23429,22346],{"class":189},[52,23431,211],{"class":102},[639,23433,23435],{"id":23434},"expecting-dictionaries-to-keep-data-in-sorted-order-automatically","Expecting dictionaries to keep data in sorted order automatically",[14,23437,23438],{},"A dictionary stores key-value pairs, but it does not automatically sort them by key or value.",[14,23440,23441],{},"If you need sorted output, you must sort it yourself.",[37,23443,23445],{"id":23444},"when-to-use-dictionary-processing","When to use dictionary processing",[14,23447,23448],{},"Dictionary processing is useful when you want to connect readable names to values.",[14,23450,23451],{},"Common examples:",[309,23453,23454,23457,23460,23463,23466],{},[24,23455,23456],{},"counting things by name",[24,23458,23459],{},"storing settings",[24,23461,23462],{},"tracking scores or prices",[24,23464,23465],{},"working with JSON-like data",[24,23467,23468],{},"grouping related values under readable keys",[14,23470,23471,23472,186],{},"If you want to see a related example, read the ",[372,23473,23475],{"href":23474},"\u002Fexamples\u002Fpython-json-to-dictionary-example\u002F","Python JSON to dictionary example",[37,23477,1942],{"id":1941},[639,23479,23481],{"id":23480},"is-this-page-a-dictionary-tutorial","Is this page a dictionary tutorial?",[14,23483,23484],{},"No. It is a practical example page.",[14,23486,23487],{},"It shows one small real use case and points you to more detailed pages if you want to go deeper.",[639,23489,23491],{"id":23490},"should-this-page-explain-every-dictionary-method","Should this page explain every dictionary method?",[14,23493,23494,23495,759,23497,186],{},"No. It only uses the methods needed for this example, such as ",[49,23496,12845],{},[49,23498,22718],{},[14,23500,23501],{},"For more detail, use the reference pages for those methods.",[639,23503,23505],{"id":23504},"what-is-the-best-beginner-way-to-loop-through-a-dictionary","What is the best beginner way to loop through a dictionary?",[14,23507,10462],{},[42,23509,23511],{"className":44,"code":23510,"language":46,"meta":47,"style":47},"for key, value in my_dict.items():\n    print(key, value)\n",[49,23512,23513,23535],{"__ignoreMap":47},[52,23514,23515,23517,23519,23521,23524,23526,23529,23531,23533],{"class":54,"line":55},[52,23516,12265],{"class":58},[52,23518,22376],{"class":62},[52,23520,199],{"class":102},[52,23522,23523],{"class":62}," value ",[52,23525,11213],{"class":58},[52,23527,23528],{"class":62}," my_dict",[52,23530,186],{"class":102},[52,23532,11220],{"class":189},[52,23534,8146],{"class":102},[52,23536,23537,23539,23541,23543,23545,23548],{"class":54,"line":66},[52,23538,1599],{"class":241},[52,23540,193],{"class":102},[52,23542,10809],{"class":189},[52,23544,199],{"class":102},[52,23546,23547],{"class":189}," value",[52,23549,211],{"class":102},[14,23551,23552],{},"This is usually the easiest way when you need both the key and the value.",[639,23554,23556],{"id":23555},"how-do-i-avoid-keyerror-in-this-example","How do I avoid KeyError in this example?",[14,23558,23559],{},"You can:",[309,23561,23562,23568],{},[24,23563,23564,23565,23567],{},"use ",[49,23566,22718],{}," when a key might be missing",[24,23569,23570,23571,23573],{},"check with ",[49,23572,11213],{}," before reading the value",[14,23575,16291],{},[42,23577,23579],{"className":44,"code":23578,"language":46,"meta":47,"style":47},"name = \"Eli\"\n\nif name in student_scores:\n    print(student_scores[name])\nelse:\n    print(\"Student not found\")\n",[49,23580,23581,23593,23597,23609,23623,23629],{"__ignoreMap":47},[52,23582,23583,23585,23587,23589,23591],{"class":54,"line":55},[52,23584,9211],{"class":62},[52,23586,80],{"class":79},[52,23588,84],{"class":83},[52,23590,22708],{"class":87},[52,23592,91],{"class":83},[52,23594,23595],{"class":54,"line":66},[52,23596,70],{"emptyLinePlaceholder":69},[52,23598,23599,23601,23603,23605,23607],{"class":54,"line":73},[52,23600,1313],{"class":58},[52,23602,11292],{"class":62},[52,23604,11213],{"class":79},[52,23606,22327],{"class":62},[52,23608,1330],{"class":102},[52,23610,23611,23613,23615,23617,23619,23621],{"class":54,"line":94},[52,23612,1599],{"class":241},[52,23614,193],{"class":102},[52,23616,22258],{"class":189},[52,23618,250],{"class":102},[52,23620,2147],{"class":189},[52,23622,269],{"class":102},[52,23624,23625,23627],{"class":54,"line":106},[52,23626,1591],{"class":58},[52,23628,1330],{"class":102},[52,23630,23631,23633,23635,23637,23640,23642],{"class":54,"line":128},[52,23632,1599],{"class":241},[52,23634,193],{"class":102},[52,23636,115],{"class":83},[52,23638,23639],{"class":87},"Student not found",[52,23641,115],{"class":83},[52,23643,211],{"class":102},[37,23645,2005],{"id":2004},[309,23647,23648,23652,23656,23660,23664,23669,23674,23678],{},[24,23649,23650],{},[372,23651,10883],{"href":10882},[24,23653,23654],{},[372,23655,22979],{"href":11838},[24,23657,23658],{},[372,23659,22764],{"href":22763},[24,23661,23662],{},[372,23663,12908],{"href":11834},[24,23665,23666],{},[372,23667,23668],{"href":22759},"How to access values in a dictionary in Python",[24,23670,23671],{},[372,23672,23673],{"href":22821},"How to add a key to a dictionary in Python",[24,23675,23676],{},[372,23677,1721],{"href":1720},[24,23679,23680],{},[372,23681,23475],{"href":23474},[2034,23683,23684],{},"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 .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 .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 .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 .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);}",{"title":47,"searchDepth":66,"depth":66,"links":23686},[23687,23688,23689,23690,23691,23692,23693,23694,23700,23701,23707],{"id":2267,"depth":66,"text":2268},{"id":22156,"depth":66,"text":22157},{"id":22441,"depth":66,"text":22442},{"id":22540,"depth":66,"text":22541},{"id":22767,"depth":66,"text":22768},{"id":22825,"depth":66,"text":22826},{"id":22984,"depth":66,"text":22985},{"id":23175,"depth":66,"text":23176,"children":23695},[23696,23697,23698,23699],{"id":23181,"depth":73,"text":23182},{"id":23241,"depth":73,"text":23242},{"id":23350,"depth":73,"text":23351},{"id":23434,"depth":73,"text":23435},{"id":23444,"depth":66,"text":23445},{"id":1941,"depth":66,"text":1942,"children":23702},[23703,23704,23705,23706],{"id":23480,"depth":73,"text":23481},{"id":23490,"depth":73,"text":23491},{"id":23504,"depth":73,"text":23505},{"id":23555,"depth":73,"text":23556},{"id":2004,"depth":66,"text":2005},"Master python dictionary processing example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-dictionary-processing-example",{"title":22092,"description":23708},"examples\u002Fpython-dictionary-processing-example","IK0sAz3b7b-X8O5ZlPC-ARtC8HbU9ry-rWS67-w30rw",{"id":23715,"title":23716,"body":23717,"description":25274,"extension":2075,"meta":25275,"navigation":69,"path":25276,"seo":25277,"stem":25278,"__hash__":25279},"content\u002Fexamples\u002Fpython-download-files-from-a-url-example.md","Python Download Files from a URL Example",{"type":7,"value":23718,"toc":25245},[23719,23722,23725,23727,23746,23748,23880,23886,23888,23906,23908,23911,23925,23930,23942,23945,23956,23960,23963,24092,24096,24101,24111,24117,24126,24130,24147,24150,24154,24177,24180,24186,24190,24205,24208,24225,24228,24232,24267,24272,24280,24284,24307,24313,24320,24328,24331,24345,24350,24356,24360,24363,24510,24513,24524,24528,24531,24821,24824,24838,24840,24843,24848,24851,24872,24875,24887,24891,24894,24897,24943,24945,25048,25051,25062,25069,25073,25076,25078,25101,25105,25108,25111,25122,25131,25135,25138,25149,25152,25155,25157,25164,25170,25178,25183,25187,25198,25202,25205,25207,25242],[10,23720,23716],{"id":23721},"python-download-files-from-a-url-example",[14,23723,23724],{},"This beginner-friendly example shows how to download a file from a URL in Python and save it on your computer.",[14,23726,6959],{},[309,23728,23729,23732,23735,23740,23743],{},[24,23730,23731],{},"download a file from a web address",[24,23733,23734],{},"save it locally",[24,23736,23564,23737,23739],{},[49,23738,774],{}," to fetch the file",[24,23741,23742],{},"write the file safely with binary mode",[24,23744,23745],{},"handle common problems like bad URLs and missing packages",[37,23747,40],{"id":39},[42,23749,23751],{"className":44,"code":23750,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\nresponse = requests.get(url)\nresponse.raise_for_status()\n\nwith open(\"file.txt\", \"wb\") as file:\n    file.write(response.content)\n\nprint(\"Download complete\")\n",[49,23752,23753,23759,23763,23776,23794,23804,23808,23840,23861,23865],{"__ignoreMap":47},[52,23754,23755,23757],{"class":54,"line":55},[52,23756,59],{"class":58},[52,23758,63],{"class":62},[52,23760,23761],{"class":54,"line":66},[52,23762,70],{"emptyLinePlaceholder":69},[52,23764,23765,23767,23769,23771,23774],{"class":54,"line":73},[52,23766,76],{"class":62},[52,23768,80],{"class":79},[52,23770,84],{"class":83},[52,23772,23773],{"class":87},"https:\u002F\u002Fexample.com\u002Ffile.txt",[52,23775,91],{"class":83},[52,23777,23778,23780,23782,23784,23786,23788,23790,23792],{"class":54,"line":94},[52,23779,178],{"class":62},[52,23781,80],{"class":79},[52,23783,183],{"class":62},[52,23785,186],{"class":102},[52,23787,190],{"class":189},[52,23789,193],{"class":102},[52,23791,196],{"class":189},[52,23793,211],{"class":102},[52,23795,23796,23798,23800,23802],{"class":54,"line":106},[52,23797,1756],{"class":62},[52,23799,186],{"class":102},[52,23801,3385],{"class":189},[52,23803,230],{"class":102},[52,23805,23806],{"class":54,"line":128},[52,23807,70],{"emptyLinePlaceholder":69},[52,23809,23810,23812,23814,23816,23818,23821,23823,23825,23827,23830,23832,23834,23836,23838],{"class":54,"line":148},[52,23811,14632],{"class":58},[52,23813,14635],{"class":241},[52,23815,193],{"class":102},[52,23817,115],{"class":83},[52,23819,23820],{"class":87},"file.txt",[52,23822,115],{"class":83},[52,23824,199],{"class":102},[52,23826,84],{"class":83},[52,23828,23829],{"class":87},"wb",[52,23831,115],{"class":83},[52,23833,1521],{"class":102},[52,23835,3567],{"class":58},[52,23837,14676],{"class":14675},[52,23839,1330],{"class":102},[52,23841,23842,23845,23847,23850,23852,23854,23856,23859],{"class":54,"line":164},[52,23843,23844],{"class":14675},"    file",[52,23846,186],{"class":102},[52,23848,23849],{"class":189},"write",[52,23851,193],{"class":102},[52,23853,1756],{"class":189},[52,23855,186],{"class":102},[52,23857,23858],{"class":1320},"content",[52,23860,211],{"class":102},[52,23862,23863],{"class":54,"line":170},[52,23864,70],{"emptyLinePlaceholder":69},[52,23866,23867,23869,23871,23873,23876,23878],{"class":54,"line":175},[52,23868,242],{"class":241},[52,23870,193],{"class":102},[52,23872,115],{"class":83},[52,23874,23875],{"class":87},"Download complete",[52,23877,115],{"class":83},[52,23879,211],{"class":102},[14,23881,23882,23883,23885],{},"This is the fastest working example. It downloads the file and saves it as ",[49,23884,23820],{}," in the current folder.",[37,23887,323],{"id":322},[309,23889,23890,23893,23896,23901],{},[24,23891,23892],{},"Downloads a file from a web address",[24,23894,23895],{},"Saves the file to your computer",[24,23897,3609,23898,23900],{},[49,23899,774],{}," to fetch data",[24,23902,3609,23903,23905],{},[49,23904,23829],{}," mode to write binary data safely",[37,23907,347],{"id":346},[14,23909,23910],{},"Before you run the script, make sure you have:",[309,23912,23913,23915,23919,23922],{},[24,23914,355],{},[24,23916,358,23917,3910],{},[49,23918,303],{},[24,23920,23921],{},"A valid file URL",[24,23923,23924],{},"Permission to write files in the current folder",[14,23926,7526,23927,23929],{},[49,23928,303],{}," is not installed, run:",[42,23931,23932],{"className":390,"code":391,"language":392,"meta":47,"style":47},[49,23933,23934],{"__ignoreMap":47},[52,23935,23936,23938,23940],{"class":54,"line":55},[52,23937,400],{"class":399},[52,23939,403],{"class":87},[52,23941,63],{"class":87},[14,23943,23944],{},"If you are not sure Python is installed correctly, check with:",[42,23946,23948],{"className":390,"code":23947,"language":392,"meta":47,"style":47},"python --version\n",[49,23949,23950],{"__ignoreMap":47},[52,23951,23952,23954],{"class":54,"line":55},[52,23953,46],{"class":399},[52,23955,2324],{"class":420},[37,23957,23959],{"id":23958},"main-example-download-and-save-a-file","Main example: download and save a file",[14,23961,23962],{},"Here is the full beginner-friendly version again:",[42,23964,23966],{"className":44,"code":23965,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\n\nresponse = requests.get(url)\nresponse.raise_for_status()\n\nwith open(\"file.txt\", \"wb\") as file:\n    file.write(response.content)\n\nprint(\"Download complete\")\n",[49,23967,23968,23974,23978,23990,23994,24012,24022,24026,24056,24074,24078],{"__ignoreMap":47},[52,23969,23970,23972],{"class":54,"line":55},[52,23971,59],{"class":58},[52,23973,63],{"class":62},[52,23975,23976],{"class":54,"line":66},[52,23977,70],{"emptyLinePlaceholder":69},[52,23979,23980,23982,23984,23986,23988],{"class":54,"line":73},[52,23981,76],{"class":62},[52,23983,80],{"class":79},[52,23985,84],{"class":83},[52,23987,23773],{"class":87},[52,23989,91],{"class":83},[52,23991,23992],{"class":54,"line":94},[52,23993,70],{"emptyLinePlaceholder":69},[52,23995,23996,23998,24000,24002,24004,24006,24008,24010],{"class":54,"line":106},[52,23997,178],{"class":62},[52,23999,80],{"class":79},[52,24001,183],{"class":62},[52,24003,186],{"class":102},[52,24005,190],{"class":189},[52,24007,193],{"class":102},[52,24009,196],{"class":189},[52,24011,211],{"class":102},[52,24013,24014,24016,24018,24020],{"class":54,"line":128},[52,24015,1756],{"class":62},[52,24017,186],{"class":102},[52,24019,3385],{"class":189},[52,24021,230],{"class":102},[52,24023,24024],{"class":54,"line":148},[52,24025,70],{"emptyLinePlaceholder":69},[52,24027,24028,24030,24032,24034,24036,24038,24040,24042,24044,24046,24048,24050,24052,24054],{"class":54,"line":164},[52,24029,14632],{"class":58},[52,24031,14635],{"class":241},[52,24033,193],{"class":102},[52,24035,115],{"class":83},[52,24037,23820],{"class":87},[52,24039,115],{"class":83},[52,24041,199],{"class":102},[52,24043,84],{"class":83},[52,24045,23829],{"class":87},[52,24047,115],{"class":83},[52,24049,1521],{"class":102},[52,24051,3567],{"class":58},[52,24053,14676],{"class":14675},[52,24055,1330],{"class":102},[52,24057,24058,24060,24062,24064,24066,24068,24070,24072],{"class":54,"line":170},[52,24059,23844],{"class":14675},[52,24061,186],{"class":102},[52,24063,23849],{"class":189},[52,24065,193],{"class":102},[52,24067,1756],{"class":189},[52,24069,186],{"class":102},[52,24071,23858],{"class":1320},[52,24073,211],{"class":102},[52,24075,24076],{"class":54,"line":175},[52,24077,70],{"emptyLinePlaceholder":69},[52,24079,24080,24082,24084,24086,24088,24090],{"class":54,"line":214},[52,24081,242],{"class":241},[52,24083,193],{"class":102},[52,24085,115],{"class":83},[52,24087,23875],{"class":87},[52,24089,115],{"class":83},[52,24091,211],{"class":102},[639,24093,24095],{"id":24094},"how-it-works","How it works",[24097,24098,17119,24099],"h4",{"id":641},[49,24100,303],{},[42,24102,24103],{"className":44,"code":645,"language":46,"meta":47,"style":47},[49,24104,24105],{"__ignoreMap":47},[52,24106,24107,24109],{"class":54,"line":55},[52,24108,59],{"class":58},[52,24110,63],{"class":62},[14,24112,24113,24114,24116],{},"This imports the ",[49,24115,303],{}," package so your script can make an HTTP request.",[14,24118,24119,24120,186],{},"If this line gives an error, see ",[372,24121,3984,24123],{"href":24122},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix",[49,24124,24125],{},"ModuleNotFoundError: No module named x",[24097,24127,24129],{"id":24128},"_2-store-the-url","2. Store the URL",[42,24131,24133],{"className":44,"code":24132,"language":46,"meta":47,"style":47},"url = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\n",[49,24134,24135],{"__ignoreMap":47},[52,24136,24137,24139,24141,24143,24145],{"class":54,"line":55},[52,24138,76],{"class":62},[52,24140,80],{"class":79},[52,24142,84],{"class":83},[52,24144,23773],{"class":87},[52,24146,91],{"class":83},[14,24148,24149],{},"Replace this with the direct URL of the file you want to download.",[24097,24151,24153],{"id":24152},"_3-send-a-get-request","3. Send a GET request",[42,24155,24157],{"className":44,"code":24156,"language":46,"meta":47,"style":47},"response = requests.get(url)\n",[49,24158,24159],{"__ignoreMap":47},[52,24160,24161,24163,24165,24167,24169,24171,24173,24175],{"class":54,"line":55},[52,24162,178],{"class":62},[52,24164,80],{"class":79},[52,24166,183],{"class":62},[52,24168,186],{"class":102},[52,24170,190],{"class":189},[52,24172,193],{"class":102},[52,24174,196],{"class":189},[52,24176,211],{"class":102},[14,24178,24179],{},"This asks the server for the file.",[14,24181,24182,24183,186],{},"If you want to learn more about requests first, see ",[372,24184,375],{"href":24185},"\u002Fhow-to\u002Fhow-to-make-an-api-request-in-python",[24097,24187,24189],{"id":24188},"_4-check-for-http-errors","4. Check for HTTP errors",[42,24191,24193],{"className":44,"code":24192,"language":46,"meta":47,"style":47},"response.raise_for_status()\n",[49,24194,24195],{"__ignoreMap":47},[52,24196,24197,24199,24201,24203],{"class":54,"line":55},[52,24198,1756],{"class":62},[52,24200,186],{"class":102},[52,24202,3385],{"class":189},[52,24204,230],{"class":102},[14,24206,24207],{},"This stops the program if the server returns an error such as:",[309,24209,24210,24215,24220],{},[24,24211,24212],{},[49,24213,24214],{},"404 Not Found",[24,24216,24217],{},[49,24218,24219],{},"403 Forbidden",[24,24221,24222],{},[49,24223,24224],{},"500 Internal Server Error",[14,24226,24227],{},"This is better than saving a bad response without noticing.",[24097,24229,24231],{"id":24230},"_5-open-a-local-file-in-binary-write-mode","5. Open a local file in binary write mode",[42,24233,24235],{"className":44,"code":24234,"language":46,"meta":47,"style":47},"with open(\"file.txt\", \"wb\") as file:\n",[49,24236,24237],{"__ignoreMap":47},[52,24238,24239,24241,24243,24245,24247,24249,24251,24253,24255,24257,24259,24261,24263,24265],{"class":54,"line":55},[52,24240,14632],{"class":58},[52,24242,14635],{"class":241},[52,24244,193],{"class":102},[52,24246,115],{"class":83},[52,24248,23820],{"class":87},[52,24250,115],{"class":83},[52,24252,199],{"class":102},[52,24254,84],{"class":83},[52,24256,23829],{"class":87},[52,24258,115],{"class":83},[52,24260,1521],{"class":102},[52,24262,3567],{"class":58},[52,24264,14676],{"class":14675},[52,24266,1330],{"class":102},[14,24268,24269,24270,23885],{},"This creates a local file named ",[49,24271,23820],{},[14,24273,14977,24274,24276,24277,186],{},[49,24275,14980],{}," better, read ",[372,24278,14985],{"href":24279},"\u002Freference\u002Fpython-open-function-explained",[24097,24281,24283],{"id":24282},"_6-write-the-downloaded-content","6. Write the downloaded content",[42,24285,24287],{"className":44,"code":24286,"language":46,"meta":47,"style":47},"file.write(response.content)\n",[49,24288,24289],{"__ignoreMap":47},[52,24290,24291,24293,24295,24297,24299,24301,24303,24305],{"class":54,"line":55},[52,24292,14698],{"class":14675},[52,24294,186],{"class":102},[52,24296,23849],{"class":189},[52,24298,193],{"class":102},[52,24300,1756],{"class":189},[52,24302,186],{"class":102},[52,24304,23858],{"class":1320},[52,24306,211],{"class":102},[14,24308,24309,24312],{},[49,24310,24311],{},"response.content"," contains the downloaded bytes. The script writes those bytes into the file.",[37,24314,24316,24317,24319],{"id":24315},"why-wb-mode-is-used","Why ",[49,24318,23829],{}," mode is used",[14,24321,24322,24324,24325,186],{},[49,24323,23829],{}," means ",[5503,24326,24327],{},"write binary",[14,24329,24330],{},"This matters because downloaded files are often not plain text. Binary mode works safely for:",[309,24332,24333,24336,24339,24342],{},[24,24334,24335],{},"images",[24,24337,24338],{},"PDFs",[24,24340,24341],{},"ZIP files",[24,24343,24344],{},"text files",[14,24346,10294,24347,24349],{},[49,24348,23829],{}," helps avoid file corruption, especially for non-text files.",[14,24351,24352,24353,186],{},"If you want a broader introduction to reading and writing files, see ",[372,24354,16513],{"href":24355},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write",[37,24357,24359],{"id":24358},"example-with-a-custom-filename","Example with a custom filename",[14,24361,24362],{},"You do not have to save the file with the same name as the URL. You can choose your own filename.",[42,24364,24366],{"className":44,"code":24365,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\noutput_file = \"downloaded_file.txt\"\n\nresponse = requests.get(url)\nresponse.raise_for_status()\n\nwith open(output_file, \"wb\") as file:\n    file.write(response.content)\n\nprint(f\"Saved as {output_file}\")\n",[49,24367,24368,24374,24378,24390,24404,24408,24426,24436,24440,24467,24485,24489],{"__ignoreMap":47},[52,24369,24370,24372],{"class":54,"line":55},[52,24371,59],{"class":58},[52,24373,63],{"class":62},[52,24375,24376],{"class":54,"line":66},[52,24377,70],{"emptyLinePlaceholder":69},[52,24379,24380,24382,24384,24386,24388],{"class":54,"line":73},[52,24381,76],{"class":62},[52,24383,80],{"class":79},[52,24385,84],{"class":83},[52,24387,23773],{"class":87},[52,24389,91],{"class":83},[52,24391,24392,24395,24397,24399,24402],{"class":54,"line":94},[52,24393,24394],{"class":62},"output_file ",[52,24396,80],{"class":79},[52,24398,84],{"class":83},[52,24400,24401],{"class":87},"downloaded_file.txt",[52,24403,91],{"class":83},[52,24405,24406],{"class":54,"line":106},[52,24407,70],{"emptyLinePlaceholder":69},[52,24409,24410,24412,24414,24416,24418,24420,24422,24424],{"class":54,"line":128},[52,24411,178],{"class":62},[52,24413,80],{"class":79},[52,24415,183],{"class":62},[52,24417,186],{"class":102},[52,24419,190],{"class":189},[52,24421,193],{"class":102},[52,24423,196],{"class":189},[52,24425,211],{"class":102},[52,24427,24428,24430,24432,24434],{"class":54,"line":148},[52,24429,1756],{"class":62},[52,24431,186],{"class":102},[52,24433,3385],{"class":189},[52,24435,230],{"class":102},[52,24437,24438],{"class":54,"line":164},[52,24439,70],{"emptyLinePlaceholder":69},[52,24441,24442,24444,24446,24448,24451,24453,24455,24457,24459,24461,24463,24465],{"class":54,"line":170},[52,24443,14632],{"class":58},[52,24445,14635],{"class":241},[52,24447,193],{"class":102},[52,24449,24450],{"class":189},"output_file",[52,24452,199],{"class":102},[52,24454,84],{"class":83},[52,24456,23829],{"class":87},[52,24458,115],{"class":83},[52,24460,1521],{"class":102},[52,24462,3567],{"class":58},[52,24464,14676],{"class":14675},[52,24466,1330],{"class":102},[52,24468,24469,24471,24473,24475,24477,24479,24481,24483],{"class":54,"line":175},[52,24470,23844],{"class":14675},[52,24472,186],{"class":102},[52,24474,23849],{"class":189},[52,24476,193],{"class":102},[52,24478,1756],{"class":189},[52,24480,186],{"class":102},[52,24482,23858],{"class":1320},[52,24484,211],{"class":102},[52,24486,24487],{"class":54,"line":214},[52,24488,70],{"emptyLinePlaceholder":69},[52,24490,24491,24493,24495,24497,24500,24502,24504,24506,24508],{"class":54,"line":233},[52,24492,242],{"class":241},[52,24494,193],{"class":102},[52,24496,1476],{"class":1475},[52,24498,24499],{"class":87},"\"Saved as ",[52,24501,1482],{"class":121},[52,24503,24450],{"class":189},[52,24505,1488],{"class":121},[52,24507,115],{"class":87},[52,24509,211],{"class":102},[14,24511,24512],{},"This version:",[309,24514,24515,24518,24521],{},[24,24516,24517],{},"uses a custom output filename",[24,24519,24520],{},"saves the file in the current folder",[24,24522,24523],{},"keeps the path simple and easy to test",[37,24525,24527],{"id":24526},"basic-error-handling","Basic error handling",[14,24529,24530],{},"A beginner script is better if it shows clear errors instead of crashing.",[42,24532,24534],{"className":44,"code":24533,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\noutput_file = \"file.txt\"\n\ntry:\n    response = requests.get(url)\n    response.raise_for_status()\n\n    with open(output_file, \"wb\") as file:\n        file.write(response.content)\n\n    print(\"Download complete\")\n\nexcept requests.exceptions.MissingSchema:\n    print(\"Invalid URL. Did you forget https:\u002F\u002F ?\")\n\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problem. Check your internet or the website address.\")\n\nexcept requests.exceptions.HTTPError as error:\n    print(f\"HTTP error: {error}\")\n\nexcept PermissionError:\n    print(\"Permission denied. Try saving to a different folder.\")\n",[49,24535,24536,24542,24546,24558,24570,24574,24580,24598,24608,24612,24639,24658,24662,24676,24680,24697,24712,24716,24732,24747,24751,24771,24793,24797,24806],{"__ignoreMap":47},[52,24537,24538,24540],{"class":54,"line":55},[52,24539,59],{"class":58},[52,24541,63],{"class":62},[52,24543,24544],{"class":54,"line":66},[52,24545,70],{"emptyLinePlaceholder":69},[52,24547,24548,24550,24552,24554,24556],{"class":54,"line":73},[52,24549,76],{"class":62},[52,24551,80],{"class":79},[52,24553,84],{"class":83},[52,24555,23773],{"class":87},[52,24557,91],{"class":83},[52,24559,24560,24562,24564,24566,24568],{"class":54,"line":94},[52,24561,24394],{"class":62},[52,24563,80],{"class":79},[52,24565,84],{"class":83},[52,24567,23820],{"class":87},[52,24569,91],{"class":83},[52,24571,24572],{"class":54,"line":106},[52,24573,70],{"emptyLinePlaceholder":69},[52,24575,24576,24578],{"class":54,"line":128},[52,24577,3336],{"class":58},[52,24579,1330],{"class":102},[52,24581,24582,24584,24586,24588,24590,24592,24594,24596],{"class":54,"line":148},[52,24583,3343],{"class":62},[52,24585,80],{"class":79},[52,24587,183],{"class":62},[52,24589,186],{"class":102},[52,24591,190],{"class":189},[52,24593,193],{"class":102},[52,24595,196],{"class":189},[52,24597,211],{"class":102},[52,24599,24600,24602,24604,24606],{"class":54,"line":164},[52,24601,3380],{"class":62},[52,24603,186],{"class":102},[52,24605,3385],{"class":189},[52,24607,230],{"class":102},[52,24609,24610],{"class":54,"line":170},[52,24611,70],{"emptyLinePlaceholder":69},[52,24613,24614,24617,24619,24621,24623,24625,24627,24629,24631,24633,24635,24637],{"class":54,"line":175},[52,24615,24616],{"class":58},"    with",[52,24618,14635],{"class":241},[52,24620,193],{"class":102},[52,24622,24450],{"class":189},[52,24624,199],{"class":102},[52,24626,84],{"class":83},[52,24628,23829],{"class":87},[52,24630,115],{"class":83},[52,24632,1521],{"class":102},[52,24634,3567],{"class":58},[52,24636,14676],{"class":14675},[52,24638,1330],{"class":102},[52,24640,24641,24644,24646,24648,24650,24652,24654,24656],{"class":54,"line":214},[52,24642,24643],{"class":14675},"        file",[52,24645,186],{"class":102},[52,24647,23849],{"class":189},[52,24649,193],{"class":102},[52,24651,1756],{"class":189},[52,24653,186],{"class":102},[52,24655,23858],{"class":1320},[52,24657,211],{"class":102},[52,24659,24660],{"class":54,"line":233},[52,24661,70],{"emptyLinePlaceholder":69},[52,24663,24664,24666,24668,24670,24672,24674],{"class":54,"line":238},[52,24665,1599],{"class":241},[52,24667,193],{"class":102},[52,24669,115],{"class":83},[52,24671,23875],{"class":87},[52,24673,115],{"class":83},[52,24675,211],{"class":102},[52,24677,24678],{"class":54,"line":272},[52,24679,70],{"emptyLinePlaceholder":69},[52,24681,24682,24684,24686,24688,24690,24692,24695],{"class":54,"line":1348},[52,24683,3552],{"class":58},[52,24685,183],{"class":62},[52,24687,186],{"class":102},[52,24689,3559],{"class":1320},[52,24691,186],{"class":102},[52,24693,24694],{"class":1320},"MissingSchema",[52,24696,1330],{"class":102},[52,24698,24699,24701,24703,24705,24708,24710],{"class":54,"line":1376},[52,24700,1599],{"class":241},[52,24702,193],{"class":102},[52,24704,115],{"class":83},[52,24706,24707],{"class":87},"Invalid URL. Did you forget https:\u002F\u002F ?",[52,24709,115],{"class":83},[52,24711,211],{"class":102},[52,24713,24714],{"class":54,"line":1381},[52,24715,70],{"emptyLinePlaceholder":69},[52,24717,24718,24720,24722,24724,24726,24728,24730],{"class":54,"line":1406},[52,24719,3552],{"class":58},[52,24721,183],{"class":62},[52,24723,186],{"class":102},[52,24725,3559],{"class":1320},[52,24727,186],{"class":102},[52,24729,4660],{"class":1320},[52,24731,1330],{"class":102},[52,24733,24734,24736,24738,24740,24743,24745],{"class":54,"line":1430},[52,24735,1599],{"class":241},[52,24737,193],{"class":102},[52,24739,115],{"class":83},[52,24741,24742],{"class":87},"Connection problem. Check your internet or the website address.",[52,24744,115],{"class":83},[52,24746,211],{"class":102},[52,24748,24749],{"class":54,"line":1435},[52,24750,70],{"emptyLinePlaceholder":69},[52,24752,24753,24755,24757,24759,24761,24763,24765,24767,24769],{"class":54,"line":1467},[52,24754,3552],{"class":58},[52,24756,183],{"class":62},[52,24758,186],{"class":102},[52,24760,3559],{"class":1320},[52,24762,186],{"class":102},[52,24764,4696],{"class":1320},[52,24766,3567],{"class":58},[52,24768,4701],{"class":62},[52,24770,1330],{"class":102},[52,24772,24773,24775,24777,24779,24782,24784,24787,24789,24791],{"class":54,"line":1495},[52,24774,1599],{"class":241},[52,24776,193],{"class":102},[52,24778,1476],{"class":1475},[52,24780,24781],{"class":87},"\"HTTP error: ",[52,24783,1482],{"class":121},[52,24785,24786],{"class":189},"error",[52,24788,1488],{"class":121},[52,24790,115],{"class":87},[52,24792,211],{"class":102},[52,24794,24795],{"class":54,"line":1531},[52,24796,70],{"emptyLinePlaceholder":69},[52,24798,24799,24801,24804],{"class":54,"line":1564},[52,24800,3552],{"class":58},[52,24802,24803],{"class":4733}," PermissionError",[52,24805,1330],{"class":102},[52,24807,24808,24810,24812,24814,24817,24819],{"class":54,"line":1572},[52,24809,1599],{"class":241},[52,24811,193],{"class":102},[52,24813,115],{"class":83},[52,24815,24816],{"class":87},"Permission denied. Try saving to a different folder.",[52,24818,115],{"class":83},[52,24820,211],{"class":102},[14,24822,24823],{},"This helps handle common problems such as:",[309,24825,24826,24829,24832,24835],{},[24,24827,24828],{},"invalid URLs",[24,24830,24831],{},"connection failures",[24,24833,24834],{},"HTTP errors like 404",[24,24836,24837],{},"file permission problems",[37,24839,3190],{"id":3189},[14,24841,24842],{},"Here are the most common issues when downloading files in Python.",[639,24844,24846,1671],{"id":24845},"requests-is-not-installed",[49,24847,303],{},[14,24849,24850],{},"You may see an error like:",[42,24852,24854],{"className":44,"code":24853,"language":46,"meta":47,"style":47},"ModuleNotFoundError: No module named 'requests'\n",[49,24855,24856],{"__ignoreMap":47},[52,24857,24858,24860,24862,24865,24867,24869],{"class":54,"line":55},[52,24859,1693],{"class":4733},[52,24861,118],{"class":102},[52,24863,24864],{"class":62}," No module named ",[52,24866,4162],{"class":83},[52,24868,303],{"class":87},[52,24870,24871],{"class":83},"'\n",[14,24873,24874],{},"Install it with:",[42,24876,24877],{"className":390,"code":391,"language":392,"meta":47,"style":47},[49,24878,24879],{"__ignoreMap":47},[52,24880,24881,24883,24885],{"class":54,"line":55},[52,24882,400],{"class":399},[52,24884,403],{"class":87},[52,24886,63],{"class":87},[639,24888,24890],{"id":24889},"the-url-opens-a-web-page-instead-of-a-file","The URL opens a web page instead of a file",[14,24892,24893],{},"Sometimes the URL does not point directly to a file. It may point to a normal web page instead.",[14,24895,24896],{},"You can debug this with:",[42,24898,24900],{"className":44,"code":24899,"language":46,"meta":47,"style":47},"print(response.url)\nprint(response.headers.get(\"content-type\"))\n",[49,24901,24902,24916],{"__ignoreMap":47},[52,24903,24904,24906,24908,24910,24912,24914],{"class":54,"line":55},[52,24905,242],{"class":241},[52,24907,193],{"class":102},[52,24909,1756],{"class":189},[52,24911,186],{"class":102},[52,24913,196],{"class":1320},[52,24915,211],{"class":102},[52,24917,24918,24920,24922,24924,24926,24928,24930,24932,24934,24936,24939,24941],{"class":54,"line":66},[52,24919,242],{"class":241},[52,24921,193],{"class":102},[52,24923,1756],{"class":189},[52,24925,186],{"class":102},[52,24927,2633],{"class":1320},[52,24929,186],{"class":102},[52,24931,190],{"class":189},[52,24933,193],{"class":102},[52,24935,115],{"class":83},[52,24937,24938],{"class":87},"content-type",[52,24940,115],{"class":83},[52,24942,8886],{"class":102},[14,24944,4259],{},[42,24946,24948],{"className":44,"code":24947,"language":46,"meta":47,"style":47},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\nresponse = requests.get(url)\n\nprint(response.status_code)\nprint(response.url)\nprint(response.headers.get(\"content-type\"))\n",[49,24949,24950,24956,24960,24972,24990,24994,25008,25022],{"__ignoreMap":47},[52,24951,24952,24954],{"class":54,"line":55},[52,24953,59],{"class":58},[52,24955,63],{"class":62},[52,24957,24958],{"class":54,"line":66},[52,24959,70],{"emptyLinePlaceholder":69},[52,24961,24962,24964,24966,24968,24970],{"class":54,"line":73},[52,24963,76],{"class":62},[52,24965,80],{"class":79},[52,24967,84],{"class":83},[52,24969,23773],{"class":87},[52,24971,91],{"class":83},[52,24973,24974,24976,24978,24980,24982,24984,24986,24988],{"class":54,"line":94},[52,24975,178],{"class":62},[52,24977,80],{"class":79},[52,24979,183],{"class":62},[52,24981,186],{"class":102},[52,24983,190],{"class":189},[52,24985,193],{"class":102},[52,24987,196],{"class":189},[52,24989,211],{"class":102},[52,24991,24992],{"class":54,"line":106},[52,24993,70],{"emptyLinePlaceholder":69},[52,24995,24996,24998,25000,25002,25004,25006],{"class":54,"line":128},[52,24997,242],{"class":241},[52,24999,193],{"class":102},[52,25001,1756],{"class":189},[52,25003,186],{"class":102},[52,25005,1321],{"class":1320},[52,25007,211],{"class":102},[52,25009,25010,25012,25014,25016,25018,25020],{"class":54,"line":148},[52,25011,242],{"class":241},[52,25013,193],{"class":102},[52,25015,1756],{"class":189},[52,25017,186],{"class":102},[52,25019,196],{"class":1320},[52,25021,211],{"class":102},[52,25023,25024,25026,25028,25030,25032,25034,25036,25038,25040,25042,25044,25046],{"class":54,"line":164},[52,25025,242],{"class":241},[52,25027,193],{"class":102},[52,25029,1756],{"class":189},[52,25031,186],{"class":102},[52,25033,2633],{"class":1320},[52,25035,186],{"class":102},[52,25037,190],{"class":189},[52,25039,193],{"class":102},[52,25041,115],{"class":83},[52,25043,24938],{"class":87},[52,25045,115],{"class":83},[52,25047,8886],{"class":102},[14,25049,25050],{},"This helps you see:",[309,25052,25053,25056,25059],{},[24,25054,25055],{},"the final URL after redirects",[24,25057,25058],{},"the status code",[24,25060,25061],{},"the content type returned by the server",[14,25063,25064,25065,25068],{},"If the content type is ",[49,25066,25067],{},"text\u002Fhtml",", you may be downloading a page instead of the file you expected.",[639,25070,25072],{"id":25071},"the-saved-file-has-the-wrong-name-or-extension","The saved file has the wrong name or extension",[14,25074,25075],{},"Make sure your output filename matches the type of file you are downloading.",[14,25077,16291],{},[309,25079,25080,25089,25095],{},[24,25081,25082,25083,857,25086],{},"image data should usually be saved as ",[49,25084,25085],{},".jpg",[49,25087,25088],{},".png",[24,25090,25091,25092],{},"PDF data should usually be saved as ",[49,25093,25094],{},".pdf",[24,25096,25097,25098],{},"ZIP files should usually be saved as ",[49,25099,25100],{},".zip",[639,25102,25104],{"id":25103},"the-script-has-no-permission-to-save-the-file","The script has no permission to save the file",[14,25106,25107],{},"If Python cannot write to the folder, you may get a permission error.",[14,25109,25110],{},"In that case:",[309,25112,25113,25116,25119],{},[24,25114,25115],{},"save to a different folder",[24,25117,25118],{},"use a simpler location",[24,25120,25121],{},"check folder permissions",[14,25123,25124,25125,186],{},"For help, see ",[372,25126,3984,25128],{"href":25127},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix",[49,25129,25130],{},"PermissionError: [Errno 13] Permission denied",[37,25132,25134],{"id":25133},"when-to-use-this-approach","When to use this approach",[14,25136,25137],{},"This approach is a good choice when you want:",[309,25139,25140,25143,25146],{},[24,25141,25142],{},"a simple download script",[24,25144,25145],{},"basic automation",[24,25147,25148],{},"a clear starting point for file download tasks",[14,25150,25151],{},"It is especially useful for beginners because the code is short and easy to test.",[14,25153,25154],{},"For very large files, a streamed download is usually better because it avoids loading the whole file into memory at once.",[37,25156,1942],{"id":1941},[639,25158,25160,25161,25163],{"id":25159},"do-i-need-requests-to-download-a-file-in-python","Do I need ",[49,25162,303],{}," to download a file in Python?",[14,25165,25166,25167,25169],{},"No. Python can also do this with built-in modules. But ",[49,25168,303],{}," is one of the easiest options for beginners because the code is short and readable.",[639,25171,25173,25174,10598,25176,1995],{"id":25172},"why-do-i-use-wb-instead-of-w","Why do I use ",[49,25175,23829],{},[49,25177,16760],{},[14,25179,25180,25182],{},[49,25181,23829],{}," writes binary data. It is safer for files like images, PDFs, and ZIP files.",[639,25184,25186],{"id":25185},"what-if-the-download-url-returns-an-error","What if the download URL returns an error?",[14,25188,3650,25189,25192,25193,10537,25195,25197],{},[49,25190,25191],{},"response.raise_for_status()"," and wrap the request in ",[49,25194,3336],{},[49,25196,3552],{}," so you can show a helpful message.",[639,25199,25201],{"id":25200},"can-this-download-large-files","Can this download large files?",[14,25203,25204],{},"Yes, but for very large files, a streamed download is better than loading everything into memory at once.",[37,25206,2005],{"id":2004},[309,25208,25209,25213,25217,25221,25228,25236],{},[24,25210,25211],{},[372,25212,2012],{"href":24185},[24,25214,25215],{},[372,25216,14985],{"href":24279},[24,25218,25219],{},[372,25220,16513],{"href":24355},[24,25222,25223],{},[372,25224,25225,25226],{"href":24122},"Fix ",[49,25227,24125],{},[24,25229,25230],{},[372,25231,25225,25233],{"href":25232},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix",[49,25234,25235],{},"FileNotFoundError: [Errno 2] No such file or directory",[24,25237,25238],{},[372,25239,25225,25240],{"href":25127},[49,25241,25130],{},[2034,25243,25244],{},"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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":25246},[25247,25248,25249,25250,25253,25255,25256,25257,25264,25265,25273],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":346,"depth":66,"text":347},{"id":23958,"depth":66,"text":23959,"children":25251},[25252],{"id":24094,"depth":73,"text":24095},{"id":24315,"depth":66,"text":25254},"Why wb mode is used",{"id":24358,"depth":66,"text":24359},{"id":24526,"depth":66,"text":24527},{"id":3189,"depth":66,"text":3190,"children":25258},[25259,25261,25262,25263],{"id":24845,"depth":73,"text":25260},"requests is not installed",{"id":24889,"depth":73,"text":24890},{"id":25071,"depth":73,"text":25072},{"id":25103,"depth":73,"text":25104},{"id":25133,"depth":66,"text":25134},{"id":1941,"depth":66,"text":1942,"children":25266},[25267,25269,25271,25272],{"id":25159,"depth":73,"text":25268},"Do I need requests to download a file in Python?",{"id":25172,"depth":73,"text":25270},"Why do I use wb instead of w?",{"id":25185,"depth":73,"text":25186},{"id":25200,"depth":73,"text":25201},{"id":2004,"depth":66,"text":2005},"Master python download files from a url example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-download-files-from-a-url-example",{"title":23716,"description":25274},"examples\u002Fpython-download-files-from-a-url-example","GqQGrzFoQjAnO_ZpPnvYHTq4ql01j_3TrC2DzkiITxg",{"id":25281,"title":25282,"body":25283,"description":26956,"extension":2075,"meta":26957,"navigation":69,"path":26958,"seo":26959,"stem":26960,"__hash__":26961},"content\u002Fexamples\u002Fpython-email-sender-script-example.md","Python Email Sender Script Example",{"type":7,"value":25284,"toc":26931},[25285,25288,25291,25294,25308,25311,25313,25552,25555,25557,25560,25574,25577,25591,25594,25598,25601,25615,25618,25635,25638,25642,25645,25859,25862,25918,25924,25928,25933,25936,25963,25968,25971,25998,26001,26006,26009,26036,26041,26044,26076,26079,26093,26098,26101,26120,26123,26127,26130,26143,26145,26151,26154,26158,26161,26164,26431,26434,26440,26444,26447,26470,26473,26493,26500,26504,26507,26528,26531,26569,26574,26578,26581,26602,26605,26861,26864,26866,26870,26879,26883,26886,26890,26896,26900,26903,26905,26928],[10,25286,25282],{"id":25287},"python-email-sender-script-example",[14,25289,25290],{},"This example shows how to send an email in Python using the standard library.",[14,25292,25293],{},"You will learn:",[309,25295,25296,25299,25302,25305],{},[24,25297,25298],{},"How to create a simple email message",[24,25300,25301],{},"How to connect to an SMTP server",[24,25303,25304],{},"How to log in and send the email",[24,25306,25307],{},"Which values you must replace before the script works",[14,25309,25310],{},"This page focuses on a small working script. It also covers common setup problems that beginners often hit.",[37,25312,40],{"id":39},[42,25314,25316],{"className":44,"code":25315,"language":46,"meta":47,"style":47},"import smtplib\nfrom email.message import EmailMessage\n\nmsg = EmailMessage()\nmsg['Subject'] = 'Test email from Python'\nmsg['From'] = 'your_email@example.com'\nmsg['To'] = 'friend@example.com'\nmsg.set_content('Hello from Python!')\n\nwith smtplib.SMTP_SSL('smtp.example.com', 465) as smtp:\n    smtp.login('your_email@example.com', 'your_password')\n    smtp.send_message(msg)\n\nprint('Email sent')\n",[49,25317,25318,25325,25342,25346,25358,25383,25407,25431,25451,25455,25490,25518,25533,25537],{"__ignoreMap":47},[52,25319,25320,25322],{"class":54,"line":55},[52,25321,59],{"class":58},[52,25323,25324],{"class":62}," smtplib\n",[52,25326,25327,25330,25333,25335,25337,25339],{"class":54,"line":66},[52,25328,25329],{"class":58},"from",[52,25331,25332],{"class":62}," email",[52,25334,186],{"class":102},[52,25336,6121],{"class":62},[52,25338,59],{"class":58},[52,25340,25341],{"class":62}," EmailMessage\n",[52,25343,25344],{"class":54,"line":73},[52,25345,70],{"emptyLinePlaceholder":69},[52,25347,25348,25351,25353,25356],{"class":54,"line":94},[52,25349,25350],{"class":62},"msg ",[52,25352,80],{"class":79},[52,25354,25355],{"class":189}," EmailMessage",[52,25357,230],{"class":102},[52,25359,25360,25363,25365,25367,25370,25372,25374,25376,25378,25381],{"class":54,"line":106},[52,25361,25362],{"class":62},"msg",[52,25364,250],{"class":102},[52,25366,4162],{"class":83},[52,25368,25369],{"class":87},"Subject",[52,25371,4162],{"class":83},[52,25373,11122],{"class":102},[52,25375,7038],{"class":79},[52,25377,4172],{"class":83},[52,25379,25380],{"class":87},"Test email from Python",[52,25382,24871],{"class":83},[52,25384,25385,25387,25389,25391,25394,25396,25398,25400,25402,25405],{"class":54,"line":128},[52,25386,25362],{"class":62},[52,25388,250],{"class":102},[52,25390,4162],{"class":83},[52,25392,25393],{"class":87},"From",[52,25395,4162],{"class":83},[52,25397,11122],{"class":102},[52,25399,7038],{"class":79},[52,25401,4172],{"class":83},[52,25403,25404],{"class":87},"your_email@example.com",[52,25406,24871],{"class":83},[52,25408,25409,25411,25413,25415,25418,25420,25422,25424,25426,25429],{"class":54,"line":148},[52,25410,25362],{"class":62},[52,25412,250],{"class":102},[52,25414,4162],{"class":83},[52,25416,25417],{"class":87},"To",[52,25419,4162],{"class":83},[52,25421,11122],{"class":102},[52,25423,7038],{"class":79},[52,25425,4172],{"class":83},[52,25427,25428],{"class":87},"friend@example.com",[52,25430,24871],{"class":83},[52,25432,25433,25435,25437,25440,25442,25444,25447,25449],{"class":54,"line":164},[52,25434,25362],{"class":62},[52,25436,186],{"class":102},[52,25438,25439],{"class":189},"set_content",[52,25441,193],{"class":102},[52,25443,4162],{"class":83},[52,25445,25446],{"class":87},"Hello from Python!",[52,25448,4162],{"class":83},[52,25450,211],{"class":102},[52,25452,25453],{"class":54,"line":170},[52,25454,70],{"emptyLinePlaceholder":69},[52,25456,25457,25459,25462,25464,25467,25469,25471,25474,25476,25478,25481,25483,25485,25488],{"class":54,"line":175},[52,25458,14632],{"class":58},[52,25460,25461],{"class":62}," smtplib",[52,25463,186],{"class":102},[52,25465,25466],{"class":189},"SMTP_SSL",[52,25468,193],{"class":102},[52,25470,4162],{"class":83},[52,25472,25473],{"class":87},"smtp.example.com",[52,25475,4162],{"class":83},[52,25477,199],{"class":102},[52,25479,25480],{"class":121}," 465",[52,25482,1521],{"class":102},[52,25484,3567],{"class":58},[52,25486,25487],{"class":62}," smtp",[52,25489,1330],{"class":102},[52,25491,25492,25495,25497,25499,25501,25503,25505,25507,25509,25511,25514,25516],{"class":54,"line":214},[52,25493,25494],{"class":62},"    smtp",[52,25496,186],{"class":102},[52,25498,4165],{"class":189},[52,25500,193],{"class":102},[52,25502,4162],{"class":83},[52,25504,25404],{"class":87},[52,25506,4162],{"class":83},[52,25508,199],{"class":102},[52,25510,4172],{"class":83},[52,25512,25513],{"class":87},"your_password",[52,25515,4162],{"class":83},[52,25517,211],{"class":102},[52,25519,25520,25522,25524,25527,25529,25531],{"class":54,"line":233},[52,25521,25494],{"class":62},[52,25523,186],{"class":102},[52,25525,25526],{"class":189},"send_message",[52,25528,193],{"class":102},[52,25530,25362],{"class":189},[52,25532,211],{"class":102},[52,25534,25535],{"class":54,"line":238},[52,25536,70],{"emptyLinePlaceholder":69},[52,25538,25539,25541,25543,25545,25548,25550],{"class":54,"line":272},[52,25540,242],{"class":241},[52,25542,193],{"class":102},[52,25544,4162],{"class":83},[52,25546,25547],{"class":87},"Email sent",[52,25549,4162],{"class":83},[52,25551,211],{"class":102},[14,25553,25554],{},"Replace the SMTP server, port, email address, and password with real values from your email provider. Some providers require an app password instead of your normal password.",[37,25556,2268],{"id":2267},[14,25558,25559],{},"This script demonstrates the basic steps for sending an email:",[309,25561,25562,25565,25568,25571],{},[24,25563,25564],{},"Build an email message with a subject, sender, recipient, and body",[24,25566,25567],{},"Connect to an SMTP server",[24,25569,25570],{},"Log in with your email account",[24,25572,25573],{},"Send the message",[14,25575,25576],{},"It uses:",[309,25578,25579,25585],{},[24,25580,25581,25584],{},[49,25582,25583],{},"smtplib"," to talk to the email server",[24,25586,25587,25590],{},[49,25588,25589],{},"EmailMessage"," to build the email",[14,25592,25593],{},"You do not need to install extra packages for this basic example. Both modules are included with Python.",[37,25595,25597],{"id":25596},"before-you-run-the-script","Before you run the script",[14,25599,25600],{},"Before this can work, you need a few real values from your email provider:",[309,25602,25603,25606,25609,25612],{},[24,25604,25605],{},"An email account that allows SMTP sending",[24,25607,25608],{},"The correct SMTP server name",[24,25610,25611],{},"The correct SMTP port",[24,25613,25614],{},"A working password or app password",[14,25616,25617],{},"A few important notes:",[309,25619,25620,25626,25632],{},[24,25621,25622,25623,25625],{},"Some providers do ",[5503,25624,20858],{}," let scripts use your normal password",[24,25627,25628,25629],{},"Some providers require an ",[5503,25630,25631],{},"app password",[24,25633,25634],{},"Some providers block sign-in attempts until you enable a security setting",[14,25636,25637],{},"An app password is a special password made for scripts or apps. It is often safer than using your normal account password directly.",[37,25639,25641],{"id":25640},"how-the-script-works","How the script works",[14,25643,25644],{},"Here is the same script again:",[42,25646,25647],{"className":44,"code":25315,"language":46,"meta":47,"style":47},[49,25648,25649,25655,25669,25673,25683,25705,25727,25749,25767,25771,25801,25827,25841,25845],{"__ignoreMap":47},[52,25650,25651,25653],{"class":54,"line":55},[52,25652,59],{"class":58},[52,25654,25324],{"class":62},[52,25656,25657,25659,25661,25663,25665,25667],{"class":54,"line":66},[52,25658,25329],{"class":58},[52,25660,25332],{"class":62},[52,25662,186],{"class":102},[52,25664,6121],{"class":62},[52,25666,59],{"class":58},[52,25668,25341],{"class":62},[52,25670,25671],{"class":54,"line":73},[52,25672,70],{"emptyLinePlaceholder":69},[52,25674,25675,25677,25679,25681],{"class":54,"line":94},[52,25676,25350],{"class":62},[52,25678,80],{"class":79},[52,25680,25355],{"class":189},[52,25682,230],{"class":102},[52,25684,25685,25687,25689,25691,25693,25695,25697,25699,25701,25703],{"class":54,"line":106},[52,25686,25362],{"class":62},[52,25688,250],{"class":102},[52,25690,4162],{"class":83},[52,25692,25369],{"class":87},[52,25694,4162],{"class":83},[52,25696,11122],{"class":102},[52,25698,7038],{"class":79},[52,25700,4172],{"class":83},[52,25702,25380],{"class":87},[52,25704,24871],{"class":83},[52,25706,25707,25709,25711,25713,25715,25717,25719,25721,25723,25725],{"class":54,"line":128},[52,25708,25362],{"class":62},[52,25710,250],{"class":102},[52,25712,4162],{"class":83},[52,25714,25393],{"class":87},[52,25716,4162],{"class":83},[52,25718,11122],{"class":102},[52,25720,7038],{"class":79},[52,25722,4172],{"class":83},[52,25724,25404],{"class":87},[52,25726,24871],{"class":83},[52,25728,25729,25731,25733,25735,25737,25739,25741,25743,25745,25747],{"class":54,"line":148},[52,25730,25362],{"class":62},[52,25732,250],{"class":102},[52,25734,4162],{"class":83},[52,25736,25417],{"class":87},[52,25738,4162],{"class":83},[52,25740,11122],{"class":102},[52,25742,7038],{"class":79},[52,25744,4172],{"class":83},[52,25746,25428],{"class":87},[52,25748,24871],{"class":83},[52,25750,25751,25753,25755,25757,25759,25761,25763,25765],{"class":54,"line":164},[52,25752,25362],{"class":62},[52,25754,186],{"class":102},[52,25756,25439],{"class":189},[52,25758,193],{"class":102},[52,25760,4162],{"class":83},[52,25762,25446],{"class":87},[52,25764,4162],{"class":83},[52,25766,211],{"class":102},[52,25768,25769],{"class":54,"line":170},[52,25770,70],{"emptyLinePlaceholder":69},[52,25772,25773,25775,25777,25779,25781,25783,25785,25787,25789,25791,25793,25795,25797,25799],{"class":54,"line":175},[52,25774,14632],{"class":58},[52,25776,25461],{"class":62},[52,25778,186],{"class":102},[52,25780,25466],{"class":189},[52,25782,193],{"class":102},[52,25784,4162],{"class":83},[52,25786,25473],{"class":87},[52,25788,4162],{"class":83},[52,25790,199],{"class":102},[52,25792,25480],{"class":121},[52,25794,1521],{"class":102},[52,25796,3567],{"class":58},[52,25798,25487],{"class":62},[52,25800,1330],{"class":102},[52,25802,25803,25805,25807,25809,25811,25813,25815,25817,25819,25821,25823,25825],{"class":54,"line":214},[52,25804,25494],{"class":62},[52,25806,186],{"class":102},[52,25808,4165],{"class":189},[52,25810,193],{"class":102},[52,25812,4162],{"class":83},[52,25814,25404],{"class":87},[52,25816,4162],{"class":83},[52,25818,199],{"class":102},[52,25820,4172],{"class":83},[52,25822,25513],{"class":87},[52,25824,4162],{"class":83},[52,25826,211],{"class":102},[52,25828,25829,25831,25833,25835,25837,25839],{"class":54,"line":233},[52,25830,25494],{"class":62},[52,25832,186],{"class":102},[52,25834,25526],{"class":189},[52,25836,193],{"class":102},[52,25838,25362],{"class":189},[52,25840,211],{"class":102},[52,25842,25843],{"class":54,"line":238},[52,25844,70],{"emptyLinePlaceholder":69},[52,25846,25847,25849,25851,25853,25855,25857],{"class":54,"line":272},[52,25848,242],{"class":241},[52,25850,193],{"class":102},[52,25852,4162],{"class":83},[52,25854,25547],{"class":87},[52,25856,4162],{"class":83},[52,25858,211],{"class":102},[14,25860,25861],{},"Step by step:",[309,25863,25864,25870,25876,25882,25894,25900,25906,25912],{},[24,25865,25866,25869],{},[49,25867,25868],{},"import smtplib"," imports the module used to connect to the mail server",[24,25871,25872,25875],{},[49,25873,25874],{},"from email.message import EmailMessage"," imports the class used to create the email",[24,25877,25878,25881],{},[49,25879,25880],{},"msg = EmailMessage()"," creates a new email message object",[24,25883,25884,2587,25887,11556,25890,25893],{},[49,25885,25886],{},"msg['Subject']",[49,25888,25889],{},"msg['From']",[49,25891,25892],{},"msg['To']"," set the email headers",[24,25895,25896,25899],{},[49,25897,25898],{},"msg.set_content(...)"," sets the plain text body",[24,25901,25902,25905],{},[49,25903,25904],{},"smtplib.SMTP_SSL(...)"," opens a secure connection to the SMTP server",[24,25907,25908,25911],{},[49,25909,25910],{},"smtp.login(...)"," signs in to your account",[24,25913,25914,25917],{},[49,25915,25916],{},"smtp.send_message(msg)"," sends the email",[14,25919,25920,25921,25923],{},"If you are new to Python input, see ",[372,25922,13726],{"href":10606}," for a simple way to ask the user for values.",[37,25925,25927],{"id":25926},"important-lines-to-explain","Important lines to explain",[639,25929,25931],{"id":25930},"msgsubject",[49,25932,25886],{},[14,25934,25935],{},"This sets the subject line that the recipient sees.",[42,25937,25939],{"className":44,"code":25938,"language":46,"meta":47,"style":47},"msg['Subject'] = 'Test email from Python'\n",[49,25940,25941],{"__ignoreMap":47},[52,25942,25943,25945,25947,25949,25951,25953,25955,25957,25959,25961],{"class":54,"line":55},[52,25944,25362],{"class":62},[52,25946,250],{"class":102},[52,25948,4162],{"class":83},[52,25950,25369],{"class":87},[52,25952,4162],{"class":83},[52,25954,11122],{"class":102},[52,25956,7038],{"class":79},[52,25958,4172],{"class":83},[52,25960,25380],{"class":87},[52,25962,24871],{"class":83},[639,25964,25966],{"id":25965},"msgfrom",[49,25967,25889],{},[14,25969,25970],{},"This sets the sender address.",[42,25972,25974],{"className":44,"code":25973,"language":46,"meta":47,"style":47},"msg['From'] = 'your_email@example.com'\n",[49,25975,25976],{"__ignoreMap":47},[52,25977,25978,25980,25982,25984,25986,25988,25990,25992,25994,25996],{"class":54,"line":55},[52,25979,25362],{"class":62},[52,25981,250],{"class":102},[52,25983,4162],{"class":83},[52,25985,25393],{"class":87},[52,25987,4162],{"class":83},[52,25989,11122],{"class":102},[52,25991,7038],{"class":79},[52,25993,4172],{"class":83},[52,25995,25404],{"class":87},[52,25997,24871],{"class":83},[14,25999,26000],{},"In many cases, this should match the account you log in with. If they do not match, some providers may reject the message.",[639,26002,26004],{"id":26003},"msgto",[49,26005,25892],{},[14,26007,26008],{},"This is the recipient email address.",[42,26010,26012],{"className":44,"code":26011,"language":46,"meta":47,"style":47},"msg['To'] = 'friend@example.com'\n",[49,26013,26014],{"__ignoreMap":47},[52,26015,26016,26018,26020,26022,26024,26026,26028,26030,26032,26034],{"class":54,"line":55},[52,26017,25362],{"class":62},[52,26019,250],{"class":102},[52,26021,4162],{"class":83},[52,26023,25417],{"class":87},[52,26025,4162],{"class":83},[52,26027,11122],{"class":102},[52,26029,7038],{"class":79},[52,26031,4172],{"class":83},[52,26033,25428],{"class":87},[52,26035,24871],{"class":83},[639,26037,26039],{"id":26038},"smtplogin",[49,26040,25910],{},[14,26042,26043],{},"This authenticates with the email server.",[42,26045,26047],{"className":44,"code":26046,"language":46,"meta":47,"style":47},"smtp.login('your_email@example.com', 'your_password')\n",[49,26048,26049],{"__ignoreMap":47},[52,26050,26051,26054,26056,26058,26060,26062,26064,26066,26068,26070,26072,26074],{"class":54,"line":55},[52,26052,26053],{"class":62},"smtp",[52,26055,186],{"class":102},[52,26057,4165],{"class":189},[52,26059,193],{"class":102},[52,26061,4162],{"class":83},[52,26063,25404],{"class":87},[52,26065,4162],{"class":83},[52,26067,199],{"class":102},[52,26069,4172],{"class":83},[52,26071,25513],{"class":87},[52,26073,4162],{"class":83},[52,26075,211],{"class":102},[14,26077,26078],{},"If this line fails, the problem is often:",[309,26080,26081,26084,26087,26090],{},[24,26082,26083],{},"Wrong email address",[24,26085,26086],{},"Wrong password",[24,26088,26089],{},"App password required",[24,26091,26092],{},"Provider security settings blocking the login",[639,26094,26096],{"id":26095},"smtpsend_messagemsg",[49,26097,25916],{},[14,26099,26100],{},"This actually sends the email.",[42,26102,26104],{"className":44,"code":26103,"language":46,"meta":47,"style":47},"smtp.send_message(msg)\n",[49,26105,26106],{"__ignoreMap":47},[52,26107,26108,26110,26112,26114,26116,26118],{"class":54,"line":55},[52,26109,26053],{"class":62},[52,26111,186],{"class":102},[52,26113,25526],{"class":189},[52,26115,193],{"class":102},[52,26117,25362],{"class":189},[52,26119,211],{"class":102},[14,26121,26122],{},"If the connection and login are correct, this sends the message to the server.",[37,26124,26126],{"id":26125},"expected-result","Expected result",[14,26128,26129],{},"If everything works:",[309,26131,26132,26137,26140],{},[24,26133,26134,26135],{},"The script prints ",[49,26136,25547],{},[24,26138,26139],{},"The recipient should receive a plain text email",[24,26141,26142],{},"The message may still be delayed, filtered, or sent to spam",[14,26144,11845],{},[42,26146,26149],{"className":26147,"code":26148,"language":955,"meta":47},[953],"Email sent\n",[49,26150,26148],{"__ignoreMap":47},[14,26152,26153],{},"Even when the script finishes without an error, delivery is not always guaranteed. The provider might still reject or filter the message later.",[37,26155,26157],{"id":26156},"safer-way-to-handle-passwords","Safer way to handle passwords",[14,26159,26160],{},"Do not hard-code real passwords in a file you plan to share.",[14,26162,26163],{},"A simple beginner-friendly option is to ask for the password while the script runs:",[42,26165,26167],{"className":44,"code":26166,"language":46,"meta":47,"style":47},"import smtplib\nfrom email.message import EmailMessage\n\nsender = input(\"Enter your email address: \")\npassword = input(\"Enter your email password or app password: \")\nrecipient = input(\"Enter recipient email address: \")\n\nmsg = EmailMessage()\nmsg[\"Subject\"] = \"Test email from Python\"\nmsg[\"From\"] = sender\nmsg[\"To\"] = recipient\nmsg.set_content(\"Hello from Python!\")\n\nwith smtplib.SMTP_SSL(\"smtp.example.com\", 465) as smtp:\n    smtp.login(sender, password)\n    smtp.send_message(msg)\n\nprint(\"Email sent\")\n",[49,26168,26169,26175,26189,26193,26213,26233,26253,26257,26267,26289,26308,26327,26345,26349,26379,26399,26413,26417],{"__ignoreMap":47},[52,26170,26171,26173],{"class":54,"line":55},[52,26172,59],{"class":58},[52,26174,25324],{"class":62},[52,26176,26177,26179,26181,26183,26185,26187],{"class":54,"line":66},[52,26178,25329],{"class":58},[52,26180,25332],{"class":62},[52,26182,186],{"class":102},[52,26184,6121],{"class":62},[52,26186,59],{"class":58},[52,26188,25341],{"class":62},[52,26190,26191],{"class":54,"line":73},[52,26192,70],{"emptyLinePlaceholder":69},[52,26194,26195,26198,26200,26202,26204,26206,26209,26211],{"class":54,"line":94},[52,26196,26197],{"class":62},"sender ",[52,26199,80],{"class":79},[52,26201,11037],{"class":241},[52,26203,193],{"class":102},[52,26205,115],{"class":83},[52,26207,26208],{"class":87},"Enter your email address: ",[52,26210,115],{"class":83},[52,26212,211],{"class":102},[52,26214,26215,26218,26220,26222,26224,26226,26229,26231],{"class":54,"line":106},[52,26216,26217],{"class":62},"password ",[52,26219,80],{"class":79},[52,26221,11037],{"class":241},[52,26223,193],{"class":102},[52,26225,115],{"class":83},[52,26227,26228],{"class":87},"Enter your email password or app password: ",[52,26230,115],{"class":83},[52,26232,211],{"class":102},[52,26234,26235,26238,26240,26242,26244,26246,26249,26251],{"class":54,"line":128},[52,26236,26237],{"class":62},"recipient ",[52,26239,80],{"class":79},[52,26241,11037],{"class":241},[52,26243,193],{"class":102},[52,26245,115],{"class":83},[52,26247,26248],{"class":87},"Enter recipient email address: ",[52,26250,115],{"class":83},[52,26252,211],{"class":102},[52,26254,26255],{"class":54,"line":148},[52,26256,70],{"emptyLinePlaceholder":69},[52,26258,26259,26261,26263,26265],{"class":54,"line":164},[52,26260,25350],{"class":62},[52,26262,80],{"class":79},[52,26264,25355],{"class":189},[52,26266,230],{"class":102},[52,26268,26269,26271,26273,26275,26277,26279,26281,26283,26285,26287],{"class":54,"line":170},[52,26270,25362],{"class":62},[52,26272,250],{"class":102},[52,26274,115],{"class":83},[52,26276,25369],{"class":87},[52,26278,115],{"class":83},[52,26280,11122],{"class":102},[52,26282,7038],{"class":79},[52,26284,84],{"class":83},[52,26286,25380],{"class":87},[52,26288,91],{"class":83},[52,26290,26291,26293,26295,26297,26299,26301,26303,26305],{"class":54,"line":175},[52,26292,25362],{"class":62},[52,26294,250],{"class":102},[52,26296,115],{"class":83},[52,26298,25393],{"class":87},[52,26300,115],{"class":83},[52,26302,11122],{"class":102},[52,26304,7038],{"class":79},[52,26306,26307],{"class":62}," sender\n",[52,26309,26310,26312,26314,26316,26318,26320,26322,26324],{"class":54,"line":214},[52,26311,25362],{"class":62},[52,26313,250],{"class":102},[52,26315,115],{"class":83},[52,26317,25417],{"class":87},[52,26319,115],{"class":83},[52,26321,11122],{"class":102},[52,26323,7038],{"class":79},[52,26325,26326],{"class":62}," recipient\n",[52,26328,26329,26331,26333,26335,26337,26339,26341,26343],{"class":54,"line":233},[52,26330,25362],{"class":62},[52,26332,186],{"class":102},[52,26334,25439],{"class":189},[52,26336,193],{"class":102},[52,26338,115],{"class":83},[52,26340,25446],{"class":87},[52,26342,115],{"class":83},[52,26344,211],{"class":102},[52,26346,26347],{"class":54,"line":238},[52,26348,70],{"emptyLinePlaceholder":69},[52,26350,26351,26353,26355,26357,26359,26361,26363,26365,26367,26369,26371,26373,26375,26377],{"class":54,"line":272},[52,26352,14632],{"class":58},[52,26354,25461],{"class":62},[52,26356,186],{"class":102},[52,26358,25466],{"class":189},[52,26360,193],{"class":102},[52,26362,115],{"class":83},[52,26364,25473],{"class":87},[52,26366,115],{"class":83},[52,26368,199],{"class":102},[52,26370,25480],{"class":121},[52,26372,1521],{"class":102},[52,26374,3567],{"class":58},[52,26376,25487],{"class":62},[52,26378,1330],{"class":102},[52,26380,26381,26383,26385,26387,26389,26392,26394,26397],{"class":54,"line":1348},[52,26382,25494],{"class":62},[52,26384,186],{"class":102},[52,26386,4165],{"class":189},[52,26388,193],{"class":102},[52,26390,26391],{"class":189},"sender",[52,26393,199],{"class":102},[52,26395,26396],{"class":189}," password",[52,26398,211],{"class":102},[52,26400,26401,26403,26405,26407,26409,26411],{"class":54,"line":1376},[52,26402,25494],{"class":62},[52,26404,186],{"class":102},[52,26406,25526],{"class":189},[52,26408,193],{"class":102},[52,26410,25362],{"class":189},[52,26412,211],{"class":102},[52,26414,26415],{"class":54,"line":1381},[52,26416,70],{"emptyLinePlaceholder":69},[52,26418,26419,26421,26423,26425,26427,26429],{"class":54,"line":1406},[52,26420,242],{"class":241},[52,26422,193],{"class":102},[52,26424,115],{"class":83},[52,26426,25547],{"class":87},[52,26428,115],{"class":83},[52,26430,211],{"class":102},[14,26432,26433],{},"This is better than saving the password in the script, but it still shows the password on screen while typing.",[14,26435,26436,26437,26439],{},"A more advanced next step is to use environment variables. If you are still learning, start with ",[49,26438,10597],{}," and move to safer storage later.",[37,26441,26443],{"id":26442},"common-problems-with-email-scripts","Common problems with email scripts",[14,26445,26446],{},"These are the most common reasons the script does not work:",[309,26448,26449,26452,26455,26458,26461,26464,26467],{},[24,26450,26451],{},"Wrong SMTP host",[24,26453,26454],{},"Wrong SMTP port",[24,26456,26457],{},"Incorrect username or password",[24,26459,26460],{},"Using a normal password when an app password is required",[24,26462,26463],{},"Provider blocks sign-in from scripts",[24,26465,26466],{},"No internet connection",[24,26468,26469],{},"Firewall or network blocks the SMTP port",[14,26471,26472],{},"Other common causes include:",[309,26474,26475,26478,26481,26484,26487],{},[24,26476,26477],{},"Using the wrong SMTP server address",[24,26479,26480],{},"Using the wrong SMTP port",[24,26482,26483],{},"Entering the wrong email password",[24,26485,26486],{},"Trying to send from an account that blocks SMTP access",[24,26488,26489,26490,26492],{},"Forgetting to match the ",[49,26491,25393],{}," address with the login account",[14,26494,26495,26496,759,26498,186],{},"If you get an error, read the full message carefully. A good next step is learning ",[372,26497,10032],{"href":10031},[372,26499,10028],{"href":10027},[37,26501,26503],{"id":26502},"simple-debugging-checks","Simple debugging checks",[14,26505,26506],{},"Try these checks before changing a lot of code:",[309,26508,26509,26516,26519,26522,26525],{},[24,26510,26511,26512,26515],{},"Make sure the email address in ",[49,26513,26514],{},"login()"," is correct",[24,26517,26518],{},"Check the SMTP server and port in your provider's documentation",[24,26520,26521],{},"Print the host and port before connecting",[24,26523,26524],{},"Try sending to your own email address first",[24,26526,26527],{},"Read the full error message instead of guessing",[14,26529,26530],{},"Useful test commands:",[42,26532,26534],{"className":390,"code":26533,"language":392,"meta":47,"style":47},"python email_sender.py\npython -c \"import smtplib; print('smtplib imported successfully')\"\npython -c \"from email.message import EmailMessage; print('EmailMessage imported successfully')\"\n",[49,26535,26536,26543,26556],{"__ignoreMap":47},[52,26537,26538,26540],{"class":54,"line":55},[52,26539,46],{"class":399},[52,26541,26542],{"class":87}," email_sender.py\n",[52,26544,26545,26547,26549,26551,26554],{"class":54,"line":66},[52,26546,46],{"class":399},[52,26548,443],{"class":420},[52,26550,84],{"class":83},[52,26552,26553],{"class":87},"import smtplib; print('smtplib imported successfully')",[52,26555,91],{"class":83},[52,26557,26558,26560,26562,26564,26567],{"class":54,"line":73},[52,26559,46],{"class":399},[52,26561,443],{"class":420},[52,26563,84],{"class":83},[52,26565,26566],{"class":87},"from email.message import EmailMessage; print('EmailMessage imported successfully')",[52,26568,91],{"class":83},[14,26570,26571,26572,186],{},"If imports fail, the problem may be with your Python installation or environment. If you see missing module errors in other scripts, read ",[372,26573,1731],{"href":1730},[37,26575,26577],{"id":26576},"good-next-steps-for-readers","Good next steps for readers",[14,26579,26580],{},"Once this basic version works, you can improve it by:",[309,26582,26583,26586,26589,26592,26595],{},[24,26584,26585],{},"Adding HTML email content",[24,26587,26588],{},"Sending to multiple recipients",[24,26590,26591],{},"Attaching a file",[24,26593,26594],{},"Moving login details out of the script",[24,26596,26597,26598,759,26600],{},"Wrapping the send code in ",[49,26599,3336],{},[49,26601,3552],{},[14,26603,26604],{},"Here is a simple version with basic error handling:",[42,26606,26608],{"className":44,"code":26607,"language":46,"meta":47,"style":47},"import smtplib\nfrom email.message import EmailMessage\n\nmsg = EmailMessage()\nmsg[\"Subject\"] = \"Test email from Python\"\nmsg[\"From\"] = \"your_email@example.com\"\nmsg[\"To\"] = \"friend@example.com\"\nmsg.set_content(\"Hello from Python!\")\n\ntry:\n    with smtplib.SMTP_SSL(\"smtp.example.com\", 465) as smtp:\n        smtp.login(\"your_email@example.com\", \"your_password\")\n        smtp.send_message(msg)\n    print(\"Email sent\")\nexcept Exception as error:\n    print(\"Could not send email:\")\n    print(error)\n",[49,26609,26610,26616,26630,26634,26644,26666,26688,26710,26728,26732,26738,26768,26795,26809,26823,26836,26851],{"__ignoreMap":47},[52,26611,26612,26614],{"class":54,"line":55},[52,26613,59],{"class":58},[52,26615,25324],{"class":62},[52,26617,26618,26620,26622,26624,26626,26628],{"class":54,"line":66},[52,26619,25329],{"class":58},[52,26621,25332],{"class":62},[52,26623,186],{"class":102},[52,26625,6121],{"class":62},[52,26627,59],{"class":58},[52,26629,25341],{"class":62},[52,26631,26632],{"class":54,"line":73},[52,26633,70],{"emptyLinePlaceholder":69},[52,26635,26636,26638,26640,26642],{"class":54,"line":94},[52,26637,25350],{"class":62},[52,26639,80],{"class":79},[52,26641,25355],{"class":189},[52,26643,230],{"class":102},[52,26645,26646,26648,26650,26652,26654,26656,26658,26660,26662,26664],{"class":54,"line":106},[52,26647,25362],{"class":62},[52,26649,250],{"class":102},[52,26651,115],{"class":83},[52,26653,25369],{"class":87},[52,26655,115],{"class":83},[52,26657,11122],{"class":102},[52,26659,7038],{"class":79},[52,26661,84],{"class":83},[52,26663,25380],{"class":87},[52,26665,91],{"class":83},[52,26667,26668,26670,26672,26674,26676,26678,26680,26682,26684,26686],{"class":54,"line":128},[52,26669,25362],{"class":62},[52,26671,250],{"class":102},[52,26673,115],{"class":83},[52,26675,25393],{"class":87},[52,26677,115],{"class":83},[52,26679,11122],{"class":102},[52,26681,7038],{"class":79},[52,26683,84],{"class":83},[52,26685,25404],{"class":87},[52,26687,91],{"class":83},[52,26689,26690,26692,26694,26696,26698,26700,26702,26704,26706,26708],{"class":54,"line":148},[52,26691,25362],{"class":62},[52,26693,250],{"class":102},[52,26695,115],{"class":83},[52,26697,25417],{"class":87},[52,26699,115],{"class":83},[52,26701,11122],{"class":102},[52,26703,7038],{"class":79},[52,26705,84],{"class":83},[52,26707,25428],{"class":87},[52,26709,91],{"class":83},[52,26711,26712,26714,26716,26718,26720,26722,26724,26726],{"class":54,"line":164},[52,26713,25362],{"class":62},[52,26715,186],{"class":102},[52,26717,25439],{"class":189},[52,26719,193],{"class":102},[52,26721,115],{"class":83},[52,26723,25446],{"class":87},[52,26725,115],{"class":83},[52,26727,211],{"class":102},[52,26729,26730],{"class":54,"line":170},[52,26731,70],{"emptyLinePlaceholder":69},[52,26733,26734,26736],{"class":54,"line":175},[52,26735,3336],{"class":58},[52,26737,1330],{"class":102},[52,26739,26740,26742,26744,26746,26748,26750,26752,26754,26756,26758,26760,26762,26764,26766],{"class":54,"line":214},[52,26741,24616],{"class":58},[52,26743,25461],{"class":62},[52,26745,186],{"class":102},[52,26747,25466],{"class":189},[52,26749,193],{"class":102},[52,26751,115],{"class":83},[52,26753,25473],{"class":87},[52,26755,115],{"class":83},[52,26757,199],{"class":102},[52,26759,25480],{"class":121},[52,26761,1521],{"class":102},[52,26763,3567],{"class":58},[52,26765,25487],{"class":62},[52,26767,1330],{"class":102},[52,26769,26770,26773,26775,26777,26779,26781,26783,26785,26787,26789,26791,26793],{"class":54,"line":233},[52,26771,26772],{"class":62},"        smtp",[52,26774,186],{"class":102},[52,26776,4165],{"class":189},[52,26778,193],{"class":102},[52,26780,115],{"class":83},[52,26782,25404],{"class":87},[52,26784,115],{"class":83},[52,26786,199],{"class":102},[52,26788,84],{"class":83},[52,26790,25513],{"class":87},[52,26792,115],{"class":83},[52,26794,211],{"class":102},[52,26796,26797,26799,26801,26803,26805,26807],{"class":54,"line":238},[52,26798,26772],{"class":62},[52,26800,186],{"class":102},[52,26802,25526],{"class":189},[52,26804,193],{"class":102},[52,26806,25362],{"class":189},[52,26808,211],{"class":102},[52,26810,26811,26813,26815,26817,26819,26821],{"class":54,"line":272},[52,26812,1599],{"class":241},[52,26814,193],{"class":102},[52,26816,115],{"class":83},[52,26818,25547],{"class":87},[52,26820,115],{"class":83},[52,26822,211],{"class":102},[52,26824,26825,26827,26830,26832,26834],{"class":54,"line":1348},[52,26826,3552],{"class":58},[52,26828,26829],{"class":4733}," Exception",[52,26831,3567],{"class":58},[52,26833,4701],{"class":62},[52,26835,1330],{"class":102},[52,26837,26838,26840,26842,26844,26847,26849],{"class":54,"line":1376},[52,26839,1599],{"class":241},[52,26841,193],{"class":102},[52,26843,115],{"class":83},[52,26845,26846],{"class":87},"Could not send email:",[52,26848,115],{"class":83},[52,26850,211],{"class":102},[52,26852,26853,26855,26857,26859],{"class":54,"line":1381},[52,26854,1599],{"class":241},[52,26856,193],{"class":102},[52,26858,24786],{"class":189},[52,26860,211],{"class":102},[14,26862,26863],{},"This does not fix every problem automatically, but it helps you see the real error instead of the program stopping with no explanation.",[37,26865,1942],{"id":1941},[639,26867,26869],{"id":26868},"do-i-need-to-install-a-package-to-send-email-in-python","Do I need to install a package to send email in Python?",[14,26871,26872,26873,759,26875,26878],{},"No. For a basic SMTP example, ",[49,26874,25583],{},[49,26876,26877],{},"email.message"," are part of Python's standard library.",[639,26880,26882],{"id":26881},"why-does-my-email-script-say-login-failed","Why does my email script say login failed?",[14,26884,26885],{},"Usually the username, password, app password, or provider security settings are wrong.",[639,26887,26889],{"id":26888},"can-i-send-html-emails-with-python","Can I send HTML emails with Python?",[14,26891,26892,26893,26895],{},"Yes. You can use ",[49,26894,25589],{}," and add HTML content. For a first script, plain text is easier to test.",[639,26897,26899],{"id":26898},"why-is-my-script-correct-but-the-email-still-does-not-arrive","Why is my script correct but the email still does not arrive?",[14,26901,26902],{},"The provider may reject, delay, or filter the message. Check the spam folder and your provider settings.",[37,26904,2005],{"id":2004},[309,26906,26907,26912,26916,26920,26924],{},[24,26908,26909],{},[372,26910,26911],{"href":10027},"How to handle exceptions in Python",[24,26913,26914],{},[372,26915,10032],{"href":10031},[24,26917,26918],{},[372,26919,13726],{"href":10606},[24,26921,26922],{},[372,26923,6785],{"href":6784},[24,26925,26926],{},[372,26927,2012],{"href":374},[2034,26929,26930],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":26932},[26933,26934,26935,26936,26937,26944,26945,26946,26947,26948,26949,26955],{"id":39,"depth":66,"text":40},{"id":2267,"depth":66,"text":2268},{"id":25596,"depth":66,"text":25597},{"id":25640,"depth":66,"text":25641},{"id":25926,"depth":66,"text":25927,"children":26938},[26939,26940,26941,26942,26943],{"id":25930,"depth":73,"text":25886},{"id":25965,"depth":73,"text":25889},{"id":26003,"depth":73,"text":25892},{"id":26038,"depth":73,"text":25910},{"id":26095,"depth":73,"text":25916},{"id":26125,"depth":66,"text":26126},{"id":26156,"depth":66,"text":26157},{"id":26442,"depth":66,"text":26443},{"id":26502,"depth":66,"text":26503},{"id":26576,"depth":66,"text":26577},{"id":1941,"depth":66,"text":1942,"children":26950},[26951,26952,26953,26954],{"id":26868,"depth":73,"text":26869},{"id":26881,"depth":73,"text":26882},{"id":26888,"depth":73,"text":26889},{"id":26898,"depth":73,"text":26899},{"id":2004,"depth":66,"text":2005},"Master python email sender script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-email-sender-script-example",{"title":25282,"description":26956},"examples\u002Fpython-email-sender-script-example","yPu0kW52TJkUKhbdrYqZL0O6TTN1MbrQs15ZuJBcokM",{"id":26963,"title":26964,"body":26965,"description":28487,"extension":2075,"meta":28488,"navigation":69,"path":28489,"seo":28490,"stem":28491,"__hash__":28492},"content\u002Fexamples\u002Fpython-error-handling-example-script.md","Python Error Handling Example Script",{"type":7,"value":26966,"toc":28444},[26967,26970,26983,26986,26997,27004,27006,27009,27154,27157,27188,27190,27193,27216,27220,27223,27367,27370,27389,27396,27408,27412,27416,27421,27476,27479,27501,27507,27510,27537,27540,27546,27553,27559,27565,27592,27599,27603,27611,27640,27643,27647,27652,27677,27680,27694,27698,27701,27707,27713,27718,27724,27729,27735,27739,27748,27754,27768,27771,27839,27842,27845,27934,27941,27945,27947,27953,27956,27959,27985,27988,28014,28021,28024,28027,28112,28114,28251,28255,28258,28262,28265,28268,28274,28280,28283,28289,28293,28296,28318,28322,28325,28357,28359,28363,28366,28370,28377,28383,28386,28391,28394,28398,28401,28403,28431,28441],[10,26968,26964],{"id":26969},"python-error-handling-example-script",[14,26971,26972,26973,2587,26975,2587,26977,11556,26979,26982],{},"This page shows a small, runnable Python script that uses ",[49,26974,3336],{},[49,26976,3552],{},[49,26978,1591],{},[49,26980,26981],{},"finally"," together.",[14,26984,26985],{},"It is a practical example, not a theory page. The goal is to help you see what happens when:",[309,26987,26988,26991,26994],{},[24,26989,26990],{},"code runs successfully",[24,26992,26993],{},"code raises an error",[24,26995,26996],{},"some final code should always run",[14,26998,26999,27000,186],{},"If you want a full explanation of the pattern itself, see ",[372,27001,27003],{"href":27002},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python\u002F","using try, except, else, and finally in Python",[37,27005,40],{"id":39},[14,27007,27008],{},"Use this script to see the main parts of Python error handling in one place:",[42,27010,27012],{"className":44,"code":27011,"language":46,"meta":47,"style":47},"try:\n    number = int(input(\"Enter a number: \"))\n    result = 10 \u002F number\nexcept ValueError:\n    print(\"Please enter a valid integer.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\nelse:\n    print(\"Result:\", result)\nfinally:\n    print(\"Program finished.\")\n",[49,27013,27014,27020,27044,27060,27068,27083,27092,27107,27113,27133,27139],{"__ignoreMap":47},[52,27015,27016,27018],{"class":54,"line":55},[52,27017,3336],{"class":58},[52,27019,1330],{"class":102},[52,27021,27022,27025,27027,27029,27031,27033,27035,27037,27040,27042],{"class":54,"line":66},[52,27023,27024],{"class":62},"    number ",[52,27026,80],{"class":79},[52,27028,9789],{"class":4733},[52,27030,193],{"class":102},[52,27032,13610],{"class":241},[52,27034,193],{"class":102},[52,27036,115],{"class":83},[52,27038,27039],{"class":87},"Enter a number: ",[52,27041,115],{"class":83},[52,27043,8886],{"class":102},[52,27045,27046,27049,27051,27054,27057],{"class":54,"line":73},[52,27047,27048],{"class":62},"    result ",[52,27050,80],{"class":79},[52,27052,27053],{"class":121}," 10",[52,27055,27056],{"class":79}," \u002F",[52,27058,27059],{"class":62}," number\n",[52,27061,27062,27064,27066],{"class":54,"line":94},[52,27063,3552],{"class":58},[52,27065,4734],{"class":4733},[52,27067,1330],{"class":102},[52,27069,27070,27072,27074,27076,27079,27081],{"class":54,"line":106},[52,27071,1599],{"class":241},[52,27073,193],{"class":102},[52,27075,115],{"class":83},[52,27077,27078],{"class":87},"Please enter a valid integer.",[52,27080,115],{"class":83},[52,27082,211],{"class":102},[52,27084,27085,27087,27090],{"class":54,"line":128},[52,27086,3552],{"class":58},[52,27088,27089],{"class":4733}," ZeroDivisionError",[52,27091,1330],{"class":102},[52,27093,27094,27096,27098,27100,27103,27105],{"class":54,"line":148},[52,27095,1599],{"class":241},[52,27097,193],{"class":102},[52,27099,115],{"class":83},[52,27101,27102],{"class":87},"You cannot divide by zero.",[52,27104,115],{"class":83},[52,27106,211],{"class":102},[52,27108,27109,27111],{"class":54,"line":164},[52,27110,1591],{"class":58},[52,27112,1330],{"class":102},[52,27114,27115,27117,27119,27121,27124,27126,27128,27131],{"class":54,"line":170},[52,27116,1599],{"class":241},[52,27118,193],{"class":102},[52,27120,115],{"class":83},[52,27122,27123],{"class":87},"Result:",[52,27125,115],{"class":83},[52,27127,199],{"class":102},[52,27129,27130],{"class":189}," result",[52,27132,211],{"class":102},[52,27134,27135,27137],{"class":54,"line":175},[52,27136,26981],{"class":58},[52,27138,1330],{"class":102},[52,27140,27141,27143,27145,27147,27150,27152],{"class":54,"line":214},[52,27142,1599],{"class":241},[52,27144,193],{"class":102},[52,27146,115],{"class":83},[52,27148,27149],{"class":87},"Program finished.",[52,27151,115],{"class":83},[52,27153,211],{"class":102},[14,27155,27156],{},"What it does:",[309,27158,27159,27166,27173,27179,27182,27185],{},[24,27160,27161,27162],{},"gets input from the user with ",[372,27163,27164],{"href":10606},[49,27165,10597],{},[24,27167,27168,27169],{},"converts that input to an integer with ",[372,27170,27171],{"href":9489},[49,27172,9492],{},[24,27174,27175,27176,27178],{},"tries to divide ",[49,27177,3373],{}," by that number",[24,27180,27181],{},"handles two common errors",[24,27183,27184],{},"prints a success message only when no error happens",[24,27186,27187],{},"always prints a final message",[37,27189,7216],{"id":7215},[14,27191,27192],{},"This script is useful because it shows a complete flow:",[309,27194,27195,27198,27210,27213],{},[24,27196,27197],{},"It uses a full script, not just isolated syntax.",[24,27199,27200,27201,2587,27203,2587,27205,11556,27207,27209],{},"It shows how ",[49,27202,3336],{},[49,27204,3552],{},[49,27206,1591],{},[49,27208,26981],{}," work together.",[24,27211,27212],{},"It focuses on beginner-friendly runtime errors.",[24,27214,27215],{},"It is small enough to run, test, and modify.",[37,27217,27219],{"id":27218},"the-full-example-script","The full example script",[14,27221,27222],{},"Here is the same example again with comments:",[42,27224,27226],{"className":44,"code":27225,"language":46,"meta":47,"style":47},"try:\n    user_input = input(\"Enter a number: \")\n    number = int(user_input)\n    result = 10 \u002F number\nexcept ValueError:\n    print(\"Please enter a valid integer.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\nelse:\n    print(\"Result:\", result)\nfinally:\n    print(\"Program finished.\")\n",[49,27227,27228,27234,27253,27267,27279,27287,27301,27309,27323,27329,27347,27353],{"__ignoreMap":47},[52,27229,27230,27232],{"class":54,"line":55},[52,27231,3336],{"class":58},[52,27233,1330],{"class":102},[52,27235,27236,27239,27241,27243,27245,27247,27249,27251],{"class":54,"line":66},[52,27237,27238],{"class":62},"    user_input ",[52,27240,80],{"class":79},[52,27242,11037],{"class":241},[52,27244,193],{"class":102},[52,27246,115],{"class":83},[52,27248,27039],{"class":87},[52,27250,115],{"class":83},[52,27252,211],{"class":102},[52,27254,27255,27257,27259,27261,27263,27265],{"class":54,"line":73},[52,27256,27024],{"class":62},[52,27258,80],{"class":79},[52,27260,9789],{"class":4733},[52,27262,193],{"class":102},[52,27264,14364],{"class":189},[52,27266,211],{"class":102},[52,27268,27269,27271,27273,27275,27277],{"class":54,"line":94},[52,27270,27048],{"class":62},[52,27272,80],{"class":79},[52,27274,27053],{"class":121},[52,27276,27056],{"class":79},[52,27278,27059],{"class":62},[52,27280,27281,27283,27285],{"class":54,"line":106},[52,27282,3552],{"class":58},[52,27284,4734],{"class":4733},[52,27286,1330],{"class":102},[52,27288,27289,27291,27293,27295,27297,27299],{"class":54,"line":128},[52,27290,1599],{"class":241},[52,27292,193],{"class":102},[52,27294,115],{"class":83},[52,27296,27078],{"class":87},[52,27298,115],{"class":83},[52,27300,211],{"class":102},[52,27302,27303,27305,27307],{"class":54,"line":148},[52,27304,3552],{"class":58},[52,27306,27089],{"class":4733},[52,27308,1330],{"class":102},[52,27310,27311,27313,27315,27317,27319,27321],{"class":54,"line":164},[52,27312,1599],{"class":241},[52,27314,193],{"class":102},[52,27316,115],{"class":83},[52,27318,27102],{"class":87},[52,27320,115],{"class":83},[52,27322,211],{"class":102},[52,27324,27325,27327],{"class":54,"line":170},[52,27326,1591],{"class":58},[52,27328,1330],{"class":102},[52,27330,27331,27333,27335,27337,27339,27341,27343,27345],{"class":54,"line":175},[52,27332,1599],{"class":241},[52,27334,193],{"class":102},[52,27336,115],{"class":83},[52,27338,27123],{"class":87},[52,27340,115],{"class":83},[52,27342,199],{"class":102},[52,27344,27130],{"class":189},[52,27346,211],{"class":102},[52,27348,27349,27351],{"class":54,"line":214},[52,27350,26981],{"class":58},[52,27352,1330],{"class":102},[52,27354,27355,27357,27359,27361,27363,27365],{"class":54,"line":233},[52,27356,1599],{"class":241},[52,27358,193],{"class":102},[52,27360,115],{"class":83},[52,27362,27149],{"class":87},[52,27364,115],{"class":83},[52,27366,211],{"class":102},[14,27368,27369],{},"This script does the following:",[309,27371,27372,27375,27378,27381,27384,27386],{},[24,27373,27374],{},"reads input from the user",[24,27376,27377],{},"converts the input to an integer",[24,27379,27380],{},"performs a calculation that may fail",[24,27382,27383],{},"catches more than one specific error",[24,27385,27184],{},[24,27387,27388],{},"runs final code every time",[14,27390,27391,27392,27395],{},"To run it, save it in a file such as ",[49,27393,27394],{},"script.py",", then run:",[42,27397,27399],{"className":390,"code":27398,"language":392,"meta":47,"style":47},"python script.py\n",[49,27400,27401],{"__ignoreMap":47},[52,27402,27403,27405],{"class":54,"line":55},[52,27404,46],{"class":399},[52,27406,27407],{"class":87}," script.py\n",[37,27409,27411],{"id":27410},"how-the-script-works-step-by-step","How the script works step by step",[639,27413,27414],{"id":3336},[49,27415,3336],{},[14,27417,358,27418,27420],{},[49,27419,3336],{}," block contains code that might raise an exception.",[42,27422,27424],{"className":44,"code":27423,"language":46,"meta":47,"style":47},"try:\n    user_input = input(\"Enter a number: \")\n    number = int(user_input)\n    result = 10 \u002F number\n",[49,27425,27426,27432,27450,27464],{"__ignoreMap":47},[52,27427,27428,27430],{"class":54,"line":55},[52,27429,3336],{"class":58},[52,27431,1330],{"class":102},[52,27433,27434,27436,27438,27440,27442,27444,27446,27448],{"class":54,"line":66},[52,27435,27238],{"class":62},[52,27437,80],{"class":79},[52,27439,11037],{"class":241},[52,27441,193],{"class":102},[52,27443,115],{"class":83},[52,27445,27039],{"class":87},[52,27447,115],{"class":83},[52,27449,211],{"class":102},[52,27451,27452,27454,27456,27458,27460,27462],{"class":54,"line":73},[52,27453,27024],{"class":62},[52,27455,80],{"class":79},[52,27457,9789],{"class":4733},[52,27459,193],{"class":102},[52,27461,14364],{"class":189},[52,27463,211],{"class":102},[52,27465,27466,27468,27470,27472,27474],{"class":54,"line":94},[52,27467,27048],{"class":62},[52,27469,80],{"class":79},[52,27471,27053],{"class":121},[52,27473,27056],{"class":79},[52,27475,27059],{"class":62},[14,27477,27478],{},"Possible problems here:",[309,27480,27481,27489],{},[24,27482,27483,27486,27487],{},[49,27484,27485],{},"int(user_input)"," can fail if the user enters text like ",[49,27488,6363],{},[24,27490,27491,27494,27495,27498,27499],{},[49,27492,27493],{},"10 \u002F number"," can fail if ",[49,27496,27497],{},"number"," is ",[49,27500,13294],{},[639,27502,27504],{"id":27503},"except-valueerror",[49,27505,27506],{},"except ValueError",[14,27508,27509],{},"This runs if Python cannot convert the input to an integer.",[42,27511,27513],{"className":44,"code":27512,"language":46,"meta":47,"style":47},"except ValueError:\n    print(\"Please enter a valid integer.\")\n",[49,27514,27515,27523],{"__ignoreMap":47},[52,27516,27517,27519,27521],{"class":54,"line":55},[52,27518,3552],{"class":58},[52,27520,4734],{"class":4733},[52,27522,1330],{"class":102},[52,27524,27525,27527,27529,27531,27533,27535],{"class":54,"line":66},[52,27526,1599],{"class":241},[52,27528,193],{"class":102},[52,27530,115],{"class":83},[52,27532,27078],{"class":87},[52,27534,115],{"class":83},[52,27536,211],{"class":102},[14,27538,27539],{},"Example input that causes this:",[42,27541,27544],{"className":27542,"code":27543,"language":955,"meta":47},[953],"hello\n",[49,27545,27543],{"__ignoreMap":47},[14,27547,27548,27549,1727,27551,186],{},"If you have seen the message about invalid input to ",[49,27550,9492],{},[372,27552,6472],{"href":6471},[639,27554,27556],{"id":27555},"except-zerodivisionerror",[49,27557,27558],{},"except ZeroDivisionError",[14,27560,27561,27562,27564],{},"This runs if the user enters ",[49,27563,13294],{}," and the script tries to divide by zero.",[42,27566,27568],{"className":44,"code":27567,"language":46,"meta":47,"style":47},"except ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n",[49,27569,27570,27578],{"__ignoreMap":47},[52,27571,27572,27574,27576],{"class":54,"line":55},[52,27573,3552],{"class":58},[52,27575,27089],{"class":4733},[52,27577,1330],{"class":102},[52,27579,27580,27582,27584,27586,27588,27590],{"class":54,"line":66},[52,27581,1599],{"class":241},[52,27583,193],{"class":102},[52,27585,115],{"class":83},[52,27587,27102],{"class":87},[52,27589,115],{"class":83},[52,27591,211],{"class":102},[14,27593,27594,27595,186],{},"Related error page: ",[372,27596,27598],{"href":27597},"\u002Ferrors\u002Fzerodivisionerror-division-by-zero-fix\u002F","ZeroDivisionError: division by zero",[639,27600,27601],{"id":1591},[49,27602,1591],{},[14,27604,358,27605,27607,27608,27610],{},[49,27606,1591],{}," block runs only if no exception happens in the ",[49,27609,3336],{}," block.",[42,27612,27614],{"className":44,"code":27613,"language":46,"meta":47,"style":47},"else:\n    print(\"Result:\", result)\n",[49,27615,27616,27622],{"__ignoreMap":47},[52,27617,27618,27620],{"class":54,"line":55},[52,27619,1591],{"class":58},[52,27621,1330],{"class":102},[52,27623,27624,27626,27628,27630,27632,27634,27636,27638],{"class":54,"line":66},[52,27625,1599],{"class":241},[52,27627,193],{"class":102},[52,27629,115],{"class":83},[52,27631,27123],{"class":87},[52,27633,115],{"class":83},[52,27635,199],{"class":102},[52,27637,27130],{"class":189},[52,27639,211],{"class":102},[14,27641,27642],{},"This keeps the success code separate from the error-handling code. That makes the script easier to read.",[639,27644,27645],{"id":26981},[49,27646,26981],{},[14,27648,358,27649,27651],{},[49,27650,26981],{}," block runs whether there is an error or not.",[42,27653,27655],{"className":44,"code":27654,"language":46,"meta":47,"style":47},"finally:\n    print(\"Program finished.\")\n",[49,27656,27657,27663],{"__ignoreMap":47},[52,27658,27659,27661],{"class":54,"line":55},[52,27660,26981],{"class":58},[52,27662,1330],{"class":102},[52,27664,27665,27667,27669,27671,27673,27675],{"class":54,"line":66},[52,27666,1599],{"class":241},[52,27668,193],{"class":102},[52,27670,115],{"class":83},[52,27672,27149],{"class":87},[52,27674,115],{"class":83},[52,27676,211],{"class":102},[14,27678,27679],{},"This is useful for:",[309,27681,27682,27685,27688,27691],{},[24,27683,27684],{},"cleanup code",[24,27686,27687],{},"closing files",[24,27689,27690],{},"closing database connections",[24,27692,27693],{},"final status messages",[37,27695,27697],{"id":27696},"expected-outputs-for-different-inputs","Expected outputs for different inputs",[14,27699,27700],{},"Here is what you should see for different user inputs.",[639,27702,27704,27705],{"id":27703},"if-the-input-is-2","If the input is ",[49,27706,9825],{},[42,27708,27711],{"className":27709,"code":27710,"language":955,"meta":47},[953],"Enter a number: 2\nResult: 5.0\nProgram finished.\n",[49,27712,27710],{"__ignoreMap":47},[639,27714,27704,27716],{"id":27715},"if-the-input-is-0",[49,27717,13294],{},[42,27719,27722],{"className":27720,"code":27721,"language":955,"meta":47},[953],"Enter a number: 0\nYou cannot divide by zero.\nProgram finished.\n",[49,27723,27721],{"__ignoreMap":47},[639,27725,27704,27727],{"id":27726},"if-the-input-is-hello",[49,27728,6363],{},[42,27730,27733],{"className":27731,"code":27732,"language":955,"meta":47},[953],"Enter a number: hello\nPlease enter a valid integer.\nProgram finished.\n",[49,27734,27732],{"__ignoreMap":47},[37,27736,27738],{"id":27737},"why-this-example-uses-specific-exceptions","Why this example uses specific exceptions",[14,27740,27741,27742,759,27744,27747],{},"This script catches ",[49,27743,13780],{},[49,27745,27746],{},"ZeroDivisionError"," on purpose.",[14,27749,27750,27751,27753],{},"That is better than using a broad ",[49,27752,3552],{}," in this case because:",[309,27755,27756,27759,27762,27765],{},[24,27757,27758],{},"specific exceptions are clearer",[24,27760,27761],{},"they help you understand what went wrong",[24,27763,27764],{},"they make debugging easier",[24,27766,27767],{},"they avoid hiding unrelated bugs",[14,27769,27770],{},"For example, this is usually too broad for a beginner script:",[42,27772,27774],{"className":44,"code":27773,"language":46,"meta":47,"style":47},"try:\n    number = int(input(\"Enter a number: \"))\n    result = 10 \u002F number\nexcept Exception:\n    print(\"Something went wrong.\")\n",[49,27775,27776,27782,27804,27816,27824],{"__ignoreMap":47},[52,27777,27778,27780],{"class":54,"line":55},[52,27779,3336],{"class":58},[52,27781,1330],{"class":102},[52,27783,27784,27786,27788,27790,27792,27794,27796,27798,27800,27802],{"class":54,"line":66},[52,27785,27024],{"class":62},[52,27787,80],{"class":79},[52,27789,9789],{"class":4733},[52,27791,193],{"class":102},[52,27793,13610],{"class":241},[52,27795,193],{"class":102},[52,27797,115],{"class":83},[52,27799,27039],{"class":87},[52,27801,115],{"class":83},[52,27803,8886],{"class":102},[52,27805,27806,27808,27810,27812,27814],{"class":54,"line":73},[52,27807,27048],{"class":62},[52,27809,80],{"class":79},[52,27811,27053],{"class":121},[52,27813,27056],{"class":79},[52,27815,27059],{"class":62},[52,27817,27818,27820,27822],{"class":54,"line":94},[52,27819,3552],{"class":58},[52,27821,26829],{"class":4733},[52,27823,1330],{"class":102},[52,27825,27826,27828,27830,27832,27835,27837],{"class":54,"line":106},[52,27827,1599],{"class":241},[52,27829,193],{"class":102},[52,27831,115],{"class":83},[52,27833,27834],{"class":87},"Something went wrong.",[52,27836,115],{"class":83},[52,27838,211],{"class":102},[14,27840,27841],{},"This catches many different errors, but the message is not very helpful.",[14,27843,27844],{},"A better version is:",[42,27846,27848],{"className":44,"code":27847,"language":46,"meta":47,"style":47},"try:\n    number = int(input(\"Enter a number: \"))\n    result = 10 \u002F number\nexcept ValueError:\n    print(\"Please enter a valid integer.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\n",[49,27849,27850,27856,27878,27890,27898,27912,27920],{"__ignoreMap":47},[52,27851,27852,27854],{"class":54,"line":55},[52,27853,3336],{"class":58},[52,27855,1330],{"class":102},[52,27857,27858,27860,27862,27864,27866,27868,27870,27872,27874,27876],{"class":54,"line":66},[52,27859,27024],{"class":62},[52,27861,80],{"class":79},[52,27863,9789],{"class":4733},[52,27865,193],{"class":102},[52,27867,13610],{"class":241},[52,27869,193],{"class":102},[52,27871,115],{"class":83},[52,27873,27039],{"class":87},[52,27875,115],{"class":83},[52,27877,8886],{"class":102},[52,27879,27880,27882,27884,27886,27888],{"class":54,"line":73},[52,27881,27048],{"class":62},[52,27883,80],{"class":79},[52,27885,27053],{"class":121},[52,27887,27056],{"class":79},[52,27889,27059],{"class":62},[52,27891,27892,27894,27896],{"class":54,"line":94},[52,27893,3552],{"class":58},[52,27895,4734],{"class":4733},[52,27897,1330],{"class":102},[52,27899,27900,27902,27904,27906,27908,27910],{"class":54,"line":106},[52,27901,1599],{"class":241},[52,27903,193],{"class":102},[52,27905,115],{"class":83},[52,27907,27078],{"class":87},[52,27909,115],{"class":83},[52,27911,211],{"class":102},[52,27913,27914,27916,27918],{"class":54,"line":128},[52,27915,3552],{"class":58},[52,27917,27089],{"class":4733},[52,27919,1330],{"class":102},[52,27921,27922,27924,27926,27928,27930,27932],{"class":54,"line":148},[52,27923,1599],{"class":241},[52,27925,193],{"class":102},[52,27927,115],{"class":83},[52,27929,27102],{"class":87},[52,27931,115],{"class":83},[52,27933,211],{"class":102},[14,27935,27936,27937,186],{},"If you want more examples like this, see ",[372,27938,27940],{"href":27939},"\u002Fhow-to\u002Fhow-to-catch-multiple-exceptions-in-python\u002F","how to catch multiple exceptions in Python",[37,27942,27944],{"id":27943},"common-beginner-mistakes-in-error-handling","Common beginner mistakes in error handling",[14,27946,17965],{},[639,27948,10294,27950,27952],{"id":27949},"using-except-without-knowing-which-error-is-expected",[49,27951,3552],{}," without knowing which error is expected",[14,27954,27955],{},"If you do not know what error you are catching, your message may be too vague.",[14,27957,27958],{},"Bad:",[42,27960,27962],{"className":44,"code":27961,"language":46,"meta":47,"style":47},"except:\n    print(\"Error\")\n",[49,27963,27964,27970],{"__ignoreMap":47},[52,27965,27966,27968],{"class":54,"line":55},[52,27967,3552],{"class":58},[52,27969,1330],{"class":102},[52,27971,27972,27974,27976,27978,27981,27983],{"class":54,"line":66},[52,27973,1599],{"class":241},[52,27975,193],{"class":102},[52,27977,115],{"class":83},[52,27979,27980],{"class":87},"Error",[52,27982,115],{"class":83},[52,27984,211],{"class":102},[14,27986,27987],{},"Better:",[42,27989,27990],{"className":44,"code":27512,"language":46,"meta":47,"style":47},[49,27991,27992,28000],{"__ignoreMap":47},[52,27993,27994,27996,27998],{"class":54,"line":55},[52,27995,3552],{"class":58},[52,27997,4734],{"class":4733},[52,27999,1330],{"class":102},[52,28001,28002,28004,28006,28008,28010,28012],{"class":54,"line":66},[52,28003,1599],{"class":241},[52,28005,193],{"class":102},[52,28007,115],{"class":83},[52,28009,27078],{"class":87},[52,28011,115],{"class":83},[52,28013,211],{"class":102},[639,28015,28017,28018,28020],{"id":28016},"putting-too-much-code-inside-the-try-block","Putting too much code inside the ",[49,28019,3336],{}," block",[14,28022,28023],{},"Keep the risky code small.",[14,28025,28026],{},"Less clear:",[42,28028,28030],{"className":44,"code":28029,"language":46,"meta":47,"style":47},"try:\n    user_input = input(\"Enter a number: \")\n    number = int(user_input)\n    result = 10 \u002F number\n    print(\"Calculation complete\")\n    print(\"Saving result\")\n",[49,28031,28032,28038,28056,28070,28082,28097],{"__ignoreMap":47},[52,28033,28034,28036],{"class":54,"line":55},[52,28035,3336],{"class":58},[52,28037,1330],{"class":102},[52,28039,28040,28042,28044,28046,28048,28050,28052,28054],{"class":54,"line":66},[52,28041,27238],{"class":62},[52,28043,80],{"class":79},[52,28045,11037],{"class":241},[52,28047,193],{"class":102},[52,28049,115],{"class":83},[52,28051,27039],{"class":87},[52,28053,115],{"class":83},[52,28055,211],{"class":102},[52,28057,28058,28060,28062,28064,28066,28068],{"class":54,"line":73},[52,28059,27024],{"class":62},[52,28061,80],{"class":79},[52,28063,9789],{"class":4733},[52,28065,193],{"class":102},[52,28067,14364],{"class":189},[52,28069,211],{"class":102},[52,28071,28072,28074,28076,28078,28080],{"class":54,"line":94},[52,28073,27048],{"class":62},[52,28075,80],{"class":79},[52,28077,27053],{"class":121},[52,28079,27056],{"class":79},[52,28081,27059],{"class":62},[52,28083,28084,28086,28088,28090,28093,28095],{"class":54,"line":106},[52,28085,1599],{"class":241},[52,28087,193],{"class":102},[52,28089,115],{"class":83},[52,28091,28092],{"class":87},"Calculation complete",[52,28094,115],{"class":83},[52,28096,211],{"class":102},[52,28098,28099,28101,28103,28105,28108,28110],{"class":54,"line":128},[52,28100,1599],{"class":241},[52,28102,193],{"class":102},[52,28104,115],{"class":83},[52,28106,28107],{"class":87},"Saving result",[52,28109,115],{"class":83},[52,28111,211],{"class":102},[14,28113,27987],{},[42,28115,28117],{"className":44,"code":28116,"language":46,"meta":47,"style":47},"try:\n    user_input = input(\"Enter a number: \")\n    number = int(user_input)\n    result = 10 \u002F number\nexcept ValueError:\n    print(\"Please enter a valid integer.\")\nexcept ZeroDivisionError:\n    print(\"You cannot divide by zero.\")\nelse:\n    print(\"Result:\", result)\n    print(\"Calculation complete\")\n",[49,28118,28119,28125,28143,28157,28169,28177,28191,28199,28213,28219,28237],{"__ignoreMap":47},[52,28120,28121,28123],{"class":54,"line":55},[52,28122,3336],{"class":58},[52,28124,1330],{"class":102},[52,28126,28127,28129,28131,28133,28135,28137,28139,28141],{"class":54,"line":66},[52,28128,27238],{"class":62},[52,28130,80],{"class":79},[52,28132,11037],{"class":241},[52,28134,193],{"class":102},[52,28136,115],{"class":83},[52,28138,27039],{"class":87},[52,28140,115],{"class":83},[52,28142,211],{"class":102},[52,28144,28145,28147,28149,28151,28153,28155],{"class":54,"line":73},[52,28146,27024],{"class":62},[52,28148,80],{"class":79},[52,28150,9789],{"class":4733},[52,28152,193],{"class":102},[52,28154,14364],{"class":189},[52,28156,211],{"class":102},[52,28158,28159,28161,28163,28165,28167],{"class":54,"line":94},[52,28160,27048],{"class":62},[52,28162,80],{"class":79},[52,28164,27053],{"class":121},[52,28166,27056],{"class":79},[52,28168,27059],{"class":62},[52,28170,28171,28173,28175],{"class":54,"line":106},[52,28172,3552],{"class":58},[52,28174,4734],{"class":4733},[52,28176,1330],{"class":102},[52,28178,28179,28181,28183,28185,28187,28189],{"class":54,"line":128},[52,28180,1599],{"class":241},[52,28182,193],{"class":102},[52,28184,115],{"class":83},[52,28186,27078],{"class":87},[52,28188,115],{"class":83},[52,28190,211],{"class":102},[52,28192,28193,28195,28197],{"class":54,"line":148},[52,28194,3552],{"class":58},[52,28196,27089],{"class":4733},[52,28198,1330],{"class":102},[52,28200,28201,28203,28205,28207,28209,28211],{"class":54,"line":164},[52,28202,1599],{"class":241},[52,28204,193],{"class":102},[52,28206,115],{"class":83},[52,28208,27102],{"class":87},[52,28210,115],{"class":83},[52,28212,211],{"class":102},[52,28214,28215,28217],{"class":54,"line":170},[52,28216,1591],{"class":58},[52,28218,1330],{"class":102},[52,28220,28221,28223,28225,28227,28229,28231,28233,28235],{"class":54,"line":175},[52,28222,1599],{"class":241},[52,28224,193],{"class":102},[52,28226,115],{"class":83},[52,28228,27123],{"class":87},[52,28230,115],{"class":83},[52,28232,199],{"class":102},[52,28234,27130],{"class":189},[52,28236,211],{"class":102},[52,28238,28239,28241,28243,28245,28247,28249],{"class":54,"line":214},[52,28240,1599],{"class":241},[52,28242,193],{"class":102},[52,28244,115],{"class":83},[52,28246,28092],{"class":87},[52,28248,115],{"class":83},[52,28250,211],{"class":102},[639,28252,28254],{"id":28253},"catching-the-error-but-not-fixing-the-real-cause","Catching the error but not fixing the real cause",[14,28256,28257],{},"Error handling should help you respond to a known problem. It should not hide mistakes in your code.",[639,28259,28261],{"id":28260},"using-error-handling-when-simple-validation-is-easier","Using error handling when simple validation is easier",[14,28263,28264],{},"Sometimes input checks are simpler than relying only on exceptions.",[14,28266,28267],{},"For example, if you want to reject empty input before conversion, a check may help.",[639,28269,12434,28271,28273],{"id":28270},"forgetting-that-finally-always-runs",[49,28272,26981],{}," always runs",[14,28275,28276,28277,28279],{},"Even if an exception happens, the ",[49,28278,26981],{}," block still runs.",[14,28281,28282],{},"That is why this script always prints:",[42,28284,28287],{"className":28285,"code":28286,"language":955,"meta":47},[953],"Program finished.\n",[49,28288,28286],{"__ignoreMap":47},[37,28290,28292],{"id":28291},"common-causes-of-problems-in-scripts-like-this","Common causes of problems in scripts like this",[14,28294,28295],{},"These issues often lead to confusing behavior:",[309,28297,28298,28301,28307,28310,28315],{},[24,28299,28300],{},"The user enters text when the script expects a number.",[24,28302,28303,28304,28306],{},"The user enters ",[49,28305,13294],{}," before division.",[24,28308,28309],{},"The code catches exceptions too broadly.",[24,28311,358,28312,28314],{},[49,28313,3336],{}," block includes lines that are unrelated to the risky operation.",[24,28316,28317],{},"The script prints unclear error messages.",[37,28319,28321],{"id":28320},"good-next-steps-after-this-example","Good next steps after this example",[14,28323,28324],{},"After you run this script, try one of these next steps:",[309,28326,28327,28332,28339,28344,28347,28350],{},[24,28328,28329,28330,186],{},"Learn the full syntax in ",[372,28331,27003],{"href":27002},[24,28333,28334,28335,186],{},"Practice the pattern in ",[372,28336,28338],{"href":28337},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use try-except blocks in Python",[24,28340,28341,28342,186],{},"Read more about ",[372,28343,10032],{"href":10031},[24,28345,28346],{},"Change the script so it accepts float input instead of integer input.",[24,28348,28349],{},"Change the script so it handles file input instead of user input.",[24,28351,28352,28353,28356],{},"Try adding your own ",[49,28354,28355],{},"raise"," statement in a new version of the script.",[37,28358,1942],{"id":1941},[639,28360,28362],{"id":28361},"is-this-page-about-python-error-handling-theory-or-a-practical-example","Is this page about Python error handling theory or a practical example?",[14,28364,28365],{},"It is an example page. It focuses on one runnable script and explains how it behaves.",[639,28367,28369],{"id":28368},"should-the-example-catch-every-possible-exception","Should the example catch every possible exception?",[14,28371,28372,28373,759,28375,186],{},"No. It should catch only the specific exceptions that match the script, such as ",[49,28374,13780],{},[49,28376,27746],{},[639,28378,1956,28380,28382],{"id":28379},"why-use-else-in-this-script",[49,28381,1591],{}," in this script?",[14,28384,28385],{},"It keeps the success code separate from the error-handling code. That makes the flow easier to read.",[639,28387,1956,28389,28382],{"id":28388},"why-use-finally-in-this-script",[49,28390,26981],{},[14,28392,28393],{},"It shows code that always runs, which is useful for cleanup or final messages.",[639,28395,28397],{"id":28396},"how-is-this-different-from-the-page-about-using-try-except-blocks","How is this different from the page about using try-except blocks?",[14,28399,28400],{},"That page explains the pattern. This page shows a complete example script using the pattern.",[37,28402,2005],{"id":2004},[309,28404,28405,28409,28414,28418,28423,28427],{},[24,28406,28407],{},[372,28408,10032],{"href":10031},[24,28410,28411],{},[372,28412,28413],{"href":27002},"Using try, except, else, and finally in Python",[24,28415,28416],{},[372,28417,26911],{"href":10027},[24,28419,28420],{},[372,28421,28422],{"href":27939},"How to catch multiple exceptions in Python",[24,28424,28425],{},[372,28426,13726],{"href":10606},[24,28428,28429],{},[372,28430,13731],{"href":9489},[14,28432,28433,28434,2587,28436,11556,28438,28440],{},"Run the script a few times with different inputs like ",[49,28435,9825],{},[49,28437,13294],{},[49,28439,6363],{},". That is the fastest way to understand how each part works.",[2034,28442,28443],{},"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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":47,"searchDepth":66,"depth":66,"links":28445},[28446,28447,28448,28449,28456,28464,28465,28475,28476,28477,28486],{"id":39,"depth":66,"text":40},{"id":7215,"depth":66,"text":7216},{"id":27218,"depth":66,"text":27219},{"id":27410,"depth":66,"text":27411,"children":28450},[28451,28452,28453,28454,28455],{"id":3336,"depth":73,"text":3336},{"id":27503,"depth":73,"text":27506},{"id":27555,"depth":73,"text":27558},{"id":1591,"depth":73,"text":1591},{"id":26981,"depth":73,"text":26981},{"id":27696,"depth":66,"text":27697,"children":28457},[28458,28460,28462],{"id":27703,"depth":73,"text":28459},"If the input is 2",{"id":27715,"depth":73,"text":28461},"If the input is 0",{"id":27726,"depth":73,"text":28463},"If the input is hello",{"id":27737,"depth":66,"text":27738},{"id":27943,"depth":66,"text":27944,"children":28466},[28467,28469,28471,28472,28473],{"id":27949,"depth":73,"text":28468},"Using except without knowing which error is expected",{"id":28016,"depth":73,"text":28470},"Putting too much code inside the try block",{"id":28253,"depth":73,"text":28254},{"id":28260,"depth":73,"text":28261},{"id":28270,"depth":73,"text":28474},"Forgetting that finally always runs",{"id":28291,"depth":66,"text":28292},{"id":28320,"depth":66,"text":28321},{"id":1941,"depth":66,"text":1942,"children":28478},[28479,28480,28481,28483,28485],{"id":28361,"depth":73,"text":28362},{"id":28368,"depth":73,"text":28369},{"id":28379,"depth":73,"text":28482},"Why use else in this script?",{"id":28388,"depth":73,"text":28484},"Why use finally in this script?",{"id":28396,"depth":73,"text":28397},{"id":2004,"depth":66,"text":2005},"Master python error handling example script in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-error-handling-example-script",{"title":26964,"description":28487},"examples\u002Fpython-error-handling-example-script","usH-sYhLG4C1JjV2nDpWUJxYWSk5SiKBz993v0cy3Nw",{"id":28494,"title":28495,"body":28496,"description":29984,"extension":2075,"meta":29985,"navigation":69,"path":29986,"seo":29987,"stem":29988,"__hash__":29989},"content\u002Fexamples\u002Fpython-file-backup-script-example.md","Python File Backup Script Example",{"type":7,"value":28497,"toc":29949},[28498,28501,28504,28507,28518,28521,28523,28526,28674,28677,28681,28695,28699,28701,28717,28724,28728,28731,28748,28751,28755,28757,28889,28893,28914,28932,28937,28941,28982,28995,28998,29003,29007,29024,29027,29030,29049,29054,29058,29081,29084,29094,29098,29121,29124,29126,29131,29142,29144,29150,29154,29157,29160,29171,29173,29368,29371,29375,29378,29380,29553,29557,29596,29601,29625,29628,29635,29639,29642,29656,29658,29678,29684,29690,29692,29695,29699,29714,29717,29721,29740,29746,29750,29780,29783,29793,29797,29812,29815,29819,29822,29840,29842,29845,29861,29869,29871,29875,29880,29884,29890,29894,29897,29901,29904,29908,29913,29915,29946],[10,28499,28495],{"id":28500},"python-file-backup-script-example",[14,28502,28503],{},"This beginner-friendly example shows how to create a simple Python file backup script.",[14,28505,28506],{},"The goal is small and practical:",[309,28508,28509,28512,28515],{},[24,28510,28511],{},"copy one file to a backup file",[24,28513,28514],{},"check that the original file exists first",[24,28516,28517],{},"print a clear message about what happened",[14,28519,28520],{},"This is a good first project if you are learning how Python works with files and paths.",[37,28522,40],{"id":39},[14,28524,28525],{},"Use this as the fastest working version:",[42,28527,28529],{"className":44,"code":28528,"language":46,"meta":47,"style":47},"from pathlib import Path\nimport shutil\n\nsource = Path(\"notes.txt\")\nbackup = Path(\"notes_backup.txt\")\n\nif source.exists():\n    shutil.copy(source, backup)\n    print(\"Backup created:\", backup)\nelse:\n    print(\"Source file not found\")\n",[49,28530,28531,28543,28550,28554,28575,28595,28599,28612,28634,28653,28659],{"__ignoreMap":47},[52,28532,28533,28535,28538,28540],{"class":54,"line":55},[52,28534,25329],{"class":58},[52,28536,28537],{"class":62}," pathlib ",[52,28539,59],{"class":58},[52,28541,28542],{"class":62}," Path\n",[52,28544,28545,28547],{"class":54,"line":66},[52,28546,59],{"class":58},[52,28548,28549],{"class":62}," shutil\n",[52,28551,28552],{"class":54,"line":73},[52,28553,70],{"emptyLinePlaceholder":69},[52,28555,28556,28559,28561,28564,28566,28568,28571,28573],{"class":54,"line":94},[52,28557,28558],{"class":62},"source ",[52,28560,80],{"class":79},[52,28562,28563],{"class":189}," Path",[52,28565,193],{"class":102},[52,28567,115],{"class":83},[52,28569,28570],{"class":87},"notes.txt",[52,28572,115],{"class":83},[52,28574,211],{"class":102},[52,28576,28577,28580,28582,28584,28586,28588,28591,28593],{"class":54,"line":106},[52,28578,28579],{"class":62},"backup ",[52,28581,80],{"class":79},[52,28583,28563],{"class":189},[52,28585,193],{"class":102},[52,28587,115],{"class":83},[52,28589,28590],{"class":87},"notes_backup.txt",[52,28592,115],{"class":83},[52,28594,211],{"class":102},[52,28596,28597],{"class":54,"line":128},[52,28598,70],{"emptyLinePlaceholder":69},[52,28600,28601,28603,28606,28608,28610],{"class":54,"line":148},[52,28602,1313],{"class":58},[52,28604,28605],{"class":62}," source",[52,28607,186],{"class":102},[52,28609,15887],{"class":189},[52,28611,8146],{"class":102},[52,28613,28614,28617,28619,28622,28624,28627,28629,28632],{"class":54,"line":164},[52,28615,28616],{"class":62},"    shutil",[52,28618,186],{"class":102},[52,28620,28621],{"class":189},"copy",[52,28623,193],{"class":102},[52,28625,28626],{"class":189},"source",[52,28628,199],{"class":102},[52,28630,28631],{"class":189}," backup",[52,28633,211],{"class":102},[52,28635,28636,28638,28640,28642,28645,28647,28649,28651],{"class":54,"line":170},[52,28637,1599],{"class":241},[52,28639,193],{"class":102},[52,28641,115],{"class":83},[52,28643,28644],{"class":87},"Backup created:",[52,28646,115],{"class":83},[52,28648,199],{"class":102},[52,28650,28631],{"class":189},[52,28652,211],{"class":102},[52,28654,28655,28657],{"class":54,"line":175},[52,28656,1591],{"class":58},[52,28658,1330],{"class":102},[52,28660,28661,28663,28665,28667,28670,28672],{"class":54,"line":214},[52,28662,1599],{"class":241},[52,28664,193],{"class":102},[52,28666,115],{"class":83},[52,28668,28669],{"class":87},"Source file not found",[52,28671,115],{"class":83},[52,28673,211],{"class":102},[14,28675,28676],{},"This script copies one file and helps you understand the main backup idea before adding folders, timestamps, or multiple files.",[37,28678,28680],{"id":28679},"what-this-script-does","What this script does",[309,28682,28683,28686,28689,28692],{},[24,28684,28685],{},"Copies one file to a new backup file",[24,28687,28688],{},"Checks whether the source file exists first",[24,28690,28691],{},"Uses built-in Python tools",[24,28693,28694],{},"Shows a practical beginner project",[37,28696,28698],{"id":28697},"what-the-user-needs-before-starting","What the user needs before starting",[14,28700,23910],{},[309,28702,28703,28706,28711,28714],{},[24,28704,28705],{},"Python installed and working",[24,28707,28708,28709],{},"A sample file to back up, such as ",[49,28710,28570],{},[24,28712,28713],{},"A basic understanding of file paths",[24,28715,28716],{},"Permission to read the source file and write the backup file",[14,28718,28719,28720,186],{},"If you are unsure how paths work, see ",[372,28721,28723],{"href":28722},"\u002Flearn\u002Fworking-with-file-paths-in-python\u002F","working with file paths in Python",[37,28725,28727],{"id":28726},"main-idea-behind-a-backup-script","Main idea behind a backup script",[14,28729,28730],{},"A basic backup script follows a simple process:",[21,28732,28733,28736,28739,28742,28745],{},[24,28734,28735],{},"Choose the original file",[24,28737,28738],{},"Choose where the backup should be saved",[24,28740,28741],{},"Check that the original file exists",[24,28743,28744],{},"Copy the file",[24,28746,28747],{},"Print a clear success or error message",[14,28749,28750],{},"That is all this example does.",[37,28752,28754],{"id":28753},"simple-backup-script-walkthrough","Simple backup script walkthrough",[14,28756,25644],{},[42,28758,28759],{"className":44,"code":28528,"language":46,"meta":47,"style":47},[49,28760,28761,28771,28777,28781,28799,28817,28821,28833,28851,28869,28875],{"__ignoreMap":47},[52,28762,28763,28765,28767,28769],{"class":54,"line":55},[52,28764,25329],{"class":58},[52,28766,28537],{"class":62},[52,28768,59],{"class":58},[52,28770,28542],{"class":62},[52,28772,28773,28775],{"class":54,"line":66},[52,28774,59],{"class":58},[52,28776,28549],{"class":62},[52,28778,28779],{"class":54,"line":73},[52,28780,70],{"emptyLinePlaceholder":69},[52,28782,28783,28785,28787,28789,28791,28793,28795,28797],{"class":54,"line":94},[52,28784,28558],{"class":62},[52,28786,80],{"class":79},[52,28788,28563],{"class":189},[52,28790,193],{"class":102},[52,28792,115],{"class":83},[52,28794,28570],{"class":87},[52,28796,115],{"class":83},[52,28798,211],{"class":102},[52,28800,28801,28803,28805,28807,28809,28811,28813,28815],{"class":54,"line":106},[52,28802,28579],{"class":62},[52,28804,80],{"class":79},[52,28806,28563],{"class":189},[52,28808,193],{"class":102},[52,28810,115],{"class":83},[52,28812,28590],{"class":87},[52,28814,115],{"class":83},[52,28816,211],{"class":102},[52,28818,28819],{"class":54,"line":128},[52,28820,70],{"emptyLinePlaceholder":69},[52,28822,28823,28825,28827,28829,28831],{"class":54,"line":148},[52,28824,1313],{"class":58},[52,28826,28605],{"class":62},[52,28828,186],{"class":102},[52,28830,15887],{"class":189},[52,28832,8146],{"class":102},[52,28834,28835,28837,28839,28841,28843,28845,28847,28849],{"class":54,"line":164},[52,28836,28616],{"class":62},[52,28838,186],{"class":102},[52,28840,28621],{"class":189},[52,28842,193],{"class":102},[52,28844,28626],{"class":189},[52,28846,199],{"class":102},[52,28848,28631],{"class":189},[52,28850,211],{"class":102},[52,28852,28853,28855,28857,28859,28861,28863,28865,28867],{"class":54,"line":170},[52,28854,1599],{"class":241},[52,28856,193],{"class":102},[52,28858,115],{"class":83},[52,28860,28644],{"class":87},[52,28862,115],{"class":83},[52,28864,199],{"class":102},[52,28866,28631],{"class":189},[52,28868,211],{"class":102},[52,28870,28871,28873],{"class":54,"line":175},[52,28872,1591],{"class":58},[52,28874,1330],{"class":102},[52,28876,28877,28879,28881,28883,28885,28887],{"class":54,"line":214},[52,28878,1599],{"class":241},[52,28880,193],{"class":102},[52,28882,115],{"class":83},[52,28884,28669],{"class":87},[52,28886,115],{"class":83},[52,28888,211],{"class":102},[639,28890,28892],{"id":28891},"step-1-import-the-tools","Step 1: import the tools",[42,28894,28896],{"className":44,"code":28895,"language":46,"meta":47,"style":47},"from pathlib import Path\nimport shutil\n",[49,28897,28898,28908],{"__ignoreMap":47},[52,28899,28900,28902,28904,28906],{"class":54,"line":55},[52,28901,25329],{"class":58},[52,28903,28537],{"class":62},[52,28905,59],{"class":58},[52,28907,28542],{"class":62},[52,28909,28910,28912],{"class":54,"line":66},[52,28911,59],{"class":58},[52,28913,28549],{"class":62},[309,28915,28916,28926],{},[24,28917,28918,28921,28922,28925],{},[49,28919,28920],{},"Path"," from ",[49,28923,28924],{},"pathlib"," makes file paths easier to read and work with",[24,28927,28928,28931],{},[49,28929,28930],{},"shutil"," includes tools for copying files",[14,28933,5339,28934,28936],{},[49,28935,28924],{}," is usually easier to understand than older string-based path handling.",[639,28938,28940],{"id":28939},"step-2-choose-the-source-and-backup-paths","Step 2: choose the source and backup paths",[42,28942,28944],{"className":44,"code":28943,"language":46,"meta":47,"style":47},"source = Path(\"notes.txt\")\nbackup = Path(\"notes_backup.txt\")\n",[49,28945,28946,28964],{"__ignoreMap":47},[52,28947,28948,28950,28952,28954,28956,28958,28960,28962],{"class":54,"line":55},[52,28949,28558],{"class":62},[52,28951,80],{"class":79},[52,28953,28563],{"class":189},[52,28955,193],{"class":102},[52,28957,115],{"class":83},[52,28959,28570],{"class":87},[52,28961,115],{"class":83},[52,28963,211],{"class":102},[52,28965,28966,28968,28970,28972,28974,28976,28978,28980],{"class":54,"line":66},[52,28967,28579],{"class":62},[52,28969,80],{"class":79},[52,28971,28563],{"class":189},[52,28973,193],{"class":102},[52,28975,115],{"class":83},[52,28977,28590],{"class":87},[52,28979,115],{"class":83},[52,28981,211],{"class":102},[309,28983,28984,28989],{},[24,28985,28986,28988],{},[49,28987,28626],{}," is the file you want to back up",[24,28990,28991,28994],{},[49,28992,28993],{},"backup"," is the new file that will be created",[14,28996,28997],{},"In this example, both files are in the current folder.",[14,28999,29000,29001,186],{},"If you want to check whether a file exists in more detail, see ",[372,29002,15908],{"href":15907},[639,29004,29006],{"id":29005},"step-3-check-that-the-source-file-exists","Step 3: check that the source file exists",[42,29008,29010],{"className":44,"code":29009,"language":46,"meta":47,"style":47},"if source.exists():\n",[49,29011,29012],{"__ignoreMap":47},[52,29013,29014,29016,29018,29020,29022],{"class":54,"line":55},[52,29015,1313],{"class":58},[52,29017,28605],{"class":62},[52,29019,186],{"class":102},[52,29021,15887],{"class":189},[52,29023,8146],{"class":102},[14,29025,29026],{},"This prevents the script from trying to copy a file that is not there.",[14,29028,29029],{},"If the file does not exist, the script prints:",[42,29031,29033],{"className":44,"code":29032,"language":46,"meta":47,"style":47},"print(\"Source file not found\")\n",[49,29034,29035],{"__ignoreMap":47},[52,29036,29037,29039,29041,29043,29045,29047],{"class":54,"line":55},[52,29038,242],{"class":241},[52,29040,193],{"class":102},[52,29042,115],{"class":83},[52,29044,28669],{"class":87},[52,29046,115],{"class":83},[52,29048,211],{"class":102},[14,29050,29051,29052,186],{},"This is a simple way to avoid a ",[49,29053,15764],{},[639,29055,29057],{"id":29056},"step-4-copy-the-file","Step 4: copy the file",[42,29059,29061],{"className":44,"code":29060,"language":46,"meta":47,"style":47},"shutil.copy(source, backup)\n",[49,29062,29063],{"__ignoreMap":47},[52,29064,29065,29067,29069,29071,29073,29075,29077,29079],{"class":54,"line":55},[52,29066,28930],{"class":62},[52,29068,186],{"class":102},[52,29070,28621],{"class":189},[52,29072,193],{"class":102},[52,29074,28626],{"class":189},[52,29076,199],{"class":102},[52,29078,28631],{"class":189},[52,29080,211],{"class":102},[14,29082,29083],{},"This creates the backup file.",[309,29085,29086,29091],{},[24,29087,7526,29088,29090],{},[49,29089,28590],{}," does not exist, Python creates it",[24,29092,29093],{},"If it already exists, it will usually be overwritten",[639,29095,29097],{"id":29096},"step-5-print-a-success-message","Step 5: print a success message",[42,29099,29101],{"className":44,"code":29100,"language":46,"meta":47,"style":47},"print(\"Backup created:\", backup)\n",[49,29102,29103],{"__ignoreMap":47},[52,29104,29105,29107,29109,29111,29113,29115,29117,29119],{"class":54,"line":55},[52,29106,242],{"class":241},[52,29108,193],{"class":102},[52,29110,115],{"class":83},[52,29112,28644],{"class":87},[52,29114,115],{"class":83},[52,29116,199],{"class":102},[52,29118,28631],{"class":189},[52,29120,211],{"class":102},[14,29122,29123],{},"This lets you know the copy worked.",[37,29125,26126],{"id":26125},[14,29127,7526,29128,29130],{},[49,29129,28570],{}," exists and the script runs successfully:",[309,29132,29133,29136,29139],{},[24,29134,29135],{},"A new file appears at the backup path",[24,29137,29138],{},"The copied file has the same contents as the original",[24,29140,29141],{},"The terminal prints a success message",[14,29143,11845],{},[42,29145,29148],{"className":29146,"code":29147,"language":955,"meta":47},[953],"Backup created: notes_backup.txt\n",[49,29149,29147],{"__ignoreMap":47},[37,29151,29153],{"id":29152},"useful-improvement-add-a-timestamp-to-the-backup-name","Useful improvement: add a timestamp to the backup name",[14,29155,29156],{},"A basic backup script can overwrite the old backup each time it runs.",[14,29158,29159],{},"Adding a timestamp helps because it:",[309,29161,29162,29165,29168],{},[24,29163,29164],{},"prevents old backups from being overwritten",[24,29166,29167],{},"makes each backup easier to identify",[24,29169,29170],{},"creates a more useful real backup system",[14,29172,4259],{},[42,29174,29176],{"className":44,"code":29175,"language":46,"meta":47,"style":47},"from pathlib import Path\nfrom datetime import datetime\nimport shutil\n\nsource = Path(\"notes.txt\")\n\ntimestamp = datetime.now().strftime(\"%Y-%m-%d_%H-%M-%S\")\nbackup = Path(f\"notes_backup_{timestamp}.txt\")\n\nif source.exists():\n    shutil.copy(source, backup)\n    print(\"Backup created:\", backup)\nelse:\n    print(\"Source file not found\")\n",[49,29177,29178,29188,29200,29206,29210,29228,29232,29269,29296,29300,29312,29330,29348,29354],{"__ignoreMap":47},[52,29179,29180,29182,29184,29186],{"class":54,"line":55},[52,29181,25329],{"class":58},[52,29183,28537],{"class":62},[52,29185,59],{"class":58},[52,29187,28542],{"class":62},[52,29189,29190,29192,29195,29197],{"class":54,"line":66},[52,29191,25329],{"class":58},[52,29193,29194],{"class":62}," datetime ",[52,29196,59],{"class":58},[52,29198,29199],{"class":62}," datetime\n",[52,29201,29202,29204],{"class":54,"line":73},[52,29203,59],{"class":58},[52,29205,28549],{"class":62},[52,29207,29208],{"class":54,"line":94},[52,29209,70],{"emptyLinePlaceholder":69},[52,29211,29212,29214,29216,29218,29220,29222,29224,29226],{"class":54,"line":106},[52,29213,28558],{"class":62},[52,29215,80],{"class":79},[52,29217,28563],{"class":189},[52,29219,193],{"class":102},[52,29221,115],{"class":83},[52,29223,28570],{"class":87},[52,29225,115],{"class":83},[52,29227,211],{"class":102},[52,29229,29230],{"class":54,"line":128},[52,29231,70],{"emptyLinePlaceholder":69},[52,29233,29234,29237,29239,29242,29244,29247,29249,29252,29254,29256,29259,29262,29265,29267],{"class":54,"line":148},[52,29235,29236],{"class":62},"timestamp ",[52,29238,80],{"class":79},[52,29240,29241],{"class":62}," datetime",[52,29243,186],{"class":102},[52,29245,29246],{"class":189},"now",[52,29248,18679],{"class":102},[52,29250,29251],{"class":189},"strftime",[52,29253,193],{"class":102},[52,29255,115],{"class":83},[52,29257,29258],{"class":87},"%Y-%m-",[52,29260,29261],{"class":121},"%d",[52,29263,29264],{"class":87},"_%H-%M-%S",[52,29266,115],{"class":83},[52,29268,211],{"class":102},[52,29270,29271,29273,29275,29277,29279,29281,29284,29286,29289,29291,29294],{"class":54,"line":164},[52,29272,28579],{"class":62},[52,29274,80],{"class":79},[52,29276,28563],{"class":189},[52,29278,193],{"class":102},[52,29280,1476],{"class":1475},[52,29282,29283],{"class":87},"\"notes_backup_",[52,29285,1482],{"class":121},[52,29287,29288],{"class":189},"timestamp",[52,29290,1488],{"class":121},[52,29292,29293],{"class":87},".txt\"",[52,29295,211],{"class":102},[52,29297,29298],{"class":54,"line":170},[52,29299,70],{"emptyLinePlaceholder":69},[52,29301,29302,29304,29306,29308,29310],{"class":54,"line":175},[52,29303,1313],{"class":58},[52,29305,28605],{"class":62},[52,29307,186],{"class":102},[52,29309,15887],{"class":189},[52,29311,8146],{"class":102},[52,29313,29314,29316,29318,29320,29322,29324,29326,29328],{"class":54,"line":214},[52,29315,28616],{"class":62},[52,29317,186],{"class":102},[52,29319,28621],{"class":189},[52,29321,193],{"class":102},[52,29323,28626],{"class":189},[52,29325,199],{"class":102},[52,29327,28631],{"class":189},[52,29329,211],{"class":102},[52,29331,29332,29334,29336,29338,29340,29342,29344,29346],{"class":54,"line":233},[52,29333,1599],{"class":241},[52,29335,193],{"class":102},[52,29337,115],{"class":83},[52,29339,28644],{"class":87},[52,29341,115],{"class":83},[52,29343,199],{"class":102},[52,29345,28631],{"class":189},[52,29347,211],{"class":102},[52,29349,29350,29352],{"class":54,"line":238},[52,29351,1591],{"class":58},[52,29353,1330],{"class":102},[52,29355,29356,29358,29360,29362,29364,29366],{"class":54,"line":272},[52,29357,1599],{"class":241},[52,29359,193],{"class":102},[52,29361,115],{"class":83},[52,29363,28669],{"class":87},[52,29365,115],{"class":83},[52,29367,211],{"class":102},[14,29369,29370],{},"If you run this script multiple times, each backup file gets a different name.",[37,29372,29374],{"id":29373},"useful-improvement-back-up-to-another-folder","Useful improvement: back up to another folder",[14,29376,29377],{},"You may want to keep backups in a dedicated folder instead of the same place as the original file.",[14,29379,4259],{},[42,29381,29383],{"className":44,"code":29382,"language":46,"meta":47,"style":47},"from pathlib import Path\nimport shutil\n\nsource = Path(\"notes.txt\")\nbackup_folder = Path(\"backups\")\nbackup = backup_folder \u002F \"notes_backup.txt\"\n\nif source.exists():\n    backup_folder.mkdir(exist_ok=True)\n    shutil.copy(source, backup)\n    print(\"Backup created:\", backup)\nelse:\n    print(\"Source file not found\")\n",[49,29384,29385,29395,29401,29405,29423,29443,29460,29464,29476,29497,29515,29533,29539],{"__ignoreMap":47},[52,29386,29387,29389,29391,29393],{"class":54,"line":55},[52,29388,25329],{"class":58},[52,29390,28537],{"class":62},[52,29392,59],{"class":58},[52,29394,28542],{"class":62},[52,29396,29397,29399],{"class":54,"line":66},[52,29398,59],{"class":58},[52,29400,28549],{"class":62},[52,29402,29403],{"class":54,"line":73},[52,29404,70],{"emptyLinePlaceholder":69},[52,29406,29407,29409,29411,29413,29415,29417,29419,29421],{"class":54,"line":94},[52,29408,28558],{"class":62},[52,29410,80],{"class":79},[52,29412,28563],{"class":189},[52,29414,193],{"class":102},[52,29416,115],{"class":83},[52,29418,28570],{"class":87},[52,29420,115],{"class":83},[52,29422,211],{"class":102},[52,29424,29425,29428,29430,29432,29434,29436,29439,29441],{"class":54,"line":106},[52,29426,29427],{"class":62},"backup_folder ",[52,29429,80],{"class":79},[52,29431,28563],{"class":189},[52,29433,193],{"class":102},[52,29435,115],{"class":83},[52,29437,29438],{"class":87},"backups",[52,29440,115],{"class":83},[52,29442,211],{"class":102},[52,29444,29445,29447,29449,29452,29454,29456,29458],{"class":54,"line":128},[52,29446,28579],{"class":62},[52,29448,80],{"class":79},[52,29450,29451],{"class":62}," backup_folder ",[52,29453,10537],{"class":79},[52,29455,84],{"class":83},[52,29457,28590],{"class":87},[52,29459,91],{"class":83},[52,29461,29462],{"class":54,"line":148},[52,29463,70],{"emptyLinePlaceholder":69},[52,29465,29466,29468,29470,29472,29474],{"class":54,"line":164},[52,29467,1313],{"class":58},[52,29469,28605],{"class":62},[52,29471,186],{"class":102},[52,29473,15887],{"class":189},[52,29475,8146],{"class":102},[52,29477,29478,29481,29483,29486,29488,29491,29493,29495],{"class":54,"line":170},[52,29479,29480],{"class":62},"    backup_folder",[52,29482,186],{"class":102},[52,29484,29485],{"class":189},"mkdir",[52,29487,193],{"class":102},[52,29489,29490],{"class":202},"exist_ok",[52,29492,80],{"class":79},[52,29494,6233],{"class":160},[52,29496,211],{"class":102},[52,29498,29499,29501,29503,29505,29507,29509,29511,29513],{"class":54,"line":175},[52,29500,28616],{"class":62},[52,29502,186],{"class":102},[52,29504,28621],{"class":189},[52,29506,193],{"class":102},[52,29508,28626],{"class":189},[52,29510,199],{"class":102},[52,29512,28631],{"class":189},[52,29514,211],{"class":102},[52,29516,29517,29519,29521,29523,29525,29527,29529,29531],{"class":54,"line":214},[52,29518,1599],{"class":241},[52,29520,193],{"class":102},[52,29522,115],{"class":83},[52,29524,28644],{"class":87},[52,29526,115],{"class":83},[52,29528,199],{"class":102},[52,29530,28631],{"class":189},[52,29532,211],{"class":102},[52,29534,29535,29537],{"class":54,"line":233},[52,29536,1591],{"class":58},[52,29538,1330],{"class":102},[52,29540,29541,29543,29545,29547,29549,29551],{"class":54,"line":238},[52,29542,1599],{"class":241},[52,29544,193],{"class":102},[52,29546,115],{"class":83},[52,29548,28669],{"class":87},[52,29550,115],{"class":83},[52,29552,211],{"class":102},[639,29554,29556],{"id":29555},"what-changed-here","What changed here?",[42,29558,29560],{"className":44,"code":29559,"language":46,"meta":47,"style":47},"backup_folder = Path(\"backups\")\nbackup = backup_folder \u002F \"notes_backup.txt\"\n",[49,29561,29562,29580],{"__ignoreMap":47},[52,29563,29564,29566,29568,29570,29572,29574,29576,29578],{"class":54,"line":55},[52,29565,29427],{"class":62},[52,29567,80],{"class":79},[52,29569,28563],{"class":189},[52,29571,193],{"class":102},[52,29573,115],{"class":83},[52,29575,29438],{"class":87},[52,29577,115],{"class":83},[52,29579,211],{"class":102},[52,29581,29582,29584,29586,29588,29590,29592,29594],{"class":54,"line":66},[52,29583,28579],{"class":62},[52,29585,80],{"class":79},[52,29587,29451],{"class":62},[52,29589,10537],{"class":79},[52,29591,84],{"class":83},[52,29593,28590],{"class":87},[52,29595,91],{"class":83},[14,29597,29598,29599,186],{},"This builds a path inside a folder called ",[49,29600,29438],{},[42,29602,29604],{"className":44,"code":29603,"language":46,"meta":47,"style":47},"backup_folder.mkdir(exist_ok=True)\n",[49,29605,29606],{"__ignoreMap":47},[52,29607,29608,29611,29613,29615,29617,29619,29621,29623],{"class":54,"line":55},[52,29609,29610],{"class":62},"backup_folder",[52,29612,186],{"class":102},[52,29614,29485],{"class":189},[52,29616,193],{"class":102},[52,29618,29490],{"class":202},[52,29620,80],{"class":79},[52,29622,6233],{"class":160},[52,29624,211],{"class":102},[14,29626,29627],{},"This creates the folder if it does not already exist.",[14,29629,29630,29631,186],{},"If you want to learn more about writing files and saving output, see ",[372,29632,29634],{"href":29633},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python\u002F","how to write to a file in Python",[37,29636,29638],{"id":29637},"common-problems","Common problems",[14,29640,29641],{},"Here are some common reasons the script may not work:",[309,29643,29644,29647,29650,29653],{},[24,29645,29646],{},"The source file path is wrong",[24,29648,29649],{},"The backup folder does not exist",[24,29651,29652],{},"The script tries to copy a folder instead of a file",[24,29654,29655],{},"The user does not have permission to access the file",[14,29657,26472],{},[309,29659,29660,29663,29666,29669,29675],{},[24,29661,29662],{},"Using the wrong file path",[24,29664,29665],{},"Running the script from a different folder than expected",[24,29667,29668],{},"Trying to save to a folder that does not exist",[24,29670,10294,29671,29674],{},[49,29672,29673],{},"shutil.copy()"," on a directory path",[24,29676,29677],{},"Permission problems when reading or writing files",[14,29679,29680,29681,186],{},"If your file path is wrong, Python may raise ",[372,29682,29683],{"href":15903},"FileNotFoundError: No such file or directory",[14,29685,29686,29687,186],{},"If Python cannot read or write the file, see ",[372,29688,29689],{"href":18237},"PermissionError: Permission denied",[37,29691,1735],{"id":1734},[14,29693,29694],{},"If the script is not working, try these simple checks.",[639,29696,29698],{"id":29697},"print-the-source-path","Print the source path",[42,29700,29702],{"className":44,"code":29701,"language":46,"meta":47,"style":47},"print(source)\n",[49,29703,29704],{"__ignoreMap":47},[52,29705,29706,29708,29710,29712],{"class":54,"line":55},[52,29707,242],{"class":241},[52,29709,193],{"class":102},[52,29711,28626],{"class":189},[52,29713,211],{"class":102},[14,29715,29716],{},"This helps you confirm the file name or full path you are using.",[639,29718,29720],{"id":29719},"check-whether-python-can-find-the-file","Check whether Python can find the file",[42,29722,29724],{"className":44,"code":29723,"language":46,"meta":47,"style":47},"print(source.exists())\n",[49,29725,29726],{"__ignoreMap":47},[52,29727,29728,29730,29732,29734,29736,29738],{"class":54,"line":55},[52,29729,242],{"class":241},[52,29731,193],{"class":102},[52,29733,28626],{"class":189},[52,29735,186],{"class":102},[52,29737,15887],{"class":189},[52,29739,1816],{"class":102},[14,29741,29742,29743,29745],{},"If this prints ",[49,29744,6241],{},", Python cannot find the file at that path.",[639,29747,29749],{"id":29748},"print-the-current-working-directory","Print the current working directory",[42,29751,29753],{"className":44,"code":29752,"language":46,"meta":47,"style":47},"from pathlib import Path\nprint(Path.cwd())\n",[49,29754,29755,29765],{"__ignoreMap":47},[52,29756,29757,29759,29761,29763],{"class":54,"line":55},[52,29758,25329],{"class":58},[52,29760,28537],{"class":62},[52,29762,59],{"class":58},[52,29764,28542],{"class":62},[52,29766,29767,29769,29771,29773,29775,29778],{"class":54,"line":66},[52,29768,242],{"class":241},[52,29770,193],{"class":102},[52,29772,28920],{"class":189},[52,29774,186],{"class":102},[52,29776,29777],{"class":189},"cwd",[52,29779,1816],{"class":102},[14,29781,29782],{},"This shows the folder where the script is currently running.",[14,29784,29785,29786,29789,29790,29792],{},"This is important because ",[49,29787,29788],{},"\"notes.txt\""," means \"look for ",[49,29791,28570],{}," in the current working directory.\"",[639,29794,29796],{"id":29795},"print-the-backup-path","Print the backup path",[42,29798,29800],{"className":44,"code":29799,"language":46,"meta":47,"style":47},"print(backup)\n",[49,29801,29802],{"__ignoreMap":47},[52,29803,29804,29806,29808,29810],{"class":54,"line":55},[52,29805,242],{"class":241},[52,29807,193],{"class":102},[52,29809,28993],{"class":189},[52,29811,211],{"class":102},[14,29813,29814],{},"This helps you confirm where Python is trying to save the copied file.",[639,29816,29818],{"id":29817},"keep-the-test-simple","Keep the test simple",[14,29820,29821],{},"For your first test:",[309,29823,29824,29830,29837],{},[24,29825,29826,29827,29829],{},"put the script and ",[49,29828,28570],{}," in the same folder",[24,29831,29832,29833,759,29835],{},"use simple names like ",[49,29834,28570],{},[49,29836,28590],{},[24,29838,29839],{},"read the full error message carefully",[37,29841,10511],{"id":10510},[14,29843,29844],{},"After you understand this one-file backup script, you can improve it by:",[309,29846,29847,29850,29853,29856],{},[24,29848,29849],{},"backing up multiple files in a loop",[24,29851,29852],{},"backing up every file in a folder",[24,29854,29855],{},"creating dated backup names automatically",[24,29857,29858,29859],{},"handling errors with ",[49,29860,3603],{},[14,29862,29863,29864,29868],{},"You may also want to learn ",[372,29865,29867],{"href":29866},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","how to read a file in Python"," if you are building larger file tools.",[37,29870,1942],{"id":1941},[639,29872,29874],{"id":29873},"what-is-the-easiest-way-to-back-up-a-file-in-python","What is the easiest way to back up a file in Python?",[14,29876,3650,29877,29879],{},[49,29878,29673],{}," with a source path and a backup path.",[639,29881,29883],{"id":29882},"does-this-script-copy-folders-too","Does this script copy folders too?",[14,29885,29886,29887,186],{},"No. This basic example is for a single file. Folder backups need a different approach, such as ",[49,29888,29889],{},"shutil.copytree()",[639,29891,29893],{"id":29892},"how-do-i-avoid-overwriting-an-old-backup","How do I avoid overwriting an old backup?",[14,29895,29896],{},"Add a timestamp to the backup file name so each backup is unique.",[639,29898,29900],{"id":29899},"why-does-python-say-the-file-does-not-exist","Why does Python say the file does not exist?",[14,29902,29903],{},"The path may be wrong, or the script may be running from a different working directory.",[639,29905,29907],{"id":29906},"should-i-use-os-or-pathlib-for-file-paths","Should I use os or pathlib for file paths?",[14,29909,5339,29910,29912],{},[49,29911,28924],{}," is often easier to read and use.",[37,29914,2005],{"id":2004},[309,29916,29917,29922,29927,29932,29936,29940],{},[24,29918,29919],{},[372,29920,29921],{"href":29866},"How to read a file in Python",[24,29923,29924],{},[372,29925,29926],{"href":29633},"How to write to a file in Python",[24,29928,29929],{},[372,29930,29931],{"href":28722},"Working with file paths in Python",[24,29933,29934],{},[372,29935,14985],{"href":14984},[24,29937,29938],{},[372,29939,16522],{"href":15907},[24,29941,29942],{},[372,29943,29945],{"href":29944},"\u002Fstandard-library\u002Fpython-os-module-overview\u002F","Python os module overview",[2034,29947,29948],{},"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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":29950},[29951,29952,29953,29954,29955,29962,29963,29964,29967,29968,29975,29976,29983],{"id":39,"depth":66,"text":40},{"id":28679,"depth":66,"text":28680},{"id":28697,"depth":66,"text":28698},{"id":28726,"depth":66,"text":28727},{"id":28753,"depth":66,"text":28754,"children":29956},[29957,29958,29959,29960,29961],{"id":28891,"depth":73,"text":28892},{"id":28939,"depth":73,"text":28940},{"id":29005,"depth":73,"text":29006},{"id":29056,"depth":73,"text":29057},{"id":29096,"depth":73,"text":29097},{"id":26125,"depth":66,"text":26126},{"id":29152,"depth":66,"text":29153},{"id":29373,"depth":66,"text":29374,"children":29965},[29966],{"id":29555,"depth":73,"text":29556},{"id":29637,"depth":66,"text":29638},{"id":1734,"depth":66,"text":1735,"children":29969},[29970,29971,29972,29973,29974],{"id":29697,"depth":73,"text":29698},{"id":29719,"depth":73,"text":29720},{"id":29748,"depth":73,"text":29749},{"id":29795,"depth":73,"text":29796},{"id":29817,"depth":73,"text":29818},{"id":10510,"depth":66,"text":10511},{"id":1941,"depth":66,"text":1942,"children":29977},[29978,29979,29980,29981,29982],{"id":29873,"depth":73,"text":29874},{"id":29882,"depth":73,"text":29883},{"id":29892,"depth":73,"text":29893},{"id":29899,"depth":73,"text":29900},{"id":29906,"depth":73,"text":29907},{"id":2004,"depth":66,"text":2005},"Master python file backup script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-file-backup-script-example",{"title":28495,"description":29984},"examples\u002Fpython-file-backup-script-example","JE7BOmKS22mm6AGEH4uULeeMP3-Pr6_HusgxFD-d3bo",{"id":29991,"title":29992,"body":29993,"description":32032,"extension":2075,"meta":32033,"navigation":69,"path":32034,"seo":32035,"stem":32036,"__hash__":32037},"content\u002Fexamples\u002Fpython-file-organizer-script-example.md","Python File Organizer Script Example",{"type":7,"value":29994,"toc":31999},[29995,29998,30001,30004,30006,30363,30372,30374,30377,30397,30403,30405,30407,30423,30430,30432,30434,30758,30761,30810,30817,30819,30825,30848,30853,30856,30880,30886,30903,30906,30909,30915,30939,30942,30945,30962,30968,30974,31006,31009,31020,31035,31041,31065,31068,31078,31084,31123,31126,31129,31131,31134,31161,31164,31168,31171,31185,31188,31563,31565,31571,31575,31578,31581,31595,31605,31607,31610,31615,31618,31621,31629,31632,31666,31670,31676,31679,31681,31689,31694,31698,31706,31709,31728,31734,31738,31741,31743,31756,31759,31763,31766,31772,31776,31779,31854,31857,31874,31877,31894,31896,31900,31903,31907,31921,31925,31930,31934,31937,31941,31948,31950,31989,31996],[10,29996,29992],{"id":29997},"python-file-organizer-script-example",[14,29999,30000],{},"This beginner-friendly example shows how to organize files in a folder by moving them into subfolders based on file type.",[14,30002,30003],{},"The goal of this page is to help you understand one practical Python script. It is not a full guide to every file-handling function in Python.",[37,30005,40],{"id":39},[42,30007,30009],{"className":44,"code":30008,"language":46,"meta":47,"style":47},"from pathlib import Path\nimport shutil\n\nsource_folder = Path(\"downloads\")\n\nfolders = {\n    \".jpg\": \"images\",\n    \".png\": \"images\",\n    \".gif\": \"images\",\n    \".txt\": \"text_files\",\n    \".pdf\": \"pdf_files\",\n    \".csv\": \"csv_files\",\n}\n\nfor item in source_folder.iterdir():\n    if item.is_file():\n        extension = item.suffix.lower()\n        folder_name = folders.get(extension, \"other\")\n        destination_folder = source_folder \u002F folder_name\n        destination_folder.mkdir(exist_ok=True)\n        shutil.move(str(item), str(destination_folder \u002F item.name))\n\nprint(\"Files organized.\")\n",[49,30010,30011,30021,30027,30031,30051,30055,30064,30082,30100,30119,30139,30158,30178,30182,30186,30205,30219,30240,30270,30285,30304,30344,30348],{"__ignoreMap":47},[52,30012,30013,30015,30017,30019],{"class":54,"line":55},[52,30014,25329],{"class":58},[52,30016,28537],{"class":62},[52,30018,59],{"class":58},[52,30020,28542],{"class":62},[52,30022,30023,30025],{"class":54,"line":66},[52,30024,59],{"class":58},[52,30026,28549],{"class":62},[52,30028,30029],{"class":54,"line":73},[52,30030,70],{"emptyLinePlaceholder":69},[52,30032,30033,30036,30038,30040,30042,30044,30047,30049],{"class":54,"line":94},[52,30034,30035],{"class":62},"source_folder ",[52,30037,80],{"class":79},[52,30039,28563],{"class":189},[52,30041,193],{"class":102},[52,30043,115],{"class":83},[52,30045,30046],{"class":87},"downloads",[52,30048,115],{"class":83},[52,30050,211],{"class":102},[52,30052,30053],{"class":54,"line":106},[52,30054,70],{"emptyLinePlaceholder":69},[52,30056,30057,30060,30062],{"class":54,"line":128},[52,30058,30059],{"class":62},"folders ",[52,30061,80],{"class":79},[52,30063,103],{"class":102},[52,30065,30066,30068,30070,30072,30074,30076,30078,30080],{"class":54,"line":148},[52,30067,109],{"class":83},[52,30069,25085],{"class":87},[52,30071,115],{"class":83},[52,30073,118],{"class":102},[52,30075,84],{"class":83},[52,30077,24335],{"class":87},[52,30079,115],{"class":83},[52,30081,125],{"class":102},[52,30083,30084,30086,30088,30090,30092,30094,30096,30098],{"class":54,"line":164},[52,30085,109],{"class":83},[52,30087,25088],{"class":87},[52,30089,115],{"class":83},[52,30091,118],{"class":102},[52,30093,84],{"class":83},[52,30095,24335],{"class":87},[52,30097,115],{"class":83},[52,30099,125],{"class":102},[52,30101,30102,30104,30107,30109,30111,30113,30115,30117],{"class":54,"line":170},[52,30103,109],{"class":83},[52,30105,30106],{"class":87},".gif",[52,30108,115],{"class":83},[52,30110,118],{"class":102},[52,30112,84],{"class":83},[52,30114,24335],{"class":87},[52,30116,115],{"class":83},[52,30118,125],{"class":102},[52,30120,30121,30123,30126,30128,30130,30132,30135,30137],{"class":54,"line":175},[52,30122,109],{"class":83},[52,30124,30125],{"class":87},".txt",[52,30127,115],{"class":83},[52,30129,118],{"class":102},[52,30131,84],{"class":83},[52,30133,30134],{"class":87},"text_files",[52,30136,115],{"class":83},[52,30138,125],{"class":102},[52,30140,30141,30143,30145,30147,30149,30151,30154,30156],{"class":54,"line":214},[52,30142,109],{"class":83},[52,30144,25094],{"class":87},[52,30146,115],{"class":83},[52,30148,118],{"class":102},[52,30150,84],{"class":83},[52,30152,30153],{"class":87},"pdf_files",[52,30155,115],{"class":83},[52,30157,125],{"class":102},[52,30159,30160,30162,30165,30167,30169,30171,30174,30176],{"class":54,"line":233},[52,30161,109],{"class":83},[52,30163,30164],{"class":87},".csv",[52,30166,115],{"class":83},[52,30168,118],{"class":102},[52,30170,84],{"class":83},[52,30172,30173],{"class":87},"csv_files",[52,30175,115],{"class":83},[52,30177,125],{"class":102},[52,30179,30180],{"class":54,"line":238},[52,30181,167],{"class":102},[52,30183,30184],{"class":54,"line":272},[52,30185,70],{"emptyLinePlaceholder":69},[52,30187,30188,30190,30193,30195,30198,30200,30203],{"class":54,"line":1348},[52,30189,12265],{"class":58},[52,30191,30192],{"class":62}," item ",[52,30194,11213],{"class":58},[52,30196,30197],{"class":62}," source_folder",[52,30199,186],{"class":102},[52,30201,30202],{"class":189},"iterdir",[52,30204,8146],{"class":102},[52,30206,30207,30209,30212,30214,30217],{"class":54,"line":1376},[52,30208,1438],{"class":58},[52,30210,30211],{"class":62}," item",[52,30213,186],{"class":102},[52,30215,30216],{"class":189},"is_file",[52,30218,8146],{"class":102},[52,30220,30221,30224,30226,30228,30230,30233,30235,30238],{"class":54,"line":1381},[52,30222,30223],{"class":62},"        extension ",[52,30225,80],{"class":79},[52,30227,30211],{"class":62},[52,30229,186],{"class":102},[52,30231,30232],{"class":1320},"suffix",[52,30234,186],{"class":102},[52,30236,30237],{"class":189},"lower",[52,30239,230],{"class":102},[52,30241,30242,30245,30247,30250,30252,30254,30256,30259,30261,30263,30266,30268],{"class":54,"line":1406},[52,30243,30244],{"class":62},"        folder_name ",[52,30246,80],{"class":79},[52,30248,30249],{"class":62}," folders",[52,30251,186],{"class":102},[52,30253,190],{"class":189},[52,30255,193],{"class":102},[52,30257,30258],{"class":189},"extension",[52,30260,199],{"class":102},[52,30262,84],{"class":83},[52,30264,30265],{"class":87},"other",[52,30267,115],{"class":83},[52,30269,211],{"class":102},[52,30271,30272,30275,30277,30280,30282],{"class":54,"line":1430},[52,30273,30274],{"class":62},"        destination_folder ",[52,30276,80],{"class":79},[52,30278,30279],{"class":62}," source_folder ",[52,30281,10537],{"class":79},[52,30283,30284],{"class":62}," folder_name\n",[52,30286,30287,30290,30292,30294,30296,30298,30300,30302],{"class":54,"line":1435},[52,30288,30289],{"class":62},"        destination_folder",[52,30291,186],{"class":102},[52,30293,29485],{"class":189},[52,30295,193],{"class":102},[52,30297,29490],{"class":202},[52,30299,80],{"class":79},[52,30301,6233],{"class":160},[52,30303,211],{"class":102},[52,30305,30306,30309,30311,30314,30316,30318,30320,30323,30326,30329,30331,30334,30336,30338,30340,30342],{"class":54,"line":1467},[52,30307,30308],{"class":62},"        shutil",[52,30310,186],{"class":102},[52,30312,30313],{"class":189},"move",[52,30315,193],{"class":102},[52,30317,19523],{"class":4733},[52,30319,193],{"class":102},[52,30321,30322],{"class":189},"item",[52,30324,30325],{"class":102},"),",[52,30327,30328],{"class":4733}," str",[52,30330,193],{"class":102},[52,30332,30333],{"class":189},"destination_folder ",[52,30335,10537],{"class":79},[52,30337,30211],{"class":189},[52,30339,186],{"class":102},[52,30341,2147],{"class":1320},[52,30343,8886],{"class":102},[52,30345,30346],{"class":54,"line":1495},[52,30347,70],{"emptyLinePlaceholder":69},[52,30349,30350,30352,30354,30356,30359,30361],{"class":54,"line":1531},[52,30351,242],{"class":241},[52,30353,193],{"class":102},[52,30355,115],{"class":83},[52,30357,30358],{"class":87},"Files organized.",[52,30360,115],{"class":83},[52,30362,211],{"class":102},[14,30364,30365,30366,30368,30369,30371],{},"This example organizes files inside a folder named ",[49,30367,30046],{},". It groups files by extension and puts unknown file types into an ",[49,30370,30265],{}," folder.",[37,30373,323],{"id":322},[14,30375,30376],{},"This script:",[309,30378,30379,30382,30385,30394],{},[24,30380,30381],{},"Shows a complete beginner-friendly script",[24,30383,30384],{},"Moves files into folders based on file extension",[24,30386,30387,30388,759,30391],{},"Uses simple rules like ",[49,30389,30390],{},".jpg -> images",[49,30392,30393],{},".pdf -> pdf_files",[24,30395,30396],{},"Helps you understand a real automation task",[14,30398,30399,30400,186],{},"If you are new to folders and paths in Python, it may also help to read ",[372,30401,28723],{"href":30402},"\u002Flearn\u002Fworking-with-file-paths-in-python",[37,30404,347],{"id":346},[14,30406,23910],{},[309,30408,30409,30411,30414,30420],{},[24,30410,3905],{},[24,30412,30413],{},"A folder with files to organize",[24,30415,30416,30417,30419],{},"Basic understanding of variables, loops, and ",[49,30418,1313],{}," statements",[24,30421,30422],{},"A test folder to practice with first",[14,30424,30425,30426,30429],{},"Using a test folder is important. This script ",[5503,30427,30428],{},"moves"," files, so your original folder contents will change.",[37,30431,27411],{"id":27410},[14,30433,25644],{},[42,30435,30436],{"className":44,"code":30008,"language":46,"meta":47,"style":47},[49,30437,30438,30448,30454,30458,30476,30480,30488,30506,30524,30542,30560,30578,30596,30600,30604,30620,30632,30650,30676,30688,30706,30740,30744],{"__ignoreMap":47},[52,30439,30440,30442,30444,30446],{"class":54,"line":55},[52,30441,25329],{"class":58},[52,30443,28537],{"class":62},[52,30445,59],{"class":58},[52,30447,28542],{"class":62},[52,30449,30450,30452],{"class":54,"line":66},[52,30451,59],{"class":58},[52,30453,28549],{"class":62},[52,30455,30456],{"class":54,"line":73},[52,30457,70],{"emptyLinePlaceholder":69},[52,30459,30460,30462,30464,30466,30468,30470,30472,30474],{"class":54,"line":94},[52,30461,30035],{"class":62},[52,30463,80],{"class":79},[52,30465,28563],{"class":189},[52,30467,193],{"class":102},[52,30469,115],{"class":83},[52,30471,30046],{"class":87},[52,30473,115],{"class":83},[52,30475,211],{"class":102},[52,30477,30478],{"class":54,"line":106},[52,30479,70],{"emptyLinePlaceholder":69},[52,30481,30482,30484,30486],{"class":54,"line":128},[52,30483,30059],{"class":62},[52,30485,80],{"class":79},[52,30487,103],{"class":102},[52,30489,30490,30492,30494,30496,30498,30500,30502,30504],{"class":54,"line":148},[52,30491,109],{"class":83},[52,30493,25085],{"class":87},[52,30495,115],{"class":83},[52,30497,118],{"class":102},[52,30499,84],{"class":83},[52,30501,24335],{"class":87},[52,30503,115],{"class":83},[52,30505,125],{"class":102},[52,30507,30508,30510,30512,30514,30516,30518,30520,30522],{"class":54,"line":164},[52,30509,109],{"class":83},[52,30511,25088],{"class":87},[52,30513,115],{"class":83},[52,30515,118],{"class":102},[52,30517,84],{"class":83},[52,30519,24335],{"class":87},[52,30521,115],{"class":83},[52,30523,125],{"class":102},[52,30525,30526,30528,30530,30532,30534,30536,30538,30540],{"class":54,"line":170},[52,30527,109],{"class":83},[52,30529,30106],{"class":87},[52,30531,115],{"class":83},[52,30533,118],{"class":102},[52,30535,84],{"class":83},[52,30537,24335],{"class":87},[52,30539,115],{"class":83},[52,30541,125],{"class":102},[52,30543,30544,30546,30548,30550,30552,30554,30556,30558],{"class":54,"line":175},[52,30545,109],{"class":83},[52,30547,30125],{"class":87},[52,30549,115],{"class":83},[52,30551,118],{"class":102},[52,30553,84],{"class":83},[52,30555,30134],{"class":87},[52,30557,115],{"class":83},[52,30559,125],{"class":102},[52,30561,30562,30564,30566,30568,30570,30572,30574,30576],{"class":54,"line":214},[52,30563,109],{"class":83},[52,30565,25094],{"class":87},[52,30567,115],{"class":83},[52,30569,118],{"class":102},[52,30571,84],{"class":83},[52,30573,30153],{"class":87},[52,30575,115],{"class":83},[52,30577,125],{"class":102},[52,30579,30580,30582,30584,30586,30588,30590,30592,30594],{"class":54,"line":233},[52,30581,109],{"class":83},[52,30583,30164],{"class":87},[52,30585,115],{"class":83},[52,30587,118],{"class":102},[52,30589,84],{"class":83},[52,30591,30173],{"class":87},[52,30593,115],{"class":83},[52,30595,125],{"class":102},[52,30597,30598],{"class":54,"line":238},[52,30599,167],{"class":102},[52,30601,30602],{"class":54,"line":272},[52,30603,70],{"emptyLinePlaceholder":69},[52,30605,30606,30608,30610,30612,30614,30616,30618],{"class":54,"line":1348},[52,30607,12265],{"class":58},[52,30609,30192],{"class":62},[52,30611,11213],{"class":58},[52,30613,30197],{"class":62},[52,30615,186],{"class":102},[52,30617,30202],{"class":189},[52,30619,8146],{"class":102},[52,30621,30622,30624,30626,30628,30630],{"class":54,"line":1376},[52,30623,1438],{"class":58},[52,30625,30211],{"class":62},[52,30627,186],{"class":102},[52,30629,30216],{"class":189},[52,30631,8146],{"class":102},[52,30633,30634,30636,30638,30640,30642,30644,30646,30648],{"class":54,"line":1381},[52,30635,30223],{"class":62},[52,30637,80],{"class":79},[52,30639,30211],{"class":62},[52,30641,186],{"class":102},[52,30643,30232],{"class":1320},[52,30645,186],{"class":102},[52,30647,30237],{"class":189},[52,30649,230],{"class":102},[52,30651,30652,30654,30656,30658,30660,30662,30664,30666,30668,30670,30672,30674],{"class":54,"line":1406},[52,30653,30244],{"class":62},[52,30655,80],{"class":79},[52,30657,30249],{"class":62},[52,30659,186],{"class":102},[52,30661,190],{"class":189},[52,30663,193],{"class":102},[52,30665,30258],{"class":189},[52,30667,199],{"class":102},[52,30669,84],{"class":83},[52,30671,30265],{"class":87},[52,30673,115],{"class":83},[52,30675,211],{"class":102},[52,30677,30678,30680,30682,30684,30686],{"class":54,"line":1430},[52,30679,30274],{"class":62},[52,30681,80],{"class":79},[52,30683,30279],{"class":62},[52,30685,10537],{"class":79},[52,30687,30284],{"class":62},[52,30689,30690,30692,30694,30696,30698,30700,30702,30704],{"class":54,"line":1435},[52,30691,30289],{"class":62},[52,30693,186],{"class":102},[52,30695,29485],{"class":189},[52,30697,193],{"class":102},[52,30699,29490],{"class":202},[52,30701,80],{"class":79},[52,30703,6233],{"class":160},[52,30705,211],{"class":102},[52,30707,30708,30710,30712,30714,30716,30718,30720,30722,30724,30726,30728,30730,30732,30734,30736,30738],{"class":54,"line":1467},[52,30709,30308],{"class":62},[52,30711,186],{"class":102},[52,30713,30313],{"class":189},[52,30715,193],{"class":102},[52,30717,19523],{"class":4733},[52,30719,193],{"class":102},[52,30721,30322],{"class":189},[52,30723,30325],{"class":102},[52,30725,30328],{"class":4733},[52,30727,193],{"class":102},[52,30729,30333],{"class":189},[52,30731,10537],{"class":79},[52,30733,30211],{"class":189},[52,30735,186],{"class":102},[52,30737,2147],{"class":1320},[52,30739,8886],{"class":102},[52,30741,30742],{"class":54,"line":1495},[52,30743,70],{"emptyLinePlaceholder":69},[52,30745,30746,30748,30750,30752,30754,30756],{"class":54,"line":1531},[52,30747,242],{"class":241},[52,30749,193],{"class":102},[52,30751,115],{"class":83},[52,30753,30358],{"class":87},[52,30755,115],{"class":83},[52,30757,211],{"class":102},[14,30759,30760],{},"Step by step, it does this:",[21,30762,30763,30771,30776,30783,30786,30789,30792,30798,30804,30807],{},[24,30764,30765,30766,28921,30768,30770],{},"Imports ",[49,30767,28920],{},[49,30769,28924],{}," to work with file paths",[24,30772,30765,30773,30775],{},[49,30774,28930],{}," so it can move files",[24,30777,30778,30779,30782],{},"Sets ",[49,30780,30781],{},"source_folder"," to the folder you want to organize",[24,30784,30785],{},"Creates a dictionary that maps file extensions to folder names",[24,30787,30788],{},"Loops through each item in the source folder",[24,30790,30791],{},"Checks that the item is a file, not a folder",[24,30793,30794,30795],{},"Reads the file extension with ",[49,30796,30797],{},"suffix.lower()",[24,30799,30800,30801],{},"Chooses a destination folder with ",[49,30802,30803],{},"folders.get()",[24,30805,30806],{},"Creates that folder if it does not already exist",[24,30808,30809],{},"Moves the file into the correct folder",[14,30811,30812,30813,186],{},"If you want to learn more about listing folder contents, see ",[372,30814,30816],{"href":30815},"\u002Fhow-to\u002Fhow-to-list-files-in-a-directory-in-python","how to list files in a directory in Python",[37,30818,25927],{"id":25926},[639,30820,30822],{"id":30821},"source_folder-pathdownloads",[49,30823,30824],{},"source_folder = Path(\"downloads\")",[42,30826,30828],{"className":44,"code":30827,"language":46,"meta":47,"style":47},"source_folder = Path(\"downloads\")\n",[49,30829,30830],{"__ignoreMap":47},[52,30831,30832,30834,30836,30838,30840,30842,30844,30846],{"class":54,"line":55},[52,30833,30035],{"class":62},[52,30835,80],{"class":79},[52,30837,28563],{"class":189},[52,30839,193],{"class":102},[52,30841,115],{"class":83},[52,30843,30046],{"class":87},[52,30845,115],{"class":83},[52,30847,211],{"class":102},[14,30849,30850,30851,186],{},"This creates a path object for the folder named ",[49,30852,30046],{},[14,30854,30855],{},"If that folder is not in the same location as your script, you may need a full path instead, such as:",[42,30857,30859],{"className":44,"code":30858,"language":46,"meta":47,"style":47},"source_folder = Path(\"\u002FUsers\u002Fyourname\u002FDownloads\")\n",[49,30860,30861],{"__ignoreMap":47},[52,30862,30863,30865,30867,30869,30871,30873,30876,30878],{"class":54,"line":55},[52,30864,30035],{"class":62},[52,30866,80],{"class":79},[52,30868,28563],{"class":189},[52,30870,193],{"class":102},[52,30872,115],{"class":83},[52,30874,30875],{"class":87},"\u002FUsers\u002Fyourname\u002FDownloads",[52,30877,115],{"class":83},[52,30879,211],{"class":102},[639,30881,30883],{"id":30882},"if-itemis_file",[49,30884,30885],{},"if item.is_file():",[42,30887,30889],{"className":44,"code":30888,"language":46,"meta":47,"style":47},"if item.is_file():\n",[49,30890,30891],{"__ignoreMap":47},[52,30892,30893,30895,30897,30899,30901],{"class":54,"line":55},[52,30894,1313],{"class":58},[52,30896,30211],{"class":62},[52,30898,186],{"class":102},[52,30900,30216],{"class":189},[52,30902,8146],{"class":102},[14,30904,30905],{},"This checks whether the current item is a file.",[14,30907,30908],{},"It skips subfolders. That matters because this script is only meant to organize files directly inside the source folder.",[639,30910,30912],{"id":30911},"itemsuffixlower",[49,30913,30914],{},"item.suffix.lower()",[42,30916,30918],{"className":44,"code":30917,"language":46,"meta":47,"style":47},"extension = item.suffix.lower()\n",[49,30919,30920],{"__ignoreMap":47},[52,30921,30922,30925,30927,30929,30931,30933,30935,30937],{"class":54,"line":55},[52,30923,30924],{"class":62},"extension ",[52,30926,80],{"class":79},[52,30928,30211],{"class":62},[52,30930,186],{"class":102},[52,30932,30232],{"class":1320},[52,30934,186],{"class":102},[52,30936,30237],{"class":189},[52,30938,230],{"class":102},[14,30940,30941],{},"This gets the file extension and changes it to lowercase.",[14,30943,30944],{},"That helps with files like:",[309,30946,30947,30952,30957],{},[24,30948,30949],{},[49,30950,30951],{},"photo.jpg",[24,30953,30954],{},[49,30955,30956],{},"photo.JPG",[24,30958,30959],{},[49,30960,30961],{},"photo.Jpg",[14,30963,30964,30965,30967],{},"All of these become ",[49,30966,25085],{},", so they match the same rule.",[639,30969,30971],{"id":30970},"foldersgetextension-other",[49,30972,30973],{},"folders.get(extension, \"other\")",[42,30975,30977],{"className":44,"code":30976,"language":46,"meta":47,"style":47},"folder_name = folders.get(extension, \"other\")\n",[49,30978,30979],{"__ignoreMap":47},[52,30980,30981,30984,30986,30988,30990,30992,30994,30996,30998,31000,31002,31004],{"class":54,"line":55},[52,30982,30983],{"class":62},"folder_name ",[52,30985,80],{"class":79},[52,30987,30249],{"class":62},[52,30989,186],{"class":102},[52,30991,190],{"class":189},[52,30993,193],{"class":102},[52,30995,30258],{"class":189},[52,30997,199],{"class":102},[52,30999,84],{"class":83},[52,31001,30265],{"class":87},[52,31003,115],{"class":83},[52,31005,211],{"class":102},[14,31007,31008],{},"This looks up the extension in the dictionary.",[309,31010,31011,31014],{},[24,31012,31013],{},"If the extension exists, it uses the matching folder name",[24,31015,31016,31017],{},"If the extension is not found, it uses ",[49,31018,31019],{},"\"other\"",[14,31021,31022,31023,31026,31027,31029,31030,31026,31033,186],{},"So a file like ",[49,31024,31025],{},"report.pdf"," goes to ",[49,31028,30153],{},", but a file like ",[49,31031,31032],{},"archive.zip",[49,31034,30265],{},[639,31036,31038],{"id":31037},"destination_foldermkdirexist_oktrue",[49,31039,31040],{},"destination_folder.mkdir(exist_ok=True)",[42,31042,31044],{"className":44,"code":31043,"language":46,"meta":47,"style":47},"destination_folder.mkdir(exist_ok=True)\n",[49,31045,31046],{"__ignoreMap":47},[52,31047,31048,31051,31053,31055,31057,31059,31061,31063],{"class":54,"line":55},[52,31049,31050],{"class":62},"destination_folder",[52,31052,186],{"class":102},[52,31054,29485],{"class":189},[52,31056,193],{"class":102},[52,31058,29490],{"class":202},[52,31060,80],{"class":79},[52,31062,6233],{"class":160},[52,31064,211],{"class":102},[14,31066,31067],{},"This creates the destination folder if needed.",[14,31069,358,31070,31073,31074,186],{},[49,31071,31072],{},"exist_ok=True"," part prevents an error if the folder already exists. If you need more help creating folders, see ",[372,31075,31077],{"href":31076},"\u002Fhow-to\u002Fhow-to-create-a-directory-in-python","how to create a directory in Python",[639,31079,31081],{"id":31080},"shutilmove",[49,31082,31083],{},"shutil.move(...)",[42,31085,31087],{"className":44,"code":31086,"language":46,"meta":47,"style":47},"shutil.move(str(item), str(destination_folder \u002F item.name))\n",[49,31088,31089],{"__ignoreMap":47},[52,31090,31091,31093,31095,31097,31099,31101,31103,31105,31107,31109,31111,31113,31115,31117,31119,31121],{"class":54,"line":55},[52,31092,28930],{"class":62},[52,31094,186],{"class":102},[52,31096,30313],{"class":189},[52,31098,193],{"class":102},[52,31100,19523],{"class":4733},[52,31102,193],{"class":102},[52,31104,30322],{"class":189},[52,31106,30325],{"class":102},[52,31108,30328],{"class":4733},[52,31110,193],{"class":102},[52,31112,30333],{"class":189},[52,31114,10537],{"class":79},[52,31116,30211],{"class":189},[52,31118,186],{"class":102},[52,31120,2147],{"class":1320},[52,31122,8886],{"class":102},[14,31124,31125],{},"This moves the file to its new location.",[14,31127,31128],{},"The file is removed from the original place and placed in the destination folder.",[37,31130,26126],{"id":26125},[14,31132,31133],{},"After the script runs, your folder may look something like this:",[309,31135,31136,31141,31146,31151,31156],{},[24,31137,31138],{},[49,31139,31140],{},"downloads\u002Fimages\u002F",[24,31142,31143],{},[49,31144,31145],{},"downloads\u002Fpdf_files\u002F",[24,31147,31148],{},[49,31149,31150],{},"downloads\u002Ftext_files\u002F",[24,31152,31153],{},[49,31154,31155],{},"downloads\u002Fcsv_files\u002F",[24,31157,31158],{},[49,31159,31160],{},"downloads\u002Fother\u002F",[14,31162,31163],{},"Files are sorted into folders automatically, which makes the original folder easier to browse.",[37,31165,31167],{"id":31166},"ways-to-customize-the-script","Ways to customize the script",[14,31169,31170],{},"You can change the script in several simple ways:",[309,31172,31173,31176,31179,31182],{},[24,31174,31175],{},"Add more file extensions to the dictionary",[24,31177,31178],{},"Change folder names to fit your own system",[24,31180,31181],{},"Organize by first letter, date, or filename pattern instead of extension",[24,31183,31184],{},"Print each move so you can see what changed",[14,31186,31187],{},"For example, this version prints each file move:",[42,31189,31191],{"className":44,"code":31190,"language":46,"meta":47,"style":47},"from pathlib import Path\nimport shutil\n\nsource_folder = Path(\"downloads\")\n\nfolders = {\n    \".jpg\": \"images\",\n    \".png\": \"images\",\n    \".gif\": \"images\",\n    \".txt\": \"text_files\",\n    \".pdf\": \"pdf_files\",\n    \".csv\": \"csv_files\",\n}\n\nfor item in source_folder.iterdir():\n    if item.is_file():\n        extension = item.suffix.lower()\n        folder_name = folders.get(extension, \"other\")\n        destination_folder = source_folder \u002F folder_name\n        destination_folder.mkdir(exist_ok=True)\n\n        destination_path = destination_folder \u002F item.name\n        print(f\"Moving {item.name} -> {destination_path}\")\n        shutil.move(str(item), str(destination_path))\n\nprint(\"Files organized.\")\n",[49,31192,31193,31203,31209,31213,31231,31235,31243,31261,31279,31297,31315,31333,31351,31355,31359,31375,31387,31405,31431,31443,31461,31465,31484,31519,31545,31549],{"__ignoreMap":47},[52,31194,31195,31197,31199,31201],{"class":54,"line":55},[52,31196,25329],{"class":58},[52,31198,28537],{"class":62},[52,31200,59],{"class":58},[52,31202,28542],{"class":62},[52,31204,31205,31207],{"class":54,"line":66},[52,31206,59],{"class":58},[52,31208,28549],{"class":62},[52,31210,31211],{"class":54,"line":73},[52,31212,70],{"emptyLinePlaceholder":69},[52,31214,31215,31217,31219,31221,31223,31225,31227,31229],{"class":54,"line":94},[52,31216,30035],{"class":62},[52,31218,80],{"class":79},[52,31220,28563],{"class":189},[52,31222,193],{"class":102},[52,31224,115],{"class":83},[52,31226,30046],{"class":87},[52,31228,115],{"class":83},[52,31230,211],{"class":102},[52,31232,31233],{"class":54,"line":106},[52,31234,70],{"emptyLinePlaceholder":69},[52,31236,31237,31239,31241],{"class":54,"line":128},[52,31238,30059],{"class":62},[52,31240,80],{"class":79},[52,31242,103],{"class":102},[52,31244,31245,31247,31249,31251,31253,31255,31257,31259],{"class":54,"line":148},[52,31246,109],{"class":83},[52,31248,25085],{"class":87},[52,31250,115],{"class":83},[52,31252,118],{"class":102},[52,31254,84],{"class":83},[52,31256,24335],{"class":87},[52,31258,115],{"class":83},[52,31260,125],{"class":102},[52,31262,31263,31265,31267,31269,31271,31273,31275,31277],{"class":54,"line":164},[52,31264,109],{"class":83},[52,31266,25088],{"class":87},[52,31268,115],{"class":83},[52,31270,118],{"class":102},[52,31272,84],{"class":83},[52,31274,24335],{"class":87},[52,31276,115],{"class":83},[52,31278,125],{"class":102},[52,31280,31281,31283,31285,31287,31289,31291,31293,31295],{"class":54,"line":170},[52,31282,109],{"class":83},[52,31284,30106],{"class":87},[52,31286,115],{"class":83},[52,31288,118],{"class":102},[52,31290,84],{"class":83},[52,31292,24335],{"class":87},[52,31294,115],{"class":83},[52,31296,125],{"class":102},[52,31298,31299,31301,31303,31305,31307,31309,31311,31313],{"class":54,"line":175},[52,31300,109],{"class":83},[52,31302,30125],{"class":87},[52,31304,115],{"class":83},[52,31306,118],{"class":102},[52,31308,84],{"class":83},[52,31310,30134],{"class":87},[52,31312,115],{"class":83},[52,31314,125],{"class":102},[52,31316,31317,31319,31321,31323,31325,31327,31329,31331],{"class":54,"line":214},[52,31318,109],{"class":83},[52,31320,25094],{"class":87},[52,31322,115],{"class":83},[52,31324,118],{"class":102},[52,31326,84],{"class":83},[52,31328,30153],{"class":87},[52,31330,115],{"class":83},[52,31332,125],{"class":102},[52,31334,31335,31337,31339,31341,31343,31345,31347,31349],{"class":54,"line":233},[52,31336,109],{"class":83},[52,31338,30164],{"class":87},[52,31340,115],{"class":83},[52,31342,118],{"class":102},[52,31344,84],{"class":83},[52,31346,30173],{"class":87},[52,31348,115],{"class":83},[52,31350,125],{"class":102},[52,31352,31353],{"class":54,"line":238},[52,31354,167],{"class":102},[52,31356,31357],{"class":54,"line":272},[52,31358,70],{"emptyLinePlaceholder":69},[52,31360,31361,31363,31365,31367,31369,31371,31373],{"class":54,"line":1348},[52,31362,12265],{"class":58},[52,31364,30192],{"class":62},[52,31366,11213],{"class":58},[52,31368,30197],{"class":62},[52,31370,186],{"class":102},[52,31372,30202],{"class":189},[52,31374,8146],{"class":102},[52,31376,31377,31379,31381,31383,31385],{"class":54,"line":1376},[52,31378,1438],{"class":58},[52,31380,30211],{"class":62},[52,31382,186],{"class":102},[52,31384,30216],{"class":189},[52,31386,8146],{"class":102},[52,31388,31389,31391,31393,31395,31397,31399,31401,31403],{"class":54,"line":1381},[52,31390,30223],{"class":62},[52,31392,80],{"class":79},[52,31394,30211],{"class":62},[52,31396,186],{"class":102},[52,31398,30232],{"class":1320},[52,31400,186],{"class":102},[52,31402,30237],{"class":189},[52,31404,230],{"class":102},[52,31406,31407,31409,31411,31413,31415,31417,31419,31421,31423,31425,31427,31429],{"class":54,"line":1406},[52,31408,30244],{"class":62},[52,31410,80],{"class":79},[52,31412,30249],{"class":62},[52,31414,186],{"class":102},[52,31416,190],{"class":189},[52,31418,193],{"class":102},[52,31420,30258],{"class":189},[52,31422,199],{"class":102},[52,31424,84],{"class":83},[52,31426,30265],{"class":87},[52,31428,115],{"class":83},[52,31430,211],{"class":102},[52,31432,31433,31435,31437,31439,31441],{"class":54,"line":1430},[52,31434,30274],{"class":62},[52,31436,80],{"class":79},[52,31438,30279],{"class":62},[52,31440,10537],{"class":79},[52,31442,30284],{"class":62},[52,31444,31445,31447,31449,31451,31453,31455,31457,31459],{"class":54,"line":1435},[52,31446,30289],{"class":62},[52,31448,186],{"class":102},[52,31450,29485],{"class":189},[52,31452,193],{"class":102},[52,31454,29490],{"class":202},[52,31456,80],{"class":79},[52,31458,6233],{"class":160},[52,31460,211],{"class":102},[52,31462,31463],{"class":54,"line":1467},[52,31464,70],{"emptyLinePlaceholder":69},[52,31466,31467,31470,31472,31475,31477,31479,31481],{"class":54,"line":1495},[52,31468,31469],{"class":62},"        destination_path ",[52,31471,80],{"class":79},[52,31473,31474],{"class":62}," destination_folder ",[52,31476,10537],{"class":79},[52,31478,30211],{"class":62},[52,31480,186],{"class":102},[52,31482,31483],{"class":1320},"name\n",[52,31485,31486,31488,31490,31492,31495,31497,31499,31501,31503,31505,31508,31510,31513,31515,31517],{"class":54,"line":1531},[52,31487,1470],{"class":241},[52,31489,193],{"class":102},[52,31491,1476],{"class":1475},[52,31493,31494],{"class":87},"\"Moving ",[52,31496,1482],{"class":121},[52,31498,30322],{"class":189},[52,31500,186],{"class":102},[52,31502,2147],{"class":1320},[52,31504,1488],{"class":121},[52,31506,31507],{"class":87}," -> ",[52,31509,1482],{"class":121},[52,31511,31512],{"class":189},"destination_path",[52,31514,1488],{"class":121},[52,31516,115],{"class":87},[52,31518,211],{"class":102},[52,31520,31521,31523,31525,31527,31529,31531,31533,31535,31537,31539,31541,31543],{"class":54,"line":1564},[52,31522,30308],{"class":62},[52,31524,186],{"class":102},[52,31526,30313],{"class":189},[52,31528,193],{"class":102},[52,31530,19523],{"class":4733},[52,31532,193],{"class":102},[52,31534,30322],{"class":189},[52,31536,30325],{"class":102},[52,31538,30328],{"class":4733},[52,31540,193],{"class":102},[52,31542,31512],{"class":189},[52,31544,8886],{"class":102},[52,31546,31547],{"class":54,"line":1572},[52,31548,70],{"emptyLinePlaceholder":69},[52,31550,31551,31553,31555,31557,31559,31561],{"class":54,"line":1588},[52,31552,242],{"class":241},[52,31554,193],{"class":102},[52,31556,115],{"class":83},[52,31558,30358],{"class":87},[52,31560,115],{"class":83},[52,31562,211],{"class":102},[14,31564,11845],{},[42,31566,31569],{"className":31567,"code":31568,"language":955,"meta":47},[953],"Moving photo.JPG -> downloads\u002Fimages\u002Fphoto.JPG\nMoving notes.txt -> downloads\u002Ftext_files\u002Fnotes.txt\nMoving report.pdf -> downloads\u002Fpdf_files\u002Freport.pdf\nFiles organized.\n",[49,31570,31568],{"__ignoreMap":47},[37,31572,31574],{"id":31573},"safety-tips-for-beginners","Safety tips for beginners",[14,31576,31577],{},"Be careful when testing a file-moving script.",[14,31579,31580],{},"Good habits:",[309,31582,31583,31586,31589,31592],{},[24,31584,31585],{},"Run the script on a copy of your files first",[24,31587,31588],{},"Test with a small folder before using a large one",[24,31590,31591],{},"Check that the folder path is correct before running",[24,31593,31594],{},"Watch out for files that have the same name",[14,31596,31597,31598,31600,31601,31604],{},"If you want to make changes without moving files yet, you can temporarily replace the ",[49,31599,31083],{}," line with a ",[49,31602,31603],{},"print(...)"," line. That lets you preview what would happen.",[37,31606,29638],{"id":29637},[14,31608,31609],{},"A few problems are common with scripts like this.",[639,31611,31613],{"id":31612},"filenotfounderror",[49,31614,15764],{},[14,31616,31617],{},"This happens if the source folder does not exist.",[14,31619,31620],{},"Common causes:",[309,31622,31623,31626],{},[24,31624,31625],{},"Using the wrong folder path",[24,31627,31628],{},"Running the script from a different working directory than expected",[14,31630,31631],{},"Useful checks:",[42,31633,31635],{"className":44,"code":31634,"language":46,"meta":47,"style":47},"print(source_folder.resolve())\nprint(source_folder.exists())\n",[49,31636,31637,31652],{"__ignoreMap":47},[52,31638,31639,31641,31643,31645,31647,31650],{"class":54,"line":55},[52,31640,242],{"class":241},[52,31642,193],{"class":102},[52,31644,30781],{"class":189},[52,31646,186],{"class":102},[52,31648,31649],{"class":189},"resolve",[52,31651,1816],{"class":102},[52,31653,31654,31656,31658,31660,31662,31664],{"class":54,"line":66},[52,31655,242],{"class":241},[52,31657,193],{"class":102},[52,31659,30781],{"class":189},[52,31661,186],{"class":102},[52,31663,15887],{"class":189},[52,31665,1816],{"class":102},[14,31667,15900,31668,186],{},[372,31669,29683],{"href":25232},[639,31671,31673],{"id":31672},"permissionerror",[49,31674,31675],{},"PermissionError",[14,31677,31678],{},"This happens if Python cannot access the folder or move the files.",[14,31680,31620],{},[309,31682,31683,31686],{},[24,31684,31685],{},"Trying to move files without permission",[24,31687,31688],{},"Using a protected system folder",[14,31690,31691,31692,186],{},"If needed, see ",[372,31693,29689],{"href":25127},[639,31695,31697],{"id":31696},"files-with-uppercase-extensions","Files with uppercase extensions",[14,31699,31700,31701,10598,31704,186],{},"Some files may use extensions like ",[49,31702,31703],{},".JPG",[49,31705,25085],{},[14,31707,31708],{},"That is why the script uses:",[42,31710,31712],{"className":44,"code":31711,"language":46,"meta":47,"style":47},"item.suffix.lower()\n",[49,31713,31714],{"__ignoreMap":47},[52,31715,31716,31718,31720,31722,31724,31726],{"class":54,"line":55},[52,31717,30322],{"class":62},[52,31719,186],{"class":102},[52,31721,30232],{"class":1320},[52,31723,186],{"class":102},[52,31725,30237],{"class":189},[52,31727,230],{"class":102},[14,31729,5788,31730,31733],{},[49,31731,31732],{},".lower()",", some files would not match your dictionary rules.",[639,31735,31737],{"id":31736},"duplicate-file-names","Duplicate file names",[14,31739,31740],{},"If two files with the same name are moved into the same folder, one may overwrite the other.",[14,31742,16291],{},[309,31744,31745,31750],{},[24,31746,31747],{},[49,31748,31749],{},"downloads\u002Fphoto.jpg",[24,31751,31752,31753,31755],{},"another ",[49,31754,30951],{}," from somewhere else later",[14,31757,31758],{},"A safer version would check whether the destination file already exists before moving.",[639,31760,31762],{"id":31761},"files-with-no-extension","Files with no extension",[14,31764,31765],{},"Some files do not have an extension at all.",[14,31767,31768,31769,31771],{},"In this script, those files will go into the ",[49,31770,30265],{}," folder because they do not match any key in the dictionary.",[37,31773,31775],{"id":31774},"common-mistakes-to-check","Common mistakes to check",[14,31777,31778],{},"If the script is not working as expected, these checks can help:",[42,31780,31782],{"className":44,"code":31781,"language":46,"meta":47,"style":47},"print(source_folder.resolve())\nprint(item.name)\nprint(item.suffix.lower())\nprint(destination_folder)\nprint(source_folder.exists())\n",[49,31783,31784,31798,31812,31830,31840],{"__ignoreMap":47},[52,31785,31786,31788,31790,31792,31794,31796],{"class":54,"line":55},[52,31787,242],{"class":241},[52,31789,193],{"class":102},[52,31791,30781],{"class":189},[52,31793,186],{"class":102},[52,31795,31649],{"class":189},[52,31797,1816],{"class":102},[52,31799,31800,31802,31804,31806,31808,31810],{"class":54,"line":66},[52,31801,242],{"class":241},[52,31803,193],{"class":102},[52,31805,30322],{"class":189},[52,31807,186],{"class":102},[52,31809,2147],{"class":1320},[52,31811,211],{"class":102},[52,31813,31814,31816,31818,31820,31822,31824,31826,31828],{"class":54,"line":73},[52,31815,242],{"class":241},[52,31817,193],{"class":102},[52,31819,30322],{"class":189},[52,31821,186],{"class":102},[52,31823,30232],{"class":1320},[52,31825,186],{"class":102},[52,31827,30237],{"class":189},[52,31829,1816],{"class":102},[52,31831,31832,31834,31836,31838],{"class":54,"line":94},[52,31833,242],{"class":241},[52,31835,193],{"class":102},[52,31837,31050],{"class":189},[52,31839,211],{"class":102},[52,31841,31842,31844,31846,31848,31850,31852],{"class":54,"line":106},[52,31843,242],{"class":241},[52,31845,193],{"class":102},[52,31847,30781],{"class":189},[52,31849,186],{"class":102},[52,31851,15887],{"class":189},[52,31853,1816],{"class":102},[14,31855,31856],{},"These print statements help you see:",[309,31858,31859,31862,31865,31868,31871],{},[24,31860,31861],{},"The full folder path Python is using",[24,31863,31864],{},"Each file name being processed",[24,31866,31867],{},"The extension Python reads",[24,31869,31870],{},"The destination folder for that file",[24,31872,31873],{},"Whether the source folder actually exists",[14,31875,31876],{},"Common causes of problems include:",[309,31878,31879,31881,31884,31886,31891],{},[24,31880,31625],{},[24,31882,31883],{},"Running the script in a different working directory than expected",[24,31885,31685],{},[24,31887,31888,31889],{},"Forgetting that extensions can be uppercase like ",[49,31890,31703],{},[24,31892,31893],{},"Not handling duplicate file names in destination folders",[37,31895,1942],{"id":1941},[639,31897,31899],{"id":31898},"does-this-script-move-files-or-copy-them","Does this script move files or copy them?",[14,31901,31902],{},"It moves files. After running, the files are no longer in their original location.",[639,31904,31906],{"id":31905},"can-i-organize-files-by-type-without-using-pathlib","Can I organize files by type without using pathlib?",[14,31908,26892,31909,759,31911,31914,31915,31917,31918,186],{},[49,31910,15848],{},[49,31912,31913],{},"os.path",", but ",[49,31916,28924],{}," is usually easier for beginners to read. If you want a broader overview, see the ",[372,31919,29945],{"href":31920},"\u002Fstandard-library\u002Fpython-os-module-overview",[639,31922,31924],{"id":31923},"what-happens-to-files-with-unknown-extensions","What happens to files with unknown extensions?",[14,31926,31927,31928,30371],{},"In this example, they go into the ",[49,31929,30265],{},[639,31931,31933],{"id":31932},"will-this-script-organize-files-inside-subfolders-too","Will this script organize files inside subfolders too?",[14,31935,31936],{},"No. This version only looks at items directly inside the source folder.",[639,31938,31940],{"id":31939},"how-can-i-avoid-overwriting-files-with-the-same-name","How can I avoid overwriting files with the same name?",[14,31942,31943,31944,186],{},"Add a check before moving. Rename the file or skip it if the destination already exists. You may also want to learn ",[372,31945,31947],{"href":31946},"\u002Fhow-to\u002Fhow-to-rename-a-file-in-python","how to rename a file in Python",[37,31949,2005],{"id":2004},[309,31951,31952,31957,31961,31965,31971,31975,31979,31984],{},[24,31953,31954],{},[372,31955,31956],{"href":30815},"How to list files in a directory in Python",[24,31958,31959],{},[372,31960,29931],{"href":30402},[24,31962,31963],{},[372,31964,29945],{"href":31920},[24,31966,31967],{},[372,31968,31970],{"href":31969},"\u002Fstandard-library\u002Fos.path.join-function-explained","os.path.join() function explained",[24,31972,31973],{},[372,31974,29683],{"href":25232},[24,31976,31977],{},[372,31978,29689],{"href":25127},[24,31980,31981],{},[372,31982,31983],{"href":31946},"How to rename a file in Python",[24,31985,31986],{},[372,31987,31988],{"href":31076},"How to create a directory in Python",[14,31990,31991,31992,31995],{},"Try changing this example for your own ",[49,31993,31994],{},"Downloads"," folder, or build a safer version that copies files instead of moving them.",[2034,31997,31998],{},"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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":32000},[32001,32002,32003,32004,32005,32013,32014,32015,32016,32023,32024,32031],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":346,"depth":66,"text":347},{"id":27410,"depth":66,"text":27411},{"id":25926,"depth":66,"text":25927,"children":32006},[32007,32008,32009,32010,32011,32012],{"id":30821,"depth":73,"text":30824},{"id":30882,"depth":73,"text":30885},{"id":30911,"depth":73,"text":30914},{"id":30970,"depth":73,"text":30973},{"id":31037,"depth":73,"text":31040},{"id":31080,"depth":73,"text":31083},{"id":26125,"depth":66,"text":26126},{"id":31166,"depth":66,"text":31167},{"id":31573,"depth":66,"text":31574},{"id":29637,"depth":66,"text":29638,"children":32017},[32018,32019,32020,32021,32022],{"id":31612,"depth":73,"text":15764},{"id":31672,"depth":73,"text":31675},{"id":31696,"depth":73,"text":31697},{"id":31736,"depth":73,"text":31737},{"id":31761,"depth":73,"text":31762},{"id":31774,"depth":66,"text":31775},{"id":1941,"depth":66,"text":1942,"children":32025},[32026,32027,32028,32029,32030],{"id":31898,"depth":73,"text":31899},{"id":31905,"depth":73,"text":31906},{"id":31923,"depth":73,"text":31924},{"id":31932,"depth":73,"text":31933},{"id":31939,"depth":73,"text":31940},{"id":2004,"depth":66,"text":2005},"Master python file organizer script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-file-organizer-script-example",{"title":29992,"description":32032},"examples\u002Fpython-file-organizer-script-example","jrktYhLn2pyVqUHFOBftbnpXHhaYCL3c_NQWBnHwbTE",{"id":32039,"title":32040,"body":32041,"description":34539,"extension":2075,"meta":34540,"navigation":69,"path":34541,"seo":34542,"stem":34543,"__hash__":34544},"content\u002Fexamples\u002Fpython-filter-data-from-a-list-example.md","Python Filter Data from a List Example",{"type":7,"value":32042,"toc":34505},[32043,32046,32049,32052,32068,32081,32083,32171,32175,32196,32199,32201,32215,32219,32224,32226,32242,32379,32383,32446,32450,32481,32484,32505,32511,32520,32524,32527,32637,32641,32701,32704,32717,32720,32727,32731,32734,32738,32814,32818,32836,32840,32922,32926,32947,32951,33048,33052,33077,33081,33178,33182,33207,33211,33301,33305,33338,33342,33345,33494,33498,33571,33574,33588,33591,33616,33623,33630,33634,33637,33641,33647,33715,33718,33807,33814,33817,33834,33839,33856,33860,33863,33944,33948,33985,33991,33995,34002,34005,34022,34025,34042,34045,34049,34052,34055,34058,34062,34065,34082,34085,34153,34156,34158,34161,34164,34231,34233,34389,34391,34406,34408,34412,34421,34425,34428,34435,34438,34442,34445,34447,34474,34477,34499,34502],[10,32044,32040],{"id":32045},"python-filter-data-from-a-list-example",[14,32047,32048],{},"Filtering a list means creating a new list that keeps only the items you want.",[14,32050,32051],{},"This example shows simple beginner-friendly ways to filter data from a Python list using:",[309,32053,32054,32059,32065],{},[24,32055,32056,32057,12570],{},"a ",[49,32058,12265],{},[24,32060,32061,32062,32064],{},"an ",[49,32063,1313],{}," statement",[24,32066,32067],{},"a list comprehension",[14,32069,32070,32071,759,32076,186],{},"If you are new to loops and conditions, this is a good place to start before reading more detailed guides on ",[372,32072,10646,32074,13139],{"href":32073},"\u002Flearn\u002Fpython-for-loops-explained\u002F",[49,32075,12265],{},[372,32077,10646,32079,30419],{"href":32078},"\u002Flearn\u002Fpython-if-statements-explained\u002F",[49,32080,1313],{},[37,32082,40],{"id":39},[42,32084,32086],{"className":44,"code":32085,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3, 4, 5, 6]\neven_numbers = [n for n in numbers if n % 2 == 0]\nprint(even_numbers)\n",[49,32087,32088,32123,32160],{"__ignoreMap":47},[52,32089,32090,32093,32095,32097,32099,32101,32103,32105,32107,32109,32112,32114,32116,32118,32121],{"class":54,"line":55},[52,32091,32092],{"class":62},"numbers ",[52,32094,80],{"class":79},[52,32096,18162],{"class":102},[52,32098,9200],{"class":121},[52,32100,199],{"class":102},[52,32102,9168],{"class":121},[52,32104,199],{"class":102},[52,32106,5261],{"class":121},[52,32108,199],{"class":102},[52,32110,32111],{"class":121}," 4",[52,32113,199],{"class":102},[52,32115,7646],{"class":121},[52,32117,199],{"class":102},[52,32119,32120],{"class":121}," 6",[52,32122,1147],{"class":102},[52,32124,32125,32128,32130,32132,32135,32137,32140,32142,32145,32147,32149,32152,32154,32156,32158],{"class":54,"line":66},[52,32126,32127],{"class":62},"even_numbers ",[52,32129,80],{"class":79},[52,32131,18162],{"class":102},[52,32133,32134],{"class":62},"n ",[52,32136,12265],{"class":58},[52,32138,32139],{"class":62}," n ",[52,32141,11213],{"class":58},[52,32143,32144],{"class":62}," numbers ",[52,32146,1313],{"class":58},[52,32148,32139],{"class":62},[52,32150,32151],{"class":79},"%",[52,32153,9168],{"class":121},[52,32155,1324],{"class":79},[52,32157,13049],{"class":121},[52,32159,1147],{"class":102},[52,32161,32162,32164,32166,32169],{"class":54,"line":73},[52,32163,242],{"class":241},[52,32165,193],{"class":102},[52,32167,32168],{"class":189},"even_numbers",[52,32170,211],{"class":102},[14,32172,32173],{},[5503,32174,5267],{},[42,32176,32178],{"className":44,"code":32177,"language":46,"meta":47,"style":47},"[2, 4, 6]\n",[49,32179,32180],{"__ignoreMap":47},[52,32181,32182,32184,32186,32188,32190,32192,32194],{"class":54,"line":55},[52,32183,250],{"class":102},[52,32185,9825],{"class":121},[52,32187,199],{"class":102},[52,32189,32111],{"class":121},[52,32191,199],{"class":102},[52,32193,32120],{"class":121},[52,32195,1147],{"class":102},[14,32197,32198],{},"Use a list comprehension when you want to create a new list that keeps only matching items.",[37,32200,2268],{"id":2267},[309,32202,32203,32206,32209,32212],{},[24,32204,32205],{},"How to create a new list from an existing list",[24,32207,32208],{},"How to keep only items that match a condition",[24,32210,32211],{},"How filtering does not change the original list unless you reassign it",[24,32213,32214],{},"When to use a loop vs a list comprehension",[37,32216,32218],{"id":32217},"example-1-filter-with-a-for-loop","Example 1: Filter with a for loop",[14,32220,3233,32221,32223],{},[49,32222,12265],{}," loop is often the easiest way to understand filtering.",[14,32225,10890],{},[21,32227,32228,32231,32234,32239],{},[24,32229,32230],{},"Start with an empty list",[24,32232,32233],{},"Loop through the original list",[24,32235,32236,32237,32064],{},"Check each item with an ",[49,32238,1313],{},[24,32240,32241],{},"Add matching items to the new list",[42,32243,32245],{"className":44,"code":32244,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3, 4, 5, 6]\neven_numbers = []\n\nfor number in numbers:\n    if number % 2 == 0:\n        even_numbers.append(number)\n\nprint(\"Original list:\", numbers)\nprint(\"Filtered list:\", even_numbers)\n",[49,32246,32247,32279,32287,32291,32305,32321,32336,32340,32359],{"__ignoreMap":47},[52,32248,32249,32251,32253,32255,32257,32259,32261,32263,32265,32267,32269,32271,32273,32275,32277],{"class":54,"line":55},[52,32250,32092],{"class":62},[52,32252,80],{"class":79},[52,32254,18162],{"class":102},[52,32256,9200],{"class":121},[52,32258,199],{"class":102},[52,32260,9168],{"class":121},[52,32262,199],{"class":102},[52,32264,5261],{"class":121},[52,32266,199],{"class":102},[52,32268,32111],{"class":121},[52,32270,199],{"class":102},[52,32272,7646],{"class":121},[52,32274,199],{"class":102},[52,32276,32120],{"class":121},[52,32278,1147],{"class":102},[52,32280,32281,32283,32285],{"class":54,"line":66},[52,32282,32127],{"class":62},[52,32284,80],{"class":79},[52,32286,18590],{"class":102},[52,32288,32289],{"class":54,"line":73},[52,32290,70],{"emptyLinePlaceholder":69},[52,32292,32293,32295,32298,32300,32303],{"class":54,"line":94},[52,32294,12265],{"class":58},[52,32296,32297],{"class":62}," number ",[52,32299,11213],{"class":58},[52,32301,32302],{"class":62}," numbers",[52,32304,1330],{"class":102},[52,32306,32307,32309,32311,32313,32315,32317,32319],{"class":54,"line":106},[52,32308,1438],{"class":58},[52,32310,32297],{"class":62},[52,32312,32151],{"class":79},[52,32314,9168],{"class":121},[52,32316,1324],{"class":79},[52,32318,13049],{"class":121},[52,32320,1330],{"class":102},[52,32322,32323,32326,32328,32330,32332,32334],{"class":54,"line":128},[52,32324,32325],{"class":62},"        even_numbers",[52,32327,186],{"class":102},[52,32329,18748],{"class":189},[52,32331,193],{"class":102},[52,32333,27497],{"class":189},[52,32335,211],{"class":102},[52,32337,32338],{"class":54,"line":148},[52,32339,70],{"emptyLinePlaceholder":69},[52,32341,32342,32344,32346,32348,32351,32353,32355,32357],{"class":54,"line":164},[52,32343,242],{"class":241},[52,32345,193],{"class":102},[52,32347,115],{"class":83},[52,32349,32350],{"class":87},"Original list:",[52,32352,115],{"class":83},[52,32354,199],{"class":102},[52,32356,32302],{"class":189},[52,32358,211],{"class":102},[52,32360,32361,32363,32365,32367,32370,32372,32374,32377],{"class":54,"line":170},[52,32362,242],{"class":241},[52,32364,193],{"class":102},[52,32366,115],{"class":83},[52,32368,32369],{"class":87},"Filtered list:",[52,32371,115],{"class":83},[52,32373,199],{"class":102},[52,32375,32376],{"class":189}," even_numbers",[52,32378,211],{"class":102},[14,32380,32381],{},[5503,32382,5267],{},[42,32384,32386],{"className":44,"code":32385,"language":46,"meta":47,"style":47},"Original list: [1, 2, 3, 4, 5, 6]\nFiltered list: [2, 4, 6]\n",[49,32387,32388,32423],{"__ignoreMap":47},[52,32389,32390,32393,32395,32397,32399,32401,32403,32405,32407,32409,32411,32413,32415,32417,32419,32421],{"class":54,"line":55},[52,32391,32392],{"class":62},"Original ",[52,32394,15301],{"class":4733},[52,32396,118],{"class":102},[52,32398,18162],{"class":102},[52,32400,9200],{"class":121},[52,32402,199],{"class":102},[52,32404,9168],{"class":121},[52,32406,199],{"class":102},[52,32408,5261],{"class":121},[52,32410,199],{"class":102},[52,32412,32111],{"class":121},[52,32414,199],{"class":102},[52,32416,7646],{"class":121},[52,32418,199],{"class":102},[52,32420,32120],{"class":121},[52,32422,1147],{"class":102},[52,32424,32425,32428,32430,32432,32434,32436,32438,32440,32442,32444],{"class":54,"line":66},[52,32426,32427],{"class":62},"Filtered ",[52,32429,15301],{"class":4733},[52,32431,118],{"class":102},[52,32433,18162],{"class":102},[52,32435,9825],{"class":121},[52,32437,199],{"class":102},[52,32439,32111],{"class":121},[52,32441,199],{"class":102},[52,32443,32120],{"class":121},[52,32445,1147],{"class":102},[639,32447,32449],{"id":32448},"why-this-works","Why this works",[309,32451,32452,32457,32463,32469,32475],{},[24,32453,32454,32456],{},[49,32455,5324],{}," is the original list",[24,32458,32459,32462],{},[49,32460,32461],{},"even_numbers = []"," creates an empty result list",[24,32464,32465,32468],{},[49,32466,32467],{},"for number in numbers:"," checks each item one by one",[24,32470,32471,32474],{},[49,32472,32473],{},"if number % 2 == 0:"," keeps only even numbers",[24,32476,32477,32480],{},[49,32478,32479],{},"even_numbers.append(number)"," adds matching items to the result",[14,32482,32483],{},"The key line is:",[42,32485,32487],{"className":44,"code":32486,"language":46,"meta":47,"style":47},"if number % 2 == 0:\n",[49,32488,32489],{"__ignoreMap":47},[52,32490,32491,32493,32495,32497,32499,32501,32503],{"class":54,"line":55},[52,32492,1313],{"class":58},[52,32494,32297],{"class":62},[52,32496,32151],{"class":79},[52,32498,9168],{"class":121},[52,32500,1324],{"class":79},[52,32502,13049],{"class":121},[52,32504,1330],{"class":102},[14,32506,32507,32508,32510],{},"That condition returns ",[49,32509,6233],{}," only for even numbers.",[14,32512,32513,32514,186],{},"If you want to learn more about adding items to a list, see ",[372,32515,10646,32517],{"href":32516},"\u002Freference\u002Fpython-list-append-method\u002F",[49,32518,32519],{},"list.append()",[37,32521,32523],{"id":32522},"example-2-filter-with-a-list-comprehension","Example 2: Filter with a list comprehension",[14,32525,32526],{},"After you understand the loop version, you can write the same logic in one line with a list comprehension.",[42,32528,32530],{"className":44,"code":32529,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3, 4, 5, 6]\neven_numbers = [number for number in numbers if number % 2 == 0]\n\nprint(\"Original list:\", numbers)\nprint(\"Filtered list:\", even_numbers)\n",[49,32531,32532,32564,32597,32601,32619],{"__ignoreMap":47},[52,32533,32534,32536,32538,32540,32542,32544,32546,32548,32550,32552,32554,32556,32558,32560,32562],{"class":54,"line":55},[52,32535,32092],{"class":62},[52,32537,80],{"class":79},[52,32539,18162],{"class":102},[52,32541,9200],{"class":121},[52,32543,199],{"class":102},[52,32545,9168],{"class":121},[52,32547,199],{"class":102},[52,32549,5261],{"class":121},[52,32551,199],{"class":102},[52,32553,32111],{"class":121},[52,32555,199],{"class":102},[52,32557,7646],{"class":121},[52,32559,199],{"class":102},[52,32561,32120],{"class":121},[52,32563,1147],{"class":102},[52,32565,32566,32568,32570,32572,32575,32577,32579,32581,32583,32585,32587,32589,32591,32593,32595],{"class":54,"line":66},[52,32567,32127],{"class":62},[52,32569,80],{"class":79},[52,32571,18162],{"class":102},[52,32573,32574],{"class":62},"number ",[52,32576,12265],{"class":58},[52,32578,32297],{"class":62},[52,32580,11213],{"class":58},[52,32582,32144],{"class":62},[52,32584,1313],{"class":58},[52,32586,32297],{"class":62},[52,32588,32151],{"class":79},[52,32590,9168],{"class":121},[52,32592,1324],{"class":79},[52,32594,13049],{"class":121},[52,32596,1147],{"class":102},[52,32598,32599],{"class":54,"line":73},[52,32600,70],{"emptyLinePlaceholder":69},[52,32602,32603,32605,32607,32609,32611,32613,32615,32617],{"class":54,"line":94},[52,32604,242],{"class":241},[52,32606,193],{"class":102},[52,32608,115],{"class":83},[52,32610,32350],{"class":87},[52,32612,115],{"class":83},[52,32614,199],{"class":102},[52,32616,32302],{"class":189},[52,32618,211],{"class":102},[52,32620,32621,32623,32625,32627,32629,32631,32633,32635],{"class":54,"line":106},[52,32622,242],{"class":241},[52,32624,193],{"class":102},[52,32626,115],{"class":83},[52,32628,32369],{"class":87},[52,32630,115],{"class":83},[52,32632,199],{"class":102},[52,32634,32376],{"class":189},[52,32636,211],{"class":102},[14,32638,32639],{},[5503,32640,5267],{},[42,32642,32643],{"className":44,"code":32385,"language":46,"meta":47,"style":47},[49,32644,32645,32679],{"__ignoreMap":47},[52,32646,32647,32649,32651,32653,32655,32657,32659,32661,32663,32665,32667,32669,32671,32673,32675,32677],{"class":54,"line":55},[52,32648,32392],{"class":62},[52,32650,15301],{"class":4733},[52,32652,118],{"class":102},[52,32654,18162],{"class":102},[52,32656,9200],{"class":121},[52,32658,199],{"class":102},[52,32660,9168],{"class":121},[52,32662,199],{"class":102},[52,32664,5261],{"class":121},[52,32666,199],{"class":102},[52,32668,32111],{"class":121},[52,32670,199],{"class":102},[52,32672,7646],{"class":121},[52,32674,199],{"class":102},[52,32676,32120],{"class":121},[52,32678,1147],{"class":102},[52,32680,32681,32683,32685,32687,32689,32691,32693,32695,32697,32699],{"class":54,"line":66},[52,32682,32427],{"class":62},[52,32684,15301],{"class":4733},[52,32686,118],{"class":102},[52,32688,18162],{"class":102},[52,32690,9825],{"class":121},[52,32692,199],{"class":102},[52,32694,32111],{"class":121},[52,32696,199],{"class":102},[52,32698,32120],{"class":121},[52,32700,1147],{"class":102},[14,32702,32703],{},"This version is shorter, but it does the same job:",[309,32705,32706,32711,32714],{},[24,32707,32708,32709],{},"loop through ",[49,32710,5324],{},[24,32712,32713],{},"test each item",[24,32715,32716],{},"keep only matching items",[14,32718,32719],{},"Use a list comprehension when the condition is short and easy to read.",[14,32721,32722,32723,186],{},"For more practice, read ",[372,32724,32726],{"href":32725},"\u002Fhow-to\u002Fhow-to-use-list-comprehensions-in-python\u002F","how to use list comprehensions in Python",[37,32728,32730],{"id":32729},"common-filtering-conditions","Common filtering conditions",[14,32732,32733],{},"You can filter lists in many ways. Here are a few simple patterns.",[639,32735,32737],{"id":32736},"numbers-greater-than-a-value","Numbers greater than a value",[42,32739,32741],{"className":44,"code":32740,"language":46,"meta":47,"style":47},"numbers = [3, 8, 12, 1, 20]\nresult = [n for n in numbers if n > 10]\nprint(result)\n",[49,32742,32743,32774,32803],{"__ignoreMap":47},[52,32744,32745,32747,32749,32751,32753,32755,32758,32760,32763,32765,32767,32769,32772],{"class":54,"line":55},[52,32746,32092],{"class":62},[52,32748,80],{"class":79},[52,32750,18162],{"class":102},[52,32752,7508],{"class":121},[52,32754,199],{"class":102},[52,32756,32757],{"class":121}," 8",[52,32759,199],{"class":102},[52,32761,32762],{"class":121}," 12",[52,32764,199],{"class":102},[52,32766,1518],{"class":121},[52,32768,199],{"class":102},[52,32770,32771],{"class":121}," 20",[52,32773,1147],{"class":102},[52,32775,32776,32779,32781,32783,32785,32787,32789,32791,32793,32795,32797,32799,32801],{"class":54,"line":66},[52,32777,32778],{"class":62},"result ",[52,32780,80],{"class":79},[52,32782,18162],{"class":102},[52,32784,32134],{"class":62},[52,32786,12265],{"class":58},[52,32788,32139],{"class":62},[52,32790,11213],{"class":58},[52,32792,32144],{"class":62},[52,32794,1313],{"class":58},[52,32796,32139],{"class":62},[52,32798,13046],{"class":79},[52,32800,27053],{"class":121},[52,32802,1147],{"class":102},[52,32804,32805,32807,32809,32812],{"class":54,"line":73},[52,32806,242],{"class":241},[52,32808,193],{"class":102},[52,32810,32811],{"class":189},"result",[52,32813,211],{"class":102},[14,32815,32816],{},[5503,32817,5267],{},[42,32819,32821],{"className":44,"code":32820,"language":46,"meta":47,"style":47},"[12, 20]\n",[49,32822,32823],{"__ignoreMap":47},[52,32824,32825,32827,32830,32832,32834],{"class":54,"line":55},[52,32826,250],{"class":102},[52,32828,32829],{"class":121},"12",[52,32831,199],{"class":102},[52,32833,32771],{"class":121},[52,32835,1147],{"class":102},[639,32837,32839],{"id":32838},"even-or-odd-numbers","Even or odd numbers",[42,32841,32843],{"className":44,"code":32842,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3, 4, 5, 6]\nodd_numbers = [n for n in numbers if n % 2 != 0]\nprint(odd_numbers)\n",[49,32844,32845,32877,32911],{"__ignoreMap":47},[52,32846,32847,32849,32851,32853,32855,32857,32859,32861,32863,32865,32867,32869,32871,32873,32875],{"class":54,"line":55},[52,32848,32092],{"class":62},[52,32850,80],{"class":79},[52,32852,18162],{"class":102},[52,32854,9200],{"class":121},[52,32856,199],{"class":102},[52,32858,9168],{"class":121},[52,32860,199],{"class":102},[52,32862,5261],{"class":121},[52,32864,199],{"class":102},[52,32866,32111],{"class":121},[52,32868,199],{"class":102},[52,32870,7646],{"class":121},[52,32872,199],{"class":102},[52,32874,32120],{"class":121},[52,32876,1147],{"class":102},[52,32878,32879,32882,32884,32886,32888,32890,32892,32894,32896,32898,32900,32902,32904,32907,32909],{"class":54,"line":66},[52,32880,32881],{"class":62},"odd_numbers ",[52,32883,80],{"class":79},[52,32885,18162],{"class":102},[52,32887,32134],{"class":62},[52,32889,12265],{"class":58},[52,32891,32139],{"class":62},[52,32893,11213],{"class":58},[52,32895,32144],{"class":62},[52,32897,1313],{"class":58},[52,32899,32139],{"class":62},[52,32901,32151],{"class":79},[52,32903,9168],{"class":121},[52,32905,32906],{"class":79}," !=",[52,32908,13049],{"class":121},[52,32910,1147],{"class":102},[52,32912,32913,32915,32917,32920],{"class":54,"line":73},[52,32914,242],{"class":241},[52,32916,193],{"class":102},[52,32918,32919],{"class":189},"odd_numbers",[52,32921,211],{"class":102},[14,32923,32924],{},[5503,32925,5267],{},[42,32927,32929],{"className":44,"code":32928,"language":46,"meta":47,"style":47},"[1, 3, 5]\n",[49,32930,32931],{"__ignoreMap":47},[52,32932,32933,32935,32937,32939,32941,32943,32945],{"class":54,"line":55},[52,32934,250],{"class":102},[52,32936,9200],{"class":121},[52,32938,199],{"class":102},[52,32940,5261],{"class":121},[52,32942,199],{"class":102},[52,32944,7646],{"class":121},[52,32946,1147],{"class":102},[639,32948,32950],{"id":32949},"strings-that-contain-a-word","Strings that contain a word",[42,32952,32954],{"className":44,"code":32953,"language":46,"meta":47,"style":47},"words = [\"apple pie\", \"banana\", \"green apple\", \"orange\"]\nresult = [word for word in words if \"apple\" in word]\nprint(result)\n",[49,32955,32956,33001,33038],{"__ignoreMap":47},[52,32957,32958,32961,32963,32965,32967,32970,32972,32974,32976,32979,32981,32983,32985,32988,32990,32992,32994,32997,32999],{"class":54,"line":55},[52,32959,32960],{"class":62},"words ",[52,32962,80],{"class":79},[52,32964,18162],{"class":102},[52,32966,115],{"class":83},[52,32968,32969],{"class":87},"apple pie",[52,32971,115],{"class":83},[52,32973,199],{"class":102},[52,32975,84],{"class":83},[52,32977,32978],{"class":87},"banana",[52,32980,115],{"class":83},[52,32982,199],{"class":102},[52,32984,84],{"class":83},[52,32986,32987],{"class":87},"green apple",[52,32989,115],{"class":83},[52,32991,199],{"class":102},[52,32993,84],{"class":83},[52,32995,32996],{"class":87},"orange",[52,32998,115],{"class":83},[52,33000,1147],{"class":102},[52,33002,33003,33005,33007,33009,33012,33014,33017,33019,33022,33024,33026,33029,33031,33033,33036],{"class":54,"line":66},[52,33004,32778],{"class":62},[52,33006,80],{"class":79},[52,33008,18162],{"class":102},[52,33010,33011],{"class":62},"word ",[52,33013,12265],{"class":58},[52,33015,33016],{"class":62}," word ",[52,33018,11213],{"class":58},[52,33020,33021],{"class":62}," words ",[52,33023,1313],{"class":58},[52,33025,84],{"class":83},[52,33027,33028],{"class":87},"apple",[52,33030,115],{"class":83},[52,33032,3101],{"class":79},[52,33034,33035],{"class":62}," word",[52,33037,1147],{"class":102},[52,33039,33040,33042,33044,33046],{"class":54,"line":73},[52,33041,242],{"class":241},[52,33043,193],{"class":102},[52,33045,32811],{"class":189},[52,33047,211],{"class":102},[14,33049,33050],{},[5503,33051,5267],{},[42,33053,33055],{"className":44,"code":33054,"language":46,"meta":47,"style":47},"['apple pie', 'green apple']\n",[49,33056,33057],{"__ignoreMap":47},[52,33058,33059,33061,33063,33065,33067,33069,33071,33073,33075],{"class":54,"line":55},[52,33060,250],{"class":102},[52,33062,4162],{"class":83},[52,33064,32969],{"class":87},[52,33066,4162],{"class":83},[52,33068,199],{"class":102},[52,33070,4172],{"class":83},[52,33072,32987],{"class":87},[52,33074,4162],{"class":83},[52,33076,1147],{"class":102},[639,33078,33080],{"id":33079},"strings-that-start-with-a-letter","Strings that start with a letter",[42,33082,33084],{"className":44,"code":33083,"language":46,"meta":47,"style":47},"names = [\"Alice\", \"Bob\", \"Anna\", \"Charlie\"]\nresult = [name for name in names if name.startswith(\"A\")]\nprint(result)\n",[49,33085,33086,33128,33168],{"__ignoreMap":47},[52,33087,33088,33091,33093,33095,33097,33099,33101,33103,33105,33107,33109,33111,33113,33116,33118,33120,33122,33124,33126],{"class":54,"line":55},[52,33089,33090],{"class":62},"names ",[52,33092,80],{"class":79},[52,33094,18162],{"class":102},[52,33096,115],{"class":83},[52,33098,2156],{"class":87},[52,33100,115],{"class":83},[52,33102,199],{"class":102},[52,33104,84],{"class":83},[52,33106,10858],{"class":87},[52,33108,115],{"class":83},[52,33110,199],{"class":102},[52,33112,84],{"class":83},[52,33114,33115],{"class":87},"Anna",[52,33117,115],{"class":83},[52,33119,199],{"class":102},[52,33121,84],{"class":83},[52,33123,15099],{"class":87},[52,33125,115],{"class":83},[52,33127,1147],{"class":102},[52,33129,33130,33132,33134,33136,33138,33140,33142,33144,33147,33149,33151,33153,33156,33158,33160,33163,33165],{"class":54,"line":66},[52,33131,32778],{"class":62},[52,33133,80],{"class":79},[52,33135,18162],{"class":102},[52,33137,9211],{"class":62},[52,33139,12265],{"class":58},[52,33141,11292],{"class":62},[52,33143,11213],{"class":58},[52,33145,33146],{"class":62}," names ",[52,33148,1313],{"class":58},[52,33150,7019],{"class":62},[52,33152,186],{"class":102},[52,33154,33155],{"class":189},"startswith",[52,33157,193],{"class":102},[52,33159,115],{"class":83},[52,33161,33162],{"class":87},"A",[52,33164,115],{"class":83},[52,33166,33167],{"class":102},")]\n",[52,33169,33170,33172,33174,33176],{"class":54,"line":73},[52,33171,242],{"class":241},[52,33173,193],{"class":102},[52,33175,32811],{"class":189},[52,33177,211],{"class":102},[14,33179,33180],{},[5503,33181,5267],{},[42,33183,33185],{"className":44,"code":33184,"language":46,"meta":47,"style":47},"['Alice', 'Anna']\n",[49,33186,33187],{"__ignoreMap":47},[52,33188,33189,33191,33193,33195,33197,33199,33201,33203,33205],{"class":54,"line":55},[52,33190,250],{"class":102},[52,33192,4162],{"class":83},[52,33194,2156],{"class":87},[52,33196,4162],{"class":83},[52,33198,199],{"class":102},[52,33200,4172],{"class":83},[52,33202,33115],{"class":87},[52,33204,4162],{"class":83},[52,33206,1147],{"class":102},[639,33208,33210],{"id":33209},"items-that-are-not-empty","Items that are not empty",[42,33212,33214],{"className":44,"code":33213,"language":46,"meta":47,"style":47},"items = [\"book\", \"\", \"pen\", \"\", \"paper\"]\nresult = [item for item in items if item != \"\"]\nprint(result)\n",[49,33215,33216,33260,33291],{"__ignoreMap":47},[52,33217,33218,33221,33223,33225,33227,33230,33232,33234,33236,33238,33240,33243,33245,33247,33249,33251,33253,33256,33258],{"class":54,"line":55},[52,33219,33220],{"class":62},"items ",[52,33222,80],{"class":79},[52,33224,18162],{"class":102},[52,33226,115],{"class":83},[52,33228,33229],{"class":87},"book",[52,33231,115],{"class":83},[52,33233,199],{"class":102},[52,33235,3448],{"class":83},[52,33237,199],{"class":102},[52,33239,84],{"class":83},[52,33241,33242],{"class":87},"pen",[52,33244,115],{"class":83},[52,33246,199],{"class":102},[52,33248,3448],{"class":83},[52,33250,199],{"class":102},[52,33252,84],{"class":83},[52,33254,33255],{"class":87},"paper",[52,33257,115],{"class":83},[52,33259,1147],{"class":102},[52,33261,33262,33264,33266,33268,33271,33273,33275,33277,33280,33282,33284,33287,33289],{"class":54,"line":66},[52,33263,32778],{"class":62},[52,33265,80],{"class":79},[52,33267,18162],{"class":102},[52,33269,33270],{"class":62},"item ",[52,33272,12265],{"class":58},[52,33274,30192],{"class":62},[52,33276,11213],{"class":58},[52,33278,33279],{"class":62}," items ",[52,33281,1313],{"class":58},[52,33283,30192],{"class":62},[52,33285,33286],{"class":79},"!=",[52,33288,3448],{"class":83},[52,33290,1147],{"class":102},[52,33292,33293,33295,33297,33299],{"class":54,"line":73},[52,33294,242],{"class":241},[52,33296,193],{"class":102},[52,33298,32811],{"class":189},[52,33300,211],{"class":102},[14,33302,33303],{},[5503,33304,5267],{},[42,33306,33308],{"className":44,"code":33307,"language":46,"meta":47,"style":47},"['book', 'pen', 'paper']\n",[49,33309,33310],{"__ignoreMap":47},[52,33311,33312,33314,33316,33318,33320,33322,33324,33326,33328,33330,33332,33334,33336],{"class":54,"line":55},[52,33313,250],{"class":102},[52,33315,4162],{"class":83},[52,33317,33229],{"class":87},[52,33319,4162],{"class":83},[52,33321,199],{"class":102},[52,33323,4172],{"class":83},[52,33325,33242],{"class":87},[52,33327,4162],{"class":83},[52,33329,199],{"class":102},[52,33331,4172],{"class":83},[52,33333,33255],{"class":87},[52,33335,4162],{"class":83},[52,33337,1147],{"class":102},[37,33339,33341],{"id":33340},"expected-output-and-why-it-works","Expected output and why it works",[14,33343,33344],{},"When you filter a list, it helps to print both the original list and the new list.",[42,33346,33348],{"className":44,"code":33347,"language":46,"meta":47,"style":47},"names = [\"Anna\", \"Ben\", \"Alex\", \"Cara\"]\nfiltered_names = []\n\nfor name in names:\n    if name.startswith(\"A\"):\n        filtered_names.append(name)\n\nprint(\"Original list:\", names)\nprint(\"Filtered list:\", filtered_names)\n",[49,33349,33350,33392,33401,33405,33418,33438,33453,33457,33475],{"__ignoreMap":47},[52,33351,33352,33354,33356,33358,33360,33362,33364,33366,33368,33371,33373,33375,33377,33380,33382,33384,33386,33388,33390],{"class":54,"line":55},[52,33353,33090],{"class":62},[52,33355,80],{"class":79},[52,33357,18162],{"class":102},[52,33359,115],{"class":83},[52,33361,33115],{"class":87},[52,33363,115],{"class":83},[52,33365,199],{"class":102},[52,33367,84],{"class":83},[52,33369,33370],{"class":87},"Ben",[52,33372,115],{"class":83},[52,33374,199],{"class":102},[52,33376,84],{"class":83},[52,33378,33379],{"class":87},"Alex",[52,33381,115],{"class":83},[52,33383,199],{"class":102},[52,33385,84],{"class":83},[52,33387,20170],{"class":87},[52,33389,115],{"class":83},[52,33391,1147],{"class":102},[52,33393,33394,33397,33399],{"class":54,"line":66},[52,33395,33396],{"class":62},"filtered_names ",[52,33398,80],{"class":79},[52,33400,18590],{"class":102},[52,33402,33403],{"class":54,"line":73},[52,33404,70],{"emptyLinePlaceholder":69},[52,33406,33407,33409,33411,33413,33416],{"class":54,"line":94},[52,33408,12265],{"class":58},[52,33410,11292],{"class":62},[52,33412,11213],{"class":58},[52,33414,33415],{"class":62}," names",[52,33417,1330],{"class":102},[52,33419,33420,33422,33424,33426,33428,33430,33432,33434,33436],{"class":54,"line":106},[52,33421,1438],{"class":58},[52,33423,7019],{"class":62},[52,33425,186],{"class":102},[52,33427,33155],{"class":189},[52,33429,193],{"class":102},[52,33431,115],{"class":83},[52,33433,33162],{"class":87},[52,33435,115],{"class":83},[52,33437,3114],{"class":102},[52,33439,33440,33443,33445,33447,33449,33451],{"class":54,"line":128},[52,33441,33442],{"class":62},"        filtered_names",[52,33444,186],{"class":102},[52,33446,18748],{"class":189},[52,33448,193],{"class":102},[52,33450,2147],{"class":189},[52,33452,211],{"class":102},[52,33454,33455],{"class":54,"line":148},[52,33456,70],{"emptyLinePlaceholder":69},[52,33458,33459,33461,33463,33465,33467,33469,33471,33473],{"class":54,"line":164},[52,33460,242],{"class":241},[52,33462,193],{"class":102},[52,33464,115],{"class":83},[52,33466,32350],{"class":87},[52,33468,115],{"class":83},[52,33470,199],{"class":102},[52,33472,33415],{"class":189},[52,33474,211],{"class":102},[52,33476,33477,33479,33481,33483,33485,33487,33489,33492],{"class":54,"line":170},[52,33478,242],{"class":241},[52,33480,193],{"class":102},[52,33482,115],{"class":83},[52,33484,32369],{"class":87},[52,33486,115],{"class":83},[52,33488,199],{"class":102},[52,33490,33491],{"class":189}," filtered_names",[52,33493,211],{"class":102},[14,33495,33496],{},[5503,33497,5267],{},[42,33499,33501],{"className":44,"code":33500,"language":46,"meta":47,"style":47},"Original list: ['Anna', 'Ben', 'Alex', 'Cara']\nFiltered list: ['Anna', 'Alex']\n",[49,33502,33503,33545],{"__ignoreMap":47},[52,33504,33505,33507,33509,33511,33513,33515,33517,33519,33521,33523,33525,33527,33529,33531,33533,33535,33537,33539,33541,33543],{"class":54,"line":55},[52,33506,32392],{"class":62},[52,33508,15301],{"class":4733},[52,33510,118],{"class":102},[52,33512,18162],{"class":102},[52,33514,4162],{"class":83},[52,33516,33115],{"class":87},[52,33518,4162],{"class":83},[52,33520,199],{"class":102},[52,33522,4172],{"class":83},[52,33524,33370],{"class":87},[52,33526,4162],{"class":83},[52,33528,199],{"class":102},[52,33530,4172],{"class":83},[52,33532,33379],{"class":87},[52,33534,4162],{"class":83},[52,33536,199],{"class":102},[52,33538,4172],{"class":83},[52,33540,20170],{"class":87},[52,33542,4162],{"class":83},[52,33544,1147],{"class":102},[52,33546,33547,33549,33551,33553,33555,33557,33559,33561,33563,33565,33567,33569],{"class":54,"line":66},[52,33548,32427],{"class":62},[52,33550,15301],{"class":4733},[52,33552,118],{"class":102},[52,33554,18162],{"class":102},[52,33556,4162],{"class":83},[52,33558,33115],{"class":87},[52,33560,4162],{"class":83},[52,33562,199],{"class":102},[52,33564,4172],{"class":83},[52,33566,33379],{"class":87},[52,33568,4162],{"class":83},[52,33570,1147],{"class":102},[14,33572,33573],{},"Why it works:",[309,33575,33576,33579,33585],{},[24,33577,33578],{},"The original list stays the same",[24,33580,33581,33582],{},"Only matching items are added to ",[49,33583,33584],{},"filtered_names",[24,33586,33587],{},"The condition is the part that decides what stays",[14,33589,33590],{},"In this example, the important line is:",[42,33592,33594],{"className":44,"code":33593,"language":46,"meta":47,"style":47},"if name.startswith(\"A\"):\n",[49,33595,33596],{"__ignoreMap":47},[52,33597,33598,33600,33602,33604,33606,33608,33610,33612,33614],{"class":54,"line":55},[52,33599,1313],{"class":58},[52,33601,7019],{"class":62},[52,33603,186],{"class":102},[52,33605,33155],{"class":189},[52,33607,193],{"class":102},[52,33609,115],{"class":83},[52,33611,33162],{"class":87},[52,33613,115],{"class":83},[52,33615,3114],{"class":102},[14,33617,33618,33619,33622],{},"Only names that start with ",[49,33620,33621],{},"\"A\""," are added to the new list.",[14,33624,33625,33626,186],{},"If you want a step-by-step guide for this task, see ",[372,33627,33629],{"href":33628},"\u002Fhow-to\u002Fhow-to-filter-a-list-in-python\u002F","how to filter a list in Python",[37,33631,33633],{"id":33632},"beginner-mistakes-to-avoid","Beginner mistakes to avoid",[14,33635,33636],{},"Here are some common problems when filtering lists.",[639,33638,33640],{"id":33639},"forgetting-to-create-a-new-list","Forgetting to create a new list",[14,33642,33643,33644,33646],{},"This will fail because ",[49,33645,32811],{}," does not exist yet:",[42,33648,33650],{"className":44,"code":33649,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3]\n\nfor number in numbers:\n    if number > 1:\n        result.append(number)\n",[49,33651,33652,33672,33676,33688,33700],{"__ignoreMap":47},[52,33653,33654,33656,33658,33660,33662,33664,33666,33668,33670],{"class":54,"line":55},[52,33655,32092],{"class":62},[52,33657,80],{"class":79},[52,33659,18162],{"class":102},[52,33661,9200],{"class":121},[52,33663,199],{"class":102},[52,33665,9168],{"class":121},[52,33667,199],{"class":102},[52,33669,5261],{"class":121},[52,33671,1147],{"class":102},[52,33673,33674],{"class":54,"line":66},[52,33675,70],{"emptyLinePlaceholder":69},[52,33677,33678,33680,33682,33684,33686],{"class":54,"line":73},[52,33679,12265],{"class":58},[52,33681,32297],{"class":62},[52,33683,11213],{"class":58},[52,33685,32302],{"class":62},[52,33687,1330],{"class":102},[52,33689,33690,33692,33694,33696,33698],{"class":54,"line":94},[52,33691,1438],{"class":58},[52,33693,32297],{"class":62},[52,33695,13046],{"class":79},[52,33697,1518],{"class":121},[52,33699,1330],{"class":102},[52,33701,33702,33705,33707,33709,33711,33713],{"class":54,"line":106},[52,33703,33704],{"class":62},"        result",[52,33706,186],{"class":102},[52,33708,18748],{"class":189},[52,33710,193],{"class":102},[52,33712,27497],{"class":189},[52,33714,211],{"class":102},[14,33716,33717],{},"Create the list first:",[42,33719,33721],{"className":44,"code":33720,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3]\nresult = []\n\nfor number in numbers:\n    if number > 1:\n        result.append(number)\n\nprint(result)\n",[49,33722,33723,33743,33751,33755,33767,33779,33793,33797],{"__ignoreMap":47},[52,33724,33725,33727,33729,33731,33733,33735,33737,33739,33741],{"class":54,"line":55},[52,33726,32092],{"class":62},[52,33728,80],{"class":79},[52,33730,18162],{"class":102},[52,33732,9200],{"class":121},[52,33734,199],{"class":102},[52,33736,9168],{"class":121},[52,33738,199],{"class":102},[52,33740,5261],{"class":121},[52,33742,1147],{"class":102},[52,33744,33745,33747,33749],{"class":54,"line":66},[52,33746,32778],{"class":62},[52,33748,80],{"class":79},[52,33750,18590],{"class":102},[52,33752,33753],{"class":54,"line":73},[52,33754,70],{"emptyLinePlaceholder":69},[52,33756,33757,33759,33761,33763,33765],{"class":54,"line":94},[52,33758,12265],{"class":58},[52,33760,32297],{"class":62},[52,33762,11213],{"class":58},[52,33764,32302],{"class":62},[52,33766,1330],{"class":102},[52,33768,33769,33771,33773,33775,33777],{"class":54,"line":106},[52,33770,1438],{"class":58},[52,33772,32297],{"class":62},[52,33774,13046],{"class":79},[52,33776,1518],{"class":121},[52,33778,1330],{"class":102},[52,33780,33781,33783,33785,33787,33789,33791],{"class":54,"line":128},[52,33782,33704],{"class":62},[52,33784,186],{"class":102},[52,33786,18748],{"class":189},[52,33788,193],{"class":102},[52,33790,27497],{"class":189},[52,33792,211],{"class":102},[52,33794,33795],{"class":54,"line":148},[52,33796,70],{"emptyLinePlaceholder":69},[52,33798,33799,33801,33803,33805],{"class":54,"line":164},[52,33800,242],{"class":241},[52,33802,193],{"class":102},[52,33804,32811],{"class":189},[52,33806,211],{"class":102},[639,33808,10294,33810,10598,33812],{"id":33809},"using-instead-of",[49,33811,80],{},[49,33813,11062],{},[14,33815,33816],{},"This is wrong inside a condition:",[42,33818,33820],{"className":44,"code":33819,"language":46,"meta":47,"style":47},"if number = 2:\n",[49,33821,33822],{"__ignoreMap":47},[52,33823,33824,33826,33828,33830,33832],{"class":54,"line":55},[52,33825,1313],{"class":58},[52,33827,32297],{"class":62},[52,33829,80],{"class":79},[52,33831,9168],{"class":121},[52,33833,1330],{"class":102},[14,33835,3650,33836,33838],{},[49,33837,11062],{}," to compare values:",[42,33840,33842],{"className":44,"code":33841,"language":46,"meta":47,"style":47},"if number == 2:\n",[49,33843,33844],{"__ignoreMap":47},[52,33845,33846,33848,33850,33852,33854],{"class":54,"line":55},[52,33847,1313],{"class":58},[52,33849,32297],{"class":62},[52,33851,11062],{"class":79},[52,33853,9168],{"class":121},[52,33855,1330],{"class":102},[639,33857,33859],{"id":33858},"expecting-the-original-list-to-change-automatically","Expecting the original list to change automatically",[14,33861,33862],{},"Filtering usually creates a new list.",[42,33864,33866],{"className":44,"code":33865,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3, 4]\nresult = [n for n in numbers if n > 2]\n\nprint(numbers)\nprint(result)\n",[49,33867,33868,33892,33920,33924,33934],{"__ignoreMap":47},[52,33869,33870,33872,33874,33876,33878,33880,33882,33884,33886,33888,33890],{"class":54,"line":55},[52,33871,32092],{"class":62},[52,33873,80],{"class":79},[52,33875,18162],{"class":102},[52,33877,9200],{"class":121},[52,33879,199],{"class":102},[52,33881,9168],{"class":121},[52,33883,199],{"class":102},[52,33885,5261],{"class":121},[52,33887,199],{"class":102},[52,33889,32111],{"class":121},[52,33891,1147],{"class":102},[52,33893,33894,33896,33898,33900,33902,33904,33906,33908,33910,33912,33914,33916,33918],{"class":54,"line":66},[52,33895,32778],{"class":62},[52,33897,80],{"class":79},[52,33899,18162],{"class":102},[52,33901,32134],{"class":62},[52,33903,12265],{"class":58},[52,33905,32139],{"class":62},[52,33907,11213],{"class":58},[52,33909,32144],{"class":62},[52,33911,1313],{"class":58},[52,33913,32139],{"class":62},[52,33915,13046],{"class":79},[52,33917,9168],{"class":121},[52,33919,1147],{"class":102},[52,33921,33922],{"class":54,"line":73},[52,33923,70],{"emptyLinePlaceholder":69},[52,33925,33926,33928,33930,33932],{"class":54,"line":94},[52,33927,242],{"class":241},[52,33929,193],{"class":102},[52,33931,5324],{"class":189},[52,33933,211],{"class":102},[52,33935,33936,33938,33940,33942],{"class":54,"line":106},[52,33937,242],{"class":241},[52,33939,193],{"class":102},[52,33941,32811],{"class":189},[52,33943,211],{"class":102},[14,33945,33946],{},[5503,33947,5267],{},[42,33949,33951],{"className":44,"code":33950,"language":46,"meta":47,"style":47},"[1, 2, 3, 4]\n[3, 4]\n",[49,33952,33953,33973],{"__ignoreMap":47},[52,33954,33955,33957,33959,33961,33963,33965,33967,33969,33971],{"class":54,"line":55},[52,33956,250],{"class":102},[52,33958,9200],{"class":121},[52,33960,199],{"class":102},[52,33962,9168],{"class":121},[52,33964,199],{"class":102},[52,33966,5261],{"class":121},[52,33968,199],{"class":102},[52,33970,32111],{"class":121},[52,33972,1147],{"class":102},[52,33974,33975,33977,33979,33981,33983],{"class":54,"line":66},[52,33976,250],{"class":102},[52,33978,7508],{"class":121},[52,33980,199],{"class":102},[52,33982,32111],{"class":121},[52,33984,1147],{"class":102},[14,33986,33987,33988,186],{},"The original list is still ",[49,33989,33990],{},"[1, 2, 3, 4]",[639,33992,33994],{"id":33993},"writing-a-condition-that-returns-the-wrong-type","Writing a condition that returns the wrong type",[14,33996,33997,33998,857,34000,186],{},"Conditions should evaluate to ",[49,33999,6233],{},[49,34001,6241],{},[14,34003,34004],{},"Good:",[42,34006,34008],{"className":44,"code":34007,"language":46,"meta":47,"style":47},"if number > 3:\n",[49,34009,34010],{"__ignoreMap":47},[52,34011,34012,34014,34016,34018,34020],{"class":54,"line":55},[52,34013,1313],{"class":58},[52,34015,32297],{"class":62},[52,34017,13046],{"class":79},[52,34019,5261],{"class":121},[52,34021,1330],{"class":102},[14,34023,34024],{},"Less useful for filtering logic:",[42,34026,34028],{"className":44,"code":34027,"language":46,"meta":47,"style":47},"if number + 3:\n",[49,34029,34030],{"__ignoreMap":47},[52,34031,34032,34034,34036,34038,34040],{"class":54,"line":55},[52,34033,1313],{"class":58},[52,34035,32297],{"class":62},[52,34037,9881],{"class":79},[52,34039,5261],{"class":121},[52,34041,1330],{"class":102},[14,34043,34044],{},"That second example produces a value, but it does not clearly express the rule you want.",[639,34046,34048],{"id":34047},"confusing-filtering-with-sorting","Confusing filtering with sorting",[14,34050,34051],{},"Filtering removes items that do not match a condition.",[14,34053,34054],{},"Sorting changes the order of items.",[14,34056,34057],{},"These are different tasks.",[37,34059,34061],{"id":34060},"common-causes-of-filtering-problems","Common causes of filtering problems",[14,34063,34064],{},"Many beginner errors come from a small number of causes:",[309,34066,34067,34070,34076,34079],{},[24,34068,34069],{},"Trying to remove items from the same list while looping over it",[24,34071,10294,34072,34075],{},[49,34073,34074],{},"append()"," on a variable that was not created as a list",[24,34077,34078],{},"Writing an invalid condition inside the loop or comprehension",[24,34080,34081],{},"Mixing numbers and strings in a comparison without conversion",[14,34083,34084],{},"For example, this can cause problems:",[42,34086,34088],{"className":44,"code":34087,"language":46,"meta":47,"style":47},"items = [1, \"2\", 3]\n\nfor item in items:\n    if item > 1:\n        print(item)\n",[49,34089,34090,34114,34118,34131,34143],{"__ignoreMap":47},[52,34091,34092,34094,34096,34098,34100,34102,34104,34106,34108,34110,34112],{"class":54,"line":55},[52,34093,33220],{"class":62},[52,34095,80],{"class":79},[52,34097,18162],{"class":102},[52,34099,9200],{"class":121},[52,34101,199],{"class":102},[52,34103,84],{"class":83},[52,34105,9825],{"class":87},[52,34107,115],{"class":83},[52,34109,199],{"class":102},[52,34111,5261],{"class":121},[52,34113,1147],{"class":102},[52,34115,34116],{"class":54,"line":66},[52,34117,70],{"emptyLinePlaceholder":69},[52,34119,34120,34122,34124,34126,34129],{"class":54,"line":73},[52,34121,12265],{"class":58},[52,34123,30192],{"class":62},[52,34125,11213],{"class":58},[52,34127,34128],{"class":62}," items",[52,34130,1330],{"class":102},[52,34132,34133,34135,34137,34139,34141],{"class":54,"line":94},[52,34134,1438],{"class":58},[52,34136,30192],{"class":62},[52,34138,13046],{"class":79},[52,34140,1518],{"class":121},[52,34142,1330],{"class":102},[52,34144,34145,34147,34149,34151],{"class":54,"line":106},[52,34146,1470],{"class":241},[52,34148,193],{"class":102},[52,34150,30322],{"class":189},[52,34152,211],{"class":102},[14,34154,34155],{},"In this list, some items are numbers and one is a string. Python cannot always compare them directly in a useful way.",[37,34157,14291],{"id":14290},[14,34159,34160],{},"If your filtering code does not work, print values as the loop runs.",[14,34162,34163],{},"Useful debug lines:",[42,34165,34167],{"className":44,"code":34166,"language":46,"meta":47,"style":47},"print(items)\nprint(item)\nprint(result)\nprint(type(item))\nprint(item % 2 == 0)\n",[49,34168,34169,34179,34189,34199,34213],{"__ignoreMap":47},[52,34170,34171,34173,34175,34177],{"class":54,"line":55},[52,34172,242],{"class":241},[52,34174,193],{"class":102},[52,34176,11220],{"class":189},[52,34178,211],{"class":102},[52,34180,34181,34183,34185,34187],{"class":54,"line":66},[52,34182,242],{"class":241},[52,34184,193],{"class":102},[52,34186,30322],{"class":189},[52,34188,211],{"class":102},[52,34190,34191,34193,34195,34197],{"class":54,"line":73},[52,34192,242],{"class":241},[52,34194,193],{"class":102},[52,34196,32811],{"class":189},[52,34198,211],{"class":102},[52,34200,34201,34203,34205,34207,34209,34211],{"class":54,"line":94},[52,34202,242],{"class":241},[52,34204,193],{"class":102},[52,34206,4198],{"class":4733},[52,34208,193],{"class":102},[52,34210,30322],{"class":189},[52,34212,8886],{"class":102},[52,34214,34215,34217,34219,34221,34223,34225,34227,34229],{"class":54,"line":106},[52,34216,242],{"class":241},[52,34218,193],{"class":102},[52,34220,33270],{"class":189},[52,34222,32151],{"class":79},[52,34224,9168],{"class":121},[52,34226,1324],{"class":79},[52,34228,13049],{"class":121},[52,34230,211],{"class":102},[14,34232,4259],{},[42,34234,34236],{"className":44,"code":34235,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3, 4]\nresult = []\n\nfor item in numbers:\n    print(\"Current item:\", item)\n    print(\"Is even?\", item % 2 == 0)\n\n    if item % 2 == 0:\n        result.append(item)\n\nprint(\"Final result:\", result)\n",[49,34237,34238,34262,34270,34274,34286,34305,34332,34336,34352,34366,34370],{"__ignoreMap":47},[52,34239,34240,34242,34244,34246,34248,34250,34252,34254,34256,34258,34260],{"class":54,"line":55},[52,34241,32092],{"class":62},[52,34243,80],{"class":79},[52,34245,18162],{"class":102},[52,34247,9200],{"class":121},[52,34249,199],{"class":102},[52,34251,9168],{"class":121},[52,34253,199],{"class":102},[52,34255,5261],{"class":121},[52,34257,199],{"class":102},[52,34259,32111],{"class":121},[52,34261,1147],{"class":102},[52,34263,34264,34266,34268],{"class":54,"line":66},[52,34265,32778],{"class":62},[52,34267,80],{"class":79},[52,34269,18590],{"class":102},[52,34271,34272],{"class":54,"line":73},[52,34273,70],{"emptyLinePlaceholder":69},[52,34275,34276,34278,34280,34282,34284],{"class":54,"line":94},[52,34277,12265],{"class":58},[52,34279,30192],{"class":62},[52,34281,11213],{"class":58},[52,34283,32302],{"class":62},[52,34285,1330],{"class":102},[52,34287,34288,34290,34292,34294,34297,34299,34301,34303],{"class":54,"line":106},[52,34289,1599],{"class":241},[52,34291,193],{"class":102},[52,34293,115],{"class":83},[52,34295,34296],{"class":87},"Current item:",[52,34298,115],{"class":83},[52,34300,199],{"class":102},[52,34302,30211],{"class":189},[52,34304,211],{"class":102},[52,34306,34307,34309,34311,34313,34316,34318,34320,34322,34324,34326,34328,34330],{"class":54,"line":128},[52,34308,1599],{"class":241},[52,34310,193],{"class":102},[52,34312,115],{"class":83},[52,34314,34315],{"class":87},"Is even?",[52,34317,115],{"class":83},[52,34319,199],{"class":102},[52,34321,30192],{"class":189},[52,34323,32151],{"class":79},[52,34325,9168],{"class":121},[52,34327,1324],{"class":79},[52,34329,13049],{"class":121},[52,34331,211],{"class":102},[52,34333,34334],{"class":54,"line":148},[52,34335,70],{"emptyLinePlaceholder":69},[52,34337,34338,34340,34342,34344,34346,34348,34350],{"class":54,"line":164},[52,34339,1438],{"class":58},[52,34341,30192],{"class":62},[52,34343,32151],{"class":79},[52,34345,9168],{"class":121},[52,34347,1324],{"class":79},[52,34349,13049],{"class":121},[52,34351,1330],{"class":102},[52,34353,34354,34356,34358,34360,34362,34364],{"class":54,"line":170},[52,34355,33704],{"class":62},[52,34357,186],{"class":102},[52,34359,18748],{"class":189},[52,34361,193],{"class":102},[52,34363,30322],{"class":189},[52,34365,211],{"class":102},[52,34367,34368],{"class":54,"line":175},[52,34369,70],{"emptyLinePlaceholder":69},[52,34371,34372,34374,34376,34378,34381,34383,34385,34387],{"class":54,"line":214},[52,34373,242],{"class":241},[52,34375,193],{"class":102},[52,34377,115],{"class":83},[52,34379,34380],{"class":87},"Final result:",[52,34382,115],{"class":83},[52,34384,199],{"class":102},[52,34386,27130],{"class":189},[52,34388,211],{"class":102},[14,34390,25050],{},[309,34392,34393,34396,34403],{},[24,34394,34395],{},"what each item is",[24,34397,34398,34399,857,34401],{},"whether the condition is ",[49,34400,6233],{},[49,34402,6241],{},[24,34404,34405],{},"what gets added to the result list",[37,34407,1942],{"id":1941},[639,34409,34411],{"id":34410},"what-is-the-easiest-way-to-filter-a-list-in-python","What is the easiest way to filter a list in Python?",[14,34413,34414,34415,34417,34418,34420],{},"For beginners, use a ",[49,34416,12265],{}," loop with an ",[49,34419,1313],{}," statement. After that, learn the list comprehension version.",[639,34422,34424],{"id":34423},"does-filtering-change-the-original-list","Does filtering change the original list?",[14,34426,34427],{},"Not unless you assign the result back to the same variable. Usually filtering creates a new list.",[639,34429,3638,34431,34434],{"id":34430},"should-i-use-filter-for-this-page",[49,34432,34433],{},"filter()"," for this page?",[14,34436,34437],{},"No. This example focuses on loops and list comprehensions because they are easier for beginners to read.",[639,34439,34441],{"id":34440},"can-i-filter-a-list-of-strings","Can I filter a list of strings?",[14,34443,34444],{},"Yes. You can keep only strings that match a condition, such as containing a word or starting with a letter.",[37,34446,2005],{"id":2004},[309,34448,34449,34454,34459,34464,34469],{},[24,34450,34451],{},[372,34452,34453],{"href":33628},"How to filter a list in Python",[24,34455,34456],{},[372,34457,34458],{"href":32725},"How to use list comprehensions in Python",[24,34460,34461],{},[372,34462,34463],{"href":32078},"Python if statements explained",[24,34465,34466],{},[372,34467,34468],{"href":32073},"Python for loops explained",[24,34470,34471],{},[372,34472,34473],{"href":32516},"Python list append() method",[14,34475,34476],{},"Try changing the examples in this page to make your own filters:",[309,34478,34479,34484,34487,34493],{},[24,34480,34481,34482],{},"keep numbers greater than ",[49,34483,3373],{},[24,34485,34486],{},"keep only odd numbers",[24,34488,34489,34490],{},"keep strings that contain ",[49,34491,34492],{},"\"cat\"",[24,34494,34495,34496],{},"keep names that start with ",[49,34497,34498],{},"\"B\"",[14,34500,34501],{},"That is the main skill: write a condition, test each item, and keep only the ones that match.",[2034,34503,34504],{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":34506},[34507,34508,34509,34512,34513,34520,34521,34529,34530,34531,34538],{"id":39,"depth":66,"text":40},{"id":2267,"depth":66,"text":2268},{"id":32217,"depth":66,"text":32218,"children":34510},[34511],{"id":32448,"depth":73,"text":32449},{"id":32522,"depth":66,"text":32523},{"id":32729,"depth":66,"text":32730,"children":34514},[34515,34516,34517,34518,34519],{"id":32736,"depth":73,"text":32737},{"id":32838,"depth":73,"text":32839},{"id":32949,"depth":73,"text":32950},{"id":33079,"depth":73,"text":33080},{"id":33209,"depth":73,"text":33210},{"id":33340,"depth":66,"text":33341},{"id":33632,"depth":66,"text":33633,"children":34522},[34523,34524,34526,34527,34528],{"id":33639,"depth":73,"text":33640},{"id":33809,"depth":73,"text":34525},"Using = instead of ==",{"id":33858,"depth":73,"text":33859},{"id":33993,"depth":73,"text":33994},{"id":34047,"depth":73,"text":34048},{"id":34060,"depth":66,"text":34061},{"id":14290,"depth":66,"text":14291},{"id":1941,"depth":66,"text":1942,"children":34532},[34533,34534,34535,34537],{"id":34410,"depth":73,"text":34411},{"id":34423,"depth":73,"text":34424},{"id":34430,"depth":73,"text":34536},"Should I use filter() for this page?",{"id":34440,"depth":73,"text":34441},{"id":2004,"depth":66,"text":2005},"Master python filter data from a list example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-filter-data-from-a-list-example",{"title":32040,"description":34539},"examples\u002Fpython-filter-data-from-a-list-example","TwoCLUPgkjPodhMrC2xGM4mKiCLlwV8DZIcAzLqCfNc",{"id":34546,"title":34547,"body":34548,"description":36591,"extension":2075,"meta":36592,"navigation":69,"path":36593,"seo":36594,"stem":36595,"__hash__":36596},"content\u002Fexamples\u002Fpython-folder-size-calculator-example.md","Python Folder Size Calculator Example",{"type":7,"value":34549,"toc":36558},[34550,34553,34558,34561,34727,34730,34732,34735,34758,34761,34765,34768,34784,34789,34793,34796,34955,34957,34983,34989,34992,34994,35026,35029,35055,35061,35063,35069,35073,35079,35241,35243,35270,35273,35277,35280,35634,35637,35643,35646,35650,35653,35670,35674,35679,35682,35742,35747,35753,35757,35763,35768,35772,35777,35780,35863,35873,35880,35885,35890,35989,35993,35998,36001,36190,36196,36200,36203,36298,36302,36305,36321,36324,36482,36485,36487,36491,36497,36501,36504,36508,36513,36517,36523,36525,36552,36555],[10,34551,34547],{"id":34552},"python-folder-size-calculator-example",[14,34554,34555,34556,13338],{},"Build a simple Python script that calculates the total size of files inside a folder. This example helps beginners practice working with file paths, loops, and the ",[49,34557,15848],{},[14,34559,34560],{},"If you want the shortest working version first, start here:",[42,34562,34564],{"className":44,"code":34563,"language":46,"meta":47,"style":47},"import os\n\nfolder_path = \".\"\ntotal_size = 0\n\nfor name in os.listdir(folder_path):\n    path = os.path.join(folder_path, name)\n    if os.path.isfile(path):\n        total_size += os.path.getsize(path)\n\nprint(f\"Total size: {total_size} bytes\")\n",[49,34565,34566,34572,34576,34589,34599,34603,34626,34654,34675,34700,34704],{"__ignoreMap":47},[52,34567,34568,34570],{"class":54,"line":55},[52,34569,59],{"class":58},[52,34571,15839],{"class":62},[52,34573,34574],{"class":54,"line":66},[52,34575,70],{"emptyLinePlaceholder":69},[52,34577,34578,34581,34583,34585,34587],{"class":54,"line":73},[52,34579,34580],{"class":62},"folder_path ",[52,34582,80],{"class":79},[52,34584,84],{"class":83},[52,34586,186],{"class":87},[52,34588,91],{"class":83},[52,34590,34591,34594,34596],{"class":54,"line":94},[52,34592,34593],{"class":62},"total_size ",[52,34595,80],{"class":79},[52,34597,34598],{"class":121}," 0\n",[52,34600,34601],{"class":54,"line":106},[52,34602,70],{"emptyLinePlaceholder":69},[52,34604,34605,34607,34609,34611,34614,34616,34619,34621,34624],{"class":54,"line":128},[52,34606,12265],{"class":58},[52,34608,11292],{"class":62},[52,34610,11213],{"class":58},[52,34612,34613],{"class":62}," os",[52,34615,186],{"class":102},[52,34617,34618],{"class":189},"listdir",[52,34620,193],{"class":102},[52,34622,34623],{"class":189},"folder_path",[52,34625,3114],{"class":102},[52,34627,34628,34631,34633,34635,34637,34639,34641,34644,34646,34648,34650,34652],{"class":54,"line":148},[52,34629,34630],{"class":62},"    path ",[52,34632,80],{"class":79},[52,34634,34613],{"class":62},[52,34636,186],{"class":102},[52,34638,15882],{"class":1320},[52,34640,186],{"class":102},[52,34642,34643],{"class":189},"join",[52,34645,193],{"class":102},[52,34647,34623],{"class":189},[52,34649,199],{"class":102},[52,34651,7019],{"class":189},[52,34653,211],{"class":102},[52,34655,34656,34658,34660,34662,34664,34666,34669,34671,34673],{"class":54,"line":164},[52,34657,1438],{"class":58},[52,34659,34613],{"class":62},[52,34661,186],{"class":102},[52,34663,15882],{"class":1320},[52,34665,186],{"class":102},[52,34667,34668],{"class":189},"isfile",[52,34670,193],{"class":102},[52,34672,15882],{"class":189},[52,34674,3114],{"class":102},[52,34676,34677,34680,34683,34685,34687,34689,34691,34694,34696,34698],{"class":54,"line":170},[52,34678,34679],{"class":62},"        total_size ",[52,34681,34682],{"class":79},"+=",[52,34684,34613],{"class":62},[52,34686,186],{"class":102},[52,34688,15882],{"class":1320},[52,34690,186],{"class":102},[52,34692,34693],{"class":189},"getsize",[52,34695,193],{"class":102},[52,34697,15882],{"class":189},[52,34699,211],{"class":102},[52,34701,34702],{"class":54,"line":175},[52,34703,70],{"emptyLinePlaceholder":69},[52,34705,34706,34708,34710,34712,34715,34717,34720,34722,34725],{"class":54,"line":214},[52,34707,242],{"class":241},[52,34709,193],{"class":102},[52,34711,1476],{"class":1475},[52,34713,34714],{"class":87},"\"Total size: ",[52,34716,1482],{"class":121},[52,34718,34719],{"class":189},"total_size",[52,34721,1488],{"class":121},[52,34723,34724],{"class":87}," bytes\"",[52,34726,211],{"class":102},[14,34728,34729],{},"This quick version adds the size of files in one folder only. A later section expands it to include subfolders.",[37,34731,323],{"id":322},[14,34733,34734],{},"This example shows how to:",[309,34736,34737,34740,34752,34755],{},[24,34738,34739],{},"Calculate the total size of files in a folder",[24,34741,34742,34743,2587,34746,11556,34749],{},"Combine ",[49,34744,34745],{},"os.listdir()",[49,34747,34748],{},"os.path.join()",[49,34750,34751],{},"os.path.getsize()",[24,34753,34754],{},"Understand how file system paths work in Python",[24,34756,34757],{},"Solve one practical task with a small script",[14,34759,34760],{},"This is a good beginner project because it uses a loop, file paths, and built-in standard library tools in one place.",[37,34762,34764],{"id":34763},"what-you-need-to-know-first","What you need to know first",[14,34766,34767],{},"Before reading the code, it helps to understand a few basics:",[309,34769,34770,34773,34776,34781],{},[24,34771,34772],{},"A folder can contain files and subfolders",[24,34774,34775],{},"A file path tells Python where a file or folder is",[24,34777,358,34778,34780],{},[49,34779,15848],{}," module gives you tools for working with files and folders",[24,34782,34783],{},"A loop lets you process items one by one",[14,34785,34786,34787,186],{},"If file paths still feel confusing, see ",[372,34788,28723],{"href":28722},[37,34790,34792],{"id":34791},"simple-version-total-size-of-files-in-one-folder","Simple version: total size of files in one folder",[14,34794,34795],{},"This version checks one folder only. It does not go inside subfolders.",[42,34797,34799],{"className":44,"code":34798,"language":46,"meta":47,"style":47},"import os\n\nfolder_path = \".\"\n\ntotal_size = 0\n\nfor name in os.listdir(folder_path):\n    path = os.path.join(folder_path, name)\n\n    if os.path.isfile(path):\n        total_size += os.path.getsize(path)\n\nprint(f\"Total size: {total_size} bytes\")\n",[49,34800,34801,34807,34811,34823,34827,34835,34839,34859,34885,34889,34909,34931,34935],{"__ignoreMap":47},[52,34802,34803,34805],{"class":54,"line":55},[52,34804,59],{"class":58},[52,34806,15839],{"class":62},[52,34808,34809],{"class":54,"line":66},[52,34810,70],{"emptyLinePlaceholder":69},[52,34812,34813,34815,34817,34819,34821],{"class":54,"line":73},[52,34814,34580],{"class":62},[52,34816,80],{"class":79},[52,34818,84],{"class":83},[52,34820,186],{"class":87},[52,34822,91],{"class":83},[52,34824,34825],{"class":54,"line":94},[52,34826,70],{"emptyLinePlaceholder":69},[52,34828,34829,34831,34833],{"class":54,"line":106},[52,34830,34593],{"class":62},[52,34832,80],{"class":79},[52,34834,34598],{"class":121},[52,34836,34837],{"class":54,"line":128},[52,34838,70],{"emptyLinePlaceholder":69},[52,34840,34841,34843,34845,34847,34849,34851,34853,34855,34857],{"class":54,"line":148},[52,34842,12265],{"class":58},[52,34844,11292],{"class":62},[52,34846,11213],{"class":58},[52,34848,34613],{"class":62},[52,34850,186],{"class":102},[52,34852,34618],{"class":189},[52,34854,193],{"class":102},[52,34856,34623],{"class":189},[52,34858,3114],{"class":102},[52,34860,34861,34863,34865,34867,34869,34871,34873,34875,34877,34879,34881,34883],{"class":54,"line":164},[52,34862,34630],{"class":62},[52,34864,80],{"class":79},[52,34866,34613],{"class":62},[52,34868,186],{"class":102},[52,34870,15882],{"class":1320},[52,34872,186],{"class":102},[52,34874,34643],{"class":189},[52,34876,193],{"class":102},[52,34878,34623],{"class":189},[52,34880,199],{"class":102},[52,34882,7019],{"class":189},[52,34884,211],{"class":102},[52,34886,34887],{"class":54,"line":170},[52,34888,70],{"emptyLinePlaceholder":69},[52,34890,34891,34893,34895,34897,34899,34901,34903,34905,34907],{"class":54,"line":175},[52,34892,1438],{"class":58},[52,34894,34613],{"class":62},[52,34896,186],{"class":102},[52,34898,15882],{"class":1320},[52,34900,186],{"class":102},[52,34902,34668],{"class":189},[52,34904,193],{"class":102},[52,34906,15882],{"class":189},[52,34908,3114],{"class":102},[52,34910,34911,34913,34915,34917,34919,34921,34923,34925,34927,34929],{"class":54,"line":214},[52,34912,34679],{"class":62},[52,34914,34682],{"class":79},[52,34916,34613],{"class":62},[52,34918,186],{"class":102},[52,34920,15882],{"class":1320},[52,34922,186],{"class":102},[52,34924,34693],{"class":189},[52,34926,193],{"class":102},[52,34928,15882],{"class":189},[52,34930,211],{"class":102},[52,34932,34933],{"class":54,"line":233},[52,34934,70],{"emptyLinePlaceholder":69},[52,34936,34937,34939,34941,34943,34945,34947,34949,34951,34953],{"class":54,"line":238},[52,34938,242],{"class":241},[52,34940,193],{"class":102},[52,34942,1476],{"class":1475},[52,34944,34714],{"class":87},[52,34946,1482],{"class":121},[52,34948,34719],{"class":189},[52,34950,1488],{"class":121},[52,34952,34724],{"class":87},[52,34954,211],{"class":102},[639,34956,24095],{"id":24094},[309,34958,34959,34965,34971,34977],{},[24,34960,34961,34964],{},[49,34962,34963],{},"os.listdir(folder_path)"," gets the names of items in the folder",[24,34966,34967,34970],{},[49,34968,34969],{},"os.path.join(folder_path, name)"," builds the full path",[24,34972,34973,34976],{},[49,34974,34975],{},"os.path.isfile(path)"," checks whether the item is a file",[24,34978,34979,34982],{},[49,34980,34981],{},"os.path.getsize(path)"," gets the file size in bytes",[639,34984,24316,34986,34988],{"id":34985},"why-ospathjoin-matters",[49,34987,34748],{}," matters",[14,34990,34991],{},"Do not build paths by hand with string addition. This can break on different operating systems.",[14,34993,34004],{},[42,34995,34997],{"className":44,"code":34996,"language":46,"meta":47,"style":47},"path = os.path.join(folder_path, name)\n",[49,34998,34999],{"__ignoreMap":47},[52,35000,35001,35004,35006,35008,35010,35012,35014,35016,35018,35020,35022,35024],{"class":54,"line":55},[52,35002,35003],{"class":62},"path ",[52,35005,80],{"class":79},[52,35007,34613],{"class":62},[52,35009,186],{"class":102},[52,35011,15882],{"class":1320},[52,35013,186],{"class":102},[52,35015,34643],{"class":189},[52,35017,193],{"class":102},[52,35019,34623],{"class":189},[52,35021,199],{"class":102},[52,35023,7019],{"class":189},[52,35025,211],{"class":102},[14,35027,35028],{},"Less reliable:",[42,35030,35032],{"className":44,"code":35031,"language":46,"meta":47,"style":47},"path = folder_path + \"\u002F\" + name\n",[49,35033,35034],{"__ignoreMap":47},[52,35035,35036,35038,35040,35043,35045,35047,35049,35051,35053],{"class":54,"line":55},[52,35037,35003],{"class":62},[52,35039,80],{"class":79},[52,35041,35042],{"class":62}," folder_path ",[52,35044,9881],{"class":79},[52,35046,84],{"class":83},[52,35048,10537],{"class":87},[52,35050,115],{"class":83},[52,35052,5234],{"class":79},[52,35054,7041],{"class":62},[14,35056,35057,35058,186],{},"If you want a deeper explanation, see ",[372,35059,35060],{"href":31969},"os.path.join() explained",[639,35062,2510],{"id":2509},[42,35064,35067],{"className":35065,"code":35066,"language":955,"meta":47},[953],"Total size: 15432 bytes\n",[49,35068,35066],{"__ignoreMap":47},[37,35070,35072],{"id":35071},"recursive-version-include-subfolders","Recursive version: include subfolders",[14,35074,35075,35076,186],{},"The basic version only counts files in the current folder. If you want the total size of a folder and everything inside it, including subfolders, use ",[49,35077,35078],{},"os.walk()",[42,35080,35082],{"className":44,"code":35081,"language":46,"meta":47,"style":47},"import os\n\nfolder_path = \".\"\n\ntotal_size = 0\n\nfor root, dirs, files in os.walk(folder_path):\n    for name in files:\n        path = os.path.join(root, name)\n        total_size += os.path.getsize(path)\n\nprint(f\"Total size: {total_size} bytes\")\n",[49,35083,35084,35090,35094,35106,35110,35118,35122,35154,35167,35195,35217,35221],{"__ignoreMap":47},[52,35085,35086,35088],{"class":54,"line":55},[52,35087,59],{"class":58},[52,35089,15839],{"class":62},[52,35091,35092],{"class":54,"line":66},[52,35093,70],{"emptyLinePlaceholder":69},[52,35095,35096,35098,35100,35102,35104],{"class":54,"line":73},[52,35097,34580],{"class":62},[52,35099,80],{"class":79},[52,35101,84],{"class":83},[52,35103,186],{"class":87},[52,35105,91],{"class":83},[52,35107,35108],{"class":54,"line":94},[52,35109,70],{"emptyLinePlaceholder":69},[52,35111,35112,35114,35116],{"class":54,"line":106},[52,35113,34593],{"class":62},[52,35115,80],{"class":79},[52,35117,34598],{"class":121},[52,35119,35120],{"class":54,"line":128},[52,35121,70],{"emptyLinePlaceholder":69},[52,35123,35124,35126,35129,35131,35134,35136,35139,35141,35143,35145,35148,35150,35152],{"class":54,"line":148},[52,35125,12265],{"class":58},[52,35127,35128],{"class":62}," root",[52,35130,199],{"class":102},[52,35132,35133],{"class":62}," dirs",[52,35135,199],{"class":102},[52,35137,35138],{"class":62}," files ",[52,35140,11213],{"class":58},[52,35142,34613],{"class":62},[52,35144,186],{"class":102},[52,35146,35147],{"class":189},"walk",[52,35149,193],{"class":102},[52,35151,34623],{"class":189},[52,35153,3114],{"class":102},[52,35155,35156,35158,35160,35162,35165],{"class":54,"line":164},[52,35157,11773],{"class":58},[52,35159,11292],{"class":62},[52,35161,11213],{"class":58},[52,35163,35164],{"class":62}," files",[52,35166,1330],{"class":102},[52,35168,35169,35172,35174,35176,35178,35180,35182,35184,35186,35189,35191,35193],{"class":54,"line":170},[52,35170,35171],{"class":62},"        path ",[52,35173,80],{"class":79},[52,35175,34613],{"class":62},[52,35177,186],{"class":102},[52,35179,15882],{"class":1320},[52,35181,186],{"class":102},[52,35183,34643],{"class":189},[52,35185,193],{"class":102},[52,35187,35188],{"class":189},"root",[52,35190,199],{"class":102},[52,35192,7019],{"class":189},[52,35194,211],{"class":102},[52,35196,35197,35199,35201,35203,35205,35207,35209,35211,35213,35215],{"class":54,"line":175},[52,35198,34679],{"class":62},[52,35200,34682],{"class":79},[52,35202,34613],{"class":62},[52,35204,186],{"class":102},[52,35206,15882],{"class":1320},[52,35208,186],{"class":102},[52,35210,34693],{"class":189},[52,35212,193],{"class":102},[52,35214,15882],{"class":189},[52,35216,211],{"class":102},[52,35218,35219],{"class":54,"line":214},[52,35220,70],{"emptyLinePlaceholder":69},[52,35222,35223,35225,35227,35229,35231,35233,35235,35237,35239],{"class":54,"line":233},[52,35224,242],{"class":241},[52,35226,193],{"class":102},[52,35228,1476],{"class":1475},[52,35230,34714],{"class":87},[52,35232,1482],{"class":121},[52,35234,34719],{"class":189},[52,35236,1488],{"class":121},[52,35238,34724],{"class":87},[52,35240,211],{"class":102},[639,35242,9910],{"id":9909},[309,35244,35245,35251,35256,35261,35267],{},[24,35246,35247,35250],{},[49,35248,35249],{},"os.walk(folder_path)"," goes through the folder and all subfolders",[24,35252,35253,35255],{},[49,35254,35188],{}," is the current folder being processed",[24,35257,35258,35260],{},[49,35259,2607],{}," is a list of file names in that folder",[24,35262,35263,35264],{},"Each file path is built with ",[49,35265,35266],{},"os.path.join(root, name)",[24,35268,35269],{},"The script adds the size of every file it finds",[14,35271,35272],{},"This version is usually more useful for real folder size checks.",[37,35274,35276],{"id":35275},"show-the-size-in-a-readable-format","Show the size in a readable format",[14,35278,35279],{},"File sizes in bytes are correct, but they are not always easy to read. You can add a helper function to show sizes in KB, MB, or GB.",[42,35281,35283],{"className":44,"code":35282,"language":46,"meta":47,"style":47},"import os\n\ndef format_size(size_in_bytes):\n    units = [\"bytes\", \"KB\", \"MB\", \"GB\", \"TB\"]\n    size = float(size_in_bytes)\n\n    for unit in units:\n        if size \u003C 1024 or unit == units[-1]:\n            return f\"{size:.2f} {unit}\"\n        size \u002F= 1024\n\nfolder_path = \".\"\ntotal_size = 0\n\nfor root, dirs, files in os.walk(folder_path):\n    for name in files:\n        path = os.path.join(root, name)\n        total_size += os.path.getsize(path)\n\nprint(f\"Folder: {folder_path}\")\nprint(f\"Total size: {format_size(total_size)}\")\n",[49,35284,35285,35291,35295,35309,35363,35379,35383,35397,35427,35455,35466,35470,35482,35490,35494,35522,35534,35560,35582,35586,35607],{"__ignoreMap":47},[52,35286,35287,35289],{"class":54,"line":55},[52,35288,59],{"class":58},[52,35290,15839],{"class":62},[52,35292,35293],{"class":54,"line":66},[52,35294,70],{"emptyLinePlaceholder":69},[52,35296,35297,35299,35302,35304,35307],{"class":54,"line":73},[52,35298,7410],{"class":1475},[52,35300,35301],{"class":7065}," format_size",[52,35303,193],{"class":102},[52,35305,35306],{"class":7018},"size_in_bytes",[52,35308,3114],{"class":102},[52,35310,35311,35314,35316,35318,35320,35323,35325,35327,35329,35332,35334,35336,35338,35341,35343,35345,35347,35350,35352,35354,35356,35359,35361],{"class":54,"line":94},[52,35312,35313],{"class":62},"    units ",[52,35315,80],{"class":79},[52,35317,18162],{"class":102},[52,35319,115],{"class":83},[52,35321,35322],{"class":87},"bytes",[52,35324,115],{"class":83},[52,35326,199],{"class":102},[52,35328,84],{"class":83},[52,35330,35331],{"class":87},"KB",[52,35333,115],{"class":83},[52,35335,199],{"class":102},[52,35337,84],{"class":83},[52,35339,35340],{"class":87},"MB",[52,35342,115],{"class":83},[52,35344,199],{"class":102},[52,35346,84],{"class":83},[52,35348,35349],{"class":87},"GB",[52,35351,115],{"class":83},[52,35353,199],{"class":102},[52,35355,84],{"class":83},[52,35357,35358],{"class":87},"TB",[52,35360,115],{"class":83},[52,35362,1147],{"class":102},[52,35364,35365,35368,35370,35373,35375,35377],{"class":54,"line":106},[52,35366,35367],{"class":62},"    size ",[52,35369,80],{"class":79},[52,35371,35372],{"class":4733}," float",[52,35374,193],{"class":102},[52,35376,35306],{"class":189},[52,35378,211],{"class":102},[52,35380,35381],{"class":54,"line":128},[52,35382,70],{"emptyLinePlaceholder":69},[52,35384,35385,35387,35390,35392,35395],{"class":54,"line":148},[52,35386,11773],{"class":58},[52,35388,35389],{"class":62}," unit ",[52,35391,11213],{"class":58},[52,35393,35394],{"class":62}," units",[52,35396,1330],{"class":102},[52,35398,35399,35401,35404,35406,35409,35412,35414,35416,35418,35420,35422,35424],{"class":54,"line":164},[52,35400,11168],{"class":58},[52,35402,35403],{"class":62}," size ",[52,35405,15294],{"class":79},[52,35407,35408],{"class":121}," 1024",[52,35410,35411],{"class":79}," or",[52,35413,35389],{"class":62},[52,35415,11062],{"class":79},[52,35417,35394],{"class":62},[52,35419,250],{"class":102},[52,35421,5889],{"class":79},[52,35423,9200],{"class":121},[52,35425,35426],{"class":102},"]:\n",[52,35428,35429,35432,35434,35436,35438,35441,35444,35446,35448,35451,35453],{"class":54,"line":170},[52,35430,35431],{"class":58},"            return",[52,35433,6126],{"class":1475},[52,35435,115],{"class":87},[52,35437,1482],{"class":121},[52,35439,35440],{"class":62},"size",[52,35442,35443],{"class":1475},":.2f",[52,35445,1488],{"class":121},[52,35447,3000],{"class":121},[52,35449,35450],{"class":62},"unit",[52,35452,1488],{"class":121},[52,35454,91],{"class":87},[52,35456,35457,35460,35463],{"class":54,"line":175},[52,35458,35459],{"class":62},"        size ",[52,35461,35462],{"class":79},"\u002F=",[52,35464,35465],{"class":121}," 1024\n",[52,35467,35468],{"class":54,"line":214},[52,35469,70],{"emptyLinePlaceholder":69},[52,35471,35472,35474,35476,35478,35480],{"class":54,"line":233},[52,35473,34580],{"class":62},[52,35475,80],{"class":79},[52,35477,84],{"class":83},[52,35479,186],{"class":87},[52,35481,91],{"class":83},[52,35483,35484,35486,35488],{"class":54,"line":238},[52,35485,34593],{"class":62},[52,35487,80],{"class":79},[52,35489,34598],{"class":121},[52,35491,35492],{"class":54,"line":272},[52,35493,70],{"emptyLinePlaceholder":69},[52,35495,35496,35498,35500,35502,35504,35506,35508,35510,35512,35514,35516,35518,35520],{"class":54,"line":1348},[52,35497,12265],{"class":58},[52,35499,35128],{"class":62},[52,35501,199],{"class":102},[52,35503,35133],{"class":62},[52,35505,199],{"class":102},[52,35507,35138],{"class":62},[52,35509,11213],{"class":58},[52,35511,34613],{"class":62},[52,35513,186],{"class":102},[52,35515,35147],{"class":189},[52,35517,193],{"class":102},[52,35519,34623],{"class":189},[52,35521,3114],{"class":102},[52,35523,35524,35526,35528,35530,35532],{"class":54,"line":1376},[52,35525,11773],{"class":58},[52,35527,11292],{"class":62},[52,35529,11213],{"class":58},[52,35531,35164],{"class":62},[52,35533,1330],{"class":102},[52,35535,35536,35538,35540,35542,35544,35546,35548,35550,35552,35554,35556,35558],{"class":54,"line":1381},[52,35537,35171],{"class":62},[52,35539,80],{"class":79},[52,35541,34613],{"class":62},[52,35543,186],{"class":102},[52,35545,15882],{"class":1320},[52,35547,186],{"class":102},[52,35549,34643],{"class":189},[52,35551,193],{"class":102},[52,35553,35188],{"class":189},[52,35555,199],{"class":102},[52,35557,7019],{"class":189},[52,35559,211],{"class":102},[52,35561,35562,35564,35566,35568,35570,35572,35574,35576,35578,35580],{"class":54,"line":1406},[52,35563,34679],{"class":62},[52,35565,34682],{"class":79},[52,35567,34613],{"class":62},[52,35569,186],{"class":102},[52,35571,15882],{"class":1320},[52,35573,186],{"class":102},[52,35575,34693],{"class":189},[52,35577,193],{"class":102},[52,35579,15882],{"class":189},[52,35581,211],{"class":102},[52,35583,35584],{"class":54,"line":1430},[52,35585,70],{"emptyLinePlaceholder":69},[52,35587,35588,35590,35592,35594,35597,35599,35601,35603,35605],{"class":54,"line":1435},[52,35589,242],{"class":241},[52,35591,193],{"class":102},[52,35593,1476],{"class":1475},[52,35595,35596],{"class":87},"\"Folder: ",[52,35598,1482],{"class":121},[52,35600,34623],{"class":189},[52,35602,1488],{"class":121},[52,35604,115],{"class":87},[52,35606,211],{"class":102},[52,35608,35609,35611,35613,35615,35617,35619,35622,35624,35626,35628,35630,35632],{"class":54,"line":1467},[52,35610,242],{"class":241},[52,35612,193],{"class":102},[52,35614,1476],{"class":1475},[52,35616,34714],{"class":87},[52,35618,1482],{"class":121},[52,35620,35621],{"class":189},"format_size",[52,35623,193],{"class":102},[52,35625,34719],{"class":189},[52,35627,1521],{"class":102},[52,35629,1488],{"class":121},[52,35631,115],{"class":87},[52,35633,211],{"class":102},[639,35635,2510],{"id":35636},"example-output-1",[42,35638,35641],{"className":35639,"code":35640,"language":955,"meta":47},[953],"Folder: .\nTotal size: 2.45 MB\n",[49,35642,35640],{"__ignoreMap":47},[14,35644,35645],{},"The helper function keeps the conversion logic separate from the main loop, which makes the script easier to read.",[37,35647,35649],{"id":35648},"common-problems-and-how-to-avoid-them","Common problems and how to avoid them",[14,35651,35652],{},"Here are the most common reasons this script does not work as expected:",[309,35654,35655,35658,35661,35664,35667],{},[24,35656,35657],{},"The folder path is wrong",[24,35659,35660],{},"The script counts only the current folder, not subfolders",[24,35662,35663],{},"File paths are built incorrectly",[24,35665,35666],{},"A permission error blocks access to some files",[24,35668,35669],{},"The output is shown only in bytes when you expected KB or MB",[639,35671,35673],{"id":35672},"_1-the-folder-path-does-not-exist","1. The folder path does not exist",[14,35675,35676,35677,186],{},"If the path is wrong, Python may raise an error such as ",[49,35678,15764],{},[14,35680,35681],{},"Check the path first:",[42,35683,35685],{"className":44,"code":35684,"language":46,"meta":47,"style":47},"import os\n\nfolder_path = \"my_folder\"\nprint(folder_path)\nprint(os.path.exists(folder_path))\n",[49,35686,35687,35693,35697,35710,35720],{"__ignoreMap":47},[52,35688,35689,35691],{"class":54,"line":55},[52,35690,59],{"class":58},[52,35692,15839],{"class":62},[52,35694,35695],{"class":54,"line":66},[52,35696,70],{"emptyLinePlaceholder":69},[52,35698,35699,35701,35703,35705,35708],{"class":54,"line":73},[52,35700,34580],{"class":62},[52,35702,80],{"class":79},[52,35704,84],{"class":83},[52,35706,35707],{"class":87},"my_folder",[52,35709,91],{"class":83},[52,35711,35712,35714,35716,35718],{"class":54,"line":94},[52,35713,242],{"class":241},[52,35715,193],{"class":102},[52,35717,34623],{"class":189},[52,35719,211],{"class":102},[52,35721,35722,35724,35726,35728,35730,35732,35734,35736,35738,35740],{"class":54,"line":106},[52,35723,242],{"class":241},[52,35725,193],{"class":102},[52,35727,15848],{"class":189},[52,35729,186],{"class":102},[52,35731,15882],{"class":1320},[52,35733,186],{"class":102},[52,35735,15887],{"class":189},[52,35737,193],{"class":102},[52,35739,34623],{"class":189},[52,35741,8886],{"class":102},[14,35743,7526,35744,35746],{},[49,35745,6241],{}," is printed, the path is not valid from the script's current location.",[14,35748,35749,35750,186],{},"For help with this error, see ",[372,35751,35752],{"href":15903},"how to fix FileNotFoundError: Errno 2 No such file or directory",[639,35754,35756],{"id":35755},"_2-you-expected-subfolders-to-be-included","2. You expected subfolders to be included",[14,35758,35759,35760,35762],{},"This is a very common mistake. The ",[49,35761,34745],{}," version only checks one folder level.",[14,35764,35765,35766,186],{},"If you want subfolders too, switch to ",[49,35767,35078],{},[639,35769,35771],{"id":35770},"_3-paths-are-built-incorrectly","3. Paths are built incorrectly",[14,35773,35774,35775,186],{},"Always combine paths with ",[49,35776,34748],{},[14,35778,35779],{},"You can debug by printing the path:",[42,35781,35783],{"className":44,"code":35782,"language":46,"meta":47,"style":47},"import os\n\nfolder_path = \".\"\nfor name in os.listdir(folder_path):\n    path = os.path.join(folder_path, name)\n    print(path)\n",[49,35784,35785,35791,35795,35807,35827,35853],{"__ignoreMap":47},[52,35786,35787,35789],{"class":54,"line":55},[52,35788,59],{"class":58},[52,35790,15839],{"class":62},[52,35792,35793],{"class":54,"line":66},[52,35794,70],{"emptyLinePlaceholder":69},[52,35796,35797,35799,35801,35803,35805],{"class":54,"line":73},[52,35798,34580],{"class":62},[52,35800,80],{"class":79},[52,35802,84],{"class":83},[52,35804,186],{"class":87},[52,35806,91],{"class":83},[52,35808,35809,35811,35813,35815,35817,35819,35821,35823,35825],{"class":54,"line":94},[52,35810,12265],{"class":58},[52,35812,11292],{"class":62},[52,35814,11213],{"class":58},[52,35816,34613],{"class":62},[52,35818,186],{"class":102},[52,35820,34618],{"class":189},[52,35822,193],{"class":102},[52,35824,34623],{"class":189},[52,35826,3114],{"class":102},[52,35828,35829,35831,35833,35835,35837,35839,35841,35843,35845,35847,35849,35851],{"class":54,"line":106},[52,35830,34630],{"class":62},[52,35832,80],{"class":79},[52,35834,34613],{"class":62},[52,35836,186],{"class":102},[52,35838,15882],{"class":1320},[52,35840,186],{"class":102},[52,35842,34643],{"class":189},[52,35844,193],{"class":102},[52,35846,34623],{"class":189},[52,35848,199],{"class":102},[52,35850,7019],{"class":189},[52,35852,211],{"class":102},[52,35854,35855,35857,35859,35861],{"class":54,"line":128},[52,35856,1599],{"class":241},[52,35858,193],{"class":102},[52,35860,15882],{"class":189},[52,35862,211],{"class":102},[14,35864,35865,35866,759,35869,186],{},"You can also learn more in ",[372,35867,30816],{"href":35868},"\u002Fhow-to\u002Fhow-to-list-files-in-a-directory-in-python\u002F",[372,35870,35872],{"href":35871},"\u002Fstandard-library\u002Fos.listdir-function-explained","os.listdir() explained",[639,35874,35876,35877],{"id":35875},"_4-a-folder-is-passed-to-getsize","4. A folder is passed to ",[49,35878,35879],{},"getsize()",[14,35881,35882,35884],{},[49,35883,34751],{}," works on paths, but in this kind of script you usually want file sizes only.",[14,35886,35887,35888,2252],{},"Check with ",[49,35889,34975],{},[42,35891,35893],{"className":44,"code":35892,"language":46,"meta":47,"style":47},"import os\n\nfolder_path = \".\"\n\nfor name in os.listdir(folder_path):\n    path = os.path.join(folder_path, name)\n    print(os.path.isfile(path))\n",[49,35894,35895,35901,35905,35917,35921,35941,35967],{"__ignoreMap":47},[52,35896,35897,35899],{"class":54,"line":55},[52,35898,59],{"class":58},[52,35900,15839],{"class":62},[52,35902,35903],{"class":54,"line":66},[52,35904,70],{"emptyLinePlaceholder":69},[52,35906,35907,35909,35911,35913,35915],{"class":54,"line":73},[52,35908,34580],{"class":62},[52,35910,80],{"class":79},[52,35912,84],{"class":83},[52,35914,186],{"class":87},[52,35916,91],{"class":83},[52,35918,35919],{"class":54,"line":94},[52,35920,70],{"emptyLinePlaceholder":69},[52,35922,35923,35925,35927,35929,35931,35933,35935,35937,35939],{"class":54,"line":106},[52,35924,12265],{"class":58},[52,35926,11292],{"class":62},[52,35928,11213],{"class":58},[52,35930,34613],{"class":62},[52,35932,186],{"class":102},[52,35934,34618],{"class":189},[52,35936,193],{"class":102},[52,35938,34623],{"class":189},[52,35940,3114],{"class":102},[52,35942,35943,35945,35947,35949,35951,35953,35955,35957,35959,35961,35963,35965],{"class":54,"line":128},[52,35944,34630],{"class":62},[52,35946,80],{"class":79},[52,35948,34613],{"class":62},[52,35950,186],{"class":102},[52,35952,15882],{"class":1320},[52,35954,186],{"class":102},[52,35956,34643],{"class":189},[52,35958,193],{"class":102},[52,35960,34623],{"class":189},[52,35962,199],{"class":102},[52,35964,7019],{"class":189},[52,35966,211],{"class":102},[52,35968,35969,35971,35973,35975,35977,35979,35981,35983,35985,35987],{"class":54,"line":148},[52,35970,1599],{"class":241},[52,35972,193],{"class":102},[52,35974,15848],{"class":189},[52,35976,186],{"class":102},[52,35978,15882],{"class":1320},[52,35980,186],{"class":102},[52,35982,34668],{"class":189},[52,35984,193],{"class":102},[52,35986,15882],{"class":189},[52,35988,8886],{"class":102},[639,35990,35992],{"id":35991},"_5-permission-errors-stop-the-script","5. Permission errors stop the script",[14,35994,35995,35996,186],{},"Some files may not be accessible. In that case, you may get ",[49,35997,31675],{},[14,35999,36000],{},"A simple way to make the script more robust is to skip files that cannot be read:",[42,36002,36004],{"className":44,"code":36003,"language":46,"meta":47,"style":47},"import os\n\nfolder_path = \".\"\ntotal_size = 0\n\nfor root, dirs, files in os.walk(folder_path):\n    for name in files:\n        path = os.path.join(root, name)\n        try:\n            total_size += os.path.getsize(path)\n        except PermissionError:\n            print(f\"Skipped: {path}\")\n\nprint(f\"Total size: {total_size} bytes\")\n",[49,36005,36006,36012,36016,36028,36036,36040,36068,36080,36106,36113,36136,36145,36166,36170],{"__ignoreMap":47},[52,36007,36008,36010],{"class":54,"line":55},[52,36009,59],{"class":58},[52,36011,15839],{"class":62},[52,36013,36014],{"class":54,"line":66},[52,36015,70],{"emptyLinePlaceholder":69},[52,36017,36018,36020,36022,36024,36026],{"class":54,"line":73},[52,36019,34580],{"class":62},[52,36021,80],{"class":79},[52,36023,84],{"class":83},[52,36025,186],{"class":87},[52,36027,91],{"class":83},[52,36029,36030,36032,36034],{"class":54,"line":94},[52,36031,34593],{"class":62},[52,36033,80],{"class":79},[52,36035,34598],{"class":121},[52,36037,36038],{"class":54,"line":106},[52,36039,70],{"emptyLinePlaceholder":69},[52,36041,36042,36044,36046,36048,36050,36052,36054,36056,36058,36060,36062,36064,36066],{"class":54,"line":128},[52,36043,12265],{"class":58},[52,36045,35128],{"class":62},[52,36047,199],{"class":102},[52,36049,35133],{"class":62},[52,36051,199],{"class":102},[52,36053,35138],{"class":62},[52,36055,11213],{"class":58},[52,36057,34613],{"class":62},[52,36059,186],{"class":102},[52,36061,35147],{"class":189},[52,36063,193],{"class":102},[52,36065,34623],{"class":189},[52,36067,3114],{"class":102},[52,36069,36070,36072,36074,36076,36078],{"class":54,"line":148},[52,36071,11773],{"class":58},[52,36073,11292],{"class":62},[52,36075,11213],{"class":58},[52,36077,35164],{"class":62},[52,36079,1330],{"class":102},[52,36081,36082,36084,36086,36088,36090,36092,36094,36096,36098,36100,36102,36104],{"class":54,"line":164},[52,36083,35171],{"class":62},[52,36085,80],{"class":79},[52,36087,34613],{"class":62},[52,36089,186],{"class":102},[52,36091,15882],{"class":1320},[52,36093,186],{"class":102},[52,36095,34643],{"class":189},[52,36097,193],{"class":102},[52,36099,35188],{"class":189},[52,36101,199],{"class":102},[52,36103,7019],{"class":189},[52,36105,211],{"class":102},[52,36107,36108,36111],{"class":54,"line":170},[52,36109,36110],{"class":58},"        try",[52,36112,1330],{"class":102},[52,36114,36115,36118,36120,36122,36124,36126,36128,36130,36132,36134],{"class":54,"line":175},[52,36116,36117],{"class":62},"            total_size ",[52,36119,34682],{"class":79},[52,36121,34613],{"class":62},[52,36123,186],{"class":102},[52,36125,15882],{"class":1320},[52,36127,186],{"class":102},[52,36129,34693],{"class":189},[52,36131,193],{"class":102},[52,36133,15882],{"class":189},[52,36135,211],{"class":102},[52,36137,36138,36141,36143],{"class":54,"line":214},[52,36139,36140],{"class":58},"        except",[52,36142,24803],{"class":4733},[52,36144,1330],{"class":102},[52,36146,36147,36149,36151,36153,36156,36158,36160,36162,36164],{"class":54,"line":233},[52,36148,11180],{"class":241},[52,36150,193],{"class":102},[52,36152,1476],{"class":1475},[52,36154,36155],{"class":87},"\"Skipped: ",[52,36157,1482],{"class":121},[52,36159,15882],{"class":189},[52,36161,1488],{"class":121},[52,36163,115],{"class":87},[52,36165,211],{"class":102},[52,36167,36168],{"class":54,"line":238},[52,36169,70],{"emptyLinePlaceholder":69},[52,36171,36172,36174,36176,36178,36180,36182,36184,36186,36188],{"class":54,"line":272},[52,36173,242],{"class":241},[52,36175,193],{"class":102},[52,36177,1476],{"class":1475},[52,36179,34714],{"class":87},[52,36181,1482],{"class":121},[52,36183,34719],{"class":189},[52,36185,1488],{"class":121},[52,36187,34724],{"class":87},[52,36189,211],{"class":102},[14,36191,36192,36193,186],{},"If this happens often, read ",[372,36194,36195],{"href":18237},"how to fix PermissionError: Errno 13 Permission denied",[639,36197,36199],{"id":36198},"helpful-debugging-prints","Helpful debugging prints",[14,36201,36202],{},"When the result looks wrong, these simple checks can help:",[42,36204,36206],{"className":44,"code":36205,"language":46,"meta":47,"style":47},"print(folder_path)\nprint(os.path.exists(folder_path))\nprint(os.path.isfile(path))\nprint(os.path.getsize(path))\nprint(root, files)\n",[49,36207,36208,36218,36240,36262,36284],{"__ignoreMap":47},[52,36209,36210,36212,36214,36216],{"class":54,"line":55},[52,36211,242],{"class":241},[52,36213,193],{"class":102},[52,36215,34623],{"class":189},[52,36217,211],{"class":102},[52,36219,36220,36222,36224,36226,36228,36230,36232,36234,36236,36238],{"class":54,"line":66},[52,36221,242],{"class":241},[52,36223,193],{"class":102},[52,36225,15848],{"class":189},[52,36227,186],{"class":102},[52,36229,15882],{"class":1320},[52,36231,186],{"class":102},[52,36233,15887],{"class":189},[52,36235,193],{"class":102},[52,36237,34623],{"class":189},[52,36239,8886],{"class":102},[52,36241,36242,36244,36246,36248,36250,36252,36254,36256,36258,36260],{"class":54,"line":73},[52,36243,242],{"class":241},[52,36245,193],{"class":102},[52,36247,15848],{"class":189},[52,36249,186],{"class":102},[52,36251,15882],{"class":1320},[52,36253,186],{"class":102},[52,36255,34668],{"class":189},[52,36257,193],{"class":102},[52,36259,15882],{"class":189},[52,36261,8886],{"class":102},[52,36263,36264,36266,36268,36270,36272,36274,36276,36278,36280,36282],{"class":54,"line":94},[52,36265,242],{"class":241},[52,36267,193],{"class":102},[52,36269,15848],{"class":189},[52,36271,186],{"class":102},[52,36273,15882],{"class":1320},[52,36275,186],{"class":102},[52,36277,34693],{"class":189},[52,36279,193],{"class":102},[52,36281,15882],{"class":189},[52,36283,8886],{"class":102},[52,36285,36286,36288,36290,36292,36294,36296],{"class":54,"line":106},[52,36287,242],{"class":241},[52,36289,193],{"class":102},[52,36291,35188],{"class":189},[52,36293,199],{"class":102},[52,36295,35164],{"class":189},[52,36297,211],{"class":102},[37,36299,36301],{"id":36300},"ways-to-improve-the-script","Ways to improve the script",[14,36303,36304],{},"Once the basic version works, you can extend it in useful ways:",[309,36306,36307,36312,36315,36318],{},[24,36308,36309,36310],{},"Ask the user for a folder path with ",[49,36311,10597],{},[24,36313,36314],{},"Skip hidden files if needed",[24,36316,36317],{},"Show the largest files in the folder",[24,36319,36320],{},"Print separate totals for each subfolder",[14,36322,36323],{},"Here is one simple improvement that lets the user choose the folder:",[42,36325,36327],{"className":44,"code":36326,"language":46,"meta":47,"style":47},"import os\n\nfolder_path = input(\"Enter a folder path: \")\ntotal_size = 0\n\nfor root, dirs, files in os.walk(folder_path):\n    for name in files:\n        path = os.path.join(root, name)\n        total_size += os.path.getsize(path)\n\nprint(f\"Total size: {total_size} bytes\")\n",[49,36328,36329,36335,36339,36358,36366,36370,36398,36410,36436,36458,36462],{"__ignoreMap":47},[52,36330,36331,36333],{"class":54,"line":55},[52,36332,59],{"class":58},[52,36334,15839],{"class":62},[52,36336,36337],{"class":54,"line":66},[52,36338,70],{"emptyLinePlaceholder":69},[52,36340,36341,36343,36345,36347,36349,36351,36354,36356],{"class":54,"line":73},[52,36342,34580],{"class":62},[52,36344,80],{"class":79},[52,36346,11037],{"class":241},[52,36348,193],{"class":102},[52,36350,115],{"class":83},[52,36352,36353],{"class":87},"Enter a folder path: ",[52,36355,115],{"class":83},[52,36357,211],{"class":102},[52,36359,36360,36362,36364],{"class":54,"line":94},[52,36361,34593],{"class":62},[52,36363,80],{"class":79},[52,36365,34598],{"class":121},[52,36367,36368],{"class":54,"line":106},[52,36369,70],{"emptyLinePlaceholder":69},[52,36371,36372,36374,36376,36378,36380,36382,36384,36386,36388,36390,36392,36394,36396],{"class":54,"line":128},[52,36373,12265],{"class":58},[52,36375,35128],{"class":62},[52,36377,199],{"class":102},[52,36379,35133],{"class":62},[52,36381,199],{"class":102},[52,36383,35138],{"class":62},[52,36385,11213],{"class":58},[52,36387,34613],{"class":62},[52,36389,186],{"class":102},[52,36391,35147],{"class":189},[52,36393,193],{"class":102},[52,36395,34623],{"class":189},[52,36397,3114],{"class":102},[52,36399,36400,36402,36404,36406,36408],{"class":54,"line":148},[52,36401,11773],{"class":58},[52,36403,11292],{"class":62},[52,36405,11213],{"class":58},[52,36407,35164],{"class":62},[52,36409,1330],{"class":102},[52,36411,36412,36414,36416,36418,36420,36422,36424,36426,36428,36430,36432,36434],{"class":54,"line":164},[52,36413,35171],{"class":62},[52,36415,80],{"class":79},[52,36417,34613],{"class":62},[52,36419,186],{"class":102},[52,36421,15882],{"class":1320},[52,36423,186],{"class":102},[52,36425,34643],{"class":189},[52,36427,193],{"class":102},[52,36429,35188],{"class":189},[52,36431,199],{"class":102},[52,36433,7019],{"class":189},[52,36435,211],{"class":102},[52,36437,36438,36440,36442,36444,36446,36448,36450,36452,36454,36456],{"class":54,"line":170},[52,36439,34679],{"class":62},[52,36441,34682],{"class":79},[52,36443,34613],{"class":62},[52,36445,186],{"class":102},[52,36447,15882],{"class":1320},[52,36449,186],{"class":102},[52,36451,34693],{"class":189},[52,36453,193],{"class":102},[52,36455,15882],{"class":189},[52,36457,211],{"class":102},[52,36459,36460],{"class":54,"line":175},[52,36461,70],{"emptyLinePlaceholder":69},[52,36463,36464,36466,36468,36470,36472,36474,36476,36478,36480],{"class":54,"line":214},[52,36465,242],{"class":241},[52,36467,193],{"class":102},[52,36469,1476],{"class":1475},[52,36471,34714],{"class":87},[52,36473,1482],{"class":121},[52,36475,34719],{"class":189},[52,36477,1488],{"class":121},[52,36479,34724],{"class":87},[52,36481,211],{"class":102},[14,36483,36484],{},"You could also combine this with the readable size function from the earlier section.",[37,36486,1942],{"id":1941},[639,36488,36490],{"id":36489},"does-this-script-include-subfolders","Does this script include subfolders?",[14,36492,36493,36494,36496],{},"The basic version does not. Use a recursive version with ",[49,36495,35078],{}," to include subfolders.",[639,36498,36500],{"id":36499},"why-am-i-getting-filenotfounderror","Why am I getting FileNotFoundError?",[14,36502,36503],{},"The path may be wrong, or the current working directory may not be what you expect.",[639,36505,36507],{"id":36506},"why-is-the-size-shown-in-bytes","Why is the size shown in bytes?",[14,36509,36510,36512],{},[49,36511,34751],{}," returns bytes. You can convert that value to KB, MB, or GB.",[639,36514,36516],{"id":36515},"can-i-calculate-folder-size-with-pathlib-instead-of-os","Can I calculate folder size with pathlib instead of os?",[14,36518,36519,36520,36522],{},"Yes, but this example keeps things simple by using ",[49,36521,15848],{}," tools that many beginners learn first.",[37,36524,2005],{"id":2004},[309,36526,36527,36531,36536,36540,36544,36548],{},[24,36528,36529],{},[372,36530,29945],{"href":29944},[24,36532,36533],{},[372,36534,36535],{"href":35871},"os.listdir() function explained",[24,36537,36538],{},[372,36539,31970],{"href":31969},[24,36541,36542],{},[372,36543,31956],{"href":35868},[24,36545,36546],{},[372,36547,16522],{"href":15907},[24,36549,36550],{},[372,36551,29931],{"href":28722},[14,36553,36554],{},"Try extending this example next by adding subfolder support, readable file sizes, or a report that shows the largest files.",[2034,36556,36557],{},"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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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}",{"title":47,"searchDepth":66,"depth":66,"links":36559},[36560,36561,36562,36568,36571,36574,36583,36584,36590],{"id":322,"depth":66,"text":323},{"id":34763,"depth":66,"text":34764},{"id":34791,"depth":66,"text":34792,"children":36563},[36564,36565,36567],{"id":24094,"depth":73,"text":24095},{"id":34985,"depth":73,"text":36566},"Why os.path.join() matters",{"id":2509,"depth":73,"text":2510},{"id":35071,"depth":66,"text":35072,"children":36569},[36570],{"id":9909,"depth":73,"text":9910},{"id":35275,"depth":66,"text":35276,"children":36572},[36573],{"id":35636,"depth":73,"text":2510},{"id":35648,"depth":66,"text":35649,"children":36575},[36576,36577,36578,36579,36581,36582],{"id":35672,"depth":73,"text":35673},{"id":35755,"depth":73,"text":35756},{"id":35770,"depth":73,"text":35771},{"id":35875,"depth":73,"text":36580},"4. A folder is passed to getsize()",{"id":35991,"depth":73,"text":35992},{"id":36198,"depth":73,"text":36199},{"id":36300,"depth":66,"text":36301},{"id":1941,"depth":66,"text":1942,"children":36585},[36586,36587,36588,36589],{"id":36489,"depth":73,"text":36490},{"id":36499,"depth":73,"text":36500},{"id":36506,"depth":73,"text":36507},{"id":36515,"depth":73,"text":36516},{"id":2004,"depth":66,"text":2005},"Master python folder size calculator example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-folder-size-calculator-example",{"title":34547,"description":36591},"examples\u002Fpython-folder-size-calculator-example","VHs3RdhFVFlNZ0qjnOI3bQeHpUwqUsqZdctWiZYhbNQ",{"id":36598,"title":36599,"body":36600,"description":38123,"extension":2075,"meta":38124,"navigation":69,"path":38125,"seo":38126,"stem":38127,"__hash__":38128},"content\u002Fexamples\u002Fpython-generate-random-numbers-example.md","Python Generate Random Numbers Example",{"type":7,"value":36601,"toc":38086},[36602,36605,36611,36613,36632,36635,36685,36691,36693,36696,36724,36731,36735,36744,36790,36792,36818,36821,36828,36832,36838,36903,36907,36921,36930,36933,37017,37019,37033,37036,37101,37105,37112,37118,37126,37165,37167,37176,37182,37189,37237,37240,37253,37265,37271,37274,37322,37324,37333,37335,37338,37341,37378,37381,37389,37392,37400,37402,37411,37414,37416,37419,37424,37429,37466,37469,37515,37522,37528,37538,37584,37586,37630,37638,37641,37662,37666,37669,37676,37685,37688,37705,37709,37716,37719,37768,37777,37781,37784,37798,37805,37807,37811,37820,37856,37863,37873,37909,37916,37932,37934,37985,37989,37992,38054,38056,38083],[10,36603,36599],{"id":36604},"python-generate-random-numbers-example",[14,36606,36607,36608,13338],{},"This beginner-friendly example shows how to generate random numbers in Python with the ",[49,36609,36610],{},"random",[14,36612,25293],{},[309,36614,36615,36620,36623,36626],{},[24,36616,36617,36618,14928],{},"How to import the ",[49,36619,36610],{},[24,36621,36622],{},"How to generate one random integer",[24,36624,36625],{},"How to generate several random numbers in a loop",[24,36627,4983,36628,36631],{},[49,36629,36630],{},"randint()"," differs from other random functions",[14,36633,36634],{},"If you want the shortest working example, start here:",[42,36636,36638],{"className":44,"code":36637,"language":46,"meta":47,"style":47},"import random\n\nnumber = random.randint(1, 10)\nprint(number)\n",[49,36639,36640,36647,36651,36675],{"__ignoreMap":47},[52,36641,36642,36644],{"class":54,"line":55},[52,36643,59],{"class":58},[52,36645,36646],{"class":62}," random\n",[52,36648,36649],{"class":54,"line":66},[52,36650,70],{"emptyLinePlaceholder":69},[52,36652,36653,36655,36657,36660,36662,36665,36667,36669,36671,36673],{"class":54,"line":73},[52,36654,32574],{"class":62},[52,36656,80],{"class":79},[52,36658,36659],{"class":62}," random",[52,36661,186],{"class":102},[52,36663,36664],{"class":189},"randint",[52,36666,193],{"class":102},[52,36668,9200],{"class":121},[52,36670,199],{"class":102},[52,36672,27053],{"class":121},[52,36674,211],{"class":102},[52,36676,36677,36679,36681,36683],{"class":54,"line":94},[52,36678,242],{"class":241},[52,36680,193],{"class":102},[52,36682,27497],{"class":189},[52,36684,211],{"class":102},[14,36686,3650,36687,36690],{},[49,36688,36689],{},"random.randint(start, end)"," to get a random integer between both numbers, including the endpoints.",[37,36692,2268],{"id":2267},[14,36694,36695],{},"This example focuses on the most common beginner use cases:",[309,36697,36698,36704,36707,36710],{},[24,36699,36700,36701,36703],{},"Importing the ",[49,36702,36610],{}," module before using it",[24,36705,36706],{},"Generating a single random whole number",[24,36708,36709],{},"Repeating that process several times with a loop",[24,36711,36712,36713,2587,36715,2587,36718,11556,36721],{},"Understanding when to use ",[49,36714,36630],{},[49,36716,36717],{},"random()",[49,36719,36720],{},"randrange()",[49,36722,36723],{},"uniform()",[14,36725,36726,36727,186],{},"If you are new to the module, see the ",[372,36728,36730],{"href":36729},"\u002Fstandard-library\u002Fpython-random-module-overview\u002F","Python random module overview",[37,36732,36734],{"id":36733},"basic-example-one-random-integer","Basic example: one random integer",[14,36736,3650,36737,36740,36741,186],{},[49,36738,36739],{},"import random"," first. Then call ",[49,36742,36743],{},"random.randint(1, 10)",[42,36745,36746],{"className":44,"code":36637,"language":46,"meta":47,"style":47},[49,36747,36748,36754,36758,36780],{"__ignoreMap":47},[52,36749,36750,36752],{"class":54,"line":55},[52,36751,59],{"class":58},[52,36753,36646],{"class":62},[52,36755,36756],{"class":54,"line":66},[52,36757,70],{"emptyLinePlaceholder":69},[52,36759,36760,36762,36764,36766,36768,36770,36772,36774,36776,36778],{"class":54,"line":73},[52,36761,32574],{"class":62},[52,36763,80],{"class":79},[52,36765,36659],{"class":62},[52,36767,186],{"class":102},[52,36769,36664],{"class":189},[52,36771,193],{"class":102},[52,36773,9200],{"class":121},[52,36775,199],{"class":102},[52,36777,27053],{"class":121},[52,36779,211],{"class":102},[52,36781,36782,36784,36786,36788],{"class":54,"line":94},[52,36783,242],{"class":241},[52,36785,193],{"class":102},[52,36787,27497],{"class":189},[52,36789,211],{"class":102},[639,36791,24095],{"id":24094},[309,36793,36794,36799,36804,36812],{},[24,36795,36796,36798],{},[49,36797,36739],{}," makes the module available",[24,36800,36801,36803],{},[49,36802,36743],{}," returns a whole number",[24,36805,36806,36807,759,36809,36811],{},"Both ",[49,36808,9200],{},[49,36810,3373],{}," can be returned",[24,36813,36814,36817],{},[49,36815,36816],{},"print(number)"," shows the result",[14,36819,36820],{},"This is a good first example because it is short and easy to test.",[14,36822,36823,36824,186],{},"If you want a full explanation of this function, see ",[372,36825,36827],{"href":36826},"\u002Fstandard-library\u002Frandom.randint-function-explained","random.randint() explained",[37,36829,36831],{"id":36830},"generate-multiple-random-numbers","Generate multiple random numbers",[14,36833,36834,36835,36837],{},"You can use a ",[49,36836,12265],{}," loop to generate several values.",[42,36839,36841],{"className":44,"code":36840,"language":46,"meta":47,"style":47},"import random\n\nfor i in range(5):\n    number = random.randint(1, 10)\n    print(number)\n",[49,36842,36843,36849,36853,36871,36893],{"__ignoreMap":47},[52,36844,36845,36847],{"class":54,"line":55},[52,36846,59],{"class":58},[52,36848,36646],{"class":62},[52,36850,36851],{"class":54,"line":66},[52,36852,70],{"emptyLinePlaceholder":69},[52,36854,36855,36857,36860,36862,36865,36867,36869],{"class":54,"line":73},[52,36856,12265],{"class":58},[52,36858,36859],{"class":62}," i ",[52,36861,11213],{"class":58},[52,36863,36864],{"class":241}," range",[52,36866,193],{"class":102},[52,36868,9479],{"class":121},[52,36870,3114],{"class":102},[52,36872,36873,36875,36877,36879,36881,36883,36885,36887,36889,36891],{"class":54,"line":94},[52,36874,27024],{"class":62},[52,36876,80],{"class":79},[52,36878,36659],{"class":62},[52,36880,186],{"class":102},[52,36882,36664],{"class":189},[52,36884,193],{"class":102},[52,36886,9200],{"class":121},[52,36888,199],{"class":102},[52,36890,27053],{"class":121},[52,36892,211],{"class":102},[52,36894,36895,36897,36899,36901],{"class":54,"line":106},[52,36896,1599],{"class":241},[52,36898,193],{"class":102},[52,36900,27497],{"class":189},[52,36902,211],{"class":102},[639,36904,36906],{"id":36905},"what-this-does","What this does",[309,36908,36909,36915,36918],{},[24,36910,36911,36914],{},[49,36912,36913],{},"range(5)"," repeats the loop 5 times",[24,36916,36917],{},"Each loop generates a new random integer",[24,36919,36920],{},"The output is usually different each time",[14,36922,36923,36924,759,36926,186],{},"If loops are still new to you, read ",[372,36925,34468],{"href":32073},[372,36927,36929],{"href":36928},"\u002Freference\u002Fpython-range-function-explained\u002F","Python range() function explained",[14,36931,36932],{},"You can also store the values in a list if you want to use them later:",[42,36934,36936],{"className":44,"code":36935,"language":46,"meta":47,"style":47},"import random\n\nnumbers = []\n\nfor i in range(5):\n    numbers.append(random.randint(1, 10))\n\nprint(numbers)\n",[49,36937,36938,36944,36948,36956,36960,36976,37003,37007],{"__ignoreMap":47},[52,36939,36940,36942],{"class":54,"line":55},[52,36941,59],{"class":58},[52,36943,36646],{"class":62},[52,36945,36946],{"class":54,"line":66},[52,36947,70],{"emptyLinePlaceholder":69},[52,36949,36950,36952,36954],{"class":54,"line":73},[52,36951,32092],{"class":62},[52,36953,80],{"class":79},[52,36955,18590],{"class":102},[52,36957,36958],{"class":54,"line":94},[52,36959,70],{"emptyLinePlaceholder":69},[52,36961,36962,36964,36966,36968,36970,36972,36974],{"class":54,"line":106},[52,36963,12265],{"class":58},[52,36965,36859],{"class":62},[52,36967,11213],{"class":58},[52,36969,36864],{"class":241},[52,36971,193],{"class":102},[52,36973,9479],{"class":121},[52,36975,3114],{"class":102},[52,36977,36978,36981,36983,36985,36987,36989,36991,36993,36995,36997,36999,37001],{"class":54,"line":128},[52,36979,36980],{"class":62},"    numbers",[52,36982,186],{"class":102},[52,36984,18748],{"class":189},[52,36986,193],{"class":102},[52,36988,36610],{"class":189},[52,36990,186],{"class":102},[52,36992,36664],{"class":189},[52,36994,193],{"class":102},[52,36996,9200],{"class":121},[52,36998,199],{"class":102},[52,37000,27053],{"class":121},[52,37002,8886],{"class":102},[52,37004,37005],{"class":54,"line":148},[52,37006,70],{"emptyLinePlaceholder":69},[52,37008,37009,37011,37013,37015],{"class":54,"line":164},[52,37010,242],{"class":241},[52,37012,193],{"class":102},[52,37014,5324],{"class":189},[52,37016,211],{"class":102},[14,37018,27679],{},[309,37020,37021,37024,37027,37030],{},[24,37022,37023],{},"Simple games",[24,37025,37026],{},"Test values",[24,37028,37029],{},"Sample data",[24,37031,37032],{},"Practice projects",[14,37034,37035],{},"You can also write the same idea with a list comprehension:",[42,37037,37039],{"className":44,"code":37038,"language":46,"meta":47,"style":47},"import random\n\nnumbers = [random.randint(1, 10) for _ in range(5)]\nprint(numbers)\n",[49,37040,37041,37047,37051,37091],{"__ignoreMap":47},[52,37042,37043,37045],{"class":54,"line":55},[52,37044,59],{"class":58},[52,37046,36646],{"class":62},[52,37048,37049],{"class":54,"line":66},[52,37050,70],{"emptyLinePlaceholder":69},[52,37052,37053,37055,37057,37059,37061,37063,37065,37067,37069,37071,37073,37075,37078,37081,37083,37085,37087,37089],{"class":54,"line":73},[52,37054,32092],{"class":62},[52,37056,80],{"class":79},[52,37058,18162],{"class":102},[52,37060,36610],{"class":62},[52,37062,186],{"class":102},[52,37064,36664],{"class":189},[52,37066,193],{"class":102},[52,37068,9200],{"class":121},[52,37070,199],{"class":102},[52,37072,27053],{"class":121},[52,37074,1521],{"class":102},[52,37076,37077],{"class":58}," for",[52,37079,37080],{"class":62}," _ ",[52,37082,11213],{"class":58},[52,37084,36864],{"class":241},[52,37086,193],{"class":102},[52,37088,9479],{"class":121},[52,37090,33167],{"class":102},[52,37092,37093,37095,37097,37099],{"class":54,"line":94},[52,37094,242],{"class":241},[52,37096,193],{"class":102},[52,37098,5324],{"class":189},[52,37100,211],{"class":102},[37,37102,37104],{"id":37103},"other-common-random-number-options","Other common random number options",[14,37106,37107,37109,37110,13338],{},[49,37108,36630],{}," is not the only useful function in the ",[49,37111,36610],{},[639,37113,37115],{"id":37114},"randomrandom",[49,37116,37117],{},"random.random()",[14,37119,37120,37121,37123,37124,186],{},"This returns a decimal number from ",[49,37122,13294],{}," up to ",[49,37125,9200],{},[42,37127,37129],{"className":44,"code":37128,"language":46,"meta":47,"style":47},"import random\n\nnumber = random.random()\nprint(number)\n",[49,37130,37131,37137,37141,37155],{"__ignoreMap":47},[52,37132,37133,37135],{"class":54,"line":55},[52,37134,59],{"class":58},[52,37136,36646],{"class":62},[52,37138,37139],{"class":54,"line":66},[52,37140,70],{"emptyLinePlaceholder":69},[52,37142,37143,37145,37147,37149,37151,37153],{"class":54,"line":73},[52,37144,32574],{"class":62},[52,37146,80],{"class":79},[52,37148,36659],{"class":62},[52,37150,186],{"class":102},[52,37152,36610],{"class":189},[52,37154,230],{"class":102},[52,37156,37157,37159,37161,37163],{"class":54,"line":94},[52,37158,242],{"class":241},[52,37160,193],{"class":102},[52,37162,27497],{"class":189},[52,37164,211],{"class":102},[14,37166,11845],{},[42,37168,37170],{"className":44,"code":37169,"language":46,"meta":47,"style":47},"0.3478215941\n",[49,37171,37172],{"__ignoreMap":47},[52,37173,37174],{"class":54,"line":55},[52,37175,37169],{"class":121},[639,37177,37179],{"id":37178},"randomrandrangestart-stop",[49,37180,37181],{},"random.randrange(start, stop)",[14,37183,37184,37185,37188],{},"This is similar to how ",[49,37186,37187],{},"range()"," works.",[42,37190,37192],{"className":44,"code":37191,"language":46,"meta":47,"style":47},"import random\n\nnumber = random.randrange(1, 10)\nprint(number)\n",[49,37193,37194,37200,37204,37227],{"__ignoreMap":47},[52,37195,37196,37198],{"class":54,"line":55},[52,37197,59],{"class":58},[52,37199,36646],{"class":62},[52,37201,37202],{"class":54,"line":66},[52,37203,70],{"emptyLinePlaceholder":69},[52,37205,37206,37208,37210,37212,37214,37217,37219,37221,37223,37225],{"class":54,"line":73},[52,37207,32574],{"class":62},[52,37209,80],{"class":79},[52,37211,36659],{"class":62},[52,37213,186],{"class":102},[52,37215,37216],{"class":189},"randrange",[52,37218,193],{"class":102},[52,37220,9200],{"class":121},[52,37222,199],{"class":102},[52,37224,27053],{"class":121},[52,37226,211],{"class":102},[52,37228,37229,37231,37233,37235],{"class":54,"line":94},[52,37230,242],{"class":241},[52,37232,193],{"class":102},[52,37234,27497],{"class":189},[52,37236,211],{"class":102},[14,37238,37239],{},"Important:",[309,37241,37242,37246],{},[24,37243,37244,36811],{},[49,37245,9200],{},[24,37247,37248,27498,37250,37252],{},[49,37249,3373],{},[5503,37251,20858],{}," included",[14,37254,23167,37255,37258,37259,37261,37262,186],{},[49,37256,37257],{},"randrange(1, 10)"," can return numbers from ",[49,37260,9200],{}," to ",[49,37263,37264],{},"9",[639,37266,37268],{"id":37267},"randomuniforma-b",[49,37269,37270],{},"random.uniform(a, b)",[14,37272,37273],{},"This returns a random decimal number in a range.",[42,37275,37277],{"className":44,"code":37276,"language":46,"meta":47,"style":47},"import random\n\nnumber = random.uniform(1, 10)\nprint(number)\n",[49,37278,37279,37285,37289,37312],{"__ignoreMap":47},[52,37280,37281,37283],{"class":54,"line":55},[52,37282,59],{"class":58},[52,37284,36646],{"class":62},[52,37286,37287],{"class":54,"line":66},[52,37288,70],{"emptyLinePlaceholder":69},[52,37290,37291,37293,37295,37297,37299,37302,37304,37306,37308,37310],{"class":54,"line":73},[52,37292,32574],{"class":62},[52,37294,80],{"class":79},[52,37296,36659],{"class":62},[52,37298,186],{"class":102},[52,37300,37301],{"class":189},"uniform",[52,37303,193],{"class":102},[52,37305,9200],{"class":121},[52,37307,199],{"class":102},[52,37309,27053],{"class":121},[52,37311,211],{"class":102},[52,37313,37314,37316,37318,37320],{"class":54,"line":94},[52,37315,242],{"class":241},[52,37317,193],{"class":102},[52,37319,27497],{"class":189},[52,37321,211],{"class":102},[14,37323,11845],{},[42,37325,37327],{"className":44,"code":37326,"language":46,"meta":47,"style":47},"6.284193204\n",[49,37328,37329],{"__ignoreMap":47},[52,37330,37331],{"class":54,"line":55},[52,37332,37326],{"class":121},[37,37334,13563],{"id":13562},[14,37336,37337],{},"Random output changes each time you run the program. That is normal.",[14,37339,37340],{},"For this code:",[42,37342,37344],{"className":44,"code":37343,"language":46,"meta":47,"style":47},"import random\n\nprint(random.randint(1, 10))\n",[49,37345,37346,37352,37356],{"__ignoreMap":47},[52,37347,37348,37350],{"class":54,"line":55},[52,37349,59],{"class":58},[52,37351,36646],{"class":62},[52,37353,37354],{"class":54,"line":66},[52,37355,70],{"emptyLinePlaceholder":69},[52,37357,37358,37360,37362,37364,37366,37368,37370,37372,37374,37376],{"class":54,"line":73},[52,37359,242],{"class":241},[52,37361,193],{"class":102},[52,37363,36610],{"class":189},[52,37365,186],{"class":102},[52,37367,36664],{"class":189},[52,37369,193],{"class":102},[52,37371,9200],{"class":121},[52,37373,199],{"class":102},[52,37375,27053],{"class":121},[52,37377,8886],{"class":102},[14,37379,37380],{},"You might see:",[42,37382,37383],{"className":44,"code":7184,"language":46,"meta":47,"style":47},[49,37384,37385],{"__ignoreMap":47},[52,37386,37387],{"class":54,"line":55},[52,37388,7184],{"class":121},[14,37390,37391],{},"Or:",[42,37393,37394],{"className":44,"code":5271,"language":46,"meta":47,"style":47},[49,37395,37396],{"__ignoreMap":47},[52,37397,37398],{"class":54,"line":55},[52,37399,5271],{"class":121},[14,37401,37391],{},[42,37403,37405],{"className":44,"code":37404,"language":46,"meta":47,"style":47},"10\n",[49,37406,37407],{"__ignoreMap":47},[52,37408,37409],{"class":54,"line":55},[52,37410,37404],{"class":121},[14,37412,37413],{},"Each run can produce a different result, and that is expected behavior.",[37,37415,6402],{"id":6401},[14,37417,37418],{},"Here are some common problems when generating random numbers in Python.",[639,37420,14067,37422],{"id":37421},"forgetting-to-import-random",[49,37423,36610],{},[14,37425,37426,37427,186],{},"This causes a ",[49,37428,14080],{},[42,37430,37432],{"className":44,"code":37431,"language":46,"meta":47,"style":47},"number = random.randint(1, 10)\nprint(number)\n",[49,37433,37434,37456],{"__ignoreMap":47},[52,37435,37436,37438,37440,37442,37444,37446,37448,37450,37452,37454],{"class":54,"line":55},[52,37437,32574],{"class":62},[52,37439,80],{"class":79},[52,37441,36659],{"class":62},[52,37443,186],{"class":102},[52,37445,36664],{"class":189},[52,37447,193],{"class":102},[52,37449,9200],{"class":121},[52,37451,199],{"class":102},[52,37453,27053],{"class":121},[52,37455,211],{"class":102},[52,37457,37458,37460,37462,37464],{"class":54,"line":66},[52,37459,242],{"class":241},[52,37461,193],{"class":102},[52,37463,27497],{"class":189},[52,37465,211],{"class":102},[14,37467,37468],{},"Fix it by adding the import:",[42,37470,37471],{"className":44,"code":36637,"language":46,"meta":47,"style":47},[49,37472,37473,37479,37483,37505],{"__ignoreMap":47},[52,37474,37475,37477],{"class":54,"line":55},[52,37476,59],{"class":58},[52,37478,36646],{"class":62},[52,37480,37481],{"class":54,"line":66},[52,37482,70],{"emptyLinePlaceholder":69},[52,37484,37485,37487,37489,37491,37493,37495,37497,37499,37501,37503],{"class":54,"line":73},[52,37486,32574],{"class":62},[52,37488,80],{"class":79},[52,37490,36659],{"class":62},[52,37492,186],{"class":102},[52,37494,36664],{"class":189},[52,37496,193],{"class":102},[52,37498,9200],{"class":121},[52,37500,199],{"class":102},[52,37502,27053],{"class":121},[52,37504,211],{"class":102},[52,37506,37507,37509,37511,37513],{"class":54,"line":94},[52,37508,242],{"class":241},[52,37510,193],{"class":102},[52,37512,27497],{"class":189},[52,37514,211],{"class":102},[14,37516,37517,37518,186],{},"If you see this error, read ",[372,37519,37521],{"href":37520},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix\u002F","NameError: name is not defined",[639,37523,10294,37525,37527],{"id":37524},"using-randint-without-the-module-name",[49,37526,36630],{}," without the module name",[14,37529,37530,37531,37533,37534,37537],{},"If you write ",[49,37532,36739],{},", you must call the function with ",[49,37535,37536],{},"random."," in front:",[42,37539,37540],{"className":44,"code":36637,"language":46,"meta":47,"style":47},[49,37541,37542,37548,37552,37574],{"__ignoreMap":47},[52,37543,37544,37546],{"class":54,"line":55},[52,37545,59],{"class":58},[52,37547,36646],{"class":62},[52,37549,37550],{"class":54,"line":66},[52,37551,70],{"emptyLinePlaceholder":69},[52,37553,37554,37556,37558,37560,37562,37564,37566,37568,37570,37572],{"class":54,"line":73},[52,37555,32574],{"class":62},[52,37557,80],{"class":79},[52,37559,36659],{"class":62},[52,37561,186],{"class":102},[52,37563,36664],{"class":189},[52,37565,193],{"class":102},[52,37567,9200],{"class":121},[52,37569,199],{"class":102},[52,37571,27053],{"class":121},[52,37573,211],{"class":102},[52,37575,37576,37578,37580,37582],{"class":54,"line":94},[52,37577,242],{"class":241},[52,37579,193],{"class":102},[52,37581,27497],{"class":189},[52,37583,211],{"class":102},[14,37585,15915],{},[42,37587,37589],{"className":44,"code":37588,"language":46,"meta":47,"style":47},"import random\n\nnumber = randint(1, 10)\nprint(number)\n",[49,37590,37591,37597,37601,37620],{"__ignoreMap":47},[52,37592,37593,37595],{"class":54,"line":55},[52,37594,59],{"class":58},[52,37596,36646],{"class":62},[52,37598,37599],{"class":54,"line":66},[52,37600,70],{"emptyLinePlaceholder":69},[52,37602,37603,37605,37607,37610,37612,37614,37616,37618],{"class":54,"line":73},[52,37604,32574],{"class":62},[52,37606,80],{"class":79},[52,37608,37609],{"class":189}," randint",[52,37611,193],{"class":102},[52,37613,9200],{"class":121},[52,37615,199],{"class":102},[52,37617,27053],{"class":121},[52,37619,211],{"class":102},[52,37621,37622,37624,37626,37628],{"class":54,"line":94},[52,37623,242],{"class":241},[52,37625,193],{"class":102},[52,37627,27497],{"class":189},[52,37629,211],{"class":102},[639,37631,37633,37634,759,37636],{"id":37632},"confusing-randint-and-randrange","Confusing ",[49,37635,36630],{},[49,37637,36720],{},[14,37639,37640],{},"These two functions are similar, but they do not handle the end value the same way.",[309,37642,37643,37653],{},[24,37644,37645,37647,37648,37650,37651],{},[49,37646,36743],{}," returns ",[49,37649,9200],{}," through ",[49,37652,3373],{},[24,37654,37655,37647,37658,37650,37660],{},[49,37656,37657],{},"random.randrange(1, 10)",[49,37659,9200],{},[49,37661,37264],{},[639,37663,37665],{"id":37664},"expecting-the-same-result-every-time","Expecting the same result every time",[14,37667,37668],{},"Random functions usually produce different results on each run. That is the point of using them.",[639,37670,37672,37673],{"id":37671},"naming-your-file-randompy","Naming your file ",[49,37674,37675],{},"random.py",[14,37677,37678,37679,37681,37682,186],{},"If your script is named ",[49,37680,37675],{},", Python may try to import your file instead of the standard library module. This can cause strange errors such as an ",[49,37683,37684],{},"AttributeError",[14,37686,37687],{},"Rename the file to something else, such as:",[309,37689,37690,37695,37700],{},[24,37691,37692],{},[49,37693,37694],{},"generate_numbers.py",[24,37696,37697],{},[49,37698,37699],{},"random_example.py",[24,37701,37702],{},[49,37703,37704],{},"test_random_values.py",[639,37706,37708],{"id":37707},"using-float-values-with-functions-that-expect-integers","Using float values with functions that expect integers",[14,37710,37711,759,37713,37715],{},[49,37712,36630],{},[49,37714,36720],{}," are meant for integer ranges.",[14,37717,37718],{},"This is not correct:",[42,37720,37722],{"className":44,"code":37721,"language":46,"meta":47,"style":47},"import random\n\nnumber = random.randint(1.5, 10.5)\nprint(number)\n",[49,37723,37724,37730,37734,37758],{"__ignoreMap":47},[52,37725,37726,37728],{"class":54,"line":55},[52,37727,59],{"class":58},[52,37729,36646],{"class":62},[52,37731,37732],{"class":54,"line":66},[52,37733,70],{"emptyLinePlaceholder":69},[52,37735,37736,37738,37740,37742,37744,37746,37748,37751,37753,37756],{"class":54,"line":73},[52,37737,32574],{"class":62},[52,37739,80],{"class":79},[52,37741,36659],{"class":62},[52,37743,186],{"class":102},[52,37745,36664],{"class":189},[52,37747,193],{"class":102},[52,37749,37750],{"class":121},"1.5",[52,37752,199],{"class":102},[52,37754,37755],{"class":121}," 10.5",[52,37757,211],{"class":102},[52,37759,37760,37762,37764,37766],{"class":54,"line":94},[52,37761,242],{"class":241},[52,37763,193],{"class":102},[52,37765,27497],{"class":189},[52,37767,211],{"class":102},[14,37769,37770,37771,37773,37774,37776],{},"Use integers with ",[49,37772,36630],{},", or use ",[49,37775,36723],{}," for decimal values.",[37,37778,37780],{"id":37779},"when-to-use-this","When to use this",[14,37782,37783],{},"Generating random numbers is useful in many beginner projects, including:",[309,37785,37786,37789,37792,37795],{},[24,37787,37788],{},"Picking a random number for a game",[24,37790,37791],{},"Creating test values",[24,37793,37794],{},"Choosing random positions or delays",[24,37796,37797],{},"Building practice projects",[14,37799,37800,37801,186],{},"A common next step is a guessing game. Try this related example: ",[372,37802,37804],{"href":37803},"\u002Fexamples\u002Fpython-number-guessing-game-example\u002F","Python number guessing game example",[37,37806,1942],{"id":1941},[639,37808,37810],{"id":37809},"how-do-i-generate-a-random-number-between-1-and-10-in-python","How do I generate a random number between 1 and 10 in Python?",[14,37812,3650,37813,37815,37816,759,37818,186],{},[49,37814,36743],{},". This includes both ",[49,37817,9200],{},[49,37819,3373],{},[42,37821,37822],{"className":44,"code":37343,"language":46,"meta":47,"style":47},[49,37823,37824,37830,37834],{"__ignoreMap":47},[52,37825,37826,37828],{"class":54,"line":55},[52,37827,59],{"class":58},[52,37829,36646],{"class":62},[52,37831,37832],{"class":54,"line":66},[52,37833,70],{"emptyLinePlaceholder":69},[52,37835,37836,37838,37840,37842,37844,37846,37848,37850,37852,37854],{"class":54,"line":73},[52,37837,242],{"class":241},[52,37839,193],{"class":102},[52,37841,36610],{"class":189},[52,37843,186],{"class":102},[52,37845,36664],{"class":189},[52,37847,193],{"class":102},[52,37849,9200],{"class":121},[52,37851,199],{"class":102},[52,37853,27053],{"class":121},[52,37855,8886],{"class":102},[639,37857,37859,37860,1995],{"id":37858},"why-do-i-get-nameerror-name-random-is-not-defined","Why do I get ",[49,37861,37862],{},"NameError: name 'random' is not defined",[14,37864,37865,37866,37868,37869,37872],{},"You need to add ",[49,37867,36739],{}," before using ",[49,37870,37871],{},"random.randint()"," or other random functions.",[42,37874,37875],{"className":44,"code":37343,"language":46,"meta":47,"style":47},[49,37876,37877,37883,37887],{"__ignoreMap":47},[52,37878,37879,37881],{"class":54,"line":55},[52,37880,59],{"class":58},[52,37882,36646],{"class":62},[52,37884,37885],{"class":54,"line":66},[52,37886,70],{"emptyLinePlaceholder":69},[52,37888,37889,37891,37893,37895,37897,37899,37901,37903,37905,37907],{"class":54,"line":73},[52,37890,242],{"class":241},[52,37892,193],{"class":102},[52,37894,36610],{"class":189},[52,37896,186],{"class":102},[52,37898,36664],{"class":189},[52,37900,193],{"class":102},[52,37902,9200],{"class":121},[52,37904,199],{"class":102},[52,37906,27053],{"class":121},[52,37908,8886],{"class":102},[639,37910,18248,37912,759,37914,1995],{"id":37911},"what-is-the-difference-between-randint-and-random",[49,37913,36630],{},[49,37915,36717],{},[309,37917,37918,37923],{},[24,37919,37920,37922],{},[49,37921,36630],{}," gives a whole number in a range",[24,37924,37925,37927,37928,759,37930],{},[49,37926,36717],{}," gives a decimal number between ",[49,37929,13294],{},[49,37931,9200],{},[14,37933,4259],{},[42,37935,37937],{"className":44,"code":37936,"language":46,"meta":47,"style":47},"import random\n\nprint(random.randint(1, 10))\nprint(random.random())\n",[49,37938,37939,37945,37949,37971],{"__ignoreMap":47},[52,37940,37941,37943],{"class":54,"line":55},[52,37942,59],{"class":58},[52,37944,36646],{"class":62},[52,37946,37947],{"class":54,"line":66},[52,37948,70],{"emptyLinePlaceholder":69},[52,37950,37951,37953,37955,37957,37959,37961,37963,37965,37967,37969],{"class":54,"line":73},[52,37952,242],{"class":241},[52,37954,193],{"class":102},[52,37956,36610],{"class":189},[52,37958,186],{"class":102},[52,37960,36664],{"class":189},[52,37962,193],{"class":102},[52,37964,9200],{"class":121},[52,37966,199],{"class":102},[52,37968,27053],{"class":121},[52,37970,8886],{"class":102},[52,37972,37973,37975,37977,37979,37981,37983],{"class":54,"line":94},[52,37974,242],{"class":241},[52,37976,193],{"class":102},[52,37978,36610],{"class":189},[52,37980,186],{"class":102},[52,37982,36610],{"class":189},[52,37984,1816],{"class":102},[639,37986,37988],{"id":37987},"can-i-generate-several-random-numbers-at-once","Can I generate several random numbers at once?",[14,37990,37991],{},"Yes. Use a loop or a list comprehension.",[42,37993,37994],{"className":44,"code":37038,"language":46,"meta":47,"style":47},[49,37995,37996,38002,38006,38044],{"__ignoreMap":47},[52,37997,37998,38000],{"class":54,"line":55},[52,37999,59],{"class":58},[52,38001,36646],{"class":62},[52,38003,38004],{"class":54,"line":66},[52,38005,70],{"emptyLinePlaceholder":69},[52,38007,38008,38010,38012,38014,38016,38018,38020,38022,38024,38026,38028,38030,38032,38034,38036,38038,38040,38042],{"class":54,"line":73},[52,38009,32092],{"class":62},[52,38011,80],{"class":79},[52,38013,18162],{"class":102},[52,38015,36610],{"class":62},[52,38017,186],{"class":102},[52,38019,36664],{"class":189},[52,38021,193],{"class":102},[52,38023,9200],{"class":121},[52,38025,199],{"class":102},[52,38027,27053],{"class":121},[52,38029,1521],{"class":102},[52,38031,37077],{"class":58},[52,38033,37080],{"class":62},[52,38035,11213],{"class":58},[52,38037,36864],{"class":241},[52,38039,193],{"class":102},[52,38041,9479],{"class":121},[52,38043,33167],{"class":102},[52,38045,38046,38048,38050,38052],{"class":54,"line":94},[52,38047,242],{"class":241},[52,38049,193],{"class":102},[52,38051,5324],{"class":189},[52,38053,211],{"class":102},[37,38055,2005],{"id":2004},[309,38057,38058,38062,38066,38070,38074,38078],{},[24,38059,38060],{},[372,38061,36730],{"href":36729},[24,38063,38064],{},[372,38065,36827],{"href":36826},[24,38067,38068],{},[372,38069,36929],{"href":36928},[24,38071,38072],{},[372,38073,34468],{"href":32073},[24,38075,38076],{},[372,38077,37804],{"href":37803},[24,38079,38080],{},[372,38081,38082],{"href":37520},"NameError: name is not defined fix",[2034,38084,38085],{},"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 .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);}",{"title":47,"searchDepth":66,"depth":66,"links":38087},[38088,38089,38092,38095,38100,38101,38113,38114,38122],{"id":2267,"depth":66,"text":2268},{"id":36733,"depth":66,"text":36734,"children":38090},[38091],{"id":24094,"depth":73,"text":24095},{"id":36830,"depth":66,"text":36831,"children":38093},[38094],{"id":36905,"depth":73,"text":36906},{"id":37103,"depth":66,"text":37104,"children":38096},[38097,38098,38099],{"id":37114,"depth":73,"text":37117},{"id":37178,"depth":73,"text":37181},{"id":37267,"depth":73,"text":37270},{"id":13562,"depth":66,"text":13563},{"id":6401,"depth":66,"text":6402,"children":38102},[38103,38105,38107,38109,38110,38112],{"id":37421,"depth":73,"text":38104},"Forgetting to import random",{"id":37524,"depth":73,"text":38106},"Using randint() without the module name",{"id":37632,"depth":73,"text":38108},"Confusing randint() and randrange()",{"id":37664,"depth":73,"text":37665},{"id":37671,"depth":73,"text":38111},"Naming your file random.py",{"id":37707,"depth":73,"text":37708},{"id":37779,"depth":66,"text":37780},{"id":1941,"depth":66,"text":1942,"children":38115},[38116,38117,38119,38121],{"id":37809,"depth":73,"text":37810},{"id":37858,"depth":73,"text":38118},"Why do I get NameError: name 'random' is not defined?",{"id":37911,"depth":73,"text":38120},"What is the difference between randint() and random()?",{"id":37987,"depth":73,"text":37988},{"id":2004,"depth":66,"text":2005},"Master python generate random numbers example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-generate-random-numbers-example",{"title":36599,"description":38123},"examples\u002Fpython-generate-random-numbers-example","wBuYGdtmBnLM3b0St2JTFh8YXIYdEUfqxGKYMJwSc3s",{"id":38130,"title":38131,"body":38132,"description":39006,"extension":2075,"meta":39007,"navigation":69,"path":39008,"seo":39009,"stem":39010,"__hash__":39011},"content\u002Fexamples\u002Fpython-hello-world-example-explained.md","Python Hello World Example Explained",{"type":7,"value":38133,"toc":38973},[38134,38137,38140,38160,38166,38168,38171,38185,38188,38192,38195,38213,38216,38235,38242,38244,38247,38253,38256,38293,38296,38307,38311,38314,38318,38321,38330,38333,38342,38345,38363,38366,38371,38375,38381,38399,38402,38414,38417,38429,38433,38436,38444,38447,38454,38458,38461,38479,38482,38504,38507,38524,38532,38536,38539,38543,38563,38567,38587,38594,38628,38630,38636,38639,38643,38670,38673,38675,38678,38682,38685,38706,38712,38715,38733,38738,38744,38746,38766,38769,38771,38789,38793,38796,38817,38820,38838,38842,38845,38848,38877,38879,38898,38905,38907,38913,38918,38922,38925,38929,38932,38936,38939,38941,38970],[10,38135,38131],{"id":38136},"python-hello-world-example-explained",[14,38138,38139],{},"This beginner-friendly example shows the simplest useful Python program. You will learn what it does, how to run it, and how to fix a few common mistakes.",[42,38141,38143],{"className":44,"code":38142,"language":46,"meta":47,"style":47},"print(\"Hello, world!\")\n",[49,38144,38145],{"__ignoreMap":47},[52,38146,38147,38149,38151,38153,38156,38158],{"class":54,"line":55},[52,38148,242],{"class":241},[52,38150,193],{"class":102},[52,38152,115],{"class":83},[52,38154,38155],{"class":87},"Hello, world!",[52,38157,115],{"class":83},[52,38159,211],{"class":102},[14,38161,38162,38163,38165],{},"Save this in a ",[49,38164,10045],{}," file or run it in the Python interpreter to see your first working Python output.",[37,38167,2268],{"id":2267},[14,38169,38170],{},"This example teaches a few important basics:",[309,38172,38173,38176,38179,38182],{},[24,38174,38175],{},"The smallest useful Python program",[24,38177,38178],{},"How to display text on the screen",[24,38180,38181],{},"How Python runs code from top to bottom",[24,38183,38184],{},"Why this is often the first program beginners write",[14,38186,38187],{},"Even though it is only one line long, it introduces an important idea: Python can execute your code and produce visible output right away.",[37,38189,38191],{"id":38190},"the-hello-world-code","The Hello World code",[14,38193,38194],{},"Here is the full program:",[42,38196,38197],{"className":44,"code":38142,"language":46,"meta":47,"style":47},[49,38198,38199],{"__ignoreMap":47},[52,38200,38201,38203,38205,38207,38209,38211],{"class":54,"line":55},[52,38202,242],{"class":241},[52,38204,193],{"class":102},[52,38206,115],{"class":83},[52,38208,38155],{"class":87},[52,38210,115],{"class":83},[52,38212,211],{"class":102},[14,38214,38215],{},"What this line means:",[309,38217,38218,38223,38229,38232],{},[24,38219,38220,38222],{},[49,38221,9687],{}," sends output to the screen",[24,38224,38225,38228],{},[49,38226,38227],{},"\"Hello, world!\""," is the text to display",[24,38230,38231],{},"The text is inside quotes, so Python treats it as a string",[24,38233,38234],{},"In Python 3, the parentheses are required",[14,38236,38237,38238,186],{},"If you want a deeper explanation of this function, see ",[372,38239,10646,38240,3711],{"href":9684},[49,38241,9687],{},[37,38243,13563],{"id":13562},[14,38245,38246],{},"When you run the code, the output should be:",[42,38248,38251],{"className":38249,"code":38250,"language":955,"meta":47},[953],"Hello, world!\n",[49,38252,38250],{"__ignoreMap":47},[14,38254,38255],{},"A beginner mistake is to confuse code with output.",[309,38257,38258,38282],{},[24,38259,38260,38263,38264],{},[5503,38261,38262],{},"Code"," is what you write:\n",[42,38265,38266],{"className":44,"code":38142,"language":46,"meta":47,"style":47},[49,38267,38268],{"__ignoreMap":47},[52,38269,38270,38272,38274,38276,38278,38280],{"class":54,"line":55},[52,38271,242],{"class":241},[52,38273,193],{"class":102},[52,38275,115],{"class":83},[52,38277,38155],{"class":87},[52,38279,115],{"class":83},[52,38281,211],{"class":102},[24,38283,38284,38287,38288],{},[5503,38285,38286],{},"Output"," is what Python shows after running the code:\n",[42,38289,38291],{"className":38290,"code":38250,"language":955,"meta":47},[953],[49,38292,38250],{"__ignoreMap":47},[14,38294,38295],{},"You will usually see the output in one of these places:",[309,38297,38298,38301,38304],{},[24,38299,38300],{},"The terminal",[24,38302,38303],{},"The Python shell",[24,38305,38306],{},"The output panel in an IDE",[37,38308,38310],{"id":38309},"how-to-run-the-example","How to run the example",[14,38312,38313],{},"Before running the program, make sure Python is installed on your computer.",[639,38315,38317],{"id":38316},"run-it-in-the-python-interpreter","Run it in the Python interpreter",[14,38319,38320],{},"Open a terminal or command prompt and start Python:",[42,38322,38324],{"className":390,"code":38323,"language":392,"meta":47,"style":47},"python\n",[49,38325,38326],{"__ignoreMap":47},[52,38327,38328],{"class":54,"line":55},[52,38329,38323],{"class":399},[14,38331,38332],{},"On some systems, you may need:",[42,38334,38336],{"className":390,"code":38335,"language":392,"meta":47,"style":47},"python3\n",[49,38337,38338],{"__ignoreMap":47},[52,38339,38340],{"class":54,"line":55},[52,38341,38335],{"class":399},[14,38343,38344],{},"Then type:",[42,38346,38347],{"className":44,"code":38142,"language":46,"meta":47,"style":47},[49,38348,38349],{"__ignoreMap":47},[52,38350,38351,38353,38355,38357,38359,38361],{"class":54,"line":55},[52,38352,242],{"class":241},[52,38354,193],{"class":102},[52,38356,115],{"class":83},[52,38358,38155],{"class":87},[52,38360,115],{"class":83},[52,38362,211],{"class":102},[14,38364,38365],{},"Press Enter, and Python should display:",[42,38367,38369],{"className":38368,"code":38250,"language":955,"meta":47},[953],[49,38370,38250],{"__ignoreMap":47},[639,38372,38374],{"id":38373},"run-it-from-a-file","Run it from a file",[14,38376,14768,38377,38380],{},[49,38378,38379],{},"hello.py"," and put this code inside it:",[42,38382,38383],{"className":44,"code":38142,"language":46,"meta":47,"style":47},[49,38384,38385],{"__ignoreMap":47},[52,38386,38387,38389,38391,38393,38395,38397],{"class":54,"line":55},[52,38388,242],{"class":241},[52,38390,193],{"class":102},[52,38392,115],{"class":83},[52,38394,38155],{"class":87},[52,38396,115],{"class":83},[52,38398,211],{"class":102},[14,38400,38401],{},"Then run the file from your terminal:",[42,38403,38405],{"className":390,"code":38404,"language":392,"meta":47,"style":47},"python hello.py\n",[49,38406,38407],{"__ignoreMap":47},[52,38408,38409,38411],{"class":54,"line":55},[52,38410,46],{"class":399},[52,38412,38413],{"class":87}," hello.py\n",[14,38415,38416],{},"Or, on some systems:",[42,38418,38420],{"className":390,"code":38419,"language":392,"meta":47,"style":47},"python3 hello.py\n",[49,38421,38422],{"__ignoreMap":47},[52,38423,38424,38427],{"class":54,"line":55},[52,38425,38426],{"class":399},"python3",[52,38428,38413],{"class":87},[639,38430,38432],{"id":38431},"run-it-in-an-ide","Run it in an IDE",[14,38434,38435],{},"You can also run the program in tools like:",[309,38437,38438,38441],{},[24,38439,38440],{},"VS Code",[24,38442,38443],{},"IDLE",[14,38445,38446],{},"Create a new Python file, paste in the code, and use the IDE's run button or menu option.",[14,38448,38449,38450,186],{},"If you need more help with this step, see ",[372,38451,38453],{"href":38452},"\u002Flearn\u002Fhow-to-run-python-code-command-line-and-ides","how to run Python code from the command line and IDEs",[37,38455,38457],{"id":38456},"line-by-line-explanation","Line-by-line explanation",[14,38459,38460],{},"This program has only one line:",[42,38462,38463],{"className":44,"code":38142,"language":46,"meta":47,"style":47},[49,38464,38465],{"__ignoreMap":47},[52,38466,38467,38469,38471,38473,38475,38477],{"class":54,"line":55},[52,38468,242],{"class":241},[52,38470,193],{"class":102},[52,38472,115],{"class":83},[52,38474,38155],{"class":87},[52,38476,115],{"class":83},[52,38478,211],{"class":102},[14,38480,38481],{},"Here is what each part does:",[309,38483,38484,38489,38495,38501],{},[24,38485,38486,38488],{},[49,38487,242],{}," is a built-in Python function",[24,38490,38491,38492,38494],{},"The parentheses ",[49,38493,8267],{}," hold the value passed to the function",[24,38496,38497,38498,38500],{},"The quotes tell Python that ",[49,38499,38155],{}," is text, not code",[24,38502,38503],{},"After printing the message, the program ends",[14,38505,38506],{},"You can think of it like this:",[309,38508,38509,38517],{},[24,38510,38511,38514,38515],{},[5503,38512,38513],{},"Function name:"," ",[49,38516,242],{},[24,38518,38519,38514,38522],{},[5503,38520,38521],{},"Value given to the function:",[49,38523,38227],{},[14,38525,38526,38527,38531],{},"If you are new to Python syntax, ",[372,38528,38530],{"href":38529},"\u002Flearn\u002Fpython-syntax-basics-explained\u002F","Python syntax basics explained"," is a good next step.",[37,38533,38535],{"id":38534},"simple-variations-to-try","Simple variations to try",[14,38537,38538],{},"Once the first version works, try changing it.",[639,38540,38542],{"id":38541},"change-the-message-text","Change the message text",[42,38544,38546],{"className":44,"code":38545,"language":46,"meta":47,"style":47},"print(\"Welcome to Python!\")\n",[49,38547,38548],{"__ignoreMap":47},[52,38549,38550,38552,38554,38556,38559,38561],{"class":54,"line":55},[52,38551,242],{"class":241},[52,38553,193],{"class":102},[52,38555,115],{"class":83},[52,38557,38558],{"class":87},"Welcome to Python!",[52,38560,115],{"class":83},[52,38562,211],{"class":102},[639,38564,38566],{"id":38565},"print-your-name","Print your name",[42,38568,38570],{"className":44,"code":38569,"language":46,"meta":47,"style":47},"print(\"My name is Sam\")\n",[49,38571,38572],{"__ignoreMap":47},[52,38573,38574,38576,38578,38580,38583,38585],{"class":54,"line":55},[52,38575,242],{"class":241},[52,38577,193],{"class":102},[52,38579,115],{"class":83},[52,38581,38582],{"class":87},"My name is Sam",[52,38584,115],{"class":83},[52,38586,211],{"class":102},[639,38588,38590,38591,38593],{"id":38589},"use-two-print-lines","Use two ",[49,38592,9687],{}," lines",[42,38595,38597],{"className":44,"code":38596,"language":46,"meta":47,"style":47},"print(\"Hello, world!\")\nprint(\"I am learning Python.\")\n",[49,38598,38599,38613],{"__ignoreMap":47},[52,38600,38601,38603,38605,38607,38609,38611],{"class":54,"line":55},[52,38602,242],{"class":241},[52,38604,193],{"class":102},[52,38606,115],{"class":83},[52,38608,38155],{"class":87},[52,38610,115],{"class":83},[52,38612,211],{"class":102},[52,38614,38615,38617,38619,38621,38624,38626],{"class":54,"line":66},[52,38616,242],{"class":241},[52,38618,193],{"class":102},[52,38620,115],{"class":83},[52,38622,38623],{"class":87},"I am learning Python.",[52,38625,115],{"class":83},[52,38627,211],{"class":102},[14,38629,9266],{},[42,38631,38634],{"className":38632,"code":38633,"language":955,"meta":47},[953],"Hello, world!\nI am learning Python.\n",[49,38635,38633],{"__ignoreMap":47},[14,38637,38638],{},"Python runs code from top to bottom, so the first line prints before the second line.",[639,38640,38642],{"id":38641},"store-text-in-a-variable-and-print-the-variable","Store text in a variable and print the variable",[42,38644,38646],{"className":44,"code":38645,"language":46,"meta":47,"style":47},"message = \"Hello, world!\"\nprint(message)\n",[49,38647,38648,38660],{"__ignoreMap":47},[52,38649,38650,38652,38654,38656,38658],{"class":54,"line":55},[52,38651,6121],{"class":62},[52,38653,80],{"class":79},[52,38655,84],{"class":83},[52,38657,38155],{"class":87},[52,38659,91],{"class":83},[52,38661,38662,38664,38666,38668],{"class":54,"line":66},[52,38663,242],{"class":241},[52,38665,193],{"class":102},[52,38667,6188],{"class":189},[52,38669,211],{"class":102},[14,38671,38672],{},"This introduces a variable, which is a name that stores a value.",[37,38674,6402],{"id":6401},[14,38676,38677],{},"Here are some problems beginners often run into.",[639,38679,38681],{"id":38680},"forgetting-quotation-marks","Forgetting quotation marks",[14,38683,38684],{},"This code is wrong:",[42,38686,38688],{"className":44,"code":38687,"language":46,"meta":47,"style":47},"print(Hello, world!)\n",[49,38689,38690],{"__ignoreMap":47},[52,38691,38692,38694,38696,38699,38701,38704],{"class":54,"line":55},[52,38693,242],{"class":241},[52,38695,193],{"class":102},[52,38697,38698],{"class":189},"Hello",[52,38700,199],{"class":102},[52,38702,38703],{"class":189}," world!",[52,38705,211],{"class":102},[14,38707,38708,38709,38711],{},"Python will not understand ",[49,38710,38155],{}," as text because it is not inside quotes.",[14,38713,38714],{},"Use this instead:",[42,38716,38717],{"className":44,"code":38142,"language":46,"meta":47,"style":47},[49,38718,38719],{"__ignoreMap":47},[52,38720,38721,38723,38725,38727,38729,38731],{"class":54,"line":55},[52,38722,242],{"class":241},[52,38724,193],{"class":102},[52,38726,115],{"class":83},[52,38728,38155],{"class":87},[52,38730,115],{"class":83},[52,38732,211],{"class":102},[14,38734,38735,38736,186],{},"If you forget quotes, you may get an error such as ",[372,38737,37521],{"href":37520},[639,38739,38741,38742],{"id":38740},"misspelling-print","Misspelling ",[49,38743,242],{},[14,38745,6505],{},[42,38747,38749],{"className":44,"code":38748,"language":46,"meta":47,"style":47},"pritn(\"Hello, world!\")\n",[49,38750,38751],{"__ignoreMap":47},[52,38752,38753,38756,38758,38760,38762,38764],{"class":54,"line":55},[52,38754,38755],{"class":189},"pritn",[52,38757,193],{"class":102},[52,38759,115],{"class":83},[52,38761,38155],{"class":87},[52,38763,115],{"class":83},[52,38765,211],{"class":102},[14,38767,38768],{},"Function names must be spelled exactly right.",[14,38770,12506],{},[42,38772,38773],{"className":44,"code":38142,"language":46,"meta":47,"style":47},[49,38774,38775],{"__ignoreMap":47},[52,38776,38777,38779,38781,38783,38785,38787],{"class":54,"line":55},[52,38778,242],{"class":241},[52,38780,193],{"class":102},[52,38782,115],{"class":83},[52,38784,38155],{"class":87},[52,38786,115],{"class":83},[52,38788,211],{"class":102},[639,38790,38792],{"id":38791},"using-smart-quotes","Using smart quotes",[14,38794,38795],{},"Sometimes code copied from a document or messaging app uses curly quotes like these:",[42,38797,38799],{"className":44,"code":38798,"language":46,"meta":47,"style":47},"print(“Hello, world!”)\n",[49,38800,38801],{"__ignoreMap":47},[52,38802,38803,38805,38807,38810,38812,38815],{"class":54,"line":55},[52,38804,242],{"class":241},[52,38806,193],{"class":102},[52,38808,38809],{"class":189},"“Hello",[52,38811,199],{"class":102},[52,38813,38814],{"class":189}," world!”",[52,38816,211],{"class":102},[14,38818,38819],{},"Those are not normal Python string quotes. Use straight quotes instead:",[42,38821,38822],{"className":44,"code":38142,"language":46,"meta":47,"style":47},[49,38823,38824],{"__ignoreMap":47},[52,38825,38826,38828,38830,38832,38834,38836],{"class":54,"line":55},[52,38827,242],{"class":241},[52,38829,193],{"class":102},[52,38831,115],{"class":83},[52,38833,38155],{"class":87},[52,38835,115],{"class":83},[52,38837,211],{"class":102},[639,38839,38841],{"id":38840},"running-python-code-in-the-wrong-tool","Running Python code in the wrong tool",[14,38843,38844],{},"Make sure you are actually running Python.",[14,38846,38847],{},"Useful commands to check:",[42,38849,38851],{"className":390,"code":38850,"language":392,"meta":47,"style":47},"python --version\npython hello.py\npython3 --version\npython3 hello.py\n",[49,38852,38853,38859,38865,38871],{"__ignoreMap":47},[52,38854,38855,38857],{"class":54,"line":55},[52,38856,46],{"class":399},[52,38858,2324],{"class":420},[52,38860,38861,38863],{"class":54,"line":66},[52,38862,46],{"class":399},[52,38864,38413],{"class":87},[52,38866,38867,38869],{"class":54,"line":73},[52,38868,38426],{"class":399},[52,38870,2324],{"class":420},[52,38872,38873,38875],{"class":54,"line":94},[52,38874,38426],{"class":399},[52,38876,38413],{"class":87},[14,38878,31876],{},[309,38880,38881,38884,38887,38892,38895],{},[24,38882,38883],{},"Python is not installed or not added to PATH",[24,38885,38886],{},"The file is saved with the wrong extension",[24,38888,38889,38890],{},"Quotes are missing around ",[49,38891,38155],{},[24,38893,38894],{},"The code uses curly quotes instead of normal quotes",[24,38896,38897],{},"The program is run with the wrong command",[14,38899,38900,38901,186],{},"If Python reports a syntax problem, see ",[372,38902,38904],{"href":38903},"\u002Ferrors\u002Fsyntaxerror-invalid-syntax-fix\u002F","SyntaxError: invalid syntax",[37,38906,1942],{"id":1941},[639,38908,38910,38911,1995],{"id":38909},"why-does-hello-world-use-print","Why does Hello World use ",[49,38912,9687],{},[14,38914,14459,38915,38917],{},[49,38916,9687],{}," is the simplest way to show output on the screen in Python.",[639,38919,38921],{"id":38920},"do-i-need-to-save-the-code-in-a-file-first","Do I need to save the code in a file first?",[14,38923,38924],{},"No. You can run it directly in the Python interpreter or save it in a file and run that file.",[639,38926,38928],{"id":38927},"why-am-i-getting-a-syntax-error","Why am I getting a syntax error?",[14,38930,38931],{},"This usually happens if the quotes, parentheses, or spelling are incorrect.",[639,38933,38935],{"id":38934},"what-should-i-learn-after-hello-world","What should I learn after Hello World?",[14,38937,38938],{},"A good next step is Python syntax, variables, strings, and how to run Python code.",[37,38940,2005],{"id":2004},[309,38942,38943,38949,38954,38960,38964],{},[24,38944,38945],{},[372,38946,38948],{"href":38947},"\u002Flearn\u002Fyour-first-python-program-hello-world-explained\u002F","Your first Python program: Hello World explained",[24,38950,38951],{},[372,38952,38953],{"href":38452},"How to run Python code from the command line and IDEs",[24,38955,38956],{},[372,38957,10646,38958,3711],{"href":9684},[49,38959,9687],{},[24,38961,38962],{},[372,38963,38530],{"href":38529},[24,38965,38966],{},[372,38967,38969],{"href":38968},"\u002Flearn\u002Fpython-strings-explained-basics-and-examples\u002F","Python strings explained: basics and examples",[2034,38971,38972],{},"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 .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 .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":38974},[38975,38976,38977,38978,38983,38984,38991,38998,39005],{"id":2267,"depth":66,"text":2268},{"id":38190,"depth":66,"text":38191},{"id":13562,"depth":66,"text":13563},{"id":38309,"depth":66,"text":38310,"children":38979},[38980,38981,38982],{"id":38316,"depth":73,"text":38317},{"id":38373,"depth":73,"text":38374},{"id":38431,"depth":73,"text":38432},{"id":38456,"depth":66,"text":38457},{"id":38534,"depth":66,"text":38535,"children":38985},[38986,38987,38988,38990],{"id":38541,"depth":73,"text":38542},{"id":38565,"depth":73,"text":38566},{"id":38589,"depth":73,"text":38989},"Use two print() lines",{"id":38641,"depth":73,"text":38642},{"id":6401,"depth":66,"text":6402,"children":38992},[38993,38994,38996,38997],{"id":38680,"depth":73,"text":38681},{"id":38740,"depth":73,"text":38995},"Misspelling print",{"id":38791,"depth":73,"text":38792},{"id":38840,"depth":73,"text":38841},{"id":1941,"depth":66,"text":1942,"children":38999},[39000,39002,39003,39004],{"id":38909,"depth":73,"text":39001},"Why does Hello World use print()?",{"id":38920,"depth":73,"text":38921},{"id":38927,"depth":73,"text":38928},{"id":38934,"depth":73,"text":38935},{"id":2004,"depth":66,"text":2005},"Master python hello world example explained in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-hello-world-example-explained",{"title":38131,"description":39006},"examples\u002Fpython-hello-world-example-explained","JlHu0l2oJg-6wu7EYwTMINheQzF8c63goXlFrcLXfqI",{"id":39013,"title":39014,"body":39015,"description":40403,"extension":2075,"meta":40404,"navigation":69,"path":40405,"seo":40406,"stem":40407,"__hash__":40408},"content\u002Fexamples\u002Fpython-inheritance-example.md","Python Inheritance Example",{"type":7,"value":39016,"toc":40378},[39017,39020,39023,39026,39028,39150,39154,39169,39171,39201,39203,39217,39227,39231,39233,39314,39317,39350,39356,39370,39377,39381,39385,39424,39429,39434,39438,39481,39490,39499,39503,39518,39527,39532,39536,39551,39566,39570,39585,39594,39598,39601,39604,39618,39621,39627,39631,39634,39648,39652,39655,39762,39765,39792,39795,39820,39822,39825,39839,39842,39848,39852,39855,39925,39928,39963,39965,40159,40164,40201,40204,40228,40230,40234,40237,40241,40244,40248,40251,40255,40258,40261,40342,40351,40353,40375],[10,39018,39014],{"id":39019},"python-inheritance-example",[14,39021,39022],{},"Inheritance is a way to reuse code in Python classes. In this example, a child class inherits from a parent class and can use the parent class method while also adding its own method.",[14,39024,39025],{},"This page gives you a simple, beginner-friendly example so you can see how inheritance works in real code.",[37,39027,40],{"id":39},[42,39029,39031],{"className":44,"code":39030,"language":46,"meta":47,"style":47},"class Animal:\n    def speak(self):\n        print(\"Animal sound\")\n\nclass Dog(Animal):\n    def bark(self):\n        print(\"Woof!\")\n\ndog = Dog()\ndog.speak()\ndog.bark()\n",[49,39032,39033,39042,39055,39070,39074,39087,39099,39113,39117,39128,39140],{"__ignoreMap":47},[52,39034,39035,39037,39040],{"class":54,"line":55},[52,39036,6994],{"class":1475},[52,39038,39039],{"class":399}," Animal",[52,39041,1330],{"class":102},[52,39043,39044,39046,39049,39051,39053],{"class":54,"line":66},[52,39045,7004],{"class":1475},[52,39047,39048],{"class":7065}," speak",[52,39050,193],{"class":102},[52,39052,7013],{"class":7012},[52,39054,3114],{"class":102},[52,39056,39057,39059,39061,39063,39066,39068],{"class":54,"line":73},[52,39058,1470],{"class":241},[52,39060,193],{"class":102},[52,39062,115],{"class":83},[52,39064,39065],{"class":87},"Animal sound",[52,39067,115],{"class":83},[52,39069,211],{"class":102},[52,39071,39072],{"class":54,"line":94},[52,39073,70],{"emptyLinePlaceholder":69},[52,39075,39076,39078,39080,39082,39085],{"class":54,"line":106},[52,39077,6994],{"class":1475},[52,39079,6997],{"class":399},[52,39081,193],{"class":102},[52,39083,39084],{"class":399},"Animal",[52,39086,3114],{"class":102},[52,39088,39089,39091,39093,39095,39097],{"class":54,"line":128},[52,39090,7004],{"class":1475},[52,39092,7066],{"class":7065},[52,39094,193],{"class":102},[52,39096,7013],{"class":7012},[52,39098,3114],{"class":102},[52,39100,39101,39103,39105,39107,39109,39111],{"class":54,"line":148},[52,39102,1470],{"class":241},[52,39104,193],{"class":102},[52,39106,115],{"class":83},[52,39108,8157],{"class":87},[52,39110,115],{"class":83},[52,39112,211],{"class":102},[52,39114,39115],{"class":54,"line":164},[52,39116,70],{"emptyLinePlaceholder":69},[52,39118,39119,39122,39124,39126],{"class":54,"line":170},[52,39120,39121],{"class":62},"dog ",[52,39123,80],{"class":79},[52,39125,6997],{"class":189},[52,39127,230],{"class":102},[52,39129,39130,39133,39135,39138],{"class":54,"line":175},[52,39131,39132],{"class":62},"dog",[52,39134,186],{"class":102},[52,39136,39137],{"class":189},"speak",[52,39139,230],{"class":102},[52,39141,39142,39144,39146,39148],{"class":54,"line":214},[52,39143,39132],{"class":62},[52,39145,186],{"class":102},[52,39147,7163],{"class":189},[52,39149,230],{"class":102},[14,39151,39152],{},[5503,39153,5267],{},[42,39155,39157],{"className":44,"code":39156,"language":46,"meta":47,"style":47},"Animal sound\nWoof!\n",[49,39158,39159,39164],{"__ignoreMap":47},[52,39160,39161],{"class":54,"line":55},[52,39162,39163],{"class":62},"Animal sound\n",[52,39165,39166],{"class":54,"line":66},[52,39167,39168],{"class":62},"Woof!\n",[14,39170,752],{},[309,39172,39173,39178,39183,39194],{},[24,39174,39175,39177],{},[49,39176,39084],{}," is the parent class",[24,39179,39180,39182],{},[49,39181,7369],{}," is the child class",[24,39184,39185,39187,39188,39191,39192],{},[49,39186,7369],{}," inherits the ",[49,39189,39190],{},"speak()"," method from ",[49,39193,39084],{},[24,39195,39196,39198,39199,7203],{},[49,39197,7369],{}," also has its own ",[49,39200,7382],{},[37,39202,7216],{"id":7215},[309,39204,39205,39208,39211,39214],{},[24,39206,39207],{},"A child class can inherit from a parent class",[24,39209,39210],{},"The child class gets the parent class methods",[24,39212,39213],{},"The child class can also add new methods of its own",[24,39215,39216],{},"Inheritance helps avoid repeating code",[14,39218,39219,39220,39222,39223,186],{},"If you are new to this topic, it may help to first read ",[372,39221,7255],{"href":7254}," or the glossary page on ",[372,39224,39226],{"href":39225},"\u002Fglossary\u002Fwhat-is-a-class-in-python\u002F","what a class is in Python",[37,39228,39230],{"id":39229},"parent-class-and-child-class","Parent class and child class",[14,39232,457],{},[42,39234,39236],{"className":44,"code":39235,"language":46,"meta":47,"style":47},"class Animal:\n    def speak(self):\n        print(\"Animal sound\")\n\nclass Dog(Animal):\n    def bark(self):\n        print(\"Woof!\")\n",[49,39237,39238,39246,39258,39272,39276,39288,39300],{"__ignoreMap":47},[52,39239,39240,39242,39244],{"class":54,"line":55},[52,39241,6994],{"class":1475},[52,39243,39039],{"class":399},[52,39245,1330],{"class":102},[52,39247,39248,39250,39252,39254,39256],{"class":54,"line":66},[52,39249,7004],{"class":1475},[52,39251,39048],{"class":7065},[52,39253,193],{"class":102},[52,39255,7013],{"class":7012},[52,39257,3114],{"class":102},[52,39259,39260,39262,39264,39266,39268,39270],{"class":54,"line":73},[52,39261,1470],{"class":241},[52,39263,193],{"class":102},[52,39265,115],{"class":83},[52,39267,39065],{"class":87},[52,39269,115],{"class":83},[52,39271,211],{"class":102},[52,39273,39274],{"class":54,"line":94},[52,39275,70],{"emptyLinePlaceholder":69},[52,39277,39278,39280,39282,39284,39286],{"class":54,"line":106},[52,39279,6994],{"class":1475},[52,39281,6997],{"class":399},[52,39283,193],{"class":102},[52,39285,39084],{"class":399},[52,39287,3114],{"class":102},[52,39289,39290,39292,39294,39296,39298],{"class":54,"line":128},[52,39291,7004],{"class":1475},[52,39293,7066],{"class":7065},[52,39295,193],{"class":102},[52,39297,7013],{"class":7012},[52,39299,3114],{"class":102},[52,39301,39302,39304,39306,39308,39310,39312],{"class":54,"line":148},[52,39303,1470],{"class":241},[52,39305,193],{"class":102},[52,39307,115],{"class":83},[52,39309,8157],{"class":87},[52,39311,115],{"class":83},[52,39313,211],{"class":102},[14,39315,39316],{},"What each part means:",[309,39318,39319,39327,39334,39344],{},[24,39320,39321,39323,39324],{},[49,39322,39084],{}," is the ",[5503,39325,39326],{},"parent class",[24,39328,39329,39323,39331],{},[49,39330,7369],{},[5503,39332,39333],{},"child class",[24,39335,39336,24324,39339,39341,39342],{},[49,39337,39338],{},"Dog(Animal)",[49,39340,7369],{}," inherits from ",[49,39343,39084],{},[24,39345,39346,39347,39349],{},"Objects created from ",[49,39348,7369],{}," can use methods from both classes",[14,39351,39352,39353,39355],{},"So when you create a ",[49,39354,7369],{}," object, Python lets that object use:",[309,39357,39358,39364],{},[24,39359,39360,28921,39362],{},[49,39361,39190],{},[49,39363,39084],{},[24,39365,39366,28921,39368],{},[49,39367,7382],{},[49,39369,7369],{},[14,39371,39372,39373,186],{},"This is the main idea behind ",[372,39374,39376],{"href":39375},"\u002Fglossary\u002Fwhat-is-inheritance-in-python\u002F","inheritance in Python",[37,39378,39380],{"id":39379},"how-the-example-works-step-by-step","How the example works step by step",[639,39382,39384],{"id":39383},"_1-define-a-parent-class-with-one-method","1. Define a parent class with one method",[42,39386,39388],{"className":44,"code":39387,"language":46,"meta":47,"style":47},"class Animal:\n    def speak(self):\n        print(\"Animal sound\")\n",[49,39389,39390,39398,39410],{"__ignoreMap":47},[52,39391,39392,39394,39396],{"class":54,"line":55},[52,39393,6994],{"class":1475},[52,39395,39039],{"class":399},[52,39397,1330],{"class":102},[52,39399,39400,39402,39404,39406,39408],{"class":54,"line":66},[52,39401,7004],{"class":1475},[52,39403,39048],{"class":7065},[52,39405,193],{"class":102},[52,39407,7013],{"class":7012},[52,39409,3114],{"class":102},[52,39411,39412,39414,39416,39418,39420,39422],{"class":54,"line":73},[52,39413,1470],{"class":241},[52,39415,193],{"class":102},[52,39417,115],{"class":83},[52,39419,39065],{"class":87},[52,39421,115],{"class":83},[52,39423,211],{"class":102},[14,39425,39426,39427,186],{},"This creates a class named ",[49,39428,39084],{},[14,39430,39431,39432,186],{},"Inside it, there is one method: ",[49,39433,39190],{},[639,39435,39437],{"id":39436},"_2-define-a-child-class-that-inherits-from-the-parent","2. Define a child class that inherits from the parent",[42,39439,39441],{"className":44,"code":39440,"language":46,"meta":47,"style":47},"class Dog(Animal):\n    def bark(self):\n        print(\"Woof!\")\n",[49,39442,39443,39455,39467],{"__ignoreMap":47},[52,39444,39445,39447,39449,39451,39453],{"class":54,"line":55},[52,39446,6994],{"class":1475},[52,39448,6997],{"class":399},[52,39450,193],{"class":102},[52,39452,39084],{"class":399},[52,39454,3114],{"class":102},[52,39456,39457,39459,39461,39463,39465],{"class":54,"line":66},[52,39458,7004],{"class":1475},[52,39460,7066],{"class":7065},[52,39462,193],{"class":102},[52,39464,7013],{"class":7012},[52,39466,3114],{"class":102},[52,39468,39469,39471,39473,39475,39477,39479],{"class":54,"line":73},[52,39470,1470],{"class":241},[52,39472,193],{"class":102},[52,39474,115],{"class":83},[52,39476,8157],{"class":87},[52,39478,115],{"class":83},[52,39480,211],{"class":102},[14,39482,39483,24324,39485,39487,39488,186],{},[49,39484,39338],{},[49,39486,7369],{}," is based on ",[49,39489,39084],{},[14,39491,39492,39493,39495,39496,39498],{},"Because of this, ",[49,39494,7369],{}," gets access to the ",[49,39497,39190],{}," method without writing it again.",[639,39500,39502],{"id":39501},"_3-create-an-object-from-the-child-class","3. Create an object from the child class",[42,39504,39506],{"className":44,"code":39505,"language":46,"meta":47,"style":47},"dog = Dog()\n",[49,39507,39508],{"__ignoreMap":47},[52,39509,39510,39512,39514,39516],{"class":54,"line":55},[52,39511,39121],{"class":62},[52,39513,80],{"class":79},[52,39515,6997],{"class":189},[52,39517,230],{"class":102},[14,39519,39520,39521,39523,39524,39526],{},"This creates an object named ",[49,39522,39132],{}," from the ",[49,39525,7369],{}," class.",[14,39528,39529,39530,186],{},"If you need more practice with this step, see ",[372,39531,7710],{"href":7709},[639,39533,39535],{"id":39534},"_4-call-an-inherited-method","4. Call an inherited method",[42,39537,39539],{"className":44,"code":39538,"language":46,"meta":47,"style":47},"dog.speak()\n",[49,39540,39541],{"__ignoreMap":47},[52,39542,39543,39545,39547,39549],{"class":54,"line":55},[52,39544,39132],{"class":62},[52,39546,186],{"class":102},[52,39548,39137],{"class":189},[52,39550,230],{"class":102},[14,39552,21354,39553,39555,39556,39558,39559,39561,39562,39341,39564,186],{},[49,39554,39190],{}," is written inside ",[49,39557,39084],{},", the ",[49,39560,39132],{}," object can still use it because ",[49,39563,7369],{},[49,39565,39084],{},[639,39567,39569],{"id":39568},"_5-call-a-method-defined-in-the-child-class","5. Call a method defined in the child class",[42,39571,39573],{"className":44,"code":39572,"language":46,"meta":47,"style":47},"dog.bark()\n",[49,39574,39575],{"__ignoreMap":47},[52,39576,39577,39579,39581,39583],{"class":54,"line":55},[52,39578,39132],{"class":62},[52,39580,186],{"class":102},[52,39582,7163],{"class":189},[52,39584,230],{"class":102},[14,39586,39587,39588,39590,39591,39593],{},"This method belongs directly to ",[49,39589,7369],{},", so the ",[49,39592,39132],{}," object can use it too.",[37,39595,39597],{"id":39596},"why-beginners-use-inheritance","Why beginners use inheritance",[14,39599,39600],{},"Beginners often use inheritance when they want related classes to share behavior.",[14,39602,39603],{},"It helps because:",[309,39605,39606,39609,39612,39615],{},[24,39607,39608],{},"It groups related classes together",[24,39610,39611],{},"It lets you reuse existing code",[24,39613,39614],{},"It makes larger programs easier to organize",[24,39616,39617],{},"It is common in object-oriented programming",[14,39619,39620],{},"For example, if you had several animal classes, they could all inherit from one parent class instead of repeating the same methods in every class.",[14,39622,39623,39624,186],{},"If you want to build your own class first, see ",[372,39625,39626],{"href":9013},"how to create a class in Python",[37,39628,39630],{"id":39629},"what-to-watch-out-for","What to watch out for",[14,39632,39633],{},"Here are some common beginner mistakes with inheritance:",[309,39635,39636,39639,39642,39645],{},[24,39637,39638],{},"Forgetting parentheses when inheriting from a class",[24,39640,39641],{},"Misspelling method names",[24,39643,39644],{},"Trying to call a child-only method on a parent object",[24,39646,39647],{},"Using inheritance when a simple class would be enough",[639,39649,39651],{"id":39650},"example-of-creating-an-object-from-the-wrong-class","Example of creating an object from the wrong class",[14,39653,39654],{},"This code causes a problem:",[42,39656,39658],{"className":44,"code":39657,"language":46,"meta":47,"style":47},"class Animal:\n    def speak(self):\n        print(\"Animal sound\")\n\nclass Dog(Animal):\n    def bark(self):\n        print(\"Woof!\")\n\nanimal = Animal()\nanimal.bark()\n",[49,39659,39660,39668,39680,39694,39698,39710,39722,39736,39740,39751],{"__ignoreMap":47},[52,39661,39662,39664,39666],{"class":54,"line":55},[52,39663,6994],{"class":1475},[52,39665,39039],{"class":399},[52,39667,1330],{"class":102},[52,39669,39670,39672,39674,39676,39678],{"class":54,"line":66},[52,39671,7004],{"class":1475},[52,39673,39048],{"class":7065},[52,39675,193],{"class":102},[52,39677,7013],{"class":7012},[52,39679,3114],{"class":102},[52,39681,39682,39684,39686,39688,39690,39692],{"class":54,"line":73},[52,39683,1470],{"class":241},[52,39685,193],{"class":102},[52,39687,115],{"class":83},[52,39689,39065],{"class":87},[52,39691,115],{"class":83},[52,39693,211],{"class":102},[52,39695,39696],{"class":54,"line":94},[52,39697,70],{"emptyLinePlaceholder":69},[52,39699,39700,39702,39704,39706,39708],{"class":54,"line":106},[52,39701,6994],{"class":1475},[52,39703,6997],{"class":399},[52,39705,193],{"class":102},[52,39707,39084],{"class":399},[52,39709,3114],{"class":102},[52,39711,39712,39714,39716,39718,39720],{"class":54,"line":128},[52,39713,7004],{"class":1475},[52,39715,7066],{"class":7065},[52,39717,193],{"class":102},[52,39719,7013],{"class":7012},[52,39721,3114],{"class":102},[52,39723,39724,39726,39728,39730,39732,39734],{"class":54,"line":148},[52,39725,1470],{"class":241},[52,39727,193],{"class":102},[52,39729,115],{"class":83},[52,39731,8157],{"class":87},[52,39733,115],{"class":83},[52,39735,211],{"class":102},[52,39737,39738],{"class":54,"line":164},[52,39739,70],{"emptyLinePlaceholder":69},[52,39741,39742,39745,39747,39749],{"class":54,"line":170},[52,39743,39744],{"class":62},"animal ",[52,39746,80],{"class":79},[52,39748,39039],{"class":189},[52,39750,230],{"class":102},[52,39752,39753,39756,39758,39760],{"class":54,"line":175},[52,39754,39755],{"class":62},"animal",[52,39757,186],{"class":102},[52,39759,7163],{"class":189},[52,39761,230],{"class":102},[14,39763,39764],{},"Why it fails:",[309,39766,39767,39774,39784],{},[24,39768,39769,39771,39772],{},[49,39770,39084],{}," has ",[49,39773,39190],{},[24,39775,39776,39778,39779,39781,39782],{},[49,39777,39084],{}," does ",[5503,39780,20858],{}," have ",[49,39783,7382],{},[24,39785,39786,39787,39789,39790],{},"Only ",[49,39788,7369],{}," objects have ",[49,39791,7382],{},[14,39793,39794],{},"A correct version is:",[42,39796,39798],{"className":44,"code":39797,"language":46,"meta":47,"style":47},"dog = Dog()\ndog.bark()\n",[49,39799,39800,39810],{"__ignoreMap":47},[52,39801,39802,39804,39806,39808],{"class":54,"line":55},[52,39803,39121],{"class":62},[52,39805,80],{"class":79},[52,39807,6997],{"class":189},[52,39809,230],{"class":102},[52,39811,39812,39814,39816,39818],{"class":54,"line":66},[52,39813,39132],{"class":62},[52,39815,186],{"class":102},[52,39817,7163],{"class":189},[52,39819,230],{"class":102},[37,39821,12322],{"id":12321},[14,39823,39824],{},"Some common causes of confusion are:",[309,39826,39827,39830,39833,39836],{},[24,39828,39829],{},"Confusing parent class methods with child class methods",[24,39831,39832],{},"Creating an object from the wrong class",[24,39834,39835],{},"Using inheritance syntax incorrectly",[24,39837,39838],{},"Assuming inheritance copies code instead of sharing behavior",[14,39840,39841],{},"A useful thing to remember is this:",[14,39843,39844,39845,39847],{},"Inheritance does ",[5503,39846,20858],{}," copy and paste methods into the child class. Instead, the child class can access behavior from the parent class.",[37,39849,39851],{"id":39850},"helpful-debugging-checks","Helpful debugging checks",[14,39853,39854],{},"If your code is not working, these quick checks can help:",[42,39856,39858],{"className":44,"code":39857,"language":46,"meta":47,"style":47},"print(type(dog))\nprint(isinstance(dog, Dog))\nprint(isinstance(dog, Animal))\nprint(dir(dog))\n",[49,39859,39860,39874,39893,39911],{"__ignoreMap":47},[52,39861,39862,39864,39866,39868,39870,39872],{"class":54,"line":55},[52,39863,242],{"class":241},[52,39865,193],{"class":102},[52,39867,4198],{"class":4733},[52,39869,193],{"class":102},[52,39871,39132],{"class":189},[52,39873,8886],{"class":102},[52,39875,39876,39878,39880,39883,39885,39887,39889,39891],{"class":54,"line":66},[52,39877,242],{"class":241},[52,39879,193],{"class":102},[52,39881,39882],{"class":241},"isinstance",[52,39884,193],{"class":102},[52,39886,39132],{"class":189},[52,39888,199],{"class":102},[52,39890,6997],{"class":189},[52,39892,8886],{"class":102},[52,39894,39895,39897,39899,39901,39903,39905,39907,39909],{"class":54,"line":73},[52,39896,242],{"class":241},[52,39898,193],{"class":102},[52,39900,39882],{"class":241},[52,39902,193],{"class":102},[52,39904,39132],{"class":189},[52,39906,199],{"class":102},[52,39908,39039],{"class":189},[52,39910,8886],{"class":102},[52,39912,39913,39915,39917,39919,39921,39923],{"class":54,"line":94},[52,39914,242],{"class":241},[52,39916,193],{"class":102},[52,39918,8919],{"class":241},[52,39920,193],{"class":102},[52,39922,39132],{"class":189},[52,39924,8886],{"class":102},[14,39926,39927],{},"What they do:",[309,39929,39930,39936,39947,39957],{},[24,39931,39932,39935],{},[49,39933,39934],{},"type(dog)"," shows the object's class",[24,39937,39938,39941,39942,39944,39945],{},[49,39939,39940],{},"isinstance(dog, Dog)"," checks if ",[49,39943,39132],{}," is a ",[49,39946,7369],{},[24,39948,39949,39941,39952,39954,39955],{},[49,39950,39951],{},"isinstance(dog, Animal)",[49,39953,39132],{}," is also considered an ",[49,39956,39084],{},[24,39958,39959,39962],{},[49,39960,39961],{},"dir(dog)"," shows available attributes and methods",[14,39964,4259],{},[42,39966,39968],{"className":44,"code":39967,"language":46,"meta":47,"style":47},"class Animal:\n    def speak(self):\n        print(\"Animal sound\")\n\nclass Dog(Animal):\n    def bark(self):\n        print(\"Woof!\")\n\ndog = Dog()\n\nprint(type(dog))\nprint(isinstance(dog, Dog))\nprint(isinstance(dog, Animal))\nprint(\"speak\" in dir(dog))\nprint(\"bark\" in dir(dog))\n",[49,39969,39970,39978,39990,40004,40008,40020,40032,40046,40050,40060,40064,40078,40096,40114,40137],{"__ignoreMap":47},[52,39971,39972,39974,39976],{"class":54,"line":55},[52,39973,6994],{"class":1475},[52,39975,39039],{"class":399},[52,39977,1330],{"class":102},[52,39979,39980,39982,39984,39986,39988],{"class":54,"line":66},[52,39981,7004],{"class":1475},[52,39983,39048],{"class":7065},[52,39985,193],{"class":102},[52,39987,7013],{"class":7012},[52,39989,3114],{"class":102},[52,39991,39992,39994,39996,39998,40000,40002],{"class":54,"line":73},[52,39993,1470],{"class":241},[52,39995,193],{"class":102},[52,39997,115],{"class":83},[52,39999,39065],{"class":87},[52,40001,115],{"class":83},[52,40003,211],{"class":102},[52,40005,40006],{"class":54,"line":94},[52,40007,70],{"emptyLinePlaceholder":69},[52,40009,40010,40012,40014,40016,40018],{"class":54,"line":106},[52,40011,6994],{"class":1475},[52,40013,6997],{"class":399},[52,40015,193],{"class":102},[52,40017,39084],{"class":399},[52,40019,3114],{"class":102},[52,40021,40022,40024,40026,40028,40030],{"class":54,"line":128},[52,40023,7004],{"class":1475},[52,40025,7066],{"class":7065},[52,40027,193],{"class":102},[52,40029,7013],{"class":7012},[52,40031,3114],{"class":102},[52,40033,40034,40036,40038,40040,40042,40044],{"class":54,"line":148},[52,40035,1470],{"class":241},[52,40037,193],{"class":102},[52,40039,115],{"class":83},[52,40041,8157],{"class":87},[52,40043,115],{"class":83},[52,40045,211],{"class":102},[52,40047,40048],{"class":54,"line":164},[52,40049,70],{"emptyLinePlaceholder":69},[52,40051,40052,40054,40056,40058],{"class":54,"line":170},[52,40053,39121],{"class":62},[52,40055,80],{"class":79},[52,40057,6997],{"class":189},[52,40059,230],{"class":102},[52,40061,40062],{"class":54,"line":175},[52,40063,70],{"emptyLinePlaceholder":69},[52,40065,40066,40068,40070,40072,40074,40076],{"class":54,"line":214},[52,40067,242],{"class":241},[52,40069,193],{"class":102},[52,40071,4198],{"class":4733},[52,40073,193],{"class":102},[52,40075,39132],{"class":189},[52,40077,8886],{"class":102},[52,40079,40080,40082,40084,40086,40088,40090,40092,40094],{"class":54,"line":233},[52,40081,242],{"class":241},[52,40083,193],{"class":102},[52,40085,39882],{"class":241},[52,40087,193],{"class":102},[52,40089,39132],{"class":189},[52,40091,199],{"class":102},[52,40093,6997],{"class":189},[52,40095,8886],{"class":102},[52,40097,40098,40100,40102,40104,40106,40108,40110,40112],{"class":54,"line":238},[52,40099,242],{"class":241},[52,40101,193],{"class":102},[52,40103,39882],{"class":241},[52,40105,193],{"class":102},[52,40107,39132],{"class":189},[52,40109,199],{"class":102},[52,40111,39039],{"class":189},[52,40113,8886],{"class":102},[52,40115,40116,40118,40120,40122,40124,40126,40128,40131,40133,40135],{"class":54,"line":272},[52,40117,242],{"class":241},[52,40119,193],{"class":102},[52,40121,115],{"class":83},[52,40123,39137],{"class":87},[52,40125,115],{"class":83},[52,40127,3101],{"class":58},[52,40129,40130],{"class":241}," dir",[52,40132,193],{"class":102},[52,40134,39132],{"class":189},[52,40136,8886],{"class":102},[52,40138,40139,40141,40143,40145,40147,40149,40151,40153,40155,40157],{"class":54,"line":1348},[52,40140,242],{"class":241},[52,40142,193],{"class":102},[52,40144,115],{"class":83},[52,40146,7163],{"class":87},[52,40148,115],{"class":83},[52,40150,3101],{"class":58},[52,40152,40130],{"class":241},[52,40154,193],{"class":102},[52,40156,39132],{"class":189},[52,40158,8886],{"class":102},[14,40160,40161],{},[5503,40162,40163],{},"Possible output:",[42,40165,40167],{"className":44,"code":40166,"language":46,"meta":47,"style":47},"\u003Cclass '__main__.Dog'>\nTrue\nTrue\nTrue\nTrue\n",[49,40168,40169,40184,40189,40193,40197],{"__ignoreMap":47},[52,40170,40171,40173,40175,40177,40180,40182],{"class":54,"line":55},[52,40172,15294],{"class":79},[52,40174,6994],{"class":1475},[52,40176,4172],{"class":83},[52,40178,40179],{"class":87},"__main__.Dog",[52,40181,4162],{"class":83},[52,40183,15306],{"class":79},[52,40185,40186],{"class":54,"line":66},[52,40187,40188],{"class":160},"True\n",[52,40190,40191],{"class":54,"line":73},[52,40192,40188],{"class":160},[52,40194,40195],{"class":54,"line":94},[52,40196,40188],{"class":160},[52,40198,40199],{"class":54,"line":106},[52,40200,40188],{"class":160},[14,40202,40203],{},"This shows that:",[309,40205,40206,40212,40219],{},[24,40207,40208,39944,40210],{},[49,40209,39132],{},[49,40211,7369],{},[24,40213,40214,40216,40217],{},[49,40215,39132],{}," is also treated as an ",[49,40218,39084],{},[24,40220,40221,40223,40224,759,40226],{},[49,40222,39132],{}," has both ",[49,40225,39137],{},[49,40227,7163],{},[37,40229,1942],{"id":1941},[639,40231,40233],{"id":40232},"what-is-inheritance-in-python","What is inheritance in Python?",[14,40235,40236],{},"Inheritance lets one class use code from another class. A child class gets methods and attributes from a parent class.",[639,40238,40240],{"id":40239},"why-would-i-use-inheritance","Why would I use inheritance?",[14,40242,40243],{},"Use it when two classes are related and should share behavior. It helps reduce repeated code.",[639,40245,40247],{"id":40246},"can-a-child-class-add-its-own-methods","Can a child class add its own methods?",[14,40249,40250],{},"Yes. A child class can use inherited methods and also define new ones.",[639,40252,40254],{"id":40253},"can-a-child-class-change-a-parent-method","Can a child class change a parent method?",[14,40256,40257],{},"Yes. This is called overriding. The child class defines a method with the same name.",[14,40259,40260],{},"Here is a simple overriding example:",[42,40262,40264],{"className":44,"code":40263,"language":46,"meta":47,"style":47},"class Animal:\n    def speak(self):\n        print(\"Animal sound\")\n\nclass Dog(Animal):\n    def speak(self):\n        print(\"Woof!\")\n",[49,40265,40266,40274,40286,40300,40304,40316,40328],{"__ignoreMap":47},[52,40267,40268,40270,40272],{"class":54,"line":55},[52,40269,6994],{"class":1475},[52,40271,39039],{"class":399},[52,40273,1330],{"class":102},[52,40275,40276,40278,40280,40282,40284],{"class":54,"line":66},[52,40277,7004],{"class":1475},[52,40279,39048],{"class":7065},[52,40281,193],{"class":102},[52,40283,7013],{"class":7012},[52,40285,3114],{"class":102},[52,40287,40288,40290,40292,40294,40296,40298],{"class":54,"line":73},[52,40289,1470],{"class":241},[52,40291,193],{"class":102},[52,40293,115],{"class":83},[52,40295,39065],{"class":87},[52,40297,115],{"class":83},[52,40299,211],{"class":102},[52,40301,40302],{"class":54,"line":94},[52,40303,70],{"emptyLinePlaceholder":69},[52,40305,40306,40308,40310,40312,40314],{"class":54,"line":106},[52,40307,6994],{"class":1475},[52,40309,6997],{"class":399},[52,40311,193],{"class":102},[52,40313,39084],{"class":399},[52,40315,3114],{"class":102},[52,40317,40318,40320,40322,40324,40326],{"class":54,"line":128},[52,40319,7004],{"class":1475},[52,40321,39048],{"class":7065},[52,40323,193],{"class":102},[52,40325,7013],{"class":7012},[52,40327,3114],{"class":102},[52,40329,40330,40332,40334,40336,40338,40340],{"class":54,"line":148},[52,40331,1470],{"class":241},[52,40333,193],{"class":102},[52,40335,115],{"class":83},[52,40337,8157],{"class":87},[52,40339,115],{"class":83},[52,40341,211],{"class":102},[14,40343,40344,40345,40347,40348,40350],{},"Now a ",[49,40346,7369],{}," object will use its own ",[49,40349,39190],{}," method instead of the parent version.",[37,40352,2005],{"id":2004},[309,40354,40355,40359,40363,40369],{},[24,40356,40357],{},[372,40358,9002],{"href":9001},[24,40360,40361],{},[372,40362,7255],{"href":7254},[24,40364,40365],{},[372,40366,40368],{"href":40367},"\u002Fhow-to\u002Fhow-to-inherit-from-a-class-in-python\u002F","How to inherit from a class in Python",[24,40370,40371],{},[372,40372,40374],{"href":40373},"\u002Fglossary\u002Fwhat-is-an-object-in-python\u002F","What is an object in Python?",[2034,40376,40377],{},"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 .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":47,"searchDepth":66,"depth":66,"links":40379},[40380,40381,40382,40383,40390,40391,40394,40395,40396,40402],{"id":39,"depth":66,"text":40},{"id":7215,"depth":66,"text":7216},{"id":39229,"depth":66,"text":39230},{"id":39379,"depth":66,"text":39380,"children":40384},[40385,40386,40387,40388,40389],{"id":39383,"depth":73,"text":39384},{"id":39436,"depth":73,"text":39437},{"id":39501,"depth":73,"text":39502},{"id":39534,"depth":73,"text":39535},{"id":39568,"depth":73,"text":39569},{"id":39596,"depth":66,"text":39597},{"id":39629,"depth":66,"text":39630,"children":40392},[40393],{"id":39650,"depth":73,"text":39651},{"id":12321,"depth":66,"text":12322},{"id":39850,"depth":66,"text":39851},{"id":1941,"depth":66,"text":1942,"children":40397},[40398,40399,40400,40401],{"id":40232,"depth":73,"text":40233},{"id":40239,"depth":73,"text":40240},{"id":40246,"depth":73,"text":40247},{"id":40253,"depth":73,"text":40254},{"id":2004,"depth":66,"text":2005},"Master python inheritance example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-inheritance-example",{"title":39014,"description":40403},"examples\u002Fpython-inheritance-example","NWWsJipwyk62hEDrM-4ibIi-ev_X7SiH71uys529BsU",{"id":40410,"title":40411,"body":40412,"description":42314,"extension":2075,"meta":42315,"navigation":69,"path":42316,"seo":42317,"stem":42318,"__hash__":42319},"content\u002Fexamples\u002Fpython-json-parser-example.md","Python JSON Parser Example",{"type":7,"value":40413,"toc":42280},[40414,40417,40420,40422,40440,40443,40552,40563,40565,40568,40586,40589,40603,40607,40610,40627,40688,40690,40711,40717,40722,40835,40837,40839,40862,40865,40874,40876,40997,41006,41012,41016,41019,41174,41176,41195,41199,41222,41232,41238,41244,41369,41371,41389,41392,41396,41399,41445,41447,41656,41658,41738,41742,41747,41827,41837,41856,41859,41868,41872,41875,41879,41882,41900,41903,41920,41927,41935,41938,41950,41953,41965,41969,41972,42027,42032,42059,42063,42066,42132,42134,42157,42163,42167,42170,42184,42189,42191,42198,42206,42210,42218,42222,42225,42229,42235,42237,42277],[10,40415,40411],{"id":40416},"python-json-parser-example",[14,40418,40419],{},"This beginner-friendly example shows how to parse JSON in Python and turn it into normal Python data.",[14,40421,25293],{},[309,40423,40424,40429,40434,40437],{},[24,40425,40426,40427,14928],{},"how to import the ",[49,40428,227],{},[24,40430,40431,40432],{},"how to parse a JSON string with ",[49,40433,2932],{},[24,40435,40436],{},"how JSON becomes Python data types",[24,40438,40439],{},"how to read values from the parsed result safely",[14,40441,40442],{},"If you want the shortest working example first, start here:",[42,40444,40446],{"className":44,"code":40445,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"name\": \"Alice\", \"age\": 25, \"skills\": [\"Python\", \"SQL\"]}'\n\ndata = json.loads(json_text)\n\nprint(data)\nprint(data[\"name\"])\nprint(data[\"skills\"][0])\n",[49,40447,40448,40455,40459,40473,40477,40497,40501,40511,40529],{"__ignoreMap":47},[52,40449,40450,40452],{"class":54,"line":55},[52,40451,59],{"class":58},[52,40453,40454],{"class":62}," json\n",[52,40456,40457],{"class":54,"line":66},[52,40458,70],{"emptyLinePlaceholder":69},[52,40460,40461,40464,40466,40468,40471],{"class":54,"line":73},[52,40462,40463],{"class":62},"json_text ",[52,40465,80],{"class":79},[52,40467,4172],{"class":83},[52,40469,40470],{"class":87},"{\"name\": \"Alice\", \"age\": 25, \"skills\": [\"Python\", \"SQL\"]}",[52,40472,24871],{"class":83},[52,40474,40475],{"class":54,"line":94},[52,40476,70],{"emptyLinePlaceholder":69},[52,40478,40479,40481,40483,40485,40487,40490,40492,40495],{"class":54,"line":106},[52,40480,217],{"class":62},[52,40482,80],{"class":79},[52,40484,2204],{"class":62},[52,40486,186],{"class":102},[52,40488,40489],{"class":189},"loads",[52,40491,193],{"class":102},[52,40493,40494],{"class":189},"json_text",[52,40496,211],{"class":102},[52,40498,40499],{"class":54,"line":128},[52,40500,70],{"emptyLinePlaceholder":69},[52,40502,40503,40505,40507,40509],{"class":54,"line":148},[52,40504,242],{"class":241},[52,40506,193],{"class":102},[52,40508,247],{"class":189},[52,40510,211],{"class":102},[52,40512,40513,40515,40517,40519,40521,40523,40525,40527],{"class":54,"line":164},[52,40514,242],{"class":241},[52,40516,193],{"class":102},[52,40518,247],{"class":189},[52,40520,250],{"class":102},[52,40522,115],{"class":83},[52,40524,2147],{"class":87},[52,40526,115],{"class":83},[52,40528,269],{"class":102},[52,40530,40531,40533,40535,40537,40539,40541,40544,40546,40548,40550],{"class":54,"line":170},[52,40532,242],{"class":241},[52,40534,193],{"class":102},[52,40536,247],{"class":189},[52,40538,250],{"class":102},[52,40540,115],{"class":83},[52,40542,40543],{"class":87},"skills",[52,40545,115],{"class":83},[52,40547,259],{"class":102},[52,40549,13294],{"class":121},[52,40551,269],{"class":102},[14,40553,40554,16853,40556,40558,40559,40562],{},[5503,40555,16852],{},[49,40557,2932],{}," for a JSON string. Use ",[49,40560,40561],{},"json.load()"," when reading JSON from a file.",[37,40564,2268],{"id":2267},[14,40566,40567],{},"In this page, the script will show you how to:",[309,40569,40570,40575,40580,40583],{},[24,40571,40572,40573,14928],{},"import Python’s built-in ",[49,40574,227],{},[24,40576,40577,40578],{},"parse JSON text with ",[49,40579,2932],{},[24,40581,40582],{},"understand what Python data type you get back",[24,40584,40585],{},"access values from dictionaries, lists, and nested data",[14,40587,40588],{},"This is a common pattern when working with:",[309,40590,40591,40594,40597,40600],{},[24,40592,40593],{},"API responses",[24,40595,40596],{},"configuration files",[24,40598,40599],{},"saved app data",[24,40601,40602],{},"practice projects that use structured data",[37,40604,40606],{"id":40605},"example-input-json","Example input JSON",[14,40608,40609],{},"Here is a small JSON string. It includes:",[309,40611,40612,40615,40618,40621,40624],{},[24,40613,40614],{},"a string",[24,40616,40617],{},"a number",[24,40619,40620],{},"a boolean",[24,40622,40623],{},"a list",[24,40625,40626],{},"a nested object",[42,40628,40630],{"className":44,"code":40629,"language":46,"meta":47,"style":47},"json_text = \"\"\"\n{\n    \"name\": \"Alice\",\n    \"age\": 25,\n    \"active\": true,\n    \"skills\": [\"Python\", \"SQL\"],\n    \"address\": {\n        \"city\": \"London\"\n    }\n}\n\"\"\"\n",[49,40631,40632,40641,40645,40650,40655,40660,40665,40670,40675,40679,40683],{"__ignoreMap":47},[52,40633,40634,40636,40638],{"class":54,"line":55},[52,40635,40463],{"class":62},[52,40637,80],{"class":79},[52,40639,40640],{"class":83}," \"\"\"\n",[52,40642,40643],{"class":54,"line":66},[52,40644,980],{"class":87},[52,40646,40647],{"class":54,"line":73},[52,40648,40649],{"class":87},"    \"name\": \"Alice\",\n",[52,40651,40652],{"class":54,"line":94},[52,40653,40654],{"class":87},"    \"age\": 25,\n",[52,40656,40657],{"class":54,"line":106},[52,40658,40659],{"class":87},"    \"active\": true,\n",[52,40661,40662],{"class":54,"line":128},[52,40663,40664],{"class":87},"    \"skills\": [\"Python\", \"SQL\"],\n",[52,40666,40667],{"class":54,"line":148},[52,40668,40669],{"class":87},"    \"address\": {\n",[52,40671,40672],{"class":54,"line":164},[52,40673,40674],{"class":87},"        \"city\": \"London\"\n",[52,40676,40677],{"class":54,"line":170},[52,40678,1090],{"class":87},[52,40680,40681],{"class":54,"line":175},[52,40682,167],{"class":87},[52,40684,40685],{"class":54,"line":214},[52,40686,40687],{"class":83},"\"\"\"\n",[14,40689,15124],{},[309,40691,40692,40699,40705],{},[24,40693,40694,40695,40698],{},"JSON uses ",[5503,40696,40697],{},"double quotes"," around keys and string values.",[24,40700,40701,40704],{},[49,40702,40703],{},"true"," in JSON is lowercase.",[24,40706,40707,40708,40710],{},"This is still just ",[5503,40709,955],{}," until Python parses it.",[37,40712,40714,40715],{"id":40713},"parse-json-with-jsonloads","Parse JSON with ",[49,40716,2932],{},[14,40718,3650,40719,40721],{},[49,40720,2932],{}," when your JSON is stored in a string.",[42,40723,40725],{"className":44,"code":40724,"language":46,"meta":47,"style":47},"import json\n\njson_text = \"\"\"\n{\n    \"name\": \"Alice\",\n    \"age\": 25,\n    \"active\": true,\n    \"skills\": [\"Python\", \"SQL\"],\n    \"address\": {\n        \"city\": \"London\"\n    }\n}\n\"\"\"\n\ndata = json.loads(json_text)\n\nprint(data)\nprint(type(data))\n",[49,40726,40727,40733,40737,40745,40749,40753,40757,40761,40765,40769,40773,40777,40781,40785,40789,40807,40811,40821],{"__ignoreMap":47},[52,40728,40729,40731],{"class":54,"line":55},[52,40730,59],{"class":58},[52,40732,40454],{"class":62},[52,40734,40735],{"class":54,"line":66},[52,40736,70],{"emptyLinePlaceholder":69},[52,40738,40739,40741,40743],{"class":54,"line":73},[52,40740,40463],{"class":62},[52,40742,80],{"class":79},[52,40744,40640],{"class":83},[52,40746,40747],{"class":54,"line":94},[52,40748,980],{"class":87},[52,40750,40751],{"class":54,"line":106},[52,40752,40649],{"class":87},[52,40754,40755],{"class":54,"line":128},[52,40756,40654],{"class":87},[52,40758,40759],{"class":54,"line":148},[52,40760,40659],{"class":87},[52,40762,40763],{"class":54,"line":164},[52,40764,40664],{"class":87},[52,40766,40767],{"class":54,"line":170},[52,40768,40669],{"class":87},[52,40770,40771],{"class":54,"line":175},[52,40772,40674],{"class":87},[52,40774,40775],{"class":54,"line":214},[52,40776,1090],{"class":87},[52,40778,40779],{"class":54,"line":233},[52,40780,167],{"class":87},[52,40782,40783],{"class":54,"line":238},[52,40784,40687],{"class":83},[52,40786,40787],{"class":54,"line":272},[52,40788,70],{"emptyLinePlaceholder":69},[52,40790,40791,40793,40795,40797,40799,40801,40803,40805],{"class":54,"line":1348},[52,40792,217],{"class":62},[52,40794,80],{"class":79},[52,40796,2204],{"class":62},[52,40798,186],{"class":102},[52,40800,40489],{"class":189},[52,40802,193],{"class":102},[52,40804,40494],{"class":189},[52,40806,211],{"class":102},[52,40808,40809],{"class":54,"line":1376},[52,40810,70],{"emptyLinePlaceholder":69},[52,40812,40813,40815,40817,40819],{"class":54,"line":1381},[52,40814,242],{"class":241},[52,40816,193],{"class":102},[52,40818,247],{"class":189},[52,40820,211],{"class":102},[52,40822,40823,40825,40827,40829,40831,40833],{"class":54,"line":1406},[52,40824,242],{"class":241},[52,40826,193],{"class":102},[52,40828,4198],{"class":4733},[52,40830,193],{"class":102},[52,40832,247],{"class":189},[52,40834,8886],{"class":102},[639,40836,36906],{"id":36905},[14,40838,32483],{},[42,40840,40842],{"className":44,"code":40841,"language":46,"meta":47,"style":47},"data = json.loads(json_text)\n",[49,40843,40844],{"__ignoreMap":47},[52,40845,40846,40848,40850,40852,40854,40856,40858,40860],{"class":54,"line":55},[52,40847,217],{"class":62},[52,40849,80],{"class":79},[52,40851,2204],{"class":62},[52,40853,186],{"class":102},[52,40855,40489],{"class":189},[52,40857,193],{"class":102},[52,40859,40494],{"class":189},[52,40861,211],{"class":102},[14,40863,40864],{},"This converts the JSON text into Python data.",[14,40866,40867,40868,40870,40871,186],{},"In this example, the top-level JSON value is an ",[5503,40869,7231],{},", so the result becomes a Python ",[5503,40872,40873],{},"dictionary",[14,40875,9266],{},[42,40877,40879],{"className":44,"code":40878,"language":46,"meta":47,"style":47},"{'name': 'Alice', 'age': 25, 'active': True, 'skills': ['Python', 'SQL'], 'address': {'city': 'London'}}\n\u003Cclass 'dict'>\n",[49,40880,40881,40982],{"__ignoreMap":47},[52,40882,40883,40885,40887,40889,40891,40893,40895,40897,40899,40901,40903,40905,40907,40909,40911,40913,40915,40918,40920,40922,40924,40926,40928,40930,40932,40934,40936,40938,40941,40943,40945,40947,40950,40952,40954,40956,40959,40961,40963,40965,40967,40969,40971,40973,40975,40977,40979],{"class":54,"line":55},[52,40884,1482],{"class":102},[52,40886,4162],{"class":83},[52,40888,2147],{"class":87},[52,40890,4162],{"class":83},[52,40892,118],{"class":102},[52,40894,4172],{"class":83},[52,40896,2156],{"class":87},[52,40898,4162],{"class":83},[52,40900,199],{"class":102},[52,40902,4172],{"class":83},[52,40904,2167],{"class":87},[52,40906,4162],{"class":83},[52,40908,118],{"class":102},[52,40910,2674],{"class":121},[52,40912,199],{"class":102},[52,40914,4172],{"class":83},[52,40916,40917],{"class":87},"active",[52,40919,4162],{"class":83},[52,40921,118],{"class":102},[52,40923,10943],{"class":160},[52,40925,199],{"class":102},[52,40927,4172],{"class":83},[52,40929,40543],{"class":87},[52,40931,4162],{"class":83},[52,40933,118],{"class":102},[52,40935,18162],{"class":102},[52,40937,4162],{"class":83},[52,40939,40940],{"class":87},"Python",[52,40942,4162],{"class":83},[52,40944,199],{"class":102},[52,40946,4172],{"class":83},[52,40948,40949],{"class":87},"SQL",[52,40951,4162],{"class":83},[52,40953,15661],{"class":102},[52,40955,4172],{"class":83},[52,40957,40958],{"class":87},"address",[52,40960,4162],{"class":83},[52,40962,118],{"class":102},[52,40964,3000],{"class":102},[52,40966,4162],{"class":83},[52,40968,15026],{"class":87},[52,40970,4162],{"class":83},[52,40972,118],{"class":102},[52,40974,4172],{"class":83},[52,40976,15056],{"class":87},[52,40978,4162],{"class":83},[52,40980,40981],{"class":102},"}}\n",[52,40983,40984,40986,40988,40990,40993,40995],{"class":54,"line":66},[52,40985,15294],{"class":79},[52,40987,6994],{"class":1475},[52,40989,4172],{"class":83},[52,40991,40992],{"class":87},"dict",[52,40994,4162],{"class":83},[52,40996,15306],{"class":79},[14,40998,40999,41000,41002,41003,41005],{},"If the JSON started with ",[49,41001,250],{}," and ended with ",[49,41004,11122],{},", the result would usually be a Python list instead.",[14,41007,41008,41009,186],{},"For a deeper explanation of this function, see ",[372,41010,41011],{"href":2929},"json.loads() explained",[37,41013,41015],{"id":41014},"read-values-from-parsed-json","Read values from parsed JSON",[14,41017,41018],{},"Once the JSON is parsed, you can use normal Python indexing to read values.",[42,41020,41022],{"className":44,"code":41021,"language":46,"meta":47,"style":47},"import json\n\njson_text = \"\"\"\n{\n    \"name\": \"Alice\",\n    \"age\": 25,\n    \"active\": true,\n    \"skills\": [\"Python\", \"SQL\"],\n    \"address\": {\n        \"city\": \"London\"\n    }\n}\n\"\"\"\n\ndata = json.loads(json_text)\n\nprint(data[\"name\"])\nprint(data[\"skills\"][0])\nprint(data[\"address\"][\"city\"])\n",[49,41023,41024,41030,41034,41042,41046,41050,41054,41058,41062,41066,41070,41074,41078,41082,41086,41104,41108,41126,41148],{"__ignoreMap":47},[52,41025,41026,41028],{"class":54,"line":55},[52,41027,59],{"class":58},[52,41029,40454],{"class":62},[52,41031,41032],{"class":54,"line":66},[52,41033,70],{"emptyLinePlaceholder":69},[52,41035,41036,41038,41040],{"class":54,"line":73},[52,41037,40463],{"class":62},[52,41039,80],{"class":79},[52,41041,40640],{"class":83},[52,41043,41044],{"class":54,"line":94},[52,41045,980],{"class":87},[52,41047,41048],{"class":54,"line":106},[52,41049,40649],{"class":87},[52,41051,41052],{"class":54,"line":128},[52,41053,40654],{"class":87},[52,41055,41056],{"class":54,"line":148},[52,41057,40659],{"class":87},[52,41059,41060],{"class":54,"line":164},[52,41061,40664],{"class":87},[52,41063,41064],{"class":54,"line":170},[52,41065,40669],{"class":87},[52,41067,41068],{"class":54,"line":175},[52,41069,40674],{"class":87},[52,41071,41072],{"class":54,"line":214},[52,41073,1090],{"class":87},[52,41075,41076],{"class":54,"line":233},[52,41077,167],{"class":87},[52,41079,41080],{"class":54,"line":238},[52,41081,40687],{"class":83},[52,41083,41084],{"class":54,"line":272},[52,41085,70],{"emptyLinePlaceholder":69},[52,41087,41088,41090,41092,41094,41096,41098,41100,41102],{"class":54,"line":1348},[52,41089,217],{"class":62},[52,41091,80],{"class":79},[52,41093,2204],{"class":62},[52,41095,186],{"class":102},[52,41097,40489],{"class":189},[52,41099,193],{"class":102},[52,41101,40494],{"class":189},[52,41103,211],{"class":102},[52,41105,41106],{"class":54,"line":1376},[52,41107,70],{"emptyLinePlaceholder":69},[52,41109,41110,41112,41114,41116,41118,41120,41122,41124],{"class":54,"line":1381},[52,41111,242],{"class":241},[52,41113,193],{"class":102},[52,41115,247],{"class":189},[52,41117,250],{"class":102},[52,41119,115],{"class":83},[52,41121,2147],{"class":87},[52,41123,115],{"class":83},[52,41125,269],{"class":102},[52,41127,41128,41130,41132,41134,41136,41138,41140,41142,41144,41146],{"class":54,"line":1406},[52,41129,242],{"class":241},[52,41131,193],{"class":102},[52,41133,247],{"class":189},[52,41135,250],{"class":102},[52,41137,115],{"class":83},[52,41139,40543],{"class":87},[52,41141,115],{"class":83},[52,41143,259],{"class":102},[52,41145,13294],{"class":121},[52,41147,269],{"class":102},[52,41149,41150,41152,41154,41156,41158,41160,41162,41164,41166,41168,41170,41172],{"class":54,"line":1430},[52,41151,242],{"class":241},[52,41153,193],{"class":102},[52,41155,247],{"class":189},[52,41157,250],{"class":102},[52,41159,115],{"class":83},[52,41161,40958],{"class":87},[52,41163,115],{"class":83},[52,41165,259],{"class":102},[52,41167,115],{"class":83},[52,41169,15026],{"class":87},[52,41171,115],{"class":83},[52,41173,269],{"class":102},[14,41175,9266],{},[42,41177,41179],{"className":44,"code":41178,"language":46,"meta":47,"style":47},"Alice\nPython\nLondon\n",[49,41180,41181,41185,41190],{"__ignoreMap":47},[52,41182,41183],{"class":54,"line":55},[52,41184,19253],{"class":62},[52,41186,41187],{"class":54,"line":66},[52,41188,41189],{"class":62},"Python\n",[52,41191,41192],{"class":54,"line":73},[52,41193,41194],{"class":62},"London\n",[639,41196,41198],{"id":41197},"how-each-line-works","How each line works",[309,41200,41201,41207,41216],{},[24,41202,41203,41206],{},[49,41204,41205],{},"data[\"name\"]"," gets a top-level value from the dictionary",[24,41208,41209,41212,41213,41215],{},[49,41210,41211],{},"data[\"skills\"][0]"," gets the first item from the ",[49,41214,40543],{}," list",[24,41217,41218,41221],{},[49,41219,41220],{},"data[\"address\"][\"city\"]"," gets a nested value step by step",[14,41223,41224,41225,41227,41228,186],{},"If you try to access a key that does not exist, Python raises a ",[49,41226,1701],{},". See ",[372,41229,41231],{"href":41230},"\u002Ferrors\u002Fkeyerror-when-accessing-dictionary-values-fix\u002F","how to fix KeyError when accessing dictionary values",[639,41233,41235,41236],{"id":41234},"safer-access-with-get","Safer access with ",[49,41237,22718],{},[14,41239,41240,41241,41243],{},"If you are not sure a key exists, ",[49,41242,22718],{}," can be safer than direct indexing.",[42,41245,41247],{"className":44,"code":41246,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"name\": \"Alice\"}'\ndata = json.loads(json_text)\n\nprint(data.get(\"name\"))\nprint(data.get(\"email\"))\nprint(data.get(\"email\", \"Not provided\"))\n",[49,41248,41249,41255,41259,41272,41290,41294,41316,41338],{"__ignoreMap":47},[52,41250,41251,41253],{"class":54,"line":55},[52,41252,59],{"class":58},[52,41254,40454],{"class":62},[52,41256,41257],{"class":54,"line":66},[52,41258,70],{"emptyLinePlaceholder":69},[52,41260,41261,41263,41265,41267,41270],{"class":54,"line":73},[52,41262,40463],{"class":62},[52,41264,80],{"class":79},[52,41266,4172],{"class":83},[52,41268,41269],{"class":87},"{\"name\": \"Alice\"}",[52,41271,24871],{"class":83},[52,41273,41274,41276,41278,41280,41282,41284,41286,41288],{"class":54,"line":94},[52,41275,217],{"class":62},[52,41277,80],{"class":79},[52,41279,2204],{"class":62},[52,41281,186],{"class":102},[52,41283,40489],{"class":189},[52,41285,193],{"class":102},[52,41287,40494],{"class":189},[52,41289,211],{"class":102},[52,41291,41292],{"class":54,"line":106},[52,41293,70],{"emptyLinePlaceholder":69},[52,41295,41296,41298,41300,41302,41304,41306,41308,41310,41312,41314],{"class":54,"line":128},[52,41297,242],{"class":241},[52,41299,193],{"class":102},[52,41301,247],{"class":189},[52,41303,186],{"class":102},[52,41305,190],{"class":189},[52,41307,193],{"class":102},[52,41309,115],{"class":83},[52,41311,2147],{"class":87},[52,41313,115],{"class":83},[52,41315,8886],{"class":102},[52,41317,41318,41320,41322,41324,41326,41328,41330,41332,41334,41336],{"class":54,"line":148},[52,41319,242],{"class":241},[52,41321,193],{"class":102},[52,41323,247],{"class":189},[52,41325,186],{"class":102},[52,41327,190],{"class":189},[52,41329,193],{"class":102},[52,41331,115],{"class":83},[52,41333,12172],{"class":87},[52,41335,115],{"class":83},[52,41337,8886],{"class":102},[52,41339,41340,41342,41344,41346,41348,41350,41352,41354,41356,41358,41360,41362,41365,41367],{"class":54,"line":164},[52,41341,242],{"class":241},[52,41343,193],{"class":102},[52,41345,247],{"class":189},[52,41347,186],{"class":102},[52,41349,190],{"class":189},[52,41351,193],{"class":102},[52,41353,115],{"class":83},[52,41355,12172],{"class":87},[52,41357,115],{"class":83},[52,41359,199],{"class":102},[52,41361,84],{"class":83},[52,41363,41364],{"class":87},"Not provided",[52,41366,115],{"class":83},[52,41368,8886],{"class":102},[14,41370,9266],{},[42,41372,41374],{"className":44,"code":41373,"language":46,"meta":47,"style":47},"Alice\nNone\nNot provided\n",[49,41375,41376,41380,41384],{"__ignoreMap":47},[52,41377,41378],{"class":54,"line":55},[52,41379,19253],{"class":62},[52,41381,41382],{"class":54,"line":66},[52,41383,19440],{"class":160},[52,41385,41386],{"class":54,"line":73},[52,41387,41388],{"class":62},"Not provided\n",[14,41390,41391],{},"This helps avoid errors when some fields may be missing.",[37,41393,41395],{"id":41394},"json-types-and-python-types","JSON types and Python types",[14,41397,41398],{},"When Python parses JSON, the data types change like this:",[309,41400,41401,41406,41411,41416,41424,41438],{},[24,41402,41403,41404],{},"JSON object → Python ",[49,41405,40992],{},[24,41407,41408,41409],{},"JSON array → Python ",[49,41410,15301],{},[24,41412,41413,41414],{},"JSON string → Python ",[49,41415,19523],{},[24,41417,41418,41419,857,41421],{},"JSON number → Python ",[49,41420,5142],{},[49,41422,41423],{},"float",[24,41425,41426,41427,41429,41430,41433,41434,41429,41436],{},"JSON ",[49,41428,40703],{}," \u002F ",[49,41431,41432],{},"false"," → Python ",[49,41435,6233],{},[49,41437,6241],{},[24,41439,41426,41440,41433,41443],{},[49,41441,41442],{},"null",[49,41444,18849],{},[14,41446,4259],{},[42,41448,41450],{"className":44,"code":41449,"language":46,"meta":47,"style":47},"import json\n\njson_text = \"\"\"\n{\n    \"title\": \"Book\",\n    \"price\": 19.99,\n    \"in_stock\": true,\n    \"tags\": [\"fiction\", \"popular\"],\n    \"discount\": null\n}\n\"\"\"\n\ndata = json.loads(json_text)\n\nprint(type(data))\nprint(type(data[\"title\"]))\nprint(type(data[\"price\"]))\nprint(type(data[\"in_stock\"]))\nprint(type(data[\"tags\"]))\nprint(data[\"discount\"])\n",[49,41451,41452,41458,41462,41470,41474,41479,41484,41489,41494,41499,41503,41507,41511,41529,41533,41547,41569,41591,41614,41637],{"__ignoreMap":47},[52,41453,41454,41456],{"class":54,"line":55},[52,41455,59],{"class":58},[52,41457,40454],{"class":62},[52,41459,41460],{"class":54,"line":66},[52,41461,70],{"emptyLinePlaceholder":69},[52,41463,41464,41466,41468],{"class":54,"line":73},[52,41465,40463],{"class":62},[52,41467,80],{"class":79},[52,41469,40640],{"class":83},[52,41471,41472],{"class":54,"line":94},[52,41473,980],{"class":87},[52,41475,41476],{"class":54,"line":106},[52,41477,41478],{"class":87},"    \"title\": \"Book\",\n",[52,41480,41481],{"class":54,"line":128},[52,41482,41483],{"class":87},"    \"price\": 19.99,\n",[52,41485,41486],{"class":54,"line":148},[52,41487,41488],{"class":87},"    \"in_stock\": true,\n",[52,41490,41491],{"class":54,"line":164},[52,41492,41493],{"class":87},"    \"tags\": [\"fiction\", \"popular\"],\n",[52,41495,41496],{"class":54,"line":170},[52,41497,41498],{"class":87},"    \"discount\": null\n",[52,41500,41501],{"class":54,"line":175},[52,41502,167],{"class":87},[52,41504,41505],{"class":54,"line":214},[52,41506,40687],{"class":83},[52,41508,41509],{"class":54,"line":233},[52,41510,70],{"emptyLinePlaceholder":69},[52,41512,41513,41515,41517,41519,41521,41523,41525,41527],{"class":54,"line":238},[52,41514,217],{"class":62},[52,41516,80],{"class":79},[52,41518,2204],{"class":62},[52,41520,186],{"class":102},[52,41522,40489],{"class":189},[52,41524,193],{"class":102},[52,41526,40494],{"class":189},[52,41528,211],{"class":102},[52,41530,41531],{"class":54,"line":272},[52,41532,70],{"emptyLinePlaceholder":69},[52,41534,41535,41537,41539,41541,41543,41545],{"class":54,"line":1348},[52,41536,242],{"class":241},[52,41538,193],{"class":102},[52,41540,4198],{"class":4733},[52,41542,193],{"class":102},[52,41544,247],{"class":189},[52,41546,8886],{"class":102},[52,41548,41549,41551,41553,41555,41557,41559,41561,41563,41565,41567],{"class":54,"line":1376},[52,41550,242],{"class":241},[52,41552,193],{"class":102},[52,41554,4198],{"class":4733},[52,41556,193],{"class":102},[52,41558,247],{"class":189},[52,41560,250],{"class":102},[52,41562,115],{"class":83},[52,41564,18682],{"class":87},[52,41566,115],{"class":83},[52,41568,20680],{"class":102},[52,41570,41571,41573,41575,41577,41579,41581,41583,41585,41587,41589],{"class":54,"line":1381},[52,41572,242],{"class":241},[52,41574,193],{"class":102},[52,41576,4198],{"class":4733},[52,41578,193],{"class":102},[52,41580,247],{"class":189},[52,41582,250],{"class":102},[52,41584,115],{"class":83},[52,41586,17838],{"class":87},[52,41588,115],{"class":83},[52,41590,20680],{"class":102},[52,41592,41593,41595,41597,41599,41601,41603,41605,41607,41610,41612],{"class":54,"line":1406},[52,41594,242],{"class":241},[52,41596,193],{"class":102},[52,41598,4198],{"class":4733},[52,41600,193],{"class":102},[52,41602,247],{"class":189},[52,41604,250],{"class":102},[52,41606,115],{"class":83},[52,41608,41609],{"class":87},"in_stock",[52,41611,115],{"class":83},[52,41613,20680],{"class":102},[52,41615,41616,41618,41620,41622,41624,41626,41628,41630,41633,41635],{"class":54,"line":1430},[52,41617,242],{"class":241},[52,41619,193],{"class":102},[52,41621,4198],{"class":4733},[52,41623,193],{"class":102},[52,41625,247],{"class":189},[52,41627,250],{"class":102},[52,41629,115],{"class":83},[52,41631,41632],{"class":87},"tags",[52,41634,115],{"class":83},[52,41636,20680],{"class":102},[52,41638,41639,41641,41643,41645,41647,41649,41652,41654],{"class":54,"line":1435},[52,41640,242],{"class":241},[52,41642,193],{"class":102},[52,41644,247],{"class":189},[52,41646,250],{"class":102},[52,41648,115],{"class":83},[52,41650,41651],{"class":87},"discount",[52,41653,115],{"class":83},[52,41655,269],{"class":102},[14,41657,9266],{},[42,41659,41661],{"className":44,"code":41660,"language":46,"meta":47,"style":47},"\u003Cclass 'dict'>\n\u003Cclass 'str'>\n\u003Cclass 'float'>\n\u003Cclass 'bool'>\n\u003Cclass 'list'>\nNone\n",[49,41662,41663,41677,41691,41705,41720,41734],{"__ignoreMap":47},[52,41664,41665,41667,41669,41671,41673,41675],{"class":54,"line":55},[52,41666,15294],{"class":79},[52,41668,6994],{"class":1475},[52,41670,4172],{"class":83},[52,41672,40992],{"class":87},[52,41674,4162],{"class":83},[52,41676,15306],{"class":79},[52,41678,41679,41681,41683,41685,41687,41689],{"class":54,"line":66},[52,41680,15294],{"class":79},[52,41682,6994],{"class":1475},[52,41684,4172],{"class":83},[52,41686,19523],{"class":87},[52,41688,4162],{"class":83},[52,41690,15306],{"class":79},[52,41692,41693,41695,41697,41699,41701,41703],{"class":54,"line":73},[52,41694,15294],{"class":79},[52,41696,6994],{"class":1475},[52,41698,4172],{"class":83},[52,41700,41423],{"class":87},[52,41702,4162],{"class":83},[52,41704,15306],{"class":79},[52,41706,41707,41709,41711,41713,41716,41718],{"class":54,"line":94},[52,41708,15294],{"class":79},[52,41710,6994],{"class":1475},[52,41712,4172],{"class":83},[52,41714,41715],{"class":87},"bool",[52,41717,4162],{"class":83},[52,41719,15306],{"class":79},[52,41721,41722,41724,41726,41728,41730,41732],{"class":54,"line":106},[52,41723,15294],{"class":79},[52,41725,6994],{"class":1475},[52,41727,4172],{"class":83},[52,41729,15301],{"class":87},[52,41731,4162],{"class":83},[52,41733,15306],{"class":79},[52,41735,41736],{"class":54,"line":128},[52,41737,19440],{"class":160},[37,41739,41741],{"id":41740},"parse-json-from-a-file","Parse JSON from a file",[14,41743,3650,41744,41746],{},[49,41745,40561],{}," when the JSON is in a file.",[42,41748,41750],{"className":44,"code":41749,"language":46,"meta":47,"style":47},"import json\n\nwith open(\"data.json\", \"r\") as file:\n    data = json.load(file)\n\nprint(data)\n",[49,41751,41752,41758,41762,41794,41813,41817],{"__ignoreMap":47},[52,41753,41754,41756],{"class":54,"line":55},[52,41755,59],{"class":58},[52,41757,40454],{"class":62},[52,41759,41760],{"class":54,"line":66},[52,41761,70],{"emptyLinePlaceholder":69},[52,41763,41764,41766,41768,41770,41772,41775,41777,41779,41781,41784,41786,41788,41790,41792],{"class":54,"line":73},[52,41765,14632],{"class":58},[52,41767,14635],{"class":241},[52,41769,193],{"class":102},[52,41771,115],{"class":83},[52,41773,41774],{"class":87},"data.json",[52,41776,115],{"class":83},[52,41778,199],{"class":102},[52,41780,84],{"class":83},[52,41782,41783],{"class":87},"r",[52,41785,115],{"class":83},[52,41787,1521],{"class":102},[52,41789,3567],{"class":58},[52,41791,14676],{"class":14675},[52,41793,1330],{"class":102},[52,41795,41796,41798,41800,41802,41804,41807,41809,41811],{"class":54,"line":94},[52,41797,1335],{"class":62},[52,41799,80],{"class":79},[52,41801,2204],{"class":62},[52,41803,186],{"class":102},[52,41805,41806],{"class":189},"load",[52,41808,193],{"class":102},[52,41810,14698],{"class":14675},[52,41812,211],{"class":102},[52,41814,41815],{"class":54,"line":106},[52,41816,70],{"emptyLinePlaceholder":69},[52,41818,41819,41821,41823,41825],{"class":54,"line":128},[52,41820,242],{"class":241},[52,41822,193],{"class":102},[52,41824,247],{"class":189},[52,41826,211],{"class":102},[639,41828,41830,41833,41834],{"id":41829},"load-vs-loads",[49,41831,41832],{},"load()"," vs ",[49,41835,41836],{},"loads()",[309,41838,41839,41848],{},[24,41840,41841,41844,41845],{},[49,41842,41843],{},"json.load(file)"," reads JSON from a ",[5503,41846,41847],{},"file object",[24,41849,41850,41844,41853],{},[49,41851,41852],{},"json.loads(text)",[5503,41854,41855],{},"string",[14,41857,41858],{},"That is the main difference.",[14,41860,41861,41862,759,41866,186],{},"If you want a full guide, see ",[372,41863,41865],{"href":41864},"\u002Fstandard-library\u002Fjson.load-function-explained","json.load() explained",[372,41867,856],{"href":855},[37,41869,41871],{"id":41870},"common-errors-beginners-make","Common errors beginners make",[14,41873,41874],{},"Here are some common problems when parsing JSON.",[639,41876,41878],{"id":41877},"_1-using-single-quotes-inside-json-text","1. Using single quotes inside JSON text",[14,41880,41881],{},"This is invalid JSON:",[42,41883,41885],{"className":44,"code":41884,"language":46,"meta":47,"style":47},"json_text = \"{'name': 'Alice'}\"\n",[49,41886,41887],{"__ignoreMap":47},[52,41888,41889,41891,41893,41895,41898],{"class":54,"line":55},[52,41890,40463],{"class":62},[52,41892,80],{"class":79},[52,41894,84],{"class":83},[52,41896,41897],{"class":87},"{'name': 'Alice'}",[52,41899,91],{"class":83},[14,41901,41902],{},"JSON requires double quotes:",[42,41904,41906],{"className":44,"code":41905,"language":46,"meta":47,"style":47},"json_text = '{\"name\": \"Alice\"}'\n",[49,41907,41908],{"__ignoreMap":47},[52,41909,41910,41912,41914,41916,41918],{"class":54,"line":55},[52,41911,40463],{"class":62},[52,41913,80],{"class":79},[52,41915,4172],{"class":83},[52,41917,41269],{"class":87},[52,41919,24871],{"class":83},[14,41921,41922,41923,186],{},"If your JSON syntax is wrong, Python may raise a decoding error. See ",[372,41924,41926],{"href":41925},"\u002Ferrors\u002Fsyntaxerror-unexpected-eof-while-parsing-fix","how to fix JSONDecodeError for invalid JSON",[639,41928,41930,41931,759,41933],{"id":41929},"_2-confusing-jsonload-and-jsonloads","2. Confusing ",[49,41932,40561],{},[49,41934,2932],{},[14,41936,41937],{},"This is a common mix-up:",[309,41939,41940,41945],{},[24,41941,41942,41944],{},[49,41943,41806],{}," = file",[24,41946,41947,41949],{},[49,41948,40489],{}," = string",[14,41951,41952],{},"A simple memory trick:",[309,41954,41955,41960],{},[24,41956,41957,41959],{},[5503,41958,41806],{}," = load from file",[24,41961,41962,41964],{},[5503,41963,40489],{}," = load from string",[639,41966,41968],{"id":41967},"_3-trying-to-access-a-missing-key","3. Trying to access a missing key",[14,41970,41971],{},"This causes an error:",[42,41973,41975],{"className":44,"code":41974,"language":46,"meta":47,"style":47},"import json\n\ndata = json.loads('{\"name\": \"Alice\"}')\nprint(data[\"email\"])\n",[49,41976,41977,41983,41987,42009],{"__ignoreMap":47},[52,41978,41979,41981],{"class":54,"line":55},[52,41980,59],{"class":58},[52,41982,40454],{"class":62},[52,41984,41985],{"class":54,"line":66},[52,41986,70],{"emptyLinePlaceholder":69},[52,41988,41989,41991,41993,41995,41997,41999,42001,42003,42005,42007],{"class":54,"line":73},[52,41990,217],{"class":62},[52,41992,80],{"class":79},[52,41994,2204],{"class":62},[52,41996,186],{"class":102},[52,41998,40489],{"class":189},[52,42000,193],{"class":102},[52,42002,4162],{"class":83},[52,42004,41269],{"class":87},[52,42006,4162],{"class":83},[52,42008,211],{"class":102},[52,42010,42011,42013,42015,42017,42019,42021,42023,42025],{"class":54,"line":94},[52,42012,242],{"class":241},[52,42014,193],{"class":102},[52,42016,247],{"class":189},[52,42018,250],{"class":102},[52,42020,115],{"class":83},[52,42022,12172],{"class":87},[52,42024,115],{"class":83},[52,42026,269],{"class":102},[14,42028,3650,42029,42031],{},[49,42030,22718],{}," if the key may not exist:",[42,42033,42035],{"className":44,"code":42034,"language":46,"meta":47,"style":47},"print(data.get(\"email\"))\n",[49,42036,42037],{"__ignoreMap":47},[52,42038,42039,42041,42043,42045,42047,42049,42051,42053,42055,42057],{"class":54,"line":55},[52,42040,242],{"class":241},[52,42042,193],{"class":102},[52,42044,247],{"class":189},[52,42046,186],{"class":102},[52,42048,190],{"class":189},[52,42050,193],{"class":102},[52,42052,115],{"class":83},[52,42054,12172],{"class":87},[52,42056,115],{"class":83},[52,42058,8886],{"class":102},[639,42060,42062],{"id":42061},"_4-forgetting-that-a-parsed-array-is-a-list","4. Forgetting that a parsed array is a list",[14,42064,42065],{},"If the JSON starts as an array:",[42,42067,42069],{"className":44,"code":42068,"language":46,"meta":47,"style":47},"import json\n\ndata = json.loads('[\"Python\", \"SQL\", \"Git\"]')\nprint(type(data))\nprint(data[0])\n",[49,42070,42071,42077,42081,42104,42118],{"__ignoreMap":47},[52,42072,42073,42075],{"class":54,"line":55},[52,42074,59],{"class":58},[52,42076,40454],{"class":62},[52,42078,42079],{"class":54,"line":66},[52,42080,70],{"emptyLinePlaceholder":69},[52,42082,42083,42085,42087,42089,42091,42093,42095,42097,42100,42102],{"class":54,"line":73},[52,42084,217],{"class":62},[52,42086,80],{"class":79},[52,42088,2204],{"class":62},[52,42090,186],{"class":102},[52,42092,40489],{"class":189},[52,42094,193],{"class":102},[52,42096,4162],{"class":83},[52,42098,42099],{"class":87},"[\"Python\", \"SQL\", \"Git\"]",[52,42101,4162],{"class":83},[52,42103,211],{"class":102},[52,42105,42106,42108,42110,42112,42114,42116],{"class":54,"line":94},[52,42107,242],{"class":241},[52,42109,193],{"class":102},[52,42111,4198],{"class":4733},[52,42113,193],{"class":102},[52,42115,247],{"class":189},[52,42117,8886],{"class":102},[52,42119,42120,42122,42124,42126,42128,42130],{"class":54,"line":106},[52,42121,242],{"class":241},[52,42123,193],{"class":102},[52,42125,247],{"class":189},[52,42127,250],{"class":102},[52,42129,13294],{"class":121},[52,42131,269],{"class":102},[14,42133,9266],{},[42,42135,42137],{"className":44,"code":42136,"language":46,"meta":47,"style":47},"\u003Cclass 'list'>\nPython\n",[49,42138,42139,42153],{"__ignoreMap":47},[52,42140,42141,42143,42145,42147,42149,42151],{"class":54,"line":55},[52,42142,15294],{"class":79},[52,42144,6994],{"class":1475},[52,42146,4172],{"class":83},[52,42148,15301],{"class":87},[52,42150,4162],{"class":83},[52,42152,15306],{"class":79},[52,42154,42155],{"class":54,"line":66},[52,42156,41189],{"class":62},[14,42158,42159,42160,42162],{},"You cannot use a string key like ",[49,42161,41205],{}," on a list.",[37,42164,42166],{"id":42165},"when-to-use-this-pattern","When to use this pattern",[14,42168,42169],{},"This simple parsing pattern is useful when you are:",[309,42171,42172,42175,42178,42181],{},[24,42173,42174],{},"reading API responses",[24,42176,42177],{},"loading app settings from JSON",[24,42179,42180],{},"working with saved data files",[24,42182,42183],{},"practicing how structured text becomes Python objects",[14,42185,42186,42187,186],{},"If you plan to use JSON from web requests, you may also like ",[372,42188,1172],{"href":1171},[37,42190,1942],{"id":1941},[639,42192,18248,42194,759,42196,1995],{"id":42193},"what-is-the-difference-between-jsonload-and-jsonloads",[49,42195,40561],{},[49,42197,2932],{},[14,42199,42200,42202,42203,42205],{},[49,42201,40561],{}," reads JSON from a file object. ",[49,42204,2932],{}," reads JSON from a string.",[639,42207,42209],{"id":42208},"does-parsed-json-always-become-a-dictionary","Does parsed JSON always become a dictionary?",[14,42211,42212,42213,42215,42216,186],{},"No. A JSON object becomes a ",[49,42214,40992],{},", but a JSON array becomes a ",[49,42217,15301],{},[639,42219,42221],{"id":42220},"why-does-my-json-string-fail-to-parse","Why does my JSON string fail to parse?",[14,42223,42224],{},"It may use invalid JSON syntax, such as single quotes, missing commas, or trailing commas.",[639,42226,42228],{"id":42227},"how-do-i-get-a-nested-value-from-parsed-json","How do I get a nested value from parsed JSON?",[14,42230,42231,42232,186],{},"Use normal Python indexing step by step, such as ",[49,42233,42234],{},"data['user']['name']",[37,42236,2005],{"id":2004},[309,42238,42239,42243,42248,42253,42257,42263,42267,42272],{},[24,42240,42241],{},[372,42242,861],{"href":860},[24,42244,42245],{},[372,42246,42247],{"href":2929},"json.loads() function explained",[24,42249,42250],{},[372,42251,42252],{"href":41864},"json.load() function explained",[24,42254,42255],{},[372,42256,2022],{"href":855},[24,42258,42259],{},[372,42260,42262],{"href":42261},"\u002Fhow-to\u002Fhow-to-convert-dictionary-to-json-in-python\u002F","How to convert a dictionary to JSON in Python",[24,42264,42265],{},[372,42266,2017],{"href":1171},[24,42268,42269],{},[372,42270,42271],{"href":41230},"Fix KeyError when accessing dictionary values",[24,42273,42274],{},[372,42275,42276],{"href":41925},"Fix JSONDecodeError for invalid JSON",[2034,42278,42279],{},"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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":47,"searchDepth":66,"depth":66,"links":42281},[42282,42283,42284,42288,42293,42294,42298,42305,42306,42313],{"id":2267,"depth":66,"text":2268},{"id":40605,"depth":66,"text":40606},{"id":40713,"depth":66,"text":42285,"children":42286},"Parse JSON with json.loads()",[42287],{"id":36905,"depth":73,"text":36906},{"id":41014,"depth":66,"text":41015,"children":42289},[42290,42291],{"id":41197,"depth":73,"text":41198},{"id":41234,"depth":73,"text":42292},"Safer access with get()",{"id":41394,"depth":66,"text":41395},{"id":41740,"depth":66,"text":41741,"children":42295},[42296],{"id":41829,"depth":73,"text":42297},"load() vs loads()",{"id":41870,"depth":66,"text":41871,"children":42299},[42300,42301,42303,42304],{"id":41877,"depth":73,"text":41878},{"id":41929,"depth":73,"text":42302},"2. Confusing json.load() and json.loads()",{"id":41967,"depth":73,"text":41968},{"id":42061,"depth":73,"text":42062},{"id":42165,"depth":66,"text":42166},{"id":1941,"depth":66,"text":1942,"children":42307},[42308,42310,42311,42312],{"id":42193,"depth":73,"text":42309},"What is the difference between json.load() and json.loads()?",{"id":42208,"depth":73,"text":42209},{"id":42220,"depth":73,"text":42221},{"id":42227,"depth":73,"text":42228},{"id":2004,"depth":66,"text":2005},"Master python json parser example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-json-parser-example",{"title":40411,"description":42314},"examples\u002Fpython-json-parser-example","mX_j2zlbeCETYTCcxPO_HNIMl_wb7W90UI9tG2cmyac",{"id":42321,"title":42322,"body":42323,"description":44275,"extension":2075,"meta":44276,"navigation":69,"path":44277,"seo":44278,"stem":44279,"__hash__":44280},"content\u002Fexamples\u002Fpython-json-to-dictionary-example.md","Python JSON to Dictionary Example",{"type":7,"value":42324,"toc":44242},[42325,42328,42333,42336,42338,42430,42434,42503,42508,42510,42523,42525,42528,42539,42543,42547,42550,42568,42668,42672,42733,42735,42752,42757,42761,42764,42767,42790,42792,42926,42930,43038,43042,43045,43149,43153,43180,43188,43203,43205,43276,43280,43288,43299,43303,43306,43484,43488,43516,43520,43530,43532,43552,43556,43559,43574,43578,43637,43641,43647,43674,43677,43753,43760,43762,43765,43782,43786,43788,43833,43838,43840,43898,43902,43905,43908,43949,43952,43970,43973,43984,43988,43994,44039,44044,44048,44051,44125,44129,44176,44178,44182,44187,44189,44195,44199,44202,44206,44209,44211,44239],[10,42326,42322],{"id":42327},"python-json-to-dictionary-example",[14,42329,42330,42331,13338],{},"If you have JSON text and want to turn it into normal Python data, the usual tool is the built-in ",[49,42332,227],{},[14,42334,42335],{},"This example shows one practical task: converting a JSON string into a Python dictionary so you can read values by key and use them in your program.",[37,42337,40],{"id":39},[42,42339,42341],{"className":44,"code":42340,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"name\": \"Alice\", \"age\": 25, \"is_admin\": false}'\ndata = json.loads(json_text)\n\nprint(data)\nprint(type(data))\nprint(data[\"name\"])\n",[49,42342,42343,42349,42353,42366,42384,42388,42398,42412],{"__ignoreMap":47},[52,42344,42345,42347],{"class":54,"line":55},[52,42346,59],{"class":58},[52,42348,40454],{"class":62},[52,42350,42351],{"class":54,"line":66},[52,42352,70],{"emptyLinePlaceholder":69},[52,42354,42355,42357,42359,42361,42364],{"class":54,"line":73},[52,42356,40463],{"class":62},[52,42358,80],{"class":79},[52,42360,4172],{"class":83},[52,42362,42363],{"class":87},"{\"name\": \"Alice\", \"age\": 25, \"is_admin\": false}",[52,42365,24871],{"class":83},[52,42367,42368,42370,42372,42374,42376,42378,42380,42382],{"class":54,"line":94},[52,42369,217],{"class":62},[52,42371,80],{"class":79},[52,42373,2204],{"class":62},[52,42375,186],{"class":102},[52,42377,40489],{"class":189},[52,42379,193],{"class":102},[52,42381,40494],{"class":189},[52,42383,211],{"class":102},[52,42385,42386],{"class":54,"line":106},[52,42387,70],{"emptyLinePlaceholder":69},[52,42389,42390,42392,42394,42396],{"class":54,"line":128},[52,42391,242],{"class":241},[52,42393,193],{"class":102},[52,42395,247],{"class":189},[52,42397,211],{"class":102},[52,42399,42400,42402,42404,42406,42408,42410],{"class":54,"line":148},[52,42401,242],{"class":241},[52,42403,193],{"class":102},[52,42405,4198],{"class":4733},[52,42407,193],{"class":102},[52,42409,247],{"class":189},[52,42411,8886],{"class":102},[52,42413,42414,42416,42418,42420,42422,42424,42426,42428],{"class":54,"line":164},[52,42415,242],{"class":241},[52,42417,193],{"class":102},[52,42419,247],{"class":189},[52,42421,250],{"class":102},[52,42423,115],{"class":83},[52,42425,2147],{"class":87},[52,42427,115],{"class":83},[52,42429,269],{"class":102},[14,42431,42432],{},[5503,42433,5267],{},[42,42435,42437],{"className":44,"code":42436,"language":46,"meta":47,"style":47},"{'name': 'Alice', 'age': 25, 'is_admin': False}\n\u003Cclass 'dict'>\nAlice\n",[49,42438,42439,42485,42499],{"__ignoreMap":47},[52,42440,42441,42443,42445,42447,42449,42451,42453,42455,42457,42459,42461,42463,42465,42467,42469,42471,42473,42476,42478,42480,42483],{"class":54,"line":55},[52,42442,1482],{"class":102},[52,42444,4162],{"class":83},[52,42446,2147],{"class":87},[52,42448,4162],{"class":83},[52,42450,118],{"class":102},[52,42452,4172],{"class":83},[52,42454,2156],{"class":87},[52,42456,4162],{"class":83},[52,42458,199],{"class":102},[52,42460,4172],{"class":83},[52,42462,2167],{"class":87},[52,42464,4162],{"class":83},[52,42466,118],{"class":102},[52,42468,2674],{"class":121},[52,42470,199],{"class":102},[52,42472,4172],{"class":83},[52,42474,42475],{"class":87},"is_admin",[52,42477,4162],{"class":83},[52,42479,118],{"class":102},[52,42481,42482],{"class":160}," False",[52,42484,167],{"class":102},[52,42486,42487,42489,42491,42493,42495,42497],{"class":54,"line":66},[52,42488,15294],{"class":79},[52,42490,6994],{"class":1475},[52,42492,4172],{"class":83},[52,42494,40992],{"class":87},[52,42496,4162],{"class":83},[52,42498,15306],{"class":79},[52,42500,42501],{"class":54,"line":73},[52,42502,19253],{"class":62},[14,42504,3650,42505,42507],{},[49,42506,2932],{}," for a JSON string. If the JSON root value is an object, it returns a Python dictionary.",[37,42509,323],{"id":322},[309,42511,42512,42515,42520],{},[24,42513,42514],{},"Shows how to convert a JSON string into a Python dictionary",[24,42516,42517,42518,14928],{},"Uses the built-in ",[49,42519,227],{},[24,42521,42522],{},"Explains the difference between JSON text and Python data",[37,42524,37780],{"id":37779},[14,42526,42527],{},"Use this approach in common situations like these:",[309,42529,42530,42533,42536],{},[24,42531,42532],{},"When you get JSON from an API",[24,42534,42535],{},"When you read JSON from a file or response body",[24,42537,42538],{},"When you need to access values by key in Python",[14,42540,4219,42541,186],{},[372,42542,856],{"href":855},[37,42544,42546],{"id":42545},"basic-example-with-jsonloads","Basic example with json.loads()",[14,42548,42549],{},"To convert JSON text into Python data:",[21,42551,42552,42557,42560,42565],{},[24,42553,42554,42555],{},"Import ",[49,42556,227],{},[24,42558,42559],{},"Store the JSON in a string",[24,42561,16483,42562],{},[49,42563,42564],{},"json.loads(json_text)",[24,42566,42567],{},"Read values from the result",[42,42569,42571],{"className":44,"code":42570,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"name\": \"Alice\", \"age\": 25, \"city\": \"London\"}'\ndata = json.loads(json_text)\n\nprint(data)\nprint(data[\"name\"])\nprint(data.get(\"age\"))\n",[49,42572,42573,42579,42583,42596,42614,42618,42628,42646],{"__ignoreMap":47},[52,42574,42575,42577],{"class":54,"line":55},[52,42576,59],{"class":58},[52,42578,40454],{"class":62},[52,42580,42581],{"class":54,"line":66},[52,42582,70],{"emptyLinePlaceholder":69},[52,42584,42585,42587,42589,42591,42594],{"class":54,"line":73},[52,42586,40463],{"class":62},[52,42588,80],{"class":79},[52,42590,4172],{"class":83},[52,42592,42593],{"class":87},"{\"name\": \"Alice\", \"age\": 25, \"city\": \"London\"}",[52,42595,24871],{"class":83},[52,42597,42598,42600,42602,42604,42606,42608,42610,42612],{"class":54,"line":94},[52,42599,217],{"class":62},[52,42601,80],{"class":79},[52,42603,2204],{"class":62},[52,42605,186],{"class":102},[52,42607,40489],{"class":189},[52,42609,193],{"class":102},[52,42611,40494],{"class":189},[52,42613,211],{"class":102},[52,42615,42616],{"class":54,"line":106},[52,42617,70],{"emptyLinePlaceholder":69},[52,42619,42620,42622,42624,42626],{"class":54,"line":128},[52,42621,242],{"class":241},[52,42623,193],{"class":102},[52,42625,247],{"class":189},[52,42627,211],{"class":102},[52,42629,42630,42632,42634,42636,42638,42640,42642,42644],{"class":54,"line":148},[52,42631,242],{"class":241},[52,42633,193],{"class":102},[52,42635,247],{"class":189},[52,42637,250],{"class":102},[52,42639,115],{"class":83},[52,42641,2147],{"class":87},[52,42643,115],{"class":83},[52,42645,269],{"class":102},[52,42647,42648,42650,42652,42654,42656,42658,42660,42662,42664,42666],{"class":54,"line":164},[52,42649,242],{"class":241},[52,42651,193],{"class":102},[52,42653,247],{"class":189},[52,42655,186],{"class":102},[52,42657,190],{"class":189},[52,42659,193],{"class":102},[52,42661,115],{"class":83},[52,42663,2167],{"class":87},[52,42665,115],{"class":83},[52,42667,8886],{"class":102},[14,42669,42670],{},[5503,42671,5267],{},[42,42673,42675],{"className":44,"code":42674,"language":46,"meta":47,"style":47},"{'name': 'Alice', 'age': 25, 'city': 'London'}\nAlice\n25\n",[49,42676,42677,42725,42729],{"__ignoreMap":47},[52,42678,42679,42681,42683,42685,42687,42689,42691,42693,42695,42697,42699,42701,42703,42705,42707,42709,42711,42713,42715,42717,42719,42721,42723],{"class":54,"line":55},[52,42680,1482],{"class":102},[52,42682,4162],{"class":83},[52,42684,2147],{"class":87},[52,42686,4162],{"class":83},[52,42688,118],{"class":102},[52,42690,4172],{"class":83},[52,42692,2156],{"class":87},[52,42694,4162],{"class":83},[52,42696,199],{"class":102},[52,42698,4172],{"class":83},[52,42700,2167],{"class":87},[52,42702,4162],{"class":83},[52,42704,118],{"class":102},[52,42706,2674],{"class":121},[52,42708,199],{"class":102},[52,42710,4172],{"class":83},[52,42712,15026],{"class":87},[52,42714,4162],{"class":83},[52,42716,118],{"class":102},[52,42718,4172],{"class":83},[52,42720,15056],{"class":87},[52,42722,4162],{"class":83},[52,42724,167],{"class":102},[52,42726,42727],{"class":54,"line":66},[52,42728,19253],{"class":62},[52,42730,42731],{"class":54,"line":73},[52,42732,19258],{"class":121},[639,42734,24095],{"id":24094},[309,42736,42737,42742,42747],{},[24,42738,42739,42741],{},[49,42740,40494],{}," is plain text in JSON format",[24,42743,42744,42746],{},[49,42745,42564],{}," parses that text",[24,42748,42749,42750],{},"Because the top-level JSON value is an object, the result is a Python ",[49,42751,40992],{},[14,42753,42754,42755,186],{},"If you want more detail on this function, read ",[372,42756,41011],{"href":2929},[37,42758,42760],{"id":42759},"what-the-result-looks-like","What the result looks like",[14,42762,42763],{},"JSON does not always become a dictionary. It depends on the JSON value you parse.",[14,42765,42766],{},"Here is the usual mapping:",[309,42768,42769,42772,42775],{},[24,42770,42771],{},"JSON objects become Python dictionaries",[24,42773,42774],{},"JSON arrays become Python lists",[24,42776,41426,42777,2587,42779,2587,42781,42783,42784,2587,42786,2587,42788],{},[49,42778,40703],{},[49,42780,41432],{},[49,42782,41442],{}," become ",[49,42785,6233],{},[49,42787,6241],{},[49,42789,18849],{},[14,42791,4259],{},[42,42793,42795],{"className":44,"code":42794,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"user\": \"Alice\", \"scores\": [10, 20, 30], \"active\": true, \"nickname\": null}'\ndata = json.loads(json_text)\n\nprint(data)\nprint(type(data))\nprint(type(data[\"scores\"]))\nprint(data[\"active\"])\nprint(data[\"nickname\"])\n",[49,42796,42797,42803,42807,42820,42838,42842,42852,42866,42889,42907],{"__ignoreMap":47},[52,42798,42799,42801],{"class":54,"line":55},[52,42800,59],{"class":58},[52,42802,40454],{"class":62},[52,42804,42805],{"class":54,"line":66},[52,42806,70],{"emptyLinePlaceholder":69},[52,42808,42809,42811,42813,42815,42818],{"class":54,"line":73},[52,42810,40463],{"class":62},[52,42812,80],{"class":79},[52,42814,4172],{"class":83},[52,42816,42817],{"class":87},"{\"user\": \"Alice\", \"scores\": [10, 20, 30], \"active\": true, \"nickname\": null}",[52,42819,24871],{"class":83},[52,42821,42822,42824,42826,42828,42830,42832,42834,42836],{"class":54,"line":94},[52,42823,217],{"class":62},[52,42825,80],{"class":79},[52,42827,2204],{"class":62},[52,42829,186],{"class":102},[52,42831,40489],{"class":189},[52,42833,193],{"class":102},[52,42835,40494],{"class":189},[52,42837,211],{"class":102},[52,42839,42840],{"class":54,"line":106},[52,42841,70],{"emptyLinePlaceholder":69},[52,42843,42844,42846,42848,42850],{"class":54,"line":128},[52,42845,242],{"class":241},[52,42847,193],{"class":102},[52,42849,247],{"class":189},[52,42851,211],{"class":102},[52,42853,42854,42856,42858,42860,42862,42864],{"class":54,"line":148},[52,42855,242],{"class":241},[52,42857,193],{"class":102},[52,42859,4198],{"class":4733},[52,42861,193],{"class":102},[52,42863,247],{"class":189},[52,42865,8886],{"class":102},[52,42867,42868,42870,42872,42874,42876,42878,42880,42882,42885,42887],{"class":54,"line":164},[52,42869,242],{"class":241},[52,42871,193],{"class":102},[52,42873,4198],{"class":4733},[52,42875,193],{"class":102},[52,42877,247],{"class":189},[52,42879,250],{"class":102},[52,42881,115],{"class":83},[52,42883,42884],{"class":87},"scores",[52,42886,115],{"class":83},[52,42888,20680],{"class":102},[52,42890,42891,42893,42895,42897,42899,42901,42903,42905],{"class":54,"line":170},[52,42892,242],{"class":241},[52,42894,193],{"class":102},[52,42896,247],{"class":189},[52,42898,250],{"class":102},[52,42900,115],{"class":83},[52,42902,40917],{"class":87},[52,42904,115],{"class":83},[52,42906,269],{"class":102},[52,42908,42909,42911,42913,42915,42917,42919,42922,42924],{"class":54,"line":175},[52,42910,242],{"class":241},[52,42912,193],{"class":102},[52,42914,247],{"class":189},[52,42916,250],{"class":102},[52,42918,115],{"class":83},[52,42920,42921],{"class":87},"nickname",[52,42923,115],{"class":83},[52,42925,269],{"class":102},[14,42927,42928],{},[5503,42929,5267],{},[42,42931,42933],{"className":44,"code":42932,"language":46,"meta":47,"style":47},"{'user': 'Alice', 'scores': [10, 20, 30], 'active': True, 'nickname': None}\n\u003Cclass 'dict'>\n\u003Cclass 'list'>\nTrue\nNone\n",[49,42934,42935,43002,43016,43030,43034],{"__ignoreMap":47},[52,42936,42937,42939,42941,42944,42946,42948,42950,42952,42954,42956,42958,42960,42962,42964,42966,42968,42970,42972,42974,42976,42978,42980,42982,42984,42986,42988,42990,42992,42994,42996,42998,43000],{"class":54,"line":55},[52,42938,1482],{"class":102},[52,42940,4162],{"class":83},[52,42942,42943],{"class":87},"user",[52,42945,4162],{"class":83},[52,42947,118],{"class":102},[52,42949,4172],{"class":83},[52,42951,2156],{"class":87},[52,42953,4162],{"class":83},[52,42955,199],{"class":102},[52,42957,4172],{"class":83},[52,42959,42884],{"class":87},[52,42961,4162],{"class":83},[52,42963,118],{"class":102},[52,42965,18162],{"class":102},[52,42967,3373],{"class":121},[52,42969,199],{"class":102},[52,42971,32771],{"class":121},[52,42973,199],{"class":102},[52,42975,16696],{"class":121},[52,42977,15661],{"class":102},[52,42979,4172],{"class":83},[52,42981,40917],{"class":87},[52,42983,4162],{"class":83},[52,42985,118],{"class":102},[52,42987,10943],{"class":160},[52,42989,199],{"class":102},[52,42991,4172],{"class":83},[52,42993,42921],{"class":87},[52,42995,4162],{"class":83},[52,42997,118],{"class":102},[52,42999,1450],{"class":160},[52,43001,167],{"class":102},[52,43003,43004,43006,43008,43010,43012,43014],{"class":54,"line":66},[52,43005,15294],{"class":79},[52,43007,6994],{"class":1475},[52,43009,4172],{"class":83},[52,43011,40992],{"class":87},[52,43013,4162],{"class":83},[52,43015,15306],{"class":79},[52,43017,43018,43020,43022,43024,43026,43028],{"class":54,"line":73},[52,43019,15294],{"class":79},[52,43021,6994],{"class":1475},[52,43023,4172],{"class":83},[52,43025,15301],{"class":87},[52,43027,4162],{"class":83},[52,43029,15306],{"class":79},[52,43031,43032],{"class":54,"line":94},[52,43033,40188],{"class":160},[52,43035,43036],{"class":54,"line":106},[52,43037,19440],{"class":160},[37,43039,43041],{"id":43040},"example-access-dictionary-values","Example: access dictionary values",[14,43043,43044],{},"Once the JSON has been converted, you can use it like a normal dictionary.",[42,43046,43048],{"className":44,"code":43047,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"name\": \"Alice\", \"age\": 25}'\ndata = json.loads(json_text)\n\nprint(data[\"name\"])\nprint(data.get(\"name\"))\nprint(type(data))\n",[49,43049,43050,43056,43060,43073,43091,43095,43113,43135],{"__ignoreMap":47},[52,43051,43052,43054],{"class":54,"line":55},[52,43053,59],{"class":58},[52,43055,40454],{"class":62},[52,43057,43058],{"class":54,"line":66},[52,43059,70],{"emptyLinePlaceholder":69},[52,43061,43062,43064,43066,43068,43071],{"class":54,"line":73},[52,43063,40463],{"class":62},[52,43065,80],{"class":79},[52,43067,4172],{"class":83},[52,43069,43070],{"class":87},"{\"name\": \"Alice\", \"age\": 25}",[52,43072,24871],{"class":83},[52,43074,43075,43077,43079,43081,43083,43085,43087,43089],{"class":54,"line":94},[52,43076,217],{"class":62},[52,43078,80],{"class":79},[52,43080,2204],{"class":62},[52,43082,186],{"class":102},[52,43084,40489],{"class":189},[52,43086,193],{"class":102},[52,43088,40494],{"class":189},[52,43090,211],{"class":102},[52,43092,43093],{"class":54,"line":106},[52,43094,70],{"emptyLinePlaceholder":69},[52,43096,43097,43099,43101,43103,43105,43107,43109,43111],{"class":54,"line":128},[52,43098,242],{"class":241},[52,43100,193],{"class":102},[52,43102,247],{"class":189},[52,43104,250],{"class":102},[52,43106,115],{"class":83},[52,43108,2147],{"class":87},[52,43110,115],{"class":83},[52,43112,269],{"class":102},[52,43114,43115,43117,43119,43121,43123,43125,43127,43129,43131,43133],{"class":54,"line":148},[52,43116,242],{"class":241},[52,43118,193],{"class":102},[52,43120,247],{"class":189},[52,43122,186],{"class":102},[52,43124,190],{"class":189},[52,43126,193],{"class":102},[52,43128,115],{"class":83},[52,43130,2147],{"class":87},[52,43132,115],{"class":83},[52,43134,8886],{"class":102},[52,43136,43137,43139,43141,43143,43145,43147],{"class":54,"line":164},[52,43138,242],{"class":241},[52,43140,193],{"class":102},[52,43142,4198],{"class":4733},[52,43144,193],{"class":102},[52,43146,247],{"class":189},[52,43148,8886],{"class":102},[14,43150,43151],{},[5503,43152,5267],{},[42,43154,43156],{"className":44,"code":43155,"language":46,"meta":47,"style":47},"Alice\nAlice\n\u003Cclass 'dict'>\n",[49,43157,43158,43162,43166],{"__ignoreMap":47},[52,43159,43160],{"class":54,"line":55},[52,43161,19253],{"class":62},[52,43163,43164],{"class":54,"line":66},[52,43165,19253],{"class":62},[52,43167,43168,43170,43172,43174,43176,43178],{"class":54,"line":73},[52,43169,15294],{"class":79},[52,43171,6994],{"class":1475},[52,43173,4172],{"class":83},[52,43175,40992],{"class":87},[52,43177,4162],{"class":83},[52,43179,15306],{"class":79},[639,43181,43183,41833,43185],{"id":43182},"dataname-vs-datagetname",[49,43184,41205],{},[49,43186,43187],{},"data.get(\"name\")",[309,43189,43190,43198],{},[24,43191,43192,43194,43195],{},[49,43193,41205],{}," gets the value for the key ",[49,43196,43197],{},"\"name\"",[24,43199,43200,43202],{},[49,43201,43187],{}," also gets the value, but is safer if the key might be missing",[14,43204,4259],{},[42,43206,43208],{"className":44,"code":43207,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"name\": \"Alice\"}'\ndata = json.loads(json_text)\n\nprint(data.get(\"age\"))\n",[49,43209,43210,43216,43220,43232,43250,43254],{"__ignoreMap":47},[52,43211,43212,43214],{"class":54,"line":55},[52,43213,59],{"class":58},[52,43215,40454],{"class":62},[52,43217,43218],{"class":54,"line":66},[52,43219,70],{"emptyLinePlaceholder":69},[52,43221,43222,43224,43226,43228,43230],{"class":54,"line":73},[52,43223,40463],{"class":62},[52,43225,80],{"class":79},[52,43227,4172],{"class":83},[52,43229,41269],{"class":87},[52,43231,24871],{"class":83},[52,43233,43234,43236,43238,43240,43242,43244,43246,43248],{"class":54,"line":94},[52,43235,217],{"class":62},[52,43237,80],{"class":79},[52,43239,2204],{"class":62},[52,43241,186],{"class":102},[52,43243,40489],{"class":189},[52,43245,193],{"class":102},[52,43247,40494],{"class":189},[52,43249,211],{"class":102},[52,43251,43252],{"class":54,"line":106},[52,43253,70],{"emptyLinePlaceholder":69},[52,43255,43256,43258,43260,43262,43264,43266,43268,43270,43272,43274],{"class":54,"line":128},[52,43257,242],{"class":241},[52,43259,193],{"class":102},[52,43261,247],{"class":189},[52,43263,186],{"class":102},[52,43265,190],{"class":189},[52,43267,193],{"class":102},[52,43269,115],{"class":83},[52,43271,2167],{"class":87},[52,43273,115],{"class":83},[52,43275,8886],{"class":102},[14,43277,43278],{},[5503,43279,5267],{},[42,43281,43282],{"className":44,"code":19440,"language":46,"meta":47,"style":47},[49,43283,43284],{"__ignoreMap":47},[52,43285,43286],{"class":54,"line":55},[52,43287,19440],{"class":160},[14,43289,6446,43290,43293,43294,186],{},[49,43291,43292],{},"data[\"age\"]"," and the key does not exist, Python raises an error. See ",[372,43295,3984,43296,43298],{"href":41230},[49,43297,1701],{}," when accessing dictionary values",[37,43300,43302],{"id":43301},"example-nested-json","Example: nested JSON",[14,43304,43305],{},"JSON often contains objects inside objects, or lists inside objects.",[42,43307,43309],{"className":44,"code":43308,"language":46,"meta":47,"style":47},"import json\n\njson_text = \"\"\"\n{\n    \"user\": {\n        \"name\": \"Alice\",\n        \"contact\": {\n            \"email\": \"alice@example.com\"\n        }\n    },\n    \"skills\": [\"Python\", \"SQL\"]\n}\n\"\"\"\n\ndata = json.loads(json_text)\n\nprint(data[\"user\"][\"name\"])\nprint(data[\"user\"][\"contact\"][\"email\"])\nprint(data[\"skills\"][0])\n",[49,43310,43311,43317,43321,43329,43333,43338,43343,43348,43353,43358,43362,43367,43371,43375,43379,43397,43401,43427,43462],{"__ignoreMap":47},[52,43312,43313,43315],{"class":54,"line":55},[52,43314,59],{"class":58},[52,43316,40454],{"class":62},[52,43318,43319],{"class":54,"line":66},[52,43320,70],{"emptyLinePlaceholder":69},[52,43322,43323,43325,43327],{"class":54,"line":73},[52,43324,40463],{"class":62},[52,43326,80],{"class":79},[52,43328,40640],{"class":83},[52,43330,43331],{"class":54,"line":94},[52,43332,980],{"class":87},[52,43334,43335],{"class":54,"line":106},[52,43336,43337],{"class":87},"    \"user\": {\n",[52,43339,43340],{"class":54,"line":128},[52,43341,43342],{"class":87},"        \"name\": \"Alice\",\n",[52,43344,43345],{"class":54,"line":148},[52,43346,43347],{"class":87},"        \"contact\": {\n",[52,43349,43350],{"class":54,"line":164},[52,43351,43352],{"class":87},"            \"email\": \"alice@example.com\"\n",[52,43354,43355],{"class":54,"line":170},[52,43356,43357],{"class":87},"        }\n",[52,43359,43360],{"class":54,"line":175},[52,43361,2649],{"class":87},[52,43363,43364],{"class":54,"line":214},[52,43365,43366],{"class":87},"    \"skills\": [\"Python\", \"SQL\"]\n",[52,43368,43369],{"class":54,"line":233},[52,43370,167],{"class":87},[52,43372,43373],{"class":54,"line":238},[52,43374,40687],{"class":83},[52,43376,43377],{"class":54,"line":272},[52,43378,70],{"emptyLinePlaceholder":69},[52,43380,43381,43383,43385,43387,43389,43391,43393,43395],{"class":54,"line":1348},[52,43382,217],{"class":62},[52,43384,80],{"class":79},[52,43386,2204],{"class":62},[52,43388,186],{"class":102},[52,43390,40489],{"class":189},[52,43392,193],{"class":102},[52,43394,40494],{"class":189},[52,43396,211],{"class":102},[52,43398,43399],{"class":54,"line":1376},[52,43400,70],{"emptyLinePlaceholder":69},[52,43402,43403,43405,43407,43409,43411,43413,43415,43417,43419,43421,43423,43425],{"class":54,"line":1381},[52,43404,242],{"class":241},[52,43406,193],{"class":102},[52,43408,247],{"class":189},[52,43410,250],{"class":102},[52,43412,115],{"class":83},[52,43414,42943],{"class":87},[52,43416,115],{"class":83},[52,43418,259],{"class":102},[52,43420,115],{"class":83},[52,43422,2147],{"class":87},[52,43424,115],{"class":83},[52,43426,269],{"class":102},[52,43428,43429,43431,43433,43435,43437,43439,43441,43443,43445,43447,43450,43452,43454,43456,43458,43460],{"class":54,"line":1406},[52,43430,242],{"class":241},[52,43432,193],{"class":102},[52,43434,247],{"class":189},[52,43436,250],{"class":102},[52,43438,115],{"class":83},[52,43440,42943],{"class":87},[52,43442,115],{"class":83},[52,43444,259],{"class":102},[52,43446,115],{"class":83},[52,43448,43449],{"class":87},"contact",[52,43451,115],{"class":83},[52,43453,259],{"class":102},[52,43455,115],{"class":83},[52,43457,12172],{"class":87},[52,43459,115],{"class":83},[52,43461,269],{"class":102},[52,43463,43464,43466,43468,43470,43472,43474,43476,43478,43480,43482],{"class":54,"line":1430},[52,43465,242],{"class":241},[52,43467,193],{"class":102},[52,43469,247],{"class":189},[52,43471,250],{"class":102},[52,43473,115],{"class":83},[52,43475,40543],{"class":87},[52,43477,115],{"class":83},[52,43479,259],{"class":102},[52,43481,13294],{"class":121},[52,43483,269],{"class":102},[14,43485,43486],{},[5503,43487,5267],{},[42,43489,43491],{"className":44,"code":43490,"language":46,"meta":47,"style":47},"Alice\nalice@example.com\nPython\n",[49,43492,43493,43497,43512],{"__ignoreMap":47},[52,43494,43495],{"class":54,"line":55},[52,43496,19253],{"class":62},[52,43498,43499,43502,43504,43507,43509],{"class":54,"line":66},[52,43500,43501],{"class":62},"alice",[52,43503,20971],{"class":79},[52,43505,43506],{"class":62},"example",[52,43508,186],{"class":102},[52,43510,43511],{"class":1320},"com\n",[52,43513,43514],{"class":54,"line":73},[52,43515,41189],{"class":62},[639,43517,43519],{"id":43518},"what-to-notice","What to notice",[309,43521,43522,43525,43527],{},[24,43523,43524],{},"Nested JSON objects become nested dictionaries",[24,43526,42774],{},[24,43528,43529],{},"You access nested values one step at a time",[14,43531,16291],{},[309,43533,43534,43540,43546],{},[24,43535,43536,43539],{},[49,43537,43538],{},"data[\"user\"]"," gives a dictionary",[24,43541,43542,43545],{},[49,43543,43544],{},"data[\"user\"][\"contact\"]"," gives another dictionary",[24,43547,43548,43551],{},[49,43549,43550],{},"data[\"skills\"]"," gives a list",[37,43553,43555],{"id":43554},"json-string-vs-json-file","JSON string vs JSON file",[14,43557,43558],{},"This is a common beginner confusion.",[309,43560,43561,43566,43571],{},[24,43562,3650,43563,43565],{},[49,43564,2932],{}," for a string",[24,43567,3650,43568,43570],{},[49,43569,40561],{}," for a file object",[24,43572,43573],{},"Do not mix the two functions",[639,43575,43577],{"id":43576},"json-string-example","JSON string example",[42,43579,43581],{"className":44,"code":43580,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"name\": \"Alice\"}'\ndata = json.loads(json_text)\n\nprint(data)\n",[49,43582,43583,43589,43593,43605,43623,43627],{"__ignoreMap":47},[52,43584,43585,43587],{"class":54,"line":55},[52,43586,59],{"class":58},[52,43588,40454],{"class":62},[52,43590,43591],{"class":54,"line":66},[52,43592,70],{"emptyLinePlaceholder":69},[52,43594,43595,43597,43599,43601,43603],{"class":54,"line":73},[52,43596,40463],{"class":62},[52,43598,80],{"class":79},[52,43600,4172],{"class":83},[52,43602,41269],{"class":87},[52,43604,24871],{"class":83},[52,43606,43607,43609,43611,43613,43615,43617,43619,43621],{"class":54,"line":94},[52,43608,217],{"class":62},[52,43610,80],{"class":79},[52,43612,2204],{"class":62},[52,43614,186],{"class":102},[52,43616,40489],{"class":189},[52,43618,193],{"class":102},[52,43620,40494],{"class":189},[52,43622,211],{"class":102},[52,43624,43625],{"class":54,"line":106},[52,43626,70],{"emptyLinePlaceholder":69},[52,43628,43629,43631,43633,43635],{"class":54,"line":128},[52,43630,242],{"class":241},[52,43632,193],{"class":102},[52,43634,247],{"class":189},[52,43636,211],{"class":102},[639,43638,43640],{"id":43639},"json-file-example","JSON file example",[14,43642,43643,43644,43646],{},"Suppose ",[49,43645,41774],{}," contains:",[42,43648,43651],{"className":43649,"code":43650,"language":227,"meta":47,"style":47},"language-json shiki shiki-themes material-theme-lighter github-light github-dark","{\"name\": \"Alice\"}\n",[49,43652,43653],{"__ignoreMap":47},[52,43654,43655,43657,43659,43662,43664,43666,43668,43670,43672],{"class":54,"line":55},[52,43656,1482],{"class":102},[52,43658,115],{"class":160},[52,43660,2147],{"class":43661},"sseR_",[52,43663,115],{"class":160},[52,43665,118],{"class":102},[52,43667,84],{"class":83},[52,43669,2156],{"class":87},[52,43671,115],{"class":83},[52,43673,167],{"class":102},[14,43675,43676],{},"Then you would read it like this:",[42,43678,43679],{"className":44,"code":41749,"language":46,"meta":47,"style":47},[49,43680,43681,43687,43691,43721,43739,43743],{"__ignoreMap":47},[52,43682,43683,43685],{"class":54,"line":55},[52,43684,59],{"class":58},[52,43686,40454],{"class":62},[52,43688,43689],{"class":54,"line":66},[52,43690,70],{"emptyLinePlaceholder":69},[52,43692,43693,43695,43697,43699,43701,43703,43705,43707,43709,43711,43713,43715,43717,43719],{"class":54,"line":73},[52,43694,14632],{"class":58},[52,43696,14635],{"class":241},[52,43698,193],{"class":102},[52,43700,115],{"class":83},[52,43702,41774],{"class":87},[52,43704,115],{"class":83},[52,43706,199],{"class":102},[52,43708,84],{"class":83},[52,43710,41783],{"class":87},[52,43712,115],{"class":83},[52,43714,1521],{"class":102},[52,43716,3567],{"class":58},[52,43718,14676],{"class":14675},[52,43720,1330],{"class":102},[52,43722,43723,43725,43727,43729,43731,43733,43735,43737],{"class":54,"line":94},[52,43724,1335],{"class":62},[52,43726,80],{"class":79},[52,43728,2204],{"class":62},[52,43730,186],{"class":102},[52,43732,41806],{"class":189},[52,43734,193],{"class":102},[52,43736,14698],{"class":14675},[52,43738,211],{"class":102},[52,43740,43741],{"class":54,"line":106},[52,43742,70],{"emptyLinePlaceholder":69},[52,43744,43745,43747,43749,43751],{"class":54,"line":128},[52,43746,242],{"class":241},[52,43748,193],{"class":102},[52,43750,247],{"class":189},[52,43752,211],{"class":102},[14,43754,43755,43756,759,43758,186],{},"If you want to learn that version in more detail, see ",[372,43757,41865],{"href":41864},[372,43759,861],{"href":860},[37,43761,6402],{"id":6401},[14,43763,43764],{},"These are some of the most common reasons JSON conversion fails.",[309,43766,43767,43770,43774,43779],{},[24,43768,43769],{},"Using single quotes inside JSON text",[24,43771,14067,43772],{},[49,43773,227],{},[24,43775,10294,43776,43778],{},[49,43777,40561],{}," on a plain string",[24,43780,43781],{},"Expecting every JSON value to become a dictionary",[639,43783,43785],{"id":43784},"_1-using-single-quotes-in-json","1. Using single quotes in JSON",[14,43787,41881],{},[42,43789,43791],{"className":44,"code":43790,"language":46,"meta":47,"style":47},"import json\n\njson_text = \"{'name': 'Alice'}\"\ndata = json.loads(json_text)\n",[49,43792,43793,43799,43803,43815],{"__ignoreMap":47},[52,43794,43795,43797],{"class":54,"line":55},[52,43796,59],{"class":58},[52,43798,40454],{"class":62},[52,43800,43801],{"class":54,"line":66},[52,43802,70],{"emptyLinePlaceholder":69},[52,43804,43805,43807,43809,43811,43813],{"class":54,"line":73},[52,43806,40463],{"class":62},[52,43808,80],{"class":79},[52,43810,84],{"class":83},[52,43812,41897],{"class":87},[52,43814,91],{"class":83},[52,43816,43817,43819,43821,43823,43825,43827,43829,43831],{"class":54,"line":94},[52,43818,217],{"class":62},[52,43820,80],{"class":79},[52,43822,2204],{"class":62},[52,43824,186],{"class":102},[52,43826,40489],{"class":189},[52,43828,193],{"class":102},[52,43830,40494],{"class":189},[52,43832,211],{"class":102},[14,43834,43835,43836,40698],{},"JSON requires ",[5503,43837,40697],{},[14,43839,12506],{},[42,43841,43842],{"className":44,"code":43580,"language":46,"meta":47,"style":47},[49,43843,43844,43850,43854,43866,43884,43888],{"__ignoreMap":47},[52,43845,43846,43848],{"class":54,"line":55},[52,43847,59],{"class":58},[52,43849,40454],{"class":62},[52,43851,43852],{"class":54,"line":66},[52,43853,70],{"emptyLinePlaceholder":69},[52,43855,43856,43858,43860,43862,43864],{"class":54,"line":73},[52,43857,40463],{"class":62},[52,43859,80],{"class":79},[52,43861,4172],{"class":83},[52,43863,41269],{"class":87},[52,43865,24871],{"class":83},[52,43867,43868,43870,43872,43874,43876,43878,43880,43882],{"class":54,"line":94},[52,43869,217],{"class":62},[52,43871,80],{"class":79},[52,43873,2204],{"class":62},[52,43875,186],{"class":102},[52,43877,40489],{"class":189},[52,43879,193],{"class":102},[52,43881,40494],{"class":189},[52,43883,211],{"class":102},[52,43885,43886],{"class":54,"line":106},[52,43887,70],{"emptyLinePlaceholder":69},[52,43889,43890,43892,43894,43896],{"class":54,"line":128},[52,43891,242],{"class":241},[52,43893,193],{"class":102},[52,43895,247],{"class":189},[52,43897,211],{"class":102},[639,43899,43901],{"id":43900},"_2-parsing-python-dictionary-syntax-as-json","2. Parsing Python dictionary syntax as JSON",[14,43903,43904],{},"Python dictionary syntax looks similar to JSON, but they are not the same thing.",[14,43906,43907],{},"This is Python data:",[42,43909,43911],{"className":44,"code":43910,"language":46,"meta":47,"style":47},"data = {\"name\": \"Alice\", \"is_admin\": False}\n",[49,43912,43913],{"__ignoreMap":47},[52,43914,43915,43917,43919,43921,43923,43925,43927,43929,43931,43933,43935,43937,43939,43941,43943,43945,43947],{"class":54,"line":55},[52,43916,217],{"class":62},[52,43918,80],{"class":79},[52,43920,3000],{"class":102},[52,43922,115],{"class":83},[52,43924,2147],{"class":87},[52,43926,115],{"class":83},[52,43928,118],{"class":102},[52,43930,84],{"class":83},[52,43932,2156],{"class":87},[52,43934,115],{"class":83},[52,43936,199],{"class":102},[52,43938,84],{"class":83},[52,43940,42475],{"class":87},[52,43942,115],{"class":83},[52,43944,118],{"class":102},[52,43946,42482],{"class":160},[52,43948,167],{"class":102},[14,43950,43951],{},"This is JSON text:",[42,43953,43955],{"className":44,"code":43954,"language":46,"meta":47,"style":47},"json_text = '{\"name\": \"Alice\", \"is_admin\": false}'\n",[49,43956,43957],{"__ignoreMap":47},[52,43958,43959,43961,43963,43965,43968],{"class":54,"line":55},[52,43960,40463],{"class":62},[52,43962,80],{"class":79},[52,43964,4172],{"class":83},[52,43966,43967],{"class":87},"{\"name\": \"Alice\", \"is_admin\": false}",[52,43969,24871],{"class":83},[14,43971,43972],{},"Notice the difference:",[309,43974,43975,43980],{},[24,43976,43977,43978],{},"Python uses ",[49,43979,6241],{},[24,43981,40694,43982],{},[49,43983,41432],{},[639,43985,43987],{"id":43986},"_3-using-the-wrong-function","3. Using the wrong function",[14,43989,43990,43991,43993],{},"This is wrong because ",[49,43992,40561],{}," expects a file object, not a string:",[42,43995,43997],{"className":44,"code":43996,"language":46,"meta":47,"style":47},"import json\n\njson_text = '{\"name\": \"Alice\"}'\ndata = json.load(json_text)\n",[49,43998,43999,44005,44009,44021],{"__ignoreMap":47},[52,44000,44001,44003],{"class":54,"line":55},[52,44002,59],{"class":58},[52,44004,40454],{"class":62},[52,44006,44007],{"class":54,"line":66},[52,44008,70],{"emptyLinePlaceholder":69},[52,44010,44011,44013,44015,44017,44019],{"class":54,"line":73},[52,44012,40463],{"class":62},[52,44014,80],{"class":79},[52,44016,4172],{"class":83},[52,44018,41269],{"class":87},[52,44020,24871],{"class":83},[52,44022,44023,44025,44027,44029,44031,44033,44035,44037],{"class":54,"line":94},[52,44024,217],{"class":62},[52,44026,80],{"class":79},[52,44028,2204],{"class":62},[52,44030,186],{"class":102},[52,44032,41806],{"class":189},[52,44034,193],{"class":102},[52,44036,40494],{"class":189},[52,44038,211],{"class":102},[14,44040,3650,44041,44043],{},[49,44042,2932],{}," for strings instead.",[639,44045,44047],{"id":44046},"_4-assuming-the-result-is-always-a-dictionary","4. Assuming the result is always a dictionary",[14,44049,44050],{},"If the top-level JSON value is an array, the result will be a list:",[42,44052,44054],{"className":44,"code":44053,"language":46,"meta":47,"style":47},"import json\n\njson_text = '[\"apple\", \"banana\", \"orange\"]'\ndata = json.loads(json_text)\n\nprint(data)\nprint(type(data))\n",[49,44055,44056,44062,44066,44079,44097,44101,44111],{"__ignoreMap":47},[52,44057,44058,44060],{"class":54,"line":55},[52,44059,59],{"class":58},[52,44061,40454],{"class":62},[52,44063,44064],{"class":54,"line":66},[52,44065,70],{"emptyLinePlaceholder":69},[52,44067,44068,44070,44072,44074,44077],{"class":54,"line":73},[52,44069,40463],{"class":62},[52,44071,80],{"class":79},[52,44073,4172],{"class":83},[52,44075,44076],{"class":87},"[\"apple\", \"banana\", \"orange\"]",[52,44078,24871],{"class":83},[52,44080,44081,44083,44085,44087,44089,44091,44093,44095],{"class":54,"line":94},[52,44082,217],{"class":62},[52,44084,80],{"class":79},[52,44086,2204],{"class":62},[52,44088,186],{"class":102},[52,44090,40489],{"class":189},[52,44092,193],{"class":102},[52,44094,40494],{"class":189},[52,44096,211],{"class":102},[52,44098,44099],{"class":54,"line":106},[52,44100,70],{"emptyLinePlaceholder":69},[52,44102,44103,44105,44107,44109],{"class":54,"line":128},[52,44104,242],{"class":241},[52,44106,193],{"class":102},[52,44108,247],{"class":189},[52,44110,211],{"class":102},[52,44112,44113,44115,44117,44119,44121,44123],{"class":54,"line":148},[52,44114,242],{"class":241},[52,44116,193],{"class":102},[52,44118,4198],{"class":4733},[52,44120,193],{"class":102},[52,44122,247],{"class":189},[52,44124,8886],{"class":102},[14,44126,44127],{},[5503,44128,5267],{},[42,44130,44132],{"className":44,"code":44131,"language":46,"meta":47,"style":47},"['apple', 'banana', 'orange']\n\u003Cclass 'list'>\n",[49,44133,44134,44162],{"__ignoreMap":47},[52,44135,44136,44138,44140,44142,44144,44146,44148,44150,44152,44154,44156,44158,44160],{"class":54,"line":55},[52,44137,250],{"class":102},[52,44139,4162],{"class":83},[52,44141,33028],{"class":87},[52,44143,4162],{"class":83},[52,44145,199],{"class":102},[52,44147,4172],{"class":83},[52,44149,32978],{"class":87},[52,44151,4162],{"class":83},[52,44153,199],{"class":102},[52,44155,4172],{"class":83},[52,44157,32996],{"class":87},[52,44159,4162],{"class":83},[52,44161,1147],{"class":102},[52,44163,44164,44166,44168,44170,44172,44174],{"class":54,"line":66},[52,44165,15294],{"class":79},[52,44167,6994],{"class":1475},[52,44169,4172],{"class":83},[52,44171,15301],{"class":87},[52,44173,4162],{"class":83},[52,44175,15306],{"class":79},[37,44177,1942],{"id":1941},[639,44179,44181],{"id":44180},"what-function-converts-json-to-a-dictionary-in-python","What function converts JSON to a dictionary in Python?",[14,44183,3650,44184,44186],{},[49,44185,2932],{}," when you have JSON as a string. If the JSON root is an object, the result is a Python dictionary.",[639,44188,42309],{"id":42193},[14,44190,44191,42202,44193,42205],{},[49,44192,40561],{},[49,44194,2932],{},[639,44196,44198],{"id":44197},"does-json-always-become-a-dictionary","Does JSON always become a dictionary?",[14,44200,44201],{},"No. A JSON object becomes a dictionary, but a JSON array becomes a list.",[639,44203,44205],{"id":44204},"why-does-my-json-conversion-fail","Why does my JSON conversion fail?",[14,44207,44208],{},"The most common reason is invalid JSON format, especially single quotes, missing commas, or trailing commas.",[37,44210,2005],{"id":2004},[309,44212,44213,44217,44221,44225,44229,44233],{},[24,44214,44215],{},[372,44216,861],{"href":860},[24,44218,44219],{},[372,44220,42247],{"href":2929},[24,44222,44223],{},[372,44224,42252],{"href":41864},[24,44226,44227],{},[372,44228,2022],{"href":855},[24,44230,44231],{},[372,44232,42262],{"href":42261},[24,44234,44235],{},[372,44236,25225,44237,43298],{"href":41230},[49,44238,1701],{},[2034,44240,44241],{},"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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sseR_, html code.shiki .sseR_{--shiki-light:#9C3EDA;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":44243},[44244,44245,44246,44247,44250,44251,44255,44258,44262,44268,44274],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":37779,"depth":66,"text":37780},{"id":42545,"depth":66,"text":42546,"children":44248},[44249],{"id":24094,"depth":73,"text":24095},{"id":42759,"depth":66,"text":42760},{"id":43040,"depth":66,"text":43041,"children":44252},[44253],{"id":43182,"depth":73,"text":44254},"data[\"name\"] vs data.get(\"name\")",{"id":43301,"depth":66,"text":43302,"children":44256},[44257],{"id":43518,"depth":73,"text":43519},{"id":43554,"depth":66,"text":43555,"children":44259},[44260,44261],{"id":43576,"depth":73,"text":43577},{"id":43639,"depth":73,"text":43640},{"id":6401,"depth":66,"text":6402,"children":44263},[44264,44265,44266,44267],{"id":43784,"depth":73,"text":43785},{"id":43900,"depth":73,"text":43901},{"id":43986,"depth":73,"text":43987},{"id":44046,"depth":73,"text":44047},{"id":1941,"depth":66,"text":1942,"children":44269},[44270,44271,44272,44273],{"id":44180,"depth":73,"text":44181},{"id":42193,"depth":73,"text":42309},{"id":44197,"depth":73,"text":44198},{"id":44204,"depth":73,"text":44205},{"id":2004,"depth":66,"text":2005},"Master python json to dictionary example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-json-to-dictionary-example",{"title":42322,"description":44275},"examples\u002Fpython-json-to-dictionary-example","YQFGEJWwpY8JsySzOEeJ29uZmydeKUZRKaQyrxjVQEU",{"id":44282,"title":44283,"body":44284,"description":46066,"extension":2075,"meta":46067,"navigation":69,"path":46068,"seo":46069,"stem":46070,"__hash__":46071},"content\u002Fexamples\u002Fpython-list-processing-example.md","Python List Processing Example",{"type":7,"value":44285,"toc":46048},[44286,44289,44292,44294,44314,44317,44319,44552,44555,44557,44560,44577,44584,44588,44591,44624,44627,44651,44660,44664,44669,44728,44730,44757,44759,44777,44784,44789,44858,44862,44865,44868,44970,44972,44989,44992,45015,45018,45032,45038,45042,45045,45050,45140,45142,45174,45179,45182,45186,45189,45192,45216,45218,45347,45349,45392,45395,45411,45413,45416,45625,45627,45713,45716,45730,45734,45737,45762,45765,45791,45793,45820,45826,45840,45842,45861,45864,45881,45893,45896,45959,45961,45975,45977,45981,45984,45988,45991,45995,45998,46002,46008,46010,46037,46045],[10,44287,44283],{"id":44288},"python-list-processing-example",[14,44290,44291],{},"This beginner-friendly example shows how to process a Python list step by step.",[14,44293,6959],{},[309,44295,44296,44299,44302,44305],{},[24,44297,44298],{},"loop through a list",[24,44300,44301],{},"filter items based on a condition",[24,44303,44304],{},"create a new list with changed values",[24,44306,44307,44308,759,44311],{},"summarize list data with functions like ",[49,44309,44310],{},"sum()",[49,44312,44313],{},"len()",[14,44315,44316],{},"The goal is to practice real list processing in one small script, without getting distracted by too much theory.",[37,44318,40],{"id":39},[42,44320,44322],{"className":44,"code":44321,"language":46,"meta":47,"style":47},"numbers = [3, 7, 2, 8, 5]\n\n# keep only even numbers\nevens = []\nfor n in numbers:\n    if n % 2 == 0:\n        evens.append(n)\n\n# make a new list with doubled values\ndoubled = []\nfor n in numbers:\n    doubled.append(n * 2)\n\nprint(\"original:\", numbers)\nprint(\"evens:\", evens)\nprint(\"doubled:\", doubled)\nprint(\"sum:\", sum(numbers))\n",[49,44323,44324,44353,44357,44362,44371,44383,44399,44415,44419,44424,44433,44445,44465,44469,44488,44508,44528],{"__ignoreMap":47},[52,44325,44326,44328,44330,44332,44334,44336,44339,44341,44343,44345,44347,44349,44351],{"class":54,"line":55},[52,44327,32092],{"class":62},[52,44329,80],{"class":79},[52,44331,18162],{"class":102},[52,44333,7508],{"class":121},[52,44335,199],{"class":102},[52,44337,44338],{"class":121}," 7",[52,44340,199],{"class":102},[52,44342,9168],{"class":121},[52,44344,199],{"class":102},[52,44346,32757],{"class":121},[52,44348,199],{"class":102},[52,44350,7646],{"class":121},[52,44352,1147],{"class":102},[52,44354,44355],{"class":54,"line":66},[52,44356,70],{"emptyLinePlaceholder":69},[52,44358,44359],{"class":54,"line":73},[52,44360,44361],{"class":4368},"# keep only even numbers\n",[52,44363,44364,44367,44369],{"class":54,"line":94},[52,44365,44366],{"class":62},"evens ",[52,44368,80],{"class":79},[52,44370,18590],{"class":102},[52,44372,44373,44375,44377,44379,44381],{"class":54,"line":106},[52,44374,12265],{"class":58},[52,44376,32139],{"class":62},[52,44378,11213],{"class":58},[52,44380,32302],{"class":62},[52,44382,1330],{"class":102},[52,44384,44385,44387,44389,44391,44393,44395,44397],{"class":54,"line":128},[52,44386,1438],{"class":58},[52,44388,32139],{"class":62},[52,44390,32151],{"class":79},[52,44392,9168],{"class":121},[52,44394,1324],{"class":79},[52,44396,13049],{"class":121},[52,44398,1330],{"class":102},[52,44400,44401,44404,44406,44408,44410,44413],{"class":54,"line":148},[52,44402,44403],{"class":62},"        evens",[52,44405,186],{"class":102},[52,44407,18748],{"class":189},[52,44409,193],{"class":102},[52,44411,44412],{"class":189},"n",[52,44414,211],{"class":102},[52,44416,44417],{"class":54,"line":164},[52,44418,70],{"emptyLinePlaceholder":69},[52,44420,44421],{"class":54,"line":170},[52,44422,44423],{"class":4368},"# make a new list with doubled values\n",[52,44425,44426,44429,44431],{"class":54,"line":175},[52,44427,44428],{"class":62},"doubled ",[52,44430,80],{"class":79},[52,44432,18590],{"class":102},[52,44434,44435,44437,44439,44441,44443],{"class":54,"line":214},[52,44436,12265],{"class":58},[52,44438,32139],{"class":62},[52,44440,11213],{"class":58},[52,44442,32302],{"class":62},[52,44444,1330],{"class":102},[52,44446,44447,44450,44452,44454,44456,44458,44461,44463],{"class":54,"line":233},[52,44448,44449],{"class":62},"    doubled",[52,44451,186],{"class":102},[52,44453,18748],{"class":189},[52,44455,193],{"class":102},[52,44457,32134],{"class":189},[52,44459,44460],{"class":79},"*",[52,44462,9168],{"class":121},[52,44464,211],{"class":102},[52,44466,44467],{"class":54,"line":238},[52,44468,70],{"emptyLinePlaceholder":69},[52,44470,44471,44473,44475,44477,44480,44482,44484,44486],{"class":54,"line":272},[52,44472,242],{"class":241},[52,44474,193],{"class":102},[52,44476,115],{"class":83},[52,44478,44479],{"class":87},"original:",[52,44481,115],{"class":83},[52,44483,199],{"class":102},[52,44485,32302],{"class":189},[52,44487,211],{"class":102},[52,44489,44490,44492,44494,44496,44499,44501,44503,44506],{"class":54,"line":1348},[52,44491,242],{"class":241},[52,44493,193],{"class":102},[52,44495,115],{"class":83},[52,44497,44498],{"class":87},"evens:",[52,44500,115],{"class":83},[52,44502,199],{"class":102},[52,44504,44505],{"class":189}," evens",[52,44507,211],{"class":102},[52,44509,44510,44512,44514,44516,44519,44521,44523,44526],{"class":54,"line":1376},[52,44511,242],{"class":241},[52,44513,193],{"class":102},[52,44515,115],{"class":83},[52,44517,44518],{"class":87},"doubled:",[52,44520,115],{"class":83},[52,44522,199],{"class":102},[52,44524,44525],{"class":189}," doubled",[52,44527,211],{"class":102},[52,44529,44530,44532,44534,44536,44539,44541,44543,44546,44548,44550],{"class":54,"line":1381},[52,44531,242],{"class":241},[52,44533,193],{"class":102},[52,44535,115],{"class":83},[52,44537,44538],{"class":87},"sum:",[52,44540,115],{"class":83},[52,44542,199],{"class":102},[52,44544,44545],{"class":241}," sum",[52,44547,193],{"class":102},[52,44549,5324],{"class":189},[52,44551,8886],{"class":102},[14,44553,44554],{},"Use this as a simple list processing pattern: loop through the list, check each item, and build a new result list when needed.",[37,44556,7216],{"id":7215},[14,44558,44559],{},"This example focuses on practical list work:",[309,44561,44562,44565,44568,44571,44574],{},[24,44563,44564],{},"It shows how to work through a list one item at a time.",[24,44566,44567],{},"It demonstrates filtering values based on a condition.",[24,44569,44570],{},"It demonstrates creating a new list from existing values.",[24,44572,44573],{},"It shows simple summary operations like total and count.",[24,44575,44576],{},"It keeps the focus on practical list processing, not list theory.",[14,44578,44579,44580,186],{},"If you are new to lists, see ",[372,44581,44583],{"href":44582},"\u002Flearn\u002Fpython-lists-explained-beginner-guide","Python lists explained for beginners",[37,44585,44587],{"id":44586},"the-example-input-list","The example input list",[14,44589,44590],{},"We will use a small list of numbers so the result is easy to follow:",[42,44592,44594],{"className":44,"code":44593,"language":46,"meta":47,"style":47},"numbers = [3, 7, 2, 8, 5]\n",[49,44595,44596],{"__ignoreMap":47},[52,44597,44598,44600,44602,44604,44606,44608,44610,44612,44614,44616,44618,44620,44622],{"class":54,"line":55},[52,44599,32092],{"class":62},[52,44601,80],{"class":79},[52,44603,18162],{"class":102},[52,44605,7508],{"class":121},[52,44607,199],{"class":102},[52,44609,44338],{"class":121},[52,44611,199],{"class":102},[52,44613,9168],{"class":121},[52,44615,199],{"class":102},[52,44617,32757],{"class":121},[52,44619,199],{"class":102},[52,44621,7646],{"class":121},[52,44623,1147],{"class":102},[14,44625,44626],{},"This list contains five items. Python will process them in order:",[21,44628,44629,44633,44638,44642,44647],{},[24,44630,44631],{},[49,44632,7508],{},[24,44634,44635],{},[49,44636,44637],{},"7",[24,44639,44640],{},[49,44641,9825],{},[24,44643,44644],{},[49,44645,44646],{},"8",[24,44648,44649],{},[49,44650,9479],{},[14,44652,44653,44654,44656,44657,44659],{},"A useful beginner idea: the original list does ",[5503,44655,20858],{}," change unless you modify it directly. In this example, we read from ",[49,44658,5324],{}," and build new lists instead.",[37,44661,44663],{"id":44662},"looping-through-the-list","Looping through the list",[14,44665,3233,44666,44668],{},[49,44667,12265],{}," loop lets you read one item at a time:",[42,44670,44672],{"className":44,"code":44671,"language":46,"meta":47,"style":47},"numbers = [3, 7, 2, 8, 5]\n\nfor n in numbers:\n    print(n)\n",[49,44673,44674,44702,44706,44718],{"__ignoreMap":47},[52,44675,44676,44678,44680,44682,44684,44686,44688,44690,44692,44694,44696,44698,44700],{"class":54,"line":55},[52,44677,32092],{"class":62},[52,44679,80],{"class":79},[52,44681,18162],{"class":102},[52,44683,7508],{"class":121},[52,44685,199],{"class":102},[52,44687,44338],{"class":121},[52,44689,199],{"class":102},[52,44691,9168],{"class":121},[52,44693,199],{"class":102},[52,44695,32757],{"class":121},[52,44697,199],{"class":102},[52,44699,7646],{"class":121},[52,44701,1147],{"class":102},[52,44703,44704],{"class":54,"line":66},[52,44705,70],{"emptyLinePlaceholder":69},[52,44707,44708,44710,44712,44714,44716],{"class":54,"line":73},[52,44709,12265],{"class":58},[52,44711,32139],{"class":62},[52,44713,11213],{"class":58},[52,44715,32302],{"class":62},[52,44717,1330],{"class":102},[52,44719,44720,44722,44724,44726],{"class":54,"line":94},[52,44721,1599],{"class":241},[52,44723,193],{"class":102},[52,44725,44412],{"class":189},[52,44727,211],{"class":102},[14,44729,9266],{},[42,44731,44733],{"className":44,"code":44732,"language":46,"meta":47,"style":47},"3\n7\n2\n8\n5\n",[49,44734,44735,44739,44744,44749,44753],{"__ignoreMap":47},[52,44736,44737],{"class":54,"line":55},[52,44738,7184],{"class":121},[52,44740,44741],{"class":54,"line":66},[52,44742,44743],{"class":121},"7\n",[52,44745,44746],{"class":54,"line":73},[52,44747,44748],{"class":121},"2\n",[52,44750,44751],{"class":54,"line":94},[52,44752,5271],{"class":121},[52,44754,44755],{"class":54,"line":106},[52,44756,8819],{"class":121},[14,44758,22954],{},[309,44760,44761,44766,44771],{},[24,44762,44763,44765],{},[49,44764,5324],{}," is the list",[24,44767,44768,44770],{},[49,44769,44412],{}," is the loop variable",[24,44772,44773,44774,44776],{},"on each loop, ",[49,44775,44412],{}," holds one value from the list",[14,44778,44779,44780,186],{},"If you want more practice with this step, read ",[372,44781,44783],{"href":44782},"\u002Fhow-to\u002Fhow-to-loop-through-a-list-in-python","how to loop through a list in Python",[14,44785,44786,44787,118],{},"For beginners, it can help to trace the loop with ",[49,44788,9687],{},[42,44790,44792],{"className":44,"code":44791,"language":46,"meta":47,"style":47},"numbers = [3, 7, 2, 8, 5]\n\nfor n in numbers:\n    print(\"current number:\", n)\n",[49,44793,44794,44822,44826,44838],{"__ignoreMap":47},[52,44795,44796,44798,44800,44802,44804,44806,44808,44810,44812,44814,44816,44818,44820],{"class":54,"line":55},[52,44797,32092],{"class":62},[52,44799,80],{"class":79},[52,44801,18162],{"class":102},[52,44803,7508],{"class":121},[52,44805,199],{"class":102},[52,44807,44338],{"class":121},[52,44809,199],{"class":102},[52,44811,9168],{"class":121},[52,44813,199],{"class":102},[52,44815,32757],{"class":121},[52,44817,199],{"class":102},[52,44819,7646],{"class":121},[52,44821,1147],{"class":102},[52,44823,44824],{"class":54,"line":66},[52,44825,70],{"emptyLinePlaceholder":69},[52,44827,44828,44830,44832,44834,44836],{"class":54,"line":73},[52,44829,12265],{"class":58},[52,44831,32139],{"class":62},[52,44833,11213],{"class":58},[52,44835,32302],{"class":62},[52,44837,1330],{"class":102},[52,44839,44840,44842,44844,44846,44849,44851,44853,44856],{"class":54,"line":94},[52,44841,1599],{"class":241},[52,44843,193],{"class":102},[52,44845,115],{"class":83},[52,44847,44848],{"class":87},"current number:",[52,44850,115],{"class":83},[52,44852,199],{"class":102},[52,44854,44855],{"class":189}," n",[52,44857,211],{"class":102},[37,44859,44861],{"id":44860},"filtering-items","Filtering items",[14,44863,44864],{},"Filtering means keeping only the items that match a rule.",[14,44866,44867],{},"In this example, the rule is: keep only even numbers.",[42,44869,44871],{"className":44,"code":44870,"language":46,"meta":47,"style":47},"numbers = [3, 7, 2, 8, 5]\nevens = []\n\nfor n in numbers:\n    if n % 2 == 0:\n        evens.append(n)\n\nprint(evens)\n",[49,44872,44873,44901,44909,44913,44925,44941,44955,44959],{"__ignoreMap":47},[52,44874,44875,44877,44879,44881,44883,44885,44887,44889,44891,44893,44895,44897,44899],{"class":54,"line":55},[52,44876,32092],{"class":62},[52,44878,80],{"class":79},[52,44880,18162],{"class":102},[52,44882,7508],{"class":121},[52,44884,199],{"class":102},[52,44886,44338],{"class":121},[52,44888,199],{"class":102},[52,44890,9168],{"class":121},[52,44892,199],{"class":102},[52,44894,32757],{"class":121},[52,44896,199],{"class":102},[52,44898,7646],{"class":121},[52,44900,1147],{"class":102},[52,44902,44903,44905,44907],{"class":54,"line":66},[52,44904,44366],{"class":62},[52,44906,80],{"class":79},[52,44908,18590],{"class":102},[52,44910,44911],{"class":54,"line":73},[52,44912,70],{"emptyLinePlaceholder":69},[52,44914,44915,44917,44919,44921,44923],{"class":54,"line":94},[52,44916,12265],{"class":58},[52,44918,32139],{"class":62},[52,44920,11213],{"class":58},[52,44922,32302],{"class":62},[52,44924,1330],{"class":102},[52,44926,44927,44929,44931,44933,44935,44937,44939],{"class":54,"line":106},[52,44928,1438],{"class":58},[52,44930,32139],{"class":62},[52,44932,32151],{"class":79},[52,44934,9168],{"class":121},[52,44936,1324],{"class":79},[52,44938,13049],{"class":121},[52,44940,1330],{"class":102},[52,44942,44943,44945,44947,44949,44951,44953],{"class":54,"line":128},[52,44944,44403],{"class":62},[52,44946,186],{"class":102},[52,44948,18748],{"class":189},[52,44950,193],{"class":102},[52,44952,44412],{"class":189},[52,44954,211],{"class":102},[52,44956,44957],{"class":54,"line":148},[52,44958,70],{"emptyLinePlaceholder":69},[52,44960,44961,44963,44965,44968],{"class":54,"line":164},[52,44962,242],{"class":241},[52,44964,193],{"class":102},[52,44966,44967],{"class":189},"evens",[52,44969,211],{"class":102},[14,44971,9266],{},[42,44973,44975],{"className":44,"code":44974,"language":46,"meta":47,"style":47},"[2, 8]\n",[49,44976,44977],{"__ignoreMap":47},[52,44978,44979,44981,44983,44985,44987],{"class":54,"line":55},[52,44980,250],{"class":102},[52,44982,9825],{"class":121},[52,44984,199],{"class":102},[52,44986,32757],{"class":121},[52,44988,1147],{"class":102},[14,44990,44991],{},"How it works:",[309,44993,44994,45000,45003,45009],{},[24,44995,44996,44999],{},[49,44997,44998],{},"evens = []"," creates an empty list",[24,45001,45002],{},"the loop reads each number",[24,45004,45005,45008],{},[49,45006,45007],{},"if n % 2 == 0"," checks whether the number is even",[24,45010,45011,45014],{},[49,45012,45013],{},"evens.append(n)"," adds matching values to the new list",[14,45016,45017],{},"This is a clear beginner pattern:",[21,45019,45020,45023,45026,45029],{},[24,45021,45022],{},"start with an empty list",[24,45024,45025],{},"loop through the original list",[24,45027,45028],{},"check a condition",[24,45030,45031],{},"append matching items",[14,45033,45034,45035,186],{},"For more filtering examples, see ",[372,45036,33629],{"href":45037},"\u002Fhow-to\u002Fhow-to-filter-a-list-in-python",[37,45039,45041],{"id":45040},"transforming-items","Transforming items",[14,45043,45044],{},"Transforming means creating a new list where each item is changed in some way.",[14,45046,45047,45048,118],{},"Here, we will multiply each number by ",[49,45049,9825],{},[42,45051,45053],{"className":44,"code":45052,"language":46,"meta":47,"style":47},"numbers = [3, 7, 2, 8, 5]\ndoubled = []\n\nfor n in numbers:\n    doubled.append(n * 2)\n\nprint(doubled)\n",[49,45054,45055,45083,45091,45095,45107,45125,45129],{"__ignoreMap":47},[52,45056,45057,45059,45061,45063,45065,45067,45069,45071,45073,45075,45077,45079,45081],{"class":54,"line":55},[52,45058,32092],{"class":62},[52,45060,80],{"class":79},[52,45062,18162],{"class":102},[52,45064,7508],{"class":121},[52,45066,199],{"class":102},[52,45068,44338],{"class":121},[52,45070,199],{"class":102},[52,45072,9168],{"class":121},[52,45074,199],{"class":102},[52,45076,32757],{"class":121},[52,45078,199],{"class":102},[52,45080,7646],{"class":121},[52,45082,1147],{"class":102},[52,45084,45085,45087,45089],{"class":54,"line":66},[52,45086,44428],{"class":62},[52,45088,80],{"class":79},[52,45090,18590],{"class":102},[52,45092,45093],{"class":54,"line":73},[52,45094,70],{"emptyLinePlaceholder":69},[52,45096,45097,45099,45101,45103,45105],{"class":54,"line":94},[52,45098,12265],{"class":58},[52,45100,32139],{"class":62},[52,45102,11213],{"class":58},[52,45104,32302],{"class":62},[52,45106,1330],{"class":102},[52,45108,45109,45111,45113,45115,45117,45119,45121,45123],{"class":54,"line":106},[52,45110,44449],{"class":62},[52,45112,186],{"class":102},[52,45114,18748],{"class":189},[52,45116,193],{"class":102},[52,45118,32134],{"class":189},[52,45120,44460],{"class":79},[52,45122,9168],{"class":121},[52,45124,211],{"class":102},[52,45126,45127],{"class":54,"line":128},[52,45128,70],{"emptyLinePlaceholder":69},[52,45130,45131,45133,45135,45138],{"class":54,"line":148},[52,45132,242],{"class":241},[52,45134,193],{"class":102},[52,45136,45137],{"class":189},"doubled",[52,45139,211],{"class":102},[14,45141,9266],{},[42,45143,45145],{"className":44,"code":45144,"language":46,"meta":47,"style":47},"[6, 14, 4, 16, 10]\n",[49,45146,45147],{"__ignoreMap":47},[52,45148,45149,45151,45154,45156,45159,45161,45163,45165,45168,45170,45172],{"class":54,"line":55},[52,45150,250],{"class":102},[52,45152,45153],{"class":121},"6",[52,45155,199],{"class":102},[52,45157,45158],{"class":121}," 14",[52,45160,199],{"class":102},[52,45162,32111],{"class":121},[52,45164,199],{"class":102},[52,45166,45167],{"class":121}," 16",[52,45169,199],{"class":102},[52,45171,27053],{"class":121},[52,45173,1147],{"class":102},[14,45175,45176,45177,186],{},"This does not change the original list. It creates a new list called ",[49,45178,45137],{},[14,45180,45181],{},"For beginners, building a new list is often easier to understand than changing the original list in place. It also makes debugging easier because you can compare the old list and the new list.",[37,45183,45185],{"id":45184},"summarizing-list-data","Summarizing list data",[14,45187,45188],{},"After processing a list, you often want a summary.",[14,45190,45191],{},"For numeric lists, common summary tools are:",[309,45193,45194,45199,45204,45210],{},[24,45195,45196,45198],{},[49,45197,44310],{}," for the total",[24,45200,45201,45203],{},[49,45202,44313],{}," for the number of items",[24,45205,45206,45209],{},[49,45207,45208],{},"min()"," for the smallest value",[24,45211,45212,45215],{},[49,45213,45214],{},"max()"," for the largest value",[14,45217,4259],{},[42,45219,45221],{"className":44,"code":45220,"language":46,"meta":47,"style":47},"numbers = [3, 7, 2, 8, 5]\n\nprint(\"sum:\", sum(numbers))\nprint(\"count:\", len(numbers))\nprint(\"smallest:\", min(numbers))\nprint(\"largest:\", max(numbers))\n",[49,45222,45223,45251,45255,45277,45300,45324],{"__ignoreMap":47},[52,45224,45225,45227,45229,45231,45233,45235,45237,45239,45241,45243,45245,45247,45249],{"class":54,"line":55},[52,45226,32092],{"class":62},[52,45228,80],{"class":79},[52,45230,18162],{"class":102},[52,45232,7508],{"class":121},[52,45234,199],{"class":102},[52,45236,44338],{"class":121},[52,45238,199],{"class":102},[52,45240,9168],{"class":121},[52,45242,199],{"class":102},[52,45244,32757],{"class":121},[52,45246,199],{"class":102},[52,45248,7646],{"class":121},[52,45250,1147],{"class":102},[52,45252,45253],{"class":54,"line":66},[52,45254,70],{"emptyLinePlaceholder":69},[52,45256,45257,45259,45261,45263,45265,45267,45269,45271,45273,45275],{"class":54,"line":73},[52,45258,242],{"class":241},[52,45260,193],{"class":102},[52,45262,115],{"class":83},[52,45264,44538],{"class":87},[52,45266,115],{"class":83},[52,45268,199],{"class":102},[52,45270,44545],{"class":241},[52,45272,193],{"class":102},[52,45274,5324],{"class":189},[52,45276,8886],{"class":102},[52,45278,45279,45281,45283,45285,45288,45290,45292,45294,45296,45298],{"class":54,"line":94},[52,45280,242],{"class":241},[52,45282,193],{"class":102},[52,45284,115],{"class":83},[52,45286,45287],{"class":87},"count:",[52,45289,115],{"class":83},[52,45291,199],{"class":102},[52,45293,9150],{"class":241},[52,45295,193],{"class":102},[52,45297,5324],{"class":189},[52,45299,8886],{"class":102},[52,45301,45302,45304,45306,45308,45311,45313,45315,45318,45320,45322],{"class":54,"line":106},[52,45303,242],{"class":241},[52,45305,193],{"class":102},[52,45307,115],{"class":83},[52,45309,45310],{"class":87},"smallest:",[52,45312,115],{"class":83},[52,45314,199],{"class":102},[52,45316,45317],{"class":241}," min",[52,45319,193],{"class":102},[52,45321,5324],{"class":189},[52,45323,8886],{"class":102},[52,45325,45326,45328,45330,45332,45335,45337,45339,45341,45343,45345],{"class":54,"line":128},[52,45327,242],{"class":241},[52,45329,193],{"class":102},[52,45331,115],{"class":83},[52,45333,45334],{"class":87},"largest:",[52,45336,115],{"class":83},[52,45338,199],{"class":102},[52,45340,22367],{"class":241},[52,45342,193],{"class":102},[52,45344,5324],{"class":189},[52,45346,8886],{"class":102},[14,45348,9266],{},[42,45350,45352],{"className":44,"code":45351,"language":46,"meta":47,"style":47},"sum: 25\ncount: 5\nsmallest: 2\nlargest: 8\n",[49,45353,45354,45363,45372,45382],{"__ignoreMap":47},[52,45355,45356,45359,45361],{"class":54,"line":55},[52,45357,45358],{"class":241},"sum",[52,45360,118],{"class":102},[52,45362,2174],{"class":121},[52,45364,45365,45368,45370],{"class":54,"line":66},[52,45366,45367],{"class":62},"count",[52,45369,118],{"class":102},[52,45371,5264],{"class":121},[52,45373,45374,45377,45379],{"class":54,"line":73},[52,45375,45376],{"class":62},"smallest",[52,45378,118],{"class":102},[52,45380,45381],{"class":121}," 2\n",[52,45383,45384,45387,45389],{"class":54,"line":94},[52,45385,45386],{"class":62},"largest",[52,45388,118],{"class":102},[52,45390,45391],{"class":121}," 8\n",[14,45393,45394],{},"If you want to understand these functions better, see:",[309,45396,45397,45404],{},[24,45398,45399],{},[372,45400,10646,45402,3711],{"href":45401},"\u002Freference\u002Fpython-sum-function-explained",[49,45403,44310],{},[24,45405,45406],{},[372,45407,10646,45409,3711],{"href":45408},"\u002Freference\u002Fpython-len-function-explained",[49,45410,44313],{},[37,45412,13563],{"id":13562},[14,45414,45415],{},"Here is the full example again:",[42,45417,45419],{"className":44,"code":45418,"language":46,"meta":47,"style":47},"numbers = [3, 7, 2, 8, 5]\n\nevens = []\nfor n in numbers:\n    if n % 2 == 0:\n        evens.append(n)\n\ndoubled = []\nfor n in numbers:\n    doubled.append(n * 2)\n\nprint(\"original:\", numbers)\nprint(\"evens:\", evens)\nprint(\"doubled:\", doubled)\nprint(\"sum:\", sum(numbers))\n",[49,45420,45421,45449,45453,45461,45473,45489,45503,45507,45515,45527,45545,45549,45567,45585,45603],{"__ignoreMap":47},[52,45422,45423,45425,45427,45429,45431,45433,45435,45437,45439,45441,45443,45445,45447],{"class":54,"line":55},[52,45424,32092],{"class":62},[52,45426,80],{"class":79},[52,45428,18162],{"class":102},[52,45430,7508],{"class":121},[52,45432,199],{"class":102},[52,45434,44338],{"class":121},[52,45436,199],{"class":102},[52,45438,9168],{"class":121},[52,45440,199],{"class":102},[52,45442,32757],{"class":121},[52,45444,199],{"class":102},[52,45446,7646],{"class":121},[52,45448,1147],{"class":102},[52,45450,45451],{"class":54,"line":66},[52,45452,70],{"emptyLinePlaceholder":69},[52,45454,45455,45457,45459],{"class":54,"line":73},[52,45456,44366],{"class":62},[52,45458,80],{"class":79},[52,45460,18590],{"class":102},[52,45462,45463,45465,45467,45469,45471],{"class":54,"line":94},[52,45464,12265],{"class":58},[52,45466,32139],{"class":62},[52,45468,11213],{"class":58},[52,45470,32302],{"class":62},[52,45472,1330],{"class":102},[52,45474,45475,45477,45479,45481,45483,45485,45487],{"class":54,"line":106},[52,45476,1438],{"class":58},[52,45478,32139],{"class":62},[52,45480,32151],{"class":79},[52,45482,9168],{"class":121},[52,45484,1324],{"class":79},[52,45486,13049],{"class":121},[52,45488,1330],{"class":102},[52,45490,45491,45493,45495,45497,45499,45501],{"class":54,"line":128},[52,45492,44403],{"class":62},[52,45494,186],{"class":102},[52,45496,18748],{"class":189},[52,45498,193],{"class":102},[52,45500,44412],{"class":189},[52,45502,211],{"class":102},[52,45504,45505],{"class":54,"line":148},[52,45506,70],{"emptyLinePlaceholder":69},[52,45508,45509,45511,45513],{"class":54,"line":164},[52,45510,44428],{"class":62},[52,45512,80],{"class":79},[52,45514,18590],{"class":102},[52,45516,45517,45519,45521,45523,45525],{"class":54,"line":170},[52,45518,12265],{"class":58},[52,45520,32139],{"class":62},[52,45522,11213],{"class":58},[52,45524,32302],{"class":62},[52,45526,1330],{"class":102},[52,45528,45529,45531,45533,45535,45537,45539,45541,45543],{"class":54,"line":175},[52,45530,44449],{"class":62},[52,45532,186],{"class":102},[52,45534,18748],{"class":189},[52,45536,193],{"class":102},[52,45538,32134],{"class":189},[52,45540,44460],{"class":79},[52,45542,9168],{"class":121},[52,45544,211],{"class":102},[52,45546,45547],{"class":54,"line":214},[52,45548,70],{"emptyLinePlaceholder":69},[52,45550,45551,45553,45555,45557,45559,45561,45563,45565],{"class":54,"line":233},[52,45552,242],{"class":241},[52,45554,193],{"class":102},[52,45556,115],{"class":83},[52,45558,44479],{"class":87},[52,45560,115],{"class":83},[52,45562,199],{"class":102},[52,45564,32302],{"class":189},[52,45566,211],{"class":102},[52,45568,45569,45571,45573,45575,45577,45579,45581,45583],{"class":54,"line":238},[52,45570,242],{"class":241},[52,45572,193],{"class":102},[52,45574,115],{"class":83},[52,45576,44498],{"class":87},[52,45578,115],{"class":83},[52,45580,199],{"class":102},[52,45582,44505],{"class":189},[52,45584,211],{"class":102},[52,45586,45587,45589,45591,45593,45595,45597,45599,45601],{"class":54,"line":272},[52,45588,242],{"class":241},[52,45590,193],{"class":102},[52,45592,115],{"class":83},[52,45594,44518],{"class":87},[52,45596,115],{"class":83},[52,45598,199],{"class":102},[52,45600,44525],{"class":189},[52,45602,211],{"class":102},[52,45604,45605,45607,45609,45611,45613,45615,45617,45619,45621,45623],{"class":54,"line":1348},[52,45606,242],{"class":241},[52,45608,193],{"class":102},[52,45610,115],{"class":83},[52,45612,44538],{"class":87},[52,45614,115],{"class":83},[52,45616,199],{"class":102},[52,45618,44545],{"class":241},[52,45620,193],{"class":102},[52,45622,5324],{"class":189},[52,45624,8886],{"class":102},[14,45626,9266],{},[42,45628,45630],{"className":44,"code":45629,"language":46,"meta":47,"style":47},"original: [3, 7, 2, 8, 5]\nevens: [2, 8]\ndoubled: [6, 14, 4, 16, 10]\nsum: 25\n",[49,45631,45632,45661,45677,45705],{"__ignoreMap":47},[52,45633,45634,45637,45639,45641,45643,45645,45647,45649,45651,45653,45655,45657,45659],{"class":54,"line":55},[52,45635,45636],{"class":62},"original",[52,45638,118],{"class":102},[52,45640,18162],{"class":102},[52,45642,7508],{"class":121},[52,45644,199],{"class":102},[52,45646,44338],{"class":121},[52,45648,199],{"class":102},[52,45650,9168],{"class":121},[52,45652,199],{"class":102},[52,45654,32757],{"class":121},[52,45656,199],{"class":102},[52,45658,7646],{"class":121},[52,45660,1147],{"class":102},[52,45662,45663,45665,45667,45669,45671,45673,45675],{"class":54,"line":66},[52,45664,44967],{"class":62},[52,45666,118],{"class":102},[52,45668,18162],{"class":102},[52,45670,9825],{"class":121},[52,45672,199],{"class":102},[52,45674,32757],{"class":121},[52,45676,1147],{"class":102},[52,45678,45679,45681,45683,45685,45687,45689,45691,45693,45695,45697,45699,45701,45703],{"class":54,"line":73},[52,45680,45137],{"class":62},[52,45682,118],{"class":102},[52,45684,18162],{"class":102},[52,45686,45153],{"class":121},[52,45688,199],{"class":102},[52,45690,45158],{"class":121},[52,45692,199],{"class":102},[52,45694,32111],{"class":121},[52,45696,199],{"class":102},[52,45698,45167],{"class":121},[52,45700,199],{"class":102},[52,45702,27053],{"class":121},[52,45704,1147],{"class":102},[52,45706,45707,45709,45711],{"class":54,"line":94},[52,45708,45358],{"class":241},[52,45710,118],{"class":102},[52,45712,2174],{"class":121},[14,45714,45715],{},"This output shows:",[309,45717,45718,45721,45724,45727],{},[24,45719,45720],{},"the original list",[24,45722,45723],{},"the filtered list of even numbers",[24,45725,45726],{},"the transformed list with doubled values",[24,45728,45729],{},"the total of the original numbers",[37,45731,45733],{"id":45732},"beginner-mistakes-to-watch-for","Beginner mistakes to watch for",[14,45735,45736],{},"These are common problems when processing lists:",[309,45738,45739,45749,45754,45757],{},[24,45740,45741,45742,45744,45745,857,45747],{},"Forgetting the colon ",[49,45743,118],{}," after ",[49,45746,12265],{},[49,45748,1313],{},[24,45750,10294,45751,45753],{},[49,45752,18748],{}," without parentheses",[24,45755,45756],{},"Changing the original list by accident",[24,45758,6496,45759,45761],{},[49,45760,44310],{}," on a list that contains non-numbers",[14,45763,45764],{},"Example of a missing colon:",[42,45766,45768],{"className":44,"code":45767,"language":46,"meta":47,"style":47},"for n in numbers\n    print(n)\n",[49,45769,45770,45781],{"__ignoreMap":47},[52,45771,45772,45774,45776,45778],{"class":54,"line":55},[52,45773,12265],{"class":58},[52,45775,32139],{"class":62},[52,45777,11213],{"class":58},[52,45779,45780],{"class":62}," numbers\n",[52,45782,45783,45785,45787,45789],{"class":54,"line":66},[52,45784,1599],{"class":241},[52,45786,193],{"class":102},[52,45788,44412],{"class":189},[52,45790,211],{"class":102},[14,45792,12506],{},[42,45794,45796],{"className":44,"code":45795,"language":46,"meta":47,"style":47},"for n in numbers:\n    print(n)\n",[49,45797,45798,45810],{"__ignoreMap":47},[52,45799,45800,45802,45804,45806,45808],{"class":54,"line":55},[52,45801,12265],{"class":58},[52,45803,32139],{"class":62},[52,45805,11213],{"class":58},[52,45807,32302],{"class":62},[52,45809,1330],{"class":102},[52,45811,45812,45814,45816,45818],{"class":54,"line":66},[52,45813,1599],{"class":241},[52,45815,193],{"class":102},[52,45817,44412],{"class":189},[52,45819,211],{"class":102},[14,45821,45822,45823,45825],{},"Example of incorrect ",[49,45824,18748],{}," use:",[42,45827,45829],{"className":44,"code":45828,"language":46,"meta":47,"style":47},"evens.append\n",[49,45830,45831],{"__ignoreMap":47},[52,45832,45833,45835,45837],{"class":54,"line":55},[52,45834,44967],{"class":62},[52,45836,186],{"class":102},[52,45838,45839],{"class":1320},"append\n",[14,45841,12506],{},[42,45843,45845],{"className":44,"code":45844,"language":46,"meta":47,"style":47},"evens.append(n)\n",[49,45846,45847],{"__ignoreMap":47},[52,45848,45849,45851,45853,45855,45857,45859],{"class":54,"line":55},[52,45850,44967],{"class":62},[52,45852,186],{"class":102},[52,45854,18748],{"class":189},[52,45856,193],{"class":102},[52,45858,44412],{"class":189},[52,45860,211],{"class":102},[14,45862,45863],{},"Other common causes of errors include:",[309,45865,45866,45869,45872,45875,45878],{},[24,45867,45868],{},"confusing filtering with modifying the original list",[24,45870,45871],{},"using the wrong indentation inside the loop",[24,45873,45874],{},"appending the wrong value to the result list",[24,45876,45877],{},"trying to use string methods on list items that are numbers",[24,45879,45880],{},"mixing strings and integers in summary operations",[14,45882,45883,45884,45888,45889,186],{},"If you get an index problem while testing lists, see ",[372,45885,45887],{"href":45886},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained","IndexError: list index out of range",". If you get a type-related loop problem, see ",[372,45890,45892],{"href":45891},"\u002Ferrors\u002Ftypeerror-int-object-is-not-iterable-fix","TypeError: int object is not iterable",[14,45894,45895],{},"Useful debug prints:",[42,45897,45899],{"className":44,"code":45898,"language":46,"meta":47,"style":47},"print(numbers)\nprint(n)\nprint(evens)\nprint(doubled)\nprint(type(numbers[0]))\n",[49,45900,45901,45911,45921,45931,45941],{"__ignoreMap":47},[52,45902,45903,45905,45907,45909],{"class":54,"line":55},[52,45904,242],{"class":241},[52,45906,193],{"class":102},[52,45908,5324],{"class":189},[52,45910,211],{"class":102},[52,45912,45913,45915,45917,45919],{"class":54,"line":66},[52,45914,242],{"class":241},[52,45916,193],{"class":102},[52,45918,44412],{"class":189},[52,45920,211],{"class":102},[52,45922,45923,45925,45927,45929],{"class":54,"line":73},[52,45924,242],{"class":241},[52,45926,193],{"class":102},[52,45928,44967],{"class":189},[52,45930,211],{"class":102},[52,45932,45933,45935,45937,45939],{"class":54,"line":94},[52,45934,242],{"class":241},[52,45936,193],{"class":102},[52,45938,45137],{"class":189},[52,45940,211],{"class":102},[52,45942,45943,45945,45947,45949,45951,45953,45955,45957],{"class":54,"line":106},[52,45944,242],{"class":241},[52,45946,193],{"class":102},[52,45948,4198],{"class":4733},[52,45950,193],{"class":102},[52,45952,5324],{"class":189},[52,45954,250],{"class":102},[52,45956,13294],{"class":121},[52,45958,20680],{"class":102},[14,45960,12298],{},[309,45962,45963,45966,45969,45972],{},[24,45964,45965],{},"what is inside the list",[24,45967,45968],{},"what the current loop value is",[24,45970,45971],{},"what has been added to your new lists",[24,45973,45974],{},"what data type you are working with",[37,45976,1942],{"id":1941},[639,45978,45980],{"id":45979},"what-does-list-processing-mean-in-python","What does list processing mean in Python?",[14,45982,45983],{},"It means reading items from a list and doing something with them, such as filtering, changing, or summarizing them.",[639,45985,45987],{"id":45986},"should-i-change-the-original-list-or-create-a-new-one","Should I change the original list or create a new one?",[14,45989,45990],{},"For beginners, creating a new list is often easier to understand and safer.",[639,45992,45994],{"id":45993},"can-i-do-this-with-strings-instead-of-numbers","Can I do this with strings instead of numbers?",[14,45996,45997],{},"Yes. You can loop through a list of strings and filter or change each item in the same way.",[639,45999,46001],{"id":46000},"is-this-the-same-as-a-list-comprehension","Is this the same as a list comprehension?",[14,46003,46004,46005,186],{},"Not exactly. A list comprehension is a shorter way to build a list, but the loop-based version is usually clearer when you are learning. After you understand this pattern, you can move on to ",[372,46006,32726],{"href":46007},"\u002Fhow-to\u002Fhow-to-use-list-comprehensions-in-python",[37,46009,2005],{"id":2004},[309,46011,46012,46016,46021,46025,46031],{},[24,46013,46014],{},[372,46015,44583],{"href":44582},[24,46017,46018],{},[372,46019,46020],{"href":44782},"How to loop through a list in Python",[24,46022,46023],{},[372,46024,34453],{"href":45037},[24,46026,46027],{},[372,46028,10646,46029,3711],{"href":45401},[49,46030,44310],{},[24,46032,46033],{},[372,46034,10646,46035,3711],{"href":45408},[49,46036,44313],{},[14,46038,46039,46040,759,46042,46044],{},"Try the same pattern with a list of names or prices next. Then move on to a filtering tutorial or a reference page for ",[49,46041,44310],{},[49,46043,44313],{}," to build your confidence.",[2034,46046,46047],{},"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 .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 .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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":46049},[46050,46051,46052,46053,46054,46055,46056,46057,46058,46059,46065],{"id":39,"depth":66,"text":40},{"id":7215,"depth":66,"text":7216},{"id":44586,"depth":66,"text":44587},{"id":44662,"depth":66,"text":44663},{"id":44860,"depth":66,"text":44861},{"id":45040,"depth":66,"text":45041},{"id":45184,"depth":66,"text":45185},{"id":13562,"depth":66,"text":13563},{"id":45732,"depth":66,"text":45733},{"id":1941,"depth":66,"text":1942,"children":46060},[46061,46062,46063,46064],{"id":45979,"depth":73,"text":45980},{"id":45986,"depth":73,"text":45987},{"id":45993,"depth":73,"text":45994},{"id":46000,"depth":73,"text":46001},{"id":2004,"depth":66,"text":2005},"Master python list processing example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-list-processing-example",{"title":44283,"description":46066},"examples\u002Fpython-list-processing-example","pD6A18Cf9r_B3LFP5vRjx2SYF1EnnIcDnw-DVYagmfU",{"id":46073,"title":46074,"body":46075,"description":49054,"extension":2075,"meta":49055,"navigation":69,"path":49056,"seo":49057,"stem":49058,"__hash__":49059},"content\u002Fexamples\u002Fpython-log-file-analyzer-example.md","Python Log File Analyzer Example",{"type":7,"value":46076,"toc":49010},[46077,46080,46083,46096,46105,46109,46112,46406,46408,46425,46428,46430,46433,46453,46457,46460,46463,46474,46477,46494,46498,46501,46538,46546,46550,46554,46557,46563,46566,46583,46587,46591,46596,46677,46680,46703,46707,46710,46795,46803,46809,46816,46911,46914,46925,46931,47113,47118,47124,47128,47131,47137,47367,47373,47422,47426,47429,47775,47777,47783,47789,47792,47796,47799,47803,47809,47812,47820,47824,47827,47988,47991,47995,47998,48130,48137,48146,48149,48386,48392,48395,48703,48707,48710,48713,48716,48739,48741,48744,48748,48753,48755,48808,48811,48822,48828,48832,48838,48842,48850,48853,48873,48877,48884,48887,48896,48902,48907,48911,48914,48917,48919,48923,48929,48933,48936,48940,48949,48953,48956,48958,48993,48996,49007],[10,46078,46074],{"id":46079},"python-log-file-analyzer-example",[14,46081,46082],{},"A log file analyzer is a good beginner project because it combines several useful Python skills in one script.",[14,46084,46085,46086,2587,46089,11556,46092,46095],{},"In this example, you will read a log file, look for important lines such as ",[49,46087,46088],{},"ERROR",[49,46090,46091],{},"WARNING",[49,46093,46094],{},"INFO",", count them, and print a simple summary.",[14,46097,46098,46099,857,46101,186],{},"This page focuses on building one practical script. If you want a separate lesson on file handling, see ",[372,46100,29867],{"href":29866},[372,46102,46104],{"href":46103},"\u002Fhow-to\u002Fhow-to-read-a-file-line-by-line-in-python\u002F","how to read a file line by line in Python",[37,46106,46108],{"id":46107},"quick-working-version","Quick working version",[14,46110,46111],{},"If you want a simple version first, start with this:",[42,46113,46115],{"className":44,"code":46114,"language":46,"meta":47,"style":47},"from collections import Counter\n\nlog_file = \"app.log\"\ncounts = Counter()\n\nwith open(log_file, \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        line = line.strip()\n        if \"ERROR\" in line:\n            counts[\"ERROR\"] += 1\n        elif \"WARNING\" in line:\n            counts[\"WARNING\"] += 1\n        elif \"INFO\" in line:\n            counts[\"INFO\"] += 1\n\nprint(\"Log summary\")\nfor level, total in counts.items():\n    print(f\"{level}: {total}\")\n",[49,46116,46117,46129,46133,46147,46159,46163,46202,46215,46231,46247,46266,46283,46301,46317,46335,46339,46354,46377],{"__ignoreMap":47},[52,46118,46119,46121,46124,46126],{"class":54,"line":55},[52,46120,25329],{"class":58},[52,46122,46123],{"class":62}," collections ",[52,46125,59],{"class":58},[52,46127,46128],{"class":62}," Counter\n",[52,46130,46131],{"class":54,"line":66},[52,46132,70],{"emptyLinePlaceholder":69},[52,46134,46135,46138,46140,46142,46145],{"class":54,"line":73},[52,46136,46137],{"class":62},"log_file ",[52,46139,80],{"class":79},[52,46141,84],{"class":83},[52,46143,46144],{"class":87},"app.log",[52,46146,91],{"class":83},[52,46148,46149,46152,46154,46157],{"class":54,"line":94},[52,46150,46151],{"class":62},"counts ",[52,46153,80],{"class":79},[52,46155,46156],{"class":189}," Counter",[52,46158,230],{"class":102},[52,46160,46161],{"class":54,"line":106},[52,46162,70],{"emptyLinePlaceholder":69},[52,46164,46165,46167,46169,46171,46174,46176,46178,46180,46182,46184,46186,46188,46190,46192,46194,46196,46198,46200],{"class":54,"line":128},[52,46166,14632],{"class":58},[52,46168,14635],{"class":241},[52,46170,193],{"class":102},[52,46172,46173],{"class":189},"log_file",[52,46175,199],{"class":102},[52,46177,84],{"class":83},[52,46179,41783],{"class":87},[52,46181,115],{"class":83},[52,46183,199],{"class":102},[52,46185,14659],{"class":202},[52,46187,80],{"class":79},[52,46189,115],{"class":83},[52,46191,14666],{"class":87},[52,46193,115],{"class":83},[52,46195,1521],{"class":102},[52,46197,3567],{"class":58},[52,46199,14676],{"class":14675},[52,46201,1330],{"class":102},[52,46203,46204,46206,46209,46211,46213],{"class":54,"line":148},[52,46205,11773],{"class":58},[52,46207,46208],{"class":62}," line ",[52,46210,11213],{"class":58},[52,46212,14676],{"class":14675},[52,46214,1330],{"class":102},[52,46216,46217,46220,46222,46225,46227,46229],{"class":54,"line":164},[52,46218,46219],{"class":62},"        line ",[52,46221,80],{"class":79},[52,46223,46224],{"class":62}," line",[52,46226,186],{"class":102},[52,46228,18630],{"class":189},[52,46230,230],{"class":102},[52,46232,46233,46235,46237,46239,46241,46243,46245],{"class":54,"line":170},[52,46234,11168],{"class":58},[52,46236,84],{"class":83},[52,46238,46088],{"class":87},[52,46240,115],{"class":83},[52,46242,3101],{"class":79},[52,46244,46224],{"class":62},[52,46246,1330],{"class":102},[52,46248,46249,46252,46254,46256,46258,46260,46262,46264],{"class":54,"line":175},[52,46250,46251],{"class":62},"            counts",[52,46253,250],{"class":102},[52,46255,115],{"class":83},[52,46257,46088],{"class":87},[52,46259,115],{"class":83},[52,46261,11122],{"class":102},[52,46263,8642],{"class":79},[52,46265,8645],{"class":121},[52,46267,46268,46271,46273,46275,46277,46279,46281],{"class":54,"line":214},[52,46269,46270],{"class":58},"        elif",[52,46272,84],{"class":83},[52,46274,46091],{"class":87},[52,46276,115],{"class":83},[52,46278,3101],{"class":79},[52,46280,46224],{"class":62},[52,46282,1330],{"class":102},[52,46284,46285,46287,46289,46291,46293,46295,46297,46299],{"class":54,"line":233},[52,46286,46251],{"class":62},[52,46288,250],{"class":102},[52,46290,115],{"class":83},[52,46292,46091],{"class":87},[52,46294,115],{"class":83},[52,46296,11122],{"class":102},[52,46298,8642],{"class":79},[52,46300,8645],{"class":121},[52,46302,46303,46305,46307,46309,46311,46313,46315],{"class":54,"line":238},[52,46304,46270],{"class":58},[52,46306,84],{"class":83},[52,46308,46094],{"class":87},[52,46310,115],{"class":83},[52,46312,3101],{"class":79},[52,46314,46224],{"class":62},[52,46316,1330],{"class":102},[52,46318,46319,46321,46323,46325,46327,46329,46331,46333],{"class":54,"line":272},[52,46320,46251],{"class":62},[52,46322,250],{"class":102},[52,46324,115],{"class":83},[52,46326,46094],{"class":87},[52,46328,115],{"class":83},[52,46330,11122],{"class":102},[52,46332,8642],{"class":79},[52,46334,8645],{"class":121},[52,46336,46337],{"class":54,"line":1348},[52,46338,70],{"emptyLinePlaceholder":69},[52,46340,46341,46343,46345,46347,46350,46352],{"class":54,"line":1376},[52,46342,242],{"class":241},[52,46344,193],{"class":102},[52,46346,115],{"class":83},[52,46348,46349],{"class":87},"Log summary",[52,46351,115],{"class":83},[52,46353,211],{"class":102},[52,46355,46356,46358,46361,46363,46366,46368,46371,46373,46375],{"class":54,"line":1381},[52,46357,12265],{"class":58},[52,46359,46360],{"class":62}," level",[52,46362,199],{"class":102},[52,46364,46365],{"class":62}," total ",[52,46367,11213],{"class":58},[52,46369,46370],{"class":62}," counts",[52,46372,186],{"class":102},[52,46374,11220],{"class":189},[52,46376,8146],{"class":102},[52,46378,46379,46381,46383,46385,46387,46389,46392,46394,46396,46398,46400,46402,46404],{"class":54,"line":1406},[52,46380,1599],{"class":241},[52,46382,193],{"class":102},[52,46384,1476],{"class":1475},[52,46386,115],{"class":87},[52,46388,1482],{"class":121},[52,46390,46391],{"class":189},"level",[52,46393,1488],{"class":121},[52,46395,2578],{"class":87},[52,46397,1482],{"class":121},[52,46399,9900],{"class":189},[52,46401,1488],{"class":121},[52,46403,115],{"class":87},[52,46405,211],{"class":102},[14,46407,24512],{},[309,46409,46410,46413,46416,46419,46422],{},[24,46411,46412],{},"opens the file",[24,46414,46415],{},"reads it one line at a time",[24,46417,46418],{},"checks each line for common log levels",[24,46420,46421],{},"counts matches",[24,46423,46424],{},"prints the totals",[14,46426,46427],{},"Next, let’s build it step by step so the code is easier to understand.",[37,46429,9295],{"id":9294},[14,46431,46432],{},"This script will:",[309,46434,46435,46438,46447,46450],{},[24,46436,46437],{},"Read a text log file line by line",[24,46439,46440,46441,2587,46443,11556,46445],{},"Look for common log levels such as ",[49,46442,46088],{},[49,46444,46091],{},[49,46446,46094],{},[24,46448,46449],{},"Count how many times each level appears",[24,46451,46452],{},"Print a simple report at the end",[37,46454,46456],{"id":46455},"what-a-log-file-is","What a log file is",[14,46458,46459],{},"A log file is a text file created by a program.",[14,46461,46462],{},"It usually stores messages about what the program did, such as:",[309,46464,46465,46468,46471],{},[24,46466,46467],{},"normal activity",[24,46469,46470],{},"warnings",[24,46472,46473],{},"errors",[14,46475,46476],{},"For example, a program might write messages like:",[309,46478,46479,46484,46489],{},[24,46480,46481],{},[49,46482,46483],{},"INFO: User logged in",[24,46485,46486],{},[49,46487,46488],{},"WARNING: Disk space is low",[24,46490,46491],{},[49,46492,46493],{},"ERROR: Could not connect to database",[37,46495,46497],{"id":46496},"skills-used-in-this-project","Skills used in this project",[14,46499,46500],{},"This example uses several basic Python skills:",[309,46502,46503,46510,46515,46523,46529,46535],{},[24,46504,46505,46506],{},"Opening a file with ",[372,46507,46508],{"href":14984},[49,46509,14980],{},[24,46511,46512,46513,12570],{},"Looping through lines with a ",[49,46514,12265],{},[24,46516,46517,46518],{},"Cleaning text with ",[372,46519,46521],{"href":46520},"\u002Freference\u002Fpython-string-strip-method\u002F",[49,46522,18831],{},[24,46524,46525,46526,46528],{},"Checking text with the ",[49,46527,11213],{}," operator",[24,46530,46531,46532],{},"Counting values with a dictionary or ",[49,46533,46534],{},"Counter",[24,46536,46537],{},"Printing readable output",[14,46539,46540,46541,46543,46544,186],{},"If dictionaries are new to you, see ",[372,46542,10883],{"href":10882},". If loops are new, see ",[372,46545,34468],{"href":32073},[37,46547,46549],{"id":46548},"sample-log-file-input","Sample log file input",[14,46551,14768,46552,14771],{},[49,46553,46144],{},[14,46555,46556],{},"Put this sample text inside it:",[42,46558,46561],{"className":46559,"code":46560,"language":955,"meta":47},[953],"INFO: Application started\nINFO: User login successful\nWARNING: Disk space is getting low\nERROR: Could not connect to server\nINFO: Retrying connection\nERROR: Connection failed again\n",[49,46562,46560],{"__ignoreMap":47},[14,46564,46565],{},"This file contains:",[309,46567,46568,46573,46578],{},[24,46569,46570,46571,38593],{},"several ",[49,46572,46094],{},[24,46574,46575,46576,46224],{},"one ",[49,46577,46091],{},[24,46579,46580,46581,38593],{},"two ",[49,46582,46088],{},[37,46584,46586],{"id":46585},"build-the-analyzer-step-by-step","Build the analyzer step by step",[639,46588,46590],{"id":46589},"_1-open-the-file-safely","1. Open the file safely",[14,46592,3650,46593,46595],{},[49,46594,14741],{}," to open the file.",[42,46597,46599],{"className":44,"code":46598,"language":46,"meta":47,"style":47},"log_file = \"app.log\"\n\nwith open(log_file, \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        print(line)\n",[49,46600,46601,46613,46617,46655,46667],{"__ignoreMap":47},[52,46602,46603,46605,46607,46609,46611],{"class":54,"line":55},[52,46604,46137],{"class":62},[52,46606,80],{"class":79},[52,46608,84],{"class":83},[52,46610,46144],{"class":87},[52,46612,91],{"class":83},[52,46614,46615],{"class":54,"line":66},[52,46616,70],{"emptyLinePlaceholder":69},[52,46618,46619,46621,46623,46625,46627,46629,46631,46633,46635,46637,46639,46641,46643,46645,46647,46649,46651,46653],{"class":54,"line":73},[52,46620,14632],{"class":58},[52,46622,14635],{"class":241},[52,46624,193],{"class":102},[52,46626,46173],{"class":189},[52,46628,199],{"class":102},[52,46630,84],{"class":83},[52,46632,41783],{"class":87},[52,46634,115],{"class":83},[52,46636,199],{"class":102},[52,46638,14659],{"class":202},[52,46640,80],{"class":79},[52,46642,115],{"class":83},[52,46644,14666],{"class":87},[52,46646,115],{"class":83},[52,46648,1521],{"class":102},[52,46650,3567],{"class":58},[52,46652,14676],{"class":14675},[52,46654,1330],{"class":102},[52,46656,46657,46659,46661,46663,46665],{"class":54,"line":94},[52,46658,11773],{"class":58},[52,46660,46208],{"class":62},[52,46662,11213],{"class":58},[52,46664,14676],{"class":14675},[52,46666,1330],{"class":102},[52,46668,46669,46671,46673,46675],{"class":54,"line":106},[52,46670,1470],{"class":241},[52,46672,193],{"class":102},[52,46674,54],{"class":189},[52,46676,211],{"class":102},[14,46678,46679],{},"Why this works:",[309,46681,46682,46687,46693,46698],{},[24,46683,46684,46686],{},[49,46685,14980],{}," opens the file for reading",[24,46688,46689,46692],{},[49,46690,46691],{},"\"r\""," means read mode",[24,46694,46695,46697],{},[49,46696,17367],{}," is a safe choice for text files",[24,46699,46700,46702],{},[49,46701,14632],{}," closes the file automatically when finished",[639,46704,46706],{"id":46705},"_2-loop-over-each-line","2. Loop over each line",[14,46708,46709],{},"A file can be looped through directly.",[42,46711,46713],{"className":44,"code":46712,"language":46,"meta":47,"style":47},"log_file = \"app.log\"\n\nwith open(log_file, \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        print(repr(line))\n",[49,46714,46715,46727,46731,46769,46781],{"__ignoreMap":47},[52,46716,46717,46719,46721,46723,46725],{"class":54,"line":55},[52,46718,46137],{"class":62},[52,46720,80],{"class":79},[52,46722,84],{"class":83},[52,46724,46144],{"class":87},[52,46726,91],{"class":83},[52,46728,46729],{"class":54,"line":66},[52,46730,70],{"emptyLinePlaceholder":69},[52,46732,46733,46735,46737,46739,46741,46743,46745,46747,46749,46751,46753,46755,46757,46759,46761,46763,46765,46767],{"class":54,"line":73},[52,46734,14632],{"class":58},[52,46736,14635],{"class":241},[52,46738,193],{"class":102},[52,46740,46173],{"class":189},[52,46742,199],{"class":102},[52,46744,84],{"class":83},[52,46746,41783],{"class":87},[52,46748,115],{"class":83},[52,46750,199],{"class":102},[52,46752,14659],{"class":202},[52,46754,80],{"class":79},[52,46756,115],{"class":83},[52,46758,14666],{"class":87},[52,46760,115],{"class":83},[52,46762,1521],{"class":102},[52,46764,3567],{"class":58},[52,46766,14676],{"class":14675},[52,46768,1330],{"class":102},[52,46770,46771,46773,46775,46777,46779],{"class":54,"line":94},[52,46772,11773],{"class":58},[52,46774,46208],{"class":62},[52,46776,11213],{"class":58},[52,46778,14676],{"class":14675},[52,46780,1330],{"class":102},[52,46782,46783,46785,46787,46789,46791,46793],{"class":54,"line":106},[52,46784,1470],{"class":241},[52,46786,193],{"class":102},[52,46788,20689],{"class":241},[52,46790,193],{"class":102},[52,46792,54],{"class":189},[52,46794,8886],{"class":102},[14,46796,10294,46797,46800,46801,186],{},[49,46798,46799],{},"repr(line)"," helps you see hidden characters like ",[49,46802,10956],{},[639,46804,46806,46807],{"id":46805},"_3-remove-extra-newline-characters-with-strip","3. Remove extra newline characters with ",[49,46808,18831],{},[14,46810,46811,46812,186],{},"Each line usually ends with a newline character. You can remove it with ",[372,46813,46814],{"href":46520},[49,46815,18831],{},[42,46817,46819],{"className":44,"code":46818,"language":46,"meta":47,"style":47},"log_file = \"app.log\"\n\nwith open(log_file, \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        line = line.strip()\n        print(line)\n",[49,46820,46821,46833,46837,46875,46887,46901],{"__ignoreMap":47},[52,46822,46823,46825,46827,46829,46831],{"class":54,"line":55},[52,46824,46137],{"class":62},[52,46826,80],{"class":79},[52,46828,84],{"class":83},[52,46830,46144],{"class":87},[52,46832,91],{"class":83},[52,46834,46835],{"class":54,"line":66},[52,46836,70],{"emptyLinePlaceholder":69},[52,46838,46839,46841,46843,46845,46847,46849,46851,46853,46855,46857,46859,46861,46863,46865,46867,46869,46871,46873],{"class":54,"line":73},[52,46840,14632],{"class":58},[52,46842,14635],{"class":241},[52,46844,193],{"class":102},[52,46846,46173],{"class":189},[52,46848,199],{"class":102},[52,46850,84],{"class":83},[52,46852,41783],{"class":87},[52,46854,115],{"class":83},[52,46856,199],{"class":102},[52,46858,14659],{"class":202},[52,46860,80],{"class":79},[52,46862,115],{"class":83},[52,46864,14666],{"class":87},[52,46866,115],{"class":83},[52,46868,1521],{"class":102},[52,46870,3567],{"class":58},[52,46872,14676],{"class":14675},[52,46874,1330],{"class":102},[52,46876,46877,46879,46881,46883,46885],{"class":54,"line":94},[52,46878,11773],{"class":58},[52,46880,46208],{"class":62},[52,46882,11213],{"class":58},[52,46884,14676],{"class":14675},[52,46886,1330],{"class":102},[52,46888,46889,46891,46893,46895,46897,46899],{"class":54,"line":106},[52,46890,46219],{"class":62},[52,46892,80],{"class":79},[52,46894,46224],{"class":62},[52,46896,186],{"class":102},[52,46898,18630],{"class":189},[52,46900,230],{"class":102},[52,46902,46903,46905,46907,46909],{"class":54,"line":128},[52,46904,1470],{"class":241},[52,46906,193],{"class":102},[52,46908,54],{"class":189},[52,46910,211],{"class":102},[14,46912,46913],{},"This makes the line cleaner before you check its content.",[639,46915,46917,46918,2587,46920,46922,46923],{"id":46916},"_4-check-whether-a-line-contains-error-warning-or-info","4. Check whether a line contains ",[49,46919,46088],{},[49,46921,46091],{},", or ",[49,46924,46094],{},[14,46926,46927,46928,46930],{},"Now check each cleaned line with the ",[49,46929,11213],{}," operator.",[42,46932,46934],{"className":44,"code":46933,"language":46,"meta":47,"style":47},"log_file = \"app.log\"\n\nwith open(log_file, \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        line = line.strip()\n\n        if \"ERROR\" in line:\n            print(\"Found an error line\")\n        elif \"WARNING\" in line:\n            print(\"Found a warning line\")\n        elif \"INFO\" in line:\n            print(\"Found an info line\")\n",[49,46935,46936,46948,46952,46990,47002,47016,47020,47036,47051,47067,47082,47098],{"__ignoreMap":47},[52,46937,46938,46940,46942,46944,46946],{"class":54,"line":55},[52,46939,46137],{"class":62},[52,46941,80],{"class":79},[52,46943,84],{"class":83},[52,46945,46144],{"class":87},[52,46947,91],{"class":83},[52,46949,46950],{"class":54,"line":66},[52,46951,70],{"emptyLinePlaceholder":69},[52,46953,46954,46956,46958,46960,46962,46964,46966,46968,46970,46972,46974,46976,46978,46980,46982,46984,46986,46988],{"class":54,"line":73},[52,46955,14632],{"class":58},[52,46957,14635],{"class":241},[52,46959,193],{"class":102},[52,46961,46173],{"class":189},[52,46963,199],{"class":102},[52,46965,84],{"class":83},[52,46967,41783],{"class":87},[52,46969,115],{"class":83},[52,46971,199],{"class":102},[52,46973,14659],{"class":202},[52,46975,80],{"class":79},[52,46977,115],{"class":83},[52,46979,14666],{"class":87},[52,46981,115],{"class":83},[52,46983,1521],{"class":102},[52,46985,3567],{"class":58},[52,46987,14676],{"class":14675},[52,46989,1330],{"class":102},[52,46991,46992,46994,46996,46998,47000],{"class":54,"line":94},[52,46993,11773],{"class":58},[52,46995,46208],{"class":62},[52,46997,11213],{"class":58},[52,46999,14676],{"class":14675},[52,47001,1330],{"class":102},[52,47003,47004,47006,47008,47010,47012,47014],{"class":54,"line":106},[52,47005,46219],{"class":62},[52,47007,80],{"class":79},[52,47009,46224],{"class":62},[52,47011,186],{"class":102},[52,47013,18630],{"class":189},[52,47015,230],{"class":102},[52,47017,47018],{"class":54,"line":128},[52,47019,70],{"emptyLinePlaceholder":69},[52,47021,47022,47024,47026,47028,47030,47032,47034],{"class":54,"line":148},[52,47023,11168],{"class":58},[52,47025,84],{"class":83},[52,47027,46088],{"class":87},[52,47029,115],{"class":83},[52,47031,3101],{"class":79},[52,47033,46224],{"class":62},[52,47035,1330],{"class":102},[52,47037,47038,47040,47042,47044,47047,47049],{"class":54,"line":164},[52,47039,11180],{"class":241},[52,47041,193],{"class":102},[52,47043,115],{"class":83},[52,47045,47046],{"class":87},"Found an error line",[52,47048,115],{"class":83},[52,47050,211],{"class":102},[52,47052,47053,47055,47057,47059,47061,47063,47065],{"class":54,"line":170},[52,47054,46270],{"class":58},[52,47056,84],{"class":83},[52,47058,46091],{"class":87},[52,47060,115],{"class":83},[52,47062,3101],{"class":79},[52,47064,46224],{"class":62},[52,47066,1330],{"class":102},[52,47068,47069,47071,47073,47075,47078,47080],{"class":54,"line":175},[52,47070,11180],{"class":241},[52,47072,193],{"class":102},[52,47074,115],{"class":83},[52,47076,47077],{"class":87},"Found a warning line",[52,47079,115],{"class":83},[52,47081,211],{"class":102},[52,47083,47084,47086,47088,47090,47092,47094,47096],{"class":54,"line":214},[52,47085,46270],{"class":58},[52,47087,84],{"class":83},[52,47089,46094],{"class":87},[52,47091,115],{"class":83},[52,47093,3101],{"class":79},[52,47095,46224],{"class":62},[52,47097,1330],{"class":102},[52,47099,47100,47102,47104,47106,47109,47111],{"class":54,"line":233},[52,47101,11180],{"class":241},[52,47103,193],{"class":102},[52,47105,115],{"class":83},[52,47107,47108],{"class":87},"Found an info line",[52,47110,115],{"class":83},[52,47112,211],{"class":102},[14,47114,1956,47115,47117],{},[49,47116,10787],{}," here?",[14,47119,47120,47121,47123],{},"Because one line should only be counted once. Using ",[49,47122,10787],{}," stops Python after the first matching condition.",[639,47125,47127],{"id":47126},"_5-increase-the-correct-count","5. Increase the correct count",[14,47129,47130],{},"Now let’s count each type of line.",[14,47132,47133,47134,47136],{},"This version uses ",[49,47135,46534],{},", which is a convenient counting tool.",[42,47138,47140],{"className":44,"code":47139,"language":46,"meta":47,"style":47},"from collections import Counter\n\nlog_file = \"app.log\"\ncounts = Counter()\n\nwith open(log_file, \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        line = line.strip()\n\n        if \"ERROR\" in line:\n            counts[\"ERROR\"] += 1\n        elif \"WARNING\" in line:\n            counts[\"WARNING\"] += 1\n        elif \"INFO\" in line:\n            counts[\"INFO\"] += 1\n\nprint(counts)\n",[49,47141,47142,47152,47156,47168,47178,47182,47220,47232,47246,47250,47266,47284,47300,47318,47334,47352,47356],{"__ignoreMap":47},[52,47143,47144,47146,47148,47150],{"class":54,"line":55},[52,47145,25329],{"class":58},[52,47147,46123],{"class":62},[52,47149,59],{"class":58},[52,47151,46128],{"class":62},[52,47153,47154],{"class":54,"line":66},[52,47155,70],{"emptyLinePlaceholder":69},[52,47157,47158,47160,47162,47164,47166],{"class":54,"line":73},[52,47159,46137],{"class":62},[52,47161,80],{"class":79},[52,47163,84],{"class":83},[52,47165,46144],{"class":87},[52,47167,91],{"class":83},[52,47169,47170,47172,47174,47176],{"class":54,"line":94},[52,47171,46151],{"class":62},[52,47173,80],{"class":79},[52,47175,46156],{"class":189},[52,47177,230],{"class":102},[52,47179,47180],{"class":54,"line":106},[52,47181,70],{"emptyLinePlaceholder":69},[52,47183,47184,47186,47188,47190,47192,47194,47196,47198,47200,47202,47204,47206,47208,47210,47212,47214,47216,47218],{"class":54,"line":128},[52,47185,14632],{"class":58},[52,47187,14635],{"class":241},[52,47189,193],{"class":102},[52,47191,46173],{"class":189},[52,47193,199],{"class":102},[52,47195,84],{"class":83},[52,47197,41783],{"class":87},[52,47199,115],{"class":83},[52,47201,199],{"class":102},[52,47203,14659],{"class":202},[52,47205,80],{"class":79},[52,47207,115],{"class":83},[52,47209,14666],{"class":87},[52,47211,115],{"class":83},[52,47213,1521],{"class":102},[52,47215,3567],{"class":58},[52,47217,14676],{"class":14675},[52,47219,1330],{"class":102},[52,47221,47222,47224,47226,47228,47230],{"class":54,"line":148},[52,47223,11773],{"class":58},[52,47225,46208],{"class":62},[52,47227,11213],{"class":58},[52,47229,14676],{"class":14675},[52,47231,1330],{"class":102},[52,47233,47234,47236,47238,47240,47242,47244],{"class":54,"line":164},[52,47235,46219],{"class":62},[52,47237,80],{"class":79},[52,47239,46224],{"class":62},[52,47241,186],{"class":102},[52,47243,18630],{"class":189},[52,47245,230],{"class":102},[52,47247,47248],{"class":54,"line":170},[52,47249,70],{"emptyLinePlaceholder":69},[52,47251,47252,47254,47256,47258,47260,47262,47264],{"class":54,"line":175},[52,47253,11168],{"class":58},[52,47255,84],{"class":83},[52,47257,46088],{"class":87},[52,47259,115],{"class":83},[52,47261,3101],{"class":79},[52,47263,46224],{"class":62},[52,47265,1330],{"class":102},[52,47267,47268,47270,47272,47274,47276,47278,47280,47282],{"class":54,"line":214},[52,47269,46251],{"class":62},[52,47271,250],{"class":102},[52,47273,115],{"class":83},[52,47275,46088],{"class":87},[52,47277,115],{"class":83},[52,47279,11122],{"class":102},[52,47281,8642],{"class":79},[52,47283,8645],{"class":121},[52,47285,47286,47288,47290,47292,47294,47296,47298],{"class":54,"line":233},[52,47287,46270],{"class":58},[52,47289,84],{"class":83},[52,47291,46091],{"class":87},[52,47293,115],{"class":83},[52,47295,3101],{"class":79},[52,47297,46224],{"class":62},[52,47299,1330],{"class":102},[52,47301,47302,47304,47306,47308,47310,47312,47314,47316],{"class":54,"line":238},[52,47303,46251],{"class":62},[52,47305,250],{"class":102},[52,47307,115],{"class":83},[52,47309,46091],{"class":87},[52,47311,115],{"class":83},[52,47313,11122],{"class":102},[52,47315,8642],{"class":79},[52,47317,8645],{"class":121},[52,47319,47320,47322,47324,47326,47328,47330,47332],{"class":54,"line":272},[52,47321,46270],{"class":58},[52,47323,84],{"class":83},[52,47325,46094],{"class":87},[52,47327,115],{"class":83},[52,47329,3101],{"class":79},[52,47331,46224],{"class":62},[52,47333,1330],{"class":102},[52,47335,47336,47338,47340,47342,47344,47346,47348,47350],{"class":54,"line":1348},[52,47337,46251],{"class":62},[52,47339,250],{"class":102},[52,47341,115],{"class":83},[52,47343,46094],{"class":87},[52,47345,115],{"class":83},[52,47347,11122],{"class":102},[52,47349,8642],{"class":79},[52,47351,8645],{"class":121},[52,47353,47354],{"class":54,"line":1376},[52,47355,70],{"emptyLinePlaceholder":69},[52,47357,47358,47360,47362,47365],{"class":54,"line":1381},[52,47359,242],{"class":241},[52,47361,193],{"class":102},[52,47363,47364],{"class":189},"counts",[52,47366,211],{"class":102},[14,47368,47369,47370,47372],{},"After reading the sample log file, ",[49,47371,47364],{}," will contain:",[42,47374,47376],{"className":44,"code":47375,"language":46,"meta":47,"style":47},"Counter({'INFO': 3, 'ERROR': 2, 'WARNING': 1})\n",[49,47377,47378],{"__ignoreMap":47},[52,47379,47380,47382,47385,47387,47389,47391,47393,47395,47397,47399,47401,47403,47405,47407,47409,47411,47413,47415,47417,47419],{"class":54,"line":55},[52,47381,46534],{"class":189},[52,47383,47384],{"class":102},"({",[52,47386,4162],{"class":83},[52,47388,46094],{"class":87},[52,47390,4162],{"class":83},[52,47392,118],{"class":102},[52,47394,5261],{"class":121},[52,47396,199],{"class":102},[52,47398,4172],{"class":83},[52,47400,46088],{"class":87},[52,47402,4162],{"class":83},[52,47404,118],{"class":102},[52,47406,9168],{"class":121},[52,47408,199],{"class":102},[52,47410,4172],{"class":83},[52,47412,46091],{"class":87},[52,47414,4162],{"class":83},[52,47416,118],{"class":102},[52,47418,1518],{"class":121},[52,47420,47421],{"class":102},"})\n",[639,47423,47425],{"id":47424},"_6-print-the-final-totals","6. Print the final totals",[14,47427,47428],{},"Here is the full script:",[42,47430,47432],{"className":44,"code":47431,"language":46,"meta":47,"style":47},"from collections import Counter\n\nlog_file = \"app.log\"\ncounts = Counter()\ntotal_lines = 0\n\nwith open(log_file, \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        total_lines += 1\n        line = line.strip()\n\n        if \"ERROR\" in line:\n            counts[\"ERROR\"] += 1\n        elif \"WARNING\" in line:\n            counts[\"WARNING\"] += 1\n        elif \"INFO\" in line:\n            counts[\"INFO\"] += 1\n\nprint(\"Log summary\")\nprint(f\"Total lines: {total_lines}\")\n\nfor level in [\"ERROR\", \"WARNING\", \"INFO\"]:\n    print(f\"{level}: {counts[level]}\")\n",[49,47433,47434,47444,47448,47460,47470,47479,47483,47521,47533,47542,47556,47560,47576,47594,47610,47628,47644,47662,47666,47680,47702,47706,47741],{"__ignoreMap":47},[52,47435,47436,47438,47440,47442],{"class":54,"line":55},[52,47437,25329],{"class":58},[52,47439,46123],{"class":62},[52,47441,59],{"class":58},[52,47443,46128],{"class":62},[52,47445,47446],{"class":54,"line":66},[52,47447,70],{"emptyLinePlaceholder":69},[52,47449,47450,47452,47454,47456,47458],{"class":54,"line":73},[52,47451,46137],{"class":62},[52,47453,80],{"class":79},[52,47455,84],{"class":83},[52,47457,46144],{"class":87},[52,47459,91],{"class":83},[52,47461,47462,47464,47466,47468],{"class":54,"line":94},[52,47463,46151],{"class":62},[52,47465,80],{"class":79},[52,47467,46156],{"class":189},[52,47469,230],{"class":102},[52,47471,47472,47475,47477],{"class":54,"line":106},[52,47473,47474],{"class":62},"total_lines ",[52,47476,80],{"class":79},[52,47478,34598],{"class":121},[52,47480,47481],{"class":54,"line":128},[52,47482,70],{"emptyLinePlaceholder":69},[52,47484,47485,47487,47489,47491,47493,47495,47497,47499,47501,47503,47505,47507,47509,47511,47513,47515,47517,47519],{"class":54,"line":148},[52,47486,14632],{"class":58},[52,47488,14635],{"class":241},[52,47490,193],{"class":102},[52,47492,46173],{"class":189},[52,47494,199],{"class":102},[52,47496,84],{"class":83},[52,47498,41783],{"class":87},[52,47500,115],{"class":83},[52,47502,199],{"class":102},[52,47504,14659],{"class":202},[52,47506,80],{"class":79},[52,47508,115],{"class":83},[52,47510,14666],{"class":87},[52,47512,115],{"class":83},[52,47514,1521],{"class":102},[52,47516,3567],{"class":58},[52,47518,14676],{"class":14675},[52,47520,1330],{"class":102},[52,47522,47523,47525,47527,47529,47531],{"class":54,"line":164},[52,47524,11773],{"class":58},[52,47526,46208],{"class":62},[52,47528,11213],{"class":58},[52,47530,14676],{"class":14675},[52,47532,1330],{"class":102},[52,47534,47535,47538,47540],{"class":54,"line":170},[52,47536,47537],{"class":62},"        total_lines ",[52,47539,34682],{"class":79},[52,47541,8645],{"class":121},[52,47543,47544,47546,47548,47550,47552,47554],{"class":54,"line":175},[52,47545,46219],{"class":62},[52,47547,80],{"class":79},[52,47549,46224],{"class":62},[52,47551,186],{"class":102},[52,47553,18630],{"class":189},[52,47555,230],{"class":102},[52,47557,47558],{"class":54,"line":214},[52,47559,70],{"emptyLinePlaceholder":69},[52,47561,47562,47564,47566,47568,47570,47572,47574],{"class":54,"line":233},[52,47563,11168],{"class":58},[52,47565,84],{"class":83},[52,47567,46088],{"class":87},[52,47569,115],{"class":83},[52,47571,3101],{"class":79},[52,47573,46224],{"class":62},[52,47575,1330],{"class":102},[52,47577,47578,47580,47582,47584,47586,47588,47590,47592],{"class":54,"line":238},[52,47579,46251],{"class":62},[52,47581,250],{"class":102},[52,47583,115],{"class":83},[52,47585,46088],{"class":87},[52,47587,115],{"class":83},[52,47589,11122],{"class":102},[52,47591,8642],{"class":79},[52,47593,8645],{"class":121},[52,47595,47596,47598,47600,47602,47604,47606,47608],{"class":54,"line":272},[52,47597,46270],{"class":58},[52,47599,84],{"class":83},[52,47601,46091],{"class":87},[52,47603,115],{"class":83},[52,47605,3101],{"class":79},[52,47607,46224],{"class":62},[52,47609,1330],{"class":102},[52,47611,47612,47614,47616,47618,47620,47622,47624,47626],{"class":54,"line":1348},[52,47613,46251],{"class":62},[52,47615,250],{"class":102},[52,47617,115],{"class":83},[52,47619,46091],{"class":87},[52,47621,115],{"class":83},[52,47623,11122],{"class":102},[52,47625,8642],{"class":79},[52,47627,8645],{"class":121},[52,47629,47630,47632,47634,47636,47638,47640,47642],{"class":54,"line":1376},[52,47631,46270],{"class":58},[52,47633,84],{"class":83},[52,47635,46094],{"class":87},[52,47637,115],{"class":83},[52,47639,3101],{"class":79},[52,47641,46224],{"class":62},[52,47643,1330],{"class":102},[52,47645,47646,47648,47650,47652,47654,47656,47658,47660],{"class":54,"line":1381},[52,47647,46251],{"class":62},[52,47649,250],{"class":102},[52,47651,115],{"class":83},[52,47653,46094],{"class":87},[52,47655,115],{"class":83},[52,47657,11122],{"class":102},[52,47659,8642],{"class":79},[52,47661,8645],{"class":121},[52,47663,47664],{"class":54,"line":1406},[52,47665,70],{"emptyLinePlaceholder":69},[52,47667,47668,47670,47672,47674,47676,47678],{"class":54,"line":1430},[52,47669,242],{"class":241},[52,47671,193],{"class":102},[52,47673,115],{"class":83},[52,47675,46349],{"class":87},[52,47677,115],{"class":83},[52,47679,211],{"class":102},[52,47681,47682,47684,47686,47688,47691,47693,47696,47698,47700],{"class":54,"line":1435},[52,47683,242],{"class":241},[52,47685,193],{"class":102},[52,47687,1476],{"class":1475},[52,47689,47690],{"class":87},"\"Total lines: ",[52,47692,1482],{"class":121},[52,47694,47695],{"class":189},"total_lines",[52,47697,1488],{"class":121},[52,47699,115],{"class":87},[52,47701,211],{"class":102},[52,47703,47704],{"class":54,"line":1467},[52,47705,70],{"emptyLinePlaceholder":69},[52,47707,47708,47710,47713,47715,47717,47719,47721,47723,47725,47727,47729,47731,47733,47735,47737,47739],{"class":54,"line":1495},[52,47709,12265],{"class":58},[52,47711,47712],{"class":62}," level ",[52,47714,11213],{"class":58},[52,47716,18162],{"class":102},[52,47718,115],{"class":83},[52,47720,46088],{"class":87},[52,47722,115],{"class":83},[52,47724,199],{"class":102},[52,47726,84],{"class":83},[52,47728,46091],{"class":87},[52,47730,115],{"class":83},[52,47732,199],{"class":102},[52,47734,84],{"class":83},[52,47736,46094],{"class":87},[52,47738,115],{"class":83},[52,47740,35426],{"class":102},[52,47742,47743,47745,47747,47749,47751,47753,47755,47757,47759,47761,47763,47765,47767,47769,47771,47773],{"class":54,"line":1531},[52,47744,1599],{"class":241},[52,47746,193],{"class":102},[52,47748,1476],{"class":1475},[52,47750,115],{"class":87},[52,47752,1482],{"class":121},[52,47754,46391],{"class":189},[52,47756,1488],{"class":121},[52,47758,2578],{"class":87},[52,47760,1482],{"class":121},[52,47762,47364],{"class":189},[52,47764,250],{"class":102},[52,47766,46391],{"class":189},[52,47768,11122],{"class":102},[52,47770,1488],{"class":121},[52,47772,115],{"class":87},[52,47774,211],{"class":102},[37,47776,13563],{"id":13562},[14,47778,47779,47780,47782],{},"With the sample ",[49,47781,46144],{}," file shown earlier, the output will be:",[42,47784,47787],{"className":47785,"code":47786,"language":955,"meta":47},[953],"Log summary\nTotal lines: 6\nERROR: 2\nWARNING: 1\nINFO: 3\n",[49,47788,47786],{"__ignoreMap":47},[14,47790,47791],{},"This output is simple and easy to compare with the input file.",[37,47793,47795],{"id":47794},"useful-beginner-improvements","Useful beginner improvements",[14,47797,47798],{},"Once the basic version works, you can try small improvements.",[639,47800,47802],{"id":47801},"count-all-lines-not-only-matched-lines","Count all lines, not only matched lines",[14,47804,47805,47806,186],{},"The full example already shows ",[49,47807,47808],{},"total_lines += 1",[14,47810,47811],{},"This helps you compare:",[309,47813,47814,47817],{},[24,47815,47816],{},"total lines in the file",[24,47818,47819],{},"matched log level lines",[639,47821,47823],{"id":47822},"store-matching-lines-in-separate-lists","Store matching lines in separate lists",[14,47825,47826],{},"You can save the actual lines, not just the totals.",[42,47828,47830],{"className":44,"code":47829,"language":46,"meta":47,"style":47},"error_lines = []\nwarning_lines = []\n\nwith open(\"app.log\", \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        line = line.strip()\n\n        if \"ERROR\" in line:\n            error_lines.append(line)\n        elif \"WARNING\" in line:\n            warning_lines.append(line)\n",[49,47831,47832,47841,47850,47854,47896,47908,47922,47926,47942,47957,47973],{"__ignoreMap":47},[52,47833,47834,47837,47839],{"class":54,"line":55},[52,47835,47836],{"class":62},"error_lines ",[52,47838,80],{"class":79},[52,47840,18590],{"class":102},[52,47842,47843,47846,47848],{"class":54,"line":66},[52,47844,47845],{"class":62},"warning_lines ",[52,47847,80],{"class":79},[52,47849,18590],{"class":102},[52,47851,47852],{"class":54,"line":73},[52,47853,70],{"emptyLinePlaceholder":69},[52,47855,47856,47858,47860,47862,47864,47866,47868,47870,47872,47874,47876,47878,47880,47882,47884,47886,47888,47890,47892,47894],{"class":54,"line":94},[52,47857,14632],{"class":58},[52,47859,14635],{"class":241},[52,47861,193],{"class":102},[52,47863,115],{"class":83},[52,47865,46144],{"class":87},[52,47867,115],{"class":83},[52,47869,199],{"class":102},[52,47871,84],{"class":83},[52,47873,41783],{"class":87},[52,47875,115],{"class":83},[52,47877,199],{"class":102},[52,47879,14659],{"class":202},[52,47881,80],{"class":79},[52,47883,115],{"class":83},[52,47885,14666],{"class":87},[52,47887,115],{"class":83},[52,47889,1521],{"class":102},[52,47891,3567],{"class":58},[52,47893,14676],{"class":14675},[52,47895,1330],{"class":102},[52,47897,47898,47900,47902,47904,47906],{"class":54,"line":106},[52,47899,11773],{"class":58},[52,47901,46208],{"class":62},[52,47903,11213],{"class":58},[52,47905,14676],{"class":14675},[52,47907,1330],{"class":102},[52,47909,47910,47912,47914,47916,47918,47920],{"class":54,"line":128},[52,47911,46219],{"class":62},[52,47913,80],{"class":79},[52,47915,46224],{"class":62},[52,47917,186],{"class":102},[52,47919,18630],{"class":189},[52,47921,230],{"class":102},[52,47923,47924],{"class":54,"line":148},[52,47925,70],{"emptyLinePlaceholder":69},[52,47927,47928,47930,47932,47934,47936,47938,47940],{"class":54,"line":164},[52,47929,11168],{"class":58},[52,47931,84],{"class":83},[52,47933,46088],{"class":87},[52,47935,115],{"class":83},[52,47937,3101],{"class":79},[52,47939,46224],{"class":62},[52,47941,1330],{"class":102},[52,47943,47944,47947,47949,47951,47953,47955],{"class":54,"line":170},[52,47945,47946],{"class":62},"            error_lines",[52,47948,186],{"class":102},[52,47950,18748],{"class":189},[52,47952,193],{"class":102},[52,47954,54],{"class":189},[52,47956,211],{"class":102},[52,47958,47959,47961,47963,47965,47967,47969,47971],{"class":54,"line":175},[52,47960,46270],{"class":58},[52,47962,84],{"class":83},[52,47964,46091],{"class":87},[52,47966,115],{"class":83},[52,47968,3101],{"class":79},[52,47970,46224],{"class":62},[52,47972,1330],{"class":102},[52,47974,47975,47978,47980,47982,47984,47986],{"class":54,"line":214},[52,47976,47977],{"class":62},"            warning_lines",[52,47979,186],{"class":102},[52,47981,18748],{"class":189},[52,47983,193],{"class":102},[52,47985,54],{"class":189},[52,47987,211],{"class":102},[14,47989,47990],{},"This is useful if you want to print or save the problem lines later.",[639,47992,47994],{"id":47993},"find-the-first-error-line","Find the first error line",[14,47996,47997],{},"You can stop as soon as the first error is found.",[42,47999,48001],{"className":44,"code":48000,"language":46,"meta":47,"style":47},"first_error = None\n\nwith open(\"app.log\", \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        line = line.strip()\n        if \"ERROR\" in line:\n            first_error = line\n            break\n\nprint(first_error)\n",[49,48002,48003,48012,48016,48058,48070,48084,48100,48110,48115,48119],{"__ignoreMap":47},[52,48004,48005,48008,48010],{"class":54,"line":55},[52,48006,48007],{"class":62},"first_error ",[52,48009,80],{"class":79},[52,48011,18734],{"class":160},[52,48013,48014],{"class":54,"line":66},[52,48015,70],{"emptyLinePlaceholder":69},[52,48017,48018,48020,48022,48024,48026,48028,48030,48032,48034,48036,48038,48040,48042,48044,48046,48048,48050,48052,48054,48056],{"class":54,"line":73},[52,48019,14632],{"class":58},[52,48021,14635],{"class":241},[52,48023,193],{"class":102},[52,48025,115],{"class":83},[52,48027,46144],{"class":87},[52,48029,115],{"class":83},[52,48031,199],{"class":102},[52,48033,84],{"class":83},[52,48035,41783],{"class":87},[52,48037,115],{"class":83},[52,48039,199],{"class":102},[52,48041,14659],{"class":202},[52,48043,80],{"class":79},[52,48045,115],{"class":83},[52,48047,14666],{"class":87},[52,48049,115],{"class":83},[52,48051,1521],{"class":102},[52,48053,3567],{"class":58},[52,48055,14676],{"class":14675},[52,48057,1330],{"class":102},[52,48059,48060,48062,48064,48066,48068],{"class":54,"line":94},[52,48061,11773],{"class":58},[52,48063,46208],{"class":62},[52,48065,11213],{"class":58},[52,48067,14676],{"class":14675},[52,48069,1330],{"class":102},[52,48071,48072,48074,48076,48078,48080,48082],{"class":54,"line":106},[52,48073,46219],{"class":62},[52,48075,80],{"class":79},[52,48077,46224],{"class":62},[52,48079,186],{"class":102},[52,48081,18630],{"class":189},[52,48083,230],{"class":102},[52,48085,48086,48088,48090,48092,48094,48096,48098],{"class":54,"line":128},[52,48087,11168],{"class":58},[52,48089,84],{"class":83},[52,48091,46088],{"class":87},[52,48093,115],{"class":83},[52,48095,3101],{"class":79},[52,48097,46224],{"class":62},[52,48099,1330],{"class":102},[52,48101,48102,48105,48107],{"class":54,"line":148},[52,48103,48104],{"class":62},"            first_error ",[52,48106,80],{"class":79},[52,48108,48109],{"class":62}," line\n",[52,48111,48112],{"class":54,"line":164},[52,48113,48114],{"class":58},"            break\n",[52,48116,48117],{"class":54,"line":170},[52,48118,70],{"emptyLinePlaceholder":69},[52,48120,48121,48123,48125,48128],{"class":54,"line":175},[52,48122,242],{"class":241},[52,48124,193],{"class":102},[52,48126,48127],{"class":189},"first_error",[52,48129,211],{"class":102},[639,48131,48133,48134],{"id":48132},"ignore-letter-case-with-upper","Ignore letter case with ",[49,48135,48136],{},"upper()",[14,48138,48139,48140,2587,48142,46922,48144,186],{},"Some files may use ",[49,48141,24786],{},[49,48143,27980],{},[49,48145,46088],{},[14,48147,48148],{},"To handle that, convert each line to one case first:",[42,48150,48152],{"className":44,"code":48151,"language":46,"meta":47,"style":47},"from collections import Counter\n\ncounts = Counter()\n\nwith open(\"app.log\", \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        line = line.strip()\n        upper_line = line.upper()\n\n        if \"ERROR\" in upper_line:\n            counts[\"ERROR\"] += 1\n        elif \"WARNING\" in upper_line:\n            counts[\"WARNING\"] += 1\n        elif \"INFO\" in upper_line:\n            counts[\"INFO\"] += 1\n\nprint(counts)\n",[49,48153,48154,48164,48168,48178,48182,48224,48236,48250,48265,48269,48286,48304,48320,48338,48354,48372,48376],{"__ignoreMap":47},[52,48155,48156,48158,48160,48162],{"class":54,"line":55},[52,48157,25329],{"class":58},[52,48159,46123],{"class":62},[52,48161,59],{"class":58},[52,48163,46128],{"class":62},[52,48165,48166],{"class":54,"line":66},[52,48167,70],{"emptyLinePlaceholder":69},[52,48169,48170,48172,48174,48176],{"class":54,"line":73},[52,48171,46151],{"class":62},[52,48173,80],{"class":79},[52,48175,46156],{"class":189},[52,48177,230],{"class":102},[52,48179,48180],{"class":54,"line":94},[52,48181,70],{"emptyLinePlaceholder":69},[52,48183,48184,48186,48188,48190,48192,48194,48196,48198,48200,48202,48204,48206,48208,48210,48212,48214,48216,48218,48220,48222],{"class":54,"line":106},[52,48185,14632],{"class":58},[52,48187,14635],{"class":241},[52,48189,193],{"class":102},[52,48191,115],{"class":83},[52,48193,46144],{"class":87},[52,48195,115],{"class":83},[52,48197,199],{"class":102},[52,48199,84],{"class":83},[52,48201,41783],{"class":87},[52,48203,115],{"class":83},[52,48205,199],{"class":102},[52,48207,14659],{"class":202},[52,48209,80],{"class":79},[52,48211,115],{"class":83},[52,48213,14666],{"class":87},[52,48215,115],{"class":83},[52,48217,1521],{"class":102},[52,48219,3567],{"class":58},[52,48221,14676],{"class":14675},[52,48223,1330],{"class":102},[52,48225,48226,48228,48230,48232,48234],{"class":54,"line":128},[52,48227,11773],{"class":58},[52,48229,46208],{"class":62},[52,48231,11213],{"class":58},[52,48233,14676],{"class":14675},[52,48235,1330],{"class":102},[52,48237,48238,48240,48242,48244,48246,48248],{"class":54,"line":148},[52,48239,46219],{"class":62},[52,48241,80],{"class":79},[52,48243,46224],{"class":62},[52,48245,186],{"class":102},[52,48247,18630],{"class":189},[52,48249,230],{"class":102},[52,48251,48252,48255,48257,48259,48261,48263],{"class":54,"line":164},[52,48253,48254],{"class":62},"        upper_line ",[52,48256,80],{"class":79},[52,48258,46224],{"class":62},[52,48260,186],{"class":102},[52,48262,6173],{"class":189},[52,48264,230],{"class":102},[52,48266,48267],{"class":54,"line":170},[52,48268,70],{"emptyLinePlaceholder":69},[52,48270,48271,48273,48275,48277,48279,48281,48284],{"class":54,"line":175},[52,48272,11168],{"class":58},[52,48274,84],{"class":83},[52,48276,46088],{"class":87},[52,48278,115],{"class":83},[52,48280,3101],{"class":79},[52,48282,48283],{"class":62}," upper_line",[52,48285,1330],{"class":102},[52,48287,48288,48290,48292,48294,48296,48298,48300,48302],{"class":54,"line":214},[52,48289,46251],{"class":62},[52,48291,250],{"class":102},[52,48293,115],{"class":83},[52,48295,46088],{"class":87},[52,48297,115],{"class":83},[52,48299,11122],{"class":102},[52,48301,8642],{"class":79},[52,48303,8645],{"class":121},[52,48305,48306,48308,48310,48312,48314,48316,48318],{"class":54,"line":233},[52,48307,46270],{"class":58},[52,48309,84],{"class":83},[52,48311,46091],{"class":87},[52,48313,115],{"class":83},[52,48315,3101],{"class":79},[52,48317,48283],{"class":62},[52,48319,1330],{"class":102},[52,48321,48322,48324,48326,48328,48330,48332,48334,48336],{"class":54,"line":238},[52,48323,46251],{"class":62},[52,48325,250],{"class":102},[52,48327,115],{"class":83},[52,48329,46091],{"class":87},[52,48331,115],{"class":83},[52,48333,11122],{"class":102},[52,48335,8642],{"class":79},[52,48337,8645],{"class":121},[52,48339,48340,48342,48344,48346,48348,48350,48352],{"class":54,"line":272},[52,48341,46270],{"class":58},[52,48343,84],{"class":83},[52,48345,46094],{"class":87},[52,48347,115],{"class":83},[52,48349,3101],{"class":79},[52,48351,48283],{"class":62},[52,48353,1330],{"class":102},[52,48355,48356,48358,48360,48362,48364,48366,48368,48370],{"class":54,"line":1348},[52,48357,46251],{"class":62},[52,48359,250],{"class":102},[52,48361,115],{"class":83},[52,48363,46094],{"class":87},[52,48365,115],{"class":83},[52,48367,11122],{"class":102},[52,48369,8642],{"class":79},[52,48371,8645],{"class":121},[52,48373,48374],{"class":54,"line":1376},[52,48375,70],{"emptyLinePlaceholder":69},[52,48377,48378,48380,48382,48384],{"class":54,"line":1381},[52,48379,242],{"class":241},[52,48381,193],{"class":102},[52,48383,47364],{"class":189},[52,48385,211],{"class":102},[639,48387,48389,48390],{"id":48388},"ask-the-user-for-the-file-name-with-input","Ask the user for the file name with ",[49,48391,10597],{},[14,48393,48394],{},"You can make the script more flexible:",[42,48396,48398],{"className":44,"code":48397,"language":46,"meta":47,"style":47},"from collections import Counter\n\nlog_file = input(\"Enter the log file name: \")\ncounts = Counter()\n\nwith open(log_file, \"r\", encoding=\"utf-8\") as file:\n    for line in file:\n        line = line.strip()\n\n        if \"ERROR\" in line:\n            counts[\"ERROR\"] += 1\n        elif \"WARNING\" in line:\n            counts[\"WARNING\"] += 1\n        elif \"INFO\" in line:\n            counts[\"INFO\"] += 1\n\nprint(\"Log summary\")\nfor level in [\"ERROR\", \"WARNING\", \"INFO\"]:\n    print(f\"{level}: {counts[level]}\")\n",[49,48399,48400,48410,48414,48433,48443,48447,48485,48497,48511,48515,48531,48549,48565,48583,48599,48617,48621,48635,48669],{"__ignoreMap":47},[52,48401,48402,48404,48406,48408],{"class":54,"line":55},[52,48403,25329],{"class":58},[52,48405,46123],{"class":62},[52,48407,59],{"class":58},[52,48409,46128],{"class":62},[52,48411,48412],{"class":54,"line":66},[52,48413,70],{"emptyLinePlaceholder":69},[52,48415,48416,48418,48420,48422,48424,48426,48429,48431],{"class":54,"line":73},[52,48417,46137],{"class":62},[52,48419,80],{"class":79},[52,48421,11037],{"class":241},[52,48423,193],{"class":102},[52,48425,115],{"class":83},[52,48427,48428],{"class":87},"Enter the log file name: ",[52,48430,115],{"class":83},[52,48432,211],{"class":102},[52,48434,48435,48437,48439,48441],{"class":54,"line":94},[52,48436,46151],{"class":62},[52,48438,80],{"class":79},[52,48440,46156],{"class":189},[52,48442,230],{"class":102},[52,48444,48445],{"class":54,"line":106},[52,48446,70],{"emptyLinePlaceholder":69},[52,48448,48449,48451,48453,48455,48457,48459,48461,48463,48465,48467,48469,48471,48473,48475,48477,48479,48481,48483],{"class":54,"line":128},[52,48450,14632],{"class":58},[52,48452,14635],{"class":241},[52,48454,193],{"class":102},[52,48456,46173],{"class":189},[52,48458,199],{"class":102},[52,48460,84],{"class":83},[52,48462,41783],{"class":87},[52,48464,115],{"class":83},[52,48466,199],{"class":102},[52,48468,14659],{"class":202},[52,48470,80],{"class":79},[52,48472,115],{"class":83},[52,48474,14666],{"class":87},[52,48476,115],{"class":83},[52,48478,1521],{"class":102},[52,48480,3567],{"class":58},[52,48482,14676],{"class":14675},[52,48484,1330],{"class":102},[52,48486,48487,48489,48491,48493,48495],{"class":54,"line":148},[52,48488,11773],{"class":58},[52,48490,46208],{"class":62},[52,48492,11213],{"class":58},[52,48494,14676],{"class":14675},[52,48496,1330],{"class":102},[52,48498,48499,48501,48503,48505,48507,48509],{"class":54,"line":164},[52,48500,46219],{"class":62},[52,48502,80],{"class":79},[52,48504,46224],{"class":62},[52,48506,186],{"class":102},[52,48508,18630],{"class":189},[52,48510,230],{"class":102},[52,48512,48513],{"class":54,"line":170},[52,48514,70],{"emptyLinePlaceholder":69},[52,48516,48517,48519,48521,48523,48525,48527,48529],{"class":54,"line":175},[52,48518,11168],{"class":58},[52,48520,84],{"class":83},[52,48522,46088],{"class":87},[52,48524,115],{"class":83},[52,48526,3101],{"class":79},[52,48528,46224],{"class":62},[52,48530,1330],{"class":102},[52,48532,48533,48535,48537,48539,48541,48543,48545,48547],{"class":54,"line":214},[52,48534,46251],{"class":62},[52,48536,250],{"class":102},[52,48538,115],{"class":83},[52,48540,46088],{"class":87},[52,48542,115],{"class":83},[52,48544,11122],{"class":102},[52,48546,8642],{"class":79},[52,48548,8645],{"class":121},[52,48550,48551,48553,48555,48557,48559,48561,48563],{"class":54,"line":233},[52,48552,46270],{"class":58},[52,48554,84],{"class":83},[52,48556,46091],{"class":87},[52,48558,115],{"class":83},[52,48560,3101],{"class":79},[52,48562,46224],{"class":62},[52,48564,1330],{"class":102},[52,48566,48567,48569,48571,48573,48575,48577,48579,48581],{"class":54,"line":238},[52,48568,46251],{"class":62},[52,48570,250],{"class":102},[52,48572,115],{"class":83},[52,48574,46091],{"class":87},[52,48576,115],{"class":83},[52,48578,11122],{"class":102},[52,48580,8642],{"class":79},[52,48582,8645],{"class":121},[52,48584,48585,48587,48589,48591,48593,48595,48597],{"class":54,"line":272},[52,48586,46270],{"class":58},[52,48588,84],{"class":83},[52,48590,46094],{"class":87},[52,48592,115],{"class":83},[52,48594,3101],{"class":79},[52,48596,46224],{"class":62},[52,48598,1330],{"class":102},[52,48600,48601,48603,48605,48607,48609,48611,48613,48615],{"class":54,"line":1348},[52,48602,46251],{"class":62},[52,48604,250],{"class":102},[52,48606,115],{"class":83},[52,48608,46094],{"class":87},[52,48610,115],{"class":83},[52,48612,11122],{"class":102},[52,48614,8642],{"class":79},[52,48616,8645],{"class":121},[52,48618,48619],{"class":54,"line":1376},[52,48620,70],{"emptyLinePlaceholder":69},[52,48622,48623,48625,48627,48629,48631,48633],{"class":54,"line":1381},[52,48624,242],{"class":241},[52,48626,193],{"class":102},[52,48628,115],{"class":83},[52,48630,46349],{"class":87},[52,48632,115],{"class":83},[52,48634,211],{"class":102},[52,48636,48637,48639,48641,48643,48645,48647,48649,48651,48653,48655,48657,48659,48661,48663,48665,48667],{"class":54,"line":1406},[52,48638,12265],{"class":58},[52,48640,47712],{"class":62},[52,48642,11213],{"class":58},[52,48644,18162],{"class":102},[52,48646,115],{"class":83},[52,48648,46088],{"class":87},[52,48650,115],{"class":83},[52,48652,199],{"class":102},[52,48654,84],{"class":83},[52,48656,46091],{"class":87},[52,48658,115],{"class":83},[52,48660,199],{"class":102},[52,48662,84],{"class":83},[52,48664,46094],{"class":87},[52,48666,115],{"class":83},[52,48668,35426],{"class":102},[52,48670,48671,48673,48675,48677,48679,48681,48683,48685,48687,48689,48691,48693,48695,48697,48699,48701],{"class":54,"line":1430},[52,48672,1599],{"class":241},[52,48674,193],{"class":102},[52,48676,1476],{"class":1475},[52,48678,115],{"class":87},[52,48680,1482],{"class":121},[52,48682,46391],{"class":189},[52,48684,1488],{"class":121},[52,48686,2578],{"class":87},[52,48688,1482],{"class":121},[52,48690,47364],{"class":189},[52,48692,250],{"class":102},[52,48694,46391],{"class":189},[52,48696,11122],{"class":102},[52,48698,1488],{"class":121},[52,48700,115],{"class":87},[52,48702,211],{"class":102},[37,48704,48706],{"id":48705},"how-this-page-is-different-from-related-pages","How this page is different from related pages",[14,48708,48709],{},"This page is a full example project.",[14,48711,48712],{},"It is not a complete lesson on every file or string method used here.",[14,48714,48715],{},"For deeper explanations, use these related pages:",[309,48717,48718,48722,48726,48732],{},[24,48719,48720],{},[372,48721,29867],{"href":29866},[24,48723,48724],{},[372,48725,46104],{"href":46103},[24,48727,48728],{},[372,48729,10646,48730,3711],{"href":14984},[49,48731,14980],{},[24,48733,48734],{},[372,48735,10646,48736,48738],{"href":46520},[49,48737,18831],{}," string method",[37,48740,12322],{"id":12321},[14,48742,48743],{},"Here are some common problems beginners run into with this kind of script.",[639,48745,48747],{"id":48746},"the-log-file-name-is-wrong","The log file name is wrong",[14,48749,48750,48751,186],{},"If the file does not exist, Python raises a ",[49,48752,15764],{},[14,48754,4259],{},[42,48756,48758],{"className":44,"code":48757,"language":46,"meta":47,"style":47},"with open(\"wrong_name.log\", \"r\", encoding=\"utf-8\") as file:\n    pass\n",[49,48759,48760,48803],{"__ignoreMap":47},[52,48761,48762,48764,48766,48768,48770,48773,48775,48777,48779,48781,48783,48785,48787,48789,48791,48793,48795,48797,48799,48801],{"class":54,"line":55},[52,48763,14632],{"class":58},[52,48765,14635],{"class":241},[52,48767,193],{"class":102},[52,48769,115],{"class":83},[52,48771,48772],{"class":87},"wrong_name.log",[52,48774,115],{"class":83},[52,48776,199],{"class":102},[52,48778,84],{"class":83},[52,48780,41783],{"class":87},[52,48782,115],{"class":83},[52,48784,199],{"class":102},[52,48786,14659],{"class":202},[52,48788,80],{"class":79},[52,48790,115],{"class":83},[52,48792,14666],{"class":87},[52,48794,115],{"class":83},[52,48796,1521],{"class":102},[52,48798,3567],{"class":58},[52,48800,14676],{"class":14675},[52,48802,1330],{"class":102},[52,48804,48805],{"class":54,"line":66},[52,48806,48807],{"class":58},"    pass\n",[14,48809,48810],{},"If that happens, check:",[309,48812,48813,48816,48819],{},[24,48814,48815],{},"the spelling of the file name",[24,48817,48818],{},"that the file is in the correct folder",[24,48820,48821],{},"your current working directory",[14,48823,48824,48825],{},"See also: ",[372,48826,15904],{"href":48827},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes",[639,48829,48831],{"id":48830},"the-path-points-to-a-folder-instead-of-a-file","The path points to a folder instead of a file",[14,48833,48834,48835,48837],{},"Make sure ",[49,48836,46173],{}," contains a file path, not just a folder name.",[639,48839,48841],{"id":48840},"the-log-levels-use-different-letter-case","The log levels use different letter case",[14,48843,48844,48845,10598,48847,48849],{},"If your file contains ",[49,48846,24786],{},[49,48848,46088],{},", your checks may fail.",[14,48851,48852],{},"Fix this by converting the line first:",[42,48854,48856],{"className":44,"code":48855,"language":46,"meta":47,"style":47},"upper_line = line.upper()\n",[49,48857,48858],{"__ignoreMap":47},[52,48859,48860,48863,48865,48867,48869,48871],{"class":54,"line":55},[52,48861,48862],{"class":62},"upper_line ",[52,48864,80],{"class":79},[52,48866,46224],{"class":62},[52,48868,186],{"class":102},[52,48870,6173],{"class":189},[52,48872,230],{"class":102},[639,48874,48876],{"id":48875},"you-forget-to-strip-newline-characters","You forget to strip newline characters",[14,48878,5788,48879,48881,48882,186],{},[49,48880,18831],{},", the line may contain extra spaces or ",[49,48883,10956],{},[14,48885,48886],{},"That can make debugging harder.",[639,48888,48890,48891,48893,48894],{"id":48889},"you-use-if-for-every-check-instead-of-elif","You use ",[49,48892,1313],{}," for every check instead of ",[49,48895,10787],{},[14,48897,48898,48899,48901],{},"If you use separate ",[49,48900,1313],{}," statements, one line could be counted more than once in some cases.",[14,48903,10294,48904,48906],{},[49,48905,10787],{}," makes the checks exclusive.",[639,48908,48910],{"id":48909},"the-file-contains-unexpected-text-format","The file contains unexpected text format",[14,48912,48913],{},"Some log files may have extra spaces, timestamps, or different wording.",[14,48915,48916],{},"If needed, print each line while testing so you can see the real format.",[37,48918,1942],{"id":1941},[639,48920,48922],{"id":48921},"do-i-need-a-special-library-to-analyze-a-log-file","Do I need a special library to analyze a log file?",[14,48924,48925,48926,48928],{},"No. A basic analyzer can use only built-in Python tools such as ",[49,48927,14980],{},", loops, strings, and dictionaries.",[639,48930,48932],{"id":48931},"should-i-read-the-whole-file-at-once","Should I read the whole file at once?",[14,48934,48935],{},"For beginners, reading line by line is usually better. It uses less memory and is easier to understand.",[639,48937,48939],{"id":48938},"what-if-my-log-file-uses-lowercase-words-like-error","What if my log file uses lowercase words like error?",[14,48941,48942,48943,48946,48947,186],{},"Convert each line to one case first, such as ",[49,48944,48945],{},"line.upper()",", then check for ",[49,48948,46088],{},[639,48950,48952],{"id":48951},"can-this-example-search-for-dates-or-usernames-too","Can this example search for dates or usernames too?",[14,48954,48955],{},"Yes. You can check each line for other patterns and store extra counts or matching lines.",[37,48957,2005],{"id":2004},[309,48959,48960,48964,48969,48975,48981,48985,48989],{},[24,48961,48962],{},[372,48963,29921],{"href":29866},[24,48965,48966],{},[372,48967,48968],{"href":46103},"How to read a file line by line in Python",[24,48970,48971],{},[372,48972,10646,48973,3711],{"href":14984},[49,48974,14980],{},[24,48976,48977],{},[372,48978,10646,48979,48738],{"href":46520},[49,48980,18831],{},[24,48982,48983],{},[372,48984,10883],{"href":10882},[24,48986,48987],{},[372,48988,34468],{"href":32073},[24,48990,48991],{},[372,48992,15904],{"href":48827},[14,48994,48995],{},"Try extending this project next:",[309,48997,48998,49001,49004],{},[24,48999,49000],{},"count dates in the log",[24,49002,49003],{},"extract all error lines",[24,49005,49006],{},"save the summary to a new file",[2034,49008,49009],{},"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 .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 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 .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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":49011},[49012,49013,49014,49015,49016,49017,49027,49028,49037,49038,49047,49053],{"id":46107,"depth":66,"text":46108},{"id":9294,"depth":66,"text":9295},{"id":46455,"depth":66,"text":46456},{"id":46496,"depth":66,"text":46497},{"id":46548,"depth":66,"text":46549},{"id":46585,"depth":66,"text":46586,"children":49018},[49019,49020,49021,49023,49025,49026],{"id":46589,"depth":73,"text":46590},{"id":46705,"depth":73,"text":46706},{"id":46805,"depth":73,"text":49022},"3. Remove extra newline characters with strip()",{"id":46916,"depth":73,"text":49024},"4. Check whether a line contains ERROR, WARNING, or INFO",{"id":47126,"depth":73,"text":47127},{"id":47424,"depth":73,"text":47425},{"id":13562,"depth":66,"text":13563},{"id":47794,"depth":66,"text":47795,"children":49029},[49030,49031,49032,49033,49035],{"id":47801,"depth":73,"text":47802},{"id":47822,"depth":73,"text":47823},{"id":47993,"depth":73,"text":47994},{"id":48132,"depth":73,"text":49034},"Ignore letter case with upper()",{"id":48388,"depth":73,"text":49036},"Ask the user for the file name with input()",{"id":48705,"depth":66,"text":48706},{"id":12321,"depth":66,"text":12322,"children":49039},[49040,49041,49042,49043,49044,49046],{"id":48746,"depth":73,"text":48747},{"id":48830,"depth":73,"text":48831},{"id":48840,"depth":73,"text":48841},{"id":48875,"depth":73,"text":48876},{"id":48889,"depth":73,"text":49045},"You use if for every check instead of elif",{"id":48909,"depth":73,"text":48910},{"id":1941,"depth":66,"text":1942,"children":49048},[49049,49050,49051,49052],{"id":48921,"depth":73,"text":48922},{"id":48931,"depth":73,"text":48932},{"id":48938,"depth":73,"text":48939},{"id":48951,"depth":73,"text":48952},{"id":2004,"depth":66,"text":2005},"Master python log file analyzer example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-log-file-analyzer-example",{"title":46074,"description":49054},"examples\u002Fpython-log-file-analyzer-example","CR9xx4nDcdyds9MLoa2jof6wnb2rblfn06nlOGo_DX4",{"id":49061,"title":49062,"body":49063,"description":51377,"extension":2075,"meta":51378,"navigation":69,"path":51379,"seo":51380,"stem":51381,"__hash__":51382},"content\u002Fexamples\u002Fpython-merge-csv-files-example.md","Python Merge CSV Files Example",{"type":7,"value":49064,"toc":51346},[49065,49068,49071,49074,49091,49094,49096,49516,49522,49524,49526,49543,49545,49548,49562,49567,49569,49572,49962,49965,49988,49997,49999,50005,50036,50044,50058,50061,50078,50081,50087,50111,50114,50126,50129,50151,50157,50206,50213,50216,50224,50233,50238,50289,50296,50299,50305,50309,50312,50326,50329,50335,50338,50359,50361,50364,50378,50380,50412,50416,50419,50423,50426,50429,50437,50440,50488,50492,50495,50498,50501,50506,50509,50512,50518,50520,50524,50527,50570,50575,50579,50582,50585,50628,50632,50635,50637,50642,50649,50656,50660,50663,50666,50686,50689,51154,51158,51161,51177,51181,51184,51247,51250,51267,51269,51273,51279,51283,51286,51290,51296,51300,51308,51310,51340,51343],[10,49066,49062],{"id":49067},"python-merge-csv-files-example",[14,49069,49070],{},"If you have several CSV files with the same columns, you can combine them into one output file with a short Python script.",[14,49072,49073],{},"This example shows a simple beginner-friendly way to:",[309,49075,49076,49082,49085,49088],{},[24,49077,49078,49079,49081],{},"read all ",[49,49080,30164],{}," files from one folder",[24,49083,49084],{},"merge them into one file",[24,49086,49087],{},"write the header row only once",[24,49089,49090],{},"skip empty files safely",[14,49092,49093],{},"It also helps you avoid common problems such as repeated headers, wrong file paths, and CSV encoding issues.",[37,49095,40],{"id":39},[42,49097,49099],{"className":44,"code":49098,"language":46,"meta":47,"style":47},"import csv\nfrom pathlib import Path\n\ninput_folder = Path(\"csv_files\")\noutput_file = Path(\"merged.csv\")\n\ncsv_files = sorted(input_folder.glob(\"*.csv\"))\n\nwith output_file.open(\"w\", newline=\"\", encoding=\"utf-8\") as outfile:\n    writer = None\n\n    for file_index, file_path in enumerate(csv_files):\n        with file_path.open(\"r\", newline=\"\", encoding=\"utf-8\") as infile:\n            reader = csv.reader(infile)\n            header = next(reader, None)\n\n            if header is None:\n                continue\n\n            if writer is None:\n                writer = csv.writer(outfile)\n                writer.writerow(header)\n\n            for row in reader:\n                writer.writerow(row)\n\nprint(f\"Merged {len(csv_files)} files into {output_file}\")\n",[49,49100,49101,49107,49117,49121,49140,49159,49163,49194,49198,49246,49254,49258,49281,49330,49350,49370,49374,49388,49393,49397,49410,49430,49446,49450,49462,49476,49480],{"__ignoreMap":47},[52,49102,49103,49105],{"class":54,"line":55},[52,49104,59],{"class":58},[52,49106,14623],{"class":62},[52,49108,49109,49111,49113,49115],{"class":54,"line":66},[52,49110,25329],{"class":58},[52,49112,28537],{"class":62},[52,49114,59],{"class":58},[52,49116,28542],{"class":62},[52,49118,49119],{"class":54,"line":73},[52,49120,70],{"emptyLinePlaceholder":69},[52,49122,49123,49126,49128,49130,49132,49134,49136,49138],{"class":54,"line":94},[52,49124,49125],{"class":62},"input_folder ",[52,49127,80],{"class":79},[52,49129,28563],{"class":189},[52,49131,193],{"class":102},[52,49133,115],{"class":83},[52,49135,30173],{"class":87},[52,49137,115],{"class":83},[52,49139,211],{"class":102},[52,49141,49142,49144,49146,49148,49150,49152,49155,49157],{"class":54,"line":106},[52,49143,24394],{"class":62},[52,49145,80],{"class":79},[52,49147,28563],{"class":189},[52,49149,193],{"class":102},[52,49151,115],{"class":83},[52,49153,49154],{"class":87},"merged.csv",[52,49156,115],{"class":83},[52,49158,211],{"class":102},[52,49160,49161],{"class":54,"line":128},[52,49162,70],{"emptyLinePlaceholder":69},[52,49164,49165,49168,49170,49173,49175,49178,49180,49183,49185,49187,49190,49192],{"class":54,"line":148},[52,49166,49167],{"class":62},"csv_files ",[52,49169,80],{"class":79},[52,49171,49172],{"class":241}," sorted",[52,49174,193],{"class":102},[52,49176,49177],{"class":189},"input_folder",[52,49179,186],{"class":102},[52,49181,49182],{"class":189},"glob",[52,49184,193],{"class":102},[52,49186,115],{"class":83},[52,49188,49189],{"class":87},"*.csv",[52,49191,115],{"class":83},[52,49193,8886],{"class":102},[52,49195,49196],{"class":54,"line":164},[52,49197,70],{"emptyLinePlaceholder":69},[52,49199,49200,49202,49205,49207,49209,49211,49213,49215,49217,49219,49221,49223,49225,49227,49229,49231,49233,49235,49237,49239,49241,49244],{"class":54,"line":170},[52,49201,14632],{"class":58},[52,49203,49204],{"class":62}," output_file",[52,49206,186],{"class":102},[52,49208,18033],{"class":189},[52,49210,193],{"class":102},[52,49212,115],{"class":83},[52,49214,16760],{"class":87},[52,49216,115],{"class":83},[52,49218,199],{"class":102},[52,49220,14649],{"class":202},[52,49222,80],{"class":79},[52,49224,16771],{"class":83},[52,49226,199],{"class":102},[52,49228,14659],{"class":202},[52,49230,80],{"class":79},[52,49232,115],{"class":83},[52,49234,14666],{"class":87},[52,49236,115],{"class":83},[52,49238,1521],{"class":102},[52,49240,3567],{"class":58},[52,49242,49243],{"class":62}," outfile",[52,49245,1330],{"class":102},[52,49247,49248,49250,49252],{"class":54,"line":175},[52,49249,16796],{"class":62},[52,49251,80],{"class":79},[52,49253,18734],{"class":160},[52,49255,49256],{"class":54,"line":214},[52,49257,70],{"emptyLinePlaceholder":69},[52,49259,49260,49262,49265,49267,49270,49272,49275,49277,49279],{"class":54,"line":233},[52,49261,11773],{"class":58},[52,49263,49264],{"class":62}," file_index",[52,49266,199],{"class":102},[52,49268,49269],{"class":62}," file_path ",[52,49271,11213],{"class":58},[52,49273,49274],{"class":241}," enumerate",[52,49276,193],{"class":102},[52,49278,30173],{"class":189},[52,49280,3114],{"class":102},[52,49282,49283,49286,49289,49291,49293,49295,49297,49299,49301,49303,49305,49307,49309,49311,49313,49315,49317,49319,49321,49323,49325,49328],{"class":54,"line":238},[52,49284,49285],{"class":58},"        with",[52,49287,49288],{"class":62}," file_path",[52,49290,186],{"class":102},[52,49292,18033],{"class":189},[52,49294,193],{"class":102},[52,49296,115],{"class":83},[52,49298,41783],{"class":87},[52,49300,115],{"class":83},[52,49302,199],{"class":102},[52,49304,14649],{"class":202},[52,49306,80],{"class":79},[52,49308,16771],{"class":83},[52,49310,199],{"class":102},[52,49312,14659],{"class":202},[52,49314,80],{"class":79},[52,49316,115],{"class":83},[52,49318,14666],{"class":87},[52,49320,115],{"class":83},[52,49322,1521],{"class":102},[52,49324,3567],{"class":58},[52,49326,49327],{"class":62}," infile",[52,49329,1330],{"class":102},[52,49331,49332,49335,49337,49339,49341,49343,49345,49348],{"class":54,"line":272},[52,49333,49334],{"class":62},"            reader ",[52,49336,80],{"class":79},[52,49338,14688],{"class":62},[52,49340,186],{"class":102},[52,49342,14693],{"class":189},[52,49344,193],{"class":102},[52,49346,49347],{"class":189},"infile",[52,49349,211],{"class":102},[52,49351,49352,49355,49357,49360,49362,49364,49366,49368],{"class":54,"line":1348},[52,49353,49354],{"class":62},"            header ",[52,49356,80],{"class":79},[52,49358,49359],{"class":241}," next",[52,49361,193],{"class":102},[52,49363,14693],{"class":189},[52,49365,199],{"class":102},[52,49367,1450],{"class":160},[52,49369,211],{"class":102},[52,49371,49372],{"class":54,"line":1376},[52,49373,70],{"emptyLinePlaceholder":69},[52,49375,49376,49379,49382,49384,49386],{"class":54,"line":1381},[52,49377,49378],{"class":58},"            if",[52,49380,49381],{"class":62}," header ",[52,49383,1444],{"class":79},[52,49385,1450],{"class":160},[52,49387,1330],{"class":102},[52,49389,49390],{"class":54,"line":1406},[52,49391,49392],{"class":58},"                continue\n",[52,49394,49395],{"class":54,"line":1430},[52,49396,70],{"emptyLinePlaceholder":69},[52,49398,49399,49401,49404,49406,49408],{"class":54,"line":1435},[52,49400,49378],{"class":58},[52,49402,49403],{"class":62}," writer ",[52,49405,1444],{"class":79},[52,49407,1450],{"class":160},[52,49409,1330],{"class":102},[52,49411,49412,49415,49417,49419,49421,49423,49425,49428],{"class":54,"line":1467},[52,49413,49414],{"class":62},"                writer ",[52,49416,80],{"class":79},[52,49418,14688],{"class":62},[52,49420,186],{"class":102},[52,49422,16805],{"class":189},[52,49424,193],{"class":102},[52,49426,49427],{"class":189},"outfile",[52,49429,211],{"class":102},[52,49431,49432,49435,49437,49439,49441,49444],{"class":54,"line":1495},[52,49433,49434],{"class":62},"                writer",[52,49436,186],{"class":102},[52,49438,17629],{"class":189},[52,49440,193],{"class":102},[52,49442,49443],{"class":189},"header",[52,49445,211],{"class":102},[52,49447,49448],{"class":54,"line":1531},[52,49449,70],{"emptyLinePlaceholder":69},[52,49451,49452,49454,49456,49458,49460],{"class":54,"line":1564},[52,49453,11203],{"class":58},[52,49455,14707],{"class":62},[52,49457,11213],{"class":58},[52,49459,14712],{"class":62},[52,49461,1330],{"class":102},[52,49463,49464,49466,49468,49470,49472,49474],{"class":54,"line":1572},[52,49465,49434],{"class":62},[52,49467,186],{"class":102},[52,49469,17629],{"class":189},[52,49471,193],{"class":102},[52,49473,14723],{"class":189},[52,49475,211],{"class":102},[52,49477,49478],{"class":54,"line":1588},[52,49479,70],{"emptyLinePlaceholder":69},[52,49481,49482,49484,49486,49488,49491,49493,49495,49497,49499,49501,49503,49506,49508,49510,49512,49514],{"class":54,"line":1596},[52,49483,242],{"class":241},[52,49485,193],{"class":102},[52,49487,1476],{"class":1475},[52,49489,49490],{"class":87},"\"Merged ",[52,49492,1482],{"class":121},[52,49494,16152],{"class":241},[52,49496,193],{"class":102},[52,49498,30173],{"class":189},[52,49500,1521],{"class":102},[52,49502,1488],{"class":121},[52,49504,49505],{"class":87}," files into ",[52,49507,1482],{"class":121},[52,49509,24450],{"class":189},[52,49511,1488],{"class":121},[52,49513,115],{"class":87},[52,49515,211],{"class":102},[14,49517,49518,49519,49521],{},"This example merges all ",[49,49520,30164],{}," files in one folder and writes the header only once.",[37,49523,323],{"id":322},[14,49525,30376],{},[309,49527,49528,49531,49537,49540],{},[24,49529,49530],{},"Combines multiple CSV files into one file",[24,49532,49533,49534,49536],{},"Reads all ",[49,49535,30164],{}," files from a folder",[24,49538,49539],{},"Writes the header row only once",[24,49541,49542],{},"Skips empty files safely",[37,49544,25134],{"id":25133},[14,49546,49547],{},"Use this script when:",[309,49549,49550,49553,49556,49559],{},[24,49551,49552],{},"You have many CSV files with the same columns",[24,49554,49555],{},"You want one combined file for analysis",[24,49557,49558],{},"You need a simple script without extra libraries",[24,49560,49561],{},"You are learning file handling and loops",[14,49563,49564,49565,23283],{},"If you are new to file operations, it may also help to read ",[372,49566,14989],{"href":14988},[37,49568,25641],{"id":25640},[14,49570,49571],{},"Here is the same script again with the key idea in mind: read each file, keep only the first header, and write all data rows into one output file.",[42,49573,49574],{"className":44,"code":49098,"language":46,"meta":47,"style":47},[49,49575,49576,49582,49592,49596,49614,49632,49636,49662,49666,49712,49720,49724,49744,49790,49808,49826,49830,49842,49846,49850,49862,49880,49894,49898,49910,49924,49928],{"__ignoreMap":47},[52,49577,49578,49580],{"class":54,"line":55},[52,49579,59],{"class":58},[52,49581,14623],{"class":62},[52,49583,49584,49586,49588,49590],{"class":54,"line":66},[52,49585,25329],{"class":58},[52,49587,28537],{"class":62},[52,49589,59],{"class":58},[52,49591,28542],{"class":62},[52,49593,49594],{"class":54,"line":73},[52,49595,70],{"emptyLinePlaceholder":69},[52,49597,49598,49600,49602,49604,49606,49608,49610,49612],{"class":54,"line":94},[52,49599,49125],{"class":62},[52,49601,80],{"class":79},[52,49603,28563],{"class":189},[52,49605,193],{"class":102},[52,49607,115],{"class":83},[52,49609,30173],{"class":87},[52,49611,115],{"class":83},[52,49613,211],{"class":102},[52,49615,49616,49618,49620,49622,49624,49626,49628,49630],{"class":54,"line":106},[52,49617,24394],{"class":62},[52,49619,80],{"class":79},[52,49621,28563],{"class":189},[52,49623,193],{"class":102},[52,49625,115],{"class":83},[52,49627,49154],{"class":87},[52,49629,115],{"class":83},[52,49631,211],{"class":102},[52,49633,49634],{"class":54,"line":128},[52,49635,70],{"emptyLinePlaceholder":69},[52,49637,49638,49640,49642,49644,49646,49648,49650,49652,49654,49656,49658,49660],{"class":54,"line":148},[52,49639,49167],{"class":62},[52,49641,80],{"class":79},[52,49643,49172],{"class":241},[52,49645,193],{"class":102},[52,49647,49177],{"class":189},[52,49649,186],{"class":102},[52,49651,49182],{"class":189},[52,49653,193],{"class":102},[52,49655,115],{"class":83},[52,49657,49189],{"class":87},[52,49659,115],{"class":83},[52,49661,8886],{"class":102},[52,49663,49664],{"class":54,"line":164},[52,49665,70],{"emptyLinePlaceholder":69},[52,49667,49668,49670,49672,49674,49676,49678,49680,49682,49684,49686,49688,49690,49692,49694,49696,49698,49700,49702,49704,49706,49708,49710],{"class":54,"line":170},[52,49669,14632],{"class":58},[52,49671,49204],{"class":62},[52,49673,186],{"class":102},[52,49675,18033],{"class":189},[52,49677,193],{"class":102},[52,49679,115],{"class":83},[52,49681,16760],{"class":87},[52,49683,115],{"class":83},[52,49685,199],{"class":102},[52,49687,14649],{"class":202},[52,49689,80],{"class":79},[52,49691,16771],{"class":83},[52,49693,199],{"class":102},[52,49695,14659],{"class":202},[52,49697,80],{"class":79},[52,49699,115],{"class":83},[52,49701,14666],{"class":87},[52,49703,115],{"class":83},[52,49705,1521],{"class":102},[52,49707,3567],{"class":58},[52,49709,49243],{"class":62},[52,49711,1330],{"class":102},[52,49713,49714,49716,49718],{"class":54,"line":175},[52,49715,16796],{"class":62},[52,49717,80],{"class":79},[52,49719,18734],{"class":160},[52,49721,49722],{"class":54,"line":214},[52,49723,70],{"emptyLinePlaceholder":69},[52,49725,49726,49728,49730,49732,49734,49736,49738,49740,49742],{"class":54,"line":233},[52,49727,11773],{"class":58},[52,49729,49264],{"class":62},[52,49731,199],{"class":102},[52,49733,49269],{"class":62},[52,49735,11213],{"class":58},[52,49737,49274],{"class":241},[52,49739,193],{"class":102},[52,49741,30173],{"class":189},[52,49743,3114],{"class":102},[52,49745,49746,49748,49750,49752,49754,49756,49758,49760,49762,49764,49766,49768,49770,49772,49774,49776,49778,49780,49782,49784,49786,49788],{"class":54,"line":238},[52,49747,49285],{"class":58},[52,49749,49288],{"class":62},[52,49751,186],{"class":102},[52,49753,18033],{"class":189},[52,49755,193],{"class":102},[52,49757,115],{"class":83},[52,49759,41783],{"class":87},[52,49761,115],{"class":83},[52,49763,199],{"class":102},[52,49765,14649],{"class":202},[52,49767,80],{"class":79},[52,49769,16771],{"class":83},[52,49771,199],{"class":102},[52,49773,14659],{"class":202},[52,49775,80],{"class":79},[52,49777,115],{"class":83},[52,49779,14666],{"class":87},[52,49781,115],{"class":83},[52,49783,1521],{"class":102},[52,49785,3567],{"class":58},[52,49787,49327],{"class":62},[52,49789,1330],{"class":102},[52,49791,49792,49794,49796,49798,49800,49802,49804,49806],{"class":54,"line":272},[52,49793,49334],{"class":62},[52,49795,80],{"class":79},[52,49797,14688],{"class":62},[52,49799,186],{"class":102},[52,49801,14693],{"class":189},[52,49803,193],{"class":102},[52,49805,49347],{"class":189},[52,49807,211],{"class":102},[52,49809,49810,49812,49814,49816,49818,49820,49822,49824],{"class":54,"line":1348},[52,49811,49354],{"class":62},[52,49813,80],{"class":79},[52,49815,49359],{"class":241},[52,49817,193],{"class":102},[52,49819,14693],{"class":189},[52,49821,199],{"class":102},[52,49823,1450],{"class":160},[52,49825,211],{"class":102},[52,49827,49828],{"class":54,"line":1376},[52,49829,70],{"emptyLinePlaceholder":69},[52,49831,49832,49834,49836,49838,49840],{"class":54,"line":1381},[52,49833,49378],{"class":58},[52,49835,49381],{"class":62},[52,49837,1444],{"class":79},[52,49839,1450],{"class":160},[52,49841,1330],{"class":102},[52,49843,49844],{"class":54,"line":1406},[52,49845,49392],{"class":58},[52,49847,49848],{"class":54,"line":1430},[52,49849,70],{"emptyLinePlaceholder":69},[52,49851,49852,49854,49856,49858,49860],{"class":54,"line":1435},[52,49853,49378],{"class":58},[52,49855,49403],{"class":62},[52,49857,1444],{"class":79},[52,49859,1450],{"class":160},[52,49861,1330],{"class":102},[52,49863,49864,49866,49868,49870,49872,49874,49876,49878],{"class":54,"line":1467},[52,49865,49414],{"class":62},[52,49867,80],{"class":79},[52,49869,14688],{"class":62},[52,49871,186],{"class":102},[52,49873,16805],{"class":189},[52,49875,193],{"class":102},[52,49877,49427],{"class":189},[52,49879,211],{"class":102},[52,49881,49882,49884,49886,49888,49890,49892],{"class":54,"line":1495},[52,49883,49434],{"class":62},[52,49885,186],{"class":102},[52,49887,17629],{"class":189},[52,49889,193],{"class":102},[52,49891,49443],{"class":189},[52,49893,211],{"class":102},[52,49895,49896],{"class":54,"line":1531},[52,49897,70],{"emptyLinePlaceholder":69},[52,49899,49900,49902,49904,49906,49908],{"class":54,"line":1564},[52,49901,11203],{"class":58},[52,49903,14707],{"class":62},[52,49905,11213],{"class":58},[52,49907,14712],{"class":62},[52,49909,1330],{"class":102},[52,49911,49912,49914,49916,49918,49920,49922],{"class":54,"line":1572},[52,49913,49434],{"class":62},[52,49915,186],{"class":102},[52,49917,17629],{"class":189},[52,49919,193],{"class":102},[52,49921,14723],{"class":189},[52,49923,211],{"class":102},[52,49925,49926],{"class":54,"line":1588},[52,49927,70],{"emptyLinePlaceholder":69},[52,49929,49930,49932,49934,49936,49938,49940,49942,49944,49946,49948,49950,49952,49954,49956,49958,49960],{"class":54,"line":1596},[52,49931,242],{"class":241},[52,49933,193],{"class":102},[52,49935,1476],{"class":1475},[52,49937,49490],{"class":87},[52,49939,1482],{"class":121},[52,49941,16152],{"class":241},[52,49943,193],{"class":102},[52,49945,30173],{"class":189},[52,49947,1521],{"class":102},[52,49949,1488],{"class":121},[52,49951,49505],{"class":87},[52,49953,1482],{"class":121},[52,49955,24450],{"class":189},[52,49957,1488],{"class":121},[52,49959,115],{"class":87},[52,49961,211],{"class":102},[14,49963,49964],{},"Step by step, the script does this:",[309,49966,49967,49973,49976,49979,49982,49985],{},[24,49968,3650,49969,49972],{},[49,49970,49971],{},"pathlib.Path"," to find CSV files in a folder",[24,49974,49975],{},"Open the output file in write mode",[24,49977,49978],{},"Loop through each input CSV file",[24,49980,49981],{},"Read the first row as the header",[24,49983,49984],{},"Write the first header only once",[24,49986,49987],{},"Write all remaining rows to the output file",[14,49989,49990,49991,49993,49994,186],{},"If you want to understand the reading side in more detail, see ",[372,49992,14761],{"href":14760},". For the writing side, see ",[372,49995,49996],{"href":17736},"how to write a CSV file in Python",[37,49998,25927],{"id":25926},[639,50000,50002],{"id":50001},"input_folderglobcsv",[49,50003,50004],{},"input_folder.glob(\"*.csv\")",[42,50006,50008],{"className":44,"code":50007,"language":46,"meta":47,"style":47},"csv_files = sorted(input_folder.glob(\"*.csv\"))\n",[49,50009,50010],{"__ignoreMap":47},[52,50011,50012,50014,50016,50018,50020,50022,50024,50026,50028,50030,50032,50034],{"class":54,"line":55},[52,50013,49167],{"class":62},[52,50015,80],{"class":79},[52,50017,49172],{"class":241},[52,50019,193],{"class":102},[52,50021,49177],{"class":189},[52,50023,186],{"class":102},[52,50025,49182],{"class":189},[52,50027,193],{"class":102},[52,50029,115],{"class":83},[52,50031,49189],{"class":87},[52,50033,115],{"class":83},[52,50035,8886],{"class":102},[14,50037,50038,50039,50041,50042,30371],{},"This finds all files ending in ",[49,50040,30164],{}," inside the ",[49,50043,30173],{},[309,50045,50046,50052],{},[24,50047,50048,50051],{},[49,50049,50050],{},"glob(\"*.csv\")"," matches CSV files only",[24,50053,50054,50057],{},[49,50055,50056],{},"sorted(...)"," gives a predictable file order",[14,50059,50060],{},"That means if your folder contains:",[309,50062,50063,50068,50073],{},[24,50064,50065],{},[49,50066,50067],{},"part1.csv",[24,50069,50070],{},[49,50071,50072],{},"part2.csv",[24,50074,50075],{},[49,50076,50077],{},"part3.csv",[14,50079,50080],{},"they will usually be processed in that sorted name order.",[639,50082,50084],{"id":50083},"nextreader-none",[49,50085,50086],{},"next(reader, None)",[42,50088,50090],{"className":44,"code":50089,"language":46,"meta":47,"style":47},"header = next(reader, None)\n",[49,50091,50092],{"__ignoreMap":47},[52,50093,50094,50097,50099,50101,50103,50105,50107,50109],{"class":54,"line":55},[52,50095,50096],{"class":62},"header ",[52,50098,80],{"class":79},[52,50100,49359],{"class":241},[52,50102,193],{"class":102},[52,50104,14693],{"class":189},[52,50106,199],{"class":102},[52,50108,1450],{"class":160},[52,50110,211],{"class":102},[14,50112,50113],{},"This reads the first row from the CSV file.",[309,50115,50116,50121],{},[24,50117,50118,50119],{},"If the file has data, the first row is stored in ",[49,50120,49443],{},[24,50122,50123,50124],{},"If the file is empty, it returns ",[49,50125,18849],{},[14,50127,50128],{},"That is why the script can safely skip empty files:",[42,50130,50132],{"className":44,"code":50131,"language":46,"meta":47,"style":47},"if header is None:\n    continue\n",[49,50133,50134,50146],{"__ignoreMap":47},[52,50135,50136,50138,50140,50142,50144],{"class":54,"line":55},[52,50137,1313],{"class":58},[52,50139,49381],{"class":62},[52,50141,1444],{"class":79},[52,50143,1450],{"class":160},[52,50145,1330],{"class":102},[52,50147,50148],{"class":54,"line":66},[52,50149,50150],{"class":58},"    continue\n",[639,50152,50154],{"id":50153},"writer-is-none",[49,50155,50156],{},"writer is None",[42,50158,50160],{"className":44,"code":50159,"language":46,"meta":47,"style":47},"if writer is None:\n    writer = csv.writer(outfile)\n    writer.writerow(header)\n",[49,50161,50162,50174,50192],{"__ignoreMap":47},[52,50163,50164,50166,50168,50170,50172],{"class":54,"line":55},[52,50165,1313],{"class":58},[52,50167,49403],{"class":62},[52,50169,1444],{"class":79},[52,50171,1450],{"class":160},[52,50173,1330],{"class":102},[52,50175,50176,50178,50180,50182,50184,50186,50188,50190],{"class":54,"line":66},[52,50177,16796],{"class":62},[52,50179,80],{"class":79},[52,50181,14688],{"class":62},[52,50183,186],{"class":102},[52,50185,16805],{"class":189},[52,50187,193],{"class":102},[52,50189,49427],{"class":189},[52,50191,211],{"class":102},[52,50193,50194,50196,50198,50200,50202,50204],{"class":54,"line":73},[52,50195,16816],{"class":62},[52,50197,186],{"class":102},[52,50199,17629],{"class":189},[52,50201,193],{"class":102},[52,50203,49443],{"class":189},[52,50205,211],{"class":102},[14,50207,50208,50209,7514,50211,186],{},"At the start, ",[49,50210,16805],{},[49,50212,18849],{},[14,50214,50215],{},"That lets the script know that it has not written the output header yet. When it reaches the first non-empty file:",[309,50217,50218,50221],{},[24,50219,50220],{},"it creates the CSV writer",[24,50222,50223],{},"it writes that file's header row once",[14,50225,50226,50227,50229,50230,50232],{},"After that, ",[49,50228,16805],{}," is no longer ",[49,50231,18849],{},", so later headers are skipped.",[639,50234,50236],{"id":50235},"newline",[49,50237,16856],{},[42,50239,50241],{"className":44,"code":50240,"language":46,"meta":47,"style":47},"with output_file.open(\"w\", newline=\"\", encoding=\"utf-8\") as outfile:\n",[49,50242,50243],{"__ignoreMap":47},[52,50244,50245,50247,50249,50251,50253,50255,50257,50259,50261,50263,50265,50267,50269,50271,50273,50275,50277,50279,50281,50283,50285,50287],{"class":54,"line":55},[52,50246,14632],{"class":58},[52,50248,49204],{"class":62},[52,50250,186],{"class":102},[52,50252,18033],{"class":189},[52,50254,193],{"class":102},[52,50256,115],{"class":83},[52,50258,16760],{"class":87},[52,50260,115],{"class":83},[52,50262,199],{"class":102},[52,50264,14649],{"class":202},[52,50266,80],{"class":79},[52,50268,16771],{"class":83},[52,50270,199],{"class":102},[52,50272,14659],{"class":202},[52,50274,80],{"class":79},[52,50276,115],{"class":83},[52,50278,14666],{"class":87},[52,50280,115],{"class":83},[52,50282,1521],{"class":102},[52,50284,3567],{"class":58},[52,50286,49243],{"class":62},[52,50288,1330],{"class":102},[14,50290,10294,50291,50293,50294,13338],{},[49,50292,16856],{}," is important when working with the ",[49,50295,14591],{},[14,50297,50298],{},"It helps CSV files be written correctly across operating systems and avoids extra blank lines in some environments.",[14,50300,50301,50302,186],{},"If you want more detail on file opening modes and parameters, see ",[372,50303,50304],{"href":14984},"Python open() explained",[37,50306,50308],{"id":50307},"expected-input-structure","Expected input structure",[14,50310,50311],{},"This example works best when:",[309,50313,50314,50317,50320,50323],{},[24,50315,50316],{},"All files are real CSV files",[24,50318,50319],{},"Each file uses the same column order",[24,50321,50322],{},"Each file usually has the same header row",[24,50324,50325],{},"Files are stored in the folder path used in the script",[14,50327,50328],{},"For example, your folder might look like this:",[42,50330,50333],{"className":50331,"code":50332,"language":955,"meta":47},[953],"project\u002F\n├── merge_csv.py\n└── csv_files\u002F\n    ├── sales_jan.csv\n    ├── sales_feb.csv\n    └── sales_mar.csv\n",[49,50334,50332],{"__ignoreMap":47},[14,50336,50337],{},"And each file might contain data like this:",[42,50339,50342],{"className":50340,"code":50341,"language":14591,"meta":47,"style":47},"language-csv shiki shiki-themes material-theme-lighter github-light github-dark","name,amount\nAlice,100\nBob,200\n",[49,50343,50344,50349,50354],{"__ignoreMap":47},[52,50345,50346],{"class":54,"line":55},[52,50347,50348],{},"name,amount\n",[52,50350,50351],{"class":54,"line":66},[52,50352,50353],{},"Alice,100\n",[52,50355,50356],{"class":54,"line":73},[52,50357,50358],{},"Bob,200\n",[37,50360,13563],{"id":13562},[14,50362,50363],{},"After running the script, you should get:",[309,50365,50366,50369,50372,50375],{},[24,50367,50368],{},"One merged CSV file",[24,50370,50371],{},"One header row at the top",[24,50373,50374],{},"All data rows from the input files below it",[24,50376,50377],{},"Rows in the order of the sorted file names",[14,50379,11845],{},[42,50381,50383],{"className":50340,"code":50382,"language":14591,"meta":47,"style":47},"name,amount\nAlice,100\nBob,200\nCarol,150\nDavid,300\nEve,250\n",[49,50384,50385,50389,50393,50397,50402,50407],{"__ignoreMap":47},[52,50386,50387],{"class":54,"line":55},[52,50388,50348],{},[52,50390,50391],{"class":54,"line":66},[52,50392,50353],{},[52,50394,50395],{"class":54,"line":73},[52,50396,50358],{},[52,50398,50399],{"class":54,"line":94},[52,50400,50401],{},"Carol,150\n",[52,50403,50404],{"class":54,"line":106},[52,50405,50406],{},"David,300\n",[52,50408,50409],{"class":54,"line":128},[52,50410,50411],{},"Eve,250\n",[37,50413,50415],{"id":50414},"common-problems-when-merging-csv-files","Common problems when merging CSV files",[14,50417,50418],{},"Here are some common issues beginners run into.",[639,50420,50422],{"id":50421},"headers-get-repeated-in-the-output-file","Headers get repeated in the output file",[14,50424,50425],{},"This happens when you write the first row from every input file.",[14,50427,50428],{},"To avoid that:",[309,50430,50431,50434],{},[24,50432,50433],{},"write the header only for the first non-empty file",[24,50435,50436],{},"skip headers in all later files",[14,50438,50439],{},"The example does this with:",[42,50441,50442],{"className":44,"code":50159,"language":46,"meta":47,"style":47},[49,50443,50444,50456,50474],{"__ignoreMap":47},[52,50445,50446,50448,50450,50452,50454],{"class":54,"line":55},[52,50447,1313],{"class":58},[52,50449,49403],{"class":62},[52,50451,1444],{"class":79},[52,50453,1450],{"class":160},[52,50455,1330],{"class":102},[52,50457,50458,50460,50462,50464,50466,50468,50470,50472],{"class":54,"line":66},[52,50459,16796],{"class":62},[52,50461,80],{"class":79},[52,50463,14688],{"class":62},[52,50465,186],{"class":102},[52,50467,16805],{"class":189},[52,50469,193],{"class":102},[52,50471,49427],{"class":189},[52,50473,211],{"class":102},[52,50475,50476,50478,50480,50482,50484,50486],{"class":54,"line":73},[52,50477,16816],{"class":62},[52,50479,186],{"class":102},[52,50481,17629],{"class":189},[52,50483,193],{"class":102},[52,50485,49443],{"class":189},[52,50487,211],{"class":102},[639,50489,50491],{"id":50490},"files-have-different-columns","Files have different columns",[14,50493,50494],{},"This simple script assumes every file has the same columns in the same order.",[14,50496,50497],{},"If one file has different headers, the output may become inconsistent.",[14,50499,50500],{},"Common cause:",[309,50502,50503],{},[24,50504,50505],{},"Using CSV files with different headers",[14,50507,50508],{},"A simple improvement is to compare each header to the first one before writing rows.",[639,50510,35657],{"id":50511},"the-folder-path-is-wrong",[14,50513,50514,50515,50517],{},"If Python cannot find the folder or files, ",[49,50516,30173],{}," may be empty.",[14,50519,50500],{},[309,50521,50522],{},[24,50523,31625],{},[14,50525,50526],{},"Useful debug checks:",[42,50528,50530],{"className":44,"code":50529,"language":46,"meta":47,"style":47},"print(csv_files)\nprint(len(csv_files))\nprint(output_file.resolve())\n",[49,50531,50532,50542,50556],{"__ignoreMap":47},[52,50533,50534,50536,50538,50540],{"class":54,"line":55},[52,50535,242],{"class":241},[52,50537,193],{"class":102},[52,50539,30173],{"class":189},[52,50541,211],{"class":102},[52,50543,50544,50546,50548,50550,50552,50554],{"class":54,"line":66},[52,50545,242],{"class":241},[52,50547,193],{"class":102},[52,50549,16152],{"class":241},[52,50551,193],{"class":102},[52,50553,30173],{"class":189},[52,50555,8886],{"class":102},[52,50557,50558,50560,50562,50564,50566,50568],{"class":54,"line":73},[52,50559,242],{"class":241},[52,50561,193],{"class":102},[52,50563,24450],{"class":189},[52,50565,186],{"class":102},[52,50567,31649],{"class":189},[52,50569,1816],{"class":102},[14,50571,50572,50573,186],{},"If you get file path errors, see ",[372,50574,15904],{"href":15903},[639,50576,50578],{"id":50577},"the-script-includes-empty-files","The script includes empty files",[14,50580,50581],{},"An empty file has no header row and no data rows.",[14,50583,50584],{},"This example handles that safely with:",[42,50586,50588],{"className":44,"code":50587,"language":46,"meta":47,"style":47},"header = next(reader, None)\n\nif header is None:\n    continue\n",[49,50589,50590,50608,50612,50624],{"__ignoreMap":47},[52,50591,50592,50594,50596,50598,50600,50602,50604,50606],{"class":54,"line":55},[52,50593,50096],{"class":62},[52,50595,80],{"class":79},[52,50597,49359],{"class":241},[52,50599,193],{"class":102},[52,50601,14693],{"class":189},[52,50603,199],{"class":102},[52,50605,1450],{"class":160},[52,50607,211],{"class":102},[52,50609,50610],{"class":54,"line":66},[52,50611,70],{"emptyLinePlaceholder":69},[52,50613,50614,50616,50618,50620,50622],{"class":54,"line":73},[52,50615,1313],{"class":58},[52,50617,49381],{"class":62},[52,50619,1444],{"class":79},[52,50621,1450],{"class":160},[52,50623,1330],{"class":102},[52,50625,50626],{"class":54,"line":94},[52,50627,50150],{"class":58},[639,50629,50631],{"id":50630},"encoding-problems-cause-read-errors","Encoding problems cause read errors",[14,50633,50634],{},"Some CSV files are not saved as UTF-8.",[14,50636,50500],{},[309,50638,50639],{},[24,50640,50641],{},"Trying to merge files with a different text encoding",[14,50643,50644,50645,50648],{},"If you see decoding errors, check the file encoding or try a different one such as ",[49,50646,50647],{},"\"latin-1\""," if appropriate.",[14,50650,50651,50652,186],{},"Related help: ",[372,50653,50655],{"href":50654},"\u002Ferrors\u002Funicodedecodeerror-utf-8-codec-cant-decode-byte-fix\u002F","UnicodeDecodeError UTF-8 codec can't decode byte fix",[37,50657,50659],{"id":50658},"simple-improvements","Simple improvements",[14,50661,50662],{},"Once the basic script works, you can improve it in small steps.",[14,50664,50665],{},"You could:",[309,50667,50668,50671,50674,50677,50683],{},[24,50669,50670],{},"Check that all headers match before writing rows",[24,50672,50673],{},"Skip files that do not match the first header",[24,50675,50676],{},"Print each file name while processing",[24,50678,3650,50679,50682],{},[49,50680,50681],{},"csv.DictReader"," for column-based handling",[24,50684,50685],{},"Write only selected columns if needed",[14,50687,50688],{},"Here is a slightly safer version that checks headers before merging rows:",[42,50690,50692],{"className":44,"code":50691,"language":46,"meta":47,"style":47},"import csv\nfrom pathlib import Path\n\ninput_folder = Path(\"csv_files\")\noutput_file = Path(\"merged.csv\")\n\ncsv_files = sorted(input_folder.glob(\"*.csv\"))\nexpected_header = None\n\nwith output_file.open(\"w\", newline=\"\", encoding=\"utf-8\") as outfile:\n    writer = None\n\n    for file_path in csv_files:\n        print(f\"Processing: {file_path}\")\n\n        with file_path.open(\"r\", newline=\"\", encoding=\"utf-8\") as infile:\n            reader = csv.reader(infile)\n            header = next(reader, None)\n\n            if header is None:\n                print(\"  Skipped empty file\")\n                continue\n\n            if expected_header is None:\n                expected_header = header\n                writer = csv.writer(outfile)\n                writer.writerow(header)\n            elif header != expected_header:\n                print(\"  Skipped file with different header\")\n                continue\n\n            for row in reader:\n                writer.writerow(row)\n\nprint(f\"Merged files into {output_file}\")\n",[49,50693,50694,50700,50710,50714,50732,50750,50754,50780,50789,50793,50839,50847,50851,50864,50886,50890,50936,50954,50972,50976,50988,51003,51007,51011,51024,51034,51052,51066,51080,51095,51099,51103,51115,51129,51133],{"__ignoreMap":47},[52,50695,50696,50698],{"class":54,"line":55},[52,50697,59],{"class":58},[52,50699,14623],{"class":62},[52,50701,50702,50704,50706,50708],{"class":54,"line":66},[52,50703,25329],{"class":58},[52,50705,28537],{"class":62},[52,50707,59],{"class":58},[52,50709,28542],{"class":62},[52,50711,50712],{"class":54,"line":73},[52,50713,70],{"emptyLinePlaceholder":69},[52,50715,50716,50718,50720,50722,50724,50726,50728,50730],{"class":54,"line":94},[52,50717,49125],{"class":62},[52,50719,80],{"class":79},[52,50721,28563],{"class":189},[52,50723,193],{"class":102},[52,50725,115],{"class":83},[52,50727,30173],{"class":87},[52,50729,115],{"class":83},[52,50731,211],{"class":102},[52,50733,50734,50736,50738,50740,50742,50744,50746,50748],{"class":54,"line":106},[52,50735,24394],{"class":62},[52,50737,80],{"class":79},[52,50739,28563],{"class":189},[52,50741,193],{"class":102},[52,50743,115],{"class":83},[52,50745,49154],{"class":87},[52,50747,115],{"class":83},[52,50749,211],{"class":102},[52,50751,50752],{"class":54,"line":128},[52,50753,70],{"emptyLinePlaceholder":69},[52,50755,50756,50758,50760,50762,50764,50766,50768,50770,50772,50774,50776,50778],{"class":54,"line":148},[52,50757,49167],{"class":62},[52,50759,80],{"class":79},[52,50761,49172],{"class":241},[52,50763,193],{"class":102},[52,50765,49177],{"class":189},[52,50767,186],{"class":102},[52,50769,49182],{"class":189},[52,50771,193],{"class":102},[52,50773,115],{"class":83},[52,50775,49189],{"class":87},[52,50777,115],{"class":83},[52,50779,8886],{"class":102},[52,50781,50782,50785,50787],{"class":54,"line":164},[52,50783,50784],{"class":62},"expected_header ",[52,50786,80],{"class":79},[52,50788,18734],{"class":160},[52,50790,50791],{"class":54,"line":170},[52,50792,70],{"emptyLinePlaceholder":69},[52,50794,50795,50797,50799,50801,50803,50805,50807,50809,50811,50813,50815,50817,50819,50821,50823,50825,50827,50829,50831,50833,50835,50837],{"class":54,"line":175},[52,50796,14632],{"class":58},[52,50798,49204],{"class":62},[52,50800,186],{"class":102},[52,50802,18033],{"class":189},[52,50804,193],{"class":102},[52,50806,115],{"class":83},[52,50808,16760],{"class":87},[52,50810,115],{"class":83},[52,50812,199],{"class":102},[52,50814,14649],{"class":202},[52,50816,80],{"class":79},[52,50818,16771],{"class":83},[52,50820,199],{"class":102},[52,50822,14659],{"class":202},[52,50824,80],{"class":79},[52,50826,115],{"class":83},[52,50828,14666],{"class":87},[52,50830,115],{"class":83},[52,50832,1521],{"class":102},[52,50834,3567],{"class":58},[52,50836,49243],{"class":62},[52,50838,1330],{"class":102},[52,50840,50841,50843,50845],{"class":54,"line":214},[52,50842,16796],{"class":62},[52,50844,80],{"class":79},[52,50846,18734],{"class":160},[52,50848,50849],{"class":54,"line":233},[52,50850,70],{"emptyLinePlaceholder":69},[52,50852,50853,50855,50857,50859,50862],{"class":54,"line":238},[52,50854,11773],{"class":58},[52,50856,49269],{"class":62},[52,50858,11213],{"class":58},[52,50860,50861],{"class":62}," csv_files",[52,50863,1330],{"class":102},[52,50865,50866,50868,50870,50872,50875,50877,50880,50882,50884],{"class":54,"line":272},[52,50867,1470],{"class":241},[52,50869,193],{"class":102},[52,50871,1476],{"class":1475},[52,50873,50874],{"class":87},"\"Processing: ",[52,50876,1482],{"class":121},[52,50878,50879],{"class":189},"file_path",[52,50881,1488],{"class":121},[52,50883,115],{"class":87},[52,50885,211],{"class":102},[52,50887,50888],{"class":54,"line":1348},[52,50889,70],{"emptyLinePlaceholder":69},[52,50891,50892,50894,50896,50898,50900,50902,50904,50906,50908,50910,50912,50914,50916,50918,50920,50922,50924,50926,50928,50930,50932,50934],{"class":54,"line":1376},[52,50893,49285],{"class":58},[52,50895,49288],{"class":62},[52,50897,186],{"class":102},[52,50899,18033],{"class":189},[52,50901,193],{"class":102},[52,50903,115],{"class":83},[52,50905,41783],{"class":87},[52,50907,115],{"class":83},[52,50909,199],{"class":102},[52,50911,14649],{"class":202},[52,50913,80],{"class":79},[52,50915,16771],{"class":83},[52,50917,199],{"class":102},[52,50919,14659],{"class":202},[52,50921,80],{"class":79},[52,50923,115],{"class":83},[52,50925,14666],{"class":87},[52,50927,115],{"class":83},[52,50929,1521],{"class":102},[52,50931,3567],{"class":58},[52,50933,49327],{"class":62},[52,50935,1330],{"class":102},[52,50937,50938,50940,50942,50944,50946,50948,50950,50952],{"class":54,"line":1381},[52,50939,49334],{"class":62},[52,50941,80],{"class":79},[52,50943,14688],{"class":62},[52,50945,186],{"class":102},[52,50947,14693],{"class":189},[52,50949,193],{"class":102},[52,50951,49347],{"class":189},[52,50953,211],{"class":102},[52,50955,50956,50958,50960,50962,50964,50966,50968,50970],{"class":54,"line":1406},[52,50957,49354],{"class":62},[52,50959,80],{"class":79},[52,50961,49359],{"class":241},[52,50963,193],{"class":102},[52,50965,14693],{"class":189},[52,50967,199],{"class":102},[52,50969,1450],{"class":160},[52,50971,211],{"class":102},[52,50973,50974],{"class":54,"line":1430},[52,50975,70],{"emptyLinePlaceholder":69},[52,50977,50978,50980,50982,50984,50986],{"class":54,"line":1435},[52,50979,49378],{"class":58},[52,50981,49381],{"class":62},[52,50983,1444],{"class":79},[52,50985,1450],{"class":160},[52,50987,1330],{"class":102},[52,50989,50990,50992,50994,50996,50999,51001],{"class":54,"line":1467},[52,50991,11227],{"class":241},[52,50993,193],{"class":102},[52,50995,115],{"class":83},[52,50997,50998],{"class":87},"  Skipped empty file",[52,51000,115],{"class":83},[52,51002,211],{"class":102},[52,51004,51005],{"class":54,"line":1495},[52,51006,49392],{"class":58},[52,51008,51009],{"class":54,"line":1531},[52,51010,70],{"emptyLinePlaceholder":69},[52,51012,51013,51015,51018,51020,51022],{"class":54,"line":1564},[52,51014,49378],{"class":58},[52,51016,51017],{"class":62}," expected_header ",[52,51019,1444],{"class":79},[52,51021,1450],{"class":160},[52,51023,1330],{"class":102},[52,51025,51026,51029,51031],{"class":54,"line":1572},[52,51027,51028],{"class":62},"                expected_header ",[52,51030,80],{"class":79},[52,51032,51033],{"class":62}," header\n",[52,51035,51036,51038,51040,51042,51044,51046,51048,51050],{"class":54,"line":1588},[52,51037,49414],{"class":62},[52,51039,80],{"class":79},[52,51041,14688],{"class":62},[52,51043,186],{"class":102},[52,51045,16805],{"class":189},[52,51047,193],{"class":102},[52,51049,49427],{"class":189},[52,51051,211],{"class":102},[52,51053,51054,51056,51058,51060,51062,51064],{"class":54,"line":1596},[52,51055,49434],{"class":62},[52,51057,186],{"class":102},[52,51059,17629],{"class":189},[52,51061,193],{"class":102},[52,51063,49443],{"class":189},[52,51065,211],{"class":102},[52,51067,51068,51071,51073,51075,51078],{"class":54,"line":1613},[52,51069,51070],{"class":58},"            elif",[52,51072,49381],{"class":62},[52,51074,33286],{"class":79},[52,51076,51077],{"class":62}," expected_header",[52,51079,1330],{"class":102},[52,51081,51082,51084,51086,51088,51091,51093],{"class":54,"line":11327},[52,51083,11227],{"class":241},[52,51085,193],{"class":102},[52,51087,115],{"class":83},[52,51089,51090],{"class":87},"  Skipped file with different header",[52,51092,115],{"class":83},[52,51094,211],{"class":102},[52,51096,51097],{"class":54,"line":11334},[52,51098,49392],{"class":58},[52,51100,51101],{"class":54,"line":11350},[52,51102,70],{"emptyLinePlaceholder":69},[52,51104,51105,51107,51109,51111,51113],{"class":54,"line":11355},[52,51106,11203],{"class":58},[52,51108,14707],{"class":62},[52,51110,11213],{"class":58},[52,51112,14712],{"class":62},[52,51114,1330],{"class":102},[52,51116,51117,51119,51121,51123,51125,51127],{"class":54,"line":11373},[52,51118,49434],{"class":62},[52,51120,186],{"class":102},[52,51122,17629],{"class":189},[52,51124,193],{"class":102},[52,51126,14723],{"class":189},[52,51128,211],{"class":102},[52,51130,51131],{"class":54,"line":11393},[52,51132,70],{"emptyLinePlaceholder":69},[52,51134,51135,51137,51139,51141,51144,51146,51148,51150,51152],{"class":54,"line":11406},[52,51136,242],{"class":241},[52,51138,193],{"class":102},[52,51140,1476],{"class":1475},[52,51142,51143],{"class":87},"\"Merged files into ",[52,51145,1482],{"class":121},[52,51147,24450],{"class":189},[52,51149,1488],{"class":121},[52,51151,115],{"class":87},[52,51153,211],{"class":102},[37,51155,51157],{"id":51156},"common-causes","Common causes",[14,51159,51160],{},"Many merge problems come from a few common mistakes:",[309,51162,51163,51165,51168,51170,51172],{},[24,51164,50505],{},[24,51166,51167],{},"Forgetting to skip the header row after the first file",[24,51169,31625],{},[24,51171,50641],{},[24,51173,51174,51175],{},"Opening CSV files without ",[49,51176,16856],{},[37,51178,51180],{"id":51179},"useful-debugging-prints","Useful debugging prints",[14,51182,51183],{},"If your script is not working as expected, these quick checks can help:",[42,51185,51187],{"className":44,"code":51186,"language":46,"meta":47,"style":47},"print(csv_files)\nprint(file_path)\nprint(header)\nprint(len(csv_files))\nprint(output_file.resolve())\n",[49,51188,51189,51199,51209,51219,51233],{"__ignoreMap":47},[52,51190,51191,51193,51195,51197],{"class":54,"line":55},[52,51192,242],{"class":241},[52,51194,193],{"class":102},[52,51196,30173],{"class":189},[52,51198,211],{"class":102},[52,51200,51201,51203,51205,51207],{"class":54,"line":66},[52,51202,242],{"class":241},[52,51204,193],{"class":102},[52,51206,50879],{"class":189},[52,51208,211],{"class":102},[52,51210,51211,51213,51215,51217],{"class":54,"line":73},[52,51212,242],{"class":241},[52,51214,193],{"class":102},[52,51216,49443],{"class":189},[52,51218,211],{"class":102},[52,51220,51221,51223,51225,51227,51229,51231],{"class":54,"line":94},[52,51222,242],{"class":241},[52,51224,193],{"class":102},[52,51226,16152],{"class":241},[52,51228,193],{"class":102},[52,51230,30173],{"class":189},[52,51232,8886],{"class":102},[52,51234,51235,51237,51239,51241,51243,51245],{"class":54,"line":106},[52,51236,242],{"class":241},[52,51238,193],{"class":102},[52,51240,24450],{"class":189},[52,51242,186],{"class":102},[52,51244,31649],{"class":189},[52,51246,1816],{"class":102},[14,51248,51249],{},"These tell you:",[309,51251,51252,51255,51258,51261,51264],{},[24,51253,51254],{},"which files were found",[24,51256,51257],{},"which file is being processed",[24,51259,51260],{},"what header was read",[24,51262,51263],{},"how many files matched",[24,51265,51266],{},"where the output file is being saved",[37,51268,1942],{"id":1941},[639,51270,51272],{"id":51271},"can-i-merge-csv-files-with-different-columns","Can I merge CSV files with different columns?",[14,51274,51275,51276,51278],{},"Yes, but this simple example assumes the same columns in every file. If columns differ, use ",[49,51277,50681],{}," and handle missing fields carefully.",[639,51280,51282],{"id":51281},"why-is-the-header-repeated-more-than-once","Why is the header repeated more than once?",[14,51284,51285],{},"This usually happens when you write the first row from every file. Write the header only for the first non-empty file.",[639,51287,51289],{"id":51288},"what-if-one-of-the-files-is-empty","What if one of the files is empty?",[14,51291,51292,51293,51295],{},"The example uses ",[49,51294,50086],{},", so empty files are skipped safely.",[639,51297,51299],{"id":51298},"can-i-merge-csv-files-from-subfolders-too","Can I merge CSV files from subfolders too?",[14,51301,26892,51302,10598,51305,51307],{},[49,51303,51304],{},"input_folder.rglob(\"*.csv\")",[49,51306,50050],{}," to search recursively.",[37,51309,2005],{"id":2004},[309,51311,51312,51316,51320,51324,51328,51332,51336],{},[24,51313,51314],{},[372,51315,16504],{"href":14760},[24,51317,51318],{},[372,51319,17737],{"href":17736},[24,51321,51322],{},[372,51323,17960],{"href":17959},[24,51325,51326],{},[372,51327,16533],{"href":16532},[24,51329,51330],{},[372,51331,50304],{"href":14984},[24,51333,51334],{},[372,51335,15904],{"href":15903},[24,51337,51338],{},[372,51339,50655],{"href":50654},[14,51341,51342],{},"If this example helped you get started, the next step is to learn the task-focused guides for reading, writing, and troubleshooting CSV files.",[2034,51344,51345],{},"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 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 .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 .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);}",{"title":47,"searchDepth":66,"depth":66,"links":51347},[51348,51349,51350,51351,51352,51358,51359,51360,51367,51368,51369,51370,51376],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":25133,"depth":66,"text":25134},{"id":25640,"depth":66,"text":25641},{"id":25926,"depth":66,"text":25927,"children":51353},[51354,51355,51356,51357],{"id":50001,"depth":73,"text":50004},{"id":50083,"depth":73,"text":50086},{"id":50153,"depth":73,"text":50156},{"id":50235,"depth":73,"text":16856},{"id":50307,"depth":66,"text":50308},{"id":13562,"depth":66,"text":13563},{"id":50414,"depth":66,"text":50415,"children":51361},[51362,51363,51364,51365,51366],{"id":50421,"depth":73,"text":50422},{"id":50490,"depth":73,"text":50491},{"id":50511,"depth":73,"text":35657},{"id":50577,"depth":73,"text":50578},{"id":50630,"depth":73,"text":50631},{"id":50658,"depth":66,"text":50659},{"id":51156,"depth":66,"text":51157},{"id":51179,"depth":66,"text":51180},{"id":1941,"depth":66,"text":1942,"children":51371},[51372,51373,51374,51375],{"id":51271,"depth":73,"text":51272},{"id":51281,"depth":73,"text":51282},{"id":51288,"depth":73,"text":51289},{"id":51298,"depth":73,"text":51299},{"id":2004,"depth":66,"text":2005},"Master python merge csv files example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-merge-csv-files-example",{"title":49062,"description":51377},"examples\u002Fpython-merge-csv-files-example","CtieVCCFatwDqX_RN-6tMJ_wYrs0V2ri6Hjzjq4n4aw",{"id":51384,"title":51385,"body":51386,"description":53407,"extension":2075,"meta":53408,"navigation":69,"path":53409,"seo":53410,"stem":53411,"__hash__":53412},"content\u002Fexamples\u002Fpython-number-guessing-game-example.md","Python Number Guessing Game Example",{"type":7,"value":51387,"toc":53373},[51388,51391,51394,51397,51399,51402,51561,51564,51608,51610,51613,51629,51633,51636,51650,51653,51657,51660,51810,51814,51825,51831,51839,51843,51870,51876,51889,51893,51921,51923,51937,51946,51950,51963,51966,51974,51978,52060,52063,52080,52087,52091,52100,52105,52113,52117,52120,52134,52137,52287,52290,52296,52300,52303,52307,52310,52499,52503,52512,52718,52722,52725,52966,52970,52973,52977,52980,52984,52987,53005,53007,53035,53041,53044,53060,53064,53069,53072,53103,53105,53140,53144,53149,53151,53186,53189,53191,53226,53231,53233,53242,53245,53255,53259,53262,53297,53299,53303,53311,53315,53320,53324,53331,53335,53338,53340,53367,53370],[10,51389,51385],{"id":51390},"python-number-guessing-game-example",[14,51392,51393],{},"A number guessing game is a great beginner project because it combines several core Python ideas in one small program.",[14,51395,51396],{},"In this example, Python picks a random number, asks the user to guess it, and keeps running until the correct answer is entered. The goal here is not just to copy the code, but to understand how each part works.",[37,51398,40],{"id":39},[14,51400,51401],{},"This is the shortest working version:",[42,51403,51405],{"className":44,"code":51404,"language":46,"meta":47,"style":47},"import random\n\nsecret_number = random.randint(1, 10)\n\nwhile True:\n    guess = int(input(\"Guess a number from 1 to 10: \"))\n\n    if guess == secret_number:\n        print(\"You guessed it!\")\n        break\n    elif guess \u003C secret_number:\n        print(\"Too low\")\n    else:\n        print(\"Too high\")\n",[49,51406,51407,51413,51417,51440,51444,51452,51476,51480,51494,51509,51513,51525,51540,51546],{"__ignoreMap":47},[52,51408,51409,51411],{"class":54,"line":55},[52,51410,59],{"class":58},[52,51412,36646],{"class":62},[52,51414,51415],{"class":54,"line":66},[52,51416,70],{"emptyLinePlaceholder":69},[52,51418,51419,51422,51424,51426,51428,51430,51432,51434,51436,51438],{"class":54,"line":73},[52,51420,51421],{"class":62},"secret_number ",[52,51423,80],{"class":79},[52,51425,36659],{"class":62},[52,51427,186],{"class":102},[52,51429,36664],{"class":189},[52,51431,193],{"class":102},[52,51433,9200],{"class":121},[52,51435,199],{"class":102},[52,51437,27053],{"class":121},[52,51439,211],{"class":102},[52,51441,51442],{"class":54,"line":94},[52,51443,70],{"emptyLinePlaceholder":69},[52,51445,51446,51448,51450],{"class":54,"line":106},[52,51447,10772],{"class":58},[52,51449,10943],{"class":160},[52,51451,1330],{"class":102},[52,51453,51454,51457,51459,51461,51463,51465,51467,51469,51472,51474],{"class":54,"line":128},[52,51455,51456],{"class":62},"    guess ",[52,51458,80],{"class":79},[52,51460,9789],{"class":4733},[52,51462,193],{"class":102},[52,51464,13610],{"class":241},[52,51466,193],{"class":102},[52,51468,115],{"class":83},[52,51470,51471],{"class":87},"Guess a number from 1 to 10: ",[52,51473,115],{"class":83},[52,51475,8886],{"class":102},[52,51477,51478],{"class":54,"line":148},[52,51479,70],{"emptyLinePlaceholder":69},[52,51481,51482,51484,51487,51489,51492],{"class":54,"line":164},[52,51483,1438],{"class":58},[52,51485,51486],{"class":62}," guess ",[52,51488,11062],{"class":79},[52,51490,51491],{"class":62}," secret_number",[52,51493,1330],{"class":102},[52,51495,51496,51498,51500,51502,51505,51507],{"class":54,"line":170},[52,51497,1470],{"class":241},[52,51499,193],{"class":102},[52,51501,115],{"class":83},[52,51503,51504],{"class":87},"You guessed it!",[52,51506,115],{"class":83},[52,51508,211],{"class":102},[52,51510,51511],{"class":54,"line":175},[52,51512,11499],{"class":58},[52,51514,51515,51517,51519,51521,51523],{"class":54,"line":214},[52,51516,11151],{"class":58},[52,51518,51486],{"class":62},[52,51520,15294],{"class":79},[52,51522,51491],{"class":62},[52,51524,1330],{"class":102},[52,51526,51527,51529,51531,51533,51536,51538],{"class":54,"line":233},[52,51528,1470],{"class":241},[52,51530,193],{"class":102},[52,51532,115],{"class":83},[52,51534,51535],{"class":87},"Too low",[52,51537,115],{"class":83},[52,51539,211],{"class":102},[52,51541,51542,51544],{"class":54,"line":238},[52,51543,1567],{"class":58},[52,51545,1330],{"class":102},[52,51547,51548,51550,51552,51554,51557,51559],{"class":54,"line":272},[52,51549,1470],{"class":241},[52,51551,193],{"class":102},[52,51553,115],{"class":83},[52,51555,51556],{"class":87},"Too high",[52,51558,115],{"class":83},[52,51560,211],{"class":102},[14,51562,51563],{},"This version uses:",[309,51565,51566,51573,51580,51587,51594,51603],{},[24,51567,51568,51572],{},[372,51569,51570],{"href":36826},[49,51571,37871],{}," to choose a secret number",[24,51574,51575,51579],{},[372,51576,51577],{"href":10606},[49,51578,10597],{}," to get the user's guess",[24,51581,51582,51586],{},[372,51583,51584],{"href":9489},[49,51585,9492],{}," to turn text into a number",[24,51588,32056,51589,51593],{},[372,51590,51591,12570],{"href":13425},[49,51592,10772],{}," to keep asking",[24,51595,51596,2587,51598,11556,51600,51602],{},[49,51597,1313],{},[49,51599,10787],{},[49,51601,1591],{}," to check the guess",[24,51604,51605,51607],{},[49,51606,10914],{}," to stop the loop when the user wins",[37,51609,7216],{"id":7215},[14,51611,51612],{},"This project helps you practice how to:",[309,51614,51615,51618,51623,51626],{},[24,51616,51617],{},"combine input, loops, and conditions in one small project",[24,51619,51620,51621],{},"generate a random number with ",[49,51622,37871],{},[24,51624,51625],{},"compare a user guess with a secret number",[24,51627,51628],{},"stop a loop when the correct answer is found",[37,51630,51632],{"id":51631},"how-the-game-works","How the game works",[14,51634,51635],{},"The program follows a simple pattern:",[21,51637,51638,51641,51644,51647],{},[24,51639,51640],{},"The program picks a random number.",[24,51642,51643],{},"The user enters a guess.",[24,51645,51646],{},"The program checks whether the guess is too low, too high, or correct.",[24,51648,51649],{},"The loop repeats until the user guesses correctly.",[14,51651,51652],{},"That makes this a good first project because each step is easy to follow.",[37,51654,51656],{"id":51655},"code-walkthrough","Code walkthrough",[14,51658,51659],{},"Here is the same program again:",[42,51661,51662],{"className":44,"code":51404,"language":46,"meta":47,"style":47},[49,51663,51664,51670,51674,51696,51700,51708,51730,51734,51746,51760,51764,51776,51790,51796],{"__ignoreMap":47},[52,51665,51666,51668],{"class":54,"line":55},[52,51667,59],{"class":58},[52,51669,36646],{"class":62},[52,51671,51672],{"class":54,"line":66},[52,51673,70],{"emptyLinePlaceholder":69},[52,51675,51676,51678,51680,51682,51684,51686,51688,51690,51692,51694],{"class":54,"line":73},[52,51677,51421],{"class":62},[52,51679,80],{"class":79},[52,51681,36659],{"class":62},[52,51683,186],{"class":102},[52,51685,36664],{"class":189},[52,51687,193],{"class":102},[52,51689,9200],{"class":121},[52,51691,199],{"class":102},[52,51693,27053],{"class":121},[52,51695,211],{"class":102},[52,51697,51698],{"class":54,"line":94},[52,51699,70],{"emptyLinePlaceholder":69},[52,51701,51702,51704,51706],{"class":54,"line":106},[52,51703,10772],{"class":58},[52,51705,10943],{"class":160},[52,51707,1330],{"class":102},[52,51709,51710,51712,51714,51716,51718,51720,51722,51724,51726,51728],{"class":54,"line":128},[52,51711,51456],{"class":62},[52,51713,80],{"class":79},[52,51715,9789],{"class":4733},[52,51717,193],{"class":102},[52,51719,13610],{"class":241},[52,51721,193],{"class":102},[52,51723,115],{"class":83},[52,51725,51471],{"class":87},[52,51727,115],{"class":83},[52,51729,8886],{"class":102},[52,51731,51732],{"class":54,"line":148},[52,51733,70],{"emptyLinePlaceholder":69},[52,51735,51736,51738,51740,51742,51744],{"class":54,"line":164},[52,51737,1438],{"class":58},[52,51739,51486],{"class":62},[52,51741,11062],{"class":79},[52,51743,51491],{"class":62},[52,51745,1330],{"class":102},[52,51747,51748,51750,51752,51754,51756,51758],{"class":54,"line":170},[52,51749,1470],{"class":241},[52,51751,193],{"class":102},[52,51753,115],{"class":83},[52,51755,51504],{"class":87},[52,51757,115],{"class":83},[52,51759,211],{"class":102},[52,51761,51762],{"class":54,"line":175},[52,51763,11499],{"class":58},[52,51765,51766,51768,51770,51772,51774],{"class":54,"line":214},[52,51767,11151],{"class":58},[52,51769,51486],{"class":62},[52,51771,15294],{"class":79},[52,51773,51491],{"class":62},[52,51775,1330],{"class":102},[52,51777,51778,51780,51782,51784,51786,51788],{"class":54,"line":233},[52,51779,1470],{"class":241},[52,51781,193],{"class":102},[52,51783,115],{"class":83},[52,51785,51535],{"class":87},[52,51787,115],{"class":83},[52,51789,211],{"class":102},[52,51791,51792,51794],{"class":54,"line":238},[52,51793,1567],{"class":58},[52,51795,1330],{"class":102},[52,51797,51798,51800,51802,51804,51806,51808],{"class":54,"line":272},[52,51799,1470],{"class":241},[52,51801,193],{"class":102},[52,51803,115],{"class":83},[52,51805,51556],{"class":87},[52,51807,115],{"class":83},[52,51809,211],{"class":102},[639,51811,51813],{"id":51812},"import-the-random-module","Import the random module",[42,51815,51817],{"className":44,"code":51816,"language":46,"meta":47,"style":47},"import random\n",[49,51818,51819],{"__ignoreMap":47},[52,51820,51821,51823],{"class":54,"line":55},[52,51822,59],{"class":58},[52,51824,36646],{"class":62},[14,51826,51827,51828,51830],{},"Python needs the ",[49,51829,36610],{}," module before it can use random number tools.",[14,51832,51833,51834,51836,51837,186],{},"If you forget this line, the program will fail when it reaches ",[49,51835,37871],{},". For a broader overview, see the ",[372,51838,36730],{"href":36729},[639,51840,51842],{"id":51841},"store-the-secret-number-in-a-variable","Store the secret number in a variable",[42,51844,51846],{"className":44,"code":51845,"language":46,"meta":47,"style":47},"secret_number = random.randint(1, 10)\n",[49,51847,51848],{"__ignoreMap":47},[52,51849,51850,51852,51854,51856,51858,51860,51862,51864,51866,51868],{"class":54,"line":55},[52,51851,51421],{"class":62},[52,51853,80],{"class":79},[52,51855,36659],{"class":62},[52,51857,186],{"class":102},[52,51859,36664],{"class":189},[52,51861,193],{"class":102},[52,51863,9200],{"class":121},[52,51865,199],{"class":102},[52,51867,27053],{"class":121},[52,51869,211],{"class":102},[14,51871,51872,51873,186],{},"This line creates a random integer from 1 to 10 and stores it in ",[49,51874,51875],{},"secret_number",[309,51877,51878,51883,51886],{},[24,51879,51880,51882],{},[49,51881,36743],{}," includes both 1 and 10",[24,51884,51885],{},"the value stays the same for the whole game",[24,51887,51888],{},"the user does not see this number",[639,51890,51892],{"id":51891},"use-input-to-get-a-guess","Use input() to get a guess",[42,51894,51896],{"className":44,"code":51895,"language":46,"meta":47,"style":47},"guess = int(input(\"Guess a number from 1 to 10: \"))\n",[49,51897,51898],{"__ignoreMap":47},[52,51899,51900,51903,51905,51907,51909,51911,51913,51915,51917,51919],{"class":54,"line":55},[52,51901,51902],{"class":62},"guess ",[52,51904,80],{"class":79},[52,51906,9789],{"class":4733},[52,51908,193],{"class":102},[52,51910,13610],{"class":241},[52,51912,193],{"class":102},[52,51914,115],{"class":83},[52,51916,51471],{"class":87},[52,51918,115],{"class":83},[52,51920,8886],{"class":102},[14,51922,11814],{},[21,51924,51925,51931],{},[24,51926,51927,51930],{},[49,51928,51929],{},"input(...)"," shows a message and waits for the user to type something",[24,51932,51933,51936],{},[49,51934,51935],{},"int(...)"," converts that text into a whole number",[14,51938,51939,51940,51942,51943,186],{},"This matters because ",[49,51941,10597],{}," always returns text. If you want more help with this step, see ",[372,51944,51945],{"href":13736},"how to convert user input to numbers in Python",[639,51947,51949],{"id":51948},"use-a-while-loop-to-keep-asking","Use a while loop to keep asking",[42,51951,51953],{"className":44,"code":51952,"language":46,"meta":47,"style":47},"while True:\n",[49,51954,51955],{"__ignoreMap":47},[52,51956,51957,51959,51961],{"class":54,"line":55},[52,51958,10772],{"class":58},[52,51960,10943],{"class":160},[52,51962,1330],{"class":102},[14,51964,51965],{},"This creates a loop that runs again and again.",[14,51967,14459,51968,51970,51971,51973],{},[49,51969,6233],{}," is always true, the loop keeps going until something stops it. In this program, ",[49,51972,10914],{}," stops it when the guess is correct.",[639,51975,51977],{"id":51976},"use-if-elif-and-else-to-compare-the-guess","Use if, elif, and else to compare the guess",[42,51979,51981],{"className":44,"code":51980,"language":46,"meta":47,"style":47},"if guess == secret_number:\n    print(\"You guessed it!\")\n    break\nelif guess \u003C secret_number:\n    print(\"Too low\")\nelse:\n    print(\"Too high\")\n",[49,51982,51983,51995,52009,52014,52026,52040,52046],{"__ignoreMap":47},[52,51984,51985,51987,51989,51991,51993],{"class":54,"line":55},[52,51986,1313],{"class":58},[52,51988,51486],{"class":62},[52,51990,11062],{"class":79},[52,51992,51491],{"class":62},[52,51994,1330],{"class":102},[52,51996,51997,51999,52001,52003,52005,52007],{"class":54,"line":66},[52,51998,1599],{"class":241},[52,52000,193],{"class":102},[52,52002,115],{"class":83},[52,52004,51504],{"class":87},[52,52006,115],{"class":83},[52,52008,211],{"class":102},[52,52010,52011],{"class":54,"line":73},[52,52012,52013],{"class":58},"    break\n",[52,52015,52016,52018,52020,52022,52024],{"class":54,"line":94},[52,52017,10787],{"class":58},[52,52019,51486],{"class":62},[52,52021,15294],{"class":79},[52,52023,51491],{"class":62},[52,52025,1330],{"class":102},[52,52027,52028,52030,52032,52034,52036,52038],{"class":54,"line":106},[52,52029,1599],{"class":241},[52,52031,193],{"class":102},[52,52033,115],{"class":83},[52,52035,51535],{"class":87},[52,52037,115],{"class":83},[52,52039,211],{"class":102},[52,52041,52042,52044],{"class":54,"line":128},[52,52043,1591],{"class":58},[52,52045,1330],{"class":102},[52,52047,52048,52050,52052,52054,52056,52058],{"class":54,"line":148},[52,52049,1599],{"class":241},[52,52051,193],{"class":102},[52,52053,115],{"class":83},[52,52055,51556],{"class":87},[52,52057,115],{"class":83},[52,52059,211],{"class":102},[14,52061,52062],{},"This comparison block checks three cases:",[309,52064,52065,52068,52074],{},[24,52066,52067],{},"if the guess matches the secret number, the player wins",[24,52069,52070,52071],{},"if the guess is smaller, the program says ",[49,52072,52073],{},"\"Too low\"",[24,52075,52076,52077],{},"otherwise, the guess must be bigger, so it says ",[49,52078,52079],{},"\"Too high\"",[14,52081,52082,52083,186],{},"If you want a full beginner explanation of this pattern, see ",[372,52084,52086],{"href":52085},"\u002Flearn\u002Fpython-if-else-and-elif-explained\u002F","Python if, else, and elif explained",[639,52088,52090],{"id":52089},"use-break-to-end-the-loop","Use break to end the loop",[42,52092,52094],{"className":44,"code":52093,"language":46,"meta":47,"style":47},"break\n",[49,52095,52096],{"__ignoreMap":47},[52,52097,52098],{"class":54,"line":55},[52,52099,52093],{"class":58},[14,52101,5788,52102,52104],{},[49,52103,10914],{},", the loop would continue forever, even after the correct guess.",[14,52106,52107,52108,52110,52111,27610],{},"That is why ",[49,52109,10914],{}," must be inside the correct ",[49,52112,1313],{},[37,52114,52116],{"id":52115},"beginner-version-to-show-first","Beginner version to show first",[14,52118,52119],{},"When you build your first guessing game, keep it simple:",[309,52121,52122,52125,52128,52131],{},[24,52123,52124],{},"use a small range such as 1 to 10",[24,52126,52127],{},"use one loop and one comparison block",[24,52129,52130],{},"avoid advanced features like functions at first",[24,52132,52133],{},"test the program after every small change",[14,52135,52136],{},"Here is a beginner-friendly version again:",[42,52138,52139],{"className":44,"code":51404,"language":46,"meta":47,"style":47},[49,52140,52141,52147,52151,52173,52177,52185,52207,52211,52223,52237,52241,52253,52267,52273],{"__ignoreMap":47},[52,52142,52143,52145],{"class":54,"line":55},[52,52144,59],{"class":58},[52,52146,36646],{"class":62},[52,52148,52149],{"class":54,"line":66},[52,52150,70],{"emptyLinePlaceholder":69},[52,52152,52153,52155,52157,52159,52161,52163,52165,52167,52169,52171],{"class":54,"line":73},[52,52154,51421],{"class":62},[52,52156,80],{"class":79},[52,52158,36659],{"class":62},[52,52160,186],{"class":102},[52,52162,36664],{"class":189},[52,52164,193],{"class":102},[52,52166,9200],{"class":121},[52,52168,199],{"class":102},[52,52170,27053],{"class":121},[52,52172,211],{"class":102},[52,52174,52175],{"class":54,"line":94},[52,52176,70],{"emptyLinePlaceholder":69},[52,52178,52179,52181,52183],{"class":54,"line":106},[52,52180,10772],{"class":58},[52,52182,10943],{"class":160},[52,52184,1330],{"class":102},[52,52186,52187,52189,52191,52193,52195,52197,52199,52201,52203,52205],{"class":54,"line":128},[52,52188,51456],{"class":62},[52,52190,80],{"class":79},[52,52192,9789],{"class":4733},[52,52194,193],{"class":102},[52,52196,13610],{"class":241},[52,52198,193],{"class":102},[52,52200,115],{"class":83},[52,52202,51471],{"class":87},[52,52204,115],{"class":83},[52,52206,8886],{"class":102},[52,52208,52209],{"class":54,"line":148},[52,52210,70],{"emptyLinePlaceholder":69},[52,52212,52213,52215,52217,52219,52221],{"class":54,"line":164},[52,52214,1438],{"class":58},[52,52216,51486],{"class":62},[52,52218,11062],{"class":79},[52,52220,51491],{"class":62},[52,52222,1330],{"class":102},[52,52224,52225,52227,52229,52231,52233,52235],{"class":54,"line":170},[52,52226,1470],{"class":241},[52,52228,193],{"class":102},[52,52230,115],{"class":83},[52,52232,51504],{"class":87},[52,52234,115],{"class":83},[52,52236,211],{"class":102},[52,52238,52239],{"class":54,"line":175},[52,52240,11499],{"class":58},[52,52242,52243,52245,52247,52249,52251],{"class":54,"line":214},[52,52244,11151],{"class":58},[52,52246,51486],{"class":62},[52,52248,15294],{"class":79},[52,52250,51491],{"class":62},[52,52252,1330],{"class":102},[52,52254,52255,52257,52259,52261,52263,52265],{"class":54,"line":233},[52,52256,1470],{"class":241},[52,52258,193],{"class":102},[52,52260,115],{"class":83},[52,52262,51535],{"class":87},[52,52264,115],{"class":83},[52,52266,211],{"class":102},[52,52268,52269,52271],{"class":54,"line":238},[52,52270,1567],{"class":58},[52,52272,1330],{"class":102},[52,52274,52275,52277,52279,52281,52283,52285],{"class":54,"line":272},[52,52276,1470],{"class":241},[52,52278,193],{"class":102},[52,52280,115],{"class":83},[52,52282,51556],{"class":87},[52,52284,115],{"class":83},[52,52286,211],{"class":102},[14,52288,52289],{},"Example run:",[42,52291,52294],{"className":52292,"code":52293,"language":955,"meta":47},[953],"Guess a number from 1 to 10: 4\nToo low\nGuess a number from 1 to 10: 8\nToo high\nGuess a number from 1 to 10: 6\nYou guessed it!\n",[49,52295,52293],{"__ignoreMap":47},[37,52297,52299],{"id":52298},"useful-improvements","Useful improvements",[14,52301,52302],{},"Once the basic game works, you can improve it in small steps.",[639,52304,52306],{"id":52305},"count-the-number-of-guesses","Count the number of guesses",[14,52308,52309],{},"This version tracks how many times the user guessed:",[42,52311,52313],{"className":44,"code":52312,"language":46,"meta":47,"style":47},"import random\n\nsecret_number = random.randint(1, 10)\nguess_count = 0\n\nwhile True:\n    guess = int(input(\"Guess a number from 1 to 10: \"))\n    guess_count += 1\n\n    if guess == secret_number:\n        print(\"You guessed it!\")\n        print(\"Guesses:\", guess_count)\n        break\n    elif guess \u003C secret_number:\n        print(\"Too low\")\n    else:\n        print(\"Too high\")\n",[49,52314,52315,52321,52325,52347,52356,52360,52368,52390,52399,52403,52415,52429,52449,52453,52465,52479,52485],{"__ignoreMap":47},[52,52316,52317,52319],{"class":54,"line":55},[52,52318,59],{"class":58},[52,52320,36646],{"class":62},[52,52322,52323],{"class":54,"line":66},[52,52324,70],{"emptyLinePlaceholder":69},[52,52326,52327,52329,52331,52333,52335,52337,52339,52341,52343,52345],{"class":54,"line":73},[52,52328,51421],{"class":62},[52,52330,80],{"class":79},[52,52332,36659],{"class":62},[52,52334,186],{"class":102},[52,52336,36664],{"class":189},[52,52338,193],{"class":102},[52,52340,9200],{"class":121},[52,52342,199],{"class":102},[52,52344,27053],{"class":121},[52,52346,211],{"class":102},[52,52348,52349,52352,52354],{"class":54,"line":94},[52,52350,52351],{"class":62},"guess_count ",[52,52353,80],{"class":79},[52,52355,34598],{"class":121},[52,52357,52358],{"class":54,"line":106},[52,52359,70],{"emptyLinePlaceholder":69},[52,52361,52362,52364,52366],{"class":54,"line":128},[52,52363,10772],{"class":58},[52,52365,10943],{"class":160},[52,52367,1330],{"class":102},[52,52369,52370,52372,52374,52376,52378,52380,52382,52384,52386,52388],{"class":54,"line":148},[52,52371,51456],{"class":62},[52,52373,80],{"class":79},[52,52375,9789],{"class":4733},[52,52377,193],{"class":102},[52,52379,13610],{"class":241},[52,52381,193],{"class":102},[52,52383,115],{"class":83},[52,52385,51471],{"class":87},[52,52387,115],{"class":83},[52,52389,8886],{"class":102},[52,52391,52392,52395,52397],{"class":54,"line":164},[52,52393,52394],{"class":62},"    guess_count ",[52,52396,34682],{"class":79},[52,52398,8645],{"class":121},[52,52400,52401],{"class":54,"line":170},[52,52402,70],{"emptyLinePlaceholder":69},[52,52404,52405,52407,52409,52411,52413],{"class":54,"line":175},[52,52406,1438],{"class":58},[52,52408,51486],{"class":62},[52,52410,11062],{"class":79},[52,52412,51491],{"class":62},[52,52414,1330],{"class":102},[52,52416,52417,52419,52421,52423,52425,52427],{"class":54,"line":214},[52,52418,1470],{"class":241},[52,52420,193],{"class":102},[52,52422,115],{"class":83},[52,52424,51504],{"class":87},[52,52426,115],{"class":83},[52,52428,211],{"class":102},[52,52430,52431,52433,52435,52437,52440,52442,52444,52447],{"class":54,"line":233},[52,52432,1470],{"class":241},[52,52434,193],{"class":102},[52,52436,115],{"class":83},[52,52438,52439],{"class":87},"Guesses:",[52,52441,115],{"class":83},[52,52443,199],{"class":102},[52,52445,52446],{"class":189}," guess_count",[52,52448,211],{"class":102},[52,52450,52451],{"class":54,"line":238},[52,52452,11499],{"class":58},[52,52454,52455,52457,52459,52461,52463],{"class":54,"line":272},[52,52456,11151],{"class":58},[52,52458,51486],{"class":62},[52,52460,15294],{"class":79},[52,52462,51491],{"class":62},[52,52464,1330],{"class":102},[52,52466,52467,52469,52471,52473,52475,52477],{"class":54,"line":1348},[52,52468,1470],{"class":241},[52,52470,193],{"class":102},[52,52472,115],{"class":83},[52,52474,51535],{"class":87},[52,52476,115],{"class":83},[52,52478,211],{"class":102},[52,52480,52481,52483],{"class":54,"line":1376},[52,52482,1567],{"class":58},[52,52484,1330],{"class":102},[52,52486,52487,52489,52491,52493,52495,52497],{"class":54,"line":1381},[52,52488,1470],{"class":241},[52,52490,193],{"class":102},[52,52492,115],{"class":83},[52,52494,51556],{"class":87},[52,52496,115],{"class":83},[52,52498,211],{"class":102},[639,52500,52502],{"id":52501},"validate-input-to-avoid-crashes","Validate input to avoid crashes",[14,52504,52505,52506,52508,52509,52511],{},"If the user types letters, ",[49,52507,9492],{}," raises a ",[49,52510,13780],{},". This safer version checks the input first:",[42,52513,52515],{"className":44,"code":52514,"language":46,"meta":47,"style":47},"import random\n\nsecret_number = random.randint(1, 10)\n\nwhile True:\n    user_text = input(\"Guess a number from 1 to 10: \")\n\n    if not user_text.isdigit():\n        print(\"Please enter a whole number.\")\n        continue\n\n    guess = int(user_text)\n\n    if guess == secret_number:\n        print(\"You guessed it!\")\n        break\n    elif guess \u003C secret_number:\n        print(\"Too low\")\n    else:\n        print(\"Too high\")\n",[49,52516,52517,52523,52527,52549,52553,52561,52580,52584,52600,52615,52619,52623,52638,52642,52654,52668,52672,52684,52698,52704],{"__ignoreMap":47},[52,52518,52519,52521],{"class":54,"line":55},[52,52520,59],{"class":58},[52,52522,36646],{"class":62},[52,52524,52525],{"class":54,"line":66},[52,52526,70],{"emptyLinePlaceholder":69},[52,52528,52529,52531,52533,52535,52537,52539,52541,52543,52545,52547],{"class":54,"line":73},[52,52530,51421],{"class":62},[52,52532,80],{"class":79},[52,52534,36659],{"class":62},[52,52536,186],{"class":102},[52,52538,36664],{"class":189},[52,52540,193],{"class":102},[52,52542,9200],{"class":121},[52,52544,199],{"class":102},[52,52546,27053],{"class":121},[52,52548,211],{"class":102},[52,52550,52551],{"class":54,"line":94},[52,52552,70],{"emptyLinePlaceholder":69},[52,52554,52555,52557,52559],{"class":54,"line":106},[52,52556,10772],{"class":58},[52,52558,10943],{"class":160},[52,52560,1330],{"class":102},[52,52562,52563,52566,52568,52570,52572,52574,52576,52578],{"class":54,"line":128},[52,52564,52565],{"class":62},"    user_text ",[52,52567,80],{"class":79},[52,52569,11037],{"class":241},[52,52571,193],{"class":102},[52,52573,115],{"class":83},[52,52575,51471],{"class":87},[52,52577,115],{"class":83},[52,52579,211],{"class":102},[52,52581,52582],{"class":54,"line":148},[52,52583,70],{"emptyLinePlaceholder":69},[52,52585,52586,52588,52590,52593,52595,52598],{"class":54,"line":164},[52,52587,1438],{"class":58},[52,52589,1447],{"class":79},[52,52591,52592],{"class":62}," user_text",[52,52594,186],{"class":102},[52,52596,52597],{"class":189},"isdigit",[52,52599,8146],{"class":102},[52,52601,52602,52604,52606,52608,52611,52613],{"class":54,"line":170},[52,52603,1470],{"class":241},[52,52605,193],{"class":102},[52,52607,115],{"class":83},[52,52609,52610],{"class":87},"Please enter a whole number.",[52,52612,115],{"class":83},[52,52614,211],{"class":102},[52,52616,52617],{"class":54,"line":175},[52,52618,18703],{"class":58},[52,52620,52621],{"class":54,"line":214},[52,52622,70],{"emptyLinePlaceholder":69},[52,52624,52625,52627,52629,52631,52633,52636],{"class":54,"line":233},[52,52626,51456],{"class":62},[52,52628,80],{"class":79},[52,52630,9789],{"class":4733},[52,52632,193],{"class":102},[52,52634,52635],{"class":189},"user_text",[52,52637,211],{"class":102},[52,52639,52640],{"class":54,"line":238},[52,52641,70],{"emptyLinePlaceholder":69},[52,52643,52644,52646,52648,52650,52652],{"class":54,"line":272},[52,52645,1438],{"class":58},[52,52647,51486],{"class":62},[52,52649,11062],{"class":79},[52,52651,51491],{"class":62},[52,52653,1330],{"class":102},[52,52655,52656,52658,52660,52662,52664,52666],{"class":54,"line":1348},[52,52657,1470],{"class":241},[52,52659,193],{"class":102},[52,52661,115],{"class":83},[52,52663,51504],{"class":87},[52,52665,115],{"class":83},[52,52667,211],{"class":102},[52,52669,52670],{"class":54,"line":1376},[52,52671,11499],{"class":58},[52,52673,52674,52676,52678,52680,52682],{"class":54,"line":1381},[52,52675,11151],{"class":58},[52,52677,51486],{"class":62},[52,52679,15294],{"class":79},[52,52681,51491],{"class":62},[52,52683,1330],{"class":102},[52,52685,52686,52688,52690,52692,52694,52696],{"class":54,"line":1406},[52,52687,1470],{"class":241},[52,52689,193],{"class":102},[52,52691,115],{"class":83},[52,52693,51535],{"class":87},[52,52695,115],{"class":83},[52,52697,211],{"class":102},[52,52699,52700,52702],{"class":54,"line":1430},[52,52701,1567],{"class":58},[52,52703,1330],{"class":102},[52,52705,52706,52708,52710,52712,52714,52716],{"class":54,"line":1435},[52,52707,1470],{"class":241},[52,52709,193],{"class":102},[52,52711,115],{"class":83},[52,52713,51556],{"class":87},[52,52715,115],{"class":83},[52,52717,211],{"class":102},[639,52719,52721],{"id":52720},"limit-the-number-of-attempts","Limit the number of attempts",[14,52723,52724],{},"You can also stop the game after a fixed number of guesses:",[42,52726,52728],{"className":44,"code":52727,"language":46,"meta":47,"style":47},"import random\n\nsecret_number = random.randint(1, 10)\nattempts_left = 3\n\nwhile attempts_left > 0:\n    guess = int(input(\"Guess a number from 1 to 10: \"))\n\n    if guess == secret_number:\n        print(\"You guessed it!\")\n        break\n    elif guess \u003C secret_number:\n        print(\"Too low\")\n    else:\n        print(\"Too high\")\n\n    attempts_left -= 1\n    print(\"Attempts left:\", attempts_left)\n\nif attempts_left == 0 and guess != secret_number:\n    print(\"Game over. The number was\", secret_number)\n",[49,52729,52730,52736,52740,52762,52771,52775,52788,52810,52814,52826,52840,52844,52856,52870,52876,52890,52894,52903,52923,52927,52947],{"__ignoreMap":47},[52,52731,52732,52734],{"class":54,"line":55},[52,52733,59],{"class":58},[52,52735,36646],{"class":62},[52,52737,52738],{"class":54,"line":66},[52,52739,70],{"emptyLinePlaceholder":69},[52,52741,52742,52744,52746,52748,52750,52752,52754,52756,52758,52760],{"class":54,"line":73},[52,52743,51421],{"class":62},[52,52745,80],{"class":79},[52,52747,36659],{"class":62},[52,52749,186],{"class":102},[52,52751,36664],{"class":189},[52,52753,193],{"class":102},[52,52755,9200],{"class":121},[52,52757,199],{"class":102},[52,52759,27053],{"class":121},[52,52761,211],{"class":102},[52,52763,52764,52767,52769],{"class":54,"line":94},[52,52765,52766],{"class":62},"attempts_left ",[52,52768,80],{"class":79},[52,52770,1085],{"class":121},[52,52772,52773],{"class":54,"line":106},[52,52774,70],{"emptyLinePlaceholder":69},[52,52776,52777,52779,52782,52784,52786],{"class":54,"line":128},[52,52778,10772],{"class":58},[52,52780,52781],{"class":62}," attempts_left ",[52,52783,13046],{"class":79},[52,52785,13049],{"class":121},[52,52787,1330],{"class":102},[52,52789,52790,52792,52794,52796,52798,52800,52802,52804,52806,52808],{"class":54,"line":148},[52,52791,51456],{"class":62},[52,52793,80],{"class":79},[52,52795,9789],{"class":4733},[52,52797,193],{"class":102},[52,52799,13610],{"class":241},[52,52801,193],{"class":102},[52,52803,115],{"class":83},[52,52805,51471],{"class":87},[52,52807,115],{"class":83},[52,52809,8886],{"class":102},[52,52811,52812],{"class":54,"line":164},[52,52813,70],{"emptyLinePlaceholder":69},[52,52815,52816,52818,52820,52822,52824],{"class":54,"line":170},[52,52817,1438],{"class":58},[52,52819,51486],{"class":62},[52,52821,11062],{"class":79},[52,52823,51491],{"class":62},[52,52825,1330],{"class":102},[52,52827,52828,52830,52832,52834,52836,52838],{"class":54,"line":175},[52,52829,1470],{"class":241},[52,52831,193],{"class":102},[52,52833,115],{"class":83},[52,52835,51504],{"class":87},[52,52837,115],{"class":83},[52,52839,211],{"class":102},[52,52841,52842],{"class":54,"line":214},[52,52843,11499],{"class":58},[52,52845,52846,52848,52850,52852,52854],{"class":54,"line":233},[52,52847,11151],{"class":58},[52,52849,51486],{"class":62},[52,52851,15294],{"class":79},[52,52853,51491],{"class":62},[52,52855,1330],{"class":102},[52,52857,52858,52860,52862,52864,52866,52868],{"class":54,"line":238},[52,52859,1470],{"class":241},[52,52861,193],{"class":102},[52,52863,115],{"class":83},[52,52865,51535],{"class":87},[52,52867,115],{"class":83},[52,52869,211],{"class":102},[52,52871,52872,52874],{"class":54,"line":272},[52,52873,1567],{"class":58},[52,52875,1330],{"class":102},[52,52877,52878,52880,52882,52884,52886,52888],{"class":54,"line":1348},[52,52879,1470],{"class":241},[52,52881,193],{"class":102},[52,52883,115],{"class":83},[52,52885,51556],{"class":87},[52,52887,115],{"class":83},[52,52889,211],{"class":102},[52,52891,52892],{"class":54,"line":1376},[52,52893,70],{"emptyLinePlaceholder":69},[52,52895,52896,52899,52901],{"class":54,"line":1381},[52,52897,52898],{"class":62},"    attempts_left ",[52,52900,13086],{"class":79},[52,52902,8645],{"class":121},[52,52904,52905,52907,52909,52911,52914,52916,52918,52921],{"class":54,"line":1406},[52,52906,1599],{"class":241},[52,52908,193],{"class":102},[52,52910,115],{"class":83},[52,52912,52913],{"class":87},"Attempts left:",[52,52915,115],{"class":83},[52,52917,199],{"class":102},[52,52919,52920],{"class":189}," attempts_left",[52,52922,211],{"class":102},[52,52924,52925],{"class":54,"line":1430},[52,52926,70],{"emptyLinePlaceholder":69},[52,52928,52929,52931,52933,52935,52937,52939,52941,52943,52945],{"class":54,"line":1435},[52,52930,1313],{"class":58},[52,52932,52781],{"class":62},[52,52934,11062],{"class":79},[52,52936,13049],{"class":121},[52,52938,1453],{"class":79},[52,52940,51486],{"class":62},[52,52942,33286],{"class":79},[52,52944,51491],{"class":62},[52,52946,1330],{"class":102},[52,52948,52949,52951,52953,52955,52958,52960,52962,52964],{"class":54,"line":1467},[52,52950,1599],{"class":241},[52,52952,193],{"class":102},[52,52954,115],{"class":83},[52,52956,52957],{"class":87},"Game over. The number was",[52,52959,115],{"class":83},[52,52961,199],{"class":102},[52,52963,51491],{"class":189},[52,52965,211],{"class":102},[639,52967,52969],{"id":52968},"let-the-user-play-again","Let the user play again",[14,52971,52972],{},"A good next step is wrapping the game in another loop so the user can start a new round after winning.",[37,52974,52976],{"id":52975},"common-problems-in-this-project","Common problems in this project",[14,52978,52979],{},"Here are the most common beginner mistakes in a guessing game.",[639,52981,52983],{"id":52982},"valueerror-when-converting-input","ValueError when converting input",[14,52985,52986],{},"Problem:",[309,52988,52989,52994,53000],{},[24,52990,52991,52992],{},"you type letters like ",[49,52993,6363],{},[24,52995,52996,52997,52999],{},"the program uses ",[49,52998,9492],{}," on that text",[24,53001,53002,53003],{},"Python crashes with ",[49,53004,13780],{},[14,53006,4259],{},[42,53008,53010],{"className":44,"code":53009,"language":46,"meta":47,"style":47},"guess = int(input(\"Guess a number: \"))\n",[49,53011,53012],{"__ignoreMap":47},[52,53013,53014,53016,53018,53020,53022,53024,53026,53028,53031,53033],{"class":54,"line":55},[52,53015,51902],{"class":62},[52,53017,80],{"class":79},[52,53019,9789],{"class":4733},[52,53021,193],{"class":102},[52,53023,13610],{"class":241},[52,53025,193],{"class":102},[52,53027,115],{"class":83},[52,53029,53030],{"class":87},"Guess a number: ",[52,53032,115],{"class":83},[52,53034,8886],{"class":102},[14,53036,53037,53038,53040],{},"If the user enters ",[49,53039,6363],{},", this line fails.",[14,53042,53043],{},"Fixes:",[309,53045,53046,53051,53056],{},[24,53047,53048,53049],{},"only enter number text such as ",[49,53050,9479],{},[24,53052,53053,53054],{},"validate the input before calling ",[49,53055,9492],{},[24,53057,10527,53058],{},[372,53059,6472],{"href":6471},[639,53061,53063],{"id":53062},"infinite-loop-because-break-is-missing","Infinite loop because break is missing",[14,53065,7526,53066,53068],{},[49,53067,10914],{}," is missing, the loop never ends after the correct answer.",[14,53070,53071],{},"Wrong idea:",[42,53073,53075],{"className":44,"code":53074,"language":46,"meta":47,"style":47},"if guess == secret_number:\n    print(\"You guessed it!\")\n",[49,53076,53077,53089],{"__ignoreMap":47},[52,53078,53079,53081,53083,53085,53087],{"class":54,"line":55},[52,53080,1313],{"class":58},[52,53082,51486],{"class":62},[52,53084,11062],{"class":79},[52,53086,51491],{"class":62},[52,53088,1330],{"class":102},[52,53090,53091,53093,53095,53097,53099,53101],{"class":54,"line":66},[52,53092,1599],{"class":241},[52,53094,193],{"class":102},[52,53096,115],{"class":83},[52,53098,51504],{"class":87},[52,53100,115],{"class":83},[52,53102,211],{"class":102},[14,53104,27987],{},[42,53106,53108],{"className":44,"code":53107,"language":46,"meta":47,"style":47},"if guess == secret_number:\n    print(\"You guessed it!\")\n    break\n",[49,53109,53110,53122,53136],{"__ignoreMap":47},[52,53111,53112,53114,53116,53118,53120],{"class":54,"line":55},[52,53113,1313],{"class":58},[52,53115,51486],{"class":62},[52,53117,11062],{"class":79},[52,53119,51491],{"class":62},[52,53121,1330],{"class":102},[52,53123,53124,53126,53128,53130,53132,53134],{"class":54,"line":66},[52,53125,1599],{"class":241},[52,53127,193],{"class":102},[52,53129,115],{"class":83},[52,53131,51504],{"class":87},[52,53133,115],{"class":83},[52,53135,211],{"class":102},[52,53137,53138],{"class":54,"line":73},[52,53139,52013],{"class":58},[639,53141,53143],{"id":53142},"wrong-indentation","Wrong indentation",[14,53145,53146,53147,27610],{},"Indentation controls which lines belong inside the loop or inside an ",[49,53148,1313],{},[14,53150,12576],{},[42,53152,53154],{"className":44,"code":53153,"language":46,"meta":47,"style":47},"while True:\nguess = int(input(\"Guess a number: \"))\n",[49,53155,53156,53164],{"__ignoreMap":47},[52,53157,53158,53160,53162],{"class":54,"line":55},[52,53159,10772],{"class":58},[52,53161,10943],{"class":160},[52,53163,1330],{"class":102},[52,53165,53166,53168,53170,53172,53174,53176,53178,53180,53182,53184],{"class":54,"line":66},[52,53167,51902],{"class":62},[52,53169,80],{"class":79},[52,53171,9789],{"class":4733},[52,53173,193],{"class":102},[52,53175,13610],{"class":241},[52,53177,193],{"class":102},[52,53179,115],{"class":83},[52,53181,53030],{"class":87},[52,53183,115],{"class":83},[52,53185,8886],{"class":102},[14,53187,53188],{},"Python expects the loop body to be indented.",[14,53190,12506],{},[42,53192,53194],{"className":44,"code":53193,"language":46,"meta":47,"style":47},"while True:\n    guess = int(input(\"Guess a number: \"))\n",[49,53195,53196,53204],{"__ignoreMap":47},[52,53197,53198,53200,53202],{"class":54,"line":55},[52,53199,10772],{"class":58},[52,53201,10943],{"class":160},[52,53203,1330],{"class":102},[52,53205,53206,53208,53210,53212,53214,53216,53218,53220,53222,53224],{"class":54,"line":66},[52,53207,51456],{"class":62},[52,53209,80],{"class":79},[52,53211,9789],{"class":4733},[52,53213,193],{"class":102},[52,53215,13610],{"class":241},[52,53217,193],{"class":102},[52,53219,115],{"class":83},[52,53221,53030],{"class":87},[52,53223,115],{"class":83},[52,53225,8886],{"class":102},[14,53227,53228,53229,186],{},"If you see this kind of problem, read ",[372,53230,8489],{"href":8488},[639,53232,38104],{"id":37421},[14,53234,6446,53235,14162,53237,53239,53240,17143],{},[49,53236,37871],{},[49,53238,36739],{},", Python does not know what ",[49,53241,36610],{},[14,53243,53244],{},"Always include:",[42,53246,53247],{"className":44,"code":51816,"language":46,"meta":47,"style":47},[49,53248,53249],{"__ignoreMap":47},[52,53250,53251,53253],{"class":54,"line":55},[52,53252,59],{"class":58},[52,53254,36646],{"class":62},[639,53256,53258],{"id":53257},"other-common-causes","Other common causes",[14,53260,53261],{},"These mistakes also happen often:",[309,53263,53264,53273,53279,53287,53294],{},[24,53265,53266,53267,53269,53270,53272],{},"using ",[49,53268,10597],{}," without converting the result to ",[49,53271,9492],{}," before comparing with a number",[24,53274,53275,53276,53278],{},"typing words like ",[49,53277,6363],{}," when the program expects a number",[24,53280,53281,53282,53284,53285,28020],{},"placing ",[49,53283,10914],{}," outside the correct ",[49,53286,1313],{},[24,53288,53289,53290,857,53292],{},"misspelling ",[49,53291,36664],{},[49,53293,36610],{},[24,53295,53296],{},"using incorrect indentation in the loop",[37,53298,1942],{"id":1941},[639,53300,53302],{"id":53301},"why-does-input-need-int-here","Why does input() need int() here?",[14,53304,53305,53307,53308,53310],{},[49,53306,10597],{}," returns text. ",[49,53309,9492],{}," changes that text into a number so it can be compared with the secret number.",[639,53312,53314],{"id":53313},"why-use-a-while-loop-in-a-guessing-game","Why use a while loop in a guessing game?",[14,53316,3233,53317,53319],{},[49,53318,10772],{}," loop lets the program keep asking until the correct guess is entered.",[639,53321,53323],{"id":53322},"why-does-my-program-crash-when-i-enter-letters","Why does my program crash when I enter letters?",[14,53325,53326,53328,53329,186],{},[49,53327,9492],{}," can only convert valid number text. If the user types letters, Python raises a ",[49,53330,13780],{},[639,53332,53334],{"id":53333},"can-i-make-the-game-harder","Can I make the game harder?",[14,53336,53337],{},"Yes. Increase the number range, limit attempts, or add hints.",[37,53339,2005],{"id":2004},[309,53341,53342,53346,53351,53355,53359,53363],{},[24,53343,53344],{},[372,53345,36730],{"href":36729},[24,53347,53348],{},[372,53349,53350],{"href":36826},"random.randint() function explained",[24,53352,53353],{},[372,53354,13726],{"href":10606},[24,53356,53357],{},[372,53358,13731],{"href":9489},[24,53360,53361],{},[372,53362,13426],{"href":13425},[24,53364,53365],{},[372,53366,13737],{"href":13736},[14,53368,53369],{},"Build the basic game first and make sure you understand every line. After that, improve it by adding input validation and a guess counter.",[2034,53371,53372],{},"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 .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 .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 .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);}",{"title":47,"searchDepth":66,"depth":66,"links":53374},[53375,53376,53377,53378,53386,53387,53393,53400,53406],{"id":39,"depth":66,"text":40},{"id":7215,"depth":66,"text":7216},{"id":51631,"depth":66,"text":51632},{"id":51655,"depth":66,"text":51656,"children":53379},[53380,53381,53382,53383,53384,53385],{"id":51812,"depth":73,"text":51813},{"id":51841,"depth":73,"text":51842},{"id":51891,"depth":73,"text":51892},{"id":51948,"depth":73,"text":51949},{"id":51976,"depth":73,"text":51977},{"id":52089,"depth":73,"text":52090},{"id":52115,"depth":66,"text":52116},{"id":52298,"depth":66,"text":52299,"children":53388},[53389,53390,53391,53392],{"id":52305,"depth":73,"text":52306},{"id":52501,"depth":73,"text":52502},{"id":52720,"depth":73,"text":52721},{"id":52968,"depth":73,"text":52969},{"id":52975,"depth":66,"text":52976,"children":53394},[53395,53396,53397,53398,53399],{"id":52982,"depth":73,"text":52983},{"id":53062,"depth":73,"text":53063},{"id":53142,"depth":73,"text":53143},{"id":37421,"depth":73,"text":38104},{"id":53257,"depth":73,"text":53258},{"id":1941,"depth":66,"text":1942,"children":53401},[53402,53403,53404,53405],{"id":53301,"depth":73,"text":53302},{"id":53313,"depth":73,"text":53314},{"id":53322,"depth":73,"text":53323},{"id":53333,"depth":73,"text":53334},{"id":2004,"depth":66,"text":2005},"Master python number guessing game example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-number-guessing-game-example",{"title":51385,"description":53407},"examples\u002Fpython-number-guessing-game-example","TmyiqGTPESDCbTGaeblB9b5ruZuhQZFdhZnXz2lByAU",{"id":53414,"title":53415,"body":53416,"description":56063,"extension":2075,"meta":56064,"navigation":69,"path":56065,"seo":56066,"stem":56067,"__hash__":56068},"content\u002Fexamples\u002Fpython-password-generator-example.md","Python Password Generator Example",{"type":7,"value":53417,"toc":56018},[53418,53421,53424,53426,53440,53443,53566,53569,53579,53582,53584,53586,53600,53603,53617,53619,53621,53624,53648,53650,53760,53762,53766,53772,53777,53779,53808,53810,53819,53825,53834,53863,53865,53878,53884,53889,53919,53921,53936,53942,53945,53978,53984,53990,53995,54000,54038,54040,54048,54053,54058,54066,54146,54148,54157,54161,54271,54274,54296,54299,54301,54305,54308,54443,54445,54462,54465,54479,54483,54517,54521,54524,54527,54554,54557,54566,54574,54576,54580,54583,54587,54590,54784,54788,54791,54794,55022,55026,55029,55259,55263,55266,55404,55411,55415,55418,55421,55424,55426,55430,55435,55442,55444,55456,55459,55461,55463,55466,55473,55482,55484,55557,55559,55576,55584,55589,55591,55648,55658,55660,55686,55692,55695,55806,55808,55814,55817,55851,55857,55860,55862,55904,55909,55912,55914,55916,55920,55923,55930,55938,55942,55952,55956,55964,55966,55968,55996,55999,56015],[10,53419,53415],{"id":53420},"python-password-generator-example",[14,53422,53423],{},"This example shows how to build a simple password generator in Python.",[14,53425,6959],{},[309,53427,53428,53431,53434,53437],{},[24,53429,53430],{},"generate a random password",[24,53432,53433],{},"use built-in Python modules",[24,53435,53436],{},"combine letters, numbers, and symbols",[24,53438,53439],{},"print one password to the screen",[14,53441,53442],{},"If you want a quick working version first, use this script:",[42,53444,53446],{"className":44,"code":53445,"language":46,"meta":47,"style":47},"import random\nimport string\n\nlength = 12\ncharacters = string.ascii_letters + string.digits + string.punctuation\npassword = ''.join(random.choice(characters) for _ in range(length))\n\nprint(password)\n",[49,53447,53448,53454,53461,53465,53475,53508,53551,53555],{"__ignoreMap":47},[52,53449,53450,53452],{"class":54,"line":55},[52,53451,59],{"class":58},[52,53453,36646],{"class":62},[52,53455,53456,53458],{"class":54,"line":66},[52,53457,59],{"class":58},[52,53459,53460],{"class":62}," string\n",[52,53462,53463],{"class":54,"line":73},[52,53464,70],{"emptyLinePlaceholder":69},[52,53466,53467,53470,53472],{"class":54,"line":94},[52,53468,53469],{"class":62},"length ",[52,53471,80],{"class":79},[52,53473,53474],{"class":121}," 12\n",[52,53476,53477,53480,53482,53485,53487,53490,53492,53494,53496,53499,53501,53503,53505],{"class":54,"line":106},[52,53478,53479],{"class":62},"characters ",[52,53481,80],{"class":79},[52,53483,53484],{"class":62}," string",[52,53486,186],{"class":102},[52,53488,53489],{"class":1320},"ascii_letters",[52,53491,5234],{"class":79},[52,53493,53484],{"class":62},[52,53495,186],{"class":102},[52,53497,53498],{"class":1320},"digits",[52,53500,5234],{"class":79},[52,53502,53484],{"class":62},[52,53504,186],{"class":102},[52,53506,53507],{"class":1320},"punctuation\n",[52,53509,53510,53512,53514,53517,53519,53521,53523,53525,53527,53529,53531,53534,53536,53538,53540,53542,53544,53546,53549],{"class":54,"line":128},[52,53511,26217],{"class":62},[52,53513,80],{"class":79},[52,53515,53516],{"class":83}," ''",[52,53518,186],{"class":102},[52,53520,34643],{"class":189},[52,53522,193],{"class":102},[52,53524,36610],{"class":189},[52,53526,186],{"class":102},[52,53528,12230],{"class":189},[52,53530,193],{"class":102},[52,53532,53533],{"class":189},"characters",[52,53535,1521],{"class":102},[52,53537,37077],{"class":58},[52,53539,37080],{"class":189},[52,53541,11213],{"class":58},[52,53543,36864],{"class":241},[52,53545,193],{"class":102},[52,53547,53548],{"class":189},"length",[52,53550,8886],{"class":102},[52,53552,53553],{"class":54,"line":148},[52,53554,70],{"emptyLinePlaceholder":69},[52,53556,53557,53559,53561,53564],{"class":54,"line":164},[52,53558,242],{"class":241},[52,53560,193],{"class":102},[52,53562,53563],{"class":189},"password",[52,53565,211],{"class":102},[14,53567,53568],{},"This creates a random 12-character password using:",[309,53570,53571,53574,53576],{},[24,53572,53573],{},"uppercase and lowercase letters",[24,53575,5324],{},[24,53577,53578],{},"symbols",[53580,53581],"hr",{},[37,53583,323],{"id":322},[14,53585,30376],{},[309,53587,53588,53591,53594,53597],{},[24,53589,53590],{},"generates a random password",[24,53592,53593],{},"uses built-in Python modules",[24,53595,53596],{},"combines letters, digits, and symbols",[24,53598,53599],{},"prints one password to the screen",[14,53601,53602],{},"It is a good beginner project because it uses a few important Python ideas in one small program:",[309,53604,53605,53608,53611,53614],{},[24,53606,53607],{},"importing modules",[24,53609,53610],{},"working with strings",[24,53612,53613],{},"using a loop",[24,53615,53616],{},"joining values into one final result",[53580,53618],{},[37,53620,25641],{"id":25640},[14,53622,53623],{},"The script follows these steps:",[21,53625,53626,53631,53636,53639,53642,53645],{},[24,53627,42554,53628,53630],{},[49,53629,36610],{}," to pick random characters.",[24,53632,42554,53633,53635],{},[49,53634,41855],{}," to get ready-made character groups.",[24,53637,53638],{},"Set a password length.",[24,53640,53641],{},"Build one string of allowed characters.",[24,53643,53644],{},"Pick random characters in a loop.",[24,53646,53647],{},"Join the characters into one final password.",[14,53649,16885],{},[42,53651,53652],{"className":44,"code":53445,"language":46,"meta":47,"style":47},[49,53653,53654,53660,53666,53670,53678,53706,53746,53750],{"__ignoreMap":47},[52,53655,53656,53658],{"class":54,"line":55},[52,53657,59],{"class":58},[52,53659,36646],{"class":62},[52,53661,53662,53664],{"class":54,"line":66},[52,53663,59],{"class":58},[52,53665,53460],{"class":62},[52,53667,53668],{"class":54,"line":73},[52,53669,70],{"emptyLinePlaceholder":69},[52,53671,53672,53674,53676],{"class":54,"line":94},[52,53673,53469],{"class":62},[52,53675,80],{"class":79},[52,53677,53474],{"class":121},[52,53679,53680,53682,53684,53686,53688,53690,53692,53694,53696,53698,53700,53702,53704],{"class":54,"line":106},[52,53681,53479],{"class":62},[52,53683,80],{"class":79},[52,53685,53484],{"class":62},[52,53687,186],{"class":102},[52,53689,53489],{"class":1320},[52,53691,5234],{"class":79},[52,53693,53484],{"class":62},[52,53695,186],{"class":102},[52,53697,53498],{"class":1320},[52,53699,5234],{"class":79},[52,53701,53484],{"class":62},[52,53703,186],{"class":102},[52,53705,53507],{"class":1320},[52,53707,53708,53710,53712,53714,53716,53718,53720,53722,53724,53726,53728,53730,53732,53734,53736,53738,53740,53742,53744],{"class":54,"line":128},[52,53709,26217],{"class":62},[52,53711,80],{"class":79},[52,53713,53516],{"class":83},[52,53715,186],{"class":102},[52,53717,34643],{"class":189},[52,53719,193],{"class":102},[52,53721,36610],{"class":189},[52,53723,186],{"class":102},[52,53725,12230],{"class":189},[52,53727,193],{"class":102},[52,53729,53533],{"class":189},[52,53731,1521],{"class":102},[52,53733,37077],{"class":58},[52,53735,37080],{"class":189},[52,53737,11213],{"class":58},[52,53739,36864],{"class":241},[52,53741,193],{"class":102},[52,53743,53548],{"class":189},[52,53745,8886],{"class":102},[52,53747,53748],{"class":54,"line":148},[52,53749,70],{"emptyLinePlaceholder":69},[52,53751,53752,53754,53756,53758],{"class":54,"line":164},[52,53753,242],{"class":241},[52,53755,193],{"class":102},[52,53757,53563],{"class":189},[52,53759,211],{"class":102},[53580,53761],{},[37,53763,53765],{"id":53764},"main-code-example-walkthrough","Main code example walkthrough",[639,53767,53769],{"id":53768},"stringascii_letters",[49,53770,53771],{},"string.ascii_letters",[14,53773,53774,53776],{},[49,53775,53771],{}," gives you all lowercase and uppercase English letters.",[14,53778,4259],{},[42,53780,53782],{"className":44,"code":53781,"language":46,"meta":47,"style":47},"import string\n\nprint(string.ascii_letters)\n",[49,53783,53784,53790,53794],{"__ignoreMap":47},[52,53785,53786,53788],{"class":54,"line":55},[52,53787,59],{"class":58},[52,53789,53460],{"class":62},[52,53791,53792],{"class":54,"line":66},[52,53793,70],{"emptyLinePlaceholder":69},[52,53795,53796,53798,53800,53802,53804,53806],{"class":54,"line":73},[52,53797,242],{"class":241},[52,53799,193],{"class":102},[52,53801,41855],{"class":189},[52,53803,186],{"class":102},[52,53805,53489],{"class":1320},[52,53807,211],{"class":102},[14,53809,40163],{},[42,53811,53813],{"className":44,"code":53812,"language":46,"meta":47,"style":47},"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n",[49,53814,53815],{"__ignoreMap":47},[52,53816,53817],{"class":54,"line":55},[52,53818,53812],{"class":62},[639,53820,53822],{"id":53821},"stringdigits",[49,53823,53824],{},"string.digits",[14,53826,53827,53829,53830,37261,53832,186],{},[49,53828,53824],{}," gives you the characters ",[49,53831,13294],{},[49,53833,37264],{},[42,53835,53837],{"className":44,"code":53836,"language":46,"meta":47,"style":47},"import string\n\nprint(string.digits)\n",[49,53838,53839,53845,53849],{"__ignoreMap":47},[52,53840,53841,53843],{"class":54,"line":55},[52,53842,59],{"class":58},[52,53844,53460],{"class":62},[52,53846,53847],{"class":54,"line":66},[52,53848,70],{"emptyLinePlaceholder":69},[52,53850,53851,53853,53855,53857,53859,53861],{"class":54,"line":73},[52,53852,242],{"class":241},[52,53854,193],{"class":102},[52,53856,41855],{"class":189},[52,53858,186],{"class":102},[52,53860,53498],{"class":1320},[52,53862,211],{"class":102},[14,53864,5267],{},[42,53866,53868],{"className":44,"code":53867,"language":46,"meta":47,"style":47},"0123456789\n",[49,53869,53870],{"__ignoreMap":47},[52,53871,53872,53874],{"class":54,"line":55},[52,53873,13294],{"class":121},[52,53875,53877],{"class":53876},"sx12J","123456789\n",[639,53879,53881],{"id":53880},"stringpunctuation",[49,53882,53883],{},"string.punctuation",[14,53885,53886,53888],{},[49,53887,53883],{}," gives you common symbol characters.",[42,53890,53892],{"className":44,"code":53891,"language":46,"meta":47,"style":47},"import string\n\nprint(string.punctuation)\n",[49,53893,53894,53900,53904],{"__ignoreMap":47},[52,53895,53896,53898],{"class":54,"line":55},[52,53897,59],{"class":58},[52,53899,53460],{"class":62},[52,53901,53902],{"class":54,"line":66},[52,53903,70],{"emptyLinePlaceholder":69},[52,53905,53906,53908,53910,53912,53914,53917],{"class":54,"line":73},[52,53907,242],{"class":241},[52,53909,193],{"class":102},[52,53911,41855],{"class":189},[52,53913,186],{"class":102},[52,53915,53916],{"class":1320},"punctuation",[52,53918,211],{"class":102},[14,53920,40163],{},[42,53922,53924],{"className":44,"code":53923,"language":46,"meta":47,"style":47},"!\"#$%&'()*+,-.\u002F:;\u003C=>?@[\\]^_`{|}~\n",[49,53925,53926],{"__ignoreMap":47},[52,53927,53928,53931,53933],{"class":54,"line":55},[52,53929,53930],{"class":62},"!",[52,53932,115],{"class":83},[52,53934,53935],{"class":87},"#$%&'()*+,-.\u002F:;\u003C=>?@[\\]^_`{|}~\n",[639,53937,53939,53940,53484],{"id":53938},"building-the-characters-string","Building the ",[49,53941,53533],{},[14,53943,53944],{},"This line puts all allowed characters into one string:",[42,53946,53948],{"className":44,"code":53947,"language":46,"meta":47,"style":47},"characters = string.ascii_letters + string.digits + string.punctuation\n",[49,53949,53950],{"__ignoreMap":47},[52,53951,53952,53954,53956,53958,53960,53962,53964,53966,53968,53970,53972,53974,53976],{"class":54,"line":55},[52,53953,53479],{"class":62},[52,53955,80],{"class":79},[52,53957,53484],{"class":62},[52,53959,186],{"class":102},[52,53961,53489],{"class":1320},[52,53963,5234],{"class":79},[52,53965,53484],{"class":62},[52,53967,186],{"class":102},[52,53969,53498],{"class":1320},[52,53971,5234],{"class":79},[52,53973,53484],{"class":62},[52,53975,186],{"class":102},[52,53977,53507],{"class":1320},[14,53979,53980,53981,53983],{},"Now ",[49,53982,53533],{}," contains letters, numbers, and symbols.",[639,53985,53987],{"id":53986},"randomchoice",[49,53988,53989],{},"random.choice()",[14,53991,53992,53994],{},[49,53993,53989],{}," picks one random item from a sequence.",[14,53996,53997,53998,186],{},"In this example, it picks one random character from ",[49,53999,53533],{},[42,54001,54003],{"className":44,"code":54002,"language":46,"meta":47,"style":47},"import random\n\nprint(random.choice(\"abc123\"))\n",[49,54004,54005,54011,54015],{"__ignoreMap":47},[52,54006,54007,54009],{"class":54,"line":55},[52,54008,59],{"class":58},[52,54010,36646],{"class":62},[52,54012,54013],{"class":54,"line":66},[52,54014,70],{"emptyLinePlaceholder":69},[52,54016,54017,54019,54021,54023,54025,54027,54029,54031,54034,54036],{"class":54,"line":73},[52,54018,242],{"class":241},[52,54020,193],{"class":102},[52,54022,36610],{"class":189},[52,54024,186],{"class":102},[52,54026,12230],{"class":189},[52,54028,193],{"class":102},[52,54030,115],{"class":83},[52,54032,54033],{"class":87},"abc123",[52,54035,115],{"class":83},[52,54037,8886],{"class":102},[14,54039,40163],{},[42,54041,54042],{"className":44,"code":44748,"language":46,"meta":47,"style":47},[49,54043,54044],{"__ignoreMap":47},[52,54045,54046],{"class":54,"line":55},[52,54047,44748],{"class":121},[14,54049,54050,54051,186],{},"If you want a beginner-friendly overview, see the ",[372,54052,36730],{"href":36729},[639,54054,54055],{"id":34643},[49,54056,54057],{},"''.join(...)",[14,54059,54060,54061,54063,54065],{},"The generator expression creates characters one at a time.",[844,54062],{},[49,54064,54057],{}," combines them into one final string.",[42,54067,54069],{"className":44,"code":54068,"language":46,"meta":47,"style":47},"letters = ['a', 'B', '7', '!']\npassword = ''.join(letters)\n\nprint(password)\n",[49,54070,54071,54113,54132,54136],{"__ignoreMap":47},[52,54072,54073,54076,54078,54080,54082,54084,54086,54088,54090,54093,54095,54097,54099,54101,54103,54105,54107,54109,54111],{"class":54,"line":55},[52,54074,54075],{"class":62},"letters ",[52,54077,80],{"class":79},[52,54079,18162],{"class":102},[52,54081,4162],{"class":83},[52,54083,372],{"class":87},[52,54085,4162],{"class":83},[52,54087,199],{"class":102},[52,54089,4172],{"class":83},[52,54091,54092],{"class":87},"B",[52,54094,4162],{"class":83},[52,54096,199],{"class":102},[52,54098,4172],{"class":83},[52,54100,44637],{"class":87},[52,54102,4162],{"class":83},[52,54104,199],{"class":102},[52,54106,4172],{"class":83},[52,54108,53930],{"class":87},[52,54110,4162],{"class":83},[52,54112,1147],{"class":102},[52,54114,54115,54117,54119,54121,54123,54125,54127,54130],{"class":54,"line":66},[52,54116,26217],{"class":62},[52,54118,80],{"class":79},[52,54120,53516],{"class":83},[52,54122,186],{"class":102},[52,54124,34643],{"class":189},[52,54126,193],{"class":102},[52,54128,54129],{"class":189},"letters",[52,54131,211],{"class":102},[52,54133,54134],{"class":54,"line":73},[52,54135,70],{"emptyLinePlaceholder":69},[52,54137,54138,54140,54142,54144],{"class":54,"line":94},[52,54139,242],{"class":241},[52,54141,193],{"class":102},[52,54143,53563],{"class":189},[52,54145,211],{"class":102},[14,54147,5267],{},[42,54149,54151],{"className":44,"code":54150,"language":46,"meta":47,"style":47},"aB7!\n",[49,54152,54153],{"__ignoreMap":47},[52,54154,54155],{"class":54,"line":55},[52,54156,54150],{"class":62},[639,54158,54160],{"id":54159},"full-example-again","Full example again",[42,54162,54163],{"className":44,"code":53445,"language":46,"meta":47,"style":47},[49,54164,54165,54171,54177,54181,54189,54217,54257,54261],{"__ignoreMap":47},[52,54166,54167,54169],{"class":54,"line":55},[52,54168,59],{"class":58},[52,54170,36646],{"class":62},[52,54172,54173,54175],{"class":54,"line":66},[52,54174,59],{"class":58},[52,54176,53460],{"class":62},[52,54178,54179],{"class":54,"line":73},[52,54180,70],{"emptyLinePlaceholder":69},[52,54182,54183,54185,54187],{"class":54,"line":94},[52,54184,53469],{"class":62},[52,54186,80],{"class":79},[52,54188,53474],{"class":121},[52,54190,54191,54193,54195,54197,54199,54201,54203,54205,54207,54209,54211,54213,54215],{"class":54,"line":106},[52,54192,53479],{"class":62},[52,54194,80],{"class":79},[52,54196,53484],{"class":62},[52,54198,186],{"class":102},[52,54200,53489],{"class":1320},[52,54202,5234],{"class":79},[52,54204,53484],{"class":62},[52,54206,186],{"class":102},[52,54208,53498],{"class":1320},[52,54210,5234],{"class":79},[52,54212,53484],{"class":62},[52,54214,186],{"class":102},[52,54216,53507],{"class":1320},[52,54218,54219,54221,54223,54225,54227,54229,54231,54233,54235,54237,54239,54241,54243,54245,54247,54249,54251,54253,54255],{"class":54,"line":128},[52,54220,26217],{"class":62},[52,54222,80],{"class":79},[52,54224,53516],{"class":83},[52,54226,186],{"class":102},[52,54228,34643],{"class":189},[52,54230,193],{"class":102},[52,54232,36610],{"class":189},[52,54234,186],{"class":102},[52,54236,12230],{"class":189},[52,54238,193],{"class":102},[52,54240,53533],{"class":189},[52,54242,1521],{"class":102},[52,54244,37077],{"class":58},[52,54246,37080],{"class":189},[52,54248,11213],{"class":58},[52,54250,36864],{"class":241},[52,54252,193],{"class":102},[52,54254,53548],{"class":189},[52,54256,8886],{"class":102},[52,54258,54259],{"class":54,"line":148},[52,54260,70],{"emptyLinePlaceholder":69},[52,54262,54263,54265,54267,54269],{"class":54,"line":164},[52,54264,242],{"class":241},[52,54266,193],{"class":102},[52,54268,53563],{"class":189},[52,54270,211],{"class":102},[14,54272,54273],{},"Sample output:",[42,54275,54277],{"className":44,"code":54276,"language":46,"meta":47,"style":47},"gT8@q!2Lp#9Z\n",[49,54278,54279],{"__ignoreMap":47},[52,54280,54281,54284,54286,54289,54293],{"class":54,"line":55},[52,54282,54283],{"class":62},"gT8",[52,54285,20971],{"class":79},[52,54287,54288],{"class":62},"q",[52,54290,54292],{"class":54291},"srjyR","!2Lp",[52,54294,54295],{"class":4368},"#9Z\n",[14,54297,54298],{},"Your output will be different each time because the characters are random.",[53580,54300],{},[37,54302,54304],{"id":54303},"example-with-user-input","Example with user input",[14,54306,54307],{},"You can also ask the user how long the password should be.",[42,54309,54311],{"className":44,"code":54310,"language":46,"meta":47,"style":47},"import random\nimport string\n\nlength = int(input(\"Enter password length: \"))\ncharacters = string.ascii_letters + string.digits + string.punctuation\npassword = ''.join(random.choice(characters) for _ in range(length))\n\nprint(\"Generated password:\", password)\n",[49,54312,54313,54319,54325,54329,54352,54380,54420,54424],{"__ignoreMap":47},[52,54314,54315,54317],{"class":54,"line":55},[52,54316,59],{"class":58},[52,54318,36646],{"class":62},[52,54320,54321,54323],{"class":54,"line":66},[52,54322,59],{"class":58},[52,54324,53460],{"class":62},[52,54326,54327],{"class":54,"line":73},[52,54328,70],{"emptyLinePlaceholder":69},[52,54330,54331,54333,54335,54337,54339,54341,54343,54345,54348,54350],{"class":54,"line":94},[52,54332,53469],{"class":62},[52,54334,80],{"class":79},[52,54336,9789],{"class":4733},[52,54338,193],{"class":102},[52,54340,13610],{"class":241},[52,54342,193],{"class":102},[52,54344,115],{"class":83},[52,54346,54347],{"class":87},"Enter password length: ",[52,54349,115],{"class":83},[52,54351,8886],{"class":102},[52,54353,54354,54356,54358,54360,54362,54364,54366,54368,54370,54372,54374,54376,54378],{"class":54,"line":106},[52,54355,53479],{"class":62},[52,54357,80],{"class":79},[52,54359,53484],{"class":62},[52,54361,186],{"class":102},[52,54363,53489],{"class":1320},[52,54365,5234],{"class":79},[52,54367,53484],{"class":62},[52,54369,186],{"class":102},[52,54371,53498],{"class":1320},[52,54373,5234],{"class":79},[52,54375,53484],{"class":62},[52,54377,186],{"class":102},[52,54379,53507],{"class":1320},[52,54381,54382,54384,54386,54388,54390,54392,54394,54396,54398,54400,54402,54404,54406,54408,54410,54412,54414,54416,54418],{"class":54,"line":128},[52,54383,26217],{"class":62},[52,54385,80],{"class":79},[52,54387,53516],{"class":83},[52,54389,186],{"class":102},[52,54391,34643],{"class":189},[52,54393,193],{"class":102},[52,54395,36610],{"class":189},[52,54397,186],{"class":102},[52,54399,12230],{"class":189},[52,54401,193],{"class":102},[52,54403,53533],{"class":189},[52,54405,1521],{"class":102},[52,54407,37077],{"class":58},[52,54409,37080],{"class":189},[52,54411,11213],{"class":58},[52,54413,36864],{"class":241},[52,54415,193],{"class":102},[52,54417,53548],{"class":189},[52,54419,8886],{"class":102},[52,54421,54422],{"class":54,"line":148},[52,54423,70],{"emptyLinePlaceholder":69},[52,54425,54426,54428,54430,54432,54435,54437,54439,54441],{"class":54,"line":164},[52,54427,242],{"class":241},[52,54429,193],{"class":102},[52,54431,115],{"class":83},[52,54433,54434],{"class":87},"Generated password:",[52,54436,115],{"class":83},[52,54438,199],{"class":102},[52,54440,26396],{"class":189},[52,54442,211],{"class":102},[639,54444,9910],{"id":9909},[309,54446,54447,54452,54456],{},[24,54448,54449,54451],{},[49,54450,10597],{}," gets text from the user",[24,54453,54454,13708],{},[49,54455,9492],{},[24,54457,54458,54461],{},[49,54459,54460],{},"range(length)"," uses that number to control how many characters are chosen",[14,54463,54464],{},"If you are new to this, these pages may help:",[309,54466,54467,54471,54475],{},[24,54468,54469],{},[372,54470,13726],{"href":10606},[24,54472,54473],{},[372,54474,13731],{"href":9489},[24,54476,54477],{},[372,54478,13737],{"href":13736},[639,54480,54482],{"id":54481},"example-run","Example run",[42,54484,54486],{"className":44,"code":54485,"language":46,"meta":47,"style":47},"Enter password length: 8\nGenerated password: A7@k9!Qp\n",[49,54487,54488,54497],{"__ignoreMap":47},[52,54489,54490,54493,54495],{"class":54,"line":55},[52,54491,54492],{"class":62},"Enter password length",[52,54494,118],{"class":102},[52,54496,45391],{"class":121},[52,54498,54499,54502,54504,54507,54509,54512,54514],{"class":54,"line":66},[52,54500,54501],{"class":62},"Generated password",[52,54503,118],{"class":102},[52,54505,54506],{"class":62}," A7",[52,54508,20971],{"class":79},[52,54510,54511],{"class":62},"k9",[52,54513,53930],{"class":54291},[52,54515,54516],{"class":62},"Qp\n",[639,54518,54520],{"id":54519},"important-note","Important note",[14,54522,54523],{},"Invalid input can cause errors.",[14,54525,54526],{},"For example, this will fail:",[42,54528,54530],{"className":44,"code":54529,"language":46,"meta":47,"style":47},"length = int(input(\"Enter password length: \"))\n",[49,54531,54532],{"__ignoreMap":47},[52,54533,54534,54536,54538,54540,54542,54544,54546,54548,54550,54552],{"class":54,"line":55},[52,54535,53469],{"class":62},[52,54537,80],{"class":79},[52,54539,9789],{"class":4733},[52,54541,193],{"class":102},[52,54543,13610],{"class":241},[52,54545,193],{"class":102},[52,54547,115],{"class":83},[52,54549,54347],{"class":87},[52,54551,115],{"class":83},[52,54553,8886],{"class":102},[14,54555,54556],{},"if the user types:",[42,54558,54560],{"className":44,"code":54559,"language":46,"meta":47,"style":47},"abc\n",[49,54561,54562],{"__ignoreMap":47},[52,54563,54564],{"class":54,"line":55},[52,54565,54559],{"class":62},[14,54567,23272,54568,54570,54571,186],{},[49,54569,13780],{},". If you hit that problem, see ",[372,54572,54573],{"href":6471},"how to fix ValueError: invalid literal for int() with base 10",[53580,54575],{},[37,54577,54579],{"id":54578},"how-to-make-the-password-generator-better","How to make the password generator better",[14,54581,54582],{},"Here are a few useful ways to improve the script.",[639,54584,54586],{"id":54585},"let-the-user-choose-whether-to-include-symbols","Let the user choose whether to include symbols",[14,54588,54589],{},"You can ask the user if symbols should be included.",[42,54591,54593],{"className":44,"code":54592,"language":46,"meta":47,"style":47},"import random\nimport string\n\nlength = int(input(\"Enter password length: \"))\nuse_symbols = input(\"Include symbols? (yes\u002Fno): \").lower()\n\ncharacters = string.ascii_letters + string.digits\n\nif use_symbols == \"yes\":\n    characters += string.punctuation\n\npassword = ''.join(random.choice(characters) for _ in range(length))\n\nprint(\"Generated password:\", password)\n",[49,54594,54595,54601,54607,54611,54633,54658,54662,54683,54687,54705,54718,54722,54762,54766],{"__ignoreMap":47},[52,54596,54597,54599],{"class":54,"line":55},[52,54598,59],{"class":58},[52,54600,36646],{"class":62},[52,54602,54603,54605],{"class":54,"line":66},[52,54604,59],{"class":58},[52,54606,53460],{"class":62},[52,54608,54609],{"class":54,"line":73},[52,54610,70],{"emptyLinePlaceholder":69},[52,54612,54613,54615,54617,54619,54621,54623,54625,54627,54629,54631],{"class":54,"line":94},[52,54614,53469],{"class":62},[52,54616,80],{"class":79},[52,54618,9789],{"class":4733},[52,54620,193],{"class":102},[52,54622,13610],{"class":241},[52,54624,193],{"class":102},[52,54626,115],{"class":83},[52,54628,54347],{"class":87},[52,54630,115],{"class":83},[52,54632,8886],{"class":102},[52,54634,54635,54638,54640,54642,54644,54646,54649,54651,54654,54656],{"class":54,"line":106},[52,54636,54637],{"class":62},"use_symbols ",[52,54639,80],{"class":79},[52,54641,11037],{"class":241},[52,54643,193],{"class":102},[52,54645,115],{"class":83},[52,54647,54648],{"class":87},"Include symbols? (yes\u002Fno): ",[52,54650,115],{"class":83},[52,54652,54653],{"class":102},").",[52,54655,30237],{"class":189},[52,54657,230],{"class":102},[52,54659,54660],{"class":54,"line":128},[52,54661,70],{"emptyLinePlaceholder":69},[52,54663,54664,54666,54668,54670,54672,54674,54676,54678,54680],{"class":54,"line":148},[52,54665,53479],{"class":62},[52,54667,80],{"class":79},[52,54669,53484],{"class":62},[52,54671,186],{"class":102},[52,54673,53489],{"class":1320},[52,54675,5234],{"class":79},[52,54677,53484],{"class":62},[52,54679,186],{"class":102},[52,54681,54682],{"class":1320},"digits\n",[52,54684,54685],{"class":54,"line":164},[52,54686,70],{"emptyLinePlaceholder":69},[52,54688,54689,54691,54694,54696,54698,54701,54703],{"class":54,"line":170},[52,54690,1313],{"class":58},[52,54692,54693],{"class":62}," use_symbols ",[52,54695,11062],{"class":79},[52,54697,84],{"class":83},[52,54699,54700],{"class":87},"yes",[52,54702,115],{"class":83},[52,54704,1330],{"class":102},[52,54706,54707,54710,54712,54714,54716],{"class":54,"line":175},[52,54708,54709],{"class":62},"    characters ",[52,54711,34682],{"class":79},[52,54713,53484],{"class":62},[52,54715,186],{"class":102},[52,54717,53507],{"class":1320},[52,54719,54720],{"class":54,"line":214},[52,54721,70],{"emptyLinePlaceholder":69},[52,54723,54724,54726,54728,54730,54732,54734,54736,54738,54740,54742,54744,54746,54748,54750,54752,54754,54756,54758,54760],{"class":54,"line":233},[52,54725,26217],{"class":62},[52,54727,80],{"class":79},[52,54729,53516],{"class":83},[52,54731,186],{"class":102},[52,54733,34643],{"class":189},[52,54735,193],{"class":102},[52,54737,36610],{"class":189},[52,54739,186],{"class":102},[52,54741,12230],{"class":189},[52,54743,193],{"class":102},[52,54745,53533],{"class":189},[52,54747,1521],{"class":102},[52,54749,37077],{"class":58},[52,54751,37080],{"class":189},[52,54753,11213],{"class":58},[52,54755,36864],{"class":241},[52,54757,193],{"class":102},[52,54759,53548],{"class":189},[52,54761,8886],{"class":102},[52,54763,54764],{"class":54,"line":238},[52,54765,70],{"emptyLinePlaceholder":69},[52,54767,54768,54770,54772,54774,54776,54778,54780,54782],{"class":54,"line":272},[52,54769,242],{"class":241},[52,54771,193],{"class":102},[52,54773,115],{"class":83},[52,54775,54434],{"class":87},[52,54777,115],{"class":83},[52,54779,199],{"class":102},[52,54781,26396],{"class":189},[52,54783,211],{"class":102},[639,54785,54787],{"id":54786},"make-sure-the-password-has-at-least-one-number","Make sure the password has at least one number",[14,54789,54790],{},"A simple random password might not include a digit by chance.",[14,54792,54793],{},"This version guarantees at least one number:",[42,54795,54797],{"className":44,"code":54796,"language":46,"meta":47,"style":47},"import random\nimport string\n\nlength = 12\n\nletters = string.ascii_letters\ndigits = string.digits\nsymbols = string.punctuation\n\npassword_chars = [\n    random.choice(digits)\n]\n\nall_characters = letters + digits + symbols\n\nfor _ in range(length - 1):\n    password_chars.append(random.choice(all_characters))\n\nrandom.shuffle(password_chars)\npassword = ''.join(password_chars)\n\nprint(password)\n",[49,54798,54799,54805,54811,54815,54823,54827,54840,54853,54866,54870,54879,54894,54898,54902,54922,54926,54946,54970,54974,54990,55008,55012],{"__ignoreMap":47},[52,54800,54801,54803],{"class":54,"line":55},[52,54802,59],{"class":58},[52,54804,36646],{"class":62},[52,54806,54807,54809],{"class":54,"line":66},[52,54808,59],{"class":58},[52,54810,53460],{"class":62},[52,54812,54813],{"class":54,"line":73},[52,54814,70],{"emptyLinePlaceholder":69},[52,54816,54817,54819,54821],{"class":54,"line":94},[52,54818,53469],{"class":62},[52,54820,80],{"class":79},[52,54822,53474],{"class":121},[52,54824,54825],{"class":54,"line":106},[52,54826,70],{"emptyLinePlaceholder":69},[52,54828,54829,54831,54833,54835,54837],{"class":54,"line":128},[52,54830,54075],{"class":62},[52,54832,80],{"class":79},[52,54834,53484],{"class":62},[52,54836,186],{"class":102},[52,54838,54839],{"class":1320},"ascii_letters\n",[52,54841,54842,54845,54847,54849,54851],{"class":54,"line":148},[52,54843,54844],{"class":62},"digits ",[52,54846,80],{"class":79},[52,54848,53484],{"class":62},[52,54850,186],{"class":102},[52,54852,54682],{"class":1320},[52,54854,54855,54858,54860,54862,54864],{"class":54,"line":164},[52,54856,54857],{"class":62},"symbols ",[52,54859,80],{"class":79},[52,54861,53484],{"class":62},[52,54863,186],{"class":102},[52,54865,53507],{"class":1320},[52,54867,54868],{"class":54,"line":170},[52,54869,70],{"emptyLinePlaceholder":69},[52,54871,54872,54875,54877],{"class":54,"line":175},[52,54873,54874],{"class":62},"password_chars ",[52,54876,80],{"class":79},[52,54878,16627],{"class":102},[52,54880,54881,54884,54886,54888,54890,54892],{"class":54,"line":214},[52,54882,54883],{"class":62},"    random",[52,54885,186],{"class":102},[52,54887,12230],{"class":189},[52,54889,193],{"class":102},[52,54891,53498],{"class":189},[52,54893,211],{"class":102},[52,54895,54896],{"class":54,"line":233},[52,54897,1147],{"class":102},[52,54899,54900],{"class":54,"line":238},[52,54901,70],{"emptyLinePlaceholder":69},[52,54903,54904,54907,54909,54912,54914,54917,54919],{"class":54,"line":272},[52,54905,54906],{"class":62},"all_characters ",[52,54908,80],{"class":79},[52,54910,54911],{"class":62}," letters ",[52,54913,9881],{"class":79},[52,54915,54916],{"class":62}," digits ",[52,54918,9881],{"class":79},[52,54920,54921],{"class":62}," symbols\n",[52,54923,54924],{"class":54,"line":1348},[52,54925,70],{"emptyLinePlaceholder":69},[52,54927,54928,54930,54932,54934,54936,54938,54940,54942,54944],{"class":54,"line":1376},[52,54929,12265],{"class":58},[52,54931,37080],{"class":62},[52,54933,11213],{"class":58},[52,54935,36864],{"class":241},[52,54937,193],{"class":102},[52,54939,53469],{"class":189},[52,54941,5889],{"class":79},[52,54943,1518],{"class":121},[52,54945,3114],{"class":102},[52,54947,54948,54951,54953,54955,54957,54959,54961,54963,54965,54968],{"class":54,"line":1381},[52,54949,54950],{"class":62},"    password_chars",[52,54952,186],{"class":102},[52,54954,18748],{"class":189},[52,54956,193],{"class":102},[52,54958,36610],{"class":189},[52,54960,186],{"class":102},[52,54962,12230],{"class":189},[52,54964,193],{"class":102},[52,54966,54967],{"class":189},"all_characters",[52,54969,8886],{"class":102},[52,54971,54972],{"class":54,"line":1406},[52,54973,70],{"emptyLinePlaceholder":69},[52,54975,54976,54978,54980,54983,54985,54988],{"class":54,"line":1430},[52,54977,36610],{"class":62},[52,54979,186],{"class":102},[52,54981,54982],{"class":189},"shuffle",[52,54984,193],{"class":102},[52,54986,54987],{"class":189},"password_chars",[52,54989,211],{"class":102},[52,54991,54992,54994,54996,54998,55000,55002,55004,55006],{"class":54,"line":1435},[52,54993,26217],{"class":62},[52,54995,80],{"class":79},[52,54997,53516],{"class":83},[52,54999,186],{"class":102},[52,55001,34643],{"class":189},[52,55003,193],{"class":102},[52,55005,54987],{"class":189},[52,55007,211],{"class":102},[52,55009,55010],{"class":54,"line":1467},[52,55011,70],{"emptyLinePlaceholder":69},[52,55013,55014,55016,55018,55020],{"class":54,"line":1495},[52,55015,242],{"class":241},[52,55017,193],{"class":102},[52,55019,53563],{"class":189},[52,55021,211],{"class":102},[639,55023,55025],{"id":55024},"make-sure-the-password-has-at-least-one-symbol","Make sure the password has at least one symbol",[14,55027,55028],{},"You can do the same for symbols:",[42,55030,55032],{"className":44,"code":55031,"language":46,"meta":47,"style":47},"import random\nimport string\n\nlength = 12\n\nletters = string.ascii_letters\ndigits = string.digits\nsymbols = string.punctuation\n\npassword_chars = [\n    random.choice(digits),\n    random.choice(symbols)\n]\n\nall_characters = letters + digits + symbols\n\nfor _ in range(length - 2):\n    password_chars.append(random.choice(all_characters))\n\nrandom.shuffle(password_chars)\npassword = ''.join(password_chars)\n\nprint(password)\n",[49,55033,55034,55040,55046,55050,55058,55062,55074,55086,55098,55102,55110,55125,55139,55143,55147,55163,55167,55187,55209,55213,55227,55245,55249],{"__ignoreMap":47},[52,55035,55036,55038],{"class":54,"line":55},[52,55037,59],{"class":58},[52,55039,36646],{"class":62},[52,55041,55042,55044],{"class":54,"line":66},[52,55043,59],{"class":58},[52,55045,53460],{"class":62},[52,55047,55048],{"class":54,"line":73},[52,55049,70],{"emptyLinePlaceholder":69},[52,55051,55052,55054,55056],{"class":54,"line":94},[52,55053,53469],{"class":62},[52,55055,80],{"class":79},[52,55057,53474],{"class":121},[52,55059,55060],{"class":54,"line":106},[52,55061,70],{"emptyLinePlaceholder":69},[52,55063,55064,55066,55068,55070,55072],{"class":54,"line":128},[52,55065,54075],{"class":62},[52,55067,80],{"class":79},[52,55069,53484],{"class":62},[52,55071,186],{"class":102},[52,55073,54839],{"class":1320},[52,55075,55076,55078,55080,55082,55084],{"class":54,"line":148},[52,55077,54844],{"class":62},[52,55079,80],{"class":79},[52,55081,53484],{"class":62},[52,55083,186],{"class":102},[52,55085,54682],{"class":1320},[52,55087,55088,55090,55092,55094,55096],{"class":54,"line":164},[52,55089,54857],{"class":62},[52,55091,80],{"class":79},[52,55093,53484],{"class":62},[52,55095,186],{"class":102},[52,55097,53507],{"class":1320},[52,55099,55100],{"class":54,"line":170},[52,55101,70],{"emptyLinePlaceholder":69},[52,55103,55104,55106,55108],{"class":54,"line":175},[52,55105,54874],{"class":62},[52,55107,80],{"class":79},[52,55109,16627],{"class":102},[52,55111,55112,55114,55116,55118,55120,55122],{"class":54,"line":214},[52,55113,54883],{"class":62},[52,55115,186],{"class":102},[52,55117,12230],{"class":189},[52,55119,193],{"class":102},[52,55121,53498],{"class":189},[52,55123,55124],{"class":102},"),\n",[52,55126,55127,55129,55131,55133,55135,55137],{"class":54,"line":233},[52,55128,54883],{"class":62},[52,55130,186],{"class":102},[52,55132,12230],{"class":189},[52,55134,193],{"class":102},[52,55136,53578],{"class":189},[52,55138,211],{"class":102},[52,55140,55141],{"class":54,"line":238},[52,55142,1147],{"class":102},[52,55144,55145],{"class":54,"line":272},[52,55146,70],{"emptyLinePlaceholder":69},[52,55148,55149,55151,55153,55155,55157,55159,55161],{"class":54,"line":1348},[52,55150,54906],{"class":62},[52,55152,80],{"class":79},[52,55154,54911],{"class":62},[52,55156,9881],{"class":79},[52,55158,54916],{"class":62},[52,55160,9881],{"class":79},[52,55162,54921],{"class":62},[52,55164,55165],{"class":54,"line":1376},[52,55166,70],{"emptyLinePlaceholder":69},[52,55168,55169,55171,55173,55175,55177,55179,55181,55183,55185],{"class":54,"line":1381},[52,55170,12265],{"class":58},[52,55172,37080],{"class":62},[52,55174,11213],{"class":58},[52,55176,36864],{"class":241},[52,55178,193],{"class":102},[52,55180,53469],{"class":189},[52,55182,5889],{"class":79},[52,55184,9168],{"class":121},[52,55186,3114],{"class":102},[52,55188,55189,55191,55193,55195,55197,55199,55201,55203,55205,55207],{"class":54,"line":1406},[52,55190,54950],{"class":62},[52,55192,186],{"class":102},[52,55194,18748],{"class":189},[52,55196,193],{"class":102},[52,55198,36610],{"class":189},[52,55200,186],{"class":102},[52,55202,12230],{"class":189},[52,55204,193],{"class":102},[52,55206,54967],{"class":189},[52,55208,8886],{"class":102},[52,55210,55211],{"class":54,"line":1430},[52,55212,70],{"emptyLinePlaceholder":69},[52,55214,55215,55217,55219,55221,55223,55225],{"class":54,"line":1435},[52,55216,36610],{"class":62},[52,55218,186],{"class":102},[52,55220,54982],{"class":189},[52,55222,193],{"class":102},[52,55224,54987],{"class":189},[52,55226,211],{"class":102},[52,55228,55229,55231,55233,55235,55237,55239,55241,55243],{"class":54,"line":1467},[52,55230,26217],{"class":62},[52,55232,80],{"class":79},[52,55234,53516],{"class":83},[52,55236,186],{"class":102},[52,55238,34643],{"class":189},[52,55240,193],{"class":102},[52,55242,54987],{"class":189},[52,55244,211],{"class":102},[52,55246,55247],{"class":54,"line":1495},[52,55248,70],{"emptyLinePlaceholder":69},[52,55250,55251,55253,55255,55257],{"class":54,"line":1531},[52,55252,242],{"class":241},[52,55254,193],{"class":102},[52,55256,53563],{"class":189},[52,55258,211],{"class":102},[639,55260,55262],{"id":55261},"generate-multiple-passwords","Generate multiple passwords",[14,55264,55265],{},"You can print several passwords in one run:",[42,55267,55269],{"className":44,"code":55268,"language":46,"meta":47,"style":47},"import random\nimport string\n\nlength = 10\ncount = 5\ncharacters = string.ascii_letters + string.digits + string.punctuation\n\nfor _ in range(count):\n    password = ''.join(random.choice(characters) for _ in range(length))\n    print(password)\n",[49,55270,55271,55277,55283,55287,55296,55305,55333,55337,55353,55394],{"__ignoreMap":47},[52,55272,55273,55275],{"class":54,"line":55},[52,55274,59],{"class":58},[52,55276,36646],{"class":62},[52,55278,55279,55281],{"class":54,"line":66},[52,55280,59],{"class":58},[52,55282,53460],{"class":62},[52,55284,55285],{"class":54,"line":73},[52,55286,70],{"emptyLinePlaceholder":69},[52,55288,55289,55291,55293],{"class":54,"line":94},[52,55290,53469],{"class":62},[52,55292,80],{"class":79},[52,55294,55295],{"class":121}," 10\n",[52,55297,55298,55301,55303],{"class":54,"line":106},[52,55299,55300],{"class":62},"count ",[52,55302,80],{"class":79},[52,55304,5264],{"class":121},[52,55306,55307,55309,55311,55313,55315,55317,55319,55321,55323,55325,55327,55329,55331],{"class":54,"line":128},[52,55308,53479],{"class":62},[52,55310,80],{"class":79},[52,55312,53484],{"class":62},[52,55314,186],{"class":102},[52,55316,53489],{"class":1320},[52,55318,5234],{"class":79},[52,55320,53484],{"class":62},[52,55322,186],{"class":102},[52,55324,53498],{"class":1320},[52,55326,5234],{"class":79},[52,55328,53484],{"class":62},[52,55330,186],{"class":102},[52,55332,53507],{"class":1320},[52,55334,55335],{"class":54,"line":148},[52,55336,70],{"emptyLinePlaceholder":69},[52,55338,55339,55341,55343,55345,55347,55349,55351],{"class":54,"line":164},[52,55340,12265],{"class":58},[52,55342,37080],{"class":62},[52,55344,11213],{"class":58},[52,55346,36864],{"class":241},[52,55348,193],{"class":102},[52,55350,45367],{"class":189},[52,55352,3114],{"class":102},[52,55354,55355,55358,55360,55362,55364,55366,55368,55370,55372,55374,55376,55378,55380,55382,55384,55386,55388,55390,55392],{"class":54,"line":170},[52,55356,55357],{"class":62},"    password ",[52,55359,80],{"class":79},[52,55361,53516],{"class":83},[52,55363,186],{"class":102},[52,55365,34643],{"class":189},[52,55367,193],{"class":102},[52,55369,36610],{"class":189},[52,55371,186],{"class":102},[52,55373,12230],{"class":189},[52,55375,193],{"class":102},[52,55377,53533],{"class":189},[52,55379,1521],{"class":102},[52,55381,37077],{"class":58},[52,55383,37080],{"class":189},[52,55385,11213],{"class":58},[52,55387,36864],{"class":241},[52,55389,193],{"class":102},[52,55391,53548],{"class":189},[52,55393,8886],{"class":102},[52,55395,55396,55398,55400,55402],{"class":54,"line":175},[52,55397,1599],{"class":241},[52,55399,193],{"class":102},[52,55401,53563],{"class":189},[52,55403,211],{"class":102},[14,55405,55406,55407,186],{},"If you want a larger example like this, see the ",[372,55408,55410],{"href":55409},"\u002Fexamples\u002Fpython-random-password-list-generator-example\u002F","Python random password list generator example",[639,55412,55414],{"id":55413},"save-passwords-to-a-file-only-if-needed","Save passwords to a file only if needed",[14,55416,55417],{},"You can save generated passwords to a file, but be careful.",[14,55419,55420],{},"If a file contains real passwords, anyone who can open that file may be able to read them.",[14,55422,55423],{},"For learning, it is usually better to print them to the screen first.",[53580,55425],{},[37,55427,55429],{"id":55428},"security-note-for-beginners","Security note for beginners",[14,55431,358,55432,55434],{},[49,55433,36610],{}," module is fine for learning examples like this one.",[14,55436,55437,55438,55441],{},"For real password generation, use the ",[49,55439,55440],{},"secrets"," module instead. It is designed for security-related tasks.",[14,55443,15138],{},[309,55445,55446,55451],{},[24,55447,23564,55448,55450],{},[49,55449,36610],{}," to learn the idea",[24,55452,23564,55453,55455],{},[49,55454,55440],{}," for real applications",[14,55457,55458],{},"You do not need to turn this into a full security project right away. First, make sure you understand how the basic version works.",[53580,55460],{},[37,55462,29638],{"id":29637},[14,55464,55465],{},"Here are some common mistakes beginners run into.",[639,55467,14067,55469,857,55471],{"id":55468},"forgetting-to-import-random-or-string",[49,55470,36610],{},[49,55472,41855],{},[14,55474,37426,55475,55477,55478,857,55480,17143],{},[49,55476,14080],{}," because Python does not know what ",[49,55479,36610],{},[49,55481,41855],{},[14,55483,8124],{},[42,55485,55487],{"className":44,"code":55486,"language":46,"meta":47,"style":47},"length = 12\ncharacters = string.ascii_letters + string.digits\npassword = ''.join(random.choice(characters) for _ in range(length))\n",[49,55488,55489,55497,55517],{"__ignoreMap":47},[52,55490,55491,55493,55495],{"class":54,"line":55},[52,55492,53469],{"class":62},[52,55494,80],{"class":79},[52,55496,53474],{"class":121},[52,55498,55499,55501,55503,55505,55507,55509,55511,55513,55515],{"class":54,"line":66},[52,55500,53479],{"class":62},[52,55502,80],{"class":79},[52,55504,53484],{"class":62},[52,55506,186],{"class":102},[52,55508,53489],{"class":1320},[52,55510,5234],{"class":79},[52,55512,53484],{"class":62},[52,55514,186],{"class":102},[52,55516,54682],{"class":1320},[52,55518,55519,55521,55523,55525,55527,55529,55531,55533,55535,55537,55539,55541,55543,55545,55547,55549,55551,55553,55555],{"class":54,"line":73},[52,55520,26217],{"class":62},[52,55522,80],{"class":79},[52,55524,53516],{"class":83},[52,55526,186],{"class":102},[52,55528,34643],{"class":189},[52,55530,193],{"class":102},[52,55532,36610],{"class":189},[52,55534,186],{"class":102},[52,55536,12230],{"class":189},[52,55538,193],{"class":102},[52,55540,53533],{"class":189},[52,55542,1521],{"class":102},[52,55544,37077],{"class":58},[52,55546,37080],{"class":189},[52,55548,11213],{"class":58},[52,55550,36864],{"class":241},[52,55552,193],{"class":102},[52,55554,53548],{"class":189},[52,55556,8886],{"class":102},[14,55558,14146],{},[42,55560,55562],{"className":44,"code":55561,"language":46,"meta":47,"style":47},"import random\nimport string\n",[49,55563,55564,55570],{"__ignoreMap":47},[52,55565,55566,55568],{"class":54,"line":55},[52,55567,59],{"class":58},[52,55569,36646],{"class":62},[52,55571,55572,55574],{"class":54,"line":66},[52,55573,59],{"class":58},[52,55575,53460],{"class":62},[639,55577,10294,55579,55581,55582],{"id":55578},"using-input-without-converting-to-int",[49,55580,10597],{}," without converting to ",[49,55583,5142],{},[14,55585,55586,55588],{},[49,55587,10597],{}," always returns text.",[14,55590,6505],{},[42,55592,55594],{"className":44,"code":55593,"language":46,"meta":47,"style":47},"length = input(\"Enter password length: \")\n\nfor _ in range(length):\n    print(\"x\")\n",[49,55595,55596,55614,55618,55634],{"__ignoreMap":47},[52,55597,55598,55600,55602,55604,55606,55608,55610,55612],{"class":54,"line":55},[52,55599,53469],{"class":62},[52,55601,80],{"class":79},[52,55603,11037],{"class":241},[52,55605,193],{"class":102},[52,55607,115],{"class":83},[52,55609,54347],{"class":87},[52,55611,115],{"class":83},[52,55613,211],{"class":102},[52,55615,55616],{"class":54,"line":66},[52,55617,70],{"emptyLinePlaceholder":69},[52,55619,55620,55622,55624,55626,55628,55630,55632],{"class":54,"line":73},[52,55621,12265],{"class":58},[52,55623,37080],{"class":62},[52,55625,11213],{"class":58},[52,55627,36864],{"class":241},[52,55629,193],{"class":102},[52,55631,53548],{"class":189},[52,55633,3114],{"class":102},[52,55635,55636,55638,55640,55642,55644,55646],{"class":54,"line":94},[52,55637,1599],{"class":241},[52,55639,193],{"class":102},[52,55641,115],{"class":83},[52,55643,5130],{"class":87},[52,55645,115],{"class":83},[52,55647,211],{"class":102},[14,55649,37426,55650,1694,55652,55654,55655,186],{},[49,55651,1707],{},[49,55653,37187],{}," needs an integer, not a string. See ",[372,55656,55657],{"href":6886},"how to fix TypeError: 'str' object cannot be interpreted as an integer",[14,55659,14146],{},[42,55661,55662],{"className":44,"code":54529,"language":46,"meta":47,"style":47},[49,55663,55664],{"__ignoreMap":47},[52,55665,55666,55668,55670,55672,55674,55676,55678,55680,55682,55684],{"class":54,"line":55},[52,55667,53469],{"class":62},[52,55669,80],{"class":79},[52,55671,9789],{"class":4733},[52,55673,193],{"class":102},[52,55675,13610],{"class":241},[52,55677,193],{"class":102},[52,55679,115],{"class":83},[52,55681,54347],{"class":87},[52,55683,115],{"class":83},[52,55685,8886],{"class":102},[639,55687,55689,55690],{"id":55688},"choosing-a-password-length-of-0","Choosing a password length of ",[49,55691,13294],{},[14,55693,55694],{},"This does not crash, but it gives you an empty password:",[42,55696,55698],{"className":44,"code":55697,"language":46,"meta":47,"style":47},"import random\nimport string\n\nlength = 0\ncharacters = string.ascii_letters + string.digits + string.punctuation\npassword = ''.join(random.choice(characters) for _ in range(length))\n\nprint(password)\n",[49,55699,55700,55706,55712,55716,55724,55752,55792,55796],{"__ignoreMap":47},[52,55701,55702,55704],{"class":54,"line":55},[52,55703,59],{"class":58},[52,55705,36646],{"class":62},[52,55707,55708,55710],{"class":54,"line":66},[52,55709,59],{"class":58},[52,55711,53460],{"class":62},[52,55713,55714],{"class":54,"line":73},[52,55715,70],{"emptyLinePlaceholder":69},[52,55717,55718,55720,55722],{"class":54,"line":94},[52,55719,53469],{"class":62},[52,55721,80],{"class":79},[52,55723,34598],{"class":121},[52,55725,55726,55728,55730,55732,55734,55736,55738,55740,55742,55744,55746,55748,55750],{"class":54,"line":106},[52,55727,53479],{"class":62},[52,55729,80],{"class":79},[52,55731,53484],{"class":62},[52,55733,186],{"class":102},[52,55735,53489],{"class":1320},[52,55737,5234],{"class":79},[52,55739,53484],{"class":62},[52,55741,186],{"class":102},[52,55743,53498],{"class":1320},[52,55745,5234],{"class":79},[52,55747,53484],{"class":62},[52,55749,186],{"class":102},[52,55751,53507],{"class":1320},[52,55753,55754,55756,55758,55760,55762,55764,55766,55768,55770,55772,55774,55776,55778,55780,55782,55784,55786,55788,55790],{"class":54,"line":128},[52,55755,26217],{"class":62},[52,55757,80],{"class":79},[52,55759,53516],{"class":83},[52,55761,186],{"class":102},[52,55763,34643],{"class":189},[52,55765,193],{"class":102},[52,55767,36610],{"class":189},[52,55769,186],{"class":102},[52,55771,12230],{"class":189},[52,55773,193],{"class":102},[52,55775,53533],{"class":189},[52,55777,1521],{"class":102},[52,55779,37077],{"class":58},[52,55781,37080],{"class":189},[52,55783,11213],{"class":58},[52,55785,36864],{"class":241},[52,55787,193],{"class":102},[52,55789,53548],{"class":189},[52,55791,8886],{"class":102},[52,55793,55794],{"class":54,"line":148},[52,55795,70],{"emptyLinePlaceholder":69},[52,55797,55798,55800,55802,55804],{"class":54,"line":164},[52,55799,242],{"class":241},[52,55801,193],{"class":102},[52,55803,53563],{"class":189},[52,55805,211],{"class":102},[14,55807,5267],{},[42,55809,55810],{"className":44,"code":47,"language":46,"meta":47,"style":47},[49,55811,55812],{"__ignoreMap":47},[52,55813],{"class":54,"line":55},[14,55815,55816],{},"A simple fix is to check the value first:",[42,55818,55820],{"className":44,"code":55819,"language":46,"meta":47,"style":47},"if length \u003C= 0:\n    print(\"Please enter a number greater than 0.\")\n",[49,55821,55822,55836],{"__ignoreMap":47},[52,55823,55824,55826,55829,55832,55834],{"class":54,"line":55},[52,55825,1313],{"class":58},[52,55827,55828],{"class":62}," length ",[52,55830,55831],{"class":79},"\u003C=",[52,55833,13049],{"class":121},[52,55835,1330],{"class":102},[52,55837,55838,55840,55842,55844,55847,55849],{"class":54,"line":66},[52,55839,1599],{"class":241},[52,55841,193],{"class":102},[52,55843,115],{"class":83},[52,55845,55846],{"class":87},"Please enter a number greater than 0.",[52,55848,115],{"class":83},[52,55850,211],{"class":102},[639,55852,55854,55855],{"id":55853},"accidentally-reusing-variable-names-like-string","Accidentally reusing variable names like ",[49,55856,41855],{},[14,55858,55859],{},"Be careful not to overwrite the module name.",[14,55861,8124],{},[42,55863,55865],{"className":44,"code":55864,"language":46,"meta":47,"style":47},"import string\n\nstring = \"hello\"\nprint(string.ascii_letters)\n",[49,55866,55867,55873,55877,55890],{"__ignoreMap":47},[52,55868,55869,55871],{"class":54,"line":55},[52,55870,59],{"class":58},[52,55872,53460],{"class":62},[52,55874,55875],{"class":54,"line":66},[52,55876,70],{"emptyLinePlaceholder":69},[52,55878,55879,55882,55884,55886,55888],{"class":54,"line":73},[52,55880,55881],{"class":62},"string ",[52,55883,80],{"class":79},[52,55885,84],{"class":83},[52,55887,6363],{"class":87},[52,55889,91],{"class":83},[52,55891,55892,55894,55896,55898,55900,55902],{"class":54,"line":94},[52,55893,242],{"class":241},[52,55895,193],{"class":102},[52,55897,41855],{"class":189},[52,55899,186],{"class":102},[52,55901,53489],{"class":1320},[52,55903,211],{"class":102},[14,55905,53980,55906,55908],{},[49,55907,41855],{}," is a normal text value, not the module. That breaks your code.",[14,55910,55911],{},"Use a different variable name instead.",[53580,55913],{},[37,55915,1942],{"id":1941},[639,55917,55919],{"id":55918},"how-do-i-generate-a-password-in-python","How do I generate a password in Python?",[14,55921,55922],{},"Create a string of allowed characters, pick random characters, and join them into one string.",[639,55924,3638,55926,857,55928,1995],{"id":55925},"should-i-use-random-or-secrets",[49,55927,36610],{},[49,55929,55440],{},[14,55931,3650,55932,55934,55935,55937],{},[49,55933,36610],{}," for simple learning examples. Use ",[49,55936,55440],{}," for real password generation.",[639,55939,55941],{"id":55940},"why-does-input-length-cause-an-error","Why does input length cause an error?",[14,55943,55944,55946,55947,55949,55950,186],{},[49,55945,10597],{}," returns text. Convert it with ",[49,55948,9492],{}," before using it in ",[49,55951,37187],{},[639,55953,55955],{"id":55954},"how-can-i-include-numbers-and-symbols","How can I include numbers and symbols?",[14,55957,55958,55959,759,55961,55963],{},"Add ",[49,55960,53824],{},[49,55962,53883],{}," to the allowed characters.",[53580,55965],{},[37,55967,2005],{"id":2004},[309,55969,55970,55974,55978,55982,55986,55992],{},[24,55971,55972],{},[372,55973,36730],{"href":36729},[24,55975,55976],{},[372,55977,13726],{"href":10606},[24,55979,55980],{},[372,55981,6876],{"href":6397},[24,55983,55984],{},[372,55985,13737],{"href":13736},[24,55987,55988],{},[372,55989,55991],{"href":55990},"\u002Fexamples\u002Fpython-random-name-picker-example\u002F","Python random name picker example",[24,55993,55994],{},[372,55995,55410],{"href":55409},[14,55997,55998],{},"Try improving this script next by:",[309,56000,56001,56004,56007,56010],{},[24,56002,56003],{},"asking the user for password length",[24,56005,56006],{},"adding an option to include symbols",[24,56008,56009],{},"making sure the password contains at least one number",[24,56011,56012,56013,14928],{},"then building a more secure version with the ",[49,56014,55440],{},[2034,56016,56017],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--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 .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 .sx12J, html code.shiki .sx12J{--shiki-light:#F76D47;--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_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":56019},[56020,56021,56022,56032,56037,56044,56045,56055,56062],{"id":322,"depth":66,"text":323},{"id":25640,"depth":66,"text":25641},{"id":53764,"depth":66,"text":53765,"children":56023},[56024,56025,56026,56027,56029,56030,56031],{"id":53768,"depth":73,"text":53771},{"id":53821,"depth":73,"text":53824},{"id":53880,"depth":73,"text":53883},{"id":53938,"depth":73,"text":56028},"Building the characters string",{"id":53986,"depth":73,"text":53989},{"id":34643,"depth":73,"text":54057},{"id":54159,"depth":73,"text":54160},{"id":54303,"depth":66,"text":54304,"children":56033},[56034,56035,56036],{"id":9909,"depth":73,"text":9910},{"id":54481,"depth":73,"text":54482},{"id":54519,"depth":73,"text":54520},{"id":54578,"depth":66,"text":54579,"children":56038},[56039,56040,56041,56042,56043],{"id":54585,"depth":73,"text":54586},{"id":54786,"depth":73,"text":54787},{"id":55024,"depth":73,"text":55025},{"id":55261,"depth":73,"text":55262},{"id":55413,"depth":73,"text":55414},{"id":55428,"depth":66,"text":55429},{"id":29637,"depth":66,"text":29638,"children":56046},[56047,56049,56051,56053],{"id":55468,"depth":73,"text":56048},"Forgetting to import random or string",{"id":55578,"depth":73,"text":56050},"Using input() without converting to int",{"id":55688,"depth":73,"text":56052},"Choosing a password length of 0",{"id":55853,"depth":73,"text":56054},"Accidentally reusing variable names like string",{"id":1941,"depth":66,"text":1942,"children":56056},[56057,56058,56060,56061],{"id":55918,"depth":73,"text":55919},{"id":55925,"depth":73,"text":56059},"Should I use random or secrets?",{"id":55940,"depth":73,"text":55941},{"id":55954,"depth":73,"text":55955},{"id":2004,"depth":66,"text":2005},"Master python password generator example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-password-generator-example",{"title":53415,"description":56063},"examples\u002Fpython-password-generator-example","ffpsKlqQz6a2ogWHjXGFE_YpygvM2E55PteMoEa1OHw",{"id":56070,"title":56071,"body":56072,"description":57542,"extension":2075,"meta":57543,"navigation":69,"path":57544,"seo":57545,"stem":57546,"__hash__":57547},"content\u002Fexamples\u002Fpython-ping-script-example.md","Python Ping Script Example",{"type":7,"value":56073,"toc":57513},[56074,56077,56084,56087,56101,56111,56113,56315,56323,56325,56328,56331,56354,56357,56361,56364,56378,56388,56392,56395,56592,56594,56644,56648,56654,56673,56676,56679,56706,56712,56718,56773,56776,56779,56793,56806,56812,56826,56829,56835,56841,56854,56857,56862,56868,56885,56891,56897,56899,56905,56907,56913,56916,56922,56925,56936,56939,56941,56944,56967,56969,56989,56991,56994,56997,57014,57017,57223,57229,57233,57236,57256,57259,57292,57295,57299,57302,57348,57351,57384,57395,57397,57401,57409,57418,57423,57436,57440,57443,57447,57450,57463,57467,57470,57473,57487,57489,57507,57510],[10,56075,56071],{"id":56076},"python-ping-script-example",[14,56078,56079,56080,56083],{},"This beginner-friendly example shows how to make a simple Python ping script by running your operating system's ",[49,56081,56082],{},"ping"," command.",[14,56085,56086],{},"This is useful when you want to:",[309,56088,56089,56092,56095,56098],{},[24,56090,56091],{},"check whether a host can be reached",[24,56093,56094],{},"test basic network connectivity",[24,56096,56097],{},"learn how Python can run terminal commands",[24,56099,56100],{},"build a small troubleshooting script",[14,56102,56103,56104,56107,56108,186],{},"Python does not have a simple built-in ",[49,56105,56106],{},"ping()"," function for this. A common beginner approach is to run the system command with ",[49,56109,56110],{},"subprocess",[37,56112,40],{"id":39},[42,56114,56116],{"className":44,"code":56115,"language":46,"meta":47,"style":47},"import platform\nimport subprocess\n\nhost = \"google.com\"\nparam = \"-n\" if platform.system().lower() == \"windows\" else \"-c\"\nresult = subprocess.run([\"ping\", param, \"4\", host], capture_output=True, text=True)\n\nprint(result.stdout)\nprint(\"Reachable\" if result.returncode == 0 else \"Not reachable\")\n",[49,56117,56118,56125,56132,56136,56150,56199,56259,56263,56278],{"__ignoreMap":47},[52,56119,56120,56122],{"class":54,"line":55},[52,56121,59],{"class":58},[52,56123,56124],{"class":62}," platform\n",[52,56126,56127,56129],{"class":54,"line":66},[52,56128,59],{"class":58},[52,56130,56131],{"class":62}," subprocess\n",[52,56133,56134],{"class":54,"line":73},[52,56135,70],{"emptyLinePlaceholder":69},[52,56137,56138,56141,56143,56145,56148],{"class":54,"line":94},[52,56139,56140],{"class":62},"host ",[52,56142,80],{"class":79},[52,56144,84],{"class":83},[52,56146,56147],{"class":87},"google.com",[52,56149,91],{"class":83},[52,56151,56152,56155,56157,56159,56162,56164,56166,56169,56171,56174,56176,56178,56180,56182,56184,56187,56189,56192,56194,56197],{"class":54,"line":106},[52,56153,56154],{"class":62},"param ",[52,56156,80],{"class":79},[52,56158,84],{"class":83},[52,56160,56161],{"class":87},"-n",[52,56163,115],{"class":83},[52,56165,18726],{"class":58},[52,56167,56168],{"class":62}," platform",[52,56170,186],{"class":102},[52,56172,56173],{"class":189},"system",[52,56175,18679],{"class":102},[52,56177,30237],{"class":189},[52,56179,8267],{"class":102},[52,56181,1324],{"class":79},[52,56183,84],{"class":83},[52,56185,56186],{"class":87},"windows",[52,56188,115],{"class":83},[52,56190,56191],{"class":58}," else",[52,56193,84],{"class":83},[52,56195,56196],{"class":87},"-c",[52,56198,91],{"class":83},[52,56200,56201,56203,56205,56208,56210,56213,56215,56217,56219,56221,56223,56226,56228,56230,56232,56234,56236,56239,56241,56244,56246,56248,56250,56253,56255,56257],{"class":54,"line":128},[52,56202,32778],{"class":62},[52,56204,80],{"class":79},[52,56206,56207],{"class":62}," subprocess",[52,56209,186],{"class":102},[52,56211,56212],{"class":189},"run",[52,56214,17632],{"class":102},[52,56216,115],{"class":83},[52,56218,56082],{"class":87},[52,56220,115],{"class":83},[52,56222,199],{"class":102},[52,56224,56225],{"class":189}," param",[52,56227,199],{"class":102},[52,56229,84],{"class":83},[52,56231,11366],{"class":87},[52,56233,115],{"class":83},[52,56235,199],{"class":102},[52,56237,56238],{"class":189}," host",[52,56240,15661],{"class":102},[52,56242,56243],{"class":202}," capture_output",[52,56245,80],{"class":79},[52,56247,6233],{"class":160},[52,56249,199],{"class":102},[52,56251,56252],{"class":202}," text",[52,56254,80],{"class":79},[52,56256,6233],{"class":160},[52,56258,211],{"class":102},[52,56260,56261],{"class":54,"line":148},[52,56262,70],{"emptyLinePlaceholder":69},[52,56264,56265,56267,56269,56271,56273,56276],{"class":54,"line":164},[52,56266,242],{"class":241},[52,56268,193],{"class":102},[52,56270,32811],{"class":189},[52,56272,186],{"class":102},[52,56274,56275],{"class":1320},"stdout",[52,56277,211],{"class":102},[52,56279,56280,56282,56284,56286,56289,56291,56293,56295,56297,56300,56302,56304,56306,56308,56311,56313],{"class":54,"line":170},[52,56281,242],{"class":241},[52,56283,193],{"class":102},[52,56285,115],{"class":83},[52,56287,56288],{"class":87},"Reachable",[52,56290,115],{"class":83},[52,56292,18726],{"class":58},[52,56294,27130],{"class":189},[52,56296,186],{"class":102},[52,56298,56299],{"class":1320},"returncode",[52,56301,1324],{"class":79},[52,56303,13049],{"class":121},[52,56305,56191],{"class":58},[52,56307,84],{"class":83},[52,56309,56310],{"class":87},"Not reachable",[52,56312,115],{"class":83},[52,56314,211],{"class":102},[14,56316,56317,56319,56320,56322],{},[5503,56318,16852],{}," This uses the operating system ",[49,56321,56082],{}," command. It works as a simple beginner script, but the exact output depends on your system.",[37,56324,28680],{"id":28679},[14,56326,56327],{},"This script checks whether a host can be reached over the network.",[14,56329,56330],{},"It does that by:",[309,56332,56333,56339,56345,56348,56351],{},[24,56334,56335,56336,56338],{},"choosing the correct ",[49,56337,56082],{}," option for your operating system",[24,56340,56341,56342,56344],{},"running the external ",[49,56343,56082],{}," command",[24,56346,56347],{},"collecting the command output",[24,56349,56350],{},"checking the return code",[24,56352,56353],{},"printing a simple result message",[14,56355,56356],{},"This is a good example for learning and small utility scripts. It is not a full monitoring system.",[37,56358,56360],{"id":56359},"when-to-use-a-python-ping-script","When to use a Python ping script",[14,56362,56363],{},"A script like this is useful when you want to:",[309,56365,56366,56369,56372,56375],{},[24,56367,56368],{},"check if a website or device responds",[24,56370,56371],{},"test network reachability in a simple script",[24,56373,56374],{},"learn how to run terminal commands from Python",[24,56376,56377],{},"build small monitoring or troubleshooting tools",[14,56379,56380,56381,14438,56385,186],{},"If you want more practice with scripts that run checks and report results, see the ",[372,56382,56384],{"href":56383},"\u002Fexamples\u002Fpython-url-checker-script-example\u002F","Python URL checker script example",[372,56386,56387],{"href":6790},"Python command-line tool example",[37,56389,56391],{"id":56390},"how-the-example-works","How the example works",[14,56393,56394],{},"Here is the example again:",[42,56396,56398],{"className":44,"code":56397,"language":46,"meta":47,"style":47},"import platform\nimport subprocess\n\nhost = \"google.com\"\nparam = \"-n\" if platform.system().lower() == \"windows\" else \"-c\"\n\nresult = subprocess.run(\n    [\"ping\", param, \"4\", host],\n    capture_output=True,\n    text=True\n)\n\nprint(result.stdout)\nprint(\"Reachable\" if result.returncode == 0 else \"Not reachable\")\n",[49,56399,56400,56406,56412,56416,56428,56470,56474,56488,56516,56527,56536,56540,56544,56558],{"__ignoreMap":47},[52,56401,56402,56404],{"class":54,"line":55},[52,56403,59],{"class":58},[52,56405,56124],{"class":62},[52,56407,56408,56410],{"class":54,"line":66},[52,56409,59],{"class":58},[52,56411,56131],{"class":62},[52,56413,56414],{"class":54,"line":73},[52,56415,70],{"emptyLinePlaceholder":69},[52,56417,56418,56420,56422,56424,56426],{"class":54,"line":94},[52,56419,56140],{"class":62},[52,56421,80],{"class":79},[52,56423,84],{"class":83},[52,56425,56147],{"class":87},[52,56427,91],{"class":83},[52,56429,56430,56432,56434,56436,56438,56440,56442,56444,56446,56448,56450,56452,56454,56456,56458,56460,56462,56464,56466,56468],{"class":54,"line":106},[52,56431,56154],{"class":62},[52,56433,80],{"class":79},[52,56435,84],{"class":83},[52,56437,56161],{"class":87},[52,56439,115],{"class":83},[52,56441,18726],{"class":58},[52,56443,56168],{"class":62},[52,56445,186],{"class":102},[52,56447,56173],{"class":189},[52,56449,18679],{"class":102},[52,56451,30237],{"class":189},[52,56453,8267],{"class":102},[52,56455,1324],{"class":79},[52,56457,84],{"class":83},[52,56459,56186],{"class":87},[52,56461,115],{"class":83},[52,56463,56191],{"class":58},[52,56465,84],{"class":83},[52,56467,56196],{"class":87},[52,56469,91],{"class":83},[52,56471,56472],{"class":54,"line":128},[52,56473,70],{"emptyLinePlaceholder":69},[52,56475,56476,56478,56480,56482,56484,56486],{"class":54,"line":148},[52,56477,32778],{"class":62},[52,56479,80],{"class":79},[52,56481,56207],{"class":62},[52,56483,186],{"class":102},[52,56485,56212],{"class":189},[52,56487,6050],{"class":102},[52,56489,56490,56492,56494,56496,56498,56500,56502,56504,56506,56508,56510,56512,56514],{"class":54,"line":164},[52,56491,16632],{"class":102},[52,56493,115],{"class":83},[52,56495,56082],{"class":87},[52,56497,115],{"class":83},[52,56499,199],{"class":102},[52,56501,56225],{"class":189},[52,56503,199],{"class":102},[52,56505,84],{"class":83},[52,56507,11366],{"class":87},[52,56509,115],{"class":83},[52,56511,199],{"class":102},[52,56513,56238],{"class":189},[52,56515,16657],{"class":102},[52,56517,56518,56521,56523,56525],{"class":54,"line":170},[52,56519,56520],{"class":202},"    capture_output",[52,56522,80],{"class":79},[52,56524,6233],{"class":160},[52,56526,125],{"class":102},[52,56528,56529,56532,56534],{"class":54,"line":175},[52,56530,56531],{"class":202},"    text",[52,56533,80],{"class":79},[52,56535,40188],{"class":160},[52,56537,56538],{"class":54,"line":214},[52,56539,211],{"class":102},[52,56541,56542],{"class":54,"line":233},[52,56543,70],{"emptyLinePlaceholder":69},[52,56545,56546,56548,56550,56552,56554,56556],{"class":54,"line":238},[52,56547,242],{"class":241},[52,56549,193],{"class":102},[52,56551,32811],{"class":189},[52,56553,186],{"class":102},[52,56555,56275],{"class":1320},[52,56557,211],{"class":102},[52,56559,56560,56562,56564,56566,56568,56570,56572,56574,56576,56578,56580,56582,56584,56586,56588,56590],{"class":54,"line":272},[52,56561,242],{"class":241},[52,56563,193],{"class":102},[52,56565,115],{"class":83},[52,56567,56288],{"class":87},[52,56569,115],{"class":83},[52,56571,18726],{"class":58},[52,56573,27130],{"class":189},[52,56575,186],{"class":102},[52,56577,56299],{"class":1320},[52,56579,1324],{"class":79},[52,56581,13049],{"class":121},[52,56583,56191],{"class":58},[52,56585,84],{"class":83},[52,56587,56310],{"class":87},[52,56589,115],{"class":83},[52,56591,211],{"class":102},[14,56593,49964],{},[21,56595,56596,56602,56607,56613,56621,56629,56634,56637],{},[24,56597,30765,56598,56601],{},[49,56599,56600],{},"platform"," to detect the operating system.",[24,56603,30765,56604,56606],{},[49,56605,56110],{}," to run an external command.",[24,56608,56609,56610,186],{},"Stores the target host in ",[49,56611,56612],{},"host",[24,56614,3609,56615,56617,56618,56620],{},[49,56616,56161],{}," on Windows and ",[49,56619,56196],{}," on macOS\u002FLinux.",[24,56622,56623,56624,56626,56627,186],{},"Runs ",[49,56625,56082],{}," with a count of ",[49,56628,11366],{},[24,56630,56631,56632,186],{},"Saves the result in ",[49,56633,32811],{},[24,56635,56636],{},"Prints the command output.",[24,56638,56639,56640,56643],{},"Checks ",[49,56641,56642],{},"result.returncode"," to decide whether the host is reachable.",[37,56645,56647],{"id":56646},"key-lines-to-explain","Key lines to explain",[639,56649,56651],{"id":56650},"platformsystemlower",[49,56652,56653],{},"platform.system().lower()",[42,56655,56657],{"className":44,"code":56656,"language":46,"meta":47,"style":47},"platform.system().lower()\n",[49,56658,56659],{"__ignoreMap":47},[52,56660,56661,56663,56665,56667,56669,56671],{"class":54,"line":55},[52,56662,56600],{"class":62},[52,56664,186],{"class":102},[52,56666,56173],{"class":189},[52,56668,18679],{"class":102},[52,56670,30237],{"class":189},[52,56672,230],{"class":102},[14,56674,56675],{},"This checks the current operating system and turns it into lowercase text.",[14,56677,56678],{},"Examples:",[309,56680,56681,56689,56697],{},[24,56682,56683,6230,56686],{},[49,56684,56685],{},"\"Windows\"",[49,56687,56688],{},"\"windows\"",[24,56690,56691,6230,56694],{},[49,56692,56693],{},"\"Linux\"",[49,56695,56696],{},"\"linux\"",[24,56698,56699,6230,56702,56705],{},[49,56700,56701],{},"\"Darwin\"",[49,56703,56704],{},"\"darwin\""," on macOS",[14,56707,56708,56709,56711],{},"That helps the script choose the correct ",[49,56710,56082],{}," option.",[639,56713,56715],{"id":56714},"subprocessrun",[49,56716,56717],{},"subprocess.run(...)",[42,56719,56721],{"className":44,"code":56720,"language":46,"meta":47,"style":47},"subprocess.run([\"ping\", param, \"4\", host], capture_output=True, text=True)\n",[49,56722,56723],{"__ignoreMap":47},[52,56724,56725,56727,56729,56731,56733,56735,56737,56739,56741,56743,56745,56747,56749,56751,56753,56755,56757,56759,56761,56763,56765,56767,56769,56771],{"class":54,"line":55},[52,56726,56110],{"class":62},[52,56728,186],{"class":102},[52,56730,56212],{"class":189},[52,56732,17632],{"class":102},[52,56734,115],{"class":83},[52,56736,56082],{"class":87},[52,56738,115],{"class":83},[52,56740,199],{"class":102},[52,56742,56225],{"class":189},[52,56744,199],{"class":102},[52,56746,84],{"class":83},[52,56748,11366],{"class":87},[52,56750,115],{"class":83},[52,56752,199],{"class":102},[52,56754,56238],{"class":189},[52,56756,15661],{"class":102},[52,56758,56243],{"class":202},[52,56760,80],{"class":79},[52,56762,6233],{"class":160},[52,56764,199],{"class":102},[52,56766,56252],{"class":202},[52,56768,80],{"class":79},[52,56770,6233],{"class":160},[52,56772,211],{"class":102},[14,56774,56775],{},"This runs an external command from Python.",[14,56777,56778],{},"In this case, it runs:",[309,56780,56781,56787],{},[24,56782,56783,56786],{},[49,56784,56785],{},"ping -n 4 google.com"," on Windows",[24,56788,56789,56792],{},[49,56790,56791],{},"ping -c 4 google.com"," on Linux or macOS",[14,56794,56795,56796,759,56801,56805],{},"If you are new to running system commands from Python, the ",[372,56797,56798,56800],{"href":29944},[49,56799,15848],{}," module overview",[372,56802,56803,56800],{"href":6784},[49,56804,9155],{}," are also useful background topics.",[639,56807,56809],{"id":56808},"capture_outputtrue",[49,56810,56811],{},"capture_output=True",[42,56813,56815],{"className":44,"code":56814,"language":46,"meta":47,"style":47},"capture_output=True\n",[49,56816,56817],{"__ignoreMap":47},[52,56818,56819,56822,56824],{"class":54,"line":55},[52,56820,56821],{"class":62},"capture_output",[52,56823,80],{"class":79},[52,56825,40188],{"class":160},[14,56827,56828],{},"This collects the command output so Python can use it later.",[14,56830,56831,56832,186],{},"Without this, the command may print directly to the terminal instead of being stored in ",[49,56833,56834],{},"result.stdout",[639,56836,56838],{"id":56837},"texttrue",[49,56839,56840],{},"text=True",[42,56842,56844],{"className":44,"code":56843,"language":46,"meta":47,"style":47},"text=True\n",[49,56845,56846],{"__ignoreMap":47},[52,56847,56848,56850,56852],{"class":54,"line":55},[52,56849,955],{"class":62},[52,56851,80],{"class":79},[52,56853,40188],{"class":160},[14,56855,56856],{},"This tells Python to return the output as normal text.",[14,56858,5788,56859,56861],{},[49,56860,56840],{},", the output would be returned as bytes, which is harder for beginners to read.",[639,56863,56865],{"id":56864},"resultreturncode-0",[49,56866,56867],{},"result.returncode == 0",[42,56869,56871],{"className":44,"code":56870,"language":46,"meta":47,"style":47},"result.returncode == 0\n",[49,56872,56873],{"__ignoreMap":47},[52,56874,56875,56877,56879,56881,56883],{"class":54,"line":55},[52,56876,32811],{"class":62},[52,56878,186],{"class":102},[52,56880,56299],{"class":1320},[52,56882,1324],{"class":79},[52,56884,34598],{"class":121},[14,56886,56887,56888,56890],{},"A return code of ",[49,56889,13294],{}," usually means the command succeeded.",[14,56892,56893,56894,56896],{},"For this script, that usually means the ",[49,56895,56082],{}," command was able to reach the host.",[37,56898,13563],{"id":13562},[14,56900,56901,56902,56904],{},"The output will usually show several lines from the ",[49,56903,56082],{}," command, followed by a final message.",[14,56906,4259],{},[42,56908,56911],{"className":56909,"code":56910,"language":955,"meta":47},[953],"PING google.com (142.250.190.14): 56 data bytes\n64 bytes from 142.250.190.14: icmp_seq=0 ttl=117 time=14.2 ms\n64 bytes from 142.250.190.14: icmp_seq=1 ttl=117 time=13.8 ms\n64 bytes from 142.250.190.14: icmp_seq=2 ttl=117 time=14.0 ms\n64 bytes from 142.250.190.14: icmp_seq=3 ttl=117 time=13.9 ms\n\nReachable\n",[49,56912,56910],{"__ignoreMap":47},[14,56914,56915],{},"If the host cannot be reached, you may see different output and this final line:",[42,56917,56920],{"className":56918,"code":56919,"language":955,"meta":47},[953],"Not reachable\n",[49,56921,56919],{"__ignoreMap":47},[14,56923,56924],{},"The exact format is different on:",[309,56926,56927,56930,56933],{},[24,56928,56929],{},"Windows",[24,56931,56932],{},"Linux",[24,56934,56935],{},"macOS",[14,56937,56938],{},"That is normal.",[37,56940,3190],{"id":3189},[14,56942,56943],{},"These are the most common reasons a simple ping script does not work as expected:",[309,56945,56946,56953,56956,56959,56964],{},[24,56947,56948,56949,41833,56951],{},"using the wrong flag: ",[49,56950,56161],{},[49,56952,56196],{},[24,56954,56955],{},"trying to ping a host with no internet connection",[24,56957,56958],{},"firewall or network rules blocking ping",[24,56960,6728,56961,56963],{},[49,56962,56082],{}," command not being available in the current environment",[24,56965,56966],{},"thinking ping success means the whole website or app is working",[639,56968,51157],{"id":51156},[309,56970,56971,56974,56977,56980,56983,56986],{},[24,56972,56973],{},"Wrong ping flag for the operating system",[24,56975,56976],{},"Host name is misspelled",[24,56978,56979],{},"No network connection",[24,56981,56982],{},"Ping blocked by firewall",[24,56984,56985],{},"Running in a restricted environment where ping is unavailable",[24,56987,56988],{},"Assuming return code meanings without checking system behavior",[37,56990,50659],{"id":50658},[14,56992,56993],{},"Once the basic version works, you can improve it in small steps.",[14,56995,56996],{},"For example, you can:",[309,56998,56999,57002,57005,57008,57011],{},[24,57000,57001],{},"let the user enter a hostname",[24,57003,57004],{},"ping multiple hosts in a loop",[24,57006,57007],{},"save results to a file",[24,57009,57010],{},"add a timeout",[24,57012,57013],{},"show only success or failure instead of the full output",[14,57015,57016],{},"Here is a small improvement that asks the user for a host:",[42,57018,57020],{"className":44,"code":57019,"language":46,"meta":47,"style":47},"import platform\nimport subprocess\n\nhost = input(\"Enter a host to ping: \").strip()\nparam = \"-n\" if platform.system().lower() == \"windows\" else \"-c\"\n\nresult = subprocess.run(\n    [\"ping\", param, \"4\", host],\n    capture_output=True,\n    text=True\n)\n\nprint(result.stdout)\nprint(\"Reachable\" if result.returncode == 0 else \"Not reachable\")\n",[49,57021,57022,57028,57034,57038,57061,57103,57107,57121,57149,57159,57167,57171,57175,57189],{"__ignoreMap":47},[52,57023,57024,57026],{"class":54,"line":55},[52,57025,59],{"class":58},[52,57027,56124],{"class":62},[52,57029,57030,57032],{"class":54,"line":66},[52,57031,59],{"class":58},[52,57033,56131],{"class":62},[52,57035,57036],{"class":54,"line":73},[52,57037,70],{"emptyLinePlaceholder":69},[52,57039,57040,57042,57044,57046,57048,57050,57053,57055,57057,57059],{"class":54,"line":94},[52,57041,56140],{"class":62},[52,57043,80],{"class":79},[52,57045,11037],{"class":241},[52,57047,193],{"class":102},[52,57049,115],{"class":83},[52,57051,57052],{"class":87},"Enter a host to ping: ",[52,57054,115],{"class":83},[52,57056,54653],{"class":102},[52,57058,18630],{"class":189},[52,57060,230],{"class":102},[52,57062,57063,57065,57067,57069,57071,57073,57075,57077,57079,57081,57083,57085,57087,57089,57091,57093,57095,57097,57099,57101],{"class":54,"line":106},[52,57064,56154],{"class":62},[52,57066,80],{"class":79},[52,57068,84],{"class":83},[52,57070,56161],{"class":87},[52,57072,115],{"class":83},[52,57074,18726],{"class":58},[52,57076,56168],{"class":62},[52,57078,186],{"class":102},[52,57080,56173],{"class":189},[52,57082,18679],{"class":102},[52,57084,30237],{"class":189},[52,57086,8267],{"class":102},[52,57088,1324],{"class":79},[52,57090,84],{"class":83},[52,57092,56186],{"class":87},[52,57094,115],{"class":83},[52,57096,56191],{"class":58},[52,57098,84],{"class":83},[52,57100,56196],{"class":87},[52,57102,91],{"class":83},[52,57104,57105],{"class":54,"line":128},[52,57106,70],{"emptyLinePlaceholder":69},[52,57108,57109,57111,57113,57115,57117,57119],{"class":54,"line":148},[52,57110,32778],{"class":62},[52,57112,80],{"class":79},[52,57114,56207],{"class":62},[52,57116,186],{"class":102},[52,57118,56212],{"class":189},[52,57120,6050],{"class":102},[52,57122,57123,57125,57127,57129,57131,57133,57135,57137,57139,57141,57143,57145,57147],{"class":54,"line":164},[52,57124,16632],{"class":102},[52,57126,115],{"class":83},[52,57128,56082],{"class":87},[52,57130,115],{"class":83},[52,57132,199],{"class":102},[52,57134,56225],{"class":189},[52,57136,199],{"class":102},[52,57138,84],{"class":83},[52,57140,11366],{"class":87},[52,57142,115],{"class":83},[52,57144,199],{"class":102},[52,57146,56238],{"class":189},[52,57148,16657],{"class":102},[52,57150,57151,57153,57155,57157],{"class":54,"line":170},[52,57152,56520],{"class":202},[52,57154,80],{"class":79},[52,57156,6233],{"class":160},[52,57158,125],{"class":102},[52,57160,57161,57163,57165],{"class":54,"line":175},[52,57162,56531],{"class":202},[52,57164,80],{"class":79},[52,57166,40188],{"class":160},[52,57168,57169],{"class":54,"line":214},[52,57170,211],{"class":102},[52,57172,57173],{"class":54,"line":233},[52,57174,70],{"emptyLinePlaceholder":69},[52,57176,57177,57179,57181,57183,57185,57187],{"class":54,"line":238},[52,57178,242],{"class":241},[52,57180,193],{"class":102},[52,57182,32811],{"class":189},[52,57184,186],{"class":102},[52,57186,56275],{"class":1320},[52,57188,211],{"class":102},[52,57190,57191,57193,57195,57197,57199,57201,57203,57205,57207,57209,57211,57213,57215,57217,57219,57221],{"class":54,"line":272},[52,57192,242],{"class":241},[52,57194,193],{"class":102},[52,57196,115],{"class":83},[52,57198,56288],{"class":87},[52,57200,115],{"class":83},[52,57202,18726],{"class":58},[52,57204,27130],{"class":189},[52,57206,186],{"class":102},[52,57208,56299],{"class":1320},[52,57210,1324],{"class":79},[52,57212,13049],{"class":121},[52,57214,56191],{"class":58},[52,57216,84],{"class":83},[52,57218,56310],{"class":87},[52,57220,115],{"class":83},[52,57222,211],{"class":102},[14,57224,57225,57226,57228],{},"If you want to make this more robust, learn ",[372,57227,10028],{"href":10027},". That will help when a command is missing or input is invalid.",[37,57230,57232],{"id":57231},"safety-and-portability-notes","Safety and portability notes",[14,57234,57235],{},"This example is simple on purpose, but a few good habits matter:",[309,57237,57238,57243,57250,57253],{},[24,57239,57240,57241],{},"Prefer passing a list to ",[49,57242,56717],{},[24,57244,57245,57246,57249],{},"Avoid ",[49,57247,57248],{},"shell=True"," for simple cases",[24,57251,57252],{},"Remember that command behavior depends on the operating system",[24,57254,57255],{},"Treat this as a basic example, not a full monitoring solution",[14,57257,57258],{},"Passing a list like this is safer:",[42,57260,57262],{"className":44,"code":57261,"language":46,"meta":47,"style":47},"[\"ping\", param, \"4\", host]\n",[49,57263,57264],{"__ignoreMap":47},[52,57265,57266,57268,57270,57272,57274,57276,57278,57280,57282,57284,57286,57288,57290],{"class":54,"line":55},[52,57267,250],{"class":102},[52,57269,115],{"class":83},[52,57271,56082],{"class":87},[52,57273,115],{"class":83},[52,57275,199],{"class":102},[52,57277,56225],{"class":62},[52,57279,199],{"class":102},[52,57281,84],{"class":83},[52,57283,11366],{"class":87},[52,57285,115],{"class":83},[52,57287,199],{"class":102},[52,57289,56238],{"class":62},[52,57291,1147],{"class":102},[14,57293,57294],{},"That is better than building one shell string yourself.",[37,57296,57298],{"id":57297},"debugging-commands","Debugging commands",[14,57300,57301],{},"If your script is not working, these commands can help you test the environment:",[42,57303,57305],{"className":390,"code":57304,"language":392,"meta":47,"style":47},"python ping_script.py\nping google.com\nping -c 4 google.com\nping -n 4 google.com\npython --version\n",[49,57306,57307,57314,57321,57331,57342],{"__ignoreMap":47},[52,57308,57309,57311],{"class":54,"line":55},[52,57310,46],{"class":399},[52,57312,57313],{"class":87}," ping_script.py\n",[52,57315,57316,57318],{"class":54,"line":66},[52,57317,56082],{"class":399},[52,57319,57320],{"class":87}," google.com\n",[52,57322,57323,57325,57327,57329],{"class":54,"line":73},[52,57324,56082],{"class":399},[52,57326,443],{"class":420},[52,57328,32111],{"class":121},[52,57330,57320],{"class":87},[52,57332,57333,57335,57338,57340],{"class":54,"line":94},[52,57334,56082],{"class":399},[52,57336,57337],{"class":420}," -n",[52,57339,32111],{"class":121},[52,57341,57320],{"class":87},[52,57343,57344,57346],{"class":54,"line":106},[52,57345,46],{"class":399},[52,57347,2324],{"class":420},[14,57349,57350],{},"Use them like this:",[309,57352,57353,57360,57369,57374,57378],{},[24,57354,57355,57356,57359],{},"run ",[49,57357,57358],{},"python ping_script.py"," to test your script",[24,57361,57355,57362,57365,57366,57368],{},[49,57363,57364],{},"ping google.com"," directly in the terminal to see if ",[49,57367,56082],{}," works at all",[24,57370,57371,57372,56792],{},"try ",[49,57373,56791],{},[24,57375,57371,57376,56786],{},[49,57377,56785],{},[24,57379,57355,57380,57383],{},[49,57381,57382],{},"python --version"," to confirm Python is installed",[14,57385,57386,57387,857,57391,186],{},"If Python cannot find a file or command while you test scripts, you may also run into errors like ",[372,57388,18228,57389,18232],{"href":15903},[52,57390,18231],{},[372,57392,18238,57393,18242],{"href":18237},[52,57394,18241],{},[37,57396,1942],{"id":1941},[639,57398,57400],{"id":57399},"does-python-have-a-built-in-ping-function","Does Python have a built-in ping function?",[14,57402,57403,57404,57406,57407,186],{},"Not as a simple built-in function. Beginners usually run the system ",[49,57405,56082],{}," command with ",[49,57408,56110],{},[639,57410,57412,57413,56617,57415,57417],{"id":57411},"why-does-my-script-use-n-on-windows-and-c-on-linux-or-macos","Why does my script use ",[49,57414,56161],{},[49,57416,56196],{}," on Linux or macOS?",[14,57419,358,57420,57422],{},[49,57421,56082],{}," command uses different options on different operating systems.",[309,57424,57425,57431],{},[24,57426,57427,57428,57430],{},"Windows uses ",[49,57429,56161],{}," for count",[24,57432,57433,57434],{},"Linux and macOS commonly use ",[49,57435,56196],{},[639,57437,57439],{"id":57438},"why-does-the-script-say-not-reachable-even-when-the-website-opens-in-my-browser","Why does the script say Not reachable even when the website opens in my browser?",[14,57441,57442],{},"Some servers block ping requests. A browser working does not always mean ping will work.",[639,57444,57446],{"id":57445},"can-i-ping-an-ip-address-instead-of-a-domain-name","Can I ping an IP address instead of a domain name?",[14,57448,57449],{},"Yes. You can use:",[309,57451,57452,57457],{},[24,57453,57454,57455],{},"a host name like ",[49,57456,56147],{},[24,57458,57459,57460],{},"an IP address like ",[49,57461,57462],{},"8.8.8.8",[639,57464,57466],{"id":57465},"is-this-the-best-way-to-monitor-servers","Is this the best way to monitor servers?",[14,57468,57469],{},"It is fine for a beginner example.",[14,57471,57472],{},"Real monitoring tools usually need:",[309,57474,57475,57478,57481,57484],{},[24,57476,57477],{},"retries",[24,57479,57480],{},"logging",[24,57482,57483],{},"timeouts",[24,57485,57486],{},"better error handling",[37,57488,2005],{"id":2004},[309,57490,57491,57495,57499,57503],{},[24,57492,57493],{},[372,57494,2012],{"href":374},[24,57496,57497],{},[372,57498,56387],{"href":6790},[24,57500,57501],{},[372,57502,56384],{"href":56383},[24,57504,57505],{},[372,57506,26911],{"href":10027},[14,57508,57509],{},"A good next step is to turn this basic ping script into a small command-line tool that checks user-provided hosts and handles errors clearly.",[2034,57511,57512],{},"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 .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 .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 .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 .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 .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 .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}",{"title":47,"searchDepth":66,"depth":66,"links":57514},[57515,57516,57517,57518,57519,57526,57527,57530,57531,57532,57533,57541],{"id":39,"depth":66,"text":40},{"id":28679,"depth":66,"text":28680},{"id":56359,"depth":66,"text":56360},{"id":56390,"depth":66,"text":56391},{"id":56646,"depth":66,"text":56647,"children":57520},[57521,57522,57523,57524,57525],{"id":56650,"depth":73,"text":56653},{"id":56714,"depth":73,"text":56717},{"id":56808,"depth":73,"text":56811},{"id":56837,"depth":73,"text":56840},{"id":56864,"depth":73,"text":56867},{"id":13562,"depth":66,"text":13563},{"id":3189,"depth":66,"text":3190,"children":57528},[57529],{"id":51156,"depth":73,"text":51157},{"id":50658,"depth":66,"text":50659},{"id":57231,"depth":66,"text":57232},{"id":57297,"depth":66,"text":57298},{"id":1941,"depth":66,"text":1942,"children":57534},[57535,57536,57538,57539,57540],{"id":57399,"depth":73,"text":57400},{"id":57411,"depth":73,"text":57537},"Why does my script use -n on Windows and -c on Linux or macOS?",{"id":57438,"depth":73,"text":57439},{"id":57445,"depth":73,"text":57446},{"id":57465,"depth":73,"text":57466},{"id":2004,"depth":66,"text":2005},"Master python ping script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-ping-script-example",{"title":56071,"description":57542},"examples\u002Fpython-ping-script-example","A5pVRNeqHdlu8ogcKv0LpNd9e0lre26AHvL2E_NelAM",{"id":57549,"title":57550,"body":57551,"description":59564,"extension":2075,"meta":59565,"navigation":69,"path":59566,"seo":59567,"stem":59568,"__hash__":59569},"content\u002Fexamples\u002Fpython-random-name-picker-example.md","Python Random Name Picker Example",{"type":7,"value":57552,"toc":59519},[57553,57556,57559,57564,57566,57662,57667,57669,57671,57687,57690,57694,57697,57796,57798,57801,57816,57819,57832,57834,57866,57871,57873,57876,57882,57892,57897,57902,57907,57911,57956,57959,57962,57966,57989,57995,58002,58006,58029,58032,58034,58037,58040,58064,58067,58081,58216,58218,58241,58244,58256,58260,58265,58268,58379,58382,58538,58540,58557,58560,58571,58573,58576,58580,58586,58688,58691,58695,58698,58783,58788,58792,58795,58942,58946,58949,58963,58965,58967,58971,58974,59029,59032,59042,59048,59051,59053,59094,59097,59149,59153,59156,59189,59195,59198,59225,59229,59232,59249,59255,59260,59303,59308,59311,59331,59334,59353,59355,59358,59415,59418,59435,59437,59441,59449,59455,59461,59465,59468,59472,59477,59479,59502,59505,59516],[10,57554,57550],{"id":57555},"python-random-name-picker-example",[14,57557,57558],{},"A simple Python project can help you practice lists, user input, and basic randomness.",[14,57560,57561,57562,13338],{},"In this example, you will build a script that picks one random name from a list. This page focuses on how the script works and how to build it step by step, not on covering the full ",[49,57563,36610],{},[37,57565,40],{"id":39},[42,57567,57569],{"className":44,"code":57568,"language":46,"meta":47,"style":47},"import random\n\nnames = [\"Alice\", \"Bob\", \"Carlos\", \"Dina\"]\nchosen_name = random.choice(names)\nprint(\"Selected:\", chosen_name)\n",[49,57570,57571,57577,57581,57623,57642],{"__ignoreMap":47},[52,57572,57573,57575],{"class":54,"line":55},[52,57574,59],{"class":58},[52,57576,36646],{"class":62},[52,57578,57579],{"class":54,"line":66},[52,57580,70],{"emptyLinePlaceholder":69},[52,57582,57583,57585,57587,57589,57591,57593,57595,57597,57599,57601,57603,57605,57607,57610,57612,57614,57616,57619,57621],{"class":54,"line":73},[52,57584,33090],{"class":62},[52,57586,80],{"class":79},[52,57588,18162],{"class":102},[52,57590,115],{"class":83},[52,57592,2156],{"class":87},[52,57594,115],{"class":83},[52,57596,199],{"class":102},[52,57598,84],{"class":83},[52,57600,10858],{"class":87},[52,57602,115],{"class":83},[52,57604,199],{"class":102},[52,57606,84],{"class":83},[52,57608,57609],{"class":87},"Carlos",[52,57611,115],{"class":83},[52,57613,199],{"class":102},[52,57615,84],{"class":83},[52,57617,57618],{"class":87},"Dina",[52,57620,115],{"class":83},[52,57622,1147],{"class":102},[52,57624,57625,57628,57630,57632,57634,57636,57638,57640],{"class":54,"line":94},[52,57626,57627],{"class":62},"chosen_name ",[52,57629,80],{"class":79},[52,57631,36659],{"class":62},[52,57633,186],{"class":102},[52,57635,12230],{"class":189},[52,57637,193],{"class":102},[52,57639,5327],{"class":189},[52,57641,211],{"class":102},[52,57643,57644,57646,57648,57650,57653,57655,57657,57660],{"class":54,"line":106},[52,57645,242],{"class":241},[52,57647,193],{"class":102},[52,57649,115],{"class":83},[52,57651,57652],{"class":87},"Selected:",[52,57654,115],{"class":83},[52,57656,199],{"class":102},[52,57658,57659],{"class":189}," chosen_name",[52,57661,211],{"class":102},[14,57663,3650,57664,57666],{},[49,57665,53989],{}," to return one item from a list. The list must not be empty.",[37,57668,323],{"id":322},[14,57670,30376],{},[309,57672,57673,57676,57681,57684],{},[24,57674,57675],{},"Creates a list of names",[24,57677,57678,57679,14928],{},"Uses the ",[49,57680,36610],{},[24,57682,57683],{},"Selects one name at random",[24,57685,57686],{},"Prints the result",[14,57688,57689],{},"This is a useful beginner project because it combines a few core Python ideas in one short program.",[37,57691,57693],{"id":57692},"basic-random-name-picker-script","Basic random name picker script",[14,57695,57696],{},"Start with a fixed list of names.",[42,57698,57700],{"className":44,"code":57699,"language":46,"meta":47,"style":47},"import random\n\nnames = [\"Alice\", \"Bob\", \"Carlos\", \"Dina\"]\n\nchosen_name = random.choice(names)\n\nprint(\"Selected:\", chosen_name)\n",[49,57701,57702,57708,57712,57752,57756,57774,57778],{"__ignoreMap":47},[52,57703,57704,57706],{"class":54,"line":55},[52,57705,59],{"class":58},[52,57707,36646],{"class":62},[52,57709,57710],{"class":54,"line":66},[52,57711,70],{"emptyLinePlaceholder":69},[52,57713,57714,57716,57718,57720,57722,57724,57726,57728,57730,57732,57734,57736,57738,57740,57742,57744,57746,57748,57750],{"class":54,"line":73},[52,57715,33090],{"class":62},[52,57717,80],{"class":79},[52,57719,18162],{"class":102},[52,57721,115],{"class":83},[52,57723,2156],{"class":87},[52,57725,115],{"class":83},[52,57727,199],{"class":102},[52,57729,84],{"class":83},[52,57731,10858],{"class":87},[52,57733,115],{"class":83},[52,57735,199],{"class":102},[52,57737,84],{"class":83},[52,57739,57609],{"class":87},[52,57741,115],{"class":83},[52,57743,199],{"class":102},[52,57745,84],{"class":83},[52,57747,57618],{"class":87},[52,57749,115],{"class":83},[52,57751,1147],{"class":102},[52,57753,57754],{"class":54,"line":94},[52,57755,70],{"emptyLinePlaceholder":69},[52,57757,57758,57760,57762,57764,57766,57768,57770,57772],{"class":54,"line":106},[52,57759,57627],{"class":62},[52,57761,80],{"class":79},[52,57763,36659],{"class":62},[52,57765,186],{"class":102},[52,57767,12230],{"class":189},[52,57769,193],{"class":102},[52,57771,5327],{"class":189},[52,57773,211],{"class":102},[52,57775,57776],{"class":54,"line":128},[52,57777,70],{"emptyLinePlaceholder":69},[52,57779,57780,57782,57784,57786,57788,57790,57792,57794],{"class":54,"line":148},[52,57781,242],{"class":241},[52,57783,193],{"class":102},[52,57785,115],{"class":83},[52,57787,57652],{"class":87},[52,57789,115],{"class":83},[52,57791,199],{"class":102},[52,57793,57659],{"class":189},[52,57795,211],{"class":102},[639,57797,13563],{"id":13562},[14,57799,57800],{},"Your output will change each time you run the program. For example:",[42,57802,57804],{"className":44,"code":57803,"language":46,"meta":47,"style":47},"Selected: Carlos\n",[49,57805,57806],{"__ignoreMap":47},[52,57807,57808,57811,57813],{"class":54,"line":55},[52,57809,57810],{"class":62},"Selected",[52,57812,118],{"class":102},[52,57814,57815],{"class":62}," Carlos\n",[14,57817,57818],{},"or:",[42,57820,57822],{"className":44,"code":57821,"language":46,"meta":47,"style":47},"Selected: Alice\n",[49,57823,57824],{"__ignoreMap":47},[52,57825,57826,57828,57830],{"class":54,"line":55},[52,57827,57810],{"class":62},[52,57829,118],{"class":102},[52,57831,5874],{"class":62},[639,57833,32449],{"id":32448},[309,57835,57836,57844,57849,57855,57861],{},[24,57837,57838,57840,57841,57843],{},[49,57839,36739],{}," makes the ",[49,57842,36610],{}," module available",[24,57845,57846,57848],{},[49,57847,5327],{}," is a list containing several strings",[24,57850,57851,57854],{},[49,57852,57853],{},"random.choice(names)"," picks one item from that list",[24,57856,57857,57858],{},"The result is stored in ",[49,57859,57860],{},"chosen_name",[24,57862,57863,57865],{},[49,57864,9687],{}," shows the selected name",[14,57867,44579,57868,186],{},[372,57869,44583],{"href":57870},"\u002Flearn\u002Fpython-lists-explained-beginner-guide\u002F",[37,57872,454],{"id":453},[14,57874,57875],{},"Let's break the script into small parts.",[639,57877,57879,57880,14928],{"id":57878},"_1-import-the-random-module","1. Import the ",[49,57881,36610],{},[42,57883,57884],{"className":44,"code":51816,"language":46,"meta":47,"style":47},[49,57885,57886],{"__ignoreMap":47},[52,57887,57888,57890],{"class":54,"line":55},[52,57889,59],{"class":58},[52,57891,36646],{"class":62},[14,57893,57894,57896],{},[49,57895,36610],{}," is a built-in Python module used for random operations.",[14,57898,57899,57900,186],{},"You need to import it before calling ",[49,57901,53989],{},[14,57903,57904,57905,186],{},"If you want a broader explanation later, see the ",[372,57906,36730],{"href":36729},[639,57908,57910],{"id":57909},"_2-create-a-list-of-names","2. Create a list of names",[42,57912,57914],{"className":44,"code":57913,"language":46,"meta":47,"style":47},"names = [\"Alice\", \"Bob\", \"Carlos\", \"Dina\"]\n",[49,57915,57916],{"__ignoreMap":47},[52,57917,57918,57920,57922,57924,57926,57928,57930,57932,57934,57936,57938,57940,57942,57944,57946,57948,57950,57952,57954],{"class":54,"line":55},[52,57919,33090],{"class":62},[52,57921,80],{"class":79},[52,57923,18162],{"class":102},[52,57925,115],{"class":83},[52,57927,2156],{"class":87},[52,57929,115],{"class":83},[52,57931,199],{"class":102},[52,57933,84],{"class":83},[52,57935,10858],{"class":87},[52,57937,115],{"class":83},[52,57939,199],{"class":102},[52,57941,84],{"class":83},[52,57943,57609],{"class":87},[52,57945,115],{"class":83},[52,57947,199],{"class":102},[52,57949,84],{"class":83},[52,57951,57618],{"class":87},[52,57953,115],{"class":83},[52,57955,1147],{"class":102},[14,57957,57958],{},"A list lets you store multiple values in one variable.",[14,57960,57961],{},"Here, each item in the list is a name stored as a string.",[639,57963,57965],{"id":57964},"_3-choose-one-random-item","3. Choose one random item",[42,57967,57969],{"className":44,"code":57968,"language":46,"meta":47,"style":47},"chosen_name = random.choice(names)\n",[49,57970,57971],{"__ignoreMap":47},[52,57972,57973,57975,57977,57979,57981,57983,57985,57987],{"class":54,"line":55},[52,57974,57627],{"class":62},[52,57976,80],{"class":79},[52,57978,36659],{"class":62},[52,57980,186],{"class":102},[52,57982,12230],{"class":189},[52,57984,193],{"class":102},[52,57986,5327],{"class":189},[52,57988,211],{"class":102},[14,57990,57991,57994],{},[49,57992,57993],{},"random.choice(list_name)"," returns one random item from the list.",[14,57996,57997,57998,186],{},"If you want to understand this function more clearly, see ",[372,57999,58001],{"href":58000},"\u002Fstandard-library\u002Frandom.choice-function-explained","random.choice() explained",[639,58003,58005],{"id":58004},"_4-print-the-result","4. Print the result",[42,58007,58009],{"className":44,"code":58008,"language":46,"meta":47,"style":47},"print(\"Selected:\", chosen_name)\n",[49,58010,58011],{"__ignoreMap":47},[52,58012,58013,58015,58017,58019,58021,58023,58025,58027],{"class":54,"line":55},[52,58014,242],{"class":241},[52,58016,193],{"class":102},[52,58018,115],{"class":83},[52,58020,57652],{"class":87},[52,58022,115],{"class":83},[52,58024,199],{"class":102},[52,58026,57659],{"class":189},[52,58028,211],{"class":102},[14,58030,58031],{},"This displays the randomly selected name.",[37,58033,54304],{"id":54303},[14,58035,58036],{},"Instead of hard-coding the names, you can let the user type them.",[14,58038,58039],{},"The user will enter names separated by commas, such as:",[42,58041,58043],{"className":44,"code":58042,"language":46,"meta":47,"style":47},"Alice, Bob, Carlos, Dina\n",[49,58044,58045],{"__ignoreMap":47},[52,58046,58047,58049,58051,58054,58056,58059,58061],{"class":54,"line":55},[52,58048,2156],{"class":62},[52,58050,199],{"class":102},[52,58052,58053],{"class":62}," Bob",[52,58055,199],{"class":102},[52,58057,58058],{"class":62}," Carlos",[52,58060,199],{"class":102},[52,58062,58063],{"class":62}," Dina\n",[14,58065,58066],{},"Then the program will:",[309,58068,58069,58072,58075,58078],{},[24,58070,58071],{},"Read the text",[24,58073,58074],{},"Split it into a list",[24,58076,58077],{},"Remove extra spaces",[24,58079,58080],{},"Pick one random name",[42,58082,58084],{"className":44,"code":58083,"language":46,"meta":47,"style":47},"import random\n\nuser_input = input(\"Enter names separated by commas: \")\n\nnames = user_input.split(\",\")\ncleaned_names = [name.strip() for name in names]\n\nchosen_name = random.choice(cleaned_names)\n\nprint(\"Selected:\", chosen_name)\n",[49,58085,58086,58092,58096,58116,58120,58144,58171,58175,58194,58198],{"__ignoreMap":47},[52,58087,58088,58090],{"class":54,"line":55},[52,58089,59],{"class":58},[52,58091,36646],{"class":62},[52,58093,58094],{"class":54,"line":66},[52,58095,70],{"emptyLinePlaceholder":69},[52,58097,58098,58101,58103,58105,58107,58109,58112,58114],{"class":54,"line":73},[52,58099,58100],{"class":62},"user_input ",[52,58102,80],{"class":79},[52,58104,11037],{"class":241},[52,58106,193],{"class":102},[52,58108,115],{"class":83},[52,58110,58111],{"class":87},"Enter names separated by commas: ",[52,58113,115],{"class":83},[52,58115,211],{"class":102},[52,58117,58118],{"class":54,"line":94},[52,58119,70],{"emptyLinePlaceholder":69},[52,58121,58122,58124,58126,58129,58131,58134,58136,58138,58140,58142],{"class":54,"line":106},[52,58123,33090],{"class":62},[52,58125,80],{"class":79},[52,58127,58128],{"class":62}," user_input",[52,58130,186],{"class":102},[52,58132,58133],{"class":189},"split",[52,58135,193],{"class":102},[52,58137,115],{"class":83},[52,58139,199],{"class":87},[52,58141,115],{"class":83},[52,58143,211],{"class":102},[52,58145,58146,58149,58151,58153,58155,58157,58159,58161,58163,58165,58167,58169],{"class":54,"line":128},[52,58147,58148],{"class":62},"cleaned_names ",[52,58150,80],{"class":79},[52,58152,18162],{"class":102},[52,58154,2147],{"class":62},[52,58156,186],{"class":102},[52,58158,18630],{"class":189},[52,58160,8267],{"class":102},[52,58162,37077],{"class":58},[52,58164,11292],{"class":62},[52,58166,11213],{"class":58},[52,58168,33415],{"class":62},[52,58170,1147],{"class":102},[52,58172,58173],{"class":54,"line":148},[52,58174,70],{"emptyLinePlaceholder":69},[52,58176,58177,58179,58181,58183,58185,58187,58189,58192],{"class":54,"line":164},[52,58178,57627],{"class":62},[52,58180,80],{"class":79},[52,58182,36659],{"class":62},[52,58184,186],{"class":102},[52,58186,12230],{"class":189},[52,58188,193],{"class":102},[52,58190,58191],{"class":189},"cleaned_names",[52,58193,211],{"class":102},[52,58195,58196],{"class":54,"line":170},[52,58197,70],{"emptyLinePlaceholder":69},[52,58199,58200,58202,58204,58206,58208,58210,58212,58214],{"class":54,"line":175},[52,58201,242],{"class":241},[52,58203,193],{"class":102},[52,58205,115],{"class":83},[52,58207,57652],{"class":87},[52,58209,115],{"class":83},[52,58211,199],{"class":102},[52,58213,57659],{"class":189},[52,58215,211],{"class":102},[639,58217,9910],{"id":9909},[309,58219,58220,58225,58231,58236],{},[24,58221,58222,58224],{},[49,58223,10597],{}," reads text from the user",[24,58226,58227,58230],{},[49,58228,58229],{},"split(\",\")"," turns one string into a list using commas",[24,58232,58233,58235],{},[49,58234,18831],{}," removes extra spaces around each name",[24,58237,58238,58240],{},[49,58239,53989],{}," picks one cleaned name",[14,58242,58243],{},"If you need help with these parts, see:",[309,58245,58246,58250],{},[24,58247,58248],{},[372,58249,6876],{"href":6397},[24,58251,58252],{},[372,58253,58255],{"href":58254},"\u002Fhow-to\u002Fhow-to-split-a-string-in-python\u002F","How to split a string in Python",[37,58257,58259],{"id":58258},"handle-empty-input-safely","Handle empty input safely",[14,58261,58262,58263,186],{},"An empty list will cause an error with ",[49,58264,53989],{},[14,58266,58267],{},"For example, this is unsafe if the user enters nothing useful:",[42,58269,58271],{"className":44,"code":58270,"language":46,"meta":47,"style":47},"import random\n\nuser_input = input(\"Enter names separated by commas: \")\nnames = [name.strip() for name in user_input.split(\",\")]\n\nchosen_name = random.choice(names)\nprint(\"Selected:\", chosen_name)\n",[49,58272,58273,58279,58283,58301,58339,58343,58361],{"__ignoreMap":47},[52,58274,58275,58277],{"class":54,"line":55},[52,58276,59],{"class":58},[52,58278,36646],{"class":62},[52,58280,58281],{"class":54,"line":66},[52,58282,70],{"emptyLinePlaceholder":69},[52,58284,58285,58287,58289,58291,58293,58295,58297,58299],{"class":54,"line":73},[52,58286,58100],{"class":62},[52,58288,80],{"class":79},[52,58290,11037],{"class":241},[52,58292,193],{"class":102},[52,58294,115],{"class":83},[52,58296,58111],{"class":87},[52,58298,115],{"class":83},[52,58300,211],{"class":102},[52,58302,58303,58305,58307,58309,58311,58313,58315,58317,58319,58321,58323,58325,58327,58329,58331,58333,58335,58337],{"class":54,"line":94},[52,58304,33090],{"class":62},[52,58306,80],{"class":79},[52,58308,18162],{"class":102},[52,58310,2147],{"class":62},[52,58312,186],{"class":102},[52,58314,18630],{"class":189},[52,58316,8267],{"class":102},[52,58318,37077],{"class":58},[52,58320,11292],{"class":62},[52,58322,11213],{"class":58},[52,58324,58128],{"class":62},[52,58326,186],{"class":102},[52,58328,58133],{"class":189},[52,58330,193],{"class":102},[52,58332,115],{"class":83},[52,58334,199],{"class":87},[52,58336,115],{"class":83},[52,58338,33167],{"class":102},[52,58340,58341],{"class":54,"line":106},[52,58342,70],{"emptyLinePlaceholder":69},[52,58344,58345,58347,58349,58351,58353,58355,58357,58359],{"class":54,"line":128},[52,58346,57627],{"class":62},[52,58348,80],{"class":79},[52,58350,36659],{"class":62},[52,58352,186],{"class":102},[52,58354,12230],{"class":189},[52,58356,193],{"class":102},[52,58358,5327],{"class":189},[52,58360,211],{"class":102},[52,58362,58363,58365,58367,58369,58371,58373,58375,58377],{"class":54,"line":148},[52,58364,242],{"class":241},[52,58366,193],{"class":102},[52,58368,115],{"class":83},[52,58370,57652],{"class":87},[52,58372,115],{"class":83},[52,58374,199],{"class":102},[52,58376,57659],{"class":189},[52,58378,211],{"class":102},[14,58380,58381],{},"A safer version checks that the list actually contains names.",[42,58383,58385],{"className":44,"code":58384,"language":46,"meta":47,"style":47},"import random\n\nuser_input = input(\"Enter names separated by commas: \")\n\nnames = [name.strip() for name in user_input.split(\",\") if name.strip()]\n\nif names:\n    chosen_name = random.choice(names)\n    print(\"Selected:\", chosen_name)\nelse:\n    print(\"No names were provided.\")\n",[49,58386,58387,58393,58397,58415,58419,58468,58472,58480,58499,58517,58523],{"__ignoreMap":47},[52,58388,58389,58391],{"class":54,"line":55},[52,58390,59],{"class":58},[52,58392,36646],{"class":62},[52,58394,58395],{"class":54,"line":66},[52,58396,70],{"emptyLinePlaceholder":69},[52,58398,58399,58401,58403,58405,58407,58409,58411,58413],{"class":54,"line":73},[52,58400,58100],{"class":62},[52,58402,80],{"class":79},[52,58404,11037],{"class":241},[52,58406,193],{"class":102},[52,58408,115],{"class":83},[52,58410,58111],{"class":87},[52,58412,115],{"class":83},[52,58414,211],{"class":102},[52,58416,58417],{"class":54,"line":94},[52,58418,70],{"emptyLinePlaceholder":69},[52,58420,58421,58423,58425,58427,58429,58431,58433,58435,58437,58439,58441,58443,58445,58447,58449,58451,58453,58455,58457,58459,58461,58463,58465],{"class":54,"line":106},[52,58422,33090],{"class":62},[52,58424,80],{"class":79},[52,58426,18162],{"class":102},[52,58428,2147],{"class":62},[52,58430,186],{"class":102},[52,58432,18630],{"class":189},[52,58434,8267],{"class":102},[52,58436,37077],{"class":58},[52,58438,11292],{"class":62},[52,58440,11213],{"class":58},[52,58442,58128],{"class":62},[52,58444,186],{"class":102},[52,58446,58133],{"class":189},[52,58448,193],{"class":102},[52,58450,115],{"class":83},[52,58452,199],{"class":87},[52,58454,115],{"class":83},[52,58456,1521],{"class":102},[52,58458,18726],{"class":58},[52,58460,7019],{"class":62},[52,58462,186],{"class":102},[52,58464,18630],{"class":189},[52,58466,58467],{"class":102},"()]\n",[52,58469,58470],{"class":54,"line":128},[52,58471,70],{"emptyLinePlaceholder":69},[52,58473,58474,58476,58478],{"class":54,"line":148},[52,58475,1313],{"class":58},[52,58477,33415],{"class":62},[52,58479,1330],{"class":102},[52,58481,58482,58485,58487,58489,58491,58493,58495,58497],{"class":54,"line":164},[52,58483,58484],{"class":62},"    chosen_name ",[52,58486,80],{"class":79},[52,58488,36659],{"class":62},[52,58490,186],{"class":102},[52,58492,12230],{"class":189},[52,58494,193],{"class":102},[52,58496,5327],{"class":189},[52,58498,211],{"class":102},[52,58500,58501,58503,58505,58507,58509,58511,58513,58515],{"class":54,"line":170},[52,58502,1599],{"class":241},[52,58504,193],{"class":102},[52,58506,115],{"class":83},[52,58508,57652],{"class":87},[52,58510,115],{"class":83},[52,58512,199],{"class":102},[52,58514,57659],{"class":189},[52,58516,211],{"class":102},[52,58518,58519,58521],{"class":54,"line":175},[52,58520,1591],{"class":58},[52,58522,1330],{"class":102},[52,58524,58525,58527,58529,58531,58534,58536],{"class":54,"line":214},[52,58526,1599],{"class":241},[52,58528,193],{"class":102},[52,58530,115],{"class":83},[52,58532,58533],{"class":87},"No names were provided.",[52,58535,115],{"class":83},[52,58537,211],{"class":102},[639,58539,4755],{"id":4754},[309,58541,58542,58548,58554],{},[24,58543,58544,58547],{},[49,58545,58546],{},"if name.strip()"," removes blank entries",[24,58549,58550,58553],{},[49,58551,58552],{},"if names:"," checks that the list is not empty",[24,58555,58556],{},"The program prints a helpful message instead of failing",[14,58558,58559],{},"This is especially useful when the user enters:",[309,58561,58562,58565,58568],{},[24,58563,58564],{},"an empty string",[24,58566,58567],{},"only spaces",[24,58569,58570],{},"commas with no real names",[37,58572,47795],{"id":47794},[14,58574,58575],{},"Once the basic version works, try small changes.",[639,58577,58579],{"id":58578},"pick-more-than-one-name","Pick more than one name",[14,58581,58582,58583,186],{},"If you want multiple different names without duplicates, use ",[49,58584,58585],{},"random.sample()",[42,58587,58589],{"className":44,"code":58588,"language":46,"meta":47,"style":47},"import random\n\nnames = [\"Alice\", \"Bob\", \"Carlos\", \"Dina\"]\nchosen_names = random.sample(names, 2)\n\nprint(\"Selected:\", chosen_names)\n",[49,58590,58591,58597,58601,58641,58665,58669],{"__ignoreMap":47},[52,58592,58593,58595],{"class":54,"line":55},[52,58594,59],{"class":58},[52,58596,36646],{"class":62},[52,58598,58599],{"class":54,"line":66},[52,58600,70],{"emptyLinePlaceholder":69},[52,58602,58603,58605,58607,58609,58611,58613,58615,58617,58619,58621,58623,58625,58627,58629,58631,58633,58635,58637,58639],{"class":54,"line":73},[52,58604,33090],{"class":62},[52,58606,80],{"class":79},[52,58608,18162],{"class":102},[52,58610,115],{"class":83},[52,58612,2156],{"class":87},[52,58614,115],{"class":83},[52,58616,199],{"class":102},[52,58618,84],{"class":83},[52,58620,10858],{"class":87},[52,58622,115],{"class":83},[52,58624,199],{"class":102},[52,58626,84],{"class":83},[52,58628,57609],{"class":87},[52,58630,115],{"class":83},[52,58632,199],{"class":102},[52,58634,84],{"class":83},[52,58636,57618],{"class":87},[52,58638,115],{"class":83},[52,58640,1147],{"class":102},[52,58642,58643,58646,58648,58650,58652,58655,58657,58659,58661,58663],{"class":54,"line":94},[52,58644,58645],{"class":62},"chosen_names ",[52,58647,80],{"class":79},[52,58649,36659],{"class":62},[52,58651,186],{"class":102},[52,58653,58654],{"class":189},"sample",[52,58656,193],{"class":102},[52,58658,5327],{"class":189},[52,58660,199],{"class":102},[52,58662,9168],{"class":121},[52,58664,211],{"class":102},[52,58666,58667],{"class":54,"line":106},[52,58668,70],{"emptyLinePlaceholder":69},[52,58670,58671,58673,58675,58677,58679,58681,58683,58686],{"class":54,"line":128},[52,58672,242],{"class":241},[52,58674,193],{"class":102},[52,58676,115],{"class":83},[52,58678,57652],{"class":87},[52,58680,115],{"class":83},[52,58682,199],{"class":102},[52,58684,58685],{"class":189}," chosen_names",[52,58687,211],{"class":102},[14,58689,58690],{},"This picks 2 different names from the list.",[639,58692,58694],{"id":58693},"prevent-duplicate-blank-entries","Prevent duplicate blank entries",[14,58696,58697],{},"If the user types messy input, you can clean it more carefully:",[42,58699,58701],{"className":44,"code":58700,"language":46,"meta":47,"style":47},"user_input = input(\"Enter names separated by commas: \")\nnames = [name.strip() for name in user_input.split(\",\") if name.strip()]\n\nprint(names)\n",[49,58702,58703,58721,58769,58773],{"__ignoreMap":47},[52,58704,58705,58707,58709,58711,58713,58715,58717,58719],{"class":54,"line":55},[52,58706,58100],{"class":62},[52,58708,80],{"class":79},[52,58710,11037],{"class":241},[52,58712,193],{"class":102},[52,58714,115],{"class":83},[52,58716,58111],{"class":87},[52,58718,115],{"class":83},[52,58720,211],{"class":102},[52,58722,58723,58725,58727,58729,58731,58733,58735,58737,58739,58741,58743,58745,58747,58749,58751,58753,58755,58757,58759,58761,58763,58765,58767],{"class":54,"line":66},[52,58724,33090],{"class":62},[52,58726,80],{"class":79},[52,58728,18162],{"class":102},[52,58730,2147],{"class":62},[52,58732,186],{"class":102},[52,58734,18630],{"class":189},[52,58736,8267],{"class":102},[52,58738,37077],{"class":58},[52,58740,11292],{"class":62},[52,58742,11213],{"class":58},[52,58744,58128],{"class":62},[52,58746,186],{"class":102},[52,58748,58133],{"class":189},[52,58750,193],{"class":102},[52,58752,115],{"class":83},[52,58754,199],{"class":87},[52,58756,115],{"class":83},[52,58758,1521],{"class":102},[52,58760,18726],{"class":58},[52,58762,7019],{"class":62},[52,58764,186],{"class":102},[52,58766,18630],{"class":189},[52,58768,58467],{"class":102},[52,58770,58771],{"class":54,"line":73},[52,58772,70],{"emptyLinePlaceholder":69},[52,58774,58775,58777,58779,58781],{"class":54,"line":94},[52,58776,242],{"class":241},[52,58778,193],{"class":102},[52,58780,5327],{"class":189},[52,58782,211],{"class":102},[14,58784,58785,58786,186],{},"This removes empty items like ",[49,58787,16771],{},[639,58789,58791],{"id":58790},"repeat-the-selection-in-a-loop","Repeat the selection in a loop",[14,58793,58794],{},"You can let the program choose again and again:",[42,58796,58798],{"className":44,"code":58797,"language":46,"meta":47,"style":47},"import random\n\nnames = [\"Alice\", \"Bob\", \"Carlos\", \"Dina\"]\n\nwhile True:\n    print(\"Selected:\", random.choice(names))\n    \n    again = input(\"Pick another name? (yes\u002Fno): \").strip().lower()\n    if again != \"yes\":\n        break\n",[49,58799,58800,58806,58810,58850,58854,58862,58888,58893,58921,58938],{"__ignoreMap":47},[52,58801,58802,58804],{"class":54,"line":55},[52,58803,59],{"class":58},[52,58805,36646],{"class":62},[52,58807,58808],{"class":54,"line":66},[52,58809,70],{"emptyLinePlaceholder":69},[52,58811,58812,58814,58816,58818,58820,58822,58824,58826,58828,58830,58832,58834,58836,58838,58840,58842,58844,58846,58848],{"class":54,"line":73},[52,58813,33090],{"class":62},[52,58815,80],{"class":79},[52,58817,18162],{"class":102},[52,58819,115],{"class":83},[52,58821,2156],{"class":87},[52,58823,115],{"class":83},[52,58825,199],{"class":102},[52,58827,84],{"class":83},[52,58829,10858],{"class":87},[52,58831,115],{"class":83},[52,58833,199],{"class":102},[52,58835,84],{"class":83},[52,58837,57609],{"class":87},[52,58839,115],{"class":83},[52,58841,199],{"class":102},[52,58843,84],{"class":83},[52,58845,57618],{"class":87},[52,58847,115],{"class":83},[52,58849,1147],{"class":102},[52,58851,58852],{"class":54,"line":94},[52,58853,70],{"emptyLinePlaceholder":69},[52,58855,58856,58858,58860],{"class":54,"line":106},[52,58857,10772],{"class":58},[52,58859,10943],{"class":160},[52,58861,1330],{"class":102},[52,58863,58864,58866,58868,58870,58872,58874,58876,58878,58880,58882,58884,58886],{"class":54,"line":128},[52,58865,1599],{"class":241},[52,58867,193],{"class":102},[52,58869,115],{"class":83},[52,58871,57652],{"class":87},[52,58873,115],{"class":83},[52,58875,199],{"class":102},[52,58877,36659],{"class":189},[52,58879,186],{"class":102},[52,58881,12230],{"class":189},[52,58883,193],{"class":102},[52,58885,5327],{"class":189},[52,58887,8886],{"class":102},[52,58889,58890],{"class":54,"line":148},[52,58891,58892],{"class":62},"    \n",[52,58894,58895,58898,58900,58902,58904,58906,58909,58911,58913,58915,58917,58919],{"class":54,"line":164},[52,58896,58897],{"class":62},"    again ",[52,58899,80],{"class":79},[52,58901,11037],{"class":241},[52,58903,193],{"class":102},[52,58905,115],{"class":83},[52,58907,58908],{"class":87},"Pick another name? (yes\u002Fno): ",[52,58910,115],{"class":83},[52,58912,54653],{"class":102},[52,58914,18630],{"class":189},[52,58916,18679],{"class":102},[52,58918,30237],{"class":189},[52,58920,230],{"class":102},[52,58922,58923,58925,58928,58930,58932,58934,58936],{"class":54,"line":170},[52,58924,1438],{"class":58},[52,58926,58927],{"class":62}," again ",[52,58929,33286],{"class":79},[52,58931,84],{"class":83},[52,58933,54700],{"class":87},[52,58935,115],{"class":83},[52,58937,1330],{"class":102},[52,58939,58940],{"class":54,"line":175},[52,58941,11499],{"class":58},[639,58943,58945],{"id":58944},"use-it-in-real-situations","Use it in real situations",[14,58947,58948],{},"This small script can be used for:",[309,58950,58951,58954,58957,58960],{},[24,58952,58953],{},"classroom activities",[24,58955,58956],{},"choosing a team member",[24,58958,58959],{},"picking a winner from a short list",[24,58961,58962],{},"simple games and practice projects",[37,58964,12322],{"id":12321},[14,58966,17965],{},[639,58968,14067,58969],{"id":37421},[49,58970,36610],{},[14,58972,58973],{},"This will fail:",[42,58975,58977],{"className":44,"code":58976,"language":46,"meta":47,"style":47},"names = [\"Alice\", \"Bob\", \"Carlos\"]\nprint(random.choice(names))\n",[49,58978,58979,59011],{"__ignoreMap":47},[52,58980,58981,58983,58985,58987,58989,58991,58993,58995,58997,58999,59001,59003,59005,59007,59009],{"class":54,"line":55},[52,58982,33090],{"class":62},[52,58984,80],{"class":79},[52,58986,18162],{"class":102},[52,58988,115],{"class":83},[52,58990,2156],{"class":87},[52,58992,115],{"class":83},[52,58994,199],{"class":102},[52,58996,84],{"class":83},[52,58998,10858],{"class":87},[52,59000,115],{"class":83},[52,59002,199],{"class":102},[52,59004,84],{"class":83},[52,59006,57609],{"class":87},[52,59008,115],{"class":83},[52,59010,1147],{"class":102},[52,59012,59013,59015,59017,59019,59021,59023,59025,59027],{"class":54,"line":66},[52,59014,242],{"class":241},[52,59016,193],{"class":102},[52,59018,36610],{"class":189},[52,59020,186],{"class":102},[52,59022,12230],{"class":189},[52,59024,193],{"class":102},[52,59026,5327],{"class":189},[52,59028,8886],{"class":102},[14,59030,59031],{},"Fix it by adding:",[42,59033,59034],{"className":44,"code":51816,"language":46,"meta":47,"style":47},[49,59035,59036],{"__ignoreMap":47},[52,59037,59038,59040],{"class":54,"line":55},[52,59039,59],{"class":58},[52,59041,36646],{"class":62},[639,59043,10294,59045,59047],{"id":59044},"using-randomchoice-on-an-empty-list",[49,59046,53989],{}," on an empty list",[14,59049,59050],{},"This causes an error because there is nothing to choose.",[14,59052,10306],{},[42,59054,59056],{"className":44,"code":59055,"language":46,"meta":47,"style":47},"import random\n\nnames = []\nprint(random.choice(names))\n",[49,59057,59058,59064,59068,59076],{"__ignoreMap":47},[52,59059,59060,59062],{"class":54,"line":55},[52,59061,59],{"class":58},[52,59063,36646],{"class":62},[52,59065,59066],{"class":54,"line":66},[52,59067,70],{"emptyLinePlaceholder":69},[52,59069,59070,59072,59074],{"class":54,"line":73},[52,59071,33090],{"class":62},[52,59073,80],{"class":79},[52,59075,18590],{"class":102},[52,59077,59078,59080,59082,59084,59086,59088,59090,59092],{"class":54,"line":94},[52,59079,242],{"class":241},[52,59081,193],{"class":102},[52,59083,36610],{"class":189},[52,59085,186],{"class":102},[52,59087,12230],{"class":189},[52,59089,193],{"class":102},[52,59091,5327],{"class":189},[52,59093,8886],{"class":102},[14,59095,59096],{},"Fix it by checking the list first:",[42,59098,59100],{"className":44,"code":59099,"language":46,"meta":47,"style":47},"if names:\n    print(random.choice(names))\nelse:\n    print(\"The list is empty.\")\n",[49,59101,59102,59110,59128,59134],{"__ignoreMap":47},[52,59103,59104,59106,59108],{"class":54,"line":55},[52,59105,1313],{"class":58},[52,59107,33415],{"class":62},[52,59109,1330],{"class":102},[52,59111,59112,59114,59116,59118,59120,59122,59124,59126],{"class":54,"line":66},[52,59113,1599],{"class":241},[52,59115,193],{"class":102},[52,59117,36610],{"class":189},[52,59119,186],{"class":102},[52,59121,12230],{"class":189},[52,59123,193],{"class":102},[52,59125,5327],{"class":189},[52,59127,8886],{"class":102},[52,59129,59130,59132],{"class":54,"line":73},[52,59131,1591],{"class":58},[52,59133,1330],{"class":102},[52,59135,59136,59138,59140,59142,59145,59147],{"class":54,"line":94},[52,59137,1599],{"class":241},[52,59139,193],{"class":102},[52,59141,115],{"class":83},[52,59143,59144],{"class":87},"The list is empty.",[52,59146,115],{"class":83},[52,59148,211],{"class":102},[639,59150,59152],{"id":59151},"reading-input-but-not-splitting-it","Reading input but not splitting it",[14,59154,59155],{},"This is wrong if you expect multiple names:",[42,59157,59159],{"className":44,"code":59158,"language":46,"meta":47,"style":47},"user_input = input(\"Enter names: \")\nnames = user_input\n",[49,59160,59161,59180],{"__ignoreMap":47},[52,59162,59163,59165,59167,59169,59171,59173,59176,59178],{"class":54,"line":55},[52,59164,58100],{"class":62},[52,59166,80],{"class":79},[52,59168,11037],{"class":241},[52,59170,193],{"class":102},[52,59172,115],{"class":83},[52,59174,59175],{"class":87},"Enter names: ",[52,59177,115],{"class":83},[52,59179,211],{"class":102},[52,59181,59182,59184,59186],{"class":54,"line":66},[52,59183,33090],{"class":62},[52,59185,80],{"class":79},[52,59187,59188],{"class":62}," user_input\n",[14,59190,59191,59192,59194],{},"Here, ",[49,59193,5327],{}," is still just one string.",[14,59196,59197],{},"Fix it with:",[42,59199,59201],{"className":44,"code":59200,"language":46,"meta":47,"style":47},"names = user_input.split(\",\")\n",[49,59202,59203],{"__ignoreMap":47},[52,59204,59205,59207,59209,59211,59213,59215,59217,59219,59221,59223],{"class":54,"line":55},[52,59206,33090],{"class":62},[52,59208,80],{"class":79},[52,59210,58128],{"class":62},[52,59212,186],{"class":102},[52,59214,58133],{"class":189},[52,59216,193],{"class":102},[52,59218,115],{"class":83},[52,59220,199],{"class":87},[52,59222,115],{"class":83},[52,59224,211],{"class":102},[639,59226,59228],{"id":59227},"keeping-extra-spaces-around-names","Keeping extra spaces around names",[14,59230,59231],{},"If the input is:",[42,59233,59235],{"className":44,"code":59234,"language":46,"meta":47,"style":47},"Alice, Bob, Carlos\n",[49,59236,59237],{"__ignoreMap":47},[52,59238,59239,59241,59243,59245,59247],{"class":54,"line":55},[52,59240,2156],{"class":62},[52,59242,199],{"class":102},[52,59244,58053],{"class":62},[52,59246,199],{"class":102},[52,59248,57815],{"class":62},[14,59250,59251,59252,186],{},"then splitting alone may produce values with spaces, such as ",[49,59253,59254],{},"\" Bob\"",[14,59256,59257,59258,118],{},"Fix it with ",[49,59259,18831],{},[42,59261,59263],{"className":44,"code":59262,"language":46,"meta":47,"style":47},"names = [name.strip() for name in user_input.split(\",\")]\n",[49,59264,59265],{"__ignoreMap":47},[52,59266,59267,59269,59271,59273,59275,59277,59279,59281,59283,59285,59287,59289,59291,59293,59295,59297,59299,59301],{"class":54,"line":55},[52,59268,33090],{"class":62},[52,59270,80],{"class":79},[52,59272,18162],{"class":102},[52,59274,2147],{"class":62},[52,59276,186],{"class":102},[52,59278,18630],{"class":189},[52,59280,8267],{"class":102},[52,59282,37077],{"class":58},[52,59284,11292],{"class":62},[52,59286,11213],{"class":58},[52,59288,58128],{"class":62},[52,59290,186],{"class":102},[52,59292,58133],{"class":189},[52,59294,193],{"class":102},[52,59296,115],{"class":83},[52,59298,199],{"class":87},[52,59300,115],{"class":83},[52,59302,33167],{"class":102},[639,59304,38741,59306],{"id":59305},"misspelling-choice",[49,59307,12230],{},[14,59309,59310],{},"This is incorrect:",[42,59312,59314],{"className":44,"code":59313,"language":46,"meta":47,"style":47},"random.choise(names)\n",[49,59315,59316],{"__ignoreMap":47},[52,59317,59318,59320,59322,59325,59327,59329],{"class":54,"line":55},[52,59319,36610],{"class":62},[52,59321,186],{"class":102},[52,59323,59324],{"class":189},"choise",[52,59326,193],{"class":102},[52,59328,5327],{"class":189},[52,59330,211],{"class":102},[14,59332,59333],{},"The correct function name is:",[42,59335,59337],{"className":44,"code":59336,"language":46,"meta":47,"style":47},"random.choice(names)\n",[49,59338,59339],{"__ignoreMap":47},[52,59340,59341,59343,59345,59347,59349,59351],{"class":54,"line":55},[52,59342,36610],{"class":62},[52,59344,186],{"class":102},[52,59346,12230],{"class":189},[52,59348,193],{"class":102},[52,59350,5327],{"class":189},[52,59352,211],{"class":102},[639,59354,39851],{"id":39850},[14,59356,59357],{},"If your program is not working, print values to inspect them:",[42,59359,59361],{"className":44,"code":59360,"language":46,"meta":47,"style":47},"print(names)\nprint(type(names))\nprint(len(names))\nprint(repr(user_input))\n",[49,59362,59363,59373,59387,59401],{"__ignoreMap":47},[52,59364,59365,59367,59369,59371],{"class":54,"line":55},[52,59366,242],{"class":241},[52,59368,193],{"class":102},[52,59370,5327],{"class":189},[52,59372,211],{"class":102},[52,59374,59375,59377,59379,59381,59383,59385],{"class":54,"line":66},[52,59376,242],{"class":241},[52,59378,193],{"class":102},[52,59380,4198],{"class":4733},[52,59382,193],{"class":102},[52,59384,5327],{"class":189},[52,59386,8886],{"class":102},[52,59388,59389,59391,59393,59395,59397,59399],{"class":54,"line":73},[52,59390,242],{"class":241},[52,59392,193],{"class":102},[52,59394,16152],{"class":241},[52,59396,193],{"class":102},[52,59398,5327],{"class":189},[52,59400,8886],{"class":102},[52,59402,59403,59405,59407,59409,59411,59413],{"class":54,"line":94},[52,59404,242],{"class":241},[52,59406,193],{"class":102},[52,59408,20689],{"class":241},[52,59410,193],{"class":102},[52,59412,14364],{"class":189},[52,59414,8886],{"class":102},[14,59416,59417],{},"These checks help you answer questions like:",[309,59419,59420,59426,59429,59432],{},[24,59421,59422,59423,59425],{},"Is ",[49,59424,5327],{}," really a list?",[24,59427,59428],{},"Is the list empty?",[24,59430,59431],{},"Did the input contain extra spaces?",[24,59433,59434],{},"Did the program read what you expected?",[37,59436,1942],{"id":1941},[639,59438,59440],{"id":59439},"how-do-i-pick-a-random-name-from-a-list-in-python","How do I pick a random name from a list in Python?",[14,59442,3650,59443,59446,59447,13338],{},[49,59444,59445],{},"random.choice(your_list)"," after importing the ",[49,59448,36610],{},[639,59450,3677,59452,59454],{"id":59451},"why-does-randomchoice-fail",[49,59453,53989],{}," fail?",[14,59456,59457,59458,59460],{},"It usually fails because the list is empty or the ",[49,59459,36610],{}," module was not imported correctly.",[639,59462,59464],{"id":59463},"can-i-let-the-user-enter-the-names","Can I let the user enter the names?",[14,59466,59467],{},"Yes. Read input as text, split it by commas, clean spaces, and then choose from the resulting list.",[639,59469,59471],{"id":59470},"how-do-i-pick-more-than-one-random-name","How do I pick more than one random name?",[14,59473,3650,59474,59476],{},[49,59475,58585],{}," if you want multiple different names without repeats.",[37,59478,2005],{"id":2004},[309,59480,59481,59485,59490,59494,59498],{},[24,59482,59483],{},[372,59484,36730],{"href":36729},[24,59486,59487],{},[372,59488,59489],{"href":58000},"random.choice() function explained",[24,59491,59492],{},[372,59493,44583],{"href":57870},[24,59495,59496],{},[372,59497,6876],{"href":6397},[24,59499,59500],{},[372,59501,58255],{"href":58254},[14,59503,59504],{},"Try changing this project next by:",[309,59506,59507,59510,59513],{},[24,59508,59509],{},"accepting names from the user",[24,59511,59512],{},"repeating the pick in a loop",[24,59514,59515],{},"choosing multiple names without duplicates",[2034,59517,59518],{},"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 .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 .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 .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":47,"searchDepth":66,"depth":66,"links":59520},[59521,59522,59523,59527,59534,59537,59540,59546,59556,59563],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":57692,"depth":66,"text":57693,"children":59524},[59525,59526],{"id":13562,"depth":73,"text":13563},{"id":32448,"depth":73,"text":32449},{"id":453,"depth":66,"text":454,"children":59528},[59529,59531,59532,59533],{"id":57878,"depth":73,"text":59530},"1. Import the random module",{"id":57909,"depth":73,"text":57910},{"id":57964,"depth":73,"text":57965},{"id":58004,"depth":73,"text":58005},{"id":54303,"depth":66,"text":54304,"children":59535},[59536],{"id":9909,"depth":73,"text":9910},{"id":58258,"depth":66,"text":58259,"children":59538},[59539],{"id":4754,"depth":73,"text":4755},{"id":47794,"depth":66,"text":47795,"children":59541},[59542,59543,59544,59545],{"id":58578,"depth":73,"text":58579},{"id":58693,"depth":73,"text":58694},{"id":58790,"depth":73,"text":58791},{"id":58944,"depth":73,"text":58945},{"id":12321,"depth":66,"text":12322,"children":59547},[59548,59549,59551,59552,59553,59555],{"id":37421,"depth":73,"text":38104},{"id":59044,"depth":73,"text":59550},"Using random.choice() on an empty list",{"id":59151,"depth":73,"text":59152},{"id":59227,"depth":73,"text":59228},{"id":59305,"depth":73,"text":59554},"Misspelling choice",{"id":39850,"depth":73,"text":39851},{"id":1941,"depth":66,"text":1942,"children":59557},[59558,59559,59561,59562],{"id":59439,"depth":73,"text":59440},{"id":59451,"depth":73,"text":59560},"Why does random.choice() fail?",{"id":59463,"depth":73,"text":59464},{"id":59470,"depth":73,"text":59471},{"id":2004,"depth":66,"text":2005},"Master python random name picker example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-random-name-picker-example",{"title":57550,"description":59564},"examples\u002Fpython-random-name-picker-example","sY9P2hDHvCZhwRSit6DyahaF7bLg0S_DpkjmHVz66ig",{"id":59571,"title":59572,"body":59573,"description":61253,"extension":2075,"meta":61254,"navigation":69,"path":61255,"seo":61256,"stem":61257,"__hash__":61258},"content\u002Fexamples\u002Fpython-random-password-list-generator-example.md","Python Random Password List Generator Example",{"type":7,"value":59574,"toc":61221},[59575,59578,59581,59584,59599,59602,59604,59723,59726,59728,59730,59741,59745,59748,59766,59770,59773,59891,59893,59896,59902,59906,59963,59965,59968,60000,60003,60012,60015,60060,60063,60086,60091,60095,60098,60106,60274,60276,60298,60306,60310,60317,60319,60345,60351,60357,60359,60362,60366,60369,60385,60388,60486,60490,60493,60604,60608,60611,60759,60763,60766,60929,60935,60937,60940,60965,60969,60972,61000,61004,61007,61074,61077,61095,61099,61102,61105,61121,61128,61130,61134,61139,61143,61151,61155,61160,61164,61167,61169,61201,61204,61218],[10,59576,59572],{"id":59577},"python-random-password-list-generator-example",[14,59579,59580],{},"This example shows how to build a simple Python script that generates a list of random passwords.",[14,59582,59583],{},"You will use:",[309,59585,59586,59590,59593,59596],{},[24,59587,6728,59588,14928],{},[49,59589,36610],{},[24,59591,59592],{},"strings of allowed characters",[24,59594,59595],{},"loops",[24,59597,59598],{},"a little user input for customization",[14,59600,59601],{},"This is a good beginner project because it combines several basic Python ideas in one small program.",[37,59603,40],{"id":39},[42,59605,59607],{"className":44,"code":59606,"language":46,"meta":47,"style":47},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits + string.punctuation\n\nfor _ in range(5):\n    password = ''.join(random.choice(characters) for _ in range(12))\n    print(password)\n",[49,59608,59609,59615,59621,59625,59653,59657,59673,59713],{"__ignoreMap":47},[52,59610,59611,59613],{"class":54,"line":55},[52,59612,59],{"class":58},[52,59614,36646],{"class":62},[52,59616,59617,59619],{"class":54,"line":66},[52,59618,59],{"class":58},[52,59620,53460],{"class":62},[52,59622,59623],{"class":54,"line":73},[52,59624,70],{"emptyLinePlaceholder":69},[52,59626,59627,59629,59631,59633,59635,59637,59639,59641,59643,59645,59647,59649,59651],{"class":54,"line":94},[52,59628,53479],{"class":62},[52,59630,80],{"class":79},[52,59632,53484],{"class":62},[52,59634,186],{"class":102},[52,59636,53489],{"class":1320},[52,59638,5234],{"class":79},[52,59640,53484],{"class":62},[52,59642,186],{"class":102},[52,59644,53498],{"class":1320},[52,59646,5234],{"class":79},[52,59648,53484],{"class":62},[52,59650,186],{"class":102},[52,59652,53507],{"class":1320},[52,59654,59655],{"class":54,"line":106},[52,59656,70],{"emptyLinePlaceholder":69},[52,59658,59659,59661,59663,59665,59667,59669,59671],{"class":54,"line":128},[52,59660,12265],{"class":58},[52,59662,37080],{"class":62},[52,59664,11213],{"class":58},[52,59666,36864],{"class":241},[52,59668,193],{"class":102},[52,59670,9479],{"class":121},[52,59672,3114],{"class":102},[52,59674,59675,59677,59679,59681,59683,59685,59687,59689,59691,59693,59695,59697,59699,59701,59703,59705,59707,59709,59711],{"class":54,"line":148},[52,59676,55357],{"class":62},[52,59678,80],{"class":79},[52,59680,53516],{"class":83},[52,59682,186],{"class":102},[52,59684,34643],{"class":189},[52,59686,193],{"class":102},[52,59688,36610],{"class":189},[52,59690,186],{"class":102},[52,59692,12230],{"class":189},[52,59694,193],{"class":102},[52,59696,53533],{"class":189},[52,59698,1521],{"class":102},[52,59700,37077],{"class":58},[52,59702,37080],{"class":189},[52,59704,11213],{"class":58},[52,59706,36864],{"class":241},[52,59708,193],{"class":102},[52,59710,32829],{"class":121},[52,59712,8886],{"class":102},[52,59714,59715,59717,59719,59721],{"class":54,"line":164},[52,59716,1599],{"class":241},[52,59718,193],{"class":102},[52,59720,53563],{"class":189},[52,59722,211],{"class":102},[14,59724,59725],{},"This quick example prints 5 random passwords, each 12 characters long.",[37,59727,9295],{"id":9294},[14,59729,13115],{},[309,59731,59732,59735,59738],{},[24,59733,59734],{},"a program that generates multiple random passwords",[24,59736,59737],{},"passwords made from letters, digits, and symbols",[24,59739,59740],{},"an easy way to change password length and how many passwords to create",[37,59742,59744],{"id":59743},"what-you-need-to-understand-first","What you need to understand first",[14,59746,59747],{},"Before looking at the full code, these ideas help:",[309,59749,59750,59753,59760,59763],{},[24,59751,59752],{},"A string can hold all characters that are allowed in the password.",[24,59754,59755,59759],{},[372,59756,59757],{"href":58000},[49,59758,53989],{}," picks one item from a sequence at random.",[24,59761,59762],{},"A loop can repeat the character-picking step to build one full password.",[24,59764,59765],{},"Another loop can repeat the whole process to generate many passwords.",[37,59767,59769],{"id":59768},"basic-password-list-generator","Basic password list generator",[14,59771,59772],{},"Here is a simple version:",[42,59774,59775],{"className":44,"code":59606,"language":46,"meta":47,"style":47},[49,59776,59777,59783,59789,59793,59821,59825,59841,59881],{"__ignoreMap":47},[52,59778,59779,59781],{"class":54,"line":55},[52,59780,59],{"class":58},[52,59782,36646],{"class":62},[52,59784,59785,59787],{"class":54,"line":66},[52,59786,59],{"class":58},[52,59788,53460],{"class":62},[52,59790,59791],{"class":54,"line":73},[52,59792,70],{"emptyLinePlaceholder":69},[52,59794,59795,59797,59799,59801,59803,59805,59807,59809,59811,59813,59815,59817,59819],{"class":54,"line":94},[52,59796,53479],{"class":62},[52,59798,80],{"class":79},[52,59800,53484],{"class":62},[52,59802,186],{"class":102},[52,59804,53489],{"class":1320},[52,59806,5234],{"class":79},[52,59808,53484],{"class":62},[52,59810,186],{"class":102},[52,59812,53498],{"class":1320},[52,59814,5234],{"class":79},[52,59816,53484],{"class":62},[52,59818,186],{"class":102},[52,59820,53507],{"class":1320},[52,59822,59823],{"class":54,"line":106},[52,59824,70],{"emptyLinePlaceholder":69},[52,59826,59827,59829,59831,59833,59835,59837,59839],{"class":54,"line":128},[52,59828,12265],{"class":58},[52,59830,37080],{"class":62},[52,59832,11213],{"class":58},[52,59834,36864],{"class":241},[52,59836,193],{"class":102},[52,59838,9479],{"class":121},[52,59840,3114],{"class":102},[52,59842,59843,59845,59847,59849,59851,59853,59855,59857,59859,59861,59863,59865,59867,59869,59871,59873,59875,59877,59879],{"class":54,"line":148},[52,59844,55357],{"class":62},[52,59846,80],{"class":79},[52,59848,53516],{"class":83},[52,59850,186],{"class":102},[52,59852,34643],{"class":189},[52,59854,193],{"class":102},[52,59856,36610],{"class":189},[52,59858,186],{"class":102},[52,59860,12230],{"class":189},[52,59862,193],{"class":102},[52,59864,53533],{"class":189},[52,59866,1521],{"class":102},[52,59868,37077],{"class":58},[52,59870,37080],{"class":189},[52,59872,11213],{"class":58},[52,59874,36864],{"class":241},[52,59876,193],{"class":102},[52,59878,32829],{"class":121},[52,59880,8886],{"class":102},[52,59882,59883,59885,59887,59889],{"class":54,"line":164},[52,59884,1599],{"class":241},[52,59886,193],{"class":102},[52,59888,53563],{"class":189},[52,59890,211],{"class":102},[639,59892,2510],{"id":2509},[14,59894,59895],{},"Your output will be different each time, but it may look like this:",[42,59897,59900],{"className":59898,"code":59899,"language":955,"meta":47},[953],"d8@Kp!2Lm#Q1\n7&nZ4^xP0!aB\nm$T9q@W2e*R5\n!L8v#Y1c%N6s\nQ2@fG7&kP!z4\n",[49,59901,59899],{"__ignoreMap":47},[639,59903,59905],{"id":59904},"what-this-code-does","What this code does",[309,59907,59908,59913,59919,59924,59933,59945,59951,59954],{},[24,59909,59910,59912],{},[49,59911,36739],{}," loads Python's random tools.",[24,59914,59915,59918],{},[49,59916,59917],{},"import string"," gives you ready-made character groups.",[24,59920,59921,59923],{},[49,59922,53771],{}," adds lowercase and uppercase letters.",[24,59925,59926,59928,59929,37261,59931,186],{},[49,59927,53824],{}," adds numbers from ",[49,59930,13294],{},[49,59932,37264],{},[24,59934,59935,59937,59938,2587,59940,11556,59942,186],{},[49,59936,53883],{}," adds symbols like ",[49,59939,53930],{},[49,59941,20971],{},[49,59943,59944],{},"#",[24,59946,358,59947,59950],{},[49,59948,59949],{},"for _ in range(5)"," loop creates 5 passwords.",[24,59952,59953],{},"The inner part builds a 12-character password by picking one random character at a time.",[24,59955,59956,59962],{},[372,59957,59959],{"href":59958},"\u002Freference\u002Fpython-string-join-method",[49,59960,59961],{},"''.join()"," combines those characters into one string.",[37,59964,13312],{"id":13311},[14,59966,59967],{},"This line creates the allowed character set:",[42,59969,59970],{"className":44,"code":53947,"language":46,"meta":47,"style":47},[49,59971,59972],{"__ignoreMap":47},[52,59973,59974,59976,59978,59980,59982,59984,59986,59988,59990,59992,59994,59996,59998],{"class":54,"line":55},[52,59975,53479],{"class":62},[52,59977,80],{"class":79},[52,59979,53484],{"class":62},[52,59981,186],{"class":102},[52,59983,53489],{"class":1320},[52,59985,5234],{"class":79},[52,59987,53484],{"class":62},[52,59989,186],{"class":102},[52,59991,53498],{"class":1320},[52,59993,5234],{"class":79},[52,59995,53484],{"class":62},[52,59997,186],{"class":102},[52,59999,53507],{"class":1320},[14,60001,60002],{},"It combines:",[309,60004,60005,60007,60009],{},[24,60006,54129],{},[24,60008,53498],{},[24,60010,60011],{},"punctuation symbols",[14,60013,60014],{},"This line builds one password:",[42,60016,60018],{"className":44,"code":60017,"language":46,"meta":47,"style":47},"password = ''.join(random.choice(characters) for _ in range(12))\n",[49,60019,60020],{"__ignoreMap":47},[52,60021,60022,60024,60026,60028,60030,60032,60034,60036,60038,60040,60042,60044,60046,60048,60050,60052,60054,60056,60058],{"class":54,"line":55},[52,60023,26217],{"class":62},[52,60025,80],{"class":79},[52,60027,53516],{"class":83},[52,60029,186],{"class":102},[52,60031,34643],{"class":189},[52,60033,193],{"class":102},[52,60035,36610],{"class":189},[52,60037,186],{"class":102},[52,60039,12230],{"class":189},[52,60041,193],{"class":102},[52,60043,53533],{"class":189},[52,60045,1521],{"class":102},[52,60047,37077],{"class":58},[52,60049,37080],{"class":189},[52,60051,11213],{"class":58},[52,60053,36864],{"class":241},[52,60055,193],{"class":102},[52,60057,32829],{"class":121},[52,60059,8886],{"class":102},[14,60061,60062],{},"Here is what happens:",[309,60064,60065,60071,60077,60080],{},[24,60066,60067,60070],{},[49,60068,60069],{},"range(12)"," means \"do this 12 times\"",[24,60072,60073,60076],{},[49,60074,60075],{},"random.choice(characters)"," picks one random character each time",[24,60078,60079],{},"the generator expression produces 12 random characters",[24,60081,60082,60085],{},[49,60083,60084],{},"join()"," combines them into one password string",[14,60087,60088,60089,186],{},"If you want a deeper explanation of random tools, see the ",[372,60090,36730],{"href":36729},[37,60092,60094],{"id":60093},"let-the-user-control-length-and-count","Let the user control length and count",[14,60096,60097],{},"You can improve the script by asking the user for:",[309,60099,60100,60103],{},[24,60101,60102],{},"password length",[24,60104,60105],{},"number of passwords to generate",[42,60107,60109],{"className":44,"code":60108,"language":46,"meta":47,"style":47},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits + string.punctuation\n\nlength = int(input(\"Enter password length: \"))\ncount = int(input(\"How many passwords do you want to generate? \"))\n\nfor _ in range(count):\n    password = ''.join(random.choice(characters) for _ in range(length))\n    print(password)\n",[49,60110,60111,60117,60123,60127,60155,60159,60181,60204,60208,60224,60264],{"__ignoreMap":47},[52,60112,60113,60115],{"class":54,"line":55},[52,60114,59],{"class":58},[52,60116,36646],{"class":62},[52,60118,60119,60121],{"class":54,"line":66},[52,60120,59],{"class":58},[52,60122,53460],{"class":62},[52,60124,60125],{"class":54,"line":73},[52,60126,70],{"emptyLinePlaceholder":69},[52,60128,60129,60131,60133,60135,60137,60139,60141,60143,60145,60147,60149,60151,60153],{"class":54,"line":94},[52,60130,53479],{"class":62},[52,60132,80],{"class":79},[52,60134,53484],{"class":62},[52,60136,186],{"class":102},[52,60138,53489],{"class":1320},[52,60140,5234],{"class":79},[52,60142,53484],{"class":62},[52,60144,186],{"class":102},[52,60146,53498],{"class":1320},[52,60148,5234],{"class":79},[52,60150,53484],{"class":62},[52,60152,186],{"class":102},[52,60154,53507],{"class":1320},[52,60156,60157],{"class":54,"line":106},[52,60158,70],{"emptyLinePlaceholder":69},[52,60160,60161,60163,60165,60167,60169,60171,60173,60175,60177,60179],{"class":54,"line":128},[52,60162,53469],{"class":62},[52,60164,80],{"class":79},[52,60166,9789],{"class":4733},[52,60168,193],{"class":102},[52,60170,13610],{"class":241},[52,60172,193],{"class":102},[52,60174,115],{"class":83},[52,60176,54347],{"class":87},[52,60178,115],{"class":83},[52,60180,8886],{"class":102},[52,60182,60183,60185,60187,60189,60191,60193,60195,60197,60200,60202],{"class":54,"line":148},[52,60184,55300],{"class":62},[52,60186,80],{"class":79},[52,60188,9789],{"class":4733},[52,60190,193],{"class":102},[52,60192,13610],{"class":241},[52,60194,193],{"class":102},[52,60196,115],{"class":83},[52,60198,60199],{"class":87},"How many passwords do you want to generate? ",[52,60201,115],{"class":83},[52,60203,8886],{"class":102},[52,60205,60206],{"class":54,"line":164},[52,60207,70],{"emptyLinePlaceholder":69},[52,60209,60210,60212,60214,60216,60218,60220,60222],{"class":54,"line":170},[52,60211,12265],{"class":58},[52,60213,37080],{"class":62},[52,60215,11213],{"class":58},[52,60217,36864],{"class":241},[52,60219,193],{"class":102},[52,60221,45367],{"class":189},[52,60223,3114],{"class":102},[52,60225,60226,60228,60230,60232,60234,60236,60238,60240,60242,60244,60246,60248,60250,60252,60254,60256,60258,60260,60262],{"class":54,"line":175},[52,60227,55357],{"class":62},[52,60229,80],{"class":79},[52,60231,53516],{"class":83},[52,60233,186],{"class":102},[52,60235,34643],{"class":189},[52,60237,193],{"class":102},[52,60239,36610],{"class":189},[52,60241,186],{"class":102},[52,60243,12230],{"class":189},[52,60245,193],{"class":102},[52,60247,53533],{"class":189},[52,60249,1521],{"class":102},[52,60251,37077],{"class":58},[52,60253,37080],{"class":189},[52,60255,11213],{"class":58},[52,60257,36864],{"class":241},[52,60259,193],{"class":102},[52,60261,53548],{"class":189},[52,60263,8886],{"class":102},[52,60265,60266,60268,60270,60272],{"class":54,"line":214},[52,60267,1599],{"class":241},[52,60269,193],{"class":102},[52,60271,53563],{"class":189},[52,60273,211],{"class":102},[639,60275,9910],{"id":9909},[309,60277,60278,60283,60288,60293],{},[24,60279,60280,60282],{},[49,60281,10597],{}," reads text typed by the user",[24,60284,60285,60287],{},[49,60286,9492],{}," converts that text to an integer",[24,60289,60290,60292],{},[49,60291,53548],{}," controls how many characters are in each password",[24,60294,60295,60297],{},[49,60296,45367],{}," controls how many passwords are generated",[14,60299,60300,60301,759,60303,186],{},"If you are new to this, read ",[372,60302,6398],{"href":6397},[372,60304,60305],{"href":15166},"how to convert a string to an int in Python",[639,60307,60309],{"id":60308},"important-note-about-errors","Important note about errors",[14,60311,60312,60313,19623,60315,186],{},"If the user types something that is not a whole number, ",[49,60314,9492],{},[49,60316,13780],{},[14,60318,16291],{},[42,60320,60321],{"className":44,"code":54529,"language":46,"meta":47,"style":47},[49,60322,60323],{"__ignoreMap":47},[52,60324,60325,60327,60329,60331,60333,60335,60337,60339,60341,60343],{"class":54,"line":55},[52,60326,53469],{"class":62},[52,60328,80],{"class":79},[52,60330,9789],{"class":4733},[52,60332,193],{"class":102},[52,60334,13610],{"class":241},[52,60336,193],{"class":102},[52,60338,115],{"class":83},[52,60340,54347],{"class":87},[52,60342,115],{"class":83},[52,60344,8886],{"class":102},[14,60346,53037,60347,60350],{},[49,60348,60349],{},"abc",", Python raises an error.",[14,60352,60353,60354,60356],{},"See ",[372,60355,54573],{"href":6471}," if that happens.",[37,60358,50659],{"id":50658},[14,60360,60361],{},"Here are a few easy ways to change the program.",[639,60363,60365],{"id":60364},"avoid-hard-to-read-characters","Avoid hard-to-read characters",[14,60367,60368],{},"Some characters are easy to confuse, such as:",[309,60370,60371,60378],{},[24,60372,60373,759,60376],{},[49,60374,60375],{},"O",[49,60377,13294],{},[24,60379,60380,759,60383],{},[49,60381,60382],{},"l",[49,60384,9200],{},[14,60386,60387],{},"You can build your own character set:",[42,60389,60391],{"className":44,"code":60390,"language":46,"meta":47,"style":47},"import random\n\ncharacters = \"abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789\"\n\nfor _ in range(5):\n    password = ''.join(random.choice(characters) for _ in range(12))\n    print(password)\n",[49,60392,60393,60399,60403,60416,60420,60436,60476],{"__ignoreMap":47},[52,60394,60395,60397],{"class":54,"line":55},[52,60396,59],{"class":58},[52,60398,36646],{"class":62},[52,60400,60401],{"class":54,"line":66},[52,60402,70],{"emptyLinePlaceholder":69},[52,60404,60405,60407,60409,60411,60414],{"class":54,"line":73},[52,60406,53479],{"class":62},[52,60408,80],{"class":79},[52,60410,84],{"class":83},[52,60412,60413],{"class":87},"abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789",[52,60415,91],{"class":83},[52,60417,60418],{"class":54,"line":94},[52,60419,70],{"emptyLinePlaceholder":69},[52,60421,60422,60424,60426,60428,60430,60432,60434],{"class":54,"line":106},[52,60423,12265],{"class":58},[52,60425,37080],{"class":62},[52,60427,11213],{"class":58},[52,60429,36864],{"class":241},[52,60431,193],{"class":102},[52,60433,9479],{"class":121},[52,60435,3114],{"class":102},[52,60437,60438,60440,60442,60444,60446,60448,60450,60452,60454,60456,60458,60460,60462,60464,60466,60468,60470,60472,60474],{"class":54,"line":128},[52,60439,55357],{"class":62},[52,60441,80],{"class":79},[52,60443,53516],{"class":83},[52,60445,186],{"class":102},[52,60447,34643],{"class":189},[52,60449,193],{"class":102},[52,60451,36610],{"class":189},[52,60453,186],{"class":102},[52,60455,12230],{"class":189},[52,60457,193],{"class":102},[52,60459,53533],{"class":189},[52,60461,1521],{"class":102},[52,60463,37077],{"class":58},[52,60465,37080],{"class":189},[52,60467,11213],{"class":58},[52,60469,36864],{"class":241},[52,60471,193],{"class":102},[52,60473,32829],{"class":121},[52,60475,8886],{"class":102},[52,60477,60478,60480,60482,60484],{"class":54,"line":148},[52,60479,1599],{"class":241},[52,60481,193],{"class":102},[52,60483,53563],{"class":189},[52,60485,211],{"class":102},[639,60487,60489],{"id":60488},"generate-passwords-without-symbols","Generate passwords without symbols",[14,60491,60492],{},"If you only want letters and numbers:",[42,60494,60496],{"className":44,"code":60495,"language":46,"meta":47,"style":47},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits\n\nfor _ in range(5):\n    password = ''.join(random.choice(characters) for _ in range(12))\n    print(password)\n",[49,60497,60498,60504,60510,60514,60534,60538,60554,60594],{"__ignoreMap":47},[52,60499,60500,60502],{"class":54,"line":55},[52,60501,59],{"class":58},[52,60503,36646],{"class":62},[52,60505,60506,60508],{"class":54,"line":66},[52,60507,59],{"class":58},[52,60509,53460],{"class":62},[52,60511,60512],{"class":54,"line":73},[52,60513,70],{"emptyLinePlaceholder":69},[52,60515,60516,60518,60520,60522,60524,60526,60528,60530,60532],{"class":54,"line":94},[52,60517,53479],{"class":62},[52,60519,80],{"class":79},[52,60521,53484],{"class":62},[52,60523,186],{"class":102},[52,60525,53489],{"class":1320},[52,60527,5234],{"class":79},[52,60529,53484],{"class":62},[52,60531,186],{"class":102},[52,60533,54682],{"class":1320},[52,60535,60536],{"class":54,"line":106},[52,60537,70],{"emptyLinePlaceholder":69},[52,60539,60540,60542,60544,60546,60548,60550,60552],{"class":54,"line":128},[52,60541,12265],{"class":58},[52,60543,37080],{"class":62},[52,60545,11213],{"class":58},[52,60547,36864],{"class":241},[52,60549,193],{"class":102},[52,60551,9479],{"class":121},[52,60553,3114],{"class":102},[52,60555,60556,60558,60560,60562,60564,60566,60568,60570,60572,60574,60576,60578,60580,60582,60584,60586,60588,60590,60592],{"class":54,"line":148},[52,60557,55357],{"class":62},[52,60559,80],{"class":79},[52,60561,53516],{"class":83},[52,60563,186],{"class":102},[52,60565,34643],{"class":189},[52,60567,193],{"class":102},[52,60569,36610],{"class":189},[52,60571,186],{"class":102},[52,60573,12230],{"class":189},[52,60575,193],{"class":102},[52,60577,53533],{"class":189},[52,60579,1521],{"class":102},[52,60581,37077],{"class":58},[52,60583,37080],{"class":189},[52,60585,11213],{"class":58},[52,60587,36864],{"class":241},[52,60589,193],{"class":102},[52,60591,32829],{"class":121},[52,60593,8886],{"class":102},[52,60595,60596,60598,60600,60602],{"class":54,"line":164},[52,60597,1599],{"class":241},[52,60599,193],{"class":102},[52,60601,53563],{"class":189},[52,60603,211],{"class":102},[639,60605,60607],{"id":60606},"store-passwords-in-a-list","Store passwords in a list",[14,60609,60610],{},"Instead of printing passwords right away, you can save them in a list:",[42,60612,60614],{"className":44,"code":60613,"language":46,"meta":47,"style":47},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits + string.punctuation\npasswords = []\n\nfor _ in range(5):\n    password = ''.join(random.choice(characters) for _ in range(12))\n    passwords.append(password)\n\nprint(passwords)\n",[49,60615,60616,60622,60628,60632,60660,60669,60673,60689,60729,60744,60748],{"__ignoreMap":47},[52,60617,60618,60620],{"class":54,"line":55},[52,60619,59],{"class":58},[52,60621,36646],{"class":62},[52,60623,60624,60626],{"class":54,"line":66},[52,60625,59],{"class":58},[52,60627,53460],{"class":62},[52,60629,60630],{"class":54,"line":73},[52,60631,70],{"emptyLinePlaceholder":69},[52,60633,60634,60636,60638,60640,60642,60644,60646,60648,60650,60652,60654,60656,60658],{"class":54,"line":94},[52,60635,53479],{"class":62},[52,60637,80],{"class":79},[52,60639,53484],{"class":62},[52,60641,186],{"class":102},[52,60643,53489],{"class":1320},[52,60645,5234],{"class":79},[52,60647,53484],{"class":62},[52,60649,186],{"class":102},[52,60651,53498],{"class":1320},[52,60653,5234],{"class":79},[52,60655,53484],{"class":62},[52,60657,186],{"class":102},[52,60659,53507],{"class":1320},[52,60661,60662,60665,60667],{"class":54,"line":106},[52,60663,60664],{"class":62},"passwords ",[52,60666,80],{"class":79},[52,60668,18590],{"class":102},[52,60670,60671],{"class":54,"line":128},[52,60672,70],{"emptyLinePlaceholder":69},[52,60674,60675,60677,60679,60681,60683,60685,60687],{"class":54,"line":148},[52,60676,12265],{"class":58},[52,60678,37080],{"class":62},[52,60680,11213],{"class":58},[52,60682,36864],{"class":241},[52,60684,193],{"class":102},[52,60686,9479],{"class":121},[52,60688,3114],{"class":102},[52,60690,60691,60693,60695,60697,60699,60701,60703,60705,60707,60709,60711,60713,60715,60717,60719,60721,60723,60725,60727],{"class":54,"line":164},[52,60692,55357],{"class":62},[52,60694,80],{"class":79},[52,60696,53516],{"class":83},[52,60698,186],{"class":102},[52,60700,34643],{"class":189},[52,60702,193],{"class":102},[52,60704,36610],{"class":189},[52,60706,186],{"class":102},[52,60708,12230],{"class":189},[52,60710,193],{"class":102},[52,60712,53533],{"class":189},[52,60714,1521],{"class":102},[52,60716,37077],{"class":58},[52,60718,37080],{"class":189},[52,60720,11213],{"class":58},[52,60722,36864],{"class":241},[52,60724,193],{"class":102},[52,60726,32829],{"class":121},[52,60728,8886],{"class":102},[52,60730,60731,60734,60736,60738,60740,60742],{"class":54,"line":170},[52,60732,60733],{"class":62},"    passwords",[52,60735,186],{"class":102},[52,60737,18748],{"class":189},[52,60739,193],{"class":102},[52,60741,53563],{"class":189},[52,60743,211],{"class":102},[52,60745,60746],{"class":54,"line":175},[52,60747,70],{"emptyLinePlaceholder":69},[52,60749,60750,60752,60754,60757],{"class":54,"line":214},[52,60751,242],{"class":241},[52,60753,193],{"class":102},[52,60755,60756],{"class":189},"passwords",[52,60758,211],{"class":102},[639,60760,60762],{"id":60761},"write-passwords-to-a-text-file","Write passwords to a text file",[14,60764,60765],{},"You can also save the generated passwords to a file:",[42,60767,60769],{"className":44,"code":60768,"language":46,"meta":47,"style":47},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits + string.punctuation\n\nwith open(\"passwords.txt\", \"w\") as file:\n    for _ in range(5):\n        password = ''.join(random.choice(characters) for _ in range(12))\n        file.write(password + \"\\n\")\n",[49,60770,60771,60777,60783,60787,60815,60819,60850,60866,60907],{"__ignoreMap":47},[52,60772,60773,60775],{"class":54,"line":55},[52,60774,59],{"class":58},[52,60776,36646],{"class":62},[52,60778,60779,60781],{"class":54,"line":66},[52,60780,59],{"class":58},[52,60782,53460],{"class":62},[52,60784,60785],{"class":54,"line":73},[52,60786,70],{"emptyLinePlaceholder":69},[52,60788,60789,60791,60793,60795,60797,60799,60801,60803,60805,60807,60809,60811,60813],{"class":54,"line":94},[52,60790,53479],{"class":62},[52,60792,80],{"class":79},[52,60794,53484],{"class":62},[52,60796,186],{"class":102},[52,60798,53489],{"class":1320},[52,60800,5234],{"class":79},[52,60802,53484],{"class":62},[52,60804,186],{"class":102},[52,60806,53498],{"class":1320},[52,60808,5234],{"class":79},[52,60810,53484],{"class":62},[52,60812,186],{"class":102},[52,60814,53507],{"class":1320},[52,60816,60817],{"class":54,"line":106},[52,60818,70],{"emptyLinePlaceholder":69},[52,60820,60821,60823,60825,60827,60829,60832,60834,60836,60838,60840,60842,60844,60846,60848],{"class":54,"line":128},[52,60822,14632],{"class":58},[52,60824,14635],{"class":241},[52,60826,193],{"class":102},[52,60828,115],{"class":83},[52,60830,60831],{"class":87},"passwords.txt",[52,60833,115],{"class":83},[52,60835,199],{"class":102},[52,60837,84],{"class":83},[52,60839,16760],{"class":87},[52,60841,115],{"class":83},[52,60843,1521],{"class":102},[52,60845,3567],{"class":58},[52,60847,14676],{"class":14675},[52,60849,1330],{"class":102},[52,60851,60852,60854,60856,60858,60860,60862,60864],{"class":54,"line":148},[52,60853,11773],{"class":58},[52,60855,37080],{"class":62},[52,60857,11213],{"class":58},[52,60859,36864],{"class":241},[52,60861,193],{"class":102},[52,60863,9479],{"class":121},[52,60865,3114],{"class":102},[52,60867,60868,60871,60873,60875,60877,60879,60881,60883,60885,60887,60889,60891,60893,60895,60897,60899,60901,60903,60905],{"class":54,"line":164},[52,60869,60870],{"class":62},"        password ",[52,60872,80],{"class":79},[52,60874,53516],{"class":83},[52,60876,186],{"class":102},[52,60878,34643],{"class":189},[52,60880,193],{"class":102},[52,60882,36610],{"class":189},[52,60884,186],{"class":102},[52,60886,12230],{"class":189},[52,60888,193],{"class":102},[52,60890,53533],{"class":189},[52,60892,1521],{"class":102},[52,60894,37077],{"class":58},[52,60896,37080],{"class":189},[52,60898,11213],{"class":58},[52,60900,36864],{"class":241},[52,60902,193],{"class":102},[52,60904,32829],{"class":121},[52,60906,8886],{"class":102},[52,60908,60909,60911,60913,60915,60917,60919,60921,60923,60925,60927],{"class":54,"line":170},[52,60910,24643],{"class":14675},[52,60912,186],{"class":102},[52,60914,23849],{"class":189},[52,60916,193],{"class":102},[52,60918,26217],{"class":189},[52,60920,9881],{"class":79},[52,60922,84],{"class":83},[52,60924,10956],{"class":2533},[52,60926,115],{"class":83},[52,60928,211],{"class":102},[14,60930,60931,60932,60934],{},"This creates a file named ",[49,60933,60831],{}," and writes one password per line.",[37,60936,45733],{"id":45732},[14,60938,60939],{},"Common problems in this example include:",[309,60941,60942,60949,60954,60959],{},[24,60943,60944,60945,857,60947],{},"forgetting to import ",[49,60946,36610],{},[49,60948,41855],{},[24,60950,53266,60951,60953],{},[49,60952,10597],{}," values as strings instead of integers",[24,60955,53266,60956,60958],{},[49,60957,58585],{}," when repeated characters should be allowed",[24,60960,60961,60962,60964],{},"expecting ",[49,60963,53989],{}," to create security-grade passwords",[639,60966,60968],{"id":60967},"common-errors","Common errors",[14,60970,60971],{},"You may run into:",[309,60973,60974,60983,60990,60997],{},[24,60975,60976,1694,60978,857,60980,60982],{},[49,60977,14080],{},[49,60979,36610],{},[49,60981,41855],{}," was not imported",[24,60984,60985,60987,60988],{},[49,60986,13780],{}," when converting user input with ",[49,60989,9492],{},[24,60991,60992,60994,60995],{},[49,60993,1707],{}," from using a string number directly in ",[49,60996,37187],{},[24,60998,60999],{},"confusion about why some passwords contain repeated characters",[639,61001,61003],{"id":61002},"useful-debugging-checks","Useful debugging checks",[14,61005,61006],{},"If something is not working, print values to inspect them:",[42,61008,61010],{"className":44,"code":61009,"language":46,"meta":47,"style":47},"print(characters)\nprint(len(characters))\nprint(password)\nprint(type(length))\nprint(type(count))\n",[49,61011,61012,61022,61036,61046,61060],{"__ignoreMap":47},[52,61013,61014,61016,61018,61020],{"class":54,"line":55},[52,61015,242],{"class":241},[52,61017,193],{"class":102},[52,61019,53533],{"class":189},[52,61021,211],{"class":102},[52,61023,61024,61026,61028,61030,61032,61034],{"class":54,"line":66},[52,61025,242],{"class":241},[52,61027,193],{"class":102},[52,61029,16152],{"class":241},[52,61031,193],{"class":102},[52,61033,53533],{"class":189},[52,61035,8886],{"class":102},[52,61037,61038,61040,61042,61044],{"class":54,"line":73},[52,61039,242],{"class":241},[52,61041,193],{"class":102},[52,61043,53563],{"class":189},[52,61045,211],{"class":102},[52,61047,61048,61050,61052,61054,61056,61058],{"class":54,"line":94},[52,61049,242],{"class":241},[52,61051,193],{"class":102},[52,61053,4198],{"class":4733},[52,61055,193],{"class":102},[52,61057,53548],{"class":189},[52,61059,8886],{"class":102},[52,61061,61062,61064,61066,61068,61070,61072],{"class":54,"line":106},[52,61063,242],{"class":241},[52,61065,193],{"class":102},[52,61067,4198],{"class":4733},[52,61069,193],{"class":102},[52,61071,45367],{"class":189},[52,61073,8886],{"class":102},[14,61075,61076],{},"These checks help you confirm:",[309,61078,61079,61082,61085,61088],{},[24,61080,61081],{},"what characters are available",[24,61083,61084],{},"how many characters are in the set",[24,61086,61087],{},"what the generated password looks like",[24,61089,12309,61090,759,61092,61094],{},[49,61091,53548],{},[49,61093,45367],{}," are really integers",[37,61096,61098],{"id":61097},"when-not-to-use-this-approach","When not to use this approach",[14,61100,61101],{},"This example is good for learning, but it is not the best choice for real security.",[14,61103,61104],{},"Keep these points in mind:",[309,61106,61107,61113,61118],{},[24,61108,61109,61110,61112],{},"Do not use the ",[49,61111,36610],{}," module for high-security passwords.",[24,61114,61115,61116,13338],{},"For stronger password generation, use the ",[49,61117,55440],{},[24,61119,61120],{},"This example is mainly for practicing loops, strings, and randomness.",[14,61122,61123,61124,186],{},"If you want a simpler version that focuses on creating one password, see ",[372,61125,61127],{"href":61126},"\u002Fexamples\u002Fpython-password-generator-example\u002F","Python password generator example",[37,61129,1942],{"id":1941},[639,61131,61133],{"id":61132},"why-do-some-generated-passwords-repeat-characters","Why do some generated passwords repeat characters?",[14,61135,14459,61136,61138],{},[49,61137,53989],{}," can pick the same character more than once. That is normal for this approach.",[639,61140,61142],{"id":61141},"can-i-generate-passwords-without-symbols","Can I generate passwords without symbols?",[14,61144,61145,61146,759,61148,61150],{},"Yes. Use only ",[49,61147,53771],{},[49,61149,53824],{}," in the character set.",[639,61152,61154],{"id":61153},"is-the-random-module-safe-for-real-password-security","Is the random module safe for real password security?",[14,61156,61157,61158,13338],{},"No. It is fine for learning, but real password tools should use the ",[49,61159,55440],{},[639,61161,61163],{"id":61162},"how-do-i-save-the-passwords-to-a-file","How do I save the passwords to a file?",[14,61165,61166],{},"Store them in a list or write each one with a file opened in write mode.",[37,61168,2005],{"id":2004},[309,61170,61171,61175,61179,61184,61188,61192,61197],{},[24,61172,61173],{},[372,61174,36730],{"href":36729},[24,61176,61177],{},[372,61178,59489],{"href":58000},[24,61180,61181],{},[372,61182,61183],{"href":59958},"Python string join() method",[24,61185,61186],{},[372,61187,6876],{"href":6397},[24,61189,61190],{},[372,61191,16527],{"href":15166},[24,61193,61194],{},[372,61195,61196],{"href":6471},"Fix ValueError: invalid literal for int() with base 10",[24,61198,61199],{},[372,61200,61127],{"href":61126},[14,61202,61203],{},"Try improving this program next by:",[309,61205,61206,61209,61212],{},[24,61207,61208],{},"taking user input safely",[24,61210,61211],{},"saving results to a file",[24,61213,61214,61215,61217],{},"rebuilding it with the ",[49,61216,55440],{}," module for stronger password generation",[2034,61219,61220],{},"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":61222},[61223,61224,61225,61226,61230,61231,61235,61241,61245,61246,61252],{"id":39,"depth":66,"text":40},{"id":9294,"depth":66,"text":9295},{"id":59743,"depth":66,"text":59744},{"id":59768,"depth":66,"text":59769,"children":61227},[61228,61229],{"id":2509,"depth":73,"text":2510},{"id":59904,"depth":73,"text":59905},{"id":13311,"depth":66,"text":13312},{"id":60093,"depth":66,"text":60094,"children":61232},[61233,61234],{"id":9909,"depth":73,"text":9910},{"id":60308,"depth":73,"text":60309},{"id":50658,"depth":66,"text":50659,"children":61236},[61237,61238,61239,61240],{"id":60364,"depth":73,"text":60365},{"id":60488,"depth":73,"text":60489},{"id":60606,"depth":73,"text":60607},{"id":60761,"depth":73,"text":60762},{"id":45732,"depth":66,"text":45733,"children":61242},[61243,61244],{"id":60967,"depth":73,"text":60968},{"id":61002,"depth":73,"text":61003},{"id":61097,"depth":66,"text":61098},{"id":1941,"depth":66,"text":1942,"children":61247},[61248,61249,61250,61251],{"id":61132,"depth":73,"text":61133},{"id":61141,"depth":73,"text":61142},{"id":61153,"depth":73,"text":61154},{"id":61162,"depth":73,"text":61163},{"id":2004,"depth":66,"text":2005},"Master python random password list generator example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-random-password-list-generator-example",{"title":59572,"description":61253},"examples\u002Fpython-random-password-list-generator-example","HmwLVsaFPCh_4qQPzUQzyqAy8T-zpDAa0RFJO1U-Sgk",{"id":61260,"title":61261,"body":61262,"description":62432,"extension":2075,"meta":62433,"navigation":69,"path":62434,"seo":62435,"stem":62436,"__hash__":62437},"content\u002Fexamples\u002Fpython-read-and-write-text-file-example.md","Python Read and Write Text File Example",{"type":7,"value":61263,"toc":62402},[61264,61267,61270,61273,61275,61426,61429,61431,61461,61469,61473,61476,61493,61496,61499,61519,61521,61546,61549,61553,61556,61643,61646,61671,61676,61680,61683,61742,61745,61769,61774,61776,61779,61785,61788,61791,61802,61806,61810,61881,61887,61891,61897,61903,61907,61952,61955,61962,61967,61974,61989,61992,61994,61997,62017,62020,62042,62049,62051,62066,62090,62123,62173,62176,62192,62196,62199,62227,62230,62298,62304,62306,62314,62319,62326,62331,62340,62345,62349,62357,62361,62369,62371,62399],[10,61265,61261],{"id":61266},"python-read-and-write-text-file-example",[14,61268,61269],{},"This example shows a small Python script that writes text to a file and then reads it back.",[14,61271,61272],{},"It is a practical beginner example. The goal is simple: create or overwrite a text file, add some text, read the file, and print the result.",[37,61274,40],{"id":39},[42,61276,61278],{"className":44,"code":61277,"language":46,"meta":47,"style":47},"file_name = \"notes.txt\"\n\nwith open(file_name, \"w\") as file:\n    file.write(\"Hello, Python!\\n\")\n    file.write(\"This is a text file example.\\n\")\n\nwith open(file_name, \"r\") as file:\n    content = file.read()\n\nprint(content)\n",[49,61279,61280,61293,61297,61324,61345,61366,61370,61396,61412,61416],{"__ignoreMap":47},[52,61281,61282,61285,61287,61289,61291],{"class":54,"line":55},[52,61283,61284],{"class":62},"file_name ",[52,61286,80],{"class":79},[52,61288,84],{"class":83},[52,61290,28570],{"class":87},[52,61292,91],{"class":83},[52,61294,61295],{"class":54,"line":66},[52,61296,70],{"emptyLinePlaceholder":69},[52,61298,61299,61301,61303,61305,61308,61310,61312,61314,61316,61318,61320,61322],{"class":54,"line":73},[52,61300,14632],{"class":58},[52,61302,14635],{"class":241},[52,61304,193],{"class":102},[52,61306,61307],{"class":189},"file_name",[52,61309,199],{"class":102},[52,61311,84],{"class":83},[52,61313,16760],{"class":87},[52,61315,115],{"class":83},[52,61317,1521],{"class":102},[52,61319,3567],{"class":58},[52,61321,14676],{"class":14675},[52,61323,1330],{"class":102},[52,61325,61326,61328,61330,61332,61334,61336,61339,61341,61343],{"class":54,"line":94},[52,61327,23844],{"class":14675},[52,61329,186],{"class":102},[52,61331,23849],{"class":189},[52,61333,193],{"class":102},[52,61335,115],{"class":83},[52,61337,61338],{"class":87},"Hello, Python!",[52,61340,10956],{"class":2533},[52,61342,115],{"class":83},[52,61344,211],{"class":102},[52,61346,61347,61349,61351,61353,61355,61357,61360,61362,61364],{"class":54,"line":106},[52,61348,23844],{"class":14675},[52,61350,186],{"class":102},[52,61352,23849],{"class":189},[52,61354,193],{"class":102},[52,61356,115],{"class":83},[52,61358,61359],{"class":87},"This is a text file example.",[52,61361,10956],{"class":2533},[52,61363,115],{"class":83},[52,61365,211],{"class":102},[52,61367,61368],{"class":54,"line":128},[52,61369,70],{"emptyLinePlaceholder":69},[52,61371,61372,61374,61376,61378,61380,61382,61384,61386,61388,61390,61392,61394],{"class":54,"line":148},[52,61373,14632],{"class":58},[52,61375,14635],{"class":241},[52,61377,193],{"class":102},[52,61379,61307],{"class":189},[52,61381,199],{"class":102},[52,61383,84],{"class":83},[52,61385,41783],{"class":87},[52,61387,115],{"class":83},[52,61389,1521],{"class":102},[52,61391,3567],{"class":58},[52,61393,14676],{"class":14675},[52,61395,1330],{"class":102},[52,61397,61398,61401,61403,61405,61407,61410],{"class":54,"line":164},[52,61399,61400],{"class":62},"    content ",[52,61402,80],{"class":79},[52,61404,14676],{"class":14675},[52,61406,186],{"class":102},[52,61408,61409],{"class":189},"read",[52,61411,230],{"class":102},[52,61413,61414],{"class":54,"line":170},[52,61415,70],{"emptyLinePlaceholder":69},[52,61417,61418,61420,61422,61424],{"class":54,"line":175},[52,61419,242],{"class":241},[52,61421,193],{"class":102},[52,61423,23858],{"class":189},[52,61425,211],{"class":102},[14,61427,61428],{},"This creates or overwrites a text file, writes two lines, reads the full file, and prints the result.",[37,61430,2268],{"id":2267},[309,61432,61433,61438,61444,61449,61455],{},[24,61434,61435,61436],{},"How to open a file for writing with mode ",[49,61437,17356],{},[24,61439,61440,61441],{},"How to write text using ",[49,61442,61443],{},"write()",[24,61445,61446,61447],{},"How to open the same file for reading with mode ",[49,61448,46691],{},[24,61450,61451,61452],{},"How to read all file content with ",[49,61453,61454],{},"read()",[24,61456,61457,61458,61460],{},"Why using ",[49,61459,14741],{}," is the beginner-friendly safe pattern",[14,61462,61463,61464,61466,61467,186],{},"If you want a fuller introduction, see ",[372,61465,16513],{"href":14988}," or the ",[372,61468,14985],{"href":14984},[37,61470,61472],{"id":61471},"example-setup","Example setup",[14,61474,61475],{},"In this example, the file name is:",[42,61477,61479],{"className":44,"code":61478,"language":46,"meta":47,"style":47},"file_name = \"notes.txt\"\n",[49,61480,61481],{"__ignoreMap":47},[52,61482,61483,61485,61487,61489,61491],{"class":54,"line":55},[52,61484,61284],{"class":62},[52,61486,80],{"class":79},[52,61488,84],{"class":83},[52,61490,28570],{"class":87},[52,61492,91],{"class":83},[14,61494,61495],{},"This means Python will look for the file in the current folder where your script runs.",[14,61497,61498],{},"A few important points:",[309,61500,61501,61505,61508,61514],{},[24,61502,61503,17357],{},[49,61504,17356],{},[24,61506,61507],{},"If the file does not exist, Python creates it",[24,61509,61510,61511,61513],{},"If the file already exists, ",[49,61512,17356],{}," removes the old content first",[24,61515,61516,61518],{},[49,61517,10956],{}," adds a new line",[14,61520,1119],{},[42,61522,61524],{"className":44,"code":61523,"language":46,"meta":47,"style":47},"file.write(\"Hello, Python!\\n\")\n",[49,61525,61526],{"__ignoreMap":47},[52,61527,61528,61530,61532,61534,61536,61538,61540,61542,61544],{"class":54,"line":55},[52,61529,14698],{"class":14675},[52,61531,186],{"class":102},[52,61533,23849],{"class":189},[52,61535,193],{"class":102},[52,61537,115],{"class":83},[52,61539,61338],{"class":87},[52,61541,10956],{"class":2533},[52,61543,115],{"class":83},[52,61545,211],{"class":102},[14,61547,61548],{},"writes the text and then moves to the next line.",[37,61550,61552],{"id":61551},"write-text-to-the-file","Write text to the file",[14,61554,61555],{},"Here is the writing part by itself:",[42,61557,61559],{"className":44,"code":61558,"language":46,"meta":47,"style":47},"file_name = \"notes.txt\"\n\nwith open(file_name, \"w\") as file:\n    file.write(\"Hello, Python!\\n\")\n    file.write(\"This is a text file example.\\n\")\n",[49,61560,61561,61573,61577,61603,61623],{"__ignoreMap":47},[52,61562,61563,61565,61567,61569,61571],{"class":54,"line":55},[52,61564,61284],{"class":62},[52,61566,80],{"class":79},[52,61568,84],{"class":83},[52,61570,28570],{"class":87},[52,61572,91],{"class":83},[52,61574,61575],{"class":54,"line":66},[52,61576,70],{"emptyLinePlaceholder":69},[52,61578,61579,61581,61583,61585,61587,61589,61591,61593,61595,61597,61599,61601],{"class":54,"line":73},[52,61580,14632],{"class":58},[52,61582,14635],{"class":241},[52,61584,193],{"class":102},[52,61586,61307],{"class":189},[52,61588,199],{"class":102},[52,61590,84],{"class":83},[52,61592,16760],{"class":87},[52,61594,115],{"class":83},[52,61596,1521],{"class":102},[52,61598,3567],{"class":58},[52,61600,14676],{"class":14675},[52,61602,1330],{"class":102},[52,61604,61605,61607,61609,61611,61613,61615,61617,61619,61621],{"class":54,"line":94},[52,61606,23844],{"class":14675},[52,61608,186],{"class":102},[52,61610,23849],{"class":189},[52,61612,193],{"class":102},[52,61614,115],{"class":83},[52,61616,61338],{"class":87},[52,61618,10956],{"class":2533},[52,61620,115],{"class":83},[52,61622,211],{"class":102},[52,61624,61625,61627,61629,61631,61633,61635,61637,61639,61641],{"class":54,"line":106},[52,61626,23844],{"class":14675},[52,61628,186],{"class":102},[52,61630,23849],{"class":189},[52,61632,193],{"class":102},[52,61634,115],{"class":83},[52,61636,61359],{"class":87},[52,61638,10956],{"class":2533},[52,61640,115],{"class":83},[52,61642,211],{"class":102},[14,61644,61645],{},"What to notice:",[309,61647,61648,61654,61659,61664],{},[24,61649,61650,61653],{},[49,61651,61652],{},"open(file_name, \"w\")"," opens the file in write mode",[24,61655,61656,61658],{},[49,61657,14632],{}," automatically closes the file when the block ends",[24,61660,61661,61663],{},[49,61662,61443],{}," writes exactly the string you give it",[24,61665,61666,39778,61668,61670],{},[49,61667,61443],{},[5503,61669,20858],{}," add a new line by itself",[14,61672,61673,61674,186],{},"If you want to learn more about writing files, see ",[372,61675,29634],{"href":29633},[37,61677,61679],{"id":61678},"read-text-from-the-file","Read text from the file",[14,61681,61682],{},"Here is the reading part:",[42,61684,61686],{"className":44,"code":61685,"language":46,"meta":47,"style":47},"with open(file_name, \"r\") as file:\n    content = file.read()\n\nprint(content)\n",[49,61687,61688,61714,61728,61732],{"__ignoreMap":47},[52,61689,61690,61692,61694,61696,61698,61700,61702,61704,61706,61708,61710,61712],{"class":54,"line":55},[52,61691,14632],{"class":58},[52,61693,14635],{"class":241},[52,61695,193],{"class":102},[52,61697,61307],{"class":189},[52,61699,199],{"class":102},[52,61701,84],{"class":83},[52,61703,41783],{"class":87},[52,61705,115],{"class":83},[52,61707,1521],{"class":102},[52,61709,3567],{"class":58},[52,61711,14676],{"class":14675},[52,61713,1330],{"class":102},[52,61715,61716,61718,61720,61722,61724,61726],{"class":54,"line":66},[52,61717,61400],{"class":62},[52,61719,80],{"class":79},[52,61721,14676],{"class":14675},[52,61723,186],{"class":102},[52,61725,61409],{"class":189},[52,61727,230],{"class":102},[52,61729,61730],{"class":54,"line":73},[52,61731,70],{"emptyLinePlaceholder":69},[52,61733,61734,61736,61738,61740],{"class":54,"line":94},[52,61735,242],{"class":241},[52,61737,193],{"class":102},[52,61739,23858],{"class":189},[52,61741,211],{"class":102},[14,61743,61744],{},"What this does:",[309,61746,61747,61753,61759,61763],{},[24,61748,61749,61752],{},[49,61750,61751],{},"open(file_name, \"r\")"," opens the file in read mode",[24,61754,61755,61758],{},[49,61756,61757],{},"file.read()"," reads the whole file as one string",[24,61760,57857,61761],{},[49,61762,23858],{},[24,61764,61765,61768],{},[49,61766,61767],{},"print(content)"," shows the text in the terminal",[14,61770,61771,61772,186],{},"If you want more reading examples, see ",[372,61773,29867],{"href":29866},[37,61775,13563],{"id":13562},[14,61777,61778],{},"The script prints:",[42,61780,61783],{"className":61781,"code":61782,"language":955,"meta":47},[953],"Hello, Python!\nThis is a text file example.\n",[49,61784,61782],{"__ignoreMap":47},[14,61786,61787],{},"This matches the two lines written earlier.",[14,61789,61790],{},"That is the main idea of this example:",[21,61792,61793,61796,61799],{},[24,61794,61795],{},"Write text into the file",[24,61797,61798],{},"Read the same text back",[24,61800,61801],{},"Print it so you can see the result",[37,61803,61805],{"id":61804},"how-this-example-works-step-by-step","How this example works step by step",[639,61807,61809],{"id":61808},"_1-the-first-block-opens-the-file-and-writes-text","1. The first block opens the file and writes text",[42,61811,61813],{"className":44,"code":61812,"language":46,"meta":47,"style":47},"with open(file_name, \"w\") as file:\n    file.write(\"Hello, Python!\\n\")\n    file.write(\"This is a text file example.\\n\")\n",[49,61814,61815,61841,61861],{"__ignoreMap":47},[52,61816,61817,61819,61821,61823,61825,61827,61829,61831,61833,61835,61837,61839],{"class":54,"line":55},[52,61818,14632],{"class":58},[52,61820,14635],{"class":241},[52,61822,193],{"class":102},[52,61824,61307],{"class":189},[52,61826,199],{"class":102},[52,61828,84],{"class":83},[52,61830,16760],{"class":87},[52,61832,115],{"class":83},[52,61834,1521],{"class":102},[52,61836,3567],{"class":58},[52,61838,14676],{"class":14675},[52,61840,1330],{"class":102},[52,61842,61843,61845,61847,61849,61851,61853,61855,61857,61859],{"class":54,"line":66},[52,61844,23844],{"class":14675},[52,61846,186],{"class":102},[52,61848,23849],{"class":189},[52,61850,193],{"class":102},[52,61852,115],{"class":83},[52,61854,61338],{"class":87},[52,61856,10956],{"class":2533},[52,61858,115],{"class":83},[52,61860,211],{"class":102},[52,61862,61863,61865,61867,61869,61871,61873,61875,61877,61879],{"class":54,"line":73},[52,61864,23844],{"class":14675},[52,61866,186],{"class":102},[52,61868,23849],{"class":189},[52,61870,193],{"class":102},[52,61872,115],{"class":83},[52,61874,61359],{"class":87},[52,61876,10956],{"class":2533},[52,61878,115],{"class":83},[52,61880,211],{"class":102},[14,61882,61883,61884,61886],{},"This creates ",[49,61885,28570],{}," if needed and writes two lines into it.",[639,61888,61890],{"id":61889},"_2-the-file-is-closed-automatically","2. The file is closed automatically",[14,61892,61893,61894,61896],{},"When the ",[49,61895,14632],{}," block ends, Python closes the file for you.",[14,61898,61899,61900,61902],{},"This is why ",[49,61901,14741],{}," is the safest beginner pattern.",[639,61904,61906],{"id":61905},"_3-the-second-block-opens-the-file-again-for-reading","3. The second block opens the file again for reading",[42,61908,61910],{"className":44,"code":61909,"language":46,"meta":47,"style":47},"with open(file_name, \"r\") as file:\n    content = file.read()\n",[49,61911,61912,61938],{"__ignoreMap":47},[52,61913,61914,61916,61918,61920,61922,61924,61926,61928,61930,61932,61934,61936],{"class":54,"line":55},[52,61915,14632],{"class":58},[52,61917,14635],{"class":241},[52,61919,193],{"class":102},[52,61921,61307],{"class":189},[52,61923,199],{"class":102},[52,61925,84],{"class":83},[52,61927,41783],{"class":87},[52,61929,115],{"class":83},[52,61931,1521],{"class":102},[52,61933,3567],{"class":58},[52,61935,14676],{"class":14675},[52,61937,1330],{"class":102},[52,61939,61940,61942,61944,61946,61948,61950],{"class":54,"line":66},[52,61941,61400],{"class":62},[52,61943,80],{"class":79},[52,61945,14676],{"class":14675},[52,61947,186],{"class":102},[52,61949,61409],{"class":189},[52,61951,230],{"class":102},[14,61953,61954],{},"Now Python opens the same file in read mode.",[639,61956,61958,61959,61961],{"id":61957},"_4-read-returns-the-file-content-as-a-string","4. ",[49,61960,61454],{}," returns the file content as a string",[14,61963,61964,61965,186],{},"The full contents of the file are stored in ",[49,61966,23858],{},[639,61968,61970,61971,61973],{"id":61969},"_5-print-displays-that-string","5. ",[49,61972,9687],{}," displays that string",[42,61975,61977],{"className":44,"code":61976,"language":46,"meta":47,"style":47},"print(content)\n",[49,61978,61979],{"__ignoreMap":47},[52,61980,61981,61983,61985,61987],{"class":54,"line":55},[52,61982,242],{"class":241},[52,61984,193],{"class":102},[52,61986,23858],{"class":189},[52,61988,211],{"class":102},[14,61990,61991],{},"This prints the file text to the terminal or console.",[37,61993,6402],{"id":6401},[14,61995,61996],{},"Here are some common problems with this kind of script:",[309,61998,61999,62004,62009,62012,62014],{},[24,62000,10294,62001,62003],{},[49,62002,17356],{}," when you wanted to keep old file content",[24,62005,8118,62006,62008],{},[49,62007,10956],{}," and expecting separate lines",[24,62010,62011],{},"Trying to read before the file was written",[24,62013,29662],{},[24,62015,62016],{},"Misspelling the file name",[14,62018,62019],{},"Also watch out for these common causes:",[309,62021,62022,62028,62031,62034,62037],{},[24,62023,62024,62025,62027],{},"Using write mode ",[49,62026,17356],{}," and accidentally overwriting existing content",[24,62029,62030],{},"Reading a file from a different folder than expected",[24,62032,62033],{},"Forgetting newline characters when writing multiple lines",[24,62035,62036],{},"Confusing text file reading with CSV or JSON reading",[24,62038,62039,62040],{},"Trying to open a file that does not exist when using ",[49,62041,46691],{},[14,62043,62044,62045,186],{},"If Python says the file cannot be found, see ",[372,62046,3984,62047],{"href":15903},[49,62048,25235],{},[14,62050,8866],{},[42,62052,62054],{"className":44,"code":62053,"language":46,"meta":47,"style":47},"print(file_name)\n",[49,62055,62056],{"__ignoreMap":47},[52,62057,62058,62060,62062,62064],{"class":54,"line":55},[52,62059,242],{"class":241},[52,62061,193],{"class":102},[52,62063,61307],{"class":189},[52,62065,211],{"class":102},[42,62067,62068],{"className":44,"code":15830,"language":46,"meta":47,"style":47},[49,62069,62070,62076],{"__ignoreMap":47},[52,62071,62072,62074],{"class":54,"line":55},[52,62073,59],{"class":58},[52,62075,15839],{"class":62},[52,62077,62078,62080,62082,62084,62086,62088],{"class":54,"line":66},[52,62079,242],{"class":241},[52,62081,193],{"class":102},[52,62083,15848],{"class":189},[52,62085,186],{"class":102},[52,62087,15853],{"class":189},[52,62089,1816],{"class":102},[42,62091,62093],{"className":44,"code":62092,"language":46,"meta":47,"style":47},"import os\nprint(os.path.exists(file_name))\n",[49,62094,62095,62101],{"__ignoreMap":47},[52,62096,62097,62099],{"class":54,"line":55},[52,62098,59],{"class":58},[52,62100,15839],{"class":62},[52,62102,62103,62105,62107,62109,62111,62113,62115,62117,62119,62121],{"class":54,"line":66},[52,62104,242],{"class":241},[52,62106,193],{"class":102},[52,62108,15848],{"class":189},[52,62110,186],{"class":102},[52,62112,15882],{"class":1320},[52,62114,186],{"class":102},[52,62116,15887],{"class":189},[52,62118,193],{"class":102},[52,62120,61307],{"class":189},[52,62122,8886],{"class":102},[42,62124,62126],{"className":44,"code":62125,"language":46,"meta":47,"style":47},"with open(file_name, 'r') as file:\n    print(repr(file.read()))\n",[49,62127,62128,62154],{"__ignoreMap":47},[52,62129,62130,62132,62134,62136,62138,62140,62142,62144,62146,62148,62150,62152],{"class":54,"line":55},[52,62131,14632],{"class":58},[52,62133,14635],{"class":241},[52,62135,193],{"class":102},[52,62137,61307],{"class":189},[52,62139,199],{"class":102},[52,62141,4172],{"class":83},[52,62143,41783],{"class":87},[52,62145,4162],{"class":83},[52,62147,1521],{"class":102},[52,62149,3567],{"class":58},[52,62151,14676],{"class":14675},[52,62153,1330],{"class":102},[52,62155,62156,62158,62160,62162,62164,62166,62168,62170],{"class":54,"line":66},[52,62157,1599],{"class":241},[52,62159,193],{"class":102},[52,62161,20689],{"class":241},[52,62163,193],{"class":102},[52,62165,14698],{"class":14675},[52,62167,186],{"class":102},[52,62169,61409],{"class":189},[52,62171,62172],{"class":102},"()))\n",[14,62174,62175],{},"These help you check:",[309,62177,62178,62181,62184,62187],{},[24,62179,62180],{},"the file name you are using",[24,62182,62183],{},"the current working folder",[24,62185,62186],{},"whether the file actually exists",[24,62188,62189,62190],{},"the exact text in the file, including ",[49,62191,10956],{},[37,62193,62195],{"id":62194},"try-a-small-variation","Try a small variation",[14,62197,62198],{},"Once this example works, try changing one small part at a time:",[309,62200,62201,62203,62206,62218],{},[24,62202,17741],{},[24,62204,62205],{},"Write three lines instead of two",[24,62207,62208,62209,62211,62212,857,62215],{},"Replace ",[49,62210,61454],{}," with ",[49,62213,62214],{},"readline()",[49,62216,62217],{},"readlines()",[24,62219,62220,62221,37261,62223,62226],{},"Switch from ",[49,62222,17356],{},[49,62224,62225],{},"\"a\""," and see what changes",[14,62228,62229],{},"For example, append mode keeps the old content instead of replacing it:",[42,62231,62233],{"className":44,"code":62232,"language":46,"meta":47,"style":47},"file_name = \"notes.txt\"\n\nwith open(file_name, \"a\") as file:\n    file.write(\"This line was added later.\\n\")\n",[49,62234,62235,62247,62251,62277],{"__ignoreMap":47},[52,62236,62237,62239,62241,62243,62245],{"class":54,"line":55},[52,62238,61284],{"class":62},[52,62240,80],{"class":79},[52,62242,84],{"class":83},[52,62244,28570],{"class":87},[52,62246,91],{"class":83},[52,62248,62249],{"class":54,"line":66},[52,62250,70],{"emptyLinePlaceholder":69},[52,62252,62253,62255,62257,62259,62261,62263,62265,62267,62269,62271,62273,62275],{"class":54,"line":73},[52,62254,14632],{"class":58},[52,62256,14635],{"class":241},[52,62258,193],{"class":102},[52,62260,61307],{"class":189},[52,62262,199],{"class":102},[52,62264,84],{"class":83},[52,62266,372],{"class":87},[52,62268,115],{"class":83},[52,62270,1521],{"class":102},[52,62272,3567],{"class":58},[52,62274,14676],{"class":14675},[52,62276,1330],{"class":102},[52,62278,62279,62281,62283,62285,62287,62289,62292,62294,62296],{"class":54,"line":94},[52,62280,23844],{"class":14675},[52,62282,186],{"class":102},[52,62284,23849],{"class":189},[52,62286,193],{"class":102},[52,62288,115],{"class":83},[52,62290,62291],{"class":87},"This line was added later.",[52,62293,10956],{"class":2533},[52,62295,115],{"class":83},[52,62297,211],{"class":102},[14,62299,62300,62301,186],{},"If you want to practice that next, see ",[372,62302,62303],{"href":17931},"how to append to a file in Python",[37,62305,1942],{"id":1941},[639,62307,3667,62309,62311,62312,1995],{"id":62308},"what-does-w-mean-in-open",[49,62310,17356],{}," mean in ",[49,62313,14980],{},[14,62315,62316,62318],{},[49,62317,17356],{}," means write mode. It creates a new file if needed and overwrites old content if the file already exists.",[639,62320,3667,62322,62311,62324,1995],{"id":62321},"what-does-r-mean-in-open",[49,62323,46691],{},[49,62325,14980],{},[14,62327,62328,62330],{},[49,62329,46691],{}," means read mode. It opens an existing file so you can read its contents.",[639,62332,1956,62334,10598,62337,62339],{"id":62333},"why-use-with-open-instead-of-open-by-itself",[49,62335,62336],{},"with open()",[49,62338,14980],{}," by itself?",[14,62341,62342,62344],{},[49,62343,62336],{}," closes the file automatically, which is safer and easier for beginners.",[639,62346,62348],{"id":62347},"how-do-i-keep-existing-text-instead-of-replacing-it","How do I keep existing text instead of replacing it?",[14,62350,62351,62352,62354,62355,186],{},"Use append mode ",[49,62353,62225],{}," instead of write mode ",[49,62356,17356],{},[639,62358,62360],{"id":62359},"why-is-my-text-all-on-one-line","Why is my text all on one line?",[14,62362,62363,62365,62366,62368],{},[49,62364,61443],{}," does not add line breaks automatically. Add ",[49,62367,10956],{}," where you want a new line.",[37,62370,2005],{"id":2004},[309,62372,62373,62377,62381,62385,62389,62393],{},[24,62374,62375],{},[372,62376,29921],{"href":29866},[24,62378,62379],{},[372,62380,29926],{"href":29633},[24,62382,62383],{},[372,62384,17932],{"href":17931},[24,62386,62387],{},[372,62388,14985],{"href":14984},[24,62390,62391],{},[372,62392,16513],{"href":14988},[24,62394,62395],{},[372,62396,4990,62397],{"href":15903},[49,62398,25235],{},[2034,62400,62401],{},"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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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}",{"title":47,"searchDepth":66,"depth":66,"links":62403},[62404,62405,62406,62407,62408,62409,62410,62419,62420,62421,62431],{"id":39,"depth":66,"text":40},{"id":2267,"depth":66,"text":2268},{"id":61471,"depth":66,"text":61472},{"id":61551,"depth":66,"text":61552},{"id":61678,"depth":66,"text":61679},{"id":13562,"depth":66,"text":13563},{"id":61804,"depth":66,"text":61805,"children":62411},[62412,62413,62414,62415,62417],{"id":61808,"depth":73,"text":61809},{"id":61889,"depth":73,"text":61890},{"id":61905,"depth":73,"text":61906},{"id":61957,"depth":73,"text":62416},"4. read() returns the file content as a string",{"id":61969,"depth":73,"text":62418},"5. print() displays that string",{"id":6401,"depth":66,"text":6402},{"id":62194,"depth":66,"text":62195},{"id":1941,"depth":66,"text":1942,"children":62422},[62423,62425,62427,62429,62430],{"id":62308,"depth":73,"text":62424},"What does \"w\" mean in open()?",{"id":62321,"depth":73,"text":62426},"What does \"r\" mean in open()?",{"id":62333,"depth":73,"text":62428},"Why use with open() instead of open() by itself?",{"id":62347,"depth":73,"text":62348},{"id":62359,"depth":73,"text":62360},{"id":2004,"depth":66,"text":2005},"Master python read and write text file example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-read-and-write-text-file-example",{"title":61261,"description":62432},"examples\u002Fpython-read-and-write-text-file-example","HZk6PryjTKtz2CQuV-T9mAn12ptaoaP5OgD_IoWkeuo",{"id":62439,"title":62440,"body":62441,"description":64185,"extension":2075,"meta":64186,"navigation":69,"path":20811,"seo":64187,"stem":64188,"__hash__":64189},"content\u002Fexamples\u002Fpython-remove-duplicates-from-data-example.md","Python Remove Duplicates from Data Example",{"type":7,"value":62442,"toc":64149},[62443,62446,62449,62452,62463,62467,62560,62566,62570,62573,62584,62591,62595,62598,62684,62686,62719,62721,62739,62741,62747,62758,62761,62850,62854,62859,62952,62954,62987,62989,62995,63002,63006,63017,63021,63025,63028,63149,63151,63183,63185,63205,63208,63211,63378,63380,63413,63417,63423,63434,63438,63449,63452,63463,63467,63474,63484,63487,63506,63509,63610,63612,63637,63644,63646,63648,63654,63657,63721,63724,63751,63757,63760,63813,63816,63824,63827,63878,63880,63905,63908,63973,63979,63982,64043,64046,64049,64072,64074,64078,64088,64095,64098,64102,64108,64114,64117,64119,64146],[10,62444,62440],{"id":62445},"python-remove-duplicates-from-data-example",[14,62447,62448],{},"This beginner-friendly example shows simple ways to remove duplicate values from Python data.",[14,62450,62451],{},"The focus is on lists, because that is where this problem appears most often. You will see:",[309,62453,62454,62457,62460],{},[24,62455,62456],{},"how to remove repeated items quickly",[24,62458,62459],{},"how to keep the original order when needed",[24,62461,62462],{},"how to do the same job step by step with a loop",[37,62464,62466],{"id":62465},"quick-answer","Quick answer",[42,62468,62470],{"className":44,"code":62469,"language":46,"meta":47,"style":47},"data = [\"apple\", \"banana\", \"apple\", \"orange\", \"banana\"]\nunique_data = list(dict.fromkeys(data))\nprint(unique_data)\n# ['apple', 'banana', 'orange']\n",[49,62471,62472,62520,62544,62555],{"__ignoreMap":47},[52,62473,62474,62476,62478,62480,62482,62484,62486,62488,62490,62492,62494,62496,62498,62500,62502,62504,62506,62508,62510,62512,62514,62516,62518],{"class":54,"line":55},[52,62475,217],{"class":62},[52,62477,80],{"class":79},[52,62479,18162],{"class":102},[52,62481,115],{"class":83},[52,62483,33028],{"class":87},[52,62485,115],{"class":83},[52,62487,199],{"class":102},[52,62489,84],{"class":83},[52,62491,32978],{"class":87},[52,62493,115],{"class":83},[52,62495,199],{"class":102},[52,62497,84],{"class":83},[52,62499,33028],{"class":87},[52,62501,115],{"class":83},[52,62503,199],{"class":102},[52,62505,84],{"class":83},[52,62507,32996],{"class":87},[52,62509,115],{"class":83},[52,62511,199],{"class":102},[52,62513,84],{"class":83},[52,62515,32978],{"class":87},[52,62517,115],{"class":83},[52,62519,1147],{"class":102},[52,62521,62522,62525,62527,62529,62531,62533,62535,62538,62540,62542],{"class":54,"line":66},[52,62523,62524],{"class":62},"unique_data ",[52,62526,80],{"class":79},[52,62528,41215],{"class":4733},[52,62530,193],{"class":102},[52,62532,40992],{"class":4733},[52,62534,186],{"class":102},[52,62536,62537],{"class":189},"fromkeys",[52,62539,193],{"class":102},[52,62541,247],{"class":189},[52,62543,8886],{"class":102},[52,62545,62546,62548,62550,62553],{"class":54,"line":73},[52,62547,242],{"class":241},[52,62549,193],{"class":102},[52,62551,62552],{"class":189},"unique_data",[52,62554,211],{"class":102},[52,62556,62557],{"class":54,"line":94},[52,62558,62559],{"class":4368},"# ['apple', 'banana', 'orange']\n",[14,62561,3650,62562,62565],{},[49,62563,62564],{},"dict.fromkeys()"," when you want to remove duplicates and keep the original order.",[37,62567,62569],{"id":62568},"what-this-example-solves","What this example solves",[14,62571,62572],{},"This example is useful when you need to:",[309,62574,62575,62578,62581],{},[24,62576,62577],{},"remove repeated items from a list",[24,62579,62580],{},"understand the difference between removing duplicates with and without keeping order",[24,62582,62583],{},"learn a practical pattern without reading a full theory lesson",[14,62585,62586,62587,186],{},"If you want a task-focused guide, see ",[372,62588,62590],{"href":62589},"\u002Fhow-to\u002Fhow-to-remove-duplicates-from-a-list-in-python\u002F","how to remove duplicates from a list in Python",[37,62592,62594],{"id":62593},"example-1-remove-duplicates-with-a-set","Example 1: Remove duplicates with a set",[14,62596,62597],{},"The shortest way to remove duplicates is to turn the list into a set.",[42,62599,62601],{"className":44,"code":62600,"language":46,"meta":47,"style":47},"data = [\"apple\", \"banana\", \"apple\", \"orange\", \"banana\"]\n\nunique_data = set(data)\n\nprint(unique_data)\n",[49,62602,62603,62651,62655,62670,62674],{"__ignoreMap":47},[52,62604,62605,62607,62609,62611,62613,62615,62617,62619,62621,62623,62625,62627,62629,62631,62633,62635,62637,62639,62641,62643,62645,62647,62649],{"class":54,"line":55},[52,62606,217],{"class":62},[52,62608,80],{"class":79},[52,62610,18162],{"class":102},[52,62612,115],{"class":83},[52,62614,33028],{"class":87},[52,62616,115],{"class":83},[52,62618,199],{"class":102},[52,62620,84],{"class":83},[52,62622,32978],{"class":87},[52,62624,115],{"class":83},[52,62626,199],{"class":102},[52,62628,84],{"class":83},[52,62630,33028],{"class":87},[52,62632,115],{"class":83},[52,62634,199],{"class":102},[52,62636,84],{"class":83},[52,62638,32996],{"class":87},[52,62640,115],{"class":83},[52,62642,199],{"class":102},[52,62644,84],{"class":83},[52,62646,32978],{"class":87},[52,62648,115],{"class":83},[52,62650,1147],{"class":102},[52,62652,62653],{"class":54,"line":66},[52,62654,70],{"emptyLinePlaceholder":69},[52,62656,62657,62659,62661,62664,62666,62668],{"class":54,"line":73},[52,62658,62524],{"class":62},[52,62660,80],{"class":79},[52,62662,62663],{"class":4733}," set",[52,62665,193],{"class":102},[52,62667,247],{"class":189},[52,62669,211],{"class":102},[52,62671,62672],{"class":54,"line":94},[52,62673,70],{"emptyLinePlaceholder":69},[52,62675,62676,62678,62680,62682],{"class":54,"line":106},[52,62677,242],{"class":241},[52,62679,193],{"class":102},[52,62681,62552],{"class":189},[52,62683,211],{"class":102},[14,62685,40163],{},[42,62687,62689],{"className":44,"code":62688,"language":46,"meta":47,"style":47},"{'banana', 'orange', 'apple'}\n",[49,62690,62691],{"__ignoreMap":47},[52,62692,62693,62695,62697,62699,62701,62703,62705,62707,62709,62711,62713,62715,62717],{"class":54,"line":55},[52,62694,1482],{"class":102},[52,62696,4162],{"class":83},[52,62698,32978],{"class":87},[52,62700,4162],{"class":83},[52,62702,199],{"class":102},[52,62704,4172],{"class":83},[52,62706,32996],{"class":87},[52,62708,4162],{"class":83},[52,62710,199],{"class":102},[52,62712,4172],{"class":83},[52,62714,33028],{"class":87},[52,62716,4162],{"class":83},[52,62718,167],{"class":102},[639,62720,36906],{"id":36905},[309,62722,62723,62729,62732],{},[24,62724,62725,62728],{},[49,62726,62727],{},"set(data)"," removes duplicate values",[24,62730,62731],{},"each value appears only once in the result",[24,62733,62734,62735,62738],{},"the result is a ",[49,62736,62737],{},"set",", not a list",[639,62740,54520],{"id":54519},[14,62742,62743,62744,62746],{},"A set does ",[5503,62745,20858],{}," keep the original order of the list. That means this method is best when order does not matter.",[14,62748,62749,62750,857,62754,186],{},"If you want to learn more about sets, read ",[372,62751,62753],{"href":62752},"\u002Flearn\u002Fpython-sets-explained\u002F","Python sets explained",[372,62755,62757],{"href":62756},"\u002Freference\u002Fpython-set-creating-a-set\u002F","Python set: creating a set",[14,62759,62760],{},"If you need the result as a list, convert it back:",[42,62762,62764],{"className":44,"code":62763,"language":46,"meta":47,"style":47},"data = [\"apple\", \"banana\", \"apple\", \"orange\", \"banana\"]\n\nunique_data = list(set(data))\n\nprint(unique_data)\n",[49,62765,62766,62814,62818,62836,62840],{"__ignoreMap":47},[52,62767,62768,62770,62772,62774,62776,62778,62780,62782,62784,62786,62788,62790,62792,62794,62796,62798,62800,62802,62804,62806,62808,62810,62812],{"class":54,"line":55},[52,62769,217],{"class":62},[52,62771,80],{"class":79},[52,62773,18162],{"class":102},[52,62775,115],{"class":83},[52,62777,33028],{"class":87},[52,62779,115],{"class":83},[52,62781,199],{"class":102},[52,62783,84],{"class":83},[52,62785,32978],{"class":87},[52,62787,115],{"class":83},[52,62789,199],{"class":102},[52,62791,84],{"class":83},[52,62793,33028],{"class":87},[52,62795,115],{"class":83},[52,62797,199],{"class":102},[52,62799,84],{"class":83},[52,62801,32996],{"class":87},[52,62803,115],{"class":83},[52,62805,199],{"class":102},[52,62807,84],{"class":83},[52,62809,32978],{"class":87},[52,62811,115],{"class":83},[52,62813,1147],{"class":102},[52,62815,62816],{"class":54,"line":66},[52,62817,70],{"emptyLinePlaceholder":69},[52,62819,62820,62822,62824,62826,62828,62830,62832,62834],{"class":54,"line":73},[52,62821,62524],{"class":62},[52,62823,80],{"class":79},[52,62825,41215],{"class":4733},[52,62827,193],{"class":102},[52,62829,62737],{"class":4733},[52,62831,193],{"class":102},[52,62833,247],{"class":189},[52,62835,8886],{"class":102},[52,62837,62838],{"class":54,"line":94},[52,62839,70],{"emptyLinePlaceholder":69},[52,62841,62842,62844,62846,62848],{"class":54,"line":106},[52,62843,242],{"class":241},[52,62845,193],{"class":102},[52,62847,62552],{"class":189},[52,62849,211],{"class":102},[37,62851,62853],{"id":62852},"example-2-remove-duplicates-and-keep-order","Example 2: Remove duplicates and keep order",[14,62855,62856,62857,186],{},"If you want to keep the first appearance of each item, use ",[49,62858,62564],{},[42,62860,62862],{"className":44,"code":62861,"language":46,"meta":47,"style":47},"data = [\"apple\", \"banana\", \"apple\", \"orange\", \"banana\"]\n\nunique_data = list(dict.fromkeys(data))\n\nprint(unique_data)\n",[49,62863,62864,62912,62916,62938,62942],{"__ignoreMap":47},[52,62865,62866,62868,62870,62872,62874,62876,62878,62880,62882,62884,62886,62888,62890,62892,62894,62896,62898,62900,62902,62904,62906,62908,62910],{"class":54,"line":55},[52,62867,217],{"class":62},[52,62869,80],{"class":79},[52,62871,18162],{"class":102},[52,62873,115],{"class":83},[52,62875,33028],{"class":87},[52,62877,115],{"class":83},[52,62879,199],{"class":102},[52,62881,84],{"class":83},[52,62883,32978],{"class":87},[52,62885,115],{"class":83},[52,62887,199],{"class":102},[52,62889,84],{"class":83},[52,62891,33028],{"class":87},[52,62893,115],{"class":83},[52,62895,199],{"class":102},[52,62897,84],{"class":83},[52,62899,32996],{"class":87},[52,62901,115],{"class":83},[52,62903,199],{"class":102},[52,62905,84],{"class":83},[52,62907,32978],{"class":87},[52,62909,115],{"class":83},[52,62911,1147],{"class":102},[52,62913,62914],{"class":54,"line":66},[52,62915,70],{"emptyLinePlaceholder":69},[52,62917,62918,62920,62922,62924,62926,62928,62930,62932,62934,62936],{"class":54,"line":73},[52,62919,62524],{"class":62},[52,62921,80],{"class":79},[52,62923,41215],{"class":4733},[52,62925,193],{"class":102},[52,62927,40992],{"class":4733},[52,62929,186],{"class":102},[52,62931,62537],{"class":189},[52,62933,193],{"class":102},[52,62935,247],{"class":189},[52,62937,8886],{"class":102},[52,62939,62940],{"class":54,"line":94},[52,62941,70],{"emptyLinePlaceholder":69},[52,62943,62944,62946,62948,62950],{"class":54,"line":106},[52,62945,242],{"class":241},[52,62947,193],{"class":102},[52,62949,62552],{"class":189},[52,62951,211],{"class":102},[14,62953,5267],{},[42,62955,62957],{"className":44,"code":62956,"language":46,"meta":47,"style":47},"['apple', 'banana', 'orange']\n",[49,62958,62959],{"__ignoreMap":47},[52,62960,62961,62963,62965,62967,62969,62971,62973,62975,62977,62979,62981,62983,62985],{"class":54,"line":55},[52,62962,250],{"class":102},[52,62964,4162],{"class":83},[52,62966,33028],{"class":87},[52,62968,4162],{"class":83},[52,62970,199],{"class":102},[52,62972,4172],{"class":83},[52,62974,32978],{"class":87},[52,62976,4162],{"class":83},[52,62978,199],{"class":102},[52,62980,4172],{"class":83},[52,62982,32996],{"class":87},[52,62984,4162],{"class":83},[52,62986,1147],{"class":102},[639,62988,32449],{"id":32448},[14,62990,62991,62994],{},[49,62992,62993],{},"dict.fromkeys(data)"," creates a dictionary using each list item as a key.",[14,62996,62997,62998,63001],{},"Dictionary keys must be unique, so duplicates are removed automatically. Then ",[49,62999,63000],{},"list(...)"," turns those keys back into a list.",[639,63003,63005],{"id":63004},"why-beginners-like-this-method","Why beginners like this method",[309,63007,63008,63011,63014],{},[24,63009,63010],{},"it is short",[24,63012,63013],{},"it keeps order",[24,63015,63016],{},"it works well for common cleanup tasks",[14,63018,46540,63019,186],{},[372,63020,10883],{"href":10882},[37,63022,63024],{"id":63023},"example-3-build-a-unique-list-with-a-loop","Example 3: Build a unique list with a loop",[14,63026,63027],{},"A loop is longer, but it helps you understand the logic step by step.",[42,63029,63031],{"className":44,"code":63030,"language":46,"meta":47,"style":47},"data = [\"apple\", \"banana\", \"apple\", \"orange\", \"banana\"]\nunique_data = []\n\nfor item in data:\n    if item not in unique_data:\n        unique_data.append(item)\n\nprint(unique_data)\n",[49,63032,63033,63081,63089,63093,63105,63120,63135,63139],{"__ignoreMap":47},[52,63034,63035,63037,63039,63041,63043,63045,63047,63049,63051,63053,63055,63057,63059,63061,63063,63065,63067,63069,63071,63073,63075,63077,63079],{"class":54,"line":55},[52,63036,217],{"class":62},[52,63038,80],{"class":79},[52,63040,18162],{"class":102},[52,63042,115],{"class":83},[52,63044,33028],{"class":87},[52,63046,115],{"class":83},[52,63048,199],{"class":102},[52,63050,84],{"class":83},[52,63052,32978],{"class":87},[52,63054,115],{"class":83},[52,63056,199],{"class":102},[52,63058,84],{"class":83},[52,63060,33028],{"class":87},[52,63062,115],{"class":83},[52,63064,199],{"class":102},[52,63066,84],{"class":83},[52,63068,32996],{"class":87},[52,63070,115],{"class":83},[52,63072,199],{"class":102},[52,63074,84],{"class":83},[52,63076,32978],{"class":87},[52,63078,115],{"class":83},[52,63080,1147],{"class":102},[52,63082,63083,63085,63087],{"class":54,"line":66},[52,63084,62524],{"class":62},[52,63086,80],{"class":79},[52,63088,18590],{"class":102},[52,63090,63091],{"class":54,"line":73},[52,63092,70],{"emptyLinePlaceholder":69},[52,63094,63095,63097,63099,63101,63103],{"class":54,"line":94},[52,63096,12265],{"class":58},[52,63098,30192],{"class":62},[52,63100,11213],{"class":58},[52,63102,1356],{"class":62},[52,63104,1330],{"class":102},[52,63106,63107,63109,63111,63113,63115,63118],{"class":54,"line":106},[52,63108,1438],{"class":58},[52,63110,30192],{"class":62},[52,63112,20858],{"class":79},[52,63114,3101],{"class":79},[52,63116,63117],{"class":62}," unique_data",[52,63119,1330],{"class":102},[52,63121,63122,63125,63127,63129,63131,63133],{"class":54,"line":128},[52,63123,63124],{"class":62},"        unique_data",[52,63126,186],{"class":102},[52,63128,18748],{"class":189},[52,63130,193],{"class":102},[52,63132,30322],{"class":189},[52,63134,211],{"class":102},[52,63136,63137],{"class":54,"line":148},[52,63138,70],{"emptyLinePlaceholder":69},[52,63140,63141,63143,63145,63147],{"class":54,"line":164},[52,63142,242],{"class":241},[52,63144,193],{"class":102},[52,63146,62552],{"class":189},[52,63148,211],{"class":102},[14,63150,5267],{},[42,63152,63153],{"className":44,"code":62956,"language":46,"meta":47,"style":47},[49,63154,63155],{"__ignoreMap":47},[52,63156,63157,63159,63161,63163,63165,63167,63169,63171,63173,63175,63177,63179,63181],{"class":54,"line":55},[52,63158,250],{"class":102},[52,63160,4162],{"class":83},[52,63162,33028],{"class":87},[52,63164,4162],{"class":83},[52,63166,199],{"class":102},[52,63168,4172],{"class":83},[52,63170,32978],{"class":87},[52,63172,4162],{"class":83},[52,63174,199],{"class":102},[52,63176,4172],{"class":83},[52,63178,32996],{"class":87},[52,63180,4162],{"class":83},[52,63182,1147],{"class":102},[639,63184,24095],{"id":24094},[309,63186,63187,63192,63197,63202],{},[24,63188,63189,63190],{},"start with an empty list called ",[49,63191,62552],{},[24,63193,63194,63195],{},"go through each item in ",[49,63196,247],{},[24,63198,63199,63200],{},"check whether the item is already in ",[49,63201,62552],{},[24,63203,63204],{},"if not, add it",[14,63206,63207],{},"This approach is useful when you want custom rules. For example, you might want to ignore case or skip empty values.",[14,63209,63210],{},"Here is a simple case-insensitive version:",[42,63212,63214],{"className":44,"code":63213,"language":46,"meta":47,"style":47},"data = [\"Apple\", \"banana\", \"apple\", \"Orange\", \"BANANA\"]\nunique_data = []\nseen = set()\n\nfor item in data:\n    normalized = item.lower()\n    if normalized not in seen:\n        seen.add(normalized)\n        unique_data.append(item)\n\nprint(unique_data)\n",[49,63215,63216,63267,63275,63286,63290,63302,63317,63333,63350,63364,63368],{"__ignoreMap":47},[52,63217,63218,63220,63222,63224,63226,63229,63231,63233,63235,63237,63239,63241,63243,63245,63247,63249,63251,63254,63256,63258,63260,63263,63265],{"class":54,"line":55},[52,63219,217],{"class":62},[52,63221,80],{"class":79},[52,63223,18162],{"class":102},[52,63225,115],{"class":83},[52,63227,63228],{"class":87},"Apple",[52,63230,115],{"class":83},[52,63232,199],{"class":102},[52,63234,84],{"class":83},[52,63236,32978],{"class":87},[52,63238,115],{"class":83},[52,63240,199],{"class":102},[52,63242,84],{"class":83},[52,63244,33028],{"class":87},[52,63246,115],{"class":83},[52,63248,199],{"class":102},[52,63250,84],{"class":83},[52,63252,63253],{"class":87},"Orange",[52,63255,115],{"class":83},[52,63257,199],{"class":102},[52,63259,84],{"class":83},[52,63261,63262],{"class":87},"BANANA",[52,63264,115],{"class":83},[52,63266,1147],{"class":102},[52,63268,63269,63271,63273],{"class":54,"line":66},[52,63270,62524],{"class":62},[52,63272,80],{"class":79},[52,63274,18590],{"class":102},[52,63276,63277,63280,63282,63284],{"class":54,"line":73},[52,63278,63279],{"class":62},"seen ",[52,63281,80],{"class":79},[52,63283,62663],{"class":4733},[52,63285,230],{"class":102},[52,63287,63288],{"class":54,"line":94},[52,63289,70],{"emptyLinePlaceholder":69},[52,63291,63292,63294,63296,63298,63300],{"class":54,"line":106},[52,63293,12265],{"class":58},[52,63295,30192],{"class":62},[52,63297,11213],{"class":58},[52,63299,1356],{"class":62},[52,63301,1330],{"class":102},[52,63303,63304,63307,63309,63311,63313,63315],{"class":54,"line":128},[52,63305,63306],{"class":62},"    normalized ",[52,63308,80],{"class":79},[52,63310,30211],{"class":62},[52,63312,186],{"class":102},[52,63314,30237],{"class":189},[52,63316,230],{"class":102},[52,63318,63319,63321,63324,63326,63328,63331],{"class":54,"line":148},[52,63320,1438],{"class":58},[52,63322,63323],{"class":62}," normalized ",[52,63325,20858],{"class":79},[52,63327,3101],{"class":79},[52,63329,63330],{"class":62}," seen",[52,63332,1330],{"class":102},[52,63334,63335,63338,63340,63343,63345,63348],{"class":54,"line":164},[52,63336,63337],{"class":62},"        seen",[52,63339,186],{"class":102},[52,63341,63342],{"class":189},"add",[52,63344,193],{"class":102},[52,63346,63347],{"class":189},"normalized",[52,63349,211],{"class":102},[52,63351,63352,63354,63356,63358,63360,63362],{"class":54,"line":170},[52,63353,63124],{"class":62},[52,63355,186],{"class":102},[52,63357,18748],{"class":189},[52,63359,193],{"class":102},[52,63361,30322],{"class":189},[52,63363,211],{"class":102},[52,63365,63366],{"class":54,"line":175},[52,63367,70],{"emptyLinePlaceholder":69},[52,63369,63370,63372,63374,63376],{"class":54,"line":214},[52,63371,242],{"class":241},[52,63373,193],{"class":102},[52,63375,62552],{"class":189},[52,63377,211],{"class":102},[14,63379,5267],{},[42,63381,63383],{"className":44,"code":63382,"language":46,"meta":47,"style":47},"['Apple', 'banana', 'Orange']\n",[49,63384,63385],{"__ignoreMap":47},[52,63386,63387,63389,63391,63393,63395,63397,63399,63401,63403,63405,63407,63409,63411],{"class":54,"line":55},[52,63388,250],{"class":102},[52,63390,4162],{"class":83},[52,63392,63228],{"class":87},[52,63394,4162],{"class":83},[52,63396,199],{"class":102},[52,63398,4172],{"class":83},[52,63400,32978],{"class":87},[52,63402,4162],{"class":83},[52,63404,199],{"class":102},[52,63406,4172],{"class":83},[52,63408,63253],{"class":87},[52,63410,4162],{"class":83},[52,63412,1147],{"class":102},[37,63414,63416],{"id":63415},"when-each-method-is-a-good-choice","When each method is a good choice",[14,63418,3650,63419,63422],{},[49,63420,63421],{},"set()"," when:",[309,63424,63425,63428,63431],{},[24,63426,63427],{},"you want the shortest solution",[24,63429,63430],{},"order does not matter",[24,63432,63433],{},"you only need unique values quickly",[14,63435,3650,63436,63422],{},[49,63437,62564],{},[309,63439,63440,63443,63446],{},[24,63441,63442],{},"you want to preserve the original order",[24,63444,63445],{},"you want a short and readable solution",[24,63447,63448],{},"you are cleaning list data in a beginner script",[14,63450,63451],{},"Use a loop when:",[309,63453,63454,63457,63460],{},[24,63455,63456],{},"you want to learn how duplicate removal works",[24,63458,63459],{},"you need custom rules",[24,63461,63462],{},"you want more control over the logic",[37,63464,63466],{"id":63465},"beginner-notes","Beginner notes",[14,63468,63469,63470,63473],{},"These methods work best with lists that contain ",[5503,63471,63472],{},"hashable"," values, such as:",[309,63475,63476,63479,63481],{},[24,63477,63478],{},"strings",[24,63480,5324],{},[24,63482,63483],{},"tuples",[14,63485,63486],{},"Be careful with these cases:",[309,63488,63489,63492,63497],{},[24,63490,63491],{},"a list of dictionaries cannot be directly turned into a set",[24,63493,63494,63496],{},[49,63495,62564],{}," also needs hashable values",[24,63498,63499,759,63502,63505],{},[49,63500,63501],{},"\"Apple\"",[49,63503,63504],{},"\"apple\""," are different unless you normalize them first",[14,63507,63508],{},"Example of normalization:",[42,63510,63512],{"className":44,"code":63511,"language":46,"meta":47,"style":47},"data = [\"Apple\", \"apple\", \"BANANA\", \"banana\"]\n\nnormalized_unique = list(dict.fromkeys(item.lower() for item in data))\n\nprint(normalized_unique)\n",[49,63513,63514,63554,63558,63595,63599],{"__ignoreMap":47},[52,63515,63516,63518,63520,63522,63524,63526,63528,63530,63532,63534,63536,63538,63540,63542,63544,63546,63548,63550,63552],{"class":54,"line":55},[52,63517,217],{"class":62},[52,63519,80],{"class":79},[52,63521,18162],{"class":102},[52,63523,115],{"class":83},[52,63525,63228],{"class":87},[52,63527,115],{"class":83},[52,63529,199],{"class":102},[52,63531,84],{"class":83},[52,63533,33028],{"class":87},[52,63535,115],{"class":83},[52,63537,199],{"class":102},[52,63539,84],{"class":83},[52,63541,63262],{"class":87},[52,63543,115],{"class":83},[52,63545,199],{"class":102},[52,63547,84],{"class":83},[52,63549,32978],{"class":87},[52,63551,115],{"class":83},[52,63553,1147],{"class":102},[52,63555,63556],{"class":54,"line":66},[52,63557,70],{"emptyLinePlaceholder":69},[52,63559,63560,63563,63565,63567,63569,63571,63573,63575,63577,63579,63581,63583,63585,63587,63589,63591,63593],{"class":54,"line":73},[52,63561,63562],{"class":62},"normalized_unique ",[52,63564,80],{"class":79},[52,63566,41215],{"class":4733},[52,63568,193],{"class":102},[52,63570,40992],{"class":4733},[52,63572,186],{"class":102},[52,63574,62537],{"class":189},[52,63576,193],{"class":102},[52,63578,30322],{"class":189},[52,63580,186],{"class":102},[52,63582,30237],{"class":189},[52,63584,8267],{"class":102},[52,63586,37077],{"class":58},[52,63588,30192],{"class":189},[52,63590,11213],{"class":58},[52,63592,1356],{"class":189},[52,63594,8886],{"class":102},[52,63596,63597],{"class":54,"line":94},[52,63598,70],{"emptyLinePlaceholder":69},[52,63600,63601,63603,63605,63608],{"class":54,"line":106},[52,63602,242],{"class":241},[52,63604,193],{"class":102},[52,63606,63607],{"class":189},"normalized_unique",[52,63609,211],{"class":102},[14,63611,5267],{},[42,63613,63615],{"className":44,"code":63614,"language":46,"meta":47,"style":47},"['apple', 'banana']\n",[49,63616,63617],{"__ignoreMap":47},[52,63618,63619,63621,63623,63625,63627,63629,63631,63633,63635],{"class":54,"line":55},[52,63620,250],{"class":102},[52,63622,4162],{"class":83},[52,63624,33028],{"class":87},[52,63626,4162],{"class":83},[52,63628,199],{"class":102},[52,63630,4172],{"class":83},[52,63632,32978],{"class":87},[52,63634,4162],{"class":83},[52,63636,1147],{"class":102},[14,63638,63639,63640,186],{},"If you are doing broader cleanup work, this may also help: ",[372,63641,63643],{"href":63642},"\u002Fexamples\u002Fpython-data-cleaning-script-example\u002F","Python data cleaning script example",[37,63645,12322],{"id":12321},[14,63647,17965],{},[639,63649,10294,63651,63653],{"id":63650},"using-set-and-expecting-the-original-order-to-stay-the-same",[49,63652,63421],{}," and expecting the original order to stay the same",[14,63655,63656],{},"This will remove duplicates, but the order may change:",[42,63658,63660],{"className":44,"code":63659,"language":46,"meta":47,"style":47},"data = [\"apple\", \"banana\", \"apple\", \"orange\"]\nprint(list(set(data)))\n",[49,63661,63662,63702],{"__ignoreMap":47},[52,63663,63664,63666,63668,63670,63672,63674,63676,63678,63680,63682,63684,63686,63688,63690,63692,63694,63696,63698,63700],{"class":54,"line":55},[52,63665,217],{"class":62},[52,63667,80],{"class":79},[52,63669,18162],{"class":102},[52,63671,115],{"class":83},[52,63673,33028],{"class":87},[52,63675,115],{"class":83},[52,63677,199],{"class":102},[52,63679,84],{"class":83},[52,63681,32978],{"class":87},[52,63683,115],{"class":83},[52,63685,199],{"class":102},[52,63687,84],{"class":83},[52,63689,33028],{"class":87},[52,63691,115],{"class":83},[52,63693,199],{"class":102},[52,63695,84],{"class":83},[52,63697,32996],{"class":87},[52,63699,115],{"class":83},[52,63701,1147],{"class":102},[52,63703,63704,63706,63708,63710,63712,63714,63716,63718],{"class":54,"line":66},[52,63705,242],{"class":241},[52,63707,193],{"class":102},[52,63709,15301],{"class":4733},[52,63711,193],{"class":102},[52,63713,62737],{"class":4733},[52,63715,193],{"class":102},[52,63717,247],{"class":189},[52,63719,63720],{"class":102},")))\n",[14,63722,63723],{},"If order matters, use:",[42,63725,63727],{"className":44,"code":63726,"language":46,"meta":47,"style":47},"print(list(dict.fromkeys(data)))\n",[49,63728,63729],{"__ignoreMap":47},[52,63730,63731,63733,63735,63737,63739,63741,63743,63745,63747,63749],{"class":54,"line":55},[52,63732,242],{"class":241},[52,63734,193],{"class":102},[52,63736,15301],{"class":4733},[52,63738,193],{"class":102},[52,63740,40992],{"class":4733},[52,63742,186],{"class":102},[52,63744,62537],{"class":189},[52,63746,193],{"class":102},[52,63748,247],{"class":189},[52,63750,63720],{"class":102},[639,63752,63754,63755],{"id":63753},"trying-to-remove-duplicates-from-a-list-of-dictionaries-with-set","Trying to remove duplicates from a list of dictionaries with ",[49,63756,63421],{},[14,63758,63759],{},"This causes an error because dictionaries cannot be added to a set.",[42,63761,63763],{"className":44,"code":63762,"language":46,"meta":47,"style":47},"data = [{\"name\": \"Alice\"}, {\"name\": \"Alice\"}]\n# set(data)  # This will fail\n",[49,63764,63765,63808],{"__ignoreMap":47},[52,63766,63767,63769,63771,63774,63776,63778,63780,63782,63784,63786,63788,63790,63792,63794,63796,63798,63800,63802,63804,63806],{"class":54,"line":55},[52,63768,217],{"class":62},[52,63770,80],{"class":79},[52,63772,63773],{"class":102}," [{",[52,63775,115],{"class":83},[52,63777,2147],{"class":87},[52,63779,115],{"class":83},[52,63781,118],{"class":102},[52,63783,84],{"class":83},[52,63785,2156],{"class":87},[52,63787,115],{"class":83},[52,63789,20109],{"class":102},[52,63791,3000],{"class":102},[52,63793,115],{"class":83},[52,63795,2147],{"class":87},[52,63797,115],{"class":83},[52,63799,118],{"class":102},[52,63801,84],{"class":83},[52,63803,2156],{"class":87},[52,63805,115],{"class":83},[52,63807,20204],{"class":102},[52,63809,63810],{"class":54,"line":66},[52,63811,63812],{"class":4368},"# set(data)  # This will fail\n",[14,63814,63815],{},"If you need to work with more complex data, you usually have to compare a specific field yourself with a loop.",[639,63817,63819,63820,759,63822],{"id":63818},"not-normalizing-values-like-apple-and-apple","Not normalizing values like ",[49,63821,63501],{},[49,63823,63504],{},[14,63825,63826],{},"Python treats them as different strings:",[42,63828,63830],{"className":44,"code":63829,"language":46,"meta":47,"style":47},"data = [\"Apple\", \"apple\"]\nprint(list(dict.fromkeys(data)))\n",[49,63831,63832,63856],{"__ignoreMap":47},[52,63833,63834,63836,63838,63840,63842,63844,63846,63848,63850,63852,63854],{"class":54,"line":55},[52,63835,217],{"class":62},[52,63837,80],{"class":79},[52,63839,18162],{"class":102},[52,63841,115],{"class":83},[52,63843,63228],{"class":87},[52,63845,115],{"class":83},[52,63847,199],{"class":102},[52,63849,84],{"class":83},[52,63851,33028],{"class":87},[52,63853,115],{"class":83},[52,63855,1147],{"class":102},[52,63857,63858,63860,63862,63864,63866,63868,63870,63872,63874,63876],{"class":54,"line":66},[52,63859,242],{"class":241},[52,63861,193],{"class":102},[52,63863,15301],{"class":4733},[52,63865,193],{"class":102},[52,63867,40992],{"class":4733},[52,63869,186],{"class":102},[52,63871,62537],{"class":189},[52,63873,193],{"class":102},[52,63875,247],{"class":189},[52,63877,63720],{"class":102},[14,63879,5267],{},[42,63881,63883],{"className":44,"code":63882,"language":46,"meta":47,"style":47},"['Apple', 'apple']\n",[49,63884,63885],{"__ignoreMap":47},[52,63886,63887,63889,63891,63893,63895,63897,63899,63901,63903],{"class":54,"line":55},[52,63888,250],{"class":102},[52,63890,4162],{"class":83},[52,63892,63228],{"class":87},[52,63894,4162],{"class":83},[52,63896,199],{"class":102},[52,63898,4172],{"class":83},[52,63900,33028],{"class":87},[52,63902,4162],{"class":83},[52,63904,1147],{"class":102},[14,63906,63907],{},"Normalize first if needed:",[42,63909,63911],{"className":44,"code":63910,"language":46,"meta":47,"style":47},"data = [\"Apple\", \"apple\"]\nprint(list(dict.fromkeys(item.lower() for item in data)))\n",[49,63912,63913,63937],{"__ignoreMap":47},[52,63914,63915,63917,63919,63921,63923,63925,63927,63929,63931,63933,63935],{"class":54,"line":55},[52,63916,217],{"class":62},[52,63918,80],{"class":79},[52,63920,18162],{"class":102},[52,63922,115],{"class":83},[52,63924,63228],{"class":87},[52,63926,115],{"class":83},[52,63928,199],{"class":102},[52,63930,84],{"class":83},[52,63932,33028],{"class":87},[52,63934,115],{"class":83},[52,63936,1147],{"class":102},[52,63938,63939,63941,63943,63945,63947,63949,63951,63953,63955,63957,63959,63961,63963,63965,63967,63969,63971],{"class":54,"line":66},[52,63940,242],{"class":241},[52,63942,193],{"class":102},[52,63944,15301],{"class":4733},[52,63946,193],{"class":102},[52,63948,40992],{"class":4733},[52,63950,186],{"class":102},[52,63952,62537],{"class":189},[52,63954,193],{"class":102},[52,63956,30322],{"class":189},[52,63958,186],{"class":102},[52,63960,30237],{"class":189},[52,63962,8267],{"class":102},[52,63964,37077],{"class":58},[52,63966,30192],{"class":189},[52,63968,11213],{"class":58},[52,63970,1356],{"class":189},[52,63972,63720],{"class":102},[639,63974,63976,63977],{"id":63975},"forgetting-to-convert-the-result-back-to-a-list-after-using-set","Forgetting to convert the result back to a list after using ",[49,63978,63421],{},[14,63980,63981],{},"This:",[42,63983,63985],{"className":44,"code":63984,"language":46,"meta":47,"style":47},"data = [\"apple\", \"banana\", \"apple\"]\nunique_data = set(data)\nprint(unique_data)\n",[49,63986,63987,64019,64033],{"__ignoreMap":47},[52,63988,63989,63991,63993,63995,63997,63999,64001,64003,64005,64007,64009,64011,64013,64015,64017],{"class":54,"line":55},[52,63990,217],{"class":62},[52,63992,80],{"class":79},[52,63994,18162],{"class":102},[52,63996,115],{"class":83},[52,63998,33028],{"class":87},[52,64000,115],{"class":83},[52,64002,199],{"class":102},[52,64004,84],{"class":83},[52,64006,32978],{"class":87},[52,64008,115],{"class":83},[52,64010,199],{"class":102},[52,64012,84],{"class":83},[52,64014,33028],{"class":87},[52,64016,115],{"class":83},[52,64018,1147],{"class":102},[52,64020,64021,64023,64025,64027,64029,64031],{"class":54,"line":66},[52,64022,62524],{"class":62},[52,64024,80],{"class":79},[52,64026,62663],{"class":4733},[52,64028,193],{"class":102},[52,64030,247],{"class":189},[52,64032,211],{"class":102},[52,64034,64035,64037,64039,64041],{"class":54,"line":73},[52,64036,242],{"class":241},[52,64038,193],{"class":102},[52,64040,62552],{"class":189},[52,64042,211],{"class":102},[14,64044,64045],{},"gives you a set, not a list.",[14,64047,64048],{},"If you need a list, write:",[42,64050,64052],{"className":44,"code":64051,"language":46,"meta":47,"style":47},"unique_data = list(set(data))\n",[49,64053,64054],{"__ignoreMap":47},[52,64055,64056,64058,64060,64062,64064,64066,64068,64070],{"class":54,"line":55},[52,64057,62524],{"class":62},[52,64059,80],{"class":79},[52,64061,41215],{"class":4733},[52,64063,193],{"class":102},[52,64065,62737],{"class":4733},[52,64067,193],{"class":102},[52,64069,247],{"class":189},[52,64071,8886],{"class":102},[37,64073,1942],{"id":1941},[639,64075,64077],{"id":64076},"what-is-the-easiest-way-to-remove-duplicates-from-a-python-list","What is the easiest way to remove duplicates from a Python list?",[14,64079,3650,64080,64083,64084,64087],{},[49,64081,64082],{},"set(your_list)"," if order does not matter. Use ",[49,64085,64086],{},"list(dict.fromkeys(your_list))"," if you want to keep the original order.",[639,64089,64091,64092,64094],{"id":64090},"does-set-keep-the-same-order-as-the-original-list","Does ",[49,64093,63421],{}," keep the same order as the original list?",[14,64096,64097],{},"No. A set removes duplicates, but it does not guarantee the same order as the original list.",[639,64099,64101],{"id":64100},"how-do-i-remove-duplicates-and-keep-order-in-python","How do I remove duplicates and keep order in Python?",[14,64103,3650,64104,64107],{},[49,64105,64106],{},"list(dict.fromkeys(data))",". It keeps the first occurrence of each value.",[639,64109,10594,64111,64113],{"id":64110},"can-i-use-set-on-a-list-of-dictionaries",[49,64112,63421],{}," on a list of dictionaries?",[14,64115,64116],{},"No. Dictionaries are mutable and cannot be added to a set directly.",[37,64118,2005],{"id":2004},[309,64120,64121,64126,64130,64134,64138,64142],{},[24,64122,64123],{},[372,64124,64125],{"href":62589},"How to remove duplicates from a list in Python",[24,64127,64128],{},[372,64129,62753],{"href":62752},[24,64131,64132],{},[372,64133,62757],{"href":62756},[24,64135,64136],{},[372,64137,10883],{"href":10882},[24,64139,64140],{},[372,64141,34453],{"href":33628},[24,64143,64144],{},[372,64145,63643],{"href":63642},[2034,64147,64148],{},"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 .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 .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 .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}",{"title":47,"searchDepth":66,"depth":66,"links":64150},[64151,64152,64153,64157,64161,64164,64165,64166,64176,64184],{"id":62465,"depth":66,"text":62466},{"id":62568,"depth":66,"text":62569},{"id":62593,"depth":66,"text":62594,"children":64154},[64155,64156],{"id":36905,"depth":73,"text":36906},{"id":54519,"depth":73,"text":54520},{"id":62852,"depth":66,"text":62853,"children":64158},[64159,64160],{"id":32448,"depth":73,"text":32449},{"id":63004,"depth":73,"text":63005},{"id":63023,"depth":66,"text":63024,"children":64162},[64163],{"id":24094,"depth":73,"text":24095},{"id":63415,"depth":66,"text":63416},{"id":63465,"depth":66,"text":63466},{"id":12321,"depth":66,"text":12322,"children":64167},[64168,64170,64172,64174],{"id":63650,"depth":73,"text":64169},"Using set() and expecting the original order to stay the same",{"id":63753,"depth":73,"text":64171},"Trying to remove duplicates from a list of dictionaries with set()",{"id":63818,"depth":73,"text":64173},"Not normalizing values like \"Apple\" and \"apple\"",{"id":63975,"depth":73,"text":64175},"Forgetting to convert the result back to a list after using set()",{"id":1941,"depth":66,"text":1942,"children":64177},[64178,64179,64181,64182],{"id":64076,"depth":73,"text":64077},{"id":64090,"depth":73,"text":64180},"Does set() keep the same order as the original list?",{"id":64100,"depth":73,"text":64101},{"id":64110,"depth":73,"text":64183},"Can I use set() on a list of dictionaries?",{"id":2004,"depth":66,"text":2005},"Master python remove duplicates from data example in our comprehensive Python beginner guide.",{},{"title":62440,"description":64185},"examples\u002Fpython-remove-duplicates-from-data-example","vVztjqRvKDL0Pft468CGFVqNBQshw-2s30J25Th0rKE",{"id":64191,"title":64192,"body":64193,"description":66449,"extension":2075,"meta":66450,"navigation":69,"path":66451,"seo":66452,"stem":66453,"__hash__":66454},"content\u002Fexamples\u002Fpython-rename-files-in-a-folder-example.md","Python Rename Files in a Folder Example",{"type":7,"value":64194,"toc":66411},[64195,64198,64201,64204,64220,64226,64228,64424,64434,64436,64452,64457,64459,64461,64639,64641,64689,64694,64701,64720,64723,64735,64738,64770,64780,64784,64787,64790,64792,64970,64974,64977,64991,64994,65000,65004,65011,65021,65024,65222,65226,65230,65233,65236,65238,65254,65257,65418,65422,65424,65451,65471,65474,65480,65484,65487,65504,65507,65511,65677,65680,65684,65686,65690,65695,65697,65765,65768,65778,65782,65786,65792,65803,65808,65812,65815,65820,65832,65836,65839,65842,65948,65954,65958,65961,65964,65972,65976,65979,65981,66058,66061,66078,66081,66100,66102,66106,66111,66113,66143,66150,66153,66350,66354,66357,66361,66364,66372,66377,66379,66405,66408],[10,64196,64192],{"id":64197},"python-rename-files-in-a-folder-example",[14,64199,64200],{},"If you want to rename many files in one folder, Python can do it with a short script.",[14,64202,64203],{},"This beginner-friendly example shows how to:",[309,64205,64206,64209,64212,64215],{},[24,64207,64208],{},"loop through files in a folder",[24,64210,64211],{},"rename each file with a simple pattern",[24,64213,64214],{},"keep the original file extension",[24,64216,23564,64217,64219],{},[49,64218,28924],{}," for clearer file path handling",[14,64221,64222,64223,64225],{},"This page focuses on one folder only. It does ",[5503,64224,20858],{}," rename files inside subfolders.",[37,64227,40],{"id":39},[42,64229,64231],{"className":44,"code":64230,"language":46,"meta":47,"style":47},"from pathlib import Path\n\nfolder = Path(\"example_folder\")\n\nfor index, file_path in enumerate(folder.iterdir(), start=1):\n    if file_path.is_file():\n        new_name = f\"file_{index}{file_path.suffix}\"\n        new_path = file_path.with_name(new_name)\n        file_path.rename(new_path)\n        print(f\"Renamed: {file_path.name} -> {new_path.name}\")\n",[49,64232,64233,64243,64247,64267,64271,64307,64319,64349,64370,64387],{"__ignoreMap":47},[52,64234,64235,64237,64239,64241],{"class":54,"line":55},[52,64236,25329],{"class":58},[52,64238,28537],{"class":62},[52,64240,59],{"class":58},[52,64242,28542],{"class":62},[52,64244,64245],{"class":54,"line":66},[52,64246,70],{"emptyLinePlaceholder":69},[52,64248,64249,64252,64254,64256,64258,64260,64263,64265],{"class":54,"line":73},[52,64250,64251],{"class":62},"folder ",[52,64253,80],{"class":79},[52,64255,28563],{"class":189},[52,64257,193],{"class":102},[52,64259,115],{"class":83},[52,64261,64262],{"class":87},"example_folder",[52,64264,115],{"class":83},[52,64266,211],{"class":102},[52,64268,64269],{"class":54,"line":94},[52,64270,70],{"emptyLinePlaceholder":69},[52,64272,64273,64275,64278,64280,64282,64284,64286,64288,64291,64293,64295,64298,64301,64303,64305],{"class":54,"line":106},[52,64274,12265],{"class":58},[52,64276,64277],{"class":62}," index",[52,64279,199],{"class":102},[52,64281,49269],{"class":62},[52,64283,11213],{"class":58},[52,64285,49274],{"class":241},[52,64287,193],{"class":102},[52,64289,64290],{"class":189},"folder",[52,64292,186],{"class":102},[52,64294,30202],{"class":189},[52,64296,64297],{"class":102},"(),",[52,64299,64300],{"class":202}," start",[52,64302,80],{"class":79},[52,64304,9200],{"class":121},[52,64306,3114],{"class":102},[52,64308,64309,64311,64313,64315,64317],{"class":54,"line":128},[52,64310,1438],{"class":58},[52,64312,49288],{"class":62},[52,64314,186],{"class":102},[52,64316,30216],{"class":189},[52,64318,8146],{"class":102},[52,64320,64321,64324,64326,64328,64331,64333,64336,64339,64341,64343,64345,64347],{"class":54,"line":148},[52,64322,64323],{"class":62},"        new_name ",[52,64325,80],{"class":79},[52,64327,6126],{"class":1475},[52,64329,64330],{"class":87},"\"file_",[52,64332,1482],{"class":121},[52,64334,64335],{"class":62},"index",[52,64337,64338],{"class":121},"}{",[52,64340,50879],{"class":62},[52,64342,186],{"class":102},[52,64344,30232],{"class":1320},[52,64346,1488],{"class":121},[52,64348,91],{"class":87},[52,64350,64351,64354,64356,64358,64360,64363,64365,64368],{"class":54,"line":164},[52,64352,64353],{"class":62},"        new_path ",[52,64355,80],{"class":79},[52,64357,49288],{"class":62},[52,64359,186],{"class":102},[52,64361,64362],{"class":189},"with_name",[52,64364,193],{"class":102},[52,64366,64367],{"class":189},"new_name",[52,64369,211],{"class":102},[52,64371,64372,64375,64377,64380,64382,64385],{"class":54,"line":170},[52,64373,64374],{"class":62},"        file_path",[52,64376,186],{"class":102},[52,64378,64379],{"class":189},"rename",[52,64381,193],{"class":102},[52,64383,64384],{"class":189},"new_path",[52,64386,211],{"class":102},[52,64388,64389,64391,64393,64395,64398,64400,64402,64404,64406,64408,64410,64412,64414,64416,64418,64420,64422],{"class":54,"line":175},[52,64390,1470],{"class":241},[52,64392,193],{"class":102},[52,64394,1476],{"class":1475},[52,64396,64397],{"class":87},"\"Renamed: ",[52,64399,1482],{"class":121},[52,64401,50879],{"class":189},[52,64403,186],{"class":102},[52,64405,2147],{"class":1320},[52,64407,1488],{"class":121},[52,64409,31507],{"class":87},[52,64411,1482],{"class":121},[52,64413,64384],{"class":189},[52,64415,186],{"class":102},[52,64417,2147],{"class":1320},[52,64419,1488],{"class":121},[52,64421,115],{"class":87},[52,64423,211],{"class":102},[14,64425,64426,64427,2587,64430,64433],{},"This renames each file in the folder to ",[49,64428,64429],{},"file_1",[49,64431,64432],{},"file_2",", and so on, while keeping the original file extension.",[37,64435,323],{"id":322},[309,64437,64438,64441,64444,64447],{},[24,64439,64440],{},"Shows how to loop through files in one folder",[24,64442,64443],{},"Renames each file using a predictable pattern",[24,64445,64446],{},"Keeps the original file extension",[24,64448,3609,64449,64451],{},[49,64450,28924],{}," for beginner-friendly path handling",[14,64453,64454,64455,186],{},"If you are new to paths, see ",[372,64456,28723],{"href":28722},[37,64458,25641],{"id":25640},[14,64460,25644],{},[42,64462,64463],{"className":44,"code":64230,"language":46,"meta":47,"style":47},[49,64464,64465,64475,64479,64497,64501,64533,64545,64571,64589,64603],{"__ignoreMap":47},[52,64466,64467,64469,64471,64473],{"class":54,"line":55},[52,64468,25329],{"class":58},[52,64470,28537],{"class":62},[52,64472,59],{"class":58},[52,64474,28542],{"class":62},[52,64476,64477],{"class":54,"line":66},[52,64478,70],{"emptyLinePlaceholder":69},[52,64480,64481,64483,64485,64487,64489,64491,64493,64495],{"class":54,"line":73},[52,64482,64251],{"class":62},[52,64484,80],{"class":79},[52,64486,28563],{"class":189},[52,64488,193],{"class":102},[52,64490,115],{"class":83},[52,64492,64262],{"class":87},[52,64494,115],{"class":83},[52,64496,211],{"class":102},[52,64498,64499],{"class":54,"line":94},[52,64500,70],{"emptyLinePlaceholder":69},[52,64502,64503,64505,64507,64509,64511,64513,64515,64517,64519,64521,64523,64525,64527,64529,64531],{"class":54,"line":106},[52,64504,12265],{"class":58},[52,64506,64277],{"class":62},[52,64508,199],{"class":102},[52,64510,49269],{"class":62},[52,64512,11213],{"class":58},[52,64514,49274],{"class":241},[52,64516,193],{"class":102},[52,64518,64290],{"class":189},[52,64520,186],{"class":102},[52,64522,30202],{"class":189},[52,64524,64297],{"class":102},[52,64526,64300],{"class":202},[52,64528,80],{"class":79},[52,64530,9200],{"class":121},[52,64532,3114],{"class":102},[52,64534,64535,64537,64539,64541,64543],{"class":54,"line":128},[52,64536,1438],{"class":58},[52,64538,49288],{"class":62},[52,64540,186],{"class":102},[52,64542,30216],{"class":189},[52,64544,8146],{"class":102},[52,64546,64547,64549,64551,64553,64555,64557,64559,64561,64563,64565,64567,64569],{"class":54,"line":148},[52,64548,64323],{"class":62},[52,64550,80],{"class":79},[52,64552,6126],{"class":1475},[52,64554,64330],{"class":87},[52,64556,1482],{"class":121},[52,64558,64335],{"class":62},[52,64560,64338],{"class":121},[52,64562,50879],{"class":62},[52,64564,186],{"class":102},[52,64566,30232],{"class":1320},[52,64568,1488],{"class":121},[52,64570,91],{"class":87},[52,64572,64573,64575,64577,64579,64581,64583,64585,64587],{"class":54,"line":164},[52,64574,64353],{"class":62},[52,64576,80],{"class":79},[52,64578,49288],{"class":62},[52,64580,186],{"class":102},[52,64582,64362],{"class":189},[52,64584,193],{"class":102},[52,64586,64367],{"class":189},[52,64588,211],{"class":102},[52,64590,64591,64593,64595,64597,64599,64601],{"class":54,"line":170},[52,64592,64374],{"class":62},[52,64594,186],{"class":102},[52,64596,64379],{"class":189},[52,64598,193],{"class":102},[52,64600,64384],{"class":189},[52,64602,211],{"class":102},[52,64604,64605,64607,64609,64611,64613,64615,64617,64619,64621,64623,64625,64627,64629,64631,64633,64635,64637],{"class":54,"line":175},[52,64606,1470],{"class":241},[52,64608,193],{"class":102},[52,64610,1476],{"class":1475},[52,64612,64397],{"class":87},[52,64614,1482],{"class":121},[52,64616,50879],{"class":189},[52,64618,186],{"class":102},[52,64620,2147],{"class":1320},[52,64622,1488],{"class":121},[52,64624,31507],{"class":87},[52,64626,1482],{"class":121},[52,64628,64384],{"class":189},[52,64630,186],{"class":102},[52,64632,2147],{"class":1320},[52,64634,1488],{"class":121},[52,64636,115],{"class":87},[52,64638,211],{"class":102},[14,64640,25861],{},[309,64642,64643,64649,64655,64661,64667,64677,64683],{},[24,64644,64645,64648],{},[49,64646,64647],{},"Path(\"example_folder\")"," creates a path object for the folder",[24,64650,64651,64654],{},[49,64652,64653],{},"folder.iterdir()"," gets the items inside that folder",[24,64656,64657,64660],{},[49,64658,64659],{},"enumerate(..., start=1)"," gives each item a number starting at 1",[24,64662,64663,64666],{},[49,64664,64665],{},"file_path.is_file()"," makes sure folders are skipped",[24,64668,64669,64672,64673,857,64675],{},[49,64670,64671],{},"file_path.suffix"," keeps extensions like ",[49,64674,30125],{},[49,64676,25085],{},[24,64678,64679,64682],{},[49,64680,64681],{},"file_path.with_name(new_name)"," builds the new file path",[24,64684,64685,64688],{},[49,64686,64687],{},"file_path.rename(new_path)"," renames the file",[639,64690,24316,64692,34988],{"id":64691},"why-suffix-matters",[49,64693,30232],{},[14,64695,64696,64697,64700],{},"If your file is named ",[49,64698,64699],{},"report.txt",", then:",[42,64702,64704],{"className":44,"code":64703,"language":46,"meta":47,"style":47},"print(file_path.suffix)\n",[49,64705,64706],{"__ignoreMap":47},[52,64707,64708,64710,64712,64714,64716,64718],{"class":54,"line":55},[52,64709,242],{"class":241},[52,64711,193],{"class":102},[52,64713,50879],{"class":189},[52,64715,186],{"class":102},[52,64717,30232],{"class":1320},[52,64719,211],{"class":102},[14,64721,64722],{},"would give:",[42,64724,64726],{"className":44,"code":64725,"language":46,"meta":47,"style":47},".txt\n",[49,64727,64728],{"__ignoreMap":47},[52,64729,64730,64732],{"class":54,"line":55},[52,64731,186],{"class":102},[52,64733,64734],{"class":1320},"txt\n",[14,64736,64737],{},"That is why this line keeps the extension:",[42,64739,64741],{"className":44,"code":64740,"language":46,"meta":47,"style":47},"new_name = f\"file_{index}{file_path.suffix}\"\n",[49,64742,64743],{"__ignoreMap":47},[52,64744,64745,64748,64750,64752,64754,64756,64758,64760,64762,64764,64766,64768],{"class":54,"line":55},[52,64746,64747],{"class":62},"new_name ",[52,64749,80],{"class":79},[52,64751,6126],{"class":1475},[52,64753,64330],{"class":87},[52,64755,1482],{"class":121},[52,64757,64335],{"class":62},[52,64759,64338],{"class":121},[52,64761,50879],{"class":62},[52,64763,186],{"class":102},[52,64765,30232],{"class":1320},[52,64767,1488],{"class":121},[52,64769,91],{"class":87},[14,64771,5788,64772,2587,64774,64776,64777,64779],{},[49,64773,30232],{},[49,64775,64699],{}," might become just ",[49,64778,64429],{},", which removes the original extension.",[37,64781,64783],{"id":64782},"example-rename-files-with-numbers","Example: rename files with numbers",[14,64785,64786],{},"This is the simplest version to learn first.",[14,64788,64789],{},"It is useful when the original file names do not matter.",[639,64791,38262],{"id":49},[42,64793,64794],{"className":44,"code":64230,"language":46,"meta":47,"style":47},[49,64795,64796,64806,64810,64828,64832,64864,64876,64902,64920,64934],{"__ignoreMap":47},[52,64797,64798,64800,64802,64804],{"class":54,"line":55},[52,64799,25329],{"class":58},[52,64801,28537],{"class":62},[52,64803,59],{"class":58},[52,64805,28542],{"class":62},[52,64807,64808],{"class":54,"line":66},[52,64809,70],{"emptyLinePlaceholder":69},[52,64811,64812,64814,64816,64818,64820,64822,64824,64826],{"class":54,"line":73},[52,64813,64251],{"class":62},[52,64815,80],{"class":79},[52,64817,28563],{"class":189},[52,64819,193],{"class":102},[52,64821,115],{"class":83},[52,64823,64262],{"class":87},[52,64825,115],{"class":83},[52,64827,211],{"class":102},[52,64829,64830],{"class":54,"line":94},[52,64831,70],{"emptyLinePlaceholder":69},[52,64833,64834,64836,64838,64840,64842,64844,64846,64848,64850,64852,64854,64856,64858,64860,64862],{"class":54,"line":106},[52,64835,12265],{"class":58},[52,64837,64277],{"class":62},[52,64839,199],{"class":102},[52,64841,49269],{"class":62},[52,64843,11213],{"class":58},[52,64845,49274],{"class":241},[52,64847,193],{"class":102},[52,64849,64290],{"class":189},[52,64851,186],{"class":102},[52,64853,30202],{"class":189},[52,64855,64297],{"class":102},[52,64857,64300],{"class":202},[52,64859,80],{"class":79},[52,64861,9200],{"class":121},[52,64863,3114],{"class":102},[52,64865,64866,64868,64870,64872,64874],{"class":54,"line":128},[52,64867,1438],{"class":58},[52,64869,49288],{"class":62},[52,64871,186],{"class":102},[52,64873,30216],{"class":189},[52,64875,8146],{"class":102},[52,64877,64878,64880,64882,64884,64886,64888,64890,64892,64894,64896,64898,64900],{"class":54,"line":148},[52,64879,64323],{"class":62},[52,64881,80],{"class":79},[52,64883,6126],{"class":1475},[52,64885,64330],{"class":87},[52,64887,1482],{"class":121},[52,64889,64335],{"class":62},[52,64891,64338],{"class":121},[52,64893,50879],{"class":62},[52,64895,186],{"class":102},[52,64897,30232],{"class":1320},[52,64899,1488],{"class":121},[52,64901,91],{"class":87},[52,64903,64904,64906,64908,64910,64912,64914,64916,64918],{"class":54,"line":164},[52,64905,64353],{"class":62},[52,64907,80],{"class":79},[52,64909,49288],{"class":62},[52,64911,186],{"class":102},[52,64913,64362],{"class":189},[52,64915,193],{"class":102},[52,64917,64367],{"class":189},[52,64919,211],{"class":102},[52,64921,64922,64924,64926,64928,64930,64932],{"class":54,"line":170},[52,64923,64374],{"class":62},[52,64925,186],{"class":102},[52,64927,64379],{"class":189},[52,64929,193],{"class":102},[52,64931,64384],{"class":189},[52,64933,211],{"class":102},[52,64935,64936,64938,64940,64942,64944,64946,64948,64950,64952,64954,64956,64958,64960,64962,64964,64966,64968],{"class":54,"line":175},[52,64937,1470],{"class":241},[52,64939,193],{"class":102},[52,64941,1476],{"class":1475},[52,64943,64397],{"class":87},[52,64945,1482],{"class":121},[52,64947,50879],{"class":189},[52,64949,186],{"class":102},[52,64951,2147],{"class":1320},[52,64953,1488],{"class":121},[52,64955,31507],{"class":87},[52,64957,1482],{"class":121},[52,64959,64384],{"class":189},[52,64961,186],{"class":102},[52,64963,2147],{"class":1320},[52,64965,1488],{"class":121},[52,64967,115],{"class":87},[52,64969,211],{"class":102},[639,64971,64973],{"id":64972},"example-result","Example result",[14,64975,64976],{},"If your folder contains:",[309,64978,64979,64983,64987],{},[24,64980,64981],{},[49,64982,64699],{},[24,64984,64985],{},[49,64986,30951],{},[24,64988,64989],{},[49,64990,28570],{},[14,64992,64993],{},"You might see output like:",[42,64995,64998],{"className":64996,"code":64997,"language":955,"meta":47},[953],"Renamed: report.txt -> file_1.txt\nRenamed: photo.jpg -> file_2.jpg\nRenamed: notes.txt -> file_3.txt\n",[49,64999,64997],{"__ignoreMap":47},[639,65001,65003],{"id":65002},"important-note-about-order","Important note about order",[14,65005,65006,65007,65010],{},"The order from ",[49,65008,65009],{},"iterdir()"," is not always guaranteed.",[14,65012,65013,65014,65016,65017,65020],{},"That means ",[49,65015,64699],{}," might become ",[49,65018,65019],{},"file_1.txt"," on one run, but a different file might get that name on another system.",[14,65022,65023],{},"If you want a more predictable order, sort the files first:",[42,65025,65027],{"className":44,"code":65026,"language":46,"meta":47,"style":47},"from pathlib import Path\n\nfolder = Path(\"example_folder\")\n\nfiles = sorted(folder.iterdir())\n\nfor index, file_path in enumerate(files, start=1):\n    if file_path.is_file():\n        new_name = f\"file_{index}{file_path.suffix}\"\n        new_path = file_path.with_name(new_name)\n        file_path.rename(new_path)\n        print(f\"Renamed: {file_path.name} -> {new_path.name}\")\n",[49,65028,65029,65039,65043,65061,65065,65084,65088,65116,65128,65154,65172,65186],{"__ignoreMap":47},[52,65030,65031,65033,65035,65037],{"class":54,"line":55},[52,65032,25329],{"class":58},[52,65034,28537],{"class":62},[52,65036,59],{"class":58},[52,65038,28542],{"class":62},[52,65040,65041],{"class":54,"line":66},[52,65042,70],{"emptyLinePlaceholder":69},[52,65044,65045,65047,65049,65051,65053,65055,65057,65059],{"class":54,"line":73},[52,65046,64251],{"class":62},[52,65048,80],{"class":79},[52,65050,28563],{"class":189},[52,65052,193],{"class":102},[52,65054,115],{"class":83},[52,65056,64262],{"class":87},[52,65058,115],{"class":83},[52,65060,211],{"class":102},[52,65062,65063],{"class":54,"line":94},[52,65064,70],{"emptyLinePlaceholder":69},[52,65066,65067,65070,65072,65074,65076,65078,65080,65082],{"class":54,"line":106},[52,65068,65069],{"class":62},"files ",[52,65071,80],{"class":79},[52,65073,49172],{"class":241},[52,65075,193],{"class":102},[52,65077,64290],{"class":189},[52,65079,186],{"class":102},[52,65081,30202],{"class":189},[52,65083,1816],{"class":102},[52,65085,65086],{"class":54,"line":128},[52,65087,70],{"emptyLinePlaceholder":69},[52,65089,65090,65092,65094,65096,65098,65100,65102,65104,65106,65108,65110,65112,65114],{"class":54,"line":148},[52,65091,12265],{"class":58},[52,65093,64277],{"class":62},[52,65095,199],{"class":102},[52,65097,49269],{"class":62},[52,65099,11213],{"class":58},[52,65101,49274],{"class":241},[52,65103,193],{"class":102},[52,65105,2607],{"class":189},[52,65107,199],{"class":102},[52,65109,64300],{"class":202},[52,65111,80],{"class":79},[52,65113,9200],{"class":121},[52,65115,3114],{"class":102},[52,65117,65118,65120,65122,65124,65126],{"class":54,"line":164},[52,65119,1438],{"class":58},[52,65121,49288],{"class":62},[52,65123,186],{"class":102},[52,65125,30216],{"class":189},[52,65127,8146],{"class":102},[52,65129,65130,65132,65134,65136,65138,65140,65142,65144,65146,65148,65150,65152],{"class":54,"line":170},[52,65131,64323],{"class":62},[52,65133,80],{"class":79},[52,65135,6126],{"class":1475},[52,65137,64330],{"class":87},[52,65139,1482],{"class":121},[52,65141,64335],{"class":62},[52,65143,64338],{"class":121},[52,65145,50879],{"class":62},[52,65147,186],{"class":102},[52,65149,30232],{"class":1320},[52,65151,1488],{"class":121},[52,65153,91],{"class":87},[52,65155,65156,65158,65160,65162,65164,65166,65168,65170],{"class":54,"line":175},[52,65157,64353],{"class":62},[52,65159,80],{"class":79},[52,65161,49288],{"class":62},[52,65163,186],{"class":102},[52,65165,64362],{"class":189},[52,65167,193],{"class":102},[52,65169,64367],{"class":189},[52,65171,211],{"class":102},[52,65173,65174,65176,65178,65180,65182,65184],{"class":54,"line":214},[52,65175,64374],{"class":62},[52,65177,186],{"class":102},[52,65179,64379],{"class":189},[52,65181,193],{"class":102},[52,65183,64384],{"class":189},[52,65185,211],{"class":102},[52,65187,65188,65190,65192,65194,65196,65198,65200,65202,65204,65206,65208,65210,65212,65214,65216,65218,65220],{"class":54,"line":233},[52,65189,1470],{"class":241},[52,65191,193],{"class":102},[52,65193,1476],{"class":1475},[52,65195,64397],{"class":87},[52,65197,1482],{"class":121},[52,65199,50879],{"class":189},[52,65201,186],{"class":102},[52,65203,2147],{"class":1320},[52,65205,1488],{"class":121},[52,65207,31507],{"class":87},[52,65209,1482],{"class":121},[52,65211,64384],{"class":189},[52,65213,186],{"class":102},[52,65215,2147],{"class":1320},[52,65217,1488],{"class":121},[52,65219,115],{"class":87},[52,65221,211],{"class":102},[14,65223,30812,65224,186],{},[372,65225,30816],{"href":35868},[37,65227,65229],{"id":65228},"example-add-a-prefix-to-each-file","Example: add a prefix to each file",[14,65231,65232],{},"This version keeps most of the original name.",[14,65234,65235],{},"It is often safer and easier to read.",[14,65237,16291],{},[309,65239,65240,65247],{},[24,65241,65242,6230,65244],{},[49,65243,28570],{},[49,65245,65246],{},"old_notes.txt",[24,65248,65249,6230,65251],{},[49,65250,30951],{},[49,65252,65253],{},"old_photo.jpg",[639,65255,38262],{"id":65256},"code-1",[42,65258,65260],{"className":44,"code":65259,"language":46,"meta":47,"style":47},"from pathlib import Path\n\nfolder = Path(\"example_folder\")\n\nfor file_path in folder.iterdir():\n    if file_path.is_file():\n        new_name = f\"old_{file_path.name}\"\n        new_path = file_path.with_name(new_name)\n        file_path.rename(new_path)\n        print(f\"Renamed: {file_path.name} -> {new_path.name}\")\n",[49,65261,65262,65272,65276,65294,65298,65315,65327,65350,65368,65382],{"__ignoreMap":47},[52,65263,65264,65266,65268,65270],{"class":54,"line":55},[52,65265,25329],{"class":58},[52,65267,28537],{"class":62},[52,65269,59],{"class":58},[52,65271,28542],{"class":62},[52,65273,65274],{"class":54,"line":66},[52,65275,70],{"emptyLinePlaceholder":69},[52,65277,65278,65280,65282,65284,65286,65288,65290,65292],{"class":54,"line":73},[52,65279,64251],{"class":62},[52,65281,80],{"class":79},[52,65283,28563],{"class":189},[52,65285,193],{"class":102},[52,65287,115],{"class":83},[52,65289,64262],{"class":87},[52,65291,115],{"class":83},[52,65293,211],{"class":102},[52,65295,65296],{"class":54,"line":94},[52,65297,70],{"emptyLinePlaceholder":69},[52,65299,65300,65302,65304,65306,65309,65311,65313],{"class":54,"line":106},[52,65301,12265],{"class":58},[52,65303,49269],{"class":62},[52,65305,11213],{"class":58},[52,65307,65308],{"class":62}," folder",[52,65310,186],{"class":102},[52,65312,30202],{"class":189},[52,65314,8146],{"class":102},[52,65316,65317,65319,65321,65323,65325],{"class":54,"line":128},[52,65318,1438],{"class":58},[52,65320,49288],{"class":62},[52,65322,186],{"class":102},[52,65324,30216],{"class":189},[52,65326,8146],{"class":102},[52,65328,65329,65331,65333,65335,65338,65340,65342,65344,65346,65348],{"class":54,"line":148},[52,65330,64323],{"class":62},[52,65332,80],{"class":79},[52,65334,6126],{"class":1475},[52,65336,65337],{"class":87},"\"old_",[52,65339,1482],{"class":121},[52,65341,50879],{"class":62},[52,65343,186],{"class":102},[52,65345,2147],{"class":1320},[52,65347,1488],{"class":121},[52,65349,91],{"class":87},[52,65351,65352,65354,65356,65358,65360,65362,65364,65366],{"class":54,"line":164},[52,65353,64353],{"class":62},[52,65355,80],{"class":79},[52,65357,49288],{"class":62},[52,65359,186],{"class":102},[52,65361,64362],{"class":189},[52,65363,193],{"class":102},[52,65365,64367],{"class":189},[52,65367,211],{"class":102},[52,65369,65370,65372,65374,65376,65378,65380],{"class":54,"line":170},[52,65371,64374],{"class":62},[52,65373,186],{"class":102},[52,65375,64379],{"class":189},[52,65377,193],{"class":102},[52,65379,64384],{"class":189},[52,65381,211],{"class":102},[52,65383,65384,65386,65388,65390,65392,65394,65396,65398,65400,65402,65404,65406,65408,65410,65412,65414,65416],{"class":54,"line":175},[52,65385,1470],{"class":241},[52,65387,193],{"class":102},[52,65389,1476],{"class":1475},[52,65391,64397],{"class":87},[52,65393,1482],{"class":121},[52,65395,50879],{"class":189},[52,65397,186],{"class":102},[52,65399,2147],{"class":1320},[52,65401,1488],{"class":121},[52,65403,31507],{"class":87},[52,65405,1482],{"class":121},[52,65407,64384],{"class":189},[52,65409,186],{"class":102},[52,65411,2147],{"class":1320},[52,65413,1488],{"class":121},[52,65415,115],{"class":87},[52,65417,211],{"class":102},[639,65419,65421],{"id":65420},"what-changed","What changed",[14,65423,32483],{},[42,65425,65427],{"className":44,"code":65426,"language":46,"meta":47,"style":47},"new_name = f\"old_{file_path.name}\"\n",[49,65428,65429],{"__ignoreMap":47},[52,65430,65431,65433,65435,65437,65439,65441,65443,65445,65447,65449],{"class":54,"line":55},[52,65432,64747],{"class":62},[52,65434,80],{"class":79},[52,65436,6126],{"class":1475},[52,65438,65337],{"class":87},[52,65440,1482],{"class":121},[52,65442,50879],{"class":62},[52,65444,186],{"class":102},[52,65446,2147],{"class":1320},[52,65448,1488],{"class":121},[52,65450,91],{"class":87},[309,65452,65453,65461,65468],{},[24,65454,65455,65458,65459],{},[49,65456,65457],{},"file_path.name"," gives the full file name, such as ",[49,65460,28570],{},[24,65462,65463,65464,65467],{},"adding ",[49,65465,65466],{},"\"old_\""," puts a prefix at the start",[24,65469,65470],{},"the extension stays because the original name stays",[14,65472,65473],{},"This is a good option when you want to rename files but still keep them recognizable.",[14,65475,65476,65477,186],{},"For renaming just one file, see ",[372,65478,31947],{"href":65479},"\u002Fhow-to\u002Fhow-to-rename-a-file-in-python\u002F",[37,65481,65483],{"id":65482},"how-to-avoid-accidental-problems","How to avoid accidental problems",[14,65485,65486],{},"Before running a renaming script on real files:",[309,65488,65489,65492,65495,65498,65501],{},[24,65490,65491],{},"Test in a folder with sample files first",[24,65493,65494],{},"Print old and new names before renaming",[24,65496,65497],{},"Skip folders unless you want to rename them too",[24,65499,65500],{},"Make sure new names do not duplicate existing names",[24,65502,65503],{},"Keep backups if the files are important",[14,65505,65506],{},"A very safe practice is to preview the rename first.",[639,65508,65510],{"id":65509},"preview-before-renaming","Preview before renaming",[42,65512,65514],{"className":44,"code":65513,"language":46,"meta":47,"style":47},"from pathlib import Path\n\nfolder = Path(\"example_folder\")\n\nfor index, file_path in enumerate(folder.iterdir(), start=1):\n    if file_path.is_file():\n        new_name = f\"file_{index}{file_path.suffix}\"\n        new_path = file_path.with_name(new_name)\n        print(f\"Would rename: {file_path.name} -> {new_path.name}\")\n",[49,65515,65516,65526,65530,65548,65552,65584,65596,65622,65640],{"__ignoreMap":47},[52,65517,65518,65520,65522,65524],{"class":54,"line":55},[52,65519,25329],{"class":58},[52,65521,28537],{"class":62},[52,65523,59],{"class":58},[52,65525,28542],{"class":62},[52,65527,65528],{"class":54,"line":66},[52,65529,70],{"emptyLinePlaceholder":69},[52,65531,65532,65534,65536,65538,65540,65542,65544,65546],{"class":54,"line":73},[52,65533,64251],{"class":62},[52,65535,80],{"class":79},[52,65537,28563],{"class":189},[52,65539,193],{"class":102},[52,65541,115],{"class":83},[52,65543,64262],{"class":87},[52,65545,115],{"class":83},[52,65547,211],{"class":102},[52,65549,65550],{"class":54,"line":94},[52,65551,70],{"emptyLinePlaceholder":69},[52,65553,65554,65556,65558,65560,65562,65564,65566,65568,65570,65572,65574,65576,65578,65580,65582],{"class":54,"line":106},[52,65555,12265],{"class":58},[52,65557,64277],{"class":62},[52,65559,199],{"class":102},[52,65561,49269],{"class":62},[52,65563,11213],{"class":58},[52,65565,49274],{"class":241},[52,65567,193],{"class":102},[52,65569,64290],{"class":189},[52,65571,186],{"class":102},[52,65573,30202],{"class":189},[52,65575,64297],{"class":102},[52,65577,64300],{"class":202},[52,65579,80],{"class":79},[52,65581,9200],{"class":121},[52,65583,3114],{"class":102},[52,65585,65586,65588,65590,65592,65594],{"class":54,"line":128},[52,65587,1438],{"class":58},[52,65589,49288],{"class":62},[52,65591,186],{"class":102},[52,65593,30216],{"class":189},[52,65595,8146],{"class":102},[52,65597,65598,65600,65602,65604,65606,65608,65610,65612,65614,65616,65618,65620],{"class":54,"line":148},[52,65599,64323],{"class":62},[52,65601,80],{"class":79},[52,65603,6126],{"class":1475},[52,65605,64330],{"class":87},[52,65607,1482],{"class":121},[52,65609,64335],{"class":62},[52,65611,64338],{"class":121},[52,65613,50879],{"class":62},[52,65615,186],{"class":102},[52,65617,30232],{"class":1320},[52,65619,1488],{"class":121},[52,65621,91],{"class":87},[52,65623,65624,65626,65628,65630,65632,65634,65636,65638],{"class":54,"line":164},[52,65625,64353],{"class":62},[52,65627,80],{"class":79},[52,65629,49288],{"class":62},[52,65631,186],{"class":102},[52,65633,64362],{"class":189},[52,65635,193],{"class":102},[52,65637,64367],{"class":189},[52,65639,211],{"class":102},[52,65641,65642,65644,65646,65648,65651,65653,65655,65657,65659,65661,65663,65665,65667,65669,65671,65673,65675],{"class":54,"line":170},[52,65643,1470],{"class":241},[52,65645,193],{"class":102},[52,65647,1476],{"class":1475},[52,65649,65650],{"class":87},"\"Would rename: ",[52,65652,1482],{"class":121},[52,65654,50879],{"class":189},[52,65656,186],{"class":102},[52,65658,2147],{"class":1320},[52,65660,1488],{"class":121},[52,65662,31507],{"class":87},[52,65664,1482],{"class":121},[52,65666,64384],{"class":189},[52,65668,186],{"class":102},[52,65670,2147],{"class":1320},[52,65672,1488],{"class":121},[52,65674,115],{"class":87},[52,65676,211],{"class":102},[14,65678,65679],{},"This prints the planned changes without actually renaming anything.",[37,65681,65683],{"id":65682},"common-errors-when-renaming-files","Common errors when renaming files",[14,65685,17965],{},[639,65687,65689],{"id":65688},"wrong-folder-path","Wrong folder path",[14,65691,65692,65693,186],{},"If the folder does not exist, you may get a ",[49,65694,15764],{},[14,65696,4259],{},[42,65698,65700],{"className":44,"code":65699,"language":46,"meta":47,"style":47},"from pathlib import Path\n\nfolder = Path(\"missing_folder\")\n\nfor file_path in folder.iterdir():\n    print(file_path)\n",[49,65701,65702,65712,65716,65735,65739,65755],{"__ignoreMap":47},[52,65703,65704,65706,65708,65710],{"class":54,"line":55},[52,65705,25329],{"class":58},[52,65707,28537],{"class":62},[52,65709,59],{"class":58},[52,65711,28542],{"class":62},[52,65713,65714],{"class":54,"line":66},[52,65715,70],{"emptyLinePlaceholder":69},[52,65717,65718,65720,65722,65724,65726,65728,65731,65733],{"class":54,"line":73},[52,65719,64251],{"class":62},[52,65721,80],{"class":79},[52,65723,28563],{"class":189},[52,65725,193],{"class":102},[52,65727,115],{"class":83},[52,65729,65730],{"class":87},"missing_folder",[52,65732,115],{"class":83},[52,65734,211],{"class":102},[52,65736,65737],{"class":54,"line":94},[52,65738,70],{"emptyLinePlaceholder":69},[52,65740,65741,65743,65745,65747,65749,65751,65753],{"class":54,"line":106},[52,65742,12265],{"class":58},[52,65744,49269],{"class":62},[52,65746,11213],{"class":58},[52,65748,65308],{"class":62},[52,65750,186],{"class":102},[52,65752,30202],{"class":189},[52,65754,8146],{"class":102},[52,65756,65757,65759,65761,65763],{"class":54,"line":128},[52,65758,1599],{"class":241},[52,65760,193],{"class":102},[52,65762,50879],{"class":189},[52,65764,211],{"class":102},[14,65766,65767],{},"If you see this kind of error, check:",[309,65769,65770,65773,65776],{},[24,65771,65772],{},"the folder name",[24,65774,65775],{},"the location of your script",[24,65777,48821],{},[14,65779,50651,65780],{},[372,65781,15904],{"href":15903},[639,65783,65785],{"id":65784},"permission-problems","Permission problems",[14,65787,65788,65789,65791],{},"You may get a ",[49,65790,31675],{}," if:",[309,65793,65794,65797,65800],{},[24,65795,65796],{},"the file is open in another program",[24,65798,65799],{},"the folder is protected",[24,65801,65802],{},"your account cannot modify the file",[14,65804,50651,65805],{},[372,65806,65807],{"href":18237},"PermissionError: Permission denied fix",[639,65809,65811],{"id":65810},"using-paths-incorrectly","Using paths incorrectly",[14,65813,65814],{},"Beginners sometimes build paths with plain strings in a way that breaks on different systems.",[14,65816,10294,65817,65819],{},[49,65818,28924],{}," helps avoid that problem.",[14,65821,65822,65823,65825,65826,41227,65828,4441,65830,186],{},"If you use the ",[49,65824,15848],{}," module instead, you usually need tools like ",[49,65827,34748],{},[372,65829,35060],{"href":31969},[372,65831,29945],{"href":29944},[639,65833,65835],{"id":65834},"duplicate-target-names","Duplicate target names",[14,65837,65838],{},"If two files end up with the same new name, the rename may fail or replace another file depending on the operating system.",[14,65840,65841],{},"For example, this is risky:",[42,65843,65845],{"className":44,"code":65844,"language":46,"meta":47,"style":47},"from pathlib import Path\n\nfolder = Path(\"example_folder\")\n\nfor file_path in folder.iterdir():\n    if file_path.is_file():\n        new_path = file_path.with_name(\"same_name.txt\")\n        file_path.rename(new_path)\n",[49,65846,65847,65857,65861,65879,65883,65899,65911,65934],{"__ignoreMap":47},[52,65848,65849,65851,65853,65855],{"class":54,"line":55},[52,65850,25329],{"class":58},[52,65852,28537],{"class":62},[52,65854,59],{"class":58},[52,65856,28542],{"class":62},[52,65858,65859],{"class":54,"line":66},[52,65860,70],{"emptyLinePlaceholder":69},[52,65862,65863,65865,65867,65869,65871,65873,65875,65877],{"class":54,"line":73},[52,65864,64251],{"class":62},[52,65866,80],{"class":79},[52,65868,28563],{"class":189},[52,65870,193],{"class":102},[52,65872,115],{"class":83},[52,65874,64262],{"class":87},[52,65876,115],{"class":83},[52,65878,211],{"class":102},[52,65880,65881],{"class":54,"line":94},[52,65882,70],{"emptyLinePlaceholder":69},[52,65884,65885,65887,65889,65891,65893,65895,65897],{"class":54,"line":106},[52,65886,12265],{"class":58},[52,65888,49269],{"class":62},[52,65890,11213],{"class":58},[52,65892,65308],{"class":62},[52,65894,186],{"class":102},[52,65896,30202],{"class":189},[52,65898,8146],{"class":102},[52,65900,65901,65903,65905,65907,65909],{"class":54,"line":128},[52,65902,1438],{"class":58},[52,65904,49288],{"class":62},[52,65906,186],{"class":102},[52,65908,30216],{"class":189},[52,65910,8146],{"class":102},[52,65912,65913,65915,65917,65919,65921,65923,65925,65927,65930,65932],{"class":54,"line":148},[52,65914,64353],{"class":62},[52,65916,80],{"class":79},[52,65918,49288],{"class":62},[52,65920,186],{"class":102},[52,65922,64362],{"class":189},[52,65924,193],{"class":102},[52,65926,115],{"class":83},[52,65928,65929],{"class":87},"same_name.txt",[52,65931,115],{"class":83},[52,65933,211],{"class":102},[52,65935,65936,65938,65940,65942,65944,65946],{"class":54,"line":164},[52,65937,64374],{"class":62},[52,65939,186],{"class":102},[52,65941,64379],{"class":189},[52,65943,193],{"class":102},[52,65945,64384],{"class":189},[52,65947,211],{"class":102},[14,65949,65950,65951,186],{},"Make sure each new name is unique before calling ",[49,65952,65953],{},"rename()",[639,65955,65957],{"id":65956},"unexpected-rename-order","Unexpected rename order",[14,65959,65960],{},"Folder listings are not always returned in the same order.",[14,65962,65963],{},"If the order matters:",[309,65965,65966,65969],{},[24,65967,65968],{},"sort the file list first",[24,65970,65971],{},"or rename based on the original file names",[37,65973,65975],{"id":65974},"beginner-debugging-checklist","Beginner debugging checklist",[14,65977,65978],{},"If your script does not work, try printing values before the rename step.",[14,65980,31631],{},[42,65982,65984],{"className":44,"code":65983,"language":46,"meta":47,"style":47},"print(folder)\nprint(file_path)\nprint(file_path.is_file())\nprint(file_path.name)\nprint(file_path.suffix)\nprint(new_path)\n",[49,65985,65986,65996,66006,66020,66034,66048],{"__ignoreMap":47},[52,65987,65988,65990,65992,65994],{"class":54,"line":55},[52,65989,242],{"class":241},[52,65991,193],{"class":102},[52,65993,64290],{"class":189},[52,65995,211],{"class":102},[52,65997,65998,66000,66002,66004],{"class":54,"line":66},[52,65999,242],{"class":241},[52,66001,193],{"class":102},[52,66003,50879],{"class":189},[52,66005,211],{"class":102},[52,66007,66008,66010,66012,66014,66016,66018],{"class":54,"line":73},[52,66009,242],{"class":241},[52,66011,193],{"class":102},[52,66013,50879],{"class":189},[52,66015,186],{"class":102},[52,66017,30216],{"class":189},[52,66019,1816],{"class":102},[52,66021,66022,66024,66026,66028,66030,66032],{"class":54,"line":94},[52,66023,242],{"class":241},[52,66025,193],{"class":102},[52,66027,50879],{"class":189},[52,66029,186],{"class":102},[52,66031,2147],{"class":1320},[52,66033,211],{"class":102},[52,66035,66036,66038,66040,66042,66044,66046],{"class":54,"line":106},[52,66037,242],{"class":241},[52,66039,193],{"class":102},[52,66041,50879],{"class":189},[52,66043,186],{"class":102},[52,66045,30232],{"class":1320},[52,66047,211],{"class":102},[52,66049,66050,66052,66054,66056],{"class":54,"line":128},[52,66051,242],{"class":241},[52,66053,193],{"class":102},[52,66055,64384],{"class":189},[52,66057,211],{"class":102},[14,66059,66060],{},"These help you confirm:",[309,66062,66063,66066,66069,66072,66075],{},[24,66064,66065],{},"the folder path is correct",[24,66067,66068],{},"each item is really a file",[24,66070,66071],{},"the file name is what you expect",[24,66073,66074],{},"the extension is being kept",[24,66076,66077],{},"the new target path looks correct",[14,66079,66080],{},"A good beginner workflow:",[21,66082,66083,66086,66089,66094,66097],{},[24,66084,66085],{},"Try with 2 or 3 test files",[24,66087,66088],{},"Print each file name before renaming",[24,66090,66091,66092],{},"Print the new name before calling ",[49,66093,65953],{},[24,66095,66096],{},"Confirm folders are being skipped",[24,66098,66099],{},"Run the real rename only after the preview looks correct",[37,66101,1942],{"id":1941},[639,66103,66105],{"id":66104},"how-do-i-keep-the-file-extension-when-renaming","How do I keep the file extension when renaming?",[14,66107,3650,66108,66110],{},[49,66109,64671],{}," and add it to the new name.",[14,66112,4259],{},[42,66114,66115],{"className":44,"code":64740,"language":46,"meta":47,"style":47},[49,66116,66117],{"__ignoreMap":47},[52,66118,66119,66121,66123,66125,66127,66129,66131,66133,66135,66137,66139,66141],{"class":54,"line":55},[52,66120,64747],{"class":62},[52,66122,80],{"class":79},[52,66124,6126],{"class":1475},[52,66126,64330],{"class":87},[52,66128,1482],{"class":121},[52,66130,64335],{"class":62},[52,66132,64338],{"class":121},[52,66134,50879],{"class":62},[52,66136,186],{"class":102},[52,66138,30232],{"class":1320},[52,66140,1488],{"class":121},[52,66142,91],{"class":87},[639,66144,66146,66147,66149],{"id":66145},"can-i-rename-only-txt-files","Can I rename only ",[49,66148,30125],{}," files?",[14,66151,66152],{},"Yes. Check the suffix before renaming.",[42,66154,66156],{"className":44,"code":66155,"language":46,"meta":47,"style":47},"from pathlib import Path\n\nfolder = Path(\"example_folder\")\n\nfor index, file_path in enumerate(folder.iterdir(), start=1):\n    if file_path.is_file() and file_path.suffix == \".txt\":\n        new_name = f\"file_{index}{file_path.suffix}\"\n        new_path = file_path.with_name(new_name)\n        file_path.rename(new_path)\n        print(f\"Renamed: {file_path.name} -> {new_path.name}\")\n",[49,66157,66158,66168,66172,66190,66194,66226,66256,66282,66300,66314],{"__ignoreMap":47},[52,66159,66160,66162,66164,66166],{"class":54,"line":55},[52,66161,25329],{"class":58},[52,66163,28537],{"class":62},[52,66165,59],{"class":58},[52,66167,28542],{"class":62},[52,66169,66170],{"class":54,"line":66},[52,66171,70],{"emptyLinePlaceholder":69},[52,66173,66174,66176,66178,66180,66182,66184,66186,66188],{"class":54,"line":73},[52,66175,64251],{"class":62},[52,66177,80],{"class":79},[52,66179,28563],{"class":189},[52,66181,193],{"class":102},[52,66183,115],{"class":83},[52,66185,64262],{"class":87},[52,66187,115],{"class":83},[52,66189,211],{"class":102},[52,66191,66192],{"class":54,"line":94},[52,66193,70],{"emptyLinePlaceholder":69},[52,66195,66196,66198,66200,66202,66204,66206,66208,66210,66212,66214,66216,66218,66220,66222,66224],{"class":54,"line":106},[52,66197,12265],{"class":58},[52,66199,64277],{"class":62},[52,66201,199],{"class":102},[52,66203,49269],{"class":62},[52,66205,11213],{"class":58},[52,66207,49274],{"class":241},[52,66209,193],{"class":102},[52,66211,64290],{"class":189},[52,66213,186],{"class":102},[52,66215,30202],{"class":189},[52,66217,64297],{"class":102},[52,66219,64300],{"class":202},[52,66221,80],{"class":79},[52,66223,9200],{"class":121},[52,66225,3114],{"class":102},[52,66227,66228,66230,66232,66234,66236,66238,66240,66242,66244,66246,66248,66250,66252,66254],{"class":54,"line":128},[52,66229,1438],{"class":58},[52,66231,49288],{"class":62},[52,66233,186],{"class":102},[52,66235,30216],{"class":189},[52,66237,8267],{"class":102},[52,66239,1453],{"class":79},[52,66241,49288],{"class":62},[52,66243,186],{"class":102},[52,66245,30232],{"class":1320},[52,66247,1324],{"class":79},[52,66249,84],{"class":83},[52,66251,30125],{"class":87},[52,66253,115],{"class":83},[52,66255,1330],{"class":102},[52,66257,66258,66260,66262,66264,66266,66268,66270,66272,66274,66276,66278,66280],{"class":54,"line":148},[52,66259,64323],{"class":62},[52,66261,80],{"class":79},[52,66263,6126],{"class":1475},[52,66265,64330],{"class":87},[52,66267,1482],{"class":121},[52,66269,64335],{"class":62},[52,66271,64338],{"class":121},[52,66273,50879],{"class":62},[52,66275,186],{"class":102},[52,66277,30232],{"class":1320},[52,66279,1488],{"class":121},[52,66281,91],{"class":87},[52,66283,66284,66286,66288,66290,66292,66294,66296,66298],{"class":54,"line":164},[52,66285,64353],{"class":62},[52,66287,80],{"class":79},[52,66289,49288],{"class":62},[52,66291,186],{"class":102},[52,66293,64362],{"class":189},[52,66295,193],{"class":102},[52,66297,64367],{"class":189},[52,66299,211],{"class":102},[52,66301,66302,66304,66306,66308,66310,66312],{"class":54,"line":170},[52,66303,64374],{"class":62},[52,66305,186],{"class":102},[52,66307,64379],{"class":189},[52,66309,193],{"class":102},[52,66311,64384],{"class":189},[52,66313,211],{"class":102},[52,66315,66316,66318,66320,66322,66324,66326,66328,66330,66332,66334,66336,66338,66340,66342,66344,66346,66348],{"class":54,"line":175},[52,66317,1470],{"class":241},[52,66319,193],{"class":102},[52,66321,1476],{"class":1475},[52,66323,64397],{"class":87},[52,66325,1482],{"class":121},[52,66327,50879],{"class":189},[52,66329,186],{"class":102},[52,66331,2147],{"class":1320},[52,66333,1488],{"class":121},[52,66335,31507],{"class":87},[52,66337,1482],{"class":121},[52,66339,64384],{"class":189},[52,66341,186],{"class":102},[52,66343,2147],{"class":1320},[52,66345,1488],{"class":121},[52,66347,115],{"class":87},[52,66349,211],{"class":102},[639,66351,66353],{"id":66352},"will-this-rename-files-inside-subfolders-too","Will this rename files inside subfolders too?",[14,66355,66356],{},"No. This example only works on files directly inside one folder.",[639,66358,66360],{"id":66359},"what-if-two-files-get-the-same-new-name","What if two files get the same new name?",[14,66362,66363],{},"The rename can fail or overwrite depending on the system. Generate unique names first.",[639,66365,3638,66367,857,66369,66371],{"id":66366},"should-i-use-os-or-pathlib-for-this",[49,66368,15848],{},[49,66370,28924],{}," for this?",[14,66373,5339,66374,66376],{},[49,66375,28924],{}," is usually easier to read and safer to work with.",[37,66378,2005],{"id":2004},[309,66380,66381,66385,66389,66393,66397,66401],{},[24,66382,66383],{},[372,66384,31983],{"href":65479},[24,66386,66387],{},[372,66388,31956],{"href":35868},[24,66390,66391],{},[372,66392,29931],{"href":28722},[24,66394,66395],{},[372,66396,29945],{"href":29944},[24,66398,66399],{},[372,66400,15904],{"href":15903},[24,66402,66403],{},[372,66404,65807],{"href":18237},[14,66406,66407],{},"Try the script in a small test folder first. Once that works, move on to learning more about listing files, handling paths, and fixing common file-related errors.",[2034,66409,66410],{},"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 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 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);}",{"title":47,"searchDepth":66,"depth":66,"links":66412},[66413,66414,66415,66419,66424,66428,66431,66438,66439,66448],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":25640,"depth":66,"text":25641,"children":66416},[66417],{"id":64691,"depth":73,"text":66418},"Why suffix matters",{"id":64782,"depth":66,"text":64783,"children":66420},[66421,66422,66423],{"id":49,"depth":73,"text":38262},{"id":64972,"depth":73,"text":64973},{"id":65002,"depth":73,"text":65003},{"id":65228,"depth":66,"text":65229,"children":66425},[66426,66427],{"id":65256,"depth":73,"text":38262},{"id":65420,"depth":73,"text":65421},{"id":65482,"depth":66,"text":65483,"children":66429},[66430],{"id":65509,"depth":73,"text":65510},{"id":65682,"depth":66,"text":65683,"children":66432},[66433,66434,66435,66436,66437],{"id":65688,"depth":73,"text":65689},{"id":65784,"depth":73,"text":65785},{"id":65810,"depth":73,"text":65811},{"id":65834,"depth":73,"text":65835},{"id":65956,"depth":73,"text":65957},{"id":65974,"depth":66,"text":65975},{"id":1941,"depth":66,"text":1942,"children":66440},[66441,66442,66444,66445,66446],{"id":66104,"depth":73,"text":66105},{"id":66145,"depth":73,"text":66443},"Can I rename only .txt files?",{"id":66352,"depth":73,"text":66353},{"id":66359,"depth":73,"text":66360},{"id":66366,"depth":73,"text":66447},"Should I use os or pathlib for this?",{"id":2004,"depth":66,"text":2005},"Master python rename files in a folder example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-rename-files-in-a-folder-example",{"title":64192,"description":66449},"examples\u002Fpython-rename-files-in-a-folder-example","36Q6NocpcXP_vQnxIq2W-WXqvCVNtVAX_SAR2hODYcE",{"id":66456,"title":66457,"body":66458,"description":68037,"extension":2075,"meta":68038,"navigation":69,"path":68039,"seo":68040,"stem":68041,"__hash__":68042},"content\u002Fexamples\u002Fpython-search-algorithm-example-linear-search.md","Python Search Algorithm Example (Linear Search)",{"type":7,"value":66459,"toc":68009},[66460,66463,66466,66469,66471,66603,66607,66615,66622,66624,66638,66642,66659,66670,66674,66677,66835,66839,66858,66860,66886,66897,66901,66904,67066,67070,67085,67090,67098,67163,67167,67178,67181,67333,67337,67346,67350,67355,67371,67373,67439,67443,67458,67460,67480,67487,67491,67494,67508,67514,67517,67519,67522,67529,67532,67600,67606,67615,67682,67686,67693,67696,67729,67741,67747,67749,67766,67771,67788,67792,67795,67798,67811,67815,67818,67830,67832,67909,67913,67928,67930,67934,67939,67943,67946,67952,67957,67961,67964,67968,67971,67973,68006],[10,66461,66457],{"id":66462},"python-search-algorithm-example-linear-search",[14,66464,66465],{},"Linear search is one of the simplest ways to find a value in Python.",[14,66467,66468],{},"It works by checking items one by one from left to right until it finds a match. This makes it a great beginner example because it uses basic ideas like loops, comparisons, and return values.",[37,66470,40],{"id":39},[42,66472,66474],{"className":44,"code":66473,"language":46,"meta":47,"style":47},"def linear_search(items, target):\n    for index, value in enumerate(items):\n        if value == target:\n            return index\n    return -1\n\nnumbers = [4, 7, 1, 9, 3]\nresult = linear_search(numbers, 9)\nprint(result)\n",[49,66475,66476,66494,66514,66526,66533,66542,66546,66575,66593],{"__ignoreMap":47},[52,66477,66478,66480,66483,66485,66487,66489,66492],{"class":54,"line":55},[52,66479,7410],{"class":1475},[52,66481,66482],{"class":7065}," linear_search",[52,66484,193],{"class":102},[52,66486,11220],{"class":7018},[52,66488,199],{"class":102},[52,66490,66491],{"class":7018}," target",[52,66493,3114],{"class":102},[52,66495,66496,66498,66500,66502,66504,66506,66508,66510,66512],{"class":54,"line":66},[52,66497,11773],{"class":58},[52,66499,64277],{"class":62},[52,66501,199],{"class":102},[52,66503,23523],{"class":62},[52,66505,11213],{"class":58},[52,66507,49274],{"class":241},[52,66509,193],{"class":102},[52,66511,11220],{"class":189},[52,66513,3114],{"class":102},[52,66515,66516,66518,66520,66522,66524],{"class":54,"line":73},[52,66517,11168],{"class":58},[52,66519,23523],{"class":62},[52,66521,11062],{"class":79},[52,66523,66491],{"class":62},[52,66525,1330],{"class":102},[52,66527,66528,66530],{"class":54,"line":94},[52,66529,35431],{"class":58},[52,66531,66532],{"class":62}," index\n",[52,66534,66535,66537,66539],{"class":54,"line":106},[52,66536,20958],{"class":58},[52,66538,140],{"class":79},[52,66540,66541],{"class":121},"1\n",[52,66543,66544],{"class":54,"line":128},[52,66545,70],{"emptyLinePlaceholder":69},[52,66547,66548,66550,66552,66554,66556,66558,66560,66562,66564,66566,66569,66571,66573],{"class":54,"line":148},[52,66549,32092],{"class":62},[52,66551,80],{"class":79},[52,66553,18162],{"class":102},[52,66555,11366],{"class":121},[52,66557,199],{"class":102},[52,66559,44338],{"class":121},[52,66561,199],{"class":102},[52,66563,1518],{"class":121},[52,66565,199],{"class":102},[52,66567,66568],{"class":121}," 9",[52,66570,199],{"class":102},[52,66572,5261],{"class":121},[52,66574,1147],{"class":102},[52,66576,66577,66579,66581,66583,66585,66587,66589,66591],{"class":54,"line":164},[52,66578,32778],{"class":62},[52,66580,80],{"class":79},[52,66582,66482],{"class":189},[52,66584,193],{"class":102},[52,66586,5324],{"class":189},[52,66588,199],{"class":102},[52,66590,66568],{"class":121},[52,66592,211],{"class":102},[52,66594,66595,66597,66599,66601],{"class":54,"line":170},[52,66596,242],{"class":241},[52,66598,193],{"class":102},[52,66600,32811],{"class":189},[52,66602,211],{"class":102},[14,66604,66605],{},[5503,66606,5267],{},[42,66608,66609],{"className":44,"code":7184,"language":46,"meta":47,"style":47},[49,66610,66611],{"__ignoreMap":47},[52,66612,66613],{"class":54,"line":55},[52,66614,7184],{"class":121},[14,66616,66617,66618,66621],{},"This function returns the index of the target if found. It returns ",[49,66619,66620],{},"-1"," if the target is not in the list.",[37,66623,7216],{"id":7215},[309,66625,66626,66629,66632,66635],{},[24,66627,66628],{},"Linear search checks each item from left to right",[24,66630,66631],{},"It stops when it finds a match",[24,66633,66634],{},"It works well for small lists or simple beginner programs",[24,66636,66637],{},"It does not require the list to be sorted",[37,66639,66641],{"id":66640},"how-linear-search-works-step-by-step","How linear search works step by step",[309,66643,66644,66647,66650,66653,66656],{},[24,66645,66646],{},"Start at the first item in the list",[24,66648,66649],{},"Compare the current item to the target value",[24,66651,66652],{},"If they match, return the position",[24,66654,66655],{},"If they do not match, move to the next item",[24,66657,66658],{},"If the loop ends, the target was not found",[14,66660,66661,66662,759,66666,186],{},"This pattern is a good way to practice ",[372,66663,10646,66664,13139],{"href":32073},[49,66665,12265],{},[372,66667,10646,66668,30419],{"href":32078},[49,66669,1313],{},[37,66671,66673],{"id":66672},"example-find-a-number-in-a-list","Example: find a number in a list",[14,66675,66676],{},"A simple way to write linear search is to put it in a function. That lets you reuse the same logic with different lists and target values.",[42,66678,66680],{"className":44,"code":66679,"language":46,"meta":47,"style":47},"def linear_search(items, target):\n    for index, value in enumerate(items):\n        if value == target:\n            return index\n    return -1\n\nnumbers = [4, 7, 1, 9, 3]\n\nprint(linear_search(numbers, 9))\nprint(linear_search(numbers, 3))\nprint(linear_search(numbers, 8))\n",[49,66681,66682,66698,66718,66730,66736,66744,66748,66776,66780,66799,66817],{"__ignoreMap":47},[52,66683,66684,66686,66688,66690,66692,66694,66696],{"class":54,"line":55},[52,66685,7410],{"class":1475},[52,66687,66482],{"class":7065},[52,66689,193],{"class":102},[52,66691,11220],{"class":7018},[52,66693,199],{"class":102},[52,66695,66491],{"class":7018},[52,66697,3114],{"class":102},[52,66699,66700,66702,66704,66706,66708,66710,66712,66714,66716],{"class":54,"line":66},[52,66701,11773],{"class":58},[52,66703,64277],{"class":62},[52,66705,199],{"class":102},[52,66707,23523],{"class":62},[52,66709,11213],{"class":58},[52,66711,49274],{"class":241},[52,66713,193],{"class":102},[52,66715,11220],{"class":189},[52,66717,3114],{"class":102},[52,66719,66720,66722,66724,66726,66728],{"class":54,"line":73},[52,66721,11168],{"class":58},[52,66723,23523],{"class":62},[52,66725,11062],{"class":79},[52,66727,66491],{"class":62},[52,66729,1330],{"class":102},[52,66731,66732,66734],{"class":54,"line":94},[52,66733,35431],{"class":58},[52,66735,66532],{"class":62},[52,66737,66738,66740,66742],{"class":54,"line":106},[52,66739,20958],{"class":58},[52,66741,140],{"class":79},[52,66743,66541],{"class":121},[52,66745,66746],{"class":54,"line":128},[52,66747,70],{"emptyLinePlaceholder":69},[52,66749,66750,66752,66754,66756,66758,66760,66762,66764,66766,66768,66770,66772,66774],{"class":54,"line":148},[52,66751,32092],{"class":62},[52,66753,80],{"class":79},[52,66755,18162],{"class":102},[52,66757,11366],{"class":121},[52,66759,199],{"class":102},[52,66761,44338],{"class":121},[52,66763,199],{"class":102},[52,66765,1518],{"class":121},[52,66767,199],{"class":102},[52,66769,66568],{"class":121},[52,66771,199],{"class":102},[52,66773,5261],{"class":121},[52,66775,1147],{"class":102},[52,66777,66778],{"class":54,"line":164},[52,66779,70],{"emptyLinePlaceholder":69},[52,66781,66782,66784,66786,66789,66791,66793,66795,66797],{"class":54,"line":170},[52,66783,242],{"class":241},[52,66785,193],{"class":102},[52,66787,66788],{"class":189},"linear_search",[52,66790,193],{"class":102},[52,66792,5324],{"class":189},[52,66794,199],{"class":102},[52,66796,66568],{"class":121},[52,66798,8886],{"class":102},[52,66800,66801,66803,66805,66807,66809,66811,66813,66815],{"class":54,"line":175},[52,66802,242],{"class":241},[52,66804,193],{"class":102},[52,66806,66788],{"class":189},[52,66808,193],{"class":102},[52,66810,5324],{"class":189},[52,66812,199],{"class":102},[52,66814,5261],{"class":121},[52,66816,8886],{"class":102},[52,66818,66819,66821,66823,66825,66827,66829,66831,66833],{"class":54,"line":214},[52,66820,242],{"class":241},[52,66822,193],{"class":102},[52,66824,66788],{"class":189},[52,66826,193],{"class":102},[52,66828,5324],{"class":189},[52,66830,199],{"class":102},[52,66832,32757],{"class":121},[52,66834,8886],{"class":102},[14,66836,66837],{},[5503,66838,5267],{},[42,66840,66842],{"className":44,"code":66841,"language":46,"meta":47,"style":47},"3\n4\n-1\n",[49,66843,66844,66848,66852],{"__ignoreMap":47},[52,66845,66846],{"class":54,"line":55},[52,66847,7184],{"class":121},[52,66849,66850],{"class":54,"line":66},[52,66851,8831],{"class":121},[52,66853,66854,66856],{"class":54,"line":73},[52,66855,5889],{"class":79},[52,66857,66541],{"class":121},[639,66859,4101],{"id":4100},[309,66861,66862,66868,66874,66880],{},[24,66863,66864,66867],{},[49,66865,66866],{},"enumerate(items)"," gives both the index and the value",[24,66869,66870,66873],{},[49,66871,66872],{},"if value == target:"," checks whether the current item matches",[24,66875,66876,66879],{},[49,66877,66878],{},"return index"," stops the function as soon as the target is found",[24,66881,66882,66885],{},[49,66883,66884],{},"return -1"," runs only if the loop finishes without finding a match",[14,66887,7526,66888,66891,66892,186],{},[49,66889,66890],{},"enumerate()"," is new to you, see ",[372,66893,10646,66895,2866],{"href":66894},"\u002Freference\u002Fpython-enumerate-function-explained\u002F",[49,66896,66890],{},[37,66898,66900],{"id":66899},"example-find-text-in-a-list-of-strings","Example: find text in a list of strings",[14,66902,66903],{},"Linear search works with strings too.",[42,66905,66907],{"className":44,"code":66906,"language":46,"meta":47,"style":47},"def linear_search(items, target):\n    for index, value in enumerate(items):\n        if value == target:\n            return index\n    return -1\n\nwords = [\"apple\", \"banana\", \"grape\", \"orange\"]\n\nprint(linear_search(words, \"grape\"))\nprint(linear_search(words, \"pear\"))\n",[49,66908,66909,66925,66945,66957,66963,66971,66975,67016,67020,67043],{"__ignoreMap":47},[52,66910,66911,66913,66915,66917,66919,66921,66923],{"class":54,"line":55},[52,66912,7410],{"class":1475},[52,66914,66482],{"class":7065},[52,66916,193],{"class":102},[52,66918,11220],{"class":7018},[52,66920,199],{"class":102},[52,66922,66491],{"class":7018},[52,66924,3114],{"class":102},[52,66926,66927,66929,66931,66933,66935,66937,66939,66941,66943],{"class":54,"line":66},[52,66928,11773],{"class":58},[52,66930,64277],{"class":62},[52,66932,199],{"class":102},[52,66934,23523],{"class":62},[52,66936,11213],{"class":58},[52,66938,49274],{"class":241},[52,66940,193],{"class":102},[52,66942,11220],{"class":189},[52,66944,3114],{"class":102},[52,66946,66947,66949,66951,66953,66955],{"class":54,"line":73},[52,66948,11168],{"class":58},[52,66950,23523],{"class":62},[52,66952,11062],{"class":79},[52,66954,66491],{"class":62},[52,66956,1330],{"class":102},[52,66958,66959,66961],{"class":54,"line":94},[52,66960,35431],{"class":58},[52,66962,66532],{"class":62},[52,66964,66965,66967,66969],{"class":54,"line":106},[52,66966,20958],{"class":58},[52,66968,140],{"class":79},[52,66970,66541],{"class":121},[52,66972,66973],{"class":54,"line":128},[52,66974,70],{"emptyLinePlaceholder":69},[52,66976,66977,66979,66981,66983,66985,66987,66989,66991,66993,66995,66997,66999,67001,67004,67006,67008,67010,67012,67014],{"class":54,"line":148},[52,66978,32960],{"class":62},[52,66980,80],{"class":79},[52,66982,18162],{"class":102},[52,66984,115],{"class":83},[52,66986,33028],{"class":87},[52,66988,115],{"class":83},[52,66990,199],{"class":102},[52,66992,84],{"class":83},[52,66994,32978],{"class":87},[52,66996,115],{"class":83},[52,66998,199],{"class":102},[52,67000,84],{"class":83},[52,67002,67003],{"class":87},"grape",[52,67005,115],{"class":83},[52,67007,199],{"class":102},[52,67009,84],{"class":83},[52,67011,32996],{"class":87},[52,67013,115],{"class":83},[52,67015,1147],{"class":102},[52,67017,67018],{"class":54,"line":164},[52,67019,70],{"emptyLinePlaceholder":69},[52,67021,67022,67024,67026,67028,67030,67033,67035,67037,67039,67041],{"class":54,"line":170},[52,67023,242],{"class":241},[52,67025,193],{"class":102},[52,67027,66788],{"class":189},[52,67029,193],{"class":102},[52,67031,67032],{"class":189},"words",[52,67034,199],{"class":102},[52,67036,84],{"class":83},[52,67038,67003],{"class":87},[52,67040,115],{"class":83},[52,67042,8886],{"class":102},[52,67044,67045,67047,67049,67051,67053,67055,67057,67059,67062,67064],{"class":54,"line":175},[52,67046,242],{"class":241},[52,67048,193],{"class":102},[52,67050,66788],{"class":189},[52,67052,193],{"class":102},[52,67054,67032],{"class":189},[52,67056,199],{"class":102},[52,67058,84],{"class":83},[52,67060,67061],{"class":87},"pear",[52,67063,115],{"class":83},[52,67065,8886],{"class":102},[14,67067,67068],{},[5503,67069,5267],{},[42,67071,67073],{"className":44,"code":67072,"language":46,"meta":47,"style":47},"2\n-1\n",[49,67074,67075,67079],{"__ignoreMap":47},[52,67076,67077],{"class":54,"line":55},[52,67078,44748],{"class":121},[52,67080,67081,67083],{"class":54,"line":66},[52,67082,5889],{"class":79},[52,67084,66541],{"class":121},[14,67086,67087,67088,186],{},"The comparison still uses ",[49,67089,11062],{},[14,67091,67092,67093,759,67095,67097],{},"Be careful with uppercase and lowercase letters. For example, ",[49,67094,63501],{},[49,67096,63504],{}," are not equal.",[42,67099,67101],{"className":44,"code":67100,"language":46,"meta":47,"style":47},"words = [\"Apple\", \"Banana\", \"Grape\"]\n\nprint(linear_search(words, \"apple\"))\n",[49,67102,67103,67137,67141],{"__ignoreMap":47},[52,67104,67105,67107,67109,67111,67113,67115,67117,67119,67121,67124,67126,67128,67130,67133,67135],{"class":54,"line":55},[52,67106,32960],{"class":62},[52,67108,80],{"class":79},[52,67110,18162],{"class":102},[52,67112,115],{"class":83},[52,67114,63228],{"class":87},[52,67116,115],{"class":83},[52,67118,199],{"class":102},[52,67120,84],{"class":83},[52,67122,67123],{"class":87},"Banana",[52,67125,115],{"class":83},[52,67127,199],{"class":102},[52,67129,84],{"class":83},[52,67131,67132],{"class":87},"Grape",[52,67134,115],{"class":83},[52,67136,1147],{"class":102},[52,67138,67139],{"class":54,"line":66},[52,67140,70],{"emptyLinePlaceholder":69},[52,67142,67143,67145,67147,67149,67151,67153,67155,67157,67159,67161],{"class":54,"line":73},[52,67144,242],{"class":241},[52,67146,193],{"class":102},[52,67148,66788],{"class":189},[52,67150,193],{"class":102},[52,67152,67032],{"class":189},[52,67154,199],{"class":102},[52,67156,84],{"class":83},[52,67158,33028],{"class":87},[52,67160,115],{"class":83},[52,67162,8886],{"class":102},[14,67164,67165],{},[5503,67166,5267],{},[42,67168,67170],{"className":44,"code":67169,"language":46,"meta":47,"style":47},"-1\n",[49,67171,67172],{"__ignoreMap":47},[52,67173,67174,67176],{"class":54,"line":55},[52,67175,5889],{"class":79},[52,67177,66541],{"class":121},[14,67179,67180],{},"If you want case-insensitive matching, you can convert both values to lowercase first:",[42,67182,67184],{"className":44,"code":67183,"language":46,"meta":47,"style":47},"def linear_search_case_insensitive(items, target):\n    target = target.lower()\n\n    for index, value in enumerate(items):\n        if value.lower() == target:\n            return index\n    return -1\n\nwords = [\"Apple\", \"Banana\", \"Grape\"]\nprint(linear_search_case_insensitive(words, \"apple\"))\n",[49,67185,67186,67203,67218,67222,67242,67260,67266,67274,67278,67310],{"__ignoreMap":47},[52,67187,67188,67190,67193,67195,67197,67199,67201],{"class":54,"line":55},[52,67189,7410],{"class":1475},[52,67191,67192],{"class":7065}," linear_search_case_insensitive",[52,67194,193],{"class":102},[52,67196,11220],{"class":7018},[52,67198,199],{"class":102},[52,67200,66491],{"class":7018},[52,67202,3114],{"class":102},[52,67204,67205,67208,67210,67212,67214,67216],{"class":54,"line":66},[52,67206,67207],{"class":62},"    target ",[52,67209,80],{"class":79},[52,67211,66491],{"class":62},[52,67213,186],{"class":102},[52,67215,30237],{"class":189},[52,67217,230],{"class":102},[52,67219,67220],{"class":54,"line":73},[52,67221,70],{"emptyLinePlaceholder":69},[52,67223,67224,67226,67228,67230,67232,67234,67236,67238,67240],{"class":54,"line":94},[52,67225,11773],{"class":58},[52,67227,64277],{"class":62},[52,67229,199],{"class":102},[52,67231,23523],{"class":62},[52,67233,11213],{"class":58},[52,67235,49274],{"class":241},[52,67237,193],{"class":102},[52,67239,11220],{"class":189},[52,67241,3114],{"class":102},[52,67243,67244,67246,67248,67250,67252,67254,67256,67258],{"class":54,"line":106},[52,67245,11168],{"class":58},[52,67247,23547],{"class":62},[52,67249,186],{"class":102},[52,67251,30237],{"class":189},[52,67253,8267],{"class":102},[52,67255,1324],{"class":79},[52,67257,66491],{"class":62},[52,67259,1330],{"class":102},[52,67261,67262,67264],{"class":54,"line":128},[52,67263,35431],{"class":58},[52,67265,66532],{"class":62},[52,67267,67268,67270,67272],{"class":54,"line":148},[52,67269,20958],{"class":58},[52,67271,140],{"class":79},[52,67273,66541],{"class":121},[52,67275,67276],{"class":54,"line":164},[52,67277,70],{"emptyLinePlaceholder":69},[52,67279,67280,67282,67284,67286,67288,67290,67292,67294,67296,67298,67300,67302,67304,67306,67308],{"class":54,"line":170},[52,67281,32960],{"class":62},[52,67283,80],{"class":79},[52,67285,18162],{"class":102},[52,67287,115],{"class":83},[52,67289,63228],{"class":87},[52,67291,115],{"class":83},[52,67293,199],{"class":102},[52,67295,84],{"class":83},[52,67297,67123],{"class":87},[52,67299,115],{"class":83},[52,67301,199],{"class":102},[52,67303,84],{"class":83},[52,67305,67132],{"class":87},[52,67307,115],{"class":83},[52,67309,1147],{"class":102},[52,67311,67312,67314,67316,67319,67321,67323,67325,67327,67329,67331],{"class":54,"line":175},[52,67313,242],{"class":241},[52,67315,193],{"class":102},[52,67317,67318],{"class":189},"linear_search_case_insensitive",[52,67320,193],{"class":102},[52,67322,67032],{"class":189},[52,67324,199],{"class":102},[52,67326,84],{"class":83},[52,67328,33028],{"class":87},[52,67330,115],{"class":83},[52,67332,8886],{"class":102},[14,67334,67335],{},[5503,67336,5267],{},[42,67338,67340],{"className":44,"code":67339,"language":46,"meta":47,"style":47},"0\n",[49,67341,67342],{"__ignoreMap":47},[52,67343,67344],{"class":54,"line":55},[52,67345,67339],{"class":121},[37,67347,67349],{"id":67348},"how-to-understand-the-result","How to understand the result",[14,67351,67352,67353,186],{},"The function in this example returns an ",[5503,67354,64335],{},[309,67356,67357,67360,67365],{},[24,67358,67359],{},"A returned index means the item was found",[24,67361,67362,67364],{},[49,67363,66620],{}," is a common \"not found\" value in simple examples",[24,67366,67367,67368,67370],{},"Some programs may return ",[49,67369,18849],{}," instead",[14,67372,16291],{},[42,67374,67376],{"className":44,"code":67375,"language":46,"meta":47,"style":47},"numbers = [10, 20, 30]\n\nprint(linear_search(numbers, 20))\nprint(linear_search(numbers, 99))\n",[49,67377,67378,67398,67402,67420],{"__ignoreMap":47},[52,67379,67380,67382,67384,67386,67388,67390,67392,67394,67396],{"class":54,"line":55},[52,67381,32092],{"class":62},[52,67383,80],{"class":79},[52,67385,18162],{"class":102},[52,67387,3373],{"class":121},[52,67389,199],{"class":102},[52,67391,32771],{"class":121},[52,67393,199],{"class":102},[52,67395,16696],{"class":121},[52,67397,1147],{"class":102},[52,67399,67400],{"class":54,"line":66},[52,67401,70],{"emptyLinePlaceholder":69},[52,67403,67404,67406,67408,67410,67412,67414,67416,67418],{"class":54,"line":73},[52,67405,242],{"class":241},[52,67407,193],{"class":102},[52,67409,66788],{"class":189},[52,67411,193],{"class":102},[52,67413,5324],{"class":189},[52,67415,199],{"class":102},[52,67417,32771],{"class":121},[52,67419,8886],{"class":102},[52,67421,67422,67424,67426,67428,67430,67432,67434,67437],{"class":54,"line":94},[52,67423,242],{"class":241},[52,67425,193],{"class":102},[52,67427,66788],{"class":189},[52,67429,193],{"class":102},[52,67431,5324],{"class":189},[52,67433,199],{"class":102},[52,67435,67436],{"class":121}," 99",[52,67438,8886],{"class":102},[14,67440,67441],{},[5503,67442,5267],{},[42,67444,67446],{"className":44,"code":67445,"language":46,"meta":47,"style":47},"1\n-1\n",[49,67447,67448,67452],{"__ignoreMap":47},[52,67449,67450],{"class":54,"line":55},[52,67451,66541],{"class":121},[52,67453,67454,67456],{"class":54,"line":66},[52,67455,5889],{"class":79},[52,67457,66541],{"class":121},[14,67459,22954],{},[309,67461,67462,67472],{},[24,67463,67464,24324,67466,67469,67470],{},[49,67465,9200],{},[49,67467,67468],{},"20"," is at index ",[49,67471,9200],{},[24,67473,67474,24324,67476,67479],{},[49,67475,66620],{},[49,67477,67478],{},"99"," was not found",[14,67481,67482,67483,186],{},"If you want more practice finding values in lists, see ",[372,67484,67486],{"href":67485},"\u002Fhow-to\u002Fhow-to-find-an-item-in-a-list-in-python\u002F","how to find an item in a list in Python",[37,67488,67490],{"id":67489},"when-to-use-linear-search","When to use linear search",[14,67492,67493],{},"Linear search is useful when:",[309,67495,67496,67499,67502,67505],{},[24,67497,67498],{},"You have a small list",[24,67500,67501],{},"You are learning loops and conditionals",[24,67503,67504],{},"Your data is not sorted",[24,67506,67507],{},"You want a simple, easy-to-read solution",[14,67509,67510,67511,67513],{},"It is usually ",[5503,67512,20858],{}," the best choice for very large lists because it may need to check every item.",[14,67515,67516],{},"For beginners, though, it is a very helpful example because it shows how programs examine data step by step.",[37,67518,6402],{"id":6401},[14,67520,67521],{},"Here are some common problems when writing linear search.",[639,67523,67525,67526,67528],{"id":67524},"returning-1-too-early","Returning ",[49,67527,66620],{}," too early",[14,67530,67531],{},"This is a very common bug:",[42,67533,67535],{"className":44,"code":67534,"language":46,"meta":47,"style":47},"def linear_search(items, target):\n    for index, value in enumerate(items):\n        if value == target:\n            return index\n        return -1\n",[49,67536,67537,67553,67573,67585,67591],{"__ignoreMap":47},[52,67538,67539,67541,67543,67545,67547,67549,67551],{"class":54,"line":55},[52,67540,7410],{"class":1475},[52,67542,66482],{"class":7065},[52,67544,193],{"class":102},[52,67546,11220],{"class":7018},[52,67548,199],{"class":102},[52,67550,66491],{"class":7018},[52,67552,3114],{"class":102},[52,67554,67555,67557,67559,67561,67563,67565,67567,67569,67571],{"class":54,"line":66},[52,67556,11773],{"class":58},[52,67558,64277],{"class":62},[52,67560,199],{"class":102},[52,67562,23523],{"class":62},[52,67564,11213],{"class":58},[52,67566,49274],{"class":241},[52,67568,193],{"class":102},[52,67570,11220],{"class":189},[52,67572,3114],{"class":102},[52,67574,67575,67577,67579,67581,67583],{"class":54,"line":73},[52,67576,11168],{"class":58},[52,67578,23523],{"class":62},[52,67580,11062],{"class":79},[52,67582,66491],{"class":62},[52,67584,1330],{"class":102},[52,67586,67587,67589],{"class":54,"line":94},[52,67588,35431],{"class":58},[52,67590,66532],{"class":62},[52,67592,67593,67596,67598],{"class":54,"line":106},[52,67594,67595],{"class":58},"        return",[52,67597,140],{"class":79},[52,67599,66541],{"class":121},[14,67601,67602,67603,67605],{},"This returns ",[49,67604,66620],{}," after checking only the first item.",[14,67607,67608,67609,38514,67611,67614],{},"The fix is to put ",[49,67610,66884],{},[5503,67612,67613],{},"after"," the loop:",[42,67616,67618],{"className":44,"code":67617,"language":46,"meta":47,"style":47},"def linear_search(items, target):\n    for index, value in enumerate(items):\n        if value == target:\n            return index\n    return -1\n",[49,67619,67620,67636,67656,67668,67674],{"__ignoreMap":47},[52,67621,67622,67624,67626,67628,67630,67632,67634],{"class":54,"line":55},[52,67623,7410],{"class":1475},[52,67625,66482],{"class":7065},[52,67627,193],{"class":102},[52,67629,11220],{"class":7018},[52,67631,199],{"class":102},[52,67633,66491],{"class":7018},[52,67635,3114],{"class":102},[52,67637,67638,67640,67642,67644,67646,67648,67650,67652,67654],{"class":54,"line":66},[52,67639,11773],{"class":58},[52,67641,64277],{"class":62},[52,67643,199],{"class":102},[52,67645,23523],{"class":62},[52,67647,11213],{"class":58},[52,67649,49274],{"class":241},[52,67651,193],{"class":102},[52,67653,11220],{"class":189},[52,67655,3114],{"class":102},[52,67657,67658,67660,67662,67664,67666],{"class":54,"line":73},[52,67659,11168],{"class":58},[52,67661,23523],{"class":62},[52,67663,11062],{"class":79},[52,67665,66491],{"class":62},[52,67667,1330],{"class":102},[52,67669,67670,67672],{"class":54,"line":94},[52,67671,35431],{"class":58},[52,67673,66532],{"class":62},[52,67675,67676,67678,67680],{"class":54,"line":106},[52,67677,20958],{"class":58},[52,67679,140],{"class":79},[52,67681,66541],{"class":121},[639,67683,67685],{"id":67684},"confusing-the-index-with-the-value","Confusing the index with the value",[14,67687,67688,67689,67692],{},"This function returns the ",[5503,67690,67691],{},"position",", not the item itself.",[14,67694,67695],{},"In this list:",[42,67697,67699],{"className":44,"code":67698,"language":46,"meta":47,"style":47},"numbers = [4, 7, 1, 9, 3]\n",[49,67700,67701],{"__ignoreMap":47},[52,67702,67703,67705,67707,67709,67711,67713,67715,67717,67719,67721,67723,67725,67727],{"class":54,"line":55},[52,67704,32092],{"class":62},[52,67706,80],{"class":79},[52,67708,18162],{"class":102},[52,67710,11366],{"class":121},[52,67712,199],{"class":102},[52,67714,44338],{"class":121},[52,67716,199],{"class":102},[52,67718,1518],{"class":121},[52,67720,199],{"class":102},[52,67722,66568],{"class":121},[52,67724,199],{"class":102},[52,67726,5261],{"class":121},[52,67728,1147],{"class":102},[14,67730,67731,67732,67734,67735,1694,67737,67469,67739,186],{},"If you search for ",[49,67733,37264],{},", the function returns ",[49,67736,7508],{},[49,67738,37264],{},[49,67740,7508],{},[639,67742,10294,67743,10598,67745],{"id":33809},[49,67744,80],{},[49,67746,11062],{},[14,67748,6505],{},[42,67750,67752],{"className":44,"code":67751,"language":46,"meta":47,"style":47},"if value = target:\n",[49,67753,67754],{"__ignoreMap":47},[52,67755,67756,67758,67760,67762,67764],{"class":54,"line":55},[52,67757,1313],{"class":58},[52,67759,23523],{"class":62},[52,67761,80],{"class":79},[52,67763,66491],{"class":62},[52,67765,1330],{"class":102},[14,67767,3650,67768,67770],{},[49,67769,11062],{}," for comparison:",[42,67772,67774],{"className":44,"code":67773,"language":46,"meta":47,"style":47},"if value == target:\n",[49,67775,67776],{"__ignoreMap":47},[52,67777,67778,67780,67782,67784,67786],{"class":54,"line":55},[52,67779,1313],{"class":58},[52,67781,23523],{"class":62},[52,67783,11062],{"class":79},[52,67785,66491],{"class":62},[52,67787,1330],{"class":102},[639,67789,67791],{"id":67790},"forgetting-the-not-found-case","Forgetting the \"not found\" case",[14,67793,67794],{},"If the target is not in the list, the function still needs to return something.",[14,67796,67797],{},"That is why this line matters:",[42,67799,67801],{"className":44,"code":67800,"language":46,"meta":47,"style":47},"return -1\n",[49,67802,67803],{"__ignoreMap":47},[52,67804,67805,67807,67809],{"class":54,"line":55},[52,67806,21233],{"class":58},[52,67808,140],{"class":79},[52,67810,66541],{"class":121},[639,67812,67814],{"id":67813},"searching-for-the-wrong-data-type","Searching for the wrong data type",[14,67816,67817],{},"These are not the same:",[309,67819,67820,67825],{},[24,67821,67822,67824],{},[49,67823,5800],{}," → a string",[24,67826,67827,67829],{},[49,67828,9479],{}," → an integer",[14,67831,4259],{},[42,67833,67835],{"className":44,"code":67834,"language":46,"meta":47,"style":47},"numbers = [1, 2, 3, 4, 5]\n\nprint(linear_search(numbers, \"5\"))\nprint(linear_search(numbers, 5))\n",[49,67836,67837,67865,67869,67891],{"__ignoreMap":47},[52,67838,67839,67841,67843,67845,67847,67849,67851,67853,67855,67857,67859,67861,67863],{"class":54,"line":55},[52,67840,32092],{"class":62},[52,67842,80],{"class":79},[52,67844,18162],{"class":102},[52,67846,9200],{"class":121},[52,67848,199],{"class":102},[52,67850,9168],{"class":121},[52,67852,199],{"class":102},[52,67854,5261],{"class":121},[52,67856,199],{"class":102},[52,67858,32111],{"class":121},[52,67860,199],{"class":102},[52,67862,7646],{"class":121},[52,67864,1147],{"class":102},[52,67866,67867],{"class":54,"line":66},[52,67868,70],{"emptyLinePlaceholder":69},[52,67870,67871,67873,67875,67877,67879,67881,67883,67885,67887,67889],{"class":54,"line":73},[52,67872,242],{"class":241},[52,67874,193],{"class":102},[52,67876,66788],{"class":189},[52,67878,193],{"class":102},[52,67880,5324],{"class":189},[52,67882,199],{"class":102},[52,67884,84],{"class":83},[52,67886,9479],{"class":87},[52,67888,115],{"class":83},[52,67890,8886],{"class":102},[52,67892,67893,67895,67897,67899,67901,67903,67905,67907],{"class":54,"line":94},[52,67894,242],{"class":241},[52,67896,193],{"class":102},[52,67898,66788],{"class":189},[52,67900,193],{"class":102},[52,67902,5324],{"class":189},[52,67904,199],{"class":102},[52,67906,7646],{"class":121},[52,67908,8886],{"class":102},[14,67910,67911],{},[5503,67912,5267],{},[42,67914,67916],{"className":44,"code":67915,"language":46,"meta":47,"style":47},"-1\n4\n",[49,67917,67918,67924],{"__ignoreMap":47},[52,67919,67920,67922],{"class":54,"line":55},[52,67921,5889],{"class":79},[52,67923,66541],{"class":121},[52,67925,67926],{"class":54,"line":66},[52,67927,8831],{"class":121},[37,67929,1942],{"id":1941},[639,67931,67933],{"id":67932},"what-does-linear-search-return-in-python","What does linear search return in Python?",[14,67935,67936,67937,186],{},"In this example, it returns the index of the matching item. If the item is not found, it returns ",[49,67938,66620],{},[639,67940,67942],{"id":67941},"does-linear-search-require-a-sorted-list","Does linear search require a sorted list?",[14,67944,67945],{},"No. It checks items one by one, so the list does not need to be sorted.",[639,67947,1956,67949,67951],{"id":67948},"why-use-enumerate-in-linear-search",[49,67950,66890],{}," in linear search?",[14,67953,67954,67956],{},[49,67955,66890],{}," gives both the index and the value, which makes the loop easier to read.",[639,67958,67960],{"id":67959},"can-linear-search-be-used-with-strings","Can linear search be used with strings?",[14,67962,67963],{},"Yes. You can search through a list of strings the same way you search through numbers.",[639,67965,67967],{"id":67966},"is-linear-search-fast","Is linear search fast?",[14,67969,67970],{},"It is simple but not very fast for large lists because it may need to check every item.",[37,67972,2005],{"id":2004},[309,67974,67975,67982,67989,67995,68000],{},[24,67976,67977],{},[372,67978,10646,67979,67981],{"href":32073},[49,67980,12265],{}," loops explained",[24,67983,67984],{},[372,67985,10646,67986,67988],{"href":32078},[49,67987,1313],{}," statements explained",[24,67990,67991],{},[372,67992,10646,67993,2866],{"href":66894},[49,67994,66890],{},[24,67996,67997],{},[372,67998,67999],{"href":67485},"How to find an item in a list in Python",[24,68001,68002],{},[372,68003,68005],{"href":68004},"\u002Fexamples\u002Fpython-list-processing-example\u002F","Python list processing example",[2034,68007,68008],{},"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 .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 .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 .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":47,"searchDepth":66,"depth":66,"links":68010},[68011,68012,68013,68014,68017,68018,68019,68020,68028,68036],{"id":39,"depth":66,"text":40},{"id":7215,"depth":66,"text":7216},{"id":66640,"depth":66,"text":66641},{"id":66672,"depth":66,"text":66673,"children":68015},[68016],{"id":4100,"depth":73,"text":4101},{"id":66899,"depth":66,"text":66900},{"id":67348,"depth":66,"text":67349},{"id":67489,"depth":66,"text":67490},{"id":6401,"depth":66,"text":6402,"children":68021},[68022,68024,68025,68026,68027],{"id":67524,"depth":73,"text":68023},"Returning -1 too early",{"id":67684,"depth":73,"text":67685},{"id":33809,"depth":73,"text":34525},{"id":67790,"depth":73,"text":67791},{"id":67813,"depth":73,"text":67814},{"id":1941,"depth":66,"text":1942,"children":68029},[68030,68031,68032,68034,68035],{"id":67932,"depth":73,"text":67933},{"id":67941,"depth":73,"text":67942},{"id":67948,"depth":73,"text":68033},"Why use enumerate() in linear search?",{"id":67959,"depth":73,"text":67960},{"id":67966,"depth":73,"text":67967},{"id":2004,"depth":66,"text":2005},"Master python search algorithm example linear search in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-search-algorithm-example-linear-search",{"title":66457,"description":68037},"examples\u002Fpython-search-algorithm-example-linear-search","UZPgmLaenho8LHdG01ckhzPelR8_0Om8y_izFDNxyRM",{"id":68044,"title":68045,"body":68046,"description":70079,"extension":2075,"meta":70080,"navigation":69,"path":70081,"seo":70082,"stem":70083,"__hash__":70084},"content\u002Fexamples\u002Fpython-simple-calculator-example.md","Python Simple Calculator Example",{"type":7,"value":68047,"toc":70040},[68048,68051,68054,68079,68082,68334,68337,68339,68342,68386,68390,68393,68410,68414,68417,68484,68491,68497,68512,68524,68526,68564,68566,68584,68587,68628,68630,68649,68656,68660,68663,68808,68815,68847,68852,68866,68870,68873,68879,68946,68949,68952,68971,68979,68983,68986,69228,69230,69255,69259,69262,69266,69324,69328,69381,69385,69437,69441,69495,69499,69502,69505,69528,69531,69672,69675,69677,69680,69694,69703,69705,69708,69715,69721,69723,69778,69785,69794,69797,69807,69809,69844,69849,69851,69886,69890,69897,69901,69904,69922,69927,69931,69940,69942,69949,69961,69967,69976,69980,69986,69990,69996,69998,70034,70037],[10,68049,68045],{"id":68050},"python-simple-calculator-example",[14,68052,68053],{},"A simple calculator is a great beginner Python project. It helps you practice several important basics in one small script:",[309,68055,68056,68059,68062,68065,68073,68076],{},[24,68057,68058],{},"getting user input",[24,68060,68061],{},"storing values in variables",[24,68063,68064],{},"converting text to numbers",[24,68066,53266,68067,2587,68069,11556,68071],{},[49,68068,1313],{},[49,68070,10787],{},[49,68072,1591],{},[24,68074,68075],{},"doing basic math",[24,68077,68078],{},"handling simple errors",[14,68080,68081],{},"In this example, you will build a calculator that asks for two numbers and an operator, then prints the result.",[42,68083,68085],{"className":44,"code":68084,"language":46,"meta":47,"style":47},"num1 = float(input(\"Enter first number: \"))\noperator = input(\"Enter operator (+, -, *, \u002F): \")\nnum2 = float(input(\"Enter second number: \"))\n\nif operator == \"+\":\n    print(num1 + num2)\nelif operator == \"-\":\n    print(num1 - num2)\nelif operator == \"*\":\n    print(num1 * num2)\nelif operator == \"\u002F\":\n    if num2 == 0:\n        print(\"Cannot divide by zero\")\n    else:\n        print(num1 \u002F num2)\nelse:\n    print(\"Invalid operator\")\n",[49,68086,68087,68110,68130,68153,68157,68174,68189,68205,68219,68235,68249,68265,68278,68293,68299,68313,68319],{"__ignoreMap":47},[52,68088,68089,68091,68093,68095,68097,68099,68101,68103,68106,68108],{"class":54,"line":55},[52,68090,10249],{"class":62},[52,68092,80],{"class":79},[52,68094,35372],{"class":4733},[52,68096,193],{"class":102},[52,68098,13610],{"class":241},[52,68100,193],{"class":102},[52,68102,115],{"class":83},[52,68104,68105],{"class":87},"Enter first number: ",[52,68107,115],{"class":83},[52,68109,8886],{"class":102},[52,68111,68112,68115,68117,68119,68121,68123,68126,68128],{"class":54,"line":66},[52,68113,68114],{"class":62},"operator ",[52,68116,80],{"class":79},[52,68118,11037],{"class":241},[52,68120,193],{"class":102},[52,68122,115],{"class":83},[52,68124,68125],{"class":87},"Enter operator (+, -, *, \u002F): ",[52,68127,115],{"class":83},[52,68129,211],{"class":102},[52,68131,68132,68134,68136,68138,68140,68142,68144,68146,68149,68151],{"class":54,"line":73},[52,68133,10272],{"class":62},[52,68135,80],{"class":79},[52,68137,35372],{"class":4733},[52,68139,193],{"class":102},[52,68141,13610],{"class":241},[52,68143,193],{"class":102},[52,68145,115],{"class":83},[52,68147,68148],{"class":87},"Enter second number: ",[52,68150,115],{"class":83},[52,68152,8886],{"class":102},[52,68154,68155],{"class":54,"line":94},[52,68156,70],{"emptyLinePlaceholder":69},[52,68158,68159,68161,68164,68166,68168,68170,68172],{"class":54,"line":106},[52,68160,1313],{"class":58},[52,68162,68163],{"class":62}," operator ",[52,68165,11062],{"class":79},[52,68167,84],{"class":83},[52,68169,9881],{"class":87},[52,68171,115],{"class":83},[52,68173,1330],{"class":102},[52,68175,68176,68178,68180,68182,68184,68187],{"class":54,"line":128},[52,68177,1599],{"class":241},[52,68179,193],{"class":102},[52,68181,10249],{"class":189},[52,68183,9881],{"class":79},[52,68185,68186],{"class":189}," num2",[52,68188,211],{"class":102},[52,68190,68191,68193,68195,68197,68199,68201,68203],{"class":54,"line":148},[52,68192,10787],{"class":58},[52,68194,68163],{"class":62},[52,68196,11062],{"class":79},[52,68198,84],{"class":83},[52,68200,5889],{"class":87},[52,68202,115],{"class":83},[52,68204,1330],{"class":102},[52,68206,68207,68209,68211,68213,68215,68217],{"class":54,"line":164},[52,68208,1599],{"class":241},[52,68210,193],{"class":102},[52,68212,10249],{"class":189},[52,68214,5889],{"class":79},[52,68216,68186],{"class":189},[52,68218,211],{"class":102},[52,68220,68221,68223,68225,68227,68229,68231,68233],{"class":54,"line":170},[52,68222,10787],{"class":58},[52,68224,68163],{"class":62},[52,68226,11062],{"class":79},[52,68228,84],{"class":83},[52,68230,44460],{"class":87},[52,68232,115],{"class":83},[52,68234,1330],{"class":102},[52,68236,68237,68239,68241,68243,68245,68247],{"class":54,"line":175},[52,68238,1599],{"class":241},[52,68240,193],{"class":102},[52,68242,10249],{"class":189},[52,68244,44460],{"class":79},[52,68246,68186],{"class":189},[52,68248,211],{"class":102},[52,68250,68251,68253,68255,68257,68259,68261,68263],{"class":54,"line":214},[52,68252,10787],{"class":58},[52,68254,68163],{"class":62},[52,68256,11062],{"class":79},[52,68258,84],{"class":83},[52,68260,10537],{"class":87},[52,68262,115],{"class":83},[52,68264,1330],{"class":102},[52,68266,68267,68269,68272,68274,68276],{"class":54,"line":233},[52,68268,1438],{"class":58},[52,68270,68271],{"class":62}," num2 ",[52,68273,11062],{"class":79},[52,68275,13049],{"class":121},[52,68277,1330],{"class":102},[52,68279,68280,68282,68284,68286,68289,68291],{"class":54,"line":238},[52,68281,1470],{"class":241},[52,68283,193],{"class":102},[52,68285,115],{"class":83},[52,68287,68288],{"class":87},"Cannot divide by zero",[52,68290,115],{"class":83},[52,68292,211],{"class":102},[52,68294,68295,68297],{"class":54,"line":272},[52,68296,1567],{"class":58},[52,68298,1330],{"class":102},[52,68300,68301,68303,68305,68307,68309,68311],{"class":54,"line":1348},[52,68302,1470],{"class":241},[52,68304,193],{"class":102},[52,68306,10249],{"class":189},[52,68308,10537],{"class":79},[52,68310,68186],{"class":189},[52,68312,211],{"class":102},[52,68314,68315,68317],{"class":54,"line":1376},[52,68316,1591],{"class":58},[52,68318,1330],{"class":102},[52,68320,68321,68323,68325,68327,68330,68332],{"class":54,"line":1381},[52,68322,1599],{"class":241},[52,68324,193],{"class":102},[52,68326,115],{"class":83},[52,68328,68329],{"class":87},"Invalid operator",[52,68331,115],{"class":83},[52,68333,211],{"class":102},[14,68335,68336],{},"Use this as the main working example. The rest of the page explains each part step by step.",[37,68338,7216],{"id":7215},[14,68340,68341],{},"This small project shows you how to:",[309,68343,68344,68351,68360,68371,68383],{},[24,68345,68346,68347],{},"get values from the user with ",[372,68348,68349],{"href":10606},[49,68350,10597],{},[24,68352,68353,68354],{},"convert text input into numbers with ",[372,68355,68357],{"href":68356},"\u002Freference\u002Fpython-float-function-explained\u002F",[49,68358,68359],{},"float()",[24,68361,68362,68363],{},"choose an action with ",[372,68364,68365,2587,68367,11556,68369],{"href":52085},[49,68366,1313],{},[49,68368,10787],{},[49,68370,1591],{},[24,68372,68373,68374,2587,68376,2587,68378,11556,68380,68382],{},"perform ",[49,68375,9881],{},[49,68377,5889],{},[49,68379,44460],{},[49,68381,10537],{}," operations",[24,68384,68385],{},"handle invalid operators and division by zero",[37,68387,68389],{"id":68388},"what-the-calculator-should-do","What the calculator should do",[14,68391,68392],{},"The calculator should:",[309,68394,68395,68398,68401,68404,68407],{},[24,68396,68397],{},"ask for the first number",[24,68399,68400],{},"ask for the operator",[24,68402,68403],{},"ask for the second number",[24,68405,68406],{},"run the correct math operation",[24,68408,68409],{},"print the result or a clear error message",[37,68411,68413],{"id":68412},"step-1-read-user-input","Step 1: Read user input",[14,68415,68416],{},"The first part of the program gets values from the user.",[42,68418,68420],{"className":44,"code":68419,"language":46,"meta":47,"style":47},"num1 = float(input(\"Enter first number: \"))\noperator = input(\"Enter operator (+, -, *, \u002F): \")\nnum2 = float(input(\"Enter second number: \"))\n",[49,68421,68422,68444,68462],{"__ignoreMap":47},[52,68423,68424,68426,68428,68430,68432,68434,68436,68438,68440,68442],{"class":54,"line":55},[52,68425,10249],{"class":62},[52,68427,80],{"class":79},[52,68429,35372],{"class":4733},[52,68431,193],{"class":102},[52,68433,13610],{"class":241},[52,68435,193],{"class":102},[52,68437,115],{"class":83},[52,68439,68105],{"class":87},[52,68441,115],{"class":83},[52,68443,8886],{"class":102},[52,68445,68446,68448,68450,68452,68454,68456,68458,68460],{"class":54,"line":66},[52,68447,68114],{"class":62},[52,68449,80],{"class":79},[52,68451,11037],{"class":241},[52,68453,193],{"class":102},[52,68455,115],{"class":83},[52,68457,68125],{"class":87},[52,68459,115],{"class":83},[52,68461,211],{"class":102},[52,68463,68464,68466,68468,68470,68472,68474,68476,68478,68480,68482],{"class":54,"line":73},[52,68465,10272],{"class":62},[52,68467,80],{"class":79},[52,68469,35372],{"class":4733},[52,68471,193],{"class":102},[52,68473,13610],{"class":241},[52,68475,193],{"class":102},[52,68477,115],{"class":83},[52,68479,68148],{"class":87},[52,68481,115],{"class":83},[52,68483,8886],{"class":102},[639,68485,68487,68488,68490],{"id":68486},"important-idea-input-always-returns-text","Important idea: ",[49,68489,10597],{}," always returns text",[14,68492,68493,68494,68496],{},"Even if the user types ",[49,68495,32829],{},", Python reads it as a string, not a number.",[14,68498,68499,68500,68502,68503,68505,68506,759,68509,186],{},"That is why the code uses ",[49,68501,68359],{}," around the ",[49,68504,10597],{}," calls for ",[49,68507,68508],{},"num1",[49,68510,68511],{},"num2",[309,68513,68514,68519],{},[24,68515,68516,68518],{},[49,68517,10597],{}," gets the user's typed value",[24,68520,68521,68523],{},[49,68522,68359],{}," converts that text into a number with decimals",[14,68525,16291],{},[42,68527,68529],{"className":44,"code":68528,"language":46,"meta":47,"style":47},"value = input(\"Enter a number: \")\nprint(type(value))\n",[49,68530,68531,68550],{"__ignoreMap":47},[52,68532,68533,68536,68538,68540,68542,68544,68546,68548],{"class":54,"line":55},[52,68534,68535],{"class":62},"value ",[52,68537,80],{"class":79},[52,68539,11037],{"class":241},[52,68541,193],{"class":102},[52,68543,115],{"class":83},[52,68545,27039],{"class":87},[52,68547,115],{"class":83},[52,68549,211],{"class":102},[52,68551,68552,68554,68556,68558,68560,68562],{"class":54,"line":66},[52,68553,242],{"class":241},[52,68555,193],{"class":102},[52,68557,4198],{"class":4733},[52,68559,193],{"class":102},[52,68561,10815],{"class":189},[52,68563,8886],{"class":102},[14,68565,5267],{},[42,68567,68568],{"className":44,"code":19510,"language":46,"meta":47,"style":47},[49,68569,68570],{"__ignoreMap":47},[52,68571,68572,68574,68576,68578,68580,68582],{"class":54,"line":55},[52,68573,15294],{"class":79},[52,68575,6994],{"class":1475},[52,68577,4172],{"class":83},[52,68579,19523],{"class":87},[52,68581,4162],{"class":83},[52,68583,15306],{"class":79},[14,68585,68586],{},"After conversion:",[42,68588,68590],{"className":44,"code":68589,"language":46,"meta":47,"style":47},"value = float(input(\"Enter a number: \"))\nprint(type(value))\n",[49,68591,68592,68614],{"__ignoreMap":47},[52,68593,68594,68596,68598,68600,68602,68604,68606,68608,68610,68612],{"class":54,"line":55},[52,68595,68535],{"class":62},[52,68597,80],{"class":79},[52,68599,35372],{"class":4733},[52,68601,193],{"class":102},[52,68603,13610],{"class":241},[52,68605,193],{"class":102},[52,68607,115],{"class":83},[52,68609,27039],{"class":87},[52,68611,115],{"class":83},[52,68613,8886],{"class":102},[52,68615,68616,68618,68620,68622,68624,68626],{"class":54,"line":66},[52,68617,242],{"class":241},[52,68619,193],{"class":102},[52,68621,4198],{"class":4733},[52,68623,193],{"class":102},[52,68625,10815],{"class":189},[52,68627,8886],{"class":102},[14,68629,5267],{},[42,68631,68633],{"className":44,"code":68632,"language":46,"meta":47,"style":47},"\u003Cclass 'float'>\n",[49,68634,68635],{"__ignoreMap":47},[52,68636,68637,68639,68641,68643,68645,68647],{"class":54,"line":55},[52,68638,15294],{"class":79},[52,68640,6994],{"class":1475},[52,68642,4172],{"class":83},[52,68644,41423],{"class":87},[52,68646,4162],{"class":83},[52,68648,15306],{"class":79},[14,68650,68651,68652,759,68654,186],{},"If you want more practice with this, see ",[372,68653,6398],{"href":6397},[372,68655,51945],{"href":13736},[37,68657,68659],{"id":68658},"step-2-check-the-operator","Step 2: Check the operator",[14,68661,68662],{},"Next, the program checks which operator the user typed.",[42,68664,68666],{"className":44,"code":68665,"language":46,"meta":47,"style":47},"if operator == \"+\":\n    print(num1 + num2)\nelif operator == \"-\":\n    print(num1 - num2)\nelif operator == \"*\":\n    print(num1 * num2)\nelif operator == \"\u002F\":\n    print(num1 \u002F num2)\nelse:\n    print(\"Invalid operator\")\n",[49,68667,68668,68684,68698,68714,68728,68744,68758,68774,68788,68794],{"__ignoreMap":47},[52,68669,68670,68672,68674,68676,68678,68680,68682],{"class":54,"line":55},[52,68671,1313],{"class":58},[52,68673,68163],{"class":62},[52,68675,11062],{"class":79},[52,68677,84],{"class":83},[52,68679,9881],{"class":87},[52,68681,115],{"class":83},[52,68683,1330],{"class":102},[52,68685,68686,68688,68690,68692,68694,68696],{"class":54,"line":66},[52,68687,1599],{"class":241},[52,68689,193],{"class":102},[52,68691,10249],{"class":189},[52,68693,9881],{"class":79},[52,68695,68186],{"class":189},[52,68697,211],{"class":102},[52,68699,68700,68702,68704,68706,68708,68710,68712],{"class":54,"line":73},[52,68701,10787],{"class":58},[52,68703,68163],{"class":62},[52,68705,11062],{"class":79},[52,68707,84],{"class":83},[52,68709,5889],{"class":87},[52,68711,115],{"class":83},[52,68713,1330],{"class":102},[52,68715,68716,68718,68720,68722,68724,68726],{"class":54,"line":94},[52,68717,1599],{"class":241},[52,68719,193],{"class":102},[52,68721,10249],{"class":189},[52,68723,5889],{"class":79},[52,68725,68186],{"class":189},[52,68727,211],{"class":102},[52,68729,68730,68732,68734,68736,68738,68740,68742],{"class":54,"line":106},[52,68731,10787],{"class":58},[52,68733,68163],{"class":62},[52,68735,11062],{"class":79},[52,68737,84],{"class":83},[52,68739,44460],{"class":87},[52,68741,115],{"class":83},[52,68743,1330],{"class":102},[52,68745,68746,68748,68750,68752,68754,68756],{"class":54,"line":128},[52,68747,1599],{"class":241},[52,68749,193],{"class":102},[52,68751,10249],{"class":189},[52,68753,44460],{"class":79},[52,68755,68186],{"class":189},[52,68757,211],{"class":102},[52,68759,68760,68762,68764,68766,68768,68770,68772],{"class":54,"line":148},[52,68761,10787],{"class":58},[52,68763,68163],{"class":62},[52,68765,11062],{"class":79},[52,68767,84],{"class":83},[52,68769,10537],{"class":87},[52,68771,115],{"class":83},[52,68773,1330],{"class":102},[52,68775,68776,68778,68780,68782,68784,68786],{"class":54,"line":164},[52,68777,1599],{"class":241},[52,68779,193],{"class":102},[52,68781,10249],{"class":189},[52,68783,10537],{"class":79},[52,68785,68186],{"class":189},[52,68787,211],{"class":102},[52,68789,68790,68792],{"class":54,"line":170},[52,68791,1591],{"class":58},[52,68793,1330],{"class":102},[52,68795,68796,68798,68800,68802,68804,68806],{"class":54,"line":175},[52,68797,1599],{"class":241},[52,68799,193],{"class":102},[52,68801,115],{"class":83},[52,68803,68329],{"class":87},[52,68805,115],{"class":83},[52,68807,211],{"class":102},[14,68809,68810,68811,68814],{},"This works by comparing the ",[49,68812,68813],{},"operator"," variable to different strings.",[309,68816,68817,68824,68830,68836,68842],{},[24,68818,68819,68820,68823],{},"if the user enters ",[49,68821,68822],{},"\"+\"",", the program adds",[24,68825,68819,68826,68829],{},[49,68827,68828],{},"\"-\"",", the program subtracts",[24,68831,68819,68832,68835],{},[49,68833,68834],{},"\"*\"",", the program multiplies",[24,68837,68819,68838,68841],{},[49,68839,68840],{},"\"\u002F\"",", the program divides",[24,68843,68844,68845],{},"anything else goes to ",[49,68846,1591],{},[14,68848,358,68849,68851],{},[49,68850,1591],{}," part is useful for unsupported input such as:",[309,68853,68854,68858,68862],{},[24,68855,68856],{},[49,68857,32151],{},[24,68859,68860],{},[49,68861,1995],{},[24,68863,68864],{},[49,68865,63342],{},[37,68867,68869],{"id":68868},"step-3-handle-division-safely","Step 3: Handle division safely",[14,68871,68872],{},"Division needs one extra check.",[14,68874,68875,68876,68878],{},"If you try to divide by zero, Python raises an error. To avoid that, check ",[49,68877,68511],{}," before dividing.",[42,68880,68882],{"className":44,"code":68881,"language":46,"meta":47,"style":47},"elif operator == \"\u002F\":\n    if num2 == 0:\n        print(\"Cannot divide by zero\")\n    else:\n        print(num1 \u002F num2)\n",[49,68883,68884,68900,68912,68926,68932],{"__ignoreMap":47},[52,68885,68886,68888,68890,68892,68894,68896,68898],{"class":54,"line":55},[52,68887,10787],{"class":58},[52,68889,68163],{"class":62},[52,68891,11062],{"class":79},[52,68893,84],{"class":83},[52,68895,10537],{"class":87},[52,68897,115],{"class":83},[52,68899,1330],{"class":102},[52,68901,68902,68904,68906,68908,68910],{"class":54,"line":66},[52,68903,1438],{"class":58},[52,68905,68271],{"class":62},[52,68907,11062],{"class":79},[52,68909,13049],{"class":121},[52,68911,1330],{"class":102},[52,68913,68914,68916,68918,68920,68922,68924],{"class":54,"line":73},[52,68915,1470],{"class":241},[52,68917,193],{"class":102},[52,68919,115],{"class":83},[52,68921,68288],{"class":87},[52,68923,115],{"class":83},[52,68925,211],{"class":102},[52,68927,68928,68930],{"class":54,"line":94},[52,68929,1567],{"class":58},[52,68931,1330],{"class":102},[52,68933,68934,68936,68938,68940,68942,68944],{"class":54,"line":106},[52,68935,1470],{"class":241},[52,68937,193],{"class":102},[52,68939,10249],{"class":189},[52,68941,10537],{"class":79},[52,68943,68186],{"class":189},[52,68945,211],{"class":102},[14,68947,68948],{},"This makes the program friendlier for beginners and users.",[14,68950,68951],{},"Without the check, code like this would fail:",[42,68953,68955],{"className":44,"code":68954,"language":46,"meta":47,"style":47},"print(8 \u002F 0)\n",[49,68956,68957],{"__ignoreMap":47},[52,68958,68959,68961,68963,68965,68967,68969],{"class":54,"line":55},[52,68960,242],{"class":241},[52,68962,193],{"class":102},[52,68964,44646],{"class":121},[52,68966,27056],{"class":79},[52,68968,13049],{"class":121},[52,68970,211],{"class":102},[14,68972,23272,68973,68975,68976,186],{},[49,68974,27746],{},". If you want to understand that error in more detail, see ",[372,68977,68978],{"href":27597},"ZeroDivisionError: division by zero fix",[37,68980,68982],{"id":68981},"full-calculator-example","Full calculator example",[14,68984,68985],{},"Here is the full program again:",[42,68987,68988],{"className":44,"code":68084,"language":46,"meta":47,"style":47},[49,68989,68990,69012,69030,69052,69056,69072,69086,69102,69116,69132,69146,69162,69174,69188,69194,69208,69214],{"__ignoreMap":47},[52,68991,68992,68994,68996,68998,69000,69002,69004,69006,69008,69010],{"class":54,"line":55},[52,68993,10249],{"class":62},[52,68995,80],{"class":79},[52,68997,35372],{"class":4733},[52,68999,193],{"class":102},[52,69001,13610],{"class":241},[52,69003,193],{"class":102},[52,69005,115],{"class":83},[52,69007,68105],{"class":87},[52,69009,115],{"class":83},[52,69011,8886],{"class":102},[52,69013,69014,69016,69018,69020,69022,69024,69026,69028],{"class":54,"line":66},[52,69015,68114],{"class":62},[52,69017,80],{"class":79},[52,69019,11037],{"class":241},[52,69021,193],{"class":102},[52,69023,115],{"class":83},[52,69025,68125],{"class":87},[52,69027,115],{"class":83},[52,69029,211],{"class":102},[52,69031,69032,69034,69036,69038,69040,69042,69044,69046,69048,69050],{"class":54,"line":73},[52,69033,10272],{"class":62},[52,69035,80],{"class":79},[52,69037,35372],{"class":4733},[52,69039,193],{"class":102},[52,69041,13610],{"class":241},[52,69043,193],{"class":102},[52,69045,115],{"class":83},[52,69047,68148],{"class":87},[52,69049,115],{"class":83},[52,69051,8886],{"class":102},[52,69053,69054],{"class":54,"line":94},[52,69055,70],{"emptyLinePlaceholder":69},[52,69057,69058,69060,69062,69064,69066,69068,69070],{"class":54,"line":106},[52,69059,1313],{"class":58},[52,69061,68163],{"class":62},[52,69063,11062],{"class":79},[52,69065,84],{"class":83},[52,69067,9881],{"class":87},[52,69069,115],{"class":83},[52,69071,1330],{"class":102},[52,69073,69074,69076,69078,69080,69082,69084],{"class":54,"line":128},[52,69075,1599],{"class":241},[52,69077,193],{"class":102},[52,69079,10249],{"class":189},[52,69081,9881],{"class":79},[52,69083,68186],{"class":189},[52,69085,211],{"class":102},[52,69087,69088,69090,69092,69094,69096,69098,69100],{"class":54,"line":148},[52,69089,10787],{"class":58},[52,69091,68163],{"class":62},[52,69093,11062],{"class":79},[52,69095,84],{"class":83},[52,69097,5889],{"class":87},[52,69099,115],{"class":83},[52,69101,1330],{"class":102},[52,69103,69104,69106,69108,69110,69112,69114],{"class":54,"line":164},[52,69105,1599],{"class":241},[52,69107,193],{"class":102},[52,69109,10249],{"class":189},[52,69111,5889],{"class":79},[52,69113,68186],{"class":189},[52,69115,211],{"class":102},[52,69117,69118,69120,69122,69124,69126,69128,69130],{"class":54,"line":170},[52,69119,10787],{"class":58},[52,69121,68163],{"class":62},[52,69123,11062],{"class":79},[52,69125,84],{"class":83},[52,69127,44460],{"class":87},[52,69129,115],{"class":83},[52,69131,1330],{"class":102},[52,69133,69134,69136,69138,69140,69142,69144],{"class":54,"line":175},[52,69135,1599],{"class":241},[52,69137,193],{"class":102},[52,69139,10249],{"class":189},[52,69141,44460],{"class":79},[52,69143,68186],{"class":189},[52,69145,211],{"class":102},[52,69147,69148,69150,69152,69154,69156,69158,69160],{"class":54,"line":214},[52,69149,10787],{"class":58},[52,69151,68163],{"class":62},[52,69153,11062],{"class":79},[52,69155,84],{"class":83},[52,69157,10537],{"class":87},[52,69159,115],{"class":83},[52,69161,1330],{"class":102},[52,69163,69164,69166,69168,69170,69172],{"class":54,"line":233},[52,69165,1438],{"class":58},[52,69167,68271],{"class":62},[52,69169,11062],{"class":79},[52,69171,13049],{"class":121},[52,69173,1330],{"class":102},[52,69175,69176,69178,69180,69182,69184,69186],{"class":54,"line":238},[52,69177,1470],{"class":241},[52,69179,193],{"class":102},[52,69181,115],{"class":83},[52,69183,68288],{"class":87},[52,69185,115],{"class":83},[52,69187,211],{"class":102},[52,69189,69190,69192],{"class":54,"line":272},[52,69191,1567],{"class":58},[52,69193,1330],{"class":102},[52,69195,69196,69198,69200,69202,69204,69206],{"class":54,"line":1348},[52,69197,1470],{"class":241},[52,69199,193],{"class":102},[52,69201,10249],{"class":189},[52,69203,10537],{"class":79},[52,69205,68186],{"class":189},[52,69207,211],{"class":102},[52,69209,69210,69212],{"class":54,"line":1376},[52,69211,1591],{"class":58},[52,69213,1330],{"class":102},[52,69215,69216,69218,69220,69222,69224,69226],{"class":54,"line":1381},[52,69217,1599],{"class":241},[52,69219,193],{"class":102},[52,69221,115],{"class":83},[52,69223,68329],{"class":87},[52,69225,115],{"class":83},[52,69227,211],{"class":102},[639,69229,24095],{"id":24094},[309,69231,69232,69237,69242,69247,69252],{},[24,69233,69234,69236],{},[49,69235,68508],{}," stores the first number",[24,69238,69239,69241],{},[49,69240,68813],{}," stores the math symbol",[24,69243,69244,69246],{},[49,69245,68511],{}," stores the second number",[24,69248,6728,69249,69251],{},[49,69250,1313],{}," structure chooses the correct calculation",[24,69253,69254],{},"the result is printed to the screen",[37,69256,69258],{"id":69257},"expected-output-examples","Expected output examples",[14,69260,69261],{},"Here are some sample runs.",[639,69263,69265],{"id":69264},"example-1","Example 1",[42,69267,69269],{"className":44,"code":69268,"language":46,"meta":47,"style":47},"Enter first number: 12\nEnter operator (+, -, *, \u002F): +\nEnter second number: 3\n15.0\n",[49,69270,69271,69280,69310,69319],{"__ignoreMap":47},[52,69272,69273,69276,69278],{"class":54,"line":55},[52,69274,69275],{"class":62},"Enter first number",[52,69277,118],{"class":102},[52,69279,53474],{"class":121},[52,69281,69282,69285,69287,69289,69291,69293,69295,69297,69300,69302,69304,69307],{"class":54,"line":66},[52,69283,69284],{"class":62},"Enter ",[52,69286,68114],{"class":189},[52,69288,193],{"class":102},[52,69290,9881],{"class":79},[52,69292,199],{"class":102},[52,69294,140],{"class":79},[52,69296,199],{"class":102},[52,69298,69299],{"class":79}," *",[52,69301,199],{"class":102},[52,69303,27056],{"class":79},[52,69305,69306],{"class":102},"):",[52,69308,69309],{"class":79}," +\n",[52,69311,69312,69315,69317],{"class":54,"line":73},[52,69313,69314],{"class":62},"Enter second number",[52,69316,118],{"class":102},[52,69318,1085],{"class":121},[52,69320,69321],{"class":54,"line":94},[52,69322,69323],{"class":121},"15.0\n",[639,69325,69327],{"id":69326},"example-2","Example 2",[42,69329,69331],{"className":44,"code":69330,"language":46,"meta":47,"style":47},"Enter first number: 10\nEnter operator (+, -, *, \u002F): \u002F\nEnter second number: 2\n5.0\n",[49,69332,69333,69341,69368,69376],{"__ignoreMap":47},[52,69334,69335,69337,69339],{"class":54,"line":55},[52,69336,69275],{"class":62},[52,69338,118],{"class":102},[52,69340,55295],{"class":121},[52,69342,69343,69345,69347,69349,69351,69353,69355,69357,69359,69361,69363,69365],{"class":54,"line":66},[52,69344,69284],{"class":62},[52,69346,68114],{"class":189},[52,69348,193],{"class":102},[52,69350,9881],{"class":79},[52,69352,199],{"class":102},[52,69354,140],{"class":79},[52,69356,199],{"class":102},[52,69358,69299],{"class":79},[52,69360,199],{"class":102},[52,69362,27056],{"class":79},[52,69364,69306],{"class":102},[52,69366,69367],{"class":79}," \u002F\n",[52,69369,69370,69372,69374],{"class":54,"line":73},[52,69371,69314],{"class":62},[52,69373,118],{"class":102},[52,69375,45381],{"class":121},[52,69377,69378],{"class":54,"line":94},[52,69379,69380],{"class":121},"5.0\n",[639,69382,69384],{"id":69383},"example-3","Example 3",[42,69386,69388],{"className":44,"code":69387,"language":46,"meta":47,"style":47},"Enter first number: 8\nEnter operator (+, -, *, \u002F): \u002F\nEnter second number: 0\nCannot divide by zero\n",[49,69389,69390,69398,69424,69432],{"__ignoreMap":47},[52,69391,69392,69394,69396],{"class":54,"line":55},[52,69393,69275],{"class":62},[52,69395,118],{"class":102},[52,69397,45391],{"class":121},[52,69399,69400,69402,69404,69406,69408,69410,69412,69414,69416,69418,69420,69422],{"class":54,"line":66},[52,69401,69284],{"class":62},[52,69403,68114],{"class":189},[52,69405,193],{"class":102},[52,69407,9881],{"class":79},[52,69409,199],{"class":102},[52,69411,140],{"class":79},[52,69413,199],{"class":102},[52,69415,69299],{"class":79},[52,69417,199],{"class":102},[52,69419,27056],{"class":79},[52,69421,69306],{"class":102},[52,69423,69367],{"class":79},[52,69425,69426,69428,69430],{"class":54,"line":73},[52,69427,69314],{"class":62},[52,69429,118],{"class":102},[52,69431,34598],{"class":121},[52,69433,69434],{"class":54,"line":94},[52,69435,69436],{"class":62},"Cannot divide by zero\n",[639,69438,69440],{"id":69439},"example-4","Example 4",[42,69442,69444],{"className":44,"code":69443,"language":46,"meta":47,"style":47},"Enter first number: 4\nEnter operator (+, -, *, \u002F): ?\nEnter second number: 2\nInvalid operator\n",[49,69445,69446,69455,69482,69490],{"__ignoreMap":47},[52,69447,69448,69450,69452],{"class":54,"line":55},[52,69449,69275],{"class":62},[52,69451,118],{"class":102},[52,69453,69454],{"class":121}," 4\n",[52,69456,69457,69459,69461,69463,69465,69467,69469,69471,69473,69475,69477,69479],{"class":54,"line":66},[52,69458,69284],{"class":62},[52,69460,68114],{"class":189},[52,69462,193],{"class":102},[52,69464,9881],{"class":79},[52,69466,199],{"class":102},[52,69468,140],{"class":79},[52,69470,199],{"class":102},[52,69472,69299],{"class":79},[52,69474,199],{"class":102},[52,69476,27056],{"class":79},[52,69478,69306],{"class":102},[52,69480,69481],{"class":54291}," ?\n",[52,69483,69484,69486,69488],{"class":54,"line":73},[52,69485,69314],{"class":62},[52,69487,118],{"class":102},[52,69489,45381],{"class":121},[52,69491,69492],{"class":54,"line":94},[52,69493,69494],{"class":62},"Invalid operator\n",[37,69496,69498],{"id":69497},"beginner-improvements","Beginner improvements",[14,69500,69501],{},"Once this version works, you can improve it in small steps.",[14,69503,69504],{},"Try one of these:",[309,69506,69507,69513,69516,69521],{},[24,69508,69509,69510],{},"round the result with ",[49,69511,69512],{},"round()",[24,69514,69515],{},"repeat the calculator in a loop",[24,69517,69518,69519],{},"let users quit with ",[49,69520,54288],{},[24,69522,23564,69523,10598,69525,69527],{},[49,69524,9492],{},[49,69526,68359],{}," if you only want whole numbers",[14,69529,69530],{},"For example, rounding the result:",[42,69532,69534],{"className":44,"code":69533,"language":46,"meta":47,"style":47},"num1 = float(input(\"Enter first number: \"))\noperator = input(\"Enter operator (+, -, *, \u002F): \")\nnum2 = float(input(\"Enter second number: \"))\n\nif operator == \"\u002F\":\n    if num2 == 0:\n        print(\"Cannot divide by zero\")\n    else:\n        print(round(num1 \u002F num2, 2))\n",[49,69535,69536,69558,69576,69598,69602,69618,69630,69644,69650],{"__ignoreMap":47},[52,69537,69538,69540,69542,69544,69546,69548,69550,69552,69554,69556],{"class":54,"line":55},[52,69539,10249],{"class":62},[52,69541,80],{"class":79},[52,69543,35372],{"class":4733},[52,69545,193],{"class":102},[52,69547,13610],{"class":241},[52,69549,193],{"class":102},[52,69551,115],{"class":83},[52,69553,68105],{"class":87},[52,69555,115],{"class":83},[52,69557,8886],{"class":102},[52,69559,69560,69562,69564,69566,69568,69570,69572,69574],{"class":54,"line":66},[52,69561,68114],{"class":62},[52,69563,80],{"class":79},[52,69565,11037],{"class":241},[52,69567,193],{"class":102},[52,69569,115],{"class":83},[52,69571,68125],{"class":87},[52,69573,115],{"class":83},[52,69575,211],{"class":102},[52,69577,69578,69580,69582,69584,69586,69588,69590,69592,69594,69596],{"class":54,"line":73},[52,69579,10272],{"class":62},[52,69581,80],{"class":79},[52,69583,35372],{"class":4733},[52,69585,193],{"class":102},[52,69587,13610],{"class":241},[52,69589,193],{"class":102},[52,69591,115],{"class":83},[52,69593,68148],{"class":87},[52,69595,115],{"class":83},[52,69597,8886],{"class":102},[52,69599,69600],{"class":54,"line":94},[52,69601,70],{"emptyLinePlaceholder":69},[52,69603,69604,69606,69608,69610,69612,69614,69616],{"class":54,"line":106},[52,69605,1313],{"class":58},[52,69607,68163],{"class":62},[52,69609,11062],{"class":79},[52,69611,84],{"class":83},[52,69613,10537],{"class":87},[52,69615,115],{"class":83},[52,69617,1330],{"class":102},[52,69619,69620,69622,69624,69626,69628],{"class":54,"line":128},[52,69621,1438],{"class":58},[52,69623,68271],{"class":62},[52,69625,11062],{"class":79},[52,69627,13049],{"class":121},[52,69629,1330],{"class":102},[52,69631,69632,69634,69636,69638,69640,69642],{"class":54,"line":148},[52,69633,1470],{"class":241},[52,69635,193],{"class":102},[52,69637,115],{"class":83},[52,69639,68288],{"class":87},[52,69641,115],{"class":83},[52,69643,211],{"class":102},[52,69645,69646,69648],{"class":54,"line":164},[52,69647,1567],{"class":58},[52,69649,1330],{"class":102},[52,69651,69652,69654,69656,69658,69660,69662,69664,69666,69668,69670],{"class":54,"line":170},[52,69653,1470],{"class":241},[52,69655,193],{"class":102},[52,69657,1509],{"class":241},[52,69659,193],{"class":102},[52,69661,10249],{"class":189},[52,69663,10537],{"class":79},[52,69665,68186],{"class":189},[52,69667,199],{"class":102},[52,69669,9168],{"class":121},[52,69671,8886],{"class":102},[14,69673,69674],{},"This prints the division result rounded to 2 decimal places.",[37,69676,17936],{"id":17935},[14,69678,69679],{},"This project is useful:",[309,69681,69682,69685,69688,69691],{},[24,69683,69684],{},"as a first small Python project",[24,69686,69687],{},"to practice conditionals and input",[24,69689,69690],{},"to combine multiple beginner concepts in one script",[24,69692,69693],{},"to prepare for larger menu-based programs",[14,69695,69696,69697,69699,69700,69702],{},"It is a good next step after learning variables, ",[49,69698,10597],{},", and basic ",[49,69701,1313],{}," statements.",[37,69704,12322],{"id":12321},[14,69706,69707],{},"Beginners often run into the same problems when building this calculator.",[639,69709,69711,69712,69714],{"id":69710},"forgetting-to-convert-input-values-to-numbers","Forgetting to convert ",[49,69713,10597],{}," values to numbers",[14,69716,69717,69718,69720],{},"This is very common. If you skip ",[49,69719,68359],{},", the values stay as strings.",[14,69722,8124],{},[42,69724,69726],{"className":44,"code":69725,"language":46,"meta":47,"style":47},"num1 = input(\"Enter first number: \")\nnum2 = input(\"Enter second number: \")\nprint(num1 + num2)\n",[49,69727,69728,69746,69764],{"__ignoreMap":47},[52,69729,69730,69732,69734,69736,69738,69740,69742,69744],{"class":54,"line":55},[52,69731,10249],{"class":62},[52,69733,80],{"class":79},[52,69735,11037],{"class":241},[52,69737,193],{"class":102},[52,69739,115],{"class":83},[52,69741,68105],{"class":87},[52,69743,115],{"class":83},[52,69745,211],{"class":102},[52,69747,69748,69750,69752,69754,69756,69758,69760,69762],{"class":54,"line":66},[52,69749,10272],{"class":62},[52,69751,80],{"class":79},[52,69753,11037],{"class":241},[52,69755,193],{"class":102},[52,69757,115],{"class":83},[52,69759,68148],{"class":87},[52,69761,115],{"class":83},[52,69763,211],{"class":102},[52,69765,69766,69768,69770,69772,69774,69776],{"class":54,"line":73},[52,69767,242],{"class":241},[52,69769,193],{"class":102},[52,69771,10249],{"class":189},[52,69773,9881],{"class":79},[52,69775,68186],{"class":189},[52,69777,211],{"class":102},[14,69779,53037,69780,759,69782,69784],{},[49,69781,9825],{},[49,69783,7508],{},", this prints:",[42,69786,69788],{"className":44,"code":69787,"language":46,"meta":47,"style":47},"23\n",[49,69789,69790],{"__ignoreMap":47},[52,69791,69792],{"class":54,"line":55},[52,69793,69787],{"class":121},[14,69795,69796],{},"That happens because Python joins the strings instead of adding numbers.",[639,69798,10294,69800,10598,69802,69804,69805,32064],{"id":69799},"using-instead-of-in-an-if-statement",[49,69801,80],{},[49,69803,11062],{}," in an ",[49,69806,1313],{},[14,69808,8124],{},[42,69810,69812],{"className":44,"code":69811,"language":46,"meta":47,"style":47},"if operator = \"+\":\n    print(num1 + num2)\n",[49,69813,69814,69830],{"__ignoreMap":47},[52,69815,69816,69818,69820,69822,69824,69826,69828],{"class":54,"line":55},[52,69817,1313],{"class":58},[52,69819,68163],{"class":62},[52,69821,80],{"class":79},[52,69823,84],{"class":83},[52,69825,9881],{"class":87},[52,69827,115],{"class":83},[52,69829,1330],{"class":102},[52,69831,69832,69834,69836,69838,69840,69842],{"class":54,"line":66},[52,69833,1599],{"class":241},[52,69835,193],{"class":102},[52,69837,10249],{"class":189},[52,69839,9881],{"class":79},[52,69841,68186],{"class":189},[52,69843,211],{"class":102},[14,69845,3650,69846,69848],{},[49,69847,11062],{}," when comparing values.",[14,69850,12800],{},[42,69852,69854],{"className":44,"code":69853,"language":46,"meta":47,"style":47},"if operator == \"+\":\n    print(num1 + num2)\n",[49,69855,69856,69872],{"__ignoreMap":47},[52,69857,69858,69860,69862,69864,69866,69868,69870],{"class":54,"line":55},[52,69859,1313],{"class":58},[52,69861,68163],{"class":62},[52,69863,11062],{"class":79},[52,69865,84],{"class":83},[52,69867,9881],{"class":87},[52,69869,115],{"class":83},[52,69871,1330],{"class":102},[52,69873,69874,69876,69878,69880,69882,69884],{"class":54,"line":66},[52,69875,1599],{"class":241},[52,69877,193],{"class":102},[52,69879,10249],{"class":189},[52,69881,9881],{"class":79},[52,69883,68186],{"class":189},[52,69885,211],{"class":102},[639,69887,69889],{"id":69888},"not-checking-for-division-by-zero","Not checking for division by zero",[14,69891,7526,69892,27498,69894,69896],{},[49,69893,68511],{},[49,69895,13294],{},", division will fail unless you handle it first.",[639,69898,69900],{"id":69899},"typing-an-unsupported-operator","Typing an unsupported operator",[14,69902,69903],{},"Your program only supports:",[309,69905,69906,69910,69914,69918],{},[24,69907,69908],{},[49,69909,9881],{},[24,69911,69912],{},[49,69913,5889],{},[24,69915,69916],{},[49,69917,44460],{},[24,69919,69920],{},[49,69921,10537],{},[14,69923,69924,69925,186],{},"If the user types something else, the program should show ",[49,69926,68329],{},[639,69928,69930],{"id":69929},"misspelling-variable-names","Misspelling variable names",[14,69932,69933,69934,10598,69937,69939],{},"For example, writing ",[49,69935,69936],{},"nub2",[49,69938,68511],{}," will cause errors or wrong behavior.",[37,69941,1942],{"id":1941},[639,69943,1956,69945,10598,69947,1995],{"id":69944},"why-use-float-instead-of-int",[49,69946,68359],{},[49,69948,9492],{},[14,69950,69951,69953,69954,69957,69958,69960],{},[49,69952,68359],{}," allows decimal numbers like ",[49,69955,69956],{},"2.5",". ",[49,69959,9492],{}," only works for whole numbers.",[639,69962,3677,69964,69966],{"id":69963},"why-does-input-need-conversion",[49,69965,10597],{}," need conversion?",[14,69968,69969,69971,69972,857,69974,186],{},[49,69970,10597],{}," returns a string. Math needs numbers, so convert with ",[49,69973,9492],{},[49,69975,68359],{},[639,69977,69979],{"id":69978},"why-does-division-return-a-decimal","Why does division return a decimal?",[14,69981,69982,69983,69985],{},"In Python, ",[49,69984,10537],{}," returns a float result even when both numbers are whole numbers.",[639,69987,69989],{"id":69988},"can-i-make-the-calculator-run-again-without-restarting","Can I make the calculator run again without restarting?",[14,69991,69992,69993,69995],{},"Yes. Put the code inside a ",[49,69994,10772],{}," loop and ask the user whether to continue.",[37,69997,2005],{"id":2004},[309,69999,70000,70006,70012,70022,70026,70030],{},[24,70001,70002],{},[372,70003,10646,70004,3711],{"href":10606},[49,70005,10597],{},[24,70007,70008],{},[372,70009,10646,70010,3711],{"href":68356},[49,70011,68359],{},[24,70013,70014],{},[372,70015,10646,70016,2587,70018,11556,70020,2866],{"href":52085},[49,70017,1313],{},[49,70019,1591],{},[49,70021,10787],{},[24,70023,70024],{},[372,70025,13737],{"href":13736},[24,70027,70028],{},[372,70029,6881],{"href":6471},[24,70031,70032],{},[372,70033,37804],{"href":37803},[14,70035,70036],{},"If you want more practice, try building a second version of this calculator with a loop, better input validation, or extra operators.",[2034,70038,70039],{},"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 .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 .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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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}",{"title":47,"searchDepth":66,"depth":66,"links":70041},[70042,70043,70044,70048,70049,70050,70053,70059,70060,70061,70070,70078],{"id":7215,"depth":66,"text":7216},{"id":68388,"depth":66,"text":68389},{"id":68412,"depth":66,"text":68413,"children":70045},[70046],{"id":68486,"depth":73,"text":70047},"Important idea: input() always returns text",{"id":68658,"depth":66,"text":68659},{"id":68868,"depth":66,"text":68869},{"id":68981,"depth":66,"text":68982,"children":70051},[70052],{"id":24094,"depth":73,"text":24095},{"id":69257,"depth":66,"text":69258,"children":70054},[70055,70056,70057,70058],{"id":69264,"depth":73,"text":69265},{"id":69326,"depth":73,"text":69327},{"id":69383,"depth":73,"text":69384},{"id":69439,"depth":73,"text":69440},{"id":69497,"depth":66,"text":69498},{"id":17935,"depth":66,"text":17936},{"id":12321,"depth":66,"text":12322,"children":70062},[70063,70065,70067,70068,70069],{"id":69710,"depth":73,"text":70064},"Forgetting to convert input() values to numbers",{"id":69799,"depth":73,"text":70066},"Using = instead of == in an if statement",{"id":69888,"depth":73,"text":69889},{"id":69899,"depth":73,"text":69900},{"id":69929,"depth":73,"text":69930},{"id":1941,"depth":66,"text":1942,"children":70071},[70072,70074,70076,70077],{"id":69944,"depth":73,"text":70073},"Why use float() instead of int()?",{"id":69963,"depth":73,"text":70075},"Why does input() need conversion?",{"id":69978,"depth":73,"text":69979},{"id":69988,"depth":73,"text":69989},{"id":2004,"depth":66,"text":2005},"Master python simple calculator example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-simple-calculator-example",{"title":68045,"description":70079},"examples\u002Fpython-simple-calculator-example","C9YHw200p5Q-FUEGjvoaWhFaaexOIJMPNrlfQhFn5ZM",{"id":70086,"title":70087,"body":70088,"description":72033,"extension":2075,"meta":72034,"navigation":69,"path":72035,"seo":72036,"stem":72037,"__hash__":72038},"content\u002Fexamples\u002Fpython-simple-chatbot-example.md","Python Simple Chatbot Example",{"type":7,"value":70089,"toc":72000},[70090,70093,70096,70099,70122,70125,70135,70137,70139,70150,70153,70157,70160,70202,70206,70209,70407,70409,70413,70446,70449,70456,70468,70473,70478,70482,70486,70517,70520,70539,70542,70548,70568,70573,70582,70701,70704,70735,70739,70746,70754,70762,70765,70767,70770,70776,70779,70797,70801,70804,70807,70827,70832,70835,70846,70850,70853,71104,71112,71115,71137,71142,71144,71147,71153,71155,71178,71180,71210,71215,71223,71225,71260,71262,71297,71309,71319,71321,71354,71356,71390,71397,71404,71406,71449,71451,71490,71495,71499,71506,71520,71523,71553,71555,71558,71590,71594,71597,71599,71619,71622,71831,71834,71838,71841,71867,71870,71881,71886,71888,71892,71895,71902,71905,71909,71915,71919,71928,71932,71941,71943,71977,71980,71983,71994,71997],[10,70091,70087],{"id":70092},"python-simple-chatbot-example",[14,70094,70095],{},"This beginner-friendly example shows how to build a very simple chatbot in Python.",[14,70097,70098],{},"It is a small terminal program that:",[309,70100,70101,70104,70113,70116],{},[24,70102,70103],{},"reads text from the user",[24,70105,70106,70107,2587,70109,11556,70111],{},"checks that text with ",[49,70108,1313],{},[49,70110,10787],{},[49,70112,1591],{},[24,70114,70115],{},"prints a fixed reply",[24,70117,70118,70119],{},"keeps running in a loop until the user types ",[49,70120,70121],{},"bye",[14,70123,70124],{},"This project is useful for practicing basic Python skills in one place.",[14,70126,70127,70128,70130,70131,70134],{},"Important: this is ",[5503,70129,20858],{}," an AI chatbot. It is a ",[5503,70132,70133],{},"rule-based chatbot",", which means it only responds to messages you have programmed.",[37,70136,9295],{"id":9294},[14,70138,10740],{},[309,70140,70141,70144,70147],{},[24,70142,70143],{},"A text-based chatbot that runs in the terminal",[24,70145,70146],{},"A bot that reads user input and replies with fixed answers",[24,70148,70149],{},"A small project for practicing beginner Python basics",[14,70151,70152],{},"The chatbot does not think or learn. It just compares the user's message to known text and returns a matching response.",[37,70154,70156],{"id":70155},"what-you-will-learn","What you will learn",[14,70158,70159],{},"By building this chatbot, you will practice:",[309,70161,70162,70170,70181,70189,70194],{},[24,70163,70164,70165,70169],{},"How to use ",[372,70166,70167],{"href":10606},[49,70168,10597],{}," to get text from the user",[24,70171,70164,70172,70180],{},[372,70173,70174,2587,70176,11556,70178],{"href":52085},[49,70175,1313],{},[49,70177,10787],{},[49,70179,1591],{}," to choose a reply",[24,70182,70183,70184,70188],{},"How to use a ",[372,70185,70186,12570],{"href":13425},[49,70187,10772],{}," to keep the chatbot running",[24,70190,70191,70192],{},"How to stop the program with ",[49,70193,10914],{},[24,70195,4983,70196,759,70199,70201],{},[49,70197,70198],{},"lower()",[49,70200,18831],{}," make input easier to match",[37,70203,70205],{"id":70204},"basic-chatbot-code-walkthrough","Basic chatbot code walkthrough",[14,70207,70208],{},"Here is the full minimal chatbot script:",[42,70210,70212],{"className":44,"code":70211,"language":46,"meta":47,"style":47},"print(\"Simple Chatbot\")\nprint(\"Type 'bye' to stop.\")\n\nwhile True:\n    message = input(\"You: \").lower().strip()\n\n    if message == \"hello\":\n        print(\"Bot: Hi!\")\n    elif message == \"how are you\":\n        print(\"Bot: I am doing well.\")\n    elif message == \"bye\":\n        print(\"Bot: Goodbye!\")\n        break\n    else:\n        print(\"Bot: Sorry, I don't understand.\")\n",[49,70213,70214,70229,70244,70248,70256,70283,70287,70304,70319,70336,70351,70367,70382,70386,70392],{"__ignoreMap":47},[52,70215,70216,70218,70220,70222,70225,70227],{"class":54,"line":55},[52,70217,242],{"class":241},[52,70219,193],{"class":102},[52,70221,115],{"class":83},[52,70223,70224],{"class":87},"Simple Chatbot",[52,70226,115],{"class":83},[52,70228,211],{"class":102},[52,70230,70231,70233,70235,70237,70240,70242],{"class":54,"line":66},[52,70232,242],{"class":241},[52,70234,193],{"class":102},[52,70236,115],{"class":83},[52,70238,70239],{"class":87},"Type 'bye' to stop.",[52,70241,115],{"class":83},[52,70243,211],{"class":102},[52,70245,70246],{"class":54,"line":73},[52,70247,70],{"emptyLinePlaceholder":69},[52,70249,70250,70252,70254],{"class":54,"line":94},[52,70251,10772],{"class":58},[52,70253,10943],{"class":160},[52,70255,1330],{"class":102},[52,70257,70258,70260,70262,70264,70266,70268,70271,70273,70275,70277,70279,70281],{"class":54,"line":106},[52,70259,6163],{"class":62},[52,70261,80],{"class":79},[52,70263,11037],{"class":241},[52,70265,193],{"class":102},[52,70267,115],{"class":83},[52,70269,70270],{"class":87},"You: ",[52,70272,115],{"class":83},[52,70274,54653],{"class":102},[52,70276,30237],{"class":189},[52,70278,18679],{"class":102},[52,70280,18630],{"class":189},[52,70282,230],{"class":102},[52,70284,70285],{"class":54,"line":128},[52,70286,70],{"emptyLinePlaceholder":69},[52,70288,70289,70291,70294,70296,70298,70300,70302],{"class":54,"line":148},[52,70290,1438],{"class":58},[52,70292,70293],{"class":62}," message ",[52,70295,11062],{"class":79},[52,70297,84],{"class":83},[52,70299,6363],{"class":87},[52,70301,115],{"class":83},[52,70303,1330],{"class":102},[52,70305,70306,70308,70310,70312,70315,70317],{"class":54,"line":164},[52,70307,1470],{"class":241},[52,70309,193],{"class":102},[52,70311,115],{"class":83},[52,70313,70314],{"class":87},"Bot: Hi!",[52,70316,115],{"class":83},[52,70318,211],{"class":102},[52,70320,70321,70323,70325,70327,70329,70332,70334],{"class":54,"line":170},[52,70322,11151],{"class":58},[52,70324,70293],{"class":62},[52,70326,11062],{"class":79},[52,70328,84],{"class":83},[52,70330,70331],{"class":87},"how are you",[52,70333,115],{"class":83},[52,70335,1330],{"class":102},[52,70337,70338,70340,70342,70344,70347,70349],{"class":54,"line":175},[52,70339,1470],{"class":241},[52,70341,193],{"class":102},[52,70343,115],{"class":83},[52,70345,70346],{"class":87},"Bot: I am doing well.",[52,70348,115],{"class":83},[52,70350,211],{"class":102},[52,70352,70353,70355,70357,70359,70361,70363,70365],{"class":54,"line":214},[52,70354,11151],{"class":58},[52,70356,70293],{"class":62},[52,70358,11062],{"class":79},[52,70360,84],{"class":83},[52,70362,70121],{"class":87},[52,70364,115],{"class":83},[52,70366,1330],{"class":102},[52,70368,70369,70371,70373,70375,70378,70380],{"class":54,"line":233},[52,70370,1470],{"class":241},[52,70372,193],{"class":102},[52,70374,115],{"class":83},[52,70376,70377],{"class":87},"Bot: Goodbye!",[52,70379,115],{"class":83},[52,70381,211],{"class":102},[52,70383,70384],{"class":54,"line":238},[52,70385,11499],{"class":58},[52,70387,70388,70390],{"class":54,"line":272},[52,70389,1567],{"class":58},[52,70391,1330],{"class":102},[52,70393,70394,70396,70398,70400,70403,70405],{"class":54,"line":1348},[52,70395,1470],{"class":241},[52,70397,193],{"class":102},[52,70399,115],{"class":83},[52,70401,70402],{"class":87},"Bot: Sorry, I don't understand.",[52,70404,115],{"class":83},[52,70406,211],{"class":102},[639,70408,4101],{"id":4100},[24097,70410,70412],{"id":70411},"_1-the-program-starts-with-two-messages","1. The program starts with two messages",[42,70414,70416],{"className":44,"code":70415,"language":46,"meta":47,"style":47},"print(\"Simple Chatbot\")\nprint(\"Type 'bye' to stop.\")\n",[49,70417,70418,70432],{"__ignoreMap":47},[52,70419,70420,70422,70424,70426,70428,70430],{"class":54,"line":55},[52,70421,242],{"class":241},[52,70423,193],{"class":102},[52,70425,115],{"class":83},[52,70427,70224],{"class":87},[52,70429,115],{"class":83},[52,70431,211],{"class":102},[52,70433,70434,70436,70438,70440,70442,70444],{"class":54,"line":66},[52,70435,242],{"class":241},[52,70437,193],{"class":102},[52,70439,115],{"class":83},[52,70441,70239],{"class":87},[52,70443,115],{"class":83},[52,70445,211],{"class":102},[14,70447,70448],{},"These lines show a title and tell the user how to exit the chatbot.",[24097,70450,70452,70453,70455],{"id":70451},"_2-the-while-true-loop-keeps-the-chatbot-running","2. The ",[49,70454,10900],{}," loop keeps the chatbot running",[42,70457,70458],{"className":44,"code":51952,"language":46,"meta":47,"style":47},[49,70459,70460],{"__ignoreMap":47},[52,70461,70462,70464,70466],{"class":54,"line":55},[52,70463,10772],{"class":58},[52,70465,10943],{"class":160},[52,70467,1330],{"class":102},[14,70469,3233,70470,70472],{},[49,70471,10900],{}," loop runs forever unless something stops it.",[14,70474,70475,70476,186],{},"In this chatbot, the loop keeps asking for messages until the user types ",[49,70477,70121],{},[14,70479,13422,70480,186],{},[372,70481,13426],{"href":13425},[24097,70483,70485],{"id":70484},"_3-the-chatbot-reads-user-input","3. The chatbot reads user input",[42,70487,70489],{"className":44,"code":70488,"language":46,"meta":47,"style":47},"message = input(\"You: \").lower().strip()\n",[49,70490,70491],{"__ignoreMap":47},[52,70492,70493,70495,70497,70499,70501,70503,70505,70507,70509,70511,70513,70515],{"class":54,"line":55},[52,70494,6121],{"class":62},[52,70496,80],{"class":79},[52,70498,11037],{"class":241},[52,70500,193],{"class":102},[52,70502,115],{"class":83},[52,70504,70270],{"class":87},[52,70506,115],{"class":83},[52,70508,54653],{"class":102},[52,70510,30237],{"class":189},[52,70512,18679],{"class":102},[52,70514,18630],{"class":189},[52,70516,230],{"class":102},[14,70518,70519],{},"This line does three things:",[309,70521,70522,70528,70533],{},[24,70523,70524,70527],{},[49,70525,70526],{},"input(\"You: \")"," waits for the user to type something",[24,70529,70530,70532],{},[49,70531,31732],{}," changes the text to lowercase",[24,70534,70535,70538],{},[49,70536,70537],{},".strip()"," removes extra spaces at the beginning and end",[14,70540,70541],{},"This helps the chatbot match input more easily.",[14,70543,70544,70545,118],{},"For example, these all become ",[49,70546,70547],{},"\"hello\"",[309,70549,70550,70554,70558,70563],{},[24,70551,70552],{},[49,70553,6363],{},[24,70555,70556],{},[49,70557,38698],{},[24,70559,70560],{},[49,70561,70562],{},"HELLO",[24,70564,70565],{},[49,70566,70567],{}," hello ",[14,70569,70570,70571,186],{},"If you want more practice with user input, read ",[372,70572,6398],{"href":6397},[24097,70574,70576,70577,759,70579,70581],{"id":70575},"_4-the-if-and-elif-statements-choose-the-reply","4. The ",[49,70578,1313],{},[49,70580,10787],{}," statements choose the reply",[42,70583,70585],{"className":44,"code":70584,"language":46,"meta":47,"style":47},"if message == \"hello\":\n    print(\"Bot: Hi!\")\nelif message == \"how are you\":\n    print(\"Bot: I am doing well.\")\nelif message == \"bye\":\n    print(\"Bot: Goodbye!\")\n    break\nelse:\n    print(\"Bot: Sorry, I don't understand.\")\n",[49,70586,70587,70603,70617,70633,70647,70663,70677,70681,70687],{"__ignoreMap":47},[52,70588,70589,70591,70593,70595,70597,70599,70601],{"class":54,"line":55},[52,70590,1313],{"class":58},[52,70592,70293],{"class":62},[52,70594,11062],{"class":79},[52,70596,84],{"class":83},[52,70598,6363],{"class":87},[52,70600,115],{"class":83},[52,70602,1330],{"class":102},[52,70604,70605,70607,70609,70611,70613,70615],{"class":54,"line":66},[52,70606,1599],{"class":241},[52,70608,193],{"class":102},[52,70610,115],{"class":83},[52,70612,70314],{"class":87},[52,70614,115],{"class":83},[52,70616,211],{"class":102},[52,70618,70619,70621,70623,70625,70627,70629,70631],{"class":54,"line":73},[52,70620,10787],{"class":58},[52,70622,70293],{"class":62},[52,70624,11062],{"class":79},[52,70626,84],{"class":83},[52,70628,70331],{"class":87},[52,70630,115],{"class":83},[52,70632,1330],{"class":102},[52,70634,70635,70637,70639,70641,70643,70645],{"class":54,"line":94},[52,70636,1599],{"class":241},[52,70638,193],{"class":102},[52,70640,115],{"class":83},[52,70642,70346],{"class":87},[52,70644,115],{"class":83},[52,70646,211],{"class":102},[52,70648,70649,70651,70653,70655,70657,70659,70661],{"class":54,"line":106},[52,70650,10787],{"class":58},[52,70652,70293],{"class":62},[52,70654,11062],{"class":79},[52,70656,84],{"class":83},[52,70658,70121],{"class":87},[52,70660,115],{"class":83},[52,70662,1330],{"class":102},[52,70664,70665,70667,70669,70671,70673,70675],{"class":54,"line":128},[52,70666,1599],{"class":241},[52,70668,193],{"class":102},[52,70670,115],{"class":83},[52,70672,70377],{"class":87},[52,70674,115],{"class":83},[52,70676,211],{"class":102},[52,70678,70679],{"class":54,"line":148},[52,70680,52013],{"class":58},[52,70682,70683,70685],{"class":54,"line":164},[52,70684,1591],{"class":58},[52,70686,1330],{"class":102},[52,70688,70689,70691,70693,70695,70697,70699],{"class":54,"line":170},[52,70690,1599],{"class":241},[52,70692,193],{"class":102},[52,70694,115],{"class":83},[52,70696,70402],{"class":87},[52,70698,115],{"class":83},[52,70700,211],{"class":102},[14,70702,70703],{},"This is the chatbot's decision logic:",[309,70705,70706,70715,70723,70729],{},[24,70707,70708,70709,70711,70712],{},"If the message is ",[49,70710,70547],{},", the bot says ",[49,70713,70714],{},"Hi!",[24,70716,70708,70717,70711,70720],{},[49,70718,70719],{},"\"how are you\"",[49,70721,70722],{},"I am doing well.",[24,70724,70708,70725,70728],{},[49,70726,70727],{},"\"bye\"",", the bot says goodbye and stops",[24,70730,70731,70732,70734],{},"If nothing matches, the ",[49,70733,1591],{}," block runs",[14,70736,21360,70737,186],{},[372,70738,34463],{"href":32078},[24097,70740,70742,70743,70745],{"id":70741},"_5-the-break-statement-ends-the-loop","5. The ",[49,70744,10914],{}," statement ends the loop",[42,70747,70748],{"className":44,"code":52093,"language":46,"meta":47,"style":47},[49,70749,70750],{"__ignoreMap":47},[52,70751,70752],{"class":54,"line":55},[52,70753,52093],{"class":58},[14,70755,70756,70758,70759,70761],{},[49,70757,10914],{}," immediately stops the ",[49,70760,10900],{}," loop.",[14,70763,70764],{},"Without it, the chatbot would keep running forever.",[37,70766,13563],{"id":13562},[14,70768,70769],{},"Here is one example conversation:",[42,70771,70774],{"className":70772,"code":70773,"language":955,"meta":47},[953],"Simple Chatbot\nType 'bye' to stop.\nYou: hello\nBot: Hi!\nYou: what is your favorite color\nBot: Sorry, I don't understand.\nYou: bye\nBot: Goodbye!\n",[49,70775,70773],{"__ignoreMap":47},[14,70777,70778],{},"This example shows:",[309,70780,70781,70786,70792],{},[24,70782,70783,70784],{},"a known message: ",[49,70785,6363],{},[24,70787,70788,70789],{},"an unknown message: ",[49,70790,70791],{},"what is your favorite color",[24,70793,70794,70795],{},"the exit message: ",[49,70796,70121],{},[37,70798,70800],{"id":70799},"how-the-chatbot-logic-works","How the chatbot logic works",[14,70802,70803],{},"The program handles one message at a time.",[14,70805,70806],{},"The logic is simple:",[21,70808,70809,70812,70818,70821,70824],{},[24,70810,70811],{},"Ask the user for a message",[24,70813,70814,70815],{},"Clean the text with ",[49,70816,70817],{},"lower().strip()",[24,70819,70820],{},"Compare the message with known phrases",[24,70822,70823],{},"Print the matching response",[24,70825,70826],{},"If no phrase matches, use the fallback reply",[14,70828,70829,70830,186],{},"This kind of chatbot is called a ",[5503,70831,70133],{},[14,70833,70834],{},"It works well for learning because:",[309,70836,70837,70840,70843],{},[24,70838,70839],{},"the logic is easy to follow",[24,70841,70842],{},"each response is predictable",[24,70844,70845],{},"you can add new replies step by step",[37,70847,70849],{"id":70848},"improved-version-with-more-responses","Improved version with more responses",[14,70851,70852],{},"Here is a slightly larger version with a few more commands:",[42,70854,70856],{"className":44,"code":70855,"language":46,"meta":47,"style":47},"print(\"Simple Chatbot\")\nprint(\"Type 'bye' to stop.\")\n\nwhile True:\n    message = input(\"You: \").lower().strip()\n\n    if message == \"hello\":\n        print(\"Bot: Hi!\")\n    elif message == \"how are you\":\n        print(\"Bot: I am doing well.\")\n    elif message == \"name\":\n        print(\"Bot: I am a simple Python chatbot.\")\n    elif message == \"help\":\n        print(\"Bot: Try typing hello, how are you, name, or bye.\")\n    elif message == \"bye\":\n        print(\"Bot: Goodbye!\")\n        break\n    else:\n        print(\"Bot: Sorry, I don't understand.\")\n",[49,70857,70858,70872,70886,70890,70898,70924,70928,70944,70958,70974,70988,71004,71019,71035,71050,71066,71080,71084,71090],{"__ignoreMap":47},[52,70859,70860,70862,70864,70866,70868,70870],{"class":54,"line":55},[52,70861,242],{"class":241},[52,70863,193],{"class":102},[52,70865,115],{"class":83},[52,70867,70224],{"class":87},[52,70869,115],{"class":83},[52,70871,211],{"class":102},[52,70873,70874,70876,70878,70880,70882,70884],{"class":54,"line":66},[52,70875,242],{"class":241},[52,70877,193],{"class":102},[52,70879,115],{"class":83},[52,70881,70239],{"class":87},[52,70883,115],{"class":83},[52,70885,211],{"class":102},[52,70887,70888],{"class":54,"line":73},[52,70889,70],{"emptyLinePlaceholder":69},[52,70891,70892,70894,70896],{"class":54,"line":94},[52,70893,10772],{"class":58},[52,70895,10943],{"class":160},[52,70897,1330],{"class":102},[52,70899,70900,70902,70904,70906,70908,70910,70912,70914,70916,70918,70920,70922],{"class":54,"line":106},[52,70901,6163],{"class":62},[52,70903,80],{"class":79},[52,70905,11037],{"class":241},[52,70907,193],{"class":102},[52,70909,115],{"class":83},[52,70911,70270],{"class":87},[52,70913,115],{"class":83},[52,70915,54653],{"class":102},[52,70917,30237],{"class":189},[52,70919,18679],{"class":102},[52,70921,18630],{"class":189},[52,70923,230],{"class":102},[52,70925,70926],{"class":54,"line":128},[52,70927,70],{"emptyLinePlaceholder":69},[52,70929,70930,70932,70934,70936,70938,70940,70942],{"class":54,"line":148},[52,70931,1438],{"class":58},[52,70933,70293],{"class":62},[52,70935,11062],{"class":79},[52,70937,84],{"class":83},[52,70939,6363],{"class":87},[52,70941,115],{"class":83},[52,70943,1330],{"class":102},[52,70945,70946,70948,70950,70952,70954,70956],{"class":54,"line":164},[52,70947,1470],{"class":241},[52,70949,193],{"class":102},[52,70951,115],{"class":83},[52,70953,70314],{"class":87},[52,70955,115],{"class":83},[52,70957,211],{"class":102},[52,70959,70960,70962,70964,70966,70968,70970,70972],{"class":54,"line":170},[52,70961,11151],{"class":58},[52,70963,70293],{"class":62},[52,70965,11062],{"class":79},[52,70967,84],{"class":83},[52,70969,70331],{"class":87},[52,70971,115],{"class":83},[52,70973,1330],{"class":102},[52,70975,70976,70978,70980,70982,70984,70986],{"class":54,"line":175},[52,70977,1470],{"class":241},[52,70979,193],{"class":102},[52,70981,115],{"class":83},[52,70983,70346],{"class":87},[52,70985,115],{"class":83},[52,70987,211],{"class":102},[52,70989,70990,70992,70994,70996,70998,71000,71002],{"class":54,"line":214},[52,70991,11151],{"class":58},[52,70993,70293],{"class":62},[52,70995,11062],{"class":79},[52,70997,84],{"class":83},[52,70999,2147],{"class":87},[52,71001,115],{"class":83},[52,71003,1330],{"class":102},[52,71005,71006,71008,71010,71012,71015,71017],{"class":54,"line":233},[52,71007,1470],{"class":241},[52,71009,193],{"class":102},[52,71011,115],{"class":83},[52,71013,71014],{"class":87},"Bot: I am a simple Python chatbot.",[52,71016,115],{"class":83},[52,71018,211],{"class":102},[52,71020,71021,71023,71025,71027,71029,71031,71033],{"class":54,"line":238},[52,71022,11151],{"class":58},[52,71024,70293],{"class":62},[52,71026,11062],{"class":79},[52,71028,84],{"class":83},[52,71030,8930],{"class":87},[52,71032,115],{"class":83},[52,71034,1330],{"class":102},[52,71036,71037,71039,71041,71043,71046,71048],{"class":54,"line":272},[52,71038,1470],{"class":241},[52,71040,193],{"class":102},[52,71042,115],{"class":83},[52,71044,71045],{"class":87},"Bot: Try typing hello, how are you, name, or bye.",[52,71047,115],{"class":83},[52,71049,211],{"class":102},[52,71051,71052,71054,71056,71058,71060,71062,71064],{"class":54,"line":1348},[52,71053,11151],{"class":58},[52,71055,70293],{"class":62},[52,71057,11062],{"class":79},[52,71059,84],{"class":83},[52,71061,70121],{"class":87},[52,71063,115],{"class":83},[52,71065,1330],{"class":102},[52,71067,71068,71070,71072,71074,71076,71078],{"class":54,"line":1376},[52,71069,1470],{"class":241},[52,71071,193],{"class":102},[52,71073,115],{"class":83},[52,71075,70377],{"class":87},[52,71077,115],{"class":83},[52,71079,211],{"class":102},[52,71081,71082],{"class":54,"line":1381},[52,71083,11499],{"class":58},[52,71085,71086,71088],{"class":54,"line":1406},[52,71087,1567],{"class":58},[52,71089,1330],{"class":102},[52,71091,71092,71094,71096,71098,71100,71102],{"class":54,"line":1430},[52,71093,1470],{"class":241},[52,71095,193],{"class":102},[52,71097,115],{"class":83},[52,71099,70402],{"class":87},[52,71101,115],{"class":83},[52,71103,211],{"class":102},[14,71105,71106,71107,10537,71109,71111],{},"This version uses the same ",[49,71108,1313],{},[49,71110,10787],{}," structure, so it should still feel familiar.",[14,71113,71114],{},"You can test messages like:",[309,71116,71117,71121,71125,71129,71133],{},[24,71118,71119],{},[49,71120,6363],{},[24,71122,71123],{},[49,71124,70331],{},[24,71126,71127],{},[49,71128,2147],{},[24,71130,71131],{},[49,71132,8930],{},[24,71134,71135],{},[49,71136,70121],{},[14,71138,71139,71140,186],{},"Later, you could move these replies into a dictionary for cleaner code. If you want to learn that next, see ",[372,71141,10883],{"href":10882},[37,71143,6402],{"id":6401},[14,71145,71146],{},"Here are some common problems beginners run into with this project.",[639,71148,71150,71151],{"id":71149},"forgetting-parentheses-after-input","Forgetting parentheses after ",[49,71152,13610],{},[14,71154,8124],{},[42,71156,71158],{"className":44,"code":71157,"language":46,"meta":47,"style":47},"message = input.lower().strip()\n",[49,71159,71160],{"__ignoreMap":47},[52,71161,71162,71164,71166,71168,71170,71172,71174,71176],{"class":54,"line":55},[52,71163,6121],{"class":62},[52,71165,80],{"class":79},[52,71167,11037],{"class":241},[52,71169,186],{"class":102},[52,71171,30237],{"class":189},[52,71173,18679],{"class":102},[52,71175,18630],{"class":189},[52,71177,230],{"class":102},[14,71179,12800],{},[42,71181,71182],{"className":44,"code":70488,"language":46,"meta":47,"style":47},[49,71183,71184],{"__ignoreMap":47},[52,71185,71186,71188,71190,71192,71194,71196,71198,71200,71202,71204,71206,71208],{"class":54,"line":55},[52,71187,6121],{"class":62},[52,71189,80],{"class":79},[52,71191,11037],{"class":241},[52,71193,193],{"class":102},[52,71195,115],{"class":83},[52,71197,70270],{"class":87},[52,71199,115],{"class":83},[52,71201,54653],{"class":102},[52,71203,30237],{"class":189},[52,71205,18679],{"class":102},[52,71207,18630],{"class":189},[52,71209,230],{"class":102},[14,71211,71212,71214],{},[49,71213,13610],{}," is a function, so it needs parentheses.",[639,71216,10294,71218,10598,71220,71222],{"id":71217},"using-instead-of-in-conditions",[49,71219,80],{},[49,71221,11062],{}," in conditions",[14,71224,8124],{},[42,71226,71228],{"className":44,"code":71227,"language":46,"meta":47,"style":47},"if message = \"hello\":\n    print(\"Bot: Hi!\")\n",[49,71229,71230,71246],{"__ignoreMap":47},[52,71231,71232,71234,71236,71238,71240,71242,71244],{"class":54,"line":55},[52,71233,1313],{"class":58},[52,71235,70293],{"class":62},[52,71237,80],{"class":79},[52,71239,84],{"class":83},[52,71241,6363],{"class":87},[52,71243,115],{"class":83},[52,71245,1330],{"class":102},[52,71247,71248,71250,71252,71254,71256,71258],{"class":54,"line":66},[52,71249,1599],{"class":241},[52,71251,193],{"class":102},[52,71253,115],{"class":83},[52,71255,70314],{"class":87},[52,71257,115],{"class":83},[52,71259,211],{"class":102},[14,71261,12800],{},[42,71263,71265],{"className":44,"code":71264,"language":46,"meta":47,"style":47},"if message == \"hello\":\n    print(\"Bot: Hi!\")\n",[49,71266,71267,71283],{"__ignoreMap":47},[52,71268,71269,71271,71273,71275,71277,71279,71281],{"class":54,"line":55},[52,71270,1313],{"class":58},[52,71272,70293],{"class":62},[52,71274,11062],{"class":79},[52,71276,84],{"class":83},[52,71278,6363],{"class":87},[52,71280,115],{"class":83},[52,71282,1330],{"class":102},[52,71284,71285,71287,71289,71291,71293,71295],{"class":54,"line":66},[52,71286,1599],{"class":241},[52,71288,193],{"class":102},[52,71290,115],{"class":83},[52,71292,70314],{"class":87},[52,71294,115],{"class":83},[52,71296,211],{"class":102},[309,71298,71299,71304],{},[24,71300,71301,71303],{},[49,71302,80],{}," assigns a value",[24,71305,71306,71308],{},[49,71307,11062],{}," compares values",[639,71310,71312,71313,2587,71315,46922,71317],{"id":71311},"forgetting-the-colon-after-if-elif-or-else","Forgetting the colon after ",[49,71314,1313],{},[49,71316,10787],{},[49,71318,1591],{},[14,71320,8124],{},[42,71322,71324],{"className":44,"code":71323,"language":46,"meta":47,"style":47},"if message == \"hello\"\n    print(\"Bot: Hi!\")\n",[49,71325,71326,71340],{"__ignoreMap":47},[52,71327,71328,71330,71332,71334,71336,71338],{"class":54,"line":55},[52,71329,1313],{"class":58},[52,71331,70293],{"class":62},[52,71333,11062],{"class":79},[52,71335,84],{"class":83},[52,71337,6363],{"class":87},[52,71339,91],{"class":83},[52,71341,71342,71344,71346,71348,71350,71352],{"class":54,"line":66},[52,71343,1599],{"class":241},[52,71345,193],{"class":102},[52,71347,115],{"class":83},[52,71349,70314],{"class":87},[52,71351,115],{"class":83},[52,71353,211],{"class":102},[14,71355,12800],{},[42,71357,71358],{"className":44,"code":71264,"language":46,"meta":47,"style":47},[49,71359,71360,71376],{"__ignoreMap":47},[52,71361,71362,71364,71366,71368,71370,71372,71374],{"class":54,"line":55},[52,71363,1313],{"class":58},[52,71365,70293],{"class":62},[52,71367,11062],{"class":79},[52,71369,84],{"class":83},[52,71371,6363],{"class":87},[52,71373,115],{"class":83},[52,71375,1330],{"class":102},[52,71377,71378,71380,71382,71384,71386,71388],{"class":54,"line":66},[52,71379,1599],{"class":241},[52,71381,193],{"class":102},[52,71383,115],{"class":83},[52,71385,70314],{"class":87},[52,71387,115],{"class":83},[52,71389,211],{"class":102},[14,71391,71392,71393,186],{},"A missing colon causes a syntax error. See ",[372,71394,3984,71395],{"href":38903},[49,71396,38904],{},[639,71398,71400,71401,71403],{"id":71399},"writing-break-outside-the-loop","Writing ",[49,71402,10914],{}," outside the loop",[14,71405,8124],{},[42,71407,71409],{"className":44,"code":71408,"language":46,"meta":47,"style":47},"if message == \"bye\":\n    print(\"Bot: Goodbye!\")\n\nbreak\n",[49,71410,71411,71427,71441,71445],{"__ignoreMap":47},[52,71412,71413,71415,71417,71419,71421,71423,71425],{"class":54,"line":55},[52,71414,1313],{"class":58},[52,71416,70293],{"class":62},[52,71418,11062],{"class":79},[52,71420,84],{"class":83},[52,71422,70121],{"class":87},[52,71424,115],{"class":83},[52,71426,1330],{"class":102},[52,71428,71429,71431,71433,71435,71437,71439],{"class":54,"line":66},[52,71430,1599],{"class":241},[52,71432,193],{"class":102},[52,71434,115],{"class":83},[52,71436,70377],{"class":87},[52,71438,115],{"class":83},[52,71440,211],{"class":102},[52,71442,71443],{"class":54,"line":73},[52,71444,70],{"emptyLinePlaceholder":69},[52,71446,71447],{"class":54,"line":94},[52,71448,52093],{"class":58},[14,71450,12800],{},[42,71452,71454],{"className":44,"code":71453,"language":46,"meta":47,"style":47},"if message == \"bye\":\n    print(\"Bot: Goodbye!\")\n    break\n",[49,71455,71456,71472,71486],{"__ignoreMap":47},[52,71457,71458,71460,71462,71464,71466,71468,71470],{"class":54,"line":55},[52,71459,1313],{"class":58},[52,71461,70293],{"class":62},[52,71463,11062],{"class":79},[52,71465,84],{"class":83},[52,71467,70121],{"class":87},[52,71469,115],{"class":83},[52,71471,1330],{"class":102},[52,71473,71474,71476,71478,71480,71482,71484],{"class":54,"line":66},[52,71475,1599],{"class":241},[52,71477,193],{"class":102},[52,71479,115],{"class":83},[52,71481,70377],{"class":87},[52,71483,115],{"class":83},[52,71485,211],{"class":102},[52,71487,71488],{"class":54,"line":73},[52,71489,52013],{"class":58},[14,71491,71492,71494],{},[49,71493,10914],{}," must be inside a loop.",[639,71496,71498],{"id":71497},"not-handling-uppercase-letters-or-extra-spaces","Not handling uppercase letters or extra spaces",[14,71500,71501,71502,71505],{},"If you do not use ",[49,71503,71504],{},".lower().strip()",", the chatbot may fail to match input like:",[309,71507,71508,71512,71516],{},[24,71509,71510],{},[49,71511,38698],{},[24,71513,71514],{},[49,71515,70562],{},[24,71517,71518],{},[49,71519,70567],{},[14,71521,71522],{},"That is why this line is helpful:",[42,71524,71525],{"className":44,"code":70488,"language":46,"meta":47,"style":47},[49,71526,71527],{"__ignoreMap":47},[52,71528,71529,71531,71533,71535,71537,71539,71541,71543,71545,71547,71549,71551],{"class":54,"line":55},[52,71530,6121],{"class":62},[52,71532,80],{"class":79},[52,71534,11037],{"class":241},[52,71536,193],{"class":102},[52,71538,115],{"class":83},[52,71540,70270],{"class":87},[52,71542,115],{"class":83},[52,71544,54653],{"class":102},[52,71546,30237],{"class":189},[52,71548,18679],{"class":102},[52,71550,18630],{"class":189},[52,71552,230],{"class":102},[639,71554,53258],{"id":53257},[14,71556,71557],{},"You may also run into these problems:",[309,71559,71560,71566,71569,71581,71587],{},[24,71561,71562,71563,71565],{},"The program never exits because there is no ",[49,71564,10914],{}," condition",[24,71567,71568],{},"The chatbot does not recognize input because the text case does not match",[24,71570,3233,71571,71574,71575,2587,71577,46922,71579],{},[49,71572,71573],{},"SyntaxError"," happens because a colon is missing after ",[49,71576,1313],{},[49,71578,10787],{},[49,71580,1591],{},[24,71582,7200,71583,71586],{},[49,71584,71585],{},"IndentationError"," happens because blocks are not indented correctly",[24,71588,71589],{},"The wrong comparison operator is used in conditions",[37,71591,71593],{"id":71592},"ways-to-extend-the-project","Ways to extend the project",[14,71595,71596],{},"Once the basic version works, try improving it.",[14,71598,23559],{},[309,71600,71601,71604,71610,71613,71616],{},[24,71602,71603],{},"Add more fixed questions and answers",[24,71605,71606,71607,71609],{},"Add a ",[49,71608,8930],{}," command that lists available inputs",[24,71611,71612],{},"Store replies in a dictionary as a next-step refactor",[24,71614,71615],{},"Count how many messages the user sends",[24,71617,71618],{},"Save a chat log to a file",[14,71620,71621],{},"Here is one small extension idea: count messages.",[42,71623,71625],{"className":44,"code":71624,"language":46,"meta":47,"style":47},"print(\"Simple Chatbot\")\nprint(\"Type 'bye' to stop.\")\n\ncount = 0\n\nwhile True:\n    message = input(\"You: \").lower().strip()\n    count += 1\n\n    if message == \"hello\":\n        print(\"Bot: Hi!\")\n    elif message == \"bye\":\n        print(\"Bot: Goodbye!\")\n        print(\"Bot: You sent\", count, \"messages.\")\n        break\n    else:\n        print(\"Bot: Sorry, I don't understand.\")\n",[49,71626,71627,71641,71655,71659,71667,71671,71679,71705,71714,71718,71734,71748,71764,71778,71807,71811,71817],{"__ignoreMap":47},[52,71628,71629,71631,71633,71635,71637,71639],{"class":54,"line":55},[52,71630,242],{"class":241},[52,71632,193],{"class":102},[52,71634,115],{"class":83},[52,71636,70224],{"class":87},[52,71638,115],{"class":83},[52,71640,211],{"class":102},[52,71642,71643,71645,71647,71649,71651,71653],{"class":54,"line":66},[52,71644,242],{"class":241},[52,71646,193],{"class":102},[52,71648,115],{"class":83},[52,71650,70239],{"class":87},[52,71652,115],{"class":83},[52,71654,211],{"class":102},[52,71656,71657],{"class":54,"line":73},[52,71658,70],{"emptyLinePlaceholder":69},[52,71660,71661,71663,71665],{"class":54,"line":94},[52,71662,55300],{"class":62},[52,71664,80],{"class":79},[52,71666,34598],{"class":121},[52,71668,71669],{"class":54,"line":106},[52,71670,70],{"emptyLinePlaceholder":69},[52,71672,71673,71675,71677],{"class":54,"line":128},[52,71674,10772],{"class":58},[52,71676,10943],{"class":160},[52,71678,1330],{"class":102},[52,71680,71681,71683,71685,71687,71689,71691,71693,71695,71697,71699,71701,71703],{"class":54,"line":148},[52,71682,6163],{"class":62},[52,71684,80],{"class":79},[52,71686,11037],{"class":241},[52,71688,193],{"class":102},[52,71690,115],{"class":83},[52,71692,70270],{"class":87},[52,71694,115],{"class":83},[52,71696,54653],{"class":102},[52,71698,30237],{"class":189},[52,71700,18679],{"class":102},[52,71702,18630],{"class":189},[52,71704,230],{"class":102},[52,71706,71707,71710,71712],{"class":54,"line":164},[52,71708,71709],{"class":62},"    count ",[52,71711,34682],{"class":79},[52,71713,8645],{"class":121},[52,71715,71716],{"class":54,"line":170},[52,71717,70],{"emptyLinePlaceholder":69},[52,71719,71720,71722,71724,71726,71728,71730,71732],{"class":54,"line":175},[52,71721,1438],{"class":58},[52,71723,70293],{"class":62},[52,71725,11062],{"class":79},[52,71727,84],{"class":83},[52,71729,6363],{"class":87},[52,71731,115],{"class":83},[52,71733,1330],{"class":102},[52,71735,71736,71738,71740,71742,71744,71746],{"class":54,"line":214},[52,71737,1470],{"class":241},[52,71739,193],{"class":102},[52,71741,115],{"class":83},[52,71743,70314],{"class":87},[52,71745,115],{"class":83},[52,71747,211],{"class":102},[52,71749,71750,71752,71754,71756,71758,71760,71762],{"class":54,"line":233},[52,71751,11151],{"class":58},[52,71753,70293],{"class":62},[52,71755,11062],{"class":79},[52,71757,84],{"class":83},[52,71759,70121],{"class":87},[52,71761,115],{"class":83},[52,71763,1330],{"class":102},[52,71765,71766,71768,71770,71772,71774,71776],{"class":54,"line":238},[52,71767,1470],{"class":241},[52,71769,193],{"class":102},[52,71771,115],{"class":83},[52,71773,70377],{"class":87},[52,71775,115],{"class":83},[52,71777,211],{"class":102},[52,71779,71780,71782,71784,71786,71789,71791,71793,71796,71798,71800,71803,71805],{"class":54,"line":272},[52,71781,1470],{"class":241},[52,71783,193],{"class":102},[52,71785,115],{"class":83},[52,71787,71788],{"class":87},"Bot: You sent",[52,71790,115],{"class":83},[52,71792,199],{"class":102},[52,71794,71795],{"class":189}," count",[52,71797,199],{"class":102},[52,71799,84],{"class":83},[52,71801,71802],{"class":87},"messages.",[52,71804,115],{"class":83},[52,71806,211],{"class":102},[52,71808,71809],{"class":54,"line":1348},[52,71810,11499],{"class":58},[52,71812,71813,71815],{"class":54,"line":1376},[52,71814,1567],{"class":58},[52,71816,1330],{"class":102},[52,71818,71819,71821,71823,71825,71827,71829],{"class":54,"line":1381},[52,71820,1470],{"class":241},[52,71822,193],{"class":102},[52,71824,115],{"class":83},[52,71826,70402],{"class":87},[52,71828,115],{"class":83},[52,71830,211],{"class":102},[14,71832,71833],{},"This adds one new idea without making the program too complex.",[37,71835,71837],{"id":71836},"when-to-move-to-the-next-topic","When to move to the next topic",[14,71839,71840],{},"You are ready for the next step when you understand:",[309,71842,71843,71847,71856,71862],{},[24,71844,4881,71845,54451],{},[49,71846,10597],{},[24,71848,4881,71849,2587,71851,11556,71853,71855],{},[49,71850,1313],{},[49,71852,10787],{},[49,71854,1591],{}," choose different actions",[24,71857,71858,71859,71861],{},"how a ",[49,71860,10900],{}," loop repeats the chatbot",[24,71863,4881,71864,71866],{},[49,71865,10914],{}," stops the loop",[14,71868,71869],{},"Good next topics are:",[309,71871,71872,71875,71878],{},[24,71873,71874],{},"functions to organize chatbot code better",[24,71876,71877],{},"dictionaries for cleaner reply matching",[24,71879,71880],{},"file handling if you want to save chat history",[14,71882,71883,71884,186],{},"A useful next lesson is ",[372,71885,21368],{"href":21367},[37,71887,1942],{"id":1941},[639,71889,71891],{"id":71890},"is-this-a-real-ai-chatbot","Is this a real AI chatbot?",[14,71893,71894],{},"No. This example is a simple rule-based chatbot that matches user input to fixed replies.",[639,71896,1956,71898,759,71900,1995],{"id":71897},"why-use-lower-and-strip",[49,71899,70198],{},[49,71901,18831],{},[14,71903,71904],{},"They make input matching easier by removing extra spaces and converting text to lowercase.",[639,71906,71908],{"id":71907},"can-i-add-more-responses","Can I add more responses?",[14,71910,71911,71912,71914],{},"Yes. Add more ",[49,71913,10787],{}," blocks or later move the replies into a dictionary.",[639,71916,71918],{"id":71917},"why-does-the-chatbot-keep-running","Why does the chatbot keep running?",[14,71920,71921,71922,71924,71925,71927],{},"It runs inside a ",[49,71923,10900],{}," loop until the user enters the exit word and ",[49,71926,10914],{}," stops the loop.",[639,71929,71931],{"id":71930},"what-python-topics-should-i-know-first","What Python topics should I know first?",[14,71933,71934,71935,71937,71938,71940],{},"Basic input, strings, ",[49,71936,1313],{}," statements, and ",[49,71939,10772],{}," loops are enough for this project.",[37,71942,2005],{"id":2004},[309,71944,71945,71951,71955,71959,71963,71967,71972],{},[24,71946,71947],{},[372,71948,10646,71949,3711],{"href":10606},[49,71950,10597],{},[24,71952,71953],{},[372,71954,52086],{"href":52085},[24,71956,71957],{},[372,71958,13426],{"href":13425},[24,71960,71961],{},[372,71962,38969],{"href":38968},[24,71964,71965],{},[372,71966,22029],{"href":21367},[24,71968,71969],{},[372,71970,71971],{"href":38903},"SyntaxError: invalid syntax fix",[24,71973,71974],{},[372,71975,71976],{"href":8488},"IndentationError: expected an indented block fix",[14,71978,71979],{},"Copy the chatbot, run it, and test a few messages.",[14,71981,71982],{},"Then improve it by:",[309,71984,71985,71988,71991],{},[24,71986,71987],{},"adding new replies",[24,71989,71990],{},"moving code into functions",[24,71992,71993],{},"saving chat history to a file",[14,71995,71996],{},"That is a great way to turn one small example into real Python practice.",[2034,71998,71999],{},"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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":72001},[72002,72003,72004,72007,72008,72009,72010,72022,72023,72024,72032],{"id":9294,"depth":66,"text":9295},{"id":70155,"depth":66,"text":70156},{"id":70204,"depth":66,"text":70205,"children":72005},[72006],{"id":4100,"depth":73,"text":4101},{"id":13562,"depth":66,"text":13563},{"id":70799,"depth":66,"text":70800},{"id":70848,"depth":66,"text":70849},{"id":6401,"depth":66,"text":6402,"children":72011},[72012,72014,72016,72018,72020,72021],{"id":71149,"depth":73,"text":72013},"Forgetting parentheses after input",{"id":71217,"depth":73,"text":72015},"Using = instead of == in conditions",{"id":71311,"depth":73,"text":72017},"Forgetting the colon after if, elif, or else",{"id":71399,"depth":73,"text":72019},"Writing break outside the loop",{"id":71497,"depth":73,"text":71498},{"id":53257,"depth":73,"text":53258},{"id":71592,"depth":66,"text":71593},{"id":71836,"depth":66,"text":71837},{"id":1941,"depth":66,"text":1942,"children":72025},[72026,72027,72029,72030,72031],{"id":71890,"depth":73,"text":71891},{"id":71897,"depth":73,"text":72028},"Why use lower() and strip()?",{"id":71907,"depth":73,"text":71908},{"id":71917,"depth":73,"text":71918},{"id":71930,"depth":73,"text":71931},{"id":2004,"depth":66,"text":2005},"Master python simple chatbot example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-simple-chatbot-example",{"title":70087,"description":72033},"examples\u002Fpython-simple-chatbot-example","ccjQToAOJnGlGte8UQYi-A2YAiC-4w2y04v1fjofh7U",{"id":72040,"title":72041,"body":72042,"description":73864,"extension":2075,"meta":73865,"navigation":69,"path":73866,"seo":73867,"stem":73868,"__hash__":73869},"content\u002Fexamples\u002Fpython-simple-logging-system-example.md","Python Simple Logging System Example",{"type":7,"value":72043,"toc":73842},[72044,72047,72055,72058,72309,72312,72314,72317,72320,72338,72340,72607,72609,72640,72647,72651,72654,72657,72674,72679,72683,72686,72706,72710,72883,72886,72913,72919,72923,72926,72948,72951,73177,73180,73184,73189,73192,73206,73208,73214,73217,73227,73230,73242,73245,73256,73259,73277,73281,73284,73287,73309,73312,73583,73592,73594,73597,73614,73618,73621,73689,73695,73700,73768,73773,73775,73779,73784,73788,73791,73795,73798,73802,73808,73810,73836,73839],[10,72045,72041],{"id":72046},"python-simple-logging-system-example",[14,72048,72049,72050,759,72052,72054],{},"Build a small Python logging system that writes messages with timestamps and levels like ",[49,72051,46094],{},[49,72053,46088],{},". This example helps beginners understand how to record program activity in a simple, practical way.",[14,72056,72057],{},"If you want a quick working version first, use this:",[42,72059,72061],{"className":44,"code":72060,"language":46,"meta":47,"style":47},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\\n\"\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line)\n\n\nlog_message(\"INFO\", \"Program started\")\nlog_message(\"ERROR\", \"Something went wrong\")\nprint(\"Messages written to app.log\")\n",[49,72062,72063,72073,72077,72081,72111,72143,72182,72186,72225,72239,72243,72247,72271,72294],{"__ignoreMap":47},[52,72064,72065,72067,72069,72071],{"class":54,"line":55},[52,72066,25329],{"class":58},[52,72068,29194],{"class":62},[52,72070,59],{"class":58},[52,72072,29199],{"class":62},[52,72074,72075],{"class":54,"line":66},[52,72076,70],{"emptyLinePlaceholder":69},[52,72078,72079],{"class":54,"line":73},[52,72080,70],{"emptyLinePlaceholder":69},[52,72082,72083,72085,72088,72090,72092,72094,72096,72098,72101,72103,72105,72107,72109],{"class":54,"line":94},[52,72084,7410],{"class":1475},[52,72086,72087],{"class":7065}," log_message",[52,72089,193],{"class":102},[52,72091,46391],{"class":7018},[52,72093,199],{"class":102},[52,72095,6168],{"class":7018},[52,72097,199],{"class":102},[52,72099,72100],{"class":7018}," filename",[52,72102,80],{"class":79},[52,72104,115],{"class":83},[52,72106,46144],{"class":87},[52,72108,115],{"class":83},[52,72110,3114],{"class":102},[52,72112,72113,72116,72118,72120,72122,72124,72126,72128,72130,72132,72134,72136,72139,72141],{"class":54,"line":106},[52,72114,72115],{"class":62},"    timestamp ",[52,72117,80],{"class":79},[52,72119,29241],{"class":62},[52,72121,186],{"class":102},[52,72123,29246],{"class":189},[52,72125,18679],{"class":102},[52,72127,29251],{"class":189},[52,72129,193],{"class":102},[52,72131,115],{"class":83},[52,72133,29258],{"class":87},[52,72135,29261],{"class":121},[52,72137,72138],{"class":87}," %H:%M:%S",[52,72140,115],{"class":83},[52,72142,211],{"class":102},[52,72144,72145,72148,72150,72152,72155,72157,72159,72161,72164,72166,72168,72170,72172,72174,72176,72178,72180],{"class":54,"line":128},[52,72146,72147],{"class":62},"    line ",[52,72149,80],{"class":79},[52,72151,6126],{"class":1475},[52,72153,72154],{"class":87},"\"[",[52,72156,1482],{"class":121},[52,72158,29288],{"class":62},[52,72160,1488],{"class":121},[52,72162,72163],{"class":87},"] ",[52,72165,1482],{"class":121},[52,72167,46391],{"class":62},[52,72169,1488],{"class":121},[52,72171,2578],{"class":87},[52,72173,1482],{"class":121},[52,72175,6188],{"class":62},[52,72177,1488],{"class":121},[52,72179,10956],{"class":2533},[52,72181,91],{"class":87},[52,72183,72184],{"class":54,"line":148},[52,72185,70],{"emptyLinePlaceholder":69},[52,72187,72188,72190,72192,72194,72197,72199,72201,72203,72205,72207,72209,72211,72213,72215,72217,72219,72221,72223],{"class":54,"line":164},[52,72189,24616],{"class":58},[52,72191,14635],{"class":241},[52,72193,193],{"class":102},[52,72195,72196],{"class":189},"filename",[52,72198,199],{"class":102},[52,72200,84],{"class":83},[52,72202,372],{"class":87},[52,72204,115],{"class":83},[52,72206,199],{"class":102},[52,72208,14659],{"class":202},[52,72210,80],{"class":79},[52,72212,115],{"class":83},[52,72214,14666],{"class":87},[52,72216,115],{"class":83},[52,72218,1521],{"class":102},[52,72220,3567],{"class":58},[52,72222,14676],{"class":14675},[52,72224,1330],{"class":102},[52,72226,72227,72229,72231,72233,72235,72237],{"class":54,"line":170},[52,72228,24643],{"class":14675},[52,72230,186],{"class":102},[52,72232,23849],{"class":189},[52,72234,193],{"class":102},[52,72236,54],{"class":189},[52,72238,211],{"class":102},[52,72240,72241],{"class":54,"line":175},[52,72242,70],{"emptyLinePlaceholder":69},[52,72244,72245],{"class":54,"line":214},[52,72246,70],{"emptyLinePlaceholder":69},[52,72248,72249,72252,72254,72256,72258,72260,72262,72264,72267,72269],{"class":54,"line":233},[52,72250,72251],{"class":189},"log_message",[52,72253,193],{"class":102},[52,72255,115],{"class":83},[52,72257,46094],{"class":87},[52,72259,115],{"class":83},[52,72261,199],{"class":102},[52,72263,84],{"class":83},[52,72265,72266],{"class":87},"Program started",[52,72268,115],{"class":83},[52,72270,211],{"class":102},[52,72272,72273,72275,72277,72279,72281,72283,72285,72287,72290,72292],{"class":54,"line":238},[52,72274,72251],{"class":189},[52,72276,193],{"class":102},[52,72278,115],{"class":83},[52,72280,46088],{"class":87},[52,72282,115],{"class":83},[52,72284,199],{"class":102},[52,72286,84],{"class":83},[52,72288,72289],{"class":87},"Something went wrong",[52,72291,115],{"class":83},[52,72293,211],{"class":102},[52,72295,72296,72298,72300,72302,72305,72307],{"class":54,"line":272},[52,72297,242],{"class":241},[52,72299,193],{"class":102},[52,72301,115],{"class":83},[52,72303,72304],{"class":87},"Messages written to app.log",[52,72306,115],{"class":83},[52,72308,211],{"class":102},[14,72310,72311],{},"This version appends log messages to a file so old messages are not erased.",[37,72313,323],{"id":322},[14,72315,72316],{},"This script creates a very simple logging system.",[14,72318,72319],{},"It:",[309,72321,72322,72325,72328,72331],{},[24,72323,72324],{},"Creates a reusable function for writing log messages",[24,72326,72327],{},"Adds a timestamp to each message",[24,72329,72330],{},"Stores logs in a text file",[24,72332,72333,72334,759,72336],{},"Uses simple log levels such as ",[49,72335,46094],{},[49,72337,46088],{},[14,72339,45415],{},[42,72341,72343],{"className":44,"code":72342,"language":46,"meta":47,"style":47},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\\n\"\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line)\n\n\nlog_message(\"INFO\", \"Program started\")\nlog_message(\"INFO\", \"Loading user data\")\nlog_message(\"ERROR\", \"Could not connect to server\")\n\nprint(\"Messages written to app.log\")\n",[49,72344,72345,72355,72359,72363,72391,72421,72457,72461,72499,72513,72517,72521,72543,72566,72589,72593],{"__ignoreMap":47},[52,72346,72347,72349,72351,72353],{"class":54,"line":55},[52,72348,25329],{"class":58},[52,72350,29194],{"class":62},[52,72352,59],{"class":58},[52,72354,29199],{"class":62},[52,72356,72357],{"class":54,"line":66},[52,72358,70],{"emptyLinePlaceholder":69},[52,72360,72361],{"class":54,"line":73},[52,72362,70],{"emptyLinePlaceholder":69},[52,72364,72365,72367,72369,72371,72373,72375,72377,72379,72381,72383,72385,72387,72389],{"class":54,"line":94},[52,72366,7410],{"class":1475},[52,72368,72087],{"class":7065},[52,72370,193],{"class":102},[52,72372,46391],{"class":7018},[52,72374,199],{"class":102},[52,72376,6168],{"class":7018},[52,72378,199],{"class":102},[52,72380,72100],{"class":7018},[52,72382,80],{"class":79},[52,72384,115],{"class":83},[52,72386,46144],{"class":87},[52,72388,115],{"class":83},[52,72390,3114],{"class":102},[52,72392,72393,72395,72397,72399,72401,72403,72405,72407,72409,72411,72413,72415,72417,72419],{"class":54,"line":106},[52,72394,72115],{"class":62},[52,72396,80],{"class":79},[52,72398,29241],{"class":62},[52,72400,186],{"class":102},[52,72402,29246],{"class":189},[52,72404,18679],{"class":102},[52,72406,29251],{"class":189},[52,72408,193],{"class":102},[52,72410,115],{"class":83},[52,72412,29258],{"class":87},[52,72414,29261],{"class":121},[52,72416,72138],{"class":87},[52,72418,115],{"class":83},[52,72420,211],{"class":102},[52,72422,72423,72425,72427,72429,72431,72433,72435,72437,72439,72441,72443,72445,72447,72449,72451,72453,72455],{"class":54,"line":128},[52,72424,72147],{"class":62},[52,72426,80],{"class":79},[52,72428,6126],{"class":1475},[52,72430,72154],{"class":87},[52,72432,1482],{"class":121},[52,72434,29288],{"class":62},[52,72436,1488],{"class":121},[52,72438,72163],{"class":87},[52,72440,1482],{"class":121},[52,72442,46391],{"class":62},[52,72444,1488],{"class":121},[52,72446,2578],{"class":87},[52,72448,1482],{"class":121},[52,72450,6188],{"class":62},[52,72452,1488],{"class":121},[52,72454,10956],{"class":2533},[52,72456,91],{"class":87},[52,72458,72459],{"class":54,"line":148},[52,72460,70],{"emptyLinePlaceholder":69},[52,72462,72463,72465,72467,72469,72471,72473,72475,72477,72479,72481,72483,72485,72487,72489,72491,72493,72495,72497],{"class":54,"line":164},[52,72464,24616],{"class":58},[52,72466,14635],{"class":241},[52,72468,193],{"class":102},[52,72470,72196],{"class":189},[52,72472,199],{"class":102},[52,72474,84],{"class":83},[52,72476,372],{"class":87},[52,72478,115],{"class":83},[52,72480,199],{"class":102},[52,72482,14659],{"class":202},[52,72484,80],{"class":79},[52,72486,115],{"class":83},[52,72488,14666],{"class":87},[52,72490,115],{"class":83},[52,72492,1521],{"class":102},[52,72494,3567],{"class":58},[52,72496,14676],{"class":14675},[52,72498,1330],{"class":102},[52,72500,72501,72503,72505,72507,72509,72511],{"class":54,"line":170},[52,72502,24643],{"class":14675},[52,72504,186],{"class":102},[52,72506,23849],{"class":189},[52,72508,193],{"class":102},[52,72510,54],{"class":189},[52,72512,211],{"class":102},[52,72514,72515],{"class":54,"line":175},[52,72516,70],{"emptyLinePlaceholder":69},[52,72518,72519],{"class":54,"line":214},[52,72520,70],{"emptyLinePlaceholder":69},[52,72522,72523,72525,72527,72529,72531,72533,72535,72537,72539,72541],{"class":54,"line":233},[52,72524,72251],{"class":189},[52,72526,193],{"class":102},[52,72528,115],{"class":83},[52,72530,46094],{"class":87},[52,72532,115],{"class":83},[52,72534,199],{"class":102},[52,72536,84],{"class":83},[52,72538,72266],{"class":87},[52,72540,115],{"class":83},[52,72542,211],{"class":102},[52,72544,72545,72547,72549,72551,72553,72555,72557,72559,72562,72564],{"class":54,"line":238},[52,72546,72251],{"class":189},[52,72548,193],{"class":102},[52,72550,115],{"class":83},[52,72552,46094],{"class":87},[52,72554,115],{"class":83},[52,72556,199],{"class":102},[52,72558,84],{"class":83},[52,72560,72561],{"class":87},"Loading user data",[52,72563,115],{"class":83},[52,72565,211],{"class":102},[52,72567,72568,72570,72572,72574,72576,72578,72580,72582,72585,72587],{"class":54,"line":272},[52,72569,72251],{"class":189},[52,72571,193],{"class":102},[52,72573,115],{"class":83},[52,72575,46088],{"class":87},[52,72577,115],{"class":83},[52,72579,199],{"class":102},[52,72581,84],{"class":83},[52,72583,72584],{"class":87},"Could not connect to server",[52,72586,115],{"class":83},[52,72588,211],{"class":102},[52,72590,72591],{"class":54,"line":1348},[52,72592,70],{"emptyLinePlaceholder":69},[52,72594,72595,72597,72599,72601,72603,72605],{"class":54,"line":1376},[52,72596,242],{"class":241},[52,72598,193],{"class":102},[52,72600,115],{"class":83},[52,72602,72304],{"class":87},[52,72604,115],{"class":83},[52,72606,211],{"class":102},[639,72608,24095],{"id":24094},[309,72610,72611,72617,72623,72628,72634],{},[24,72612,72613,72616],{},[49,72614,72615],{},"datetime.now()"," gets the current date and time",[24,72618,72619,72622],{},[49,72620,72621],{},"strftime(\"%Y-%m-%d %H:%M:%S\")"," turns that date and time into readable text",[24,72624,72625,72626],{},"The formatted log line is stored in ",[49,72627,54],{},[24,72629,72630,72633],{},[49,72631,72632],{},"open(filename, \"a\", encoding=\"utf-8\")"," opens the file in append mode",[24,72635,72636,72639],{},[49,72637,72638],{},"file.write(line)"," adds the message to the file",[14,72641,72642,72643,759,72645,186],{},"If you are new to writing files, see ",[372,72644,29634],{"href":29633},[372,72646,62303],{"href":17931},[37,72648,72650],{"id":72649},"why-logging-is-useful","Why logging is useful",[14,72652,72653],{},"Logging helps you keep a record of what your program did.",[14,72655,72656],{},"This is useful because it:",[309,72658,72659,72662,72665,72668],{},[24,72660,72661],{},"Helps you see what your program did",[24,72663,72664],{},"Makes debugging easier",[24,72666,72667],{},"Lets you keep a record of errors",[24,72669,72670,72671,72673],{},"Is more useful than ",[49,72672,9687],{}," when you want saved output",[14,72675,5794,72676,72678],{},[49,72677,9687],{}," only shows text in the terminal while the program is running. A log file stays on your computer, so you can check it later.",[37,72680,72682],{"id":72681},"core-parts-of-the-script","Core parts of the script",[14,72684,72685],{},"These are the most important parts of the example:",[309,72687,72688,72692,72698,72703],{},[24,72689,72690,72616],{},[49,72691,72615],{},[24,72693,72694,72697],{},[49,72695,72696],{},"strftime()"," formats the timestamp as readable text",[24,72699,72700,72633],{},[49,72701,72702],{},"open(filename, \"a\")",[24,72704,72705],{},"The log function keeps the code organized and reusable",[639,72707,72709],{"id":72708},"the-logging-function","The logging function",[42,72711,72713],{"className":44,"code":72712,"language":46,"meta":47,"style":47},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\\n\"\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line)\n",[49,72714,72715,72725,72729,72733,72761,72791,72827,72831,72869],{"__ignoreMap":47},[52,72716,72717,72719,72721,72723],{"class":54,"line":55},[52,72718,25329],{"class":58},[52,72720,29194],{"class":62},[52,72722,59],{"class":58},[52,72724,29199],{"class":62},[52,72726,72727],{"class":54,"line":66},[52,72728,70],{"emptyLinePlaceholder":69},[52,72730,72731],{"class":54,"line":73},[52,72732,70],{"emptyLinePlaceholder":69},[52,72734,72735,72737,72739,72741,72743,72745,72747,72749,72751,72753,72755,72757,72759],{"class":54,"line":94},[52,72736,7410],{"class":1475},[52,72738,72087],{"class":7065},[52,72740,193],{"class":102},[52,72742,46391],{"class":7018},[52,72744,199],{"class":102},[52,72746,6168],{"class":7018},[52,72748,199],{"class":102},[52,72750,72100],{"class":7018},[52,72752,80],{"class":79},[52,72754,115],{"class":83},[52,72756,46144],{"class":87},[52,72758,115],{"class":83},[52,72760,3114],{"class":102},[52,72762,72763,72765,72767,72769,72771,72773,72775,72777,72779,72781,72783,72785,72787,72789],{"class":54,"line":106},[52,72764,72115],{"class":62},[52,72766,80],{"class":79},[52,72768,29241],{"class":62},[52,72770,186],{"class":102},[52,72772,29246],{"class":189},[52,72774,18679],{"class":102},[52,72776,29251],{"class":189},[52,72778,193],{"class":102},[52,72780,115],{"class":83},[52,72782,29258],{"class":87},[52,72784,29261],{"class":121},[52,72786,72138],{"class":87},[52,72788,115],{"class":83},[52,72790,211],{"class":102},[52,72792,72793,72795,72797,72799,72801,72803,72805,72807,72809,72811,72813,72815,72817,72819,72821,72823,72825],{"class":54,"line":128},[52,72794,72147],{"class":62},[52,72796,80],{"class":79},[52,72798,6126],{"class":1475},[52,72800,72154],{"class":87},[52,72802,1482],{"class":121},[52,72804,29288],{"class":62},[52,72806,1488],{"class":121},[52,72808,72163],{"class":87},[52,72810,1482],{"class":121},[52,72812,46391],{"class":62},[52,72814,1488],{"class":121},[52,72816,2578],{"class":87},[52,72818,1482],{"class":121},[52,72820,6188],{"class":62},[52,72822,1488],{"class":121},[52,72824,10956],{"class":2533},[52,72826,91],{"class":87},[52,72828,72829],{"class":54,"line":148},[52,72830,70],{"emptyLinePlaceholder":69},[52,72832,72833,72835,72837,72839,72841,72843,72845,72847,72849,72851,72853,72855,72857,72859,72861,72863,72865,72867],{"class":54,"line":164},[52,72834,24616],{"class":58},[52,72836,14635],{"class":241},[52,72838,193],{"class":102},[52,72840,72196],{"class":189},[52,72842,199],{"class":102},[52,72844,84],{"class":83},[52,72846,372],{"class":87},[52,72848,115],{"class":83},[52,72850,199],{"class":102},[52,72852,14659],{"class":202},[52,72854,80],{"class":79},[52,72856,115],{"class":83},[52,72858,14666],{"class":87},[52,72860,115],{"class":83},[52,72862,1521],{"class":102},[52,72864,3567],{"class":58},[52,72866,14676],{"class":14675},[52,72868,1330],{"class":102},[52,72870,72871,72873,72875,72877,72879,72881],{"class":54,"line":170},[52,72872,24643],{"class":14675},[52,72874,186],{"class":102},[52,72876,23849],{"class":189},[52,72878,193],{"class":102},[52,72880,54],{"class":189},[52,72882,211],{"class":102},[14,72884,72885],{},"Key lines:",[309,72887,72888,72897,72902,72908],{},[24,72889,72890,72892,72893,857,72895],{},[49,72891,46391],{}," is a label such as ",[49,72894,46094],{},[49,72896,46088],{},[24,72898,72899,72901],{},[49,72900,6188],{}," is the text you want to save",[24,72903,72904,72907],{},[49,72905,72906],{},"filename=\"app.log\""," gives the function a default log file name",[24,72909,72910,72912],{},[49,72911,10956],{}," adds a newline so each log entry appears on its own line",[14,72914,14977,72915,14981,72917,186],{},[49,72916,14980],{},[372,72918,14985],{"href":14984},[37,72920,72922],{"id":72921},"step-by-step-build-order","Step-by-step build order",[14,72924,72925],{},"A good way to build this script is:",[21,72927,72928,72933,72936,72939,72942,72945],{},[24,72929,42554,72930],{},[49,72931,72932],{},"datetime",[24,72934,72935],{},"Create a function that accepts a level and message",[24,72937,72938],{},"Build the log line as a string",[24,72940,72941],{},"Open the log file in append mode",[24,72943,72944],{},"Write the line to the file",[24,72946,72947],{},"Call the function with different message types",[14,72949,72950],{},"Here is the same process as code:",[42,72952,72954],{"className":44,"code":72953,"language":46,"meta":47,"style":47},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\\n\"\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line)\n\n\nlog_message(\"INFO\", \"Program started\")\nlog_message(\"ERROR\", \"File not found\")\n",[49,72955,72956,72966,72970,72974,73002,73032,73068,73072,73110,73124,73128,73132,73154],{"__ignoreMap":47},[52,72957,72958,72960,72962,72964],{"class":54,"line":55},[52,72959,25329],{"class":58},[52,72961,29194],{"class":62},[52,72963,59],{"class":58},[52,72965,29199],{"class":62},[52,72967,72968],{"class":54,"line":66},[52,72969,70],{"emptyLinePlaceholder":69},[52,72971,72972],{"class":54,"line":73},[52,72973,70],{"emptyLinePlaceholder":69},[52,72975,72976,72978,72980,72982,72984,72986,72988,72990,72992,72994,72996,72998,73000],{"class":54,"line":94},[52,72977,7410],{"class":1475},[52,72979,72087],{"class":7065},[52,72981,193],{"class":102},[52,72983,46391],{"class":7018},[52,72985,199],{"class":102},[52,72987,6168],{"class":7018},[52,72989,199],{"class":102},[52,72991,72100],{"class":7018},[52,72993,80],{"class":79},[52,72995,115],{"class":83},[52,72997,46144],{"class":87},[52,72999,115],{"class":83},[52,73001,3114],{"class":102},[52,73003,73004,73006,73008,73010,73012,73014,73016,73018,73020,73022,73024,73026,73028,73030],{"class":54,"line":106},[52,73005,72115],{"class":62},[52,73007,80],{"class":79},[52,73009,29241],{"class":62},[52,73011,186],{"class":102},[52,73013,29246],{"class":189},[52,73015,18679],{"class":102},[52,73017,29251],{"class":189},[52,73019,193],{"class":102},[52,73021,115],{"class":83},[52,73023,29258],{"class":87},[52,73025,29261],{"class":121},[52,73027,72138],{"class":87},[52,73029,115],{"class":83},[52,73031,211],{"class":102},[52,73033,73034,73036,73038,73040,73042,73044,73046,73048,73050,73052,73054,73056,73058,73060,73062,73064,73066],{"class":54,"line":128},[52,73035,72147],{"class":62},[52,73037,80],{"class":79},[52,73039,6126],{"class":1475},[52,73041,72154],{"class":87},[52,73043,1482],{"class":121},[52,73045,29288],{"class":62},[52,73047,1488],{"class":121},[52,73049,72163],{"class":87},[52,73051,1482],{"class":121},[52,73053,46391],{"class":62},[52,73055,1488],{"class":121},[52,73057,2578],{"class":87},[52,73059,1482],{"class":121},[52,73061,6188],{"class":62},[52,73063,1488],{"class":121},[52,73065,10956],{"class":2533},[52,73067,91],{"class":87},[52,73069,73070],{"class":54,"line":148},[52,73071,70],{"emptyLinePlaceholder":69},[52,73073,73074,73076,73078,73080,73082,73084,73086,73088,73090,73092,73094,73096,73098,73100,73102,73104,73106,73108],{"class":54,"line":164},[52,73075,24616],{"class":58},[52,73077,14635],{"class":241},[52,73079,193],{"class":102},[52,73081,72196],{"class":189},[52,73083,199],{"class":102},[52,73085,84],{"class":83},[52,73087,372],{"class":87},[52,73089,115],{"class":83},[52,73091,199],{"class":102},[52,73093,14659],{"class":202},[52,73095,80],{"class":79},[52,73097,115],{"class":83},[52,73099,14666],{"class":87},[52,73101,115],{"class":83},[52,73103,1521],{"class":102},[52,73105,3567],{"class":58},[52,73107,14676],{"class":14675},[52,73109,1330],{"class":102},[52,73111,73112,73114,73116,73118,73120,73122],{"class":54,"line":170},[52,73113,24643],{"class":14675},[52,73115,186],{"class":102},[52,73117,23849],{"class":189},[52,73119,193],{"class":102},[52,73121,54],{"class":189},[52,73123,211],{"class":102},[52,73125,73126],{"class":54,"line":175},[52,73127,70],{"emptyLinePlaceholder":69},[52,73129,73130],{"class":54,"line":214},[52,73131,70],{"emptyLinePlaceholder":69},[52,73133,73134,73136,73138,73140,73142,73144,73146,73148,73150,73152],{"class":54,"line":233},[52,73135,72251],{"class":189},[52,73137,193],{"class":102},[52,73139,115],{"class":83},[52,73141,46094],{"class":87},[52,73143,115],{"class":83},[52,73145,199],{"class":102},[52,73147,84],{"class":83},[52,73149,72266],{"class":87},[52,73151,115],{"class":83},[52,73153,211],{"class":102},[52,73155,73156,73158,73160,73162,73164,73166,73168,73170,73173,73175],{"class":54,"line":238},[52,73157,72251],{"class":189},[52,73159,193],{"class":102},[52,73161,115],{"class":83},[52,73163,46088],{"class":87},[52,73165,115],{"class":83},[52,73167,199],{"class":102},[52,73169,84],{"class":83},[52,73171,73172],{"class":87},"File not found",[52,73174,115],{"class":83},[52,73176,211],{"class":102},[14,73178,73179],{},"This keeps the script simple and easy to reuse in other small programs.",[37,73181,73183],{"id":73182},"expected-output-in-the-log-file","Expected output in the log file",[14,73185,73186,73187,186],{},"Lines are saved in ",[49,73188,46144],{},[14,73190,73191],{},"Each line includes:",[309,73193,73194,73197,73200,73203],{},[24,73195,73196],{},"Date",[24,73198,73199],{},"Time",[24,73201,73202],{},"Level",[24,73204,73205],{},"Message",[14,73207,4259],{},[42,73209,73212],{"className":73210,"code":73211,"language":955,"meta":47},[953],"[2026-04-22 10:30:15] INFO: Program started\n[2026-04-22 10:30:16] ERROR: Something went wrong\n",[49,73213,73211],{"__ignoreMap":47},[14,73215,73216],{},"To run the script:",[42,73218,73219],{"className":390,"code":27398,"language":392,"meta":47,"style":47},[49,73220,73221],{"__ignoreMap":47},[52,73222,73223,73225],{"class":54,"line":55},[52,73224,46],{"class":399},[52,73226,27407],{"class":87},[14,73228,73229],{},"To check the log file on macOS or Linux:",[42,73231,73233],{"className":390,"code":73232,"language":392,"meta":47,"style":47},"cat app.log\n",[49,73234,73235],{"__ignoreMap":47},[52,73236,73237,73239],{"class":54,"line":55},[52,73238,17514],{"class":399},[52,73240,73241],{"class":87}," app.log\n",[14,73243,73244],{},"To check the log file on Windows Command Prompt:",[42,73246,73248],{"className":390,"code":73247,"language":392,"meta":47,"style":47},"type app.log\n",[49,73249,73250],{"__ignoreMap":47},[52,73251,73252,73254],{"class":54,"line":55},[52,73253,4198],{"class":241},[52,73255,73241],{"class":87},[14,73257,73258],{},"To see the full path of the log file:",[42,73260,73262],{"className":390,"code":73261,"language":392,"meta":47,"style":47},"python -c \"from pathlib import Path; print(Path('app.log').resolve())\"\n",[49,73263,73264],{"__ignoreMap":47},[52,73265,73266,73268,73270,73272,73275],{"class":54,"line":55},[52,73267,46],{"class":399},[52,73269,443],{"class":420},[52,73271,84],{"class":83},[52,73273,73274],{"class":87},"from pathlib import Path; print(Path('app.log').resolve())",[52,73276,91],{"class":83},[37,73278,73280],{"id":73279},"easy-ways-to-improve-this-example","Easy ways to improve this example",[14,73282,73283],{},"Once the basic version works, you can extend it.",[14,73285,73286],{},"Easy improvements include:",[309,73288,73289,73297,73300,73303],{},[24,73290,73291,73292,857,73294],{},"Add more log levels such as ",[49,73293,46091],{},[49,73295,73296],{},"DEBUG",[24,73298,73299],{},"Print the message to the screen and save it to the file",[24,73301,73302],{},"Use a different file name for each day",[24,73304,73305,73306,73308],{},"Wrap risky code in ",[49,73307,3603],{}," and log errors",[14,73310,73311],{},"Here is a slightly improved version that prints and logs at the same time:",[42,73313,73315],{"className":44,"code":73314,"language":46,"meta":47,"style":47},"from datetime import datetime\n\n\ndef log_message(level, message, filename=\"app.log\"):\n    timestamp = datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n    line = f\"[{timestamp}] {level}: {message}\"\n\n    print(line)\n\n    with open(filename, \"a\", encoding=\"utf-8\") as file:\n        file.write(line + \"\\n\")\n\n\nlog_message(\"INFO\", \"User opened the program\")\nlog_message(\"WARNING\", \"Input was empty\")\nlog_message(\"ERROR\", \"Could not save file\")\n",[49,73316,73317,73327,73331,73335,73363,73393,73427,73431,73441,73445,73483,73506,73510,73514,73537,73560],{"__ignoreMap":47},[52,73318,73319,73321,73323,73325],{"class":54,"line":55},[52,73320,25329],{"class":58},[52,73322,29194],{"class":62},[52,73324,59],{"class":58},[52,73326,29199],{"class":62},[52,73328,73329],{"class":54,"line":66},[52,73330,70],{"emptyLinePlaceholder":69},[52,73332,73333],{"class":54,"line":73},[52,73334,70],{"emptyLinePlaceholder":69},[52,73336,73337,73339,73341,73343,73345,73347,73349,73351,73353,73355,73357,73359,73361],{"class":54,"line":94},[52,73338,7410],{"class":1475},[52,73340,72087],{"class":7065},[52,73342,193],{"class":102},[52,73344,46391],{"class":7018},[52,73346,199],{"class":102},[52,73348,6168],{"class":7018},[52,73350,199],{"class":102},[52,73352,72100],{"class":7018},[52,73354,80],{"class":79},[52,73356,115],{"class":83},[52,73358,46144],{"class":87},[52,73360,115],{"class":83},[52,73362,3114],{"class":102},[52,73364,73365,73367,73369,73371,73373,73375,73377,73379,73381,73383,73385,73387,73389,73391],{"class":54,"line":106},[52,73366,72115],{"class":62},[52,73368,80],{"class":79},[52,73370,29241],{"class":62},[52,73372,186],{"class":102},[52,73374,29246],{"class":189},[52,73376,18679],{"class":102},[52,73378,29251],{"class":189},[52,73380,193],{"class":102},[52,73382,115],{"class":83},[52,73384,29258],{"class":87},[52,73386,29261],{"class":121},[52,73388,72138],{"class":87},[52,73390,115],{"class":83},[52,73392,211],{"class":102},[52,73394,73395,73397,73399,73401,73403,73405,73407,73409,73411,73413,73415,73417,73419,73421,73423,73425],{"class":54,"line":128},[52,73396,72147],{"class":62},[52,73398,80],{"class":79},[52,73400,6126],{"class":1475},[52,73402,72154],{"class":87},[52,73404,1482],{"class":121},[52,73406,29288],{"class":62},[52,73408,1488],{"class":121},[52,73410,72163],{"class":87},[52,73412,1482],{"class":121},[52,73414,46391],{"class":62},[52,73416,1488],{"class":121},[52,73418,2578],{"class":87},[52,73420,1482],{"class":121},[52,73422,6188],{"class":62},[52,73424,1488],{"class":121},[52,73426,91],{"class":87},[52,73428,73429],{"class":54,"line":148},[52,73430,70],{"emptyLinePlaceholder":69},[52,73432,73433,73435,73437,73439],{"class":54,"line":164},[52,73434,1599],{"class":241},[52,73436,193],{"class":102},[52,73438,54],{"class":189},[52,73440,211],{"class":102},[52,73442,73443],{"class":54,"line":170},[52,73444,70],{"emptyLinePlaceholder":69},[52,73446,73447,73449,73451,73453,73455,73457,73459,73461,73463,73465,73467,73469,73471,73473,73475,73477,73479,73481],{"class":54,"line":175},[52,73448,24616],{"class":58},[52,73450,14635],{"class":241},[52,73452,193],{"class":102},[52,73454,72196],{"class":189},[52,73456,199],{"class":102},[52,73458,84],{"class":83},[52,73460,372],{"class":87},[52,73462,115],{"class":83},[52,73464,199],{"class":102},[52,73466,14659],{"class":202},[52,73468,80],{"class":79},[52,73470,115],{"class":83},[52,73472,14666],{"class":87},[52,73474,115],{"class":83},[52,73476,1521],{"class":102},[52,73478,3567],{"class":58},[52,73480,14676],{"class":14675},[52,73482,1330],{"class":102},[52,73484,73485,73487,73489,73491,73493,73496,73498,73500,73502,73504],{"class":54,"line":214},[52,73486,24643],{"class":14675},[52,73488,186],{"class":102},[52,73490,23849],{"class":189},[52,73492,193],{"class":102},[52,73494,73495],{"class":189},"line ",[52,73497,9881],{"class":79},[52,73499,84],{"class":83},[52,73501,10956],{"class":2533},[52,73503,115],{"class":83},[52,73505,211],{"class":102},[52,73507,73508],{"class":54,"line":233},[52,73509,70],{"emptyLinePlaceholder":69},[52,73511,73512],{"class":54,"line":238},[52,73513,70],{"emptyLinePlaceholder":69},[52,73515,73516,73518,73520,73522,73524,73526,73528,73530,73533,73535],{"class":54,"line":272},[52,73517,72251],{"class":189},[52,73519,193],{"class":102},[52,73521,115],{"class":83},[52,73523,46094],{"class":87},[52,73525,115],{"class":83},[52,73527,199],{"class":102},[52,73529,84],{"class":83},[52,73531,73532],{"class":87},"User opened the program",[52,73534,115],{"class":83},[52,73536,211],{"class":102},[52,73538,73539,73541,73543,73545,73547,73549,73551,73553,73556,73558],{"class":54,"line":1348},[52,73540,72251],{"class":189},[52,73542,193],{"class":102},[52,73544,115],{"class":83},[52,73546,46091],{"class":87},[52,73548,115],{"class":83},[52,73550,199],{"class":102},[52,73552,84],{"class":83},[52,73554,73555],{"class":87},"Input was empty",[52,73557,115],{"class":83},[52,73559,211],{"class":102},[52,73561,73562,73564,73566,73568,73570,73572,73574,73576,73579,73581],{"class":54,"line":1376},[52,73563,72251],{"class":189},[52,73565,193],{"class":102},[52,73567,115],{"class":83},[52,73569,46088],{"class":87},[52,73571,115],{"class":83},[52,73573,199],{"class":102},[52,73575,84],{"class":83},[52,73577,73578],{"class":87},"Could not save file",[52,73580,115],{"class":83},[52,73582,211],{"class":102},[14,73584,73585,73586,4441,73588,186],{},"If you want to handle errors while logging program problems, see ",[372,73587,10028],{"href":10027},[372,73589,73591],{"href":73590},"\u002Fexamples\u002Fpython-error-handling-example-script\u002F","Python error handling example script",[37,73593,12322],{"id":12321},[14,73595,73596],{},"These are common reasons beginners think the script is not working:",[309,73598,73599,73602,73605,73608,73611],{},[24,73600,73601],{},"Opening the file in write mode instead of append mode, which erases old logs",[24,73603,73604],{},"Forgetting the newline character so all logs appear on one line",[24,73606,73607],{},"Using a file path that does not exist",[24,73609,73610],{},"Misspelling the filename and checking the wrong file",[24,73612,73613],{},"Expecting logging to work without calling the function",[639,73615,73617],{"id":73616},"example-of-a-common-mistake","Example of a common mistake",[14,73619,73620],{},"This version uses write mode:",[42,73622,73624],{"className":44,"code":73623,"language":46,"meta":47,"style":47},"with open(\"app.log\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"This will replace the old file contents.\\n\")\n",[49,73625,73626,73668],{"__ignoreMap":47},[52,73627,73628,73630,73632,73634,73636,73638,73640,73642,73644,73646,73648,73650,73652,73654,73656,73658,73660,73662,73664,73666],{"class":54,"line":55},[52,73629,14632],{"class":58},[52,73631,14635],{"class":241},[52,73633,193],{"class":102},[52,73635,115],{"class":83},[52,73637,46144],{"class":87},[52,73639,115],{"class":83},[52,73641,199],{"class":102},[52,73643,84],{"class":83},[52,73645,16760],{"class":87},[52,73647,115],{"class":83},[52,73649,199],{"class":102},[52,73651,14659],{"class":202},[52,73653,80],{"class":79},[52,73655,115],{"class":83},[52,73657,14666],{"class":87},[52,73659,115],{"class":83},[52,73661,1521],{"class":102},[52,73663,3567],{"class":58},[52,73665,14676],{"class":14675},[52,73667,1330],{"class":102},[52,73669,73670,73672,73674,73676,73678,73680,73683,73685,73687],{"class":54,"line":66},[52,73671,23844],{"class":14675},[52,73673,186],{"class":102},[52,73675,23849],{"class":189},[52,73677,193],{"class":102},[52,73679,115],{"class":83},[52,73681,73682],{"class":87},"This will replace the old file contents.",[52,73684,10956],{"class":2533},[52,73686,115],{"class":83},[52,73688,211],{"class":102},[14,73690,73691,73692,73694],{},"Mode ",[49,73693,17356],{}," creates a new file or overwrites an existing file.",[14,73696,73697,73698,22719],{},"If you want to keep old log entries, use ",[49,73699,62225],{},[42,73701,73703],{"className":44,"code":73702,"language":46,"meta":47,"style":47},"with open(\"app.log\", \"a\", encoding=\"utf-8\") as file:\n    file.write(\"This will be added to the end of the file.\\n\")\n",[49,73704,73705,73747],{"__ignoreMap":47},[52,73706,73707,73709,73711,73713,73715,73717,73719,73721,73723,73725,73727,73729,73731,73733,73735,73737,73739,73741,73743,73745],{"class":54,"line":55},[52,73708,14632],{"class":58},[52,73710,14635],{"class":241},[52,73712,193],{"class":102},[52,73714,115],{"class":83},[52,73716,46144],{"class":87},[52,73718,115],{"class":83},[52,73720,199],{"class":102},[52,73722,84],{"class":83},[52,73724,372],{"class":87},[52,73726,115],{"class":83},[52,73728,199],{"class":102},[52,73730,14659],{"class":202},[52,73732,80],{"class":79},[52,73734,115],{"class":83},[52,73736,14666],{"class":87},[52,73738,115],{"class":83},[52,73740,1521],{"class":102},[52,73742,3567],{"class":58},[52,73744,14676],{"class":14675},[52,73746,1330],{"class":102},[52,73748,73749,73751,73753,73755,73757,73759,73762,73764,73766],{"class":54,"line":66},[52,73750,23844],{"class":14675},[52,73752,186],{"class":102},[52,73754,23849],{"class":189},[52,73756,193],{"class":102},[52,73758,115],{"class":83},[52,73760,73761],{"class":87},"This will be added to the end of the file.",[52,73763,10956],{"class":2533},[52,73765,115],{"class":83},[52,73767,211],{"class":102},[14,73769,73770,73771,186],{},"For a broader introduction to files, see ",[372,73772,16513],{"href":14988},[37,73774,1942],{"id":1941},[639,73776,73778],{"id":73777},"what-is-the-difference-between-logging-and-print","What is the difference between logging and print()?",[14,73780,73781,73783],{},[49,73782,9687],{}," shows output on the screen. Logging saves messages so you can review them later.",[639,73785,73787],{"id":73786},"why-use-append-mode-when-opening-the-file","Why use append mode when opening the file?",[14,73789,73790],{},"Append mode adds new messages to the end of the file instead of deleting old ones.",[639,73792,73794],{"id":73793},"can-i-log-errors-only","Can I log errors only?",[14,73796,73797],{},"Yes. You can call the function only when an error happens, but logging normal steps can also help with debugging.",[639,73799,73801],{"id":73800},"do-i-need-the-logging-module-for-this-example","Do I need the logging module for this example?",[14,73803,73804,73805,73807],{},"No. This page shows a simple beginner-friendly version. The standard ",[49,73806,57480],{}," module is more powerful but more advanced.",[37,73809,2005],{"id":2004},[309,73811,73812,73816,73820,73824,73828,73832],{},[24,73813,73814],{},[372,73815,29926],{"href":29633},[24,73817,73818],{},[372,73819,17932],{"href":17931},[24,73821,73822],{},[372,73823,16513],{"href":14988},[24,73825,73826],{},[372,73827,14985],{"href":14984},[24,73829,73830],{},[372,73831,26911],{"href":10027},[24,73833,73834],{},[372,73835,73591],{"href":73590},[14,73837,73838],{},"Try extending this script in one of your own projects by logging user actions, important steps, or errors.",[2034,73840,73841],{},"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 .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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 .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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}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}",{"title":47,"searchDepth":66,"depth":66,"links":73843},[73844,73847,73848,73851,73852,73853,73854,73857,73863],{"id":322,"depth":66,"text":323,"children":73845},[73846],{"id":24094,"depth":73,"text":24095},{"id":72649,"depth":66,"text":72650},{"id":72681,"depth":66,"text":72682,"children":73849},[73850],{"id":72708,"depth":73,"text":72709},{"id":72921,"depth":66,"text":72922},{"id":73182,"depth":66,"text":73183},{"id":73279,"depth":66,"text":73280},{"id":12321,"depth":66,"text":12322,"children":73855},[73856],{"id":73616,"depth":73,"text":73617},{"id":1941,"depth":66,"text":1942,"children":73858},[73859,73860,73861,73862],{"id":73777,"depth":73,"text":73778},{"id":73786,"depth":73,"text":73787},{"id":73793,"depth":73,"text":73794},{"id":73800,"depth":73,"text":73801},{"id":2004,"depth":66,"text":2005},"Master python simple logging system example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-simple-logging-system-example",{"title":72041,"description":73864},"examples\u002Fpython-simple-logging-system-example","GQK6gpLUJ34Shi4bqeCtjF60_npfuyey6nECuLnScjs",{"id":73871,"title":73872,"body":73873,"description":75427,"extension":2075,"meta":75428,"navigation":69,"path":75429,"seo":75430,"stem":75431,"__hash__":75432},"content\u002Fexamples\u002Fpython-simple-web-scraper-for-titles-example.md","Python Simple Web Scraper for Titles Example",{"type":7,"value":73874,"toc":75400},[73875,73878,73881,73888,73890,73899,74013,74015,74035,74037,74046,74048,74051,74065,74068,74070,74072,74090,74093,74103,74107,74112,74128,74133,74148,74153,74159,74170,74174,74177,74311,74313,74345,74351,74359,74363,74366,74380,74382,74596,74598,74616,74625,74629,74632,74930,74933,75015,75018,75022,75025,75038,75045,75070,75073,75076,75090,75097,75099,75102,75127,75129,75156,75159,75192,75196,75201,75216,75220,75223,75246,75253,75256,75274,75277,75294,75298,75300,75316,75322,75324,75332,75335,75339,75345,75349,75352,75356,75363,75367,75370,75372,75397],[10,73876,73872],{"id":73877},"python-simple-web-scraper-for-titles-example",[14,73879,73880],{},"Build a small Python script that downloads a web page and extracts page titles. This example is meant to be practical, short, and easy to run step by step.",[14,73882,73883,73884,186],{},"If you are new to packages, see ",[372,73885,73887],{"href":73886},"\u002Fhow-to\u002Fhow-to-install-a-python-package-with-pip\u002F","how to install a Python package with pip",[37,73889,40],{"id":39},[14,73891,73892,73893,759,73895,73898],{},"This is the fastest working example. It assumes ",[49,73894,303],{},[49,73896,73897],{},"beautifulsoup4"," are installed.",[42,73900,73902],{"className":44,"code":73901,"language":46,"meta":47,"style":47},"import requests\nfrom bs4 import BeautifulSoup\n\nurl = \"https:\u002F\u002Fexample.com\"\nresponse = requests.get(url)\nsoup = BeautifulSoup(response.text, \"html.parser\")\n\nprint(soup.title.string.strip())\n",[49,73903,73904,73910,73922,73926,73939,73957,73986,73990],{"__ignoreMap":47},[52,73905,73906,73908],{"class":54,"line":55},[52,73907,59],{"class":58},[52,73909,63],{"class":62},[52,73911,73912,73914,73917,73919],{"class":54,"line":66},[52,73913,25329],{"class":58},[52,73915,73916],{"class":62}," bs4 ",[52,73918,59],{"class":58},[52,73920,73921],{"class":62}," BeautifulSoup\n",[52,73923,73924],{"class":54,"line":73},[52,73925,70],{"emptyLinePlaceholder":69},[52,73927,73928,73930,73932,73934,73937],{"class":54,"line":94},[52,73929,76],{"class":62},[52,73931,80],{"class":79},[52,73933,84],{"class":83},[52,73935,73936],{"class":87},"https:\u002F\u002Fexample.com",[52,73938,91],{"class":83},[52,73940,73941,73943,73945,73947,73949,73951,73953,73955],{"class":54,"line":106},[52,73942,178],{"class":62},[52,73944,80],{"class":79},[52,73946,183],{"class":62},[52,73948,186],{"class":102},[52,73950,190],{"class":189},[52,73952,193],{"class":102},[52,73954,196],{"class":189},[52,73956,211],{"class":102},[52,73958,73959,73962,73964,73967,73969,73971,73973,73975,73977,73979,73982,73984],{"class":54,"line":128},[52,73960,73961],{"class":62},"soup ",[52,73963,80],{"class":79},[52,73965,73966],{"class":189}," BeautifulSoup",[52,73968,193],{"class":102},[52,73970,1756],{"class":189},[52,73972,186],{"class":102},[52,73974,955],{"class":1320},[52,73976,199],{"class":102},[52,73978,84],{"class":83},[52,73980,73981],{"class":87},"html.parser",[52,73983,115],{"class":83},[52,73985,211],{"class":102},[52,73987,73988],{"class":54,"line":148},[52,73989,70],{"emptyLinePlaceholder":69},[52,73991,73992,73994,73996,73999,74001,74003,74005,74007,74009,74011],{"class":54,"line":164},[52,73993,242],{"class":241},[52,73995,193],{"class":102},[52,73997,73998],{"class":189},"soup",[52,74000,186],{"class":102},[52,74002,18682],{"class":1320},[52,74004,186],{"class":102},[52,74006,41855],{"class":1320},[52,74008,186],{"class":102},[52,74010,18630],{"class":189},[52,74012,1816],{"class":102},[14,74014,27156],{},[309,74016,74017,74022,74025,74032],{},[24,74018,74019,74020],{},"Downloads the page at ",[49,74021,73936],{},[24,74023,74024],{},"Parses the HTML",[24,74026,74027,74028,74031],{},"Finds the ",[49,74029,74030],{},"\u003Ctitle>"," tag",[24,74033,74034],{},"Prints the title text",[14,74036,9266],{},[42,74038,74040],{"className":44,"code":74039,"language":46,"meta":47,"style":47},"Example Domain\n",[49,74041,74042],{"__ignoreMap":47},[52,74043,74044],{"class":54,"line":55},[52,74045,74039],{"class":62},[37,74047,323],{"id":322},[14,74049,74050],{},"This script does four simple things:",[309,74052,74053,74056,74059,74063],{},[24,74054,74055],{},"Downloads HTML from a web page",[24,74057,74058],{},"Parses the HTML with BeautifulSoup",[24,74060,74027,74061,74031],{},[49,74062,74030],{},[24,74064,74034],{},[14,74066,74067],{},"This is a good beginner scraping example because it focuses on one small task.",[37,74069,347],{"id":346},[14,74071,23910],{},[309,74073,74074,74076,74079,74083,74087],{},[24,74075,355],{},[24,74077,74078],{},"Basic understanding of running a Python script",[24,74080,358,74081,3910],{},[49,74082,303],{},[24,74084,358,74085,3910],{},[49,74086,73897],{},[24,74088,74089],{},"An internet connection for live pages",[14,74091,74092],{},"You can check your Python version with:",[42,74094,74095],{"className":390,"code":23947,"language":392,"meta":47,"style":47},[49,74096,74097],{"__ignoreMap":47},[52,74098,74099,74101],{"class":54,"line":55},[52,74100,46],{"class":399},[52,74102,2324],{"class":420},[37,74104,74106],{"id":74105},"install-the-required-packages","Install the required packages",[14,74108,74109,74110,118],{},"Install the required packages with ",[49,74111,400],{},[42,74113,74115],{"className":390,"code":74114,"language":392,"meta":47,"style":47},"pip install requests beautifulsoup4\n",[49,74116,74117],{"__ignoreMap":47},[52,74118,74119,74121,74123,74125],{"class":54,"line":55},[52,74120,400],{"class":399},[52,74122,403],{"class":87},[52,74124,183],{"class":87},[52,74126,74127],{"class":87}," beautifulsoup4\n",[14,74129,7526,74130,74132],{},[49,74131,400],{}," gives you errors, the problem is usually:",[309,74134,74135,74139,74142],{},[24,74136,74137,1671],{},[49,74138,400],{},[24,74140,74141],{},"You are using the wrong Python environment",[24,74143,74144,74145,74147],{},"Python and ",[49,74146,400],{}," point to different installations",[14,74149,74150,74151,186],{},"If needed, read ",[372,74152,73887],{"href":73886},[14,74154,74155,74156,74158],{},"You can also check ",[49,74157,400],{}," with:",[42,74160,74162],{"className":390,"code":74161,"language":392,"meta":47,"style":47},"pip --version\n",[49,74163,74164],{"__ignoreMap":47},[52,74165,74166,74168],{"class":54,"line":55},[52,74167,400],{"class":399},[52,74169,2324],{"class":420},[37,74171,74173],{"id":74172},"minimal-example-get-one-page-title","Minimal example: get one page title",[14,74175,74176],{},"Here is a simple version with the main steps shown clearly.",[42,74178,74180],{"className":44,"code":74179,"language":46,"meta":47,"style":47},"import requests\nfrom bs4 import BeautifulSoup\n\nurl = \"https:\u002F\u002Fexample.com\"\n\nresponse = requests.get(url)\nhtml = response.text\n\nsoup = BeautifulSoup(html, \"html.parser\")\n\ntitle_text = soup.title.string\nprint(title_text)\n",[49,74181,74182,74188,74198,74202,74214,74218,74236,74250,74254,74277,74281,74300],{"__ignoreMap":47},[52,74183,74184,74186],{"class":54,"line":55},[52,74185,59],{"class":58},[52,74187,63],{"class":62},[52,74189,74190,74192,74194,74196],{"class":54,"line":66},[52,74191,25329],{"class":58},[52,74193,73916],{"class":62},[52,74195,59],{"class":58},[52,74197,73921],{"class":62},[52,74199,74200],{"class":54,"line":73},[52,74201,70],{"emptyLinePlaceholder":69},[52,74203,74204,74206,74208,74210,74212],{"class":54,"line":94},[52,74205,76],{"class":62},[52,74207,80],{"class":79},[52,74209,84],{"class":83},[52,74211,73936],{"class":87},[52,74213,91],{"class":83},[52,74215,74216],{"class":54,"line":106},[52,74217,70],{"emptyLinePlaceholder":69},[52,74219,74220,74222,74224,74226,74228,74230,74232,74234],{"class":54,"line":128},[52,74221,178],{"class":62},[52,74223,80],{"class":79},[52,74225,183],{"class":62},[52,74227,186],{"class":102},[52,74229,190],{"class":189},[52,74231,193],{"class":102},[52,74233,196],{"class":189},[52,74235,211],{"class":102},[52,74237,74238,74241,74243,74245,74247],{"class":54,"line":148},[52,74239,74240],{"class":62},"html ",[52,74242,80],{"class":79},[52,74244,222],{"class":62},[52,74246,186],{"class":102},[52,74248,74249],{"class":1320},"text\n",[52,74251,74252],{"class":54,"line":164},[52,74253,70],{"emptyLinePlaceholder":69},[52,74255,74256,74258,74260,74262,74264,74267,74269,74271,74273,74275],{"class":54,"line":170},[52,74257,73961],{"class":62},[52,74259,80],{"class":79},[52,74261,73966],{"class":189},[52,74263,193],{"class":102},[52,74265,74266],{"class":189},"html",[52,74268,199],{"class":102},[52,74270,84],{"class":83},[52,74272,73981],{"class":87},[52,74274,115],{"class":83},[52,74276,211],{"class":102},[52,74278,74279],{"class":54,"line":175},[52,74280,70],{"emptyLinePlaceholder":69},[52,74282,74283,74286,74288,74291,74293,74295,74297],{"class":54,"line":214},[52,74284,74285],{"class":62},"title_text ",[52,74287,80],{"class":79},[52,74289,74290],{"class":62}," soup",[52,74292,186],{"class":102},[52,74294,18682],{"class":1320},[52,74296,186],{"class":102},[52,74298,74299],{"class":1320},"string\n",[52,74301,74302,74304,74306,74309],{"class":54,"line":233},[52,74303,242],{"class":241},[52,74305,193],{"class":102},[52,74307,74308],{"class":189},"title_text",[52,74310,211],{"class":102},[639,74312,4101],{"id":4100},[309,74314,74315,74320,74325,74331,74339],{},[24,74316,74317,74319],{},[49,74318,4120],{}," fetches the page",[24,74321,74322,74324],{},[49,74323,4247],{}," gives you the HTML as a string",[24,74326,74327,74330],{},[49,74328,74329],{},"BeautifulSoup(html, \"html.parser\")"," parses the HTML",[24,74332,74333,74336,74337,74031],{},[49,74334,74335],{},"soup.title"," finds the ",[49,74338,74030],{},[24,74340,74341,74344],{},[49,74342,74343],{},"soup.title.string"," gets the text inside the tag",[14,74346,74347,74348,74350],{},"For ",[49,74349,73936],{},", the output is:",[42,74352,74353],{"className":44,"code":74039,"language":46,"meta":47,"style":47},[49,74354,74355],{"__ignoreMap":47},[52,74356,74357],{"class":54,"line":55},[52,74358,74039],{"class":62},[37,74360,74362],{"id":74361},"safer-version-with-basic-error-handling","Safer version with basic error handling",[14,74364,74365],{},"The first example is useful, but it can fail if:",[309,74367,74368,74371,74374,74377],{},[24,74369,74370],{},"The page cannot be downloaded",[24,74372,74373],{},"The website returns an error",[24,74375,74376],{},"The page has no title tag",[24,74378,74379],{},"The title exists but has no text",[14,74381,4451],{},[42,74383,74385],{"className":44,"code":74384,"language":46,"meta":47,"style":47},"import requests\nfrom bs4 import BeautifulSoup\n\nurl = \"https:\u002F\u002Fexample.com\"\n\ntry:\n    response = requests.get(url, timeout=10)\n    response.raise_for_status()\nexcept requests.RequestException:\n    print(\"Could not fetch the page.\")\nelse:\n    soup = BeautifulSoup(response.text, \"html.parser\")\n\n    if soup.title and soup.title.string:\n        print(soup.title.string.strip())\n    else:\n        print(\"No title tag was found.\")\n",[49,74386,74387,74393,74403,74407,74419,74423,74429,74455,74465,74477,74492,74498,74525,74529,74553,74575,74581],{"__ignoreMap":47},[52,74388,74389,74391],{"class":54,"line":55},[52,74390,59],{"class":58},[52,74392,63],{"class":62},[52,74394,74395,74397,74399,74401],{"class":54,"line":66},[52,74396,25329],{"class":58},[52,74398,73916],{"class":62},[52,74400,59],{"class":58},[52,74402,73921],{"class":62},[52,74404,74405],{"class":54,"line":73},[52,74406,70],{"emptyLinePlaceholder":69},[52,74408,74409,74411,74413,74415,74417],{"class":54,"line":94},[52,74410,76],{"class":62},[52,74412,80],{"class":79},[52,74414,84],{"class":83},[52,74416,73936],{"class":87},[52,74418,91],{"class":83},[52,74420,74421],{"class":54,"line":106},[52,74422,70],{"emptyLinePlaceholder":69},[52,74424,74425,74427],{"class":54,"line":128},[52,74426,3336],{"class":58},[52,74428,1330],{"class":102},[52,74430,74431,74433,74435,74437,74439,74441,74443,74445,74447,74449,74451,74453],{"class":54,"line":148},[52,74432,3343],{"class":62},[52,74434,80],{"class":79},[52,74436,183],{"class":62},[52,74438,186],{"class":102},[52,74440,190],{"class":189},[52,74442,193],{"class":102},[52,74444,196],{"class":189},[52,74446,199],{"class":102},[52,74448,3368],{"class":202},[52,74450,80],{"class":79},[52,74452,3373],{"class":121},[52,74454,211],{"class":102},[52,74456,74457,74459,74461,74463],{"class":54,"line":164},[52,74458,3380],{"class":62},[52,74460,186],{"class":102},[52,74462,3385],{"class":189},[52,74464,230],{"class":102},[52,74466,74467,74469,74471,74473,74475],{"class":54,"line":170},[52,74468,3552],{"class":58},[52,74470,183],{"class":62},[52,74472,186],{"class":102},[52,74474,3564],{"class":1320},[52,74476,1330],{"class":102},[52,74478,74479,74481,74483,74485,74488,74490],{"class":54,"line":175},[52,74480,1599],{"class":241},[52,74482,193],{"class":102},[52,74484,115],{"class":83},[52,74486,74487],{"class":87},"Could not fetch the page.",[52,74489,115],{"class":83},[52,74491,211],{"class":102},[52,74493,74494,74496],{"class":54,"line":214},[52,74495,1591],{"class":58},[52,74497,1330],{"class":102},[52,74499,74500,74503,74505,74507,74509,74511,74513,74515,74517,74519,74521,74523],{"class":54,"line":233},[52,74501,74502],{"class":62},"    soup ",[52,74504,80],{"class":79},[52,74506,73966],{"class":189},[52,74508,193],{"class":102},[52,74510,1756],{"class":189},[52,74512,186],{"class":102},[52,74514,955],{"class":1320},[52,74516,199],{"class":102},[52,74518,84],{"class":83},[52,74520,73981],{"class":87},[52,74522,115],{"class":83},[52,74524,211],{"class":102},[52,74526,74527],{"class":54,"line":238},[52,74528,70],{"emptyLinePlaceholder":69},[52,74530,74531,74533,74535,74537,74539,74541,74543,74545,74547,74549,74551],{"class":54,"line":272},[52,74532,1438],{"class":58},[52,74534,74290],{"class":62},[52,74536,186],{"class":102},[52,74538,18682],{"class":1320},[52,74540,1453],{"class":79},[52,74542,74290],{"class":62},[52,74544,186],{"class":102},[52,74546,18682],{"class":1320},[52,74548,186],{"class":102},[52,74550,41855],{"class":1320},[52,74552,1330],{"class":102},[52,74554,74555,74557,74559,74561,74563,74565,74567,74569,74571,74573],{"class":54,"line":1348},[52,74556,1470],{"class":241},[52,74558,193],{"class":102},[52,74560,73998],{"class":189},[52,74562,186],{"class":102},[52,74564,18682],{"class":1320},[52,74566,186],{"class":102},[52,74568,41855],{"class":1320},[52,74570,186],{"class":102},[52,74572,18630],{"class":189},[52,74574,1816],{"class":102},[52,74576,74577,74579],{"class":54,"line":1376},[52,74578,1567],{"class":58},[52,74580,1330],{"class":102},[52,74582,74583,74585,74587,74589,74592,74594],{"class":54,"line":1381},[52,74584,1470],{"class":241},[52,74586,193],{"class":102},[52,74588,115],{"class":83},[52,74590,74591],{"class":87},"No title tag was found.",[52,74593,115],{"class":83},[52,74595,211],{"class":102},[639,74597,4755],{"id":4754},[309,74599,74600,74605,74610],{},[24,74601,74602,74604],{},[49,74603,4762],{}," prevents the request from hanging too long",[24,74606,74607,74609],{},[49,74608,25191],{}," catches HTTP errors like 404 and 500",[24,74611,74612,74615],{},[49,74613,74614],{},"if soup.title and soup.title.string"," avoids crashes when the title is missing",[14,74617,74618,74619,74621,74622,74624],{},"If you are not familiar with HTTP requests yet, see ",[372,74620,375],{"href":374},". The same ",[49,74623,303],{}," library is used here.",[37,74626,74628],{"id":74627},"example-scrape-titles-from-multiple-pages","Example: scrape titles from multiple pages",[14,74630,74631],{},"You can also put several URLs in a list and loop through them.",[42,74633,74635],{"className":44,"code":74634,"language":46,"meta":47,"style":47},"import requests\nfrom bs4 import BeautifulSoup\n\nurls = [\n    \"https:\u002F\u002Fexample.com\",\n    \"https:\u002F\u002Fwww.python.org\",\n    \"https:\u002F\u002Fwww.wikipedia.org\",\n]\n\nfor url in urls:\n    try:\n        response = requests.get(url, timeout=10)\n        response.raise_for_status()\n        soup = BeautifulSoup(response.text, \"html.parser\")\n\n        if soup.title and soup.title.string:\n            title = soup.title.string.strip()\n        else:\n            title = \"No title found\"\n\n        print(f\"{url} -> {title}\")\n\n    except requests.RequestException:\n        print(f\"{url} -> Could not fetch page\")\n",[49,74636,74637,74643,74653,74657,74666,74676,74687,74698,74702,74706,74720,74726,74753,74764,74791,74795,74819,74842,74848,74861,74865,74893,74897,74909],{"__ignoreMap":47},[52,74638,74639,74641],{"class":54,"line":55},[52,74640,59],{"class":58},[52,74642,63],{"class":62},[52,74644,74645,74647,74649,74651],{"class":54,"line":66},[52,74646,25329],{"class":58},[52,74648,73916],{"class":62},[52,74650,59],{"class":58},[52,74652,73921],{"class":62},[52,74654,74655],{"class":54,"line":73},[52,74656,70],{"emptyLinePlaceholder":69},[52,74658,74659,74662,74664],{"class":54,"line":94},[52,74660,74661],{"class":62},"urls ",[52,74663,80],{"class":79},[52,74665,16627],{"class":102},[52,74667,74668,74670,74672,74674],{"class":54,"line":106},[52,74669,109],{"class":83},[52,74671,73936],{"class":87},[52,74673,115],{"class":83},[52,74675,125],{"class":102},[52,74677,74678,74680,74683,74685],{"class":54,"line":128},[52,74679,109],{"class":83},[52,74681,74682],{"class":87},"https:\u002F\u002Fwww.python.org",[52,74684,115],{"class":83},[52,74686,125],{"class":102},[52,74688,74689,74691,74694,74696],{"class":54,"line":148},[52,74690,109],{"class":83},[52,74692,74693],{"class":87},"https:\u002F\u002Fwww.wikipedia.org",[52,74695,115],{"class":83},[52,74697,125],{"class":102},[52,74699,74700],{"class":54,"line":164},[52,74701,1147],{"class":102},[52,74703,74704],{"class":54,"line":170},[52,74705,70],{"emptyLinePlaceholder":69},[52,74707,74708,74710,74713,74715,74718],{"class":54,"line":175},[52,74709,12265],{"class":58},[52,74711,74712],{"class":62}," url ",[52,74714,11213],{"class":58},[52,74716,74717],{"class":62}," urls",[52,74719,1330],{"class":102},[52,74721,74722,74724],{"class":54,"line":214},[52,74723,20482],{"class":58},[52,74725,1330],{"class":102},[52,74727,74728,74731,74733,74735,74737,74739,74741,74743,74745,74747,74749,74751],{"class":54,"line":233},[52,74729,74730],{"class":62},"        response ",[52,74732,80],{"class":79},[52,74734,183],{"class":62},[52,74736,186],{"class":102},[52,74738,190],{"class":189},[52,74740,193],{"class":102},[52,74742,196],{"class":189},[52,74744,199],{"class":102},[52,74746,3368],{"class":202},[52,74748,80],{"class":79},[52,74750,3373],{"class":121},[52,74752,211],{"class":102},[52,74754,74755,74758,74760,74762],{"class":54,"line":238},[52,74756,74757],{"class":62},"        response",[52,74759,186],{"class":102},[52,74761,3385],{"class":189},[52,74763,230],{"class":102},[52,74765,74766,74769,74771,74773,74775,74777,74779,74781,74783,74785,74787,74789],{"class":54,"line":272},[52,74767,74768],{"class":62},"        soup ",[52,74770,80],{"class":79},[52,74772,73966],{"class":189},[52,74774,193],{"class":102},[52,74776,1756],{"class":189},[52,74778,186],{"class":102},[52,74780,955],{"class":1320},[52,74782,199],{"class":102},[52,74784,84],{"class":83},[52,74786,73981],{"class":87},[52,74788,115],{"class":83},[52,74790,211],{"class":102},[52,74792,74793],{"class":54,"line":1348},[52,74794,70],{"emptyLinePlaceholder":69},[52,74796,74797,74799,74801,74803,74805,74807,74809,74811,74813,74815,74817],{"class":54,"line":1376},[52,74798,11168],{"class":58},[52,74800,74290],{"class":62},[52,74802,186],{"class":102},[52,74804,18682],{"class":1320},[52,74806,1453],{"class":79},[52,74808,74290],{"class":62},[52,74810,186],{"class":102},[52,74812,18682],{"class":1320},[52,74814,186],{"class":102},[52,74816,41855],{"class":1320},[52,74818,1330],{"class":102},[52,74820,74821,74824,74826,74828,74830,74832,74834,74836,74838,74840],{"class":54,"line":1381},[52,74822,74823],{"class":62},"            title ",[52,74825,80],{"class":79},[52,74827,74290],{"class":62},[52,74829,186],{"class":102},[52,74831,18682],{"class":1320},[52,74833,186],{"class":102},[52,74835,41855],{"class":1320},[52,74837,186],{"class":102},[52,74839,18630],{"class":189},[52,74841,230],{"class":102},[52,74843,74844,74846],{"class":54,"line":1406},[52,74845,11196],{"class":58},[52,74847,1330],{"class":102},[52,74849,74850,74852,74854,74856,74859],{"class":54,"line":1430},[52,74851,74823],{"class":62},[52,74853,80],{"class":79},[52,74855,84],{"class":83},[52,74857,74858],{"class":87},"No title found",[52,74860,91],{"class":83},[52,74862,74863],{"class":54,"line":1435},[52,74864,70],{"emptyLinePlaceholder":69},[52,74866,74867,74869,74871,74873,74875,74877,74879,74881,74883,74885,74887,74889,74891],{"class":54,"line":1467},[52,74868,1470],{"class":241},[52,74870,193],{"class":102},[52,74872,1476],{"class":1475},[52,74874,115],{"class":87},[52,74876,1482],{"class":121},[52,74878,196],{"class":189},[52,74880,1488],{"class":121},[52,74882,31507],{"class":87},[52,74884,1482],{"class":121},[52,74886,18682],{"class":189},[52,74888,1488],{"class":121},[52,74890,115],{"class":87},[52,74892,211],{"class":102},[52,74894,74895],{"class":54,"line":1495},[52,74896,70],{"emptyLinePlaceholder":69},[52,74898,74899,74901,74903,74905,74907],{"class":54,"line":1531},[52,74900,20512],{"class":58},[52,74902,183],{"class":62},[52,74904,186],{"class":102},[52,74906,3564],{"class":1320},[52,74908,1330],{"class":102},[52,74910,74911,74913,74915,74917,74919,74921,74923,74925,74928],{"class":54,"line":1564},[52,74912,1470],{"class":241},[52,74914,193],{"class":102},[52,74916,1476],{"class":1475},[52,74918,115],{"class":87},[52,74920,1482],{"class":121},[52,74922,196],{"class":189},[52,74924,1488],{"class":121},[52,74926,74927],{"class":87}," -> Could not fetch page\"",[52,74929,211],{"class":102},[14,74931,74932],{},"Example output might look like this:",[42,74934,74936],{"className":44,"code":74935,"language":46,"meta":47,"style":47},"https:\u002F\u002Fexample.com -> Example Domain\nhttps:\u002F\u002Fwww.python.org -> Welcome to Python.org\nhttps:\u002F\u002Fwww.wikipedia.org -> Wikipedia\n",[49,74937,74938,74961,74991],{"__ignoreMap":47},[52,74939,74940,74943,74945,74948,74950,74952,74955,74958],{"class":54,"line":55},[52,74941,74942],{"class":62},"https",[52,74944,118],{"class":102},[52,74946,74947],{"class":79},"\u002F\u002F",[52,74949,43506],{"class":62},[52,74951,186],{"class":102},[52,74953,74954],{"class":1320},"com",[52,74956,74957],{"class":54291}," ->",[52,74959,74960],{"class":62}," Example Domain\n",[52,74962,74963,74965,74967,74969,74972,74974,74976,74978,74981,74983,74986,74988],{"class":54,"line":66},[52,74964,74942],{"class":62},[52,74966,118],{"class":102},[52,74968,74947],{"class":79},[52,74970,74971],{"class":62},"www",[52,74973,186],{"class":102},[52,74975,46],{"class":1320},[52,74977,186],{"class":102},[52,74979,74980],{"class":1320},"org",[52,74982,74957],{"class":54291},[52,74984,74985],{"class":62}," Welcome to Python",[52,74987,186],{"class":102},[52,74989,74990],{"class":1320},"org\n",[52,74992,74993,74995,74997,74999,75001,75003,75006,75008,75010,75012],{"class":54,"line":73},[52,74994,74942],{"class":62},[52,74996,118],{"class":102},[52,74998,74947],{"class":79},[52,75000,74971],{"class":62},[52,75002,186],{"class":102},[52,75004,75005],{"class":1320},"wikipedia",[52,75007,186],{"class":102},[52,75009,74980],{"class":1320},[52,75011,74957],{"class":54291},[52,75013,75014],{"class":62}," Wikipedia\n",[14,75016,75017],{},"This example keeps the loop small and readable. That is a good way to start.",[37,75019,75021],{"id":75020},"how-this-example-works","How this example works",[14,75023,75024],{},"There are two main tools in this script:",[309,75026,75027,75032],{},[24,75028,75029,75031],{},[49,75030,303],{}," handles the HTTP request",[24,75033,75034,75037],{},[49,75035,75036],{},"BeautifulSoup"," reads and searches the HTML",[14,75039,75040,75041,75044],{},"A page title is usually inside the ",[49,75042,75043],{},"\u003Chead>"," section, like this:",[42,75046,75049],{"className":75047,"code":75048,"language":74266,"meta":47,"style":47},"language-html shiki shiki-themes material-theme-lighter github-light github-dark","\u003Ctitle>Example Domain\u003C\u002Ftitle>\n",[49,75050,75051],{"__ignoreMap":47},[52,75052,75053,75055,75058,75060,75063,75066,75068],{"class":54,"line":55},[52,75054,15294],{"class":102},[52,75056,18682],{"class":75057},"sQzsp",[52,75059,13046],{"class":102},[52,75061,75062],{"class":62},"Example Domain",[52,75064,75065],{"class":102},"\u003C\u002F",[52,75067,18682],{"class":75057},[52,75069,15306],{"class":102},[14,75071,75072],{},"BeautifulSoup makes it easy to find that tag.",[14,75074,75075],{},"Keep in mind:",[309,75077,75078,75081,75084,75087],{},[24,75079,75080],{},"Not every page has a title",[24,75082,75083],{},"Some pages have messy HTML",[24,75085,75086],{},"Some pages return different content than you expect",[24,75088,75089],{},"Some sites load content later with JavaScript",[14,75091,75092,75093,186],{},"If you want a broader BeautifulSoup example after this one, see ",[372,75094,75096],{"href":75095},"\u002Fexamples\u002Fpython-web-scraping-example-beautifulsoup\u002F","Python web scraping example with BeautifulSoup",[37,75098,4780],{"id":4779},[14,75100,75101],{},"Here are the most common causes when this example does not work:",[309,75103,75104,75108,75112,75118,75121,75124],{},[24,75105,75106,1671],{},[49,75107,303],{},[24,75109,75110,1671],{},[49,75111,73897],{},[24,75113,75114,75115],{},"The URL is wrong or missing ",[49,75116,75117],{},"https:\u002F\u002F",[24,75119,75120],{},"The page request failed",[24,75122,75123],{},"The HTML has no title tag",[24,75125,75126],{},"The site uses JavaScript for content you expected to scrape",[14,75128,1686],{},[309,75130,75131,75144],{},[24,75132,75133,4812,75138,857,75140,75143],{},[372,75134,75135],{"href":1730},[49,75136,75137],{},"ModuleNotFoundError: No module named ...",[49,75139,303],{},[49,75141,75142],{},"bs4"," is missing",[24,75145,75146,4812,75152,27498,75154],{},[372,75147,75149],{"href":75148},"\u002Ferrors\u002Fattributeerror-nonetype-object-has-no-attribute-fix\u002F",[49,75150,75151],{},"AttributeError: 'NoneType' object has no attribute ...",[49,75153,74335],{},[49,75155,18849],{},[14,75157,75158],{},"Useful commands for debugging:",[42,75160,75162],{"className":390,"code":75161,"language":392,"meta":47,"style":47},"pip install requests beautifulsoup4\npython --version\npip --version\npython script.py\n",[49,75163,75164,75174,75180,75186],{"__ignoreMap":47},[52,75165,75166,75168,75170,75172],{"class":54,"line":55},[52,75167,400],{"class":399},[52,75169,403],{"class":87},[52,75171,183],{"class":87},[52,75173,74127],{"class":87},[52,75175,75176,75178],{"class":54,"line":66},[52,75177,46],{"class":399},[52,75179,2324],{"class":420},[52,75181,75182,75184],{"class":54,"line":73},[52,75183,400],{"class":399},[52,75185,2324],{"class":420},[52,75187,75188,75190],{"class":54,"line":94},[52,75189,46],{"class":399},[52,75191,27407],{"class":87},[639,75193,75195],{"id":75194},"a-few-examples-of-common-mistakes","A few examples of common mistakes",[14,75197,7526,75198,75200],{},[49,75199,75142],{}," is not installed:",[42,75202,75204],{"className":44,"code":75203,"language":46,"meta":47,"style":47},"from bs4 import BeautifulSoup\n",[49,75205,75206],{"__ignoreMap":47},[52,75207,75208,75210,75212,75214],{"class":54,"line":55},[52,75209,25329],{"class":58},[52,75211,73916],{"class":62},[52,75213,59],{"class":58},[52,75215,73921],{"class":62},[14,75217,65788,75218,186],{},[49,75219,1693],{},[14,75221,75222],{},"If the page has no title and you do this:",[42,75224,75226],{"className":44,"code":75225,"language":46,"meta":47,"style":47},"print(soup.title.string)\n",[49,75227,75228],{"__ignoreMap":47},[52,75229,75230,75232,75234,75236,75238,75240,75242,75244],{"class":54,"line":55},[52,75231,242],{"class":241},[52,75233,193],{"class":102},[52,75235,73998],{"class":189},[52,75237,186],{"class":102},[52,75239,18682],{"class":1320},[52,75241,186],{"class":102},[52,75243,41855],{"class":1320},[52,75245,211],{"class":102},[14,75247,75248,75249,27498,75251,186],{},"You may get an error because ",[49,75250,74335],{},[49,75252,18849],{},[14,75254,75255],{},"If the URL is incomplete:",[42,75257,75259],{"className":44,"code":75258,"language":46,"meta":47,"style":47},"url = \"example.com\"\n",[49,75260,75261],{"__ignoreMap":47},[52,75262,75263,75265,75267,75269,75272],{"class":54,"line":55},[52,75264,76],{"class":62},[52,75266,80],{"class":79},[52,75268,84],{"class":83},[52,75270,75271],{"class":87},"example.com",[52,75273,91],{"class":83},[14,75275,75276],{},"The request may fail. Use the full URL instead:",[42,75278,75280],{"className":44,"code":75279,"language":46,"meta":47,"style":47},"url = \"https:\u002F\u002Fexample.com\"\n",[49,75281,75282],{"__ignoreMap":47},[52,75283,75284,75286,75288,75290,75292],{"class":54,"line":55},[52,75285,76],{"class":62},[52,75287,80],{"class":79},[52,75289,84],{"class":83},[52,75291,73936],{"class":87},[52,75293,91],{"class":83},[37,75295,75297],{"id":75296},"important-beginner-notes","Important beginner notes",[14,75299,61104],{},[309,75301,75302,75305,75308,75311],{},[24,75303,75304],{},"This example only reads page HTML and extracts the title",[24,75306,75307],{},"It is not a full web scraping guide",[24,75309,75310],{},"Some sites do not allow scraping",[24,75312,75313,75314],{},"Start with simple public pages like ",[49,75315,75271],{},[14,75317,75318,75319,75321],{},"Also remember that ",[49,75320,303],{}," only downloads the initial HTML. If a site loads text later with JavaScript, your script may not see that content.",[37,75323,1942],{"id":1941},[639,75325,3677,75327,75329,75330,1995],{"id":75326},"why-does-souptitle-return-none",[49,75328,74335],{}," return ",[49,75331,18849],{},[14,75333,75334],{},"The page may not have a title tag, or the HTML was not fetched correctly.",[639,75336,75338],{"id":75337},"why-is-my-scraper-not-finding-text-i-can-see-in-the-browser","Why is my scraper not finding text I can see in the browser?",[14,75340,75341,75342,75344],{},"Some websites load content with JavaScript. ",[49,75343,303],{}," only gets the initial HTML.",[639,75346,75348],{"id":75347},"do-i-need-beautifulsoup-just-to-get-the-title","Do I need BeautifulSoup just to get the title?",[14,75350,75351],{},"Not always, but it makes HTML parsing much easier and clearer for beginners.",[639,75353,75355],{"id":75354},"what-packages-do-i-need-for-this-example","What packages do I need for this example?",[14,75357,75358,75359,759,75361,186],{},"You usually need ",[49,75360,303],{},[49,75362,73897],{},[639,75364,75366],{"id":75365},"can-i-scrape-many-pages-with-a-loop","Can I scrape many pages with a loop?",[14,75368,75369],{},"Yes. Start with a short list of URLs and print each page title.",[37,75371,2005],{"id":2004},[309,75373,75374,75379,75383,75388,75393],{},[24,75375,75376],{},[372,75377,75378],{"href":73886},"How to install a Python package with pip",[24,75380,75381],{},[372,75382,2012],{"href":374},[24,75384,75385],{},[372,75386,75387],{"href":1730},"ModuleNotFoundError: No module named ... fix",[24,75389,75390],{},[372,75391,75392],{"href":75148},"AttributeError: 'NoneType' object has no attribute ... fix",[24,75394,75395],{},[372,75396,75096],{"href":75095},[2034,75398,75399],{},"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 .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 .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 .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 .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 .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 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 .sQzsp, html code.shiki .sQzsp{--shiki-light:#E53935;--shiki-default:#22863A;--shiki-dark:#85E89D}",{"title":47,"searchDepth":66,"depth":66,"links":75401},[75402,75403,75404,75405,75406,75409,75412,75413,75414,75417,75418,75426],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":346,"depth":66,"text":347},{"id":74105,"depth":66,"text":74106},{"id":74172,"depth":66,"text":74173,"children":75407},[75408],{"id":4100,"depth":73,"text":4101},{"id":74361,"depth":66,"text":74362,"children":75410},[75411],{"id":4754,"depth":73,"text":4755},{"id":74627,"depth":66,"text":74628},{"id":75020,"depth":66,"text":75021},{"id":4779,"depth":66,"text":4780,"children":75415},[75416],{"id":75194,"depth":73,"text":75195},{"id":75296,"depth":66,"text":75297},{"id":1941,"depth":66,"text":1942,"children":75419},[75420,75422,75423,75424,75425],{"id":75326,"depth":73,"text":75421},"Why does soup.title return None?",{"id":75337,"depth":73,"text":75338},{"id":75347,"depth":73,"text":75348},{"id":75354,"depth":73,"text":75355},{"id":75365,"depth":73,"text":75366},{"id":2004,"depth":66,"text":2005},"Master python simple web scraper for titles example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-simple-web-scraper-for-titles-example",{"title":73872,"description":75427},"examples\u002Fpython-simple-web-scraper-for-titles-example","g05xlCKpRH0PAY2eIVG3ALxUlzEj8hRfXCi_xgBUQrQ",{"id":75434,"title":75435,"body":75436,"description":77820,"extension":2075,"meta":77821,"navigation":69,"path":77822,"seo":77823,"stem":77824,"__hash__":77825},"content\u002Fexamples\u002Fpython-sorting-algorithm-example-bubble-sort.md","Python Sorting Algorithm Example (Bubble Sort)",{"type":7,"value":75437,"toc":77789},[75438,75441,75444,75447,75460,75725,75729,75758,75760,75763,75777,75784,75788,75791,75794,75822,75825,75854,75858,75861,75898,75904,75908,75911,75937,75942,75946,75949,75977,75981,75984,76187,76189,76193,76210,76213,76217,76236,76239,76242,76331,76334,76396,76400,76419,76422,76426,76447,76450,76454,76487,76490,76502,76508,76512,76588,76591,76594,76597,76646,76649,76653,76656,76670,76673,76676,76689,76692,76711,76714,76727,76731,76734,77027,77031,77095,77099,77105,77338,77342,77370,77373,77377,77380,77383,77394,77397,77420,77422,77425,77429,77435,77437,77571,77578,77584,77588,77591,77594,77642,77645,77648,77666,77669,77673,77676,77707,77713,77715,77719,77722,77728,77734,77738,77741,77747,77753,77755,77786],[10,75439,75435],{"id":75440},"python-sorting-algorithm-example-bubble-sort",[14,75442,75443],{},"Bubble sort is a simple sorting algorithm that is often used for learning.",[14,75445,75446],{},"In this example, you will see how bubble sort works in Python with a small step-by-step program. The goal is to understand the algorithm, not to use it as the main way to sort data in real projects.",[75448,75449,75450],"blockquote",{},[14,75451,75452,75453,75459],{},"Use this as a learning example. In normal Python code, ",[372,75454,75456],{"href":75455},"\u002Freference\u002Fpython-sorted-function-explained\u002F",[49,75457,75458],{},"sorted()"," is usually the better choice.",[42,75461,75463],{"className":44,"code":75462,"language":46,"meta":47,"style":47},"def bubble_sort(numbers):\n    items = numbers.copy()\n    n = len(items)\n\n    for i in range(n):\n        swapped = False\n        for j in range(0, n - i - 1):\n            if items[j] > items[j + 1]:\n                items[j], items[j + 1] = items[j + 1], items[j]\n                swapped = True\n        if not swapped:\n            break\n\n    return items\n\nvalues = [5, 2, 9, 1, 3]\nprint(bubble_sort(values))\n",[49,75464,75465,75478,75493,75508,75512,75528,75538,75568,75597,75642,75651,75662,75666,75670,75677,75681,75710],{"__ignoreMap":47},[52,75466,75467,75469,75472,75474,75476],{"class":54,"line":55},[52,75468,7410],{"class":1475},[52,75470,75471],{"class":7065}," bubble_sort",[52,75473,193],{"class":102},[52,75475,5324],{"class":7018},[52,75477,3114],{"class":102},[52,75479,75480,75483,75485,75487,75489,75491],{"class":54,"line":66},[52,75481,75482],{"class":62},"    items ",[52,75484,80],{"class":79},[52,75486,32302],{"class":62},[52,75488,186],{"class":102},[52,75490,28621],{"class":189},[52,75492,230],{"class":102},[52,75494,75495,75498,75500,75502,75504,75506],{"class":54,"line":73},[52,75496,75497],{"class":62},"    n ",[52,75499,80],{"class":79},[52,75501,9150],{"class":241},[52,75503,193],{"class":102},[52,75505,11220],{"class":189},[52,75507,211],{"class":102},[52,75509,75510],{"class":54,"line":94},[52,75511,70],{"emptyLinePlaceholder":69},[52,75513,75514,75516,75518,75520,75522,75524,75526],{"class":54,"line":106},[52,75515,11773],{"class":58},[52,75517,36859],{"class":62},[52,75519,11213],{"class":58},[52,75521,36864],{"class":241},[52,75523,193],{"class":102},[52,75525,44412],{"class":189},[52,75527,3114],{"class":102},[52,75529,75530,75533,75535],{"class":54,"line":128},[52,75531,75532],{"class":62},"        swapped ",[52,75534,80],{"class":79},[52,75536,75537],{"class":160}," False\n",[52,75539,75540,75543,75546,75548,75550,75552,75554,75556,75558,75560,75562,75564,75566],{"class":54,"line":148},[52,75541,75542],{"class":58},"        for",[52,75544,75545],{"class":62}," j ",[52,75547,11213],{"class":58},[52,75549,36864],{"class":241},[52,75551,193],{"class":102},[52,75553,13294],{"class":121},[52,75555,199],{"class":102},[52,75557,32139],{"class":189},[52,75559,5889],{"class":79},[52,75561,36859],{"class":189},[52,75563,5889],{"class":79},[52,75565,1518],{"class":121},[52,75567,3114],{"class":102},[52,75569,75570,75572,75574,75576,75579,75581,75584,75586,75588,75591,75593,75595],{"class":54,"line":164},[52,75571,49378],{"class":58},[52,75573,34128],{"class":62},[52,75575,250],{"class":102},[52,75577,75578],{"class":62},"j",[52,75580,11122],{"class":102},[52,75582,75583],{"class":79}," >",[52,75585,34128],{"class":62},[52,75587,250],{"class":102},[52,75589,75590],{"class":62},"j ",[52,75592,9881],{"class":79},[52,75594,1518],{"class":121},[52,75596,35426],{"class":102},[52,75598,75599,75602,75604,75606,75608,75610,75612,75614,75616,75618,75620,75622,75624,75626,75628,75630,75632,75634,75636,75638,75640],{"class":54,"line":170},[52,75600,75601],{"class":62},"                items",[52,75603,250],{"class":102},[52,75605,75578],{"class":62},[52,75607,15661],{"class":102},[52,75609,34128],{"class":62},[52,75611,250],{"class":102},[52,75613,75590],{"class":62},[52,75615,9881],{"class":79},[52,75617,1518],{"class":121},[52,75619,11122],{"class":102},[52,75621,7038],{"class":79},[52,75623,34128],{"class":62},[52,75625,250],{"class":102},[52,75627,75590],{"class":62},[52,75629,9881],{"class":79},[52,75631,1518],{"class":121},[52,75633,15661],{"class":102},[52,75635,34128],{"class":62},[52,75637,250],{"class":102},[52,75639,75578],{"class":62},[52,75641,1147],{"class":102},[52,75643,75644,75647,75649],{"class":54,"line":175},[52,75645,75646],{"class":62},"                swapped ",[52,75648,80],{"class":79},[52,75650,161],{"class":160},[52,75652,75653,75655,75657,75660],{"class":54,"line":214},[52,75654,11168],{"class":58},[52,75656,1447],{"class":79},[52,75658,75659],{"class":62}," swapped",[52,75661,1330],{"class":102},[52,75663,75664],{"class":54,"line":233},[52,75665,48114],{"class":58},[52,75667,75668],{"class":54,"line":238},[52,75669,70],{"emptyLinePlaceholder":69},[52,75671,75672,75674],{"class":54,"line":272},[52,75673,20958],{"class":58},[52,75675,75676],{"class":62}," items\n",[52,75678,75679],{"class":54,"line":1348},[52,75680,70],{"emptyLinePlaceholder":69},[52,75682,75683,75686,75688,75690,75692,75694,75696,75698,75700,75702,75704,75706,75708],{"class":54,"line":1376},[52,75684,75685],{"class":62},"values ",[52,75687,80],{"class":79},[52,75689,18162],{"class":102},[52,75691,9479],{"class":121},[52,75693,199],{"class":102},[52,75695,9168],{"class":121},[52,75697,199],{"class":102},[52,75699,66568],{"class":121},[52,75701,199],{"class":102},[52,75703,1518],{"class":121},[52,75705,199],{"class":102},[52,75707,5261],{"class":121},[52,75709,1147],{"class":102},[52,75711,75712,75714,75716,75719,75721,75723],{"class":54,"line":1381},[52,75713,242],{"class":241},[52,75715,193],{"class":102},[52,75717,75718],{"class":189},"bubble_sort",[52,75720,193],{"class":102},[52,75722,22462],{"class":189},[52,75724,8886],{"class":102},[14,75726,75727],{},[5503,75728,5267],{},[42,75730,75732],{"className":44,"code":75731,"language":46,"meta":47,"style":47},"[1, 2, 3, 5, 9]\n",[49,75733,75734],{"__ignoreMap":47},[52,75735,75736,75738,75740,75742,75744,75746,75748,75750,75752,75754,75756],{"class":54,"line":55},[52,75737,250],{"class":102},[52,75739,9200],{"class":121},[52,75741,199],{"class":102},[52,75743,9168],{"class":121},[52,75745,199],{"class":102},[52,75747,5261],{"class":121},[52,75749,199],{"class":102},[52,75751,7646],{"class":121},[52,75753,199],{"class":102},[52,75755,66568],{"class":121},[52,75757,1147],{"class":102},[37,75759,7216],{"id":7215},[14,75761,75762],{},"This example helps you understand several important Python ideas:",[309,75764,75765,75768,75771,75774],{},[24,75766,75767],{},"Bubble sort compares nearby items and swaps them when they are in the wrong order.",[24,75769,75770],{},"After each full pass, the largest remaining value moves toward the end.",[24,75772,75773],{},"It gives you practice with loops, comparisons, and swapping values.",[24,75775,75776],{},"It is useful for learning, but not for most real programs.",[14,75778,7526,75779,75781,75782,186],{},[49,75780,12265],{}," loops still feel new, see ",[372,75783,34468],{"href":32073},[37,75785,75787],{"id":75786},"how-bubble-sort-works-step-by-step","How bubble sort works step by step",[14,75789,75790],{},"Bubble sort works by repeatedly moving through the list and comparing neighboring values.",[14,75792,75793],{},"Here is the basic idea:",[309,75795,75796,75799,75813,75816,75819],{},[24,75797,75798],{},"Start with a list of numbers.",[24,75800,75801,75802,75804,75805,75807,75808,75804,75810,75812],{},"Compare item ",[49,75803,13294],{}," with item ",[49,75806,9200],{},", then item ",[49,75809,9200],{},[49,75811,9825],{},", and so on.",[24,75814,75815],{},"If the left item is bigger than the right item, swap them.",[24,75817,75818],{},"Repeat this process until the list is sorted.",[24,75820,75821],{},"After each outer loop pass, one more largest value ends up in the correct position.",[14,75823,75824],{},"Let’s use this list:",[42,75826,75828],{"className":44,"code":75827,"language":46,"meta":47,"style":47},"[5, 2, 9, 1, 3]\n",[49,75829,75830],{"__ignoreMap":47},[52,75831,75832,75834,75836,75838,75840,75842,75844,75846,75848,75850,75852],{"class":54,"line":55},[52,75833,250],{"class":102},[52,75835,9479],{"class":121},[52,75837,199],{"class":102},[52,75839,9168],{"class":121},[52,75841,199],{"class":102},[52,75843,66568],{"class":121},[52,75845,199],{"class":102},[52,75847,1518],{"class":121},[52,75849,199],{"class":102},[52,75851,5261],{"class":121},[52,75853,1147],{"class":102},[639,75855,75857],{"id":75856},"first-pass","First pass",[14,75859,75860],{},"Compare nearby values from left to right:",[309,75862,75863,75873,75880,75889],{},[24,75864,75865,759,75867,75869,75870],{},[49,75866,9479],{},[49,75868,9825],{}," → swap → ",[49,75871,75872],{},"[2, 5, 9, 1, 3]",[24,75874,75875,759,75877,75879],{},[49,75876,9479],{},[49,75878,37264],{}," → no swap",[24,75881,75882,759,75884,75869,75886],{},[49,75883,37264],{},[49,75885,9200],{},[49,75887,75888],{},"[2, 5, 1, 9, 3]",[24,75890,75891,759,75893,75869,75895],{},[49,75892,37264],{},[49,75894,7508],{},[49,75896,75897],{},"[2, 5, 1, 3, 9]",[14,75899,75900,75901,75903],{},"Now the largest value, ",[49,75902,37264],{},", has moved to the end.",[639,75905,75907],{"id":75906},"second-pass","Second pass",[14,75909,75910],{},"Start again, but stop one item earlier because the last item is already in the right place:",[309,75912,75913,75919,75928],{},[24,75914,75915,759,75917,75879],{},[49,75916,9825],{},[49,75918,9479],{},[24,75920,75921,759,75923,75869,75925],{},[49,75922,9479],{},[49,75924,9200],{},[49,75926,75927],{},"[2, 1, 5, 3, 9]",[24,75929,75930,759,75932,75869,75934],{},[49,75931,9479],{},[49,75933,7508],{},[49,75935,75936],{},"[2, 1, 3, 5, 9]",[14,75938,53980,75939,75941],{},[49,75940,9479],{}," is also in the correct position.",[639,75943,75945],{"id":75944},"keep-repeating","Keep repeating",[14,75947,75948],{},"More passes continue until the list becomes:",[42,75950,75951],{"className":44,"code":75731,"language":46,"meta":47,"style":47},[49,75952,75953],{"__ignoreMap":47},[52,75954,75955,75957,75959,75961,75963,75965,75967,75969,75971,75973,75975],{"class":54,"line":55},[52,75956,250],{"class":102},[52,75958,9200],{"class":121},[52,75960,199],{"class":102},[52,75962,9168],{"class":121},[52,75964,199],{"class":102},[52,75966,5261],{"class":121},[52,75968,199],{"class":102},[52,75970,7646],{"class":121},[52,75972,199],{"class":102},[52,75974,66568],{"class":121},[52,75976,1147],{"class":102},[37,75978,75980],{"id":75979},"build-the-python-bubble-sort-function","Build the Python bubble sort function",[14,75982,75983],{},"Here is the full function again:",[42,75985,75987],{"className":44,"code":75986,"language":46,"meta":47,"style":47},"def bubble_sort(numbers):\n    items = numbers.copy()\n    n = len(items)\n\n    for i in range(n):\n        swapped = False\n        for j in range(0, n - i - 1):\n            if items[j] > items[j + 1]:\n                items[j], items[j + 1] = items[j + 1], items[j]\n                swapped = True\n        if not swapped:\n            break\n\n    return items\n",[49,75988,75989,76001,76015,76029,76033,76049,76057,76085,76111,76155,76163,76173,76177,76181],{"__ignoreMap":47},[52,75990,75991,75993,75995,75997,75999],{"class":54,"line":55},[52,75992,7410],{"class":1475},[52,75994,75471],{"class":7065},[52,75996,193],{"class":102},[52,75998,5324],{"class":7018},[52,76000,3114],{"class":102},[52,76002,76003,76005,76007,76009,76011,76013],{"class":54,"line":66},[52,76004,75482],{"class":62},[52,76006,80],{"class":79},[52,76008,32302],{"class":62},[52,76010,186],{"class":102},[52,76012,28621],{"class":189},[52,76014,230],{"class":102},[52,76016,76017,76019,76021,76023,76025,76027],{"class":54,"line":73},[52,76018,75497],{"class":62},[52,76020,80],{"class":79},[52,76022,9150],{"class":241},[52,76024,193],{"class":102},[52,76026,11220],{"class":189},[52,76028,211],{"class":102},[52,76030,76031],{"class":54,"line":94},[52,76032,70],{"emptyLinePlaceholder":69},[52,76034,76035,76037,76039,76041,76043,76045,76047],{"class":54,"line":106},[52,76036,11773],{"class":58},[52,76038,36859],{"class":62},[52,76040,11213],{"class":58},[52,76042,36864],{"class":241},[52,76044,193],{"class":102},[52,76046,44412],{"class":189},[52,76048,3114],{"class":102},[52,76050,76051,76053,76055],{"class":54,"line":128},[52,76052,75532],{"class":62},[52,76054,80],{"class":79},[52,76056,75537],{"class":160},[52,76058,76059,76061,76063,76065,76067,76069,76071,76073,76075,76077,76079,76081,76083],{"class":54,"line":148},[52,76060,75542],{"class":58},[52,76062,75545],{"class":62},[52,76064,11213],{"class":58},[52,76066,36864],{"class":241},[52,76068,193],{"class":102},[52,76070,13294],{"class":121},[52,76072,199],{"class":102},[52,76074,32139],{"class":189},[52,76076,5889],{"class":79},[52,76078,36859],{"class":189},[52,76080,5889],{"class":79},[52,76082,1518],{"class":121},[52,76084,3114],{"class":102},[52,76086,76087,76089,76091,76093,76095,76097,76099,76101,76103,76105,76107,76109],{"class":54,"line":164},[52,76088,49378],{"class":58},[52,76090,34128],{"class":62},[52,76092,250],{"class":102},[52,76094,75578],{"class":62},[52,76096,11122],{"class":102},[52,76098,75583],{"class":79},[52,76100,34128],{"class":62},[52,76102,250],{"class":102},[52,76104,75590],{"class":62},[52,76106,9881],{"class":79},[52,76108,1518],{"class":121},[52,76110,35426],{"class":102},[52,76112,76113,76115,76117,76119,76121,76123,76125,76127,76129,76131,76133,76135,76137,76139,76141,76143,76145,76147,76149,76151,76153],{"class":54,"line":170},[52,76114,75601],{"class":62},[52,76116,250],{"class":102},[52,76118,75578],{"class":62},[52,76120,15661],{"class":102},[52,76122,34128],{"class":62},[52,76124,250],{"class":102},[52,76126,75590],{"class":62},[52,76128,9881],{"class":79},[52,76130,1518],{"class":121},[52,76132,11122],{"class":102},[52,76134,7038],{"class":79},[52,76136,34128],{"class":62},[52,76138,250],{"class":102},[52,76140,75590],{"class":62},[52,76142,9881],{"class":79},[52,76144,1518],{"class":121},[52,76146,15661],{"class":102},[52,76148,34128],{"class":62},[52,76150,250],{"class":102},[52,76152,75578],{"class":62},[52,76154,1147],{"class":102},[52,76156,76157,76159,76161],{"class":54,"line":175},[52,76158,75646],{"class":62},[52,76160,80],{"class":79},[52,76162,161],{"class":160},[52,76164,76165,76167,76169,76171],{"class":54,"line":214},[52,76166,11168],{"class":58},[52,76168,1447],{"class":79},[52,76170,75659],{"class":62},[52,76172,1330],{"class":102},[52,76174,76175],{"class":54,"line":233},[52,76176,48114],{"class":58},[52,76178,76179],{"class":54,"line":238},[52,76180,70],{"emptyLinePlaceholder":69},[52,76182,76183,76185],{"class":54,"line":272},[52,76184,20958],{"class":58},[52,76186,75676],{"class":62},[639,76188,9610],{"id":9609},[24097,76190,76192],{"id":76191},"_1-create-a-reusable-function","1. Create a reusable function",[42,76194,76196],{"className":44,"code":76195,"language":46,"meta":47,"style":47},"def bubble_sort(numbers):\n",[49,76197,76198],{"__ignoreMap":47},[52,76199,76200,76202,76204,76206,76208],{"class":54,"line":55},[52,76201,7410],{"class":1475},[52,76203,75471],{"class":7065},[52,76205,193],{"class":102},[52,76207,5324],{"class":7018},[52,76209,3114],{"class":102},[14,76211,76212],{},"This puts the sorting logic inside a function so you can use it with different lists.",[24097,76214,76216],{"id":76215},"_2-copy-the-input-list","2. Copy the input list",[42,76218,76220],{"className":44,"code":76219,"language":46,"meta":47,"style":47},"items = numbers.copy()\n",[49,76221,76222],{"__ignoreMap":47},[52,76223,76224,76226,76228,76230,76232,76234],{"class":54,"line":55},[52,76225,33220],{"class":62},[52,76227,80],{"class":79},[52,76229,32302],{"class":62},[52,76231,186],{"class":102},[52,76233,28621],{"class":189},[52,76235,230],{"class":102},[14,76237,76238],{},"This creates a copy so the original list is not changed.",[14,76240,76241],{},"That means this code:",[42,76243,76245],{"className":44,"code":76244,"language":46,"meta":47,"style":47},"values = [5, 2, 9, 1, 3]\nresult = bubble_sort(values)\n\nprint(\"original:\", values)\nprint(\"sorted:\", result)\n",[49,76246,76247,76275,76289,76293,76312],{"__ignoreMap":47},[52,76248,76249,76251,76253,76255,76257,76259,76261,76263,76265,76267,76269,76271,76273],{"class":54,"line":55},[52,76250,75685],{"class":62},[52,76252,80],{"class":79},[52,76254,18162],{"class":102},[52,76256,9479],{"class":121},[52,76258,199],{"class":102},[52,76260,9168],{"class":121},[52,76262,199],{"class":102},[52,76264,66568],{"class":121},[52,76266,199],{"class":102},[52,76268,1518],{"class":121},[52,76270,199],{"class":102},[52,76272,5261],{"class":121},[52,76274,1147],{"class":102},[52,76276,76277,76279,76281,76283,76285,76287],{"class":54,"line":66},[52,76278,32778],{"class":62},[52,76280,80],{"class":79},[52,76282,75471],{"class":189},[52,76284,193],{"class":102},[52,76286,22462],{"class":189},[52,76288,211],{"class":102},[52,76290,76291],{"class":54,"line":73},[52,76292,70],{"emptyLinePlaceholder":69},[52,76294,76295,76297,76299,76301,76303,76305,76307,76310],{"class":54,"line":94},[52,76296,242],{"class":241},[52,76298,193],{"class":102},[52,76300,115],{"class":83},[52,76302,44479],{"class":87},[52,76304,115],{"class":83},[52,76306,199],{"class":102},[52,76308,76309],{"class":189}," values",[52,76311,211],{"class":102},[52,76313,76314,76316,76318,76320,76323,76325,76327,76329],{"class":54,"line":106},[52,76315,242],{"class":241},[52,76317,193],{"class":102},[52,76319,115],{"class":83},[52,76321,76322],{"class":87},"sorted:",[52,76324,115],{"class":83},[52,76326,199],{"class":102},[52,76328,27130],{"class":189},[52,76330,211],{"class":102},[14,76332,76333],{},"produces:",[42,76335,76337],{"className":44,"code":76336,"language":46,"meta":47,"style":47},"original: [5, 2, 9, 1, 3]\nsorted: [1, 2, 3, 5, 9]\n",[49,76338,76339,76367],{"__ignoreMap":47},[52,76340,76341,76343,76345,76347,76349,76351,76353,76355,76357,76359,76361,76363,76365],{"class":54,"line":55},[52,76342,45636],{"class":62},[52,76344,118],{"class":102},[52,76346,18162],{"class":102},[52,76348,9479],{"class":121},[52,76350,199],{"class":102},[52,76352,9168],{"class":121},[52,76354,199],{"class":102},[52,76356,66568],{"class":121},[52,76358,199],{"class":102},[52,76360,1518],{"class":121},[52,76362,199],{"class":102},[52,76364,5261],{"class":121},[52,76366,1147],{"class":102},[52,76368,76369,76372,76374,76376,76378,76380,76382,76384,76386,76388,76390,76392,76394],{"class":54,"line":66},[52,76370,76371],{"class":241},"sorted",[52,76373,118],{"class":102},[52,76375,18162],{"class":102},[52,76377,9200],{"class":121},[52,76379,199],{"class":102},[52,76381,9168],{"class":121},[52,76383,199],{"class":102},[52,76385,5261],{"class":121},[52,76387,199],{"class":102},[52,76389,7646],{"class":121},[52,76391,199],{"class":102},[52,76393,66568],{"class":121},[52,76395,1147],{"class":102},[24097,76397,76399],{"id":76398},"_3-get-the-list-length","3. Get the list length",[42,76401,76403],{"className":44,"code":76402,"language":46,"meta":47,"style":47},"n = len(items)\n",[49,76404,76405],{"__ignoreMap":47},[52,76406,76407,76409,76411,76413,76415,76417],{"class":54,"line":55},[52,76408,32134],{"class":62},[52,76410,80],{"class":79},[52,76412,9150],{"class":241},[52,76414,193],{"class":102},[52,76416,11220],{"class":189},[52,76418,211],{"class":102},[14,76420,76421],{},"We store the length because we use it several times in the loops.",[24097,76423,76425],{"id":76424},"_4-use-an-outer-loop-for-repeated-passes","4. Use an outer loop for repeated passes",[42,76427,76429],{"className":44,"code":76428,"language":46,"meta":47,"style":47},"for i in range(n):\n",[49,76430,76431],{"__ignoreMap":47},[52,76432,76433,76435,76437,76439,76441,76443,76445],{"class":54,"line":55},[52,76434,12265],{"class":58},[52,76436,36859],{"class":62},[52,76438,11213],{"class":58},[52,76440,36864],{"class":241},[52,76442,193],{"class":102},[52,76444,44412],{"class":189},[52,76446,3114],{"class":102},[14,76448,76449],{},"Each pass moves one more large value into the correct position.",[24097,76451,76453],{"id":76452},"_5-use-an-inner-loop-for-adjacent-comparisons","5. Use an inner loop for adjacent comparisons",[42,76455,76457],{"className":44,"code":76456,"language":46,"meta":47,"style":47},"for j in range(0, n - i - 1):\n",[49,76458,76459],{"__ignoreMap":47},[52,76460,76461,76463,76465,76467,76469,76471,76473,76475,76477,76479,76481,76483,76485],{"class":54,"line":55},[52,76462,12265],{"class":58},[52,76464,75545],{"class":62},[52,76466,11213],{"class":58},[52,76468,36864],{"class":241},[52,76470,193],{"class":102},[52,76472,13294],{"class":121},[52,76474,199],{"class":102},[52,76476,32139],{"class":189},[52,76478,5889],{"class":79},[52,76480,36859],{"class":189},[52,76482,5889],{"class":79},[52,76484,1518],{"class":121},[52,76486,3114],{"class":102},[14,76488,76489],{},"This loop compares neighboring items:",[309,76491,76492,76497],{},[24,76493,76494],{},[49,76495,76496],{},"items[j]",[24,76498,76499],{},[49,76500,76501],{},"items[j + 1]",[14,76503,358,76504,76507],{},[49,76505,76506],{},"n - i - 1"," part is important. It prevents the loop from going too far and avoids comparing values that are already in the correct place.",[24097,76509,76511],{"id":76510},"_6-swap-values-when-needed","6. Swap values when needed",[42,76513,76515],{"className":44,"code":76514,"language":46,"meta":47,"style":47},"if items[j] > items[j + 1]:\n    items[j], items[j + 1] = items[j + 1], items[j]\n",[49,76516,76517,76543],{"__ignoreMap":47},[52,76518,76519,76521,76523,76525,76527,76529,76531,76533,76535,76537,76539,76541],{"class":54,"line":55},[52,76520,1313],{"class":58},[52,76522,34128],{"class":62},[52,76524,250],{"class":102},[52,76526,75578],{"class":62},[52,76528,11122],{"class":102},[52,76530,75583],{"class":79},[52,76532,34128],{"class":62},[52,76534,250],{"class":102},[52,76536,75590],{"class":62},[52,76538,9881],{"class":79},[52,76540,1518],{"class":121},[52,76542,35426],{"class":102},[52,76544,76545,76548,76550,76552,76554,76556,76558,76560,76562,76564,76566,76568,76570,76572,76574,76576,76578,76580,76582,76584,76586],{"class":54,"line":66},[52,76546,76547],{"class":62},"    items",[52,76549,250],{"class":102},[52,76551,75578],{"class":62},[52,76553,15661],{"class":102},[52,76555,34128],{"class":62},[52,76557,250],{"class":102},[52,76559,75590],{"class":62},[52,76561,9881],{"class":79},[52,76563,1518],{"class":121},[52,76565,11122],{"class":102},[52,76567,7038],{"class":79},[52,76569,34128],{"class":62},[52,76571,250],{"class":102},[52,76573,75590],{"class":62},[52,76575,9881],{"class":79},[52,76577,1518],{"class":121},[52,76579,15661],{"class":102},[52,76581,34128],{"class":62},[52,76583,250],{"class":102},[52,76585,75578],{"class":62},[52,76587,1147],{"class":102},[14,76589,76590],{},"This checks whether the left value is bigger than the right value.",[14,76592,76593],{},"If it is, the two values are swapped.",[14,76595,76596],{},"The line:",[42,76598,76600],{"className":44,"code":76599,"language":46,"meta":47,"style":47},"items[j], items[j + 1] = items[j + 1], items[j]\n",[49,76601,76602],{"__ignoreMap":47},[52,76603,76604,76606,76608,76610,76612,76614,76616,76618,76620,76622,76624,76626,76628,76630,76632,76634,76636,76638,76640,76642,76644],{"class":54,"line":55},[52,76605,11220],{"class":62},[52,76607,250],{"class":102},[52,76609,75578],{"class":62},[52,76611,15661],{"class":102},[52,76613,34128],{"class":62},[52,76615,250],{"class":102},[52,76617,75590],{"class":62},[52,76619,9881],{"class":79},[52,76621,1518],{"class":121},[52,76623,11122],{"class":102},[52,76625,7038],{"class":79},[52,76627,34128],{"class":62},[52,76629,250],{"class":102},[52,76631,75590],{"class":62},[52,76633,9881],{"class":79},[52,76635,1518],{"class":121},[52,76637,15661],{"class":102},[52,76639,34128],{"class":62},[52,76641,250],{"class":102},[52,76643,75578],{"class":62},[52,76645,1147],{"class":102},[14,76647,76648],{},"uses tuple assignment, which is a short Python way to swap two values.",[37,76650,76652],{"id":76651},"add-a-simple-optimization","Add a simple optimization",[14,76654,76655],{},"This version includes a small improvement:",[42,76657,76659],{"className":44,"code":76658,"language":46,"meta":47,"style":47},"swapped = False\n",[49,76660,76661],{"__ignoreMap":47},[52,76662,76663,76666,76668],{"class":54,"line":55},[52,76664,76665],{"class":62},"swapped ",[52,76667,80],{"class":79},[52,76669,75537],{"class":160},[14,76671,76672],{},"At the start of each pass, we assume no swaps will happen.",[14,76674,76675],{},"Then, if a swap does happen, we change it:",[42,76677,76679],{"className":44,"code":76678,"language":46,"meta":47,"style":47},"swapped = True\n",[49,76680,76681],{"__ignoreMap":47},[52,76682,76683,76685,76687],{"class":54,"line":55},[52,76684,76665],{"class":62},[52,76686,80],{"class":79},[52,76688,161],{"class":160},[14,76690,76691],{},"At the end of the pass:",[42,76693,76695],{"className":44,"code":76694,"language":46,"meta":47,"style":47},"if not swapped:\n    break\n",[49,76696,76697,76707],{"__ignoreMap":47},[52,76698,76699,76701,76703,76705],{"class":54,"line":55},[52,76700,1313],{"class":58},[52,76702,1447],{"class":79},[52,76704,75659],{"class":62},[52,76706,1330],{"class":102},[52,76708,76709],{"class":54,"line":66},[52,76710,52013],{"class":58},[14,76712,76713],{},"If no swap happened, the list is already sorted, so the function stops early.",[14,76715,76716,76717,759,76721,186],{},"This makes the example a little better, but bubble sort is still slow compared with Python’s built-in tools like ",[372,76718,76719],{"href":75455},[49,76720,75458],{},[372,76722,76724],{"href":76723},"\u002Freference\u002Fpython-list-sort-method\u002F",[49,76725,76726],{},"list.sort()",[37,76728,76730],{"id":76729},"example-input-and-expected-output","Example input and expected output",[14,76732,76733],{},"Here is a complete example:",[42,76735,76737],{"className":44,"code":76736,"language":46,"meta":47,"style":47},"def bubble_sort(numbers):\n    items = numbers.copy()\n    n = len(items)\n\n    for i in range(n):\n        swapped = False\n        for j in range(0, n - i - 1):\n            if items[j] > items[j + 1]:\n                items[j], items[j + 1] = items[j + 1], items[j]\n                swapped = True\n        if not swapped:\n            break\n\n    return items\n\nvalues = [5, 2, 9, 1, 3]\nsorted_values = bubble_sort(values)\n\nprint(\"Original:\", values)\nprint(\"Sorted:  \", sorted_values)\n",[49,76738,76739,76751,76765,76779,76783,76799,76807,76835,76861,76905,76913,76923,76927,76931,76937,76941,76969,76984,76988,77007],{"__ignoreMap":47},[52,76740,76741,76743,76745,76747,76749],{"class":54,"line":55},[52,76742,7410],{"class":1475},[52,76744,75471],{"class":7065},[52,76746,193],{"class":102},[52,76748,5324],{"class":7018},[52,76750,3114],{"class":102},[52,76752,76753,76755,76757,76759,76761,76763],{"class":54,"line":66},[52,76754,75482],{"class":62},[52,76756,80],{"class":79},[52,76758,32302],{"class":62},[52,76760,186],{"class":102},[52,76762,28621],{"class":189},[52,76764,230],{"class":102},[52,76766,76767,76769,76771,76773,76775,76777],{"class":54,"line":73},[52,76768,75497],{"class":62},[52,76770,80],{"class":79},[52,76772,9150],{"class":241},[52,76774,193],{"class":102},[52,76776,11220],{"class":189},[52,76778,211],{"class":102},[52,76780,76781],{"class":54,"line":94},[52,76782,70],{"emptyLinePlaceholder":69},[52,76784,76785,76787,76789,76791,76793,76795,76797],{"class":54,"line":106},[52,76786,11773],{"class":58},[52,76788,36859],{"class":62},[52,76790,11213],{"class":58},[52,76792,36864],{"class":241},[52,76794,193],{"class":102},[52,76796,44412],{"class":189},[52,76798,3114],{"class":102},[52,76800,76801,76803,76805],{"class":54,"line":128},[52,76802,75532],{"class":62},[52,76804,80],{"class":79},[52,76806,75537],{"class":160},[52,76808,76809,76811,76813,76815,76817,76819,76821,76823,76825,76827,76829,76831,76833],{"class":54,"line":148},[52,76810,75542],{"class":58},[52,76812,75545],{"class":62},[52,76814,11213],{"class":58},[52,76816,36864],{"class":241},[52,76818,193],{"class":102},[52,76820,13294],{"class":121},[52,76822,199],{"class":102},[52,76824,32139],{"class":189},[52,76826,5889],{"class":79},[52,76828,36859],{"class":189},[52,76830,5889],{"class":79},[52,76832,1518],{"class":121},[52,76834,3114],{"class":102},[52,76836,76837,76839,76841,76843,76845,76847,76849,76851,76853,76855,76857,76859],{"class":54,"line":164},[52,76838,49378],{"class":58},[52,76840,34128],{"class":62},[52,76842,250],{"class":102},[52,76844,75578],{"class":62},[52,76846,11122],{"class":102},[52,76848,75583],{"class":79},[52,76850,34128],{"class":62},[52,76852,250],{"class":102},[52,76854,75590],{"class":62},[52,76856,9881],{"class":79},[52,76858,1518],{"class":121},[52,76860,35426],{"class":102},[52,76862,76863,76865,76867,76869,76871,76873,76875,76877,76879,76881,76883,76885,76887,76889,76891,76893,76895,76897,76899,76901,76903],{"class":54,"line":170},[52,76864,75601],{"class":62},[52,76866,250],{"class":102},[52,76868,75578],{"class":62},[52,76870,15661],{"class":102},[52,76872,34128],{"class":62},[52,76874,250],{"class":102},[52,76876,75590],{"class":62},[52,76878,9881],{"class":79},[52,76880,1518],{"class":121},[52,76882,11122],{"class":102},[52,76884,7038],{"class":79},[52,76886,34128],{"class":62},[52,76888,250],{"class":102},[52,76890,75590],{"class":62},[52,76892,9881],{"class":79},[52,76894,1518],{"class":121},[52,76896,15661],{"class":102},[52,76898,34128],{"class":62},[52,76900,250],{"class":102},[52,76902,75578],{"class":62},[52,76904,1147],{"class":102},[52,76906,76907,76909,76911],{"class":54,"line":175},[52,76908,75646],{"class":62},[52,76910,80],{"class":79},[52,76912,161],{"class":160},[52,76914,76915,76917,76919,76921],{"class":54,"line":214},[52,76916,11168],{"class":58},[52,76918,1447],{"class":79},[52,76920,75659],{"class":62},[52,76922,1330],{"class":102},[52,76924,76925],{"class":54,"line":233},[52,76926,48114],{"class":58},[52,76928,76929],{"class":54,"line":238},[52,76930,70],{"emptyLinePlaceholder":69},[52,76932,76933,76935],{"class":54,"line":272},[52,76934,20958],{"class":58},[52,76936,75676],{"class":62},[52,76938,76939],{"class":54,"line":1348},[52,76940,70],{"emptyLinePlaceholder":69},[52,76942,76943,76945,76947,76949,76951,76953,76955,76957,76959,76961,76963,76965,76967],{"class":54,"line":1376},[52,76944,75685],{"class":62},[52,76946,80],{"class":79},[52,76948,18162],{"class":102},[52,76950,9479],{"class":121},[52,76952,199],{"class":102},[52,76954,9168],{"class":121},[52,76956,199],{"class":102},[52,76958,66568],{"class":121},[52,76960,199],{"class":102},[52,76962,1518],{"class":121},[52,76964,199],{"class":102},[52,76966,5261],{"class":121},[52,76968,1147],{"class":102},[52,76970,76971,76974,76976,76978,76980,76982],{"class":54,"line":1381},[52,76972,76973],{"class":62},"sorted_values ",[52,76975,80],{"class":79},[52,76977,75471],{"class":189},[52,76979,193],{"class":102},[52,76981,22462],{"class":189},[52,76983,211],{"class":102},[52,76985,76986],{"class":54,"line":1406},[52,76987,70],{"emptyLinePlaceholder":69},[52,76989,76990,76992,76994,76996,76999,77001,77003,77005],{"class":54,"line":1430},[52,76991,242],{"class":241},[52,76993,193],{"class":102},[52,76995,115],{"class":83},[52,76997,76998],{"class":87},"Original:",[52,77000,115],{"class":83},[52,77002,199],{"class":102},[52,77004,76309],{"class":189},[52,77006,211],{"class":102},[52,77008,77009,77011,77013,77015,77018,77020,77022,77025],{"class":54,"line":1435},[52,77010,242],{"class":241},[52,77012,193],{"class":102},[52,77014,115],{"class":83},[52,77016,77017],{"class":87},"Sorted:  ",[52,77019,115],{"class":83},[52,77021,199],{"class":102},[52,77023,77024],{"class":189}," sorted_values",[52,77026,211],{"class":102},[14,77028,77029],{},[5503,77030,5267],{},[42,77032,77034],{"className":44,"code":77033,"language":46,"meta":47,"style":47},"Original: [5, 2, 9, 1, 3]\nSorted:   [1, 2, 3, 5, 9]\n",[49,77035,77036,77065],{"__ignoreMap":47},[52,77037,77038,77041,77043,77045,77047,77049,77051,77053,77055,77057,77059,77061,77063],{"class":54,"line":55},[52,77039,77040],{"class":62},"Original",[52,77042,118],{"class":102},[52,77044,18162],{"class":102},[52,77046,9479],{"class":121},[52,77048,199],{"class":102},[52,77050,9168],{"class":121},[52,77052,199],{"class":102},[52,77054,66568],{"class":121},[52,77056,199],{"class":102},[52,77058,1518],{"class":121},[52,77060,199],{"class":102},[52,77062,5261],{"class":121},[52,77064,1147],{"class":102},[52,77066,77067,77070,77072,77075,77077,77079,77081,77083,77085,77087,77089,77091,77093],{"class":54,"line":66},[52,77068,77069],{"class":62},"Sorted",[52,77071,118],{"class":102},[52,77073,77074],{"class":102},"   [",[52,77076,9200],{"class":121},[52,77078,199],{"class":102},[52,77080,9168],{"class":121},[52,77082,199],{"class":102},[52,77084,5261],{"class":121},[52,77086,199],{"class":102},[52,77088,7646],{"class":121},[52,77090,199],{"class":102},[52,77092,66568],{"class":121},[52,77094,1147],{"class":102},[639,77096,77098],{"id":77097},"version-that-sorts-in-place","Version that sorts in place",[14,77100,77101,77102,118],{},"If you want to change the original list directly, you can remove ",[49,77103,77104],{},".copy()",[42,77106,77108],{"className":44,"code":77107,"language":46,"meta":47,"style":47},"def bubble_sort_in_place(items):\n    n = len(items)\n\n    for i in range(n):\n        swapped = False\n        for j in range(0, n - i - 1):\n            if items[j] > items[j + 1]:\n                items[j], items[j + 1] = items[j + 1], items[j]\n                swapped = True\n        if not swapped:\n            break\n\nvalues = [5, 2, 9, 1, 3]\nbubble_sort_in_place(values)\nprint(values)\n",[49,77109,77110,77123,77137,77141,77157,77165,77193,77219,77263,77271,77281,77285,77289,77317,77328],{"__ignoreMap":47},[52,77111,77112,77114,77117,77119,77121],{"class":54,"line":55},[52,77113,7410],{"class":1475},[52,77115,77116],{"class":7065}," bubble_sort_in_place",[52,77118,193],{"class":102},[52,77120,11220],{"class":7018},[52,77122,3114],{"class":102},[52,77124,77125,77127,77129,77131,77133,77135],{"class":54,"line":66},[52,77126,75497],{"class":62},[52,77128,80],{"class":79},[52,77130,9150],{"class":241},[52,77132,193],{"class":102},[52,77134,11220],{"class":189},[52,77136,211],{"class":102},[52,77138,77139],{"class":54,"line":73},[52,77140,70],{"emptyLinePlaceholder":69},[52,77142,77143,77145,77147,77149,77151,77153,77155],{"class":54,"line":94},[52,77144,11773],{"class":58},[52,77146,36859],{"class":62},[52,77148,11213],{"class":58},[52,77150,36864],{"class":241},[52,77152,193],{"class":102},[52,77154,44412],{"class":189},[52,77156,3114],{"class":102},[52,77158,77159,77161,77163],{"class":54,"line":106},[52,77160,75532],{"class":62},[52,77162,80],{"class":79},[52,77164,75537],{"class":160},[52,77166,77167,77169,77171,77173,77175,77177,77179,77181,77183,77185,77187,77189,77191],{"class":54,"line":128},[52,77168,75542],{"class":58},[52,77170,75545],{"class":62},[52,77172,11213],{"class":58},[52,77174,36864],{"class":241},[52,77176,193],{"class":102},[52,77178,13294],{"class":121},[52,77180,199],{"class":102},[52,77182,32139],{"class":189},[52,77184,5889],{"class":79},[52,77186,36859],{"class":189},[52,77188,5889],{"class":79},[52,77190,1518],{"class":121},[52,77192,3114],{"class":102},[52,77194,77195,77197,77199,77201,77203,77205,77207,77209,77211,77213,77215,77217],{"class":54,"line":148},[52,77196,49378],{"class":58},[52,77198,34128],{"class":62},[52,77200,250],{"class":102},[52,77202,75578],{"class":62},[52,77204,11122],{"class":102},[52,77206,75583],{"class":79},[52,77208,34128],{"class":62},[52,77210,250],{"class":102},[52,77212,75590],{"class":62},[52,77214,9881],{"class":79},[52,77216,1518],{"class":121},[52,77218,35426],{"class":102},[52,77220,77221,77223,77225,77227,77229,77231,77233,77235,77237,77239,77241,77243,77245,77247,77249,77251,77253,77255,77257,77259,77261],{"class":54,"line":164},[52,77222,75601],{"class":62},[52,77224,250],{"class":102},[52,77226,75578],{"class":62},[52,77228,15661],{"class":102},[52,77230,34128],{"class":62},[52,77232,250],{"class":102},[52,77234,75590],{"class":62},[52,77236,9881],{"class":79},[52,77238,1518],{"class":121},[52,77240,11122],{"class":102},[52,77242,7038],{"class":79},[52,77244,34128],{"class":62},[52,77246,250],{"class":102},[52,77248,75590],{"class":62},[52,77250,9881],{"class":79},[52,77252,1518],{"class":121},[52,77254,15661],{"class":102},[52,77256,34128],{"class":62},[52,77258,250],{"class":102},[52,77260,75578],{"class":62},[52,77262,1147],{"class":102},[52,77264,77265,77267,77269],{"class":54,"line":170},[52,77266,75646],{"class":62},[52,77268,80],{"class":79},[52,77270,161],{"class":160},[52,77272,77273,77275,77277,77279],{"class":54,"line":175},[52,77274,11168],{"class":58},[52,77276,1447],{"class":79},[52,77278,75659],{"class":62},[52,77280,1330],{"class":102},[52,77282,77283],{"class":54,"line":214},[52,77284,48114],{"class":58},[52,77286,77287],{"class":54,"line":233},[52,77288,70],{"emptyLinePlaceholder":69},[52,77290,77291,77293,77295,77297,77299,77301,77303,77305,77307,77309,77311,77313,77315],{"class":54,"line":238},[52,77292,75685],{"class":62},[52,77294,80],{"class":79},[52,77296,18162],{"class":102},[52,77298,9479],{"class":121},[52,77300,199],{"class":102},[52,77302,9168],{"class":121},[52,77304,199],{"class":102},[52,77306,66568],{"class":121},[52,77308,199],{"class":102},[52,77310,1518],{"class":121},[52,77312,199],{"class":102},[52,77314,5261],{"class":121},[52,77316,1147],{"class":102},[52,77318,77319,77322,77324,77326],{"class":54,"line":272},[52,77320,77321],{"class":189},"bubble_sort_in_place",[52,77323,193],{"class":102},[52,77325,22462],{"class":189},[52,77327,211],{"class":102},[52,77329,77330,77332,77334,77336],{"class":54,"line":1348},[52,77331,242],{"class":241},[52,77333,193],{"class":102},[52,77335,22462],{"class":189},[52,77337,211],{"class":102},[14,77339,77340],{},[5503,77341,5267],{},[42,77343,77344],{"className":44,"code":75731,"language":46,"meta":47,"style":47},[49,77345,77346],{"__ignoreMap":47},[52,77347,77348,77350,77352,77354,77356,77358,77360,77362,77364,77366,77368],{"class":54,"line":55},[52,77349,250],{"class":102},[52,77351,9200],{"class":121},[52,77353,199],{"class":102},[52,77355,9168],{"class":121},[52,77357,199],{"class":102},[52,77359,5261],{"class":121},[52,77361,199],{"class":102},[52,77363,7646],{"class":121},[52,77365,199],{"class":102},[52,77367,66568],{"class":121},[52,77369,1147],{"class":102},[14,77371,77372],{},"This version changes the original list.",[37,77374,77376],{"id":77375},"when-not-to-use-bubble-sort","When not to use bubble sort",[14,77378,77379],{},"Bubble sort is helpful for understanding how sorting works, but it is not a good choice for most real code.",[14,77381,77382],{},"Avoid it when:",[309,77384,77385,77388,77391],{},[24,77386,77387],{},"You are sorting larger lists",[24,77389,77390],{},"You want the simplest solution",[24,77392,77393],{},"You care about performance",[14,77395,77396],{},"In normal Python code, use:",[309,77398,77399,77406,77413],{},[24,77400,77401],{},[372,77402,77403,77405],{"href":75455},[49,77404,75458],{}," for returning a new sorted list",[24,77407,77408],{},[372,77409,77410,77412],{"href":76723},[49,77411,76726],{}," for sorting a list in place",[24,77414,77415,77419],{},[372,77416,77418],{"href":77417},"\u002Fhow-to\u002Fhow-to-sort-a-list-in-python\u002F","How to sort a list in Python"," if you want the practical approach",[37,77421,12322],{"id":12321},[14,77423,77424],{},"Here are some common problems beginners run into when writing bubble sort.",[639,77426,77428],{"id":77427},"using-the-wrong-inner-loop-range","Using the wrong inner loop range",[14,77430,77431,77432,77434],{},"A very common bug is letting ",[49,77433,75578],{}," go too far.",[14,77436,10306],{},[42,77438,77440],{"className":44,"code":77439,"language":46,"meta":47,"style":47},"def broken_bubble_sort(items):\n    n = len(items)\n    for i in range(n):\n        for j in range(n):\n            if items[j] > items[j + 1]:\n                items[j], items[j + 1] = items[j + 1], items[j]\n",[49,77441,77442,77455,77469,77485,77501,77527],{"__ignoreMap":47},[52,77443,77444,77446,77449,77451,77453],{"class":54,"line":55},[52,77445,7410],{"class":1475},[52,77447,77448],{"class":7065}," broken_bubble_sort",[52,77450,193],{"class":102},[52,77452,11220],{"class":7018},[52,77454,3114],{"class":102},[52,77456,77457,77459,77461,77463,77465,77467],{"class":54,"line":66},[52,77458,75497],{"class":62},[52,77460,80],{"class":79},[52,77462,9150],{"class":241},[52,77464,193],{"class":102},[52,77466,11220],{"class":189},[52,77468,211],{"class":102},[52,77470,77471,77473,77475,77477,77479,77481,77483],{"class":54,"line":73},[52,77472,11773],{"class":58},[52,77474,36859],{"class":62},[52,77476,11213],{"class":58},[52,77478,36864],{"class":241},[52,77480,193],{"class":102},[52,77482,44412],{"class":189},[52,77484,3114],{"class":102},[52,77486,77487,77489,77491,77493,77495,77497,77499],{"class":54,"line":94},[52,77488,75542],{"class":58},[52,77490,75545],{"class":62},[52,77492,11213],{"class":58},[52,77494,36864],{"class":241},[52,77496,193],{"class":102},[52,77498,44412],{"class":189},[52,77500,3114],{"class":102},[52,77502,77503,77505,77507,77509,77511,77513,77515,77517,77519,77521,77523,77525],{"class":54,"line":106},[52,77504,49378],{"class":58},[52,77506,34128],{"class":62},[52,77508,250],{"class":102},[52,77510,75578],{"class":62},[52,77512,11122],{"class":102},[52,77514,75583],{"class":79},[52,77516,34128],{"class":62},[52,77518,250],{"class":102},[52,77520,75590],{"class":62},[52,77522,9881],{"class":79},[52,77524,1518],{"class":121},[52,77526,35426],{"class":102},[52,77528,77529,77531,77533,77535,77537,77539,77541,77543,77545,77547,77549,77551,77553,77555,77557,77559,77561,77563,77565,77567,77569],{"class":54,"line":128},[52,77530,75601],{"class":62},[52,77532,250],{"class":102},[52,77534,75578],{"class":62},[52,77536,15661],{"class":102},[52,77538,34128],{"class":62},[52,77540,250],{"class":102},[52,77542,75590],{"class":62},[52,77544,9881],{"class":79},[52,77546,1518],{"class":121},[52,77548,11122],{"class":102},[52,77550,7038],{"class":79},[52,77552,34128],{"class":62},[52,77554,250],{"class":102},[52,77556,75590],{"class":62},[52,77558,9881],{"class":79},[52,77560,1518],{"class":121},[52,77562,15661],{"class":102},[52,77564,34128],{"class":62},[52,77566,250],{"class":102},[52,77568,75578],{"class":62},[52,77570,1147],{"class":102},[14,77572,77573,77574,77577],{},"This can raise an error because ",[49,77575,77576],{},"j + 1"," becomes invalid near the end of the list.",[14,77579,77580,77581,186],{},"If you get this problem, see ",[372,77582,45887],{"href":77583},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained\u002F",[639,77585,77587],{"id":77586},"forgetting-to-swap-values","Forgetting to swap values",[14,77589,77590],{},"Sometimes the comparison is correct, but the code never swaps the values.",[14,77592,77593],{},"Make sure you include:",[42,77595,77596],{"className":44,"code":76599,"language":46,"meta":47,"style":47},[49,77597,77598],{"__ignoreMap":47},[52,77599,77600,77602,77604,77606,77608,77610,77612,77614,77616,77618,77620,77622,77624,77626,77628,77630,77632,77634,77636,77638,77640],{"class":54,"line":55},[52,77601,11220],{"class":62},[52,77603,250],{"class":102},[52,77605,75578],{"class":62},[52,77607,15661],{"class":102},[52,77609,34128],{"class":62},[52,77611,250],{"class":102},[52,77613,75590],{"class":62},[52,77615,9881],{"class":79},[52,77617,1518],{"class":121},[52,77619,11122],{"class":102},[52,77621,7038],{"class":79},[52,77623,34128],{"class":62},[52,77625,250],{"class":102},[52,77627,75590],{"class":62},[52,77629,9881],{"class":79},[52,77631,1518],{"class":121},[52,77633,15661],{"class":102},[52,77635,34128],{"class":62},[52,77637,250],{"class":102},[52,77639,75578],{"class":62},[52,77641,1147],{"class":102},[639,77643,45756],{"id":77644},"changing-the-original-list-by-accident",[14,77646,77647],{},"If you expected the original list to stay the same, use:",[42,77649,77650],{"className":44,"code":76219,"language":46,"meta":47,"style":47},[49,77651,77652],{"__ignoreMap":47},[52,77653,77654,77656,77658,77660,77662,77664],{"class":54,"line":55},[52,77655,33220],{"class":62},[52,77657,80],{"class":79},[52,77659,32302],{"class":62},[52,77661,186],{"class":102},[52,77663,28621],{"class":189},[52,77665,230],{"class":102},[14,77667,77668],{},"Without that line, the function will change the original list directly.",[639,77670,77672],{"id":77671},"using-the-wrong-comparison-operator","Using the wrong comparison operator",[14,77674,77675],{},"For ascending order, use:",[42,77677,77679],{"className":44,"code":77678,"language":46,"meta":47,"style":47},"if items[j] > items[j + 1]:\n",[49,77680,77681],{"__ignoreMap":47},[52,77682,77683,77685,77687,77689,77691,77693,77695,77697,77699,77701,77703,77705],{"class":54,"line":55},[52,77684,1313],{"class":58},[52,77686,34128],{"class":62},[52,77688,250],{"class":102},[52,77690,75578],{"class":62},[52,77692,11122],{"class":102},[52,77694,75583],{"class":79},[52,77696,34128],{"class":62},[52,77698,250],{"class":102},[52,77700,75590],{"class":62},[52,77702,9881],{"class":79},[52,77704,1518],{"class":121},[52,77706,35426],{"class":102},[14,77708,77709,77710,77712],{},"If you accidentally use ",[49,77711,15294],{},", you will sort in descending order instead.",[37,77714,1942],{"id":1941},[639,77716,77718],{"id":77717},"is-bubble-sort-useful-in-real-python-programs","Is bubble sort useful in real Python programs?",[14,77720,77721],{},"Usually no. It is mainly useful for learning how sorting algorithms work.",[639,77723,3638,77725,77727],{"id":77724},"should-i-use-sorted-instead-of-bubble-sort",[49,77726,75458],{}," instead of bubble sort?",[14,77729,77730,77731,77733],{},"Yes, in most real code. ",[49,77732,75458],{}," is simpler and much faster.",[639,77735,77737],{"id":77736},"does-this-function-change-the-original-list","Does this function change the original list?",[14,77739,77740],{},"Not if the function sorts a copy. If it works directly on the input list, then yes.",[639,77742,37859,77744,1995],{"id":77743},"why-do-i-get-list-index-out-of-range",[49,77745,77746],{},"list index out of range",[14,77748,77749,77750,77752],{},"The inner loop probably goes too far, so ",[49,77751,77576],{}," becomes an invalid index.",[37,77754,2005],{"id":2004},[309,77756,77757,77762,77767,77771,77775,77780],{},[24,77758,77759],{},[372,77760,77761],{"href":75455},"Python sorted() function explained",[24,77763,77764],{},[372,77765,77766],{"href":76723},"Python list.sort() method",[24,77768,77769],{},[372,77770,77418],{"href":77417},[24,77772,77773],{},[372,77774,34468],{"href":32073},[24,77776,77777],{},[372,77778,77779],{"href":77583},"IndexError: list index out of range fix explained",[24,77781,77782],{},[372,77783,77785],{"href":77784},"\u002Fexamples\u002Fpython-search-algorithm-example-linear-search\u002F","Python search algorithm example (linear search)",[2034,77787,77788],{},"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 .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 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 .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":47,"searchDepth":66,"depth":66,"links":77790},[77791,77792,77797,77800,77801,77804,77805,77811,77819],{"id":7215,"depth":66,"text":7216},{"id":75786,"depth":66,"text":75787,"children":77793},[77794,77795,77796],{"id":75856,"depth":73,"text":75857},{"id":75906,"depth":73,"text":75907},{"id":75944,"depth":73,"text":75945},{"id":75979,"depth":66,"text":75980,"children":77798},[77799],{"id":9609,"depth":73,"text":9610},{"id":76651,"depth":66,"text":76652},{"id":76729,"depth":66,"text":76730,"children":77802},[77803],{"id":77097,"depth":73,"text":77098},{"id":77375,"depth":66,"text":77376},{"id":12321,"depth":66,"text":12322,"children":77806},[77807,77808,77809,77810],{"id":77427,"depth":73,"text":77428},{"id":77586,"depth":73,"text":77587},{"id":77644,"depth":73,"text":45756},{"id":77671,"depth":73,"text":77672},{"id":1941,"depth":66,"text":1942,"children":77812},[77813,77814,77816,77817],{"id":77717,"depth":73,"text":77718},{"id":77724,"depth":73,"text":77815},"Should I use sorted() instead of bubble sort?",{"id":77736,"depth":73,"text":77737},{"id":77743,"depth":73,"text":77818},"Why do I get list index out of range?",{"id":2004,"depth":66,"text":2005},"Master python sorting algorithm example bubble sort in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-sorting-algorithm-example-bubble-sort",{"title":75435,"description":77820},"examples\u002Fpython-sorting-algorithm-example-bubble-sort","W_Qtoi2ulMW13sNYH_2N33O4LqgasUr4xqBwcM-lBb4",{"id":77827,"title":77828,"body":77829,"description":79295,"extension":2075,"meta":79296,"navigation":69,"path":79297,"seo":79298,"stem":79299,"__hash__":79300},"content\u002Fexamples\u002Fpython-stopwatch-example.md","Python Stopwatch Example",{"type":7,"value":77830,"toc":79258},[77831,77834,77840,77843,77845,77969,77972,77974,77977,77994,77998,78001,78034,78036,78148,78150,78156,78160,78164,78174,78177,78182,78188,78207,78212,78217,78223,78242,78247,78250,78269,78276,78280,78297,78300,78315,78319,78346,78352,78357,78365,78374,78378,78381,78385,78388,78414,78417,78421,78424,78590,78592,78598,78605,78615,78768,78772,78775,78908,78911,78913,78916,78939,78945,78952,78962,78965,78985,78988,79007,79013,79017,79020,79045,79051,79093,79099,79103,79106,79118,79153,79156,79175,79182,79184,79188,79191,79196,79199,79203,79206,79212,79217,79219,79252,79255],[10,77832,77828],{"id":77833},"python-stopwatch-example",[14,77835,77836,77837,77839],{},"Build a simple stopwatch in Python that measures elapsed time. This example uses the ",[49,77838,13181],{}," module and shows, step by step, how to record a start time, record an end time, and calculate the difference.",[14,77841,77842],{},"If you are new to timing code in Python, this is a good first example because it uses only a few lines of code and one standard library module.",[37,77844,40],{"id":39},[42,77846,77848],{"className":44,"code":77847,"language":46,"meta":47,"style":47},"import time\n\ninput(\"Press Enter to start the stopwatch...\")\nstart_time = time.time()\n\ninput(\"Press Enter to stop the stopwatch...\")\nend_time = time.time()\n\nelapsed = end_time - start_time\nprint(f\"Elapsed time: {elapsed:.2f} seconds\")\n",[49,77849,77850,77856,77860,77875,77891,77895,77910,77925,77929,77944],{"__ignoreMap":47},[52,77851,77852,77854],{"class":54,"line":55},[52,77853,59],{"class":58},[52,77855,13019],{"class":62},[52,77857,77858],{"class":54,"line":66},[52,77859,70],{"emptyLinePlaceholder":69},[52,77861,77862,77864,77866,77868,77871,77873],{"class":54,"line":73},[52,77863,13610],{"class":241},[52,77865,193],{"class":102},[52,77867,115],{"class":83},[52,77869,77870],{"class":87},"Press Enter to start the stopwatch...",[52,77872,115],{"class":83},[52,77874,211],{"class":102},[52,77876,77877,77880,77882,77885,77887,77889],{"class":54,"line":94},[52,77878,77879],{"class":62},"start_time ",[52,77881,80],{"class":79},[52,77883,77884],{"class":62}," time",[52,77886,186],{"class":102},[52,77888,13181],{"class":189},[52,77890,230],{"class":102},[52,77892,77893],{"class":54,"line":106},[52,77894,70],{"emptyLinePlaceholder":69},[52,77896,77897,77899,77901,77903,77906,77908],{"class":54,"line":128},[52,77898,13610],{"class":241},[52,77900,193],{"class":102},[52,77902,115],{"class":83},[52,77904,77905],{"class":87},"Press Enter to stop the stopwatch...",[52,77907,115],{"class":83},[52,77909,211],{"class":102},[52,77911,77912,77915,77917,77919,77921,77923],{"class":54,"line":148},[52,77913,77914],{"class":62},"end_time ",[52,77916,80],{"class":79},[52,77918,77884],{"class":62},[52,77920,186],{"class":102},[52,77922,13181],{"class":189},[52,77924,230],{"class":102},[52,77926,77927],{"class":54,"line":164},[52,77928,70],{"emptyLinePlaceholder":69},[52,77930,77931,77934,77936,77939,77941],{"class":54,"line":170},[52,77932,77933],{"class":62},"elapsed ",[52,77935,80],{"class":79},[52,77937,77938],{"class":62}," end_time ",[52,77940,5889],{"class":79},[52,77942,77943],{"class":62}," start_time\n",[52,77945,77946,77948,77950,77952,77955,77957,77960,77962,77964,77967],{"class":54,"line":175},[52,77947,242],{"class":241},[52,77949,193],{"class":102},[52,77951,1476],{"class":1475},[52,77953,77954],{"class":87},"\"Elapsed time: ",[52,77956,1482],{"class":121},[52,77958,77959],{"class":189},"elapsed",[52,77961,35443],{"class":1475},[52,77963,1488],{"class":121},[52,77965,77966],{"class":87}," seconds\"",[52,77968,211],{"class":102},[14,77970,77971],{},"This is the simplest stopwatch version. It starts when you press Enter and stops when you press Enter again.",[37,77973,323],{"id":322},[14,77975,77976],{},"This stopwatch script:",[309,77978,77979,77982,77988,77991],{},[24,77980,77981],{},"Measures how much time passes between two user actions",[24,77983,3609,77984,77987],{},[49,77985,77986],{},"time.time()"," to get the current time in seconds",[24,77989,77990],{},"Calculates elapsed time by subtracting the start time from the end time",[24,77992,77993],{},"Prints the result in a readable format",[37,77995,77997],{"id":77996},"how-the-basic-stopwatch-works","How the basic stopwatch works",[14,77999,78000],{},"The basic idea is simple:",[309,78002,78003,78006,78012,78015,78021,78027],{},[24,78004,78005],{},"Ask the user to press Enter to begin",[24,78007,78008,78009],{},"Store the current time in a variable like ",[49,78010,78011],{},"start_time",[24,78013,78014],{},"Ask the user to press Enter again to stop",[24,78016,78017,78018],{},"Store the current time in ",[49,78019,78020],{},"end_time",[24,78022,13303,78023,28921,78025],{},[49,78024,78011],{},[49,78026,78020],{},[24,78028,78029,78030,78033],{},"Display the result with formatting such as ",[49,78031,78032],{},".2f"," for two decimal places",[14,78035,45415],{},[42,78037,78038],{"className":44,"code":77847,"language":46,"meta":47,"style":47},[49,78039,78040,78046,78050,78064,78078,78082,78096,78110,78114,78126],{"__ignoreMap":47},[52,78041,78042,78044],{"class":54,"line":55},[52,78043,59],{"class":58},[52,78045,13019],{"class":62},[52,78047,78048],{"class":54,"line":66},[52,78049,70],{"emptyLinePlaceholder":69},[52,78051,78052,78054,78056,78058,78060,78062],{"class":54,"line":73},[52,78053,13610],{"class":241},[52,78055,193],{"class":102},[52,78057,115],{"class":83},[52,78059,77870],{"class":87},[52,78061,115],{"class":83},[52,78063,211],{"class":102},[52,78065,78066,78068,78070,78072,78074,78076],{"class":54,"line":94},[52,78067,77879],{"class":62},[52,78069,80],{"class":79},[52,78071,77884],{"class":62},[52,78073,186],{"class":102},[52,78075,13181],{"class":189},[52,78077,230],{"class":102},[52,78079,78080],{"class":54,"line":106},[52,78081,70],{"emptyLinePlaceholder":69},[52,78083,78084,78086,78088,78090,78092,78094],{"class":54,"line":128},[52,78085,13610],{"class":241},[52,78087,193],{"class":102},[52,78089,115],{"class":83},[52,78091,77905],{"class":87},[52,78093,115],{"class":83},[52,78095,211],{"class":102},[52,78097,78098,78100,78102,78104,78106,78108],{"class":54,"line":148},[52,78099,77914],{"class":62},[52,78101,80],{"class":79},[52,78103,77884],{"class":62},[52,78105,186],{"class":102},[52,78107,13181],{"class":189},[52,78109,230],{"class":102},[52,78111,78112],{"class":54,"line":164},[52,78113,70],{"emptyLinePlaceholder":69},[52,78115,78116,78118,78120,78122,78124],{"class":54,"line":170},[52,78117,77933],{"class":62},[52,78119,80],{"class":79},[52,78121,77938],{"class":62},[52,78123,5889],{"class":79},[52,78125,77943],{"class":62},[52,78127,78128,78130,78132,78134,78136,78138,78140,78142,78144,78146],{"class":54,"line":175},[52,78129,242],{"class":241},[52,78131,193],{"class":102},[52,78133,1476],{"class":1475},[52,78135,77954],{"class":87},[52,78137,1482],{"class":121},[52,78139,77959],{"class":189},[52,78141,35443],{"class":1475},[52,78143,1488],{"class":121},[52,78145,77966],{"class":87},[52,78147,211],{"class":102},[14,78149,11845],{},[42,78151,78154],{"className":78152,"code":78153,"language":955,"meta":47},[953],"Press Enter to start the stopwatch...\nPress Enter to stop the stopwatch...\nElapsed time: 3.47 seconds\n",[49,78155,78153],{"__ignoreMap":47},[37,78157,78159],{"id":78158},"code-walkthrough-points","Code walkthrough points",[639,78161,78162],{"id":13318},[49,78163,13321],{},[42,78165,78166],{"className":44,"code":13324,"language":46,"meta":47,"style":47},[49,78167,78168],{"__ignoreMap":47},[52,78169,78170,78172],{"class":54,"line":55},[52,78171,59],{"class":58},[52,78173,13019],{"class":62},[14,78175,78176],{},"This line gives your program access to Python's time-related functions.",[14,78178,78179,78180,186],{},"If you want a broader introduction, see the ",[372,78181,13186],{"href":13185},[639,78183,78185,78187],{"id":78184},"input-pauses-the-program",[49,78186,10597],{}," pauses the program",[42,78189,78191],{"className":44,"code":78190,"language":46,"meta":47,"style":47},"input(\"Press Enter to start the stopwatch...\")\n",[49,78192,78193],{"__ignoreMap":47},[52,78194,78195,78197,78199,78201,78203,78205],{"class":54,"line":55},[52,78196,13610],{"class":241},[52,78198,193],{"class":102},[52,78200,115],{"class":83},[52,78202,77870],{"class":87},[52,78204,115],{"class":83},[52,78206,211],{"class":102},[14,78208,358,78209,78211],{},[49,78210,10597],{}," function waits until the user presses Enter. That makes it useful for a simple start-and-stop stopwatch.",[14,78213,78214,78215,186],{},"If you need help with this part, see ",[372,78216,6398],{"href":6397},[639,78218,78220,78222],{"id":78219},"timetime-gets-the-current-time",[49,78221,77986],{}," gets the current time",[42,78224,78226],{"className":44,"code":78225,"language":46,"meta":47,"style":47},"start_time = time.time()\n",[49,78227,78228],{"__ignoreMap":47},[52,78229,78230,78232,78234,78236,78238,78240],{"class":54,"line":55},[52,78231,77879],{"class":62},[52,78233,80],{"class":79},[52,78235,77884],{"class":62},[52,78237,186],{"class":102},[52,78239,13181],{"class":189},[52,78241,230],{"class":102},[14,78243,78244,78246],{},[49,78245,77986],{}," returns the current Unix timestamp as a floating-point number. That means the value includes fractions of a second.",[14,78248,78249],{},"Later, the script does the same thing again:",[42,78251,78253],{"className":44,"code":78252,"language":46,"meta":47,"style":47},"end_time = time.time()\n",[49,78254,78255],{"__ignoreMap":47},[52,78256,78257,78259,78261,78263,78265,78267],{"class":54,"line":55},[52,78258,77914],{"class":62},[52,78260,80],{"class":79},[52,78262,77884],{"class":62},[52,78264,186],{"class":102},[52,78266,13181],{"class":189},[52,78268,230],{"class":102},[14,78270,78271,78272,186],{},"You can learn more here: ",[372,78273,78275],{"href":78274},"\u002Fstandard-library\u002Ftime.time-function-explained","time.time() explained",[639,78277,78279],{"id":78278},"subtracting-timestamps-gives-elapsed-time","Subtracting timestamps gives elapsed time",[42,78281,78283],{"className":44,"code":78282,"language":46,"meta":47,"style":47},"elapsed = end_time - start_time\n",[49,78284,78285],{"__ignoreMap":47},[52,78286,78287,78289,78291,78293,78295],{"class":54,"line":55},[52,78288,77933],{"class":62},[52,78290,80],{"class":79},[52,78292,77938],{"class":62},[52,78294,5889],{"class":79},[52,78296,77943],{"class":62},[14,78298,78299],{},"This is the key step.",[309,78301,78302,78307,78312],{},[24,78303,78304,78306],{},[49,78305,78011],{}," is the moment the stopwatch began",[24,78308,78309,78311],{},[49,78310,78020],{}," is the moment it stopped",[24,78313,78314],{},"The difference is the elapsed time in seconds",[639,78316,78318],{"id":78317},"f-strings-make-output-easier-to-read","f-strings make output easier to read",[42,78320,78322],{"className":44,"code":78321,"language":46,"meta":47,"style":47},"print(f\"Elapsed time: {elapsed:.2f} seconds\")\n",[49,78323,78324],{"__ignoreMap":47},[52,78325,78326,78328,78330,78332,78334,78336,78338,78340,78342,78344],{"class":54,"line":55},[52,78327,242],{"class":241},[52,78329,193],{"class":102},[52,78331,1476],{"class":1475},[52,78333,77954],{"class":87},[52,78335,1482],{"class":121},[52,78337,77959],{"class":189},[52,78339,35443],{"class":1475},[52,78341,1488],{"class":121},[52,78343,77966],{"class":87},[52,78345,211],{"class":102},[14,78347,78348,78349,78351],{},"This uses an f-string to insert the value of ",[49,78350,77959],{}," into the text.",[14,78353,358,78354,78356],{},[49,78355,78032],{}," part means:",[309,78358,78359,78362],{},[24,78360,78361],{},"show the number as a floating-point value",[24,78363,78364],{},"keep 2 digits after the decimal point",[14,78366,78367,78368,6230,78371,186],{},"So a value like ",[49,78369,78370],{},"3.472891",[49,78372,78373],{},"3.47",[37,78375,78377],{"id":78376},"small-improvements-to-show","Small improvements to show",[14,78379,78380],{},"Once the basic version works, you can improve it in a few useful ways.",[639,78382,78384],{"id":78383},"round-the-output-to-2-decimal-places","Round the output to 2 decimal places",[14,78386,78387],{},"The first example already does this with:",[42,78389,78390],{"className":44,"code":78321,"language":46,"meta":47,"style":47},[49,78391,78392],{"__ignoreMap":47},[52,78393,78394,78396,78398,78400,78402,78404,78406,78408,78410,78412],{"class":54,"line":55},[52,78395,242],{"class":241},[52,78397,193],{"class":102},[52,78399,1476],{"class":1475},[52,78401,77954],{"class":87},[52,78403,1482],{"class":121},[52,78405,77959],{"class":189},[52,78407,35443],{"class":1475},[52,78409,1488],{"class":121},[52,78411,77966],{"class":87},[52,78413,211],{"class":102},[14,78415,78416],{},"Without formatting, the result may show too many decimal places.",[639,78418,78420],{"id":78419},"show-minutes-and-seconds","Show minutes and seconds",[14,78422,78423],{},"For longer times, minutes and seconds are easier to read than a decimal number.",[42,78425,78427],{"className":44,"code":78426,"language":46,"meta":47,"style":47},"import time\n\ninput(\"Press Enter to start the stopwatch...\")\nstart_time = time.time()\n\ninput(\"Press Enter to stop the stopwatch...\")\nend_time = time.time()\n\nelapsed = end_time - start_time\n\nminutes = int(elapsed \u002F\u002F 60)\nseconds = elapsed % 60\n\nprint(f\"Elapsed time: {minutes} minute(s) and {seconds:.2f} second(s)\")\n",[49,78428,78429,78435,78439,78453,78467,78471,78485,78499,78503,78515,78519,78539,78553,78557],{"__ignoreMap":47},[52,78430,78431,78433],{"class":54,"line":55},[52,78432,59],{"class":58},[52,78434,13019],{"class":62},[52,78436,78437],{"class":54,"line":66},[52,78438,70],{"emptyLinePlaceholder":69},[52,78440,78441,78443,78445,78447,78449,78451],{"class":54,"line":73},[52,78442,13610],{"class":241},[52,78444,193],{"class":102},[52,78446,115],{"class":83},[52,78448,77870],{"class":87},[52,78450,115],{"class":83},[52,78452,211],{"class":102},[52,78454,78455,78457,78459,78461,78463,78465],{"class":54,"line":94},[52,78456,77879],{"class":62},[52,78458,80],{"class":79},[52,78460,77884],{"class":62},[52,78462,186],{"class":102},[52,78464,13181],{"class":189},[52,78466,230],{"class":102},[52,78468,78469],{"class":54,"line":106},[52,78470,70],{"emptyLinePlaceholder":69},[52,78472,78473,78475,78477,78479,78481,78483],{"class":54,"line":128},[52,78474,13610],{"class":241},[52,78476,193],{"class":102},[52,78478,115],{"class":83},[52,78480,77905],{"class":87},[52,78482,115],{"class":83},[52,78484,211],{"class":102},[52,78486,78487,78489,78491,78493,78495,78497],{"class":54,"line":148},[52,78488,77914],{"class":62},[52,78490,80],{"class":79},[52,78492,77884],{"class":62},[52,78494,186],{"class":102},[52,78496,13181],{"class":189},[52,78498,230],{"class":102},[52,78500,78501],{"class":54,"line":164},[52,78502,70],{"emptyLinePlaceholder":69},[52,78504,78505,78507,78509,78511,78513],{"class":54,"line":170},[52,78506,77933],{"class":62},[52,78508,80],{"class":79},[52,78510,77938],{"class":62},[52,78512,5889],{"class":79},[52,78514,77943],{"class":62},[52,78516,78517],{"class":54,"line":175},[52,78518,70],{"emptyLinePlaceholder":69},[52,78520,78521,78524,78526,78528,78530,78532,78534,78537],{"class":54,"line":214},[52,78522,78523],{"class":62},"minutes ",[52,78525,80],{"class":79},[52,78527,9789],{"class":4733},[52,78529,193],{"class":102},[52,78531,77933],{"class":189},[52,78533,74947],{"class":79},[52,78535,78536],{"class":121}," 60",[52,78538,211],{"class":102},[52,78540,78541,78543,78545,78548,78550],{"class":54,"line":233},[52,78542,13028],{"class":62},[52,78544,80],{"class":79},[52,78546,78547],{"class":62}," elapsed ",[52,78549,32151],{"class":79},[52,78551,78552],{"class":121}," 60\n",[52,78554,78555],{"class":54,"line":238},[52,78556,70],{"emptyLinePlaceholder":69},[52,78558,78559,78561,78563,78565,78567,78569,78572,78574,78577,78579,78581,78583,78585,78588],{"class":54,"line":272},[52,78560,242],{"class":241},[52,78562,193],{"class":102},[52,78564,1476],{"class":1475},[52,78566,77954],{"class":87},[52,78568,1482],{"class":121},[52,78570,78571],{"class":189},"minutes",[52,78573,1488],{"class":121},[52,78575,78576],{"class":87}," minute(s) and ",[52,78578,1482],{"class":121},[52,78580,13060],{"class":189},[52,78582,35443],{"class":1475},[52,78584,1488],{"class":121},[52,78586,78587],{"class":87}," second(s)\"",[52,78589,211],{"class":102},[14,78591,11845],{},[42,78593,78596],{"className":78594,"code":78595,"language":955,"meta":47},[953],"Elapsed time: 1 minute(s) and 12.35 second(s)\n",[49,78597,78595],{"__ignoreMap":47},[639,78599,78601,78602],{"id":78600},"handle-keyboardinterrupt","Handle ",[49,78603,78604],{},"KeyboardInterrupt",[14,78606,78607,78608,78611,78612,78614],{},"If the user stops the program with ",[49,78609,78610],{},"Ctrl+C",", Python raises a ",[49,78613,78604],{},". You can handle that so the program exits more cleanly.",[42,78616,78618],{"className":44,"code":78617,"language":46,"meta":47,"style":47},"import time\n\ntry:\n    input(\"Press Enter to start the stopwatch...\")\n    start_time = time.time()\n\n    input(\"Press Enter to stop the stopwatch...\")\n    end_time = time.time()\n\n    elapsed = end_time - start_time\n    print(f\"Elapsed time: {elapsed:.2f} seconds\")\n\nexcept KeyboardInterrupt:\n    print(\"\\nStopwatch cancelled.\")\n",[49,78619,78620,78626,78630,78636,78651,78666,78670,78684,78699,78703,78716,78738,78742,78751],{"__ignoreMap":47},[52,78621,78622,78624],{"class":54,"line":55},[52,78623,59],{"class":58},[52,78625,13019],{"class":62},[52,78627,78628],{"class":54,"line":66},[52,78629,70],{"emptyLinePlaceholder":69},[52,78631,78632,78634],{"class":54,"line":73},[52,78633,3336],{"class":58},[52,78635,1330],{"class":102},[52,78637,78638,78641,78643,78645,78647,78649],{"class":54,"line":94},[52,78639,78640],{"class":241},"    input",[52,78642,193],{"class":102},[52,78644,115],{"class":83},[52,78646,77870],{"class":87},[52,78648,115],{"class":83},[52,78650,211],{"class":102},[52,78652,78653,78656,78658,78660,78662,78664],{"class":54,"line":106},[52,78654,78655],{"class":62},"    start_time ",[52,78657,80],{"class":79},[52,78659,77884],{"class":62},[52,78661,186],{"class":102},[52,78663,13181],{"class":189},[52,78665,230],{"class":102},[52,78667,78668],{"class":54,"line":128},[52,78669,70],{"emptyLinePlaceholder":69},[52,78671,78672,78674,78676,78678,78680,78682],{"class":54,"line":148},[52,78673,78640],{"class":241},[52,78675,193],{"class":102},[52,78677,115],{"class":83},[52,78679,77905],{"class":87},[52,78681,115],{"class":83},[52,78683,211],{"class":102},[52,78685,78686,78689,78691,78693,78695,78697],{"class":54,"line":164},[52,78687,78688],{"class":62},"    end_time ",[52,78690,80],{"class":79},[52,78692,77884],{"class":62},[52,78694,186],{"class":102},[52,78696,13181],{"class":189},[52,78698,230],{"class":102},[52,78700,78701],{"class":54,"line":170},[52,78702,70],{"emptyLinePlaceholder":69},[52,78704,78705,78708,78710,78712,78714],{"class":54,"line":175},[52,78706,78707],{"class":62},"    elapsed ",[52,78709,80],{"class":79},[52,78711,77938],{"class":62},[52,78713,5889],{"class":79},[52,78715,77943],{"class":62},[52,78717,78718,78720,78722,78724,78726,78728,78730,78732,78734,78736],{"class":54,"line":214},[52,78719,1599],{"class":241},[52,78721,193],{"class":102},[52,78723,1476],{"class":1475},[52,78725,77954],{"class":87},[52,78727,1482],{"class":121},[52,78729,77959],{"class":189},[52,78731,35443],{"class":1475},[52,78733,1488],{"class":121},[52,78735,77966],{"class":87},[52,78737,211],{"class":102},[52,78739,78740],{"class":54,"line":233},[52,78741,70],{"emptyLinePlaceholder":69},[52,78743,78744,78746,78749],{"class":54,"line":238},[52,78745,3552],{"class":58},[52,78747,78748],{"class":4733}," KeyboardInterrupt",[52,78750,1330],{"class":102},[52,78752,78753,78755,78757,78759,78761,78764,78766],{"class":54,"line":272},[52,78754,1599],{"class":241},[52,78756,193],{"class":102},[52,78758,115],{"class":83},[52,78760,10956],{"class":2533},[52,78762,78763],{"class":87},"Stopwatch cancelled.",[52,78765,115],{"class":83},[52,78767,211],{"class":102},[639,78769,78771],{"id":78770},"wrap-the-logic-in-a-function","Wrap the logic in a function",[14,78773,78774],{},"Putting the code in a function makes it easier to reuse later.",[42,78776,78778],{"className":44,"code":78777,"language":46,"meta":47,"style":47},"import time\n\ndef run_stopwatch():\n    input(\"Press Enter to start the stopwatch...\")\n    start_time = time.time()\n\n    input(\"Press Enter to stop the stopwatch...\")\n    end_time = time.time()\n\n    elapsed = end_time - start_time\n    print(f\"Elapsed time: {elapsed:.2f} seconds\")\n\nrun_stopwatch()\n",[49,78779,78780,78786,78790,78799,78813,78827,78831,78845,78859,78863,78875,78897,78901],{"__ignoreMap":47},[52,78781,78782,78784],{"class":54,"line":55},[52,78783,59],{"class":58},[52,78785,13019],{"class":62},[52,78787,78788],{"class":54,"line":66},[52,78789,70],{"emptyLinePlaceholder":69},[52,78791,78792,78794,78797],{"class":54,"line":73},[52,78793,7410],{"class":1475},[52,78795,78796],{"class":7065}," run_stopwatch",[52,78798,8146],{"class":102},[52,78800,78801,78803,78805,78807,78809,78811],{"class":54,"line":94},[52,78802,78640],{"class":241},[52,78804,193],{"class":102},[52,78806,115],{"class":83},[52,78808,77870],{"class":87},[52,78810,115],{"class":83},[52,78812,211],{"class":102},[52,78814,78815,78817,78819,78821,78823,78825],{"class":54,"line":106},[52,78816,78655],{"class":62},[52,78818,80],{"class":79},[52,78820,77884],{"class":62},[52,78822,186],{"class":102},[52,78824,13181],{"class":189},[52,78826,230],{"class":102},[52,78828,78829],{"class":54,"line":128},[52,78830,70],{"emptyLinePlaceholder":69},[52,78832,78833,78835,78837,78839,78841,78843],{"class":54,"line":148},[52,78834,78640],{"class":241},[52,78836,193],{"class":102},[52,78838,115],{"class":83},[52,78840,77905],{"class":87},[52,78842,115],{"class":83},[52,78844,211],{"class":102},[52,78846,78847,78849,78851,78853,78855,78857],{"class":54,"line":164},[52,78848,78688],{"class":62},[52,78850,80],{"class":79},[52,78852,77884],{"class":62},[52,78854,186],{"class":102},[52,78856,13181],{"class":189},[52,78858,230],{"class":102},[52,78860,78861],{"class":54,"line":170},[52,78862,70],{"emptyLinePlaceholder":69},[52,78864,78865,78867,78869,78871,78873],{"class":54,"line":175},[52,78866,78707],{"class":62},[52,78868,80],{"class":79},[52,78870,77938],{"class":62},[52,78872,5889],{"class":79},[52,78874,77943],{"class":62},[52,78876,78877,78879,78881,78883,78885,78887,78889,78891,78893,78895],{"class":54,"line":214},[52,78878,1599],{"class":241},[52,78880,193],{"class":102},[52,78882,1476],{"class":1475},[52,78884,77954],{"class":87},[52,78886,1482],{"class":121},[52,78888,77959],{"class":189},[52,78890,35443],{"class":1475},[52,78892,1488],{"class":121},[52,78894,77966],{"class":87},[52,78896,211],{"class":102},[52,78898,78899],{"class":54,"line":233},[52,78900,70],{"emptyLinePlaceholder":69},[52,78902,78903,78906],{"class":54,"line":238},[52,78904,78905],{"class":189},"run_stopwatch",[52,78907,230],{"class":102},[14,78909,78910],{},"This is a good step if you want to turn the stopwatch into a larger program.",[37,78912,45733],{"id":45732},[14,78914,78915],{},"Common beginner mistakes with this example include:",[309,78917,78918,78923,78930,78933],{},[24,78919,78920,78921,14928],{},"Forgetting to import the ",[49,78922,13181],{},[24,78924,10294,78925,10598,78927,78929],{},[49,78926,13147],{},[49,78928,77986],{}," for measuring elapsed time",[24,78931,78932],{},"Trying to subtract strings instead of numeric time values",[24,78934,78935,78936,78938],{},"Overwriting variable names like ",[49,78937,13181],{}," with your own variable",[639,78940,78942,78944],{"id":78941},"timesleep-is-not-the-same-thing",[49,78943,13147],{}," is not the same thing",[14,78946,78947,78948,759,78950,186],{},"A very common confusion is mixing up ",[49,78949,13147],{},[49,78951,77986],{},[309,78953,78954,78958],{},[24,78955,78956,78222],{},[49,78957,77986],{},[24,78959,78960,78187],{},[49,78961,13147],{},[14,78963,78964],{},"So this is for measurement:",[42,78966,78968],{"className":44,"code":78967,"language":46,"meta":47,"style":47},"now = time.time()\n",[49,78969,78970],{"__ignoreMap":47},[52,78971,78972,78975,78977,78979,78981,78983],{"class":54,"line":55},[52,78973,78974],{"class":62},"now ",[52,78976,80],{"class":79},[52,78978,77884],{"class":62},[52,78980,186],{"class":102},[52,78982,13181],{"class":189},[52,78984,230],{"class":102},[14,78986,78987],{},"And this is for pausing:",[42,78989,78991],{"className":44,"code":78990,"language":46,"meta":47,"style":47},"time.sleep(2)\n",[49,78992,78993],{"__ignoreMap":47},[52,78994,78995,78997,78999,79001,79003,79005],{"class":54,"line":55},[52,78996,13181],{"class":62},[52,78998,186],{"class":102},[52,79000,13072],{"class":189},[52,79002,193],{"class":102},[52,79004,9825],{"class":121},[52,79006,211],{"class":102},[14,79008,79009,79010,186],{},"If you want to understand the difference better, see ",[372,79011,79012],{"href":13144},"time.sleep() explained",[37,79014,79016],{"id":79015},"common-mistakes-and-debugging","Common mistakes and debugging",[14,79018,79019],{},"Here are some common causes of problems:",[309,79021,79022,79031,79039,79042],{},[24,79023,79024,4812,79026,79028,79029],{},[49,79025,14080],{},[49,79027,13181],{}," is used without ",[49,79030,13321],{},[24,79032,79033,79035,79036,79038],{},[49,79034,37684],{}," if a variable named ",[49,79037,13181],{}," replaces the module name",[24,79040,79041],{},"Confusion between pausing code and measuring time",[24,79043,79044],{},"Incorrect indentation when placing code inside a function or loop",[14,79046,79047,79048,79050],{},"For example, this causes a problem because ",[49,79049,13181],{}," is no longer the module:",[42,79052,79054],{"className":44,"code":79053,"language":46,"meta":47,"style":47},"import time\n\ntime = \"hello\"\nprint(time.time())\n",[49,79055,79056,79062,79066,79079],{"__ignoreMap":47},[52,79057,79058,79060],{"class":54,"line":55},[52,79059,59],{"class":58},[52,79061,13019],{"class":62},[52,79063,79064],{"class":54,"line":66},[52,79065,70],{"emptyLinePlaceholder":69},[52,79067,79068,79071,79073,79075,79077],{"class":54,"line":73},[52,79069,79070],{"class":62},"time ",[52,79072,80],{"class":79},[52,79074,84],{"class":83},[52,79076,6363],{"class":87},[52,79078,91],{"class":83},[52,79080,79081,79083,79085,79087,79089,79091],{"class":54,"line":94},[52,79082,242],{"class":241},[52,79084,193],{"class":102},[52,79086,13181],{"class":189},[52,79088,186],{"class":102},[52,79090,13181],{"class":189},[52,79092,1816],{"class":102},[14,79094,79095,79096,79098],{},"Python will fail because ",[49,79097,13181],{}," now refers to a string, not the module.",[639,79100,79102],{"id":79101},"helpful-debugging-commands","Helpful debugging commands",[14,79104,79105],{},"You can use these commands or print statements while testing:",[42,79107,79109],{"className":390,"code":79108,"language":392,"meta":47,"style":47},"python stopwatch.py\n",[49,79110,79111],{"__ignoreMap":47},[52,79112,79113,79115],{"class":54,"line":55},[52,79114,46],{"class":399},[52,79116,79117],{"class":87}," stopwatch.py\n",[42,79119,79121],{"className":44,"code":79120,"language":46,"meta":47,"style":47},"print(start_time)\nprint(end_time)\nprint(elapsed)\n",[49,79122,79123,79133,79143],{"__ignoreMap":47},[52,79124,79125,79127,79129,79131],{"class":54,"line":55},[52,79126,242],{"class":241},[52,79128,193],{"class":102},[52,79130,78011],{"class":189},[52,79132,211],{"class":102},[52,79134,79135,79137,79139,79141],{"class":54,"line":66},[52,79136,242],{"class":241},[52,79138,193],{"class":102},[52,79140,78020],{"class":189},[52,79142,211],{"class":102},[52,79144,79145,79147,79149,79151],{"class":54,"line":73},[52,79146,242],{"class":241},[52,79148,193],{"class":102},[52,79150,77959],{"class":189},[52,79152,211],{"class":102},[14,79154,79155],{},"You can also trace the script step by step:",[42,79157,79159],{"className":390,"code":79158,"language":392,"meta":47,"style":47},"python -m trace --trace stopwatch.py\n",[49,79160,79161],{"__ignoreMap":47},[52,79162,79163,79165,79167,79170,79173],{"class":54,"line":55},[52,79164,46],{"class":399},[52,79166,421],{"class":420},[52,79168,79169],{"class":87}," trace",[52,79171,79172],{"class":420}," --trace",[52,79174,79117],{"class":87},[14,79176,79177,79178,186],{},"If you are new to debugging, see the ",[372,79179,79181],{"href":79180},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide\u002F","beginner guide to debugging Python code",[37,79183,1942],{"id":1941},[639,79185,79187],{"id":79186},"is-this-a-real-stopwatch-that-updates-on-the-screen","Is this a real stopwatch that updates on the screen?",[14,79189,79190],{},"No. This basic version only measures the time between start and stop. A live updating stopwatch would need a loop and repeated output.",[639,79192,1956,79194,47117],{"id":79193},"why-use-timetime-here",[49,79195,77986],{},[14,79197,79198],{},"It returns the current time in seconds, so you can subtract two values to find elapsed time.",[639,79200,79202],{"id":79201},"can-i-show-minutes-and-seconds-instead-of-decimal-seconds","Can I show minutes and seconds instead of decimal seconds?",[14,79204,79205],{},"Yes. You can divide the elapsed time into minutes and remaining seconds before printing.",[639,79207,79209,79210,1995],{"id":79208},"what-is-the-difference-between-this-and-timesleep","What is the difference between this and ",[49,79211,13147],{},[14,79213,79214,79216],{},[49,79215,13147],{}," pauses the program. It does not calculate elapsed time for you.",[37,79218,2005],{"id":2004},[309,79220,79221,79225,79229,79233,79237,79242,79248],{},[24,79222,79223],{},[372,79224,13186],{"href":13185},[24,79226,79227],{},[372,79228,78275],{"href":78274},[24,79230,79231],{},[372,79232,79012],{"href":13144},[24,79234,79235],{},[372,79236,6876],{"href":6397},[24,79238,79239],{},[372,79240,79241],{"href":79180},"How to debug Python code",[24,79243,79244],{},[372,79245,79247],{"href":79246},"\u002Fexamples\u002Fpython-countdown-timer-example\u002F","Python countdown timer example",[24,79249,79250],{},[372,79251,14437],{"href":14436},[14,79253,79254],{},"Try improving this basic stopwatch next by adding a live display, formatting the result as minutes and seconds, or turning the code into a reusable function.",[2034,79256,79257],{},"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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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":47,"searchDepth":66,"depth":66,"links":79259},[79260,79261,79262,79263,79272,79279,79283,79286,79294],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":77996,"depth":66,"text":77997},{"id":78158,"depth":66,"text":78159,"children":79264},[79265,79266,79268,79270,79271],{"id":13318,"depth":73,"text":13321},{"id":78184,"depth":73,"text":79267},"input() pauses the program",{"id":78219,"depth":73,"text":79269},"time.time() gets the current time",{"id":78278,"depth":73,"text":78279},{"id":78317,"depth":73,"text":78318},{"id":78376,"depth":66,"text":78377,"children":79273},[79274,79275,79276,79278],{"id":78383,"depth":73,"text":78384},{"id":78419,"depth":73,"text":78420},{"id":78600,"depth":73,"text":79277},"Handle KeyboardInterrupt",{"id":78770,"depth":73,"text":78771},{"id":45732,"depth":66,"text":45733,"children":79280},[79281],{"id":78941,"depth":73,"text":79282},"time.sleep() is not the same thing",{"id":79015,"depth":66,"text":79016,"children":79284},[79285],{"id":79101,"depth":73,"text":79102},{"id":1941,"depth":66,"text":1942,"children":79287},[79288,79289,79291,79292],{"id":79186,"depth":73,"text":79187},{"id":79193,"depth":73,"text":79290},"Why use time.time() here?",{"id":79201,"depth":73,"text":79202},{"id":79208,"depth":73,"text":79293},"What is the difference between this and time.sleep()?",{"id":2004,"depth":66,"text":2005},"Master python stopwatch example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-stopwatch-example",{"title":77828,"description":79295},"examples\u002Fpython-stopwatch-example","LgyOn4eNHBXKarEn5XgUtwE_282is-ZBzarCGodMsYw",{"id":79302,"title":79303,"body":79304,"description":81066,"extension":2075,"meta":81067,"navigation":69,"path":81068,"seo":81069,"stem":81070,"__hash__":81071},"content\u002Fexamples\u002Fpython-text-analysis-script-example.md","Python Text Analysis Script Example",{"type":7,"value":79305,"toc":81038},[79306,79309,79312,79315,79329,79332,79334,79337,79551,79553,79556,79558,79572,79577,79581,79584,79609,79614,79618,79621,79768,79770,79802,79811,79813,79847,79850,79854,79857,80025,80029,80033,80048,80056,80060,80091,80100,80104,80123,80126,80130,80143,80146,80150,80199,80202,80220,80223,80379,80383,80386,80389,80409,80412,80635,80638,80684,80689,80692,80695,80709,80711,80811,80813,80816,80818,80881,80884,80905,80908,80925,80930,80932,80935,80957,80960,80962,80966,80972,80978,80985,80989,80992,80998,81001,81003,81032,81035],[10,79307,79303],{"id":79308},"python-text-analysis-script-example",[14,79310,79311],{},"This beginner-friendly example shows how to analyze text in Python.",[14,79313,79314],{},"You will build a small script that:",[309,79316,79317,79320,79323,79326],{},[24,79318,79319],{},"Counts characters",[24,79321,79322],{},"Counts words",[24,79324,79325],{},"Counts lines",[24,79327,79328],{},"Counts how often each word appears",[14,79330,79331],{},"It is a good practice project because it uses basic Python tools in a real script: strings, loops, dictionaries, and printing results clearly.",[37,79333,40],{"id":39},[14,79335,79336],{},"Use this small script if you want a fast example of basic text analysis without reading from a file.",[42,79338,79340],{"className":44,"code":79339,"language":46,"meta":47,"style":47},"text = \"Python is simple. Python is useful.\"\n\nwords = text.lower().replace(\".\", \"\").split()\nprint(\"Characters:\", len(text))\nprint(\"Words:\", len(words))\nprint(\"Lines:\", len(text.splitlines()))\n\ncounts = {}\nfor word in words:\n    counts[word] = counts.get(word, 0) + 1\n\nprint(\"Word counts:\", counts)\n",[49,79341,79342,79356,79360,79395,79418,79441,79469,79473,79481,79494,79528,79532],{"__ignoreMap":47},[52,79343,79344,79347,79349,79351,79354],{"class":54,"line":55},[52,79345,79346],{"class":62},"text ",[52,79348,80],{"class":79},[52,79350,84],{"class":83},[52,79352,79353],{"class":87},"Python is simple. Python is useful.",[52,79355,91],{"class":83},[52,79357,79358],{"class":54,"line":66},[52,79359,70],{"emptyLinePlaceholder":69},[52,79361,79362,79364,79366,79368,79370,79372,79374,79377,79379,79381,79383,79385,79387,79389,79391,79393],{"class":54,"line":73},[52,79363,32960],{"class":62},[52,79365,80],{"class":79},[52,79367,56252],{"class":62},[52,79369,186],{"class":102},[52,79371,30237],{"class":189},[52,79373,18679],{"class":102},[52,79375,79376],{"class":189},"replace",[52,79378,193],{"class":102},[52,79380,115],{"class":83},[52,79382,186],{"class":87},[52,79384,115],{"class":83},[52,79386,199],{"class":102},[52,79388,3448],{"class":83},[52,79390,54653],{"class":102},[52,79392,58133],{"class":189},[52,79394,230],{"class":102},[52,79396,79397,79399,79401,79403,79406,79408,79410,79412,79414,79416],{"class":54,"line":94},[52,79398,242],{"class":241},[52,79400,193],{"class":102},[52,79402,115],{"class":83},[52,79404,79405],{"class":87},"Characters:",[52,79407,115],{"class":83},[52,79409,199],{"class":102},[52,79411,9150],{"class":241},[52,79413,193],{"class":102},[52,79415,955],{"class":189},[52,79417,8886],{"class":102},[52,79419,79420,79422,79424,79426,79429,79431,79433,79435,79437,79439],{"class":54,"line":106},[52,79421,242],{"class":241},[52,79423,193],{"class":102},[52,79425,115],{"class":83},[52,79427,79428],{"class":87},"Words:",[52,79430,115],{"class":83},[52,79432,199],{"class":102},[52,79434,9150],{"class":241},[52,79436,193],{"class":102},[52,79438,67032],{"class":189},[52,79440,8886],{"class":102},[52,79442,79443,79445,79447,79449,79452,79454,79456,79458,79460,79462,79464,79467],{"class":54,"line":128},[52,79444,242],{"class":241},[52,79446,193],{"class":102},[52,79448,115],{"class":83},[52,79450,79451],{"class":87},"Lines:",[52,79453,115],{"class":83},[52,79455,199],{"class":102},[52,79457,9150],{"class":241},[52,79459,193],{"class":102},[52,79461,955],{"class":189},[52,79463,186],{"class":102},[52,79465,79466],{"class":189},"splitlines",[52,79468,62172],{"class":102},[52,79470,79471],{"class":54,"line":148},[52,79472,70],{"emptyLinePlaceholder":69},[52,79474,79475,79477,79479],{"class":54,"line":164},[52,79476,46151],{"class":62},[52,79478,80],{"class":79},[52,79480,10932],{"class":102},[52,79482,79483,79485,79487,79489,79492],{"class":54,"line":170},[52,79484,12265],{"class":58},[52,79486,33016],{"class":62},[52,79488,11213],{"class":58},[52,79490,79491],{"class":62}," words",[52,79493,1330],{"class":102},[52,79495,79496,79499,79501,79504,79506,79508,79510,79512,79514,79516,79518,79520,79522,79524,79526],{"class":54,"line":175},[52,79497,79498],{"class":62},"    counts",[52,79500,250],{"class":102},[52,79502,79503],{"class":62},"word",[52,79505,11122],{"class":102},[52,79507,7038],{"class":79},[52,79509,46370],{"class":62},[52,79511,186],{"class":102},[52,79513,190],{"class":189},[52,79515,193],{"class":102},[52,79517,79503],{"class":189},[52,79519,199],{"class":102},[52,79521,13049],{"class":121},[52,79523,1521],{"class":102},[52,79525,5234],{"class":79},[52,79527,8645],{"class":121},[52,79529,79530],{"class":54,"line":214},[52,79531,70],{"emptyLinePlaceholder":69},[52,79533,79534,79536,79538,79540,79543,79545,79547,79549],{"class":54,"line":233},[52,79535,242],{"class":241},[52,79537,193],{"class":102},[52,79539,115],{"class":83},[52,79541,79542],{"class":87},"Word counts:",[52,79544,115],{"class":83},[52,79546,199],{"class":102},[52,79548,46370],{"class":189},[52,79550,211],{"class":102},[37,79552,28680],{"id":28679},[14,79554,79555],{},"This example is useful because it stays small, but still teaches real Python skills.",[14,79557,9301],{},[309,79559,79560,79563,79566,79569],{},[24,79561,79562],{},"Use a string as input text",[24,79564,79565],{},"Analyze text by counting characters, words, and lines",[24,79567,79568],{},"Count repeated words with a dictionary",[24,79570,79571],{},"Build a simple script that you can improve later",[14,79573,79574,79575,186],{},"If you are new to strings, see ",[372,79576,38969],{"href":38968},[37,79578,79580],{"id":79579},"skills-you-practice","Skills you practice",[14,79582,79583],{},"By working through this example, you practice:",[309,79585,79586,79589,79598,79603,79606],{},[24,79587,79588],{},"Working with strings",[24,79590,10294,79591,79597],{},[372,79592,79594],{"href":79593},"\u002Freference\u002Fpython-string-split-method\u002F",[49,79595,79596],{},"split()"," to break text into words",[24,79599,79600,79601],{},"Looping through data with ",[49,79602,12265],{},[24,79604,79605],{},"Storing counts in a dictionary",[24,79607,79608],{},"Printing readable results",[14,79610,79611,79612,186],{},"If dictionaries are new to you, read ",[372,79613,10883],{"href":10882},[37,79615,79617],{"id":79616},"basic-version-count-lines-words-and-characters","Basic version: count lines, words, and characters",[14,79619,79620],{},"Start with a simple script that counts the text size in different ways.",[42,79622,79624],{"className":44,"code":79623,"language":46,"meta":47,"style":47},"text = \"\"\"Python is simple.\nPython is useful.\nPython is fun to learn.\"\"\"\n\ncharacter_count = len(text)\nword_count = len(text.split())\nline_count = len(text.splitlines())\n\nprint(\"Characters:\", character_count)\nprint(\"Words:\", word_count)\nprint(\"Lines:\", line_count)\n",[49,79625,79626,79638,79643,79650,79654,79669,79688,79707,79711,79730,79749],{"__ignoreMap":47},[52,79627,79628,79630,79632,79635],{"class":54,"line":55},[52,79629,79346],{"class":62},[52,79631,80],{"class":79},[52,79633,79634],{"class":83}," \"\"\"",[52,79636,79637],{"class":87},"Python is simple.\n",[52,79639,79640],{"class":54,"line":66},[52,79641,79642],{"class":87},"Python is useful.\n",[52,79644,79645,79648],{"class":54,"line":73},[52,79646,79647],{"class":87},"Python is fun to learn.",[52,79649,40687],{"class":83},[52,79651,79652],{"class":54,"line":94},[52,79653,70],{"emptyLinePlaceholder":69},[52,79655,79656,79659,79661,79663,79665,79667],{"class":54,"line":106},[52,79657,79658],{"class":62},"character_count ",[52,79660,80],{"class":79},[52,79662,9150],{"class":241},[52,79664,193],{"class":102},[52,79666,955],{"class":189},[52,79668,211],{"class":102},[52,79670,79671,79674,79676,79678,79680,79682,79684,79686],{"class":54,"line":128},[52,79672,79673],{"class":62},"word_count ",[52,79675,80],{"class":79},[52,79677,9150],{"class":241},[52,79679,193],{"class":102},[52,79681,955],{"class":189},[52,79683,186],{"class":102},[52,79685,58133],{"class":189},[52,79687,1816],{"class":102},[52,79689,79690,79693,79695,79697,79699,79701,79703,79705],{"class":54,"line":148},[52,79691,79692],{"class":62},"line_count ",[52,79694,80],{"class":79},[52,79696,9150],{"class":241},[52,79698,193],{"class":102},[52,79700,955],{"class":189},[52,79702,186],{"class":102},[52,79704,79466],{"class":189},[52,79706,1816],{"class":102},[52,79708,79709],{"class":54,"line":164},[52,79710,70],{"emptyLinePlaceholder":69},[52,79712,79713,79715,79717,79719,79721,79723,79725,79728],{"class":54,"line":170},[52,79714,242],{"class":241},[52,79716,193],{"class":102},[52,79718,115],{"class":83},[52,79720,79405],{"class":87},[52,79722,115],{"class":83},[52,79724,199],{"class":102},[52,79726,79727],{"class":189}," character_count",[52,79729,211],{"class":102},[52,79731,79732,79734,79736,79738,79740,79742,79744,79747],{"class":54,"line":175},[52,79733,242],{"class":241},[52,79735,193],{"class":102},[52,79737,115],{"class":83},[52,79739,79428],{"class":87},[52,79741,115],{"class":83},[52,79743,199],{"class":102},[52,79745,79746],{"class":189}," word_count",[52,79748,211],{"class":102},[52,79750,79751,79753,79755,79757,79759,79761,79763,79766],{"class":54,"line":214},[52,79752,242],{"class":241},[52,79754,193],{"class":102},[52,79756,115],{"class":83},[52,79758,79451],{"class":87},[52,79760,115],{"class":83},[52,79762,199],{"class":102},[52,79764,79765],{"class":189}," line_count",[52,79767,211],{"class":102},[639,79769,24095],{"id":24094},[309,79771,79772,79778,79784,79790,79796],{},[24,79773,79774,79777],{},[49,79775,79776],{},"len(text)"," counts every character in the string",[24,79779,79780,79783],{},[49,79781,79782],{},"text.split()"," breaks the text into words",[24,79785,79786,79789],{},[49,79787,79788],{},"len(text.split())"," gives the number of words",[24,79791,79792,79795],{},[49,79793,79794],{},"text.splitlines()"," breaks the text into lines",[24,79797,79798,79801],{},[49,79799,79800],{},"len(text.splitlines())"," gives the number of lines",[14,79803,79804,79805,1727,79807,186],{},"If you want a closer look at ",[49,79806,44313],{},[372,79808,79810],{"href":79809},"\u002Freference\u002Fpython-len-function-explained\u002F","Python len() function explained",[639,79812,13563],{"id":13562},[42,79814,79816],{"className":44,"code":79815,"language":46,"meta":47,"style":47},"Characters: 57\nWords: 11\nLines: 3\n",[49,79817,79818,79828,79838],{"__ignoreMap":47},[52,79819,79820,79823,79825],{"class":54,"line":55},[52,79821,79822],{"class":62},"Characters",[52,79824,118],{"class":102},[52,79826,79827],{"class":121}," 57\n",[52,79829,79830,79833,79835],{"class":54,"line":66},[52,79831,79832],{"class":62},"Words",[52,79834,118],{"class":102},[52,79836,79837],{"class":121}," 11\n",[52,79839,79840,79843,79845],{"class":54,"line":73},[52,79841,79842],{"class":62},"Lines",[52,79844,118],{"class":102},[52,79846,1085],{"class":121},[14,79848,79849],{},"The exact character count depends on the text, including spaces and punctuation.",[37,79851,79853],{"id":79852},"how-word-counting-works","How word counting works",[14,79855,79856],{},"Now let’s extend the script to count how many times each word appears.",[42,79858,79860],{"className":44,"code":79859,"language":46,"meta":47,"style":47},"text = \"Python is simple. Python is useful. Python is fun.\"\n\ncleaned_text = text.lower().replace(\".\", \"\")\nwords = cleaned_text.split()\n\ncounts = {}\n\nfor word in words:\n    counts[word] = counts.get(word, 0) + 1\n\nprint(\"Words:\", words)\nprint(\"Word counts:\", counts)\n",[49,79861,79862,79875,79879,79910,79925,79929,79937,79941,79953,79985,79989,80007],{"__ignoreMap":47},[52,79863,79864,79866,79868,79870,79873],{"class":54,"line":55},[52,79865,79346],{"class":62},[52,79867,80],{"class":79},[52,79869,84],{"class":83},[52,79871,79872],{"class":87},"Python is simple. Python is useful. Python is fun.",[52,79874,91],{"class":83},[52,79876,79877],{"class":54,"line":66},[52,79878,70],{"emptyLinePlaceholder":69},[52,79880,79881,79884,79886,79888,79890,79892,79894,79896,79898,79900,79902,79904,79906,79908],{"class":54,"line":73},[52,79882,79883],{"class":62},"cleaned_text ",[52,79885,80],{"class":79},[52,79887,56252],{"class":62},[52,79889,186],{"class":102},[52,79891,30237],{"class":189},[52,79893,18679],{"class":102},[52,79895,79376],{"class":189},[52,79897,193],{"class":102},[52,79899,115],{"class":83},[52,79901,186],{"class":87},[52,79903,115],{"class":83},[52,79905,199],{"class":102},[52,79907,3448],{"class":83},[52,79909,211],{"class":102},[52,79911,79912,79914,79916,79919,79921,79923],{"class":54,"line":94},[52,79913,32960],{"class":62},[52,79915,80],{"class":79},[52,79917,79918],{"class":62}," cleaned_text",[52,79920,186],{"class":102},[52,79922,58133],{"class":189},[52,79924,230],{"class":102},[52,79926,79927],{"class":54,"line":106},[52,79928,70],{"emptyLinePlaceholder":69},[52,79930,79931,79933,79935],{"class":54,"line":128},[52,79932,46151],{"class":62},[52,79934,80],{"class":79},[52,79936,10932],{"class":102},[52,79938,79939],{"class":54,"line":148},[52,79940,70],{"emptyLinePlaceholder":69},[52,79942,79943,79945,79947,79949,79951],{"class":54,"line":164},[52,79944,12265],{"class":58},[52,79946,33016],{"class":62},[52,79948,11213],{"class":58},[52,79950,79491],{"class":62},[52,79952,1330],{"class":102},[52,79954,79955,79957,79959,79961,79963,79965,79967,79969,79971,79973,79975,79977,79979,79981,79983],{"class":54,"line":170},[52,79956,79498],{"class":62},[52,79958,250],{"class":102},[52,79960,79503],{"class":62},[52,79962,11122],{"class":102},[52,79964,7038],{"class":79},[52,79966,46370],{"class":62},[52,79968,186],{"class":102},[52,79970,190],{"class":189},[52,79972,193],{"class":102},[52,79974,79503],{"class":189},[52,79976,199],{"class":102},[52,79978,13049],{"class":121},[52,79980,1521],{"class":102},[52,79982,5234],{"class":79},[52,79984,8645],{"class":121},[52,79986,79987],{"class":54,"line":175},[52,79988,70],{"emptyLinePlaceholder":69},[52,79990,79991,79993,79995,79997,79999,80001,80003,80005],{"class":54,"line":214},[52,79992,242],{"class":241},[52,79994,193],{"class":102},[52,79996,115],{"class":83},[52,79998,79428],{"class":87},[52,80000,115],{"class":83},[52,80002,199],{"class":102},[52,80004,79491],{"class":189},[52,80006,211],{"class":102},[52,80008,80009,80011,80013,80015,80017,80019,80021,80023],{"class":54,"line":233},[52,80010,242],{"class":241},[52,80012,193],{"class":102},[52,80014,115],{"class":83},[52,80016,79542],{"class":87},[52,80018,115],{"class":83},[52,80020,199],{"class":102},[52,80022,46370],{"class":189},[52,80024,211],{"class":102},[639,80026,80028],{"id":80027},"step-by-step","Step by step",[24097,80030,80032],{"id":80031},"_1-convert-to-lowercase","1. Convert to lowercase",[42,80034,80036],{"className":44,"code":80035,"language":46,"meta":47,"style":47},"text.lower()\n",[49,80037,80038],{"__ignoreMap":47},[52,80039,80040,80042,80044,80046],{"class":54,"line":55},[52,80041,955],{"class":62},[52,80043,186],{"class":102},[52,80045,30237],{"class":189},[52,80047,230],{"class":102},[14,80049,80050,80051,759,80053,80055],{},"This makes ",[49,80052,40940],{},[49,80054,46],{}," count as the same word.",[24097,80057,80059],{"id":80058},"_2-remove-simple-punctuation","2. Remove simple punctuation",[42,80061,80063],{"className":44,"code":80062,"language":46,"meta":47,"style":47},"text.lower().replace(\".\", \"\")\n",[49,80064,80065],{"__ignoreMap":47},[52,80066,80067,80069,80071,80073,80075,80077,80079,80081,80083,80085,80087,80089],{"class":54,"line":55},[52,80068,955],{"class":62},[52,80070,186],{"class":102},[52,80072,30237],{"class":189},[52,80074,18679],{"class":102},[52,80076,79376],{"class":189},[52,80078,193],{"class":102},[52,80080,115],{"class":83},[52,80082,186],{"class":87},[52,80084,115],{"class":83},[52,80086,199],{"class":102},[52,80088,3448],{"class":83},[52,80090,211],{"class":102},[14,80092,80093,80094,42783,80097,186],{},"This removes periods so words like ",[49,80095,80096],{},"simple.",[49,80098,80099],{},"simple",[24097,80101,80103],{"id":80102},"_3-split-into-words","3. Split into words",[42,80105,80107],{"className":44,"code":80106,"language":46,"meta":47,"style":47},"words = cleaned_text.split()\n",[49,80108,80109],{"__ignoreMap":47},[52,80110,80111,80113,80115,80117,80119,80121],{"class":54,"line":55},[52,80112,32960],{"class":62},[52,80114,80],{"class":79},[52,80116,79918],{"class":62},[52,80118,186],{"class":102},[52,80120,58133],{"class":189},[52,80122,230],{"class":102},[14,80124,80125],{},"This creates a list of words.",[24097,80127,80129],{"id":80128},"_4-create-an-empty-dictionary","4. Create an empty dictionary",[42,80131,80133],{"className":44,"code":80132,"language":46,"meta":47,"style":47},"counts = {}\n",[49,80134,80135],{"__ignoreMap":47},[52,80136,80137,80139,80141],{"class":54,"line":55},[52,80138,46151],{"class":62},[52,80140,80],{"class":79},[52,80142,10932],{"class":102},[14,80144,80145],{},"This dictionary will store each word and its count.",[24097,80147,80149],{"id":80148},"_5-loop-through-the-words","5. Loop through the words",[42,80151,80153],{"className":44,"code":80152,"language":46,"meta":47,"style":47},"for word in words:\n    counts[word] = counts.get(word, 0) + 1\n",[49,80154,80155,80167],{"__ignoreMap":47},[52,80156,80157,80159,80161,80163,80165],{"class":54,"line":55},[52,80158,12265],{"class":58},[52,80160,33016],{"class":62},[52,80162,11213],{"class":58},[52,80164,79491],{"class":62},[52,80166,1330],{"class":102},[52,80168,80169,80171,80173,80175,80177,80179,80181,80183,80185,80187,80189,80191,80193,80195,80197],{"class":54,"line":66},[52,80170,79498],{"class":62},[52,80172,250],{"class":102},[52,80174,79503],{"class":62},[52,80176,11122],{"class":102},[52,80178,7038],{"class":79},[52,80180,46370],{"class":62},[52,80182,186],{"class":102},[52,80184,190],{"class":189},[52,80186,193],{"class":102},[52,80188,79503],{"class":189},[52,80190,199],{"class":102},[52,80192,13049],{"class":121},[52,80194,1521],{"class":102},[52,80196,5234],{"class":79},[52,80198,8645],{"class":121},[14,80200,80201],{},"This does the counting:",[309,80203,80204,80210,80215],{},[24,80205,80206,80209],{},[49,80207,80208],{},"counts.get(word, 0)"," gets the current count",[24,80211,80212,80213],{},"If the word is not in the dictionary yet, it uses ",[49,80214,13294],{},[24,80216,80217,80218],{},"Then it adds ",[49,80219,9200],{},[639,80221,13563],{"id":80222},"expected-output-1",[42,80224,80226],{"className":44,"code":80225,"language":46,"meta":47,"style":47},"Words: ['python', 'is', 'simple', 'python', 'is', 'useful', 'python', 'is', 'fun']\nWord counts: {'python': 3, 'is': 3, 'simple': 1, 'useful': 1, 'fun': 1}\n",[49,80227,80228,80310],{"__ignoreMap":47},[52,80229,80230,80232,80234,80236,80238,80240,80242,80244,80246,80248,80250,80252,80254,80256,80258,80260,80262,80264,80266,80268,80270,80272,80274,80276,80278,80281,80283,80285,80287,80289,80291,80293,80295,80297,80299,80301,80303,80306,80308],{"class":54,"line":55},[52,80231,79832],{"class":62},[52,80233,118],{"class":102},[52,80235,18162],{"class":102},[52,80237,4162],{"class":83},[52,80239,46],{"class":87},[52,80241,4162],{"class":83},[52,80243,199],{"class":102},[52,80245,4172],{"class":83},[52,80247,1444],{"class":87},[52,80249,4162],{"class":83},[52,80251,199],{"class":102},[52,80253,4172],{"class":83},[52,80255,80099],{"class":87},[52,80257,4162],{"class":83},[52,80259,199],{"class":102},[52,80261,4172],{"class":83},[52,80263,46],{"class":87},[52,80265,4162],{"class":83},[52,80267,199],{"class":102},[52,80269,4172],{"class":83},[52,80271,1444],{"class":87},[52,80273,4162],{"class":83},[52,80275,199],{"class":102},[52,80277,4172],{"class":83},[52,80279,80280],{"class":87},"useful",[52,80282,4162],{"class":83},[52,80284,199],{"class":102},[52,80286,4172],{"class":83},[52,80288,46],{"class":87},[52,80290,4162],{"class":83},[52,80292,199],{"class":102},[52,80294,4172],{"class":83},[52,80296,1444],{"class":87},[52,80298,4162],{"class":83},[52,80300,199],{"class":102},[52,80302,4172],{"class":83},[52,80304,80305],{"class":87},"fun",[52,80307,4162],{"class":83},[52,80309,1147],{"class":102},[52,80311,80312,80315,80317,80319,80321,80323,80325,80327,80329,80331,80333,80335,80337,80339,80341,80343,80345,80347,80349,80351,80353,80355,80357,80359,80361,80363,80365,80367,80369,80371,80373,80375,80377],{"class":54,"line":66},[52,80313,80314],{"class":62},"Word counts",[52,80316,118],{"class":102},[52,80318,3000],{"class":102},[52,80320,4162],{"class":83},[52,80322,46],{"class":87},[52,80324,4162],{"class":83},[52,80326,118],{"class":102},[52,80328,5261],{"class":121},[52,80330,199],{"class":102},[52,80332,4172],{"class":83},[52,80334,1444],{"class":87},[52,80336,4162],{"class":83},[52,80338,118],{"class":102},[52,80340,5261],{"class":121},[52,80342,199],{"class":102},[52,80344,4172],{"class":83},[52,80346,80099],{"class":87},[52,80348,4162],{"class":83},[52,80350,118],{"class":102},[52,80352,1518],{"class":121},[52,80354,199],{"class":102},[52,80356,4172],{"class":83},[52,80358,80280],{"class":87},[52,80360,4162],{"class":83},[52,80362,118],{"class":102},[52,80364,1518],{"class":121},[52,80366,199],{"class":102},[52,80368,4172],{"class":83},[52,80370,80305],{"class":87},[52,80372,4162],{"class":83},[52,80374,118],{"class":102},[52,80376,1518],{"class":121},[52,80378,167],{"class":102},[37,80380,80382],{"id":80381},"improving-the-script","Improving the script",[14,80384,80385],{},"Once the basic version works, you can make it more useful.",[14,80387,80388],{},"Common improvements:",[309,80390,80391,80394,80397,80400,80406],{},[24,80392,80393],{},"Sort words by frequency",[24,80395,80396],{},"Ignore very common words if needed",[24,80398,80399],{},"Read text from a file instead of using a hardcoded string",[24,80401,80402,80403],{},"Clean more punctuation with ",[49,80404,80405],{},"replace()",[24,80407,80408],{},"Show only the top 5 or top 10 words",[14,80410,80411],{},"Here is a simple version that sorts word counts from highest to lowest:",[42,80413,80415],{"className":44,"code":80414,"language":46,"meta":47,"style":47},"text = \"Python is simple. Python is useful. Python is fun.\"\n\ncleaned_text = text.lower().replace(\".\", \"\")\nwords = cleaned_text.split()\n\ncounts = {}\nfor word in words:\n    counts[word] = counts.get(word, 0) + 1\n\nsorted_counts = sorted(counts.items(), key=lambda item: item[1], reverse=True)\n\nprint(\"Most common words:\")\nfor word, count in sorted_counts:\n    print(word, count)\n",[49,80416,80417,80429,80433,80463,80477,80481,80489,80501,80533,80537,80584,80588,80603,80621],{"__ignoreMap":47},[52,80418,80419,80421,80423,80425,80427],{"class":54,"line":55},[52,80420,79346],{"class":62},[52,80422,80],{"class":79},[52,80424,84],{"class":83},[52,80426,79872],{"class":87},[52,80428,91],{"class":83},[52,80430,80431],{"class":54,"line":66},[52,80432,70],{"emptyLinePlaceholder":69},[52,80434,80435,80437,80439,80441,80443,80445,80447,80449,80451,80453,80455,80457,80459,80461],{"class":54,"line":73},[52,80436,79883],{"class":62},[52,80438,80],{"class":79},[52,80440,56252],{"class":62},[52,80442,186],{"class":102},[52,80444,30237],{"class":189},[52,80446,18679],{"class":102},[52,80448,79376],{"class":189},[52,80450,193],{"class":102},[52,80452,115],{"class":83},[52,80454,186],{"class":87},[52,80456,115],{"class":83},[52,80458,199],{"class":102},[52,80460,3448],{"class":83},[52,80462,211],{"class":102},[52,80464,80465,80467,80469,80471,80473,80475],{"class":54,"line":94},[52,80466,32960],{"class":62},[52,80468,80],{"class":79},[52,80470,79918],{"class":62},[52,80472,186],{"class":102},[52,80474,58133],{"class":189},[52,80476,230],{"class":102},[52,80478,80479],{"class":54,"line":106},[52,80480,70],{"emptyLinePlaceholder":69},[52,80482,80483,80485,80487],{"class":54,"line":128},[52,80484,46151],{"class":62},[52,80486,80],{"class":79},[52,80488,10932],{"class":102},[52,80490,80491,80493,80495,80497,80499],{"class":54,"line":148},[52,80492,12265],{"class":58},[52,80494,33016],{"class":62},[52,80496,11213],{"class":58},[52,80498,79491],{"class":62},[52,80500,1330],{"class":102},[52,80502,80503,80505,80507,80509,80511,80513,80515,80517,80519,80521,80523,80525,80527,80529,80531],{"class":54,"line":164},[52,80504,79498],{"class":62},[52,80506,250],{"class":102},[52,80508,79503],{"class":62},[52,80510,11122],{"class":102},[52,80512,7038],{"class":79},[52,80514,46370],{"class":62},[52,80516,186],{"class":102},[52,80518,190],{"class":189},[52,80520,193],{"class":102},[52,80522,79503],{"class":189},[52,80524,199],{"class":102},[52,80526,13049],{"class":121},[52,80528,1521],{"class":102},[52,80530,5234],{"class":79},[52,80532,8645],{"class":121},[52,80534,80535],{"class":54,"line":170},[52,80536,70],{"emptyLinePlaceholder":69},[52,80538,80539,80542,80544,80546,80548,80550,80552,80554,80556,80558,80560,80563,80565,80567,80569,80571,80573,80575,80578,80580,80582],{"class":54,"line":175},[52,80540,80541],{"class":62},"sorted_counts ",[52,80543,80],{"class":79},[52,80545,49172],{"class":241},[52,80547,193],{"class":102},[52,80549,47364],{"class":189},[52,80551,186],{"class":102},[52,80553,11220],{"class":189},[52,80555,64297],{"class":102},[52,80557,22376],{"class":202},[52,80559,80],{"class":79},[52,80561,80562],{"class":1475},"lambda",[52,80564,30211],{"class":7018},[52,80566,118],{"class":102},[52,80568,30211],{"class":189},[52,80570,250],{"class":102},[52,80572,9200],{"class":121},[52,80574,15661],{"class":102},[52,80576,80577],{"class":202}," reverse",[52,80579,80],{"class":79},[52,80581,6233],{"class":160},[52,80583,211],{"class":102},[52,80585,80586],{"class":54,"line":214},[52,80587,70],{"emptyLinePlaceholder":69},[52,80589,80590,80592,80594,80596,80599,80601],{"class":54,"line":233},[52,80591,242],{"class":241},[52,80593,193],{"class":102},[52,80595,115],{"class":83},[52,80597,80598],{"class":87},"Most common words:",[52,80600,115],{"class":83},[52,80602,211],{"class":102},[52,80604,80605,80607,80609,80611,80614,80616,80619],{"class":54,"line":238},[52,80606,12265],{"class":58},[52,80608,33035],{"class":62},[52,80610,199],{"class":102},[52,80612,80613],{"class":62}," count ",[52,80615,11213],{"class":58},[52,80617,80618],{"class":62}," sorted_counts",[52,80620,1330],{"class":102},[52,80622,80623,80625,80627,80629,80631,80633],{"class":54,"line":272},[52,80624,1599],{"class":241},[52,80626,193],{"class":102},[52,80628,79503],{"class":189},[52,80630,199],{"class":102},[52,80632,71795],{"class":189},[52,80634,211],{"class":102},[639,80636,13563],{"id":80637},"expected-output-2",[42,80639,80641],{"className":44,"code":80640,"language":46,"meta":47,"style":47},"Most common words:\npython 3\nis 3\nsimple 1\nuseful 1\nfun 1\n",[49,80642,80643,80650,80657,80663,80670,80677],{"__ignoreMap":47},[52,80644,80645,80648],{"class":54,"line":55},[52,80646,80647],{"class":62},"Most common words",[52,80649,1330],{"class":102},[52,80651,80652,80655],{"class":54,"line":66},[52,80653,80654],{"class":62},"python ",[52,80656,7184],{"class":121},[52,80658,80659,80661],{"class":54,"line":73},[52,80660,1444],{"class":79},[52,80662,1085],{"class":121},[52,80664,80665,80668],{"class":54,"line":94},[52,80666,80667],{"class":62},"simple ",[52,80669,66541],{"class":121},[52,80671,80672,80675],{"class":54,"line":106},[52,80673,80674],{"class":62},"useful ",[52,80676,66541],{"class":121},[52,80678,80679,80682],{"class":54,"line":128},[52,80680,80681],{"class":62},"fun ",[52,80683,66541],{"class":121},[14,80685,80686,80687,186],{},"You can also read text from a file and then use the same logic. For that, see ",[372,80688,29921],{"href":29866},[37,80690,13563],{"id":80691},"expected-output-3",[14,80693,80694],{},"Your script should show results like these:",[309,80696,80697,80700,80703,80706],{},[24,80698,80699],{},"Total characters",[24,80701,80702],{},"Total words",[24,80704,80705],{},"Total lines",[24,80707,80708],{},"A dictionary or sorted list of word counts",[14,80710,16291],{},[42,80712,80714],{"className":44,"code":80713,"language":46,"meta":47,"style":47},"Characters: 42\nWords: 7\nLines: 1\nWord counts: {'python': 2, 'is': 2, 'simple': 1, 'and': 1, 'useful': 1}\n",[49,80715,80716,80725,80734,80742],{"__ignoreMap":47},[52,80717,80718,80720,80722],{"class":54,"line":55},[52,80719,79822],{"class":62},[52,80721,118],{"class":102},[52,80723,80724],{"class":121}," 42\n",[52,80726,80727,80729,80731],{"class":54,"line":66},[52,80728,79832],{"class":62},[52,80730,118],{"class":102},[52,80732,80733],{"class":121}," 7\n",[52,80735,80736,80738,80740],{"class":54,"line":73},[52,80737,79842],{"class":62},[52,80739,118],{"class":102},[52,80741,8645],{"class":121},[52,80743,80744,80746,80748,80750,80752,80754,80756,80758,80760,80762,80764,80766,80768,80770,80772,80774,80776,80778,80780,80782,80784,80786,80788,80791,80793,80795,80797,80799,80801,80803,80805,80807,80809],{"class":54,"line":94},[52,80745,80314],{"class":62},[52,80747,118],{"class":102},[52,80749,3000],{"class":102},[52,80751,4162],{"class":83},[52,80753,46],{"class":87},[52,80755,4162],{"class":83},[52,80757,118],{"class":102},[52,80759,9168],{"class":121},[52,80761,199],{"class":102},[52,80763,4172],{"class":83},[52,80765,1444],{"class":87},[52,80767,4162],{"class":83},[52,80769,118],{"class":102},[52,80771,9168],{"class":121},[52,80773,199],{"class":102},[52,80775,4172],{"class":83},[52,80777,80099],{"class":87},[52,80779,4162],{"class":83},[52,80781,118],{"class":102},[52,80783,1518],{"class":121},[52,80785,199],{"class":102},[52,80787,4172],{"class":83},[52,80789,80790],{"class":87},"and",[52,80792,4162],{"class":83},[52,80794,118],{"class":102},[52,80796,1518],{"class":121},[52,80798,199],{"class":102},[52,80800,4172],{"class":83},[52,80802,80280],{"class":87},[52,80804,4162],{"class":83},[52,80806,118],{"class":102},[52,80808,1518],{"class":121},[52,80810,167],{"class":102},[37,80812,12200],{"id":12199},[14,80814,80815],{},"If your result looks wrong, check the data at each step.",[14,80817,45895],{},[42,80819,80821],{"className":44,"code":80820,"language":46,"meta":47,"style":47},"print(text)\nprint(text.split())\nprint(text.lower())\nprint(words)\nprint(counts)\n",[49,80822,80823,80833,80847,80861,80871],{"__ignoreMap":47},[52,80824,80825,80827,80829,80831],{"class":54,"line":55},[52,80826,242],{"class":241},[52,80828,193],{"class":102},[52,80830,955],{"class":189},[52,80832,211],{"class":102},[52,80834,80835,80837,80839,80841,80843,80845],{"class":54,"line":66},[52,80836,242],{"class":241},[52,80838,193],{"class":102},[52,80840,955],{"class":189},[52,80842,186],{"class":102},[52,80844,58133],{"class":189},[52,80846,1816],{"class":102},[52,80848,80849,80851,80853,80855,80857,80859],{"class":54,"line":73},[52,80850,242],{"class":241},[52,80852,193],{"class":102},[52,80854,955],{"class":189},[52,80856,186],{"class":102},[52,80858,30237],{"class":189},[52,80860,1816],{"class":102},[52,80862,80863,80865,80867,80869],{"class":54,"line":94},[52,80864,242],{"class":241},[52,80866,193],{"class":102},[52,80868,67032],{"class":189},[52,80870,211],{"class":102},[52,80872,80873,80875,80877,80879],{"class":54,"line":106},[52,80874,242],{"class":241},[52,80876,193],{"class":102},[52,80878,47364],{"class":189},[52,80880,211],{"class":102},[14,80882,80883],{},"These help you see:",[309,80885,80886,80889,80894,80897,80902],{},[24,80887,80888],{},"The original text",[24,80890,4983,80891,80893],{},[49,80892,79596],{}," is breaking the text",[24,80895,80896],{},"Whether lowercase conversion worked",[24,80898,80899,80900,41215],{},"What is inside the ",[49,80901,67032],{},[24,80903,80904],{},"Whether the dictionary is counting correctly",[14,80906,80907],{},"Good things to check:",[309,80909,80910,80913,80916,80919,80922],{},[24,80911,80912],{},"Print the cleaned text before counting",[24,80914,80915],{},"Print the words list to confirm the split result",[24,80917,80918],{},"Print the dictionary after the loop",[24,80920,80921],{},"Check punctuation if the counts look strange",[24,80923,80924],{},"Check uppercase and lowercase words if duplicates appear",[14,80926,80927,80928,186],{},"If you want more practice with dictionaries, see ",[372,80929,11835],{"href":11834},[37,80931,12322],{"id":12321},[14,80933,80934],{},"These are some common problems beginners run into:",[309,80936,80937,80940,80943,80951,80954],{},[24,80938,80939],{},"Forgetting to lowercase text before counting words",[24,80941,80942],{},"Not removing punctuation, which creates different versions of the same word",[24,80944,10294,80945,10598,80948,80950],{},[49,80946,80947],{},"split(' ')",[49,80949,79596],{},", which can behave badly with extra spaces",[24,80952,80953],{},"Trying to count words before converting non-string data to text",[24,80955,80956],{},"Expecting perfect natural language analysis from a simple script",[14,80958,80959],{},"A beginner script like this is great for learning, but it is still simple. Real text analysis usually needs better punctuation handling and more advanced cleaning.",[37,80961,1942],{"id":1941},[639,80963,80965],{"id":80964},"does-this-script-count-punctuation-as-characters","Does this script count punctuation as characters?",[14,80967,80968,80969,80971],{},"Yes. ",[49,80970,79776],{}," counts all characters in the string, including spaces and punctuation.",[639,80973,1956,80975,80977],{"id":80974},"why-use-lower-before-counting-words",[49,80976,70198],{}," before counting words?",[14,80979,80980,80981,759,80983,80055],{},"It makes words like ",[49,80982,40940],{},[49,80984,46],{},[639,80986,80988],{"id":80987},"can-i-analyze-a-text-file-instead-of-a-string","Can I analyze a text file instead of a string?",[14,80990,80991],{},"Yes. Read the file into a string first, then use the same counting steps.",[639,80993,59422,80995,80997],{"id":80994},"is-split-enough-for-real-text-analysis",[49,80996,79596],{}," enough for real text analysis?",[14,80999,81000],{},"It is enough for a beginner example, but more advanced text analysis needs better text cleaning.",[37,81002,2005],{"id":2004},[309,81004,81005,81009,81013,81018,81022,81026],{},[24,81006,81007],{},[372,81008,38969],{"href":38968},[24,81010,81011],{},[372,81012,10883],{"href":10882},[24,81014,81015],{},[372,81016,81017],{"href":79593},"Python string split() method",[24,81019,81020],{},[372,81021,79810],{"href":79809},[24,81023,81024],{},[372,81025,29921],{"href":29866},[24,81027,81028],{},[372,81029,81031],{"href":81030},"\u002Fexamples\u002Fpython-word-count-script-example\u002F","Python word count script example",[14,81033,81034],{},"Try the same script with file input next, then extend it so it shows the most common words first.",[2034,81036,81037],{},"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 .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 .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 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 .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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":81039},[81040,81041,81042,81043,81047,81051,81054,81055,81056,81057,81065],{"id":39,"depth":66,"text":40},{"id":28679,"depth":66,"text":28680},{"id":79579,"depth":66,"text":79580},{"id":79616,"depth":66,"text":79617,"children":81044},[81045,81046],{"id":24094,"depth":73,"text":24095},{"id":13562,"depth":73,"text":13563},{"id":79852,"depth":66,"text":79853,"children":81048},[81049,81050],{"id":80027,"depth":73,"text":80028},{"id":80222,"depth":73,"text":13563},{"id":80381,"depth":66,"text":80382,"children":81052},[81053],{"id":80637,"depth":73,"text":13563},{"id":80691,"depth":66,"text":13563},{"id":12199,"depth":66,"text":12200},{"id":12321,"depth":66,"text":12322},{"id":1941,"depth":66,"text":1942,"children":81058},[81059,81060,81062,81063],{"id":80964,"depth":73,"text":80965},{"id":80974,"depth":73,"text":81061},"Why use lower() before counting words?",{"id":80987,"depth":73,"text":80988},{"id":80994,"depth":73,"text":81064},"Is split() enough for real text analysis?",{"id":2004,"depth":66,"text":2005},"Master python text analysis script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-text-analysis-script-example",{"title":79303,"description":81066},"examples\u002Fpython-text-analysis-script-example","ogQWoTUqNOu7OCRLullf4PuvgMEY5MxnwjQTfsO74T4",{"id":81073,"title":81074,"body":81075,"description":82718,"extension":2075,"meta":82719,"navigation":69,"path":82720,"seo":82721,"stem":82722,"__hash__":82723},"content\u002Fexamples\u002Fpython-timer-script-example.md","Python Timer Script Example",{"type":7,"value":81076,"toc":82679},[81077,81080,81083,81088,81090,81168,81171,81173,81176,81192,81198,81202,81205,81227,81229,81300,81302,81328,81330,81336,81339,81346,81350,81353,81356,81483,81485,81508,81516,81522,81525,81527,81533,81538,81542,81545,81550,81639,81645,81650,81652,81689,81692,81697,81706,81713,81715,81723,81877,81880,81891,81893,81896,81900,81906,81908,81935,81941,81943,81980,81986,81990,81992,82033,82036,82083,82091,82095,82100,82102,82128,82135,82142,82148,82152,82155,82158,82237,82244,82247,82249,82290,82293,82299,82304,82307,82373,82377,82380,82392,82435,82438,82454,82458,82461,82464,82480,82483,82574,82577,82582,82584,82588,82593,82597,82603,82611,82623,82630,82639,82641,82673,82676],[10,81078,81074],{"id":81079},"python-timer-script-example",[14,81081,81082],{},"Build a simple Python timer script that waits for a number of seconds, shows progress, and prints a message when the timer finishes.",[14,81084,81085,81086,13338],{},"This page focuses on a small runnable example, not a full guide to the ",[49,81087,13181],{},[37,81089,40],{"id":39},[42,81091,81093],{"className":44,"code":81092,"language":46,"meta":47,"style":47},"import time\n\nseconds = 5\nprint(f\"Starting {seconds}-second timer...\")\n\ntime.sleep(seconds)\nprint(\"Time is up!\")\n",[49,81094,81095,81101,81105,81113,81135,81139,81153],{"__ignoreMap":47},[52,81096,81097,81099],{"class":54,"line":55},[52,81098,59],{"class":58},[52,81100,13019],{"class":62},[52,81102,81103],{"class":54,"line":66},[52,81104,70],{"emptyLinePlaceholder":69},[52,81106,81107,81109,81111],{"class":54,"line":73},[52,81108,13028],{"class":62},[52,81110,80],{"class":79},[52,81112,5264],{"class":121},[52,81114,81115,81117,81119,81121,81124,81126,81128,81130,81133],{"class":54,"line":94},[52,81116,242],{"class":241},[52,81118,193],{"class":102},[52,81120,1476],{"class":1475},[52,81122,81123],{"class":87},"\"Starting ",[52,81125,1482],{"class":121},[52,81127,13060],{"class":189},[52,81129,1488],{"class":121},[52,81131,81132],{"class":87},"-second timer...\"",[52,81134,211],{"class":102},[52,81136,81137],{"class":54,"line":106},[52,81138,70],{"emptyLinePlaceholder":69},[52,81140,81141,81143,81145,81147,81149,81151],{"class":54,"line":128},[52,81142,13181],{"class":62},[52,81144,186],{"class":102},[52,81146,13072],{"class":189},[52,81148,193],{"class":102},[52,81150,13060],{"class":189},[52,81152,211],{"class":102},[52,81154,81155,81157,81159,81161,81164,81166],{"class":54,"line":148},[52,81156,242],{"class":241},[52,81158,193],{"class":102},[52,81160,115],{"class":83},[52,81162,81163],{"class":87},"Time is up!",[52,81165,115],{"class":83},[52,81167,211],{"class":102},[14,81169,81170],{},"This is the simplest timer example. It pauses the program for 5 seconds, then prints a message.",[37,81172,323],{"id":322},[14,81174,81175],{},"This example helps you learn the basic idea behind a timer in Python:",[309,81177,81178,81181,81186,81189],{},[24,81179,81180],{},"It shows how to pause a Python program for a set number of seconds",[24,81182,8049,81183,81185],{},[49,81184,13147],{}," to create the delay",[24,81187,81188],{},"It helps beginners understand basic timing scripts",[24,81190,81191],{},"It keeps the example small and easy to run",[14,81193,81194,81195,81197],{},"If you are new to this, the key idea is simple: Python runs line by line, and ",[49,81196,13147],{}," tells the program to wait before moving to the next line.",[37,81199,81201],{"id":81200},"basic-timer-script","Basic timer script",[14,81203,81204],{},"A basic timer needs only a few steps:",[309,81206,81207,81213,81218,81224],{},[24,81208,81209,81210,81212],{},"Import the ",[49,81211,13181],{}," module first",[24,81214,81215,81216],{},"Store the timer length in a variable like ",[49,81217,13060],{},[24,81219,16483,81220,81223],{},[49,81221,81222],{},"time.sleep(seconds)"," to wait",[24,81225,81226],{},"Print a message before and after the delay",[14,81228,45415],{},[42,81230,81232],{"className":44,"code":81231,"language":46,"meta":47,"style":47},"import time\n\nseconds = 3\n\nprint(\"Timer started\")\ntime.sleep(seconds)\nprint(\"Timer finished\")\n",[49,81233,81234,81240,81244,81252,81256,81271,81285],{"__ignoreMap":47},[52,81235,81236,81238],{"class":54,"line":55},[52,81237,59],{"class":58},[52,81239,13019],{"class":62},[52,81241,81242],{"class":54,"line":66},[52,81243,70],{"emptyLinePlaceholder":69},[52,81245,81246,81248,81250],{"class":54,"line":73},[52,81247,13028],{"class":62},[52,81249,80],{"class":79},[52,81251,1085],{"class":121},[52,81253,81254],{"class":54,"line":94},[52,81255,70],{"emptyLinePlaceholder":69},[52,81257,81258,81260,81262,81264,81267,81269],{"class":54,"line":106},[52,81259,242],{"class":241},[52,81261,193],{"class":102},[52,81263,115],{"class":83},[52,81265,81266],{"class":87},"Timer started",[52,81268,115],{"class":83},[52,81270,211],{"class":102},[52,81272,81273,81275,81277,81279,81281,81283],{"class":54,"line":128},[52,81274,13181],{"class":62},[52,81276,186],{"class":102},[52,81278,13072],{"class":189},[52,81280,193],{"class":102},[52,81282,13060],{"class":189},[52,81284,211],{"class":102},[52,81286,81287,81289,81291,81293,81296,81298],{"class":54,"line":148},[52,81288,242],{"class":241},[52,81290,193],{"class":102},[52,81292,115],{"class":83},[52,81294,81295],{"class":87},"Timer finished",[52,81297,115],{"class":83},[52,81299,211],{"class":102},[639,81301,24095],{"id":24094},[309,81303,81304,81311,81317,81322],{},[24,81305,81306,81308,81309,14928],{},[49,81307,13321],{}," makes Python load the ",[49,81310,13181],{},[24,81312,81313,81316],{},[49,81314,81315],{},"seconds = 3"," stores the length of the timer",[24,81318,81319,81321],{},[49,81320,81222],{}," pauses the program for 3 seconds",[24,81323,81324,81325,81327],{},"The last ",[49,81326,9687],{}," runs after the pause ends",[639,81329,13563],{"id":13562},[42,81331,81334],{"className":81332,"code":81333,"language":955,"meta":47},[953],"Timer started\nTimer finished\n",[49,81335,81333],{"__ignoreMap":47},[14,81337,81338],{},"You will not see anything during the 3-second wait in this version. If you want visible progress, use a countdown loop.",[14,81340,81341,81342,186],{},"For more about this function, see ",[372,81343,81344,2866],{"href":13144},[49,81345,13147],{},[37,81347,81349],{"id":81348},"countdown-timer-version","Countdown timer version",[14,81351,81352],{},"A countdown timer is more useful because it shows the remaining time.",[14,81354,81355],{},"This version counts down one second at a time:",[42,81357,81359],{"className":44,"code":81358,"language":46,"meta":47,"style":47},"import time\n\nseconds = 5\n\nprint(\"Countdown starting...\")\n\nfor remaining in range(seconds, 0, -1):\n    print(f\"{remaining}...\")\n    time.sleep(1)\n\nprint(\"Time is up!\")\n",[49,81360,81361,81367,81371,81379,81383,81398,81402,81429,81451,81465,81469],{"__ignoreMap":47},[52,81362,81363,81365],{"class":54,"line":55},[52,81364,59],{"class":58},[52,81366,13019],{"class":62},[52,81368,81369],{"class":54,"line":66},[52,81370,70],{"emptyLinePlaceholder":69},[52,81372,81373,81375,81377],{"class":54,"line":73},[52,81374,13028],{"class":62},[52,81376,80],{"class":79},[52,81378,5264],{"class":121},[52,81380,81381],{"class":54,"line":94},[52,81382,70],{"emptyLinePlaceholder":69},[52,81384,81385,81387,81389,81391,81394,81396],{"class":54,"line":106},[52,81386,242],{"class":241},[52,81388,193],{"class":102},[52,81390,115],{"class":83},[52,81392,81393],{"class":87},"Countdown starting...",[52,81395,115],{"class":83},[52,81397,211],{"class":102},[52,81399,81400],{"class":54,"line":128},[52,81401,70],{"emptyLinePlaceholder":69},[52,81403,81404,81406,81409,81411,81413,81415,81417,81419,81421,81423,81425,81427],{"class":54,"line":148},[52,81405,12265],{"class":58},[52,81407,81408],{"class":62}," remaining ",[52,81410,11213],{"class":58},[52,81412,36864],{"class":241},[52,81414,193],{"class":102},[52,81416,13060],{"class":189},[52,81418,199],{"class":102},[52,81420,13049],{"class":121},[52,81422,199],{"class":102},[52,81424,140],{"class":79},[52,81426,9200],{"class":121},[52,81428,3114],{"class":102},[52,81430,81431,81433,81435,81437,81439,81441,81444,81446,81449],{"class":54,"line":164},[52,81432,1599],{"class":241},[52,81434,193],{"class":102},[52,81436,1476],{"class":1475},[52,81438,115],{"class":87},[52,81440,1482],{"class":121},[52,81442,81443],{"class":189},"remaining",[52,81445,1488],{"class":121},[52,81447,81448],{"class":87},"...\"",[52,81450,211],{"class":102},[52,81452,81453,81455,81457,81459,81461,81463],{"class":54,"line":170},[52,81454,13067],{"class":62},[52,81456,186],{"class":102},[52,81458,13072],{"class":189},[52,81460,193],{"class":102},[52,81462,9200],{"class":121},[52,81464,211],{"class":102},[52,81466,81467],{"class":54,"line":175},[52,81468,70],{"emptyLinePlaceholder":69},[52,81470,81471,81473,81475,81477,81479,81481],{"class":54,"line":214},[52,81472,242],{"class":241},[52,81474,193],{"class":102},[52,81476,115],{"class":83},[52,81478,81163],{"class":87},[52,81480,115],{"class":83},[52,81482,211],{"class":102},[639,81484,9910],{"id":9909},[309,81486,81487,81493,81498,81503],{},[24,81488,81489,81492],{},[49,81490,81491],{},"range(seconds, 0, -1)"," counts backward",[24,81494,81495,81496],{},"It starts at ",[49,81497,13060],{},[24,81499,81500,81501],{},"It stops before ",[49,81502,13294],{},[24,81504,81505,81507],{},[49,81506,66620],{}," means it moves down by 1 each time",[14,81509,81510,81511,27498,81513,81515],{},"So if ",[49,81512,13060],{},[49,81514,9479],{},", the loop prints:",[42,81517,81520],{"className":81518,"code":81519,"language":955,"meta":47},[953],"5...\n4...\n3...\n2...\n1...\n",[49,81521,81519],{"__ignoreMap":47},[14,81523,81524],{},"Then it prints the final message.",[639,81526,13563],{"id":80222},[42,81528,81531],{"className":81529,"code":81530,"language":955,"meta":47},[953],"Countdown starting...\n5...\n4...\n3...\n2...\n1...\nTime is up!\n",[49,81532,81530],{"__ignoreMap":47},[14,81534,81535,81536,186],{},"If you want another similar example, see the ",[372,81537,79247],{"href":79246},[37,81539,81541],{"id":81540},"let-the-user-choose-the-timer-length","Let the user choose the timer length",[14,81543,81544],{},"You can make the script more practical by asking the user for the number of seconds.",[14,81546,3650,81547,81549],{},[49,81548,10597],{}," to get text from the user, then convert it to a number.",[42,81551,81553],{"className":44,"code":81552,"language":46,"meta":47,"style":47},"import time\n\nseconds = int(input(\"Enter the number of seconds: \"))\n\nprint(f\"Starting {seconds}-second timer...\")\ntime.sleep(seconds)\nprint(\"Time is up!\")\n",[49,81554,81555,81561,81565,81587,81591,81611,81625],{"__ignoreMap":47},[52,81556,81557,81559],{"class":54,"line":55},[52,81558,59],{"class":58},[52,81560,13019],{"class":62},[52,81562,81563],{"class":54,"line":66},[52,81564,70],{"emptyLinePlaceholder":69},[52,81566,81567,81569,81571,81573,81575,81577,81579,81581,81583,81585],{"class":54,"line":73},[52,81568,13028],{"class":62},[52,81570,80],{"class":79},[52,81572,9789],{"class":4733},[52,81574,193],{"class":102},[52,81576,13610],{"class":241},[52,81578,193],{"class":102},[52,81580,115],{"class":83},[52,81582,13617],{"class":87},[52,81584,115],{"class":83},[52,81586,8886],{"class":102},[52,81588,81589],{"class":54,"line":94},[52,81590,70],{"emptyLinePlaceholder":69},[52,81592,81593,81595,81597,81599,81601,81603,81605,81607,81609],{"class":54,"line":106},[52,81594,242],{"class":241},[52,81596,193],{"class":102},[52,81598,1476],{"class":1475},[52,81600,81123],{"class":87},[52,81602,1482],{"class":121},[52,81604,13060],{"class":189},[52,81606,1488],{"class":121},[52,81608,81132],{"class":87},[52,81610,211],{"class":102},[52,81612,81613,81615,81617,81619,81621,81623],{"class":54,"line":128},[52,81614,13181],{"class":62},[52,81616,186],{"class":102},[52,81618,13072],{"class":189},[52,81620,193],{"class":102},[52,81622,13060],{"class":189},[52,81624,211],{"class":102},[52,81626,81627,81629,81631,81633,81635,81637],{"class":54,"line":148},[52,81628,242],{"class":241},[52,81630,193],{"class":102},[52,81632,115],{"class":83},[52,81634,81163],{"class":87},[52,81636,115],{"class":83},[52,81638,211],{"class":102},[639,81640,24316,81642,81644],{"id":81641},"why-int-is-needed",[49,81643,9492],{}," is needed",[14,81646,81647,81649],{},[49,81648,10597],{}," always returns a string, even if the user types a number.",[14,81651,16291],{},[42,81653,81655],{"className":44,"code":81654,"language":46,"meta":47,"style":47},"text = input(\"Enter seconds: \")\nprint(type(text))\n",[49,81656,81657,81675],{"__ignoreMap":47},[52,81658,81659,81661,81663,81665,81667,81669,81671,81673],{"class":54,"line":55},[52,81660,79346],{"class":62},[52,81662,80],{"class":79},[52,81664,11037],{"class":241},[52,81666,193],{"class":102},[52,81668,115],{"class":83},[52,81670,14191],{"class":87},[52,81672,115],{"class":83},[52,81674,211],{"class":102},[52,81676,81677,81679,81681,81683,81685,81687],{"class":54,"line":66},[52,81678,242],{"class":241},[52,81680,193],{"class":102},[52,81682,4198],{"class":4733},[52,81684,193],{"class":102},[52,81686,955],{"class":189},[52,81688,8886],{"class":102},[14,81690,81691],{},"This prints:",[42,81693,81695],{"className":81694,"code":19510,"language":955,"meta":47},[953],[49,81696,19510],{"__ignoreMap":47},[14,81698,81699,81700,81702,81703,81705],{},"But ",[49,81701,13147],{}," needs a number, not a string. That is why ",[49,81704,9492],{}," is used.",[14,81707,81708,81709,759,81711,186],{},"If you need help with this step, see ",[372,81710,6398],{"href":6397},[372,81712,51945],{"href":13736},[639,81714,3255],{"id":3254},[14,81716,81717,81718,2587,81720,81722],{},"If the user types text like ",[49,81719,6363],{},[49,81721,9492],{}," will fail. This version handles bad input:",[42,81724,81726],{"className":44,"code":81725,"language":46,"meta":47,"style":47},"import time\n\ntry:\n    seconds = int(input(\"Enter the number of seconds: \"))\n\n    if seconds \u003C 0:\n        print(\"Please enter 0 or a positive number.\")\n    else:\n        print(f\"Starting {seconds}-second timer...\")\n        time.sleep(seconds)\n        print(\"Time is up!\")\n\nexcept ValueError:\n    print(\"Please enter a whole number.\")\n",[49,81727,81728,81734,81738,81744,81766,81770,81782,81797,81803,81823,81837,81851,81855,81863],{"__ignoreMap":47},[52,81729,81730,81732],{"class":54,"line":55},[52,81731,59],{"class":58},[52,81733,13019],{"class":62},[52,81735,81736],{"class":54,"line":66},[52,81737,70],{"emptyLinePlaceholder":69},[52,81739,81740,81742],{"class":54,"line":73},[52,81741,3336],{"class":58},[52,81743,1330],{"class":102},[52,81745,81746,81748,81750,81752,81754,81756,81758,81760,81762,81764],{"class":54,"line":94},[52,81747,13083],{"class":62},[52,81749,80],{"class":79},[52,81751,9789],{"class":4733},[52,81753,193],{"class":102},[52,81755,13610],{"class":241},[52,81757,193],{"class":102},[52,81759,115],{"class":83},[52,81761,13617],{"class":87},[52,81763,115],{"class":83},[52,81765,8886],{"class":102},[52,81767,81768],{"class":54,"line":106},[52,81769,70],{"emptyLinePlaceholder":69},[52,81771,81772,81774,81776,81778,81780],{"class":54,"line":128},[52,81773,1438],{"class":58},[52,81775,13043],{"class":62},[52,81777,15294],{"class":79},[52,81779,13049],{"class":121},[52,81781,1330],{"class":102},[52,81783,81784,81786,81788,81790,81793,81795],{"class":54,"line":148},[52,81785,1470],{"class":241},[52,81787,193],{"class":102},[52,81789,115],{"class":83},[52,81791,81792],{"class":87},"Please enter 0 or a positive number.",[52,81794,115],{"class":83},[52,81796,211],{"class":102},[52,81798,81799,81801],{"class":54,"line":164},[52,81800,1567],{"class":58},[52,81802,1330],{"class":102},[52,81804,81805,81807,81809,81811,81813,81815,81817,81819,81821],{"class":54,"line":170},[52,81806,1470],{"class":241},[52,81808,193],{"class":102},[52,81810,1476],{"class":1475},[52,81812,81123],{"class":87},[52,81814,1482],{"class":121},[52,81816,13060],{"class":189},[52,81818,1488],{"class":121},[52,81820,81132],{"class":87},[52,81822,211],{"class":102},[52,81824,81825,81827,81829,81831,81833,81835],{"class":54,"line":175},[52,81826,14010],{"class":62},[52,81828,186],{"class":102},[52,81830,13072],{"class":189},[52,81832,193],{"class":102},[52,81834,13060],{"class":189},[52,81836,211],{"class":102},[52,81838,81839,81841,81843,81845,81847,81849],{"class":54,"line":214},[52,81840,1470],{"class":241},[52,81842,193],{"class":102},[52,81844,115],{"class":83},[52,81846,81163],{"class":87},[52,81848,115],{"class":83},[52,81850,211],{"class":102},[52,81852,81853],{"class":54,"line":233},[52,81854,70],{"emptyLinePlaceholder":69},[52,81856,81857,81859,81861],{"class":54,"line":238},[52,81858,3552],{"class":58},[52,81860,4734],{"class":4733},[52,81862,1330],{"class":102},[52,81864,81865,81867,81869,81871,81873,81875],{"class":54,"line":272},[52,81866,1599],{"class":241},[52,81868,193],{"class":102},[52,81870,115],{"class":83},[52,81872,52610],{"class":87},[52,81874,115],{"class":83},[52,81876,211],{"class":102},[14,81878,81879],{},"This makes the script easier to use because it does not crash on invalid input.",[14,81881,81882,81883,759,81887,186],{},"You can learn more about ",[372,81884,81885],{"href":10606},[49,81886,10597],{},[372,81888,81889],{"href":9489},[49,81890,9492],{},[37,81892,3190],{"id":3189},[14,81894,81895],{},"These are some common reasons a timer script does not work as expected.",[639,81897,14067,81898],{"id":14066},[49,81899,13181],{},[14,81901,6446,81902,14074,81904,17976],{},[49,81903,13147],{},[49,81905,13181],{},[14,81907,4259],{},[42,81909,81911],{"className":44,"code":81910,"language":46,"meta":47,"style":47},"seconds = 2\ntime.sleep(seconds)\n",[49,81912,81913,81921],{"__ignoreMap":47},[52,81914,81915,81917,81919],{"class":54,"line":55},[52,81916,13028],{"class":62},[52,81918,80],{"class":79},[52,81920,45381],{"class":121},[52,81922,81923,81925,81927,81929,81931,81933],{"class":54,"line":66},[52,81924,13181],{"class":62},[52,81926,186],{"class":102},[52,81928,13072],{"class":189},[52,81930,193],{"class":102},[52,81932,13060],{"class":189},[52,81934,211],{"class":102},[14,81936,81937,81938,81940],{},"This fails because ",[49,81939,13181],{}," is not defined.",[14,81942,14146],{},[42,81944,81946],{"className":44,"code":81945,"language":46,"meta":47,"style":47},"import time\n\nseconds = 2\ntime.sleep(seconds)\n",[49,81947,81948,81954,81958,81966],{"__ignoreMap":47},[52,81949,81950,81952],{"class":54,"line":55},[52,81951,59],{"class":58},[52,81953,13019],{"class":62},[52,81955,81956],{"class":54,"line":66},[52,81957,70],{"emptyLinePlaceholder":69},[52,81959,81960,81962,81964],{"class":54,"line":73},[52,81961,13028],{"class":62},[52,81963,80],{"class":79},[52,81965,45381],{"class":121},[52,81967,81968,81970,81972,81974,81976,81978],{"class":54,"line":94},[52,81969,13181],{"class":62},[52,81971,186],{"class":102},[52,81973,13072],{"class":189},[52,81975,193],{"class":102},[52,81977,13060],{"class":189},[52,81979,211],{"class":102},[639,81981,81983,81984],{"id":81982},"passing-a-string-to-timesleep","Passing a string to ",[49,81985,13147],{},[14,81987,37426,81988,186],{},[49,81989,1707],{},[14,81991,4259],{},[42,81993,81995],{"className":44,"code":81994,"language":46,"meta":47,"style":47},"import time\n\nseconds = \"5\"\ntime.sleep(seconds)\n",[49,81996,81997,82003,82007,82019],{"__ignoreMap":47},[52,81998,81999,82001],{"class":54,"line":55},[52,82000,59],{"class":58},[52,82002,13019],{"class":62},[52,82004,82005],{"class":54,"line":66},[52,82006,70],{"emptyLinePlaceholder":69},[52,82008,82009,82011,82013,82015,82017],{"class":54,"line":73},[52,82010,13028],{"class":62},[52,82012,80],{"class":79},[52,82014,84],{"class":83},[52,82016,9479],{"class":87},[52,82018,91],{"class":83},[52,82020,82021,82023,82025,82027,82029,82031],{"class":54,"line":94},[52,82022,13181],{"class":62},[52,82024,186],{"class":102},[52,82026,13072],{"class":189},[52,82028,193],{"class":102},[52,82030,13060],{"class":189},[52,82032,211],{"class":102},[14,82034,82035],{},"Fix it by converting the value first:",[42,82037,82039],{"className":44,"code":82038,"language":46,"meta":47,"style":47},"import time\n\nseconds = int(\"5\")\ntime.sleep(seconds)\n",[49,82040,82041,82047,82051,82069],{"__ignoreMap":47},[52,82042,82043,82045],{"class":54,"line":55},[52,82044,59],{"class":58},[52,82046,13019],{"class":62},[52,82048,82049],{"class":54,"line":66},[52,82050,70],{"emptyLinePlaceholder":69},[52,82052,82053,82055,82057,82059,82061,82063,82065,82067],{"class":54,"line":73},[52,82054,13028],{"class":62},[52,82056,80],{"class":79},[52,82058,9789],{"class":4733},[52,82060,193],{"class":102},[52,82062,115],{"class":83},[52,82064,9479],{"class":87},[52,82066,115],{"class":83},[52,82068,211],{"class":102},[52,82070,82071,82073,82075,82077,82079,82081],{"class":54,"line":94},[52,82072,13181],{"class":62},[52,82074,186],{"class":102},[52,82076,13072],{"class":189},[52,82078,193],{"class":102},[52,82080,13060],{"class":189},[52,82082,211],{"class":102},[14,82084,50651,82085],{},[372,82086,82087,82088],{"href":6886},"fix ",[49,82089,82090],{},"TypeError: 'str' object cannot be interpreted as an integer",[639,82092,82094],{"id":82093},"invalid-text-entered-instead-of-a-number","Invalid text entered instead of a number",[14,82096,82097,82098,186],{},"This often happens with ",[49,82099,10597],{},[14,82101,4259],{},[42,82103,82104],{"className":44,"code":14203,"language":46,"meta":47,"style":47},[49,82105,82106],{"__ignoreMap":47},[52,82107,82108,82110,82112,82114,82116,82118,82120,82122,82124,82126],{"class":54,"line":55},[52,82109,13028],{"class":62},[52,82111,80],{"class":79},[52,82113,9789],{"class":4733},[52,82115,193],{"class":102},[52,82117,13610],{"class":241},[52,82119,193],{"class":102},[52,82121,115],{"class":83},[52,82123,14191],{"class":87},[52,82125,115],{"class":83},[52,82127,8886],{"class":102},[14,82129,82130,82131,78611,82133,186],{},"If the user types ",[49,82132,60349],{},[49,82134,13780],{},[14,82136,59257,82137,759,82139,82141],{},[49,82138,3336],{},[49,82140,3552],{},", as shown earlier.",[14,82143,50651,82144],{},[372,82145,82087,82146],{"href":6471},[49,82147,6472],{},[639,82149,82151],{"id":82150},"negative-values-are-not-useful-for-a-timer","Negative values are not useful for a timer",[14,82153,82154],{},"A negative timer does not make sense for most beginner scripts.",[14,82156,82157],{},"Check for this before running the timer:",[42,82159,82161],{"className":44,"code":82160,"language":46,"meta":47,"style":47},"seconds = int(input(\"Enter seconds: \"))\n\nif seconds \u003C 0:\n    print(\"Seconds cannot be negative.\")\nelse:\n    print(\"Valid timer value\")\n",[49,82162,82163,82185,82189,82201,82216,82222],{"__ignoreMap":47},[52,82164,82165,82167,82169,82171,82173,82175,82177,82179,82181,82183],{"class":54,"line":55},[52,82166,13028],{"class":62},[52,82168,80],{"class":79},[52,82170,9789],{"class":4733},[52,82172,193],{"class":102},[52,82174,13610],{"class":241},[52,82176,193],{"class":102},[52,82178,115],{"class":83},[52,82180,14191],{"class":87},[52,82182,115],{"class":83},[52,82184,8886],{"class":102},[52,82186,82187],{"class":54,"line":66},[52,82188,70],{"emptyLinePlaceholder":69},[52,82190,82191,82193,82195,82197,82199],{"class":54,"line":73},[52,82192,1313],{"class":58},[52,82194,13043],{"class":62},[52,82196,15294],{"class":79},[52,82198,13049],{"class":121},[52,82200,1330],{"class":102},[52,82202,82203,82205,82207,82209,82212,82214],{"class":54,"line":94},[52,82204,1599],{"class":241},[52,82206,193],{"class":102},[52,82208,115],{"class":83},[52,82210,82211],{"class":87},"Seconds cannot be negative.",[52,82213,115],{"class":83},[52,82215,211],{"class":102},[52,82217,82218,82220],{"class":54,"line":106},[52,82219,1591],{"class":58},[52,82221,1330],{"class":102},[52,82223,82224,82226,82228,82230,82233,82235],{"class":54,"line":128},[52,82225,1599],{"class":241},[52,82227,193],{"class":102},[52,82229,115],{"class":83},[52,82231,82232],{"class":87},"Valid timer value",[52,82234,115],{"class":83},[52,82236,211],{"class":102},[639,82238,82240,82241,82243],{"id":82239},"wrong-range-values-in-a-countdown-loop","Wrong ",[49,82242,37187],{}," values in a countdown loop",[14,82245,82246],{},"If the countdown does not work, check the loop carefully.",[14,82248,12506],{},[42,82250,82252],{"className":44,"code":82251,"language":46,"meta":47,"style":47},"for remaining in range(seconds, 0, -1):\n    print(remaining)\n",[49,82253,82254,82280],{"__ignoreMap":47},[52,82255,82256,82258,82260,82262,82264,82266,82268,82270,82272,82274,82276,82278],{"class":54,"line":55},[52,82257,12265],{"class":58},[52,82259,81408],{"class":62},[52,82261,11213],{"class":58},[52,82263,36864],{"class":241},[52,82265,193],{"class":102},[52,82267,13060],{"class":189},[52,82269,199],{"class":102},[52,82271,13049],{"class":121},[52,82273,199],{"class":102},[52,82275,140],{"class":79},[52,82277,9200],{"class":121},[52,82279,3114],{"class":102},[52,82281,82282,82284,82286,82288],{"class":54,"line":66},[52,82283,1599],{"class":241},[52,82285,193],{"class":102},[52,82287,81443],{"class":189},[52,82289,211],{"class":102},[14,82291,82292],{},"A common mistake is using the wrong start, stop, or step value.",[639,82294,14279,82296,82298],{"id":82295},"expecting-sleep-to-show-progress-by-itself",[49,82297,12988],{}," to show progress by itself",[14,82300,82301,82303],{},[49,82302,13147],{}," only pauses the program. It does not display progress.",[14,82305,82306],{},"If you want visible updates, you need a loop:",[42,82308,82310],{"className":44,"code":82309,"language":46,"meta":47,"style":47},"import time\n\nfor i in range(3, 0, -1):\n    print(i)\n    time.sleep(1)\n",[49,82311,82312,82318,82322,82348,82359],{"__ignoreMap":47},[52,82313,82314,82316],{"class":54,"line":55},[52,82315,59],{"class":58},[52,82317,13019],{"class":62},[52,82319,82320],{"class":54,"line":66},[52,82321,70],{"emptyLinePlaceholder":69},[52,82323,82324,82326,82328,82330,82332,82334,82336,82338,82340,82342,82344,82346],{"class":54,"line":73},[52,82325,12265],{"class":58},[52,82327,36859],{"class":62},[52,82329,11213],{"class":58},[52,82331,36864],{"class":241},[52,82333,193],{"class":102},[52,82335,7508],{"class":121},[52,82337,199],{"class":102},[52,82339,13049],{"class":121},[52,82341,199],{"class":102},[52,82343,140],{"class":79},[52,82345,9200],{"class":121},[52,82347,3114],{"class":102},[52,82349,82350,82352,82354,82357],{"class":54,"line":94},[52,82351,1599],{"class":241},[52,82353,193],{"class":102},[52,82355,82356],{"class":189},"i",[52,82358,211],{"class":102},[52,82360,82361,82363,82365,82367,82369,82371],{"class":54,"line":106},[52,82362,13067],{"class":62},[52,82364,186],{"class":102},[52,82366,13072],{"class":189},[52,82368,193],{"class":102},[52,82370,9200],{"class":121},[52,82372,211],{"class":102},[639,82374,82376],{"id":82375},"useful-debugging-commands","Useful debugging commands",[14,82378,82379],{},"These can help when your timer is not working:",[42,82381,82383],{"className":390,"code":82382,"language":392,"meta":47,"style":47},"python timer.py\n",[49,82384,82385],{"__ignoreMap":47},[52,82386,82387,82389],{"class":54,"line":55},[52,82388,46],{"class":399},[52,82390,82391],{"class":87}," timer.py\n",[42,82393,82395],{"className":44,"code":82394,"language":46,"meta":47,"style":47},"print(type(seconds))\nprint(seconds)\nhelp(time.sleep)\n",[49,82396,82397,82411,82421],{"__ignoreMap":47},[52,82398,82399,82401,82403,82405,82407,82409],{"class":54,"line":55},[52,82400,242],{"class":241},[52,82402,193],{"class":102},[52,82404,4198],{"class":4733},[52,82406,193],{"class":102},[52,82408,13060],{"class":189},[52,82410,8886],{"class":102},[52,82412,82413,82415,82417,82419],{"class":54,"line":66},[52,82414,242],{"class":241},[52,82416,193],{"class":102},[52,82418,13060],{"class":189},[52,82420,211],{"class":102},[52,82422,82423,82425,82427,82429,82431,82433],{"class":54,"line":73},[52,82424,8930],{"class":241},[52,82426,193],{"class":102},[52,82428,13181],{"class":189},[52,82430,186],{"class":102},[52,82432,13072],{"class":1320},[52,82434,211],{"class":102},[14,82436,82437],{},"These checks can help you confirm:",[309,82439,82440,82446,82449],{},[24,82441,82442,82443,82445],{},"Whether ",[49,82444,13060],{}," is really a number",[24,82447,82448],{},"What value it contains",[24,82450,4983,82451,82453],{},[49,82452,13147],{}," is supposed to work",[37,82455,82457],{"id":82456},"how-to-improve-this-script","How to improve this script",[14,82459,82460],{},"Once the basic version works, you can build on it.",[14,82462,82463],{},"Some useful improvements are:",[309,82465,82466,82471,82474,82477],{},[24,82467,82468,82469],{},"Add input validation with ",[49,82470,3603],{},[24,82472,82473],{},"Display minutes and seconds instead of only seconds",[24,82475,82476],{},"Play a sound or print a stronger alert at the end",[24,82478,82479],{},"Turn the timer code into a function for reuse",[14,82481,82482],{},"Here is one small improvement: turning the timer into a function.",[42,82484,82486],{"className":44,"code":82485,"language":46,"meta":47,"style":47},"import time\n\ndef start_timer(seconds):\n    print(f\"Starting {seconds}-second timer...\")\n    time.sleep(seconds)\n    print(\"Time is up!\")\n\nstart_timer(4)\n",[49,82487,82488,82494,82498,82511,82531,82545,82559,82563],{"__ignoreMap":47},[52,82489,82490,82492],{"class":54,"line":55},[52,82491,59],{"class":58},[52,82493,13019],{"class":62},[52,82495,82496],{"class":54,"line":66},[52,82497,70],{"emptyLinePlaceholder":69},[52,82499,82500,82502,82505,82507,82509],{"class":54,"line":73},[52,82501,7410],{"class":1475},[52,82503,82504],{"class":7065}," start_timer",[52,82506,193],{"class":102},[52,82508,13060],{"class":7018},[52,82510,3114],{"class":102},[52,82512,82513,82515,82517,82519,82521,82523,82525,82527,82529],{"class":54,"line":94},[52,82514,1599],{"class":241},[52,82516,193],{"class":102},[52,82518,1476],{"class":1475},[52,82520,81123],{"class":87},[52,82522,1482],{"class":121},[52,82524,13060],{"class":189},[52,82526,1488],{"class":121},[52,82528,81132],{"class":87},[52,82530,211],{"class":102},[52,82532,82533,82535,82537,82539,82541,82543],{"class":54,"line":106},[52,82534,13067],{"class":62},[52,82536,186],{"class":102},[52,82538,13072],{"class":189},[52,82540,193],{"class":102},[52,82542,13060],{"class":189},[52,82544,211],{"class":102},[52,82546,82547,82549,82551,82553,82555,82557],{"class":54,"line":128},[52,82548,1599],{"class":241},[52,82550,193],{"class":102},[52,82552,115],{"class":83},[52,82554,81163],{"class":87},[52,82556,115],{"class":83},[52,82558,211],{"class":102},[52,82560,82561],{"class":54,"line":148},[52,82562,70],{"emptyLinePlaceholder":69},[52,82564,82565,82568,82570,82572],{"class":54,"line":164},[52,82566,82567],{"class":189},"start_timer",[52,82569,193],{"class":102},[52,82571,11366],{"class":121},[52,82573,211],{"class":102},[14,82575,82576],{},"This is useful when you want to reuse the same logic more than once in a program.",[14,82578,82579,82580,186],{},"If you want a broader overview, see the ",[372,82581,13186],{"href":13185},[37,82583,1942],{"id":1941},[639,82585,82587],{"id":82586},"how-do-i-make-a-countdown-timer-in-python","How do I make a countdown timer in Python?",[14,82589,82590,82591,186],{},"Use a loop that prints the remaining seconds and pauses for 1 second with ",[49,82592,13344],{},[639,82594,82596],{"id":82595},"why-does-my-timer-not-wait","Why does my timer not wait?",[14,82598,82599,82600,82602],{},"Check that you are calling ",[49,82601,13147],{}," with a number and that the code is actually being reached.",[639,82604,3677,82606,82608,82609,1995],{"id":82605},"why-does-input-not-work-with-timesleep",[49,82607,10597],{}," not work with ",[49,82610,13147],{},[14,82612,82613,82615,82616,857,82618,82620,82621,186],{},[49,82614,10597],{}," returns a string. Convert it first with ",[49,82617,9492],{},[49,82619,68359],{}," before passing it to ",[49,82622,12988],{},[639,82624,82626,82627,82629],{"id":82625},"can-timesleep-use-decimal-values","Can ",[49,82628,13147],{}," use decimal values?",[14,82631,82632,82633,857,82636,82638],{},"Yes. You can use values like ",[49,82634,82635],{},"0.5",[49,82637,37750],{}," for fractions of a second.",[37,82640,2005],{"id":2004},[309,82642,82643,82647,82653,82659,82665,82669],{},[24,82644,82645],{},[372,82646,13186],{"href":13185},[24,82648,82649],{},[372,82650,82651,2866],{"href":13144},[49,82652,13147],{},[24,82654,82655],{},[372,82656,10646,82657,3711],{"href":10606},[49,82658,10597],{},[24,82660,82661],{},[372,82662,10646,82663,3711],{"href":9489},[49,82664,9492],{},[24,82666,82667],{},[372,82668,13737],{"href":13736},[24,82670,82671],{},[372,82672,79247],{"href":79246},[14,82674,82675],{},"Try changing the timer so the user chooses the number of seconds, then turn it into a countdown script. That is a good next step after this basic example.",[2034,82677,82678],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--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 .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 pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .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}",{"title":47,"searchDepth":66,"depth":66,"links":82680},[82681,82682,82683,82687,82691,82696,82708,82709,82717],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":81200,"depth":66,"text":81201,"children":82684},[82685,82686],{"id":24094,"depth":73,"text":24095},{"id":13562,"depth":73,"text":13563},{"id":81348,"depth":66,"text":81349,"children":82688},[82689,82690],{"id":9909,"depth":73,"text":9910},{"id":80222,"depth":73,"text":13563},{"id":81540,"depth":66,"text":81541,"children":82692},[82693,82695],{"id":81641,"depth":73,"text":82694},"Why int() is needed",{"id":3254,"depth":73,"text":3255},{"id":3189,"depth":66,"text":3190,"children":82697},[82698,82699,82701,82702,82703,82705,82707],{"id":14066,"depth":73,"text":14549},{"id":81982,"depth":73,"text":82700},"Passing a string to time.sleep()",{"id":82093,"depth":73,"text":82094},{"id":82150,"depth":73,"text":82151},{"id":82239,"depth":73,"text":82704},"Wrong range() values in a countdown loop",{"id":82295,"depth":73,"text":82706},"Expecting sleep() to show progress by itself",{"id":82375,"depth":73,"text":82376},{"id":82456,"depth":66,"text":82457},{"id":1941,"depth":66,"text":1942,"children":82710},[82711,82712,82713,82715],{"id":82586,"depth":73,"text":82587},{"id":82595,"depth":73,"text":82596},{"id":82605,"depth":73,"text":82714},"Why does input() not work with time.sleep()?",{"id":82625,"depth":73,"text":82716},"Can time.sleep() use decimal values?",{"id":2004,"depth":66,"text":2005},"Master python timer script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-timer-script-example",{"title":81074,"description":82718},"examples\u002Fpython-timer-script-example","7bqLAERCMUre11YpeytlJK9liJcI3xHwrdmWl1x5xik",{"id":82725,"title":82726,"body":82727,"description":85582,"extension":2075,"meta":85583,"navigation":69,"path":85584,"seo":85585,"stem":85586,"__hash__":85587},"content\u002Fexamples\u002Fpython-to-do-list-script-example.md","Python To-Do List Script Example",{"type":7,"value":82728,"toc":85557},[82729,82732,82735,82738,82761,82764,82766,82769,82783,82787,82790,82826,82837,82841,82844,83390,83394,83400,83404,83407,83420,83423,83426,83442,83444,83513,83515,83521,83527,83536,83540,83543,83555,83558,83566,83569,83580,83589,83763,83766,83777,83781,83784,83836,83839,83858,83860,83923,83929,83935,83938,84027,84031,84034,84063,84066,84069,84130,84135,84151,84153,84251,84253,84259,84267,84271,84274,84277,84337,84340,84367,84370,84381,84384,84432,84437,84448,84450,84564,84566,84572,84583,84585,84603,84607,84610,84613,85272,85275,85291,85297,85301,85303,85306,85323,85329,85331,85334,85362,85364,85367,85444,85447,85464,85469,85476,85478,85485,85488,85492,85495,85499,85508,85512,85515,85517,85551,85554],[10,82730,82726],{"id":82731},"python-to-do-list-script-example",[14,82733,82734],{},"This beginner-friendly project shows how to build a simple command-line to-do list script in Python.",[14,82736,82737],{},"It is a good example because it combines several core Python skills in one small program:",[309,82739,82740,82743,82745,82749,82753],{},[24,82741,82742],{},"lists",[24,82744,59595],{},[24,82746,82747],{},[49,82748,10597],{},[24,82750,82751,30419],{},[49,82752,1313],{},[24,82754,82755,82756,759,82758],{},"functions like ",[49,82757,34074],{},[49,82759,82760],{},"pop()",[14,82762,82763],{},"If you are still learning these basics, this example helps you see how they work together in a real script.",[37,82765,28680],{"id":28679},[14,82767,82768],{},"This program:",[309,82770,82771,82774,82777,82780],{},[24,82772,82773],{},"Creates a simple text-based to-do list",[24,82775,82776],{},"Lets the user view tasks, add tasks, and remove tasks",[24,82778,82779],{},"Stores tasks in a Python list",[24,82781,82782],{},"Runs in a loop until the user chooses to quit",[37,82784,82786],{"id":82785},"skills-this-example-teaches","Skills this example teaches",[14,82788,82789],{},"By reading and running this script, you practice:",[309,82791,82792,82795,82800,82805,82814,82821],{},[24,82793,82794],{},"How to store items in a list",[24,82796,70164,82797,82799],{},[49,82798,10597],{}," to get user choices",[24,82801,70164,82802,82804],{},[49,82803,10900],{}," for a menu loop",[24,82806,70164,82807,2587,82809,11556,82811,82813],{},[49,82808,1313],{},[49,82810,10787],{},[49,82812,1591],{}," to handle options",[24,82815,70164,82816,759,82818,82820],{},[49,82817,34074],{},[49,82819,82760],{}," with lists",[24,82822,82823,82824],{},"How to display numbered items with ",[49,82825,66890],{},[14,82827,82828,82829,2587,82831,11556,82834,186],{},"If you need more background first, see ",[372,82830,44583],{"href":44582},[372,82832,13426],{"href":82833},"\u002Flearn\u002Fpython-while-loops-explained",[372,82835,52086],{"href":82836},"\u002Flearn\u002Fpython-if-else-and-elif-explained",[37,82838,82840],{"id":82839},"the-complete-to-do-list-script","The complete to-do list script",[14,82842,82843],{},"Here is the full runnable script:",[42,82845,82847],{"className":44,"code":82846,"language":46,"meta":47,"style":47},"tasks = []\n\nwhile True:\n    print(\"\\nTo-Do List\")\n    print(\"1. View tasks\")\n    print(\"2. Add task\")\n    print(\"3. Remove task\")\n    print(\"4. Quit\")\n\n    choice = input(\"Choose an option: \")\n\n    if choice == \"1\":\n        if not tasks:\n            print(\"No tasks yet.\")\n        else:\n            for index, task in enumerate(tasks, start=1):\n                print(f\"{index}. {task}\")\n\n    elif choice == \"2\":\n        task = input(\"Enter a task: \")\n        tasks.append(task)\n        print(\"Task added.\")\n\n    elif choice == \"3\":\n        if not tasks:\n            print(\"No tasks to remove.\")\n        else:\n            for index, task in enumerate(tasks, start=1):\n                print(f\"{index}. {task}\")\n            number = int(input(\"Enter task number to remove: \"))\n            removed = tasks.pop(number - 1)\n            print(f\"Removed: {removed}\")\n\n    elif choice == \"4\":\n        print(\"Goodbye!\")\n        break\n\n    else:\n        print(\"Invalid choice.\")\n",[49,82848,82849,82858,82862,82870,82887,82902,82917,82932,82947,82951,82969,82973,82989,83000,83015,83021,83051,83080,83084,83100,83120,83135,83150,83154,83170,83180,83195,83201,83229,83257,83281,83305,83327,83331,83347,83361,83365,83369,83375],{"__ignoreMap":47},[52,82850,82851,82854,82856],{"class":54,"line":55},[52,82852,82853],{"class":62},"tasks ",[52,82855,80],{"class":79},[52,82857,18590],{"class":102},[52,82859,82860],{"class":54,"line":66},[52,82861,70],{"emptyLinePlaceholder":69},[52,82863,82864,82866,82868],{"class":54,"line":73},[52,82865,10772],{"class":58},[52,82867,10943],{"class":160},[52,82869,1330],{"class":102},[52,82871,82872,82874,82876,82878,82880,82883,82885],{"class":54,"line":94},[52,82873,1599],{"class":241},[52,82875,193],{"class":102},[52,82877,115],{"class":83},[52,82879,10956],{"class":2533},[52,82881,82882],{"class":87},"To-Do List",[52,82884,115],{"class":83},[52,82886,211],{"class":102},[52,82888,82889,82891,82893,82895,82898,82900],{"class":54,"line":106},[52,82890,1599],{"class":241},[52,82892,193],{"class":102},[52,82894,115],{"class":83},[52,82896,82897],{"class":87},"1. View tasks",[52,82899,115],{"class":83},[52,82901,211],{"class":102},[52,82903,82904,82906,82908,82910,82913,82915],{"class":54,"line":128},[52,82905,1599],{"class":241},[52,82907,193],{"class":102},[52,82909,115],{"class":83},[52,82911,82912],{"class":87},"2. Add task",[52,82914,115],{"class":83},[52,82916,211],{"class":102},[52,82918,82919,82921,82923,82925,82928,82930],{"class":54,"line":148},[52,82920,1599],{"class":241},[52,82922,193],{"class":102},[52,82924,115],{"class":83},[52,82926,82927],{"class":87},"3. Remove task",[52,82929,115],{"class":83},[52,82931,211],{"class":102},[52,82933,82934,82936,82938,82940,82943,82945],{"class":54,"line":164},[52,82935,1599],{"class":241},[52,82937,193],{"class":102},[52,82939,115],{"class":83},[52,82941,82942],{"class":87},"4. Quit",[52,82944,115],{"class":83},[52,82946,211],{"class":102},[52,82948,82949],{"class":54,"line":170},[52,82950,70],{"emptyLinePlaceholder":69},[52,82952,82953,82955,82957,82959,82961,82963,82965,82967],{"class":54,"line":175},[52,82954,11032],{"class":62},[52,82956,80],{"class":79},[52,82958,11037],{"class":241},[52,82960,193],{"class":102},[52,82962,115],{"class":83},[52,82964,11044],{"class":87},[52,82966,115],{"class":83},[52,82968,211],{"class":102},[52,82970,82971],{"class":54,"line":214},[52,82972,70],{"emptyLinePlaceholder":69},[52,82974,82975,82977,82979,82981,82983,82985,82987],{"class":54,"line":233},[52,82976,1438],{"class":58},[52,82978,11059],{"class":62},[52,82980,11062],{"class":79},[52,82982,84],{"class":83},[52,82984,9200],{"class":87},[52,82986,115],{"class":83},[52,82988,1330],{"class":102},[52,82990,82991,82993,82995,82998],{"class":54,"line":238},[52,82992,11168],{"class":58},[52,82994,1447],{"class":79},[52,82996,82997],{"class":62}," tasks",[52,82999,1330],{"class":102},[52,83001,83002,83004,83006,83008,83011,83013],{"class":54,"line":272},[52,83003,11180],{"class":241},[52,83005,193],{"class":102},[52,83007,115],{"class":83},[52,83009,83010],{"class":87},"No tasks yet.",[52,83012,115],{"class":83},[52,83014,211],{"class":102},[52,83016,83017,83019],{"class":54,"line":1348},[52,83018,11196],{"class":58},[52,83020,1330],{"class":102},[52,83022,83023,83025,83027,83029,83032,83034,83036,83038,83041,83043,83045,83047,83049],{"class":54,"line":1376},[52,83024,11203],{"class":58},[52,83026,64277],{"class":62},[52,83028,199],{"class":102},[52,83030,83031],{"class":62}," task ",[52,83033,11213],{"class":58},[52,83035,49274],{"class":241},[52,83037,193],{"class":102},[52,83039,83040],{"class":189},"tasks",[52,83042,199],{"class":102},[52,83044,64300],{"class":202},[52,83046,80],{"class":79},[52,83048,9200],{"class":121},[52,83050,3114],{"class":102},[52,83052,83053,83055,83057,83059,83061,83063,83065,83067,83069,83071,83074,83076,83078],{"class":54,"line":1381},[52,83054,11227],{"class":241},[52,83056,193],{"class":102},[52,83058,1476],{"class":1475},[52,83060,115],{"class":87},[52,83062,1482],{"class":121},[52,83064,64335],{"class":189},[52,83066,1488],{"class":121},[52,83068,69957],{"class":87},[52,83070,1482],{"class":121},[52,83072,83073],{"class":189},"task",[52,83075,1488],{"class":121},[52,83077,115],{"class":87},[52,83079,211],{"class":102},[52,83081,83082],{"class":54,"line":1406},[52,83083,70],{"emptyLinePlaceholder":69},[52,83085,83086,83088,83090,83092,83094,83096,83098],{"class":54,"line":1430},[52,83087,11151],{"class":58},[52,83089,11059],{"class":62},[52,83091,11062],{"class":79},[52,83093,84],{"class":83},[52,83095,9825],{"class":87},[52,83097,115],{"class":83},[52,83099,1330],{"class":102},[52,83101,83102,83105,83107,83109,83111,83113,83116,83118],{"class":54,"line":1435},[52,83103,83104],{"class":62},"        task ",[52,83106,80],{"class":79},[52,83108,11037],{"class":241},[52,83110,193],{"class":102},[52,83112,115],{"class":83},[52,83114,83115],{"class":87},"Enter a task: ",[52,83117,115],{"class":83},[52,83119,211],{"class":102},[52,83121,83122,83125,83127,83129,83131,83133],{"class":54,"line":1467},[52,83123,83124],{"class":62},"        tasks",[52,83126,186],{"class":102},[52,83128,18748],{"class":189},[52,83130,193],{"class":102},[52,83132,83073],{"class":189},[52,83134,211],{"class":102},[52,83136,83137,83139,83141,83143,83146,83148],{"class":54,"line":1495},[52,83138,1470],{"class":241},[52,83140,193],{"class":102},[52,83142,115],{"class":83},[52,83144,83145],{"class":87},"Task added.",[52,83147,115],{"class":83},[52,83149,211],{"class":102},[52,83151,83152],{"class":54,"line":1531},[52,83153,70],{"emptyLinePlaceholder":69},[52,83155,83156,83158,83160,83162,83164,83166,83168],{"class":54,"line":1564},[52,83157,11151],{"class":58},[52,83159,11059],{"class":62},[52,83161,11062],{"class":79},[52,83163,84],{"class":83},[52,83165,7508],{"class":87},[52,83167,115],{"class":83},[52,83169,1330],{"class":102},[52,83171,83172,83174,83176,83178],{"class":54,"line":1572},[52,83173,11168],{"class":58},[52,83175,1447],{"class":79},[52,83177,82997],{"class":62},[52,83179,1330],{"class":102},[52,83181,83182,83184,83186,83188,83191,83193],{"class":54,"line":1588},[52,83183,11180],{"class":241},[52,83185,193],{"class":102},[52,83187,115],{"class":83},[52,83189,83190],{"class":87},"No tasks to remove.",[52,83192,115],{"class":83},[52,83194,211],{"class":102},[52,83196,83197,83199],{"class":54,"line":1596},[52,83198,11196],{"class":58},[52,83200,1330],{"class":102},[52,83202,83203,83205,83207,83209,83211,83213,83215,83217,83219,83221,83223,83225,83227],{"class":54,"line":1613},[52,83204,11203],{"class":58},[52,83206,64277],{"class":62},[52,83208,199],{"class":102},[52,83210,83031],{"class":62},[52,83212,11213],{"class":58},[52,83214,49274],{"class":241},[52,83216,193],{"class":102},[52,83218,83040],{"class":189},[52,83220,199],{"class":102},[52,83222,64300],{"class":202},[52,83224,80],{"class":79},[52,83226,9200],{"class":121},[52,83228,3114],{"class":102},[52,83230,83231,83233,83235,83237,83239,83241,83243,83245,83247,83249,83251,83253,83255],{"class":54,"line":11327},[52,83232,11227],{"class":241},[52,83234,193],{"class":102},[52,83236,1476],{"class":1475},[52,83238,115],{"class":87},[52,83240,1482],{"class":121},[52,83242,64335],{"class":189},[52,83244,1488],{"class":121},[52,83246,69957],{"class":87},[52,83248,1482],{"class":121},[52,83250,83073],{"class":189},[52,83252,1488],{"class":121},[52,83254,115],{"class":87},[52,83256,211],{"class":102},[52,83258,83259,83262,83264,83266,83268,83270,83272,83274,83277,83279],{"class":54,"line":11334},[52,83260,83261],{"class":62},"            number ",[52,83263,80],{"class":79},[52,83265,9789],{"class":4733},[52,83267,193],{"class":102},[52,83269,13610],{"class":241},[52,83271,193],{"class":102},[52,83273,115],{"class":83},[52,83275,83276],{"class":87},"Enter task number to remove: ",[52,83278,115],{"class":83},[52,83280,8886],{"class":102},[52,83282,83283,83286,83288,83290,83292,83295,83297,83299,83301,83303],{"class":54,"line":11350},[52,83284,83285],{"class":62},"            removed ",[52,83287,80],{"class":79},[52,83289,82997],{"class":62},[52,83291,186],{"class":102},[52,83293,83294],{"class":189},"pop",[52,83296,193],{"class":102},[52,83298,32574],{"class":189},[52,83300,5889],{"class":79},[52,83302,1518],{"class":121},[52,83304,211],{"class":102},[52,83306,83307,83309,83311,83313,83316,83318,83321,83323,83325],{"class":54,"line":11355},[52,83308,11180],{"class":241},[52,83310,193],{"class":102},[52,83312,1476],{"class":1475},[52,83314,83315],{"class":87},"\"Removed: ",[52,83317,1482],{"class":121},[52,83319,83320],{"class":189},"removed",[52,83322,1488],{"class":121},[52,83324,115],{"class":87},[52,83326,211],{"class":102},[52,83328,83329],{"class":54,"line":11373},[52,83330,70],{"emptyLinePlaceholder":69},[52,83332,83333,83335,83337,83339,83341,83343,83345],{"class":54,"line":11393},[52,83334,11151],{"class":58},[52,83336,11059],{"class":62},[52,83338,11062],{"class":79},[52,83340,84],{"class":83},[52,83342,11366],{"class":87},[52,83344,115],{"class":83},[52,83346,1330],{"class":102},[52,83348,83349,83351,83353,83355,83357,83359],{"class":54,"line":11406},[52,83350,1470],{"class":241},[52,83352,193],{"class":102},[52,83354,115],{"class":83},[52,83356,11489],{"class":87},[52,83358,115],{"class":83},[52,83360,211],{"class":102},[52,83362,83363],{"class":54,"line":11420},[52,83364,11499],{"class":58},[52,83366,83367],{"class":54,"line":11436},[52,83368,70],{"emptyLinePlaceholder":69},[52,83370,83371,83373],{"class":54,"line":11443},[52,83372,1567],{"class":58},[52,83374,1330],{"class":102},[52,83376,83377,83379,83381,83383,83386,83388],{"class":54,"line":11458},[52,83378,1470],{"class":241},[52,83380,193],{"class":102},[52,83382,115],{"class":83},[52,83384,83385],{"class":87},"Invalid choice.",[52,83387,115],{"class":83},[52,83389,211],{"class":102},[639,83391,83393],{"id":83392},"sample-interaction","Sample interaction",[42,83395,83398],{"className":83396,"code":83397,"language":955,"meta":47},[953],"To-Do List\n1. View tasks\n2. Add task\n3. Remove task\n4. Quit\nChoose an option: 2\nEnter a task: Buy milk\nTask added.\n\nTo-Do List\n1. View tasks\n2. Add task\n3. Remove task\n4. Quit\nChoose an option: 1\n1. Buy milk\n\nTo-Do List\n1. View tasks\n2. Add task\n3. Remove task\n4. Quit\nChoose an option: 3\n1. Buy milk\nEnter task number to remove: 1\nRemoved: Buy milk\n",[49,83399,83397],{"__ignoreMap":47},[37,83401,83403],{"id":83402},"how-the-task-list-is-stored","How the task list is stored",[14,83405,83406],{},"The line below creates an empty list:",[42,83408,83410],{"className":44,"code":83409,"language":46,"meta":47,"style":47},"tasks = []\n",[49,83411,83412],{"__ignoreMap":47},[52,83413,83414,83416,83418],{"class":54,"line":55},[52,83415,82853],{"class":62},[52,83417,80],{"class":79},[52,83419,18590],{"class":102},[14,83421,83422],{},"This list holds all tasks while the program is running.",[14,83424,83425],{},"Important points:",[309,83427,83428,83433,83436,83439],{},[24,83429,83430,83432],{},[49,83431,83040],{}," starts empty",[24,83434,83435],{},"Each task is stored as a string",[24,83437,83438],{},"New tasks are added to the end of the list",[24,83440,83441],{},"Removing a task changes the list only in memory",[14,83443,16291],{},[42,83445,83447],{"className":44,"code":83446,"language":46,"meta":47,"style":47},"tasks = []\n\ntasks.append(\"Buy milk\")\ntasks.append(\"Study Python\")\n\nprint(tasks)\n",[49,83448,83449,83457,83461,83480,83499,83503],{"__ignoreMap":47},[52,83450,83451,83453,83455],{"class":54,"line":55},[52,83452,82853],{"class":62},[52,83454,80],{"class":79},[52,83456,18590],{"class":102},[52,83458,83459],{"class":54,"line":66},[52,83460,70],{"emptyLinePlaceholder":69},[52,83462,83463,83465,83467,83469,83471,83473,83476,83478],{"class":54,"line":73},[52,83464,83040],{"class":62},[52,83466,186],{"class":102},[52,83468,18748],{"class":189},[52,83470,193],{"class":102},[52,83472,115],{"class":83},[52,83474,83475],{"class":87},"Buy milk",[52,83477,115],{"class":83},[52,83479,211],{"class":102},[52,83481,83482,83484,83486,83488,83490,83492,83495,83497],{"class":54,"line":94},[52,83483,83040],{"class":62},[52,83485,186],{"class":102},[52,83487,18748],{"class":189},[52,83489,193],{"class":102},[52,83491,115],{"class":83},[52,83493,83494],{"class":87},"Study Python",[52,83496,115],{"class":83},[52,83498,211],{"class":102},[52,83500,83501],{"class":54,"line":106},[52,83502,70],{"emptyLinePlaceholder":69},[52,83504,83505,83507,83509,83511],{"class":54,"line":128},[52,83506,242],{"class":241},[52,83508,193],{"class":102},[52,83510,83040],{"class":189},[52,83512,211],{"class":102},[14,83514,5267],{},[42,83516,83519],{"className":83517,"code":83518,"language":955,"meta":47},[953],"['Buy milk', 'Study Python']\n",[49,83520,83518],{"__ignoreMap":47},[14,83522,83523,83524,83526],{},"The basic version does ",[5503,83525,20858],{}," save tasks to a file. That means when you close the program, the list disappears.",[14,83528,83529,83530,186],{},"If you want to understand the add method in more detail, see ",[372,83531,83533,83534,7203],{"href":83532},"\u002Freference\u002Fpython-list-append-method","Python list ",[49,83535,34074],{},[37,83537,83539],{"id":83538},"how-the-menu-loop-works","How the menu loop works",[14,83541,83542],{},"The whole program runs inside this loop:",[42,83544,83545],{"className":44,"code":51952,"language":46,"meta":47,"style":47},[49,83546,83547],{"__ignoreMap":47},[52,83548,83549,83551,83553],{"class":54,"line":55},[52,83550,10772],{"class":58},[52,83552,10943],{"class":160},[52,83554,1330],{"class":102},[14,83556,83557],{},"This means the menu keeps showing again and again until the program reaches:",[42,83559,83560],{"className":44,"code":52093,"language":46,"meta":47,"style":47},[49,83561,83562],{"__ignoreMap":47},[52,83563,83564],{"class":54,"line":55},[52,83565,52093],{"class":58},[14,83567,83568],{},"Inside the loop, the program:",[21,83570,83571,83574,83577],{},[24,83572,83573],{},"Prints the menu",[24,83575,83576],{},"Gets the user's choice",[24,83578,83579],{},"Runs a different block of code based on that choice",[14,83581,83582,83583,2587,83585,11556,83587,118],{},"The choice is handled with ",[49,83584,1313],{},[49,83586,10787],{},[49,83588,1591],{},[42,83590,83592],{"className":44,"code":83591,"language":46,"meta":47,"style":47},"choice = input(\"Choose an option: \")\n\nif choice == \"1\":\n    print(\"View tasks\")\nelif choice == \"2\":\n    print(\"Add task\")\nelif choice == \"3\":\n    print(\"Remove task\")\nelif choice == \"4\":\n    print(\"Goodbye!\")\n    break\nelse:\n    print(\"Invalid choice.\")\n",[49,83593,83594,83612,83616,83632,83647,83663,83678,83694,83709,83725,83739,83743,83749],{"__ignoreMap":47},[52,83595,83596,83598,83600,83602,83604,83606,83608,83610],{"class":54,"line":55},[52,83597,12452],{"class":62},[52,83599,80],{"class":79},[52,83601,11037],{"class":241},[52,83603,193],{"class":102},[52,83605,115],{"class":83},[52,83607,11044],{"class":87},[52,83609,115],{"class":83},[52,83611,211],{"class":102},[52,83613,83614],{"class":54,"line":66},[52,83615,70],{"emptyLinePlaceholder":69},[52,83617,83618,83620,83622,83624,83626,83628,83630],{"class":54,"line":73},[52,83619,1313],{"class":58},[52,83621,11059],{"class":62},[52,83623,11062],{"class":79},[52,83625,84],{"class":83},[52,83627,9200],{"class":87},[52,83629,115],{"class":83},[52,83631,1330],{"class":102},[52,83633,83634,83636,83638,83640,83643,83645],{"class":54,"line":94},[52,83635,1599],{"class":241},[52,83637,193],{"class":102},[52,83639,115],{"class":83},[52,83641,83642],{"class":87},"View tasks",[52,83644,115],{"class":83},[52,83646,211],{"class":102},[52,83648,83649,83651,83653,83655,83657,83659,83661],{"class":54,"line":106},[52,83650,10787],{"class":58},[52,83652,11059],{"class":62},[52,83654,11062],{"class":79},[52,83656,84],{"class":83},[52,83658,9825],{"class":87},[52,83660,115],{"class":83},[52,83662,1330],{"class":102},[52,83664,83665,83667,83669,83671,83674,83676],{"class":54,"line":128},[52,83666,1599],{"class":241},[52,83668,193],{"class":102},[52,83670,115],{"class":83},[52,83672,83673],{"class":87},"Add task",[52,83675,115],{"class":83},[52,83677,211],{"class":102},[52,83679,83680,83682,83684,83686,83688,83690,83692],{"class":54,"line":148},[52,83681,10787],{"class":58},[52,83683,11059],{"class":62},[52,83685,11062],{"class":79},[52,83687,84],{"class":83},[52,83689,7508],{"class":87},[52,83691,115],{"class":83},[52,83693,1330],{"class":102},[52,83695,83696,83698,83700,83702,83705,83707],{"class":54,"line":164},[52,83697,1599],{"class":241},[52,83699,193],{"class":102},[52,83701,115],{"class":83},[52,83703,83704],{"class":87},"Remove task",[52,83706,115],{"class":83},[52,83708,211],{"class":102},[52,83710,83711,83713,83715,83717,83719,83721,83723],{"class":54,"line":170},[52,83712,10787],{"class":58},[52,83714,11059],{"class":62},[52,83716,11062],{"class":79},[52,83718,84],{"class":83},[52,83720,11366],{"class":87},[52,83722,115],{"class":83},[52,83724,1330],{"class":102},[52,83726,83727,83729,83731,83733,83735,83737],{"class":54,"line":175},[52,83728,1599],{"class":241},[52,83730,193],{"class":102},[52,83732,115],{"class":83},[52,83734,11489],{"class":87},[52,83736,115],{"class":83},[52,83738,211],{"class":102},[52,83740,83741],{"class":54,"line":214},[52,83742,52013],{"class":58},[52,83744,83745,83747],{"class":54,"line":233},[52,83746,1591],{"class":58},[52,83748,1330],{"class":102},[52,83750,83751,83753,83755,83757,83759,83761],{"class":54,"line":238},[52,83752,1599],{"class":241},[52,83754,193],{"class":102},[52,83756,115],{"class":83},[52,83758,83385],{"class":87},[52,83760,115],{"class":83},[52,83762,211],{"class":102},[14,83764,83765],{},"This is a common pattern in simple command-line programs.",[14,83767,83768,83769,857,83774,186],{},"If you want more detail on user input, see ",[372,83770,10646,83772,3711],{"href":83771},"\u002Freference\u002Fpython-input-function-explained",[49,83773,10597],{},[372,83775,6398],{"href":83776},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python",[37,83778,83780],{"id":83779},"how-adding-a-task-works","How adding a task works",[14,83782,83783],{},"The add option uses this code:",[42,83785,83787],{"className":44,"code":83786,"language":46,"meta":47,"style":47},"task = input(\"Enter a task: \")\ntasks.append(task)\nprint(\"Task added.\")\n",[49,83788,83789,83808,83822],{"__ignoreMap":47},[52,83790,83791,83794,83796,83798,83800,83802,83804,83806],{"class":54,"line":55},[52,83792,83793],{"class":62},"task ",[52,83795,80],{"class":79},[52,83797,11037],{"class":241},[52,83799,193],{"class":102},[52,83801,115],{"class":83},[52,83803,83115],{"class":87},[52,83805,115],{"class":83},[52,83807,211],{"class":102},[52,83809,83810,83812,83814,83816,83818,83820],{"class":54,"line":66},[52,83811,83040],{"class":62},[52,83813,186],{"class":102},[52,83815,18748],{"class":189},[52,83817,193],{"class":102},[52,83819,83073],{"class":189},[52,83821,211],{"class":102},[52,83823,83824,83826,83828,83830,83832,83834],{"class":54,"line":73},[52,83825,242],{"class":241},[52,83827,193],{"class":102},[52,83829,115],{"class":83},[52,83831,83145],{"class":87},[52,83833,115],{"class":83},[52,83835,211],{"class":102},[14,83837,83838],{},"What happens here:",[309,83840,83841,83844,83849,83855],{},[24,83842,83843],{},"The program asks the user for task text",[24,83845,83846,83847],{},"The text is saved in the variable ",[49,83848,83073],{},[24,83850,83851,83854],{},[49,83852,83853],{},"tasks.append(task)"," adds it to the list",[24,83856,83857],{},"A short confirmation message is printed",[14,83859,4259],{},[42,83861,83863],{"className":44,"code":83862,"language":46,"meta":47,"style":47},"tasks = []\n\ntask = input(\"Enter a task: \")\ntasks.append(task)\n\nprint(tasks)\n",[49,83864,83865,83873,83877,83895,83909,83913],{"__ignoreMap":47},[52,83866,83867,83869,83871],{"class":54,"line":55},[52,83868,82853],{"class":62},[52,83870,80],{"class":79},[52,83872,18590],{"class":102},[52,83874,83875],{"class":54,"line":66},[52,83876,70],{"emptyLinePlaceholder":69},[52,83878,83879,83881,83883,83885,83887,83889,83891,83893],{"class":54,"line":73},[52,83880,83793],{"class":62},[52,83882,80],{"class":79},[52,83884,11037],{"class":241},[52,83886,193],{"class":102},[52,83888,115],{"class":83},[52,83890,83115],{"class":87},[52,83892,115],{"class":83},[52,83894,211],{"class":102},[52,83896,83897,83899,83901,83903,83905,83907],{"class":54,"line":94},[52,83898,83040],{"class":62},[52,83900,186],{"class":102},[52,83902,18748],{"class":189},[52,83904,193],{"class":102},[52,83906,83073],{"class":189},[52,83908,211],{"class":102},[52,83910,83911],{"class":54,"line":106},[52,83912,70],{"emptyLinePlaceholder":69},[52,83914,83915,83917,83919,83921],{"class":54,"line":128},[52,83916,242],{"class":241},[52,83918,193],{"class":102},[52,83920,83040],{"class":189},[52,83922,211],{"class":102},[14,83924,82130,83925,83928],{},[49,83926,83927],{},"Wash dishes",", the list becomes:",[42,83930,83933],{"className":83931,"code":83932,"language":955,"meta":47},[953],"['Wash dishes']\n",[49,83934,83932],{"__ignoreMap":47},[14,83936,83937],{},"You could also improve this by checking for empty input:",[42,83939,83941],{"className":44,"code":83940,"language":46,"meta":47,"style":47},"task = input(\"Enter a task: \")\n\nif task == \"\":\n    print(\"Task cannot be empty.\")\nelse:\n    tasks.append(task)\n    print(\"Task added.\")\n",[49,83942,83943,83961,83965,83977,83992,83998,84013],{"__ignoreMap":47},[52,83944,83945,83947,83949,83951,83953,83955,83957,83959],{"class":54,"line":55},[52,83946,83793],{"class":62},[52,83948,80],{"class":79},[52,83950,11037],{"class":241},[52,83952,193],{"class":102},[52,83954,115],{"class":83},[52,83956,83115],{"class":87},[52,83958,115],{"class":83},[52,83960,211],{"class":102},[52,83962,83963],{"class":54,"line":66},[52,83964,70],{"emptyLinePlaceholder":69},[52,83966,83967,83969,83971,83973,83975],{"class":54,"line":73},[52,83968,1313],{"class":58},[52,83970,83031],{"class":62},[52,83972,11062],{"class":79},[52,83974,3448],{"class":83},[52,83976,1330],{"class":102},[52,83978,83979,83981,83983,83985,83988,83990],{"class":54,"line":94},[52,83980,1599],{"class":241},[52,83982,193],{"class":102},[52,83984,115],{"class":83},[52,83986,83987],{"class":87},"Task cannot be empty.",[52,83989,115],{"class":83},[52,83991,211],{"class":102},[52,83993,83994,83996],{"class":54,"line":106},[52,83995,1591],{"class":58},[52,83997,1330],{"class":102},[52,83999,84000,84003,84005,84007,84009,84011],{"class":54,"line":128},[52,84001,84002],{"class":62},"    tasks",[52,84004,186],{"class":102},[52,84006,18748],{"class":189},[52,84008,193],{"class":102},[52,84010,83073],{"class":189},[52,84012,211],{"class":102},[52,84014,84015,84017,84019,84021,84023,84025],{"class":54,"line":148},[52,84016,1599],{"class":241},[52,84018,193],{"class":102},[52,84020,115],{"class":83},[52,84022,83145],{"class":87},[52,84024,115],{"class":83},[52,84026,211],{"class":102},[37,84028,84030],{"id":84029},"how-viewing-tasks-works","How viewing tasks works",[14,84032,84033],{},"The view option first checks whether the list is empty:",[42,84035,84037],{"className":44,"code":84036,"language":46,"meta":47,"style":47},"if not tasks:\n    print(\"No tasks yet.\")\n",[49,84038,84039,84049],{"__ignoreMap":47},[52,84040,84041,84043,84045,84047],{"class":54,"line":55},[52,84042,1313],{"class":58},[52,84044,1447],{"class":79},[52,84046,82997],{"class":62},[52,84048,1330],{"class":102},[52,84050,84051,84053,84055,84057,84059,84061],{"class":54,"line":66},[52,84052,1599],{"class":241},[52,84054,193],{"class":102},[52,84056,115],{"class":83},[52,84058,83010],{"class":87},[52,84060,115],{"class":83},[52,84062,211],{"class":102},[14,84064,84065],{},"This is a simple way to test whether a list has any items.",[14,84067,84068],{},"If the list is not empty, the script prints each task with a number:",[42,84070,84072],{"className":44,"code":84071,"language":46,"meta":47,"style":47},"for index, task in enumerate(tasks, start=1):\n    print(f\"{index}. {task}\")\n",[49,84073,84074,84102],{"__ignoreMap":47},[52,84075,84076,84078,84080,84082,84084,84086,84088,84090,84092,84094,84096,84098,84100],{"class":54,"line":55},[52,84077,12265],{"class":58},[52,84079,64277],{"class":62},[52,84081,199],{"class":102},[52,84083,83031],{"class":62},[52,84085,11213],{"class":58},[52,84087,49274],{"class":241},[52,84089,193],{"class":102},[52,84091,83040],{"class":189},[52,84093,199],{"class":102},[52,84095,64300],{"class":202},[52,84097,80],{"class":79},[52,84099,9200],{"class":121},[52,84101,3114],{"class":102},[52,84103,84104,84106,84108,84110,84112,84114,84116,84118,84120,84122,84124,84126,84128],{"class":54,"line":66},[52,84105,1599],{"class":241},[52,84107,193],{"class":102},[52,84109,1476],{"class":1475},[52,84111,115],{"class":87},[52,84113,1482],{"class":121},[52,84115,64335],{"class":189},[52,84117,1488],{"class":121},[52,84119,69957],{"class":87},[52,84121,1482],{"class":121},[52,84123,83073],{"class":189},[52,84125,1488],{"class":121},[52,84127,115],{"class":87},[52,84129,211],{"class":102},[14,84131,1956,84132,1995],{},[49,84133,84134],{},"enumerate(tasks, start=1)",[309,84136,84137,84142,84148],{},[24,84138,84139,84141],{},[49,84140,66890],{}," gives both the position and the value",[24,84143,84144,84147],{},[49,84145,84146],{},"start=1"," makes the numbering begin at 1 instead of 0",[24,84149,84150],{},"This is easier for users to read",[14,84152,4259],{},[42,84154,84156],{"className":44,"code":84155,"language":46,"meta":47,"style":47},"tasks = [\"Buy milk\", \"Call Sam\", \"Study Python\"]\n\nfor index, task in enumerate(tasks, start=1):\n    print(f\"{index}. {task}\")\n",[49,84157,84158,84191,84195,84223],{"__ignoreMap":47},[52,84159,84160,84162,84164,84166,84168,84170,84172,84174,84176,84179,84181,84183,84185,84187,84189],{"class":54,"line":55},[52,84161,82853],{"class":62},[52,84163,80],{"class":79},[52,84165,18162],{"class":102},[52,84167,115],{"class":83},[52,84169,83475],{"class":87},[52,84171,115],{"class":83},[52,84173,199],{"class":102},[52,84175,84],{"class":83},[52,84177,84178],{"class":87},"Call Sam",[52,84180,115],{"class":83},[52,84182,199],{"class":102},[52,84184,84],{"class":83},[52,84186,83494],{"class":87},[52,84188,115],{"class":83},[52,84190,1147],{"class":102},[52,84192,84193],{"class":54,"line":66},[52,84194,70],{"emptyLinePlaceholder":69},[52,84196,84197,84199,84201,84203,84205,84207,84209,84211,84213,84215,84217,84219,84221],{"class":54,"line":73},[52,84198,12265],{"class":58},[52,84200,64277],{"class":62},[52,84202,199],{"class":102},[52,84204,83031],{"class":62},[52,84206,11213],{"class":58},[52,84208,49274],{"class":241},[52,84210,193],{"class":102},[52,84212,83040],{"class":189},[52,84214,199],{"class":102},[52,84216,64300],{"class":202},[52,84218,80],{"class":79},[52,84220,9200],{"class":121},[52,84222,3114],{"class":102},[52,84224,84225,84227,84229,84231,84233,84235,84237,84239,84241,84243,84245,84247,84249],{"class":54,"line":94},[52,84226,1599],{"class":241},[52,84228,193],{"class":102},[52,84230,1476],{"class":1475},[52,84232,115],{"class":87},[52,84234,1482],{"class":121},[52,84236,64335],{"class":189},[52,84238,1488],{"class":121},[52,84240,69957],{"class":87},[52,84242,1482],{"class":121},[52,84244,83073],{"class":189},[52,84246,1488],{"class":121},[52,84248,115],{"class":87},[52,84250,211],{"class":102},[14,84252,5267],{},[42,84254,84257],{"className":84255,"code":84256,"language":955,"meta":47},[953],"1. Buy milk\n2. Call Sam\n3. Study Python\n",[49,84258,84256],{"__ignoreMap":47},[14,84260,84261,84262,186],{},"For more on this, see ",[372,84263,10646,84265,3711],{"href":84264},"\u002Freference\u002Fpython-enumerate-function-explained",[49,84266,66890],{},[37,84268,84270],{"id":84269},"how-removing-a-task-works","How removing a task works",[14,84272,84273],{},"Removing a task takes a few more steps.",[14,84275,84276],{},"First, the script shows the numbered task list:",[42,84278,84279],{"className":44,"code":84071,"language":46,"meta":47,"style":47},[49,84280,84281,84309],{"__ignoreMap":47},[52,84282,84283,84285,84287,84289,84291,84293,84295,84297,84299,84301,84303,84305,84307],{"class":54,"line":55},[52,84284,12265],{"class":58},[52,84286,64277],{"class":62},[52,84288,199],{"class":102},[52,84290,83031],{"class":62},[52,84292,11213],{"class":58},[52,84294,49274],{"class":241},[52,84296,193],{"class":102},[52,84298,83040],{"class":189},[52,84300,199],{"class":102},[52,84302,64300],{"class":202},[52,84304,80],{"class":79},[52,84306,9200],{"class":121},[52,84308,3114],{"class":102},[52,84310,84311,84313,84315,84317,84319,84321,84323,84325,84327,84329,84331,84333,84335],{"class":54,"line":66},[52,84312,1599],{"class":241},[52,84314,193],{"class":102},[52,84316,1476],{"class":1475},[52,84318,115],{"class":87},[52,84320,1482],{"class":121},[52,84322,64335],{"class":189},[52,84324,1488],{"class":121},[52,84326,69957],{"class":87},[52,84328,1482],{"class":121},[52,84330,83073],{"class":189},[52,84332,1488],{"class":121},[52,84334,115],{"class":87},[52,84336,211],{"class":102},[14,84338,84339],{},"Then it asks which task to remove:",[42,84341,84343],{"className":44,"code":84342,"language":46,"meta":47,"style":47},"number = int(input(\"Enter task number to remove: \"))\n",[49,84344,84345],{"__ignoreMap":47},[52,84346,84347,84349,84351,84353,84355,84357,84359,84361,84363,84365],{"class":54,"line":55},[52,84348,32574],{"class":62},[52,84350,80],{"class":79},[52,84352,9789],{"class":4733},[52,84354,193],{"class":102},[52,84356,13610],{"class":241},[52,84358,193],{"class":102},[52,84360,115],{"class":83},[52,84362,83276],{"class":87},[52,84364,115],{"class":83},[52,84366,8886],{"class":102},[14,84368,84369],{},"This line does two things:",[309,84371,84372,84376],{},[24,84373,84374,54451],{},[49,84375,10597],{},[24,84377,84378,84380],{},[49,84379,9492],{}," converts that text into a number",[14,84382,84383],{},"Finally, it removes the item:",[42,84385,84387],{"className":44,"code":84386,"language":46,"meta":47,"style":47},"removed = tasks.pop(number - 1)\nprint(f\"Removed: {removed}\")\n",[49,84388,84389,84412],{"__ignoreMap":47},[52,84390,84391,84394,84396,84398,84400,84402,84404,84406,84408,84410],{"class":54,"line":55},[52,84392,84393],{"class":62},"removed ",[52,84395,80],{"class":79},[52,84397,82997],{"class":62},[52,84399,186],{"class":102},[52,84401,83294],{"class":189},[52,84403,193],{"class":102},[52,84405,32574],{"class":189},[52,84407,5889],{"class":79},[52,84409,1518],{"class":121},[52,84411,211],{"class":102},[52,84413,84414,84416,84418,84420,84422,84424,84426,84428,84430],{"class":54,"line":66},[52,84415,242],{"class":241},[52,84417,193],{"class":102},[52,84419,1476],{"class":1475},[52,84421,83315],{"class":87},[52,84423,1482],{"class":121},[52,84425,83320],{"class":189},[52,84427,1488],{"class":121},[52,84429,115],{"class":87},[52,84431,211],{"class":102},[14,84433,24316,84434,1995],{},[49,84435,84436],{},"number - 1",[309,84438,84439,84442,84445],{},[24,84440,84441],{},"Users count tasks as 1, 2, 3",[24,84443,84444],{},"Python list indexes are 0, 1, 2",[24,84446,84447],{},"So task 1 is actually index 0",[14,84449,4259],{},[42,84451,84453],{"className":44,"code":84452,"language":46,"meta":47,"style":47},"tasks = [\"Buy milk\", \"Call Sam\", \"Study Python\"]\n\nnumber = 2\nremoved = tasks.pop(number - 1)\n\nprint(\"Removed:\", removed)\nprint(\"Remaining tasks:\", tasks)\n",[49,84454,84455,84487,84491,84499,84521,84525,84545],{"__ignoreMap":47},[52,84456,84457,84459,84461,84463,84465,84467,84469,84471,84473,84475,84477,84479,84481,84483,84485],{"class":54,"line":55},[52,84458,82853],{"class":62},[52,84460,80],{"class":79},[52,84462,18162],{"class":102},[52,84464,115],{"class":83},[52,84466,83475],{"class":87},[52,84468,115],{"class":83},[52,84470,199],{"class":102},[52,84472,84],{"class":83},[52,84474,84178],{"class":87},[52,84476,115],{"class":83},[52,84478,199],{"class":102},[52,84480,84],{"class":83},[52,84482,83494],{"class":87},[52,84484,115],{"class":83},[52,84486,1147],{"class":102},[52,84488,84489],{"class":54,"line":66},[52,84490,70],{"emptyLinePlaceholder":69},[52,84492,84493,84495,84497],{"class":54,"line":73},[52,84494,32574],{"class":62},[52,84496,80],{"class":79},[52,84498,45381],{"class":121},[52,84500,84501,84503,84505,84507,84509,84511,84513,84515,84517,84519],{"class":54,"line":94},[52,84502,84393],{"class":62},[52,84504,80],{"class":79},[52,84506,82997],{"class":62},[52,84508,186],{"class":102},[52,84510,83294],{"class":189},[52,84512,193],{"class":102},[52,84514,32574],{"class":189},[52,84516,5889],{"class":79},[52,84518,1518],{"class":121},[52,84520,211],{"class":102},[52,84522,84523],{"class":54,"line":106},[52,84524,70],{"emptyLinePlaceholder":69},[52,84526,84527,84529,84531,84533,84536,84538,84540,84543],{"class":54,"line":128},[52,84528,242],{"class":241},[52,84530,193],{"class":102},[52,84532,115],{"class":83},[52,84534,84535],{"class":87},"Removed:",[52,84537,115],{"class":83},[52,84539,199],{"class":102},[52,84541,84542],{"class":189}," removed",[52,84544,211],{"class":102},[52,84546,84547,84549,84551,84553,84556,84558,84560,84562],{"class":54,"line":148},[52,84548,242],{"class":241},[52,84550,193],{"class":102},[52,84552,115],{"class":83},[52,84554,84555],{"class":87},"Remaining tasks:",[52,84557,115],{"class":83},[52,84559,199],{"class":102},[52,84561,82997],{"class":189},[52,84563,211],{"class":102},[14,84565,5267],{},[42,84567,84570],{"className":84568,"code":84569,"language":955,"meta":47},[953],"Removed: Call Sam\nRemaining tasks: ['Buy milk', 'Study Python']\n",[49,84571,84569],{"__ignoreMap":47},[14,84573,358,84574,84576,84577,84582],{},[49,84575,82760],{}," method removes an item and returns it at the same time. See ",[372,84578,83533,84580,7203],{"href":84579},"\u002Freference\u002Fpython-list-pop-method",[49,84581,82760],{}," for more details.",[14,84584,21178],{},[309,84586,84587,84595],{},[24,84588,84589,84590,84592,84593],{},"If the user types text instead of a number, ",[49,84591,9492],{}," can raise a ",[49,84594,13780],{},[24,84596,84597,84598,84600,84601],{},"If the number is too large or too small, ",[49,84599,82760],{}," can raise an ",[49,84602,10303],{},[37,84604,84606],{"id":84605},"a-safer-version-with-basic-error-handling","A safer version with basic error handling",[14,84608,84609],{},"The first script works, but the remove section can crash if the user enters bad input.",[14,84611,84612],{},"Here is a safer version with basic error handling:",[42,84614,84616],{"className":44,"code":84615,"language":46,"meta":47,"style":47},"tasks = []\n\nwhile True:\n    print(\"\\nTo-Do List\")\n    print(\"1. View tasks\")\n    print(\"2. Add task\")\n    print(\"3. Remove task\")\n    print(\"4. Quit\")\n\n    choice = input(\"Choose an option: \")\n\n    if choice == \"1\":\n        if not tasks:\n            print(\"No tasks yet.\")\n        else:\n            for index, task in enumerate(tasks, start=1):\n                print(f\"{index}. {task}\")\n\n    elif choice == \"2\":\n        task = input(\"Enter a task: \").strip()\n        if task == \"\":\n            print(\"Task cannot be empty.\")\n        else:\n            tasks.append(task)\n            print(\"Task added.\")\n\n    elif choice == \"3\":\n        if not tasks:\n            print(\"No tasks to remove.\")\n        else:\n            for index, task in enumerate(tasks, start=1):\n                print(f\"{index}. {task}\")\n\n            try:\n                number = int(input(\"Enter task number to remove: \"))\n\n                if number \u003C 1 or number > len(tasks):\n                    print(\"That task number does not exist.\")\n                else:\n                    removed = tasks.pop(number - 1)\n                    print(f\"Removed: {removed}\")\n\n            except ValueError:\n                print(\"Please enter a valid number.\")\n\n    elif choice == \"4\":\n        print(\"Goodbye!\")\n        break\n\n    else:\n        print(\"Invalid choice.\")\n",[49,84617,84618,84626,84630,84638,84654,84668,84682,84696,84710,84714,84732,84736,84752,84762,84776,84782,84810,84838,84842,84858,84880,84892,84906,84912,84927,84941,84945,84961,84971,84985,84991,85019,85047,85051,85058,85081,85085,85110,85126,85133,85156,85176,85180,85189,85204,85208,85225,85240,85245,85250,85257],{"__ignoreMap":47},[52,84619,84620,84622,84624],{"class":54,"line":55},[52,84621,82853],{"class":62},[52,84623,80],{"class":79},[52,84625,18590],{"class":102},[52,84627,84628],{"class":54,"line":66},[52,84629,70],{"emptyLinePlaceholder":69},[52,84631,84632,84634,84636],{"class":54,"line":73},[52,84633,10772],{"class":58},[52,84635,10943],{"class":160},[52,84637,1330],{"class":102},[52,84639,84640,84642,84644,84646,84648,84650,84652],{"class":54,"line":94},[52,84641,1599],{"class":241},[52,84643,193],{"class":102},[52,84645,115],{"class":83},[52,84647,10956],{"class":2533},[52,84649,82882],{"class":87},[52,84651,115],{"class":83},[52,84653,211],{"class":102},[52,84655,84656,84658,84660,84662,84664,84666],{"class":54,"line":106},[52,84657,1599],{"class":241},[52,84659,193],{"class":102},[52,84661,115],{"class":83},[52,84663,82897],{"class":87},[52,84665,115],{"class":83},[52,84667,211],{"class":102},[52,84669,84670,84672,84674,84676,84678,84680],{"class":54,"line":128},[52,84671,1599],{"class":241},[52,84673,193],{"class":102},[52,84675,115],{"class":83},[52,84677,82912],{"class":87},[52,84679,115],{"class":83},[52,84681,211],{"class":102},[52,84683,84684,84686,84688,84690,84692,84694],{"class":54,"line":148},[52,84685,1599],{"class":241},[52,84687,193],{"class":102},[52,84689,115],{"class":83},[52,84691,82927],{"class":87},[52,84693,115],{"class":83},[52,84695,211],{"class":102},[52,84697,84698,84700,84702,84704,84706,84708],{"class":54,"line":164},[52,84699,1599],{"class":241},[52,84701,193],{"class":102},[52,84703,115],{"class":83},[52,84705,82942],{"class":87},[52,84707,115],{"class":83},[52,84709,211],{"class":102},[52,84711,84712],{"class":54,"line":170},[52,84713,70],{"emptyLinePlaceholder":69},[52,84715,84716,84718,84720,84722,84724,84726,84728,84730],{"class":54,"line":175},[52,84717,11032],{"class":62},[52,84719,80],{"class":79},[52,84721,11037],{"class":241},[52,84723,193],{"class":102},[52,84725,115],{"class":83},[52,84727,11044],{"class":87},[52,84729,115],{"class":83},[52,84731,211],{"class":102},[52,84733,84734],{"class":54,"line":214},[52,84735,70],{"emptyLinePlaceholder":69},[52,84737,84738,84740,84742,84744,84746,84748,84750],{"class":54,"line":233},[52,84739,1438],{"class":58},[52,84741,11059],{"class":62},[52,84743,11062],{"class":79},[52,84745,84],{"class":83},[52,84747,9200],{"class":87},[52,84749,115],{"class":83},[52,84751,1330],{"class":102},[52,84753,84754,84756,84758,84760],{"class":54,"line":238},[52,84755,11168],{"class":58},[52,84757,1447],{"class":79},[52,84759,82997],{"class":62},[52,84761,1330],{"class":102},[52,84763,84764,84766,84768,84770,84772,84774],{"class":54,"line":272},[52,84765,11180],{"class":241},[52,84767,193],{"class":102},[52,84769,115],{"class":83},[52,84771,83010],{"class":87},[52,84773,115],{"class":83},[52,84775,211],{"class":102},[52,84777,84778,84780],{"class":54,"line":1348},[52,84779,11196],{"class":58},[52,84781,1330],{"class":102},[52,84783,84784,84786,84788,84790,84792,84794,84796,84798,84800,84802,84804,84806,84808],{"class":54,"line":1376},[52,84785,11203],{"class":58},[52,84787,64277],{"class":62},[52,84789,199],{"class":102},[52,84791,83031],{"class":62},[52,84793,11213],{"class":58},[52,84795,49274],{"class":241},[52,84797,193],{"class":102},[52,84799,83040],{"class":189},[52,84801,199],{"class":102},[52,84803,64300],{"class":202},[52,84805,80],{"class":79},[52,84807,9200],{"class":121},[52,84809,3114],{"class":102},[52,84811,84812,84814,84816,84818,84820,84822,84824,84826,84828,84830,84832,84834,84836],{"class":54,"line":1381},[52,84813,11227],{"class":241},[52,84815,193],{"class":102},[52,84817,1476],{"class":1475},[52,84819,115],{"class":87},[52,84821,1482],{"class":121},[52,84823,64335],{"class":189},[52,84825,1488],{"class":121},[52,84827,69957],{"class":87},[52,84829,1482],{"class":121},[52,84831,83073],{"class":189},[52,84833,1488],{"class":121},[52,84835,115],{"class":87},[52,84837,211],{"class":102},[52,84839,84840],{"class":54,"line":1406},[52,84841,70],{"emptyLinePlaceholder":69},[52,84843,84844,84846,84848,84850,84852,84854,84856],{"class":54,"line":1430},[52,84845,11151],{"class":58},[52,84847,11059],{"class":62},[52,84849,11062],{"class":79},[52,84851,84],{"class":83},[52,84853,9825],{"class":87},[52,84855,115],{"class":83},[52,84857,1330],{"class":102},[52,84859,84860,84862,84864,84866,84868,84870,84872,84874,84876,84878],{"class":54,"line":1435},[52,84861,83104],{"class":62},[52,84863,80],{"class":79},[52,84865,11037],{"class":241},[52,84867,193],{"class":102},[52,84869,115],{"class":83},[52,84871,83115],{"class":87},[52,84873,115],{"class":83},[52,84875,54653],{"class":102},[52,84877,18630],{"class":189},[52,84879,230],{"class":102},[52,84881,84882,84884,84886,84888,84890],{"class":54,"line":1467},[52,84883,11168],{"class":58},[52,84885,83031],{"class":62},[52,84887,11062],{"class":79},[52,84889,3448],{"class":83},[52,84891,1330],{"class":102},[52,84893,84894,84896,84898,84900,84902,84904],{"class":54,"line":1495},[52,84895,11180],{"class":241},[52,84897,193],{"class":102},[52,84899,115],{"class":83},[52,84901,83987],{"class":87},[52,84903,115],{"class":83},[52,84905,211],{"class":102},[52,84907,84908,84910],{"class":54,"line":1531},[52,84909,11196],{"class":58},[52,84911,1330],{"class":102},[52,84913,84914,84917,84919,84921,84923,84925],{"class":54,"line":1564},[52,84915,84916],{"class":62},"            tasks",[52,84918,186],{"class":102},[52,84920,18748],{"class":189},[52,84922,193],{"class":102},[52,84924,83073],{"class":189},[52,84926,211],{"class":102},[52,84928,84929,84931,84933,84935,84937,84939],{"class":54,"line":1572},[52,84930,11180],{"class":241},[52,84932,193],{"class":102},[52,84934,115],{"class":83},[52,84936,83145],{"class":87},[52,84938,115],{"class":83},[52,84940,211],{"class":102},[52,84942,84943],{"class":54,"line":1588},[52,84944,70],{"emptyLinePlaceholder":69},[52,84946,84947,84949,84951,84953,84955,84957,84959],{"class":54,"line":1596},[52,84948,11151],{"class":58},[52,84950,11059],{"class":62},[52,84952,11062],{"class":79},[52,84954,84],{"class":83},[52,84956,7508],{"class":87},[52,84958,115],{"class":83},[52,84960,1330],{"class":102},[52,84962,84963,84965,84967,84969],{"class":54,"line":1613},[52,84964,11168],{"class":58},[52,84966,1447],{"class":79},[52,84968,82997],{"class":62},[52,84970,1330],{"class":102},[52,84972,84973,84975,84977,84979,84981,84983],{"class":54,"line":11327},[52,84974,11180],{"class":241},[52,84976,193],{"class":102},[52,84978,115],{"class":83},[52,84980,83190],{"class":87},[52,84982,115],{"class":83},[52,84984,211],{"class":102},[52,84986,84987,84989],{"class":54,"line":11334},[52,84988,11196],{"class":58},[52,84990,1330],{"class":102},[52,84992,84993,84995,84997,84999,85001,85003,85005,85007,85009,85011,85013,85015,85017],{"class":54,"line":11350},[52,84994,11203],{"class":58},[52,84996,64277],{"class":62},[52,84998,199],{"class":102},[52,85000,83031],{"class":62},[52,85002,11213],{"class":58},[52,85004,49274],{"class":241},[52,85006,193],{"class":102},[52,85008,83040],{"class":189},[52,85010,199],{"class":102},[52,85012,64300],{"class":202},[52,85014,80],{"class":79},[52,85016,9200],{"class":121},[52,85018,3114],{"class":102},[52,85020,85021,85023,85025,85027,85029,85031,85033,85035,85037,85039,85041,85043,85045],{"class":54,"line":11355},[52,85022,11227],{"class":241},[52,85024,193],{"class":102},[52,85026,1476],{"class":1475},[52,85028,115],{"class":87},[52,85030,1482],{"class":121},[52,85032,64335],{"class":189},[52,85034,1488],{"class":121},[52,85036,69957],{"class":87},[52,85038,1482],{"class":121},[52,85040,83073],{"class":189},[52,85042,1488],{"class":121},[52,85044,115],{"class":87},[52,85046,211],{"class":102},[52,85048,85049],{"class":54,"line":11373},[52,85050,70],{"emptyLinePlaceholder":69},[52,85052,85053,85056],{"class":54,"line":11393},[52,85054,85055],{"class":58},"            try",[52,85057,1330],{"class":102},[52,85059,85060,85063,85065,85067,85069,85071,85073,85075,85077,85079],{"class":54,"line":11406},[52,85061,85062],{"class":62},"                number ",[52,85064,80],{"class":79},[52,85066,9789],{"class":4733},[52,85068,193],{"class":102},[52,85070,13610],{"class":241},[52,85072,193],{"class":102},[52,85074,115],{"class":83},[52,85076,83276],{"class":87},[52,85078,115],{"class":83},[52,85080,8886],{"class":102},[52,85082,85083],{"class":54,"line":11420},[52,85084,70],{"emptyLinePlaceholder":69},[52,85086,85087,85090,85092,85094,85096,85098,85100,85102,85104,85106,85108],{"class":54,"line":11436},[52,85088,85089],{"class":58},"                if",[52,85091,32297],{"class":62},[52,85093,15294],{"class":79},[52,85095,1518],{"class":121},[52,85097,35411],{"class":79},[52,85099,32297],{"class":62},[52,85101,13046],{"class":79},[52,85103,9150],{"class":241},[52,85105,193],{"class":102},[52,85107,83040],{"class":189},[52,85109,3114],{"class":102},[52,85111,85112,85115,85117,85119,85122,85124],{"class":54,"line":11443},[52,85113,85114],{"class":241},"                    print",[52,85116,193],{"class":102},[52,85118,115],{"class":83},[52,85120,85121],{"class":87},"That task number does not exist.",[52,85123,115],{"class":83},[52,85125,211],{"class":102},[52,85127,85128,85131],{"class":54,"line":11458},[52,85129,85130],{"class":58},"                else",[52,85132,1330],{"class":102},[52,85134,85135,85138,85140,85142,85144,85146,85148,85150,85152,85154],{"class":54,"line":11463},[52,85136,85137],{"class":62},"                    removed ",[52,85139,80],{"class":79},[52,85141,82997],{"class":62},[52,85143,186],{"class":102},[52,85145,83294],{"class":189},[52,85147,193],{"class":102},[52,85149,32574],{"class":189},[52,85151,5889],{"class":79},[52,85153,1518],{"class":121},[52,85155,211],{"class":102},[52,85157,85158,85160,85162,85164,85166,85168,85170,85172,85174],{"class":54,"line":11480},[52,85159,85114],{"class":241},[52,85161,193],{"class":102},[52,85163,1476],{"class":1475},[52,85165,83315],{"class":87},[52,85167,1482],{"class":121},[52,85169,83320],{"class":189},[52,85171,1488],{"class":121},[52,85173,115],{"class":87},[52,85175,211],{"class":102},[52,85177,85178],{"class":54,"line":11496},[52,85179,70],{"emptyLinePlaceholder":69},[52,85181,85182,85185,85187],{"class":54,"line":11502},[52,85183,85184],{"class":58},"            except",[52,85186,4734],{"class":4733},[52,85188,1330],{"class":102},[52,85190,85191,85193,85195,85197,85200,85202],{"class":54,"line":11507},[52,85192,11227],{"class":241},[52,85194,193],{"class":102},[52,85196,115],{"class":83},[52,85198,85199],{"class":87},"Please enter a valid number.",[52,85201,115],{"class":83},[52,85203,211],{"class":102},[52,85205,85206],{"class":54,"line":11514},[52,85207,70],{"emptyLinePlaceholder":69},[52,85209,85211,85213,85215,85217,85219,85221,85223],{"class":54,"line":85210},46,[52,85212,11151],{"class":58},[52,85214,11059],{"class":62},[52,85216,11062],{"class":79},[52,85218,84],{"class":83},[52,85220,11366],{"class":87},[52,85222,115],{"class":83},[52,85224,1330],{"class":102},[52,85226,85228,85230,85232,85234,85236,85238],{"class":54,"line":85227},47,[52,85229,1470],{"class":241},[52,85231,193],{"class":102},[52,85233,115],{"class":83},[52,85235,11489],{"class":87},[52,85237,115],{"class":83},[52,85239,211],{"class":102},[52,85241,85243],{"class":54,"line":85242},48,[52,85244,11499],{"class":58},[52,85246,85248],{"class":54,"line":85247},49,[52,85249,70],{"emptyLinePlaceholder":69},[52,85251,85253,85255],{"class":54,"line":85252},50,[52,85254,1567],{"class":58},[52,85256,1330],{"class":102},[52,85258,85260,85262,85264,85266,85268,85270],{"class":54,"line":85259},51,[52,85261,1470],{"class":241},[52,85263,193],{"class":102},[52,85265,115],{"class":83},[52,85267,83385],{"class":87},[52,85269,115],{"class":83},[52,85271,211],{"class":102},[14,85273,85274],{},"This version improves the program by:",[309,85276,85277,85283,85286],{},[24,85278,85279,85280,85282],{},"Handling ",[49,85281,13780],{}," if the user types text",[24,85284,85285],{},"Checking that the task number is in range",[24,85287,85288,85289],{},"Avoiding empty tasks with ",[49,85290,70537],{},[14,85292,85293,85294,186],{},"If you want to learn this pattern, see ",[372,85295,10028],{"href":85296},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python",[37,85298,85300],{"id":85299},"ways-to-improve-the-project","Ways to improve the project",[14,85302,73283],{},[14,85304,85305],{},"Good next steps:",[309,85307,85308,85311,85314,85317,85320],{},[24,85309,85310],{},"Mark tasks as done",[24,85312,85313],{},"Save tasks to a file",[24,85315,85316],{},"Load tasks when the program starts",[24,85318,85319],{},"Sort tasks by priority",[24,85321,85322],{},"Split repeated code into functions",[14,85324,85325,85326,186],{},"For example, saving tasks to a file is a natural next project. A related example is ",[372,85327,85328],{"href":62434},"Python read and write text file example",[37,85330,12322],{"id":12321},[14,85332,85333],{},"These are some common problems beginners run into with this project:",[309,85335,85336,85343,85346,85349,85352,85357],{},[24,85337,69711,85338,37261,85340,85342],{},[49,85339,10597],{},[49,85341,9492],{}," before using it as a number",[24,85344,85345],{},"Using the task number directly instead of subtracting 1",[24,85347,85348],{},"Trying to remove a task when the list is empty",[24,85350,85351],{},"Entering a task number that does not exist",[24,85353,85354,85355,45753],{},"Accidentally using ",[49,85356,18748],{},[24,85358,85359,85360,71403],{},"Placing ",[49,85361,10914],{},[639,85363,36199],{"id":36198},[14,85365,85366],{},"If something is not working, print values to see what the program is doing:",[42,85368,85370],{"className":44,"code":85369,"language":46,"meta":47,"style":47},"print(tasks)\nprint(choice)\nprint(number)\nprint(len(tasks))\nprint(type(choice))\nprint(type(number))\n",[49,85371,85372,85382,85392,85402,85416,85430],{"__ignoreMap":47},[52,85373,85374,85376,85378,85380],{"class":54,"line":55},[52,85375,242],{"class":241},[52,85377,193],{"class":102},[52,85379,83040],{"class":189},[52,85381,211],{"class":102},[52,85383,85384,85386,85388,85390],{"class":54,"line":66},[52,85385,242],{"class":241},[52,85387,193],{"class":102},[52,85389,12230],{"class":189},[52,85391,211],{"class":102},[52,85393,85394,85396,85398,85400],{"class":54,"line":73},[52,85395,242],{"class":241},[52,85397,193],{"class":102},[52,85399,27497],{"class":189},[52,85401,211],{"class":102},[52,85403,85404,85406,85408,85410,85412,85414],{"class":54,"line":94},[52,85405,242],{"class":241},[52,85407,193],{"class":102},[52,85409,16152],{"class":241},[52,85411,193],{"class":102},[52,85413,83040],{"class":189},[52,85415,8886],{"class":102},[52,85417,85418,85420,85422,85424,85426,85428],{"class":54,"line":106},[52,85419,242],{"class":241},[52,85421,193],{"class":102},[52,85423,4198],{"class":4733},[52,85425,193],{"class":102},[52,85427,12230],{"class":189},[52,85429,8886],{"class":102},[52,85431,85432,85434,85436,85438,85440,85442],{"class":54,"line":128},[52,85433,242],{"class":241},[52,85435,193],{"class":102},[52,85437,4198],{"class":4733},[52,85439,193],{"class":102},[52,85441,27497],{"class":189},[52,85443,8886],{"class":102},[14,85445,85446],{},"These can help you answer questions like:",[309,85448,85449,85454,85459],{},[24,85450,59422,85451,85453],{},[49,85452,12230],{}," a string or a number?",[24,85455,59422,85456,85458],{},[49,85457,27497],{}," the value you expected?",[24,85460,64091,85461,85463],{},[49,85462,83040],{}," contain what you think it contains?",[14,85465,85466,85467,186],{},"If you get an out-of-range error, see ",[372,85468,77779],{"href":45886},[14,85470,7526,85471,85473,85474,186],{},[49,85472,9492],{}," fails, see ",[372,85475,6881],{"href":19629},[37,85477,1942],{"id":1941},[639,85479,85481,85482,85484],{"id":85480},"why-does-the-script-use-number-1-when-removing-a-task","Why does the script use ",[49,85483,84436],{}," when removing a task?",[14,85486,85487],{},"Users usually count from 1, but Python list indexes start at 0. Subtracting 1 matches the user's number to the correct list position.",[639,85489,85491],{"id":85490},"why-are-my-tasks-lost-when-i-close-the-program","Why are my tasks lost when I close the program?",[14,85493,85494],{},"The basic version stores tasks only in memory. To keep them after the program closes, save them to a file.",[639,85496,85498],{"id":85497},"what-error-happens-if-i-type-text-instead-of-a-task-number","What error happens if I type text instead of a task number?",[14,85500,85501,85502,85504,85505,85507],{},"You may get ",[49,85503,13780],{}," when ",[49,85506,9492],{}," tries to convert text that is not a valid number.",[639,85509,85511],{"id":85510},"can-i-build-this-project-with-functions","Can I build this project with functions?",[14,85513,85514],{},"Yes. A cleaner version can use separate functions for showing tasks, adding tasks, and removing tasks.",[37,85516,2005],{"id":2004},[309,85518,85519,85523,85529,85535,85541,85547],{},[24,85520,85521],{},[372,85522,44583],{"href":44582},[24,85524,85525],{},[372,85526,83533,85527,7203],{"href":83532},[49,85528,34074],{},[24,85530,85531],{},[372,85532,83533,85533,7203],{"href":84579},[49,85534,82760],{},[24,85536,85537],{},[372,85538,10646,85539,3711],{"href":84264},[49,85540,66890],{},[24,85542,85543],{},[372,85544,10646,85545,3711],{"href":83771},[49,85546,10597],{},[24,85548,85549],{},[372,85550,26911],{"href":85296},[14,85552,85553],{},"Copy the script, run it, and then improve it. A great next step is adding file saving or moving repeated code into functions.",[2034,85555,85556],{},"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 .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 .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_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 .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 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);}",{"title":47,"searchDepth":66,"depth":66,"links":85558},[85559,85560,85561,85564,85565,85566,85567,85568,85569,85570,85571,85574,85581],{"id":28679,"depth":66,"text":28680},{"id":82785,"depth":66,"text":82786},{"id":82839,"depth":66,"text":82840,"children":85562},[85563],{"id":83392,"depth":73,"text":83393},{"id":83402,"depth":66,"text":83403},{"id":83538,"depth":66,"text":83539},{"id":83779,"depth":66,"text":83780},{"id":84029,"depth":66,"text":84030},{"id":84269,"depth":66,"text":84270},{"id":84605,"depth":66,"text":84606},{"id":85299,"depth":66,"text":85300},{"id":12321,"depth":66,"text":12322,"children":85572},[85573],{"id":36198,"depth":73,"text":36199},{"id":1941,"depth":66,"text":1942,"children":85575},[85576,85578,85579,85580],{"id":85480,"depth":73,"text":85577},"Why does the script use number - 1 when removing a task?",{"id":85490,"depth":73,"text":85491},{"id":85497,"depth":73,"text":85498},{"id":85510,"depth":73,"text":85511},{"id":2004,"depth":66,"text":2005},"Master python to do list script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-to-do-list-script-example",{"title":82726,"description":85582},"examples\u002Fpython-to-do-list-script-example","lNDUgwJ74f8DN-gfISbFa30414v6dMDypK3kGtMdeQ4",{"id":85589,"title":85590,"body":85591,"description":86973,"extension":2075,"meta":86974,"navigation":69,"path":86975,"seo":86976,"stem":86977,"__hash__":86978},"content\u002Fexamples\u002Fpython-url-checker-script-example.md","Python URL Checker Script Example",{"type":7,"value":85592,"toc":86948},[85593,85596,85599,85601,85615,85618,85620,85781,85784,85786,85788,85806,85809,85811,85813,85831,85837,85841,85845,85847,85859,85861,85879,85882,85895,85901,85903,85905,86063,86067,86112,86115,86118,86122,86139,86142,86145,86151,86182,86188,86190,86203,86206,86211,86215,86238,86243,86245,86251,86255,86298,86301,86314,86322,86327,86331,86334,86337,86363,86365,86424,86426,86432,86435,86437,86447,86451,86454,86456,86473,86476,86722,86724,86738,86744,86748,86751,86774,86777,86793,86795,86817,86819,86859,86863,86866,86869,86883,86886,86888,86892,86895,86899,86905,86909,86912,86916,86919,86921,86945],[10,85594,85590],{"id":85595},"python-url-checker-script-example",[14,85597,85598],{},"This example shows a simple Python script that checks whether URLs respond successfully.",[14,85600,6959],{},[309,85602,85603,85606,85609,85612],{},[24,85604,85605],{},"check one or more URLs from Python",[24,85607,85608],{},"send an HTTP request to each URL",[24,85610,85611],{},"read the response status code",[24,85613,85614],{},"handle errors without stopping the whole script",[14,85616,85617],{},"This is a practical beginner-friendly example. The goal is to understand the script, run it, and then make small improvements.",[37,85619,40],{"id":39},[42,85621,85623],{"className":44,"code":85622,"language":46,"meta":47,"style":47},"import requests\n\nurls = [\n    \"https:\u002F\u002Fwww.python.org\",\n    \"https:\u002F\u002Fexample.com\",\n    \"https:\u002F\u002Fthis-url-does-not-exist-12345.com\"\n]\n\nfor url in urls:\n    try:\n        response = requests.get(url, timeout=5)\n        print(url, response.status_code)\n    except requests.RequestException as e:\n        print(url, \"ERROR:\", e)\n",[49,85624,85625,85631,85635,85643,85653,85663,85672,85676,85680,85692,85698,85724,85742,85758],{"__ignoreMap":47},[52,85626,85627,85629],{"class":54,"line":55},[52,85628,59],{"class":58},[52,85630,63],{"class":62},[52,85632,85633],{"class":54,"line":66},[52,85634,70],{"emptyLinePlaceholder":69},[52,85636,85637,85639,85641],{"class":54,"line":73},[52,85638,74661],{"class":62},[52,85640,80],{"class":79},[52,85642,16627],{"class":102},[52,85644,85645,85647,85649,85651],{"class":54,"line":94},[52,85646,109],{"class":83},[52,85648,74682],{"class":87},[52,85650,115],{"class":83},[52,85652,125],{"class":102},[52,85654,85655,85657,85659,85661],{"class":54,"line":106},[52,85656,109],{"class":83},[52,85658,73936],{"class":87},[52,85660,115],{"class":83},[52,85662,125],{"class":102},[52,85664,85665,85667,85670],{"class":54,"line":128},[52,85666,109],{"class":83},[52,85668,85669],{"class":87},"https:\u002F\u002Fthis-url-does-not-exist-12345.com",[52,85671,91],{"class":83},[52,85673,85674],{"class":54,"line":148},[52,85675,1147],{"class":102},[52,85677,85678],{"class":54,"line":164},[52,85679,70],{"emptyLinePlaceholder":69},[52,85681,85682,85684,85686,85688,85690],{"class":54,"line":170},[52,85683,12265],{"class":58},[52,85685,74712],{"class":62},[52,85687,11213],{"class":58},[52,85689,74717],{"class":62},[52,85691,1330],{"class":102},[52,85693,85694,85696],{"class":54,"line":175},[52,85695,20482],{"class":58},[52,85697,1330],{"class":102},[52,85699,85700,85702,85704,85706,85708,85710,85712,85714,85716,85718,85720,85722],{"class":54,"line":214},[52,85701,74730],{"class":62},[52,85703,80],{"class":79},[52,85705,183],{"class":62},[52,85707,186],{"class":102},[52,85709,190],{"class":189},[52,85711,193],{"class":102},[52,85713,196],{"class":189},[52,85715,199],{"class":102},[52,85717,3368],{"class":202},[52,85719,80],{"class":79},[52,85721,9479],{"class":121},[52,85723,211],{"class":102},[52,85725,85726,85728,85730,85732,85734,85736,85738,85740],{"class":54,"line":233},[52,85727,1470],{"class":241},[52,85729,193],{"class":102},[52,85731,196],{"class":189},[52,85733,199],{"class":102},[52,85735,222],{"class":189},[52,85737,186],{"class":102},[52,85739,1321],{"class":1320},[52,85741,211],{"class":102},[52,85743,85744,85746,85748,85750,85752,85754,85756],{"class":54,"line":238},[52,85745,20512],{"class":58},[52,85747,183],{"class":62},[52,85749,186],{"class":102},[52,85751,3564],{"class":1320},[52,85753,3567],{"class":58},[52,85755,3570],{"class":62},[52,85757,1330],{"class":102},[52,85759,85760,85762,85764,85766,85768,85770,85773,85775,85777,85779],{"class":54,"line":272},[52,85761,1470],{"class":241},[52,85763,193],{"class":102},[52,85765,196],{"class":189},[52,85767,199],{"class":102},[52,85769,84],{"class":83},[52,85771,85772],{"class":87},"ERROR:",[52,85774,115],{"class":83},[52,85776,199],{"class":102},[52,85778,3570],{"class":189},[52,85780,211],{"class":102},[14,85782,85783],{},"This is the fastest working example. It checks each URL and prints either the status code or an error.",[37,85785,323],{"id":322},[14,85787,30376],{},[309,85789,85790,85793,85800,85803],{},[24,85791,85792],{},"checks one or more URLs from Python",[24,85794,85795,85796,85799],{},"sends an HTTP ",[49,85797,85798],{},"GET"," request to each URL",[24,85801,85802],{},"shows the response status code",[24,85804,85805],{},"handles connection errors without stopping the whole script",[14,85807,85808],{},"A URL checker like this is useful when you want to test links, verify website pages, or learn how Python works with web requests.",[37,85810,347],{"id":346},[14,85812,23910],{},[309,85814,85815,85817,85820,85824],{},[24,85816,355],{},[24,85818,85819],{},"an internet connection",[24,85821,6728,85822,3910],{},[49,85823,303],{},[24,85825,85826,85827,5889,85829],{},"a basic understanding of loops and ",[49,85828,3336],{},[49,85830,3552],{},[14,85832,85833,85834,186],{},"If you are new to error handling, see ",[372,85835,85836],{"href":27002},"using try-except, else, and finally in Python",[37,85838,85840],{"id":85839},"install-the-requests-package","Install the requests package",[14,85842,3857,85843,304],{},[49,85844,303],{},[14,85846,24874],{},[42,85848,85849],{"className":390,"code":391,"language":392,"meta":47,"style":47},[49,85850,85851],{"__ignoreMap":47},[52,85852,85853,85855,85857],{"class":54,"line":55},[52,85854,400],{"class":399},[52,85856,403],{"class":87},[52,85858,63],{"class":87},[14,85860,11958],{},[309,85862,85863,85868,85874],{},[24,85864,85865,85867],{},[49,85866,303],{}," is not part of Python's basic built-in functions",[24,85869,85870,85871,85873],{},"if it is missing, ",[49,85872,4108],{}," will fail",[24,85875,85876,85877],{},"you may see a ",[49,85878,1693],{},[14,85880,85881],{},"You can check whether it is installed with:",[42,85883,85885],{"className":390,"code":85884,"language":392,"meta":47,"style":47},"pip show requests\n",[49,85886,85887],{"__ignoreMap":47},[52,85888,85889,85891,85893],{"class":54,"line":55},[52,85890,400],{"class":399},[52,85892,2339],{"class":87},[52,85894,63],{"class":87},[14,85896,85897,85898,186],{},"If you get an import error, see ",[372,85899,85900],{"href":1730},"how to fix ModuleNotFoundError: No module named X",[37,85902,25641],{"id":25640},[14,85904,25644],{},[42,85906,85907],{"className":44,"code":85622,"language":46,"meta":47,"style":47},[49,85908,85909,85915,85919,85927,85937,85947,85955,85959,85963,85975,85981,86007,86025,86041],{"__ignoreMap":47},[52,85910,85911,85913],{"class":54,"line":55},[52,85912,59],{"class":58},[52,85914,63],{"class":62},[52,85916,85917],{"class":54,"line":66},[52,85918,70],{"emptyLinePlaceholder":69},[52,85920,85921,85923,85925],{"class":54,"line":73},[52,85922,74661],{"class":62},[52,85924,80],{"class":79},[52,85926,16627],{"class":102},[52,85928,85929,85931,85933,85935],{"class":54,"line":94},[52,85930,109],{"class":83},[52,85932,74682],{"class":87},[52,85934,115],{"class":83},[52,85936,125],{"class":102},[52,85938,85939,85941,85943,85945],{"class":54,"line":106},[52,85940,109],{"class":83},[52,85942,73936],{"class":87},[52,85944,115],{"class":83},[52,85946,125],{"class":102},[52,85948,85949,85951,85953],{"class":54,"line":128},[52,85950,109],{"class":83},[52,85952,85669],{"class":87},[52,85954,91],{"class":83},[52,85956,85957],{"class":54,"line":148},[52,85958,1147],{"class":102},[52,85960,85961],{"class":54,"line":164},[52,85962,70],{"emptyLinePlaceholder":69},[52,85964,85965,85967,85969,85971,85973],{"class":54,"line":170},[52,85966,12265],{"class":58},[52,85968,74712],{"class":62},[52,85970,11213],{"class":58},[52,85972,74717],{"class":62},[52,85974,1330],{"class":102},[52,85976,85977,85979],{"class":54,"line":175},[52,85978,20482],{"class":58},[52,85980,1330],{"class":102},[52,85982,85983,85985,85987,85989,85991,85993,85995,85997,85999,86001,86003,86005],{"class":54,"line":214},[52,85984,74730],{"class":62},[52,85986,80],{"class":79},[52,85988,183],{"class":62},[52,85990,186],{"class":102},[52,85992,190],{"class":189},[52,85994,193],{"class":102},[52,85996,196],{"class":189},[52,85998,199],{"class":102},[52,86000,3368],{"class":202},[52,86002,80],{"class":79},[52,86004,9479],{"class":121},[52,86006,211],{"class":102},[52,86008,86009,86011,86013,86015,86017,86019,86021,86023],{"class":54,"line":233},[52,86010,1470],{"class":241},[52,86012,193],{"class":102},[52,86014,196],{"class":189},[52,86016,199],{"class":102},[52,86018,222],{"class":189},[52,86020,186],{"class":102},[52,86022,1321],{"class":1320},[52,86024,211],{"class":102},[52,86026,86027,86029,86031,86033,86035,86037,86039],{"class":54,"line":238},[52,86028,20512],{"class":58},[52,86030,183],{"class":62},[52,86032,186],{"class":102},[52,86034,3564],{"class":1320},[52,86036,3567],{"class":58},[52,86038,3570],{"class":62},[52,86040,1330],{"class":102},[52,86042,86043,86045,86047,86049,86051,86053,86055,86057,86059,86061],{"class":54,"line":272},[52,86044,1470],{"class":241},[52,86046,193],{"class":102},[52,86048,196],{"class":189},[52,86050,199],{"class":102},[52,86052,84],{"class":83},[52,86054,85772],{"class":87},[52,86056,115],{"class":83},[52,86058,199],{"class":102},[52,86060,3570],{"class":189},[52,86062,211],{"class":102},[639,86064,86066],{"id":86065},"_1-create-a-list-of-urls","1. Create a list of URLs",[42,86068,86070],{"className":44,"code":86069,"language":46,"meta":47,"style":47},"urls = [\n    \"https:\u002F\u002Fwww.python.org\",\n    \"https:\u002F\u002Fexample.com\",\n    \"https:\u002F\u002Fthis-url-does-not-exist-12345.com\"\n]\n",[49,86071,86072,86080,86090,86100,86108],{"__ignoreMap":47},[52,86073,86074,86076,86078],{"class":54,"line":55},[52,86075,74661],{"class":62},[52,86077,80],{"class":79},[52,86079,16627],{"class":102},[52,86081,86082,86084,86086,86088],{"class":54,"line":66},[52,86083,109],{"class":83},[52,86085,74682],{"class":87},[52,86087,115],{"class":83},[52,86089,125],{"class":102},[52,86091,86092,86094,86096,86098],{"class":54,"line":73},[52,86093,109],{"class":83},[52,86095,73936],{"class":87},[52,86097,115],{"class":83},[52,86099,125],{"class":102},[52,86101,86102,86104,86106],{"class":54,"line":94},[52,86103,109],{"class":83},[52,86105,85669],{"class":87},[52,86107,91],{"class":83},[52,86109,86110],{"class":54,"line":106},[52,86111,1147],{"class":102},[14,86113,86114],{},"This list stores the URLs you want to check.",[14,86116,86117],{},"Each item is a string. Later, the loop will process them one by one.",[639,86119,86121],{"id":86120},"_2-loop-through-the-list","2. Loop through the list",[42,86123,86125],{"className":44,"code":86124,"language":46,"meta":47,"style":47},"for url in urls:\n",[49,86126,86127],{"__ignoreMap":47},[52,86128,86129,86131,86133,86135,86137],{"class":54,"line":55},[52,86130,12265],{"class":58},[52,86132,74712],{"class":62},[52,86134,11213],{"class":58},[52,86136,74717],{"class":62},[52,86138,1330],{"class":102},[14,86140,86141],{},"This line goes through the list one URL at a time.",[14,86143,86144],{},"If the list has 3 URLs, the loop runs 3 times.",[639,86146,86148,86149],{"id":86147},"_3-send-a-request-with-requestsget","3. Send a request with ",[49,86150,774],{},[42,86152,86154],{"className":44,"code":86153,"language":46,"meta":47,"style":47},"response = requests.get(url, timeout=5)\n",[49,86155,86156],{"__ignoreMap":47},[52,86157,86158,86160,86162,86164,86166,86168,86170,86172,86174,86176,86178,86180],{"class":54,"line":55},[52,86159,178],{"class":62},[52,86161,80],{"class":79},[52,86163,183],{"class":62},[52,86165,186],{"class":102},[52,86167,190],{"class":189},[52,86169,193],{"class":102},[52,86171,196],{"class":189},[52,86173,199],{"class":102},[52,86175,3368],{"class":202},[52,86177,80],{"class":79},[52,86179,9479],{"class":121},[52,86181,211],{"class":102},[14,86183,86184,86185,86187],{},"This sends an HTTP ",[49,86186,85798],{}," request to the current URL.",[14,86189,1097],{},[309,86191,86192,86197],{},[24,86193,86194,86196],{},[49,86195,196],{}," is the web address to check",[24,86198,86199,86202],{},[49,86200,86201],{},"timeout=5"," means \"stop waiting after 5 seconds\"",[14,86204,86205],{},"Without a timeout, your script might wait too long if a site is slow or not responding.",[14,86207,86208,86209,186],{},"If you want to learn more about making web requests, see ",[372,86210,375],{"href":374},[639,86212,86214],{"id":86213},"_4-read-the-status-code","4. Read the status code",[42,86216,86218],{"className":44,"code":86217,"language":46,"meta":47,"style":47},"print(url, response.status_code)\n",[49,86219,86220],{"__ignoreMap":47},[52,86221,86222,86224,86226,86228,86230,86232,86234,86236],{"class":54,"line":55},[52,86223,242],{"class":241},[52,86225,193],{"class":102},[52,86227,196],{"class":189},[52,86229,199],{"class":102},[52,86231,222],{"class":189},[52,86233,186],{"class":102},[52,86235,1321],{"class":1320},[52,86237,211],{"class":102},[14,86239,358,86240,86242],{},[49,86241,2872],{}," value tells you how the server responded.",[14,86244,11845],{},[42,86246,86249],{"className":86247,"code":86248,"language":955,"meta":47},[953],"https:\u002F\u002Fwww.python.org 200\nhttps:\u002F\u002Fexample.com 200\nhttps:\u002F\u002Fthis-url-does-not-exist-12345.com ERROR: HTTPSConnectionPool(...)\n",[49,86250,86248],{"__ignoreMap":47},[639,86252,86254],{"id":86253},"_5-catch-request-errors","5. Catch request errors",[42,86256,86258],{"className":44,"code":86257,"language":46,"meta":47,"style":47},"except requests.RequestException as e:\n    print(url, \"ERROR:\", e)\n",[49,86259,86260,86276],{"__ignoreMap":47},[52,86261,86262,86264,86266,86268,86270,86272,86274],{"class":54,"line":55},[52,86263,3552],{"class":58},[52,86265,183],{"class":62},[52,86267,186],{"class":102},[52,86269,3564],{"class":1320},[52,86271,3567],{"class":58},[52,86273,3570],{"class":62},[52,86275,1330],{"class":102},[52,86277,86278,86280,86282,86284,86286,86288,86290,86292,86294,86296],{"class":54,"line":66},[52,86279,1599],{"class":241},[52,86281,193],{"class":102},[52,86283,196],{"class":189},[52,86285,199],{"class":102},[52,86287,84],{"class":83},[52,86289,85772],{"class":87},[52,86291,115],{"class":83},[52,86293,199],{"class":102},[52,86295,3570],{"class":189},[52,86297,211],{"class":102},[14,86299,86300],{},"This catches network-related problems, such as:",[309,86302,86303,86305,86308,86311],{},[24,86304,24831],{},[24,86306,86307],{},"timeout errors",[24,86309,86310],{},"invalid domains",[24,86312,86313],{},"other request problems",[14,86315,86316,86317,5889,86319,86321],{},"The script keeps running even if one URL fails. That is why ",[49,86318,3336],{},[49,86320,3552],{}," is useful here.",[14,86323,86324,86325,186],{},"For more about working with responses, see ",[372,86326,1172],{"href":1171},[37,86328,86330],{"id":86329},"understanding-common-status-codes","Understanding common status codes",[14,86332,86333],{},"A status code is a number returned by the server.",[14,86335,86336],{},"Common ones include:",[309,86338,86339,86344,86353,86358],{},[24,86340,86341,86343],{},[49,86342,2727],{}," means success",[24,86345,86346,857,86349,86352],{},[49,86347,86348],{},"301",[49,86350,86351],{},"302"," means redirect",[24,86354,86355,86357],{},[49,86356,4822],{}," means page not found",[24,86359,86360,86362],{},[49,86361,2960],{}," means server error",[14,86364,4259],{},[42,86366,86368],{"className":44,"code":86367,"language":46,"meta":47,"style":47},"import requests\n\nresponse = requests.get(\"https:\u002F\u002Fexample.com\", timeout=5)\nprint(response.status_code)\n",[49,86369,86370,86376,86380,86410],{"__ignoreMap":47},[52,86371,86372,86374],{"class":54,"line":55},[52,86373,59],{"class":58},[52,86375,63],{"class":62},[52,86377,86378],{"class":54,"line":66},[52,86379,70],{"emptyLinePlaceholder":69},[52,86381,86382,86384,86386,86388,86390,86392,86394,86396,86398,86400,86402,86404,86406,86408],{"class":54,"line":73},[52,86383,178],{"class":62},[52,86385,80],{"class":79},[52,86387,183],{"class":62},[52,86389,186],{"class":102},[52,86391,190],{"class":189},[52,86393,193],{"class":102},[52,86395,115],{"class":83},[52,86397,73936],{"class":87},[52,86399,115],{"class":83},[52,86401,199],{"class":102},[52,86403,3368],{"class":202},[52,86405,80],{"class":79},[52,86407,9479],{"class":121},[52,86409,211],{"class":102},[52,86411,86412,86414,86416,86418,86420,86422],{"class":54,"line":94},[52,86413,242],{"class":241},[52,86415,193],{"class":102},[52,86417,1756],{"class":189},[52,86419,186],{"class":102},[52,86421,1321],{"class":1320},[52,86423,211],{"class":102},[14,86425,40163],{},[42,86427,86430],{"className":86428,"code":86429,"language":955,"meta":47},[953],"200\n",[49,86431,86429],{"__ignoreMap":47},[14,86433,86434],{},"A status code tells you what happened at a high level, but not always the full reason.",[14,86436,16291],{},[309,86438,86439,86444],{},[24,86440,86441,86443],{},[49,86442,4822],{}," means the server was reached, but the page was not found",[24,86445,86446],{},"this is different from a connection error, where Python could not reach the server at all",[37,86448,86450],{"id":86449},"beginner-improvements-to-the-script","Beginner improvements to the script",[14,86452,86453],{},"Once the basic version works, you can improve it in simple ways.",[14,86455,50665],{},[309,86457,86458,86461,86464,86467,86470],{},[24,86459,86460],{},"read URLs from a text file instead of hard-coding them",[24,86462,86463],{},"save results to a CSV file",[24,86465,86466],{},"mark working and broken URLs clearly",[24,86468,86469],{},"skip blank lines",[24,86471,86472],{},"check redirects if needed",[14,86474,86475],{},"Here is a small improved version that labels results more clearly:",[42,86477,86479],{"className":44,"code":86478,"language":46,"meta":47,"style":47},"import requests\n\nurls = [\n    \"https:\u002F\u002Fwww.python.org\",\n    \"https:\u002F\u002Fexample.com\",\n    \"\",\n    \"https:\u002F\u002Fthis-url-does-not-exist-12345.com\"\n]\n\nfor url in urls:\n    if not url:\n        continue\n\n    try:\n        response = requests.get(url, timeout=5)\n\n        if response.status_code == 200:\n            print(url, \"- WORKING\")\n        else:\n            print(url, f\"- STATUS {response.status_code}\")\n\n    except requests.RequestException as e:\n        print(url, \"- ERROR:\", e)\n",[49,86480,86481,86487,86491,86499,86509,86519,86526,86534,86538,86542,86554,86565,86569,86573,86579,86605,86609,86625,86644,86650,86679,86683,86699],{"__ignoreMap":47},[52,86482,86483,86485],{"class":54,"line":55},[52,86484,59],{"class":58},[52,86486,63],{"class":62},[52,86488,86489],{"class":54,"line":66},[52,86490,70],{"emptyLinePlaceholder":69},[52,86492,86493,86495,86497],{"class":54,"line":73},[52,86494,74661],{"class":62},[52,86496,80],{"class":79},[52,86498,16627],{"class":102},[52,86500,86501,86503,86505,86507],{"class":54,"line":94},[52,86502,109],{"class":83},[52,86504,74682],{"class":87},[52,86506,115],{"class":83},[52,86508,125],{"class":102},[52,86510,86511,86513,86515,86517],{"class":54,"line":106},[52,86512,109],{"class":83},[52,86514,73936],{"class":87},[52,86516,115],{"class":83},[52,86518,125],{"class":102},[52,86520,86521,86524],{"class":54,"line":128},[52,86522,86523],{"class":83},"    \"\"",[52,86525,125],{"class":102},[52,86527,86528,86530,86532],{"class":54,"line":148},[52,86529,109],{"class":83},[52,86531,85669],{"class":87},[52,86533,91],{"class":83},[52,86535,86536],{"class":54,"line":164},[52,86537,1147],{"class":102},[52,86539,86540],{"class":54,"line":170},[52,86541,70],{"emptyLinePlaceholder":69},[52,86543,86544,86546,86548,86550,86552],{"class":54,"line":175},[52,86545,12265],{"class":58},[52,86547,74712],{"class":62},[52,86549,11213],{"class":58},[52,86551,74717],{"class":62},[52,86553,1330],{"class":102},[52,86555,86556,86558,86560,86563],{"class":54,"line":214},[52,86557,1438],{"class":58},[52,86559,1447],{"class":79},[52,86561,86562],{"class":62}," url",[52,86564,1330],{"class":102},[52,86566,86567],{"class":54,"line":233},[52,86568,18703],{"class":58},[52,86570,86571],{"class":54,"line":238},[52,86572,70],{"emptyLinePlaceholder":69},[52,86574,86575,86577],{"class":54,"line":272},[52,86576,20482],{"class":58},[52,86578,1330],{"class":102},[52,86580,86581,86583,86585,86587,86589,86591,86593,86595,86597,86599,86601,86603],{"class":54,"line":1348},[52,86582,74730],{"class":62},[52,86584,80],{"class":79},[52,86586,183],{"class":62},[52,86588,186],{"class":102},[52,86590,190],{"class":189},[52,86592,193],{"class":102},[52,86594,196],{"class":189},[52,86596,199],{"class":102},[52,86598,3368],{"class":202},[52,86600,80],{"class":79},[52,86602,9479],{"class":121},[52,86604,211],{"class":102},[52,86606,86607],{"class":54,"line":1376},[52,86608,70],{"emptyLinePlaceholder":69},[52,86610,86611,86613,86615,86617,86619,86621,86623],{"class":54,"line":1381},[52,86612,11168],{"class":58},[52,86614,222],{"class":62},[52,86616,186],{"class":102},[52,86618,1321],{"class":1320},[52,86620,1324],{"class":79},[52,86622,1327],{"class":121},[52,86624,1330],{"class":102},[52,86626,86627,86629,86631,86633,86635,86637,86640,86642],{"class":54,"line":1406},[52,86628,11180],{"class":241},[52,86630,193],{"class":102},[52,86632,196],{"class":189},[52,86634,199],{"class":102},[52,86636,84],{"class":83},[52,86638,86639],{"class":87},"- WORKING",[52,86641,115],{"class":83},[52,86643,211],{"class":102},[52,86645,86646,86648],{"class":54,"line":1430},[52,86647,11196],{"class":58},[52,86649,1330],{"class":102},[52,86651,86652,86654,86656,86658,86660,86662,86665,86667,86669,86671,86673,86675,86677],{"class":54,"line":1435},[52,86653,11180],{"class":241},[52,86655,193],{"class":102},[52,86657,196],{"class":189},[52,86659,199],{"class":102},[52,86661,6126],{"class":1475},[52,86663,86664],{"class":87},"\"- STATUS ",[52,86666,1482],{"class":121},[52,86668,1756],{"class":189},[52,86670,186],{"class":102},[52,86672,1321],{"class":1320},[52,86674,1488],{"class":121},[52,86676,115],{"class":87},[52,86678,211],{"class":102},[52,86680,86681],{"class":54,"line":1467},[52,86682,70],{"emptyLinePlaceholder":69},[52,86684,86685,86687,86689,86691,86693,86695,86697],{"class":54,"line":1495},[52,86686,20512],{"class":58},[52,86688,183],{"class":62},[52,86690,186],{"class":102},[52,86692,3564],{"class":1320},[52,86694,3567],{"class":58},[52,86696,3570],{"class":62},[52,86698,1330],{"class":102},[52,86700,86701,86703,86705,86707,86709,86711,86714,86716,86718,86720],{"class":54,"line":1531},[52,86702,1470],{"class":241},[52,86704,193],{"class":102},[52,86706,196],{"class":189},[52,86708,199],{"class":102},[52,86710,84],{"class":83},[52,86712,86713],{"class":87},"- ERROR:",[52,86715,115],{"class":83},[52,86717,199],{"class":102},[52,86719,3570],{"class":189},[52,86721,211],{"class":102},[14,86723,24512],{},[309,86725,86726,86729,86735],{},[24,86727,86728],{},"skips blank values",[24,86730,86731,86732,86734],{},"marks ",[49,86733,2727],{}," responses as working",[24,86736,86737],{},"still shows other status codes and errors",[14,86739,86740,86741,86743],{},"If you later want to load URLs from files, the ",[372,86742,29945],{"href":29944}," can help when working with file paths.",[37,86745,86747],{"id":86746},"common-problems-when-checking-urls","Common problems when checking URLs",[14,86749,86750],{},"Here are common issues beginners run into:",[309,86752,86753,86759,86762,86765,86768],{},[24,86754,86755,4812,86757,1671],{},[49,86756,1693],{},[49,86758,303],{},[24,86760,86761],{},"timeout errors if the site is slow",[24,86763,86764],{},"connection errors if the domain is wrong",[24,86766,86767],{},"some websites block automated requests",[24,86769,86770,86771,86773],{},"a valid domain can still return a ",[49,86772,4822],{}," page",[14,86775,86776],{},"Also remember:",[309,86778,86779,86787,86790],{},[24,86780,86781,86782,857,86785],{},"the URL should usually include ",[49,86783,86784],{},"http:\u002F\u002F",[49,86786,75117],{},[24,86788,86789],{},"no internet connection means the request cannot succeed",[24,86791,86792],{},"no timeout can make the script wait too long",[14,86794,14379],{},[309,86796,86797,86801,86808,86811,86814],{},[24,86798,86799,4790],{},[49,86800,303],{},[24,86802,86803,86804,857,86806],{},"URL is missing ",[49,86805,86784],{},[49,86807,75117],{},[24,86809,86810],{},"site is offline or domain name is wrong",[24,86812,86813],{},"internet connection is unavailable",[24,86815,86816],{},"the request waits too long because no timeout was set",[14,86818,75158],{},[42,86820,86822],{"className":390,"code":86821,"language":392,"meta":47,"style":47},"python --version\npip install requests\npip show requests\npython your_script.py\nping python.org\n",[49,86823,86824,86830,86838,86846,86852],{"__ignoreMap":47},[52,86825,86826,86828],{"class":54,"line":55},[52,86827,46],{"class":399},[52,86829,2324],{"class":420},[52,86831,86832,86834,86836],{"class":54,"line":66},[52,86833,400],{"class":399},[52,86835,403],{"class":87},[52,86837,63],{"class":87},[52,86839,86840,86842,86844],{"class":54,"line":73},[52,86841,400],{"class":399},[52,86843,2339],{"class":87},[52,86845,63],{"class":87},[52,86847,86848,86850],{"class":54,"line":94},[52,86849,46],{"class":399},[52,86851,17494],{"class":87},[52,86853,86854,86856],{"class":54,"line":106},[52,86855,56082],{"class":399},[52,86857,86858],{"class":87}," python.org\n",[37,86860,86862],{"id":86861},"what-this-example-does-not-cover","What this example does not cover",[14,86864,86865],{},"This example is intentionally simple.",[14,86867,86868],{},"It does not cover:",[309,86870,86871,86874,86877,86880],{},[24,86872,86873],{},"advanced HTTP concepts",[24,86875,86876],{},"asynchronous requests",[24,86878,86879],{},"authentication",[24,86881,86882],{},"large-scale website monitoring",[14,86884,86885],{},"That keeps the script easier to understand if you are still learning the basics.",[37,86887,1942],{"id":1941},[639,86889,86891],{"id":86890},"do-i-need-the-requests-library-for-a-url-checker","Do I need the requests library for a URL checker?",[14,86893,86894],{},"For this example, yes. It makes HTTP requests easier for beginners.",[639,86896,86898],{"id":86897},"why-does-a-url-return-404-instead-of-causing-an-exception","Why does a URL return 404 instead of causing an exception?",[14,86900,86901,86902,86904],{},"Because the server responded successfully. A ",[49,86903,4822],{}," is still a real HTTP response.",[639,86906,86908],{"id":86907},"what-is-the-difference-between-a-status-code-and-an-error","What is the difference between a status code and an error?",[14,86910,86911],{},"A status code comes from the server. An error usually means Python could not complete the request.",[639,86913,86915],{"id":86914},"can-i-check-many-urls-at-once","Can I check many URLs at once?",[14,86917,86918],{},"Yes. Store them in a list or file and loop through them.",[37,86920,2005],{"id":2004},[309,86922,86923,86927,86931,86936,86941],{},[24,86924,86925],{},[372,86926,2012],{"href":374},[24,86928,86929],{},[372,86930,2017],{"href":1171},[24,86932,86933],{},[372,86934,86935],{"href":27002},"Using try-except, else, and finally in Python",[24,86937,86938],{},[372,86939,86940],{"href":1730},"How to fix ModuleNotFoundError: No module named X",[24,86942,86943],{},[372,86944,2028],{"href":2027},[2034,86946,86947],{},"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 .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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":66,"depth":66,"links":86949},[86950,86951,86952,86953,86954,86962,86963,86964,86965,86966,86972],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":346,"depth":66,"text":347},{"id":85839,"depth":66,"text":85840},{"id":25640,"depth":66,"text":25641,"children":86955},[86956,86957,86958,86960,86961],{"id":86065,"depth":73,"text":86066},{"id":86120,"depth":73,"text":86121},{"id":86147,"depth":73,"text":86959},"3. Send a request with requests.get()",{"id":86213,"depth":73,"text":86214},{"id":86253,"depth":73,"text":86254},{"id":86329,"depth":66,"text":86330},{"id":86449,"depth":66,"text":86450},{"id":86746,"depth":66,"text":86747},{"id":86861,"depth":66,"text":86862},{"id":1941,"depth":66,"text":1942,"children":86967},[86968,86969,86970,86971],{"id":86890,"depth":73,"text":86891},{"id":86897,"depth":73,"text":86898},{"id":86907,"depth":73,"text":86908},{"id":86914,"depth":73,"text":86915},{"id":2004,"depth":66,"text":2005},"Master python url checker script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-url-checker-script-example",{"title":85590,"description":86973},"examples\u002Fpython-url-checker-script-example","sio9USnP8eLB7g4QKAP2VBRztkp0yOrPblYnmMXaQSk",{"id":4,"title":5,"body":86980,"description":2074,"extension":2075,"meta":88657,"navigation":69,"path":2077,"seo":88658,"stem":2079,"__hash__":2080},{"type":7,"value":86981,"toc":88625},[86982,86984,86986,86988,86998,87000,87180,87184,87186,87194,87196,87198,87210,87212,87214,87226,87230,87232,87236,87238,87250,87252,87268,87270,87286,87288,87290,87470,87472,87482,87486,87488,87504,87506,87508,87566,87568,87570,87582,87586,87616,87618,87622,87624,87642,87648,87654,87656,87712,87714,87724,87726,87728,87730,87735,87737,87741,87743,87855,87857,87873,87875,87901,87903,87913,87917,87919,87921,88319,88321,88335,88337,88339,88353,88355,88375,88385,88387,88389,88391,88409,88411,88413,88431,88433,88435,88453,88455,88459,88481,88483,88485,88487,88489,88519,88521,88523,88559,88561,88563,88565,88567,88571,88575,88577,88585,88589,88591,88595,88597,88599,88601,88623],[10,86983,5],{"id":12},[14,86985,16],{},[14,86987,19],{},[21,86989,86990,86992,86994,86996],{},[24,86991,26],{},[24,86993,29],{},[24,86995,32],{},[24,86997,35],{},[37,86999,40],{"id":39},[42,87001,87002],{"className":44,"code":45,"language":46,"meta":47,"style":47},[49,87003,87004,87010,87014,87026,87034,87048,87064,87076,87080,87084,87110,87124,87128,87154],{"__ignoreMap":47},[52,87005,87006,87008],{"class":54,"line":55},[52,87007,59],{"class":58},[52,87009,63],{"class":62},[52,87011,87012],{"class":54,"line":66},[52,87013,70],{"emptyLinePlaceholder":69},[52,87015,87016,87018,87020,87022,87024],{"class":54,"line":73},[52,87017,76],{"class":62},[52,87019,80],{"class":79},[52,87021,84],{"class":83},[52,87023,88],{"class":87},[52,87025,91],{"class":83},[52,87027,87028,87030,87032],{"class":54,"line":94},[52,87029,97],{"class":62},[52,87031,80],{"class":79},[52,87033,103],{"class":102},[52,87035,87036,87038,87040,87042,87044,87046],{"class":54,"line":106},[52,87037,109],{"class":83},[52,87039,112],{"class":87},[52,87041,115],{"class":83},[52,87043,118],{"class":102},[52,87045,122],{"class":121},[52,87047,125],{"class":102},[52,87049,87050,87052,87054,87056,87058,87060,87062],{"class":54,"line":128},[52,87051,109],{"class":83},[52,87053,133],{"class":87},[52,87055,115],{"class":83},[52,87057,118],{"class":102},[52,87059,140],{"class":79},[52,87061,143],{"class":121},[52,87063,125],{"class":102},[52,87065,87066,87068,87070,87072,87074],{"class":54,"line":148},[52,87067,109],{"class":83},[52,87069,153],{"class":87},[52,87071,115],{"class":83},[52,87073,118],{"class":102},[52,87075,161],{"class":160},[52,87077,87078],{"class":54,"line":164},[52,87079,167],{"class":102},[52,87081,87082],{"class":54,"line":170},[52,87083,70],{"emptyLinePlaceholder":69},[52,87085,87086,87088,87090,87092,87094,87096,87098,87100,87102,87104,87106,87108],{"class":54,"line":175},[52,87087,178],{"class":62},[52,87089,80],{"class":79},[52,87091,183],{"class":62},[52,87093,186],{"class":102},[52,87095,190],{"class":189},[52,87097,193],{"class":102},[52,87099,196],{"class":189},[52,87101,199],{"class":102},[52,87103,203],{"class":202},[52,87105,80],{"class":79},[52,87107,208],{"class":189},[52,87109,211],{"class":102},[52,87111,87112,87114,87116,87118,87120,87122],{"class":54,"line":214},[52,87113,217],{"class":62},[52,87115,80],{"class":79},[52,87117,222],{"class":62},[52,87119,186],{"class":102},[52,87121,227],{"class":189},[52,87123,230],{"class":102},[52,87125,87126],{"class":54,"line":233},[52,87127,70],{"emptyLinePlaceholder":69},[52,87129,87130,87132,87134,87136,87138,87140,87142,87144,87146,87148,87150,87152],{"class":54,"line":238},[52,87131,242],{"class":241},[52,87133,193],{"class":102},[52,87135,247],{"class":189},[52,87137,250],{"class":102},[52,87139,115],{"class":83},[52,87141,153],{"class":87},[52,87143,115],{"class":83},[52,87145,259],{"class":102},[52,87147,115],{"class":83},[52,87149,264],{"class":87},[52,87151,115],{"class":83},[52,87153,269],{"class":102},[52,87155,87156,87158,87160,87162,87164,87166,87168,87170,87172,87174,87176,87178],{"class":54,"line":272},[52,87157,242],{"class":241},[52,87159,193],{"class":102},[52,87161,247],{"class":189},[52,87163,250],{"class":102},[52,87165,115],{"class":83},[52,87167,153],{"class":87},[52,87169,115],{"class":83},[52,87171,259],{"class":102},[52,87173,115],{"class":83},[52,87175,293],{"class":87},[52,87177,115],{"class":83},[52,87179,269],{"class":102},[14,87181,300,87182,304],{},[49,87183,303],{},[14,87185,307],{},[309,87187,87188,87190,87192],{},[24,87189,313],{},[24,87191,316],{},[24,87193,319],{},[37,87195,323],{"id":322},[14,87197,326],{},[309,87199,87200,87202,87204,87206,87208],{},[24,87201,331],{},[24,87203,334],{},[24,87205,337],{},[24,87207,340],{},[24,87209,343],{},[37,87211,347],{"id":346},[14,87213,350],{},[309,87215,87216,87218,87222,87224],{},[24,87217,355],{},[24,87219,358,87220,361],{},[49,87221,303],{},[24,87223,364],{},[24,87225,367],{},[14,87227,370,87228,186],{},[372,87229,375],{"href":374},[37,87231,379],{"id":378},[14,87233,358,87234,384],{},[49,87235,303],{},[14,87237,387],{},[42,87239,87240],{"className":390,"code":391,"language":392,"meta":47,"style":47},[49,87241,87242],{"__ignoreMap":47},[52,87243,87244,87246,87248],{"class":54,"line":55},[52,87245,400],{"class":399},[52,87247,403],{"class":87},[52,87249,63],{"class":87},[14,87251,408],{},[42,87253,87254],{"className":390,"code":411,"language":392,"meta":47,"style":47},[49,87255,87256],{"__ignoreMap":47},[52,87257,87258,87260,87262,87264,87266],{"class":54,"line":55},[52,87259,46],{"class":399},[52,87261,421],{"class":420},[52,87263,424],{"class":87},[52,87265,403],{"class":87},[52,87267,63],{"class":87},[14,87269,431],{},[42,87271,87272],{"className":390,"code":434,"language":392,"meta":47,"style":47},[49,87273,87274],{"__ignoreMap":47},[52,87275,87276,87278,87280,87282,87284],{"class":54,"line":55},[52,87277,46],{"class":399},[52,87279,443],{"class":420},[52,87281,84],{"class":83},[52,87283,448],{"class":87},[52,87285,91],{"class":83},[37,87287,454],{"id":453},[14,87289,457],{},[42,87291,87292],{"className":44,"code":45,"language":46,"meta":47,"style":47},[49,87293,87294,87300,87304,87316,87324,87338,87354,87366,87370,87374,87400,87414,87418,87444],{"__ignoreMap":47},[52,87295,87296,87298],{"class":54,"line":55},[52,87297,59],{"class":58},[52,87299,63],{"class":62},[52,87301,87302],{"class":54,"line":66},[52,87303,70],{"emptyLinePlaceholder":69},[52,87305,87306,87308,87310,87312,87314],{"class":54,"line":73},[52,87307,76],{"class":62},[52,87309,80],{"class":79},[52,87311,84],{"class":83},[52,87313,88],{"class":87},[52,87315,91],{"class":83},[52,87317,87318,87320,87322],{"class":54,"line":94},[52,87319,97],{"class":62},[52,87321,80],{"class":79},[52,87323,103],{"class":102},[52,87325,87326,87328,87330,87332,87334,87336],{"class":54,"line":106},[52,87327,109],{"class":83},[52,87329,112],{"class":87},[52,87331,115],{"class":83},[52,87333,118],{"class":102},[52,87335,122],{"class":121},[52,87337,125],{"class":102},[52,87339,87340,87342,87344,87346,87348,87350,87352],{"class":54,"line":128},[52,87341,109],{"class":83},[52,87343,133],{"class":87},[52,87345,115],{"class":83},[52,87347,118],{"class":102},[52,87349,140],{"class":79},[52,87351,143],{"class":121},[52,87353,125],{"class":102},[52,87355,87356,87358,87360,87362,87364],{"class":54,"line":148},[52,87357,109],{"class":83},[52,87359,153],{"class":87},[52,87361,115],{"class":83},[52,87363,118],{"class":102},[52,87365,161],{"class":160},[52,87367,87368],{"class":54,"line":164},[52,87369,167],{"class":102},[52,87371,87372],{"class":54,"line":170},[52,87373,70],{"emptyLinePlaceholder":69},[52,87375,87376,87378,87380,87382,87384,87386,87388,87390,87392,87394,87396,87398],{"class":54,"line":175},[52,87377,178],{"class":62},[52,87379,80],{"class":79},[52,87381,183],{"class":62},[52,87383,186],{"class":102},[52,87385,190],{"class":189},[52,87387,193],{"class":102},[52,87389,196],{"class":189},[52,87391,199],{"class":102},[52,87393,203],{"class":202},[52,87395,80],{"class":79},[52,87397,208],{"class":189},[52,87399,211],{"class":102},[52,87401,87402,87404,87406,87408,87410,87412],{"class":54,"line":214},[52,87403,217],{"class":62},[52,87405,80],{"class":79},[52,87407,222],{"class":62},[52,87409,186],{"class":102},[52,87411,227],{"class":189},[52,87413,230],{"class":102},[52,87415,87416],{"class":54,"line":233},[52,87417,70],{"emptyLinePlaceholder":69},[52,87419,87420,87422,87424,87426,87428,87430,87432,87434,87436,87438,87440,87442],{"class":54,"line":238},[52,87421,242],{"class":241},[52,87423,193],{"class":102},[52,87425,247],{"class":189},[52,87427,250],{"class":102},[52,87429,115],{"class":83},[52,87431,153],{"class":87},[52,87433,115],{"class":83},[52,87435,259],{"class":102},[52,87437,115],{"class":83},[52,87439,264],{"class":87},[52,87441,115],{"class":83},[52,87443,269],{"class":102},[52,87445,87446,87448,87450,87452,87454,87456,87458,87460,87462,87464,87466,87468],{"class":54,"line":272},[52,87447,242],{"class":241},[52,87449,193],{"class":102},[52,87451,247],{"class":189},[52,87453,250],{"class":102},[52,87455,115],{"class":83},[52,87457,153],{"class":87},[52,87459,115],{"class":83},[52,87461,259],{"class":102},[52,87463,115],{"class":83},[52,87465,293],{"class":87},[52,87467,115],{"class":83},[52,87469,269],{"class":102},[639,87471,642],{"id":641},[42,87473,87474],{"className":44,"code":645,"language":46,"meta":47,"style":47},[49,87475,87476],{"__ignoreMap":47},[52,87477,87478,87480],{"class":54,"line":55},[52,87479,59],{"class":58},[52,87481,63],{"class":62},[14,87483,656,87484,659],{},[49,87485,303],{},[639,87487,663],{"id":662},[42,87489,87490],{"className":44,"code":666,"language":46,"meta":47,"style":47},[49,87491,87492],{"__ignoreMap":47},[52,87493,87494,87496,87498,87500,87502],{"class":54,"line":55},[52,87495,76],{"class":62},[52,87497,80],{"class":79},[52,87499,84],{"class":83},[52,87501,88],{"class":87},[52,87503,91],{"class":83},[14,87505,683],{},[639,87507,687],{"id":686},[42,87509,87510],{"className":44,"code":690,"language":46,"meta":47,"style":47},[49,87511,87512,87520,87534,87550,87562],{"__ignoreMap":47},[52,87513,87514,87516,87518],{"class":54,"line":55},[52,87515,97],{"class":62},[52,87517,80],{"class":79},[52,87519,103],{"class":102},[52,87521,87522,87524,87526,87528,87530,87532],{"class":54,"line":66},[52,87523,109],{"class":83},[52,87525,112],{"class":87},[52,87527,115],{"class":83},[52,87529,118],{"class":102},[52,87531,122],{"class":121},[52,87533,125],{"class":102},[52,87535,87536,87538,87540,87542,87544,87546,87548],{"class":54,"line":73},[52,87537,109],{"class":83},[52,87539,133],{"class":87},[52,87541,115],{"class":83},[52,87543,118],{"class":102},[52,87545,140],{"class":79},[52,87547,143],{"class":121},[52,87549,125],{"class":102},[52,87551,87552,87554,87556,87558,87560],{"class":54,"line":94},[52,87553,109],{"class":83},[52,87555,153],{"class":87},[52,87557,115],{"class":83},[52,87559,118],{"class":102},[52,87561,161],{"class":160},[52,87563,87564],{"class":54,"line":106},[52,87565,167],{"class":102},[14,87567,749],{},[14,87569,752],{},[309,87571,87572,87578],{},[24,87573,87574,759,87576,762],{},[49,87575,112],{},[49,87577,133],{},[24,87579,87580,767],{},[49,87581,153],{},[639,87583,771,87584,775],{"id":770},[49,87585,774],{},[42,87587,87588],{"className":44,"code":778,"language":46,"meta":47,"style":47},[49,87589,87590],{"__ignoreMap":47},[52,87591,87592,87594,87596,87598,87600,87602,87604,87606,87608,87610,87612,87614],{"class":54,"line":55},[52,87593,178],{"class":62},[52,87595,80],{"class":79},[52,87597,183],{"class":62},[52,87599,186],{"class":102},[52,87601,190],{"class":189},[52,87603,193],{"class":102},[52,87605,196],{"class":189},[52,87607,199],{"class":102},[52,87609,203],{"class":202},[52,87611,80],{"class":79},[52,87613,208],{"class":189},[52,87615,211],{"class":102},[14,87617,809],{},[14,87619,812,87620,816],{},[49,87621,815],{},[639,87623,820],{"id":819},[42,87625,87626],{"className":44,"code":823,"language":46,"meta":47,"style":47},[49,87627,87628],{"__ignoreMap":47},[52,87629,87630,87632,87634,87636,87638,87640],{"class":54,"line":55},[52,87631,217],{"class":62},[52,87633,80],{"class":79},[52,87635,222],{"class":62},[52,87637,186],{"class":102},[52,87639,227],{"class":189},[52,87641,230],{"class":102},[14,87643,842,87644,87646,849],{},[844,87645],{},[49,87647,848],{},[14,87649,852,87650,857,87652,186],{},[372,87651,856],{"href":855},[372,87653,861],{"href":860},[639,87655,865],{"id":864},[42,87657,87658],{"className":44,"code":868,"language":46,"meta":47,"style":47},[49,87659,87660,87686],{"__ignoreMap":47},[52,87661,87662,87664,87666,87668,87670,87672,87674,87676,87678,87680,87682,87684],{"class":54,"line":55},[52,87663,242],{"class":241},[52,87665,193],{"class":102},[52,87667,247],{"class":189},[52,87669,250],{"class":102},[52,87671,115],{"class":83},[52,87673,153],{"class":87},[52,87675,115],{"class":83},[52,87677,259],{"class":102},[52,87679,115],{"class":83},[52,87681,264],{"class":87},[52,87683,115],{"class":83},[52,87685,269],{"class":102},[52,87687,87688,87690,87692,87694,87696,87698,87700,87702,87704,87706,87708,87710],{"class":54,"line":66},[52,87689,242],{"class":241},[52,87691,193],{"class":102},[52,87693,247],{"class":189},[52,87695,250],{"class":102},[52,87697,115],{"class":83},[52,87699,153],{"class":87},[52,87701,115],{"class":83},[52,87703,259],{"class":102},[52,87705,115],{"class":83},[52,87707,293],{"class":87},[52,87709,115],{"class":83},[52,87711,269],{"class":102},[14,87713,925],{},[14,87715,928,87716,931,87718,934,87720,759,87722,939],{},[49,87717,153],{},[844,87719],{},[49,87721,264],{},[49,87723,293],{},[639,87725,943],{"id":942},[14,87727,946],{},[14,87729,949],{},[42,87731,87733],{"className":87732,"code":954,"language":955,"meta":47},[953],[49,87734,954],{"__ignoreMap":47},[37,87736,961],{"id":960},[14,87738,964,87739,967],{},[49,87740,848],{},[14,87742,970],{},[42,87744,87745],{"className":44,"code":973,"language":46,"meta":47,"style":47},[49,87746,87747,87751,87765,87781,87793,87807,87821,87835,87847,87851],{"__ignoreMap":47},[52,87748,87749],{"class":54,"line":55},[52,87750,980],{"class":102},[52,87752,87753,87755,87757,87759,87761,87763],{"class":54,"line":66},[52,87754,109],{"class":83},[52,87756,112],{"class":87},[52,87758,115],{"class":83},[52,87760,118],{"class":102},[52,87762,993],{"class":121},[52,87764,125],{"class":102},[52,87766,87767,87769,87771,87773,87775,87777,87779],{"class":54,"line":73},[52,87768,109],{"class":83},[52,87770,133],{"class":87},[52,87772,115],{"class":83},[52,87774,118],{"class":102},[52,87776,140],{"class":79},[52,87778,1010],{"class":121},[52,87780,125],{"class":102},[52,87782,87783,87785,87787,87789,87791],{"class":54,"line":94},[52,87784,109],{"class":83},[52,87786,153],{"class":87},[52,87788,115],{"class":83},[52,87790,118],{"class":102},[52,87792,103],{"class":102},[52,87794,87795,87797,87799,87801,87803,87805],{"class":54,"line":106},[52,87796,1029],{"class":83},[52,87798,264],{"class":87},[52,87800,115],{"class":83},[52,87802,118],{"class":102},[52,87804,1038],{"class":121},[52,87806,125],{"class":102},[52,87808,87809,87811,87813,87815,87817,87819],{"class":54,"line":128},[52,87810,1029],{"class":83},[52,87812,293],{"class":87},[52,87814,115],{"class":83},[52,87816,118],{"class":102},[52,87818,1053],{"class":121},[52,87820,125],{"class":102},[52,87822,87823,87825,87827,87829,87831,87833],{"class":54,"line":148},[52,87824,1029],{"class":83},[52,87826,1062],{"class":87},[52,87828,115],{"class":83},[52,87830,118],{"class":102},[52,87832,1069],{"class":121},[52,87834,125],{"class":102},[52,87836,87837,87839,87841,87843,87845],{"class":54,"line":164},[52,87838,1029],{"class":83},[52,87840,1078],{"class":87},[52,87842,115],{"class":83},[52,87844,118],{"class":102},[52,87846,1085],{"class":121},[52,87848,87849],{"class":54,"line":170},[52,87850,1090],{"class":102},[52,87852,87853],{"class":54,"line":175},[52,87854,167],{"class":102},[14,87856,1097],{},[309,87858,87859,87861,87865],{},[24,87860,1102],{},[24,87862,87863,1107],{},[49,87864,153],{},[24,87866,87867,759,87869,1114,87871],{},[49,87868,264],{},[49,87870,293],{},[49,87872,153],{},[14,87874,1119],{},[42,87876,87877],{"className":44,"code":1122,"language":46,"meta":47,"style":47},[49,87878,87879],{"__ignoreMap":47},[52,87880,87881,87883,87885,87887,87889,87891,87893,87895,87897,87899],{"class":54,"line":55},[52,87882,247],{"class":62},[52,87884,250],{"class":102},[52,87886,115],{"class":83},[52,87888,153],{"class":87},[52,87890,115],{"class":83},[52,87892,259],{"class":102},[52,87894,115],{"class":83},[52,87896,264],{"class":87},[52,87898,115],{"class":83},[52,87900,1147],{"class":102},[14,87902,1150],{},[309,87904,87905,87909],{},[24,87906,1155,87907],{},[49,87908,1158],{},[24,87910,1161,87911,1165],{},[49,87912,1164],{},[14,87914,1168,87915,186],{},[372,87916,1172],{"href":1171},[37,87918,1176],{"id":1175},[14,87920,1179],{},[42,87922,87923],{"className":44,"code":1182,"language":46,"meta":47,"style":47},[49,87924,87925,87931,87935,87947,87959,87967,87981,87997,88009,88013,88017,88043,88047,88063,88077,88101,88105,88127,88149,88153,88177,88197,88227,88257,88263,88277,88283,88297],{"__ignoreMap":47},[52,87926,87927,87929],{"class":54,"line":55},[52,87928,59],{"class":58},[52,87930,63],{"class":62},[52,87932,87933],{"class":54,"line":66},[52,87934,70],{"emptyLinePlaceholder":69},[52,87936,87937,87939,87941,87943,87945],{"class":54,"line":73},[52,87938,1199],{"class":62},[52,87940,80],{"class":79},[52,87942,84],{"class":83},[52,87944,1206],{"class":87},[52,87946,91],{"class":83},[52,87948,87949,87951,87953,87955,87957],{"class":54,"line":94},[52,87950,76],{"class":62},[52,87952,80],{"class":79},[52,87954,84],{"class":83},[52,87956,88],{"class":87},[52,87958,91],{"class":83},[52,87960,87961,87963,87965],{"class":54,"line":106},[52,87962,97],{"class":62},[52,87964,80],{"class":79},[52,87966,103],{"class":102},[52,87968,87969,87971,87973,87975,87977,87979],{"class":54,"line":128},[52,87970,109],{"class":83},[52,87972,112],{"class":87},[52,87974,115],{"class":83},[52,87976,118],{"class":102},[52,87978,122],{"class":121},[52,87980,125],{"class":102},[52,87982,87983,87985,87987,87989,87991,87993,87995],{"class":54,"line":148},[52,87984,109],{"class":83},[52,87986,133],{"class":87},[52,87988,115],{"class":83},[52,87990,118],{"class":102},[52,87992,140],{"class":79},[52,87994,143],{"class":121},[52,87996,125],{"class":102},[52,87998,87999,88001,88003,88005,88007],{"class":54,"line":164},[52,88000,109],{"class":83},[52,88002,153],{"class":87},[52,88004,115],{"class":83},[52,88006,118],{"class":102},[52,88008,161],{"class":160},[52,88010,88011],{"class":54,"line":170},[52,88012,167],{"class":102},[52,88014,88015],{"class":54,"line":175},[52,88016,70],{"emptyLinePlaceholder":69},[52,88018,88019,88021,88023,88025,88027,88029,88031,88033,88035,88037,88039,88041],{"class":54,"line":214},[52,88020,178],{"class":62},[52,88022,80],{"class":79},[52,88024,183],{"class":62},[52,88026,186],{"class":102},[52,88028,190],{"class":189},[52,88030,193],{"class":102},[52,88032,196],{"class":189},[52,88034,199],{"class":102},[52,88036,203],{"class":202},[52,88038,80],{"class":79},[52,88040,208],{"class":189},[52,88042,211],{"class":102},[52,88044,88045],{"class":54,"line":233},[52,88046,70],{"emptyLinePlaceholder":69},[52,88048,88049,88051,88053,88055,88057,88059,88061],{"class":54,"line":238},[52,88050,1313],{"class":58},[52,88052,222],{"class":62},[52,88054,186],{"class":102},[52,88056,1321],{"class":1320},[52,88058,1324],{"class":79},[52,88060,1327],{"class":121},[52,88062,1330],{"class":102},[52,88064,88065,88067,88069,88071,88073,88075],{"class":54,"line":272},[52,88066,1335],{"class":62},[52,88068,80],{"class":79},[52,88070,222],{"class":62},[52,88072,186],{"class":102},[52,88074,227],{"class":189},[52,88076,230],{"class":102},[52,88078,88079,88081,88083,88085,88087,88089,88091,88093,88095,88097,88099],{"class":54,"line":1348},[52,88080,1351],{"class":62},[52,88082,80],{"class":79},[52,88084,1356],{"class":62},[52,88086,186],{"class":102},[52,88088,190],{"class":189},[52,88090,193],{"class":102},[52,88092,115],{"class":83},[52,88094,153],{"class":87},[52,88096,115],{"class":83},[52,88098,199],{"class":102},[52,88100,1373],{"class":102},[52,88102,88103],{"class":54,"line":1376},[52,88104,70],{"emptyLinePlaceholder":69},[52,88106,88107,88109,88111,88113,88115,88117,88119,88121,88123,88125],{"class":54,"line":1381},[52,88108,1384],{"class":62},[52,88110,80],{"class":79},[52,88112,1389],{"class":62},[52,88114,186],{"class":102},[52,88116,190],{"class":189},[52,88118,193],{"class":102},[52,88120,115],{"class":83},[52,88122,264],{"class":87},[52,88124,115],{"class":83},[52,88126,211],{"class":102},[52,88128,88129,88131,88133,88135,88137,88139,88141,88143,88145,88147],{"class":54,"line":1406},[52,88130,1409],{"class":62},[52,88132,80],{"class":79},[52,88134,1389],{"class":62},[52,88136,186],{"class":102},[52,88138,190],{"class":189},[52,88140,193],{"class":102},[52,88142,115],{"class":83},[52,88144,293],{"class":87},[52,88146,115],{"class":83},[52,88148,211],{"class":102},[52,88150,88151],{"class":54,"line":1430},[52,88152,70],{"emptyLinePlaceholder":69},[52,88154,88155,88157,88159,88161,88163,88165,88167,88169,88171,88173,88175],{"class":54,"line":1435},[52,88156,1438],{"class":58},[52,88158,1441],{"class":62},[52,88160,1444],{"class":79},[52,88162,1447],{"class":79},[52,88164,1450],{"class":160},[52,88166,1453],{"class":79},[52,88168,1456],{"class":62},[52,88170,1444],{"class":79},[52,88172,1447],{"class":79},[52,88174,1450],{"class":160},[52,88176,1330],{"class":102},[52,88178,88179,88181,88183,88185,88187,88189,88191,88193,88195],{"class":54,"line":1467},[52,88180,1470],{"class":241},[52,88182,193],{"class":102},[52,88184,1476],{"class":1475},[52,88186,1479],{"class":87},[52,88188,1482],{"class":121},[52,88190,1485],{"class":189},[52,88192,1488],{"class":121},[52,88194,115],{"class":87},[52,88196,211],{"class":102},[52,88198,88199,88201,88203,88205,88207,88209,88211,88213,88215,88217,88219,88221,88223,88225],{"class":54,"line":1495},[52,88200,1470],{"class":241},[52,88202,193],{"class":102},[52,88204,1476],{"class":1475},[52,88206,1504],{"class":87},[52,88208,1482],{"class":121},[52,88210,1509],{"class":241},[52,88212,193],{"class":102},[52,88214,264],{"class":189},[52,88216,199],{"class":102},[52,88218,1518],{"class":121},[52,88220,1521],{"class":102},[52,88222,1488],{"class":121},[52,88224,1526],{"class":87},[52,88226,211],{"class":102},[52,88228,88229,88231,88233,88235,88237,88239,88241,88243,88245,88247,88249,88251,88253,88255],{"class":54,"line":1531},[52,88230,1470],{"class":241},[52,88232,193],{"class":102},[52,88234,1476],{"class":1475},[52,88236,1540],{"class":87},[52,88238,1482],{"class":121},[52,88240,1509],{"class":241},[52,88242,193],{"class":102},[52,88244,293],{"class":189},[52,88246,199],{"class":102},[52,88248,1518],{"class":121},[52,88250,1521],{"class":102},[52,88252,1488],{"class":121},[52,88254,1559],{"class":87},[52,88256,211],{"class":102},[52,88258,88259,88261],{"class":54,"line":1564},[52,88260,1567],{"class":58},[52,88262,1330],{"class":102},[52,88264,88265,88267,88269,88271,88273,88275],{"class":54,"line":1572},[52,88266,1470],{"class":241},[52,88268,193],{"class":102},[52,88270,115],{"class":83},[52,88272,1581],{"class":87},[52,88274,115],{"class":83},[52,88276,211],{"class":102},[52,88278,88279,88281],{"class":54,"line":1588},[52,88280,1591],{"class":58},[52,88282,1330],{"class":102},[52,88284,88285,88287,88289,88291,88293,88295],{"class":54,"line":1596},[52,88286,1599],{"class":241},[52,88288,193],{"class":102},[52,88290,115],{"class":83},[52,88292,1606],{"class":87},[52,88294,115],{"class":83},[52,88296,211],{"class":102},[52,88298,88299,88301,88303,88305,88307,88309,88311,88313,88315,88317],{"class":54,"line":1613},[52,88300,1599],{"class":241},[52,88302,193],{"class":102},[52,88304,115],{"class":83},[52,88306,1622],{"class":87},[52,88308,115],{"class":83},[52,88310,199],{"class":102},[52,88312,222],{"class":189},[52,88314,186],{"class":102},[52,88316,1321],{"class":1320},[52,88318,211],{"class":102},[14,88320,1637],{},[309,88322,88323,88325,88327,88329,88331],{},[24,88324,1642],{},[24,88326,1645],{},[24,88328,1648],{},[24,88330,1651],{},[24,88332,1654,88333],{},[49,88334,1657],{},[37,88336,1661],{"id":1660},[14,88338,1664],{},[309,88340,88341,88345,88347,88349,88351],{},[24,88342,88343,1671],{},[49,88344,303],{},[24,88346,1674],{},[24,88348,1677],{},[24,88350,1680],{},[24,88352,1683],{},[14,88354,1686],{},[309,88356,88357,88363,88367,88371,88373],{},[24,88358,88359,1694,88361,1671],{},[49,88360,1693],{},[49,88362,303],{},[24,88364,88365,1702],{},[49,88366,1701],{},[24,88368,88369,1708],{},[49,88370,1707],{},[24,88372,1711],{},[24,88374,1714],{},[14,88376,1717,88377,186,88379,1724,88381,1727,88383,186],{},[372,88378,1721],{"href":1720},[844,88380],{},[49,88382,303],{},[372,88384,1731],{"href":1730},[37,88386,1735],{"id":1734},[14,88388,1738],{},[639,88390,1742],{"id":1741},[42,88392,88393],{"className":44,"code":1745,"language":46,"meta":47,"style":47},[49,88394,88395],{"__ignoreMap":47},[52,88396,88397,88399,88401,88403,88405,88407],{"class":54,"line":55},[52,88398,242],{"class":241},[52,88400,193],{"class":102},[52,88402,1756],{"class":189},[52,88404,186],{"class":102},[52,88406,1321],{"class":1320},[52,88408,211],{"class":102},[14,88410,1765],{},[639,88412,1769],{"id":1768},[42,88414,88415],{"className":44,"code":1772,"language":46,"meta":47,"style":47},[49,88416,88417],{"__ignoreMap":47},[52,88418,88419,88421,88423,88425,88427,88429],{"class":54,"line":55},[52,88420,242],{"class":241},[52,88422,193],{"class":102},[52,88424,1756],{"class":189},[52,88426,186],{"class":102},[52,88428,955],{"class":1320},[52,88430,211],{"class":102},[14,88432,1791],{},[639,88434,1795],{"id":1794},[42,88436,88437],{"className":44,"code":1798,"language":46,"meta":47,"style":47},[49,88438,88439],{"__ignoreMap":47},[52,88440,88441,88443,88445,88447,88449,88451],{"class":54,"line":55},[52,88442,242],{"class":241},[52,88444,193],{"class":102},[52,88446,247],{"class":189},[52,88448,186],{"class":102},[52,88450,1813],{"class":189},[52,88452,1816],{"class":102},[14,88454,1819],{},[639,88456,1823,88457,1826],{"id":1822},[49,88458,153],{},[42,88460,88461],{"className":44,"code":1829,"language":46,"meta":47,"style":47},[49,88462,88463],{"__ignoreMap":47},[52,88464,88465,88467,88469,88471,88473,88475,88477,88479],{"class":54,"line":55},[52,88466,242],{"class":241},[52,88468,193],{"class":102},[52,88470,247],{"class":189},[52,88472,250],{"class":102},[52,88474,115],{"class":83},[52,88476,153],{"class":87},[52,88478,115],{"class":83},[52,88480,269],{"class":102},[14,88482,1852],{},[639,88484,1856],{"id":1855},[14,88486,1859],{},[14,88488,1862],{},[42,88490,88491],{"className":44,"code":1865,"language":46,"meta":47,"style":47},[49,88492,88493],{"__ignoreMap":47},[52,88494,88495,88497,88499,88501,88503,88505,88507,88509,88511,88513,88515,88517],{"class":54,"line":55},[52,88496,242],{"class":241},[52,88498,193],{"class":102},[52,88500,247],{"class":189},[52,88502,250],{"class":102},[52,88504,115],{"class":83},[52,88506,1882],{"class":87},[52,88508,115],{"class":83},[52,88510,259],{"class":102},[52,88512,115],{"class":83},[52,88514,1891],{"class":87},[52,88516,115],{"class":83},[52,88518,269],{"class":102},[14,88520,1898],{},[14,88522,1901],{},[42,88524,88525],{"className":390,"code":1904,"language":392,"meta":47,"style":47},[49,88526,88527,88535,88547],{"__ignoreMap":47},[52,88528,88529,88531,88533],{"class":54,"line":55},[52,88530,400],{"class":399},[52,88532,403],{"class":87},[52,88534,63],{"class":87},[52,88536,88537,88539,88541,88543,88545],{"class":54,"line":66},[52,88538,46],{"class":399},[52,88540,421],{"class":420},[52,88542,424],{"class":87},[52,88544,403],{"class":87},[52,88546,63],{"class":87},[52,88548,88549,88551,88553,88555,88557],{"class":54,"line":73},[52,88550,46],{"class":399},[52,88552,443],{"class":420},[52,88554,84],{"class":83},[52,88556,448],{"class":87},[52,88558,91],{"class":83},[37,88560,1942],{"id":1941},[639,88562,1946],{"id":1945},[14,88564,1949],{},[14,88566,1952],{},[639,88568,1956,88569,1959],{"id":1955},[49,88570,774],{},[14,88572,88573,1964],{},[49,88574,774],{},[14,88576,1967],{},[309,88578,88579,88581,88583],{},[24,88580,1972],{},[24,88582,1975],{},[24,88584,1978],{},[639,88586,1982,88587,1985],{"id":1981},[49,88588,848],{},[14,88590,1988],{},[639,88592,1992,88593,1995],{"id":1991},[49,88594,1701],{},[14,88596,1998],{},[14,88598,2001],{},[37,88600,2005],{"id":2004},[309,88602,88603,88607,88611,88615,88619],{},[24,88604,88605],{},[372,88606,2012],{"href":374},[24,88608,88609],{},[372,88610,2017],{"href":1171},[24,88612,88613],{},[372,88614,2022],{"href":855},[24,88616,88617],{},[372,88618,2028],{"href":2027},[24,88620,88621],{},[372,88622,1721],{"href":1720},[2034,88624,2036],{},{"title":47,"searchDepth":66,"depth":66,"links":88626},[88627,88628,88629,88630,88631,88640,88641,88642,88643,88650,88656],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":346,"depth":66,"text":347},{"id":378,"depth":66,"text":379},{"id":453,"depth":66,"text":454,"children":88632},[88633,88634,88635,88636,88637,88638,88639],{"id":641,"depth":73,"text":642},{"id":662,"depth":73,"text":663},{"id":686,"depth":73,"text":687},{"id":770,"depth":73,"text":2049},{"id":819,"depth":73,"text":820},{"id":864,"depth":73,"text":865},{"id":942,"depth":73,"text":943},{"id":960,"depth":66,"text":961},{"id":1175,"depth":66,"text":1176},{"id":1660,"depth":66,"text":1661},{"id":1734,"depth":66,"text":1735,"children":88644},[88645,88646,88647,88648,88649],{"id":1741,"depth":73,"text":1742},{"id":1768,"depth":73,"text":1769},{"id":1794,"depth":73,"text":1795},{"id":1822,"depth":73,"text":2062},{"id":1855,"depth":73,"text":1856},{"id":1941,"depth":66,"text":1942,"children":88651},[88652,88653,88654,88655],{"id":1945,"depth":73,"text":1946},{"id":1955,"depth":73,"text":2068},{"id":1981,"depth":73,"text":2070},{"id":1991,"depth":73,"text":2072},{"id":2004,"depth":66,"text":2005},{},{"title":5,"description":2074},{"id":88660,"title":88661,"body":88662,"description":90503,"extension":2075,"meta":90504,"navigation":69,"path":90505,"seo":90506,"stem":90507,"__hash__":90508},"content\u002Fexamples\u002Fpython-web-scraping-example-beautifulsoup.md","Python Web Scraping Example (BeautifulSoup)",{"type":7,"value":88663,"toc":90458},[88664,88667,88670,88673,88675,88863,88872,88874,88876,88891,88893,88896,88916,88921,88923,88926,88940,88942,88961,88964,88976,88979,89006,89008,89010,89192,89196,89212,89215,89221,89252,89255,89267,89273,89277,89291,89294,89297,89311,89314,89318,89349,89362,89368,89372,89403,89409,89412,89432,89435,89441,89445,89496,89503,89509,89515,89534,89537,89555,89557,89560,89566,89568,89590,89594,89597,90022,90025,90052,90056,90059,90063,90068,90072,90075,90078,90082,90085,90090,90094,90097,90101,90104,90108,90111,90128,90131,90135,90138,90149,90152,90166,90168,90171,90176,90184,90187,90201,90203,90221,90225,90231,90234,90256,90262,90265,90312,90320,90327,90334,90343,90346,90357,90362,90366,90369,90372,90374,90378,90381,90385,90391,90395,90398,90404,90414,90418,90421,90423,90449,90455],[10,88665,88661],{"id":88666},"python-web-scraping-example-beautifulsoup",[14,88668,88669],{},"This beginner-friendly example shows how to download a web page, parse its HTML with BeautifulSoup, and extract simple data like the page title and links.",[14,88671,88672],{},"The goal is to help you build a small working scraper. This page focuses on a practical example, not every detail of web scraping.",[37,88674,40],{"id":39},[42,88676,88678],{"className":44,"code":88677,"language":46,"meta":47,"style":47},"import requests\nfrom bs4 import BeautifulSoup\n\nurl = \"https:\u002F\u002Fexample.com\"\nresponse = requests.get(url, timeout=10)\nresponse.raise_for_status()\n\nsoup = BeautifulSoup(response.text, \"html.parser\")\n\nprint(\"Page title:\", soup.title.string)\n\nfor link in soup.find_all(\"a\"):\n    print(link.get(\"href\"))\n",[49,88679,88680,88686,88696,88700,88712,88738,88748,88752,88778,88782,88809,88813,88839],{"__ignoreMap":47},[52,88681,88682,88684],{"class":54,"line":55},[52,88683,59],{"class":58},[52,88685,63],{"class":62},[52,88687,88688,88690,88692,88694],{"class":54,"line":66},[52,88689,25329],{"class":58},[52,88691,73916],{"class":62},[52,88693,59],{"class":58},[52,88695,73921],{"class":62},[52,88697,88698],{"class":54,"line":73},[52,88699,70],{"emptyLinePlaceholder":69},[52,88701,88702,88704,88706,88708,88710],{"class":54,"line":94},[52,88703,76],{"class":62},[52,88705,80],{"class":79},[52,88707,84],{"class":83},[52,88709,73936],{"class":87},[52,88711,91],{"class":83},[52,88713,88714,88716,88718,88720,88722,88724,88726,88728,88730,88732,88734,88736],{"class":54,"line":106},[52,88715,178],{"class":62},[52,88717,80],{"class":79},[52,88719,183],{"class":62},[52,88721,186],{"class":102},[52,88723,190],{"class":189},[52,88725,193],{"class":102},[52,88727,196],{"class":189},[52,88729,199],{"class":102},[52,88731,3368],{"class":202},[52,88733,80],{"class":79},[52,88735,3373],{"class":121},[52,88737,211],{"class":102},[52,88739,88740,88742,88744,88746],{"class":54,"line":128},[52,88741,1756],{"class":62},[52,88743,186],{"class":102},[52,88745,3385],{"class":189},[52,88747,230],{"class":102},[52,88749,88750],{"class":54,"line":148},[52,88751,70],{"emptyLinePlaceholder":69},[52,88753,88754,88756,88758,88760,88762,88764,88766,88768,88770,88772,88774,88776],{"class":54,"line":164},[52,88755,73961],{"class":62},[52,88757,80],{"class":79},[52,88759,73966],{"class":189},[52,88761,193],{"class":102},[52,88763,1756],{"class":189},[52,88765,186],{"class":102},[52,88767,955],{"class":1320},[52,88769,199],{"class":102},[52,88771,84],{"class":83},[52,88773,73981],{"class":87},[52,88775,115],{"class":83},[52,88777,211],{"class":102},[52,88779,88780],{"class":54,"line":170},[52,88781,70],{"emptyLinePlaceholder":69},[52,88783,88784,88786,88788,88790,88793,88795,88797,88799,88801,88803,88805,88807],{"class":54,"line":175},[52,88785,242],{"class":241},[52,88787,193],{"class":102},[52,88789,115],{"class":83},[52,88791,88792],{"class":87},"Page title:",[52,88794,115],{"class":83},[52,88796,199],{"class":102},[52,88798,74290],{"class":189},[52,88800,186],{"class":102},[52,88802,18682],{"class":1320},[52,88804,186],{"class":102},[52,88806,41855],{"class":1320},[52,88808,211],{"class":102},[52,88810,88811],{"class":54,"line":214},[52,88812,70],{"emptyLinePlaceholder":69},[52,88814,88815,88817,88820,88822,88824,88826,88829,88831,88833,88835,88837],{"class":54,"line":233},[52,88816,12265],{"class":58},[52,88818,88819],{"class":62}," link ",[52,88821,11213],{"class":58},[52,88823,74290],{"class":62},[52,88825,186],{"class":102},[52,88827,88828],{"class":189},"find_all",[52,88830,193],{"class":102},[52,88832,115],{"class":83},[52,88834,372],{"class":87},[52,88836,115],{"class":83},[52,88838,3114],{"class":102},[52,88840,88841,88843,88845,88848,88850,88852,88854,88856,88859,88861],{"class":54,"line":238},[52,88842,1599],{"class":241},[52,88844,193],{"class":102},[52,88846,88847],{"class":189},"link",[52,88849,186],{"class":102},[52,88851,190],{"class":189},[52,88853,193],{"class":102},[52,88855,115],{"class":83},[52,88857,88858],{"class":87},"href",[52,88860,115],{"class":83},[52,88862,8886],{"class":102},[14,88864,88865,88867,88868,759,88870,23283],{},[5503,88866,16852],{}," This is a minimal working example. You need to install ",[49,88869,303],{},[49,88871,73897],{},[37,88873,323],{"id":322},[14,88875,30376],{},[309,88877,88878,88880,88882,88885,88888],{},[24,88879,74055],{},[24,88881,74058],{},[24,88883,88884],{},"Gets the page title",[24,88886,88887],{},"Finds all link tags",[24,88889,88890],{},"Prints each link URL",[37,88892,347],{"id":346},[14,88894,88895],{},"Before you run the example, make sure you have:",[309,88897,88898,88900,88905,88909,88913],{},[24,88899,355],{},[24,88901,88902,88903],{},"A basic understanding of ",[49,88904,59],{},[24,88906,358,88907,3910],{},[49,88908,303],{},[24,88910,358,88911,3910],{},[49,88912,73897],{},[24,88914,88915],{},"Internet access for the example URL",[14,88917,88918,88919,186],{},"If you need help installing packages, see ",[372,88920,73887],{"href":73886},[37,88922,74106],{"id":74105},[14,88924,88925],{},"You can install both packages with pip:",[42,88927,88928],{"className":390,"code":74114,"language":392,"meta":47,"style":47},[49,88929,88930],{"__ignoreMap":47},[52,88931,88932,88934,88936,88938],{"class":54,"line":55},[52,88933,400],{"class":399},[52,88935,403],{"class":87},[52,88937,183],{"class":87},[52,88939,74127],{"class":87},[14,88941,408],{},[42,88943,88945],{"className":390,"code":88944,"language":392,"meta":47,"style":47},"python -m pip install requests beautifulsoup4\n",[49,88946,88947],{"__ignoreMap":47},[52,88948,88949,88951,88953,88955,88957,88959],{"class":54,"line":55},[52,88950,46],{"class":399},[52,88952,421],{"class":420},[52,88954,424],{"class":87},[52,88956,403],{"class":87},[52,88958,183],{"class":87},[52,88960,74127],{"class":87},[14,88962,88963],{},"What each package does:",[309,88965,88966,88971],{},[24,88967,88968,88970],{},[49,88969,303],{}," downloads the web page",[24,88972,88973,88975],{},[49,88974,75036],{}," reads the HTML structure so you can search through tags and attributes",[14,88977,88978],{},"Useful commands for checking your setup:",[42,88980,88982],{"className":390,"code":88981,"language":392,"meta":47,"style":47},"python --version\npip show requests\npip show beautifulsoup4\n",[49,88983,88984,88990,88998],{"__ignoreMap":47},[52,88985,88986,88988],{"class":54,"line":55},[52,88987,46],{"class":399},[52,88989,2324],{"class":420},[52,88991,88992,88994,88996],{"class":54,"line":66},[52,88993,400],{"class":399},[52,88995,2339],{"class":87},[52,88997,63],{"class":87},[52,88999,89000,89002,89004],{"class":54,"line":73},[52,89001,400],{"class":399},[52,89003,2339],{"class":87},[52,89005,74127],{"class":87},[37,89007,7893],{"id":7892},[14,89009,457],{},[42,89011,89012],{"className":44,"code":88677,"language":46,"meta":47,"style":47},[49,89013,89014,89020,89030,89034,89046,89072,89082,89086,89112,89116,89142,89146,89170],{"__ignoreMap":47},[52,89015,89016,89018],{"class":54,"line":55},[52,89017,59],{"class":58},[52,89019,63],{"class":62},[52,89021,89022,89024,89026,89028],{"class":54,"line":66},[52,89023,25329],{"class":58},[52,89025,73916],{"class":62},[52,89027,59],{"class":58},[52,89029,73921],{"class":62},[52,89031,89032],{"class":54,"line":73},[52,89033,70],{"emptyLinePlaceholder":69},[52,89035,89036,89038,89040,89042,89044],{"class":54,"line":94},[52,89037,76],{"class":62},[52,89039,80],{"class":79},[52,89041,84],{"class":83},[52,89043,73936],{"class":87},[52,89045,91],{"class":83},[52,89047,89048,89050,89052,89054,89056,89058,89060,89062,89064,89066,89068,89070],{"class":54,"line":106},[52,89049,178],{"class":62},[52,89051,80],{"class":79},[52,89053,183],{"class":62},[52,89055,186],{"class":102},[52,89057,190],{"class":189},[52,89059,193],{"class":102},[52,89061,196],{"class":189},[52,89063,199],{"class":102},[52,89065,3368],{"class":202},[52,89067,80],{"class":79},[52,89069,3373],{"class":121},[52,89071,211],{"class":102},[52,89073,89074,89076,89078,89080],{"class":54,"line":128},[52,89075,1756],{"class":62},[52,89077,186],{"class":102},[52,89079,3385],{"class":189},[52,89081,230],{"class":102},[52,89083,89084],{"class":54,"line":148},[52,89085,70],{"emptyLinePlaceholder":69},[52,89087,89088,89090,89092,89094,89096,89098,89100,89102,89104,89106,89108,89110],{"class":54,"line":164},[52,89089,73961],{"class":62},[52,89091,80],{"class":79},[52,89093,73966],{"class":189},[52,89095,193],{"class":102},[52,89097,1756],{"class":189},[52,89099,186],{"class":102},[52,89101,955],{"class":1320},[52,89103,199],{"class":102},[52,89105,84],{"class":83},[52,89107,73981],{"class":87},[52,89109,115],{"class":83},[52,89111,211],{"class":102},[52,89113,89114],{"class":54,"line":170},[52,89115,70],{"emptyLinePlaceholder":69},[52,89117,89118,89120,89122,89124,89126,89128,89130,89132,89134,89136,89138,89140],{"class":54,"line":175},[52,89119,242],{"class":241},[52,89121,193],{"class":102},[52,89123,115],{"class":83},[52,89125,88792],{"class":87},[52,89127,115],{"class":83},[52,89129,199],{"class":102},[52,89131,74290],{"class":189},[52,89133,186],{"class":102},[52,89135,18682],{"class":1320},[52,89137,186],{"class":102},[52,89139,41855],{"class":1320},[52,89141,211],{"class":102},[52,89143,89144],{"class":54,"line":214},[52,89145,70],{"emptyLinePlaceholder":69},[52,89147,89148,89150,89152,89154,89156,89158,89160,89162,89164,89166,89168],{"class":54,"line":233},[52,89149,12265],{"class":58},[52,89151,88819],{"class":62},[52,89153,11213],{"class":58},[52,89155,74290],{"class":62},[52,89157,186],{"class":102},[52,89159,88828],{"class":189},[52,89161,193],{"class":102},[52,89163,115],{"class":83},[52,89165,372],{"class":87},[52,89167,115],{"class":83},[52,89169,3114],{"class":102},[52,89171,89172,89174,89176,89178,89180,89182,89184,89186,89188,89190],{"class":54,"line":238},[52,89173,1599],{"class":241},[52,89175,193],{"class":102},[52,89177,88847],{"class":189},[52,89179,186],{"class":102},[52,89181,190],{"class":189},[52,89183,193],{"class":102},[52,89185,115],{"class":83},[52,89187,88858],{"class":87},[52,89189,115],{"class":83},[52,89191,8886],{"class":102},[639,89193,89195],{"id":89194},"_1-set-the-target-url","1. Set the target URL",[42,89197,89198],{"className":44,"code":75279,"language":46,"meta":47,"style":47},[49,89199,89200],{"__ignoreMap":47},[52,89201,89202,89204,89206,89208,89210],{"class":54,"line":55},[52,89203,76],{"class":62},[52,89205,80],{"class":79},[52,89207,84],{"class":83},[52,89209,73936],{"class":87},[52,89211,91],{"class":83},[14,89213,89214],{},"This is the page you want to scrape.",[639,89216,89218,89219],{"id":89217},"_2-fetch-the-page-with-requestsget","2. Fetch the page with ",[49,89220,774],{},[42,89222,89224],{"className":44,"code":89223,"language":46,"meta":47,"style":47},"response = requests.get(url, timeout=10)\n",[49,89225,89226],{"__ignoreMap":47},[52,89227,89228,89230,89232,89234,89236,89238,89240,89242,89244,89246,89248,89250],{"class":54,"line":55},[52,89229,178],{"class":62},[52,89231,80],{"class":79},[52,89233,183],{"class":62},[52,89235,186],{"class":102},[52,89237,190],{"class":189},[52,89239,193],{"class":102},[52,89241,196],{"class":189},[52,89243,199],{"class":102},[52,89245,3368],{"class":202},[52,89247,80],{"class":79},[52,89249,3373],{"class":121},[52,89251,211],{"class":102},[14,89253,89254],{},"This sends an HTTP request and downloads the page.",[309,89256,89257,89262],{},[24,89258,89259,89261],{},[49,89260,196],{}," is the page address",[24,89263,89264,89266],{},[49,89265,4762],{}," means Python will stop waiting after 10 seconds",[14,89268,89269,89270,89272],{},"If you are new to HTTP requests, ",[372,89271,375],{"href":374}," explains the basic idea.",[639,89274,89276],{"id":89275},"_3-stop-on-http-errors","3. Stop on HTTP errors",[42,89278,89279],{"className":44,"code":24192,"language":46,"meta":47,"style":47},[49,89280,89281],{"__ignoreMap":47},[52,89282,89283,89285,89287,89289],{"class":54,"line":55},[52,89284,1756],{"class":62},[52,89286,186],{"class":102},[52,89288,3385],{"class":189},[52,89290,230],{"class":102},[14,89292,89293],{},"This is important.",[14,89295,89296],{},"It raises an error if the page could not be fetched correctly, such as:",[309,89298,89299,89303,89307],{},[24,89300,89301],{},[49,89302,24214],{},[24,89304,89305],{},[49,89306,24219],{},[24,89308,89309],{},[49,89310,24224],{},[14,89312,89313],{},"Without this line, your code may continue with a bad response and give confusing results later.",[639,89315,89317],{"id":89316},"_4-parse-the-html","4. Parse the HTML",[42,89319,89321],{"className":44,"code":89320,"language":46,"meta":47,"style":47},"soup = BeautifulSoup(response.text, \"html.parser\")\n",[49,89322,89323],{"__ignoreMap":47},[52,89324,89325,89327,89329,89331,89333,89335,89337,89339,89341,89343,89345,89347],{"class":54,"line":55},[52,89326,73961],{"class":62},[52,89328,80],{"class":79},[52,89330,73966],{"class":189},[52,89332,193],{"class":102},[52,89334,1756],{"class":189},[52,89336,186],{"class":102},[52,89338,955],{"class":1320},[52,89340,199],{"class":102},[52,89342,84],{"class":83},[52,89344,73981],{"class":87},[52,89346,115],{"class":83},[52,89348,211],{"class":102},[309,89350,89351,89356],{},[24,89352,89353,89355],{},[49,89354,4247],{}," is the HTML content as a string",[24,89357,89358,89361],{},[49,89359,89360],{},"\"html.parser\""," tells BeautifulSoup which parser to use",[14,89363,89364,89365,89367],{},"After this, ",[49,89366,73998],{}," becomes an object you can search.",[639,89369,89371],{"id":89370},"_5-get-the-page-title","5. Get the page title",[42,89373,89375],{"className":44,"code":89374,"language":46,"meta":47,"style":47},"print(\"Page title:\", soup.title.string)\n",[49,89376,89377],{"__ignoreMap":47},[52,89378,89379,89381,89383,89385,89387,89389,89391,89393,89395,89397,89399,89401],{"class":54,"line":55},[52,89380,242],{"class":241},[52,89382,193],{"class":102},[52,89384,115],{"class":83},[52,89386,88792],{"class":87},[52,89388,115],{"class":83},[52,89390,199],{"class":102},[52,89392,74290],{"class":189},[52,89394,186],{"class":102},[52,89396,18682],{"class":1320},[52,89398,186],{"class":102},[52,89400,41855],{"class":1320},[52,89402,211],{"class":102},[14,89404,89405,89406,89408],{},"This tries to find the ",[49,89407,74030],{}," tag and print its text.",[14,89410,89411],{},"For example, if the HTML contains:",[42,89413,89414],{"className":75047,"code":75048,"language":74266,"meta":47,"style":47},[49,89415,89416],{"__ignoreMap":47},[52,89417,89418,89420,89422,89424,89426,89428,89430],{"class":54,"line":55},[52,89419,15294],{"class":102},[52,89421,18682],{"class":75057},[52,89423,13046],{"class":102},[52,89425,75062],{"class":62},[52,89427,75065],{"class":102},[52,89429,18682],{"class":75057},[52,89431,15306],{"class":102},[14,89433,89434],{},"The output will be:",[42,89436,89439],{"className":89437,"code":89438,"language":955,"meta":47},[953],"Page title: Example Domain\n",[49,89440,89438],{"__ignoreMap":47},[639,89442,89444],{"id":89443},"_6-find-all-links","6. Find all links",[42,89446,89448],{"className":44,"code":89447,"language":46,"meta":47,"style":47},"for link in soup.find_all(\"a\"):\n    print(link.get(\"href\"))\n",[49,89449,89450,89474],{"__ignoreMap":47},[52,89451,89452,89454,89456,89458,89460,89462,89464,89466,89468,89470,89472],{"class":54,"line":55},[52,89453,12265],{"class":58},[52,89455,88819],{"class":62},[52,89457,11213],{"class":58},[52,89459,74290],{"class":62},[52,89461,186],{"class":102},[52,89463,88828],{"class":189},[52,89465,193],{"class":102},[52,89467,115],{"class":83},[52,89469,372],{"class":87},[52,89471,115],{"class":83},[52,89473,3114],{"class":102},[52,89475,89476,89478,89480,89482,89484,89486,89488,89490,89492,89494],{"class":54,"line":66},[52,89477,1599],{"class":241},[52,89479,193],{"class":102},[52,89481,88847],{"class":189},[52,89483,186],{"class":102},[52,89485,190],{"class":189},[52,89487,193],{"class":102},[52,89489,115],{"class":83},[52,89491,88858],{"class":87},[52,89493,115],{"class":83},[52,89495,8886],{"class":102},[14,89497,89498,89499,89502],{},"This finds every ",[49,89500,89501],{},"\u003Ca>"," tag on the page.",[14,89504,89505,89506,89508],{},"Then it prints the value of the ",[49,89507,88858],{}," attribute for each one.",[14,89510,10294,89511,89514],{},[49,89512,89513],{},"link.get(\"href\")"," is safer than:",[42,89516,89518],{"className":44,"code":89517,"language":46,"meta":47,"style":47},"link[\"href\"]\n",[49,89519,89520],{"__ignoreMap":47},[52,89521,89522,89524,89526,89528,89530,89532],{"class":54,"line":55},[52,89523,88847],{"class":62},[52,89525,250],{"class":102},[52,89527,115],{"class":83},[52,89529,88858],{"class":87},[52,89531,115],{"class":83},[52,89533,1147],{"class":102},[14,89535,89536],{},"Why?",[309,89538,89539,89547],{},[24,89540,89541,37647,89543,4812,89545,75143],{},[49,89542,89513],{},[49,89544,18849],{},[49,89546,88858],{},[24,89548,89549,89552,89553,75143],{},[49,89550,89551],{},"link[\"href\"]"," raises an error if ",[49,89554,88858],{},[37,89556,13563],{"id":13562},[14,89558,89559],{},"The exact output depends on the page, but it will usually look something like this:",[42,89561,89564],{"className":89562,"code":89563,"language":955,"meta":47},[953],"Page title: Example Domain\nhttps:\u002F\u002Fwww.iana.org\u002Fdomains\u002Fexample\n",[49,89565,89563],{"__ignoreMap":47},[14,89567,75075],{},[309,89569,89570,89573,89576,89582],{},[24,89571,89572],{},"The page title is printed first",[24,89574,89575],{},"Each link URL is printed on a new line",[24,89577,89578,89579],{},"Some links may be relative paths like ",[49,89580,89581],{},"\u002Fabout",[24,89583,89584,89585,89587,89588],{},"Some ",[49,89586,88858],{}," values may be ",[49,89589,18849],{},[37,89591,89593],{"id":89592},"beginner-friendly-improvements","Beginner-friendly improvements",[14,89595,89596],{},"The first example is intentionally small. Here is a slightly better version that skips missing links and converts relative URLs into full URLs.",[42,89598,89600],{"className":44,"code":89599,"language":46,"meta":47,"style":47},"import requests\nfrom bs4 import BeautifulSoup\nfrom urllib.parse import urljoin\n\nurl = \"https:\u002F\u002Fexample.com\"\nresponse = requests.get(url, timeout=10)\nresponse.raise_for_status()\n\nsoup = BeautifulSoup(response.text, \"html.parser\")\n\ntitle = soup.title.string if soup.title else \"No title found\"\nprint(\"Page title:\", title)\n\nlinks = []\n\nfor link in soup.find_all(\"a\"):\n    href = link.get(\"href\")\n    text = link.get_text(strip=True)\n\n    if href is None:\n        continue\n\n    full_url = urljoin(url, href)\n    links.append((text, full_url))\n\nfor text, full_url in links:\n    print(f\"Link text: {text or '[no text]'}\")\n    print(f\"URL: {full_url}\")\n    print(\"-\" * 20)\n",[49,89601,89602,89608,89618,89635,89639,89651,89677,89687,89691,89717,89721,89754,89773,89777,89786,89790,89814,89838,89862,89866,89879,89883,89887,89908,89929,89933,89951,89982,90004],{"__ignoreMap":47},[52,89603,89604,89606],{"class":54,"line":55},[52,89605,59],{"class":58},[52,89607,63],{"class":62},[52,89609,89610,89612,89614,89616],{"class":54,"line":66},[52,89611,25329],{"class":58},[52,89613,73916],{"class":62},[52,89615,59],{"class":58},[52,89617,73921],{"class":62},[52,89619,89620,89622,89625,89627,89630,89632],{"class":54,"line":73},[52,89621,25329],{"class":58},[52,89623,89624],{"class":62}," urllib",[52,89626,186],{"class":102},[52,89628,89629],{"class":62},"parse ",[52,89631,59],{"class":58},[52,89633,89634],{"class":62}," urljoin\n",[52,89636,89637],{"class":54,"line":94},[52,89638,70],{"emptyLinePlaceholder":69},[52,89640,89641,89643,89645,89647,89649],{"class":54,"line":106},[52,89642,76],{"class":62},[52,89644,80],{"class":79},[52,89646,84],{"class":83},[52,89648,73936],{"class":87},[52,89650,91],{"class":83},[52,89652,89653,89655,89657,89659,89661,89663,89665,89667,89669,89671,89673,89675],{"class":54,"line":128},[52,89654,178],{"class":62},[52,89656,80],{"class":79},[52,89658,183],{"class":62},[52,89660,186],{"class":102},[52,89662,190],{"class":189},[52,89664,193],{"class":102},[52,89666,196],{"class":189},[52,89668,199],{"class":102},[52,89670,3368],{"class":202},[52,89672,80],{"class":79},[52,89674,3373],{"class":121},[52,89676,211],{"class":102},[52,89678,89679,89681,89683,89685],{"class":54,"line":148},[52,89680,1756],{"class":62},[52,89682,186],{"class":102},[52,89684,3385],{"class":189},[52,89686,230],{"class":102},[52,89688,89689],{"class":54,"line":164},[52,89690,70],{"emptyLinePlaceholder":69},[52,89692,89693,89695,89697,89699,89701,89703,89705,89707,89709,89711,89713,89715],{"class":54,"line":170},[52,89694,73961],{"class":62},[52,89696,80],{"class":79},[52,89698,73966],{"class":189},[52,89700,193],{"class":102},[52,89702,1756],{"class":189},[52,89704,186],{"class":102},[52,89706,955],{"class":1320},[52,89708,199],{"class":102},[52,89710,84],{"class":83},[52,89712,73981],{"class":87},[52,89714,115],{"class":83},[52,89716,211],{"class":102},[52,89718,89719],{"class":54,"line":175},[52,89720,70],{"emptyLinePlaceholder":69},[52,89722,89723,89726,89728,89730,89732,89734,89736,89738,89740,89742,89744,89746,89748,89750,89752],{"class":54,"line":214},[52,89724,89725],{"class":62},"title ",[52,89727,80],{"class":79},[52,89729,74290],{"class":62},[52,89731,186],{"class":102},[52,89733,18682],{"class":1320},[52,89735,186],{"class":102},[52,89737,41855],{"class":1320},[52,89739,18726],{"class":58},[52,89741,74290],{"class":62},[52,89743,186],{"class":102},[52,89745,18682],{"class":1320},[52,89747,56191],{"class":58},[52,89749,84],{"class":83},[52,89751,74858],{"class":87},[52,89753,91],{"class":83},[52,89755,89756,89758,89760,89762,89764,89766,89768,89771],{"class":54,"line":233},[52,89757,242],{"class":241},[52,89759,193],{"class":102},[52,89761,115],{"class":83},[52,89763,88792],{"class":87},[52,89765,115],{"class":83},[52,89767,199],{"class":102},[52,89769,89770],{"class":189}," title",[52,89772,211],{"class":102},[52,89774,89775],{"class":54,"line":238},[52,89776,70],{"emptyLinePlaceholder":69},[52,89778,89779,89782,89784],{"class":54,"line":272},[52,89780,89781],{"class":62},"links ",[52,89783,80],{"class":79},[52,89785,18590],{"class":102},[52,89787,89788],{"class":54,"line":1348},[52,89789,70],{"emptyLinePlaceholder":69},[52,89791,89792,89794,89796,89798,89800,89802,89804,89806,89808,89810,89812],{"class":54,"line":1376},[52,89793,12265],{"class":58},[52,89795,88819],{"class":62},[52,89797,11213],{"class":58},[52,89799,74290],{"class":62},[52,89801,186],{"class":102},[52,89803,88828],{"class":189},[52,89805,193],{"class":102},[52,89807,115],{"class":83},[52,89809,372],{"class":87},[52,89811,115],{"class":83},[52,89813,3114],{"class":102},[52,89815,89816,89819,89821,89824,89826,89828,89830,89832,89834,89836],{"class":54,"line":1381},[52,89817,89818],{"class":62},"    href ",[52,89820,80],{"class":79},[52,89822,89823],{"class":62}," link",[52,89825,186],{"class":102},[52,89827,190],{"class":189},[52,89829,193],{"class":102},[52,89831,115],{"class":83},[52,89833,88858],{"class":87},[52,89835,115],{"class":83},[52,89837,211],{"class":102},[52,89839,89840,89843,89845,89847,89849,89852,89854,89856,89858,89860],{"class":54,"line":1406},[52,89841,89842],{"class":62},"    text ",[52,89844,80],{"class":79},[52,89846,89823],{"class":62},[52,89848,186],{"class":102},[52,89850,89851],{"class":189},"get_text",[52,89853,193],{"class":102},[52,89855,18630],{"class":202},[52,89857,80],{"class":79},[52,89859,6233],{"class":160},[52,89861,211],{"class":102},[52,89863,89864],{"class":54,"line":1430},[52,89865,70],{"emptyLinePlaceholder":69},[52,89867,89868,89870,89873,89875,89877],{"class":54,"line":1435},[52,89869,1438],{"class":58},[52,89871,89872],{"class":62}," href ",[52,89874,1444],{"class":79},[52,89876,1450],{"class":160},[52,89878,1330],{"class":102},[52,89880,89881],{"class":54,"line":1467},[52,89882,18703],{"class":58},[52,89884,89885],{"class":54,"line":1495},[52,89886,70],{"emptyLinePlaceholder":69},[52,89888,89889,89892,89894,89897,89899,89901,89903,89906],{"class":54,"line":1531},[52,89890,89891],{"class":62},"    full_url ",[52,89893,80],{"class":79},[52,89895,89896],{"class":189}," urljoin",[52,89898,193],{"class":102},[52,89900,196],{"class":189},[52,89902,199],{"class":102},[52,89904,89905],{"class":189}," href",[52,89907,211],{"class":102},[52,89909,89910,89913,89915,89917,89920,89922,89924,89927],{"class":54,"line":1564},[52,89911,89912],{"class":62},"    links",[52,89914,186],{"class":102},[52,89916,18748],{"class":189},[52,89918,89919],{"class":102},"((",[52,89921,955],{"class":189},[52,89923,199],{"class":102},[52,89925,89926],{"class":189}," full_url",[52,89928,8886],{"class":102},[52,89930,89931],{"class":54,"line":1572},[52,89932,70],{"emptyLinePlaceholder":69},[52,89934,89935,89937,89939,89941,89944,89946,89949],{"class":54,"line":1588},[52,89936,12265],{"class":58},[52,89938,56252],{"class":62},[52,89940,199],{"class":102},[52,89942,89943],{"class":62}," full_url ",[52,89945,11213],{"class":58},[52,89947,89948],{"class":62}," links",[52,89950,1330],{"class":102},[52,89952,89953,89955,89957,89959,89962,89964,89966,89969,89971,89974,89976,89978,89980],{"class":54,"line":1596},[52,89954,1599],{"class":241},[52,89956,193],{"class":102},[52,89958,1476],{"class":1475},[52,89960,89961],{"class":87},"\"Link text: ",[52,89963,1482],{"class":121},[52,89965,79346],{"class":189},[52,89967,89968],{"class":79},"or",[52,89970,4172],{"class":83},[52,89972,89973],{"class":87},"[no text]",[52,89975,4162],{"class":83},[52,89977,1488],{"class":121},[52,89979,115],{"class":87},[52,89981,211],{"class":102},[52,89983,89984,89986,89988,89990,89993,89995,89998,90000,90002],{"class":54,"line":1613},[52,89985,1599],{"class":241},[52,89987,193],{"class":102},[52,89989,1476],{"class":1475},[52,89991,89992],{"class":87},"\"URL: ",[52,89994,1482],{"class":121},[52,89996,89997],{"class":189},"full_url",[52,89999,1488],{"class":121},[52,90001,115],{"class":87},[52,90003,211],{"class":102},[52,90005,90006,90008,90010,90012,90014,90016,90018,90020],{"class":54,"line":11327},[52,90007,1599],{"class":241},[52,90009,193],{"class":102},[52,90011,115],{"class":83},[52,90013,5889],{"class":87},[52,90015,115],{"class":83},[52,90017,69299],{"class":79},[52,90019,32771],{"class":121},[52,90021,211],{"class":102},[14,90023,90024],{},"This improved version:",[309,90026,90027,90034,90040,90046,90049],{},[24,90028,90029,90030,27498,90032],{},"Skips links where ",[49,90031,88858],{},[49,90033,18849],{},[24,90035,90036,90037],{},"Converts relative links into full URLs with ",[49,90038,90039],{},"urljoin",[24,90041,90042,90043],{},"Extracts link text with ",[49,90044,90045],{},"get_text()",[24,90047,90048],{},"Stores results in a list",[24,90050,90051],{},"Prints cleaner output",[37,90053,90055],{"id":90054},"common-problems-when-scraping","Common problems when scraping",[14,90057,90058],{},"Web scraping often works well on simple pages, but there are common problems.",[639,90060,90062],{"id":90061},"the-site-blocks-requests-from-scripts","The site blocks requests from scripts",[14,90064,90065,90066,186],{},"Some websites do not allow automated scraping. They may return errors like ",[49,90067,24219],{},[639,90069,90071],{"id":90070},"the-page-needs-javascript-before-content-appears","The page needs JavaScript before content appears",[14,90073,90074],{},"BeautifulSoup only parses the HTML you give it. It does not run JavaScript.",[14,90076,90077],{},"If the site loads data after the page opens in the browser, your scraper may see very little content.",[639,90079,90081],{"id":90080},"the-html-structure-is-different-than-expected","The HTML structure is different than expected",[14,90083,90084],{},"You may expect a title, a link, or a certain tag, but the page may not contain it.",[14,90086,90087,90088,186],{},"This can lead to errors such as ",[372,90089,8360],{"href":8359},[639,90091,90093],{"id":90092},"the-request-fails-because-of-a-bad-url-or-timeout","The request fails because of a bad URL or timeout",[14,90095,90096],{},"A typo in the URL or a slow website can cause the request to fail.",[639,90098,90100],{"id":90099},"the-scraper-breaks-when-the-site-layout-changes","The scraper breaks when the site layout changes",[14,90102,90103],{},"If the website changes its HTML, your scraper may stop finding the tags you need.",[37,90105,90107],{"id":90106},"important-beginner-note-about-legality-and-ethics","Important beginner note about legality and ethics",[14,90109,90110],{},"Before scraping any website, be careful.",[309,90112,90113,90116,90122,90125],{},[24,90114,90115],{},"Do not scrape sites that forbid it",[24,90117,90118,90119],{},"Check the site's terms and ",[49,90120,90121],{},"robots.txt",[24,90123,90124],{},"Do not send too many requests too quickly",[24,90126,90127],{},"Only scrape data you are allowed to access",[14,90129,90130],{},"A small practice scraper is fine for learning, but real websites may have rules you need to follow.",[37,90132,90134],{"id":90133},"when-this-example-is-enough-and-when-it-is-not","When this example is enough and when it is not",[14,90136,90137],{},"This example is a good starting point when:",[309,90139,90140,90143,90146],{},[24,90141,90142],{},"You are scraping a simple static HTML page",[24,90144,90145],{},"You want to learn how tags, attributes, and parsing work",[24,90147,90148],{},"You want a small script that gets titles and links",[14,90150,90151],{},"This example is not enough when:",[309,90153,90154,90157,90160,90163],{},[24,90155,90156],{},"The site requires login",[24,90158,90159],{},"The site depends heavily on JavaScript",[24,90161,90162],{},"You need a large production scraper",[24,90164,90165],{},"You need advanced retry logic, rate limiting, or data storage",[37,90167,12322],{"id":12321},[14,90169,90170],{},"Here are some problems beginners often hit with this kind of script.",[639,90172,90174],{"id":90173},"modulenotfounderror",[49,90175,1693],{},[14,90177,90178,90179,857,90181,90183],{},"You may see an error because ",[49,90180,303],{},[49,90182,75142],{}," is not installed.",[14,90185,90186],{},"Try:",[42,90188,90189],{"className":390,"code":74114,"language":392,"meta":47,"style":47},[49,90190,90191],{"__ignoreMap":47},[52,90192,90193,90195,90197,90199],{"class":54,"line":55},[52,90194,400],{"class":399},[52,90196,403],{"class":87},[52,90198,183],{"class":87},[52,90200,74127],{"class":87},[14,90202,37391],{},[42,90204,90205],{"className":390,"code":88944,"language":392,"meta":47,"style":47},[49,90206,90207],{"__ignoreMap":47},[52,90208,90209,90211,90213,90215,90217,90219],{"class":54,"line":55},[52,90210,46],{"class":399},[52,90212,421],{"class":420},[52,90214,424],{"class":87},[52,90216,403],{"class":87},[52,90218,183],{"class":87},[52,90220,74127],{"class":87},[14,90222,31691,90223,186],{},[372,90224,85900],{"href":1730},[639,90226,90228,90230],{"id":90227},"attributeerror-when-a-tag-does-not-exist",[49,90229,37684],{}," when a tag does not exist",[14,90232,90233],{},"This can happen if you write code like:",[42,90235,90236],{"className":44,"code":75225,"language":46,"meta":47,"style":47},[49,90237,90238],{"__ignoreMap":47},[52,90239,90240,90242,90244,90246,90248,90250,90252,90254],{"class":54,"line":55},[52,90241,242],{"class":241},[52,90243,193],{"class":102},[52,90245,73998],{"class":189},[52,90247,186],{"class":102},[52,90249,18682],{"class":1320},[52,90251,186],{"class":102},[52,90253,41855],{"class":1320},[52,90255,211],{"class":102},[14,90257,90258,90259,90261],{},"but the page has no ",[49,90260,74030],{}," tag.",[14,90263,90264],{},"A safer version is:",[42,90266,90268],{"className":44,"code":90267,"language":46,"meta":47,"style":47},"title = soup.title.string if soup.title else \"No title found\"\nprint(title)\n",[49,90269,90270,90302],{"__ignoreMap":47},[52,90271,90272,90274,90276,90278,90280,90282,90284,90286,90288,90290,90292,90294,90296,90298,90300],{"class":54,"line":55},[52,90273,89725],{"class":62},[52,90275,80],{"class":79},[52,90277,74290],{"class":62},[52,90279,186],{"class":102},[52,90281,18682],{"class":1320},[52,90283,186],{"class":102},[52,90285,41855],{"class":1320},[52,90287,18726],{"class":58},[52,90289,74290],{"class":62},[52,90291,186],{"class":102},[52,90293,18682],{"class":1320},[52,90295,56191],{"class":58},[52,90297,84],{"class":83},[52,90299,74858],{"class":87},[52,90301,91],{"class":83},[52,90303,90304,90306,90308,90310],{"class":54,"line":66},[52,90305,242],{"class":241},[52,90307,193],{"class":102},[52,90309,18682],{"class":189},[52,90311,211],{"class":102},[639,90313,90315,90317,90318,76309],{"id":90314},"typeerror-or-bad-output-from-missing-href-values",[49,90316,1707],{}," or bad output from missing ",[49,90319,88858],{},[14,90321,89584,90322,90324,90325,186],{},[49,90323,89501],{}," tags do not have ",[49,90326,88858],{},[14,90328,61899,90329,90331,90332,186],{},[49,90330,89513],{}," is safer than ",[49,90333,89551],{},[639,90335,90337,90338,857,90341],{"id":90336},"http-errors-like-403-or-404","HTTP errors like ",[49,90339,90340],{},"403",[49,90342,4822],{},[14,90344,90345],{},"These happen when:",[309,90347,90348,90351,90354],{},[24,90349,90350],{},"The page does not exist",[24,90352,90353],{},"The site blocks the request",[24,90355,90356],{},"The URL is wrong",[14,90358,10294,90359,90361],{},[49,90360,25191],{}," helps catch this early.",[639,90363,90365],{"id":90364},"empty-results-because-content-is-loaded-with-javascript","Empty results because content is loaded with JavaScript",[14,90367,90368],{},"If your browser shows content but your script does not, the page may be using JavaScript to load data after the initial HTML response.",[14,90370,90371],{},"In that case, BeautifulSoup alone may not be enough.",[37,90373,1942],{"id":1941},[639,90375,90377],{"id":90376},"what-is-beautifulsoup-used-for","What is BeautifulSoup used for?",[14,90379,90380],{},"It parses HTML or XML so you can find tags, attributes, and text more easily in Python.",[639,90382,90384],{"id":90383},"do-i-need-requests-to-use-beautifulsoup","Do I need requests to use BeautifulSoup?",[14,90386,90387,90388,90390],{},"Not always, but beginners often use ",[49,90389,303],{}," to download the page and BeautifulSoup to parse it.",[639,90392,90394],{"id":90393},"why-does-my-scraper-return-nothing","Why does my scraper return nothing?",[14,90396,90397],{},"The page may use JavaScript, the selectors may be wrong, or the request may have failed.",[639,90399,90401,90402,1995],{"id":90400},"why-do-some-links-print-none","Why do some links print ",[49,90403,18849],{},[14,90405,90406,90407,90409,90410,37647,90412,186],{},"Some anchor tags do not have an ",[49,90408,88858],{}," attribute, so ",[49,90411,89513],{},[49,90413,18849],{},[639,90415,90417],{"id":90416},"can-beautifulsoup-scrape-javascript-rendered-websites","Can BeautifulSoup scrape JavaScript-rendered websites?",[14,90419,90420],{},"It can parse the HTML you give it, but it does not run JavaScript by itself.",[37,90422,2005],{"id":2004},[309,90424,90425,90429,90433,90438,90443],{},[24,90426,90427],{},[372,90428,75378],{"href":73886},[24,90430,90431],{},[372,90432,2012],{"href":374},[24,90434,90435],{},[372,90436,90437],{"href":1730},"ModuleNotFoundError: No module named X fix",[24,90439,90440],{},[372,90441,90442],{"href":8359},"AttributeError: object has no attribute fix",[24,90444,90445],{},[372,90446,90448],{"href":90447},"\u002Fexamples\u002Fpython-simple-web-scraper-for-titles-example\u002F","Python simple web scraper for titles example",[14,90450,90451,90452,186],{},"Try this scraper on a simple practice page first. After that, move to a smaller focused example, such as scraping only page titles or saving the results to a file with the ",[372,90453,90454],{"href":14984},"Python open() function",[2034,90456,90457],{},"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 .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 .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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--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 .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 .sQzsp, html code.shiki .sQzsp{--shiki-light:#E53935;--shiki-default:#22863A;--shiki-dark:#85E89D}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}",{"title":47,"searchDepth":66,"depth":66,"links":90459},[90460,90461,90462,90463,90464,90473,90474,90475,90482,90483,90484,90494,90502],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":346,"depth":66,"text":347},{"id":74105,"depth":66,"text":74106},{"id":7892,"depth":66,"text":7893,"children":90465},[90466,90467,90469,90470,90471,90472],{"id":89194,"depth":73,"text":89195},{"id":89217,"depth":73,"text":90468},"2. Fetch the page with requests.get()",{"id":89275,"depth":73,"text":89276},{"id":89316,"depth":73,"text":89317},{"id":89370,"depth":73,"text":89371},{"id":89443,"depth":73,"text":89444},{"id":13562,"depth":66,"text":13563},{"id":89592,"depth":66,"text":89593},{"id":90054,"depth":66,"text":90055,"children":90476},[90477,90478,90479,90480,90481],{"id":90061,"depth":73,"text":90062},{"id":90070,"depth":73,"text":90071},{"id":90080,"depth":73,"text":90081},{"id":90092,"depth":73,"text":90093},{"id":90099,"depth":73,"text":90100},{"id":90106,"depth":66,"text":90107},{"id":90133,"depth":66,"text":90134},{"id":12321,"depth":66,"text":12322,"children":90485},[90486,90487,90489,90491,90493],{"id":90173,"depth":73,"text":1693},{"id":90227,"depth":73,"text":90488},"AttributeError when a tag does not exist",{"id":90314,"depth":73,"text":90490},"TypeError or bad output from missing href values",{"id":90336,"depth":73,"text":90492},"HTTP errors like 403 or 404",{"id":90364,"depth":73,"text":90365},{"id":1941,"depth":66,"text":1942,"children":90495},[90496,90497,90498,90499,90501],{"id":90376,"depth":73,"text":90377},{"id":90383,"depth":73,"text":90384},{"id":90393,"depth":73,"text":90394},{"id":90400,"depth":73,"text":90500},"Why do some links print None?",{"id":90416,"depth":73,"text":90417},{"id":2004,"depth":66,"text":2005},"Master python web scraping example beautifulsoup in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-web-scraping-example-beautifulsoup",{"title":88661,"description":90503},"examples\u002Fpython-web-scraping-example-beautifulsoup","ITX90HC-qBkMeHM5Ohdc0nms2H2nKV0kGANK6yzRlbA",{"id":90510,"title":90511,"body":90512,"description":92458,"extension":2075,"meta":92459,"navigation":69,"path":92460,"seo":92461,"stem":92462,"__hash__":92463},"content\u002Fexamples\u002Fpython-word-count-script-example.md","Python Word Count Script Example",{"type":7,"value":90513,"toc":92420},[90514,90517,90520,90522,90537,90543,90545,90614,90618,90632,90635,90637,90639,90660,90664,90667,90752,90756,90819,90822,90840,90842,90844,90910,90914,90931,90937,90941,90960,90965,90968,90971,91020,91024,91043,91048,91053,91064,91068,91071,91076,91150,91152,91180,91183,91198,91202,91205,91207,91221,91225,91341,91345,91371,91375,91389,91395,91399,91402,91405,91419,91421,91470,91474,91505,91514,91518,91521,91523,91534,91741,91745,91833,91836,91839,91841,91844,91910,91914,91926,91929,91931,91933,91940,91943,92004,92010,92012,92030,92033,92051,92054,92058,92061,92126,92129,92212,92216,92219,92230,92233,92236,92238,92241,92243,92304,92307,92334,92336,92342,92345,92352,92360,92364,92371,92375,92380,92382,92417],[10,90515,90511],{"id":90516},"python-word-count-script-example",[14,90518,90519],{},"This example shows a simple way to count words in Python.",[14,90521,79314],{},[309,90523,90524,90527,90532],{},[24,90525,90526],{},"takes a piece of text",[24,90528,90529,90530],{},"splits it into words with ",[49,90531,79596],{},[24,90533,90534,90535],{},"counts those words with ",[49,90536,44313],{},[14,90538,90539,90540,90542],{},"This is a beginner-friendly example focused on one practical task. It does ",[5503,90541,20858],{}," try to do advanced text analysis.",[37,90544,40],{"id":39},[42,90546,90548],{"className":44,"code":90547,"language":46,"meta":47,"style":47},"text = \"Python makes word counting simple\"\nwords = text.split()\ncount = len(words)\n\nprint(\"Word count:\", count)\n",[49,90549,90550,90563,90577,90591,90595],{"__ignoreMap":47},[52,90551,90552,90554,90556,90558,90561],{"class":54,"line":55},[52,90553,79346],{"class":62},[52,90555,80],{"class":79},[52,90557,84],{"class":83},[52,90559,90560],{"class":87},"Python makes word counting simple",[52,90562,91],{"class":83},[52,90564,90565,90567,90569,90571,90573,90575],{"class":54,"line":66},[52,90566,32960],{"class":62},[52,90568,80],{"class":79},[52,90570,56252],{"class":62},[52,90572,186],{"class":102},[52,90574,58133],{"class":189},[52,90576,230],{"class":102},[52,90578,90579,90581,90583,90585,90587,90589],{"class":54,"line":73},[52,90580,55300],{"class":62},[52,90582,80],{"class":79},[52,90584,9150],{"class":241},[52,90586,193],{"class":102},[52,90588,67032],{"class":189},[52,90590,211],{"class":102},[52,90592,90593],{"class":54,"line":94},[52,90594,70],{"emptyLinePlaceholder":69},[52,90596,90597,90599,90601,90603,90606,90608,90610,90612],{"class":54,"line":106},[52,90598,242],{"class":241},[52,90600,193],{"class":102},[52,90602,115],{"class":83},[52,90604,90605],{"class":87},"Word count:",[52,90607,115],{"class":83},[52,90609,199],{"class":102},[52,90611,71795],{"class":189},[52,90613,211],{"class":102},[14,90615,90616],{},[5503,90617,5267],{},[42,90619,90621],{"className":44,"code":90620,"language":46,"meta":47,"style":47},"Word count: 5\n",[49,90622,90623],{"__ignoreMap":47},[52,90624,90625,90628,90630],{"class":54,"line":55},[52,90626,90627],{"class":62},"Word count",[52,90629,118],{"class":102},[52,90631,5264],{"class":121},[14,90633,90634],{},"This is the simplest version. It splits text by spaces and counts the resulting words.",[37,90636,323],{"id":322},[14,90638,30376],{},[309,90640,90641,90644,90650,90657],{},[24,90642,90643],{},"shows how to count words in a short string",[24,90645,5286,90646,79597],{},[372,90647,90648],{"href":79593},[49,90649,79596],{},[24,90651,5286,90652,90656],{},[372,90653,90654],{"href":79809},[49,90655,44313],{}," to count how many words were found",[24,90658,90659],{},"targets beginners who want a small practical script",[37,90661,90663],{"id":90662},"basic-word-count-example","Basic word count example",[14,90665,90666],{},"Start with a plain text string, split it, and count the result.",[42,90668,90670],{"className":44,"code":90669,"language":46,"meta":47,"style":47},"text = \"Python makes word counting simple\"\nwords = text.split()\ncount = len(words)\n\nprint(\"Words:\", words)\nprint(\"Word count:\", count)\n",[49,90671,90672,90684,90698,90712,90716,90734],{"__ignoreMap":47},[52,90673,90674,90676,90678,90680,90682],{"class":54,"line":55},[52,90675,79346],{"class":62},[52,90677,80],{"class":79},[52,90679,84],{"class":83},[52,90681,90560],{"class":87},[52,90683,91],{"class":83},[52,90685,90686,90688,90690,90692,90694,90696],{"class":54,"line":66},[52,90687,32960],{"class":62},[52,90689,80],{"class":79},[52,90691,56252],{"class":62},[52,90693,186],{"class":102},[52,90695,58133],{"class":189},[52,90697,230],{"class":102},[52,90699,90700,90702,90704,90706,90708,90710],{"class":54,"line":73},[52,90701,55300],{"class":62},[52,90703,80],{"class":79},[52,90705,9150],{"class":241},[52,90707,193],{"class":102},[52,90709,67032],{"class":189},[52,90711,211],{"class":102},[52,90713,90714],{"class":54,"line":94},[52,90715,70],{"emptyLinePlaceholder":69},[52,90717,90718,90720,90722,90724,90726,90728,90730,90732],{"class":54,"line":106},[52,90719,242],{"class":241},[52,90721,193],{"class":102},[52,90723,115],{"class":83},[52,90725,79428],{"class":87},[52,90727,115],{"class":83},[52,90729,199],{"class":102},[52,90731,79491],{"class":189},[52,90733,211],{"class":102},[52,90735,90736,90738,90740,90742,90744,90746,90748,90750],{"class":54,"line":128},[52,90737,242],{"class":241},[52,90739,193],{"class":102},[52,90741,115],{"class":83},[52,90743,90605],{"class":87},[52,90745,115],{"class":83},[52,90747,199],{"class":102},[52,90749,71795],{"class":189},[52,90751,211],{"class":102},[14,90753,90754],{},[5503,90755,5267],{},[42,90757,90759],{"className":44,"code":90758,"language":46,"meta":47,"style":47},"Words: ['Python', 'makes', 'word', 'counting', 'simple']\nWord count: 5\n",[49,90760,90761,90811],{"__ignoreMap":47},[52,90762,90763,90765,90767,90769,90771,90773,90775,90777,90779,90782,90784,90786,90788,90790,90792,90794,90796,90799,90801,90803,90805,90807,90809],{"class":54,"line":55},[52,90764,79832],{"class":62},[52,90766,118],{"class":102},[52,90768,18162],{"class":102},[52,90770,4162],{"class":83},[52,90772,40940],{"class":87},[52,90774,4162],{"class":83},[52,90776,199],{"class":102},[52,90778,4172],{"class":83},[52,90780,90781],{"class":87},"makes",[52,90783,4162],{"class":83},[52,90785,199],{"class":102},[52,90787,4172],{"class":83},[52,90789,79503],{"class":87},[52,90791,4162],{"class":83},[52,90793,199],{"class":102},[52,90795,4172],{"class":83},[52,90797,90798],{"class":87},"counting",[52,90800,4162],{"class":83},[52,90802,199],{"class":102},[52,90804,4172],{"class":83},[52,90806,80099],{"class":87},[52,90808,4162],{"class":83},[52,90810,1147],{"class":102},[52,90812,90813,90815,90817],{"class":54,"line":66},[52,90814,90627],{"class":62},[52,90816,118],{"class":102},[52,90818,5264],{"class":121},[14,90820,90821],{},"This works because:",[309,90823,90824,90829,90835],{},[24,90825,90826,90828],{},[49,90827,79782],{}," creates a list of words",[24,90830,90831,90834],{},[49,90832,90833],{},"len(words)"," counts how many items are in that list",[24,90836,90837,90839],{},[49,90838,9687],{}," shows the result clearly",[37,90841,454],{"id":453},[14,90843,16885],{},[42,90845,90846],{"className":44,"code":90547,"language":46,"meta":47,"style":47},[49,90847,90848,90860,90874,90888,90892],{"__ignoreMap":47},[52,90849,90850,90852,90854,90856,90858],{"class":54,"line":55},[52,90851,79346],{"class":62},[52,90853,80],{"class":79},[52,90855,84],{"class":83},[52,90857,90560],{"class":87},[52,90859,91],{"class":83},[52,90861,90862,90864,90866,90868,90870,90872],{"class":54,"line":66},[52,90863,32960],{"class":62},[52,90865,80],{"class":79},[52,90867,56252],{"class":62},[52,90869,186],{"class":102},[52,90871,58133],{"class":189},[52,90873,230],{"class":102},[52,90875,90876,90878,90880,90882,90884,90886],{"class":54,"line":73},[52,90877,55300],{"class":62},[52,90879,80],{"class":79},[52,90881,9150],{"class":241},[52,90883,193],{"class":102},[52,90885,67032],{"class":189},[52,90887,211],{"class":102},[52,90889,90890],{"class":54,"line":94},[52,90891,70],{"emptyLinePlaceholder":69},[52,90893,90894,90896,90898,90900,90902,90904,90906,90908],{"class":54,"line":106},[52,90895,242],{"class":241},[52,90897,193],{"class":102},[52,90899,115],{"class":83},[52,90901,90605],{"class":87},[52,90903,115],{"class":83},[52,90905,199],{"class":102},[52,90907,71795],{"class":189},[52,90909,211],{"class":102},[639,90911,90913],{"id":90912},"step-1-store-the-text","Step 1: Store the text",[42,90915,90917],{"className":44,"code":90916,"language":46,"meta":47,"style":47},"text = \"Python makes word counting simple\"\n",[49,90918,90919],{"__ignoreMap":47},[52,90920,90921,90923,90925,90927,90929],{"class":54,"line":55},[52,90922,79346],{"class":62},[52,90924,80],{"class":79},[52,90926,84],{"class":83},[52,90928,90560],{"class":87},[52,90930,91],{"class":83},[14,90932,90933,90934,90936],{},"The variable ",[49,90935,955],{}," stores the sentence as a string.",[639,90938,90940],{"id":90939},"step-2-split-the-text-into-words","Step 2: Split the text into words",[42,90942,90944],{"className":44,"code":90943,"language":46,"meta":47,"style":47},"words = text.split()\n",[49,90945,90946],{"__ignoreMap":47},[52,90947,90948,90950,90952,90954,90956,90958],{"class":54,"line":55},[52,90949,32960],{"class":62},[52,90951,80],{"class":79},[52,90953,56252],{"class":62},[52,90955,186],{"class":102},[52,90957,58133],{"class":189},[52,90959,230],{"class":102},[14,90961,90962,90964],{},[49,90963,79596],{}," separates the string on whitespace.",[14,90966,90967],{},"That means spaces, tabs, and line breaks can all act as separators.",[14,90969,90970],{},"The result here is:",[42,90972,90974],{"className":44,"code":90973,"language":46,"meta":47,"style":47},"['Python', 'makes', 'word', 'counting', 'simple']\n",[49,90975,90976],{"__ignoreMap":47},[52,90977,90978,90980,90982,90984,90986,90988,90990,90992,90994,90996,90998,91000,91002,91004,91006,91008,91010,91012,91014,91016,91018],{"class":54,"line":55},[52,90979,250],{"class":102},[52,90981,4162],{"class":83},[52,90983,40940],{"class":87},[52,90985,4162],{"class":83},[52,90987,199],{"class":102},[52,90989,4172],{"class":83},[52,90991,90781],{"class":87},[52,90993,4162],{"class":83},[52,90995,199],{"class":102},[52,90997,4172],{"class":83},[52,90999,79503],{"class":87},[52,91001,4162],{"class":83},[52,91003,199],{"class":102},[52,91005,4172],{"class":83},[52,91007,90798],{"class":87},[52,91009,4162],{"class":83},[52,91011,199],{"class":102},[52,91013,4172],{"class":83},[52,91015,80099],{"class":87},[52,91017,4162],{"class":83},[52,91019,1147],{"class":102},[639,91021,91023],{"id":91022},"step-3-count-the-words","Step 3: Count the words",[42,91025,91027],{"className":44,"code":91026,"language":46,"meta":47,"style":47},"count = len(words)\n",[49,91028,91029],{"__ignoreMap":47},[52,91030,91031,91033,91035,91037,91039,91041],{"class":54,"line":55},[52,91032,55300],{"class":62},[52,91034,80],{"class":79},[52,91036,9150],{"class":241},[52,91038,193],{"class":102},[52,91040,67032],{"class":189},[52,91042,211],{"class":102},[14,91044,91045,91047],{},[49,91046,44313],{}," returns the number of items in the list.",[14,91049,91050,91051,186],{},"Since the list has 5 items, the word count is ",[49,91052,9479],{},[14,91054,91055,91056,759,91060,186],{},"If you want to learn these parts in more detail, see the reference pages for ",[372,91057,91058],{"href":79593},[49,91059,79596],{},[372,91061,91062],{"href":79809},[49,91063,44313],{},[37,91065,91067],{"id":91066},"reading-text-from-user-input","Reading text from user input",[14,91069,91070],{},"You can make the script interactive by asking the user to enter a sentence.",[14,91072,91073,91074,186],{},"If you are new to this, see ",[372,91075,6398],{"href":6397},[42,91077,91079],{"className":44,"code":91078,"language":46,"meta":47,"style":47},"text = input(\"Enter a sentence: \")\nwords = text.split()\ncount = len(words)\n\nprint(\"Word count:\", count)\n",[49,91080,91081,91100,91114,91128,91132],{"__ignoreMap":47},[52,91082,91083,91085,91087,91089,91091,91093,91096,91098],{"class":54,"line":55},[52,91084,79346],{"class":62},[52,91086,80],{"class":79},[52,91088,11037],{"class":241},[52,91090,193],{"class":102},[52,91092,115],{"class":83},[52,91094,91095],{"class":87},"Enter a sentence: ",[52,91097,115],{"class":83},[52,91099,211],{"class":102},[52,91101,91102,91104,91106,91108,91110,91112],{"class":54,"line":66},[52,91103,32960],{"class":62},[52,91105,80],{"class":79},[52,91107,56252],{"class":62},[52,91109,186],{"class":102},[52,91111,58133],{"class":189},[52,91113,230],{"class":102},[52,91115,91116,91118,91120,91122,91124,91126],{"class":54,"line":73},[52,91117,55300],{"class":62},[52,91119,80],{"class":79},[52,91121,9150],{"class":241},[52,91123,193],{"class":102},[52,91125,67032],{"class":189},[52,91127,211],{"class":102},[52,91129,91130],{"class":54,"line":94},[52,91131,70],{"emptyLinePlaceholder":69},[52,91133,91134,91136,91138,91140,91142,91144,91146,91148],{"class":54,"line":106},[52,91135,242],{"class":241},[52,91137,193],{"class":102},[52,91139,115],{"class":83},[52,91141,90605],{"class":87},[52,91143,115],{"class":83},[52,91145,199],{"class":102},[52,91147,71795],{"class":189},[52,91149,211],{"class":102},[639,91151,54482],{"id":54481},[42,91153,91155],{"className":44,"code":91154,"language":46,"meta":47,"style":47},"Enter a sentence: Python is fun to learn\nWord count: 5\n",[49,91156,91157,91172],{"__ignoreMap":47},[52,91158,91159,91162,91164,91167,91169],{"class":54,"line":55},[52,91160,91161],{"class":62},"Enter a sentence",[52,91163,118],{"class":102},[52,91165,91166],{"class":62}," Python ",[52,91168,1444],{"class":79},[52,91170,91171],{"class":62}," fun to learn\n",[52,91173,91174,91176,91178],{"class":54,"line":66},[52,91175,90627],{"class":62},[52,91177,118],{"class":102},[52,91179,5264],{"class":121},[14,91181,91182],{},"This follows the same pattern:",[309,91184,91185,91190,91193],{},[24,91186,91187,91188],{},"get text with ",[49,91189,10597],{},[24,91191,91192],{},"split it into words",[24,91194,91195,91196],{},"count the words with ",[49,91197,44313],{},[37,91199,91201],{"id":91200},"counting-words-in-a-text-file","Counting words in a text file",[14,91203,91204],{},"You can also count words in a file.",[14,91206,10890],{},[309,91208,91209,91212,91215],{},[24,91210,91211],{},"open the file",[24,91213,91214],{},"read all text into one string",[24,91216,23564,91217,759,91219],{},[49,91218,79596],{},[49,91220,44313],{},[14,91222,31691,91223,186],{},[372,91224,29867],{"href":29866},[42,91226,91228],{"className":44,"code":91227,"language":46,"meta":47,"style":47},"with open(\"sample.txt\", \"r\", encoding=\"utf-8\") as file:\n    text = file.read()\n\nwords = text.split()\ncount = len(words)\n\nprint(\"Word count:\", count)\n",[49,91229,91230,91273,91287,91291,91305,91319,91323],{"__ignoreMap":47},[52,91231,91232,91234,91236,91238,91240,91243,91245,91247,91249,91251,91253,91255,91257,91259,91261,91263,91265,91267,91269,91271],{"class":54,"line":55},[52,91233,14632],{"class":58},[52,91235,14635],{"class":241},[52,91237,193],{"class":102},[52,91239,115],{"class":83},[52,91241,91242],{"class":87},"sample.txt",[52,91244,115],{"class":83},[52,91246,199],{"class":102},[52,91248,84],{"class":83},[52,91250,41783],{"class":87},[52,91252,115],{"class":83},[52,91254,199],{"class":102},[52,91256,14659],{"class":202},[52,91258,80],{"class":79},[52,91260,115],{"class":83},[52,91262,14666],{"class":87},[52,91264,115],{"class":83},[52,91266,1521],{"class":102},[52,91268,3567],{"class":58},[52,91270,14676],{"class":14675},[52,91272,1330],{"class":102},[52,91274,91275,91277,91279,91281,91283,91285],{"class":54,"line":66},[52,91276,89842],{"class":62},[52,91278,80],{"class":79},[52,91280,14676],{"class":14675},[52,91282,186],{"class":102},[52,91284,61409],{"class":189},[52,91286,230],{"class":102},[52,91288,91289],{"class":54,"line":73},[52,91290,70],{"emptyLinePlaceholder":69},[52,91292,91293,91295,91297,91299,91301,91303],{"class":54,"line":94},[52,91294,32960],{"class":62},[52,91296,80],{"class":79},[52,91298,56252],{"class":62},[52,91300,186],{"class":102},[52,91302,58133],{"class":189},[52,91304,230],{"class":102},[52,91306,91307,91309,91311,91313,91315,91317],{"class":54,"line":106},[52,91308,55300],{"class":62},[52,91310,80],{"class":79},[52,91312,9150],{"class":241},[52,91314,193],{"class":102},[52,91316,67032],{"class":189},[52,91318,211],{"class":102},[52,91320,91321],{"class":54,"line":128},[52,91322,70],{"emptyLinePlaceholder":69},[52,91324,91325,91327,91329,91331,91333,91335,91337,91339],{"class":54,"line":148},[52,91326,242],{"class":241},[52,91328,193],{"class":102},[52,91330,115],{"class":83},[52,91332,90605],{"class":87},[52,91334,115],{"class":83},[52,91336,199],{"class":102},[52,91338,71795],{"class":189},[52,91340,211],{"class":102},[639,91342,91344],{"id":91343},"example-file-content","Example file content",[42,91346,91348],{"className":44,"code":91347,"language":46,"meta":47,"style":47},"Python makes word counting simple.\nThis is a short file.\n",[49,91349,91350,91357],{"__ignoreMap":47},[52,91351,91352,91354],{"class":54,"line":55},[52,91353,90560],{"class":62},[52,91355,91356],{"class":102},".\n",[52,91358,91359,91362,91364,91367,91369],{"class":54,"line":66},[52,91360,91361],{"class":62},"This ",[52,91363,1444],{"class":79},[52,91365,91366],{"class":62}," a short ",[52,91368,14698],{"class":14675},[52,91370,91356],{"class":102},[639,91372,91374],{"id":91373},"possible-output","Possible output",[42,91376,91378],{"className":44,"code":91377,"language":46,"meta":47,"style":47},"Word count: 9\n",[49,91379,91380],{"__ignoreMap":47},[52,91381,91382,91384,91386],{"class":54,"line":55},[52,91383,90627],{"class":62},[52,91385,118],{"class":102},[52,91387,91388],{"class":121}," 9\n",[14,91390,91391,91392,91394],{},"The important part is ",[49,91393,61757],{},". It reads the file content into one string so you can count the words.",[37,91396,91398],{"id":91397},"limits-of-the-simple-approach","Limits of the simple approach",[14,91400,91401],{},"This example is useful, but it is still a simple method.",[14,91403,91404],{},"Things to know:",[309,91406,91407,91410,91413,91416],{},[24,91408,91409],{},"punctuation stays attached to words",[24,91411,91412],{},"hyphenated words may not behave as expected",[24,91414,91415],{},"multiple spaces are handled well, but special text patterns may not be",[24,91417,91418],{},"this is for simple counting, not advanced language processing",[14,91420,16291],{},[42,91422,91424],{"className":44,"code":91423,"language":46,"meta":47,"style":47},"text = \"Hello, world!\"\nprint(text.split())\nprint(len(text.split()))\n",[49,91425,91426,91438,91452],{"__ignoreMap":47},[52,91427,91428,91430,91432,91434,91436],{"class":54,"line":55},[52,91429,79346],{"class":62},[52,91431,80],{"class":79},[52,91433,84],{"class":83},[52,91435,38155],{"class":87},[52,91437,91],{"class":83},[52,91439,91440,91442,91444,91446,91448,91450],{"class":54,"line":66},[52,91441,242],{"class":241},[52,91443,193],{"class":102},[52,91445,955],{"class":189},[52,91447,186],{"class":102},[52,91449,58133],{"class":189},[52,91451,1816],{"class":102},[52,91453,91454,91456,91458,91460,91462,91464,91466,91468],{"class":54,"line":73},[52,91455,242],{"class":241},[52,91457,193],{"class":102},[52,91459,16152],{"class":241},[52,91461,193],{"class":102},[52,91463,955],{"class":189},[52,91465,186],{"class":102},[52,91467,58133],{"class":189},[52,91469,62172],{"class":102},[14,91471,91472],{},[5503,91473,5267],{},[42,91475,91477],{"className":44,"code":91476,"language":46,"meta":47,"style":47},"['Hello,', 'world!']\n2\n",[49,91478,91479,91501],{"__ignoreMap":47},[52,91480,91481,91483,91485,91488,91490,91492,91494,91497,91499],{"class":54,"line":55},[52,91482,250],{"class":102},[52,91484,4162],{"class":83},[52,91486,91487],{"class":87},"Hello,",[52,91489,4162],{"class":83},[52,91491,199],{"class":102},[52,91493,4172],{"class":83},[52,91495,91496],{"class":87},"world!",[52,91498,4162],{"class":83},[52,91500,1147],{"class":102},[52,91502,91503],{"class":54,"line":66},[52,91504,44748],{"class":121},[14,91506,91507,91508,91510,91511,91513],{},"This counts 2 words, which may be fine for many cases. But notice that ",[49,91509,91487],{}," still includes the comma, and ",[49,91512,91496],{}," still includes the exclamation mark.",[37,91515,91517],{"id":91516},"simple-improvement-with-cleanup","Simple improvement with cleanup",[14,91519,91520],{},"A small improvement is to clean basic punctuation before splitting.",[14,91522,24512],{},[309,91524,91525,91528,91531],{},[24,91526,91527],{},"converts text to lowercase",[24,91529,91530],{},"removes a few common punctuation marks",[24,91532,91533],{},"then counts the words",[42,91535,91537],{"className":44,"code":91536,"language":46,"meta":47,"style":47},"text = \"Hello, world! Python makes word counting simple.\"\n\nclean_text = text.lower()\nclean_text = clean_text.replace(\",\", \"\")\nclean_text = clean_text.replace(\".\", \"\")\nclean_text = clean_text.replace(\"!\", \"\")\n\nwords = clean_text.split()\ncount = len(words)\n\nprint(\"Clean text:\", clean_text)\nprint(\"Words:\", words)\nprint(\"Word count:\", count)\n",[49,91538,91539,91552,91556,91571,91598,91624,91650,91654,91668,91682,91686,91705,91723],{"__ignoreMap":47},[52,91540,91541,91543,91545,91547,91550],{"class":54,"line":55},[52,91542,79346],{"class":62},[52,91544,80],{"class":79},[52,91546,84],{"class":83},[52,91548,91549],{"class":87},"Hello, world! Python makes word counting simple.",[52,91551,91],{"class":83},[52,91553,91554],{"class":54,"line":66},[52,91555,70],{"emptyLinePlaceholder":69},[52,91557,91558,91561,91563,91565,91567,91569],{"class":54,"line":73},[52,91559,91560],{"class":62},"clean_text ",[52,91562,80],{"class":79},[52,91564,56252],{"class":62},[52,91566,186],{"class":102},[52,91568,30237],{"class":189},[52,91570,230],{"class":102},[52,91572,91573,91575,91577,91580,91582,91584,91586,91588,91590,91592,91594,91596],{"class":54,"line":94},[52,91574,91560],{"class":62},[52,91576,80],{"class":79},[52,91578,91579],{"class":62}," clean_text",[52,91581,186],{"class":102},[52,91583,79376],{"class":189},[52,91585,193],{"class":102},[52,91587,115],{"class":83},[52,91589,199],{"class":87},[52,91591,115],{"class":83},[52,91593,199],{"class":102},[52,91595,3448],{"class":83},[52,91597,211],{"class":102},[52,91599,91600,91602,91604,91606,91608,91610,91612,91614,91616,91618,91620,91622],{"class":54,"line":106},[52,91601,91560],{"class":62},[52,91603,80],{"class":79},[52,91605,91579],{"class":62},[52,91607,186],{"class":102},[52,91609,79376],{"class":189},[52,91611,193],{"class":102},[52,91613,115],{"class":83},[52,91615,186],{"class":87},[52,91617,115],{"class":83},[52,91619,199],{"class":102},[52,91621,3448],{"class":83},[52,91623,211],{"class":102},[52,91625,91626,91628,91630,91632,91634,91636,91638,91640,91642,91644,91646,91648],{"class":54,"line":128},[52,91627,91560],{"class":62},[52,91629,80],{"class":79},[52,91631,91579],{"class":62},[52,91633,186],{"class":102},[52,91635,79376],{"class":189},[52,91637,193],{"class":102},[52,91639,115],{"class":83},[52,91641,53930],{"class":87},[52,91643,115],{"class":83},[52,91645,199],{"class":102},[52,91647,3448],{"class":83},[52,91649,211],{"class":102},[52,91651,91652],{"class":54,"line":148},[52,91653,70],{"emptyLinePlaceholder":69},[52,91655,91656,91658,91660,91662,91664,91666],{"class":54,"line":164},[52,91657,32960],{"class":62},[52,91659,80],{"class":79},[52,91661,91579],{"class":62},[52,91663,186],{"class":102},[52,91665,58133],{"class":189},[52,91667,230],{"class":102},[52,91669,91670,91672,91674,91676,91678,91680],{"class":54,"line":170},[52,91671,55300],{"class":62},[52,91673,80],{"class":79},[52,91675,9150],{"class":241},[52,91677,193],{"class":102},[52,91679,67032],{"class":189},[52,91681,211],{"class":102},[52,91683,91684],{"class":54,"line":175},[52,91685,70],{"emptyLinePlaceholder":69},[52,91687,91688,91690,91692,91694,91697,91699,91701,91703],{"class":54,"line":214},[52,91689,242],{"class":241},[52,91691,193],{"class":102},[52,91693,115],{"class":83},[52,91695,91696],{"class":87},"Clean text:",[52,91698,115],{"class":83},[52,91700,199],{"class":102},[52,91702,91579],{"class":189},[52,91704,211],{"class":102},[52,91706,91707,91709,91711,91713,91715,91717,91719,91721],{"class":54,"line":233},[52,91708,242],{"class":241},[52,91710,193],{"class":102},[52,91712,115],{"class":83},[52,91714,79428],{"class":87},[52,91716,115],{"class":83},[52,91718,199],{"class":102},[52,91720,79491],{"class":189},[52,91722,211],{"class":102},[52,91724,91725,91727,91729,91731,91733,91735,91737,91739],{"class":54,"line":238},[52,91726,242],{"class":241},[52,91728,193],{"class":102},[52,91730,115],{"class":83},[52,91732,90605],{"class":87},[52,91734,115],{"class":83},[52,91736,199],{"class":102},[52,91738,71795],{"class":189},[52,91740,211],{"class":102},[14,91742,91743],{},[5503,91744,5267],{},[42,91746,91748],{"className":44,"code":91747,"language":46,"meta":47,"style":47},"Clean text: hello world python makes word counting simple\nWords: ['hello', 'world', 'python', 'makes', 'word', 'counting', 'simple']\nWord count: 7\n",[49,91749,91750,91760,91825],{"__ignoreMap":47},[52,91751,91752,91755,91757],{"class":54,"line":55},[52,91753,91754],{"class":62},"Clean text",[52,91756,118],{"class":102},[52,91758,91759],{"class":62}," hello world python makes word counting simple\n",[52,91761,91762,91764,91766,91768,91770,91772,91774,91776,91778,91781,91783,91785,91787,91789,91791,91793,91795,91797,91799,91801,91803,91805,91807,91809,91811,91813,91815,91817,91819,91821,91823],{"class":54,"line":66},[52,91763,79832],{"class":62},[52,91765,118],{"class":102},[52,91767,18162],{"class":102},[52,91769,4162],{"class":83},[52,91771,6363],{"class":87},[52,91773,4162],{"class":83},[52,91775,199],{"class":102},[52,91777,4172],{"class":83},[52,91779,91780],{"class":87},"world",[52,91782,4162],{"class":83},[52,91784,199],{"class":102},[52,91786,4172],{"class":83},[52,91788,46],{"class":87},[52,91790,4162],{"class":83},[52,91792,199],{"class":102},[52,91794,4172],{"class":83},[52,91796,90781],{"class":87},[52,91798,4162],{"class":83},[52,91800,199],{"class":102},[52,91802,4172],{"class":83},[52,91804,79503],{"class":87},[52,91806,4162],{"class":83},[52,91808,199],{"class":102},[52,91810,4172],{"class":83},[52,91812,90798],{"class":87},[52,91814,4162],{"class":83},[52,91816,199],{"class":102},[52,91818,4172],{"class":83},[52,91820,80099],{"class":87},[52,91822,4162],{"class":83},[52,91824,1147],{"class":102},[52,91826,91827,91829,91831],{"class":54,"line":73},[52,91828,90627],{"class":62},[52,91830,118],{"class":102},[52,91832,80733],{"class":121},[14,91834,91835],{},"This improves results for many common sentences.",[14,91837,91838],{},"It is still a simple approach, but it is often better than splitting the raw text directly.",[37,91840,13563],{"id":13562},[14,91842,91843],{},"Here is a short sample sentence and its expected result:",[42,91845,91846],{"className":44,"code":90547,"language":46,"meta":47,"style":47},[49,91847,91848,91860,91874,91888,91892],{"__ignoreMap":47},[52,91849,91850,91852,91854,91856,91858],{"class":54,"line":55},[52,91851,79346],{"class":62},[52,91853,80],{"class":79},[52,91855,84],{"class":83},[52,91857,90560],{"class":87},[52,91859,91],{"class":83},[52,91861,91862,91864,91866,91868,91870,91872],{"class":54,"line":66},[52,91863,32960],{"class":62},[52,91865,80],{"class":79},[52,91867,56252],{"class":62},[52,91869,186],{"class":102},[52,91871,58133],{"class":189},[52,91873,230],{"class":102},[52,91875,91876,91878,91880,91882,91884,91886],{"class":54,"line":73},[52,91877,55300],{"class":62},[52,91879,80],{"class":79},[52,91881,9150],{"class":241},[52,91883,193],{"class":102},[52,91885,67032],{"class":189},[52,91887,211],{"class":102},[52,91889,91890],{"class":54,"line":94},[52,91891,70],{"emptyLinePlaceholder":69},[52,91893,91894,91896,91898,91900,91902,91904,91906,91908],{"class":54,"line":106},[52,91895,242],{"class":241},[52,91897,193],{"class":102},[52,91899,115],{"class":83},[52,91901,90605],{"class":87},[52,91903,115],{"class":83},[52,91905,199],{"class":102},[52,91907,71795],{"class":189},[52,91909,211],{"class":102},[14,91911,91912],{},[5503,91913,5267],{},[42,91915,91916],{"className":44,"code":90620,"language":46,"meta":47,"style":47},[49,91917,91918],{"__ignoreMap":47},[52,91919,91920,91922,91924],{"class":54,"line":55},[52,91921,90627],{"class":62},[52,91923,118],{"class":102},[52,91925,5264],{"class":121},[14,91927,91928],{},"If your result is different, check the exact text you used.",[37,91930,12322],{"id":12321},[14,91932,17965],{},[639,91934,10294,91936,10598,91938],{"id":91935},"using-lentext-instead-of-lentextsplit",[49,91937,79776],{},[49,91939,79788],{},[14,91941,91942],{},"This counts characters, not words.",[42,91944,91946],{"className":44,"code":91945,"language":46,"meta":47,"style":47},"text = \"Python makes word counting simple\"\n\nprint(len(text))          # character count\nprint(len(text.split()))  # word count\n",[49,91947,91948,91960,91964,91982],{"__ignoreMap":47},[52,91949,91950,91952,91954,91956,91958],{"class":54,"line":55},[52,91951,79346],{"class":62},[52,91953,80],{"class":79},[52,91955,84],{"class":83},[52,91957,90560],{"class":87},[52,91959,91],{"class":83},[52,91961,91962],{"class":54,"line":66},[52,91963,70],{"emptyLinePlaceholder":69},[52,91965,91966,91968,91970,91972,91974,91976,91979],{"class":54,"line":73},[52,91967,242],{"class":241},[52,91969,193],{"class":102},[52,91971,16152],{"class":241},[52,91973,193],{"class":102},[52,91975,955],{"class":189},[52,91977,91978],{"class":102},"))",[52,91980,91981],{"class":4368},"          # character count\n",[52,91983,91984,91986,91988,91990,91992,91994,91996,91998,92001],{"class":54,"line":94},[52,91985,242],{"class":241},[52,91987,193],{"class":102},[52,91989,16152],{"class":241},[52,91991,193],{"class":102},[52,91993,955],{"class":189},[52,91995,186],{"class":102},[52,91997,58133],{"class":189},[52,91999,92000],{"class":102},"()))",[52,92002,92003],{"class":4368},"  # word count\n",[639,92005,92007,92008],{"id":92006},"forgetting-parentheses-in-split","Forgetting parentheses in ",[49,92009,79596],{},[14,92011,6505],{},[42,92013,92015],{"className":44,"code":92014,"language":46,"meta":47,"style":47},"words = text.split\n",[49,92016,92017],{"__ignoreMap":47},[52,92018,92019,92021,92023,92025,92027],{"class":54,"line":55},[52,92020,32960],{"class":62},[52,92022,80],{"class":79},[52,92024,56252],{"class":62},[52,92026,186],{"class":102},[52,92028,92029],{"class":1320},"split\n",[14,92031,92032],{},"This is correct:",[42,92034,92035],{"className":44,"code":90943,"language":46,"meta":47,"style":47},[49,92036,92037],{"__ignoreMap":47},[52,92038,92039,92041,92043,92045,92047,92049],{"class":54,"line":55},[52,92040,32960],{"class":62},[52,92042,80],{"class":79},[52,92044,56252],{"class":62},[52,92046,186],{"class":102},[52,92048,58133],{"class":189},[52,92050,230],{"class":102},[14,92052,92053],{},"You must call the method with parentheses.",[639,92055,92057],{"id":92056},"trying-to-count-file-words-without-reading-the-file-first","Trying to count file words without reading the file first",[14,92059,92060],{},"This will not work the way you want:",[42,92062,92064],{"className":44,"code":92063,"language":46,"meta":47,"style":47},"with open(\"sample.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(len(file.split()))\n",[49,92065,92066,92108],{"__ignoreMap":47},[52,92067,92068,92070,92072,92074,92076,92078,92080,92082,92084,92086,92088,92090,92092,92094,92096,92098,92100,92102,92104,92106],{"class":54,"line":55},[52,92069,14632],{"class":58},[52,92071,14635],{"class":241},[52,92073,193],{"class":102},[52,92075,115],{"class":83},[52,92077,91242],{"class":87},[52,92079,115],{"class":83},[52,92081,199],{"class":102},[52,92083,84],{"class":83},[52,92085,41783],{"class":87},[52,92087,115],{"class":83},[52,92089,199],{"class":102},[52,92091,14659],{"class":202},[52,92093,80],{"class":79},[52,92095,115],{"class":83},[52,92097,14666],{"class":87},[52,92099,115],{"class":83},[52,92101,1521],{"class":102},[52,92103,3567],{"class":58},[52,92105,14676],{"class":14675},[52,92107,1330],{"class":102},[52,92109,92110,92112,92114,92116,92118,92120,92122,92124],{"class":54,"line":66},[52,92111,1599],{"class":241},[52,92113,193],{"class":102},[52,92115,16152],{"class":241},[52,92117,193],{"class":102},[52,92119,14698],{"class":14675},[52,92121,186],{"class":102},[52,92123,58133],{"class":189},[52,92125,62172],{"class":102},[14,92127,92128],{},"A file object does not have the text yet. You need to read the content first:",[42,92130,92132],{"className":44,"code":92131,"language":46,"meta":47,"style":47},"with open(\"sample.txt\", \"r\", encoding=\"utf-8\") as file:\n    text = file.read()\n\nprint(len(text.split()))\n",[49,92133,92134,92176,92190,92194],{"__ignoreMap":47},[52,92135,92136,92138,92140,92142,92144,92146,92148,92150,92152,92154,92156,92158,92160,92162,92164,92166,92168,92170,92172,92174],{"class":54,"line":55},[52,92137,14632],{"class":58},[52,92139,14635],{"class":241},[52,92141,193],{"class":102},[52,92143,115],{"class":83},[52,92145,91242],{"class":87},[52,92147,115],{"class":83},[52,92149,199],{"class":102},[52,92151,84],{"class":83},[52,92153,41783],{"class":87},[52,92155,115],{"class":83},[52,92157,199],{"class":102},[52,92159,14659],{"class":202},[52,92161,80],{"class":79},[52,92163,115],{"class":83},[52,92165,14666],{"class":87},[52,92167,115],{"class":83},[52,92169,1521],{"class":102},[52,92171,3567],{"class":58},[52,92173,14676],{"class":14675},[52,92175,1330],{"class":102},[52,92177,92178,92180,92182,92184,92186,92188],{"class":54,"line":66},[52,92179,89842],{"class":62},[52,92181,80],{"class":79},[52,92183,14676],{"class":14675},[52,92185,186],{"class":102},[52,92187,61409],{"class":189},[52,92189,230],{"class":102},[52,92191,92192],{"class":54,"line":73},[52,92193,70],{"emptyLinePlaceholder":69},[52,92195,92196,92198,92200,92202,92204,92206,92208,92210],{"class":54,"line":94},[52,92197,242],{"class":241},[52,92199,193],{"class":102},[52,92201,16152],{"class":241},[52,92203,193],{"class":102},[52,92205,955],{"class":189},[52,92207,186],{"class":102},[52,92209,58133],{"class":189},[52,92211,62172],{"class":102},[639,92213,92215],{"id":92214},"expecting-punctuation-to-be-treated-the-same","Expecting punctuation to be treated the same",[14,92217,92218],{},"In the simple version:",[309,92220,92221,92225],{},[24,92222,92223],{},[49,92224,70547],{},[24,92226,92227],{},[49,92228,92229],{},"\"hello,\"",[14,92231,92232],{},"are not exactly the same text.",[14,92234,92235],{},"That is why cleanup can improve results.",[37,92237,14291],{"id":14290},[14,92239,92240],{},"If your script is not giving the result you expect, print the intermediate values.",[14,92242,31631],{},[42,92244,92246],{"className":44,"code":92245,"language":46,"meta":47,"style":47},"print(text)\nprint(text.split())\nprint(len(text.split()))\nprint(repr(text))\n",[49,92247,92248,92258,92272,92290],{"__ignoreMap":47},[52,92249,92250,92252,92254,92256],{"class":54,"line":55},[52,92251,242],{"class":241},[52,92253,193],{"class":102},[52,92255,955],{"class":189},[52,92257,211],{"class":102},[52,92259,92260,92262,92264,92266,92268,92270],{"class":54,"line":66},[52,92261,242],{"class":241},[52,92263,193],{"class":102},[52,92265,955],{"class":189},[52,92267,186],{"class":102},[52,92269,58133],{"class":189},[52,92271,1816],{"class":102},[52,92273,92274,92276,92278,92280,92282,92284,92286,92288],{"class":54,"line":73},[52,92275,242],{"class":241},[52,92277,193],{"class":102},[52,92279,16152],{"class":241},[52,92281,193],{"class":102},[52,92283,955],{"class":189},[52,92285,186],{"class":102},[52,92287,58133],{"class":189},[52,92289,62172],{"class":102},[52,92291,92292,92294,92296,92298,92300,92302],{"class":54,"line":94},[52,92293,242],{"class":241},[52,92295,193],{"class":102},[52,92297,20689],{"class":241},[52,92299,193],{"class":102},[52,92301,955],{"class":189},[52,92303,8886],{"class":102},[14,92305,92306],{},"What these help with:",[309,92308,92309,92315,92321,92327],{},[24,92310,92311,92314],{},[49,92312,92313],{},"print(text)"," shows the original text",[24,92316,92317,92320],{},[49,92318,92319],{},"print(text.split())"," shows the exact pieces being counted",[24,92322,92323,92326],{},[49,92324,92325],{},"print(len(text.split()))"," confirms the final count",[24,92328,92329,46800,92332],{},[49,92330,92331],{},"print(repr(text))",[49,92333,10956],{},[37,92335,1942],{"id":1941},[639,92337,64091,92339,92341],{"id":92338},"does-split-always-count-words-correctly",[49,92340,79596],{}," always count words correctly?",[14,92343,92344],{},"No. It works well for simple text, but punctuation and special cases can affect the result.",[639,92346,92348,92349,92351],{"id":92347},"why-not-use-lentext-for-word-count","Why not use ",[49,92350,79776],{}," for word count?",[14,92353,92354,92356,92357,92359],{},[49,92355,79776],{}," counts characters, not words. You need ",[49,92358,79788],{}," to count word-like pieces.",[639,92361,92363],{"id":92362},"can-i-count-words-in-a-file-with-the-same-method","Can I count words in a file with the same method?",[14,92365,92366,92367,759,92369,186],{},"Yes. Read the file into a string first, then use ",[49,92368,79596],{},[49,92370,44313],{},[639,92372,92374],{"id":92373},"what-happens-with-extra-spaces","What happens with extra spaces?",[14,92376,92377,92379],{},[49,92378,79596],{}," handles normal extra whitespace well and usually ignores repeated spaces.",[37,92381,2005],{"id":2004},[309,92383,92384,92391,92398,92402,92406,92412],{},[24,92385,92386],{},[372,92387,92388],{"href":79593},[49,92389,92390],{},"python string split() method",[24,92392,92393],{},[372,92394,92395],{"href":79809},[49,92396,92397],{},"python len() function explained",[24,92399,92400],{},[372,92401,6876],{"href":6397},[24,92403,92404],{},[372,92405,29921],{"href":29866},[24,92407,92408],{},[372,92409,92411],{"href":92410},"\u002Fexamples\u002Fpython-text-analysis-script-example\u002F","Python text analysis script example",[24,92413,92414],{},[372,92415,85328],{"href":92416},"\u002Fexamples\u002Fpython-read-and-write-text-file-example\u002F",[2034,92418,92419],{},"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 .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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}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}",{"title":47,"searchDepth":66,"depth":66,"links":92421},[92422,92423,92424,92425,92430,92433,92437,92438,92439,92440,92448,92449,92457],{"id":39,"depth":66,"text":40},{"id":322,"depth":66,"text":323},{"id":90662,"depth":66,"text":90663},{"id":453,"depth":66,"text":454,"children":92426},[92427,92428,92429],{"id":90912,"depth":73,"text":90913},{"id":90939,"depth":73,"text":90940},{"id":91022,"depth":73,"text":91023},{"id":91066,"depth":66,"text":91067,"children":92431},[92432],{"id":54481,"depth":73,"text":54482},{"id":91200,"depth":66,"text":91201,"children":92434},[92435,92436],{"id":91343,"depth":73,"text":91344},{"id":91373,"depth":73,"text":91374},{"id":91397,"depth":66,"text":91398},{"id":91516,"depth":66,"text":91517},{"id":13562,"depth":66,"text":13563},{"id":12321,"depth":66,"text":12322,"children":92441},[92442,92444,92446,92447],{"id":91935,"depth":73,"text":92443},"Using len(text) instead of len(text.split())",{"id":92006,"depth":73,"text":92445},"Forgetting parentheses in split()",{"id":92056,"depth":73,"text":92057},{"id":92214,"depth":73,"text":92215},{"id":14290,"depth":66,"text":14291},{"id":1941,"depth":66,"text":1942,"children":92450},[92451,92453,92455,92456],{"id":92338,"depth":73,"text":92452},"Does split() always count words correctly?",{"id":92347,"depth":73,"text":92454},"Why not use len(text) for word count?",{"id":92362,"depth":73,"text":92363},{"id":92373,"depth":73,"text":92374},{"id":2004,"depth":66,"text":2005},"Master python word count script example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-word-count-script-example",{"title":90511,"description":92458},"examples\u002Fpython-word-count-script-example","I-X6WwWAcdyZ8xDKjt8-diF-VmkrQSuh5KAQ1zSlc5E",{"id":92465,"title":92466,"body":92467,"description":94030,"extension":2075,"meta":94031,"navigation":69,"path":94032,"seo":94033,"stem":94034,"__hash__":94035},"content\u002Fexamples\u002Fpython-working-with-dates-example.md","Python Working with Dates Example",{"type":7,"value":92468,"toc":94008},[92469,92472,92478,92481,92501,92503,92598,92607,92609,92613,92630,92634,92637,92658,92662,92668,92703,92709,92713,92723,92766,92768,92789,92796,92800,92805,92848,92850,92881,92884,92907,92914,92918,92923,93010,93012,93046,93049,93086,93092,93098,93102,93108,93203,93205,93238,93241,93261,93271,93278,93282,93291,93421,93423,93449,93452,93461,93465,93471,93584,93586,93617,93623,93626,93732,93734,93736,93739,93767,93769,93791,93794,93871,93873,93884,93886,93889,93903,93906,93926,93928,93932,93940,93944,93955,93959,93964,93968,93974,93978,93981,93983,94005],[10,92470,92466],{"id":92471},"python-working-with-dates-example",[14,92473,92474,92475,92477],{},"Python can work with dates and times using the ",[49,92476,72932],{}," module. In this example, you will learn how to do common date tasks with small, clear examples.",[14,92479,92480],{},"You will see how to:",[309,92482,92483,92486,92489,92492,92495,92498],{},[24,92484,92485],{},"Get the current date",[24,92487,92488],{},"Get the current date and time",[24,92490,92491],{},"Format dates as readable text",[24,92493,92494],{},"Convert a string into a date",[24,92496,92497],{},"Compare dates",[24,92499,92500],{},"Find the number of days between dates",[37,92502,40],{"id":39},[42,92504,92506],{"className":44,"code":92505,"language":46,"meta":47,"style":47},"from datetime import datetime, date\n\ntoday = date.today()\nprint(today)\n\nnow = datetime.now()\nprint(now.strftime(\"%Y-%m-%d %H:%M:%S\"))\n",[49,92507,92508,92523,92527,92544,92554,92558,92572],{"__ignoreMap":47},[52,92509,92510,92512,92514,92516,92518,92520],{"class":54,"line":55},[52,92511,25329],{"class":58},[52,92513,29194],{"class":62},[52,92515,59],{"class":58},[52,92517,29241],{"class":62},[52,92519,199],{"class":102},[52,92521,92522],{"class":62}," date\n",[52,92524,92525],{"class":54,"line":66},[52,92526,70],{"emptyLinePlaceholder":69},[52,92528,92529,92532,92534,92537,92539,92542],{"class":54,"line":73},[52,92530,92531],{"class":62},"today ",[52,92533,80],{"class":79},[52,92535,92536],{"class":62}," date",[52,92538,186],{"class":102},[52,92540,92541],{"class":189},"today",[52,92543,230],{"class":102},[52,92545,92546,92548,92550,92552],{"class":54,"line":94},[52,92547,242],{"class":241},[52,92549,193],{"class":102},[52,92551,92541],{"class":189},[52,92553,211],{"class":102},[52,92555,92556],{"class":54,"line":106},[52,92557,70],{"emptyLinePlaceholder":69},[52,92559,92560,92562,92564,92566,92568,92570],{"class":54,"line":128},[52,92561,78974],{"class":62},[52,92563,80],{"class":79},[52,92565,29241],{"class":62},[52,92567,186],{"class":102},[52,92569,29246],{"class":189},[52,92571,230],{"class":102},[52,92573,92574,92576,92578,92580,92582,92584,92586,92588,92590,92592,92594,92596],{"class":54,"line":148},[52,92575,242],{"class":241},[52,92577,193],{"class":102},[52,92579,29246],{"class":189},[52,92581,186],{"class":102},[52,92583,29251],{"class":189},[52,92585,193],{"class":102},[52,92587,115],{"class":83},[52,92589,29258],{"class":87},[52,92591,29261],{"class":121},[52,92593,72138],{"class":87},[52,92595,115],{"class":83},[52,92597,8886],{"class":102},[14,92599,3650,92600,92603,92604,92606],{},[49,92601,92602],{},"date.today()"," for the current date and ",[49,92605,72615],{}," when you also need the current time.",[53580,92608],{},[37,92610,92612],{"id":92611},"what-this-example-covers","What this example covers",[309,92614,92615,92618,92621,92624,92627],{},[24,92616,92617],{},"Show how to get the current date",[24,92619,92620],{},"Show how to get the current date and time",[24,92622,92623],{},"Show how to format dates as readable strings",[24,92625,92626],{},"Show how to convert text into a date",[24,92628,92629],{},"Show how to compare two dates",[37,92631,92633],{"id":92632},"when-to-use-date-handling-in-python","When to use date handling in Python",[14,92635,92636],{},"Date handling is useful when you need to:",[309,92638,92639,92642,92645,92648,92651],{},[24,92640,92641],{},"Display today's date in a program",[24,92643,92644],{},"Timestamp files or logs",[24,92646,92647],{},"Check deadlines or due dates",[24,92649,92650],{},"Calculate how many days are between two dates",[24,92652,92653,92654,92657],{},"Convert user input like ",[49,92655,92656],{},"2025-04-22"," into a date object",[37,92659,92661],{"id":92660},"main-tools-used-in-this-example","Main tools used in this example",[14,92663,92664,92665,92667],{},"This page uses a few important parts of the ",[49,92666,72932],{}," module:",[309,92669,92670,92676,92682,92687,92692,92697],{},[24,92671,92672,92675],{},[49,92673,92674],{},"datetime.date"," for dates only",[24,92677,92678,92681],{},[49,92679,92680],{},"datetime.datetime"," for date and time together",[24,92683,92684,92686],{},[49,92685,72615],{}," to get the current date and time",[24,92688,92689,92691],{},[49,92690,92602],{}," to get the current date",[24,92693,92694,92696],{},[49,92695,72696],{}," to format a date as text",[24,92698,92699,92702],{},[49,92700,92701],{},"strptime()"," to parse text into a date",[14,92704,82579,92705,186],{},[372,92706,92708],{"href":92707},"\u002Fstandard-library\u002Fpython-datetime-module-overview\u002F","Python datetime module overview",[37,92710,92712],{"id":92711},"step-1-get-the-current-date","Step 1: Get the current date",[14,92714,42554,92715,28921,92718,92720,92721,186],{},[49,92716,92717],{},"date",[49,92719,72932],{},", then call ",[49,92722,92602],{},[42,92724,92726],{"className":44,"code":92725,"language":46,"meta":47,"style":47},"from datetime import date\n\ntoday = date.today()\nprint(today)\n",[49,92727,92728,92738,92742,92756],{"__ignoreMap":47},[52,92729,92730,92732,92734,92736],{"class":54,"line":55},[52,92731,25329],{"class":58},[52,92733,29194],{"class":62},[52,92735,59],{"class":58},[52,92737,92522],{"class":62},[52,92739,92740],{"class":54,"line":66},[52,92741,70],{"emptyLinePlaceholder":69},[52,92743,92744,92746,92748,92750,92752,92754],{"class":54,"line":73},[52,92745,92531],{"class":62},[52,92747,80],{"class":79},[52,92749,92536],{"class":62},[52,92751,186],{"class":102},[52,92753,92541],{"class":189},[52,92755,230],{"class":102},[52,92757,92758,92760,92762,92764],{"class":54,"line":94},[52,92759,242],{"class":241},[52,92761,193],{"class":102},[52,92763,92541],{"class":189},[52,92765,211],{"class":102},[14,92767,11845],{},[42,92769,92771],{"className":44,"code":92770,"language":46,"meta":47,"style":47},"2026-04-22\n",[49,92772,92773],{"__ignoreMap":47},[52,92774,92775,92778,92780,92782,92784,92786],{"class":54,"line":55},[52,92776,92777],{"class":121},"2026",[52,92779,5889],{"class":79},[52,92781,13294],{"class":121},[52,92783,11366],{"class":53876},[52,92785,5889],{"class":79},[52,92787,92788],{"class":121},"22\n",[14,92790,92791,92792,92795],{},"By default, Python prints dates in ",[49,92793,92794],{},"year-month-day"," format.",[37,92797,92799],{"id":92798},"step-2-get-the-current-date-and-time","Step 2: Get the current date and time",[14,92801,92802,92803,186],{},"If you also need the time, use ",[49,92804,72615],{},[42,92806,92808],{"className":44,"code":92807,"language":46,"meta":47,"style":47},"from datetime import datetime\n\nnow = datetime.now()\nprint(now)\n",[49,92809,92810,92820,92824,92838],{"__ignoreMap":47},[52,92811,92812,92814,92816,92818],{"class":54,"line":55},[52,92813,25329],{"class":58},[52,92815,29194],{"class":62},[52,92817,59],{"class":58},[52,92819,29199],{"class":62},[52,92821,92822],{"class":54,"line":66},[52,92823,70],{"emptyLinePlaceholder":69},[52,92825,92826,92828,92830,92832,92834,92836],{"class":54,"line":73},[52,92827,78974],{"class":62},[52,92829,80],{"class":79},[52,92831,29241],{"class":62},[52,92833,186],{"class":102},[52,92835,29246],{"class":189},[52,92837,230],{"class":102},[52,92839,92840,92842,92844,92846],{"class":54,"line":94},[52,92841,242],{"class":241},[52,92843,193],{"class":102},[52,92845,29246],{"class":189},[52,92847,211],{"class":102},[14,92849,11845],{},[42,92851,92853],{"className":44,"code":92852,"language":46,"meta":47,"style":47},"2026-04-22 14:35:10.123456\n",[49,92854,92855],{"__ignoreMap":47},[52,92856,92857,92859,92861,92863,92865,92867,92870,92872,92874,92876,92878],{"class":54,"line":55},[52,92858,92777],{"class":121},[52,92860,5889],{"class":79},[52,92862,13294],{"class":121},[52,92864,11366],{"class":53876},[52,92866,5889],{"class":79},[52,92868,92869],{"class":121},"22",[52,92871,45158],{"class":121},[52,92873,118],{"class":102},[52,92875,15108],{"class":121},[52,92877,118],{"class":102},[52,92879,92880],{"class":121},"10.123456\n",[14,92882,92883],{},"This includes:",[309,92885,92886,92889,92892,92895,92898,92901,92904],{},[24,92887,92888],{},"Year",[24,92890,92891],{},"Month",[24,92893,92894],{},"Day",[24,92896,92897],{},"Hour",[24,92899,92900],{},"Minute",[24,92902,92903],{},"Second",[24,92905,92906],{},"Microseconds",[14,92908,92909,92910,186],{},"For a focused explanation, see ",[372,92911,92913],{"href":92912},"\u002Fstandard-library\u002Fdatetime.now-explained","datetime.now() explained",[37,92915,92917],{"id":92916},"step-3-format-a-date-for-display","Step 3: Format a date for display",[14,92919,3650,92920,92922],{},[49,92921,72696],{}," to turn a date or datetime object into a string in the format you want.",[42,92924,92926],{"className":44,"code":92925,"language":46,"meta":47,"style":47},"from datetime import date\n\ntoday = date.today()\n\nprint(today.strftime(\"%d-%m-%Y\"))\nprint(today.strftime(\"%Y\u002F%m\u002F%d\"))\n",[49,92927,92928,92938,92942,92956,92960,92985],{"__ignoreMap":47},[52,92929,92930,92932,92934,92936],{"class":54,"line":55},[52,92931,25329],{"class":58},[52,92933,29194],{"class":62},[52,92935,59],{"class":58},[52,92937,92522],{"class":62},[52,92939,92940],{"class":54,"line":66},[52,92941,70],{"emptyLinePlaceholder":69},[52,92943,92944,92946,92948,92950,92952,92954],{"class":54,"line":73},[52,92945,92531],{"class":62},[52,92947,80],{"class":79},[52,92949,92536],{"class":62},[52,92951,186],{"class":102},[52,92953,92541],{"class":189},[52,92955,230],{"class":102},[52,92957,92958],{"class":54,"line":94},[52,92959,70],{"emptyLinePlaceholder":69},[52,92961,92962,92964,92966,92968,92970,92972,92974,92976,92978,92981,92983],{"class":54,"line":106},[52,92963,242],{"class":241},[52,92965,193],{"class":102},[52,92967,92541],{"class":189},[52,92969,186],{"class":102},[52,92971,29251],{"class":189},[52,92973,193],{"class":102},[52,92975,115],{"class":83},[52,92977,29261],{"class":121},[52,92979,92980],{"class":87},"-%m-%Y",[52,92982,115],{"class":83},[52,92984,8886],{"class":102},[52,92986,92987,92989,92991,92993,92995,92997,92999,93001,93004,93006,93008],{"class":54,"line":128},[52,92988,242],{"class":241},[52,92990,193],{"class":102},[52,92992,92541],{"class":189},[52,92994,186],{"class":102},[52,92996,29251],{"class":189},[52,92998,193],{"class":102},[52,93000,115],{"class":83},[52,93002,93003],{"class":87},"%Y\u002F%m\u002F",[52,93005,29261],{"class":121},[52,93007,115],{"class":83},[52,93009,8886],{"class":102},[14,93011,11845],{},[42,93013,93015],{"className":44,"code":93014,"language":46,"meta":47,"style":47},"22-04-2026\n2026\u002F04\u002F22\n",[49,93016,93017,93032],{"__ignoreMap":47},[52,93018,93019,93021,93023,93025,93027,93029],{"class":54,"line":55},[52,93020,92869],{"class":121},[52,93022,5889],{"class":79},[52,93024,13294],{"class":121},[52,93026,11366],{"class":53876},[52,93028,5889],{"class":79},[52,93030,93031],{"class":121},"2026\n",[52,93033,93034,93036,93038,93040,93042,93044],{"class":54,"line":66},[52,93035,92777],{"class":121},[52,93037,10537],{"class":79},[52,93039,13294],{"class":121},[52,93041,11366],{"class":53876},[52,93043,10537],{"class":79},[52,93045,92788],{"class":121},[14,93047,93048],{},"Common format codes:",[309,93050,93051,93057,93063,93068,93074,93080],{},[24,93052,93053,93056],{},[49,93054,93055],{},"%Y"," = 4-digit year",[24,93058,93059,93062],{},[49,93060,93061],{},"%m"," = month as a number",[24,93064,93065,93067],{},[49,93066,29261],{}," = day of month",[24,93069,93070,93073],{},[49,93071,93072],{},"%H"," = hour",[24,93075,93076,93079],{},[49,93077,93078],{},"%M"," = minute",[24,93081,93082,93085],{},[49,93083,93084],{},"%S"," = second",[14,93087,93088,93089,93091],{},"Important: ",[49,93090,72696],{}," changes how the value is displayed. It does not change the original date object.",[14,93093,7706,93094,186],{},[372,93095,93097],{"href":93096},"\u002Fstandard-library\u002Fdatetime.strftime-explained","datetime.strftime() explained",[37,93099,93101],{"id":93100},"step-4-convert-a-string-into-a-date","Step 4: Convert a string into a date",[14,93103,3650,93104,93107],{},[49,93105,93106],{},"datetime.strptime()"," when you have text and want to turn it into a date value.",[42,93109,93111],{"className":44,"code":93110,"language":46,"meta":47,"style":47},"from datetime import datetime\n\ntext = \"2026-04-22\"\nparsed_date = datetime.strptime(text, \"%Y-%m-%d\").date()\n\nprint(parsed_date)\nprint(type(parsed_date))\n",[49,93112,93113,93123,93127,93140,93174,93178,93189],{"__ignoreMap":47},[52,93114,93115,93117,93119,93121],{"class":54,"line":55},[52,93116,25329],{"class":58},[52,93118,29194],{"class":62},[52,93120,59],{"class":58},[52,93122,29199],{"class":62},[52,93124,93125],{"class":54,"line":66},[52,93126,70],{"emptyLinePlaceholder":69},[52,93128,93129,93131,93133,93135,93138],{"class":54,"line":73},[52,93130,79346],{"class":62},[52,93132,80],{"class":79},[52,93134,84],{"class":83},[52,93136,93137],{"class":87},"2026-04-22",[52,93139,91],{"class":83},[52,93141,93142,93145,93147,93149,93151,93154,93156,93158,93160,93162,93164,93166,93168,93170,93172],{"class":54,"line":94},[52,93143,93144],{"class":62},"parsed_date ",[52,93146,80],{"class":79},[52,93148,29241],{"class":62},[52,93150,186],{"class":102},[52,93152,93153],{"class":189},"strptime",[52,93155,193],{"class":102},[52,93157,955],{"class":189},[52,93159,199],{"class":102},[52,93161,84],{"class":83},[52,93163,29258],{"class":87},[52,93165,29261],{"class":121},[52,93167,115],{"class":83},[52,93169,54653],{"class":102},[52,93171,92717],{"class":189},[52,93173,230],{"class":102},[52,93175,93176],{"class":54,"line":106},[52,93177,70],{"emptyLinePlaceholder":69},[52,93179,93180,93182,93184,93187],{"class":54,"line":128},[52,93181,242],{"class":241},[52,93183,193],{"class":102},[52,93185,93186],{"class":189},"parsed_date",[52,93188,211],{"class":102},[52,93190,93191,93193,93195,93197,93199,93201],{"class":54,"line":148},[52,93192,242],{"class":241},[52,93194,193],{"class":102},[52,93196,4198],{"class":4733},[52,93198,193],{"class":102},[52,93200,93186],{"class":189},[52,93202,8886],{"class":102},[14,93204,5267],{},[42,93206,93208],{"className":44,"code":93207,"language":46,"meta":47,"style":47},"2026-04-22\n\u003Cclass 'datetime.date'>\n",[49,93209,93210,93224],{"__ignoreMap":47},[52,93211,93212,93214,93216,93218,93220,93222],{"class":54,"line":55},[52,93213,92777],{"class":121},[52,93215,5889],{"class":79},[52,93217,13294],{"class":121},[52,93219,11366],{"class":53876},[52,93221,5889],{"class":79},[52,93223,92788],{"class":121},[52,93225,93226,93228,93230,93232,93234,93236],{"class":54,"line":66},[52,93227,15294],{"class":79},[52,93229,6994],{"class":1475},[52,93231,4172],{"class":83},[52,93233,92674],{"class":87},[52,93235,4162],{"class":83},[52,93237,15306],{"class":79},[14,93239,93240],{},"The format string must match the input text exactly:",[309,93242,93243,93252],{},[24,93244,93245,93248,93249],{},[49,93246,93247],{},"\"2026-04-22\""," matches ",[49,93250,93251],{},"\"%Y-%m-%d\"",[24,93253,93254,93257,93258],{},[49,93255,93256],{},"\"22\u002F04\u002F2026\""," would need ",[49,93259,93260],{},"\"%d\u002F%m\u002F%Y\"",[14,93262,93263,93264,93266,93267,186],{},"If the format does not match, Python raises a ",[49,93265,13780],{},". If that happens, see ",[372,93268,93270],{"href":93269},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F","ValueError in Python: causes and fixes",[14,93272,93273,93274,186],{},"You can also read ",[372,93275,93277],{"href":93276},"\u002Fstandard-library\u002Fdatetime.strptime-explained","datetime.strptime() explained",[37,93279,93281],{"id":93280},"step-5-compare-dates","Step 5: Compare dates",[14,93283,93284,93285,2587,93287,11556,93289,186],{},"You can compare date objects with ",[49,93286,15294],{},[49,93288,13046],{},[49,93290,11062],{},[42,93292,93294],{"className":44,"code":93293,"language":46,"meta":47,"style":47},"from datetime import date\n\nstart_date = date(2026, 4, 20)\ndeadline = date(2026, 4, 25)\n\nprint(start_date \u003C deadline)\nprint(start_date == deadline)\n\nif start_date \u003C deadline:\n    print(\"The start date is earlier than the deadline.\")\n",[49,93295,93296,93306,93310,93333,93356,93360,93375,93389,93393,93406],{"__ignoreMap":47},[52,93297,93298,93300,93302,93304],{"class":54,"line":55},[52,93299,25329],{"class":58},[52,93301,29194],{"class":62},[52,93303,59],{"class":58},[52,93305,92522],{"class":62},[52,93307,93308],{"class":54,"line":66},[52,93309,70],{"emptyLinePlaceholder":69},[52,93311,93312,93315,93317,93319,93321,93323,93325,93327,93329,93331],{"class":54,"line":73},[52,93313,93314],{"class":62},"start_date ",[52,93316,80],{"class":79},[52,93318,92536],{"class":189},[52,93320,193],{"class":102},[52,93322,92777],{"class":121},[52,93324,199],{"class":102},[52,93326,32111],{"class":121},[52,93328,199],{"class":102},[52,93330,32771],{"class":121},[52,93332,211],{"class":102},[52,93334,93335,93338,93340,93342,93344,93346,93348,93350,93352,93354],{"class":54,"line":94},[52,93336,93337],{"class":62},"deadline ",[52,93339,80],{"class":79},[52,93341,92536],{"class":189},[52,93343,193],{"class":102},[52,93345,92777],{"class":121},[52,93347,199],{"class":102},[52,93349,32111],{"class":121},[52,93351,199],{"class":102},[52,93353,2674],{"class":121},[52,93355,211],{"class":102},[52,93357,93358],{"class":54,"line":106},[52,93359,70],{"emptyLinePlaceholder":69},[52,93361,93362,93364,93366,93368,93370,93373],{"class":54,"line":128},[52,93363,242],{"class":241},[52,93365,193],{"class":102},[52,93367,93314],{"class":189},[52,93369,15294],{"class":79},[52,93371,93372],{"class":189}," deadline",[52,93374,211],{"class":102},[52,93376,93377,93379,93381,93383,93385,93387],{"class":54,"line":148},[52,93378,242],{"class":241},[52,93380,193],{"class":102},[52,93382,93314],{"class":189},[52,93384,11062],{"class":79},[52,93386,93372],{"class":189},[52,93388,211],{"class":102},[52,93390,93391],{"class":54,"line":164},[52,93392,70],{"emptyLinePlaceholder":69},[52,93394,93395,93397,93400,93402,93404],{"class":54,"line":170},[52,93396,1313],{"class":58},[52,93398,93399],{"class":62}," start_date ",[52,93401,15294],{"class":79},[52,93403,93372],{"class":62},[52,93405,1330],{"class":102},[52,93407,93408,93410,93412,93414,93417,93419],{"class":54,"line":175},[52,93409,1599],{"class":241},[52,93411,193],{"class":102},[52,93413,115],{"class":83},[52,93415,93416],{"class":87},"The start date is earlier than the deadline.",[52,93418,115],{"class":83},[52,93420,211],{"class":102},[14,93422,5267],{},[42,93424,93426],{"className":44,"code":93425,"language":46,"meta":47,"style":47},"True\nFalse\nThe start date is earlier than the deadline.\n",[49,93427,93428,93432,93437],{"__ignoreMap":47},[52,93429,93430],{"class":54,"line":55},[52,93431,40188],{"class":160},[52,93433,93434],{"class":54,"line":66},[52,93435,93436],{"class":160},"False\n",[52,93438,93439,93442,93444,93447],{"class":54,"line":73},[52,93440,93441],{"class":62},"The start date ",[52,93443,1444],{"class":79},[52,93445,93446],{"class":62}," earlier than the deadline",[52,93448,91356],{"class":102},[14,93450,93451],{},"Earlier dates are smaller than later dates.",[14,93453,93454,93455,93457,93458,93460],{},"Make sure both values are date objects. Do not compare a plain string like ",[49,93456,93247],{}," with a ",[49,93459,92717],{}," object.",[37,93462,93464],{"id":93463},"step-6-find-the-difference-between-dates","Step 6: Find the difference between dates",[14,93466,93467,93468,93460],{},"You can subtract one date from another. The result is a ",[49,93469,93470],{},"timedelta",[42,93472,93474],{"className":44,"code":93473,"language":46,"meta":47,"style":47},"from datetime import date\n\ntoday = date(2026, 4, 22)\ndue_date = date(2026, 5, 1)\n\ndifference = due_date - today\n\nprint(difference)\nprint(difference.days)\n",[49,93475,93476,93486,93490,93512,93535,93539,93554,93558,93569],{"__ignoreMap":47},[52,93477,93478,93480,93482,93484],{"class":54,"line":55},[52,93479,25329],{"class":58},[52,93481,29194],{"class":62},[52,93483,59],{"class":58},[52,93485,92522],{"class":62},[52,93487,93488],{"class":54,"line":66},[52,93489,70],{"emptyLinePlaceholder":69},[52,93491,93492,93494,93496,93498,93500,93502,93504,93506,93508,93510],{"class":54,"line":73},[52,93493,92531],{"class":62},[52,93495,80],{"class":79},[52,93497,92536],{"class":189},[52,93499,193],{"class":102},[52,93501,92777],{"class":121},[52,93503,199],{"class":102},[52,93505,32111],{"class":121},[52,93507,199],{"class":102},[52,93509,16721],{"class":121},[52,93511,211],{"class":102},[52,93513,93514,93517,93519,93521,93523,93525,93527,93529,93531,93533],{"class":54,"line":94},[52,93515,93516],{"class":62},"due_date ",[52,93518,80],{"class":79},[52,93520,92536],{"class":189},[52,93522,193],{"class":102},[52,93524,92777],{"class":121},[52,93526,199],{"class":102},[52,93528,7646],{"class":121},[52,93530,199],{"class":102},[52,93532,1518],{"class":121},[52,93534,211],{"class":102},[52,93536,93537],{"class":54,"line":106},[52,93538,70],{"emptyLinePlaceholder":69},[52,93540,93541,93544,93546,93549,93551],{"class":54,"line":128},[52,93542,93543],{"class":62},"difference ",[52,93545,80],{"class":79},[52,93547,93548],{"class":62}," due_date ",[52,93550,5889],{"class":79},[52,93552,93553],{"class":62}," today\n",[52,93555,93556],{"class":54,"line":148},[52,93557,70],{"emptyLinePlaceholder":69},[52,93559,93560,93562,93564,93567],{"class":54,"line":164},[52,93561,242],{"class":241},[52,93563,193],{"class":102},[52,93565,93566],{"class":189},"difference",[52,93568,211],{"class":102},[52,93570,93571,93573,93575,93577,93579,93582],{"class":54,"line":170},[52,93572,242],{"class":241},[52,93574,193],{"class":102},[52,93576,93566],{"class":189},[52,93578,186],{"class":102},[52,93580,93581],{"class":1320},"days",[52,93583,211],{"class":102},[14,93585,5267],{},[42,93587,93589],{"className":44,"code":93588,"language":46,"meta":47,"style":47},"9 days, 0:00:00\n9\n",[49,93590,93591,93612],{"__ignoreMap":47},[52,93592,93593,93595,93598,93600,93602,93604,93607,93609],{"class":54,"line":55},[52,93594,37264],{"class":121},[52,93596,93597],{"class":62}," days",[52,93599,199],{"class":102},[52,93601,13049],{"class":121},[52,93603,118],{"class":102},[52,93605,93606],{"class":121},"00",[52,93608,118],{"class":102},[52,93610,93611],{"class":121},"00\n",[52,93613,93614],{"class":54,"line":66},[52,93615,93616],{"class":121},"9\n",[14,93618,3650,93619,93622],{},[49,93620,93621],{},".days"," when you want just the number of days.",[14,93624,93625],{},"Here is a simple countdown example:",[42,93627,93629],{"className":44,"code":93628,"language":46,"meta":47,"style":47},"from datetime import date\n\ntoday = date.today()\ndeadline = date(2026, 5, 1)\n\ndays_left = (deadline - today).days\n\nprint(f\"Days left: {days_left}\")\n",[49,93630,93631,93641,93645,93659,93681,93685,93706,93710],{"__ignoreMap":47},[52,93632,93633,93635,93637,93639],{"class":54,"line":55},[52,93634,25329],{"class":58},[52,93636,29194],{"class":62},[52,93638,59],{"class":58},[52,93640,92522],{"class":62},[52,93642,93643],{"class":54,"line":66},[52,93644,70],{"emptyLinePlaceholder":69},[52,93646,93647,93649,93651,93653,93655,93657],{"class":54,"line":73},[52,93648,92531],{"class":62},[52,93650,80],{"class":79},[52,93652,92536],{"class":62},[52,93654,186],{"class":102},[52,93656,92541],{"class":189},[52,93658,230],{"class":102},[52,93660,93661,93663,93665,93667,93669,93671,93673,93675,93677,93679],{"class":54,"line":94},[52,93662,93337],{"class":62},[52,93664,80],{"class":79},[52,93666,92536],{"class":189},[52,93668,193],{"class":102},[52,93670,92777],{"class":121},[52,93672,199],{"class":102},[52,93674,7646],{"class":121},[52,93676,199],{"class":102},[52,93678,1518],{"class":121},[52,93680,211],{"class":102},[52,93682,93683],{"class":54,"line":106},[52,93684,70],{"emptyLinePlaceholder":69},[52,93686,93687,93690,93692,93694,93696,93698,93701,93703],{"class":54,"line":128},[52,93688,93689],{"class":62},"days_left ",[52,93691,80],{"class":79},[52,93693,3104],{"class":102},[52,93695,93337],{"class":62},[52,93697,5889],{"class":79},[52,93699,93700],{"class":62}," today",[52,93702,54653],{"class":102},[52,93704,93705],{"class":1320},"days\n",[52,93707,93708],{"class":54,"line":148},[52,93709,70],{"emptyLinePlaceholder":69},[52,93711,93712,93714,93716,93718,93721,93723,93726,93728,93730],{"class":54,"line":164},[52,93713,242],{"class":241},[52,93715,193],{"class":102},[52,93717,1476],{"class":1475},[52,93719,93720],{"class":87},"\"Days left: ",[52,93722,1482],{"class":121},[52,93724,93725],{"class":189},"days_left",[52,93727,1488],{"class":121},[52,93729,115],{"class":87},[52,93731,211],{"class":102},[53580,93733],{},[37,93735,6402],{"id":6401},[14,93737,93738],{},"Here are some common problems when working with dates in Python:",[309,93740,93741,93747,93753,93756,93762],{},[24,93742,14067,93743,857,93745],{},[49,93744,92717],{},[49,93746,72932],{},[24,93748,93749,93750,93752],{},"Using the wrong ",[49,93751,92701],{}," format string",[24,93754,93755],{},"Mixing strings and date objects in comparisons",[24,93757,93758,93759,45753],{},"Calling ",[49,93760,93761],{},"datetime.today",[24,93763,93764,93765],{},"Expecting formatted output without using ",[49,93766,72696],{},[14,93768,14379],{},[309,93770,93771,93774,93779,93785,93788],{},[24,93772,93773],{},"Using a date string without converting it first",[24,93775,93776,93777],{},"Wrong format code in ",[49,93778,92701],{},[24,93780,37633,93781,62211,93783],{},[49,93782,92717],{},[49,93784,72932],{},[24,93786,93787],{},"Trying to compare text values instead of date objects",[24,93789,93790],{},"Forgetting that month and day order must match the format string",[14,93792,93793],{},"If something is not working, these simple checks can help:",[42,93795,93797],{"className":44,"code":93796,"language":46,"meta":47,"style":47},"python your_script.py\nprint(type(today))\nprint(type(user_input))\nprint(parsed_date)\nprint(parsed_date.strftime('%Y-%m-%d'))\n",[49,93798,93799,93809,93823,93837,93847],{"__ignoreMap":47},[52,93800,93801,93804,93806],{"class":54,"line":55},[52,93802,93803],{"class":62},"python your_script",[52,93805,186],{"class":102},[52,93807,93808],{"class":1320},"py\n",[52,93810,93811,93813,93815,93817,93819,93821],{"class":54,"line":66},[52,93812,242],{"class":241},[52,93814,193],{"class":102},[52,93816,4198],{"class":4733},[52,93818,193],{"class":102},[52,93820,92541],{"class":189},[52,93822,8886],{"class":102},[52,93824,93825,93827,93829,93831,93833,93835],{"class":54,"line":73},[52,93826,242],{"class":241},[52,93828,193],{"class":102},[52,93830,4198],{"class":4733},[52,93832,193],{"class":102},[52,93834,14364],{"class":189},[52,93836,8886],{"class":102},[52,93838,93839,93841,93843,93845],{"class":54,"line":94},[52,93840,242],{"class":241},[52,93842,193],{"class":102},[52,93844,93186],{"class":189},[52,93846,211],{"class":102},[52,93848,93849,93851,93853,93855,93857,93859,93861,93863,93865,93867,93869],{"class":54,"line":106},[52,93850,242],{"class":241},[52,93852,193],{"class":102},[52,93854,93186],{"class":189},[52,93856,186],{"class":102},[52,93858,29251],{"class":189},[52,93860,193],{"class":102},[52,93862,4162],{"class":83},[52,93864,29258],{"class":87},[52,93866,29261],{"class":121},[52,93868,4162],{"class":83},[52,93870,8886],{"class":102},[14,93872,61076],{},[309,93874,93875,93878,93881],{},[24,93876,93877],{},"Whether a value is a string or a date object",[24,93879,93880],{},"Whether parsing worked correctly",[24,93882,93883],{},"Whether your formatted output looks the way you expect",[37,93885,14407],{"id":14406},[14,93887,93888],{},"After this example, a good next step is to practice real tasks such as:",[309,93890,93891,93894,93897,93900],{},[24,93892,93893],{},"Adding timestamps to logs",[24,93895,93896],{},"Checking whether a deadline has passed",[24,93898,93899],{},"Parsing user-entered dates",[24,93901,93902],{},"Building a small due-date checker script",[14,93904,93905],{},"You may also want to learn:",[309,93907,93908,93912,93916,93920],{},[24,93909,93910],{},[372,93911,92913],{"href":92912},[24,93913,93914],{},[372,93915,93277],{"href":93276},[24,93917,93918],{},[372,93919,93097],{"href":93096},[24,93921,93922],{},[372,93923,93925],{"href":93924},"\u002Fhow-to\u002Fhow-to-format-strings-in-python\u002F","How to format strings in Python",[37,93927,1942],{"id":1941},[639,93929,93931],{"id":93930},"what-is-the-difference-between-date-and-datetime-in-python","What is the difference between date and datetime in Python?",[14,93933,93934,93936,93937,93939],{},[49,93935,92717],{}," stores only the year, month, and day. ",[49,93938,72932],{}," stores the date and the time.",[639,93941,93943],{"id":93942},"how-do-i-print-a-date-in-a-different-format","How do I print a date in a different format?",[14,93945,3650,93946,93948,93949,2587,93951,11556,93953,186],{},[49,93947,72696],{}," with format codes like ",[49,93950,93055],{},[49,93952,93061],{},[49,93954,29261],{},[639,93956,93958],{"id":93957},"how-do-i-turn-a-string-into-a-date","How do I turn a string into a date?",[14,93960,3650,93961,93963],{},[49,93962,93106],{}," and make sure the format string matches the input text.",[639,93965,93967],{"id":93966},"how-do-i-calculate-the-number-of-days-between-two-dates","How do I calculate the number of days between two dates?",[14,93969,93970,93971,93973],{},"Subtract one date from another and use the ",[49,93972,93621],{}," value from the result.",[639,93975,93977],{"id":93976},"why-does-parsing-a-date-string-fail","Why does parsing a date string fail?",[14,93979,93980],{},"Usually the input text does not match the format string exactly.",[37,93982,2005],{"id":2004},[309,93984,93985,93989,93993,93997,94001],{},[24,93986,93987],{},[372,93988,92708],{"href":92707},[24,93990,93991],{},[372,93992,92913],{"href":92912},[24,93994,93995],{},[372,93996,93277],{"href":93276},[24,93998,93999],{},[372,94000,93097],{"href":93096},[24,94002,94003],{},[372,94004,93270],{"href":93269},[2034,94006,94007],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html 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 .sx12J, html code.shiki .sx12J{--shiki-light:#F76D47;--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 .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}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":47,"searchDepth":66,"depth":66,"links":94009},[94010,94011,94012,94013,94014,94015,94016,94017,94018,94019,94020,94021,94022,94029],{"id":39,"depth":66,"text":40},{"id":92611,"depth":66,"text":92612},{"id":92632,"depth":66,"text":92633},{"id":92660,"depth":66,"text":92661},{"id":92711,"depth":66,"text":92712},{"id":92798,"depth":66,"text":92799},{"id":92916,"depth":66,"text":92917},{"id":93100,"depth":66,"text":93101},{"id":93280,"depth":66,"text":93281},{"id":93463,"depth":66,"text":93464},{"id":6401,"depth":66,"text":6402},{"id":14406,"depth":66,"text":14407},{"id":1941,"depth":66,"text":1942,"children":94023},[94024,94025,94026,94027,94028],{"id":93930,"depth":73,"text":93931},{"id":93942,"depth":73,"text":93943},{"id":93957,"depth":73,"text":93958},{"id":93966,"depth":73,"text":93967},{"id":93976,"depth":73,"text":93977},{"id":2004,"depth":66,"text":2005},"Master python working with dates example in our comprehensive Python beginner guide.",{},"\u002Fexamples\u002Fpython-working-with-dates-example",{"title":92466,"description":94030},"examples\u002Fpython-working-with-dates-example","1EZ-rO7HcJWagJfG7cd8BPo9eJ2I3LXPa6mY4_hO3GA",1776944748609]