What is the usage of the sleep function in Python?

In Python, the function sleep() is used to pause the execution of a program for a certain amount of time. It takes a floating point number as a parameter, representing the number of seconds to pause.

Example usage:

import time

print("开始")
time.sleep(2.5)   # 暂停2.5秒
print("结束")

After running the above code, a pause of 2.5 seconds will occur between the start and end.

The sleep() function is often useful in many situations, such as simulating delays when handling network requests or setting time intervals for scheduled tasks.

bannerAds