Skill - Inducing time delays in python scripts using ‘sleep’ function in time module
Skills Required
- Setup Python Development environment
- Commenting in python
- Basic printing in python
- Managing Variables in python
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
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
- image credits - https://realpython.com/python-sleep/
Comments
Post a Comment