Inducing time delays in python using sleep function

sleep_in_python

Skill - Inducing time delays in python scripts using ‘sleep’ function in time module

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


Using the sleep function in time module, we can make the code execution delayed by a desired time interval

sleep_illustration

Function Usage

First import the time module
To induce a delay of say 5 seconds in your code, just call time.delay(5)

Example

import time
print("Hello ")
time.sleep(2)
print("World!")
# this prints 'Hello ' first waits 2 seconds and then prints 'World!'

Video

The video tutorial for this post can be found here

Online Interpreter

Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://pynative.com/online-python-code-editor-to-execute-python-code/

References


Table of Contents

Comments