Learn the Basics of sleep()
time.sleep() is a function that you can use to instruct your code to take a break, and it comes in the
time module that is included in the standard library of Python, but you will need to import it for your script in order to use it.
Then you can say
time.sleep() and pass it an amount of seconds. Now, when I press Enter, you’ll see that it’s going to take three seconds before those three little prompts here are going to come back. And in that time, your Python code is sleeping and you can’t do anything.
If I put in
3 again, you get a bit more time to relax. And here it is, the prompt is back. So in the meantime, while Python is sleeping, you can’t do anything. But once the sleep is over, Python is going to continue executing whatever other parts you’re going to have inside of your script.
01:37 So, you might be wondering, “Were those really 3 seconds?” And I didn’t really even try to count it because I’m not that great at counting seconds, but we can, of course, use a little test to figure out whether those were really 3 seconds.
Become a Member to join the conversation.