🐡

What is the sleeping time of Python

2022/10/14に公開

Introduction:

Hello readers, I hope you are doing well. In this piece of article, we are going to put light on the following points:

What is the sleeping time of Python?
sleep() function in Python.

Let us move forward step by step.

What is the sleeping time of Python?

In Python, we can add delay in the execution of a program. The time during which a Python program’s execution is halted is known as the sleeping time of Python.

Sleep() function:

Python offers an inbuilt sleep() function that allows us to suspend the current thread's execution for a few seconds specified by a number.

Here, It is important to note that the Python sleep() function halts the current thread's execution only and not the entire program.

Sleep() function provides the most accurate and flexible method to halt the flow of code for any period of time.

Syntax:
sleep(numberOfSeconds)

Parameter:
numberOfSeconds: The number of seconds for which the execution of the current thread is required to be halted.

Return Type:
Void

Now let us see how we can implement sleep() function in Python:

Example 1:

Source Code:

# Python program to demonstrate
# the of working of sleep() function
 
# Import time library
import time							// Statement 1
 
# Print the starting time
print("The time at which the code execution begins: ", end="") 	// Statement 2
print(time.ctime())						// Statement 3
 
# Halt the execution of the current thread for eight seconds
time.sleep(8)                                                   // Statement 4
 
# Print the ending time
print("The time at which the code execution ends is : ", end="") // Statement 5
print(time.ctime())						// Statement 6

Output:

You can execute the above program with the help of InterviewBit’s Online Python compiler.

Snippet

Explanation:

  • Statement 1 corresponds to importing the time package. The sleep() method is defined under the same package.
  • Statement 2 corresponds to printing a string:
    “The time at which the code execution begins:”.
  • Statement 3 corresponds to the printing current time. We have used time.ctime() method for this purpose.
  • In statement 4, we have used the sleep() function. Since we have passed 8 as the parameter. Hence, the thread would go into a sleep state for 8 seconds.
  • In statement 5, we are printing a string:
    “The time at which the code execution ends:”.
  • Statement 6 corresponds to displaying the current time.
  • As we can notice clearly in the output there is a difference of 8 seconds between the two-time intervals.

Applications:

The sleep() function has many applications of which some have been listed below:

  • Execution of the background thread that repeats itself after a fixed interval of time.
  • It can be used to print letters of a string one by one for better visualization.
  • We can also use it for creating time delays in a list.
  • It can be used to delay printing items in a tuple.
    Let's try to implement the last application discussed above:

Example 1:

Source Code:

# Python program to demonstrate
# the application of sleep()
 
# import Time library
import time										// Statement 1
 
# Initialize a string
myString = "InterviewBit"								// Statement 2
 
# Print every letter one by one
# after an interval of three seconds  
for i in range(0, len(myString)):							// Statement 3
    print(myString[i], end="")								// Statement 4
    time.sleep(3)									// Statement 5

Output:

The effect of using sleep() can be seen in the following output:

You can execute the above program with the help of InterviewBit’s Online Python compiler.

Snippet

Explanation:

  • Statement 1 corresponds to importing the time package. The sleep() method is defined under the same package.
  • Statement 2 corresponds to initializing a string:
    “The time at which the code execution begins:”.
  • In statement 3 we are traversing over the length of the string using the for a loop.
  • In statement 4, we have printed the ith character of the string.
  • In statement 5, we have used the sleep() function. Since the parameter, we are passing is 3. Therefore, characters will be printed one after the other after an interval of 3 seconds.
  • You can see the visualization in the above output.

Example 2:

Source Code:

# Python program to demonstrate
# the application of sleep()

# Import time package
import time							// Statement 1

# creating and Initializing a list
myList = ['Harshit', 'Ram', 'Shivani', 'Jai', 'Naveen', 'Anil', 'Piyush', 'Jatin']	// Statement 2

# Creating a time delay of 3 seconds
time.sleep(3)							// Statement 3

# The above list will be displayed after a
# delay of 3 seconds
print(myList)							// Statement 4

Output:

You can execute the above program with the help of InterviewBit’s Online Python compiler.

Snippet

Explanation:

  • The statement 1 corresponds to importing time package. The sleep() method is defined under the same package.
  • In the statement 2, we have initialized a string.
  • In statement 3 we have used calling time.sleep() function. We have passed 3 as a parameter. Hence, a delay of three seconds of the current will be produced.
  • In statement 4, we are printing the list.
  • You can see the visualization in the above output.

Example 3:

Source Code:

# Python program to demonstrate
# the application of sleep() function
 
 
# Import time package
import time						// Statement 1
 
# Creating and Initializing a tuple
myTuple = ('Harshit', 'Ram', 'Shivani', 'Jai', 'Naveen', 'Anil', 'Piyush', 'Jatin')	// Statement 2
 
# Creating a time delay of 3 seconds
time.sleep(3)						// Statement 3
 
# The tuple will be displayed after a delay of 3 seconds
print(myTuple)						// Statement 4

Output:

You can execute the above program with the help of InterviewBit’s Online Python compiler.

Snippet

Explanation:

  • Statement 1 corresponds to importing the time package. The sleep() method is defined under the same package.
  • In statement 2, we have initialized a tuple.
  • In statement 3, we have used calling time.sleep() function. We have passed 3 as a parameter. Hence, a delay of three seconds of the current will be produced.
  • In statement 4, we are printing the myTuple. The tuple would be printed on the console after a delay of 3 seconds.
  • You can see the visualization in the above output.

Example 4:

Source Code:

# Python program to demonstrate
# the application of sleep()
 
# Importing time package
import time							// Statement 1
	
# Creating and Initializing a list
myList = ['Harshit', 'Ram', 'Shivani', 'Jai', 'Naveen', 'Anil', 'Piyush', 'Jatin']	// Statement 2
 
# Creating a time delay of 3 seconds
time.sleep(3)							// Statement 3
 
# the list will be displayed after the delay of 5 seconds
print(myList)							// Statement 4
 
for myListItem in myList:					// Statement 5
   
    # Creating a time delay of 6 seconds
    time.sleep(6)						// Statement 6
   
    # After an interval of 6 seconds an item of myList will be displayed
    print(myListItem)						// Statement 7

Output:

You can execute the above program with the help of InterviewBit’s Online Python compiler.

Snippet

Explanation:

  • Statement 1 corresponds to importing the time package. The sleep() method is defined under the same package.
  • In statement 2, we have initialized a string.
  • In statement 3, we have used the calling time.sleep() function. We have passed 3 as a parameter. Hence, a delay of three seconds of the current will be produced.
  • Statement 4 corresponds to the printing list to the console.
  • In statement 5, we are iterating over the list and at each iteration, we are producing a delay of 6 seconds and then printing the current list item.
  • You can see the visualization in the above output.

Example 5:

**Source Code:**

# Python program to demonstrate the application
# of sleep() function
 
# Import time package
import time					 // Statement 1
 
# Initializing a list
myList = ['Shivani', 'Bhuwanesh', 'Madhu','Neha', 'Anil', 'Saumya', 'Piyush', 'Ishi']                                         // Statement 2
 
# Creating a time delay of 3 seconds
# After every 3 seconds list item will be displayed
myList = [(time.sleep(3), print(myList)) for myListItem in myList] # Statement 3

Output:

You can execute the above program with the help of InterviewBit’s Online Python compiler.

Snippet

Explanation:

  • Statement 1 corresponds to importing the time package. The sleep() method is defined under the same package.
  • In statement 2, we have initialized a list of strings.
  • In statement 3, using list comprehension we are printing each list item after an interval of 3 seconds. For this purpose, we have used time.sleep() method.

Summary:

In this article, we learned about the basics of the time package and sleep() method. Then we discussed the applications of time.sleep() method along with their implementation.

Conclusion:

Congratulations! We have moved one step forward in learning Python functions. I hope it will help you to become a better Python developer.

Discussion