![]() |
VOOZH | about |
Python time sleep() function suspends execution for the given number of seconds.
Syntax : sleep(sec)
Parameters :
- sec : Number of seconds for which the code is required to be stopped.
Returns : VOID.
Sometimes, there is a need to halt the flow of the program so that several other executions can take place or simply due to the utility required. sleep() can come in handy in such a situation which provides an accurate and flexible way to halt the flow of code for any period of time. This function discusses the insight of this function.
The start time and end time will be printed with 6 seconds delay.
Output:
The time of code execution begin is : Mon Apr 9 20:57:10 2018 The time of code execution end is : Mon Apr 9 20:57:16 2018
The list will be displayed after the delay of 3 minutes
Output:
After the delay of 3 minutes, the list will be displayed as:
['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']
There are many applications that sleep() is used for. Be its execution of the background thread which is repeated at regular intervals, this can be implemented with the help of sleep(). Another popular application is using sleep() to print the words letter by letter for a good user interface. The latter is represented in the code below.
Output:
GeeksForGeeks
Note: Visible effect of sleep() can be seen in the local editor.
Output:
After the delay of 5 seconds, we will get the output as:
['Jai', 'Shree', 'RAM', 5, 'August', 2020]
Output:
After the delay of 4 seconds, we will get the output as:
('Anil Kumbl', 'Sachin Tendulkar', 'Sunil Gavaskar', 'Rahul Dravid',
'Mahendra Singh Dhoni', 'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne')Output:
After every 7 seconds, the items on the list will be displayed as:
Anil Kumble Sachin Tendulkar Sunil Gavaskar Rahul Dravid Mahendra Singh Dhoni Dennis Lillee Muttiah Muralitharan Shane Warne
Output:
After the delay of 5 seconds, the list will be displayed as:
['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']
Then after every 13 seconds, the items on the list will be displayed as:
Java C++ Python Javascript C# C Kotlin