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 input()
Python Program to Split a List Into Evenly Sized Chunks
Python Set add()
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Find HCF or GCD
Python pass statement
Python frozenset()
Python String split()
Python Program to Get a Substring of a String
Python sleep()
Python pow()
Python String partition()
Python String translate()
Machine Learning with Python for everyone - Mark E.Fenner
Python next()
Python Set issuperset()
Python Decorators
Python Strings
Python Program to Check if a Key is Already Present in a Dictionary
Python del Statement
Python String isupper()
Python Tuple
Python Program to Count the Number of Each Vowel
Python String rfind()
Python List extend()
Python timestamp to datetime and vice-versa
Python Program to Check If a String Is a Number (Float)
Python Program to Merge Two Dictionaries
Python Set clear()
Python tuple()
Python Program to Count the Number of Digits Present In a Number
Python time Module