time.sleep() in Python

08 Oct, 2021
Python time sleep() function suspends execution for the given number of seconds. 
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 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.
Syntax : sleep(sec)
Parameters : 
sec : Number of seconds for which the code is required to be stopped.
Returns : VOID. 
Python3
# Python code to demonstrate# working of sleep() import time # printing the start timeprint("The time of code execution begin is : ", end="")print(time.ctime()) # using sleep() to hault the code executiontime.sleep(6) # printing the end timeprint("The time of code execution end is : ", end="")print(time.ctime())
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
Application : 
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 from the code below.
Python3
# Python code to demonstrate# application of sleep() import time # initializing stringstrn = "GeeksforGeeks" # printing geeksforgeeks after delay# of each characterfor i in range(0, len(strn)):    print(strn[i], end="")    time.sleep(2)
Output:
GeeksForGeeks
Note : Visible effect of sleep() can be seen in the local editor.
Python3
# importing time packageimport time # creating a time delay of 5 secondstime.sleep(5) # creating and Initializing a listmyList = ['Jai', 'Shree', 'RAM', 5, 'August', 2020] # the list will be displayed after the# delay of 5 secondsprint(myList)
Output:
After the delay of 5 seconds, we will get the output as:
['Jai', 'Shree', 'RAM', 5, 'August', 2020]
Python3
# importing time packageimport time # creating a time delay of 4 secondstime.sleep(4) # creating and Initializing a tuplemytuple = ('Anil Kumbl', 'Sachin Tendulkar', 'Sunil Gavaskar',           'Rahul Dravid', 'Mahendra Singh Dhoni',           'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne') # the tuple will be displayed after the delay of 4 secondsprint(mytuple)
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')
Python3
# importing time packageimport time # creating and Initializing a listLanguages = ['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin'] # creating a time delay of 5 secondstime.sleep(5) # the list will be displayed after the delay of 5 secondsprint(Languages) for lan in Languages:         # creating a time delay of 13 seconds    time.sleep(13)         # After every 13 seconds an item of list will be displayed    print(lan)
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 of the list will be displayed as:
Java
C++
Python
Javascript
C#
C
Kotlin
Python3
# importing time packageimport time # creating and Initializing a listcricketers = ['Anil Kumble', 'Sachin Tendulkar', 'Sunil Gavaskar',              'Rahul Dravid', 'Mahendra Singh Dhoni',              'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne'] # time delay of 7 seconds is created# after every 7 seconds item of list gets displayedcricketers = [(time.sleep(7), print(cric)) for cric in cricketers]
Output:
After every 7  seconds, the items of the list will be displayed as:
Anil Kumble
Sachin Tendulkar
Sunil Gavaskar
Rahul Dravid
Mahendra Singh Dhoni
Dennis Lillee
Muttiah Muralitharan
Shane Warne
Python3
# importing time packageimport time # creating and Initializing a listLanguages = ['Java', 'C++', 'Python', 'Javascript',             'C#', 'C', 'Kotlin'] # creating a time delay of 3 minutestime.sleep(3 * 60) # the list will be displayed after the delay# of 3 minutesprint(Languages)
Output:
After the delay of 3 minutes, the list will be displayed as:
['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']
vanshgaur14866
rajeev0719singh
kumar_satyam
Python-Built-in-functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts
Convert integer to string in Python