[{"data":1,"prerenderedAt":58061},["ShallowReactive",2],{"doc-\u002Fstandard-library\u002Frandom.randint-function-explained":3,"nav-standard-library":1583},{"id":4,"title":5,"body":6,"description":1576,"extension":1577,"meta":1578,"navigation":71,"path":1579,"seo":1580,"stem":1581,"__hash__":1582},"content\u002Fstandard-library\u002Frandom.randint-function-explained.md","random.randint() Function Explained",{"type":7,"value":8,"toc":1548},"minimark",[9,13,21,39,44,124,139,143,148,171,174,188,196,200,203,227,230,251,254,303,306,315,328,332,341,344,383,386,402,413,416,447,450,470,473,490,494,497,559,562,577,580,583,597,601,606,655,659,709,716,720,780,783,787,794,938,940,965,975,979,983,989,992,1029,1032,1078,1088,1092,1095,1118,1126,1133,1137,1142,1145,1194,1197,1200,1248,1252,1254,1301,1308,1311,1357,1361,1370,1373,1407,1416,1420,1424,1427,1431,1434,1436,1476,1480,1485,1489,1497,1501,1507,1511,1544],[10,11,5],"h1",{"id":12},"randomrandint-function-explained",[14,15,16,20],"p",{},[17,18,19],"code",{},"random.randint()"," gives you a random whole number between two values.",[14,22,23,24,27,28,30,31,33,34,38],{},"It is part of Python’s ",[17,25,26],{},"random"," module, so you must import ",[17,29,26],{}," before using it. A very important detail is that ",[17,32,19],{}," includes ",[35,36,37],"strong",{},"both"," the start and end values.",[40,41,43],"h2",{"id":42},"quick-answer","Quick answer",[45,46,51],"pre",{"className":47,"code":48,"language":49,"meta":50,"style":50},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import random\n\nnumber = random.randint(1, 10)\nprint(number)\n","python","",[17,52,53,66,73,110],{"__ignoreMap":50},[54,55,58,62],"span",{"class":56,"line":57},"line",1,[54,59,61],{"class":60},"sVHd0","import",[54,63,65],{"class":64},"su5hD"," random\n",[54,67,69],{"class":56,"line":68},2,[54,70,72],{"emptyLinePlaceholder":71},true,"\n",[54,74,76,79,83,86,90,94,97,101,104,107],{"class":56,"line":75},3,[54,77,78],{"class":64},"number ",[54,80,82],{"class":81},"smGrS","=",[54,84,85],{"class":64}," random",[54,87,89],{"class":88},"sP7_E",".",[54,91,93],{"class":92},"slqww","randint",[54,95,96],{"class":88},"(",[54,98,100],{"class":99},"srdBf","1",[54,102,103],{"class":88},",",[54,105,106],{"class":99}," 10",[54,108,109],{"class":88},")\n",[54,111,113,117,119,122],{"class":56,"line":112},4,[54,114,116],{"class":115},"sptTA","print",[54,118,96],{"class":88},[54,120,121],{"class":92},"number",[54,123,109],{"class":88},[14,125,126,127,130,131,134,135,138],{},"Use ",[17,128,129],{},"random.randint(start, end)"," to get a random integer between ",[17,132,133],{},"start"," and ",[17,136,137],{},"end",", including both values.",[40,140,142],{"id":141},"what-randomrandint-does","What random.randint() does",[14,144,145,147],{},[17,146,19],{},":",[149,150,151,155,161,168],"ul",{},[152,153,154],"li",{},"Returns a random integer",[152,156,157,158,160],{},"Belongs to Python’s ",[17,159,26],{}," module",[152,162,163,164,167],{},"Requires ",[17,165,166],{},"import random"," first",[152,169,170],{},"Includes both the starting and ending numbers",[14,172,173],{},"This makes it useful when you need a random whole number for things like:",[149,175,176,179,182,185],{},[152,177,178],{},"Dice rolls",[152,180,181],{},"Guessing games",[152,183,184],{},"Simple test values",[152,186,187],{},"Random positions or indexes",[14,189,190,191,89],{},"If you want a broader introduction to random tools in Python, see the ",[192,193,195],"a",{"href":194},"\u002Fstandard-library\u002Fpython-random-module-overview","Python random module overview",[40,197,199],{"id":198},"basic-syntax","Basic syntax",[14,201,202],{},"The basic syntax is:",[45,204,206],{"className":47,"code":205,"language":49,"meta":50,"style":50},"random.randint(a, b)\n",[17,207,208],{"__ignoreMap":50},[54,209,210,212,214,216,218,220,222,225],{"class":56,"line":57},[54,211,26],{"class":64},[54,213,89],{"class":88},[54,215,93],{"class":92},[54,217,96],{"class":88},[54,219,192],{"class":92},[54,221,103],{"class":88},[54,223,224],{"class":92}," b",[54,226,109],{"class":88},[14,228,229],{},"Here:",[149,231,232,237,243],{},[152,233,234,236],{},[17,235,192],{}," is the smallest possible integer",[152,238,239,242],{},[17,240,241],{},"b"," is the largest possible integer",[152,244,245,246,248,249],{},"The result can be any whole number from ",[17,247,192],{}," to ",[17,250,241],{},[14,252,253],{},"Example:",[45,255,257],{"className":47,"code":256,"language":49,"meta":50,"style":50},"import random\n\nnumber = random.randint(5, 8)\nprint(number)\n",[17,258,259,265,269,293],{"__ignoreMap":50},[54,260,261,263],{"class":56,"line":57},[54,262,61],{"class":60},[54,264,65],{"class":64},[54,266,267],{"class":56,"line":68},[54,268,72],{"emptyLinePlaceholder":71},[54,270,271,273,275,277,279,281,283,286,288,291],{"class":56,"line":75},[54,272,78],{"class":64},[54,274,82],{"class":81},[54,276,85],{"class":64},[54,278,89],{"class":88},[54,280,93],{"class":92},[54,282,96],{"class":88},[54,284,285],{"class":99},"5",[54,287,103],{"class":88},[54,289,290],{"class":99}," 8",[54,292,109],{"class":88},[54,294,295,297,299,301],{"class":56,"line":112},[54,296,116],{"class":115},[54,298,96],{"class":88},[54,300,121],{"class":92},[54,302,109],{"class":88},[14,304,305],{},"Possible output:",[45,307,309],{"className":47,"code":308,"language":49,"meta":50,"style":50},"7\n",[17,310,311],{"__ignoreMap":50},[54,312,313],{"class":56,"line":57},[54,314,308],{"class":99},[14,316,317,318,320,321,324,325,89],{},"It could also print ",[17,319,285],{},", ",[17,322,323],{},"6",", or ",[17,326,327],{},"8",[40,329,331],{"id":330},"how-the-range-works","How the range works",[14,333,334,335,337,338,89],{},"The range used by ",[17,336,19],{}," is ",[35,339,340],{},"inclusive",[14,342,343],{},"That means:",[45,345,347],{"className":47,"code":346,"language":49,"meta":50,"style":50},"import random\n\nprint(random.randint(1, 3))\n",[17,348,349,355,359],{"__ignoreMap":50},[54,350,351,353],{"class":56,"line":57},[54,352,61],{"class":60},[54,354,65],{"class":64},[54,356,357],{"class":56,"line":68},[54,358,72],{"emptyLinePlaceholder":71},[54,360,361,363,365,367,369,371,373,375,377,380],{"class":56,"line":75},[54,362,116],{"class":115},[54,364,96],{"class":88},[54,366,26],{"class":92},[54,368,89],{"class":88},[54,370,93],{"class":92},[54,372,96],{"class":88},[54,374,100],{"class":99},[54,376,103],{"class":88},[54,378,379],{"class":99}," 3",[54,381,382],{"class":88},"))\n",[14,384,385],{},"Can return:",[149,387,388,392,397],{},[152,389,390],{},[17,391,100],{},[152,393,394],{},[17,395,396],{},"2",[152,398,399],{},[17,400,401],{},"3",[14,403,404,405,412],{},"This is different from Python’s ",[192,406,408,411],{"href":407},"\u002Freference\u002Fpython-range-function-explained",[17,409,410],{},"range()"," function",", where the stop value is not included.",[14,414,415],{},"For example:",[45,417,419],{"className":47,"code":418,"language":49,"meta":50,"style":50},"print(list(range(1, 3)))\n",[17,420,421],{"__ignoreMap":50},[54,422,423,425,427,431,433,436,438,440,442,444],{"class":56,"line":57},[54,424,116],{"class":115},[54,426,96],{"class":88},[54,428,430],{"class":429},"sZMiF","list",[54,432,96],{"class":88},[54,434,435],{"class":115},"range",[54,437,96],{"class":88},[54,439,100],{"class":99},[54,441,103],{"class":88},[54,443,379],{"class":99},[54,445,446],{"class":88},")))\n",[14,448,449],{},"Output:",[45,451,453],{"className":47,"code":452,"language":49,"meta":50,"style":50},"[1, 2]\n",[17,454,455],{"__ignoreMap":50},[54,456,457,460,462,464,467],{"class":56,"line":57},[54,458,459],{"class":88},"[",[54,461,100],{"class":99},[54,463,103],{"class":88},[54,465,466],{"class":99}," 2",[54,468,469],{"class":88},"]\n",[14,471,472],{},"This difference causes a lot of beginner mistakes:",[149,474,475,482],{},[152,476,477,33,480],{},[17,478,479],{},"random.randint(1, 3)",[17,481,401],{},[152,483,484,487,488],{},[17,485,486],{},"range(1, 3)"," does not include ",[17,489,401],{},[40,491,493],{"id":492},"simple-example","Simple example",[14,495,496],{},"Here is a simple dice roll example:",[45,498,500],{"className":47,"code":499,"language":49,"meta":50,"style":50},"import random\n\nroll = random.randint(1, 6)\nprint(\"You rolled:\", roll)\n",[17,501,502,508,512,536],{"__ignoreMap":50},[54,503,504,506],{"class":56,"line":57},[54,505,61],{"class":60},[54,507,65],{"class":64},[54,509,510],{"class":56,"line":68},[54,511,72],{"emptyLinePlaceholder":71},[54,513,514,517,519,521,523,525,527,529,531,534],{"class":56,"line":75},[54,515,516],{"class":64},"roll ",[54,518,82],{"class":81},[54,520,85],{"class":64},[54,522,89],{"class":88},[54,524,93],{"class":92},[54,526,96],{"class":88},[54,528,100],{"class":99},[54,530,103],{"class":88},[54,532,533],{"class":99}," 6",[54,535,109],{"class":88},[54,537,538,540,542,546,550,552,554,557],{"class":56,"line":112},[54,539,116],{"class":115},[54,541,96],{"class":88},[54,543,545],{"class":544},"sjJ54","\"",[54,547,549],{"class":548},"s_sjI","You rolled:",[54,551,545],{"class":544},[54,553,103],{"class":88},[54,555,556],{"class":92}," roll",[54,558,109],{"class":88},[14,560,561],{},"Example output:",[45,563,565],{"className":47,"code":564,"language":49,"meta":50,"style":50},"You rolled: 4\n",[17,566,567],{"__ignoreMap":50},[54,568,569,572,574],{"class":56,"line":57},[54,570,571],{"class":64},"You rolled",[54,573,147],{"class":88},[54,575,576],{"class":99}," 4\n",[14,578,579],{},"The output changes each time you run the program.",[14,581,582],{},"This is a common beginner use case because it matches real-world rules:",[149,584,585,588,591],{},[152,586,587],{},"A die has values from 1 to 6",[152,589,590],{},"Both 1 and 6 should be possible",[152,592,593,596],{},[17,594,595],{},"randint(1, 6)"," fits perfectly",[40,598,600],{"id":599},"using-randomrandint-in-real-tasks","Using random.randint() in real tasks",[602,603,605],"h3",{"id":604},"pick-a-random-dice-roll","Pick a random dice roll",[45,607,609],{"className":47,"code":608,"language":49,"meta":50,"style":50},"import random\n\ndice = random.randint(1, 6)\nprint(dice)\n",[17,610,611,617,621,644],{"__ignoreMap":50},[54,612,613,615],{"class":56,"line":57},[54,614,61],{"class":60},[54,616,65],{"class":64},[54,618,619],{"class":56,"line":68},[54,620,72],{"emptyLinePlaceholder":71},[54,622,623,626,628,630,632,634,636,638,640,642],{"class":56,"line":75},[54,624,625],{"class":64},"dice ",[54,627,82],{"class":81},[54,629,85],{"class":64},[54,631,89],{"class":88},[54,633,93],{"class":92},[54,635,96],{"class":88},[54,637,100],{"class":99},[54,639,103],{"class":88},[54,641,533],{"class":99},[54,643,109],{"class":88},[54,645,646,648,650,653],{"class":56,"line":112},[54,647,116],{"class":115},[54,649,96],{"class":88},[54,651,652],{"class":92},"dice",[54,654,109],{"class":88},[602,656,658],{"id":657},"choose-a-random-number-for-a-guessing-game","Choose a random number for a guessing game",[45,660,662],{"className":47,"code":661,"language":49,"meta":50,"style":50},"import random\n\nsecret_number = random.randint(1, 20)\nprint(secret_number)\n",[17,663,664,670,674,698],{"__ignoreMap":50},[54,665,666,668],{"class":56,"line":57},[54,667,61],{"class":60},[54,669,65],{"class":64},[54,671,672],{"class":56,"line":68},[54,673,72],{"emptyLinePlaceholder":71},[54,675,676,679,681,683,685,687,689,691,693,696],{"class":56,"line":75},[54,677,678],{"class":64},"secret_number ",[54,680,82],{"class":81},[54,682,85],{"class":64},[54,684,89],{"class":88},[54,686,93],{"class":92},[54,688,96],{"class":88},[54,690,100],{"class":99},[54,692,103],{"class":88},[54,694,695],{"class":99}," 20",[54,697,109],{"class":88},[54,699,700,702,704,707],{"class":56,"line":112},[54,701,116],{"class":115},[54,703,96],{"class":88},[54,705,706],{"class":92},"secret_number",[54,708,109],{"class":88},[14,710,711,712,89],{},"You can see this idea in a full beginner project: ",[192,713,715],{"href":714},"\u002Fexamples\u002Fpython-number-guessing-game-example","Python number guessing game example",[602,717,719],{"id":718},"create-simple-test-data","Create simple test data",[45,721,723],{"className":47,"code":722,"language":49,"meta":50,"style":50},"import random\n\nscore = random.randint(0, 100)\nprint(\"Test score:\", score)\n",[17,724,725,731,735,760],{"__ignoreMap":50},[54,726,727,729],{"class":56,"line":57},[54,728,61],{"class":60},[54,730,65],{"class":64},[54,732,733],{"class":56,"line":68},[54,734,72],{"emptyLinePlaceholder":71},[54,736,737,740,742,744,746,748,750,753,755,758],{"class":56,"line":75},[54,738,739],{"class":64},"score ",[54,741,82],{"class":81},[54,743,85],{"class":64},[54,745,89],{"class":88},[54,747,93],{"class":92},[54,749,96],{"class":88},[54,751,752],{"class":99},"0",[54,754,103],{"class":88},[54,756,757],{"class":99}," 100",[54,759,109],{"class":88},[54,761,762,764,766,768,771,773,775,778],{"class":56,"line":112},[54,763,116],{"class":115},[54,765,96],{"class":88},[54,767,545],{"class":544},[54,769,770],{"class":548},"Test score:",[54,772,545],{"class":544},[54,774,103],{"class":88},[54,776,777],{"class":92}," score",[54,779,109],{"class":88},[14,781,782],{},"This is useful when you want sample numbers while practicing.",[602,784,786],{"id":785},"generate-a-random-index-carefully","Generate a random index carefully",[14,788,789,790,793],{},"You can use ",[17,791,792],{},"randint()"," to choose an index, but you must be careful to stay inside the valid range.",[45,795,797],{"className":47,"code":796,"language":49,"meta":50,"style":50},"import random\n\nnames = [\"Ana\", \"Ben\", \"Cara\"]\nindex = random.randint(0, len(names) - 1)\n\nprint(\"Index:\", index)\nprint(\"Chosen name:\", names[index])\n",[17,798,799,805,809,847,885,890,911],{"__ignoreMap":50},[54,800,801,803],{"class":56,"line":57},[54,802,61],{"class":60},[54,804,65],{"class":64},[54,806,807],{"class":56,"line":68},[54,808,72],{"emptyLinePlaceholder":71},[54,810,811,814,816,819,821,824,826,828,831,834,836,838,840,843,845],{"class":56,"line":75},[54,812,813],{"class":64},"names ",[54,815,82],{"class":81},[54,817,818],{"class":88}," [",[54,820,545],{"class":544},[54,822,823],{"class":548},"Ana",[54,825,545],{"class":544},[54,827,103],{"class":88},[54,829,830],{"class":544}," \"",[54,832,833],{"class":548},"Ben",[54,835,545],{"class":544},[54,837,103],{"class":88},[54,839,830],{"class":544},[54,841,842],{"class":548},"Cara",[54,844,545],{"class":544},[54,846,469],{"class":88},[54,848,849,852,854,856,858,860,862,864,866,869,871,874,877,880,883],{"class":56,"line":112},[54,850,851],{"class":64},"index ",[54,853,82],{"class":81},[54,855,85],{"class":64},[54,857,89],{"class":88},[54,859,93],{"class":92},[54,861,96],{"class":88},[54,863,752],{"class":99},[54,865,103],{"class":88},[54,867,868],{"class":115}," len",[54,870,96],{"class":88},[54,872,873],{"class":92},"names",[54,875,876],{"class":88},")",[54,878,879],{"class":81}," -",[54,881,882],{"class":99}," 1",[54,884,109],{"class":88},[54,886,888],{"class":56,"line":887},5,[54,889,72],{"emptyLinePlaceholder":71},[54,891,893,895,897,899,902,904,906,909],{"class":56,"line":892},6,[54,894,116],{"class":115},[54,896,96],{"class":88},[54,898,545],{"class":544},[54,900,901],{"class":548},"Index:",[54,903,545],{"class":544},[54,905,103],{"class":88},[54,907,908],{"class":92}," index",[54,910,109],{"class":88},[54,912,914,916,918,920,923,925,927,930,932,935],{"class":56,"line":913},7,[54,915,116],{"class":115},[54,917,96],{"class":88},[54,919,545],{"class":544},[54,921,922],{"class":548},"Chosen name:",[54,924,545],{"class":544},[54,926,103],{"class":88},[54,928,929],{"class":92}," names",[54,931,459],{"class":88},[54,933,934],{"class":92},"index",[54,936,937],{"class":88},"])\n",[14,939,561],{},[45,941,943],{"className":47,"code":942,"language":49,"meta":50,"style":50},"Index: 2\nChosen name: Cara\n",[17,944,945,955],{"__ignoreMap":50},[54,946,947,950,952],{"class":56,"line":57},[54,948,949],{"class":64},"Index",[54,951,147],{"class":88},[54,953,954],{"class":99}," 2\n",[54,956,957,960,962],{"class":56,"line":68},[54,958,959],{"class":64},"Chosen name",[54,961,147],{"class":88},[54,963,964],{"class":64}," Cara\n",[14,966,967,968,974],{},"If you want a random item from a list, ",[192,969,971],{"href":970},"\u002Fstandard-library\u002Frandom.choice-function-explained",[17,972,973],{},"random.choice()"," is often simpler than generating an index yourself.",[40,976,978],{"id":977},"common-mistakes","Common mistakes",[602,980,982],{"id":981},"forgetting-to-import-the-random-module","Forgetting to import the random module",[14,984,985,986,89],{},"This causes a ",[17,987,988],{},"NameError",[14,990,991],{},"Wrong:",[45,993,995],{"className":47,"code":994,"language":49,"meta":50,"style":50},"number = random.randint(1, 10)\nprint(number)\n",[17,996,997,1019],{"__ignoreMap":50},[54,998,999,1001,1003,1005,1007,1009,1011,1013,1015,1017],{"class":56,"line":57},[54,1000,78],{"class":64},[54,1002,82],{"class":81},[54,1004,85],{"class":64},[54,1006,89],{"class":88},[54,1008,93],{"class":92},[54,1010,96],{"class":88},[54,1012,100],{"class":99},[54,1014,103],{"class":88},[54,1016,106],{"class":99},[54,1018,109],{"class":88},[54,1020,1021,1023,1025,1027],{"class":56,"line":68},[54,1022,116],{"class":115},[54,1024,96],{"class":88},[54,1026,121],{"class":92},[54,1028,109],{"class":88},[14,1030,1031],{},"Fix:",[45,1033,1034],{"className":47,"code":48,"language":49,"meta":50,"style":50},[17,1035,1036,1042,1046,1068],{"__ignoreMap":50},[54,1037,1038,1040],{"class":56,"line":57},[54,1039,61],{"class":60},[54,1041,65],{"class":64},[54,1043,1044],{"class":56,"line":68},[54,1045,72],{"emptyLinePlaceholder":71},[54,1047,1048,1050,1052,1054,1056,1058,1060,1062,1064,1066],{"class":56,"line":75},[54,1049,78],{"class":64},[54,1051,82],{"class":81},[54,1053,85],{"class":64},[54,1055,89],{"class":88},[54,1057,93],{"class":92},[54,1059,96],{"class":88},[54,1061,100],{"class":99},[54,1063,103],{"class":88},[54,1065,106],{"class":99},[54,1067,109],{"class":88},[54,1069,1070,1072,1074,1076],{"class":56,"line":112},[54,1071,116],{"class":115},[54,1073,96],{"class":88},[54,1075,121],{"class":92},[54,1077,109],{"class":88},[14,1079,1080,1081,89],{},"If you run into this, see ",[192,1082,1084,1085],{"href":1083},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix","how to fix ",[17,1086,1087],{},"NameError: name is not defined",[602,1089,1091],{"id":1090},"assuming-the-end-value-is-excluded","Assuming the end value is excluded",[14,1093,1094],{},"Some beginners think this:",[45,1096,1098],{"className":47,"code":1097,"language":49,"meta":50,"style":50},"random.randint(1, 10)\n",[17,1099,1100],{"__ignoreMap":50},[54,1101,1102,1104,1106,1108,1110,1112,1114,1116],{"class":56,"line":57},[54,1103,26],{"class":64},[54,1105,89],{"class":88},[54,1107,93],{"class":92},[54,1109,96],{"class":88},[54,1111,100],{"class":99},[54,1113,103],{"class":88},[54,1115,106],{"class":99},[54,1117,109],{"class":88},[14,1119,1120,1121,248,1123,89],{},"returns numbers from ",[17,1122,100],{},[17,1124,1125],{},"9",[14,1127,1128,1129,1132],{},"That is incorrect. It can return ",[17,1130,1131],{},"10"," too.",[602,1134,1136],{"id":1135},"passing-non-integer-values","Passing non-integer values",[14,1138,1139,1141],{},[17,1140,792],{}," is meant for integers.",[14,1143,1144],{},"Problem example:",[45,1146,1148],{"className":47,"code":1147,"language":49,"meta":50,"style":50},"import random\n\nnumber = random.randint(1.5, 5.5)\nprint(number)\n",[17,1149,1150,1156,1160,1184],{"__ignoreMap":50},[54,1151,1152,1154],{"class":56,"line":57},[54,1153,61],{"class":60},[54,1155,65],{"class":64},[54,1157,1158],{"class":56,"line":68},[54,1159,72],{"emptyLinePlaceholder":71},[54,1161,1162,1164,1166,1168,1170,1172,1174,1177,1179,1182],{"class":56,"line":75},[54,1163,78],{"class":64},[54,1165,82],{"class":81},[54,1167,85],{"class":64},[54,1169,89],{"class":88},[54,1171,93],{"class":92},[54,1173,96],{"class":88},[54,1175,1176],{"class":99},"1.5",[54,1178,103],{"class":88},[54,1180,1181],{"class":99}," 5.5",[54,1183,109],{"class":88},[54,1185,1186,1188,1190,1192],{"class":56,"line":112},[54,1187,116],{"class":115},[54,1189,96],{"class":88},[54,1191,121],{"class":92},[54,1193,109],{"class":88},[14,1195,1196],{},"This can raise an error because the arguments should be whole numbers.",[14,1198,1199],{},"Use integers instead:",[45,1201,1203],{"className":47,"code":1202,"language":49,"meta":50,"style":50},"import random\n\nnumber = random.randint(1, 5)\nprint(number)\n",[17,1204,1205,1211,1215,1238],{"__ignoreMap":50},[54,1206,1207,1209],{"class":56,"line":57},[54,1208,61],{"class":60},[54,1210,65],{"class":64},[54,1212,1213],{"class":56,"line":68},[54,1214,72],{"emptyLinePlaceholder":71},[54,1216,1217,1219,1221,1223,1225,1227,1229,1231,1233,1236],{"class":56,"line":75},[54,1218,78],{"class":64},[54,1220,82],{"class":81},[54,1222,85],{"class":64},[54,1224,89],{"class":88},[54,1226,93],{"class":92},[54,1228,96],{"class":88},[54,1230,100],{"class":99},[54,1232,103],{"class":88},[54,1234,1235],{"class":99}," 5",[54,1237,109],{"class":88},[54,1239,1240,1242,1244,1246],{"class":56,"line":112},[54,1241,116],{"class":115},[54,1243,96],{"class":88},[54,1245,121],{"class":92},[54,1247,109],{"class":88},[602,1249,1251],{"id":1250},"using-a-start-value-larger-than-the-end-value","Using a start value larger than the end value",[14,1253,1144],{},[45,1255,1257],{"className":47,"code":1256,"language":49,"meta":50,"style":50},"import random\n\nnumber = random.randint(10, 1)\nprint(number)\n",[17,1258,1259,1265,1269,1291],{"__ignoreMap":50},[54,1260,1261,1263],{"class":56,"line":57},[54,1262,61],{"class":60},[54,1264,65],{"class":64},[54,1266,1267],{"class":56,"line":68},[54,1268,72],{"emptyLinePlaceholder":71},[54,1270,1271,1273,1275,1277,1279,1281,1283,1285,1287,1289],{"class":56,"line":75},[54,1272,78],{"class":64},[54,1274,82],{"class":81},[54,1276,85],{"class":64},[54,1278,89],{"class":88},[54,1280,93],{"class":92},[54,1282,96],{"class":88},[54,1284,1131],{"class":99},[54,1286,103],{"class":88},[54,1288,882],{"class":99},[54,1290,109],{"class":88},[54,1292,1293,1295,1297,1299],{"class":56,"line":112},[54,1294,116],{"class":115},[54,1296,96],{"class":88},[54,1298,121],{"class":92},[54,1300,109],{"class":88},[14,1302,1303,1304,1307],{},"This can raise a ",[17,1305,1306],{},"ValueError"," because the lower value must come first.",[14,1309,1310],{},"Correct version:",[45,1312,1313],{"className":47,"code":48,"language":49,"meta":50,"style":50},[17,1314,1315,1321,1325,1347],{"__ignoreMap":50},[54,1316,1317,1319],{"class":56,"line":57},[54,1318,61],{"class":60},[54,1320,65],{"class":64},[54,1322,1323],{"class":56,"line":68},[54,1324,72],{"emptyLinePlaceholder":71},[54,1326,1327,1329,1331,1333,1335,1337,1339,1341,1343,1345],{"class":56,"line":75},[54,1328,78],{"class":64},[54,1330,82],{"class":81},[54,1332,85],{"class":64},[54,1334,89],{"class":88},[54,1336,93],{"class":92},[54,1338,96],{"class":88},[54,1340,100],{"class":99},[54,1342,103],{"class":88},[54,1344,106],{"class":99},[54,1346,109],{"class":88},[54,1348,1349,1351,1353,1355],{"class":56,"line":112},[54,1350,116],{"class":115},[54,1352,96],{"class":88},[54,1354,121],{"class":92},[54,1356,109],{"class":88},[40,1358,1360],{"id":1359},"when-to-use-randint-vs-other-random-functions","When to use randint() vs other random functions",[14,1362,126,1363,1365,1366,1369],{},[17,1364,19],{}," when you need a random ",[35,1367,1368],{},"whole number"," in an inclusive range.",[14,1371,1372],{},"Choose other functions when the task is different:",[149,1374,1375,1384,1391,1401],{},[152,1376,126,1377,1379,1380,248,1382],{},[17,1378,792],{}," for a random integer like ",[17,1381,100],{},[17,1383,323],{},[152,1385,126,1386,1390],{},[192,1387,1388],{"href":970},[17,1389,973],{}," for a random item from a list",[152,1392,126,1393,1396,1397],{},[17,1394,1395],{},"random.randrange()"," when you want behavior more like ",[192,1398,1399],{"href":407},[17,1400,410],{},[152,1402,1403,1404,1406],{},"Do not use ",[17,1405,792],{}," for passwords, tokens, or other security-sensitive code",[14,1408,1409,1410,1413,1414,89],{},"For security-related randomness, use the ",[17,1411,1412],{},"secrets"," module instead of ",[17,1415,26],{},[40,1417,1419],{"id":1418},"faq","FAQ",[602,1421,1423],{"id":1422},"does-randomrandint-include-the-last-number","Does random.randint() include the last number?",[14,1425,1426],{},"Yes. Both the start and end numbers are included.",[602,1428,1430],{"id":1429},"can-randomrandint-return-negative-numbers","Can random.randint() return negative numbers?",[14,1432,1433],{},"Yes. It can return any integer in the given inclusive range, including negative values.",[14,1435,253],{},[45,1437,1439],{"className":47,"code":1438,"language":49,"meta":50,"style":50},"import random\n\nprint(random.randint(-5, 5))\n",[17,1440,1441,1447,1451],{"__ignoreMap":50},[54,1442,1443,1445],{"class":56,"line":57},[54,1444,61],{"class":60},[54,1446,65],{"class":64},[54,1448,1449],{"class":56,"line":68},[54,1450,72],{"emptyLinePlaceholder":71},[54,1452,1453,1455,1457,1459,1461,1463,1465,1468,1470,1472,1474],{"class":56,"line":75},[54,1454,116],{"class":115},[54,1456,96],{"class":88},[54,1458,26],{"class":92},[54,1460,89],{"class":88},[54,1462,93],{"class":92},[54,1464,96],{"class":88},[54,1466,1467],{"class":81},"-",[54,1469,285],{"class":99},[54,1471,103],{"class":88},[54,1473,1235],{"class":99},[54,1475,382],{"class":88},[602,1477,1479],{"id":1478},"what-happens-if-i-use-decimal-numbers-with-randint","What happens if I use decimal numbers with randint()?",[14,1481,1482,1484],{},[17,1483,792],{}," is for integers. Non-integer values can cause an error.",[602,1486,1488],{"id":1487},"do-i-need-to-import-random-first","Do I need to import random first?",[14,1490,1491,1492,1494,1495,89],{},"Yes. Use ",[17,1493,166],{}," before calling ",[17,1496,19],{},[602,1498,1500],{"id":1499},"should-i-use-randomrandint-for-passwords-or-security-tokens","Should I use random.randint() for passwords or security tokens?",[14,1502,1503,1504,1506],{},"No. For security-related randomness, use the ",[17,1505,1412],{}," module instead.",[40,1508,1510],{"id":1509},"see-also","See also",[149,1512,1513,1517,1522,1527,1531,1537],{},[152,1514,1515],{},[192,1516,195],{"href":194},[152,1518,1519],{},[192,1520,1521],{"href":970},"random.choice() function explained",[152,1523,1524],{},[192,1525,1526],{"href":407},"Python range() function explained",[152,1528,1529],{},[192,1530,715],{"href":714},[152,1532,1533],{},[192,1534,1536],{"href":1535},"\u002Fexamples\u002Fpython-random-name-picker-example","Python random name picker example",[152,1538,1539],{},[192,1540,1541,1542],{"href":1083},"How to fix ",[17,1543,1087],{},[1545,1546,1547],"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":50,"searchDepth":68,"depth":68,"links":1549},[1550,1551,1552,1553,1554,1555,1561,1567,1568,1575],{"id":42,"depth":68,"text":43},{"id":141,"depth":68,"text":142},{"id":198,"depth":68,"text":199},{"id":330,"depth":68,"text":331},{"id":492,"depth":68,"text":493},{"id":599,"depth":68,"text":600,"children":1556},[1557,1558,1559,1560],{"id":604,"depth":75,"text":605},{"id":657,"depth":75,"text":658},{"id":718,"depth":75,"text":719},{"id":785,"depth":75,"text":786},{"id":977,"depth":68,"text":978,"children":1562},[1563,1564,1565,1566],{"id":981,"depth":75,"text":982},{"id":1090,"depth":75,"text":1091},{"id":1135,"depth":75,"text":1136},{"id":1250,"depth":75,"text":1251},{"id":1359,"depth":68,"text":1360},{"id":1418,"depth":68,"text":1419,"children":1569},[1570,1571,1572,1573,1574],{"id":1422,"depth":75,"text":1423},{"id":1429,"depth":75,"text":1430},{"id":1478,"depth":75,"text":1479},{"id":1487,"depth":75,"text":1488},{"id":1499,"depth":75,"text":1500},{"id":1509,"depth":68,"text":1510},"Master random.randint function explained in our comprehensive Python beginner guide.","md",{},"\u002Fstandard-library\u002Frandom.randint-function-explained",{"title":5,"description":1576},"standard-library\u002Frandom.randint-function-explained","yMcPIVi-8loKqZzeVQtm_2k3jxbnxNHwjZN-tJX8coQ",[1584,3539,6106,8178,11299,13570,15728,18253,20478,22273,23657,25120,26176,27934,29458,31003,33500,36054,37553,38948,40946,42321,44120,46261,47532,49461,51080,53744,55172,56409],{"id":1585,"title":1586,"body":1587,"description":3533,"extension":1577,"meta":3534,"navigation":71,"path":3535,"seo":3536,"stem":3537,"__hash__":3538},"content\u002Fstandard-library\u002Fdatetime.now-explained.md","datetime.now() Explained",{"type":7,"value":1588,"toc":3493},[1589,1596,1601,1604,1618,1631,1727,1733,1740,1744,1765,1768,1775,1782,1785,1800,1803,1818,1822,1865,1868,1911,1914,1918,1921,1951,1961,1964,1997,2004,2008,2014,2034,2037,2040,2164,2167,2215,2218,2244,2255,2259,2262,2365,2367,2428,2431,2435,2438,2441,2455,2458,2562,2564,2635,2638,2642,2649,2655,2659,2719,2721,2741,2745,2801,2803,2822,2825,2842,2846,2854,2857,2860,2871,2876,2880,2885,2889,2892,2920,2923,2955,2958,2991,2995,2998,3031,3038,3041,3073,3077,3083,3129,3132,3207,3211,3214,3225,3228,3242,3245,3256,3260,3263,3346,3349,3385,3387,3394,3400,3410,3418,3422,3431,3438,3446,3453,3456,3458,3490],[10,1590,1592,1595],{"id":1591},"datetimenow-explained",[17,1593,1594],{},"datetime.now()"," Explained",[14,1597,1598,1600],{},[17,1599,1594],{}," gives you the current local date and time in Python.",[14,1602,1603],{},"Beginners often use it when they want to:",[149,1605,1606,1609,1612,1615],{},[152,1607,1608],{},"record the current time",[152,1610,1611],{},"print today’s date",[152,1613,1614],{},"create timestamps",[152,1616,1617],{},"work with dates and times in programs",[14,1619,1620,1621,1623,1624,1630],{},"It is important to know that ",[17,1622,1594],{}," returns a ",[35,1625,1626,1629],{},[17,1627,1628],{},"datetime"," object",", not a plain string. That means you can access parts of it like the year, month, day, or hour.",[45,1632,1634],{"className":47,"code":1633,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(now)\nprint(now.year)\nprint(now.month)\nprint(now.day)\n",[17,1635,1636,1649,1653,1671,1681,1697,1712],{"__ignoreMap":50},[54,1637,1638,1641,1644,1646],{"class":56,"line":57},[54,1639,1640],{"class":60},"from",[54,1642,1643],{"class":64}," datetime ",[54,1645,61],{"class":60},[54,1647,1648],{"class":64}," datetime\n",[54,1650,1651],{"class":56,"line":68},[54,1652,72],{"emptyLinePlaceholder":71},[54,1654,1655,1658,1660,1663,1665,1668],{"class":56,"line":75},[54,1656,1657],{"class":64},"now ",[54,1659,82],{"class":81},[54,1661,1662],{"class":64}," datetime",[54,1664,89],{"class":88},[54,1666,1667],{"class":92},"now",[54,1669,1670],{"class":88},"()\n",[54,1672,1673,1675,1677,1679],{"class":56,"line":112},[54,1674,116],{"class":115},[54,1676,96],{"class":88},[54,1678,1667],{"class":92},[54,1680,109],{"class":88},[54,1682,1683,1685,1687,1689,1691,1695],{"class":56,"line":887},[54,1684,116],{"class":115},[54,1686,96],{"class":88},[54,1688,1667],{"class":92},[54,1690,89],{"class":88},[54,1692,1694],{"class":1693},"skxfh","year",[54,1696,109],{"class":88},[54,1698,1699,1701,1703,1705,1707,1710],{"class":56,"line":892},[54,1700,116],{"class":115},[54,1702,96],{"class":88},[54,1704,1667],{"class":92},[54,1706,89],{"class":88},[54,1708,1709],{"class":1693},"month",[54,1711,109],{"class":88},[54,1713,1714,1716,1718,1720,1722,1725],{"class":56,"line":913},[54,1715,116],{"class":115},[54,1717,96],{"class":88},[54,1719,1667],{"class":92},[54,1721,89],{"class":88},[54,1723,1724],{"class":1693},"day",[54,1726,109],{"class":88},[14,1728,1729,1730,1732],{},"Use this when you want the current local date and time as a ",[17,1731,1628],{}," object.",[40,1734,1736,1737,1739],{"id":1735},"what-datetimenow-does","What ",[17,1738,1594],{}," does",[14,1741,1742,147],{},[17,1743,1594],{},[149,1745,1746,1749,1757,1762],{},[152,1747,1748],{},"returns the current local date and time",[152,1750,1751,1752,1754,1755,160],{},"belongs to the ",[17,1753,1628],{}," class in the ",[17,1756,1628],{},[152,1758,1759,1760,1629],{},"returns a ",[17,1761,1628],{},[152,1763,1764],{},"is useful when you need the current timestamp in your program",[14,1766,1767],{},"A common beginner mistake is thinking it returns text. It does not. It returns an object with useful attributes and methods.",[14,1769,1770,1771,89],{},"If you want a broader introduction to working with dates and times, see the ",[192,1772,1774],{"href":1773},"\u002Fstandard-library\u002Fpython-datetime-module-overview","Python datetime module overview",[40,1776,1778,1779,1781],{"id":1777},"importing-datetimenow-correctly","Importing ",[17,1780,1594],{}," correctly",[14,1783,1784],{},"The most common beginner import is:",[45,1786,1788],{"className":47,"code":1787,"language":49,"meta":50,"style":50},"from datetime import datetime\n",[17,1789,1790],{"__ignoreMap":50},[54,1791,1792,1794,1796,1798],{"class":56,"line":57},[54,1793,1640],{"class":60},[54,1795,1643],{"class":64},[54,1797,61],{"class":60},[54,1799,1648],{"class":64},[14,1801,1802],{},"Then you call:",[45,1804,1806],{"className":47,"code":1805,"language":49,"meta":50,"style":50},"datetime.now()\n",[17,1807,1808],{"__ignoreMap":50},[54,1809,1810,1812,1814,1816],{"class":56,"line":57},[54,1811,1628],{"class":64},[54,1813,89],{"class":88},[54,1815,1667],{"class":92},[54,1817,1670],{"class":88},[602,1819,1821],{"id":1820},"example","Example",[45,1823,1825],{"className":47,"code":1824,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(now)\n",[17,1826,1827,1837,1841,1855],{"__ignoreMap":50},[54,1828,1829,1831,1833,1835],{"class":56,"line":57},[54,1830,1640],{"class":60},[54,1832,1643],{"class":64},[54,1834,61],{"class":60},[54,1836,1648],{"class":64},[54,1838,1839],{"class":56,"line":68},[54,1840,72],{"emptyLinePlaceholder":71},[54,1842,1843,1845,1847,1849,1851,1853],{"class":56,"line":75},[54,1844,1657],{"class":64},[54,1846,82],{"class":81},[54,1848,1662],{"class":64},[54,1850,89],{"class":88},[54,1852,1667],{"class":92},[54,1854,1670],{"class":88},[54,1856,1857,1859,1861,1863],{"class":56,"line":112},[54,1858,116],{"class":115},[54,1860,96],{"class":88},[54,1862,1667],{"class":92},[54,1864,109],{"class":88},[14,1866,1867],{},"You can also import the whole module:",[45,1869,1871],{"className":47,"code":1870,"language":49,"meta":50,"style":50},"import datetime\n\nnow = datetime.datetime.now()\nprint(now)\n",[17,1872,1873,1879,1883,1901],{"__ignoreMap":50},[54,1874,1875,1877],{"class":56,"line":57},[54,1876,61],{"class":60},[54,1878,1648],{"class":64},[54,1880,1881],{"class":56,"line":68},[54,1882,72],{"emptyLinePlaceholder":71},[54,1884,1885,1887,1889,1891,1893,1895,1897,1899],{"class":56,"line":75},[54,1886,1657],{"class":64},[54,1888,82],{"class":81},[54,1890,1662],{"class":64},[54,1892,89],{"class":88},[54,1894,1628],{"class":1693},[54,1896,89],{"class":88},[54,1898,1667],{"class":92},[54,1900,1670],{"class":88},[54,1902,1903,1905,1907,1909],{"class":56,"line":112},[54,1904,116],{"class":115},[54,1906,96],{"class":88},[54,1908,1667],{"class":92},[54,1910,109],{"class":88},[14,1912,1913],{},"This version works too, but it is longer.",[602,1915,1917],{"id":1916},"why-beginners-get-confused","Why beginners get confused",[14,1919,1920],{},"If you write this:",[45,1922,1924],{"className":47,"code":1923,"language":49,"meta":50,"style":50},"import datetime\n\nprint(datetime.now())\n",[17,1925,1926,1932,1936],{"__ignoreMap":50},[54,1927,1928,1930],{"class":56,"line":57},[54,1929,61],{"class":60},[54,1931,1648],{"class":64},[54,1933,1934],{"class":56,"line":68},[54,1935,72],{"emptyLinePlaceholder":71},[54,1937,1938,1940,1942,1944,1946,1948],{"class":56,"line":75},[54,1939,116],{"class":115},[54,1941,96],{"class":88},[54,1943,1628],{"class":92},[54,1945,89],{"class":88},[54,1947,1667],{"class":92},[54,1949,1950],{"class":88},"())\n",[14,1952,1953,1954,1956,1957,1960],{},"you will get an error, because ",[17,1955,1628],{}," here is the ",[35,1958,1959],{},"module",", not the class.",[14,1962,1963],{},"You need:",[45,1965,1967],{"className":47,"code":1966,"language":49,"meta":50,"style":50},"import datetime\n\nprint(datetime.datetime.now())\n",[17,1968,1969,1975,1979],{"__ignoreMap":50},[54,1970,1971,1973],{"class":56,"line":57},[54,1972,61],{"class":60},[54,1974,1648],{"class":64},[54,1976,1977],{"class":56,"line":68},[54,1978,72],{"emptyLinePlaceholder":71},[54,1980,1981,1983,1985,1987,1989,1991,1993,1995],{"class":56,"line":75},[54,1982,116],{"class":115},[54,1984,96],{"class":88},[54,1986,1628],{"class":92},[54,1988,89],{"class":88},[54,1990,1628],{"class":1693},[54,1992,89],{"class":88},[54,1994,1667],{"class":92},[54,1996,1950],{"class":88},[14,1998,1999,2000,89],{},"If you run into this kind of problem, see ",[192,2001,2003],{"href":2002},"\u002Ferrors\u002Fattributeerror-module-has-no-attribute-fix","AttributeError: module has no attribute fix",[40,2005,2007],{"id":2006},"what-the-returned-object-contains","What the returned object contains",[14,2009,2010,2011,2013],{},"The object returned by ",[17,2012,1594],{}," includes:",[149,2015,2016,2018,2020,2022,2025,2028,2031],{},[152,2017,1694],{},[152,2019,1709],{},[152,2021,1724],{},[152,2023,2024],{},"hour",[152,2026,2027],{},"minute",[152,2029,2030],{},"second",[152,2032,2033],{},"microsecond",[14,2035,2036],{},"You can access these parts with attributes.",[602,2038,1821],{"id":2039},"example-1",[45,2041,2043],{"className":47,"code":2042,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\n\nprint(now.year)\nprint(now.month)\nprint(now.day)\nprint(now.hour)\nprint(now.minute)\nprint(now.second)\n",[17,2044,2045,2055,2059,2073,2077,2091,2105,2119,2134,2149],{"__ignoreMap":50},[54,2046,2047,2049,2051,2053],{"class":56,"line":57},[54,2048,1640],{"class":60},[54,2050,1643],{"class":64},[54,2052,61],{"class":60},[54,2054,1648],{"class":64},[54,2056,2057],{"class":56,"line":68},[54,2058,72],{"emptyLinePlaceholder":71},[54,2060,2061,2063,2065,2067,2069,2071],{"class":56,"line":75},[54,2062,1657],{"class":64},[54,2064,82],{"class":81},[54,2066,1662],{"class":64},[54,2068,89],{"class":88},[54,2070,1667],{"class":92},[54,2072,1670],{"class":88},[54,2074,2075],{"class":56,"line":112},[54,2076,72],{"emptyLinePlaceholder":71},[54,2078,2079,2081,2083,2085,2087,2089],{"class":56,"line":887},[54,2080,116],{"class":115},[54,2082,96],{"class":88},[54,2084,1667],{"class":92},[54,2086,89],{"class":88},[54,2088,1694],{"class":1693},[54,2090,109],{"class":88},[54,2092,2093,2095,2097,2099,2101,2103],{"class":56,"line":892},[54,2094,116],{"class":115},[54,2096,96],{"class":88},[54,2098,1667],{"class":92},[54,2100,89],{"class":88},[54,2102,1709],{"class":1693},[54,2104,109],{"class":88},[54,2106,2107,2109,2111,2113,2115,2117],{"class":56,"line":913},[54,2108,116],{"class":115},[54,2110,96],{"class":88},[54,2112,1667],{"class":92},[54,2114,89],{"class":88},[54,2116,1724],{"class":1693},[54,2118,109],{"class":88},[54,2120,2122,2124,2126,2128,2130,2132],{"class":56,"line":2121},8,[54,2123,116],{"class":115},[54,2125,96],{"class":88},[54,2127,1667],{"class":92},[54,2129,89],{"class":88},[54,2131,2024],{"class":1693},[54,2133,109],{"class":88},[54,2135,2137,2139,2141,2143,2145,2147],{"class":56,"line":2136},9,[54,2138,116],{"class":115},[54,2140,96],{"class":88},[54,2142,1667],{"class":92},[54,2144,89],{"class":88},[54,2146,2027],{"class":1693},[54,2148,109],{"class":88},[54,2150,2152,2154,2156,2158,2160,2162],{"class":56,"line":2151},10,[54,2153,116],{"class":115},[54,2155,96],{"class":88},[54,2157,1667],{"class":92},[54,2159,89],{"class":88},[54,2161,2030],{"class":1693},[54,2163,109],{"class":88},[14,2165,2166],{},"You can also check its type:",[45,2168,2170],{"className":47,"code":2169,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(type(now))\n",[17,2171,2172,2182,2186,2200],{"__ignoreMap":50},[54,2173,2174,2176,2178,2180],{"class":56,"line":57},[54,2175,1640],{"class":60},[54,2177,1643],{"class":64},[54,2179,61],{"class":60},[54,2181,1648],{"class":64},[54,2183,2184],{"class":56,"line":68},[54,2185,72],{"emptyLinePlaceholder":71},[54,2187,2188,2190,2192,2194,2196,2198],{"class":56,"line":75},[54,2189,1657],{"class":64},[54,2191,82],{"class":81},[54,2193,1662],{"class":64},[54,2195,89],{"class":88},[54,2197,1667],{"class":92},[54,2199,1670],{"class":88},[54,2201,2202,2204,2206,2209,2211,2213],{"class":56,"line":112},[54,2203,116],{"class":115},[54,2205,96],{"class":88},[54,2207,2208],{"class":429},"type",[54,2210,96],{"class":88},[54,2212,1667],{"class":92},[54,2214,382],{"class":88},[14,2216,2217],{},"Expected output will look like:",[45,2219,2221],{"className":47,"code":2220,"language":49,"meta":50,"style":50},"\u003Cclass 'datetime.datetime'>\n",[17,2222,2223],{"__ignoreMap":50},[54,2224,2225,2228,2232,2235,2238,2241],{"class":56,"line":57},[54,2226,2227],{"class":81},"\u003C",[54,2229,2231],{"class":2230},"sbsja","class",[54,2233,2234],{"class":544}," '",[54,2236,2237],{"class":548},"datetime.datetime",[54,2239,2240],{"class":544},"'",[54,2242,2243],{"class":81},">\n",[14,2245,2246,2247,2250,2251,89],{},"If you want to understand ",[17,2248,2249],{},"type()"," better, see ",[192,2252,2254],{"href":2253},"\u002Freference\u002Fpython-type-function-explained","Python type() function explained",[40,2256,2258],{"id":2257},"basic-example","Basic example",[14,2260,2261],{},"Here is a simple example that gets the current date and time, prints it, and accesses one part of it.",[45,2263,2265],{"className":47,"code":2264,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\n\nprint(\"Full value:\", now)\nprint(\"Year:\", now.year)\nprint(\"Hour:\", now.hour)\n",[17,2266,2267,2277,2281,2295,2299,2319,2342],{"__ignoreMap":50},[54,2268,2269,2271,2273,2275],{"class":56,"line":57},[54,2270,1640],{"class":60},[54,2272,1643],{"class":64},[54,2274,61],{"class":60},[54,2276,1648],{"class":64},[54,2278,2279],{"class":56,"line":68},[54,2280,72],{"emptyLinePlaceholder":71},[54,2282,2283,2285,2287,2289,2291,2293],{"class":56,"line":75},[54,2284,1657],{"class":64},[54,2286,82],{"class":81},[54,2288,1662],{"class":64},[54,2290,89],{"class":88},[54,2292,1667],{"class":92},[54,2294,1670],{"class":88},[54,2296,2297],{"class":56,"line":112},[54,2298,72],{"emptyLinePlaceholder":71},[54,2300,2301,2303,2305,2307,2310,2312,2314,2317],{"class":56,"line":887},[54,2302,116],{"class":115},[54,2304,96],{"class":88},[54,2306,545],{"class":544},[54,2308,2309],{"class":548},"Full value:",[54,2311,545],{"class":544},[54,2313,103],{"class":88},[54,2315,2316],{"class":92}," now",[54,2318,109],{"class":88},[54,2320,2321,2323,2325,2327,2330,2332,2334,2336,2338,2340],{"class":56,"line":892},[54,2322,116],{"class":115},[54,2324,96],{"class":88},[54,2326,545],{"class":544},[54,2328,2329],{"class":548},"Year:",[54,2331,545],{"class":544},[54,2333,103],{"class":88},[54,2335,2316],{"class":92},[54,2337,89],{"class":88},[54,2339,1694],{"class":1693},[54,2341,109],{"class":88},[54,2343,2344,2346,2348,2350,2353,2355,2357,2359,2361,2363],{"class":56,"line":913},[54,2345,116],{"class":115},[54,2347,96],{"class":88},[54,2349,545],{"class":544},[54,2351,2352],{"class":548},"Hour:",[54,2354,545],{"class":544},[54,2356,103],{"class":88},[54,2358,2316],{"class":92},[54,2360,89],{"class":88},[54,2362,2024],{"class":1693},[54,2364,109],{"class":88},[14,2366,561],{},[45,2368,2370],{"className":47,"code":2369,"language":49,"meta":50,"style":50},"Full value: 2026-04-22 14:35:10.123456\nYear: 2026\nHour: 14\n",[17,2371,2372,2408,2418],{"__ignoreMap":50},[54,2373,2374,2377,2379,2382,2384,2386,2390,2392,2395,2398,2400,2403,2405],{"class":56,"line":57},[54,2375,2376],{"class":64},"Full value",[54,2378,147],{"class":88},[54,2380,2381],{"class":99}," 2026",[54,2383,1467],{"class":81},[54,2385,752],{"class":99},[54,2387,2389],{"class":2388},"sx12J","4",[54,2391,1467],{"class":81},[54,2393,2394],{"class":99},"22",[54,2396,2397],{"class":99}," 14",[54,2399,147],{"class":88},[54,2401,2402],{"class":99},"35",[54,2404,147],{"class":88},[54,2406,2407],{"class":99},"10.123456\n",[54,2409,2410,2413,2415],{"class":56,"line":68},[54,2411,2412],{"class":64},"Year",[54,2414,147],{"class":88},[54,2416,2417],{"class":99}," 2026\n",[54,2419,2420,2423,2425],{"class":56,"line":75},[54,2421,2422],{"class":64},"Hour",[54,2424,147],{"class":88},[54,2426,2427],{"class":99}," 14\n",[14,2429,2430],{},"The exact output will be different on your computer because it uses your current local time.",[40,2432,2434],{"id":2433},"getting-only-the-date-or-only-the-time","Getting only the date or only the time",[14,2436,2437],{},"Sometimes the full datetime value is more than you need.",[14,2439,2440],{},"You can use:",[149,2442,2443,2449],{},[152,2444,2445,2448],{},[17,2446,2447],{},".date()"," to get only the date",[152,2450,2451,2454],{},[17,2452,2453],{},".time()"," to get only the time",[602,2456,1821],{"id":2457},"example-2",[45,2459,2461],{"className":47,"code":2460,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\n\nprint(\"Full datetime:\", now)\nprint(\"Date only:\", now.date())\nprint(\"Time only:\", now.time())\n",[17,2462,2463,2473,2477,2491,2495,2514,2538],{"__ignoreMap":50},[54,2464,2465,2467,2469,2471],{"class":56,"line":57},[54,2466,1640],{"class":60},[54,2468,1643],{"class":64},[54,2470,61],{"class":60},[54,2472,1648],{"class":64},[54,2474,2475],{"class":56,"line":68},[54,2476,72],{"emptyLinePlaceholder":71},[54,2478,2479,2481,2483,2485,2487,2489],{"class":56,"line":75},[54,2480,1657],{"class":64},[54,2482,82],{"class":81},[54,2484,1662],{"class":64},[54,2486,89],{"class":88},[54,2488,1667],{"class":92},[54,2490,1670],{"class":88},[54,2492,2493],{"class":56,"line":112},[54,2494,72],{"emptyLinePlaceholder":71},[54,2496,2497,2499,2501,2503,2506,2508,2510,2512],{"class":56,"line":887},[54,2498,116],{"class":115},[54,2500,96],{"class":88},[54,2502,545],{"class":544},[54,2504,2505],{"class":548},"Full datetime:",[54,2507,545],{"class":544},[54,2509,103],{"class":88},[54,2511,2316],{"class":92},[54,2513,109],{"class":88},[54,2515,2516,2518,2520,2522,2525,2527,2529,2531,2533,2536],{"class":56,"line":892},[54,2517,116],{"class":115},[54,2519,96],{"class":88},[54,2521,545],{"class":544},[54,2523,2524],{"class":548},"Date only:",[54,2526,545],{"class":544},[54,2528,103],{"class":88},[54,2530,2316],{"class":92},[54,2532,89],{"class":88},[54,2534,2535],{"class":92},"date",[54,2537,1950],{"class":88},[54,2539,2540,2542,2544,2546,2549,2551,2553,2555,2557,2560],{"class":56,"line":913},[54,2541,116],{"class":115},[54,2543,96],{"class":88},[54,2545,545],{"class":544},[54,2547,2548],{"class":548},"Time only:",[54,2550,545],{"class":544},[54,2552,103],{"class":88},[54,2554,2316],{"class":92},[54,2556,89],{"class":88},[54,2558,2559],{"class":92},"time",[54,2561,1950],{"class":88},[14,2563,561],{},[45,2565,2567],{"className":47,"code":2566,"language":49,"meta":50,"style":50},"Full datetime: 2026-04-22 14:35:10.123456\nDate only: 2026-04-22\nTime only: 14:35:10.123456\n",[17,2568,2569,2598,2618],{"__ignoreMap":50},[54,2570,2571,2574,2576,2578,2580,2582,2584,2586,2588,2590,2592,2594,2596],{"class":56,"line":57},[54,2572,2573],{"class":64},"Full datetime",[54,2575,147],{"class":88},[54,2577,2381],{"class":99},[54,2579,1467],{"class":81},[54,2581,752],{"class":99},[54,2583,2389],{"class":2388},[54,2585,1467],{"class":81},[54,2587,2394],{"class":99},[54,2589,2397],{"class":99},[54,2591,147],{"class":88},[54,2593,2402],{"class":99},[54,2595,147],{"class":88},[54,2597,2407],{"class":99},[54,2599,2600,2603,2605,2607,2609,2611,2613,2615],{"class":56,"line":68},[54,2601,2602],{"class":64},"Date only",[54,2604,147],{"class":88},[54,2606,2381],{"class":99},[54,2608,1467],{"class":81},[54,2610,752],{"class":99},[54,2612,2389],{"class":2388},[54,2614,1467],{"class":81},[54,2616,2617],{"class":99},"22\n",[54,2619,2620,2623,2625,2627,2629,2631,2633],{"class":56,"line":75},[54,2621,2622],{"class":64},"Time only",[54,2624,147],{"class":88},[54,2626,2397],{"class":99},[54,2628,147],{"class":88},[54,2630,2402],{"class":99},[54,2632,147],{"class":88},[54,2634,2407],{"class":99},[14,2636,2637],{},"This can make your code clearer when you only need one part.",[40,2639,2641],{"id":2640},"formatting-the-result-for-display","Formatting the result for display",[14,2643,2644,2645,2648],{},"If you use ",[17,2646,2647],{},"print(now)",", Python uses the default datetime format.",[14,2650,2651,2652,89],{},"If you want a custom format, use ",[17,2653,2654],{},"strftime()",[602,2656,2658],{"id":2657},"example-year-month-day","Example: year-month-day",[45,2660,2662],{"className":47,"code":2661,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(now.strftime(\"%Y-%m-%d\"))\n",[17,2663,2664,2674,2678,2692],{"__ignoreMap":50},[54,2665,2666,2668,2670,2672],{"class":56,"line":57},[54,2667,1640],{"class":60},[54,2669,1643],{"class":64},[54,2671,61],{"class":60},[54,2673,1648],{"class":64},[54,2675,2676],{"class":56,"line":68},[54,2677,72],{"emptyLinePlaceholder":71},[54,2679,2680,2682,2684,2686,2688,2690],{"class":56,"line":75},[54,2681,1657],{"class":64},[54,2683,82],{"class":81},[54,2685,1662],{"class":64},[54,2687,89],{"class":88},[54,2689,1667],{"class":92},[54,2691,1670],{"class":88},[54,2693,2694,2696,2698,2700,2702,2705,2707,2709,2712,2715,2717],{"class":56,"line":112},[54,2695,116],{"class":115},[54,2697,96],{"class":88},[54,2699,1667],{"class":92},[54,2701,89],{"class":88},[54,2703,2704],{"class":92},"strftime",[54,2706,96],{"class":88},[54,2708,545],{"class":544},[54,2710,2711],{"class":548},"%Y-%m-",[54,2713,2714],{"class":99},"%d",[54,2716,545],{"class":544},[54,2718,382],{"class":88},[14,2720,561],{},[45,2722,2724],{"className":47,"code":2723,"language":49,"meta":50,"style":50},"2026-04-22\n",[17,2725,2726],{"__ignoreMap":50},[54,2727,2728,2731,2733,2735,2737,2739],{"class":56,"line":57},[54,2729,2730],{"class":99},"2026",[54,2732,1467],{"class":81},[54,2734,752],{"class":99},[54,2736,2389],{"class":2388},[54,2738,1467],{"class":81},[54,2740,2617],{"class":99},[602,2742,2744],{"id":2743},"example-hourminutesecond","Example: hour:minute:second",[45,2746,2748],{"className":47,"code":2747,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(now.strftime(\"%H:%M:%S\"))\n",[17,2749,2750,2760,2764,2778],{"__ignoreMap":50},[54,2751,2752,2754,2756,2758],{"class":56,"line":57},[54,2753,1640],{"class":60},[54,2755,1643],{"class":64},[54,2757,61],{"class":60},[54,2759,1648],{"class":64},[54,2761,2762],{"class":56,"line":68},[54,2763,72],{"emptyLinePlaceholder":71},[54,2765,2766,2768,2770,2772,2774,2776],{"class":56,"line":75},[54,2767,1657],{"class":64},[54,2769,82],{"class":81},[54,2771,1662],{"class":64},[54,2773,89],{"class":88},[54,2775,1667],{"class":92},[54,2777,1670],{"class":88},[54,2779,2780,2782,2784,2786,2788,2790,2792,2794,2797,2799],{"class":56,"line":112},[54,2781,116],{"class":115},[54,2783,96],{"class":88},[54,2785,1667],{"class":92},[54,2787,89],{"class":88},[54,2789,2704],{"class":92},[54,2791,96],{"class":88},[54,2793,545],{"class":544},[54,2795,2796],{"class":548},"%H:%M:%S",[54,2798,545],{"class":544},[54,2800,382],{"class":88},[14,2802,561],{},[45,2804,2806],{"className":47,"code":2805,"language":49,"meta":50,"style":50},"14:35:10\n",[17,2807,2808],{"__ignoreMap":50},[54,2809,2810,2813,2815,2817,2819],{"class":56,"line":57},[54,2811,2812],{"class":99},"14",[54,2814,147],{"class":88},[54,2816,2402],{"class":99},[54,2818,147],{"class":88},[54,2820,2821],{"class":99},"10\n",[14,2823,2824],{},"This is useful when you want a cleaner, human-readable string.",[14,2826,2827,2828,2835,2836,89],{},"To learn more, see ",[192,2829,2831,2834],{"href":2830},"\u002Fstandard-library\u002Fdatetime.strftime-explained",[17,2832,2833],{},"datetime.strftime()"," explained",". If you later need to turn a string back into a datetime value, see ",[192,2837,2839,2834],{"href":2838},"\u002Fstandard-library\u002Fdatetime.strptime-explained",[17,2840,2841],{},"datetime.strptime()",[40,2843,2845],{"id":2844},"timezone-note-for-beginners","Timezone note for beginners",[14,2847,2848,2850,2851,89],{},[17,2849,1594],{}," gives ",[35,2852,2853],{},"local time by default",[14,2855,2856],{},"For beginner use, that is often enough.",[14,2858,2859],{},"A few important points:",[149,2861,2862,2865,2868],{},[152,2863,2864],{},"it uses the current local time on your system",[152,2866,2867],{},"basic use does not automatically include timezone information",[152,2869,2870],{},"two computers in different places may return different times",[14,2872,2873,2874,89],{},"If you want to learn more about the bigger picture, start with the ",[192,2875,1774],{"href":1773},[40,2877,2879],{"id":2878},"common-errors-and-confusion","Common errors and confusion",[14,2881,2882,2883,89],{},"Here are some common mistakes beginners make with ",[17,2884,1594],{},[602,2886,2888],{"id":2887},"using-the-wrong-import-style","Using the wrong import style",[14,2890,2891],{},"This fails:",[45,2893,2894],{"className":47,"code":1923,"language":49,"meta":50,"style":50},[17,2895,2896,2902,2906],{"__ignoreMap":50},[54,2897,2898,2900],{"class":56,"line":57},[54,2899,61],{"class":60},[54,2901,1648],{"class":64},[54,2903,2904],{"class":56,"line":68},[54,2905,72],{"emptyLinePlaceholder":71},[54,2907,2908,2910,2912,2914,2916,2918],{"class":56,"line":75},[54,2909,116],{"class":115},[54,2911,96],{"class":88},[54,2913,1628],{"class":92},[54,2915,89],{"class":88},[54,2917,1667],{"class":92},[54,2919,1950],{"class":88},[14,2921,2922],{},"This works:",[45,2924,2925],{"className":47,"code":1966,"language":49,"meta":50,"style":50},[17,2926,2927,2933,2937],{"__ignoreMap":50},[54,2928,2929,2931],{"class":56,"line":57},[54,2930,61],{"class":60},[54,2932,1648],{"class":64},[54,2934,2935],{"class":56,"line":68},[54,2936,72],{"emptyLinePlaceholder":71},[54,2938,2939,2941,2943,2945,2947,2949,2951,2953],{"class":56,"line":75},[54,2940,116],{"class":115},[54,2942,96],{"class":88},[54,2944,1628],{"class":92},[54,2946,89],{"class":88},[54,2948,1628],{"class":1693},[54,2950,89],{"class":88},[54,2952,1667],{"class":92},[54,2954,1950],{"class":88},[14,2956,2957],{},"Or use:",[45,2959,2961],{"className":47,"code":2960,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nprint(datetime.now())\n",[17,2962,2963,2973,2977],{"__ignoreMap":50},[54,2964,2965,2967,2969,2971],{"class":56,"line":57},[54,2966,1640],{"class":60},[54,2968,1643],{"class":64},[54,2970,61],{"class":60},[54,2972,1648],{"class":64},[54,2974,2975],{"class":56,"line":68},[54,2976,72],{"emptyLinePlaceholder":71},[54,2978,2979,2981,2983,2985,2987,2989],{"class":56,"line":75},[54,2980,116],{"class":115},[54,2982,96],{"class":88},[54,2984,1628],{"class":92},[54,2986,89],{"class":88},[54,2988,1667],{"class":92},[54,2990,1950],{"class":88},[602,2992,2994],{"id":2993},"forgetting-the-parentheses","Forgetting the parentheses",[14,2996,2997],{},"This:",[45,2999,3001],{"className":47,"code":3000,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nprint(datetime.now)\n",[17,3002,3003,3013,3017],{"__ignoreMap":50},[54,3004,3005,3007,3009,3011],{"class":56,"line":57},[54,3006,1640],{"class":60},[54,3008,1643],{"class":64},[54,3010,61],{"class":60},[54,3012,1648],{"class":64},[54,3014,3015],{"class":56,"line":68},[54,3016,72],{"emptyLinePlaceholder":71},[54,3018,3019,3021,3023,3025,3027,3029],{"class":56,"line":75},[54,3020,116],{"class":115},[54,3022,96],{"class":88},[54,3024,1628],{"class":92},[54,3026,89],{"class":88},[54,3028,1667],{"class":1693},[54,3030,109],{"class":88},[14,3032,3033,3034,3037],{},"does ",[35,3035,3036],{},"not"," call the function. It gives you the method itself.",[14,3039,3040],{},"Use:",[45,3042,3043],{"className":47,"code":2960,"language":49,"meta":50,"style":50},[17,3044,3045,3055,3059],{"__ignoreMap":50},[54,3046,3047,3049,3051,3053],{"class":56,"line":57},[54,3048,1640],{"class":60},[54,3050,1643],{"class":64},[54,3052,61],{"class":60},[54,3054,1648],{"class":64},[54,3056,3057],{"class":56,"line":68},[54,3058,72],{"emptyLinePlaceholder":71},[54,3060,3061,3063,3065,3067,3069,3071],{"class":56,"line":75},[54,3062,116],{"class":115},[54,3064,96],{"class":88},[54,3066,1628],{"class":92},[54,3068,89],{"class":88},[54,3070,1667],{"class":92},[54,3072,1950],{"class":88},[602,3074,3076],{"id":3075},"expecting-a-string","Expecting a string",[14,3078,3079,3080,3082],{},"This returns a ",[17,3081,1628],{}," object:",[45,3084,3085],{"className":47,"code":2169,"language":49,"meta":50,"style":50},[17,3086,3087,3097,3101,3115],{"__ignoreMap":50},[54,3088,3089,3091,3093,3095],{"class":56,"line":57},[54,3090,1640],{"class":60},[54,3092,1643],{"class":64},[54,3094,61],{"class":60},[54,3096,1648],{"class":64},[54,3098,3099],{"class":56,"line":68},[54,3100,72],{"emptyLinePlaceholder":71},[54,3102,3103,3105,3107,3109,3111,3113],{"class":56,"line":75},[54,3104,1657],{"class":64},[54,3106,82],{"class":81},[54,3108,1662],{"class":64},[54,3110,89],{"class":88},[54,3112,1667],{"class":92},[54,3114,1670],{"class":88},[54,3116,3117,3119,3121,3123,3125,3127],{"class":56,"line":112},[54,3118,116],{"class":115},[54,3120,96],{"class":88},[54,3122,2208],{"class":429},[54,3124,96],{"class":88},[54,3126,1667],{"class":92},[54,3128,382],{"class":88},[14,3130,3131],{},"If you need a string, use:",[45,3133,3135],{"className":47,"code":3134,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(str(now))\nprint(now.strftime(\"%Y-%m-%d %H:%M:%S\"))\n",[17,3136,3137,3147,3151,3165,3180],{"__ignoreMap":50},[54,3138,3139,3141,3143,3145],{"class":56,"line":57},[54,3140,1640],{"class":60},[54,3142,1643],{"class":64},[54,3144,61],{"class":60},[54,3146,1648],{"class":64},[54,3148,3149],{"class":56,"line":68},[54,3150,72],{"emptyLinePlaceholder":71},[54,3152,3153,3155,3157,3159,3161,3163],{"class":56,"line":75},[54,3154,1657],{"class":64},[54,3156,82],{"class":81},[54,3158,1662],{"class":64},[54,3160,89],{"class":88},[54,3162,1667],{"class":92},[54,3164,1670],{"class":88},[54,3166,3167,3169,3171,3174,3176,3178],{"class":56,"line":112},[54,3168,116],{"class":115},[54,3170,96],{"class":88},[54,3172,3173],{"class":429},"str",[54,3175,96],{"class":88},[54,3177,1667],{"class":92},[54,3179,382],{"class":88},[54,3181,3182,3184,3186,3188,3190,3192,3194,3196,3198,3200,3203,3205],{"class":56,"line":887},[54,3183,116],{"class":115},[54,3185,96],{"class":88},[54,3187,1667],{"class":92},[54,3189,89],{"class":88},[54,3191,2704],{"class":92},[54,3193,96],{"class":88},[54,3195,545],{"class":544},[54,3197,2711],{"class":548},[54,3199,2714],{"class":99},[54,3201,3202],{"class":548}," %H:%M:%S",[54,3204,545],{"class":544},[54,3206,382],{"class":88},[602,3208,3210],{"id":3209},"treating-the-module-and-class-as-the-same-thing","Treating the module and class as the same thing",[14,3212,3213],{},"This is very common:",[149,3215,3216,3220],{},[152,3217,3218,160],{},[17,3219,1628],{},[152,3221,3222,3224],{},[17,3223,1628],{}," class inside that module",[14,3226,3227],{},"That is why these two import styles behave differently:",[45,3229,3230],{"className":47,"code":1787,"language":49,"meta":50,"style":50},[17,3231,3232],{"__ignoreMap":50},[54,3233,3234,3236,3238,3240],{"class":56,"line":57},[54,3235,1640],{"class":60},[54,3237,1643],{"class":64},[54,3239,61],{"class":60},[54,3241,1648],{"class":64},[14,3243,3244],{},"and",[45,3246,3248],{"className":47,"code":3247,"language":49,"meta":50,"style":50},"import datetime\n",[17,3249,3250],{"__ignoreMap":50},[54,3251,3252,3254],{"class":56,"line":57},[54,3253,61],{"class":60},[54,3255,1648],{"class":64},[602,3257,3259],{"id":3258},"quick-checks-you-can-run","Quick checks you can run",[14,3261,3262],{},"If something is not working, try these commands:",[45,3264,3266],{"className":47,"code":3265,"language":49,"meta":50,"style":50},"python --version\npython\nfrom datetime import datetime\nprint(datetime.now())\nimport datetime; print(datetime.datetime.now())\ntype(datetime.now())\n",[17,3267,3268,3280,3285,3295,3309,3332],{"__ignoreMap":50},[54,3269,3270,3273,3277],{"class":56,"line":57},[54,3271,3272],{"class":64},"python ",[54,3274,3276],{"class":3275},"srjyR","--",[54,3278,3279],{"class":64},"version\n",[54,3281,3282],{"class":56,"line":68},[54,3283,3284],{"class":64},"python\n",[54,3286,3287,3289,3291,3293],{"class":56,"line":75},[54,3288,1640],{"class":60},[54,3290,1643],{"class":64},[54,3292,61],{"class":60},[54,3294,1648],{"class":64},[54,3296,3297,3299,3301,3303,3305,3307],{"class":56,"line":112},[54,3298,116],{"class":115},[54,3300,96],{"class":88},[54,3302,1628],{"class":92},[54,3304,89],{"class":88},[54,3306,1667],{"class":92},[54,3308,1950],{"class":88},[54,3310,3311,3313,3316,3318,3320,3322,3324,3326,3328,3330],{"class":56,"line":887},[54,3312,61],{"class":60},[54,3314,3315],{"class":64}," datetime; ",[54,3317,116],{"class":115},[54,3319,96],{"class":88},[54,3321,1628],{"class":92},[54,3323,89],{"class":88},[54,3325,1628],{"class":1693},[54,3327,89],{"class":88},[54,3329,1667],{"class":92},[54,3331,1950],{"class":88},[54,3333,3334,3336,3338,3340,3342,3344],{"class":56,"line":892},[54,3335,2208],{"class":429},[54,3337,96],{"class":88},[54,3339,1628],{"class":92},[54,3341,89],{"class":88},[54,3343,1667],{"class":92},[54,3345,1950],{"class":88},[14,3347,3348],{},"Common causes include:",[149,3350,3351,3364,3372,3377,3382],{},[152,3352,3353,3354,3357,3358,3360,3361],{},"imported the module with ",[17,3355,3356],{},"import datetime"," but called ",[17,3359,1594],{}," instead of ",[17,3362,3363],{},"datetime.datetime.now()",[152,3365,3366,3367,3360,3370],{},"forgot parentheses and wrote ",[17,3368,3369],{},"datetime.now",[17,3371,1594],{},[152,3373,3374,3375,1629],{},"expected a string, but got a ",[17,3376,1628],{},[152,3378,3379,3380],{},"tried to format the value without using ",[17,3381,2654],{},[152,3383,3384],{},"mixed up local time with timezone-aware time",[40,3386,1419],{"id":1418},[602,3388,3390,3391,3393],{"id":3389},"what-does-datetimenow-return","What does ",[17,3392,1594],{}," return?",[14,3395,3396,3397,3399],{},"It returns a ",[17,3398,1628],{}," object with the current local date and time.",[602,3401,3403,3404,3406,3407,3409],{"id":3402},"why-does-datetimenow-not-work-after-import-datetime","Why does ",[17,3405,1594],{}," not work after ",[17,3408,3356],{},"?",[14,3411,3412,3413,3415,3416,89],{},"Because ",[17,3414,1628],{}," is then the module name. You need ",[17,3417,3363],{},[602,3419,3421],{"id":3420},"how-do-i-print-only-the-date","How do I print only the date?",[14,3423,3424,3425,3427,3428,3430],{},"Call ",[17,3426,2447],{}," on the result, or use ",[17,3429,2654],{}," for a custom format.",[602,3432,3434,3435,3437],{"id":3433},"how-do-i-convert-datetimenow-to-a-string","How do I convert ",[17,3436,1594],{}," to a string?",[14,3439,126,3440,3443,3444,3430],{},[17,3441,3442],{},"str()"," for the default format or ",[17,3445,2654],{},[602,3447,3449,3450,3452],{"id":3448},"is-datetimenow-the-same-on-every-computer","Is ",[17,3451,1594],{}," the same on every computer?",[14,3454,3455],{},"No. It uses the current local time on that system.",[40,3457,1510],{"id":1509},[149,3459,3460,3464,3470,3476,3480,3486],{},[152,3461,3462],{},[192,3463,1774],{"href":1773},[152,3465,3466],{},[192,3467,3468,2834],{"href":2830},[17,3469,2833],{},[152,3471,3472],{},[192,3473,3474,2834],{"href":2838},[17,3475,2841],{},[152,3477,3478],{},[192,3479,2254],{"href":2253},[152,3481,3482],{},[192,3483,3485],{"href":3484},"\u002Fhow-to\u002Fhow-to-format-strings-in-python","How to format strings in Python",[152,3487,3488],{},[192,3489,2003],{"href":2002},[1545,3491,3492],{},"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 .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 .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 .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 .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 .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":50,"searchDepth":68,"depth":68,"links":3494},[3495,3497,3502,3505,3506,3509,3513,3514,3521,3532],{"id":1735,"depth":68,"text":3496},"What datetime.now() does",{"id":1777,"depth":68,"text":3498,"children":3499},"Importing datetime.now() correctly",[3500,3501],{"id":1820,"depth":75,"text":1821},{"id":1916,"depth":75,"text":1917},{"id":2006,"depth":68,"text":2007,"children":3503},[3504],{"id":2039,"depth":75,"text":1821},{"id":2257,"depth":68,"text":2258},{"id":2433,"depth":68,"text":2434,"children":3507},[3508],{"id":2457,"depth":75,"text":1821},{"id":2640,"depth":68,"text":2641,"children":3510},[3511,3512],{"id":2657,"depth":75,"text":2658},{"id":2743,"depth":75,"text":2744},{"id":2844,"depth":68,"text":2845},{"id":2878,"depth":68,"text":2879,"children":3515},[3516,3517,3518,3519,3520],{"id":2887,"depth":75,"text":2888},{"id":2993,"depth":75,"text":2994},{"id":3075,"depth":75,"text":3076},{"id":3209,"depth":75,"text":3210},{"id":3258,"depth":75,"text":3259},{"id":1418,"depth":68,"text":1419,"children":3522},[3523,3525,3527,3528,3530],{"id":3389,"depth":75,"text":3524},"What does datetime.now() return?",{"id":3402,"depth":75,"text":3526},"Why does datetime.now() not work after import datetime?",{"id":3420,"depth":75,"text":3421},{"id":3433,"depth":75,"text":3529},"How do I convert datetime.now() to a string?",{"id":3448,"depth":75,"text":3531},"Is datetime.now() the same on every computer?",{"id":1509,"depth":68,"text":1510},"Master datetime.now explained in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fdatetime.now-explained",{"title":1586,"description":3533},"standard-library\u002Fdatetime.now-explained","tR51qDIDVR-G8We5LClZ5Z4YIU1LZt7NKa56wwVnFws",{"id":3540,"title":3541,"body":3542,"description":6101,"extension":1577,"meta":6102,"navigation":71,"path":2830,"seo":6103,"stem":6104,"__hash__":6105},"content\u002Fstandard-library\u002Fdatetime.strftime-explained.md","datetime.strftime() Explained",{"type":7,"value":3543,"toc":6067},[3544,3547,3552,3555,3577,3593,3597,3668,3670,3702,3710,3714,3718,3739,3742,3744,3765,3794,3796,3883,3885,3918,3926,3928,3931,4065,4067,4134,4137,4158,4162,4168,4227,4229,4492,4494,4544,4548,4551,4555,4611,4613,4632,4636,4696,4698,4715,4718,4772,4774,4791,4795,4851,4853,4872,4876,4935,4939,4945,5017,5019,5047,5053,5057,5060,5078,5080,5174,5176,5201,5207,5211,5215,5221,5265,5268,5298,5311,5319,5322,5333,5335,5443,5445,5478,5488,5511,5514,5586,5588,5603,5606,5677,5679,5695,5699,5702,5777,5779,5798,5801,5805,5808,5811,5933,5936,5955,5959,5964,5978,5981,5983,5987,5990,5994,6002,6006,6012,6016,6026,6030,6039,6041,6064],[10,3545,3541],{"id":3546},"datetimestrftime-explained",[14,3548,3549,3551],{},[17,3550,2833],{}," formats a date or time object as a string.",[14,3553,3554],{},"Use it when you want to control how a date or time looks, such as:",[149,3556,3557,3562,3567,3572],{},[152,3558,3559],{},[17,3560,3561],{},"2025-04-22",[152,3563,3564],{},[17,3565,3566],{},"04\u002F22\u002F2025",[152,3568,3569],{},[17,3570,3571],{},"April 22, 2025",[152,3573,3574],{},[17,3575,3576],{},"14:30:00",[14,3578,3579,3580,3582,3583,320,3585,3587,3588,3590,3591,89],{},"This method is part of Python’s ",[17,3581,1628],{}," tools. It works with ",[17,3584,1628],{},[17,3586,2535],{},", and ",[17,3589,2559],{}," objects. If you are new to the module, see the ",[192,3592,1774],{"href":1773},[40,3594,3596],{"id":3595},"quick-example","Quick example",[45,3598,3600],{"className":47,"code":3599,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nformatted = now.strftime(\"%Y-%m-%d %H:%M:%S\")\nprint(formatted)\n",[17,3601,3602,3612,3616,3630,3657],{"__ignoreMap":50},[54,3603,3604,3606,3608,3610],{"class":56,"line":57},[54,3605,1640],{"class":60},[54,3607,1643],{"class":64},[54,3609,61],{"class":60},[54,3611,1648],{"class":64},[54,3613,3614],{"class":56,"line":68},[54,3615,72],{"emptyLinePlaceholder":71},[54,3617,3618,3620,3622,3624,3626,3628],{"class":56,"line":75},[54,3619,1657],{"class":64},[54,3621,82],{"class":81},[54,3623,1662],{"class":64},[54,3625,89],{"class":88},[54,3627,1667],{"class":92},[54,3629,1670],{"class":88},[54,3631,3632,3635,3637,3639,3641,3643,3645,3647,3649,3651,3653,3655],{"class":56,"line":112},[54,3633,3634],{"class":64},"formatted ",[54,3636,82],{"class":81},[54,3638,2316],{"class":64},[54,3640,89],{"class":88},[54,3642,2704],{"class":92},[54,3644,96],{"class":88},[54,3646,545],{"class":544},[54,3648,2711],{"class":548},[54,3650,2714],{"class":99},[54,3652,3202],{"class":548},[54,3654,545],{"class":544},[54,3656,109],{"class":88},[54,3658,3659,3661,3663,3666],{"class":56,"line":887},[54,3660,116],{"class":115},[54,3662,96],{"class":88},[54,3664,3665],{"class":92},"formatted",[54,3667,109],{"class":88},[14,3669,561],{},[45,3671,3673],{"className":47,"code":3672,"language":49,"meta":50,"style":50},"2025-04-22 14:30:45\n",[17,3674,3675],{"__ignoreMap":50},[54,3676,3677,3680,3682,3684,3686,3688,3690,3692,3694,3697,3699],{"class":56,"line":57},[54,3678,3679],{"class":99},"2025",[54,3681,1467],{"class":81},[54,3683,752],{"class":99},[54,3685,2389],{"class":2388},[54,3687,1467],{"class":81},[54,3689,2394],{"class":99},[54,3691,2397],{"class":99},[54,3693,147],{"class":88},[54,3695,3696],{"class":99},"30",[54,3698,147],{"class":88},[54,3700,3701],{"class":99},"45\n",[14,3703,126,3704,3706,3707,3709],{},[17,3705,2654],{}," when you need to turn a ",[17,3708,1628],{}," object into a formatted string.",[40,3711,3713],{"id":3712},"what-datetimestrftime-does","What datetime.strftime() does",[14,3715,3716,147],{},[17,3717,2654],{},[149,3719,3720,3723,3726,3729],{},[152,3721,3722],{},"Converts a date or datetime object into a string",[152,3724,3725],{},"Lets you control the output format",[152,3727,3728],{},"Is useful for logs, filenames, reports, and user-friendly dates",[152,3730,3731,3732,320,3734,3587,3736,3738],{},"Works on ",[17,3733,1628],{},[17,3735,2535],{},[17,3737,2559],{}," objects",[14,3740,3741],{},"In other words, it takes a Python date\u002Ftime value and returns text in the format you choose.",[40,3743,199],{"id":198},[45,3745,3747],{"className":47,"code":3746,"language":49,"meta":50,"style":50},"object.strftime(format_string)\n",[17,3748,3749],{"__ignoreMap":50},[54,3750,3751,3754,3756,3758,3760,3763],{"class":56,"line":57},[54,3752,3753],{"class":429},"object",[54,3755,89],{"class":88},[54,3757,2704],{"class":92},[54,3759,96],{"class":88},[54,3761,3762],{"class":92},"format_string",[54,3764,109],{"class":88},[149,3766,3767,3778,3791],{},[152,3768,3769,3771,3772,320,3774,324,3776,1629],{},[17,3770,3753],{}," is usually a ",[17,3773,1628],{},[17,3775,2535],{},[17,3777,2559],{},[152,3779,3780,3782,3783,320,3786,3587,3789],{},[17,3781,3762],{}," contains special codes such as ",[17,3784,3785],{},"%Y",[17,3787,3788],{},"%m",[17,3790,2714],{},[152,3792,3793],{},"The result is always a string",[14,3795,253],{},[45,3797,3799],{"className":47,"code":3798,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nresult = now.strftime(\"%Y-%m-%d\")\n\nprint(result)\nprint(type(result))\n",[17,3800,3801,3811,3815,3829,3854,3858,3869],{"__ignoreMap":50},[54,3802,3803,3805,3807,3809],{"class":56,"line":57},[54,3804,1640],{"class":60},[54,3806,1643],{"class":64},[54,3808,61],{"class":60},[54,3810,1648],{"class":64},[54,3812,3813],{"class":56,"line":68},[54,3814,72],{"emptyLinePlaceholder":71},[54,3816,3817,3819,3821,3823,3825,3827],{"class":56,"line":75},[54,3818,1657],{"class":64},[54,3820,82],{"class":81},[54,3822,1662],{"class":64},[54,3824,89],{"class":88},[54,3826,1667],{"class":92},[54,3828,1670],{"class":88},[54,3830,3831,3834,3836,3838,3840,3842,3844,3846,3848,3850,3852],{"class":56,"line":112},[54,3832,3833],{"class":64},"result ",[54,3835,82],{"class":81},[54,3837,2316],{"class":64},[54,3839,89],{"class":88},[54,3841,2704],{"class":92},[54,3843,96],{"class":88},[54,3845,545],{"class":544},[54,3847,2711],{"class":548},[54,3849,2714],{"class":99},[54,3851,545],{"class":544},[54,3853,109],{"class":88},[54,3855,3856],{"class":56,"line":887},[54,3857,72],{"emptyLinePlaceholder":71},[54,3859,3860,3862,3864,3867],{"class":56,"line":892},[54,3861,116],{"class":115},[54,3863,96],{"class":88},[54,3865,3866],{"class":92},"result",[54,3868,109],{"class":88},[54,3870,3871,3873,3875,3877,3879,3881],{"class":56,"line":913},[54,3872,116],{"class":115},[54,3874,96],{"class":88},[54,3876,2208],{"class":429},[54,3878,96],{"class":88},[54,3880,3866],{"class":92},[54,3882,382],{"class":88},[14,3884,449],{},[45,3886,3888],{"className":47,"code":3887,"language":49,"meta":50,"style":50},"2025-04-22\n\u003Cclass 'str'>\n",[17,3889,3890,3904],{"__ignoreMap":50},[54,3891,3892,3894,3896,3898,3900,3902],{"class":56,"line":57},[54,3893,3679],{"class":99},[54,3895,1467],{"class":81},[54,3897,752],{"class":99},[54,3899,2389],{"class":2388},[54,3901,1467],{"class":81},[54,3903,2617],{"class":99},[54,3905,3906,3908,3910,3912,3914,3916],{"class":56,"line":68},[54,3907,2227],{"class":81},[54,3909,2231],{"class":2230},[54,3911,2234],{"class":544},[54,3913,3173],{"class":548},[54,3915,2240],{"class":544},[54,3917,2243],{"class":81},[14,3919,3920,3921,3923,3924,1732],{},"Notice that ",[17,3922,2654],{}," returns a string, not another ",[17,3925,1628],{},[40,3927,493],{"id":492},[14,3929,3930],{},"Here is a complete example step by step:",[45,3932,3934],{"className":47,"code":3933,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nmeeting = datetime(2025, 7, 14, 9, 30, 0)\n\nformatted = meeting.strftime(\"%B %d, %Y at %H:%M\")\n\nprint(\"Original datetime:\", meeting)\nprint(\"Formatted string:\", formatted)\n",[17,3935,3936,3946,3950,3989,3993,4022,4026,4045],{"__ignoreMap":50},[54,3937,3938,3940,3942,3944],{"class":56,"line":57},[54,3939,1640],{"class":60},[54,3941,1643],{"class":64},[54,3943,61],{"class":60},[54,3945,1648],{"class":64},[54,3947,3948],{"class":56,"line":68},[54,3949,72],{"emptyLinePlaceholder":71},[54,3951,3952,3955,3957,3959,3961,3963,3965,3968,3970,3972,3974,3977,3979,3982,3984,3987],{"class":56,"line":75},[54,3953,3954],{"class":64},"meeting ",[54,3956,82],{"class":81},[54,3958,1662],{"class":92},[54,3960,96],{"class":88},[54,3962,3679],{"class":99},[54,3964,103],{"class":88},[54,3966,3967],{"class":99}," 7",[54,3969,103],{"class":88},[54,3971,2397],{"class":99},[54,3973,103],{"class":88},[54,3975,3976],{"class":99}," 9",[54,3978,103],{"class":88},[54,3980,3981],{"class":99}," 30",[54,3983,103],{"class":88},[54,3985,3986],{"class":99}," 0",[54,3988,109],{"class":88},[54,3990,3991],{"class":56,"line":112},[54,3992,72],{"emptyLinePlaceholder":71},[54,3994,3995,3997,3999,4002,4004,4006,4008,4010,4013,4015,4018,4020],{"class":56,"line":887},[54,3996,3634],{"class":64},[54,3998,82],{"class":81},[54,4000,4001],{"class":64}," meeting",[54,4003,89],{"class":88},[54,4005,2704],{"class":92},[54,4007,96],{"class":88},[54,4009,545],{"class":544},[54,4011,4012],{"class":548},"%B ",[54,4014,2714],{"class":99},[54,4016,4017],{"class":548},", %Y at %H:%M",[54,4019,545],{"class":544},[54,4021,109],{"class":88},[54,4023,4024],{"class":56,"line":892},[54,4025,72],{"emptyLinePlaceholder":71},[54,4027,4028,4030,4032,4034,4037,4039,4041,4043],{"class":56,"line":913},[54,4029,116],{"class":115},[54,4031,96],{"class":88},[54,4033,545],{"class":544},[54,4035,4036],{"class":548},"Original datetime:",[54,4038,545],{"class":544},[54,4040,103],{"class":88},[54,4042,4001],{"class":92},[54,4044,109],{"class":88},[54,4046,4047,4049,4051,4053,4056,4058,4060,4063],{"class":56,"line":2121},[54,4048,116],{"class":115},[54,4050,96],{"class":88},[54,4052,545],{"class":544},[54,4054,4055],{"class":548},"Formatted string:",[54,4057,545],{"class":544},[54,4059,103],{"class":88},[54,4061,4062],{"class":92}," formatted",[54,4064,109],{"class":88},[14,4066,449],{},[45,4068,4070],{"className":47,"code":4069,"language":49,"meta":50,"style":50},"Original datetime: 2025-07-14 09:30:00\nFormatted string: July 14, 2025 at 09:30\n",[17,4071,4072,4106],{"__ignoreMap":50},[54,4073,4074,4077,4079,4082,4084,4086,4089,4091,4093,4095,4097,4099,4101,4103],{"class":56,"line":57},[54,4075,4076],{"class":64},"Original datetime",[54,4078,147],{"class":88},[54,4080,4081],{"class":99}," 2025",[54,4083,1467],{"class":81},[54,4085,752],{"class":99},[54,4087,4088],{"class":2388},"7",[54,4090,1467],{"class":81},[54,4092,2812],{"class":99},[54,4094,3986],{"class":99},[54,4096,1125],{"class":2388},[54,4098,147],{"class":88},[54,4100,3696],{"class":99},[54,4102,147],{"class":88},[54,4104,4105],{"class":99},"00\n",[54,4107,4108,4111,4113,4116,4118,4120,4122,4125,4127,4129,4131],{"class":56,"line":68},[54,4109,4110],{"class":64},"Formatted string",[54,4112,147],{"class":88},[54,4114,4115],{"class":64}," July ",[54,4117,2812],{"class":99},[54,4119,103],{"class":88},[54,4121,4081],{"class":99},[54,4123,4124],{"class":64}," at ",[54,4126,752],{"class":99},[54,4128,1125],{"class":2388},[54,4130,147],{"class":88},[54,4132,4133],{"class":99},"30\n",[14,4135,4136],{},"Important points:",[149,4138,4139,4147,4152],{},[152,4140,4141,4144,4145,1629],{},[17,4142,4143],{},"meeting"," is a ",[17,4146,1628],{},[152,4148,4149,4151],{},[17,4150,3665],{}," is a string",[152,4153,4154,4155,4157],{},"The original ",[17,4156,4143],{}," value does not change",[40,4159,4161],{"id":4160},"common-format-codes","Common format codes",[14,4163,4164,4165,4167],{},"These are some of the most useful ",[17,4166,2654],{}," format codes:",[149,4169,4170,4175,4180,4185,4191,4197,4203,4209,4215,4221],{},[152,4171,4172,4174],{},[17,4173,3785],{}," = 4-digit year",[152,4176,4177,4179],{},[17,4178,3788],{}," = month as number",[152,4181,4182,4184],{},[17,4183,2714],{}," = day of month",[152,4186,4187,4190],{},[17,4188,4189],{},"%H"," = hour in 24-hour format",[152,4192,4193,4196],{},[17,4194,4195],{},"%M"," = minute",[152,4198,4199,4202],{},[17,4200,4201],{},"%S"," = second",[152,4204,4205,4208],{},[17,4206,4207],{},"%I"," = hour in 12-hour format",[152,4210,4211,4214],{},[17,4212,4213],{},"%p"," = AM or PM",[152,4216,4217,4220],{},[17,4218,4219],{},"%A"," = full weekday name",[152,4222,4223,4226],{},[17,4224,4225],{},"%B"," = full month name",[14,4228,253],{},[45,4230,4232],{"className":47,"code":4231,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime(2025, 4, 22, 14, 5, 9)\n\nprint(now.strftime(\"%Y\"))  # year\nprint(now.strftime(\"%m\"))  # month\nprint(now.strftime(\"%d\"))  # day\nprint(now.strftime(\"%H\"))  # hour (24-hour)\nprint(now.strftime(\"%M\"))  # minute\nprint(now.strftime(\"%S\"))  # second\nprint(now.strftime(\"%A\"))  # weekday name\nprint(now.strftime(\"%B\"))  # month name\n",[17,4233,4234,4244,4248,4284,4288,4315,4340,4365,4390,4415,4440,4466],{"__ignoreMap":50},[54,4235,4236,4238,4240,4242],{"class":56,"line":57},[54,4237,1640],{"class":60},[54,4239,1643],{"class":64},[54,4241,61],{"class":60},[54,4243,1648],{"class":64},[54,4245,4246],{"class":56,"line":68},[54,4247,72],{"emptyLinePlaceholder":71},[54,4249,4250,4252,4254,4256,4258,4260,4262,4265,4267,4270,4272,4274,4276,4278,4280,4282],{"class":56,"line":75},[54,4251,1657],{"class":64},[54,4253,82],{"class":81},[54,4255,1662],{"class":92},[54,4257,96],{"class":88},[54,4259,3679],{"class":99},[54,4261,103],{"class":88},[54,4263,4264],{"class":99}," 4",[54,4266,103],{"class":88},[54,4268,4269],{"class":99}," 22",[54,4271,103],{"class":88},[54,4273,2397],{"class":99},[54,4275,103],{"class":88},[54,4277,1235],{"class":99},[54,4279,103],{"class":88},[54,4281,3976],{"class":99},[54,4283,109],{"class":88},[54,4285,4286],{"class":56,"line":112},[54,4287,72],{"emptyLinePlaceholder":71},[54,4289,4290,4292,4294,4296,4298,4300,4302,4304,4306,4308,4311],{"class":56,"line":887},[54,4291,116],{"class":115},[54,4293,96],{"class":88},[54,4295,1667],{"class":92},[54,4297,89],{"class":88},[54,4299,2704],{"class":92},[54,4301,96],{"class":88},[54,4303,545],{"class":544},[54,4305,3785],{"class":548},[54,4307,545],{"class":544},[54,4309,4310],{"class":88},"))",[54,4312,4314],{"class":4313},"sutJx","  # year\n",[54,4316,4317,4319,4321,4323,4325,4327,4329,4331,4333,4335,4337],{"class":56,"line":892},[54,4318,116],{"class":115},[54,4320,96],{"class":88},[54,4322,1667],{"class":92},[54,4324,89],{"class":88},[54,4326,2704],{"class":92},[54,4328,96],{"class":88},[54,4330,545],{"class":544},[54,4332,3788],{"class":548},[54,4334,545],{"class":544},[54,4336,4310],{"class":88},[54,4338,4339],{"class":4313},"  # month\n",[54,4341,4342,4344,4346,4348,4350,4352,4354,4356,4358,4360,4362],{"class":56,"line":913},[54,4343,116],{"class":115},[54,4345,96],{"class":88},[54,4347,1667],{"class":92},[54,4349,89],{"class":88},[54,4351,2704],{"class":92},[54,4353,96],{"class":88},[54,4355,545],{"class":544},[54,4357,2714],{"class":99},[54,4359,545],{"class":544},[54,4361,4310],{"class":88},[54,4363,4364],{"class":4313},"  # day\n",[54,4366,4367,4369,4371,4373,4375,4377,4379,4381,4383,4385,4387],{"class":56,"line":2121},[54,4368,116],{"class":115},[54,4370,96],{"class":88},[54,4372,1667],{"class":92},[54,4374,89],{"class":88},[54,4376,2704],{"class":92},[54,4378,96],{"class":88},[54,4380,545],{"class":544},[54,4382,4189],{"class":548},[54,4384,545],{"class":544},[54,4386,4310],{"class":88},[54,4388,4389],{"class":4313},"  # hour (24-hour)\n",[54,4391,4392,4394,4396,4398,4400,4402,4404,4406,4408,4410,4412],{"class":56,"line":2136},[54,4393,116],{"class":115},[54,4395,96],{"class":88},[54,4397,1667],{"class":92},[54,4399,89],{"class":88},[54,4401,2704],{"class":92},[54,4403,96],{"class":88},[54,4405,545],{"class":544},[54,4407,4195],{"class":548},[54,4409,545],{"class":544},[54,4411,4310],{"class":88},[54,4413,4414],{"class":4313},"  # minute\n",[54,4416,4417,4419,4421,4423,4425,4427,4429,4431,4433,4435,4437],{"class":56,"line":2151},[54,4418,116],{"class":115},[54,4420,96],{"class":88},[54,4422,1667],{"class":92},[54,4424,89],{"class":88},[54,4426,2704],{"class":92},[54,4428,96],{"class":88},[54,4430,545],{"class":544},[54,4432,4201],{"class":548},[54,4434,545],{"class":544},[54,4436,4310],{"class":88},[54,4438,4439],{"class":4313},"  # second\n",[54,4441,4443,4445,4447,4449,4451,4453,4455,4457,4459,4461,4463],{"class":56,"line":4442},11,[54,4444,116],{"class":115},[54,4446,96],{"class":88},[54,4448,1667],{"class":92},[54,4450,89],{"class":88},[54,4452,2704],{"class":92},[54,4454,96],{"class":88},[54,4456,545],{"class":544},[54,4458,4219],{"class":548},[54,4460,545],{"class":544},[54,4462,4310],{"class":88},[54,4464,4465],{"class":4313},"  # weekday name\n",[54,4467,4469,4471,4473,4475,4477,4479,4481,4483,4485,4487,4489],{"class":56,"line":4468},12,[54,4470,116],{"class":115},[54,4472,96],{"class":88},[54,4474,1667],{"class":92},[54,4476,89],{"class":88},[54,4478,2704],{"class":92},[54,4480,96],{"class":88},[54,4482,545],{"class":544},[54,4484,4225],{"class":548},[54,4486,545],{"class":544},[54,4488,4310],{"class":88},[54,4490,4491],{"class":4313},"  # month name\n",[14,4493,449],{},[45,4495,4497],{"className":47,"code":4496,"language":49,"meta":50,"style":50},"2025\n04\n22\n14\n05\n09\nTuesday\nApril\n",[17,4498,4499,4504,4511,4515,4520,4527,4534,4539],{"__ignoreMap":50},[54,4500,4501],{"class":56,"line":57},[54,4502,4503],{"class":99},"2025\n",[54,4505,4506,4508],{"class":56,"line":68},[54,4507,752],{"class":99},[54,4509,4510],{"class":2388},"4\n",[54,4512,4513],{"class":56,"line":75},[54,4514,2617],{"class":99},[54,4516,4517],{"class":56,"line":112},[54,4518,4519],{"class":99},"14\n",[54,4521,4522,4524],{"class":56,"line":887},[54,4523,752],{"class":99},[54,4525,4526],{"class":2388},"5\n",[54,4528,4529,4531],{"class":56,"line":892},[54,4530,752],{"class":99},[54,4532,4533],{"class":2388},"9\n",[54,4535,4536],{"class":56,"line":913},[54,4537,4538],{"class":64},"Tuesday\n",[54,4540,4541],{"class":56,"line":2121},[54,4542,4543],{"class":64},"April\n",[40,4545,4547],{"id":4546},"common-formatting-patterns","Common formatting patterns",[14,4549,4550],{},"These patterns are common in real programs.",[602,4552,4554],{"id":4553},"iso-style-date","ISO-style date",[45,4556,4557],{"className":47,"code":2661,"language":49,"meta":50,"style":50},[17,4558,4559,4569,4573,4587],{"__ignoreMap":50},[54,4560,4561,4563,4565,4567],{"class":56,"line":57},[54,4562,1640],{"class":60},[54,4564,1643],{"class":64},[54,4566,61],{"class":60},[54,4568,1648],{"class":64},[54,4570,4571],{"class":56,"line":68},[54,4572,72],{"emptyLinePlaceholder":71},[54,4574,4575,4577,4579,4581,4583,4585],{"class":56,"line":75},[54,4576,1657],{"class":64},[54,4578,82],{"class":81},[54,4580,1662],{"class":64},[54,4582,89],{"class":88},[54,4584,1667],{"class":92},[54,4586,1670],{"class":88},[54,4588,4589,4591,4593,4595,4597,4599,4601,4603,4605,4607,4609],{"class":56,"line":112},[54,4590,116],{"class":115},[54,4592,96],{"class":88},[54,4594,1667],{"class":92},[54,4596,89],{"class":88},[54,4598,2704],{"class":92},[54,4600,96],{"class":88},[54,4602,545],{"class":544},[54,4604,2711],{"class":548},[54,4606,2714],{"class":99},[54,4608,545],{"class":544},[54,4610,382],{"class":88},[14,4612,561],{},[45,4614,4616],{"className":47,"code":4615,"language":49,"meta":50,"style":50},"2025-04-22\n",[17,4617,4618],{"__ignoreMap":50},[54,4619,4620,4622,4624,4626,4628,4630],{"class":56,"line":57},[54,4621,3679],{"class":99},[54,4623,1467],{"class":81},[54,4625,752],{"class":99},[54,4627,2389],{"class":2388},[54,4629,1467],{"class":81},[54,4631,2617],{"class":99},[602,4633,4635],{"id":4634},"readable-date","Readable date",[45,4637,4639],{"className":47,"code":4638,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(now.strftime(\"%B %d, %Y\"))\n",[17,4640,4641,4651,4655,4669],{"__ignoreMap":50},[54,4642,4643,4645,4647,4649],{"class":56,"line":57},[54,4644,1640],{"class":60},[54,4646,1643],{"class":64},[54,4648,61],{"class":60},[54,4650,1648],{"class":64},[54,4652,4653],{"class":56,"line":68},[54,4654,72],{"emptyLinePlaceholder":71},[54,4656,4657,4659,4661,4663,4665,4667],{"class":56,"line":75},[54,4658,1657],{"class":64},[54,4660,82],{"class":81},[54,4662,1662],{"class":64},[54,4664,89],{"class":88},[54,4666,1667],{"class":92},[54,4668,1670],{"class":88},[54,4670,4671,4673,4675,4677,4679,4681,4683,4685,4687,4689,4692,4694],{"class":56,"line":112},[54,4672,116],{"class":115},[54,4674,96],{"class":88},[54,4676,1667],{"class":92},[54,4678,89],{"class":88},[54,4680,2704],{"class":92},[54,4682,96],{"class":88},[54,4684,545],{"class":544},[54,4686,4012],{"class":548},[54,4688,2714],{"class":99},[54,4690,4691],{"class":548},", %Y",[54,4693,545],{"class":544},[54,4695,382],{"class":88},[14,4697,561],{},[45,4699,4701],{"className":47,"code":4700,"language":49,"meta":50,"style":50},"April 22, 2025\n",[17,4702,4703],{"__ignoreMap":50},[54,4704,4705,4708,4710,4712],{"class":56,"line":57},[54,4706,4707],{"class":64},"April ",[54,4709,2394],{"class":99},[54,4711,103],{"class":88},[54,4713,4714],{"class":99}," 2025\n",[602,4716,2622],{"id":4717},"time-only",[45,4719,4720],{"className":47,"code":2747,"language":49,"meta":50,"style":50},[17,4721,4722,4732,4736,4750],{"__ignoreMap":50},[54,4723,4724,4726,4728,4730],{"class":56,"line":57},[54,4725,1640],{"class":60},[54,4727,1643],{"class":64},[54,4729,61],{"class":60},[54,4731,1648],{"class":64},[54,4733,4734],{"class":56,"line":68},[54,4735,72],{"emptyLinePlaceholder":71},[54,4737,4738,4740,4742,4744,4746,4748],{"class":56,"line":75},[54,4739,1657],{"class":64},[54,4741,82],{"class":81},[54,4743,1662],{"class":64},[54,4745,89],{"class":88},[54,4747,1667],{"class":92},[54,4749,1670],{"class":88},[54,4751,4752,4754,4756,4758,4760,4762,4764,4766,4768,4770],{"class":56,"line":112},[54,4753,116],{"class":115},[54,4755,96],{"class":88},[54,4757,1667],{"class":92},[54,4759,89],{"class":88},[54,4761,2704],{"class":92},[54,4763,96],{"class":88},[54,4765,545],{"class":544},[54,4767,2796],{"class":548},[54,4769,545],{"class":544},[54,4771,382],{"class":88},[14,4773,561],{},[45,4775,4777],{"className":47,"code":4776,"language":49,"meta":50,"style":50},"14:30:45\n",[17,4778,4779],{"__ignoreMap":50},[54,4780,4781,4783,4785,4787,4789],{"class":56,"line":57},[54,4782,2812],{"class":99},[54,4784,147],{"class":88},[54,4786,3696],{"class":99},[54,4788,147],{"class":88},[54,4790,3701],{"class":99},[602,4792,4794],{"id":4793},"_12-hour-time","12-hour time",[45,4796,4798],{"className":47,"code":4797,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(now.strftime(\"%I:%M %p\"))\n",[17,4799,4800,4810,4814,4828],{"__ignoreMap":50},[54,4801,4802,4804,4806,4808],{"class":56,"line":57},[54,4803,1640],{"class":60},[54,4805,1643],{"class":64},[54,4807,61],{"class":60},[54,4809,1648],{"class":64},[54,4811,4812],{"class":56,"line":68},[54,4813,72],{"emptyLinePlaceholder":71},[54,4815,4816,4818,4820,4822,4824,4826],{"class":56,"line":75},[54,4817,1657],{"class":64},[54,4819,82],{"class":81},[54,4821,1662],{"class":64},[54,4823,89],{"class":88},[54,4825,1667],{"class":92},[54,4827,1670],{"class":88},[54,4829,4830,4832,4834,4836,4838,4840,4842,4844,4847,4849],{"class":56,"line":112},[54,4831,116],{"class":115},[54,4833,96],{"class":88},[54,4835,1667],{"class":92},[54,4837,89],{"class":88},[54,4839,2704],{"class":92},[54,4841,96],{"class":88},[54,4843,545],{"class":544},[54,4845,4846],{"class":548},"%I:%M %p",[54,4848,545],{"class":544},[54,4850,382],{"class":88},[14,4852,561],{},[45,4854,4856],{"className":47,"code":4855,"language":49,"meta":50,"style":50},"02:30 PM\n",[17,4857,4858],{"__ignoreMap":50},[54,4859,4860,4862,4864,4866,4868],{"class":56,"line":57},[54,4861,752],{"class":99},[54,4863,396],{"class":2388},[54,4865,147],{"class":88},[54,4867,3696],{"class":99},[54,4869,4871],{"class":4870},"s_hVV"," PM\n",[602,4873,4875],{"id":4874},"date-and-time-together","Date and time together",[45,4877,4879],{"className":47,"code":4878,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nprint(now.strftime(\"%Y-%m-%d %H:%M:%S\"))\n",[17,4880,4881,4891,4895,4909],{"__ignoreMap":50},[54,4882,4883,4885,4887,4889],{"class":56,"line":57},[54,4884,1640],{"class":60},[54,4886,1643],{"class":64},[54,4888,61],{"class":60},[54,4890,1648],{"class":64},[54,4892,4893],{"class":56,"line":68},[54,4894,72],{"emptyLinePlaceholder":71},[54,4896,4897,4899,4901,4903,4905,4907],{"class":56,"line":75},[54,4898,1657],{"class":64},[54,4900,82],{"class":81},[54,4902,1662],{"class":64},[54,4904,89],{"class":88},[54,4906,1667],{"class":92},[54,4908,1670],{"class":88},[54,4910,4911,4913,4915,4917,4919,4921,4923,4925,4927,4929,4931,4933],{"class":56,"line":112},[54,4912,116],{"class":115},[54,4914,96],{"class":88},[54,4916,1667],{"class":92},[54,4918,89],{"class":88},[54,4920,2704],{"class":92},[54,4922,96],{"class":88},[54,4924,545],{"class":544},[54,4926,2711],{"class":548},[54,4928,2714],{"class":99},[54,4930,3202],{"class":548},[54,4932,545],{"class":544},[54,4934,382],{"class":88},[602,4936,4938],{"id":4937},"filename-safe-timestamp","Filename-safe timestamp",[14,4940,4941,4942,4944],{},"This is useful because it avoids characters like ",[17,4943,147],{}," that can cause problems in filenames on some systems.",[45,4946,4948],{"className":47,"code":4947,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nfilename_stamp = now.strftime(\"%Y-%m-%d_%H-%M-%S\")\nprint(filename_stamp)\n",[17,4949,4950,4960,4964,4978,5006],{"__ignoreMap":50},[54,4951,4952,4954,4956,4958],{"class":56,"line":57},[54,4953,1640],{"class":60},[54,4955,1643],{"class":64},[54,4957,61],{"class":60},[54,4959,1648],{"class":64},[54,4961,4962],{"class":56,"line":68},[54,4963,72],{"emptyLinePlaceholder":71},[54,4965,4966,4968,4970,4972,4974,4976],{"class":56,"line":75},[54,4967,1657],{"class":64},[54,4969,82],{"class":81},[54,4971,1662],{"class":64},[54,4973,89],{"class":88},[54,4975,1667],{"class":92},[54,4977,1670],{"class":88},[54,4979,4980,4983,4985,4987,4989,4991,4993,4995,4997,4999,5002,5004],{"class":56,"line":112},[54,4981,4982],{"class":64},"filename_stamp ",[54,4984,82],{"class":81},[54,4986,2316],{"class":64},[54,4988,89],{"class":88},[54,4990,2704],{"class":92},[54,4992,96],{"class":88},[54,4994,545],{"class":544},[54,4996,2711],{"class":548},[54,4998,2714],{"class":99},[54,5000,5001],{"class":548},"_%H-%M-%S",[54,5003,545],{"class":544},[54,5005,109],{"class":88},[54,5007,5008,5010,5012,5015],{"class":56,"line":887},[54,5009,116],{"class":115},[54,5011,96],{"class":88},[54,5013,5014],{"class":92},"filename_stamp",[54,5016,109],{"class":88},[14,5018,561],{},[45,5020,5022],{"className":47,"code":5021,"language":49,"meta":50,"style":50},"2025-04-22_14-30-45\n",[17,5023,5024],{"__ignoreMap":50},[54,5025,5026,5028,5030,5032,5034,5036,5039,5041,5043,5045],{"class":56,"line":57},[54,5027,3679],{"class":99},[54,5029,1467],{"class":81},[54,5031,752],{"class":99},[54,5033,2389],{"class":2388},[54,5035,1467],{"class":81},[54,5037,5038],{"class":99},"22_14",[54,5040,1467],{"class":81},[54,5042,3696],{"class":99},[54,5044,1467],{"class":81},[54,5046,3701],{"class":99},[14,5048,5049,5050,89],{},"If you need the current date and time first, see ",[192,5051,5052],{"href":3535},"datetime.now() explained",[40,5054,5056],{"id":5055},"datetimestrftime-vs-datetimestrptime","datetime.strftime() vs datetime.strptime()",[14,5058,5059],{},"These two methods do opposite jobs:",[149,5061,5062,5070],{},[152,5063,5064,5066,5067,5069],{},[17,5065,2654],{}," turns a ",[17,5068,1628],{}," into a string",[152,5071,5072,5075,5076],{},[17,5073,5074],{},"strptime()"," turns a string into a ",[17,5077,1628],{},[14,5079,253],{},[45,5081,5083],{"className":47,"code":5082,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ndt = datetime(2025, 4, 22, 14, 30)\ntext = dt.strftime(\"%Y-%m-%d %H:%M\")\n\nprint(text)\n",[17,5084,5085,5095,5099,5130,5159,5163],{"__ignoreMap":50},[54,5086,5087,5089,5091,5093],{"class":56,"line":57},[54,5088,1640],{"class":60},[54,5090,1643],{"class":64},[54,5092,61],{"class":60},[54,5094,1648],{"class":64},[54,5096,5097],{"class":56,"line":68},[54,5098,72],{"emptyLinePlaceholder":71},[54,5100,5101,5104,5106,5108,5110,5112,5114,5116,5118,5120,5122,5124,5126,5128],{"class":56,"line":75},[54,5102,5103],{"class":64},"dt ",[54,5105,82],{"class":81},[54,5107,1662],{"class":92},[54,5109,96],{"class":88},[54,5111,3679],{"class":99},[54,5113,103],{"class":88},[54,5115,4264],{"class":99},[54,5117,103],{"class":88},[54,5119,4269],{"class":99},[54,5121,103],{"class":88},[54,5123,2397],{"class":99},[54,5125,103],{"class":88},[54,5127,3981],{"class":99},[54,5129,109],{"class":88},[54,5131,5132,5135,5137,5140,5142,5144,5146,5148,5150,5152,5155,5157],{"class":56,"line":112},[54,5133,5134],{"class":64},"text ",[54,5136,82],{"class":81},[54,5138,5139],{"class":64}," dt",[54,5141,89],{"class":88},[54,5143,2704],{"class":92},[54,5145,96],{"class":88},[54,5147,545],{"class":544},[54,5149,2711],{"class":548},[54,5151,2714],{"class":99},[54,5153,5154],{"class":548}," %H:%M",[54,5156,545],{"class":544},[54,5158,109],{"class":88},[54,5160,5161],{"class":56,"line":887},[54,5162,72],{"emptyLinePlaceholder":71},[54,5164,5165,5167,5169,5172],{"class":56,"line":892},[54,5166,116],{"class":115},[54,5168,96],{"class":88},[54,5170,5171],{"class":92},"text",[54,5173,109],{"class":88},[14,5175,449],{},[45,5177,5179],{"className":47,"code":5178,"language":49,"meta":50,"style":50},"2025-04-22 14:30\n",[17,5180,5181],{"__ignoreMap":50},[54,5182,5183,5185,5187,5189,5191,5193,5195,5197,5199],{"class":56,"line":57},[54,5184,3679],{"class":99},[54,5186,1467],{"class":81},[54,5188,752],{"class":99},[54,5190,2389],{"class":2388},[54,5192,1467],{"class":81},[54,5194,2394],{"class":99},[54,5196,2397],{"class":99},[54,5198,147],{"class":88},[54,5200,4133],{"class":99},[14,5202,5203,5204,89],{},"If you need to go the other way and convert text into a date, see ",[192,5205,5206],{"href":2838},"datetime.strptime() explained",[40,5208,5210],{"id":5209},"common-beginner-mistakes","Common beginner mistakes",[602,5212,5214],{"id":5213},"calling-strftime-on-a-plain-string","Calling strftime() on a plain string",[14,5216,5217,5218,5220],{},"This will fail because strings do not have a ",[17,5219,2654],{}," method.",[45,5222,5224],{"className":47,"code":5223,"language":49,"meta":50,"style":50},"value = \"2025-04-22\"\nprint(value.strftime(\"%Y-%m-%d\"))\n",[17,5225,5226,5240],{"__ignoreMap":50},[54,5227,5228,5231,5233,5235,5237],{"class":56,"line":57},[54,5229,5230],{"class":64},"value ",[54,5232,82],{"class":81},[54,5234,830],{"class":544},[54,5236,3561],{"class":548},[54,5238,5239],{"class":544},"\"\n",[54,5241,5242,5244,5246,5249,5251,5253,5255,5257,5259,5261,5263],{"class":56,"line":68},[54,5243,116],{"class":115},[54,5245,96],{"class":88},[54,5247,5248],{"class":92},"value",[54,5250,89],{"class":88},[54,5252,2704],{"class":92},[54,5254,96],{"class":88},[54,5256,545],{"class":544},[54,5258,2711],{"class":548},[54,5260,2714],{"class":99},[54,5262,545],{"class":544},[54,5264,382],{"class":88},[14,5266,5267],{},"You would get an error like:",[45,5269,5271],{"className":47,"code":5270,"language":49,"meta":50,"style":50},"AttributeError: 'str' object has no attribute 'strftime'\n",[17,5272,5273],{"__ignoreMap":50},[54,5274,5275,5278,5280,5282,5284,5286,5288,5291,5293,5295],{"class":56,"line":57},[54,5276,5277],{"class":429},"AttributeError",[54,5279,147],{"class":88},[54,5281,2234],{"class":544},[54,5283,3173],{"class":548},[54,5285,2240],{"class":544},[54,5287,1629],{"class":429},[54,5289,5290],{"class":64}," has no attribute ",[54,5292,2240],{"class":544},[54,5294,2704],{"class":548},[54,5296,5297],{"class":544},"'\n",[14,5299,5300,5301,5303,5304,5306,5307,89],{},"If your value is already text, you do not format it with ",[17,5302,2654],{},". You may need to parse it first with ",[17,5305,5074],{},". If you are unsure what a string is, see ",[192,5308,5310],{"href":5309},"\u002Fglossary\u002Fwhat-is-a-string-in-python","what is a string in Python",[602,5312,5314,5315,134,5317],{"id":5313},"mixing-up-m-and-m","Mixing up ",[17,5316,3788],{},[17,5318,4195],{},[14,5320,5321],{},"This is one of the most common mistakes.",[149,5323,5324,5329],{},[152,5325,5326,5328],{},[17,5327,3788],{}," = month",[152,5330,5331,4196],{},[17,5332,4195],{},[14,5334,253],{},[45,5336,5338],{"className":47,"code":5337,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime(2025, 4, 22, 14, 7)\n\nprint(now.strftime(\"%Y-%m-%d\"))  # correct month\nprint(now.strftime(\"%Y-%M-%d\"))  # wrong: %M means minute\n",[17,5339,5340,5350,5354,5384,5388,5415],{"__ignoreMap":50},[54,5341,5342,5344,5346,5348],{"class":56,"line":57},[54,5343,1640],{"class":60},[54,5345,1643],{"class":64},[54,5347,61],{"class":60},[54,5349,1648],{"class":64},[54,5351,5352],{"class":56,"line":68},[54,5353,72],{"emptyLinePlaceholder":71},[54,5355,5356,5358,5360,5362,5364,5366,5368,5370,5372,5374,5376,5378,5380,5382],{"class":56,"line":75},[54,5357,1657],{"class":64},[54,5359,82],{"class":81},[54,5361,1662],{"class":92},[54,5363,96],{"class":88},[54,5365,3679],{"class":99},[54,5367,103],{"class":88},[54,5369,4264],{"class":99},[54,5371,103],{"class":88},[54,5373,4269],{"class":99},[54,5375,103],{"class":88},[54,5377,2397],{"class":99},[54,5379,103],{"class":88},[54,5381,3967],{"class":99},[54,5383,109],{"class":88},[54,5385,5386],{"class":56,"line":112},[54,5387,72],{"emptyLinePlaceholder":71},[54,5389,5390,5392,5394,5396,5398,5400,5402,5404,5406,5408,5410,5412],{"class":56,"line":887},[54,5391,116],{"class":115},[54,5393,96],{"class":88},[54,5395,1667],{"class":92},[54,5397,89],{"class":88},[54,5399,2704],{"class":92},[54,5401,96],{"class":88},[54,5403,545],{"class":544},[54,5405,2711],{"class":548},[54,5407,2714],{"class":99},[54,5409,545],{"class":544},[54,5411,4310],{"class":88},[54,5413,5414],{"class":4313},"  # correct month\n",[54,5416,5417,5419,5421,5423,5425,5427,5429,5431,5434,5436,5438,5440],{"class":56,"line":892},[54,5418,116],{"class":115},[54,5420,96],{"class":88},[54,5422,1667],{"class":92},[54,5424,89],{"class":88},[54,5426,2704],{"class":92},[54,5428,96],{"class":88},[54,5430,545],{"class":544},[54,5432,5433],{"class":548},"%Y-%M-",[54,5435,2714],{"class":99},[54,5437,545],{"class":544},[54,5439,4310],{"class":88},[54,5441,5442],{"class":4313},"  # wrong: %M means minute\n",[14,5444,449],{},[45,5446,5448],{"className":47,"code":5447,"language":49,"meta":50,"style":50},"2025-04-22\n2025-07-22\n",[17,5449,5450,5464],{"__ignoreMap":50},[54,5451,5452,5454,5456,5458,5460,5462],{"class":56,"line":57},[54,5453,3679],{"class":99},[54,5455,1467],{"class":81},[54,5457,752],{"class":99},[54,5459,2389],{"class":2388},[54,5461,1467],{"class":81},[54,5463,2617],{"class":99},[54,5465,5466,5468,5470,5472,5474,5476],{"class":56,"line":68},[54,5467,3679],{"class":99},[54,5469,1467],{"class":81},[54,5471,752],{"class":99},[54,5473,4088],{"class":2388},[54,5475,1467],{"class":81},[54,5477,2617],{"class":99},[602,5479,5481,5482,5484,5485,5487],{"id":5480},"using-h-with-p-and-expecting-12-hour-output","Using ",[17,5483,4189],{}," with ",[17,5486,4213],{}," and expecting 12-hour output",[149,5489,5490,5495,5500],{},[152,5491,5492,5494],{},[17,5493,4189],{}," is 24-hour format",[152,5496,5497,5499],{},[17,5498,4207],{}," is 12-hour format",[152,5501,5502,2850,5504,5507,5508],{},[17,5503,4213],{},[17,5505,5506],{},"AM"," or ",[17,5509,5510],{},"PM",[14,5512,5513],{},"Wrong idea:",[45,5515,5517],{"className":47,"code":5516,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime(2025, 4, 22, 14, 30)\nprint(now.strftime(\"%H:%M %p\"))\n",[17,5518,5519,5529,5533,5563],{"__ignoreMap":50},[54,5520,5521,5523,5525,5527],{"class":56,"line":57},[54,5522,1640],{"class":60},[54,5524,1643],{"class":64},[54,5526,61],{"class":60},[54,5528,1648],{"class":64},[54,5530,5531],{"class":56,"line":68},[54,5532,72],{"emptyLinePlaceholder":71},[54,5534,5535,5537,5539,5541,5543,5545,5547,5549,5551,5553,5555,5557,5559,5561],{"class":56,"line":75},[54,5536,1657],{"class":64},[54,5538,82],{"class":81},[54,5540,1662],{"class":92},[54,5542,96],{"class":88},[54,5544,3679],{"class":99},[54,5546,103],{"class":88},[54,5548,4264],{"class":99},[54,5550,103],{"class":88},[54,5552,4269],{"class":99},[54,5554,103],{"class":88},[54,5556,2397],{"class":99},[54,5558,103],{"class":88},[54,5560,3981],{"class":99},[54,5562,109],{"class":88},[54,5564,5565,5567,5569,5571,5573,5575,5577,5579,5582,5584],{"class":56,"line":112},[54,5566,116],{"class":115},[54,5568,96],{"class":88},[54,5570,1667],{"class":92},[54,5572,89],{"class":88},[54,5574,2704],{"class":92},[54,5576,96],{"class":88},[54,5578,545],{"class":544},[54,5580,5581],{"class":548},"%H:%M %p",[54,5583,545],{"class":544},[54,5585,382],{"class":88},[14,5587,449],{},[45,5589,5591],{"className":47,"code":5590,"language":49,"meta":50,"style":50},"14:30 PM\n",[17,5592,5593],{"__ignoreMap":50},[54,5594,5595,5597,5599,5601],{"class":56,"line":57},[54,5596,2812],{"class":99},[54,5598,147],{"class":88},[54,5600,3696],{"class":99},[54,5602,4871],{"class":4870},[14,5604,5605],{},"Better:",[45,5607,5609],{"className":47,"code":5608,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime(2025, 4, 22, 14, 30)\nprint(now.strftime(\"%I:%M %p\"))\n",[17,5610,5611,5621,5625,5655],{"__ignoreMap":50},[54,5612,5613,5615,5617,5619],{"class":56,"line":57},[54,5614,1640],{"class":60},[54,5616,1643],{"class":64},[54,5618,61],{"class":60},[54,5620,1648],{"class":64},[54,5622,5623],{"class":56,"line":68},[54,5624,72],{"emptyLinePlaceholder":71},[54,5626,5627,5629,5631,5633,5635,5637,5639,5641,5643,5645,5647,5649,5651,5653],{"class":56,"line":75},[54,5628,1657],{"class":64},[54,5630,82],{"class":81},[54,5632,1662],{"class":92},[54,5634,96],{"class":88},[54,5636,3679],{"class":99},[54,5638,103],{"class":88},[54,5640,4264],{"class":99},[54,5642,103],{"class":88},[54,5644,4269],{"class":99},[54,5646,103],{"class":88},[54,5648,2397],{"class":99},[54,5650,103],{"class":88},[54,5652,3981],{"class":99},[54,5654,109],{"class":88},[54,5656,5657,5659,5661,5663,5665,5667,5669,5671,5673,5675],{"class":56,"line":112},[54,5658,116],{"class":115},[54,5660,96],{"class":88},[54,5662,1667],{"class":92},[54,5664,89],{"class":88},[54,5666,2704],{"class":92},[54,5668,96],{"class":88},[54,5670,545],{"class":544},[54,5672,4846],{"class":548},[54,5674,545],{"class":544},[54,5676,382],{"class":88},[14,5678,449],{},[45,5680,5681],{"className":47,"code":4855,"language":49,"meta":50,"style":50},[17,5682,5683],{"__ignoreMap":50},[54,5684,5685,5687,5689,5691,5693],{"class":56,"line":57},[54,5686,752],{"class":99},[54,5688,396],{"class":2388},[54,5690,147],{"class":88},[54,5692,3696],{"class":99},[54,5694,4871],{"class":4870},[602,5696,5698],{"id":5697},"forgetting-that-the-result-is-a-string","Forgetting that the result is a string",[14,5700,5701],{},"After formatting, the value is text.",[45,5703,5705],{"className":47,"code":5704,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nformatted = now.strftime(\"%Y-%m-%d\")\n\nprint(type(formatted))\n",[17,5706,5707,5717,5721,5735,5759,5763],{"__ignoreMap":50},[54,5708,5709,5711,5713,5715],{"class":56,"line":57},[54,5710,1640],{"class":60},[54,5712,1643],{"class":64},[54,5714,61],{"class":60},[54,5716,1648],{"class":64},[54,5718,5719],{"class":56,"line":68},[54,5720,72],{"emptyLinePlaceholder":71},[54,5722,5723,5725,5727,5729,5731,5733],{"class":56,"line":75},[54,5724,1657],{"class":64},[54,5726,82],{"class":81},[54,5728,1662],{"class":64},[54,5730,89],{"class":88},[54,5732,1667],{"class":92},[54,5734,1670],{"class":88},[54,5736,5737,5739,5741,5743,5745,5747,5749,5751,5753,5755,5757],{"class":56,"line":112},[54,5738,3634],{"class":64},[54,5740,82],{"class":81},[54,5742,2316],{"class":64},[54,5744,89],{"class":88},[54,5746,2704],{"class":92},[54,5748,96],{"class":88},[54,5750,545],{"class":544},[54,5752,2711],{"class":548},[54,5754,2714],{"class":99},[54,5756,545],{"class":544},[54,5758,109],{"class":88},[54,5760,5761],{"class":56,"line":887},[54,5762,72],{"emptyLinePlaceholder":71},[54,5764,5765,5767,5769,5771,5773,5775],{"class":56,"line":892},[54,5766,116],{"class":115},[54,5768,96],{"class":88},[54,5770,2208],{"class":429},[54,5772,96],{"class":88},[54,5774,3665],{"class":92},[54,5776,382],{"class":88},[14,5778,449],{},[45,5780,5782],{"className":47,"code":5781,"language":49,"meta":50,"style":50},"\u003Cclass 'str'>\n",[17,5783,5784],{"__ignoreMap":50},[54,5785,5786,5788,5790,5792,5794,5796],{"class":56,"line":57},[54,5787,2227],{"class":81},[54,5789,2231],{"class":2230},[54,5791,2234],{"class":544},[54,5793,3173],{"class":548},[54,5795,2240],{"class":544},[54,5797,2243],{"class":81},[14,5799,5800],{},"That means you cannot use datetime-specific methods on the result unless you convert it back.",[602,5802,5804],{"id":5803},"using-unsupported-or-mistyped-format-codes","Using unsupported or mistyped format codes",[14,5806,5807],{},"If your output looks wrong, check your format string carefully. A small typo can change the result.",[14,5809,5810],{},"Useful checks:",[45,5812,5814],{"className":47,"code":5813,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\n\nprint(type(now))\nprint(now)\nprint(now.strftime(\"%Y-%m-%d\"))\nprint(now.strftime(\"%H:%M:%S\"))\nhelp(datetime.strftime)\n",[17,5815,5816,5826,5830,5844,5848,5862,5872,5896,5918],{"__ignoreMap":50},[54,5817,5818,5820,5822,5824],{"class":56,"line":57},[54,5819,1640],{"class":60},[54,5821,1643],{"class":64},[54,5823,61],{"class":60},[54,5825,1648],{"class":64},[54,5827,5828],{"class":56,"line":68},[54,5829,72],{"emptyLinePlaceholder":71},[54,5831,5832,5834,5836,5838,5840,5842],{"class":56,"line":75},[54,5833,1657],{"class":64},[54,5835,82],{"class":81},[54,5837,1662],{"class":64},[54,5839,89],{"class":88},[54,5841,1667],{"class":92},[54,5843,1670],{"class":88},[54,5845,5846],{"class":56,"line":112},[54,5847,72],{"emptyLinePlaceholder":71},[54,5849,5850,5852,5854,5856,5858,5860],{"class":56,"line":887},[54,5851,116],{"class":115},[54,5853,96],{"class":88},[54,5855,2208],{"class":429},[54,5857,96],{"class":88},[54,5859,1667],{"class":92},[54,5861,382],{"class":88},[54,5863,5864,5866,5868,5870],{"class":56,"line":892},[54,5865,116],{"class":115},[54,5867,96],{"class":88},[54,5869,1667],{"class":92},[54,5871,109],{"class":88},[54,5873,5874,5876,5878,5880,5882,5884,5886,5888,5890,5892,5894],{"class":56,"line":913},[54,5875,116],{"class":115},[54,5877,96],{"class":88},[54,5879,1667],{"class":92},[54,5881,89],{"class":88},[54,5883,2704],{"class":92},[54,5885,96],{"class":88},[54,5887,545],{"class":544},[54,5889,2711],{"class":548},[54,5891,2714],{"class":99},[54,5893,545],{"class":544},[54,5895,382],{"class":88},[54,5897,5898,5900,5902,5904,5906,5908,5910,5912,5914,5916],{"class":56,"line":2121},[54,5899,116],{"class":115},[54,5901,96],{"class":88},[54,5903,1667],{"class":92},[54,5905,89],{"class":88},[54,5907,2704],{"class":92},[54,5909,96],{"class":88},[54,5911,545],{"class":544},[54,5913,2796],{"class":548},[54,5915,545],{"class":544},[54,5917,382],{"class":88},[54,5919,5920,5923,5925,5927,5929,5931],{"class":56,"line":2136},[54,5921,5922],{"class":115},"help",[54,5924,96],{"class":88},[54,5926,1628],{"class":92},[54,5928,89],{"class":88},[54,5930,2704],{"class":1693},[54,5932,109],{"class":88},[14,5934,5935],{},"Common causes of problems include:",[149,5937,5938,5941,5944,5947],{},[152,5939,5940],{},"Using a format code with the wrong meaning",[152,5942,5943],{},"Trying to format a value that is already a string",[152,5945,5946],{},"Confusing formatting dates with parsing dates",[152,5948,5949,5950,5952,5953,1629],{},"Expecting ",[17,5951,2654],{}," to change the original ",[17,5954,1628],{},[40,5956,5958],{"id":5957},"when-to-use-strftime","When to use strftime()",[14,5960,126,5961,5963],{},[17,5962,2654],{}," when you want to:",[149,5965,5966,5969,5972,5975],{},[152,5967,5968],{},"Display dates in a readable format",[152,5970,5971],{},"Build timestamps for logs",[152,5973,5974],{},"Create filenames with the current date and time",[152,5976,5977],{},"Prepare date text for reports or messages",[14,5979,5980],{},"It is a formatting tool. It does not create the date. It only changes how the date is shown as text.",[40,5982,1419],{"id":1418},[602,5984,5986],{"id":5985},"what-does-strftime-mean-in-python","What does strftime mean in Python?",[14,5988,5989],{},"It means “string format time.” It formats a date or datetime object as a string.",[602,5991,5993],{"id":5992},"what-is-the-difference-between-strftime-and-strptime","What is the difference between strftime() and strptime()?",[14,5995,5996,5998,5999,6001],{},[17,5997,2654],{}," formats a datetime as text. ",[17,6000,5074],{}," parses text into a datetime.",[602,6003,6005],{"id":6004},"does-strftime-return-a-string","Does strftime() return a string?",[14,6007,6008,6009,6011],{},"Yes. The result of ",[17,6010,2654],{}," is always a string.",[602,6013,6015],{"id":6014},"can-i-use-strftime-with-date-objects","Can I use strftime() with date objects?",[14,6017,6018,6019,320,6021,3587,6023,6025],{},"Yes. You can use it with ",[17,6020,1628],{},[17,6022,2535],{},[17,6024,2559],{}," objects.",[602,6027,6029],{"id":6028},"why-is-my-month-wrong-when-using-strftime","Why is my month wrong when using strftime()?",[14,6031,6032,6033,6035,6036,6038],{},"You may have used ",[17,6034,4195],{},", which means minutes. Use ",[17,6037,3788],{}," for month.",[40,6040,1510],{"id":1509},[149,6042,6043,6047,6051,6055,6059],{},[152,6044,6045],{},[192,6046,1774],{"href":1773},[152,6048,6049],{},[192,6050,5052],{"href":3535},[152,6052,6053],{},[192,6054,5206],{"href":2838},[152,6056,6057],{},[192,6058,3485],{"href":3484},[152,6060,6061],{},[192,6062,6063],{"href":5309},"What is a string in Python",[1545,6065,6066],{},"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 .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 .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 .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":50,"searchDepth":68,"depth":68,"links":6068},[6069,6070,6071,6072,6073,6074,6082,6083,6092,6093,6100],{"id":3595,"depth":68,"text":3596},{"id":3712,"depth":68,"text":3713},{"id":198,"depth":68,"text":199},{"id":492,"depth":68,"text":493},{"id":4160,"depth":68,"text":4161},{"id":4546,"depth":68,"text":4547,"children":6075},[6076,6077,6078,6079,6080,6081],{"id":4553,"depth":75,"text":4554},{"id":4634,"depth":75,"text":4635},{"id":4717,"depth":75,"text":2622},{"id":4793,"depth":75,"text":4794},{"id":4874,"depth":75,"text":4875},{"id":4937,"depth":75,"text":4938},{"id":5055,"depth":68,"text":5056},{"id":5209,"depth":68,"text":5210,"children":6084},[6085,6086,6088,6090,6091],{"id":5213,"depth":75,"text":5214},{"id":5313,"depth":75,"text":6087},"Mixing up %m and %M",{"id":5480,"depth":75,"text":6089},"Using %H with %p and expecting 12-hour output",{"id":5697,"depth":75,"text":5698},{"id":5803,"depth":75,"text":5804},{"id":5957,"depth":68,"text":5958},{"id":1418,"depth":68,"text":1419,"children":6094},[6095,6096,6097,6098,6099],{"id":5985,"depth":75,"text":5986},{"id":5992,"depth":75,"text":5993},{"id":6004,"depth":75,"text":6005},{"id":6014,"depth":75,"text":6015},{"id":6028,"depth":75,"text":6029},{"id":1509,"depth":68,"text":1510},"Master datetime.strftime explained in our comprehensive Python beginner guide.",{},{"title":3541,"description":6101},"standard-library\u002Fdatetime.strftime-explained","dv5GD_RHfVTTjWXyqpYfoQUwnHggwP29xeotzOPD9X0",{"id":6107,"title":6108,"body":6109,"description":8173,"extension":1577,"meta":8174,"navigation":71,"path":2838,"seo":8175,"stem":8176,"__hash__":8177},"content\u002Fstandard-library\u002Fdatetime.strptime-explained.md","datetime.strptime() Explained",{"type":7,"value":6110,"toc":8145},[6111,6116,6127,6130,6147,6150,6158,6169,6171,6263,6265,6311,6318,6323,6327,6340,6343,6352,6354,6379,6382,6395,6399,6401,6476,6478,6510,6517,6521,6524,6597,6599,6627,6633,6635,6650,6653,6671,6680,6684,6689,6731,6734,6751,6753,6831,6833,6863,6867,6873,6893,6896,6912,6915,6929,6932,6945,6948,7022,7024,7052,7057,7061,7064,7067,7110,7112,7218,7220,7260,7263,7277,7281,7286,7289,7292,7352,7360,7363,7386,7389,7399,7402,7416,7423,7427,7433,7436,7457,7460,7522,7528,7531,7656,7659,7776,7778,7812,7820,7823,7840,7842,7953,7955,7998,8005,8007,8010,8013,8047,8050,8052,8057,8061,8069,8072,8080,8088,8095,8098,8105,8118,8120,8142],[10,6112,6114,1595],{"id":6113},"datetimestrptime-explained",[17,6115,2841],{},[14,6117,6118,6120,6121,6124,6125,1732],{},[17,6119,2841],{}," turns a date or time ",[35,6122,6123],{},"string"," into a real ",[17,6126,1628],{},[14,6128,6129],{},"This is useful when your date comes from:",[149,6131,6132,6135,6138,6141,6144],{},[152,6133,6134],{},"user input",[152,6136,6137],{},"a file",[152,6139,6140],{},"a CSV",[152,6142,6143],{},"an API response",[152,6145,6146],{},"a log entry",[14,6148,6149],{},"The important rule is simple:",[149,6151,6152],{},[152,6153,6154,6155],{},"the ",[35,6156,6157],{},"text must match the format string exactly",[14,6159,6160,6161,6164,6165,89],{},"This page focuses on ",[35,6162,6163],{},"parsing strings into datetime objects",". If you want to go the other way and turn a datetime object into text, see ",[192,6166,6167,2834],{"href":2830},[17,6168,2833],{},[40,6170,3596],{"id":3595},[45,6172,6174],{"className":47,"code":6173,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"2024-01-31 14:45\"\ndt = datetime.strptime(text, \"%Y-%m-%d %H:%M\")\n\nprint(dt)\nprint(type(dt))\n",[17,6175,6176,6186,6190,6203,6234,6238,6249],{"__ignoreMap":50},[54,6177,6178,6180,6182,6184],{"class":56,"line":57},[54,6179,1640],{"class":60},[54,6181,1643],{"class":64},[54,6183,61],{"class":60},[54,6185,1648],{"class":64},[54,6187,6188],{"class":56,"line":68},[54,6189,72],{"emptyLinePlaceholder":71},[54,6191,6192,6194,6196,6198,6201],{"class":56,"line":75},[54,6193,5134],{"class":64},[54,6195,82],{"class":81},[54,6197,830],{"class":544},[54,6199,6200],{"class":548},"2024-01-31 14:45",[54,6202,5239],{"class":544},[54,6204,6205,6207,6209,6211,6213,6216,6218,6220,6222,6224,6226,6228,6230,6232],{"class":56,"line":112},[54,6206,5103],{"class":64},[54,6208,82],{"class":81},[54,6210,1662],{"class":64},[54,6212,89],{"class":88},[54,6214,6215],{"class":92},"strptime",[54,6217,96],{"class":88},[54,6219,5171],{"class":92},[54,6221,103],{"class":88},[54,6223,830],{"class":544},[54,6225,2711],{"class":548},[54,6227,2714],{"class":99},[54,6229,5154],{"class":548},[54,6231,545],{"class":544},[54,6233,109],{"class":88},[54,6235,6236],{"class":56,"line":887},[54,6237,72],{"emptyLinePlaceholder":71},[54,6239,6240,6242,6244,6247],{"class":56,"line":892},[54,6241,116],{"class":115},[54,6243,96],{"class":88},[54,6245,6246],{"class":92},"dt",[54,6248,109],{"class":88},[54,6250,6251,6253,6255,6257,6259,6261],{"class":56,"line":913},[54,6252,116],{"class":115},[54,6254,96],{"class":88},[54,6256,2208],{"class":429},[54,6258,96],{"class":88},[54,6260,6246],{"class":92},[54,6262,382],{"class":88},[14,6264,449],{},[45,6266,6268],{"className":47,"code":6267,"language":49,"meta":50,"style":50},"2024-01-31 14:45:00\n\u003Cclass 'datetime.datetime'>\n",[17,6269,6270,6297],{"__ignoreMap":50},[54,6271,6272,6275,6277,6279,6281,6283,6286,6288,6290,6293,6295],{"class":56,"line":57},[54,6273,6274],{"class":99},"2024",[54,6276,1467],{"class":81},[54,6278,752],{"class":99},[54,6280,100],{"class":2388},[54,6282,1467],{"class":81},[54,6284,6285],{"class":99},"31",[54,6287,2397],{"class":99},[54,6289,147],{"class":88},[54,6291,6292],{"class":99},"45",[54,6294,147],{"class":88},[54,6296,4105],{"class":99},[54,6298,6299,6301,6303,6305,6307,6309],{"class":56,"line":68},[54,6300,2227],{"class":81},[54,6302,2231],{"class":2230},[54,6304,2234],{"class":544},[54,6306,2237],{"class":548},[54,6308,2240],{"class":544},[54,6310,2243],{"class":81},[14,6312,126,6313,6315,6316,1732],{},[17,6314,5074],{}," when you have a date as a string and need a real ",[17,6317,1628],{},[40,6319,1736,6321,1739],{"id":6320},"what-datetimestrptime-does",[17,6322,2841],{},[14,6324,6325,147],{},[17,6326,2841],{},[149,6328,6329,6334,6337],{},[152,6330,6331,6332,1629],{},"converts a string into a ",[17,6333,1628],{},[152,6335,6336],{},"reads the string using a format pattern",[152,6338,6339],{},"requires the string and format to match exactly",[14,6341,6342],{},"A common use case is parsing dates from files, APIs, or user input before working with them in Python.",[14,6344,6345,6346,6348,6349,89],{},"If you are new to the ",[17,6347,1628],{}," module, see the ",[192,6350,1774],{"href":6351},"\u002Fstandard-library\u002Fpython-datetime-module-overview\u002F",[40,6353,199],{"id":198},[45,6355,6357],{"className":47,"code":6356,"language":49,"meta":50,"style":50},"datetime.strptime(date_string, format)\n",[17,6358,6359],{"__ignoreMap":50},[54,6360,6361,6363,6365,6367,6369,6372,6374,6377],{"class":56,"line":57},[54,6362,1628],{"class":64},[54,6364,89],{"class":88},[54,6366,6215],{"class":92},[54,6368,96],{"class":88},[54,6370,6371],{"class":92},"date_string",[54,6373,103],{"class":88},[54,6375,6376],{"class":115}," format",[54,6378,109],{"class":88},[14,6380,6381],{},"Parts:",[149,6383,6384,6389],{},[152,6385,6386,6388],{},[17,6387,6371],{}," is the text you want to parse",[152,6390,6391,6394],{},[17,6392,6393],{},"format"," tells Python how that text is arranged",[14,6396,3396,6397,1732],{},[17,6398,2237],{},[14,6400,253],{},[45,6402,6404],{"className":47,"code":6403,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ndate_string = \"2024-08-15\"\ndt = datetime.strptime(date_string, \"%Y-%m-%d\")\n\nprint(dt)\n",[17,6405,6406,6416,6420,6434,6462,6466],{"__ignoreMap":50},[54,6407,6408,6410,6412,6414],{"class":56,"line":57},[54,6409,1640],{"class":60},[54,6411,1643],{"class":64},[54,6413,61],{"class":60},[54,6415,1648],{"class":64},[54,6417,6418],{"class":56,"line":68},[54,6419,72],{"emptyLinePlaceholder":71},[54,6421,6422,6425,6427,6429,6432],{"class":56,"line":75},[54,6423,6424],{"class":64},"date_string ",[54,6426,82],{"class":81},[54,6428,830],{"class":544},[54,6430,6431],{"class":548},"2024-08-15",[54,6433,5239],{"class":544},[54,6435,6436,6438,6440,6442,6444,6446,6448,6450,6452,6454,6456,6458,6460],{"class":56,"line":112},[54,6437,5103],{"class":64},[54,6439,82],{"class":81},[54,6441,1662],{"class":64},[54,6443,89],{"class":88},[54,6445,6215],{"class":92},[54,6447,96],{"class":88},[54,6449,6371],{"class":92},[54,6451,103],{"class":88},[54,6453,830],{"class":544},[54,6455,2711],{"class":548},[54,6457,2714],{"class":99},[54,6459,545],{"class":544},[54,6461,109],{"class":88},[54,6463,6464],{"class":56,"line":887},[54,6465,72],{"emptyLinePlaceholder":71},[54,6467,6468,6470,6472,6474],{"class":56,"line":892},[54,6469,116],{"class":115},[54,6471,96],{"class":88},[54,6473,6246],{"class":92},[54,6475,109],{"class":88},[14,6477,449],{},[45,6479,6481],{"className":47,"code":6480,"language":49,"meta":50,"style":50},"2024-08-15 00:00:00\n",[17,6482,6483],{"__ignoreMap":50},[54,6484,6485,6487,6489,6491,6493,6495,6498,6501,6503,6506,6508],{"class":56,"line":57},[54,6486,6274],{"class":99},[54,6488,1467],{"class":81},[54,6490,752],{"class":99},[54,6492,327],{"class":2388},[54,6494,1467],{"class":81},[54,6496,6497],{"class":99},"15",[54,6499,6500],{"class":99}," 00",[54,6502,147],{"class":88},[54,6504,6505],{"class":99},"00",[54,6507,147],{"class":88},[54,6509,4105],{"class":99},[14,6511,6512,6513,6516],{},"Notice that the time becomes ",[17,6514,6515],{},"00:00:00"," if your string only contains a date.",[40,6518,6520],{"id":6519},"a-simple-example","A simple example",[14,6522,6523],{},"Here is a basic date string:",[45,6525,6527],{"className":47,"code":6526,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"2024-08-15\"\ndt = datetime.strptime(text, \"%Y-%m-%d\")\n\nprint(dt)\n",[17,6528,6529,6539,6543,6555,6583,6587],{"__ignoreMap":50},[54,6530,6531,6533,6535,6537],{"class":56,"line":57},[54,6532,1640],{"class":60},[54,6534,1643],{"class":64},[54,6536,61],{"class":60},[54,6538,1648],{"class":64},[54,6540,6541],{"class":56,"line":68},[54,6542,72],{"emptyLinePlaceholder":71},[54,6544,6545,6547,6549,6551,6553],{"class":56,"line":75},[54,6546,5134],{"class":64},[54,6548,82],{"class":81},[54,6550,830],{"class":544},[54,6552,6431],{"class":548},[54,6554,5239],{"class":544},[54,6556,6557,6559,6561,6563,6565,6567,6569,6571,6573,6575,6577,6579,6581],{"class":56,"line":112},[54,6558,5103],{"class":64},[54,6560,82],{"class":81},[54,6562,1662],{"class":64},[54,6564,89],{"class":88},[54,6566,6215],{"class":92},[54,6568,96],{"class":88},[54,6570,5171],{"class":92},[54,6572,103],{"class":88},[54,6574,830],{"class":544},[54,6576,2711],{"class":548},[54,6578,2714],{"class":99},[54,6580,545],{"class":544},[54,6582,109],{"class":88},[54,6584,6585],{"class":56,"line":887},[54,6586,72],{"emptyLinePlaceholder":71},[54,6588,6589,6591,6593,6595],{"class":56,"line":892},[54,6590,116],{"class":115},[54,6592,96],{"class":88},[54,6594,6246],{"class":92},[54,6596,109],{"class":88},[14,6598,449],{},[45,6600,6601],{"className":47,"code":6480,"language":49,"meta":50,"style":50},[17,6602,6603],{"__ignoreMap":50},[54,6604,6605,6607,6609,6611,6613,6615,6617,6619,6621,6623,6625],{"class":56,"line":57},[54,6606,6274],{"class":99},[54,6608,1467],{"class":81},[54,6610,752],{"class":99},[54,6612,327],{"class":2388},[54,6614,1467],{"class":81},[54,6616,6497],{"class":99},[54,6618,6500],{"class":99},[54,6620,147],{"class":88},[54,6622,6505],{"class":99},[54,6624,147],{"class":88},[54,6626,4105],{"class":99},[14,6628,6629,6630,89],{},"The format string is ",[17,6631,6632],{},"%Y-%m-%d",[14,6634,343],{},[149,6636,6637,6641,6646],{},[152,6638,6639,4174],{},[17,6640,3785],{},[152,6642,6643,6645],{},[17,6644,3788],{}," = month number",[152,6647,6648,4184],{},[17,6649,2714],{},[14,6651,6652],{},"So Python reads:",[149,6654,6655,6660,6666],{},[152,6656,6657,6659],{},[17,6658,6274],{}," as the year",[152,6661,6662,6665],{},[17,6663,6664],{},"08"," as the month",[152,6667,6668,6670],{},[17,6669,6497],{}," as the day",[14,6672,6673,6674,6677,6678,1132],{},"The dashes also matter. If your string uses ",[17,6675,6676],{},"\u002F",", your format must use ",[17,6679,6676],{},[40,6681,6683],{"id":6682},"common-format-codes-beginners-need","Common format codes beginners need",[14,6685,6686,6687,147],{},"These are the format codes most beginners use with ",[17,6688,5074],{},[149,6690,6691,6695,6701,6705,6709,6714,6718,6722,6727],{},[152,6692,6693,4174],{},[17,6694,3785],{},[152,6696,6697,6700],{},[17,6698,6699],{},"%y"," = 2-digit year",[152,6702,6703,4179],{},[17,6704,3788],{},[152,6706,6707,4184],{},[17,6708,2714],{},[152,6710,6711,6713],{},[17,6712,4189],{}," = hour in 24-hour time",[152,6715,6716,4196],{},[17,6717,4195],{},[152,6719,6720,4202],{},[17,6721,4201],{},[152,6723,6724,6726],{},[17,6725,4207],{}," = hour in 12-hour time",[152,6728,6729,4214],{},[17,6730,4213],{},[14,6732,6733],{},"You must also include separators exactly as they appear in the string, such as:",[149,6735,6736,6740,6744,6748],{},[152,6737,6738],{},[17,6739,1467],{},[152,6741,6742],{},[17,6743,6676],{},[152,6745,6746],{},[17,6747,147],{},[152,6749,6750],{},"spaces",[14,6752,253],{},[45,6754,6756],{"className":47,"code":6755,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"08\u002F15\u002F2024 09:30 PM\"\ndt = datetime.strptime(text, \"%m\u002F%d\u002F%Y %I:%M %p\")\n\nprint(dt)\n",[17,6757,6758,6768,6772,6785,6817,6821],{"__ignoreMap":50},[54,6759,6760,6762,6764,6766],{"class":56,"line":57},[54,6761,1640],{"class":60},[54,6763,1643],{"class":64},[54,6765,61],{"class":60},[54,6767,1648],{"class":64},[54,6769,6770],{"class":56,"line":68},[54,6771,72],{"emptyLinePlaceholder":71},[54,6773,6774,6776,6778,6780,6783],{"class":56,"line":75},[54,6775,5134],{"class":64},[54,6777,82],{"class":81},[54,6779,830],{"class":544},[54,6781,6782],{"class":548},"08\u002F15\u002F2024 09:30 PM",[54,6784,5239],{"class":544},[54,6786,6787,6789,6791,6793,6795,6797,6799,6801,6803,6805,6808,6810,6813,6815],{"class":56,"line":112},[54,6788,5103],{"class":64},[54,6790,82],{"class":81},[54,6792,1662],{"class":64},[54,6794,89],{"class":88},[54,6796,6215],{"class":92},[54,6798,96],{"class":88},[54,6800,5171],{"class":92},[54,6802,103],{"class":88},[54,6804,830],{"class":544},[54,6806,6807],{"class":548},"%m\u002F",[54,6809,2714],{"class":99},[54,6811,6812],{"class":548},"\u002F%Y %I:%M %p",[54,6814,545],{"class":544},[54,6816,109],{"class":88},[54,6818,6819],{"class":56,"line":887},[54,6820,72],{"emptyLinePlaceholder":71},[54,6822,6823,6825,6827,6829],{"class":56,"line":892},[54,6824,116],{"class":115},[54,6826,96],{"class":88},[54,6828,6246],{"class":92},[54,6830,109],{"class":88},[14,6832,449],{},[45,6834,6836],{"className":47,"code":6835,"language":49,"meta":50,"style":50},"2024-08-15 21:30:00\n",[17,6837,6838],{"__ignoreMap":50},[54,6839,6840,6842,6844,6846,6848,6850,6852,6855,6857,6859,6861],{"class":56,"line":57},[54,6841,6274],{"class":99},[54,6843,1467],{"class":81},[54,6845,752],{"class":99},[54,6847,327],{"class":2388},[54,6849,1467],{"class":81},[54,6851,6497],{"class":99},[54,6853,6854],{"class":99}," 21",[54,6856,147],{"class":88},[54,6858,3696],{"class":99},[54,6860,147],{"class":88},[54,6862,4105],{"class":99},[40,6864,6866],{"id":6865},"matching-the-string-to-the-format","Matching the string to the format",[14,6868,6869,6870,6872],{},"When using ",[17,6871,5074],{},", the string and format must match in three ways:",[149,6874,6875,6881,6886],{},[152,6876,6154,6877,6880],{},[35,6878,6879],{},"order"," must match",[152,6882,6154,6883,6880],{},[35,6884,6885],{},"separators",[152,6887,6888,6889,6892],{},"any ",[35,6890,6891],{},"extra text"," can cause an error",[14,6894,6895],{},"For example, if the string is:",[45,6897,6899],{"className":47,"code":6898,"language":49,"meta":50,"style":50},"\"15\u002F08\u002F2024\"\n",[17,6900,6901],{"__ignoreMap":50},[54,6902,6903,6906,6910],{"class":56,"line":57},[54,6904,545],{"class":6905},"s2W-s",[54,6907,6909],{"class":6908},"sithA","15\u002F08\u002F2024",[54,6911,5239],{"class":6905},[14,6913,6914],{},"the correct format is:",[45,6916,6918],{"className":47,"code":6917,"language":49,"meta":50,"style":50},"\"%d\u002F%m\u002F%Y\"\n",[17,6919,6920],{"__ignoreMap":50},[54,6921,6922,6924,6927],{"class":56,"line":57},[54,6923,545],{"class":6905},[54,6925,6926],{"class":6908},"%d\u002F%m\u002F%Y",[54,6928,5239],{"class":6905},[14,6930,6931],{},"not:",[45,6933,6935],{"className":47,"code":6934,"language":49,"meta":50,"style":50},"\"%Y-%m-%d\"\n",[17,6936,6937],{"__ignoreMap":50},[54,6938,6939,6941,6943],{"class":56,"line":57},[54,6940,545],{"class":6905},[54,6942,6632],{"class":6908},[54,6944,5239],{"class":6905},[14,6946,6947],{},"Compare these examples:",[45,6949,6951],{"className":47,"code":6950,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"15\u002F08\u002F2024\"\ndt = datetime.strptime(text, \"%d\u002F%m\u002F%Y\")\n\nprint(dt)\n",[17,6952,6953,6963,6967,6979,7008,7012],{"__ignoreMap":50},[54,6954,6955,6957,6959,6961],{"class":56,"line":57},[54,6956,1640],{"class":60},[54,6958,1643],{"class":64},[54,6960,61],{"class":60},[54,6962,1648],{"class":64},[54,6964,6965],{"class":56,"line":68},[54,6966,72],{"emptyLinePlaceholder":71},[54,6968,6969,6971,6973,6975,6977],{"class":56,"line":75},[54,6970,5134],{"class":64},[54,6972,82],{"class":81},[54,6974,830],{"class":544},[54,6976,6909],{"class":548},[54,6978,5239],{"class":544},[54,6980,6981,6983,6985,6987,6989,6991,6993,6995,6997,6999,7001,7004,7006],{"class":56,"line":112},[54,6982,5103],{"class":64},[54,6984,82],{"class":81},[54,6986,1662],{"class":64},[54,6988,89],{"class":88},[54,6990,6215],{"class":92},[54,6992,96],{"class":88},[54,6994,5171],{"class":92},[54,6996,103],{"class":88},[54,6998,830],{"class":544},[54,7000,2714],{"class":99},[54,7002,7003],{"class":548},"\u002F%m\u002F%Y",[54,7005,545],{"class":544},[54,7007,109],{"class":88},[54,7009,7010],{"class":56,"line":887},[54,7011,72],{"emptyLinePlaceholder":71},[54,7013,7014,7016,7018,7020],{"class":56,"line":892},[54,7015,116],{"class":115},[54,7017,96],{"class":88},[54,7019,6246],{"class":92},[54,7021,109],{"class":88},[14,7023,449],{},[45,7025,7026],{"className":47,"code":6480,"language":49,"meta":50,"style":50},[17,7027,7028],{"__ignoreMap":50},[54,7029,7030,7032,7034,7036,7038,7040,7042,7044,7046,7048,7050],{"class":56,"line":57},[54,7031,6274],{"class":99},[54,7033,1467],{"class":81},[54,7035,752],{"class":99},[54,7037,327],{"class":2388},[54,7039,1467],{"class":81},[54,7041,6497],{"class":99},[54,7043,6500],{"class":99},[54,7045,147],{"class":88},[54,7047,6505],{"class":99},[54,7049,147],{"class":88},[54,7051,4105],{"class":99},[14,7053,7054,7055,89],{},"If you use the wrong format, Python raises a ",[17,7056,1306],{},[40,7058,7060],{"id":7059},"parsing-date-and-time-together","Parsing date and time together",[14,7062,7063],{},"You can parse both date and time in the same string.",[14,7065,7066],{},"A common pattern is:",[45,7068,7070],{"className":47,"code":7069,"language":49,"meta":50,"style":50},"%Y-%m-%d %H:%M:%S\n",[17,7071,7072],{"__ignoreMap":50},[54,7073,7074,7077,7080,7083,7086,7088,7091,7093,7096,7098,7100,7103,7105,7107],{"class":56,"line":57},[54,7075,7076],{"class":81},"%",[54,7078,7079],{"class":64},"Y",[54,7081,7082],{"class":81},"-%",[54,7084,7085],{"class":64},"m",[54,7087,7082],{"class":81},[54,7089,7090],{"class":64},"d ",[54,7092,7076],{"class":81},[54,7094,7095],{"class":64},"H",[54,7097,147],{"class":88},[54,7099,7076],{"class":81},[54,7101,7102],{"class":64},"M",[54,7104,147],{"class":88},[54,7106,7076],{"class":81},[54,7108,7109],{"class":64},"S\n",[14,7111,253],{},[45,7113,7115],{"className":47,"code":7114,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntimestamp = \"2024-08-15 16:45:30\"\ndt = datetime.strptime(timestamp, \"%Y-%m-%d %H:%M:%S\")\n\nprint(dt)\nprint(dt.year)\nprint(dt.hour)\n",[17,7116,7117,7127,7131,7145,7176,7180,7190,7204],{"__ignoreMap":50},[54,7118,7119,7121,7123,7125],{"class":56,"line":57},[54,7120,1640],{"class":60},[54,7122,1643],{"class":64},[54,7124,61],{"class":60},[54,7126,1648],{"class":64},[54,7128,7129],{"class":56,"line":68},[54,7130,72],{"emptyLinePlaceholder":71},[54,7132,7133,7136,7138,7140,7143],{"class":56,"line":75},[54,7134,7135],{"class":64},"timestamp ",[54,7137,82],{"class":81},[54,7139,830],{"class":544},[54,7141,7142],{"class":548},"2024-08-15 16:45:30",[54,7144,5239],{"class":544},[54,7146,7147,7149,7151,7153,7155,7157,7159,7162,7164,7166,7168,7170,7172,7174],{"class":56,"line":112},[54,7148,5103],{"class":64},[54,7150,82],{"class":81},[54,7152,1662],{"class":64},[54,7154,89],{"class":88},[54,7156,6215],{"class":92},[54,7158,96],{"class":88},[54,7160,7161],{"class":92},"timestamp",[54,7163,103],{"class":88},[54,7165,830],{"class":544},[54,7167,2711],{"class":548},[54,7169,2714],{"class":99},[54,7171,3202],{"class":548},[54,7173,545],{"class":544},[54,7175,109],{"class":88},[54,7177,7178],{"class":56,"line":887},[54,7179,72],{"emptyLinePlaceholder":71},[54,7181,7182,7184,7186,7188],{"class":56,"line":892},[54,7183,116],{"class":115},[54,7185,96],{"class":88},[54,7187,6246],{"class":92},[54,7189,109],{"class":88},[54,7191,7192,7194,7196,7198,7200,7202],{"class":56,"line":913},[54,7193,116],{"class":115},[54,7195,96],{"class":88},[54,7197,6246],{"class":92},[54,7199,89],{"class":88},[54,7201,1694],{"class":1693},[54,7203,109],{"class":88},[54,7205,7206,7208,7210,7212,7214,7216],{"class":56,"line":2121},[54,7207,116],{"class":115},[54,7209,96],{"class":88},[54,7211,6246],{"class":92},[54,7213,89],{"class":88},[54,7215,2024],{"class":1693},[54,7217,109],{"class":88},[14,7219,449],{},[45,7221,7223],{"className":47,"code":7222,"language":49,"meta":50,"style":50},"2024-08-15 16:45:30\n2024\n16\n",[17,7224,7225,7250,7255],{"__ignoreMap":50},[54,7226,7227,7229,7231,7233,7235,7237,7239,7242,7244,7246,7248],{"class":56,"line":57},[54,7228,6274],{"class":99},[54,7230,1467],{"class":81},[54,7232,752],{"class":99},[54,7234,327],{"class":2388},[54,7236,1467],{"class":81},[54,7238,6497],{"class":99},[54,7240,7241],{"class":99}," 16",[54,7243,147],{"class":88},[54,7245,6292],{"class":99},[54,7247,147],{"class":88},[54,7249,4133],{"class":99},[54,7251,7252],{"class":56,"line":68},[54,7253,7254],{"class":99},"2024\n",[54,7256,7257],{"class":56,"line":75},[54,7258,7259],{"class":99},"16\n",[14,7261,7262],{},"This is useful for:",[149,7264,7265,7268,7271,7274],{},[152,7266,7267],{},"log files",[152,7269,7270],{},"timestamps",[152,7272,7273],{},"CSV data",[152,7275,7276],{},"API responses",[40,7278,7280],{"id":7279},"what-errors-beginners-often-see","What errors beginners often see",[14,7282,7283,7284,89],{},"The most common error is ",[17,7285,1306],{},[14,7287,7288],{},"This usually means the string does not match the format string.",[14,7290,7291],{},"Example of code that causes an error:",[45,7293,7295],{"className":47,"code":7294,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"2024\u002F08\u002F15\"\ndt = datetime.strptime(text, \"%Y-%m-%d\")\n",[17,7296,7297,7307,7311,7324],{"__ignoreMap":50},[54,7298,7299,7301,7303,7305],{"class":56,"line":57},[54,7300,1640],{"class":60},[54,7302,1643],{"class":64},[54,7304,61],{"class":60},[54,7306,1648],{"class":64},[54,7308,7309],{"class":56,"line":68},[54,7310,72],{"emptyLinePlaceholder":71},[54,7312,7313,7315,7317,7319,7322],{"class":56,"line":75},[54,7314,5134],{"class":64},[54,7316,82],{"class":81},[54,7318,830],{"class":544},[54,7320,7321],{"class":548},"2024\u002F08\u002F15",[54,7323,5239],{"class":544},[54,7325,7326,7328,7330,7332,7334,7336,7338,7340,7342,7344,7346,7348,7350],{"class":56,"line":112},[54,7327,5103],{"class":64},[54,7329,82],{"class":81},[54,7331,1662],{"class":64},[54,7333,89],{"class":88},[54,7335,6215],{"class":92},[54,7337,96],{"class":88},[54,7339,5171],{"class":92},[54,7341,103],{"class":88},[54,7343,830],{"class":544},[54,7345,2711],{"class":548},[54,7347,2714],{"class":99},[54,7349,545],{"class":544},[54,7351,109],{"class":88},[14,7353,7354,7355,7357,7358,89],{},"This fails because the string uses ",[17,7356,6676],{}," but the format uses ",[17,7359,1467],{},[14,7361,7362],{},"Common causes:",[149,7364,7365,7368,7371,7377,7380],{},[152,7366,7367],{},"wrong separators",[152,7369,7370],{},"wrong day-month-year order",[152,7372,7373,7374,7376],{},"using ",[17,7375,3788],{}," for minutes by mistake",[152,7378,7379],{},"extra spaces or extra text in the string",[152,7381,7382,7383],{},"invalid dates such as ",[17,7384,7385],{},"2024-02-30",[14,7387,7388],{},"Also watch out for this common mistake:",[149,7390,7391,7395],{},[152,7392,7393,5328],{},[17,7394,3788],{},[152,7396,7397,4196],{},[17,7398,4195],{},[14,7400,7401],{},"And this one:",[149,7403,7404,7409],{},[152,7405,7406,7408],{},[17,7407,4189],{}," = 24-hour clock",[152,7410,7411,7413,7414],{},[17,7412,4207],{}," = 12-hour clock, usually used with ",[17,7415,4213],{},[14,7417,7418,7419,89],{},"If you want a broader explanation of this exception, see ",[192,7420,7422],{"href":7421},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F","ValueError in Python: causes and fixes",[40,7424,7426],{"id":7425},"how-to-fix-parsing-problems","How to fix parsing problems",[14,7428,7429,7430,7432],{},"If ",[17,7431,5074],{}," is failing, check the string carefully.",[14,7434,7435],{},"Helpful steps:",[149,7437,7438,7441,7444,7447],{},[152,7439,7440],{},"print the original string",[152,7442,7443],{},"compare each part to each format code",[152,7445,7446],{},"check spaces, commas, slashes, and dashes",[152,7448,7449,7450,134,7453,7456],{},"use ",[17,7451,7452],{},"try",[17,7454,7455],{},"except"," if the input may be invalid",[14,7458,7459],{},"Useful debugging commands:",[45,7461,7463],{"className":47,"code":7462,"language":49,"meta":50,"style":50},"print(date_string)\nprint(repr(date_string))\nprint(datetime.strptime('2024-08-15', '%Y-%m-%d'))\n",[17,7464,7465,7475,7490],{"__ignoreMap":50},[54,7466,7467,7469,7471,7473],{"class":56,"line":57},[54,7468,116],{"class":115},[54,7470,96],{"class":88},[54,7472,6371],{"class":92},[54,7474,109],{"class":88},[54,7476,7477,7479,7481,7484,7486,7488],{"class":56,"line":68},[54,7478,116],{"class":115},[54,7480,96],{"class":88},[54,7482,7483],{"class":115},"repr",[54,7485,96],{"class":88},[54,7487,6371],{"class":92},[54,7489,382],{"class":88},[54,7491,7492,7494,7496,7498,7500,7502,7504,7506,7508,7510,7512,7514,7516,7518,7520],{"class":56,"line":75},[54,7493,116],{"class":115},[54,7495,96],{"class":88},[54,7497,1628],{"class":92},[54,7499,89],{"class":88},[54,7501,6215],{"class":92},[54,7503,96],{"class":88},[54,7505,2240],{"class":544},[54,7507,6431],{"class":548},[54,7509,2240],{"class":544},[54,7511,103],{"class":88},[54,7513,2234],{"class":544},[54,7515,2711],{"class":548},[54,7517,2714],{"class":99},[54,7519,2240],{"class":544},[54,7521,382],{"class":88},[14,7523,7524,7527],{},[17,7525,7526],{},"repr()"," is especially helpful because it shows hidden spaces.",[14,7529,7530],{},"Example with error handling:",[45,7532,7534],{"className":47,"code":7533,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ndate_string = \"2024-08-15\"\n\ntry:\n    dt = datetime.strptime(date_string, \"%Y-%m-%d\")\n    print(\"Parsed:\", dt)\nexcept ValueError as e:\n    print(\"Could not parse date:\", e)\n",[17,7535,7536,7546,7550,7562,7566,7573,7602,7622,7637],{"__ignoreMap":50},[54,7537,7538,7540,7542,7544],{"class":56,"line":57},[54,7539,1640],{"class":60},[54,7541,1643],{"class":64},[54,7543,61],{"class":60},[54,7545,1648],{"class":64},[54,7547,7548],{"class":56,"line":68},[54,7549,72],{"emptyLinePlaceholder":71},[54,7551,7552,7554,7556,7558,7560],{"class":56,"line":75},[54,7553,6424],{"class":64},[54,7555,82],{"class":81},[54,7557,830],{"class":544},[54,7559,6431],{"class":548},[54,7561,5239],{"class":544},[54,7563,7564],{"class":56,"line":112},[54,7565,72],{"emptyLinePlaceholder":71},[54,7567,7568,7570],{"class":56,"line":887},[54,7569,7452],{"class":60},[54,7571,7572],{"class":88},":\n",[54,7574,7575,7578,7580,7582,7584,7586,7588,7590,7592,7594,7596,7598,7600],{"class":56,"line":892},[54,7576,7577],{"class":64},"    dt ",[54,7579,82],{"class":81},[54,7581,1662],{"class":64},[54,7583,89],{"class":88},[54,7585,6215],{"class":92},[54,7587,96],{"class":88},[54,7589,6371],{"class":92},[54,7591,103],{"class":88},[54,7593,830],{"class":544},[54,7595,2711],{"class":548},[54,7597,2714],{"class":99},[54,7599,545],{"class":544},[54,7601,109],{"class":88},[54,7603,7604,7607,7609,7611,7614,7616,7618,7620],{"class":56,"line":913},[54,7605,7606],{"class":115},"    print",[54,7608,96],{"class":88},[54,7610,545],{"class":544},[54,7612,7613],{"class":548},"Parsed:",[54,7615,545],{"class":544},[54,7617,103],{"class":88},[54,7619,5139],{"class":92},[54,7621,109],{"class":88},[54,7623,7624,7626,7629,7632,7635],{"class":56,"line":2121},[54,7625,7455],{"class":60},[54,7627,7628],{"class":429}," ValueError",[54,7630,7631],{"class":60}," as",[54,7633,7634],{"class":64}," e",[54,7636,7572],{"class":88},[54,7638,7639,7641,7643,7645,7648,7650,7652,7654],{"class":56,"line":2136},[54,7640,7606],{"class":115},[54,7642,96],{"class":88},[54,7644,545],{"class":544},[54,7646,7647],{"class":548},"Could not parse date:",[54,7649,545],{"class":544},[54,7651,103],{"class":88},[54,7653,7634],{"class":92},[54,7655,109],{"class":88},[14,7657,7658],{},"Example with invalid input:",[45,7660,7662],{"className":47,"code":7661,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ndate_string = \"2024-02-30\"\n\ntry:\n    dt = datetime.strptime(date_string, \"%Y-%m-%d\")\n    print(\"Parsed:\", dt)\nexcept ValueError as e:\n    print(\"Could not parse date:\", e)\n",[17,7663,7664,7674,7678,7690,7694,7700,7728,7746,7758],{"__ignoreMap":50},[54,7665,7666,7668,7670,7672],{"class":56,"line":57},[54,7667,1640],{"class":60},[54,7669,1643],{"class":64},[54,7671,61],{"class":60},[54,7673,1648],{"class":64},[54,7675,7676],{"class":56,"line":68},[54,7677,72],{"emptyLinePlaceholder":71},[54,7679,7680,7682,7684,7686,7688],{"class":56,"line":75},[54,7681,6424],{"class":64},[54,7683,82],{"class":81},[54,7685,830],{"class":544},[54,7687,7385],{"class":548},[54,7689,5239],{"class":544},[54,7691,7692],{"class":56,"line":112},[54,7693,72],{"emptyLinePlaceholder":71},[54,7695,7696,7698],{"class":56,"line":887},[54,7697,7452],{"class":60},[54,7699,7572],{"class":88},[54,7701,7702,7704,7706,7708,7710,7712,7714,7716,7718,7720,7722,7724,7726],{"class":56,"line":892},[54,7703,7577],{"class":64},[54,7705,82],{"class":81},[54,7707,1662],{"class":64},[54,7709,89],{"class":88},[54,7711,6215],{"class":92},[54,7713,96],{"class":88},[54,7715,6371],{"class":92},[54,7717,103],{"class":88},[54,7719,830],{"class":544},[54,7721,2711],{"class":548},[54,7723,2714],{"class":99},[54,7725,545],{"class":544},[54,7727,109],{"class":88},[54,7729,7730,7732,7734,7736,7738,7740,7742,7744],{"class":56,"line":913},[54,7731,7606],{"class":115},[54,7733,96],{"class":88},[54,7735,545],{"class":544},[54,7737,7613],{"class":548},[54,7739,545],{"class":544},[54,7741,103],{"class":88},[54,7743,5139],{"class":92},[54,7745,109],{"class":88},[54,7747,7748,7750,7752,7754,7756],{"class":56,"line":2121},[54,7749,7455],{"class":60},[54,7751,7628],{"class":429},[54,7753,7631],{"class":60},[54,7755,7634],{"class":64},[54,7757,7572],{"class":88},[54,7759,7760,7762,7764,7766,7768,7770,7772,7774],{"class":56,"line":2136},[54,7761,7606],{"class":115},[54,7763,96],{"class":88},[54,7765,545],{"class":544},[54,7767,7647],{"class":548},[54,7769,545],{"class":544},[54,7771,103],{"class":88},[54,7773,7634],{"class":92},[54,7775,109],{"class":88},[14,7777,449],{},[45,7779,7781],{"className":47,"code":7780,"language":49,"meta":50,"style":50},"Could not parse date: day is out of range for month\n",[17,7782,7783],{"__ignoreMap":50},[54,7784,7785,7788,7790,7793,7795,7798,7801,7804,7806,7809],{"class":56,"line":57},[54,7786,7787],{"class":64},"Could ",[54,7789,3036],{"class":81},[54,7791,7792],{"class":64}," parse date",[54,7794,147],{"class":88},[54,7796,7797],{"class":64}," day ",[54,7799,7800],{"class":81},"is",[54,7802,7803],{"class":64}," out of ",[54,7805,435],{"class":115},[54,7807,7808],{"class":60}," for",[54,7810,7811],{"class":64}," month\n",[40,7813,7815,7817,7818],{"id":7814},"strptime-vs-strftime",[17,7816,5074],{}," vs ",[17,7819,2654],{},[14,7821,7822],{},"These two functions do opposite jobs.",[149,7824,7825,7832],{},[152,7826,7827,7829,7830,1629],{},[17,7828,5074],{}," parses a string into a ",[17,7831,1628],{},[152,7833,7834,7836,7837,7839],{},[17,7835,2654],{}," formats a ",[17,7838,1628],{}," object into a string",[14,7841,253],{},[45,7843,7845],{"className":47,"code":7844,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"2024-08-15\"\ndt = datetime.strptime(text, \"%Y-%m-%d\")\n\nformatted = dt.strftime(\"%d\u002F%m\u002F%Y\")\n\nprint(dt)\nprint(formatted)\n",[17,7846,7847,7857,7861,7873,7901,7905,7929,7933,7943],{"__ignoreMap":50},[54,7848,7849,7851,7853,7855],{"class":56,"line":57},[54,7850,1640],{"class":60},[54,7852,1643],{"class":64},[54,7854,61],{"class":60},[54,7856,1648],{"class":64},[54,7858,7859],{"class":56,"line":68},[54,7860,72],{"emptyLinePlaceholder":71},[54,7862,7863,7865,7867,7869,7871],{"class":56,"line":75},[54,7864,5134],{"class":64},[54,7866,82],{"class":81},[54,7868,830],{"class":544},[54,7870,6431],{"class":548},[54,7872,5239],{"class":544},[54,7874,7875,7877,7879,7881,7883,7885,7887,7889,7891,7893,7895,7897,7899],{"class":56,"line":112},[54,7876,5103],{"class":64},[54,7878,82],{"class":81},[54,7880,1662],{"class":64},[54,7882,89],{"class":88},[54,7884,6215],{"class":92},[54,7886,96],{"class":88},[54,7888,5171],{"class":92},[54,7890,103],{"class":88},[54,7892,830],{"class":544},[54,7894,2711],{"class":548},[54,7896,2714],{"class":99},[54,7898,545],{"class":544},[54,7900,109],{"class":88},[54,7902,7903],{"class":56,"line":887},[54,7904,72],{"emptyLinePlaceholder":71},[54,7906,7907,7909,7911,7913,7915,7917,7919,7921,7923,7925,7927],{"class":56,"line":892},[54,7908,3634],{"class":64},[54,7910,82],{"class":81},[54,7912,5139],{"class":64},[54,7914,89],{"class":88},[54,7916,2704],{"class":92},[54,7918,96],{"class":88},[54,7920,545],{"class":544},[54,7922,2714],{"class":99},[54,7924,7003],{"class":548},[54,7926,545],{"class":544},[54,7928,109],{"class":88},[54,7930,7931],{"class":56,"line":913},[54,7932,72],{"emptyLinePlaceholder":71},[54,7934,7935,7937,7939,7941],{"class":56,"line":2121},[54,7936,116],{"class":115},[54,7938,96],{"class":88},[54,7940,6246],{"class":92},[54,7942,109],{"class":88},[54,7944,7945,7947,7949,7951],{"class":56,"line":2136},[54,7946,116],{"class":115},[54,7948,96],{"class":88},[54,7950,3665],{"class":92},[54,7952,109],{"class":88},[14,7954,449],{},[45,7956,7958],{"className":47,"code":7957,"language":49,"meta":50,"style":50},"2024-08-15 00:00:00\n15\u002F08\u002F2024\n",[17,7959,7960,7984],{"__ignoreMap":50},[54,7961,7962,7964,7966,7968,7970,7972,7974,7976,7978,7980,7982],{"class":56,"line":57},[54,7963,6274],{"class":99},[54,7965,1467],{"class":81},[54,7967,752],{"class":99},[54,7969,327],{"class":2388},[54,7971,1467],{"class":81},[54,7973,6497],{"class":99},[54,7975,6500],{"class":99},[54,7977,147],{"class":88},[54,7979,6505],{"class":99},[54,7981,147],{"class":88},[54,7983,4105],{"class":99},[54,7985,7986,7988,7990,7992,7994,7996],{"class":56,"line":68},[54,7987,6497],{"class":99},[54,7989,6676],{"class":81},[54,7991,752],{"class":99},[54,7993,327],{"class":2388},[54,7995,6676],{"class":81},[54,7997,7254],{"class":99},[14,7999,8000,8001,89],{},"If you need output formatting, read ",[192,8002,8003,2834],{"href":2830},[17,8004,2833],{},[40,8006,978],{"id":977},[14,8008,8009],{},"Beginners often run into the same parsing problems.",[14,8011,8012],{},"Here are the most common ones:",[149,8014,8015,8018,8027,8036,8039,8042],{},[152,8016,8017],{},"using the wrong format code for the string",[152,8019,8020,8021,8023,8024,8026],{},"mixing up ",[17,8022,3788],{}," (month) and ",[17,8025,4195],{}," (minute)",[152,8028,7373,8029,8031,8032,5484,8034],{},[17,8030,4189],{}," with AM\u002FPM instead of ",[17,8033,4207],{},[17,8035,4213],{},[152,8037,8038],{},"leaving extra spaces or text in the string",[152,8040,8041],{},"using a date order that does not match the format string",[152,8043,8044,8045],{},"trying to parse invalid dates such as ",[17,8046,7385],{},[14,8048,8049],{},"A good habit is to test with one known value first, then apply the same format to the rest of your data.",[40,8051,1419],{"id":1418},[602,8053,3390,8055,3393],{"id":8054},"what-does-datetimestrptime-return",[17,8056,2841],{},[14,8058,3396,8059,1732],{},[17,8060,2237],{},[602,8062,8064,8065,5484,8067,3409],{"id":8063},"why-am-i-getting-valueerror-with-strptime","Why am I getting ",[17,8066,1306],{},[17,8068,5074],{},[14,8070,8071],{},"Usually because the string does not exactly match the format string.",[602,8073,8075,8076,134,8078,3409],{"id":8074},"what-is-the-difference-between-m-and-m","What is the difference between ",[17,8077,3788],{},[17,8079,4195],{},[14,8081,8082,8084,8085,8087],{},[17,8083,3788],{}," is month. ",[17,8086,4195],{}," is minute.",[602,8089,8091,8092,8094],{"id":8090},"can-strptime-parse-both-date-and-time","Can ",[17,8093,5074],{}," parse both date and time?",[14,8096,8097],{},"Yes. You can include year, month, day, hour, minute, and second format codes.",[602,8099,8075,8101,134,8103,3409],{"id":8100},"what-is-the-difference-between-strptime-and-strftime",[17,8102,5074],{},[17,8104,2654],{},[14,8106,8107,8109,8110,8112,8113,5066,8115,8117],{},[17,8108,5074],{}," reads a string into a ",[17,8111,1628],{}," object. ",[17,8114,2654],{},[17,8116,1628],{}," object into a string.",[40,8119,1510],{"id":1509},[149,8121,8122,8126,8132,8136],{},[152,8123,8124],{},[192,8125,1774],{"href":6351},[152,8127,8128],{},[192,8129,8130,2834],{"href":2830},[17,8131,2833],{},[152,8133,8134],{},[192,8135,7422],{"href":7421},[152,8137,8138],{},[192,8139,8141],{"href":8140},"\u002Fhow-to\u002Fhow-to-parse-json-in-python\u002F","How to parse JSON in Python",[1545,8143,8144],{},"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 .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 .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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":50,"searchDepth":68,"depth":68,"links":8146},[8147,8148,8150,8151,8152,8153,8154,8155,8156,8157,8159,8160,8172],{"id":3595,"depth":68,"text":3596},{"id":6320,"depth":68,"text":8149},"What datetime.strptime() does",{"id":198,"depth":68,"text":199},{"id":6519,"depth":68,"text":6520},{"id":6682,"depth":68,"text":6683},{"id":6865,"depth":68,"text":6866},{"id":7059,"depth":68,"text":7060},{"id":7279,"depth":68,"text":7280},{"id":7425,"depth":68,"text":7426},{"id":7814,"depth":68,"text":8158},"strptime() vs strftime()",{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":8161},[8162,8164,8166,8168,8170],{"id":8054,"depth":75,"text":8163},"What does datetime.strptime() return?",{"id":8063,"depth":75,"text":8165},"Why am I getting ValueError with strptime()?",{"id":8074,"depth":75,"text":8167},"What is the difference between %m and %M?",{"id":8090,"depth":75,"text":8169},"Can strptime() parse both date and time?",{"id":8100,"depth":75,"text":8171},"What is the difference between strptime() and strftime()?",{"id":1509,"depth":68,"text":1510},"Master datetime.strptime explained in our comprehensive Python beginner guide.",{},{"title":6108,"description":8173},"standard-library\u002Fdatetime.strptime-explained","WAICSMeIcPX02YGdBXUPDqduBnjCZbTjJ5n3qA9Mr6k",{"id":8179,"title":8180,"body":8181,"description":11293,"extension":1577,"meta":11294,"navigation":71,"path":11295,"seo":11296,"stem":11297,"__hash__":11298},"content\u002Fstandard-library\u002Fjson.dump-function-explained.md","json.dump() Function Explained",{"type":7,"value":8182,"toc":11253},[8183,8186,8192,8199,8201,8320,8325,8329,8333,8350,8353,8363,8372,8374,8376,8401,8421,8423,8516,8529,8533,8536,8667,8671,8691,8695,8701,8754,8757,8776,8783,8787,8792,8799,8805,8948,8951,9034,9041,9048,9178,9181,9235,9242,9245,9385,9388,9392,9395,9400,9405,9496,9501,9506,9581,9583,9608,9610,9622,9629,9633,9638,9672,9675,9677,9909,9913,9916,9926,9929,10028,10031,10063,10072,10174,10177,10181,10186,10190,10193,10254,10259,10261,10351,10355,10361,10465,10468,10571,10579,10584,10589,10593,10595,10687,10690,10693,10783,10787,10790,10928,10931,11081,11085,11090,11160,11163,11177,11179,11183,11191,11195,11198,11202,11207,11211,11217,11219,11250],[10,8184,8180],{"id":8185},"jsondump-function-explained",[14,8187,8188,8191],{},[17,8189,8190],{},"json.dump()"," writes Python data to a file in JSON format.",[14,8193,8194,8195,8198],{},"Use it when you want to save data like dictionaries or lists into a ",[17,8196,8197],{},".json"," file. This is a common way to store settings, API data, or simple structured data.",[40,8200,3596],{"id":3595},[45,8202,8204],{"className":47,"code":8203,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\", \"age\": 30}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,8205,8206,8213,8217,8259,8263,8299],{"__ignoreMap":50},[54,8207,8208,8210],{"class":56,"line":57},[54,8209,61],{"class":60},[54,8211,8212],{"class":64}," json\n",[54,8214,8215],{"class":56,"line":68},[54,8216,72],{"emptyLinePlaceholder":71},[54,8218,8219,8222,8224,8227,8229,8232,8234,8236,8238,8241,8243,8245,8247,8250,8252,8254,8256],{"class":56,"line":75},[54,8220,8221],{"class":64},"data ",[54,8223,82],{"class":81},[54,8225,8226],{"class":88}," {",[54,8228,545],{"class":544},[54,8230,8231],{"class":548},"name",[54,8233,545],{"class":544},[54,8235,147],{"class":88},[54,8237,830],{"class":544},[54,8239,8240],{"class":548},"Alice",[54,8242,545],{"class":544},[54,8244,103],{"class":88},[54,8246,830],{"class":544},[54,8248,8249],{"class":548},"age",[54,8251,545],{"class":544},[54,8253,147],{"class":88},[54,8255,3981],{"class":99},[54,8257,8258],{"class":88},"}\n",[54,8260,8261],{"class":56,"line":112},[54,8262,72],{"emptyLinePlaceholder":71},[54,8264,8265,8268,8271,8273,8275,8278,8280,8282,8284,8287,8289,8291,8293,8297],{"class":56,"line":887},[54,8266,8267],{"class":60},"with",[54,8269,8270],{"class":115}," open",[54,8272,96],{"class":88},[54,8274,545],{"class":544},[54,8276,8277],{"class":548},"data.json",[54,8279,545],{"class":544},[54,8281,103],{"class":88},[54,8283,830],{"class":544},[54,8285,8286],{"class":548},"w",[54,8288,545],{"class":544},[54,8290,876],{"class":88},[54,8292,7631],{"class":60},[54,8294,8296],{"class":8295},"sMMDD"," file",[54,8298,7572],{"class":88},[54,8300,8301,8304,8306,8309,8311,8314,8316,8318],{"class":56,"line":892},[54,8302,8303],{"class":64},"    json",[54,8305,89],{"class":88},[54,8307,8308],{"class":92},"dump",[54,8310,96],{"class":88},[54,8312,8313],{"class":92},"data",[54,8315,103],{"class":88},[54,8317,8296],{"class":8295},[54,8319,109],{"class":88},[14,8321,126,8322,8324],{},[17,8323,8190],{}," when you want to write Python data directly to a file as JSON.",[40,8326,8328],{"id":8327},"what-jsondump-does","What json.dump() does",[14,8330,8331,147],{},[17,8332,8190],{},[149,8334,8335,8338,8341,8344],{},[152,8336,8337],{},"Writes Python data to a file in JSON format",[152,8339,8340],{},"Takes a Python object and a file object",[152,8342,8343],{},"Is commonly used with dictionaries and lists",[152,8345,8346,8347,8349],{},"Does ",[35,8348,3036],{}," return the JSON string",[14,8351,8352],{},"This part is important:",[149,8354,8355,8360],{},[152,8356,8357,8359],{},[17,8358,8190],{}," writes to a file",[152,8361,8362],{},"It does not give you the JSON text back as a return value",[14,8364,8365,8366,89],{},"If you need the JSON text as a string instead, see ",[192,8367,8369,2834],{"href":8368},"\u002Fstandard-library\u002Fjson.dumps-function-explained",[17,8370,8371],{},"json.dumps()",[40,8373,199],{"id":198},[14,8375,202],{},[45,8377,8379],{"className":47,"code":8378,"language":49,"meta":50,"style":50},"json.dump(obj, file)\n",[17,8380,8381],{"__ignoreMap":50},[54,8382,8383,8386,8388,8390,8392,8395,8397,8399],{"class":56,"line":57},[54,8384,8385],{"class":64},"json",[54,8387,89],{"class":88},[54,8389,8308],{"class":92},[54,8391,96],{"class":88},[54,8393,8394],{"class":92},"obj",[54,8396,103],{"class":88},[54,8398,8296],{"class":8295},[54,8400,109],{"class":88},[149,8402,8403,8408,8414],{},[152,8404,8405,8407],{},[17,8406,8394],{}," is the Python data you want to save",[152,8409,8410,8413],{},[17,8411,8412],{},"file"," must be an open file object in write mode",[152,8415,8416,8417,8420],{},"It is usually used inside a ",[17,8418,8419],{},"with open(...)"," block",[14,8422,253],{},[45,8424,8426],{"className":47,"code":8425,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"city\": \"Paris\"}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,8427,8428,8434,8438,8464,8468,8498],{"__ignoreMap":50},[54,8429,8430,8432],{"class":56,"line":57},[54,8431,61],{"class":60},[54,8433,8212],{"class":64},[54,8435,8436],{"class":56,"line":68},[54,8437,72],{"emptyLinePlaceholder":71},[54,8439,8440,8442,8444,8446,8448,8451,8453,8455,8457,8460,8462],{"class":56,"line":75},[54,8441,8221],{"class":64},[54,8443,82],{"class":81},[54,8445,8226],{"class":88},[54,8447,545],{"class":544},[54,8449,8450],{"class":548},"city",[54,8452,545],{"class":544},[54,8454,147],{"class":88},[54,8456,830],{"class":544},[54,8458,8459],{"class":548},"Paris",[54,8461,545],{"class":544},[54,8463,8258],{"class":88},[54,8465,8466],{"class":56,"line":112},[54,8467,72],{"emptyLinePlaceholder":71},[54,8469,8470,8472,8474,8476,8478,8480,8482,8484,8486,8488,8490,8492,8494,8496],{"class":56,"line":887},[54,8471,8267],{"class":60},[54,8473,8270],{"class":115},[54,8475,96],{"class":88},[54,8477,545],{"class":544},[54,8479,8277],{"class":548},[54,8481,545],{"class":544},[54,8483,103],{"class":88},[54,8485,830],{"class":544},[54,8487,8286],{"class":548},[54,8489,545],{"class":544},[54,8491,876],{"class":88},[54,8493,7631],{"class":60},[54,8495,8296],{"class":8295},[54,8497,7572],{"class":88},[54,8499,8500,8502,8504,8506,8508,8510,8512,8514],{"class":56,"line":892},[54,8501,8303],{"class":64},[54,8503,89],{"class":88},[54,8505,8308],{"class":92},[54,8507,96],{"class":88},[54,8509,8313],{"class":92},[54,8511,103],{"class":88},[54,8513,8296],{"class":8295},[54,8515,109],{"class":88},[14,8517,8518,8519,134,8525,89],{},"If you are not familiar with opening files, see ",[192,8520,8522,2834],{"href":8521},"\u002Freference\u002Fpython-open-function-explained",[17,8523,8524],{},"open()",[192,8526,8528],{"href":8527},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python","how to write to a file in Python",[40,8530,8532],{"id":8531},"simple-working-example","Simple working example",[14,8534,8535],{},"Here is a complete example that writes a dictionary to a JSON file:",[45,8537,8539],{"className":47,"code":8538,"language":49,"meta":50,"style":50},"import json\n\nperson = {\n    \"name\": \"Alice\",\n    \"age\": 30,\n    \"is_admin\": False\n}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(person, file)\n",[17,8540,8541,8547,8551,8561,8581,8595,8610,8614,8618,8648],{"__ignoreMap":50},[54,8542,8543,8545],{"class":56,"line":57},[54,8544,61],{"class":60},[54,8546,8212],{"class":64},[54,8548,8549],{"class":56,"line":68},[54,8550,72],{"emptyLinePlaceholder":71},[54,8552,8553,8556,8558],{"class":56,"line":75},[54,8554,8555],{"class":64},"person ",[54,8557,82],{"class":81},[54,8559,8560],{"class":88}," {\n",[54,8562,8563,8566,8568,8570,8572,8574,8576,8578],{"class":56,"line":112},[54,8564,8565],{"class":544},"    \"",[54,8567,8231],{"class":548},[54,8569,545],{"class":544},[54,8571,147],{"class":88},[54,8573,830],{"class":544},[54,8575,8240],{"class":548},[54,8577,545],{"class":544},[54,8579,8580],{"class":88},",\n",[54,8582,8583,8585,8587,8589,8591,8593],{"class":56,"line":887},[54,8584,8565],{"class":544},[54,8586,8249],{"class":548},[54,8588,545],{"class":544},[54,8590,147],{"class":88},[54,8592,3981],{"class":99},[54,8594,8580],{"class":88},[54,8596,8597,8599,8602,8604,8606],{"class":56,"line":892},[54,8598,8565],{"class":544},[54,8600,8601],{"class":548},"is_admin",[54,8603,545],{"class":544},[54,8605,147],{"class":88},[54,8607,8609],{"class":8608},"s39Yj"," False\n",[54,8611,8612],{"class":56,"line":913},[54,8613,8258],{"class":88},[54,8615,8616],{"class":56,"line":2121},[54,8617,72],{"emptyLinePlaceholder":71},[54,8619,8620,8622,8624,8626,8628,8630,8632,8634,8636,8638,8640,8642,8644,8646],{"class":56,"line":2136},[54,8621,8267],{"class":60},[54,8623,8270],{"class":115},[54,8625,96],{"class":88},[54,8627,545],{"class":544},[54,8629,8277],{"class":548},[54,8631,545],{"class":544},[54,8633,103],{"class":88},[54,8635,830],{"class":544},[54,8637,8286],{"class":548},[54,8639,545],{"class":544},[54,8641,876],{"class":88},[54,8643,7631],{"class":60},[54,8645,8296],{"class":8295},[54,8647,7572],{"class":88},[54,8649,8650,8652,8654,8656,8658,8661,8663,8665],{"class":56,"line":2151},[54,8651,8303],{"class":64},[54,8653,89],{"class":88},[54,8655,8308],{"class":92},[54,8657,96],{"class":88},[54,8659,8660],{"class":92},"person",[54,8662,103],{"class":88},[54,8664,8296],{"class":8295},[54,8666,109],{"class":88},[602,8668,8670],{"id":8669},"what-this-code-does","What this code does",[149,8672,8673,8682,8685,8688],{},[152,8674,8675,8676,8678,8679],{},"Opens ",[17,8677,8277],{}," in write mode using ",[17,8680,8681],{},"\"w\"",[152,8683,8684],{},"Creates the file if it does not exist",[152,8686,8687],{},"Overwrites the file if it already exists",[152,8689,8690],{},"Writes the dictionary as JSON",[602,8692,8694],{"id":8693},"resulting-file-content","Resulting file content",[14,8696,8697,8698,8700],{},"After running the code, ",[17,8699,8277],{}," will contain:",[45,8702,8705],{"className":8703,"code":8704,"language":8385,"meta":50,"style":50},"language-json shiki shiki-themes material-theme-lighter github-light github-dark","{\"name\": \"Alice\", \"age\": 30, \"is_admin\": false}\n",[17,8706,8707],{"__ignoreMap":50},[54,8708,8709,8712,8714,8717,8719,8721,8723,8725,8727,8729,8731,8733,8735,8737,8739,8741,8743,8745,8747,8749,8752],{"class":56,"line":57},[54,8710,8711],{"class":88},"{",[54,8713,545],{"class":8608},[54,8715,8231],{"class":8716},"sseR_",[54,8718,545],{"class":8608},[54,8720,147],{"class":88},[54,8722,830],{"class":544},[54,8724,8240],{"class":548},[54,8726,545],{"class":544},[54,8728,103],{"class":88},[54,8730,830],{"class":8608},[54,8732,8249],{"class":8716},[54,8734,545],{"class":8608},[54,8736,147],{"class":88},[54,8738,3981],{"class":99},[54,8740,103],{"class":88},[54,8742,830],{"class":8608},[54,8744,8601],{"class":8716},[54,8746,545],{"class":8608},[54,8748,147],{"class":88},[54,8750,8751],{"class":8608}," false",[54,8753,8258],{"class":88},[14,8755,8756],{},"Notice that JSON uses:",[149,8758,8759,8768],{},[152,8760,8761,8764,8765],{},[17,8762,8763],{},"false"," instead of Python ",[17,8766,8767],{},"False",[152,8769,8770,8764,8773],{},[17,8771,8772],{},"null",[17,8774,8775],{},"None",[14,8777,8778,8779,89],{},"If you want a broader introduction, see the ",[192,8780,8782],{"href":8781},"\u002Fstandard-library\u002Fpython-json-module-overview","Python JSON module overview",[40,8784,8786],{"id":8785},"common-useful-options","Common useful options",[14,8788,8789,8791],{},[17,8790,8190],{}," has several optional arguments that are often useful.",[602,8793,126,8795,8798],{"id":8794},"use-indent-to-make-json-easier-to-read",[17,8796,8797],{},"indent"," to make JSON easier to read",[14,8800,8801,8802,8804],{},"Without ",[17,8803,8797],{},", JSON is usually written on one line.",[45,8806,8808],{"className":47,"code":8807,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\", \"age\": 30, \"skills\": [\"Python\", \"SQL\"]}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file, indent=2)\n",[17,8809,8810,8816,8820,8886,8890,8920],{"__ignoreMap":50},[54,8811,8812,8814],{"class":56,"line":57},[54,8813,61],{"class":60},[54,8815,8212],{"class":64},[54,8817,8818],{"class":56,"line":68},[54,8819,72],{"emptyLinePlaceholder":71},[54,8821,8822,8824,8826,8828,8830,8832,8834,8836,8838,8840,8842,8844,8846,8848,8850,8852,8854,8856,8858,8861,8863,8865,8867,8869,8872,8874,8876,8878,8881,8883],{"class":56,"line":75},[54,8823,8221],{"class":64},[54,8825,82],{"class":81},[54,8827,8226],{"class":88},[54,8829,545],{"class":544},[54,8831,8231],{"class":548},[54,8833,545],{"class":544},[54,8835,147],{"class":88},[54,8837,830],{"class":544},[54,8839,8240],{"class":548},[54,8841,545],{"class":544},[54,8843,103],{"class":88},[54,8845,830],{"class":544},[54,8847,8249],{"class":548},[54,8849,545],{"class":544},[54,8851,147],{"class":88},[54,8853,3981],{"class":99},[54,8855,103],{"class":88},[54,8857,830],{"class":544},[54,8859,8860],{"class":548},"skills",[54,8862,545],{"class":544},[54,8864,147],{"class":88},[54,8866,818],{"class":88},[54,8868,545],{"class":544},[54,8870,8871],{"class":548},"Python",[54,8873,545],{"class":544},[54,8875,103],{"class":88},[54,8877,830],{"class":544},[54,8879,8880],{"class":548},"SQL",[54,8882,545],{"class":544},[54,8884,8885],{"class":88},"]}\n",[54,8887,8888],{"class":56,"line":112},[54,8889,72],{"emptyLinePlaceholder":71},[54,8891,8892,8894,8896,8898,8900,8902,8904,8906,8908,8910,8912,8914,8916,8918],{"class":56,"line":887},[54,8893,8267],{"class":60},[54,8895,8270],{"class":115},[54,8897,96],{"class":88},[54,8899,545],{"class":544},[54,8901,8277],{"class":548},[54,8903,545],{"class":544},[54,8905,103],{"class":88},[54,8907,830],{"class":544},[54,8909,8286],{"class":548},[54,8911,545],{"class":544},[54,8913,876],{"class":88},[54,8915,7631],{"class":60},[54,8917,8296],{"class":8295},[54,8919,7572],{"class":88},[54,8921,8922,8924,8926,8928,8930,8932,8934,8936,8938,8942,8944,8946],{"class":56,"line":892},[54,8923,8303],{"class":64},[54,8925,89],{"class":88},[54,8927,8308],{"class":92},[54,8929,96],{"class":88},[54,8931,8313],{"class":92},[54,8933,103],{"class":88},[54,8935,8296],{"class":8295},[54,8937,103],{"class":88},[54,8939,8941],{"class":8940},"s99_P"," indent",[54,8943,82],{"class":81},[54,8945,396],{"class":99},[54,8947,109],{"class":88},[14,8949,8950],{},"Result in the file:",[45,8952,8954],{"className":8703,"code":8953,"language":8385,"meta":50,"style":50},"{\n  \"name\": \"Alice\",\n  \"age\": 30,\n  \"skills\": [\n    \"Python\",\n    \"SQL\"\n  ]\n}\n",[17,8955,8956,8961,8980,8994,9007,9017,9025,9030],{"__ignoreMap":50},[54,8957,8958],{"class":56,"line":57},[54,8959,8960],{"class":88},"{\n",[54,8962,8963,8966,8968,8970,8972,8974,8976,8978],{"class":56,"line":68},[54,8964,8965],{"class":8608},"  \"",[54,8967,8231],{"class":8716},[54,8969,545],{"class":8608},[54,8971,147],{"class":88},[54,8973,830],{"class":544},[54,8975,8240],{"class":548},[54,8977,545],{"class":544},[54,8979,8580],{"class":88},[54,8981,8982,8984,8986,8988,8990,8992],{"class":56,"line":75},[54,8983,8965],{"class":8608},[54,8985,8249],{"class":8716},[54,8987,545],{"class":8608},[54,8989,147],{"class":88},[54,8991,3981],{"class":99},[54,8993,8580],{"class":88},[54,8995,8996,8998,9000,9002,9004],{"class":56,"line":112},[54,8997,8965],{"class":8608},[54,8999,8860],{"class":8716},[54,9001,545],{"class":8608},[54,9003,147],{"class":88},[54,9005,9006],{"class":88}," [\n",[54,9008,9009,9011,9013,9015],{"class":56,"line":887},[54,9010,8565],{"class":544},[54,9012,8871],{"class":548},[54,9014,545],{"class":544},[54,9016,8580],{"class":88},[54,9018,9019,9021,9023],{"class":56,"line":892},[54,9020,8565],{"class":544},[54,9022,8880],{"class":548},[54,9024,5239],{"class":544},[54,9026,9027],{"class":56,"line":913},[54,9028,9029],{"class":88},"  ]\n",[54,9031,9032],{"class":56,"line":2121},[54,9033,8258],{"class":88},[14,9035,9036,9037,89],{},"This is often called pretty-printed JSON. For more, see ",[192,9038,9040],{"href":9039},"\u002Fhow-to\u002Fhow-to-pretty-print-json-in-python","how to pretty-print JSON in Python",[602,9042,126,9044,9047],{"id":9043},"use-sort_keystrue-to-sort-dictionary-keys",[17,9045,9046],{},"sort_keys=True"," to sort dictionary keys",[45,9049,9051],{"className":47,"code":9050,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"b\": 2, \"a\": 1, \"c\": 3}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file, indent=2, sort_keys=True)\n",[17,9052,9053,9059,9063,9108,9112,9142],{"__ignoreMap":50},[54,9054,9055,9057],{"class":56,"line":57},[54,9056,61],{"class":60},[54,9058,8212],{"class":64},[54,9060,9061],{"class":56,"line":68},[54,9062,72],{"emptyLinePlaceholder":71},[54,9064,9065,9067,9069,9071,9073,9075,9077,9079,9081,9083,9085,9087,9089,9091,9093,9095,9097,9100,9102,9104,9106],{"class":56,"line":75},[54,9066,8221],{"class":64},[54,9068,82],{"class":81},[54,9070,8226],{"class":88},[54,9072,545],{"class":544},[54,9074,241],{"class":548},[54,9076,545],{"class":544},[54,9078,147],{"class":88},[54,9080,466],{"class":99},[54,9082,103],{"class":88},[54,9084,830],{"class":544},[54,9086,192],{"class":548},[54,9088,545],{"class":544},[54,9090,147],{"class":88},[54,9092,882],{"class":99},[54,9094,103],{"class":88},[54,9096,830],{"class":544},[54,9098,9099],{"class":548},"c",[54,9101,545],{"class":544},[54,9103,147],{"class":88},[54,9105,379],{"class":99},[54,9107,8258],{"class":88},[54,9109,9110],{"class":56,"line":112},[54,9111,72],{"emptyLinePlaceholder":71},[54,9113,9114,9116,9118,9120,9122,9124,9126,9128,9130,9132,9134,9136,9138,9140],{"class":56,"line":887},[54,9115,8267],{"class":60},[54,9117,8270],{"class":115},[54,9119,96],{"class":88},[54,9121,545],{"class":544},[54,9123,8277],{"class":548},[54,9125,545],{"class":544},[54,9127,103],{"class":88},[54,9129,830],{"class":544},[54,9131,8286],{"class":548},[54,9133,545],{"class":544},[54,9135,876],{"class":88},[54,9137,7631],{"class":60},[54,9139,8296],{"class":8295},[54,9141,7572],{"class":88},[54,9143,9144,9146,9148,9150,9152,9154,9156,9158,9160,9162,9164,9166,9168,9171,9173,9176],{"class":56,"line":892},[54,9145,8303],{"class":64},[54,9147,89],{"class":88},[54,9149,8308],{"class":92},[54,9151,96],{"class":88},[54,9153,8313],{"class":92},[54,9155,103],{"class":88},[54,9157,8296],{"class":8295},[54,9159,103],{"class":88},[54,9161,8941],{"class":8940},[54,9163,82],{"class":81},[54,9165,396],{"class":99},[54,9167,103],{"class":88},[54,9169,9170],{"class":8940}," sort_keys",[54,9172,82],{"class":81},[54,9174,9175],{"class":8608},"True",[54,9177,109],{"class":88},[14,9179,9180],{},"This writes the keys in alphabetical order:",[45,9182,9184],{"className":8703,"code":9183,"language":8385,"meta":50,"style":50},"{\n  \"a\": 1,\n  \"b\": 2,\n  \"c\": 3\n}\n",[17,9185,9186,9190,9204,9218,9231],{"__ignoreMap":50},[54,9187,9188],{"class":56,"line":57},[54,9189,8960],{"class":88},[54,9191,9192,9194,9196,9198,9200,9202],{"class":56,"line":68},[54,9193,8965],{"class":8608},[54,9195,192],{"class":8716},[54,9197,545],{"class":8608},[54,9199,147],{"class":88},[54,9201,882],{"class":99},[54,9203,8580],{"class":88},[54,9205,9206,9208,9210,9212,9214,9216],{"class":56,"line":75},[54,9207,8965],{"class":8608},[54,9209,241],{"class":8716},[54,9211,545],{"class":8608},[54,9213,147],{"class":88},[54,9215,466],{"class":99},[54,9217,8580],{"class":88},[54,9219,9220,9222,9224,9226,9228],{"class":56,"line":112},[54,9221,8965],{"class":8608},[54,9223,9099],{"class":8716},[54,9225,545],{"class":8608},[54,9227,147],{"class":88},[54,9229,9230],{"class":99}," 3\n",[54,9232,9233],{"class":56,"line":887},[54,9234,8258],{"class":88},[602,9236,126,9238,9241],{"id":9237},"use-ensure_asciifalse-for-non-english-characters",[17,9239,9240],{},"ensure_ascii=False"," for non-English characters",[14,9243,9244],{},"By default, some non-English characters are escaped.",[45,9246,9248],{"className":47,"code":9247,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"José\", \"city\": \"München\"}\n\nwith open(\"data.json\", \"w\", encoding=\"utf-8\") as file:\n    json.dump(data, file, ensure_ascii=False, indent=2)\n",[17,9249,9250,9256,9260,9302,9306,9350],{"__ignoreMap":50},[54,9251,9252,9254],{"class":56,"line":57},[54,9253,61],{"class":60},[54,9255,8212],{"class":64},[54,9257,9258],{"class":56,"line":68},[54,9259,72],{"emptyLinePlaceholder":71},[54,9261,9262,9264,9266,9268,9270,9272,9274,9276,9278,9281,9283,9285,9287,9289,9291,9293,9295,9298,9300],{"class":56,"line":75},[54,9263,8221],{"class":64},[54,9265,82],{"class":81},[54,9267,8226],{"class":88},[54,9269,545],{"class":544},[54,9271,8231],{"class":548},[54,9273,545],{"class":544},[54,9275,147],{"class":88},[54,9277,830],{"class":544},[54,9279,9280],{"class":548},"José",[54,9282,545],{"class":544},[54,9284,103],{"class":88},[54,9286,830],{"class":544},[54,9288,8450],{"class":548},[54,9290,545],{"class":544},[54,9292,147],{"class":88},[54,9294,830],{"class":544},[54,9296,9297],{"class":548},"München",[54,9299,545],{"class":544},[54,9301,8258],{"class":88},[54,9303,9304],{"class":56,"line":112},[54,9305,72],{"emptyLinePlaceholder":71},[54,9307,9308,9310,9312,9314,9316,9318,9320,9322,9324,9326,9328,9330,9333,9335,9337,9340,9342,9344,9346,9348],{"class":56,"line":887},[54,9309,8267],{"class":60},[54,9311,8270],{"class":115},[54,9313,96],{"class":88},[54,9315,545],{"class":544},[54,9317,8277],{"class":548},[54,9319,545],{"class":544},[54,9321,103],{"class":88},[54,9323,830],{"class":544},[54,9325,8286],{"class":548},[54,9327,545],{"class":544},[54,9329,103],{"class":88},[54,9331,9332],{"class":8940}," encoding",[54,9334,82],{"class":81},[54,9336,545],{"class":544},[54,9338,9339],{"class":548},"utf-8",[54,9341,545],{"class":544},[54,9343,876],{"class":88},[54,9345,7631],{"class":60},[54,9347,8296],{"class":8295},[54,9349,7572],{"class":88},[54,9351,9352,9354,9356,9358,9360,9362,9364,9366,9368,9371,9373,9375,9377,9379,9381,9383],{"class":56,"line":892},[54,9353,8303],{"class":64},[54,9355,89],{"class":88},[54,9357,8308],{"class":92},[54,9359,96],{"class":88},[54,9361,8313],{"class":92},[54,9363,103],{"class":88},[54,9365,8296],{"class":8295},[54,9367,103],{"class":88},[54,9369,9370],{"class":8940}," ensure_ascii",[54,9372,82],{"class":81},[54,9374,8767],{"class":8608},[54,9376,103],{"class":88},[54,9378,8941],{"class":8940},[54,9380,82],{"class":81},[54,9382,396],{"class":99},[54,9384,109],{"class":88},[14,9386,9387],{},"This keeps the characters readable in the file.",[40,9389,9391],{"id":9390},"jsondump-vs-jsondumps","json.dump() vs json.dumps()",[14,9393,9394],{},"These two functions are similar, but they do different jobs.",[602,9396,9398],{"id":9397},"jsondump",[17,9399,8190],{},[149,9401,9402],{},[152,9403,9404],{},"Writes JSON directly to a file",[45,9406,9408],{"className":47,"code":9407,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\"}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,9409,9410,9416,9420,9444,9448,9478],{"__ignoreMap":50},[54,9411,9412,9414],{"class":56,"line":57},[54,9413,61],{"class":60},[54,9415,8212],{"class":64},[54,9417,9418],{"class":56,"line":68},[54,9419,72],{"emptyLinePlaceholder":71},[54,9421,9422,9424,9426,9428,9430,9432,9434,9436,9438,9440,9442],{"class":56,"line":75},[54,9423,8221],{"class":64},[54,9425,82],{"class":81},[54,9427,8226],{"class":88},[54,9429,545],{"class":544},[54,9431,8231],{"class":548},[54,9433,545],{"class":544},[54,9435,147],{"class":88},[54,9437,830],{"class":544},[54,9439,8240],{"class":548},[54,9441,545],{"class":544},[54,9443,8258],{"class":88},[54,9445,9446],{"class":56,"line":112},[54,9447,72],{"emptyLinePlaceholder":71},[54,9449,9450,9452,9454,9456,9458,9460,9462,9464,9466,9468,9470,9472,9474,9476],{"class":56,"line":887},[54,9451,8267],{"class":60},[54,9453,8270],{"class":115},[54,9455,96],{"class":88},[54,9457,545],{"class":544},[54,9459,8277],{"class":548},[54,9461,545],{"class":544},[54,9463,103],{"class":88},[54,9465,830],{"class":544},[54,9467,8286],{"class":548},[54,9469,545],{"class":544},[54,9471,876],{"class":88},[54,9473,7631],{"class":60},[54,9475,8296],{"class":8295},[54,9477,7572],{"class":88},[54,9479,9480,9482,9484,9486,9488,9490,9492,9494],{"class":56,"line":892},[54,9481,8303],{"class":64},[54,9483,89],{"class":88},[54,9485,8308],{"class":92},[54,9487,96],{"class":88},[54,9489,8313],{"class":92},[54,9491,103],{"class":88},[54,9493,8296],{"class":8295},[54,9495,109],{"class":88},[602,9497,9499],{"id":9498},"jsondumps",[17,9500,8371],{},[149,9502,9503],{},[152,9504,9505],{},"Returns JSON as a string",[45,9507,9509],{"className":47,"code":9508,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\"}\n\njson_text = json.dumps(data)\nprint(json_text)\n",[17,9510,9511,9517,9521,9545,9549,9570],{"__ignoreMap":50},[54,9512,9513,9515],{"class":56,"line":57},[54,9514,61],{"class":60},[54,9516,8212],{"class":64},[54,9518,9519],{"class":56,"line":68},[54,9520,72],{"emptyLinePlaceholder":71},[54,9522,9523,9525,9527,9529,9531,9533,9535,9537,9539,9541,9543],{"class":56,"line":75},[54,9524,8221],{"class":64},[54,9526,82],{"class":81},[54,9528,8226],{"class":88},[54,9530,545],{"class":544},[54,9532,8231],{"class":548},[54,9534,545],{"class":544},[54,9536,147],{"class":88},[54,9538,830],{"class":544},[54,9540,8240],{"class":548},[54,9542,545],{"class":544},[54,9544,8258],{"class":88},[54,9546,9547],{"class":56,"line":112},[54,9548,72],{"emptyLinePlaceholder":71},[54,9550,9551,9554,9556,9559,9561,9564,9566,9568],{"class":56,"line":887},[54,9552,9553],{"class":64},"json_text ",[54,9555,82],{"class":81},[54,9557,9558],{"class":64}," json",[54,9560,89],{"class":88},[54,9562,9563],{"class":92},"dumps",[54,9565,96],{"class":88},[54,9567,8313],{"class":92},[54,9569,109],{"class":88},[54,9571,9572,9574,9576,9579],{"class":56,"line":892},[54,9573,116],{"class":115},[54,9575,96],{"class":88},[54,9577,9578],{"class":92},"json_text",[54,9580,109],{"class":88},[14,9582,449],{},[45,9584,9586],{"className":47,"code":9585,"language":49,"meta":50,"style":50},"{\"name\": \"Alice\"}\n",[17,9587,9588],{"__ignoreMap":50},[54,9589,9590,9592,9594,9596,9598,9600,9602,9604,9606],{"class":56,"line":57},[54,9591,8711],{"class":88},[54,9593,545],{"class":544},[54,9595,8231],{"class":548},[54,9597,545],{"class":544},[54,9599,147],{"class":88},[54,9601,830],{"class":544},[54,9603,8240],{"class":548},[54,9605,545],{"class":544},[54,9607,8258],{"class":88},[14,9609,3040],{},[149,9611,9612,9617],{},[152,9613,9614,9616],{},[17,9615,8190],{}," for saving JSON to files",[152,9618,9619,9621],{},[17,9620,8371],{}," when you need the JSON text in memory",[14,9623,9624,9625,89],{},"You can also see ",[192,9626,9628],{"href":9627},"\u002Fhow-to\u002Fhow-to-convert-dictionary-to-json-in-python","how to convert a dictionary to JSON in Python",[40,9630,9632],{"id":9631},"what-data-types-work","What data types work",[14,9634,9635,9637],{},[17,9636,8190],{}," works with standard JSON-compatible Python types:",[149,9639,9640,9645,9649,9653,9658,9663,9668],{},[152,9641,9642],{},[17,9643,9644],{},"dict",[152,9646,9647],{},[17,9648,430],{},[152,9650,9651],{},[17,9652,3173],{},[152,9654,9655],{},[17,9656,9657],{},"int",[152,9659,9660],{},[17,9661,9662],{},"float",[152,9664,9665],{},[17,9666,9667],{},"bool",[152,9669,9670],{},[17,9671,8775],{},[14,9673,9674],{},"Nested combinations of these types also work.",[14,9676,253],{},[45,9678,9680],{"className":47,"code":9679,"language":49,"meta":50,"style":50},"import json\n\ndata = {\n    \"user\": \"Alice\",\n    \"age\": 30,\n    \"active\": True,\n    \"scores\": [10, 20, 30],\n    \"profile\": {\n        \"country\": \"UK\",\n        \"verified\": False\n    },\n    \"nickname\": None\n}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file, indent=2)\n",[17,9681,9682,9688,9692,9700,9719,9733,9749,9775,9788,9809,9822,9827,9841,9846,9851,9882],{"__ignoreMap":50},[54,9683,9684,9686],{"class":56,"line":57},[54,9685,61],{"class":60},[54,9687,8212],{"class":64},[54,9689,9690],{"class":56,"line":68},[54,9691,72],{"emptyLinePlaceholder":71},[54,9693,9694,9696,9698],{"class":56,"line":75},[54,9695,8221],{"class":64},[54,9697,82],{"class":81},[54,9699,8560],{"class":88},[54,9701,9702,9704,9707,9709,9711,9713,9715,9717],{"class":56,"line":112},[54,9703,8565],{"class":544},[54,9705,9706],{"class":548},"user",[54,9708,545],{"class":544},[54,9710,147],{"class":88},[54,9712,830],{"class":544},[54,9714,8240],{"class":548},[54,9716,545],{"class":544},[54,9718,8580],{"class":88},[54,9720,9721,9723,9725,9727,9729,9731],{"class":56,"line":887},[54,9722,8565],{"class":544},[54,9724,8249],{"class":548},[54,9726,545],{"class":544},[54,9728,147],{"class":88},[54,9730,3981],{"class":99},[54,9732,8580],{"class":88},[54,9734,9735,9737,9740,9742,9744,9747],{"class":56,"line":892},[54,9736,8565],{"class":544},[54,9738,9739],{"class":548},"active",[54,9741,545],{"class":544},[54,9743,147],{"class":88},[54,9745,9746],{"class":8608}," True",[54,9748,8580],{"class":88},[54,9750,9751,9753,9756,9758,9760,9762,9764,9766,9768,9770,9772],{"class":56,"line":913},[54,9752,8565],{"class":544},[54,9754,9755],{"class":548},"scores",[54,9757,545],{"class":544},[54,9759,147],{"class":88},[54,9761,818],{"class":88},[54,9763,1131],{"class":99},[54,9765,103],{"class":88},[54,9767,695],{"class":99},[54,9769,103],{"class":88},[54,9771,3981],{"class":99},[54,9773,9774],{"class":88},"],\n",[54,9776,9777,9779,9782,9784,9786],{"class":56,"line":2121},[54,9778,8565],{"class":544},[54,9780,9781],{"class":548},"profile",[54,9783,545],{"class":544},[54,9785,147],{"class":88},[54,9787,8560],{"class":88},[54,9789,9790,9793,9796,9798,9800,9802,9805,9807],{"class":56,"line":2136},[54,9791,9792],{"class":544},"        \"",[54,9794,9795],{"class":548},"country",[54,9797,545],{"class":544},[54,9799,147],{"class":88},[54,9801,830],{"class":544},[54,9803,9804],{"class":548},"UK",[54,9806,545],{"class":544},[54,9808,8580],{"class":88},[54,9810,9811,9813,9816,9818,9820],{"class":56,"line":2151},[54,9812,9792],{"class":544},[54,9814,9815],{"class":548},"verified",[54,9817,545],{"class":544},[54,9819,147],{"class":88},[54,9821,8609],{"class":8608},[54,9823,9824],{"class":56,"line":4442},[54,9825,9826],{"class":88},"    },\n",[54,9828,9829,9831,9834,9836,9838],{"class":56,"line":4468},[54,9830,8565],{"class":544},[54,9832,9833],{"class":548},"nickname",[54,9835,545],{"class":544},[54,9837,147],{"class":88},[54,9839,9840],{"class":8608}," None\n",[54,9842,9844],{"class":56,"line":9843},13,[54,9845,8258],{"class":88},[54,9847,9849],{"class":56,"line":9848},14,[54,9850,72],{"emptyLinePlaceholder":71},[54,9852,9854,9856,9858,9860,9862,9864,9866,9868,9870,9872,9874,9876,9878,9880],{"class":56,"line":9853},15,[54,9855,8267],{"class":60},[54,9857,8270],{"class":115},[54,9859,96],{"class":88},[54,9861,545],{"class":544},[54,9863,8277],{"class":548},[54,9865,545],{"class":544},[54,9867,103],{"class":88},[54,9869,830],{"class":544},[54,9871,8286],{"class":548},[54,9873,545],{"class":544},[54,9875,876],{"class":88},[54,9877,7631],{"class":60},[54,9879,8296],{"class":8295},[54,9881,7572],{"class":88},[54,9883,9885,9887,9889,9891,9893,9895,9897,9899,9901,9903,9905,9907],{"class":56,"line":9884},16,[54,9886,8303],{"class":64},[54,9888,89],{"class":88},[54,9890,8308],{"class":92},[54,9892,96],{"class":88},[54,9894,8313],{"class":92},[54,9896,103],{"class":88},[54,9898,8296],{"class":8295},[54,9900,103],{"class":88},[54,9902,8941],{"class":8940},[54,9904,82],{"class":81},[54,9906,396],{"class":99},[54,9908,109],{"class":88},[602,9910,9912],{"id":9911},"types-that-do-not-work-directly","Types that do not work directly",[14,9914,9915],{},"Some Python types are not JSON serializable by default, such as:",[149,9917,9918,9923],{},[152,9919,9920],{},[17,9921,9922],{},"set",[152,9924,9925],{},"Custom class objects",[14,9927,9928],{},"This example causes an error:",[45,9930,9932],{"className":47,"code":9931,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"numbers\": {1, 2, 3}}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,9933,9934,9940,9944,9976,9980,10010],{"__ignoreMap":50},[54,9935,9936,9938],{"class":56,"line":57},[54,9937,61],{"class":60},[54,9939,8212],{"class":64},[54,9941,9942],{"class":56,"line":68},[54,9943,72],{"emptyLinePlaceholder":71},[54,9945,9946,9948,9950,9952,9954,9957,9959,9961,9963,9965,9967,9969,9971,9973],{"class":56,"line":75},[54,9947,8221],{"class":64},[54,9949,82],{"class":81},[54,9951,8226],{"class":88},[54,9953,545],{"class":544},[54,9955,9956],{"class":548},"numbers",[54,9958,545],{"class":544},[54,9960,147],{"class":88},[54,9962,8226],{"class":88},[54,9964,100],{"class":99},[54,9966,103],{"class":88},[54,9968,466],{"class":99},[54,9970,103],{"class":88},[54,9972,379],{"class":99},[54,9974,9975],{"class":88},"}}\n",[54,9977,9978],{"class":56,"line":112},[54,9979,72],{"emptyLinePlaceholder":71},[54,9981,9982,9984,9986,9988,9990,9992,9994,9996,9998,10000,10002,10004,10006,10008],{"class":56,"line":887},[54,9983,8267],{"class":60},[54,9985,8270],{"class":115},[54,9987,96],{"class":88},[54,9989,545],{"class":544},[54,9991,8277],{"class":548},[54,9993,545],{"class":544},[54,9995,103],{"class":88},[54,9997,830],{"class":544},[54,9999,8286],{"class":548},[54,10001,545],{"class":544},[54,10003,876],{"class":88},[54,10005,7631],{"class":60},[54,10007,8296],{"class":8295},[54,10009,7572],{"class":88},[54,10011,10012,10014,10016,10018,10020,10022,10024,10026],{"class":56,"line":892},[54,10013,8303],{"class":64},[54,10015,89],{"class":88},[54,10017,8308],{"class":92},[54,10019,96],{"class":88},[54,10021,8313],{"class":92},[54,10023,103],{"class":88},[54,10025,8296],{"class":8295},[54,10027,109],{"class":88},[14,10029,10030],{},"Typical error:",[45,10032,10034],{"className":47,"code":10033,"language":49,"meta":50,"style":50},"TypeError: Object of type set is not JSON serializable\n",[17,10035,10036],{"__ignoreMap":50},[54,10037,10038,10041,10043,10046,10048,10051,10054,10057,10060],{"class":56,"line":57},[54,10039,10040],{"class":429},"TypeError",[54,10042,147],{"class":88},[54,10044,10045],{"class":64}," Object of ",[54,10047,2208],{"class":429},[54,10049,10050],{"class":429}," set",[54,10052,10053],{"class":81}," is",[54,10055,10056],{"class":81}," not",[54,10058,10059],{"class":4870}," JSON",[54,10061,10062],{"class":64}," serializable\n",[14,10064,10065,10066,10068,10069,10071],{},"To fix this, convert the ",[17,10067,9922],{}," to a ",[17,10070,430],{}," first:",[45,10073,10075],{"className":47,"code":10074,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"numbers\": list({1, 2, 3})}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,10076,10077,10083,10087,10122,10126,10156],{"__ignoreMap":50},[54,10078,10079,10081],{"class":56,"line":57},[54,10080,61],{"class":60},[54,10082,8212],{"class":64},[54,10084,10085],{"class":56,"line":68},[54,10086,72],{"emptyLinePlaceholder":71},[54,10088,10089,10091,10093,10095,10097,10099,10101,10103,10106,10109,10111,10113,10115,10117,10119],{"class":56,"line":75},[54,10090,8221],{"class":64},[54,10092,82],{"class":81},[54,10094,8226],{"class":88},[54,10096,545],{"class":544},[54,10098,9956],{"class":548},[54,10100,545],{"class":544},[54,10102,147],{"class":88},[54,10104,10105],{"class":429}," list",[54,10107,10108],{"class":88},"({",[54,10110,100],{"class":99},[54,10112,103],{"class":88},[54,10114,466],{"class":99},[54,10116,103],{"class":88},[54,10118,379],{"class":99},[54,10120,10121],{"class":88},"})}\n",[54,10123,10124],{"class":56,"line":112},[54,10125,72],{"emptyLinePlaceholder":71},[54,10127,10128,10130,10132,10134,10136,10138,10140,10142,10144,10146,10148,10150,10152,10154],{"class":56,"line":887},[54,10129,8267],{"class":60},[54,10131,8270],{"class":115},[54,10133,96],{"class":88},[54,10135,545],{"class":544},[54,10137,8277],{"class":548},[54,10139,545],{"class":544},[54,10141,103],{"class":88},[54,10143,830],{"class":544},[54,10145,8286],{"class":548},[54,10147,545],{"class":544},[54,10149,876],{"class":88},[54,10151,7631],{"class":60},[54,10153,8296],{"class":8295},[54,10155,7572],{"class":88},[54,10157,10158,10160,10162,10164,10166,10168,10170,10172],{"class":56,"line":892},[54,10159,8303],{"class":64},[54,10161,89],{"class":88},[54,10163,8308],{"class":92},[54,10165,96],{"class":88},[54,10167,8313],{"class":92},[54,10169,103],{"class":88},[54,10171,8296],{"class":8295},[54,10173,109],{"class":88},[14,10175,10176],{},"For this specific problem, see TypeError: Object is not JSON serializable.",[40,10178,10180],{"id":10179},"common-errors-and-fixes","Common errors and fixes",[14,10182,10183,10184,89],{},"Here are the most common problems beginners run into with ",[17,10185,8190],{},[602,10187,10189],{"id":10188},"_1-passing-a-file-path-instead-of-a-file-object","1. Passing a file path instead of a file object",[14,10191,10192],{},"This is wrong:",[45,10194,10196],{"className":47,"code":10195,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\"}\njson.dump(data, \"data.json\")\n",[17,10197,10198,10204,10208,10232],{"__ignoreMap":50},[54,10199,10200,10202],{"class":56,"line":57},[54,10201,61],{"class":60},[54,10203,8212],{"class":64},[54,10205,10206],{"class":56,"line":68},[54,10207,72],{"emptyLinePlaceholder":71},[54,10209,10210,10212,10214,10216,10218,10220,10222,10224,10226,10228,10230],{"class":56,"line":75},[54,10211,8221],{"class":64},[54,10213,82],{"class":81},[54,10215,8226],{"class":88},[54,10217,545],{"class":544},[54,10219,8231],{"class":548},[54,10221,545],{"class":544},[54,10223,147],{"class":88},[54,10225,830],{"class":544},[54,10227,8240],{"class":548},[54,10229,545],{"class":544},[54,10231,8258],{"class":88},[54,10233,10234,10236,10238,10240,10242,10244,10246,10248,10250,10252],{"class":56,"line":112},[54,10235,8385],{"class":64},[54,10237,89],{"class":88},[54,10239,8308],{"class":92},[54,10241,96],{"class":88},[54,10243,8313],{"class":92},[54,10245,103],{"class":88},[54,10247,830],{"class":544},[54,10249,8277],{"class":548},[54,10251,545],{"class":544},[54,10253,109],{"class":88},[14,10255,10256,10258],{},[17,10257,8190],{}," expects an open file object, not a file name string.",[14,10260,1310],{},[45,10262,10263],{"className":47,"code":9407,"language":49,"meta":50,"style":50},[17,10264,10265,10271,10275,10299,10303,10333],{"__ignoreMap":50},[54,10266,10267,10269],{"class":56,"line":57},[54,10268,61],{"class":60},[54,10270,8212],{"class":64},[54,10272,10273],{"class":56,"line":68},[54,10274,72],{"emptyLinePlaceholder":71},[54,10276,10277,10279,10281,10283,10285,10287,10289,10291,10293,10295,10297],{"class":56,"line":75},[54,10278,8221],{"class":64},[54,10280,82],{"class":81},[54,10282,8226],{"class":88},[54,10284,545],{"class":544},[54,10286,8231],{"class":548},[54,10288,545],{"class":544},[54,10290,147],{"class":88},[54,10292,830],{"class":544},[54,10294,8240],{"class":548},[54,10296,545],{"class":544},[54,10298,8258],{"class":88},[54,10300,10301],{"class":56,"line":112},[54,10302,72],{"emptyLinePlaceholder":71},[54,10304,10305,10307,10309,10311,10313,10315,10317,10319,10321,10323,10325,10327,10329,10331],{"class":56,"line":887},[54,10306,8267],{"class":60},[54,10308,8270],{"class":115},[54,10310,96],{"class":88},[54,10312,545],{"class":544},[54,10314,8277],{"class":548},[54,10316,545],{"class":544},[54,10318,103],{"class":88},[54,10320,830],{"class":544},[54,10322,8286],{"class":548},[54,10324,545],{"class":544},[54,10326,876],{"class":88},[54,10328,7631],{"class":60},[54,10330,8296],{"class":8295},[54,10332,7572],{"class":88},[54,10334,10335,10337,10339,10341,10343,10345,10347,10349],{"class":56,"line":892},[54,10336,8303],{"class":64},[54,10338,89],{"class":88},[54,10340,8308],{"class":92},[54,10342,96],{"class":88},[54,10344,8313],{"class":92},[54,10346,103],{"class":88},[54,10348,8296],{"class":8295},[54,10350,109],{"class":88},[602,10352,10354],{"id":10353},"_2-using-unsupported-data-types","2. Using unsupported data types",[14,10356,10357,10358,10360],{},"This fails because ",[17,10359,9922],{}," is not supported:",[45,10362,10364],{"className":47,"code":10363,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"items\": {\"apple\", \"banana\"}}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,10365,10366,10372,10376,10413,10417,10447],{"__ignoreMap":50},[54,10367,10368,10370],{"class":56,"line":57},[54,10369,61],{"class":60},[54,10371,8212],{"class":64},[54,10373,10374],{"class":56,"line":68},[54,10375,72],{"emptyLinePlaceholder":71},[54,10377,10378,10380,10382,10384,10386,10389,10391,10393,10395,10397,10400,10402,10404,10406,10409,10411],{"class":56,"line":75},[54,10379,8221],{"class":64},[54,10381,82],{"class":81},[54,10383,8226],{"class":88},[54,10385,545],{"class":544},[54,10387,10388],{"class":548},"items",[54,10390,545],{"class":544},[54,10392,147],{"class":88},[54,10394,8226],{"class":88},[54,10396,545],{"class":544},[54,10398,10399],{"class":548},"apple",[54,10401,545],{"class":544},[54,10403,103],{"class":88},[54,10405,830],{"class":544},[54,10407,10408],{"class":548},"banana",[54,10410,545],{"class":544},[54,10412,9975],{"class":88},[54,10414,10415],{"class":56,"line":112},[54,10416,72],{"emptyLinePlaceholder":71},[54,10418,10419,10421,10423,10425,10427,10429,10431,10433,10435,10437,10439,10441,10443,10445],{"class":56,"line":887},[54,10420,8267],{"class":60},[54,10422,8270],{"class":115},[54,10424,96],{"class":88},[54,10426,545],{"class":544},[54,10428,8277],{"class":548},[54,10430,545],{"class":544},[54,10432,103],{"class":88},[54,10434,830],{"class":544},[54,10436,8286],{"class":548},[54,10438,545],{"class":544},[54,10440,876],{"class":88},[54,10442,7631],{"class":60},[54,10444,8296],{"class":8295},[54,10446,7572],{"class":88},[54,10448,10449,10451,10453,10455,10457,10459,10461,10463],{"class":56,"line":892},[54,10450,8303],{"class":64},[54,10452,89],{"class":88},[54,10454,8308],{"class":92},[54,10456,96],{"class":88},[54,10458,8313],{"class":92},[54,10460,103],{"class":88},[54,10462,8296],{"class":8295},[54,10464,109],{"class":88},[14,10466,10467],{},"Fix it by converting the set:",[45,10469,10471],{"className":47,"code":10470,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"items\": list({\"apple\", \"banana\"})}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,10472,10473,10479,10483,10519,10523,10553],{"__ignoreMap":50},[54,10474,10475,10477],{"class":56,"line":57},[54,10476,61],{"class":60},[54,10478,8212],{"class":64},[54,10480,10481],{"class":56,"line":68},[54,10482,72],{"emptyLinePlaceholder":71},[54,10484,10485,10487,10489,10491,10493,10495,10497,10499,10501,10503,10505,10507,10509,10511,10513,10515,10517],{"class":56,"line":75},[54,10486,8221],{"class":64},[54,10488,82],{"class":81},[54,10490,8226],{"class":88},[54,10492,545],{"class":544},[54,10494,10388],{"class":548},[54,10496,545],{"class":544},[54,10498,147],{"class":88},[54,10500,10105],{"class":429},[54,10502,10108],{"class":88},[54,10504,545],{"class":544},[54,10506,10399],{"class":548},[54,10508,545],{"class":544},[54,10510,103],{"class":88},[54,10512,830],{"class":544},[54,10514,10408],{"class":548},[54,10516,545],{"class":544},[54,10518,10121],{"class":88},[54,10520,10521],{"class":56,"line":112},[54,10522,72],{"emptyLinePlaceholder":71},[54,10524,10525,10527,10529,10531,10533,10535,10537,10539,10541,10543,10545,10547,10549,10551],{"class":56,"line":887},[54,10526,8267],{"class":60},[54,10528,8270],{"class":115},[54,10530,96],{"class":88},[54,10532,545],{"class":544},[54,10534,8277],{"class":548},[54,10536,545],{"class":544},[54,10538,103],{"class":88},[54,10540,830],{"class":544},[54,10542,8286],{"class":548},[54,10544,545],{"class":544},[54,10546,876],{"class":88},[54,10548,7631],{"class":60},[54,10550,8296],{"class":8295},[54,10552,7572],{"class":88},[54,10554,10555,10557,10559,10561,10563,10565,10567,10569],{"class":56,"line":892},[54,10556,8303],{"class":64},[54,10558,89],{"class":88},[54,10560,8308],{"class":92},[54,10562,96],{"class":88},[54,10564,8313],{"class":92},[54,10566,103],{"class":88},[54,10568,8296],{"class":8295},[54,10570,109],{"class":88},[602,10572,10574,10575,5484,10577],{"id":10573},"_3-confusing-jsondump-with-jsondumps","3. Confusing ",[17,10576,8190],{},[17,10578,8371],{},[14,10580,10581,10582,89],{},"If you want to save to a file, use ",[17,10583,8190],{},[14,10585,10586,10587,89],{},"If you want a string, use ",[17,10588,8371],{},[602,10590,10592],{"id":10591},"_4-opening-the-file-in-the-wrong-mode","4. Opening the file in the wrong mode",[14,10594,10192],{},[45,10596,10598],{"className":47,"code":10597,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\"}\n\nwith open(\"data.json\", \"r\") as file:\n    json.dump(data, file)\n",[17,10599,10600,10606,10610,10634,10638,10669],{"__ignoreMap":50},[54,10601,10602,10604],{"class":56,"line":57},[54,10603,61],{"class":60},[54,10605,8212],{"class":64},[54,10607,10608],{"class":56,"line":68},[54,10609,72],{"emptyLinePlaceholder":71},[54,10611,10612,10614,10616,10618,10620,10622,10624,10626,10628,10630,10632],{"class":56,"line":75},[54,10613,8221],{"class":64},[54,10615,82],{"class":81},[54,10617,8226],{"class":88},[54,10619,545],{"class":544},[54,10621,8231],{"class":548},[54,10623,545],{"class":544},[54,10625,147],{"class":88},[54,10627,830],{"class":544},[54,10629,8240],{"class":548},[54,10631,545],{"class":544},[54,10633,8258],{"class":88},[54,10635,10636],{"class":56,"line":112},[54,10637,72],{"emptyLinePlaceholder":71},[54,10639,10640,10642,10644,10646,10648,10650,10652,10654,10656,10659,10661,10663,10665,10667],{"class":56,"line":887},[54,10641,8267],{"class":60},[54,10643,8270],{"class":115},[54,10645,96],{"class":88},[54,10647,545],{"class":544},[54,10649,8277],{"class":548},[54,10651,545],{"class":544},[54,10653,103],{"class":88},[54,10655,830],{"class":544},[54,10657,10658],{"class":548},"r",[54,10660,545],{"class":544},[54,10662,876],{"class":88},[54,10664,7631],{"class":60},[54,10666,8296],{"class":8295},[54,10668,7572],{"class":88},[54,10670,10671,10673,10675,10677,10679,10681,10683,10685],{"class":56,"line":892},[54,10672,8303],{"class":64},[54,10674,89],{"class":88},[54,10676,8308],{"class":92},[54,10678,96],{"class":88},[54,10680,8313],{"class":92},[54,10682,103],{"class":88},[54,10684,8296],{"class":8295},[54,10686,109],{"class":88},[14,10688,10689],{},"The file is opened in read mode, so Python cannot write to it.",[14,10691,10692],{},"Use write mode:",[45,10694,10695],{"className":47,"code":9407,"language":49,"meta":50,"style":50},[17,10696,10697,10703,10707,10731,10735,10765],{"__ignoreMap":50},[54,10698,10699,10701],{"class":56,"line":57},[54,10700,61],{"class":60},[54,10702,8212],{"class":64},[54,10704,10705],{"class":56,"line":68},[54,10706,72],{"emptyLinePlaceholder":71},[54,10708,10709,10711,10713,10715,10717,10719,10721,10723,10725,10727,10729],{"class":56,"line":75},[54,10710,8221],{"class":64},[54,10712,82],{"class":81},[54,10714,8226],{"class":88},[54,10716,545],{"class":544},[54,10718,8231],{"class":548},[54,10720,545],{"class":544},[54,10722,147],{"class":88},[54,10724,830],{"class":544},[54,10726,8240],{"class":548},[54,10728,545],{"class":544},[54,10730,8258],{"class":88},[54,10732,10733],{"class":56,"line":112},[54,10734,72],{"emptyLinePlaceholder":71},[54,10736,10737,10739,10741,10743,10745,10747,10749,10751,10753,10755,10757,10759,10761,10763],{"class":56,"line":887},[54,10738,8267],{"class":60},[54,10740,8270],{"class":115},[54,10742,96],{"class":88},[54,10744,545],{"class":544},[54,10746,8277],{"class":548},[54,10748,545],{"class":544},[54,10750,103],{"class":88},[54,10752,830],{"class":544},[54,10754,8286],{"class":548},[54,10756,545],{"class":544},[54,10758,876],{"class":88},[54,10760,7631],{"class":60},[54,10762,8296],{"class":8295},[54,10764,7572],{"class":88},[54,10766,10767,10769,10771,10773,10775,10777,10779,10781],{"class":56,"line":892},[54,10768,8303],{"class":64},[54,10770,89],{"class":88},[54,10772,8308],{"class":92},[54,10774,96],{"class":88},[54,10776,8313],{"class":92},[54,10778,103],{"class":88},[54,10780,8296],{"class":8295},[54,10782,109],{"class":88},[602,10784,10786],{"id":10785},"_5-trying-to-save-custom-objects-directly","5. Trying to save custom objects directly",[14,10788,10789],{},"This usually fails:",[45,10791,10793],{"className":47,"code":10792,"language":49,"meta":50,"style":50},"import json\n\nclass User:\n    def __init__(self, name):\n        self.name = name\n\nuser = User(\"Alice\")\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(user, file)\n",[17,10794,10795,10801,10805,10815,10838,10853,10857,10876,10880,10910],{"__ignoreMap":50},[54,10796,10797,10799],{"class":56,"line":57},[54,10798,61],{"class":60},[54,10800,8212],{"class":64},[54,10802,10803],{"class":56,"line":68},[54,10804,72],{"emptyLinePlaceholder":71},[54,10806,10807,10809,10813],{"class":56,"line":75},[54,10808,2231],{"class":2230},[54,10810,10812],{"class":10811},"sbgvK"," User",[54,10814,7572],{"class":88},[54,10816,10817,10820,10823,10825,10829,10831,10835],{"class":56,"line":112},[54,10818,10819],{"class":2230},"    def",[54,10821,10822],{"class":115}," __init__",[54,10824,96],{"class":88},[54,10826,10828],{"class":10827},"smCYv","self",[54,10830,103],{"class":88},[54,10832,10834],{"class":10833},"sFwrP"," name",[54,10836,10837],{"class":88},"):\n",[54,10839,10840,10843,10845,10847,10850],{"class":56,"line":887},[54,10841,10842],{"class":4870},"        self",[54,10844,89],{"class":88},[54,10846,8231],{"class":1693},[54,10848,10849],{"class":81}," =",[54,10851,10852],{"class":64}," name\n",[54,10854,10855],{"class":56,"line":892},[54,10856,72],{"emptyLinePlaceholder":71},[54,10858,10859,10862,10864,10866,10868,10870,10872,10874],{"class":56,"line":913},[54,10860,10861],{"class":64},"user ",[54,10863,82],{"class":81},[54,10865,10812],{"class":92},[54,10867,96],{"class":88},[54,10869,545],{"class":544},[54,10871,8240],{"class":548},[54,10873,545],{"class":544},[54,10875,109],{"class":88},[54,10877,10878],{"class":56,"line":2121},[54,10879,72],{"emptyLinePlaceholder":71},[54,10881,10882,10884,10886,10888,10890,10892,10894,10896,10898,10900,10902,10904,10906,10908],{"class":56,"line":2136},[54,10883,8267],{"class":60},[54,10885,8270],{"class":115},[54,10887,96],{"class":88},[54,10889,545],{"class":544},[54,10891,8277],{"class":548},[54,10893,545],{"class":544},[54,10895,103],{"class":88},[54,10897,830],{"class":544},[54,10899,8286],{"class":548},[54,10901,545],{"class":544},[54,10903,876],{"class":88},[54,10905,7631],{"class":60},[54,10907,8296],{"class":8295},[54,10909,7572],{"class":88},[54,10911,10912,10914,10916,10918,10920,10922,10924,10926],{"class":56,"line":2151},[54,10913,8303],{"class":64},[54,10915,89],{"class":88},[54,10917,8308],{"class":92},[54,10919,96],{"class":88},[54,10921,9706],{"class":92},[54,10923,103],{"class":88},[54,10925,8296],{"class":8295},[54,10927,109],{"class":88},[14,10929,10930],{},"Fix it by converting the object to a dictionary:",[45,10932,10934],{"className":47,"code":10933,"language":49,"meta":50,"style":50},"import json\n\nclass User:\n    def __init__(self, name):\n        self.name = name\n\nuser = User(\"Alice\")\ndata = {\"name\": user.name}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,10935,10936,10942,10946,10954,10970,10982,10986,11004,11029,11033,11063],{"__ignoreMap":50},[54,10937,10938,10940],{"class":56,"line":57},[54,10939,61],{"class":60},[54,10941,8212],{"class":64},[54,10943,10944],{"class":56,"line":68},[54,10945,72],{"emptyLinePlaceholder":71},[54,10947,10948,10950,10952],{"class":56,"line":75},[54,10949,2231],{"class":2230},[54,10951,10812],{"class":10811},[54,10953,7572],{"class":88},[54,10955,10956,10958,10960,10962,10964,10966,10968],{"class":56,"line":112},[54,10957,10819],{"class":2230},[54,10959,10822],{"class":115},[54,10961,96],{"class":88},[54,10963,10828],{"class":10827},[54,10965,103],{"class":88},[54,10967,10834],{"class":10833},[54,10969,10837],{"class":88},[54,10971,10972,10974,10976,10978,10980],{"class":56,"line":887},[54,10973,10842],{"class":4870},[54,10975,89],{"class":88},[54,10977,8231],{"class":1693},[54,10979,10849],{"class":81},[54,10981,10852],{"class":64},[54,10983,10984],{"class":56,"line":892},[54,10985,72],{"emptyLinePlaceholder":71},[54,10987,10988,10990,10992,10994,10996,10998,11000,11002],{"class":56,"line":913},[54,10989,10861],{"class":64},[54,10991,82],{"class":81},[54,10993,10812],{"class":92},[54,10995,96],{"class":88},[54,10997,545],{"class":544},[54,10999,8240],{"class":548},[54,11001,545],{"class":544},[54,11003,109],{"class":88},[54,11005,11006,11008,11010,11012,11014,11016,11018,11020,11023,11025,11027],{"class":56,"line":2121},[54,11007,8221],{"class":64},[54,11009,82],{"class":81},[54,11011,8226],{"class":88},[54,11013,545],{"class":544},[54,11015,8231],{"class":548},[54,11017,545],{"class":544},[54,11019,147],{"class":88},[54,11021,11022],{"class":64}," user",[54,11024,89],{"class":88},[54,11026,8231],{"class":1693},[54,11028,8258],{"class":88},[54,11030,11031],{"class":56,"line":2136},[54,11032,72],{"emptyLinePlaceholder":71},[54,11034,11035,11037,11039,11041,11043,11045,11047,11049,11051,11053,11055,11057,11059,11061],{"class":56,"line":2151},[54,11036,8267],{"class":60},[54,11038,8270],{"class":115},[54,11040,96],{"class":88},[54,11042,545],{"class":544},[54,11044,8277],{"class":548},[54,11046,545],{"class":544},[54,11048,103],{"class":88},[54,11050,830],{"class":544},[54,11052,8286],{"class":548},[54,11054,545],{"class":544},[54,11056,876],{"class":88},[54,11058,7631],{"class":60},[54,11060,8296],{"class":8295},[54,11062,7572],{"class":88},[54,11064,11065,11067,11069,11071,11073,11075,11077,11079],{"class":56,"line":4442},[54,11066,8303],{"class":64},[54,11068,89],{"class":88},[54,11070,8308],{"class":92},[54,11072,96],{"class":88},[54,11074,8313],{"class":92},[54,11076,103],{"class":88},[54,11078,8296],{"class":8295},[54,11080,109],{"class":88},[602,11082,11084],{"id":11083},"helpful-debugging-checks","Helpful debugging checks",[14,11086,7429,11087,11089],{},[17,11088,8190],{}," is failing, these checks can help:",[45,11091,11093],{"className":47,"code":11092,"language":49,"meta":50,"style":50},"print(type(data))\nprint(data)\nprint(json.dumps(data, indent=2))\nprint(file.mode)\n",[17,11094,11095,11109,11119,11145],{"__ignoreMap":50},[54,11096,11097,11099,11101,11103,11105,11107],{"class":56,"line":57},[54,11098,116],{"class":115},[54,11100,96],{"class":88},[54,11102,2208],{"class":429},[54,11104,96],{"class":88},[54,11106,8313],{"class":92},[54,11108,382],{"class":88},[54,11110,11111,11113,11115,11117],{"class":56,"line":68},[54,11112,116],{"class":115},[54,11114,96],{"class":88},[54,11116,8313],{"class":92},[54,11118,109],{"class":88},[54,11120,11121,11123,11125,11127,11129,11131,11133,11135,11137,11139,11141,11143],{"class":56,"line":75},[54,11122,116],{"class":115},[54,11124,96],{"class":88},[54,11126,8385],{"class":92},[54,11128,89],{"class":88},[54,11130,9563],{"class":92},[54,11132,96],{"class":88},[54,11134,8313],{"class":92},[54,11136,103],{"class":88},[54,11138,8941],{"class":8940},[54,11140,82],{"class":81},[54,11142,396],{"class":99},[54,11144,382],{"class":88},[54,11146,11147,11149,11151,11153,11155,11158],{"class":56,"line":112},[54,11148,116],{"class":115},[54,11150,96],{"class":88},[54,11152,8412],{"class":8295},[54,11154,89],{"class":88},[54,11156,11157],{"class":1693},"mode",[54,11159,109],{"class":88},[14,11161,11162],{},"These help you check:",[149,11164,11165,11168,11171,11174],{},[152,11166,11167],{},"What type your data is",[152,11169,11170],{},"What values it contains",[152,11172,11173],{},"Whether the data can be converted to JSON",[152,11175,11176],{},"Whether the file is open in the correct mode",[40,11178,1419],{"id":1418},[602,11180,11182],{"id":11181},"what-is-the-difference-between-jsondump-and-jsondumps","What is the difference between json.dump() and json.dumps()?",[14,11184,11185,11187,11188,11190],{},[17,11186,8190],{}," writes JSON to a file. ",[17,11189,8371],{}," returns JSON as a string.",[602,11192,11194],{"id":11193},"does-jsondump-create-a-file","Does json.dump() create a file?",[14,11196,11197],{},"It writes to a file object. If you open a new file in write mode, Python creates it.",[602,11199,11201],{"id":11200},"can-jsondump-write-a-list","Can json.dump() write a list?",[14,11203,11204,11205,89],{},"Yes. It can write lists, dictionaries, strings, numbers, booleans, and ",[17,11206,8775],{},[602,11208,11210],{"id":11209},"why-does-jsondump-give-a-typeerror","Why does json.dump() give a TypeError?",[14,11212,11213,11214,11216],{},"Usually because the data contains a type that JSON cannot serialize, such as a ",[17,11215,9922],{}," or custom object.",[40,11218,1510],{"id":1509},[149,11220,11221,11225,11230,11235,11240,11245],{},[152,11222,11223],{},[192,11224,8782],{"href":8781},[152,11226,11227],{},[192,11228,11229],{"href":8368},"json.dumps() function explained",[152,11231,11232],{},[192,11233,11234],{"href":9627},"How to convert a dictionary to JSON in Python",[152,11236,11237],{},[192,11238,11239],{"href":9039},"How to pretty-print JSON in Python",[152,11241,11242],{},[192,11243,11244],{"href":8527},"How to write to a file in Python",[152,11246,11247],{},[192,11248,11249],{"href":8521},"Python open() function explained",[1545,11251,11252],{},"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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sseR_, html code.shiki .sseR_{--shiki-light:#9C3EDA;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":50,"searchDepth":68,"depth":68,"links":11254},[11255,11256,11257,11258,11262,11270,11274,11277,11286,11292],{"id":3595,"depth":68,"text":3596},{"id":8327,"depth":68,"text":8328},{"id":198,"depth":68,"text":199},{"id":8531,"depth":68,"text":8532,"children":11259},[11260,11261],{"id":8669,"depth":75,"text":8670},{"id":8693,"depth":75,"text":8694},{"id":8785,"depth":68,"text":8786,"children":11263},[11264,11266,11268],{"id":8794,"depth":75,"text":11265},"Use indent to make JSON easier to read",{"id":9043,"depth":75,"text":11267},"Use sort_keys=True to sort dictionary keys",{"id":9237,"depth":75,"text":11269},"Use ensure_ascii=False for non-English characters",{"id":9390,"depth":68,"text":9391,"children":11271},[11272,11273],{"id":9397,"depth":75,"text":8190},{"id":9498,"depth":75,"text":8371},{"id":9631,"depth":68,"text":9632,"children":11275},[11276],{"id":9911,"depth":75,"text":9912},{"id":10179,"depth":68,"text":10180,"children":11278},[11279,11280,11281,11283,11284,11285],{"id":10188,"depth":75,"text":10189},{"id":10353,"depth":75,"text":10354},{"id":10573,"depth":75,"text":11282},"3. Confusing json.dump() with json.dumps()",{"id":10591,"depth":75,"text":10592},{"id":10785,"depth":75,"text":10786},{"id":11083,"depth":75,"text":11084},{"id":1418,"depth":68,"text":1419,"children":11287},[11288,11289,11290,11291],{"id":11181,"depth":75,"text":11182},{"id":11193,"depth":75,"text":11194},{"id":11200,"depth":75,"text":11201},{"id":11209,"depth":75,"text":11210},{"id":1509,"depth":68,"text":1510},"Master json.dump function explained in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fjson.dump-function-explained",{"title":8180,"description":11293},"standard-library\u002Fjson.dump-function-explained","djb2VEpW--a4s9GRIZTEBfujKSVwkrTA5CUWQ-gwr58",{"id":11300,"title":11301,"body":11302,"description":13565,"extension":1577,"meta":13566,"navigation":71,"path":8368,"seo":13567,"stem":13568,"__hash__":13569},"content\u002Fstandard-library\u002Fjson.dumps-function-explained.md","json.dumps() Function Explained",{"type":7,"value":11303,"toc":13530},[11304,11310,11315,11318,11320,11402,11404,11441,11446,11451,11456,11459,11470,11476,11478,11481,11538,11541,11554,11557,11639,11641,11676,11679,11685,11688,11780,11782,11902,11904,11978,11981,12004,12006,12009,12107,12109,12160,12163,12174,12179,12185,12191,12197,12324,12326,12405,12407,12418,12421,12427,12431,12436,12525,12527,12572,12575,12586,12591,12688,12695,12698,12702,12707,12778,12782,12787,12877,12879,12891,12899,12903,12906,12909,12921,12926,12931,12994,12997,13021,13025,13030,13038,13103,13107,13275,13278,13280,13283,13307,13310,13425,13428,13442,13444,13452,13460,13468,13474,13477,13483,13489,13495,13498,13500,13527],[10,11305,11307,11309],{"id":11306},"jsondumps-function-explained",[17,11308,8371],{}," Function Explained",[14,11311,11312,11314],{},[17,11313,8371],{}," converts a Python object into a JSON-formatted string.",[14,11316,11317],{},"Use it when you want JSON as text, not as a file. This is common when sending data to an API, storing JSON in a variable, or printing JSON for debugging.",[40,11319,3596],{"id":3595},[45,11321,11323],{"className":47,"code":11322,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\", \"age\": 25}\njson_string = json.dumps(data)\nprint(json_string)\n",[17,11324,11325,11331,11335,11372,11391],{"__ignoreMap":50},[54,11326,11327,11329],{"class":56,"line":57},[54,11328,61],{"class":60},[54,11330,8212],{"class":64},[54,11332,11333],{"class":56,"line":68},[54,11334,72],{"emptyLinePlaceholder":71},[54,11336,11337,11339,11341,11343,11345,11347,11349,11351,11353,11355,11357,11359,11361,11363,11365,11367,11370],{"class":56,"line":75},[54,11338,8221],{"class":64},[54,11340,82],{"class":81},[54,11342,8226],{"class":88},[54,11344,545],{"class":544},[54,11346,8231],{"class":548},[54,11348,545],{"class":544},[54,11350,147],{"class":88},[54,11352,830],{"class":544},[54,11354,8240],{"class":548},[54,11356,545],{"class":544},[54,11358,103],{"class":88},[54,11360,830],{"class":544},[54,11362,8249],{"class":548},[54,11364,545],{"class":544},[54,11366,147],{"class":88},[54,11368,11369],{"class":99}," 25",[54,11371,8258],{"class":88},[54,11373,11374,11377,11379,11381,11383,11385,11387,11389],{"class":56,"line":112},[54,11375,11376],{"class":64},"json_string ",[54,11378,82],{"class":81},[54,11380,9558],{"class":64},[54,11382,89],{"class":88},[54,11384,9563],{"class":92},[54,11386,96],{"class":88},[54,11388,8313],{"class":92},[54,11390,109],{"class":88},[54,11392,11393,11395,11397,11400],{"class":56,"line":887},[54,11394,116],{"class":115},[54,11396,96],{"class":88},[54,11398,11399],{"class":92},"json_string",[54,11401,109],{"class":88},[14,11403,449],{},[45,11405,11407],{"className":47,"code":11406,"language":49,"meta":50,"style":50},"{\"name\": \"Alice\", \"age\": 25}\n",[17,11408,11409],{"__ignoreMap":50},[54,11410,11411,11413,11415,11417,11419,11421,11423,11425,11427,11429,11431,11433,11435,11437,11439],{"class":56,"line":57},[54,11412,8711],{"class":88},[54,11414,545],{"class":544},[54,11416,8231],{"class":548},[54,11418,545],{"class":544},[54,11420,147],{"class":88},[54,11422,830],{"class":544},[54,11424,8240],{"class":548},[54,11426,545],{"class":544},[54,11428,103],{"class":88},[54,11430,830],{"class":544},[54,11432,8249],{"class":548},[54,11434,545],{"class":544},[54,11436,147],{"class":88},[54,11438,11369],{"class":99},[54,11440,8258],{"class":88},[14,11442,126,11443,11445],{},[17,11444,8371],{}," when you want a JSON string, not a file. It converts Python data like dictionaries and lists into text in JSON format.",[40,11447,1736,11449,1739],{"id":11448},"what-jsondumps-does",[17,11450,8371],{},[14,11452,11453,11455],{},[17,11454,8371],{}," takes a Python value and returns a string in JSON format.",[14,11457,11458],{},"Key points:",[149,11460,11461,11464,11467],{},[152,11462,11463],{},"It converts a Python object into a JSON string",[152,11465,11466],{},"It returns text, not a Python dictionary or list",[152,11468,11469],{},"It is commonly used before sending data to an API or saving JSON text",[14,11471,6345,11472,6348,11474,89],{},[17,11473,8385],{},[192,11475,8782],{"href":8781},[40,11477,199],{"id":198},[14,11479,11480],{},"The basic form looks like this:",[45,11482,11484],{"className":47,"code":11483,"language":49,"meta":50,"style":50},"json.dumps(obj, *, skipkeys=False, ensure_ascii=True, indent=None, sort_keys=False)\n",[17,11485,11486],{"__ignoreMap":50},[54,11487,11488,11490,11492,11494,11496,11498,11500,11503,11505,11508,11510,11512,11514,11516,11518,11520,11522,11524,11526,11528,11530,11532,11534,11536],{"class":56,"line":57},[54,11489,8385],{"class":64},[54,11491,89],{"class":88},[54,11493,9563],{"class":92},[54,11495,96],{"class":88},[54,11497,8394],{"class":92},[54,11499,103],{"class":88},[54,11501,11502],{"class":81}," *",[54,11504,103],{"class":88},[54,11506,11507],{"class":8940}," skipkeys",[54,11509,82],{"class":81},[54,11511,8767],{"class":8608},[54,11513,103],{"class":88},[54,11515,9370],{"class":8940},[54,11517,82],{"class":81},[54,11519,9175],{"class":8608},[54,11521,103],{"class":88},[54,11523,8941],{"class":8940},[54,11525,82],{"class":81},[54,11527,8775],{"class":8608},[54,11529,103],{"class":88},[54,11531,9170],{"class":8940},[54,11533,82],{"class":81},[54,11535,8767],{"class":8608},[54,11537,109],{"class":88},[14,11539,11540],{},"What this means:",[149,11542,11543,11548,11551],{},[152,11544,11545,11547],{},[17,11546,8394],{}," is the Python object you want to convert",[152,11549,11550],{},"The function returns a string",[152,11552,11553],{},"Optional arguments let you format the output",[14,11555,11556],{},"A simple example:",[45,11558,11560],{"className":47,"code":11559,"language":49,"meta":50,"style":50},"import json\n\nnumbers = [1, 2, 3]\nresult = json.dumps(numbers)\n\nprint(result)\nprint(type(result))\n",[17,11561,11562,11568,11572,11593,11611,11615,11625],{"__ignoreMap":50},[54,11563,11564,11566],{"class":56,"line":57},[54,11565,61],{"class":60},[54,11567,8212],{"class":64},[54,11569,11570],{"class":56,"line":68},[54,11571,72],{"emptyLinePlaceholder":71},[54,11573,11574,11577,11579,11581,11583,11585,11587,11589,11591],{"class":56,"line":75},[54,11575,11576],{"class":64},"numbers ",[54,11578,82],{"class":81},[54,11580,818],{"class":88},[54,11582,100],{"class":99},[54,11584,103],{"class":88},[54,11586,466],{"class":99},[54,11588,103],{"class":88},[54,11590,379],{"class":99},[54,11592,469],{"class":88},[54,11594,11595,11597,11599,11601,11603,11605,11607,11609],{"class":56,"line":112},[54,11596,3833],{"class":64},[54,11598,82],{"class":81},[54,11600,9558],{"class":64},[54,11602,89],{"class":88},[54,11604,9563],{"class":92},[54,11606,96],{"class":88},[54,11608,9956],{"class":92},[54,11610,109],{"class":88},[54,11612,11613],{"class":56,"line":887},[54,11614,72],{"emptyLinePlaceholder":71},[54,11616,11617,11619,11621,11623],{"class":56,"line":892},[54,11618,116],{"class":115},[54,11620,96],{"class":88},[54,11622,3866],{"class":92},[54,11624,109],{"class":88},[54,11626,11627,11629,11631,11633,11635,11637],{"class":56,"line":913},[54,11628,116],{"class":115},[54,11630,96],{"class":88},[54,11632,2208],{"class":429},[54,11634,96],{"class":88},[54,11636,3866],{"class":92},[54,11638,382],{"class":88},[14,11640,449],{},[45,11642,11644],{"className":47,"code":11643,"language":49,"meta":50,"style":50},"[1, 2, 3]\n\u003Cclass 'str'>\n",[17,11645,11646,11662],{"__ignoreMap":50},[54,11647,11648,11650,11652,11654,11656,11658,11660],{"class":56,"line":57},[54,11649,459],{"class":88},[54,11651,100],{"class":99},[54,11653,103],{"class":88},[54,11655,466],{"class":99},[54,11657,103],{"class":88},[54,11659,379],{"class":99},[54,11661,469],{"class":88},[54,11663,11664,11666,11668,11670,11672,11674],{"class":56,"line":68},[54,11665,2227],{"class":81},[54,11667,2231],{"class":2230},[54,11669,2234],{"class":544},[54,11671,3173],{"class":548},[54,11673,2240],{"class":544},[54,11675,2243],{"class":81},[14,11677,11678],{},"Even though the result looks like a Python list, it is actually a string.",[40,11680,11682,11683],{"id":11681},"python-types-that-work-with-jsondumps","Python types that work with ",[17,11684,8371],{},[14,11686,11687],{},"Many common Python types can be converted directly.",[11689,11690,11691,11704],"table",{},[11692,11693,11694],"thead",{},[11695,11696,11697,11701],"tr",{},[11698,11699,11700],"th",{},"Python type",[11698,11702,11703],{},"JSON result",[11705,11706,11707,11717,11726,11735,11744,11755,11770],"tbody",{},[11695,11708,11709,11714],{},[11710,11711,11712],"td",{},[17,11713,9644],{},[11710,11715,11716],{},"JSON object",[11695,11718,11719,11723],{},[11710,11720,11721],{},[17,11722,430],{},[11710,11724,11725],{},"JSON array",[11695,11727,11728,11733],{},[11710,11729,11730],{},[17,11731,11732],{},"tuple",[11710,11734,11725],{},[11695,11736,11737,11741],{},[11710,11738,11739],{},[17,11740,3173],{},[11710,11742,11743],{},"JSON string",[11695,11745,11746,11752],{},[11710,11747,11748,134,11750],{},[17,11749,9657],{},[17,11751,9662],{},[11710,11753,11754],{},"JSON number",[11695,11756,11757,11763],{},[11710,11758,11759,134,11761],{},[17,11760,9175],{},[17,11762,8767],{},[11710,11764,11765,134,11768],{},[17,11766,11767],{},"true",[17,11769,8763],{},[11695,11771,11772,11776],{},[11710,11773,11774],{},[17,11775,8775],{},[11710,11777,11778],{},[17,11779,8772],{},[14,11781,253],{},[45,11783,11785],{"className":47,"code":11784,"language":49,"meta":50,"style":50},"import json\n\ndata = {\n    \"name\": \"Alice\",\n    \"scores\": [95, 88, 91],\n    \"active\": True,\n    \"nickname\": None\n}\n\nprint(json.dumps(data))\n",[17,11786,11787,11793,11797,11805,11823,11850,11864,11876,11880,11884],{"__ignoreMap":50},[54,11788,11789,11791],{"class":56,"line":57},[54,11790,61],{"class":60},[54,11792,8212],{"class":64},[54,11794,11795],{"class":56,"line":68},[54,11796,72],{"emptyLinePlaceholder":71},[54,11798,11799,11801,11803],{"class":56,"line":75},[54,11800,8221],{"class":64},[54,11802,82],{"class":81},[54,11804,8560],{"class":88},[54,11806,11807,11809,11811,11813,11815,11817,11819,11821],{"class":56,"line":112},[54,11808,8565],{"class":544},[54,11810,8231],{"class":548},[54,11812,545],{"class":544},[54,11814,147],{"class":88},[54,11816,830],{"class":544},[54,11818,8240],{"class":548},[54,11820,545],{"class":544},[54,11822,8580],{"class":88},[54,11824,11825,11827,11829,11831,11833,11835,11838,11840,11843,11845,11848],{"class":56,"line":887},[54,11826,8565],{"class":544},[54,11828,9755],{"class":548},[54,11830,545],{"class":544},[54,11832,147],{"class":88},[54,11834,818],{"class":88},[54,11836,11837],{"class":99},"95",[54,11839,103],{"class":88},[54,11841,11842],{"class":99}," 88",[54,11844,103],{"class":88},[54,11846,11847],{"class":99}," 91",[54,11849,9774],{"class":88},[54,11851,11852,11854,11856,11858,11860,11862],{"class":56,"line":892},[54,11853,8565],{"class":544},[54,11855,9739],{"class":548},[54,11857,545],{"class":544},[54,11859,147],{"class":88},[54,11861,9746],{"class":8608},[54,11863,8580],{"class":88},[54,11865,11866,11868,11870,11872,11874],{"class":56,"line":913},[54,11867,8565],{"class":544},[54,11869,9833],{"class":548},[54,11871,545],{"class":544},[54,11873,147],{"class":88},[54,11875,9840],{"class":8608},[54,11877,11878],{"class":56,"line":2121},[54,11879,8258],{"class":88},[54,11881,11882],{"class":56,"line":2136},[54,11883,72],{"emptyLinePlaceholder":71},[54,11885,11886,11888,11890,11892,11894,11896,11898,11900],{"class":56,"line":2151},[54,11887,116],{"class":115},[54,11889,96],{"class":88},[54,11891,8385],{"class":92},[54,11893,89],{"class":88},[54,11895,9563],{"class":92},[54,11897,96],{"class":88},[54,11899,8313],{"class":92},[54,11901,382],{"class":88},[14,11903,449],{},[45,11905,11907],{"className":47,"code":11906,"language":49,"meta":50,"style":50},"{\"name\": \"Alice\", \"scores\": [95, 88, 91], \"active\": true, \"nickname\": null}\n",[17,11908,11909],{"__ignoreMap":50},[54,11910,11911,11913,11915,11917,11919,11921,11923,11925,11927,11929,11931,11933,11935,11937,11939,11941,11943,11945,11947,11949,11952,11954,11956,11958,11960,11963,11965,11967,11969,11971,11973,11976],{"class":56,"line":57},[54,11912,8711],{"class":88},[54,11914,545],{"class":544},[54,11916,8231],{"class":548},[54,11918,545],{"class":544},[54,11920,147],{"class":88},[54,11922,830],{"class":544},[54,11924,8240],{"class":548},[54,11926,545],{"class":544},[54,11928,103],{"class":88},[54,11930,830],{"class":544},[54,11932,9755],{"class":548},[54,11934,545],{"class":544},[54,11936,147],{"class":88},[54,11938,818],{"class":88},[54,11940,11837],{"class":99},[54,11942,103],{"class":88},[54,11944,11842],{"class":99},[54,11946,103],{"class":88},[54,11948,11847],{"class":99},[54,11950,11951],{"class":88},"],",[54,11953,830],{"class":544},[54,11955,9739],{"class":548},[54,11957,545],{"class":544},[54,11959,147],{"class":88},[54,11961,11962],{"class":64}," true",[54,11964,103],{"class":88},[54,11966,830],{"class":544},[54,11968,9833],{"class":548},[54,11970,545],{"class":544},[54,11972,147],{"class":88},[54,11974,11975],{"class":64}," null",[54,11977,8258],{"class":88},[14,11979,11980],{},"Notice these JSON differences:",[149,11982,11983,11995,12001],{},[152,11984,11985,11986,134,11988,11990,11991,134,11993],{},"JSON uses ",[17,11987,11767],{},[17,11989,8763],{},", not ",[17,11992,9175],{},[17,11994,8767],{},[152,11996,11985,11997,11990,11999],{},[17,11998,8772],{},[17,12000,8775],{},[152,12002,12003],{},"JSON uses double quotes for strings",[40,12005,493],{"id":492},[14,12007,12008],{},"Here is a basic dictionary converted to a JSON string:",[45,12010,12012],{"className":47,"code":12011,"language":49,"meta":50,"style":50},"import json\n\nperson = {\"name\": \"Bob\", \"age\": 30}\njson_text = json.dumps(person)\n\nprint(json_text)\nprint(type(json_text))\n",[17,12013,12014,12020,12024,12061,12079,12083,12093],{"__ignoreMap":50},[54,12015,12016,12018],{"class":56,"line":57},[54,12017,61],{"class":60},[54,12019,8212],{"class":64},[54,12021,12022],{"class":56,"line":68},[54,12023,72],{"emptyLinePlaceholder":71},[54,12025,12026,12028,12030,12032,12034,12036,12038,12040,12042,12045,12047,12049,12051,12053,12055,12057,12059],{"class":56,"line":75},[54,12027,8555],{"class":64},[54,12029,82],{"class":81},[54,12031,8226],{"class":88},[54,12033,545],{"class":544},[54,12035,8231],{"class":548},[54,12037,545],{"class":544},[54,12039,147],{"class":88},[54,12041,830],{"class":544},[54,12043,12044],{"class":548},"Bob",[54,12046,545],{"class":544},[54,12048,103],{"class":88},[54,12050,830],{"class":544},[54,12052,8249],{"class":548},[54,12054,545],{"class":544},[54,12056,147],{"class":88},[54,12058,3981],{"class":99},[54,12060,8258],{"class":88},[54,12062,12063,12065,12067,12069,12071,12073,12075,12077],{"class":56,"line":112},[54,12064,9553],{"class":64},[54,12066,82],{"class":81},[54,12068,9558],{"class":64},[54,12070,89],{"class":88},[54,12072,9563],{"class":92},[54,12074,96],{"class":88},[54,12076,8660],{"class":92},[54,12078,109],{"class":88},[54,12080,12081],{"class":56,"line":887},[54,12082,72],{"emptyLinePlaceholder":71},[54,12084,12085,12087,12089,12091],{"class":56,"line":892},[54,12086,116],{"class":115},[54,12088,96],{"class":88},[54,12090,9578],{"class":92},[54,12092,109],{"class":88},[54,12094,12095,12097,12099,12101,12103,12105],{"class":56,"line":913},[54,12096,116],{"class":115},[54,12098,96],{"class":88},[54,12100,2208],{"class":429},[54,12102,96],{"class":88},[54,12104,9578],{"class":92},[54,12106,382],{"class":88},[14,12108,449],{},[45,12110,12112],{"className":47,"code":12111,"language":49,"meta":50,"style":50},"{\"name\": \"Bob\", \"age\": 30}\n\u003Cclass 'str'>\n",[17,12113,12114,12146],{"__ignoreMap":50},[54,12115,12116,12118,12120,12122,12124,12126,12128,12130,12132,12134,12136,12138,12140,12142,12144],{"class":56,"line":57},[54,12117,8711],{"class":88},[54,12119,545],{"class":544},[54,12121,8231],{"class":548},[54,12123,545],{"class":544},[54,12125,147],{"class":88},[54,12127,830],{"class":544},[54,12129,12044],{"class":548},[54,12131,545],{"class":544},[54,12133,103],{"class":88},[54,12135,830],{"class":544},[54,12137,8249],{"class":548},[54,12139,545],{"class":544},[54,12141,147],{"class":88},[54,12143,3981],{"class":99},[54,12145,8258],{"class":88},[54,12147,12148,12150,12152,12154,12156,12158],{"class":56,"line":68},[54,12149,2227],{"class":81},[54,12151,2231],{"class":2230},[54,12153,2234],{"class":544},[54,12155,3173],{"class":548},[54,12157,2240],{"class":544},[54,12159,2243],{"class":81},[14,12161,12162],{},"Important details:",[149,12164,12165,12168,12171],{},[152,12166,12167],{},"The result uses double quotes",[152,12169,12170],{},"The result is a string",[152,12172,12173],{},"It is JSON-formatted text, not a Python dictionary",[14,12175,12176,12177,89],{},"If your goal is specifically to convert a dictionary, see ",[192,12178,9628],{"href":9627},[40,12180,12182,12183],{"id":12181},"pretty-printing-with-indent","Pretty printing with ",[17,12184,8797],{},[14,12186,12187,12188,12190],{},"By default, ",[17,12189,8371],{}," returns compact JSON on one line.",[14,12192,12193,12194,12196],{},"You can make it easier to read with the ",[17,12195,8797],{}," argument:",[45,12198,12200],{"className":47,"code":12199,"language":49,"meta":50,"style":50},"import json\n\ndata = {\n    \"name\": \"Alice\",\n    \"age\": 25,\n    \"skills\": [\"Python\", \"SQL\"]\n}\n\njson_text = json.dumps(data, indent=2)\nprint(json_text)\n",[17,12201,12202,12208,12212,12220,12238,12252,12280,12284,12288,12314],{"__ignoreMap":50},[54,12203,12204,12206],{"class":56,"line":57},[54,12205,61],{"class":60},[54,12207,8212],{"class":64},[54,12209,12210],{"class":56,"line":68},[54,12211,72],{"emptyLinePlaceholder":71},[54,12213,12214,12216,12218],{"class":56,"line":75},[54,12215,8221],{"class":64},[54,12217,82],{"class":81},[54,12219,8560],{"class":88},[54,12221,12222,12224,12226,12228,12230,12232,12234,12236],{"class":56,"line":112},[54,12223,8565],{"class":544},[54,12225,8231],{"class":548},[54,12227,545],{"class":544},[54,12229,147],{"class":88},[54,12231,830],{"class":544},[54,12233,8240],{"class":548},[54,12235,545],{"class":544},[54,12237,8580],{"class":88},[54,12239,12240,12242,12244,12246,12248,12250],{"class":56,"line":887},[54,12241,8565],{"class":544},[54,12243,8249],{"class":548},[54,12245,545],{"class":544},[54,12247,147],{"class":88},[54,12249,11369],{"class":99},[54,12251,8580],{"class":88},[54,12253,12254,12256,12258,12260,12262,12264,12266,12268,12270,12272,12274,12276,12278],{"class":56,"line":892},[54,12255,8565],{"class":544},[54,12257,8860],{"class":548},[54,12259,545],{"class":544},[54,12261,147],{"class":88},[54,12263,818],{"class":88},[54,12265,545],{"class":544},[54,12267,8871],{"class":548},[54,12269,545],{"class":544},[54,12271,103],{"class":88},[54,12273,830],{"class":544},[54,12275,8880],{"class":548},[54,12277,545],{"class":544},[54,12279,469],{"class":88},[54,12281,12282],{"class":56,"line":913},[54,12283,8258],{"class":88},[54,12285,12286],{"class":56,"line":2121},[54,12287,72],{"emptyLinePlaceholder":71},[54,12289,12290,12292,12294,12296,12298,12300,12302,12304,12306,12308,12310,12312],{"class":56,"line":2136},[54,12291,9553],{"class":64},[54,12293,82],{"class":81},[54,12295,9558],{"class":64},[54,12297,89],{"class":88},[54,12299,9563],{"class":92},[54,12301,96],{"class":88},[54,12303,8313],{"class":92},[54,12305,103],{"class":88},[54,12307,8941],{"class":8940},[54,12309,82],{"class":81},[54,12311,396],{"class":99},[54,12313,109],{"class":88},[54,12315,12316,12318,12320,12322],{"class":56,"line":2151},[54,12317,116],{"class":115},[54,12319,96],{"class":88},[54,12321,9578],{"class":92},[54,12323,109],{"class":88},[14,12325,449],{},[45,12327,12329],{"className":47,"code":12328,"language":49,"meta":50,"style":50},"{\n  \"name\": \"Alice\",\n  \"age\": 25,\n  \"skills\": [\n    \"Python\",\n    \"SQL\"\n  ]\n}\n",[17,12330,12331,12335,12353,12367,12379,12389,12397,12401],{"__ignoreMap":50},[54,12332,12333],{"class":56,"line":57},[54,12334,8960],{"class":88},[54,12336,12337,12339,12341,12343,12345,12347,12349,12351],{"class":56,"line":68},[54,12338,8965],{"class":544},[54,12340,8231],{"class":548},[54,12342,545],{"class":544},[54,12344,147],{"class":88},[54,12346,830],{"class":544},[54,12348,8240],{"class":548},[54,12350,545],{"class":544},[54,12352,8580],{"class":88},[54,12354,12355,12357,12359,12361,12363,12365],{"class":56,"line":75},[54,12356,8965],{"class":544},[54,12358,8249],{"class":548},[54,12360,545],{"class":544},[54,12362,147],{"class":88},[54,12364,11369],{"class":99},[54,12366,8580],{"class":88},[54,12368,12369,12371,12373,12375,12377],{"class":56,"line":112},[54,12370,8965],{"class":544},[54,12372,8860],{"class":548},[54,12374,545],{"class":544},[54,12376,147],{"class":88},[54,12378,9006],{"class":88},[54,12380,12381,12383,12385,12387],{"class":56,"line":887},[54,12382,8565],{"class":544},[54,12384,8871],{"class":548},[54,12386,545],{"class":544},[54,12388,8580],{"class":88},[54,12390,12391,12393,12395],{"class":56,"line":892},[54,12392,8565],{"class":544},[54,12394,8880],{"class":548},[54,12396,5239],{"class":544},[54,12398,12399],{"class":56,"line":913},[54,12400,9029],{"class":88},[54,12402,12403],{"class":56,"line":2121},[54,12404,8258],{"class":88},[14,12406,7262],{},[149,12408,12409,12412,12415],{},[152,12410,12411],{},"Debugging",[152,12413,12414],{},"Logging",[152,12416,12417],{},"Reading JSON more easily",[14,12419,12420],{},"It still returns a string.",[14,12422,12423,12424,89],{},"If you want more formatting examples, see ",[192,12425,12426],{"href":9039},"how to pretty print JSON in Python",[40,12428,12430],{"id":12429},"sorting-keys","Sorting keys",[14,12432,126,12433,12435],{},[17,12434,9046],{}," to output dictionary keys in sorted order.",[45,12437,12439],{"className":47,"code":12438,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"b\": 2, \"a\": 1, \"c\": 3}\n\nprint(json.dumps(data, sort_keys=True))\n",[17,12440,12441,12447,12451,12495,12499],{"__ignoreMap":50},[54,12442,12443,12445],{"class":56,"line":57},[54,12444,61],{"class":60},[54,12446,8212],{"class":64},[54,12448,12449],{"class":56,"line":68},[54,12450,72],{"emptyLinePlaceholder":71},[54,12452,12453,12455,12457,12459,12461,12463,12465,12467,12469,12471,12473,12475,12477,12479,12481,12483,12485,12487,12489,12491,12493],{"class":56,"line":75},[54,12454,8221],{"class":64},[54,12456,82],{"class":81},[54,12458,8226],{"class":88},[54,12460,545],{"class":544},[54,12462,241],{"class":548},[54,12464,545],{"class":544},[54,12466,147],{"class":88},[54,12468,466],{"class":99},[54,12470,103],{"class":88},[54,12472,830],{"class":544},[54,12474,192],{"class":548},[54,12476,545],{"class":544},[54,12478,147],{"class":88},[54,12480,882],{"class":99},[54,12482,103],{"class":88},[54,12484,830],{"class":544},[54,12486,9099],{"class":548},[54,12488,545],{"class":544},[54,12490,147],{"class":88},[54,12492,379],{"class":99},[54,12494,8258],{"class":88},[54,12496,12497],{"class":56,"line":112},[54,12498,72],{"emptyLinePlaceholder":71},[54,12500,12501,12503,12505,12507,12509,12511,12513,12515,12517,12519,12521,12523],{"class":56,"line":887},[54,12502,116],{"class":115},[54,12504,96],{"class":88},[54,12506,8385],{"class":92},[54,12508,89],{"class":88},[54,12510,9563],{"class":92},[54,12512,96],{"class":88},[54,12514,8313],{"class":92},[54,12516,103],{"class":88},[54,12518,9170],{"class":8940},[54,12520,82],{"class":81},[54,12522,9175],{"class":8608},[54,12524,382],{"class":88},[14,12526,449],{},[45,12528,12530],{"className":47,"code":12529,"language":49,"meta":50,"style":50},"{\"a\": 1, \"b\": 2, \"c\": 3}\n",[17,12531,12532],{"__ignoreMap":50},[54,12533,12534,12536,12538,12540,12542,12544,12546,12548,12550,12552,12554,12556,12558,12560,12562,12564,12566,12568,12570],{"class":56,"line":57},[54,12535,8711],{"class":88},[54,12537,545],{"class":544},[54,12539,192],{"class":548},[54,12541,545],{"class":544},[54,12543,147],{"class":88},[54,12545,882],{"class":99},[54,12547,103],{"class":88},[54,12549,830],{"class":544},[54,12551,241],{"class":548},[54,12553,545],{"class":544},[54,12555,147],{"class":88},[54,12557,466],{"class":99},[54,12559,103],{"class":88},[54,12561,830],{"class":544},[54,12563,9099],{"class":548},[54,12565,545],{"class":544},[54,12567,147],{"class":88},[54,12569,379],{"class":99},[54,12571,8258],{"class":88},[14,12573,12574],{},"This can help when:",[149,12576,12577,12580,12583],{},[152,12578,12579],{},"Comparing JSON output",[152,12581,12582],{},"Making output more predictable",[152,12584,12585],{},"Keeping test results consistent",[14,12587,12588,12589,147],{},"You can combine it with ",[17,12590,8797],{},[45,12592,12594],{"className":47,"code":12593,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"b\": 2, \"a\": 1, \"c\": 3}\n\nprint(json.dumps(data, indent=2, sort_keys=True))\n",[17,12595,12596,12602,12606,12650,12654],{"__ignoreMap":50},[54,12597,12598,12600],{"class":56,"line":57},[54,12599,61],{"class":60},[54,12601,8212],{"class":64},[54,12603,12604],{"class":56,"line":68},[54,12605,72],{"emptyLinePlaceholder":71},[54,12607,12608,12610,12612,12614,12616,12618,12620,12622,12624,12626,12628,12630,12632,12634,12636,12638,12640,12642,12644,12646,12648],{"class":56,"line":75},[54,12609,8221],{"class":64},[54,12611,82],{"class":81},[54,12613,8226],{"class":88},[54,12615,545],{"class":544},[54,12617,241],{"class":548},[54,12619,545],{"class":544},[54,12621,147],{"class":88},[54,12623,466],{"class":99},[54,12625,103],{"class":88},[54,12627,830],{"class":544},[54,12629,192],{"class":548},[54,12631,545],{"class":544},[54,12633,147],{"class":88},[54,12635,882],{"class":99},[54,12637,103],{"class":88},[54,12639,830],{"class":544},[54,12641,9099],{"class":548},[54,12643,545],{"class":544},[54,12645,147],{"class":88},[54,12647,379],{"class":99},[54,12649,8258],{"class":88},[54,12651,12652],{"class":56,"line":112},[54,12653,72],{"emptyLinePlaceholder":71},[54,12655,12656,12658,12660,12662,12664,12666,12668,12670,12672,12674,12676,12678,12680,12682,12684,12686],{"class":56,"line":887},[54,12657,116],{"class":115},[54,12659,96],{"class":88},[54,12661,8385],{"class":92},[54,12663,89],{"class":88},[54,12665,9563],{"class":92},[54,12667,96],{"class":88},[54,12669,8313],{"class":92},[54,12671,103],{"class":88},[54,12673,8941],{"class":8940},[54,12675,82],{"class":81},[54,12677,396],{"class":99},[54,12679,103],{"class":88},[54,12681,9170],{"class":8940},[54,12683,82],{"class":81},[54,12685,9175],{"class":8608},[54,12687,382],{"class":88},[40,12689,12691,7817,12693],{"id":12690},"jsondumps-vs-jsondump",[17,12692,8371],{},[17,12694,8190],{},[14,12696,12697],{},"These two functions are similar, but they do different things.",[602,12699,12700],{"id":9498},[17,12701,8371],{},[149,12703,12704],{},[152,12705,12706],{},"Returns a JSON string",[45,12708,12710],{"className":47,"code":12709,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\"}\njson_text = json.dumps(data)\n\nprint(json_text)\n",[17,12711,12712,12718,12722,12746,12764,12768],{"__ignoreMap":50},[54,12713,12714,12716],{"class":56,"line":57},[54,12715,61],{"class":60},[54,12717,8212],{"class":64},[54,12719,12720],{"class":56,"line":68},[54,12721,72],{"emptyLinePlaceholder":71},[54,12723,12724,12726,12728,12730,12732,12734,12736,12738,12740,12742,12744],{"class":56,"line":75},[54,12725,8221],{"class":64},[54,12727,82],{"class":81},[54,12729,8226],{"class":88},[54,12731,545],{"class":544},[54,12733,8231],{"class":548},[54,12735,545],{"class":544},[54,12737,147],{"class":88},[54,12739,830],{"class":544},[54,12741,8240],{"class":548},[54,12743,545],{"class":544},[54,12745,8258],{"class":88},[54,12747,12748,12750,12752,12754,12756,12758,12760,12762],{"class":56,"line":112},[54,12749,9553],{"class":64},[54,12751,82],{"class":81},[54,12753,9558],{"class":64},[54,12755,89],{"class":88},[54,12757,9563],{"class":92},[54,12759,96],{"class":88},[54,12761,8313],{"class":92},[54,12763,109],{"class":88},[54,12765,12766],{"class":56,"line":887},[54,12767,72],{"emptyLinePlaceholder":71},[54,12769,12770,12772,12774,12776],{"class":56,"line":892},[54,12771,116],{"class":115},[54,12773,96],{"class":88},[54,12775,9578],{"class":92},[54,12777,109],{"class":88},[602,12779,12780],{"id":9397},[17,12781,8190],{},[149,12783,12784],{},[152,12785,12786],{},"Writes JSON directly to a file object",[45,12788,12789],{"className":47,"code":9407,"language":49,"meta":50,"style":50},[17,12790,12791,12797,12801,12825,12829,12859],{"__ignoreMap":50},[54,12792,12793,12795],{"class":56,"line":57},[54,12794,61],{"class":60},[54,12796,8212],{"class":64},[54,12798,12799],{"class":56,"line":68},[54,12800,72],{"emptyLinePlaceholder":71},[54,12802,12803,12805,12807,12809,12811,12813,12815,12817,12819,12821,12823],{"class":56,"line":75},[54,12804,8221],{"class":64},[54,12806,82],{"class":81},[54,12808,8226],{"class":88},[54,12810,545],{"class":544},[54,12812,8231],{"class":548},[54,12814,545],{"class":544},[54,12816,147],{"class":88},[54,12818,830],{"class":544},[54,12820,8240],{"class":548},[54,12822,545],{"class":544},[54,12824,8258],{"class":88},[54,12826,12827],{"class":56,"line":112},[54,12828,72],{"emptyLinePlaceholder":71},[54,12830,12831,12833,12835,12837,12839,12841,12843,12845,12847,12849,12851,12853,12855,12857],{"class":56,"line":887},[54,12832,8267],{"class":60},[54,12834,8270],{"class":115},[54,12836,96],{"class":88},[54,12838,545],{"class":544},[54,12840,8277],{"class":548},[54,12842,545],{"class":544},[54,12844,103],{"class":88},[54,12846,830],{"class":544},[54,12848,8286],{"class":548},[54,12850,545],{"class":544},[54,12852,876],{"class":88},[54,12854,7631],{"class":60},[54,12856,8296],{"class":8295},[54,12858,7572],{"class":88},[54,12860,12861,12863,12865,12867,12869,12871,12873,12875],{"class":56,"line":892},[54,12862,8303],{"class":64},[54,12864,89],{"class":88},[54,12866,8308],{"class":92},[54,12868,96],{"class":88},[54,12870,8313],{"class":92},[54,12872,103],{"class":88},[54,12874,8296],{"class":8295},[54,12876,109],{"class":88},[14,12878,3040],{},[149,12880,12881,12886],{},[152,12882,12883,12885],{},[17,12884,8371],{}," when you want a string",[152,12887,12888,12890],{},[17,12889,8190],{}," when you want to write to a file",[14,12892,12893,12894,89],{},"For a full explanation, see ",[192,12895,12896,12898],{"href":11295},[17,12897,8190],{}," function explained",[40,12900,12902],{"id":12901},"common-errors-and-limits","Common errors and limits",[14,12904,12905],{},"Not every Python object can be converted by default.",[14,12907,12908],{},"These often cause problems:",[149,12910,12911,12915,12918],{},[152,12912,12913],{},[17,12914,9922],{},[152,12916,12917],{},"Custom class instances",[152,12919,12920],{},"Functions",[14,12922,12923,12924,89],{},"These usually raise a ",[17,12925,10040],{},[14,12927,12928,12929,147],{},"Example with a ",[17,12930,9922],{},[45,12932,12934],{"className":47,"code":12933,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"numbers\": {1, 2, 3}}\nprint(json.dumps(data))\n",[17,12935,12936,12942,12946,12976],{"__ignoreMap":50},[54,12937,12938,12940],{"class":56,"line":57},[54,12939,61],{"class":60},[54,12941,8212],{"class":64},[54,12943,12944],{"class":56,"line":68},[54,12945,72],{"emptyLinePlaceholder":71},[54,12947,12948,12950,12952,12954,12956,12958,12960,12962,12964,12966,12968,12970,12972,12974],{"class":56,"line":75},[54,12949,8221],{"class":64},[54,12951,82],{"class":81},[54,12953,8226],{"class":88},[54,12955,545],{"class":544},[54,12957,9956],{"class":548},[54,12959,545],{"class":544},[54,12961,147],{"class":88},[54,12963,8226],{"class":88},[54,12965,100],{"class":99},[54,12967,103],{"class":88},[54,12969,466],{"class":99},[54,12971,103],{"class":88},[54,12973,379],{"class":99},[54,12975,9975],{"class":88},[54,12977,12978,12980,12982,12984,12986,12988,12990,12992],{"class":56,"line":112},[54,12979,116],{"class":115},[54,12981,96],{"class":88},[54,12983,8385],{"class":92},[54,12985,89],{"class":88},[54,12987,9563],{"class":92},[54,12989,96],{"class":88},[54,12991,8313],{"class":92},[54,12993,382],{"class":88},[14,12995,12996],{},"This raises an error like:",[45,12998,12999],{"className":47,"code":10033,"language":49,"meta":50,"style":50},[17,13000,13001],{"__ignoreMap":50},[54,13002,13003,13005,13007,13009,13011,13013,13015,13017,13019],{"class":56,"line":57},[54,13004,10040],{"class":429},[54,13006,147],{"class":88},[54,13008,10045],{"class":64},[54,13010,2208],{"class":429},[54,13012,10050],{"class":429},[54,13014,10053],{"class":81},[54,13016,10056],{"class":81},[54,13018,10059],{"class":4870},[54,13020,10062],{"class":64},[40,13022,13024],{"id":13023},"how-to-fix-unsupported-object-errors","How to fix unsupported object errors",[14,13026,13027,13028,89],{},"A common fix is to convert unsupported values into supported ones before calling ",[17,13029,8371],{},[602,13031,13033,13034,10068,13036],{"id":13032},"convert-a-set-to-a-list","Convert a ",[17,13035,9922],{},[17,13037,430],{},[45,13039,13041],{"className":47,"code":13040,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"numbers\": list({1, 2, 3})}\nprint(json.dumps(data))\n",[17,13042,13043,13049,13053,13085],{"__ignoreMap":50},[54,13044,13045,13047],{"class":56,"line":57},[54,13046,61],{"class":60},[54,13048,8212],{"class":64},[54,13050,13051],{"class":56,"line":68},[54,13052,72],{"emptyLinePlaceholder":71},[54,13054,13055,13057,13059,13061,13063,13065,13067,13069,13071,13073,13075,13077,13079,13081,13083],{"class":56,"line":75},[54,13056,8221],{"class":64},[54,13058,82],{"class":81},[54,13060,8226],{"class":88},[54,13062,545],{"class":544},[54,13064,9956],{"class":548},[54,13066,545],{"class":544},[54,13068,147],{"class":88},[54,13070,10105],{"class":429},[54,13072,10108],{"class":88},[54,13074,100],{"class":99},[54,13076,103],{"class":88},[54,13078,466],{"class":99},[54,13080,103],{"class":88},[54,13082,379],{"class":99},[54,13084,10121],{"class":88},[54,13086,13087,13089,13091,13093,13095,13097,13099,13101],{"class":56,"line":112},[54,13088,116],{"class":115},[54,13090,96],{"class":88},[54,13092,8385],{"class":92},[54,13094,89],{"class":88},[54,13096,9563],{"class":92},[54,13098,96],{"class":88},[54,13100,8313],{"class":92},[54,13102,382],{"class":88},[602,13104,13106],{"id":13105},"convert-a-custom-object-to-a-dictionary","Convert a custom object to a dictionary",[45,13108,13110],{"className":47,"code":13109,"language":49,"meta":50,"style":50},"import json\n\nclass User:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\nuser = User(\"Alice\", 25)\n\ndata = {\n    \"name\": user.name,\n    \"age\": user.age\n}\n\nprint(json.dumps(data))\n",[17,13111,13112,13118,13122,13130,13151,13163,13176,13180,13202,13206,13214,13232,13249,13253,13257],{"__ignoreMap":50},[54,13113,13114,13116],{"class":56,"line":57},[54,13115,61],{"class":60},[54,13117,8212],{"class":64},[54,13119,13120],{"class":56,"line":68},[54,13121,72],{"emptyLinePlaceholder":71},[54,13123,13124,13126,13128],{"class":56,"line":75},[54,13125,2231],{"class":2230},[54,13127,10812],{"class":10811},[54,13129,7572],{"class":88},[54,13131,13132,13134,13136,13138,13140,13142,13144,13146,13149],{"class":56,"line":112},[54,13133,10819],{"class":2230},[54,13135,10822],{"class":115},[54,13137,96],{"class":88},[54,13139,10828],{"class":10827},[54,13141,103],{"class":88},[54,13143,10834],{"class":10833},[54,13145,103],{"class":88},[54,13147,13148],{"class":10833}," age",[54,13150,10837],{"class":88},[54,13152,13153,13155,13157,13159,13161],{"class":56,"line":887},[54,13154,10842],{"class":4870},[54,13156,89],{"class":88},[54,13158,8231],{"class":1693},[54,13160,10849],{"class":81},[54,13162,10852],{"class":64},[54,13164,13165,13167,13169,13171,13173],{"class":56,"line":892},[54,13166,10842],{"class":4870},[54,13168,89],{"class":88},[54,13170,8249],{"class":1693},[54,13172,10849],{"class":81},[54,13174,13175],{"class":64}," age\n",[54,13177,13178],{"class":56,"line":913},[54,13179,72],{"emptyLinePlaceholder":71},[54,13181,13182,13184,13186,13188,13190,13192,13194,13196,13198,13200],{"class":56,"line":2121},[54,13183,10861],{"class":64},[54,13185,82],{"class":81},[54,13187,10812],{"class":92},[54,13189,96],{"class":88},[54,13191,545],{"class":544},[54,13193,8240],{"class":548},[54,13195,545],{"class":544},[54,13197,103],{"class":88},[54,13199,11369],{"class":99},[54,13201,109],{"class":88},[54,13203,13204],{"class":56,"line":2136},[54,13205,72],{"emptyLinePlaceholder":71},[54,13207,13208,13210,13212],{"class":56,"line":2151},[54,13209,8221],{"class":64},[54,13211,82],{"class":81},[54,13213,8560],{"class":88},[54,13215,13216,13218,13220,13222,13224,13226,13228,13230],{"class":56,"line":4442},[54,13217,8565],{"class":544},[54,13219,8231],{"class":548},[54,13221,545],{"class":544},[54,13223,147],{"class":88},[54,13225,11022],{"class":64},[54,13227,89],{"class":88},[54,13229,8231],{"class":1693},[54,13231,8580],{"class":88},[54,13233,13234,13236,13238,13240,13242,13244,13246],{"class":56,"line":4468},[54,13235,8565],{"class":544},[54,13237,8249],{"class":548},[54,13239,545],{"class":544},[54,13241,147],{"class":88},[54,13243,11022],{"class":64},[54,13245,89],{"class":88},[54,13247,13248],{"class":1693},"age\n",[54,13250,13251],{"class":56,"line":9843},[54,13252,8258],{"class":88},[54,13254,13255],{"class":56,"line":9848},[54,13256,72],{"emptyLinePlaceholder":71},[54,13258,13259,13261,13263,13265,13267,13269,13271,13273],{"class":56,"line":9853},[54,13260,116],{"class":115},[54,13262,96],{"class":88},[54,13264,8385],{"class":92},[54,13266,89],{"class":88},[54,13268,9563],{"class":92},[54,13270,96],{"class":88},[54,13272,8313],{"class":92},[54,13274,382],{"class":88},[14,13276,13277],{},"If you are seeing this error, read TypeError: Object is not JSON serializable.",[40,13279,978],{"id":977},[14,13281,13282],{},"Beginners often run into these problems:",[149,13284,13285,13291,13294,13299,13302],{},[152,13286,13287,13288,13290],{},"Trying to convert a ",[17,13289,9922],{}," directly",[152,13292,13293],{},"Passing a custom object without converting it first",[152,13295,5949,13296,13298],{},[17,13297,8371],{}," to write to a file",[152,13300,13301],{},"Confusing the returned JSON string with a Python dictionary",[152,13303,13304,13305,160],{},"Forgetting to import the ",[17,13306,8385],{},[14,13308,13309],{},"Useful checks while debugging:",[45,13311,13313],{"className":47,"code":13312,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Alice\"}\n\nprint(type(data))\nprint(data)\nprint(type(json.dumps(data)))\nprint(json.dumps(data, indent=2))\n",[17,13314,13315,13321,13325,13349,13353,13367,13377,13399],{"__ignoreMap":50},[54,13316,13317,13319],{"class":56,"line":57},[54,13318,61],{"class":60},[54,13320,8212],{"class":64},[54,13322,13323],{"class":56,"line":68},[54,13324,72],{"emptyLinePlaceholder":71},[54,13326,13327,13329,13331,13333,13335,13337,13339,13341,13343,13345,13347],{"class":56,"line":75},[54,13328,8221],{"class":64},[54,13330,82],{"class":81},[54,13332,8226],{"class":88},[54,13334,545],{"class":544},[54,13336,8231],{"class":548},[54,13338,545],{"class":544},[54,13340,147],{"class":88},[54,13342,830],{"class":544},[54,13344,8240],{"class":548},[54,13346,545],{"class":544},[54,13348,8258],{"class":88},[54,13350,13351],{"class":56,"line":112},[54,13352,72],{"emptyLinePlaceholder":71},[54,13354,13355,13357,13359,13361,13363,13365],{"class":56,"line":887},[54,13356,116],{"class":115},[54,13358,96],{"class":88},[54,13360,2208],{"class":429},[54,13362,96],{"class":88},[54,13364,8313],{"class":92},[54,13366,382],{"class":88},[54,13368,13369,13371,13373,13375],{"class":56,"line":892},[54,13370,116],{"class":115},[54,13372,96],{"class":88},[54,13374,8313],{"class":92},[54,13376,109],{"class":88},[54,13378,13379,13381,13383,13385,13387,13389,13391,13393,13395,13397],{"class":56,"line":913},[54,13380,116],{"class":115},[54,13382,96],{"class":88},[54,13384,2208],{"class":429},[54,13386,96],{"class":88},[54,13388,8385],{"class":92},[54,13390,89],{"class":88},[54,13392,9563],{"class":92},[54,13394,96],{"class":88},[54,13396,8313],{"class":92},[54,13398,446],{"class":88},[54,13400,13401,13403,13405,13407,13409,13411,13413,13415,13417,13419,13421,13423],{"class":56,"line":2121},[54,13402,116],{"class":115},[54,13404,96],{"class":88},[54,13406,8385],{"class":92},[54,13408,89],{"class":88},[54,13410,9563],{"class":92},[54,13412,96],{"class":88},[54,13414,8313],{"class":92},[54,13416,103],{"class":88},[54,13418,8941],{"class":8940},[54,13420,82],{"class":81},[54,13422,396],{"class":99},[54,13424,382],{"class":88},[14,13426,13427],{},"These help you confirm:",[149,13429,13430,13433,13436],{},[152,13431,13432],{},"What type your original data is",[152,13434,13435],{},"What the JSON output looks like",[152,13437,13438,13439,13441],{},"That ",[17,13440,8371],{}," returns a string",[40,13443,1419],{"id":1418},[602,13445,8075,13447,134,13449,3409],{"id":13446},"what-is-the-difference-between-jsondumps-and-jsonloads",[17,13448,8371],{},[17,13450,13451],{},"json.loads()",[14,13453,13454,13456,13457,13459],{},[17,13455,8371],{}," converts a Python object to a JSON string. ",[17,13458,13451],{}," converts a JSON string back into a Python object.",[14,13461,13462,13463,89],{},"See ",[192,13464,13466,12898],{"href":13465},"\u002Fstandard-library\u002Fjson.loads-function-explained",[17,13467,13451],{},[602,13469,8346,13471,13473],{"id":13470},"does-jsondumps-return-a-dictionary",[17,13472,8371],{}," return a dictionary?",[14,13475,13476],{},"No. It returns a string that contains JSON-formatted text.",[602,13478,8091,13480,13482],{"id":13479},"can-jsondumps-write-to-a-file",[17,13481,8371],{}," write to a file?",[14,13484,13485,13486,13488],{},"No. Use ",[17,13487,8190],{}," if you want to write JSON directly to a file.",[602,13490,3403,13492,13494],{"id":13491},"why-does-jsondumps-use-double-quotes",[17,13493,8371],{}," use double quotes?",[14,13496,13497],{},"JSON requires double quotes around string keys and string values.",[40,13499,1510],{"id":1509},[149,13501,13502,13506,13512,13518,13522],{},[152,13503,13504],{},[192,13505,8782],{"href":8781},[152,13507,13508],{},[192,13509,13510,12898],{"href":11295},[17,13511,8190],{},[152,13513,13514],{},[192,13515,13516,12898],{"href":13465},[17,13517,13451],{},[152,13519,13520],{},[192,13521,11234],{"href":9627},[152,13523,13524],{},[192,13525,13526],{"href":9039},"How to pretty print JSON in Python",[1545,13528,13529],{},"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 .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 .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 .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}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":50,"searchDepth":68,"depth":68,"links":13531},[13532,13533,13535,13536,13538,13539,13541,13542,13547,13548,13553,13554,13564],{"id":3595,"depth":68,"text":3596},{"id":11448,"depth":68,"text":13534},"What json.dumps() does",{"id":198,"depth":68,"text":199},{"id":11681,"depth":68,"text":13537},"Python types that work with json.dumps()",{"id":492,"depth":68,"text":493},{"id":12181,"depth":68,"text":13540},"Pretty printing with indent",{"id":12429,"depth":68,"text":12430},{"id":12690,"depth":68,"text":13543,"children":13544},"json.dumps() vs json.dump()",[13545,13546],{"id":9498,"depth":75,"text":8371},{"id":9397,"depth":75,"text":8190},{"id":12901,"depth":68,"text":12902},{"id":13023,"depth":68,"text":13024,"children":13549},[13550,13552],{"id":13032,"depth":75,"text":13551},"Convert a set to a list",{"id":13105,"depth":75,"text":13106},{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":13555},[13556,13558,13560,13562],{"id":13446,"depth":75,"text":13557},"What is the difference between json.dumps() and json.loads()?",{"id":13470,"depth":75,"text":13559},"Does json.dumps() return a dictionary?",{"id":13479,"depth":75,"text":13561},"Can json.dumps() write to a file?",{"id":13491,"depth":75,"text":13563},"Why does json.dumps() use double quotes?",{"id":1509,"depth":68,"text":1510},"Master json.dumps function explained in our comprehensive Python beginner guide.",{},{"title":11301,"description":13565},"standard-library\u002Fjson.dumps-function-explained","3j03A5jDKeYPwSuQxkA5X0rf9b-N1-SVraM2hgImhP0",{"id":13571,"title":13572,"body":13573,"description":15722,"extension":1577,"meta":15723,"navigation":71,"path":15724,"seo":15725,"stem":15726,"__hash__":15727},"content\u002Fstandard-library\u002Fjson.load-function-explained.md","json.load() Function Explained",{"type":7,"value":13574,"toc":15678},[13575,13581,13586,13589,13591,13696,13715,13720,13724,13740,13743,13754,13764,13766,13768,13788,13790,13806,13809,13884,13887,13924,13931,13938,13944,13949,13952,13994,13997,14099,14102,14246,14249,14371,14375,14378,14382,14387,14450,14454,14462,14505,14512,14515,14538,14542,14548,14552,14555,14698,14700,14764,14767,14789,14792,14798,14805,14808,14814,14819,14893,14899,14904,14965,14967,15005,15008,15019,15026,15030,15035,15039,15041,15077,15080,15092,15094,15168,15172,15175,15178,15201,15203,15208,15211,15241,15251,15254,15268,15271,15285,15288,15328,15332,15338,15340,15416,15423,15429,15434,15437,15451,15458,15462,15468,15492,15495,15509,15552,15555,15569,15571,15578,15586,15592,15609,15615,15620,15626,15629,15631,15675],[10,13576,13578,11309],{"id":13577},"jsonload-function-explained",[17,13579,13580],{},"json.load()",[14,13582,13583,13585],{},[17,13584,13580],{}," reads JSON data from an open file and converts it into normal Python data.",[14,13587,13588],{},"Use it when your JSON is stored in a file, not in a Python string. This is a common way to load configuration files, saved data, or exported API responses.",[40,13590,43],{"id":42},[45,13592,13594],{"className":47,"code":13593,"language":49,"meta":50,"style":50},"import json\n\nwith open(\"data.json\", \"r\", encoding=\"utf-8\") as file:\n    data = json.load(file)\n\nprint(data)\nprint(type(data))\n",[17,13595,13596,13602,13606,13648,13668,13672,13682],{"__ignoreMap":50},[54,13597,13598,13600],{"class":56,"line":57},[54,13599,61],{"class":60},[54,13601,8212],{"class":64},[54,13603,13604],{"class":56,"line":68},[54,13605,72],{"emptyLinePlaceholder":71},[54,13607,13608,13610,13612,13614,13616,13618,13620,13622,13624,13626,13628,13630,13632,13634,13636,13638,13640,13642,13644,13646],{"class":56,"line":75},[54,13609,8267],{"class":60},[54,13611,8270],{"class":115},[54,13613,96],{"class":88},[54,13615,545],{"class":544},[54,13617,8277],{"class":548},[54,13619,545],{"class":544},[54,13621,103],{"class":88},[54,13623,830],{"class":544},[54,13625,10658],{"class":548},[54,13627,545],{"class":544},[54,13629,103],{"class":88},[54,13631,9332],{"class":8940},[54,13633,82],{"class":81},[54,13635,545],{"class":544},[54,13637,9339],{"class":548},[54,13639,545],{"class":544},[54,13641,876],{"class":88},[54,13643,7631],{"class":60},[54,13645,8296],{"class":8295},[54,13647,7572],{"class":88},[54,13649,13650,13653,13655,13657,13659,13662,13664,13666],{"class":56,"line":112},[54,13651,13652],{"class":64},"    data ",[54,13654,82],{"class":81},[54,13656,9558],{"class":64},[54,13658,89],{"class":88},[54,13660,13661],{"class":92},"load",[54,13663,96],{"class":88},[54,13665,8412],{"class":8295},[54,13667,109],{"class":88},[54,13669,13670],{"class":56,"line":887},[54,13671,72],{"emptyLinePlaceholder":71},[54,13673,13674,13676,13678,13680],{"class":56,"line":892},[54,13675,116],{"class":115},[54,13677,96],{"class":88},[54,13679,8313],{"class":92},[54,13681,109],{"class":88},[54,13683,13684,13686,13688,13690,13692,13694],{"class":56,"line":913},[54,13685,116],{"class":115},[54,13687,96],{"class":88},[54,13689,2208],{"class":429},[54,13691,96],{"class":88},[54,13693,8313],{"class":92},[54,13695,382],{"class":88},[14,13697,126,13698,13700,13701,320,13703,320,13705,320,13707,320,13709,320,13711,324,13713,89],{},[17,13699,13580],{}," when you already have an open file object. It converts JSON from the file into Python data like ",[17,13702,9644],{},[17,13704,430],{},[17,13706,3173],{},[17,13708,9657],{},[17,13710,9662],{},[17,13712,9667],{},[17,13714,8775],{},[40,13716,1736,13718,1739],{"id":13717},"what-jsonload-does",[17,13719,13580],{},[14,13721,13722,147],{},[17,13723,13580],{},[149,13725,13726,13729,13732,13735],{},[152,13727,13728],{},"Reads JSON content from a file object",[152,13730,13731],{},"Parses the JSON text into Python data",[152,13733,13734],{},"Usually returns a dictionary or list",[152,13736,13737,13738,160],{},"Belongs to Python’s built-in ",[17,13739,8385],{},[14,13741,13742],{},"Before using it, import the module:",[45,13744,13746],{"className":47,"code":13745,"language":49,"meta":50,"style":50},"import json\n",[17,13747,13748],{"__ignoreMap":50},[54,13749,13750,13752],{"class":56,"line":57},[54,13751,61],{"class":60},[54,13753,8212],{"class":64},[14,13755,13756,13757,89],{},"If you want a broader introduction first, see the ",[192,13758,13760,13761,13763],{"href":13759},"\u002Fstandard-library\u002Fpython-json-module-overview\u002F","Python ",[17,13762,8385],{}," module overview",[40,13765,199],{"id":198},[14,13767,202],{},[45,13769,13771],{"className":47,"code":13770,"language":49,"meta":50,"style":50},"json.load(file_object)\n",[17,13772,13773],{"__ignoreMap":50},[54,13774,13775,13777,13779,13781,13783,13786],{"class":56,"line":57},[54,13776,8385],{"class":64},[54,13778,89],{"class":88},[54,13780,13661],{"class":92},[54,13782,96],{"class":88},[54,13784,13785],{"class":92},"file_object",[54,13787,109],{"class":88},[14,13789,4136],{},[149,13791,13792,13798,13803],{},[152,13793,13794,13795],{},"Import the module first with ",[17,13796,13797],{},"import json",[152,13799,13800,13801],{},"Open the file before calling ",[17,13802,13580],{},[152,13804,13805],{},"Pass the file variable, not the file name string",[14,13807,13808],{},"Correct:",[45,13810,13812],{"className":47,"code":13811,"language":49,"meta":50,"style":50},"import json\n\nwith open(\"data.json\", \"r\", encoding=\"utf-8\") as file:\n    data = json.load(file)\n",[17,13813,13814,13820,13824,13866],{"__ignoreMap":50},[54,13815,13816,13818],{"class":56,"line":57},[54,13817,61],{"class":60},[54,13819,8212],{"class":64},[54,13821,13822],{"class":56,"line":68},[54,13823,72],{"emptyLinePlaceholder":71},[54,13825,13826,13828,13830,13832,13834,13836,13838,13840,13842,13844,13846,13848,13850,13852,13854,13856,13858,13860,13862,13864],{"class":56,"line":75},[54,13827,8267],{"class":60},[54,13829,8270],{"class":115},[54,13831,96],{"class":88},[54,13833,545],{"class":544},[54,13835,8277],{"class":548},[54,13837,545],{"class":544},[54,13839,103],{"class":88},[54,13841,830],{"class":544},[54,13843,10658],{"class":548},[54,13845,545],{"class":544},[54,13847,103],{"class":88},[54,13849,9332],{"class":8940},[54,13851,82],{"class":81},[54,13853,545],{"class":544},[54,13855,9339],{"class":548},[54,13857,545],{"class":544},[54,13859,876],{"class":88},[54,13861,7631],{"class":60},[54,13863,8296],{"class":8295},[54,13865,7572],{"class":88},[54,13867,13868,13870,13872,13874,13876,13878,13880,13882],{"class":56,"line":112},[54,13869,13652],{"class":64},[54,13871,82],{"class":81},[54,13873,9558],{"class":64},[54,13875,89],{"class":88},[54,13877,13661],{"class":92},[54,13879,96],{"class":88},[54,13881,8412],{"class":8295},[54,13883,109],{"class":88},[14,13885,13886],{},"Incorrect:",[45,13888,13890],{"className":47,"code":13889,"language":49,"meta":50,"style":50},"import json\n\ndata = json.load(\"data.json\")\n",[17,13891,13892,13898,13902],{"__ignoreMap":50},[54,13893,13894,13896],{"class":56,"line":57},[54,13895,61],{"class":60},[54,13897,8212],{"class":64},[54,13899,13900],{"class":56,"line":68},[54,13901,72],{"emptyLinePlaceholder":71},[54,13903,13904,13906,13908,13910,13912,13914,13916,13918,13920,13922],{"class":56,"line":75},[54,13905,8221],{"class":64},[54,13907,82],{"class":81},[54,13909,9558],{"class":64},[54,13911,89],{"class":88},[54,13913,13661],{"class":92},[54,13915,96],{"class":88},[54,13917,545],{"class":544},[54,13919,8277],{"class":548},[54,13921,545],{"class":544},[54,13923,109],{"class":88},[14,13925,13926,13927,13930],{},"The second example fails because ",[17,13928,13929],{},"\"data.json\""," is a string path, not an open file object.",[14,13932,13933,13934,89],{},"If you need help opening files, see ",[192,13935,13937],{"href":13936},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","how to read a file in Python",[40,13939,1736,13941,13943],{"id":13940},"what-jsonload-returns",[17,13942,13580],{}," returns",[14,13945,13946,13948],{},[17,13947,13580],{}," returns the Python type that matches the JSON content.",[14,13950,13951],{},"Here is the usual conversion:",[149,13953,13954,13959,13964,13969,13976,13988],{},[152,13955,13956,13957],{},"JSON object → Python ",[17,13958,9644],{},[152,13960,13961,13962],{},"JSON array → Python ",[17,13963,430],{},[152,13965,13966,13967],{},"JSON string → Python ",[17,13968,3173],{},[152,13970,13971,13972,5507,13974],{},"JSON number → Python ",[17,13973,9657],{},[17,13975,9662],{},[152,13977,13978,13979,134,13981,13983,13984,134,13986],{},"JSON ",[17,13980,11767],{},[17,13982,8763],{}," → Python ",[17,13985,9175],{},[17,13987,8767],{},[152,13989,13978,13990,13983,13992],{},[17,13991,8772],{},[17,13993,8775],{},[14,13995,13996],{},"Example JSON file:",[45,13998,14000],{"className":8703,"code":13999,"language":8385,"meta":50,"style":50},"{\n  \"name\": \"Maya\",\n  \"age\": 25,\n  \"is_admin\": false,\n  \"skills\": [\"Python\", \"SQL\"],\n  \"manager\": null\n}\n",[17,14001,14002,14006,14025,14039,14053,14081,14095],{"__ignoreMap":50},[54,14003,14004],{"class":56,"line":57},[54,14005,8960],{"class":88},[54,14007,14008,14010,14012,14014,14016,14018,14021,14023],{"class":56,"line":68},[54,14009,8965],{"class":8608},[54,14011,8231],{"class":8716},[54,14013,545],{"class":8608},[54,14015,147],{"class":88},[54,14017,830],{"class":544},[54,14019,14020],{"class":548},"Maya",[54,14022,545],{"class":544},[54,14024,8580],{"class":88},[54,14026,14027,14029,14031,14033,14035,14037],{"class":56,"line":75},[54,14028,8965],{"class":8608},[54,14030,8249],{"class":8716},[54,14032,545],{"class":8608},[54,14034,147],{"class":88},[54,14036,11369],{"class":99},[54,14038,8580],{"class":88},[54,14040,14041,14043,14045,14047,14049,14051],{"class":56,"line":112},[54,14042,8965],{"class":8608},[54,14044,8601],{"class":8716},[54,14046,545],{"class":8608},[54,14048,147],{"class":88},[54,14050,8751],{"class":8608},[54,14052,8580],{"class":88},[54,14054,14055,14057,14059,14061,14063,14065,14067,14069,14071,14073,14075,14077,14079],{"class":56,"line":887},[54,14056,8965],{"class":8608},[54,14058,8860],{"class":8716},[54,14060,545],{"class":8608},[54,14062,147],{"class":88},[54,14064,818],{"class":88},[54,14066,545],{"class":544},[54,14068,8871],{"class":548},[54,14070,545],{"class":544},[54,14072,103],{"class":88},[54,14074,830],{"class":544},[54,14076,8880],{"class":548},[54,14078,545],{"class":544},[54,14080,9774],{"class":88},[54,14082,14083,14085,14088,14090,14092],{"class":56,"line":892},[54,14084,8965],{"class":8608},[54,14086,14087],{"class":8716},"manager",[54,14089,545],{"class":8608},[54,14091,147],{"class":88},[54,14093,14094],{"class":8608}," null\n",[54,14096,14097],{"class":56,"line":913},[54,14098,8258],{"class":88},[14,14100,14101],{},"Python code:",[45,14103,14105],{"className":47,"code":14104,"language":49,"meta":50,"style":50},"import json\n\nwith open(\"data.json\", \"r\", encoding=\"utf-8\") as file:\n    data = json.load(file)\n\nprint(data)\nprint(type(data))\nprint(data[\"name\"])\nprint(type(data[\"skills\"]))\n",[17,14106,14107,14113,14117,14159,14177,14181,14191,14205,14223],{"__ignoreMap":50},[54,14108,14109,14111],{"class":56,"line":57},[54,14110,61],{"class":60},[54,14112,8212],{"class":64},[54,14114,14115],{"class":56,"line":68},[54,14116,72],{"emptyLinePlaceholder":71},[54,14118,14119,14121,14123,14125,14127,14129,14131,14133,14135,14137,14139,14141,14143,14145,14147,14149,14151,14153,14155,14157],{"class":56,"line":75},[54,14120,8267],{"class":60},[54,14122,8270],{"class":115},[54,14124,96],{"class":88},[54,14126,545],{"class":544},[54,14128,8277],{"class":548},[54,14130,545],{"class":544},[54,14132,103],{"class":88},[54,14134,830],{"class":544},[54,14136,10658],{"class":548},[54,14138,545],{"class":544},[54,14140,103],{"class":88},[54,14142,9332],{"class":8940},[54,14144,82],{"class":81},[54,14146,545],{"class":544},[54,14148,9339],{"class":548},[54,14150,545],{"class":544},[54,14152,876],{"class":88},[54,14154,7631],{"class":60},[54,14156,8296],{"class":8295},[54,14158,7572],{"class":88},[54,14160,14161,14163,14165,14167,14169,14171,14173,14175],{"class":56,"line":112},[54,14162,13652],{"class":64},[54,14164,82],{"class":81},[54,14166,9558],{"class":64},[54,14168,89],{"class":88},[54,14170,13661],{"class":92},[54,14172,96],{"class":88},[54,14174,8412],{"class":8295},[54,14176,109],{"class":88},[54,14178,14179],{"class":56,"line":887},[54,14180,72],{"emptyLinePlaceholder":71},[54,14182,14183,14185,14187,14189],{"class":56,"line":892},[54,14184,116],{"class":115},[54,14186,96],{"class":88},[54,14188,8313],{"class":92},[54,14190,109],{"class":88},[54,14192,14193,14195,14197,14199,14201,14203],{"class":56,"line":913},[54,14194,116],{"class":115},[54,14196,96],{"class":88},[54,14198,2208],{"class":429},[54,14200,96],{"class":88},[54,14202,8313],{"class":92},[54,14204,382],{"class":88},[54,14206,14207,14209,14211,14213,14215,14217,14219,14221],{"class":56,"line":2121},[54,14208,116],{"class":115},[54,14210,96],{"class":88},[54,14212,8313],{"class":92},[54,14214,459],{"class":88},[54,14216,545],{"class":544},[54,14218,8231],{"class":548},[54,14220,545],{"class":544},[54,14222,937],{"class":88},[54,14224,14225,14227,14229,14231,14233,14235,14237,14239,14241,14243],{"class":56,"line":2136},[54,14226,116],{"class":115},[54,14228,96],{"class":88},[54,14230,2208],{"class":429},[54,14232,96],{"class":88},[54,14234,8313],{"class":92},[54,14236,459],{"class":88},[54,14238,545],{"class":544},[54,14240,8860],{"class":548},[54,14242,545],{"class":544},[54,14244,14245],{"class":88},"]))\n",[14,14247,14248],{},"Expected output:",[45,14250,14252],{"className":47,"code":14251,"language":49,"meta":50,"style":50},"{'name': 'Maya', 'age': 25, 'is_admin': False, 'skills': ['Python', 'SQL'], 'manager': None}\n\u003Cclass 'dict'>\nMaya\n\u003Cclass 'list'>\n",[17,14253,14254,14338,14352,14357],{"__ignoreMap":50},[54,14255,14256,14258,14260,14262,14264,14266,14268,14270,14272,14274,14276,14278,14280,14282,14284,14286,14288,14290,14292,14294,14297,14299,14301,14303,14305,14307,14309,14311,14313,14315,14317,14319,14321,14323,14325,14327,14329,14331,14333,14336],{"class":56,"line":57},[54,14257,8711],{"class":88},[54,14259,2240],{"class":544},[54,14261,8231],{"class":548},[54,14263,2240],{"class":544},[54,14265,147],{"class":88},[54,14267,2234],{"class":544},[54,14269,14020],{"class":548},[54,14271,2240],{"class":544},[54,14273,103],{"class":88},[54,14275,2234],{"class":544},[54,14277,8249],{"class":548},[54,14279,2240],{"class":544},[54,14281,147],{"class":88},[54,14283,11369],{"class":99},[54,14285,103],{"class":88},[54,14287,2234],{"class":544},[54,14289,8601],{"class":548},[54,14291,2240],{"class":544},[54,14293,147],{"class":88},[54,14295,14296],{"class":8608}," False",[54,14298,103],{"class":88},[54,14300,2234],{"class":544},[54,14302,8860],{"class":548},[54,14304,2240],{"class":544},[54,14306,147],{"class":88},[54,14308,818],{"class":88},[54,14310,2240],{"class":544},[54,14312,8871],{"class":548},[54,14314,2240],{"class":544},[54,14316,103],{"class":88},[54,14318,2234],{"class":544},[54,14320,8880],{"class":548},[54,14322,2240],{"class":544},[54,14324,11951],{"class":88},[54,14326,2234],{"class":544},[54,14328,14087],{"class":548},[54,14330,2240],{"class":544},[54,14332,147],{"class":88},[54,14334,14335],{"class":8608}," None",[54,14337,8258],{"class":88},[54,14339,14340,14342,14344,14346,14348,14350],{"class":56,"line":68},[54,14341,2227],{"class":81},[54,14343,2231],{"class":2230},[54,14345,2234],{"class":544},[54,14347,9644],{"class":548},[54,14349,2240],{"class":544},[54,14351,2243],{"class":81},[54,14353,14354],{"class":56,"line":75},[54,14355,14356],{"class":64},"Maya\n",[54,14358,14359,14361,14363,14365,14367,14369],{"class":56,"line":112},[54,14360,2227],{"class":81},[54,14362,2231],{"class":2230},[54,14364,2234],{"class":544},[54,14366,430],{"class":548},[54,14368,2240],{"class":544},[54,14370,2243],{"class":81},[40,14372,14374],{"id":14373},"step-by-step-example","Step-by-step example",[14,14376,14377],{},"Let’s go through a full example.",[602,14379,14381],{"id":14380},"_1-create-a-json-file","1. Create a JSON file",[14,14383,14384,14385,147],{},"Create a file named ",[17,14386,8277],{},[45,14388,14390],{"className":8703,"code":14389,"language":8385,"meta":50,"style":50},"{\n  \"title\": \"Python Basics\",\n  \"pages\": 120,\n  \"available\": true\n}\n",[17,14391,14392,14396,14416,14432,14446],{"__ignoreMap":50},[54,14393,14394],{"class":56,"line":57},[54,14395,8960],{"class":88},[54,14397,14398,14400,14403,14405,14407,14409,14412,14414],{"class":56,"line":68},[54,14399,8965],{"class":8608},[54,14401,14402],{"class":8716},"title",[54,14404,545],{"class":8608},[54,14406,147],{"class":88},[54,14408,830],{"class":544},[54,14410,14411],{"class":548},"Python Basics",[54,14413,545],{"class":544},[54,14415,8580],{"class":88},[54,14417,14418,14420,14423,14425,14427,14430],{"class":56,"line":75},[54,14419,8965],{"class":8608},[54,14421,14422],{"class":8716},"pages",[54,14424,545],{"class":8608},[54,14426,147],{"class":88},[54,14428,14429],{"class":99}," 120",[54,14431,8580],{"class":88},[54,14433,14434,14436,14439,14441,14443],{"class":56,"line":112},[54,14435,8965],{"class":8608},[54,14437,14438],{"class":8716},"available",[54,14440,545],{"class":8608},[54,14442,147],{"class":88},[54,14444,14445],{"class":8608}," true\n",[54,14447,14448],{"class":56,"line":887},[54,14449,8258],{"class":88},[602,14451,14453],{"id":14452},"_2-open-the-file-in-read-mode","2. Open the file in read mode",[14,14455,126,14456,5484,14458,14461],{},[17,14457,8524],{},[17,14459,14460],{},"\"r\""," mode:",[45,14463,14465],{"className":47,"code":14464,"language":49,"meta":50,"style":50},"file = open(\"data.json\", \"r\", encoding=\"utf-8\")\n",[17,14466,14467],{"__ignoreMap":50},[54,14468,14469,14471,14473,14475,14477,14479,14481,14483,14485,14487,14489,14491,14493,14495,14497,14499,14501,14503],{"class":56,"line":57},[54,14470,8412],{"class":8295},[54,14472,10849],{"class":81},[54,14474,8270],{"class":115},[54,14476,96],{"class":88},[54,14478,545],{"class":544},[54,14480,8277],{"class":548},[54,14482,545],{"class":544},[54,14484,103],{"class":88},[54,14486,830],{"class":544},[54,14488,10658],{"class":548},[54,14490,545],{"class":544},[54,14492,103],{"class":88},[54,14494,9332],{"class":8940},[54,14496,82],{"class":81},[54,14498,545],{"class":544},[54,14500,9339],{"class":548},[54,14502,545],{"class":544},[54,14504,109],{"class":88},[602,14506,14508,14509],{"id":14507},"_3-call-jsonloadfile","3. Call ",[17,14510,14511],{},"json.load(file)",[14,14513,14514],{},"This reads and parses the JSON:",[45,14516,14518],{"className":47,"code":14517,"language":49,"meta":50,"style":50},"data = json.load(file)\n",[17,14519,14520],{"__ignoreMap":50},[54,14521,14522,14524,14526,14528,14530,14532,14534,14536],{"class":56,"line":57},[54,14523,8221],{"class":64},[54,14525,82],{"class":81},[54,14527,9558],{"class":64},[54,14529,89],{"class":88},[54,14531,13661],{"class":92},[54,14533,96],{"class":88},[54,14535,8412],{"class":8295},[54,14537,109],{"class":88},[602,14539,14541],{"id":14540},"_4-store-the-result-in-a-variable","4. Store the result in a variable",[14,14543,14544,14545,14547],{},"Now ",[17,14546,8313],{}," contains Python data you can use in your program.",[602,14549,14551],{"id":14550},"_5-access-values-like-normal-python-data","5. Access values like normal Python data",[14,14553,14554],{},"Full example:",[45,14556,14558],{"className":47,"code":14557,"language":49,"meta":50,"style":50},"import json\n\nwith open(\"data.json\", \"r\", encoding=\"utf-8\") as file:\n    data = json.load(file)\n\nprint(data)\nprint(data[\"title\"])\nprint(data[\"pages\"])\nprint(data[\"available\"])\n",[17,14559,14560,14566,14570,14612,14630,14634,14644,14662,14680],{"__ignoreMap":50},[54,14561,14562,14564],{"class":56,"line":57},[54,14563,61],{"class":60},[54,14565,8212],{"class":64},[54,14567,14568],{"class":56,"line":68},[54,14569,72],{"emptyLinePlaceholder":71},[54,14571,14572,14574,14576,14578,14580,14582,14584,14586,14588,14590,14592,14594,14596,14598,14600,14602,14604,14606,14608,14610],{"class":56,"line":75},[54,14573,8267],{"class":60},[54,14575,8270],{"class":115},[54,14577,96],{"class":88},[54,14579,545],{"class":544},[54,14581,8277],{"class":548},[54,14583,545],{"class":544},[54,14585,103],{"class":88},[54,14587,830],{"class":544},[54,14589,10658],{"class":548},[54,14591,545],{"class":544},[54,14593,103],{"class":88},[54,14595,9332],{"class":8940},[54,14597,82],{"class":81},[54,14599,545],{"class":544},[54,14601,9339],{"class":548},[54,14603,545],{"class":544},[54,14605,876],{"class":88},[54,14607,7631],{"class":60},[54,14609,8296],{"class":8295},[54,14611,7572],{"class":88},[54,14613,14614,14616,14618,14620,14622,14624,14626,14628],{"class":56,"line":112},[54,14615,13652],{"class":64},[54,14617,82],{"class":81},[54,14619,9558],{"class":64},[54,14621,89],{"class":88},[54,14623,13661],{"class":92},[54,14625,96],{"class":88},[54,14627,8412],{"class":8295},[54,14629,109],{"class":88},[54,14631,14632],{"class":56,"line":887},[54,14633,72],{"emptyLinePlaceholder":71},[54,14635,14636,14638,14640,14642],{"class":56,"line":892},[54,14637,116],{"class":115},[54,14639,96],{"class":88},[54,14641,8313],{"class":92},[54,14643,109],{"class":88},[54,14645,14646,14648,14650,14652,14654,14656,14658,14660],{"class":56,"line":913},[54,14647,116],{"class":115},[54,14649,96],{"class":88},[54,14651,8313],{"class":92},[54,14653,459],{"class":88},[54,14655,545],{"class":544},[54,14657,14402],{"class":548},[54,14659,545],{"class":544},[54,14661,937],{"class":88},[54,14663,14664,14666,14668,14670,14672,14674,14676,14678],{"class":56,"line":2121},[54,14665,116],{"class":115},[54,14667,96],{"class":88},[54,14669,8313],{"class":92},[54,14671,459],{"class":88},[54,14673,545],{"class":544},[54,14675,14422],{"class":548},[54,14677,545],{"class":544},[54,14679,937],{"class":88},[54,14681,14682,14684,14686,14688,14690,14692,14694,14696],{"class":56,"line":2136},[54,14683,116],{"class":115},[54,14685,96],{"class":88},[54,14687,8313],{"class":92},[54,14689,459],{"class":88},[54,14691,545],{"class":544},[54,14693,14438],{"class":548},[54,14695,545],{"class":544},[54,14697,937],{"class":88},[14,14699,14248],{},[45,14701,14703],{"className":47,"code":14702,"language":49,"meta":50,"style":50},"{'title': 'Python Basics', 'pages': 120, 'available': True}\nPython Basics\n120\nTrue\n",[17,14704,14705,14749,14754,14759],{"__ignoreMap":50},[54,14706,14707,14709,14711,14713,14715,14717,14719,14721,14723,14725,14727,14729,14731,14733,14735,14737,14739,14741,14743,14745,14747],{"class":56,"line":57},[54,14708,8711],{"class":88},[54,14710,2240],{"class":544},[54,14712,14402],{"class":548},[54,14714,2240],{"class":544},[54,14716,147],{"class":88},[54,14718,2234],{"class":544},[54,14720,14411],{"class":548},[54,14722,2240],{"class":544},[54,14724,103],{"class":88},[54,14726,2234],{"class":544},[54,14728,14422],{"class":548},[54,14730,2240],{"class":544},[54,14732,147],{"class":88},[54,14734,14429],{"class":99},[54,14736,103],{"class":88},[54,14738,2234],{"class":544},[54,14740,14438],{"class":548},[54,14742,2240],{"class":544},[54,14744,147],{"class":88},[54,14746,9746],{"class":8608},[54,14748,8258],{"class":88},[54,14750,14751],{"class":56,"line":68},[54,14752,14753],{"class":64},"Python Basics\n",[54,14755,14756],{"class":56,"line":75},[54,14757,14758],{"class":99},"120\n",[54,14760,14761],{"class":56,"line":112},[54,14762,14763],{"class":8608},"True\n",[14,14765,14766],{},"Key line:",[45,14768,14769],{"className":47,"code":14517,"language":49,"meta":50,"style":50},[17,14770,14771],{"__ignoreMap":50},[54,14772,14773,14775,14777,14779,14781,14783,14785,14787],{"class":56,"line":57},[54,14774,8221],{"class":64},[54,14776,82],{"class":81},[54,14778,9558],{"class":64},[54,14780,89],{"class":88},[54,14782,13661],{"class":92},[54,14784,96],{"class":88},[54,14786,8412],{"class":8295},[54,14788,109],{"class":88},[14,14790,14791],{},"This is the line that turns JSON text from the file into a Python dictionary.",[14,14793,14794,14795,89],{},"If you want a more task-focused guide, see ",[192,14796,14797],{"href":8140},"how to parse JSON in Python",[40,14799,14801,7817,14803],{"id":14800},"jsonload-vs-jsonloads",[17,14802,13580],{},[17,14804,13451],{},[14,14806,14807],{},"These two functions are similar, but they do not take the same kind of input.",[602,14809,126,14811,14813],{"id":14810},"use-jsonload-for-files",[17,14812,13580],{}," for files",[14,14815,14816,14818],{},[17,14817,13580],{}," reads JSON from a file object:",[45,14820,14821],{"className":47,"code":13811,"language":49,"meta":50,"style":50},[17,14822,14823,14829,14833,14875],{"__ignoreMap":50},[54,14824,14825,14827],{"class":56,"line":57},[54,14826,61],{"class":60},[54,14828,8212],{"class":64},[54,14830,14831],{"class":56,"line":68},[54,14832,72],{"emptyLinePlaceholder":71},[54,14834,14835,14837,14839,14841,14843,14845,14847,14849,14851,14853,14855,14857,14859,14861,14863,14865,14867,14869,14871,14873],{"class":56,"line":75},[54,14836,8267],{"class":60},[54,14838,8270],{"class":115},[54,14840,96],{"class":88},[54,14842,545],{"class":544},[54,14844,8277],{"class":548},[54,14846,545],{"class":544},[54,14848,103],{"class":88},[54,14850,830],{"class":544},[54,14852,10658],{"class":548},[54,14854,545],{"class":544},[54,14856,103],{"class":88},[54,14858,9332],{"class":8940},[54,14860,82],{"class":81},[54,14862,545],{"class":544},[54,14864,9339],{"class":548},[54,14866,545],{"class":544},[54,14868,876],{"class":88},[54,14870,7631],{"class":60},[54,14872,8296],{"class":8295},[54,14874,7572],{"class":88},[54,14876,14877,14879,14881,14883,14885,14887,14889,14891],{"class":56,"line":112},[54,14878,13652],{"class":64},[54,14880,82],{"class":81},[54,14882,9558],{"class":64},[54,14884,89],{"class":88},[54,14886,13661],{"class":92},[54,14888,96],{"class":88},[54,14890,8412],{"class":8295},[54,14892,109],{"class":88},[602,14894,126,14896,14898],{"id":14895},"use-jsonloads-for-strings",[17,14897,13451],{}," for strings",[14,14900,14901,14903],{},[17,14902,13451],{}," reads JSON from a string:",[45,14905,14907],{"className":47,"code":14906,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"name\": \"Liam\", \"age\": 30}'\ndata = json.loads(text)\n\nprint(data)\n",[17,14908,14909,14915,14919,14932,14951,14955],{"__ignoreMap":50},[54,14910,14911,14913],{"class":56,"line":57},[54,14912,61],{"class":60},[54,14914,8212],{"class":64},[54,14916,14917],{"class":56,"line":68},[54,14918,72],{"emptyLinePlaceholder":71},[54,14920,14921,14923,14925,14927,14930],{"class":56,"line":75},[54,14922,5134],{"class":64},[54,14924,82],{"class":81},[54,14926,2234],{"class":544},[54,14928,14929],{"class":548},"{\"name\": \"Liam\", \"age\": 30}",[54,14931,5297],{"class":544},[54,14933,14934,14936,14938,14940,14942,14945,14947,14949],{"class":56,"line":112},[54,14935,8221],{"class":64},[54,14937,82],{"class":81},[54,14939,9558],{"class":64},[54,14941,89],{"class":88},[54,14943,14944],{"class":92},"loads",[54,14946,96],{"class":88},[54,14948,5171],{"class":92},[54,14950,109],{"class":88},[54,14952,14953],{"class":56,"line":887},[54,14954,72],{"emptyLinePlaceholder":71},[54,14956,14957,14959,14961,14963],{"class":56,"line":892},[54,14958,116],{"class":115},[54,14960,96],{"class":88},[54,14962,8313],{"class":92},[54,14964,109],{"class":88},[14,14966,14248],{},[45,14968,14970],{"className":47,"code":14969,"language":49,"meta":50,"style":50},"{'name': 'Liam', 'age': 30}\n",[17,14971,14972],{"__ignoreMap":50},[54,14973,14974,14976,14978,14980,14982,14984,14986,14989,14991,14993,14995,14997,14999,15001,15003],{"class":56,"line":57},[54,14975,8711],{"class":88},[54,14977,2240],{"class":544},[54,14979,8231],{"class":548},[54,14981,2240],{"class":544},[54,14983,147],{"class":88},[54,14985,2234],{"class":544},[54,14987,14988],{"class":548},"Liam",[54,14990,2240],{"class":544},[54,14992,103],{"class":88},[54,14994,2234],{"class":544},[54,14996,8249],{"class":548},[54,14998,2240],{"class":544},[54,15000,147],{"class":88},[54,15002,3981],{"class":99},[54,15004,8258],{"class":88},[14,15006,15007],{},"In short:",[149,15009,15010,15014],{},[152,15011,126,15012,14813],{},[17,15013,13661],{},[152,15015,126,15016,15018],{},[17,15017,14944],{}," for JSON text already stored in a variable",[14,15020,15021,15022,89],{},"For more detail, see ",[192,15023,15024,12898],{"href":13465},[17,15025,13451],{},[40,15027,15029],{"id":15028},"common-errors-beginners-make","Common errors beginners make",[14,15031,15032,15033,89],{},"Here are some common problems when using ",[17,15034,13580],{},[602,15036,15038],{"id":15037},"passing-a-file-path-string-instead-of-an-open-file","Passing a file path string instead of an open file",[14,15040,10192],{},[45,15042,15043],{"className":47,"code":13889,"language":49,"meta":50,"style":50},[17,15044,15045,15051,15055],{"__ignoreMap":50},[54,15046,15047,15049],{"class":56,"line":57},[54,15048,61],{"class":60},[54,15050,8212],{"class":64},[54,15052,15053],{"class":56,"line":68},[54,15054,72],{"emptyLinePlaceholder":71},[54,15056,15057,15059,15061,15063,15065,15067,15069,15071,15073,15075],{"class":56,"line":75},[54,15058,8221],{"class":64},[54,15060,82],{"class":81},[54,15062,9558],{"class":64},[54,15064,89],{"class":88},[54,15066,13661],{"class":92},[54,15068,96],{"class":88},[54,15070,545],{"class":544},[54,15072,8277],{"class":548},[54,15074,545],{"class":544},[54,15076,109],{"class":88},[14,15078,15079],{},"Why it fails:",[149,15081,15082,15087],{},[152,15083,15084,15086],{},[17,15085,13580],{}," expects a file object",[152,15088,15089,15091],{},[17,15090,13929],{}," is only a string",[14,15093,1031],{},[45,15095,15096],{"className":47,"code":13811,"language":49,"meta":50,"style":50},[17,15097,15098,15104,15108,15150],{"__ignoreMap":50},[54,15099,15100,15102],{"class":56,"line":57},[54,15101,61],{"class":60},[54,15103,8212],{"class":64},[54,15105,15106],{"class":56,"line":68},[54,15107,72],{"emptyLinePlaceholder":71},[54,15109,15110,15112,15114,15116,15118,15120,15122,15124,15126,15128,15130,15132,15134,15136,15138,15140,15142,15144,15146,15148],{"class":56,"line":75},[54,15111,8267],{"class":60},[54,15113,8270],{"class":115},[54,15115,96],{"class":88},[54,15117,545],{"class":544},[54,15119,8277],{"class":548},[54,15121,545],{"class":544},[54,15123,103],{"class":88},[54,15125,830],{"class":544},[54,15127,10658],{"class":548},[54,15129,545],{"class":544},[54,15131,103],{"class":88},[54,15133,9332],{"class":8940},[54,15135,82],{"class":81},[54,15137,545],{"class":544},[54,15139,9339],{"class":548},[54,15141,545],{"class":544},[54,15143,876],{"class":88},[54,15145,7631],{"class":60},[54,15147,8296],{"class":8295},[54,15149,7572],{"class":88},[54,15151,15152,15154,15156,15158,15160,15162,15164,15166],{"class":56,"line":112},[54,15153,13652],{"class":64},[54,15155,82],{"class":81},[54,15157,9558],{"class":64},[54,15159,89],{"class":88},[54,15161,13661],{"class":92},[54,15163,96],{"class":88},[54,15165,8412],{"class":8295},[54,15167,109],{"class":88},[602,15169,15171],{"id":15170},"trying-to-load-invalid-json","Trying to load invalid JSON",[14,15173,15174],{},"JSON has strict syntax rules.",[14,15176,15177],{},"This is invalid JSON:",[45,15179,15181],{"className":8703,"code":15180,"language":8385,"meta":50,"style":50},"{\n  'name': 'Ava'\n}\n",[17,15182,15183,15187,15197],{"__ignoreMap":50},[54,15184,15185],{"class":56,"line":57},[54,15186,8960],{"class":88},[54,15188,15189,15192,15194],{"class":56,"line":68},[54,15190,15191],{"class":3275},"  'name'",[54,15193,147],{"class":88},[54,15195,15196],{"class":3275}," 'Ava'\n",[54,15198,15199],{"class":56,"line":75},[54,15200,8258],{"class":88},[14,15202,15079],{},[149,15204,15205],{},[152,15206,15207],{},"JSON uses double quotes, not single quotes",[14,15209,15210],{},"Correct JSON:",[45,15212,15214],{"className":8703,"code":15213,"language":8385,"meta":50,"style":50},"{\n  \"name\": \"Ava\"\n}\n",[17,15215,15216,15220,15237],{"__ignoreMap":50},[54,15217,15218],{"class":56,"line":57},[54,15219,8960],{"class":88},[54,15221,15222,15224,15226,15228,15230,15232,15235],{"class":56,"line":68},[54,15223,8965],{"class":8608},[54,15225,8231],{"class":8716},[54,15227,545],{"class":8608},[54,15229,147],{"class":88},[54,15231,830],{"class":544},[54,15233,15234],{"class":548},"Ava",[54,15236,5239],{"class":544},[54,15238,15239],{"class":56,"line":75},[54,15240,8258],{"class":88},[602,15242,15244,15245,320,15247,3587,15249],{"id":15243},"forgetting-json-uses-true-false-and-null","Forgetting JSON uses ",[17,15246,11767],{},[17,15248,8763],{},[17,15250,8772],{},[14,15252,15253],{},"Inside a JSON file, use:",[149,15255,15256,15260,15264],{},[152,15257,15258],{},[17,15259,11767],{},[152,15261,15262],{},[17,15263,8763],{},[152,15265,15266],{},[17,15267,8772],{},[14,15269,15270],{},"Not:",[149,15272,15273,15277,15281],{},[152,15274,15275],{},[17,15276,9175],{},[152,15278,15279],{},[17,15280,8767],{},[152,15282,15283],{},[17,15284,8775],{},[14,15286,15287],{},"Example valid JSON:",[45,15289,15291],{"className":8703,"code":15290,"language":8385,"meta":50,"style":50},"{\n  \"active\": true,\n  \"notes\": null\n}\n",[17,15292,15293,15297,15311,15324],{"__ignoreMap":50},[54,15294,15295],{"class":56,"line":57},[54,15296,8960],{"class":88},[54,15298,15299,15301,15303,15305,15307,15309],{"class":56,"line":68},[54,15300,8965],{"class":8608},[54,15302,9739],{"class":8716},[54,15304,545],{"class":8608},[54,15306,147],{"class":88},[54,15308,11962],{"class":8608},[54,15310,8580],{"class":88},[54,15312,15313,15315,15318,15320,15322],{"class":56,"line":75},[54,15314,8965],{"class":8608},[54,15316,15317],{"class":8716},"notes",[54,15319,545],{"class":8608},[54,15321,147],{"class":88},[54,15323,14094],{"class":8608},[54,15325,15326],{"class":56,"line":112},[54,15327,8258],{"class":88},[602,15329,15331],{"id":15330},"opening-the-wrong-file-or-wrong-path","Opening the wrong file or wrong path",[14,15333,15334,15335,89],{},"If Python cannot find the file, you may get a ",[17,15336,15337],{},"FileNotFoundError",[14,15339,253],{},[45,15341,15343],{"className":47,"code":15342,"language":49,"meta":50,"style":50},"import json\n\nwith open(\"missing.json\", \"r\", encoding=\"utf-8\") as file:\n    data = json.load(file)\n",[17,15344,15345,15351,15355,15398],{"__ignoreMap":50},[54,15346,15347,15349],{"class":56,"line":57},[54,15348,61],{"class":60},[54,15350,8212],{"class":64},[54,15352,15353],{"class":56,"line":68},[54,15354,72],{"emptyLinePlaceholder":71},[54,15356,15357,15359,15361,15363,15365,15368,15370,15372,15374,15376,15378,15380,15382,15384,15386,15388,15390,15392,15394,15396],{"class":56,"line":75},[54,15358,8267],{"class":60},[54,15360,8270],{"class":115},[54,15362,96],{"class":88},[54,15364,545],{"class":544},[54,15366,15367],{"class":548},"missing.json",[54,15369,545],{"class":544},[54,15371,103],{"class":88},[54,15373,830],{"class":544},[54,15375,10658],{"class":548},[54,15377,545],{"class":544},[54,15379,103],{"class":88},[54,15381,9332],{"class":8940},[54,15383,82],{"class":81},[54,15385,545],{"class":544},[54,15387,9339],{"class":548},[54,15389,545],{"class":544},[54,15391,876],{"class":88},[54,15393,7631],{"class":60},[54,15395,8296],{"class":8295},[54,15397,7572],{"class":88},[54,15399,15400,15402,15404,15406,15408,15410,15412,15414],{"class":56,"line":112},[54,15401,13652],{"class":64},[54,15403,82],{"class":81},[54,15405,9558],{"class":64},[54,15407,89],{"class":88},[54,15409,13661],{"class":92},[54,15411,96],{"class":88},[54,15413,8412],{"class":8295},[54,15415,109],{"class":88},[14,15417,15418,15419,89],{},"If that happens, see ",[192,15420,15422],{"href":15421},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes\u002F","FileNotFoundError in Python: causes and fixes",[40,15424,15426,15427],{"id":15425},"when-to-use-jsonload","When to use ",[17,15428,13580],{},[14,15430,15431,15433],{},[17,15432,13580],{}," is useful when you need to read structured data from a file.",[14,15435,15436],{},"Common use cases:",[149,15438,15439,15442,15445,15448],{},[152,15440,15441],{},"Reading configuration files",[152,15443,15444],{},"Loading saved app data",[152,15446,15447],{},"Working with exported API data stored in a file",[152,15449,15450],{},"Reading structured data for scripts",[14,15452,15453,15454,89],{},"If you want to save Python data back to a file, the matching function is ",[192,15455,15456],{"href":11295},[17,15457,8190],{},[40,15459,15461],{"id":15460},"common-causes-of-errors","Common causes of errors",[14,15463,15464,15465,15467],{},"When ",[17,15466,13580],{}," fails, these are common causes:",[149,15469,15470,15475,15481,15486],{},[152,15471,15472,15474],{},[17,15473,10040],{}," because a string path was passed instead of a file object",[152,15476,15477,15480],{},[17,15478,15479],{},"JSONDecodeError"," because the JSON file has invalid syntax",[152,15482,15483,15485],{},[17,15484,15337],{}," because the file path is wrong",[152,15487,15488,15491],{},[17,15489,15490],{},"UnicodeDecodeError"," because the file encoding does not match the content",[14,15493,15494],{},"Helpful debugging steps:",[45,15496,15500],{"className":15497,"code":15498,"language":15499,"meta":50,"style":50},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python your_script.py\n","bash",[17,15501,15502],{"__ignoreMap":50},[54,15503,15504,15506],{"class":56,"line":57},[54,15505,49],{"class":10811},[54,15507,15508],{"class":548}," your_script.py\n",[45,15510,15512],{"className":47,"code":15511,"language":49,"meta":50,"style":50},"print(type(file))\nprint(data)\nprint(type(data))\n",[17,15513,15514,15528,15538],{"__ignoreMap":50},[54,15515,15516,15518,15520,15522,15524,15526],{"class":56,"line":57},[54,15517,116],{"class":115},[54,15519,96],{"class":88},[54,15521,2208],{"class":429},[54,15523,96],{"class":88},[54,15525,8412],{"class":8295},[54,15527,382],{"class":88},[54,15529,15530,15532,15534,15536],{"class":56,"line":68},[54,15531,116],{"class":115},[54,15533,96],{"class":88},[54,15535,8313],{"class":92},[54,15537,109],{"class":88},[54,15539,15540,15542,15544,15546,15548,15550],{"class":56,"line":75},[54,15541,116],{"class":115},[54,15543,96],{"class":88},[54,15545,2208],{"class":429},[54,15547,96],{"class":88},[54,15549,8313],{"class":92},[54,15551,382],{"class":88},[14,15553,15554],{},"These checks can help you confirm:",[149,15556,15557,15563,15566],{},[152,15558,15559,15560,15562],{},"whether ",[17,15561,8412],{}," is really an open file object",[152,15564,15565],{},"what data was loaded",[152,15567,15568],{},"what Python type was returned",[40,15570,1419],{"id":1418},[602,15572,8075,15574,134,15576,3409],{"id":15573},"what-is-the-difference-between-jsonload-and-jsonloads",[17,15575,13580],{},[17,15577,13451],{},[14,15579,15580,15582,15583,15585],{},[17,15581,13580],{}," reads JSON from an open file. ",[17,15584,13451],{}," reads JSON from a string.",[602,15587,8346,15589,15591],{"id":15588},"does-jsonload-always-return-a-dictionary",[17,15590,13580],{}," always return a dictionary?",[14,15593,15594,15595,320,15597,320,15599,320,15601,320,15603,320,15605,324,15607,89],{},"No. It returns the Python type that matches the JSON content, such as ",[17,15596,9644],{},[17,15598,430],{},[17,15600,3173],{},[17,15602,9657],{},[17,15604,9662],{},[17,15606,9667],{},[17,15608,8775],{},[602,15610,15612,15613,3409],{"id":15611},"can-i-pass-a-file-name-directly-to-jsonload","Can I pass a file name directly to ",[17,15614,13580],{},[14,15616,15617,15618,89],{},"No. Open the file first, then pass the file object to ",[17,15619,13580],{},[602,15621,3403,15623,15625],{"id":15622},"why-does-jsonload-fail-on-my-file",[17,15624,13580],{}," fail on my file?",[14,15627,15628],{},"The file may not exist, the JSON may be invalid, or the encoding may be wrong.",[40,15630,1510],{"id":1509},[149,15632,15633,15639,15645,15651,15657,15661,15666,15671],{},[152,15634,15635],{},[192,15636,13760,15637,13763],{"href":13759},[17,15638,8385],{},[152,15640,15641],{},[192,15642,15643,12898],{"href":13465},[17,15644,13451],{},[152,15646,15647],{},[192,15648,15649,12898],{"href":11295},[17,15650,8190],{},[152,15652,15653],{},[192,15654,15655,12898],{"href":8368},[17,15656,8371],{},[152,15658,15659],{},[192,15660,8141],{"href":8140},[152,15662,15663],{},[192,15664,15665],{"href":13936},"How to read a file in Python",[152,15667,15668],{},[192,15669,11234],{"href":15670},"\u002Fhow-to\u002Fhow-to-convert-dictionary-to-json-in-python\u002F",[152,15672,15673],{},[192,15674,15422],{"href":15421},[1545,15676,15677],{},"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 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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sseR_, html code.shiki .sseR_{--shiki-light:#9C3EDA;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":50,"searchDepth":68,"depth":68,"links":15679},[15680,15681,15683,15684,15686,15694,15701,15708,15710,15711,15721],{"id":42,"depth":68,"text":43},{"id":13717,"depth":68,"text":15682},"What json.load() does",{"id":198,"depth":68,"text":199},{"id":13940,"depth":68,"text":15685},"What json.load() returns",{"id":14373,"depth":68,"text":14374,"children":15687},[15688,15689,15690,15692,15693],{"id":14380,"depth":75,"text":14381},{"id":14452,"depth":75,"text":14453},{"id":14507,"depth":75,"text":15691},"3. Call json.load(file)",{"id":14540,"depth":75,"text":14541},{"id":14550,"depth":75,"text":14551},{"id":14800,"depth":68,"text":15695,"children":15696},"json.load() vs json.loads()",[15697,15699],{"id":14810,"depth":75,"text":15698},"Use json.load() for files",{"id":14895,"depth":75,"text":15700},"Use json.loads() for strings",{"id":15028,"depth":68,"text":15029,"children":15702},[15703,15704,15705,15707],{"id":15037,"depth":75,"text":15038},{"id":15170,"depth":75,"text":15171},{"id":15243,"depth":75,"text":15706},"Forgetting JSON uses true, false, and null",{"id":15330,"depth":75,"text":15331},{"id":15425,"depth":68,"text":15709},"When to use json.load()",{"id":15460,"depth":68,"text":15461},{"id":1418,"depth":68,"text":1419,"children":15712},[15713,15715,15717,15719],{"id":15573,"depth":75,"text":15714},"What is the difference between json.load() and json.loads()?",{"id":15588,"depth":75,"text":15716},"Does json.load() always return a dictionary?",{"id":15611,"depth":75,"text":15718},"Can I pass a file name directly to json.load()?",{"id":15622,"depth":75,"text":15720},"Why does json.load() fail on my file?",{"id":1509,"depth":68,"text":1510},"Master json.load function explained in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fjson.load-function-explained",{"title":13572,"description":15722},"standard-library\u002Fjson.load-function-explained","7v10uaUtJy_WesuopHso_uHoS4NkqWLesUvcY1eVuOk",{"id":15729,"title":15730,"body":15731,"description":18248,"extension":1577,"meta":18249,"navigation":71,"path":13465,"seo":18250,"stem":18251,"__hash__":18252},"content\u002Fstandard-library\u002Fjson.loads-function-explained.md","json.loads() Function Explained",{"type":7,"value":15732,"toc":18203},[15733,15738,15743,15746,15760,15766,15768,15860,15862,15931,15950,15955,15959,15992,15995,16009,16012,16049,16054,16062,16065,16079,16085,16093,16095,16098,16208,16210,16282,16285,16294,16297,16320,16324,16329,16397,16399,16614,16616,16696,16703,16706,16711,16718,16774,16779,16786,16863,16866,16878,16885,16889,16893,16897,16902,16904,16950,16953,16957,16964,16972,16975,16989,16992,17005,17011,17015,17050,17052,17062,17069,17074,17076,17133,17135,17182,17192,17196,17199,17241,17246,17250,17257,17383,17386,17389,17406,17409,17452,17455,17569,17573,17578,17582,17585,17675,17679,17747,17753,17763,17856,17863,17867,17956,17958,18011,18013,18016,18042,18045,18087,18089,18094,18101,18108,18116,18122,18125,18131,18134,18141,18147,18149,18180,18183,18200],[10,15734,15736,11309],{"id":15735},"jsonloads-function-explained",[17,15737,13451],{},[14,15739,15740,15742],{},[17,15741,13451],{}," converts JSON text into normal Python data.",[14,15744,15745],{},"Use it when your JSON is already stored in a string, such as:",[149,15747,15748,15751,15754],{},[152,15749,15750],{},"API response text",[152,15752,15753],{},"JSON copied into a variable",[152,15755,15756,15757],{},"JSON read with ",[17,15758,15759],{},"file.read()",[14,15761,15762,15763,15765],{},"It is part of Python’s built-in ",[17,15764,8385],{}," module.",[40,15767,3596],{"id":3595},[45,15769,15771],{"className":47,"code":15770,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"name\": \"Ana\", \"age\": 20, \"is_student\": true}'\ndata = json.loads(text)\n\nprint(data)\nprint(type(data))\nprint(data[\"name\"])\n",[17,15772,15773,15779,15783,15796,15814,15818,15828,15842],{"__ignoreMap":50},[54,15774,15775,15777],{"class":56,"line":57},[54,15776,61],{"class":60},[54,15778,8212],{"class":64},[54,15780,15781],{"class":56,"line":68},[54,15782,72],{"emptyLinePlaceholder":71},[54,15784,15785,15787,15789,15791,15794],{"class":56,"line":75},[54,15786,5134],{"class":64},[54,15788,82],{"class":81},[54,15790,2234],{"class":544},[54,15792,15793],{"class":548},"{\"name\": \"Ana\", \"age\": 20, \"is_student\": true}",[54,15795,5297],{"class":544},[54,15797,15798,15800,15802,15804,15806,15808,15810,15812],{"class":56,"line":112},[54,15799,8221],{"class":64},[54,15801,82],{"class":81},[54,15803,9558],{"class":64},[54,15805,89],{"class":88},[54,15807,14944],{"class":92},[54,15809,96],{"class":88},[54,15811,5171],{"class":92},[54,15813,109],{"class":88},[54,15815,15816],{"class":56,"line":887},[54,15817,72],{"emptyLinePlaceholder":71},[54,15819,15820,15822,15824,15826],{"class":56,"line":892},[54,15821,116],{"class":115},[54,15823,96],{"class":88},[54,15825,8313],{"class":92},[54,15827,109],{"class":88},[54,15829,15830,15832,15834,15836,15838,15840],{"class":56,"line":913},[54,15831,116],{"class":115},[54,15833,96],{"class":88},[54,15835,2208],{"class":429},[54,15837,96],{"class":88},[54,15839,8313],{"class":92},[54,15841,382],{"class":88},[54,15843,15844,15846,15848,15850,15852,15854,15856,15858],{"class":56,"line":2121},[54,15845,116],{"class":115},[54,15847,96],{"class":88},[54,15849,8313],{"class":92},[54,15851,459],{"class":88},[54,15853,545],{"class":544},[54,15855,8231],{"class":548},[54,15857,545],{"class":544},[54,15859,937],{"class":88},[14,15861,449],{},[45,15863,15865],{"className":47,"code":15864,"language":49,"meta":50,"style":50},"{'name': 'Ana', 'age': 20, 'is_student': True}\n\u003Cclass 'dict'>\nAna\n",[17,15866,15867,15912,15926],{"__ignoreMap":50},[54,15868,15869,15871,15873,15875,15877,15879,15881,15883,15885,15887,15889,15891,15893,15895,15897,15899,15901,15904,15906,15908,15910],{"class":56,"line":57},[54,15870,8711],{"class":88},[54,15872,2240],{"class":544},[54,15874,8231],{"class":548},[54,15876,2240],{"class":544},[54,15878,147],{"class":88},[54,15880,2234],{"class":544},[54,15882,823],{"class":548},[54,15884,2240],{"class":544},[54,15886,103],{"class":88},[54,15888,2234],{"class":544},[54,15890,8249],{"class":548},[54,15892,2240],{"class":544},[54,15894,147],{"class":88},[54,15896,695],{"class":99},[54,15898,103],{"class":88},[54,15900,2234],{"class":544},[54,15902,15903],{"class":548},"is_student",[54,15905,2240],{"class":544},[54,15907,147],{"class":88},[54,15909,9746],{"class":8608},[54,15911,8258],{"class":88},[54,15913,15914,15916,15918,15920,15922,15924],{"class":56,"line":68},[54,15915,2227],{"class":81},[54,15917,2231],{"class":2230},[54,15919,2234],{"class":544},[54,15921,9644],{"class":548},[54,15923,2240],{"class":544},[54,15925,2243],{"class":81},[54,15927,15928],{"class":56,"line":75},[54,15929,15930],{"class":64},"Ana\n",[14,15932,126,15933,15935,15936,320,15938,320,15940,320,15942,320,15944,320,15946,3587,15948,89],{},[17,15934,13451],{}," when your JSON is already in a string. It returns normal Python objects like ",[17,15937,9644],{},[17,15939,430],{},[17,15941,3173],{},[17,15943,9657],{},[17,15945,9662],{},[17,15947,9667],{},[17,15949,8775],{},[40,15951,1736,15953,1739],{"id":15952},"what-jsonloads-does",[17,15954,13451],{},[14,15956,15957,147],{},[17,15958,13451],{},[149,15960,15961,15964,15967,15974,15987],{},[152,15962,15963],{},"Parses a JSON-formatted string",[152,15965,15966],{},"Converts JSON text into Python objects",[152,15968,15969,15970,5507,15972],{},"Commonly returns a ",[17,15971,9644],{},[17,15973,430],{},[152,15975,15976,15977,320,15979,320,15981,320,15983,324,15985],{},"Can also return ",[17,15978,3173],{},[17,15980,9657],{},[17,15982,9662],{},[17,15984,9667],{},[17,15986,8775],{},[152,15988,15989,15990,160],{},"Comes from Python’s built-in ",[17,15991,8385],{},[14,15993,15994],{},"In short, it takes text like this:",[45,15996,15998],{"className":47,"code":15997,"language":49,"meta":50,"style":50},"'{\"name\": \"Ana\", \"age\": 20}'\n",[17,15999,16000],{"__ignoreMap":50},[54,16001,16002,16004,16007],{"class":56,"line":57},[54,16003,2240],{"class":6905},[54,16005,16006],{"class":6908},"{\"name\": \"Ana\", \"age\": 20}",[54,16008,5297],{"class":6905},[14,16010,16011],{},"and turns it into a real Python dictionary:",[45,16013,16015],{"className":47,"code":16014,"language":49,"meta":50,"style":50},"{\"name\": \"Ana\", \"age\": 20}\n",[17,16016,16017],{"__ignoreMap":50},[54,16018,16019,16021,16023,16025,16027,16029,16031,16033,16035,16037,16039,16041,16043,16045,16047],{"class":56,"line":57},[54,16020,8711],{"class":88},[54,16022,545],{"class":544},[54,16024,8231],{"class":548},[54,16026,545],{"class":544},[54,16028,147],{"class":88},[54,16030,830],{"class":544},[54,16032,823],{"class":548},[54,16034,545],{"class":544},[54,16036,103],{"class":88},[54,16038,830],{"class":544},[54,16040,8249],{"class":548},[54,16042,545],{"class":544},[54,16044,147],{"class":88},[54,16046,695],{"class":99},[54,16048,8258],{"class":88},[40,16050,15426,16052],{"id":16051},"when-to-use-jsonloads",[17,16053,13451],{},[14,16055,126,16056,16058,16059,16061],{},[17,16057,13451],{}," when the input is a ",[35,16060,6123],{}," containing valid JSON.",[14,16063,16064],{},"Common cases:",[149,16066,16067,16070,16073,16076],{},[152,16068,16069],{},"JSON returned from an API as text",[152,16071,16072],{},"A text file that you already read into a string",[152,16074,16075],{},"JSON pasted into your code for testing",[152,16077,16078],{},"A JSON list stored in a variable",[14,16080,16081,16082,16084],{},"Do ",[35,16083,3036],{}," use it directly on a file object.",[14,16086,16087,16088,16092],{},"If you are reading JSON from an open file, use ",[192,16089,16090],{"href":15724},[17,16091,13580],{}," instead.",[40,16094,2258],{"id":2257},[14,16096,16097],{},"Here is a simple example that converts a JSON object string into a Python dictionary:",[45,16099,16101],{"className":47,"code":16100,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"name\": \"Maya\", \"age\": 18, \"is_student\": true}'\ndata = json.loads(text)\n\nprint(data)\nprint(type(data))\nprint(data[\"is_student\"])\nprint(data[\"name\"])\n",[17,16102,16103,16109,16113,16126,16144,16148,16158,16172,16190],{"__ignoreMap":50},[54,16104,16105,16107],{"class":56,"line":57},[54,16106,61],{"class":60},[54,16108,8212],{"class":64},[54,16110,16111],{"class":56,"line":68},[54,16112,72],{"emptyLinePlaceholder":71},[54,16114,16115,16117,16119,16121,16124],{"class":56,"line":75},[54,16116,5134],{"class":64},[54,16118,82],{"class":81},[54,16120,2234],{"class":544},[54,16122,16123],{"class":548},"{\"name\": \"Maya\", \"age\": 18, \"is_student\": true}",[54,16125,5297],{"class":544},[54,16127,16128,16130,16132,16134,16136,16138,16140,16142],{"class":56,"line":112},[54,16129,8221],{"class":64},[54,16131,82],{"class":81},[54,16133,9558],{"class":64},[54,16135,89],{"class":88},[54,16137,14944],{"class":92},[54,16139,96],{"class":88},[54,16141,5171],{"class":92},[54,16143,109],{"class":88},[54,16145,16146],{"class":56,"line":887},[54,16147,72],{"emptyLinePlaceholder":71},[54,16149,16150,16152,16154,16156],{"class":56,"line":892},[54,16151,116],{"class":115},[54,16153,96],{"class":88},[54,16155,8313],{"class":92},[54,16157,109],{"class":88},[54,16159,16160,16162,16164,16166,16168,16170],{"class":56,"line":913},[54,16161,116],{"class":115},[54,16163,96],{"class":88},[54,16165,2208],{"class":429},[54,16167,96],{"class":88},[54,16169,8313],{"class":92},[54,16171,382],{"class":88},[54,16173,16174,16176,16178,16180,16182,16184,16186,16188],{"class":56,"line":2121},[54,16175,116],{"class":115},[54,16177,96],{"class":88},[54,16179,8313],{"class":92},[54,16181,459],{"class":88},[54,16183,545],{"class":544},[54,16185,15903],{"class":548},[54,16187,545],{"class":544},[54,16189,937],{"class":88},[54,16191,16192,16194,16196,16198,16200,16202,16204,16206],{"class":56,"line":2136},[54,16193,116],{"class":115},[54,16195,96],{"class":88},[54,16197,8313],{"class":92},[54,16199,459],{"class":88},[54,16201,545],{"class":544},[54,16203,8231],{"class":548},[54,16205,545],{"class":544},[54,16207,937],{"class":88},[14,16209,449],{},[45,16211,16213],{"className":47,"code":16212,"language":49,"meta":50,"style":50},"{'name': 'Maya', 'age': 18, 'is_student': True}\n\u003Cclass 'dict'>\nTrue\nMaya\n",[17,16214,16215,16260,16274,16278],{"__ignoreMap":50},[54,16216,16217,16219,16221,16223,16225,16227,16229,16231,16233,16235,16237,16239,16241,16243,16246,16248,16250,16252,16254,16256,16258],{"class":56,"line":57},[54,16218,8711],{"class":88},[54,16220,2240],{"class":544},[54,16222,8231],{"class":548},[54,16224,2240],{"class":544},[54,16226,147],{"class":88},[54,16228,2234],{"class":544},[54,16230,14020],{"class":548},[54,16232,2240],{"class":544},[54,16234,103],{"class":88},[54,16236,2234],{"class":544},[54,16238,8249],{"class":548},[54,16240,2240],{"class":544},[54,16242,147],{"class":88},[54,16244,16245],{"class":99}," 18",[54,16247,103],{"class":88},[54,16249,2234],{"class":544},[54,16251,15903],{"class":548},[54,16253,2240],{"class":544},[54,16255,147],{"class":88},[54,16257,9746],{"class":8608},[54,16259,8258],{"class":88},[54,16261,16262,16264,16266,16268,16270,16272],{"class":56,"line":68},[54,16263,2227],{"class":81},[54,16265,2231],{"class":2230},[54,16267,2234],{"class":544},[54,16269,9644],{"class":548},[54,16271,2240],{"class":544},[54,16273,2243],{"class":81},[54,16275,16276],{"class":56,"line":75},[54,16277,14763],{"class":8608},[54,16279,16280],{"class":56,"line":112},[54,16281,14356],{"class":64},[14,16283,16284],{},"Notice this important detail:",[149,16286,16287],{},[152,16288,13978,16289,16291,16292],{},[17,16290,11767],{}," becomes Python ",[17,16293,9175],{},[14,16295,16296],{},"After parsing, you can use normal Python dictionary access:",[45,16298,16300],{"className":47,"code":16299,"language":49,"meta":50,"style":50},"print(data[\"name\"])\n",[17,16301,16302],{"__ignoreMap":50},[54,16303,16304,16306,16308,16310,16312,16314,16316,16318],{"class":56,"line":57},[54,16305,116],{"class":115},[54,16307,96],{"class":88},[54,16309,8313],{"class":92},[54,16311,459],{"class":88},[54,16313,545],{"class":544},[54,16315,8231],{"class":548},[54,16317,545],{"class":544},[54,16319,937],{"class":88},[40,16321,16323],{"id":16322},"what-types-json-becomes-in-python","What types JSON becomes in Python",[14,16325,15464,16326,16328],{},[17,16327,13451],{}," parses JSON, it converts each JSON type into a matching Python type.",[11689,16330,16331,16340],{},[11692,16332,16333],{},[11695,16334,16335,16338],{},[11698,16336,16337],{},"JSON type",[11698,16339,11700],{},[11705,16341,16342,16350,16359,16367,16377,16389],{},[11695,16343,16344,16346],{},[11710,16345,3753],{},[11710,16347,16348],{},[17,16349,9644],{},[11695,16351,16352,16355],{},[11710,16353,16354],{},"array",[11710,16356,16357],{},[17,16358,430],{},[11695,16360,16361,16363],{},[11710,16362,6123],{},[11710,16364,16365],{},[17,16366,3173],{},[11695,16368,16369,16371],{},[11710,16370,121],{},[11710,16372,16373,5507,16375],{},[17,16374,9657],{},[17,16376,9662],{},[11695,16378,16379,16382],{},[11710,16380,16381],{},"true \u002F false",[11710,16383,16384,16386,16387],{},[17,16385,9175],{}," \u002F ",[17,16388,8767],{},[11695,16390,16391,16393],{},[11710,16392,8772],{},[11710,16394,16395],{},[17,16396,8775],{},[14,16398,253],{},[45,16400,16402],{"className":47,"code":16401,"language":49,"meta":50,"style":50},"import json\n\ntext = '''\n{\n    \"user\": \"Liam\",\n    \"score\": 9.5,\n    \"active\": true,\n    \"tags\": [\"python\", \"json\"],\n    \"extra\": null\n}\n'''\n\ndata = json.loads(text)\n\nprint(type(data))\nprint(type(data[\"user\"]))\nprint(type(data[\"score\"]))\nprint(type(data[\"active\"]))\nprint(type(data[\"tags\"]))\nprint(data[\"extra\"])\n",[17,16403,16404,16410,16414,16423,16427,16432,16437,16442,16447,16452,16456,16461,16465,16483,16487,16501,16523,16547,16570,16594],{"__ignoreMap":50},[54,16405,16406,16408],{"class":56,"line":57},[54,16407,61],{"class":60},[54,16409,8212],{"class":64},[54,16411,16412],{"class":56,"line":68},[54,16413,72],{"emptyLinePlaceholder":71},[54,16415,16416,16418,16420],{"class":56,"line":75},[54,16417,5134],{"class":64},[54,16419,82],{"class":81},[54,16421,16422],{"class":544}," '''\n",[54,16424,16425],{"class":56,"line":112},[54,16426,8960],{"class":548},[54,16428,16429],{"class":56,"line":887},[54,16430,16431],{"class":548},"    \"user\": \"Liam\",\n",[54,16433,16434],{"class":56,"line":892},[54,16435,16436],{"class":548},"    \"score\": 9.5,\n",[54,16438,16439],{"class":56,"line":913},[54,16440,16441],{"class":548},"    \"active\": true,\n",[54,16443,16444],{"class":56,"line":2121},[54,16445,16446],{"class":548},"    \"tags\": [\"python\", \"json\"],\n",[54,16448,16449],{"class":56,"line":2136},[54,16450,16451],{"class":548},"    \"extra\": null\n",[54,16453,16454],{"class":56,"line":2151},[54,16455,8258],{"class":548},[54,16457,16458],{"class":56,"line":4442},[54,16459,16460],{"class":544},"'''\n",[54,16462,16463],{"class":56,"line":4468},[54,16464,72],{"emptyLinePlaceholder":71},[54,16466,16467,16469,16471,16473,16475,16477,16479,16481],{"class":56,"line":9843},[54,16468,8221],{"class":64},[54,16470,82],{"class":81},[54,16472,9558],{"class":64},[54,16474,89],{"class":88},[54,16476,14944],{"class":92},[54,16478,96],{"class":88},[54,16480,5171],{"class":92},[54,16482,109],{"class":88},[54,16484,16485],{"class":56,"line":9848},[54,16486,72],{"emptyLinePlaceholder":71},[54,16488,16489,16491,16493,16495,16497,16499],{"class":56,"line":9853},[54,16490,116],{"class":115},[54,16492,96],{"class":88},[54,16494,2208],{"class":429},[54,16496,96],{"class":88},[54,16498,8313],{"class":92},[54,16500,382],{"class":88},[54,16502,16503,16505,16507,16509,16511,16513,16515,16517,16519,16521],{"class":56,"line":9884},[54,16504,116],{"class":115},[54,16506,96],{"class":88},[54,16508,2208],{"class":429},[54,16510,96],{"class":88},[54,16512,8313],{"class":92},[54,16514,459],{"class":88},[54,16516,545],{"class":544},[54,16518,9706],{"class":548},[54,16520,545],{"class":544},[54,16522,14245],{"class":88},[54,16524,16526,16528,16530,16532,16534,16536,16538,16540,16543,16545],{"class":56,"line":16525},17,[54,16527,116],{"class":115},[54,16529,96],{"class":88},[54,16531,2208],{"class":429},[54,16533,96],{"class":88},[54,16535,8313],{"class":92},[54,16537,459],{"class":88},[54,16539,545],{"class":544},[54,16541,16542],{"class":548},"score",[54,16544,545],{"class":544},[54,16546,14245],{"class":88},[54,16548,16550,16552,16554,16556,16558,16560,16562,16564,16566,16568],{"class":56,"line":16549},18,[54,16551,116],{"class":115},[54,16553,96],{"class":88},[54,16555,2208],{"class":429},[54,16557,96],{"class":88},[54,16559,8313],{"class":92},[54,16561,459],{"class":88},[54,16563,545],{"class":544},[54,16565,9739],{"class":548},[54,16567,545],{"class":544},[54,16569,14245],{"class":88},[54,16571,16573,16575,16577,16579,16581,16583,16585,16587,16590,16592],{"class":56,"line":16572},19,[54,16574,116],{"class":115},[54,16576,96],{"class":88},[54,16578,2208],{"class":429},[54,16580,96],{"class":88},[54,16582,8313],{"class":92},[54,16584,459],{"class":88},[54,16586,545],{"class":544},[54,16588,16589],{"class":548},"tags",[54,16591,545],{"class":544},[54,16593,14245],{"class":88},[54,16595,16597,16599,16601,16603,16605,16607,16610,16612],{"class":56,"line":16596},20,[54,16598,116],{"class":115},[54,16600,96],{"class":88},[54,16602,8313],{"class":92},[54,16604,459],{"class":88},[54,16606,545],{"class":544},[54,16608,16609],{"class":548},"extra",[54,16611,545],{"class":544},[54,16613,937],{"class":88},[14,16615,449],{},[45,16617,16619],{"className":47,"code":16618,"language":49,"meta":50,"style":50},"\u003Cclass 'dict'>\n\u003Cclass 'str'>\n\u003Cclass 'float'>\n\u003Cclass 'bool'>\n\u003Cclass 'list'>\nNone\n",[17,16620,16621,16635,16649,16663,16677,16691],{"__ignoreMap":50},[54,16622,16623,16625,16627,16629,16631,16633],{"class":56,"line":57},[54,16624,2227],{"class":81},[54,16626,2231],{"class":2230},[54,16628,2234],{"class":544},[54,16630,9644],{"class":548},[54,16632,2240],{"class":544},[54,16634,2243],{"class":81},[54,16636,16637,16639,16641,16643,16645,16647],{"class":56,"line":68},[54,16638,2227],{"class":81},[54,16640,2231],{"class":2230},[54,16642,2234],{"class":544},[54,16644,3173],{"class":548},[54,16646,2240],{"class":544},[54,16648,2243],{"class":81},[54,16650,16651,16653,16655,16657,16659,16661],{"class":56,"line":75},[54,16652,2227],{"class":81},[54,16654,2231],{"class":2230},[54,16656,2234],{"class":544},[54,16658,9662],{"class":548},[54,16660,2240],{"class":544},[54,16662,2243],{"class":81},[54,16664,16665,16667,16669,16671,16673,16675],{"class":56,"line":112},[54,16666,2227],{"class":81},[54,16668,2231],{"class":2230},[54,16670,2234],{"class":544},[54,16672,9667],{"class":548},[54,16674,2240],{"class":544},[54,16676,2243],{"class":81},[54,16678,16679,16681,16683,16685,16687,16689],{"class":56,"line":887},[54,16680,2227],{"class":81},[54,16682,2231],{"class":2230},[54,16684,2234],{"class":544},[54,16686,430],{"class":548},[54,16688,2240],{"class":544},[54,16690,2243],{"class":81},[54,16692,16693],{"class":56,"line":892},[54,16694,16695],{"class":8608},"None\n",[40,16697,16699,7817,16701],{"id":16698},"jsonloads-vs-jsonload",[17,16700,13451],{},[17,16702,13580],{},[14,16704,16705],{},"These two functions are very similar, but they read from different kinds of input.",[602,16707,16709],{"id":16708},"jsonloads",[17,16710,13451],{},[14,16712,126,16713,16715,16716,147],{},[17,16714,13451],{}," when you have a ",[35,16717,6123],{},[45,16719,16721],{"className":47,"code":16720,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"name\": \"Ana\"}'\ndata = json.loads(text)\nprint(data)\n",[17,16722,16723,16729,16733,16746,16764],{"__ignoreMap":50},[54,16724,16725,16727],{"class":56,"line":57},[54,16726,61],{"class":60},[54,16728,8212],{"class":64},[54,16730,16731],{"class":56,"line":68},[54,16732,72],{"emptyLinePlaceholder":71},[54,16734,16735,16737,16739,16741,16744],{"class":56,"line":75},[54,16736,5134],{"class":64},[54,16738,82],{"class":81},[54,16740,2234],{"class":544},[54,16742,16743],{"class":548},"{\"name\": \"Ana\"}",[54,16745,5297],{"class":544},[54,16747,16748,16750,16752,16754,16756,16758,16760,16762],{"class":56,"line":112},[54,16749,8221],{"class":64},[54,16751,82],{"class":81},[54,16753,9558],{"class":64},[54,16755,89],{"class":88},[54,16757,14944],{"class":92},[54,16759,96],{"class":88},[54,16761,5171],{"class":92},[54,16763,109],{"class":88},[54,16765,16766,16768,16770,16772],{"class":56,"line":887},[54,16767,116],{"class":115},[54,16769,96],{"class":88},[54,16771,8313],{"class":92},[54,16773,109],{"class":88},[602,16775,16777],{"id":16776},"jsonload",[17,16778,13580],{},[14,16780,126,16781,16715,16783,147],{},[17,16782,13580],{},[35,16784,16785],{},"file object",[45,16787,16789],{"className":47,"code":16788,"language":49,"meta":50,"style":50},"import json\n\nwith open(\"data.json\", \"r\") as file:\n    data = json.load(file)\n\nprint(data)\n",[17,16790,16791,16797,16801,16831,16849,16853],{"__ignoreMap":50},[54,16792,16793,16795],{"class":56,"line":57},[54,16794,61],{"class":60},[54,16796,8212],{"class":64},[54,16798,16799],{"class":56,"line":68},[54,16800,72],{"emptyLinePlaceholder":71},[54,16802,16803,16805,16807,16809,16811,16813,16815,16817,16819,16821,16823,16825,16827,16829],{"class":56,"line":75},[54,16804,8267],{"class":60},[54,16806,8270],{"class":115},[54,16808,96],{"class":88},[54,16810,545],{"class":544},[54,16812,8277],{"class":548},[54,16814,545],{"class":544},[54,16816,103],{"class":88},[54,16818,830],{"class":544},[54,16820,10658],{"class":548},[54,16822,545],{"class":544},[54,16824,876],{"class":88},[54,16826,7631],{"class":60},[54,16828,8296],{"class":8295},[54,16830,7572],{"class":88},[54,16832,16833,16835,16837,16839,16841,16843,16845,16847],{"class":56,"line":112},[54,16834,13652],{"class":64},[54,16836,82],{"class":81},[54,16838,9558],{"class":64},[54,16840,89],{"class":88},[54,16842,13661],{"class":92},[54,16844,96],{"class":88},[54,16846,8412],{"class":8295},[54,16848,109],{"class":88},[54,16850,16851],{"class":56,"line":887},[54,16852,72],{"emptyLinePlaceholder":71},[54,16854,16855,16857,16859,16861],{"class":56,"line":892},[54,16856,116],{"class":115},[54,16858,96],{"class":88},[54,16860,8313],{"class":92},[54,16862,109],{"class":88},[14,16864,16865],{},"The difference is:",[149,16867,16868,16873],{},[152,16869,16870,16872],{},[17,16871,13451],{}," reads from a string",[152,16874,16875,16877],{},[17,16876,13580],{}," reads from a file object",[14,16879,16880,16881,89],{},"If you want the file-based version, see ",[192,16882,16883,2834],{"href":15724},[17,16884,13580],{},[40,16886,16888],{"id":16887},"common-errors-beginners-hit","Common errors beginners hit",[14,16890,15032,16891,89],{},[17,16892,13451],{},[602,16894,16896],{"id":16895},"passing-invalid-json-text","Passing invalid JSON text",[14,16898,985,16899,89],{},[17,16900,16901],{},"json.JSONDecodeError",[14,16903,253],{},[45,16905,16907],{"className":47,"code":16906,"language":49,"meta":50,"style":50},"import json\n\ntext = \"{name: 'Ana'}\"\ndata = json.loads(text)\n",[17,16908,16909,16915,16919,16932],{"__ignoreMap":50},[54,16910,16911,16913],{"class":56,"line":57},[54,16912,61],{"class":60},[54,16914,8212],{"class":64},[54,16916,16917],{"class":56,"line":68},[54,16918,72],{"emptyLinePlaceholder":71},[54,16920,16921,16923,16925,16927,16930],{"class":56,"line":75},[54,16922,5134],{"class":64},[54,16924,82],{"class":81},[54,16926,830],{"class":544},[54,16928,16929],{"class":548},"{name: 'Ana'}",[54,16931,5239],{"class":544},[54,16933,16934,16936,16938,16940,16942,16944,16946,16948],{"class":56,"line":112},[54,16935,8221],{"class":64},[54,16937,82],{"class":81},[54,16939,9558],{"class":64},[54,16941,89],{"class":88},[54,16943,14944],{"class":92},[54,16945,96],{"class":88},[54,16947,5171],{"class":92},[54,16949,109],{"class":88},[14,16951,16952],{},"This fails because the text is not valid JSON.",[602,16954,16956],{"id":16955},"using-single-quotes-instead-of-double-quotes","Using single quotes instead of double quotes",[14,16958,16959,16960,16963],{},"JSON requires ",[35,16961,16962],{},"double quotes"," for:",[149,16965,16966,16969],{},[152,16967,16968],{},"object keys",[152,16970,16971],{},"string values",[14,16973,16974],{},"Invalid JSON:",[45,16976,16978],{"className":47,"code":16977,"language":49,"meta":50,"style":50},"\"{'name': 'Ana'}\"\n",[17,16979,16980],{"__ignoreMap":50},[54,16981,16982,16984,16987],{"class":56,"line":57},[54,16983,545],{"class":6905},[54,16985,16986],{"class":6908},"{'name': 'Ana'}",[54,16988,5239],{"class":6905},[14,16990,16991],{},"Valid JSON:",[45,16993,16995],{"className":47,"code":16994,"language":49,"meta":50,"style":50},"'{\"name\": \"Ana\"}'\n",[17,16996,16997],{"__ignoreMap":50},[54,16998,16999,17001,17003],{"class":56,"line":57},[54,17000,2240],{"class":6905},[54,17002,16743],{"class":6908},[54,17004,5297],{"class":6905},[602,17006,17008,17009],{"id":17007},"forgetting-to-import-json","Forgetting to import ",[17,17010,8385],{},[14,17012,985,17013,89],{},[17,17014,988],{},[45,17016,17018],{"className":47,"code":17017,"language":49,"meta":50,"style":50},"text = '{\"name\": \"Ana\"}'\ndata = json.loads(text)\n",[17,17019,17020,17032],{"__ignoreMap":50},[54,17021,17022,17024,17026,17028,17030],{"class":56,"line":57},[54,17023,5134],{"class":64},[54,17025,82],{"class":81},[54,17027,2234],{"class":544},[54,17029,16743],{"class":548},[54,17031,5297],{"class":544},[54,17033,17034,17036,17038,17040,17042,17044,17046,17048],{"class":56,"line":68},[54,17035,8221],{"class":64},[54,17037,82],{"class":81},[54,17039,9558],{"class":64},[54,17041,89],{"class":88},[54,17043,14944],{"class":92},[54,17045,96],{"class":88},[54,17047,5171],{"class":92},[54,17049,109],{"class":88},[14,17051,1031],{},[45,17053,17054],{"className":47,"code":13745,"language":49,"meta":50,"style":50},[17,17055,17056],{"__ignoreMap":50},[54,17057,17058,17060],{"class":56,"line":57},[54,17059,61],{"class":60},[54,17061,8212],{"class":64},[602,17063,17065,17066,17068],{"id":17064},"passing-a-dict-instead-of-a-json-string","Passing a ",[17,17067,9644],{}," instead of a JSON string",[14,17070,17071,17072,89],{},"If you already have a Python dictionary, you do not need ",[17,17073,13451],{},[14,17075,991],{},[45,17077,17079],{"className":47,"code":17078,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"name\": \"Ana\"}\nresult = json.loads(data)\n",[17,17080,17081,17087,17091,17115],{"__ignoreMap":50},[54,17082,17083,17085],{"class":56,"line":57},[54,17084,61],{"class":60},[54,17086,8212],{"class":64},[54,17088,17089],{"class":56,"line":68},[54,17090,72],{"emptyLinePlaceholder":71},[54,17092,17093,17095,17097,17099,17101,17103,17105,17107,17109,17111,17113],{"class":56,"line":75},[54,17094,8221],{"class":64},[54,17096,82],{"class":81},[54,17098,8226],{"class":88},[54,17100,545],{"class":544},[54,17102,8231],{"class":548},[54,17104,545],{"class":544},[54,17106,147],{"class":88},[54,17108,830],{"class":544},[54,17110,823],{"class":548},[54,17112,545],{"class":544},[54,17114,8258],{"class":88},[54,17116,17117,17119,17121,17123,17125,17127,17129,17131],{"class":56,"line":112},[54,17118,3833],{"class":64},[54,17120,82],{"class":81},[54,17122,9558],{"class":64},[54,17124,89],{"class":88},[54,17126,14944],{"class":92},[54,17128,96],{"class":88},[54,17130,8313],{"class":92},[54,17132,109],{"class":88},[14,17134,13808],{},[45,17136,17138],{"className":47,"code":17137,"language":49,"meta":50,"style":50},"data = {\"name\": \"Ana\"}\nprint(data[\"name\"])\n",[17,17139,17140,17164],{"__ignoreMap":50},[54,17141,17142,17144,17146,17148,17150,17152,17154,17156,17158,17160,17162],{"class":56,"line":57},[54,17143,8221],{"class":64},[54,17145,82],{"class":81},[54,17147,8226],{"class":88},[54,17149,545],{"class":544},[54,17151,8231],{"class":548},[54,17153,545],{"class":544},[54,17155,147],{"class":88},[54,17157,830],{"class":544},[54,17159,823],{"class":548},[54,17161,545],{"class":544},[54,17163,8258],{"class":88},[54,17165,17166,17168,17170,17172,17174,17176,17178,17180],{"class":56,"line":68},[54,17167,116],{"class":115},[54,17169,96],{"class":88},[54,17171,8313],{"class":92},[54,17173,459],{"class":88},[54,17175,545],{"class":544},[54,17177,8231],{"class":548},[54,17179,545],{"class":544},[54,17181,937],{"class":88},[14,17183,126,17184,17188,17189,89],{},[192,17185,17186],{"href":8368},[17,17187,8371],{}," if you want to convert a Python object ",[35,17190,17191],{},"into JSON text",[602,17193,17195],{"id":17194},"trying-to-parse-an-empty-string","Trying to parse an empty string",[14,17197,17198],{},"An empty string is not valid JSON.",[45,17200,17202],{"className":47,"code":17201,"language":49,"meta":50,"style":50},"import json\n\ntext = \"\"\ndata = json.loads(text)\n",[17,17203,17204,17210,17214,17223],{"__ignoreMap":50},[54,17205,17206,17208],{"class":56,"line":57},[54,17207,61],{"class":60},[54,17209,8212],{"class":64},[54,17211,17212],{"class":56,"line":68},[54,17213,72],{"emptyLinePlaceholder":71},[54,17215,17216,17218,17220],{"class":56,"line":75},[54,17217,5134],{"class":64},[54,17219,82],{"class":81},[54,17221,17222],{"class":544}," \"\"\n",[54,17224,17225,17227,17229,17231,17233,17235,17237,17239],{"class":56,"line":112},[54,17226,8221],{"class":64},[54,17228,82],{"class":81},[54,17230,9558],{"class":64},[54,17232,89],{"class":88},[54,17234,14944],{"class":92},[54,17236,96],{"class":88},[54,17238,5171],{"class":92},[54,17240,109],{"class":88},[14,17242,17243,17244,89],{},"This raises ",[17,17245,16901],{},[40,17247,17249],{"id":17248},"how-to-handle-bad-json-safely","How to handle bad JSON safely",[14,17251,17252,17253,134,17255,89],{},"If the JSON might be invalid, use ",[17,17254,7452],{},[17,17256,7455],{},[45,17258,17260],{"className":47,"code":17259,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"name\": \"Ana\", \"age\": 20}'\n\ntry:\n    data = json.loads(text)\n    print(\"Parsed successfully:\", data)\nexcept json.JSONDecodeError:\n    print(\"Invalid JSON\")\n    print(\"Raw text:\", repr(text))\n",[17,17261,17262,17268,17272,17284,17288,17294,17312,17332,17344,17359],{"__ignoreMap":50},[54,17263,17264,17266],{"class":56,"line":57},[54,17265,61],{"class":60},[54,17267,8212],{"class":64},[54,17269,17270],{"class":56,"line":68},[54,17271,72],{"emptyLinePlaceholder":71},[54,17273,17274,17276,17278,17280,17282],{"class":56,"line":75},[54,17275,5134],{"class":64},[54,17277,82],{"class":81},[54,17279,2234],{"class":544},[54,17281,16006],{"class":548},[54,17283,5297],{"class":544},[54,17285,17286],{"class":56,"line":112},[54,17287,72],{"emptyLinePlaceholder":71},[54,17289,17290,17292],{"class":56,"line":887},[54,17291,7452],{"class":60},[54,17293,7572],{"class":88},[54,17295,17296,17298,17300,17302,17304,17306,17308,17310],{"class":56,"line":892},[54,17297,13652],{"class":64},[54,17299,82],{"class":81},[54,17301,9558],{"class":64},[54,17303,89],{"class":88},[54,17305,14944],{"class":92},[54,17307,96],{"class":88},[54,17309,5171],{"class":92},[54,17311,109],{"class":88},[54,17313,17314,17316,17318,17320,17323,17325,17327,17330],{"class":56,"line":913},[54,17315,7606],{"class":115},[54,17317,96],{"class":88},[54,17319,545],{"class":544},[54,17321,17322],{"class":548},"Parsed successfully:",[54,17324,545],{"class":544},[54,17326,103],{"class":88},[54,17328,17329],{"class":92}," data",[54,17331,109],{"class":88},[54,17333,17334,17336,17338,17340,17342],{"class":56,"line":2121},[54,17335,7455],{"class":60},[54,17337,9558],{"class":64},[54,17339,89],{"class":88},[54,17341,15479],{"class":1693},[54,17343,7572],{"class":88},[54,17345,17346,17348,17350,17352,17355,17357],{"class":56,"line":2136},[54,17347,7606],{"class":115},[54,17349,96],{"class":88},[54,17351,545],{"class":544},[54,17353,17354],{"class":548},"Invalid JSON",[54,17356,545],{"class":544},[54,17358,109],{"class":88},[54,17360,17361,17363,17365,17367,17370,17372,17374,17377,17379,17381],{"class":56,"line":2151},[54,17362,7606],{"class":115},[54,17364,96],{"class":88},[54,17366,545],{"class":544},[54,17368,17369],{"class":548},"Raw text:",[54,17371,545],{"class":544},[54,17373,103],{"class":88},[54,17375,17376],{"class":115}," repr",[54,17378,96],{"class":88},[54,17380,5171],{"class":92},[54,17382,382],{"class":88},[14,17384,17385],{},"This helps your program fail safely instead of crashing.",[14,17387,17388],{},"When parsing fails:",[149,17390,17391,17394,17397,17400,17403],{},[152,17392,17393],{},"Print the raw string",[152,17395,17396],{},"Check whether the input is really a string",[152,17398,17399],{},"Look for missing commas",[152,17401,17402],{},"Check for single quotes instead of double quotes",[152,17404,17405],{},"Look for extra characters before or after the JSON",[14,17407,17408],{},"Helpful debugging lines:",[45,17410,17412],{"className":47,"code":17411,"language":49,"meta":50,"style":50},"print(text)\nprint(type(text))\nprint(repr(text))\n",[17,17413,17414,17424,17438],{"__ignoreMap":50},[54,17415,17416,17418,17420,17422],{"class":56,"line":57},[54,17417,116],{"class":115},[54,17419,96],{"class":88},[54,17421,5171],{"class":92},[54,17423,109],{"class":88},[54,17425,17426,17428,17430,17432,17434,17436],{"class":56,"line":68},[54,17427,116],{"class":115},[54,17429,96],{"class":88},[54,17431,2208],{"class":429},[54,17433,96],{"class":88},[54,17435,5171],{"class":92},[54,17437,382],{"class":88},[54,17439,17440,17442,17444,17446,17448,17450],{"class":56,"line":75},[54,17441,116],{"class":115},[54,17443,96],{"class":88},[54,17445,7483],{"class":115},[54,17447,96],{"class":88},[54,17449,5171],{"class":92},[54,17451,382],{"class":88},[14,17453,17454],{},"Example with bad JSON:",[45,17456,17458],{"className":47,"code":17457,"language":49,"meta":50,"style":50},"import json\n\ntext = \"{'name': 'Ana'}\"\n\ntry:\n    data = json.loads(text)\nexcept json.JSONDecodeError as error:\n    print(\"JSON error:\", error)\n    print(\"Problem text:\", repr(text))\n",[17,17459,17460,17466,17470,17482,17486,17492,17510,17527,17546],{"__ignoreMap":50},[54,17461,17462,17464],{"class":56,"line":57},[54,17463,61],{"class":60},[54,17465,8212],{"class":64},[54,17467,17468],{"class":56,"line":68},[54,17469,72],{"emptyLinePlaceholder":71},[54,17471,17472,17474,17476,17478,17480],{"class":56,"line":75},[54,17473,5134],{"class":64},[54,17475,82],{"class":81},[54,17477,830],{"class":544},[54,17479,16986],{"class":548},[54,17481,5239],{"class":544},[54,17483,17484],{"class":56,"line":112},[54,17485,72],{"emptyLinePlaceholder":71},[54,17487,17488,17490],{"class":56,"line":887},[54,17489,7452],{"class":60},[54,17491,7572],{"class":88},[54,17493,17494,17496,17498,17500,17502,17504,17506,17508],{"class":56,"line":892},[54,17495,13652],{"class":64},[54,17497,82],{"class":81},[54,17499,9558],{"class":64},[54,17501,89],{"class":88},[54,17503,14944],{"class":92},[54,17505,96],{"class":88},[54,17507,5171],{"class":92},[54,17509,109],{"class":88},[54,17511,17512,17514,17516,17518,17520,17522,17525],{"class":56,"line":913},[54,17513,7455],{"class":60},[54,17515,9558],{"class":64},[54,17517,89],{"class":88},[54,17519,15479],{"class":1693},[54,17521,7631],{"class":60},[54,17523,17524],{"class":64}," error",[54,17526,7572],{"class":88},[54,17528,17529,17531,17533,17535,17538,17540,17542,17544],{"class":56,"line":2121},[54,17530,7606],{"class":115},[54,17532,96],{"class":88},[54,17534,545],{"class":544},[54,17536,17537],{"class":548},"JSON error:",[54,17539,545],{"class":544},[54,17541,103],{"class":88},[54,17543,17524],{"class":92},[54,17545,109],{"class":88},[54,17547,17548,17550,17552,17554,17557,17559,17561,17563,17565,17567],{"class":56,"line":2136},[54,17549,7606],{"class":115},[54,17551,96],{"class":88},[54,17553,545],{"class":544},[54,17555,17556],{"class":548},"Problem text:",[54,17558,545],{"class":544},[54,17560,103],{"class":88},[54,17562,17376],{"class":115},[54,17564,96],{"class":88},[54,17566,5171],{"class":92},[54,17568,382],{"class":88},[40,17570,17572],{"id":17571},"common-real-world-uses","Common real-world uses",[14,17574,17575,17577],{},[17,17576,13451],{}," is often used in practical Python code.",[602,17579,17581],{"id":17580},"parsing-api-response-text","Parsing API response text",[14,17583,17584],{},"Some APIs return JSON as text. You can parse that text into Python data.",[45,17586,17588],{"className":47,"code":17587,"language":49,"meta":50,"style":50},"import json\n\nresponse_text = '{\"status\": \"ok\", \"results\": [1, 2, 3]}'\ndata = json.loads(response_text)\n\nprint(data[\"status\"])\nprint(data[\"results\"])\n",[17,17589,17590,17596,17600,17614,17633,17637,17656],{"__ignoreMap":50},[54,17591,17592,17594],{"class":56,"line":57},[54,17593,61],{"class":60},[54,17595,8212],{"class":64},[54,17597,17598],{"class":56,"line":68},[54,17599,72],{"emptyLinePlaceholder":71},[54,17601,17602,17605,17607,17609,17612],{"class":56,"line":75},[54,17603,17604],{"class":64},"response_text ",[54,17606,82],{"class":81},[54,17608,2234],{"class":544},[54,17610,17611],{"class":548},"{\"status\": \"ok\", \"results\": [1, 2, 3]}",[54,17613,5297],{"class":544},[54,17615,17616,17618,17620,17622,17624,17626,17628,17631],{"class":56,"line":112},[54,17617,8221],{"class":64},[54,17619,82],{"class":81},[54,17621,9558],{"class":64},[54,17623,89],{"class":88},[54,17625,14944],{"class":92},[54,17627,96],{"class":88},[54,17629,17630],{"class":92},"response_text",[54,17632,109],{"class":88},[54,17634,17635],{"class":56,"line":887},[54,17636,72],{"emptyLinePlaceholder":71},[54,17638,17639,17641,17643,17645,17647,17649,17652,17654],{"class":56,"line":892},[54,17640,116],{"class":115},[54,17642,96],{"class":88},[54,17644,8313],{"class":92},[54,17646,459],{"class":88},[54,17648,545],{"class":544},[54,17650,17651],{"class":548},"status",[54,17653,545],{"class":544},[54,17655,937],{"class":88},[54,17657,17658,17660,17662,17664,17666,17668,17671,17673],{"class":56,"line":913},[54,17659,116],{"class":115},[54,17661,96],{"class":88},[54,17663,8313],{"class":92},[54,17665,459],{"class":88},[54,17667,545],{"class":544},[54,17669,17670],{"class":548},"results",[54,17672,545],{"class":544},[54,17674,937],{"class":88},[602,17676,17678],{"id":17677},"converting-copied-json-text-into-a-python-dict","Converting copied JSON text into a Python dict",[45,17680,17682],{"className":47,"code":17681,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"city\": \"Tokyo\", \"population\": 14}'\ndata = json.loads(text)\n\nprint(data[\"city\"])\n",[17,17683,17684,17690,17694,17707,17725,17729],{"__ignoreMap":50},[54,17685,17686,17688],{"class":56,"line":57},[54,17687,61],{"class":60},[54,17689,8212],{"class":64},[54,17691,17692],{"class":56,"line":68},[54,17693,72],{"emptyLinePlaceholder":71},[54,17695,17696,17698,17700,17702,17705],{"class":56,"line":75},[54,17697,5134],{"class":64},[54,17699,82],{"class":81},[54,17701,2234],{"class":544},[54,17703,17704],{"class":548},"{\"city\": \"Tokyo\", \"population\": 14}",[54,17706,5297],{"class":544},[54,17708,17709,17711,17713,17715,17717,17719,17721,17723],{"class":56,"line":112},[54,17710,8221],{"class":64},[54,17712,82],{"class":81},[54,17714,9558],{"class":64},[54,17716,89],{"class":88},[54,17718,14944],{"class":92},[54,17720,96],{"class":88},[54,17722,5171],{"class":92},[54,17724,109],{"class":88},[54,17726,17727],{"class":56,"line":887},[54,17728,72],{"emptyLinePlaceholder":71},[54,17730,17731,17733,17735,17737,17739,17741,17743,17745],{"class":56,"line":892},[54,17732,116],{"class":115},[54,17734,96],{"class":88},[54,17736,8313],{"class":92},[54,17738,459],{"class":88},[54,17740,545],{"class":544},[54,17742,8450],{"class":548},[54,17744,545],{"class":544},[54,17746,937],{"class":88},[602,17748,17750,17751],{"id":17749},"reading-json-after-using-fileread","Reading JSON after using ",[17,17752,15759],{},[14,17754,17755,17756,17759,17760,17762],{},"If you already used ",[17,17757,17758],{},".read()",", the result is a string, so ",[17,17761,13451],{}," is correct.",[45,17764,17766],{"className":47,"code":17765,"language":49,"meta":50,"style":50},"import json\n\nwith open(\"data.json\", \"r\") as file:\n    text = file.read()\n\ndata = json.loads(text)\nprint(data)\n",[17,17767,17768,17774,17778,17808,17824,17828,17846],{"__ignoreMap":50},[54,17769,17770,17772],{"class":56,"line":57},[54,17771,61],{"class":60},[54,17773,8212],{"class":64},[54,17775,17776],{"class":56,"line":68},[54,17777,72],{"emptyLinePlaceholder":71},[54,17779,17780,17782,17784,17786,17788,17790,17792,17794,17796,17798,17800,17802,17804,17806],{"class":56,"line":75},[54,17781,8267],{"class":60},[54,17783,8270],{"class":115},[54,17785,96],{"class":88},[54,17787,545],{"class":544},[54,17789,8277],{"class":548},[54,17791,545],{"class":544},[54,17793,103],{"class":88},[54,17795,830],{"class":544},[54,17797,10658],{"class":548},[54,17799,545],{"class":544},[54,17801,876],{"class":88},[54,17803,7631],{"class":60},[54,17805,8296],{"class":8295},[54,17807,7572],{"class":88},[54,17809,17810,17813,17815,17817,17819,17822],{"class":56,"line":112},[54,17811,17812],{"class":64},"    text ",[54,17814,82],{"class":81},[54,17816,8296],{"class":8295},[54,17818,89],{"class":88},[54,17820,17821],{"class":92},"read",[54,17823,1670],{"class":88},[54,17825,17826],{"class":56,"line":887},[54,17827,72],{"emptyLinePlaceholder":71},[54,17829,17830,17832,17834,17836,17838,17840,17842,17844],{"class":56,"line":892},[54,17831,8221],{"class":64},[54,17833,82],{"class":81},[54,17835,9558],{"class":64},[54,17837,89],{"class":88},[54,17839,14944],{"class":92},[54,17841,96],{"class":88},[54,17843,5171],{"class":92},[54,17845,109],{"class":88},[54,17847,17848,17850,17852,17854],{"class":56,"line":913},[54,17849,116],{"class":115},[54,17851,96],{"class":88},[54,17853,8313],{"class":92},[54,17855,109],{"class":88},[14,17857,17858,17859,89],{},"If you want to parse directly from the file object, use ",[192,17860,17861],{"href":15724},[17,17862,13580],{},[602,17864,17866],{"id":17865},"turning-a-json-list-string-into-a-python-list","Turning a JSON list string into a Python list",[45,17868,17870],{"className":47,"code":17869,"language":49,"meta":50,"style":50},"import json\n\ntext = '[\"apple\", \"banana\", \"cherry\"]'\nitems = json.loads(text)\n\nprint(items)\nprint(type(items))\nprint(items[0])\n",[17,17871,17872,17878,17882,17895,17914,17918,17928,17942],{"__ignoreMap":50},[54,17873,17874,17876],{"class":56,"line":57},[54,17875,61],{"class":60},[54,17877,8212],{"class":64},[54,17879,17880],{"class":56,"line":68},[54,17881,72],{"emptyLinePlaceholder":71},[54,17883,17884,17886,17888,17890,17893],{"class":56,"line":75},[54,17885,5134],{"class":64},[54,17887,82],{"class":81},[54,17889,2234],{"class":544},[54,17891,17892],{"class":548},"[\"apple\", \"banana\", \"cherry\"]",[54,17894,5297],{"class":544},[54,17896,17897,17900,17902,17904,17906,17908,17910,17912],{"class":56,"line":112},[54,17898,17899],{"class":64},"items ",[54,17901,82],{"class":81},[54,17903,9558],{"class":64},[54,17905,89],{"class":88},[54,17907,14944],{"class":92},[54,17909,96],{"class":88},[54,17911,5171],{"class":92},[54,17913,109],{"class":88},[54,17915,17916],{"class":56,"line":887},[54,17917,72],{"emptyLinePlaceholder":71},[54,17919,17920,17922,17924,17926],{"class":56,"line":892},[54,17921,116],{"class":115},[54,17923,96],{"class":88},[54,17925,10388],{"class":92},[54,17927,109],{"class":88},[54,17929,17930,17932,17934,17936,17938,17940],{"class":56,"line":913},[54,17931,116],{"class":115},[54,17933,96],{"class":88},[54,17935,2208],{"class":429},[54,17937,96],{"class":88},[54,17939,10388],{"class":92},[54,17941,382],{"class":88},[54,17943,17944,17946,17948,17950,17952,17954],{"class":56,"line":2121},[54,17945,116],{"class":115},[54,17947,96],{"class":88},[54,17949,10388],{"class":92},[54,17951,459],{"class":88},[54,17953,752],{"class":99},[54,17955,937],{"class":88},[14,17957,449],{},[45,17959,17961],{"className":47,"code":17960,"language":49,"meta":50,"style":50},"['apple', 'banana', 'cherry']\n\u003Cclass 'list'>\napple\n",[17,17962,17963,17992,18006],{"__ignoreMap":50},[54,17964,17965,17967,17969,17971,17973,17975,17977,17979,17981,17983,17985,17988,17990],{"class":56,"line":57},[54,17966,459],{"class":88},[54,17968,2240],{"class":544},[54,17970,10399],{"class":548},[54,17972,2240],{"class":544},[54,17974,103],{"class":88},[54,17976,2234],{"class":544},[54,17978,10408],{"class":548},[54,17980,2240],{"class":544},[54,17982,103],{"class":88},[54,17984,2234],{"class":544},[54,17986,17987],{"class":548},"cherry",[54,17989,2240],{"class":544},[54,17991,469],{"class":88},[54,17993,17994,17996,17998,18000,18002,18004],{"class":56,"line":68},[54,17995,2227],{"class":81},[54,17997,2231],{"class":2230},[54,17999,2234],{"class":544},[54,18001,430],{"class":548},[54,18003,2240],{"class":544},[54,18005,2243],{"class":81},[54,18007,18008],{"class":56,"line":75},[54,18009,18010],{"class":64},"apple\n",[40,18012,978],{"id":977},[14,18014,18015],{},"These are frequent causes of problems:",[149,18017,18018,18021,18028,18031,18034],{},[152,18019,18020],{},"JSON uses double quotes, but your input uses single quotes",[152,18022,18023,18024,5507,18026],{},"The input is already a Python ",[17,18025,9644],{},[17,18027,430],{},[152,18029,18030],{},"The string is empty or incomplete",[152,18032,18033],{},"The JSON text has a trailing comma",[152,18035,18036,18037,18039,18040],{},"You used ",[17,18038,13451],{}," on a file object instead of ",[17,18041,13580],{},[14,18043,18044],{},"If you are not sure what you have, inspect it first:",[45,18046,18047],{"className":47,"code":17411,"language":49,"meta":50,"style":50},[17,18048,18049,18059,18073],{"__ignoreMap":50},[54,18050,18051,18053,18055,18057],{"class":56,"line":57},[54,18052,116],{"class":115},[54,18054,96],{"class":88},[54,18056,5171],{"class":92},[54,18058,109],{"class":88},[54,18060,18061,18063,18065,18067,18069,18071],{"class":56,"line":68},[54,18062,116],{"class":115},[54,18064,96],{"class":88},[54,18066,2208],{"class":429},[54,18068,96],{"class":88},[54,18070,5171],{"class":92},[54,18072,382],{"class":88},[54,18074,18075,18077,18079,18081,18083,18085],{"class":56,"line":75},[54,18076,116],{"class":115},[54,18078,96],{"class":88},[54,18080,7483],{"class":115},[54,18082,96],{"class":88},[54,18084,5171],{"class":92},[54,18086,382],{"class":88},[40,18088,1419],{"id":1418},[602,18090,3390,18092,3393],{"id":18091},"what-does-jsonloads-return",[17,18093,13451],{},[14,18095,18096,18097,5507,18099,89],{},"It returns a Python object created from the JSON string, usually a ",[17,18098,9644],{},[17,18100,430],{},[602,18102,8075,18104,134,18106,3409],{"id":18103},"what-is-the-difference-between-jsonloads-and-jsonload",[17,18105,13451],{},[17,18107,13580],{},[14,18109,18110,18112,18113,18115],{},[17,18111,13451],{}," parses a string. ",[17,18114,13580],{}," parses a file object.",[602,18117,8091,18119,18121],{"id":18118},"can-jsonloads-parse-single-quotes",[17,18120,13451],{}," parse single quotes?",[14,18123,18124],{},"No. Valid JSON uses double quotes for strings and object keys.",[602,18126,18128,18129,3409],{"id":18127},"why-do-i-get-jsondecodeerror","Why do I get ",[17,18130,15479],{},[14,18132,18133],{},"The string is not valid JSON. Check quotes, commas, brackets, and extra characters.",[602,18135,18137,18138,18140],{"id":18136},"do-i-need-jsonloads-if-i-already-have-a-python-dict","Do I need ",[17,18139,13451],{}," if I already have a Python dict?",[14,18142,18143,18144,18146],{},"No. ",[17,18145,13451],{}," is only for JSON strings, not Python objects.",[40,18148,1510],{"id":1509},[149,18150,18151,18157,18163,18169,18174],{},[152,18152,18153],{},[192,18154,13760,18155,13763],{"href":8781},[17,18156,8385],{},[152,18158,18159],{},[192,18160,18161,12898],{"href":15724},[17,18162,13580],{},[152,18164,18165],{},[192,18166,18167,12898],{"href":8368},[17,18168,8371],{},[152,18170,18171],{},[192,18172,8141],{"href":18173},"\u002Fhow-to\u002Fhow-to-parse-json-in-python",[152,18175,18176],{},[192,18177,18179],{"href":18178},"\u002Fhow-to\u002Fhow-to-handle-api-responses-in-python","How to handle API responses in Python",[14,18181,18182],{},"If you are choosing the right JSON tool:",[149,18184,18185,18190,18195],{},[152,18186,126,18187,18189],{},[17,18188,13451],{}," to parse JSON from a string",[152,18191,126,18192,18194],{},[17,18193,13580],{}," to parse JSON from a file",[152,18196,126,18197,18199],{},[17,18198,8371],{}," to convert Python objects back into JSON text",[1545,18201,18202],{},"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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .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}",{"title":50,"searchDepth":68,"depth":68,"links":18204},[18205,18206,18208,18210,18211,18212,18217,18226,18227,18234,18235,18247],{"id":3595,"depth":68,"text":3596},{"id":15952,"depth":68,"text":18207},"What json.loads() does",{"id":16051,"depth":68,"text":18209},"When to use json.loads()",{"id":2257,"depth":68,"text":2258},{"id":16322,"depth":68,"text":16323},{"id":16698,"depth":68,"text":18213,"children":18214},"json.loads() vs json.load()",[18215,18216],{"id":16708,"depth":75,"text":13451},{"id":16776,"depth":75,"text":13580},{"id":16887,"depth":68,"text":16888,"children":18218},[18219,18220,18221,18223,18225],{"id":16895,"depth":75,"text":16896},{"id":16955,"depth":75,"text":16956},{"id":17007,"depth":75,"text":18222},"Forgetting to import json",{"id":17064,"depth":75,"text":18224},"Passing a dict instead of a JSON string",{"id":17194,"depth":75,"text":17195},{"id":17248,"depth":68,"text":17249},{"id":17571,"depth":68,"text":17572,"children":18228},[18229,18230,18231,18233],{"id":17580,"depth":75,"text":17581},{"id":17677,"depth":75,"text":17678},{"id":17749,"depth":75,"text":18232},"Reading JSON after using file.read()",{"id":17865,"depth":75,"text":17866},{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":18236},[18237,18239,18241,18243,18245],{"id":18091,"depth":75,"text":18238},"What does json.loads() return?",{"id":18103,"depth":75,"text":18240},"What is the difference between json.loads() and json.load()?",{"id":18118,"depth":75,"text":18242},"Can json.loads() parse single quotes?",{"id":18127,"depth":75,"text":18244},"Why do I get JSONDecodeError?",{"id":18136,"depth":75,"text":18246},"Do I need json.loads() if I already have a Python dict?",{"id":1509,"depth":68,"text":1510},"Master json.loads function explained in our comprehensive Python beginner guide.",{},{"title":15730,"description":18248},"standard-library\u002Fjson.loads-function-explained","RQgLS9oxirAbxaJrRLGPjDXiEZdpKBkwYRXI8rgRvrg",{"id":18254,"title":18255,"body":18256,"description":20472,"extension":1577,"meta":20473,"navigation":71,"path":20474,"seo":20475,"stem":20476,"__hash__":20477},"content\u002Fstandard-library\u002Fmath.ceil-and-math.floor-explained.md","math.ceil() and math.floor() Explained",{"type":7,"value":18257,"toc":20423},[18258,18261,18270,18287,18300,18307,18327,18329,18437,18445,18449,18471,18475,18483,18490,18494,18574,18577,18601,18604,18610,18671,18675,18682,18688,18691,18766,18768,18788,18791,18797,18856,18860,18863,18866,18879,18882,18885,18899,18905,18923,18926,18942,18945,18954,18969,18973,18976,18981,18984,19042,19047,19050,19106,19111,19114,19152,19155,19179,19187,19194,19198,19201,19205,19260,19264,19325,19329,19385,19388,19392,19395,19398,19474,19480,19502,19506,19509,19584,19588,19602,19606,19616,19622,19674,19677,19700,19713,19717,19720,19726,19729,19743,19745,19821,19831,19837,19839,19853,19855,19931,19934,19936,19939,19944,19946,19970,19972,20005,20011,20019,20022,20057,20066,20072,20075,20113,20120,20128,20131,20184,20192,20196,20203,20244,20247,20317,20324,20326,20330,20338,20342,20345,20349,20362,20366,20376,20380,20388,20390,20420],[10,18259,18255],{"id":18260},"mathceil-and-mathfloor-explained",[14,18262,18263,134,18266,18269],{},[17,18264,18265],{},"math.ceil()",[17,18267,18268],{},"math.floor()"," are useful when you need to round numbers in a specific direction.",[149,18271,18272,18280],{},[152,18273,18274,18276,18277],{},[17,18275,18265],{}," rounds ",[35,18278,18279],{},"up",[152,18281,18282,18276,18284],{},[17,18283,18268],{},[35,18285,18286],{},"down",[14,18288,18289,18290,18296,18297,18299],{},"These functions are part of Python’s ",[192,18291,18293,13763],{"href":18292},"\u002Fstandard-library\u002Fpython-math-module-overview\u002F",[17,18294,18295],{},"math",", so you must import ",[17,18298,18295],{}," before using them.",[14,18301,18302,18303,18306],{},"If you are comparing them with ",[17,18304,18305],{},"round()",", the key difference is simple:",[149,18308,18309,18314,18319],{},[152,18310,18311,18313],{},[17,18312,18265],{}," always goes up",[152,18315,18316,18318],{},[17,18317,18268],{}," always goes down",[152,18320,18321,18326],{},[192,18322,18324],{"href":18323},"\u002Freference\u002Fpython-round-function-explained\u002F",[17,18325,18305],{}," goes to the nearest value",[40,18328,3596],{"id":3595},[45,18330,18332],{"className":47,"code":18331,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(3.2))   # 4\nprint(math.floor(3.8))  # 3\nprint(math.ceil(-3.8))  # -3\nprint(math.floor(-3.2)) # -4\n",[17,18333,18334,18341,18345,18368,18391,18414],{"__ignoreMap":50},[54,18335,18336,18338],{"class":56,"line":57},[54,18337,61],{"class":60},[54,18339,18340],{"class":64}," math\n",[54,18342,18343],{"class":56,"line":68},[54,18344,72],{"emptyLinePlaceholder":71},[54,18346,18347,18349,18351,18353,18355,18358,18360,18363,18365],{"class":56,"line":75},[54,18348,116],{"class":115},[54,18350,96],{"class":88},[54,18352,18295],{"class":92},[54,18354,89],{"class":88},[54,18356,18357],{"class":92},"ceil",[54,18359,96],{"class":88},[54,18361,18362],{"class":99},"3.2",[54,18364,4310],{"class":88},[54,18366,18367],{"class":4313},"   # 4\n",[54,18369,18370,18372,18374,18376,18378,18381,18383,18386,18388],{"class":56,"line":112},[54,18371,116],{"class":115},[54,18373,96],{"class":88},[54,18375,18295],{"class":92},[54,18377,89],{"class":88},[54,18379,18380],{"class":92},"floor",[54,18382,96],{"class":88},[54,18384,18385],{"class":99},"3.8",[54,18387,4310],{"class":88},[54,18389,18390],{"class":4313},"  # 3\n",[54,18392,18393,18395,18397,18399,18401,18403,18405,18407,18409,18411],{"class":56,"line":887},[54,18394,116],{"class":115},[54,18396,96],{"class":88},[54,18398,18295],{"class":92},[54,18400,89],{"class":88},[54,18402,18357],{"class":92},[54,18404,96],{"class":88},[54,18406,1467],{"class":81},[54,18408,18385],{"class":99},[54,18410,4310],{"class":88},[54,18412,18413],{"class":4313},"  # -3\n",[54,18415,18416,18418,18420,18422,18424,18426,18428,18430,18432,18434],{"class":56,"line":892},[54,18417,116],{"class":115},[54,18419,96],{"class":88},[54,18421,18295],{"class":92},[54,18423,89],{"class":88},[54,18425,18380],{"class":92},[54,18427,96],{"class":88},[54,18429,1467],{"class":81},[54,18431,18362],{"class":99},[54,18433,4310],{"class":88},[54,18435,18436],{"class":4313}," # -4\n",[14,18438,126,18439,18441,18442,18444],{},[17,18440,18265],{}," to round up to the next whole number. Use ",[17,18443,18268],{}," to round down to the previous whole number.",[40,18446,18448],{"id":18447},"what-this-page-covers","What this page covers",[149,18450,18451,18456,18461,18468],{},[152,18452,18453,18455],{},[17,18454,18265],{}," returns the smallest integer greater than or equal to a number",[152,18457,18458,18460],{},[17,18459,18268],{}," returns the largest integer less than or equal to a number",[152,18462,18463,18464,27,18466,167],{},"Both functions are in the ",[17,18465,18295],{},[17,18467,18295],{},[152,18469,18470],{},"These functions return integers",[40,18472,18474],{"id":18473},"how-mathceil-works","How math.ceil() works",[14,18476,18477,18479,18480,89],{},[17,18478,18265],{}," rounds a number ",[35,18481,18482],{},"upward",[14,18484,18485,18486,18489],{},"That means it returns the smallest integer that is still ",[35,18487,18488],{},"greater than or equal to"," the original number.",[602,18491,18493],{"id":18492},"examples","Examples",[45,18495,18497],{"className":47,"code":18496,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(2.1))   # 3\nprint(math.ceil(2.9))   # 3\nprint(math.ceil(2.0))   # 2\n",[17,18498,18499,18505,18509,18531,18552],{"__ignoreMap":50},[54,18500,18501,18503],{"class":56,"line":57},[54,18502,61],{"class":60},[54,18504,18340],{"class":64},[54,18506,18507],{"class":56,"line":68},[54,18508,72],{"emptyLinePlaceholder":71},[54,18510,18511,18513,18515,18517,18519,18521,18523,18526,18528],{"class":56,"line":75},[54,18512,116],{"class":115},[54,18514,96],{"class":88},[54,18516,18295],{"class":92},[54,18518,89],{"class":88},[54,18520,18357],{"class":92},[54,18522,96],{"class":88},[54,18524,18525],{"class":99},"2.1",[54,18527,4310],{"class":88},[54,18529,18530],{"class":4313},"   # 3\n",[54,18532,18533,18535,18537,18539,18541,18543,18545,18548,18550],{"class":56,"line":112},[54,18534,116],{"class":115},[54,18536,96],{"class":88},[54,18538,18295],{"class":92},[54,18540,89],{"class":88},[54,18542,18357],{"class":92},[54,18544,96],{"class":88},[54,18546,18547],{"class":99},"2.9",[54,18549,4310],{"class":88},[54,18551,18530],{"class":4313},[54,18553,18554,18556,18558,18560,18562,18564,18566,18569,18571],{"class":56,"line":887},[54,18555,116],{"class":115},[54,18557,96],{"class":88},[54,18559,18295],{"class":92},[54,18561,89],{"class":88},[54,18563,18357],{"class":92},[54,18565,96],{"class":88},[54,18567,18568],{"class":99},"2.0",[54,18570,4310],{"class":88},[54,18572,18573],{"class":4313},"   # 2\n",[14,18575,18576],{},"What to notice:",[149,18578,18579,18586,18593],{},[152,18580,18581,18583,18584],{},[17,18582,18525],{}," becomes ",[17,18585,401],{},[152,18587,18588,18590,18591],{},[17,18589,18547],{}," also becomes ",[17,18592,401],{},[152,18594,18595,18597,18598,18600],{},[17,18596,18568],{}," stays ",[17,18599,396],{}," because it is already a whole number",[14,18602,18603],{},"For positive numbers, “up” means moving to a larger number.",[14,18605,18606,18607,89],{},"For negative numbers, “up” still means moving to a larger number, which is why it goes ",[35,18608,18609],{},"closer to zero",[45,18611,18613],{"className":47,"code":18612,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(-2.7))  # -2\nprint(math.ceil(-2.1))  # -2\n",[17,18614,18615,18621,18625,18649],{"__ignoreMap":50},[54,18616,18617,18619],{"class":56,"line":57},[54,18618,61],{"class":60},[54,18620,18340],{"class":64},[54,18622,18623],{"class":56,"line":68},[54,18624,72],{"emptyLinePlaceholder":71},[54,18626,18627,18629,18631,18633,18635,18637,18639,18641,18644,18646],{"class":56,"line":75},[54,18628,116],{"class":115},[54,18630,96],{"class":88},[54,18632,18295],{"class":92},[54,18634,89],{"class":88},[54,18636,18357],{"class":92},[54,18638,96],{"class":88},[54,18640,1467],{"class":81},[54,18642,18643],{"class":99},"2.7",[54,18645,4310],{"class":88},[54,18647,18648],{"class":4313},"  # -2\n",[54,18650,18651,18653,18655,18657,18659,18661,18663,18665,18667,18669],{"class":56,"line":112},[54,18652,116],{"class":115},[54,18654,96],{"class":88},[54,18656,18295],{"class":92},[54,18658,89],{"class":88},[54,18660,18357],{"class":92},[54,18662,96],{"class":88},[54,18664,1467],{"class":81},[54,18666,18525],{"class":99},[54,18668,4310],{"class":88},[54,18670,18648],{"class":4313},[40,18672,18674],{"id":18673},"how-mathfloor-works","How math.floor() works",[14,18676,18677,18479,18679,89],{},[17,18678,18268],{},[35,18680,18681],{},"downward",[14,18683,18684,18685,18489],{},"That means it returns the largest integer that is still ",[35,18686,18687],{},"less than or equal to",[602,18689,18493],{"id":18690},"examples-1",[45,18692,18694],{"className":47,"code":18693,"language":49,"meta":50,"style":50},"import math\n\nprint(math.floor(2.1))   # 2\nprint(math.floor(2.9))   # 2\nprint(math.floor(2.0))   # 2\n",[17,18695,18696,18702,18706,18726,18746],{"__ignoreMap":50},[54,18697,18698,18700],{"class":56,"line":57},[54,18699,61],{"class":60},[54,18701,18340],{"class":64},[54,18703,18704],{"class":56,"line":68},[54,18705,72],{"emptyLinePlaceholder":71},[54,18707,18708,18710,18712,18714,18716,18718,18720,18722,18724],{"class":56,"line":75},[54,18709,116],{"class":115},[54,18711,96],{"class":88},[54,18713,18295],{"class":92},[54,18715,89],{"class":88},[54,18717,18380],{"class":92},[54,18719,96],{"class":88},[54,18721,18525],{"class":99},[54,18723,4310],{"class":88},[54,18725,18573],{"class":4313},[54,18727,18728,18730,18732,18734,18736,18738,18740,18742,18744],{"class":56,"line":112},[54,18729,116],{"class":115},[54,18731,96],{"class":88},[54,18733,18295],{"class":92},[54,18735,89],{"class":88},[54,18737,18380],{"class":92},[54,18739,96],{"class":88},[54,18741,18547],{"class":99},[54,18743,4310],{"class":88},[54,18745,18573],{"class":4313},[54,18747,18748,18750,18752,18754,18756,18758,18760,18762,18764],{"class":56,"line":887},[54,18749,116],{"class":115},[54,18751,96],{"class":88},[54,18753,18295],{"class":92},[54,18755,89],{"class":88},[54,18757,18380],{"class":92},[54,18759,96],{"class":88},[54,18761,18568],{"class":99},[54,18763,4310],{"class":88},[54,18765,18573],{"class":4313},[14,18767,18576],{},[149,18769,18770,18776,18782],{},[152,18771,18772,18583,18774],{},[17,18773,18525],{},[17,18775,396],{},[152,18777,18778,18590,18780],{},[17,18779,18547],{},[17,18781,396],{},[152,18783,18784,18597,18786],{},[17,18785,18568],{},[17,18787,396],{},[14,18789,18790],{},"For positive numbers, “down” means moving to a smaller number.",[14,18792,18793,18794,89],{},"For negative numbers, “down” means moving to a smaller number too, so it goes ",[35,18795,18796],{},"farther from zero",[45,18798,18800],{"className":47,"code":18799,"language":49,"meta":50,"style":50},"import math\n\nprint(math.floor(-2.7))  # -3\nprint(math.floor(-2.1))  # -3\n",[17,18801,18802,18808,18812,18834],{"__ignoreMap":50},[54,18803,18804,18806],{"class":56,"line":57},[54,18805,61],{"class":60},[54,18807,18340],{"class":64},[54,18809,18810],{"class":56,"line":68},[54,18811,72],{"emptyLinePlaceholder":71},[54,18813,18814,18816,18818,18820,18822,18824,18826,18828,18830,18832],{"class":56,"line":75},[54,18815,116],{"class":115},[54,18817,96],{"class":88},[54,18819,18295],{"class":92},[54,18821,89],{"class":88},[54,18823,18380],{"class":92},[54,18825,96],{"class":88},[54,18827,1467],{"class":81},[54,18829,18643],{"class":99},[54,18831,4310],{"class":88},[54,18833,18413],{"class":4313},[54,18835,18836,18838,18840,18842,18844,18846,18848,18850,18852,18854],{"class":56,"line":112},[54,18837,116],{"class":115},[54,18839,96],{"class":88},[54,18841,18295],{"class":92},[54,18843,89],{"class":88},[54,18845,18380],{"class":92},[54,18847,96],{"class":88},[54,18849,1467],{"class":81},[54,18851,18525],{"class":99},[54,18853,4310],{"class":88},[54,18855,18413],{"class":4313},[40,18857,18859],{"id":18858},"why-negative-numbers-confuse-beginners","Why negative numbers confuse beginners",[14,18861,18862],{},"Negative numbers are where most confusion happens.",[14,18864,18865],{},"Many beginners think:",[149,18867,18868,18874],{},[152,18869,18870,18873],{},[17,18871,18872],{},"ceil()"," just removes the decimal part",[152,18875,18876,18873],{},[17,18877,18878],{},"floor()",[14,18880,18881],{},"That is not how these functions work.",[14,18883,18884],{},"They are based on direction on the number line:",[149,18886,18887,18893],{},[152,18888,18889,18892],{},[35,18890,18891],{},"Up"," means greater",[152,18894,18895,18898],{},[35,18896,18897],{},"Down"," means smaller",[14,18900,18901,18902,147],{},"So for ",[17,18903,18904],{},"-2.7",[149,18906,18907,18915],{},[152,18908,18909,337,18912],{},[17,18910,18911],{},"math.ceil(-2.7)",[17,18913,18914],{},"-2",[152,18916,18917,337,18920],{},[17,18918,18919],{},"math.floor(-2.7)",[17,18921,18922],{},"-3",[14,18924,18925],{},"Why?",[149,18927,18928,18935],{},[152,18929,18930,18932,18933],{},[17,18931,18914],{}," is greater than ",[17,18934,18904],{},[152,18936,18937,18939,18940],{},[17,18938,18922],{}," is smaller than ",[17,18941,18904],{},[14,18943,18944],{},"Think of the number line like this:",[45,18946,18948],{"className":47,"code":18947,"language":49,"meta":50,"style":50},"# -3   -2.7   -2\n",[17,18949,18950],{"__ignoreMap":50},[54,18951,18952],{"class":56,"line":57},[54,18953,18947],{"class":4313},[149,18955,18956,18963],{},[152,18957,18958,18959,18962],{},"Ceiling goes to the next integer ",[35,18960,18961],{},"above"," the number",[152,18964,18965,18966,18962],{},"Floor goes to the next integer ",[35,18967,18968],{},"below",[40,18970,18972],{"id":18971},"mathceil-vs-mathfloor-vs-round","math.ceil() vs math.floor() vs round()",[14,18974,18975],{},"These three are often confused, but they do different jobs.",[602,18977,18979],{"id":18978},"mathceil",[17,18980,18265],{},[14,18982,18983],{},"Always rounds upward.",[45,18985,18987],{"className":47,"code":18986,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(4.2))   # 5\nprint(math.ceil(4.8))   # 5\n",[17,18988,18989,18995,18999,19021],{"__ignoreMap":50},[54,18990,18991,18993],{"class":56,"line":57},[54,18992,61],{"class":60},[54,18994,18340],{"class":64},[54,18996,18997],{"class":56,"line":68},[54,18998,72],{"emptyLinePlaceholder":71},[54,19000,19001,19003,19005,19007,19009,19011,19013,19016,19018],{"class":56,"line":75},[54,19002,116],{"class":115},[54,19004,96],{"class":88},[54,19006,18295],{"class":92},[54,19008,89],{"class":88},[54,19010,18357],{"class":92},[54,19012,96],{"class":88},[54,19014,19015],{"class":99},"4.2",[54,19017,4310],{"class":88},[54,19019,19020],{"class":4313},"   # 5\n",[54,19022,19023,19025,19027,19029,19031,19033,19035,19038,19040],{"class":56,"line":112},[54,19024,116],{"class":115},[54,19026,96],{"class":88},[54,19028,18295],{"class":92},[54,19030,89],{"class":88},[54,19032,18357],{"class":92},[54,19034,96],{"class":88},[54,19036,19037],{"class":99},"4.8",[54,19039,4310],{"class":88},[54,19041,19020],{"class":4313},[602,19043,19045],{"id":19044},"mathfloor",[17,19046,18268],{},[14,19048,19049],{},"Always rounds downward.",[45,19051,19053],{"className":47,"code":19052,"language":49,"meta":50,"style":50},"import math\n\nprint(math.floor(4.2))  # 4\nprint(math.floor(4.8))  # 4\n",[17,19054,19055,19061,19065,19086],{"__ignoreMap":50},[54,19056,19057,19059],{"class":56,"line":57},[54,19058,61],{"class":60},[54,19060,18340],{"class":64},[54,19062,19063],{"class":56,"line":68},[54,19064,72],{"emptyLinePlaceholder":71},[54,19066,19067,19069,19071,19073,19075,19077,19079,19081,19083],{"class":56,"line":75},[54,19068,116],{"class":115},[54,19070,96],{"class":88},[54,19072,18295],{"class":92},[54,19074,89],{"class":88},[54,19076,18380],{"class":92},[54,19078,96],{"class":88},[54,19080,19015],{"class":99},[54,19082,4310],{"class":88},[54,19084,19085],{"class":4313},"  # 4\n",[54,19087,19088,19090,19092,19094,19096,19098,19100,19102,19104],{"class":56,"line":112},[54,19089,116],{"class":115},[54,19091,96],{"class":88},[54,19093,18295],{"class":92},[54,19095,89],{"class":88},[54,19097,18380],{"class":92},[54,19099,96],{"class":88},[54,19101,19037],{"class":99},[54,19103,4310],{"class":88},[54,19105,19085],{"class":4313},[602,19107,19109],{"id":19108},"round",[17,19110,18305],{},[14,19112,19113],{},"Rounds to the nearest value instead.",[45,19115,19117],{"className":47,"code":19116,"language":49,"meta":50,"style":50},"print(round(4.2))  # 4\nprint(round(4.8))  # 5\n",[17,19118,19119,19135],{"__ignoreMap":50},[54,19120,19121,19123,19125,19127,19129,19131,19133],{"class":56,"line":57},[54,19122,116],{"class":115},[54,19124,96],{"class":88},[54,19126,19108],{"class":115},[54,19128,96],{"class":88},[54,19130,19015],{"class":99},[54,19132,4310],{"class":88},[54,19134,19085],{"class":4313},[54,19136,19137,19139,19141,19143,19145,19147,19149],{"class":56,"line":68},[54,19138,116],{"class":115},[54,19140,96],{"class":88},[54,19142,19108],{"class":115},[54,19144,96],{"class":88},[54,19146,19037],{"class":99},[54,19148,4310],{"class":88},[54,19150,19151],{"class":4313},"  # 5\n",[14,19153,19154],{},"So:",[149,19156,19157,19165,19172],{},[152,19158,19159,19162,19163],{},[17,19160,19161],{},"math.ceil(4.2)"," → ",[17,19164,285],{},[152,19166,19167,19162,19170],{},[17,19168,19169],{},"math.floor(4.2)",[17,19171,2389],{},[152,19173,19174,19162,19177],{},[17,19175,19176],{},"round(4.2)",[17,19178,2389],{},[14,19180,19181,19183,19184,19186],{},[17,19182,18305],{}," does ",[35,19185,3036],{}," mean “always round up.”",[14,19188,19189,19190,89],{},"If you want a full explanation of standard rounding rules, see ",[192,19191,19192,2834],{"href":18323},[17,19193,18305],{},[40,19195,19197],{"id":19196},"basic-examples-to-include","Basic examples to include",[14,19199,19200],{},"Here are simple examples that show how both functions behave with different kinds of numbers.",[602,19202,19204],{"id":19203},"positive-floats","Positive floats",[45,19206,19208],{"className":47,"code":19207,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(3.2))   # 4\nprint(math.floor(3.2))  # 3\n",[17,19209,19210,19216,19220,19240],{"__ignoreMap":50},[54,19211,19212,19214],{"class":56,"line":57},[54,19213,61],{"class":60},[54,19215,18340],{"class":64},[54,19217,19218],{"class":56,"line":68},[54,19219,72],{"emptyLinePlaceholder":71},[54,19221,19222,19224,19226,19228,19230,19232,19234,19236,19238],{"class":56,"line":75},[54,19223,116],{"class":115},[54,19225,96],{"class":88},[54,19227,18295],{"class":92},[54,19229,89],{"class":88},[54,19231,18357],{"class":92},[54,19233,96],{"class":88},[54,19235,18362],{"class":99},[54,19237,4310],{"class":88},[54,19239,18367],{"class":4313},[54,19241,19242,19244,19246,19248,19250,19252,19254,19256,19258],{"class":56,"line":112},[54,19243,116],{"class":115},[54,19245,96],{"class":88},[54,19247,18295],{"class":92},[54,19249,89],{"class":88},[54,19251,18380],{"class":92},[54,19253,96],{"class":88},[54,19255,18362],{"class":99},[54,19257,4310],{"class":88},[54,19259,18390],{"class":4313},[602,19261,19263],{"id":19262},"negative-floats","Negative floats",[45,19265,19267],{"className":47,"code":19266,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(-3.2))   # -3\nprint(math.floor(-3.2))  # -4\n",[17,19268,19269,19275,19279,19302],{"__ignoreMap":50},[54,19270,19271,19273],{"class":56,"line":57},[54,19272,61],{"class":60},[54,19274,18340],{"class":64},[54,19276,19277],{"class":56,"line":68},[54,19278,72],{"emptyLinePlaceholder":71},[54,19280,19281,19283,19285,19287,19289,19291,19293,19295,19297,19299],{"class":56,"line":75},[54,19282,116],{"class":115},[54,19284,96],{"class":88},[54,19286,18295],{"class":92},[54,19288,89],{"class":88},[54,19290,18357],{"class":92},[54,19292,96],{"class":88},[54,19294,1467],{"class":81},[54,19296,18362],{"class":99},[54,19298,4310],{"class":88},[54,19300,19301],{"class":4313},"   # -3\n",[54,19303,19304,19306,19308,19310,19312,19314,19316,19318,19320,19322],{"class":56,"line":112},[54,19305,116],{"class":115},[54,19307,96],{"class":88},[54,19309,18295],{"class":92},[54,19311,89],{"class":88},[54,19313,18380],{"class":92},[54,19315,96],{"class":88},[54,19317,1467],{"class":81},[54,19319,18362],{"class":99},[54,19321,4310],{"class":88},[54,19323,19324],{"class":4313},"  # -4\n",[602,19326,19328],{"id":19327},"whole-numbers","Whole numbers",[45,19330,19332],{"className":47,"code":19331,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(5))    # 5\nprint(math.floor(5))   # 5\n",[17,19333,19334,19340,19344,19365],{"__ignoreMap":50},[54,19335,19336,19338],{"class":56,"line":57},[54,19337,61],{"class":60},[54,19339,18340],{"class":64},[54,19341,19342],{"class":56,"line":68},[54,19343,72],{"emptyLinePlaceholder":71},[54,19345,19346,19348,19350,19352,19354,19356,19358,19360,19362],{"class":56,"line":75},[54,19347,116],{"class":115},[54,19349,96],{"class":88},[54,19351,18295],{"class":92},[54,19353,89],{"class":88},[54,19355,18357],{"class":92},[54,19357,96],{"class":88},[54,19359,285],{"class":99},[54,19361,4310],{"class":88},[54,19363,19364],{"class":4313},"    # 5\n",[54,19366,19367,19369,19371,19373,19375,19377,19379,19381,19383],{"class":56,"line":112},[54,19368,116],{"class":115},[54,19370,96],{"class":88},[54,19372,18295],{"class":92},[54,19374,89],{"class":88},[54,19376,18380],{"class":92},[54,19378,96],{"class":88},[54,19380,285],{"class":99},[54,19382,4310],{"class":88},[54,19384,19020],{"class":4313},[14,19386,19387],{},"If the number already has no decimal part, both functions return the same whole number.",[602,19389,19391],{"id":19390},"real-use-case-pages-needed","Real use case: pages needed",[14,19393,19394],{},"Suppose each page can hold 10 items, and you have 23 items.",[14,19396,19397],{},"You need enough pages to fit everything, even the last partial page.",[45,19399,19401],{"className":47,"code":19400,"language":49,"meta":50,"style":50},"import math\n\nitems = 23\nitems_per_page = 10\n\npages_needed = math.ceil(items \u002F items_per_page)\nprint(pages_needed)  # 3\n",[17,19402,19403,19409,19413,19422,19432,19436,19461],{"__ignoreMap":50},[54,19404,19405,19407],{"class":56,"line":57},[54,19406,61],{"class":60},[54,19408,18340],{"class":64},[54,19410,19411],{"class":56,"line":68},[54,19412,72],{"emptyLinePlaceholder":71},[54,19414,19415,19417,19419],{"class":56,"line":75},[54,19416,17899],{"class":64},[54,19418,82],{"class":81},[54,19420,19421],{"class":99}," 23\n",[54,19423,19424,19427,19429],{"class":56,"line":112},[54,19425,19426],{"class":64},"items_per_page ",[54,19428,82],{"class":81},[54,19430,19431],{"class":99}," 10\n",[54,19433,19434],{"class":56,"line":887},[54,19435,72],{"emptyLinePlaceholder":71},[54,19437,19438,19441,19443,19446,19448,19450,19452,19454,19456,19459],{"class":56,"line":892},[54,19439,19440],{"class":64},"pages_needed ",[54,19442,82],{"class":81},[54,19444,19445],{"class":64}," math",[54,19447,89],{"class":88},[54,19449,18357],{"class":92},[54,19451,96],{"class":88},[54,19453,17899],{"class":92},[54,19455,6676],{"class":81},[54,19457,19458],{"class":92}," items_per_page",[54,19460,109],{"class":88},[54,19462,19463,19465,19467,19470,19472],{"class":56,"line":913},[54,19464,116],{"class":115},[54,19466,96],{"class":88},[54,19468,19469],{"class":92},"pages_needed",[54,19471,876],{"class":88},[54,19473,18390],{"class":4313},[14,19475,19476,19477,19479],{},"Why use ",[17,19478,18265],{}," here?",[149,19481,19482,19490,19496],{},[152,19483,19484,337,19487],{},[17,19485,19486],{},"23 \u002F 10",[17,19488,19489],{},"2.3",[152,19491,19492,19493,19495],{},"You cannot use ",[17,19494,19489],{}," pages",[152,19497,19498,19499,19501],{},"You need ",[17,19500,401],{}," full pages to hold all items",[602,19503,19505],{"id":19504},"real-use-case-full-groups-only","Real use case: full groups only",[14,19507,19508],{},"Now imagine you only want to count complete groups of 10.",[45,19510,19512],{"className":47,"code":19511,"language":49,"meta":50,"style":50},"import math\n\nstudents = 23\ngroup_size = 10\n\nfull_groups = math.floor(students \u002F group_size)\nprint(full_groups)  # 2\n",[17,19513,19514,19520,19524,19533,19542,19546,19570],{"__ignoreMap":50},[54,19515,19516,19518],{"class":56,"line":57},[54,19517,61],{"class":60},[54,19519,18340],{"class":64},[54,19521,19522],{"class":56,"line":68},[54,19523,72],{"emptyLinePlaceholder":71},[54,19525,19526,19529,19531],{"class":56,"line":75},[54,19527,19528],{"class":64},"students ",[54,19530,82],{"class":81},[54,19532,19421],{"class":99},[54,19534,19535,19538,19540],{"class":56,"line":112},[54,19536,19537],{"class":64},"group_size ",[54,19539,82],{"class":81},[54,19541,19431],{"class":99},[54,19543,19544],{"class":56,"line":887},[54,19545,72],{"emptyLinePlaceholder":71},[54,19547,19548,19551,19553,19555,19557,19559,19561,19563,19565,19568],{"class":56,"line":892},[54,19549,19550],{"class":64},"full_groups ",[54,19552,82],{"class":81},[54,19554,19445],{"class":64},[54,19556,89],{"class":88},[54,19558,18380],{"class":92},[54,19560,96],{"class":88},[54,19562,19528],{"class":92},[54,19564,6676],{"class":81},[54,19566,19567],{"class":92}," group_size",[54,19569,109],{"class":88},[54,19571,19572,19574,19576,19579,19581],{"class":56,"line":913},[54,19573,116],{"class":115},[54,19575,96],{"class":88},[54,19577,19578],{"class":92},"full_groups",[54,19580,876],{"class":88},[54,19582,19583],{"class":4313},"  # 2\n",[14,19585,19476,19586,19479],{},[17,19587,18268],{},[149,19589,19590,19596],{},[152,19591,19592,337,19594],{},[17,19593,19486],{},[17,19595,19489],{},[152,19597,19598,19599,19601],{},"Only ",[17,19600,396],{}," groups are completely full",[40,19603,19605],{"id":19604},"import-requirement","Import requirement",[14,19607,19608,134,19610,19612,19613,19615],{},[17,19609,18265],{},[17,19611,18268],{}," are ",[35,19614,3036],{}," built-in functions.",[14,19617,19618,19619,19621],{},"You must import the ",[17,19620,18295],{}," module first:",[45,19623,19625],{"className":47,"code":19624,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(3.4))\nprint(math.floor(3.4))\n",[17,19626,19627,19633,19637,19656],{"__ignoreMap":50},[54,19628,19629,19631],{"class":56,"line":57},[54,19630,61],{"class":60},[54,19632,18340],{"class":64},[54,19634,19635],{"class":56,"line":68},[54,19636,72],{"emptyLinePlaceholder":71},[54,19638,19639,19641,19643,19645,19647,19649,19651,19654],{"class":56,"line":75},[54,19640,116],{"class":115},[54,19642,96],{"class":88},[54,19644,18295],{"class":92},[54,19646,89],{"class":88},[54,19648,18357],{"class":92},[54,19650,96],{"class":88},[54,19652,19653],{"class":99},"3.4",[54,19655,382],{"class":88},[54,19657,19658,19660,19662,19664,19666,19668,19670,19672],{"class":56,"line":112},[54,19659,116],{"class":115},[54,19661,96],{"class":88},[54,19663,18295],{"class":92},[54,19665,89],{"class":88},[54,19667,18380],{"class":92},[54,19669,96],{"class":88},[54,19671,19653],{"class":99},[54,19673,382],{"class":88},[14,19675,19676],{},"If you forget the import, Python will raise an error.",[45,19678,19680],{"className":47,"code":19679,"language":49,"meta":50,"style":50},"print(math.ceil(3.4))\n",[17,19681,19682],{"__ignoreMap":50},[54,19683,19684,19686,19688,19690,19692,19694,19696,19698],{"class":56,"line":57},[54,19685,116],{"class":115},[54,19687,96],{"class":88},[54,19689,18295],{"class":92},[54,19691,89],{"class":88},[54,19693,18357],{"class":92},[54,19695,96],{"class":88},[54,19697,19653],{"class":99},[54,19699,382],{"class":88},[14,19701,985,19702,19704,19705,19707,19708,89],{},[17,19703,988],{}," because ",[17,19706,18295],{}," has not been defined yet. If that happens, see ",[192,19709,1084,19711],{"href":19710},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix\u002F",[17,19712,1087],{},[40,19714,19716],{"id":19715},"when-to-use-each-function","When to use each function",[14,19718,19719],{},"Choose the function based on what the result means in real life.",[602,19721,126,19723,19725],{"id":19722},"use-mathceil-when-partial-values-still-need-a-full-unit",[17,19724,18265],{}," when partial values still need a full unit",[14,19727,19728],{},"Common examples:",[149,19730,19731,19734,19737,19740],{},[152,19732,19733],{},"boxes needed for items",[152,19735,19736],{},"pages needed for results",[152,19738,19739],{},"buses needed for passengers",[152,19741,19742],{},"batches needed to finish work",[14,19744,253],{},[45,19746,19748],{"className":47,"code":19747,"language":49,"meta":50,"style":50},"import math\n\npassengers = 41\nseats_per_bus = 20\n\nbuses_needed = math.ceil(passengers \u002F seats_per_bus)\nprint(buses_needed)  # 3\n",[17,19749,19750,19756,19760,19770,19780,19784,19808],{"__ignoreMap":50},[54,19751,19752,19754],{"class":56,"line":57},[54,19753,61],{"class":60},[54,19755,18340],{"class":64},[54,19757,19758],{"class":56,"line":68},[54,19759,72],{"emptyLinePlaceholder":71},[54,19761,19762,19765,19767],{"class":56,"line":75},[54,19763,19764],{"class":64},"passengers ",[54,19766,82],{"class":81},[54,19768,19769],{"class":99}," 41\n",[54,19771,19772,19775,19777],{"class":56,"line":112},[54,19773,19774],{"class":64},"seats_per_bus ",[54,19776,82],{"class":81},[54,19778,19779],{"class":99}," 20\n",[54,19781,19782],{"class":56,"line":887},[54,19783,72],{"emptyLinePlaceholder":71},[54,19785,19786,19789,19791,19793,19795,19797,19799,19801,19803,19806],{"class":56,"line":892},[54,19787,19788],{"class":64},"buses_needed ",[54,19790,82],{"class":81},[54,19792,19445],{"class":64},[54,19794,89],{"class":88},[54,19796,18357],{"class":92},[54,19798,96],{"class":88},[54,19800,19764],{"class":92},[54,19802,6676],{"class":81},[54,19804,19805],{"class":92}," seats_per_bus",[54,19807,109],{"class":88},[54,19809,19810,19812,19814,19817,19819],{"class":56,"line":913},[54,19811,116],{"class":115},[54,19813,96],{"class":88},[54,19815,19816],{"class":92},"buses_needed",[54,19818,876],{"class":88},[54,19820,18390],{"class":4313},[14,19822,19823,19824,337,19827,19830],{},"Even though ",[17,19825,19826],{},"41 \u002F 20",[17,19828,19829],{},"2.05",", you still need 3 buses.",[602,19832,126,19834,19836],{"id":19833},"use-mathfloor-when-only-complete-units-count",[17,19835,18268],{}," when only complete units count",[14,19838,19728],{},[149,19840,19841,19844,19847,19850],{},[152,19842,19843],{},"full rows completed",[152,19845,19846],{},"completed groups",[152,19848,19849],{},"whole dollars below a price",[152,19851,19852],{},"full containers filled",[14,19854,253],{},[45,19856,19858],{"className":47,"code":19857,"language":49,"meta":50,"style":50},"import math\n\ncookies = 47\ncookies_per_box = 12\n\nfull_boxes = math.floor(cookies \u002F cookies_per_box)\nprint(full_boxes)  # 3\n",[17,19859,19860,19866,19870,19880,19890,19894,19918],{"__ignoreMap":50},[54,19861,19862,19864],{"class":56,"line":57},[54,19863,61],{"class":60},[54,19865,18340],{"class":64},[54,19867,19868],{"class":56,"line":68},[54,19869,72],{"emptyLinePlaceholder":71},[54,19871,19872,19875,19877],{"class":56,"line":75},[54,19873,19874],{"class":64},"cookies ",[54,19876,82],{"class":81},[54,19878,19879],{"class":99}," 47\n",[54,19881,19882,19885,19887],{"class":56,"line":112},[54,19883,19884],{"class":64},"cookies_per_box ",[54,19886,82],{"class":81},[54,19888,19889],{"class":99}," 12\n",[54,19891,19892],{"class":56,"line":887},[54,19893,72],{"emptyLinePlaceholder":71},[54,19895,19896,19899,19901,19903,19905,19907,19909,19911,19913,19916],{"class":56,"line":892},[54,19897,19898],{"class":64},"full_boxes ",[54,19900,82],{"class":81},[54,19902,19445],{"class":64},[54,19904,89],{"class":88},[54,19906,18380],{"class":92},[54,19908,96],{"class":88},[54,19910,19874],{"class":92},[54,19912,6676],{"class":81},[54,19914,19915],{"class":92}," cookies_per_box",[54,19917,109],{"class":88},[54,19919,19920,19922,19924,19927,19929],{"class":56,"line":913},[54,19921,116],{"class":115},[54,19923,96],{"class":88},[54,19925,19926],{"class":92},"full_boxes",[54,19928,876],{"class":88},[54,19930,18390],{"class":4313},[14,19932,19933],{},"Only 3 boxes can be completely filled.",[40,19935,978],{"id":977},[14,19937,19938],{},"Here are the most common problems beginners run into.",[602,19940,17008,19942],{"id":19941},"forgetting-to-import-math",[17,19943,18295],{},[14,19945,991],{},[45,19947,19949],{"className":47,"code":19948,"language":49,"meta":50,"style":50},"print(math.ceil(4.1))\n",[17,19950,19951],{"__ignoreMap":50},[54,19952,19953,19955,19957,19959,19961,19963,19965,19968],{"class":56,"line":57},[54,19954,116],{"class":115},[54,19956,96],{"class":88},[54,19958,18295],{"class":92},[54,19960,89],{"class":88},[54,19962,18357],{"class":92},[54,19964,96],{"class":88},[54,19966,19967],{"class":99},"4.1",[54,19969,382],{"class":88},[14,19971,13808],{},[45,19973,19975],{"className":47,"code":19974,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(4.1))\n",[17,19976,19977,19983,19987],{"__ignoreMap":50},[54,19978,19979,19981],{"class":56,"line":57},[54,19980,61],{"class":60},[54,19982,18340],{"class":64},[54,19984,19985],{"class":56,"line":68},[54,19986,72],{"emptyLinePlaceholder":71},[54,19988,19989,19991,19993,19995,19997,19999,20001,20003],{"class":56,"line":75},[54,19990,116],{"class":115},[54,19992,96],{"class":88},[54,19994,18295],{"class":92},[54,19996,89],{"class":88},[54,19998,18357],{"class":92},[54,20000,96],{"class":88},[54,20002,19967],{"class":99},[54,20004,382],{"class":88},[602,20006,5949,20008,20010],{"id":20007},"expecting-ceil-to-just-remove-decimals",[17,20009,18872],{}," to just remove decimals",[14,20012,20013,20016,20017,89],{},[17,20014,20015],{},"math.ceil(4.1)"," is not ",[17,20018,2389],{},[14,20020,20021],{},"It is:",[45,20023,20025],{"className":47,"code":20024,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(4.1))  # 5\n",[17,20026,20027,20033,20037],{"__ignoreMap":50},[54,20028,20029,20031],{"class":56,"line":57},[54,20030,61],{"class":60},[54,20032,18340],{"class":64},[54,20034,20035],{"class":56,"line":68},[54,20036,72],{"emptyLinePlaceholder":71},[54,20038,20039,20041,20043,20045,20047,20049,20051,20053,20055],{"class":56,"line":75},[54,20040,116],{"class":115},[54,20042,96],{"class":88},[54,20044,18295],{"class":92},[54,20046,89],{"class":88},[54,20048,18357],{"class":92},[54,20050,96],{"class":88},[54,20052,19967],{"class":99},[54,20054,4310],{"class":88},[54,20056,19151],{"class":4313},[14,20058,20059,20060,89],{},"If you want to remove the decimal part instead of rounding up or down, you may want ",[192,20061,20063,2834],{"href":20062},"\u002Freference\u002Fpython-int-function-explained\u002F",[17,20064,20065],{},"int()",[602,20067,5949,20069,20071],{"id":20068},"expecting-floor-to-behave-the-same-for-negative-numbers",[17,20070,18878],{}," to behave the same for negative numbers",[14,20073,20074],{},"This surprises many beginners:",[45,20076,20078],{"className":47,"code":20077,"language":49,"meta":50,"style":50},"import math\n\nprint(math.floor(-4.1))  # -5\n",[17,20079,20080,20086,20090],{"__ignoreMap":50},[54,20081,20082,20084],{"class":56,"line":57},[54,20083,61],{"class":60},[54,20085,18340],{"class":64},[54,20087,20088],{"class":56,"line":68},[54,20089,72],{"emptyLinePlaceholder":71},[54,20091,20092,20094,20096,20098,20100,20102,20104,20106,20108,20110],{"class":56,"line":75},[54,20093,116],{"class":115},[54,20095,96],{"class":88},[54,20097,18295],{"class":92},[54,20099,89],{"class":88},[54,20101,18380],{"class":92},[54,20103,96],{"class":88},[54,20105,1467],{"class":81},[54,20107,19967],{"class":99},[54,20109,4310],{"class":88},[54,20111,20112],{"class":4313},"  # -5\n",[14,20114,20115,20116,20119],{},"That happens because ",[17,20117,20118],{},"-5"," is the next lower integer.",[602,20121,20123,20124,5484,20126],{"id":20122},"confusing-mathfloor-with-round","Confusing ",[17,20125,18268],{},[17,20127,18305],{},[14,20129,20130],{},"These are not the same:",[45,20132,20134],{"className":47,"code":20133,"language":49,"meta":50,"style":50},"import math\n\nprint(math.floor(4.9))  # 4\nprint(round(4.9))       # 5\n",[17,20135,20136,20142,20146,20167],{"__ignoreMap":50},[54,20137,20138,20140],{"class":56,"line":57},[54,20139,61],{"class":60},[54,20141,18340],{"class":64},[54,20143,20144],{"class":56,"line":68},[54,20145,72],{"emptyLinePlaceholder":71},[54,20147,20148,20150,20152,20154,20156,20158,20160,20163,20165],{"class":56,"line":75},[54,20149,116],{"class":115},[54,20151,96],{"class":88},[54,20153,18295],{"class":92},[54,20155,89],{"class":88},[54,20157,18380],{"class":92},[54,20159,96],{"class":88},[54,20161,20162],{"class":99},"4.9",[54,20164,4310],{"class":88},[54,20166,19085],{"class":4313},[54,20168,20169,20171,20173,20175,20177,20179,20181],{"class":56,"line":112},[54,20170,116],{"class":115},[54,20172,96],{"class":88},[54,20174,19108],{"class":115},[54,20176,96],{"class":88},[54,20178,20162],{"class":99},[54,20180,4310],{"class":88},[54,20182,20183],{"class":4313},"       # 5\n",[14,20185,20186,20188,20189,20191],{},[17,20187,18878],{}," always goes down. ",[17,20190,18305],{}," goes to the nearest value.",[602,20193,20195],{"id":20194},"using-string-input-without-converting-it-first","Using string input without converting it first",[14,20197,20198,20199,20202],{},"If your number comes from ",[17,20200,20201],{},"input()",", it starts as text.",[45,20204,20206],{"className":47,"code":20205,"language":49,"meta":50,"style":50},"user_value = input(\"Enter a number: \")\nprint(type(user_value))\n",[17,20207,20208,20229],{"__ignoreMap":50},[54,20209,20210,20213,20215,20218,20220,20222,20225,20227],{"class":56,"line":57},[54,20211,20212],{"class":64},"user_value ",[54,20214,82],{"class":81},[54,20216,20217],{"class":115}," input",[54,20219,96],{"class":88},[54,20221,545],{"class":544},[54,20223,20224],{"class":548},"Enter a number: ",[54,20226,545],{"class":544},[54,20228,109],{"class":88},[54,20230,20231,20233,20235,20237,20239,20242],{"class":56,"line":68},[54,20232,116],{"class":115},[54,20234,96],{"class":88},[54,20236,2208],{"class":429},[54,20238,96],{"class":88},[54,20240,20241],{"class":92},"user_value",[54,20243,382],{"class":88},[14,20245,20246],{},"To use it with math functions, convert it first:",[45,20248,20250],{"className":47,"code":20249,"language":49,"meta":50,"style":50},"import math\n\nuser_value = input(\"Enter a number: \")\nnumber = float(user_value)\n\nprint(math.ceil(number))\n",[17,20251,20252,20258,20262,20280,20295,20299],{"__ignoreMap":50},[54,20253,20254,20256],{"class":56,"line":57},[54,20255,61],{"class":60},[54,20257,18340],{"class":64},[54,20259,20260],{"class":56,"line":68},[54,20261,72],{"emptyLinePlaceholder":71},[54,20263,20264,20266,20268,20270,20272,20274,20276,20278],{"class":56,"line":75},[54,20265,20212],{"class":64},[54,20267,82],{"class":81},[54,20269,20217],{"class":115},[54,20271,96],{"class":88},[54,20273,545],{"class":544},[54,20275,20224],{"class":548},[54,20277,545],{"class":544},[54,20279,109],{"class":88},[54,20281,20282,20284,20286,20289,20291,20293],{"class":56,"line":112},[54,20283,78],{"class":64},[54,20285,82],{"class":81},[54,20287,20288],{"class":429}," float",[54,20290,96],{"class":88},[54,20292,20241],{"class":92},[54,20294,109],{"class":88},[54,20296,20297],{"class":56,"line":887},[54,20298,72],{"emptyLinePlaceholder":71},[54,20300,20301,20303,20305,20307,20309,20311,20313,20315],{"class":56,"line":892},[54,20302,116],{"class":115},[54,20304,96],{"class":88},[54,20306,18295],{"class":92},[54,20308,89],{"class":88},[54,20310,18357],{"class":92},[54,20312,96],{"class":88},[54,20314,121],{"class":92},[54,20316,382],{"class":88},[14,20318,20319,20320,89],{},"If needed, see ",[192,20321,20323],{"href":20322},"\u002Fhow-to\u002Fhow-to-convert-string-to-float-in-python\u002F","how to convert a string to a float in Python",[40,20325,1419],{"id":1418},[602,20327,20329],{"id":20328},"what-is-the-difference-between-mathceil-and-mathfloor","What is the difference between math.ceil() and math.floor()?",[14,20331,20332,20334,20335,20337],{},[17,20333,18265],{}," rounds up to the next integer. ",[17,20336,18268],{}," rounds down to the previous integer.",[602,20339,20341],{"id":20340},"do-mathceil-and-mathfloor-return-int-values","Do math.ceil() and math.floor() return int values?",[14,20343,20344],{},"Yes. In normal use, both return integer results.",[602,20346,20348],{"id":20347},"why-does-mathfloor-23-return-3","Why does math.floor(-2.3) return -3?",[14,20350,20351,20352,20354,20355,20358,20359,20361],{},"Because floor goes to the greatest integer less than or equal to the value. ",[17,20353,18922],{}," is less than ",[17,20356,20357],{},"-2.3",", while ",[17,20360,18914],{}," is greater than it.",[602,20363,20365],{"id":20364},"do-i-need-to-import-math-to-use-ceil-and-floor","Do I need to import math to use ceil() and floor()?",[14,20367,20368,20369,20371,20372,20375],{},"Yes. They are part of the ",[17,20370,18295],{}," module, so use ",[17,20373,20374],{},"import math"," first.",[602,20377,20379],{"id":20378},"is-mathceil-the-same-as-round","Is math.ceil() the same as round()?",[14,20381,18143,20382,20384,20385,20387],{},[17,20383,18265],{}," always rounds upward. ",[17,20386,18305],{}," rounds to the nearest value.",[40,20389,1510],{"id":1509},[149,20391,20392,20397,20403,20409,20414],{},[152,20393,20394],{},[192,20395,20396],{"href":18292},"Python math module overview",[152,20398,20399],{},[192,20400,13760,20401,12898],{"href":18323},[17,20402,18305],{},[152,20404,20405],{},[192,20406,13760,20407,12898],{"href":20062},[17,20408,20065],{},[152,20410,20411],{},[192,20412,20413],{"href":20322},"How to convert a string to a float in Python",[152,20415,20416],{},[192,20417,1541,20418],{"href":19710},[17,20419,1087],{},[1545,20421,20422],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .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":50,"searchDepth":68,"depth":68,"links":20424},[20425,20426,20427,20430,20433,20434,20439,20446,20447,20453,20464,20471],{"id":3595,"depth":68,"text":3596},{"id":18447,"depth":68,"text":18448},{"id":18473,"depth":68,"text":18474,"children":20428},[20429],{"id":18492,"depth":75,"text":18493},{"id":18673,"depth":68,"text":18674,"children":20431},[20432],{"id":18690,"depth":75,"text":18493},{"id":18858,"depth":68,"text":18859},{"id":18971,"depth":68,"text":18972,"children":20435},[20436,20437,20438],{"id":18978,"depth":75,"text":18265},{"id":19044,"depth":75,"text":18268},{"id":19108,"depth":75,"text":18305},{"id":19196,"depth":68,"text":19197,"children":20440},[20441,20442,20443,20444,20445],{"id":19203,"depth":75,"text":19204},{"id":19262,"depth":75,"text":19263},{"id":19327,"depth":75,"text":19328},{"id":19390,"depth":75,"text":19391},{"id":19504,"depth":75,"text":19505},{"id":19604,"depth":68,"text":19605},{"id":19715,"depth":68,"text":19716,"children":20448},[20449,20451],{"id":19722,"depth":75,"text":20450},"Use math.ceil() when partial values still need a full unit",{"id":19833,"depth":75,"text":20452},"Use math.floor() when only complete units count",{"id":977,"depth":68,"text":978,"children":20454},[20455,20457,20459,20461,20463],{"id":19941,"depth":75,"text":20456},"Forgetting to import math",{"id":20007,"depth":75,"text":20458},"Expecting ceil() to just remove decimals",{"id":20068,"depth":75,"text":20460},"Expecting floor() to behave the same for negative numbers",{"id":20122,"depth":75,"text":20462},"Confusing math.floor() with round()",{"id":20194,"depth":75,"text":20195},{"id":1418,"depth":68,"text":1419,"children":20465},[20466,20467,20468,20469,20470],{"id":20328,"depth":75,"text":20329},{"id":20340,"depth":75,"text":20341},{"id":20347,"depth":75,"text":20348},{"id":20364,"depth":75,"text":20365},{"id":20378,"depth":75,"text":20379},{"id":1509,"depth":68,"text":1510},"Master math.ceil and math.floor explained in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fmath.ceil-and-math.floor-explained",{"title":18255,"description":20472},"standard-library\u002Fmath.ceil-and-math.floor-explained","-1ECieRNsHp50QVNT9PR0SS9ABiffj3x47p1x4Zd5Js",{"id":20479,"title":20480,"body":20481,"description":22267,"extension":1577,"meta":22268,"navigation":71,"path":22269,"seo":22270,"stem":22271,"__hash__":22272},"content\u002Fstandard-library\u002Fmath.pow-function-explained.md","math.pow() Function Explained",{"type":7,"value":20482,"toc":22227},[20483,20486,20492,20506,20508,20559,20573,20577,20582,20594,20597,20606,20609,20633,20635,20674,20676,20695,20701,20703,20705,20755,20757,20778,20784,20789,20856,20860,20864,20904,20910,20914,20954,20961,20965,21007,21021,21025,21066,21080,21084,21092,21096,21153,21157,21196,21200,21205,21210,21223,21227,21240,21249,21253,21258,21261,21274,21277,21290,21299,21303,21305,21313,21340,21345,21348,21385,21390,21397,21400,21431,21436,21439,21486,21489,21519,21523,21526,21574,21584,21587,21609,21613,21617,21662,21665,21703,21706,21785,21789,21797,21799,21838,21841,21845,21850,21856,21881,21887,21892,21900,21936,21942,21949,21952,22048,22052,22057,22129,22135,22137,22141,22144,22148,22159,22163,22176,22180,22185,22189,22197,22199,22224],[10,20484,20480],{"id":20485},"mathpow-function-explained",[14,20487,20488,20491],{},[17,20489,20490],{},"math.pow()"," raises one number to the power of another.",[14,20493,23,20494,27,20496,20498,20499,20501,20502,20505],{},[17,20495,18295],{},[17,20497,18295],{}," before using it. This function is useful when you are already working with other math functions, but beginners should also know that ",[17,20500,20490],{}," is different from the ",[17,20503,20504],{},"**"," operator.",[40,20507,43],{"id":42},[45,20509,20511],{"className":47,"code":20510,"language":49,"meta":50,"style":50},"import math\n\nresult = math.pow(2, 3)\nprint(result)  # 8.0\n",[17,20512,20513,20519,20523,20546],{"__ignoreMap":50},[54,20514,20515,20517],{"class":56,"line":57},[54,20516,61],{"class":60},[54,20518,18340],{"class":64},[54,20520,20521],{"class":56,"line":68},[54,20522,72],{"emptyLinePlaceholder":71},[54,20524,20525,20527,20529,20531,20533,20536,20538,20540,20542,20544],{"class":56,"line":75},[54,20526,3833],{"class":64},[54,20528,82],{"class":81},[54,20530,19445],{"class":64},[54,20532,89],{"class":88},[54,20534,20535],{"class":92},"pow",[54,20537,96],{"class":88},[54,20539,396],{"class":99},[54,20541,103],{"class":88},[54,20543,379],{"class":99},[54,20545,109],{"class":88},[54,20547,20548,20550,20552,20554,20556],{"class":56,"line":112},[54,20549,116],{"class":115},[54,20551,96],{"class":88},[54,20553,3866],{"class":92},[54,20555,876],{"class":88},[54,20557,20558],{"class":4313},"  # 8.0\n",[14,20560,20561,20564,20565,20568,20569,20572],{},[17,20562,20563],{},"math.pow(x, y)"," returns ",[17,20566,20567],{},"x"," raised to the power of ",[17,20570,20571],{},"y",". It always returns a float.",[40,20574,20576],{"id":20575},"what-mathpow-does","What math.pow() does",[14,20578,20579,20581],{},[17,20580,20490],{}," takes two numbers:",[149,20583,20584,20589],{},[152,20585,20586,20588],{},[17,20587,20567],{},": the base number",[152,20590,20591,20593],{},[17,20592,20571],{},": the exponent",[14,20595,20596],{},"It calculates:",[45,20598,20600],{"className":47,"code":20599,"language":49,"meta":50,"style":50},"x raised to the power of y\n",[17,20601,20602],{"__ignoreMap":50},[54,20603,20604],{"class":56,"line":57},[54,20605,20599],{"class":64},[14,20607,20608],{},"The syntax is:",[45,20610,20612],{"className":47,"code":20611,"language":49,"meta":50,"style":50},"math.pow(x, y)\n",[17,20613,20614],{"__ignoreMap":50},[54,20615,20616,20618,20620,20622,20624,20626,20628,20631],{"class":56,"line":57},[54,20617,18295],{"class":64},[54,20619,89],{"class":88},[54,20621,20535],{"class":92},[54,20623,96],{"class":88},[54,20625,20567],{"class":92},[54,20627,103],{"class":88},[54,20629,20630],{"class":92}," y",[54,20632,109],{"class":88},[14,20634,253],{},[45,20636,20638],{"className":47,"code":20637,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(2, 3))  # 8.0\n",[17,20639,20640,20646,20650],{"__ignoreMap":50},[54,20641,20642,20644],{"class":56,"line":57},[54,20643,61],{"class":60},[54,20645,18340],{"class":64},[54,20647,20648],{"class":56,"line":68},[54,20649,72],{"emptyLinePlaceholder":71},[54,20651,20652,20654,20656,20658,20660,20662,20664,20666,20668,20670,20672],{"class":56,"line":75},[54,20653,116],{"class":115},[54,20655,96],{"class":88},[54,20657,18295],{"class":92},[54,20659,89],{"class":88},[54,20661,20535],{"class":92},[54,20663,96],{"class":88},[54,20665,396],{"class":99},[54,20667,103],{"class":88},[54,20669,379],{"class":99},[54,20671,4310],{"class":88},[54,20673,20558],{"class":4313},[14,20675,4136],{},[149,20677,20678,20681,20686,20691],{},[152,20679,20680],{},"It raises one number to the power of another",[152,20682,20683,20684,160],{},"It is part of the ",[17,20685,18295],{},[152,20687,20688,20689,167],{},"You must write ",[17,20690,20374],{},[152,20692,3396,20693],{},[17,20694,9662],{},[14,20696,20697,20698,89],{},"If you want a broader introduction to math functions, see the ",[192,20699,20396],{"href":20700},"\u002Fstandard-library\u002Fpython-math-module-overview",[40,20702,199],{"id":198},[14,20704,11480],{},[45,20706,20708],{"className":47,"code":20707,"language":49,"meta":50,"style":50},"import math\n\nresult = math.pow(4, 2)\nprint(result)  # 16.0\n",[17,20709,20710,20716,20720,20742],{"__ignoreMap":50},[54,20711,20712,20714],{"class":56,"line":57},[54,20713,61],{"class":60},[54,20715,18340],{"class":64},[54,20717,20718],{"class":56,"line":68},[54,20719,72],{"emptyLinePlaceholder":71},[54,20721,20722,20724,20726,20728,20730,20732,20734,20736,20738,20740],{"class":56,"line":75},[54,20723,3833],{"class":64},[54,20725,82],{"class":81},[54,20727,19445],{"class":64},[54,20729,89],{"class":88},[54,20731,20535],{"class":92},[54,20733,96],{"class":88},[54,20735,2389],{"class":99},[54,20737,103],{"class":88},[54,20739,466],{"class":99},[54,20741,109],{"class":88},[54,20743,20744,20746,20748,20750,20752],{"class":56,"line":112},[54,20745,116],{"class":115},[54,20747,96],{"class":88},[54,20749,3866],{"class":92},[54,20751,876],{"class":88},[54,20753,20754],{"class":4313},"  # 16.0\n",[14,20756,229],{},[149,20758,20759,20764,20769],{},[152,20760,20761,20763],{},[17,20762,2389],{}," is the base",[152,20765,20766,20768],{},[17,20767,396],{}," is the exponent",[152,20770,20771,20772,11990,20775],{},"the result is ",[17,20773,20774],{},"16.0",[17,20776,20777],{},"16",[14,20779,20780,20781,20783],{},"Even when the answer looks like a whole number, ",[17,20782,20490],{}," still returns a float.",[14,20785,20786,20787,147],{},"You can confirm that with ",[17,20788,2249],{},[45,20790,20792],{"className":47,"code":20791,"language":49,"meta":50,"style":50},"import math\n\nresult = math.pow(4, 2)\nprint(result)         # 16.0\nprint(type(result))   # \u003Cclass 'float'>\n",[17,20793,20794,20800,20804,20826,20839],{"__ignoreMap":50},[54,20795,20796,20798],{"class":56,"line":57},[54,20797,61],{"class":60},[54,20799,18340],{"class":64},[54,20801,20802],{"class":56,"line":68},[54,20803,72],{"emptyLinePlaceholder":71},[54,20805,20806,20808,20810,20812,20814,20816,20818,20820,20822,20824],{"class":56,"line":75},[54,20807,3833],{"class":64},[54,20809,82],{"class":81},[54,20811,19445],{"class":64},[54,20813,89],{"class":88},[54,20815,20535],{"class":92},[54,20817,96],{"class":88},[54,20819,2389],{"class":99},[54,20821,103],{"class":88},[54,20823,466],{"class":99},[54,20825,109],{"class":88},[54,20827,20828,20830,20832,20834,20836],{"class":56,"line":112},[54,20829,116],{"class":115},[54,20831,96],{"class":88},[54,20833,3866],{"class":92},[54,20835,876],{"class":88},[54,20837,20838],{"class":4313},"         # 16.0\n",[54,20840,20841,20843,20845,20847,20849,20851,20853],{"class":56,"line":887},[54,20842,116],{"class":115},[54,20844,96],{"class":88},[54,20846,2208],{"class":429},[54,20848,96],{"class":88},[54,20850,3866],{"class":92},[54,20852,4310],{"class":88},[54,20854,20855],{"class":4313},"   # \u003Cclass 'float'>\n",[40,20857,20859],{"id":20858},"simple-examples","Simple examples",[602,20861,20863],{"id":20862},"positive-exponent","Positive exponent",[45,20865,20867],{"className":47,"code":20866,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(3, 2))  # 9.0\n",[17,20868,20869,20875,20879],{"__ignoreMap":50},[54,20870,20871,20873],{"class":56,"line":57},[54,20872,61],{"class":60},[54,20874,18340],{"class":64},[54,20876,20877],{"class":56,"line":68},[54,20878,72],{"emptyLinePlaceholder":71},[54,20880,20881,20883,20885,20887,20889,20891,20893,20895,20897,20899,20901],{"class":56,"line":75},[54,20882,116],{"class":115},[54,20884,96],{"class":88},[54,20886,18295],{"class":92},[54,20888,89],{"class":88},[54,20890,20535],{"class":92},[54,20892,96],{"class":88},[54,20894,401],{"class":99},[54,20896,103],{"class":88},[54,20898,466],{"class":99},[54,20900,4310],{"class":88},[54,20902,20903],{"class":4313},"  # 9.0\n",[14,20905,20906,20907,89],{},"This means ",[17,20908,20909],{},"3 × 3",[602,20911,20913],{"id":20912},"zero-exponent","Zero exponent",[45,20915,20917],{"className":47,"code":20916,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(5, 0))  # 1.0\n",[17,20918,20919,20925,20929],{"__ignoreMap":50},[54,20920,20921,20923],{"class":56,"line":57},[54,20922,61],{"class":60},[54,20924,18340],{"class":64},[54,20926,20927],{"class":56,"line":68},[54,20928,72],{"emptyLinePlaceholder":71},[54,20930,20931,20933,20935,20937,20939,20941,20943,20945,20947,20949,20951],{"class":56,"line":75},[54,20932,116],{"class":115},[54,20934,96],{"class":88},[54,20936,18295],{"class":92},[54,20938,89],{"class":88},[54,20940,20535],{"class":92},[54,20942,96],{"class":88},[54,20944,285],{"class":99},[54,20946,103],{"class":88},[54,20948,3986],{"class":99},[54,20950,4310],{"class":88},[54,20952,20953],{"class":4313},"  # 1.0\n",[14,20955,20956,20957,337,20959,89],{},"Any non-zero number raised to the power of ",[17,20958,752],{},[17,20960,100],{},[602,20962,20964],{"id":20963},"negative-exponent","Negative exponent",[45,20966,20968],{"className":47,"code":20967,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(2, -1))  # 0.5\n",[17,20969,20970,20976,20980],{"__ignoreMap":50},[54,20971,20972,20974],{"class":56,"line":57},[54,20973,61],{"class":60},[54,20975,18340],{"class":64},[54,20977,20978],{"class":56,"line":68},[54,20979,72],{"emptyLinePlaceholder":71},[54,20981,20982,20984,20986,20988,20990,20992,20994,20996,20998,21000,21002,21004],{"class":56,"line":75},[54,20983,116],{"class":115},[54,20985,96],{"class":88},[54,20987,18295],{"class":92},[54,20989,89],{"class":88},[54,20991,20535],{"class":92},[54,20993,96],{"class":88},[54,20995,396],{"class":99},[54,20997,103],{"class":88},[54,20999,879],{"class":81},[54,21001,100],{"class":99},[54,21003,4310],{"class":88},[54,21005,21006],{"class":4313},"  # 0.5\n",[14,21008,21009,21010,21013,21014,21017,21018,89],{},"A negative exponent means reciprocal power.",[21011,21012],"br",{},"\nSo ",[17,21015,21016],{},"2^-1"," means ",[17,21019,21020],{},"1 \u002F 2",[602,21022,21024],{"id":21023},"fractional-exponent","Fractional exponent",[45,21026,21028],{"className":47,"code":21027,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(9, 0.5))  # 3.0\n",[17,21029,21030,21036,21040],{"__ignoreMap":50},[54,21031,21032,21034],{"class":56,"line":57},[54,21033,61],{"class":60},[54,21035,18340],{"class":64},[54,21037,21038],{"class":56,"line":68},[54,21039,72],{"emptyLinePlaceholder":71},[54,21041,21042,21044,21046,21048,21050,21052,21054,21056,21058,21061,21063],{"class":56,"line":75},[54,21043,116],{"class":115},[54,21045,96],{"class":88},[54,21047,18295],{"class":92},[54,21049,89],{"class":88},[54,21051,20535],{"class":92},[54,21053,96],{"class":88},[54,21055,1125],{"class":99},[54,21057,103],{"class":88},[54,21059,21060],{"class":99}," 0.5",[54,21062,4310],{"class":88},[54,21064,21065],{"class":4313},"  # 3.0\n",[14,21067,21068,21069,21072,21073,21079],{},"An exponent of ",[17,21070,21071],{},"0.5"," means square root. For square roots, ",[192,21074,21076],{"href":21075},"\u002Fstandard-library\u002Fmath.sqrt-function-explained",[17,21077,21078],{},"math.sqrt()"," is often easier to read.",[40,21081,21083],{"id":21082},"mathpow-vs","math.pow() vs **",[14,21085,21086,21087,134,21089,21091],{},"Beginners often ask whether ",[17,21088,20490],{},[17,21090,20504],{}," are the same. They both perform exponentiation, but they do not behave exactly the same way.",[602,21093,21095],{"id":21094},"example-comparison","Example comparison",[45,21097,21099],{"className":47,"code":21098,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(2, 3))  # 8.0\nprint(2 ** 3)          # 8\n",[17,21100,21101,21107,21111,21135],{"__ignoreMap":50},[54,21102,21103,21105],{"class":56,"line":57},[54,21104,61],{"class":60},[54,21106,18340],{"class":64},[54,21108,21109],{"class":56,"line":68},[54,21110,72],{"emptyLinePlaceholder":71},[54,21112,21113,21115,21117,21119,21121,21123,21125,21127,21129,21131,21133],{"class":56,"line":75},[54,21114,116],{"class":115},[54,21116,96],{"class":88},[54,21118,18295],{"class":92},[54,21120,89],{"class":88},[54,21122,20535],{"class":92},[54,21124,96],{"class":88},[54,21126,396],{"class":99},[54,21128,103],{"class":88},[54,21130,379],{"class":99},[54,21132,4310],{"class":88},[54,21134,20558],{"class":4313},[54,21136,21137,21139,21141,21143,21146,21148,21150],{"class":56,"line":112},[54,21138,116],{"class":115},[54,21140,96],{"class":88},[54,21142,396],{"class":99},[54,21144,21145],{"class":81}," **",[54,21147,379],{"class":99},[54,21149,876],{"class":88},[54,21151,21152],{"class":4313},"          # 8\n",[602,21154,21156],{"id":21155},"main-differences","Main differences",[149,21158,21159,21167,21174,21181,21186,21191],{},[152,21160,21161,20564,21164],{},[17,21162,21163],{},"math.pow(2, 3)",[17,21165,21166],{},"8.0",[152,21168,21169,20564,21172],{},[17,21170,21171],{},"2 ** 3",[17,21173,327],{},[152,21175,21176,21178,21179],{},[17,21177,20490],{}," requires ",[17,21180,20374],{},[152,21182,21183,21185],{},[17,21184,20504],{}," works without importing anything",[152,21187,21188,21190],{},[17,21189,20490],{}," converts values to float",[152,21192,21193,21195],{},[17,21194,20504],{}," follows normal Python exponent behavior",[602,21197,21199],{"id":21198},"which-should-you-use","Which should you use?",[14,21201,21202,21203,89],{},"In most everyday Python code, beginners will usually want ",[17,21204,20504],{},[14,21206,126,21207,21209],{},[17,21208,20504],{}," when:",[149,21211,21212,21215,21220],{},[152,21213,21214],{},"you want simple exponentiation",[152,21216,21217,21218],{},"you do not want to import ",[17,21219,18295],{},[152,21221,21222],{},"you want normal Python numeric behavior",[14,21224,126,21225,21209],{},[17,21226,20490],{},[149,21228,21229,21234,21237],{},[152,21230,21231,21232,160],{},"you are already using the ",[17,21233,18295],{},[152,21235,21236],{},"you are learning standard library math functions together",[152,21238,21239],{},"a float result is fine",[14,21241,21242,21243,21248],{},"If you want to clean up a float result for display, ",[192,21244,21246],{"href":21245},"\u002Freference\u002Fpython-round-function-explained",[17,21247,18305],{}," can help.",[40,21250,21252],{"id":21251},"when-beginners-should-use-mathpow","When beginners should use math.pow()",[14,21254,21255,21257],{},[17,21256,20490],{}," is not wrong. It is just not always necessary.",[14,21259,21260],{},"It makes sense to use it when:",[149,21262,21263,21268,21271],{},[152,21264,21265,21266],{},"you are already working with other functions from ",[17,21267,18295],{},[152,21269,21270],{},"you want to keep your code consistent with other math-module calls",[152,21272,21273],{},"a float result is acceptable",[14,21275,21276],{},"It is usually not necessary when:",[149,21278,21279,21282,21287],{},[152,21280,21281],{},"you only need simple exponentiation",[152,21283,21284,21286],{},[17,21285,20504],{}," would be shorter and clearer",[152,21288,21289],{},"you expect an integer result",[14,21291,21292,21293,21295,21296,21298],{},"For many beginners, ",[17,21294,20504],{}," is the more natural choice, while ",[17,21297,20490],{}," is useful to recognize and understand.",[40,21300,21302],{"id":21301},"common-errors-and-mistakes","Common errors and mistakes",[602,21304,20456],{"id":19941},[14,21306,985,21307,21309,21310,21312],{},[17,21308,988],{}," because Python does not know what ",[17,21311,18295],{}," is.",[45,21314,21316],{"className":47,"code":21315,"language":49,"meta":50,"style":50},"print(math.pow(2, 3))\n",[17,21317,21318],{"__ignoreMap":50},[54,21319,21320,21322,21324,21326,21328,21330,21332,21334,21336,21338],{"class":56,"line":57},[54,21321,116],{"class":115},[54,21323,96],{"class":88},[54,21325,18295],{"class":92},[54,21327,89],{"class":88},[54,21329,20535],{"class":92},[54,21331,96],{"class":88},[54,21333,396],{"class":99},[54,21335,103],{"class":88},[54,21337,379],{"class":99},[54,21339,382],{"class":88},[14,21341,10357,21342,21344],{},[17,21343,18295],{}," was never imported.",[14,21346,21347],{},"Fix it like this:",[45,21349,21351],{"className":47,"code":21350,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(2, 3))\n",[17,21352,21353,21359,21363],{"__ignoreMap":50},[54,21354,21355,21357],{"class":56,"line":57},[54,21356,61],{"class":60},[54,21358,18340],{"class":64},[54,21360,21361],{"class":56,"line":68},[54,21362,72],{"emptyLinePlaceholder":71},[54,21364,21365,21367,21369,21371,21373,21375,21377,21379,21381,21383],{"class":56,"line":75},[54,21366,116],{"class":115},[54,21368,96],{"class":88},[54,21370,18295],{"class":92},[54,21372,89],{"class":88},[54,21374,20535],{"class":92},[54,21376,96],{"class":88},[54,21378,396],{"class":99},[54,21380,103],{"class":88},[54,21382,379],{"class":99},[54,21384,382],{"class":88},[14,21386,21387,21388,89],{},"If you see this kind of problem, read ",[192,21389,1087],{"href":1083},[602,21391,5481,21393,21396],{"id":21392},"using-pow-like-a-method",[17,21394,21395],{},".pow()"," like a method",[14,21398,21399],{},"This is incorrect:",[45,21401,21403],{"className":47,"code":21402,"language":49,"meta":50,"style":50},"number = 4\nprint(number.pow(2))\n",[17,21404,21405,21413],{"__ignoreMap":50},[54,21406,21407,21409,21411],{"class":56,"line":57},[54,21408,78],{"class":64},[54,21410,82],{"class":81},[54,21412,576],{"class":99},[54,21414,21415,21417,21419,21421,21423,21425,21427,21429],{"class":56,"line":68},[54,21416,116],{"class":115},[54,21418,96],{"class":88},[54,21420,121],{"class":92},[54,21422,89],{"class":88},[54,21424,20535],{"class":92},[54,21426,96],{"class":88},[54,21428,396],{"class":99},[54,21430,382],{"class":88},[14,21432,21433,21434,5220],{},"Numbers do not have a ",[17,21435,21395],{},[14,21437,21438],{},"Use this instead:",[45,21440,21442],{"className":47,"code":21441,"language":49,"meta":50,"style":50},"import math\n\nnumber = 4\nprint(math.pow(number, 2))  # 16.0\n",[17,21443,21444,21450,21454,21462],{"__ignoreMap":50},[54,21445,21446,21448],{"class":56,"line":57},[54,21447,61],{"class":60},[54,21449,18340],{"class":64},[54,21451,21452],{"class":56,"line":68},[54,21453,72],{"emptyLinePlaceholder":71},[54,21455,21456,21458,21460],{"class":56,"line":75},[54,21457,78],{"class":64},[54,21459,82],{"class":81},[54,21461,576],{"class":99},[54,21463,21464,21466,21468,21470,21472,21474,21476,21478,21480,21482,21484],{"class":56,"line":112},[54,21465,116],{"class":115},[54,21467,96],{"class":88},[54,21469,18295],{"class":92},[54,21471,89],{"class":88},[54,21473,20535],{"class":92},[54,21475,96],{"class":88},[54,21477,121],{"class":92},[54,21479,103],{"class":88},[54,21481,466],{"class":99},[54,21483,4310],{"class":88},[54,21485,20754],{"class":4313},[14,21487,21488],{},"Or use the operator:",[45,21490,21492],{"className":47,"code":21491,"language":49,"meta":50,"style":50},"number = 4\nprint(number ** 2)  # 16\n",[17,21493,21494,21502],{"__ignoreMap":50},[54,21495,21496,21498,21500],{"class":56,"line":57},[54,21497,78],{"class":64},[54,21499,82],{"class":81},[54,21501,576],{"class":99},[54,21503,21504,21506,21508,21510,21512,21514,21516],{"class":56,"line":68},[54,21505,116],{"class":115},[54,21507,96],{"class":88},[54,21509,78],{"class":92},[54,21511,20504],{"class":81},[54,21513,466],{"class":99},[54,21515,876],{"class":88},[54,21517,21518],{"class":4313},"  # 16\n",[602,21520,21522],{"id":21521},"expecting-an-int-instead-of-a-float","Expecting an int instead of a float",[14,21524,21525],{},"Many beginners expect this:",[45,21527,21528],{"className":47,"code":20510,"language":49,"meta":50,"style":50},[17,21529,21530,21536,21540,21562],{"__ignoreMap":50},[54,21531,21532,21534],{"class":56,"line":57},[54,21533,61],{"class":60},[54,21535,18340],{"class":64},[54,21537,21538],{"class":56,"line":68},[54,21539,72],{"emptyLinePlaceholder":71},[54,21541,21542,21544,21546,21548,21550,21552,21554,21556,21558,21560],{"class":56,"line":75},[54,21543,3833],{"class":64},[54,21545,82],{"class":81},[54,21547,19445],{"class":64},[54,21549,89],{"class":88},[54,21551,20535],{"class":92},[54,21553,96],{"class":88},[54,21555,396],{"class":99},[54,21557,103],{"class":88},[54,21559,379],{"class":99},[54,21561,109],{"class":88},[54,21563,21564,21566,21568,21570,21572],{"class":56,"line":112},[54,21565,116],{"class":115},[54,21567,96],{"class":88},[54,21569,3866],{"class":92},[54,21571,876],{"class":88},[54,21573,20558],{"class":4313},[14,21575,21576,21577,21579,21580,20564,21582,89],{},"They expect ",[17,21578,327],{},", but ",[17,21581,20490],{},[17,21583,21166],{},[14,21585,21586],{},"If you need normal integer exponent behavior, use:",[45,21588,21590],{"className":47,"code":21589,"language":49,"meta":50,"style":50},"print(2 ** 3)  # 8\n",[17,21591,21592],{"__ignoreMap":50},[54,21593,21594,21596,21598,21600,21602,21604,21606],{"class":56,"line":57},[54,21595,116],{"class":115},[54,21597,96],{"class":88},[54,21599,396],{"class":99},[54,21601,21145],{"class":81},[54,21603,379],{"class":99},[54,21605,876],{"class":88},[54,21607,21608],{"class":4313},"  # 8\n",[602,21610,21612],{"id":21611},"passing-strings-instead-of-numbers","Passing strings instead of numbers",[14,21614,985,21615,147],{},[17,21616,10040],{},[45,21618,21620],{"className":47,"code":21619,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(\"2\", \"3\"))\n",[17,21621,21622,21628,21632],{"__ignoreMap":50},[54,21623,21624,21626],{"class":56,"line":57},[54,21625,61],{"class":60},[54,21627,18340],{"class":64},[54,21629,21630],{"class":56,"line":68},[54,21631,72],{"emptyLinePlaceholder":71},[54,21633,21634,21636,21638,21640,21642,21644,21646,21648,21650,21652,21654,21656,21658,21660],{"class":56,"line":75},[54,21635,116],{"class":115},[54,21637,96],{"class":88},[54,21639,18295],{"class":92},[54,21641,89],{"class":88},[54,21643,20535],{"class":92},[54,21645,96],{"class":88},[54,21647,545],{"class":544},[54,21649,396],{"class":548},[54,21651,545],{"class":544},[54,21653,103],{"class":88},[54,21655,830],{"class":544},[54,21657,401],{"class":548},[54,21659,545],{"class":544},[54,21661,382],{"class":88},[14,21663,21664],{},"Fix it by using numbers:",[45,21666,21667],{"className":47,"code":20637,"language":49,"meta":50,"style":50},[17,21668,21669,21675,21679],{"__ignoreMap":50},[54,21670,21671,21673],{"class":56,"line":57},[54,21672,61],{"class":60},[54,21674,18340],{"class":64},[54,21676,21677],{"class":56,"line":68},[54,21678,72],{"emptyLinePlaceholder":71},[54,21680,21681,21683,21685,21687,21689,21691,21693,21695,21697,21699,21701],{"class":56,"line":75},[54,21682,116],{"class":115},[54,21684,96],{"class":88},[54,21686,18295],{"class":92},[54,21688,89],{"class":88},[54,21690,20535],{"class":92},[54,21692,96],{"class":88},[54,21694,396],{"class":99},[54,21696,103],{"class":88},[54,21698,379],{"class":99},[54,21700,4310],{"class":88},[54,21702,20558],{"class":4313},[14,21704,21705],{},"Or convert input values first:",[45,21707,21709],{"className":47,"code":21708,"language":49,"meta":50,"style":50},"import math\n\nx = \"2\"\ny = \"3\"\n\nprint(math.pow(float(x), float(y)))  # 8.0\n",[17,21710,21711,21717,21721,21734,21747,21751],{"__ignoreMap":50},[54,21712,21713,21715],{"class":56,"line":57},[54,21714,61],{"class":60},[54,21716,18340],{"class":64},[54,21718,21719],{"class":56,"line":68},[54,21720,72],{"emptyLinePlaceholder":71},[54,21722,21723,21726,21728,21730,21732],{"class":56,"line":75},[54,21724,21725],{"class":64},"x ",[54,21727,82],{"class":81},[54,21729,830],{"class":544},[54,21731,396],{"class":548},[54,21733,5239],{"class":544},[54,21735,21736,21739,21741,21743,21745],{"class":56,"line":112},[54,21737,21738],{"class":64},"y ",[54,21740,82],{"class":81},[54,21742,830],{"class":544},[54,21744,401],{"class":548},[54,21746,5239],{"class":544},[54,21748,21749],{"class":56,"line":887},[54,21750,72],{"emptyLinePlaceholder":71},[54,21752,21753,21755,21757,21759,21761,21763,21765,21767,21769,21771,21774,21776,21778,21780,21783],{"class":56,"line":892},[54,21754,116],{"class":115},[54,21756,96],{"class":88},[54,21758,18295],{"class":92},[54,21760,89],{"class":88},[54,21762,20535],{"class":92},[54,21764,96],{"class":88},[54,21766,9662],{"class":429},[54,21768,96],{"class":88},[54,21770,20567],{"class":92},[54,21772,21773],{"class":88},"),",[54,21775,20288],{"class":429},[54,21777,96],{"class":88},[54,21779,20571],{"class":92},[54,21781,21782],{"class":88},")))",[54,21784,20558],{"class":4313},[602,21786,21788],{"id":21787},"invalid-math-cases","Invalid math cases",[14,21790,21791,21792,21794,21795,89],{},"Some values are mathematically invalid for ",[17,21793,20490],{}," and may raise ",[17,21796,1306],{},[14,21798,415],{},[45,21800,21802],{"className":47,"code":21801,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pow(-9, 0.5))\n",[17,21803,21804,21810,21814],{"__ignoreMap":50},[54,21805,21806,21808],{"class":56,"line":57},[54,21807,61],{"class":60},[54,21809,18340],{"class":64},[54,21811,21812],{"class":56,"line":68},[54,21813,72],{"emptyLinePlaceholder":71},[54,21815,21816,21818,21820,21822,21824,21826,21828,21830,21832,21834,21836],{"class":56,"line":75},[54,21817,116],{"class":115},[54,21819,96],{"class":88},[54,21821,18295],{"class":92},[54,21823,89],{"class":88},[54,21825,20535],{"class":92},[54,21827,96],{"class":88},[54,21829,1467],{"class":81},[54,21831,1125],{"class":99},[54,21833,103],{"class":88},[54,21835,21060],{"class":99},[54,21837,382],{"class":88},[14,21839,21840],{},"This tries to take a square root of a negative number using real-number math, which is not allowed here.",[40,21842,21844],{"id":21843},"useful-related-functions","Useful related functions",[602,21846,21847],{"id":20535},[17,21848,21849],{},"pow()",[14,21851,21852,21853,21855],{},"Python also has a built-in ",[17,21854,21849],{}," function.",[45,21857,21859],{"className":47,"code":21858,"language":49,"meta":50,"style":50},"print(pow(2, 3))  # 8\n",[17,21860,21861],{"__ignoreMap":50},[54,21862,21863,21865,21867,21869,21871,21873,21875,21877,21879],{"class":56,"line":57},[54,21864,116],{"class":115},[54,21866,96],{"class":88},[54,21868,20535],{"class":115},[54,21870,96],{"class":88},[54,21872,396],{"class":99},[54,21874,103],{"class":88},[54,21876,379],{"class":99},[54,21878,4310],{"class":88},[54,21880,21608],{"class":4313},[14,21882,21883,21884,21886],{},"This is different from ",[17,21885,20490],{}," because it does not always force the result to be a float.",[602,21888,21890],{"id":21889},"mathsqrt",[17,21891,21078],{},[14,21893,21894,21895,21897,21898,89],{},"For square roots, ",[17,21896,21078],{}," is often clearer than using an exponent like ",[17,21899,21071],{},[45,21901,21903],{"className":47,"code":21902,"language":49,"meta":50,"style":50},"import math\n\nprint(math.sqrt(9))  # 3.0\n",[17,21904,21905,21911,21915],{"__ignoreMap":50},[54,21906,21907,21909],{"class":56,"line":57},[54,21908,61],{"class":60},[54,21910,18340],{"class":64},[54,21912,21913],{"class":56,"line":68},[54,21914,72],{"emptyLinePlaceholder":71},[54,21916,21917,21919,21921,21923,21925,21928,21930,21932,21934],{"class":56,"line":75},[54,21918,116],{"class":115},[54,21920,96],{"class":88},[54,21922,18295],{"class":92},[54,21924,89],{"class":88},[54,21926,21927],{"class":92},"sqrt",[54,21929,96],{"class":88},[54,21931,1125],{"class":99},[54,21933,4310],{"class":88},[54,21935,21065],{"class":4313},[14,21937,13462,21938,89],{},[192,21939,21940,2834],{"href":21075},[17,21941,21078],{},[602,21943,21945,134,21947],{"id":21944},"mathceil-and-mathfloor",[17,21946,18265],{},[17,21948,18268],{},[14,21950,21951],{},"These are useful when you are working with float results and want to move up or down to a whole number.",[45,21953,21955],{"className":47,"code":21954,"language":49,"meta":50,"style":50},"import math\n\nvalue = math.pow(2, 3) \u002F 3\nprint(value)             # 2.6666666666666665\nprint(math.ceil(value))  # 3\nprint(math.floor(value)) # 2\n",[17,21956,21957,21963,21967,21994,22007,22027],{"__ignoreMap":50},[54,21958,21959,21961],{"class":56,"line":57},[54,21960,61],{"class":60},[54,21962,18340],{"class":64},[54,21964,21965],{"class":56,"line":68},[54,21966,72],{"emptyLinePlaceholder":71},[54,21968,21969,21971,21973,21975,21977,21979,21981,21983,21985,21987,21989,21992],{"class":56,"line":75},[54,21970,5230],{"class":64},[54,21972,82],{"class":81},[54,21974,19445],{"class":64},[54,21976,89],{"class":88},[54,21978,20535],{"class":92},[54,21980,96],{"class":88},[54,21982,396],{"class":99},[54,21984,103],{"class":88},[54,21986,379],{"class":99},[54,21988,876],{"class":88},[54,21990,21991],{"class":81}," \u002F",[54,21993,9230],{"class":99},[54,21995,21996,21998,22000,22002,22004],{"class":56,"line":112},[54,21997,116],{"class":115},[54,21999,96],{"class":88},[54,22001,5248],{"class":92},[54,22003,876],{"class":88},[54,22005,22006],{"class":4313},"             # 2.6666666666666665\n",[54,22008,22009,22011,22013,22015,22017,22019,22021,22023,22025],{"class":56,"line":887},[54,22010,116],{"class":115},[54,22012,96],{"class":88},[54,22014,18295],{"class":92},[54,22016,89],{"class":88},[54,22018,18357],{"class":92},[54,22020,96],{"class":88},[54,22022,5248],{"class":92},[54,22024,4310],{"class":88},[54,22026,18390],{"class":4313},[54,22028,22029,22031,22033,22035,22037,22039,22041,22043,22045],{"class":56,"line":892},[54,22030,116],{"class":115},[54,22032,96],{"class":88},[54,22034,18295],{"class":92},[54,22036,89],{"class":88},[54,22038,18380],{"class":92},[54,22040,96],{"class":88},[54,22042,5248],{"class":92},[54,22044,4310],{"class":88},[54,22046,22047],{"class":4313}," # 2\n",[602,22049,22050],{"id":19108},[17,22051,18305],{},[14,22053,126,22054,22056],{},[17,22055,18305],{}," when you want cleaner output.",[45,22058,22060],{"className":47,"code":22059,"language":49,"meta":50,"style":50},"import math\n\nvalue = math.pow(2, -1)\nprint(value)          # 0.5\nprint(round(value, 1))  # 0.5\n",[17,22061,22062,22068,22072,22096,22109],{"__ignoreMap":50},[54,22063,22064,22066],{"class":56,"line":57},[54,22065,61],{"class":60},[54,22067,18340],{"class":64},[54,22069,22070],{"class":56,"line":68},[54,22071,72],{"emptyLinePlaceholder":71},[54,22073,22074,22076,22078,22080,22082,22084,22086,22088,22090,22092,22094],{"class":56,"line":75},[54,22075,5230],{"class":64},[54,22077,82],{"class":81},[54,22079,19445],{"class":64},[54,22081,89],{"class":88},[54,22083,20535],{"class":92},[54,22085,96],{"class":88},[54,22087,396],{"class":99},[54,22089,103],{"class":88},[54,22091,879],{"class":81},[54,22093,100],{"class":99},[54,22095,109],{"class":88},[54,22097,22098,22100,22102,22104,22106],{"class":56,"line":112},[54,22099,116],{"class":115},[54,22101,96],{"class":88},[54,22103,5248],{"class":92},[54,22105,876],{"class":88},[54,22107,22108],{"class":4313},"          # 0.5\n",[54,22110,22111,22113,22115,22117,22119,22121,22123,22125,22127],{"class":56,"line":887},[54,22112,116],{"class":115},[54,22114,96],{"class":88},[54,22116,19108],{"class":115},[54,22118,96],{"class":88},[54,22120,5248],{"class":92},[54,22122,103],{"class":88},[54,22124,882],{"class":99},[54,22126,4310],{"class":88},[54,22128,21006],{"class":4313},[14,22130,22131,22132,89],{},"You can also read ",[192,22133,22134],{"href":21245},"Python round() explained",[40,22136,1419],{"id":1418},[602,22138,22140],{"id":22139},"what-does-mathpow-do-in-python","What does math.pow() do in Python?",[14,22142,22143],{},"It raises one number to the power of another and returns the result as a float.",[602,22145,22147],{"id":22146},"do-i-need-to-import-math-to-use-mathpow","Do I need to import math to use math.pow()?",[14,22149,22150,22151,22153,22154,22156,22157,20375],{},"Yes. ",[17,22152,20490],{}," is part of the ",[17,22155,18295],{}," module, so you must write ",[17,22158,20374],{},[602,22160,22162],{"id":22161},"what-is-the-difference-between-mathpow-and-in-python","What is the difference between math.pow() and ** in Python?",[14,22164,22165,22167,22168,22170,22171,22173,22174,89],{},[17,22166,20490],{}," always returns a float and requires ",[17,22169,20374],{},". The ",[17,22172,20504],{}," operator does not need an import and may return an ",[17,22175,9657],{},[602,22177,22179],{"id":22178},"why-does-mathpow2-3-return-80-instead-of-8","Why does math.pow(2, 3) return 8.0 instead of 8?",[14,22181,3412,22182,22184],{},[17,22183,20490],{}," returns a float result, even when the answer is a whole number.",[602,22186,22188],{"id":22187},"can-mathpow-use-negative-exponents","Can math.pow() use negative exponents?",[14,22190,22191,22192,20564,22195,89],{},"Yes. For example, ",[17,22193,22194],{},"math.pow(2, -1)",[17,22196,21071],{},[40,22198,1510],{"id":1509},[149,22200,22201,22205,22210,22214,22220],{},[152,22202,22203],{},[192,22204,20396],{"href":20700},[152,22206,22207],{},[192,22208,22209],{"href":21075},"math.sqrt() function explained",[152,22211,22212],{},[192,22213,22134],{"href":21245},[152,22215,22216],{},[192,22217,22219],{"href":22218},"\u002Freference\u002Fpython-abs-function-explained","Python abs() explained",[152,22221,22222],{},[192,22223,1087],{"href":1083},[1545,22225,22226],{},"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 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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":50,"searchDepth":68,"depth":68,"links":22228},[22229,22230,22231,22232,22238,22243,22244,22252,22259,22266],{"id":42,"depth":68,"text":43},{"id":20575,"depth":68,"text":20576},{"id":198,"depth":68,"text":199},{"id":20858,"depth":68,"text":20859,"children":22233},[22234,22235,22236,22237],{"id":20862,"depth":75,"text":20863},{"id":20912,"depth":75,"text":20913},{"id":20963,"depth":75,"text":20964},{"id":21023,"depth":75,"text":21024},{"id":21082,"depth":68,"text":21083,"children":22239},[22240,22241,22242],{"id":21094,"depth":75,"text":21095},{"id":21155,"depth":75,"text":21156},{"id":21198,"depth":75,"text":21199},{"id":21251,"depth":68,"text":21252},{"id":21301,"depth":68,"text":21302,"children":22245},[22246,22247,22249,22250,22251],{"id":19941,"depth":75,"text":20456},{"id":21392,"depth":75,"text":22248},"Using .pow() like a method",{"id":21521,"depth":75,"text":21522},{"id":21611,"depth":75,"text":21612},{"id":21787,"depth":75,"text":21788},{"id":21843,"depth":68,"text":21844,"children":22253},[22254,22255,22256,22258],{"id":20535,"depth":75,"text":21849},{"id":21889,"depth":75,"text":21078},{"id":21944,"depth":75,"text":22257},"math.ceil() and math.floor()",{"id":19108,"depth":75,"text":18305},{"id":1418,"depth":68,"text":1419,"children":22260},[22261,22262,22263,22264,22265],{"id":22139,"depth":75,"text":22140},{"id":22146,"depth":75,"text":22147},{"id":22161,"depth":75,"text":22162},{"id":22178,"depth":75,"text":22179},{"id":22187,"depth":75,"text":22188},{"id":1509,"depth":68,"text":1510},"Master math.pow function explained in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fmath.pow-function-explained",{"title":20480,"description":22267},"standard-library\u002Fmath.pow-function-explained","4rEQE9HwUMZpH-UtAnBTUiON7JO-CZ-8b0jtmqxvBME",{"id":22274,"title":22275,"body":22276,"description":23652,"extension":1577,"meta":23653,"navigation":71,"path":21075,"seo":23654,"stem":23655,"__hash__":23656},"content\u002Fstandard-library\u002Fmath.sqrt-function-explained.md","math.sqrt() Function Explained",{"type":7,"value":22277,"toc":23620},[22278,22283,22291,22297,22299,22346,22354,22359,22376,22379,22381,22396,22402,22404,22406,22425,22427,22448,22450,22483,22485,22494,22496,22499,22555,22557,22566,22569,22594,22598,22603,22609,22611,22644,22646,22655,22663,22674,22677,22724,22726,22734,22739,22743,22754,22756,22816,22819,22823,22828,22830,22865,22868,22882,22885,22898,22907,22910,22993,22995,23000,23004,23009,23011,23044,23047,23090,23100,23108,23110,23139,23141,23173,23177,23184,23186,23223,23226,23267,23269,23332,23336,23341,23421,23425,23428,23461,23463,23472,23478,23484,23486,23513,23515,23523,23530,23532,23542,23549,23560,23564,23570,23578,23586,23592,23594,23618],[10,22279,22281,11309],{"id":22280},"mathsqrt-function-explained",[17,22282,21078],{},[14,22284,22285,22287,22288,22290],{},[17,22286,21078],{}," returns the square root of a number in Python. It is part of the ",[17,22289,18295],{}," module, so you need to import that module before using it.",[14,22292,22293,22294,22296],{},"This function is useful when writing formulas, solving geometry problems, or doing basic calculations. On this page, you will learn what ",[17,22295,21078],{}," does, what it returns, when to use it, and the most common beginner mistakes.",[40,22298,43],{"id":42},[45,22300,22302],{"className":47,"code":22301,"language":49,"meta":50,"style":50},"import math\n\nresult = math.sqrt(25)\nprint(result)  # 5.0\n",[17,22303,22304,22310,22314,22333],{"__ignoreMap":50},[54,22305,22306,22308],{"class":56,"line":57},[54,22307,61],{"class":60},[54,22309,18340],{"class":64},[54,22311,22312],{"class":56,"line":68},[54,22313,72],{"emptyLinePlaceholder":71},[54,22315,22316,22318,22320,22322,22324,22326,22328,22331],{"class":56,"line":75},[54,22317,3833],{"class":64},[54,22319,82],{"class":81},[54,22321,19445],{"class":64},[54,22323,89],{"class":88},[54,22325,21927],{"class":92},[54,22327,96],{"class":88},[54,22329,22330],{"class":99},"25",[54,22332,109],{"class":88},[54,22334,22335,22337,22339,22341,22343],{"class":56,"line":112},[54,22336,116],{"class":115},[54,22338,96],{"class":88},[54,22340,3866],{"class":92},[54,22342,876],{"class":88},[54,22344,22345],{"class":4313},"  # 5.0\n",[14,22347,126,22348,22350,22351,22353],{},[17,22349,21078],{}," after importing the ",[17,22352,18295],{}," module. It returns a float.",[40,22355,1736,22357,1739],{"id":22356},"what-mathsqrt-does",[17,22358,21078],{},[149,22360,22361,22364,22368,22373],{},[152,22362,22363],{},"Returns the square root of a number",[152,22365,15989,22366,160],{},[17,22367,18295],{},[152,22369,20688,22370,22372],{},[17,22371,20374],{}," before using it",[152,22374,22375],{},"The result is usually returned as a float",[14,22377,22378],{},"A square root is a number that multiplies by itself to make the original number.",[14,22380,415],{},[149,22382,22383,22390],{},[152,22384,22385,22386,337,22388],{},"The square root of ",[17,22387,1125],{},[17,22389,401],{},[152,22391,22385,22392,337,22394],{},[17,22393,22330],{},[17,22395,285],{},[14,22397,6345,22398,6348,22400,89],{},[17,22399,18295],{},[192,22401,20396],{"href":20700},[40,22403,199],{"id":198},[14,22405,20608],{},[45,22407,22409],{"className":47,"code":22408,"language":49,"meta":50,"style":50},"math.sqrt(x)\n",[17,22410,22411],{"__ignoreMap":50},[54,22412,22413,22415,22417,22419,22421,22423],{"class":56,"line":57},[54,22414,18295],{"class":64},[54,22416,89],{"class":88},[54,22418,21927],{"class":92},[54,22420,96],{"class":88},[54,22422,20567],{"class":92},[54,22424,109],{"class":88},[14,22426,229],{},[149,22428,22429,22434,22443],{},[152,22430,22431,22433],{},[17,22432,20567],{}," should be a number",[152,22435,22436,22438,22439,5507,22441],{},[17,22437,20567],{}," is usually an ",[17,22440,9657],{},[17,22442,9662],{},[152,22444,22445,22446],{},"The function returns the square root of ",[17,22447,20567],{},[14,22449,253],{},[45,22451,22453],{"className":47,"code":22452,"language":49,"meta":50,"style":50},"import math\n\nprint(math.sqrt(9))\n",[17,22454,22455,22461,22465],{"__ignoreMap":50},[54,22456,22457,22459],{"class":56,"line":57},[54,22458,61],{"class":60},[54,22460,18340],{"class":64},[54,22462,22463],{"class":56,"line":68},[54,22464,72],{"emptyLinePlaceholder":71},[54,22466,22467,22469,22471,22473,22475,22477,22479,22481],{"class":56,"line":75},[54,22468,116],{"class":115},[54,22470,96],{"class":88},[54,22472,18295],{"class":92},[54,22474,89],{"class":88},[54,22476,21927],{"class":92},[54,22478,96],{"class":88},[54,22480,1125],{"class":99},[54,22482,382],{"class":88},[14,22484,449],{},[45,22486,22488],{"className":47,"code":22487,"language":49,"meta":50,"style":50},"3.0\n",[17,22489,22490],{"__ignoreMap":50},[54,22491,22492],{"class":56,"line":57},[54,22493,22487],{"class":99},[40,22495,8532],{"id":8531},[14,22497,22498],{},"Here is a complete example:",[45,22500,22502],{"className":47,"code":22501,"language":49,"meta":50,"style":50},"import math\n\nnumber = 36\nresult = math.sqrt(number)\n\nprint(result)\n",[17,22503,22504,22510,22514,22523,22541,22545],{"__ignoreMap":50},[54,22505,22506,22508],{"class":56,"line":57},[54,22507,61],{"class":60},[54,22509,18340],{"class":64},[54,22511,22512],{"class":56,"line":68},[54,22513,72],{"emptyLinePlaceholder":71},[54,22515,22516,22518,22520],{"class":56,"line":75},[54,22517,78],{"class":64},[54,22519,82],{"class":81},[54,22521,22522],{"class":99}," 36\n",[54,22524,22525,22527,22529,22531,22533,22535,22537,22539],{"class":56,"line":112},[54,22526,3833],{"class":64},[54,22528,82],{"class":81},[54,22530,19445],{"class":64},[54,22532,89],{"class":88},[54,22534,21927],{"class":92},[54,22536,96],{"class":88},[54,22538,121],{"class":92},[54,22540,109],{"class":88},[54,22542,22543],{"class":56,"line":887},[54,22544,72],{"emptyLinePlaceholder":71},[54,22546,22547,22549,22551,22553],{"class":56,"line":892},[54,22548,116],{"class":115},[54,22550,96],{"class":88},[54,22552,3866],{"class":92},[54,22554,109],{"class":88},[14,22556,449],{},[45,22558,22560],{"className":47,"code":22559,"language":49,"meta":50,"style":50},"6.0\n",[17,22561,22562],{"__ignoreMap":50},[54,22563,22564],{"class":56,"line":57},[54,22565,22559],{"class":99},[14,22567,22568],{},"What this code does:",[149,22570,22571,22576,22585,22591],{},[152,22572,22573,22574,160],{},"Imports the ",[17,22575,18295],{},[152,22577,22578,22579,22582,22583],{},"Stores ",[17,22580,22581],{},"36"," in the variable ",[17,22584,121],{},[152,22586,22587,22588],{},"Calculates the square root with ",[17,22589,22590],{},"math.sqrt(number)",[152,22592,22593],{},"Prints the result",[40,22595,22597],{"id":22596},"what-the-return-value-looks-like","What the return value looks like",[14,22599,22600,22602],{},[17,22601,21078],{}," returns a float.",[14,22604,22605,22606,89],{},"That means even if the answer looks like a whole number, Python usually shows it with ",[17,22607,22608],{},".0",[14,22610,253],{},[45,22612,22614],{"className":47,"code":22613,"language":49,"meta":50,"style":50},"import math\n\nprint(math.sqrt(16))\n",[17,22615,22616,22622,22626],{"__ignoreMap":50},[54,22617,22618,22620],{"class":56,"line":57},[54,22619,61],{"class":60},[54,22621,18340],{"class":64},[54,22623,22624],{"class":56,"line":68},[54,22625,72],{"emptyLinePlaceholder":71},[54,22627,22628,22630,22632,22634,22636,22638,22640,22642],{"class":56,"line":75},[54,22629,116],{"class":115},[54,22631,96],{"class":88},[54,22633,18295],{"class":92},[54,22635,89],{"class":88},[54,22637,21927],{"class":92},[54,22639,96],{"class":88},[54,22641,20777],{"class":99},[54,22643,382],{"class":88},[14,22645,449],{},[45,22647,22649],{"className":47,"code":22648,"language":49,"meta":50,"style":50},"4.0\n",[17,22650,22651],{"__ignoreMap":50},[54,22652,22653],{"class":56,"line":57},[54,22654,22648],{"class":99},[14,22656,22657,22658,11990,22661,89],{},"This is normal. The function returns ",[17,22659,22660],{},"4.0",[17,22662,2389],{},[14,22664,22665,22666,134,22670,89],{},"If you need to understand floats better, see ",[192,22667,22669],{"href":22668},"\u002Freference\u002Fpython-float-function-explained","Python float() explained",[192,22671,22673],{"href":22672},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex","Python numbers explained: int, float, complex",[14,22675,22676],{},"Only convert the result to an integer if you are sure the square root should be a whole number.",[45,22678,22680],{"className":47,"code":22679,"language":49,"meta":50,"style":50},"import math\n\nresult = math.sqrt(16)\nprint(int(result))\n",[17,22681,22682,22688,22692,22710],{"__ignoreMap":50},[54,22683,22684,22686],{"class":56,"line":57},[54,22685,61],{"class":60},[54,22687,18340],{"class":64},[54,22689,22690],{"class":56,"line":68},[54,22691,72],{"emptyLinePlaceholder":71},[54,22693,22694,22696,22698,22700,22702,22704,22706,22708],{"class":56,"line":75},[54,22695,3833],{"class":64},[54,22697,82],{"class":81},[54,22699,19445],{"class":64},[54,22701,89],{"class":88},[54,22703,21927],{"class":92},[54,22705,96],{"class":88},[54,22707,20777],{"class":99},[54,22709,109],{"class":88},[54,22711,22712,22714,22716,22718,22720,22722],{"class":56,"line":112},[54,22713,116],{"class":115},[54,22715,96],{"class":88},[54,22717,9657],{"class":429},[54,22719,96],{"class":88},[54,22721,3866],{"class":92},[54,22723,382],{"class":88},[14,22725,449],{},[45,22727,22728],{"className":47,"code":4510,"language":49,"meta":50,"style":50},[17,22729,22730],{"__ignoreMap":50},[54,22731,22732],{"class":56,"line":57},[54,22733,4510],{"class":99},[40,22735,15426,22737],{"id":22736},"when-to-use-mathsqrt",[17,22738,21078],{},[14,22740,126,22741,21209],{},[17,22742,21078],{},[149,22744,22745,22748,22751],{},[152,22746,22747],{},"You want the square root of a positive number",[152,22749,22750],{},"You are writing math formulas",[152,22752,22753],{},"You are working with distance, geometry, or basic calculations",[14,22755,253],{},[45,22757,22759],{"className":47,"code":22758,"language":49,"meta":50,"style":50},"import math\n\nside = 49\nlength = math.sqrt(side)\n\nprint(length)\n",[17,22760,22761,22767,22771,22781,22801,22805],{"__ignoreMap":50},[54,22762,22763,22765],{"class":56,"line":57},[54,22764,61],{"class":60},[54,22766,18340],{"class":64},[54,22768,22769],{"class":56,"line":68},[54,22770,72],{"emptyLinePlaceholder":71},[54,22772,22773,22776,22778],{"class":56,"line":75},[54,22774,22775],{"class":64},"side ",[54,22777,82],{"class":81},[54,22779,22780],{"class":99}," 49\n",[54,22782,22783,22786,22788,22790,22792,22794,22796,22799],{"class":56,"line":112},[54,22784,22785],{"class":64},"length ",[54,22787,82],{"class":81},[54,22789,19445],{"class":64},[54,22791,89],{"class":88},[54,22793,21927],{"class":92},[54,22795,96],{"class":88},[54,22797,22798],{"class":92},"side",[54,22800,109],{"class":88},[54,22802,22803],{"class":56,"line":887},[54,22804,72],{"emptyLinePlaceholder":71},[54,22806,22807,22809,22811,22814],{"class":56,"line":892},[54,22808,116],{"class":115},[54,22810,96],{"class":88},[54,22812,22813],{"class":92},"length",[54,22815,109],{"class":88},[14,22817,22818],{},"This is clearer than writing a more complex expression when all you need is a square root.",[40,22820,22822],{"id":22821},"negative-numbers-and-errors","Negative numbers and errors",[14,22824,22825,22827],{},[17,22826,21078],{}," does not support negative numbers in normal real-number math.",[14,22829,253],{},[45,22831,22833],{"className":47,"code":22832,"language":49,"meta":50,"style":50},"import math\n\nprint(math.sqrt(-1))\n",[17,22834,22835,22841,22845],{"__ignoreMap":50},[54,22836,22837,22839],{"class":56,"line":57},[54,22838,61],{"class":60},[54,22840,18340],{"class":64},[54,22842,22843],{"class":56,"line":68},[54,22844,72],{"emptyLinePlaceholder":71},[54,22846,22847,22849,22851,22853,22855,22857,22859,22861,22863],{"class":56,"line":75},[54,22848,116],{"class":115},[54,22850,96],{"class":88},[54,22852,18295],{"class":92},[54,22854,89],{"class":88},[54,22856,21927],{"class":92},[54,22858,96],{"class":88},[54,22860,1467],{"class":81},[54,22862,100],{"class":99},[54,22864,382],{"class":88},[14,22866,22867],{},"This raises:",[45,22869,22871],{"className":47,"code":22870,"language":49,"meta":50,"style":50},"ValueError: math domain error\n",[17,22872,22873],{"__ignoreMap":50},[54,22874,22875,22877,22879],{"class":56,"line":57},[54,22876,1306],{"class":429},[54,22878,147],{"class":88},[54,22880,22881],{"class":64}," math domain error\n",[14,22883,22884],{},"Why this happens:",[149,22886,22887,22892,22895],{},[152,22888,22889,22891],{},[17,22890,21078],{}," works with real numbers",[152,22893,22894],{},"The square root of a negative number is not a real number",[152,22896,22897],{},"So Python raises an error instead of returning a result",[14,22899,22900,22901,89],{},"If this is the error you are seeing, read ",[192,22902,1084,22904],{"href":22903},"\u002Ferrors\u002Fvalueerror-math-domain-error-fix",[17,22905,22906],{},"ValueError: math domain error",[14,22908,22909],{},"A simple way to avoid this problem is to check the value first:",[45,22911,22913],{"className":47,"code":22912,"language":49,"meta":50,"style":50},"import math\n\nvalue = 25\n\nif value >= 0:\n    print(math.sqrt(value))\nelse:\n    print(\"Cannot take the square root of a negative number\")\n",[17,22914,22915,22921,22925,22934,22938,22953,22971,22978],{"__ignoreMap":50},[54,22916,22917,22919],{"class":56,"line":57},[54,22918,61],{"class":60},[54,22920,18340],{"class":64},[54,22922,22923],{"class":56,"line":68},[54,22924,72],{"emptyLinePlaceholder":71},[54,22926,22927,22929,22931],{"class":56,"line":75},[54,22928,5230],{"class":64},[54,22930,82],{"class":81},[54,22932,22933],{"class":99}," 25\n",[54,22935,22936],{"class":56,"line":112},[54,22937,72],{"emptyLinePlaceholder":71},[54,22939,22940,22943,22946,22949,22951],{"class":56,"line":887},[54,22941,22942],{"class":60},"if",[54,22944,22945],{"class":64}," value ",[54,22947,22948],{"class":81},">=",[54,22950,3986],{"class":99},[54,22952,7572],{"class":88},[54,22954,22955,22957,22959,22961,22963,22965,22967,22969],{"class":56,"line":892},[54,22956,7606],{"class":115},[54,22958,96],{"class":88},[54,22960,18295],{"class":92},[54,22962,89],{"class":88},[54,22964,21927],{"class":92},[54,22966,96],{"class":88},[54,22968,5248],{"class":92},[54,22970,382],{"class":88},[54,22972,22973,22976],{"class":56,"line":913},[54,22974,22975],{"class":60},"else",[54,22977,7572],{"class":88},[54,22979,22980,22982,22984,22986,22989,22991],{"class":56,"line":2121},[54,22981,7606],{"class":115},[54,22983,96],{"class":88},[54,22985,545],{"class":544},[54,22987,22988],{"class":548},"Cannot take the square root of a negative number",[54,22990,545],{"class":544},[54,22992,109],{"class":88},[40,22994,5210],{"id":5209},[14,22996,22997,22998,89],{},"Here are the most common problems when using ",[17,22999,21078],{},[602,23001,17008,23002],{"id":19941},[17,23003,18295],{},[14,23005,23006,23007,21312],{},"This causes an error because Python does not know what ",[17,23008,18295],{},[14,23010,991],{},[45,23012,23014],{"className":47,"code":23013,"language":49,"meta":50,"style":50},"result = math.sqrt(25)\nprint(result)\n",[17,23015,23016,23034],{"__ignoreMap":50},[54,23017,23018,23020,23022,23024,23026,23028,23030,23032],{"class":56,"line":57},[54,23019,3833],{"class":64},[54,23021,82],{"class":81},[54,23023,19445],{"class":64},[54,23025,89],{"class":88},[54,23027,21927],{"class":92},[54,23029,96],{"class":88},[54,23031,22330],{"class":99},[54,23033,109],{"class":88},[54,23035,23036,23038,23040,23042],{"class":56,"line":68},[54,23037,116],{"class":115},[54,23039,96],{"class":88},[54,23041,3866],{"class":92},[54,23043,109],{"class":88},[14,23045,23046],{},"Right:",[45,23048,23050],{"className":47,"code":23049,"language":49,"meta":50,"style":50},"import math\n\nresult = math.sqrt(25)\nprint(result)\n",[17,23051,23052,23058,23062,23080],{"__ignoreMap":50},[54,23053,23054,23056],{"class":56,"line":57},[54,23055,61],{"class":60},[54,23057,18340],{"class":64},[54,23059,23060],{"class":56,"line":68},[54,23061,72],{"emptyLinePlaceholder":71},[54,23063,23064,23066,23068,23070,23072,23074,23076,23078],{"class":56,"line":75},[54,23065,3833],{"class":64},[54,23067,82],{"class":81},[54,23069,19445],{"class":64},[54,23071,89],{"class":88},[54,23073,21927],{"class":92},[54,23075,96],{"class":88},[54,23077,22330],{"class":99},[54,23079,109],{"class":88},[54,23081,23082,23084,23086,23088],{"class":56,"line":112},[54,23083,116],{"class":115},[54,23085,96],{"class":88},[54,23087,3866],{"class":92},[54,23089,109],{"class":88},[602,23091,23093,23094,3360,23097],{"id":23092},"writing-sqrt9-instead-of-mathsqrt9","Writing ",[17,23095,23096],{},"sqrt(9)",[17,23098,23099],{},"math.sqrt(9)",[14,23101,23102,23103,23105,23106,89],{},"If you only write ",[17,23104,23096],{},", Python will usually raise a ",[17,23107,988],{},[14,23109,991],{},[45,23111,23113],{"className":47,"code":23112,"language":49,"meta":50,"style":50},"import math\n\nprint(sqrt(9))\n",[17,23114,23115,23121,23125],{"__ignoreMap":50},[54,23116,23117,23119],{"class":56,"line":57},[54,23118,61],{"class":60},[54,23120,18340],{"class":64},[54,23122,23123],{"class":56,"line":68},[54,23124,72],{"emptyLinePlaceholder":71},[54,23126,23127,23129,23131,23133,23135,23137],{"class":56,"line":75},[54,23128,116],{"class":115},[54,23130,96],{"class":88},[54,23132,21927],{"class":92},[54,23134,96],{"class":88},[54,23136,1125],{"class":99},[54,23138,382],{"class":88},[14,23140,23046],{},[45,23142,23143],{"className":47,"code":22452,"language":49,"meta":50,"style":50},[17,23144,23145,23151,23155],{"__ignoreMap":50},[54,23146,23147,23149],{"class":56,"line":57},[54,23148,61],{"class":60},[54,23150,18340],{"class":64},[54,23152,23153],{"class":56,"line":68},[54,23154,72],{"emptyLinePlaceholder":71},[54,23156,23157,23159,23161,23163,23165,23167,23169,23171],{"class":56,"line":75},[54,23158,116],{"class":115},[54,23160,96],{"class":88},[54,23162,18295],{"class":92},[54,23164,89],{"class":88},[54,23166,21927],{"class":92},[54,23168,96],{"class":88},[54,23170,1125],{"class":99},[54,23172,382],{"class":88},[602,23174,23176],{"id":23175},"passing-a-string-instead-of-a-number","Passing a string instead of a number",[14,23178,23179,23180,23183],{},"This will fail because ",[17,23181,23182],{},"\"25\""," is text, not a number.",[14,23185,991],{},[45,23187,23189],{"className":47,"code":23188,"language":49,"meta":50,"style":50},"import math\n\nprint(math.sqrt(\"25\"))\n",[17,23190,23191,23197,23201],{"__ignoreMap":50},[54,23192,23193,23195],{"class":56,"line":57},[54,23194,61],{"class":60},[54,23196,18340],{"class":64},[54,23198,23199],{"class":56,"line":68},[54,23200,72],{"emptyLinePlaceholder":71},[54,23202,23203,23205,23207,23209,23211,23213,23215,23217,23219,23221],{"class":56,"line":75},[54,23204,116],{"class":115},[54,23206,96],{"class":88},[54,23208,18295],{"class":92},[54,23210,89],{"class":88},[54,23212,21927],{"class":92},[54,23214,96],{"class":88},[54,23216,545],{"class":544},[54,23218,22330],{"class":548},[54,23220,545],{"class":544},[54,23222,382],{"class":88},[14,23224,23225],{},"You can check the type of a value with:",[45,23227,23229],{"className":47,"code":23228,"language":49,"meta":50,"style":50},"value = \"25\"\nprint(type(value))\nprint(value)\n",[17,23230,23231,23243,23257],{"__ignoreMap":50},[54,23232,23233,23235,23237,23239,23241],{"class":56,"line":57},[54,23234,5230],{"class":64},[54,23236,82],{"class":81},[54,23238,830],{"class":544},[54,23240,22330],{"class":548},[54,23242,5239],{"class":544},[54,23244,23245,23247,23249,23251,23253,23255],{"class":56,"line":68},[54,23246,116],{"class":115},[54,23248,96],{"class":88},[54,23250,2208],{"class":429},[54,23252,96],{"class":88},[54,23254,5248],{"class":92},[54,23256,382],{"class":88},[54,23258,23259,23261,23263,23265],{"class":56,"line":75},[54,23260,116],{"class":115},[54,23262,96],{"class":88},[54,23264,5248],{"class":92},[54,23266,109],{"class":88},[14,23268,23046],{},[45,23270,23272],{"className":47,"code":23271,"language":49,"meta":50,"style":50},"import math\n\nvalue = \"25\"\nnumber = float(value)\n\nprint(math.sqrt(number))\n",[17,23273,23274,23280,23284,23296,23310,23314],{"__ignoreMap":50},[54,23275,23276,23278],{"class":56,"line":57},[54,23277,61],{"class":60},[54,23279,18340],{"class":64},[54,23281,23282],{"class":56,"line":68},[54,23283,72],{"emptyLinePlaceholder":71},[54,23285,23286,23288,23290,23292,23294],{"class":56,"line":75},[54,23287,5230],{"class":64},[54,23289,82],{"class":81},[54,23291,830],{"class":544},[54,23293,22330],{"class":548},[54,23295,5239],{"class":544},[54,23297,23298,23300,23302,23304,23306,23308],{"class":56,"line":112},[54,23299,78],{"class":64},[54,23301,82],{"class":81},[54,23303,20288],{"class":429},[54,23305,96],{"class":88},[54,23307,5248],{"class":92},[54,23309,109],{"class":88},[54,23311,23312],{"class":56,"line":887},[54,23313,72],{"emptyLinePlaceholder":71},[54,23315,23316,23318,23320,23322,23324,23326,23328,23330],{"class":56,"line":892},[54,23317,116],{"class":115},[54,23319,96],{"class":88},[54,23321,18295],{"class":92},[54,23323,89],{"class":88},[54,23325,21927],{"class":92},[54,23327,96],{"class":88},[54,23329,121],{"class":92},[54,23331,382],{"class":88},[602,23333,23335],{"id":23334},"using-a-negative-value-without-checking-first","Using a negative value without checking first",[14,23337,23338,23339,89],{},"If the value might be negative, test it before calling ",[17,23340,21078],{},[45,23342,23344],{"className":47,"code":23343,"language":49,"meta":50,"style":50},"import math\n\nvalue = -9\n\nif value >= 0:\n    print(math.sqrt(value))\nelse:\n    print(\"Value must be 0 or greater\")\n",[17,23345,23346,23352,23356,23366,23370,23382,23400,23406],{"__ignoreMap":50},[54,23347,23348,23350],{"class":56,"line":57},[54,23349,61],{"class":60},[54,23351,18340],{"class":64},[54,23353,23354],{"class":56,"line":68},[54,23355,72],{"emptyLinePlaceholder":71},[54,23357,23358,23360,23362,23364],{"class":56,"line":75},[54,23359,5230],{"class":64},[54,23361,82],{"class":81},[54,23363,879],{"class":81},[54,23365,4533],{"class":99},[54,23367,23368],{"class":56,"line":112},[54,23369,72],{"emptyLinePlaceholder":71},[54,23371,23372,23374,23376,23378,23380],{"class":56,"line":887},[54,23373,22942],{"class":60},[54,23375,22945],{"class":64},[54,23377,22948],{"class":81},[54,23379,3986],{"class":99},[54,23381,7572],{"class":88},[54,23383,23384,23386,23388,23390,23392,23394,23396,23398],{"class":56,"line":892},[54,23385,7606],{"class":115},[54,23387,96],{"class":88},[54,23389,18295],{"class":92},[54,23391,89],{"class":88},[54,23393,21927],{"class":92},[54,23395,96],{"class":88},[54,23397,5248],{"class":92},[54,23399,382],{"class":88},[54,23401,23402,23404],{"class":56,"line":913},[54,23403,22975],{"class":60},[54,23405,7572],{"class":88},[54,23407,23408,23410,23412,23414,23417,23419],{"class":56,"line":2121},[54,23409,7606],{"class":115},[54,23411,96],{"class":88},[54,23413,545],{"class":544},[54,23415,23416],{"class":548},"Value must be 0 or greater",[54,23418,545],{"class":544},[54,23420,109],{"class":88},[602,23422,23424],{"id":23423},"expecting-an-integer-but-getting-a-float","Expecting an integer but getting a float",[14,23426,23427],{},"This is normal behavior.",[45,23429,23431],{"className":47,"code":23430,"language":49,"meta":50,"style":50},"import math\n\nprint(math.sqrt(25))\n",[17,23432,23433,23439,23443],{"__ignoreMap":50},[54,23434,23435,23437],{"class":56,"line":57},[54,23436,61],{"class":60},[54,23438,18340],{"class":64},[54,23440,23441],{"class":56,"line":68},[54,23442,72],{"emptyLinePlaceholder":71},[54,23444,23445,23447,23449,23451,23453,23455,23457,23459],{"class":56,"line":75},[54,23446,116],{"class":115},[54,23448,96],{"class":88},[54,23450,18295],{"class":92},[54,23452,89],{"class":88},[54,23454,21927],{"class":92},[54,23456,96],{"class":88},[54,23458,22330],{"class":99},[54,23460,382],{"class":88},[14,23462,449],{},[45,23464,23466],{"className":47,"code":23465,"language":49,"meta":50,"style":50},"5.0\n",[17,23467,23468],{"__ignoreMap":50},[54,23469,23470],{"class":56,"line":57},[54,23471,23465],{"class":99},[40,23473,23475,23476],{"id":23474},"alternatives-to-mathsqrt","Alternatives to ",[17,23477,21078],{},[14,23479,23480,23481,89],{},"You can also calculate a square root with ",[17,23482,23483],{},"x ** 0.5",[14,23485,253],{},[45,23487,23489],{"className":47,"code":23488,"language":49,"meta":50,"style":50},"x = 25\nprint(x ** 0.5)\n",[17,23490,23491,23499],{"__ignoreMap":50},[54,23492,23493,23495,23497],{"class":56,"line":57},[54,23494,21725],{"class":64},[54,23496,82],{"class":81},[54,23498,22933],{"class":99},[54,23500,23501,23503,23505,23507,23509,23511],{"class":56,"line":68},[54,23502,116],{"class":115},[54,23504,96],{"class":88},[54,23506,21725],{"class":92},[54,23508,20504],{"class":81},[54,23510,21060],{"class":99},[54,23512,109],{"class":88},[14,23514,449],{},[45,23516,23517],{"className":47,"code":23465,"language":49,"meta":50,"style":50},[17,23518,23519],{"__ignoreMap":50},[54,23520,23521],{"class":56,"line":57},[54,23522,23465],{"class":99},[14,23524,23525,23526,23529],{},"This works, but ",[17,23527,23528],{},"math.sqrt(x)"," is often easier for beginners to read. It clearly shows that you want the square root.",[40,23531,1419],{"id":1418},[602,23533,23535,23536,23538,23539,3409],{"id":23534},"do-i-need-to-import-math-to-use-sqrt","Do I need to import ",[17,23537,18295],{}," to use ",[17,23540,23541],{},"sqrt()",[14,23543,1491,23544,23546,23547,89],{},[17,23545,20374],{},", then call ",[17,23548,22590],{},[602,23550,3403,23552,23554,23555,3360,23558,3409],{"id":23551},"why-does-mathsqrt9-return-30-instead-of-3",[17,23553,23099],{}," return ",[17,23556,23557],{},"3.0",[17,23559,401],{},[14,23561,3412,23562,22602],{},[17,23563,21078],{},[602,23565,8091,23567,23569],{"id":23566},"can-mathsqrt-work-with-negative-numbers",[17,23568,21078],{}," work with negative numbers?",[14,23571,23572,23573,23575,23576,89],{},"Not in the regular ",[17,23574,18295],{}," module for real numbers. It raises ",[17,23577,1306],{},[602,23579,3449,23581,23583,23584,3409],{"id":23580},"is-mathsqrt-better-than-x-05",[17,23582,21078],{}," better than ",[17,23585,23483],{},[14,23587,23588,23589,23591],{},"For beginners, ",[17,23590,21078],{}," is often clearer and easier to read.",[40,23593,1510],{"id":1509},[149,23595,23596,23600,23606,23610,23614],{},[152,23597,23598],{},[192,23599,20396],{"href":20700},[152,23601,23602],{},[192,23603,1541,23604],{"href":22903},[17,23605,22906],{},[152,23607,23608],{},[192,23609,22669],{"href":22668},[152,23611,23612],{},[192,23613,22673],{"href":22672},[152,23615,23616],{},[192,23617,22219],{"href":22218},[1545,23619,22226],{},{"title":50,"searchDepth":68,"depth":68,"links":23621},[23622,23623,23625,23626,23627,23628,23630,23631,23639,23641,23651],{"id":42,"depth":68,"text":43},{"id":22356,"depth":68,"text":23624},"What math.sqrt() does",{"id":198,"depth":68,"text":199},{"id":8531,"depth":68,"text":8532},{"id":22596,"depth":68,"text":22597},{"id":22736,"depth":68,"text":23629},"When to use math.sqrt()",{"id":22821,"depth":68,"text":22822},{"id":5209,"depth":68,"text":5210,"children":23632},[23633,23634,23636,23637,23638],{"id":19941,"depth":75,"text":20456},{"id":23092,"depth":75,"text":23635},"Writing sqrt(9) instead of math.sqrt(9)",{"id":23175,"depth":75,"text":23176},{"id":23334,"depth":75,"text":23335},{"id":23423,"depth":75,"text":23424},{"id":23474,"depth":68,"text":23640},"Alternatives to math.sqrt()",{"id":1418,"depth":68,"text":1419,"children":23642},[23643,23645,23647,23649],{"id":23534,"depth":75,"text":23644},"Do I need to import math to use sqrt()?",{"id":23551,"depth":75,"text":23646},"Why does math.sqrt(9) return 3.0 instead of 3?",{"id":23566,"depth":75,"text":23648},"Can math.sqrt() work with negative numbers?",{"id":23580,"depth":75,"text":23650},"Is math.sqrt() better than x ** 0.5?",{"id":1509,"depth":68,"text":1510},"Master math.sqrt function explained in our comprehensive Python beginner guide.",{},{"title":22275,"description":23652},"standard-library\u002Fmath.sqrt-function-explained","-jPkdrL2ePUy2oVBiBqbjh-07WkOv8K_T-6kq984eK8",{"id":23658,"title":23659,"body":23660,"description":25114,"extension":1577,"meta":25115,"navigation":71,"path":25116,"seo":25117,"stem":25118,"__hash__":25119},"content\u002Fstandard-library\u002Fos.chdir-function-explained.md","os.chdir() Function Explained",{"type":7,"value":23661,"toc":25087},[23662,23665,23671,23688,23694,23696,23762,23771,23775,23783,23786,23801,23804,23820,23823,23844,23850,23861,23875,23877,23879,23899,23920,23922,23955,23963,23965,23968,24056,24058,24110,24114,24135,24141,24145,24150,24154,24157,24204,24207,24220,24223,24227,24230,24278,24281,24358,24365,24369,24375,24386,24388,24436,24442,24445,24449,24452,24455,24458,24492,24495,24535,24541,24545,24548,24582,24587,24593,24597,24600,24634,24637,24643,24647,24652,24655,24675,24677,24848,24857,24859,24864,24881,24884,24908,24943,24966,25002,25005,25019,25021,25025,25031,25035,25040,25044,25047,25051,25054,25056,25084],[10,23663,23659],{"id":23664},"oschdir-function-explained",[14,23666,23667,23670],{},[17,23668,23669],{},"os.chdir()"," changes Python’s current working directory.",[14,23672,23673,23674,23677,23678,5507,23681,23684,23685,23687],{},"This matters because the current working directory is the folder Python uses when you give a ",[35,23675,23676],{},"relative path"," like ",[17,23679,23680],{},"\"data.txt\"",[17,23682,23683],{},"\"images\u002Fphoto.jpg\"",". After you change directories, file operations such as ",[17,23686,8524],{}," will use the new folder.",[14,23689,23690,23691,23693],{},"If you are learning how files and folders work in Python, ",[17,23692,23669],{}," can be useful. But it can also cause confusion if you are not sure which folder your program is currently using.",[40,23695,3596],{"id":3595},[45,23697,23699],{"className":47,"code":23698,"language":49,"meta":50,"style":50},"import os\n\nprint(os.getcwd())\nos.chdir(\"my_folder\")\nprint(os.getcwd())\n",[17,23700,23701,23708,23712,23728,23748],{"__ignoreMap":50},[54,23702,23703,23705],{"class":56,"line":57},[54,23704,61],{"class":60},[54,23706,23707],{"class":64}," os\n",[54,23709,23710],{"class":56,"line":68},[54,23711,72],{"emptyLinePlaceholder":71},[54,23713,23714,23716,23718,23721,23723,23726],{"class":56,"line":75},[54,23715,116],{"class":115},[54,23717,96],{"class":88},[54,23719,23720],{"class":92},"os",[54,23722,89],{"class":88},[54,23724,23725],{"class":92},"getcwd",[54,23727,1950],{"class":88},[54,23729,23730,23732,23734,23737,23739,23741,23744,23746],{"class":56,"line":112},[54,23731,23720],{"class":64},[54,23733,89],{"class":88},[54,23735,23736],{"class":92},"chdir",[54,23738,96],{"class":88},[54,23740,545],{"class":544},[54,23742,23743],{"class":548},"my_folder",[54,23745,545],{"class":544},[54,23747,109],{"class":88},[54,23749,23750,23752,23754,23756,23758,23760],{"class":56,"line":887},[54,23751,116],{"class":115},[54,23753,96],{"class":88},[54,23755,23720],{"class":92},[54,23757,89],{"class":88},[54,23759,23725],{"class":92},[54,23761,1950],{"class":88},[14,23763,126,23764,23767,23768,23770],{},[17,23765,23766],{},"os.getcwd()"," before and after ",[17,23769,23669],{}," to confirm that the working directory changed.",[40,23772,23774],{"id":23773},"what-oschdir-does","What os.chdir() does",[14,23776,23777,23779,23780,89],{},[17,23778,23669],{}," changes the program's ",[35,23781,23782],{},"current working directory",[14,23784,23785],{},"In simple terms:",[149,23787,23788,23791,23794],{},[152,23789,23790],{},"The working directory is the folder Python is currently “inside”",[152,23792,23793],{},"Relative file paths are based on that folder",[152,23795,23796,23797,23800],{},"After changing it, code like ",[17,23798,23799],{},"open(\"notes.txt\")"," looks in the new folder",[14,23802,23803],{},"For example, if your current folder is:",[45,23805,23807],{"className":47,"code":23806,"language":49,"meta":50,"style":50},"C:\\Users\\Sam\\project\n",[17,23808,23809],{"__ignoreMap":50},[54,23810,23811,23814,23817],{"class":56,"line":57},[54,23812,23813],{"class":64},"C",[54,23815,23816],{"class":88},":\\",[54,23818,23819],{"class":3275},"Users\\Sam\\project\n",[14,23821,23822],{},"and you run:",[45,23824,23826],{"className":47,"code":23825,"language":49,"meta":50,"style":50},"open(\"data.txt\")\n",[17,23827,23828],{"__ignoreMap":50},[54,23829,23830,23833,23835,23837,23840,23842],{"class":56,"line":57},[54,23831,23832],{"class":115},"open",[54,23834,96],{"class":88},[54,23836,545],{"class":544},[54,23838,23839],{"class":548},"data.txt",[54,23841,545],{"class":544},[54,23843,109],{"class":88},[14,23845,23846,23847,23849],{},"Python looks for ",[17,23848,23839],{}," inside that folder.",[14,23851,23852,23853,23856,23857,23860],{},"If you then use ",[17,23854,23855],{},"os.chdir(\"reports\")",", Python will start looking relative to the ",[17,23858,23859],{},"reports"," folder instead.",[14,23862,23863,23864,134,23869,23874],{},"If you are new to this idea, the ",[192,23865,23867,13763],{"href":23866},"\u002Fstandard-library\u002Fpython-os-module-overview",[17,23868,23720],{},[192,23870,23872,12898],{"href":23871},"\u002Fstandard-library\u002Fos.getcwd-function-explained",[17,23873,23766],{}," pages are good next steps.",[40,23876,199],{"id":198},[14,23878,20608],{},[45,23880,23882],{"className":47,"code":23881,"language":49,"meta":50,"style":50},"os.chdir(path)\n",[17,23883,23884],{"__ignoreMap":50},[54,23885,23886,23888,23890,23892,23894,23897],{"class":56,"line":57},[54,23887,23720],{"class":64},[54,23889,89],{"class":88},[54,23891,23736],{"class":92},[54,23893,96],{"class":88},[54,23895,23896],{"class":92},"path",[54,23898,109],{"class":88},[149,23900,23901,23906,23915],{},[152,23902,23903,23905],{},[17,23904,23896],{}," is the folder you want Python to switch to",[152,23907,23908,23909,5507,23912],{},"The path can be ",[35,23910,23911],{},"relative",[35,23913,23914],{},"absolute",[152,23916,19618,23917,23919],{},[17,23918,23720],{}," module first",[14,23921,253],{},[45,23923,23925],{"className":47,"code":23924,"language":49,"meta":50,"style":50},"import os\n\nos.chdir(\"my_folder\")\n",[17,23926,23927,23933,23937],{"__ignoreMap":50},[54,23928,23929,23931],{"class":56,"line":57},[54,23930,61],{"class":60},[54,23932,23707],{"class":64},[54,23934,23935],{"class":56,"line":68},[54,23936,72],{"emptyLinePlaceholder":71},[54,23938,23939,23941,23943,23945,23947,23949,23951,23953],{"class":56,"line":75},[54,23940,23720],{"class":64},[54,23942,89],{"class":88},[54,23944,23736],{"class":92},[54,23946,96],{"class":88},[54,23948,545],{"class":544},[54,23950,23743],{"class":548},[54,23952,545],{"class":544},[54,23954,109],{"class":88},[14,23956,23957,23958,19183,23960,23962],{},"One important detail: ",[17,23959,23669],{},[35,23961,3036],{}," return the new path. It only changes the current directory.",[40,23964,493],{"id":492},[14,23966,23967],{},"This example shows the current folder, changes to another folder, and then shows the new folder.",[45,23969,23971],{"className":47,"code":23970,"language":49,"meta":50,"style":50},"import os\n\nprint(\"Before:\", os.getcwd())\n\nos.chdir(\"my_folder\")\n\nprint(\"After:\", os.getcwd())\n",[17,23972,23973,23979,23983,24007,24011,24029,24033],{"__ignoreMap":50},[54,23974,23975,23977],{"class":56,"line":57},[54,23976,61],{"class":60},[54,23978,23707],{"class":64},[54,23980,23981],{"class":56,"line":68},[54,23982,72],{"emptyLinePlaceholder":71},[54,23984,23985,23987,23989,23991,23994,23996,23998,24001,24003,24005],{"class":56,"line":75},[54,23986,116],{"class":115},[54,23988,96],{"class":88},[54,23990,545],{"class":544},[54,23992,23993],{"class":548},"Before:",[54,23995,545],{"class":544},[54,23997,103],{"class":88},[54,23999,24000],{"class":92}," os",[54,24002,89],{"class":88},[54,24004,23725],{"class":92},[54,24006,1950],{"class":88},[54,24008,24009],{"class":56,"line":112},[54,24010,72],{"emptyLinePlaceholder":71},[54,24012,24013,24015,24017,24019,24021,24023,24025,24027],{"class":56,"line":887},[54,24014,23720],{"class":64},[54,24016,89],{"class":88},[54,24018,23736],{"class":92},[54,24020,96],{"class":88},[54,24022,545],{"class":544},[54,24024,23743],{"class":548},[54,24026,545],{"class":544},[54,24028,109],{"class":88},[54,24030,24031],{"class":56,"line":892},[54,24032,72],{"emptyLinePlaceholder":71},[54,24034,24035,24037,24039,24041,24044,24046,24048,24050,24052,24054],{"class":56,"line":913},[54,24036,116],{"class":115},[54,24038,96],{"class":88},[54,24040,545],{"class":544},[54,24042,24043],{"class":548},"After:",[54,24045,545],{"class":544},[54,24047,103],{"class":88},[54,24049,24000],{"class":92},[54,24051,89],{"class":88},[54,24053,23725],{"class":92},[54,24055,1950],{"class":88},[14,24057,561],{},[45,24059,24061],{"className":47,"code":24060,"language":49,"meta":50,"style":50},"Before: \u002FUsers\u002Falex\u002Fproject\nAfter: \u002FUsers\u002Falex\u002Fproject\u002Fmy_folder\n",[17,24062,24063,24085],{"__ignoreMap":50},[54,24064,24065,24068,24070,24072,24075,24077,24080,24082],{"class":56,"line":57},[54,24066,24067],{"class":64},"Before",[54,24069,147],{"class":88},[54,24071,21991],{"class":81},[54,24073,24074],{"class":64},"Users",[54,24076,6676],{"class":81},[54,24078,24079],{"class":64},"alex",[54,24081,6676],{"class":81},[54,24083,24084],{"class":64},"project\n",[54,24086,24087,24090,24092,24094,24096,24098,24100,24102,24105,24107],{"class":56,"line":68},[54,24088,24089],{"class":64},"After",[54,24091,147],{"class":88},[54,24093,21991],{"class":81},[54,24095,24074],{"class":64},[54,24097,6676],{"class":81},[54,24099,24079],{"class":64},[54,24101,6676],{"class":81},[54,24103,24104],{"class":64},"project",[54,24106,6676],{"class":81},[54,24108,24109],{"class":64},"my_folder\n",[602,24111,24113],{"id":24112},"what-the-code-does","What the code does",[149,24115,24116,24121,24129],{},[152,24117,24118,24120],{},[17,24119,23766],{}," gets the current working directory",[152,24122,24123,24126,24127],{},[17,24124,24125],{},"os.chdir(\"my_folder\")"," changes into a folder named ",[17,24128,23743],{},[152,24130,24131,24132,24134],{},"The second ",[17,24133,23766],{}," confirms the change",[14,24136,24137,24138,24140],{},"This only works if ",[17,24139,23743],{}," exists inside the current folder.",[40,24142,24144],{"id":24143},"using-relative-and-absolute-paths","Using relative and absolute paths",[14,24146,789,24147,24149],{},[17,24148,23669],{}," with either a relative path or an absolute path.",[602,24151,24153],{"id":24152},"relative-path","Relative path",[14,24155,24156],{},"A relative path is based on the current working directory.",[45,24158,24160],{"className":47,"code":24159,"language":49,"meta":50,"style":50},"import os\n\nos.chdir(\"my_folder\")\nprint(os.getcwd())\n",[17,24161,24162,24168,24172,24190],{"__ignoreMap":50},[54,24163,24164,24166],{"class":56,"line":57},[54,24165,61],{"class":60},[54,24167,23707],{"class":64},[54,24169,24170],{"class":56,"line":68},[54,24171,72],{"emptyLinePlaceholder":71},[54,24173,24174,24176,24178,24180,24182,24184,24186,24188],{"class":56,"line":75},[54,24175,23720],{"class":64},[54,24177,89],{"class":88},[54,24179,23736],{"class":92},[54,24181,96],{"class":88},[54,24183,545],{"class":544},[54,24185,23743],{"class":548},[54,24187,545],{"class":544},[54,24189,109],{"class":88},[54,24191,24192,24194,24196,24198,24200,24202],{"class":56,"line":112},[54,24193,116],{"class":115},[54,24195,96],{"class":88},[54,24197,23720],{"class":92},[54,24199,89],{"class":88},[54,24201,23725],{"class":92},[54,24203,1950],{"class":88},[14,24205,24206],{},"This means:",[149,24208,24209,24212,24217],{},[152,24210,24211],{},"Start from the current folder",[152,24213,24214,24215],{},"Look for a folder called ",[17,24216,23743],{},[152,24218,24219],{},"Move into it",[14,24221,24222],{},"If the current folder is not what you expect, the relative path may fail.",[602,24224,24226],{"id":24225},"absolute-path","Absolute path",[14,24228,24229],{},"An absolute path gives the full location of the folder.",[45,24231,24233],{"className":47,"code":24232,"language":49,"meta":50,"style":50},"import os\n\nos.chdir(\"\u002FUsers\u002Falex\u002Fproject\u002Fmy_folder\")\nprint(os.getcwd())\n",[17,24234,24235,24241,24245,24264],{"__ignoreMap":50},[54,24236,24237,24239],{"class":56,"line":57},[54,24238,61],{"class":60},[54,24240,23707],{"class":64},[54,24242,24243],{"class":56,"line":68},[54,24244,72],{"emptyLinePlaceholder":71},[54,24246,24247,24249,24251,24253,24255,24257,24260,24262],{"class":56,"line":75},[54,24248,23720],{"class":64},[54,24250,89],{"class":88},[54,24252,23736],{"class":92},[54,24254,96],{"class":88},[54,24256,545],{"class":544},[54,24258,24259],{"class":548},"\u002FUsers\u002Falex\u002Fproject\u002Fmy_folder",[54,24261,545],{"class":544},[54,24263,109],{"class":88},[54,24265,24266,24268,24270,24272,24274,24276],{"class":56,"line":112},[54,24267,116],{"class":115},[54,24269,96],{"class":88},[54,24271,23720],{"class":92},[54,24273,89],{"class":88},[54,24275,23725],{"class":92},[54,24277,1950],{"class":88},[14,24279,24280],{},"On Windows, it might look like this:",[45,24282,24284],{"className":47,"code":24283,"language":49,"meta":50,"style":50},"import os\n\nos.chdir(r\"C:\\Users\\Alex\\project\\my_folder\")\nprint(os.getcwd())\n",[17,24285,24286,24292,24296,24344],{"__ignoreMap":50},[54,24287,24288,24290],{"class":56,"line":57},[54,24289,61],{"class":60},[54,24291,23707],{"class":64},[54,24293,24294],{"class":56,"line":68},[54,24295,72],{"emptyLinePlaceholder":71},[54,24297,24298,24300,24302,24304,24306,24308,24310,24314,24318,24321,24325,24328,24331,24334,24337,24340,24342],{"class":56,"line":75},[54,24299,23720],{"class":64},[54,24301,89],{"class":88},[54,24303,23736],{"class":92},[54,24305,96],{"class":88},[54,24307,10658],{"class":2230},[54,24309,545],{"class":544},[54,24311,24313],{"class":24312},"sQRbd","C:",[54,24315,24317],{"class":24316},"sjYin","\\U",[54,24319,24320],{"class":24312},"sers",[54,24322,24324],{"class":24323},"stzsN","\\A",[54,24326,24327],{"class":24312},"lex",[54,24329,24330],{"class":24316},"\\p",[54,24332,24333],{"class":24312},"roject",[54,24335,24336],{"class":24316},"\\m",[54,24338,24339],{"class":24312},"y_folder",[54,24341,545],{"class":544},[54,24343,109],{"class":88},[54,24345,24346,24348,24350,24352,24354,24356],{"class":56,"line":112},[54,24347,116],{"class":115},[54,24349,96],{"class":88},[54,24351,23720],{"class":92},[54,24353,89],{"class":88},[54,24355,23725],{"class":92},[54,24357,1950],{"class":88},[14,24359,24360,24361,89],{},"Absolute paths are often easier to debug because they do not depend on the current folder. If file paths are still confusing, see ",[192,24362,24364],{"href":24363},"\u002Flearn\u002Fworking-with-file-paths-in-python\u002F","working with file paths in Python",[40,24366,24368],{"id":24367},"why-beginners-use-oschdir","Why beginners use os.chdir()",[14,24370,24371,24372,24374],{},"Beginners often use ",[17,24373,23669],{}," for simple tasks like these:",[149,24376,24377,24380,24383],{},[152,24378,24379],{},"To make file reading and writing easier in small scripts",[152,24381,24382],{},"To switch into a project folder before listing files",[152,24384,24385],{},"To work with folders step by step while learning",[14,24387,253],{},[45,24389,24391],{"className":47,"code":24390,"language":49,"meta":50,"style":50},"import os\n\nos.chdir(\"my_folder\")\nprint(os.listdir())\n",[17,24392,24393,24399,24403,24421],{"__ignoreMap":50},[54,24394,24395,24397],{"class":56,"line":57},[54,24396,61],{"class":60},[54,24398,23707],{"class":64},[54,24400,24401],{"class":56,"line":68},[54,24402,72],{"emptyLinePlaceholder":71},[54,24404,24405,24407,24409,24411,24413,24415,24417,24419],{"class":56,"line":75},[54,24406,23720],{"class":64},[54,24408,89],{"class":88},[54,24410,23736],{"class":92},[54,24412,96],{"class":88},[54,24414,545],{"class":544},[54,24416,23743],{"class":548},[54,24418,545],{"class":544},[54,24420,109],{"class":88},[54,24422,24423,24425,24427,24429,24431,24434],{"class":56,"line":112},[54,24424,116],{"class":115},[54,24426,96],{"class":88},[54,24428,23720],{"class":92},[54,24430,89],{"class":88},[54,24432,24433],{"class":92},"listdir",[54,24435,1950],{"class":88},[14,24437,24438,24439,24441],{},"This changes into ",[17,24440,23743],{}," and then lists the files inside it.",[14,24443,24444],{},"For small practice scripts, this can be convenient. In larger programs, changing directories too often can make code harder to understand.",[40,24446,24448],{"id":24447},"common-errors-when-using-oschdir","Common errors when using os.chdir()",[14,24450,24451],{},"A few common errors happen when the path is wrong or inaccessible.",[602,24453,15337],{"id":24454},"filenotfounderror",[14,24456,24457],{},"This happens when the folder does not exist.",[45,24459,24461],{"className":47,"code":24460,"language":49,"meta":50,"style":50},"import os\n\nos.chdir(\"missing_folder\")\n",[17,24462,24463,24469,24473],{"__ignoreMap":50},[54,24464,24465,24467],{"class":56,"line":57},[54,24466,61],{"class":60},[54,24468,23707],{"class":64},[54,24470,24471],{"class":56,"line":68},[54,24472,72],{"emptyLinePlaceholder":71},[54,24474,24475,24477,24479,24481,24483,24485,24488,24490],{"class":56,"line":75},[54,24476,23720],{"class":64},[54,24478,89],{"class":88},[54,24480,23736],{"class":92},[54,24482,96],{"class":88},[54,24484,545],{"class":544},[54,24486,24487],{"class":548},"missing_folder",[54,24489,545],{"class":544},[54,24491,109],{"class":88},[14,24493,24494],{},"Python may show an error like:",[45,24496,24498],{"className":47,"code":24497,"language":49,"meta":50,"style":50},"FileNotFoundError: [Errno 2] No such file or directory: 'missing_folder'\n",[17,24499,24500],{"__ignoreMap":50},[54,24501,24502,24504,24506,24508,24511,24513,24516,24519,24521,24524,24527,24529,24531,24533],{"class":56,"line":57},[54,24503,15337],{"class":429},[54,24505,147],{"class":88},[54,24507,818],{"class":88},[54,24509,24510],{"class":64},"Errno ",[54,24512,396],{"class":99},[54,24514,24515],{"class":88},"]",[54,24517,24518],{"class":64}," No such ",[54,24520,8412],{"class":8295},[54,24522,24523],{"class":81}," or",[54,24525,24526],{"class":64}," directory",[54,24528,147],{"class":88},[54,24530,2234],{"class":544},[54,24532,24487],{"class":548},[54,24534,5297],{"class":544},[14,24536,13462,24537,89],{},[192,24538,24540],{"href":24539},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","how to fix FileNotFoundError: No such file or directory",[602,24542,24544],{"id":24543},"notadirectoryerror","NotADirectoryError",[14,24546,24547],{},"This happens when the path points to a file instead of a folder.",[45,24549,24551],{"className":47,"code":24550,"language":49,"meta":50,"style":50},"import os\n\nos.chdir(\"notes.txt\")\n",[17,24552,24553,24559,24563],{"__ignoreMap":50},[54,24554,24555,24557],{"class":56,"line":57},[54,24556,61],{"class":60},[54,24558,23707],{"class":64},[54,24560,24561],{"class":56,"line":68},[54,24562,72],{"emptyLinePlaceholder":71},[54,24564,24565,24567,24569,24571,24573,24575,24578,24580],{"class":56,"line":75},[54,24566,23720],{"class":64},[54,24568,89],{"class":88},[54,24570,23736],{"class":92},[54,24572,96],{"class":88},[54,24574,545],{"class":544},[54,24576,24577],{"class":548},"notes.txt",[54,24579,545],{"class":544},[54,24581,109],{"class":88},[14,24583,7429,24584,24586],{},[17,24585,24577],{}," is a file, not a directory, Python raises this error.",[14,24588,13462,24589,89],{},[192,24590,24592],{"href":24591},"\u002Ferrors\u002Fnotadirectoryerror-errno-20-not-a-directory-fix\u002F","how to fix NotADirectoryError",[602,24594,24596],{"id":24595},"permissionerror","PermissionError",[14,24598,24599],{},"This happens when Python does not have permission to enter the folder.",[45,24601,24603],{"className":47,"code":24602,"language":49,"meta":50,"style":50},"import os\n\nos.chdir(\"\u002Froot\")\n",[17,24604,24605,24611,24615],{"__ignoreMap":50},[54,24606,24607,24609],{"class":56,"line":57},[54,24608,61],{"class":60},[54,24610,23707],{"class":64},[54,24612,24613],{"class":56,"line":68},[54,24614,72],{"emptyLinePlaceholder":71},[54,24616,24617,24619,24621,24623,24625,24627,24630,24632],{"class":56,"line":75},[54,24618,23720],{"class":64},[54,24620,89],{"class":88},[54,24622,23736],{"class":92},[54,24624,96],{"class":88},[54,24626,545],{"class":544},[54,24628,24629],{"class":548},"\u002Froot",[54,24631,545],{"class":544},[54,24633,109],{"class":88},[14,24635,24636],{},"Some system folders are restricted.",[14,24638,13462,24639,89],{},[192,24640,24642],{"href":24641},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix\u002F","how to fix PermissionError: permission denied",[40,24644,24646],{"id":24645},"safer-ways-to-work-with-paths","Safer ways to work with paths",[14,24648,24649,24651],{},[17,24650,23669],{}," is useful, but it helps to be careful.",[14,24653,24654],{},"Here are some safer habits:",[149,24656,24657,24663,24669,24672],{},[152,24658,24659,24660,24662],{},"Check the current folder with ",[17,24661,23766],{}," before changing it",[152,24664,126,24665,24668],{},[17,24666,24667],{},"os.path.exists()"," to check whether a path exists",[152,24670,24671],{},"Consider using full paths when debugging",[152,24673,24674],{},"Avoid changing directories many times in one script",[14,24676,253],{},[45,24678,24680],{"className":47,"code":24679,"language":49,"meta":50,"style":50},"import os\n\nfolder = \"my_folder\"\n\nprint(\"Current folder:\", os.getcwd())\nprint(\"Exists:\", os.path.exists(folder))\n\nif os.path.exists(folder):\n    os.chdir(folder)\n    print(\"Changed to:\", os.getcwd())\nelse:\n    print(\"Folder not found\")\n",[17,24681,24682,24688,24692,24705,24709,24732,24765,24769,24789,24804,24827,24833],{"__ignoreMap":50},[54,24683,24684,24686],{"class":56,"line":57},[54,24685,61],{"class":60},[54,24687,23707],{"class":64},[54,24689,24690],{"class":56,"line":68},[54,24691,72],{"emptyLinePlaceholder":71},[54,24693,24694,24697,24699,24701,24703],{"class":56,"line":75},[54,24695,24696],{"class":64},"folder ",[54,24698,82],{"class":81},[54,24700,830],{"class":544},[54,24702,23743],{"class":548},[54,24704,5239],{"class":544},[54,24706,24707],{"class":56,"line":112},[54,24708,72],{"emptyLinePlaceholder":71},[54,24710,24711,24713,24715,24717,24720,24722,24724,24726,24728,24730],{"class":56,"line":887},[54,24712,116],{"class":115},[54,24714,96],{"class":88},[54,24716,545],{"class":544},[54,24718,24719],{"class":548},"Current folder:",[54,24721,545],{"class":544},[54,24723,103],{"class":88},[54,24725,24000],{"class":92},[54,24727,89],{"class":88},[54,24729,23725],{"class":92},[54,24731,1950],{"class":88},[54,24733,24734,24736,24738,24740,24743,24745,24747,24749,24751,24753,24755,24758,24760,24763],{"class":56,"line":892},[54,24735,116],{"class":115},[54,24737,96],{"class":88},[54,24739,545],{"class":544},[54,24741,24742],{"class":548},"Exists:",[54,24744,545],{"class":544},[54,24746,103],{"class":88},[54,24748,24000],{"class":92},[54,24750,89],{"class":88},[54,24752,23896],{"class":1693},[54,24754,89],{"class":88},[54,24756,24757],{"class":92},"exists",[54,24759,96],{"class":88},[54,24761,24762],{"class":92},"folder",[54,24764,382],{"class":88},[54,24766,24767],{"class":56,"line":913},[54,24768,72],{"emptyLinePlaceholder":71},[54,24770,24771,24773,24775,24777,24779,24781,24783,24785,24787],{"class":56,"line":2121},[54,24772,22942],{"class":60},[54,24774,24000],{"class":64},[54,24776,89],{"class":88},[54,24778,23896],{"class":1693},[54,24780,89],{"class":88},[54,24782,24757],{"class":92},[54,24784,96],{"class":88},[54,24786,24762],{"class":92},[54,24788,10837],{"class":88},[54,24790,24791,24794,24796,24798,24800,24802],{"class":56,"line":2136},[54,24792,24793],{"class":64},"    os",[54,24795,89],{"class":88},[54,24797,23736],{"class":92},[54,24799,96],{"class":88},[54,24801,24762],{"class":92},[54,24803,109],{"class":88},[54,24805,24806,24808,24810,24812,24815,24817,24819,24821,24823,24825],{"class":56,"line":2151},[54,24807,7606],{"class":115},[54,24809,96],{"class":88},[54,24811,545],{"class":544},[54,24813,24814],{"class":548},"Changed to:",[54,24816,545],{"class":544},[54,24818,103],{"class":88},[54,24820,24000],{"class":92},[54,24822,89],{"class":88},[54,24824,23725],{"class":92},[54,24826,1950],{"class":88},[54,24828,24829,24831],{"class":56,"line":4442},[54,24830,22975],{"class":60},[54,24832,7572],{"class":88},[54,24834,24835,24837,24839,24841,24844,24846],{"class":56,"line":4468},[54,24836,7606],{"class":115},[54,24838,96],{"class":88},[54,24840,545],{"class":544},[54,24842,24843],{"class":548},"Folder not found",[54,24845,545],{"class":544},[54,24847,109],{"class":88},[14,24849,24850,24851,24856],{},"You can learn more about ",[192,24852,24854],{"href":24853},"\u002Fstandard-library\u002Fos.path.exists-function-explained",[17,24855,24667],{}," if you want to check paths before using them.",[40,24858,978],{"id":977},[14,24860,24861,24862,147],{},"These are the most common causes of problems with ",[17,24863,23669],{},[149,24865,24866,24869,24872,24875,24878],{},[152,24867,24868],{},"Trying to change to a folder that does not exist",[152,24870,24871],{},"Using the wrong relative path",[152,24873,24874],{},"Forgetting which folder the script started in",[152,24876,24877],{},"Passing a file path instead of a directory path",[152,24879,24880],{},"Using a path that Python does not have permission to access",[14,24882,24883],{},"Helpful debugging commands:",[45,24885,24887],{"className":47,"code":24886,"language":49,"meta":50,"style":50},"import os; print(os.getcwd())\n",[17,24888,24889],{"__ignoreMap":50},[54,24890,24891,24893,24896,24898,24900,24902,24904,24906],{"class":56,"line":57},[54,24892,61],{"class":60},[54,24894,24895],{"class":64}," os; ",[54,24897,116],{"class":115},[54,24899,96],{"class":88},[54,24901,23720],{"class":92},[54,24903,89],{"class":88},[54,24905,23725],{"class":92},[54,24907,1950],{"class":88},[45,24909,24911],{"className":47,"code":24910,"language":49,"meta":50,"style":50},"import os; print(os.path.exists('my_folder'))\n",[17,24912,24913],{"__ignoreMap":50},[54,24914,24915,24917,24919,24921,24923,24925,24927,24929,24931,24933,24935,24937,24939,24941],{"class":56,"line":57},[54,24916,61],{"class":60},[54,24918,24895],{"class":64},[54,24920,116],{"class":115},[54,24922,96],{"class":88},[54,24924,23720],{"class":92},[54,24926,89],{"class":88},[54,24928,23896],{"class":1693},[54,24930,89],{"class":88},[54,24932,24757],{"class":92},[54,24934,96],{"class":88},[54,24936,2240],{"class":544},[54,24938,23743],{"class":548},[54,24940,2240],{"class":544},[54,24942,382],{"class":88},[45,24944,24946],{"className":47,"code":24945,"language":49,"meta":50,"style":50},"import os; print(os.listdir())\n",[17,24947,24948],{"__ignoreMap":50},[54,24949,24950,24952,24954,24956,24958,24960,24962,24964],{"class":56,"line":57},[54,24951,61],{"class":60},[54,24953,24895],{"class":64},[54,24955,116],{"class":115},[54,24957,96],{"class":88},[54,24959,23720],{"class":92},[54,24961,89],{"class":88},[54,24963,24433],{"class":92},[54,24965,1950],{"class":88},[45,24967,24969],{"className":47,"code":24968,"language":49,"meta":50,"style":50},"import os; print(os.path.isdir('my_folder'))\n",[17,24970,24971],{"__ignoreMap":50},[54,24972,24973,24975,24977,24979,24981,24983,24985,24987,24989,24992,24994,24996,24998,25000],{"class":56,"line":57},[54,24974,61],{"class":60},[54,24976,24895],{"class":64},[54,24978,116],{"class":115},[54,24980,96],{"class":88},[54,24982,23720],{"class":92},[54,24984,89],{"class":88},[54,24986,23896],{"class":1693},[54,24988,89],{"class":88},[54,24990,24991],{"class":92},"isdir",[54,24993,96],{"class":88},[54,24995,2240],{"class":544},[54,24997,23743],{"class":548},[54,24999,2240],{"class":544},[54,25001,382],{"class":88},[14,25003,25004],{},"These commands help you answer basic questions:",[149,25006,25007,25010,25013,25016],{},[152,25008,25009],{},"Where am I right now?",[152,25011,25012],{},"Does this path exist?",[152,25014,25015],{},"What files and folders are here?",[152,25017,25018],{},"Is this path a directory?",[40,25020,1419],{"id":1418},[602,25022,25024],{"id":25023},"what-does-oschdir-return","What does os.chdir() return?",[14,25026,25027,25028,25030],{},"It returns ",[17,25029,8775],{},". It changes the current working directory but does not return the new path.",[602,25032,25034],{"id":25033},"how-do-i-check-the-current-working-directory","How do I check the current working directory?",[14,25036,126,25037,25039],{},[17,25038,23766],{}," to print the current working directory.",[602,25041,25043],{"id":25042},"can-i-use-oschdir-with-a-relative-path","Can I use os.chdir() with a relative path?",[14,25045,25046],{},"Yes. Python will interpret the path relative to the current working directory.",[602,25048,25050],{"id":25049},"why-does-oschdir-break-my-file-code","Why does os.chdir() break my file code?",[14,25052,25053],{},"Changing the working directory changes how relative paths are resolved, so files may no longer be found where your code expects them.",[40,25055,1510],{"id":1509},[149,25057,25058,25063,25068,25073,25078],{},[152,25059,25060],{},[192,25061,25062],{"href":23866},"os module overview",[152,25064,25065],{},[192,25066,25067],{"href":23871},"os.getcwd() function explained",[152,25069,25070],{},[192,25071,25072],{"href":24853},"os.path.exists() function explained",[152,25074,25075],{},[192,25076,25077],{"href":24363},"Working with file paths in Python",[152,25079,25080],{},[192,25081,25083],{"href":25082},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python\u002F","How to check if a file exists in Python",[1545,25085,25086],{},"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 .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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sQRbd, html code.shiki .sQRbd{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#DBEDFF}html pre.shiki code .sjYin, html code.shiki .sjYin{--shiki-light:#90A4AE;--shiki-light-font-weight:inherit;--shiki-default:#22863A;--shiki-default-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold}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}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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}",{"title":50,"searchDepth":68,"depth":68,"links":25088},[25089,25090,25091,25092,25095,25099,25100,25105,25106,25107,25113],{"id":3595,"depth":68,"text":3596},{"id":23773,"depth":68,"text":23774},{"id":198,"depth":68,"text":199},{"id":492,"depth":68,"text":493,"children":25093},[25094],{"id":24112,"depth":75,"text":24113},{"id":24143,"depth":68,"text":24144,"children":25096},[25097,25098],{"id":24152,"depth":75,"text":24153},{"id":24225,"depth":75,"text":24226},{"id":24367,"depth":68,"text":24368},{"id":24447,"depth":68,"text":24448,"children":25101},[25102,25103,25104],{"id":24454,"depth":75,"text":15337},{"id":24543,"depth":75,"text":24544},{"id":24595,"depth":75,"text":24596},{"id":24645,"depth":68,"text":24646},{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":25108},[25109,25110,25111,25112],{"id":25023,"depth":75,"text":25024},{"id":25033,"depth":75,"text":25034},{"id":25042,"depth":75,"text":25043},{"id":25049,"depth":75,"text":25050},{"id":1509,"depth":68,"text":1510},"Master os.chdir function explained in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fos.chdir-function-explained",{"title":23659,"description":25114},"standard-library\u002Fos.chdir-function-explained","o-fwz80q92MH2hmSRWtxCtWfTJi2tFZ8YK91rv_QD48",{"id":25121,"title":25122,"body":25123,"description":26171,"extension":1577,"meta":26172,"navigation":71,"path":23871,"seo":26173,"stem":26174,"__hash__":26175},"content\u002Fstandard-library\u002Fos.getcwd-function-explained.md","os.getcwd() Function Explained",{"type":7,"value":25124,"toc":26149},[25125,25128,25133,25139,25141,25182,25187,25191,25195,25209,25215,25217,25226,25264,25266,25287,25291,25294,25308,25311,25333,25336,25340,25346,25348,25397,25406,25409,25413,25499,25505,25508,25522,25528,25532,25535,25541,25554,25557,25592,25595,25611,25617,25631,25637,25640,25644,25649,25663,25665,25752,25762,25766,25771,25794,25796,25898,25904,25906,25911,25931,25934,26011,26016,26019,26071,26082,26084,26088,26091,26095,26098,26102,26105,26109,26112,26114,26146],[10,25126,25122],{"id":25127},"osgetcwd-function-explained",[14,25129,25130,25132],{},[17,25131,23766],{}," returns the current working directory in Python.",[14,25134,25135,25136,25138],{},"This is useful when you want to know which folder Python is currently using. It matters because relative file paths are based on this folder. If your code opens or saves files using names like ",[17,25137,23680],{},", Python will look in the current working directory unless you give a full path.",[40,25140,43],{"id":42},[45,25142,25144],{"className":47,"code":25143,"language":49,"meta":50,"style":50},"import os\n\ncurrent_folder = os.getcwd()\nprint(current_folder)\n",[17,25145,25146,25152,25156,25171],{"__ignoreMap":50},[54,25147,25148,25150],{"class":56,"line":57},[54,25149,61],{"class":60},[54,25151,23707],{"class":64},[54,25153,25154],{"class":56,"line":68},[54,25155,72],{"emptyLinePlaceholder":71},[54,25157,25158,25161,25163,25165,25167,25169],{"class":56,"line":75},[54,25159,25160],{"class":64},"current_folder ",[54,25162,82],{"class":81},[54,25164,24000],{"class":64},[54,25166,89],{"class":88},[54,25168,23725],{"class":92},[54,25170,1670],{"class":88},[54,25172,25173,25175,25177,25180],{"class":56,"line":112},[54,25174,116],{"class":115},[54,25176,96],{"class":88},[54,25178,25179],{"class":92},"current_folder",[54,25181,109],{"class":88},[14,25183,126,25184,25186],{},[17,25185,23766],{}," to get the current working directory as a string.",[40,25188,25190],{"id":25189},"what-osgetcwd-does","What os.getcwd() does",[14,25192,25193,147],{},[17,25194,23766],{},[149,25196,25197,25200,25203,25206],{},[152,25198,25199],{},"Returns the current working directory",[152,25201,25202],{},"Gives the result as a string path",[152,25204,25205],{},"Shows the folder Python is currently using",[152,25207,25208],{},"Is often helpful before opening or saving files",[14,25210,25211,25212],{},"In simple terms, it tells you: ",[35,25213,25214],{},"“What folder is Python working in right now?”",[40,25216,2258],{"id":2257},[14,25218,25219,25220,25222,25223,25225],{},"First, import the ",[17,25221,23720],{}," module. Then call ",[17,25224,23766],{}," with no arguments.",[45,25227,25228],{"className":47,"code":25143,"language":49,"meta":50,"style":50},[17,25229,25230,25236,25240,25254],{"__ignoreMap":50},[54,25231,25232,25234],{"class":56,"line":57},[54,25233,61],{"class":60},[54,25235,23707],{"class":64},[54,25237,25238],{"class":56,"line":68},[54,25239,72],{"emptyLinePlaceholder":71},[54,25241,25242,25244,25246,25248,25250,25252],{"class":56,"line":75},[54,25243,25160],{"class":64},[54,25245,82],{"class":81},[54,25247,24000],{"class":64},[54,25249,89],{"class":88},[54,25251,23725],{"class":92},[54,25253,1670],{"class":88},[54,25255,25256,25258,25260,25262],{"class":56,"line":112},[54,25257,116],{"class":115},[54,25259,96],{"class":88},[54,25261,25179],{"class":92},[54,25263,109],{"class":88},[602,25265,8670],{"id":8669},[149,25267,25268,25277,25281],{},[152,25269,25270,25273,25274,25276],{},[17,25271,25272],{},"import os"," makes the ",[17,25275,23720],{}," module available",[152,25278,25279,24120],{},[17,25280,23766],{},[152,25282,25283,25286],{},[17,25284,25285],{},"print(current_folder)"," shows that path on the screen",[602,25288,25290],{"id":25289},"example-output","Example output",[14,25292,25293],{},"On Windows, you might see:",[45,25295,25297],{"className":47,"code":25296,"language":49,"meta":50,"style":50},"C:\\Users\\Alice\\Projects\n",[17,25298,25299],{"__ignoreMap":50},[54,25300,25301,25303,25305],{"class":56,"line":57},[54,25302,23813],{"class":64},[54,25304,23816],{"class":88},[54,25306,25307],{"class":3275},"Users\\Alice\\Projects\n",[14,25309,25310],{},"On macOS or Linux, you might see:",[45,25312,25314],{"className":47,"code":25313,"language":49,"meta":50,"style":50},"\u002Fhome\u002Falice\u002Fprojects\n",[17,25315,25316],{"__ignoreMap":50},[54,25317,25318,25320,25323,25325,25328,25330],{"class":56,"line":57},[54,25319,6676],{"class":81},[54,25321,25322],{"class":64},"home",[54,25324,6676],{"class":81},[54,25326,25327],{"class":64},"alice",[54,25329,6676],{"class":81},[54,25331,25332],{"class":64},"projects\n",[14,25334,25335],{},"The exact result depends on where Python is running.",[40,25337,25339],{"id":25338},"why-the-current-working-directory-matters","Why the current working directory matters",[14,25341,25342,25343,89],{},"The current working directory is important because ",[35,25344,25345],{},"relative paths depend on it",[14,25347,415],{},[45,25349,25351],{"className":47,"code":25350,"language":49,"meta":50,"style":50},"with open(\"notes.txt\", \"r\") as file:\n    print(file.read())\n",[17,25352,25353,25383],{"__ignoreMap":50},[54,25354,25355,25357,25359,25361,25363,25365,25367,25369,25371,25373,25375,25377,25379,25381],{"class":56,"line":57},[54,25356,8267],{"class":60},[54,25358,8270],{"class":115},[54,25360,96],{"class":88},[54,25362,545],{"class":544},[54,25364,24577],{"class":548},[54,25366,545],{"class":544},[54,25368,103],{"class":88},[54,25370,830],{"class":544},[54,25372,10658],{"class":548},[54,25374,545],{"class":544},[54,25376,876],{"class":88},[54,25378,7631],{"class":60},[54,25380,8296],{"class":8295},[54,25382,7572],{"class":88},[54,25384,25385,25387,25389,25391,25393,25395],{"class":56,"line":68},[54,25386,7606],{"class":115},[54,25388,96],{"class":88},[54,25390,8412],{"class":8295},[54,25392,89],{"class":88},[54,25394,17821],{"class":92},[54,25396,1950],{"class":88},[14,25398,25399,25400,25402,25403,25405],{},"This code does ",[35,25401,3036],{}," look everywhere on your computer for ",[17,25404,24577],{},". It looks in the current working directory.",[14,25407,25408],{},"If Python is running in a different folder than you expected, you may get a file error.",[602,25410,25412],{"id":25411},"debugging-example","Debugging example",[45,25414,25416],{"className":47,"code":25415,"language":49,"meta":50,"style":50},"import os\n\nprint(\"Current working directory:\", os.getcwd())\n\nwith open(\"notes.txt\", \"r\") as file:\n    print(file.read())\n",[17,25417,25418,25424,25428,25451,25455,25485],{"__ignoreMap":50},[54,25419,25420,25422],{"class":56,"line":57},[54,25421,61],{"class":60},[54,25423,23707],{"class":64},[54,25425,25426],{"class":56,"line":68},[54,25427,72],{"emptyLinePlaceholder":71},[54,25429,25430,25432,25434,25436,25439,25441,25443,25445,25447,25449],{"class":56,"line":75},[54,25431,116],{"class":115},[54,25433,96],{"class":88},[54,25435,545],{"class":544},[54,25437,25438],{"class":548},"Current working directory:",[54,25440,545],{"class":544},[54,25442,103],{"class":88},[54,25444,24000],{"class":92},[54,25446,89],{"class":88},[54,25448,23725],{"class":92},[54,25450,1950],{"class":88},[54,25452,25453],{"class":56,"line":112},[54,25454,72],{"emptyLinePlaceholder":71},[54,25456,25457,25459,25461,25463,25465,25467,25469,25471,25473,25475,25477,25479,25481,25483],{"class":56,"line":887},[54,25458,8267],{"class":60},[54,25460,8270],{"class":115},[54,25462,96],{"class":88},[54,25464,545],{"class":544},[54,25466,24577],{"class":548},[54,25468,545],{"class":544},[54,25470,103],{"class":88},[54,25472,830],{"class":544},[54,25474,10658],{"class":548},[54,25476,545],{"class":544},[54,25478,876],{"class":88},[54,25480,7631],{"class":60},[54,25482,8296],{"class":8295},[54,25484,7572],{"class":88},[54,25486,25487,25489,25491,25493,25495,25497],{"class":56,"line":892},[54,25488,7606],{"class":115},[54,25490,96],{"class":88},[54,25492,8412],{"class":8295},[54,25494,89],{"class":88},[54,25496,17821],{"class":92},[54,25498,1950],{"class":88},[14,25500,25501,25502,25504],{},"If the file cannot be found, printing ",[17,25503,23766],{}," helps you check where Python is looking.",[14,25506,25507],{},"This is especially helpful when running code from:",[149,25509,25510,25513,25516,25519],{},[152,25511,25512],{},"A terminal",[152,25514,25515],{},"An IDE",[152,25517,25518],{},"A code editor run button",[152,25520,25521],{},"A notebook environment",[14,25523,25524,25525,89],{},"If you are working with file paths, see ",[192,25526,24364],{"href":25527},"\u002Flearn\u002Fworking-with-file-paths-in-python",[40,25529,25531],{"id":25530},"osgetcwd-vs-script-location","os.getcwd() vs script location",[14,25533,25534],{},"Beginners often confuse the current working directory with the folder where the Python file is saved.",[14,25536,25537,25538,89],{},"They are ",[35,25539,25540],{},"not always the same",[149,25542,25543,25548],{},[152,25544,25545,25547],{},[17,25546,23766],{}," gives the current working folder",[152,25549,25550,25551,25553],{},"It does ",[35,25552,3036],{}," necessarily give the script file location",[14,25555,25556],{},"For example, suppose your file is saved here:",[45,25558,25560],{"className":47,"code":25559,"language":49,"meta":50,"style":50},"\u002Fhome\u002Falice\u002Fprojects\u002Fapp\u002Fscript.py\n",[17,25561,25562],{"__ignoreMap":50},[54,25563,25564,25566,25568,25570,25572,25574,25577,25579,25582,25584,25587,25589],{"class":56,"line":57},[54,25565,6676],{"class":81},[54,25567,25322],{"class":64},[54,25569,6676],{"class":81},[54,25571,25327],{"class":64},[54,25573,6676],{"class":81},[54,25575,25576],{"class":64},"projects",[54,25578,6676],{"class":81},[54,25580,25581],{"class":64},"app",[54,25583,6676],{"class":81},[54,25585,25586],{"class":64},"script",[54,25588,89],{"class":88},[54,25590,25591],{"class":1693},"py\n",[14,25593,25594],{},"But you run it from this folder:",[45,25596,25598],{"className":47,"code":25597,"language":49,"meta":50,"style":50},"\u002Fhome\u002Falice\n",[17,25599,25600],{"__ignoreMap":50},[54,25601,25602,25604,25606,25608],{"class":56,"line":57},[54,25603,6676],{"class":81},[54,25605,25322],{"class":64},[54,25607,6676],{"class":81},[54,25609,25610],{"class":64},"alice\n",[14,25612,25613,25614,25616],{},"Then ",[17,25615,23766],{}," may return:",[45,25618,25619],{"className":47,"code":25597,"language":49,"meta":50,"style":50},[17,25620,25621],{"__ignoreMap":50},[54,25622,25623,25625,25627,25629],{"class":56,"line":57},[54,25624,6676],{"class":81},[54,25626,25322],{"class":64},[54,25628,6676],{"class":81},[54,25630,25610],{"class":64},[14,25632,25633,25634,89],{},"Even though the script itself is stored in ",[17,25635,25636],{},"\u002Fhome\u002Falice\u002Fprojects\u002Fapp",[14,25638,25639],{},"This difference is common in IDEs, where the program may start from a project folder, workspace folder, or another configured location.",[40,25641,25643],{"id":25642},"common-beginner-use-cases","Common beginner use cases",[14,25645,25646,25647,147],{},"Here are a few practical ways beginners use ",[17,25648,23766],{},[149,25650,25651,25654,25657,25660],{},[152,25652,25653],{},"Check where Python is looking for files",[152,25655,25656],{},"Build a full path before opening a file",[152,25658,25659],{},"Print the folder while debugging",[152,25661,25662],{},"Confirm the environment before changing directories",[14,25664,253],{},[45,25666,25668],{"className":47,"code":25667,"language":49,"meta":50,"style":50},"import os\n\nfolder = os.getcwd()\nfilename = \"report.txt\"\nfull_path = os.path.join(folder, filename)\n\nprint(full_path)\n",[17,25669,25670,25676,25680,25694,25708,25737,25741],{"__ignoreMap":50},[54,25671,25672,25674],{"class":56,"line":57},[54,25673,61],{"class":60},[54,25675,23707],{"class":64},[54,25677,25678],{"class":56,"line":68},[54,25679,72],{"emptyLinePlaceholder":71},[54,25681,25682,25684,25686,25688,25690,25692],{"class":56,"line":75},[54,25683,24696],{"class":64},[54,25685,82],{"class":81},[54,25687,24000],{"class":64},[54,25689,89],{"class":88},[54,25691,23725],{"class":92},[54,25693,1670],{"class":88},[54,25695,25696,25699,25701,25703,25706],{"class":56,"line":112},[54,25697,25698],{"class":64},"filename ",[54,25700,82],{"class":81},[54,25702,830],{"class":544},[54,25704,25705],{"class":548},"report.txt",[54,25707,5239],{"class":544},[54,25709,25710,25713,25715,25717,25719,25721,25723,25726,25728,25730,25732,25735],{"class":56,"line":887},[54,25711,25712],{"class":64},"full_path ",[54,25714,82],{"class":81},[54,25716,24000],{"class":64},[54,25718,89],{"class":88},[54,25720,23896],{"class":1693},[54,25722,89],{"class":88},[54,25724,25725],{"class":92},"join",[54,25727,96],{"class":88},[54,25729,24762],{"class":92},[54,25731,103],{"class":88},[54,25733,25734],{"class":92}," filename",[54,25736,109],{"class":88},[54,25738,25739],{"class":56,"line":892},[54,25740,72],{"emptyLinePlaceholder":71},[54,25742,25743,25745,25747,25750],{"class":56,"line":913},[54,25744,116],{"class":115},[54,25746,96],{"class":88},[54,25748,25749],{"class":92},"full_path",[54,25751,109],{"class":88},[14,25753,25754,25755,25761],{},"This example uses ",[192,25756,25758],{"href":25757},"\u002Fstandard-library\u002Fos.path.join-function-explained",[17,25759,25760],{},"os.path.join()"," to build a path safely.",[40,25763,25765],{"id":25764},"related-functions-to-know","Related functions to know",[14,25767,25768,25770],{},[17,25769,23766],{}," is often used with other path-related tools:",[149,25772,25773,25780,25787],{},[152,25774,25775,25779],{},[192,25776,25777],{"href":25116},[17,25778,23669],{}," changes the current working directory",[152,25781,25782,25786],{},[192,25783,25784],{"href":25757},[17,25785,25760],{}," combines folder and file names safely",[152,25788,25789,25793],{},[192,25790,25791],{"href":24853},[17,25792,24667],{}," checks whether a path exists",[14,25795,253],{},[45,25797,25799],{"className":47,"code":25798,"language":49,"meta":50,"style":50},"import os\n\nprint(\"Before:\", os.getcwd())\n\nif os.path.exists(\"myfile.txt\"):\n    print(\"File found\")\nelse:\n    print(\"File not found\")\n",[17,25800,25801,25807,25811,25833,25837,25862,25877,25883],{"__ignoreMap":50},[54,25802,25803,25805],{"class":56,"line":57},[54,25804,61],{"class":60},[54,25806,23707],{"class":64},[54,25808,25809],{"class":56,"line":68},[54,25810,72],{"emptyLinePlaceholder":71},[54,25812,25813,25815,25817,25819,25821,25823,25825,25827,25829,25831],{"class":56,"line":75},[54,25814,116],{"class":115},[54,25816,96],{"class":88},[54,25818,545],{"class":544},[54,25820,23993],{"class":548},[54,25822,545],{"class":544},[54,25824,103],{"class":88},[54,25826,24000],{"class":92},[54,25828,89],{"class":88},[54,25830,23725],{"class":92},[54,25832,1950],{"class":88},[54,25834,25835],{"class":56,"line":112},[54,25836,72],{"emptyLinePlaceholder":71},[54,25838,25839,25841,25843,25845,25847,25849,25851,25853,25855,25858,25860],{"class":56,"line":887},[54,25840,22942],{"class":60},[54,25842,24000],{"class":64},[54,25844,89],{"class":88},[54,25846,23896],{"class":1693},[54,25848,89],{"class":88},[54,25850,24757],{"class":92},[54,25852,96],{"class":88},[54,25854,545],{"class":544},[54,25856,25857],{"class":548},"myfile.txt",[54,25859,545],{"class":544},[54,25861,10837],{"class":88},[54,25863,25864,25866,25868,25870,25873,25875],{"class":56,"line":892},[54,25865,7606],{"class":115},[54,25867,96],{"class":88},[54,25869,545],{"class":544},[54,25871,25872],{"class":548},"File found",[54,25874,545],{"class":544},[54,25876,109],{"class":88},[54,25878,25879,25881],{"class":56,"line":913},[54,25880,22975],{"class":60},[54,25882,7572],{"class":88},[54,25884,25885,25887,25889,25891,25894,25896],{"class":56,"line":2121},[54,25886,7606],{"class":115},[54,25888,96],{"class":88},[54,25890,545],{"class":544},[54,25892,25893],{"class":548},"File not found",[54,25895,545],{"class":544},[54,25897,109],{"class":88},[14,25899,25900,25901,89],{},"If you want a broader view of path and system tools, read the ",[192,25902,25903],{"href":23866},"Python os module overview",[40,25905,978],{"id":977},[14,25907,25908,25909,147],{},"These are common beginner mistakes when using ",[17,25910,23766],{},[149,25912,25913,25916,25922,25928],{},[152,25914,25915],{},"Using a relative file path without checking the current working directory",[152,25917,25918,25919,25921],{},"Assuming ",[17,25920,23766],{}," returns the script file location",[152,25923,17008,25924,1494,25926],{},[17,25925,23720],{},[17,25927,23766],{},[152,25929,25930],{},"Running the same script from different folders and getting different results",[14,25932,25933],{},"A very common problem looks like this:",[45,25935,25937],{"className":47,"code":25936,"language":49,"meta":50,"style":50},"import os\n\nprint(os.getcwd())\n\nwith open(\"myfile.txt\", \"r\") as file:\n    print(file.read())\n",[17,25938,25939,25945,25949,25963,25967,25997],{"__ignoreMap":50},[54,25940,25941,25943],{"class":56,"line":57},[54,25942,61],{"class":60},[54,25944,23707],{"class":64},[54,25946,25947],{"class":56,"line":68},[54,25948,72],{"emptyLinePlaceholder":71},[54,25950,25951,25953,25955,25957,25959,25961],{"class":56,"line":75},[54,25952,116],{"class":115},[54,25954,96],{"class":88},[54,25956,23720],{"class":92},[54,25958,89],{"class":88},[54,25960,23725],{"class":92},[54,25962,1950],{"class":88},[54,25964,25965],{"class":56,"line":112},[54,25966,72],{"emptyLinePlaceholder":71},[54,25968,25969,25971,25973,25975,25977,25979,25981,25983,25985,25987,25989,25991,25993,25995],{"class":56,"line":887},[54,25970,8267],{"class":60},[54,25972,8270],{"class":115},[54,25974,96],{"class":88},[54,25976,545],{"class":544},[54,25978,25857],{"class":548},[54,25980,545],{"class":544},[54,25982,103],{"class":88},[54,25984,830],{"class":544},[54,25986,10658],{"class":548},[54,25988,545],{"class":544},[54,25990,876],{"class":88},[54,25992,7631],{"class":60},[54,25994,8296],{"class":8295},[54,25996,7572],{"class":88},[54,25998,25999,26001,26003,26005,26007,26009],{"class":56,"line":892},[54,26000,7606],{"class":115},[54,26002,96],{"class":88},[54,26004,8412],{"class":8295},[54,26006,89],{"class":88},[54,26008,17821],{"class":92},[54,26010,1950],{"class":88},[14,26012,26013,26014,89],{},"If this fails, the file may not be in the folder shown by ",[17,26015,23766],{},[14,26017,26018],{},"You can also run quick checks from the command line:",[45,26020,26022],{"className":15497,"code":26021,"language":15499,"meta":50,"style":50},"python script.py\npwd\ncd \u002Fpath\u002Fto\u002Ffolder\npython -c \"import os; print(os.getcwd())\"\npython -c \"import os; print(os.path.exists('myfile.txt'))\"\n",[17,26023,26024,26031,26036,26044,26058],{"__ignoreMap":50},[54,26025,26026,26028],{"class":56,"line":57},[54,26027,49],{"class":10811},[54,26029,26030],{"class":548}," script.py\n",[54,26032,26033],{"class":56,"line":68},[54,26034,26035],{"class":115},"pwd\n",[54,26037,26038,26041],{"class":56,"line":75},[54,26039,26040],{"class":115},"cd",[54,26042,26043],{"class":548}," \u002Fpath\u002Fto\u002Ffolder\n",[54,26045,26046,26048,26051,26053,26056],{"class":56,"line":112},[54,26047,49],{"class":10811},[54,26049,26050],{"class":24323}," -c",[54,26052,830],{"class":544},[54,26054,26055],{"class":548},"import os; print(os.getcwd())",[54,26057,5239],{"class":544},[54,26059,26060,26062,26064,26066,26069],{"class":56,"line":887},[54,26061,49],{"class":10811},[54,26063,26050],{"class":24323},[54,26065,830],{"class":544},[54,26067,26068],{"class":548},"import os; print(os.path.exists('myfile.txt'))",[54,26070,5239],{"class":544},[14,26072,26073,26074,89],{},"If your program cannot find a file, see ",[192,26075,26077,26078,26081],{"href":26076},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix","FileNotFoundError: ",[54,26079,26080],{},"Errno 2"," No such file or directory",[40,26083,1419],{"id":1418},[602,26085,26087],{"id":26086},"what-does-osgetcwd-return","What does os.getcwd() return?",[14,26089,26090],{},"It returns the current working directory as a string.",[602,26092,26094],{"id":26093},"does-osgetcwd-return-the-script-location","Does os.getcwd() return the script location?",[14,26096,26097],{},"No. It returns the current working directory, which may be different from the script folder.",[602,26099,26101],{"id":26100},"do-i-need-to-pass-an-argument-to-osgetcwd","Do I need to pass an argument to os.getcwd()?",[14,26103,26104],{},"No. It takes no arguments.",[602,26106,26108],{"id":26107},"why-does-my-file-open-in-one-place-but-not-another","Why does my file open in one place but not another?",[14,26110,26111],{},"Your current working directory may be different, so a relative path points to a different folder.",[40,26113,1510],{"id":1509},[149,26115,26116,26120,26125,26130,26134,26139],{},[152,26117,26118],{},[192,26119,25903],{"href":23866},[152,26121,26122],{},[192,26123,26124],{"href":25116},"os.chdir() function explained",[152,26126,26127],{},[192,26128,26129],{"href":25757},"os.path.join() function explained",[152,26131,26132],{},[192,26133,25072],{"href":24853},[152,26135,26136],{},[192,26137,25083],{"href":26138},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python",[152,26140,26141],{},[192,26142,26077,26143,26145],{"href":26076},[54,26144,26080],{}," No such file or directory fix",[1545,26147,26148],{},"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 .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 .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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":26150},[26151,26152,26153,26157,26160,26161,26162,26163,26164,26170],{"id":42,"depth":68,"text":43},{"id":25189,"depth":68,"text":25190},{"id":2257,"depth":68,"text":2258,"children":26154},[26155,26156],{"id":8669,"depth":75,"text":8670},{"id":25289,"depth":75,"text":25290},{"id":25338,"depth":68,"text":25339,"children":26158},[26159],{"id":25411,"depth":75,"text":25412},{"id":25530,"depth":68,"text":25531},{"id":25642,"depth":68,"text":25643},{"id":25764,"depth":68,"text":25765},{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":26165},[26166,26167,26168,26169],{"id":26086,"depth":75,"text":26087},{"id":26093,"depth":75,"text":26094},{"id":26100,"depth":75,"text":26101},{"id":26107,"depth":75,"text":26108},{"id":1509,"depth":68,"text":1510},"Master os.getcwd function explained in our comprehensive Python beginner guide.",{},{"title":25122,"description":26171},"standard-library\u002Fos.getcwd-function-explained","NG4N4UIqUA-AxFPytoejr6i7kl84omZZRDGhJyjJEYU",{"id":26177,"title":26178,"body":26179,"description":27928,"extension":1577,"meta":27929,"navigation":71,"path":27930,"seo":27931,"stem":27932,"__hash__":27933},"content\u002Fstandard-library\u002Fos.listdir-function-explained.md","os.listdir() Function Explained",{"type":7,"value":26180,"toc":27893},[26181,26184,26190,26193,26207,26209,26256,26259,26262,26266,26271,26273,26284,26287,26303,26308,26341,26354,26356,26383,26387,26403,26405,26456,26459,26466,26468,26471,26517,26519,26553,26557,26560,26563,26596,26602,26654,26658,26663,26667,26714,26720,26724,26772,26775,26787,26791,26796,26798,26812,26814,26881,26884,26912,26915,26969,26973,26978,26983,27071,27073,27115,27118,27129,27135,27139,27144,27152,27154,27323,27326,27340,27347,27351,27356,27358,27361,27363,27400,27405,27408,27425,27430,27432,27435,27437,27475,27477,27488,27494,27498,27505,27508,27533,27536,27540,27543,27549,27553,27558,27561,27572,27578,27584,27590,27592,27597,27614,27617,27641,27674,27712,27749,27752,27766,27768,27772,27775,27779,27785,27789,27792,27796,27802,27806,27812,27854,27856,27890],[10,26182,26178],{"id":26183},"oslistdir-function-explained",[14,26185,26186,26189],{},[17,26187,26188],{},"os.listdir()"," gives you the names of files and folders inside a directory.",[14,26191,26192],{},"It is a useful function when you want to:",[149,26194,26195,26198,26201,26204],{},[152,26196,26197],{},"see what is inside a folder",[152,26199,26200],{},"loop through files",[152,26202,26203],{},"build file paths for later work",[152,26205,26206],{},"check directory contents before opening files",[40,26208,3596],{"id":3595},[45,26210,26212],{"className":47,"code":26211,"language":49,"meta":50,"style":50},"import os\n\nitems = os.listdir('.')\nprint(items)\n",[17,26213,26214,26220,26224,26246],{"__ignoreMap":50},[54,26215,26216,26218],{"class":56,"line":57},[54,26217,61],{"class":60},[54,26219,23707],{"class":64},[54,26221,26222],{"class":56,"line":68},[54,26223,72],{"emptyLinePlaceholder":71},[54,26225,26226,26228,26230,26232,26234,26236,26238,26240,26242,26244],{"class":56,"line":75},[54,26227,17899],{"class":64},[54,26229,82],{"class":81},[54,26231,24000],{"class":64},[54,26233,89],{"class":88},[54,26235,24433],{"class":92},[54,26237,96],{"class":88},[54,26239,2240],{"class":544},[54,26241,89],{"class":548},[54,26243,2240],{"class":544},[54,26245,109],{"class":88},[54,26247,26248,26250,26252,26254],{"class":56,"line":112},[54,26249,116],{"class":115},[54,26251,96],{"class":88},[54,26253,10388],{"class":92},[54,26255,109],{"class":88},[14,26257,26258],{},"This lists the names of files and folders in the current directory.",[26260,26261],"hr",{},[40,26263,26265],{"id":26264},"what-oslistdir-does","What os.listdir() does",[14,26267,26268,26270],{},[17,26269,26188],{}," returns the names inside a directory.",[14,26272,11458],{},[149,26274,26275,26278,26281],{},[152,26276,26277],{},"It usually returns a list of strings",[152,26279,26280],{},"It includes both files and folders",[152,26282,26283],{},"It does not tell you which names are files and which are directories by itself",[14,26285,26286],{},"So if a folder contains these entries:",[149,26288,26289,26293,26298],{},[152,26290,26291],{},[17,26292,24577],{},[152,26294,26295],{},[17,26296,26297],{},"images",[152,26299,26300],{},[17,26301,26302],{},"script.py",[14,26304,26305,26306,25616],{},"then ",[17,26307,26188],{},[45,26309,26311],{"className":47,"code":26310,"language":49,"meta":50,"style":50},"['notes.txt', 'images', 'script.py']\n",[17,26312,26313],{"__ignoreMap":50},[54,26314,26315,26317,26319,26321,26323,26325,26327,26329,26331,26333,26335,26337,26339],{"class":56,"line":57},[54,26316,459],{"class":88},[54,26318,2240],{"class":544},[54,26320,24577],{"class":548},[54,26322,2240],{"class":544},[54,26324,103],{"class":88},[54,26326,2234],{"class":544},[54,26328,26297],{"class":548},[54,26330,2240],{"class":544},[54,26332,103],{"class":88},[54,26334,2234],{"class":544},[54,26336,26302],{"class":548},[54,26338,2240],{"class":544},[54,26340,469],{"class":88},[14,26342,26343,26344,26346,26347,134,26351,89],{},"If you need to check what each name represents, you can combine ",[17,26345,26188],{}," with functions like ",[192,26348,26349],{"href":25757},[17,26350,25760],{},[17,26352,26353],{},"os.path.isfile()",[40,26355,199],{"id":198},[45,26357,26359],{"className":47,"code":26358,"language":49,"meta":50,"style":50},"os.listdir(path='.')\n",[17,26360,26361],{"__ignoreMap":50},[54,26362,26363,26365,26367,26369,26371,26373,26375,26377,26379,26381],{"class":56,"line":57},[54,26364,23720],{"class":64},[54,26366,89],{"class":88},[54,26368,24433],{"class":92},[54,26370,96],{"class":88},[54,26372,23896],{"class":8940},[54,26374,82],{"class":81},[54,26376,2240],{"class":544},[54,26378,89],{"class":548},[54,26380,2240],{"class":544},[54,26382,109],{"class":88},[602,26384,26386],{"id":26385},"how-it-works","How it works",[149,26388,26389,26394,26397,26400],{},[152,26390,26391,26393],{},[17,26392,23896],{}," is the folder you want to inspect",[152,26395,26396],{},"If you do not give a path, Python uses the current working directory",[152,26398,26399],{},"You can pass a folder path as a string",[152,26401,26402],{},"The returned values are names only, not full paths",[14,26404,253],{},[45,26406,26408],{"className":47,"code":26407,"language":49,"meta":50,"style":50},"import os\n\nprint(os.listdir())\nprint(os.listdir('.'))\n",[17,26409,26410,26416,26420,26434],{"__ignoreMap":50},[54,26411,26412,26414],{"class":56,"line":57},[54,26413,61],{"class":60},[54,26415,23707],{"class":64},[54,26417,26418],{"class":56,"line":68},[54,26419,72],{"emptyLinePlaceholder":71},[54,26421,26422,26424,26426,26428,26430,26432],{"class":56,"line":75},[54,26423,116],{"class":115},[54,26425,96],{"class":88},[54,26427,23720],{"class":92},[54,26429,89],{"class":88},[54,26431,24433],{"class":92},[54,26433,1950],{"class":88},[54,26435,26436,26438,26440,26442,26444,26446,26448,26450,26452,26454],{"class":56,"line":112},[54,26437,116],{"class":115},[54,26439,96],{"class":88},[54,26441,23720],{"class":92},[54,26443,89],{"class":88},[54,26445,24433],{"class":92},[54,26447,96],{"class":88},[54,26449,2240],{"class":544},[54,26451,89],{"class":548},[54,26453,2240],{"class":544},[54,26455,382],{"class":88},[14,26457,26458],{},"Both examples list the current working directory.",[14,26460,26461,26462,89],{},"If you are not sure what your current working directory is, see ",[192,26463,26464,2834],{"href":23871},[17,26465,23766],{},[40,26467,493],{"id":492},[14,26469,26470],{},"Here is a basic example that lists the current directory:",[45,26472,26473],{"className":47,"code":26211,"language":49,"meta":50,"style":50},[17,26474,26475,26481,26485,26507],{"__ignoreMap":50},[54,26476,26477,26479],{"class":56,"line":57},[54,26478,61],{"class":60},[54,26480,23707],{"class":64},[54,26482,26483],{"class":56,"line":68},[54,26484,72],{"emptyLinePlaceholder":71},[54,26486,26487,26489,26491,26493,26495,26497,26499,26501,26503,26505],{"class":56,"line":75},[54,26488,17899],{"class":64},[54,26490,82],{"class":81},[54,26492,24000],{"class":64},[54,26494,89],{"class":88},[54,26496,24433],{"class":92},[54,26498,96],{"class":88},[54,26500,2240],{"class":544},[54,26502,89],{"class":548},[54,26504,2240],{"class":544},[54,26506,109],{"class":88},[54,26508,26509,26511,26513,26515],{"class":56,"line":112},[54,26510,116],{"class":115},[54,26512,96],{"class":88},[54,26514,10388],{"class":92},[54,26516,109],{"class":88},[14,26518,305],{},[45,26520,26522],{"className":47,"code":26521,"language":49,"meta":50,"style":50},"['main.py', 'data', 'notes.txt']\n",[17,26523,26524],{"__ignoreMap":50},[54,26525,26526,26528,26530,26533,26535,26537,26539,26541,26543,26545,26547,26549,26551],{"class":56,"line":57},[54,26527,459],{"class":88},[54,26529,2240],{"class":544},[54,26531,26532],{"class":548},"main.py",[54,26534,2240],{"class":544},[54,26536,103],{"class":88},[54,26538,2234],{"class":544},[54,26540,8313],{"class":548},[54,26542,2240],{"class":544},[54,26544,103],{"class":88},[54,26546,2234],{"class":544},[54,26548,24577],{"class":548},[54,26550,2240],{"class":544},[54,26552,469],{"class":88},[602,26554,26556],{"id":26555},"important-note-about-order","Important note about order",[14,26558,26559],{},"Do not rely on the order of the returned items.",[14,26561,26562],{},"The order is not guaranteed, so this might also happen:",[45,26564,26566],{"className":47,"code":26565,"language":49,"meta":50,"style":50},"['notes.txt', 'main.py', 'data']\n",[17,26567,26568],{"__ignoreMap":50},[54,26569,26570,26572,26574,26576,26578,26580,26582,26584,26586,26588,26590,26592,26594],{"class":56,"line":57},[54,26571,459],{"class":88},[54,26573,2240],{"class":544},[54,26575,24577],{"class":548},[54,26577,2240],{"class":544},[54,26579,103],{"class":88},[54,26581,2234],{"class":544},[54,26583,26532],{"class":548},[54,26585,2240],{"class":544},[54,26587,103],{"class":88},[54,26589,2234],{"class":544},[54,26591,8313],{"class":548},[54,26593,2240],{"class":544},[54,26595,469],{"class":88},[14,26597,26598,26599,147],{},"If you want a stable order, use ",[17,26600,26601],{},"sorted()",[45,26603,26605],{"className":47,"code":26604,"language":49,"meta":50,"style":50},"import os\n\nitems = sorted(os.listdir('.'))\nprint(items)\n",[17,26606,26607,26613,26617,26644],{"__ignoreMap":50},[54,26608,26609,26611],{"class":56,"line":57},[54,26610,61],{"class":60},[54,26612,23707],{"class":64},[54,26614,26615],{"class":56,"line":68},[54,26616,72],{"emptyLinePlaceholder":71},[54,26618,26619,26621,26623,26626,26628,26630,26632,26634,26636,26638,26640,26642],{"class":56,"line":75},[54,26620,17899],{"class":64},[54,26622,82],{"class":81},[54,26624,26625],{"class":115}," sorted",[54,26627,96],{"class":88},[54,26629,23720],{"class":92},[54,26631,89],{"class":88},[54,26633,24433],{"class":92},[54,26635,96],{"class":88},[54,26637,2240],{"class":544},[54,26639,89],{"class":548},[54,26641,2240],{"class":544},[54,26643,382],{"class":88},[54,26645,26646,26648,26650,26652],{"class":56,"line":112},[54,26647,116],{"class":115},[54,26649,96],{"class":88},[54,26651,10388],{"class":92},[54,26653,109],{"class":88},[40,26655,26657],{"id":26656},"list-another-directory","List another directory",[14,26659,26660,26661,89],{},"You can pass a different folder path to ",[17,26662,26188],{},[602,26664,26666],{"id":26665},"using-a-relative-path","Using a relative path",[45,26668,26670],{"className":47,"code":26669,"language":49,"meta":50,"style":50},"import os\n\nitems = os.listdir('data')\nprint(items)\n",[17,26671,26672,26678,26682,26704],{"__ignoreMap":50},[54,26673,26674,26676],{"class":56,"line":57},[54,26675,61],{"class":60},[54,26677,23707],{"class":64},[54,26679,26680],{"class":56,"line":68},[54,26681,72],{"emptyLinePlaceholder":71},[54,26683,26684,26686,26688,26690,26692,26694,26696,26698,26700,26702],{"class":56,"line":75},[54,26685,17899],{"class":64},[54,26687,82],{"class":81},[54,26689,24000],{"class":64},[54,26691,89],{"class":88},[54,26693,24433],{"class":92},[54,26695,96],{"class":88},[54,26697,2240],{"class":544},[54,26699,8313],{"class":548},[54,26701,2240],{"class":544},[54,26703,109],{"class":88},[54,26705,26706,26708,26710,26712],{"class":56,"line":112},[54,26707,116],{"class":115},[54,26709,96],{"class":88},[54,26711,10388],{"class":92},[54,26713,109],{"class":88},[14,26715,26716,26717,26719],{},"This lists the contents of a folder named ",[17,26718,8313],{}," relative to your current working directory.",[602,26721,26723],{"id":26722},"using-an-absolute-path","Using an absolute path",[45,26725,26727],{"className":47,"code":26726,"language":49,"meta":50,"style":50},"import os\n\nitems = os.listdir('\u002FUsers\u002Fname\u002Fproject\u002Fdata')\nprint(items)\n",[17,26728,26729,26735,26739,26762],{"__ignoreMap":50},[54,26730,26731,26733],{"class":56,"line":57},[54,26732,61],{"class":60},[54,26734,23707],{"class":64},[54,26736,26737],{"class":56,"line":68},[54,26738,72],{"emptyLinePlaceholder":71},[54,26740,26741,26743,26745,26747,26749,26751,26753,26755,26758,26760],{"class":56,"line":75},[54,26742,17899],{"class":64},[54,26744,82],{"class":81},[54,26746,24000],{"class":64},[54,26748,89],{"class":88},[54,26750,24433],{"class":92},[54,26752,96],{"class":88},[54,26754,2240],{"class":544},[54,26756,26757],{"class":548},"\u002FUsers\u002Fname\u002Fproject\u002Fdata",[54,26759,2240],{"class":544},[54,26761,109],{"class":88},[54,26763,26764,26766,26768,26770],{"class":56,"line":112},[54,26765,116],{"class":115},[54,26767,96],{"class":88},[54,26769,10388],{"class":92},[54,26771,109],{"class":88},[14,26773,26774],{},"An absolute path is useful when the folder is outside your current location.",[14,26776,26777,26778,26782,26783,89],{},"If your folder is not being found, the problem is often the path. It helps to check the current working directory with ",[192,26779,26780],{"href":23871},[17,26781,23766],{}," and confirm the folder exists with ",[192,26784,26785],{"href":24853},[17,26786,24667],{},[40,26788,26790],{"id":26789},"important-return-value-details","Important return value details",[14,26792,26793,26795],{},[17,26794,26188],{}," returns directory entry names.",[14,26797,343],{},[149,26799,26800,26803,26806,26809],{},[152,26801,26802],{},"it returns names only",[152,26804,26805],{},"it does not return file contents",[152,26807,26808],{},"the names can be files, folders, or other entries",[152,26810,26811],{},"the order is not guaranteed",[14,26813,253],{},[45,26815,26817],{"className":47,"code":26816,"language":49,"meta":50,"style":50},"import os\n\nitems = os.listdir('.')\n\nfor name in items:\n    print(name)\n",[17,26818,26819,26825,26829,26851,26855,26871],{"__ignoreMap":50},[54,26820,26821,26823],{"class":56,"line":57},[54,26822,61],{"class":60},[54,26824,23707],{"class":64},[54,26826,26827],{"class":56,"line":68},[54,26828,72],{"emptyLinePlaceholder":71},[54,26830,26831,26833,26835,26837,26839,26841,26843,26845,26847,26849],{"class":56,"line":75},[54,26832,17899],{"class":64},[54,26834,82],{"class":81},[54,26836,24000],{"class":64},[54,26838,89],{"class":88},[54,26840,24433],{"class":92},[54,26842,96],{"class":88},[54,26844,2240],{"class":544},[54,26846,89],{"class":548},[54,26848,2240],{"class":544},[54,26850,109],{"class":88},[54,26852,26853],{"class":56,"line":112},[54,26854,72],{"emptyLinePlaceholder":71},[54,26856,26857,26860,26863,26866,26869],{"class":56,"line":887},[54,26858,26859],{"class":60},"for",[54,26861,26862],{"class":64}," name ",[54,26864,26865],{"class":60},"in",[54,26867,26868],{"class":64}," items",[54,26870,7572],{"class":88},[54,26872,26873,26875,26877,26879],{"class":56,"line":892},[54,26874,7606],{"class":115},[54,26876,96],{"class":88},[54,26878,8231],{"class":92},[54,26880,109],{"class":88},[14,26882,26883],{},"This prints names like:",[45,26885,26887],{"className":47,"code":26886,"language":49,"meta":50,"style":50},"main.py\ndata\nnotes.txt\n",[17,26888,26889,26898,26903],{"__ignoreMap":50},[54,26890,26891,26894,26896],{"class":56,"line":57},[54,26892,26893],{"class":64},"main",[54,26895,89],{"class":88},[54,26897,25591],{"class":1693},[54,26899,26900],{"class":56,"line":68},[54,26901,26902],{"class":64},"data\n",[54,26904,26905,26907,26909],{"class":56,"line":75},[54,26906,15317],{"class":64},[54,26908,89],{"class":88},[54,26910,26911],{"class":1693},"txt\n",[14,26913,26914],{},"If you want predictable output, sort the result first:",[45,26916,26918],{"className":47,"code":26917,"language":49,"meta":50,"style":50},"import os\n\nfor name in sorted(os.listdir('.')):\n    print(name)\n",[17,26919,26920,26926,26930,26959],{"__ignoreMap":50},[54,26921,26922,26924],{"class":56,"line":57},[54,26923,61],{"class":60},[54,26925,23707],{"class":64},[54,26927,26928],{"class":56,"line":68},[54,26929,72],{"emptyLinePlaceholder":71},[54,26931,26932,26934,26936,26938,26940,26942,26944,26946,26948,26950,26952,26954,26956],{"class":56,"line":75},[54,26933,26859],{"class":60},[54,26935,26862],{"class":64},[54,26937,26865],{"class":60},[54,26939,26625],{"class":115},[54,26941,96],{"class":88},[54,26943,23720],{"class":92},[54,26945,89],{"class":88},[54,26947,24433],{"class":92},[54,26949,96],{"class":88},[54,26951,2240],{"class":544},[54,26953,89],{"class":548},[54,26955,2240],{"class":544},[54,26957,26958],{"class":88},")):\n",[54,26960,26961,26963,26965,26967],{"class":56,"line":112},[54,26962,7606],{"class":115},[54,26964,96],{"class":88},[54,26966,8231],{"class":92},[54,26968,109],{"class":88},[40,26970,26972],{"id":26971},"how-to-get-full-paths","How to get full paths",[14,26974,3412,26975,26977],{},[17,26976,26188],{}," returns names only, you often need to build full paths before doing more work.",[14,26979,126,26980,26982],{},[17,26981,25760],{}," for that:",[45,26984,26986],{"className":47,"code":26985,"language":49,"meta":50,"style":50},"import os\n\nfolder = 'data'\n\nfor name in os.listdir(folder):\n    full_path = os.path.join(folder, name)\n    print(full_path)\n",[17,26987,26988,26994,26998,27010,27014,27034,27061],{"__ignoreMap":50},[54,26989,26990,26992],{"class":56,"line":57},[54,26991,61],{"class":60},[54,26993,23707],{"class":64},[54,26995,26996],{"class":56,"line":68},[54,26997,72],{"emptyLinePlaceholder":71},[54,26999,27000,27002,27004,27006,27008],{"class":56,"line":75},[54,27001,24696],{"class":64},[54,27003,82],{"class":81},[54,27005,2234],{"class":544},[54,27007,8313],{"class":548},[54,27009,5297],{"class":544},[54,27011,27012],{"class":56,"line":112},[54,27013,72],{"emptyLinePlaceholder":71},[54,27015,27016,27018,27020,27022,27024,27026,27028,27030,27032],{"class":56,"line":887},[54,27017,26859],{"class":60},[54,27019,26862],{"class":64},[54,27021,26865],{"class":60},[54,27023,24000],{"class":64},[54,27025,89],{"class":88},[54,27027,24433],{"class":92},[54,27029,96],{"class":88},[54,27031,24762],{"class":92},[54,27033,10837],{"class":88},[54,27035,27036,27039,27041,27043,27045,27047,27049,27051,27053,27055,27057,27059],{"class":56,"line":892},[54,27037,27038],{"class":64},"    full_path ",[54,27040,82],{"class":81},[54,27042,24000],{"class":64},[54,27044,89],{"class":88},[54,27046,23896],{"class":1693},[54,27048,89],{"class":88},[54,27050,25725],{"class":92},[54,27052,96],{"class":88},[54,27054,24762],{"class":92},[54,27056,103],{"class":88},[54,27058,10834],{"class":92},[54,27060,109],{"class":88},[54,27062,27063,27065,27067,27069],{"class":56,"line":913},[54,27064,7606],{"class":115},[54,27066,96],{"class":88},[54,27068,25749],{"class":92},[54,27070,109],{"class":88},[14,27072,305],{},[45,27074,27076],{"className":47,"code":27075,"language":49,"meta":50,"style":50},"data\u002Freport.csv\ndata\u002Fimage.png\ndata\u002Farchive\n",[17,27077,27078,27092,27106],{"__ignoreMap":50},[54,27079,27080,27082,27084,27087,27089],{"class":56,"line":57},[54,27081,8313],{"class":64},[54,27083,6676],{"class":81},[54,27085,27086],{"class":64},"report",[54,27088,89],{"class":88},[54,27090,27091],{"class":1693},"csv\n",[54,27093,27094,27096,27098,27101,27103],{"class":56,"line":68},[54,27095,8313],{"class":64},[54,27097,6676],{"class":81},[54,27099,27100],{"class":64},"image",[54,27102,89],{"class":88},[54,27104,27105],{"class":1693},"png\n",[54,27107,27108,27110,27112],{"class":56,"line":75},[54,27109,8313],{"class":64},[54,27111,6676],{"class":81},[54,27113,27114],{"class":64},"archive\n",[14,27116,27117],{},"This is helpful before:",[149,27119,27120,27123,27126],{},[152,27121,27122],{},"opening files",[152,27124,27125],{},"checking whether something is a file or folder",[152,27127,27128],{},"passing the path to another function",[14,27130,15021,27131,89],{},[192,27132,27133,2834],{"href":25757},[17,27134,25760],{},[40,27136,27138],{"id":27137},"how-to-tell-files-from-folders","How to tell files from folders",[14,27140,27141,27143],{},[17,27142,26188],{}," does not separate files and folders for you.",[14,27145,27146,27147,5507,27149,89],{},"To check each item, first build the full path, then use ",[17,27148,26353],{},[17,27150,27151],{},"os.path.isdir()",[602,27153,1821],{"id":1820},[45,27155,27157],{"className":47,"code":27156,"language":49,"meta":50,"style":50},"import os\n\nfolder = 'data'\n\nfor name in os.listdir(folder):\n    full_path = os.path.join(folder, name)\n\n    if os.path.isfile(full_path):\n        print(f\"{name} is a file\")\n    elif os.path.isdir(full_path):\n        print(f\"{name} is a directory\")\n",[17,27158,27159,27165,27169,27181,27185,27205,27231,27235,27257,27281,27302],{"__ignoreMap":50},[54,27160,27161,27163],{"class":56,"line":57},[54,27162,61],{"class":60},[54,27164,23707],{"class":64},[54,27166,27167],{"class":56,"line":68},[54,27168,72],{"emptyLinePlaceholder":71},[54,27170,27171,27173,27175,27177,27179],{"class":56,"line":75},[54,27172,24696],{"class":64},[54,27174,82],{"class":81},[54,27176,2234],{"class":544},[54,27178,8313],{"class":548},[54,27180,5297],{"class":544},[54,27182,27183],{"class":56,"line":112},[54,27184,72],{"emptyLinePlaceholder":71},[54,27186,27187,27189,27191,27193,27195,27197,27199,27201,27203],{"class":56,"line":887},[54,27188,26859],{"class":60},[54,27190,26862],{"class":64},[54,27192,26865],{"class":60},[54,27194,24000],{"class":64},[54,27196,89],{"class":88},[54,27198,24433],{"class":92},[54,27200,96],{"class":88},[54,27202,24762],{"class":92},[54,27204,10837],{"class":88},[54,27206,27207,27209,27211,27213,27215,27217,27219,27221,27223,27225,27227,27229],{"class":56,"line":892},[54,27208,27038],{"class":64},[54,27210,82],{"class":81},[54,27212,24000],{"class":64},[54,27214,89],{"class":88},[54,27216,23896],{"class":1693},[54,27218,89],{"class":88},[54,27220,25725],{"class":92},[54,27222,96],{"class":88},[54,27224,24762],{"class":92},[54,27226,103],{"class":88},[54,27228,10834],{"class":92},[54,27230,109],{"class":88},[54,27232,27233],{"class":56,"line":913},[54,27234,72],{"emptyLinePlaceholder":71},[54,27236,27237,27240,27242,27244,27246,27248,27251,27253,27255],{"class":56,"line":2121},[54,27238,27239],{"class":60},"    if",[54,27241,24000],{"class":64},[54,27243,89],{"class":88},[54,27245,23896],{"class":1693},[54,27247,89],{"class":88},[54,27249,27250],{"class":92},"isfile",[54,27252,96],{"class":88},[54,27254,25749],{"class":92},[54,27256,10837],{"class":88},[54,27258,27259,27262,27264,27267,27269,27271,27273,27276,27279],{"class":56,"line":2136},[54,27260,27261],{"class":115},"        print",[54,27263,96],{"class":88},[54,27265,27266],{"class":2230},"f",[54,27268,545],{"class":548},[54,27270,8711],{"class":99},[54,27272,8231],{"class":92},[54,27274,27275],{"class":99},"}",[54,27277,27278],{"class":548}," is a file\"",[54,27280,109],{"class":88},[54,27282,27283,27286,27288,27290,27292,27294,27296,27298,27300],{"class":56,"line":2151},[54,27284,27285],{"class":60},"    elif",[54,27287,24000],{"class":64},[54,27289,89],{"class":88},[54,27291,23896],{"class":1693},[54,27293,89],{"class":88},[54,27295,24991],{"class":92},[54,27297,96],{"class":88},[54,27299,25749],{"class":92},[54,27301,10837],{"class":88},[54,27303,27304,27306,27308,27310,27312,27314,27316,27318,27321],{"class":56,"line":4442},[54,27305,27261],{"class":115},[54,27307,96],{"class":88},[54,27309,27266],{"class":2230},[54,27311,545],{"class":548},[54,27313,8711],{"class":99},[54,27315,8231],{"class":92},[54,27317,27275],{"class":99},[54,27319,27320],{"class":548}," is a directory\"",[54,27322,109],{"class":88},[14,27324,27325],{},"This is the safest pattern for beginners:",[27327,27328,27329,27334,27337],"ol",{},[152,27330,27331,27332],{},"get the name from ",[17,27333,26188],{},[152,27335,27336],{},"build the full path",[152,27338,27339],{},"check the path type",[14,27341,27342,27343,89],{},"If your goal is specifically to list only files, see ",[192,27344,27346],{"href":27345},"\u002Fhow-to\u002Fhow-to-list-files-in-a-directory-in-python\u002F","How to list files in a directory in Python",[40,27348,27350],{"id":27349},"common-errors-and-problems","Common errors and problems",[14,27352,27353,27354,89],{},"A few common problems come up when using ",[17,27355,26188],{},[602,27357,15337],{"id":24454},[14,27359,27360],{},"This happens when the path does not exist.",[14,27362,253],{},[45,27364,27366],{"className":47,"code":27365,"language":49,"meta":50,"style":50},"import os\n\nprint(os.listdir('missing_folder'))\n",[17,27367,27368,27374,27378],{"__ignoreMap":50},[54,27369,27370,27372],{"class":56,"line":57},[54,27371,61],{"class":60},[54,27373,23707],{"class":64},[54,27375,27376],{"class":56,"line":68},[54,27377,72],{"emptyLinePlaceholder":71},[54,27379,27380,27382,27384,27386,27388,27390,27392,27394,27396,27398],{"class":56,"line":75},[54,27381,116],{"class":115},[54,27383,96],{"class":88},[54,27385,23720],{"class":92},[54,27387,89],{"class":88},[54,27389,24433],{"class":92},[54,27391,96],{"class":88},[54,27393,2240],{"class":544},[54,27395,24487],{"class":548},[54,27397,2240],{"class":544},[54,27399,382],{"class":88},[14,27401,7429,27402,27404],{},[17,27403,24487],{}," is not real, Python raises an error.",[14,27406,27407],{},"Fixes:",[149,27409,27410,27413,27416,27419],{},[152,27411,27412],{},"make sure the folder exists",[152,27414,27415],{},"check the spelling",[152,27417,27418],{},"confirm you are using the correct relative path",[152,27420,27421,27422,27424],{},"print ",[17,27423,23766],{}," to see where Python is looking",[14,27426,27427,27428],{},"Related help: ",[192,27429,15422],{"href":15421},[602,27431,24596],{"id":24595},[14,27433,27434],{},"This happens when Python cannot access the folder.",[14,27436,253],{},[45,27438,27440],{"className":47,"code":27439,"language":49,"meta":50,"style":50},"import os\n\nprint(os.listdir('\u002Frestricted_folder'))\n",[17,27441,27442,27448,27452],{"__ignoreMap":50},[54,27443,27444,27446],{"class":56,"line":57},[54,27445,61],{"class":60},[54,27447,23707],{"class":64},[54,27449,27450],{"class":56,"line":68},[54,27451,72],{"emptyLinePlaceholder":71},[54,27453,27454,27456,27458,27460,27462,27464,27466,27468,27471,27473],{"class":56,"line":75},[54,27455,116],{"class":115},[54,27457,96],{"class":88},[54,27459,23720],{"class":92},[54,27461,89],{"class":88},[54,27463,24433],{"class":92},[54,27465,96],{"class":88},[54,27467,2240],{"class":544},[54,27469,27470],{"class":548},"\u002Frestricted_folder",[54,27472,2240],{"class":544},[54,27474,382],{"class":88},[14,27476,27407],{},[149,27478,27479,27482,27485],{},[152,27480,27481],{},"use a folder your program can access",[152,27483,27484],{},"check file system permissions",[152,27486,27487],{},"avoid protected system locations unless needed",[14,27489,27427,27490],{},[192,27491,27493],{"href":27492},"\u002Ferrors\u002Fpermissionerror-in-python-causes-and-fixes\u002F","PermissionError in Python: causes and fixes",[602,27495,27497],{"id":27496},"relative-path-confusion","Relative path confusion",[14,27499,27500,27501,27504],{},"A path like ",[17,27502,27503],{},"'data'"," only works if Python is running from the right place.",[14,27506,27507],{},"This command helps you check:",[45,27509,27511],{"className":47,"code":27510,"language":49,"meta":50,"style":50},"import os\nprint(os.getcwd())\n",[17,27512,27513,27519],{"__ignoreMap":50},[54,27514,27515,27517],{"class":56,"line":57},[54,27516,61],{"class":60},[54,27518,23707],{"class":64},[54,27520,27521,27523,27525,27527,27529,27531],{"class":56,"line":68},[54,27522,116],{"class":115},[54,27524,96],{"class":88},[54,27526,23720],{"class":92},[54,27528,89],{"class":88},[54,27530,23725],{"class":92},[54,27532,1950],{"class":88},[14,27534,27535],{},"If needed, use an absolute path or adjust your working directory.",[602,27537,27539],{"id":27538},"hidden-files","Hidden files",[14,27541,27542],{},"Some systems include hidden files in directory listings.",[14,27544,27545,27546,27548],{},"So you may see names you were not expecting, such as files starting with ",[17,27547,89],{}," on Unix-like systems.",[40,27550,27552],{"id":27551},"oslistdir-vs-osscandir","os.listdir() vs os.scandir()",[14,27554,27555,27557],{},[17,27556,26188],{}," is a simple choice when you only need names.",[14,27559,27560],{},"Use it when:",[149,27562,27563,27566,27569],{},[152,27564,27565],{},"you want a quick list of entries",[152,27567,27568],{},"you are learning basic file handling",[152,27570,27571],{},"you do not need extra information right away",[14,27573,27574,27577],{},[17,27575,27576],{},"os.scandir()"," gives more information about each entry, such as methods to check whether it is a file or directory more directly.",[14,27579,27580,27581,27583],{},"For many beginner tasks, ",[17,27582,26188],{}," is easier to understand first.",[14,27585,27586,27587,89],{},"If you want a broader view of file and directory tools, see the ",[192,27588,25903],{"href":27589},"\u002Fstandard-library\u002Fpython-os-module-overview\u002F",[40,27591,978],{"id":977},[14,27593,27594,27595,147],{},"These are the most common beginner mistakes with ",[17,27596,26188],{},[149,27598,27599,27602,27605,27608,27611],{},[152,27600,27601],{},"Using a path that does not exist",[152,27603,27604],{},"Expecting full paths instead of names only",[152,27606,27607],{},"Assuming the result contains only files",[152,27609,27610],{},"Assuming the order will always be the same",[152,27612,27613],{},"Using a relative path from the wrong working directory",[14,27615,27616],{},"Useful debugging checks:",[45,27618,27619],{"className":47,"code":27510,"language":49,"meta":50,"style":50},[17,27620,27621,27627],{"__ignoreMap":50},[54,27622,27623,27625],{"class":56,"line":57},[54,27624,61],{"class":60},[54,27626,23707],{"class":64},[54,27628,27629,27631,27633,27635,27637,27639],{"class":56,"line":68},[54,27630,116],{"class":115},[54,27632,96],{"class":88},[54,27634,23720],{"class":92},[54,27636,89],{"class":88},[54,27638,23725],{"class":92},[54,27640,1950],{"class":88},[45,27642,27644],{"className":47,"code":27643,"language":49,"meta":50,"style":50},"import os\nprint(os.listdir('.'))\n",[17,27645,27646,27652],{"__ignoreMap":50},[54,27647,27648,27650],{"class":56,"line":57},[54,27649,61],{"class":60},[54,27651,23707],{"class":64},[54,27653,27654,27656,27658,27660,27662,27664,27666,27668,27670,27672],{"class":56,"line":68},[54,27655,116],{"class":115},[54,27657,96],{"class":88},[54,27659,23720],{"class":92},[54,27661,89],{"class":88},[54,27663,24433],{"class":92},[54,27665,96],{"class":88},[54,27667,2240],{"class":544},[54,27669,89],{"class":548},[54,27671,2240],{"class":544},[54,27673,382],{"class":88},[45,27675,27677],{"className":47,"code":27676,"language":49,"meta":50,"style":50},"import os\nprint(os.path.exists('your_folder'))\n",[17,27678,27679,27685],{"__ignoreMap":50},[54,27680,27681,27683],{"class":56,"line":57},[54,27682,61],{"class":60},[54,27684,23707],{"class":64},[54,27686,27687,27689,27691,27693,27695,27697,27699,27701,27703,27705,27708,27710],{"class":56,"line":68},[54,27688,116],{"class":115},[54,27690,96],{"class":88},[54,27692,23720],{"class":92},[54,27694,89],{"class":88},[54,27696,23896],{"class":1693},[54,27698,89],{"class":88},[54,27700,24757],{"class":92},[54,27702,96],{"class":88},[54,27704,2240],{"class":544},[54,27706,27707],{"class":548},"your_folder",[54,27709,2240],{"class":544},[54,27711,382],{"class":88},[45,27713,27715],{"className":47,"code":27714,"language":49,"meta":50,"style":50},"import os\nprint(os.path.isdir('your_folder'))\n",[17,27716,27717,27723],{"__ignoreMap":50},[54,27718,27719,27721],{"class":56,"line":57},[54,27720,61],{"class":60},[54,27722,23707],{"class":64},[54,27724,27725,27727,27729,27731,27733,27735,27737,27739,27741,27743,27745,27747],{"class":56,"line":68},[54,27726,116],{"class":115},[54,27728,96],{"class":88},[54,27730,23720],{"class":92},[54,27732,89],{"class":88},[54,27734,23896],{"class":1693},[54,27736,89],{"class":88},[54,27738,24991],{"class":92},[54,27740,96],{"class":88},[54,27742,2240],{"class":544},[54,27744,27707],{"class":548},[54,27746,2240],{"class":544},[54,27748,382],{"class":88},[14,27750,27751],{},"These checks help you answer:",[149,27753,27754,27757,27760,27763],{},[152,27755,27756],{},"Where is Python running from?",[152,27758,27759],{},"Does the folder exist?",[152,27761,27762],{},"Is the path really a directory?",[152,27764,27765],{},"What does the current directory contain?",[40,27767,1419],{"id":1418},[602,27769,27771],{"id":27770},"does-oslistdir-return-files-only","Does os.listdir() return files only?",[14,27773,27774],{},"No. It returns names of both files and directories.",[602,27776,27778],{"id":27777},"does-oslistdir-return-full-file-paths","Does os.listdir() return full file paths?",[14,27780,27781,27782,27784],{},"No. It returns names only. Use ",[17,27783,25760],{}," to build full paths.",[602,27786,27788],{"id":27787},"can-i-use-oslistdir-without-a-path","Can I use os.listdir() without a path?",[14,27790,27791],{},"Yes. It will list the current working directory.",[602,27793,27795],{"id":27794},"why-is-my-folder-not-being-found","Why is my folder not being found?",[14,27797,27798,27799,27801],{},"You may be using the wrong relative path. Check ",[17,27800,23766],{}," and confirm the folder exists.",[602,27803,27805],{"id":27804},"how-do-i-sort-the-results","How do I sort the results?",[14,27807,27808,27809,27811],{},"Wrap the result with ",[17,27810,26601],{},", for example:",[45,27813,27815],{"className":47,"code":27814,"language":49,"meta":50,"style":50},"import os\n\nprint(sorted(os.listdir('.')))\n",[17,27816,27817,27823,27827],{"__ignoreMap":50},[54,27818,27819,27821],{"class":56,"line":57},[54,27820,61],{"class":60},[54,27822,23707],{"class":64},[54,27824,27825],{"class":56,"line":68},[54,27826,72],{"emptyLinePlaceholder":71},[54,27828,27829,27831,27833,27836,27838,27840,27842,27844,27846,27848,27850,27852],{"class":56,"line":75},[54,27830,116],{"class":115},[54,27832,96],{"class":88},[54,27834,27835],{"class":115},"sorted",[54,27837,96],{"class":88},[54,27839,23720],{"class":92},[54,27841,89],{"class":88},[54,27843,24433],{"class":92},[54,27845,96],{"class":88},[54,27847,2240],{"class":544},[54,27849,89],{"class":548},[54,27851,2240],{"class":544},[54,27853,446],{"class":88},[40,27855,1510],{"id":1509},[149,27857,27858,27862,27866,27870,27874,27878,27882,27886],{},[152,27859,27860],{},[192,27861,25903],{"href":27589},[152,27863,27864],{},[192,27865,25067],{"href":23871},[152,27867,27868],{},[192,27869,26129],{"href":25757},[152,27871,27872],{},[192,27873,25072],{"href":24853},[152,27875,27876],{},[192,27877,27346],{"href":27345},[152,27879,27880],{},[192,27881,25083],{"href":25082},[152,27883,27884],{},[192,27885,15422],{"href":15421},[152,27887,27888],{},[192,27889,27493],{"href":27492},[1545,27891,27892],{},"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 .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 .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 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":50,"searchDepth":68,"depth":68,"links":27894},[27895,27896,27897,27900,27903,27907,27908,27909,27912,27918,27919,27920,27927],{"id":3595,"depth":68,"text":3596},{"id":26264,"depth":68,"text":26265},{"id":198,"depth":68,"text":199,"children":27898},[27899],{"id":26385,"depth":75,"text":26386},{"id":492,"depth":68,"text":493,"children":27901},[27902],{"id":26555,"depth":75,"text":26556},{"id":26656,"depth":68,"text":26657,"children":27904},[27905,27906],{"id":26665,"depth":75,"text":26666},{"id":26722,"depth":75,"text":26723},{"id":26789,"depth":68,"text":26790},{"id":26971,"depth":68,"text":26972},{"id":27137,"depth":68,"text":27138,"children":27910},[27911],{"id":1820,"depth":75,"text":1821},{"id":27349,"depth":68,"text":27350,"children":27913},[27914,27915,27916,27917],{"id":24454,"depth":75,"text":15337},{"id":24595,"depth":75,"text":24596},{"id":27496,"depth":75,"text":27497},{"id":27538,"depth":75,"text":27539},{"id":27551,"depth":68,"text":27552},{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":27921},[27922,27923,27924,27925,27926],{"id":27770,"depth":75,"text":27771},{"id":27777,"depth":75,"text":27778},{"id":27787,"depth":75,"text":27788},{"id":27794,"depth":75,"text":27795},{"id":27804,"depth":75,"text":27805},{"id":1509,"depth":68,"text":1510},"Master os.listdir function explained in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fos.listdir-function-explained",{"title":26178,"description":27928},"standard-library\u002Fos.listdir-function-explained","80lSXSNt71eACs1LYK7Igmelz1ZgMptDiI-CRfLm-kQ",{"id":27935,"title":27936,"body":27937,"description":29453,"extension":1577,"meta":29454,"navigation":71,"path":24853,"seo":29455,"stem":29456,"__hash__":29457},"content\u002Fstandard-library\u002Fos.path.exists-function-explained.md","os.path.exists() Function Explained",{"type":7,"value":27938,"toc":29418},[27939,27942,27947,27950,27961,27964,27966,28055,28058,28062,28070,28073,28092,28097,28102,28104,28106,28139,28141,28158,28160,28211,28213,28221,28224,28233,28235,28238,28327,28331,28349,28353,28373,28379,28383,28388,28477,28486,28498,28502,28505,28522,28524,28621,28624,28641,28644,28661,28668,28670,28676,28679,28697,28701,28704,28717,28720,28722,28736,28742,28750,28754,28757,28760,28784,28790,28794,28797,28799,28810,28816,28822,28825,28827,28838,28845,28849,28852,28856,28869,28873,28890,28892,28895,28898,28912,28915,28951,28954,28972,28975,28993,28998,29002,29007,29010,29024,29026,29120,29124,29135,29155,29159,29162,29186,29223,29260,29297,29322,29325,29341,29343,29347,29353,29357,29365,29369,29378,29382,29385,29387,29415],[10,27940,27936],{"id":27941},"ospathexists-function-explained",[14,27943,27944,27946],{},[17,27945,24667],{}," checks whether a path exists in Python.",[14,27948,27949],{},"You can use it to test:",[149,27951,27952,27955,27958],{},[152,27953,27954],{},"whether a file exists",[152,27956,27957],{},"whether a folder exists",[152,27959,27960],{},"whether a path is missing",[14,27962,27963],{},"This is useful before you try to open, delete, rename, or work with a path.",[40,27965,3596],{"id":3595},[45,27967,27969],{"className":47,"code":27968,"language":49,"meta":50,"style":50},"import os\n\npath = \"example.txt\"\n\nif os.path.exists(path):\n    print(\"Path exists\")\nelse:\n    print(\"Path does not exist\")\n",[17,27970,27971,27977,27981,27995,27999,28019,28034,28040],{"__ignoreMap":50},[54,27972,27973,27975],{"class":56,"line":57},[54,27974,61],{"class":60},[54,27976,23707],{"class":64},[54,27978,27979],{"class":56,"line":68},[54,27980,72],{"emptyLinePlaceholder":71},[54,27982,27983,27986,27988,27990,27993],{"class":56,"line":75},[54,27984,27985],{"class":64},"path ",[54,27987,82],{"class":81},[54,27989,830],{"class":544},[54,27991,27992],{"class":548},"example.txt",[54,27994,5239],{"class":544},[54,27996,27997],{"class":56,"line":112},[54,27998,72],{"emptyLinePlaceholder":71},[54,28000,28001,28003,28005,28007,28009,28011,28013,28015,28017],{"class":56,"line":887},[54,28002,22942],{"class":60},[54,28004,24000],{"class":64},[54,28006,89],{"class":88},[54,28008,23896],{"class":1693},[54,28010,89],{"class":88},[54,28012,24757],{"class":92},[54,28014,96],{"class":88},[54,28016,23896],{"class":92},[54,28018,10837],{"class":88},[54,28020,28021,28023,28025,28027,28030,28032],{"class":56,"line":892},[54,28022,7606],{"class":115},[54,28024,96],{"class":88},[54,28026,545],{"class":544},[54,28028,28029],{"class":548},"Path exists",[54,28031,545],{"class":544},[54,28033,109],{"class":88},[54,28035,28036,28038],{"class":56,"line":913},[54,28037,22975],{"class":60},[54,28039,7572],{"class":88},[54,28041,28042,28044,28046,28048,28051,28053],{"class":56,"line":2121},[54,28043,7606],{"class":115},[54,28045,96],{"class":88},[54,28047,545],{"class":544},[54,28049,28050],{"class":548},"Path does not exist",[54,28052,545],{"class":544},[54,28054,109],{"class":88},[14,28056,28057],{},"Use this to quickly check whether a file or folder path exists before trying to open or use it.",[40,28059,28061],{"id":28060},"what-ospathexists-does","What os.path.exists() does",[14,28063,28064,22153,28066,28069],{},[17,28065,24667],{},[17,28067,28068],{},"os.path"," tools in Python.",[14,28071,28072],{},"It:",[149,28074,28075,28078,28081,28087],{},[152,28076,28077],{},"checks whether a path exists",[152,28079,28080],{},"works for both files and directories",[152,28082,28083,28084,28086],{},"returns ",[17,28085,9175],{}," if the path exists",[152,28088,28083,28089,28091],{},[17,28090,8767],{}," if the path does not exist",[14,28093,25550,28094,28096],{},[35,28095,3036],{}," tell you whether the path is a file or a folder. It only checks whether something exists at that path.",[14,28098,28099,28100,89],{},"If you want a broader introduction to path tools, see the ",[192,28101,25903],{"href":27589},[40,28103,199],{"id":198},[14,28105,202],{},[45,28107,28109],{"className":47,"code":28108,"language":49,"meta":50,"style":50},"import os\n\nos.path.exists(path)\n",[17,28110,28111,28117,28121],{"__ignoreMap":50},[54,28112,28113,28115],{"class":56,"line":57},[54,28114,61],{"class":60},[54,28116,23707],{"class":64},[54,28118,28119],{"class":56,"line":68},[54,28120,72],{"emptyLinePlaceholder":71},[54,28122,28123,28125,28127,28129,28131,28133,28135,28137],{"class":56,"line":75},[54,28124,23720],{"class":64},[54,28126,89],{"class":88},[54,28128,23896],{"class":1693},[54,28130,89],{"class":88},[54,28132,24757],{"class":92},[54,28134,96],{"class":88},[54,28136,23896],{"class":92},[54,28138,109],{"class":88},[602,28140,26386],{"id":26385},[149,28142,28143,28148,28151],{},[152,28144,28145,28147],{},[17,28146,23896],{}," is usually a string",[152,28149,28150],{},"the function returns a Boolean value",[152,28152,28153,28154,5507,28156],{},"that value is either ",[17,28155,9175],{},[17,28157,8767],{},[14,28159,253],{},[45,28161,28163],{"className":47,"code":28162,"language":49,"meta":50,"style":50},"import os\n\nresult = os.path.exists(\"example.txt\")\nprint(result)\n",[17,28164,28165,28171,28175,28201],{"__ignoreMap":50},[54,28166,28167,28169],{"class":56,"line":57},[54,28168,61],{"class":60},[54,28170,23707],{"class":64},[54,28172,28173],{"class":56,"line":68},[54,28174,72],{"emptyLinePlaceholder":71},[54,28176,28177,28179,28181,28183,28185,28187,28189,28191,28193,28195,28197,28199],{"class":56,"line":75},[54,28178,3833],{"class":64},[54,28180,82],{"class":81},[54,28182,24000],{"class":64},[54,28184,89],{"class":88},[54,28186,23896],{"class":1693},[54,28188,89],{"class":88},[54,28190,24757],{"class":92},[54,28192,96],{"class":88},[54,28194,545],{"class":544},[54,28196,27992],{"class":548},[54,28198,545],{"class":544},[54,28200,109],{"class":88},[54,28202,28203,28205,28207,28209],{"class":56,"line":112},[54,28204,116],{"class":115},[54,28206,96],{"class":88},[54,28208,3866],{"class":92},[54,28210,109],{"class":88},[14,28212,305],{},[45,28214,28215],{"className":47,"code":14763,"language":49,"meta":50,"style":50},[17,28216,28217],{"__ignoreMap":50},[54,28218,28219],{"class":56,"line":57},[54,28220,14763],{"class":8608},[14,28222,28223],{},"or:",[45,28225,28227],{"className":47,"code":28226,"language":49,"meta":50,"style":50},"False\n",[17,28228,28229],{"__ignoreMap":50},[54,28230,28231],{"class":56,"line":57},[54,28232,28226],{"class":8608},[40,28234,493],{"id":492},[14,28236,28237],{},"Here is a simple file check:",[45,28239,28241],{"className":47,"code":28240,"language":49,"meta":50,"style":50},"import os\n\nfile_path = \"example.txt\"\n\nif os.path.exists(file_path):\n    print(\"The file exists.\")\nelse:\n    print(\"The file does not exist.\")\n",[17,28242,28243,28249,28253,28266,28270,28291,28306,28312],{"__ignoreMap":50},[54,28244,28245,28247],{"class":56,"line":57},[54,28246,61],{"class":60},[54,28248,23707],{"class":64},[54,28250,28251],{"class":56,"line":68},[54,28252,72],{"emptyLinePlaceholder":71},[54,28254,28255,28258,28260,28262,28264],{"class":56,"line":75},[54,28256,28257],{"class":64},"file_path ",[54,28259,82],{"class":81},[54,28261,830],{"class":544},[54,28263,27992],{"class":548},[54,28265,5239],{"class":544},[54,28267,28268],{"class":56,"line":112},[54,28269,72],{"emptyLinePlaceholder":71},[54,28271,28272,28274,28276,28278,28280,28282,28284,28286,28289],{"class":56,"line":887},[54,28273,22942],{"class":60},[54,28275,24000],{"class":64},[54,28277,89],{"class":88},[54,28279,23896],{"class":1693},[54,28281,89],{"class":88},[54,28283,24757],{"class":92},[54,28285,96],{"class":88},[54,28287,28288],{"class":92},"file_path",[54,28290,10837],{"class":88},[54,28292,28293,28295,28297,28299,28302,28304],{"class":56,"line":892},[54,28294,7606],{"class":115},[54,28296,96],{"class":88},[54,28298,545],{"class":544},[54,28300,28301],{"class":548},"The file exists.",[54,28303,545],{"class":544},[54,28305,109],{"class":88},[54,28307,28308,28310],{"class":56,"line":913},[54,28309,22975],{"class":60},[54,28311,7572],{"class":88},[54,28313,28314,28316,28318,28320,28323,28325],{"class":56,"line":2121},[54,28315,7606],{"class":115},[54,28317,96],{"class":88},[54,28319,545],{"class":544},[54,28321,28322],{"class":548},"The file does not exist.",[54,28324,545],{"class":544},[54,28326,109],{"class":88},[602,28328,28330],{"id":28329},"expected-output-if-the-file-exists","Expected output if the file exists",[45,28332,28334],{"className":47,"code":28333,"language":49,"meta":50,"style":50},"The file exists.\n",[17,28335,28336],{"__ignoreMap":50},[54,28337,28338,28341,28343,28346],{"class":56,"line":57},[54,28339,28340],{"class":64},"The ",[54,28342,8412],{"class":8295},[54,28344,28345],{"class":64}," exists",[54,28347,28348],{"class":88},".\n",[602,28350,28352],{"id":28351},"expected-output-if-the-file-does-not-exist","Expected output if the file does not exist",[45,28354,28356],{"className":47,"code":28355,"language":49,"meta":50,"style":50},"The file does not exist.\n",[17,28357,28358],{"__ignoreMap":50},[54,28359,28360,28362,28364,28366,28368,28371],{"class":56,"line":57},[54,28361,28340],{"class":64},[54,28363,8412],{"class":8295},[54,28365,19183],{"class":64},[54,28367,3036],{"class":81},[54,28369,28370],{"class":64}," exist",[54,28372,28348],{"class":88},[14,28374,28375,28376,89],{},"This is a common pattern before opening a file. If you are doing that often, you may also want to read ",[192,28377,28378],{"href":25082},"how to check if a file exists in Python",[40,28380,28382],{"id":28381},"checking-a-directory","Checking a directory",[14,28384,28385,28387],{},[17,28386,24667],{}," also works with folders.",[45,28389,28391],{"className":47,"code":28390,"language":49,"meta":50,"style":50},"import os\n\nfolder_path = \"data\"\n\nif os.path.exists(folder_path):\n    print(\"The folder exists.\")\nelse:\n    print(\"The folder does not exist.\")\n",[17,28392,28393,28399,28403,28416,28420,28441,28456,28462],{"__ignoreMap":50},[54,28394,28395,28397],{"class":56,"line":57},[54,28396,61],{"class":60},[54,28398,23707],{"class":64},[54,28400,28401],{"class":56,"line":68},[54,28402,72],{"emptyLinePlaceholder":71},[54,28404,28405,28408,28410,28412,28414],{"class":56,"line":75},[54,28406,28407],{"class":64},"folder_path ",[54,28409,82],{"class":81},[54,28411,830],{"class":544},[54,28413,8313],{"class":548},[54,28415,5239],{"class":544},[54,28417,28418],{"class":56,"line":112},[54,28419,72],{"emptyLinePlaceholder":71},[54,28421,28422,28424,28426,28428,28430,28432,28434,28436,28439],{"class":56,"line":887},[54,28423,22942],{"class":60},[54,28425,24000],{"class":64},[54,28427,89],{"class":88},[54,28429,23896],{"class":1693},[54,28431,89],{"class":88},[54,28433,24757],{"class":92},[54,28435,96],{"class":88},[54,28437,28438],{"class":92},"folder_path",[54,28440,10837],{"class":88},[54,28442,28443,28445,28447,28449,28452,28454],{"class":56,"line":892},[54,28444,7606],{"class":115},[54,28446,96],{"class":88},[54,28448,545],{"class":544},[54,28450,28451],{"class":548},"The folder exists.",[54,28453,545],{"class":544},[54,28455,109],{"class":88},[54,28457,28458,28460],{"class":56,"line":913},[54,28459,22975],{"class":60},[54,28461,7572],{"class":88},[54,28463,28464,28466,28468,28470,28473,28475],{"class":56,"line":2121},[54,28465,7606],{"class":115},[54,28467,96],{"class":88},[54,28469,545],{"class":544},[54,28471,28472],{"class":548},"The folder does not exist.",[54,28474,545],{"class":544},[54,28476,109],{"class":88},[14,28478,28479,28480,28482,28483,28485],{},"This checks whether the path exists, but it does ",[35,28481,3036],{}," tell you whether ",[17,28484,8313],{}," is a file or a directory.",[14,28487,28488,28489,28491,28492,28495,28496,89],{},"For example, if a file named ",[17,28490,8313],{}," exists, ",[17,28493,28494],{},"os.path.exists(\"data\")"," will still return ",[17,28497,9175],{},[40,28499,28501],{"id":28500},"file-or-folder-use-the-right-function","File or folder? Use the right function",[14,28503,28504],{},"Use the right function for the job:",[149,28506,28507,28512,28517],{},[152,28508,28509,28511],{},[17,28510,24667],{}," → checks whether any path exists",[152,28513,28514,28516],{},[17,28515,26353],{}," → checks whether the path exists and is a file",[152,28518,28519,28521],{},[17,28520,27151],{}," → checks whether the path exists and is a directory",[14,28523,253],{},[45,28525,28527],{"className":47,"code":28526,"language":49,"meta":50,"style":50},"import os\n\npath = \"example.txt\"\n\nprint(os.path.exists(path))\nprint(os.path.isfile(path))\nprint(os.path.isdir(path))\n",[17,28528,28529,28535,28539,28551,28555,28577,28599],{"__ignoreMap":50},[54,28530,28531,28533],{"class":56,"line":57},[54,28532,61],{"class":60},[54,28534,23707],{"class":64},[54,28536,28537],{"class":56,"line":68},[54,28538,72],{"emptyLinePlaceholder":71},[54,28540,28541,28543,28545,28547,28549],{"class":56,"line":75},[54,28542,27985],{"class":64},[54,28544,82],{"class":81},[54,28546,830],{"class":544},[54,28548,27992],{"class":548},[54,28550,5239],{"class":544},[54,28552,28553],{"class":56,"line":112},[54,28554,72],{"emptyLinePlaceholder":71},[54,28556,28557,28559,28561,28563,28565,28567,28569,28571,28573,28575],{"class":56,"line":887},[54,28558,116],{"class":115},[54,28560,96],{"class":88},[54,28562,23720],{"class":92},[54,28564,89],{"class":88},[54,28566,23896],{"class":1693},[54,28568,89],{"class":88},[54,28570,24757],{"class":92},[54,28572,96],{"class":88},[54,28574,23896],{"class":92},[54,28576,382],{"class":88},[54,28578,28579,28581,28583,28585,28587,28589,28591,28593,28595,28597],{"class":56,"line":892},[54,28580,116],{"class":115},[54,28582,96],{"class":88},[54,28584,23720],{"class":92},[54,28586,89],{"class":88},[54,28588,23896],{"class":1693},[54,28590,89],{"class":88},[54,28592,27250],{"class":92},[54,28594,96],{"class":88},[54,28596,23896],{"class":92},[54,28598,382],{"class":88},[54,28600,28601,28603,28605,28607,28609,28611,28613,28615,28617,28619],{"class":56,"line":913},[54,28602,116],{"class":115},[54,28604,96],{"class":88},[54,28606,23720],{"class":92},[54,28608,89],{"class":88},[54,28610,23896],{"class":1693},[54,28612,89],{"class":88},[54,28614,24991],{"class":92},[54,28616,96],{"class":88},[54,28618,23896],{"class":92},[54,28620,382],{"class":88},[14,28622,28623],{},"Possible output for a real file:",[45,28625,28627],{"className":47,"code":28626,"language":49,"meta":50,"style":50},"True\nTrue\nFalse\n",[17,28628,28629,28633,28637],{"__ignoreMap":50},[54,28630,28631],{"class":56,"line":57},[54,28632,14763],{"class":8608},[54,28634,28635],{"class":56,"line":68},[54,28636,14763],{"class":8608},[54,28638,28639],{"class":56,"line":75},[54,28640,28226],{"class":8608},[14,28642,28643],{},"Possible output for a real folder:",[45,28645,28647],{"className":47,"code":28646,"language":49,"meta":50,"style":50},"True\nFalse\nTrue\n",[17,28648,28649,28653,28657],{"__ignoreMap":50},[54,28650,28651],{"class":56,"line":57},[54,28652,14763],{"class":8608},[54,28654,28655],{"class":56,"line":68},[54,28656,28226],{"class":8608},[54,28658,28659],{"class":56,"line":75},[54,28660,14763],{"class":8608},[14,28662,28663,28664,28667],{},"If you are building paths from folder names and file names, ",[192,28665,28666],{"href":25757},"os.path.join() explained"," is also useful.",[40,28669,5210],{"id":5209},[14,28671,28672,28673,28675],{},"Beginners often run into problems with ",[17,28674,24667],{}," because the path is not what they think it is.",[14,28677,28678],{},"Common mistakes include:",[149,28680,28681,28684,28687,28690],{},[152,28682,28683],{},"using the wrong relative path",[152,28685,28686],{},"forgetting that the current working directory matters",[152,28688,28689],{},"checking a path after the file was already deleted or moved",[152,28691,28692,28693,28696],{},"assuming ",[17,28694,28695],{},"exists()"," means the file can be opened",[602,28698,28700],{"id":28699},"_1-using-the-wrong-relative-path","1. Using the wrong relative path",[14,28702,28703],{},"This path:",[45,28705,28707],{"className":47,"code":28706,"language":49,"meta":50,"style":50},"\"example.txt\"\n",[17,28708,28709],{"__ignoreMap":50},[54,28710,28711,28713,28715],{"class":56,"line":57},[54,28712,545],{"class":6905},[54,28714,27992],{"class":6908},[54,28716,5239],{"class":6905},[14,28718,28719],{},"means “look in the current working directory.”",[14,28721,28703],{},[45,28723,28725],{"className":47,"code":28724,"language":49,"meta":50,"style":50},"\"data\u002Fexample.txt\"\n",[17,28726,28727],{"__ignoreMap":50},[54,28728,28729,28731,28734],{"class":56,"line":57},[54,28730,545],{"class":6905},[54,28732,28733],{"class":6908},"data\u002Fexample.txt",[54,28735,5239],{"class":6905},[14,28737,28738,28739,28741],{},"means “look in the ",[17,28740,8313],{}," folder inside the current working directory.”",[14,28743,28744,28745,28747,28748,89],{},"If your file is somewhere else, ",[17,28746,28695],{}," will return ",[17,28749,8767],{},[602,28751,28753],{"id":28752},"_2-forgetting-about-the-current-working-directory","2. Forgetting about the current working directory",[14,28755,28756],{},"Python does not always run from the same folder as your script.",[14,28758,28759],{},"Check your current working directory with:",[45,28761,28762],{"className":47,"code":27510,"language":49,"meta":50,"style":50},[17,28763,28764,28770],{"__ignoreMap":50},[54,28765,28766,28768],{"class":56,"line":57},[54,28767,61],{"class":60},[54,28769,23707],{"class":64},[54,28771,28772,28774,28776,28778,28780,28782],{"class":56,"line":68},[54,28773,116],{"class":115},[54,28775,96],{"class":88},[54,28777,23720],{"class":92},[54,28779,89],{"class":88},[54,28781,23725],{"class":92},[54,28783,1950],{"class":88},[14,28785,20319,28786,28789],{},[192,28787,28788],{"href":23871},"os.getcwd() explained"," to understand what folder your script is using.",[602,28791,28793],{"id":28792},"_3-checking-after-the-file-was-moved-or-deleted","3. Checking after the file was moved or deleted",[14,28795,28796],{},"A path that existed a moment ago may no longer exist.",[14,28798,415],{},[149,28800,28801,28804,28807],{},[152,28802,28803],{},"the file was deleted",[152,28805,28806],{},"the file was renamed",[152,28808,28809],{},"the file was moved to another folder",[14,28811,28812,28813,28815],{},"So ",[17,28814,24667],{}," only tells you what is true at the moment you check.",[602,28817,28819,28820,28696],{"id":28818},"_4-assuming-exists-means-the-file-can-be-opened","4. Assuming ",[17,28821,28695],{},[14,28823,28824],{},"A path may exist, but you can still have problems opening it.",[14,28826,415],{},[149,28828,28829,28832,28835],{},[152,28830,28831],{},"you may not have permission",[152,28833,28834],{},"it may be locked by another process",[152,28836,28837],{},"it may be a directory, not a file",[14,28839,28840,28841,89],{},"If you try to open a missing file, Python may raise a ",[192,28842,26077,28843,26081],{"href":24539},[54,28844,26080],{},[40,28846,28848],{"id":28847},"path-examples-beginners-often-need","Path examples beginners often need",[14,28850,28851],{},"Here are some common path formats.",[602,28853,28855],{"id":28854},"file-in-the-current-folder","File in the current folder",[45,28857,28859],{"className":47,"code":28858,"language":49,"meta":50,"style":50},"example.txt\n",[17,28860,28861],{"__ignoreMap":50},[54,28862,28863,28865,28867],{"class":56,"line":57},[54,28864,1820],{"class":64},[54,28866,89],{"class":88},[54,28868,26911],{"class":1693},[602,28870,28872],{"id":28871},"file-in-a-subfolder","File in a subfolder",[45,28874,28876],{"className":47,"code":28875,"language":49,"meta":50,"style":50},"data\u002Fexample.txt\n",[17,28877,28878],{"__ignoreMap":50},[54,28879,28880,28882,28884,28886,28888],{"class":56,"line":57},[54,28881,8313],{"class":64},[54,28883,6676],{"class":81},[54,28885,1820],{"class":64},[54,28887,89],{"class":88},[54,28889,26911],{"class":1693},[602,28891,24226],{"id":24225},[14,28893,28894],{},"An absolute path starts from the full location on your computer.",[14,28896,28897],{},"Windows example:",[45,28899,28901],{"className":47,"code":28900,"language":49,"meta":50,"style":50},"C:\\Users\\YourName\\Documents\\example.txt\n",[17,28902,28903],{"__ignoreMap":50},[54,28904,28905,28907,28909],{"class":56,"line":57},[54,28906,23813],{"class":64},[54,28908,23816],{"class":88},[54,28910,28911],{"class":3275},"Users\\YourName\\Documents\\example.txt\n",[14,28913,28914],{},"In a normal Python string, backslashes need escaping:",[45,28916,28918],{"className":47,"code":28917,"language":49,"meta":50,"style":50},"path = \"C:\\\\Users\\\\YourName\\\\Documents\\\\example.txt\"\n",[17,28919,28920],{"__ignoreMap":50},[54,28921,28922,28924,28926,28928,28930,28933,28935,28937,28940,28942,28945,28947,28949],{"class":56,"line":57},[54,28923,27985],{"class":64},[54,28925,82],{"class":81},[54,28927,830],{"class":544},[54,28929,24313],{"class":548},[54,28931,28932],{"class":4870},"\\\\",[54,28934,24074],{"class":548},[54,28936,28932],{"class":4870},[54,28938,28939],{"class":548},"YourName",[54,28941,28932],{"class":4870},[54,28943,28944],{"class":548},"Documents",[54,28946,28932],{"class":4870},[54,28948,27992],{"class":548},[54,28950,5239],{"class":544},[14,28952,28953],{},"You can also use forward slashes on Windows:",[45,28955,28957],{"className":47,"code":28956,"language":49,"meta":50,"style":50},"path = \"C:\u002FUsers\u002FYourName\u002FDocuments\u002Fexample.txt\"\n",[17,28958,28959],{"__ignoreMap":50},[54,28960,28961,28963,28965,28967,28970],{"class":56,"line":57},[54,28962,27985],{"class":64},[54,28964,82],{"class":81},[54,28966,830],{"class":544},[54,28968,28969],{"class":548},"C:\u002FUsers\u002FYourName\u002FDocuments\u002Fexample.txt",[54,28971,5239],{"class":544},[14,28973,28974],{},"macOS\u002FLinux example:",[45,28976,28978],{"className":47,"code":28977,"language":49,"meta":50,"style":50},"path = \"\u002Fhome\u002Fyourname\u002Fexample.txt\"\n",[17,28979,28980],{"__ignoreMap":50},[54,28981,28982,28984,28986,28988,28991],{"class":56,"line":57},[54,28983,27985],{"class":64},[54,28985,82],{"class":81},[54,28987,830],{"class":544},[54,28989,28990],{"class":548},"\u002Fhome\u002Fyourname\u002Fexample.txt",[54,28992,5239],{"class":544},[14,28994,28995,28996,89],{},"If you want a beginner-friendly explanation of relative and absolute paths, see ",[192,28997,24364],{"href":24363},[40,28999,29001],{"id":29000},"when-to-use-ospathexists","When to use os.path.exists()",[14,29003,29004,29006],{},[17,29005,24667],{}," is useful in many real programs.",[14,29008,29009],{},"Use it:",[149,29011,29012,29015,29018,29021],{},[152,29013,29014],{},"before opening a file",[152,29016,29017],{},"before deleting or renaming a file",[152,29019,29020],{},"before creating something that should not already exist",[152,29022,29023],{},"when validating user input paths",[14,29025,253],{},[45,29027,29029],{"className":47,"code":29028,"language":49,"meta":50,"style":50},"import os\n\npath = input(\"Enter a file path: \")\n\nif os.path.exists(path):\n    print(\"That path exists.\")\nelse:\n    print(\"That path does not exist.\")\n",[17,29030,29031,29037,29041,29060,29064,29084,29099,29105],{"__ignoreMap":50},[54,29032,29033,29035],{"class":56,"line":57},[54,29034,61],{"class":60},[54,29036,23707],{"class":64},[54,29038,29039],{"class":56,"line":68},[54,29040,72],{"emptyLinePlaceholder":71},[54,29042,29043,29045,29047,29049,29051,29053,29056,29058],{"class":56,"line":75},[54,29044,27985],{"class":64},[54,29046,82],{"class":81},[54,29048,20217],{"class":115},[54,29050,96],{"class":88},[54,29052,545],{"class":544},[54,29054,29055],{"class":548},"Enter a file path: ",[54,29057,545],{"class":544},[54,29059,109],{"class":88},[54,29061,29062],{"class":56,"line":112},[54,29063,72],{"emptyLinePlaceholder":71},[54,29065,29066,29068,29070,29072,29074,29076,29078,29080,29082],{"class":56,"line":887},[54,29067,22942],{"class":60},[54,29069,24000],{"class":64},[54,29071,89],{"class":88},[54,29073,23896],{"class":1693},[54,29075,89],{"class":88},[54,29077,24757],{"class":92},[54,29079,96],{"class":88},[54,29081,23896],{"class":92},[54,29083,10837],{"class":88},[54,29085,29086,29088,29090,29092,29095,29097],{"class":56,"line":892},[54,29087,7606],{"class":115},[54,29089,96],{"class":88},[54,29091,545],{"class":544},[54,29093,29094],{"class":548},"That path exists.",[54,29096,545],{"class":544},[54,29098,109],{"class":88},[54,29100,29101,29103],{"class":56,"line":913},[54,29102,22975],{"class":60},[54,29104,7572],{"class":88},[54,29106,29107,29109,29111,29113,29116,29118],{"class":56,"line":2121},[54,29108,7606],{"class":115},[54,29110,96],{"class":88},[54,29112,545],{"class":544},[54,29114,29115],{"class":548},"That path does not exist.",[54,29117,545],{"class":544},[54,29119,109],{"class":88},[40,29121,29123],{"id":29122},"common-causes-of-unexpected-results","Common causes of unexpected results",[14,29125,7429,29126,29128,29129,29131,29132,29134],{},[17,29127,24667],{}," is returning ",[17,29130,8767],{}," when you expect ",[17,29133,9175],{},", common causes are:",[149,29136,29137,29140,29143,29146,29149],{},[152,29138,29139],{},"the path string is misspelled",[152,29141,29142],{},"the file is in a different folder than expected",[152,29144,29145],{},"the script is running from a different working directory",[152,29147,29148],{},"a relative path is used when an absolute path is needed",[152,29150,29151,29152,29154],{},"you expect ",[17,29153,28695],{}," to check permissions instead of existence",[40,29156,29158],{"id":29157},"useful-debugging-checks","Useful debugging checks",[14,29160,29161],{},"These quick commands can help you debug path problems:",[45,29163,29164],{"className":47,"code":27510,"language":49,"meta":50,"style":50},[17,29165,29166,29172],{"__ignoreMap":50},[54,29167,29168,29170],{"class":56,"line":57},[54,29169,61],{"class":60},[54,29171,23707],{"class":64},[54,29173,29174,29176,29178,29180,29182,29184],{"class":56,"line":68},[54,29175,116],{"class":115},[54,29177,96],{"class":88},[54,29179,23720],{"class":92},[54,29181,89],{"class":88},[54,29183,23725],{"class":92},[54,29185,1950],{"class":88},[45,29187,29189],{"className":47,"code":29188,"language":49,"meta":50,"style":50},"import os\nprint(os.path.exists(\"example.txt\"))\n",[17,29190,29191,29197],{"__ignoreMap":50},[54,29192,29193,29195],{"class":56,"line":57},[54,29194,61],{"class":60},[54,29196,23707],{"class":64},[54,29198,29199,29201,29203,29205,29207,29209,29211,29213,29215,29217,29219,29221],{"class":56,"line":68},[54,29200,116],{"class":115},[54,29202,96],{"class":88},[54,29204,23720],{"class":92},[54,29206,89],{"class":88},[54,29208,23896],{"class":1693},[54,29210,89],{"class":88},[54,29212,24757],{"class":92},[54,29214,96],{"class":88},[54,29216,545],{"class":544},[54,29218,27992],{"class":548},[54,29220,545],{"class":544},[54,29222,382],{"class":88},[45,29224,29226],{"className":47,"code":29225,"language":49,"meta":50,"style":50},"import os\nprint(os.path.isfile(\"example.txt\"))\n",[17,29227,29228,29234],{"__ignoreMap":50},[54,29229,29230,29232],{"class":56,"line":57},[54,29231,61],{"class":60},[54,29233,23707],{"class":64},[54,29235,29236,29238,29240,29242,29244,29246,29248,29250,29252,29254,29256,29258],{"class":56,"line":68},[54,29237,116],{"class":115},[54,29239,96],{"class":88},[54,29241,23720],{"class":92},[54,29243,89],{"class":88},[54,29245,23896],{"class":1693},[54,29247,89],{"class":88},[54,29249,27250],{"class":92},[54,29251,96],{"class":88},[54,29253,545],{"class":544},[54,29255,27992],{"class":548},[54,29257,545],{"class":544},[54,29259,382],{"class":88},[45,29261,29263],{"className":47,"code":29262,"language":49,"meta":50,"style":50},"import os\nprint(os.path.isdir(\"example.txt\"))\n",[17,29264,29265,29271],{"__ignoreMap":50},[54,29266,29267,29269],{"class":56,"line":57},[54,29268,61],{"class":60},[54,29270,23707],{"class":64},[54,29272,29273,29275,29277,29279,29281,29283,29285,29287,29289,29291,29293,29295],{"class":56,"line":68},[54,29274,116],{"class":115},[54,29276,96],{"class":88},[54,29278,23720],{"class":92},[54,29280,89],{"class":88},[54,29282,23896],{"class":1693},[54,29284,89],{"class":88},[54,29286,24991],{"class":92},[54,29288,96],{"class":88},[54,29290,545],{"class":544},[54,29292,27992],{"class":548},[54,29294,545],{"class":544},[54,29296,382],{"class":88},[45,29298,29300],{"className":47,"code":29299,"language":49,"meta":50,"style":50},"import os\nprint(os.listdir())\n",[17,29301,29302,29308],{"__ignoreMap":50},[54,29303,29304,29306],{"class":56,"line":57},[54,29305,61],{"class":60},[54,29307,23707],{"class":64},[54,29309,29310,29312,29314,29316,29318,29320],{"class":56,"line":68},[54,29311,116],{"class":115},[54,29313,96],{"class":88},[54,29315,23720],{"class":92},[54,29317,89],{"class":88},[54,29319,24433],{"class":92},[54,29321,1950],{"class":88},[14,29323,29324],{},"These checks help you answer questions like:",[149,29326,29327,29330,29332,29335,29338],{},[152,29328,29329],{},"What folder am I running from?",[152,29331,25012],{},[152,29333,29334],{},"Is it a file?",[152,29336,29337],{},"Is it a directory?",[152,29339,29340],{},"What files are actually in the current folder?",[40,29342,1419],{"id":1418},[602,29344,29346],{"id":29345},"does-ospathexists-work-for-folders","Does os.path.exists() work for folders?",[14,29348,29349,29350,29352],{},"Yes. It returns ",[17,29351,9175],{}," for both files and directories if the path exists.",[602,29354,29356],{"id":29355},"what-does-ospathexists-return","What does os.path.exists() return?",[14,29358,25027,29359,29361,29362,29364],{},[17,29360,9175],{}," if the path exists and ",[17,29363,8767],{}," if it does not.",[602,29366,29368],{"id":29367},"what-is-the-difference-between-ospathexists-and-ospathisfile","What is the difference between os.path.exists() and os.path.isfile()?",[14,29370,29371,29373,29374,29377],{},[17,29372,28695],{}," checks whether any path exists. ",[17,29375,29376],{},"isfile()"," checks whether the path exists and is a file.",[602,29379,29381],{"id":29380},"why-does-ospathexists-return-false-when-the-file-is-there","Why does os.path.exists() return False when the file is there?",[14,29383,29384],{},"The path may be wrong, or your script may be running from a different working directory than you expect.",[40,29386,1510],{"id":1509},[149,29388,29389,29393,29397,29401,29405,29409],{},[152,29390,29391],{},[192,29392,25903],{"href":27589},[152,29394,29395],{},[192,29396,26129],{"href":25757},[152,29398,29399],{},[192,29400,25067],{"href":23871},[152,29402,29403],{},[192,29404,25083],{"href":25082},[152,29406,29407],{},[192,29408,25077],{"href":24363},[152,29410,29411],{},[192,29412,26077,29413,26145],{"href":24539},[54,29414,26080],{},[1545,29416,29417],{},"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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .srjyR, html code.shiki .srjyR{--shiki-light:#90A4AE;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":29419},[29420,29421,29422,29425,29429,29430,29431,29438,29443,29444,29445,29446,29452],{"id":3595,"depth":68,"text":3596},{"id":28060,"depth":68,"text":28061},{"id":198,"depth":68,"text":199,"children":29423},[29424],{"id":26385,"depth":75,"text":26386},{"id":492,"depth":68,"text":493,"children":29426},[29427,29428],{"id":28329,"depth":75,"text":28330},{"id":28351,"depth":75,"text":28352},{"id":28381,"depth":68,"text":28382},{"id":28500,"depth":68,"text":28501},{"id":5209,"depth":68,"text":5210,"children":29432},[29433,29434,29435,29436],{"id":28699,"depth":75,"text":28700},{"id":28752,"depth":75,"text":28753},{"id":28792,"depth":75,"text":28793},{"id":28818,"depth":75,"text":29437},"4. Assuming exists() means the file can be opened",{"id":28847,"depth":68,"text":28848,"children":29439},[29440,29441,29442],{"id":28854,"depth":75,"text":28855},{"id":28871,"depth":75,"text":28872},{"id":24225,"depth":75,"text":24226},{"id":29000,"depth":68,"text":29001},{"id":29122,"depth":68,"text":29123},{"id":29157,"depth":68,"text":29158},{"id":1418,"depth":68,"text":1419,"children":29447},[29448,29449,29450,29451],{"id":29345,"depth":75,"text":29346},{"id":29355,"depth":75,"text":29356},{"id":29367,"depth":75,"text":29368},{"id":29380,"depth":75,"text":29381},{"id":1509,"depth":68,"text":1510},"Master os.path.exists function explained in our comprehensive Python beginner guide.",{},{"title":27936,"description":29453},"standard-library\u002Fos.path.exists-function-explained","28Hqs9kvfwsrArdBmH5MRexpi4MmlbsxLgsVAOMCVpM",{"id":29459,"title":29460,"body":29461,"description":30998,"extension":1577,"meta":30999,"navigation":71,"path":25757,"seo":31000,"stem":31001,"__hash__":31002},"content\u002Fstandard-library\u002Fos.path.join-function-explained.md","os.path.join() Function Explained",{"type":7,"value":29462,"toc":30959},[29463,29466,29471,29480,29482,29550,29555,29578,29581,29603,29606,29611,29616,29621,29623,29641,29649,29651,29654,29714,29716,29731,29734,29758,29761,29763,29766,29826,29828,29847,29851,29862,29870,29874,29879,29978,29982,29993,29998,30033,30036,30040,30043,30047,30050,30118,30127,30130,30134,30137,30201,30204,30208,30213,30217,30228,30235,30237,30308,30312,30317,30319,30333,30335,30460,30465,30467,30470,30476,30479,30544,30549,30555,30558,30618,30621,30680,30684,30689,30700,30704,30707,30710,30776,30781,30858,30862,30881,30886,30888,30894,30897,30907,30910,30916,30919,30923,30926,30928,30956],[10,29464,29460],{"id":29465},"ospathjoin-function-explained",[14,29467,29468,29470],{},[17,29469,25760],{}," combines file and folder names into a single path string.",[14,29472,29473,29474,134,29476,29479],{},"Beginners should use it instead of joining path parts by hand because it uses the correct path separator for the current operating system. This helps you avoid mistakes with ",[17,29475,6676],{},[17,29477,29478],{},"\\"," when your code runs on different computers.",[40,29481,3596],{"id":3595},[45,29483,29485],{"className":47,"code":29484,"language":49,"meta":50,"style":50},"import os\n\nfile_path = os.path.join(\"data\", \"reports\", \"output.txt\")\nprint(file_path)\n",[17,29486,29487,29493,29497,29540],{"__ignoreMap":50},[54,29488,29489,29491],{"class":56,"line":57},[54,29490,61],{"class":60},[54,29492,23707],{"class":64},[54,29494,29495],{"class":56,"line":68},[54,29496,72],{"emptyLinePlaceholder":71},[54,29498,29499,29501,29503,29505,29507,29509,29511,29513,29515,29517,29519,29521,29523,29525,29527,29529,29531,29533,29536,29538],{"class":56,"line":75},[54,29500,28257],{"class":64},[54,29502,82],{"class":81},[54,29504,24000],{"class":64},[54,29506,89],{"class":88},[54,29508,23896],{"class":1693},[54,29510,89],{"class":88},[54,29512,25725],{"class":92},[54,29514,96],{"class":88},[54,29516,545],{"class":544},[54,29518,8313],{"class":548},[54,29520,545],{"class":544},[54,29522,103],{"class":88},[54,29524,830],{"class":544},[54,29526,23859],{"class":548},[54,29528,545],{"class":544},[54,29530,103],{"class":88},[54,29532,830],{"class":544},[54,29534,29535],{"class":548},"output.txt",[54,29537,545],{"class":544},[54,29539,109],{"class":88},[54,29541,29542,29544,29546,29548],{"class":56,"line":112},[54,29543,116],{"class":115},[54,29545,96],{"class":88},[54,29547,28288],{"class":92},[54,29549,109],{"class":88},[14,29551,29552],{},[35,29553,29554],{},"What this does:",[149,29556,29557,29570,29573],{},[152,29558,29559,29560,320,29563,3587,29566,29569],{},"Combines ",[17,29561,29562],{},"\"data\"",[17,29564,29565],{},"\"reports\"",[17,29567,29568],{},"\"output.txt\""," into one path",[152,29571,29572],{},"Returns the path as a string",[152,29574,8346,29575,29577],{},[35,29576,3036],{}," create the file or folder",[14,29579,29580],{},"On one system, the output might look like this:",[45,29582,29584],{"className":47,"code":29583,"language":49,"meta":50,"style":50},"data\u002Freports\u002Foutput.txt\n",[17,29585,29586],{"__ignoreMap":50},[54,29587,29588,29590,29592,29594,29596,29599,29601],{"class":56,"line":57},[54,29589,8313],{"class":64},[54,29591,6676],{"class":81},[54,29593,23859],{"class":64},[54,29595,6676],{"class":81},[54,29597,29598],{"class":64},"output",[54,29600,89],{"class":88},[54,29602,26911],{"class":1693},[14,29604,29605],{},"On another system, it might use a different separator.",[14,29607,126,29608,29610],{},[17,29609,25760],{}," to combine folder and file names into one path without manually typing path separators.",[40,29612,1736,29614,1739],{"id":29613},"what-ospathjoin-does",[17,29615,25760],{},[14,29617,29618,29620],{},[17,29619,25760],{}," is used to build paths safely.",[14,29622,28072],{},[149,29624,29625,29628,29631,29638],{},[152,29626,29627],{},"Combines multiple path parts into one path string",[152,29629,29630],{},"Uses the correct path separator for the current operating system",[152,29632,29633,29634,5507,29636],{},"Helps avoid mistakes from manually adding ",[17,29635,6676],{},[17,29637,29478],{},[152,29639,29640],{},"Is useful when building file and folder paths step by step",[14,29642,29643,29644,29648],{},"This is part of the ",[192,29645,29646,160],{"href":27589},[17,29647,23720],{},", which contains tools for working with the operating system.",[40,29650,199],{"id":198},[14,29652,29653],{},"A common form looks like this:",[45,29655,29657],{"className":47,"code":29656,"language":49,"meta":50,"style":50},"import os\n\npath = os.path.join(\"folder\", \"file.txt\")\nprint(path)\n",[17,29658,29659,29665,29669,29704],{"__ignoreMap":50},[54,29660,29661,29663],{"class":56,"line":57},[54,29662,61],{"class":60},[54,29664,23707],{"class":64},[54,29666,29667],{"class":56,"line":68},[54,29668,72],{"emptyLinePlaceholder":71},[54,29670,29671,29673,29675,29677,29679,29681,29683,29685,29687,29689,29691,29693,29695,29697,29700,29702],{"class":56,"line":75},[54,29672,27985],{"class":64},[54,29674,82],{"class":81},[54,29676,24000],{"class":64},[54,29678,89],{"class":88},[54,29680,23896],{"class":1693},[54,29682,89],{"class":88},[54,29684,25725],{"class":92},[54,29686,96],{"class":88},[54,29688,545],{"class":544},[54,29690,24762],{"class":548},[54,29692,545],{"class":544},[54,29694,103],{"class":88},[54,29696,830],{"class":544},[54,29698,29699],{"class":548},"file.txt",[54,29701,545],{"class":544},[54,29703,109],{"class":88},[54,29705,29706,29708,29710,29712],{"class":56,"line":112},[54,29707,116],{"class":115},[54,29709,96],{"class":88},[54,29711,23896],{"class":92},[54,29713,109],{"class":88},[602,29715,26386],{"id":26385},[149,29717,29718,29721,29724,29727],{},[152,29719,29720],{},"Each argument is one part of the path",[152,29722,29723],{},"The function returns a new string",[152,29725,29726],{},"It only builds the path",[152,29728,25550,29729,29577],{},[35,29730,3036],{},[14,29732,29733],{},"You can think of it as a safer replacement for writing something like:",[45,29735,29737],{"className":47,"code":29736,"language":49,"meta":50,"style":50},"folder + \"\u002F\" + file_name\n",[17,29738,29739],{"__ignoreMap":50},[54,29740,29741,29743,29746,29748,29750,29752,29755],{"class":56,"line":57},[54,29742,24696],{"class":64},[54,29744,29745],{"class":81},"+",[54,29747,830],{"class":544},[54,29749,6676],{"class":548},[54,29751,545],{"class":544},[54,29753,29754],{"class":81}," +",[54,29756,29757],{"class":64}," file_name\n",[14,29759,29760],{},"That manual approach is more error-prone.",[40,29762,493],{"id":492},[14,29764,29765],{},"Here is a basic example with one folder and one file name:",[45,29767,29769],{"className":47,"code":29768,"language":49,"meta":50,"style":50},"import os\n\npath = os.path.join(\"images\", \"photo.jpg\")\nprint(path)\n",[17,29770,29771,29777,29781,29816],{"__ignoreMap":50},[54,29772,29773,29775],{"class":56,"line":57},[54,29774,61],{"class":60},[54,29776,23707],{"class":64},[54,29778,29779],{"class":56,"line":68},[54,29780,72],{"emptyLinePlaceholder":71},[54,29782,29783,29785,29787,29789,29791,29793,29795,29797,29799,29801,29803,29805,29807,29809,29812,29814],{"class":56,"line":75},[54,29784,27985],{"class":64},[54,29786,82],{"class":81},[54,29788,24000],{"class":64},[54,29790,89],{"class":88},[54,29792,23896],{"class":1693},[54,29794,89],{"class":88},[54,29796,25725],{"class":92},[54,29798,96],{"class":88},[54,29800,545],{"class":544},[54,29802,26297],{"class":548},[54,29804,545],{"class":544},[54,29806,103],{"class":88},[54,29808,830],{"class":544},[54,29810,29811],{"class":548},"photo.jpg",[54,29813,545],{"class":544},[54,29815,109],{"class":88},[54,29817,29818,29820,29822,29824],{"class":56,"line":112},[54,29819,116],{"class":115},[54,29821,96],{"class":88},[54,29823,23896],{"class":92},[54,29825,109],{"class":88},[14,29827,305],{},[45,29829,29831],{"className":47,"code":29830,"language":49,"meta":50,"style":50},"images\u002Fphoto.jpg\n",[17,29832,29833],{"__ignoreMap":50},[54,29834,29835,29837,29839,29842,29844],{"class":56,"line":57},[54,29836,26297],{"class":64},[54,29838,6676],{"class":81},[54,29840,29841],{"class":64},"photo",[54,29843,89],{"class":88},[54,29845,29846],{"class":1693},"jpg\n",[602,29848,29850],{"id":29849},"why-this-is-useful","Why this is useful",[149,29852,29853,29856,29859],{},[152,29854,29855],{},"The result depends on the operating system",[152,29857,29858],{},"Your code is easier to move between computers",[152,29860,29861],{},"You do not have to remember which separator to use",[14,29863,29864,29865,89],{},"This becomes especially helpful when you later open the file with the ",[192,29866,29868,411],{"href":29867},"\u002Freference\u002Fpython-open-function-explained\u002F",[17,29869,8524],{},[40,29871,29873],{"id":29872},"joining-many-path-parts","Joining many path parts",[14,29875,29876,29877,89],{},"You can pass more than two parts to ",[17,29878,25760],{},[45,29880,29882],{"className":47,"code":29881,"language":49,"meta":50,"style":50},"import os\n\nbase_folder = \"projects\"\nsubfolder = \"python\"\nfilename = \"notes.txt\"\n\npath = os.path.join(base_folder, subfolder, filename)\nprint(path)\n",[17,29883,29884,29890,29894,29907,29920,29932,29936,29968],{"__ignoreMap":50},[54,29885,29886,29888],{"class":56,"line":57},[54,29887,61],{"class":60},[54,29889,23707],{"class":64},[54,29891,29892],{"class":56,"line":68},[54,29893,72],{"emptyLinePlaceholder":71},[54,29895,29896,29899,29901,29903,29905],{"class":56,"line":75},[54,29897,29898],{"class":64},"base_folder ",[54,29900,82],{"class":81},[54,29902,830],{"class":544},[54,29904,25576],{"class":548},[54,29906,5239],{"class":544},[54,29908,29909,29912,29914,29916,29918],{"class":56,"line":112},[54,29910,29911],{"class":64},"subfolder ",[54,29913,82],{"class":81},[54,29915,830],{"class":544},[54,29917,49],{"class":548},[54,29919,5239],{"class":544},[54,29921,29922,29924,29926,29928,29930],{"class":56,"line":887},[54,29923,25698],{"class":64},[54,29925,82],{"class":81},[54,29927,830],{"class":544},[54,29929,24577],{"class":548},[54,29931,5239],{"class":544},[54,29933,29934],{"class":56,"line":892},[54,29935,72],{"emptyLinePlaceholder":71},[54,29937,29938,29940,29942,29944,29946,29948,29950,29952,29954,29957,29959,29962,29964,29966],{"class":56,"line":913},[54,29939,27985],{"class":64},[54,29941,82],{"class":81},[54,29943,24000],{"class":64},[54,29945,89],{"class":88},[54,29947,23896],{"class":1693},[54,29949,89],{"class":88},[54,29951,25725],{"class":92},[54,29953,96],{"class":88},[54,29955,29956],{"class":92},"base_folder",[54,29958,103],{"class":88},[54,29960,29961],{"class":92}," subfolder",[54,29963,103],{"class":88},[54,29965,25734],{"class":92},[54,29967,109],{"class":88},[54,29969,29970,29972,29974,29976],{"class":56,"line":2121},[54,29971,116],{"class":115},[54,29973,96],{"class":88},[54,29975,23896],{"class":92},[54,29977,109],{"class":88},[602,29979,29981],{"id":29980},"why-this-is-helpful","Why this is helpful",[149,29983,29984,29987,29990],{},[152,29985,29986],{},"Useful for nested folders",[152,29988,29989],{},"Easier to read than long string concatenation",[152,29991,29992],{},"Good when path parts come from variables",[14,29994,8801,29995,29997],{},[17,29996,25760],{},", beginners often write code like this:",[45,29999,30001],{"className":47,"code":30000,"language":49,"meta":50,"style":50},"base_folder + \"\u002F\" + subfolder + \"\u002F\" + filename\n",[17,30002,30003],{"__ignoreMap":50},[54,30004,30005,30007,30009,30011,30013,30015,30017,30020,30022,30024,30026,30028,30030],{"class":56,"line":57},[54,30006,29898],{"class":64},[54,30008,29745],{"class":81},[54,30010,830],{"class":544},[54,30012,6676],{"class":548},[54,30014,545],{"class":544},[54,30016,29754],{"class":81},[54,30018,30019],{"class":64}," subfolder ",[54,30021,29745],{"class":81},[54,30023,830],{"class":544},[54,30025,6676],{"class":548},[54,30027,545],{"class":544},[54,30029,29754],{"class":81},[54,30031,30032],{"class":64}," filename\n",[14,30034,30035],{},"That works in some cases, but it is easier to make mistakes.",[40,30037,30039],{"id":30038},"important-behavior-to-know","Important behavior to know",[14,30041,30042],{},"There are a few details beginners should understand.",[602,30044,30046],{"id":30045},"a-later-absolute-path-can-override-earlier-parts","A later absolute path can override earlier parts",[14,30048,30049],{},"If one of the later arguments is an absolute path, earlier parts may be ignored.",[45,30051,30053],{"className":47,"code":30052,"language":49,"meta":50,"style":50},"import os\n\npath = os.path.join(\"folder\", \"\u002Ftmp\", \"file.txt\")\nprint(path)\n",[17,30054,30055,30061,30065,30108],{"__ignoreMap":50},[54,30056,30057,30059],{"class":56,"line":57},[54,30058,61],{"class":60},[54,30060,23707],{"class":64},[54,30062,30063],{"class":56,"line":68},[54,30064,72],{"emptyLinePlaceholder":71},[54,30066,30067,30069,30071,30073,30075,30077,30079,30081,30083,30085,30087,30089,30091,30093,30096,30098,30100,30102,30104,30106],{"class":56,"line":75},[54,30068,27985],{"class":64},[54,30070,82],{"class":81},[54,30072,24000],{"class":64},[54,30074,89],{"class":88},[54,30076,23896],{"class":1693},[54,30078,89],{"class":88},[54,30080,25725],{"class":92},[54,30082,96],{"class":88},[54,30084,545],{"class":544},[54,30086,24762],{"class":548},[54,30088,545],{"class":544},[54,30090,103],{"class":88},[54,30092,830],{"class":544},[54,30094,30095],{"class":548},"\u002Ftmp",[54,30097,545],{"class":544},[54,30099,103],{"class":88},[54,30101,830],{"class":544},[54,30103,29699],{"class":548},[54,30105,545],{"class":544},[54,30107,109],{"class":88},[54,30109,30110,30112,30114,30116],{"class":56,"line":112},[54,30111,116],{"class":115},[54,30113,96],{"class":88},[54,30115,23896],{"class":92},[54,30117,109],{"class":88},[14,30119,30120,30121,3360,30124,89],{},"On many systems, the result will start from ",[17,30122,30123],{},"\"\u002Ftmp\"",[17,30125,30126],{},"\"folder\"",[14,30128,30129],{},"So if your final path looks wrong, check whether one argument is an absolute path.",[602,30131,30133],{"id":30132},"empty-strings-can-affect-the-result","Empty strings can affect the result",[14,30135,30136],{},"Empty strings do not always cause an error, but they can change the final path.",[45,30138,30140],{"className":47,"code":30139,"language":49,"meta":50,"style":50},"import os\n\npath = os.path.join(\"data\", \"\", \"file.txt\")\nprint(path)\n",[17,30141,30142,30148,30152,30191],{"__ignoreMap":50},[54,30143,30144,30146],{"class":56,"line":57},[54,30145,61],{"class":60},[54,30147,23707],{"class":64},[54,30149,30150],{"class":56,"line":68},[54,30151,72],{"emptyLinePlaceholder":71},[54,30153,30154,30156,30158,30160,30162,30164,30166,30168,30170,30172,30174,30176,30178,30181,30183,30185,30187,30189],{"class":56,"line":75},[54,30155,27985],{"class":64},[54,30157,82],{"class":81},[54,30159,24000],{"class":64},[54,30161,89],{"class":88},[54,30163,23896],{"class":1693},[54,30165,89],{"class":88},[54,30167,25725],{"class":92},[54,30169,96],{"class":88},[54,30171,545],{"class":544},[54,30173,8313],{"class":548},[54,30175,545],{"class":544},[54,30177,103],{"class":88},[54,30179,30180],{"class":544}," \"\"",[54,30182,103],{"class":88},[54,30184,830],{"class":544},[54,30186,29699],{"class":548},[54,30188,545],{"class":544},[54,30190,109],{"class":88},[54,30192,30193,30195,30197,30199],{"class":56,"line":112},[54,30194,116],{"class":115},[54,30196,96],{"class":88},[54,30198,23896],{"class":92},[54,30200,109],{"class":88},[14,30202,30203],{},"This may still produce a valid path, but it is a good idea to inspect your inputs if the result is unexpected.",[602,30205,30207],{"id":30206},"it-does-not-check-whether-the-path-exists","It does not check whether the path exists",[14,30209,30210,30212],{},[17,30211,25760],{}," only builds a string.",[14,30214,25550,30215,147],{},[35,30216,3036],{},[149,30218,30219,30222,30225],{},[152,30220,30221],{},"check whether the file exists",[152,30223,30224],{},"check whether the folder exists",[152,30226,30227],{},"create anything on disk",[14,30229,30230,30231,89],{},"If you need to test whether the path exists, use ",[192,30232,30233],{"href":24853},[17,30234,24667],{},[14,30236,253],{},[45,30238,30240],{"className":47,"code":30239,"language":49,"meta":50,"style":50},"import os\n\npath = os.path.join(\"data\", \"file.txt\")\nprint(os.path.exists(path))\n",[17,30241,30242,30248,30252,30286],{"__ignoreMap":50},[54,30243,30244,30246],{"class":56,"line":57},[54,30245,61],{"class":60},[54,30247,23707],{"class":64},[54,30249,30250],{"class":56,"line":68},[54,30251,72],{"emptyLinePlaceholder":71},[54,30253,30254,30256,30258,30260,30262,30264,30266,30268,30270,30272,30274,30276,30278,30280,30282,30284],{"class":56,"line":75},[54,30255,27985],{"class":64},[54,30257,82],{"class":81},[54,30259,24000],{"class":64},[54,30261,89],{"class":88},[54,30263,23896],{"class":1693},[54,30265,89],{"class":88},[54,30267,25725],{"class":92},[54,30269,96],{"class":88},[54,30271,545],{"class":544},[54,30273,8313],{"class":548},[54,30275,545],{"class":544},[54,30277,103],{"class":88},[54,30279,830],{"class":544},[54,30281,29699],{"class":548},[54,30283,545],{"class":544},[54,30285,109],{"class":88},[54,30287,30288,30290,30292,30294,30296,30298,30300,30302,30304,30306],{"class":56,"line":112},[54,30289,116],{"class":115},[54,30291,96],{"class":88},[54,30293,23720],{"class":92},[54,30295,89],{"class":88},[54,30297,23896],{"class":1693},[54,30299,89],{"class":88},[54,30301,24757],{"class":92},[54,30303,96],{"class":88},[54,30305,23896],{"class":92},[54,30307,382],{"class":88},[40,30309,30311],{"id":30310},"when-to-use-it","When to use it",[14,30313,126,30314,30316],{},[17,30315,25760],{}," whenever you need to build a path from separate parts.",[14,30318,19728],{},[149,30320,30321,30324,30327,30330],{},[152,30322,30323],{},"Opening files in subfolders",[152,30325,30326],{},"Saving output files",[152,30328,30329],{},"Looping through file names in a directory",[152,30331,30332],{},"Writing code that should work across operating systems",[14,30334,253],{},[45,30336,30338],{"className":47,"code":30337,"language":49,"meta":50,"style":50},"import os\n\nfolder = \"data\"\nfilename = \"report.txt\"\npath = os.path.join(folder, filename)\n\nwith open(path, \"r\") as file:\n    content = file.read()\n\nprint(content)\n",[17,30339,30340,30346,30350,30362,30374,30400,30404,30430,30445,30449],{"__ignoreMap":50},[54,30341,30342,30344],{"class":56,"line":57},[54,30343,61],{"class":60},[54,30345,23707],{"class":64},[54,30347,30348],{"class":56,"line":68},[54,30349,72],{"emptyLinePlaceholder":71},[54,30351,30352,30354,30356,30358,30360],{"class":56,"line":75},[54,30353,24696],{"class":64},[54,30355,82],{"class":81},[54,30357,830],{"class":544},[54,30359,8313],{"class":548},[54,30361,5239],{"class":544},[54,30363,30364,30366,30368,30370,30372],{"class":56,"line":112},[54,30365,25698],{"class":64},[54,30367,82],{"class":81},[54,30369,830],{"class":544},[54,30371,25705],{"class":548},[54,30373,5239],{"class":544},[54,30375,30376,30378,30380,30382,30384,30386,30388,30390,30392,30394,30396,30398],{"class":56,"line":887},[54,30377,27985],{"class":64},[54,30379,82],{"class":81},[54,30381,24000],{"class":64},[54,30383,89],{"class":88},[54,30385,23896],{"class":1693},[54,30387,89],{"class":88},[54,30389,25725],{"class":92},[54,30391,96],{"class":88},[54,30393,24762],{"class":92},[54,30395,103],{"class":88},[54,30397,25734],{"class":92},[54,30399,109],{"class":88},[54,30401,30402],{"class":56,"line":892},[54,30403,72],{"emptyLinePlaceholder":71},[54,30405,30406,30408,30410,30412,30414,30416,30418,30420,30422,30424,30426,30428],{"class":56,"line":913},[54,30407,8267],{"class":60},[54,30409,8270],{"class":115},[54,30411,96],{"class":88},[54,30413,23896],{"class":92},[54,30415,103],{"class":88},[54,30417,830],{"class":544},[54,30419,10658],{"class":548},[54,30421,545],{"class":544},[54,30423,876],{"class":88},[54,30425,7631],{"class":60},[54,30427,8296],{"class":8295},[54,30429,7572],{"class":88},[54,30431,30432,30435,30437,30439,30441,30443],{"class":56,"line":2121},[54,30433,30434],{"class":64},"    content ",[54,30436,82],{"class":81},[54,30438,8296],{"class":8295},[54,30440,89],{"class":88},[54,30442,17821],{"class":92},[54,30444,1670],{"class":88},[54,30446,30447],{"class":56,"line":2136},[54,30448,72],{"emptyLinePlaceholder":71},[54,30450,30451,30453,30455,30458],{"class":56,"line":2151},[54,30452,116],{"class":115},[54,30454,96],{"class":88},[54,30456,30457],{"class":92},"content",[54,30459,109],{"class":88},[14,30461,30462,30463,89],{},"If you want a broader explanation of path building, see ",[192,30464,24364],{"href":24363},[40,30466,5210],{"id":5209},[14,30468,30469],{},"These are some common problems beginners run into.",[602,30471,5481,30473,30475],{"id":30472},"using-to-join-path-pieces",[17,30474,29745],{}," to join path pieces",[14,30477,30478],{},"This is a common habit:",[45,30480,30482],{"className":47,"code":30481,"language":49,"meta":50,"style":50},"folder = \"data\"\nfile_name = \"report.txt\"\n\npath = folder + \"\u002F\" + file_name\nprint(path)\n",[17,30483,30484,30496,30509,30513,30534],{"__ignoreMap":50},[54,30485,30486,30488,30490,30492,30494],{"class":56,"line":57},[54,30487,24696],{"class":64},[54,30489,82],{"class":81},[54,30491,830],{"class":544},[54,30493,8313],{"class":548},[54,30495,5239],{"class":544},[54,30497,30498,30501,30503,30505,30507],{"class":56,"line":68},[54,30499,30500],{"class":64},"file_name ",[54,30502,82],{"class":81},[54,30504,830],{"class":544},[54,30506,25705],{"class":548},[54,30508,5239],{"class":544},[54,30510,30511],{"class":56,"line":75},[54,30512,72],{"emptyLinePlaceholder":71},[54,30514,30515,30517,30519,30522,30524,30526,30528,30530,30532],{"class":56,"line":112},[54,30516,27985],{"class":64},[54,30518,82],{"class":81},[54,30520,30521],{"class":64}," folder ",[54,30523,29745],{"class":81},[54,30525,830],{"class":544},[54,30527,6676],{"class":548},[54,30529,545],{"class":544},[54,30531,29754],{"class":81},[54,30533,29757],{"class":64},[54,30535,30536,30538,30540,30542],{"class":56,"line":887},[54,30537,116],{"class":115},[54,30539,96],{"class":88},[54,30541,23896],{"class":92},[54,30543,109],{"class":88},[14,30545,30546,30547,89],{},"This may work, but it is less safe and less readable than ",[17,30548,25760],{},[602,30550,30552,30553],{"id":30551},"mixing-manual-separators-with-ospathjoin","Mixing manual separators with ",[17,30554,25760],{},[14,30556,30557],{},"Avoid writing code like this:",[45,30559,30561],{"className":47,"code":30560,"language":49,"meta":50,"style":50},"import os\n\npath = os.path.join(\"data\u002F\", \"report.txt\")\nprint(path)\n",[17,30562,30563,30569,30573,30608],{"__ignoreMap":50},[54,30564,30565,30567],{"class":56,"line":57},[54,30566,61],{"class":60},[54,30568,23707],{"class":64},[54,30570,30571],{"class":56,"line":68},[54,30572,72],{"emptyLinePlaceholder":71},[54,30574,30575,30577,30579,30581,30583,30585,30587,30589,30591,30593,30596,30598,30600,30602,30604,30606],{"class":56,"line":75},[54,30576,27985],{"class":64},[54,30578,82],{"class":81},[54,30580,24000],{"class":64},[54,30582,89],{"class":88},[54,30584,23896],{"class":1693},[54,30586,89],{"class":88},[54,30588,25725],{"class":92},[54,30590,96],{"class":88},[54,30592,545],{"class":544},[54,30594,30595],{"class":548},"data\u002F",[54,30597,545],{"class":544},[54,30599,103],{"class":88},[54,30601,830],{"class":544},[54,30603,25705],{"class":548},[54,30605,545],{"class":544},[54,30607,109],{"class":88},[54,30609,30610,30612,30614,30616],{"class":56,"line":112},[54,30611,116],{"class":115},[54,30613,96],{"class":88},[54,30615,23896],{"class":92},[54,30617,109],{"class":88},[14,30619,30620],{},"It is better to pass clean path parts:",[45,30622,30624],{"className":47,"code":30623,"language":49,"meta":50,"style":50},"import os\n\npath = os.path.join(\"data\", \"report.txt\")\nprint(path)\n",[17,30625,30626,30632,30636,30670],{"__ignoreMap":50},[54,30627,30628,30630],{"class":56,"line":57},[54,30629,61],{"class":60},[54,30631,23707],{"class":64},[54,30633,30634],{"class":56,"line":68},[54,30635,72],{"emptyLinePlaceholder":71},[54,30637,30638,30640,30642,30644,30646,30648,30650,30652,30654,30656,30658,30660,30662,30664,30666,30668],{"class":56,"line":75},[54,30639,27985],{"class":64},[54,30641,82],{"class":81},[54,30643,24000],{"class":64},[54,30645,89],{"class":88},[54,30647,23896],{"class":1693},[54,30649,89],{"class":88},[54,30651,25725],{"class":92},[54,30653,96],{"class":88},[54,30655,545],{"class":544},[54,30657,8313],{"class":548},[54,30659,545],{"class":544},[54,30661,103],{"class":88},[54,30663,830],{"class":544},[54,30665,25705],{"class":548},[54,30667,545],{"class":544},[54,30669,109],{"class":88},[54,30671,30672,30674,30676,30678],{"class":56,"line":112},[54,30673,116],{"class":115},[54,30675,96],{"class":88},[54,30677,23896],{"class":92},[54,30679,109],{"class":88},[602,30681,30683],{"id":30682},"assuming-the-function-creates-folders","Assuming the function creates folders",[14,30685,30686,30688],{},[17,30687,25760],{}," does not create anything.",[14,30690,30691,30692,30695,30696,30699],{},"If you build a path like ",[17,30693,30694],{},"\"logs\u002Foutput.txt\"",", the ",[17,30697,30698],{},"logs"," folder will not be created automatically.",[602,30701,30703],{"id":30702},"passing-non-string-values","Passing non-string values",[14,30705,30706],{},"Path parts should usually be strings.",[14,30708,30709],{},"This can cause problems:",[45,30711,30713],{"className":47,"code":30712,"language":49,"meta":50,"style":50},"import os\n\nyear = 2024\npath = os.path.join(\"reports\", year)\nprint(path)\n",[17,30714,30715,30721,30725,30735,30766],{"__ignoreMap":50},[54,30716,30717,30719],{"class":56,"line":57},[54,30718,61],{"class":60},[54,30720,23707],{"class":64},[54,30722,30723],{"class":56,"line":68},[54,30724,72],{"emptyLinePlaceholder":71},[54,30726,30727,30730,30732],{"class":56,"line":75},[54,30728,30729],{"class":64},"year ",[54,30731,82],{"class":81},[54,30733,30734],{"class":99}," 2024\n",[54,30736,30737,30739,30741,30743,30745,30747,30749,30751,30753,30755,30757,30759,30761,30764],{"class":56,"line":112},[54,30738,27985],{"class":64},[54,30740,82],{"class":81},[54,30742,24000],{"class":64},[54,30744,89],{"class":88},[54,30746,23896],{"class":1693},[54,30748,89],{"class":88},[54,30750,25725],{"class":92},[54,30752,96],{"class":88},[54,30754,545],{"class":544},[54,30756,23859],{"class":548},[54,30758,545],{"class":544},[54,30760,103],{"class":88},[54,30762,30763],{"class":92}," year",[54,30765,109],{"class":88},[54,30767,30768,30770,30772,30774],{"class":56,"line":887},[54,30769,116],{"class":115},[54,30771,96],{"class":88},[54,30773,23896],{"class":92},[54,30775,109],{"class":88},[14,30777,126,30778,30780],{},[17,30779,3442],{}," when needed:",[45,30782,30784],{"className":47,"code":30783,"language":49,"meta":50,"style":50},"import os\n\nyear = 2024\npath = os.path.join(\"reports\", str(year), \"summary.txt\")\nprint(path)\n",[17,30785,30786,30792,30796,30804,30848],{"__ignoreMap":50},[54,30787,30788,30790],{"class":56,"line":57},[54,30789,61],{"class":60},[54,30791,23707],{"class":64},[54,30793,30794],{"class":56,"line":68},[54,30795,72],{"emptyLinePlaceholder":71},[54,30797,30798,30800,30802],{"class":56,"line":75},[54,30799,30729],{"class":64},[54,30801,82],{"class":81},[54,30803,30734],{"class":99},[54,30805,30806,30808,30810,30812,30814,30816,30818,30820,30822,30824,30826,30828,30830,30833,30835,30837,30839,30841,30844,30846],{"class":56,"line":112},[54,30807,27985],{"class":64},[54,30809,82],{"class":81},[54,30811,24000],{"class":64},[54,30813,89],{"class":88},[54,30815,23896],{"class":1693},[54,30817,89],{"class":88},[54,30819,25725],{"class":92},[54,30821,96],{"class":88},[54,30823,545],{"class":544},[54,30825,23859],{"class":548},[54,30827,545],{"class":544},[54,30829,103],{"class":88},[54,30831,30832],{"class":429}," str",[54,30834,96],{"class":88},[54,30836,1694],{"class":92},[54,30838,21773],{"class":88},[54,30840,830],{"class":544},[54,30842,30843],{"class":548},"summary.txt",[54,30845,545],{"class":544},[54,30847,109],{"class":88},[54,30849,30850,30852,30854,30856],{"class":56,"line":887},[54,30851,116],{"class":115},[54,30853,96],{"class":88},[54,30855,23896],{"class":92},[54,30857,109],{"class":88},[602,30859,30861],{"id":30860},"common-causes-of-confusion","Common causes of confusion",[149,30863,30864,30870,30873,30878],{},[152,30865,30866,30867],{},"Manually writing paths like ",[17,30868,30869],{},"folder + '\u002F' + file",[152,30871,30872],{},"Using Windows-style backslashes directly in strings",[152,30874,5949,30875,30877],{},[17,30876,25760],{}," to verify or create a path",[152,30879,30880],{},"Accidentally passing an absolute path as a later argument",[14,30882,30883,30884,89],{},"If you are checking whether a file is really there after building the path, see ",[192,30885,28378],{"href":25082},[40,30887,1419],{"id":1418},[602,30889,8346,30891,30893],{"id":30890},"does-ospathjoin-create-a-folder-or-file",[17,30892,25760],{}," create a folder or file?",[14,30895,30896],{},"No. It only returns a path string.",[602,30898,30900,30901,30903,30904,30906],{"id":30899},"why-should-i-use-ospathjoin-instead-of-writing-yourself","Why should I use ",[17,30902,25760],{}," instead of writing ",[17,30905,6676],{}," yourself?",[14,30908,30909],{},"It uses the correct separator for the current operating system and reduces path formatting mistakes.",[602,30911,8091,30913,30915],{"id":30912},"can-ospathjoin-join-more-than-two-parts",[17,30914,25760],{}," join more than two parts?",[14,30917,30918],{},"Yes. You can pass as many path parts as needed.",[602,30920,30922],{"id":30921},"what-if-one-part-is-an-absolute-path","What if one part is an absolute path?",[14,30924,30925],{},"A later absolute path can override earlier parts, so the final result may not include everything you passed in.",[40,30927,1510],{"id":1509},[149,30929,30930,30936,30942,30946,30950],{},[152,30931,30932],{},[192,30933,13760,30934,13763],{"href":27589},[17,30935,23720],{},[152,30937,30938],{},[192,30939,30940,12898],{"href":24853},[17,30941,24667],{},[152,30943,30944],{},[192,30945,25077],{"href":24363},[152,30947,30948],{},[192,30949,25083],{"href":25082},[152,30951,30952],{},[192,30953,13760,30954,12898],{"href":29867},[17,30955,8524],{},[1545,30957,30958],{},"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 .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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":30960},[30961,30962,30964,30967,30970,30973,30978,30979,30988,30997],{"id":3595,"depth":68,"text":3596},{"id":29613,"depth":68,"text":30963},"What os.path.join() does",{"id":198,"depth":68,"text":199,"children":30965},[30966],{"id":26385,"depth":75,"text":26386},{"id":492,"depth":68,"text":493,"children":30968},[30969],{"id":29849,"depth":75,"text":29850},{"id":29872,"depth":68,"text":29873,"children":30971},[30972],{"id":29980,"depth":75,"text":29981},{"id":30038,"depth":68,"text":30039,"children":30974},[30975,30976,30977],{"id":30045,"depth":75,"text":30046},{"id":30132,"depth":75,"text":30133},{"id":30206,"depth":75,"text":30207},{"id":30310,"depth":68,"text":30311},{"id":5209,"depth":68,"text":5210,"children":30980},[30981,30983,30985,30986,30987],{"id":30472,"depth":75,"text":30982},"Using + to join path pieces",{"id":30551,"depth":75,"text":30984},"Mixing manual separators with os.path.join()",{"id":30682,"depth":75,"text":30683},{"id":30702,"depth":75,"text":30703},{"id":30860,"depth":75,"text":30861},{"id":1418,"depth":68,"text":1419,"children":30989},[30990,30992,30994,30996],{"id":30890,"depth":75,"text":30991},"Does os.path.join() create a folder or file?",{"id":30899,"depth":75,"text":30993},"Why should I use os.path.join() instead of writing \u002F yourself?",{"id":30912,"depth":75,"text":30995},"Can os.path.join() join more than two parts?",{"id":30921,"depth":75,"text":30922},{"id":1509,"depth":68,"text":1510},"Master os.path.join function explained in our comprehensive Python beginner guide.",{},{"title":29460,"description":30998},"standard-library\u002Fos.path.join-function-explained","gwLGP1afufavVRfe40RlDOxEuHUEtg_TmfXDR-PdLQc",{"id":31004,"title":31005,"body":31006,"description":33495,"extension":1577,"meta":33496,"navigation":71,"path":1773,"seo":33497,"stem":33498,"__hash__":33499},"content\u002Fstandard-library\u002Fpython-datetime-module-overview.md","Python datetime Module Overview",{"type":7,"value":31007,"toc":33446},[31008,31011,31016,31019,31021,31173,31177,31208,31212,31293,31299,31303,31309,31326,31329,31346,31350,31355,31359,31365,31375,31377,31430,31432,31450,31455,31459,31463,31476,31478,31528,31530,31547,31552,31556,31561,31569,31571,31630,31632,31661,31666,31671,31676,31681,31684,31698,31700,31786,31791,31796,31801,31804,31807,31811,31814,31839,31842,32020,32024,32029,32035,32078,32084,32126,32131,32137,32142,32215,32217,32243,32249,32255,32261,32337,32339,32367,32372,32379,32465,32469,32472,32477,32480,32531,32534,32550,32556,32562,32594,32599,32603,32611,32614,32624,32638,32647,32650,32665,32669,32672,32692,32695,32697,32702,32712,32714,32742,32752,32755,32787,32789,32821,32827,32830,32890,32898,32900,32969,32975,32978,33016,33022,33091,33097,33102,33110,33113,33155,33160,33164,33167,33191,33200,33207,33209,33213,33221,33225,33230,33234,33243,33275,33279,33284,33340,33344,33349,33419,33421,33440,33443],[10,31009,31005],{"id":31010},"python-datetime-module-overview",[14,31012,28340,31013,31015],{},[17,31014,1628],{}," module helps you work with dates, times, and time differences in Python.",[14,31017,31018],{},"It is part of the Python standard library, so you do not need to install anything extra. Beginners often use it to get the current date, create a specific date, format a date as text, or add a number of days to a date.",[40,31020,3596],{"id":3595},[45,31022,31024],{"className":47,"code":31023,"language":49,"meta":50,"style":50},"from datetime import datetime, date, time, timedelta\n\nnow = datetime.now()\ntoday = date.today()\nmeeting = time(14, 30)\none_week = timedelta(days=7)\n\nprint(now)\nprint(today)\nprint(meeting)\nprint(now + one_week)\n",[17,31025,31026,31051,31055,31069,31085,31103,31124,31128,31138,31148,31158],{"__ignoreMap":50},[54,31027,31028,31030,31032,31034,31036,31038,31041,31043,31046,31048],{"class":56,"line":57},[54,31029,1640],{"class":60},[54,31031,1643],{"class":64},[54,31033,61],{"class":60},[54,31035,1662],{"class":64},[54,31037,103],{"class":88},[54,31039,31040],{"class":64}," date",[54,31042,103],{"class":88},[54,31044,31045],{"class":64}," time",[54,31047,103],{"class":88},[54,31049,31050],{"class":64}," timedelta\n",[54,31052,31053],{"class":56,"line":68},[54,31054,72],{"emptyLinePlaceholder":71},[54,31056,31057,31059,31061,31063,31065,31067],{"class":56,"line":75},[54,31058,1657],{"class":64},[54,31060,82],{"class":81},[54,31062,1662],{"class":64},[54,31064,89],{"class":88},[54,31066,1667],{"class":92},[54,31068,1670],{"class":88},[54,31070,31071,31074,31076,31078,31080,31083],{"class":56,"line":112},[54,31072,31073],{"class":64},"today ",[54,31075,82],{"class":81},[54,31077,31040],{"class":64},[54,31079,89],{"class":88},[54,31081,31082],{"class":92},"today",[54,31084,1670],{"class":88},[54,31086,31087,31089,31091,31093,31095,31097,31099,31101],{"class":56,"line":887},[54,31088,3954],{"class":64},[54,31090,82],{"class":81},[54,31092,31045],{"class":92},[54,31094,96],{"class":88},[54,31096,2812],{"class":99},[54,31098,103],{"class":88},[54,31100,3981],{"class":99},[54,31102,109],{"class":88},[54,31104,31105,31108,31110,31113,31115,31118,31120,31122],{"class":56,"line":892},[54,31106,31107],{"class":64},"one_week ",[54,31109,82],{"class":81},[54,31111,31112],{"class":92}," timedelta",[54,31114,96],{"class":88},[54,31116,31117],{"class":8940},"days",[54,31119,82],{"class":81},[54,31121,4088],{"class":99},[54,31123,109],{"class":88},[54,31125,31126],{"class":56,"line":913},[54,31127,72],{"emptyLinePlaceholder":71},[54,31129,31130,31132,31134,31136],{"class":56,"line":2121},[54,31131,116],{"class":115},[54,31133,96],{"class":88},[54,31135,1667],{"class":92},[54,31137,109],{"class":88},[54,31139,31140,31142,31144,31146],{"class":56,"line":2136},[54,31141,116],{"class":115},[54,31143,96],{"class":88},[54,31145,31082],{"class":92},[54,31147,109],{"class":88},[54,31149,31150,31152,31154,31156],{"class":56,"line":2151},[54,31151,116],{"class":115},[54,31153,96],{"class":88},[54,31155,4143],{"class":92},[54,31157,109],{"class":88},[54,31159,31160,31162,31164,31166,31168,31171],{"class":56,"line":4442},[54,31161,116],{"class":115},[54,31163,96],{"class":88},[54,31165,1657],{"class":92},[54,31167,29745],{"class":81},[54,31169,31170],{"class":92}," one_week",[54,31172,109],{"class":88},[14,31174,31175],{},[35,31176,22568],{},[149,31178,31179,31184,31190,31196,31202],{},[152,31180,31181,31183],{},[17,31182,1594],{}," gets the current date and time",[152,31185,31186,31189],{},[17,31187,31188],{},"date.today()"," gets today's date",[152,31191,31192,31195],{},[17,31193,31194],{},"time(14, 30)"," creates a time value for 2:30 PM",[152,31197,31198,31201],{},[17,31199,31200],{},"timedelta(days=7)"," represents 7 days",[152,31203,31204,31207],{},[17,31205,31206],{},"now + one_week"," gives the date and time one week from now",[14,31209,31210],{},[35,31211,561],{},[45,31213,31215],{"className":47,"code":31214,"language":49,"meta":50,"style":50},"2026-04-22 10:15:30.123456\n2026-04-22\n14:30:00\n2026-04-29 10:15:30.123456\n",[17,31216,31217,31242,31256,31268],{"__ignoreMap":50},[54,31218,31219,31221,31223,31225,31227,31229,31231,31233,31235,31237,31239],{"class":56,"line":57},[54,31220,2730],{"class":99},[54,31222,1467],{"class":81},[54,31224,752],{"class":99},[54,31226,2389],{"class":2388},[54,31228,1467],{"class":81},[54,31230,2394],{"class":99},[54,31232,106],{"class":99},[54,31234,147],{"class":88},[54,31236,6497],{"class":99},[54,31238,147],{"class":88},[54,31240,31241],{"class":99},"30.123456\n",[54,31243,31244,31246,31248,31250,31252,31254],{"class":56,"line":68},[54,31245,2730],{"class":99},[54,31247,1467],{"class":81},[54,31249,752],{"class":99},[54,31251,2389],{"class":2388},[54,31253,1467],{"class":81},[54,31255,2617],{"class":99},[54,31257,31258,31260,31262,31264,31266],{"class":56,"line":75},[54,31259,2812],{"class":99},[54,31261,147],{"class":88},[54,31263,3696],{"class":99},[54,31265,147],{"class":88},[54,31267,4105],{"class":99},[54,31269,31270,31272,31274,31276,31278,31280,31283,31285,31287,31289,31291],{"class":56,"line":112},[54,31271,2730],{"class":99},[54,31273,1467],{"class":81},[54,31275,752],{"class":99},[54,31277,2389],{"class":2388},[54,31279,1467],{"class":81},[54,31281,31282],{"class":99},"29",[54,31284,106],{"class":99},[54,31286,147],{"class":88},[54,31288,6497],{"class":99},[54,31290,147],{"class":88},[54,31292,31241],{"class":99},[14,31294,31295,31296,31298],{},"Use this as a quick example of the most common ",[17,31297,1628],{}," module objects.",[40,31300,31302],{"id":31301},"what-the-datetime-module-is-for","What the datetime module is for",[14,31304,31305,31306,31308],{},"You use the ",[17,31307,1628],{}," module when your program needs to work with:",[149,31310,31311,31314,31317,31320,31323],{},[152,31312,31313],{},"Dates",[152,31315,31316],{},"Times",[152,31318,31319],{},"Dates and times together",[152,31321,31322],{},"Time differences",[152,31324,31325],{},"Converting between date objects and strings",[14,31327,31328],{},"Common uses include:",[149,31330,31331,31334,31337,31340,31343],{},[152,31332,31333],{},"Getting the current date and time",[152,31335,31336],{},"Creating a birthday or deadline",[152,31338,31339],{},"Adding 7 days to a date",[152,31341,31342],{},"Showing a date in a readable format",[152,31344,31345],{},"Reading a date from user input",[40,31347,31349],{"id":31348},"main-objects-in-the-datetime-module","Main objects in the datetime module",[14,31351,28340,31352,31354],{},[17,31353,1628],{}," module contains several useful classes.",[602,31356,31357],{"id":2535},[17,31358,2535],{},[14,31360,31361,31362,31364],{},"A ",[17,31363,2535],{}," stores:",[149,31366,31367,31369,31372],{},[152,31368,2412],{},[152,31370,31371],{},"Month",[152,31373,31374],{},"Day",[14,31376,253],{},[45,31378,31380],{"className":47,"code":31379,"language":49,"meta":50,"style":50},"from datetime import date\n\nd = date(2026, 4, 22)\nprint(d)\n",[17,31381,31382,31393,31397,31419],{"__ignoreMap":50},[54,31383,31384,31386,31388,31390],{"class":56,"line":57},[54,31385,1640],{"class":60},[54,31387,1643],{"class":64},[54,31389,61],{"class":60},[54,31391,31392],{"class":64}," date\n",[54,31394,31395],{"class":56,"line":68},[54,31396,72],{"emptyLinePlaceholder":71},[54,31398,31399,31401,31403,31405,31407,31409,31411,31413,31415,31417],{"class":56,"line":75},[54,31400,7090],{"class":64},[54,31402,82],{"class":81},[54,31404,31040],{"class":92},[54,31406,96],{"class":88},[54,31408,2730],{"class":99},[54,31410,103],{"class":88},[54,31412,4264],{"class":99},[54,31414,103],{"class":88},[54,31416,4269],{"class":99},[54,31418,109],{"class":88},[54,31420,31421,31423,31425,31428],{"class":56,"line":112},[54,31422,116],{"class":115},[54,31424,96],{"class":88},[54,31426,31427],{"class":92},"d",[54,31429,109],{"class":88},[14,31431,449],{},[45,31433,31434],{"className":47,"code":2723,"language":49,"meta":50,"style":50},[17,31435,31436],{"__ignoreMap":50},[54,31437,31438,31440,31442,31444,31446,31448],{"class":56,"line":57},[54,31439,2730],{"class":99},[54,31441,1467],{"class":81},[54,31443,752],{"class":99},[54,31445,2389],{"class":2388},[54,31447,1467],{"class":81},[54,31449,2617],{"class":99},[14,31451,126,31452,31454],{},[17,31453,2535],{}," when you only care about the calendar date.",[602,31456,31457],{"id":2559},[17,31458,2559],{},[14,31460,31361,31461,31364],{},[17,31462,2559],{},[149,31464,31465,31467,31470,31473],{},[152,31466,2422],{},[152,31468,31469],{},"Minute",[152,31471,31472],{},"Second",[152,31474,31475],{},"Smaller units like microseconds if needed",[14,31477,253],{},[45,31479,31481],{"className":47,"code":31480,"language":49,"meta":50,"style":50},"from datetime import time\n\nt = time(14, 30)\nprint(t)\n",[17,31482,31483,31494,31498,31517],{"__ignoreMap":50},[54,31484,31485,31487,31489,31491],{"class":56,"line":57},[54,31486,1640],{"class":60},[54,31488,1643],{"class":64},[54,31490,61],{"class":60},[54,31492,31493],{"class":64}," time\n",[54,31495,31496],{"class":56,"line":68},[54,31497,72],{"emptyLinePlaceholder":71},[54,31499,31500,31503,31505,31507,31509,31511,31513,31515],{"class":56,"line":75},[54,31501,31502],{"class":64},"t ",[54,31504,82],{"class":81},[54,31506,31045],{"class":92},[54,31508,96],{"class":88},[54,31510,2812],{"class":99},[54,31512,103],{"class":88},[54,31514,3981],{"class":99},[54,31516,109],{"class":88},[54,31518,31519,31521,31523,31526],{"class":56,"line":112},[54,31520,116],{"class":115},[54,31522,96],{"class":88},[54,31524,31525],{"class":92},"t",[54,31527,109],{"class":88},[14,31529,449],{},[45,31531,31533],{"className":47,"code":31532,"language":49,"meta":50,"style":50},"14:30:00\n",[17,31534,31535],{"__ignoreMap":50},[54,31536,31537,31539,31541,31543,31545],{"class":56,"line":57},[54,31538,2812],{"class":99},[54,31540,147],{"class":88},[54,31542,3696],{"class":99},[54,31544,147],{"class":88},[54,31546,4105],{"class":99},[14,31548,126,31549,31551],{},[17,31550,2559],{}," when you only need a clock time.",[602,31553,31554],{"id":1628},[17,31555,1628],{},[14,31557,31361,31558,31560],{},[17,31559,1628],{}," stores both:",[149,31562,31563,31566],{},[152,31564,31565],{},"Date",[152,31567,31568],{},"Time",[14,31570,253],{},[45,31572,31574],{"className":47,"code":31573,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ndt = datetime(2026, 4, 22, 14, 30)\nprint(dt)\n",[17,31575,31576,31586,31590,31620],{"__ignoreMap":50},[54,31577,31578,31580,31582,31584],{"class":56,"line":57},[54,31579,1640],{"class":60},[54,31581,1643],{"class":64},[54,31583,61],{"class":60},[54,31585,1648],{"class":64},[54,31587,31588],{"class":56,"line":68},[54,31589,72],{"emptyLinePlaceholder":71},[54,31591,31592,31594,31596,31598,31600,31602,31604,31606,31608,31610,31612,31614,31616,31618],{"class":56,"line":75},[54,31593,5103],{"class":64},[54,31595,82],{"class":81},[54,31597,1662],{"class":92},[54,31599,96],{"class":88},[54,31601,2730],{"class":99},[54,31603,103],{"class":88},[54,31605,4264],{"class":99},[54,31607,103],{"class":88},[54,31609,4269],{"class":99},[54,31611,103],{"class":88},[54,31613,2397],{"class":99},[54,31615,103],{"class":88},[54,31617,3981],{"class":99},[54,31619,109],{"class":88},[54,31621,31622,31624,31626,31628],{"class":56,"line":112},[54,31623,116],{"class":115},[54,31625,96],{"class":88},[54,31627,6246],{"class":92},[54,31629,109],{"class":88},[14,31631,449],{},[45,31633,31635],{"className":47,"code":31634,"language":49,"meta":50,"style":50},"2026-04-22 14:30:00\n",[17,31636,31637],{"__ignoreMap":50},[54,31638,31639,31641,31643,31645,31647,31649,31651,31653,31655,31657,31659],{"class":56,"line":57},[54,31640,2730],{"class":99},[54,31642,1467],{"class":81},[54,31644,752],{"class":99},[54,31646,2389],{"class":2388},[54,31648,1467],{"class":81},[54,31650,2394],{"class":99},[54,31652,2397],{"class":99},[54,31654,147],{"class":88},[54,31656,3696],{"class":99},[54,31658,147],{"class":88},[54,31660,4105],{"class":99},[14,31662,126,31663,31665],{},[17,31664,1628],{}," when you need the full date and time together.",[14,31667,31668,31669,89],{},"If you want the current date and time, see ",[192,31670,5052],{"href":3535},[602,31672,31674],{"id":31673},"timedelta",[17,31675,31673],{},[14,31677,31361,31678,31680],{},[17,31679,31673],{}," stores a length of time.",[14,31682,31683],{},"You can use it to add or subtract:",[149,31685,31686,31689,31692,31695],{},[152,31687,31688],{},"Days",[152,31690,31691],{},"Hours",[152,31693,31694],{},"Minutes",[152,31696,31697],{},"Seconds",[14,31699,253],{},[45,31701,31703],{"className":47,"code":31702,"language":49,"meta":50,"style":50},"from datetime import datetime, timedelta\n\nnow = datetime.now()\nlater = now + timedelta(days=3)\n\nprint(now)\nprint(later)\n",[17,31704,31705,31719,31723,31737,31761,31765,31775],{"__ignoreMap":50},[54,31706,31707,31709,31711,31713,31715,31717],{"class":56,"line":57},[54,31708,1640],{"class":60},[54,31710,1643],{"class":64},[54,31712,61],{"class":60},[54,31714,1662],{"class":64},[54,31716,103],{"class":88},[54,31718,31050],{"class":64},[54,31720,31721],{"class":56,"line":68},[54,31722,72],{"emptyLinePlaceholder":71},[54,31724,31725,31727,31729,31731,31733,31735],{"class":56,"line":75},[54,31726,1657],{"class":64},[54,31728,82],{"class":81},[54,31730,1662],{"class":64},[54,31732,89],{"class":88},[54,31734,1667],{"class":92},[54,31736,1670],{"class":88},[54,31738,31739,31742,31744,31747,31749,31751,31753,31755,31757,31759],{"class":56,"line":112},[54,31740,31741],{"class":64},"later ",[54,31743,82],{"class":81},[54,31745,31746],{"class":64}," now ",[54,31748,29745],{"class":81},[54,31750,31112],{"class":92},[54,31752,96],{"class":88},[54,31754,31117],{"class":8940},[54,31756,82],{"class":81},[54,31758,401],{"class":99},[54,31760,109],{"class":88},[54,31762,31763],{"class":56,"line":887},[54,31764,72],{"emptyLinePlaceholder":71},[54,31766,31767,31769,31771,31773],{"class":56,"line":892},[54,31768,116],{"class":115},[54,31770,96],{"class":88},[54,31772,1667],{"class":92},[54,31774,109],{"class":88},[54,31776,31777,31779,31781,31784],{"class":56,"line":913},[54,31778,116],{"class":115},[54,31780,96],{"class":88},[54,31782,31783],{"class":92},"later",[54,31785,109],{"class":88},[14,31787,126,31788,31790],{},[17,31789,31673],{}," when you want to move forward or backward in time.",[602,31792,31794],{"id":31793},"timezone",[17,31795,31793],{},[14,31797,31361,31798,31800],{},[17,31799,31793],{}," object helps represent time zone information.",[14,31802,31803],{},"For beginners, it is enough to know that time zones exist and can matter when you work with users in different places.",[14,31805,31806],{},"This page does not go deep into time zone handling.",[40,31808,31810],{"id":31809},"when-to-use-each-object","When to use each object",[14,31812,31813],{},"Here is a simple guide:",[149,31815,31816,31821,31826,31831,31836],{},[152,31817,126,31818,31820],{},[17,31819,2535],{}," when you only need a calendar date",[152,31822,126,31823,31825],{},[17,31824,2559],{}," when you only need a clock time",[152,31827,126,31828,31830],{},[17,31829,1628],{}," when you need both date and time",[152,31832,126,31833,31835],{},[17,31834,31673],{}," to add or subtract days, hours, or seconds",[152,31837,31838],{},"Leave deeper time zone handling for dedicated pages",[14,31840,31841],{},"Examples:",[45,31843,31845],{"className":47,"code":31844,"language":49,"meta":50,"style":50},"from datetime import date, time, datetime, timedelta\n\nbirthday = date(2026, 9, 10)\nalarm_time = time(7, 0)\nappointment = datetime(2026, 9, 10, 15, 45)\nnext_week = appointment + timedelta(days=7)\n\nprint(birthday)\nprint(alarm_time)\nprint(appointment)\nprint(next_week)\n",[17,31846,31847,31869,31873,31896,31915,31948,31972,31976,31987,31998,32009],{"__ignoreMap":50},[54,31848,31849,31851,31853,31855,31857,31859,31861,31863,31865,31867],{"class":56,"line":57},[54,31850,1640],{"class":60},[54,31852,1643],{"class":64},[54,31854,61],{"class":60},[54,31856,31040],{"class":64},[54,31858,103],{"class":88},[54,31860,31045],{"class":64},[54,31862,103],{"class":88},[54,31864,1662],{"class":64},[54,31866,103],{"class":88},[54,31868,31050],{"class":64},[54,31870,31871],{"class":56,"line":68},[54,31872,72],{"emptyLinePlaceholder":71},[54,31874,31875,31878,31880,31882,31884,31886,31888,31890,31892,31894],{"class":56,"line":75},[54,31876,31877],{"class":64},"birthday ",[54,31879,82],{"class":81},[54,31881,31040],{"class":92},[54,31883,96],{"class":88},[54,31885,2730],{"class":99},[54,31887,103],{"class":88},[54,31889,3976],{"class":99},[54,31891,103],{"class":88},[54,31893,106],{"class":99},[54,31895,109],{"class":88},[54,31897,31898,31901,31903,31905,31907,31909,31911,31913],{"class":56,"line":112},[54,31899,31900],{"class":64},"alarm_time ",[54,31902,82],{"class":81},[54,31904,31045],{"class":92},[54,31906,96],{"class":88},[54,31908,4088],{"class":99},[54,31910,103],{"class":88},[54,31912,3986],{"class":99},[54,31914,109],{"class":88},[54,31916,31917,31920,31922,31924,31926,31928,31930,31932,31934,31936,31938,31941,31943,31946],{"class":56,"line":887},[54,31918,31919],{"class":64},"appointment ",[54,31921,82],{"class":81},[54,31923,1662],{"class":92},[54,31925,96],{"class":88},[54,31927,2730],{"class":99},[54,31929,103],{"class":88},[54,31931,3976],{"class":99},[54,31933,103],{"class":88},[54,31935,106],{"class":99},[54,31937,103],{"class":88},[54,31939,31940],{"class":99}," 15",[54,31942,103],{"class":88},[54,31944,31945],{"class":99}," 45",[54,31947,109],{"class":88},[54,31949,31950,31953,31955,31958,31960,31962,31964,31966,31968,31970],{"class":56,"line":892},[54,31951,31952],{"class":64},"next_week ",[54,31954,82],{"class":81},[54,31956,31957],{"class":64}," appointment ",[54,31959,29745],{"class":81},[54,31961,31112],{"class":92},[54,31963,96],{"class":88},[54,31965,31117],{"class":8940},[54,31967,82],{"class":81},[54,31969,4088],{"class":99},[54,31971,109],{"class":88},[54,31973,31974],{"class":56,"line":913},[54,31975,72],{"emptyLinePlaceholder":71},[54,31977,31978,31980,31982,31985],{"class":56,"line":2121},[54,31979,116],{"class":115},[54,31981,96],{"class":88},[54,31983,31984],{"class":92},"birthday",[54,31986,109],{"class":88},[54,31988,31989,31991,31993,31996],{"class":56,"line":2136},[54,31990,116],{"class":115},[54,31992,96],{"class":88},[54,31994,31995],{"class":92},"alarm_time",[54,31997,109],{"class":88},[54,31999,32000,32002,32004,32007],{"class":56,"line":2151},[54,32001,116],{"class":115},[54,32003,96],{"class":88},[54,32005,32006],{"class":92},"appointment",[54,32008,109],{"class":88},[54,32010,32011,32013,32015,32018],{"class":56,"line":4442},[54,32012,116],{"class":115},[54,32014,96],{"class":88},[54,32016,32017],{"class":92},"next_week",[54,32019,109],{"class":88},[40,32021,32023],{"id":32022},"common-beginner-tasks","Common beginner tasks",[14,32025,32026,32027,89],{},"These are some of the most common things beginners do with ",[17,32028,1628],{},[602,32030,32032,32033],{"id":32031},"get-the-current-date-with-datetoday","Get the current date with ",[17,32034,31188],{},[45,32036,32038],{"className":47,"code":32037,"language":49,"meta":50,"style":50},"from datetime import date\n\ntoday = date.today()\nprint(today)\n",[17,32039,32040,32050,32054,32068],{"__ignoreMap":50},[54,32041,32042,32044,32046,32048],{"class":56,"line":57},[54,32043,1640],{"class":60},[54,32045,1643],{"class":64},[54,32047,61],{"class":60},[54,32049,31392],{"class":64},[54,32051,32052],{"class":56,"line":68},[54,32053,72],{"emptyLinePlaceholder":71},[54,32055,32056,32058,32060,32062,32064,32066],{"class":56,"line":75},[54,32057,31073],{"class":64},[54,32059,82],{"class":81},[54,32061,31040],{"class":64},[54,32063,89],{"class":88},[54,32065,31082],{"class":92},[54,32067,1670],{"class":88},[54,32069,32070,32072,32074,32076],{"class":56,"line":112},[54,32071,116],{"class":115},[54,32073,96],{"class":88},[54,32075,31082],{"class":92},[54,32077,109],{"class":88},[602,32079,32081,32082],{"id":32080},"get-the-current-date-and-time-with-datetimenow","Get the current date and time with ",[17,32083,1594],{},[45,32085,32086],{"className":47,"code":1824,"language":49,"meta":50,"style":50},[17,32087,32088,32098,32102,32116],{"__ignoreMap":50},[54,32089,32090,32092,32094,32096],{"class":56,"line":57},[54,32091,1640],{"class":60},[54,32093,1643],{"class":64},[54,32095,61],{"class":60},[54,32097,1648],{"class":64},[54,32099,32100],{"class":56,"line":68},[54,32101,72],{"emptyLinePlaceholder":71},[54,32103,32104,32106,32108,32110,32112,32114],{"class":56,"line":75},[54,32105,1657],{"class":64},[54,32107,82],{"class":81},[54,32109,1662],{"class":64},[54,32111,89],{"class":88},[54,32113,1667],{"class":92},[54,32115,1670],{"class":88},[54,32117,32118,32120,32122,32124],{"class":56,"line":112},[54,32119,116],{"class":115},[54,32121,96],{"class":88},[54,32123,1667],{"class":92},[54,32125,109],{"class":88},[14,32127,32128,32129,89],{},"For a focused explanation, see ",[192,32130,5052],{"href":3535},[602,32132,32134,32135],{"id":32133},"format-a-datetime-as-text-with-strftime","Format a datetime as text with ",[17,32136,2654],{},[14,32138,32139,32141],{},[17,32140,2654],{}," turns a date or datetime object into a string.",[45,32143,32145],{"className":47,"code":32144,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\nformatted = now.strftime(\"%Y-%m-%d %H:%M\")\n\nprint(formatted)\n",[17,32146,32147,32157,32161,32175,32201,32205],{"__ignoreMap":50},[54,32148,32149,32151,32153,32155],{"class":56,"line":57},[54,32150,1640],{"class":60},[54,32152,1643],{"class":64},[54,32154,61],{"class":60},[54,32156,1648],{"class":64},[54,32158,32159],{"class":56,"line":68},[54,32160,72],{"emptyLinePlaceholder":71},[54,32162,32163,32165,32167,32169,32171,32173],{"class":56,"line":75},[54,32164,1657],{"class":64},[54,32166,82],{"class":81},[54,32168,1662],{"class":64},[54,32170,89],{"class":88},[54,32172,1667],{"class":92},[54,32174,1670],{"class":88},[54,32176,32177,32179,32181,32183,32185,32187,32189,32191,32193,32195,32197,32199],{"class":56,"line":112},[54,32178,3634],{"class":64},[54,32180,82],{"class":81},[54,32182,2316],{"class":64},[54,32184,89],{"class":88},[54,32186,2704],{"class":92},[54,32188,96],{"class":88},[54,32190,545],{"class":544},[54,32192,2711],{"class":548},[54,32194,2714],{"class":99},[54,32196,5154],{"class":548},[54,32198,545],{"class":544},[54,32200,109],{"class":88},[54,32202,32203],{"class":56,"line":887},[54,32204,72],{"emptyLinePlaceholder":71},[54,32206,32207,32209,32211,32213],{"class":56,"line":892},[54,32208,116],{"class":115},[54,32210,96],{"class":88},[54,32212,3665],{"class":92},[54,32214,109],{"class":88},[14,32216,305],{},[45,32218,32220],{"className":47,"code":32219,"language":49,"meta":50,"style":50},"2026-04-22 10:15\n",[17,32221,32222],{"__ignoreMap":50},[54,32223,32224,32226,32228,32230,32232,32234,32236,32238,32240],{"class":56,"line":57},[54,32225,2730],{"class":99},[54,32227,1467],{"class":81},[54,32229,752],{"class":99},[54,32231,2389],{"class":2388},[54,32233,1467],{"class":81},[54,32235,2394],{"class":99},[54,32237,106],{"class":99},[54,32239,147],{"class":88},[54,32241,32242],{"class":99},"15\n",[14,32244,32245,32246,89],{},"If you want to learn format codes step by step, see ",[192,32247,32248],{"href":2830},"datetime.strftime() explained",[602,32250,32252,32253],{"id":32251},"parse-text-into-a-datetime-with-strptime","Parse text into a datetime with ",[17,32254,5074],{},[14,32256,32257,5075,32259,1732],{},[17,32258,5074],{},[17,32260,1628],{},[45,32262,32264],{"className":47,"code":32263,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"2026-04-22 14:30\"\ndt = datetime.strptime(text, \"%Y-%m-%d %H:%M\")\n\nprint(dt)\n",[17,32265,32266,32276,32280,32293,32323,32327],{"__ignoreMap":50},[54,32267,32268,32270,32272,32274],{"class":56,"line":57},[54,32269,1640],{"class":60},[54,32271,1643],{"class":64},[54,32273,61],{"class":60},[54,32275,1648],{"class":64},[54,32277,32278],{"class":56,"line":68},[54,32279,72],{"emptyLinePlaceholder":71},[54,32281,32282,32284,32286,32288,32291],{"class":56,"line":75},[54,32283,5134],{"class":64},[54,32285,82],{"class":81},[54,32287,830],{"class":544},[54,32289,32290],{"class":548},"2026-04-22 14:30",[54,32292,5239],{"class":544},[54,32294,32295,32297,32299,32301,32303,32305,32307,32309,32311,32313,32315,32317,32319,32321],{"class":56,"line":112},[54,32296,5103],{"class":64},[54,32298,82],{"class":81},[54,32300,1662],{"class":64},[54,32302,89],{"class":88},[54,32304,6215],{"class":92},[54,32306,96],{"class":88},[54,32308,5171],{"class":92},[54,32310,103],{"class":88},[54,32312,830],{"class":544},[54,32314,2711],{"class":548},[54,32316,2714],{"class":99},[54,32318,5154],{"class":548},[54,32320,545],{"class":544},[54,32322,109],{"class":88},[54,32324,32325],{"class":56,"line":887},[54,32326,72],{"emptyLinePlaceholder":71},[54,32328,32329,32331,32333,32335],{"class":56,"line":892},[54,32330,116],{"class":115},[54,32332,96],{"class":88},[54,32334,6246],{"class":92},[54,32336,109],{"class":88},[14,32338,449],{},[45,32340,32341],{"className":47,"code":31634,"language":49,"meta":50,"style":50},[17,32342,32343],{"__ignoreMap":50},[54,32344,32345,32347,32349,32351,32353,32355,32357,32359,32361,32363,32365],{"class":56,"line":57},[54,32346,2730],{"class":99},[54,32348,1467],{"class":81},[54,32350,752],{"class":99},[54,32352,2389],{"class":2388},[54,32354,1467],{"class":81},[54,32356,2394],{"class":99},[54,32358,2397],{"class":99},[54,32360,147],{"class":88},[54,32362,3696],{"class":99},[54,32364,147],{"class":88},[54,32366,4105],{"class":99},[14,32368,32369,32370,89],{},"For more examples, see ",[192,32371,5206],{"href":2838},[602,32373,32375,32376],{"id":32374},"add-days-with-timedeltadays","Add days with ",[17,32377,32378],{},"timedelta(days=...)",[45,32380,32382],{"className":47,"code":32381,"language":49,"meta":50,"style":50},"from datetime import date, timedelta\n\ntoday = date.today()\nfuture_date = today + timedelta(days=10)\n\nprint(today)\nprint(future_date)\n",[17,32383,32384,32398,32402,32416,32440,32444,32454],{"__ignoreMap":50},[54,32385,32386,32388,32390,32392,32394,32396],{"class":56,"line":57},[54,32387,1640],{"class":60},[54,32389,1643],{"class":64},[54,32391,61],{"class":60},[54,32393,31040],{"class":64},[54,32395,103],{"class":88},[54,32397,31050],{"class":64},[54,32399,32400],{"class":56,"line":68},[54,32401,72],{"emptyLinePlaceholder":71},[54,32403,32404,32406,32408,32410,32412,32414],{"class":56,"line":75},[54,32405,31073],{"class":64},[54,32407,82],{"class":81},[54,32409,31040],{"class":64},[54,32411,89],{"class":88},[54,32413,31082],{"class":92},[54,32415,1670],{"class":88},[54,32417,32418,32421,32423,32426,32428,32430,32432,32434,32436,32438],{"class":56,"line":112},[54,32419,32420],{"class":64},"future_date ",[54,32422,82],{"class":81},[54,32424,32425],{"class":64}," today ",[54,32427,29745],{"class":81},[54,32429,31112],{"class":92},[54,32431,96],{"class":88},[54,32433,31117],{"class":8940},[54,32435,82],{"class":81},[54,32437,1131],{"class":99},[54,32439,109],{"class":88},[54,32441,32442],{"class":56,"line":887},[54,32443,72],{"emptyLinePlaceholder":71},[54,32445,32446,32448,32450,32452],{"class":56,"line":892},[54,32447,116],{"class":115},[54,32449,96],{"class":88},[54,32451,31082],{"class":92},[54,32453,109],{"class":88},[54,32455,32456,32458,32460,32463],{"class":56,"line":913},[54,32457,116],{"class":115},[54,32459,96],{"class":88},[54,32461,32462],{"class":92},"future_date",[54,32464,109],{"class":88},[40,32466,32468],{"id":32467},"how-importing-works","How importing works",[14,32470,32471],{},"This is one of the biggest beginner confusion points.",[602,32473,32475],{"id":32474},"import-datetime",[17,32476,3356],{},[14,32478,32479],{},"This imports the whole module.",[45,32481,32483],{"className":47,"code":32482,"language":49,"meta":50,"style":50},"import datetime\n\nprint(datetime.date.today())\nprint(datetime.datetime.now())\n",[17,32484,32485,32491,32495,32513],{"__ignoreMap":50},[54,32486,32487,32489],{"class":56,"line":57},[54,32488,61],{"class":60},[54,32490,1648],{"class":64},[54,32492,32493],{"class":56,"line":68},[54,32494,72],{"emptyLinePlaceholder":71},[54,32496,32497,32499,32501,32503,32505,32507,32509,32511],{"class":56,"line":75},[54,32498,116],{"class":115},[54,32500,96],{"class":88},[54,32502,1628],{"class":92},[54,32504,89],{"class":88},[54,32506,2535],{"class":1693},[54,32508,89],{"class":88},[54,32510,31082],{"class":92},[54,32512,1950],{"class":88},[54,32514,32515,32517,32519,32521,32523,32525,32527,32529],{"class":56,"line":112},[54,32516,116],{"class":115},[54,32518,96],{"class":88},[54,32520,1628],{"class":92},[54,32522,89],{"class":88},[54,32524,1628],{"class":1693},[54,32526,89],{"class":88},[54,32528,1667],{"class":92},[54,32530,1950],{"class":88},[14,32532,32533],{},"Here, you must use the module name first:",[149,32535,32536,32541,32545],{},[152,32537,32538],{},[17,32539,32540],{},"datetime.date",[152,32542,32543],{},[17,32544,2237],{},[152,32546,32547],{},[17,32548,32549],{},"datetime.timedelta",[602,32551,32553],{"id":32552},"from-datetime-import-datetime",[17,32554,32555],{},"from datetime import datetime",[14,32557,32558,32559,32561],{},"This imports only the ",[17,32560,1628],{}," class.",[45,32563,32564],{"className":47,"code":2960,"language":49,"meta":50,"style":50},[17,32565,32566,32576,32580],{"__ignoreMap":50},[54,32567,32568,32570,32572,32574],{"class":56,"line":57},[54,32569,1640],{"class":60},[54,32571,1643],{"class":64},[54,32573,61],{"class":60},[54,32575,1648],{"class":64},[54,32577,32578],{"class":56,"line":68},[54,32579,72],{"emptyLinePlaceholder":71},[54,32581,32582,32584,32586,32588,32590,32592],{"class":56,"line":75},[54,32583,116],{"class":115},[54,32585,96],{"class":88},[54,32587,1628],{"class":92},[54,32589,89],{"class":88},[54,32591,1667],{"class":92},[54,32593,1950],{"class":88},[14,32595,14544,32596,32598],{},[17,32597,1628],{}," refers to the class, not the module.",[602,32600,32602],{"id":32601},"why-this-confuses-beginners","Why this confuses beginners",[14,32604,32605,32606,32608,32609,89],{},"The module is named ",[17,32607,1628],{},", and one class inside it is also named ",[17,32610,1628],{},[14,32612,32613],{},"That means these two lines do different things:",[45,32615,32616],{"className":47,"code":3247,"language":49,"meta":50,"style":50},[17,32617,32618],{"__ignoreMap":50},[54,32619,32620,32622],{"class":56,"line":57},[54,32621,61],{"class":60},[54,32623,1648],{"class":64},[45,32625,32626],{"className":47,"code":1787,"language":49,"meta":50,"style":50},[17,32627,32628],{"__ignoreMap":50},[54,32629,32630,32632,32634,32636],{"class":56,"line":57},[54,32631,1640],{"class":60},[54,32633,1643],{"class":64},[54,32635,61],{"class":60},[54,32637,1648],{"class":64},[14,32639,32640,32641,32643,32644,89],{},"If you mix them up, you may get an error such as an ",[17,32642,5277],{},". If that happens, see ",[192,32645,32646],{"href":2002},"how to fix \"module has no attribute\"",[14,32648,32649],{},"A quick way to remember it:",[149,32651,32652,32659],{},[152,32653,32654,32656,32657],{},[17,32655,3356],{}," → use ",[17,32658,3363],{},[152,32660,32661,32656,32663],{},[17,32662,32555],{},[17,32664,1594],{},[40,32666,32668],{"id":32667},"what-this-page-does-not-cover-in-depth","What this page does not cover in depth",[14,32670,32671],{},"This overview does not go deep into:",[149,32673,32674,32677,32680,32683],{},[152,32675,32676],{},"Detailed formatting codes",[152,32678,32679],{},"Advanced time zone handling",[152,32681,32682],{},"Date arithmetic edge cases",[152,32684,32685,32686,5507,32689],{},"Third-party libraries like ",[17,32687,32688],{},"pandas",[17,32690,32691],{},"dateutil",[14,32693,32694],{},"This page is meant to help you understand the main pieces of the module first.",[40,32696,978],{"id":977},[14,32698,32699,32700,89],{},"These are some common beginner problems when using ",[17,32701,1628],{},[602,32703,32705,32706,32708,32709,32711],{"id":32704},"confusing-the-datetime-module-with-the-datetime-class","Confusing the ",[17,32707,1628],{}," module with the ",[17,32710,1628],{}," class",[14,32713,3213],{},[45,32715,32716],{"className":47,"code":1923,"language":49,"meta":50,"style":50},[17,32717,32718,32724,32728],{"__ignoreMap":50},[54,32719,32720,32722],{"class":56,"line":57},[54,32721,61],{"class":60},[54,32723,1648],{"class":64},[54,32725,32726],{"class":56,"line":68},[54,32727,72],{"emptyLinePlaceholder":71},[54,32729,32730,32732,32734,32736,32738,32740],{"class":56,"line":75},[54,32731,116],{"class":115},[54,32733,96],{"class":88},[54,32735,1628],{"class":92},[54,32737,89],{"class":88},[54,32739,1667],{"class":92},[54,32741,1950],{"class":88},[14,32743,32744,32745,32748,32749,32751],{},"This causes a problem because ",[17,32746,32747],{},"now()"," belongs to the ",[17,32750,1628],{}," class, not directly to the module.",[14,32753,32754],{},"Correct versions:",[45,32756,32757],{"className":47,"code":1966,"language":49,"meta":50,"style":50},[17,32758,32759,32765,32769],{"__ignoreMap":50},[54,32760,32761,32763],{"class":56,"line":57},[54,32762,61],{"class":60},[54,32764,1648],{"class":64},[54,32766,32767],{"class":56,"line":68},[54,32768,72],{"emptyLinePlaceholder":71},[54,32770,32771,32773,32775,32777,32779,32781,32783,32785],{"class":56,"line":75},[54,32772,116],{"class":115},[54,32774,96],{"class":88},[54,32776,1628],{"class":92},[54,32778,89],{"class":88},[54,32780,1628],{"class":1693},[54,32782,89],{"class":88},[54,32784,1667],{"class":92},[54,32786,1950],{"class":88},[14,32788,28223],{},[45,32790,32791],{"className":47,"code":2960,"language":49,"meta":50,"style":50},[17,32792,32793,32803,32807],{"__ignoreMap":50},[54,32794,32795,32797,32799,32801],{"class":56,"line":57},[54,32796,1640],{"class":60},[54,32798,1643],{"class":64},[54,32800,61],{"class":60},[54,32802,1648],{"class":64},[54,32804,32805],{"class":56,"line":68},[54,32806,72],{"emptyLinePlaceholder":71},[54,32808,32809,32811,32813,32815,32817,32819],{"class":56,"line":75},[54,32810,116],{"class":115},[54,32812,96],{"class":88},[54,32814,1628],{"class":92},[54,32816,89],{"class":88},[54,32818,1667],{"class":92},[54,32820,1950],{"class":88},[602,32822,5481,32824,32826],{"id":32823},"using-strptime-with-the-wrong-format-string",[17,32825,5074],{}," with the wrong format string",[14,32828,32829],{},"If the text and the format do not match, Python raises an error.",[45,32831,32833],{"className":47,"code":32832,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"22\u002F04\u002F2026\"\ndt = datetime.strptime(text, \"%Y-%m-%d\")\n",[17,32834,32835,32845,32849,32862],{"__ignoreMap":50},[54,32836,32837,32839,32841,32843],{"class":56,"line":57},[54,32838,1640],{"class":60},[54,32840,1643],{"class":64},[54,32842,61],{"class":60},[54,32844,1648],{"class":64},[54,32846,32847],{"class":56,"line":68},[54,32848,72],{"emptyLinePlaceholder":71},[54,32850,32851,32853,32855,32857,32860],{"class":56,"line":75},[54,32852,5134],{"class":64},[54,32854,82],{"class":81},[54,32856,830],{"class":544},[54,32858,32859],{"class":548},"22\u002F04\u002F2026",[54,32861,5239],{"class":544},[54,32863,32864,32866,32868,32870,32872,32874,32876,32878,32880,32882,32884,32886,32888],{"class":56,"line":112},[54,32865,5103],{"class":64},[54,32867,82],{"class":81},[54,32869,1662],{"class":64},[54,32871,89],{"class":88},[54,32873,6215],{"class":92},[54,32875,96],{"class":88},[54,32877,5171],{"class":92},[54,32879,103],{"class":88},[54,32881,830],{"class":544},[54,32883,2711],{"class":548},[54,32885,2714],{"class":99},[54,32887,545],{"class":544},[54,32889,109],{"class":88},[14,32891,7354,32892,32894,32895,89],{},[17,32893,6676],{}," and day-first order, but the format expects ",[17,32896,32897],{},"year-month-day",[14,32899,1310],{},[45,32901,32903],{"className":47,"code":32902,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"22\u002F04\u002F2026\"\ndt = datetime.strptime(text, \"%d\u002F%m\u002F%Y\")\nprint(dt)\n",[17,32904,32905,32915,32919,32931,32959],{"__ignoreMap":50},[54,32906,32907,32909,32911,32913],{"class":56,"line":57},[54,32908,1640],{"class":60},[54,32910,1643],{"class":64},[54,32912,61],{"class":60},[54,32914,1648],{"class":64},[54,32916,32917],{"class":56,"line":68},[54,32918,72],{"emptyLinePlaceholder":71},[54,32920,32921,32923,32925,32927,32929],{"class":56,"line":75},[54,32922,5134],{"class":64},[54,32924,82],{"class":81},[54,32926,830],{"class":544},[54,32928,32859],{"class":548},[54,32930,5239],{"class":544},[54,32932,32933,32935,32937,32939,32941,32943,32945,32947,32949,32951,32953,32955,32957],{"class":56,"line":112},[54,32934,5103],{"class":64},[54,32936,82],{"class":81},[54,32938,1662],{"class":64},[54,32940,89],{"class":88},[54,32942,6215],{"class":92},[54,32944,96],{"class":88},[54,32946,5171],{"class":92},[54,32948,103],{"class":88},[54,32950,830],{"class":544},[54,32952,2714],{"class":99},[54,32954,7003],{"class":548},[54,32956,545],{"class":544},[54,32958,109],{"class":88},[54,32960,32961,32963,32965,32967],{"class":56,"line":887},[54,32962,116],{"class":115},[54,32964,96],{"class":88},[54,32966,6246],{"class":92},[54,32968,109],{"class":88},[602,32970,32972,32973,1629],{"id":32971},"trying-to-add-a-string-directly-to-a-datetime-object","Trying to add a string directly to a ",[17,32974,1628],{},[14,32976,32977],{},"This does not work:",[45,32979,32981],{"className":47,"code":32980,"language":49,"meta":50,"style":50},"from datetime import datetime\n\nnow = datetime.now()\n# print(now + \"7 days\")\n",[17,32982,32983,32993,32997,33011],{"__ignoreMap":50},[54,32984,32985,32987,32989,32991],{"class":56,"line":57},[54,32986,1640],{"class":60},[54,32988,1643],{"class":64},[54,32990,61],{"class":60},[54,32992,1648],{"class":64},[54,32994,32995],{"class":56,"line":68},[54,32996,72],{"emptyLinePlaceholder":71},[54,32998,32999,33001,33003,33005,33007,33009],{"class":56,"line":75},[54,33000,1657],{"class":64},[54,33002,82],{"class":81},[54,33004,1662],{"class":64},[54,33006,89],{"class":88},[54,33008,1667],{"class":92},[54,33010,1670],{"class":88},[54,33012,33013],{"class":56,"line":112},[54,33014,33015],{"class":4313},"# print(now + \"7 days\")\n",[14,33017,33018,33019,33021],{},"You must use ",[17,33020,31673],{}," instead:",[45,33023,33025],{"className":47,"code":33024,"language":49,"meta":50,"style":50},"from datetime import datetime, timedelta\n\nnow = datetime.now()\nresult = now + timedelta(days=7)\nprint(result)\n",[17,33026,33027,33041,33045,33059,33081],{"__ignoreMap":50},[54,33028,33029,33031,33033,33035,33037,33039],{"class":56,"line":57},[54,33030,1640],{"class":60},[54,33032,1643],{"class":64},[54,33034,61],{"class":60},[54,33036,1662],{"class":64},[54,33038,103],{"class":88},[54,33040,31050],{"class":64},[54,33042,33043],{"class":56,"line":68},[54,33044,72],{"emptyLinePlaceholder":71},[54,33046,33047,33049,33051,33053,33055,33057],{"class":56,"line":75},[54,33048,1657],{"class":64},[54,33050,82],{"class":81},[54,33052,1662],{"class":64},[54,33054,89],{"class":88},[54,33056,1667],{"class":92},[54,33058,1670],{"class":88},[54,33060,33061,33063,33065,33067,33069,33071,33073,33075,33077,33079],{"class":56,"line":112},[54,33062,3833],{"class":64},[54,33064,82],{"class":81},[54,33066,31746],{"class":64},[54,33068,29745],{"class":81},[54,33070,31112],{"class":92},[54,33072,96],{"class":88},[54,33074,31117],{"class":8940},[54,33076,82],{"class":81},[54,33078,4088],{"class":99},[54,33080,109],{"class":88},[54,33082,33083,33085,33087,33089],{"class":56,"line":887},[54,33084,116],{"class":115},[54,33086,96],{"class":88},[54,33088,3866],{"class":92},[54,33090,109],{"class":88},[602,33092,5949,33094,33096],{"id":33093},"expecting-date-objects-to-have-time-values",[17,33095,2535],{}," objects to have time values",[14,33098,31361,33099,33101],{},[17,33100,2535],{}," only stores:",[149,33103,33104,33106,33108],{},[152,33105,2412],{},[152,33107,31371],{},[152,33109,31374],{},[14,33111,33112],{},"It does not store hours or minutes.",[45,33114,33115],{"className":47,"code":32037,"language":49,"meta":50,"style":50},[17,33116,33117,33127,33131,33145],{"__ignoreMap":50},[54,33118,33119,33121,33123,33125],{"class":56,"line":57},[54,33120,1640],{"class":60},[54,33122,1643],{"class":64},[54,33124,61],{"class":60},[54,33126,31392],{"class":64},[54,33128,33129],{"class":56,"line":68},[54,33130,72],{"emptyLinePlaceholder":71},[54,33132,33133,33135,33137,33139,33141,33143],{"class":56,"line":75},[54,33134,31073],{"class":64},[54,33136,82],{"class":81},[54,33138,31040],{"class":64},[54,33140,89],{"class":88},[54,33142,31082],{"class":92},[54,33144,1670],{"class":88},[54,33146,33147,33149,33151,33153],{"class":56,"line":112},[54,33148,116],{"class":115},[54,33150,96],{"class":88},[54,33152,31082],{"class":92},[54,33154,109],{"class":88},[14,33156,33157,33158,89],{},"If you need the time too, use ",[17,33159,1628],{},[602,33161,33163],{"id":33162},"forgetting-that-months-and-days-must-be-valid-numbers","Forgetting that months and days must be valid numbers",[14,33165,33166],{},"This will fail:",[45,33168,33170],{"className":47,"code":33169,"language":49,"meta":50,"style":50},"from datetime import date\n\n# bad_date = date(2026, 13, 40)\n",[17,33171,33172,33182,33186],{"__ignoreMap":50},[54,33173,33174,33176,33178,33180],{"class":56,"line":57},[54,33175,1640],{"class":60},[54,33177,1643],{"class":64},[54,33179,61],{"class":60},[54,33181,31392],{"class":64},[54,33183,33184],{"class":56,"line":68},[54,33185,72],{"emptyLinePlaceholder":71},[54,33187,33188],{"class":56,"line":75},[54,33189,33190],{"class":4313},"# bad_date = date(2026, 13, 40)\n",[14,33192,33193,33194,248,33196,33199],{},"Month must be from ",[17,33195,100],{},[17,33197,33198],{},"12",", and day must be valid for that month.",[14,33201,33202,33203,89],{},"If you see value conversion problems in other code, you may also need to check your input first. A related beginner error is ",[192,33204,33206],{"href":33205},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix","invalid literal for int() with base 10",[40,33208,1419],{"id":1418},[602,33210,33212],{"id":33211},"what-is-the-difference-between-date-and-datetime-in-python","What is the difference between date and datetime in Python?",[14,33214,33215,33217,33218,33220],{},[17,33216,2535],{}," stores only year, month, and day. ",[17,33219,1628],{}," stores both the date and the time.",[602,33222,33224],{"id":33223},"why-does-python-have-both-datetime-the-module-and-datetime-the-class","Why does Python have both datetime the module and datetime the class?",[14,33226,33227,33228,89],{},"The module contains several classes. One of those classes is also named ",[17,33229,1628],{},[602,33231,33233],{"id":33232},"how-do-i-get-the-current-date-and-time","How do I get the current date and time?",[14,33235,126,33236,22350,33238,33240,33241,15765],{},[17,33237,1594],{},[17,33239,1628],{}," class from the ",[17,33242,1628],{},[45,33244,33245],{"className":47,"code":2960,"language":49,"meta":50,"style":50},[17,33246,33247,33257,33261],{"__ignoreMap":50},[54,33248,33249,33251,33253,33255],{"class":56,"line":57},[54,33250,1640],{"class":60},[54,33252,1643],{"class":64},[54,33254,61],{"class":60},[54,33256,1648],{"class":64},[54,33258,33259],{"class":56,"line":68},[54,33260,72],{"emptyLinePlaceholder":71},[54,33262,33263,33265,33267,33269,33271,33273],{"class":56,"line":75},[54,33264,116],{"class":115},[54,33266,96],{"class":88},[54,33268,1628],{"class":92},[54,33270,89],{"class":88},[54,33272,1667],{"class":92},[54,33274,1950],{"class":88},[602,33276,33278],{"id":33277},"how-do-i-turn-a-datetime-into-a-string","How do I turn a datetime into a string?",[14,33280,126,33281,33283],{},[17,33282,2654],{}," to format it as text.",[45,33285,33286],{"className":47,"code":2661,"language":49,"meta":50,"style":50},[17,33287,33288,33298,33302,33316],{"__ignoreMap":50},[54,33289,33290,33292,33294,33296],{"class":56,"line":57},[54,33291,1640],{"class":60},[54,33293,1643],{"class":64},[54,33295,61],{"class":60},[54,33297,1648],{"class":64},[54,33299,33300],{"class":56,"line":68},[54,33301,72],{"emptyLinePlaceholder":71},[54,33303,33304,33306,33308,33310,33312,33314],{"class":56,"line":75},[54,33305,1657],{"class":64},[54,33307,82],{"class":81},[54,33309,1662],{"class":64},[54,33311,89],{"class":88},[54,33313,1667],{"class":92},[54,33315,1670],{"class":88},[54,33317,33318,33320,33322,33324,33326,33328,33330,33332,33334,33336,33338],{"class":56,"line":112},[54,33319,116],{"class":115},[54,33321,96],{"class":88},[54,33323,1667],{"class":92},[54,33325,89],{"class":88},[54,33327,2704],{"class":92},[54,33329,96],{"class":88},[54,33331,545],{"class":544},[54,33333,2711],{"class":548},[54,33335,2714],{"class":99},[54,33337,545],{"class":544},[54,33339,382],{"class":88},[602,33341,33343],{"id":33342},"how-do-i-turn-a-string-into-a-datetime","How do I turn a string into a datetime?",[14,33345,126,33346,33348],{},[17,33347,5074],{}," with a matching format pattern.",[45,33350,33352],{"className":47,"code":33351,"language":49,"meta":50,"style":50},"from datetime import datetime\n\ntext = \"2026-04-22\"\ndt = datetime.strptime(text, \"%Y-%m-%d\")\nprint(dt)\n",[17,33353,33354,33364,33368,33381,33409],{"__ignoreMap":50},[54,33355,33356,33358,33360,33362],{"class":56,"line":57},[54,33357,1640],{"class":60},[54,33359,1643],{"class":64},[54,33361,61],{"class":60},[54,33363,1648],{"class":64},[54,33365,33366],{"class":56,"line":68},[54,33367,72],{"emptyLinePlaceholder":71},[54,33369,33370,33372,33374,33376,33379],{"class":56,"line":75},[54,33371,5134],{"class":64},[54,33373,82],{"class":81},[54,33375,830],{"class":544},[54,33377,33378],{"class":548},"2026-04-22",[54,33380,5239],{"class":544},[54,33382,33383,33385,33387,33389,33391,33393,33395,33397,33399,33401,33403,33405,33407],{"class":56,"line":112},[54,33384,5103],{"class":64},[54,33386,82],{"class":81},[54,33388,1662],{"class":64},[54,33390,89],{"class":88},[54,33392,6215],{"class":92},[54,33394,96],{"class":88},[54,33396,5171],{"class":92},[54,33398,103],{"class":88},[54,33400,830],{"class":544},[54,33402,2711],{"class":548},[54,33404,2714],{"class":99},[54,33406,545],{"class":544},[54,33408,109],{"class":88},[54,33410,33411,33413,33415,33417],{"class":56,"line":887},[54,33412,116],{"class":115},[54,33414,96],{"class":88},[54,33416,6246],{"class":92},[54,33418,109],{"class":88},[40,33420,1510],{"id":1509},[149,33422,33423,33427,33431,33435],{},[152,33424,33425],{},[192,33426,5052],{"href":3535},[152,33428,33429],{},[192,33430,5206],{"href":2838},[152,33432,33433],{},[192,33434,32248],{"href":2830},[152,33436,33437],{},[192,33438,33439],{"href":2002},"How to fix \"module has no attribute\"",[14,33441,33442],{},"If you are learning Python step by step, the best next move is to pick one exact task: getting the current date, parsing a date string, or formatting a datetime for output.",[1545,33444,33445],{},"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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .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 .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 .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":50,"searchDepth":68,"depth":68,"links":33447},[33448,33449,33450,33457,33458,33470,33475,33476,33487,33494],{"id":3595,"depth":68,"text":3596},{"id":31301,"depth":68,"text":31302},{"id":31348,"depth":68,"text":31349,"children":33451},[33452,33453,33454,33455,33456],{"id":2535,"depth":75,"text":2535},{"id":2559,"depth":75,"text":2559},{"id":1628,"depth":75,"text":1628},{"id":31673,"depth":75,"text":31673},{"id":31793,"depth":75,"text":31793},{"id":31809,"depth":68,"text":31810},{"id":32022,"depth":68,"text":32023,"children":33459},[33460,33462,33464,33466,33468],{"id":32031,"depth":75,"text":33461},"Get the current date with date.today()",{"id":32080,"depth":75,"text":33463},"Get the current date and time with datetime.now()",{"id":32133,"depth":75,"text":33465},"Format a datetime as text with strftime()",{"id":32251,"depth":75,"text":33467},"Parse text into a datetime with strptime()",{"id":32374,"depth":75,"text":33469},"Add days with timedelta(days=...)",{"id":32467,"depth":68,"text":32468,"children":33471},[33472,33473,33474],{"id":32474,"depth":75,"text":3356},{"id":32552,"depth":75,"text":32555},{"id":32601,"depth":75,"text":32602},{"id":32667,"depth":68,"text":32668},{"id":977,"depth":68,"text":978,"children":33477},[33478,33480,33482,33484,33486],{"id":32704,"depth":75,"text":33479},"Confusing the datetime module with the datetime class",{"id":32823,"depth":75,"text":33481},"Using strptime() with the wrong format string",{"id":32971,"depth":75,"text":33483},"Trying to add a string directly to a datetime object",{"id":33093,"depth":75,"text":33485},"Expecting date objects to have time values",{"id":33162,"depth":75,"text":33163},{"id":1418,"depth":68,"text":1419,"children":33488},[33489,33490,33491,33492,33493],{"id":33211,"depth":75,"text":33212},{"id":33223,"depth":75,"text":33224},{"id":33232,"depth":75,"text":33233},{"id":33277,"depth":75,"text":33278},{"id":33342,"depth":75,"text":33343},{"id":1509,"depth":68,"text":1510},"Master python datetime module overview in our comprehensive Python beginner guide.",{},{"title":31005,"description":33495},"standard-library\u002Fpython-datetime-module-overview","gHiCzAaKWgT4Z4-8u4aU4w2Csy8utBFBD1t3i70vdhU",{"id":33501,"title":33502,"body":33503,"description":36049,"extension":1577,"meta":36050,"navigation":71,"path":8781,"seo":36051,"stem":36052,"__hash__":36053},"content\u002Fstandard-library\u002Fpython-json-module-overview.md","Python json Module Overview",{"type":7,"value":33504,"toc":36008},[33505,33508,33513,33516,33530,33536,33538,33660,33664,33706,33714,33718,33723,33726,33742,33745,33755,33759,33762,33765,33798,33800,33914,33918,34037,34044,34048,34054,34076,34079,34098,34101,34122,34124,34127,34132,34137,34205,34210,34215,34291,34296,34301,34402,34407,34412,34516,34521,34525,34528,34642,34646,34719,34722,34741,34745,34748,34870,34874,34937,34940,34953,34958,35069,35073,35130,35136,35140,35150,35154,35160,35199,35202,35287,35291,35402,35407,35412,35416,35421,35428,35431,35442,35444,35493,35495,35540,35547,35559,35566,35570,35573,35576,35590,35592,35606,35610,35613,35615,35629,35631,35643,35647,35650,35656,35719,35722,35725,35804,35806,35809,35812,35828,35835,35839,35842,35865,35868,35916,35921,35923,35927,35933,35939,35946,35952,35959,35963,35966,35970,35973,35975,36005],[10,33506,33502],{"id":33507},"python-json-module-overview",[14,33509,28340,33510,33512],{},[17,33511,8385],{}," module is Python’s built-in tool for working with JSON data.",[14,33514,33515],{},"Use it when you need to:",[149,33517,33518,33521,33524,33527],{},[152,33519,33520],{},"Read JSON text into Python",[152,33522,33523],{},"Convert Python data into JSON",[152,33525,33526],{},"Work with JSON files",[152,33528,33529],{},"Send or receive structured data in APIs",[14,33531,33532,33533,33535],{},"JSON is one of the most common data formats in programming, so learning the basic ",[17,33534,8385],{}," functions is very useful.",[40,33537,3596],{"id":3595},[45,33539,33541],{"className":47,"code":33540,"language":49,"meta":50,"style":50},"import json\n\nperson = {\"name\": \"Sam\", \"age\": 25}\njson_text = json.dumps(person)\nprint(json_text)\n\nback_to_dict = json.loads(json_text)\nprint(back_to_dict[\"name\"])\n",[17,33542,33543,33549,33553,33590,33608,33618,33622,33641],{"__ignoreMap":50},[54,33544,33545,33547],{"class":56,"line":57},[54,33546,61],{"class":60},[54,33548,8212],{"class":64},[54,33550,33551],{"class":56,"line":68},[54,33552,72],{"emptyLinePlaceholder":71},[54,33554,33555,33557,33559,33561,33563,33565,33567,33569,33571,33574,33576,33578,33580,33582,33584,33586,33588],{"class":56,"line":75},[54,33556,8555],{"class":64},[54,33558,82],{"class":81},[54,33560,8226],{"class":88},[54,33562,545],{"class":544},[54,33564,8231],{"class":548},[54,33566,545],{"class":544},[54,33568,147],{"class":88},[54,33570,830],{"class":544},[54,33572,33573],{"class":548},"Sam",[54,33575,545],{"class":544},[54,33577,103],{"class":88},[54,33579,830],{"class":544},[54,33581,8249],{"class":548},[54,33583,545],{"class":544},[54,33585,147],{"class":88},[54,33587,11369],{"class":99},[54,33589,8258],{"class":88},[54,33591,33592,33594,33596,33598,33600,33602,33604,33606],{"class":56,"line":112},[54,33593,9553],{"class":64},[54,33595,82],{"class":81},[54,33597,9558],{"class":64},[54,33599,89],{"class":88},[54,33601,9563],{"class":92},[54,33603,96],{"class":88},[54,33605,8660],{"class":92},[54,33607,109],{"class":88},[54,33609,33610,33612,33614,33616],{"class":56,"line":887},[54,33611,116],{"class":115},[54,33613,96],{"class":88},[54,33615,9578],{"class":92},[54,33617,109],{"class":88},[54,33619,33620],{"class":56,"line":892},[54,33621,72],{"emptyLinePlaceholder":71},[54,33623,33624,33627,33629,33631,33633,33635,33637,33639],{"class":56,"line":913},[54,33625,33626],{"class":64},"back_to_dict ",[54,33628,82],{"class":81},[54,33630,9558],{"class":64},[54,33632,89],{"class":88},[54,33634,14944],{"class":92},[54,33636,96],{"class":88},[54,33638,9578],{"class":92},[54,33640,109],{"class":88},[54,33642,33643,33645,33647,33650,33652,33654,33656,33658],{"class":56,"line":2121},[54,33644,116],{"class":115},[54,33646,96],{"class":88},[54,33648,33649],{"class":92},"back_to_dict",[54,33651,459],{"class":88},[54,33653,545],{"class":544},[54,33655,8231],{"class":548},[54,33657,545],{"class":544},[54,33659,937],{"class":88},[14,33661,33662],{},[35,33663,449],{},[45,33665,33667],{"className":47,"code":33666,"language":49,"meta":50,"style":50},"{\"name\": \"Sam\", \"age\": 25}\nSam\n",[17,33668,33669,33701],{"__ignoreMap":50},[54,33670,33671,33673,33675,33677,33679,33681,33683,33685,33687,33689,33691,33693,33695,33697,33699],{"class":56,"line":57},[54,33672,8711],{"class":88},[54,33674,545],{"class":544},[54,33676,8231],{"class":548},[54,33678,545],{"class":544},[54,33680,147],{"class":88},[54,33682,830],{"class":544},[54,33684,33573],{"class":548},[54,33686,545],{"class":544},[54,33688,103],{"class":88},[54,33690,830],{"class":544},[54,33692,8249],{"class":548},[54,33694,545],{"class":544},[54,33696,147],{"class":88},[54,33698,11369],{"class":99},[54,33700,8258],{"class":88},[54,33702,33703],{"class":56,"line":68},[54,33704,33705],{"class":64},"Sam\n",[14,33707,126,33708,33710,33711,33713],{},[17,33709,8371],{}," to convert a Python object to a JSON string, and ",[17,33712,13451],{}," to convert a JSON string back to a Python object.",[40,33715,33717],{"id":33716},"what-the-json-module-does","What the json module does",[14,33719,28340,33720,33722],{},[17,33721,8385],{}," module works with JSON data in Python.",[14,33724,33725],{},"A few key points:",[149,33727,33728,33731,33734,33739],{},[152,33729,33730],{},"JSON is a text format for storing and sending data",[152,33732,33733],{},"It is commonly used in APIs, configuration files, and data exchange",[152,33735,28340,33736,33738],{},[17,33737,8385],{}," module is part of Python’s standard library",[152,33740,33741],{},"You do not need to install anything before using it",[14,33743,33744],{},"To use it, import it first:",[45,33746,33747],{"className":47,"code":13745,"language":49,"meta":50,"style":50},[17,33748,33749],{"__ignoreMap":50},[54,33750,33751,33753],{"class":56,"line":57},[54,33752,61],{"class":60},[54,33754,8212],{"class":64},[40,33756,33758],{"id":33757},"json-vs-python-data-types","JSON vs Python data types",[14,33760,33761],{},"When Python reads JSON, it converts JSON values into Python data types.",[14,33763,33764],{},"Here is the basic mapping:",[149,33766,33767,33770,33773,33776,33782,33792],{},[152,33768,33769],{},"JSON object → Python dictionary",[152,33771,33772],{},"JSON array → Python list",[152,33774,33775],{},"JSON string → Python string",[152,33777,13971,33778,5507,33780],{},[17,33779,9657],{},[17,33781,9662],{},[152,33783,13978,33784,16386,33786,13983,33788,16386,33790],{},[17,33785,11767],{},[17,33787,8763],{},[17,33789,9175],{},[17,33791,8767],{},[152,33793,13978,33794,13983,33796],{},[17,33795,8772],{},[17,33797,8775],{},[14,33799,253],{},[45,33801,33803],{"className":47,"code":33802,"language":49,"meta":50,"style":50},"import json\n\njson_text = '{\"name\": \"Ana\", \"age\": 30, \"is_admin\": false, \"skills\": [\"Python\", \"SQL\"], \"manager\": null}'\ndata = json.loads(json_text)\n\nprint(data)\nprint(type(data))\nprint(type(data[\"skills\"]))\nprint(data[\"manager\"])\n",[17,33804,33805,33811,33815,33828,33846,33850,33860,33874,33896],{"__ignoreMap":50},[54,33806,33807,33809],{"class":56,"line":57},[54,33808,61],{"class":60},[54,33810,8212],{"class":64},[54,33812,33813],{"class":56,"line":68},[54,33814,72],{"emptyLinePlaceholder":71},[54,33816,33817,33819,33821,33823,33826],{"class":56,"line":75},[54,33818,9553],{"class":64},[54,33820,82],{"class":81},[54,33822,2234],{"class":544},[54,33824,33825],{"class":548},"{\"name\": \"Ana\", \"age\": 30, \"is_admin\": false, \"skills\": [\"Python\", \"SQL\"], \"manager\": null}",[54,33827,5297],{"class":544},[54,33829,33830,33832,33834,33836,33838,33840,33842,33844],{"class":56,"line":112},[54,33831,8221],{"class":64},[54,33833,82],{"class":81},[54,33835,9558],{"class":64},[54,33837,89],{"class":88},[54,33839,14944],{"class":92},[54,33841,96],{"class":88},[54,33843,9578],{"class":92},[54,33845,109],{"class":88},[54,33847,33848],{"class":56,"line":887},[54,33849,72],{"emptyLinePlaceholder":71},[54,33851,33852,33854,33856,33858],{"class":56,"line":892},[54,33853,116],{"class":115},[54,33855,96],{"class":88},[54,33857,8313],{"class":92},[54,33859,109],{"class":88},[54,33861,33862,33864,33866,33868,33870,33872],{"class":56,"line":913},[54,33863,116],{"class":115},[54,33865,96],{"class":88},[54,33867,2208],{"class":429},[54,33869,96],{"class":88},[54,33871,8313],{"class":92},[54,33873,382],{"class":88},[54,33875,33876,33878,33880,33882,33884,33886,33888,33890,33892,33894],{"class":56,"line":2121},[54,33877,116],{"class":115},[54,33879,96],{"class":88},[54,33881,2208],{"class":429},[54,33883,96],{"class":88},[54,33885,8313],{"class":92},[54,33887,459],{"class":88},[54,33889,545],{"class":544},[54,33891,8860],{"class":548},[54,33893,545],{"class":544},[54,33895,14245],{"class":88},[54,33897,33898,33900,33902,33904,33906,33908,33910,33912],{"class":56,"line":2136},[54,33899,116],{"class":115},[54,33901,96],{"class":88},[54,33903,8313],{"class":92},[54,33905,459],{"class":88},[54,33907,545],{"class":544},[54,33909,14087],{"class":548},[54,33911,545],{"class":544},[54,33913,937],{"class":88},[14,33915,33916],{},[35,33917,449],{},[45,33919,33921],{"className":47,"code":33920,"language":49,"meta":50,"style":50},"{'name': 'Ana', 'age': 30, 'is_admin': False, 'skills': ['Python', 'SQL'], 'manager': None}\n\u003Cclass 'dict'>\n\u003Cclass 'list'>\nNone\n",[17,33922,33923,34005,34019,34033],{"__ignoreMap":50},[54,33924,33925,33927,33929,33931,33933,33935,33937,33939,33941,33943,33945,33947,33949,33951,33953,33955,33957,33959,33961,33963,33965,33967,33969,33971,33973,33975,33977,33979,33981,33983,33985,33987,33989,33991,33993,33995,33997,33999,34001,34003],{"class":56,"line":57},[54,33926,8711],{"class":88},[54,33928,2240],{"class":544},[54,33930,8231],{"class":548},[54,33932,2240],{"class":544},[54,33934,147],{"class":88},[54,33936,2234],{"class":544},[54,33938,823],{"class":548},[54,33940,2240],{"class":544},[54,33942,103],{"class":88},[54,33944,2234],{"class":544},[54,33946,8249],{"class":548},[54,33948,2240],{"class":544},[54,33950,147],{"class":88},[54,33952,3981],{"class":99},[54,33954,103],{"class":88},[54,33956,2234],{"class":544},[54,33958,8601],{"class":548},[54,33960,2240],{"class":544},[54,33962,147],{"class":88},[54,33964,14296],{"class":8608},[54,33966,103],{"class":88},[54,33968,2234],{"class":544},[54,33970,8860],{"class":548},[54,33972,2240],{"class":544},[54,33974,147],{"class":88},[54,33976,818],{"class":88},[54,33978,2240],{"class":544},[54,33980,8871],{"class":548},[54,33982,2240],{"class":544},[54,33984,103],{"class":88},[54,33986,2234],{"class":544},[54,33988,8880],{"class":548},[54,33990,2240],{"class":544},[54,33992,11951],{"class":88},[54,33994,2234],{"class":544},[54,33996,14087],{"class":548},[54,33998,2240],{"class":544},[54,34000,147],{"class":88},[54,34002,14335],{"class":8608},[54,34004,8258],{"class":88},[54,34006,34007,34009,34011,34013,34015,34017],{"class":56,"line":68},[54,34008,2227],{"class":81},[54,34010,2231],{"class":2230},[54,34012,2234],{"class":544},[54,34014,9644],{"class":548},[54,34016,2240],{"class":544},[54,34018,2243],{"class":81},[54,34020,34021,34023,34025,34027,34029,34031],{"class":56,"line":75},[54,34022,2227],{"class":81},[54,34024,2231],{"class":2230},[54,34026,2234],{"class":544},[54,34028,430],{"class":548},[54,34030,2240],{"class":544},[54,34032,2243],{"class":81},[54,34034,34035],{"class":56,"line":112},[54,34036,16695],{"class":8608},[14,34038,34039,34040,89],{},"If you want a refresher on dictionaries, see ",[192,34041,34043],{"href":34042},"\u002Flearn\u002Fpython-dictionaries-explained","Python dictionaries explained",[40,34045,34047],{"id":34046},"main-functions-to-know","Main functions to know",[14,34049,34050,34051,34053],{},"These are the four ",[17,34052,8385],{}," functions beginners see most often:",[149,34055,34056,34061,34066,34071],{},[152,34057,34058,34060],{},[17,34059,13451],{}," reads JSON from a string",[152,34062,34063,34065],{},[17,34064,13580],{}," reads JSON from a file object",[152,34067,34068,34070],{},[17,34069,8371],{}," creates a JSON string from a Python object",[152,34072,34073,34075],{},[17,34074,8190],{}," writes JSON to a file object",[14,34077,34078],{},"A simple way to remember them:",[149,34080,34081,34086,34091],{},[152,34082,34083,34085],{},[17,34084,13661],{}," = read",[152,34087,34088,34090],{},[17,34089,8308],{}," = write",[152,34092,34093,34094,34097],{},"extra ",[17,34095,34096],{},"s"," = string",[14,34099,34100],{},"If you want a deeper explanation of each one, see:",[149,34102,34103,34108,34113,34118],{},[152,34104,34105],{},[192,34106,34107],{"href":15724},"json.load() function explained",[152,34109,34110],{},[192,34111,34112],{"href":13465},"json.loads() function explained",[152,34114,34115],{},[192,34116,34117],{"href":11295},"json.dump() function explained",[152,34119,34120],{},[192,34121,11229],{"href":8368},[40,34123,19716],{"id":19715},[14,34125,34126],{},"Use the function based on what kind of data you already have.",[602,34128,126,34130],{"id":34129},"use-jsonloads",[17,34131,13451],{},[14,34133,126,34134,34136],{},[17,34135,13451],{}," when your JSON is already in a string.",[45,34138,34140],{"className":47,"code":34139,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"city\": \"Paris\", \"country\": \"France\"}'\ndata = json.loads(text)\n\nprint(data[\"city\"])\n",[17,34141,34142,34148,34152,34165,34183,34187],{"__ignoreMap":50},[54,34143,34144,34146],{"class":56,"line":57},[54,34145,61],{"class":60},[54,34147,8212],{"class":64},[54,34149,34150],{"class":56,"line":68},[54,34151,72],{"emptyLinePlaceholder":71},[54,34153,34154,34156,34158,34160,34163],{"class":56,"line":75},[54,34155,5134],{"class":64},[54,34157,82],{"class":81},[54,34159,2234],{"class":544},[54,34161,34162],{"class":548},"{\"city\": \"Paris\", \"country\": \"France\"}",[54,34164,5297],{"class":544},[54,34166,34167,34169,34171,34173,34175,34177,34179,34181],{"class":56,"line":112},[54,34168,8221],{"class":64},[54,34170,82],{"class":81},[54,34172,9558],{"class":64},[54,34174,89],{"class":88},[54,34176,14944],{"class":92},[54,34178,96],{"class":88},[54,34180,5171],{"class":92},[54,34182,109],{"class":88},[54,34184,34185],{"class":56,"line":887},[54,34186,72],{"emptyLinePlaceholder":71},[54,34188,34189,34191,34193,34195,34197,34199,34201,34203],{"class":56,"line":892},[54,34190,116],{"class":115},[54,34192,96],{"class":88},[54,34194,8313],{"class":92},[54,34196,459],{"class":88},[54,34198,545],{"class":544},[54,34200,8450],{"class":548},[54,34202,545],{"class":544},[54,34204,937],{"class":88},[602,34206,126,34208],{"id":34207},"use-jsonload",[17,34209,13580],{},[14,34211,126,34212,34214],{},[17,34213,13580],{}," when reading JSON from a file.",[45,34216,34217],{"className":47,"code":16788,"language":49,"meta":50,"style":50},[17,34218,34219,34225,34229,34259,34277,34281],{"__ignoreMap":50},[54,34220,34221,34223],{"class":56,"line":57},[54,34222,61],{"class":60},[54,34224,8212],{"class":64},[54,34226,34227],{"class":56,"line":68},[54,34228,72],{"emptyLinePlaceholder":71},[54,34230,34231,34233,34235,34237,34239,34241,34243,34245,34247,34249,34251,34253,34255,34257],{"class":56,"line":75},[54,34232,8267],{"class":60},[54,34234,8270],{"class":115},[54,34236,96],{"class":88},[54,34238,545],{"class":544},[54,34240,8277],{"class":548},[54,34242,545],{"class":544},[54,34244,103],{"class":88},[54,34246,830],{"class":544},[54,34248,10658],{"class":548},[54,34250,545],{"class":544},[54,34252,876],{"class":88},[54,34254,7631],{"class":60},[54,34256,8296],{"class":8295},[54,34258,7572],{"class":88},[54,34260,34261,34263,34265,34267,34269,34271,34273,34275],{"class":56,"line":112},[54,34262,13652],{"class":64},[54,34264,82],{"class":81},[54,34266,9558],{"class":64},[54,34268,89],{"class":88},[54,34270,13661],{"class":92},[54,34272,96],{"class":88},[54,34274,8412],{"class":8295},[54,34276,109],{"class":88},[54,34278,34279],{"class":56,"line":887},[54,34280,72],{"emptyLinePlaceholder":71},[54,34282,34283,34285,34287,34289],{"class":56,"line":892},[54,34284,116],{"class":115},[54,34286,96],{"class":88},[54,34288,8313],{"class":92},[54,34290,109],{"class":88},[602,34292,126,34294],{"id":34293},"use-jsondumps",[17,34295,8371],{},[14,34297,126,34298,34300],{},[17,34299,8371],{}," when you need JSON as a string.",[45,34302,34304],{"className":47,"code":34303,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"product\": \"Book\", \"price\": 12.99}\ntext = json.dumps(data)\n\nprint(text)\nprint(type(text))\n",[17,34305,34306,34312,34316,34356,34374,34378,34388],{"__ignoreMap":50},[54,34307,34308,34310],{"class":56,"line":57},[54,34309,61],{"class":60},[54,34311,8212],{"class":64},[54,34313,34314],{"class":56,"line":68},[54,34315,72],{"emptyLinePlaceholder":71},[54,34317,34318,34320,34322,34324,34326,34329,34331,34333,34335,34338,34340,34342,34344,34347,34349,34351,34354],{"class":56,"line":75},[54,34319,8221],{"class":64},[54,34321,82],{"class":81},[54,34323,8226],{"class":88},[54,34325,545],{"class":544},[54,34327,34328],{"class":548},"product",[54,34330,545],{"class":544},[54,34332,147],{"class":88},[54,34334,830],{"class":544},[54,34336,34337],{"class":548},"Book",[54,34339,545],{"class":544},[54,34341,103],{"class":88},[54,34343,830],{"class":544},[54,34345,34346],{"class":548},"price",[54,34348,545],{"class":544},[54,34350,147],{"class":88},[54,34352,34353],{"class":99}," 12.99",[54,34355,8258],{"class":88},[54,34357,34358,34360,34362,34364,34366,34368,34370,34372],{"class":56,"line":112},[54,34359,5134],{"class":64},[54,34361,82],{"class":81},[54,34363,9558],{"class":64},[54,34365,89],{"class":88},[54,34367,9563],{"class":92},[54,34369,96],{"class":88},[54,34371,8313],{"class":92},[54,34373,109],{"class":88},[54,34375,34376],{"class":56,"line":887},[54,34377,72],{"emptyLinePlaceholder":71},[54,34379,34380,34382,34384,34386],{"class":56,"line":892},[54,34381,116],{"class":115},[54,34383,96],{"class":88},[54,34385,5171],{"class":92},[54,34387,109],{"class":88},[54,34389,34390,34392,34394,34396,34398,34400],{"class":56,"line":913},[54,34391,116],{"class":115},[54,34393,96],{"class":88},[54,34395,2208],{"class":429},[54,34397,96],{"class":88},[54,34399,5171],{"class":92},[54,34401,382],{"class":88},[602,34403,126,34405],{"id":34404},"use-jsondump",[17,34406,8190],{},[14,34408,126,34409,34411],{},[17,34410,8190],{}," when saving Python data directly to a file.",[45,34413,34415],{"className":47,"code":34414,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"product\": \"Book\", \"price\": 12.99}\n\nwith open(\"output.json\", \"w\") as file:\n    json.dump(data, file)\n",[17,34416,34417,34423,34427,34463,34467,34498],{"__ignoreMap":50},[54,34418,34419,34421],{"class":56,"line":57},[54,34420,61],{"class":60},[54,34422,8212],{"class":64},[54,34424,34425],{"class":56,"line":68},[54,34426,72],{"emptyLinePlaceholder":71},[54,34428,34429,34431,34433,34435,34437,34439,34441,34443,34445,34447,34449,34451,34453,34455,34457,34459,34461],{"class":56,"line":75},[54,34430,8221],{"class":64},[54,34432,82],{"class":81},[54,34434,8226],{"class":88},[54,34436,545],{"class":544},[54,34438,34328],{"class":548},[54,34440,545],{"class":544},[54,34442,147],{"class":88},[54,34444,830],{"class":544},[54,34446,34337],{"class":548},[54,34448,545],{"class":544},[54,34450,103],{"class":88},[54,34452,830],{"class":544},[54,34454,34346],{"class":548},[54,34456,545],{"class":544},[54,34458,147],{"class":88},[54,34460,34353],{"class":99},[54,34462,8258],{"class":88},[54,34464,34465],{"class":56,"line":112},[54,34466,72],{"emptyLinePlaceholder":71},[54,34468,34469,34471,34473,34475,34477,34480,34482,34484,34486,34488,34490,34492,34494,34496],{"class":56,"line":887},[54,34470,8267],{"class":60},[54,34472,8270],{"class":115},[54,34474,96],{"class":88},[54,34476,545],{"class":544},[54,34478,34479],{"class":548},"output.json",[54,34481,545],{"class":544},[54,34483,103],{"class":88},[54,34485,830],{"class":544},[54,34487,8286],{"class":548},[54,34489,545],{"class":544},[54,34491,876],{"class":88},[54,34493,7631],{"class":60},[54,34495,8296],{"class":8295},[54,34497,7572],{"class":88},[54,34499,34500,34502,34504,34506,34508,34510,34512,34514],{"class":56,"line":892},[54,34501,8303],{"class":64},[54,34503,89],{"class":88},[54,34505,8308],{"class":92},[54,34507,96],{"class":88},[54,34509,8313],{"class":92},[54,34511,103],{"class":88},[54,34513,8296],{"class":8295},[54,34515,109],{"class":88},[14,34517,34518,34519,89],{},"If your goal is to parse JSON step by step, see ",[192,34520,14797],{"href":8140},[40,34522,34524],{"id":34523},"basic-reading-example","Basic reading example",[14,34526,34527],{},"Here is a simple example of reading JSON from a string.",[45,34529,34531],{"className":47,"code":34530,"language":49,"meta":50,"style":50},"import json\n\njson_text = '{\"name\": \"Lina\", \"age\": 22, \"active\": true}'\n\ndata = json.loads(json_text)\n\nprint(data)\nprint(data[\"name\"])\nprint(data[\"age\"])\nprint(type(data))\n",[17,34532,34533,34539,34543,34556,34560,34578,34582,34592,34610,34628],{"__ignoreMap":50},[54,34534,34535,34537],{"class":56,"line":57},[54,34536,61],{"class":60},[54,34538,8212],{"class":64},[54,34540,34541],{"class":56,"line":68},[54,34542,72],{"emptyLinePlaceholder":71},[54,34544,34545,34547,34549,34551,34554],{"class":56,"line":75},[54,34546,9553],{"class":64},[54,34548,82],{"class":81},[54,34550,2234],{"class":544},[54,34552,34553],{"class":548},"{\"name\": \"Lina\", \"age\": 22, \"active\": true}",[54,34555,5297],{"class":544},[54,34557,34558],{"class":56,"line":112},[54,34559,72],{"emptyLinePlaceholder":71},[54,34561,34562,34564,34566,34568,34570,34572,34574,34576],{"class":56,"line":887},[54,34563,8221],{"class":64},[54,34565,82],{"class":81},[54,34567,9558],{"class":64},[54,34569,89],{"class":88},[54,34571,14944],{"class":92},[54,34573,96],{"class":88},[54,34575,9578],{"class":92},[54,34577,109],{"class":88},[54,34579,34580],{"class":56,"line":892},[54,34581,72],{"emptyLinePlaceholder":71},[54,34583,34584,34586,34588,34590],{"class":56,"line":913},[54,34585,116],{"class":115},[54,34587,96],{"class":88},[54,34589,8313],{"class":92},[54,34591,109],{"class":88},[54,34593,34594,34596,34598,34600,34602,34604,34606,34608],{"class":56,"line":2121},[54,34595,116],{"class":115},[54,34597,96],{"class":88},[54,34599,8313],{"class":92},[54,34601,459],{"class":88},[54,34603,545],{"class":544},[54,34605,8231],{"class":548},[54,34607,545],{"class":544},[54,34609,937],{"class":88},[54,34611,34612,34614,34616,34618,34620,34622,34624,34626],{"class":56,"line":2136},[54,34613,116],{"class":115},[54,34615,96],{"class":88},[54,34617,8313],{"class":92},[54,34619,459],{"class":88},[54,34621,545],{"class":544},[54,34623,8249],{"class":548},[54,34625,545],{"class":544},[54,34627,937],{"class":88},[54,34629,34630,34632,34634,34636,34638,34640],{"class":56,"line":2151},[54,34631,116],{"class":115},[54,34633,96],{"class":88},[54,34635,2208],{"class":429},[54,34637,96],{"class":88},[54,34639,8313],{"class":92},[54,34641,382],{"class":88},[14,34643,34644],{},[35,34645,449],{},[45,34647,34649],{"className":47,"code":34648,"language":49,"meta":50,"style":50},"{'name': 'Lina', 'age': 22, 'active': True}\nLina\n22\n\u003Cclass 'dict'>\n",[17,34650,34651,34696,34701,34705],{"__ignoreMap":50},[54,34652,34653,34655,34657,34659,34661,34663,34665,34668,34670,34672,34674,34676,34678,34680,34682,34684,34686,34688,34690,34692,34694],{"class":56,"line":57},[54,34654,8711],{"class":88},[54,34656,2240],{"class":544},[54,34658,8231],{"class":548},[54,34660,2240],{"class":544},[54,34662,147],{"class":88},[54,34664,2234],{"class":544},[54,34666,34667],{"class":548},"Lina",[54,34669,2240],{"class":544},[54,34671,103],{"class":88},[54,34673,2234],{"class":544},[54,34675,8249],{"class":548},[54,34677,2240],{"class":544},[54,34679,147],{"class":88},[54,34681,4269],{"class":99},[54,34683,103],{"class":88},[54,34685,2234],{"class":544},[54,34687,9739],{"class":548},[54,34689,2240],{"class":544},[54,34691,147],{"class":88},[54,34693,9746],{"class":8608},[54,34695,8258],{"class":88},[54,34697,34698],{"class":56,"line":68},[54,34699,34700],{"class":64},"Lina\n",[54,34702,34703],{"class":56,"line":75},[54,34704,2617],{"class":99},[54,34706,34707,34709,34711,34713,34715,34717],{"class":56,"line":112},[54,34708,2227],{"class":81},[54,34710,2231],{"class":2230},[54,34712,2234],{"class":544},[54,34714,9644],{"class":548},[54,34716,2240],{"class":544},[54,34718,2243],{"class":81},[14,34720,34721],{},"What happens here:",[149,34723,34724,34729,34735],{},[152,34725,34726,34728],{},[17,34727,9578],{}," is a string containing JSON",[152,34730,34731,34734],{},[17,34732,34733],{},"json.loads(json_text)"," converts that JSON into a Python dictionary",[152,34736,34737,34738],{},"After that, you can access values with dictionary keys like ",[17,34739,34740],{},"data[\"name\"]",[40,34742,34744],{"id":34743},"basic-writing-example","Basic writing example",[14,34746,34747],{},"Here is a simple example of converting a Python dictionary into JSON.",[45,34749,34751],{"className":47,"code":34750,"language":49,"meta":50,"style":50},"import json\n\nperson = {\n    \"name\": \"Lina\",\n    \"age\": 22,\n    \"active\": True\n}\n\njson_text = json.dumps(person)\n\nprint(json_text)\nprint(type(json_text))\n",[17,34752,34753,34759,34763,34771,34789,34803,34816,34820,34824,34842,34846,34856],{"__ignoreMap":50},[54,34754,34755,34757],{"class":56,"line":57},[54,34756,61],{"class":60},[54,34758,8212],{"class":64},[54,34760,34761],{"class":56,"line":68},[54,34762,72],{"emptyLinePlaceholder":71},[54,34764,34765,34767,34769],{"class":56,"line":75},[54,34766,8555],{"class":64},[54,34768,82],{"class":81},[54,34770,8560],{"class":88},[54,34772,34773,34775,34777,34779,34781,34783,34785,34787],{"class":56,"line":112},[54,34774,8565],{"class":544},[54,34776,8231],{"class":548},[54,34778,545],{"class":544},[54,34780,147],{"class":88},[54,34782,830],{"class":544},[54,34784,34667],{"class":548},[54,34786,545],{"class":544},[54,34788,8580],{"class":88},[54,34790,34791,34793,34795,34797,34799,34801],{"class":56,"line":887},[54,34792,8565],{"class":544},[54,34794,8249],{"class":548},[54,34796,545],{"class":544},[54,34798,147],{"class":88},[54,34800,4269],{"class":99},[54,34802,8580],{"class":88},[54,34804,34805,34807,34809,34811,34813],{"class":56,"line":892},[54,34806,8565],{"class":544},[54,34808,9739],{"class":548},[54,34810,545],{"class":544},[54,34812,147],{"class":88},[54,34814,34815],{"class":8608}," True\n",[54,34817,34818],{"class":56,"line":913},[54,34819,8258],{"class":88},[54,34821,34822],{"class":56,"line":2121},[54,34823,72],{"emptyLinePlaceholder":71},[54,34825,34826,34828,34830,34832,34834,34836,34838,34840],{"class":56,"line":2136},[54,34827,9553],{"class":64},[54,34829,82],{"class":81},[54,34831,9558],{"class":64},[54,34833,89],{"class":88},[54,34835,9563],{"class":92},[54,34837,96],{"class":88},[54,34839,8660],{"class":92},[54,34841,109],{"class":88},[54,34843,34844],{"class":56,"line":2151},[54,34845,72],{"emptyLinePlaceholder":71},[54,34847,34848,34850,34852,34854],{"class":56,"line":4442},[54,34849,116],{"class":115},[54,34851,96],{"class":88},[54,34853,9578],{"class":92},[54,34855,109],{"class":88},[54,34857,34858,34860,34862,34864,34866,34868],{"class":56,"line":4468},[54,34859,116],{"class":115},[54,34861,96],{"class":88},[54,34863,2208],{"class":429},[54,34865,96],{"class":88},[54,34867,9578],{"class":92},[54,34869,382],{"class":88},[14,34871,34872],{},[35,34873,449],{},[45,34875,34877],{"className":47,"code":34876,"language":49,"meta":50,"style":50},"{\"name\": \"Lina\", \"age\": 22, \"active\": true}\n\u003Cclass 'str'>\n",[17,34878,34879,34923],{"__ignoreMap":50},[54,34880,34881,34883,34885,34887,34889,34891,34893,34895,34897,34899,34901,34903,34905,34907,34909,34911,34913,34915,34917,34919,34921],{"class":56,"line":57},[54,34882,8711],{"class":88},[54,34884,545],{"class":544},[54,34886,8231],{"class":548},[54,34888,545],{"class":544},[54,34890,147],{"class":88},[54,34892,830],{"class":544},[54,34894,34667],{"class":548},[54,34896,545],{"class":544},[54,34898,103],{"class":88},[54,34900,830],{"class":544},[54,34902,8249],{"class":548},[54,34904,545],{"class":544},[54,34906,147],{"class":88},[54,34908,4269],{"class":99},[54,34910,103],{"class":88},[54,34912,830],{"class":544},[54,34914,9739],{"class":548},[54,34916,545],{"class":544},[54,34918,147],{"class":88},[54,34920,11962],{"class":64},[54,34922,8258],{"class":88},[54,34924,34925,34927,34929,34931,34933,34935],{"class":56,"line":68},[54,34926,2227],{"class":81},[54,34928,2231],{"class":2230},[54,34930,2234],{"class":544},[54,34932,3173],{"class":548},[54,34934,2240],{"class":544},[54,34936,2243],{"class":81},[14,34938,34939],{},"Important:",[149,34941,34942,34948],{},[152,34943,34944,1623,34946],{},[17,34945,8371],{},[35,34947,6123],{},[152,34949,25550,34950,34952],{},[35,34951,3036],{}," return a dictionary",[14,34954,34955,34956,147],{},"You can also pretty-print JSON using ",[17,34957,8797],{},[45,34959,34961],{"className":47,"code":34960,"language":49,"meta":50,"style":50},"import json\n\nperson = {\n    \"name\": \"Lina\",\n    \"age\": 22,\n    \"active\": True\n}\n\njson_text = json.dumps(person, indent=4)\nprint(json_text)\n",[17,34962,34963,34969,34973,34981,34999,35013,35025,35029,35033,35059],{"__ignoreMap":50},[54,34964,34965,34967],{"class":56,"line":57},[54,34966,61],{"class":60},[54,34968,8212],{"class":64},[54,34970,34971],{"class":56,"line":68},[54,34972,72],{"emptyLinePlaceholder":71},[54,34974,34975,34977,34979],{"class":56,"line":75},[54,34976,8555],{"class":64},[54,34978,82],{"class":81},[54,34980,8560],{"class":88},[54,34982,34983,34985,34987,34989,34991,34993,34995,34997],{"class":56,"line":112},[54,34984,8565],{"class":544},[54,34986,8231],{"class":548},[54,34988,545],{"class":544},[54,34990,147],{"class":88},[54,34992,830],{"class":544},[54,34994,34667],{"class":548},[54,34996,545],{"class":544},[54,34998,8580],{"class":88},[54,35000,35001,35003,35005,35007,35009,35011],{"class":56,"line":887},[54,35002,8565],{"class":544},[54,35004,8249],{"class":548},[54,35006,545],{"class":544},[54,35008,147],{"class":88},[54,35010,4269],{"class":99},[54,35012,8580],{"class":88},[54,35014,35015,35017,35019,35021,35023],{"class":56,"line":892},[54,35016,8565],{"class":544},[54,35018,9739],{"class":548},[54,35020,545],{"class":544},[54,35022,147],{"class":88},[54,35024,34815],{"class":8608},[54,35026,35027],{"class":56,"line":913},[54,35028,8258],{"class":88},[54,35030,35031],{"class":56,"line":2121},[54,35032,72],{"emptyLinePlaceholder":71},[54,35034,35035,35037,35039,35041,35043,35045,35047,35049,35051,35053,35055,35057],{"class":56,"line":2136},[54,35036,9553],{"class":64},[54,35038,82],{"class":81},[54,35040,9558],{"class":64},[54,35042,89],{"class":88},[54,35044,9563],{"class":92},[54,35046,96],{"class":88},[54,35048,8660],{"class":92},[54,35050,103],{"class":88},[54,35052,8941],{"class":8940},[54,35054,82],{"class":81},[54,35056,2389],{"class":99},[54,35058,109],{"class":88},[54,35060,35061,35063,35065,35067],{"class":56,"line":2151},[54,35062,116],{"class":115},[54,35064,96],{"class":88},[54,35066,9578],{"class":92},[54,35068,109],{"class":88},[14,35070,35071],{},[35,35072,449],{},[45,35074,35076],{"className":47,"code":35075,"language":49,"meta":50,"style":50},"{\n    \"name\": \"Lina\",\n    \"age\": 22,\n    \"active\": true\n}\n",[17,35077,35078,35082,35100,35114,35126],{"__ignoreMap":50},[54,35079,35080],{"class":56,"line":57},[54,35081,8960],{"class":88},[54,35083,35084,35086,35088,35090,35092,35094,35096,35098],{"class":56,"line":68},[54,35085,8565],{"class":544},[54,35087,8231],{"class":548},[54,35089,545],{"class":544},[54,35091,147],{"class":88},[54,35093,830],{"class":544},[54,35095,34667],{"class":548},[54,35097,545],{"class":544},[54,35099,8580],{"class":88},[54,35101,35102,35104,35106,35108,35110,35112],{"class":56,"line":75},[54,35103,8565],{"class":544},[54,35105,8249],{"class":548},[54,35107,545],{"class":544},[54,35109,147],{"class":88},[54,35111,4269],{"class":99},[54,35113,8580],{"class":88},[54,35115,35116,35118,35120,35122,35124],{"class":56,"line":112},[54,35117,8565],{"class":544},[54,35119,9739],{"class":548},[54,35121,545],{"class":544},[54,35123,147],{"class":88},[54,35125,14445],{"class":64},[54,35127,35128],{"class":56,"line":887},[54,35129,8258],{"class":88},[14,35131,35132,35133,89],{},"For more on formatting output, see ",[192,35134,9040],{"href":35135},"\u002Fhow-to\u002Fhow-to-pretty-print-json-in-python\u002F",[40,35137,35139],{"id":35138},"working-with-json-files","Working with JSON files",[14,35141,35142,35143,35145,35146,5507,35148,89],{},"JSON is often stored in files. In that case, use ",[17,35144,8524],{}," together with ",[17,35147,13580],{},[17,35149,8190],{},[602,35151,35153],{"id":35152},"read-json-from-a-file","Read JSON from a file",[14,35155,35156,35157,35159],{},"Suppose ",[17,35158,8277],{}," contains this:",[45,35161,35163],{"className":8703,"code":35162,"language":8385,"meta":50,"style":50},"{\"username\": \"sam\", \"score\": 100}\n",[17,35164,35165],{"__ignoreMap":50},[54,35166,35167,35169,35171,35174,35176,35178,35180,35183,35185,35187,35189,35191,35193,35195,35197],{"class":56,"line":57},[54,35168,8711],{"class":88},[54,35170,545],{"class":8608},[54,35172,35173],{"class":8716},"username",[54,35175,545],{"class":8608},[54,35177,147],{"class":88},[54,35179,830],{"class":544},[54,35181,35182],{"class":548},"sam",[54,35184,545],{"class":544},[54,35186,103],{"class":88},[54,35188,830],{"class":8608},[54,35190,16542],{"class":8716},[54,35192,545],{"class":8608},[54,35194,147],{"class":88},[54,35196,757],{"class":99},[54,35198,8258],{"class":88},[14,35200,35201],{},"You can read it like this:",[45,35203,35205],{"className":47,"code":35204,"language":49,"meta":50,"style":50},"import json\n\nwith open(\"data.json\", \"r\") as file:\n    data = json.load(file)\n\nprint(data[\"username\"])\n",[17,35206,35207,35213,35217,35247,35265,35269],{"__ignoreMap":50},[54,35208,35209,35211],{"class":56,"line":57},[54,35210,61],{"class":60},[54,35212,8212],{"class":64},[54,35214,35215],{"class":56,"line":68},[54,35216,72],{"emptyLinePlaceholder":71},[54,35218,35219,35221,35223,35225,35227,35229,35231,35233,35235,35237,35239,35241,35243,35245],{"class":56,"line":75},[54,35220,8267],{"class":60},[54,35222,8270],{"class":115},[54,35224,96],{"class":88},[54,35226,545],{"class":544},[54,35228,8277],{"class":548},[54,35230,545],{"class":544},[54,35232,103],{"class":88},[54,35234,830],{"class":544},[54,35236,10658],{"class":548},[54,35238,545],{"class":544},[54,35240,876],{"class":88},[54,35242,7631],{"class":60},[54,35244,8296],{"class":8295},[54,35246,7572],{"class":88},[54,35248,35249,35251,35253,35255,35257,35259,35261,35263],{"class":56,"line":112},[54,35250,13652],{"class":64},[54,35252,82],{"class":81},[54,35254,9558],{"class":64},[54,35256,89],{"class":88},[54,35258,13661],{"class":92},[54,35260,96],{"class":88},[54,35262,8412],{"class":8295},[54,35264,109],{"class":88},[54,35266,35267],{"class":56,"line":887},[54,35268,72],{"emptyLinePlaceholder":71},[54,35270,35271,35273,35275,35277,35279,35281,35283,35285],{"class":56,"line":892},[54,35272,116],{"class":115},[54,35274,96],{"class":88},[54,35276,8313],{"class":92},[54,35278,459],{"class":88},[54,35280,545],{"class":544},[54,35282,35173],{"class":548},[54,35284,545],{"class":544},[54,35286,937],{"class":88},[602,35288,35290],{"id":35289},"write-json-to-a-file","Write JSON to a file",[45,35292,35294],{"className":47,"code":35293,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"username\": \"sam\", \"score\": 100}\n\nwith open(\"data.json\", \"w\") as file:\n    json.dump(data, file, indent=4)\n",[17,35295,35296,35302,35306,35342,35346,35376],{"__ignoreMap":50},[54,35297,35298,35300],{"class":56,"line":57},[54,35299,61],{"class":60},[54,35301,8212],{"class":64},[54,35303,35304],{"class":56,"line":68},[54,35305,72],{"emptyLinePlaceholder":71},[54,35307,35308,35310,35312,35314,35316,35318,35320,35322,35324,35326,35328,35330,35332,35334,35336,35338,35340],{"class":56,"line":75},[54,35309,8221],{"class":64},[54,35311,82],{"class":81},[54,35313,8226],{"class":88},[54,35315,545],{"class":544},[54,35317,35173],{"class":548},[54,35319,545],{"class":544},[54,35321,147],{"class":88},[54,35323,830],{"class":544},[54,35325,35182],{"class":548},[54,35327,545],{"class":544},[54,35329,103],{"class":88},[54,35331,830],{"class":544},[54,35333,16542],{"class":548},[54,35335,545],{"class":544},[54,35337,147],{"class":88},[54,35339,757],{"class":99},[54,35341,8258],{"class":88},[54,35343,35344],{"class":56,"line":112},[54,35345,72],{"emptyLinePlaceholder":71},[54,35347,35348,35350,35352,35354,35356,35358,35360,35362,35364,35366,35368,35370,35372,35374],{"class":56,"line":887},[54,35349,8267],{"class":60},[54,35351,8270],{"class":115},[54,35353,96],{"class":88},[54,35355,545],{"class":544},[54,35357,8277],{"class":548},[54,35359,545],{"class":544},[54,35361,103],{"class":88},[54,35363,830],{"class":544},[54,35365,8286],{"class":548},[54,35367,545],{"class":544},[54,35369,876],{"class":88},[54,35371,7631],{"class":60},[54,35373,8296],{"class":8295},[54,35375,7572],{"class":88},[54,35377,35378,35380,35382,35384,35386,35388,35390,35392,35394,35396,35398,35400],{"class":56,"line":892},[54,35379,8303],{"class":64},[54,35381,89],{"class":88},[54,35383,8308],{"class":92},[54,35385,96],{"class":88},[54,35387,8313],{"class":92},[54,35389,103],{"class":88},[54,35391,8296],{"class":8295},[54,35393,103],{"class":88},[54,35395,8941],{"class":8940},[54,35397,82],{"class":81},[54,35399,2389],{"class":99},[54,35401,109],{"class":88},[14,35403,5481,35404,35406],{},[17,35405,8419],{}," is the safest approach because Python closes the file automatically when you are done.",[14,35408,35409,35410,89],{},"If you need to convert a dictionary before saving it, see ",[192,35411,9628],{"href":15670},[40,35413,35415],{"id":35414},"common-beginner-problems","Common beginner problems",[14,35417,35418,35419,15765],{},"These are some of the most common mistakes when using the ",[17,35420,8385],{},[602,35422,5314,35424,134,35426],{"id":35423},"mixing-up-load-and-loads",[17,35425,13661],{},[17,35427,14944],{},[14,35429,35430],{},"This is a very common problem.",[149,35432,35433,35437],{},[152,35434,35435,16877],{},[17,35436,14511],{},[152,35438,35439,16872],{},[17,35440,35441],{},"json.loads(text)",[14,35443,5513],{},[45,35445,35447],{"className":47,"code":35446,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"a\": 1}'\ndata = json.load(text)  # wrong\n",[17,35448,35449,35455,35459,35472],{"__ignoreMap":50},[54,35450,35451,35453],{"class":56,"line":57},[54,35452,61],{"class":60},[54,35454,8212],{"class":64},[54,35456,35457],{"class":56,"line":68},[54,35458,72],{"emptyLinePlaceholder":71},[54,35460,35461,35463,35465,35467,35470],{"class":56,"line":75},[54,35462,5134],{"class":64},[54,35464,82],{"class":81},[54,35466,2234],{"class":544},[54,35468,35469],{"class":548},"{\"a\": 1}",[54,35471,5297],{"class":544},[54,35473,35474,35476,35478,35480,35482,35484,35486,35488,35490],{"class":56,"line":112},[54,35475,8221],{"class":64},[54,35477,82],{"class":81},[54,35479,9558],{"class":64},[54,35481,89],{"class":88},[54,35483,13661],{"class":92},[54,35485,96],{"class":88},[54,35487,5171],{"class":92},[54,35489,876],{"class":88},[54,35491,35492],{"class":4313},"  # wrong\n",[14,35494,13808],{},[45,35496,35498],{"className":47,"code":35497,"language":49,"meta":50,"style":50},"import json\n\ntext = '{\"a\": 1}'\ndata = json.loads(text)\n",[17,35499,35500,35506,35510,35522],{"__ignoreMap":50},[54,35501,35502,35504],{"class":56,"line":57},[54,35503,61],{"class":60},[54,35505,8212],{"class":64},[54,35507,35508],{"class":56,"line":68},[54,35509,72],{"emptyLinePlaceholder":71},[54,35511,35512,35514,35516,35518,35520],{"class":56,"line":75},[54,35513,5134],{"class":64},[54,35515,82],{"class":81},[54,35517,2234],{"class":544},[54,35519,35469],{"class":548},[54,35521,5297],{"class":544},[54,35523,35524,35526,35528,35530,35532,35534,35536,35538],{"class":56,"line":112},[54,35525,8221],{"class":64},[54,35527,82],{"class":81},[54,35529,9558],{"class":64},[54,35531,89],{"class":88},[54,35533,14944],{"class":92},[54,35535,96],{"class":88},[54,35537,5171],{"class":92},[54,35539,109],{"class":88},[602,35541,5314,35543,134,35545],{"id":35542},"mixing-up-dump-and-dumps",[17,35544,8308],{},[17,35546,9563],{},[149,35548,35549,35554],{},[152,35550,35551,8359],{},[17,35552,35553],{},"json.dump(data, file)",[152,35555,35556,13441],{},[17,35557,35558],{},"json.dumps(data)",[14,35560,35561,35562,35565],{},"If you expect ",[17,35563,35564],{},"dumps()"," to create a file automatically, it will not. It only gives you a string.",[602,35567,35569],{"id":35568},"using-single-quotes-in-json-text","Using single quotes in JSON text",[14,35571,35572],{},"Python dictionaries can use single quotes, but JSON text must use double quotes.",[14,35574,35575],{},"Wrong JSON:",[45,35577,35579],{"className":47,"code":35578,"language":49,"meta":50,"style":50},"\"{'name': 'Sam'}\"\n",[17,35580,35581],{"__ignoreMap":50},[54,35582,35583,35585,35588],{"class":56,"line":57},[54,35584,545],{"class":6905},[54,35586,35587],{"class":6908},"{'name': 'Sam'}",[54,35589,5239],{"class":6905},[14,35591,16991],{},[45,35593,35595],{"className":47,"code":35594,"language":49,"meta":50,"style":50},"'{\"name\": \"Sam\"}'\n",[17,35596,35597],{"__ignoreMap":50},[54,35598,35599,35601,35604],{"class":56,"line":57},[54,35600,2240],{"class":6905},[54,35602,35603],{"class":6908},"{\"name\": \"Sam\"}",[54,35605,5297],{"class":6905},[602,35607,35609],{"id":35608},"forgetting-that-json-keys-and-strings-use-double-quotes","Forgetting that JSON keys and strings use double quotes",[14,35611,35612],{},"In JSON, object keys must use double quotes.",[14,35614,16974],{},[45,35616,35618],{"className":47,"code":35617,"language":49,"meta":50,"style":50},"'{name: \"Sam\"}'\n",[17,35619,35620],{"__ignoreMap":50},[54,35621,35622,35624,35627],{"class":56,"line":57},[54,35623,2240],{"class":6905},[54,35625,35626],{"class":6908},"{name: \"Sam\"}",[54,35628,5297],{"class":6905},[14,35630,16991],{},[45,35632,35633],{"className":47,"code":35594,"language":49,"meta":50,"style":50},[17,35634,35635],{"__ignoreMap":50},[54,35636,35637,35639,35641],{"class":56,"line":57},[54,35638,2240],{"class":6905},[54,35640,35603],{"class":6908},[54,35642,5297],{"class":6905},[602,35644,35646],{"id":35645},"trying-to-serialize-unsupported-python-objects-directly","Trying to serialize unsupported Python objects directly",[14,35648,35649],{},"Some Python objects cannot be converted to JSON automatically.",[14,35651,35652,35653,35655],{},"For example, a ",[17,35654,9922],{}," will fail:",[45,35657,35659],{"className":47,"code":35658,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"numbers\": {1, 2, 3}}\njson_text = json.dumps(data)\n",[17,35660,35661,35667,35671,35701],{"__ignoreMap":50},[54,35662,35663,35665],{"class":56,"line":57},[54,35664,61],{"class":60},[54,35666,8212],{"class":64},[54,35668,35669],{"class":56,"line":68},[54,35670,72],{"emptyLinePlaceholder":71},[54,35672,35673,35675,35677,35679,35681,35683,35685,35687,35689,35691,35693,35695,35697,35699],{"class":56,"line":75},[54,35674,8221],{"class":64},[54,35676,82],{"class":81},[54,35678,8226],{"class":88},[54,35680,545],{"class":544},[54,35682,9956],{"class":548},[54,35684,545],{"class":544},[54,35686,147],{"class":88},[54,35688,8226],{"class":88},[54,35690,100],{"class":99},[54,35692,103],{"class":88},[54,35694,466],{"class":99},[54,35696,103],{"class":88},[54,35698,379],{"class":99},[54,35700,9975],{"class":88},[54,35702,35703,35705,35707,35709,35711,35713,35715,35717],{"class":56,"line":112},[54,35704,9553],{"class":64},[54,35706,82],{"class":81},[54,35708,9558],{"class":64},[54,35710,89],{"class":88},[54,35712,9563],{"class":92},[54,35714,96],{"class":88},[54,35716,8313],{"class":92},[54,35718,109],{"class":88},[14,35720,35721],{},"This raises an error because sets are not standard JSON data types.",[14,35723,35724],{},"One simple fix is to convert the set to a list first:",[45,35726,35728],{"className":47,"code":35727,"language":49,"meta":50,"style":50},"import json\n\ndata = {\"numbers\": list({1, 2, 3})}\njson_text = json.dumps(data)\n\nprint(json_text)\n",[17,35729,35730,35736,35740,35772,35790,35794],{"__ignoreMap":50},[54,35731,35732,35734],{"class":56,"line":57},[54,35733,61],{"class":60},[54,35735,8212],{"class":64},[54,35737,35738],{"class":56,"line":68},[54,35739,72],{"emptyLinePlaceholder":71},[54,35741,35742,35744,35746,35748,35750,35752,35754,35756,35758,35760,35762,35764,35766,35768,35770],{"class":56,"line":75},[54,35743,8221],{"class":64},[54,35745,82],{"class":81},[54,35747,8226],{"class":88},[54,35749,545],{"class":544},[54,35751,9956],{"class":548},[54,35753,545],{"class":544},[54,35755,147],{"class":88},[54,35757,10105],{"class":429},[54,35759,10108],{"class":88},[54,35761,100],{"class":99},[54,35763,103],{"class":88},[54,35765,466],{"class":99},[54,35767,103],{"class":88},[54,35769,379],{"class":99},[54,35771,10121],{"class":88},[54,35773,35774,35776,35778,35780,35782,35784,35786,35788],{"class":56,"line":112},[54,35775,9553],{"class":64},[54,35777,82],{"class":81},[54,35779,9558],{"class":64},[54,35781,89],{"class":88},[54,35783,9563],{"class":92},[54,35785,96],{"class":88},[54,35787,8313],{"class":92},[54,35789,109],{"class":88},[54,35791,35792],{"class":56,"line":887},[54,35793,72],{"emptyLinePlaceholder":71},[54,35795,35796,35798,35800,35802],{"class":56,"line":892},[54,35797,116],{"class":115},[54,35799,96],{"class":88},[54,35801,9578],{"class":92},[54,35803,109],{"class":88},[40,35805,32668],{"id":32667},[14,35807,35808],{},"This page is an overview, so it does not go deep into every detail.",[14,35810,35811],{},"It does not fully cover:",[149,35813,35814,35819,35822,35825],{},[152,35815,35816,35817,411],{},"Detailed behavior of each ",[17,35818,8385],{},[152,35820,35821],{},"Error handling for broken JSON",[152,35823,35824],{},"Advanced options such as custom encoders",[152,35826,35827],{},"Full API request workflows",[14,35829,35830,35831,89],{},"If you are working with API responses, see ",[192,35832,35834],{"href":35833},"\u002Fhow-to\u002Fhow-to-handle-api-responses-in-python\u002F","how to handle API responses in Python",[40,35836,35838],{"id":35837},"common-causes-of-json-problems","Common causes of json problems",[14,35840,35841],{},"If your code is not working, check for these common causes:",[149,35843,35844,35849,35854,35857,35860],{},[152,35845,5481,35846,35848],{},[17,35847,13451],{}," on a file object instead of a string",[152,35850,5481,35851,35853],{},[17,35852,13580],{}," on a plain string instead of a file object",[152,35855,35856],{},"Passing invalid JSON with single quotes",[152,35858,35859],{},"Trying to convert unsupported objects like sets without preprocessing",[152,35861,25918,35862,35864],{},[17,35863,35564],{}," returns a dictionary instead of a string",[14,35866,35867],{},"Helpful commands for quick testing:",[45,35869,35871],{"className":15497,"code":35870,"language":15499,"meta":50,"style":50},"python --version\npython -c \"import json; print(json.dumps({'a': 1}))\"\npython -c \"import json; print(type(json.loads('{\\\"a\\\": 1}')))\"\n",[17,35872,35873,35880,35893],{"__ignoreMap":50},[54,35874,35875,35877],{"class":56,"line":57},[54,35876,49],{"class":10811},[54,35878,35879],{"class":24323}," --version\n",[54,35881,35882,35884,35886,35888,35891],{"class":56,"line":68},[54,35883,49],{"class":10811},[54,35885,26050],{"class":24323},[54,35887,830],{"class":544},[54,35889,35890],{"class":548},"import json; print(json.dumps({'a': 1}))",[54,35892,5239],{"class":544},[54,35894,35895,35897,35899,35901,35904,35907,35909,35911,35914],{"class":56,"line":75},[54,35896,49],{"class":10811},[54,35898,26050],{"class":24323},[54,35900,830],{"class":544},[54,35902,35903],{"class":548},"import json; print(type(json.loads('{",[54,35905,35906],{"class":4870},"\\\"",[54,35908,192],{"class":548},[54,35910,35906],{"class":4870},[54,35912,35913],{"class":548},": 1}')))",[54,35915,5239],{"class":544},[14,35917,35918,35919,89],{},"If the JSON text is broken, you may also run into parsing errors. For general debugging help, see ",[192,35920,7422],{"href":7421},[40,35922,1419],{"id":1418},[602,35924,35926],{"id":35925},"is-the-json-module-built-into-python","Is the json module built into Python?",[14,35928,35929,35930,35932],{},"Yes. It is part of Python's standard library, so you can use ",[17,35931,13797],{}," without installing anything.",[602,35934,8075,35935,134,35937,3409],{"id":15573},[17,35936,13580],{},[17,35938,13451],{},[14,35940,35941,35943,35944,15585],{},[17,35942,13580],{}," reads JSON from a file object. ",[17,35945,13451],{},[602,35947,8075,35948,134,35950,3409],{"id":11181},[17,35949,8190],{},[17,35951,8371],{},[14,35953,35954,35956,35957,11190],{},[17,35955,8190],{}," writes JSON to a file object. ",[17,35958,8371],{},[602,35960,35962],{"id":35961},"can-json-store-every-python-object","Can JSON store every Python object?",[14,35964,35965],{},"No. JSON supports basic data types like objects, arrays, strings, numbers, booleans, and null.",[602,35967,35969],{"id":35968},"why-does-my-json-text-fail-to-load","Why does my JSON text fail to load?",[14,35971,35972],{},"A common reason is invalid JSON syntax, such as single quotes, missing commas, or missing double quotes around keys.",[40,35974,1510],{"id":1509},[149,35976,35977,35981,35985,35989,35993,35997,36001],{},[152,35978,35979],{},[192,35980,34107],{"href":15724},[152,35982,35983],{},[192,35984,34112],{"href":13465},[152,35986,35987],{},[192,35988,34117],{"href":11295},[152,35990,35991],{},[192,35992,11229],{"href":8368},[152,35994,35995],{},[192,35996,8141],{"href":8140},[152,35998,35999],{},[192,36000,11234],{"href":15670},[152,36002,36003],{},[192,36004,11239],{"href":35135},[1545,36006,36007],{},"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 .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 .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}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 .sseR_, html code.shiki .sseR_{--shiki-light:#9C3EDA;--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 .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .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":50,"searchDepth":68,"depth":68,"links":36009},[36010,36011,36012,36013,36014,36024,36025,36026,36030,36039,36040,36041,36048],{"id":3595,"depth":68,"text":3596},{"id":33716,"depth":68,"text":33717},{"id":33757,"depth":68,"text":33758},{"id":34046,"depth":68,"text":34047},{"id":19715,"depth":68,"text":19716,"children":36015},[36016,36018,36020,36022],{"id":34129,"depth":75,"text":36017},"Use json.loads()",{"id":34207,"depth":75,"text":36019},"Use json.load()",{"id":34293,"depth":75,"text":36021},"Use json.dumps()",{"id":34404,"depth":75,"text":36023},"Use json.dump()",{"id":34523,"depth":68,"text":34524},{"id":34743,"depth":68,"text":34744},{"id":35138,"depth":68,"text":35139,"children":36027},[36028,36029],{"id":35152,"depth":75,"text":35153},{"id":35289,"depth":75,"text":35290},{"id":35414,"depth":68,"text":35415,"children":36031},[36032,36034,36036,36037,36038],{"id":35423,"depth":75,"text":36033},"Mixing up load and loads",{"id":35542,"depth":75,"text":36035},"Mixing up dump and dumps",{"id":35568,"depth":75,"text":35569},{"id":35608,"depth":75,"text":35609},{"id":35645,"depth":75,"text":35646},{"id":32667,"depth":68,"text":32668},{"id":35837,"depth":68,"text":35838},{"id":1418,"depth":68,"text":1419,"children":36042},[36043,36044,36045,36046,36047],{"id":35925,"depth":75,"text":35926},{"id":15573,"depth":75,"text":15714},{"id":11181,"depth":75,"text":11182},{"id":35961,"depth":75,"text":35962},{"id":35968,"depth":75,"text":35969},{"id":1509,"depth":68,"text":1510},"Master python json module overview in our comprehensive Python beginner guide.",{},{"title":33502,"description":36049},"standard-library\u002Fpython-json-module-overview","eyj2Tuu_sWoPePHszxWieYpAZb7YnnehbjXv1mZ-sLw",{"id":36055,"title":36056,"body":36057,"description":37548,"extension":1577,"meta":37549,"navigation":71,"path":20700,"seo":37550,"stem":37551,"__hash__":37552},"content\u002Fstandard-library\u002Fpython-math-module-overview.md","Python math Module Overview",{"type":7,"value":36058,"toc":37511},[36059,36062,36067,36070,36073,36075,36159,36161,36184,36193,36197,36202,36204,36215,36218,36229,36232,36249,36253,36256,36267,36270,36302,36304,36312,36315,36338,36342,36348,36353,36358,36390,36392,36400,36406,36412,36417,36469,36471,36484,36490,36495,36546,36548,36561,36566,36571,36576,36612,36614,36623,36632,36638,36643,36679,36681,36690,36696,36702,36707,36710,36744,36746,36754,36758,36763,36766,36772,36777,36806,36812,36817,36847,36850,36854,36859,36862,36883,36889,36909,36911,36936,36938,36941,36952,37052,37054,37078,37080,37101,37105,37110,37114,37120,37143,37145,37177,37184,37192,37200,37229,37231,37263,37267,37270,37276,37310,37315,37322,37328,37333,37336,37378,37383,37387,37390,37393,37425,37427,37431,37437,37441,37447,37454,37463,37470,37476,37478,37508],[10,36060,36056],{"id":36061},"python-math-module-overview",[14,36063,28340,36064,36066],{},[17,36065,18295],{}," module is a built-in Python module for common math work.",[14,36068,36069],{},"It gives you extra functions and constants that are not available as basic language features. Beginners often use it for square roots, rounding, powers, and simple numeric calculations.",[14,36071,36072],{},"This page is an overview. It shows what the module is, how to import it, and which parts are most useful to start with.",[40,36074,3596],{"id":3595},[45,36076,36078],{"className":47,"code":36077,"language":49,"meta":50,"style":50},"import math\n\nprint(math.sqrt(25))\nprint(math.ceil(3.2))\nprint(math.floor(3.8))\nprint(math.pi)\n",[17,36079,36080,36086,36090,36108,36126,36144],{"__ignoreMap":50},[54,36081,36082,36084],{"class":56,"line":57},[54,36083,61],{"class":60},[54,36085,18340],{"class":64},[54,36087,36088],{"class":56,"line":68},[54,36089,72],{"emptyLinePlaceholder":71},[54,36091,36092,36094,36096,36098,36100,36102,36104,36106],{"class":56,"line":75},[54,36093,116],{"class":115},[54,36095,96],{"class":88},[54,36097,18295],{"class":92},[54,36099,89],{"class":88},[54,36101,21927],{"class":92},[54,36103,96],{"class":88},[54,36105,22330],{"class":99},[54,36107,382],{"class":88},[54,36109,36110,36112,36114,36116,36118,36120,36122,36124],{"class":56,"line":112},[54,36111,116],{"class":115},[54,36113,96],{"class":88},[54,36115,18295],{"class":92},[54,36117,89],{"class":88},[54,36119,18357],{"class":92},[54,36121,96],{"class":88},[54,36123,18362],{"class":99},[54,36125,382],{"class":88},[54,36127,36128,36130,36132,36134,36136,36138,36140,36142],{"class":56,"line":887},[54,36129,116],{"class":115},[54,36131,96],{"class":88},[54,36133,18295],{"class":92},[54,36135,89],{"class":88},[54,36137,18380],{"class":92},[54,36139,96],{"class":88},[54,36141,18385],{"class":99},[54,36143,382],{"class":88},[54,36145,36146,36148,36150,36152,36154,36157],{"class":56,"line":892},[54,36147,116],{"class":115},[54,36149,96],{"class":88},[54,36151,18295],{"class":92},[54,36153,89],{"class":88},[54,36155,36156],{"class":1693},"pi",[54,36158,109],{"class":88},[14,36160,14248],{},[45,36162,36164],{"className":47,"code":36163,"language":49,"meta":50,"style":50},"5.0\n4\n3\n3.141592653589793\n",[17,36165,36166,36170,36174,36179],{"__ignoreMap":50},[54,36167,36168],{"class":56,"line":57},[54,36169,23465],{"class":99},[54,36171,36172],{"class":56,"line":68},[54,36173,4510],{"class":99},[54,36175,36176],{"class":56,"line":75},[54,36177,36178],{"class":99},"3\n",[54,36180,36181],{"class":56,"line":112},[54,36182,36183],{"class":99},"3.141592653589793\n",[14,36185,126,36186,36188,36189,36192],{},[17,36187,20374],{}," first. Then call functions and constants with the ",[17,36190,36191],{},"math."," prefix.",[40,36194,36196],{"id":36195},"what-the-math-module-is","What the math module is",[14,36198,28340,36199,36201],{},[17,36200,18295],{}," module is part of Python’s standard library.",[14,36203,343],{},[149,36205,36206,36209,36212],{},[152,36207,36208],{},"You do not need to install it",[152,36210,36211],{},"It is included with Python",[152,36213,36214],{},"You can use it after importing it",[14,36216,36217],{},"The module gives you:",[149,36219,36220,36223,36226],{},[152,36221,36222],{},"Extra math functions",[152,36224,36225],{},"Useful numeric constants",[152,36227,36228],{},"A simple way to do common calculations",[14,36230,36231],{},"You will often use it for:",[149,36233,36234,36237,36240,36243,36246],{},[152,36235,36236],{},"Square roots",[152,36238,36239],{},"Rounding up or down",[152,36241,36242],{},"Powers",[152,36244,36245],{},"Trigonometry",[152,36247,36248],{},"Factorials",[40,36250,36252],{"id":36251},"how-to-import-the-math-module","How to import the math module",[14,36254,36255],{},"Import the module at the top of your file:",[45,36257,36259],{"className":47,"code":36258,"language":49,"meta":50,"style":50},"import math\n",[17,36260,36261],{"__ignoreMap":50},[54,36262,36263,36265],{"class":56,"line":57},[54,36264,61],{"class":60},[54,36266,18340],{"class":64},[14,36268,36269],{},"After that, use dot notation to access items inside the module:",[45,36271,36272],{"className":47,"code":22452,"language":49,"meta":50,"style":50},[17,36273,36274,36280,36284],{"__ignoreMap":50},[54,36275,36276,36278],{"class":56,"line":57},[54,36277,61],{"class":60},[54,36279,18340],{"class":64},[54,36281,36282],{"class":56,"line":68},[54,36283,72],{"emptyLinePlaceholder":71},[54,36285,36286,36288,36290,36292,36294,36296,36298,36300],{"class":56,"line":75},[54,36287,116],{"class":115},[54,36289,96],{"class":88},[54,36291,18295],{"class":92},[54,36293,89],{"class":88},[54,36295,21927],{"class":92},[54,36297,96],{"class":88},[54,36299,1125],{"class":99},[54,36301,382],{"class":88},[14,36303,449],{},[45,36305,36306],{"className":47,"code":22487,"language":49,"meta":50,"style":50},[17,36307,36308],{"__ignoreMap":50},[54,36309,36310],{"class":56,"line":57},[54,36311,22487],{"class":99},[14,36313,36314],{},"The important idea is:",[149,36316,36317,36322,36327],{},[152,36318,36319,36321],{},[17,36320,18295],{}," is the module name",[152,36323,36324,36326],{},[17,36325,21927],{}," is a function inside that module",[152,36328,36329,36331,36332,36334,36335,36337],{},[17,36330,23099],{}," means “use the ",[17,36333,21927],{}," function from the ",[17,36336,18295],{}," module”",[40,36339,36341],{"id":36340},"common-functions-beginners-use","Common functions beginners use",[14,36343,36344,36345,36347],{},"Here are some of the most common ",[17,36346,18295],{}," functions for beginners.",[602,36349,36351],{"id":36350},"mathsqrtx",[17,36352,23528],{},[14,36354,126,36355,36357],{},[17,36356,23528],{}," to find the square root of a number.",[45,36359,36360],{"className":47,"code":22613,"language":49,"meta":50,"style":50},[17,36361,36362,36368,36372],{"__ignoreMap":50},[54,36363,36364,36366],{"class":56,"line":57},[54,36365,61],{"class":60},[54,36367,18340],{"class":64},[54,36369,36370],{"class":56,"line":68},[54,36371,72],{"emptyLinePlaceholder":71},[54,36373,36374,36376,36378,36380,36382,36384,36386,36388],{"class":56,"line":75},[54,36375,116],{"class":115},[54,36377,96],{"class":88},[54,36379,18295],{"class":92},[54,36381,89],{"class":88},[54,36383,21927],{"class":92},[54,36385,96],{"class":88},[54,36387,20777],{"class":99},[54,36389,382],{"class":88},[14,36391,449],{},[45,36393,36394],{"className":47,"code":22648,"language":49,"meta":50,"style":50},[17,36395,36396],{"__ignoreMap":50},[54,36397,36398],{"class":56,"line":57},[54,36399,22648],{"class":99},[14,36401,36402,36403,89],{},"For a full guide, see ",[192,36404,36405],{"href":21075},"math.sqrt() explained",[602,36407,36409],{"id":36408},"mathceilx",[17,36410,36411],{},"math.ceil(x)",[14,36413,126,36414,36416],{},[17,36415,36411],{}," to round a number up to the next whole number.",[45,36418,36420],{"className":47,"code":36419,"language":49,"meta":50,"style":50},"import math\n\nprint(math.ceil(3.2))\nprint(math.ceil(7.0))\n",[17,36421,36422,36428,36432,36450],{"__ignoreMap":50},[54,36423,36424,36426],{"class":56,"line":57},[54,36425,61],{"class":60},[54,36427,18340],{"class":64},[54,36429,36430],{"class":56,"line":68},[54,36431,72],{"emptyLinePlaceholder":71},[54,36433,36434,36436,36438,36440,36442,36444,36446,36448],{"class":56,"line":75},[54,36435,116],{"class":115},[54,36437,96],{"class":88},[54,36439,18295],{"class":92},[54,36441,89],{"class":88},[54,36443,18357],{"class":92},[54,36445,96],{"class":88},[54,36447,18362],{"class":99},[54,36449,382],{"class":88},[54,36451,36452,36454,36456,36458,36460,36462,36464,36467],{"class":56,"line":112},[54,36453,116],{"class":115},[54,36455,96],{"class":88},[54,36457,18295],{"class":92},[54,36459,89],{"class":88},[54,36461,18357],{"class":92},[54,36463,96],{"class":88},[54,36465,36466],{"class":99},"7.0",[54,36468,382],{"class":88},[14,36470,449],{},[45,36472,36474],{"className":47,"code":36473,"language":49,"meta":50,"style":50},"4\n7\n",[17,36475,36476,36480],{"__ignoreMap":50},[54,36477,36478],{"class":56,"line":57},[54,36479,4510],{"class":99},[54,36481,36482],{"class":56,"line":68},[54,36483,308],{"class":99},[602,36485,36487],{"id":36486},"mathfloorx",[17,36488,36489],{},"math.floor(x)",[14,36491,126,36492,36494],{},[17,36493,36489],{}," to round a number down to the lower whole number.",[45,36496,36498],{"className":47,"code":36497,"language":49,"meta":50,"style":50},"import math\n\nprint(math.floor(3.8))\nprint(math.floor(7.0))\n",[17,36499,36500,36506,36510,36528],{"__ignoreMap":50},[54,36501,36502,36504],{"class":56,"line":57},[54,36503,61],{"class":60},[54,36505,18340],{"class":64},[54,36507,36508],{"class":56,"line":68},[54,36509,72],{"emptyLinePlaceholder":71},[54,36511,36512,36514,36516,36518,36520,36522,36524,36526],{"class":56,"line":75},[54,36513,116],{"class":115},[54,36515,96],{"class":88},[54,36517,18295],{"class":92},[54,36519,89],{"class":88},[54,36521,18380],{"class":92},[54,36523,96],{"class":88},[54,36525,18385],{"class":99},[54,36527,382],{"class":88},[54,36529,36530,36532,36534,36536,36538,36540,36542,36544],{"class":56,"line":112},[54,36531,116],{"class":115},[54,36533,96],{"class":88},[54,36535,18295],{"class":92},[54,36537,89],{"class":88},[54,36539,18380],{"class":92},[54,36541,96],{"class":88},[54,36543,36466],{"class":99},[54,36545,382],{"class":88},[14,36547,449],{},[45,36549,36551],{"className":47,"code":36550,"language":49,"meta":50,"style":50},"3\n7\n",[17,36552,36553,36557],{"__ignoreMap":50},[54,36554,36555],{"class":56,"line":57},[54,36556,36178],{"class":99},[54,36558,36559],{"class":56,"line":68},[54,36560,308],{"class":99},[14,36562,32369,36563,89],{},[192,36564,36565],{"href":20474},"math.ceil() and math.floor() explained",[602,36567,36569],{"id":36568},"mathpowx-y",[17,36570,20563],{},[14,36572,126,36573,36575],{},[17,36574,20563],{}," to raise a number to a power.",[45,36577,36578],{"className":47,"code":21350,"language":49,"meta":50,"style":50},[17,36579,36580,36586,36590],{"__ignoreMap":50},[54,36581,36582,36584],{"class":56,"line":57},[54,36583,61],{"class":60},[54,36585,18340],{"class":64},[54,36587,36588],{"class":56,"line":68},[54,36589,72],{"emptyLinePlaceholder":71},[54,36591,36592,36594,36596,36598,36600,36602,36604,36606,36608,36610],{"class":56,"line":75},[54,36593,116],{"class":115},[54,36595,96],{"class":88},[54,36597,18295],{"class":92},[54,36599,89],{"class":88},[54,36601,20535],{"class":92},[54,36603,96],{"class":88},[54,36605,396],{"class":99},[54,36607,103],{"class":88},[54,36609,379],{"class":99},[54,36611,382],{"class":88},[14,36613,449],{},[45,36615,36617],{"className":47,"code":36616,"language":49,"meta":50,"style":50},"8.0\n",[17,36618,36619],{"__ignoreMap":50},[54,36620,36621],{"class":56,"line":57},[54,36622,36616],{"class":99},[14,36624,36625,36626,36628,36629,89],{},"If you want to compare it with the ",[17,36627,20504],{}," operator, see ",[192,36630,36631],{"href":22269},"math.pow() explained",[602,36633,36635],{"id":36634},"mathfabsx",[17,36636,36637],{},"math.fabs(x)",[14,36639,126,36640,36642],{},[17,36641,36637],{}," to get the absolute value as a float.",[45,36644,36646],{"className":47,"code":36645,"language":49,"meta":50,"style":50},"import math\n\nprint(math.fabs(-12))\n",[17,36647,36648,36654,36658],{"__ignoreMap":50},[54,36649,36650,36652],{"class":56,"line":57},[54,36651,61],{"class":60},[54,36653,18340],{"class":64},[54,36655,36656],{"class":56,"line":68},[54,36657,72],{"emptyLinePlaceholder":71},[54,36659,36660,36662,36664,36666,36668,36671,36673,36675,36677],{"class":56,"line":75},[54,36661,116],{"class":115},[54,36663,96],{"class":88},[54,36665,18295],{"class":92},[54,36667,89],{"class":88},[54,36669,36670],{"class":92},"fabs",[54,36672,96],{"class":88},[54,36674,1467],{"class":81},[54,36676,33198],{"class":99},[54,36678,382],{"class":88},[14,36680,449],{},[45,36682,36684],{"className":47,"code":36683,"language":49,"meta":50,"style":50},"12.0\n",[17,36685,36686],{"__ignoreMap":50},[54,36687,36688],{"class":56,"line":57},[54,36689,36683],{"class":99},[14,36691,36692,36693,89],{},"If you want the normal built-in absolute value function, see ",[192,36694,22219],{"href":36695},"\u002Freference\u002Fpython-abs-function-explained\u002F",[602,36697,36699],{"id":36698},"mathfactorialn",[17,36700,36701],{},"math.factorial(n)",[14,36703,126,36704,36706],{},[17,36705,36701],{}," to calculate a factorial.",[14,36708,36709],{},"A factorial means multiplying a whole number by all smaller positive whole numbers down to 1.",[45,36711,36713],{"className":47,"code":36712,"language":49,"meta":50,"style":50},"import math\n\nprint(math.factorial(5))\n",[17,36714,36715,36721,36725],{"__ignoreMap":50},[54,36716,36717,36719],{"class":56,"line":57},[54,36718,61],{"class":60},[54,36720,18340],{"class":64},[54,36722,36723],{"class":56,"line":68},[54,36724,72],{"emptyLinePlaceholder":71},[54,36726,36727,36729,36731,36733,36735,36738,36740,36742],{"class":56,"line":75},[54,36728,116],{"class":115},[54,36730,96],{"class":88},[54,36732,18295],{"class":92},[54,36734,89],{"class":88},[54,36736,36737],{"class":92},"factorial",[54,36739,96],{"class":88},[54,36741,285],{"class":99},[54,36743,382],{"class":88},[14,36745,449],{},[45,36747,36748],{"className":47,"code":14758,"language":49,"meta":50,"style":50},[17,36749,36750],{"__ignoreMap":50},[54,36751,36752],{"class":56,"line":57},[54,36753,14758],{"class":99},[40,36755,36757],{"id":36756},"useful-constants","Useful constants",[14,36759,28340,36760,36762],{},[17,36761,18295],{}," module also includes constants.",[14,36764,36765],{},"Constants are values stored in the module that do not change.",[602,36767,36769],{"id":36768},"mathpi",[17,36770,36771],{},"math.pi",[14,36773,126,36774,36776],{},[17,36775,36771],{}," when you need the value of pi.",[45,36778,36780],{"className":47,"code":36779,"language":49,"meta":50,"style":50},"import math\n\nprint(math.pi)\n",[17,36781,36782,36788,36792],{"__ignoreMap":50},[54,36783,36784,36786],{"class":56,"line":57},[54,36785,61],{"class":60},[54,36787,18340],{"class":64},[54,36789,36790],{"class":56,"line":68},[54,36791,72],{"emptyLinePlaceholder":71},[54,36793,36794,36796,36798,36800,36802,36804],{"class":56,"line":75},[54,36795,116],{"class":115},[54,36797,96],{"class":88},[54,36799,18295],{"class":92},[54,36801,89],{"class":88},[54,36803,36156],{"class":1693},[54,36805,109],{"class":88},[602,36807,36809],{"id":36808},"mathe",[17,36810,36811],{},"math.e",[14,36813,126,36814,36816],{},[17,36815,36811],{}," for Euler’s number.",[45,36818,36820],{"className":47,"code":36819,"language":49,"meta":50,"style":50},"import math\n\nprint(math.e)\n",[17,36821,36822,36828,36832],{"__ignoreMap":50},[54,36823,36824,36826],{"class":56,"line":57},[54,36825,61],{"class":60},[54,36827,18340],{"class":64},[54,36829,36830],{"class":56,"line":68},[54,36831,72],{"emptyLinePlaceholder":71},[54,36833,36834,36836,36838,36840,36842,36845],{"class":56,"line":75},[54,36835,116],{"class":115},[54,36837,96],{"class":88},[54,36839,18295],{"class":92},[54,36841,89],{"class":88},[54,36843,36844],{"class":1693},"e",[54,36846,109],{"class":88},[14,36848,36849],{},"These constants are useful because they give you accurate built-in values instead of making you type them yourself.",[40,36851,36853],{"id":36852},"when-to-use-math-instead-of-built-in-functions","When to use math instead of built-in functions",[14,36855,36856,36857,15765],{},"Python already has some built-in numeric tools, so you do not always need the ",[17,36858,18295],{},[14,36860,36861],{},"Built-in functions are often enough for simple tasks, such as:",[149,36863,36864,36869,36873,36878],{},[152,36865,36866],{},[17,36867,36868],{},"abs()",[152,36870,36871],{},[17,36872,18305],{},[152,36874,36875],{},[17,36876,36877],{},"min()",[152,36879,36880],{},[17,36881,36882],{},"max()",[14,36884,36885,36886,36888],{},"Use the ",[17,36887,18295],{}," module when:",[149,36890,36891,36894,36901,36906],{},[152,36892,36893],{},"You need square roots",[152,36895,36896,36897,5507,36899],{},"You need special rounding behavior like ",[17,36898,18872],{},[17,36900,18878],{},[152,36902,36903,36904],{},"You need constants like ",[17,36905,36156],{},[152,36907,36908],{},"You need functions that are not built into Python directly",[14,36910,415],{},[149,36912,36913,36919,36926,36931],{},[152,36914,126,36915,36918],{},[17,36916,36917],{},"math.sqrt(25)"," for square roots",[152,36920,126,36921,36925],{},[192,36922,36923],{"href":18323},[17,36924,18305],{}," for normal rounding",[152,36927,126,36928,36930],{},[17,36929,36868],{}," for a simple absolute value",[152,36932,126,36933,36935],{},[17,36934,18295],{}," when you need more specialized math tools",[40,36937,493],{"id":492},[14,36939,36940],{},"This example shows:",[149,36942,36943,36946,36949],{},[152,36944,36945],{},"Importing the module",[152,36947,36948],{},"Using one function",[152,36950,36951],{},"Using one constant",[45,36953,36955],{"className":47,"code":36954,"language":49,"meta":50,"style":50},"import math\n\nradius = 3\narea = math.pi * math.pow(radius, 2)\n\nprint(\"Radius:\", radius)\nprint(\"Area:\", area)\n",[17,36956,36957,36963,36967,36976,37008,37012,37032],{"__ignoreMap":50},[54,36958,36959,36961],{"class":56,"line":57},[54,36960,61],{"class":60},[54,36962,18340],{"class":64},[54,36964,36965],{"class":56,"line":68},[54,36966,72],{"emptyLinePlaceholder":71},[54,36968,36969,36972,36974],{"class":56,"line":75},[54,36970,36971],{"class":64},"radius ",[54,36973,82],{"class":81},[54,36975,9230],{"class":99},[54,36977,36978,36981,36983,36985,36987,36989,36991,36993,36995,36997,36999,37002,37004,37006],{"class":56,"line":112},[54,36979,36980],{"class":64},"area ",[54,36982,82],{"class":81},[54,36984,19445],{"class":64},[54,36986,89],{"class":88},[54,36988,36156],{"class":1693},[54,36990,11502],{"class":81},[54,36992,19445],{"class":64},[54,36994,89],{"class":88},[54,36996,20535],{"class":92},[54,36998,96],{"class":88},[54,37000,37001],{"class":92},"radius",[54,37003,103],{"class":88},[54,37005,466],{"class":99},[54,37007,109],{"class":88},[54,37009,37010],{"class":56,"line":887},[54,37011,72],{"emptyLinePlaceholder":71},[54,37013,37014,37016,37018,37020,37023,37025,37027,37030],{"class":56,"line":892},[54,37015,116],{"class":115},[54,37017,96],{"class":88},[54,37019,545],{"class":544},[54,37021,37022],{"class":548},"Radius:",[54,37024,545],{"class":544},[54,37026,103],{"class":88},[54,37028,37029],{"class":92}," radius",[54,37031,109],{"class":88},[54,37033,37034,37036,37038,37040,37043,37045,37047,37050],{"class":56,"line":913},[54,37035,116],{"class":115},[54,37037,96],{"class":88},[54,37039,545],{"class":544},[54,37041,37042],{"class":548},"Area:",[54,37044,545],{"class":544},[54,37046,103],{"class":88},[54,37048,37049],{"class":92}," area",[54,37051,109],{"class":88},[14,37053,14248],{},[45,37055,37057],{"className":47,"code":37056,"language":49,"meta":50,"style":50},"Radius: 3\nArea: 28.274333882308138\n",[17,37058,37059,37068],{"__ignoreMap":50},[54,37060,37061,37064,37066],{"class":56,"line":57},[54,37062,37063],{"class":64},"Radius",[54,37065,147],{"class":88},[54,37067,9230],{"class":99},[54,37069,37070,37073,37075],{"class":56,"line":68},[54,37071,37072],{"class":64},"Area",[54,37074,147],{"class":88},[54,37076,37077],{"class":99}," 28.274333882308138\n",[14,37079,22568],{},[149,37081,37082,37087,37092,37098],{},[152,37083,37084,37086],{},[17,37085,20374],{}," makes the module available",[152,37088,37089,37091],{},[17,37090,36771],{}," gives the value of pi",[152,37093,37094,37097],{},[17,37095,37096],{},"math.pow(radius, 2)"," squares the radius",[152,37099,37100],{},"The result is used to calculate the area of a circle",[40,37102,37104],{"id":37103},"common-errors-and-beginner-problems","Common errors and beginner problems",[14,37106,37107,37108,15765],{},"Here are some common problems when using the ",[17,37109,18295],{},[602,37111,17008,37112],{"id":19941},[17,37113,18295],{},[14,37115,985,37116,21309,37118,21312],{},[17,37117,988],{},[17,37119,18295],{},[45,37121,37123],{"className":47,"code":37122,"language":49,"meta":50,"style":50},"print(math.sqrt(16))\n",[17,37124,37125],{"__ignoreMap":50},[54,37126,37127,37129,37131,37133,37135,37137,37139,37141],{"class":56,"line":57},[54,37128,116],{"class":115},[54,37130,96],{"class":88},[54,37132,18295],{"class":92},[54,37134,89],{"class":88},[54,37136,21927],{"class":92},[54,37138,96],{"class":88},[54,37140,20777],{"class":99},[54,37142,382],{"class":88},[14,37144,1031],{},[45,37146,37147],{"className":47,"code":22613,"language":49,"meta":50,"style":50},[17,37148,37149,37155,37159],{"__ignoreMap":50},[54,37150,37151,37153],{"class":56,"line":57},[54,37152,61],{"class":60},[54,37154,18340],{"class":64},[54,37156,37157],{"class":56,"line":68},[54,37158,72],{"emptyLinePlaceholder":71},[54,37160,37161,37163,37165,37167,37169,37171,37173,37175],{"class":56,"line":75},[54,37162,116],{"class":115},[54,37164,96],{"class":88},[54,37166,18295],{"class":92},[54,37168,89],{"class":88},[54,37170,21927],{"class":92},[54,37172,96],{"class":88},[54,37174,20777],{"class":99},[54,37176,382],{"class":88},[14,37178,37179,37180,89],{},"If this happens, read ",[192,37181,37183],{"href":37182},"\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes\u002F","NameError in Python: causes and fixes",[602,37185,5481,37187,37189,37190],{"id":37186},"using-sqrt-without-math",[17,37188,23541],{}," without ",[17,37191,36191],{},[14,37193,37194,37195,37197,37198,89],{},"This also causes a ",[17,37196,988],{}," if you imported the module with ",[17,37199,20374],{},[45,37201,37203],{"className":47,"code":37202,"language":49,"meta":50,"style":50},"import math\n\nprint(sqrt(16))\n",[17,37204,37205,37211,37215],{"__ignoreMap":50},[54,37206,37207,37209],{"class":56,"line":57},[54,37208,61],{"class":60},[54,37210,18340],{"class":64},[54,37212,37213],{"class":56,"line":68},[54,37214,72],{"emptyLinePlaceholder":71},[54,37216,37217,37219,37221,37223,37225,37227],{"class":56,"line":75},[54,37218,116],{"class":115},[54,37220,96],{"class":88},[54,37222,21927],{"class":92},[54,37224,96],{"class":88},[54,37226,20777],{"class":99},[54,37228,382],{"class":88},[14,37230,1031],{},[45,37232,37233],{"className":47,"code":22613,"language":49,"meta":50,"style":50},[17,37234,37235,37241,37245],{"__ignoreMap":50},[54,37236,37237,37239],{"class":56,"line":57},[54,37238,61],{"class":60},[54,37240,18340],{"class":64},[54,37242,37243],{"class":56,"line":68},[54,37244,72],{"emptyLinePlaceholder":71},[54,37246,37247,37249,37251,37253,37255,37257,37259,37261],{"class":56,"line":75},[54,37248,116],{"class":115},[54,37250,96],{"class":88},[54,37252,18295],{"class":92},[54,37254,89],{"class":88},[54,37256,21927],{"class":92},[54,37258,96],{"class":88},[54,37260,20777],{"class":99},[54,37262,382],{"class":88},[602,37264,37266],{"id":37265},"passing-invalid-values","Passing invalid values",[14,37268,37269],{},"Some functions only accept certain values.",[14,37271,37272,37273,37275],{},"For example, ",[17,37274,21078],{}," does not accept negative numbers in normal real-number math.",[45,37277,37278],{"className":47,"code":22832,"language":49,"meta":50,"style":50},[17,37279,37280,37286,37290],{"__ignoreMap":50},[54,37281,37282,37284],{"class":56,"line":57},[54,37283,61],{"class":60},[54,37285,18340],{"class":64},[54,37287,37288],{"class":56,"line":68},[54,37289,72],{"emptyLinePlaceholder":71},[54,37291,37292,37294,37296,37298,37300,37302,37304,37306,37308],{"class":56,"line":75},[54,37293,116],{"class":115},[54,37295,96],{"class":88},[54,37297,18295],{"class":92},[54,37299,89],{"class":88},[54,37301,21927],{"class":92},[54,37303,96],{"class":88},[54,37305,1467],{"class":81},[54,37307,100],{"class":99},[54,37309,382],{"class":88},[14,37311,37312,37313,89],{},"This raises a ",[17,37314,1306],{},[14,37316,37317,37318,89],{},"If you see this problem, read ",[192,37319,37321],{"href":37320},"\u002Ferrors\u002Fvalueerror-math-domain-error-fix\u002F","ValueError: math domain error fix",[602,37323,37325,37326],{"id":37324},"shadowing-the-name-math","Shadowing the name ",[17,37327,18295],{},[14,37329,1403,37330,37332],{},[17,37331,18295],{}," as your own variable name.",[14,37334,37335],{},"This can overwrite the module name in your code.",[45,37337,37339],{"className":47,"code":37338,"language":49,"meta":50,"style":50},"import math\n\nmath = 10\nprint(math.sqrt(16))\n",[17,37340,37341,37347,37351,37360],{"__ignoreMap":50},[54,37342,37343,37345],{"class":56,"line":57},[54,37344,61],{"class":60},[54,37346,18340],{"class":64},[54,37348,37349],{"class":56,"line":68},[54,37350,72],{"emptyLinePlaceholder":71},[54,37352,37353,37356,37358],{"class":56,"line":75},[54,37354,37355],{"class":64},"math ",[54,37357,82],{"class":81},[54,37359,19431],{"class":99},[54,37361,37362,37364,37366,37368,37370,37372,37374,37376],{"class":56,"line":112},[54,37363,116],{"class":115},[54,37365,96],{"class":88},[54,37367,18295],{"class":92},[54,37369,89],{"class":88},[54,37371,21927],{"class":92},[54,37373,96],{"class":88},[54,37375,20777],{"class":99},[54,37377,382],{"class":88},[14,37379,10357,37380,37382],{},[17,37381,18295],{}," is now an integer, not the module.",[40,37384,37386],{"id":37385},"what-to-learn-next","What to learn next",[14,37388,37389],{},"After this overview, the best next step is to learn one function at a time.",[14,37391,37392],{},"Good next topics include:",[149,37394,37395,37399,37406,37411,37418],{},[152,37396,37397,36918],{},[17,37398,21078],{},[152,37400,37401,134,37403,37405],{},[17,37402,18265],{},[17,37404,18268],{}," for rounding",[152,37407,37408,37410],{},[17,37409,20490],{}," for exponent calculations",[152,37412,37413,37414,134,37416],{},"Built-in numeric functions like ",[17,37415,36868],{},[17,37417,18305],{},[152,37419,37420,37421,5507,37423],{},"Error pages if your code raises ",[17,37422,988],{},[17,37424,1306],{},[40,37426,1419],{"id":1418},[602,37428,37430],{"id":37429},"do-i-need-to-install-the-math-module","Do I need to install the math module?",[14,37432,37433,37434,37436],{},"No. The ",[17,37435,18295],{}," module comes with Python, so you only need to import it.",[602,37438,37440],{"id":37439},"why-does-python-say-math-is-not-defined","Why does Python say math is not defined?",[14,37442,37443,37444,37446],{},"You probably forgot to import ",[17,37445,18295],{},", or you used the name before importing it.",[602,37448,8075,37450,134,37452,3409],{"id":37449},"what-is-the-difference-between-mathpow-and",[17,37451,20490],{},[17,37453,20504],{},[14,37455,37456,37457,37459,37460,37462],{},"Both do exponent calculations, but ",[17,37458,20504],{}," is an operator and ",[17,37461,20490],{}," is a function that returns a float.",[602,37464,37466,37467,37469],{"id":37465},"can-i-use-mathsqrt-on-negative-numbers","Can I use ",[17,37468,21078],{}," on negative numbers?",[14,37471,37472,37473,37475],{},"No for normal real-number math. It raises a ",[17,37474,1306],{},". Complex numbers use different tools.",[40,37477,1510],{"id":1509},[149,37479,37480,37484,37488,37492,37496,37500,37504],{},[152,37481,37482],{},[192,37483,36405],{"href":21075},[152,37485,37486],{},[192,37487,36565],{"href":20474},[152,37489,37490],{},[192,37491,36631],{"href":22269},[152,37493,37494],{},[192,37495,22219],{"href":36695},[152,37497,37498],{},[192,37499,22134],{"href":18323},[152,37501,37502],{},[192,37503,37183],{"href":37182},[152,37505,37506],{},[192,37507,37321],{"href":37320},[1545,37509,37510],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .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}",{"title":50,"searchDepth":68,"depth":68,"links":37512},[37513,37514,37515,37516,37524,37528,37529,37530,37538,37539,37547],{"id":3595,"depth":68,"text":3596},{"id":36195,"depth":68,"text":36196},{"id":36251,"depth":68,"text":36252},{"id":36340,"depth":68,"text":36341,"children":37517},[37518,37519,37520,37521,37522,37523],{"id":36350,"depth":75,"text":23528},{"id":36408,"depth":75,"text":36411},{"id":36486,"depth":75,"text":36489},{"id":36568,"depth":75,"text":20563},{"id":36634,"depth":75,"text":36637},{"id":36698,"depth":75,"text":36701},{"id":36756,"depth":68,"text":36757,"children":37525},[37526,37527],{"id":36768,"depth":75,"text":36771},{"id":36808,"depth":75,"text":36811},{"id":36852,"depth":68,"text":36853},{"id":492,"depth":68,"text":493},{"id":37103,"depth":68,"text":37104,"children":37531},[37532,37533,37535,37536],{"id":19941,"depth":75,"text":20456},{"id":37186,"depth":75,"text":37534},"Using sqrt() without math.",{"id":37265,"depth":75,"text":37266},{"id":37324,"depth":75,"text":37537},"Shadowing the name math",{"id":37385,"depth":68,"text":37386},{"id":1418,"depth":68,"text":1419,"children":37540},[37541,37542,37543,37545],{"id":37429,"depth":75,"text":37430},{"id":37439,"depth":75,"text":37440},{"id":37449,"depth":75,"text":37544},"What is the difference between math.pow() and **?",{"id":37465,"depth":75,"text":37546},"Can I use math.sqrt() on negative numbers?",{"id":1509,"depth":68,"text":1510},"Master python math module overview in our comprehensive Python beginner guide.",{},{"title":36056,"description":37548},"standard-library\u002Fpython-math-module-overview","2iiu4A4n4BWf7MnlpZ8CBpd4aa7Rxcbl3QLAQSlwyZ8",{"id":37554,"title":37555,"body":37556,"description":38943,"extension":1577,"meta":38944,"navigation":71,"path":23866,"seo":38945,"stem":38946,"__hash__":38947},"content\u002Fstandard-library\u002Fpython-os-module-overview.md","Python os Module Overview",{"type":7,"value":37557,"toc":38906},[37558,37561,37566,37569,37575,37577,37620,37623,37636,37640,37645,37648,37665,37670,37681,37685,37691,37711,37713,37736,37740,37743,37748,37751,37789,37794,37799,37802,37841,37844,37849,37854,37857,37903,37906,37912,37917,37920,37979,37982,37986,37991,37994,38035,38040,38042,38048,38140,38142,38163,38165,38237,38240,38244,38251,38255,38261,38278,38283,38288,38298,38301,38303,38407,38412,38417,38421,38427,38430,38441,38443,38462,38464,38469,38473,38475,38494,38496,38525,38529,38532,38550,38553,38621,38627,38632,38670,38673,38762,38766,38771,38774,38802,38806,38809,38812,38814,38818,38821,38825,38828,38832,38840,38844,38854,38856,38862,38864,38903],[10,37559,37555],{"id":37560},"python-os-module-overview",[14,37562,28340,37563,37565],{},[17,37564,23720],{}," module helps Python work with your operating system. Beginners often use it for files, folders, paths, and environment information.",[14,37567,37568],{},"It is part of Python’s standard library, so you do not need to install anything.",[14,37570,37571,37572,37574],{},"This page is an overview of what ",[17,37573,23720],{}," does and when to use it. It is not a full guide to every function in the module.",[40,37576,3596],{"id":3595},[45,37578,37580],{"className":47,"code":37579,"language":49,"meta":50,"style":50},"import os\n\nprint(os.getcwd())\nprint(os.listdir())\n",[17,37581,37582,37588,37592,37606],{"__ignoreMap":50},[54,37583,37584,37586],{"class":56,"line":57},[54,37585,61],{"class":60},[54,37587,23707],{"class":64},[54,37589,37590],{"class":56,"line":68},[54,37591,72],{"emptyLinePlaceholder":71},[54,37593,37594,37596,37598,37600,37602,37604],{"class":56,"line":75},[54,37595,116],{"class":115},[54,37597,96],{"class":88},[54,37599,23720],{"class":92},[54,37601,89],{"class":88},[54,37603,23725],{"class":92},[54,37605,1950],{"class":88},[54,37607,37608,37610,37612,37614,37616,37618],{"class":56,"line":112},[54,37609,116],{"class":115},[54,37611,96],{"class":88},[54,37613,23720],{"class":92},[54,37615,89],{"class":88},[54,37617,24433],{"class":92},[54,37619,1950],{"class":88},[14,37621,37622],{},"This quick example:",[149,37624,37625,37630,37633],{},[152,37626,37627,37628],{},"imports ",[17,37629,23720],{},[152,37631,37632],{},"shows the current working directory",[152,37634,37635],{},"lists files and folders in that directory",[40,37637,37639],{"id":37638},"what-the-os-module-is","What the os module is",[14,37641,28340,37642,37644],{},[17,37643,23720],{}," module lets your Python code interact with the operating system.",[14,37646,37647],{},"You will often see it used for tasks like:",[149,37649,37650,37653,37656,37659,37662],{},[152,37651,37652],{},"finding the current folder",[152,37654,37655],{},"listing files and directories",[152,37657,37658],{},"changing directories",[152,37660,37661],{},"checking whether a path exists",[152,37663,37664],{},"working with environment variables",[14,37666,3412,37667,37669],{},[17,37668,23720],{}," is built into Python, you can use it right away:",[45,37671,37673],{"className":47,"code":37672,"language":49,"meta":50,"style":50},"import os\n",[17,37674,37675],{"__ignoreMap":50},[54,37676,37677,37679],{"class":56,"line":57},[54,37678,61],{"class":60},[54,37680,23707],{"class":64},[40,37682,37684],{"id":37683},"what-beginners-use-os-for","What beginners use os for",[14,37686,37687,37688,37690],{},"Beginners usually use ",[17,37689,23720],{}," for a few common tasks:",[149,37692,37693,37696,37699,37702,37705,37708],{},[152,37694,37695],{},"Get the current working directory",[152,37697,37698],{},"List files and folders",[152,37700,37701],{},"Change the current directory",[152,37703,37704],{},"Check whether a path exists",[152,37706,37707],{},"Join path parts safely",[152,37709,37710],{},"Access environment variables",[14,37712,415],{},[149,37714,37715,37722,37729],{},[152,37716,126,37717,37721],{},[192,37718,37719],{"href":23871},[17,37720,23766],{}," to see where your script is currently working.",[152,37723,126,37724,37728],{},[192,37725,37726],{"href":27930},[17,37727,26188],{}," to see names inside a folder.",[152,37730,126,37731,37735],{},[192,37732,37733],{"href":24853},[17,37734,24667],{}," to check whether a file or folder is there.",[40,37737,37739],{"id":37738},"commonly-used-os-functions","Commonly used os functions",[14,37741,37742],{},"Here are some of the most common functions beginners use.",[602,37744,37746],{"id":37745},"osgetcwd",[17,37747,23766],{},[14,37749,37750],{},"Returns the current working directory.",[45,37752,37753],{"className":47,"code":25143,"language":49,"meta":50,"style":50},[17,37754,37755,37761,37765,37779],{"__ignoreMap":50},[54,37756,37757,37759],{"class":56,"line":57},[54,37758,61],{"class":60},[54,37760,23707],{"class":64},[54,37762,37763],{"class":56,"line":68},[54,37764,72],{"emptyLinePlaceholder":71},[54,37766,37767,37769,37771,37773,37775,37777],{"class":56,"line":75},[54,37768,25160],{"class":64},[54,37770,82],{"class":81},[54,37772,24000],{"class":64},[54,37774,89],{"class":88},[54,37776,23725],{"class":92},[54,37778,1670],{"class":88},[54,37780,37781,37783,37785,37787],{"class":56,"line":112},[54,37782,116],{"class":115},[54,37784,96],{"class":88},[54,37786,25179],{"class":92},[54,37788,109],{"class":88},[14,37790,37791,37792,89],{},"If you want a full beginner explanation, see ",[192,37793,28788],{"href":23871},[602,37795,37797],{"id":37796},"oslistdir",[17,37798,26188],{},[14,37800,37801],{},"Returns the names inside a directory.",[45,37803,37805],{"className":47,"code":37804,"language":49,"meta":50,"style":50},"import os\n\nitems = os.listdir()\nprint(items)\n",[17,37806,37807,37813,37817,37831],{"__ignoreMap":50},[54,37808,37809,37811],{"class":56,"line":57},[54,37810,61],{"class":60},[54,37812,23707],{"class":64},[54,37814,37815],{"class":56,"line":68},[54,37816,72],{"emptyLinePlaceholder":71},[54,37818,37819,37821,37823,37825,37827,37829],{"class":56,"line":75},[54,37820,17899],{"class":64},[54,37822,82],{"class":81},[54,37824,24000],{"class":64},[54,37826,89],{"class":88},[54,37828,24433],{"class":92},[54,37830,1670],{"class":88},[54,37832,37833,37835,37837,37839],{"class":56,"line":112},[54,37834,116],{"class":115},[54,37836,96],{"class":88},[54,37838,10388],{"class":92},[54,37840,109],{"class":88},[14,37842,37843],{},"This usually returns names only, not full paths.",[14,37845,15021,37846,89],{},[192,37847,37848],{"href":27930},"os.listdir() explained",[602,37850,37852],{"id":37851},"oschdir",[17,37853,23669],{},[14,37855,37856],{},"Changes the current working directory.",[45,37858,37859],{"className":47,"code":24159,"language":49,"meta":50,"style":50},[17,37860,37861,37867,37871,37889],{"__ignoreMap":50},[54,37862,37863,37865],{"class":56,"line":57},[54,37864,61],{"class":60},[54,37866,23707],{"class":64},[54,37868,37869],{"class":56,"line":68},[54,37870,72],{"emptyLinePlaceholder":71},[54,37872,37873,37875,37877,37879,37881,37883,37885,37887],{"class":56,"line":75},[54,37874,23720],{"class":64},[54,37876,89],{"class":88},[54,37878,23736],{"class":92},[54,37880,96],{"class":88},[54,37882,545],{"class":544},[54,37884,23743],{"class":548},[54,37886,545],{"class":544},[54,37888,109],{"class":88},[54,37890,37891,37893,37895,37897,37899,37901],{"class":56,"line":112},[54,37892,116],{"class":115},[54,37894,96],{"class":88},[54,37896,23720],{"class":92},[54,37898,89],{"class":88},[54,37900,23725],{"class":92},[54,37902,1950],{"class":88},[14,37904,37905],{},"Be careful with this. After changing directories, relative paths will point somewhere else.",[14,37907,37908,37909,89],{},"For syntax and examples, see ",[192,37910,37911],{"href":25116},"os.chdir() explained",[602,37913,37915],{"id":37914},"ospathjoin",[17,37916,25760],{},[14,37918,37919],{},"Builds a path from multiple parts safely.",[45,37921,37923],{"className":47,"code":37922,"language":49,"meta":50,"style":50},"import os\n\nfile_path = os.path.join(\"projects\", \"notes.txt\")\nprint(file_path)\n",[17,37924,37925,37931,37935,37969],{"__ignoreMap":50},[54,37926,37927,37929],{"class":56,"line":57},[54,37928,61],{"class":60},[54,37930,23707],{"class":64},[54,37932,37933],{"class":56,"line":68},[54,37934,72],{"emptyLinePlaceholder":71},[54,37936,37937,37939,37941,37943,37945,37947,37949,37951,37953,37955,37957,37959,37961,37963,37965,37967],{"class":56,"line":75},[54,37938,28257],{"class":64},[54,37940,82],{"class":81},[54,37942,24000],{"class":64},[54,37944,89],{"class":88},[54,37946,23896],{"class":1693},[54,37948,89],{"class":88},[54,37950,25725],{"class":92},[54,37952,96],{"class":88},[54,37954,545],{"class":544},[54,37956,25576],{"class":548},[54,37958,545],{"class":544},[54,37960,103],{"class":88},[54,37962,830],{"class":544},[54,37964,24577],{"class":548},[54,37966,545],{"class":544},[54,37968,109],{"class":88},[54,37970,37971,37973,37975,37977],{"class":56,"line":112},[54,37972,116],{"class":115},[54,37974,96],{"class":88},[54,37976,28288],{"class":92},[54,37978,109],{"class":88},[14,37980,37981],{},"This is better than joining path parts by hand with slashes.",[14,37983,13462,37984,89],{},[192,37985,28666],{"href":25757},[602,37987,37989],{"id":37988},"ospathexists",[17,37990,24667],{},[14,37992,37993],{},"Checks whether a file or folder exists.",[45,37995,37997],{"className":47,"code":37996,"language":49,"meta":50,"style":50},"import os\n\nprint(os.path.exists(\"notes.txt\"))\n",[17,37998,37999,38005,38009],{"__ignoreMap":50},[54,38000,38001,38003],{"class":56,"line":57},[54,38002,61],{"class":60},[54,38004,23707],{"class":64},[54,38006,38007],{"class":56,"line":68},[54,38008,72],{"emptyLinePlaceholder":71},[54,38010,38011,38013,38015,38017,38019,38021,38023,38025,38027,38029,38031,38033],{"class":56,"line":75},[54,38012,116],{"class":115},[54,38014,96],{"class":88},[54,38016,23720],{"class":92},[54,38018,89],{"class":88},[54,38020,23896],{"class":1693},[54,38022,89],{"class":88},[54,38024,24757],{"class":92},[54,38026,96],{"class":88},[54,38028,545],{"class":544},[54,38030,24577],{"class":548},[54,38032,545],{"class":544},[54,38034,382],{"class":88},[14,38036,38037,38038,89],{},"If you want to solve that task step by step, read ",[192,38039,28378],{"href":25082},[40,38041,2258],{"id":2257},[14,38043,38044,38045,38047],{},"Here is a short runnable example that imports ",[17,38046,23720],{},", prints the current folder, and lists the names in that folder.",[45,38049,38051],{"className":47,"code":38050,"language":49,"meta":50,"style":50},"import os\n\nprint(\"Current folder:\")\nprint(os.getcwd())\n\nprint(\"\\nNames in this folder:\")\nfor name in os.listdir():\n    print(name)\n",[17,38052,38053,38059,38063,38077,38091,38095,38113,38130],{"__ignoreMap":50},[54,38054,38055,38057],{"class":56,"line":57},[54,38056,61],{"class":60},[54,38058,23707],{"class":64},[54,38060,38061],{"class":56,"line":68},[54,38062,72],{"emptyLinePlaceholder":71},[54,38064,38065,38067,38069,38071,38073,38075],{"class":56,"line":75},[54,38066,116],{"class":115},[54,38068,96],{"class":88},[54,38070,545],{"class":544},[54,38072,24719],{"class":548},[54,38074,545],{"class":544},[54,38076,109],{"class":88},[54,38078,38079,38081,38083,38085,38087,38089],{"class":56,"line":112},[54,38080,116],{"class":115},[54,38082,96],{"class":88},[54,38084,23720],{"class":92},[54,38086,89],{"class":88},[54,38088,23725],{"class":92},[54,38090,1950],{"class":88},[54,38092,38093],{"class":56,"line":887},[54,38094,72],{"emptyLinePlaceholder":71},[54,38096,38097,38099,38101,38103,38106,38109,38111],{"class":56,"line":892},[54,38098,116],{"class":115},[54,38100,96],{"class":88},[54,38102,545],{"class":544},[54,38104,38105],{"class":4870},"\\n",[54,38107,38108],{"class":548},"Names in this folder:",[54,38110,545],{"class":544},[54,38112,109],{"class":88},[54,38114,38115,38117,38119,38121,38123,38125,38127],{"class":56,"line":913},[54,38116,26859],{"class":60},[54,38118,26862],{"class":64},[54,38120,26865],{"class":60},[54,38122,24000],{"class":64},[54,38124,89],{"class":88},[54,38126,24433],{"class":92},[54,38128,38129],{"class":88},"():\n",[54,38131,38132,38134,38136,38138],{"class":56,"line":2121},[54,38133,7606],{"class":115},[54,38135,96],{"class":88},[54,38137,8231],{"class":92},[54,38139,109],{"class":88},[602,38141,8670],{"id":8669},[149,38143,38144,38148,38153,38158],{},[152,38145,38146,37086],{},[17,38147,25272],{},[152,38149,38150,38152],{},[17,38151,23766],{}," returns the current working directory",[152,38154,38155,38157],{},[17,38156,26188],{}," returns the names in that directory",[152,38159,6154,38160,38162],{},[17,38161,26859],{}," loop prints each name on its own line",[602,38164,25290],{"id":25289},[45,38166,38168],{"className":47,"code":38167,"language":49,"meta":50,"style":50},"Current folder:\n\u002FUsers\u002Fsam\u002Fprojects\u002Fpython-demo\n\nNames in this folder:\nmain.py\nnotes.txt\nimages\n",[17,38169,38170,38177,38200,38204,38216,38224,38232],{"__ignoreMap":50},[54,38171,38172,38175],{"class":56,"line":57},[54,38173,38174],{"class":64},"Current folder",[54,38176,7572],{"class":88},[54,38178,38179,38181,38183,38185,38187,38189,38191,38193,38195,38197],{"class":56,"line":68},[54,38180,6676],{"class":81},[54,38182,24074],{"class":64},[54,38184,6676],{"class":81},[54,38186,35182],{"class":64},[54,38188,6676],{"class":81},[54,38190,25576],{"class":64},[54,38192,6676],{"class":81},[54,38194,49],{"class":64},[54,38196,1467],{"class":81},[54,38198,38199],{"class":64},"demo\n",[54,38201,38202],{"class":56,"line":75},[54,38203,72],{"emptyLinePlaceholder":71},[54,38205,38206,38209,38211,38214],{"class":56,"line":112},[54,38207,38208],{"class":64},"Names ",[54,38210,26865],{"class":81},[54,38212,38213],{"class":64}," this folder",[54,38215,7572],{"class":88},[54,38217,38218,38220,38222],{"class":56,"line":887},[54,38219,26893],{"class":64},[54,38221,89],{"class":88},[54,38223,25591],{"class":1693},[54,38225,38226,38228,38230],{"class":56,"line":892},[54,38227,15317],{"class":64},[54,38229,89],{"class":88},[54,38231,26911],{"class":1693},[54,38233,38234],{"class":56,"line":913},[54,38235,38236],{"class":64},"images\n",[14,38238,38239],{},"Your output will be different because it depends on your own folder.",[40,38241,38243],{"id":38242},"os-vs-ospath","os vs os.path",[14,38245,38246,134,38248,38250],{},[17,38247,23720],{},[17,38249,28068],{}," are related, but they are not the same thing.",[602,38252,38253],{"id":23720},[17,38254,23720],{},[14,38256,38257,38258,38260],{},"The main ",[17,38259,23720],{}," module contains general operating system tools, such as:",[149,38262,38263,38267,38271,38275],{},[152,38264,38265],{},[17,38266,23766],{},[152,38268,38269],{},[17,38270,26188],{},[152,38272,38273],{},[17,38274,23669],{},[152,38276,38277],{},"environment variable access",[602,38279,38281],{"id":38280},"ospath",[17,38282,28068],{},[14,38284,38285,38287],{},[17,38286,28068],{}," focuses on file and folder paths, such as:",[149,38289,38290,38294],{},[152,38291,38292],{},[17,38293,25760],{},[152,38295,38296],{},[17,38297,24667],{},[14,38299,38300],{},"Beginners often use both together in the same script.",[14,38302,415],{},[45,38304,38306],{"className":47,"code":38305,"language":49,"meta":50,"style":50},"import os\n\nfolder = os.getcwd()\nfile_name = \"example.txt\"\nfull_path = os.path.join(folder, file_name)\n\nprint(full_path)\nprint(os.path.exists(full_path))\n",[17,38307,38308,38314,38318,38332,38344,38371,38375,38385],{"__ignoreMap":50},[54,38309,38310,38312],{"class":56,"line":57},[54,38311,61],{"class":60},[54,38313,23707],{"class":64},[54,38315,38316],{"class":56,"line":68},[54,38317,72],{"emptyLinePlaceholder":71},[54,38319,38320,38322,38324,38326,38328,38330],{"class":56,"line":75},[54,38321,24696],{"class":64},[54,38323,82],{"class":81},[54,38325,24000],{"class":64},[54,38327,89],{"class":88},[54,38329,23725],{"class":92},[54,38331,1670],{"class":88},[54,38333,38334,38336,38338,38340,38342],{"class":56,"line":112},[54,38335,30500],{"class":64},[54,38337,82],{"class":81},[54,38339,830],{"class":544},[54,38341,27992],{"class":548},[54,38343,5239],{"class":544},[54,38345,38346,38348,38350,38352,38354,38356,38358,38360,38362,38364,38366,38369],{"class":56,"line":887},[54,38347,25712],{"class":64},[54,38349,82],{"class":81},[54,38351,24000],{"class":64},[54,38353,89],{"class":88},[54,38355,23896],{"class":1693},[54,38357,89],{"class":88},[54,38359,25725],{"class":92},[54,38361,96],{"class":88},[54,38363,24762],{"class":92},[54,38365,103],{"class":88},[54,38367,38368],{"class":92}," file_name",[54,38370,109],{"class":88},[54,38372,38373],{"class":56,"line":892},[54,38374,72],{"emptyLinePlaceholder":71},[54,38376,38377,38379,38381,38383],{"class":56,"line":913},[54,38378,116],{"class":115},[54,38380,96],{"class":88},[54,38382,25749],{"class":92},[54,38384,109],{"class":88},[54,38386,38387,38389,38391,38393,38395,38397,38399,38401,38403,38405],{"class":56,"line":2121},[54,38388,116],{"class":115},[54,38390,96],{"class":88},[54,38392,23720],{"class":92},[54,38394,89],{"class":88},[54,38396,23896],{"class":1693},[54,38398,89],{"class":88},[54,38400,24757],{"class":92},[54,38402,96],{"class":88},[54,38404,25749],{"class":92},[54,38406,382],{"class":88},[14,38408,126,38409,38411],{},[17,38410,25760],{}," instead of building paths by hand. This helps your code work more reliably across different systems.",[14,38413,38414,38415,89],{},"If you want a broader explanation, see ",[192,38416,24364],{"href":24363},[40,38418,38420],{"id":38419},"when-to-use-this-page-vs-other-pages","When to use this page vs other pages",[14,38422,38423,38424,38426],{},"Use this page when you want a quick overview of what the ",[17,38425,23720],{}," module does.",[14,38428,38429],{},"Use other pages when you need something more specific:",[149,38431,38432,38435,38438],{},[152,38433,38434],{},"Use function pages for exact syntax, parameters, and return values",[152,38436,38437],{},"Use how-to pages for practical tasks",[152,38439,38440],{},"Use error pages when something is going wrong",[14,38442,415],{},[149,38444,38445,38451,38456],{},[152,38446,38447,38448,89],{},"Want to list folder contents? See ",[192,38449,38450],{"href":27345},"how to list files in a directory in Python",[152,38452,38453,38454,89],{},"Want to check whether a file exists? See ",[192,38455,28378],{"href":25082},[152,38457,38458,38459,89],{},"Getting file path problems? See ",[192,38460,15422],{"href":38461},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes",[40,38463,5210],{"id":5209},[14,38465,38466,38467,89],{},"These are some common problems beginners run into when using ",[17,38468,23720],{},[602,38470,38472],{"id":38471},"forgetting-to-import-os","Forgetting to import os",[14,38474,33166],{},[45,38476,38478],{"className":47,"code":38477,"language":49,"meta":50,"style":50},"print(os.getcwd())\n",[17,38479,38480],{"__ignoreMap":50},[54,38481,38482,38484,38486,38488,38490,38492],{"class":56,"line":57},[54,38483,116],{"class":115},[54,38485,96],{"class":88},[54,38487,23720],{"class":92},[54,38489,89],{"class":88},[54,38491,23725],{"class":92},[54,38493,1950],{"class":88},[14,38495,1963],{},[45,38497,38499],{"className":47,"code":38498,"language":49,"meta":50,"style":50},"import os\n\nprint(os.getcwd())\n",[17,38500,38501,38507,38511],{"__ignoreMap":50},[54,38502,38503,38505],{"class":56,"line":57},[54,38504,61],{"class":60},[54,38506,23707],{"class":64},[54,38508,38509],{"class":56,"line":68},[54,38510,72],{"emptyLinePlaceholder":71},[54,38512,38513,38515,38517,38519,38521,38523],{"class":56,"line":75},[54,38514,116],{"class":115},[54,38516,96],{"class":88},[54,38518,23720],{"class":92},[54,38520,89],{"class":88},[54,38522,23725],{"class":92},[54,38524,1950],{"class":88},[602,38526,38528],{"id":38527},"using-hard-coded-slashes-in-paths","Using hard-coded slashes in paths",[14,38530,38531],{},"Avoid building paths like this:",[45,38533,38535],{"className":47,"code":38534,"language":49,"meta":50,"style":50},"path = \"folder\u002Fsubfolder\u002Ffile.txt\"\n",[17,38536,38537],{"__ignoreMap":50},[54,38538,38539,38541,38543,38545,38548],{"class":56,"line":57},[54,38540,27985],{"class":64},[54,38542,82],{"class":81},[54,38544,830],{"class":544},[54,38546,38547],{"class":548},"folder\u002Fsubfolder\u002Ffile.txt",[54,38549,5239],{"class":544},[14,38551,38552],{},"Instead, use:",[45,38554,38556],{"className":47,"code":38555,"language":49,"meta":50,"style":50},"import os\n\npath = os.path.join(\"folder\", \"subfolder\", \"file.txt\")\nprint(path)\n",[17,38557,38558,38564,38568,38611],{"__ignoreMap":50},[54,38559,38560,38562],{"class":56,"line":57},[54,38561,61],{"class":60},[54,38563,23707],{"class":64},[54,38565,38566],{"class":56,"line":68},[54,38567,72],{"emptyLinePlaceholder":71},[54,38569,38570,38572,38574,38576,38578,38580,38582,38584,38586,38588,38590,38592,38594,38596,38599,38601,38603,38605,38607,38609],{"class":56,"line":75},[54,38571,27985],{"class":64},[54,38573,82],{"class":81},[54,38575,24000],{"class":64},[54,38577,89],{"class":88},[54,38579,23896],{"class":1693},[54,38581,89],{"class":88},[54,38583,25725],{"class":92},[54,38585,96],{"class":88},[54,38587,545],{"class":544},[54,38589,24762],{"class":548},[54,38591,545],{"class":544},[54,38593,103],{"class":88},[54,38595,830],{"class":544},[54,38597,38598],{"class":548},"subfolder",[54,38600,545],{"class":544},[54,38602,103],{"class":88},[54,38604,830],{"class":544},[54,38606,29699],{"class":548},[54,38608,545],{"class":544},[54,38610,109],{"class":88},[54,38612,38613,38615,38617,38619],{"class":56,"line":112},[54,38614,116],{"class":115},[54,38616,96],{"class":88},[54,38618,23896],{"class":92},[54,38620,109],{"class":88},[602,38622,25918,38624,38626],{"id":38623},"assuming-oslistdir-returns-full-paths",[17,38625,26188],{}," returns full paths",[14,38628,38629,38631],{},[17,38630,26188],{}," usually returns names only:",[45,38633,38634],{"className":47,"code":37804,"language":49,"meta":50,"style":50},[17,38635,38636,38642,38646,38660],{"__ignoreMap":50},[54,38637,38638,38640],{"class":56,"line":57},[54,38639,61],{"class":60},[54,38641,23707],{"class":64},[54,38643,38644],{"class":56,"line":68},[54,38645,72],{"emptyLinePlaceholder":71},[54,38647,38648,38650,38652,38654,38656,38658],{"class":56,"line":75},[54,38649,17899],{"class":64},[54,38651,82],{"class":81},[54,38653,24000],{"class":64},[54,38655,89],{"class":88},[54,38657,24433],{"class":92},[54,38659,1670],{"class":88},[54,38661,38662,38664,38666,38668],{"class":56,"line":112},[54,38663,116],{"class":115},[54,38665,96],{"class":88},[54,38667,10388],{"class":92},[54,38669,109],{"class":88},[14,38671,38672],{},"If you need a full path, join the directory and the item name:",[45,38674,38676],{"className":47,"code":38675,"language":49,"meta":50,"style":50},"import os\n\nfolder = os.getcwd()\n\nfor name in os.listdir(folder):\n    full_path = os.path.join(folder, name)\n    print(full_path)\n",[17,38677,38678,38684,38688,38702,38706,38726,38752],{"__ignoreMap":50},[54,38679,38680,38682],{"class":56,"line":57},[54,38681,61],{"class":60},[54,38683,23707],{"class":64},[54,38685,38686],{"class":56,"line":68},[54,38687,72],{"emptyLinePlaceholder":71},[54,38689,38690,38692,38694,38696,38698,38700],{"class":56,"line":75},[54,38691,24696],{"class":64},[54,38693,82],{"class":81},[54,38695,24000],{"class":64},[54,38697,89],{"class":88},[54,38699,23725],{"class":92},[54,38701,1670],{"class":88},[54,38703,38704],{"class":56,"line":112},[54,38705,72],{"emptyLinePlaceholder":71},[54,38707,38708,38710,38712,38714,38716,38718,38720,38722,38724],{"class":56,"line":887},[54,38709,26859],{"class":60},[54,38711,26862],{"class":64},[54,38713,26865],{"class":60},[54,38715,24000],{"class":64},[54,38717,89],{"class":88},[54,38719,24433],{"class":92},[54,38721,96],{"class":88},[54,38723,24762],{"class":92},[54,38725,10837],{"class":88},[54,38727,38728,38730,38732,38734,38736,38738,38740,38742,38744,38746,38748,38750],{"class":56,"line":892},[54,38729,27038],{"class":64},[54,38731,82],{"class":81},[54,38733,24000],{"class":64},[54,38735,89],{"class":88},[54,38737,23896],{"class":1693},[54,38739,89],{"class":88},[54,38741,25725],{"class":92},[54,38743,96],{"class":88},[54,38745,24762],{"class":92},[54,38747,103],{"class":88},[54,38749,10834],{"class":92},[54,38751,109],{"class":88},[54,38753,38754,38756,38758,38760],{"class":56,"line":913},[54,38755,7606],{"class":115},[54,38757,96],{"class":88},[54,38759,25749],{"class":92},[54,38761,109],{"class":88},[602,38763,38765],{"id":38764},"changing-directories-and-losing-track-of-where-you-are","Changing directories and losing track of where you are",[14,38767,2644,38768,38770],{},[17,38769,23669],{},", your script may start looking in a different folder than you expected.",[14,38772,38773],{},"A simple way to check is:",[45,38775,38776],{"className":47,"code":38498,"language":49,"meta":50,"style":50},[17,38777,38778,38784,38788],{"__ignoreMap":50},[54,38779,38780,38782],{"class":56,"line":57},[54,38781,61],{"class":60},[54,38783,23707],{"class":64},[54,38785,38786],{"class":56,"line":68},[54,38787,72],{"emptyLinePlaceholder":71},[54,38789,38790,38792,38794,38796,38798,38800],{"class":56,"line":75},[54,38791,116],{"class":115},[54,38793,96],{"class":88},[54,38795,23720],{"class":92},[54,38797,89],{"class":88},[54,38799,23725],{"class":92},[54,38801,1950],{"class":88},[602,38803,38805],{"id":38804},"mixing-up-files-and-directories","Mixing up files and directories",[14,38807,38808],{},"A path can point to either a file or a folder. If your code assumes the wrong one, it may fail.",[14,38810,38811],{},"It helps to print the path you are checking and confirm it is the one you expect.",[40,38813,1419],{"id":1418},[602,38815,38817],{"id":38816},"what-is-the-os-module-in-python","What is the os module in Python?",[14,38819,38820],{},"It is a standard library module that helps Python interact with the operating system, especially files, folders, and paths.",[602,38822,38824],{"id":38823},"do-i-need-to-install-the-os-module","Do I need to install the os module?",[14,38826,38827],{},"No. It comes with Python.",[602,38829,38831],{"id":38830},"what-is-the-difference-between-os-and-ospath","What is the difference between os and os.path?",[14,38833,38834,38836,38837,38839],{},[17,38835,23720],{}," includes general operating system tools. ",[17,38838,28068],{}," is used specifically for working with paths.",[602,38841,38843],{"id":38842},"should-i-use-os-or-pathlib","Should I use os or pathlib?",[14,38845,38846,38847,38849,38850,38853],{},"Both work. Beginners often see ",[17,38848,23720],{}," in many examples, but ",[17,38851,38852],{},"pathlib"," is also a good modern option for path handling.",[602,38855,27778],{"id":27777},[14,38857,38858,38859,38861],{},"No. It usually returns names only. You often need ",[17,38860,25760],{}," to create a full path.",[40,38863,1510],{"id":1509},[149,38865,38866,38870,38874,38878,38882,38887,38891,38895,38899],{},[152,38867,38868],{},[192,38869,28788],{"href":23871},[152,38871,38872],{},[192,38873,37848],{"href":27930},[152,38875,38876],{},[192,38877,37911],{"href":25116},[152,38879,38880],{},[192,38881,28666],{"href":25757},[152,38883,38884],{},[192,38885,38886],{"href":24853},"os.path.exists() explained",[152,38888,38889],{},[192,38890,25083],{"href":25082},[152,38892,38893],{},[192,38894,27346],{"href":27345},[152,38896,38897],{},[192,38898,25077],{"href":24363},[152,38900,38901],{},[192,38902,15422],{"href":38461},[1545,38904,38905],{},"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 .light .shiki span {color: var(--shiki-light);background: var(--shiki-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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":38907},[38908,38909,38910,38911,38918,38922,38926,38927,38935,38942],{"id":3595,"depth":68,"text":3596},{"id":37638,"depth":68,"text":37639},{"id":37683,"depth":68,"text":37684},{"id":37738,"depth":68,"text":37739,"children":38912},[38913,38914,38915,38916,38917],{"id":37745,"depth":75,"text":23766},{"id":37796,"depth":75,"text":26188},{"id":37851,"depth":75,"text":23669},{"id":37914,"depth":75,"text":25760},{"id":37988,"depth":75,"text":24667},{"id":2257,"depth":68,"text":2258,"children":38919},[38920,38921],{"id":8669,"depth":75,"text":8670},{"id":25289,"depth":75,"text":25290},{"id":38242,"depth":68,"text":38243,"children":38923},[38924,38925],{"id":23720,"depth":75,"text":23720},{"id":38280,"depth":75,"text":28068},{"id":38419,"depth":68,"text":38420},{"id":5209,"depth":68,"text":5210,"children":38928},[38929,38930,38931,38933,38934],{"id":38471,"depth":75,"text":38472},{"id":38527,"depth":75,"text":38528},{"id":38623,"depth":75,"text":38932},"Assuming os.listdir() returns full paths",{"id":38764,"depth":75,"text":38765},{"id":38804,"depth":75,"text":38805},{"id":1418,"depth":68,"text":1419,"children":38936},[38937,38938,38939,38940,38941],{"id":38816,"depth":75,"text":38817},{"id":38823,"depth":75,"text":38824},{"id":38830,"depth":75,"text":38831},{"id":38842,"depth":75,"text":38843},{"id":27777,"depth":75,"text":27778},{"id":1509,"depth":68,"text":1510},"Master python os module overview in our comprehensive Python beginner guide.",{},{"title":37555,"description":38943},"standard-library\u002Fpython-os-module-overview","Tmq0s-pnBt1Z3fZFABEG1kM1ZNQTR6vjFX6VE3yiEl0",{"id":38949,"title":38950,"body":38951,"description":40941,"extension":1577,"meta":40942,"navigation":71,"path":194,"seo":40943,"stem":40944,"__hash__":40945},"content\u002Fstandard-library\u002Fpython-random-module-overview.md","Python random Module Overview",{"type":7,"value":38952,"toc":40901},[38953,38956,38961,38964,38978,38981,38989,38991,39079,39088,39092,39098,39101,39115,39122,39126,39140,39145,39156,39160,39165,39181,39184,39198,39202,39207,39213,39227,39256,39258,39267,39270,39276,39286,39323,39325,39333,39336,39342,39348,39354,39418,39420,39433,39436,39457,39462,39468,39471,39538,39540,39549,39552,39589,39591,39600,39605,39611,39617,39697,39699,39748,39751,39757,39764,39834,39836,39857,39863,39869,39875,39943,39945,39974,39981,39987,39991,39996,40024,40027,40217,40221,40230,40233,40237,40251,40256,40269,40273,40277,40282,40289,40316,40319,40330,40336,40342,40345,40387,40392,40461,40467,40470,40535,40537,40545,40551,40553,40612,40620,40623,40688,40691,40693,40696,40728,40735,40739,40742,40792,40794,40798,40803,40810,40816,40824,40831,40834,40840,40846,40853,40859,40861,40890,40898],[10,38954,38950],{"id":38955},"python-random-module-overview",[14,38957,28340,38958,38960],{},[17,38959,26],{}," module is a built-in Python module for working with pseudo-random values.",[14,38962,38963],{},"Beginners often use it to:",[149,38965,38966,38969,38972,38975],{},[152,38967,38968],{},"generate random numbers",[152,38970,38971],{},"choose a random item from a list",[152,38973,38974],{},"shuffle a list",[152,38976,38977],{},"pick several random items",[14,38979,38980],{},"It is useful for games, simple scripts, testing, and practice programs.",[14,38982,38983,38984,38986,38987,1506],{},"It is ",[35,38985,3036],{}," the right tool for passwords, secure tokens, or anything security-related. For that, use the ",[17,38988,1412],{},[40,38990,3596],{"id":3595},[45,38992,38994],{"className":47,"code":38993,"language":49,"meta":50,"style":50},"import random\n\nprint(random.randint(1, 10))\nitems = [\"apple\", \"banana\", \"cherry\"]\nprint(random.choice(items))\n",[17,38995,38996,39002,39006,39028,39060],{"__ignoreMap":50},[54,38997,38998,39000],{"class":56,"line":57},[54,38999,61],{"class":60},[54,39001,65],{"class":64},[54,39003,39004],{"class":56,"line":68},[54,39005,72],{"emptyLinePlaceholder":71},[54,39007,39008,39010,39012,39014,39016,39018,39020,39022,39024,39026],{"class":56,"line":75},[54,39009,116],{"class":115},[54,39011,96],{"class":88},[54,39013,26],{"class":92},[54,39015,89],{"class":88},[54,39017,93],{"class":92},[54,39019,96],{"class":88},[54,39021,100],{"class":99},[54,39023,103],{"class":88},[54,39025,106],{"class":99},[54,39027,382],{"class":88},[54,39029,39030,39032,39034,39036,39038,39040,39042,39044,39046,39048,39050,39052,39054,39056,39058],{"class":56,"line":112},[54,39031,17899],{"class":64},[54,39033,82],{"class":81},[54,39035,818],{"class":88},[54,39037,545],{"class":544},[54,39039,10399],{"class":548},[54,39041,545],{"class":544},[54,39043,103],{"class":88},[54,39045,830],{"class":544},[54,39047,10408],{"class":548},[54,39049,545],{"class":544},[54,39051,103],{"class":88},[54,39053,830],{"class":544},[54,39055,17987],{"class":548},[54,39057,545],{"class":544},[54,39059,469],{"class":88},[54,39061,39062,39064,39066,39068,39070,39073,39075,39077],{"class":56,"line":887},[54,39063,116],{"class":115},[54,39065,96],{"class":88},[54,39067,26],{"class":92},[54,39069,89],{"class":88},[54,39071,39072],{"class":92},"choice",[54,39074,96],{"class":88},[54,39076,10388],{"class":92},[54,39078,382],{"class":88},[14,39080,126,39081,39083,39084,39087],{},[17,39082,792],{}," for a random whole number in a range, and ",[17,39085,39086],{},"choice()"," to pick one item from a list.",[40,39089,39091],{"id":39090},"what-the-random-module-is","What the random module is",[14,39093,39094,39095,39097],{},"Python’s ",[17,39096,26],{}," module is part of the standard library, so you do not need to install anything.",[14,39099,39100],{},"It gives you functions for:",[149,39102,39103,39106,39109,39112],{},[152,39104,39105],{},"random numbers",[152,39107,39108],{},"random choices from sequences",[152,39110,39111],{},"random sampling",[152,39113,39114],{},"shuffling lists",[14,39116,39117,39118,39121],{},"The values are ",[35,39119,39120],{},"pseudo-random",". That means they are produced by an algorithm, but they behave randomly enough for most beginner programs.",[14,39123,126,39124,16963],{},[17,39125,26],{},[149,39127,39128,39131,39134,39137],{},[152,39129,39130],{},"small games",[152,39132,39133],{},"classroom examples",[152,39135,39136],{},"simple automation",[152,39138,39139],{},"testing with sample data",[14,39141,16081,39142,39144],{},[35,39143,3036],{}," use it for:",[149,39146,39147,39150,39153],{},[152,39148,39149],{},"passwords",[152,39151,39152],{},"authentication tokens",[152,39154,39155],{},"security-sensitive code",[40,39157,39159],{"id":39158},"when-beginners-use-it-most","When beginners use it most",[14,39161,37687,39162,39164],{},[17,39163,26],{}," when they want to:",[149,39166,39167,39170,39172,39175,39178],{},[152,39168,39169],{},"pick a random number in a range",[152,39171,38971],{},[152,39173,39174],{},"shuffle a list into a new order",[152,39176,39177],{},"pick multiple random items",[152,39179,39180],{},"create simple game behavior",[14,39182,39183],{},"For example, you might:",[149,39185,39186,39189,39192,39195],{},[152,39187,39188],{},"roll a die",[152,39190,39191],{},"choose a random student name",[152,39193,39194],{},"shuffle quiz questions",[152,39196,39197],{},"build a number guessing game",[40,39199,39201],{"id":39200},"functions-to-know-first","Functions to know first",[14,39203,39204,39205,15765],{},"These are the most common functions in the ",[17,39206,26],{},[602,39208,39210],{"id":39209},"randomrandom",[17,39211,39212],{},"random.random()",[14,39214,39215,39216,39219,39220,39223,39224,89],{},"Returns a floating-point number from ",[17,39217,39218],{},"0.0"," up to but ",[35,39221,39222],{},"not including"," ",[17,39225,39226],{},"1.0",[45,39228,39230],{"className":47,"code":39229,"language":49,"meta":50,"style":50},"import random\n\nprint(random.random())\n",[17,39231,39232,39238,39242],{"__ignoreMap":50},[54,39233,39234,39236],{"class":56,"line":57},[54,39235,61],{"class":60},[54,39237,65],{"class":64},[54,39239,39240],{"class":56,"line":68},[54,39241,72],{"emptyLinePlaceholder":71},[54,39243,39244,39246,39248,39250,39252,39254],{"class":56,"line":75},[54,39245,116],{"class":115},[54,39247,96],{"class":88},[54,39249,26],{"class":92},[54,39251,89],{"class":88},[54,39253,26],{"class":92},[54,39255,1950],{"class":88},[14,39257,305],{},[45,39259,39261],{"className":47,"code":39260,"language":49,"meta":50,"style":50},"0.472918374\n",[17,39262,39263],{"__ignoreMap":50},[54,39264,39265],{"class":56,"line":57},[54,39266,39260],{"class":99},[14,39268,39269],{},"Use this when you want a random decimal value.",[602,39271,39273],{"id":39272},"randomrandinta-b",[17,39274,39275],{},"random.randint(a, b)",[14,39277,39278,39279,248,39281,320,39283,89],{},"Returns a random whole number from ",[17,39280,192],{},[17,39282,241],{},[35,39284,39285],{},"including both ends",[45,39287,39289],{"className":47,"code":39288,"language":49,"meta":50,"style":50},"import random\n\nprint(random.randint(1, 6))\n",[17,39290,39291,39297,39301],{"__ignoreMap":50},[54,39292,39293,39295],{"class":56,"line":57},[54,39294,61],{"class":60},[54,39296,65],{"class":64},[54,39298,39299],{"class":56,"line":68},[54,39300,72],{"emptyLinePlaceholder":71},[54,39302,39303,39305,39307,39309,39311,39313,39315,39317,39319,39321],{"class":56,"line":75},[54,39304,116],{"class":115},[54,39306,96],{"class":88},[54,39308,26],{"class":92},[54,39310,89],{"class":88},[54,39312,93],{"class":92},[54,39314,96],{"class":88},[54,39316,100],{"class":99},[54,39318,103],{"class":88},[54,39320,533],{"class":99},[54,39322,382],{"class":88},[14,39324,305],{},[45,39326,39327],{"className":47,"code":4510,"language":49,"meta":50,"style":50},[17,39328,39329],{"__ignoreMap":50},[54,39330,39331],{"class":56,"line":57},[54,39332,4510],{"class":99},[14,39334,39335],{},"This is a common choice for dice rolls and number guessing games.",[14,39337,39338,39339,89],{},"If you want a deeper explanation, see ",[192,39340,39341],{"href":1579},"random.randint() explained",[602,39343,39345],{"id":39344},"randomrandrangestart-stop-step",[17,39346,39347],{},"random.randrange(start, stop[, step])",[14,39349,39350,39351,39353],{},"Works like ",[17,39352,410],{},", but returns one random value from that range.",[45,39355,39357],{"className":47,"code":39356,"language":49,"meta":50,"style":50},"import random\n\nprint(random.randrange(0, 10))\nprint(random.randrange(0, 10, 2))\n",[17,39358,39359,39365,39369,39392],{"__ignoreMap":50},[54,39360,39361,39363],{"class":56,"line":57},[54,39362,61],{"class":60},[54,39364,65],{"class":64},[54,39366,39367],{"class":56,"line":68},[54,39368,72],{"emptyLinePlaceholder":71},[54,39370,39371,39373,39375,39377,39379,39382,39384,39386,39388,39390],{"class":56,"line":75},[54,39372,116],{"class":115},[54,39374,96],{"class":88},[54,39376,26],{"class":92},[54,39378,89],{"class":88},[54,39380,39381],{"class":92},"randrange",[54,39383,96],{"class":88},[54,39385,752],{"class":99},[54,39387,103],{"class":88},[54,39389,106],{"class":99},[54,39391,382],{"class":88},[54,39393,39394,39396,39398,39400,39402,39404,39406,39408,39410,39412,39414,39416],{"class":56,"line":112},[54,39395,116],{"class":115},[54,39397,96],{"class":88},[54,39399,26],{"class":92},[54,39401,89],{"class":88},[54,39403,39381],{"class":92},[54,39405,96],{"class":88},[54,39407,752],{"class":99},[54,39409,103],{"class":88},[54,39411,106],{"class":99},[54,39413,103],{"class":88},[54,39415,466],{"class":99},[54,39417,382],{"class":88},[14,39419,305],{},[45,39421,39423],{"className":47,"code":39422,"language":49,"meta":50,"style":50},"7\n4\n",[17,39424,39425,39429],{"__ignoreMap":50},[54,39426,39427],{"class":56,"line":57},[54,39428,308],{"class":99},[54,39430,39431],{"class":56,"line":68},[54,39432,4510],{"class":99},[14,39434,39435],{},"Important detail:",[149,39437,39438,39449],{},[152,39439,39440,39443,39444,39446,39447],{},[17,39441,39442],{},"random.randrange(0, 10)"," can return ",[17,39445,752],{}," through ",[17,39448,1125],{},[152,39450,39451,39452,39454,39455],{},"it does ",[35,39453,3036],{}," include ",[17,39456,1131],{},[14,39458,39459,39460,89],{},"This is one of the main differences from ",[17,39461,792],{},[602,39463,39465],{"id":39464},"randomchoiceseq",[17,39466,39467],{},"random.choice(seq)",[14,39469,39470],{},"Returns one random item from a sequence such as a list, tuple, or string.",[45,39472,39474],{"className":47,"code":39473,"language":49,"meta":50,"style":50},"import random\n\nfruits = [\"apple\", \"banana\", \"cherry\"]\nprint(random.choice(fruits))\n",[17,39475,39476,39482,39486,39519],{"__ignoreMap":50},[54,39477,39478,39480],{"class":56,"line":57},[54,39479,61],{"class":60},[54,39481,65],{"class":64},[54,39483,39484],{"class":56,"line":68},[54,39485,72],{"emptyLinePlaceholder":71},[54,39487,39488,39491,39493,39495,39497,39499,39501,39503,39505,39507,39509,39511,39513,39515,39517],{"class":56,"line":75},[54,39489,39490],{"class":64},"fruits ",[54,39492,82],{"class":81},[54,39494,818],{"class":88},[54,39496,545],{"class":544},[54,39498,10399],{"class":548},[54,39500,545],{"class":544},[54,39502,103],{"class":88},[54,39504,830],{"class":544},[54,39506,10408],{"class":548},[54,39508,545],{"class":544},[54,39510,103],{"class":88},[54,39512,830],{"class":544},[54,39514,17987],{"class":548},[54,39516,545],{"class":544},[54,39518,469],{"class":88},[54,39520,39521,39523,39525,39527,39529,39531,39533,39536],{"class":56,"line":112},[54,39522,116],{"class":115},[54,39524,96],{"class":88},[54,39526,26],{"class":92},[54,39528,89],{"class":88},[54,39530,39072],{"class":92},[54,39532,96],{"class":88},[54,39534,39535],{"class":92},"fruits",[54,39537,382],{"class":88},[14,39539,305],{},[45,39541,39543],{"className":47,"code":39542,"language":49,"meta":50,"style":50},"banana\n",[17,39544,39545],{"__ignoreMap":50},[54,39546,39547],{"class":56,"line":57},[54,39548,39542],{"class":64},[14,39550,39551],{},"It also works with strings:",[45,39553,39555],{"className":47,"code":39554,"language":49,"meta":50,"style":50},"import random\n\nprint(random.choice(\"python\"))\n",[17,39556,39557,39563,39567],{"__ignoreMap":50},[54,39558,39559,39561],{"class":56,"line":57},[54,39560,61],{"class":60},[54,39562,65],{"class":64},[54,39564,39565],{"class":56,"line":68},[54,39566,72],{"emptyLinePlaceholder":71},[54,39568,39569,39571,39573,39575,39577,39579,39581,39583,39585,39587],{"class":56,"line":75},[54,39570,116],{"class":115},[54,39572,96],{"class":88},[54,39574,26],{"class":92},[54,39576,89],{"class":88},[54,39578,39072],{"class":92},[54,39580,96],{"class":88},[54,39582,545],{"class":544},[54,39584,49],{"class":548},[54,39586,545],{"class":544},[54,39588,382],{"class":88},[14,39590,305],{},[45,39592,39594],{"className":47,"code":39593,"language":49,"meta":50,"style":50},"t\n",[17,39595,39596],{"__ignoreMap":50},[54,39597,39598],{"class":56,"line":57},[54,39599,39593],{"class":64},[14,39601,15021,39602,89],{},[192,39603,39604],{"href":970},"random.choice() explained",[602,39606,39608],{"id":39607},"randomchoicesseq-kn",[17,39609,39610],{},"random.choices(seq, k=n)",[14,39612,39613,39614,89],{},"Returns multiple random items. Values ",[35,39615,39616],{},"can repeat",[45,39618,39620],{"className":47,"code":39619,"language":49,"meta":50,"style":50},"import random\n\ncolors = [\"red\", \"blue\", \"green\"]\nprint(random.choices(colors, k=5))\n",[17,39621,39622,39628,39632,39668],{"__ignoreMap":50},[54,39623,39624,39626],{"class":56,"line":57},[54,39625,61],{"class":60},[54,39627,65],{"class":64},[54,39629,39630],{"class":56,"line":68},[54,39631,72],{"emptyLinePlaceholder":71},[54,39633,39634,39637,39639,39641,39643,39646,39648,39650,39652,39655,39657,39659,39661,39664,39666],{"class":56,"line":75},[54,39635,39636],{"class":64},"colors ",[54,39638,82],{"class":81},[54,39640,818],{"class":88},[54,39642,545],{"class":544},[54,39644,39645],{"class":548},"red",[54,39647,545],{"class":544},[54,39649,103],{"class":88},[54,39651,830],{"class":544},[54,39653,39654],{"class":548},"blue",[54,39656,545],{"class":544},[54,39658,103],{"class":88},[54,39660,830],{"class":544},[54,39662,39663],{"class":548},"green",[54,39665,545],{"class":544},[54,39667,469],{"class":88},[54,39669,39670,39672,39674,39676,39678,39681,39683,39686,39688,39691,39693,39695],{"class":56,"line":112},[54,39671,116],{"class":115},[54,39673,96],{"class":88},[54,39675,26],{"class":92},[54,39677,89],{"class":88},[54,39679,39680],{"class":92},"choices",[54,39682,96],{"class":88},[54,39684,39685],{"class":92},"colors",[54,39687,103],{"class":88},[54,39689,39690],{"class":8940}," k",[54,39692,82],{"class":81},[54,39694,285],{"class":99},[54,39696,382],{"class":88},[14,39698,305],{},[45,39700,39702],{"className":47,"code":39701,"language":49,"meta":50,"style":50},"['blue', 'red', 'blue', 'green', 'blue']\n",[17,39703,39704],{"__ignoreMap":50},[54,39705,39706,39708,39710,39712,39714,39716,39718,39720,39722,39724,39726,39728,39730,39732,39734,39736,39738,39740,39742,39744,39746],{"class":56,"line":57},[54,39707,459],{"class":88},[54,39709,2240],{"class":544},[54,39711,39654],{"class":548},[54,39713,2240],{"class":544},[54,39715,103],{"class":88},[54,39717,2234],{"class":544},[54,39719,39645],{"class":548},[54,39721,2240],{"class":544},[54,39723,103],{"class":88},[54,39725,2234],{"class":544},[54,39727,39654],{"class":548},[54,39729,2240],{"class":544},[54,39731,103],{"class":88},[54,39733,2234],{"class":544},[54,39735,39663],{"class":548},[54,39737,2240],{"class":544},[54,39739,103],{"class":88},[54,39741,2234],{"class":544},[54,39743,39654],{"class":548},[54,39745,2240],{"class":544},[54,39747,469],{"class":88},[14,39749,39750],{},"Use this when repeats are allowed.",[602,39752,39754],{"id":39753},"randomsampleseq-kn",[17,39755,39756],{},"random.sample(seq, k=n)",[14,39758,39759,39760,39763],{},"Returns multiple ",[35,39761,39762],{},"unique"," random items.",[45,39765,39767],{"className":47,"code":39766,"language":49,"meta":50,"style":50},"import random\n\nnumbers = [1, 2, 3, 4, 5]\nprint(random.sample(numbers, k=3))\n",[17,39768,39769,39775,39779,39807],{"__ignoreMap":50},[54,39770,39771,39773],{"class":56,"line":57},[54,39772,61],{"class":60},[54,39774,65],{"class":64},[54,39776,39777],{"class":56,"line":68},[54,39778,72],{"emptyLinePlaceholder":71},[54,39780,39781,39783,39785,39787,39789,39791,39793,39795,39797,39799,39801,39803,39805],{"class":56,"line":75},[54,39782,11576],{"class":64},[54,39784,82],{"class":81},[54,39786,818],{"class":88},[54,39788,100],{"class":99},[54,39790,103],{"class":88},[54,39792,466],{"class":99},[54,39794,103],{"class":88},[54,39796,379],{"class":99},[54,39798,103],{"class":88},[54,39800,4264],{"class":99},[54,39802,103],{"class":88},[54,39804,1235],{"class":99},[54,39806,469],{"class":88},[54,39808,39809,39811,39813,39815,39817,39820,39822,39824,39826,39828,39830,39832],{"class":56,"line":112},[54,39810,116],{"class":115},[54,39812,96],{"class":88},[54,39814,26],{"class":92},[54,39816,89],{"class":88},[54,39818,39819],{"class":92},"sample",[54,39821,96],{"class":88},[54,39823,9956],{"class":92},[54,39825,103],{"class":88},[54,39827,39690],{"class":8940},[54,39829,82],{"class":81},[54,39831,401],{"class":99},[54,39833,382],{"class":88},[14,39835,305],{},[45,39837,39839],{"className":47,"code":39838,"language":49,"meta":50,"style":50},"[5, 2, 1]\n",[17,39840,39841],{"__ignoreMap":50},[54,39842,39843,39845,39847,39849,39851,39853,39855],{"class":56,"line":57},[54,39844,459],{"class":88},[54,39846,285],{"class":99},[54,39848,103],{"class":88},[54,39850,466],{"class":99},[54,39852,103],{"class":88},[54,39854,882],{"class":99},[54,39856,469],{"class":88},[14,39858,39859,39860,89],{},"Use this when you want random items with ",[35,39861,39862],{},"no duplicates",[602,39864,39866],{"id":39865},"randomshufflex",[17,39867,39868],{},"random.shuffle(x)",[14,39870,39871,39872,89],{},"Shuffles a list ",[35,39873,39874],{},"in place",[45,39876,39878],{"className":47,"code":39877,"language":49,"meta":50,"style":50},"import random\n\ndata = [1, 2, 3, 4, 5]\nrandom.shuffle(data)\nprint(data)\n",[17,39879,39880,39886,39890,39918,39933],{"__ignoreMap":50},[54,39881,39882,39884],{"class":56,"line":57},[54,39883,61],{"class":60},[54,39885,65],{"class":64},[54,39887,39888],{"class":56,"line":68},[54,39889,72],{"emptyLinePlaceholder":71},[54,39891,39892,39894,39896,39898,39900,39902,39904,39906,39908,39910,39912,39914,39916],{"class":56,"line":75},[54,39893,8221],{"class":64},[54,39895,82],{"class":81},[54,39897,818],{"class":88},[54,39899,100],{"class":99},[54,39901,103],{"class":88},[54,39903,466],{"class":99},[54,39905,103],{"class":88},[54,39907,379],{"class":99},[54,39909,103],{"class":88},[54,39911,4264],{"class":99},[54,39913,103],{"class":88},[54,39915,1235],{"class":99},[54,39917,469],{"class":88},[54,39919,39920,39922,39924,39927,39929,39931],{"class":56,"line":112},[54,39921,26],{"class":64},[54,39923,89],{"class":88},[54,39925,39926],{"class":92},"shuffle",[54,39928,96],{"class":88},[54,39930,8313],{"class":92},[54,39932,109],{"class":88},[54,39934,39935,39937,39939,39941],{"class":56,"line":887},[54,39936,116],{"class":115},[54,39938,96],{"class":88},[54,39940,8313],{"class":92},[54,39942,109],{"class":88},[14,39944,305],{},[45,39946,39948],{"className":47,"code":39947,"language":49,"meta":50,"style":50},"[3, 5, 1, 4, 2]\n",[17,39949,39950],{"__ignoreMap":50},[54,39951,39952,39954,39956,39958,39960,39962,39964,39966,39968,39970,39972],{"class":56,"line":57},[54,39953,459],{"class":88},[54,39955,401],{"class":99},[54,39957,103],{"class":88},[54,39959,1235],{"class":99},[54,39961,103],{"class":88},[54,39963,882],{"class":99},[54,39965,103],{"class":88},[54,39967,4264],{"class":99},[54,39969,103],{"class":88},[54,39971,466],{"class":99},[54,39973,469],{"class":88},[14,39975,39976,39977,39980],{},"Important: ",[17,39978,39979],{},"shuffle()"," changes the original list. It does not create a new one.",[14,39982,15021,39983,89],{},[192,39984,39986],{"href":39985},"\u002Fstandard-library\u002Frandom.shuffle-function-explained","random.shuffle() explained",[40,39988,39990],{"id":39989},"important-beginner-rules","Important beginner rules",[14,39992,39993,39994,147],{},"Keep these rules in mind when using ",[17,39995,26],{},[149,39997,39998,40003,40008,40013,40019],{},[152,39999,40000,40001],{},"Always import the module first with ",[17,40002,166],{},[152,40004,126,40005,40007],{},[17,40006,39086],{}," only with a non-empty sequence",[152,40009,40010,40012],{},[17,40011,39979],{}," changes the original list",[152,40014,40015,40018],{},[17,40016,40017],{},"sample()"," cannot choose more unique items than the sequence contains",[152,40020,40021,40023],{},[17,40022,792],{}," includes both the start and end values",[14,40025,40026],{},"Here is a simple example that combines several functions:",[45,40028,40030],{"className":47,"code":40029,"language":49,"meta":50,"style":50},"import random\n\nnames = [\"Ana\", \"Ben\", \"Cara\", \"Drew\"]\n\nprint(\"Random name:\", random.choice(names))\nprint(\"Random number:\", random.randint(1, 10))\nprint(\"Two unique names:\", random.sample(names, k=2))\n\nrandom.shuffle(names)\nprint(\"Shuffled list:\", names)\n",[17,40031,40032,40038,40042,40083,40087,40114,40145,40180,40184,40198],{"__ignoreMap":50},[54,40033,40034,40036],{"class":56,"line":57},[54,40035,61],{"class":60},[54,40037,65],{"class":64},[54,40039,40040],{"class":56,"line":68},[54,40041,72],{"emptyLinePlaceholder":71},[54,40043,40044,40046,40048,40050,40052,40054,40056,40058,40060,40062,40064,40066,40068,40070,40072,40074,40076,40079,40081],{"class":56,"line":75},[54,40045,813],{"class":64},[54,40047,82],{"class":81},[54,40049,818],{"class":88},[54,40051,545],{"class":544},[54,40053,823],{"class":548},[54,40055,545],{"class":544},[54,40057,103],{"class":88},[54,40059,830],{"class":544},[54,40061,833],{"class":548},[54,40063,545],{"class":544},[54,40065,103],{"class":88},[54,40067,830],{"class":544},[54,40069,842],{"class":548},[54,40071,545],{"class":544},[54,40073,103],{"class":88},[54,40075,830],{"class":544},[54,40077,40078],{"class":548},"Drew",[54,40080,545],{"class":544},[54,40082,469],{"class":88},[54,40084,40085],{"class":56,"line":112},[54,40086,72],{"emptyLinePlaceholder":71},[54,40088,40089,40091,40093,40095,40098,40100,40102,40104,40106,40108,40110,40112],{"class":56,"line":887},[54,40090,116],{"class":115},[54,40092,96],{"class":88},[54,40094,545],{"class":544},[54,40096,40097],{"class":548},"Random name:",[54,40099,545],{"class":544},[54,40101,103],{"class":88},[54,40103,85],{"class":92},[54,40105,89],{"class":88},[54,40107,39072],{"class":92},[54,40109,96],{"class":88},[54,40111,873],{"class":92},[54,40113,382],{"class":88},[54,40115,40116,40118,40120,40122,40125,40127,40129,40131,40133,40135,40137,40139,40141,40143],{"class":56,"line":892},[54,40117,116],{"class":115},[54,40119,96],{"class":88},[54,40121,545],{"class":544},[54,40123,40124],{"class":548},"Random number:",[54,40126,545],{"class":544},[54,40128,103],{"class":88},[54,40130,85],{"class":92},[54,40132,89],{"class":88},[54,40134,93],{"class":92},[54,40136,96],{"class":88},[54,40138,100],{"class":99},[54,40140,103],{"class":88},[54,40142,106],{"class":99},[54,40144,382],{"class":88},[54,40146,40147,40149,40151,40153,40156,40158,40160,40162,40164,40166,40168,40170,40172,40174,40176,40178],{"class":56,"line":913},[54,40148,116],{"class":115},[54,40150,96],{"class":88},[54,40152,545],{"class":544},[54,40154,40155],{"class":548},"Two unique names:",[54,40157,545],{"class":544},[54,40159,103],{"class":88},[54,40161,85],{"class":92},[54,40163,89],{"class":88},[54,40165,39819],{"class":92},[54,40167,96],{"class":88},[54,40169,873],{"class":92},[54,40171,103],{"class":88},[54,40173,39690],{"class":8940},[54,40175,82],{"class":81},[54,40177,396],{"class":99},[54,40179,382],{"class":88},[54,40181,40182],{"class":56,"line":2121},[54,40183,72],{"emptyLinePlaceholder":71},[54,40185,40186,40188,40190,40192,40194,40196],{"class":56,"line":2136},[54,40187,26],{"class":64},[54,40189,89],{"class":88},[54,40191,39926],{"class":92},[54,40193,96],{"class":88},[54,40195,873],{"class":92},[54,40197,109],{"class":88},[54,40199,40200,40202,40204,40206,40209,40211,40213,40215],{"class":56,"line":2151},[54,40201,116],{"class":115},[54,40203,96],{"class":88},[54,40205,545],{"class":544},[54,40207,40208],{"class":548},"Shuffled list:",[54,40210,545],{"class":544},[54,40212,103],{"class":88},[54,40214,929],{"class":92},[54,40216,109],{"class":88},[40,40218,40220],{"id":40219},"pseudo-random-vs-truly-random","Pseudo-random vs truly random",[14,40222,28340,40223,40225,40226,40229],{},[17,40224,26],{}," module produces values that ",[35,40227,40228],{},"look random",", but they come from a mathematical process.",[14,40231,40232],{},"For most beginner programs, that is completely fine.",[14,40234,126,40235,16963],{},[17,40236,26],{},[149,40238,40239,40242,40245,40248],{},[152,40240,40241],{},"games",[152,40243,40244],{},"practice projects",[152,40246,40247],{},"simulations",[152,40249,40250],{},"random test data",[14,40252,36885,40253,40255],{},[17,40254,1412],{}," module instead for:",[149,40257,40258,40260,40263,40266],{},[152,40259,39149],{},[152,40261,40262],{},"reset links",[152,40264,40265],{},"access tokens",[152,40267,40268],{},"security codes",[40,40270,40272],{"id":40271},"common-problems-and-confusion","Common problems and confusion",[14,40274,2882,40275,89],{},[17,40276,26],{},[602,40278,17008,40280],{"id":40279},"forgetting-to-import-random",[17,40281,26],{},[14,40283,2644,40284,40286,40287,89],{},[17,40285,19],{}," without importing the module first, you will get a ",[17,40288,988],{},[45,40290,40292],{"className":47,"code":40291,"language":49,"meta":50,"style":50},"print(random.randint(1, 10))\n",[17,40293,40294],{"__ignoreMap":50},[54,40295,40296,40298,40300,40302,40304,40306,40308,40310,40312,40314],{"class":56,"line":57},[54,40297,116],{"class":115},[54,40299,96],{"class":88},[54,40301,26],{"class":92},[54,40303,89],{"class":88},[54,40305,93],{"class":92},[54,40307,96],{"class":88},[54,40309,100],{"class":99},[54,40311,103],{"class":88},[54,40313,106],{"class":99},[54,40315,382],{"class":88},[14,40317,40318],{},"Fix it by adding:",[45,40320,40322],{"className":47,"code":40321,"language":49,"meta":50,"style":50},"import random\n",[17,40323,40324],{"__ignoreMap":50},[54,40325,40326,40328],{"class":56,"line":57},[54,40327,61],{"class":60},[54,40329,65],{"class":64},[14,40331,40332,40333,89],{},"If needed, read ",[192,40334,40335],{"href":19710},"how to fix NameError: name is not defined",[602,40337,5481,40339,40341],{"id":40338},"using-choice-with-an-empty-list",[17,40340,39086],{}," with an empty list",[14,40343,40344],{},"This causes an error because there is nothing to choose.",[45,40346,40348],{"className":47,"code":40347,"language":49,"meta":50,"style":50},"import random\n\nitems = []\nprint(random.choice(items))\n",[17,40349,40350,40356,40360,40369],{"__ignoreMap":50},[54,40351,40352,40354],{"class":56,"line":57},[54,40353,61],{"class":60},[54,40355,65],{"class":64},[54,40357,40358],{"class":56,"line":68},[54,40359,72],{"emptyLinePlaceholder":71},[54,40361,40362,40364,40366],{"class":56,"line":75},[54,40363,17899],{"class":64},[54,40365,82],{"class":81},[54,40367,40368],{"class":88}," []\n",[54,40370,40371,40373,40375,40377,40379,40381,40383,40385],{"class":56,"line":112},[54,40372,116],{"class":115},[54,40374,96],{"class":88},[54,40376,26],{"class":92},[54,40378,89],{"class":88},[54,40380,39072],{"class":92},[54,40382,96],{"class":88},[54,40384,10388],{"class":92},[54,40386,382],{"class":88},[14,40388,40389,40390,89],{},"Make sure the sequence is not empty before calling ",[17,40391,39086],{},[45,40393,40395],{"className":47,"code":40394,"language":49,"meta":50,"style":50},"import random\n\nitems = [\"apple\", \"banana\"]\n\nif items:\n    print(random.choice(items))\n",[17,40396,40397,40403,40407,40431,40435,40443],{"__ignoreMap":50},[54,40398,40399,40401],{"class":56,"line":57},[54,40400,61],{"class":60},[54,40402,65],{"class":64},[54,40404,40405],{"class":56,"line":68},[54,40406,72],{"emptyLinePlaceholder":71},[54,40408,40409,40411,40413,40415,40417,40419,40421,40423,40425,40427,40429],{"class":56,"line":75},[54,40410,17899],{"class":64},[54,40412,82],{"class":81},[54,40414,818],{"class":88},[54,40416,545],{"class":544},[54,40418,10399],{"class":548},[54,40420,545],{"class":544},[54,40422,103],{"class":88},[54,40424,830],{"class":544},[54,40426,10408],{"class":548},[54,40428,545],{"class":544},[54,40430,469],{"class":88},[54,40432,40433],{"class":56,"line":112},[54,40434,72],{"emptyLinePlaceholder":71},[54,40436,40437,40439,40441],{"class":56,"line":887},[54,40438,22942],{"class":60},[54,40440,26868],{"class":64},[54,40442,7572],{"class":88},[54,40444,40445,40447,40449,40451,40453,40455,40457,40459],{"class":56,"line":892},[54,40446,7606],{"class":115},[54,40448,96],{"class":88},[54,40450,26],{"class":92},[54,40452,89],{"class":88},[54,40454,39072],{"class":92},[54,40456,96],{"class":88},[54,40458,10388],{"class":92},[54,40460,382],{"class":88},[602,40462,5949,40464,40466],{"id":40463},"expecting-shuffle-to-return-a-new-list",[17,40465,39979],{}," to return a new list",[14,40468,40469],{},"This is a very common mistake:",[45,40471,40473],{"className":47,"code":40472,"language":49,"meta":50,"style":50},"import random\n\nmy_list = [1, 2, 3]\nresult = random.shuffle(my_list)\nprint(result)\n",[17,40474,40475,40481,40485,40506,40525],{"__ignoreMap":50},[54,40476,40477,40479],{"class":56,"line":57},[54,40478,61],{"class":60},[54,40480,65],{"class":64},[54,40482,40483],{"class":56,"line":68},[54,40484,72],{"emptyLinePlaceholder":71},[54,40486,40487,40490,40492,40494,40496,40498,40500,40502,40504],{"class":56,"line":75},[54,40488,40489],{"class":64},"my_list ",[54,40491,82],{"class":81},[54,40493,818],{"class":88},[54,40495,100],{"class":99},[54,40497,103],{"class":88},[54,40499,466],{"class":99},[54,40501,103],{"class":88},[54,40503,379],{"class":99},[54,40505,469],{"class":88},[54,40507,40508,40510,40512,40514,40516,40518,40520,40523],{"class":56,"line":112},[54,40509,3833],{"class":64},[54,40511,82],{"class":81},[54,40513,85],{"class":64},[54,40515,89],{"class":88},[54,40517,39926],{"class":92},[54,40519,96],{"class":88},[54,40521,40522],{"class":92},"my_list",[54,40524,109],{"class":88},[54,40526,40527,40529,40531,40533],{"class":56,"line":887},[54,40528,116],{"class":115},[54,40530,96],{"class":88},[54,40532,3866],{"class":92},[54,40534,109],{"class":88},[14,40536,449],{},[45,40538,40539],{"className":47,"code":16695,"language":49,"meta":50,"style":50},[17,40540,40541],{"__ignoreMap":50},[54,40542,40543],{"class":56,"line":57},[54,40544,16695],{"class":8608},[14,40546,40547,40548,40550],{},"Why? Because ",[17,40549,39979],{}," changes the original list directly.",[14,40552,1310],{},[45,40554,40556],{"className":47,"code":40555,"language":49,"meta":50,"style":50},"import random\n\nmy_list = [1, 2, 3]\nrandom.shuffle(my_list)\nprint(my_list)\n",[17,40557,40558,40564,40568,40588,40602],{"__ignoreMap":50},[54,40559,40560,40562],{"class":56,"line":57},[54,40561,61],{"class":60},[54,40563,65],{"class":64},[54,40565,40566],{"class":56,"line":68},[54,40567,72],{"emptyLinePlaceholder":71},[54,40569,40570,40572,40574,40576,40578,40580,40582,40584,40586],{"class":56,"line":75},[54,40571,40489],{"class":64},[54,40573,82],{"class":81},[54,40575,818],{"class":88},[54,40577,100],{"class":99},[54,40579,103],{"class":88},[54,40581,466],{"class":99},[54,40583,103],{"class":88},[54,40585,379],{"class":99},[54,40587,469],{"class":88},[54,40589,40590,40592,40594,40596,40598,40600],{"class":56,"line":112},[54,40591,26],{"class":64},[54,40593,89],{"class":88},[54,40595,39926],{"class":92},[54,40597,96],{"class":88},[54,40599,40522],{"class":92},[54,40601,109],{"class":88},[54,40603,40604,40606,40608,40610],{"class":56,"line":887},[54,40605,116],{"class":115},[54,40607,96],{"class":88},[54,40609,40522],{"class":92},[54,40611,109],{"class":88},[602,40613,5314,40615,134,40617],{"id":40614},"mixing-up-randint-and-randrange",[17,40616,792],{},[17,40618,40619],{},"randrange()",[14,40621,40622],{},"These two functions are similar, but they do not behave exactly the same.",[45,40624,40626],{"className":47,"code":40625,"language":49,"meta":50,"style":50},"import random\n\nprint(random.randint(1, 10))   # can return 10\nprint(random.randrange(1, 10)) # cannot return 10\n",[17,40627,40628,40634,40638,40663],{"__ignoreMap":50},[54,40629,40630,40632],{"class":56,"line":57},[54,40631,61],{"class":60},[54,40633,65],{"class":64},[54,40635,40636],{"class":56,"line":68},[54,40637,72],{"emptyLinePlaceholder":71},[54,40639,40640,40642,40644,40646,40648,40650,40652,40654,40656,40658,40660],{"class":56,"line":75},[54,40641,116],{"class":115},[54,40643,96],{"class":88},[54,40645,26],{"class":92},[54,40647,89],{"class":88},[54,40649,93],{"class":92},[54,40651,96],{"class":88},[54,40653,100],{"class":99},[54,40655,103],{"class":88},[54,40657,106],{"class":99},[54,40659,4310],{"class":88},[54,40661,40662],{"class":4313},"   # can return 10\n",[54,40664,40665,40667,40669,40671,40673,40675,40677,40679,40681,40683,40685],{"class":56,"line":112},[54,40666,116],{"class":115},[54,40668,96],{"class":88},[54,40670,26],{"class":92},[54,40672,89],{"class":88},[54,40674,39381],{"class":92},[54,40676,96],{"class":88},[54,40678,100],{"class":99},[54,40680,103],{"class":88},[54,40682,106],{"class":99},[54,40684,4310],{"class":88},[54,40686,40687],{"class":4313}," # cannot return 10\n",[14,40689,40690],{},"This difference can cause off-by-one mistakes.",[40,40692,978],{"id":977},[14,40694,40695],{},"These are some beginner mistakes to watch for:",[149,40697,40698,40702,40710,40717,40722],{},[152,40699,5481,40700,40341],{},[17,40701,973],{},[152,40703,23093,40704,3360,40707],{},[17,40705,40706],{},"random(1, 10)",[17,40708,40709],{},"random.randint(1, 10)",[152,40711,40712,40713,40716],{},"Assigning ",[17,40714,40715],{},"result = random.shuffle(my_list)"," and expecting a shuffled list back",[152,40718,5481,40719,40721],{},[17,40720,26],{}," for passwords or secure tokens",[152,40723,40724,40725,40727],{},"Forgetting that ",[17,40726,792],{}," includes the end value",[14,40729,40730,40731,89],{},"If you work with lists a lot, it helps to understand how they behave first. See ",[192,40732,40734],{"href":40733},"\u002Flearn\u002Fpython-lists-explained-beginner-guide\u002F","Python lists explained for beginners",[40,40736,40738],{"id":40737},"useful-test-commands","Useful test commands",[14,40740,40741],{},"You can quickly test the module from the command line:",[45,40743,40745],{"className":15497,"code":40744,"language":15499,"meta":50,"style":50},"python --version\npython -c \"import random; print(random.randint(1, 10))\"\npython -c \"import random; data=[1,2,3]; random.shuffle(data); print(data)\"\npython -c \"import random; print(random.choice(['a','b','c']))\"\n",[17,40746,40747,40753,40766,40779],{"__ignoreMap":50},[54,40748,40749,40751],{"class":56,"line":57},[54,40750,49],{"class":10811},[54,40752,35879],{"class":24323},[54,40754,40755,40757,40759,40761,40764],{"class":56,"line":68},[54,40756,49],{"class":10811},[54,40758,26050],{"class":24323},[54,40760,830],{"class":544},[54,40762,40763],{"class":548},"import random; print(random.randint(1, 10))",[54,40765,5239],{"class":544},[54,40767,40768,40770,40772,40774,40777],{"class":56,"line":75},[54,40769,49],{"class":10811},[54,40771,26050],{"class":24323},[54,40773,830],{"class":544},[54,40775,40776],{"class":548},"import random; data=[1,2,3]; random.shuffle(data); print(data)",[54,40778,5239],{"class":544},[54,40780,40781,40783,40785,40787,40790],{"class":56,"line":112},[54,40782,49],{"class":10811},[54,40784,26050],{"class":24323},[54,40786,830],{"class":544},[54,40788,40789],{"class":548},"import random; print(random.choice(['a','b','c']))",[54,40791,5239],{"class":544},[40,40793,1419],{"id":1418},[602,40795,40797],{"id":40796},"do-i-need-to-install-the-random-module","Do I need to install the random module?",[14,40799,18143,40800,40802],{},[17,40801,26],{}," is part of Python's standard library.",[602,40804,8075,40806,134,40808,3409],{"id":40805},"what-is-the-difference-between-randint-and-randrange",[17,40807,792],{},[17,40809,40619],{},[14,40811,40812,40815],{},[17,40813,40814],{},"randint(a, b)"," includes both ends.",[14,40817,40818,40820,40821,40823],{},[17,40819,40619],{}," works more like ",[17,40822,410],{}," and usually excludes the stop value.",[602,40825,3403,40827,23554,40829,3409],{"id":40826},"why-does-shuffle-return-none",[17,40828,39979],{},[17,40830,8775],{},[14,40832,40833],{},"Because it changes the original list directly instead of creating a new one.",[602,40835,8091,40837,40839],{"id":40836},"can-randomchoice-work-with-strings",[17,40838,973],{}," work with strings?",[14,40841,40842,40843,40845],{},"Yes. A string is a sequence, so ",[17,40844,39086],{}," can return one random character.",[602,40847,40849,40850,40852],{"id":40848},"should-i-use-random-for-passwords","Should I use ",[17,40851,26],{}," for passwords?",[14,40854,40855,40856,40858],{},"No. Use the ",[17,40857,1412],{}," module for security-related values.",[40,40860,1510],{"id":1509},[149,40862,40863,40867,40871,40875,40879,40884],{},[152,40864,40865],{},[192,40866,39341],{"href":1579},[152,40868,40869],{},[192,40870,39604],{"href":970},[152,40872,40873],{},[192,40874,39986],{"href":39985},[152,40876,40877],{},[192,40878,40734],{"href":40733},[152,40880,40881],{},[192,40882,40883],{"href":19710},"How to fix NameError: name is not defined",[152,40885,40886],{},[192,40887,40889],{"href":40888},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained","How to fix IndexError: list index out of range",[14,40891,40892,40893,5507,40895,40897],{},"Next, read a focused function page like ",[17,40894,19],{},[17,40896,973],{},", then try a small beginner project such as a number guessing game.",[1545,40899,40900],{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .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 .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":40902},[40903,40904,40905,40906,40915,40916,40917,40927,40928,40929,40940],{"id":3595,"depth":68,"text":3596},{"id":39090,"depth":68,"text":39091},{"id":39158,"depth":68,"text":39159},{"id":39200,"depth":68,"text":39201,"children":40907},[40908,40909,40910,40911,40912,40913,40914],{"id":39209,"depth":75,"text":39212},{"id":39272,"depth":75,"text":39275},{"id":39344,"depth":75,"text":39347},{"id":39464,"depth":75,"text":39467},{"id":39607,"depth":75,"text":39610},{"id":39753,"depth":75,"text":39756},{"id":39865,"depth":75,"text":39868},{"id":39989,"depth":68,"text":39990},{"id":40219,"depth":68,"text":40220},{"id":40271,"depth":68,"text":40272,"children":40918},[40919,40921,40923,40925],{"id":40279,"depth":75,"text":40920},"Forgetting to import random",{"id":40338,"depth":75,"text":40922},"Using choice() with an empty list",{"id":40463,"depth":75,"text":40924},"Expecting shuffle() to return a new list",{"id":40614,"depth":75,"text":40926},"Mixing up randint() and randrange()",{"id":977,"depth":68,"text":978},{"id":40737,"depth":68,"text":40738},{"id":1418,"depth":68,"text":1419,"children":40930},[40931,40932,40934,40936,40938],{"id":40796,"depth":75,"text":40797},{"id":40805,"depth":75,"text":40933},"What is the difference between randint() and randrange()?",{"id":40826,"depth":75,"text":40935},"Why does shuffle() return None?",{"id":40836,"depth":75,"text":40937},"Can random.choice() work with strings?",{"id":40848,"depth":75,"text":40939},"Should I use random for passwords?",{"id":1509,"depth":68,"text":1510},"Master python random module overview in our comprehensive Python beginner guide.",{},{"title":38950,"description":40941},"standard-library\u002Fpython-random-module-overview","fgpugUWJ2LNurBHjQsthmJ4dN13MBowSf7KGYaNXsDE",{"id":40947,"title":40948,"body":40949,"description":42315,"extension":1577,"meta":42316,"navigation":71,"path":42317,"seo":42318,"stem":42319,"__hash__":42320},"content\u002Fstandard-library\u002Fpython-sys-module-overview.md","Python sys Module Overview",{"type":7,"value":40950,"toc":42295},[40951,40954,40960,40964,40984,40990,40992,41053,41059,41063,41068,41071,41088,41091,41102,41106,41111,41142,41153,41157,41163,41209,41213,41220,41282,41284,41335,41338,41342,41347,41364,41367,41507,41510,41525,41528,41585,41594,41598,41603,41715,41718,41735,41738,41778,41783,41787,41792,41794,41836,41841,41844,41860,41868,41882,41886,41891,41914,41917,42014,42024,42026,42030,42062,42065,42150,42153,42217,42219,42223,42226,42230,42235,42239,42242,42246,42249,42253,42256,42258,42292],[10,40952,40948],{"id":40953},"python-sys-module-overview",[14,40955,28340,40956,40959],{},[17,40957,40958],{},"sys"," module is a built-in Python standard library module. It gives you access to information about the Python interpreter and some parts of the program runtime.",[14,40961,24371,40962,16963],{},[17,40963,40958],{},[149,40965,40966,40969,40975,40978,40981],{},[152,40967,40968],{},"reading command-line arguments",[152,40970,40971,40972],{},"stopping a program with ",[17,40973,40974],{},"sys.exit()",[152,40976,40977],{},"checking the Python version",[152,40979,40980],{},"checking the platform",[152,40982,40983],{},"understanding where Python looks for modules",[14,40985,40986,40987,40989],{},"This page is an overview of the most useful parts of ",[17,40988,40958],{},". It is not a full reference for every attribute in the module.",[40,40991,3596],{"id":3595},[45,40993,40995],{"className":47,"code":40994,"language":49,"meta":50,"style":50},"import sys\n\nprint(sys.version)\nprint(sys.platform)\nprint(sys.argv)\n",[17,40996,40997,41004,41008,41023,41038],{"__ignoreMap":50},[54,40998,40999,41001],{"class":56,"line":57},[54,41000,61],{"class":60},[54,41002,41003],{"class":64}," sys\n",[54,41005,41006],{"class":56,"line":68},[54,41007,72],{"emptyLinePlaceholder":71},[54,41009,41010,41012,41014,41016,41018,41021],{"class":56,"line":75},[54,41011,116],{"class":115},[54,41013,96],{"class":88},[54,41015,40958],{"class":92},[54,41017,89],{"class":88},[54,41019,41020],{"class":1693},"version",[54,41022,109],{"class":88},[54,41024,41025,41027,41029,41031,41033,41036],{"class":56,"line":112},[54,41026,116],{"class":115},[54,41028,96],{"class":88},[54,41030,40958],{"class":92},[54,41032,89],{"class":88},[54,41034,41035],{"class":1693},"platform",[54,41037,109],{"class":88},[54,41039,41040,41042,41044,41046,41048,41051],{"class":56,"line":887},[54,41041,116],{"class":115},[54,41043,96],{"class":88},[54,41045,40958],{"class":92},[54,41047,89],{"class":88},[54,41049,41050],{"class":1693},"argv",[54,41052,109],{"class":88},[14,41054,41055,41056,41058],{},"Use this short example to see basic information from the ",[17,41057,40958],{}," module and inspect command-line arguments.",[40,41060,41062],{"id":41061},"what-the-sys-module-is","What the sys module is",[14,41064,41065,41067],{},[17,41066,40958],{}," is part of Python's standard library, so you do not need to install it.",[14,41069,41070],{},"It gives access to:",[149,41072,41073,41076,41079,41082,41085],{},[152,41074,41075],{},"Python interpreter settings",[152,41077,41078],{},"runtime information",[152,41080,41081],{},"command-line arguments",[152,41083,41084],{},"program exit behavior",[152,41086,41087],{},"module search paths",[14,41089,41090],{},"You must import it before using it:",[45,41092,41094],{"className":47,"code":41093,"language":49,"meta":50,"style":50},"import sys\n",[17,41095,41096],{"__ignoreMap":50},[54,41097,41098,41100],{"class":56,"line":57},[54,41099,61],{"class":60},[54,41101,41003],{"class":64},[40,41103,41105],{"id":41104},"when-to-use-sys","When to use sys",[14,41107,126,41108,41110],{},[17,41109,40958],{}," when you need one of these common tasks:",[149,41112,41113,41119,41124,41130,41136],{},[152,41114,126,41115,41118],{},[17,41116,41117],{},"sys.argv"," to read command-line arguments.",[152,41120,126,41121,41123],{},[17,41122,40974],{}," to stop a program intentionally.",[152,41125,126,41126,41129],{},[17,41127,41128],{},"sys.version"," to check the Python version.",[152,41131,126,41132,41135],{},[17,41133,41134],{},"sys.path"," to see where Python looks for modules.",[152,41137,126,41138,41141],{},[17,41139,41140],{},"sys.platform"," to detect the operating system in simple scripts.",[14,41143,41144,41145,5507,41149,89],{},"If you only need one specific part, it can help to read a focused page such as ",[192,41146,41148],{"href":41147},"\u002Fstandard-library\u002Fsys.argv-explained","sys.argv explained",[192,41150,41152],{"href":41151},"\u002Fstandard-library\u002Fsys.exit-function-explained","sys.exit() explained",[40,41154,41156],{"id":41155},"commonly-used-parts-of-sys","Commonly used parts of sys",[14,41158,41159,41160,41162],{},"Here are the ",[17,41161,40958],{}," features beginners use most often:",[149,41164,41165,41172,41179,41186,41202],{},[152,41166,41167,41169,41171],{},[17,41168,41117],{},[21011,41170],{},"\nA list of command-line arguments.",[152,41173,41174,41176,41178],{},[17,41175,40974],{},[21011,41177],{},"\nStops the program and can return an exit code.",[152,41180,41181,41183,41185],{},[17,41182,41128],{},[21011,41184],{},"\nA full string showing the Python version.",[152,41187,41188,41190,41192,41193,320,41196,324,41199,89],{},[17,41189,41140],{},[21011,41191],{},"\nA short platform name such as ",[17,41194,41195],{},"win32",[17,41197,41198],{},"linux",[17,41200,41201],{},"darwin",[152,41203,41204,41206,41208],{},[17,41205,41134],{},[21011,41207],{},"\nA list of folders Python searches when importing modules.",[40,41210,41212],{"id":41211},"example-checking-python-version-and-platform","Example: checking Python version and platform",[14,41214,789,41215,134,41217,41219],{},[17,41216,41128],{},[17,41218,41140],{}," to quickly inspect your Python environment.",[45,41221,41223],{"className":47,"code":41222,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Python version:\", sys.version)\nprint(\"Platform:\", sys.platform)\n",[17,41224,41225,41231,41235,41259],{"__ignoreMap":50},[54,41226,41227,41229],{"class":56,"line":57},[54,41228,61],{"class":60},[54,41230,41003],{"class":64},[54,41232,41233],{"class":56,"line":68},[54,41234,72],{"emptyLinePlaceholder":71},[54,41236,41237,41239,41241,41243,41246,41248,41250,41253,41255,41257],{"class":56,"line":75},[54,41238,116],{"class":115},[54,41240,96],{"class":88},[54,41242,545],{"class":544},[54,41244,41245],{"class":548},"Python version:",[54,41247,545],{"class":544},[54,41249,103],{"class":88},[54,41251,41252],{"class":92}," sys",[54,41254,89],{"class":88},[54,41256,41020],{"class":1693},[54,41258,109],{"class":88},[54,41260,41261,41263,41265,41267,41270,41272,41274,41276,41278,41280],{"class":56,"line":112},[54,41262,116],{"class":115},[54,41264,96],{"class":88},[54,41266,545],{"class":544},[54,41268,41269],{"class":548},"Platform:",[54,41271,545],{"class":544},[54,41273,103],{"class":88},[54,41275,41252],{"class":92},[54,41277,89],{"class":88},[54,41279,41035],{"class":1693},[54,41281,109],{"class":88},[14,41283,561],{},[45,41285,41287],{"className":47,"code":41286,"language":49,"meta":50,"style":50},"Python version: 3.12.2 (main, Feb  6 2024, ...)\nPlatform: win32\n",[17,41288,41289,41325],{"__ignoreMap":50},[54,41290,41291,41294,41296,41299,41301,41303,41306,41308,41310,41313,41315,41318,41320,41323],{"class":56,"line":57},[54,41292,41293],{"class":64},"Python version",[54,41295,147],{"class":88},[54,41297,41298],{"class":99}," 3.12",[54,41300,89],{"class":88},[54,41302,396],{"class":1693},[54,41304,41305],{"class":88}," (",[54,41307,26893],{"class":64},[54,41309,103],{"class":88},[54,41311,41312],{"class":64}," Feb  ",[54,41314,323],{"class":99},[54,41316,41317],{"class":99}," 2024",[54,41319,103],{"class":88},[54,41321,41322],{"class":4870}," ...",[54,41324,109],{"class":88},[54,41326,41327,41330,41332],{"class":56,"line":68},[54,41328,41329],{"class":64},"Platform",[54,41331,147],{"class":88},[54,41333,41334],{"class":64}," win32\n",[14,41336,41337],{},"This is useful when code behaves differently on different systems, or when you need to confirm which Python version is running.",[40,41339,41341],{"id":41340},"example-reading-command-line-arguments","Example: reading command-line arguments",[14,41343,41344,41346],{},[17,41345,41117],{}," is a list.",[149,41348,41349,41355,41358],{},[152,41350,41351,41354],{},[17,41352,41353],{},"sys.argv[0]"," is usually the script name.",[152,41356,41357],{},"Values after the script name are extra arguments.",[152,41359,41360,41361,41363],{},"All values in ",[17,41362,41117],{}," are strings.",[14,41365,41366],{},"Example script:",[45,41368,41370],{"className":47,"code":41369,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Full argv list:\", sys.argv)\nprint(\"Script name:\", sys.argv[0])\n\nif len(sys.argv) > 1:\n    print(\"First extra argument:\", sys.argv[1])\nelse:\n    print(\"No extra argument was provided.\")\n",[17,41371,41372,41378,41382,41405,41432,41436,41459,41486,41492],{"__ignoreMap":50},[54,41373,41374,41376],{"class":56,"line":57},[54,41375,61],{"class":60},[54,41377,41003],{"class":64},[54,41379,41380],{"class":56,"line":68},[54,41381,72],{"emptyLinePlaceholder":71},[54,41383,41384,41386,41388,41390,41393,41395,41397,41399,41401,41403],{"class":56,"line":75},[54,41385,116],{"class":115},[54,41387,96],{"class":88},[54,41389,545],{"class":544},[54,41391,41392],{"class":548},"Full argv list:",[54,41394,545],{"class":544},[54,41396,103],{"class":88},[54,41398,41252],{"class":92},[54,41400,89],{"class":88},[54,41402,41050],{"class":1693},[54,41404,109],{"class":88},[54,41406,41407,41409,41411,41413,41416,41418,41420,41422,41424,41426,41428,41430],{"class":56,"line":112},[54,41408,116],{"class":115},[54,41410,96],{"class":88},[54,41412,545],{"class":544},[54,41414,41415],{"class":548},"Script name:",[54,41417,545],{"class":544},[54,41419,103],{"class":88},[54,41421,41252],{"class":92},[54,41423,89],{"class":88},[54,41425,41050],{"class":1693},[54,41427,459],{"class":88},[54,41429,752],{"class":99},[54,41431,937],{"class":88},[54,41433,41434],{"class":56,"line":887},[54,41435,72],{"emptyLinePlaceholder":71},[54,41437,41438,41440,41442,41444,41446,41448,41450,41452,41455,41457],{"class":56,"line":892},[54,41439,22942],{"class":60},[54,41441,868],{"class":115},[54,41443,96],{"class":88},[54,41445,40958],{"class":92},[54,41447,89],{"class":88},[54,41449,41050],{"class":1693},[54,41451,876],{"class":88},[54,41453,41454],{"class":81}," >",[54,41456,882],{"class":99},[54,41458,7572],{"class":88},[54,41460,41461,41463,41465,41467,41470,41472,41474,41476,41478,41480,41482,41484],{"class":56,"line":913},[54,41462,7606],{"class":115},[54,41464,96],{"class":88},[54,41466,545],{"class":544},[54,41468,41469],{"class":548},"First extra argument:",[54,41471,545],{"class":544},[54,41473,103],{"class":88},[54,41475,41252],{"class":92},[54,41477,89],{"class":88},[54,41479,41050],{"class":1693},[54,41481,459],{"class":88},[54,41483,100],{"class":99},[54,41485,937],{"class":88},[54,41487,41488,41490],{"class":56,"line":2121},[54,41489,22975],{"class":60},[54,41491,7572],{"class":88},[54,41493,41494,41496,41498,41500,41503,41505],{"class":56,"line":2136},[54,41495,7606],{"class":115},[54,41497,96],{"class":88},[54,41499,545],{"class":544},[54,41501,41502],{"class":548},"No extra argument was provided.",[54,41504,545],{"class":544},[54,41506,109],{"class":88},[14,41508,41509],{},"If you run:",[45,41511,41513],{"className":15497,"code":41512,"language":15499,"meta":50,"style":50},"python your_script.py hello\n",[17,41514,41515],{"__ignoreMap":50},[54,41516,41517,41519,41522],{"class":56,"line":57},[54,41518,49],{"class":10811},[54,41520,41521],{"class":548}," your_script.py",[54,41523,41524],{"class":548}," hello\n",[14,41526,41527],{},"You will usually get output like:",[45,41529,41531],{"className":47,"code":41530,"language":49,"meta":50,"style":50},"Full argv list: ['your_script.py', 'hello']\nScript name: your_script.py\nFirst extra argument: hello\n",[17,41532,41533,41562,41576],{"__ignoreMap":50},[54,41534,41535,41538,41540,41542,41544,41546,41549,41551,41553,41555,41558,41560],{"class":56,"line":57},[54,41536,41537],{"class":64},"Full argv ",[54,41539,430],{"class":429},[54,41541,147],{"class":88},[54,41543,818],{"class":88},[54,41545,2240],{"class":544},[54,41547,41548],{"class":548},"your_script.py",[54,41550,2240],{"class":544},[54,41552,103],{"class":88},[54,41554,2234],{"class":544},[54,41556,41557],{"class":548},"hello",[54,41559,2240],{"class":544},[54,41561,469],{"class":88},[54,41563,41564,41567,41569,41572,41574],{"class":56,"line":68},[54,41565,41566],{"class":64},"Script name",[54,41568,147],{"class":88},[54,41570,41571],{"class":64}," your_script",[54,41573,89],{"class":88},[54,41575,25591],{"class":1693},[54,41577,41578,41581,41583],{"class":56,"line":75},[54,41579,41580],{"class":64},"First extra argument",[54,41582,147],{"class":88},[54,41584,41524],{"class":64},[14,41586,41587,41588,5507,41590,89],{},"If you need more detail, see ",[192,41589,41148],{"href":41147},[192,41591,41593],{"href":41592},"\u002Fhow-to\u002Fhow-to-use-command-line-arguments-in-python","how to use command-line arguments in Python",[40,41595,41597],{"id":41596},"example-exiting-a-program","Example: exiting a program",[14,41599,126,41600,41602],{},[17,41601,40974],{}," when you want to stop a script early.",[45,41604,41606],{"className":47,"code":41605,"language":49,"meta":50,"style":50},"import sys\n\nname = input(\"Enter your name: \").strip()\n\nif name == \"\":\n    print(\"Name is required.\")\n    sys.exit(1)\n\nprint(\"Hello,\", name)\n",[17,41607,41608,41614,41618,41644,41648,41661,41676,41692,41696],{"__ignoreMap":50},[54,41609,41610,41612],{"class":56,"line":57},[54,41611,61],{"class":60},[54,41613,41003],{"class":64},[54,41615,41616],{"class":56,"line":68},[54,41617,72],{"emptyLinePlaceholder":71},[54,41619,41620,41623,41625,41627,41629,41631,41634,41636,41639,41642],{"class":56,"line":75},[54,41621,41622],{"class":64},"name ",[54,41624,82],{"class":81},[54,41626,20217],{"class":115},[54,41628,96],{"class":88},[54,41630,545],{"class":544},[54,41632,41633],{"class":548},"Enter your name: ",[54,41635,545],{"class":544},[54,41637,41638],{"class":88},").",[54,41640,41641],{"class":92},"strip",[54,41643,1670],{"class":88},[54,41645,41646],{"class":56,"line":112},[54,41647,72],{"emptyLinePlaceholder":71},[54,41649,41650,41652,41654,41657,41659],{"class":56,"line":887},[54,41651,22942],{"class":60},[54,41653,26862],{"class":64},[54,41655,41656],{"class":81},"==",[54,41658,30180],{"class":544},[54,41660,7572],{"class":88},[54,41662,41663,41665,41667,41669,41672,41674],{"class":56,"line":892},[54,41664,7606],{"class":115},[54,41666,96],{"class":88},[54,41668,545],{"class":544},[54,41670,41671],{"class":548},"Name is required.",[54,41673,545],{"class":544},[54,41675,109],{"class":88},[54,41677,41678,41681,41683,41686,41688,41690],{"class":56,"line":913},[54,41679,41680],{"class":64},"    sys",[54,41682,89],{"class":88},[54,41684,41685],{"class":92},"exit",[54,41687,96],{"class":88},[54,41689,100],{"class":99},[54,41691,109],{"class":88},[54,41693,41694],{"class":56,"line":2121},[54,41695,72],{"emptyLinePlaceholder":71},[54,41697,41698,41700,41702,41704,41707,41709,41711,41713],{"class":56,"line":2136},[54,41699,116],{"class":115},[54,41701,96],{"class":88},[54,41703,545],{"class":544},[54,41705,41706],{"class":548},"Hello,",[54,41708,545],{"class":544},[54,41710,103],{"class":88},[54,41712,10834],{"class":92},[54,41714,109],{"class":88},[14,41716,41717],{},"How this works:",[149,41719,41720,41723,41729],{},[152,41721,41722],{},"If the user enters nothing, the script prints a message and stops.",[152,41724,41725,41728],{},[17,41726,41727],{},"sys.exit(1)"," usually means the program ended with an error.",[152,41730,41731,41734],{},[17,41732,41733],{},"sys.exit(0)"," usually means success.",[14,41736,41737],{},"You can also exit without a code:",[45,41739,41741],{"className":47,"code":41740,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Stopping the program now.\")\nsys.exit()\n",[17,41742,41743,41749,41753,41768],{"__ignoreMap":50},[54,41744,41745,41747],{"class":56,"line":57},[54,41746,61],{"class":60},[54,41748,41003],{"class":64},[54,41750,41751],{"class":56,"line":68},[54,41752,72],{"emptyLinePlaceholder":71},[54,41754,41755,41757,41759,41761,41764,41766],{"class":56,"line":75},[54,41756,116],{"class":115},[54,41758,96],{"class":88},[54,41760,545],{"class":544},[54,41762,41763],{"class":548},"Stopping the program now.",[54,41765,545],{"class":544},[54,41767,109],{"class":88},[54,41769,41770,41772,41774,41776],{"class":56,"line":112},[54,41771,40958],{"class":64},[54,41773,89],{"class":88},[54,41775,41685],{"class":92},[54,41777,1670],{"class":88},[14,41779,41780,41781,89],{},"For a deeper explanation, see ",[192,41782,41152],{"href":41151},[40,41784,41786],{"id":41785},"understanding-syspath-at-a-beginner-level","Understanding sys.path at a beginner level",[14,41788,41789,41791],{},[17,41790,41134],{}," is the list of locations Python checks when you import modules.",[14,41793,253],{},[45,41795,41797],{"className":47,"code":41796,"language":49,"meta":50,"style":50},"import sys\n\nfor path in sys.path:\n    print(path)\n",[17,41798,41799,41805,41809,41826],{"__ignoreMap":50},[54,41800,41801,41803],{"class":56,"line":57},[54,41802,61],{"class":60},[54,41804,41003],{"class":64},[54,41806,41807],{"class":56,"line":68},[54,41808,72],{"emptyLinePlaceholder":71},[54,41810,41811,41813,41816,41818,41820,41822,41824],{"class":56,"line":75},[54,41812,26859],{"class":60},[54,41814,41815],{"class":64}," path ",[54,41817,26865],{"class":60},[54,41819,41252],{"class":64},[54,41821,89],{"class":88},[54,41823,23896],{"class":1693},[54,41825,7572],{"class":88},[54,41827,41828,41830,41832,41834],{"class":56,"line":112},[54,41829,7606],{"class":115},[54,41831,96],{"class":88},[54,41833,23896],{"class":92},[54,41835,109],{"class":88},[14,41837,41838,41839,89],{},"This helps explain some import problems. If Python cannot find a module, one possible reason is that the module is not in one of the folders listed in ",[17,41840,41134],{},[14,41842,41843],{},"For beginners, the main idea is:",[149,41845,41846,41851,41854],{},[152,41847,41848,41850],{},[17,41849,41134],{}," affects imports",[152,41852,41853],{},"import errors often relate to search paths",[152,41855,41856,41857,41859],{},"you usually should not modify ",[17,41858,41134],{}," unless you understand why",[14,41861,41862,41863,41867],{},"If imports are confusing, read ",[192,41864,41866],{"href":41865},"\u002Flearn\u002Fhow-import-works-in-python","how import works in Python",". If you are fixing an error, these pages may help:",[149,41869,41870,41876],{},[152,41871,41872],{},[192,41873,41875],{"href":41874},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix","ModuleNotFoundError: No module named X fix",[152,41877,41878],{},[192,41879,41881],{"href":41880},"\u002Ferrors\u002Fimporterror-no-module-named-x-fix","ImportError: No module named X fix",[40,41883,41885],{"id":41884},"beginner-tips","Beginner tips",[14,41887,41888,41889,147],{},"Keep these points in mind when using ",[17,41890,40958],{},[149,41892,41893,41899,41904,41907],{},[152,41894,41895,41896,20375],{},"Always write ",[17,41897,41898],{},"import sys",[152,41900,41901,41903],{},[17,41902,41117],{}," values are strings.",[152,41905,41906],{},"Use conversion functions if you need numbers.",[152,41908,41909,41910,41913],{},"For larger command-line programs, ",[17,41911,41912],{},"argparse"," is usually a better choice.",[14,41915,41916],{},"Example of converting an argument to a number:",[45,41918,41920],{"className":47,"code":41919,"language":49,"meta":50,"style":50},"import sys\n\nif len(sys.argv) > 1:\n    number = int(sys.argv[1])\n    print(number * 2)\nelse:\n    print(\"Please provide a number.\")\n",[17,41921,41922,41928,41932,41954,41978,41993,41999],{"__ignoreMap":50},[54,41923,41924,41926],{"class":56,"line":57},[54,41925,61],{"class":60},[54,41927,41003],{"class":64},[54,41929,41930],{"class":56,"line":68},[54,41931,72],{"emptyLinePlaceholder":71},[54,41933,41934,41936,41938,41940,41942,41944,41946,41948,41950,41952],{"class":56,"line":75},[54,41935,22942],{"class":60},[54,41937,868],{"class":115},[54,41939,96],{"class":88},[54,41941,40958],{"class":92},[54,41943,89],{"class":88},[54,41945,41050],{"class":1693},[54,41947,876],{"class":88},[54,41949,41454],{"class":81},[54,41951,882],{"class":99},[54,41953,7572],{"class":88},[54,41955,41956,41959,41961,41964,41966,41968,41970,41972,41974,41976],{"class":56,"line":112},[54,41957,41958],{"class":64},"    number ",[54,41960,82],{"class":81},[54,41962,41963],{"class":429}," int",[54,41965,96],{"class":88},[54,41967,40958],{"class":92},[54,41969,89],{"class":88},[54,41971,41050],{"class":1693},[54,41973,459],{"class":88},[54,41975,100],{"class":99},[54,41977,937],{"class":88},[54,41979,41980,41982,41984,41986,41989,41991],{"class":56,"line":887},[54,41981,7606],{"class":115},[54,41983,96],{"class":88},[54,41985,78],{"class":92},[54,41987,41988],{"class":81},"*",[54,41990,466],{"class":99},[54,41992,109],{"class":88},[54,41994,41995,41997],{"class":56,"line":892},[54,41996,22975],{"class":60},[54,41998,7572],{"class":88},[54,42000,42001,42003,42005,42007,42010,42012],{"class":56,"line":913},[54,42002,7606],{"class":115},[54,42004,96],{"class":88},[54,42006,545],{"class":544},[54,42008,42009],{"class":548},"Please provide a number.",[54,42011,545],{"class":544},[54,42013,109],{"class":88},[14,42015,42016,42017,42019,42020,89],{},"Here, ",[17,42018,20065],{}," converts the string argument into an integer. If you are not familiar with that conversion, see ",[192,42021,42023],{"href":42022},"\u002Freference\u002Fpython-int-function-explained","Python int() explained",[40,42025,978],{"id":977},[14,42027,25908,42028,147],{},[17,42029,40958],{},[149,42031,42032,42042,42045,42051,42056],{},[152,42033,42034,42035,42037,42038,5507,42040],{},"Forgetting to write ",[17,42036,41898],{}," before using ",[17,42039,41117],{},[17,42041,40974],{},[152,42043,42044],{},"Assuming command-line arguments are numbers instead of strings",[152,42046,5481,42047,42050],{},[17,42048,42049],{},"sys.argv[1]"," when no extra argument was provided",[152,42052,20123,42053,42055],{},[17,42054,41134],{}," with the current working directory",[152,42057,42058,42059,42061],{},"Trying to call ",[17,42060,40958],{}," as if it were a function",[14,42063,42064],{},"A common safe pattern is to check the length first:",[45,42066,42068],{"className":47,"code":42067,"language":49,"meta":50,"style":50},"import sys\n\nif len(sys.argv) > 1:\n    print(\"Argument:\", sys.argv[1])\nelse:\n    print(\"No argument provided.\")\n",[17,42069,42070,42076,42080,42102,42129,42135],{"__ignoreMap":50},[54,42071,42072,42074],{"class":56,"line":57},[54,42073,61],{"class":60},[54,42075,41003],{"class":64},[54,42077,42078],{"class":56,"line":68},[54,42079,72],{"emptyLinePlaceholder":71},[54,42081,42082,42084,42086,42088,42090,42092,42094,42096,42098,42100],{"class":56,"line":75},[54,42083,22942],{"class":60},[54,42085,868],{"class":115},[54,42087,96],{"class":88},[54,42089,40958],{"class":92},[54,42091,89],{"class":88},[54,42093,41050],{"class":1693},[54,42095,876],{"class":88},[54,42097,41454],{"class":81},[54,42099,882],{"class":99},[54,42101,7572],{"class":88},[54,42103,42104,42106,42108,42110,42113,42115,42117,42119,42121,42123,42125,42127],{"class":56,"line":112},[54,42105,7606],{"class":115},[54,42107,96],{"class":88},[54,42109,545],{"class":544},[54,42111,42112],{"class":548},"Argument:",[54,42114,545],{"class":544},[54,42116,103],{"class":88},[54,42118,41252],{"class":92},[54,42120,89],{"class":88},[54,42122,41050],{"class":1693},[54,42124,459],{"class":88},[54,42126,100],{"class":99},[54,42128,937],{"class":88},[54,42130,42131,42133],{"class":56,"line":887},[54,42132,22975],{"class":60},[54,42134,7572],{"class":88},[54,42136,42137,42139,42141,42143,42146,42148],{"class":56,"line":892},[54,42138,7606],{"class":115},[54,42140,96],{"class":88},[54,42142,545],{"class":544},[54,42144,42145],{"class":548},"No argument provided.",[54,42147,545],{"class":544},[54,42149,109],{"class":88},[14,42151,42152],{},"Useful commands for testing:",[45,42154,42156],{"className":15497,"code":42155,"language":15499,"meta":50,"style":50},"python --version\npython your_script.py\npython your_script.py hello\npython -c \"import sys; print(sys.version)\"\npython -c \"import sys; print(sys.argv)\"\npython -c \"import sys; print(sys.path)\"\n",[17,42157,42158,42164,42170,42178,42191,42204],{"__ignoreMap":50},[54,42159,42160,42162],{"class":56,"line":57},[54,42161,49],{"class":10811},[54,42163,35879],{"class":24323},[54,42165,42166,42168],{"class":56,"line":68},[54,42167,49],{"class":10811},[54,42169,15508],{"class":548},[54,42171,42172,42174,42176],{"class":56,"line":75},[54,42173,49],{"class":10811},[54,42175,41521],{"class":548},[54,42177,41524],{"class":548},[54,42179,42180,42182,42184,42186,42189],{"class":56,"line":112},[54,42181,49],{"class":10811},[54,42183,26050],{"class":24323},[54,42185,830],{"class":544},[54,42187,42188],{"class":548},"import sys; print(sys.version)",[54,42190,5239],{"class":544},[54,42192,42193,42195,42197,42199,42202],{"class":56,"line":887},[54,42194,49],{"class":10811},[54,42196,26050],{"class":24323},[54,42198,830],{"class":544},[54,42200,42201],{"class":548},"import sys; print(sys.argv)",[54,42203,5239],{"class":544},[54,42205,42206,42208,42210,42212,42215],{"class":56,"line":892},[54,42207,49],{"class":10811},[54,42209,26050],{"class":24323},[54,42211,830],{"class":544},[54,42213,42214],{"class":548},"import sys; print(sys.path)",[54,42216,5239],{"class":544},[40,42218,1419],{"id":1418},[602,42220,42222],{"id":42221},"what-is-the-sys-module-in-python","What is the sys module in Python?",[14,42224,42225],{},"It is a standard library module that gives information about the Python interpreter and access to features like command-line arguments and program exit.",[602,42227,42229],{"id":42228},"do-i-need-to-install-sys","Do I need to install sys?",[14,42231,18143,42232,42234],{},[17,42233,40958],{}," is built into Python, so you only need to import it.",[602,42236,42238],{"id":42237},"what-does-sysargv-do","What does sys.argv do?",[14,42240,42241],{},"It stores command-line arguments in a list. The first item is usually the script name.",[602,42243,42245],{"id":42244},"what-does-sysexit-do","What does sys.exit() do?",[14,42247,42248],{},"It stops the program. You can also pass an exit code or message.",[602,42250,42252],{"id":42251},"what-is-syspath-used-for","What is sys.path used for?",[14,42254,42255],{},"It shows the folders Python searches when importing modules.",[40,42257,1510],{"id":1509},[149,42259,42260,42264,42268,42274,42279,42284,42288],{},[152,42261,42262],{},[192,42263,41148],{"href":41147},[152,42265,42266],{},[192,42267,41152],{"href":41151},[152,42269,42270],{},[192,42271,42273],{"href":42272},"\u002Fstandard-library\u002Fsys.path-explained","sys.path explained",[152,42275,42276],{},[192,42277,42278],{"href":41865},"How import works in Python",[152,42280,42281],{},[192,42282,42283],{"href":41592},"How to use command-line arguments in Python",[152,42285,42286],{},[192,42287,41875],{"href":41874},[152,42289,42290],{},[192,42291,42023],{"href":42022},[1545,42293,42294],{},"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 .light .shiki span {color: var(--shiki-light);background: var(--shiki-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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--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 .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":50,"searchDepth":68,"depth":68,"links":42296},[42297,42298,42299,42300,42301,42302,42303,42304,42305,42306,42307,42314],{"id":3595,"depth":68,"text":3596},{"id":41061,"depth":68,"text":41062},{"id":41104,"depth":68,"text":41105},{"id":41155,"depth":68,"text":41156},{"id":41211,"depth":68,"text":41212},{"id":41340,"depth":68,"text":41341},{"id":41596,"depth":68,"text":41597},{"id":41785,"depth":68,"text":41786},{"id":41884,"depth":68,"text":41885},{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":42308},[42309,42310,42311,42312,42313],{"id":42221,"depth":75,"text":42222},{"id":42228,"depth":75,"text":42229},{"id":42237,"depth":75,"text":42238},{"id":42244,"depth":75,"text":42245},{"id":42251,"depth":75,"text":42252},{"id":1509,"depth":68,"text":1510},"Master python sys module overview in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fpython-sys-module-overview",{"title":40948,"description":42315},"standard-library\u002Fpython-sys-module-overview","LkwcllMOEx69Sr6q2Idl2OewRGzyVHjhNVGjzZbNL4o",{"id":42322,"title":42323,"body":42324,"description":44114,"extension":1577,"meta":44115,"navigation":71,"path":44116,"seo":44117,"stem":44118,"__hash__":44119},"content\u002Fstandard-library\u002Fpython-time-module-overview.md","Python time Module Overview",{"type":7,"value":42325,"toc":44073},[42326,42329,42334,42337,42348,42356,42358,42432,42434,42448,42450,42473,42477,42482,42485,42495,42498,42509,42516,42528,42532,42537,42539,42561,42566,42577,42581,42587,42592,42595,42636,42641,42647,42650,42709,42712,42756,42762,42765,42813,42815,42842,42848,42851,42938,42941,42947,42950,43020,43022,43054,43058,43061,43081,43083,43221,43224,43238,43240,43245,43249,43308,43312,43315,43451,43455,43458,43543,43547,43585,43589,43592,43597,43607,43611,43620,43623,43641,43643,43648,43653,43657,43676,43678,43707,43713,43719,43752,43754,43783,43789,43794,43822,43829,43858,43867,43875,43882,43887,43944,43948,43951,44001,44003,44007,44012,44016,44019,44023,44026,44030,44038,44042,44048,44050,44070],[10,42327,42323],{"id":42328},"python-time-module-overview",[14,42330,28340,42331,42333],{},[17,42332,2559],{}," module is a built-in Python module for simple time-related tasks.",[14,42335,42336],{},"Beginners usually use it for:",[149,42338,42339,42342,42345],{},[152,42340,42341],{},"getting the current timestamp",[152,42343,42344],{},"pausing a program",[152,42346,42347],{},"measuring how long code takes to run",[14,42349,42350,42351,42355],{},"It is a good starting point when you need something simple. If you need readable dates, calendar values, or date math, the ",[192,42352,42353,13763],{"href":6351},[17,42354,1628],{}," is usually a better fit.",[40,42357,3596],{"id":3595},[45,42359,42361],{"className":47,"code":42360,"language":49,"meta":50,"style":50},"import time\n\nprint(time.time())\nprint(\"Waiting...\")\ntime.sleep(2)\nprint(\"Done\")\n",[17,42362,42363,42369,42373,42387,42402,42417],{"__ignoreMap":50},[54,42364,42365,42367],{"class":56,"line":57},[54,42366,61],{"class":60},[54,42368,31493],{"class":64},[54,42370,42371],{"class":56,"line":68},[54,42372,72],{"emptyLinePlaceholder":71},[54,42374,42375,42377,42379,42381,42383,42385],{"class":56,"line":75},[54,42376,116],{"class":115},[54,42378,96],{"class":88},[54,42380,2559],{"class":92},[54,42382,89],{"class":88},[54,42384,2559],{"class":92},[54,42386,1950],{"class":88},[54,42388,42389,42391,42393,42395,42398,42400],{"class":56,"line":112},[54,42390,116],{"class":115},[54,42392,96],{"class":88},[54,42394,545],{"class":544},[54,42396,42397],{"class":548},"Waiting...",[54,42399,545],{"class":544},[54,42401,109],{"class":88},[54,42403,42404,42406,42408,42411,42413,42415],{"class":56,"line":887},[54,42405,2559],{"class":64},[54,42407,89],{"class":88},[54,42409,42410],{"class":92},"sleep",[54,42412,96],{"class":88},[54,42414,396],{"class":99},[54,42416,109],{"class":88},[54,42418,42419,42421,42423,42425,42428,42430],{"class":56,"line":892},[54,42420,116],{"class":115},[54,42422,96],{"class":88},[54,42424,545],{"class":544},[54,42426,42427],{"class":548},"Done",[54,42429,545],{"class":544},[54,42431,109],{"class":88},[14,42433,29554],{},[149,42435,42436,42442],{},[152,42437,42438,42441],{},[17,42439,42440],{},"time.time()"," gets the current Unix timestamp",[152,42443,42444,42447],{},[17,42445,42446],{},"time.sleep(2)"," pauses the whole program for 2 seconds",[14,42449,561],{},[45,42451,42453],{"className":47,"code":42452,"language":49,"meta":50,"style":50},"1712345678.123456\nWaiting...\nDone\n",[17,42454,42455,42460,42468],{"__ignoreMap":50},[54,42456,42457],{"class":56,"line":57},[54,42458,42459],{"class":99},"1712345678.123456\n",[54,42461,42462,42465],{"class":56,"line":68},[54,42463,42464],{"class":64},"Waiting",[54,42466,42467],{"class":4870},"...\n",[54,42469,42470],{"class":56,"line":75},[54,42471,42472],{"class":64},"Done\n",[40,42474,42476],{"id":42475},"what-the-time-module-is","What the time module is",[14,42478,28340,42479,42481],{},[17,42480,2559],{}," module is built into Python, so you do not need to install anything.",[14,42483,42484],{},"It helps with:",[149,42486,42487,42489,42492],{},[152,42488,7270],{},[152,42490,42491],{},"delays",[152,42493,42494],{},"simple timing tasks",[14,42496,42497],{},"This makes it useful when you want to:",[149,42499,42500,42503,42506],{},[152,42501,42502],{},"pause code for a short time",[152,42504,42505],{},"measure how long something takes",[152,42507,42508],{},"get a numeric current time value",[14,42510,28340,42511,42513,42514,89],{},[17,42512,2559],{}," module is different from ",[17,42515,1628],{},[149,42517,42518,42523],{},[152,42519,42520,42522],{},[17,42521,2559],{}," is better for delays and simple timestamps",[152,42524,42525,42527],{},[17,42526,1628],{}," is better for readable dates like year, month, and day",[40,42529,42531],{"id":42530},"when-to-use-the-time-module","When to use the time module",[14,42533,36885,42534,42536],{},[17,42535,2559],{}," module when you need simple time features.",[14,42538,16064],{},[149,42540,42541,42550,42558],{},[152,42542,42543,42544],{},"Pause a program with ",[192,42545,42547],{"href":42546},"\u002Fstandard-library\u002Ftime.sleep-function-explained",[17,42548,42549],{},"time.sleep()",[152,42551,42552,42553],{},"Get the current timestamp with ",[192,42554,42556],{"href":42555},"\u002Fstandard-library\u002Ftime.time-function-explained",[17,42557,42440],{},[152,42559,42560],{},"Measure how long code takes to run",[14,42562,126,42563,42565],{},[17,42564,1628],{}," instead when you need:",[149,42567,42568,42571,42574],{},[152,42569,42570],{},"readable dates",[152,42572,42573],{},"year, month, and day values",[152,42575,42576],{},"date calculations",[40,42578,42580],{"id":42579},"functions-beginners-should-know-first","Functions beginners should know first",[14,42582,42583,42584,42586],{},"Here are the most useful ",[17,42585,2559],{}," module functions to learn first.",[602,42588,42590],{"id":42589},"timetime",[17,42591,42440],{},[14,42593,42594],{},"Returns the current time as the number of seconds since the Unix epoch.",[45,42596,42598],{"className":47,"code":42597,"language":49,"meta":50,"style":50},"import time\n\ncurrent_timestamp = time.time()\nprint(current_timestamp)\n",[17,42599,42600,42606,42610,42625],{"__ignoreMap":50},[54,42601,42602,42604],{"class":56,"line":57},[54,42603,61],{"class":60},[54,42605,31493],{"class":64},[54,42607,42608],{"class":56,"line":68},[54,42609,72],{"emptyLinePlaceholder":71},[54,42611,42612,42615,42617,42619,42621,42623],{"class":56,"line":75},[54,42613,42614],{"class":64},"current_timestamp ",[54,42616,82],{"class":81},[54,42618,31045],{"class":64},[54,42620,89],{"class":88},[54,42622,2559],{"class":92},[54,42624,1670],{"class":88},[54,42626,42627,42629,42631,42634],{"class":56,"line":112},[54,42628,116],{"class":115},[54,42630,96],{"class":88},[54,42632,42633],{"class":92},"current_timestamp",[54,42635,109],{"class":88},[14,42637,3079,42638,42640],{},[17,42639,9662],{},", not a formatted date string.",[602,42642,42644],{"id":42643},"timesleepseconds",[17,42645,42646],{},"time.sleep(seconds)",[14,42648,42649],{},"Pauses the whole program for the number of seconds you give it.",[45,42651,42653],{"className":47,"code":42652,"language":49,"meta":50,"style":50},"import time\n\nprint(\"Start\")\ntime.sleep(1)\nprint(\"One second later\")\n",[17,42654,42655,42661,42665,42680,42694],{"__ignoreMap":50},[54,42656,42657,42659],{"class":56,"line":57},[54,42658,61],{"class":60},[54,42660,31493],{"class":64},[54,42662,42663],{"class":56,"line":68},[54,42664,72],{"emptyLinePlaceholder":71},[54,42666,42667,42669,42671,42673,42676,42678],{"class":56,"line":75},[54,42668,116],{"class":115},[54,42670,96],{"class":88},[54,42672,545],{"class":544},[54,42674,42675],{"class":548},"Start",[54,42677,545],{"class":544},[54,42679,109],{"class":88},[54,42681,42682,42684,42686,42688,42690,42692],{"class":56,"line":112},[54,42683,2559],{"class":64},[54,42685,89],{"class":88},[54,42687,42410],{"class":92},[54,42689,96],{"class":88},[54,42691,100],{"class":99},[54,42693,109],{"class":88},[54,42695,42696,42698,42700,42702,42705,42707],{"class":56,"line":887},[54,42697,116],{"class":115},[54,42699,96],{"class":88},[54,42701,545],{"class":544},[54,42703,42704],{"class":548},"One second later",[54,42706,545],{"class":544},[54,42708,109],{"class":88},[14,42710,42711],{},"You can also use decimal values:",[45,42713,42715],{"className":47,"code":42714,"language":49,"meta":50,"style":50},"import time\n\ntime.sleep(0.5)\nprint(\"Half a second passed\")\n",[17,42716,42717,42723,42727,42741],{"__ignoreMap":50},[54,42718,42719,42721],{"class":56,"line":57},[54,42720,61],{"class":60},[54,42722,31493],{"class":64},[54,42724,42725],{"class":56,"line":68},[54,42726,72],{"emptyLinePlaceholder":71},[54,42728,42729,42731,42733,42735,42737,42739],{"class":56,"line":75},[54,42730,2559],{"class":64},[54,42732,89],{"class":88},[54,42734,42410],{"class":92},[54,42736,96],{"class":88},[54,42738,21071],{"class":99},[54,42740,109],{"class":88},[54,42742,42743,42745,42747,42749,42752,42754],{"class":56,"line":112},[54,42744,116],{"class":115},[54,42746,96],{"class":88},[54,42748,545],{"class":544},[54,42750,42751],{"class":548},"Half a second passed",[54,42753,545],{"class":544},[54,42755,109],{"class":88},[602,42757,42759],{"id":42758},"timectime",[17,42760,42761],{},"time.ctime()",[14,42763,42764],{},"Turns a timestamp into a readable string.",[45,42766,42768],{"className":47,"code":42767,"language":49,"meta":50,"style":50},"import time\n\ntimestamp = time.time()\nprint(time.ctime(timestamp))\n",[17,42769,42770,42776,42780,42794],{"__ignoreMap":50},[54,42771,42772,42774],{"class":56,"line":57},[54,42773,61],{"class":60},[54,42775,31493],{"class":64},[54,42777,42778],{"class":56,"line":68},[54,42779,72],{"emptyLinePlaceholder":71},[54,42781,42782,42784,42786,42788,42790,42792],{"class":56,"line":75},[54,42783,7135],{"class":64},[54,42785,82],{"class":81},[54,42787,31045],{"class":64},[54,42789,89],{"class":88},[54,42791,2559],{"class":92},[54,42793,1670],{"class":88},[54,42795,42796,42798,42800,42802,42804,42807,42809,42811],{"class":56,"line":112},[54,42797,116],{"class":115},[54,42799,96],{"class":88},[54,42801,2559],{"class":92},[54,42803,89],{"class":88},[54,42805,42806],{"class":92},"ctime",[54,42808,96],{"class":88},[54,42810,7161],{"class":92},[54,42812,382],{"class":88},[14,42814,561],{},[45,42816,42818],{"className":47,"code":42817,"language":49,"meta":50,"style":50},"Fri Apr  5 12:34:56 2024\n",[17,42819,42820],{"__ignoreMap":50},[54,42821,42822,42825,42827,42830,42832,42835,42837,42840],{"class":56,"line":57},[54,42823,42824],{"class":64},"Fri Apr  ",[54,42826,285],{"class":99},[54,42828,42829],{"class":99}," 12",[54,42831,147],{"class":88},[54,42833,42834],{"class":99},"34",[54,42836,147],{"class":88},[54,42838,42839],{"class":99},"56",[54,42841,30734],{"class":99},[602,42843,42845],{"id":42844},"timelocaltime",[17,42846,42847],{},"time.localtime()",[14,42849,42850],{},"Converts a timestamp into a structured local time value.",[45,42852,42854],{"className":47,"code":42853,"language":49,"meta":50,"style":50},"import time\n\ncurrent_time = time.localtime()\nprint(current_time)\nprint(current_time.tm_year)\nprint(current_time.tm_mon)\nprint(current_time.tm_mday)\n",[17,42855,42856,42862,42866,42882,42893,42908,42923],{"__ignoreMap":50},[54,42857,42858,42860],{"class":56,"line":57},[54,42859,61],{"class":60},[54,42861,31493],{"class":64},[54,42863,42864],{"class":56,"line":68},[54,42865,72],{"emptyLinePlaceholder":71},[54,42867,42868,42871,42873,42875,42877,42880],{"class":56,"line":75},[54,42869,42870],{"class":64},"current_time ",[54,42872,82],{"class":81},[54,42874,31045],{"class":64},[54,42876,89],{"class":88},[54,42878,42879],{"class":92},"localtime",[54,42881,1670],{"class":88},[54,42883,42884,42886,42888,42891],{"class":56,"line":112},[54,42885,116],{"class":115},[54,42887,96],{"class":88},[54,42889,42890],{"class":92},"current_time",[54,42892,109],{"class":88},[54,42894,42895,42897,42899,42901,42903,42906],{"class":56,"line":887},[54,42896,116],{"class":115},[54,42898,96],{"class":88},[54,42900,42890],{"class":92},[54,42902,89],{"class":88},[54,42904,42905],{"class":1693},"tm_year",[54,42907,109],{"class":88},[54,42909,42910,42912,42914,42916,42918,42921],{"class":56,"line":892},[54,42911,116],{"class":115},[54,42913,96],{"class":88},[54,42915,42890],{"class":92},[54,42917,89],{"class":88},[54,42919,42920],{"class":1693},"tm_mon",[54,42922,109],{"class":88},[54,42924,42925,42927,42929,42931,42933,42936],{"class":56,"line":913},[54,42926,116],{"class":115},[54,42928,96],{"class":88},[54,42930,42890],{"class":92},[54,42932,89],{"class":88},[54,42934,42935],{"class":1693},"tm_mday",[54,42937,109],{"class":88},[14,42939,42940],{},"This is useful when you want parts of the local date and time.",[602,42942,42944],{"id":42943},"timestrftime",[17,42945,42946],{},"time.strftime()",[14,42948,42949],{},"Formats a time value as text.",[45,42951,42953],{"className":47,"code":42952,"language":49,"meta":50,"style":50},"import time\n\ncurrent_time = time.localtime()\nformatted = time.strftime(\"%Y-%m-%d %H:%M:%S\", current_time)\nprint(formatted)\n",[17,42954,42955,42961,42965,42979,43010],{"__ignoreMap":50},[54,42956,42957,42959],{"class":56,"line":57},[54,42958,61],{"class":60},[54,42960,31493],{"class":64},[54,42962,42963],{"class":56,"line":68},[54,42964,72],{"emptyLinePlaceholder":71},[54,42966,42967,42969,42971,42973,42975,42977],{"class":56,"line":75},[54,42968,42870],{"class":64},[54,42970,82],{"class":81},[54,42972,31045],{"class":64},[54,42974,89],{"class":88},[54,42976,42879],{"class":92},[54,42978,1670],{"class":88},[54,42980,42981,42983,42985,42987,42989,42991,42993,42995,42997,42999,43001,43003,43005,43008],{"class":56,"line":112},[54,42982,3634],{"class":64},[54,42984,82],{"class":81},[54,42986,31045],{"class":64},[54,42988,89],{"class":88},[54,42990,2704],{"class":92},[54,42992,96],{"class":88},[54,42994,545],{"class":544},[54,42996,2711],{"class":548},[54,42998,2714],{"class":99},[54,43000,3202],{"class":548},[54,43002,545],{"class":544},[54,43004,103],{"class":88},[54,43006,43007],{"class":92}," current_time",[54,43009,109],{"class":88},[54,43011,43012,43014,43016,43018],{"class":56,"line":887},[54,43013,116],{"class":115},[54,43015,96],{"class":88},[54,43017,3665],{"class":92},[54,43019,109],{"class":88},[14,43021,561],{},[45,43023,43025],{"className":47,"code":43024,"language":49,"meta":50,"style":50},"2024-04-05 12:34:56\n",[17,43026,43027],{"__ignoreMap":50},[54,43028,43029,43031,43033,43035,43037,43039,43041,43043,43045,43047,43049,43051],{"class":56,"line":57},[54,43030,6274],{"class":99},[54,43032,1467],{"class":81},[54,43034,752],{"class":99},[54,43036,2389],{"class":2388},[54,43038,1467],{"class":81},[54,43040,752],{"class":99},[54,43042,285],{"class":2388},[54,43044,42829],{"class":99},[54,43046,147],{"class":88},[54,43048,42834],{"class":99},[54,43050,147],{"class":88},[54,43052,43053],{"class":99},"56\n",[40,43055,43057],{"id":43056},"simple-example-flow","Simple example flow",[14,43059,43060],{},"A common beginner workflow looks like this:",[27327,43062,43063,43069,43073,43078],{},[152,43064,43065,43066],{},"Import the module with ",[17,43067,43068],{},"import time",[152,43070,42552,43071],{},[17,43072,42440],{},[152,43074,43075,43076],{},"Pause execution with ",[17,43077,42549],{},[152,43079,43080],{},"Format the output only if you need readable text",[14,43082,253],{},[45,43084,43086],{"className":47,"code":43085,"language":49,"meta":50,"style":50},"import time\n\nstart = time.time()\nprint(\"Start timestamp:\", start)\n\ntime.sleep(2)\n\nend = time.time()\nprint(\"End timestamp:\", end)\n\nprint(\"Readable end time:\", time.ctime(end))\n",[17,43087,43088,43094,43098,43113,43133,43137,43151,43155,43170,43190,43194],{"__ignoreMap":50},[54,43089,43090,43092],{"class":56,"line":57},[54,43091,61],{"class":60},[54,43093,31493],{"class":64},[54,43095,43096],{"class":56,"line":68},[54,43097,72],{"emptyLinePlaceholder":71},[54,43099,43100,43103,43105,43107,43109,43111],{"class":56,"line":75},[54,43101,43102],{"class":64},"start ",[54,43104,82],{"class":81},[54,43106,31045],{"class":64},[54,43108,89],{"class":88},[54,43110,2559],{"class":92},[54,43112,1670],{"class":88},[54,43114,43115,43117,43119,43121,43124,43126,43128,43131],{"class":56,"line":112},[54,43116,116],{"class":115},[54,43118,96],{"class":88},[54,43120,545],{"class":544},[54,43122,43123],{"class":548},"Start timestamp:",[54,43125,545],{"class":544},[54,43127,103],{"class":88},[54,43129,43130],{"class":92}," start",[54,43132,109],{"class":88},[54,43134,43135],{"class":56,"line":887},[54,43136,72],{"emptyLinePlaceholder":71},[54,43138,43139,43141,43143,43145,43147,43149],{"class":56,"line":892},[54,43140,2559],{"class":64},[54,43142,89],{"class":88},[54,43144,42410],{"class":92},[54,43146,96],{"class":88},[54,43148,396],{"class":99},[54,43150,109],{"class":88},[54,43152,43153],{"class":56,"line":913},[54,43154,72],{"emptyLinePlaceholder":71},[54,43156,43157,43160,43162,43164,43166,43168],{"class":56,"line":2121},[54,43158,43159],{"class":64},"end ",[54,43161,82],{"class":81},[54,43163,31045],{"class":64},[54,43165,89],{"class":88},[54,43167,2559],{"class":92},[54,43169,1670],{"class":88},[54,43171,43172,43174,43176,43178,43181,43183,43185,43188],{"class":56,"line":2136},[54,43173,116],{"class":115},[54,43175,96],{"class":88},[54,43177,545],{"class":544},[54,43179,43180],{"class":548},"End timestamp:",[54,43182,545],{"class":544},[54,43184,103],{"class":88},[54,43186,43187],{"class":92}," end",[54,43189,109],{"class":88},[54,43191,43192],{"class":56,"line":2151},[54,43193,72],{"emptyLinePlaceholder":71},[54,43195,43196,43198,43200,43202,43205,43207,43209,43211,43213,43215,43217,43219],{"class":56,"line":4442},[54,43197,116],{"class":115},[54,43199,96],{"class":88},[54,43201,545],{"class":544},[54,43203,43204],{"class":548},"Readable end time:",[54,43206,545],{"class":544},[54,43208,103],{"class":88},[54,43210,31045],{"class":92},[54,43212,89],{"class":88},[54,43214,42806],{"class":92},[54,43216,96],{"class":88},[54,43218,137],{"class":92},[54,43220,382],{"class":88},[14,43222,43223],{},"This example:",[149,43225,43226,43229,43232,43235],{},[152,43227,43228],{},"gets a start timestamp",[152,43230,43231],{},"waits 2 seconds",[152,43233,43234],{},"gets an end timestamp",[152,43236,43237],{},"shows the end time in a readable format",[40,43239,25643],{"id":25642},[14,43241,28340,43242,43244],{},[17,43243,2559],{}," module is often used for small, practical tasks.",[602,43246,43248],{"id":43247},"adding-a-delay-in-a-script","Adding a delay in a script",[45,43250,43252],{"className":47,"code":43251,"language":49,"meta":50,"style":50},"import time\n\nprint(\"Loading...\")\ntime.sleep(2)\nprint(\"Finished\")\n",[17,43253,43254,43260,43264,43279,43293],{"__ignoreMap":50},[54,43255,43256,43258],{"class":56,"line":57},[54,43257,61],{"class":60},[54,43259,31493],{"class":64},[54,43261,43262],{"class":56,"line":68},[54,43263,72],{"emptyLinePlaceholder":71},[54,43265,43266,43268,43270,43272,43275,43277],{"class":56,"line":75},[54,43267,116],{"class":115},[54,43269,96],{"class":88},[54,43271,545],{"class":544},[54,43273,43274],{"class":548},"Loading...",[54,43276,545],{"class":544},[54,43278,109],{"class":88},[54,43280,43281,43283,43285,43287,43289,43291],{"class":56,"line":112},[54,43282,2559],{"class":64},[54,43284,89],{"class":88},[54,43286,42410],{"class":92},[54,43288,96],{"class":88},[54,43290,396],{"class":99},[54,43292,109],{"class":88},[54,43294,43295,43297,43299,43301,43304,43306],{"class":56,"line":887},[54,43296,116],{"class":115},[54,43298,96],{"class":88},[54,43300,545],{"class":544},[54,43302,43303],{"class":548},"Finished",[54,43305,545],{"class":544},[54,43307,109],{"class":88},[602,43309,43311],{"id":43310},"timing-how-long-a-task-takes","Timing how long a task takes",[14,43313,43314],{},"For a step-by-step guide, see how to measure execution time in Python.",[45,43316,43318],{"className":47,"code":43317,"language":49,"meta":50,"style":50},"import time\n\nstart = time.time()\n\nfor i in range(3):\n    print(\"Working...\")\n    time.sleep(1)\n\nend = time.time()\n\nprint(\"Duration:\", end - start, \"seconds\")\n",[17,43319,43320,43326,43330,43344,43348,43366,43381,43396,43400,43414,43418],{"__ignoreMap":50},[54,43321,43322,43324],{"class":56,"line":57},[54,43323,61],{"class":60},[54,43325,31493],{"class":64},[54,43327,43328],{"class":56,"line":68},[54,43329,72],{"emptyLinePlaceholder":71},[54,43331,43332,43334,43336,43338,43340,43342],{"class":56,"line":75},[54,43333,43102],{"class":64},[54,43335,82],{"class":81},[54,43337,31045],{"class":64},[54,43339,89],{"class":88},[54,43341,2559],{"class":92},[54,43343,1670],{"class":88},[54,43345,43346],{"class":56,"line":112},[54,43347,72],{"emptyLinePlaceholder":71},[54,43349,43350,43352,43355,43357,43360,43362,43364],{"class":56,"line":887},[54,43351,26859],{"class":60},[54,43353,43354],{"class":64}," i ",[54,43356,26865],{"class":60},[54,43358,43359],{"class":115}," range",[54,43361,96],{"class":88},[54,43363,401],{"class":99},[54,43365,10837],{"class":88},[54,43367,43368,43370,43372,43374,43377,43379],{"class":56,"line":892},[54,43369,7606],{"class":115},[54,43371,96],{"class":88},[54,43373,545],{"class":544},[54,43375,43376],{"class":548},"Working...",[54,43378,545],{"class":544},[54,43380,109],{"class":88},[54,43382,43383,43386,43388,43390,43392,43394],{"class":56,"line":913},[54,43384,43385],{"class":64},"    time",[54,43387,89],{"class":88},[54,43389,42410],{"class":92},[54,43391,96],{"class":88},[54,43393,100],{"class":99},[54,43395,109],{"class":88},[54,43397,43398],{"class":56,"line":2121},[54,43399,72],{"emptyLinePlaceholder":71},[54,43401,43402,43404,43406,43408,43410,43412],{"class":56,"line":2136},[54,43403,43159],{"class":64},[54,43405,82],{"class":81},[54,43407,31045],{"class":64},[54,43409,89],{"class":88},[54,43411,2559],{"class":92},[54,43413,1670],{"class":88},[54,43415,43416],{"class":56,"line":2151},[54,43417,72],{"emptyLinePlaceholder":71},[54,43419,43420,43422,43424,43426,43429,43431,43433,43436,43438,43440,43442,43444,43447,43449],{"class":56,"line":4442},[54,43421,116],{"class":115},[54,43423,96],{"class":88},[54,43425,545],{"class":544},[54,43427,43428],{"class":548},"Duration:",[54,43430,545],{"class":544},[54,43432,103],{"class":88},[54,43434,43435],{"class":92}," end ",[54,43437,1467],{"class":81},[54,43439,43130],{"class":92},[54,43441,103],{"class":88},[54,43443,830],{"class":544},[54,43445,43446],{"class":548},"seconds",[54,43448,545],{"class":544},[54,43450,109],{"class":88},[602,43452,43454],{"id":43453},"creating-a-simple-countdown-or-timer","Creating a simple countdown or timer",[14,43456,43457],{},"For a full walkthrough, see how to create a countdown timer in Python.",[45,43459,43461],{"className":47,"code":43460,"language":49,"meta":50,"style":50},"import time\n\nfor number in range(3, 0, -1):\n    print(number)\n    time.sleep(1)\n\nprint(\"Go!\")\n",[17,43462,43463,43469,43473,43500,43510,43524,43528],{"__ignoreMap":50},[54,43464,43465,43467],{"class":56,"line":57},[54,43466,61],{"class":60},[54,43468,31493],{"class":64},[54,43470,43471],{"class":56,"line":68},[54,43472,72],{"emptyLinePlaceholder":71},[54,43474,43475,43477,43480,43482,43484,43486,43488,43490,43492,43494,43496,43498],{"class":56,"line":75},[54,43476,26859],{"class":60},[54,43478,43479],{"class":64}," number ",[54,43481,26865],{"class":60},[54,43483,43359],{"class":115},[54,43485,96],{"class":88},[54,43487,401],{"class":99},[54,43489,103],{"class":88},[54,43491,3986],{"class":99},[54,43493,103],{"class":88},[54,43495,879],{"class":81},[54,43497,100],{"class":99},[54,43499,10837],{"class":88},[54,43501,43502,43504,43506,43508],{"class":56,"line":112},[54,43503,7606],{"class":115},[54,43505,96],{"class":88},[54,43507,121],{"class":92},[54,43509,109],{"class":88},[54,43511,43512,43514,43516,43518,43520,43522],{"class":56,"line":887},[54,43513,43385],{"class":64},[54,43515,89],{"class":88},[54,43517,42410],{"class":92},[54,43519,96],{"class":88},[54,43521,100],{"class":99},[54,43523,109],{"class":88},[54,43525,43526],{"class":56,"line":892},[54,43527,72],{"emptyLinePlaceholder":71},[54,43529,43530,43532,43534,43536,43539,43541],{"class":56,"line":913},[54,43531,116],{"class":115},[54,43533,96],{"class":88},[54,43535,545],{"class":544},[54,43537,43538],{"class":548},"Go!",[54,43540,545],{"class":544},[54,43542,109],{"class":88},[602,43544,43546],{"id":43545},"printing-the-current-timestamp-for-logs","Printing the current timestamp for logs",[45,43548,43550],{"className":47,"code":43549,"language":49,"meta":50,"style":50},"import time\n\nprint(\"Log time:\", time.time())\n",[17,43551,43552,43558,43562],{"__ignoreMap":50},[54,43553,43554,43556],{"class":56,"line":57},[54,43555,61],{"class":60},[54,43557,31493],{"class":64},[54,43559,43560],{"class":56,"line":68},[54,43561,72],{"emptyLinePlaceholder":71},[54,43563,43564,43566,43568,43570,43573,43575,43577,43579,43581,43583],{"class":56,"line":75},[54,43565,116],{"class":115},[54,43567,96],{"class":88},[54,43569,545],{"class":544},[54,43571,43572],{"class":548},"Log time:",[54,43574,545],{"class":544},[54,43576,103],{"class":88},[54,43578,31045],{"class":92},[54,43580,89],{"class":88},[54,43582,2559],{"class":92},[54,43584,1950],{"class":88},[40,43586,43588],{"id":43587},"time-module-vs-datetime-module","time module vs datetime module",[14,43590,43591],{},"Beginners often wonder which module to use.",[14,43593,126,43594,43596],{},[17,43595,2559],{}," when you need:",[149,43598,43599,43601,43604],{},[152,43600,42491],{},[152,43602,43603],{},"simple timestamps",[152,43605,43606],{},"basic timing",[14,43608,126,43609,43596],{},[17,43610,1628],{},[149,43612,43613,43615,43617],{},[152,43614,42570],{},[152,43616,42573],{},[152,43618,43619],{},"date arithmetic",[14,43621,43622],{},"A simple rule:",[149,43624,43625,43633],{},[152,43626,43627,43628,134,43630,43632],{},"start with ",[17,43629,42549],{},[17,43631,42440],{}," for basic tasks",[152,43634,43635,43636,43640],{},"move to ",[192,43637,43638],{"href":6351},[17,43639,1628],{}," for date display and date math",[40,43642,978],{"id":977},[14,43644,43645,43646,89],{},"Here are some common beginner problems when using ",[17,43647,2559],{},[602,43649,17008,43651],{"id":43650},"forgetting-to-import-time",[17,43652,2559],{},[14,43654,985,43655,89],{},[17,43656,988],{},[45,43658,43660],{"className":47,"code":43659,"language":49,"meta":50,"style":50},"print(time.time())\n",[17,43661,43662],{"__ignoreMap":50},[54,43663,43664,43666,43668,43670,43672,43674],{"class":56,"line":57},[54,43665,116],{"class":115},[54,43667,96],{"class":88},[54,43669,2559],{"class":92},[54,43671,89],{"class":88},[54,43673,2559],{"class":92},[54,43675,1950],{"class":88},[14,43677,1031],{},[45,43679,43681],{"className":47,"code":43680,"language":49,"meta":50,"style":50},"import time\n\nprint(time.time())\n",[17,43682,43683,43689,43693],{"__ignoreMap":50},[54,43684,43685,43687],{"class":56,"line":57},[54,43686,61],{"class":60},[54,43688,31493],{"class":64},[54,43690,43691],{"class":56,"line":68},[54,43692,72],{"emptyLinePlaceholder":71},[54,43694,43695,43697,43699,43701,43703,43705],{"class":56,"line":75},[54,43696,116],{"class":115},[54,43698,96],{"class":88},[54,43700,2559],{"class":92},[54,43702,89],{"class":88},[54,43704,2559],{"class":92},[54,43706,1950],{"class":88},[602,43708,43710,43711],{"id":43709},"passing-a-string-to-timesleep","Passing a string to ",[17,43712,42549],{},[14,43714,23179,43715,43718],{},[17,43716,43717],{},"sleep()"," expects a number.",[45,43720,43722],{"className":47,"code":43721,"language":49,"meta":50,"style":50},"import time\n\ntime.sleep(\"2\")\n",[17,43723,43724,43730,43734],{"__ignoreMap":50},[54,43725,43726,43728],{"class":56,"line":57},[54,43727,61],{"class":60},[54,43729,31493],{"class":64},[54,43731,43732],{"class":56,"line":68},[54,43733,72],{"emptyLinePlaceholder":71},[54,43735,43736,43738,43740,43742,43744,43746,43748,43750],{"class":56,"line":75},[54,43737,2559],{"class":64},[54,43739,89],{"class":88},[54,43741,42410],{"class":92},[54,43743,96],{"class":88},[54,43745,545],{"class":544},[54,43747,396],{"class":548},[54,43749,545],{"class":544},[54,43751,109],{"class":88},[14,43753,1031],{},[45,43755,43757],{"className":47,"code":43756,"language":49,"meta":50,"style":50},"import time\n\ntime.sleep(2)\n",[17,43758,43759,43765,43769],{"__ignoreMap":50},[54,43760,43761,43763],{"class":56,"line":57},[54,43762,61],{"class":60},[54,43764,31493],{"class":64},[54,43766,43767],{"class":56,"line":68},[54,43768,72],{"emptyLinePlaceholder":71},[54,43770,43771,43773,43775,43777,43779,43781],{"class":56,"line":75},[54,43772,2559],{"class":64},[54,43774,89],{"class":88},[54,43776,42410],{"class":92},[54,43778,96],{"class":88},[54,43780,396],{"class":99},[54,43782,109],{"class":88},[602,43784,5949,43786,43788],{"id":43785},"expecting-timetime-to-return-a-readable-date",[17,43787,42440],{}," to return a readable date",[14,43790,43791,43793],{},[17,43792,42440],{}," returns a numeric Unix timestamp.",[45,43795,43796],{"className":47,"code":43680,"language":49,"meta":50,"style":50},[17,43797,43798,43804,43808],{"__ignoreMap":50},[54,43799,43800,43802],{"class":56,"line":57},[54,43801,61],{"class":60},[54,43803,31493],{"class":64},[54,43805,43806],{"class":56,"line":68},[54,43807,72],{"emptyLinePlaceholder":71},[54,43809,43810,43812,43814,43816,43818,43820],{"class":56,"line":75},[54,43811,116],{"class":115},[54,43813,96],{"class":88},[54,43815,2559],{"class":92},[54,43817,89],{"class":88},[54,43819,2559],{"class":92},[54,43821,1950],{"class":88},[14,43823,43824,43825,5507,43827,89],{},"If you want readable text, use ",[17,43826,42761],{},[17,43828,42946],{},[45,43830,43832],{"className":47,"code":43831,"language":49,"meta":50,"style":50},"import time\n\nprint(time.ctime())\n",[17,43833,43834,43840,43844],{"__ignoreMap":50},[54,43835,43836,43838],{"class":56,"line":57},[54,43837,61],{"class":60},[54,43839,31493],{"class":64},[54,43841,43842],{"class":56,"line":68},[54,43843,72],{"emptyLinePlaceholder":71},[54,43845,43846,43848,43850,43852,43854,43856],{"class":56,"line":75},[54,43847,116],{"class":115},[54,43849,96],{"class":88},[54,43851,2559],{"class":92},[54,43853,89],{"class":88},[54,43855,42806],{"class":92},[54,43857,1950],{"class":88},[602,43859,5481,43861,43863,43864,43866],{"id":43860},"using-time-when-datetime-would-be-easier",[17,43862,2559],{}," when ",[17,43865,1628],{}," would be easier",[14,43868,43869,43870,43874],{},"If you need values like year, month, and day in a clean object, the ",[192,43871,43872,13763],{"href":6351},[17,43873,1628],{}," is usually easier to work with.",[602,43876,43878,43879,43881],{"id":43877},"thinking-sleep-pauses-only-one-line","Thinking ",[17,43880,43717],{}," pauses only one line",[14,43883,43884,43886],{},[17,43885,42549],{}," pauses the whole program before it moves to the next line.",[45,43888,43890],{"className":47,"code":43889,"language":49,"meta":50,"style":50},"import time\n\nprint(\"Before\")\ntime.sleep(2)\nprint(\"After\")\n",[17,43891,43892,43898,43902,43916,43930],{"__ignoreMap":50},[54,43893,43894,43896],{"class":56,"line":57},[54,43895,61],{"class":60},[54,43897,31493],{"class":64},[54,43899,43900],{"class":56,"line":68},[54,43901,72],{"emptyLinePlaceholder":71},[54,43903,43904,43906,43908,43910,43912,43914],{"class":56,"line":75},[54,43905,116],{"class":115},[54,43907,96],{"class":88},[54,43909,545],{"class":544},[54,43911,24067],{"class":548},[54,43913,545],{"class":544},[54,43915,109],{"class":88},[54,43917,43918,43920,43922,43924,43926,43928],{"class":56,"line":112},[54,43919,2559],{"class":64},[54,43921,89],{"class":88},[54,43923,42410],{"class":92},[54,43925,96],{"class":88},[54,43927,396],{"class":99},[54,43929,109],{"class":88},[54,43931,43932,43934,43936,43938,43940,43942],{"class":56,"line":887},[54,43933,116],{"class":115},[54,43935,96],{"class":88},[54,43937,545],{"class":544},[54,43939,24089],{"class":548},[54,43941,545],{"class":544},[54,43943,109],{"class":88},[40,43945,43947],{"id":43946},"useful-commands-to-test-the-module","Useful commands to test the module",[14,43949,43950],{},"These commands let you quickly check that the module works in your Python installation.",[45,43952,43954],{"className":15497,"code":43953,"language":15499,"meta":50,"style":50},"python --version\npython -c \"import time; print(time.time())\"\npython -c \"import time; time.sleep(1); print('done')\"\npython -c \"import time; print(time.ctime())\"\n",[17,43955,43956,43962,43975,43988],{"__ignoreMap":50},[54,43957,43958,43960],{"class":56,"line":57},[54,43959,49],{"class":10811},[54,43961,35879],{"class":24323},[54,43963,43964,43966,43968,43970,43973],{"class":56,"line":68},[54,43965,49],{"class":10811},[54,43967,26050],{"class":24323},[54,43969,830],{"class":544},[54,43971,43972],{"class":548},"import time; print(time.time())",[54,43974,5239],{"class":544},[54,43976,43977,43979,43981,43983,43986],{"class":56,"line":75},[54,43978,49],{"class":10811},[54,43980,26050],{"class":24323},[54,43982,830],{"class":544},[54,43984,43985],{"class":548},"import time; time.sleep(1); print('done')",[54,43987,5239],{"class":544},[54,43989,43990,43992,43994,43996,43999],{"class":56,"line":112},[54,43991,49],{"class":10811},[54,43993,26050],{"class":24323},[54,43995,830],{"class":544},[54,43997,43998],{"class":548},"import time; print(time.ctime())",[54,44000,5239],{"class":544},[40,44002,1419],{"id":1418},[602,44004,44006],{"id":44005},"is-the-time-module-built-into-python","Is the time module built into Python?",[14,44008,6018,44009,44011],{},[17,44010,43068],{}," and do not need to install anything.",[602,44013,44015],{"id":44014},"what-does-timesleep-do","What does time.sleep() do?",[14,44017,44018],{},"It pauses the whole program for the number of seconds you give it.",[602,44020,44022],{"id":44021},"what-does-timetime-return","What does time.time() return?",[14,44024,44025],{},"It returns the current Unix timestamp as a float.",[602,44027,44029],{"id":44028},"should-i-use-time-or-datetime","Should I use time or datetime?",[14,44031,126,44032,44034,44035,44037],{},[17,44033,2559],{}," for delays and simple timestamps. Use ",[17,44036,1628],{}," for readable dates and date calculations.",[602,44039,44041],{"id":44040},"can-timesleep-use-decimal-values","Can time.sleep() use decimal values?",[14,44043,22191,44044,44047],{},[17,44045,44046],{},"time.sleep(0.5)"," pauses for half a second.",[40,44049,1510],{"id":1509},[149,44051,44052,44058,44064],{},[152,44053,44054],{},[192,44055,44056,12898],{"href":42546},[17,44057,42549],{},[152,44059,44060],{},[192,44061,44062,12898],{"href":42555},[17,44063,42440],{},[152,44065,44066],{},[192,44067,44068,13763],{"href":6351},[17,44069,1628],{},[1545,44071,44072],{},"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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .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":50,"searchDepth":68,"depth":68,"links":44074},[44075,44076,44077,44078,44085,44086,44092,44093,44105,44106,44113],{"id":3595,"depth":68,"text":3596},{"id":42475,"depth":68,"text":42476},{"id":42530,"depth":68,"text":42531},{"id":42579,"depth":68,"text":42580,"children":44079},[44080,44081,44082,44083,44084],{"id":42589,"depth":75,"text":42440},{"id":42643,"depth":75,"text":42646},{"id":42758,"depth":75,"text":42761},{"id":42844,"depth":75,"text":42847},{"id":42943,"depth":75,"text":42946},{"id":43056,"depth":68,"text":43057},{"id":25642,"depth":68,"text":25643,"children":44087},[44088,44089,44090,44091],{"id":43247,"depth":75,"text":43248},{"id":43310,"depth":75,"text":43311},{"id":43453,"depth":75,"text":43454},{"id":43545,"depth":75,"text":43546},{"id":43587,"depth":68,"text":43588},{"id":977,"depth":68,"text":978,"children":44094},[44095,44097,44099,44101,44103],{"id":43650,"depth":75,"text":44096},"Forgetting to import time",{"id":43709,"depth":75,"text":44098},"Passing a string to time.sleep()",{"id":43785,"depth":75,"text":44100},"Expecting time.time() to return a readable date",{"id":43860,"depth":75,"text":44102},"Using time when datetime would be easier",{"id":43877,"depth":75,"text":44104},"Thinking sleep() pauses only one line",{"id":43946,"depth":68,"text":43947},{"id":1418,"depth":68,"text":1419,"children":44107},[44108,44109,44110,44111,44112],{"id":44005,"depth":75,"text":44006},{"id":44014,"depth":75,"text":44015},{"id":44021,"depth":75,"text":44022},{"id":44028,"depth":75,"text":44029},{"id":44040,"depth":75,"text":44041},{"id":1509,"depth":68,"text":1510},"Master python time module overview in our comprehensive Python beginner guide.",{},"\u002Fstandard-library\u002Fpython-time-module-overview",{"title":42323,"description":44114},"standard-library\u002Fpython-time-module-overview","6V4Co50mQjZt7MniDwC0Cd6CU8eTXYJZZMofcyukkGQ",{"id":44121,"title":44122,"body":44123,"description":46256,"extension":1577,"meta":46257,"navigation":71,"path":970,"seo":46258,"stem":46259,"__hash__":46260},"content\u002Fstandard-library\u002Frandom.choice-function-explained.md","random.choice() Function Explained",{"type":7,"value":44124,"toc":46220},[44125,44130,44139,44142,44153,44156,44232,44237,44242,44246,44260,44263,44277,44283,44285,44287,44307,44311,44321,44324,44387,44389,44398,44401,44403,44420,44424,44428,44519,44522,44526,44599,44602,44606,44667,44670,44673,44677,44682,44685,44696,44700,44765,44769,44822,44826,44873,44879,44882,44887,44924,44927,45008,45012,45018,45020,45060,45063,45066,45140,45149,45152,45231,45235,45243,45246,45248,45360,45363,45369,45372,45401,45407,45473,45478,45514,45519,45604,45610,45612,45617,45634,45638,45640,45695,45698,45762,45766,45769,45809,45812,45886,45890,45896,45899,45972,45975,45979,45982,46049,46056,46058,46133,46135,46146,46148,46154,46157,46163,46166,46172,46175,46181,46184,46186,46211,46217],[10,44126,44128,11309],{"id":44127},"randomchoice-function-explained",[17,44129,973],{},[14,44131,44132,44134,44135,44138],{},[17,44133,973],{}," lets you pick ",[35,44136,44137],{},"one random item"," from a sequence.",[14,44140,44141],{},"Use it when you want a single random value from something like:",[149,44143,44144,44147,44150],{},[152,44145,44146],{},"a list",[152,44148,44149],{},"a tuple",[152,44151,44152],{},"a string",[14,44154,44155],{},"It returns the chosen item directly. The sequence must contain at least one item.",[45,44157,44159],{"className":47,"code":44158,"language":49,"meta":50,"style":50},"import random\n\ncolors = [\"red\", \"blue\", \"green\"]\nchoice = random.choice(colors)\nprint(choice)\n",[17,44160,44161,44167,44171,44203,44222],{"__ignoreMap":50},[54,44162,44163,44165],{"class":56,"line":57},[54,44164,61],{"class":60},[54,44166,65],{"class":64},[54,44168,44169],{"class":56,"line":68},[54,44170,72],{"emptyLinePlaceholder":71},[54,44172,44173,44175,44177,44179,44181,44183,44185,44187,44189,44191,44193,44195,44197,44199,44201],{"class":56,"line":75},[54,44174,39636],{"class":64},[54,44176,82],{"class":81},[54,44178,818],{"class":88},[54,44180,545],{"class":544},[54,44182,39645],{"class":548},[54,44184,545],{"class":544},[54,44186,103],{"class":88},[54,44188,830],{"class":544},[54,44190,39654],{"class":548},[54,44192,545],{"class":544},[54,44194,103],{"class":88},[54,44196,830],{"class":544},[54,44198,39663],{"class":548},[54,44200,545],{"class":544},[54,44202,469],{"class":88},[54,44204,44205,44208,44210,44212,44214,44216,44218,44220],{"class":56,"line":112},[54,44206,44207],{"class":64},"choice ",[54,44209,82],{"class":81},[54,44211,85],{"class":64},[54,44213,89],{"class":88},[54,44215,39072],{"class":92},[54,44217,96],{"class":88},[54,44219,39685],{"class":92},[54,44221,109],{"class":88},[54,44223,44224,44226,44228,44230],{"class":56,"line":887},[54,44225,116],{"class":115},[54,44227,96],{"class":88},[54,44229,39072],{"class":92},[54,44231,109],{"class":88},[14,44233,126,44234,44236],{},[17,44235,973],{}," when you want one random item from a non-empty sequence like a list, tuple, or string.",[40,44238,1736,44240,1739],{"id":44239},"what-randomchoice-does",[17,44241,973],{},[14,44243,44244,147],{},[17,44245,973],{},[149,44247,44248,44251,44254,44257],{},[152,44249,44250],{},"Picks one random item from a sequence",[152,44252,44253],{},"Works with lists, tuples, and strings",[152,44255,44256],{},"Returns the chosen item",[152,44258,44259],{},"Raises an error if the sequence is empty",[14,44261,44262],{},"This is useful in small programs such as:",[149,44264,44265,44268,44271,44274],{},[152,44266,44267],{},"picking a random player",[152,44269,44270],{},"choosing a quiz question",[152,44272,44273],{},"selecting a menu option",[152,44275,44276],{},"choosing a random letter or word",[14,44278,6345,44279,6348,44281,89],{},[17,44280,26],{},[192,44282,195],{"href":194},[40,44284,199],{"id":198},[14,44286,202],{},[45,44288,44290],{"className":47,"code":44289,"language":49,"meta":50,"style":50},"random.choice(sequence)\n",[17,44291,44292],{"__ignoreMap":50},[54,44293,44294,44296,44298,44300,44302,44305],{"class":56,"line":57},[54,44295,26],{"class":64},[54,44297,89],{"class":88},[54,44299,39072],{"class":92},[54,44301,96],{"class":88},[54,44303,44304],{"class":92},"sequence",[54,44306,109],{"class":88},[14,44308,19618,44309,19621],{},[17,44310,26],{},[45,44312,44313],{"className":47,"code":40321,"language":49,"meta":50,"style":50},[17,44314,44315],{"__ignoreMap":50},[54,44316,44317,44319],{"class":56,"line":57},[54,44318,61],{"class":60},[54,44320,65],{"class":64},[14,44322,44323],{},"Then pass one sequence argument:",[45,44325,44327],{"className":47,"code":44326,"language":49,"meta":50,"style":50},"import random\n\nnumbers = [10, 20, 30]\nresult = random.choice(numbers)\nprint(result)\n",[17,44328,44329,44335,44339,44359,44377],{"__ignoreMap":50},[54,44330,44331,44333],{"class":56,"line":57},[54,44332,61],{"class":60},[54,44334,65],{"class":64},[54,44336,44337],{"class":56,"line":68},[54,44338,72],{"emptyLinePlaceholder":71},[54,44340,44341,44343,44345,44347,44349,44351,44353,44355,44357],{"class":56,"line":75},[54,44342,11576],{"class":64},[54,44344,82],{"class":81},[54,44346,818],{"class":88},[54,44348,1131],{"class":99},[54,44350,103],{"class":88},[54,44352,695],{"class":99},[54,44354,103],{"class":88},[54,44356,3981],{"class":99},[54,44358,469],{"class":88},[54,44360,44361,44363,44365,44367,44369,44371,44373,44375],{"class":56,"line":112},[54,44362,3833],{"class":64},[54,44364,82],{"class":81},[54,44366,85],{"class":64},[54,44368,89],{"class":88},[54,44370,39072],{"class":92},[54,44372,96],{"class":88},[54,44374,9956],{"class":92},[54,44376,109],{"class":88},[54,44378,44379,44381,44383,44385],{"class":56,"line":887},[54,44380,116],{"class":115},[54,44382,96],{"class":88},[54,44384,3866],{"class":92},[54,44386,109],{"class":88},[14,44388,561],{},[45,44390,44392],{"className":47,"code":44391,"language":49,"meta":50,"style":50},"20\n",[17,44393,44394],{"__ignoreMap":50},[54,44395,44396],{"class":56,"line":57},[54,44397,44391],{"class":99},[14,44399,44400],{},"Your output may be different, because the result is random.",[14,44402,4136],{},[149,44404,44405,44410,44415],{},[152,44406,44407,44409],{},[17,44408,973],{}," takes one sequence",[152,44411,25027,44412],{},[35,44413,44414],{},"one value",[152,44416,25550,44417,44419],{},[35,44418,3036],{}," return a list",[40,44421,44423],{"id":44422},"common-examples","Common examples",[602,44425,44427],{"id":44426},"pick-a-random-name-from-a-list","Pick a random name from a list",[45,44429,44431],{"className":47,"code":44430,"language":49,"meta":50,"style":50},"import random\n\nnames = [\"Ana\", \"Ben\", \"Chris\", \"Dina\"]\nwinner = random.choice(names)\n\nprint(winner)\n",[17,44432,44433,44439,44443,44485,44504,44508],{"__ignoreMap":50},[54,44434,44435,44437],{"class":56,"line":57},[54,44436,61],{"class":60},[54,44438,65],{"class":64},[54,44440,44441],{"class":56,"line":68},[54,44442,72],{"emptyLinePlaceholder":71},[54,44444,44445,44447,44449,44451,44453,44455,44457,44459,44461,44463,44465,44467,44469,44472,44474,44476,44478,44481,44483],{"class":56,"line":75},[54,44446,813],{"class":64},[54,44448,82],{"class":81},[54,44450,818],{"class":88},[54,44452,545],{"class":544},[54,44454,823],{"class":548},[54,44456,545],{"class":544},[54,44458,103],{"class":88},[54,44460,830],{"class":544},[54,44462,833],{"class":548},[54,44464,545],{"class":544},[54,44466,103],{"class":88},[54,44468,830],{"class":544},[54,44470,44471],{"class":548},"Chris",[54,44473,545],{"class":544},[54,44475,103],{"class":88},[54,44477,830],{"class":544},[54,44479,44480],{"class":548},"Dina",[54,44482,545],{"class":544},[54,44484,469],{"class":88},[54,44486,44487,44490,44492,44494,44496,44498,44500,44502],{"class":56,"line":112},[54,44488,44489],{"class":64},"winner ",[54,44491,82],{"class":81},[54,44493,85],{"class":64},[54,44495,89],{"class":88},[54,44497,39072],{"class":92},[54,44499,96],{"class":88},[54,44501,873],{"class":92},[54,44503,109],{"class":88},[54,44505,44506],{"class":56,"line":887},[54,44507,72],{"emptyLinePlaceholder":71},[54,44509,44510,44512,44514,44517],{"class":56,"line":892},[54,44511,116],{"class":115},[54,44513,96],{"class":88},[54,44515,44516],{"class":92},"winner",[54,44518,109],{"class":88},[14,44520,44521],{},"This chooses one name from the list.",[602,44523,44525],{"id":44524},"pick-a-random-number-from-a-tuple","Pick a random number from a tuple",[45,44527,44529],{"className":47,"code":44528,"language":49,"meta":50,"style":50},"import random\n\nvalues = (5, 10, 15, 20)\nnumber = random.choice(values)\n\nprint(number)\n",[17,44530,44531,44537,44541,44566,44585,44589],{"__ignoreMap":50},[54,44532,44533,44535],{"class":56,"line":57},[54,44534,61],{"class":60},[54,44536,65],{"class":64},[54,44538,44539],{"class":56,"line":68},[54,44540,72],{"emptyLinePlaceholder":71},[54,44542,44543,44546,44548,44550,44552,44554,44556,44558,44560,44562,44564],{"class":56,"line":75},[54,44544,44545],{"class":64},"values ",[54,44547,82],{"class":81},[54,44549,41305],{"class":88},[54,44551,285],{"class":99},[54,44553,103],{"class":88},[54,44555,106],{"class":99},[54,44557,103],{"class":88},[54,44559,31940],{"class":99},[54,44561,103],{"class":88},[54,44563,695],{"class":99},[54,44565,109],{"class":88},[54,44567,44568,44570,44572,44574,44576,44578,44580,44583],{"class":56,"line":112},[54,44569,78],{"class":64},[54,44571,82],{"class":81},[54,44573,85],{"class":64},[54,44575,89],{"class":88},[54,44577,39072],{"class":92},[54,44579,96],{"class":88},[54,44581,44582],{"class":92},"values",[54,44584,109],{"class":88},[54,44586,44587],{"class":56,"line":887},[54,44588,72],{"emptyLinePlaceholder":71},[54,44590,44591,44593,44595,44597],{"class":56,"line":892},[54,44592,116],{"class":115},[54,44594,96],{"class":88},[54,44596,121],{"class":92},[54,44598,109],{"class":88},[14,44600,44601],{},"This works because tuples are sequences.",[602,44603,44605],{"id":44604},"pick-a-random-character-from-a-string","Pick a random character from a string",[45,44607,44609],{"className":47,"code":44608,"language":49,"meta":50,"style":50},"import random\n\ntext = \"python\"\nletter = random.choice(text)\n\nprint(letter)\n",[17,44610,44611,44617,44621,44633,44652,44656],{"__ignoreMap":50},[54,44612,44613,44615],{"class":56,"line":57},[54,44614,61],{"class":60},[54,44616,65],{"class":64},[54,44618,44619],{"class":56,"line":68},[54,44620,72],{"emptyLinePlaceholder":71},[54,44622,44623,44625,44627,44629,44631],{"class":56,"line":75},[54,44624,5134],{"class":64},[54,44626,82],{"class":81},[54,44628,830],{"class":544},[54,44630,49],{"class":548},[54,44632,5239],{"class":544},[54,44634,44635,44638,44640,44642,44644,44646,44648,44650],{"class":56,"line":112},[54,44636,44637],{"class":64},"letter ",[54,44639,82],{"class":81},[54,44641,85],{"class":64},[54,44643,89],{"class":88},[54,44645,39072],{"class":92},[54,44647,96],{"class":88},[54,44649,5171],{"class":92},[54,44651,109],{"class":88},[54,44653,44654],{"class":56,"line":887},[54,44655,72],{"emptyLinePlaceholder":71},[54,44657,44658,44660,44662,44665],{"class":56,"line":892},[54,44659,116],{"class":115},[54,44661,96],{"class":88},[54,44663,44664],{"class":92},"letter",[54,44666,109],{"class":88},[14,44668,44669],{},"This returns one character from the string.",[14,44671,44672],{},"These kinds of examples are common in beginner projects like games, quizzes, and simple scripts.",[40,44674,44676],{"id":44675},"what-counts-as-a-sequence","What counts as a sequence",[14,44678,44679,44681],{},[17,44680,973],{}," works with sequences.",[14,44683,44684],{},"Valid examples:",[149,44686,44687,44690,44693],{},[152,44688,44689],{},"Lists",[152,44691,44692],{},"Tuples",[152,44694,44695],{},"Strings",[602,44697,44699],{"id":44698},"list-example","List example",[45,44701,44703],{"className":47,"code":44702,"language":49,"meta":50,"style":50},"import random\n\ncolors = [\"red\", \"blue\", \"green\"]\nprint(random.choice(colors))\n",[17,44704,44705,44711,44715,44747],{"__ignoreMap":50},[54,44706,44707,44709],{"class":56,"line":57},[54,44708,61],{"class":60},[54,44710,65],{"class":64},[54,44712,44713],{"class":56,"line":68},[54,44714,72],{"emptyLinePlaceholder":71},[54,44716,44717,44719,44721,44723,44725,44727,44729,44731,44733,44735,44737,44739,44741,44743,44745],{"class":56,"line":75},[54,44718,39636],{"class":64},[54,44720,82],{"class":81},[54,44722,818],{"class":88},[54,44724,545],{"class":544},[54,44726,39645],{"class":548},[54,44728,545],{"class":544},[54,44730,103],{"class":88},[54,44732,830],{"class":544},[54,44734,39654],{"class":548},[54,44736,545],{"class":544},[54,44738,103],{"class":88},[54,44740,830],{"class":544},[54,44742,39663],{"class":548},[54,44744,545],{"class":544},[54,44746,469],{"class":88},[54,44748,44749,44751,44753,44755,44757,44759,44761,44763],{"class":56,"line":112},[54,44750,116],{"class":115},[54,44752,96],{"class":88},[54,44754,26],{"class":92},[54,44756,89],{"class":88},[54,44758,39072],{"class":92},[54,44760,96],{"class":88},[54,44762,39685],{"class":92},[54,44764,382],{"class":88},[602,44766,44768],{"id":44767},"tuple-example","Tuple example",[45,44770,44772],{"className":47,"code":44771,"language":49,"meta":50,"style":50},"import random\n\nnumbers = (1, 2, 3)\nprint(random.choice(numbers))\n",[17,44773,44774,44780,44784,44804],{"__ignoreMap":50},[54,44775,44776,44778],{"class":56,"line":57},[54,44777,61],{"class":60},[54,44779,65],{"class":64},[54,44781,44782],{"class":56,"line":68},[54,44783,72],{"emptyLinePlaceholder":71},[54,44785,44786,44788,44790,44792,44794,44796,44798,44800,44802],{"class":56,"line":75},[54,44787,11576],{"class":64},[54,44789,82],{"class":81},[54,44791,41305],{"class":88},[54,44793,100],{"class":99},[54,44795,103],{"class":88},[54,44797,466],{"class":99},[54,44799,103],{"class":88},[54,44801,379],{"class":99},[54,44803,109],{"class":88},[54,44805,44806,44808,44810,44812,44814,44816,44818,44820],{"class":56,"line":112},[54,44807,116],{"class":115},[54,44809,96],{"class":88},[54,44811,26],{"class":92},[54,44813,89],{"class":88},[54,44815,39072],{"class":92},[54,44817,96],{"class":88},[54,44819,9956],{"class":92},[54,44821,382],{"class":88},[602,44823,44825],{"id":44824},"string-example","String example",[45,44827,44829],{"className":47,"code":44828,"language":49,"meta":50,"style":50},"import random\n\nword = \"apple\"\nprint(random.choice(word))\n",[17,44830,44831,44837,44841,44854],{"__ignoreMap":50},[54,44832,44833,44835],{"class":56,"line":57},[54,44834,61],{"class":60},[54,44836,65],{"class":64},[54,44838,44839],{"class":56,"line":68},[54,44840,72],{"emptyLinePlaceholder":71},[54,44842,44843,44846,44848,44850,44852],{"class":56,"line":75},[54,44844,44845],{"class":64},"word ",[54,44847,82],{"class":81},[54,44849,830],{"class":544},[54,44851,10399],{"class":548},[54,44853,5239],{"class":544},[54,44855,44856,44858,44860,44862,44864,44866,44868,44871],{"class":56,"line":112},[54,44857,116],{"class":115},[54,44859,96],{"class":88},[54,44861,26],{"class":92},[54,44863,89],{"class":88},[54,44865,39072],{"class":92},[54,44867,96],{"class":88},[54,44869,44870],{"class":92},"word",[54,44872,382],{"class":88},[14,44874,44875,44876,89],{},"If you need help with lists, see ",[192,44877,40734],{"href":44878},"\u002Flearn\u002Fpython-lists-explained-beginner-guide",[14,44880,44881],{},"Sets and dictionaries are not good direct choices here because they are not sequences in this use.",[14,44883,44884,44885,147],{},"For example, this is not the normal way to use ",[17,44886,973],{},[45,44888,44890],{"className":47,"code":44889,"language":49,"meta":50,"style":50},"data = {\"a\", \"b\", \"c\"}\n",[17,44891,44892],{"__ignoreMap":50},[54,44893,44894,44896,44898,44900,44902,44904,44906,44908,44910,44912,44914,44916,44918,44920,44922],{"class":56,"line":57},[54,44895,8221],{"class":64},[54,44897,82],{"class":81},[54,44899,8226],{"class":88},[54,44901,545],{"class":544},[54,44903,192],{"class":548},[54,44905,545],{"class":544},[54,44907,103],{"class":88},[54,44909,830],{"class":544},[54,44911,241],{"class":548},[54,44913,545],{"class":544},[54,44915,103],{"class":88},[54,44917,830],{"class":544},[54,44919,9099],{"class":548},[54,44921,545],{"class":544},[54,44923,8258],{"class":88},[14,44925,44926],{},"If your data is in a set or dictionary, convert it to a list first:",[45,44928,44930],{"className":47,"code":44929,"language":49,"meta":50,"style":50},"import random\n\ndata = {\"a\", \"b\", \"c\"}\nitem = random.choice(list(data))\nprint(item)\n",[17,44931,44932,44938,44942,44974,44997],{"__ignoreMap":50},[54,44933,44934,44936],{"class":56,"line":57},[54,44935,61],{"class":60},[54,44937,65],{"class":64},[54,44939,44940],{"class":56,"line":68},[54,44941,72],{"emptyLinePlaceholder":71},[54,44943,44944,44946,44948,44950,44952,44954,44956,44958,44960,44962,44964,44966,44968,44970,44972],{"class":56,"line":75},[54,44945,8221],{"class":64},[54,44947,82],{"class":81},[54,44949,8226],{"class":88},[54,44951,545],{"class":544},[54,44953,192],{"class":548},[54,44955,545],{"class":544},[54,44957,103],{"class":88},[54,44959,830],{"class":544},[54,44961,241],{"class":548},[54,44963,545],{"class":544},[54,44965,103],{"class":88},[54,44967,830],{"class":544},[54,44969,9099],{"class":548},[54,44971,545],{"class":544},[54,44973,8258],{"class":88},[54,44975,44976,44979,44981,44983,44985,44987,44989,44991,44993,44995],{"class":56,"line":112},[54,44977,44978],{"class":64},"item ",[54,44980,82],{"class":81},[54,44982,85],{"class":64},[54,44984,89],{"class":88},[54,44986,39072],{"class":92},[54,44988,96],{"class":88},[54,44990,430],{"class":429},[54,44992,96],{"class":88},[54,44994,8313],{"class":92},[54,44996,382],{"class":88},[54,44998,44999,45001,45003,45006],{"class":56,"line":887},[54,45000,116],{"class":115},[54,45002,96],{"class":88},[54,45004,45005],{"class":92},"item",[54,45007,109],{"class":88},[40,45009,45011],{"id":45010},"what-happens-with-an-empty-sequence","What happens with an empty sequence",[14,45013,45014,45015,45017],{},"If the sequence is empty, ",[17,45016,973],{}," raises an error.",[14,45019,253],{},[45,45021,45022],{"className":47,"code":40347,"language":49,"meta":50,"style":50},[17,45023,45024,45030,45034,45042],{"__ignoreMap":50},[54,45025,45026,45028],{"class":56,"line":57},[54,45027,61],{"class":60},[54,45029,65],{"class":64},[54,45031,45032],{"class":56,"line":68},[54,45033,72],{"emptyLinePlaceholder":71},[54,45035,45036,45038,45040],{"class":56,"line":75},[54,45037,17899],{"class":64},[54,45039,82],{"class":81},[54,45041,40368],{"class":88},[54,45043,45044,45046,45048,45050,45052,45054,45056,45058],{"class":56,"line":112},[54,45045,116],{"class":115},[54,45047,96],{"class":88},[54,45049,26],{"class":92},[54,45051,89],{"class":88},[54,45053,39072],{"class":92},[54,45055,96],{"class":88},[54,45057,10388],{"class":92},[54,45059,382],{"class":88},[14,45061,45062],{},"This causes an error because there is nothing to choose from.",[14,45064,45065],{},"A simple way to prevent this is to check first:",[45,45067,45069],{"className":47,"code":45068,"language":49,"meta":50,"style":50},"import random\n\nitems = []\n\nif items:\n    print(random.choice(items))\nelse:\n    print(\"The list is empty.\")\n",[17,45070,45071,45077,45081,45089,45093,45101,45119,45125],{"__ignoreMap":50},[54,45072,45073,45075],{"class":56,"line":57},[54,45074,61],{"class":60},[54,45076,65],{"class":64},[54,45078,45079],{"class":56,"line":68},[54,45080,72],{"emptyLinePlaceholder":71},[54,45082,45083,45085,45087],{"class":56,"line":75},[54,45084,17899],{"class":64},[54,45086,82],{"class":81},[54,45088,40368],{"class":88},[54,45090,45091],{"class":56,"line":112},[54,45092,72],{"emptyLinePlaceholder":71},[54,45094,45095,45097,45099],{"class":56,"line":887},[54,45096,22942],{"class":60},[54,45098,26868],{"class":64},[54,45100,7572],{"class":88},[54,45102,45103,45105,45107,45109,45111,45113,45115,45117],{"class":56,"line":892},[54,45104,7606],{"class":115},[54,45106,96],{"class":88},[54,45108,26],{"class":92},[54,45110,89],{"class":88},[54,45112,39072],{"class":92},[54,45114,96],{"class":88},[54,45116,10388],{"class":92},[54,45118,382],{"class":88},[54,45120,45121,45123],{"class":56,"line":913},[54,45122,22975],{"class":60},[54,45124,7572],{"class":88},[54,45126,45127,45129,45131,45133,45136,45138],{"class":56,"line":2121},[54,45128,7606],{"class":115},[54,45130,96],{"class":88},[54,45132,545],{"class":544},[54,45134,45135],{"class":548},"The list is empty.",[54,45137,545],{"class":544},[54,45139,109],{"class":88},[14,45141,45142,45143,45145,45146,45148],{},"This works because an empty list is treated as ",[17,45144,8767],{}," in an ",[17,45147,22942],{}," statement.",[14,45150,45151],{},"You can also check the length:",[45,45153,45155],{"className":47,"code":45154,"language":49,"meta":50,"style":50},"import random\n\nitems = [\"apple\", \"banana\"]\n\nif len(items) > 0:\n    print(random.choice(items))\n",[17,45156,45157,45163,45167,45191,45195,45213],{"__ignoreMap":50},[54,45158,45159,45161],{"class":56,"line":57},[54,45160,61],{"class":60},[54,45162,65],{"class":64},[54,45164,45165],{"class":56,"line":68},[54,45166,72],{"emptyLinePlaceholder":71},[54,45168,45169,45171,45173,45175,45177,45179,45181,45183,45185,45187,45189],{"class":56,"line":75},[54,45170,17899],{"class":64},[54,45172,82],{"class":81},[54,45174,818],{"class":88},[54,45176,545],{"class":544},[54,45178,10399],{"class":548},[54,45180,545],{"class":544},[54,45182,103],{"class":88},[54,45184,830],{"class":544},[54,45186,10408],{"class":548},[54,45188,545],{"class":544},[54,45190,469],{"class":88},[54,45192,45193],{"class":56,"line":112},[54,45194,72],{"emptyLinePlaceholder":71},[54,45196,45197,45199,45201,45203,45205,45207,45209,45211],{"class":56,"line":887},[54,45198,22942],{"class":60},[54,45200,868],{"class":115},[54,45202,96],{"class":88},[54,45204,10388],{"class":92},[54,45206,876],{"class":88},[54,45208,41454],{"class":81},[54,45210,3986],{"class":99},[54,45212,7572],{"class":88},[54,45214,45215,45217,45219,45221,45223,45225,45227,45229],{"class":56,"line":892},[54,45216,7606],{"class":115},[54,45218,96],{"class":88},[54,45220,26],{"class":92},[54,45222,89],{"class":88},[54,45224,39072],{"class":92},[54,45226,96],{"class":88},[54,45228,10388],{"class":92},[54,45230,382],{"class":88},[40,45232,45234],{"id":45233},"return-value","Return value",[14,45236,45237,20564,45239,45242],{},[17,45238,973],{},[35,45240,45241],{},"one item"," from the sequence.",[14,45244,45245],{},"The type of the result depends on what was chosen.",[14,45247,31841],{},[45,45249,45251],{"className":47,"code":45250,"language":49,"meta":50,"style":50},"import random\n\nprint(random.choice([1, 2, 3]))      # int\nprint(random.choice([\"a\", \"b\", \"c\"]))  # str\nprint(random.choice(\"hello\"))        # one-character str\n",[17,45252,45253,45259,45263,45294,45335],{"__ignoreMap":50},[54,45254,45255,45257],{"class":56,"line":57},[54,45256,61],{"class":60},[54,45258,65],{"class":64},[54,45260,45261],{"class":56,"line":68},[54,45262,72],{"emptyLinePlaceholder":71},[54,45264,45265,45267,45269,45271,45273,45275,45278,45280,45282,45284,45286,45288,45291],{"class":56,"line":75},[54,45266,116],{"class":115},[54,45268,96],{"class":88},[54,45270,26],{"class":92},[54,45272,89],{"class":88},[54,45274,39072],{"class":92},[54,45276,45277],{"class":88},"([",[54,45279,100],{"class":99},[54,45281,103],{"class":88},[54,45283,466],{"class":99},[54,45285,103],{"class":88},[54,45287,379],{"class":99},[54,45289,45290],{"class":88},"]))",[54,45292,45293],{"class":4313},"      # int\n",[54,45295,45296,45298,45300,45302,45304,45306,45308,45310,45312,45314,45316,45318,45320,45322,45324,45326,45328,45330,45332],{"class":56,"line":112},[54,45297,116],{"class":115},[54,45299,96],{"class":88},[54,45301,26],{"class":92},[54,45303,89],{"class":88},[54,45305,39072],{"class":92},[54,45307,45277],{"class":88},[54,45309,545],{"class":544},[54,45311,192],{"class":548},[54,45313,545],{"class":544},[54,45315,103],{"class":88},[54,45317,830],{"class":544},[54,45319,241],{"class":548},[54,45321,545],{"class":544},[54,45323,103],{"class":88},[54,45325,830],{"class":544},[54,45327,9099],{"class":548},[54,45329,545],{"class":544},[54,45331,45290],{"class":88},[54,45333,45334],{"class":4313},"  # str\n",[54,45336,45337,45339,45341,45343,45345,45347,45349,45351,45353,45355,45357],{"class":56,"line":887},[54,45338,116],{"class":115},[54,45340,96],{"class":88},[54,45342,26],{"class":92},[54,45344,89],{"class":88},[54,45346,39072],{"class":92},[54,45348,96],{"class":88},[54,45350,545],{"class":544},[54,45352,41557],{"class":548},[54,45354,545],{"class":544},[54,45356,4310],{"class":88},[54,45358,45359],{"class":4313},"        # one-character str\n",[14,45361,45362],{},"If you choose from a string, the result is a one-character string, not a list and not a longer substring.",[40,45364,15426,45366,45368],{"id":45365},"when-to-use-randomchoice-vs-other-random-tools",[17,45367,973],{}," vs other random tools",[14,45370,45371],{},"Use the right tool for the job:",[149,45373,45374,45381,45388,45396],{},[152,45375,126,45376,45378,45379],{},[17,45377,973],{}," for ",[35,45380,44137],{},[152,45382,126,45383,45387],{},[192,45384,45385],{"href":1579},[17,45386,19],{}," for a random integer in a range",[152,45389,126,45390,45395],{},[192,45391,45392],{"href":39985},[17,45393,45394],{},"random.shuffle()"," to reorder a list",[152,45397,1403,45398,45400],{},[17,45399,973],{}," when you need multiple unique items",[14,45402,45403,45404,45406],{},"Example of ",[17,45405,973],{}," for one item:",[45,45408,45410],{"className":47,"code":45409,"language":49,"meta":50,"style":50},"import random\n\nfruits = [\"apple\", \"banana\", \"orange\"]\nprint(random.choice(fruits))\n",[17,45411,45412,45418,45422,45455],{"__ignoreMap":50},[54,45413,45414,45416],{"class":56,"line":57},[54,45415,61],{"class":60},[54,45417,65],{"class":64},[54,45419,45420],{"class":56,"line":68},[54,45421,72],{"emptyLinePlaceholder":71},[54,45423,45424,45426,45428,45430,45432,45434,45436,45438,45440,45442,45444,45446,45448,45451,45453],{"class":56,"line":75},[54,45425,39490],{"class":64},[54,45427,82],{"class":81},[54,45429,818],{"class":88},[54,45431,545],{"class":544},[54,45433,10399],{"class":548},[54,45435,545],{"class":544},[54,45437,103],{"class":88},[54,45439,830],{"class":544},[54,45441,10408],{"class":548},[54,45443,545],{"class":544},[54,45445,103],{"class":88},[54,45447,830],{"class":544},[54,45449,45450],{"class":548},"orange",[54,45452,545],{"class":544},[54,45454,469],{"class":88},[54,45456,45457,45459,45461,45463,45465,45467,45469,45471],{"class":56,"line":112},[54,45458,116],{"class":115},[54,45460,96],{"class":88},[54,45462,26],{"class":92},[54,45464,89],{"class":88},[54,45466,39072],{"class":92},[54,45468,96],{"class":88},[54,45470,39535],{"class":92},[54,45472,382],{"class":88},[14,45474,45403,45475,45477],{},[17,45476,19],{}," for a number in a range:",[45,45479,45480],{"className":47,"code":39288,"language":49,"meta":50,"style":50},[17,45481,45482,45488,45492],{"__ignoreMap":50},[54,45483,45484,45486],{"class":56,"line":57},[54,45485,61],{"class":60},[54,45487,65],{"class":64},[54,45489,45490],{"class":56,"line":68},[54,45491,72],{"emptyLinePlaceholder":71},[54,45493,45494,45496,45498,45500,45502,45504,45506,45508,45510,45512],{"class":56,"line":75},[54,45495,116],{"class":115},[54,45497,96],{"class":88},[54,45499,26],{"class":92},[54,45501,89],{"class":88},[54,45503,93],{"class":92},[54,45505,96],{"class":88},[54,45507,100],{"class":99},[54,45509,103],{"class":88},[54,45511,533],{"class":99},[54,45513,382],{"class":88},[14,45515,45403,45516,45518],{},[17,45517,45394],{}," to reorder a list:",[45,45520,45522],{"className":47,"code":45521,"language":49,"meta":50,"style":50},"import random\n\ncards = [\"A\", \"K\", \"Q\", \"J\"]\nrandom.shuffle(cards)\nprint(cards)\n",[17,45523,45524,45530,45534,45579,45594],{"__ignoreMap":50},[54,45525,45526,45528],{"class":56,"line":57},[54,45527,61],{"class":60},[54,45529,65],{"class":64},[54,45531,45532],{"class":56,"line":68},[54,45533,72],{"emptyLinePlaceholder":71},[54,45535,45536,45539,45541,45543,45545,45548,45550,45552,45554,45557,45559,45561,45563,45566,45568,45570,45572,45575,45577],{"class":56,"line":75},[54,45537,45538],{"class":64},"cards ",[54,45540,82],{"class":81},[54,45542,818],{"class":88},[54,45544,545],{"class":544},[54,45546,45547],{"class":548},"A",[54,45549,545],{"class":544},[54,45551,103],{"class":88},[54,45553,830],{"class":544},[54,45555,45556],{"class":548},"K",[54,45558,545],{"class":544},[54,45560,103],{"class":88},[54,45562,830],{"class":544},[54,45564,45565],{"class":548},"Q",[54,45567,545],{"class":544},[54,45569,103],{"class":88},[54,45571,830],{"class":544},[54,45573,45574],{"class":548},"J",[54,45576,545],{"class":544},[54,45578,469],{"class":88},[54,45580,45581,45583,45585,45587,45589,45592],{"class":56,"line":112},[54,45582,26],{"class":64},[54,45584,89],{"class":88},[54,45586,39926],{"class":92},[54,45588,96],{"class":88},[54,45590,45591],{"class":92},"cards",[54,45593,109],{"class":88},[54,45595,45596,45598,45600,45602],{"class":56,"line":887},[54,45597,116],{"class":115},[54,45599,96],{"class":88},[54,45601,45591],{"class":92},[54,45603,109],{"class":88},[14,45605,45606,45607,45609],{},"If you need several unique selections, ",[17,45608,973],{}," is not the best option because it picks only one item per call and can repeat values.",[40,45611,978],{"id":977},[14,45613,45614,45615,147],{},"Here are some common problems beginners run into with ",[17,45616,973],{},[149,45618,45619,45625,45628,45631],{},[152,45620,17008,45621,1494,45623],{},[17,45622,26],{},[17,45624,973],{},[152,45626,45627],{},"Passing an empty list, tuple, or string",[152,45629,45630],{},"Trying to use it directly on data that is not a sequence",[152,45632,45633],{},"Expecting more than one item to be returned",[602,45635,17008,45636],{"id":40279},[17,45637,26],{},[14,45639,33166],{},[45,45641,45643],{"className":47,"code":45642,"language":49,"meta":50,"style":50},"colors = [\"red\", \"blue\", \"green\"]\nprint(random.choice(colors))\n",[17,45644,45645,45677],{"__ignoreMap":50},[54,45646,45647,45649,45651,45653,45655,45657,45659,45661,45663,45665,45667,45669,45671,45673,45675],{"class":56,"line":57},[54,45648,39636],{"class":64},[54,45650,82],{"class":81},[54,45652,818],{"class":88},[54,45654,545],{"class":544},[54,45656,39645],{"class":548},[54,45658,545],{"class":544},[54,45660,103],{"class":88},[54,45662,830],{"class":544},[54,45664,39654],{"class":548},[54,45666,545],{"class":544},[54,45668,103],{"class":88},[54,45670,830],{"class":544},[54,45672,39663],{"class":548},[54,45674,545],{"class":544},[54,45676,469],{"class":88},[54,45678,45679,45681,45683,45685,45687,45689,45691,45693],{"class":56,"line":68},[54,45680,116],{"class":115},[54,45682,96],{"class":88},[54,45684,26],{"class":92},[54,45686,89],{"class":88},[54,45688,39072],{"class":92},[54,45690,96],{"class":88},[54,45692,39685],{"class":92},[54,45694,382],{"class":88},[14,45696,45697],{},"Fix it by importing the module:",[45,45699,45700],{"className":47,"code":44702,"language":49,"meta":50,"style":50},[17,45701,45702,45708,45712,45744],{"__ignoreMap":50},[54,45703,45704,45706],{"class":56,"line":57},[54,45705,61],{"class":60},[54,45707,65],{"class":64},[54,45709,45710],{"class":56,"line":68},[54,45711,72],{"emptyLinePlaceholder":71},[54,45713,45714,45716,45718,45720,45722,45724,45726,45728,45730,45732,45734,45736,45738,45740,45742],{"class":56,"line":75},[54,45715,39636],{"class":64},[54,45717,82],{"class":81},[54,45719,818],{"class":88},[54,45721,545],{"class":544},[54,45723,39645],{"class":548},[54,45725,545],{"class":544},[54,45727,103],{"class":88},[54,45729,830],{"class":544},[54,45731,39654],{"class":548},[54,45733,545],{"class":544},[54,45735,103],{"class":88},[54,45737,830],{"class":544},[54,45739,39663],{"class":548},[54,45741,545],{"class":544},[54,45743,469],{"class":88},[54,45745,45746,45748,45750,45752,45754,45756,45758,45760],{"class":56,"line":112},[54,45747,116],{"class":115},[54,45749,96],{"class":88},[54,45751,26],{"class":92},[54,45753,89],{"class":88},[54,45755,39072],{"class":92},[54,45757,96],{"class":88},[54,45759,39685],{"class":92},[54,45761,382],{"class":88},[602,45763,45765],{"id":45764},"passing-an-empty-sequence","Passing an empty sequence",[14,45767,45768],{},"This causes an error:",[45,45770,45771],{"className":47,"code":40347,"language":49,"meta":50,"style":50},[17,45772,45773,45779,45783,45791],{"__ignoreMap":50},[54,45774,45775,45777],{"class":56,"line":57},[54,45776,61],{"class":60},[54,45778,65],{"class":64},[54,45780,45781],{"class":56,"line":68},[54,45782,72],{"emptyLinePlaceholder":71},[54,45784,45785,45787,45789],{"class":56,"line":75},[54,45786,17899],{"class":64},[54,45788,82],{"class":81},[54,45790,40368],{"class":88},[54,45792,45793,45795,45797,45799,45801,45803,45805,45807],{"class":56,"line":112},[54,45794,116],{"class":115},[54,45796,96],{"class":88},[54,45798,26],{"class":92},[54,45800,89],{"class":88},[54,45802,39072],{"class":92},[54,45804,96],{"class":88},[54,45806,10388],{"class":92},[54,45808,382],{"class":88},[14,45810,45811],{},"Fix it by checking first:",[45,45813,45815],{"className":47,"code":45814,"language":49,"meta":50,"style":50},"import random\n\nitems = []\n\nif items:\n    print(random.choice(items))\nelse:\n    print(\"No items available\")\n",[17,45816,45817,45823,45827,45835,45839,45847,45865,45871],{"__ignoreMap":50},[54,45818,45819,45821],{"class":56,"line":57},[54,45820,61],{"class":60},[54,45822,65],{"class":64},[54,45824,45825],{"class":56,"line":68},[54,45826,72],{"emptyLinePlaceholder":71},[54,45828,45829,45831,45833],{"class":56,"line":75},[54,45830,17899],{"class":64},[54,45832,82],{"class":81},[54,45834,40368],{"class":88},[54,45836,45837],{"class":56,"line":112},[54,45838,72],{"emptyLinePlaceholder":71},[54,45840,45841,45843,45845],{"class":56,"line":887},[54,45842,22942],{"class":60},[54,45844,26868],{"class":64},[54,45846,7572],{"class":88},[54,45848,45849,45851,45853,45855,45857,45859,45861,45863],{"class":56,"line":892},[54,45850,7606],{"class":115},[54,45852,96],{"class":88},[54,45854,26],{"class":92},[54,45856,89],{"class":88},[54,45858,39072],{"class":92},[54,45860,96],{"class":88},[54,45862,10388],{"class":92},[54,45864,382],{"class":88},[54,45866,45867,45869],{"class":56,"line":913},[54,45868,22975],{"class":60},[54,45870,7572],{"class":88},[54,45872,45873,45875,45877,45879,45882,45884],{"class":56,"line":2121},[54,45874,7606],{"class":115},[54,45876,96],{"class":88},[54,45878,545],{"class":544},[54,45880,45881],{"class":548},"No items available",[54,45883,545],{"class":544},[54,45885,109],{"class":88},[602,45887,45889],{"id":45888},"passing-the-wrong-kind-of-value","Passing the wrong kind of value",[14,45891,45892,45893,45895],{},"If the value is not a sequence, ",[17,45894,973],{}," is the wrong tool.",[14,45897,45898],{},"For example, if you have a dictionary:",[45,45900,45902],{"className":47,"code":45901,"language":49,"meta":50,"style":50},"import random\n\ndata = {\"name\": \"Ana\", \"age\": 20}\nprint(random.choice(list(data)))\n",[17,45903,45904,45910,45914,45950],{"__ignoreMap":50},[54,45905,45906,45908],{"class":56,"line":57},[54,45907,61],{"class":60},[54,45909,65],{"class":64},[54,45911,45912],{"class":56,"line":68},[54,45913,72],{"emptyLinePlaceholder":71},[54,45915,45916,45918,45920,45922,45924,45926,45928,45930,45932,45934,45936,45938,45940,45942,45944,45946,45948],{"class":56,"line":75},[54,45917,8221],{"class":64},[54,45919,82],{"class":81},[54,45921,8226],{"class":88},[54,45923,545],{"class":544},[54,45925,8231],{"class":548},[54,45927,545],{"class":544},[54,45929,147],{"class":88},[54,45931,830],{"class":544},[54,45933,823],{"class":548},[54,45935,545],{"class":544},[54,45937,103],{"class":88},[54,45939,830],{"class":544},[54,45941,8249],{"class":548},[54,45943,545],{"class":544},[54,45945,147],{"class":88},[54,45947,695],{"class":99},[54,45949,8258],{"class":88},[54,45951,45952,45954,45956,45958,45960,45962,45964,45966,45968,45970],{"class":56,"line":112},[54,45953,116],{"class":115},[54,45955,96],{"class":88},[54,45957,26],{"class":92},[54,45959,89],{"class":88},[54,45961,39072],{"class":92},[54,45963,96],{"class":88},[54,45965,430],{"class":429},[54,45967,96],{"class":88},[54,45969,8313],{"class":92},[54,45971,446],{"class":88},[14,45973,45974],{},"This converts the dictionary keys to a list first.",[602,45976,45978],{"id":45977},"expecting-more-than-one-result","Expecting more than one result",[14,45980,45981],{},"This returns only one item:",[45,45983,45985],{"className":47,"code":45984,"language":49,"meta":50,"style":50},"import random\n\nnumbers = [1, 2, 3, 4]\nresult = random.choice(numbers)\nprint(result)\n",[17,45986,45987,45993,45997,46021,46039],{"__ignoreMap":50},[54,45988,45989,45991],{"class":56,"line":57},[54,45990,61],{"class":60},[54,45992,65],{"class":64},[54,45994,45995],{"class":56,"line":68},[54,45996,72],{"emptyLinePlaceholder":71},[54,45998,45999,46001,46003,46005,46007,46009,46011,46013,46015,46017,46019],{"class":56,"line":75},[54,46000,11576],{"class":64},[54,46002,82],{"class":81},[54,46004,818],{"class":88},[54,46006,100],{"class":99},[54,46008,103],{"class":88},[54,46010,466],{"class":99},[54,46012,103],{"class":88},[54,46014,379],{"class":99},[54,46016,103],{"class":88},[54,46018,4264],{"class":99},[54,46020,469],{"class":88},[54,46022,46023,46025,46027,46029,46031,46033,46035,46037],{"class":56,"line":112},[54,46024,3833],{"class":64},[54,46026,82],{"class":81},[54,46028,85],{"class":64},[54,46030,89],{"class":88},[54,46032,39072],{"class":92},[54,46034,96],{"class":88},[54,46036,9956],{"class":92},[54,46038,109],{"class":88},[54,46040,46041,46043,46045,46047],{"class":56,"line":887},[54,46042,116],{"class":115},[54,46044,96],{"class":88},[54,46046,3866],{"class":92},[54,46048,109],{"class":88},[14,46050,46051,46052,89],{},"If you need to work with list values more confidently, it also helps to know ",[192,46053,46055],{"href":46054},"\u002Fhow-to\u002Fhow-to-check-if-a-value-exists-in-a-list-in-python","how to check if a value exists in a list in Python",[14,46057,27616],{},[45,46059,46061],{"className":47,"code":46060,"language":49,"meta":50,"style":50},"print(sequence)\nprint(type(sequence))\nprint(len(sequence))\nimport random; print(random.choice([1, 2, 3]))\n",[17,46062,46063,46073,46087,46102],{"__ignoreMap":50},[54,46064,46065,46067,46069,46071],{"class":56,"line":57},[54,46066,116],{"class":115},[54,46068,96],{"class":88},[54,46070,44304],{"class":92},[54,46072,109],{"class":88},[54,46074,46075,46077,46079,46081,46083,46085],{"class":56,"line":68},[54,46076,116],{"class":115},[54,46078,96],{"class":88},[54,46080,2208],{"class":429},[54,46082,96],{"class":88},[54,46084,44304],{"class":92},[54,46086,382],{"class":88},[54,46088,46089,46091,46093,46096,46098,46100],{"class":56,"line":75},[54,46090,116],{"class":115},[54,46092,96],{"class":88},[54,46094,46095],{"class":115},"len",[54,46097,96],{"class":88},[54,46099,44304],{"class":92},[54,46101,382],{"class":88},[54,46103,46104,46106,46109,46111,46113,46115,46117,46119,46121,46123,46125,46127,46129,46131],{"class":56,"line":112},[54,46105,61],{"class":60},[54,46107,46108],{"class":64}," random; ",[54,46110,116],{"class":115},[54,46112,96],{"class":88},[54,46114,26],{"class":92},[54,46116,89],{"class":88},[54,46118,39072],{"class":92},[54,46120,45277],{"class":88},[54,46122,100],{"class":99},[54,46124,103],{"class":88},[54,46126,466],{"class":99},[54,46128,103],{"class":88},[54,46130,379],{"class":99},[54,46132,14245],{"class":88},[14,46134,13427],{},[149,46136,46137,46140,46143],{},[152,46138,46139],{},"what value you passed in",[152,46141,46142],{},"whether it is the right type",[152,46144,46145],{},"whether it is empty",[40,46147,1419],{"id":1418},[602,46149,8346,46151,46153],{"id":46150},"does-randomchoice-return-more-than-one-item",[17,46152,973],{}," return more than one item?",[14,46155,46156],{},"No. It returns one random item each time you call it.",[602,46158,37466,46160,46162],{"id":46159},"can-i-use-randomchoice-with-a-string",[17,46161,973],{}," with a string?",[14,46164,46165],{},"Yes. It returns one random character from the string.",[602,46167,3403,46169,46171],{"id":46168},"why-does-randomchoice-fail-on-an-empty-list",[17,46170,973],{}," fail on an empty list?",[14,46173,46174],{},"Because there is no item to choose. Check that the sequence is not empty first.",[602,46176,3449,46178,46180],{"id":46177},"is-randomchoice-truly-random",[17,46179,973],{}," truly random?",[14,46182,46183],{},"It is pseudorandom. It is fine for beginner programs, games, and simple scripts.",[40,46185,1510],{"id":1509},[149,46187,46188,46192,46197,46202,46207],{},[152,46189,46190],{},[192,46191,195],{"href":194},[152,46193,46194],{},[192,46195,46196],{"href":1579},"random.randint() function explained",[152,46198,46199],{},[192,46200,46201],{"href":39985},"random.shuffle() function explained",[152,46203,46204],{},[192,46205,46206],{"href":46054},"How to check if a value exists in a list in Python",[152,46208,46209],{},[192,46210,40734],{"href":44878},[14,46212,46213,46214,46216],{},"Now try using ",[17,46215,973],{}," in a small practice script, such as a name picker, a simple game, or a menu selector.",[1545,46218,46219],{},"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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}",{"title":50,"searchDepth":68,"depth":68,"links":46221},[46222,46224,46225,46230,46235,46236,46237,46239,46245,46255],{"id":44239,"depth":68,"text":46223},"What random.choice() does",{"id":198,"depth":68,"text":199},{"id":44422,"depth":68,"text":44423,"children":46226},[46227,46228,46229],{"id":44426,"depth":75,"text":44427},{"id":44524,"depth":75,"text":44525},{"id":44604,"depth":75,"text":44605},{"id":44675,"depth":68,"text":44676,"children":46231},[46232,46233,46234],{"id":44698,"depth":75,"text":44699},{"id":44767,"depth":75,"text":44768},{"id":44824,"depth":75,"text":44825},{"id":45010,"depth":68,"text":45011},{"id":45233,"depth":68,"text":45234},{"id":45365,"depth":68,"text":46238},"When to use random.choice() vs other random tools",{"id":977,"depth":68,"text":978,"children":46240},[46241,46242,46243,46244],{"id":40279,"depth":75,"text":40920},{"id":45764,"depth":75,"text":45765},{"id":45888,"depth":75,"text":45889},{"id":45977,"depth":75,"text":45978},{"id":1418,"depth":68,"text":1419,"children":46246},[46247,46249,46251,46253],{"id":46150,"depth":75,"text":46248},"Does random.choice() return more than one item?",{"id":46159,"depth":75,"text":46250},"Can I use random.choice() with a string?",{"id":46168,"depth":75,"text":46252},"Why does random.choice() fail on an empty list?",{"id":46177,"depth":75,"text":46254},"Is random.choice() truly random?",{"id":1509,"depth":68,"text":1510},"Master random.choice function explained in our comprehensive Python beginner guide.",{},{"title":44122,"description":46256},"standard-library\u002Frandom.choice-function-explained","DDl4P3hcWPD8LY4CQmHjTZ3d7IgQfHJK86R3hiUVDxc",{"id":4,"title":5,"body":46262,"description":1576,"extension":1577,"meta":47530,"navigation":71,"path":1579,"seo":47531,"stem":1581,"__hash__":1582},{"type":7,"value":46263,"toc":47502},[46264,46266,46270,46280,46282,46328,46336,46338,46342,46356,46358,46368,46372,46374,46376,46398,46400,46416,46418,46464,46466,46474,46482,46484,46490,46492,46528,46530,46544,46550,46552,46578,46580,46596,46598,46612,46614,46616,46670,46672,46684,46686,46688,46698,46700,46702,46748,46750,46796,46800,46802,46856,46858,46860,46864,46986,46988,47008,47014,47016,47018,47022,47024,47060,47062,47108,47114,47116,47118,47140,47146,47150,47152,47156,47158,47204,47206,47208,47254,47256,47258,47304,47308,47310,47356,47358,47364,47366,47394,47400,47402,47404,47406,47408,47410,47412,47450,47452,47456,47458,47464,47466,47470,47472,47500],[10,46265,5],{"id":12},[14,46267,46268,20],{},[17,46269,19],{},[14,46271,23,46272,27,46274,30,46276,33,46278,38],{},[17,46273,26],{},[17,46275,26],{},[17,46277,19],{},[35,46279,37],{},[40,46281,43],{"id":42},[45,46283,46284],{"className":47,"code":48,"language":49,"meta":50,"style":50},[17,46285,46286,46292,46296,46318],{"__ignoreMap":50},[54,46287,46288,46290],{"class":56,"line":57},[54,46289,61],{"class":60},[54,46291,65],{"class":64},[54,46293,46294],{"class":56,"line":68},[54,46295,72],{"emptyLinePlaceholder":71},[54,46297,46298,46300,46302,46304,46306,46308,46310,46312,46314,46316],{"class":56,"line":75},[54,46299,78],{"class":64},[54,46301,82],{"class":81},[54,46303,85],{"class":64},[54,46305,89],{"class":88},[54,46307,93],{"class":92},[54,46309,96],{"class":88},[54,46311,100],{"class":99},[54,46313,103],{"class":88},[54,46315,106],{"class":99},[54,46317,109],{"class":88},[54,46319,46320,46322,46324,46326],{"class":56,"line":112},[54,46321,116],{"class":115},[54,46323,96],{"class":88},[54,46325,121],{"class":92},[54,46327,109],{"class":88},[14,46329,126,46330,130,46332,134,46334,138],{},[17,46331,129],{},[17,46333,133],{},[17,46335,137],{},[40,46337,142],{"id":141},[14,46339,46340,147],{},[17,46341,19],{},[149,46343,46344,46346,46350,46354],{},[152,46345,154],{},[152,46347,157,46348,160],{},[17,46349,26],{},[152,46351,163,46352,167],{},[17,46353,166],{},[152,46355,170],{},[14,46357,173],{},[149,46359,46360,46362,46364,46366],{},[152,46361,178],{},[152,46363,181],{},[152,46365,184],{},[152,46367,187],{},[14,46369,190,46370,89],{},[192,46371,195],{"href":194},[40,46373,199],{"id":198},[14,46375,202],{},[45,46377,46378],{"className":47,"code":205,"language":49,"meta":50,"style":50},[17,46379,46380],{"__ignoreMap":50},[54,46381,46382,46384,46386,46388,46390,46392,46394,46396],{"class":56,"line":57},[54,46383,26],{"class":64},[54,46385,89],{"class":88},[54,46387,93],{"class":92},[54,46389,96],{"class":88},[54,46391,192],{"class":92},[54,46393,103],{"class":88},[54,46395,224],{"class":92},[54,46397,109],{"class":88},[14,46399,229],{},[149,46401,46402,46406,46410],{},[152,46403,46404,236],{},[17,46405,192],{},[152,46407,46408,242],{},[17,46409,241],{},[152,46411,245,46412,248,46414],{},[17,46413,192],{},[17,46415,241],{},[14,46417,253],{},[45,46419,46420],{"className":47,"code":256,"language":49,"meta":50,"style":50},[17,46421,46422,46428,46432,46454],{"__ignoreMap":50},[54,46423,46424,46426],{"class":56,"line":57},[54,46425,61],{"class":60},[54,46427,65],{"class":64},[54,46429,46430],{"class":56,"line":68},[54,46431,72],{"emptyLinePlaceholder":71},[54,46433,46434,46436,46438,46440,46442,46444,46446,46448,46450,46452],{"class":56,"line":75},[54,46435,78],{"class":64},[54,46437,82],{"class":81},[54,46439,85],{"class":64},[54,46441,89],{"class":88},[54,46443,93],{"class":92},[54,46445,96],{"class":88},[54,46447,285],{"class":99},[54,46449,103],{"class":88},[54,46451,290],{"class":99},[54,46453,109],{"class":88},[54,46455,46456,46458,46460,46462],{"class":56,"line":112},[54,46457,116],{"class":115},[54,46459,96],{"class":88},[54,46461,121],{"class":92},[54,46463,109],{"class":88},[14,46465,305],{},[45,46467,46468],{"className":47,"code":308,"language":49,"meta":50,"style":50},[17,46469,46470],{"__ignoreMap":50},[54,46471,46472],{"class":56,"line":57},[54,46473,308],{"class":99},[14,46475,317,46476,320,46478,324,46480,89],{},[17,46477,285],{},[17,46479,323],{},[17,46481,327],{},[40,46483,331],{"id":330},[14,46485,334,46486,337,46488,89],{},[17,46487,19],{},[35,46489,340],{},[14,46491,343],{},[45,46493,46494],{"className":47,"code":346,"language":49,"meta":50,"style":50},[17,46495,46496,46502,46506],{"__ignoreMap":50},[54,46497,46498,46500],{"class":56,"line":57},[54,46499,61],{"class":60},[54,46501,65],{"class":64},[54,46503,46504],{"class":56,"line":68},[54,46505,72],{"emptyLinePlaceholder":71},[54,46507,46508,46510,46512,46514,46516,46518,46520,46522,46524,46526],{"class":56,"line":75},[54,46509,116],{"class":115},[54,46511,96],{"class":88},[54,46513,26],{"class":92},[54,46515,89],{"class":88},[54,46517,93],{"class":92},[54,46519,96],{"class":88},[54,46521,100],{"class":99},[54,46523,103],{"class":88},[54,46525,379],{"class":99},[54,46527,382],{"class":88},[14,46529,385],{},[149,46531,46532,46536,46540],{},[152,46533,46534],{},[17,46535,100],{},[152,46537,46538],{},[17,46539,396],{},[152,46541,46542],{},[17,46543,401],{},[14,46545,404,46546,412],{},[192,46547,46548,411],{"href":407},[17,46549,410],{},[14,46551,415],{},[45,46553,46554],{"className":47,"code":418,"language":49,"meta":50,"style":50},[17,46555,46556],{"__ignoreMap":50},[54,46557,46558,46560,46562,46564,46566,46568,46570,46572,46574,46576],{"class":56,"line":57},[54,46559,116],{"class":115},[54,46561,96],{"class":88},[54,46563,430],{"class":429},[54,46565,96],{"class":88},[54,46567,435],{"class":115},[54,46569,96],{"class":88},[54,46571,100],{"class":99},[54,46573,103],{"class":88},[54,46575,379],{"class":99},[54,46577,446],{"class":88},[14,46579,449],{},[45,46581,46582],{"className":47,"code":452,"language":49,"meta":50,"style":50},[17,46583,46584],{"__ignoreMap":50},[54,46585,46586,46588,46590,46592,46594],{"class":56,"line":57},[54,46587,459],{"class":88},[54,46589,100],{"class":99},[54,46591,103],{"class":88},[54,46593,466],{"class":99},[54,46595,469],{"class":88},[14,46597,472],{},[149,46599,46600,46606],{},[152,46601,46602,33,46604],{},[17,46603,479],{},[17,46605,401],{},[152,46607,46608,487,46610],{},[17,46609,486],{},[17,46611,401],{},[40,46613,493],{"id":492},[14,46615,496],{},[45,46617,46618],{"className":47,"code":499,"language":49,"meta":50,"style":50},[17,46619,46620,46626,46630,46652],{"__ignoreMap":50},[54,46621,46622,46624],{"class":56,"line":57},[54,46623,61],{"class":60},[54,46625,65],{"class":64},[54,46627,46628],{"class":56,"line":68},[54,46629,72],{"emptyLinePlaceholder":71},[54,46631,46632,46634,46636,46638,46640,46642,46644,46646,46648,46650],{"class":56,"line":75},[54,46633,516],{"class":64},[54,46635,82],{"class":81},[54,46637,85],{"class":64},[54,46639,89],{"class":88},[54,46641,93],{"class":92},[54,46643,96],{"class":88},[54,46645,100],{"class":99},[54,46647,103],{"class":88},[54,46649,533],{"class":99},[54,46651,109],{"class":88},[54,46653,46654,46656,46658,46660,46662,46664,46666,46668],{"class":56,"line":112},[54,46655,116],{"class":115},[54,46657,96],{"class":88},[54,46659,545],{"class":544},[54,46661,549],{"class":548},[54,46663,545],{"class":544},[54,46665,103],{"class":88},[54,46667,556],{"class":92},[54,46669,109],{"class":88},[14,46671,561],{},[45,46673,46674],{"className":47,"code":564,"language":49,"meta":50,"style":50},[17,46675,46676],{"__ignoreMap":50},[54,46677,46678,46680,46682],{"class":56,"line":57},[54,46679,571],{"class":64},[54,46681,147],{"class":88},[54,46683,576],{"class":99},[14,46685,579],{},[14,46687,582],{},[149,46689,46690,46692,46694],{},[152,46691,587],{},[152,46693,590],{},[152,46695,46696,596],{},[17,46697,595],{},[40,46699,600],{"id":599},[602,46701,605],{"id":604},[45,46703,46704],{"className":47,"code":608,"language":49,"meta":50,"style":50},[17,46705,46706,46712,46716,46738],{"__ignoreMap":50},[54,46707,46708,46710],{"class":56,"line":57},[54,46709,61],{"class":60},[54,46711,65],{"class":64},[54,46713,46714],{"class":56,"line":68},[54,46715,72],{"emptyLinePlaceholder":71},[54,46717,46718,46720,46722,46724,46726,46728,46730,46732,46734,46736],{"class":56,"line":75},[54,46719,625],{"class":64},[54,46721,82],{"class":81},[54,46723,85],{"class":64},[54,46725,89],{"class":88},[54,46727,93],{"class":92},[54,46729,96],{"class":88},[54,46731,100],{"class":99},[54,46733,103],{"class":88},[54,46735,533],{"class":99},[54,46737,109],{"class":88},[54,46739,46740,46742,46744,46746],{"class":56,"line":112},[54,46741,116],{"class":115},[54,46743,96],{"class":88},[54,46745,652],{"class":92},[54,46747,109],{"class":88},[602,46749,658],{"id":657},[45,46751,46752],{"className":47,"code":661,"language":49,"meta":50,"style":50},[17,46753,46754,46760,46764,46786],{"__ignoreMap":50},[54,46755,46756,46758],{"class":56,"line":57},[54,46757,61],{"class":60},[54,46759,65],{"class":64},[54,46761,46762],{"class":56,"line":68},[54,46763,72],{"emptyLinePlaceholder":71},[54,46765,46766,46768,46770,46772,46774,46776,46778,46780,46782,46784],{"class":56,"line":75},[54,46767,678],{"class":64},[54,46769,82],{"class":81},[54,46771,85],{"class":64},[54,46773,89],{"class":88},[54,46775,93],{"class":92},[54,46777,96],{"class":88},[54,46779,100],{"class":99},[54,46781,103],{"class":88},[54,46783,695],{"class":99},[54,46785,109],{"class":88},[54,46787,46788,46790,46792,46794],{"class":56,"line":112},[54,46789,116],{"class":115},[54,46791,96],{"class":88},[54,46793,706],{"class":92},[54,46795,109],{"class":88},[14,46797,711,46798,89],{},[192,46799,715],{"href":714},[602,46801,719],{"id":718},[45,46803,46804],{"className":47,"code":722,"language":49,"meta":50,"style":50},[17,46805,46806,46812,46816,46838],{"__ignoreMap":50},[54,46807,46808,46810],{"class":56,"line":57},[54,46809,61],{"class":60},[54,46811,65],{"class":64},[54,46813,46814],{"class":56,"line":68},[54,46815,72],{"emptyLinePlaceholder":71},[54,46817,46818,46820,46822,46824,46826,46828,46830,46832,46834,46836],{"class":56,"line":75},[54,46819,739],{"class":64},[54,46821,82],{"class":81},[54,46823,85],{"class":64},[54,46825,89],{"class":88},[54,46827,93],{"class":92},[54,46829,96],{"class":88},[54,46831,752],{"class":99},[54,46833,103],{"class":88},[54,46835,757],{"class":99},[54,46837,109],{"class":88},[54,46839,46840,46842,46844,46846,46848,46850,46852,46854],{"class":56,"line":112},[54,46841,116],{"class":115},[54,46843,96],{"class":88},[54,46845,545],{"class":544},[54,46847,770],{"class":548},[54,46849,545],{"class":544},[54,46851,103],{"class":88},[54,46853,777],{"class":92},[54,46855,109],{"class":88},[14,46857,782],{},[602,46859,786],{"id":785},[14,46861,789,46862,793],{},[17,46863,792],{},[45,46865,46866],{"className":47,"code":796,"language":49,"meta":50,"style":50},[17,46867,46868,46874,46878,46910,46942,46946,46964],{"__ignoreMap":50},[54,46869,46870,46872],{"class":56,"line":57},[54,46871,61],{"class":60},[54,46873,65],{"class":64},[54,46875,46876],{"class":56,"line":68},[54,46877,72],{"emptyLinePlaceholder":71},[54,46879,46880,46882,46884,46886,46888,46890,46892,46894,46896,46898,46900,46902,46904,46906,46908],{"class":56,"line":75},[54,46881,813],{"class":64},[54,46883,82],{"class":81},[54,46885,818],{"class":88},[54,46887,545],{"class":544},[54,46889,823],{"class":548},[54,46891,545],{"class":544},[54,46893,103],{"class":88},[54,46895,830],{"class":544},[54,46897,833],{"class":548},[54,46899,545],{"class":544},[54,46901,103],{"class":88},[54,46903,830],{"class":544},[54,46905,842],{"class":548},[54,46907,545],{"class":544},[54,46909,469],{"class":88},[54,46911,46912,46914,46916,46918,46920,46922,46924,46926,46928,46930,46932,46934,46936,46938,46940],{"class":56,"line":112},[54,46913,851],{"class":64},[54,46915,82],{"class":81},[54,46917,85],{"class":64},[54,46919,89],{"class":88},[54,46921,93],{"class":92},[54,46923,96],{"class":88},[54,46925,752],{"class":99},[54,46927,103],{"class":88},[54,46929,868],{"class":115},[54,46931,96],{"class":88},[54,46933,873],{"class":92},[54,46935,876],{"class":88},[54,46937,879],{"class":81},[54,46939,882],{"class":99},[54,46941,109],{"class":88},[54,46943,46944],{"class":56,"line":887},[54,46945,72],{"emptyLinePlaceholder":71},[54,46947,46948,46950,46952,46954,46956,46958,46960,46962],{"class":56,"line":892},[54,46949,116],{"class":115},[54,46951,96],{"class":88},[54,46953,545],{"class":544},[54,46955,901],{"class":548},[54,46957,545],{"class":544},[54,46959,103],{"class":88},[54,46961,908],{"class":92},[54,46963,109],{"class":88},[54,46965,46966,46968,46970,46972,46974,46976,46978,46980,46982,46984],{"class":56,"line":913},[54,46967,116],{"class":115},[54,46969,96],{"class":88},[54,46971,545],{"class":544},[54,46973,922],{"class":548},[54,46975,545],{"class":544},[54,46977,103],{"class":88},[54,46979,929],{"class":92},[54,46981,459],{"class":88},[54,46983,934],{"class":92},[54,46985,937],{"class":88},[14,46987,561],{},[45,46989,46990],{"className":47,"code":942,"language":49,"meta":50,"style":50},[17,46991,46992,47000],{"__ignoreMap":50},[54,46993,46994,46996,46998],{"class":56,"line":57},[54,46995,949],{"class":64},[54,46997,147],{"class":88},[54,46999,954],{"class":99},[54,47001,47002,47004,47006],{"class":56,"line":68},[54,47003,959],{"class":64},[54,47005,147],{"class":88},[54,47007,964],{"class":64},[14,47009,967,47010,974],{},[192,47011,47012],{"href":970},[17,47013,973],{},[40,47015,978],{"id":977},[602,47017,982],{"id":981},[14,47019,985,47020,89],{},[17,47021,988],{},[14,47023,991],{},[45,47025,47026],{"className":47,"code":994,"language":49,"meta":50,"style":50},[17,47027,47028,47050],{"__ignoreMap":50},[54,47029,47030,47032,47034,47036,47038,47040,47042,47044,47046,47048],{"class":56,"line":57},[54,47031,78],{"class":64},[54,47033,82],{"class":81},[54,47035,85],{"class":64},[54,47037,89],{"class":88},[54,47039,93],{"class":92},[54,47041,96],{"class":88},[54,47043,100],{"class":99},[54,47045,103],{"class":88},[54,47047,106],{"class":99},[54,47049,109],{"class":88},[54,47051,47052,47054,47056,47058],{"class":56,"line":68},[54,47053,116],{"class":115},[54,47055,96],{"class":88},[54,47057,121],{"class":92},[54,47059,109],{"class":88},[14,47061,1031],{},[45,47063,47064],{"className":47,"code":48,"language":49,"meta":50,"style":50},[17,47065,47066,47072,47076,47098],{"__ignoreMap":50},[54,47067,47068,47070],{"class":56,"line":57},[54,47069,61],{"class":60},[54,47071,65],{"class":64},[54,47073,47074],{"class":56,"line":68},[54,47075,72],{"emptyLinePlaceholder":71},[54,47077,47078,47080,47082,47084,47086,47088,47090,47092,47094,47096],{"class":56,"line":75},[54,47079,78],{"class":64},[54,47081,82],{"class":81},[54,47083,85],{"class":64},[54,47085,89],{"class":88},[54,47087,93],{"class":92},[54,47089,96],{"class":88},[54,47091,100],{"class":99},[54,47093,103],{"class":88},[54,47095,106],{"class":99},[54,47097,109],{"class":88},[54,47099,47100,47102,47104,47106],{"class":56,"line":112},[54,47101,116],{"class":115},[54,47103,96],{"class":88},[54,47105,121],{"class":92},[54,47107,109],{"class":88},[14,47109,1080,47110,89],{},[192,47111,1084,47112],{"href":1083},[17,47113,1087],{},[602,47115,1091],{"id":1090},[14,47117,1094],{},[45,47119,47120],{"className":47,"code":1097,"language":49,"meta":50,"style":50},[17,47121,47122],{"__ignoreMap":50},[54,47123,47124,47126,47128,47130,47132,47134,47136,47138],{"class":56,"line":57},[54,47125,26],{"class":64},[54,47127,89],{"class":88},[54,47129,93],{"class":92},[54,47131,96],{"class":88},[54,47133,100],{"class":99},[54,47135,103],{"class":88},[54,47137,106],{"class":99},[54,47139,109],{"class":88},[14,47141,1120,47142,248,47144,89],{},[17,47143,100],{},[17,47145,1125],{},[14,47147,1128,47148,1132],{},[17,47149,1131],{},[602,47151,1136],{"id":1135},[14,47153,47154,1141],{},[17,47155,792],{},[14,47157,1144],{},[45,47159,47160],{"className":47,"code":1147,"language":49,"meta":50,"style":50},[17,47161,47162,47168,47172,47194],{"__ignoreMap":50},[54,47163,47164,47166],{"class":56,"line":57},[54,47165,61],{"class":60},[54,47167,65],{"class":64},[54,47169,47170],{"class":56,"line":68},[54,47171,72],{"emptyLinePlaceholder":71},[54,47173,47174,47176,47178,47180,47182,47184,47186,47188,47190,47192],{"class":56,"line":75},[54,47175,78],{"class":64},[54,47177,82],{"class":81},[54,47179,85],{"class":64},[54,47181,89],{"class":88},[54,47183,93],{"class":92},[54,47185,96],{"class":88},[54,47187,1176],{"class":99},[54,47189,103],{"class":88},[54,47191,1181],{"class":99},[54,47193,109],{"class":88},[54,47195,47196,47198,47200,47202],{"class":56,"line":112},[54,47197,116],{"class":115},[54,47199,96],{"class":88},[54,47201,121],{"class":92},[54,47203,109],{"class":88},[14,47205,1196],{},[14,47207,1199],{},[45,47209,47210],{"className":47,"code":1202,"language":49,"meta":50,"style":50},[17,47211,47212,47218,47222,47244],{"__ignoreMap":50},[54,47213,47214,47216],{"class":56,"line":57},[54,47215,61],{"class":60},[54,47217,65],{"class":64},[54,47219,47220],{"class":56,"line":68},[54,47221,72],{"emptyLinePlaceholder":71},[54,47223,47224,47226,47228,47230,47232,47234,47236,47238,47240,47242],{"class":56,"line":75},[54,47225,78],{"class":64},[54,47227,82],{"class":81},[54,47229,85],{"class":64},[54,47231,89],{"class":88},[54,47233,93],{"class":92},[54,47235,96],{"class":88},[54,47237,100],{"class":99},[54,47239,103],{"class":88},[54,47241,1235],{"class":99},[54,47243,109],{"class":88},[54,47245,47246,47248,47250,47252],{"class":56,"line":112},[54,47247,116],{"class":115},[54,47249,96],{"class":88},[54,47251,121],{"class":92},[54,47253,109],{"class":88},[602,47255,1251],{"id":1250},[14,47257,1144],{},[45,47259,47260],{"className":47,"code":1256,"language":49,"meta":50,"style":50},[17,47261,47262,47268,47272,47294],{"__ignoreMap":50},[54,47263,47264,47266],{"class":56,"line":57},[54,47265,61],{"class":60},[54,47267,65],{"class":64},[54,47269,47270],{"class":56,"line":68},[54,47271,72],{"emptyLinePlaceholder":71},[54,47273,47274,47276,47278,47280,47282,47284,47286,47288,47290,47292],{"class":56,"line":75},[54,47275,78],{"class":64},[54,47277,82],{"class":81},[54,47279,85],{"class":64},[54,47281,89],{"class":88},[54,47283,93],{"class":92},[54,47285,96],{"class":88},[54,47287,1131],{"class":99},[54,47289,103],{"class":88},[54,47291,882],{"class":99},[54,47293,109],{"class":88},[54,47295,47296,47298,47300,47302],{"class":56,"line":112},[54,47297,116],{"class":115},[54,47299,96],{"class":88},[54,47301,121],{"class":92},[54,47303,109],{"class":88},[14,47305,1303,47306,1307],{},[17,47307,1306],{},[14,47309,1310],{},[45,47311,47312],{"className":47,"code":48,"language":49,"meta":50,"style":50},[17,47313,47314,47320,47324,47346],{"__ignoreMap":50},[54,47315,47316,47318],{"class":56,"line":57},[54,47317,61],{"class":60},[54,47319,65],{"class":64},[54,47321,47322],{"class":56,"line":68},[54,47323,72],{"emptyLinePlaceholder":71},[54,47325,47326,47328,47330,47332,47334,47336,47338,47340,47342,47344],{"class":56,"line":75},[54,47327,78],{"class":64},[54,47329,82],{"class":81},[54,47331,85],{"class":64},[54,47333,89],{"class":88},[54,47335,93],{"class":92},[54,47337,96],{"class":88},[54,47339,100],{"class":99},[54,47341,103],{"class":88},[54,47343,106],{"class":99},[54,47345,109],{"class":88},[54,47347,47348,47350,47352,47354],{"class":56,"line":112},[54,47349,116],{"class":115},[54,47351,96],{"class":88},[54,47353,121],{"class":92},[54,47355,109],{"class":88},[40,47357,1360],{"id":1359},[14,47359,126,47360,1365,47362,1369],{},[17,47361,19],{},[35,47363,1368],{},[14,47365,1372],{},[149,47367,47368,47376,47382,47390],{},[152,47369,126,47370,1379,47372,248,47374],{},[17,47371,792],{},[17,47373,100],{},[17,47375,323],{},[152,47377,126,47378,1390],{},[192,47379,47380],{"href":970},[17,47381,973],{},[152,47383,126,47384,1396,47386],{},[17,47385,1395],{},[192,47387,47388],{"href":407},[17,47389,410],{},[152,47391,1403,47392,1406],{},[17,47393,792],{},[14,47395,1409,47396,1413,47398,89],{},[17,47397,1412],{},[17,47399,26],{},[40,47401,1419],{"id":1418},[602,47403,1423],{"id":1422},[14,47405,1426],{},[602,47407,1430],{"id":1429},[14,47409,1433],{},[14,47411,253],{},[45,47413,47414],{"className":47,"code":1438,"language":49,"meta":50,"style":50},[17,47415,47416,47422,47426],{"__ignoreMap":50},[54,47417,47418,47420],{"class":56,"line":57},[54,47419,61],{"class":60},[54,47421,65],{"class":64},[54,47423,47424],{"class":56,"line":68},[54,47425,72],{"emptyLinePlaceholder":71},[54,47427,47428,47430,47432,47434,47436,47438,47440,47442,47444,47446,47448],{"class":56,"line":75},[54,47429,116],{"class":115},[54,47431,96],{"class":88},[54,47433,26],{"class":92},[54,47435,89],{"class":88},[54,47437,93],{"class":92},[54,47439,96],{"class":88},[54,47441,1467],{"class":81},[54,47443,285],{"class":99},[54,47445,103],{"class":88},[54,47447,1235],{"class":99},[54,47449,382],{"class":88},[602,47451,1479],{"id":1478},[14,47453,47454,1484],{},[17,47455,792],{},[602,47457,1488],{"id":1487},[14,47459,1491,47460,1494,47462,89],{},[17,47461,166],{},[17,47463,19],{},[602,47465,1500],{"id":1499},[14,47467,1503,47468,1506],{},[17,47469,1412],{},[40,47471,1510],{"id":1509},[149,47473,47474,47478,47482,47486,47490,47494],{},[152,47475,47476],{},[192,47477,195],{"href":194},[152,47479,47480],{},[192,47481,1521],{"href":970},[152,47483,47484],{},[192,47485,1526],{"href":407},[152,47487,47488],{},[192,47489,715],{"href":714},[152,47491,47492],{},[192,47493,1536],{"href":1535},[152,47495,47496],{},[192,47497,1541,47498],{"href":1083},[17,47499,1087],{},[1545,47501,1547],{},{"title":50,"searchDepth":68,"depth":68,"links":47503},[47504,47505,47506,47507,47508,47509,47515,47521,47522,47529],{"id":42,"depth":68,"text":43},{"id":141,"depth":68,"text":142},{"id":198,"depth":68,"text":199},{"id":330,"depth":68,"text":331},{"id":492,"depth":68,"text":493},{"id":599,"depth":68,"text":600,"children":47510},[47511,47512,47513,47514],{"id":604,"depth":75,"text":605},{"id":657,"depth":75,"text":658},{"id":718,"depth":75,"text":719},{"id":785,"depth":75,"text":786},{"id":977,"depth":68,"text":978,"children":47516},[47517,47518,47519,47520],{"id":981,"depth":75,"text":982},{"id":1090,"depth":75,"text":1091},{"id":1135,"depth":75,"text":1136},{"id":1250,"depth":75,"text":1251},{"id":1359,"depth":68,"text":1360},{"id":1418,"depth":68,"text":1419,"children":47523},[47524,47525,47526,47527,47528],{"id":1422,"depth":75,"text":1423},{"id":1429,"depth":75,"text":1430},{"id":1478,"depth":75,"text":1479},{"id":1487,"depth":75,"text":1488},{"id":1499,"depth":75,"text":1500},{"id":1509,"depth":68,"text":1510},{},{"title":5,"description":1576},{"id":47533,"title":47534,"body":47535,"description":49456,"extension":1577,"meta":49457,"navigation":71,"path":39985,"seo":49458,"stem":49459,"__hash__":49460},"content\u002Fstandard-library\u002Frandom.shuffle-function-explained.md","random.shuffle() Function Explained",{"type":7,"value":47536,"toc":49429},[47537,47542,47547,47550,47559,47562,47564,47631,47633,47662,47669,47674,47679,47681,47699,47707,47709,47712,47741,47744,47756,47760,47767,47838,47840,47869,47872,47968,47970,48023,48026,48033,48035,48038,48123,48125,48166,48169,48175,48179,48182,48188,48311,48313,48376,48379,48491,48494,48502,48511,48515,48521,48524,48611,48613,48646,48648,48669,48672,48742,48746,48751,48754,48807,48810,48813,48816,48827,48830,48917,48920,49015,49022,49025,49029,49037,49041,49049,49051,49170,49172,49215,49222,49224,49228,49249,49252,49329,49332,49350,49353,49355,49361,49366,49372,49375,49384,49391,49395,49398,49400,49426],[10,47538,47540,11309],{"id":47539},"randomshuffle-function-explained",[17,47541,45394],{},[14,47543,47544,47546],{},[17,47545,45394],{}," puts the items in a list into a random order.",[14,47548,47549],{},"It is important to know two things about this function:",[149,47551,47552,47555],{},[152,47553,47554],{},"It changes the original list",[152,47556,25027,47557],{},[17,47558,8775],{},[14,47560,47561],{},"This is a common source of beginner mistakes. If you expect a new list to be returned, your code will not work the way you expect.",[40,47563,3596],{"id":3595},[45,47565,47567],{"className":47,"code":47566,"language":49,"meta":50,"style":50},"import random\n\nnumbers = [1, 2, 3, 4, 5]\nrandom.shuffle(numbers)\nprint(numbers)\n",[17,47568,47569,47575,47579,47607,47621],{"__ignoreMap":50},[54,47570,47571,47573],{"class":56,"line":57},[54,47572,61],{"class":60},[54,47574,65],{"class":64},[54,47576,47577],{"class":56,"line":68},[54,47578,72],{"emptyLinePlaceholder":71},[54,47580,47581,47583,47585,47587,47589,47591,47593,47595,47597,47599,47601,47603,47605],{"class":56,"line":75},[54,47582,11576],{"class":64},[54,47584,82],{"class":81},[54,47586,818],{"class":88},[54,47588,100],{"class":99},[54,47590,103],{"class":88},[54,47592,466],{"class":99},[54,47594,103],{"class":88},[54,47596,379],{"class":99},[54,47598,103],{"class":88},[54,47600,4264],{"class":99},[54,47602,103],{"class":88},[54,47604,1235],{"class":99},[54,47606,469],{"class":88},[54,47608,47609,47611,47613,47615,47617,47619],{"class":56,"line":112},[54,47610,26],{"class":64},[54,47612,89],{"class":88},[54,47614,39926],{"class":92},[54,47616,96],{"class":88},[54,47618,9956],{"class":92},[54,47620,109],{"class":88},[54,47622,47623,47625,47627,47629],{"class":56,"line":887},[54,47624,116],{"class":115},[54,47626,96],{"class":88},[54,47628,9956],{"class":92},[54,47630,109],{"class":88},[14,47632,561],{},[45,47634,47636],{"className":47,"code":47635,"language":49,"meta":50,"style":50},"[3, 1, 5, 2, 4]\n",[17,47637,47638],{"__ignoreMap":50},[54,47639,47640,47642,47644,47646,47648,47650,47652,47654,47656,47658,47660],{"class":56,"line":57},[54,47641,459],{"class":88},[54,47643,401],{"class":99},[54,47645,103],{"class":88},[54,47647,882],{"class":99},[54,47649,103],{"class":88},[54,47651,1235],{"class":99},[54,47653,103],{"class":88},[54,47655,466],{"class":99},[54,47657,103],{"class":88},[54,47659,4264],{"class":99},[54,47661,469],{"class":88},[14,47663,47664,47666,47667,89],{},[17,47665,45394],{}," changes the original list and returns ",[17,47668,8775],{},[40,47670,1736,47672,1739],{"id":47671},"what-randomshuffle-does",[17,47673,45394],{},[14,47675,47676,47678],{},[17,47677,45394],{}," is used when you want to reorder all items in a list randomly.",[14,47680,11458],{},[149,47682,47683,47686,47692,47695],{},[152,47684,47685],{},"It shuffles the items in a list into a random order",[152,47687,47688,47689,47691],{},"It works ",[35,47690,39874],{},", which means it changes the original list",[152,47693,47694],{},"It does not create a new shuffled list",[152,47696,25027,47697],{},[17,47698,8775],{},[14,47700,6345,47701,6348,47703,89],{},[17,47702,26],{},[192,47704,47705,13763],{"href":194},[17,47706,26],{},[40,47708,199],{"id":198},[14,47710,47711],{},"Use this form:",[45,47713,47715],{"className":47,"code":47714,"language":49,"meta":50,"style":50},"import random\n\nrandom.shuffle(my_list)\n",[17,47716,47717,47723,47727],{"__ignoreMap":50},[54,47718,47719,47721],{"class":56,"line":57},[54,47720,61],{"class":60},[54,47722,65],{"class":64},[54,47724,47725],{"class":56,"line":68},[54,47726,72],{"emptyLinePlaceholder":71},[54,47728,47729,47731,47733,47735,47737,47739],{"class":56,"line":75},[54,47730,26],{"class":64},[54,47732,89],{"class":88},[54,47734,39926],{"class":92},[54,47736,96],{"class":88},[54,47738,40522],{"class":92},[54,47740,109],{"class":88},[14,47742,47743],{},"Things to remember:",[149,47745,47746,47750,47753],{},[152,47747,19618,47748,23919],{},[17,47749,26],{},[152,47751,47752],{},"You should pass a mutable sequence such as a list",[152,47754,47755],{},"The list is changed directly",[40,47757,47759],{"id":47758},"what-gets-changed","What gets changed",[14,47761,47762,47763,47766],{},"When you call ",[17,47764,47765],{},"random.shuffle(my_list)",", the original list is modified.",[45,47768,47770],{"className":47,"code":47769,"language":49,"meta":50,"style":50},"import random\n\nnumbers = [1, 2, 3, 4, 5]\nrandom.shuffle(numbers)\n\nprint(numbers)\n",[17,47771,47772,47778,47782,47810,47824,47828],{"__ignoreMap":50},[54,47773,47774,47776],{"class":56,"line":57},[54,47775,61],{"class":60},[54,47777,65],{"class":64},[54,47779,47780],{"class":56,"line":68},[54,47781,72],{"emptyLinePlaceholder":71},[54,47783,47784,47786,47788,47790,47792,47794,47796,47798,47800,47802,47804,47806,47808],{"class":56,"line":75},[54,47785,11576],{"class":64},[54,47787,82],{"class":81},[54,47789,818],{"class":88},[54,47791,100],{"class":99},[54,47793,103],{"class":88},[54,47795,466],{"class":99},[54,47797,103],{"class":88},[54,47799,379],{"class":99},[54,47801,103],{"class":88},[54,47803,4264],{"class":99},[54,47805,103],{"class":88},[54,47807,1235],{"class":99},[54,47809,469],{"class":88},[54,47811,47812,47814,47816,47818,47820,47822],{"class":56,"line":112},[54,47813,26],{"class":64},[54,47815,89],{"class":88},[54,47817,39926],{"class":92},[54,47819,96],{"class":88},[54,47821,9956],{"class":92},[54,47823,109],{"class":88},[54,47825,47826],{"class":56,"line":887},[54,47827,72],{"emptyLinePlaceholder":71},[54,47829,47830,47832,47834,47836],{"class":56,"line":892},[54,47831,116],{"class":115},[54,47833,96],{"class":88},[54,47835,9956],{"class":92},[54,47837,109],{"class":88},[14,47839,561],{},[45,47841,47843],{"className":47,"code":47842,"language":49,"meta":50,"style":50},"[4, 1, 5, 3, 2]\n",[17,47844,47845],{"__ignoreMap":50},[54,47846,47847,47849,47851,47853,47855,47857,47859,47861,47863,47865,47867],{"class":56,"line":57},[54,47848,459],{"class":88},[54,47850,2389],{"class":99},[54,47852,103],{"class":88},[54,47854,882],{"class":99},[54,47856,103],{"class":88},[54,47858,1235],{"class":99},[54,47860,103],{"class":88},[54,47862,379],{"class":99},[54,47864,103],{"class":88},[54,47866,466],{"class":99},[54,47868,469],{"class":88},[14,47870,47871],{},"If another variable points to the same list, it will also see the changed order.",[45,47873,47875],{"className":47,"code":47874,"language":49,"meta":50,"style":50},"import random\n\nnumbers = [1, 2, 3, 4, 5]\nother_name = numbers\n\nrandom.shuffle(numbers)\n\nprint(numbers)\nprint(other_name)\n",[17,47876,47877,47883,47887,47915,47925,47929,47943,47947,47957],{"__ignoreMap":50},[54,47878,47879,47881],{"class":56,"line":57},[54,47880,61],{"class":60},[54,47882,65],{"class":64},[54,47884,47885],{"class":56,"line":68},[54,47886,72],{"emptyLinePlaceholder":71},[54,47888,47889,47891,47893,47895,47897,47899,47901,47903,47905,47907,47909,47911,47913],{"class":56,"line":75},[54,47890,11576],{"class":64},[54,47892,82],{"class":81},[54,47894,818],{"class":88},[54,47896,100],{"class":99},[54,47898,103],{"class":88},[54,47900,466],{"class":99},[54,47902,103],{"class":88},[54,47904,379],{"class":99},[54,47906,103],{"class":88},[54,47908,4264],{"class":99},[54,47910,103],{"class":88},[54,47912,1235],{"class":99},[54,47914,469],{"class":88},[54,47916,47917,47920,47922],{"class":56,"line":112},[54,47918,47919],{"class":64},"other_name ",[54,47921,82],{"class":81},[54,47923,47924],{"class":64}," numbers\n",[54,47926,47927],{"class":56,"line":887},[54,47928,72],{"emptyLinePlaceholder":71},[54,47930,47931,47933,47935,47937,47939,47941],{"class":56,"line":892},[54,47932,26],{"class":64},[54,47934,89],{"class":88},[54,47936,39926],{"class":92},[54,47938,96],{"class":88},[54,47940,9956],{"class":92},[54,47942,109],{"class":88},[54,47944,47945],{"class":56,"line":913},[54,47946,72],{"emptyLinePlaceholder":71},[54,47948,47949,47951,47953,47955],{"class":56,"line":2121},[54,47950,116],{"class":115},[54,47952,96],{"class":88},[54,47954,9956],{"class":92},[54,47956,109],{"class":88},[54,47958,47959,47961,47963,47966],{"class":56,"line":2136},[54,47960,116],{"class":115},[54,47962,96],{"class":88},[54,47964,47965],{"class":92},"other_name",[54,47967,109],{"class":88},[14,47969,305],{},[45,47971,47973],{"className":47,"code":47972,"language":49,"meta":50,"style":50},"[2, 5, 1, 4, 3]\n[2, 5, 1, 4, 3]\n",[17,47974,47975,47999],{"__ignoreMap":50},[54,47976,47977,47979,47981,47983,47985,47987,47989,47991,47993,47995,47997],{"class":56,"line":57},[54,47978,459],{"class":88},[54,47980,396],{"class":99},[54,47982,103],{"class":88},[54,47984,1235],{"class":99},[54,47986,103],{"class":88},[54,47988,882],{"class":99},[54,47990,103],{"class":88},[54,47992,4264],{"class":99},[54,47994,103],{"class":88},[54,47996,379],{"class":99},[54,47998,469],{"class":88},[54,48000,48001,48003,48005,48007,48009,48011,48013,48015,48017,48019,48021],{"class":56,"line":68},[54,48002,459],{"class":88},[54,48004,396],{"class":99},[54,48006,103],{"class":88},[54,48008,1235],{"class":99},[54,48010,103],{"class":88},[54,48012,882],{"class":99},[54,48014,103],{"class":88},[54,48016,4264],{"class":99},[54,48018,103],{"class":88},[54,48020,379],{"class":99},[54,48022,469],{"class":88},[14,48024,48025],{},"This happens because both variables refer to the same list object.",[14,48027,48028,48029,89],{},"If this idea is unfamiliar, it helps to understand ",[192,48030,48032],{"href":48031},"\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types","mutability in Python",[40,48034,493],{"id":492},[14,48036,48037],{},"Here is a basic example with a short list:",[45,48039,48041],{"className":47,"code":48040,"language":49,"meta":50,"style":50},"import random\n\nletters = [\"a\", \"b\", \"c\", \"d\"]\nrandom.shuffle(letters)\n\nprint(letters)\n",[17,48042,48043,48049,48053,48094,48109,48113],{"__ignoreMap":50},[54,48044,48045,48047],{"class":56,"line":57},[54,48046,61],{"class":60},[54,48048,65],{"class":64},[54,48050,48051],{"class":56,"line":68},[54,48052,72],{"emptyLinePlaceholder":71},[54,48054,48055,48058,48060,48062,48064,48066,48068,48070,48072,48074,48076,48078,48080,48082,48084,48086,48088,48090,48092],{"class":56,"line":75},[54,48056,48057],{"class":64},"letters ",[54,48059,82],{"class":81},[54,48061,818],{"class":88},[54,48063,545],{"class":544},[54,48065,192],{"class":548},[54,48067,545],{"class":544},[54,48069,103],{"class":88},[54,48071,830],{"class":544},[54,48073,241],{"class":548},[54,48075,545],{"class":544},[54,48077,103],{"class":88},[54,48079,830],{"class":544},[54,48081,9099],{"class":548},[54,48083,545],{"class":544},[54,48085,103],{"class":88},[54,48087,830],{"class":544},[54,48089,31427],{"class":548},[54,48091,545],{"class":544},[54,48093,469],{"class":88},[54,48095,48096,48098,48100,48102,48104,48107],{"class":56,"line":112},[54,48097,26],{"class":64},[54,48099,89],{"class":88},[54,48101,39926],{"class":92},[54,48103,96],{"class":88},[54,48105,48106],{"class":92},"letters",[54,48108,109],{"class":88},[54,48110,48111],{"class":56,"line":887},[54,48112,72],{"emptyLinePlaceholder":71},[54,48114,48115,48117,48119,48121],{"class":56,"line":892},[54,48116,116],{"class":115},[54,48118,96],{"class":88},[54,48120,48106],{"class":92},[54,48122,109],{"class":88},[14,48124,305],{},[45,48126,48128],{"className":47,"code":48127,"language":49,"meta":50,"style":50},"['c', 'a', 'd', 'b']\n",[17,48129,48130],{"__ignoreMap":50},[54,48131,48132,48134,48136,48138,48140,48142,48144,48146,48148,48150,48152,48154,48156,48158,48160,48162,48164],{"class":56,"line":57},[54,48133,459],{"class":88},[54,48135,2240],{"class":544},[54,48137,9099],{"class":548},[54,48139,2240],{"class":544},[54,48141,103],{"class":88},[54,48143,2234],{"class":544},[54,48145,192],{"class":548},[54,48147,2240],{"class":544},[54,48149,103],{"class":88},[54,48151,2234],{"class":544},[54,48153,31427],{"class":548},[54,48155,2240],{"class":544},[54,48157,103],{"class":88},[54,48159,2234],{"class":544},[54,48161,241],{"class":548},[54,48163,2240],{"class":544},[54,48165,469],{"class":88},[14,48167,48168],{},"If you run the program again, the order will usually be different.",[14,48170,48171,48172,48174],{},"That is normal. The purpose of ",[17,48173,39979],{}," is to produce a random ordering.",[40,48176,48178],{"id":48177},"how-to-keep-the-original-list-unchanged","How to keep the original list unchanged",[14,48180,48181],{},"If you need to keep the original order, make a copy first and shuffle the copy.",[14,48183,48184,48185,147],{},"You can copy a list with ",[17,48186,48187],{},".copy()",[45,48189,48191],{"className":47,"code":48190,"language":49,"meta":50,"style":50},"import random\n\noriginal = [1, 2, 3, 4, 5]\nshuffled = original.copy()\n\nrandom.shuffle(shuffled)\n\nprint(\"Original:\", original)\nprint(\"Shuffled:\", shuffled)\n",[17,48192,48193,48199,48203,48232,48249,48253,48268,48272,48291],{"__ignoreMap":50},[54,48194,48195,48197],{"class":56,"line":57},[54,48196,61],{"class":60},[54,48198,65],{"class":64},[54,48200,48201],{"class":56,"line":68},[54,48202,72],{"emptyLinePlaceholder":71},[54,48204,48205,48208,48210,48212,48214,48216,48218,48220,48222,48224,48226,48228,48230],{"class":56,"line":75},[54,48206,48207],{"class":64},"original ",[54,48209,82],{"class":81},[54,48211,818],{"class":88},[54,48213,100],{"class":99},[54,48215,103],{"class":88},[54,48217,466],{"class":99},[54,48219,103],{"class":88},[54,48221,379],{"class":99},[54,48223,103],{"class":88},[54,48225,4264],{"class":99},[54,48227,103],{"class":88},[54,48229,1235],{"class":99},[54,48231,469],{"class":88},[54,48233,48234,48237,48239,48242,48244,48247],{"class":56,"line":112},[54,48235,48236],{"class":64},"shuffled ",[54,48238,82],{"class":81},[54,48240,48241],{"class":64}," original",[54,48243,89],{"class":88},[54,48245,48246],{"class":92},"copy",[54,48248,1670],{"class":88},[54,48250,48251],{"class":56,"line":887},[54,48252,72],{"emptyLinePlaceholder":71},[54,48254,48255,48257,48259,48261,48263,48266],{"class":56,"line":892},[54,48256,26],{"class":64},[54,48258,89],{"class":88},[54,48260,39926],{"class":92},[54,48262,96],{"class":88},[54,48264,48265],{"class":92},"shuffled",[54,48267,109],{"class":88},[54,48269,48270],{"class":56,"line":913},[54,48271,72],{"emptyLinePlaceholder":71},[54,48273,48274,48276,48278,48280,48283,48285,48287,48289],{"class":56,"line":2121},[54,48275,116],{"class":115},[54,48277,96],{"class":88},[54,48279,545],{"class":544},[54,48281,48282],{"class":548},"Original:",[54,48284,545],{"class":544},[54,48286,103],{"class":88},[54,48288,48241],{"class":92},[54,48290,109],{"class":88},[54,48292,48293,48295,48297,48299,48302,48304,48306,48309],{"class":56,"line":2136},[54,48294,116],{"class":115},[54,48296,96],{"class":88},[54,48298,545],{"class":544},[54,48300,48301],{"class":548},"Shuffled:",[54,48303,545],{"class":544},[54,48305,103],{"class":88},[54,48307,48308],{"class":92}," shuffled",[54,48310,109],{"class":88},[14,48312,305],{},[45,48314,48316],{"className":47,"code":48315,"language":49,"meta":50,"style":50},"Original: [1, 2, 3, 4, 5]\nShuffled: [4, 1, 5, 2, 3]\n",[17,48317,48318,48347],{"__ignoreMap":50},[54,48319,48320,48323,48325,48327,48329,48331,48333,48335,48337,48339,48341,48343,48345],{"class":56,"line":57},[54,48321,48322],{"class":64},"Original",[54,48324,147],{"class":88},[54,48326,818],{"class":88},[54,48328,100],{"class":99},[54,48330,103],{"class":88},[54,48332,466],{"class":99},[54,48334,103],{"class":88},[54,48336,379],{"class":99},[54,48338,103],{"class":88},[54,48340,4264],{"class":99},[54,48342,103],{"class":88},[54,48344,1235],{"class":99},[54,48346,469],{"class":88},[54,48348,48349,48352,48354,48356,48358,48360,48362,48364,48366,48368,48370,48372,48374],{"class":56,"line":68},[54,48350,48351],{"class":64},"Shuffled",[54,48353,147],{"class":88},[54,48355,818],{"class":88},[54,48357,2389],{"class":99},[54,48359,103],{"class":88},[54,48361,882],{"class":99},[54,48363,103],{"class":88},[54,48365,1235],{"class":99},[54,48367,103],{"class":88},[54,48369,466],{"class":99},[54,48371,103],{"class":88},[54,48373,379],{"class":99},[54,48375,469],{"class":88},[14,48377,48378],{},"You can also copy with slicing:",[45,48380,48382],{"className":47,"code":48381,"language":49,"meta":50,"style":50},"import random\n\noriginal = [1, 2, 3, 4, 5]\nshuffled = original[:]\n\nrandom.shuffle(shuffled)\n\nprint(\"Original:\", original)\nprint(\"Shuffled:\", shuffled)\n",[17,48383,48384,48390,48394,48422,48433,48437,48451,48455,48473],{"__ignoreMap":50},[54,48385,48386,48388],{"class":56,"line":57},[54,48387,61],{"class":60},[54,48389,65],{"class":64},[54,48391,48392],{"class":56,"line":68},[54,48393,72],{"emptyLinePlaceholder":71},[54,48395,48396,48398,48400,48402,48404,48406,48408,48410,48412,48414,48416,48418,48420],{"class":56,"line":75},[54,48397,48207],{"class":64},[54,48399,82],{"class":81},[54,48401,818],{"class":88},[54,48403,100],{"class":99},[54,48405,103],{"class":88},[54,48407,466],{"class":99},[54,48409,103],{"class":88},[54,48411,379],{"class":99},[54,48413,103],{"class":88},[54,48415,4264],{"class":99},[54,48417,103],{"class":88},[54,48419,1235],{"class":99},[54,48421,469],{"class":88},[54,48423,48424,48426,48428,48430],{"class":56,"line":112},[54,48425,48236],{"class":64},[54,48427,82],{"class":81},[54,48429,48241],{"class":64},[54,48431,48432],{"class":88},"[:]\n",[54,48434,48435],{"class":56,"line":887},[54,48436,72],{"emptyLinePlaceholder":71},[54,48438,48439,48441,48443,48445,48447,48449],{"class":56,"line":892},[54,48440,26],{"class":64},[54,48442,89],{"class":88},[54,48444,39926],{"class":92},[54,48446,96],{"class":88},[54,48448,48265],{"class":92},[54,48450,109],{"class":88},[54,48452,48453],{"class":56,"line":913},[54,48454,72],{"emptyLinePlaceholder":71},[54,48456,48457,48459,48461,48463,48465,48467,48469,48471],{"class":56,"line":2121},[54,48458,116],{"class":115},[54,48460,96],{"class":88},[54,48462,545],{"class":544},[54,48464,48282],{"class":548},[54,48466,545],{"class":544},[54,48468,103],{"class":88},[54,48470,48241],{"class":92},[54,48472,109],{"class":88},[54,48474,48475,48477,48479,48481,48483,48485,48487,48489],{"class":56,"line":2136},[54,48476,116],{"class":115},[54,48478,96],{"class":88},[54,48480,545],{"class":544},[54,48482,48301],{"class":548},[54,48484,545],{"class":544},[54,48486,103],{"class":88},[54,48488,48308],{"class":92},[54,48490,109],{"class":88},[14,48492,48493],{},"This is useful when you need both:",[149,48495,48496,48499],{},[152,48497,48498],{},"the original list",[152,48500,48501],{},"a random version of the same items",[14,48503,48504,48505,89],{},"If you want to learn more about copying lists, see ",[192,48506,48508],{"href":48507},"\u002Freference\u002Fpython-list-copy-method",[17,48509,48510],{},"list.copy()",[40,48512,48514],{"id":48513},"common-beginner-confusion","Common beginner confusion",[602,48516,48518,48520],{"id":48517},"randomshufflemy_list-does-not-return-the-shuffled-list",[17,48519,47765],{}," does not return the shuffled list",[14,48522,48523],{},"This is one of the most common mistakes:",[45,48525,48527],{"className":47,"code":48526,"language":49,"meta":50,"style":50},"import random\n\nnumbers = [1, 2, 3, 4, 5]\nnew_list = random.shuffle(numbers)\n\nprint(new_list)\nprint(numbers)\n",[17,48528,48529,48535,48539,48567,48586,48590,48601],{"__ignoreMap":50},[54,48530,48531,48533],{"class":56,"line":57},[54,48532,61],{"class":60},[54,48534,65],{"class":64},[54,48536,48537],{"class":56,"line":68},[54,48538,72],{"emptyLinePlaceholder":71},[54,48540,48541,48543,48545,48547,48549,48551,48553,48555,48557,48559,48561,48563,48565],{"class":56,"line":75},[54,48542,11576],{"class":64},[54,48544,82],{"class":81},[54,48546,818],{"class":88},[54,48548,100],{"class":99},[54,48550,103],{"class":88},[54,48552,466],{"class":99},[54,48554,103],{"class":88},[54,48556,379],{"class":99},[54,48558,103],{"class":88},[54,48560,4264],{"class":99},[54,48562,103],{"class":88},[54,48564,1235],{"class":99},[54,48566,469],{"class":88},[54,48568,48569,48572,48574,48576,48578,48580,48582,48584],{"class":56,"line":112},[54,48570,48571],{"class":64},"new_list ",[54,48573,82],{"class":81},[54,48575,85],{"class":64},[54,48577,89],{"class":88},[54,48579,39926],{"class":92},[54,48581,96],{"class":88},[54,48583,9956],{"class":92},[54,48585,109],{"class":88},[54,48587,48588],{"class":56,"line":887},[54,48589,72],{"emptyLinePlaceholder":71},[54,48591,48592,48594,48596,48599],{"class":56,"line":892},[54,48593,116],{"class":115},[54,48595,96],{"class":88},[54,48597,48598],{"class":92},"new_list",[54,48600,109],{"class":88},[54,48602,48603,48605,48607,48609],{"class":56,"line":913},[54,48604,116],{"class":115},[54,48606,96],{"class":88},[54,48608,9956],{"class":92},[54,48610,109],{"class":88},[14,48612,449],{},[45,48614,48616],{"className":47,"code":48615,"language":49,"meta":50,"style":50},"None\n[2, 4, 1, 5, 3]\n",[17,48617,48618,48622],{"__ignoreMap":50},[54,48619,48620],{"class":56,"line":57},[54,48621,16695],{"class":8608},[54,48623,48624,48626,48628,48630,48632,48634,48636,48638,48640,48642,48644],{"class":56,"line":68},[54,48625,459],{"class":88},[54,48627,396],{"class":99},[54,48629,103],{"class":88},[54,48631,4264],{"class":99},[54,48633,103],{"class":88},[54,48635,882],{"class":99},[54,48637,103],{"class":88},[54,48639,1235],{"class":99},[54,48641,103],{"class":88},[54,48643,379],{"class":99},[54,48645,469],{"class":88},[14,48647,22884],{},[149,48649,48650,48657,48662],{},[152,48651,48652,48654,48655,13290],{},[17,48653,39979],{}," changes ",[17,48656,9956],{},[152,48658,48659,48660],{},"the return value is ",[17,48661,8775],{},[152,48663,48664,48665,18583,48667],{},"so ",[17,48666,48598],{},[17,48668,8775],{},[14,48670,48671],{},"The correct way is:",[45,48673,48674],{"className":47,"code":47769,"language":49,"meta":50,"style":50},[17,48675,48676,48682,48686,48714,48728,48732],{"__ignoreMap":50},[54,48677,48678,48680],{"class":56,"line":57},[54,48679,61],{"class":60},[54,48681,65],{"class":64},[54,48683,48684],{"class":56,"line":68},[54,48685,72],{"emptyLinePlaceholder":71},[54,48687,48688,48690,48692,48694,48696,48698,48700,48702,48704,48706,48708,48710,48712],{"class":56,"line":75},[54,48689,11576],{"class":64},[54,48691,82],{"class":81},[54,48693,818],{"class":88},[54,48695,100],{"class":99},[54,48697,103],{"class":88},[54,48699,466],{"class":99},[54,48701,103],{"class":88},[54,48703,379],{"class":99},[54,48705,103],{"class":88},[54,48707,4264],{"class":99},[54,48709,103],{"class":88},[54,48711,1235],{"class":99},[54,48713,469],{"class":88},[54,48715,48716,48718,48720,48722,48724,48726],{"class":56,"line":112},[54,48717,26],{"class":64},[54,48719,89],{"class":88},[54,48721,39926],{"class":92},[54,48723,96],{"class":88},[54,48725,9956],{"class":92},[54,48727,109],{"class":88},[54,48729,48730],{"class":56,"line":887},[54,48731,72],{"emptyLinePlaceholder":71},[54,48733,48734,48736,48738,48740],{"class":56,"line":892},[54,48735,116],{"class":115},[54,48737,96],{"class":88},[54,48739,9956],{"class":92},[54,48741,109],{"class":88},[602,48743,48745],{"id":48744},"you-cannot-shuffle-a-tuple-or-string-in-place","You cannot shuffle a tuple or string in place",[14,48747,48748,48750],{},[17,48749,45394],{}," is meant for mutable data, usually lists.",[14,48752,48753],{},"This will not work as expected with a tuple:",[45,48755,48757],{"className":47,"code":48756,"language":49,"meta":50,"style":50},"import random\n\nvalues = (1, 2, 3, 4)\nrandom.shuffle(values)\n",[17,48758,48759,48765,48769,48793],{"__ignoreMap":50},[54,48760,48761,48763],{"class":56,"line":57},[54,48762,61],{"class":60},[54,48764,65],{"class":64},[54,48766,48767],{"class":56,"line":68},[54,48768,72],{"emptyLinePlaceholder":71},[54,48770,48771,48773,48775,48777,48779,48781,48783,48785,48787,48789,48791],{"class":56,"line":75},[54,48772,44545],{"class":64},[54,48774,82],{"class":81},[54,48776,41305],{"class":88},[54,48778,100],{"class":99},[54,48780,103],{"class":88},[54,48782,466],{"class":99},[54,48784,103],{"class":88},[54,48786,379],{"class":99},[54,48788,103],{"class":88},[54,48790,4264],{"class":99},[54,48792,109],{"class":88},[54,48794,48795,48797,48799,48801,48803,48805],{"class":56,"line":112},[54,48796,26],{"class":64},[54,48798,89],{"class":88},[54,48800,39926],{"class":92},[54,48802,96],{"class":88},[54,48804,44582],{"class":92},[54,48806,109],{"class":88},[14,48808,48809],{},"A tuple cannot be changed in place.",[14,48811,48812],{},"A string also cannot be shuffled in place because strings are immutable.",[14,48814,48815],{},"If you need to shuffle a tuple or string:",[27327,48817,48818,48821,48824],{},[152,48819,48820],{},"Convert it to a list",[152,48822,48823],{},"Shuffle the list",[152,48825,48826],{},"Convert it back if needed",[14,48828,48829],{},"Example with a tuple:",[45,48831,48833],{"className":47,"code":48832,"language":49,"meta":50,"style":50},"import random\n\nvalues = (1, 2, 3, 4)\ntemp_list = list(values)\n\nrandom.shuffle(temp_list)\n\nprint(temp_list)\n",[17,48834,48835,48841,48845,48869,48884,48888,48903,48907],{"__ignoreMap":50},[54,48836,48837,48839],{"class":56,"line":57},[54,48838,61],{"class":60},[54,48840,65],{"class":64},[54,48842,48843],{"class":56,"line":68},[54,48844,72],{"emptyLinePlaceholder":71},[54,48846,48847,48849,48851,48853,48855,48857,48859,48861,48863,48865,48867],{"class":56,"line":75},[54,48848,44545],{"class":64},[54,48850,82],{"class":81},[54,48852,41305],{"class":88},[54,48854,100],{"class":99},[54,48856,103],{"class":88},[54,48858,466],{"class":99},[54,48860,103],{"class":88},[54,48862,379],{"class":99},[54,48864,103],{"class":88},[54,48866,4264],{"class":99},[54,48868,109],{"class":88},[54,48870,48871,48874,48876,48878,48880,48882],{"class":56,"line":112},[54,48872,48873],{"class":64},"temp_list ",[54,48875,82],{"class":81},[54,48877,10105],{"class":429},[54,48879,96],{"class":88},[54,48881,44582],{"class":92},[54,48883,109],{"class":88},[54,48885,48886],{"class":56,"line":887},[54,48887,72],{"emptyLinePlaceholder":71},[54,48889,48890,48892,48894,48896,48898,48901],{"class":56,"line":892},[54,48891,26],{"class":64},[54,48893,89],{"class":88},[54,48895,39926],{"class":92},[54,48897,96],{"class":88},[54,48899,48900],{"class":92},"temp_list",[54,48902,109],{"class":88},[54,48904,48905],{"class":56,"line":913},[54,48906,72],{"emptyLinePlaceholder":71},[54,48908,48909,48911,48913,48915],{"class":56,"line":2121},[54,48910,116],{"class":115},[54,48912,96],{"class":88},[54,48914,48900],{"class":92},[54,48916,109],{"class":88},[14,48918,48919],{},"Example with a string:",[45,48921,48923],{"className":47,"code":48922,"language":49,"meta":50,"style":50},"import random\n\ntext = \"hello\"\nchars = list(text)\n\nrandom.shuffle(chars)\n\nshuffled_text = \"\".join(chars)\nprint(shuffled_text)\n",[17,48924,48925,48931,48935,48947,48962,48966,48981,48985,49004],{"__ignoreMap":50},[54,48926,48927,48929],{"class":56,"line":57},[54,48928,61],{"class":60},[54,48930,65],{"class":64},[54,48932,48933],{"class":56,"line":68},[54,48934,72],{"emptyLinePlaceholder":71},[54,48936,48937,48939,48941,48943,48945],{"class":56,"line":75},[54,48938,5134],{"class":64},[54,48940,82],{"class":81},[54,48942,830],{"class":544},[54,48944,41557],{"class":548},[54,48946,5239],{"class":544},[54,48948,48949,48952,48954,48956,48958,48960],{"class":56,"line":112},[54,48950,48951],{"class":64},"chars ",[54,48953,82],{"class":81},[54,48955,10105],{"class":429},[54,48957,96],{"class":88},[54,48959,5171],{"class":92},[54,48961,109],{"class":88},[54,48963,48964],{"class":56,"line":887},[54,48965,72],{"emptyLinePlaceholder":71},[54,48967,48968,48970,48972,48974,48976,48979],{"class":56,"line":892},[54,48969,26],{"class":64},[54,48971,89],{"class":88},[54,48973,39926],{"class":92},[54,48975,96],{"class":88},[54,48977,48978],{"class":92},"chars",[54,48980,109],{"class":88},[54,48982,48983],{"class":56,"line":913},[54,48984,72],{"emptyLinePlaceholder":71},[54,48986,48987,48990,48992,48994,48996,48998,49000,49002],{"class":56,"line":2121},[54,48988,48989],{"class":64},"shuffled_text ",[54,48991,82],{"class":81},[54,48993,30180],{"class":544},[54,48995,89],{"class":88},[54,48997,25725],{"class":92},[54,48999,96],{"class":88},[54,49001,48978],{"class":92},[54,49003,109],{"class":88},[54,49005,49006,49008,49010,49013],{"class":56,"line":2136},[54,49007,116],{"class":115},[54,49009,96],{"class":88},[54,49011,49012],{"class":92},"shuffled_text",[54,49014,109],{"class":88},[40,49016,15426,49018,7817,49020],{"id":49017},"when-to-use-randomshuffle-vs-randomchoice",[17,49019,45394],{},[17,49021,973],{},[14,49023,49024],{},"These two functions solve different problems.",[14,49026,126,49027,5963],{},[17,49028,45394],{},[149,49030,49031,49034],{},[152,49032,49033],{},"reorder all items in a list",[152,49035,49036],{},"get a random arrangement of the whole list",[14,49038,126,49039,5963],{},[17,49040,973],{},[149,49042,49043,49046],{},[152,49044,49045],{},"pick one random item",[152,49047,49048],{},"leave the original list order unchanged",[14,49050,253],{},[45,49052,49054],{"className":47,"code":49053,"language":49,"meta":50,"style":50},"import random\n\nnumbers = [10, 20, 30, 40]\n\nitem = random.choice(numbers)\nprint(\"Random item:\", item)\n\nrandom.shuffle(numbers)\nprint(\"Shuffled list:\", numbers)\n",[17,49055,49056,49062,49066,49091,49095,49113,49133,49137,49151],{"__ignoreMap":50},[54,49057,49058,49060],{"class":56,"line":57},[54,49059,61],{"class":60},[54,49061,65],{"class":64},[54,49063,49064],{"class":56,"line":68},[54,49065,72],{"emptyLinePlaceholder":71},[54,49067,49068,49070,49072,49074,49076,49078,49080,49082,49084,49086,49089],{"class":56,"line":75},[54,49069,11576],{"class":64},[54,49071,82],{"class":81},[54,49073,818],{"class":88},[54,49075,1131],{"class":99},[54,49077,103],{"class":88},[54,49079,695],{"class":99},[54,49081,103],{"class":88},[54,49083,3981],{"class":99},[54,49085,103],{"class":88},[54,49087,49088],{"class":99}," 40",[54,49090,469],{"class":88},[54,49092,49093],{"class":56,"line":112},[54,49094,72],{"emptyLinePlaceholder":71},[54,49096,49097,49099,49101,49103,49105,49107,49109,49111],{"class":56,"line":887},[54,49098,44978],{"class":64},[54,49100,82],{"class":81},[54,49102,85],{"class":64},[54,49104,89],{"class":88},[54,49106,39072],{"class":92},[54,49108,96],{"class":88},[54,49110,9956],{"class":92},[54,49112,109],{"class":88},[54,49114,49115,49117,49119,49121,49124,49126,49128,49131],{"class":56,"line":892},[54,49116,116],{"class":115},[54,49118,96],{"class":88},[54,49120,545],{"class":544},[54,49122,49123],{"class":548},"Random item:",[54,49125,545],{"class":544},[54,49127,103],{"class":88},[54,49129,49130],{"class":92}," item",[54,49132,109],{"class":88},[54,49134,49135],{"class":56,"line":913},[54,49136,72],{"emptyLinePlaceholder":71},[54,49138,49139,49141,49143,49145,49147,49149],{"class":56,"line":2121},[54,49140,26],{"class":64},[54,49142,89],{"class":88},[54,49144,39926],{"class":92},[54,49146,96],{"class":88},[54,49148,9956],{"class":92},[54,49150,109],{"class":88},[54,49152,49153,49155,49157,49159,49161,49163,49165,49168],{"class":56,"line":2136},[54,49154,116],{"class":115},[54,49156,96],{"class":88},[54,49158,545],{"class":544},[54,49160,40208],{"class":548},[54,49162,545],{"class":544},[54,49164,103],{"class":88},[54,49166,49167],{"class":92}," numbers",[54,49169,109],{"class":88},[14,49171,305],{},[45,49173,49175],{"className":47,"code":49174,"language":49,"meta":50,"style":50},"Random item: 30\nShuffled list: [20, 40, 10, 30]\n",[17,49176,49177,49187],{"__ignoreMap":50},[54,49178,49179,49182,49184],{"class":56,"line":57},[54,49180,49181],{"class":64},"Random item",[54,49183,147],{"class":88},[54,49185,49186],{"class":99}," 30\n",[54,49188,49189,49192,49194,49196,49198,49201,49203,49205,49207,49209,49211,49213],{"class":56,"line":68},[54,49190,49191],{"class":64},"Shuffled ",[54,49193,430],{"class":429},[54,49195,147],{"class":88},[54,49197,818],{"class":88},[54,49199,49200],{"class":99},"20",[54,49202,103],{"class":88},[54,49204,49088],{"class":99},[54,49206,103],{"class":88},[54,49208,106],{"class":99},[54,49210,103],{"class":88},[54,49212,3981],{"class":99},[54,49214,469],{"class":88},[14,49216,49217,49218,89],{},"If you want to learn that function next, see ",[192,49219,49220,2834],{"href":970},[17,49221,973],{},[40,49223,978],{"id":977},[14,49225,10183,49226,147],{},[17,49227,45394],{},[149,49229,49230,49238,49241,49246],{},[152,49231,49232,49233,49235,49236],{},"Assigning the result of ",[17,49234,45394],{}," to a variable and getting ",[17,49237,8775],{},[152,49239,49240],{},"Trying to shuffle a tuple or string",[152,49242,49243,49244],{},"Forgetting to ",[17,49245,166],{},[152,49247,49248],{},"Expecting the same shuffled order every time without setting a seed",[14,49250,49251],{},"These quick checks can help when debugging:",[45,49253,49255],{"className":47,"code":49254,"language":49,"meta":50,"style":50},"import random\nprint(my_list)\nresult = random.shuffle(my_list)\nprint(result)\nprint(type(my_list))\nprint(len(my_list))\n",[17,49256,49257,49263,49273,49291,49301,49315],{"__ignoreMap":50},[54,49258,49259,49261],{"class":56,"line":57},[54,49260,61],{"class":60},[54,49262,65],{"class":64},[54,49264,49265,49267,49269,49271],{"class":56,"line":68},[54,49266,116],{"class":115},[54,49268,96],{"class":88},[54,49270,40522],{"class":92},[54,49272,109],{"class":88},[54,49274,49275,49277,49279,49281,49283,49285,49287,49289],{"class":56,"line":75},[54,49276,3833],{"class":64},[54,49278,82],{"class":81},[54,49280,85],{"class":64},[54,49282,89],{"class":88},[54,49284,39926],{"class":92},[54,49286,96],{"class":88},[54,49288,40522],{"class":92},[54,49290,109],{"class":88},[54,49292,49293,49295,49297,49299],{"class":56,"line":112},[54,49294,116],{"class":115},[54,49296,96],{"class":88},[54,49298,3866],{"class":92},[54,49300,109],{"class":88},[54,49302,49303,49305,49307,49309,49311,49313],{"class":56,"line":887},[54,49304,116],{"class":115},[54,49306,96],{"class":88},[54,49308,2208],{"class":429},[54,49310,96],{"class":88},[54,49312,40522],{"class":92},[54,49314,382],{"class":88},[54,49316,49317,49319,49321,49323,49325,49327],{"class":56,"line":892},[54,49318,116],{"class":115},[54,49320,96],{"class":88},[54,49322,46095],{"class":115},[54,49324,96],{"class":88},[54,49326,40522],{"class":92},[54,49328,382],{"class":88},[14,49330,49331],{},"What these lines help you check:",[149,49333,49334,49337,49344,49347],{},[152,49335,49336],{},"whether your list contains the values you expect",[152,49338,15559,49339,49341,49342],{},[17,49340,39979],{}," returned ",[17,49343,8775],{},[152,49345,49346],{},"whether your variable is actually a list",[152,49348,49349],{},"whether the list has items in it",[14,49351,49352],{},"If you want a task-focused example, see how to shuffle a list in Python.",[40,49354,1419],{"id":1418},[602,49356,8346,49358,49360],{"id":49357},"does-randomshuffle-return-a-new-list",[17,49359,45394],{}," return a new list?",[14,49362,49363,49364,89],{},"No. It changes the original list and returns ",[17,49365,8775],{},[602,49367,37466,49369,49371],{"id":49368},"can-i-use-randomshuffle-on-a-tuple",[17,49370,45394],{}," on a tuple?",[14,49373,49374],{},"No. Tuples cannot be changed in place. Convert the tuple to a list first.",[602,49376,49378,49379,49381,49382,3409],{"id":49377},"why-is-my-variable-none-after-using-randomshuffle","Why is my variable ",[17,49380,8775],{}," after using ",[17,49383,45394],{},[14,49385,3412,49386,20564,49388,49390],{},[17,49387,39979],{},[17,49389,8775],{},". It modifies the list directly.",[602,49392,49394],{"id":49393},"how-do-i-shuffle-without-changing-the-original-list","How do I shuffle without changing the original list?",[14,49396,49397],{},"Copy the list first, then shuffle the copy.",[40,49399,1510],{"id":1509},[149,49401,49402,49408,49414,49421],{},[152,49403,49404],{},[192,49405,49406,13763],{"href":194},[17,49407,26],{},[152,49409,49410],{},[192,49411,49412,12898],{"href":970},[17,49413,973],{},[152,49415,49416],{},[192,49417,49418,49420],{"href":48507},[17,49419,48510],{}," method",[152,49422,49423],{},[192,49424,49425],{"href":48031},"Mutability in Python: mutable vs immutable types",[1545,49427,49428],{},"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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":49430},[49431,49432,49434,49435,49436,49437,49438,49443,49445,49446,49455],{"id":3595,"depth":68,"text":3596},{"id":47671,"depth":68,"text":49433},"What random.shuffle() does",{"id":198,"depth":68,"text":199},{"id":47758,"depth":68,"text":47759},{"id":492,"depth":68,"text":493},{"id":48177,"depth":68,"text":48178},{"id":48513,"depth":68,"text":48514,"children":49439},[49440,49442],{"id":48517,"depth":75,"text":49441},"random.shuffle(my_list) does not return the shuffled list",{"id":48744,"depth":75,"text":48745},{"id":49017,"depth":68,"text":49444},"When to use random.shuffle() vs random.choice()",{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":49447},[49448,49450,49452,49454],{"id":49357,"depth":75,"text":49449},"Does random.shuffle() return a new list?",{"id":49368,"depth":75,"text":49451},"Can I use random.shuffle() on a tuple?",{"id":49377,"depth":75,"text":49453},"Why is my variable None after using random.shuffle()?",{"id":49393,"depth":75,"text":49394},{"id":1509,"depth":68,"text":1510},"Master random.shuffle function explained in our comprehensive Python beginner guide.",{},{"title":47534,"description":49456},"standard-library\u002Frandom.shuffle-function-explained","dRepyD7NkhcCJNDZVZ76UnuC9o-x0RJkvFmfvo7JNtc",{"id":49462,"title":49463,"body":49464,"description":51075,"extension":1577,"meta":51076,"navigation":71,"path":41147,"seo":51077,"stem":51078,"__hash__":51079},"content\u002Fstandard-library\u002Fsys.argv-explained.md","sys.argv Explained",{"type":7,"value":49465,"toc":51050},[49466,49469,49474,49477,49479,49522,49527,49531,49541,49543,49563,49566,49584,49587,49621,49623,49647,49652,49656,49660,49671,49674,49685,49690,49692,49698,49742,49748,49765,49768,49810,49813,49817,49820,49967,49969,49990,49993,50002,50005,50038,50040,50051,50057,50060,50136,50140,50143,50149,50220,50222,50237,50240,50249,50252,50255,50259,50377,50380,50394,50397,50405,50410,50414,50530,50538,50542,50547,50549,50596,50599,50612,50618,50622,50626,50643,50646,50671,50678,50682,50685,50688,50869,50872,50890,50895,50899,50904,50909,50929,50932,50948,50955,50957,50963,50966,50970,50973,50980,50986,50990,50993,50997,51005,51007,51047],[10,49467,49463],{"id":49468},"sysargv-explained",[14,49470,49471,49473],{},[17,49472,41117],{}," lets a Python script read values passed from the command line.",[14,49475,49476],{},"It is useful for small scripts where you want to give input when you run the file, such as a file name, a word, or a number.",[40,49478,3596],{"id":3595},[45,49480,49482],{"className":47,"code":49481,"language":49,"meta":50,"style":50},"import sys\n\nprint(sys.argv)\n\n# Run from terminal:\n# python script.py hello 123\n",[17,49483,49484,49490,49494,49508,49512,49517],{"__ignoreMap":50},[54,49485,49486,49488],{"class":56,"line":57},[54,49487,61],{"class":60},[54,49489,41003],{"class":64},[54,49491,49492],{"class":56,"line":68},[54,49493,72],{"emptyLinePlaceholder":71},[54,49495,49496,49498,49500,49502,49504,49506],{"class":56,"line":75},[54,49497,116],{"class":115},[54,49499,96],{"class":88},[54,49501,40958],{"class":92},[54,49503,89],{"class":88},[54,49505,41050],{"class":1693},[54,49507,109],{"class":88},[54,49509,49510],{"class":56,"line":112},[54,49511,72],{"emptyLinePlaceholder":71},[54,49513,49514],{"class":56,"line":887},[54,49515,49516],{"class":4313},"# Run from terminal:\n",[54,49518,49519],{"class":56,"line":892},[54,49520,49521],{"class":4313},"# python script.py hello 123\n",[14,49523,49524,49526],{},[17,49525,41117],{}," is a list of command line arguments. The first item is the script name.",[40,49528,49530],{"id":49529},"what-sysargv-is","What sys.argv is",[14,49532,49533,49535,49536,49538,49539,89],{},[17,49534,41117],{}," comes from Python’s ",[17,49537,40958],{}," module. If you want to use it, you must first import ",[17,49540,40958],{},[14,49542,11458],{},[149,49544,49545,49550,49553,49558],{},[152,49546,49547,49549],{},[17,49548,41117],{}," stores command line arguments in a list",[152,49551,49552],{},"Arguments are values typed after the script name",[152,49554,49555,49557],{},[17,49556,41353],{}," is usually the script file name",[152,49559,49560,49561,4151],{},"Every item in ",[17,49562,41117],{},[14,49564,49565],{},"For example, if you run:",[45,49567,49569],{"className":15497,"code":49568,"language":15499,"meta":50,"style":50},"python script.py hello 123\n",[17,49570,49571],{"__ignoreMap":50},[54,49572,49573,49575,49578,49581],{"class":56,"line":57},[54,49574,49],{"class":10811},[54,49576,49577],{"class":548}," script.py",[54,49579,49580],{"class":548}," hello",[54,49582,49583],{"class":99}," 123\n",[14,49585,49586],{},"Python will usually build this list:",[45,49588,49590],{"className":47,"code":49589,"language":49,"meta":50,"style":50},"['script.py', 'hello', '123']\n",[17,49591,49592],{"__ignoreMap":50},[54,49593,49594,49596,49598,49600,49602,49604,49606,49608,49610,49612,49614,49617,49619],{"class":56,"line":57},[54,49595,459],{"class":88},[54,49597,2240],{"class":544},[54,49599,26302],{"class":548},[54,49601,2240],{"class":544},[54,49603,103],{"class":88},[54,49605,2234],{"class":544},[54,49607,41557],{"class":548},[54,49609,2240],{"class":544},[54,49611,103],{"class":88},[54,49613,2234],{"class":544},[54,49615,49616],{"class":548},"123",[54,49618,2240],{"class":544},[54,49620,469],{"class":88},[14,49622,343],{},[149,49624,49625,49632,49639],{},[152,49626,49627,337,49629],{},[17,49628,41353],{},[17,49630,49631],{},"'script.py'",[152,49633,49634,337,49636],{},[17,49635,42049],{},[17,49637,49638],{},"'hello'",[152,49640,49641,337,49644],{},[17,49642,49643],{},"sys.argv[2]",[17,49645,49646],{},"'123'",[14,49648,8778,49649,89],{},[192,49650,49651],{"href":42317},"Python sys module overview",[40,49653,49655],{"id":49654},"when-to-use-sysargv","When to use sys.argv",[14,49657,126,49658,21209],{},[17,49659,41117],{},[149,49661,49662,49665,49668],{},[152,49663,49664],{},"You want input from the command line",[152,49666,49667],{},"You are building a small script or beginner command line tool",[152,49669,49670],{},"You want to pass file names, numbers, or simple options",[14,49672,49673],{},"It is a good fit for simple tasks such as:",[149,49675,49676,49679,49682],{},[152,49677,49678],{},"adding two numbers",[152,49680,49681],{},"reading a file name",[152,49683,49684],{},"printing a custom message",[14,49686,49687,49688,41913],{},"It is not ideal for more advanced command line programs with many options. For that, ",[17,49689,41912],{},[40,49691,2258],{"id":2257},[14,49693,49694,49695,49697],{},"Here is a simple script that prints the full ",[17,49696,41117],{}," list.",[45,49699,49701],{"className":47,"code":49700,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Full argument list:\")\nprint(sys.argv)\n",[17,49702,49703,49709,49713,49728],{"__ignoreMap":50},[54,49704,49705,49707],{"class":56,"line":57},[54,49706,61],{"class":60},[54,49708,41003],{"class":64},[54,49710,49711],{"class":56,"line":68},[54,49712,72],{"emptyLinePlaceholder":71},[54,49714,49715,49717,49719,49721,49724,49726],{"class":56,"line":75},[54,49716,116],{"class":115},[54,49718,96],{"class":88},[54,49720,545],{"class":544},[54,49722,49723],{"class":548},"Full argument list:",[54,49725,545],{"class":544},[54,49727,109],{"class":88},[54,49729,49730,49732,49734,49736,49738,49740],{"class":56,"line":112},[54,49731,116],{"class":115},[54,49733,96],{"class":88},[54,49735,40958],{"class":92},[54,49737,89],{"class":88},[54,49739,41050],{"class":1693},[54,49741,109],{"class":88},[14,49743,49744,49745,49747],{},"If the file is named ",[17,49746,26302],{}," and you run:",[45,49749,49751],{"className":15497,"code":49750,"language":15499,"meta":50,"style":50},"python script.py apple banana\n",[17,49752,49753],{"__ignoreMap":50},[54,49754,49755,49757,49759,49762],{"class":56,"line":57},[54,49756,49],{"class":10811},[54,49758,49577],{"class":548},[54,49760,49761],{"class":548}," apple",[54,49763,49764],{"class":548}," banana\n",[14,49766,49767],{},"You might see:",[45,49769,49771],{"className":47,"code":49770,"language":49,"meta":50,"style":50},"Full argument list:\n['script.py', 'apple', 'banana']\n",[17,49772,49773,49782],{"__ignoreMap":50},[54,49774,49775,49778,49780],{"class":56,"line":57},[54,49776,49777],{"class":64},"Full argument ",[54,49779,430],{"class":429},[54,49781,7572],{"class":88},[54,49783,49784,49786,49788,49790,49792,49794,49796,49798,49800,49802,49804,49806,49808],{"class":56,"line":68},[54,49785,459],{"class":88},[54,49787,2240],{"class":544},[54,49789,26302],{"class":548},[54,49791,2240],{"class":544},[54,49793,103],{"class":88},[54,49795,2234],{"class":544},[54,49797,10399],{"class":548},[54,49799,2240],{"class":544},[54,49801,103],{"class":88},[54,49803,2234],{"class":544},[54,49805,10408],{"class":548},[54,49807,2240],{"class":544},[54,49809,469],{"class":88},[14,49811,49812],{},"The script name appears first because Python includes it as the first item in the argument list.",[40,49814,49816],{"id":49815},"reading-specific-arguments","Reading specific arguments",[14,49818,49819],{},"You can access individual values by index.",[45,49821,49823],{"className":47,"code":49822,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Script name:\", sys.argv[0])\n\nif len(sys.argv) > 1:\n    print(\"First user argument:\", sys.argv[1])\n\nif len(sys.argv) > 2:\n    print(\"Second user argument:\", sys.argv[2])\n",[17,49824,49825,49831,49835,49861,49865,49887,49914,49918,49940],{"__ignoreMap":50},[54,49826,49827,49829],{"class":56,"line":57},[54,49828,61],{"class":60},[54,49830,41003],{"class":64},[54,49832,49833],{"class":56,"line":68},[54,49834,72],{"emptyLinePlaceholder":71},[54,49836,49837,49839,49841,49843,49845,49847,49849,49851,49853,49855,49857,49859],{"class":56,"line":75},[54,49838,116],{"class":115},[54,49840,96],{"class":88},[54,49842,545],{"class":544},[54,49844,41415],{"class":548},[54,49846,545],{"class":544},[54,49848,103],{"class":88},[54,49850,41252],{"class":92},[54,49852,89],{"class":88},[54,49854,41050],{"class":1693},[54,49856,459],{"class":88},[54,49858,752],{"class":99},[54,49860,937],{"class":88},[54,49862,49863],{"class":56,"line":112},[54,49864,72],{"emptyLinePlaceholder":71},[54,49866,49867,49869,49871,49873,49875,49877,49879,49881,49883,49885],{"class":56,"line":887},[54,49868,22942],{"class":60},[54,49870,868],{"class":115},[54,49872,96],{"class":88},[54,49874,40958],{"class":92},[54,49876,89],{"class":88},[54,49878,41050],{"class":1693},[54,49880,876],{"class":88},[54,49882,41454],{"class":81},[54,49884,882],{"class":99},[54,49886,7572],{"class":88},[54,49888,49889,49891,49893,49895,49898,49900,49902,49904,49906,49908,49910,49912],{"class":56,"line":892},[54,49890,7606],{"class":115},[54,49892,96],{"class":88},[54,49894,545],{"class":544},[54,49896,49897],{"class":548},"First user argument:",[54,49899,545],{"class":544},[54,49901,103],{"class":88},[54,49903,41252],{"class":92},[54,49905,89],{"class":88},[54,49907,41050],{"class":1693},[54,49909,459],{"class":88},[54,49911,100],{"class":99},[54,49913,937],{"class":88},[54,49915,49916],{"class":56,"line":913},[54,49917,72],{"emptyLinePlaceholder":71},[54,49919,49920,49922,49924,49926,49928,49930,49932,49934,49936,49938],{"class":56,"line":2121},[54,49921,22942],{"class":60},[54,49923,868],{"class":115},[54,49925,96],{"class":88},[54,49927,40958],{"class":92},[54,49929,89],{"class":88},[54,49931,41050],{"class":1693},[54,49933,876],{"class":88},[54,49935,41454],{"class":81},[54,49937,466],{"class":99},[54,49939,7572],{"class":88},[54,49941,49942,49944,49946,49948,49951,49953,49955,49957,49959,49961,49963,49965],{"class":56,"line":2136},[54,49943,7606],{"class":115},[54,49945,96],{"class":88},[54,49947,545],{"class":544},[54,49949,49950],{"class":548},"Second user argument:",[54,49952,545],{"class":544},[54,49954,103],{"class":88},[54,49956,41252],{"class":92},[54,49958,89],{"class":88},[54,49960,41050],{"class":1693},[54,49962,459],{"class":88},[54,49964,396],{"class":99},[54,49966,937],{"class":88},[14,49968,4136],{},[149,49970,49971,49976,49981],{},[152,49972,49973,49974],{},"User-provided values start at ",[17,49975,42049],{},[152,49977,49978,49980],{},[17,49979,41353],{}," is usually the script name",[152,49982,49983,49984],{},"Always check the length first with ",[192,49985,49987],{"href":49986},"\u002Freference\u002Fpython-len-function-explained",[17,49988,49989],{},"len()",[14,49991,49992],{},"Why this matters:",[14,49994,49995,49996,49998,49999,89],{},"If you try to read ",[17,49997,42049],{}," when no argument was given, Python raises an error because that list position does not exist. This is the same kind of problem explained in ",[192,50000,50001],{"href":40888},"IndexError: list index out of range",[14,50003,50004],{},"Example of unsafe code:",[45,50006,50008],{"className":47,"code":50007,"language":49,"meta":50,"style":50},"import sys\n\nprint(sys.argv[1])\n",[17,50009,50010,50016,50020],{"__ignoreMap":50},[54,50011,50012,50014],{"class":56,"line":57},[54,50013,61],{"class":60},[54,50015,41003],{"class":64},[54,50017,50018],{"class":56,"line":68},[54,50019,72],{"emptyLinePlaceholder":71},[54,50021,50022,50024,50026,50028,50030,50032,50034,50036],{"class":56,"line":75},[54,50023,116],{"class":115},[54,50025,96],{"class":88},[54,50027,40958],{"class":92},[54,50029,89],{"class":88},[54,50031,41050],{"class":1693},[54,50033,459],{"class":88},[54,50035,100],{"class":99},[54,50037,937],{"class":88},[14,50039,41509],{},[45,50041,50043],{"className":15497,"code":50042,"language":15499,"meta":50,"style":50},"python script.py\n",[17,50044,50045],{"__ignoreMap":50},[54,50046,50047,50049],{"class":56,"line":57},[54,50048,49],{"class":10811},[54,50050,26030],{"class":548},[14,50052,50053,50054,89],{},"You will get an ",[17,50055,50056],{},"IndexError",[14,50058,50059],{},"Safer version:",[45,50061,50063],{"className":47,"code":50062,"language":49,"meta":50,"style":50},"import sys\n\nif len(sys.argv) > 1:\n    print(sys.argv[1])\nelse:\n    print(\"No argument was provided.\")\n",[17,50064,50065,50071,50075,50097,50115,50121],{"__ignoreMap":50},[54,50066,50067,50069],{"class":56,"line":57},[54,50068,61],{"class":60},[54,50070,41003],{"class":64},[54,50072,50073],{"class":56,"line":68},[54,50074,72],{"emptyLinePlaceholder":71},[54,50076,50077,50079,50081,50083,50085,50087,50089,50091,50093,50095],{"class":56,"line":75},[54,50078,22942],{"class":60},[54,50080,868],{"class":115},[54,50082,96],{"class":88},[54,50084,40958],{"class":92},[54,50086,89],{"class":88},[54,50088,41050],{"class":1693},[54,50090,876],{"class":88},[54,50092,41454],{"class":81},[54,50094,882],{"class":99},[54,50096,7572],{"class":88},[54,50098,50099,50101,50103,50105,50107,50109,50111,50113],{"class":56,"line":112},[54,50100,7606],{"class":115},[54,50102,96],{"class":88},[54,50104,40958],{"class":92},[54,50106,89],{"class":88},[54,50108,41050],{"class":1693},[54,50110,459],{"class":88},[54,50112,100],{"class":99},[54,50114,937],{"class":88},[54,50116,50117,50119],{"class":56,"line":887},[54,50118,22975],{"class":60},[54,50120,7572],{"class":88},[54,50122,50123,50125,50127,50129,50132,50134],{"class":56,"line":892},[54,50124,7606],{"class":115},[54,50126,96],{"class":88},[54,50128,545],{"class":544},[54,50130,50131],{"class":548},"No argument was provided.",[54,50133,545],{"class":544},[54,50135,109],{"class":88},[40,50137,50139],{"id":50138},"converting-argument-types","Converting argument types",[14,50141,50142],{},"All command line arguments are strings by default.",[14,50144,50145,50146,50148],{},"That means this code does ",[35,50147,3036],{}," add numbers correctly:",[45,50150,50152],{"className":47,"code":50151,"language":49,"meta":50,"style":50},"import sys\n\na = sys.argv[1]\nb = sys.argv[2]\n\nprint(a + b)\n",[17,50153,50154,50160,50164,50183,50202,50206],{"__ignoreMap":50},[54,50155,50156,50158],{"class":56,"line":57},[54,50157,61],{"class":60},[54,50159,41003],{"class":64},[54,50161,50162],{"class":56,"line":68},[54,50163,72],{"emptyLinePlaceholder":71},[54,50165,50166,50169,50171,50173,50175,50177,50179,50181],{"class":56,"line":75},[54,50167,50168],{"class":64},"a ",[54,50170,82],{"class":81},[54,50172,41252],{"class":64},[54,50174,89],{"class":88},[54,50176,41050],{"class":1693},[54,50178,459],{"class":88},[54,50180,100],{"class":99},[54,50182,469],{"class":88},[54,50184,50185,50188,50190,50192,50194,50196,50198,50200],{"class":56,"line":112},[54,50186,50187],{"class":64},"b ",[54,50189,82],{"class":81},[54,50191,41252],{"class":64},[54,50193,89],{"class":88},[54,50195,41050],{"class":1693},[54,50197,459],{"class":88},[54,50199,396],{"class":99},[54,50201,469],{"class":88},[54,50203,50204],{"class":56,"line":887},[54,50205,72],{"emptyLinePlaceholder":71},[54,50207,50208,50210,50212,50214,50216,50218],{"class":56,"line":892},[54,50209,116],{"class":115},[54,50211,96],{"class":88},[54,50213,50168],{"class":92},[54,50215,29745],{"class":81},[54,50217,224],{"class":92},[54,50219,109],{"class":88},[14,50221,41509],{},[45,50223,50225],{"className":15497,"code":50224,"language":15499,"meta":50,"style":50},"python script.py 5 10\n",[17,50226,50227],{"__ignoreMap":50},[54,50228,50229,50231,50233,50235],{"class":56,"line":57},[54,50230,49],{"class":10811},[54,50232,49577],{"class":548},[54,50234,1235],{"class":99},[54,50236,19431],{"class":99},[14,50238,50239],{},"The result is:",[45,50241,50243],{"className":47,"code":50242,"language":49,"meta":50,"style":50},"510\n",[17,50244,50245],{"__ignoreMap":50},[54,50246,50247],{"class":56,"line":57},[54,50248,50242],{"class":99},[14,50250,50251],{},"This happens because Python is joining two strings, not adding two numbers.",[14,50253,50254],{},"To work with numbers, convert them first.",[602,50256,50258],{"id":50257},"convert-to-int","Convert to int",[45,50260,50262],{"className":47,"code":50261,"language":49,"meta":50,"style":50},"import sys\n\nif len(sys.argv) > 2:\n    a = int(sys.argv[1])\n    b = int(sys.argv[2])\n    print(a + b)\nelse:\n    print(\"Please provide two whole numbers.\")\n",[17,50263,50264,50270,50274,50296,50319,50342,50356,50362],{"__ignoreMap":50},[54,50265,50266,50268],{"class":56,"line":57},[54,50267,61],{"class":60},[54,50269,41003],{"class":64},[54,50271,50272],{"class":56,"line":68},[54,50273,72],{"emptyLinePlaceholder":71},[54,50275,50276,50278,50280,50282,50284,50286,50288,50290,50292,50294],{"class":56,"line":75},[54,50277,22942],{"class":60},[54,50279,868],{"class":115},[54,50281,96],{"class":88},[54,50283,40958],{"class":92},[54,50285,89],{"class":88},[54,50287,41050],{"class":1693},[54,50289,876],{"class":88},[54,50291,41454],{"class":81},[54,50293,466],{"class":99},[54,50295,7572],{"class":88},[54,50297,50298,50301,50303,50305,50307,50309,50311,50313,50315,50317],{"class":56,"line":112},[54,50299,50300],{"class":64},"    a ",[54,50302,82],{"class":81},[54,50304,41963],{"class":429},[54,50306,96],{"class":88},[54,50308,40958],{"class":92},[54,50310,89],{"class":88},[54,50312,41050],{"class":1693},[54,50314,459],{"class":88},[54,50316,100],{"class":99},[54,50318,937],{"class":88},[54,50320,50321,50324,50326,50328,50330,50332,50334,50336,50338,50340],{"class":56,"line":887},[54,50322,50323],{"class":64},"    b ",[54,50325,82],{"class":81},[54,50327,41963],{"class":429},[54,50329,96],{"class":88},[54,50331,40958],{"class":92},[54,50333,89],{"class":88},[54,50335,41050],{"class":1693},[54,50337,459],{"class":88},[54,50339,396],{"class":99},[54,50341,937],{"class":88},[54,50343,50344,50346,50348,50350,50352,50354],{"class":56,"line":892},[54,50345,7606],{"class":115},[54,50347,96],{"class":88},[54,50349,50168],{"class":92},[54,50351,29745],{"class":81},[54,50353,224],{"class":92},[54,50355,109],{"class":88},[54,50357,50358,50360],{"class":56,"line":913},[54,50359,22975],{"class":60},[54,50361,7572],{"class":88},[54,50363,50364,50366,50368,50370,50373,50375],{"class":56,"line":2121},[54,50365,7606],{"class":115},[54,50367,96],{"class":88},[54,50369,545],{"class":544},[54,50371,50372],{"class":548},"Please provide two whole numbers.",[54,50374,545],{"class":544},[54,50376,109],{"class":88},[14,50378,50379],{},"Output for:",[45,50381,50382],{"className":15497,"code":50224,"language":15499,"meta":50,"style":50},[17,50383,50384],{"__ignoreMap":50},[54,50385,50386,50388,50390,50392],{"class":56,"line":57},[54,50387,49],{"class":10811},[54,50389,49577],{"class":548},[54,50391,1235],{"class":99},[54,50393,19431],{"class":99},[14,50395,50396],{},"is:",[45,50398,50399],{"className":47,"code":32242,"language":49,"meta":50,"style":50},[17,50400,50401],{"__ignoreMap":50},[54,50402,50403],{"class":56,"line":57},[54,50404,32242],{"class":99},[14,50406,50407,50408,89],{},"You can learn more about integer conversion in ",[192,50409,42023],{"href":42022},[602,50411,50413],{"id":50412},"convert-to-float","Convert to float",[45,50415,50417],{"className":47,"code":50416,"language":49,"meta":50,"style":50},"import sys\n\nif len(sys.argv) > 2:\n    a = float(sys.argv[1])\n    b = float(sys.argv[2])\n    print(a + b)\nelse:\n    print(\"Please provide two decimal numbers.\")\n",[17,50418,50419,50425,50429,50451,50473,50495,50509,50515],{"__ignoreMap":50},[54,50420,50421,50423],{"class":56,"line":57},[54,50422,61],{"class":60},[54,50424,41003],{"class":64},[54,50426,50427],{"class":56,"line":68},[54,50428,72],{"emptyLinePlaceholder":71},[54,50430,50431,50433,50435,50437,50439,50441,50443,50445,50447,50449],{"class":56,"line":75},[54,50432,22942],{"class":60},[54,50434,868],{"class":115},[54,50436,96],{"class":88},[54,50438,40958],{"class":92},[54,50440,89],{"class":88},[54,50442,41050],{"class":1693},[54,50444,876],{"class":88},[54,50446,41454],{"class":81},[54,50448,466],{"class":99},[54,50450,7572],{"class":88},[54,50452,50453,50455,50457,50459,50461,50463,50465,50467,50469,50471],{"class":56,"line":112},[54,50454,50300],{"class":64},[54,50456,82],{"class":81},[54,50458,20288],{"class":429},[54,50460,96],{"class":88},[54,50462,40958],{"class":92},[54,50464,89],{"class":88},[54,50466,41050],{"class":1693},[54,50468,459],{"class":88},[54,50470,100],{"class":99},[54,50472,937],{"class":88},[54,50474,50475,50477,50479,50481,50483,50485,50487,50489,50491,50493],{"class":56,"line":887},[54,50476,50323],{"class":64},[54,50478,82],{"class":81},[54,50480,20288],{"class":429},[54,50482,96],{"class":88},[54,50484,40958],{"class":92},[54,50486,89],{"class":88},[54,50488,41050],{"class":1693},[54,50490,459],{"class":88},[54,50492,396],{"class":99},[54,50494,937],{"class":88},[54,50496,50497,50499,50501,50503,50505,50507],{"class":56,"line":892},[54,50498,7606],{"class":115},[54,50500,96],{"class":88},[54,50502,50168],{"class":92},[54,50504,29745],{"class":81},[54,50506,224],{"class":92},[54,50508,109],{"class":88},[54,50510,50511,50513],{"class":56,"line":913},[54,50512,22975],{"class":60},[54,50514,7572],{"class":88},[54,50516,50517,50519,50521,50523,50526,50528],{"class":56,"line":2121},[54,50518,7606],{"class":115},[54,50520,96],{"class":88},[54,50522,545],{"class":544},[54,50524,50525],{"class":548},"Please provide two decimal numbers.",[54,50527,545],{"class":544},[54,50529,109],{"class":88},[14,50531,50532,50533,134,50536,89],{},"This is useful when values may include decimals such as ",[17,50534,50535],{},"2.5",[17,50537,19967],{},[602,50539,50541],{"id":50540},"invalid-conversions","Invalid conversions",[14,50543,50544,50545,89],{},"If a value cannot be turned into a number, Python raises ",[17,50546,1306],{},[14,50548,253],{},[45,50550,50552],{"className":47,"code":50551,"language":49,"meta":50,"style":50},"import sys\n\nnumber = int(sys.argv[1])\nprint(number)\n",[17,50553,50554,50560,50564,50586],{"__ignoreMap":50},[54,50555,50556,50558],{"class":56,"line":57},[54,50557,61],{"class":60},[54,50559,41003],{"class":64},[54,50561,50562],{"class":56,"line":68},[54,50563,72],{"emptyLinePlaceholder":71},[54,50565,50566,50568,50570,50572,50574,50576,50578,50580,50582,50584],{"class":56,"line":75},[54,50567,78],{"class":64},[54,50569,82],{"class":81},[54,50571,41963],{"class":429},[54,50573,96],{"class":88},[54,50575,40958],{"class":92},[54,50577,89],{"class":88},[54,50579,41050],{"class":1693},[54,50581,459],{"class":88},[54,50583,100],{"class":99},[54,50585,937],{"class":88},[54,50587,50588,50590,50592,50594],{"class":56,"line":112},[54,50589,116],{"class":115},[54,50591,96],{"class":88},[54,50593,121],{"class":92},[54,50595,109],{"class":88},[14,50597,50598],{},"Run:",[45,50600,50602],{"className":15497,"code":50601,"language":15499,"meta":50,"style":50},"python script.py hello\n",[17,50603,50604],{"__ignoreMap":50},[54,50605,50606,50608,50610],{"class":56,"line":57},[54,50607,49],{"class":10811},[54,50609,49577],{"class":548},[54,50611,41524],{"class":548},[14,50613,10357,50614,50617],{},[17,50615,50616],{},"\"hello\""," is not a valid integer.",[40,50619,50621],{"id":50620},"common-beginner-errors","Common beginner errors",[14,50623,15032,50624,147],{},[17,50625,41117],{},[149,50627,50628,50632,50637,50640],{},[152,50629,49243,50630],{},[17,50631,41898],{},[152,50633,5481,50634,50636],{},[17,50635,42049],{}," when no argument was passed",[152,50638,50639],{},"Trying to add argument values as numbers without converting them",[152,50641,50642],{},"Running code inside an IDE without setting command line arguments first",[14,50644,50645],{},"Common error causes include:",[149,50647,50648,50653,50663,50668],{},[152,50649,50650,50652],{},[17,50651,50056],{}," when accessing a missing argument",[152,50654,50655,50657,50658,5507,50660],{},[17,50656,1306],{}," when converting text with ",[17,50659,20065],{},[17,50661,50662],{},"float()",[152,50664,50665,50667],{},[17,50666,10040],{}," or incorrect results when treating strings like numbers",[152,50669,50670],{},"Confusion from counting the script name as a user argument",[14,50672,50673,50674,50677],{},"If you are just getting started with this topic, the step-by-step guide on ",[192,50675,50676],{"href":41592},"how to use command line arguments in Python"," may help.",[40,50679,50681],{"id":50680},"simple-pattern-for-safer-scripts","Simple pattern for safer scripts",[14,50683,50684],{},"A small script should check input before using it.",[14,50686,50687],{},"Here is a safe pattern:",[45,50689,50691],{"className":47,"code":50690,"language":49,"meta":50,"style":50},"import sys\n\nif len(sys.argv) != 3:\n    print(\"Usage: python script.py \u003Cnum1> \u003Cnum2>\")\n    sys.exit()\n\ntry:\n    num1 = int(sys.argv[1])\n    num2 = int(sys.argv[2])\nexcept ValueError:\n    print(\"Both arguments must be whole numbers.\")\n    sys.exit()\n\nprint(\"Sum:\", num1 + num2)\n",[17,50692,50693,50699,50703,50726,50741,50751,50755,50761,50784,50807,50815,50830,50840,50844],{"__ignoreMap":50},[54,50694,50695,50697],{"class":56,"line":57},[54,50696,61],{"class":60},[54,50698,41003],{"class":64},[54,50700,50701],{"class":56,"line":68},[54,50702,72],{"emptyLinePlaceholder":71},[54,50704,50705,50707,50709,50711,50713,50715,50717,50719,50722,50724],{"class":56,"line":75},[54,50706,22942],{"class":60},[54,50708,868],{"class":115},[54,50710,96],{"class":88},[54,50712,40958],{"class":92},[54,50714,89],{"class":88},[54,50716,41050],{"class":1693},[54,50718,876],{"class":88},[54,50720,50721],{"class":81}," !=",[54,50723,379],{"class":99},[54,50725,7572],{"class":88},[54,50727,50728,50730,50732,50734,50737,50739],{"class":56,"line":112},[54,50729,7606],{"class":115},[54,50731,96],{"class":88},[54,50733,545],{"class":544},[54,50735,50736],{"class":548},"Usage: python script.py \u003Cnum1> \u003Cnum2>",[54,50738,545],{"class":544},[54,50740,109],{"class":88},[54,50742,50743,50745,50747,50749],{"class":56,"line":887},[54,50744,41680],{"class":64},[54,50746,89],{"class":88},[54,50748,41685],{"class":92},[54,50750,1670],{"class":88},[54,50752,50753],{"class":56,"line":892},[54,50754,72],{"emptyLinePlaceholder":71},[54,50756,50757,50759],{"class":56,"line":913},[54,50758,7452],{"class":60},[54,50760,7572],{"class":88},[54,50762,50763,50766,50768,50770,50772,50774,50776,50778,50780,50782],{"class":56,"line":2121},[54,50764,50765],{"class":64},"    num1 ",[54,50767,82],{"class":81},[54,50769,41963],{"class":429},[54,50771,96],{"class":88},[54,50773,40958],{"class":92},[54,50775,89],{"class":88},[54,50777,41050],{"class":1693},[54,50779,459],{"class":88},[54,50781,100],{"class":99},[54,50783,937],{"class":88},[54,50785,50786,50789,50791,50793,50795,50797,50799,50801,50803,50805],{"class":56,"line":2136},[54,50787,50788],{"class":64},"    num2 ",[54,50790,82],{"class":81},[54,50792,41963],{"class":429},[54,50794,96],{"class":88},[54,50796,40958],{"class":92},[54,50798,89],{"class":88},[54,50800,41050],{"class":1693},[54,50802,459],{"class":88},[54,50804,396],{"class":99},[54,50806,937],{"class":88},[54,50808,50809,50811,50813],{"class":56,"line":2151},[54,50810,7455],{"class":60},[54,50812,7628],{"class":429},[54,50814,7572],{"class":88},[54,50816,50817,50819,50821,50823,50826,50828],{"class":56,"line":4442},[54,50818,7606],{"class":115},[54,50820,96],{"class":88},[54,50822,545],{"class":544},[54,50824,50825],{"class":548},"Both arguments must be whole numbers.",[54,50827,545],{"class":544},[54,50829,109],{"class":88},[54,50831,50832,50834,50836,50838],{"class":56,"line":4468},[54,50833,41680],{"class":64},[54,50835,89],{"class":88},[54,50837,41685],{"class":92},[54,50839,1670],{"class":88},[54,50841,50842],{"class":56,"line":9843},[54,50843,72],{"emptyLinePlaceholder":71},[54,50845,50846,50848,50850,50852,50855,50857,50859,50862,50864,50867],{"class":56,"line":9848},[54,50847,116],{"class":115},[54,50849,96],{"class":88},[54,50851,545],{"class":544},[54,50853,50854],{"class":548},"Sum:",[54,50856,545],{"class":544},[54,50858,103],{"class":88},[54,50860,50861],{"class":92}," num1 ",[54,50863,29745],{"class":81},[54,50865,50866],{"class":92}," num2",[54,50868,109],{"class":88},[14,50870,50871],{},"What this script does:",[149,50873,50874,50877,50880,50887],{},[152,50875,50876],{},"checks that exactly two user arguments were given",[152,50878,50879],{},"prints a usage message if they are missing",[152,50881,50882,50883,6676,50885],{},"converts the values inside ",[17,50884,7452],{},[17,50886,7455],{},[152,50888,50889],{},"exits early if the input is invalid",[14,50891,50892,50893,89],{},"If you want to stop a script early, see ",[192,50894,41152],{"href":41151},[40,50896,50898],{"id":50897},"when-to-use-argparse-instead","When to use argparse instead",[14,50900,50901,50903],{},[17,50902,41117],{}," is great for learning and for very small scripts.",[14,50905,126,50906,50908],{},[17,50907,41912],{}," instead when:",[149,50910,50911,50920,50923,50926],{},[152,50912,50913,50914,5507,50917],{},"you want named options like ",[17,50915,50916],{},"--file",[17,50918,50919],{},"--verbose",[152,50921,50922],{},"you want automatic help messages",[152,50924,50925],{},"your script has many arguments",[152,50927,50928],{},"your command line tool is getting larger",[14,50930,50931],{},"A good learning path is:",[27327,50933,50934,50939,50942],{},[152,50935,50936,50937],{},"Start with ",[17,50938,41117],{},[152,50940,50941],{},"Build one or two small scripts",[152,50943,50944,50945,50947],{},"Move to ",[17,50946,41912],{}," when you need more structure",[14,50949,50950,50951,89],{},"For a next step, see the ",[192,50952,50954],{"href":50953},"\u002Fexamples\u002Fpython-argument-parser-example-argparse","Python argparse example",[40,50956,1419],{"id":1418},[602,50958,50960,50961,3409],{"id":50959},"what-is-sysargv0","What is sys.argv",[54,50962,752],{},[14,50964,50965],{},"It is usually the script name or path used to run the script.",[602,50967,50969],{"id":50968},"why-are-command-line-arguments-strings","Why are command line arguments strings?",[14,50971,50972],{},"Python reads terminal input as text, so you must convert values yourself when needed.",[602,50974,50976,50977,50979],{"id":50975},"what-happens-if-i-use-sysargv1-with-no-argument","What happens if I use sys.argv",[54,50978,100],{}," with no argument?",[14,50981,50982,50983,50985],{},"Python raises ",[17,50984,50056],{}," because that list position does not exist.",[602,50987,50989],{"id":50988},"can-i-use-sysargv-in-an-ide","Can I use sys.argv in an IDE?",[14,50991,50992],{},"Yes, but many IDEs require you to set command line arguments in the run configuration.",[602,50994,50996],{"id":50995},"should-i-learn-sysargv-or-argparse-first","Should I learn sys.argv or argparse first?",[14,50998,50936,50999,51001,51002,51004],{},[17,51000,41117],{}," because it is simpler, then move to ",[17,51003,41912],{}," for more advanced scripts.",[40,51006,1510],{"id":1509},[149,51008,51009,51013,51018,51023,51027,51032,51036,51042],{},[152,51010,51011],{},[192,51012,49651],{"href":42317},[152,51014,51015],{},[192,51016,51017],{"href":41592},"How to use command line arguments in Python",[152,51019,51020],{},[192,51021,51022],{"href":40888},"IndexError: list index out of range fix explained",[152,51024,51025],{},[192,51026,42023],{"href":42022},[152,51028,51029],{},[192,51030,51031],{"href":49986},"Python len() explained",[152,51033,51034],{},[192,51035,41152],{"href":41151},[152,51037,51038],{},[192,51039,51041],{"href":51040},"\u002Fexamples\u002Fpython-command-line-tool-example","Python command line tool example",[152,51043,51044],{},[192,51045,51046],{"href":50953},"Python argument parser example with argparse",[1545,51048,51049],{},"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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":50,"searchDepth":68,"depth":68,"links":51051},[51052,51053,51054,51055,51056,51057,51062,51063,51064,51065,51074],{"id":3595,"depth":68,"text":3596},{"id":49529,"depth":68,"text":49530},{"id":49654,"depth":68,"text":49655},{"id":2257,"depth":68,"text":2258},{"id":49815,"depth":68,"text":49816},{"id":50138,"depth":68,"text":50139,"children":51058},[51059,51060,51061],{"id":50257,"depth":75,"text":50258},{"id":50412,"depth":75,"text":50413},{"id":50540,"depth":75,"text":50541},{"id":50620,"depth":68,"text":50621},{"id":50680,"depth":68,"text":50681},{"id":50897,"depth":68,"text":50898},{"id":1418,"depth":68,"text":1419,"children":51066},[51067,51069,51070,51072,51073],{"id":50959,"depth":75,"text":51068},"What is sys.argv0?",{"id":50968,"depth":75,"text":50969},{"id":50975,"depth":75,"text":51071},"What happens if I use sys.argv1 with no argument?",{"id":50988,"depth":75,"text":50989},{"id":50995,"depth":75,"text":50996},{"id":1509,"depth":68,"text":1510},"Master sys.argv explained in our comprehensive Python beginner guide.",{},{"title":49463,"description":51075},"standard-library\u002Fsys.argv-explained","9bWXpsQ_FfWyqdy9TccP4kdFm0DgOO_GrWpg75t0Gvc",{"id":51081,"title":51082,"body":51083,"description":53739,"extension":1577,"meta":53740,"navigation":71,"path":41151,"seo":53741,"stem":53742,"__hash__":53743},"content\u002Fstandard-library\u002Fsys.exit-function-explained.md","sys.exit() Function Explained",{"type":7,"value":51084,"toc":53690},[51085,51088,51093,51100,51117,51119,51204,51207,51214,51218,51239,51242,51297,51300,51307,51314,51316,51322,51351,51354,51357,51374,51376,51427,51431,51437,51444,51447,51490,51495,51498,51542,51548,51551,51559,51593,51599,51607,51636,51640,51645,51653,51655,51673,51684,51691,51762,51764,51770,51778,51784,51903,51905,51911,51920,51924,51929,51932,51949,51953,52045,52049,52152,52156,52241,52248,52252,52257,52260,52286,52289,52368,52371,52373,52423,52429,52433,52436,52460,52462,52625,52627,52633,52641,52645,52650,52655,52667,52669,52736,52738,52744,52749,52830,52832,52838,52842,52845,52848,52858,52865,52877,52880,52884,52889,52893,52985,52989,53090,53094,53138,53144,53215,53223,53360,53362,53367,53399,53405,53420,53425,53427,53452,53456,53512,53522,53524,53573,53582,53584,53588,53597,53601,53610,53614,53617,53621,53624,53628,53634,53636,53664,53667,53687],[10,51086,51082],{"id":51087},"sysexit-function-explained",[14,51089,51090,51092],{},[17,51091,40974],{}," stops a Python program early.",[14,51094,20683,51095,27,51097,51099],{},[17,51096,40958],{},[17,51098,40958],{}," before using it. This function is common in scripts that need to stop after invalid input, a missing file, or a finished command-line task.",[14,51101,51102,51103,51105,51106,51109,51110,51113,51114,51116],{},"A key detail is that ",[17,51104,40974],{}," does not simply \"shut Python down.\" It raises a special exception called ",[17,51107,51108],{},"SystemExit",". That matters because ",[17,51111,51112],{},"finally"," blocks still run, and code can catch ",[17,51115,51108],{}," if needed.",[40,51118,3596],{"id":3595},[45,51120,51122],{"className":47,"code":51121,"language":49,"meta":50,"style":50},"import sys\n\nname = input(\"Enter your name: \")\nif not name:\n    sys.exit(\"No name provided\")\n\nprint(\"Hello\", name)\n",[17,51123,51124,51130,51134,51152,51162,51181,51185],{"__ignoreMap":50},[54,51125,51126,51128],{"class":56,"line":57},[54,51127,61],{"class":60},[54,51129,41003],{"class":64},[54,51131,51132],{"class":56,"line":68},[54,51133,72],{"emptyLinePlaceholder":71},[54,51135,51136,51138,51140,51142,51144,51146,51148,51150],{"class":56,"line":75},[54,51137,41622],{"class":64},[54,51139,82],{"class":81},[54,51141,20217],{"class":115},[54,51143,96],{"class":88},[54,51145,545],{"class":544},[54,51147,41633],{"class":548},[54,51149,545],{"class":544},[54,51151,109],{"class":88},[54,51153,51154,51156,51158,51160],{"class":56,"line":112},[54,51155,22942],{"class":60},[54,51157,10056],{"class":81},[54,51159,10834],{"class":64},[54,51161,7572],{"class":88},[54,51163,51164,51166,51168,51170,51172,51174,51177,51179],{"class":56,"line":887},[54,51165,41680],{"class":64},[54,51167,89],{"class":88},[54,51169,41685],{"class":92},[54,51171,96],{"class":88},[54,51173,545],{"class":544},[54,51175,51176],{"class":548},"No name provided",[54,51178,545],{"class":544},[54,51180,109],{"class":88},[54,51182,51183],{"class":56,"line":892},[54,51184,72],{"emptyLinePlaceholder":71},[54,51186,51187,51189,51191,51193,51196,51198,51200,51202],{"class":56,"line":913},[54,51188,116],{"class":115},[54,51190,96],{"class":88},[54,51192,545],{"class":544},[54,51194,51195],{"class":548},"Hello",[54,51197,545],{"class":544},[54,51199,103],{"class":88},[54,51201,10834],{"class":92},[54,51203,109],{"class":88},[14,51205,51206],{},"If the user presses Enter without typing a name, the program stops.",[14,51208,51209,51210,51213],{},"The string ",[17,51211,51212],{},"\"No name provided\""," is shown as a message, and the program exits.",[40,51215,51217],{"id":51216},"what-sysexit-does","What sys.exit() does",[149,51219,51220,51227,51233,51236],{},[152,51221,51222,51224,51225],{},[17,51223,40974],{}," stops the current Python program by raising ",[17,51226,51108],{},[152,51228,20683,51229,27,51231,167],{},[17,51230,40958],{},[17,51232,40958],{},[152,51234,51235],{},"It is commonly used to exit after an error, invalid input, or a finished command-line task",[152,51237,51238],{},"This page explains the function itself, not general command-line argument handling",[14,51240,51241],{},"Simple example:",[45,51243,51245],{"className":47,"code":51244,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Program started\")\nsys.exit()\nprint(\"This line never runs\")\n",[17,51246,51247,51253,51257,51272,51282],{"__ignoreMap":50},[54,51248,51249,51251],{"class":56,"line":57},[54,51250,61],{"class":60},[54,51252,41003],{"class":64},[54,51254,51255],{"class":56,"line":68},[54,51256,72],{"emptyLinePlaceholder":71},[54,51258,51259,51261,51263,51265,51268,51270],{"class":56,"line":75},[54,51260,116],{"class":115},[54,51262,96],{"class":88},[54,51264,545],{"class":544},[54,51266,51267],{"class":548},"Program started",[54,51269,545],{"class":544},[54,51271,109],{"class":88},[54,51273,51274,51276,51278,51280],{"class":56,"line":112},[54,51275,40958],{"class":64},[54,51277,89],{"class":88},[54,51279,41685],{"class":92},[54,51281,1670],{"class":88},[54,51283,51284,51286,51288,51290,51293,51295],{"class":56,"line":887},[54,51285,116],{"class":115},[54,51287,96],{"class":88},[54,51289,545],{"class":544},[54,51291,51292],{"class":548},"This line never runs",[54,51294,545],{"class":544},[54,51296,109],{"class":88},[14,51298,51299],{},"Expected result:",[45,51301,51305],{"className":51302,"code":51304,"language":5171,"meta":50},[51303],"language-text","Program started\n",[17,51306,51304],{"__ignoreMap":50},[14,51308,51309,51310,51313],{},"The last ",[17,51311,51312],{},"print()"," does not run because the program exits first.",[40,51315,199],{"id":198},[14,51317,51318,51319,51321],{},"You can call ",[17,51320,40974],{}," with or without an argument.",[45,51323,51325],{"className":47,"code":51324,"language":49,"meta":50,"style":50},"sys.exit()\nsys.exit(code)\n",[17,51326,51327,51337],{"__ignoreMap":50},[54,51328,51329,51331,51333,51335],{"class":56,"line":57},[54,51330,40958],{"class":64},[54,51332,89],{"class":88},[54,51334,41685],{"class":92},[54,51336,1670],{"class":88},[54,51338,51339,51341,51343,51345,51347,51349],{"class":56,"line":68},[54,51340,40958],{"class":64},[54,51342,89],{"class":88},[54,51344,41685],{"class":92},[54,51346,96],{"class":88},[54,51348,17],{"class":92},[54,51350,109],{"class":88},[14,51352,51353],{},"The argument is optional.",[14,51355,51356],{},"Common patterns:",[149,51358,51359,51364,51369],{},[152,51360,51361,51363],{},[17,51362,40974],{}," for a normal exit",[152,51365,51366,51368],{},[17,51367,41733],{}," for a successful exit",[152,51370,51371,51373],{},[17,51372,41727],{}," for an error",[14,51375,253],{},[45,51377,51379],{"className":47,"code":51378,"language":49,"meta":50,"style":50},"import sys\n\nfinished = True\n\nif finished:\n    sys.exit(0)\n",[17,51380,51381,51387,51391,51400,51404,51413],{"__ignoreMap":50},[54,51382,51383,51385],{"class":56,"line":57},[54,51384,61],{"class":60},[54,51386,41003],{"class":64},[54,51388,51389],{"class":56,"line":68},[54,51390,72],{"emptyLinePlaceholder":71},[54,51392,51393,51396,51398],{"class":56,"line":75},[54,51394,51395],{"class":64},"finished ",[54,51397,82],{"class":81},[54,51399,34815],{"class":8608},[54,51401,51402],{"class":56,"line":112},[54,51403,72],{"emptyLinePlaceholder":71},[54,51405,51406,51408,51411],{"class":56,"line":887},[54,51407,22942],{"class":60},[54,51409,51410],{"class":64}," finished",[54,51412,7572],{"class":88},[54,51414,51415,51417,51419,51421,51423,51425],{"class":56,"line":892},[54,51416,41680],{"class":64},[54,51418,89],{"class":88},[54,51420,41685],{"class":92},[54,51422,96],{"class":88},[54,51424,752],{"class":99},[54,51426,109],{"class":88},[40,51428,51430],{"id":51429},"what-the-exit-argument-means","What the exit argument means",[14,51432,51433,51434,51436],{},"The value you pass to ",[17,51435,40974],{}," becomes the exit status or exit message.",[602,51438,51440,5507,51442],{"id":51439},"sysexit-or-sysexit0",[17,51441,40974],{},[17,51443,41733],{},[14,51445,51446],{},"These usually mean success.",[45,51448,51450],{"className":47,"code":51449,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Done\")\nsys.exit(0)\n",[17,51451,51452,51458,51462,51476],{"__ignoreMap":50},[54,51453,51454,51456],{"class":56,"line":57},[54,51455,61],{"class":60},[54,51457,41003],{"class":64},[54,51459,51460],{"class":56,"line":68},[54,51461,72],{"emptyLinePlaceholder":71},[54,51463,51464,51466,51468,51470,51472,51474],{"class":56,"line":75},[54,51465,116],{"class":115},[54,51467,96],{"class":88},[54,51469,545],{"class":544},[54,51471,42427],{"class":548},[54,51473,545],{"class":544},[54,51475,109],{"class":88},[54,51477,51478,51480,51482,51484,51486,51488],{"class":56,"line":112},[54,51479,40958],{"class":64},[54,51481,89],{"class":88},[54,51483,41685],{"class":92},[54,51485,96],{"class":88},[54,51487,752],{"class":99},[54,51489,109],{"class":88},[602,51491,51493],{"id":51492},"sysexit1",[17,51494,41727],{},[14,51496,51497],{},"This usually means the program stopped because of a problem.",[45,51499,51501],{"className":47,"code":51500,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Something went wrong\")\nsys.exit(1)\n",[17,51502,51503,51509,51513,51528],{"__ignoreMap":50},[54,51504,51505,51507],{"class":56,"line":57},[54,51506,61],{"class":60},[54,51508,41003],{"class":64},[54,51510,51511],{"class":56,"line":68},[54,51512,72],{"emptyLinePlaceholder":71},[54,51514,51515,51517,51519,51521,51524,51526],{"class":56,"line":75},[54,51516,116],{"class":115},[54,51518,96],{"class":88},[54,51520,545],{"class":544},[54,51522,51523],{"class":548},"Something went wrong",[54,51525,545],{"class":544},[54,51527,109],{"class":88},[54,51529,51530,51532,51534,51536,51538,51540],{"class":56,"line":112},[54,51531,40958],{"class":64},[54,51533,89],{"class":88},[54,51535,41685],{"class":92},[54,51537,96],{"class":88},[54,51539,100],{"class":99},[54,51541,109],{"class":88},[602,51543,51545],{"id":51544},"sysexitmessage",[17,51546,51547],{},"sys.exit(\"message\")",[14,51549,51550],{},"A string usually does two things:",[149,51552,51553,51556],{},[152,51554,51555],{},"prints the message",[152,51557,51558],{},"exits with a non-zero status",[45,51560,51562],{"className":47,"code":51561,"language":49,"meta":50,"style":50},"import sys\n\nsys.exit(\"Missing required file\")\n",[17,51563,51564,51570,51574],{"__ignoreMap":50},[54,51565,51566,51568],{"class":56,"line":57},[54,51567,61],{"class":60},[54,51569,41003],{"class":64},[54,51571,51572],{"class":56,"line":68},[54,51573,72],{"emptyLinePlaceholder":71},[54,51575,51576,51578,51580,51582,51584,51586,51589,51591],{"class":56,"line":75},[54,51577,40958],{"class":64},[54,51579,89],{"class":88},[54,51581,41685],{"class":92},[54,51583,96],{"class":88},[54,51585,545],{"class":544},[54,51587,51588],{"class":548},"Missing required file",[54,51590,545],{"class":544},[54,51592,109],{"class":88},[602,51594,51596],{"id":51595},"sysexitnone",[17,51597,51598],{},"sys.exit(None)",[14,51600,51601,51603,51604,51606],{},[17,51602,8775],{}," behaves like ",[17,51605,752],{},", so it usually means a normal exit.",[45,51608,51610],{"className":47,"code":51609,"language":49,"meta":50,"style":50},"import sys\n\nsys.exit(None)\n",[17,51611,51612,51618,51622],{"__ignoreMap":50},[54,51613,51614,51616],{"class":56,"line":57},[54,51615,61],{"class":60},[54,51617,41003],{"class":64},[54,51619,51620],{"class":56,"line":68},[54,51621,72],{"emptyLinePlaceholder":71},[54,51623,51624,51626,51628,51630,51632,51634],{"class":56,"line":75},[54,51625,40958],{"class":64},[54,51627,89],{"class":88},[54,51629,41685],{"class":92},[54,51631,96],{"class":88},[54,51633,8775],{"class":8608},[54,51635,109],{"class":88},[40,51637,51639],{"id":51638},"important-behavior-it-raises-systemexit","Important behavior: it raises SystemExit",[14,51641,51642,51643,89],{},"This is the most important thing to understand about ",[17,51644,40974],{},[14,51646,51647,51649,51650,51652],{},[17,51648,40974],{}," does not force-stop Python instantly in every situation. It raises the ",[17,51651,51108],{}," exception.",[14,51654,343],{},[149,51656,51657,51662,51667],{},[152,51658,51659,51661],{},[17,51660,51112],{}," blocks still run",[152,51663,51664,51665],{},"code can catch ",[17,51666,51108],{},[152,51668,51669,51670,51672],{},"the program may continue if ",[17,51671,51108],{}," is caught",[14,51674,51675,51676,134,51680,89],{},"If you are still learning exceptions, see ",[192,51677,51679],{"href":51678},"\u002Flearn\u002Fpython-errors-and-exceptions-explained","Python errors and exceptions explained",[192,51681,51683],{"href":51682},"\u002Flearn\u002Fusing-try-except-else-and-finally-in-python","using try, except, else, and finally in Python",[602,51685,51687,51688,51690],{"id":51686},"example-finally-still-runs","Example: ",[17,51689,51112],{}," still runs",[45,51692,51694],{"className":47,"code":51693,"language":49,"meta":50,"style":50},"import sys\n\ntry:\n    print(\"Inside try\")\n    sys.exit(1)\nfinally:\n    print(\"Cleaning up in finally\")\n",[17,51695,51696,51702,51706,51712,51727,51741,51747],{"__ignoreMap":50},[54,51697,51698,51700],{"class":56,"line":57},[54,51699,61],{"class":60},[54,51701,41003],{"class":64},[54,51703,51704],{"class":56,"line":68},[54,51705,72],{"emptyLinePlaceholder":71},[54,51707,51708,51710],{"class":56,"line":75},[54,51709,7452],{"class":60},[54,51711,7572],{"class":88},[54,51713,51714,51716,51718,51720,51723,51725],{"class":56,"line":112},[54,51715,7606],{"class":115},[54,51717,96],{"class":88},[54,51719,545],{"class":544},[54,51721,51722],{"class":548},"Inside try",[54,51724,545],{"class":544},[54,51726,109],{"class":88},[54,51728,51729,51731,51733,51735,51737,51739],{"class":56,"line":887},[54,51730,41680],{"class":64},[54,51732,89],{"class":88},[54,51734,41685],{"class":92},[54,51736,96],{"class":88},[54,51738,100],{"class":99},[54,51740,109],{"class":88},[54,51742,51743,51745],{"class":56,"line":892},[54,51744,51112],{"class":60},[54,51746,7572],{"class":88},[54,51748,51749,51751,51753,51755,51758,51760],{"class":56,"line":913},[54,51750,7606],{"class":115},[54,51752,96],{"class":88},[54,51754,545],{"class":544},[54,51756,51757],{"class":548},"Cleaning up in finally",[54,51759,545],{"class":544},[54,51761,109],{"class":88},[14,51763,14248],{},[45,51765,51768],{"className":51766,"code":51767,"language":5171,"meta":50},[51303],"Inside try\nCleaning up in finally\n",[17,51769,51767],{"__ignoreMap":50},[14,51771,19823,51772,51774,51775,51777],{},[17,51773,41727],{}," is called, the ",[17,51776,51112],{}," block still runs before Python exits.",[602,51779,51781,51782],{"id":51780},"example-catching-systemexit","Example: catching ",[17,51783,51108],{},[45,51785,51787],{"className":47,"code":51786,"language":49,"meta":50,"style":50},"import sys\n\ntry:\n    sys.exit(1)\nexcept SystemExit as e:\n    print(\"Caught:\", type(e).__name__)\n    print(\"Exit code:\", e.code)\n\nprint(\"Program continued\")\n",[17,51788,51789,51795,51799,51805,51819,51832,51861,51884,51888],{"__ignoreMap":50},[54,51790,51791,51793],{"class":56,"line":57},[54,51792,61],{"class":60},[54,51794,41003],{"class":64},[54,51796,51797],{"class":56,"line":68},[54,51798,72],{"emptyLinePlaceholder":71},[54,51800,51801,51803],{"class":56,"line":75},[54,51802,7452],{"class":60},[54,51804,7572],{"class":88},[54,51806,51807,51809,51811,51813,51815,51817],{"class":56,"line":112},[54,51808,41680],{"class":64},[54,51810,89],{"class":88},[54,51812,41685],{"class":92},[54,51814,96],{"class":88},[54,51816,100],{"class":99},[54,51818,109],{"class":88},[54,51820,51821,51823,51826,51828,51830],{"class":56,"line":887},[54,51822,7455],{"class":60},[54,51824,51825],{"class":429}," SystemExit",[54,51827,7631],{"class":60},[54,51829,7634],{"class":64},[54,51831,7572],{"class":88},[54,51833,51834,51836,51838,51840,51843,51845,51847,51850,51852,51854,51856,51859],{"class":56,"line":892},[54,51835,7606],{"class":115},[54,51837,96],{"class":88},[54,51839,545],{"class":544},[54,51841,51842],{"class":548},"Caught:",[54,51844,545],{"class":544},[54,51846,103],{"class":88},[54,51848,51849],{"class":429}," type",[54,51851,96],{"class":88},[54,51853,36844],{"class":92},[54,51855,41638],{"class":88},[54,51857,51858],{"class":4870},"__name__",[54,51860,109],{"class":88},[54,51862,51863,51865,51867,51869,51872,51874,51876,51878,51880,51882],{"class":56,"line":913},[54,51864,7606],{"class":115},[54,51866,96],{"class":88},[54,51868,545],{"class":544},[54,51870,51871],{"class":548},"Exit code:",[54,51873,545],{"class":544},[54,51875,103],{"class":88},[54,51877,7634],{"class":92},[54,51879,89],{"class":88},[54,51881,17],{"class":1693},[54,51883,109],{"class":88},[54,51885,51886],{"class":56,"line":2121},[54,51887,72],{"emptyLinePlaceholder":71},[54,51889,51890,51892,51894,51896,51899,51901],{"class":56,"line":2136},[54,51891,116],{"class":115},[54,51893,96],{"class":88},[54,51895,545],{"class":544},[54,51897,51898],{"class":548},"Program continued",[54,51900,545],{"class":544},[54,51902,109],{"class":88},[14,51904,14248],{},[45,51906,51909],{"className":51907,"code":51908,"language":5171,"meta":50},[51303],"Caught: SystemExit\nExit code: 1\nProgram continued\n",[17,51910,51908],{"__ignoreMap":50},[14,51912,51913,51914,51916,51917,51919],{},"In normal scripts, you usually do not catch ",[17,51915,51108],{},". But it is useful to know that ",[17,51918,40974],{}," works through an exception.",[40,51921,51923],{"id":51922},"when-beginners-should-use-sysexit","When beginners should use sys.exit()",[14,51925,51926,51928],{},[17,51927,40974],{}," is useful when your script should stop completely.",[14,51930,51931],{},"Good beginner use cases:",[149,51933,51934,51937,51940,51943,51946],{},[152,51935,51936],{},"stop a script when required input is missing",[152,51938,51939],{},"end a menu program when the user chooses quit",[152,51941,51942],{},"exit after printing a usage message",[152,51944,51945],{},"stop after detecting a file or configuration problem",[152,51947,51948],{},"use it in scripts more often than inside reusable functions",[602,51950,51952],{"id":51951},"example-exit-when-user-input-is-empty","Example: exit when user input is empty",[45,51954,51956],{"className":47,"code":51955,"language":49,"meta":50,"style":50},"import sys\n\nusername = input(\"Username: \")\n\nif not username:\n    sys.exit(\"Username is required\")\n\nprint(\"Welcome,\", username)\n",[17,51957,51958,51964,51968,51988,51992,52003,52022,52026],{"__ignoreMap":50},[54,51959,51960,51962],{"class":56,"line":57},[54,51961,61],{"class":60},[54,51963,41003],{"class":64},[54,51965,51966],{"class":56,"line":68},[54,51967,72],{"emptyLinePlaceholder":71},[54,51969,51970,51973,51975,51977,51979,51981,51984,51986],{"class":56,"line":75},[54,51971,51972],{"class":64},"username ",[54,51974,82],{"class":81},[54,51976,20217],{"class":115},[54,51978,96],{"class":88},[54,51980,545],{"class":544},[54,51982,51983],{"class":548},"Username: ",[54,51985,545],{"class":544},[54,51987,109],{"class":88},[54,51989,51990],{"class":56,"line":112},[54,51991,72],{"emptyLinePlaceholder":71},[54,51993,51994,51996,51998,52001],{"class":56,"line":887},[54,51995,22942],{"class":60},[54,51997,10056],{"class":81},[54,51999,52000],{"class":64}," username",[54,52002,7572],{"class":88},[54,52004,52005,52007,52009,52011,52013,52015,52018,52020],{"class":56,"line":892},[54,52006,41680],{"class":64},[54,52008,89],{"class":88},[54,52010,41685],{"class":92},[54,52012,96],{"class":88},[54,52014,545],{"class":544},[54,52016,52017],{"class":548},"Username is required",[54,52019,545],{"class":544},[54,52021,109],{"class":88},[54,52023,52024],{"class":56,"line":913},[54,52025,72],{"emptyLinePlaceholder":71},[54,52027,52028,52030,52032,52034,52037,52039,52041,52043],{"class":56,"line":2121},[54,52029,116],{"class":115},[54,52031,96],{"class":88},[54,52033,545],{"class":544},[54,52035,52036],{"class":548},"Welcome,",[54,52038,545],{"class":544},[54,52040,103],{"class":88},[54,52042,52000],{"class":92},[54,52044,109],{"class":88},[602,52046,52048],{"id":52047},"example-exit-when-a-file-does-not-exist","Example: exit when a file does not exist",[45,52050,52052],{"className":47,"code":52051,"language":49,"meta":50,"style":50},"import sys\nfrom pathlib import Path\n\nfile_path = Path(\"data.txt\")\n\nif not file_path.exists():\n    sys.exit(\"data.txt was not found\")\n\nprint(\"File exists\")\n",[17,52053,52054,52060,52072,52076,52095,52099,52114,52133,52137],{"__ignoreMap":50},[54,52055,52056,52058],{"class":56,"line":57},[54,52057,61],{"class":60},[54,52059,41003],{"class":64},[54,52061,52062,52064,52067,52069],{"class":56,"line":68},[54,52063,1640],{"class":60},[54,52065,52066],{"class":64}," pathlib ",[54,52068,61],{"class":60},[54,52070,52071],{"class":64}," Path\n",[54,52073,52074],{"class":56,"line":75},[54,52075,72],{"emptyLinePlaceholder":71},[54,52077,52078,52080,52082,52085,52087,52089,52091,52093],{"class":56,"line":112},[54,52079,28257],{"class":64},[54,52081,82],{"class":81},[54,52083,52084],{"class":92}," Path",[54,52086,96],{"class":88},[54,52088,545],{"class":544},[54,52090,23839],{"class":548},[54,52092,545],{"class":544},[54,52094,109],{"class":88},[54,52096,52097],{"class":56,"line":887},[54,52098,72],{"emptyLinePlaceholder":71},[54,52100,52101,52103,52105,52108,52110,52112],{"class":56,"line":892},[54,52102,22942],{"class":60},[54,52104,10056],{"class":81},[54,52106,52107],{"class":64}," file_path",[54,52109,89],{"class":88},[54,52111,24757],{"class":92},[54,52113,38129],{"class":88},[54,52115,52116,52118,52120,52122,52124,52126,52129,52131],{"class":56,"line":913},[54,52117,41680],{"class":64},[54,52119,89],{"class":88},[54,52121,41685],{"class":92},[54,52123,96],{"class":88},[54,52125,545],{"class":544},[54,52127,52128],{"class":548},"data.txt was not found",[54,52130,545],{"class":544},[54,52132,109],{"class":88},[54,52134,52135],{"class":56,"line":2121},[54,52136,72],{"emptyLinePlaceholder":71},[54,52138,52139,52141,52143,52145,52148,52150],{"class":56,"line":2136},[54,52140,116],{"class":115},[54,52142,96],{"class":88},[54,52144,545],{"class":544},[54,52146,52147],{"class":548},"File exists",[54,52149,545],{"class":544},[54,52151,109],{"class":88},[602,52153,52155],{"id":52154},"example-exit-after-printing-help-text","Example: exit after printing help text",[45,52157,52159],{"className":47,"code":52158,"language":49,"meta":50,"style":50},"import sys\n\nshow_help = True\n\nif show_help:\n    print(\"Usage: python script.py [filename]\")\n    sys.exit(0)\n\nprint(\"Main program runs here\")\n",[17,52160,52161,52167,52171,52180,52184,52193,52208,52222,52226],{"__ignoreMap":50},[54,52162,52163,52165],{"class":56,"line":57},[54,52164,61],{"class":60},[54,52166,41003],{"class":64},[54,52168,52169],{"class":56,"line":68},[54,52170,72],{"emptyLinePlaceholder":71},[54,52172,52173,52176,52178],{"class":56,"line":75},[54,52174,52175],{"class":64},"show_help ",[54,52177,82],{"class":81},[54,52179,34815],{"class":8608},[54,52181,52182],{"class":56,"line":112},[54,52183,72],{"emptyLinePlaceholder":71},[54,52185,52186,52188,52191],{"class":56,"line":887},[54,52187,22942],{"class":60},[54,52189,52190],{"class":64}," show_help",[54,52192,7572],{"class":88},[54,52194,52195,52197,52199,52201,52204,52206],{"class":56,"line":892},[54,52196,7606],{"class":115},[54,52198,96],{"class":88},[54,52200,545],{"class":544},[54,52202,52203],{"class":548},"Usage: python script.py [filename]",[54,52205,545],{"class":544},[54,52207,109],{"class":88},[54,52209,52210,52212,52214,52216,52218,52220],{"class":56,"line":913},[54,52211,41680],{"class":64},[54,52213,89],{"class":88},[54,52215,41685],{"class":92},[54,52217,96],{"class":88},[54,52219,752],{"class":99},[54,52221,109],{"class":88},[54,52223,52224],{"class":56,"line":2121},[54,52225,72],{"emptyLinePlaceholder":71},[54,52227,52228,52230,52232,52234,52237,52239],{"class":56,"line":2136},[54,52229,116],{"class":115},[54,52231,96],{"class":88},[54,52233,545],{"class":544},[54,52235,52236],{"class":548},"Main program runs here",[54,52238,545],{"class":544},[54,52240,109],{"class":88},[14,52242,52243,52244,89],{},"If you want a broader guide, see ",[192,52245,52247],{"href":52246},"\u002Fhow-to\u002Fhow-to-exit-a-program-in-python","how to exit a program in Python",[40,52249,52251],{"id":52250},"when-not-to-use-sysexit","When not to use sys.exit()",[14,52253,1403,52254,52256],{},[17,52255,40974],{}," for every kind of control flow.",[14,52258,52259],{},"Avoid it in these cases:",[149,52261,52262,52269,52272,52279],{},[152,52263,52264,52265,52268],{},"do not use it as the normal way to leave a function; use ",[17,52266,52267],{},"return"," there",[152,52270,52271],{},"avoid using it deep inside library code that other code imports",[152,52273,52274,52275,52278],{},"do not confuse it with ",[17,52276,52277],{},"break",", which only leaves a loop",[152,52280,52281,52282,52285],{},"do not use it to handle every small condition if a simple ",[17,52283,52284],{},"if\u002Felse"," is enough",[14,52287,52288],{},"Bad fit:",[45,52290,52292],{"className":47,"code":52291,"language":49,"meta":50,"style":50},"import sys\n\ndef add(a, b):\n    if a \u003C 0:\n        sys.exit(\"Negative numbers are not allowed\")\n    return a + b\n",[17,52293,52294,52300,52304,52323,52336,52356],{"__ignoreMap":50},[54,52295,52296,52298],{"class":56,"line":57},[54,52297,61],{"class":60},[54,52299,41003],{"class":64},[54,52301,52302],{"class":56,"line":68},[54,52303,72],{"emptyLinePlaceholder":71},[54,52305,52306,52309,52313,52315,52317,52319,52321],{"class":56,"line":75},[54,52307,52308],{"class":2230},"def",[54,52310,52312],{"class":52311},"sGLFI"," add",[54,52314,96],{"class":88},[54,52316,192],{"class":10833},[54,52318,103],{"class":88},[54,52320,224],{"class":10833},[54,52322,10837],{"class":88},[54,52324,52325,52327,52330,52332,52334],{"class":56,"line":112},[54,52326,27239],{"class":60},[54,52328,52329],{"class":64}," a ",[54,52331,2227],{"class":81},[54,52333,3986],{"class":99},[54,52335,7572],{"class":88},[54,52337,52338,52341,52343,52345,52347,52349,52352,52354],{"class":56,"line":887},[54,52339,52340],{"class":64},"        sys",[54,52342,89],{"class":88},[54,52344,41685],{"class":92},[54,52346,96],{"class":88},[54,52348,545],{"class":544},[54,52350,52351],{"class":548},"Negative numbers are not allowed",[54,52353,545],{"class":544},[54,52355,109],{"class":88},[54,52357,52358,52361,52363,52365],{"class":56,"line":892},[54,52359,52360],{"class":60},"    return",[54,52362,52329],{"class":64},[54,52364,29745],{"class":81},[54,52366,52367],{"class":64}," b\n",[14,52369,52370],{},"This makes the whole program stop, even though the function looks like a normal reusable function.",[14,52372,5605],{},[45,52374,52376],{"className":47,"code":52375,"language":49,"meta":50,"style":50},"def add(a, b):\n    if a \u003C 0:\n        return None\n    return a + b\n",[17,52377,52378,52394,52406,52413],{"__ignoreMap":50},[54,52379,52380,52382,52384,52386,52388,52390,52392],{"class":56,"line":57},[54,52381,52308],{"class":2230},[54,52383,52312],{"class":52311},[54,52385,96],{"class":88},[54,52387,192],{"class":10833},[54,52389,103],{"class":88},[54,52391,224],{"class":10833},[54,52393,10837],{"class":88},[54,52395,52396,52398,52400,52402,52404],{"class":56,"line":68},[54,52397,27239],{"class":60},[54,52399,52329],{"class":64},[54,52401,2227],{"class":81},[54,52403,3986],{"class":99},[54,52405,7572],{"class":88},[54,52407,52408,52411],{"class":56,"line":75},[54,52409,52410],{"class":60},"        return",[54,52412,9840],{"class":8608},[54,52414,52415,52417,52419,52421],{"class":56,"line":112},[54,52416,52360],{"class":60},[54,52418,52329],{"class":64},[54,52420,29745],{"class":81},[54,52422,52367],{"class":64},[14,52424,52425,52426,52428],{},"Or, in many real programs, raising a regular exception may be more appropriate than calling ",[17,52427,40974],{}," inside reusable code.",[40,52430,52432],{"id":52431},"difference-between-sysexit-and-return","Difference between sys.exit() and return",[14,52434,52435],{},"These two are not the same.",[149,52437,52438,52443,52450,52455],{},[152,52439,52440,52442],{},[17,52441,52267],{}," exits the current function only",[152,52444,52445,52447,52448,51672],{},[17,52446,40974],{}," exits the whole program unless ",[17,52449,51108],{},[152,52451,7449,52452,52454],{},[17,52453,52267],{}," inside functions to send a result back",[152,52456,7449,52457,52459],{},[17,52458,40974],{}," when the script should stop completely",[14,52461,253],{},[45,52463,52465],{"className":47,"code":52464,"language":49,"meta":50,"style":50},"import sys\n\ndef use_return():\n    print(\"Before return\")\n    return\n    print(\"This will not run\")\n\ndef use_sys_exit():\n    print(\"Before sys.exit()\")\n    sys.exit()\n    print(\"This will not run\")\n\nuse_return()\nprint(\"Program still running after return\")\n\nuse_sys_exit()\nprint(\"This line will not run\")\n",[17,52466,52467,52473,52477,52486,52501,52506,52521,52525,52534,52549,52559,52573,52577,52584,52599,52603,52610],{"__ignoreMap":50},[54,52468,52469,52471],{"class":56,"line":57},[54,52470,61],{"class":60},[54,52472,41003],{"class":64},[54,52474,52475],{"class":56,"line":68},[54,52476,72],{"emptyLinePlaceholder":71},[54,52478,52479,52481,52484],{"class":56,"line":75},[54,52480,52308],{"class":2230},[54,52482,52483],{"class":52311}," use_return",[54,52485,38129],{"class":88},[54,52487,52488,52490,52492,52494,52497,52499],{"class":56,"line":112},[54,52489,7606],{"class":115},[54,52491,96],{"class":88},[54,52493,545],{"class":544},[54,52495,52496],{"class":548},"Before return",[54,52498,545],{"class":544},[54,52500,109],{"class":88},[54,52502,52503],{"class":56,"line":887},[54,52504,52505],{"class":60},"    return\n",[54,52507,52508,52510,52512,52514,52517,52519],{"class":56,"line":892},[54,52509,7606],{"class":115},[54,52511,96],{"class":88},[54,52513,545],{"class":544},[54,52515,52516],{"class":548},"This will not run",[54,52518,545],{"class":544},[54,52520,109],{"class":88},[54,52522,52523],{"class":56,"line":913},[54,52524,72],{"emptyLinePlaceholder":71},[54,52526,52527,52529,52532],{"class":56,"line":2121},[54,52528,52308],{"class":2230},[54,52530,52531],{"class":52311}," use_sys_exit",[54,52533,38129],{"class":88},[54,52535,52536,52538,52540,52542,52545,52547],{"class":56,"line":2136},[54,52537,7606],{"class":115},[54,52539,96],{"class":88},[54,52541,545],{"class":544},[54,52543,52544],{"class":548},"Before sys.exit()",[54,52546,545],{"class":544},[54,52548,109],{"class":88},[54,52550,52551,52553,52555,52557],{"class":56,"line":2151},[54,52552,41680],{"class":64},[54,52554,89],{"class":88},[54,52556,41685],{"class":92},[54,52558,1670],{"class":88},[54,52560,52561,52563,52565,52567,52569,52571],{"class":56,"line":4442},[54,52562,7606],{"class":115},[54,52564,96],{"class":88},[54,52566,545],{"class":544},[54,52568,52516],{"class":548},[54,52570,545],{"class":544},[54,52572,109],{"class":88},[54,52574,52575],{"class":56,"line":4468},[54,52576,72],{"emptyLinePlaceholder":71},[54,52578,52579,52582],{"class":56,"line":9843},[54,52580,52581],{"class":92},"use_return",[54,52583,1670],{"class":88},[54,52585,52586,52588,52590,52592,52595,52597],{"class":56,"line":9848},[54,52587,116],{"class":115},[54,52589,96],{"class":88},[54,52591,545],{"class":544},[54,52593,52594],{"class":548},"Program still running after return",[54,52596,545],{"class":544},[54,52598,109],{"class":88},[54,52600,52601],{"class":56,"line":9853},[54,52602,72],{"emptyLinePlaceholder":71},[54,52604,52605,52608],{"class":56,"line":9884},[54,52606,52607],{"class":92},"use_sys_exit",[54,52609,1670],{"class":88},[54,52611,52612,52614,52616,52618,52621,52623],{"class":56,"line":16525},[54,52613,116],{"class":115},[54,52615,96],{"class":88},[54,52617,545],{"class":544},[54,52619,52620],{"class":548},"This line will not run",[54,52622,545],{"class":544},[54,52624,109],{"class":88},[14,52626,14248],{},[45,52628,52631],{"className":52629,"code":52630,"language":5171,"meta":50},[51303],"Before return\nProgram still running after return\nBefore sys.exit()\n",[17,52632,52630],{"__ignoreMap":50},[14,52634,52635,52636,52638,52639,89],{},"If you need the ",[17,52637,40958],{}," module more generally, see ",[192,52640,49651],{"href":42317},[40,52642,52644],{"id":52643},"difference-between-sysexit-and-break","Difference between sys.exit() and break",[14,52646,52647,52649],{},[17,52648,52277],{}," only stops the nearest loop.",[14,52651,52652,52654],{},[17,52653,40974],{}," stops the whole program.",[14,52656,52657,52658,52660,52661,52664,52665,16092],{},"If you only want to stop a ",[17,52659,26859],{}," loop or ",[17,52662,52663],{},"while"," loop, use ",[17,52666,52277],{},[14,52668,253],{},[45,52670,52672],{"className":47,"code":52671,"language":49,"meta":50,"style":50},"for number in range(5):\n    if number == 3:\n        break\n    print(number)\n\nprint(\"Loop ended, program continues\")\n",[17,52673,52674,52690,52702,52707,52717,52721],{"__ignoreMap":50},[54,52675,52676,52678,52680,52682,52684,52686,52688],{"class":56,"line":57},[54,52677,26859],{"class":60},[54,52679,43479],{"class":64},[54,52681,26865],{"class":60},[54,52683,43359],{"class":115},[54,52685,96],{"class":88},[54,52687,285],{"class":99},[54,52689,10837],{"class":88},[54,52691,52692,52694,52696,52698,52700],{"class":56,"line":68},[54,52693,27239],{"class":60},[54,52695,43479],{"class":64},[54,52697,41656],{"class":81},[54,52699,379],{"class":99},[54,52701,7572],{"class":88},[54,52703,52704],{"class":56,"line":75},[54,52705,52706],{"class":60},"        break\n",[54,52708,52709,52711,52713,52715],{"class":56,"line":112},[54,52710,7606],{"class":115},[54,52712,96],{"class":88},[54,52714,121],{"class":92},[54,52716,109],{"class":88},[54,52718,52719],{"class":56,"line":887},[54,52720,72],{"emptyLinePlaceholder":71},[54,52722,52723,52725,52727,52729,52732,52734],{"class":56,"line":892},[54,52724,116],{"class":115},[54,52726,96],{"class":88},[54,52728,545],{"class":544},[54,52730,52731],{"class":548},"Loop ended, program continues",[54,52733,545],{"class":544},[54,52735,109],{"class":88},[14,52737,449],{},[45,52739,52742],{"className":52740,"code":52741,"language":5171,"meta":50},[51303],"0\n1\n2\nLoop ended, program continues\n",[17,52743,52741],{"__ignoreMap":50},[14,52745,52746,52747,147],{},"Compare that with ",[17,52748,40974],{},[45,52750,52752],{"className":47,"code":52751,"language":49,"meta":50,"style":50},"import sys\n\nfor number in range(5):\n    if number == 3:\n        sys.exit()\n    print(number)\n\nprint(\"This line will not run\")\n",[17,52753,52754,52760,52764,52780,52792,52802,52812,52816],{"__ignoreMap":50},[54,52755,52756,52758],{"class":56,"line":57},[54,52757,61],{"class":60},[54,52759,41003],{"class":64},[54,52761,52762],{"class":56,"line":68},[54,52763,72],{"emptyLinePlaceholder":71},[54,52765,52766,52768,52770,52772,52774,52776,52778],{"class":56,"line":75},[54,52767,26859],{"class":60},[54,52769,43479],{"class":64},[54,52771,26865],{"class":60},[54,52773,43359],{"class":115},[54,52775,96],{"class":88},[54,52777,285],{"class":99},[54,52779,10837],{"class":88},[54,52781,52782,52784,52786,52788,52790],{"class":56,"line":112},[54,52783,27239],{"class":60},[54,52785,43479],{"class":64},[54,52787,41656],{"class":81},[54,52789,379],{"class":99},[54,52791,7572],{"class":88},[54,52793,52794,52796,52798,52800],{"class":56,"line":887},[54,52795,52340],{"class":64},[54,52797,89],{"class":88},[54,52799,41685],{"class":92},[54,52801,1670],{"class":88},[54,52803,52804,52806,52808,52810],{"class":56,"line":892},[54,52805,7606],{"class":115},[54,52807,96],{"class":88},[54,52809,121],{"class":92},[54,52811,109],{"class":88},[54,52813,52814],{"class":56,"line":913},[54,52815,72],{"emptyLinePlaceholder":71},[54,52817,52818,52820,52822,52824,52826,52828],{"class":56,"line":2121},[54,52819,116],{"class":115},[54,52821,96],{"class":88},[54,52823,545],{"class":544},[54,52825,52620],{"class":548},[54,52827,545],{"class":544},[54,52829,109],{"class":88},[14,52831,449],{},[45,52833,52836],{"className":52834,"code":52835,"language":5171,"meta":50},[51303],"0\n1\n2\n",[17,52837,52835],{"__ignoreMap":50},[40,52839,52841],{"id":52840},"exit-codes-in-simple-terms","Exit codes in simple terms",[14,52843,52844],{},"Exit codes help the operating system or shell understand whether your program succeeded.",[14,52846,52847],{},"Simple rule:",[149,52849,52850,52855],{},[152,52851,52852,52854],{},[17,52853,752],{}," usually means success",[152,52856,52857],{},"non-zero usually means an error or special stop reason",[14,52859,23588,52860,134,52862,52864],{},[17,52861,752],{},[17,52863,100],{}," are usually enough.",[149,52866,52867,52872],{},[152,52868,7449,52869,52871],{},[17,52870,752],{}," for normal success",[152,52873,7449,52874,52876],{},[17,52875,100],{}," when something went wrong",[14,52878,52879],{},"This matters more in command-line tools, where other programs may check whether your script succeeded.",[40,52881,52883],{"id":52882},"common-use-cases-to-include-in-examples","Common use cases to include in examples",[14,52885,52886,52887,89],{},"Here are the most common beginner-friendly uses of ",[17,52888,40974],{},[602,52890,52892],{"id":52891},"exit-when-user-input-is-empty","Exit when user input is empty",[45,52894,52896],{"className":47,"code":52895,"language":49,"meta":50,"style":50},"import sys\n\nemail = input(\"Enter email: \")\n\nif not email:\n    sys.exit(\"Email is required\")\n\nprint(\"Email saved:\", email)\n",[17,52897,52898,52904,52908,52928,52932,52943,52962,52966],{"__ignoreMap":50},[54,52899,52900,52902],{"class":56,"line":57},[54,52901,61],{"class":60},[54,52903,41003],{"class":64},[54,52905,52906],{"class":56,"line":68},[54,52907,72],{"emptyLinePlaceholder":71},[54,52909,52910,52913,52915,52917,52919,52921,52924,52926],{"class":56,"line":75},[54,52911,52912],{"class":64},"email ",[54,52914,82],{"class":81},[54,52916,20217],{"class":115},[54,52918,96],{"class":88},[54,52920,545],{"class":544},[54,52922,52923],{"class":548},"Enter email: ",[54,52925,545],{"class":544},[54,52927,109],{"class":88},[54,52929,52930],{"class":56,"line":112},[54,52931,72],{"emptyLinePlaceholder":71},[54,52933,52934,52936,52938,52941],{"class":56,"line":887},[54,52935,22942],{"class":60},[54,52937,10056],{"class":81},[54,52939,52940],{"class":64}," email",[54,52942,7572],{"class":88},[54,52944,52945,52947,52949,52951,52953,52955,52958,52960],{"class":56,"line":892},[54,52946,41680],{"class":64},[54,52948,89],{"class":88},[54,52950,41685],{"class":92},[54,52952,96],{"class":88},[54,52954,545],{"class":544},[54,52956,52957],{"class":548},"Email is required",[54,52959,545],{"class":544},[54,52961,109],{"class":88},[54,52963,52964],{"class":56,"line":913},[54,52965,72],{"emptyLinePlaceholder":71},[54,52967,52968,52970,52972,52974,52977,52979,52981,52983],{"class":56,"line":2121},[54,52969,116],{"class":115},[54,52971,96],{"class":88},[54,52973,545],{"class":544},[54,52975,52976],{"class":548},"Email saved:",[54,52978,545],{"class":544},[54,52980,103],{"class":88},[54,52982,52940],{"class":92},[54,52984,109],{"class":88},[602,52986,52988],{"id":52987},"exit-when-a-file-does-not-exist","Exit when a file does not exist",[45,52990,52992],{"className":47,"code":52991,"language":49,"meta":50,"style":50},"import sys\nfrom pathlib import Path\n\npath = Path(\"config.json\")\n\nif not path.exists():\n    sys.exit(\"config.json does not exist\")\n\nprint(\"Config file found\")\n",[17,52993,52994,53000,53010,53014,53033,53037,53052,53071,53075],{"__ignoreMap":50},[54,52995,52996,52998],{"class":56,"line":57},[54,52997,61],{"class":60},[54,52999,41003],{"class":64},[54,53001,53002,53004,53006,53008],{"class":56,"line":68},[54,53003,1640],{"class":60},[54,53005,52066],{"class":64},[54,53007,61],{"class":60},[54,53009,52071],{"class":64},[54,53011,53012],{"class":56,"line":75},[54,53013,72],{"emptyLinePlaceholder":71},[54,53015,53016,53018,53020,53022,53024,53026,53029,53031],{"class":56,"line":112},[54,53017,27985],{"class":64},[54,53019,82],{"class":81},[54,53021,52084],{"class":92},[54,53023,96],{"class":88},[54,53025,545],{"class":544},[54,53027,53028],{"class":548},"config.json",[54,53030,545],{"class":544},[54,53032,109],{"class":88},[54,53034,53035],{"class":56,"line":887},[54,53036,72],{"emptyLinePlaceholder":71},[54,53038,53039,53041,53043,53046,53048,53050],{"class":56,"line":892},[54,53040,22942],{"class":60},[54,53042,10056],{"class":81},[54,53044,53045],{"class":64}," path",[54,53047,89],{"class":88},[54,53049,24757],{"class":92},[54,53051,38129],{"class":88},[54,53053,53054,53056,53058,53060,53062,53064,53067,53069],{"class":56,"line":913},[54,53055,41680],{"class":64},[54,53057,89],{"class":88},[54,53059,41685],{"class":92},[54,53061,96],{"class":88},[54,53063,545],{"class":544},[54,53065,53066],{"class":548},"config.json does not exist",[54,53068,545],{"class":544},[54,53070,109],{"class":88},[54,53072,53073],{"class":56,"line":2121},[54,53074,72],{"emptyLinePlaceholder":71},[54,53076,53077,53079,53081,53083,53086,53088],{"class":56,"line":2136},[54,53078,116],{"class":115},[54,53080,96],{"class":88},[54,53082,545],{"class":544},[54,53084,53085],{"class":548},"Config file found",[54,53087,545],{"class":544},[54,53089,109],{"class":88},[602,53091,53093],{"id":53092},"exit-after-printing-help-text","Exit after printing help text",[45,53095,53097],{"className":47,"code":53096,"language":49,"meta":50,"style":50},"import sys\n\nprint(\"Usage: python app.py \u003Cinput_file>\")\nsys.exit(0)\n",[17,53098,53099,53105,53109,53124],{"__ignoreMap":50},[54,53100,53101,53103],{"class":56,"line":57},[54,53102,61],{"class":60},[54,53104,41003],{"class":64},[54,53106,53107],{"class":56,"line":68},[54,53108,72],{"emptyLinePlaceholder":71},[54,53110,53111,53113,53115,53117,53120,53122],{"class":56,"line":75},[54,53112,116],{"class":115},[54,53114,96],{"class":88},[54,53116,545],{"class":544},[54,53118,53119],{"class":548},"Usage: python app.py \u003Cinput_file>",[54,53121,545],{"class":544},[54,53123,109],{"class":88},[54,53125,53126,53128,53130,53132,53134,53136],{"class":56,"line":112},[54,53127,40958],{"class":64},[54,53129,89],{"class":88},[54,53131,41685],{"class":92},[54,53133,96],{"class":88},[54,53135,752],{"class":99},[54,53137,109],{"class":88},[602,53139,53141,53142,51690],{"id":53140},"show-that-finally-still-runs","Show that ",[17,53143,51112],{},[45,53145,53147],{"className":47,"code":53146,"language":49,"meta":50,"style":50},"import sys\n\ntry:\n    print(\"Opening resource\")\n    sys.exit(1)\nfinally:\n    print(\"Closing resource\")\n",[17,53148,53149,53155,53159,53165,53180,53194,53200],{"__ignoreMap":50},[54,53150,53151,53153],{"class":56,"line":57},[54,53152,61],{"class":60},[54,53154,41003],{"class":64},[54,53156,53157],{"class":56,"line":68},[54,53158,72],{"emptyLinePlaceholder":71},[54,53160,53161,53163],{"class":56,"line":75},[54,53162,7452],{"class":60},[54,53164,7572],{"class":88},[54,53166,53167,53169,53171,53173,53176,53178],{"class":56,"line":112},[54,53168,7606],{"class":115},[54,53170,96],{"class":88},[54,53172,545],{"class":544},[54,53174,53175],{"class":548},"Opening resource",[54,53177,545],{"class":544},[54,53179,109],{"class":88},[54,53181,53182,53184,53186,53188,53190,53192],{"class":56,"line":887},[54,53183,41680],{"class":64},[54,53185,89],{"class":88},[54,53187,41685],{"class":92},[54,53189,96],{"class":88},[54,53191,100],{"class":99},[54,53193,109],{"class":88},[54,53195,53196,53198],{"class":56,"line":892},[54,53197,51112],{"class":60},[54,53199,7572],{"class":88},[54,53201,53202,53204,53206,53208,53211,53213],{"class":56,"line":913},[54,53203,7606],{"class":115},[54,53205,96],{"class":88},[54,53207,545],{"class":544},[54,53209,53210],{"class":548},"Closing resource",[54,53212,545],{"class":544},[54,53214,109],{"class":88},[602,53216,53218,53219,134,53221],{"id":53217},"show-the-difference-between-return-and-sysexit","Show the difference between ",[17,53220,52267],{},[17,53222,40974],{},[45,53224,53226],{"className":47,"code":53225,"language":49,"meta":50,"style":50},"import sys\n\ndef check_number(n):\n    if n \u003C 0:\n        return \"negative\"\n    return \"ok\"\n\nprint(check_number(-1))\nprint(\"Program still running\")\n\nsys.exit(\"Stopping now\")\nprint(\"This line never runs\")\n",[17,53227,53228,53234,53238,53252,53265,53276,53287,53291,53308,53323,53327,53346],{"__ignoreMap":50},[54,53229,53230,53232],{"class":56,"line":57},[54,53231,61],{"class":60},[54,53233,41003],{"class":64},[54,53235,53236],{"class":56,"line":68},[54,53237,72],{"emptyLinePlaceholder":71},[54,53239,53240,53242,53245,53247,53250],{"class":56,"line":75},[54,53241,52308],{"class":2230},[54,53243,53244],{"class":52311}," check_number",[54,53246,96],{"class":88},[54,53248,53249],{"class":10833},"n",[54,53251,10837],{"class":88},[54,53253,53254,53256,53259,53261,53263],{"class":56,"line":112},[54,53255,27239],{"class":60},[54,53257,53258],{"class":64}," n ",[54,53260,2227],{"class":81},[54,53262,3986],{"class":99},[54,53264,7572],{"class":88},[54,53266,53267,53269,53271,53274],{"class":56,"line":887},[54,53268,52410],{"class":60},[54,53270,830],{"class":544},[54,53272,53273],{"class":548},"negative",[54,53275,5239],{"class":544},[54,53277,53278,53280,53282,53285],{"class":56,"line":892},[54,53279,52360],{"class":60},[54,53281,830],{"class":544},[54,53283,53284],{"class":548},"ok",[54,53286,5239],{"class":544},[54,53288,53289],{"class":56,"line":913},[54,53290,72],{"emptyLinePlaceholder":71},[54,53292,53293,53295,53297,53300,53302,53304,53306],{"class":56,"line":2121},[54,53294,116],{"class":115},[54,53296,96],{"class":88},[54,53298,53299],{"class":92},"check_number",[54,53301,96],{"class":88},[54,53303,1467],{"class":81},[54,53305,100],{"class":99},[54,53307,382],{"class":88},[54,53309,53310,53312,53314,53316,53319,53321],{"class":56,"line":2136},[54,53311,116],{"class":115},[54,53313,96],{"class":88},[54,53315,545],{"class":544},[54,53317,53318],{"class":548},"Program still running",[54,53320,545],{"class":544},[54,53322,109],{"class":88},[54,53324,53325],{"class":56,"line":2151},[54,53326,72],{"emptyLinePlaceholder":71},[54,53328,53329,53331,53333,53335,53337,53339,53342,53344],{"class":56,"line":4442},[54,53330,40958],{"class":64},[54,53332,89],{"class":88},[54,53334,41685],{"class":92},[54,53336,96],{"class":88},[54,53338,545],{"class":544},[54,53340,53341],{"class":548},"Stopping now",[54,53343,545],{"class":544},[54,53345,109],{"class":88},[54,53347,53348,53350,53352,53354,53356,53358],{"class":56,"line":4468},[54,53349,116],{"class":115},[54,53351,96],{"class":88},[54,53353,545],{"class":544},[54,53355,51292],{"class":548},[54,53357,545],{"class":544},[54,53359,109],{"class":88},[40,53361,978],{"id":977},[14,53363,53364,53365,147],{},"Beginners often run into these problems when using ",[17,53366,40974],{},[149,53368,53369,53376,53385,53391,53396],{},[152,53370,53371,53372,1494,53374],{},"forgetting to import ",[17,53373,40958],{},[17,53375,40974],{},[152,53377,7373,53378,53381,53382,53384],{},[17,53379,53380],{},"sys.exit"," in a function when ",[17,53383,52267],{}," would be better",[152,53386,53387,53388,53390],{},"expecting ",[17,53389,40974],{}," to only leave a loop",[152,53392,53393,53394],{},"catching all exceptions and accidentally catching ",[17,53395,51108],{},[152,53397,53398],{},"passing a string and not realizing it signals an error exit",[602,53400,53402,53403],{"id":53401},"mistake-forgetting-to-import-sys","Mistake: forgetting to import ",[17,53404,40958],{},[45,53406,53408],{"className":47,"code":53407,"language":49,"meta":50,"style":50},"sys.exit()\n",[17,53409,53410],{"__ignoreMap":50},[54,53411,53412,53414,53416,53418],{"class":56,"line":57},[54,53413,40958],{"class":64},[54,53415,89],{"class":88},[54,53417,41685],{"class":92},[54,53419,1670],{"class":88},[14,53421,23179,53422,53424],{},[17,53423,40958],{}," is not defined.",[14,53426,1310],{},[45,53428,53430],{"className":47,"code":53429,"language":49,"meta":50,"style":50},"import sys\n\nsys.exit()\n",[17,53431,53432,53438,53442],{"__ignoreMap":50},[54,53433,53434,53436],{"class":56,"line":57},[54,53435,61],{"class":60},[54,53437,41003],{"class":64},[54,53439,53440],{"class":56,"line":68},[54,53441,72],{"emptyLinePlaceholder":71},[54,53443,53444,53446,53448,53450],{"class":56,"line":75},[54,53445,40958],{"class":64},[54,53447,89],{"class":88},[54,53449,41685],{"class":92},[54,53451,1670],{"class":88},[602,53453,53455],{"id":53454},"mistake-catching-too-broadly","Mistake: catching too broadly",[45,53457,53459],{"className":47,"code":53458,"language":49,"meta":50,"style":50},"import sys\n\ntry:\n    sys.exit(1)\nexcept:\n    print(\"Caught something\")\n",[17,53460,53461,53467,53471,53477,53491,53497],{"__ignoreMap":50},[54,53462,53463,53465],{"class":56,"line":57},[54,53464,61],{"class":60},[54,53466,41003],{"class":64},[54,53468,53469],{"class":56,"line":68},[54,53470,72],{"emptyLinePlaceholder":71},[54,53472,53473,53475],{"class":56,"line":75},[54,53474,7452],{"class":60},[54,53476,7572],{"class":88},[54,53478,53479,53481,53483,53485,53487,53489],{"class":56,"line":112},[54,53480,41680],{"class":64},[54,53482,89],{"class":88},[54,53484,41685],{"class":92},[54,53486,96],{"class":88},[54,53488,100],{"class":99},[54,53490,109],{"class":88},[54,53492,53493,53495],{"class":56,"line":887},[54,53494,7455],{"class":60},[54,53496,7572],{"class":88},[54,53498,53499,53501,53503,53505,53508,53510],{"class":56,"line":892},[54,53500,7606],{"class":115},[54,53502,96],{"class":88},[54,53504,545],{"class":544},[54,53506,53507],{"class":548},"Caught something",[54,53509,545],{"class":544},[54,53511,109],{"class":88},[14,53513,53514,53515,53518,53519,53521],{},"A broad ",[17,53516,53517],{},"except:"," can catch ",[17,53520,51108],{}," too, which may prevent the program from exiting as expected.",[14,53523,5605],{},[45,53525,53527],{"className":47,"code":53526,"language":49,"meta":50,"style":50},"try:\n    x = 10 \u002F 0\nexcept ZeroDivisionError:\n    print(\"Cannot divide by zero\")\n",[17,53528,53529,53535,53549,53558],{"__ignoreMap":50},[54,53530,53531,53533],{"class":56,"line":57},[54,53532,7452],{"class":60},[54,53534,7572],{"class":88},[54,53536,53537,53540,53542,53544,53546],{"class":56,"line":68},[54,53538,53539],{"class":64},"    x ",[54,53541,82],{"class":81},[54,53543,106],{"class":99},[54,53545,21991],{"class":81},[54,53547,53548],{"class":99}," 0\n",[54,53550,53551,53553,53556],{"class":56,"line":75},[54,53552,7455],{"class":60},[54,53554,53555],{"class":429}," ZeroDivisionError",[54,53557,7572],{"class":88},[54,53559,53560,53562,53564,53566,53569,53571],{"class":56,"line":112},[54,53561,7606],{"class":115},[54,53563,96],{"class":88},[54,53565,545],{"class":544},[54,53567,53568],{"class":548},"Cannot divide by zero",[54,53570,545],{"class":544},[54,53572,109],{"class":88},[14,53574,2246,53575,53577,53578,89],{},[17,53576,51108],{}," itself, see ",[192,53579,53581],{"href":53580},"\u002Ferrors\u002Fkeyboardinterrupt-exception-in-python-explained","SystemExit exception in Python explained",[40,53583,1419],{"id":1418},[602,53585,53587],{"id":53586},"does-sysexit-always-stop-the-program-immediately","Does sys.exit() always stop the program immediately?",[14,53589,53590,53591,53593,53594,53596],{},"Not exactly. It raises ",[17,53592,51108],{}," first, so ",[17,53595,51112],{}," blocks can still run.",[602,53598,53600],{"id":53599},"what-is-the-difference-between-sysexit-and-exit","What is the difference between sys.exit() and exit()?",[14,53602,53603,53605,53606,53609],{},[17,53604,40974],{}," is the standard choice in scripts. ",[17,53607,53608],{},"exit()"," is mainly for interactive use.",[602,53611,53613],{"id":53612},"what-does-sysexit0-mean","What does sys.exit(0) mean?",[14,53615,53616],{},"It usually means the program ended successfully.",[602,53618,53620],{"id":53619},"what-does-sysexit1-mean","What does sys.exit(1) mean?",[14,53622,53623],{},"It usually means the program ended because of an error or problem.",[602,53625,53627],{"id":53626},"can-i-use-sysexit-inside-a-function","Can I use sys.exit() inside a function?",[14,53629,53630,53631,53633],{},"Yes, but if you only want to leave the function, ",[17,53632,52267],{}," is usually better.",[40,53635,1510],{"id":1509},[149,53637,53638,53642,53647,53652,53656,53660],{},[152,53639,53640],{},[192,53641,49651],{"href":42317},[152,53643,53644],{},[192,53645,53646],{"href":52246},"How to exit a program in Python",[152,53648,53649],{},[192,53650,53651],{"href":51682},"Using try, except, else, and finally in Python",[152,53653,53654],{},[192,53655,53581],{"href":53580},[152,53657,53658],{},[192,53659,41148],{"href":41147},[152,53661,53662],{},[192,53663,51679],{"href":51678},[14,53665,53666],{},"If you are deciding between control-flow tools, the simple rule is:",[149,53668,53669,53674,53679,53682],{},[152,53670,7449,53671,53673],{},[17,53672,52267],{}," to leave a function",[152,53675,7449,53676,53678],{},[17,53677,52277],{}," to leave a loop",[152,53680,53681],{},"use exceptions for error handling",[152,53683,7449,53684,53686],{},[17,53685,40974],{}," when the whole script should stop",[1545,53688,53689],{},"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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .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 .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 .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":50,"searchDepth":68,"depth":68,"links":53691},[53692,53693,53694,53695,53702,53708,53713,53714,53715,53716,53717,53726,53731,53738],{"id":3595,"depth":68,"text":3596},{"id":51216,"depth":68,"text":51217},{"id":198,"depth":68,"text":199},{"id":51429,"depth":68,"text":51430,"children":53696},[53697,53699,53700,53701],{"id":51439,"depth":75,"text":53698},"sys.exit() or sys.exit(0)",{"id":51492,"depth":75,"text":41727},{"id":51544,"depth":75,"text":51547},{"id":51595,"depth":75,"text":51598},{"id":51638,"depth":68,"text":51639,"children":53703},[53704,53706],{"id":51686,"depth":75,"text":53705},"Example: finally still runs",{"id":51780,"depth":75,"text":53707},"Example: catching SystemExit",{"id":51922,"depth":68,"text":51923,"children":53709},[53710,53711,53712],{"id":51951,"depth":75,"text":51952},{"id":52047,"depth":75,"text":52048},{"id":52154,"depth":75,"text":52155},{"id":52250,"depth":68,"text":52251},{"id":52431,"depth":68,"text":52432},{"id":52643,"depth":68,"text":52644},{"id":52840,"depth":68,"text":52841},{"id":52882,"depth":68,"text":52883,"children":53718},[53719,53720,53721,53722,53724],{"id":52891,"depth":75,"text":52892},{"id":52987,"depth":75,"text":52988},{"id":53092,"depth":75,"text":53093},{"id":53140,"depth":75,"text":53723},"Show that finally still runs",{"id":53217,"depth":75,"text":53725},"Show the difference between return and sys.exit()",{"id":977,"depth":68,"text":978,"children":53727},[53728,53730],{"id":53401,"depth":75,"text":53729},"Mistake: forgetting to import sys",{"id":53454,"depth":75,"text":53455},{"id":1418,"depth":68,"text":1419,"children":53732},[53733,53734,53735,53736,53737],{"id":53586,"depth":75,"text":53587},{"id":53599,"depth":75,"text":53600},{"id":53612,"depth":75,"text":53613},{"id":53619,"depth":75,"text":53620},{"id":53626,"depth":75,"text":53627},{"id":1509,"depth":68,"text":1510},"Master sys.exit function explained in our comprehensive Python beginner guide.",{},{"title":51082,"description":53739},"standard-library\u002Fsys.exit-function-explained","-5D8JAhjWk2U9dmZPaB-FmP30AnsjMiMkePRhBBBz6g",{"id":53745,"title":53746,"body":53747,"description":55167,"extension":1577,"meta":55168,"navigation":71,"path":42272,"seo":55169,"stem":55170,"__hash__":55171},"content\u002Fstandard-library\u002Fsys.path-explained.md","sys.path Explained",{"type":7,"value":53748,"toc":55111},[53749,53752,53757,53770,53773,53792,53798,53801,53842,53845,53850,53862,53864,53884,53886,53938,53940,53990,53993,54002,54009,54017,54020,54037,54048,54055,54058,54070,54078,54081,54095,54098,54117,54120,54151,54154,54161,54167,54173,54190,54193,54209,54218,54232,54238,54246,54250,54279,54282,54286,54326,54329,54333,54379,54382,54388,54394,54398,54456,54459,54463,54523,54526,54530,54536,54539,54542,54549,54555,54558,54572,54575,54613,54616,54619,54625,54628,54642,54649,54655,54658,54664,54667,54671,54674,54678,54687,54690,54694,54697,54700,54726,54729,54756,54760,54763,54769,54809,54813,54841,54845,54874,54878,54909,54912,54915,54970,54977,54979,54984,55003,55005,55013,55018,55023,55026,55033,55036,55039,55043,55049,55054,55056,55059,55066,55069,55072,55074,55108],[10,53750,53746],{"id":53751},"syspath-explained",[14,53753,53754,53756],{},[17,53755,41134],{}," is one of the most useful things to understand when Python imports are confusing.",[14,53758,53759,53760,53763,53764,53766,53767,53769],{},"It tells Python ",[35,53761,53762],{},"where to look for modules and packages"," when you use ",[17,53765,61],{},". If an import fails, or Python imports the wrong file, ",[17,53768,41134],{}," often explains why.",[14,53771,53772],{},"This page shows:",[149,53774,53775,53780,53783,53786,53789],{},[152,53776,53777,53778,10053],{},"what ",[17,53779,41134],{},[152,53781,53782],{},"what it usually contains",[152,53784,53785],{},"how Python uses it during import",[152,53787,53788],{},"how to inspect it safely",[152,53790,53791],{},"when changing it helps, and when it causes more problems",[40,53793,53795,53796],{"id":53794},"quick-way-to-inspect-syspath","Quick way to inspect ",[17,53797,41134],{},[14,53799,53800],{},"Use this to see where Python is looking for modules:",[45,53802,53804],{"className":47,"code":53803,"language":49,"meta":50,"style":50},"import sys\n\nfor folder in sys.path:\n    print(folder)\n",[17,53805,53806,53812,53816,53832],{"__ignoreMap":50},[54,53807,53808,53810],{"class":56,"line":57},[54,53809,61],{"class":60},[54,53811,41003],{"class":64},[54,53813,53814],{"class":56,"line":68},[54,53815,72],{"emptyLinePlaceholder":71},[54,53817,53818,53820,53822,53824,53826,53828,53830],{"class":56,"line":75},[54,53819,26859],{"class":60},[54,53821,30521],{"class":64},[54,53823,26865],{"class":60},[54,53825,41252],{"class":64},[54,53827,89],{"class":88},[54,53829,23896],{"class":1693},[54,53831,7572],{"class":88},[54,53833,53834,53836,53838,53840],{"class":56,"line":112},[54,53835,7606],{"class":115},[54,53837,96],{"class":88},[54,53839,24762],{"class":92},[54,53841,109],{"class":88},[14,53843,53844],{},"This is helpful when an import does not work as expected.",[40,53846,1736,53848,10053],{"id":53847},"what-syspath-is",[17,53849,41134],{},[14,53851,53852,4144,53854,53857,53858,89],{},[17,53853,41134],{},[35,53855,53856],{},"list of folder paths"," stored in the ",[192,53859,53860,160],{"href":42317},[17,53861,40958],{},[14,53863,4136],{},[149,53865,53866,53871,53876,53879],{},[152,53867,53868,53870],{},[17,53869,41134],{}," is a list",[152,53872,53873,53874],{},"Python checks these folders when you use ",[17,53875,61],{},[152,53877,53878],{},"Each item is usually a string containing a path",[152,53880,53881,53882,160],{},"It belongs to the ",[17,53883,40958],{},[14,53885,253],{},[45,53887,53889],{"className":47,"code":53888,"language":49,"meta":50,"style":50},"import sys\n\nprint(type(sys.path))\nprint(sys.path[:3])\n",[17,53890,53891,53897,53901,53919],{"__ignoreMap":50},[54,53892,53893,53895],{"class":56,"line":57},[54,53894,61],{"class":60},[54,53896,41003],{"class":64},[54,53898,53899],{"class":56,"line":68},[54,53900,72],{"emptyLinePlaceholder":71},[54,53902,53903,53905,53907,53909,53911,53913,53915,53917],{"class":56,"line":75},[54,53904,116],{"class":115},[54,53906,96],{"class":88},[54,53908,2208],{"class":429},[54,53910,96],{"class":88},[54,53912,40958],{"class":92},[54,53914,89],{"class":88},[54,53916,23896],{"class":1693},[54,53918,382],{"class":88},[54,53920,53921,53923,53925,53927,53929,53931,53934,53936],{"class":56,"line":112},[54,53922,116],{"class":115},[54,53924,96],{"class":88},[54,53926,40958],{"class":92},[54,53928,89],{"class":88},[54,53930,23896],{"class":1693},[54,53932,53933],{"class":88},"[:",[54,53935,401],{"class":99},[54,53937,937],{"class":88},[14,53939,305],{},[45,53941,53943],{"className":47,"code":53942,"language":49,"meta":50,"style":50},"\u003Cclass 'list'>\n['\u002Fmy-project', '\u002Fusr\u002Flib\u002Fpython3.12', '\u002Fusr\u002Flib\u002Fpython3.12\u002Fsite-packages']\n",[17,53944,53945,53959],{"__ignoreMap":50},[54,53946,53947,53949,53951,53953,53955,53957],{"class":56,"line":57},[54,53948,2227],{"class":81},[54,53950,2231],{"class":2230},[54,53952,2234],{"class":544},[54,53954,430],{"class":548},[54,53956,2240],{"class":544},[54,53958,2243],{"class":81},[54,53960,53961,53963,53965,53968,53970,53972,53974,53977,53979,53981,53983,53986,53988],{"class":56,"line":68},[54,53962,459],{"class":88},[54,53964,2240],{"class":544},[54,53966,53967],{"class":548},"\u002Fmy-project",[54,53969,2240],{"class":544},[54,53971,103],{"class":88},[54,53973,2234],{"class":544},[54,53975,53976],{"class":548},"\u002Fusr\u002Flib\u002Fpython3.12",[54,53978,2240],{"class":544},[54,53980,103],{"class":88},[54,53982,2234],{"class":544},[54,53984,53985],{"class":548},"\u002Fusr\u002Flib\u002Fpython3.12\u002Fsite-packages",[54,53987,2240],{"class":544},[54,53989,469],{"class":88},[14,53991,53992],{},"This means Python will search those locations for modules.",[14,53994,53995,53996,134,53998,89],{},"If you are new to imports, see ",[192,53997,41866],{"href":41865},[192,53999,54001],{"href":54000},"\u002Flearn\u002Fpython-modules-explained","Python modules explained",[40,54003,54005,54006,54008],{"id":54004},"why-syspath-matters","Why ",[17,54007,41134],{}," matters",[14,54010,54011,54013,54014,89],{},[17,54012,41134],{}," matters because it explains ",[35,54015,54016],{},"where Python searches for modules",[14,54018,54019],{},"That helps you understand problems like these:",[149,54021,54022,54025,54031,54034],{},[152,54023,54024],{},"one file imports correctly, but another does not",[152,54026,54027,54028],{},"you get ",[17,54029,54030],{},"ModuleNotFoundError",[152,54032,54033],{},"Python imports the wrong file",[152,54035,54036],{},"code works on one computer but not another",[14,54038,54039,54040,54042,54043,54047],{},"If you are fixing an import error, ",[17,54041,41134],{}," is often one of the first things to check. It is especially useful for debugging ",[192,54044,54045],{"href":41874},[17,54046,54030],{}," and other import problems.",[40,54049,54051,54052,54054],{"id":54050},"how-python-uses-syspath-during-import","How Python uses ",[17,54053,41134],{}," during import",[14,54056,54057],{},"When you write an import like this:",[45,54059,54061],{"className":47,"code":54060,"language":49,"meta":50,"style":50},"import mymodule\n",[17,54062,54063],{"__ignoreMap":50},[54,54064,54065,54067],{"class":56,"line":57},[54,54066,61],{"class":60},[54,54068,54069],{"class":64}," mymodule\n",[14,54071,54072,54073,39223,54075,89],{},"Python checks the folders in ",[17,54074,41134],{},[35,54076,54077],{},"from first to last",[14,54079,54080],{},"Important rules:",[149,54082,54083,54086,54089,54092],{},[152,54084,54085],{},"Python searches in order",[152,54087,54088],{},"earlier paths have higher priority",[152,54090,54091],{},"if two modules have the same name, the first match is used",[152,54093,54094],{},"the search order affects which file gets imported",[14,54096,54097],{},"Example idea:",[149,54099,54100,54106,54111],{},[152,54101,54102,54103],{},"Folder A contains ",[17,54104,54105],{},"mymodule.py",[152,54107,54108,54109],{},"Folder B also contains ",[17,54110,54105],{},[152,54112,54113,54114,54116],{},"If Folder A appears earlier in ",[17,54115,41134],{},", Python imports that one",[14,54118,54119],{},"You can verify which file was imported like this:",[45,54121,54123],{"className":47,"code":54122,"language":49,"meta":50,"style":50},"import mymodule\n\nprint(mymodule.__file__)\n",[17,54124,54125,54131,54135],{"__ignoreMap":50},[54,54126,54127,54129],{"class":56,"line":57},[54,54128,61],{"class":60},[54,54130,54069],{"class":64},[54,54132,54133],{"class":56,"line":68},[54,54134,72],{"emptyLinePlaceholder":71},[54,54136,54137,54139,54141,54144,54146,54149],{"class":56,"line":75},[54,54138,116],{"class":115},[54,54140,96],{"class":88},[54,54142,54143],{"class":92},"mymodule",[54,54145,89],{"class":88},[54,54147,54148],{"class":4870},"__file__",[54,54150,109],{"class":88},[14,54152,54153],{},"This is a very useful check when Python seems to import the wrong module.",[14,54155,54156,54157,89],{},"For a beginner-friendly import overview, see ",[192,54158,54160],{"href":54159},"\u002Fhow-to\u002Fhow-to-import-a-module-in-python","how to import a module in Python",[40,54162,54164,54165],{"id":54163},"what-you-usually-find-inside-syspath","What you usually find inside ",[17,54166,41134],{},[14,54168,54169,54170,54172],{},"The exact contents are different on each computer, but ",[17,54171,41134],{}," often includes:",[149,54174,54175,54178,54181,54187],{},[152,54176,54177],{},"the current script folder or working location",[152,54179,54180],{},"standard library folders",[152,54182,54183,54186],{},[17,54184,54185],{},"site-packages"," folders for installed packages",[152,54188,54189],{},"extra paths added by environment variables, tools, or project settings",[14,54191,54192],{},"For example, you might see paths like:",[149,54194,54195,54198,54201,54204],{},[152,54196,54197],{},"your project folder",[152,54199,54200],{},"a Python installation folder",[152,54202,54203],{},"a virtual environment folder",[152,54205,50168,54206,54208],{},[17,54207,54185],{}," folder",[14,54210,54211,54212,337,54214,54217],{},"Because of this, ",[17,54213,41134],{},[35,54215,54216],{},"not the same on every computer",". It depends on:",[149,54219,54220,54223,54226,54229],{},[152,54221,54222],{},"your operating system",[152,54224,54225],{},"your Python version",[152,54227,54228],{},"your virtual environment",[152,54230,54231],{},"how you run the script",[40,54233,54235,54236],{"id":54234},"how-to-view-syspath","How to view ",[17,54237,41134],{},[14,54239,54240,54241,54243,54244,89],{},"First import ",[17,54242,40958],{},", then print ",[17,54245,41134],{},[602,54247,54249],{"id":54248},"print-the-whole-list","Print the whole list",[45,54251,54253],{"className":47,"code":54252,"language":49,"meta":50,"style":50},"import sys\n\nprint(sys.path)\n",[17,54254,54255,54261,54265],{"__ignoreMap":50},[54,54256,54257,54259],{"class":56,"line":57},[54,54258,61],{"class":60},[54,54260,41003],{"class":64},[54,54262,54263],{"class":56,"line":68},[54,54264,72],{"emptyLinePlaceholder":71},[54,54266,54267,54269,54271,54273,54275,54277],{"class":56,"line":75},[54,54268,116],{"class":115},[54,54270,96],{"class":88},[54,54272,40958],{"class":92},[54,54274,89],{"class":88},[54,54276,23896],{"class":1693},[54,54278,109],{"class":88},[14,54280,54281],{},"This works, but the output can be hard to read.",[602,54283,54285],{"id":54284},"print-one-path-per-line","Print one path per line",[45,54287,54288],{"className":47,"code":41796,"language":49,"meta":50,"style":50},[17,54289,54290,54296,54300,54316],{"__ignoreMap":50},[54,54291,54292,54294],{"class":56,"line":57},[54,54293,61],{"class":60},[54,54295,41003],{"class":64},[54,54297,54298],{"class":56,"line":68},[54,54299,72],{"emptyLinePlaceholder":71},[54,54301,54302,54304,54306,54308,54310,54312,54314],{"class":56,"line":75},[54,54303,26859],{"class":60},[54,54305,41815],{"class":64},[54,54307,26865],{"class":60},[54,54309,41252],{"class":64},[54,54311,89],{"class":88},[54,54313,23896],{"class":1693},[54,54315,7572],{"class":88},[54,54317,54318,54320,54322,54324],{"class":56,"line":112},[54,54319,7606],{"class":115},[54,54321,96],{"class":88},[54,54323,23896],{"class":92},[54,54325,109],{"class":88},[14,54327,54328],{},"This is usually easier to inspect.",[602,54330,54332],{"id":54331},"check-whether-a-folder-is-included","Check whether a folder is included",[45,54334,54336],{"className":47,"code":54335,"language":49,"meta":50,"style":50},"import sys\n\ntarget = \"\u002Fmy-project\"\nprint(target in sys.path)\n",[17,54337,54338,54344,54348,54361],{"__ignoreMap":50},[54,54339,54340,54342],{"class":56,"line":57},[54,54341,61],{"class":60},[54,54343,41003],{"class":64},[54,54345,54346],{"class":56,"line":68},[54,54347,72],{"emptyLinePlaceholder":71},[54,54349,54350,54353,54355,54357,54359],{"class":56,"line":75},[54,54351,54352],{"class":64},"target ",[54,54354,82],{"class":81},[54,54356,830],{"class":544},[54,54358,53967],{"class":548},[54,54360,5239],{"class":544},[54,54362,54363,54365,54367,54369,54371,54373,54375,54377],{"class":56,"line":112},[54,54364,116],{"class":115},[54,54366,96],{"class":88},[54,54368,54352],{"class":92},[54,54370,26865],{"class":60},[54,54372,41252],{"class":92},[54,54374,89],{"class":88},[54,54376,23896],{"class":1693},[54,54378,109],{"class":88},[14,54380,54381],{},"If the folder you expect is missing, Python may not be able to import your module from that location.",[40,54383,54385,54386],{"id":54384},"temporary-changes-to-syspath","Temporary changes to ",[17,54387,41134],{},[14,54389,54390,54391,54393],{},"You can change ",[17,54392,41134],{}," while your program is running.",[602,54395,54397],{"id":54396},"add-a-path-to-the-end","Add a path to the end",[45,54399,54401],{"className":47,"code":54400,"language":49,"meta":50,"style":50},"import sys\n\nsys.path.append(\"\u002Fmy-project\")\nprint(sys.path[-1])\n",[17,54402,54403,54409,54413,54436],{"__ignoreMap":50},[54,54404,54405,54407],{"class":56,"line":57},[54,54406,61],{"class":60},[54,54408,41003],{"class":64},[54,54410,54411],{"class":56,"line":68},[54,54412,72],{"emptyLinePlaceholder":71},[54,54414,54415,54417,54419,54421,54423,54426,54428,54430,54432,54434],{"class":56,"line":75},[54,54416,40958],{"class":64},[54,54418,89],{"class":88},[54,54420,23896],{"class":1693},[54,54422,89],{"class":88},[54,54424,54425],{"class":92},"append",[54,54427,96],{"class":88},[54,54429,545],{"class":544},[54,54431,53967],{"class":548},[54,54433,545],{"class":544},[54,54435,109],{"class":88},[54,54437,54438,54440,54442,54444,54446,54448,54450,54452,54454],{"class":56,"line":112},[54,54439,116],{"class":115},[54,54441,96],{"class":88},[54,54443,40958],{"class":92},[54,54445,89],{"class":88},[54,54447,23896],{"class":1693},[54,54449,459],{"class":88},[54,54451,1467],{"class":81},[54,54453,100],{"class":99},[54,54455,937],{"class":88},[14,54457,54458],{},"This adds the folder at the end of the search list.",[602,54460,54462],{"id":54461},"add-a-path-to-the-beginning","Add a path to the beginning",[45,54464,54466],{"className":47,"code":54465,"language":49,"meta":50,"style":50},"import sys\n\nsys.path.insert(0, \"\u002Fmy-project\")\nprint(sys.path[0])\n",[17,54467,54468,54474,54478,54505],{"__ignoreMap":50},[54,54469,54470,54472],{"class":56,"line":57},[54,54471,61],{"class":60},[54,54473,41003],{"class":64},[54,54475,54476],{"class":56,"line":68},[54,54477,72],{"emptyLinePlaceholder":71},[54,54479,54480,54482,54484,54486,54488,54491,54493,54495,54497,54499,54501,54503],{"class":56,"line":75},[54,54481,40958],{"class":64},[54,54483,89],{"class":88},[54,54485,23896],{"class":1693},[54,54487,89],{"class":88},[54,54489,54490],{"class":92},"insert",[54,54492,96],{"class":88},[54,54494,752],{"class":99},[54,54496,103],{"class":88},[54,54498,830],{"class":544},[54,54500,53967],{"class":548},[54,54502,545],{"class":544},[54,54504,109],{"class":88},[54,54506,54507,54509,54511,54513,54515,54517,54519,54521],{"class":56,"line":112},[54,54508,116],{"class":115},[54,54510,96],{"class":88},[54,54512,40958],{"class":92},[54,54514,89],{"class":88},[54,54516,23896],{"class":1693},[54,54518,459],{"class":88},[54,54520,752],{"class":99},[54,54522,937],{"class":88},[14,54524,54525],{},"This gives that folder higher priority during import.",[602,54527,54529],{"id":54528},"important-note","Important note",[14,54531,54532,54533,89],{},"These changes are ",[35,54534,54535],{},"temporary",[14,54537,54538],{},"They only last while the program runs. When the script ends, the changes are gone.",[14,54540,54541],{},"This can be useful for quick testing, but it is usually not the best long-term solution.",[40,54543,54545,54546,54548],{"id":54544},"when-changing-syspath-is-a-bad-idea","When changing ",[17,54547,41134],{}," is a bad idea",[14,54550,54551,54552,54554],{},"Editing ",[17,54553,41134],{}," can solve a problem quickly, but it often creates new ones.",[14,54556,54557],{},"Common downsides:",[149,54559,54560,54563,54566,54569],{},[152,54561,54562],{},"it can hide project structure problems",[152,54564,54565],{},"it can make code harder to understand",[152,54567,54568],{},"it may behave differently on another computer",[152,54570,54571],{},"it can cause the wrong module to be imported",[14,54573,54574],{},"For example, this kind of code may work on your machine:",[45,54576,54578],{"className":47,"code":54577,"language":49,"meta":50,"style":50},"import sys\nsys.path.insert(0, \"\u002FUsers\u002Falex\u002FDesktop\u002Fproject\")\n",[17,54579,54580,54586],{"__ignoreMap":50},[54,54581,54582,54584],{"class":56,"line":57},[54,54583,61],{"class":60},[54,54585,41003],{"class":64},[54,54587,54588,54590,54592,54594,54596,54598,54600,54602,54604,54606,54609,54611],{"class":56,"line":68},[54,54589,40958],{"class":64},[54,54591,89],{"class":88},[54,54593,23896],{"class":1693},[54,54595,89],{"class":88},[54,54597,54490],{"class":92},[54,54599,96],{"class":88},[54,54601,752],{"class":99},[54,54603,103],{"class":88},[54,54605,830],{"class":544},[54,54607,54608],{"class":548},"\u002FUsers\u002Falex\u002FDesktop\u002Fproject",[54,54610,545],{"class":544},[54,54612,109],{"class":88},[14,54614,54615],{},"But it may fail on someone else's computer because that folder does not exist there.",[14,54617,54618],{},"For beginners, it is usually better to fix the root cause instead of adding paths manually.",[40,54620,54622,54623],{"id":54621},"better-fixes-than-editing-syspath","Better fixes than editing ",[17,54624,41134],{},[14,54626,54627],{},"In many cases, these are better solutions:",[149,54629,54630,54633,54636,54639],{},[152,54631,54632],{},"run the script from the correct project folder",[152,54634,54635],{},"use a proper package structure",[152,54637,54638],{},"use virtual environments for installed packages",[152,54640,54641],{},"check that the module name and file name are correct",[14,54643,54644,54645,89],{},"If a package is installed in the wrong environment, creating and using a virtual environment is often the real fix. See ",[192,54646,54648],{"href":54647},"\u002Fhow-to\u002Fhow-to-create-and-use-a-virtual-environment-in-python","how to create and use a virtual environment in Python",[40,54650,54652,54653],{"id":54651},"common-import-problems-linked-to-syspath","Common import problems linked to ",[17,54654,41134],{},[14,54656,54657],{},"Here are common reasons imports fail or behave strangely.",[602,54659,54661,54662],{"id":54660},"the-needed-folder-is-not-in-syspath","The needed folder is not in ",[17,54663,41134],{},[14,54665,54666],{},"If Python never searches the folder that contains your module, the import will fail.",[602,54668,54670],{"id":54669},"you-are-running-python-from-a-different-folder-than-expected","You are running Python from a different folder than expected",[14,54672,54673],{},"The folder you run from can affect imports. A script may work in one location and fail in another.",[602,54675,54677],{"id":54676},"a-local-file-has-the-same-name-as-a-standard-library-or-installed-module","A local file has the same name as a standard library or installed module",[14,54679,54680,54681,54684,54685,15765],{},"For example, if you create a file named ",[17,54682,54683],{},"random.py",", it may interfere with Python's standard ",[17,54686,26],{},[14,54688,54689],{},"This can make Python import your file instead of the one you expected.",[602,54691,54693],{"id":54692},"the-package-is-installed-in-a-different-python-environment","The package is installed in a different Python environment",[14,54695,54696],{},"You may have installed a package in one interpreter, but be running another interpreter.",[14,54698,54699],{},"That is why these two checks are so useful:",[45,54701,54703],{"className":47,"code":54702,"language":49,"meta":50,"style":50},"import sys\nprint(sys.executable)\n",[17,54704,54705,54711],{"__ignoreMap":50},[54,54706,54707,54709],{"class":56,"line":57},[54,54708,61],{"class":60},[54,54710,41003],{"class":64},[54,54712,54713,54715,54717,54719,54721,54724],{"class":56,"line":68},[54,54714,116],{"class":115},[54,54716,96],{"class":88},[54,54718,40958],{"class":92},[54,54720,89],{"class":88},[54,54722,54723],{"class":1693},"executable",[54,54725,109],{"class":88},[14,54727,54728],{},"and:",[45,54730,54732],{"className":47,"code":54731,"language":49,"meta":50,"style":50},"import some_module\nprint(some_module.__file__)\n",[17,54733,54734,54741],{"__ignoreMap":50},[54,54735,54736,54738],{"class":56,"line":57},[54,54737,61],{"class":60},[54,54739,54740],{"class":64}," some_module\n",[54,54742,54743,54745,54747,54750,54752,54754],{"class":56,"line":68},[54,54744,116],{"class":115},[54,54746,96],{"class":88},[54,54748,54749],{"class":92},"some_module",[54,54751,89],{"class":88},[54,54753,54148],{"class":4870},[54,54755,109],{"class":88},[40,54757,54759],{"id":54758},"simple-debugging-checklist","Simple debugging checklist",[14,54761,54762],{},"When imports are not working, try these steps.",[602,54764,54766,54767],{"id":54765},"_1-print-syspath","1. Print ",[17,54768,41134],{},[45,54770,54771],{"className":47,"code":41796,"language":49,"meta":50,"style":50},[17,54772,54773,54779,54783,54799],{"__ignoreMap":50},[54,54774,54775,54777],{"class":56,"line":57},[54,54776,61],{"class":60},[54,54778,41003],{"class":64},[54,54780,54781],{"class":56,"line":68},[54,54782,72],{"emptyLinePlaceholder":71},[54,54784,54785,54787,54789,54791,54793,54795,54797],{"class":56,"line":75},[54,54786,26859],{"class":60},[54,54788,41815],{"class":64},[54,54790,26865],{"class":60},[54,54792,41252],{"class":64},[54,54794,89],{"class":88},[54,54796,23896],{"class":1693},[54,54798,7572],{"class":88},[54,54800,54801,54803,54805,54807],{"class":56,"line":112},[54,54802,7606],{"class":115},[54,54804,96],{"class":88},[54,54806,23896],{"class":92},[54,54808,109],{"class":88},[602,54810,54812],{"id":54811},"_2-print-the-current-working-directory","2. Print the current working directory",[45,54814,54815],{"className":47,"code":38498,"language":49,"meta":50,"style":50},[17,54816,54817,54823,54827],{"__ignoreMap":50},[54,54818,54819,54821],{"class":56,"line":57},[54,54820,61],{"class":60},[54,54822,23707],{"class":64},[54,54824,54825],{"class":56,"line":68},[54,54826,72],{"emptyLinePlaceholder":71},[54,54828,54829,54831,54833,54835,54837,54839],{"class":56,"line":75},[54,54830,116],{"class":115},[54,54832,96],{"class":88},[54,54834,23720],{"class":92},[54,54836,89],{"class":88},[54,54838,23725],{"class":92},[54,54840,1950],{"class":88},[602,54842,54844],{"id":54843},"_3-check-which-python-interpreter-is-running","3. Check which Python interpreter is running",[45,54846,54848],{"className":47,"code":54847,"language":49,"meta":50,"style":50},"import sys\n\nprint(sys.executable)\n",[17,54849,54850,54856,54860],{"__ignoreMap":50},[54,54851,54852,54854],{"class":56,"line":57},[54,54853,61],{"class":60},[54,54855,41003],{"class":64},[54,54857,54858],{"class":56,"line":68},[54,54859,72],{"emptyLinePlaceholder":71},[54,54861,54862,54864,54866,54868,54870,54872],{"class":56,"line":75},[54,54863,116],{"class":115},[54,54865,96],{"class":88},[54,54867,40958],{"class":92},[54,54869,89],{"class":88},[54,54871,54723],{"class":1693},[54,54873,109],{"class":88},[602,54875,54877],{"id":54876},"_4-check-the-exact-module-file-being-imported","4. Check the exact module file being imported",[45,54879,54881],{"className":47,"code":54880,"language":49,"meta":50,"style":50},"import module_name\n\nprint(module_name.__file__)\n",[17,54882,54883,54890,54894],{"__ignoreMap":50},[54,54884,54885,54887],{"class":56,"line":57},[54,54886,61],{"class":60},[54,54888,54889],{"class":64}," module_name\n",[54,54891,54892],{"class":56,"line":68},[54,54893,72],{"emptyLinePlaceholder":71},[54,54895,54896,54898,54900,54903,54905,54907],{"class":56,"line":75},[54,54897,116],{"class":115},[54,54899,96],{"class":88},[54,54901,54902],{"class":92},"module_name",[54,54904,89],{"class":88},[54,54906,54148],{"class":4870},[54,54908,109],{"class":88},[14,54910,54911],{},"This is often enough to find the real problem.",[14,54913,54914],{},"You can also run these checks directly from the command line:",[45,54916,54918],{"className":15497,"code":54917,"language":15499,"meta":50,"style":50},"python -c \"import sys; print(sys.path)\"\npython -c \"import os; print(os.getcwd())\"\npython -c \"import sys; print(sys.executable)\"\npython -c \"import module_name; print(module_name.__file__)\"\n",[17,54919,54920,54932,54944,54957],{"__ignoreMap":50},[54,54921,54922,54924,54926,54928,54930],{"class":56,"line":57},[54,54923,49],{"class":10811},[54,54925,26050],{"class":24323},[54,54927,830],{"class":544},[54,54929,42214],{"class":548},[54,54931,5239],{"class":544},[54,54933,54934,54936,54938,54940,54942],{"class":56,"line":68},[54,54935,49],{"class":10811},[54,54937,26050],{"class":24323},[54,54939,830],{"class":544},[54,54941,26055],{"class":548},[54,54943,5239],{"class":544},[54,54945,54946,54948,54950,54952,54955],{"class":56,"line":75},[54,54947,49],{"class":10811},[54,54949,26050],{"class":24323},[54,54951,830],{"class":544},[54,54953,54954],{"class":548},"import sys; print(sys.executable)",[54,54956,5239],{"class":544},[54,54958,54959,54961,54963,54965,54968],{"class":56,"line":112},[54,54960,49],{"class":10811},[54,54962,26050],{"class":24323},[54,54964,830],{"class":544},[54,54966,54967],{"class":548},"import module_name; print(module_name.__file__)",[54,54969,5239],{"class":544},[14,54971,54972,54973,89],{},"If you want a broader troubleshooting process, see ",[192,54974,54976],{"href":54975},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide","how to debug Python code",[40,54978,978],{"id":977},[14,54980,54981,54982,147],{},"These problems are often connected to ",[17,54983,41134],{},[149,54985,54986,54989,54992,54995,54998],{},[152,54987,54988],{},"running a script from the wrong folder",[152,54990,54991],{},"using a different Python interpreter than the one where the package was installed",[152,54993,54994],{},"creating a file with the same name as a module you want to import",[152,54996,54997],{},"adding paths manually and forgetting that import order matters",[152,54999,53387,55000,55002],{},[17,55001,41134],{}," changes to remain after the script ends",[40,55004,1419],{"id":1418},[602,55006,8075,55008,134,55010,3409],{"id":55007},"what-is-the-difference-between-syspath-and-path",[17,55009,41134],{},[17,55011,55012],{},"PATH",[14,55014,55015,55017],{},[17,55016,41134],{}," is used by Python to find modules.",[14,55019,55020,55022],{},[17,55021,55012],{}," is an operating system environment variable used to find programs and commands.",[14,55024,55025],{},"They are different things.",[602,55027,55029,55030,55032],{"id":55028},"does-changing-syspath-install-a-package","Does changing ",[17,55031,41134],{}," install a package?",[14,55034,55035],{},"No.",[14,55037,55038],{},"It only changes where Python looks for modules during that run.",[602,55040,55042],{"id":55041},"why-is-python-importing-the-wrong-file","Why is Python importing the wrong file?",[14,55044,55045,55046,55048],{},"A folder earlier in ",[17,55047,41134],{}," may contain a module with the same name, so Python uses that one first.",[602,55050,3449,55052,3452],{"id":55051},"is-syspath-the-same-on-every-computer",[17,55053,41134],{},[14,55055,55035],{},[14,55057,55058],{},"It depends on the Python version, operating system, environment, and project setup.",[602,55060,55062,55063,55065],{"id":55061},"should-beginners-edit-syspath-often","Should beginners edit ",[17,55064,41134],{}," often?",[14,55067,55068],{},"Usually no.",[14,55070,55071],{},"It is better to fix the project structure or environment when possible.",[40,55073,1510],{"id":1509},[149,55075,55076,55080,55084,55089,55094,55099,55104],{},[152,55077,55078],{},[192,55079,42278],{"href":41865},[152,55081,55082],{},[192,55083,49651],{"href":42317},[152,55085,55086],{},[192,55087,55088],{"href":41874},"ModuleNotFoundError: No module named X",[152,55090,55091],{},[192,55092,55093],{"href":41880},"ImportError: No module named X",[152,55095,55096],{},[192,55097,55098],{"href":54647},"How to create and use a virtual environment in Python",[152,55100,55101],{},[192,55102,55103],{"href":54975},"How to debug Python code",[152,55105,55106],{},[192,55107,41148],{"href":41147},[1545,55109,55110],{},"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 .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":55112},[55113,55115,55117,55119,55121,55123,55129,55135,55137,55139,55147,55154,55155,55166],{"id":53794,"depth":68,"text":55114},"Quick way to inspect sys.path",{"id":53847,"depth":68,"text":55116},"What sys.path is",{"id":54004,"depth":68,"text":55118},"Why sys.path matters",{"id":54050,"depth":68,"text":55120},"How Python uses sys.path during import",{"id":54163,"depth":68,"text":55122},"What you usually find inside sys.path",{"id":54234,"depth":68,"text":55124,"children":55125},"How to view sys.path",[55126,55127,55128],{"id":54248,"depth":75,"text":54249},{"id":54284,"depth":75,"text":54285},{"id":54331,"depth":75,"text":54332},{"id":54384,"depth":68,"text":55130,"children":55131},"Temporary changes to sys.path",[55132,55133,55134],{"id":54396,"depth":75,"text":54397},{"id":54461,"depth":75,"text":54462},{"id":54528,"depth":75,"text":54529},{"id":54544,"depth":68,"text":55136},"When changing sys.path is a bad idea",{"id":54621,"depth":68,"text":55138},"Better fixes than editing sys.path",{"id":54651,"depth":68,"text":55140,"children":55141},"Common import problems linked to sys.path",[55142,55144,55145,55146],{"id":54660,"depth":75,"text":55143},"The needed folder is not in sys.path",{"id":54669,"depth":75,"text":54670},{"id":54676,"depth":75,"text":54677},{"id":54692,"depth":75,"text":54693},{"id":54758,"depth":68,"text":54759,"children":55148},[55149,55151,55152,55153],{"id":54765,"depth":75,"text":55150},"1. Print sys.path",{"id":54811,"depth":75,"text":54812},{"id":54843,"depth":75,"text":54844},{"id":54876,"depth":75,"text":54877},{"id":977,"depth":68,"text":978},{"id":1418,"depth":68,"text":1419,"children":55156},[55157,55159,55161,55162,55164],{"id":55007,"depth":75,"text":55158},"What is the difference between sys.path and PATH?",{"id":55028,"depth":75,"text":55160},"Does changing sys.path install a package?",{"id":55041,"depth":75,"text":55042},{"id":55051,"depth":75,"text":55163},"Is sys.path the same on every computer?",{"id":55061,"depth":75,"text":55165},"Should beginners edit sys.path often?",{"id":1509,"depth":68,"text":1510},"Master sys.path explained in our comprehensive Python beginner guide.",{},{"title":53746,"description":55167},"standard-library\u002Fsys.path-explained","CK3U6xoEpBJb28h77IKDxcsxBxhuoy-YVGUmF9qmgWw",{"id":55173,"title":55174,"body":55175,"description":56404,"extension":1577,"meta":56405,"navigation":71,"path":42546,"seo":56406,"stem":56407,"__hash__":56408},"content\u002Fstandard-library\u002Ftime.sleep-function-explained.md","time.sleep() Function Explained",{"type":7,"value":55176,"toc":56375},[55177,55180,55185,55188,55202,55208,55210,55268,55278,55282,55287,55289,55312,55317,55319,55321,55340,55345,55347,55361,55364,55408,55410,55413,55472,55475,55481,55489,55493,55498,55500,55511,55513,55596,55599,55616,55623,55627,55632,55636,55639,55683,55687,55690,55734,55738,55743,55787,55791,55794,55797,55828,55830,55835,55837,55841,55860,55866,55868,55896,55901,55903,55907,55939,55945,55947,55975,55978,56046,56050,56055,56080,56086,56114,56117,56162,56170,56174,56179,56182,56192,56197,56199,56204,56207,56221,56224,56228,56233,56236,56247,56250,56252,56256,56261,56265,56268,56272,56277,56281,56284,56286,56311,56314,56339,56343,56346,56348,56372],[10,55178,55174],{"id":55179},"timesleep-function-explained",[14,55181,55182,55184],{},[17,55183,42549],{}," pauses a Python program for a set amount of time.",[14,55186,55187],{},"It is a simple and useful function for:",[149,55189,55190,55193,55196,55199],{},[152,55191,55192],{},"adding a delay in a script",[152,55194,55195],{},"building a basic countdown",[152,55197,55198],{},"spacing out repeated output",[152,55200,55201],{},"practicing program flow",[14,55203,55204,55205,55207],{},"This function is part of Python’s ",[17,55206,2559],{}," module, so you must import that module before using it.",[40,55209,3596],{"id":3595},[45,55211,55213],{"className":47,"code":55212,"language":49,"meta":50,"style":50},"import time\n\nprint(\"Start\")\ntime.sleep(2)\nprint(\"After 2 seconds\")\n",[17,55214,55215,55221,55225,55239,55253],{"__ignoreMap":50},[54,55216,55217,55219],{"class":56,"line":57},[54,55218,61],{"class":60},[54,55220,31493],{"class":64},[54,55222,55223],{"class":56,"line":68},[54,55224,72],{"emptyLinePlaceholder":71},[54,55226,55227,55229,55231,55233,55235,55237],{"class":56,"line":75},[54,55228,116],{"class":115},[54,55230,96],{"class":88},[54,55232,545],{"class":544},[54,55234,42675],{"class":548},[54,55236,545],{"class":544},[54,55238,109],{"class":88},[54,55240,55241,55243,55245,55247,55249,55251],{"class":56,"line":112},[54,55242,2559],{"class":64},[54,55244,89],{"class":88},[54,55246,42410],{"class":92},[54,55248,96],{"class":88},[54,55250,396],{"class":99},[54,55252,109],{"class":88},[54,55254,55255,55257,55259,55261,55264,55266],{"class":56,"line":887},[54,55256,116],{"class":115},[54,55258,96],{"class":88},[54,55260,545],{"class":544},[54,55262,55263],{"class":548},"After 2 seconds",[54,55265,545],{"class":544},[54,55267,109],{"class":88},[14,55269,126,55270,55272,55273,55275,55276,89],{},[17,55271,42646],{}," to pause the program. The value can be an integer like ",[17,55274,396],{}," or a decimal like ",[17,55277,21071],{},[40,55279,55281],{"id":55280},"what-timesleep-does","What time.sleep() does",[14,55283,55284,55286],{},[17,55285,42549],{}," stops your program for a number of seconds.",[14,55288,11458],{},[149,55290,55291,55294,55299,55304],{},[152,55292,55293],{},"It pauses program execution.",[152,55295,55296,55297,15765],{},"It belongs to the built-in ",[17,55298,2559],{},[152,55300,20688,55301,55303],{},[17,55302,43068],{}," before using it.",[152,55305,55306,55307,55309,55310,89],{},"It accepts whole numbers like ",[17,55308,396],{}," and decimal numbers like ",[17,55311,21071],{},[14,55313,55314,55315,20375],{},"If you are new to modules, see ",[192,55316,54001],{"href":54000},[40,55318,199],{"id":198},[14,55320,202],{},[45,55322,55324],{"className":47,"code":55323,"language":49,"meta":50,"style":50},"time.sleep(seconds)\n",[17,55325,55326],{"__ignoreMap":50},[54,55327,55328,55330,55332,55334,55336,55338],{"class":56,"line":57},[54,55329,2559],{"class":64},[54,55331,89],{"class":88},[54,55333,42410],{"class":92},[54,55335,96],{"class":88},[54,55337,43446],{"class":92},[54,55339,109],{"class":88},[14,55341,42016,55342,55344],{},[17,55343,43446],{}," is the length of the pause.",[14,55346,31841],{},[149,55348,55349,55355],{},[152,55350,55351,55354],{},[17,55352,55353],{},"time.sleep(1)"," pauses for about one second",[152,55356,55357,55360],{},[17,55358,55359],{},"time.sleep(0.25)"," pauses for about a quarter of a second",[14,55362,55363],{},"A full example:",[45,55365,55367],{"className":47,"code":55366,"language":49,"meta":50,"style":50},"import time\n\ntime.sleep(1)\nprint(\"Done waiting\")\n",[17,55368,55369,55375,55379,55393],{"__ignoreMap":50},[54,55370,55371,55373],{"class":56,"line":57},[54,55372,61],{"class":60},[54,55374,31493],{"class":64},[54,55376,55377],{"class":56,"line":68},[54,55378,72],{"emptyLinePlaceholder":71},[54,55380,55381,55383,55385,55387,55389,55391],{"class":56,"line":75},[54,55382,2559],{"class":64},[54,55384,89],{"class":88},[54,55386,42410],{"class":92},[54,55388,96],{"class":88},[54,55390,100],{"class":99},[54,55392,109],{"class":88},[54,55394,55395,55397,55399,55401,55404,55406],{"class":56,"line":112},[54,55396,116],{"class":115},[54,55398,96],{"class":88},[54,55400,545],{"class":544},[54,55402,55403],{"class":548},"Done waiting",[54,55405,545],{"class":544},[54,55407,109],{"class":88},[40,55409,493],{"id":492},[14,55411,55412],{},"This example prints one message, waits, and then prints another message:",[45,55414,55416],{"className":47,"code":55415,"language":49,"meta":50,"style":50},"import time\n\nprint(\"Before pause\")\ntime.sleep(3)\nprint(\"After pause\")\n",[17,55417,55418,55424,55428,55443,55457],{"__ignoreMap":50},[54,55419,55420,55422],{"class":56,"line":57},[54,55421,61],{"class":60},[54,55423,31493],{"class":64},[54,55425,55426],{"class":56,"line":68},[54,55427,72],{"emptyLinePlaceholder":71},[54,55429,55430,55432,55434,55436,55439,55441],{"class":56,"line":75},[54,55431,116],{"class":115},[54,55433,96],{"class":88},[54,55435,545],{"class":544},[54,55437,55438],{"class":548},"Before pause",[54,55440,545],{"class":544},[54,55442,109],{"class":88},[54,55444,55445,55447,55449,55451,55453,55455],{"class":56,"line":112},[54,55446,2559],{"class":64},[54,55448,89],{"class":88},[54,55450,42410],{"class":92},[54,55452,96],{"class":88},[54,55454,401],{"class":99},[54,55456,109],{"class":88},[54,55458,55459,55461,55463,55465,55468,55470],{"class":56,"line":887},[54,55460,116],{"class":115},[54,55462,96],{"class":88},[54,55464,545],{"class":544},[54,55466,55467],{"class":548},"After pause",[54,55469,545],{"class":544},[54,55471,109],{"class":88},[14,55473,55474],{},"Expected output order:",[45,55476,55479],{"className":55477,"code":55478,"language":5171,"meta":50},[51303],"Before pause\nAfter pause\n",[17,55480,55478],{"__ignoreMap":50},[14,55482,55483,55484,55486,55487,89],{},"You will see ",[17,55485,55438],{}," first. Then the program waits about 3 seconds. After that, it prints ",[17,55488,55467],{},[40,55490,55492],{"id":55491},"using-timesleep-in-a-loop","Using time.sleep() in a loop",[14,55494,55495,55497],{},[17,55496,42549],{}," is often used inside loops.",[14,55499,7262],{},[149,55501,55502,55505,55508],{},[152,55503,55504],{},"countdowns",[152,55506,55507],{},"repeated messages",[152,55509,55510],{},"making output easier to follow",[14,55512,253],{},[45,55514,55516],{"className":47,"code":55515,"language":49,"meta":50,"style":50},"import time\n\nfor i in range(3):\n    print(\"Loop number:\", i)\n    time.sleep(1)\n\nprint(\"Finished\")\n",[17,55517,55518,55524,55528,55544,55564,55578,55582],{"__ignoreMap":50},[54,55519,55520,55522],{"class":56,"line":57},[54,55521,61],{"class":60},[54,55523,31493],{"class":64},[54,55525,55526],{"class":56,"line":68},[54,55527,72],{"emptyLinePlaceholder":71},[54,55529,55530,55532,55534,55536,55538,55540,55542],{"class":56,"line":75},[54,55531,26859],{"class":60},[54,55533,43354],{"class":64},[54,55535,26865],{"class":60},[54,55537,43359],{"class":115},[54,55539,96],{"class":88},[54,55541,401],{"class":99},[54,55543,10837],{"class":88},[54,55545,55546,55548,55550,55552,55555,55557,55559,55562],{"class":56,"line":112},[54,55547,7606],{"class":115},[54,55549,96],{"class":88},[54,55551,545],{"class":544},[54,55553,55554],{"class":548},"Loop number:",[54,55556,545],{"class":544},[54,55558,103],{"class":88},[54,55560,55561],{"class":92}," i",[54,55563,109],{"class":88},[54,55565,55566,55568,55570,55572,55574,55576],{"class":56,"line":887},[54,55567,43385],{"class":64},[54,55569,89],{"class":88},[54,55571,42410],{"class":92},[54,55573,96],{"class":88},[54,55575,100],{"class":99},[54,55577,109],{"class":88},[54,55579,55580],{"class":56,"line":892},[54,55581,72],{"emptyLinePlaceholder":71},[54,55583,55584,55586,55588,55590,55592,55594],{"class":56,"line":913},[54,55585,116],{"class":115},[54,55587,96],{"class":88},[54,55589,545],{"class":544},[54,55591,43303],{"class":548},[54,55593,545],{"class":544},[54,55595,109],{"class":88},[14,55597,55598],{},"What happens:",[149,55600,55601,55604,55607,55610],{},[152,55602,55603],{},"The loop prints a line.",[152,55605,55606],{},"The program pauses for 1 second.",[152,55608,55609],{},"The loop continues.",[152,55611,55612,55613,55615],{},"After the loop ends, ",[17,55614,43303],{}," is printed.",[14,55617,55618,55619,89],{},"A beginner-friendly real use case is a countdown timer. For a complete example, see ",[192,55620,55622],{"href":55621},"\u002Fexamples\u002Fpython-countdown-timer-example","Python countdown timer example",[40,55624,55626],{"id":55625},"what-value-to-pass","What value to pass",[14,55628,55629,55630,89],{},"You can pass different kinds of numbers to ",[17,55631,42549],{},[602,55633,55635],{"id":55634},"full-seconds","Full seconds",[14,55637,55638],{},"Use an integer when you want a whole number of seconds:",[45,55640,55642],{"className":47,"code":55641,"language":49,"meta":50,"style":50},"import time\n\ntime.sleep(2)\nprint(\"Waited 2 seconds\")\n",[17,55643,55644,55650,55654,55668],{"__ignoreMap":50},[54,55645,55646,55648],{"class":56,"line":57},[54,55647,61],{"class":60},[54,55649,31493],{"class":64},[54,55651,55652],{"class":56,"line":68},[54,55653,72],{"emptyLinePlaceholder":71},[54,55655,55656,55658,55660,55662,55664,55666],{"class":56,"line":75},[54,55657,2559],{"class":64},[54,55659,89],{"class":88},[54,55661,42410],{"class":92},[54,55663,96],{"class":88},[54,55665,396],{"class":99},[54,55667,109],{"class":88},[54,55669,55670,55672,55674,55676,55679,55681],{"class":56,"line":112},[54,55671,116],{"class":115},[54,55673,96],{"class":88},[54,55675,545],{"class":544},[54,55677,55678],{"class":548},"Waited 2 seconds",[54,55680,545],{"class":544},[54,55682,109],{"class":88},[602,55684,55686],{"id":55685},"fractional-seconds","Fractional seconds",[14,55688,55689],{},"Use a float when you want a shorter pause:",[45,55691,55693],{"className":47,"code":55692,"language":49,"meta":50,"style":50},"import time\n\ntime.sleep(0.5)\nprint(\"Waited half a second\")\n",[17,55694,55695,55701,55705,55719],{"__ignoreMap":50},[54,55696,55697,55699],{"class":56,"line":57},[54,55698,61],{"class":60},[54,55700,31493],{"class":64},[54,55702,55703],{"class":56,"line":68},[54,55704,72],{"emptyLinePlaceholder":71},[54,55706,55707,55709,55711,55713,55715,55717],{"class":56,"line":75},[54,55708,2559],{"class":64},[54,55710,89],{"class":88},[54,55712,42410],{"class":92},[54,55714,96],{"class":88},[54,55716,21071],{"class":99},[54,55718,109],{"class":88},[54,55720,55721,55723,55725,55727,55730,55732],{"class":56,"line":112},[54,55722,116],{"class":115},[54,55724,96],{"class":88},[54,55726,545],{"class":544},[54,55728,55729],{"class":548},"Waited half a second",[54,55731,545],{"class":544},[54,55733,109],{"class":88},[602,55735,55737],{"id":55736},"zero","Zero",[14,55739,55740,55742],{},[17,55741,752],{}," is valid, but it usually creates no visible delay:",[45,55744,55746],{"className":47,"code":55745,"language":49,"meta":50,"style":50},"import time\n\ntime.sleep(0)\nprint(\"No visible pause\")\n",[17,55747,55748,55754,55758,55772],{"__ignoreMap":50},[54,55749,55750,55752],{"class":56,"line":57},[54,55751,61],{"class":60},[54,55753,31493],{"class":64},[54,55755,55756],{"class":56,"line":68},[54,55757,72],{"emptyLinePlaceholder":71},[54,55759,55760,55762,55764,55766,55768,55770],{"class":56,"line":75},[54,55761,2559],{"class":64},[54,55763,89],{"class":88},[54,55765,42410],{"class":92},[54,55767,96],{"class":88},[54,55769,752],{"class":99},[54,55771,109],{"class":88},[54,55773,55774,55776,55778,55780,55783,55785],{"class":56,"line":112},[54,55775,116],{"class":115},[54,55777,96],{"class":88},[54,55779,545],{"class":544},[54,55781,55782],{"class":548},"No visible pause",[54,55784,545],{"class":544},[54,55786,109],{"class":88},[602,55788,55790],{"id":55789},"negative-numbers","Negative numbers",[14,55792,55793],{},"Negative numbers are not valid.",[14,55795,55796],{},"This will cause an error:",[45,55798,55800],{"className":47,"code":55799,"language":49,"meta":50,"style":50},"import time\n\ntime.sleep(-1)\n",[17,55801,55802,55808,55812],{"__ignoreMap":50},[54,55803,55804,55806],{"class":56,"line":57},[54,55805,61],{"class":60},[54,55807,31493],{"class":64},[54,55809,55810],{"class":56,"line":68},[54,55811,72],{"emptyLinePlaceholder":71},[54,55813,55814,55816,55818,55820,55822,55824,55826],{"class":56,"line":75},[54,55815,2559],{"class":64},[54,55817,89],{"class":88},[54,55819,42410],{"class":92},[54,55821,96],{"class":88},[54,55823,1467],{"class":81},[54,55825,100],{"class":99},[54,55827,109],{"class":88},[40,55829,2879],{"id":2878},[14,55831,55832,55833,89],{},"Beginners often run into a few common problems with ",[17,55834,42549],{},[602,55836,44096],{"id":43650},[14,55838,985,55839,147],{},[17,55840,988],{},[45,55842,55844],{"className":47,"code":55843,"language":49,"meta":50,"style":50},"time.sleep(2)\n",[17,55845,55846],{"__ignoreMap":50},[54,55847,55848,55850,55852,55854,55856,55858],{"class":56,"line":57},[54,55849,2559],{"class":64},[54,55851,89],{"class":88},[54,55853,42410],{"class":92},[54,55855,96],{"class":88},[54,55857,396],{"class":99},[54,55859,109],{"class":88},[14,55861,55862,55863,55865],{},"Python does not know what ",[17,55864,2559],{}," means unless you import it first.",[14,55867,1310],{},[45,55869,55870],{"className":47,"code":43756,"language":49,"meta":50,"style":50},[17,55871,55872,55878,55882],{"__ignoreMap":50},[54,55873,55874,55876],{"class":56,"line":57},[54,55875,61],{"class":60},[54,55877,31493],{"class":64},[54,55879,55880],{"class":56,"line":68},[54,55881,72],{"emptyLinePlaceholder":71},[54,55883,55884,55886,55888,55890,55892,55894],{"class":56,"line":75},[54,55885,2559],{"class":64},[54,55887,89],{"class":88},[54,55889,42410],{"class":92},[54,55891,96],{"class":88},[54,55893,396],{"class":99},[54,55895,109],{"class":88},[14,55897,55898,55899,89],{},"If you want help with this error, see ",[192,55900,1087],{"href":1083},[602,55902,23176],{"id":23175},[14,55904,985,55905,147],{},[17,55906,10040],{},[45,55908,55909],{"className":47,"code":43721,"language":49,"meta":50,"style":50},[17,55910,55911,55917,55921],{"__ignoreMap":50},[54,55912,55913,55915],{"class":56,"line":57},[54,55914,61],{"class":60},[54,55916,31493],{"class":64},[54,55918,55919],{"class":56,"line":68},[54,55920,72],{"emptyLinePlaceholder":71},[54,55922,55923,55925,55927,55929,55931,55933,55935,55937],{"class":56,"line":75},[54,55924,2559],{"class":64},[54,55926,89],{"class":88},[54,55928,42410],{"class":92},[54,55930,96],{"class":88},[54,55932,545],{"class":544},[54,55934,396],{"class":548},[54,55936,545],{"class":544},[54,55938,109],{"class":88},[14,55940,55941,55944],{},[17,55942,55943],{},"\"2\""," is a string, not a number.",[14,55946,1310],{},[45,55948,55949],{"className":47,"code":43756,"language":49,"meta":50,"style":50},[17,55950,55951,55957,55961],{"__ignoreMap":50},[54,55952,55953,55955],{"class":56,"line":57},[54,55954,61],{"class":60},[54,55956,31493],{"class":64},[54,55958,55959],{"class":56,"line":68},[54,55960,72],{"emptyLinePlaceholder":71},[54,55962,55963,55965,55967,55969,55971,55973],{"class":56,"line":75},[54,55964,2559],{"class":64},[54,55966,89],{"class":88},[54,55968,42410],{"class":92},[54,55970,96],{"class":88},[54,55972,396],{"class":99},[54,55974,109],{"class":88},[14,55976,55977],{},"You can also convert user input if needed:",[45,55979,55981],{"className":47,"code":55980,"language":49,"meta":50,"style":50},"import time\n\nseconds = float(input(\"Enter seconds: \"))\ntime.sleep(seconds)\nprint(\"Done\")\n",[17,55982,55983,55989,55993,56018,56032],{"__ignoreMap":50},[54,55984,55985,55987],{"class":56,"line":57},[54,55986,61],{"class":60},[54,55988,31493],{"class":64},[54,55990,55991],{"class":56,"line":68},[54,55992,72],{"emptyLinePlaceholder":71},[54,55994,55995,55998,56000,56002,56004,56007,56009,56011,56014,56016],{"class":56,"line":75},[54,55996,55997],{"class":64},"seconds ",[54,55999,82],{"class":81},[54,56001,20288],{"class":429},[54,56003,96],{"class":88},[54,56005,56006],{"class":115},"input",[54,56008,96],{"class":88},[54,56010,545],{"class":544},[54,56012,56013],{"class":548},"Enter seconds: ",[54,56015,545],{"class":544},[54,56017,382],{"class":88},[54,56019,56020,56022,56024,56026,56028,56030],{"class":56,"line":112},[54,56021,2559],{"class":64},[54,56023,89],{"class":88},[54,56025,42410],{"class":92},[54,56027,96],{"class":88},[54,56029,43446],{"class":92},[54,56031,109],{"class":88},[54,56033,56034,56036,56038,56040,56042,56044],{"class":56,"line":887},[54,56035,116],{"class":115},[54,56037,96],{"class":88},[54,56039,545],{"class":544},[54,56041,42427],{"class":548},[54,56043,545],{"class":544},[54,56045,109],{"class":88},[602,56047,56049],{"id":56048},"using-sleep-without-the-module-name","Using sleep() without the module name",[14,56051,56052,56053,147],{},"This does not work if you only wrote ",[17,56054,43068],{},[45,56056,56058],{"className":47,"code":56057,"language":49,"meta":50,"style":50},"import time\n\nsleep(2)\n",[17,56059,56060,56066,56070],{"__ignoreMap":50},[54,56061,56062,56064],{"class":56,"line":57},[54,56063,61],{"class":60},[54,56065,31493],{"class":64},[54,56067,56068],{"class":56,"line":68},[54,56069,72],{"emptyLinePlaceholder":71},[54,56071,56072,56074,56076,56078],{"class":56,"line":75},[54,56073,42410],{"class":92},[54,56075,96],{"class":88},[54,56077,396],{"class":99},[54,56079,109],{"class":88},[14,56081,56082,56083,56085],{},"With ",[17,56084,43068],{},", you must write:",[45,56087,56088],{"className":47,"code":43756,"language":49,"meta":50,"style":50},[17,56089,56090,56096,56100],{"__ignoreMap":50},[54,56091,56092,56094],{"class":56,"line":57},[54,56093,61],{"class":60},[54,56095,31493],{"class":64},[54,56097,56098],{"class":56,"line":68},[54,56099,72],{"emptyLinePlaceholder":71},[54,56101,56102,56104,56106,56108,56110,56112],{"class":56,"line":75},[54,56103,2559],{"class":64},[54,56105,89],{"class":88},[54,56107,42410],{"class":92},[54,56109,96],{"class":88},[54,56111,396],{"class":99},[54,56113,109],{"class":88},[14,56115,56116],{},"Another valid option is:",[45,56118,56120],{"className":47,"code":56119,"language":49,"meta":50,"style":50},"from time import sleep\n\nsleep(2)\nprint(\"Done\")\n",[17,56121,56122,56134,56138,56148],{"__ignoreMap":50},[54,56123,56124,56126,56129,56131],{"class":56,"line":57},[54,56125,1640],{"class":60},[54,56127,56128],{"class":64}," time ",[54,56130,61],{"class":60},[54,56132,56133],{"class":64}," sleep\n",[54,56135,56136],{"class":56,"line":68},[54,56137,72],{"emptyLinePlaceholder":71},[54,56139,56140,56142,56144,56146],{"class":56,"line":75},[54,56141,42410],{"class":92},[54,56143,96],{"class":88},[54,56145,396],{"class":99},[54,56147,109],{"class":88},[54,56149,56150,56152,56154,56156,56158,56160],{"class":56,"line":112},[54,56151,116],{"class":115},[54,56153,96],{"class":88},[54,56155,545],{"class":544},[54,56157,42427],{"class":548},[54,56159,545],{"class":544},[54,56161,109],{"class":88},[14,56163,23588,56164,134,56166,56169],{},[17,56165,43068],{},[17,56167,56168],{},"time.sleep(...)"," is usually clearer.",[602,56171,56173],{"id":56172},"expecting-exact-timing","Expecting exact timing",[14,56175,56176,56178],{},[17,56177,42549],{}," usually waits about the amount of time you requested, but it is not perfectly exact.",[14,56180,56181],{},"Small timing differences can happen because of:",[149,56183,56184,56186,56189],{},[152,56185,54222],{},[152,56187,56188],{},"other programs running",[152,56190,56191],{},"normal scheduling delays",[14,56193,28812,56194,56196],{},[17,56195,55353],{}," means “wait about 1 second,” not “wait with perfect precision.”",[40,56198,30311],{"id":30310},[14,56200,56201,56203],{},[17,56202,42549],{}," is a good choice for simple beginner programs.",[14,56205,56206],{},"Common uses:",[149,56208,56209,56212,56215,56218],{},[152,56210,56211],{},"simple delays in scripts",[152,56213,56214],{},"basic countdown timers",[152,56216,56217],{},"spacing out repeated actions",[152,56219,56220],{},"learning how program flow works",[14,56222,56223],{},"It is especially helpful when you want to see your program run step by step.",[40,56225,56227],{"id":56226},"when-not-to-use-it","When not to use it",[14,56229,56230,56232],{},[17,56231,42549],{}," is not the best tool in every situation.",[14,56234,56235],{},"It is not ideal when:",[149,56237,56238,56241,56244],{},[152,56239,56240],{},"you need precise timing",[152,56242,56243],{},"your program must stay responsive during the delay",[152,56245,56246],{},"you want other work to continue at the same time",[14,56248,56249],{},"To beginners, a sleeping program can look frozen. That is normal. While the program is sleeping, that part of the code is not doing anything else.",[40,56251,1419],{"id":1418},[602,56253,56255],{"id":56254},"does-timesleep-use-seconds-or-milliseconds","Does time.sleep() use seconds or milliseconds?",[14,56257,56258,56259,89],{},"It uses seconds. You can use decimals for part of a second, such as ",[17,56260,21071],{},[602,56262,56264],{"id":56263},"can-i-use-timesleep01","Can I use time.sleep(0.1)?",[14,56266,56267],{},"Yes. A float value is valid and pauses for a fraction of a second.",[602,56269,56271],{"id":56270},"why-does-my-program-seem-frozen","Why does my program seem frozen?",[14,56273,56274,56276],{},[17,56275,42549],{}," pauses execution, so nothing else in that part of the program runs during the delay.",[602,56278,56280],{"id":56279},"why-do-i-get-an-error-when-i-use-sleep2","Why do I get an error when I use sleep(2)?",[14,56282,56283],{},"You probably forgot to import it correctly.",[14,56285,3040],{},[45,56287,56289],{"className":47,"code":56288,"language":49,"meta":50,"style":50},"import time\ntime.sleep(2)\n",[17,56290,56291,56297],{"__ignoreMap":50},[54,56292,56293,56295],{"class":56,"line":57},[54,56294,61],{"class":60},[54,56296,31493],{"class":64},[54,56298,56299,56301,56303,56305,56307,56309],{"class":56,"line":68},[54,56300,2559],{"class":64},[54,56302,89],{"class":88},[54,56304,42410],{"class":92},[54,56306,96],{"class":88},[54,56308,396],{"class":99},[54,56310,109],{"class":88},[14,56312,56313],{},"Or:",[45,56315,56317],{"className":47,"code":56316,"language":49,"meta":50,"style":50},"from time import sleep\nsleep(2)\n",[17,56318,56319,56329],{"__ignoreMap":50},[54,56320,56321,56323,56325,56327],{"class":56,"line":57},[54,56322,1640],{"class":60},[54,56324,56128],{"class":64},[54,56326,61],{"class":60},[54,56328,56133],{"class":64},[54,56330,56331,56333,56335,56337],{"class":56,"line":68},[54,56332,42410],{"class":92},[54,56334,96],{"class":88},[54,56336,396],{"class":99},[54,56338,109],{"class":88},[602,56340,56342],{"id":56341},"is-the-delay-always-exact","Is the delay always exact?",[14,56344,56345],{},"No. It is usually close, but the exact timing can vary slightly.",[40,56347,1510],{"id":1509},[149,56349,56350,56355,56359,56363,56367],{},[152,56351,56352],{},[192,56353,56354],{"href":44116},"Python time module overview",[152,56356,56357],{},[192,56358,54001],{"href":54000},[152,56360,56361],{},[192,56362,53646],{"href":52246},[152,56364,56365],{},[192,56366,55622],{"href":55621},[152,56368,56369],{},[192,56370,56371],{"href":1083},"NameError: name is not defined fix",[1545,56373,56374],{},"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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":56376},[56377,56378,56379,56380,56381,56382,56388,56394,56395,56396,56403],{"id":3595,"depth":68,"text":3596},{"id":55280,"depth":68,"text":55281},{"id":198,"depth":68,"text":199},{"id":492,"depth":68,"text":493},{"id":55491,"depth":68,"text":55492},{"id":55625,"depth":68,"text":55626,"children":56383},[56384,56385,56386,56387],{"id":55634,"depth":75,"text":55635},{"id":55685,"depth":75,"text":55686},{"id":55736,"depth":75,"text":55737},{"id":55789,"depth":75,"text":55790},{"id":2878,"depth":68,"text":2879,"children":56389},[56390,56391,56392,56393],{"id":43650,"depth":75,"text":44096},{"id":23175,"depth":75,"text":23176},{"id":56048,"depth":75,"text":56049},{"id":56172,"depth":75,"text":56173},{"id":30310,"depth":68,"text":30311},{"id":56226,"depth":68,"text":56227},{"id":1418,"depth":68,"text":1419,"children":56397},[56398,56399,56400,56401,56402],{"id":56254,"depth":75,"text":56255},{"id":56263,"depth":75,"text":56264},{"id":56270,"depth":75,"text":56271},{"id":56279,"depth":75,"text":56280},{"id":56341,"depth":75,"text":56342},{"id":1509,"depth":68,"text":1510},"Master time.sleep function explained in our comprehensive Python beginner guide.",{},{"title":55174,"description":56404},"standard-library\u002Ftime.sleep-function-explained","yUWK8e6_JRz8fIt2Aor6zfYOSdfwW7kOTtUTeQY-mVQ",{"id":56410,"title":56411,"body":56412,"description":58056,"extension":1577,"meta":58057,"navigation":71,"path":42555,"seo":58058,"stem":58059,"__hash__":58060},"content\u002Fstandard-library\u002Ftime.time-function-explained.md","time.time() Function Explained",{"type":7,"value":56413,"toc":58022},[56414,56419,56424,56435,56442,56451,56453,56552,56557,56559,56574,56579,56583,56604,56606,56659,56661,56684,56687,56689,56695,56734,56736,56751,56759,56761,56786,56791,56793,56796,56804,56806,56814,56819,56826,56879,56889,56893,56897,56911,56914,56963,56966,57064,57074,57080,57083,57086,57100,57102,57243,57245,57282,57285,57290,57294,57299,57317,57320,57337,57339,57343,57348,57353,57384,57386,57425,57429,57431,57459,57464,57469,57476,57479,57491,57493,57566,57576,57578,57602,57605,57633,57637,57643,57646,57730,57733,57808,57814,57820,57823,57834,57837,57854,57857,57875,57878,57896,57903,57905,57911,57914,57920,57923,57929,57932,57938,57943,57977,57979,58009,58019],[10,56415,56417,11309],{"id":56416},"timetime-function-explained",[17,56418,42440],{},[14,56420,56421,56423],{},[17,56422,42440],{}," returns the current time as a number.",[14,56425,56426,56427,56430,56431,56434],{},"In Python, this number is the current ",[35,56428,56429],{},"Unix timestamp",": the number of seconds since ",[35,56432,56433],{},"January 1, 1970 UTC",". Beginners often use it for two simple tasks:",[149,56436,56437,56439],{},[152,56438,42341],{},[152,56440,56441],{},"measuring how long some code takes to run",[14,56443,56444,56445,56448,56449,16092],{},"If you want a readable date like ",[17,56446,56447],{},"2025-04-22 10:30:00",", use the ",[192,56450,1774],{"href":1773},[40,56452,3596],{"id":3595},[45,56454,56456],{"className":47,"code":56455,"language":49,"meta":50,"style":50},"import time\n\ncurrent_timestamp = time.time()\nprint(current_timestamp)\n\nstart = time.time()\ntime.sleep(1)\nend = time.time()\nprint(end - start)\n",[17,56457,56458,56464,56468,56482,56492,56496,56510,56524,56538],{"__ignoreMap":50},[54,56459,56460,56462],{"class":56,"line":57},[54,56461,61],{"class":60},[54,56463,31493],{"class":64},[54,56465,56466],{"class":56,"line":68},[54,56467,72],{"emptyLinePlaceholder":71},[54,56469,56470,56472,56474,56476,56478,56480],{"class":56,"line":75},[54,56471,42614],{"class":64},[54,56473,82],{"class":81},[54,56475,31045],{"class":64},[54,56477,89],{"class":88},[54,56479,2559],{"class":92},[54,56481,1670],{"class":88},[54,56483,56484,56486,56488,56490],{"class":56,"line":112},[54,56485,116],{"class":115},[54,56487,96],{"class":88},[54,56489,42633],{"class":92},[54,56491,109],{"class":88},[54,56493,56494],{"class":56,"line":887},[54,56495,72],{"emptyLinePlaceholder":71},[54,56497,56498,56500,56502,56504,56506,56508],{"class":56,"line":892},[54,56499,43102],{"class":64},[54,56501,82],{"class":81},[54,56503,31045],{"class":64},[54,56505,89],{"class":88},[54,56507,2559],{"class":92},[54,56509,1670],{"class":88},[54,56511,56512,56514,56516,56518,56520,56522],{"class":56,"line":913},[54,56513,2559],{"class":64},[54,56515,89],{"class":88},[54,56517,42410],{"class":92},[54,56519,96],{"class":88},[54,56521,100],{"class":99},[54,56523,109],{"class":88},[54,56525,56526,56528,56530,56532,56534,56536],{"class":56,"line":2121},[54,56527,43159],{"class":64},[54,56529,82],{"class":81},[54,56531,31045],{"class":64},[54,56533,89],{"class":88},[54,56535,2559],{"class":92},[54,56537,1670],{"class":88},[54,56539,56540,56542,56544,56546,56548,56550],{"class":56,"line":2136},[54,56541,116],{"class":115},[54,56543,96],{"class":88},[54,56545,43159],{"class":92},[54,56547,1467],{"class":81},[54,56549,43130],{"class":92},[54,56551,109],{"class":88},[14,56553,56554,56556],{},[17,56555,42440],{}," returns the current time as seconds since the Unix epoch. It is often used for simple timing.",[14,56558,561],{},[45,56560,56562],{"className":47,"code":56561,"language":49,"meta":50,"style":50},"1712345678.12345\n1.00012\n",[17,56563,56564,56569],{"__ignoreMap":50},[54,56565,56566],{"class":56,"line":57},[54,56567,56568],{"class":99},"1712345678.12345\n",[54,56570,56571],{"class":56,"line":68},[54,56572,56573],{"class":99},"1.00012\n",[40,56575,1736,56577,1739],{"id":56576},"what-timetime-does",[17,56578,42440],{},[14,56580,56581,147],{},[17,56582,42440],{},[149,56584,56585,56590,56593,56601],{},[152,56586,56587,56588],{},"returns the current time as a ",[17,56589,9662],{},[152,56591,56592],{},"gives the number of seconds since January 1, 1970 UTC",[152,56594,56595,56596,5507,56598],{},"returns what is often called a ",[35,56597,56429],{},[35,56599,56600],{},"epoch time",[152,56602,56603],{},"includes fractions of a second in the decimal part",[14,56605,11556],{},[45,56607,56609],{"className":47,"code":56608,"language":49,"meta":50,"style":50},"import time\n\ntimestamp = time.time()\nprint(timestamp)\nprint(type(timestamp))\n",[17,56610,56611,56617,56621,56635,56645],{"__ignoreMap":50},[54,56612,56613,56615],{"class":56,"line":57},[54,56614,61],{"class":60},[54,56616,31493],{"class":64},[54,56618,56619],{"class":56,"line":68},[54,56620,72],{"emptyLinePlaceholder":71},[54,56622,56623,56625,56627,56629,56631,56633],{"class":56,"line":75},[54,56624,7135],{"class":64},[54,56626,82],{"class":81},[54,56628,31045],{"class":64},[54,56630,89],{"class":88},[54,56632,2559],{"class":92},[54,56634,1670],{"class":88},[54,56636,56637,56639,56641,56643],{"class":56,"line":112},[54,56638,116],{"class":115},[54,56640,96],{"class":88},[54,56642,7161],{"class":92},[54,56644,109],{"class":88},[54,56646,56647,56649,56651,56653,56655,56657],{"class":56,"line":887},[54,56648,116],{"class":115},[54,56650,96],{"class":88},[54,56652,2208],{"class":429},[54,56654,96],{"class":88},[54,56656,7161],{"class":92},[54,56658,382],{"class":88},[14,56660,305],{},[45,56662,56664],{"className":47,"code":56663,"language":49,"meta":50,"style":50},"1712345678.12345\n\u003Cclass 'float'>\n",[17,56665,56666,56670],{"__ignoreMap":50},[54,56667,56668],{"class":56,"line":57},[54,56669,56568],{"class":99},[54,56671,56672,56674,56676,56678,56680,56682],{"class":56,"line":68},[54,56673,2227],{"class":81},[54,56675,2231],{"class":2230},[54,56677,2234],{"class":544},[54,56679,9662],{"class":548},[54,56681,2240],{"class":544},[54,56683,2243],{"class":81},[14,56685,56686],{},"This value is useful for storing or comparing times, but it is not easy for humans to read.",[40,56688,199],{"id":198},[14,56690,25219,56691,25222,56693,25225],{},[17,56692,2559],{},[17,56694,42440],{},[45,56696,56698],{"className":47,"code":56697,"language":49,"meta":50,"style":50},"import time\n\ncurrent_time = time.time()\nprint(current_time)\n",[17,56699,56700,56706,56710,56724],{"__ignoreMap":50},[54,56701,56702,56704],{"class":56,"line":57},[54,56703,61],{"class":60},[54,56705,31493],{"class":64},[54,56707,56708],{"class":56,"line":68},[54,56709,72],{"emptyLinePlaceholder":71},[54,56711,56712,56714,56716,56718,56720,56722],{"class":56,"line":75},[54,56713,42870],{"class":64},[54,56715,82],{"class":81},[54,56717,31045],{"class":64},[54,56719,89],{"class":88},[54,56721,2559],{"class":92},[54,56723,1670],{"class":88},[54,56725,56726,56728,56730,56732],{"class":56,"line":112},[54,56727,116],{"class":115},[54,56729,96],{"class":88},[54,56731,42890],{"class":92},[54,56733,109],{"class":88},[14,56735,11458],{},[149,56737,56738,56743,56748],{},[152,56739,56740,56741,167],{},"you must ",[17,56742,43068],{},[152,56744,56745,56747],{},[17,56746,42440],{}," takes no arguments",[152,56749,56750],{},"store the result in a variable if you want to use it again later",[14,56752,56753,56754,56756,56757,89],{},"If you used ",[17,56755,43068],{},", you must call the function as ",[17,56758,42440],{},[14,56760,33166],{},[45,56762,56764],{"className":47,"code":56763,"language":49,"meta":50,"style":50},"import time\n\nprint(time())\n",[17,56765,56766,56772,56776],{"__ignoreMap":50},[54,56767,56768,56770],{"class":56,"line":57},[54,56769,61],{"class":60},[54,56771,31493],{"class":64},[54,56773,56774],{"class":56,"line":68},[54,56775,72],{"emptyLinePlaceholder":71},[54,56777,56778,56780,56782,56784],{"class":56,"line":75},[54,56779,116],{"class":115},[54,56781,96],{"class":88},[54,56783,2559],{"class":92},[54,56785,1950],{"class":88},[14,56787,3412,56788,56790],{},[17,56789,2559],{}," is the module name, not the function name.",[40,56792,22597],{"id":22596},[14,56794,56795],{},"The result is usually a large floating-point number, such as:",[45,56797,56798],{"className":47,"code":56568,"language":49,"meta":50,"style":50},[17,56799,56800],{"__ignoreMap":50},[54,56801,56802],{"class":56,"line":57},[54,56803,56568],{"class":99},[14,56805,24206],{},[149,56807,56808,56811],{},[152,56809,56810],{},"the whole number part is the number of full seconds",[152,56812,56813],{},"the decimal part is the fraction of a second",[14,56815,38983,56816,56818],{},[35,56817,3036],{}," a formatted date string.",[14,56820,56821,56822,147],{},"If you want only whole seconds, you can convert it with ",[192,56823,56824],{"href":42022},[17,56825,20065],{},[45,56827,56829],{"className":47,"code":56828,"language":49,"meta":50,"style":50},"import time\n\ntimestamp = time.time()\nprint(timestamp)\nprint(int(timestamp))\n",[17,56830,56831,56837,56841,56855,56865],{"__ignoreMap":50},[54,56832,56833,56835],{"class":56,"line":57},[54,56834,61],{"class":60},[54,56836,31493],{"class":64},[54,56838,56839],{"class":56,"line":68},[54,56840,72],{"emptyLinePlaceholder":71},[54,56842,56843,56845,56847,56849,56851,56853],{"class":56,"line":75},[54,56844,7135],{"class":64},[54,56846,82],{"class":81},[54,56848,31045],{"class":64},[54,56850,89],{"class":88},[54,56852,2559],{"class":92},[54,56854,1670],{"class":88},[54,56856,56857,56859,56861,56863],{"class":56,"line":112},[54,56858,116],{"class":115},[54,56860,96],{"class":88},[54,56862,7161],{"class":92},[54,56864,109],{"class":88},[54,56866,56867,56869,56871,56873,56875,56877],{"class":56,"line":887},[54,56868,116],{"class":115},[54,56870,96],{"class":88},[54,56872,9657],{"class":429},[54,56874,96],{"class":88},[54,56876,7161],{"class":92},[54,56878,382],{"class":88},[14,56880,56881,56882,56886,56887,89],{},"If you want a readable date and time, look at ",[192,56883,56884,2834],{"href":3535},[17,56885,1594],{}," or the ",[192,56888,1774],{"href":1773},[40,56890,56892],{"id":56891},"common-use-cases","Common use cases",[14,56894,37687,56895,24374],{},[17,56896,42440],{},[149,56898,56899,56902,56905,56908],{},[152,56900,56901],{},"get the current timestamp",[152,56903,56904],{},"measure how long code takes to run",[152,56906,56907],{},"compare two times by subtraction",[152,56909,56910],{},"store simple time values in logs or files",[14,56912,56913],{},"Example: getting the current timestamp",[45,56915,56917],{"className":47,"code":56916,"language":49,"meta":50,"style":50},"import time\n\ntimestamp = time.time()\nprint(\"Current timestamp:\", timestamp)\n",[17,56918,56919,56925,56929,56943],{"__ignoreMap":50},[54,56920,56921,56923],{"class":56,"line":57},[54,56922,61],{"class":60},[54,56924,31493],{"class":64},[54,56926,56927],{"class":56,"line":68},[54,56928,72],{"emptyLinePlaceholder":71},[54,56930,56931,56933,56935,56937,56939,56941],{"class":56,"line":75},[54,56932,7135],{"class":64},[54,56934,82],{"class":81},[54,56936,31045],{"class":64},[54,56938,89],{"class":88},[54,56940,2559],{"class":92},[54,56942,1670],{"class":88},[54,56944,56945,56947,56949,56951,56954,56956,56958,56961],{"class":56,"line":112},[54,56946,116],{"class":115},[54,56948,96],{"class":88},[54,56950,545],{"class":544},[54,56952,56953],{"class":548},"Current timestamp:",[54,56955,545],{"class":544},[54,56957,103],{"class":88},[54,56959,56960],{"class":92}," timestamp",[54,56962,109],{"class":88},[14,56964,56965],{},"Example: comparing two times",[45,56967,56969],{"className":47,"code":56968,"language":49,"meta":50,"style":50},"import time\n\nfirst = time.time()\ntime.sleep(2)\nsecond = time.time()\n\ndifference = second - first\nprint(\"Seconds between times:\", difference)\n",[17,56970,56971,56977,56981,56996,57010,57025,57029,57044],{"__ignoreMap":50},[54,56972,56973,56975],{"class":56,"line":57},[54,56974,61],{"class":60},[54,56976,31493],{"class":64},[54,56978,56979],{"class":56,"line":68},[54,56980,72],{"emptyLinePlaceholder":71},[54,56982,56983,56986,56988,56990,56992,56994],{"class":56,"line":75},[54,56984,56985],{"class":64},"first ",[54,56987,82],{"class":81},[54,56989,31045],{"class":64},[54,56991,89],{"class":88},[54,56993,2559],{"class":92},[54,56995,1670],{"class":88},[54,56997,56998,57000,57002,57004,57006,57008],{"class":56,"line":112},[54,56999,2559],{"class":64},[54,57001,89],{"class":88},[54,57003,42410],{"class":92},[54,57005,96],{"class":88},[54,57007,396],{"class":99},[54,57009,109],{"class":88},[54,57011,57012,57015,57017,57019,57021,57023],{"class":56,"line":887},[54,57013,57014],{"class":64},"second ",[54,57016,82],{"class":81},[54,57018,31045],{"class":64},[54,57020,89],{"class":88},[54,57022,2559],{"class":92},[54,57024,1670],{"class":88},[54,57026,57027],{"class":56,"line":892},[54,57028,72],{"emptyLinePlaceholder":71},[54,57030,57031,57034,57036,57039,57041],{"class":56,"line":913},[54,57032,57033],{"class":64},"difference ",[54,57035,82],{"class":81},[54,57037,57038],{"class":64}," second ",[54,57040,1467],{"class":81},[54,57042,57043],{"class":64}," first\n",[54,57045,57046,57048,57050,57052,57055,57057,57059,57062],{"class":56,"line":2121},[54,57047,116],{"class":115},[54,57049,96],{"class":88},[54,57051,545],{"class":544},[54,57053,57054],{"class":548},"Seconds between times:",[54,57056,545],{"class":544},[54,57058,103],{"class":88},[54,57060,57061],{"class":92}," difference",[54,57063,109],{"class":88},[14,57065,57066,57067,57071,57072,89],{},"In this example, ",[192,57068,57069],{"href":42546},[17,57070,42549],{}," pauses the program for about 2 seconds, so the difference is about ",[17,57073,18568],{},[40,57075,5481,57077,57079],{"id":57076},"using-timetime-to-measure-elapsed-time",[17,57078,42440],{}," to measure elapsed time",[14,57081,57082],{},"This is one of the most common beginner uses.",[14,57084,57085],{},"The pattern is:",[27327,57087,57088,57091,57094,57097],{},[152,57089,57090],{},"save the start time",[152,57092,57093],{},"run your code",[152,57095,57096],{},"save the end time",[152,57098,57099],{},"subtract start from end",[14,57101,253],{},[45,57103,57105],{"className":47,"code":57104,"language":49,"meta":50,"style":50},"import time\n\nstart = time.time()\n\nfor i in range(3):\n    print(\"Working...\")\n    time.sleep(0.5)\n\nend = time.time()\n\nelapsed = end - start\nprint(\"Elapsed time:\", elapsed, \"seconds\")\n",[17,57106,57107,57113,57117,57131,57135,57151,57165,57179,57183,57197,57201,57215],{"__ignoreMap":50},[54,57108,57109,57111],{"class":56,"line":57},[54,57110,61],{"class":60},[54,57112,31493],{"class":64},[54,57114,57115],{"class":56,"line":68},[54,57116,72],{"emptyLinePlaceholder":71},[54,57118,57119,57121,57123,57125,57127,57129],{"class":56,"line":75},[54,57120,43102],{"class":64},[54,57122,82],{"class":81},[54,57124,31045],{"class":64},[54,57126,89],{"class":88},[54,57128,2559],{"class":92},[54,57130,1670],{"class":88},[54,57132,57133],{"class":56,"line":112},[54,57134,72],{"emptyLinePlaceholder":71},[54,57136,57137,57139,57141,57143,57145,57147,57149],{"class":56,"line":887},[54,57138,26859],{"class":60},[54,57140,43354],{"class":64},[54,57142,26865],{"class":60},[54,57144,43359],{"class":115},[54,57146,96],{"class":88},[54,57148,401],{"class":99},[54,57150,10837],{"class":88},[54,57152,57153,57155,57157,57159,57161,57163],{"class":56,"line":892},[54,57154,7606],{"class":115},[54,57156,96],{"class":88},[54,57158,545],{"class":544},[54,57160,43376],{"class":548},[54,57162,545],{"class":544},[54,57164,109],{"class":88},[54,57166,57167,57169,57171,57173,57175,57177],{"class":56,"line":913},[54,57168,43385],{"class":64},[54,57170,89],{"class":88},[54,57172,42410],{"class":92},[54,57174,96],{"class":88},[54,57176,21071],{"class":99},[54,57178,109],{"class":88},[54,57180,57181],{"class":56,"line":2121},[54,57182,72],{"emptyLinePlaceholder":71},[54,57184,57185,57187,57189,57191,57193,57195],{"class":56,"line":2136},[54,57186,43159],{"class":64},[54,57188,82],{"class":81},[54,57190,31045],{"class":64},[54,57192,89],{"class":88},[54,57194,2559],{"class":92},[54,57196,1670],{"class":88},[54,57198,57199],{"class":56,"line":2151},[54,57200,72],{"emptyLinePlaceholder":71},[54,57202,57203,57206,57208,57210,57212],{"class":56,"line":4442},[54,57204,57205],{"class":64},"elapsed ",[54,57207,82],{"class":81},[54,57209,43435],{"class":64},[54,57211,1467],{"class":81},[54,57213,57214],{"class":64}," start\n",[54,57216,57217,57219,57221,57223,57226,57228,57230,57233,57235,57237,57239,57241],{"class":56,"line":4468},[54,57218,116],{"class":115},[54,57220,96],{"class":88},[54,57222,545],{"class":544},[54,57224,57225],{"class":548},"Elapsed time:",[54,57227,545],{"class":544},[54,57229,103],{"class":88},[54,57231,57232],{"class":92}," elapsed",[54,57234,103],{"class":88},[54,57236,830],{"class":544},[54,57238,43446],{"class":548},[54,57240,545],{"class":544},[54,57242,109],{"class":88},[14,57244,305],{},[45,57246,57248],{"className":47,"code":57247,"language":49,"meta":50,"style":50},"Working...\nWorking...\nWorking...\nElapsed time: 1.5008 seconds\n",[17,57249,57250,57257,57263,57269],{"__ignoreMap":50},[54,57251,57252,57255],{"class":56,"line":57},[54,57253,57254],{"class":64},"Working",[54,57256,42467],{"class":4870},[54,57258,57259,57261],{"class":56,"line":68},[54,57260,57254],{"class":64},[54,57262,42467],{"class":4870},[54,57264,57265,57267],{"class":56,"line":75},[54,57266,57254],{"class":64},[54,57268,42467],{"class":4870},[54,57270,57271,57274,57276,57279],{"class":56,"line":112},[54,57272,57273],{"class":64},"Elapsed time",[54,57275,147],{"class":88},[54,57277,57278],{"class":99}," 1.5008",[54,57280,57281],{"class":64}," seconds\n",[14,57283,57284],{},"This works well for simple timing examples and quick checks.",[14,57286,57287,57288,89],{},"If you want to learn more about the module, see the ",[192,57289,56354],{"href":44116},[40,57291,57293],{"id":57292},"limits-beginners-should-know","Limits beginners should know",[14,57295,57296,57298],{},[17,57297,42440],{}," is useful, but it has some limits:",[149,57300,57301,57304,57307,57310],{},[152,57302,57303],{},"the result depends on your system clock",[152,57305,57306],{},"it is not the best tool for precise benchmarking",[152,57308,57309],{},"it does not give a human-readable date by itself",[152,57311,57312,57313],{},"it is not a replacement for delays like ",[192,57314,57315],{"href":42546},[17,57316,42549],{},[14,57318,57319],{},"So a good rule is:",[149,57321,57322,57327,57332],{},[152,57323,7449,57324,57326],{},[17,57325,42440],{}," for timestamps and simple elapsed-time checks",[152,57328,7449,57329,57331],{},[17,57330,1628],{}," for readable dates and times",[152,57333,7449,57334,57336],{},[17,57335,42549],{}," when you want to pause your program",[40,57338,978],{"id":977},[14,57340,45614,57341,89],{},[17,57342,42440],{},[602,57344,13304,57346,160],{"id":57345},"forgetting-to-import-the-time-module",[17,57347,2559],{},[14,57349,23006,57350,57352],{},[17,57351,2559],{}," means yet.",[45,57354,57356],{"className":47,"code":57355,"language":49,"meta":50,"style":50},"current = time.time()\nprint(current)\n",[17,57357,57358,57373],{"__ignoreMap":50},[54,57359,57360,57363,57365,57367,57369,57371],{"class":56,"line":57},[54,57361,57362],{"class":64},"current ",[54,57364,82],{"class":81},[54,57366,31045],{"class":64},[54,57368,89],{"class":88},[54,57370,2559],{"class":92},[54,57372,1670],{"class":88},[54,57374,57375,57377,57379,57382],{"class":56,"line":68},[54,57376,116],{"class":115},[54,57378,96],{"class":88},[54,57380,57381],{"class":92},"current",[54,57383,109],{"class":88},[14,57385,1031],{},[45,57387,57389],{"className":47,"code":57388,"language":49,"meta":50,"style":50},"import time\n\ncurrent = time.time()\nprint(current)\n",[17,57390,57391,57397,57401,57415],{"__ignoreMap":50},[54,57392,57393,57395],{"class":56,"line":57},[54,57394,61],{"class":60},[54,57396,31493],{"class":64},[54,57398,57399],{"class":56,"line":68},[54,57400,72],{"emptyLinePlaceholder":71},[54,57402,57403,57405,57407,57409,57411,57413],{"class":56,"line":75},[54,57404,57362],{"class":64},[54,57406,82],{"class":81},[54,57408,31045],{"class":64},[54,57410,89],{"class":88},[54,57412,2559],{"class":92},[54,57414,1670],{"class":88},[54,57416,57417,57419,57421,57423],{"class":56,"line":112},[54,57418,116],{"class":115},[54,57420,96],{"class":88},[54,57422,57381],{"class":92},[54,57424,109],{"class":88},[602,57426,57428],{"id":57427},"expecting-a-formatted-date-string","Expecting a formatted date string",[14,57430,2997],{},[45,57432,57433],{"className":47,"code":43680,"language":49,"meta":50,"style":50},[17,57434,57435,57441,57445],{"__ignoreMap":50},[54,57436,57437,57439],{"class":56,"line":57},[54,57438,61],{"class":60},[54,57440,31493],{"class":64},[54,57442,57443],{"class":56,"line":68},[54,57444,72],{"emptyLinePlaceholder":71},[54,57446,57447,57449,57451,57453,57455,57457],{"class":56,"line":75},[54,57448,116],{"class":115},[54,57450,96],{"class":88},[54,57452,2559],{"class":92},[54,57454,89],{"class":88},[54,57456,2559],{"class":92},[54,57458,1950],{"class":88},[14,57460,57461,57462,89],{},"returns a number, not something like ",[17,57463,56447],{},[14,57465,57466,57467,89],{},"If you need a readable date, use the ",[192,57468,1774],{"href":1773},[602,57470,20123,57472,5484,57474],{"id":57471},"confusing-timetime-with-timesleep",[17,57473,42440],{},[17,57475,42549],{},[14,57477,57478],{},"These functions do different jobs:",[149,57480,57481,57486],{},[152,57482,57483,57485],{},[17,57484,42440],{}," gets the current timestamp",[152,57487,57488,57490],{},[17,57489,42549],{}," pauses the program",[14,57492,253],{},[45,57494,57496],{"className":47,"code":57495,"language":49,"meta":50,"style":50},"import time\n\nprint(\"Before:\", time.time())\ntime.sleep(1)\nprint(\"After:\", time.time())\n",[17,57497,57498,57504,57508,57530,57544],{"__ignoreMap":50},[54,57499,57500,57502],{"class":56,"line":57},[54,57501,61],{"class":60},[54,57503,31493],{"class":64},[54,57505,57506],{"class":56,"line":68},[54,57507,72],{"emptyLinePlaceholder":71},[54,57509,57510,57512,57514,57516,57518,57520,57522,57524,57526,57528],{"class":56,"line":75},[54,57511,116],{"class":115},[54,57513,96],{"class":88},[54,57515,545],{"class":544},[54,57517,23993],{"class":548},[54,57519,545],{"class":544},[54,57521,103],{"class":88},[54,57523,31045],{"class":92},[54,57525,89],{"class":88},[54,57527,2559],{"class":92},[54,57529,1950],{"class":88},[54,57531,57532,57534,57536,57538,57540,57542],{"class":56,"line":112},[54,57533,2559],{"class":64},[54,57535,89],{"class":88},[54,57537,42410],{"class":92},[54,57539,96],{"class":88},[54,57541,100],{"class":99},[54,57543,109],{"class":88},[54,57545,57546,57548,57550,57552,57554,57556,57558,57560,57562,57564],{"class":56,"line":887},[54,57547,116],{"class":115},[54,57549,96],{"class":88},[54,57551,545],{"class":544},[54,57553,24043],{"class":548},[54,57555,545],{"class":544},[54,57557,103],{"class":88},[54,57559,31045],{"class":92},[54,57561,89],{"class":88},[54,57563,2559],{"class":92},[54,57565,1950],{"class":88},[602,57567,57569,57570,57573,57574],{"id":57568},"calling-time-directly-after-import-time","Calling ",[17,57571,57572],{},"time()"," directly after ",[17,57575,43068],{},[14,57577,10192],{},[45,57579,57580],{"className":47,"code":56763,"language":49,"meta":50,"style":50},[17,57581,57582,57588,57592],{"__ignoreMap":50},[54,57583,57584,57586],{"class":56,"line":57},[54,57585,61],{"class":60},[54,57587,31493],{"class":64},[54,57589,57590],{"class":56,"line":68},[54,57591,72],{"emptyLinePlaceholder":71},[54,57593,57594,57596,57598,57600],{"class":56,"line":75},[54,57595,116],{"class":115},[54,57597,96],{"class":88},[54,57599,2559],{"class":92},[54,57601,1950],{"class":88},[14,57603,57604],{},"This is correct:",[45,57606,57607],{"className":47,"code":43680,"language":49,"meta":50,"style":50},[17,57608,57609,57615,57619],{"__ignoreMap":50},[54,57610,57611,57613],{"class":56,"line":57},[54,57612,61],{"class":60},[54,57614,31493],{"class":64},[54,57616,57617],{"class":56,"line":68},[54,57618,72],{"emptyLinePlaceholder":71},[54,57620,57621,57623,57625,57627,57629,57631],{"class":56,"line":75},[54,57622,116],{"class":115},[54,57624,96],{"class":88},[54,57626,2559],{"class":92},[54,57628,89],{"class":88},[54,57630,2559],{"class":92},[54,57632,1950],{"class":88},[602,57634,57636],{"id":57635},"rounding-too-early","Rounding too early",[14,57638,57639,57640,57642],{},"If you round or convert to ",[17,57641,20065],{}," too soon, you may lose useful timing detail.",[14,57644,57645],{},"Less precise:",[45,57647,57649],{"className":47,"code":57648,"language":49,"meta":50,"style":50},"import time\n\nstart = int(time.time())\ntime.sleep(1.2)\nend = int(time.time())\n\nprint(end - start)\n",[17,57650,57651,57657,57661,57679,57694,57712,57716],{"__ignoreMap":50},[54,57652,57653,57655],{"class":56,"line":57},[54,57654,61],{"class":60},[54,57656,31493],{"class":64},[54,57658,57659],{"class":56,"line":68},[54,57660,72],{"emptyLinePlaceholder":71},[54,57662,57663,57665,57667,57669,57671,57673,57675,57677],{"class":56,"line":75},[54,57664,43102],{"class":64},[54,57666,82],{"class":81},[54,57668,41963],{"class":429},[54,57670,96],{"class":88},[54,57672,2559],{"class":92},[54,57674,89],{"class":88},[54,57676,2559],{"class":92},[54,57678,1950],{"class":88},[54,57680,57681,57683,57685,57687,57689,57692],{"class":56,"line":112},[54,57682,2559],{"class":64},[54,57684,89],{"class":88},[54,57686,42410],{"class":92},[54,57688,96],{"class":88},[54,57690,57691],{"class":99},"1.2",[54,57693,109],{"class":88},[54,57695,57696,57698,57700,57702,57704,57706,57708,57710],{"class":56,"line":887},[54,57697,43159],{"class":64},[54,57699,82],{"class":81},[54,57701,41963],{"class":429},[54,57703,96],{"class":88},[54,57705,2559],{"class":92},[54,57707,89],{"class":88},[54,57709,2559],{"class":92},[54,57711,1950],{"class":88},[54,57713,57714],{"class":56,"line":892},[54,57715,72],{"emptyLinePlaceholder":71},[54,57717,57718,57720,57722,57724,57726,57728],{"class":56,"line":913},[54,57719,116],{"class":115},[54,57721,96],{"class":88},[54,57723,43159],{"class":92},[54,57725,1467],{"class":81},[54,57727,43130],{"class":92},[54,57729,109],{"class":88},[14,57731,57732],{},"More precise:",[45,57734,57736],{"className":47,"code":57735,"language":49,"meta":50,"style":50},"import time\n\nstart = time.time()\ntime.sleep(1.2)\nend = time.time()\n\nprint(end - start)\n",[17,57737,57738,57744,57748,57762,57776,57790,57794],{"__ignoreMap":50},[54,57739,57740,57742],{"class":56,"line":57},[54,57741,61],{"class":60},[54,57743,31493],{"class":64},[54,57745,57746],{"class":56,"line":68},[54,57747,72],{"emptyLinePlaceholder":71},[54,57749,57750,57752,57754,57756,57758,57760],{"class":56,"line":75},[54,57751,43102],{"class":64},[54,57753,82],{"class":81},[54,57755,31045],{"class":64},[54,57757,89],{"class":88},[54,57759,2559],{"class":92},[54,57761,1670],{"class":88},[54,57763,57764,57766,57768,57770,57772,57774],{"class":56,"line":112},[54,57765,2559],{"class":64},[54,57767,89],{"class":88},[54,57769,42410],{"class":92},[54,57771,96],{"class":88},[54,57773,57691],{"class":99},[54,57775,109],{"class":88},[54,57777,57778,57780,57782,57784,57786,57788],{"class":56,"line":887},[54,57779,43159],{"class":64},[54,57781,82],{"class":81},[54,57783,31045],{"class":64},[54,57785,89],{"class":88},[54,57787,2559],{"class":92},[54,57789,1670],{"class":88},[54,57791,57792],{"class":56,"line":892},[54,57793,72],{"emptyLinePlaceholder":71},[54,57795,57796,57798,57800,57802,57804,57806],{"class":56,"line":913},[54,57797,116],{"class":115},[54,57799,96],{"class":88},[54,57801,43159],{"class":92},[54,57803,1467],{"class":81},[54,57805,43130],{"class":92},[54,57807,109],{"class":88},[40,57809,57811,57812],{"id":57810},"useful-commands-to-test-timetime","Useful commands to test ",[17,57813,42440],{},[14,57815,57816,57817,57819],{},"You can test ",[17,57818,42440],{}," quickly from your terminal.",[14,57821,57822],{},"Check your Python version:",[45,57824,57826],{"className":15497,"code":57825,"language":15499,"meta":50,"style":50},"python --version\n",[17,57827,57828],{"__ignoreMap":50},[54,57829,57830,57832],{"class":56,"line":57},[54,57831,49],{"class":10811},[54,57833,35879],{"class":24323},[14,57835,57836],{},"Print the current timestamp:",[45,57838,57840],{"className":15497,"code":57839,"language":15499,"meta":50,"style":50},"python -c \"import time; print(time.time())\"\n",[17,57841,57842],{"__ignoreMap":50},[54,57843,57844,57846,57848,57850,57852],{"class":56,"line":57},[54,57845,49],{"class":10811},[54,57847,26050],{"class":24323},[54,57849,830],{"class":544},[54,57851,43972],{"class":548},[54,57853,5239],{"class":544},[14,57855,57856],{},"Measure about 1 second of sleep:",[45,57858,57860],{"className":15497,"code":57859,"language":15499,"meta":50,"style":50},"python -c \"import time; start=time.time(); time.sleep(1); print(time.time()-start)\"\n",[17,57861,57862],{"__ignoreMap":50},[54,57863,57864,57866,57868,57870,57873],{"class":56,"line":57},[54,57865,49],{"class":10811},[54,57867,26050],{"class":24323},[54,57869,830],{"class":544},[54,57871,57872],{"class":548},"import time; start=time.time(); time.sleep(1); print(time.time()-start)",[54,57874,5239],{"class":544},[14,57876,57877],{},"Check the return type:",[45,57879,57881],{"className":15497,"code":57880,"language":15499,"meta":50,"style":50},"python -c \"import time; print(type(time.time()))\"\n",[17,57882,57883],{"__ignoreMap":50},[54,57884,57885,57887,57889,57891,57894],{"class":56,"line":57},[54,57886,49],{"class":10811},[54,57888,26050],{"class":24323},[54,57890,830],{"class":544},[54,57892,57893],{"class":548},"import time; print(type(time.time()))",[54,57895,5239],{"class":544},[14,57897,57898,57899,57902],{},"If you are new to troubleshooting, the ",[192,57900,57901],{"href":54975},"how to debug Python code beginner guide"," is a good next step.",[40,57904,1419],{"id":1418},[602,57906,3390,57908,57910],{"id":57907},"what-does-timetime-return-in-python",[17,57909,42440],{}," return in Python?",[14,57912,57913],{},"It returns the current time as a float representing seconds since the Unix epoch.",[602,57915,3403,57917,57919],{"id":57916},"why-does-timetime-return-a-large-number",[17,57918,42440],{}," return a large number?",[14,57921,57922],{},"Because it counts seconds from January 1, 1970 UTC to the current moment.",[602,57924,3449,57926,57928],{"id":57925},"is-timetime-good-for-measuring-code-speed",[17,57927,42440],{}," good for measuring code speed?",[14,57930,57931],{},"It is fine for simple timing, but more precise tools are better for benchmarking.",[602,57933,57935,57936,3409],{"id":57934},"how-do-i-get-whole-seconds-from-timetime","How do I get whole seconds from ",[17,57937,42440],{},[14,57939,27808,57940,57942],{},[17,57941,20065],{}," if you only want the integer part.",[45,57944,57946],{"className":47,"code":57945,"language":49,"meta":50,"style":50},"import time\n\nprint(int(time.time()))\n",[17,57947,57948,57954,57958],{"__ignoreMap":50},[54,57949,57950,57952],{"class":56,"line":57},[54,57951,61],{"class":60},[54,57953,31493],{"class":64},[54,57955,57956],{"class":56,"line":68},[54,57957,72],{"emptyLinePlaceholder":71},[54,57959,57960,57962,57964,57966,57968,57970,57972,57974],{"class":56,"line":75},[54,57961,116],{"class":115},[54,57963,96],{"class":88},[54,57965,9657],{"class":429},[54,57967,96],{"class":88},[54,57969,2559],{"class":92},[54,57971,89],{"class":88},[54,57973,2559],{"class":92},[54,57975,57976],{"class":88},"()))\n",[40,57978,1510],{"id":1509},[149,57980,57981,57985,57990,57994,57998,58003],{},[152,57982,57983],{},[192,57984,56354],{"href":44116},[152,57986,57987],{},[192,57988,57989],{"href":42546},"time.sleep() function explained",[152,57991,57992],{},[192,57993,1774],{"href":1773},[152,57995,57996],{},[192,57997,5052],{"href":3535},[152,57999,58000],{},[192,58001,58002],{"href":54975},"How to debug Python code: beginner guide",[152,58004,58005],{},[192,58006,13760,58007,12898],{"href":42022},[17,58008,20065],{},[14,58010,126,58011,58013,58014,58016,58017,89],{},[17,58012,42440],{}," when you need a timestamp or a simple elapsed-time check. If you need readable dates, move to ",[17,58015,1628],{},". If you need to pause your program, use ",[17,58018,42549],{},[1545,58020,58021],{},"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 .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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":68,"depth":68,"links":58023},[58024,58025,58027,58028,58029,58030,58032,58033,58043,58045,58055],{"id":3595,"depth":68,"text":3596},{"id":56576,"depth":68,"text":58026},"What time.time() does",{"id":198,"depth":68,"text":199},{"id":22596,"depth":68,"text":22597},{"id":56891,"depth":68,"text":56892},{"id":57076,"depth":68,"text":58031},"Using time.time() to measure elapsed time",{"id":57292,"depth":68,"text":57293},{"id":977,"depth":68,"text":978,"children":58034},[58035,58037,58038,58040,58042],{"id":57345,"depth":75,"text":58036},"Forgetting to import the time module",{"id":57427,"depth":75,"text":57428},{"id":57471,"depth":75,"text":58039},"Confusing time.time() with time.sleep()",{"id":57568,"depth":75,"text":58041},"Calling time() directly after import time",{"id":57635,"depth":75,"text":57636},{"id":57810,"depth":68,"text":58044},"Useful commands to test time.time()",{"id":1418,"depth":68,"text":1419,"children":58046},[58047,58049,58051,58053],{"id":57907,"depth":75,"text":58048},"What does time.time() return in Python?",{"id":57916,"depth":75,"text":58050},"Why does time.time() return a large number?",{"id":57925,"depth":75,"text":58052},"Is time.time() good for measuring code speed?",{"id":57934,"depth":75,"text":58054},"How do I get whole seconds from time.time()?",{"id":1509,"depth":68,"text":1510},"Master time.time function explained in our comprehensive Python beginner guide.",{},{"title":56411,"description":58056},"standard-library\u002Ftime.time-function-explained","tBvmudHhfLdisypsNQJljwOze5fjFNRKRBJoEqauyUI",1776944736615]