What is the sleep function in Python?

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.")

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved