Updated March 27, 2023
Introduction to Python Countdown Timer
What do you do if you require to run a certain piece of code after a specific interval of time? Suppose you need to refresh a file every 30 minutes. Will, you manually submit the same every time. Not Exactly, Right? This is where Python Countdown Timer comes into the picture as the same can be utilized to wait for a certain duration of time in the idle state before reiterating the same piece of code in the loop again as required.
How does a Python Countdown Timer Works?
To build the “Python Countdown Timer”, We need to import the time library using the import statement like below:
import time as t
Here we have specified a short name for the library by creating an alias for the same using the “as” keyword.
In Short, Aliasing the Python library gives us the capability of utilizing this shorthand notation to call the time class as:
t.function_name()<\code>
Python’s time library contains a predefined sleep() function, which can be called using the below Syntax:
time.sleep(duration)
The “duration” for which we want to delay the execution is passed as an argument to the sleep() function in seconds. The “duration” can be a more precise floating number as well instead of an integer.
The delay duration can be more than that specified due to any other activity scheduled in the system. After Python 3.5, the sleep() function provides a delay of at least the specified duration even if it’s being interrupted by a signal.
Examples of Python Countdown Timer
Let’s try to understand the same with the help of an example:
Example #1
let’s make a countdown timer function in Python
We need to import the time library
Code
import time as t
##this will enable to utilize specified functions within time library such as sleep()
##Asking user the duration for which the user wants to delay the process
seconds = int(input("How many seconds to wait"))
##Let's use a ranged loop to create the counter
for i in range(seconds):
print(str(seconds-i) + " seconds remaining \n")
##we also need the loop to wait for 1 second between each iteration
t.sleep(1)
print("Time is up")
Output:
PS: The program with wait for 1 second before printing the line “i second remaining.”
How does it work?
- The program starts off with a user inputting the duration.
- Notice, We have used the int() function for type conversion as the input function converts the info provided by the user into s string before saving it to the specified variable (seconds)
- A for loop is used to repeat the same operation the required number of times. Here we are printing the text after a delay of 1 second by using the sleep() function.
- That’s why each line is printed after a delay of 1 second, and that’s how we can create “Python Countdown Timer” by utilizing the functions available in Python’s time library.
Example #2
Here we will build a stopwatch similar to the one we have on mobile phones. Yes, Instead of each iteration getting printed in the subsequent line.
Here we are going to update the counter in a dynamic manner.
Code:
#Import time library
import time as t
## Countdown function starts here
def stopwatch(sec):
while sec:
minn, secc = divmod(sec, 60)
timeformat = '{:02d}:{:02d}'.format(minn, secc)
print(timeformat, end='\r')
t.sleep(1)
sec -= 1
print('Goodbye!\n')
## calling stopwatch function
stopwatch(15)
Output:
How Does This work?
- The program starts off with an importing time library, creating the stopwatch function wherein the actual logic resides.
- A while loop is used to repeat the same operation the required number of times. Here we are printing the text after a delay of 1 second by using the sleep() function.
- That’s why after a delay of 1 second, the stopwatch is updated by utilizing the functions available in Python’s time library.
Let’s talk more about the functions utilized in this example:
- divmod()function is utilized to calculate the #minutes and #seconds
- Thereafter we are formatting the minutes and seconds using the format() function.
- By incorporating end=’\r’ as an argument in the print() function enables the cursor to go back and to the initial position on the screen an start from there itself. This provides a perspective or an illusion that only the last second digit is changing dynamically in this program.
- We are printing the text after a delay of 1 second by using the sleep() function, & that’s how it behaves like a stopwatch.
Example #3
Let’s take up one more example:
Code:
def countdown(tme):
import time
print('This window will remain open for 3 more seconds...')
while tme >= 0:
print(tme, end=' . . . ')
time.sleep(1)
tme -= 1
print('Goodbye!!!! \n')
tme=3
countdown(tme)
Output:
Conclusion
Python Countdown Timer can be utilized to wait for a certain duration of time in the idle state before reiterating the same piece of code in the loop again as required. Python’s time library contains a predefined sleep() function. The “duration” for which we want to delay the execution is passed as an argument to the sleep() function in seconds. This function, in combination with a loop, serves as the Python countdown timer.
Recommended Articles
This is a guide to Python Countdown Timer. Here we discuss how do the Python Countdown Timer Works along with the respective examples. You can also go through our other related articles to learn more–