Syntax of time sleep()

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.

Example 1: Creating a Time Delay in seconds

The start time and end time will be printed with 6 seconds delay.

Python3




import time
 
# printing the start time
print("The time of code execution begin is : ", time.ctime())
 
# using sleep() to hault the code execution
time.sleep(6)
 
# printing the end time
print("The time of code execution end is : ", 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

Example 2: Creating a Time Delay in minutes

The list will be displayed after the delay of 3 minutes

Python3




import time
 
# creating and Initializing a list
Languages = ['Java', 'C++', 'Python', 'Javascript',
             'C#', 'C', 'Kotlin']
 
# creating a time delay of 3 minutes
time.sleep(3 * 60)
 
print(Languages)


Output:

After the delay of 3 minutes, the list will be displayed as:

['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']

time.sleep() in Python

Python time sleep() function suspends execution for the given number of seconds.

Similar Reads

Syntax of time sleep()

Syntax : sleep(sec) Parameters :  sec : Number of seconds for which the code is required to be stopped. Returns : VOID....

Application of time.sleep()

...