This is a detailed tutorial of the Python sleep() Method. Learn to suspend the execution of a given Python program or code for a given number of seconds.
sleep() of the Python time module is used to suspend the execution of a given Python program for the desired number of seconds. It’s a kind of function to pause your Python code execution for some time.
Given below is the syntax for the
This method only takes a single argument and that is the time in seconds. This is a floating-point number value. You can simply provide the
seconds argument as an integer value to just specify the timings upto the precision of seconds. But if you want the execution suspension timings to be more precise, you can use floating-point numbers.
For example, the valid values of this argument are 1, 2, 3.5, 4.965, etc.
sleep() Return Value
It does not return any value as it simply suspends the program execution for a particular period of time.
Given below is an example illustrating the purpose of the Python time
sleep() method. Here we’ve first imported the time module and then printed the current time using the
ctime() method. Then we’ve suspended the program execution for 4 seconds using this method and then again printed the current time.
import time print(time.ctime()) #Suspending Execution for 4 seconds time.sleep(4) print(time.ctime())
As you can notice the two printed timings, there’s a difference of 4 seconds because for that interval of time, the program execution was suspended and the control was sleeping.
Thu Jul 9 07:51:10 2020 Thu Jul 9 07:51:14 2020
Similarly, another python code given below suspends the execution of the program, even more precisely. Here we’ve just requested to pause the execution for 3.569 seconds i.e. for 3 seconds and 569 milliseconds.
import time print(time.time()) #Suspending Execution for 3 seconds and 569 milliseconds time.sleep(3.569) print(time.time())
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!