Python Sleep Method To Wait, Sleep, Pause, Stop, Delay

By | January 3, 2018
 

Let us understand what is Python sleep() method and how we can implement Python wait, pause, stop and delay functionalities using the sleep() function.

What is Python sleep() function?

The sleep() function in Python suspends the execution of a Python script for a given period of time, usually in seconds. The sleep() function is defined in the Python’s time module.

With the sleep() method, it is possible to implement delay, pause, wait and stop the Python script and all these tasks can be performed by the sleep() method.

Syntax

It is important to import the time module in the Python script where you want to use the sleep() method. Remeb

Uses of Python.sleep() method

  • The Python script can wait for user inputs for a given period of time
  • Create intentional delays in executing certain parts of the script
  • Stop the execution of a script
  • Pause the script execution for a given period of time

Note: This script to implement Python wait functionality using Python sleep() function is written using Python IDLE on Microsoft Windows operating system and the version is Python 3.6.4.

Method 1: Basic Python wait functionality

The time module in Python provides various functionalities for time access and conversions. It is very much similar to the time library of C programming.

   

We need to initially import time module using import time statement. Then, use the sleep method with the time module using a dot operator with the following statement time.sleep(10).

Method 2: Python sleep() function with print statements

Method 3: Implementing time.sleep() method with start and end time

   

Method 4: Python wait function with User Input

If you’re writing this script in Python 2.x versions, then you should use raw_input() method and for Python 3.x versions, you can use input() method. We’re converting the user input into float datatype here since the default input is string. You can also use any other numeric datatype.

Output

Python Sleep Method Explained To Implement Python Wait, Sleep, Delay, Pause and Stop functionalities

If you have any issues while writing a script to implement Python sleep() method, let us know about it in the comment section below.

Let's Discuss