Sobes.tech
Intern
206

What is the sleep() function and what is it used for?

Answer from AI

sobes.tech AI

The sleep() function from the standard time library pauses the execution of the current thread for a specified number of seconds.

It is used for the following purposes:

  • Simulating real-time delays.
  • Regulating the speed of a loop or operation.
  • Waiting for a resource to become ready (e.g., a file, network connection).
  • Preventing excessive load on the system or external service.
  • Debugging purposes.
import time

print("Start")
time.sleep(2)  # Pause execution for 2 seconds
print("End")