Python Sleep Method

An infographic on the Python sleep method. Explore how to introduce time delays, control animations, and optimize program timing.

What is sleep() method in Python?

The sleep() is a method of time module that enables suspending the execution of the current thread for the given time.

The time is provided in seconds.

How to use the time.sleep method?

After importing the time module in your program, you may use the sleep method as follows:

time.sleep(time_in_seconds)

Where time in seconds can be an int or float type. The float type enables you to pause the execution of the thread more precisely. For example:

time.sleep( 2 ) #an Int value = 2 seconds

time.sleep( 3.55 ) #Time in floating point

For specifying the time in milliseconds (ms), for less than a second, you may also use this:

time.sleep( 0.7 )

That means 700 ms sleep or delay.

Note: The actual suspension time may be less or more than the requested time in the sleep method. The reason can be the operating system, scheduling of other activities in the system, or any caught signal that will terminate the sleep().

See the next section for different examples of using the sleep Python method.

A simple example of using the Python sleep method

In this example, I simply used the print function to display two sentences.

The first print function executes normally. One second of delay is given for executing the second print function. See the code below:

#The sleep method demo

import time


print ("The time.sleep method demo")

time.sleep( 1 )

print ("Delay of one second")

The output:

The time.sleep method demo
Delay of one second

As you execute this code, you will see the second line will display after the one-second wait.

A Demo of using float value for seconds parameter

For this demo of using the float value for the delay, I used the 500 ms value in the sleep command parameter. A while loop is used that will run until the value of the variable x reaches 50.

In each loop, the current value of the variable x is displayed and the wait of 500 ms is given in the sleep method:

#The sleep method demo

import time


x = 10

while x <= 50:             

   x = x + 10

   time.sleep(.500)

   print ("The value of x = :", x)

The result of this code:

sleep while

As you run this code, you will see each line will display with a gap of 500 ms or half of a second.

Using the sleep method for iterating a list

For the final example, I used a list of five items and used the sleep method in a for loop.

The value of one and a half seconds (1.5) is given in the sleep method to delay the execution. Have a look:

#The sleep method demo

import time

lst_sleep = [10,20,30,40,50]


for x in lst_sleep:

    print ("Display list item with gap of 1.5 seconds: ", x)

     time.sleep(1.5)

delay

As you run this code, the list items will display with a gap of 1.5 seconds starting from the second item. As such, I placed the sleep command after the print function.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!