Python Certification Training for Data Scienc ...
- 61k Enrolled Learners
- Live Class
Today, everywhere you look around you will find applications. While all these applications have been coded in various different programming languages, one of the most popular till date is the Python programming language. In this article, we will know more about the famous module, time sleep in Python in the following sequence:
In day to day programming there often comes the need of pausing a program in between so that other operations can take place. While halting a program in between can serve a specific purpose, it can also simply add to the efficiency of the entire operation. Whatever might be the need, the sleep() module in Python can be used to achieve this.
The use of the sleep() module provides an accurate and flexible way to do this. In the latest versions of Python that is Python 2 and 3, the sleep module has been replaced with the time module, and both of them serve the same function.
In the above syntax, sec is used to define the number of seconds for which the execution will be halted or paused.
In order to understand the use of the sleep function better, take a look at the example below:
# Python code to demonstrate # working of sleep() import time # printing the start time print("The time of code execution begin is : ", end ="") print(time.ctime()) # using sleep() to hault the code execution time.sleep(6) # printing the end time print("The time of code execution end is : ", end ="") print(time.ctime())
Let us take another example to understand how the delay function works in Python:
import time print("Printed immediately.") time.sleep(2.4) print("Printed after 2.4 seconds.")
In the above program, the first string is printed immediately, followed by the second string which is printed after a delay of 2.4 seconds as mentioned in the time.sleep module.
Similar to all other modules present within the Python interface, the sleep function serves a multitude of applications. One of the most significant uses of the sleep function can be to execute a background thread at regular intervals. Another great use of the sleep function can be to print a string letter by letter, for better user experience.
To understand this application better, take a look at the example below:
# Python code to demonstrate # application of sleep() import time # initializing string strn = "Edureka says Hello!" # printing geeksforgeeks after delay # of each character for i in range(0, len(strn)): print(strn[i], end ="") time.sleep(2)
Let us take a look at another example, where we create a digital clock by using the time.sleep module in Python:
import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result) time.sleep(1)
If you see the above program, you will find that we have printed the local time multiple times inside the infinite while loop and this has been achieved through the time.sleep function. After the first iteration, the program waits for 1 second, computes the local time and then prints it, and this repetition is computed infinite times unless prompted to stop.
Mentioned below is a slightly modified version of the above program:
import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result, end="", flush=True) print("r", end="", flush=True) time.sleep(1)
The time and sleep module can be used in multithreaded python programs as well to achieve certain outcomes. The main difference between its uses in single-threaded and multithreaded programs is the fact that in single-threaded programs, the sleep function suspends execution of the thread as well as the process. On the other hand, in multithreaded programs a single thread rather than the whole process is suspended.
To understand this concept better, take a look at the example below:
import threading import time def print_Edureka(): for i in range(4): time.sleep(0.5) print("Edureka ") def print_Python(): for i in range(4): time.sleep(0.7) print("Python") t1 = threading.Thread(target=print_ Edureka) t2 = threading.Thread(target=print_ Python) t1.start() t2.start()
In the above program, there are two threads each with a delay of 0.5 and 0.75 seconds respectively. These are executed simultaneously when the program is run in the interpreter without halting the entire process.
The time and sleep modules in Python can be used to achieve a lot of different purposes. From the above examples, we hope that you have learned their individual functions, differences and how you can use them in your day to day usage.
Now that you have understood what is Python, check out the Python Programming Certification by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of this “Time Sleep in Python” blog and we will get back to you as soon as possible.