When you run a simple Python program, the code execution happens sequentially—one statement after the other—without any time delay. However, you may need to delay the execution of code in some cases. The sleep() function from Python built-in time module helps you do this. In this tutorial, you’ll learn the syntax of using the sleep() function in Python and several examples to understand how it works. Let’s get started!

Syntax of Python time.sleep()

The time module, built into the Python standard library, provides several useful time-related functions. As a first step, import the time module into your working environment: As the sleep() function is part of the time module, you can now access and use it with the following general syntax: Here, n is the number of seconds to sleep. It can be an integer or a floating point number. Sometimes the required delay may be a few milliseconds. In these cases, you can convert the duration in milliseconds to seconds and use it in the call to the sleep function. For example, if you want to introduce a delay of 100 milliseconds, you can specify it as 0.1 second: time.sleep(0.1). ▶ You can also import only the sleep function from the time module: If you use the above method for importing, you can then call the sleep() function directly—without using time.sleep(). Now that you’ve learned the syntax of the Python sleep() function, let’s code examples to see the function in action. You can download the Python scripts used in this tutorial from the python-sleep folder in this GitHub repo. 👩🏽‍💻

Delay Code Execution with sleep() 

As a first example, let’s use the sleep function to delay the execution of a simple Python program. In the following code snippet:

The first print() statement is executed without any delay. We then introduce a delay of 5 seconds using the sleep() function.  The second print() statement will be executed only after the sleep operation finishes.

Now run the simple_example.py file and observe the output:

Add Different Delays to a Code Block

In the previous example, we introduced a fixed delay of 5 seconds between the execution of two print() statements. Next, let’s code another example to introduce different delay times when looping through an iterable. In this example, we would like to do the following:

Loop through a sentence, access each word, and print it out.  After printing out each word, we’d like to wait for a specific duration of time—before printing out the next word in the sentence.

Looping Through a String of Strings

Consider the string, sentence. It is a string where each word is a string in itself. If we loop through the string, we will get each character, as shown: But this is not what we want. We would like to loop through the sentence and access each word. To do this, we can call the split() method on the sentence string. This will return a list of strings—obtained by splitting the sentence string—on all occurrences of whitespace.

Looping Through Iterables with Different Delays

Let’s revisit the example:

sentence is the string we’d like to loop through to access each word. delay_times is the list of delay times we’ll use as argument to the sleep() function during each pass through the loop.

Here we would like to simultaneously loop through two lists: the delay_times list and the list of strings obtained by splitting the sentence string. You can use the zip() function to perform this parallel iteration. Without the sleep function, the control would immediately proceed to the next iteration. Because we have introduced a delay, the next pass through the loop occurs only after the sleep operation is complete. Now run delay_times.py and observe the output: The subsequent words in the string will be printed out after a delay. The delay after printing out the word at index i in the string is the number at index i in the delay_times list.

Countdown Timer in Python

As a next example, let us code a simple countdown timer in Python. Let’s define a function countDown(): Next, let’s parse the definition of the countDown() function:

The function takes in a number n as the argument and counts down to zero starting from that number n. We use time.sleep(1) to achieve a delay of one second between the counts. When the count reaches 0, the function prints out “Ready to go!”.

Next let’s add a call to the countDown() function with 5 as the argument. Now run the script countdown.py and see the countDown function in action!

Sleep Function in Multithreading

Python threading module offers out-of-the-box multithreading capabilities. In Python, the Global Interpreter Lock or GIL ensures that there is only one active thread running at any point in time. However, during I/O operations and wait operations such as sleep, the processor can suspend the execution of the current thread and switch to another thread that is waiting. To understand how this works, let’s take an example.

Creating and Running Threads in Python

Consider the following functions, func1(), func2(), and func3(). They loop through a range of numbers and print them out. This is followed by a sleep operation—for a specific number of seconds—during every pass through the loop. We’ve used different delay times for each of the functions to better understand how the execution switches between threads concurrently. In Python, you can use the Thread() constructor to instantiate a thread object. Using the syntax threading.Thread(target = …, args = …) creates a thread that runs the target function with the argument specified in the args tuple. In this example the functions, func1, func2, and func3, do not take in any arguments. So it suffices to specify only the name of the function as the target. We then define thread objects, t1, t2, and t3 with func1, func2, and func3 as the targets, respectively. Here’s the complete code for the threading example: Observe the output. The execution alters between the three threads. The thread t3 has the lowest waiting time, so it is suspended for the least amount of time. Thread t1 has the longest sleep duration of two seconds, so it’s the last thread to finish execution. To learn more, read the tutorial on the basics of multithreading in Python.

Conclusion

In this tutorial, you have learned how to use Python’s sleep() function to add time delays to code. You can access the sleep() function from the built-in time module, time.sleep(). To delay execution by n seconds, use time.sleep(n). Also, you’ve seen examples of delaying subsequent iterations in a loop by different values, countdown, and multithreading. You may now explore more advanced capabilities of the time module. Want to work with dates and times in Python? In addition to the time module, you can leverage the functionality of the datetime and calendar modules. Next, learn to calculate time difference in Python.⏰

Python Sleep Function  How to Add Delays to Code - 50Python Sleep Function  How to Add Delays to Code - 62Python Sleep Function  How to Add Delays to Code - 45Python Sleep Function  How to Add Delays to Code - 2Python Sleep Function  How to Add Delays to Code - 93Python Sleep Function  How to Add Delays to Code - 44Python Sleep Function  How to Add Delays to Code - 95Python Sleep Function  How to Add Delays to Code - 37Python Sleep Function  How to Add Delays to Code - 84Python Sleep Function  How to Add Delays to Code - 92Python Sleep Function  How to Add Delays to Code - 89Python Sleep Function  How to Add Delays to Code - 47Python Sleep Function  How to Add Delays to Code - 25Python Sleep Function  How to Add Delays to Code - 41Python Sleep Function  How to Add Delays to Code - 39Python Sleep Function  How to Add Delays to Code - 22Python Sleep Function  How to Add Delays to Code - 17Python Sleep Function  How to Add Delays to Code - 41Python Sleep Function  How to Add Delays to Code - 3Python Sleep Function  How to Add Delays to Code - 99Python Sleep Function  How to Add Delays to Code - 25Python Sleep Function  How to Add Delays to Code - 52Python Sleep Function  How to Add Delays to Code - 74Python Sleep Function  How to Add Delays to Code - 83Python Sleep Function  How to Add Delays to Code - 29Python Sleep Function  How to Add Delays to Code - 32Python Sleep Function  How to Add Delays to Code - 94Python Sleep Function  How to Add Delays to Code - 61Python Sleep Function  How to Add Delays to Code - 81Python Sleep Function  How to Add Delays to Code - 14