Python
Python Variables: How to Define/Declare String Variable Types
What is a Variable in Python? A Python variable is a reserved memory location to store values. In other...
Python sleep() is a function used to delay the execution of code for the number of seconds given as input to sleep(). The sleep() command is a part of the time module. You can use the sleep() function to temporarily halt the execution of your code. For example, you are waiting for a process to complete or a file upload.
In this tutorial, you will learn:
import time time.sleep(seconds)
seconds: The number of seconds you want the execution of your code to be halted.
Follow the steps given below to add sleep() in your python script.
Step 1:
import time
Step 2: Add time.sleep()
The number 5 given as input to sleep(), is the number of seconds you want the code execution to halt when it is executed.
time.sleep(5)
Here is a working code along with messages inside print(), to show the delay of message display on the terminal when executed.
import time
print("Welcome to gtupapers Python Tutorials")
time.sleep(5)
print("This message will be printed after a wait of 5 seconds")
Output:
Welcome to gtupapers Python Tutorials This message will be printed after a wait of 5 seconds
The example shown below has a function defined called display(). The display() function prints a message "Welcome to gtupapers Tutorials". When the function is called, it will execute and display the message inside the terminal.
To add delay to the execution of the function, let us add the time.sleep in Python before making a call to the function. During the execution, Python time.sleep will halt there for the number of seconds given, and later the function display() will be called.
Example:
import time
print('Code Execution Started')
def display():
print('Welcome to gtupapers Tutorials')
time.sleep(5)
display()
print('Function Execution Delayed')
Output:
Code Execution Started Welcome to gtupapers Tutorials Function Execution Delayed
We have seen a few examples earlier on how to use time.sleep(). Let us try a different example here using time.sleep().
Example:
The code has a for loop that will take the string variable and print each character with a delay of 1 seconds.
import time my_message = "gtupapers" for i in my_message: print(i) time.sleep(1)
Output:
G u r u 9 9
You can make use of asyncio.sleep with python version 3.4 and higher. To make use of the asyncio sleep method, you need to add async and await to the function, as shown in the example below:
Example:
The script has a function call display() that prints a message "Welcome to gtupapers tutorials". There are two keywords used in the function async and await. The async keyword is added at the start of the function definition, and await is added just before the asyncio.sleep(). Both the keywords async / await are meant to handle the asynchronous task.
When the function display() is called, and it encounters await asyncio.sleep(5), the code will sleep or halt at that point for 5 seconds and, once done, will print the message.
import asyncio
print('Code Execution Started')
async def display():
await asyncio.sleep(5)
print('Welcome to gtupapers Tutorials')
asyncio.run(display())
Output:
Code Execution Started Welcome to gtupapers Tutorials
The Event().wait method comes from the threading module. Event.wait() method will halt the execution of any process for the number of seconds it takes as an argument. The working of Event is shown in the example below:
Example:
The code is using Event().wait(5).The number 5 is the number of seconds the code will delay to go to the next line that calls the function display(). Once the 5 seconds are done, the function display() will be called, and the message will be printed inside in the terminal.
from threading import Event
print('Code Execution Started')
def display():
print('Welcome to gtupapers Tutorials')
Event().wait(5)
display()
Output:
Code Execution Started Welcome to gtupapers Tutorials
The Timer is another method available with Threading, and it helps to get the same functionality as Python time sleep. The working of the Timer is shown in the example below:
Example:
A Timer takes in input as the delay time in Python in seconds, along with a task that needs to be started. To make a timer working, you need to call the start() method. In the code, the Timer is given 5 seconds, and the function display that has to be called when 5 seconds are done. The timer will start working when the Timer.start() method is called.
from threading import Timer
print('Code Execution Started')
def display():
print('Welcome to gtupapers Tutorials')
t = Timer(5, display)
t.start()
Output:
Code Execution Started Welcome to gtupapers Tutorials
What is a Variable in Python? A Python variable is a reserved memory location to store values. In other...
What is a Dictionary in Python? A Dictionary in Python is the unordered and changeable collection of...
PyCharm is a cross-platform editor developed by JetBrains. Pycharm provides all the tools you need for...
In Python, date, time and datetime classes provides a number of function to deal with dates, times and...
What is Python String format()? Python String format() is a function used to replace, substitute, or...
Python code editors are designed for the developers to code and debug program easily. Using these...