Learn the Basics of sleep()

00:00 In this lesson, you’ll get to know the basic functionality of Python’s time.sleep().

00:06 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.

00:19 Then it goes by calling the module, dot (.), the function name sleep(), and then pass it as an argument the amount of seconds that you want it to sleep.

00:30 And then what Python is going to do, it’s going to take a break for the specified amount of seconds.

00:37 Let’s go over to the terminal and look at time.sleep() in action. I’m going to start the Python interpreter, and then I will import the time module.

00:50 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.

01:09 Here, the prompts are back. So that took three seconds. Let’s look at it again. Or I’m going to show you if I put in 1 here, the time is shorter.

01:18 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.

01:52 In the next lesson, you’re going to take a terminal test to figure out whether time.sleep() actually works as promised.

Become a Member to join the conversation.