You can create a time delay in Python using the time.sleep()
function from the time
module. This function pauses the execution of your program for a specified number of seconds. Here's how to use it with an example:
python
import time
# Sleep for 3 seconds
time.sleep(3)
# This code will resume execution after a 3-second delay
print("Three seconds have passed.")
In the example above, we import the time
module and then use time.sleep(3)
to pause the program's execution for 3 seconds. After the sleep duration, the program will continue to execute, and "Three seconds have passed." will be printed to the console.
You can adjust the argument to time.sleep()
to specify the number of seconds you want the delay to last. For example, time.sleep(1)
will pause the program for 1 second, and time.sleep(0.5)
will pause it for half a second.
Comments
Post a Comment