Trusted answers to developer questions

What is the sleep function in Python?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

The sleep function is used to pause the execution of a current thread for a given number of seconds or milliseconds, depending on the programming language being used. So, what is a thread?

Every computer program contains a series of instructions; the execution of these instructions is known as a process. A thread is the smallest subset of a process.

Example

For the code below, if you click the run button, you will see that its execution time is around 0.40.4 to 0.60.6 seconds.

print("Printed immediately.")

Now, using the sleep function in Python, the print function should run 22 seconds after the code is run. The execution time should be around 2.42.4-2.62.6 seconds.

import time
time.sleep(2.4)
print("Printed after 2.4 seconds.")

RELATED TAGS

sleep()
threads
python
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?