46. Python's time Module
1. Sleeping for a Specified Time
import time
print("Program starts")
time.sleep(3) # Pauses for 3 seconds
print("Program resumes after 3 seconds")2. Measuring Elapsed Time with time.time()
import time
start_time = time.time()
# Some time-consuming task
time.sleep(2)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time} seconds")3. Formatting Current Time with time.strftime()
4. Converting Time to Seconds with time.mktime()
5. Getting the Current Time in Epoch Format
6. Pausing Program for User Input Delay
7. Time-based Loop (Periodic Tasks)
8. Get the Time in Different Time Zones
9. Calculating Execution Time Using time.perf_counter()
10. Getting the Current Time in UTC
Last updated