In this example, you will learn to create a countdown timer.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python while Loop
- Python divmod()
- Python time Module
Countdown time in Python
import time def countdown(time_sec): while time_sec: mins, secs = divmod(time_sec, 60) timeformat = '{:02d}:{:02d}'.format(mins, secs) print(timeformat, end='\r') time.sleep(1) time_sec -= 1 print("stop") countdown(5)
- The
divmod()
method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder. end='\r'
overwrites the output for each iteration.- The value of
time_sec
is decremented at the end of each iteration.
Related posts:
Python Set update()
Python Program to Get the Last Element of the List
Python super()
Python Set intersection()
Python Set remove()
Python Dictionary setdefault()
Python Dictionary get()
Python Program to Count the Number of Each Vowel
Python Set clear()
Python hasattr()
Python sorted()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Custom Exceptions
Python pass statement
Python issubclass()
Python hash()
Python Program to Concatenate Two Lists
Python Program to Sort Words in Alphabetic Order
Python vars()
Python Statement, Indentation and Comments
Python Program to Find ASCII Value of Character
Python Set pop()
Python help()
Python hex()
Python setattr()
Python Set intersection_update()
Python Program to Find the Square Root
Python RegEx
Python min()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Remove Punctuations From a String