Creating Countdown Timer in Python (Source Code)

In this blog post, we will be discussing how to create a countdown timer in Python. For those unfamiliar with Python, it is a versatile scripting language that is widely used in many different applications. We will be using the built-in time module to create our timer.

Creating the Timer

The first thing we need to do is import the time module. We will also be using the sys module so that we can exit our program when the timer is finished.

import time import sys
Code language: JavaScript (javascript)

Next, we will define our countdown timer function. We will set our target time in minutes and have our timer print out the remaining time every second until it reaches 0. To do this, we will use a while loop and the sleep function from the time module. The code for this is shown below.

def countdown(t): while t: mins, secs = divmod(t, 60) timeformat = '{:02d}:{:02d}'.format(mins, secs) print(timeformat, end='\r') time.sleep(1) t -= 1 print('Goodbye!') sys.exit() # This exits the program once the timer reaches 0 # Now let's put everything together and run our countdown timer! We will set our target time to 2 minutes (120 seconds). if name == "__main__": t = 120 # Set target time in seconds. countdown(t)
Code language: Python (python)

Another Countdown Clock and Timer

A simple timer that can be used to keep track of runtime. The goal is to make it easy and standard to keep track of runtime.

  • This is a great way to test implementations and keep track of the progress of programs that run for a long time.
  • When the time is up, the app will let the user know.

Full Source code, Creating Countdown Clock and Timer

# import modules like 'os' and 'time' import os import time os.system('clear') # using ctime() to show present time times = time.ctime() print("\nCurrent Time: ", times) print( "\n Welcome to CountdownTimer!\n\n Let's set up the countdown timer...\n" ) # User input for the timer hours = int(input(" How many hours? ")) minutes = int(input(" How many minutes? ")) seconds = int(input(" How many seconds? ")) # To display message when the given value is not a number if hours or minutes or seconds == "": print("\n Invalid entry. You must enter a number.") # Conversion of hours amd minutes into seconds hrsToSec = (hours * 60) * 60 mnsToSec = (minutes * 60) seconds = seconds seconds = hrsToSec + mnsToSec + seconds print("\n Timer has been set for " + str(seconds) + " seconds.") # Loop for displaying the timer for i in range(seconds, -1, -1): displayHours = int(seconds / 3600) displayMinutes = int(seconds / 60) if displayMinutes >= 60: displayMinutes = displayMinutes - (displayHours * 60) else: displayMinutes = displayMinutes displaySeconds = int(seconds % 60) print("\n Your time remaining is: {}:{}:{}".format( str(displayHours).zfill(2), str(displayMinutes).zfill(2), str(displaySeconds).zfill(2))) seconds -= 1 time.sleep(1) # delays in the excution of a program for 1 second print("\n Time is over.")
Code language: Python (python)

Instructions to run this application

  1. Download and Run the
  2. Set the countdown time.
  3. Enter the hours, minutes and seconds .
  4. Timer will be set according to the given input by the user.
  5. It will show the countdown time and display it when the time gets over.

Example Output

Output will be shown like that for the three seconds –

00:00:03 00:00:02 00:00:01 00:00:00 'Time is over'
Code language: JavaScript (javascript)

Creating a countdown timer in Python is a relatively simple task that can be accomplished in just a few lines of code. In this blog post, we walked through how to do just that using the built-in time module. Try running the code yourself and see how it works!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts