[{"data":1,"prerenderedAt":1931},["ShallowReactive",2],{"doc-\u002Fstandard-library\u002Fpython-time-module-overview":3},{"id":4,"title":5,"body":6,"description":1924,"extension":1925,"meta":1926,"navigation":82,"path":1927,"seo":1928,"stem":1929,"__hash__":1930},"content\u002Fstandard-library\u002Fpython-time-module-overview.md","Python time Module Overview",{"type":7,"value":8,"toc":1883},"minimark",[9,13,22,25,38,50,55,160,163,177,180,204,208,213,216,227,230,241,248,260,264,270,273,295,301,312,316,322,328,331,376,383,389,392,452,455,500,506,509,560,562,592,598,601,690,693,699,702,779,781,818,822,825,846,849,992,995,1009,1013,1018,1022,1081,1085,1088,1230,1234,1237,1325,1329,1367,1371,1374,1379,1389,1393,1402,1405,1424,1428,1433,1439,1445,1464,1467,1496,1502,1509,1542,1544,1573,1580,1585,1613,1621,1650,1660,1668,1675,1680,1739,1743,1746,1803,1807,1811,1817,1821,1824,1828,1831,1835,1843,1847,1854,1858,1879],[10,11,5],"h1",{"id":12},"python-time-module-overview",[14,15,16,17,21],"p",{},"The ",[18,19,20],"code",{},"time"," module is a built-in Python module for simple time-related tasks.",[14,23,24],{},"Beginners usually use it for:",[26,27,28,32,35],"ul",{},[29,30,31],"li",{},"getting the current timestamp",[29,33,34],{},"pausing a program",[29,36,37],{},"measuring how long code takes to run",[14,39,40,41,49],{},"It is a good starting point when you need something simple. If you need readable dates, calendar values, or date math, the ",[42,43,45,48],"a",{"href":44},"\u002Fstandard-library\u002Fpython-datetime-module-overview\u002F",[18,46,47],{},"datetime"," module overview"," is usually a better fit.",[51,52,54],"h2",{"id":53},"quick-example","Quick example",[56,57,62],"pre",{"className":58,"code":59,"language":60,"meta":61,"style":61},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import time\n\nprint(time.time())\nprint(\"Waiting...\")\ntime.sleep(2)\nprint(\"Done\")\n","python","",[18,63,64,77,84,106,126,144],{"__ignoreMap":61},[65,66,69,73],"span",{"class":67,"line":68},"line",1,[65,70,72],{"class":71},"sVHd0","import",[65,74,76],{"class":75},"su5hD"," time\n",[65,78,80],{"class":67,"line":79},2,[65,81,83],{"emptyLinePlaceholder":82},true,"\n",[65,85,87,91,95,98,101,103],{"class":67,"line":86},3,[65,88,90],{"class":89},"sptTA","print",[65,92,94],{"class":93},"sP7_E","(",[65,96,20],{"class":97},"slqww",[65,99,100],{"class":93},".",[65,102,20],{"class":97},[65,104,105],{"class":93},"())\n",[65,107,109,111,113,117,121,123],{"class":67,"line":108},4,[65,110,90],{"class":89},[65,112,94],{"class":93},[65,114,116],{"class":115},"sjJ54","\"",[65,118,120],{"class":119},"s_sjI","Waiting...",[65,122,116],{"class":115},[65,124,125],{"class":93},")\n",[65,127,129,131,133,136,138,142],{"class":67,"line":128},5,[65,130,20],{"class":75},[65,132,100],{"class":93},[65,134,135],{"class":97},"sleep",[65,137,94],{"class":93},[65,139,141],{"class":140},"srdBf","2",[65,143,125],{"class":93},[65,145,147,149,151,153,156,158],{"class":67,"line":146},6,[65,148,90],{"class":89},[65,150,94],{"class":93},[65,152,116],{"class":115},[65,154,155],{"class":119},"Done",[65,157,116],{"class":115},[65,159,125],{"class":93},[14,161,162],{},"What this does:",[26,164,165,171],{},[29,166,167,170],{},[18,168,169],{},"time.time()"," gets the current Unix timestamp",[29,172,173,176],{},[18,174,175],{},"time.sleep(2)"," pauses the whole program for 2 seconds",[14,178,179],{},"Example output:",[56,181,183],{"className":58,"code":182,"language":60,"meta":61,"style":61},"1712345678.123456\nWaiting...\nDone\n",[18,184,185,190,199],{"__ignoreMap":61},[65,186,187],{"class":67,"line":68},[65,188,189],{"class":140},"1712345678.123456\n",[65,191,192,195],{"class":67,"line":79},[65,193,194],{"class":75},"Waiting",[65,196,198],{"class":197},"s_hVV","...\n",[65,200,201],{"class":67,"line":86},[65,202,203],{"class":75},"Done\n",[51,205,207],{"id":206},"what-the-time-module-is","What the time module is",[14,209,16,210,212],{},[18,211,20],{}," module is built into Python, so you do not need to install anything.",[14,214,215],{},"It helps with:",[26,217,218,221,224],{},[29,219,220],{},"timestamps",[29,222,223],{},"delays",[29,225,226],{},"simple timing tasks",[14,228,229],{},"This makes it useful when you want to:",[26,231,232,235,238],{},[29,233,234],{},"pause code for a short time",[29,236,237],{},"measure how long something takes",[29,239,240],{},"get a numeric current time value",[14,242,16,243,245,246,100],{},[18,244,20],{}," module is different from ",[18,247,47],{},[26,249,250,255],{},[29,251,252,254],{},[18,253,20],{}," is better for delays and simple timestamps",[29,256,257,259],{},[18,258,47],{}," is better for readable dates like year, month, and day",[51,261,263],{"id":262},"when-to-use-the-time-module","When to use the time module",[14,265,266,267,269],{},"Use the ",[18,268,20],{}," module when you need simple time features.",[14,271,272],{},"Common cases:",[26,274,275,284,292],{},[29,276,277,278],{},"Pause a program with ",[42,279,281],{"href":280},"\u002Fstandard-library\u002Ftime.sleep-function-explained",[18,282,283],{},"time.sleep()",[29,285,286,287],{},"Get the current timestamp with ",[42,288,290],{"href":289},"\u002Fstandard-library\u002Ftime.time-function-explained",[18,291,169],{},[29,293,294],{},"Measure how long code takes to run",[14,296,297,298,300],{},"Use ",[18,299,47],{}," instead when you need:",[26,302,303,306,309],{},[29,304,305],{},"readable dates",[29,307,308],{},"year, month, and day values",[29,310,311],{},"date calculations",[51,313,315],{"id":314},"functions-beginners-should-know-first","Functions beginners should know first",[14,317,318,319,321],{},"Here are the most useful ",[18,320,20],{}," module functions to learn first.",[323,324,326],"h3",{"id":325},"timetime",[18,327,169],{},[14,329,330],{},"Returns the current time as the number of seconds since the Unix epoch.",[56,332,334],{"className":58,"code":333,"language":60,"meta":61,"style":61},"import time\n\ncurrent_timestamp = time.time()\nprint(current_timestamp)\n",[18,335,336,342,346,365],{"__ignoreMap":61},[65,337,338,340],{"class":67,"line":68},[65,339,72],{"class":71},[65,341,76],{"class":75},[65,343,344],{"class":67,"line":79},[65,345,83],{"emptyLinePlaceholder":82},[65,347,348,351,355,358,360,362],{"class":67,"line":86},[65,349,350],{"class":75},"current_timestamp ",[65,352,354],{"class":353},"smGrS","=",[65,356,357],{"class":75}," time",[65,359,100],{"class":93},[65,361,20],{"class":97},[65,363,364],{"class":93},"()\n",[65,366,367,369,371,374],{"class":67,"line":108},[65,368,90],{"class":89},[65,370,94],{"class":93},[65,372,373],{"class":97},"current_timestamp",[65,375,125],{"class":93},[14,377,378,379,382],{},"This returns a ",[18,380,381],{},"float",", not a formatted date string.",[323,384,386],{"id":385},"timesleepseconds",[18,387,388],{},"time.sleep(seconds)",[14,390,391],{},"Pauses the whole program for the number of seconds you give it.",[56,393,395],{"className":58,"code":394,"language":60,"meta":61,"style":61},"import time\n\nprint(\"Start\")\ntime.sleep(1)\nprint(\"One second later\")\n",[18,396,397,403,407,422,437],{"__ignoreMap":61},[65,398,399,401],{"class":67,"line":68},[65,400,72],{"class":71},[65,402,76],{"class":75},[65,404,405],{"class":67,"line":79},[65,406,83],{"emptyLinePlaceholder":82},[65,408,409,411,413,415,418,420],{"class":67,"line":86},[65,410,90],{"class":89},[65,412,94],{"class":93},[65,414,116],{"class":115},[65,416,417],{"class":119},"Start",[65,419,116],{"class":115},[65,421,125],{"class":93},[65,423,424,426,428,430,432,435],{"class":67,"line":108},[65,425,20],{"class":75},[65,427,100],{"class":93},[65,429,135],{"class":97},[65,431,94],{"class":93},[65,433,434],{"class":140},"1",[65,436,125],{"class":93},[65,438,439,441,443,445,448,450],{"class":67,"line":128},[65,440,90],{"class":89},[65,442,94],{"class":93},[65,444,116],{"class":115},[65,446,447],{"class":119},"One second later",[65,449,116],{"class":115},[65,451,125],{"class":93},[14,453,454],{},"You can also use decimal values:",[56,456,458],{"className":58,"code":457,"language":60,"meta":61,"style":61},"import time\n\ntime.sleep(0.5)\nprint(\"Half a second passed\")\n",[18,459,460,466,470,485],{"__ignoreMap":61},[65,461,462,464],{"class":67,"line":68},[65,463,72],{"class":71},[65,465,76],{"class":75},[65,467,468],{"class":67,"line":79},[65,469,83],{"emptyLinePlaceholder":82},[65,471,472,474,476,478,480,483],{"class":67,"line":86},[65,473,20],{"class":75},[65,475,100],{"class":93},[65,477,135],{"class":97},[65,479,94],{"class":93},[65,481,482],{"class":140},"0.5",[65,484,125],{"class":93},[65,486,487,489,491,493,496,498],{"class":67,"line":108},[65,488,90],{"class":89},[65,490,94],{"class":93},[65,492,116],{"class":115},[65,494,495],{"class":119},"Half a second passed",[65,497,116],{"class":115},[65,499,125],{"class":93},[323,501,503],{"id":502},"timectime",[18,504,505],{},"time.ctime()",[14,507,508],{},"Turns a timestamp into a readable string.",[56,510,512],{"className":58,"code":511,"language":60,"meta":61,"style":61},"import time\n\ntimestamp = time.time()\nprint(time.ctime(timestamp))\n",[18,513,514,520,524,539],{"__ignoreMap":61},[65,515,516,518],{"class":67,"line":68},[65,517,72],{"class":71},[65,519,76],{"class":75},[65,521,522],{"class":67,"line":79},[65,523,83],{"emptyLinePlaceholder":82},[65,525,526,529,531,533,535,537],{"class":67,"line":86},[65,527,528],{"class":75},"timestamp ",[65,530,354],{"class":353},[65,532,357],{"class":75},[65,534,100],{"class":93},[65,536,20],{"class":97},[65,538,364],{"class":93},[65,540,541,543,545,547,549,552,554,557],{"class":67,"line":108},[65,542,90],{"class":89},[65,544,94],{"class":93},[65,546,20],{"class":97},[65,548,100],{"class":93},[65,550,551],{"class":97},"ctime",[65,553,94],{"class":93},[65,555,556],{"class":97},"timestamp",[65,558,559],{"class":93},"))\n",[14,561,179],{},[56,563,565],{"className":58,"code":564,"language":60,"meta":61,"style":61},"Fri Apr  5 12:34:56 2024\n",[18,566,567],{"__ignoreMap":61},[65,568,569,572,575,578,581,584,586,589],{"class":67,"line":68},[65,570,571],{"class":75},"Fri Apr  ",[65,573,574],{"class":140},"5",[65,576,577],{"class":140}," 12",[65,579,580],{"class":93},":",[65,582,583],{"class":140},"34",[65,585,580],{"class":93},[65,587,588],{"class":140},"56",[65,590,591],{"class":140}," 2024\n",[323,593,595],{"id":594},"timelocaltime",[18,596,597],{},"time.localtime()",[14,599,600],{},"Converts a timestamp into a structured local time value.",[56,602,604],{"className":58,"code":603,"language":60,"meta":61,"style":61},"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",[18,605,606,612,616,632,643,659,674],{"__ignoreMap":61},[65,607,608,610],{"class":67,"line":68},[65,609,72],{"class":71},[65,611,76],{"class":75},[65,613,614],{"class":67,"line":79},[65,615,83],{"emptyLinePlaceholder":82},[65,617,618,621,623,625,627,630],{"class":67,"line":86},[65,619,620],{"class":75},"current_time ",[65,622,354],{"class":353},[65,624,357],{"class":75},[65,626,100],{"class":93},[65,628,629],{"class":97},"localtime",[65,631,364],{"class":93},[65,633,634,636,638,641],{"class":67,"line":108},[65,635,90],{"class":89},[65,637,94],{"class":93},[65,639,640],{"class":97},"current_time",[65,642,125],{"class":93},[65,644,645,647,649,651,653,657],{"class":67,"line":128},[65,646,90],{"class":89},[65,648,94],{"class":93},[65,650,640],{"class":97},[65,652,100],{"class":93},[65,654,656],{"class":655},"skxfh","tm_year",[65,658,125],{"class":93},[65,660,661,663,665,667,669,672],{"class":67,"line":146},[65,662,90],{"class":89},[65,664,94],{"class":93},[65,666,640],{"class":97},[65,668,100],{"class":93},[65,670,671],{"class":655},"tm_mon",[65,673,125],{"class":93},[65,675,677,679,681,683,685,688],{"class":67,"line":676},7,[65,678,90],{"class":89},[65,680,94],{"class":93},[65,682,640],{"class":97},[65,684,100],{"class":93},[65,686,687],{"class":655},"tm_mday",[65,689,125],{"class":93},[14,691,692],{},"This is useful when you want parts of the local date and time.",[323,694,696],{"id":695},"timestrftime",[18,697,698],{},"time.strftime()",[14,700,701],{},"Formats a time value as text.",[56,703,705],{"className":58,"code":704,"language":60,"meta":61,"style":61},"import time\n\ncurrent_time = time.localtime()\nformatted = time.strftime(\"%Y-%m-%d %H:%M:%S\", current_time)\nprint(formatted)\n",[18,706,707,713,717,731,768],{"__ignoreMap":61},[65,708,709,711],{"class":67,"line":68},[65,710,72],{"class":71},[65,712,76],{"class":75},[65,714,715],{"class":67,"line":79},[65,716,83],{"emptyLinePlaceholder":82},[65,718,719,721,723,725,727,729],{"class":67,"line":86},[65,720,620],{"class":75},[65,722,354],{"class":353},[65,724,357],{"class":75},[65,726,100],{"class":93},[65,728,629],{"class":97},[65,730,364],{"class":93},[65,732,733,736,738,740,742,745,747,749,752,755,758,760,763,766],{"class":67,"line":108},[65,734,735],{"class":75},"formatted ",[65,737,354],{"class":353},[65,739,357],{"class":75},[65,741,100],{"class":93},[65,743,744],{"class":97},"strftime",[65,746,94],{"class":93},[65,748,116],{"class":115},[65,750,751],{"class":119},"%Y-%m-",[65,753,754],{"class":140},"%d",[65,756,757],{"class":119}," %H:%M:%S",[65,759,116],{"class":115},[65,761,762],{"class":93},",",[65,764,765],{"class":97}," current_time",[65,767,125],{"class":93},[65,769,770,772,774,777],{"class":67,"line":128},[65,771,90],{"class":89},[65,773,94],{"class":93},[65,775,776],{"class":97},"formatted",[65,778,125],{"class":93},[14,780,179],{},[56,782,784],{"className":58,"code":783,"language":60,"meta":61,"style":61},"2024-04-05 12:34:56\n",[18,785,786],{"__ignoreMap":61},[65,787,788,791,794,797,801,803,805,807,809,811,813,815],{"class":67,"line":68},[65,789,790],{"class":140},"2024",[65,792,793],{"class":353},"-",[65,795,796],{"class":140},"0",[65,798,800],{"class":799},"sx12J","4",[65,802,793],{"class":353},[65,804,796],{"class":140},[65,806,574],{"class":799},[65,808,577],{"class":140},[65,810,580],{"class":93},[65,812,583],{"class":140},[65,814,580],{"class":93},[65,816,817],{"class":140},"56\n",[51,819,821],{"id":820},"simple-example-flow","Simple example flow",[14,823,824],{},"A common beginner workflow looks like this:",[826,827,828,834,838,843],"ol",{},[29,829,830,831],{},"Import the module with ",[18,832,833],{},"import time",[29,835,286,836],{},[18,837,169],{},[29,839,840,841],{},"Pause execution with ",[18,842,283],{},[29,844,845],{},"Format the output only if you need readable text",[14,847,848],{},"Example:",[56,850,852],{"className":58,"code":851,"language":60,"meta":61,"style":61},"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",[18,853,854,860,864,879,899,903,917,921,937,958,963],{"__ignoreMap":61},[65,855,856,858],{"class":67,"line":68},[65,857,72],{"class":71},[65,859,76],{"class":75},[65,861,862],{"class":67,"line":79},[65,863,83],{"emptyLinePlaceholder":82},[65,865,866,869,871,873,875,877],{"class":67,"line":86},[65,867,868],{"class":75},"start ",[65,870,354],{"class":353},[65,872,357],{"class":75},[65,874,100],{"class":93},[65,876,20],{"class":97},[65,878,364],{"class":93},[65,880,881,883,885,887,890,892,894,897],{"class":67,"line":108},[65,882,90],{"class":89},[65,884,94],{"class":93},[65,886,116],{"class":115},[65,888,889],{"class":119},"Start timestamp:",[65,891,116],{"class":115},[65,893,762],{"class":93},[65,895,896],{"class":97}," start",[65,898,125],{"class":93},[65,900,901],{"class":67,"line":128},[65,902,83],{"emptyLinePlaceholder":82},[65,904,905,907,909,911,913,915],{"class":67,"line":146},[65,906,20],{"class":75},[65,908,100],{"class":93},[65,910,135],{"class":97},[65,912,94],{"class":93},[65,914,141],{"class":140},[65,916,125],{"class":93},[65,918,919],{"class":67,"line":676},[65,920,83],{"emptyLinePlaceholder":82},[65,922,924,927,929,931,933,935],{"class":67,"line":923},8,[65,925,926],{"class":75},"end ",[65,928,354],{"class":353},[65,930,357],{"class":75},[65,932,100],{"class":93},[65,934,20],{"class":97},[65,936,364],{"class":93},[65,938,940,942,944,946,949,951,953,956],{"class":67,"line":939},9,[65,941,90],{"class":89},[65,943,94],{"class":93},[65,945,116],{"class":115},[65,947,948],{"class":119},"End timestamp:",[65,950,116],{"class":115},[65,952,762],{"class":93},[65,954,955],{"class":97}," end",[65,957,125],{"class":93},[65,959,961],{"class":67,"line":960},10,[65,962,83],{"emptyLinePlaceholder":82},[65,964,966,968,970,972,975,977,979,981,983,985,987,990],{"class":67,"line":965},11,[65,967,90],{"class":89},[65,969,94],{"class":93},[65,971,116],{"class":115},[65,973,974],{"class":119},"Readable end time:",[65,976,116],{"class":115},[65,978,762],{"class":93},[65,980,357],{"class":97},[65,982,100],{"class":93},[65,984,551],{"class":97},[65,986,94],{"class":93},[65,988,989],{"class":97},"end",[65,991,559],{"class":93},[14,993,994],{},"This example:",[26,996,997,1000,1003,1006],{},[29,998,999],{},"gets a start timestamp",[29,1001,1002],{},"waits 2 seconds",[29,1004,1005],{},"gets an end timestamp",[29,1007,1008],{},"shows the end time in a readable format",[51,1010,1012],{"id":1011},"common-beginner-use-cases","Common beginner use cases",[14,1014,16,1015,1017],{},[18,1016,20],{}," module is often used for small, practical tasks.",[323,1019,1021],{"id":1020},"adding-a-delay-in-a-script","Adding a delay in a script",[56,1023,1025],{"className":58,"code":1024,"language":60,"meta":61,"style":61},"import time\n\nprint(\"Loading...\")\ntime.sleep(2)\nprint(\"Finished\")\n",[18,1026,1027,1033,1037,1052,1066],{"__ignoreMap":61},[65,1028,1029,1031],{"class":67,"line":68},[65,1030,72],{"class":71},[65,1032,76],{"class":75},[65,1034,1035],{"class":67,"line":79},[65,1036,83],{"emptyLinePlaceholder":82},[65,1038,1039,1041,1043,1045,1048,1050],{"class":67,"line":86},[65,1040,90],{"class":89},[65,1042,94],{"class":93},[65,1044,116],{"class":115},[65,1046,1047],{"class":119},"Loading...",[65,1049,116],{"class":115},[65,1051,125],{"class":93},[65,1053,1054,1056,1058,1060,1062,1064],{"class":67,"line":108},[65,1055,20],{"class":75},[65,1057,100],{"class":93},[65,1059,135],{"class":97},[65,1061,94],{"class":93},[65,1063,141],{"class":140},[65,1065,125],{"class":93},[65,1067,1068,1070,1072,1074,1077,1079],{"class":67,"line":128},[65,1069,90],{"class":89},[65,1071,94],{"class":93},[65,1073,116],{"class":115},[65,1075,1076],{"class":119},"Finished",[65,1078,116],{"class":115},[65,1080,125],{"class":93},[323,1082,1084],{"id":1083},"timing-how-long-a-task-takes","Timing how long a task takes",[14,1086,1087],{},"For a step-by-step guide, see how to measure execution time in Python.",[56,1089,1091],{"className":58,"code":1090,"language":60,"meta":61,"style":61},"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",[18,1092,1093,1099,1103,1117,1121,1143,1159,1174,1178,1192,1196],{"__ignoreMap":61},[65,1094,1095,1097],{"class":67,"line":68},[65,1096,72],{"class":71},[65,1098,76],{"class":75},[65,1100,1101],{"class":67,"line":79},[65,1102,83],{"emptyLinePlaceholder":82},[65,1104,1105,1107,1109,1111,1113,1115],{"class":67,"line":86},[65,1106,868],{"class":75},[65,1108,354],{"class":353},[65,1110,357],{"class":75},[65,1112,100],{"class":93},[65,1114,20],{"class":97},[65,1116,364],{"class":93},[65,1118,1119],{"class":67,"line":108},[65,1120,83],{"emptyLinePlaceholder":82},[65,1122,1123,1126,1129,1132,1135,1137,1140],{"class":67,"line":128},[65,1124,1125],{"class":71},"for",[65,1127,1128],{"class":75}," i ",[65,1130,1131],{"class":71},"in",[65,1133,1134],{"class":89}," range",[65,1136,94],{"class":93},[65,1138,1139],{"class":140},"3",[65,1141,1142],{"class":93},"):\n",[65,1144,1145,1148,1150,1152,1155,1157],{"class":67,"line":146},[65,1146,1147],{"class":89},"    print",[65,1149,94],{"class":93},[65,1151,116],{"class":115},[65,1153,1154],{"class":119},"Working...",[65,1156,116],{"class":115},[65,1158,125],{"class":93},[65,1160,1161,1164,1166,1168,1170,1172],{"class":67,"line":676},[65,1162,1163],{"class":75},"    time",[65,1165,100],{"class":93},[65,1167,135],{"class":97},[65,1169,94],{"class":93},[65,1171,434],{"class":140},[65,1173,125],{"class":93},[65,1175,1176],{"class":67,"line":923},[65,1177,83],{"emptyLinePlaceholder":82},[65,1179,1180,1182,1184,1186,1188,1190],{"class":67,"line":939},[65,1181,926],{"class":75},[65,1183,354],{"class":353},[65,1185,357],{"class":75},[65,1187,100],{"class":93},[65,1189,20],{"class":97},[65,1191,364],{"class":93},[65,1193,1194],{"class":67,"line":960},[65,1195,83],{"emptyLinePlaceholder":82},[65,1197,1198,1200,1202,1204,1207,1209,1211,1214,1216,1218,1220,1223,1226,1228],{"class":67,"line":965},[65,1199,90],{"class":89},[65,1201,94],{"class":93},[65,1203,116],{"class":115},[65,1205,1206],{"class":119},"Duration:",[65,1208,116],{"class":115},[65,1210,762],{"class":93},[65,1212,1213],{"class":97}," end ",[65,1215,793],{"class":353},[65,1217,896],{"class":97},[65,1219,762],{"class":93},[65,1221,1222],{"class":115}," \"",[65,1224,1225],{"class":119},"seconds",[65,1227,116],{"class":115},[65,1229,125],{"class":93},[323,1231,1233],{"id":1232},"creating-a-simple-countdown-or-timer","Creating a simple countdown or timer",[14,1235,1236],{},"For a full walkthrough, see how to create a countdown timer in Python.",[56,1238,1240],{"className":58,"code":1239,"language":60,"meta":61,"style":61},"import time\n\nfor number in range(3, 0, -1):\n    print(number)\n    time.sleep(1)\n\nprint(\"Go!\")\n",[18,1241,1242,1248,1252,1281,1292,1306,1310],{"__ignoreMap":61},[65,1243,1244,1246],{"class":67,"line":68},[65,1245,72],{"class":71},[65,1247,76],{"class":75},[65,1249,1250],{"class":67,"line":79},[65,1251,83],{"emptyLinePlaceholder":82},[65,1253,1254,1256,1259,1261,1263,1265,1267,1269,1272,1274,1277,1279],{"class":67,"line":86},[65,1255,1125],{"class":71},[65,1257,1258],{"class":75}," number ",[65,1260,1131],{"class":71},[65,1262,1134],{"class":89},[65,1264,94],{"class":93},[65,1266,1139],{"class":140},[65,1268,762],{"class":93},[65,1270,1271],{"class":140}," 0",[65,1273,762],{"class":93},[65,1275,1276],{"class":353}," -",[65,1278,434],{"class":140},[65,1280,1142],{"class":93},[65,1282,1283,1285,1287,1290],{"class":67,"line":108},[65,1284,1147],{"class":89},[65,1286,94],{"class":93},[65,1288,1289],{"class":97},"number",[65,1291,125],{"class":93},[65,1293,1294,1296,1298,1300,1302,1304],{"class":67,"line":128},[65,1295,1163],{"class":75},[65,1297,100],{"class":93},[65,1299,135],{"class":97},[65,1301,94],{"class":93},[65,1303,434],{"class":140},[65,1305,125],{"class":93},[65,1307,1308],{"class":67,"line":146},[65,1309,83],{"emptyLinePlaceholder":82},[65,1311,1312,1314,1316,1318,1321,1323],{"class":67,"line":676},[65,1313,90],{"class":89},[65,1315,94],{"class":93},[65,1317,116],{"class":115},[65,1319,1320],{"class":119},"Go!",[65,1322,116],{"class":115},[65,1324,125],{"class":93},[323,1326,1328],{"id":1327},"printing-the-current-timestamp-for-logs","Printing the current timestamp for logs",[56,1330,1332],{"className":58,"code":1331,"language":60,"meta":61,"style":61},"import time\n\nprint(\"Log time:\", time.time())\n",[18,1333,1334,1340,1344],{"__ignoreMap":61},[65,1335,1336,1338],{"class":67,"line":68},[65,1337,72],{"class":71},[65,1339,76],{"class":75},[65,1341,1342],{"class":67,"line":79},[65,1343,83],{"emptyLinePlaceholder":82},[65,1345,1346,1348,1350,1352,1355,1357,1359,1361,1363,1365],{"class":67,"line":86},[65,1347,90],{"class":89},[65,1349,94],{"class":93},[65,1351,116],{"class":115},[65,1353,1354],{"class":119},"Log time:",[65,1356,116],{"class":115},[65,1358,762],{"class":93},[65,1360,357],{"class":97},[65,1362,100],{"class":93},[65,1364,20],{"class":97},[65,1366,105],{"class":93},[51,1368,1370],{"id":1369},"time-module-vs-datetime-module","time module vs datetime module",[14,1372,1373],{},"Beginners often wonder which module to use.",[14,1375,297,1376,1378],{},[18,1377,20],{}," when you need:",[26,1380,1381,1383,1386],{},[29,1382,223],{},[29,1384,1385],{},"simple timestamps",[29,1387,1388],{},"basic timing",[14,1390,297,1391,1378],{},[18,1392,47],{},[26,1394,1395,1397,1399],{},[29,1396,305],{},[29,1398,308],{},[29,1400,1401],{},"date arithmetic",[14,1403,1404],{},"A simple rule:",[26,1406,1407,1416],{},[29,1408,1409,1410,1412,1413,1415],{},"start with ",[18,1411,283],{}," and ",[18,1414,169],{}," for basic tasks",[29,1417,1418,1419,1423],{},"move to ",[42,1420,1421],{"href":44},[18,1422,47],{}," for date display and date math",[51,1425,1427],{"id":1426},"common-mistakes","Common mistakes",[14,1429,1430,1431,100],{},"Here are some common beginner problems when using ",[18,1432,20],{},[323,1434,1436,1437],{"id":1435},"forgetting-to-import-time","Forgetting to import ",[18,1438,20],{},[14,1440,1441,1442,100],{},"This causes a ",[18,1443,1444],{},"NameError",[56,1446,1448],{"className":58,"code":1447,"language":60,"meta":61,"style":61},"print(time.time())\n",[18,1449,1450],{"__ignoreMap":61},[65,1451,1452,1454,1456,1458,1460,1462],{"class":67,"line":68},[65,1453,90],{"class":89},[65,1455,94],{"class":93},[65,1457,20],{"class":97},[65,1459,100],{"class":93},[65,1461,20],{"class":97},[65,1463,105],{"class":93},[14,1465,1466],{},"Fix:",[56,1468,1470],{"className":58,"code":1469,"language":60,"meta":61,"style":61},"import time\n\nprint(time.time())\n",[18,1471,1472,1478,1482],{"__ignoreMap":61},[65,1473,1474,1476],{"class":67,"line":68},[65,1475,72],{"class":71},[65,1477,76],{"class":75},[65,1479,1480],{"class":67,"line":79},[65,1481,83],{"emptyLinePlaceholder":82},[65,1483,1484,1486,1488,1490,1492,1494],{"class":67,"line":86},[65,1485,90],{"class":89},[65,1487,94],{"class":93},[65,1489,20],{"class":97},[65,1491,100],{"class":93},[65,1493,20],{"class":97},[65,1495,105],{"class":93},[323,1497,1499,1500],{"id":1498},"passing-a-string-to-timesleep","Passing a string to ",[18,1501,283],{},[14,1503,1504,1505,1508],{},"This will fail because ",[18,1506,1507],{},"sleep()"," expects a number.",[56,1510,1512],{"className":58,"code":1511,"language":60,"meta":61,"style":61},"import time\n\ntime.sleep(\"2\")\n",[18,1513,1514,1520,1524],{"__ignoreMap":61},[65,1515,1516,1518],{"class":67,"line":68},[65,1517,72],{"class":71},[65,1519,76],{"class":75},[65,1521,1522],{"class":67,"line":79},[65,1523,83],{"emptyLinePlaceholder":82},[65,1525,1526,1528,1530,1532,1534,1536,1538,1540],{"class":67,"line":86},[65,1527,20],{"class":75},[65,1529,100],{"class":93},[65,1531,135],{"class":97},[65,1533,94],{"class":93},[65,1535,116],{"class":115},[65,1537,141],{"class":119},[65,1539,116],{"class":115},[65,1541,125],{"class":93},[14,1543,1466],{},[56,1545,1547],{"className":58,"code":1546,"language":60,"meta":61,"style":61},"import time\n\ntime.sleep(2)\n",[18,1548,1549,1555,1559],{"__ignoreMap":61},[65,1550,1551,1553],{"class":67,"line":68},[65,1552,72],{"class":71},[65,1554,76],{"class":75},[65,1556,1557],{"class":67,"line":79},[65,1558,83],{"emptyLinePlaceholder":82},[65,1560,1561,1563,1565,1567,1569,1571],{"class":67,"line":86},[65,1562,20],{"class":75},[65,1564,100],{"class":93},[65,1566,135],{"class":97},[65,1568,94],{"class":93},[65,1570,141],{"class":140},[65,1572,125],{"class":93},[323,1574,1576,1577,1579],{"id":1575},"expecting-timetime-to-return-a-readable-date","Expecting ",[18,1578,169],{}," to return a readable date",[14,1581,1582,1584],{},[18,1583,169],{}," returns a numeric Unix timestamp.",[56,1586,1587],{"className":58,"code":1469,"language":60,"meta":61,"style":61},[18,1588,1589,1595,1599],{"__ignoreMap":61},[65,1590,1591,1593],{"class":67,"line":68},[65,1592,72],{"class":71},[65,1594,76],{"class":75},[65,1596,1597],{"class":67,"line":79},[65,1598,83],{"emptyLinePlaceholder":82},[65,1600,1601,1603,1605,1607,1609,1611],{"class":67,"line":86},[65,1602,90],{"class":89},[65,1604,94],{"class":93},[65,1606,20],{"class":97},[65,1608,100],{"class":93},[65,1610,20],{"class":97},[65,1612,105],{"class":93},[14,1614,1615,1616,1618,1619,100],{},"If you want readable text, use ",[18,1617,505],{}," or ",[18,1620,698],{},[56,1622,1624],{"className":58,"code":1623,"language":60,"meta":61,"style":61},"import time\n\nprint(time.ctime())\n",[18,1625,1626,1632,1636],{"__ignoreMap":61},[65,1627,1628,1630],{"class":67,"line":68},[65,1629,72],{"class":71},[65,1631,76],{"class":75},[65,1633,1634],{"class":67,"line":79},[65,1635,83],{"emptyLinePlaceholder":82},[65,1637,1638,1640,1642,1644,1646,1648],{"class":67,"line":86},[65,1639,90],{"class":89},[65,1641,94],{"class":93},[65,1643,20],{"class":97},[65,1645,100],{"class":93},[65,1647,551],{"class":97},[65,1649,105],{"class":93},[323,1651,1653,1654,1656,1657,1659],{"id":1652},"using-time-when-datetime-would-be-easier","Using ",[18,1655,20],{}," when ",[18,1658,47],{}," would be easier",[14,1661,1662,1663,1667],{},"If you need values like year, month, and day in a clean object, the ",[42,1664,1665,48],{"href":44},[18,1666,47],{}," is usually easier to work with.",[323,1669,1671,1672,1674],{"id":1670},"thinking-sleep-pauses-only-one-line","Thinking ",[18,1673,1507],{}," pauses only one line",[14,1676,1677,1679],{},[18,1678,283],{}," pauses the whole program before it moves to the next line.",[56,1681,1683],{"className":58,"code":1682,"language":60,"meta":61,"style":61},"import time\n\nprint(\"Before\")\ntime.sleep(2)\nprint(\"After\")\n",[18,1684,1685,1691,1695,1710,1724],{"__ignoreMap":61},[65,1686,1687,1689],{"class":67,"line":68},[65,1688,72],{"class":71},[65,1690,76],{"class":75},[65,1692,1693],{"class":67,"line":79},[65,1694,83],{"emptyLinePlaceholder":82},[65,1696,1697,1699,1701,1703,1706,1708],{"class":67,"line":86},[65,1698,90],{"class":89},[65,1700,94],{"class":93},[65,1702,116],{"class":115},[65,1704,1705],{"class":119},"Before",[65,1707,116],{"class":115},[65,1709,125],{"class":93},[65,1711,1712,1714,1716,1718,1720,1722],{"class":67,"line":108},[65,1713,20],{"class":75},[65,1715,100],{"class":93},[65,1717,135],{"class":97},[65,1719,94],{"class":93},[65,1721,141],{"class":140},[65,1723,125],{"class":93},[65,1725,1726,1728,1730,1732,1735,1737],{"class":67,"line":128},[65,1727,90],{"class":89},[65,1729,94],{"class":93},[65,1731,116],{"class":115},[65,1733,1734],{"class":119},"After",[65,1736,116],{"class":115},[65,1738,125],{"class":93},[51,1740,1742],{"id":1741},"useful-commands-to-test-the-module","Useful commands to test the module",[14,1744,1745],{},"These commands let you quickly check that the module works in your Python installation.",[56,1747,1751],{"className":1748,"code":1749,"language":1750,"meta":61,"style":61},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","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","bash",[18,1752,1753,1762,1777,1790],{"__ignoreMap":61},[65,1754,1755,1758],{"class":67,"line":68},[65,1756,60],{"class":1757},"sbgvK",[65,1759,1761],{"class":1760},"stzsN"," --version\n",[65,1763,1764,1766,1769,1771,1774],{"class":67,"line":79},[65,1765,60],{"class":1757},[65,1767,1768],{"class":1760}," -c",[65,1770,1222],{"class":115},[65,1772,1773],{"class":119},"import time; print(time.time())",[65,1775,1776],{"class":115},"\"\n",[65,1778,1779,1781,1783,1785,1788],{"class":67,"line":86},[65,1780,60],{"class":1757},[65,1782,1768],{"class":1760},[65,1784,1222],{"class":115},[65,1786,1787],{"class":119},"import time; time.sleep(1); print('done')",[65,1789,1776],{"class":115},[65,1791,1792,1794,1796,1798,1801],{"class":67,"line":108},[65,1793,60],{"class":1757},[65,1795,1768],{"class":1760},[65,1797,1222],{"class":115},[65,1799,1800],{"class":119},"import time; print(time.ctime())",[65,1802,1776],{"class":115},[51,1804,1806],{"id":1805},"faq","FAQ",[323,1808,1810],{"id":1809},"is-the-time-module-built-into-python","Is the time module built into Python?",[14,1812,1813,1814,1816],{},"Yes. You can use it with ",[18,1815,833],{}," and do not need to install anything.",[323,1818,1820],{"id":1819},"what-does-timesleep-do","What does time.sleep() do?",[14,1822,1823],{},"It pauses the whole program for the number of seconds you give it.",[323,1825,1827],{"id":1826},"what-does-timetime-return","What does time.time() return?",[14,1829,1830],{},"It returns the current Unix timestamp as a float.",[323,1832,1834],{"id":1833},"should-i-use-time-or-datetime","Should I use time or datetime?",[14,1836,297,1837,1839,1840,1842],{},[18,1838,20],{}," for delays and simple timestamps. Use ",[18,1841,47],{}," for readable dates and date calculations.",[323,1844,1846],{"id":1845},"can-timesleep-use-decimal-values","Can time.sleep() use decimal values?",[14,1848,1849,1850,1853],{},"Yes. For example, ",[18,1851,1852],{},"time.sleep(0.5)"," pauses for half a second.",[51,1855,1857],{"id":1856},"see-also","See also",[26,1859,1860,1867,1873],{},[29,1861,1862],{},[42,1863,1864,1866],{"href":280},[18,1865,283],{}," function explained",[29,1868,1869],{},[42,1870,1871,1866],{"href":289},[18,1872,169],{},[29,1874,1875],{},[42,1876,1877,48],{"href":44},[18,1878,47],{},[1880,1881,1882],"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 .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":61,"searchDepth":79,"depth":79,"links":1884},[1885,1886,1887,1888,1895,1896,1902,1903,1915,1916,1923],{"id":53,"depth":79,"text":54},{"id":206,"depth":79,"text":207},{"id":262,"depth":79,"text":263},{"id":314,"depth":79,"text":315,"children":1889},[1890,1891,1892,1893,1894],{"id":325,"depth":86,"text":169},{"id":385,"depth":86,"text":388},{"id":502,"depth":86,"text":505},{"id":594,"depth":86,"text":597},{"id":695,"depth":86,"text":698},{"id":820,"depth":79,"text":821},{"id":1011,"depth":79,"text":1012,"children":1897},[1898,1899,1900,1901],{"id":1020,"depth":86,"text":1021},{"id":1083,"depth":86,"text":1084},{"id":1232,"depth":86,"text":1233},{"id":1327,"depth":86,"text":1328},{"id":1369,"depth":79,"text":1370},{"id":1426,"depth":79,"text":1427,"children":1904},[1905,1907,1909,1911,1913],{"id":1435,"depth":86,"text":1906},"Forgetting to import time",{"id":1498,"depth":86,"text":1908},"Passing a string to time.sleep()",{"id":1575,"depth":86,"text":1910},"Expecting time.time() to return a readable date",{"id":1652,"depth":86,"text":1912},"Using time when datetime would be easier",{"id":1670,"depth":86,"text":1914},"Thinking sleep() pauses only one line",{"id":1741,"depth":79,"text":1742},{"id":1805,"depth":79,"text":1806,"children":1917},[1918,1919,1920,1921,1922],{"id":1809,"depth":86,"text":1810},{"id":1819,"depth":86,"text":1820},{"id":1826,"depth":86,"text":1827},{"id":1833,"depth":86,"text":1834},{"id":1845,"depth":86,"text":1846},{"id":1856,"depth":79,"text":1857},"Master python time module overview in our comprehensive Python beginner guide.","md",{},"\u002Fstandard-library\u002Fpython-time-module-overview",{"title":5,"description":1924},"standard-library\u002Fpython-time-module-overview","6V4Co50mQjZt7MniDwC0Cd6CU8eTXYJZZMofcyukkGQ",1777585471144]