How to Put a Time Delay in Python?
There are multiple ways to put a time delay in Python. Some commonly used methods are:
- Using the
time
module:
import time
# delay for 2 seconds
time.sleep(2)
- Using the
datetime
module:
import datetime
# delay for 2 seconds
start_time = datetime.datetime.now()
while (datetime.datetime.now() - start_time).seconds < 2:
pass
- Using the
timeit
module:
import timeit
# delay for 2 seconds
timeit.time.sleep(2)
Note: It is important to note that the accuracy of the delay may vary depending on the system load and other factors.